blob: 61ba32af4d2fd4716f071b3df36a5797c024a7a5 [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070066static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
David Howellsadfa6f92006-08-22 20:06:08 -040067static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Prevent leaks of NFSv4 errors into userland */
70int nfs4_map_errors(int err)
71{
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
74 __FUNCTION__, -err);
75 return -EIO;
76 }
77 return err;
78}
79
80/*
81 * This is our standard bitmap for GETATTR requests.
82 */
83const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98};
99
100const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107};
108
Trond Myklebust4ce79712005-06-22 17:16:21 +0000109const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120};
121
Manoj Naik830b8e32006-06-09 09:34:25 -0400122const u32 nfs4_fs_locations_bitmap[2] = {
123 FATTR4_WORD0_TYPE
124 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_SIZE
126 | FATTR4_WORD0_FSID
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
139};
140
Al Virobc4785c2006-10-19 23:28:51 -0700141static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct nfs4_readdir_arg *readdir)
143{
Al Viro0dbb4c62006-10-19 23:28:49 -0700144 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 BUG_ON(readdir->count < 80);
147 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000148 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
150 return;
151 }
152
153 readdir->cookie = 0;
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
155 if (cookie == 2)
156 return;
157
158 /*
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
163 * instead of 1 or 2.
164 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (cookie == 0) {
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
173 p++;
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
177 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
178 }
179
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
185 p++;
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
189 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
190
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
194}
195
Trond Myklebust26e976a2006-01-03 09:55:21 +0100196static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howells7539bba2006-08-22 20:06:09 -0400198 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock(&clp->cl_lock);
200 if (time_before(clp->cl_last_renewal,timestamp))
201 clp->cl_last_renewal = timestamp;
202 spin_unlock(&clp->cl_lock);
203}
204
Trond Myklebust38478b22006-05-25 01:40:57 -0400205static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Trond Myklebust38478b22006-05-25 01:40:57 -0400207 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Trond Myklebust38478b22006-05-25 01:40:57 -0400209 spin_lock(&dir->i_lock);
210 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
212 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400213 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100216struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400217 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100218 struct nfs_openargs o_arg;
219 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100220 struct nfs_open_confirmargs c_arg;
221 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100222 struct nfs_fattr f_attr;
223 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400224 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100225 struct dentry *dir;
226 struct nfs4_state_owner *owner;
227 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100228 unsigned long timestamp;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100229 int rpc_status;
230 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100231};
232
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400233
234static void nfs4_init_opendata_res(struct nfs4_opendata *p)
235{
236 p->o_res.f_attr = &p->f_attr;
237 p->o_res.dir_attr = &p->dir_attr;
238 p->o_res.server = p->o_arg.server;
239 nfs_fattr_init(&p->f_attr);
240 nfs_fattr_init(&p->dir_attr);
241}
242
Trond Myklebustad389da2007-06-05 12:30:00 -0400243static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100244 struct nfs4_state_owner *sp, int flags,
245 const struct iattr *attrs)
246{
Trond Myklebustad389da2007-06-05 12:30:00 -0400247 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100248 struct inode *dir = parent->d_inode;
249 struct nfs_server *server = NFS_SERVER(dir);
250 struct nfs4_opendata *p;
251
252 p = kzalloc(sizeof(*p), GFP_KERNEL);
253 if (p == NULL)
254 goto err;
255 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
256 if (p->o_arg.seqid == NULL)
257 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400258 p->path.mnt = mntget(path->mnt);
259 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100260 p->dir = parent;
261 p->owner = sp;
262 atomic_inc(&sp->so_count);
263 p->o_arg.fh = NFS_FH(dir);
264 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400265 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400266 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400267 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100268 p->o_arg.server = server;
269 p->o_arg.bitmask = server->attr_bitmask;
270 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100271 if (flags & O_EXCL) {
272 u32 *s = (u32 *) p->o_arg.u.verifier.data;
273 s[0] = jiffies;
274 s[1] = current->pid;
275 } else if (flags & O_CREAT) {
276 p->o_arg.u.attrs = &p->attrs;
277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
278 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100279 p->c_arg.fh = &p->o_res.fh;
280 p->c_arg.stateid = &p->o_res.stateid;
281 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400282 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400283 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100284 return p;
285err_free:
286 kfree(p);
287err:
288 dput(parent);
289 return NULL;
290}
291
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400292static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100293{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400294 struct nfs4_opendata *p = container_of(kref,
295 struct nfs4_opendata, kref);
296
297 nfs_free_seqid(p->o_arg.seqid);
298 nfs4_put_state_owner(p->owner);
299 dput(p->dir);
300 dput(p->path.dentry);
301 mntput(p->path.mnt);
302 kfree(p);
303}
304
305static void nfs4_opendata_put(struct nfs4_opendata *p)
306{
307 if (p != NULL)
308 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100309}
310
Trond Myklebust06f814a2006-01-03 09:55:07 +0100311static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
312{
313 sigset_t oldset;
314 int ret;
315
316 rpc_clnt_sigmask(task->tk_client, &oldset);
317 ret = rpc_wait_for_completion_task(task);
318 rpc_clnt_sigunmask(task->tk_client, &oldset);
319 return ret;
320}
321
Trond Myklebuste7616922006-01-03 09:55:13 +0100322static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
323{
324 switch (open_flags) {
325 case FMODE_WRITE:
326 state->n_wronly++;
327 break;
328 case FMODE_READ:
329 state->n_rdonly++;
330 break;
331 case FMODE_READ|FMODE_WRITE:
332 state->n_rdwr++;
333 }
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
337{
338 struct inode *inode = state->inode;
339
340 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500341 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700342 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500344 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100345 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500346 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700348 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100351static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
352{
353 struct inode *inode;
354 struct nfs4_state *state = NULL;
355
356 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
357 goto out;
358 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500359 if (IS_ERR(inode))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100360 goto out;
361 state = nfs4_get_open_state(inode, data->owner);
362 if (state == NULL)
363 goto put_inode;
364 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400365 if (data->o_res.delegation_type != 0) {
366 struct nfs_inode *nfsi = NFS_I(inode);
367 int delegation_flags = 0;
368
369 if (nfsi->delegation)
370 delegation_flags = nfsi->delegation->flags;
371 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
372 nfs_inode_set_delegation(state->inode,
373 data->owner->so_cred,
374 &data->o_res);
375 else
376 nfs_inode_reclaim_delegation(state->inode,
377 data->owner->so_cred,
378 &data->o_res);
379 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100380put_inode:
381 iput(inode);
382out:
383 return state;
384}
385
Trond Myklebust864472e2006-01-03 09:55:15 +0100386static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
387{
388 struct nfs_inode *nfsi = NFS_I(state->inode);
389 struct nfs_open_context *ctx;
390
391 spin_lock(&state->inode->i_lock);
392 list_for_each_entry(ctx, &nfsi->open_files, list) {
393 if (ctx->state != state)
394 continue;
395 get_nfs_open_context(ctx);
396 spin_unlock(&state->inode->i_lock);
397 return ctx;
398 }
399 spin_unlock(&state->inode->i_lock);
400 return ERR_PTR(-ENOENT);
401}
402
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400403static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100404{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400405 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100406 int ret;
407
408 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400409 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
410 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
411 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100412 ret = _nfs4_proc_open(opendata);
413 if (ret != 0)
414 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400415 newstate = nfs4_opendata_to_nfs4_state(opendata);
416 if (newstate != NULL)
417 nfs4_close_state(&opendata->path, newstate, openflags);
418 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100419 return 0;
420}
421
422static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
423{
Trond Myklebust864472e2006-01-03 09:55:15 +0100424 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100425 int ret;
426
427 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400428 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100429 smp_rmb();
430 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400431 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100432 if (ret != 0)
433 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400434 if (newstate != state)
435 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100436 }
437 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400438 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100439 if (ret != 0)
440 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400441 if (newstate != state)
442 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100443 }
444 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400445 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100446 if (ret != 0)
447 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400448 if (newstate != state)
449 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100450 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100451 return 0;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * OPEN_RECLAIM:
456 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400458static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Trond Myklebust864472e2006-01-03 09:55:15 +0100460 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
461 struct nfs4_opendata *opendata;
462 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int status;
464
465 if (delegation != NULL) {
466 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
467 memcpy(&state->stateid, &delegation->stateid,
468 sizeof(state->stateid));
469 set_bit(NFS_DELEGATED_STATE, &state->flags);
470 return 0;
471 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100472 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400474 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
Trond Myklebust864472e2006-01-03 09:55:15 +0100475 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700476 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100477 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
478 opendata->o_arg.fh = NFS_FH(state->inode);
479 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
480 opendata->o_arg.u.delegation_type = delegation_type;
481 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400482 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return status;
484}
485
Trond Myklebust539cd032007-06-05 11:46:42 -0400486static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct nfs_server *server = NFS_SERVER(state->inode);
489 struct nfs4_exception exception = { };
490 int err;
491 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400492 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000493 if (err != -NFS4ERR_DELAY)
494 break;
495 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } while (exception.retry);
497 return err;
498}
499
Trond Myklebust864472e2006-01-03 09:55:15 +0100500static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
501{
502 struct nfs_open_context *ctx;
503 int ret;
504
505 ctx = nfs4_state_find_open_context(state);
506 if (IS_ERR(ctx))
507 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400508 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100509 put_nfs_open_context(ctx);
510 return ret;
511}
512
Trond Myklebust13437e12007-07-06 15:10:43 -0400513static 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 -0700514{
515 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100516 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Trond Myklebustad389da2007-06-05 12:30:00 -0400519 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100520 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100521 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100522 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400523 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100524 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100525 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400526 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Trond Myklebust13437e12007-07-06 15:10:43 -0400530int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400533 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int err;
535 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400536 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 switch (err) {
538 case 0:
539 return err;
540 case -NFS4ERR_STALE_CLIENTID:
541 case -NFS4ERR_STALE_STATEID:
542 case -NFS4ERR_EXPIRED:
543 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400544 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return err;
546 }
547 err = nfs4_handle_exception(server, err, &exception);
548 } while (exception.retry);
549 return err;
550}
551
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100552static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100554 struct nfs4_opendata *data = calldata;
555 struct rpc_message msg = {
556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
557 .rpc_argp = &data->c_arg,
558 .rpc_resp = &data->c_res,
559 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100561 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100562 rpc_call_setup(task, &msg, 0);
563}
564
565static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
566{
567 struct nfs4_opendata *data = calldata;
568
569 data->rpc_status = task->tk_status;
570 if (RPC_ASSASSINATED(task))
571 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100572 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100573 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
574 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100575 renew_lease(data->o_res.server, data->timestamp);
576 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100577 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400578 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100579}
580
581static void nfs4_open_confirm_release(void *calldata)
582{
583 struct nfs4_opendata *data = calldata;
584 struct nfs4_state *state = NULL;
585
586 /* If this request hasn't been cancelled, do nothing */
587 if (data->cancelled == 0)
588 goto out_free;
589 /* In case of error, no cleanup! */
590 if (data->rpc_status != 0)
591 goto out_free;
592 nfs_confirm_seqid(&data->owner->so_seqid, 0);
593 state = nfs4_opendata_to_nfs4_state(data);
594 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400595 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100596out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400597 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100598}
599
600static const struct rpc_call_ops nfs4_open_confirm_ops = {
601 .rpc_call_prepare = nfs4_open_confirm_prepare,
602 .rpc_call_done = nfs4_open_confirm_done,
603 .rpc_release = nfs4_open_confirm_release,
604};
605
606/*
607 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
608 */
609static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
610{
611 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
612 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int status;
614
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400615 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500616 /*
617 * If rpc_run_task() ends up calling ->rpc_release(), we
618 * want to ensure that it takes the 'error' code path.
619 */
620 data->rpc_status = -ENOMEM;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100621 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500622 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100623 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100624 status = nfs4_wait_for_completion_rpc_task(task);
625 if (status != 0) {
626 data->cancelled = 1;
627 smp_wmb();
628 } else
629 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500630 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return status;
632}
633
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100634static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100636 struct nfs4_opendata *data = calldata;
637 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct rpc_message msg = {
639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100640 .rpc_argp = &data->o_arg,
641 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 .rpc_cred = sp->so_cred,
643 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100644
645 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
646 return;
647 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400648 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100649 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100650 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
651 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100652 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100653 rpc_call_setup(task, &msg, 0);
654}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100656static void nfs4_open_done(struct rpc_task *task, void *calldata)
657{
658 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100660 data->rpc_status = task->tk_status;
661 if (RPC_ASSASSINATED(task))
662 return;
663 if (task->tk_status == 0) {
664 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700665 case S_IFREG:
666 break;
667 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100668 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700669 break;
670 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100671 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700672 break;
673 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100674 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700675 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100676 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400677 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
678 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700679 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100680 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
681}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700682
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100683static void nfs4_open_release(void *calldata)
684{
685 struct nfs4_opendata *data = calldata;
686 struct nfs4_state *state = NULL;
687
688 /* If this request hasn't been cancelled, do nothing */
689 if (data->cancelled == 0)
690 goto out_free;
691 /* In case of error, no cleanup! */
692 if (data->rpc_status != 0)
693 goto out_free;
694 /* In case we need an open_confirm, no cleanup! */
695 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
696 goto out_free;
697 nfs_confirm_seqid(&data->owner->so_seqid, 0);
698 state = nfs4_opendata_to_nfs4_state(data);
699 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400700 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100701out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400702 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100703}
704
705static const struct rpc_call_ops nfs4_open_ops = {
706 .rpc_call_prepare = nfs4_open_prepare,
707 .rpc_call_done = nfs4_open_done,
708 .rpc_release = nfs4_open_release,
709};
710
711/*
712 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
713 */
714static int _nfs4_proc_open(struct nfs4_opendata *data)
715{
716 struct inode *dir = data->dir->d_inode;
717 struct nfs_server *server = NFS_SERVER(dir);
718 struct nfs_openargs *o_arg = &data->o_arg;
719 struct nfs_openres *o_res = &data->o_res;
720 struct rpc_task *task;
721 int status;
722
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400723 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500724 /*
725 * If rpc_run_task() ends up calling ->rpc_release(), we
726 * want to ensure that it takes the 'error' code path.
727 */
728 data->rpc_status = -ENOMEM;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400729 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100730 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500731 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100732 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100733 status = nfs4_wait_for_completion_rpc_task(task);
734 if (status != 0) {
735 data->cancelled = 1;
736 smp_wmb();
737 } else
738 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500739 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100741 return status;
742
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400743 if (o_arg->open_flags & O_CREAT) {
744 update_changeattr(dir, &o_res->cinfo);
745 nfs_post_op_update_inode(dir, o_res->dir_attr);
746 } else
747 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100749 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100751 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400754 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
759{
760 struct nfs_access_entry cache;
761 int mask = 0;
762 int status;
763
764 if (openflags & FMODE_READ)
765 mask |= MAY_READ;
766 if (openflags & FMODE_WRITE)
767 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400768 if (openflags & FMODE_EXEC)
769 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 status = nfs_access_get_cached(inode, cred, &cache);
771 if (status == 0)
772 goto out;
773
774 /* Be clever: ask server to check for all possible rights */
775 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
776 cache.cred = cred;
777 cache.jiffies = jiffies;
778 status = _nfs4_proc_access(inode, &cache);
779 if (status != 0)
780 return status;
781 nfs_access_add_cache(inode, &cache);
782out:
783 if ((cache.mask & mask) == mask)
784 return 0;
785 return -EACCES;
786}
787
Trond Myklebust10afec92007-05-14 17:16:04 -0400788static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100789{
David Howells7539bba2006-08-22 20:06:09 -0400790 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400791 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100792
Trond Myklebust6b309542006-09-14 14:03:14 -0400793 for (;;) {
794 ret = nfs4_wait_clnt_recover(server->client, clp);
795 if (ret != 0)
796 return ret;
797 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
798 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100799 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400800 }
801 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/*
805 * OPEN_EXPIRED:
806 * reclaim state on the server after a network partition.
807 * Assumes caller holds the appropriate lock
808 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400809static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100814 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100815 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400818 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100819 if (ret < 0)
820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
822 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400825 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100827 return -ENOMEM;
828 ret = nfs4_open_recover(opendata, state);
829 if (ret == -ESTALE) {
830 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400831 nfs4_drop_state_owner(state->owner);
832 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400834 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Trond Myklebust539cd032007-06-05 11:46:42 -0400838static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000839{
Trond Myklebust539cd032007-06-05 11:46:42 -0400840 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000841 struct nfs4_exception exception = { };
842 int err;
843
844 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400845 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000846 if (err == -NFS4ERR_DELAY)
847 nfs4_handle_exception(server, err, &exception);
848 } while (exception.retry);
849 return err;
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Trond Myklebust864472e2006-01-03 09:55:15 +0100857 ctx = nfs4_state_find_open_context(state);
858 if (IS_ERR(ctx))
859 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400860 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100861 put_nfs_open_context(ctx);
862 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100866 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
868static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
869{
870 struct nfs_delegation *delegation;
871 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400872 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct nfs_inode *nfsi = NFS_I(inode);
874 struct nfs4_state_owner *sp = NULL;
875 struct nfs4_state *state = NULL;
876 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
877 int err;
878
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100879 err = -ENOMEM;
880 if (!(sp = nfs4_get_state_owner(server, cred))) {
881 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
882 return err;
883 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100884 err = nfs4_recover_expired_lease(server);
885 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100886 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Protect against reboot recovery - NOTE ORDER! */
888 down_read(&clp->cl_sem);
889 /* Protect against delegation recall */
890 down_read(&nfsi->rwsem);
891 delegation = NFS_I(inode)->delegation;
892 err = -ENOENT;
893 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
894 goto out_err;
895 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 state = nfs4_get_open_state(inode, sp);
897 if (state == NULL)
898 goto out_err;
899
900 err = -ENOENT;
901 if ((state->state & open_flags) == open_flags) {
902 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100903 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&inode->i_lock);
905 goto out_ok;
906 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100907 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 lock_kernel();
910 err = _nfs4_do_access(inode, cred, open_flags);
911 unlock_kernel();
912 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100913 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 set_bit(NFS_DELEGATED_STATE, &state->flags);
915 update_open_stateid(state, &delegation->stateid, open_flags);
916out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 nfs4_put_state_owner(sp);
918 up_read(&nfsi->rwsem);
919 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100921 return 0;
922out_put_open_state:
923 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 up_read(&nfsi->rwsem);
926 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700927 if (err != -EACCES)
928 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100929out_put_state_owner:
930 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return err;
932}
933
934static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
935{
936 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500937 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int err;
939
940 do {
941 err = _nfs4_open_delegated(inode, flags, cred, &res);
942 if (err == 0)
943 break;
944 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
945 err, &exception));
946 } while (exception.retry);
947 return res;
948}
949
950/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400951 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
952 * fields corresponding to attributes that were used to store the verifier.
953 * Make sure we clobber those fields in the later setattr call
954 */
955static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
956{
957 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
958 !(sattr->ia_valid & ATTR_ATIME_SET))
959 sattr->ia_valid |= ATTR_ATIME;
960
961 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
962 !(sattr->ia_valid & ATTR_MTIME_SET))
963 sattr->ia_valid |= ATTR_MTIME;
964}
965
966/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100967 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Trond Myklebustad389da2007-06-05 12:30:00 -0400969static 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 -0700970{
971 struct nfs4_state_owner *sp;
972 struct nfs4_state *state = NULL;
973 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -0400974 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100975 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 status = -ENOMEM;
980 if (!(sp = nfs4_get_state_owner(server, cred))) {
981 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
982 goto out_err;
983 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100984 status = nfs4_recover_expired_lease(server);
985 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100986 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100987 down_read(&clp->cl_sem);
988 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -0400989 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -0400991 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400995 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400997 if (opendata->o_arg.open_flags & O_EXCL)
998 nfs4_exclusive_attrset(opendata, sattr);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001001 state = nfs4_opendata_to_nfs4_state(opendata);
1002 if (state == NULL)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001003 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001004 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 nfs4_put_state_owner(sp);
1006 up_read(&clp->cl_sem);
1007 *res = state;
1008 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001009err_opendata_put:
1010 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001011err_release_rwsem:
1012 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013err_put_state_owner:
1014 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *res = NULL;
1017 return status;
1018}
1019
1020
Trond Myklebustad389da2007-06-05 12:30:00 -04001021static 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 -07001022{
1023 struct nfs4_exception exception = { };
1024 struct nfs4_state *res;
1025 int status;
1026
1027 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001028 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (status == 0)
1030 break;
1031 /* NOTE: BAD_SEQID means the server and client disagree about the
1032 * book-keeping w.r.t. state-changing operations
1033 * (OPEN/CLOSE/LOCK/LOCKU...)
1034 * It is actually a sign of a bug on the client or on the server.
1035 *
1036 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001037 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * have unhashed the old state_owner for us, and that we can
1039 * therefore safely retry using a new one. We should still warn
1040 * the user though...
1041 */
1042 if (status == -NFS4ERR_BAD_SEQID) {
1043 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1044 exception.retry = 1;
1045 continue;
1046 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001047 /*
1048 * BAD_STATEID on OPEN means that the server cancelled our
1049 * state before it received the OPEN_CONFIRM.
1050 * Recover by retrying the request as per the discussion
1051 * on Page 181 of RFC3530.
1052 */
1053 if (status == -NFS4ERR_BAD_STATEID) {
1054 exception.retry = 1;
1055 continue;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1058 status, &exception));
1059 } while (exception.retry);
1060 return res;
1061}
1062
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001063static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1064 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001066 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001068 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .iap = sattr,
1070 .server = server,
1071 .bitmask = server->attr_bitmask,
1072 };
1073 struct nfs_setattrres res = {
1074 .fattr = fattr,
1075 .server = server,
1076 };
1077 struct rpc_message msg = {
1078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1079 .rpc_argp = &arg,
1080 .rpc_resp = &res,
1081 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001082 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001083 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Trond Myklebust0e574af2005-10-27 22:12:38 -04001085 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001087 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1088 /* Use that stateid */
1089 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001091 nfs4_copy_stateid(&arg.stateid, state, current->files);
1092 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1094
Trond Myklebust65e43082005-08-16 11:49:44 -04001095 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001096 if (status == 0 && state != NULL)
1097 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001098 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001101static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1102 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001104 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct nfs4_exception exception = { };
1106 int err;
1107 do {
1108 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001109 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 &exception);
1111 } while (exception.retry);
1112 return err;
1113}
1114
1115struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001116 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct inode *inode;
1118 struct nfs4_state *state;
1119 struct nfs_closeargs arg;
1120 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001121 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001122 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123};
1124
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001125static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001126{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001127 struct nfs4_closedata *calldata = data;
1128 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001129
1130 nfs4_put_open_state(calldata->state);
1131 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001132 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001133 dput(calldata->path.dentry);
1134 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001135 kfree(calldata);
1136}
1137
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001138static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001140 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct nfs_server *server = NFS_SERVER(calldata->inode);
1143
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001144 if (RPC_ASSASSINATED(task))
1145 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* hmm. we are done with the inode, and in the process of freeing
1147 * the state_owner. we keep this around to process errors
1148 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001149 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 switch (task->tk_status) {
1151 case 0:
1152 memcpy(&state->stateid, &calldata->res.stateid,
1153 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001154 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 case -NFS4ERR_STALE_STATEID:
1157 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 default:
1160 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1161 rpc_restart_call(task);
1162 return;
1163 }
1164 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001165 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001168static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001170 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001171 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct rpc_message msg = {
1173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1174 .rpc_argp = &calldata->arg,
1175 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001176 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001178 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001179
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001180 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001181 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001182 /* Recalculate the new open mode in case someone reopened the file
1183 * while we were waiting in line to be scheduled.
1184 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001185 spin_lock(&state->owner->so_lock);
1186 spin_lock(&calldata->inode->i_lock);
1187 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001188 if (state->n_rdwr == 0) {
1189 if (state->n_rdonly == 0)
1190 mode &= ~FMODE_READ;
1191 if (state->n_wronly == 0)
1192 mode &= ~FMODE_WRITE;
1193 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001194 nfs4_state_set_mode_locked(state, mode);
1195 spin_unlock(&calldata->inode->i_lock);
1196 spin_unlock(&state->owner->so_lock);
1197 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001198 /* Note: exit _without_ calling nfs4_close_done */
1199 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001200 return;
1201 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001202 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001203 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001205 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001206 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001207 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001210static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001211 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001212 .rpc_call_done = nfs4_close_done,
1213 .rpc_release = nfs4_free_closedata,
1214};
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/*
1217 * It is possible for data to be read/written from a mem-mapped file
1218 * after the sys_close call (which hits the vfs layer as a flush).
1219 * This means that we can't safely call nfsv4 close on a file until
1220 * the inode is cleared. This in turn means that we are not good
1221 * NFSv4 citizens - we do not indicate to the server to update the file's
1222 * share state even when we are done with one of the three share
1223 * stateid's in the inode.
1224 *
1225 * NOTE: Caller must be holding the sp->so_owner semaphore!
1226 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001227int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001229 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001231 struct nfs4_state_owner *sp = state->owner;
1232 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001233 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Trond Myklebust95121352005-10-18 14:20:12 -07001235 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001237 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001238 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001240 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001241 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001243 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001244 if (calldata->arg.seqid == NULL)
1245 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001246 calldata->arg.bitmask = server->attr_bitmask;
1247 calldata->res.fattr = &calldata->fattr;
1248 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001249 calldata->path.mnt = mntget(path->mnt);
1250 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001251
Trond Myklebustb39e6252007-06-11 23:05:07 -04001252 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1253 if (IS_ERR(task))
1254 return PTR_ERR(task);
1255 rpc_put_task(task);
1256 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001257out_free_calldata:
1258 kfree(calldata);
1259out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001260 nfs4_put_open_state(state);
1261 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001262 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001265static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001266{
1267 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001268 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001269
Trond Myklebust1b45c462007-07-03 13:04:56 -04001270 /* If the open_intent is for execute, we have an extra check to make */
1271 if (nd->intent.open.flags & FMODE_EXEC) {
1272 ret = _nfs4_do_access(state->inode,
1273 state->owner->so_cred,
1274 nd->intent.open.flags);
1275 if (ret < 0)
1276 goto out_close;
1277 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001278 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001279 if (!IS_ERR(filp)) {
1280 struct nfs_open_context *ctx;
1281 ctx = (struct nfs_open_context *)filp->private_data;
1282 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001283 return 0;
1284 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001285 ret = PTR_ERR(filp);
1286out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001287 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001288 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001289}
1290
1291struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1293{
Trond Myklebustad389da2007-06-05 12:30:00 -04001294 struct path path = {
1295 .mnt = nd->mnt,
1296 .dentry = dentry,
1297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct iattr attr;
1299 struct rpc_cred *cred;
1300 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001301 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (nd->flags & LOOKUP_CREATE) {
1304 attr.ia_mode = nd->intent.open.create_mode;
1305 attr.ia_valid = ATTR_MODE;
1306 if (!IS_POSIXACL(dir))
1307 attr.ia_mode &= ~current->fs->umask;
1308 } else {
1309 attr.ia_valid = 0;
1310 BUG_ON(nd->intent.open.flags & O_CREAT);
1311 }
1312
David Howells1f163412006-08-22 20:06:11 -04001313 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001315 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001316 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001318 if (IS_ERR(state)) {
1319 if (PTR_ERR(state) == -ENOENT)
1320 d_add(dentry, NULL);
1321 return (struct dentry *)state;
1322 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001323 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001324 if (res != NULL)
1325 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001326 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001327 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001331nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebustad389da2007-06-05 12:30:00 -04001333 struct path path = {
1334 .mnt = nd->mnt,
1335 .dentry = dentry,
1336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct rpc_cred *cred;
1338 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
David Howells1f163412006-08-22 20:06:11 -04001340 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (IS_ERR(cred))
1342 return PTR_ERR(cred);
1343 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1344 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001345 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001347 if (IS_ERR(state)) {
1348 switch (PTR_ERR(state)) {
1349 case -EPERM:
1350 case -EACCES:
1351 case -EDQUOT:
1352 case -ENOSPC:
1353 case -EROFS:
1354 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1355 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001356 default:
1357 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001358 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001359 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001361 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 1;
1363 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001364 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001365out_drop:
1366 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369
1370
1371static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1372{
1373 struct nfs4_server_caps_res res = {};
1374 struct rpc_message msg = {
1375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1376 .rpc_argp = fhandle,
1377 .rpc_resp = &res,
1378 };
1379 int status;
1380
1381 status = rpc_call_sync(server->client, &msg, 0);
1382 if (status == 0) {
1383 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1384 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1385 server->caps |= NFS_CAP_ACLS;
1386 if (res.has_links != 0)
1387 server->caps |= NFS_CAP_HARDLINKS;
1388 if (res.has_symlinks != 0)
1389 server->caps |= NFS_CAP_SYMLINKS;
1390 server->acl_bitmask = res.acl_bitmask;
1391 }
1392 return status;
1393}
1394
Trond Myklebust55a97592006-06-09 09:34:19 -04001395int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct nfs4_exception exception = { };
1398 int err;
1399 do {
1400 err = nfs4_handle_exception(server,
1401 _nfs4_server_capabilities(server, fhandle),
1402 &exception);
1403 } while (exception.retry);
1404 return err;
1405}
1406
1407static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1408 struct nfs_fsinfo *info)
1409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct nfs4_lookup_root_arg args = {
1411 .bitmask = nfs4_fattr_bitmap,
1412 };
1413 struct nfs4_lookup_res res = {
1414 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001415 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 .fh = fhandle,
1417 };
1418 struct rpc_message msg = {
1419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1420 .rpc_argp = &args,
1421 .rpc_resp = &res,
1422 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001423 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return rpc_call_sync(server->client, &msg, 0);
1425}
1426
1427static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1428 struct nfs_fsinfo *info)
1429{
1430 struct nfs4_exception exception = { };
1431 int err;
1432 do {
1433 err = nfs4_handle_exception(server,
1434 _nfs4_lookup_root(server, fhandle, info),
1435 &exception);
1436 } while (exception.retry);
1437 return err;
1438}
1439
David Howells54ceac42006-08-22 20:06:13 -04001440/*
1441 * get the file handle for the "/" directory on the server
1442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001444 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int status;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (status == 0)
1450 status = nfs4_server_capabilities(server, fhandle);
1451 if (status == 0)
1452 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001453 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Manoj Naik6b97fd32006-06-09 09:34:29 -04001456/*
1457 * Get locations and (maybe) other attributes of a referral.
1458 * Note that we'll actually follow the referral later when
1459 * we detect fsid mismatch in inode revalidation
1460 */
1461static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1462{
1463 int status = -ENOMEM;
1464 struct page *page = NULL;
1465 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001466
1467 page = alloc_page(GFP_KERNEL);
1468 if (page == NULL)
1469 goto out;
1470 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1471 if (locations == NULL)
1472 goto out;
1473
Trond Myklebustc228fd32007-01-13 02:28:11 -05001474 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001475 if (status != 0)
1476 goto out;
1477 /* Make sure server returned a different fsid for the referral */
1478 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1479 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1480 status = -EIO;
1481 goto out;
1482 }
1483
1484 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1485 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1486 if (!fattr->mode)
1487 fattr->mode = S_IFDIR;
1488 memset(fhandle, 0, sizeof(struct nfs_fh));
1489out:
1490 if (page)
1491 __free_page(page);
1492 if (locations)
1493 kfree(locations);
1494 return status;
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1498{
1499 struct nfs4_getattr_arg args = {
1500 .fh = fhandle,
1501 .bitmask = server->attr_bitmask,
1502 };
1503 struct nfs4_getattr_res res = {
1504 .fattr = fattr,
1505 .server = server,
1506 };
1507 struct rpc_message msg = {
1508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1509 .rpc_argp = &args,
1510 .rpc_resp = &res,
1511 };
1512
Trond Myklebust0e574af2005-10-27 22:12:38 -04001513 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return rpc_call_sync(server->client, &msg, 0);
1515}
1516
1517static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1518{
1519 struct nfs4_exception exception = { };
1520 int err;
1521 do {
1522 err = nfs4_handle_exception(server,
1523 _nfs4_proc_getattr(server, fhandle, fattr),
1524 &exception);
1525 } while (exception.retry);
1526 return err;
1527}
1528
1529/*
1530 * The file is not closed if it is opened due to the a request to change
1531 * the size of the file. The open call will not be needed once the
1532 * VFS layer lookup-intents are implemented.
1533 *
1534 * Close is called when the inode is destroyed.
1535 * If we haven't opened the file for O_WRONLY, we
1536 * need to in the size_change case to obtain a stateid.
1537 *
1538 * Got race?
1539 * Because OPEN is always done by name in nfsv4, it is
1540 * possible that we opened a different file by the same
1541 * name. We can recognize this race condition, but we
1542 * can't do anything about it besides returning an error.
1543 *
1544 * This will be fixed with VFS changes (lookup-intent).
1545 */
1546static int
1547nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1548 struct iattr *sattr)
1549{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001550 struct rpc_cred *cred;
1551 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001552 struct nfs_open_context *ctx;
1553 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int status;
1555
Trond Myklebust0e574af2005-10-27 22:12:38 -04001556 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
David Howells1f163412006-08-22 20:06:11 -04001558 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001559 if (IS_ERR(cred))
1560 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001561
1562 /* Search for an existing open(O_WRITE) file */
1563 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1564 if (ctx != NULL)
1565 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001566
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001567 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001568 if (status == 0)
1569 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001570 if (ctx != NULL)
1571 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001572 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return status;
1574}
1575
David Howells2b3de442006-08-22 20:06:09 -04001576static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1577 struct qstr *name, struct nfs_fh *fhandle,
1578 struct nfs_fattr *fattr)
1579{
1580 int status;
1581 struct nfs4_lookup_arg args = {
1582 .bitmask = server->attr_bitmask,
1583 .dir_fh = dirfh,
1584 .name = name,
1585 };
1586 struct nfs4_lookup_res res = {
1587 .server = server,
1588 .fattr = fattr,
1589 .fh = fhandle,
1590 };
1591 struct rpc_message msg = {
1592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1593 .rpc_argp = &args,
1594 .rpc_resp = &res,
1595 };
1596
1597 nfs_fattr_init(fattr);
1598
1599 dprintk("NFS call lookupfh %s\n", name->name);
1600 status = rpc_call_sync(server->client, &msg, 0);
1601 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001602 return status;
1603}
1604
1605static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1606 struct qstr *name, struct nfs_fh *fhandle,
1607 struct nfs_fattr *fattr)
1608{
1609 struct nfs4_exception exception = { };
1610 int err;
1611 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001612 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1613 /* FIXME: !!!! */
1614 if (err == -NFS4ERR_MOVED) {
1615 err = -EREMOTE;
1616 break;
1617 }
1618 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001619 } while (exception.retry);
1620 return err;
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1624 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1625{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001626 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001629 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001630 if (status == -NFS4ERR_MOVED)
1631 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 dprintk("NFS reply lookup: %d\n", status);
1633 return status;
1634}
1635
1636static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1637{
1638 struct nfs4_exception exception = { };
1639 int err;
1640 do {
1641 err = nfs4_handle_exception(NFS_SERVER(dir),
1642 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1643 &exception);
1644 } while (exception.retry);
1645 return err;
1646}
1647
1648static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1649{
1650 struct nfs4_accessargs args = {
1651 .fh = NFS_FH(inode),
1652 };
1653 struct nfs4_accessres res = { 0 };
1654 struct rpc_message msg = {
1655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1656 .rpc_argp = &args,
1657 .rpc_resp = &res,
1658 .rpc_cred = entry->cred,
1659 };
1660 int mode = entry->mask;
1661 int status;
1662
1663 /*
1664 * Determine which access bits we want to ask for...
1665 */
1666 if (mode & MAY_READ)
1667 args.access |= NFS4_ACCESS_READ;
1668 if (S_ISDIR(inode->i_mode)) {
1669 if (mode & MAY_WRITE)
1670 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1671 if (mode & MAY_EXEC)
1672 args.access |= NFS4_ACCESS_LOOKUP;
1673 } else {
1674 if (mode & MAY_WRITE)
1675 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1676 if (mode & MAY_EXEC)
1677 args.access |= NFS4_ACCESS_EXECUTE;
1678 }
1679 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1680 if (!status) {
1681 entry->mask = 0;
1682 if (res.access & NFS4_ACCESS_READ)
1683 entry->mask |= MAY_READ;
1684 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1685 entry->mask |= MAY_WRITE;
1686 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1687 entry->mask |= MAY_EXEC;
1688 }
1689 return status;
1690}
1691
1692static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1693{
1694 struct nfs4_exception exception = { };
1695 int err;
1696 do {
1697 err = nfs4_handle_exception(NFS_SERVER(inode),
1698 _nfs4_proc_access(inode, entry),
1699 &exception);
1700 } while (exception.retry);
1701 return err;
1702}
1703
1704/*
1705 * TODO: For the time being, we don't try to get any attributes
1706 * along with any of the zero-copy operations READ, READDIR,
1707 * READLINK, WRITE.
1708 *
1709 * In the case of the first three, we want to put the GETATTR
1710 * after the read-type operation -- this is because it is hard
1711 * to predict the length of a GETATTR response in v4, and thus
1712 * align the READ data correctly. This means that the GETATTR
1713 * may end up partially falling into the page cache, and we should
1714 * shift it into the 'tail' of the xdr_buf before processing.
1715 * To do this efficiently, we need to know the total length
1716 * of data received, which doesn't seem to be available outside
1717 * of the RPC layer.
1718 *
1719 * In the case of WRITE, we also want to put the GETATTR after
1720 * the operation -- in this case because we want to make sure
1721 * we get the post-operation mtime and size. This means that
1722 * we can't use xdr_encode_pages() as written: we need a variant
1723 * of it which would leave room in the 'tail' iovec.
1724 *
1725 * Both of these changes to the XDR layer would in fact be quite
1726 * minor, but I decided to leave them for a subsequent patch.
1727 */
1728static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1729 unsigned int pgbase, unsigned int pglen)
1730{
1731 struct nfs4_readlink args = {
1732 .fh = NFS_FH(inode),
1733 .pgbase = pgbase,
1734 .pglen = pglen,
1735 .pages = &page,
1736 };
1737 struct rpc_message msg = {
1738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1739 .rpc_argp = &args,
1740 .rpc_resp = NULL,
1741 };
1742
1743 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1744}
1745
1746static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1747 unsigned int pgbase, unsigned int pglen)
1748{
1749 struct nfs4_exception exception = { };
1750 int err;
1751 do {
1752 err = nfs4_handle_exception(NFS_SERVER(inode),
1753 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1754 &exception);
1755 } while (exception.retry);
1756 return err;
1757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/*
1760 * Got race?
1761 * We will need to arrange for the VFS layer to provide an atomic open.
1762 * Until then, this create/open method is prone to inefficiency and race
1763 * conditions due to the lookup, create, and open VFS calls from sys_open()
1764 * placed on the wire.
1765 *
1766 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1767 * The file will be opened again in the subsequent VFS open call
1768 * (nfs4_proc_file_open).
1769 *
1770 * The open for read will just hang around to be used by any process that
1771 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1772 */
1773
1774static int
1775nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001776 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Trond Myklebustad389da2007-06-05 12:30:00 -04001778 struct path path = {
1779 .mnt = nd->mnt,
1780 .dentry = dentry,
1781 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct nfs4_state *state;
1783 struct rpc_cred *cred;
1784 int status = 0;
1785
David Howells1f163412006-08-22 20:06:11 -04001786 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (IS_ERR(cred)) {
1788 status = PTR_ERR(cred);
1789 goto out;
1790 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001791 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 put_rpccred(cred);
1793 if (IS_ERR(state)) {
1794 status = PTR_ERR(state);
1795 goto out;
1796 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001797 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (flags & O_EXCL) {
1799 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001800 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001801 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001802 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001803 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001804 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001805 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001806 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001807 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001808 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809out:
1810 return status;
1811}
1812
1813static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1814{
Trond Myklebust16e42952005-10-27 22:12:44 -04001815 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct nfs4_remove_arg args = {
1817 .fh = NFS_FH(dir),
1818 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001819 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001821 struct nfs_fattr dir_attr;
1822 struct nfs4_remove_res res = {
1823 .server = server,
1824 .dir_attr = &dir_attr,
1825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct rpc_message msg = {
1827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1828 .rpc_argp = &args,
1829 .rpc_resp = &res,
1830 };
1831 int status;
1832
Trond Myklebust16e42952005-10-27 22:12:44 -04001833 nfs_fattr_init(res.dir_attr);
1834 status = rpc_call_sync(server->client, &msg, 0);
1835 if (status == 0) {
1836 update_changeattr(dir, &res.cinfo);
1837 nfs_post_op_update_inode(dir, res.dir_attr);
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return status;
1840}
1841
1842static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1843{
1844 struct nfs4_exception exception = { };
1845 int err;
1846 do {
1847 err = nfs4_handle_exception(NFS_SERVER(dir),
1848 _nfs4_proc_remove(dir, name),
1849 &exception);
1850 } while (exception.retry);
1851 return err;
1852}
1853
1854struct unlink_desc {
1855 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001856 struct nfs4_remove_res res;
1857 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858};
1859
1860static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1861 struct qstr *name)
1862{
Trond Myklebust16e42952005-10-27 22:12:44 -04001863 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct unlink_desc *up;
1865
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001866 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!up)
1868 return -ENOMEM;
1869
1870 up->args.fh = NFS_FH(dir->d_inode);
1871 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001872 up->args.bitmask = server->attr_bitmask;
1873 up->res.server = server;
1874 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1877 msg->rpc_argp = &up->args;
1878 msg->rpc_resp = &up->res;
1879 return 0;
1880}
1881
1882static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1883{
1884 struct rpc_message *msg = &task->tk_msg;
1885 struct unlink_desc *up;
1886
1887 if (msg->rpc_resp != NULL) {
1888 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001889 update_changeattr(dir->d_inode, &up->res.cinfo);
1890 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 kfree(up);
1892 msg->rpc_resp = NULL;
1893 msg->rpc_argp = NULL;
1894 }
1895 return 0;
1896}
1897
1898static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1899 struct inode *new_dir, struct qstr *new_name)
1900{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001901 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct nfs4_rename_arg arg = {
1903 .old_dir = NFS_FH(old_dir),
1904 .new_dir = NFS_FH(new_dir),
1905 .old_name = old_name,
1906 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001907 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001909 struct nfs_fattr old_fattr, new_fattr;
1910 struct nfs4_rename_res res = {
1911 .server = server,
1912 .old_fattr = &old_fattr,
1913 .new_fattr = &new_fattr,
1914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct rpc_message msg = {
1916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1917 .rpc_argp = &arg,
1918 .rpc_resp = &res,
1919 };
1920 int status;
1921
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001922 nfs_fattr_init(res.old_fattr);
1923 nfs_fattr_init(res.new_fattr);
1924 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 if (!status) {
1927 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001928 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001930 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 return status;
1933}
1934
1935static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1936 struct inode *new_dir, struct qstr *new_name)
1937{
1938 struct nfs4_exception exception = { };
1939 int err;
1940 do {
1941 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1942 _nfs4_proc_rename(old_dir, old_name,
1943 new_dir, new_name),
1944 &exception);
1945 } while (exception.retry);
1946 return err;
1947}
1948
1949static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1950{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001951 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs4_link_arg arg = {
1953 .fh = NFS_FH(inode),
1954 .dir_fh = NFS_FH(dir),
1955 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001956 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001958 struct nfs_fattr fattr, dir_attr;
1959 struct nfs4_link_res res = {
1960 .server = server,
1961 .fattr = &fattr,
1962 .dir_attr = &dir_attr,
1963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct rpc_message msg = {
1965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1966 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001967 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 };
1969 int status;
1970
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001971 nfs_fattr_init(res.fattr);
1972 nfs_fattr_init(res.dir_attr);
1973 status = rpc_call_sync(server->client, &msg, 0);
1974 if (!status) {
1975 update_changeattr(dir, &res.cinfo);
1976 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04001977 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 return status;
1981}
1982
1983static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1984{
1985 struct nfs4_exception exception = { };
1986 int err;
1987 do {
1988 err = nfs4_handle_exception(NFS_SERVER(inode),
1989 _nfs4_proc_link(inode, dir, name),
1990 &exception);
1991 } while (exception.retry);
1992 return err;
1993}
1994
Chuck Lever4f390c12006-08-22 20:06:22 -04001995static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04001996 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04001999 struct nfs_fh fhandle;
2000 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct nfs4_create_arg arg = {
2002 .dir_fh = NFS_FH(dir),
2003 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002004 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 .attrs = sattr,
2006 .ftype = NF4LNK,
2007 .bitmask = server->attr_bitmask,
2008 };
2009 struct nfs4_create_res res = {
2010 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002011 .fh = &fhandle,
2012 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002013 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 };
2015 struct rpc_message msg = {
2016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2017 .rpc_argp = &arg,
2018 .rpc_resp = &res,
2019 };
2020 int status;
2021
Chuck Lever94a6d752006-08-22 20:06:23 -04002022 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002024
Chuck Lever94a6d752006-08-22 20:06:23 -04002025 arg.u.symlink.pages = &page;
2026 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002027 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002028 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002031 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002033 nfs_post_op_update_inode(dir, res.dir_fattr);
2034 status = nfs_instantiate(dentry, &fhandle, &fattr);
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Chuck Lever4f390c12006-08-22 20:06:22 -04002039static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002040 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 struct nfs4_exception exception = { };
2043 int err;
2044 do {
2045 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002046 _nfs4_proc_symlink(dir, dentry, page,
2047 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 &exception);
2049 } while (exception.retry);
2050 return err;
2051}
2052
2053static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2054 struct iattr *sattr)
2055{
2056 struct nfs_server *server = NFS_SERVER(dir);
2057 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002058 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct nfs4_create_arg arg = {
2060 .dir_fh = NFS_FH(dir),
2061 .server = server,
2062 .name = &dentry->d_name,
2063 .attrs = sattr,
2064 .ftype = NF4DIR,
2065 .bitmask = server->attr_bitmask,
2066 };
2067 struct nfs4_create_res res = {
2068 .server = server,
2069 .fh = &fhandle,
2070 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002071 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 };
2073 struct rpc_message msg = {
2074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2075 .rpc_argp = &arg,
2076 .rpc_resp = &res,
2077 };
2078 int status;
2079
Trond Myklebust0e574af2005-10-27 22:12:38 -04002080 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002081 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2084 if (!status) {
2085 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002086 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 status = nfs_instantiate(dentry, &fhandle, &fattr);
2088 }
2089 return status;
2090}
2091
2092static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2093 struct iattr *sattr)
2094{
2095 struct nfs4_exception exception = { };
2096 int err;
2097 do {
2098 err = nfs4_handle_exception(NFS_SERVER(dir),
2099 _nfs4_proc_mkdir(dir, dentry, sattr),
2100 &exception);
2101 } while (exception.retry);
2102 return err;
2103}
2104
2105static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2106 u64 cookie, struct page *page, unsigned int count, int plus)
2107{
2108 struct inode *dir = dentry->d_inode;
2109 struct nfs4_readdir_arg args = {
2110 .fh = NFS_FH(dir),
2111 .pages = &page,
2112 .pgbase = 0,
2113 .count = count,
2114 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2115 };
2116 struct nfs4_readdir_res res;
2117 struct rpc_message msg = {
2118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2119 .rpc_argp = &args,
2120 .rpc_resp = &res,
2121 .rpc_cred = cred,
2122 };
2123 int status;
2124
Trond Myklebusteadf4592005-06-22 17:16:39 +00002125 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2126 dentry->d_parent->d_name.name,
2127 dentry->d_name.name,
2128 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2130 res.pgbase = args.pgbase;
2131 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2132 if (status == 0)
2133 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002134 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return status;
2136}
2137
2138static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2139 u64 cookie, struct page *page, unsigned int count, int plus)
2140{
2141 struct nfs4_exception exception = { };
2142 int err;
2143 do {
2144 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2145 _nfs4_proc_readdir(dentry, cred, cookie,
2146 page, count, plus),
2147 &exception);
2148 } while (exception.retry);
2149 return err;
2150}
2151
2152static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2153 struct iattr *sattr, dev_t rdev)
2154{
2155 struct nfs_server *server = NFS_SERVER(dir);
2156 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002157 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct nfs4_create_arg arg = {
2159 .dir_fh = NFS_FH(dir),
2160 .server = server,
2161 .name = &dentry->d_name,
2162 .attrs = sattr,
2163 .bitmask = server->attr_bitmask,
2164 };
2165 struct nfs4_create_res res = {
2166 .server = server,
2167 .fh = &fh,
2168 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002169 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 };
2171 struct rpc_message msg = {
2172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2173 .rpc_argp = &arg,
2174 .rpc_resp = &res,
2175 };
2176 int status;
2177 int mode = sattr->ia_mode;
2178
Trond Myklebust0e574af2005-10-27 22:12:38 -04002179 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002180 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2183 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2184 if (S_ISFIFO(mode))
2185 arg.ftype = NF4FIFO;
2186 else if (S_ISBLK(mode)) {
2187 arg.ftype = NF4BLK;
2188 arg.u.device.specdata1 = MAJOR(rdev);
2189 arg.u.device.specdata2 = MINOR(rdev);
2190 }
2191 else if (S_ISCHR(mode)) {
2192 arg.ftype = NF4CHR;
2193 arg.u.device.specdata1 = MAJOR(rdev);
2194 arg.u.device.specdata2 = MINOR(rdev);
2195 }
2196 else
2197 arg.ftype = NF4SOCK;
2198
2199 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2200 if (status == 0) {
2201 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002202 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 status = nfs_instantiate(dentry, &fh, &fattr);
2204 }
2205 return status;
2206}
2207
2208static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2209 struct iattr *sattr, dev_t rdev)
2210{
2211 struct nfs4_exception exception = { };
2212 int err;
2213 do {
2214 err = nfs4_handle_exception(NFS_SERVER(dir),
2215 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2216 &exception);
2217 } while (exception.retry);
2218 return err;
2219}
2220
2221static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2222 struct nfs_fsstat *fsstat)
2223{
2224 struct nfs4_statfs_arg args = {
2225 .fh = fhandle,
2226 .bitmask = server->attr_bitmask,
2227 };
2228 struct rpc_message msg = {
2229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2230 .rpc_argp = &args,
2231 .rpc_resp = fsstat,
2232 };
2233
Trond Myklebust0e574af2005-10-27 22:12:38 -04002234 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return rpc_call_sync(server->client, &msg, 0);
2236}
2237
2238static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2239{
2240 struct nfs4_exception exception = { };
2241 int err;
2242 do {
2243 err = nfs4_handle_exception(server,
2244 _nfs4_proc_statfs(server, fhandle, fsstat),
2245 &exception);
2246 } while (exception.retry);
2247 return err;
2248}
2249
2250static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2251 struct nfs_fsinfo *fsinfo)
2252{
2253 struct nfs4_fsinfo_arg args = {
2254 .fh = fhandle,
2255 .bitmask = server->attr_bitmask,
2256 };
2257 struct rpc_message msg = {
2258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2259 .rpc_argp = &args,
2260 .rpc_resp = fsinfo,
2261 };
2262
2263 return rpc_call_sync(server->client, &msg, 0);
2264}
2265
2266static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2267{
2268 struct nfs4_exception exception = { };
2269 int err;
2270
2271 do {
2272 err = nfs4_handle_exception(server,
2273 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2274 &exception);
2275 } while (exception.retry);
2276 return err;
2277}
2278
2279static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2280{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002281 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2283}
2284
2285static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2286 struct nfs_pathconf *pathconf)
2287{
2288 struct nfs4_pathconf_arg args = {
2289 .fh = fhandle,
2290 .bitmask = server->attr_bitmask,
2291 };
2292 struct rpc_message msg = {
2293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2294 .rpc_argp = &args,
2295 .rpc_resp = pathconf,
2296 };
2297
2298 /* None of the pathconf attributes are mandatory to implement */
2299 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2300 memset(pathconf, 0, sizeof(*pathconf));
2301 return 0;
2302 }
2303
Trond Myklebust0e574af2005-10-27 22:12:38 -04002304 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return rpc_call_sync(server->client, &msg, 0);
2306}
2307
2308static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2309 struct nfs_pathconf *pathconf)
2310{
2311 struct nfs4_exception exception = { };
2312 int err;
2313
2314 do {
2315 err = nfs4_handle_exception(server,
2316 _nfs4_proc_pathconf(server, fhandle, pathconf),
2317 &exception);
2318 } while (exception.retry);
2319 return err;
2320}
2321
Trond Myklebustec06c092006-03-20 13:44:27 -05002322static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Trond Myklebustec06c092006-03-20 13:44:27 -05002324 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Trond Myklebustec06c092006-03-20 13:44:27 -05002326 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002328 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002331 renew_lease(server, data->timestamp);
2332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Trond Myklebustec06c092006-03-20 13:44:27 -05002335static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2339 .rpc_argp = &data->args,
2340 .rpc_resp = &data->res,
2341 .rpc_cred = data->cred,
2342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 data->timestamp = jiffies;
2345
Trond Myklebustec06c092006-03-20 13:44:27 -05002346 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Trond Myklebust788e7a82006-03-20 13:44:27 -05002349static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct inode *inode = data->inode;
2352
2353 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2354 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002355 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002357 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002359 nfs_post_op_update_inode(inode, data->res.fattr);
2360 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Trond Myklebust788e7a82006-03-20 13:44:27 -05002364static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2368 .rpc_argp = &data->args,
2369 .rpc_resp = &data->res,
2370 .rpc_cred = data->cred,
2371 };
2372 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002373 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 if (how & FLUSH_STABLE) {
2377 if (!NFS_I(inode)->ncommit)
2378 stable = NFS_FILE_SYNC;
2379 else
2380 stable = NFS_DATA_SYNC;
2381 } else
2382 stable = NFS_UNSTABLE;
2383 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002384 data->args.bitmask = server->attr_bitmask;
2385 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 data->timestamp = jiffies;
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002390 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Trond Myklebust788e7a82006-03-20 13:44:27 -05002393static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct inode *inode = data->inode;
2396
2397 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2398 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002399 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002401 if (task->tk_status >= 0)
2402 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Trond Myklebust788e7a82006-03-20 13:44:27 -05002406static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct rpc_message msg = {
2409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2410 .rpc_argp = &data->args,
2411 .rpc_resp = &data->res,
2412 .rpc_cred = data->cred,
2413 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002414 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002416 data->args.bitmask = server->attr_bitmask;
2417 data->res.server = server;
2418
Trond Myklebust788e7a82006-03-20 13:44:27 -05002419 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
2422/*
2423 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2424 * standalone procedure for queueing an asynchronous RENEW.
2425 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002426static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
David Howellsadfa6f92006-08-22 20:06:08 -04002428 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002429 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 if (task->tk_status < 0) {
2432 switch (task->tk_status) {
2433 case -NFS4ERR_STALE_CLIENTID:
2434 case -NFS4ERR_EXPIRED:
2435 case -NFS4ERR_CB_PATH_DOWN:
2436 nfs4_schedule_state_recovery(clp);
2437 }
2438 return;
2439 }
2440 spin_lock(&clp->cl_lock);
2441 if (time_before(clp->cl_last_renewal,timestamp))
2442 clp->cl_last_renewal = timestamp;
2443 spin_unlock(&clp->cl_lock);
2444}
2445
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002446static const struct rpc_call_ops nfs4_renew_ops = {
2447 .rpc_call_done = nfs4_renew_done,
2448};
2449
David Howellsadfa6f92006-08-22 20:06:08 -04002450int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 struct rpc_message msg = {
2453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2454 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002455 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 };
2457
2458 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002459 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
David Howellsadfa6f92006-08-22 20:06:08 -04002462int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct rpc_message msg = {
2465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2466 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002467 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 };
2469 unsigned long now = jiffies;
2470 int status;
2471
2472 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2473 if (status < 0)
2474 return status;
2475 spin_lock(&clp->cl_lock);
2476 if (time_before(clp->cl_last_renewal,now))
2477 clp->cl_last_renewal = now;
2478 spin_unlock(&clp->cl_lock);
2479 return 0;
2480}
2481
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002482static inline int nfs4_server_supports_acls(struct nfs_server *server)
2483{
2484 return (server->caps & NFS_CAP_ACLS)
2485 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2486 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2487}
2488
2489/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2490 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2491 * the stack.
2492 */
2493#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2494
2495static void buf_to_pages(const void *buf, size_t buflen,
2496 struct page **pages, unsigned int *pgbase)
2497{
2498 const void *p = buf;
2499
2500 *pgbase = offset_in_page(buf);
2501 p -= *pgbase;
2502 while (p < buf + buflen) {
2503 *(pages++) = virt_to_page(p);
2504 p += PAGE_CACHE_SIZE;
2505 }
2506}
2507
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002508struct nfs4_cached_acl {
2509 int cached;
2510 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002511 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002512};
2513
2514static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002515{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002516 struct nfs_inode *nfsi = NFS_I(inode);
2517
2518 spin_lock(&inode->i_lock);
2519 kfree(nfsi->nfs4_acl);
2520 nfsi->nfs4_acl = acl;
2521 spin_unlock(&inode->i_lock);
2522}
2523
2524static void nfs4_zap_acl_attr(struct inode *inode)
2525{
2526 nfs4_set_cached_acl(inode, NULL);
2527}
2528
2529static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2530{
2531 struct nfs_inode *nfsi = NFS_I(inode);
2532 struct nfs4_cached_acl *acl;
2533 int ret = -ENOENT;
2534
2535 spin_lock(&inode->i_lock);
2536 acl = nfsi->nfs4_acl;
2537 if (acl == NULL)
2538 goto out;
2539 if (buf == NULL) /* user is just asking for length */
2540 goto out_len;
2541 if (acl->cached == 0)
2542 goto out;
2543 ret = -ERANGE; /* see getxattr(2) man page */
2544 if (acl->len > buflen)
2545 goto out;
2546 memcpy(buf, acl->data, acl->len);
2547out_len:
2548 ret = acl->len;
2549out:
2550 spin_unlock(&inode->i_lock);
2551 return ret;
2552}
2553
2554static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2555{
2556 struct nfs4_cached_acl *acl;
2557
2558 if (buf && acl_len <= PAGE_SIZE) {
2559 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2560 if (acl == NULL)
2561 goto out;
2562 acl->cached = 1;
2563 memcpy(acl->data, buf, acl_len);
2564 } else {
2565 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2566 if (acl == NULL)
2567 goto out;
2568 acl->cached = 0;
2569 }
2570 acl->len = acl_len;
2571out:
2572 nfs4_set_cached_acl(inode, acl);
2573}
2574
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002575static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002576{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002577 struct page *pages[NFS4ACL_MAXPAGES];
2578 struct nfs_getaclargs args = {
2579 .fh = NFS_FH(inode),
2580 .acl_pages = pages,
2581 .acl_len = buflen,
2582 };
2583 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002584 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2587 .rpc_argp = &args,
2588 .rpc_resp = &resp_len,
2589 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002590 struct page *localpage = NULL;
2591 int ret;
2592
2593 if (buflen < PAGE_SIZE) {
2594 /* As long as we're doing a round trip to the server anyway,
2595 * let's be prepared for a page of acl data. */
2596 localpage = alloc_page(GFP_KERNEL);
2597 resp_buf = page_address(localpage);
2598 if (localpage == NULL)
2599 return -ENOMEM;
2600 args.acl_pages[0] = localpage;
2601 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002602 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002603 } else {
2604 resp_buf = buf;
2605 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2606 }
2607 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2608 if (ret)
2609 goto out_free;
2610 if (resp_len > args.acl_len)
2611 nfs4_write_cached_acl(inode, NULL, resp_len);
2612 else
2613 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2614 if (buf) {
2615 ret = -ERANGE;
2616 if (resp_len > buflen)
2617 goto out_free;
2618 if (localpage)
2619 memcpy(buf, resp_buf, resp_len);
2620 }
2621 ret = resp_len;
2622out_free:
2623 if (localpage)
2624 __free_page(localpage);
2625 return ret;
2626}
2627
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002628static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2629{
2630 struct nfs4_exception exception = { };
2631 ssize_t ret;
2632 do {
2633 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2634 if (ret >= 0)
2635 break;
2636 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2637 } while (exception.retry);
2638 return ret;
2639}
2640
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002641static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2642{
2643 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002644 int ret;
2645
2646 if (!nfs4_server_supports_acls(server))
2647 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002648 ret = nfs_revalidate_inode(server, inode);
2649 if (ret < 0)
2650 return ret;
2651 ret = nfs4_read_cached_acl(inode, buf, buflen);
2652 if (ret != -ENOENT)
2653 return ret;
2654 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002655}
2656
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002657static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002658{
2659 struct nfs_server *server = NFS_SERVER(inode);
2660 struct page *pages[NFS4ACL_MAXPAGES];
2661 struct nfs_setaclargs arg = {
2662 .fh = NFS_FH(inode),
2663 .acl_pages = pages,
2664 .acl_len = buflen,
2665 };
2666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2668 .rpc_argp = &arg,
2669 .rpc_resp = NULL,
2670 };
2671 int ret;
2672
2673 if (!nfs4_server_supports_acls(server))
2674 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002675 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002676 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002677 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002678 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002679 return ret;
2680}
2681
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002682static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2683{
2684 struct nfs4_exception exception = { };
2685 int err;
2686 do {
2687 err = nfs4_handle_exception(NFS_SERVER(inode),
2688 __nfs4_proc_set_acl(inode, buf, buflen),
2689 &exception);
2690 } while (exception.retry);
2691 return err;
2692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002695nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
David Howells7539bba2006-08-22 20:06:09 -04002697 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 if (!clp || task->tk_status >= 0)
2700 return 0;
2701 switch(task->tk_status) {
2702 case -NFS4ERR_STALE_CLIENTID:
2703 case -NFS4ERR_STALE_STATEID:
2704 case -NFS4ERR_EXPIRED:
2705 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2706 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002707 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 rpc_wake_up_task(task);
2709 task->tk_status = 0;
2710 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002712 nfs_inc_server_stats((struct nfs_server *) server,
2713 NFSIOS_DELAY);
2714 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2716 task->tk_status = 0;
2717 return -EAGAIN;
2718 case -NFS4ERR_OLD_STATEID:
2719 task->tk_status = 0;
2720 return -EAGAIN;
2721 }
2722 task->tk_status = nfs4_map_errors(task->tk_status);
2723 return 0;
2724}
2725
Trond Myklebust433fbe42006-01-03 09:55:22 +01002726static int nfs4_wait_bit_interruptible(void *word)
2727{
2728 if (signal_pending(current))
2729 return -ERESTARTSYS;
2730 schedule();
2731 return 0;
2732}
2733
David Howellsadfa6f92006-08-22 20:06:08 -04002734static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002737 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 might_sleep();
2740
Trond Myklebuste1485822006-12-13 16:43:13 -05002741 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002744 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2745 nfs4_wait_bit_interruptible,
2746 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002748
2749 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return res;
2751}
2752
2753static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2754{
2755 sigset_t oldset;
2756 int res = 0;
2757
2758 might_sleep();
2759
2760 if (*timeout <= 0)
2761 *timeout = NFS4_POLL_RETRY_MIN;
2762 if (*timeout > NFS4_POLL_RETRY_MAX)
2763 *timeout = NFS4_POLL_RETRY_MAX;
2764 rpc_clnt_sigmask(clnt, &oldset);
2765 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002766 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (signalled())
2768 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002769 } else
2770 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 rpc_clnt_sigunmask(clnt, &oldset);
2772 *timeout <<= 1;
2773 return res;
2774}
2775
2776/* This is the error handling routine for processes that are allowed
2777 * to sleep.
2778 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002779static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
David Howells7539bba2006-08-22 20:06:09 -04002781 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 int ret = errorcode;
2783
2784 exception->retry = 0;
2785 switch(errorcode) {
2786 case 0:
2787 return 0;
2788 case -NFS4ERR_STALE_CLIENTID:
2789 case -NFS4ERR_STALE_STATEID:
2790 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002791 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 ret = nfs4_wait_clnt_recover(server->client, clp);
2793 if (ret == 0)
2794 exception->retry = 1;
2795 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002796 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 case -NFS4ERR_GRACE:
2798 case -NFS4ERR_DELAY:
2799 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002800 if (ret != 0)
2801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002803 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805 /* We failed to handle the error */
2806 return nfs4_map_errors(ret);
2807}
2808
David Howellsadfa6f92006-08-22 20:06:08 -04002809int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 nfs4_verifier sc_verifier;
2812 struct nfs4_setclientid setclientid = {
2813 .sc_verifier = &sc_verifier,
2814 .sc_prog = program,
2815 };
2816 struct rpc_message msg = {
2817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2818 .rpc_argp = &setclientid,
2819 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002820 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
Al Virobc4785c2006-10-19 23:28:51 -07002822 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int loop = 0;
2824 int status;
2825
Al Virobc4785c2006-10-19 23:28:51 -07002826 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2828 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2829
2830 for(;;) {
2831 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2832 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002833 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002834 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 clp->cl_id_uniquifier);
2836 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2837 sizeof(setclientid.sc_netid), "tcp");
2838 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2839 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2840 clp->cl_ipaddr, port >> 8, port & 255);
2841
2842 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2843 if (status != -NFS4ERR_CLID_INUSE)
2844 break;
2845 if (signalled())
2846 break;
2847 if (loop++ & 1)
2848 ssleep(clp->cl_lease_time + 1);
2849 else
2850 if (++clp->cl_id_uniquifier == 0)
2851 break;
2852 }
2853 return status;
2854}
2855
David Howellsadfa6f92006-08-22 20:06:08 -04002856static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct nfs_fsinfo fsinfo;
2859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2861 .rpc_argp = clp,
2862 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002863 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 };
2865 unsigned long now;
2866 int status;
2867
2868 now = jiffies;
2869 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2870 if (status == 0) {
2871 spin_lock(&clp->cl_lock);
2872 clp->cl_lease_time = fsinfo.lease_time * HZ;
2873 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002874 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 spin_unlock(&clp->cl_lock);
2876 }
2877 return status;
2878}
2879
David Howellsadfa6f92006-08-22 20:06:08 -04002880int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002881{
2882 long timeout;
2883 int err;
2884 do {
2885 err = _nfs4_proc_setclientid_confirm(clp, cred);
2886 switch (err) {
2887 case 0:
2888 return err;
2889 case -NFS4ERR_RESOURCE:
2890 /* The IBM lawyers misread another document! */
2891 case -NFS4ERR_DELAY:
2892 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2893 }
2894 } while (err == 0);
2895 return err;
2896}
2897
Trond Myklebustfe650402006-01-03 09:55:18 +01002898struct nfs4_delegreturndata {
2899 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002900 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002901 struct nfs_fh fh;
2902 nfs4_stateid stateid;
2903 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002904 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002905 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002906 int rpc_status;
2907};
2908
2909static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Trond Myklebustfe650402006-01-03 09:55:18 +01002911 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 struct rpc_message msg = {
2913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002914 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002915 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002916 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002918 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002919 rpc_call_setup(task, &msg, 0);
2920}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebustfe650402006-01-03 09:55:18 +01002922static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2923{
2924 struct nfs4_delegreturndata *data = calldata;
2925 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002926 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002927 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002928}
2929
2930static void nfs4_delegreturn_release(void *calldata)
2931{
2932 struct nfs4_delegreturndata *data = calldata;
2933
2934 put_rpccred(data->cred);
2935 kfree(calldata);
2936}
2937
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002938static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002939 .rpc_call_prepare = nfs4_delegreturn_prepare,
2940 .rpc_call_done = nfs4_delegreturn_done,
2941 .rpc_release = nfs4_delegreturn_release,
2942};
2943
2944static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2945{
2946 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002947 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002948 struct rpc_task *task;
2949 int status;
2950
2951 data = kmalloc(sizeof(*data), GFP_KERNEL);
2952 if (data == NULL)
2953 return -ENOMEM;
2954 data->args.fhandle = &data->fh;
2955 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002956 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002957 nfs_copy_fh(&data->fh, NFS_FH(inode));
2958 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002959 data->res.fattr = &data->fattr;
2960 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002961 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002962 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002963 data->rpc_status = 0;
2964
2965 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002966 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01002967 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002968 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002969 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002970 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002971 if (status == 0)
2972 nfs_post_op_update_inode(inode, &data->fattr);
2973 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002974 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002975 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2979{
2980 struct nfs_server *server = NFS_SERVER(inode);
2981 struct nfs4_exception exception = { };
2982 int err;
2983 do {
2984 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2985 switch (err) {
2986 case -NFS4ERR_STALE_STATEID:
2987 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 case 0:
2989 return 0;
2990 }
2991 err = nfs4_handle_exception(server, err, &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2997#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2998
2999/*
3000 * sleep, with exponential backoff, and retry the LOCK operation.
3001 */
3002static unsigned long
3003nfs4_set_lock_task_retry(unsigned long timeout)
3004{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003005 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 timeout <<= 1;
3007 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3008 return NFS4_LOCK_MAXTIMEOUT;
3009 return timeout;
3010}
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3013{
3014 struct inode *inode = state->inode;
3015 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003016 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003017 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003019 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003021 struct nfs_lockt_res res = {
3022 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 };
3024 struct rpc_message msg = {
3025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3026 .rpc_argp = &arg,
3027 .rpc_resp = &res,
3028 .rpc_cred = state->owner->so_cred,
3029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_lock_state *lsp;
3031 int status;
3032
3033 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003034 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003035 status = nfs4_set_lock_state(state, request);
3036 if (status != 0)
3037 goto out;
3038 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003039 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003041 switch (status) {
3042 case 0:
3043 request->fl_type = F_UNLCK;
3044 break;
3045 case -NFS4ERR_DENIED:
3046 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003048 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003049out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 up_read(&clp->cl_sem);
3051 return status;
3052}
3053
3054static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058
3059 do {
3060 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3061 _nfs4_proc_getlk(state, cmd, request),
3062 &exception);
3063 } while (exception.retry);
3064 return err;
3065}
3066
3067static int do_vfs_lock(struct file *file, struct file_lock *fl)
3068{
3069 int res = 0;
3070 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3071 case FL_POSIX:
3072 res = posix_lock_file_wait(file, fl);
3073 break;
3074 case FL_FLOCK:
3075 res = flock_lock_file_wait(file, fl);
3076 break;
3077 default:
3078 BUG();
3079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return res;
3081}
3082
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003083struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003084 struct nfs_locku_args arg;
3085 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003086 struct nfs4_lock_state *lsp;
3087 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003088 struct file_lock fl;
3089 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003090 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003091};
3092
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003093static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3094 struct nfs_open_context *ctx,
3095 struct nfs4_lock_state *lsp,
3096 struct nfs_seqid *seqid)
3097{
3098 struct nfs4_unlockdata *p;
3099 struct inode *inode = lsp->ls_state->inode;
3100
3101 p = kmalloc(sizeof(*p), GFP_KERNEL);
3102 if (p == NULL)
3103 return NULL;
3104 p->arg.fh = NFS_FH(inode);
3105 p->arg.fl = &p->fl;
3106 p->arg.seqid = seqid;
3107 p->arg.stateid = &lsp->ls_stateid;
3108 p->lsp = lsp;
3109 atomic_inc(&lsp->ls_count);
3110 /* Ensure we don't close file until we're done freeing locks! */
3111 p->ctx = get_nfs_open_context(ctx);
3112 memcpy(&p->fl, fl, sizeof(p->fl));
3113 p->server = NFS_SERVER(inode);
3114 return p;
3115}
3116
Trond Myklebust06f814a2006-01-03 09:55:07 +01003117static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003118{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003119 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003120 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003121 nfs4_put_lock_state(calldata->lsp);
3122 put_nfs_open_context(calldata->ctx);
3123 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003124}
3125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003126static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003127{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003128 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003129
Trond Myklebust06f814a2006-01-03 09:55:07 +01003130 if (RPC_ASSASSINATED(task))
3131 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003132 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003133 switch (task->tk_status) {
3134 case 0:
3135 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003136 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003137 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003138 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003139 break;
3140 case -NFS4ERR_STALE_STATEID:
3141 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003142 break;
3143 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003144 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003145 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003146 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003147}
3148
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003149static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003150{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003151 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct rpc_message msg = {
3153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003154 .rpc_argp = &calldata->arg,
3155 .rpc_resp = &calldata->res,
3156 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003159 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003160 return;
3161 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003162 /* Note: exit _without_ running nfs4_locku_done */
3163 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003164 return;
3165 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003166 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003167 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003170static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003171 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003173 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174};
3175
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003176static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3177 struct nfs_open_context *ctx,
3178 struct nfs4_lock_state *lsp,
3179 struct nfs_seqid *seqid)
3180{
3181 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003182
3183 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3184 if (data == NULL) {
3185 nfs_free_seqid(seqid);
3186 return ERR_PTR(-ENOMEM);
3187 }
3188
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003189 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3193{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003194 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003195 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003196 struct rpc_task *task;
3197 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Trond Myklebust9b073572006-06-29 16:38:34 -04003199 status = nfs4_set_lock_state(state, request);
3200 /* Unlock _before_ we do the RPC call */
3201 request->fl_flags |= FL_EXISTS;
3202 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3203 goto out;
3204 if (status != 0)
3205 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003206 /* Is this a delegated lock? */
3207 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003208 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003209 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003210 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003211 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003212 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003213 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003214 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3215 status = PTR_ERR(task);
3216 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003217 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003218 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003219 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003220out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003221 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003224struct nfs4_lockdata {
3225 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003226 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003227 struct nfs4_lock_state *lsp;
3228 struct nfs_open_context *ctx;
3229 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003230 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003231 int rpc_status;
3232 int cancelled;
3233};
3234
3235static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3236 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3237{
3238 struct nfs4_lockdata *p;
3239 struct inode *inode = lsp->ls_state->inode;
3240 struct nfs_server *server = NFS_SERVER(inode);
3241
3242 p = kzalloc(sizeof(*p), GFP_KERNEL);
3243 if (p == NULL)
3244 return NULL;
3245
3246 p->arg.fh = NFS_FH(inode);
3247 p->arg.fl = &p->fl;
3248 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3249 if (p->arg.lock_seqid == NULL)
3250 goto out_free;
3251 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003252 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003253 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003254 p->lsp = lsp;
3255 atomic_inc(&lsp->ls_count);
3256 p->ctx = get_nfs_open_context(ctx);
3257 memcpy(&p->fl, fl, sizeof(p->fl));
3258 return p;
3259out_free:
3260 kfree(p);
3261 return NULL;
3262}
3263
3264static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3265{
3266 struct nfs4_lockdata *data = calldata;
3267 struct nfs4_state *state = data->lsp->ls_state;
3268 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3271 .rpc_argp = &data->arg,
3272 .rpc_resp = &data->res,
3273 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003276 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3277 return;
3278 dprintk("%s: begin!\n", __FUNCTION__);
3279 /* Do we need to do an open_to_lock_owner? */
3280 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3281 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3282 if (data->arg.open_seqid == NULL) {
3283 data->rpc_status = -ENOMEM;
3284 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003285 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003286 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003287 data->arg.open_stateid = &state->stateid;
3288 data->arg.new_lock_owner = 1;
3289 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003290 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003291 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003292out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003293 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3294}
3295
3296static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3297{
3298 struct nfs4_lockdata *data = calldata;
3299
3300 dprintk("%s: begin!\n", __FUNCTION__);
3301
3302 data->rpc_status = task->tk_status;
3303 if (RPC_ASSASSINATED(task))
3304 goto out;
3305 if (data->arg.new_lock_owner != 0) {
3306 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3307 if (data->rpc_status == 0)
3308 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3309 else
3310 goto out;
3311 }
3312 if (data->rpc_status == 0) {
3313 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3314 sizeof(data->lsp->ls_stateid.data));
3315 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003316 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003317 }
3318 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3319out:
3320 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3321}
3322
3323static void nfs4_lock_release(void *calldata)
3324{
3325 struct nfs4_lockdata *data = calldata;
3326
3327 dprintk("%s: begin!\n", __FUNCTION__);
3328 if (data->arg.open_seqid != NULL)
3329 nfs_free_seqid(data->arg.open_seqid);
3330 if (data->cancelled != 0) {
3331 struct rpc_task *task;
3332 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3333 data->arg.lock_seqid);
3334 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003335 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003336 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3337 } else
3338 nfs_free_seqid(data->arg.lock_seqid);
3339 nfs4_put_lock_state(data->lsp);
3340 put_nfs_open_context(data->ctx);
3341 kfree(data);
3342 dprintk("%s: done!\n", __FUNCTION__);
3343}
3344
3345static const struct rpc_call_ops nfs4_lock_ops = {
3346 .rpc_call_prepare = nfs4_lock_prepare,
3347 .rpc_call_done = nfs4_lock_done,
3348 .rpc_release = nfs4_lock_release,
3349};
3350
3351static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3352{
3353 struct nfs4_lockdata *data;
3354 struct rpc_task *task;
3355 int ret;
3356
3357 dprintk("%s: begin!\n", __FUNCTION__);
3358 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3359 fl->fl_u.nfs4_fl.owner);
3360 if (data == NULL)
3361 return -ENOMEM;
3362 if (IS_SETLKW(cmd))
3363 data->arg.block = 1;
3364 if (reclaim != 0)
3365 data->arg.reclaim = 1;
3366 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3367 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003368 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003369 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003370 ret = nfs4_wait_for_completion_rpc_task(task);
3371 if (ret == 0) {
3372 ret = data->rpc_status;
3373 if (ret == -NFS4ERR_DENIED)
3374 ret = -EAGAIN;
3375 } else
3376 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003377 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003378 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
3382static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3383{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003384 struct nfs_server *server = NFS_SERVER(state->inode);
3385 struct nfs4_exception exception = { };
3386 int err;
3387
3388 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003389 /* Cache the lock if possible... */
3390 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3391 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003392 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3393 if (err != -NFS4ERR_DELAY)
3394 break;
3395 nfs4_handle_exception(server, err, &exception);
3396 } while (exception.retry);
3397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
3400static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3401{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003402 struct nfs_server *server = NFS_SERVER(state->inode);
3403 struct nfs4_exception exception = { };
3404 int err;
3405
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003406 err = nfs4_set_lock_state(state, request);
3407 if (err != 0)
3408 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003409 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003410 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3411 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003412 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3413 if (err != -NFS4ERR_DELAY)
3414 break;
3415 nfs4_handle_exception(server, err, &exception);
3416 } while (exception.retry);
3417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
3420static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3421{
David Howellsadfa6f92006-08-22 20:06:08 -04003422 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003423 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 int status;
3425
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003426 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003427 status = nfs4_set_lock_state(state, request);
3428 if (status != 0)
3429 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003430 request->fl_flags |= FL_ACCESS;
3431 status = do_vfs_lock(request->fl_file, request);
3432 if (status < 0)
3433 goto out;
3434 down_read(&clp->cl_sem);
3435 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3436 struct nfs_inode *nfsi = NFS_I(state->inode);
3437 /* Yes: cache locks! */
3438 down_read(&nfsi->rwsem);
3439 /* ...but avoid races with delegation recall... */
3440 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3441 request->fl_flags = fl_flags & ~FL_SLEEP;
3442 status = do_vfs_lock(request->fl_file, request);
3443 up_read(&nfsi->rwsem);
3444 goto out_unlock;
3445 }
3446 up_read(&nfsi->rwsem);
3447 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003448 status = _nfs4_do_setlk(state, cmd, request, 0);
3449 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003450 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003451 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003452 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003453 if (do_vfs_lock(request->fl_file, request) < 0)
3454 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003455out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003457out:
3458 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return status;
3460}
3461
3462static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3463{
3464 struct nfs4_exception exception = { };
3465 int err;
3466
3467 do {
3468 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3469 _nfs4_proc_setlk(state, cmd, request),
3470 &exception);
3471 } while (exception.retry);
3472 return err;
3473}
3474
3475static int
3476nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3477{
3478 struct nfs_open_context *ctx;
3479 struct nfs4_state *state;
3480 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3481 int status;
3482
3483 /* verify open state */
3484 ctx = (struct nfs_open_context *)filp->private_data;
3485 state = ctx->state;
3486
3487 if (request->fl_start < 0 || request->fl_end < 0)
3488 return -EINVAL;
3489
3490 if (IS_GETLK(cmd))
3491 return nfs4_proc_getlk(state, F_GETLK, request);
3492
3493 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3494 return -EINVAL;
3495
3496 if (request->fl_type == F_UNLCK)
3497 return nfs4_proc_unlck(state, cmd, request);
3498
3499 do {
3500 status = nfs4_proc_setlk(state, cmd, request);
3501 if ((status != -EAGAIN) || IS_SETLK(cmd))
3502 break;
3503 timeout = nfs4_set_lock_task_retry(timeout);
3504 status = -ERESTARTSYS;
3505 if (signalled())
3506 break;
3507 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return status;
3509}
3510
Trond Myklebust888e6942005-11-04 15:38:11 -05003511int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3512{
3513 struct nfs_server *server = NFS_SERVER(state->inode);
3514 struct nfs4_exception exception = { };
3515 int err;
3516
3517 err = nfs4_set_lock_state(state, fl);
3518 if (err != 0)
3519 goto out;
3520 do {
3521 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3522 if (err != -NFS4ERR_DELAY)
3523 break;
3524 err = nfs4_handle_exception(server, err, &exception);
3525 } while (exception.retry);
3526out:
3527 return err;
3528}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003529
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003530#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3531
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003532int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3533 size_t buflen, int flags)
3534{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003535 struct inode *inode = dentry->d_inode;
3536
3537 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3538 return -EOPNOTSUPP;
3539
3540 if (!S_ISREG(inode->i_mode) &&
3541 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3542 return -EPERM;
3543
3544 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003545}
3546
3547/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3548 * and that's what we'll do for e.g. user attributes that haven't been set.
3549 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3550 * attributes in kernel-managed attribute namespaces. */
3551ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3552 size_t buflen)
3553{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003554 struct inode *inode = dentry->d_inode;
3555
3556 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3557 return -EOPNOTSUPP;
3558
3559 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003560}
3561
3562ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3563{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003564 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003565
J. Bruce Fields096455a2006-03-20 23:23:42 -05003566 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3567 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003568 if (buf && buflen < len)
3569 return -ERANGE;
3570 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003571 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3572 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003573}
3574
Trond Myklebustc228fd32007-01-13 02:28:11 -05003575int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003576 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003577{
3578 struct nfs_server *server = NFS_SERVER(dir);
3579 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003580 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3581 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003582 };
3583 struct nfs4_fs_locations_arg args = {
3584 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003585 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003586 .page = page,
3587 .bitmask = bitmask,
3588 };
3589 struct rpc_message msg = {
3590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3591 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003592 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003593 };
3594 int status;
3595
3596 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003597 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003598 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003599 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003600 status = rpc_call_sync(server->client, &msg, 0);
3601 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3602 return status;
3603}
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3606 .recover_open = nfs4_open_reclaim,
3607 .recover_lock = nfs4_lock_reclaim,
3608};
3609
3610struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3611 .recover_open = nfs4_open_expired,
3612 .recover_lock = nfs4_lock_expired,
3613};
3614
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003615static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003616 .permission = nfs_permission,
3617 .getattr = nfs_getattr,
3618 .setattr = nfs_setattr,
3619 .getxattr = nfs4_getxattr,
3620 .setxattr = nfs4_setxattr,
3621 .listxattr = nfs4_listxattr,
3622};
3623
David Howells509de812006-08-22 20:06:11 -04003624const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 .version = 4, /* protocol version */
3626 .dentry_ops = &nfs4_dentry_operations,
3627 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003628 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 .getroot = nfs4_proc_get_root,
3630 .getattr = nfs4_proc_getattr,
3631 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003632 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 .lookup = nfs4_proc_lookup,
3634 .access = nfs4_proc_access,
3635 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 .create = nfs4_proc_create,
3637 .remove = nfs4_proc_remove,
3638 .unlink_setup = nfs4_proc_unlink_setup,
3639 .unlink_done = nfs4_proc_unlink_done,
3640 .rename = nfs4_proc_rename,
3641 .link = nfs4_proc_link,
3642 .symlink = nfs4_proc_symlink,
3643 .mkdir = nfs4_proc_mkdir,
3644 .rmdir = nfs4_proc_remove,
3645 .readdir = nfs4_proc_readdir,
3646 .mknod = nfs4_proc_mknod,
3647 .statfs = nfs4_proc_statfs,
3648 .fsinfo = nfs4_proc_fsinfo,
3649 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003650 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 .decode_dirent = nfs4_decode_dirent,
3652 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003653 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003655 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003657 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003658 .file_open = nfs_open,
3659 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003661 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662};
3663
3664/*
3665 * Local variables:
3666 * c-basic-offset: 8
3667 * End:
3668 */