blob: 10946415de773b7f31d9e582384121b7ddd012d6 [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_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
578 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
579}
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 Myklebust6f926b52005-10-18 14:20:18 -0700677 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100678 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
679}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700680
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100681static void nfs4_open_release(void *calldata)
682{
683 struct nfs4_opendata *data = calldata;
684 struct nfs4_state *state = NULL;
685
686 /* If this request hasn't been cancelled, do nothing */
687 if (data->cancelled == 0)
688 goto out_free;
689 /* In case of error, no cleanup! */
690 if (data->rpc_status != 0)
691 goto out_free;
692 /* In case we need an open_confirm, no cleanup! */
693 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
694 goto out_free;
695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
696 state = nfs4_opendata_to_nfs4_state(data);
697 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400698 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100699out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400700 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100701}
702
703static const struct rpc_call_ops nfs4_open_ops = {
704 .rpc_call_prepare = nfs4_open_prepare,
705 .rpc_call_done = nfs4_open_done,
706 .rpc_release = nfs4_open_release,
707};
708
709/*
710 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
711 */
712static int _nfs4_proc_open(struct nfs4_opendata *data)
713{
714 struct inode *dir = data->dir->d_inode;
715 struct nfs_server *server = NFS_SERVER(dir);
716 struct nfs_openargs *o_arg = &data->o_arg;
717 struct nfs_openres *o_res = &data->o_res;
718 struct rpc_task *task;
719 int status;
720
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400721 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500722 /*
723 * If rpc_run_task() ends up calling ->rpc_release(), we
724 * want to ensure that it takes the 'error' code path.
725 */
726 data->rpc_status = -ENOMEM;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400727 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100728 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500729 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100730 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100731 status = nfs4_wait_for_completion_rpc_task(task);
732 if (status != 0) {
733 data->cancelled = 1;
734 smp_wmb();
735 } else
736 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500737 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100739 return status;
740
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400741 if (o_arg->open_flags & O_CREAT) {
742 update_changeattr(dir, &o_res->cinfo);
743 nfs_post_op_update_inode(dir, o_res->dir_attr);
744 } else
745 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100747 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100749 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100751 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400753 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
758{
759 struct nfs_access_entry cache;
760 int mask = 0;
761 int status;
762
763 if (openflags & FMODE_READ)
764 mask |= MAY_READ;
765 if (openflags & FMODE_WRITE)
766 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400767 if (openflags & FMODE_EXEC)
768 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 status = nfs_access_get_cached(inode, cred, &cache);
770 if (status == 0)
771 goto out;
772
773 /* Be clever: ask server to check for all possible rights */
774 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
775 cache.cred = cred;
776 cache.jiffies = jiffies;
777 status = _nfs4_proc_access(inode, &cache);
778 if (status != 0)
779 return status;
780 nfs_access_add_cache(inode, &cache);
781out:
782 if ((cache.mask & mask) == mask)
783 return 0;
784 return -EACCES;
785}
786
Trond Myklebust10afec92007-05-14 17:16:04 -0400787static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100788{
David Howells7539bba2006-08-22 20:06:09 -0400789 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400790 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100791
Trond Myklebust6b309542006-09-14 14:03:14 -0400792 for (;;) {
793 ret = nfs4_wait_clnt_recover(server->client, clp);
794 if (ret != 0)
795 return ret;
796 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
797 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100798 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400799 }
800 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * OPEN_EXPIRED:
805 * reclaim state on the server after a network partition.
806 * Assumes caller holds the appropriate lock
807 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400808static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100813 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100814 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400817 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100818 if (ret < 0)
819 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
821 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400824 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100826 return -ENOMEM;
827 ret = nfs4_open_recover(opendata, state);
828 if (ret == -ESTALE) {
829 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400830 nfs4_drop_state_owner(state->owner);
831 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400833 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Trond Myklebust539cd032007-06-05 11:46:42 -0400837static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000838{
Trond Myklebust539cd032007-06-05 11:46:42 -0400839 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000840 struct nfs4_exception exception = { };
841 int err;
842
843 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400844 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000845 if (err == -NFS4ERR_DELAY)
846 nfs4_handle_exception(server, err, &exception);
847 } while (exception.retry);
848 return err;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100854 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Trond Myklebust864472e2006-01-03 09:55:15 +0100856 ctx = nfs4_state_find_open_context(state);
857 if (IS_ERR(ctx))
858 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400859 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100860 put_nfs_open_context(ctx);
861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100865 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
867static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
868{
869 struct nfs_delegation *delegation;
870 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400871 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct nfs_inode *nfsi = NFS_I(inode);
873 struct nfs4_state_owner *sp = NULL;
874 struct nfs4_state *state = NULL;
875 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
876 int err;
877
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100878 err = -ENOMEM;
879 if (!(sp = nfs4_get_state_owner(server, cred))) {
880 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
881 return err;
882 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100883 err = nfs4_recover_expired_lease(server);
884 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100885 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* Protect against reboot recovery - NOTE ORDER! */
887 down_read(&clp->cl_sem);
888 /* Protect against delegation recall */
889 down_read(&nfsi->rwsem);
890 delegation = NFS_I(inode)->delegation;
891 err = -ENOENT;
892 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
893 goto out_err;
894 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 state = nfs4_get_open_state(inode, sp);
896 if (state == NULL)
897 goto out_err;
898
899 err = -ENOENT;
900 if ((state->state & open_flags) == open_flags) {
901 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100902 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 spin_unlock(&inode->i_lock);
904 goto out_ok;
905 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100906 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 lock_kernel();
909 err = _nfs4_do_access(inode, cred, open_flags);
910 unlock_kernel();
911 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100912 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 set_bit(NFS_DELEGATED_STATE, &state->flags);
914 update_open_stateid(state, &delegation->stateid, open_flags);
915out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 nfs4_put_state_owner(sp);
917 up_read(&nfsi->rwsem);
918 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100920 return 0;
921out_put_open_state:
922 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 up_read(&nfsi->rwsem);
925 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700926 if (err != -EACCES)
927 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100928out_put_state_owner:
929 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return err;
931}
932
933static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
934{
935 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500936 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int err;
938
939 do {
940 err = _nfs4_open_delegated(inode, flags, cred, &res);
941 if (err == 0)
942 break;
943 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
944 err, &exception));
945 } while (exception.retry);
946 return res;
947}
948
949/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400950 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
951 * fields corresponding to attributes that were used to store the verifier.
952 * Make sure we clobber those fields in the later setattr call
953 */
954static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
955{
956 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
957 !(sattr->ia_valid & ATTR_ATIME_SET))
958 sattr->ia_valid |= ATTR_ATIME;
959
960 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
961 !(sattr->ia_valid & ATTR_MTIME_SET))
962 sattr->ia_valid |= ATTR_MTIME;
963}
964
965/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100966 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Trond Myklebustad389da2007-06-05 12:30:00 -0400968static 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 -0700969{
970 struct nfs4_state_owner *sp;
971 struct nfs4_state *state = NULL;
972 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -0400973 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 status = -ENOMEM;
979 if (!(sp = nfs4_get_state_owner(server, cred))) {
980 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
981 goto out_err;
982 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100983 status = nfs4_recover_expired_lease(server);
984 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100985 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100986 down_read(&clp->cl_sem);
987 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -0400988 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -0400990 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100992 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400994 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400996 if (opendata->o_arg.open_flags & O_EXCL)
997 nfs4_exclusive_attrset(opendata, sattr);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001000 state = nfs4_opendata_to_nfs4_state(opendata);
1001 if (state == NULL)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001002 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001003 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 nfs4_put_state_owner(sp);
1005 up_read(&clp->cl_sem);
1006 *res = state;
1007 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001008err_opendata_put:
1009 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001010err_release_rwsem:
1011 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012err_put_state_owner:
1013 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 *res = NULL;
1016 return status;
1017}
1018
1019
Trond Myklebustad389da2007-06-05 12:30:00 -04001020static 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 -07001021{
1022 struct nfs4_exception exception = { };
1023 struct nfs4_state *res;
1024 int status;
1025
1026 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001027 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (status == 0)
1029 break;
1030 /* NOTE: BAD_SEQID means the server and client disagree about the
1031 * book-keeping w.r.t. state-changing operations
1032 * (OPEN/CLOSE/LOCK/LOCKU...)
1033 * It is actually a sign of a bug on the client or on the server.
1034 *
1035 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001036 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * have unhashed the old state_owner for us, and that we can
1038 * therefore safely retry using a new one. We should still warn
1039 * the user though...
1040 */
1041 if (status == -NFS4ERR_BAD_SEQID) {
1042 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1043 exception.retry = 1;
1044 continue;
1045 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001046 /*
1047 * BAD_STATEID on OPEN means that the server cancelled our
1048 * state before it received the OPEN_CONFIRM.
1049 * Recover by retrying the request as per the discussion
1050 * on Page 181 of RFC3530.
1051 */
1052 if (status == -NFS4ERR_BAD_STATEID) {
1053 exception.retry = 1;
1054 continue;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1057 status, &exception));
1058 } while (exception.retry);
1059 return res;
1060}
1061
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001062static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1063 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001065 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001067 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 .iap = sattr,
1069 .server = server,
1070 .bitmask = server->attr_bitmask,
1071 };
1072 struct nfs_setattrres res = {
1073 .fattr = fattr,
1074 .server = server,
1075 };
1076 struct rpc_message msg = {
1077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1078 .rpc_argp = &arg,
1079 .rpc_resp = &res,
1080 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001081 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001082 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebust0e574af2005-10-27 22:12:38 -04001084 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001086 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1087 /* Use that stateid */
1088 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001090 nfs4_copy_stateid(&arg.stateid, state, current->files);
1091 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1093
Trond Myklebust65e43082005-08-16 11:49:44 -04001094 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001095 if (status == 0 && state != NULL)
1096 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001097 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001100static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1101 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001103 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct nfs4_exception exception = { };
1105 int err;
1106 do {
1107 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001108 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 &exception);
1110 } while (exception.retry);
1111 return err;
1112}
1113
1114struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001115 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct inode *inode;
1117 struct nfs4_state *state;
1118 struct nfs_closeargs arg;
1119 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001120 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001121 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122};
1123
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001124static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001125{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001126 struct nfs4_closedata *calldata = data;
1127 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001128
1129 nfs4_put_open_state(calldata->state);
1130 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001131 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001132 dput(calldata->path.dentry);
1133 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001134 kfree(calldata);
1135}
1136
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001137static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001139 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct nfs_server *server = NFS_SERVER(calldata->inode);
1142
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001143 if (RPC_ASSASSINATED(task))
1144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* hmm. we are done with the inode, and in the process of freeing
1146 * the state_owner. we keep this around to process errors
1147 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001148 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 switch (task->tk_status) {
1150 case 0:
1151 memcpy(&state->stateid, &calldata->res.stateid,
1152 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001153 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 case -NFS4ERR_STALE_STATEID:
1156 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 default:
1159 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1160 rpc_restart_call(task);
1161 return;
1162 }
1163 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001164 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001167static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001169 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001170 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct rpc_message msg = {
1172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1173 .rpc_argp = &calldata->arg,
1174 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001175 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001177 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001179 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001180 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001181 /* Recalculate the new open mode in case someone reopened the file
1182 * while we were waiting in line to be scheduled.
1183 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001184 spin_lock(&state->owner->so_lock);
1185 spin_lock(&calldata->inode->i_lock);
1186 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001187 if (state->n_rdwr == 0) {
1188 if (state->n_rdonly == 0)
1189 mode &= ~FMODE_READ;
1190 if (state->n_wronly == 0)
1191 mode &= ~FMODE_WRITE;
1192 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001193 nfs4_state_set_mode_locked(state, mode);
1194 spin_unlock(&calldata->inode->i_lock);
1195 spin_unlock(&state->owner->so_lock);
1196 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001197 /* Note: exit _without_ calling nfs4_close_done */
1198 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001199 return;
1200 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001201 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001202 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001204 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001205 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001206 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001209static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001210 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001211 .rpc_call_done = nfs4_close_done,
1212 .rpc_release = nfs4_free_closedata,
1213};
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/*
1216 * It is possible for data to be read/written from a mem-mapped file
1217 * after the sys_close call (which hits the vfs layer as a flush).
1218 * This means that we can't safely call nfsv4 close on a file until
1219 * the inode is cleared. This in turn means that we are not good
1220 * NFSv4 citizens - we do not indicate to the server to update the file's
1221 * share state even when we are done with one of the three share
1222 * stateid's in the inode.
1223 *
1224 * NOTE: Caller must be holding the sp->so_owner semaphore!
1225 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001226int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001228 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001230 struct nfs4_state_owner *sp = state->owner;
1231 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001232 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Trond Myklebust95121352005-10-18 14:20:12 -07001234 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001236 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001237 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001239 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001240 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001242 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001243 if (calldata->arg.seqid == NULL)
1244 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001245 calldata->arg.bitmask = server->attr_bitmask;
1246 calldata->res.fattr = &calldata->fattr;
1247 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001248 calldata->path.mnt = mntget(path->mnt);
1249 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001250
Trond Myklebustb39e6252007-06-11 23:05:07 -04001251 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1252 if (IS_ERR(task))
1253 return PTR_ERR(task);
1254 rpc_put_task(task);
1255 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001256out_free_calldata:
1257 kfree(calldata);
1258out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001259 nfs4_put_open_state(state);
1260 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001264static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001265{
1266 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001267 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001268
Trond Myklebust1b45c462007-07-03 13:04:56 -04001269 /* If the open_intent is for execute, we have an extra check to make */
1270 if (nd->intent.open.flags & FMODE_EXEC) {
1271 ret = _nfs4_do_access(state->inode,
1272 state->owner->so_cred,
1273 nd->intent.open.flags);
1274 if (ret < 0)
1275 goto out_close;
1276 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001277 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001278 if (!IS_ERR(filp)) {
1279 struct nfs_open_context *ctx;
1280 ctx = (struct nfs_open_context *)filp->private_data;
1281 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001282 return 0;
1283 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001284 ret = PTR_ERR(filp);
1285out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001286 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001287 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001288}
1289
1290struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1292{
Trond Myklebustad389da2007-06-05 12:30:00 -04001293 struct path path = {
1294 .mnt = nd->mnt,
1295 .dentry = dentry,
1296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct iattr attr;
1298 struct rpc_cred *cred;
1299 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001300 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (nd->flags & LOOKUP_CREATE) {
1303 attr.ia_mode = nd->intent.open.create_mode;
1304 attr.ia_valid = ATTR_MODE;
1305 if (!IS_POSIXACL(dir))
1306 attr.ia_mode &= ~current->fs->umask;
1307 } else {
1308 attr.ia_valid = 0;
1309 BUG_ON(nd->intent.open.flags & O_CREAT);
1310 }
1311
David Howells1f163412006-08-22 20:06:11 -04001312 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001314 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001315 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001317 if (IS_ERR(state)) {
1318 if (PTR_ERR(state) == -ENOENT)
1319 d_add(dentry, NULL);
1320 return (struct dentry *)state;
1321 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001322 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001323 if (res != NULL)
1324 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001325 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001326 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001330nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Trond Myklebustad389da2007-06-05 12:30:00 -04001332 struct path path = {
1333 .mnt = nd->mnt,
1334 .dentry = dentry,
1335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct rpc_cred *cred;
1337 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
David Howells1f163412006-08-22 20:06:11 -04001339 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (IS_ERR(cred))
1341 return PTR_ERR(cred);
1342 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1343 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001344 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001346 if (IS_ERR(state)) {
1347 switch (PTR_ERR(state)) {
1348 case -EPERM:
1349 case -EACCES:
1350 case -EDQUOT:
1351 case -ENOSPC:
1352 case -EROFS:
1353 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1354 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001355 default:
1356 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001357 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001358 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001360 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 1;
1362 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001363 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001364out_drop:
1365 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367}
1368
1369
1370static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1371{
1372 struct nfs4_server_caps_res res = {};
1373 struct rpc_message msg = {
1374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1375 .rpc_argp = fhandle,
1376 .rpc_resp = &res,
1377 };
1378 int status;
1379
1380 status = rpc_call_sync(server->client, &msg, 0);
1381 if (status == 0) {
1382 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1383 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1384 server->caps |= NFS_CAP_ACLS;
1385 if (res.has_links != 0)
1386 server->caps |= NFS_CAP_HARDLINKS;
1387 if (res.has_symlinks != 0)
1388 server->caps |= NFS_CAP_SYMLINKS;
1389 server->acl_bitmask = res.acl_bitmask;
1390 }
1391 return status;
1392}
1393
Trond Myklebust55a97592006-06-09 09:34:19 -04001394int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct nfs4_exception exception = { };
1397 int err;
1398 do {
1399 err = nfs4_handle_exception(server,
1400 _nfs4_server_capabilities(server, fhandle),
1401 &exception);
1402 } while (exception.retry);
1403 return err;
1404}
1405
1406static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1407 struct nfs_fsinfo *info)
1408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct nfs4_lookup_root_arg args = {
1410 .bitmask = nfs4_fattr_bitmap,
1411 };
1412 struct nfs4_lookup_res res = {
1413 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001414 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 .fh = fhandle,
1416 };
1417 struct rpc_message msg = {
1418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1419 .rpc_argp = &args,
1420 .rpc_resp = &res,
1421 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001422 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return rpc_call_sync(server->client, &msg, 0);
1424}
1425
1426static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1427 struct nfs_fsinfo *info)
1428{
1429 struct nfs4_exception exception = { };
1430 int err;
1431 do {
1432 err = nfs4_handle_exception(server,
1433 _nfs4_lookup_root(server, fhandle, info),
1434 &exception);
1435 } while (exception.retry);
1436 return err;
1437}
1438
David Howells54ceac42006-08-22 20:06:13 -04001439/*
1440 * get the file handle for the "/" directory on the server
1441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001443 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int status;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (status == 0)
1449 status = nfs4_server_capabilities(server, fhandle);
1450 if (status == 0)
1451 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001452 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Manoj Naik6b97fd32006-06-09 09:34:29 -04001455/*
1456 * Get locations and (maybe) other attributes of a referral.
1457 * Note that we'll actually follow the referral later when
1458 * we detect fsid mismatch in inode revalidation
1459 */
1460static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1461{
1462 int status = -ENOMEM;
1463 struct page *page = NULL;
1464 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001465
1466 page = alloc_page(GFP_KERNEL);
1467 if (page == NULL)
1468 goto out;
1469 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1470 if (locations == NULL)
1471 goto out;
1472
Trond Myklebustc228fd32007-01-13 02:28:11 -05001473 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001474 if (status != 0)
1475 goto out;
1476 /* Make sure server returned a different fsid for the referral */
1477 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1478 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1479 status = -EIO;
1480 goto out;
1481 }
1482
1483 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1484 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1485 if (!fattr->mode)
1486 fattr->mode = S_IFDIR;
1487 memset(fhandle, 0, sizeof(struct nfs_fh));
1488out:
1489 if (page)
1490 __free_page(page);
1491 if (locations)
1492 kfree(locations);
1493 return status;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1497{
1498 struct nfs4_getattr_arg args = {
1499 .fh = fhandle,
1500 .bitmask = server->attr_bitmask,
1501 };
1502 struct nfs4_getattr_res res = {
1503 .fattr = fattr,
1504 .server = server,
1505 };
1506 struct rpc_message msg = {
1507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1508 .rpc_argp = &args,
1509 .rpc_resp = &res,
1510 };
1511
Trond Myklebust0e574af2005-10-27 22:12:38 -04001512 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return rpc_call_sync(server->client, &msg, 0);
1514}
1515
1516static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1517{
1518 struct nfs4_exception exception = { };
1519 int err;
1520 do {
1521 err = nfs4_handle_exception(server,
1522 _nfs4_proc_getattr(server, fhandle, fattr),
1523 &exception);
1524 } while (exception.retry);
1525 return err;
1526}
1527
1528/*
1529 * The file is not closed if it is opened due to the a request to change
1530 * the size of the file. The open call will not be needed once the
1531 * VFS layer lookup-intents are implemented.
1532 *
1533 * Close is called when the inode is destroyed.
1534 * If we haven't opened the file for O_WRONLY, we
1535 * need to in the size_change case to obtain a stateid.
1536 *
1537 * Got race?
1538 * Because OPEN is always done by name in nfsv4, it is
1539 * possible that we opened a different file by the same
1540 * name. We can recognize this race condition, but we
1541 * can't do anything about it besides returning an error.
1542 *
1543 * This will be fixed with VFS changes (lookup-intent).
1544 */
1545static int
1546nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1547 struct iattr *sattr)
1548{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001549 struct rpc_cred *cred;
1550 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001551 struct nfs_open_context *ctx;
1552 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int status;
1554
Trond Myklebust0e574af2005-10-27 22:12:38 -04001555 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
David Howells1f163412006-08-22 20:06:11 -04001557 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001558 if (IS_ERR(cred))
1559 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001560
1561 /* Search for an existing open(O_WRITE) file */
1562 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1563 if (ctx != NULL)
1564 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001565
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001566 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001567 if (status == 0)
1568 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001569 if (ctx != NULL)
1570 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001571 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return status;
1573}
1574
David Howells2b3de442006-08-22 20:06:09 -04001575static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1576 struct qstr *name, struct nfs_fh *fhandle,
1577 struct nfs_fattr *fattr)
1578{
1579 int status;
1580 struct nfs4_lookup_arg args = {
1581 .bitmask = server->attr_bitmask,
1582 .dir_fh = dirfh,
1583 .name = name,
1584 };
1585 struct nfs4_lookup_res res = {
1586 .server = server,
1587 .fattr = fattr,
1588 .fh = fhandle,
1589 };
1590 struct rpc_message msg = {
1591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1592 .rpc_argp = &args,
1593 .rpc_resp = &res,
1594 };
1595
1596 nfs_fattr_init(fattr);
1597
1598 dprintk("NFS call lookupfh %s\n", name->name);
1599 status = rpc_call_sync(server->client, &msg, 0);
1600 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001601 return status;
1602}
1603
1604static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1605 struct qstr *name, struct nfs_fh *fhandle,
1606 struct nfs_fattr *fattr)
1607{
1608 struct nfs4_exception exception = { };
1609 int err;
1610 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001611 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1612 /* FIXME: !!!! */
1613 if (err == -NFS4ERR_MOVED) {
1614 err = -EREMOTE;
1615 break;
1616 }
1617 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001618 } while (exception.retry);
1619 return err;
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1623 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1624{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001625 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001628 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001629 if (status == -NFS4ERR_MOVED)
1630 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 dprintk("NFS reply lookup: %d\n", status);
1632 return status;
1633}
1634
1635static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1636{
1637 struct nfs4_exception exception = { };
1638 int err;
1639 do {
1640 err = nfs4_handle_exception(NFS_SERVER(dir),
1641 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1642 &exception);
1643 } while (exception.retry);
1644 return err;
1645}
1646
1647static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1648{
1649 struct nfs4_accessargs args = {
1650 .fh = NFS_FH(inode),
1651 };
1652 struct nfs4_accessres res = { 0 };
1653 struct rpc_message msg = {
1654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1655 .rpc_argp = &args,
1656 .rpc_resp = &res,
1657 .rpc_cred = entry->cred,
1658 };
1659 int mode = entry->mask;
1660 int status;
1661
1662 /*
1663 * Determine which access bits we want to ask for...
1664 */
1665 if (mode & MAY_READ)
1666 args.access |= NFS4_ACCESS_READ;
1667 if (S_ISDIR(inode->i_mode)) {
1668 if (mode & MAY_WRITE)
1669 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1670 if (mode & MAY_EXEC)
1671 args.access |= NFS4_ACCESS_LOOKUP;
1672 } else {
1673 if (mode & MAY_WRITE)
1674 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1675 if (mode & MAY_EXEC)
1676 args.access |= NFS4_ACCESS_EXECUTE;
1677 }
1678 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1679 if (!status) {
1680 entry->mask = 0;
1681 if (res.access & NFS4_ACCESS_READ)
1682 entry->mask |= MAY_READ;
1683 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1684 entry->mask |= MAY_WRITE;
1685 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1686 entry->mask |= MAY_EXEC;
1687 }
1688 return status;
1689}
1690
1691static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1692{
1693 struct nfs4_exception exception = { };
1694 int err;
1695 do {
1696 err = nfs4_handle_exception(NFS_SERVER(inode),
1697 _nfs4_proc_access(inode, entry),
1698 &exception);
1699 } while (exception.retry);
1700 return err;
1701}
1702
1703/*
1704 * TODO: For the time being, we don't try to get any attributes
1705 * along with any of the zero-copy operations READ, READDIR,
1706 * READLINK, WRITE.
1707 *
1708 * In the case of the first three, we want to put the GETATTR
1709 * after the read-type operation -- this is because it is hard
1710 * to predict the length of a GETATTR response in v4, and thus
1711 * align the READ data correctly. This means that the GETATTR
1712 * may end up partially falling into the page cache, and we should
1713 * shift it into the 'tail' of the xdr_buf before processing.
1714 * To do this efficiently, we need to know the total length
1715 * of data received, which doesn't seem to be available outside
1716 * of the RPC layer.
1717 *
1718 * In the case of WRITE, we also want to put the GETATTR after
1719 * the operation -- in this case because we want to make sure
1720 * we get the post-operation mtime and size. This means that
1721 * we can't use xdr_encode_pages() as written: we need a variant
1722 * of it which would leave room in the 'tail' iovec.
1723 *
1724 * Both of these changes to the XDR layer would in fact be quite
1725 * minor, but I decided to leave them for a subsequent patch.
1726 */
1727static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1728 unsigned int pgbase, unsigned int pglen)
1729{
1730 struct nfs4_readlink args = {
1731 .fh = NFS_FH(inode),
1732 .pgbase = pgbase,
1733 .pglen = pglen,
1734 .pages = &page,
1735 };
1736 struct rpc_message msg = {
1737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1738 .rpc_argp = &args,
1739 .rpc_resp = NULL,
1740 };
1741
1742 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1743}
1744
1745static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1746 unsigned int pgbase, unsigned int pglen)
1747{
1748 struct nfs4_exception exception = { };
1749 int err;
1750 do {
1751 err = nfs4_handle_exception(NFS_SERVER(inode),
1752 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1753 &exception);
1754 } while (exception.retry);
1755 return err;
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*
1759 * Got race?
1760 * We will need to arrange for the VFS layer to provide an atomic open.
1761 * Until then, this create/open method is prone to inefficiency and race
1762 * conditions due to the lookup, create, and open VFS calls from sys_open()
1763 * placed on the wire.
1764 *
1765 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1766 * The file will be opened again in the subsequent VFS open call
1767 * (nfs4_proc_file_open).
1768 *
1769 * The open for read will just hang around to be used by any process that
1770 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1771 */
1772
1773static int
1774nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001775 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Trond Myklebustad389da2007-06-05 12:30:00 -04001777 struct path path = {
1778 .mnt = nd->mnt,
1779 .dentry = dentry,
1780 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct nfs4_state *state;
1782 struct rpc_cred *cred;
1783 int status = 0;
1784
David Howells1f163412006-08-22 20:06:11 -04001785 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (IS_ERR(cred)) {
1787 status = PTR_ERR(cred);
1788 goto out;
1789 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001790 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 put_rpccred(cred);
1792 if (IS_ERR(state)) {
1793 status = PTR_ERR(state);
1794 goto out;
1795 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001796 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (flags & O_EXCL) {
1798 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001799 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001800 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001801 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001802 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001803 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001804 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001805 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001806 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001807 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808out:
1809 return status;
1810}
1811
1812static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1813{
Trond Myklebust16e42952005-10-27 22:12:44 -04001814 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct nfs4_remove_arg args = {
1816 .fh = NFS_FH(dir),
1817 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001818 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001820 struct nfs_fattr dir_attr;
1821 struct nfs4_remove_res res = {
1822 .server = server,
1823 .dir_attr = &dir_attr,
1824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct rpc_message msg = {
1826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1827 .rpc_argp = &args,
1828 .rpc_resp = &res,
1829 };
1830 int status;
1831
Trond Myklebust16e42952005-10-27 22:12:44 -04001832 nfs_fattr_init(res.dir_attr);
1833 status = rpc_call_sync(server->client, &msg, 0);
1834 if (status == 0) {
1835 update_changeattr(dir, &res.cinfo);
1836 nfs_post_op_update_inode(dir, res.dir_attr);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return status;
1839}
1840
1841static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1842{
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
1846 err = nfs4_handle_exception(NFS_SERVER(dir),
1847 _nfs4_proc_remove(dir, name),
1848 &exception);
1849 } while (exception.retry);
1850 return err;
1851}
1852
1853struct unlink_desc {
1854 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001855 struct nfs4_remove_res res;
1856 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857};
1858
1859static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1860 struct qstr *name)
1861{
Trond Myklebust16e42952005-10-27 22:12:44 -04001862 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct unlink_desc *up;
1864
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001865 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (!up)
1867 return -ENOMEM;
1868
1869 up->args.fh = NFS_FH(dir->d_inode);
1870 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001871 up->args.bitmask = server->attr_bitmask;
1872 up->res.server = server;
1873 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1876 msg->rpc_argp = &up->args;
1877 msg->rpc_resp = &up->res;
1878 return 0;
1879}
1880
1881static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1882{
1883 struct rpc_message *msg = &task->tk_msg;
1884 struct unlink_desc *up;
1885
1886 if (msg->rpc_resp != NULL) {
1887 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001888 update_changeattr(dir->d_inode, &up->res.cinfo);
1889 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 kfree(up);
1891 msg->rpc_resp = NULL;
1892 msg->rpc_argp = NULL;
1893 }
1894 return 0;
1895}
1896
1897static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1898 struct inode *new_dir, struct qstr *new_name)
1899{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001900 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct nfs4_rename_arg arg = {
1902 .old_dir = NFS_FH(old_dir),
1903 .new_dir = NFS_FH(new_dir),
1904 .old_name = old_name,
1905 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001906 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001908 struct nfs_fattr old_fattr, new_fattr;
1909 struct nfs4_rename_res res = {
1910 .server = server,
1911 .old_fattr = &old_fattr,
1912 .new_fattr = &new_fattr,
1913 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct rpc_message msg = {
1915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1916 .rpc_argp = &arg,
1917 .rpc_resp = &res,
1918 };
1919 int status;
1920
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001921 nfs_fattr_init(res.old_fattr);
1922 nfs_fattr_init(res.new_fattr);
1923 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (!status) {
1926 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001927 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001929 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931 return status;
1932}
1933
1934static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1935 struct inode *new_dir, struct qstr *new_name)
1936{
1937 struct nfs4_exception exception = { };
1938 int err;
1939 do {
1940 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1941 _nfs4_proc_rename(old_dir, old_name,
1942 new_dir, new_name),
1943 &exception);
1944 } while (exception.retry);
1945 return err;
1946}
1947
1948static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1949{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001950 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct nfs4_link_arg arg = {
1952 .fh = NFS_FH(inode),
1953 .dir_fh = NFS_FH(dir),
1954 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001955 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001957 struct nfs_fattr fattr, dir_attr;
1958 struct nfs4_link_res res = {
1959 .server = server,
1960 .fattr = &fattr,
1961 .dir_attr = &dir_attr,
1962 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct rpc_message msg = {
1964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1965 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001966 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 };
1968 int status;
1969
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001970 nfs_fattr_init(res.fattr);
1971 nfs_fattr_init(res.dir_attr);
1972 status = rpc_call_sync(server->client, &msg, 0);
1973 if (!status) {
1974 update_changeattr(dir, &res.cinfo);
1975 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04001976 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 return status;
1980}
1981
1982static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1983{
1984 struct nfs4_exception exception = { };
1985 int err;
1986 do {
1987 err = nfs4_handle_exception(NFS_SERVER(inode),
1988 _nfs4_proc_link(inode, dir, name),
1989 &exception);
1990 } while (exception.retry);
1991 return err;
1992}
1993
Chuck Lever4f390c12006-08-22 20:06:22 -04001994static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04001995 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04001998 struct nfs_fh fhandle;
1999 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct nfs4_create_arg arg = {
2001 .dir_fh = NFS_FH(dir),
2002 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002003 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 .attrs = sattr,
2005 .ftype = NF4LNK,
2006 .bitmask = server->attr_bitmask,
2007 };
2008 struct nfs4_create_res res = {
2009 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002010 .fh = &fhandle,
2011 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002012 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 };
2014 struct rpc_message msg = {
2015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2016 .rpc_argp = &arg,
2017 .rpc_resp = &res,
2018 };
2019 int status;
2020
Chuck Lever94a6d752006-08-22 20:06:23 -04002021 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002023
Chuck Lever94a6d752006-08-22 20:06:23 -04002024 arg.u.symlink.pages = &page;
2025 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002026 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002027 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002030 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002032 nfs_post_op_update_inode(dir, res.dir_fattr);
2033 status = nfs_instantiate(dentry, &fhandle, &fattr);
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return status;
2036}
2037
Chuck Lever4f390c12006-08-22 20:06:22 -04002038static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002039 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct nfs4_exception exception = { };
2042 int err;
2043 do {
2044 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002045 _nfs4_proc_symlink(dir, dentry, page,
2046 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 &exception);
2048 } while (exception.retry);
2049 return err;
2050}
2051
2052static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2053 struct iattr *sattr)
2054{
2055 struct nfs_server *server = NFS_SERVER(dir);
2056 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002057 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct nfs4_create_arg arg = {
2059 .dir_fh = NFS_FH(dir),
2060 .server = server,
2061 .name = &dentry->d_name,
2062 .attrs = sattr,
2063 .ftype = NF4DIR,
2064 .bitmask = server->attr_bitmask,
2065 };
2066 struct nfs4_create_res res = {
2067 .server = server,
2068 .fh = &fhandle,
2069 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002070 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 };
2072 struct rpc_message msg = {
2073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2074 .rpc_argp = &arg,
2075 .rpc_resp = &res,
2076 };
2077 int status;
2078
Trond Myklebust0e574af2005-10-27 22:12:38 -04002079 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002080 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2083 if (!status) {
2084 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002085 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 status = nfs_instantiate(dentry, &fhandle, &fattr);
2087 }
2088 return status;
2089}
2090
2091static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2092 struct iattr *sattr)
2093{
2094 struct nfs4_exception exception = { };
2095 int err;
2096 do {
2097 err = nfs4_handle_exception(NFS_SERVER(dir),
2098 _nfs4_proc_mkdir(dir, dentry, sattr),
2099 &exception);
2100 } while (exception.retry);
2101 return err;
2102}
2103
2104static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2105 u64 cookie, struct page *page, unsigned int count, int plus)
2106{
2107 struct inode *dir = dentry->d_inode;
2108 struct nfs4_readdir_arg args = {
2109 .fh = NFS_FH(dir),
2110 .pages = &page,
2111 .pgbase = 0,
2112 .count = count,
2113 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2114 };
2115 struct nfs4_readdir_res res;
2116 struct rpc_message msg = {
2117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2118 .rpc_argp = &args,
2119 .rpc_resp = &res,
2120 .rpc_cred = cred,
2121 };
2122 int status;
2123
Trond Myklebusteadf4592005-06-22 17:16:39 +00002124 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2125 dentry->d_parent->d_name.name,
2126 dentry->d_name.name,
2127 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2129 res.pgbase = args.pgbase;
2130 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2131 if (status == 0)
2132 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002133 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return status;
2135}
2136
2137static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2138 u64 cookie, struct page *page, unsigned int count, int plus)
2139{
2140 struct nfs4_exception exception = { };
2141 int err;
2142 do {
2143 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2144 _nfs4_proc_readdir(dentry, cred, cookie,
2145 page, count, plus),
2146 &exception);
2147 } while (exception.retry);
2148 return err;
2149}
2150
2151static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2152 struct iattr *sattr, dev_t rdev)
2153{
2154 struct nfs_server *server = NFS_SERVER(dir);
2155 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002156 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct nfs4_create_arg arg = {
2158 .dir_fh = NFS_FH(dir),
2159 .server = server,
2160 .name = &dentry->d_name,
2161 .attrs = sattr,
2162 .bitmask = server->attr_bitmask,
2163 };
2164 struct nfs4_create_res res = {
2165 .server = server,
2166 .fh = &fh,
2167 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002168 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 };
2170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2172 .rpc_argp = &arg,
2173 .rpc_resp = &res,
2174 };
2175 int status;
2176 int mode = sattr->ia_mode;
2177
Trond Myklebust0e574af2005-10-27 22:12:38 -04002178 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002179 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2182 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2183 if (S_ISFIFO(mode))
2184 arg.ftype = NF4FIFO;
2185 else if (S_ISBLK(mode)) {
2186 arg.ftype = NF4BLK;
2187 arg.u.device.specdata1 = MAJOR(rdev);
2188 arg.u.device.specdata2 = MINOR(rdev);
2189 }
2190 else if (S_ISCHR(mode)) {
2191 arg.ftype = NF4CHR;
2192 arg.u.device.specdata1 = MAJOR(rdev);
2193 arg.u.device.specdata2 = MINOR(rdev);
2194 }
2195 else
2196 arg.ftype = NF4SOCK;
2197
2198 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2199 if (status == 0) {
2200 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002201 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 status = nfs_instantiate(dentry, &fh, &fattr);
2203 }
2204 return status;
2205}
2206
2207static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2208 struct iattr *sattr, dev_t rdev)
2209{
2210 struct nfs4_exception exception = { };
2211 int err;
2212 do {
2213 err = nfs4_handle_exception(NFS_SERVER(dir),
2214 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2215 &exception);
2216 } while (exception.retry);
2217 return err;
2218}
2219
2220static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsstat *fsstat)
2222{
2223 struct nfs4_statfs_arg args = {
2224 .fh = fhandle,
2225 .bitmask = server->attr_bitmask,
2226 };
2227 struct rpc_message msg = {
2228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2229 .rpc_argp = &args,
2230 .rpc_resp = fsstat,
2231 };
2232
Trond Myklebust0e574af2005-10-27 22:12:38 -04002233 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return rpc_call_sync(server->client, &msg, 0);
2235}
2236
2237static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2238{
2239 struct nfs4_exception exception = { };
2240 int err;
2241 do {
2242 err = nfs4_handle_exception(server,
2243 _nfs4_proc_statfs(server, fhandle, fsstat),
2244 &exception);
2245 } while (exception.retry);
2246 return err;
2247}
2248
2249static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2250 struct nfs_fsinfo *fsinfo)
2251{
2252 struct nfs4_fsinfo_arg args = {
2253 .fh = fhandle,
2254 .bitmask = server->attr_bitmask,
2255 };
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2258 .rpc_argp = &args,
2259 .rpc_resp = fsinfo,
2260 };
2261
2262 return rpc_call_sync(server->client, &msg, 0);
2263}
2264
2265static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2266{
2267 struct nfs4_exception exception = { };
2268 int err;
2269
2270 do {
2271 err = nfs4_handle_exception(server,
2272 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2273 &exception);
2274 } while (exception.retry);
2275 return err;
2276}
2277
2278static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2279{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2282}
2283
2284static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2285 struct nfs_pathconf *pathconf)
2286{
2287 struct nfs4_pathconf_arg args = {
2288 .fh = fhandle,
2289 .bitmask = server->attr_bitmask,
2290 };
2291 struct rpc_message msg = {
2292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2293 .rpc_argp = &args,
2294 .rpc_resp = pathconf,
2295 };
2296
2297 /* None of the pathconf attributes are mandatory to implement */
2298 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2299 memset(pathconf, 0, sizeof(*pathconf));
2300 return 0;
2301 }
2302
Trond Myklebust0e574af2005-10-27 22:12:38 -04002303 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return rpc_call_sync(server->client, &msg, 0);
2305}
2306
2307static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2308 struct nfs_pathconf *pathconf)
2309{
2310 struct nfs4_exception exception = { };
2311 int err;
2312
2313 do {
2314 err = nfs4_handle_exception(server,
2315 _nfs4_proc_pathconf(server, fhandle, pathconf),
2316 &exception);
2317 } while (exception.retry);
2318 return err;
2319}
2320
Trond Myklebustec06c092006-03-20 13:44:27 -05002321static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Trond Myklebustec06c092006-03-20 13:44:27 -05002323 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Trond Myklebustec06c092006-03-20 13:44:27 -05002325 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002327 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002330 renew_lease(server, data->timestamp);
2331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Trond Myklebustec06c092006-03-20 13:44:27 -05002334static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct rpc_message msg = {
2337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2338 .rpc_argp = &data->args,
2339 .rpc_resp = &data->res,
2340 .rpc_cred = data->cred,
2341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 data->timestamp = jiffies;
2344
Trond Myklebustec06c092006-03-20 13:44:27 -05002345 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Trond Myklebust788e7a82006-03-20 13:44:27 -05002348static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct inode *inode = data->inode;
2351
2352 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2353 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002354 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002356 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002358 nfs_post_op_update_inode(inode, data->res.fattr);
2359 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Trond Myklebust788e7a82006-03-20 13:44:27 -05002363static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct rpc_message msg = {
2366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2367 .rpc_argp = &data->args,
2368 .rpc_resp = &data->res,
2369 .rpc_cred = data->cred,
2370 };
2371 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002372 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (how & FLUSH_STABLE) {
2376 if (!NFS_I(inode)->ncommit)
2377 stable = NFS_FILE_SYNC;
2378 else
2379 stable = NFS_DATA_SYNC;
2380 } else
2381 stable = NFS_UNSTABLE;
2382 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002383 data->args.bitmask = server->attr_bitmask;
2384 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 data->timestamp = jiffies;
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002389 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Trond Myklebust788e7a82006-03-20 13:44:27 -05002392static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct inode *inode = data->inode;
2395
2396 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2397 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002398 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002400 if (task->tk_status >= 0)
2401 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Trond Myklebust788e7a82006-03-20 13:44:27 -05002405static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2409 .rpc_argp = &data->args,
2410 .rpc_resp = &data->res,
2411 .rpc_cred = data->cred,
2412 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002413 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002415 data->args.bitmask = server->attr_bitmask;
2416 data->res.server = server;
2417
Trond Myklebust788e7a82006-03-20 13:44:27 -05002418 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
2421/*
2422 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2423 * standalone procedure for queueing an asynchronous RENEW.
2424 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002425static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
David Howellsadfa6f92006-08-22 20:06:08 -04002427 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002428 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 if (task->tk_status < 0) {
2431 switch (task->tk_status) {
2432 case -NFS4ERR_STALE_CLIENTID:
2433 case -NFS4ERR_EXPIRED:
2434 case -NFS4ERR_CB_PATH_DOWN:
2435 nfs4_schedule_state_recovery(clp);
2436 }
2437 return;
2438 }
2439 spin_lock(&clp->cl_lock);
2440 if (time_before(clp->cl_last_renewal,timestamp))
2441 clp->cl_last_renewal = timestamp;
2442 spin_unlock(&clp->cl_lock);
2443}
2444
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002445static const struct rpc_call_ops nfs4_renew_ops = {
2446 .rpc_call_done = nfs4_renew_done,
2447};
2448
David Howellsadfa6f92006-08-22 20:06:08 -04002449int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct rpc_message msg = {
2452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2453 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002454 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 };
2456
2457 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002458 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
David Howellsadfa6f92006-08-22 20:06:08 -04002461int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct rpc_message msg = {
2464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2465 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002466 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 };
2468 unsigned long now = jiffies;
2469 int status;
2470
2471 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2472 if (status < 0)
2473 return status;
2474 spin_lock(&clp->cl_lock);
2475 if (time_before(clp->cl_last_renewal,now))
2476 clp->cl_last_renewal = now;
2477 spin_unlock(&clp->cl_lock);
2478 return 0;
2479}
2480
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002481static inline int nfs4_server_supports_acls(struct nfs_server *server)
2482{
2483 return (server->caps & NFS_CAP_ACLS)
2484 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2485 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2486}
2487
2488/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2489 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2490 * the stack.
2491 */
2492#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2493
2494static void buf_to_pages(const void *buf, size_t buflen,
2495 struct page **pages, unsigned int *pgbase)
2496{
2497 const void *p = buf;
2498
2499 *pgbase = offset_in_page(buf);
2500 p -= *pgbase;
2501 while (p < buf + buflen) {
2502 *(pages++) = virt_to_page(p);
2503 p += PAGE_CACHE_SIZE;
2504 }
2505}
2506
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002507struct nfs4_cached_acl {
2508 int cached;
2509 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002510 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002511};
2512
2513static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002514{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002515 struct nfs_inode *nfsi = NFS_I(inode);
2516
2517 spin_lock(&inode->i_lock);
2518 kfree(nfsi->nfs4_acl);
2519 nfsi->nfs4_acl = acl;
2520 spin_unlock(&inode->i_lock);
2521}
2522
2523static void nfs4_zap_acl_attr(struct inode *inode)
2524{
2525 nfs4_set_cached_acl(inode, NULL);
2526}
2527
2528static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2529{
2530 struct nfs_inode *nfsi = NFS_I(inode);
2531 struct nfs4_cached_acl *acl;
2532 int ret = -ENOENT;
2533
2534 spin_lock(&inode->i_lock);
2535 acl = nfsi->nfs4_acl;
2536 if (acl == NULL)
2537 goto out;
2538 if (buf == NULL) /* user is just asking for length */
2539 goto out_len;
2540 if (acl->cached == 0)
2541 goto out;
2542 ret = -ERANGE; /* see getxattr(2) man page */
2543 if (acl->len > buflen)
2544 goto out;
2545 memcpy(buf, acl->data, acl->len);
2546out_len:
2547 ret = acl->len;
2548out:
2549 spin_unlock(&inode->i_lock);
2550 return ret;
2551}
2552
2553static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2554{
2555 struct nfs4_cached_acl *acl;
2556
2557 if (buf && acl_len <= PAGE_SIZE) {
2558 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2559 if (acl == NULL)
2560 goto out;
2561 acl->cached = 1;
2562 memcpy(acl->data, buf, acl_len);
2563 } else {
2564 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2565 if (acl == NULL)
2566 goto out;
2567 acl->cached = 0;
2568 }
2569 acl->len = acl_len;
2570out:
2571 nfs4_set_cached_acl(inode, acl);
2572}
2573
Trond Myklebust16b42892006-08-24 12:27:15 -04002574static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002575{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002576 struct page *pages[NFS4ACL_MAXPAGES];
2577 struct nfs_getaclargs args = {
2578 .fh = NFS_FH(inode),
2579 .acl_pages = pages,
2580 .acl_len = buflen,
2581 };
2582 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002583 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002584 struct rpc_message msg = {
2585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2586 .rpc_argp = &args,
2587 .rpc_resp = &resp_len,
2588 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002589 struct page *localpage = NULL;
2590 int ret;
2591
2592 if (buflen < PAGE_SIZE) {
2593 /* As long as we're doing a round trip to the server anyway,
2594 * let's be prepared for a page of acl data. */
2595 localpage = alloc_page(GFP_KERNEL);
2596 resp_buf = page_address(localpage);
2597 if (localpage == NULL)
2598 return -ENOMEM;
2599 args.acl_pages[0] = localpage;
2600 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002601 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002602 } else {
2603 resp_buf = buf;
2604 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2605 }
2606 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2607 if (ret)
2608 goto out_free;
2609 if (resp_len > args.acl_len)
2610 nfs4_write_cached_acl(inode, NULL, resp_len);
2611 else
2612 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2613 if (buf) {
2614 ret = -ERANGE;
2615 if (resp_len > buflen)
2616 goto out_free;
2617 if (localpage)
2618 memcpy(buf, resp_buf, resp_len);
2619 }
2620 ret = resp_len;
2621out_free:
2622 if (localpage)
2623 __free_page(localpage);
2624 return ret;
2625}
2626
Trond Myklebust16b42892006-08-24 12:27:15 -04002627static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2628{
2629 struct nfs4_exception exception = { };
2630 ssize_t ret;
2631 do {
2632 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2633 if (ret >= 0)
2634 break;
2635 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2636 } while (exception.retry);
2637 return ret;
2638}
2639
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002640static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2641{
2642 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002643 int ret;
2644
2645 if (!nfs4_server_supports_acls(server))
2646 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002647 ret = nfs_revalidate_inode(server, inode);
2648 if (ret < 0)
2649 return ret;
2650 ret = nfs4_read_cached_acl(inode, buf, buflen);
2651 if (ret != -ENOENT)
2652 return ret;
2653 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002654}
2655
Trond Myklebust16b42892006-08-24 12:27:15 -04002656static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002657{
2658 struct nfs_server *server = NFS_SERVER(inode);
2659 struct page *pages[NFS4ACL_MAXPAGES];
2660 struct nfs_setaclargs arg = {
2661 .fh = NFS_FH(inode),
2662 .acl_pages = pages,
2663 .acl_len = buflen,
2664 };
2665 struct rpc_message msg = {
2666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2667 .rpc_argp = &arg,
2668 .rpc_resp = NULL,
2669 };
2670 int ret;
2671
2672 if (!nfs4_server_supports_acls(server))
2673 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002674 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002675 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002676 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002677 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002678 return ret;
2679}
2680
Trond Myklebust16b42892006-08-24 12:27:15 -04002681static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2682{
2683 struct nfs4_exception exception = { };
2684 int err;
2685 do {
2686 err = nfs4_handle_exception(NFS_SERVER(inode),
2687 __nfs4_proc_set_acl(inode, buf, buflen),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691}
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002694nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
David Howells7539bba2006-08-22 20:06:09 -04002696 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 if (!clp || task->tk_status >= 0)
2699 return 0;
2700 switch(task->tk_status) {
2701 case -NFS4ERR_STALE_CLIENTID:
2702 case -NFS4ERR_STALE_STATEID:
2703 case -NFS4ERR_EXPIRED:
2704 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2705 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002706 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 rpc_wake_up_task(task);
2708 task->tk_status = 0;
2709 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002711 nfs_inc_server_stats((struct nfs_server *) server,
2712 NFSIOS_DELAY);
2713 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2715 task->tk_status = 0;
2716 return -EAGAIN;
2717 case -NFS4ERR_OLD_STATEID:
2718 task->tk_status = 0;
2719 return -EAGAIN;
2720 }
2721 task->tk_status = nfs4_map_errors(task->tk_status);
2722 return 0;
2723}
2724
Trond Myklebust433fbe42006-01-03 09:55:22 +01002725static int nfs4_wait_bit_interruptible(void *word)
2726{
2727 if (signal_pending(current))
2728 return -ERESTARTSYS;
2729 schedule();
2730 return 0;
2731}
2732
David Howellsadfa6f92006-08-22 20:06:08 -04002733static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002736 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 might_sleep();
2739
Trond Myklebuste1485822006-12-13 16:43:13 -05002740 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002743 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2744 nfs4_wait_bit_interruptible,
2745 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002747
2748 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return res;
2750}
2751
2752static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2753{
2754 sigset_t oldset;
2755 int res = 0;
2756
2757 might_sleep();
2758
2759 if (*timeout <= 0)
2760 *timeout = NFS4_POLL_RETRY_MIN;
2761 if (*timeout > NFS4_POLL_RETRY_MAX)
2762 *timeout = NFS4_POLL_RETRY_MAX;
2763 rpc_clnt_sigmask(clnt, &oldset);
2764 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002765 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (signalled())
2767 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002768 } else
2769 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 rpc_clnt_sigunmask(clnt, &oldset);
2771 *timeout <<= 1;
2772 return res;
2773}
2774
2775/* This is the error handling routine for processes that are allowed
2776 * to sleep.
2777 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002778static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
David Howells7539bba2006-08-22 20:06:09 -04002780 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 int ret = errorcode;
2782
2783 exception->retry = 0;
2784 switch(errorcode) {
2785 case 0:
2786 return 0;
2787 case -NFS4ERR_STALE_CLIENTID:
2788 case -NFS4ERR_STALE_STATEID:
2789 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002790 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 ret = nfs4_wait_clnt_recover(server->client, clp);
2792 if (ret == 0)
2793 exception->retry = 1;
2794 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002795 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 case -NFS4ERR_GRACE:
2797 case -NFS4ERR_DELAY:
2798 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002799 if (ret != 0)
2800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002802 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 /* We failed to handle the error */
2805 return nfs4_map_errors(ret);
2806}
2807
David Howellsadfa6f92006-08-22 20:06:08 -04002808int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 nfs4_verifier sc_verifier;
2811 struct nfs4_setclientid setclientid = {
2812 .sc_verifier = &sc_verifier,
2813 .sc_prog = program,
2814 };
2815 struct rpc_message msg = {
2816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2817 .rpc_argp = &setclientid,
2818 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002819 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 };
Al Virobc4785c2006-10-19 23:28:51 -07002821 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 int loop = 0;
2823 int status;
2824
Al Virobc4785c2006-10-19 23:28:51 -07002825 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2827 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2828
2829 for(;;) {
2830 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2831 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002832 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002833 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 clp->cl_id_uniquifier);
2835 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2836 sizeof(setclientid.sc_netid), "tcp");
2837 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2838 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2839 clp->cl_ipaddr, port >> 8, port & 255);
2840
2841 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2842 if (status != -NFS4ERR_CLID_INUSE)
2843 break;
2844 if (signalled())
2845 break;
2846 if (loop++ & 1)
2847 ssleep(clp->cl_lease_time + 1);
2848 else
2849 if (++clp->cl_id_uniquifier == 0)
2850 break;
2851 }
2852 return status;
2853}
2854
David Howellsadfa6f92006-08-22 20:06:08 -04002855static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 struct nfs_fsinfo fsinfo;
2858 struct rpc_message msg = {
2859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2860 .rpc_argp = clp,
2861 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002862 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 };
2864 unsigned long now;
2865 int status;
2866
2867 now = jiffies;
2868 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2869 if (status == 0) {
2870 spin_lock(&clp->cl_lock);
2871 clp->cl_lease_time = fsinfo.lease_time * HZ;
2872 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002873 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 spin_unlock(&clp->cl_lock);
2875 }
2876 return status;
2877}
2878
David Howellsadfa6f92006-08-22 20:06:08 -04002879int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002880{
2881 long timeout;
2882 int err;
2883 do {
2884 err = _nfs4_proc_setclientid_confirm(clp, cred);
2885 switch (err) {
2886 case 0:
2887 return err;
2888 case -NFS4ERR_RESOURCE:
2889 /* The IBM lawyers misread another document! */
2890 case -NFS4ERR_DELAY:
2891 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2892 }
2893 } while (err == 0);
2894 return err;
2895}
2896
Trond Myklebustfe650402006-01-03 09:55:18 +01002897struct nfs4_delegreturndata {
2898 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002899 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002900 struct nfs_fh fh;
2901 nfs4_stateid stateid;
2902 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002903 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002904 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002905 int rpc_status;
2906};
2907
2908static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Trond Myklebustfe650402006-01-03 09:55:18 +01002910 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 struct rpc_message msg = {
2912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002913 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002914 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002915 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002917 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002918 rpc_call_setup(task, &msg, 0);
2919}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Trond Myklebustfe650402006-01-03 09:55:18 +01002921static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2922{
2923 struct nfs4_delegreturndata *data = calldata;
2924 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002925 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002926 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002927}
2928
2929static void nfs4_delegreturn_release(void *calldata)
2930{
2931 struct nfs4_delegreturndata *data = calldata;
2932
2933 put_rpccred(data->cred);
2934 kfree(calldata);
2935}
2936
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002937static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002938 .rpc_call_prepare = nfs4_delegreturn_prepare,
2939 .rpc_call_done = nfs4_delegreturn_done,
2940 .rpc_release = nfs4_delegreturn_release,
2941};
2942
2943static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2944{
2945 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002946 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002947 struct rpc_task *task;
2948 int status;
2949
2950 data = kmalloc(sizeof(*data), GFP_KERNEL);
2951 if (data == NULL)
2952 return -ENOMEM;
2953 data->args.fhandle = &data->fh;
2954 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002955 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002956 nfs_copy_fh(&data->fh, NFS_FH(inode));
2957 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002958 data->res.fattr = &data->fattr;
2959 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002960 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002961 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002962 data->rpc_status = 0;
2963
2964 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002965 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01002966 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002967 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002968 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002970 if (status == 0)
2971 nfs_post_op_update_inode(inode, &data->fattr);
2972 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002973 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002974 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2978{
2979 struct nfs_server *server = NFS_SERVER(inode);
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2984 switch (err) {
2985 case -NFS4ERR_STALE_STATEID:
2986 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 case 0:
2988 return 0;
2989 }
2990 err = nfs4_handle_exception(server, err, &exception);
2991 } while (exception.retry);
2992 return err;
2993}
2994
2995#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2996#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2997
2998/*
2999 * sleep, with exponential backoff, and retry the LOCK operation.
3000 */
3001static unsigned long
3002nfs4_set_lock_task_retry(unsigned long timeout)
3003{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003004 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 timeout <<= 1;
3006 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3007 return NFS4_LOCK_MAXTIMEOUT;
3008 return timeout;
3009}
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3012{
3013 struct inode *inode = state->inode;
3014 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003015 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003016 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003018 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003020 struct nfs_lockt_res res = {
3021 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 };
3023 struct rpc_message msg = {
3024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3025 .rpc_argp = &arg,
3026 .rpc_resp = &res,
3027 .rpc_cred = state->owner->so_cred,
3028 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct nfs4_lock_state *lsp;
3030 int status;
3031
3032 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003033 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003034 status = nfs4_set_lock_state(state, request);
3035 if (status != 0)
3036 goto out;
3037 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003038 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003040 switch (status) {
3041 case 0:
3042 request->fl_type = F_UNLCK;
3043 break;
3044 case -NFS4ERR_DENIED:
3045 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003047 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003048out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 up_read(&clp->cl_sem);
3050 return status;
3051}
3052
3053static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3054{
3055 struct nfs4_exception exception = { };
3056 int err;
3057
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3060 _nfs4_proc_getlk(state, cmd, request),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
3066static int do_vfs_lock(struct file *file, struct file_lock *fl)
3067{
3068 int res = 0;
3069 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3070 case FL_POSIX:
3071 res = posix_lock_file_wait(file, fl);
3072 break;
3073 case FL_FLOCK:
3074 res = flock_lock_file_wait(file, fl);
3075 break;
3076 default:
3077 BUG();
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return res;
3080}
3081
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003082struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003083 struct nfs_locku_args arg;
3084 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003085 struct nfs4_lock_state *lsp;
3086 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003087 struct file_lock fl;
3088 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003089 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003090};
3091
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003092static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3093 struct nfs_open_context *ctx,
3094 struct nfs4_lock_state *lsp,
3095 struct nfs_seqid *seqid)
3096{
3097 struct nfs4_unlockdata *p;
3098 struct inode *inode = lsp->ls_state->inode;
3099
3100 p = kmalloc(sizeof(*p), GFP_KERNEL);
3101 if (p == NULL)
3102 return NULL;
3103 p->arg.fh = NFS_FH(inode);
3104 p->arg.fl = &p->fl;
3105 p->arg.seqid = seqid;
3106 p->arg.stateid = &lsp->ls_stateid;
3107 p->lsp = lsp;
3108 atomic_inc(&lsp->ls_count);
3109 /* Ensure we don't close file until we're done freeing locks! */
3110 p->ctx = get_nfs_open_context(ctx);
3111 memcpy(&p->fl, fl, sizeof(p->fl));
3112 p->server = NFS_SERVER(inode);
3113 return p;
3114}
3115
Trond Myklebust06f814a2006-01-03 09:55:07 +01003116static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003117{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003118 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003119 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003120 nfs4_put_lock_state(calldata->lsp);
3121 put_nfs_open_context(calldata->ctx);
3122 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003123}
3124
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003125static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003126{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003127 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003128
Trond Myklebust06f814a2006-01-03 09:55:07 +01003129 if (RPC_ASSASSINATED(task))
3130 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003131 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003132 switch (task->tk_status) {
3133 case 0:
3134 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003135 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003136 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003137 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003138 break;
3139 case -NFS4ERR_STALE_STATEID:
3140 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003141 break;
3142 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003143 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003144 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003145 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003146}
3147
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003148static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003149{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003150 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct rpc_message msg = {
3152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003153 .rpc_argp = &calldata->arg,
3154 .rpc_resp = &calldata->res,
3155 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003158 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003159 return;
3160 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003161 /* Note: exit _without_ running nfs4_locku_done */
3162 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003163 return;
3164 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003165 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003166 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003169static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003170 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003171 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003172 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003173};
3174
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003175static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3176 struct nfs_open_context *ctx,
3177 struct nfs4_lock_state *lsp,
3178 struct nfs_seqid *seqid)
3179{
3180 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003181
3182 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3183 if (data == NULL) {
3184 nfs_free_seqid(seqid);
3185 return ERR_PTR(-ENOMEM);
3186 }
3187
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003188 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003189}
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3192{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003193 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003194 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003195 struct rpc_task *task;
3196 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Trond Myklebust9b073572006-06-29 16:38:34 -04003198 status = nfs4_set_lock_state(state, request);
3199 /* Unlock _before_ we do the RPC call */
3200 request->fl_flags |= FL_EXISTS;
3201 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3202 goto out;
3203 if (status != 0)
3204 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003205 /* Is this a delegated lock? */
3206 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003207 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003208 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003209 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003210 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003211 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003212 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003213 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3214 status = PTR_ERR(task);
3215 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003216 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003217 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003218 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003219out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003223struct nfs4_lockdata {
3224 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003225 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003226 struct nfs4_lock_state *lsp;
3227 struct nfs_open_context *ctx;
3228 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003229 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003230 int rpc_status;
3231 int cancelled;
3232};
3233
3234static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3235 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3236{
3237 struct nfs4_lockdata *p;
3238 struct inode *inode = lsp->ls_state->inode;
3239 struct nfs_server *server = NFS_SERVER(inode);
3240
3241 p = kzalloc(sizeof(*p), GFP_KERNEL);
3242 if (p == NULL)
3243 return NULL;
3244
3245 p->arg.fh = NFS_FH(inode);
3246 p->arg.fl = &p->fl;
3247 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3248 if (p->arg.lock_seqid == NULL)
3249 goto out_free;
3250 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003251 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003252 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003253 p->lsp = lsp;
3254 atomic_inc(&lsp->ls_count);
3255 p->ctx = get_nfs_open_context(ctx);
3256 memcpy(&p->fl, fl, sizeof(p->fl));
3257 return p;
3258out_free:
3259 kfree(p);
3260 return NULL;
3261}
3262
3263static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3264{
3265 struct nfs4_lockdata *data = calldata;
3266 struct nfs4_state *state = data->lsp->ls_state;
3267 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3270 .rpc_argp = &data->arg,
3271 .rpc_resp = &data->res,
3272 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003275 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3276 return;
3277 dprintk("%s: begin!\n", __FUNCTION__);
3278 /* Do we need to do an open_to_lock_owner? */
3279 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3280 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3281 if (data->arg.open_seqid == NULL) {
3282 data->rpc_status = -ENOMEM;
3283 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003284 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003285 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003286 data->arg.open_stateid = &state->stateid;
3287 data->arg.new_lock_owner = 1;
3288 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003289 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003290 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003291out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003292 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3293}
3294
3295static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3296{
3297 struct nfs4_lockdata *data = calldata;
3298
3299 dprintk("%s: begin!\n", __FUNCTION__);
3300
3301 data->rpc_status = task->tk_status;
3302 if (RPC_ASSASSINATED(task))
3303 goto out;
3304 if (data->arg.new_lock_owner != 0) {
3305 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3306 if (data->rpc_status == 0)
3307 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3308 else
3309 goto out;
3310 }
3311 if (data->rpc_status == 0) {
3312 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3313 sizeof(data->lsp->ls_stateid.data));
3314 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003315 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003316 }
3317 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3318out:
3319 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3320}
3321
3322static void nfs4_lock_release(void *calldata)
3323{
3324 struct nfs4_lockdata *data = calldata;
3325
3326 dprintk("%s: begin!\n", __FUNCTION__);
3327 if (data->arg.open_seqid != NULL)
3328 nfs_free_seqid(data->arg.open_seqid);
3329 if (data->cancelled != 0) {
3330 struct rpc_task *task;
3331 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3332 data->arg.lock_seqid);
3333 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003334 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003335 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3336 } else
3337 nfs_free_seqid(data->arg.lock_seqid);
3338 nfs4_put_lock_state(data->lsp);
3339 put_nfs_open_context(data->ctx);
3340 kfree(data);
3341 dprintk("%s: done!\n", __FUNCTION__);
3342}
3343
3344static const struct rpc_call_ops nfs4_lock_ops = {
3345 .rpc_call_prepare = nfs4_lock_prepare,
3346 .rpc_call_done = nfs4_lock_done,
3347 .rpc_release = nfs4_lock_release,
3348};
3349
3350static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3351{
3352 struct nfs4_lockdata *data;
3353 struct rpc_task *task;
3354 int ret;
3355
3356 dprintk("%s: begin!\n", __FUNCTION__);
3357 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3358 fl->fl_u.nfs4_fl.owner);
3359 if (data == NULL)
3360 return -ENOMEM;
3361 if (IS_SETLKW(cmd))
3362 data->arg.block = 1;
3363 if (reclaim != 0)
3364 data->arg.reclaim = 1;
3365 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3366 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003367 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003368 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003369 ret = nfs4_wait_for_completion_rpc_task(task);
3370 if (ret == 0) {
3371 ret = data->rpc_status;
3372 if (ret == -NFS4ERR_DENIED)
3373 ret = -EAGAIN;
3374 } else
3375 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003376 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003377 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3378 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
3381static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3382{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003383 struct nfs_server *server = NFS_SERVER(state->inode);
3384 struct nfs4_exception exception = { };
3385 int err;
3386
3387 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003388 /* Cache the lock if possible... */
3389 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3390 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003391 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3392 if (err != -NFS4ERR_DELAY)
3393 break;
3394 nfs4_handle_exception(server, err, &exception);
3395 } while (exception.retry);
3396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3400{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003401 struct nfs_server *server = NFS_SERVER(state->inode);
3402 struct nfs4_exception exception = { };
3403 int err;
3404
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003405 err = nfs4_set_lock_state(state, request);
3406 if (err != 0)
3407 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003408 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3410 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003411 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3412 if (err != -NFS4ERR_DELAY)
3413 break;
3414 nfs4_handle_exception(server, err, &exception);
3415 } while (exception.retry);
3416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
3419static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3420{
David Howellsadfa6f92006-08-22 20:06:08 -04003421 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003422 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 int status;
3424
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003425 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003426 status = nfs4_set_lock_state(state, request);
3427 if (status != 0)
3428 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003429 request->fl_flags |= FL_ACCESS;
3430 status = do_vfs_lock(request->fl_file, request);
3431 if (status < 0)
3432 goto out;
3433 down_read(&clp->cl_sem);
3434 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3435 struct nfs_inode *nfsi = NFS_I(state->inode);
3436 /* Yes: cache locks! */
3437 down_read(&nfsi->rwsem);
3438 /* ...but avoid races with delegation recall... */
3439 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3440 request->fl_flags = fl_flags & ~FL_SLEEP;
3441 status = do_vfs_lock(request->fl_file, request);
3442 up_read(&nfsi->rwsem);
3443 goto out_unlock;
3444 }
3445 up_read(&nfsi->rwsem);
3446 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003447 status = _nfs4_do_setlk(state, cmd, request, 0);
3448 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003449 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003450 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003451 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003452 if (do_vfs_lock(request->fl_file, request) < 0)
3453 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003454out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003456out:
3457 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return status;
3459}
3460
3461static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3462{
3463 struct nfs4_exception exception = { };
3464 int err;
3465
3466 do {
3467 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3468 _nfs4_proc_setlk(state, cmd, request),
3469 &exception);
3470 } while (exception.retry);
3471 return err;
3472}
3473
3474static int
3475nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3476{
3477 struct nfs_open_context *ctx;
3478 struct nfs4_state *state;
3479 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3480 int status;
3481
3482 /* verify open state */
3483 ctx = (struct nfs_open_context *)filp->private_data;
3484 state = ctx->state;
3485
3486 if (request->fl_start < 0 || request->fl_end < 0)
3487 return -EINVAL;
3488
3489 if (IS_GETLK(cmd))
3490 return nfs4_proc_getlk(state, F_GETLK, request);
3491
3492 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3493 return -EINVAL;
3494
3495 if (request->fl_type == F_UNLCK)
3496 return nfs4_proc_unlck(state, cmd, request);
3497
3498 do {
3499 status = nfs4_proc_setlk(state, cmd, request);
3500 if ((status != -EAGAIN) || IS_SETLK(cmd))
3501 break;
3502 timeout = nfs4_set_lock_task_retry(timeout);
3503 status = -ERESTARTSYS;
3504 if (signalled())
3505 break;
3506 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return status;
3508}
3509
Trond Myklebust888e6942005-11-04 15:38:11 -05003510int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3511{
3512 struct nfs_server *server = NFS_SERVER(state->inode);
3513 struct nfs4_exception exception = { };
3514 int err;
3515
3516 err = nfs4_set_lock_state(state, fl);
3517 if (err != 0)
3518 goto out;
3519 do {
3520 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3521 if (err != -NFS4ERR_DELAY)
3522 break;
3523 err = nfs4_handle_exception(server, err, &exception);
3524 } while (exception.retry);
3525out:
3526 return err;
3527}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003528
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003529#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3530
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003531int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3532 size_t buflen, int flags)
3533{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003534 struct inode *inode = dentry->d_inode;
3535
3536 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3537 return -EOPNOTSUPP;
3538
3539 if (!S_ISREG(inode->i_mode) &&
3540 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3541 return -EPERM;
3542
3543 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003544}
3545
3546/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3547 * and that's what we'll do for e.g. user attributes that haven't been set.
3548 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3549 * attributes in kernel-managed attribute namespaces. */
3550ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3551 size_t buflen)
3552{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003553 struct inode *inode = dentry->d_inode;
3554
3555 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3556 return -EOPNOTSUPP;
3557
3558 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003559}
3560
3561ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3562{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003563 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003564
J. Bruce Fields096455a2006-03-20 23:23:42 -05003565 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3566 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003567 if (buf && buflen < len)
3568 return -ERANGE;
3569 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003570 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3571 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003572}
3573
Trond Myklebustc228fd32007-01-13 02:28:11 -05003574int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003575 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003576{
3577 struct nfs_server *server = NFS_SERVER(dir);
3578 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003579 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3580 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003581 };
3582 struct nfs4_fs_locations_arg args = {
3583 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003584 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003585 .page = page,
3586 .bitmask = bitmask,
3587 };
3588 struct rpc_message msg = {
3589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3590 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003591 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003592 };
3593 int status;
3594
3595 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003596 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003597 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003598 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003599 status = rpc_call_sync(server->client, &msg, 0);
3600 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3601 return status;
3602}
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3605 .recover_open = nfs4_open_reclaim,
3606 .recover_lock = nfs4_lock_reclaim,
3607};
3608
3609struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3610 .recover_open = nfs4_open_expired,
3611 .recover_lock = nfs4_lock_expired,
3612};
3613
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003614static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003615 .permission = nfs_permission,
3616 .getattr = nfs_getattr,
3617 .setattr = nfs_setattr,
3618 .getxattr = nfs4_getxattr,
3619 .setxattr = nfs4_setxattr,
3620 .listxattr = nfs4_listxattr,
3621};
3622
David Howells509de812006-08-22 20:06:11 -04003623const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 .version = 4, /* protocol version */
3625 .dentry_ops = &nfs4_dentry_operations,
3626 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003627 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 .getroot = nfs4_proc_get_root,
3629 .getattr = nfs4_proc_getattr,
3630 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003631 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 .lookup = nfs4_proc_lookup,
3633 .access = nfs4_proc_access,
3634 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 .create = nfs4_proc_create,
3636 .remove = nfs4_proc_remove,
3637 .unlink_setup = nfs4_proc_unlink_setup,
3638 .unlink_done = nfs4_proc_unlink_done,
3639 .rename = nfs4_proc_rename,
3640 .link = nfs4_proc_link,
3641 .symlink = nfs4_proc_symlink,
3642 .mkdir = nfs4_proc_mkdir,
3643 .rmdir = nfs4_proc_remove,
3644 .readdir = nfs4_proc_readdir,
3645 .mknod = nfs4_proc_mknod,
3646 .statfs = nfs4_proc_statfs,
3647 .fsinfo = nfs4_proc_fsinfo,
3648 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003649 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 .decode_dirent = nfs4_decode_dirent,
3651 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003652 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003654 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003656 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003657 .file_open = nfs_open,
3658 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003660 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661};
3662
3663/*
3664 * Local variables:
3665 * c-basic-offset: 8
3666 * End:
3667 */