blob: 0e366a31f63b1a9f4ab66df313ee6d4230dad93a [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"
Chuck Lever006ea732006-03-20 13:44:14 -050054#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_PROC
57
Trond Myklebust2066fe82006-09-15 08:30:46 -040058#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFS4_POLL_RETRY_MAX (15*HZ)
60
Trond Myklebustcdd4e682006-01-03 09:55:12 +010061struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010062static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070065static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
David Howellsadfa6f92006-08-22 20:06:08 -040066static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
Trond Myklebust99367812007-07-17 21:52:41 -040067static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
68static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Prevent leaks of NFSv4 errors into userland */
71int nfs4_map_errors(int err)
72{
73 if (err < -1000) {
74 dprintk("%s could not handle NFSv4 error %d\n",
75 __FUNCTION__, -err);
76 return -EIO;
77 }
78 return err;
79}
80
81/*
82 * This is our standard bitmap for GETATTR requests.
83 */
84const u32 nfs4_fattr_bitmap[2] = {
85 FATTR4_WORD0_TYPE
86 | FATTR4_WORD0_CHANGE
87 | FATTR4_WORD0_SIZE
88 | FATTR4_WORD0_FSID
89 | FATTR4_WORD0_FILEID,
90 FATTR4_WORD1_MODE
91 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER
93 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_RAWDEV
95 | FATTR4_WORD1_SPACE_USED
96 | FATTR4_WORD1_TIME_ACCESS
97 | FATTR4_WORD1_TIME_METADATA
98 | FATTR4_WORD1_TIME_MODIFY
99};
100
101const u32 nfs4_statfs_bitmap[2] = {
102 FATTR4_WORD0_FILES_AVAIL
103 | FATTR4_WORD0_FILES_FREE
104 | FATTR4_WORD0_FILES_TOTAL,
105 FATTR4_WORD1_SPACE_AVAIL
106 | FATTR4_WORD1_SPACE_FREE
107 | FATTR4_WORD1_SPACE_TOTAL
108};
109
Trond Myklebust4ce79712005-06-22 17:16:21 +0000110const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 FATTR4_WORD0_MAXLINK
112 | FATTR4_WORD0_MAXNAME,
113 0
114};
115
116const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
117 | FATTR4_WORD0_MAXREAD
118 | FATTR4_WORD0_MAXWRITE
119 | FATTR4_WORD0_LEASE_TIME,
120 0
121};
122
Manoj Naik830b8e32006-06-09 09:34:25 -0400123const u32 nfs4_fs_locations_bitmap[2] = {
124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID
129 | FATTR4_WORD0_FS_LOCATIONS,
130 FATTR4_WORD1_MODE
131 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER
133 | FATTR4_WORD1_OWNER_GROUP
134 | FATTR4_WORD1_RAWDEV
135 | FATTR4_WORD1_SPACE_USED
136 | FATTR4_WORD1_TIME_ACCESS
137 | FATTR4_WORD1_TIME_METADATA
138 | FATTR4_WORD1_TIME_MODIFY
139 | FATTR4_WORD1_MOUNTED_ON_FILEID
140};
141
Al Virobc4785c2006-10-19 23:28:51 -0700142static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct nfs4_readdir_arg *readdir)
144{
Al Viro0dbb4c62006-10-19 23:28:49 -0700145 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 BUG_ON(readdir->count < 80);
148 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000149 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
151 return;
152 }
153
154 readdir->cookie = 0;
155 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
156 if (cookie == 2)
157 return;
158
159 /*
160 * NFSv4 servers do not return entries for '.' and '..'
161 * Therefore, we fake these entries here. We let '.'
162 * have cookie 0 and '..' have cookie 1. Note that
163 * when talking to the server, we always send cookie 0
164 * instead of 1 or 2.
165 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700166 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (cookie == 0) {
169 *p++ = xdr_one; /* next */
170 *p++ = xdr_zero; /* cookie, first word */
171 *p++ = xdr_one; /* cookie, second word */
172 *p++ = xdr_one; /* entry len */
173 memcpy(p, ".\0\0\0", 4); /* entry */
174 p++;
175 *p++ = xdr_one; /* bitmap length */
176 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
177 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400178 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_two; /* cookie, second word */
184 *p++ = xdr_two; /* entry len */
185 memcpy(p, "..\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 readdir->pgbase = (char *)p - (char *)start;
193 readdir->count -= readdir->pgbase;
194 kunmap_atomic(start, KM_USER0);
195}
196
Trond Myklebust26e976a2006-01-03 09:55:21 +0100197static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
David Howells7539bba2006-08-22 20:06:09 -0400199 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 spin_lock(&clp->cl_lock);
201 if (time_before(clp->cl_last_renewal,timestamp))
202 clp->cl_last_renewal = timestamp;
203 spin_unlock(&clp->cl_lock);
204}
205
Trond Myklebust38478b22006-05-25 01:40:57 -0400206static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Trond Myklebust38478b22006-05-25 01:40:57 -0400208 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Trond Myklebust38478b22006-05-25 01:40:57 -0400210 spin_lock(&dir->i_lock);
211 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
213 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400214 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100217struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400218 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100219 struct nfs_openargs o_arg;
220 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100221 struct nfs_open_confirmargs c_arg;
222 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100223 struct nfs_fattr f_attr;
224 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400225 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100226 struct dentry *dir;
227 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400228 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100229 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100230 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400231 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100232 int rpc_status;
233 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100234};
235
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400236
237static void nfs4_init_opendata_res(struct nfs4_opendata *p)
238{
239 p->o_res.f_attr = &p->f_attr;
240 p->o_res.dir_attr = &p->dir_attr;
241 p->o_res.server = p->o_arg.server;
242 nfs_fattr_init(&p->f_attr);
243 nfs_fattr_init(&p->dir_attr);
244}
245
Trond Myklebustad389da2007-06-05 12:30:00 -0400246static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100247 struct nfs4_state_owner *sp, int flags,
248 const struct iattr *attrs)
249{
Trond Myklebustad389da2007-06-05 12:30:00 -0400250 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100251 struct inode *dir = parent->d_inode;
252 struct nfs_server *server = NFS_SERVER(dir);
253 struct nfs4_opendata *p;
254
255 p = kzalloc(sizeof(*p), GFP_KERNEL);
256 if (p == NULL)
257 goto err;
258 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
259 if (p->o_arg.seqid == NULL)
260 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400261 p->path.mnt = mntget(path->mnt);
262 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100263 p->dir = parent;
264 p->owner = sp;
265 atomic_inc(&sp->so_count);
266 p->o_arg.fh = NFS_FH(dir);
267 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400268 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400269 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400270 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100271 p->o_arg.server = server;
272 p->o_arg.bitmask = server->attr_bitmask;
273 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100274 if (flags & O_EXCL) {
275 u32 *s = (u32 *) p->o_arg.u.verifier.data;
276 s[0] = jiffies;
277 s[1] = current->pid;
278 } else if (flags & O_CREAT) {
279 p->o_arg.u.attrs = &p->attrs;
280 memcpy(&p->attrs, attrs, sizeof(p->attrs));
281 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100282 p->c_arg.fh = &p->o_res.fh;
283 p->c_arg.stateid = &p->o_res.stateid;
284 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400285 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400286 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100287 return p;
288err_free:
289 kfree(p);
290err:
291 dput(parent);
292 return NULL;
293}
294
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400295static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100296{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400297 struct nfs4_opendata *p = container_of(kref,
298 struct nfs4_opendata, kref);
299
300 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400301 if (p->state != NULL)
302 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400303 nfs4_put_state_owner(p->owner);
304 dput(p->dir);
305 dput(p->path.dentry);
306 mntput(p->path.mnt);
307 kfree(p);
308}
309
310static void nfs4_opendata_put(struct nfs4_opendata *p)
311{
312 if (p != NULL)
313 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100314}
315
Trond Myklebust06f814a2006-01-03 09:55:07 +0100316static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
317{
318 sigset_t oldset;
319 int ret;
320
321 rpc_clnt_sigmask(task->tk_client, &oldset);
322 ret = rpc_wait_for_completion_task(task);
323 rpc_clnt_sigunmask(task->tk_client, &oldset);
324 return ret;
325}
326
Trond Myklebust6ee41262007-07-08 14:11:36 -0400327static int can_open_cached(struct nfs4_state *state, int mode)
328{
329 int ret = 0;
330 switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
331 case FMODE_READ:
332 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400333 break;
334 case FMODE_WRITE:
335 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400336 break;
337 case FMODE_READ|FMODE_WRITE:
338 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
339 }
340 return ret;
341}
342
Trond Myklebustaac00a82007-07-05 19:02:21 -0400343static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
344{
345 if ((delegation->type & open_flags) != open_flags)
346 return 0;
347 if (delegation->flags & NFS_DELEGATION_NEED_RECLAIM)
348 return 0;
349 return 1;
350}
351
Trond Myklebust003707c2007-07-05 18:07:55 -0400352static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
Trond Myklebuste7616922006-01-03 09:55:13 +0100353{
354 switch (open_flags) {
355 case FMODE_WRITE:
356 state->n_wronly++;
357 break;
358 case FMODE_READ:
359 state->n_rdonly++;
360 break;
361 case FMODE_READ|FMODE_WRITE:
362 state->n_rdwr++;
363 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400364 nfs4_state_set_mode_locked(state, state->state | open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100365}
366
Trond Myklebust003707c2007-07-05 18:07:55 -0400367static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
368{
369 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
370 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
371 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
372 switch (open_flags) {
373 case FMODE_READ:
374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
375 break;
376 case FMODE_WRITE:
377 set_bit(NFS_O_WRONLY_STATE, &state->flags);
378 break;
379 case FMODE_READ|FMODE_WRITE:
380 set_bit(NFS_O_RDWR_STATE, &state->flags);
381 }
382}
383
384static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
385{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400386 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400387 nfs_set_open_stateid_locked(state, stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400388 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400389}
390
391static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400394 /*
395 * Protect the call to nfs4_state_set_mode_locked and
396 * serialise the stateid update
397 */
398 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400399 if (deleg_stateid != NULL) {
400 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
401 set_bit(NFS_DELEGATED_STATE, &state->flags);
402 }
403 if (open_stateid != NULL)
404 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400405 write_sequnlock(&state->seqlock);
406 spin_lock(&state->owner->so_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100407 update_open_stateflags(state, open_flags);
Trond Myklebustec073422005-10-20 14:22:47 -0700408 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Trond Myklebustaac00a82007-07-05 19:02:21 -0400411static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
412{
413 struct nfs_delegation *delegation;
414
415 rcu_read_lock();
416 delegation = rcu_dereference(NFS_I(inode)->delegation);
417 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
418 rcu_read_unlock();
419 return;
420 }
421 rcu_read_unlock();
422 nfs_inode_return_delegation(inode);
423}
424
Trond Myklebust6ee41262007-07-08 14:11:36 -0400425static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400426{
427 struct nfs4_state *state = opendata->state;
428 struct nfs_inode *nfsi = NFS_I(state->inode);
429 struct nfs_delegation *delegation;
430 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
431 nfs4_stateid stateid;
432 int ret = -EAGAIN;
433
434 rcu_read_lock();
435 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400436 for (;;) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400437 if (can_open_cached(state, open_mode)) {
438 spin_lock(&state->owner->so_lock);
439 if (can_open_cached(state, open_mode)) {
440 update_open_stateflags(state, open_mode);
441 spin_unlock(&state->owner->so_lock);
442 rcu_read_unlock();
443 goto out_return_state;
444 }
445 spin_unlock(&state->owner->so_lock);
446 }
447 if (delegation == NULL)
448 break;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400449 if (!can_open_delegated(delegation, open_mode))
450 break;
451 /* Save the delegation */
452 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
453 rcu_read_unlock();
454 lock_kernel();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400455 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400456 unlock_kernel();
457 if (ret != 0)
458 goto out;
459 ret = -EAGAIN;
460 rcu_read_lock();
461 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400462 /* If no delegation, try a cached open */
Trond Myklebustaac00a82007-07-05 19:02:21 -0400463 if (delegation == NULL)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400464 continue;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400465 /* Is the delegation still valid? */
466 if (memcmp(stateid.data, delegation->stateid.data, sizeof(stateid.data)) != 0)
467 continue;
468 rcu_read_unlock();
469 update_open_stateid(state, NULL, &stateid, open_mode);
470 goto out_return_state;
471 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400472 rcu_read_unlock();
473out:
474 return ERR_PTR(ret);
475out_return_state:
476 atomic_inc(&state->count);
477 return state;
478}
479
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100480static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
481{
482 struct inode *inode;
483 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400484 struct nfs_delegation *delegation;
485 nfs4_stateid *deleg_stateid = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400486 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100487
Trond Myklebustaac00a82007-07-05 19:02:21 -0400488 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400489 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400490 goto out;
491 }
492
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400493 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100494 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400495 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100496 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400497 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500498 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400499 goto err;
500 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100501 state = nfs4_get_open_state(inode, data->owner);
502 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400503 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400504 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400505 int delegation_flags = 0;
506
Trond Myklebust003707c2007-07-05 18:07:55 -0400507 rcu_read_lock();
508 delegation = rcu_dereference(NFS_I(inode)->delegation);
509 if (delegation)
510 delegation_flags = delegation->flags;
511 rcu_read_unlock();
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400512 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
513 nfs_inode_set_delegation(state->inode,
514 data->owner->so_cred,
515 &data->o_res);
516 else
517 nfs_inode_reclaim_delegation(state->inode,
518 data->owner->so_cred,
519 &data->o_res);
520 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400521 rcu_read_lock();
522 delegation = rcu_dereference(NFS_I(inode)->delegation);
523 if (delegation != NULL)
524 deleg_stateid = &delegation->stateid;
525 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
526 rcu_read_unlock();
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100527 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400528out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100529 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400530err_put_inode:
531 iput(inode);
532err:
533 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100534}
535
Trond Myklebust864472e2006-01-03 09:55:15 +0100536static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
537{
538 struct nfs_inode *nfsi = NFS_I(state->inode);
539 struct nfs_open_context *ctx;
540
541 spin_lock(&state->inode->i_lock);
542 list_for_each_entry(ctx, &nfsi->open_files, list) {
543 if (ctx->state != state)
544 continue;
545 get_nfs_open_context(ctx);
546 spin_unlock(&state->inode->i_lock);
547 return ctx;
548 }
549 spin_unlock(&state->inode->i_lock);
550 return ERR_PTR(-ENOENT);
551}
552
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400553static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
554{
555 struct nfs4_opendata *opendata;
556
557 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
558 if (opendata == NULL)
559 return ERR_PTR(-ENOMEM);
560 opendata->state = state;
561 atomic_inc(&state->count);
562 return opendata;
563}
564
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400565static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100566{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400567 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100568 int ret;
569
570 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400571 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
572 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
573 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100574 ret = _nfs4_proc_open(opendata);
575 if (ret != 0)
576 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400577 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400578 if (IS_ERR(newstate))
579 return PTR_ERR(newstate);
580 nfs4_close_state(&opendata->path, newstate, openflags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400581 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100582 return 0;
583}
584
585static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
586{
Trond Myklebust864472e2006-01-03 09:55:15 +0100587 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100588 int ret;
589
590 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400591 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100592 smp_rmb();
593 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400594 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100595 if (ret != 0)
596 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400597 if (newstate != state)
598 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100599 }
600 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400601 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100602 if (ret != 0)
603 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400604 if (newstate != state)
605 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100606 }
607 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400608 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100609 if (ret != 0)
610 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400611 if (newstate != state)
612 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100613 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400614 /*
615 * We may have performed cached opens for all three recoveries.
616 * Check if we need to update the current stateid.
617 */
618 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
619 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400620 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400621 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
622 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400623 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400624 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100625 return 0;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * OPEN_RECLAIM:
630 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400632static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400634 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100635 struct nfs4_opendata *opendata;
636 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int status;
638
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400639 opendata = nfs4_open_recoverdata_alloc(ctx, state);
640 if (IS_ERR(opendata))
641 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100642 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
643 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400644 rcu_read_lock();
645 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
646 if (delegation != NULL && (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400647 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400648 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100649 opendata->o_arg.u.delegation_type = delegation_type;
650 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400651 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return status;
653}
654
Trond Myklebust539cd032007-06-05 11:46:42 -0400655static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct nfs_server *server = NFS_SERVER(state->inode);
658 struct nfs4_exception exception = { };
659 int err;
660 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400661 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000662 if (err != -NFS4ERR_DELAY)
663 break;
664 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 } while (exception.retry);
666 return err;
667}
668
Trond Myklebust864472e2006-01-03 09:55:15 +0100669static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
670{
671 struct nfs_open_context *ctx;
672 int ret;
673
674 ctx = nfs4_state_find_open_context(state);
675 if (IS_ERR(ctx))
676 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400677 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100678 put_nfs_open_context(ctx);
679 return ret;
680}
681
Trond Myklebust13437e12007-07-06 15:10:43 -0400682static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100685 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400687 opendata = nfs4_open_recoverdata_alloc(ctx, state);
688 if (IS_ERR(opendata))
689 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100690 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400691 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100692 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100693 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400694 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Trond Myklebust13437e12007-07-06 15:10:43 -0400698int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400701 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int err;
703 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400704 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 switch (err) {
706 case 0:
707 return err;
708 case -NFS4ERR_STALE_CLIENTID:
709 case -NFS4ERR_STALE_STATEID:
710 case -NFS4ERR_EXPIRED:
711 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400712 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return err;
714 }
715 err = nfs4_handle_exception(server, err, &exception);
716 } while (exception.retry);
717 return err;
718}
719
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100720static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100722 struct nfs4_opendata *data = calldata;
723 struct rpc_message msg = {
724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
725 .rpc_argp = &data->c_arg,
726 .rpc_resp = &data->c_res,
727 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100729 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100730 rpc_call_setup(task, &msg, 0);
731}
732
733static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
734{
735 struct nfs4_opendata *data = calldata;
736
737 data->rpc_status = task->tk_status;
738 if (RPC_ASSASSINATED(task))
739 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100740 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100741 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
742 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100743 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400744 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100745 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100746 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400747 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100748}
749
750static void nfs4_open_confirm_release(void *calldata)
751{
752 struct nfs4_opendata *data = calldata;
753 struct nfs4_state *state = NULL;
754
755 /* If this request hasn't been cancelled, do nothing */
756 if (data->cancelled == 0)
757 goto out_free;
758 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400759 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 goto out_free;
761 nfs_confirm_seqid(&data->owner->so_seqid, 0);
762 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400763 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400764 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100765out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400766 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100767}
768
769static const struct rpc_call_ops nfs4_open_confirm_ops = {
770 .rpc_call_prepare = nfs4_open_confirm_prepare,
771 .rpc_call_done = nfs4_open_confirm_done,
772 .rpc_release = nfs4_open_confirm_release,
773};
774
775/*
776 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
777 */
778static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
779{
780 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
781 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int status;
783
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400784 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400785 data->rpc_done = 0;
786 data->rpc_status = 0;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100787 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500788 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100789 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100790 status = nfs4_wait_for_completion_rpc_task(task);
791 if (status != 0) {
792 data->cancelled = 1;
793 smp_wmb();
794 } else
795 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500796 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return status;
798}
799
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100800static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100802 struct nfs4_opendata *data = calldata;
803 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct rpc_message msg = {
805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100806 .rpc_argp = &data->o_arg,
807 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .rpc_cred = sp->so_cred,
809 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100810
811 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
812 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400813 /*
814 * Check if we still need to send an OPEN call, or if we can use
815 * a delegation instead.
816 */
817 if (data->state != NULL) {
818 struct nfs_delegation *delegation;
819
Trond Myklebust6ee41262007-07-08 14:11:36 -0400820 if (can_open_cached(data->state, data->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL)))
821 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 rcu_read_lock();
823 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
824 if (delegation != NULL &&
825 (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) {
826 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400827 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 }
829 rcu_read_unlock();
830 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100831 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400832 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100833 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400834 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100835 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400836 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
837 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100838 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100839 rpc_call_setup(task, &msg, 0);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400840 return;
841out_no_action:
842 task->tk_action = NULL;
843
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100846static void nfs4_open_done(struct rpc_task *task, void *calldata)
847{
848 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100850 data->rpc_status = task->tk_status;
851 if (RPC_ASSASSINATED(task))
852 return;
853 if (task->tk_status == 0) {
854 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700855 case S_IFREG:
856 break;
857 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100858 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700859 break;
860 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100861 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700862 break;
863 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100864 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700865 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100866 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400867 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
868 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700869 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100870 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
Trond Myklebust3e309912007-07-07 13:19:59 -0400871 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100872}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700873
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100874static void nfs4_open_release(void *calldata)
875{
876 struct nfs4_opendata *data = calldata;
877 struct nfs4_state *state = NULL;
878
879 /* If this request hasn't been cancelled, do nothing */
880 if (data->cancelled == 0)
881 goto out_free;
882 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400883 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100884 goto out_free;
885 /* In case we need an open_confirm, no cleanup! */
886 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
887 goto out_free;
888 nfs_confirm_seqid(&data->owner->so_seqid, 0);
889 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400890 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400891 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100892out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100894}
895
896static const struct rpc_call_ops nfs4_open_ops = {
897 .rpc_call_prepare = nfs4_open_prepare,
898 .rpc_call_done = nfs4_open_done,
899 .rpc_release = nfs4_open_release,
900};
901
902/*
903 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
904 */
905static int _nfs4_proc_open(struct nfs4_opendata *data)
906{
907 struct inode *dir = data->dir->d_inode;
908 struct nfs_server *server = NFS_SERVER(dir);
909 struct nfs_openargs *o_arg = &data->o_arg;
910 struct nfs_openres *o_res = &data->o_res;
911 struct rpc_task *task;
912 int status;
913
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400915 data->rpc_done = 0;
916 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400917 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100918 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500919 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100920 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100921 status = nfs4_wait_for_completion_rpc_task(task);
922 if (status != 0) {
923 data->cancelled = 1;
924 smp_wmb();
925 } else
926 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500927 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -0400928 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100929 return status;
930
Trond Myklebust99367812007-07-17 21:52:41 -0400931 if (o_res->fh.size == 0)
932 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
933
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400934 if (o_arg->open_flags & O_CREAT) {
935 update_changeattr(dir, &o_res->cinfo);
936 nfs_post_op_update_inode(dir, o_res->dir_attr);
937 } else
938 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100940 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100942 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -0400945 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Trond Myklebust10afec92007-05-14 17:16:04 -0400949static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100950{
David Howells7539bba2006-08-22 20:06:09 -0400951 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400952 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100953
Trond Myklebust6b309542006-09-14 14:03:14 -0400954 for (;;) {
955 ret = nfs4_wait_clnt_recover(server->client, clp);
956 if (ret != 0)
957 return ret;
958 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
959 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100960 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400961 }
962 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/*
966 * OPEN_EXPIRED:
967 * reclaim state on the server after a network partition.
968 * Assumes caller holds the appropriate lock
969 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400970static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100972 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100973 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400975 opendata = nfs4_open_recoverdata_alloc(ctx, state);
976 if (IS_ERR(opendata))
977 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100978 ret = nfs4_open_recover(opendata, state);
979 if (ret == -ESTALE) {
980 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400981 nfs4_drop_state_owner(state->owner);
982 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400984 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Trond Myklebust539cd032007-06-05 11:46:42 -0400988static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000989{
Trond Myklebust539cd032007-06-05 11:46:42 -0400990 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000991 struct nfs4_exception exception = { };
992 int err;
993
994 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400995 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000996 if (err == -NFS4ERR_DELAY)
997 nfs4_handle_exception(server, err, &exception);
998 } while (exception.retry);
999 return err;
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001005 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Trond Myklebust864472e2006-01-03 09:55:15 +01001007 ctx = nfs4_state_find_open_context(state);
1008 if (IS_ERR(ctx))
1009 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001010 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001011 put_nfs_open_context(ctx);
1012 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001016 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1017 * fields corresponding to attributes that were used to store the verifier.
1018 * Make sure we clobber those fields in the later setattr call
1019 */
1020static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1021{
1022 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1023 !(sattr->ia_valid & ATTR_ATIME_SET))
1024 sattr->ia_valid |= ATTR_ATIME;
1025
1026 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1027 !(sattr->ia_valid & ATTR_MTIME_SET))
1028 sattr->ia_valid |= ATTR_MTIME;
1029}
1030
1031/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001034static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct nfs4_state_owner *sp;
1037 struct nfs4_state *state = NULL;
1038 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -04001039 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 status = -ENOMEM;
1045 if (!(sp = nfs4_get_state_owner(server, cred))) {
1046 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1047 goto out_err;
1048 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001049 status = nfs4_recover_expired_lease(server);
1050 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001051 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 if (path->dentry->d_inode != NULL)
1053 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
Trond Myklebust58d97142006-01-03 09:55:24 +01001054 down_read(&clp->cl_sem);
1055 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001056 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001057 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -04001058 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 if (path->dentry->d_inode != NULL)
1061 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1062
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001063 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001065 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001067 if (opendata->o_arg.open_flags & O_EXCL)
1068 nfs4_exclusive_attrset(opendata, sattr);
1069
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001070 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 status = PTR_ERR(state);
1072 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001073 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001074 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 nfs4_put_state_owner(sp);
1076 up_read(&clp->cl_sem);
1077 *res = state;
1078 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001079err_opendata_put:
1080 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001081err_release_rwsem:
1082 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001083err_put_state_owner:
1084 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *res = NULL;
1087 return status;
1088}
1089
1090
Trond Myklebustad389da2007-06-05 12:30:00 -04001091static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct nfs4_exception exception = { };
1094 struct nfs4_state *res;
1095 int status;
1096
1097 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001098 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (status == 0)
1100 break;
1101 /* NOTE: BAD_SEQID means the server and client disagree about the
1102 * book-keeping w.r.t. state-changing operations
1103 * (OPEN/CLOSE/LOCK/LOCKU...)
1104 * It is actually a sign of a bug on the client or on the server.
1105 *
1106 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001107 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * have unhashed the old state_owner for us, and that we can
1109 * therefore safely retry using a new one. We should still warn
1110 * the user though...
1111 */
1112 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001113 printk(KERN_WARNING "NFS: v4 server %s "
1114 " returned a bad sequence-id error!\n",
1115 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 exception.retry = 1;
1117 continue;
1118 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001119 /*
1120 * BAD_STATEID on OPEN means that the server cancelled our
1121 * state before it received the OPEN_CONFIRM.
1122 * Recover by retrying the request as per the discussion
1123 * on Page 181 of RFC3530.
1124 */
1125 if (status == -NFS4ERR_BAD_STATEID) {
1126 exception.retry = 1;
1127 continue;
1128 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129 if (status == -EAGAIN) {
1130 /* We must have found a delegation */
1131 exception.retry = 1;
1132 continue;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1135 status, &exception));
1136 } while (exception.retry);
1137 return res;
1138}
1139
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001140static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1141 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001143 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001145 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 .iap = sattr,
1147 .server = server,
1148 .bitmask = server->attr_bitmask,
1149 };
1150 struct nfs_setattrres res = {
1151 .fattr = fattr,
1152 .server = server,
1153 };
1154 struct rpc_message msg = {
1155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1156 .rpc_argp = &arg,
1157 .rpc_resp = &res,
1158 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001159 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001160 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Trond Myklebust0e574af2005-10-27 22:12:38 -04001162 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001164 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1165 /* Use that stateid */
1166 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001168 nfs4_copy_stateid(&arg.stateid, state, current->files);
1169 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1171
Trond Myklebust65e43082005-08-16 11:49:44 -04001172 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001173 if (status == 0 && state != NULL)
1174 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001175 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001178static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1179 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001181 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct nfs4_exception exception = { };
1183 int err;
1184 do {
1185 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001186 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 &exception);
1188 } while (exception.retry);
1189 return err;
1190}
1191
1192struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001193 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct inode *inode;
1195 struct nfs4_state *state;
1196 struct nfs_closeargs arg;
1197 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001198 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001199 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200};
1201
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001202static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001203{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001204 struct nfs4_closedata *calldata = data;
1205 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001206
1207 nfs4_put_open_state(calldata->state);
1208 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001209 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001210 dput(calldata->path.dentry);
1211 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001212 kfree(calldata);
1213}
1214
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001215static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001217 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct nfs_server *server = NFS_SERVER(calldata->inode);
1220
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001221 if (RPC_ASSASSINATED(task))
1222 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* hmm. we are done with the inode, and in the process of freeing
1224 * the state_owner. we keep this around to process errors
1225 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001226 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 switch (task->tk_status) {
1228 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001229 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001230 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break;
1232 case -NFS4ERR_STALE_STATEID:
1233 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
1235 default:
1236 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1237 rpc_restart_call(task);
1238 return;
1239 }
1240 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001241 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001244static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001246 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001247 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct rpc_message msg = {
1249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1250 .rpc_argp = &calldata->arg,
1251 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001252 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 };
Trond Myklebust003707c2007-07-05 18:07:55 -04001254 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001255
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001256 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001257 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001258
Trond Myklebust003707c2007-07-05 18:07:55 -04001259 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001260 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001261 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001262 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001263 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001264 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1265 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1266 }
1267 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001268 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1269 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1270 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001271 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001272 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001273 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001274 /* Note: exit _without_ calling nfs4_close_done */
1275 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001276 return;
1277 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001278 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001279 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust45328c32007-07-26 17:47:34 -04001281 calldata->arg.open_flags = FMODE_READ;
1282 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1283 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1284 calldata->arg.open_flags = FMODE_WRITE;
1285 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001286 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001287 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001290static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001291 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001292 .rpc_call_done = nfs4_close_done,
1293 .rpc_release = nfs4_free_closedata,
1294};
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/*
1297 * It is possible for data to be read/written from a mem-mapped file
1298 * after the sys_close call (which hits the vfs layer as a flush).
1299 * This means that we can't safely call nfsv4 close on a file until
1300 * the inode is cleared. This in turn means that we are not good
1301 * NFSv4 citizens - we do not indicate to the server to update the file's
1302 * share state even when we are done with one of the three share
1303 * stateid's in the inode.
1304 *
1305 * NOTE: Caller must be holding the sp->so_owner semaphore!
1306 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001307int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001309 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001311 struct nfs4_state_owner *sp = state->owner;
1312 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001313 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Trond Myklebust95121352005-10-18 14:20:12 -07001315 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001317 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001318 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001320 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001321 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001323 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001324 if (calldata->arg.seqid == NULL)
1325 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001326 calldata->arg.bitmask = server->attr_bitmask;
1327 calldata->res.fattr = &calldata->fattr;
1328 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001329 calldata->path.mnt = mntget(path->mnt);
1330 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001331
Trond Myklebustb39e6252007-06-11 23:05:07 -04001332 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1333 if (IS_ERR(task))
1334 return PTR_ERR(task);
1335 rpc_put_task(task);
1336 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001337out_free_calldata:
1338 kfree(calldata);
1339out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001340 nfs4_put_open_state(state);
1341 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001342 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001345static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001346{
1347 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001348 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001349
Trond Myklebust1b45c462007-07-03 13:04:56 -04001350 /* If the open_intent is for execute, we have an extra check to make */
1351 if (nd->intent.open.flags & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001352 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001353 state->owner->so_cred,
1354 nd->intent.open.flags);
1355 if (ret < 0)
1356 goto out_close;
1357 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001358 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001359 if (!IS_ERR(filp)) {
1360 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001361 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001362 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001363 return 0;
1364 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001365 ret = PTR_ERR(filp);
1366out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001367 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001368 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001369}
1370
1371struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1373{
Trond Myklebustad389da2007-06-05 12:30:00 -04001374 struct path path = {
1375 .mnt = nd->mnt,
1376 .dentry = dentry,
1377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct iattr attr;
1379 struct rpc_cred *cred;
1380 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001381 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 if (nd->flags & LOOKUP_CREATE) {
1384 attr.ia_mode = nd->intent.open.create_mode;
1385 attr.ia_valid = ATTR_MODE;
1386 if (!IS_POSIXACL(dir))
1387 attr.ia_mode &= ~current->fs->umask;
1388 } else {
1389 attr.ia_valid = 0;
1390 BUG_ON(nd->intent.open.flags & O_CREAT);
1391 }
1392
David Howells1f163412006-08-22 20:06:11 -04001393 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001395 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001396 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001398 if (IS_ERR(state)) {
1399 if (PTR_ERR(state) == -ENOENT)
1400 d_add(dentry, NULL);
1401 return (struct dentry *)state;
1402 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001404 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001405 path.dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001406 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001407 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001411nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Trond Myklebustad389da2007-06-05 12:30:00 -04001413 struct path path = {
1414 .mnt = nd->mnt,
1415 .dentry = dentry,
1416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct rpc_cred *cred;
1418 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
David Howells1f163412006-08-22 20:06:11 -04001420 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (IS_ERR(cred))
1422 return PTR_ERR(cred);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001423 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001425 if (IS_ERR(state)) {
1426 switch (PTR_ERR(state)) {
1427 case -EPERM:
1428 case -EACCES:
1429 case -EDQUOT:
1430 case -ENOSPC:
1431 case -EROFS:
1432 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1433 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001434 default:
1435 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001436 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001437 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001439 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 1;
1441 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001442 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001443out_drop:
1444 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return 0;
1446}
1447
1448
1449static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1450{
1451 struct nfs4_server_caps_res res = {};
1452 struct rpc_message msg = {
1453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1454 .rpc_argp = fhandle,
1455 .rpc_resp = &res,
1456 };
1457 int status;
1458
1459 status = rpc_call_sync(server->client, &msg, 0);
1460 if (status == 0) {
1461 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1462 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1463 server->caps |= NFS_CAP_ACLS;
1464 if (res.has_links != 0)
1465 server->caps |= NFS_CAP_HARDLINKS;
1466 if (res.has_symlinks != 0)
1467 server->caps |= NFS_CAP_SYMLINKS;
1468 server->acl_bitmask = res.acl_bitmask;
1469 }
1470 return status;
1471}
1472
Trond Myklebust55a97592006-06-09 09:34:19 -04001473int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct nfs4_exception exception = { };
1476 int err;
1477 do {
1478 err = nfs4_handle_exception(server,
1479 _nfs4_server_capabilities(server, fhandle),
1480 &exception);
1481 } while (exception.retry);
1482 return err;
1483}
1484
1485static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1486 struct nfs_fsinfo *info)
1487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct nfs4_lookup_root_arg args = {
1489 .bitmask = nfs4_fattr_bitmap,
1490 };
1491 struct nfs4_lookup_res res = {
1492 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001493 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .fh = fhandle,
1495 };
1496 struct rpc_message msg = {
1497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1498 .rpc_argp = &args,
1499 .rpc_resp = &res,
1500 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001501 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return rpc_call_sync(server->client, &msg, 0);
1503}
1504
1505static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1506 struct nfs_fsinfo *info)
1507{
1508 struct nfs4_exception exception = { };
1509 int err;
1510 do {
1511 err = nfs4_handle_exception(server,
1512 _nfs4_lookup_root(server, fhandle, info),
1513 &exception);
1514 } while (exception.retry);
1515 return err;
1516}
1517
David Howells54ceac42006-08-22 20:06:13 -04001518/*
1519 * get the file handle for the "/" directory on the server
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001522 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int status;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (status == 0)
1528 status = nfs4_server_capabilities(server, fhandle);
1529 if (status == 0)
1530 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001531 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Manoj Naik6b97fd32006-06-09 09:34:29 -04001534/*
1535 * Get locations and (maybe) other attributes of a referral.
1536 * Note that we'll actually follow the referral later when
1537 * we detect fsid mismatch in inode revalidation
1538 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001539static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04001540{
1541 int status = -ENOMEM;
1542 struct page *page = NULL;
1543 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001544
1545 page = alloc_page(GFP_KERNEL);
1546 if (page == NULL)
1547 goto out;
1548 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1549 if (locations == NULL)
1550 goto out;
1551
Trond Myklebustc228fd32007-01-13 02:28:11 -05001552 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001553 if (status != 0)
1554 goto out;
1555 /* Make sure server returned a different fsid for the referral */
1556 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1557 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1558 status = -EIO;
1559 goto out;
1560 }
1561
1562 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1563 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1564 if (!fattr->mode)
1565 fattr->mode = S_IFDIR;
1566 memset(fhandle, 0, sizeof(struct nfs_fh));
1567out:
1568 if (page)
1569 __free_page(page);
1570 if (locations)
1571 kfree(locations);
1572 return status;
1573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1576{
1577 struct nfs4_getattr_arg args = {
1578 .fh = fhandle,
1579 .bitmask = server->attr_bitmask,
1580 };
1581 struct nfs4_getattr_res res = {
1582 .fattr = fattr,
1583 .server = server,
1584 };
1585 struct rpc_message msg = {
1586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1587 .rpc_argp = &args,
1588 .rpc_resp = &res,
1589 };
1590
Trond Myklebust0e574af2005-10-27 22:12:38 -04001591 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return rpc_call_sync(server->client, &msg, 0);
1593}
1594
1595static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1596{
1597 struct nfs4_exception exception = { };
1598 int err;
1599 do {
1600 err = nfs4_handle_exception(server,
1601 _nfs4_proc_getattr(server, fhandle, fattr),
1602 &exception);
1603 } while (exception.retry);
1604 return err;
1605}
1606
1607/*
1608 * The file is not closed if it is opened due to the a request to change
1609 * the size of the file. The open call will not be needed once the
1610 * VFS layer lookup-intents are implemented.
1611 *
1612 * Close is called when the inode is destroyed.
1613 * If we haven't opened the file for O_WRONLY, we
1614 * need to in the size_change case to obtain a stateid.
1615 *
1616 * Got race?
1617 * Because OPEN is always done by name in nfsv4, it is
1618 * possible that we opened a different file by the same
1619 * name. We can recognize this race condition, but we
1620 * can't do anything about it besides returning an error.
1621 *
1622 * This will be fixed with VFS changes (lookup-intent).
1623 */
1624static int
1625nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1626 struct iattr *sattr)
1627{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001628 struct rpc_cred *cred;
1629 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001630 struct nfs_open_context *ctx;
1631 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 int status;
1633
Trond Myklebust0e574af2005-10-27 22:12:38 -04001634 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
David Howells1f163412006-08-22 20:06:11 -04001636 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001637 if (IS_ERR(cred))
1638 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001639
1640 /* Search for an existing open(O_WRITE) file */
1641 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1642 if (ctx != NULL)
1643 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001644
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001645 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001646 if (status == 0)
1647 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001648 if (ctx != NULL)
1649 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001650 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return status;
1652}
1653
Trond Myklebust56659e92007-07-17 21:52:39 -04001654static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1655 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001656 struct nfs_fattr *fattr)
1657{
1658 int status;
1659 struct nfs4_lookup_arg args = {
1660 .bitmask = server->attr_bitmask,
1661 .dir_fh = dirfh,
1662 .name = name,
1663 };
1664 struct nfs4_lookup_res res = {
1665 .server = server,
1666 .fattr = fattr,
1667 .fh = fhandle,
1668 };
1669 struct rpc_message msg = {
1670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1671 .rpc_argp = &args,
1672 .rpc_resp = &res,
1673 };
1674
1675 nfs_fattr_init(fattr);
1676
1677 dprintk("NFS call lookupfh %s\n", name->name);
1678 status = rpc_call_sync(server->client, &msg, 0);
1679 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001680 return status;
1681}
1682
1683static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1684 struct qstr *name, struct nfs_fh *fhandle,
1685 struct nfs_fattr *fattr)
1686{
1687 struct nfs4_exception exception = { };
1688 int err;
1689 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001690 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1691 /* FIXME: !!!! */
1692 if (err == -NFS4ERR_MOVED) {
1693 err = -EREMOTE;
1694 break;
1695 }
1696 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001697 } while (exception.retry);
1698 return err;
1699}
1700
Trond Myklebust56659e92007-07-17 21:52:39 -04001701static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1703{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001704 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001707 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001708 if (status == -NFS4ERR_MOVED)
1709 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 dprintk("NFS reply lookup: %d\n", status);
1711 return status;
1712}
1713
1714static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1715{
1716 struct nfs4_exception exception = { };
1717 int err;
1718 do {
1719 err = nfs4_handle_exception(NFS_SERVER(dir),
1720 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1721 &exception);
1722 } while (exception.retry);
1723 return err;
1724}
1725
1726static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1727{
Trond Myklebust76b32992007-08-10 17:45:11 -04001728 struct nfs_server *server = NFS_SERVER(inode);
1729 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct nfs4_accessargs args = {
1731 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04001732 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 };
Trond Myklebust76b32992007-08-10 17:45:11 -04001734 struct nfs4_accessres res = {
1735 .server = server,
1736 .fattr = &fattr,
1737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct rpc_message msg = {
1739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1740 .rpc_argp = &args,
1741 .rpc_resp = &res,
1742 .rpc_cred = entry->cred,
1743 };
1744 int mode = entry->mask;
1745 int status;
1746
1747 /*
1748 * Determine which access bits we want to ask for...
1749 */
1750 if (mode & MAY_READ)
1751 args.access |= NFS4_ACCESS_READ;
1752 if (S_ISDIR(inode->i_mode)) {
1753 if (mode & MAY_WRITE)
1754 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1755 if (mode & MAY_EXEC)
1756 args.access |= NFS4_ACCESS_LOOKUP;
1757 } else {
1758 if (mode & MAY_WRITE)
1759 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1760 if (mode & MAY_EXEC)
1761 args.access |= NFS4_ACCESS_EXECUTE;
1762 }
Trond Myklebust76b32992007-08-10 17:45:11 -04001763 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1765 if (!status) {
1766 entry->mask = 0;
1767 if (res.access & NFS4_ACCESS_READ)
1768 entry->mask |= MAY_READ;
1769 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1770 entry->mask |= MAY_WRITE;
1771 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1772 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04001773 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 return status;
1776}
1777
1778static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1779{
1780 struct nfs4_exception exception = { };
1781 int err;
1782 do {
1783 err = nfs4_handle_exception(NFS_SERVER(inode),
1784 _nfs4_proc_access(inode, entry),
1785 &exception);
1786 } while (exception.retry);
1787 return err;
1788}
1789
1790/*
1791 * TODO: For the time being, we don't try to get any attributes
1792 * along with any of the zero-copy operations READ, READDIR,
1793 * READLINK, WRITE.
1794 *
1795 * In the case of the first three, we want to put the GETATTR
1796 * after the read-type operation -- this is because it is hard
1797 * to predict the length of a GETATTR response in v4, and thus
1798 * align the READ data correctly. This means that the GETATTR
1799 * may end up partially falling into the page cache, and we should
1800 * shift it into the 'tail' of the xdr_buf before processing.
1801 * To do this efficiently, we need to know the total length
1802 * of data received, which doesn't seem to be available outside
1803 * of the RPC layer.
1804 *
1805 * In the case of WRITE, we also want to put the GETATTR after
1806 * the operation -- in this case because we want to make sure
1807 * we get the post-operation mtime and size. This means that
1808 * we can't use xdr_encode_pages() as written: we need a variant
1809 * of it which would leave room in the 'tail' iovec.
1810 *
1811 * Both of these changes to the XDR layer would in fact be quite
1812 * minor, but I decided to leave them for a subsequent patch.
1813 */
1814static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1815 unsigned int pgbase, unsigned int pglen)
1816{
1817 struct nfs4_readlink args = {
1818 .fh = NFS_FH(inode),
1819 .pgbase = pgbase,
1820 .pglen = pglen,
1821 .pages = &page,
1822 };
1823 struct rpc_message msg = {
1824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1825 .rpc_argp = &args,
1826 .rpc_resp = NULL,
1827 };
1828
1829 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1830}
1831
1832static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1833 unsigned int pgbase, unsigned int pglen)
1834{
1835 struct nfs4_exception exception = { };
1836 int err;
1837 do {
1838 err = nfs4_handle_exception(NFS_SERVER(inode),
1839 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1840 &exception);
1841 } while (exception.retry);
1842 return err;
1843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845/*
1846 * Got race?
1847 * We will need to arrange for the VFS layer to provide an atomic open.
1848 * Until then, this create/open method is prone to inefficiency and race
1849 * conditions due to the lookup, create, and open VFS calls from sys_open()
1850 * placed on the wire.
1851 *
1852 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1853 * The file will be opened again in the subsequent VFS open call
1854 * (nfs4_proc_file_open).
1855 *
1856 * The open for read will just hang around to be used by any process that
1857 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1858 */
1859
1860static int
1861nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001862 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Trond Myklebustad389da2007-06-05 12:30:00 -04001864 struct path path = {
1865 .mnt = nd->mnt,
1866 .dentry = dentry,
1867 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct nfs4_state *state;
1869 struct rpc_cred *cred;
1870 int status = 0;
1871
David Howells1f163412006-08-22 20:06:11 -04001872 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (IS_ERR(cred)) {
1874 status = PTR_ERR(cred);
1875 goto out;
1876 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001877 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 put_rpccred(cred);
1879 if (IS_ERR(state)) {
1880 status = PTR_ERR(state);
1881 goto out;
1882 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001883 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (flags & O_EXCL) {
1885 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001886 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001887 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001888 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001889 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001890 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001891 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001892 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001893 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001894 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895out:
1896 return status;
1897}
1898
1899static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1900{
Trond Myklebust16e42952005-10-27 22:12:44 -04001901 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001902 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001904 .name.len = name->len,
1905 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001906 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001908 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001909 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04001910 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1913 .rpc_argp = &args,
1914 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 };
1916 int status;
1917
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001918 nfs_fattr_init(&res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001919 status = rpc_call_sync(server->client, &msg, 0);
1920 if (status == 0) {
1921 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001922 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return status;
1925}
1926
1927static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1928{
1929 struct nfs4_exception exception = { };
1930 int err;
1931 do {
1932 err = nfs4_handle_exception(NFS_SERVER(dir),
1933 _nfs4_proc_remove(dir, name),
1934 &exception);
1935 } while (exception.retry);
1936 return err;
1937}
1938
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001939static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001941 struct nfs_server *server = NFS_SERVER(dir);
1942 struct nfs_removeargs *args = msg->rpc_argp;
1943 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001945 args->bitmask = server->attr_bitmask;
1946 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001950static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001952 struct nfs_removeres *res = task->tk_msg.rpc_resp;
1953
1954 if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
1955 return 0;
1956 update_changeattr(dir, &res->cinfo);
1957 nfs_post_op_update_inode(dir, &res->dir_attr);
1958 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
1961static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1962 struct inode *new_dir, struct qstr *new_name)
1963{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001964 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct nfs4_rename_arg arg = {
1966 .old_dir = NFS_FH(old_dir),
1967 .new_dir = NFS_FH(new_dir),
1968 .old_name = old_name,
1969 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001970 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001972 struct nfs_fattr old_fattr, new_fattr;
1973 struct nfs4_rename_res res = {
1974 .server = server,
1975 .old_fattr = &old_fattr,
1976 .new_fattr = &new_fattr,
1977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct rpc_message msg = {
1979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1980 .rpc_argp = &arg,
1981 .rpc_resp = &res,
1982 };
1983 int status;
1984
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001985 nfs_fattr_init(res.old_fattr);
1986 nfs_fattr_init(res.new_fattr);
1987 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (!status) {
1990 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001991 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001993 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 return status;
1996}
1997
1998static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1999 struct inode *new_dir, struct qstr *new_name)
2000{
2001 struct nfs4_exception exception = { };
2002 int err;
2003 do {
2004 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2005 _nfs4_proc_rename(old_dir, old_name,
2006 new_dir, new_name),
2007 &exception);
2008 } while (exception.retry);
2009 return err;
2010}
2011
2012static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2013{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002014 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct nfs4_link_arg arg = {
2016 .fh = NFS_FH(inode),
2017 .dir_fh = NFS_FH(dir),
2018 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002019 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002021 struct nfs_fattr fattr, dir_attr;
2022 struct nfs4_link_res res = {
2023 .server = server,
2024 .fattr = &fattr,
2025 .dir_attr = &dir_attr,
2026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct rpc_message msg = {
2028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2029 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002030 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 };
2032 int status;
2033
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002034 nfs_fattr_init(res.fattr);
2035 nfs_fattr_init(res.dir_attr);
2036 status = rpc_call_sync(server->client, &msg, 0);
2037 if (!status) {
2038 update_changeattr(dir, &res.cinfo);
2039 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002040 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 return status;
2044}
2045
2046static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2047{
2048 struct nfs4_exception exception = { };
2049 int err;
2050 do {
2051 err = nfs4_handle_exception(NFS_SERVER(inode),
2052 _nfs4_proc_link(inode, dir, name),
2053 &exception);
2054 } while (exception.retry);
2055 return err;
2056}
2057
Chuck Lever4f390c12006-08-22 20:06:22 -04002058static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002059 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002062 struct nfs_fh fhandle;
2063 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct nfs4_create_arg arg = {
2065 .dir_fh = NFS_FH(dir),
2066 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002067 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 .attrs = sattr,
2069 .ftype = NF4LNK,
2070 .bitmask = server->attr_bitmask,
2071 };
2072 struct nfs4_create_res res = {
2073 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002074 .fh = &fhandle,
2075 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002076 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 };
2078 struct rpc_message msg = {
2079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2080 .rpc_argp = &arg,
2081 .rpc_resp = &res,
2082 };
2083 int status;
2084
Chuck Lever94a6d752006-08-22 20:06:23 -04002085 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002087
Chuck Lever94a6d752006-08-22 20:06:23 -04002088 arg.u.symlink.pages = &page;
2089 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002090 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002091 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002094 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002096 nfs_post_op_update_inode(dir, res.dir_fattr);
2097 status = nfs_instantiate(dentry, &fhandle, &fattr);
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return status;
2100}
2101
Chuck Lever4f390c12006-08-22 20:06:22 -04002102static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002103 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 struct nfs4_exception exception = { };
2106 int err;
2107 do {
2108 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002109 _nfs4_proc_symlink(dir, dentry, page,
2110 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 &exception);
2112 } while (exception.retry);
2113 return err;
2114}
2115
2116static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2117 struct iattr *sattr)
2118{
2119 struct nfs_server *server = NFS_SERVER(dir);
2120 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002121 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 struct nfs4_create_arg arg = {
2123 .dir_fh = NFS_FH(dir),
2124 .server = server,
2125 .name = &dentry->d_name,
2126 .attrs = sattr,
2127 .ftype = NF4DIR,
2128 .bitmask = server->attr_bitmask,
2129 };
2130 struct nfs4_create_res res = {
2131 .server = server,
2132 .fh = &fhandle,
2133 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002134 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 };
2136 struct rpc_message msg = {
2137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2138 .rpc_argp = &arg,
2139 .rpc_resp = &res,
2140 };
2141 int status;
2142
Trond Myklebust0e574af2005-10-27 22:12:38 -04002143 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002144 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2147 if (!status) {
2148 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002149 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 status = nfs_instantiate(dentry, &fhandle, &fattr);
2151 }
2152 return status;
2153}
2154
2155static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2156 struct iattr *sattr)
2157{
2158 struct nfs4_exception exception = { };
2159 int err;
2160 do {
2161 err = nfs4_handle_exception(NFS_SERVER(dir),
2162 _nfs4_proc_mkdir(dir, dentry, sattr),
2163 &exception);
2164 } while (exception.retry);
2165 return err;
2166}
2167
2168static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2169 u64 cookie, struct page *page, unsigned int count, int plus)
2170{
2171 struct inode *dir = dentry->d_inode;
2172 struct nfs4_readdir_arg args = {
2173 .fh = NFS_FH(dir),
2174 .pages = &page,
2175 .pgbase = 0,
2176 .count = count,
2177 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2178 };
2179 struct nfs4_readdir_res res;
2180 struct rpc_message msg = {
2181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2182 .rpc_argp = &args,
2183 .rpc_resp = &res,
2184 .rpc_cred = cred,
2185 };
2186 int status;
2187
Trond Myklebusteadf4592005-06-22 17:16:39 +00002188 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2189 dentry->d_parent->d_name.name,
2190 dentry->d_name.name,
2191 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2193 res.pgbase = args.pgbase;
2194 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2195 if (status == 0)
2196 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002197 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return status;
2199}
2200
2201static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2202 u64 cookie, struct page *page, unsigned int count, int plus)
2203{
2204 struct nfs4_exception exception = { };
2205 int err;
2206 do {
2207 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2208 _nfs4_proc_readdir(dentry, cred, cookie,
2209 page, count, plus),
2210 &exception);
2211 } while (exception.retry);
2212 return err;
2213}
2214
2215static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2216 struct iattr *sattr, dev_t rdev)
2217{
2218 struct nfs_server *server = NFS_SERVER(dir);
2219 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002220 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs4_create_arg arg = {
2222 .dir_fh = NFS_FH(dir),
2223 .server = server,
2224 .name = &dentry->d_name,
2225 .attrs = sattr,
2226 .bitmask = server->attr_bitmask,
2227 };
2228 struct nfs4_create_res res = {
2229 .server = server,
2230 .fh = &fh,
2231 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002232 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 };
2234 struct rpc_message msg = {
2235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2236 .rpc_argp = &arg,
2237 .rpc_resp = &res,
2238 };
2239 int status;
2240 int mode = sattr->ia_mode;
2241
Trond Myklebust0e574af2005-10-27 22:12:38 -04002242 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002243 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2246 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2247 if (S_ISFIFO(mode))
2248 arg.ftype = NF4FIFO;
2249 else if (S_ISBLK(mode)) {
2250 arg.ftype = NF4BLK;
2251 arg.u.device.specdata1 = MAJOR(rdev);
2252 arg.u.device.specdata2 = MINOR(rdev);
2253 }
2254 else if (S_ISCHR(mode)) {
2255 arg.ftype = NF4CHR;
2256 arg.u.device.specdata1 = MAJOR(rdev);
2257 arg.u.device.specdata2 = MINOR(rdev);
2258 }
2259 else
2260 arg.ftype = NF4SOCK;
2261
2262 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2263 if (status == 0) {
2264 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002265 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 status = nfs_instantiate(dentry, &fh, &fattr);
2267 }
2268 return status;
2269}
2270
2271static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2272 struct iattr *sattr, dev_t rdev)
2273{
2274 struct nfs4_exception exception = { };
2275 int err;
2276 do {
2277 err = nfs4_handle_exception(NFS_SERVER(dir),
2278 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2279 &exception);
2280 } while (exception.retry);
2281 return err;
2282}
2283
2284static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2285 struct nfs_fsstat *fsstat)
2286{
2287 struct nfs4_statfs_arg args = {
2288 .fh = fhandle,
2289 .bitmask = server->attr_bitmask,
2290 };
2291 struct rpc_message msg = {
2292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2293 .rpc_argp = &args,
2294 .rpc_resp = fsstat,
2295 };
2296
Trond Myklebust0e574af2005-10-27 22:12:38 -04002297 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return rpc_call_sync(server->client, &msg, 0);
2299}
2300
2301static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2302{
2303 struct nfs4_exception exception = { };
2304 int err;
2305 do {
2306 err = nfs4_handle_exception(server,
2307 _nfs4_proc_statfs(server, fhandle, fsstat),
2308 &exception);
2309 } while (exception.retry);
2310 return err;
2311}
2312
2313static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2314 struct nfs_fsinfo *fsinfo)
2315{
2316 struct nfs4_fsinfo_arg args = {
2317 .fh = fhandle,
2318 .bitmask = server->attr_bitmask,
2319 };
2320 struct rpc_message msg = {
2321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2322 .rpc_argp = &args,
2323 .rpc_resp = fsinfo,
2324 };
2325
2326 return rpc_call_sync(server->client, &msg, 0);
2327}
2328
2329static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2330{
2331 struct nfs4_exception exception = { };
2332 int err;
2333
2334 do {
2335 err = nfs4_handle_exception(server,
2336 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2337 &exception);
2338 } while (exception.retry);
2339 return err;
2340}
2341
2342static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2343{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002344 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2346}
2347
2348static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_pathconf *pathconf)
2350{
2351 struct nfs4_pathconf_arg args = {
2352 .fh = fhandle,
2353 .bitmask = server->attr_bitmask,
2354 };
2355 struct rpc_message msg = {
2356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2357 .rpc_argp = &args,
2358 .rpc_resp = pathconf,
2359 };
2360
2361 /* None of the pathconf attributes are mandatory to implement */
2362 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2363 memset(pathconf, 0, sizeof(*pathconf));
2364 return 0;
2365 }
2366
Trond Myklebust0e574af2005-10-27 22:12:38 -04002367 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return rpc_call_sync(server->client, &msg, 0);
2369}
2370
2371static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2372 struct nfs_pathconf *pathconf)
2373{
2374 struct nfs4_exception exception = { };
2375 int err;
2376
2377 do {
2378 err = nfs4_handle_exception(server,
2379 _nfs4_proc_pathconf(server, fhandle, pathconf),
2380 &exception);
2381 } while (exception.retry);
2382 return err;
2383}
2384
Trond Myklebustec06c092006-03-20 13:44:27 -05002385static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Trond Myklebustec06c092006-03-20 13:44:27 -05002387 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Trond Myklebustec06c092006-03-20 13:44:27 -05002389 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002391 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002394 renew_lease(server, data->timestamp);
2395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
Trond Myklebustec06c092006-03-20 13:44:27 -05002398static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct rpc_message msg = {
2401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2402 .rpc_argp = &data->args,
2403 .rpc_resp = &data->res,
2404 .rpc_cred = data->cred,
2405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 data->timestamp = jiffies;
2408
Trond Myklebustec06c092006-03-20 13:44:27 -05002409 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebust788e7a82006-03-20 13:44:27 -05002412static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct inode *inode = data->inode;
2415
2416 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2417 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002418 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002420 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002422 nfs_post_op_update_inode(inode, data->res.fattr);
2423 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Trond Myklebust788e7a82006-03-20 13:44:27 -05002427static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct rpc_message msg = {
2430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2431 .rpc_argp = &data->args,
2432 .rpc_resp = &data->res,
2433 .rpc_cred = data->cred,
2434 };
2435 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002436 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (how & FLUSH_STABLE) {
2440 if (!NFS_I(inode)->ncommit)
2441 stable = NFS_FILE_SYNC;
2442 else
2443 stable = NFS_DATA_SYNC;
2444 } else
2445 stable = NFS_UNSTABLE;
2446 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002447 data->args.bitmask = server->attr_bitmask;
2448 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 data->timestamp = jiffies;
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002453 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Trond Myklebust788e7a82006-03-20 13:44:27 -05002456static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct inode *inode = data->inode;
2459
2460 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2461 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002462 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002464 if (task->tk_status >= 0)
2465 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Trond Myklebust788e7a82006-03-20 13:44:27 -05002469static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2473 .rpc_argp = &data->args,
2474 .rpc_resp = &data->res,
2475 .rpc_cred = data->cred,
2476 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002477 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002479 data->args.bitmask = server->attr_bitmask;
2480 data->res.server = server;
2481
Trond Myklebust788e7a82006-03-20 13:44:27 -05002482 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485/*
2486 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2487 * standalone procedure for queueing an asynchronous RENEW.
2488 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002489static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
David Howellsadfa6f92006-08-22 20:06:08 -04002491 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002492 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 if (task->tk_status < 0) {
2495 switch (task->tk_status) {
2496 case -NFS4ERR_STALE_CLIENTID:
2497 case -NFS4ERR_EXPIRED:
2498 case -NFS4ERR_CB_PATH_DOWN:
2499 nfs4_schedule_state_recovery(clp);
2500 }
2501 return;
2502 }
2503 spin_lock(&clp->cl_lock);
2504 if (time_before(clp->cl_last_renewal,timestamp))
2505 clp->cl_last_renewal = timestamp;
2506 spin_unlock(&clp->cl_lock);
2507}
2508
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002509static const struct rpc_call_ops nfs4_renew_ops = {
2510 .rpc_call_done = nfs4_renew_done,
2511};
2512
David Howellsadfa6f92006-08-22 20:06:08 -04002513int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct rpc_message msg = {
2516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2517 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002518 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 };
2520
2521 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002522 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
David Howellsadfa6f92006-08-22 20:06:08 -04002525int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 struct rpc_message msg = {
2528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2529 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002530 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 };
2532 unsigned long now = jiffies;
2533 int status;
2534
2535 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2536 if (status < 0)
2537 return status;
2538 spin_lock(&clp->cl_lock);
2539 if (time_before(clp->cl_last_renewal,now))
2540 clp->cl_last_renewal = now;
2541 spin_unlock(&clp->cl_lock);
2542 return 0;
2543}
2544
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002545static inline int nfs4_server_supports_acls(struct nfs_server *server)
2546{
2547 return (server->caps & NFS_CAP_ACLS)
2548 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2549 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2550}
2551
2552/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2553 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2554 * the stack.
2555 */
2556#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2557
2558static void buf_to_pages(const void *buf, size_t buflen,
2559 struct page **pages, unsigned int *pgbase)
2560{
2561 const void *p = buf;
2562
2563 *pgbase = offset_in_page(buf);
2564 p -= *pgbase;
2565 while (p < buf + buflen) {
2566 *(pages++) = virt_to_page(p);
2567 p += PAGE_CACHE_SIZE;
2568 }
2569}
2570
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002571struct nfs4_cached_acl {
2572 int cached;
2573 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002574 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002575};
2576
2577static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002578{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002579 struct nfs_inode *nfsi = NFS_I(inode);
2580
2581 spin_lock(&inode->i_lock);
2582 kfree(nfsi->nfs4_acl);
2583 nfsi->nfs4_acl = acl;
2584 spin_unlock(&inode->i_lock);
2585}
2586
2587static void nfs4_zap_acl_attr(struct inode *inode)
2588{
2589 nfs4_set_cached_acl(inode, NULL);
2590}
2591
2592static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2593{
2594 struct nfs_inode *nfsi = NFS_I(inode);
2595 struct nfs4_cached_acl *acl;
2596 int ret = -ENOENT;
2597
2598 spin_lock(&inode->i_lock);
2599 acl = nfsi->nfs4_acl;
2600 if (acl == NULL)
2601 goto out;
2602 if (buf == NULL) /* user is just asking for length */
2603 goto out_len;
2604 if (acl->cached == 0)
2605 goto out;
2606 ret = -ERANGE; /* see getxattr(2) man page */
2607 if (acl->len > buflen)
2608 goto out;
2609 memcpy(buf, acl->data, acl->len);
2610out_len:
2611 ret = acl->len;
2612out:
2613 spin_unlock(&inode->i_lock);
2614 return ret;
2615}
2616
2617static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2618{
2619 struct nfs4_cached_acl *acl;
2620
2621 if (buf && acl_len <= PAGE_SIZE) {
2622 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2623 if (acl == NULL)
2624 goto out;
2625 acl->cached = 1;
2626 memcpy(acl->data, buf, acl_len);
2627 } else {
2628 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2629 if (acl == NULL)
2630 goto out;
2631 acl->cached = 0;
2632 }
2633 acl->len = acl_len;
2634out:
2635 nfs4_set_cached_acl(inode, acl);
2636}
2637
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002638static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002639{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002640 struct page *pages[NFS4ACL_MAXPAGES];
2641 struct nfs_getaclargs args = {
2642 .fh = NFS_FH(inode),
2643 .acl_pages = pages,
2644 .acl_len = buflen,
2645 };
2646 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002647 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2650 .rpc_argp = &args,
2651 .rpc_resp = &resp_len,
2652 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002653 struct page *localpage = NULL;
2654 int ret;
2655
2656 if (buflen < PAGE_SIZE) {
2657 /* As long as we're doing a round trip to the server anyway,
2658 * let's be prepared for a page of acl data. */
2659 localpage = alloc_page(GFP_KERNEL);
2660 resp_buf = page_address(localpage);
2661 if (localpage == NULL)
2662 return -ENOMEM;
2663 args.acl_pages[0] = localpage;
2664 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002665 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002666 } else {
2667 resp_buf = buf;
2668 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2669 }
2670 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2671 if (ret)
2672 goto out_free;
2673 if (resp_len > args.acl_len)
2674 nfs4_write_cached_acl(inode, NULL, resp_len);
2675 else
2676 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2677 if (buf) {
2678 ret = -ERANGE;
2679 if (resp_len > buflen)
2680 goto out_free;
2681 if (localpage)
2682 memcpy(buf, resp_buf, resp_len);
2683 }
2684 ret = resp_len;
2685out_free:
2686 if (localpage)
2687 __free_page(localpage);
2688 return ret;
2689}
2690
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002691static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2692{
2693 struct nfs4_exception exception = { };
2694 ssize_t ret;
2695 do {
2696 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2697 if (ret >= 0)
2698 break;
2699 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2700 } while (exception.retry);
2701 return ret;
2702}
2703
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002704static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2705{
2706 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002707 int ret;
2708
2709 if (!nfs4_server_supports_acls(server))
2710 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002711 ret = nfs_revalidate_inode(server, inode);
2712 if (ret < 0)
2713 return ret;
2714 ret = nfs4_read_cached_acl(inode, buf, buflen);
2715 if (ret != -ENOENT)
2716 return ret;
2717 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002718}
2719
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002720static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002721{
2722 struct nfs_server *server = NFS_SERVER(inode);
2723 struct page *pages[NFS4ACL_MAXPAGES];
2724 struct nfs_setaclargs arg = {
2725 .fh = NFS_FH(inode),
2726 .acl_pages = pages,
2727 .acl_len = buflen,
2728 };
2729 struct rpc_message msg = {
2730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2731 .rpc_argp = &arg,
2732 .rpc_resp = NULL,
2733 };
2734 int ret;
2735
2736 if (!nfs4_server_supports_acls(server))
2737 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002738 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002739 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002740 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002741 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002742 return ret;
2743}
2744
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002745static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2746{
2747 struct nfs4_exception exception = { };
2748 int err;
2749 do {
2750 err = nfs4_handle_exception(NFS_SERVER(inode),
2751 __nfs4_proc_set_acl(inode, buf, buflen),
2752 &exception);
2753 } while (exception.retry);
2754 return err;
2755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002758nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
David Howells7539bba2006-08-22 20:06:09 -04002760 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 if (!clp || task->tk_status >= 0)
2763 return 0;
2764 switch(task->tk_status) {
2765 case -NFS4ERR_STALE_CLIENTID:
2766 case -NFS4ERR_STALE_STATEID:
2767 case -NFS4ERR_EXPIRED:
2768 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2769 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002770 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 rpc_wake_up_task(task);
2772 task->tk_status = 0;
2773 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002775 nfs_inc_server_stats((struct nfs_server *) server,
2776 NFSIOS_DELAY);
2777 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2779 task->tk_status = 0;
2780 return -EAGAIN;
2781 case -NFS4ERR_OLD_STATEID:
2782 task->tk_status = 0;
2783 return -EAGAIN;
2784 }
2785 task->tk_status = nfs4_map_errors(task->tk_status);
2786 return 0;
2787}
2788
Trond Myklebust433fbe42006-01-03 09:55:22 +01002789static int nfs4_wait_bit_interruptible(void *word)
2790{
2791 if (signal_pending(current))
2792 return -ERESTARTSYS;
2793 schedule();
2794 return 0;
2795}
2796
David Howellsadfa6f92006-08-22 20:06:08 -04002797static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002800 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 might_sleep();
2803
Trond Myklebuste1485822006-12-13 16:43:13 -05002804 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002807 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2808 nfs4_wait_bit_interruptible,
2809 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002811
2812 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return res;
2814}
2815
2816static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2817{
2818 sigset_t oldset;
2819 int res = 0;
2820
2821 might_sleep();
2822
2823 if (*timeout <= 0)
2824 *timeout = NFS4_POLL_RETRY_MIN;
2825 if (*timeout > NFS4_POLL_RETRY_MAX)
2826 *timeout = NFS4_POLL_RETRY_MAX;
2827 rpc_clnt_sigmask(clnt, &oldset);
2828 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002829 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (signalled())
2831 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002832 } else
2833 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 rpc_clnt_sigunmask(clnt, &oldset);
2835 *timeout <<= 1;
2836 return res;
2837}
2838
2839/* This is the error handling routine for processes that are allowed
2840 * to sleep.
2841 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002842static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
David Howells7539bba2006-08-22 20:06:09 -04002844 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int ret = errorcode;
2846
2847 exception->retry = 0;
2848 switch(errorcode) {
2849 case 0:
2850 return 0;
2851 case -NFS4ERR_STALE_CLIENTID:
2852 case -NFS4ERR_STALE_STATEID:
2853 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002854 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ret = nfs4_wait_clnt_recover(server->client, clp);
2856 if (ret == 0)
2857 exception->retry = 1;
2858 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002859 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 case -NFS4ERR_GRACE:
2861 case -NFS4ERR_DELAY:
2862 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002863 if (ret != 0)
2864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002866 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868 /* We failed to handle the error */
2869 return nfs4_map_errors(ret);
2870}
2871
David Howellsadfa6f92006-08-22 20:06:08 -04002872int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
2874 nfs4_verifier sc_verifier;
2875 struct nfs4_setclientid setclientid = {
2876 .sc_verifier = &sc_verifier,
2877 .sc_prog = program,
2878 };
2879 struct rpc_message msg = {
2880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2881 .rpc_argp = &setclientid,
2882 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002883 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 };
Al Virobc4785c2006-10-19 23:28:51 -07002885 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int loop = 0;
2887 int status;
2888
Al Virobc4785c2006-10-19 23:28:51 -07002889 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2891 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2892
2893 for(;;) {
2894 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2895 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002896 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002897 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 clp->cl_id_uniquifier);
2899 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2900 sizeof(setclientid.sc_netid), "tcp");
2901 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2902 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2903 clp->cl_ipaddr, port >> 8, port & 255);
2904
2905 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2906 if (status != -NFS4ERR_CLID_INUSE)
2907 break;
2908 if (signalled())
2909 break;
2910 if (loop++ & 1)
2911 ssleep(clp->cl_lease_time + 1);
2912 else
2913 if (++clp->cl_id_uniquifier == 0)
2914 break;
2915 }
2916 return status;
2917}
2918
David Howellsadfa6f92006-08-22 20:06:08 -04002919static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct nfs_fsinfo fsinfo;
2922 struct rpc_message msg = {
2923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2924 .rpc_argp = clp,
2925 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002926 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 };
2928 unsigned long now;
2929 int status;
2930
2931 now = jiffies;
2932 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2933 if (status == 0) {
2934 spin_lock(&clp->cl_lock);
2935 clp->cl_lease_time = fsinfo.lease_time * HZ;
2936 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002937 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 spin_unlock(&clp->cl_lock);
2939 }
2940 return status;
2941}
2942
David Howellsadfa6f92006-08-22 20:06:08 -04002943int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002944{
2945 long timeout;
2946 int err;
2947 do {
2948 err = _nfs4_proc_setclientid_confirm(clp, cred);
2949 switch (err) {
2950 case 0:
2951 return err;
2952 case -NFS4ERR_RESOURCE:
2953 /* The IBM lawyers misread another document! */
2954 case -NFS4ERR_DELAY:
2955 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2956 }
2957 } while (err == 0);
2958 return err;
2959}
2960
Trond Myklebustfe650402006-01-03 09:55:18 +01002961struct nfs4_delegreturndata {
2962 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002963 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002964 struct nfs_fh fh;
2965 nfs4_stateid stateid;
2966 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002967 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002968 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 int rpc_status;
2970};
2971
2972static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Trond Myklebustfe650402006-01-03 09:55:18 +01002974 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002977 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002978 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002979 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002981 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002982 rpc_call_setup(task, &msg, 0);
2983}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Trond Myklebustfe650402006-01-03 09:55:18 +01002985static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2986{
2987 struct nfs4_delegreturndata *data = calldata;
2988 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002989 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002990 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002991}
2992
2993static void nfs4_delegreturn_release(void *calldata)
2994{
2995 struct nfs4_delegreturndata *data = calldata;
2996
2997 put_rpccred(data->cred);
2998 kfree(calldata);
2999}
3000
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003001static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003002 .rpc_call_prepare = nfs4_delegreturn_prepare,
3003 .rpc_call_done = nfs4_delegreturn_done,
3004 .rpc_release = nfs4_delegreturn_release,
3005};
3006
3007static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3008{
3009 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003010 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003011 struct rpc_task *task;
3012 int status;
3013
3014 data = kmalloc(sizeof(*data), GFP_KERNEL);
3015 if (data == NULL)
3016 return -ENOMEM;
3017 data->args.fhandle = &data->fh;
3018 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003019 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003020 nfs_copy_fh(&data->fh, NFS_FH(inode));
3021 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003022 data->res.fattr = &data->fattr;
3023 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01003024 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003025 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003026 data->rpc_status = 0;
3027
3028 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003029 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003030 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003031 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01003032 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01003033 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003034 if (status == 0)
3035 nfs_post_op_update_inode(inode, &data->fattr);
3036 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003037 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003038 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3042{
3043 struct nfs_server *server = NFS_SERVER(inode);
3044 struct nfs4_exception exception = { };
3045 int err;
3046 do {
3047 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3048 switch (err) {
3049 case -NFS4ERR_STALE_STATEID:
3050 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 case 0:
3052 return 0;
3053 }
3054 err = nfs4_handle_exception(server, err, &exception);
3055 } while (exception.retry);
3056 return err;
3057}
3058
3059#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3060#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3061
3062/*
3063 * sleep, with exponential backoff, and retry the LOCK operation.
3064 */
3065static unsigned long
3066nfs4_set_lock_task_retry(unsigned long timeout)
3067{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003068 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 timeout <<= 1;
3070 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3071 return NFS4_LOCK_MAXTIMEOUT;
3072 return timeout;
3073}
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3076{
3077 struct inode *inode = state->inode;
3078 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003079 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003080 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003082 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003084 struct nfs_lockt_res res = {
3085 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 };
3087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3089 .rpc_argp = &arg,
3090 .rpc_resp = &res,
3091 .rpc_cred = state->owner->so_cred,
3092 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 struct nfs4_lock_state *lsp;
3094 int status;
3095
3096 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003097 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003098 status = nfs4_set_lock_state(state, request);
3099 if (status != 0)
3100 goto out;
3101 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003102 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003104 switch (status) {
3105 case 0:
3106 request->fl_type = F_UNLCK;
3107 break;
3108 case -NFS4ERR_DENIED:
3109 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003111 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 up_read(&clp->cl_sem);
3114 return status;
3115}
3116
3117static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3118{
3119 struct nfs4_exception exception = { };
3120 int err;
3121
3122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3124 _nfs4_proc_getlk(state, cmd, request),
3125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int do_vfs_lock(struct file *file, struct file_lock *fl)
3131{
3132 int res = 0;
3133 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3134 case FL_POSIX:
3135 res = posix_lock_file_wait(file, fl);
3136 break;
3137 case FL_FLOCK:
3138 res = flock_lock_file_wait(file, fl);
3139 break;
3140 default:
3141 BUG();
3142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return res;
3144}
3145
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003146struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003147 struct nfs_locku_args arg;
3148 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003149 struct nfs4_lock_state *lsp;
3150 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003151 struct file_lock fl;
3152 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003153 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003154};
3155
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003156static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3157 struct nfs_open_context *ctx,
3158 struct nfs4_lock_state *lsp,
3159 struct nfs_seqid *seqid)
3160{
3161 struct nfs4_unlockdata *p;
3162 struct inode *inode = lsp->ls_state->inode;
3163
3164 p = kmalloc(sizeof(*p), GFP_KERNEL);
3165 if (p == NULL)
3166 return NULL;
3167 p->arg.fh = NFS_FH(inode);
3168 p->arg.fl = &p->fl;
3169 p->arg.seqid = seqid;
3170 p->arg.stateid = &lsp->ls_stateid;
3171 p->lsp = lsp;
3172 atomic_inc(&lsp->ls_count);
3173 /* Ensure we don't close file until we're done freeing locks! */
3174 p->ctx = get_nfs_open_context(ctx);
3175 memcpy(&p->fl, fl, sizeof(p->fl));
3176 p->server = NFS_SERVER(inode);
3177 return p;
3178}
3179
Trond Myklebust06f814a2006-01-03 09:55:07 +01003180static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003181{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003182 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003183 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003184 nfs4_put_lock_state(calldata->lsp);
3185 put_nfs_open_context(calldata->ctx);
3186 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003187}
3188
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003189static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003190{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003191 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003192
Trond Myklebust06f814a2006-01-03 09:55:07 +01003193 if (RPC_ASSASSINATED(task))
3194 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003195 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003196 switch (task->tk_status) {
3197 case 0:
3198 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003199 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003200 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003201 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003202 break;
3203 case -NFS4ERR_STALE_STATEID:
3204 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003205 break;
3206 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003207 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003208 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003209 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003210}
3211
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003212static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003213{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003214 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct rpc_message msg = {
3216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003217 .rpc_argp = &calldata->arg,
3218 .rpc_resp = &calldata->res,
3219 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003222 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003223 return;
3224 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003225 /* Note: exit _without_ running nfs4_locku_done */
3226 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003227 return;
3228 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003229 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003230 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003233static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003234 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003235 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003236 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003237};
3238
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003239static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3240 struct nfs_open_context *ctx,
3241 struct nfs4_lock_state *lsp,
3242 struct nfs_seqid *seqid)
3243{
3244 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003245
Frank Filz137d6ac2007-07-09 15:32:29 -07003246 /* Ensure this is an unlock - when canceling a lock, the
3247 * canceled lock is passed in, and it won't be an unlock.
3248 */
3249 fl->fl_type = F_UNLCK;
3250
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003251 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3252 if (data == NULL) {
3253 nfs_free_seqid(seqid);
3254 return ERR_PTR(-ENOMEM);
3255 }
3256
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003257 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003258}
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3261{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003262 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003263 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003264 struct rpc_task *task;
3265 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Trond Myklebust9b073572006-06-29 16:38:34 -04003267 status = nfs4_set_lock_state(state, request);
3268 /* Unlock _before_ we do the RPC call */
3269 request->fl_flags |= FL_EXISTS;
3270 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3271 goto out;
3272 if (status != 0)
3273 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003274 /* Is this a delegated lock? */
3275 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003276 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003277 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003278 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003279 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003280 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003281 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003282 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003283 status = PTR_ERR(task);
3284 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003285 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003286 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003287 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003288out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003289 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003292struct nfs4_lockdata {
3293 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003294 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003295 struct nfs4_lock_state *lsp;
3296 struct nfs_open_context *ctx;
3297 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003298 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003299 int rpc_status;
3300 int cancelled;
3301};
3302
3303static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3304 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3305{
3306 struct nfs4_lockdata *p;
3307 struct inode *inode = lsp->ls_state->inode;
3308 struct nfs_server *server = NFS_SERVER(inode);
3309
3310 p = kzalloc(sizeof(*p), GFP_KERNEL);
3311 if (p == NULL)
3312 return NULL;
3313
3314 p->arg.fh = NFS_FH(inode);
3315 p->arg.fl = &p->fl;
3316 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3317 if (p->arg.lock_seqid == NULL)
3318 goto out_free;
3319 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003320 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003321 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003322 p->lsp = lsp;
3323 atomic_inc(&lsp->ls_count);
3324 p->ctx = get_nfs_open_context(ctx);
3325 memcpy(&p->fl, fl, sizeof(p->fl));
3326 return p;
3327out_free:
3328 kfree(p);
3329 return NULL;
3330}
3331
3332static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3333{
3334 struct nfs4_lockdata *data = calldata;
3335 struct nfs4_state *state = data->lsp->ls_state;
3336 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3339 .rpc_argp = &data->arg,
3340 .rpc_resp = &data->res,
3341 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003344 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3345 return;
3346 dprintk("%s: begin!\n", __FUNCTION__);
3347 /* Do we need to do an open_to_lock_owner? */
3348 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3349 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3350 if (data->arg.open_seqid == NULL) {
3351 data->rpc_status = -ENOMEM;
3352 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003353 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003354 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003355 data->arg.open_stateid = &state->stateid;
3356 data->arg.new_lock_owner = 1;
3357 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003358 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003359 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003360out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003361 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3362}
3363
3364static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3365{
3366 struct nfs4_lockdata *data = calldata;
3367
3368 dprintk("%s: begin!\n", __FUNCTION__);
3369
3370 data->rpc_status = task->tk_status;
3371 if (RPC_ASSASSINATED(task))
3372 goto out;
3373 if (data->arg.new_lock_owner != 0) {
3374 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3375 if (data->rpc_status == 0)
3376 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3377 else
3378 goto out;
3379 }
3380 if (data->rpc_status == 0) {
3381 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3382 sizeof(data->lsp->ls_stateid.data));
3383 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003384 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003385 }
3386 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3387out:
3388 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3389}
3390
3391static void nfs4_lock_release(void *calldata)
3392{
3393 struct nfs4_lockdata *data = calldata;
3394
3395 dprintk("%s: begin!\n", __FUNCTION__);
3396 if (data->arg.open_seqid != NULL)
3397 nfs_free_seqid(data->arg.open_seqid);
3398 if (data->cancelled != 0) {
3399 struct rpc_task *task;
3400 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3401 data->arg.lock_seqid);
3402 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003403 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003404 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3405 } else
3406 nfs_free_seqid(data->arg.lock_seqid);
3407 nfs4_put_lock_state(data->lsp);
3408 put_nfs_open_context(data->ctx);
3409 kfree(data);
3410 dprintk("%s: done!\n", __FUNCTION__);
3411}
3412
3413static const struct rpc_call_ops nfs4_lock_ops = {
3414 .rpc_call_prepare = nfs4_lock_prepare,
3415 .rpc_call_done = nfs4_lock_done,
3416 .rpc_release = nfs4_lock_release,
3417};
3418
3419static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3420{
3421 struct nfs4_lockdata *data;
3422 struct rpc_task *task;
3423 int ret;
3424
3425 dprintk("%s: begin!\n", __FUNCTION__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003426 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003427 fl->fl_u.nfs4_fl.owner);
3428 if (data == NULL)
3429 return -ENOMEM;
3430 if (IS_SETLKW(cmd))
3431 data->arg.block = 1;
3432 if (reclaim != 0)
3433 data->arg.reclaim = 1;
3434 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3435 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003436 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003437 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003438 ret = nfs4_wait_for_completion_rpc_task(task);
3439 if (ret == 0) {
3440 ret = data->rpc_status;
3441 if (ret == -NFS4ERR_DENIED)
3442 ret = -EAGAIN;
3443 } else
3444 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003445 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003446 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
3450static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3451{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003452 struct nfs_server *server = NFS_SERVER(state->inode);
3453 struct nfs4_exception exception = { };
3454 int err;
3455
3456 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003457 /* Cache the lock if possible... */
3458 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3459 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003460 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3461 if (err != -NFS4ERR_DELAY)
3462 break;
3463 nfs4_handle_exception(server, err, &exception);
3464 } while (exception.retry);
3465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466}
3467
3468static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3469{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003470 struct nfs_server *server = NFS_SERVER(state->inode);
3471 struct nfs4_exception exception = { };
3472 int err;
3473
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003474 err = nfs4_set_lock_state(state, request);
3475 if (err != 0)
3476 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003477 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003478 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3479 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003480 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3481 if (err != -NFS4ERR_DELAY)
3482 break;
3483 nfs4_handle_exception(server, err, &exception);
3484 } while (exception.retry);
3485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
3488static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3489{
David Howellsadfa6f92006-08-22 20:06:08 -04003490 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003491 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 int status;
3493
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003494 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003495 status = nfs4_set_lock_state(state, request);
3496 if (status != 0)
3497 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003498 request->fl_flags |= FL_ACCESS;
3499 status = do_vfs_lock(request->fl_file, request);
3500 if (status < 0)
3501 goto out;
3502 down_read(&clp->cl_sem);
3503 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3504 struct nfs_inode *nfsi = NFS_I(state->inode);
3505 /* Yes: cache locks! */
3506 down_read(&nfsi->rwsem);
3507 /* ...but avoid races with delegation recall... */
3508 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3509 request->fl_flags = fl_flags & ~FL_SLEEP;
3510 status = do_vfs_lock(request->fl_file, request);
3511 up_read(&nfsi->rwsem);
3512 goto out_unlock;
3513 }
3514 up_read(&nfsi->rwsem);
3515 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003516 status = _nfs4_do_setlk(state, cmd, request, 0);
3517 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003518 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003519 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003520 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003521 if (do_vfs_lock(request->fl_file, request) < 0)
3522 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003523out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003525out:
3526 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return status;
3528}
3529
3530static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3531{
3532 struct nfs4_exception exception = { };
3533 int err;
3534
3535 do {
3536 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3537 _nfs4_proc_setlk(state, cmd, request),
3538 &exception);
3539 } while (exception.retry);
3540 return err;
3541}
3542
3543static int
3544nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3545{
3546 struct nfs_open_context *ctx;
3547 struct nfs4_state *state;
3548 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3549 int status;
3550
3551 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003552 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 state = ctx->state;
3554
3555 if (request->fl_start < 0 || request->fl_end < 0)
3556 return -EINVAL;
3557
3558 if (IS_GETLK(cmd))
3559 return nfs4_proc_getlk(state, F_GETLK, request);
3560
3561 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3562 return -EINVAL;
3563
3564 if (request->fl_type == F_UNLCK)
3565 return nfs4_proc_unlck(state, cmd, request);
3566
3567 do {
3568 status = nfs4_proc_setlk(state, cmd, request);
3569 if ((status != -EAGAIN) || IS_SETLK(cmd))
3570 break;
3571 timeout = nfs4_set_lock_task_retry(timeout);
3572 status = -ERESTARTSYS;
3573 if (signalled())
3574 break;
3575 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return status;
3577}
3578
Trond Myklebust888e6942005-11-04 15:38:11 -05003579int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3580{
3581 struct nfs_server *server = NFS_SERVER(state->inode);
3582 struct nfs4_exception exception = { };
3583 int err;
3584
3585 err = nfs4_set_lock_state(state, fl);
3586 if (err != 0)
3587 goto out;
3588 do {
3589 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3590 if (err != -NFS4ERR_DELAY)
3591 break;
3592 err = nfs4_handle_exception(server, err, &exception);
3593 } while (exception.retry);
3594out:
3595 return err;
3596}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003597
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003598#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3599
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003600int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3601 size_t buflen, int flags)
3602{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003603 struct inode *inode = dentry->d_inode;
3604
3605 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3606 return -EOPNOTSUPP;
3607
3608 if (!S_ISREG(inode->i_mode) &&
3609 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3610 return -EPERM;
3611
3612 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003613}
3614
3615/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3616 * and that's what we'll do for e.g. user attributes that haven't been set.
3617 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3618 * attributes in kernel-managed attribute namespaces. */
3619ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3620 size_t buflen)
3621{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003622 struct inode *inode = dentry->d_inode;
3623
3624 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3625 return -EOPNOTSUPP;
3626
3627 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003628}
3629
3630ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3631{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003632 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003633
J. Bruce Fields096455a2006-03-20 23:23:42 -05003634 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3635 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003636 if (buf && buflen < len)
3637 return -ERANGE;
3638 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003639 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3640 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003641}
3642
Trond Myklebust56659e92007-07-17 21:52:39 -04003643int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003644 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003645{
3646 struct nfs_server *server = NFS_SERVER(dir);
3647 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003648 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3649 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003650 };
3651 struct nfs4_fs_locations_arg args = {
3652 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003653 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003654 .page = page,
3655 .bitmask = bitmask,
3656 };
3657 struct rpc_message msg = {
3658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3659 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003660 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003661 };
3662 int status;
3663
3664 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003665 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003666 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003667 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003668 status = rpc_call_sync(server->client, &msg, 0);
3669 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3670 return status;
3671}
3672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3674 .recover_open = nfs4_open_reclaim,
3675 .recover_lock = nfs4_lock_reclaim,
3676};
3677
3678struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3679 .recover_open = nfs4_open_expired,
3680 .recover_lock = nfs4_lock_expired,
3681};
3682
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003683static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003684 .permission = nfs_permission,
3685 .getattr = nfs_getattr,
3686 .setattr = nfs_setattr,
3687 .getxattr = nfs4_getxattr,
3688 .setxattr = nfs4_setxattr,
3689 .listxattr = nfs4_listxattr,
3690};
3691
David Howells509de812006-08-22 20:06:11 -04003692const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 .version = 4, /* protocol version */
3694 .dentry_ops = &nfs4_dentry_operations,
3695 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003696 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 .getroot = nfs4_proc_get_root,
3698 .getattr = nfs4_proc_getattr,
3699 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003700 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 .lookup = nfs4_proc_lookup,
3702 .access = nfs4_proc_access,
3703 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .create = nfs4_proc_create,
3705 .remove = nfs4_proc_remove,
3706 .unlink_setup = nfs4_proc_unlink_setup,
3707 .unlink_done = nfs4_proc_unlink_done,
3708 .rename = nfs4_proc_rename,
3709 .link = nfs4_proc_link,
3710 .symlink = nfs4_proc_symlink,
3711 .mkdir = nfs4_proc_mkdir,
3712 .rmdir = nfs4_proc_remove,
3713 .readdir = nfs4_proc_readdir,
3714 .mknod = nfs4_proc_mknod,
3715 .statfs = nfs4_proc_statfs,
3716 .fsinfo = nfs4_proc_fsinfo,
3717 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003718 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 .decode_dirent = nfs4_decode_dirent,
3720 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003721 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003723 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003725 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003726 .file_open = nfs_open,
3727 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003729 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730};
3731
3732/*
3733 * Local variables:
3734 * c-basic-offset: 8
3735 * End:
3736 */