blob: 69aab8db49473dba7529463b7a0cbe99988c7a92 [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 Myklebustad389da2007-06-05 12:30:00 -0400233static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100234 struct nfs4_state_owner *sp, int flags,
235 const struct iattr *attrs)
236{
Trond Myklebustad389da2007-06-05 12:30:00 -0400237 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100238 struct inode *dir = parent->d_inode;
239 struct nfs_server *server = NFS_SERVER(dir);
240 struct nfs4_opendata *p;
241
242 p = kzalloc(sizeof(*p), GFP_KERNEL);
243 if (p == NULL)
244 goto err;
245 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
246 if (p->o_arg.seqid == NULL)
247 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400248 p->path.mnt = mntget(path->mnt);
249 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100250 p->dir = parent;
251 p->owner = sp;
252 atomic_inc(&sp->so_count);
253 p->o_arg.fh = NFS_FH(dir);
254 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400255 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400256 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400257 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100258 p->o_arg.server = server;
259 p->o_arg.bitmask = server->attr_bitmask;
260 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
261 p->o_res.f_attr = &p->f_attr;
262 p->o_res.dir_attr = &p->dir_attr;
263 p->o_res.server = server;
264 nfs_fattr_init(&p->f_attr);
265 nfs_fattr_init(&p->dir_attr);
266 if (flags & O_EXCL) {
267 u32 *s = (u32 *) p->o_arg.u.verifier.data;
268 s[0] = jiffies;
269 s[1] = current->pid;
270 } else if (flags & O_CREAT) {
271 p->o_arg.u.attrs = &p->attrs;
272 memcpy(&p->attrs, attrs, sizeof(p->attrs));
273 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100274 p->c_arg.fh = &p->o_res.fh;
275 p->c_arg.stateid = &p->o_res.stateid;
276 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400277 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100278 return p;
279err_free:
280 kfree(p);
281err:
282 dput(parent);
283 return NULL;
284}
285
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400286static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100287{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400288 struct nfs4_opendata *p = container_of(kref,
289 struct nfs4_opendata, kref);
290
291 nfs_free_seqid(p->o_arg.seqid);
292 nfs4_put_state_owner(p->owner);
293 dput(p->dir);
294 dput(p->path.dentry);
295 mntput(p->path.mnt);
296 kfree(p);
297}
298
299static void nfs4_opendata_put(struct nfs4_opendata *p)
300{
301 if (p != NULL)
302 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100303}
304
Trond Myklebust06f814a2006-01-03 09:55:07 +0100305static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
306{
307 sigset_t oldset;
308 int ret;
309
310 rpc_clnt_sigmask(task->tk_client, &oldset);
311 ret = rpc_wait_for_completion_task(task);
312 rpc_clnt_sigunmask(task->tk_client, &oldset);
313 return ret;
314}
315
Trond Myklebuste7616922006-01-03 09:55:13 +0100316static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
317{
318 switch (open_flags) {
319 case FMODE_WRITE:
320 state->n_wronly++;
321 break;
322 case FMODE_READ:
323 state->n_rdonly++;
324 break;
325 case FMODE_READ|FMODE_WRITE:
326 state->n_rdwr++;
327 }
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
331{
332 struct inode *inode = state->inode;
333
334 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500335 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700336 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500338 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100339 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500340 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700342 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100345static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
346{
347 struct inode *inode;
348 struct nfs4_state *state = NULL;
349
350 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
351 goto out;
352 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500353 if (IS_ERR(inode))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100354 goto out;
355 state = nfs4_get_open_state(inode, data->owner);
356 if (state == NULL)
357 goto put_inode;
358 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
359put_inode:
360 iput(inode);
361out:
362 return state;
363}
364
Trond Myklebust864472e2006-01-03 09:55:15 +0100365static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
366{
367 struct nfs_inode *nfsi = NFS_I(state->inode);
368 struct nfs_open_context *ctx;
369
370 spin_lock(&state->inode->i_lock);
371 list_for_each_entry(ctx, &nfsi->open_files, list) {
372 if (ctx->state != state)
373 continue;
374 get_nfs_open_context(ctx);
375 spin_unlock(&state->inode->i_lock);
376 return ctx;
377 }
378 spin_unlock(&state->inode->i_lock);
379 return ERR_PTR(-ENOENT);
380}
381
382static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
383{
384 int ret;
385
386 opendata->o_arg.open_flags = openflags;
387 ret = _nfs4_proc_open(opendata);
388 if (ret != 0)
389 return ret;
390 memcpy(stateid->data, opendata->o_res.stateid.data,
391 sizeof(stateid->data));
392 return 0;
393}
394
395static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
396{
397 nfs4_stateid stateid;
398 struct nfs4_state *newstate;
399 int mode = 0;
400 int delegation = 0;
401 int ret;
402
403 /* memory barrier prior to reading state->n_* */
404 smp_rmb();
405 if (state->n_rdwr != 0) {
406 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
407 if (ret != 0)
408 return ret;
409 mode |= FMODE_READ|FMODE_WRITE;
410 if (opendata->o_res.delegation_type != 0)
411 delegation = opendata->o_res.delegation_type;
412 smp_rmb();
413 }
414 if (state->n_wronly != 0) {
415 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
416 if (ret != 0)
417 return ret;
418 mode |= FMODE_WRITE;
419 if (opendata->o_res.delegation_type != 0)
420 delegation = opendata->o_res.delegation_type;
421 smp_rmb();
422 }
423 if (state->n_rdonly != 0) {
424 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
425 if (ret != 0)
426 return ret;
427 mode |= FMODE_READ;
428 }
429 clear_bit(NFS_DELEGATED_STATE, &state->flags);
430 if (mode == 0)
431 return 0;
432 if (opendata->o_res.delegation_type == 0)
433 opendata->o_res.delegation_type = delegation;
434 opendata->o_arg.open_flags |= mode;
435 newstate = nfs4_opendata_to_nfs4_state(opendata);
436 if (newstate != NULL) {
437 if (opendata->o_res.delegation_type != 0) {
438 struct nfs_inode *nfsi = NFS_I(newstate->inode);
439 int delegation_flags = 0;
440 if (nfsi->delegation)
441 delegation_flags = nfsi->delegation->flags;
442 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
443 nfs_inode_set_delegation(newstate->inode,
444 opendata->owner->so_cred,
445 &opendata->o_res);
446 else
447 nfs_inode_reclaim_delegation(newstate->inode,
448 opendata->owner->so_cred,
449 &opendata->o_res);
450 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400451 nfs4_close_state(&opendata->path, newstate, opendata->o_arg.open_flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100452 }
453 if (newstate != state)
454 return -ESTALE;
455 return 0;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * OPEN_RECLAIM:
460 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400462static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Trond Myklebust864472e2006-01-03 09:55:15 +0100464 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
465 struct nfs4_opendata *opendata;
466 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int status;
468
469 if (delegation != NULL) {
470 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
471 memcpy(&state->stateid, &delegation->stateid,
472 sizeof(state->stateid));
473 set_bit(NFS_DELEGATED_STATE, &state->flags);
474 return 0;
475 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100476 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400478 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
Trond Myklebust864472e2006-01-03 09:55:15 +0100479 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700480 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100481 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
482 opendata->o_arg.fh = NFS_FH(state->inode);
483 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
484 opendata->o_arg.u.delegation_type = delegation_type;
485 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400486 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return status;
488}
489
Trond Myklebust539cd032007-06-05 11:46:42 -0400490static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct nfs_server *server = NFS_SERVER(state->inode);
493 struct nfs4_exception exception = { };
494 int err;
495 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400496 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000497 if (err != -NFS4ERR_DELAY)
498 break;
499 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 } while (exception.retry);
501 return err;
502}
503
Trond Myklebust864472e2006-01-03 09:55:15 +0100504static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
505{
506 struct nfs_open_context *ctx;
507 int ret;
508
509 ctx = nfs4_state_find_open_context(state);
510 if (IS_ERR(ctx))
511 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400512 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100513 put_nfs_open_context(ctx);
514 return ret;
515}
516
Trond Myklebust539cd032007-06-05 11:46:42 -0400517static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100520 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100521 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust864472e2006-01-03 09:55:15 +0100524 return 0;
Trond Myklebustad389da2007-06-05 12:30:00 -0400525 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100526 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100527 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100528 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100529 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
530 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100531 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400532 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Trond Myklebust539cd032007-06-05 11:46:42 -0400536int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400539 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int err;
541 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400542 err = _nfs4_open_delegation_recall(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 switch (err) {
544 case 0:
545 return err;
546 case -NFS4ERR_STALE_CLIENTID:
547 case -NFS4ERR_STALE_STATEID:
548 case -NFS4ERR_EXPIRED:
549 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400550 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return err;
552 }
553 err = nfs4_handle_exception(server, err, &exception);
554 } while (exception.retry);
555 return err;
556}
557
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100558static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100560 struct nfs4_opendata *data = calldata;
561 struct rpc_message msg = {
562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
563 .rpc_argp = &data->c_arg,
564 .rpc_resp = &data->c_res,
565 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100567 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100568 rpc_call_setup(task, &msg, 0);
569}
570
571static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
572{
573 struct nfs4_opendata *data = calldata;
574
575 data->rpc_status = task->tk_status;
576 if (RPC_ASSASSINATED(task))
577 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100578 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100579 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
580 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100581 renew_lease(data->o_res.server, data->timestamp);
582 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100583 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
584 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
585}
586
587static void nfs4_open_confirm_release(void *calldata)
588{
589 struct nfs4_opendata *data = calldata;
590 struct nfs4_state *state = NULL;
591
592 /* If this request hasn't been cancelled, do nothing */
593 if (data->cancelled == 0)
594 goto out_free;
595 /* In case of error, no cleanup! */
596 if (data->rpc_status != 0)
597 goto out_free;
598 nfs_confirm_seqid(&data->owner->so_seqid, 0);
599 state = nfs4_opendata_to_nfs4_state(data);
600 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400601 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100602out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400603 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100604}
605
606static const struct rpc_call_ops nfs4_open_confirm_ops = {
607 .rpc_call_prepare = nfs4_open_confirm_prepare,
608 .rpc_call_done = nfs4_open_confirm_done,
609 .rpc_release = nfs4_open_confirm_release,
610};
611
612/*
613 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
614 */
615static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
616{
617 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
618 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int status;
620
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400621 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500622 /*
623 * If rpc_run_task() ends up calling ->rpc_release(), we
624 * want to ensure that it takes the 'error' code path.
625 */
626 data->rpc_status = -ENOMEM;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100627 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500628 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100629 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100630 status = nfs4_wait_for_completion_rpc_task(task);
631 if (status != 0) {
632 data->cancelled = 1;
633 smp_wmb();
634 } else
635 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500636 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return status;
638}
639
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100640static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100642 struct nfs4_opendata *data = calldata;
643 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct rpc_message msg = {
645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100646 .rpc_argp = &data->o_arg,
647 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 .rpc_cred = sp->so_cred,
649 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100650
651 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
652 return;
653 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400654 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100655 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100656 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
657 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100658 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100659 rpc_call_setup(task, &msg, 0);
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100662static void nfs4_open_done(struct rpc_task *task, void *calldata)
663{
664 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100666 data->rpc_status = task->tk_status;
667 if (RPC_ASSASSINATED(task))
668 return;
669 if (task->tk_status == 0) {
670 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700671 case S_IFREG:
672 break;
673 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100674 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700675 break;
676 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100677 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700678 break;
679 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100680 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700681 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100682 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700683 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100684 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
685}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700686
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100687static void nfs4_open_release(void *calldata)
688{
689 struct nfs4_opendata *data = calldata;
690 struct nfs4_state *state = NULL;
691
692 /* If this request hasn't been cancelled, do nothing */
693 if (data->cancelled == 0)
694 goto out_free;
695 /* In case of error, no cleanup! */
696 if (data->rpc_status != 0)
697 goto out_free;
698 /* In case we need an open_confirm, no cleanup! */
699 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
700 goto out_free;
701 nfs_confirm_seqid(&data->owner->so_seqid, 0);
702 state = nfs4_opendata_to_nfs4_state(data);
703 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400704 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100705out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400706 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100707}
708
709static const struct rpc_call_ops nfs4_open_ops = {
710 .rpc_call_prepare = nfs4_open_prepare,
711 .rpc_call_done = nfs4_open_done,
712 .rpc_release = nfs4_open_release,
713};
714
715/*
716 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
717 */
718static int _nfs4_proc_open(struct nfs4_opendata *data)
719{
720 struct inode *dir = data->dir->d_inode;
721 struct nfs_server *server = NFS_SERVER(dir);
722 struct nfs_openargs *o_arg = &data->o_arg;
723 struct nfs_openres *o_res = &data->o_res;
724 struct rpc_task *task;
725 int status;
726
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400727 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500728 /*
729 * If rpc_run_task() ends up calling ->rpc_release(), we
730 * want to ensure that it takes the 'error' code path.
731 */
732 data->rpc_status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100733 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500734 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100735 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100736 status = nfs4_wait_for_completion_rpc_task(task);
737 if (status != 0) {
738 data->cancelled = 1;
739 smp_wmb();
740 } else
741 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500742 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100744 return status;
745
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400746 if (o_arg->open_flags & O_CREAT) {
747 update_changeattr(dir, &o_res->cinfo);
748 nfs_post_op_update_inode(dir, o_res->dir_attr);
749 } else
750 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100752 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100754 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100756 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400758 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
763{
764 struct nfs_access_entry cache;
765 int mask = 0;
766 int status;
767
768 if (openflags & FMODE_READ)
769 mask |= MAY_READ;
770 if (openflags & FMODE_WRITE)
771 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400772 if (openflags & FMODE_EXEC)
773 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 status = nfs_access_get_cached(inode, cred, &cache);
775 if (status == 0)
776 goto out;
777
778 /* Be clever: ask server to check for all possible rights */
779 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
780 cache.cred = cred;
781 cache.jiffies = jiffies;
782 status = _nfs4_proc_access(inode, &cache);
783 if (status != 0)
784 return status;
785 nfs_access_add_cache(inode, &cache);
786out:
787 if ((cache.mask & mask) == mask)
788 return 0;
789 return -EACCES;
790}
791
Trond Myklebust10afec92007-05-14 17:16:04 -0400792static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100793{
David Howells7539bba2006-08-22 20:06:09 -0400794 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400795 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100796
Trond Myklebust6b309542006-09-14 14:03:14 -0400797 for (;;) {
798 ret = nfs4_wait_clnt_recover(server->client, clp);
799 if (ret != 0)
800 return ret;
801 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
802 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100803 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400804 }
805 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * OPEN_EXPIRED:
810 * reclaim state on the server after a network partition.
811 * Assumes caller holds the appropriate lock
812 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400813static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100818 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100819 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400822 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100823 if (ret < 0)
824 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
826 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400829 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100831 return -ENOMEM;
832 ret = nfs4_open_recover(opendata, state);
833 if (ret == -ESTALE) {
834 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400835 nfs4_drop_state_owner(state->owner);
836 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400838 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100839 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Trond Myklebust539cd032007-06-05 11:46:42 -0400842static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000843{
Trond Myklebust539cd032007-06-05 11:46:42 -0400844 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000845 struct nfs4_exception exception = { };
846 int err;
847
848 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400849 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000850 if (err == -NFS4ERR_DELAY)
851 nfs4_handle_exception(server, err, &exception);
852 } while (exception.retry);
853 return err;
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100859 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Trond Myklebust864472e2006-01-03 09:55:15 +0100861 ctx = nfs4_state_find_open_context(state);
862 if (IS_ERR(ctx))
863 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400864 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100865 put_nfs_open_context(ctx);
866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100870 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
873{
874 struct nfs_delegation *delegation;
875 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400876 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct nfs_inode *nfsi = NFS_I(inode);
878 struct nfs4_state_owner *sp = NULL;
879 struct nfs4_state *state = NULL;
880 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
881 int err;
882
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100883 err = -ENOMEM;
884 if (!(sp = nfs4_get_state_owner(server, cred))) {
885 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
886 return err;
887 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100888 err = nfs4_recover_expired_lease(server);
889 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100890 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Protect against reboot recovery - NOTE ORDER! */
892 down_read(&clp->cl_sem);
893 /* Protect against delegation recall */
894 down_read(&nfsi->rwsem);
895 delegation = NFS_I(inode)->delegation;
896 err = -ENOENT;
897 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
898 goto out_err;
899 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 state = nfs4_get_open_state(inode, sp);
901 if (state == NULL)
902 goto out_err;
903
904 err = -ENOENT;
905 if ((state->state & open_flags) == open_flags) {
906 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100907 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 spin_unlock(&inode->i_lock);
909 goto out_ok;
910 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100911 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 lock_kernel();
914 err = _nfs4_do_access(inode, cred, open_flags);
915 unlock_kernel();
916 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100917 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 set_bit(NFS_DELEGATED_STATE, &state->flags);
919 update_open_stateid(state, &delegation->stateid, open_flags);
920out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 nfs4_put_state_owner(sp);
922 up_read(&nfsi->rwsem);
923 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100925 return 0;
926out_put_open_state:
927 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 up_read(&nfsi->rwsem);
930 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700931 if (err != -EACCES)
932 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100933out_put_state_owner:
934 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return err;
936}
937
938static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
939{
940 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500941 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int err;
943
944 do {
945 err = _nfs4_open_delegated(inode, flags, cred, &res);
946 if (err == 0)
947 break;
948 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
949 err, &exception));
950 } while (exception.retry);
951 return res;
952}
953
954/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400955 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
956 * fields corresponding to attributes that were used to store the verifier.
957 * Make sure we clobber those fields in the later setattr call
958 */
959static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
960{
961 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
962 !(sattr->ia_valid & ATTR_ATIME_SET))
963 sattr->ia_valid |= ATTR_ATIME;
964
965 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
966 !(sattr->ia_valid & ATTR_MTIME_SET))
967 sattr->ia_valid |= ATTR_MTIME;
968}
969
970/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100971 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Trond Myklebustad389da2007-06-05 12:30:00 -0400973static 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 -0700974{
975 struct nfs4_state_owner *sp;
976 struct nfs4_state *state = NULL;
977 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -0400978 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 status = -ENOMEM;
984 if (!(sp = nfs4_get_state_owner(server, cred))) {
985 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
986 goto out_err;
987 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100988 status = nfs4_recover_expired_lease(server);
989 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100990 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100991 down_read(&clp->cl_sem);
992 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -0400993 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100994 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -0400995 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100997 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400999 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001001 if (opendata->o_arg.open_flags & O_EXCL)
1002 nfs4_exclusive_attrset(opendata, sattr);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001005 state = nfs4_opendata_to_nfs4_state(opendata);
1006 if (state == NULL)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001007 goto err_opendata_put;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001010 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs4_put_state_owner(sp);
1012 up_read(&clp->cl_sem);
1013 *res = state;
1014 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001015err_opendata_put:
1016 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001017err_release_rwsem:
1018 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001019err_put_state_owner:
1020 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 *res = NULL;
1023 return status;
1024}
1025
1026
Trond Myklebustad389da2007-06-05 12:30:00 -04001027static 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 -07001028{
1029 struct nfs4_exception exception = { };
1030 struct nfs4_state *res;
1031 int status;
1032
1033 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001034 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (status == 0)
1036 break;
1037 /* NOTE: BAD_SEQID means the server and client disagree about the
1038 * book-keeping w.r.t. state-changing operations
1039 * (OPEN/CLOSE/LOCK/LOCKU...)
1040 * It is actually a sign of a bug on the client or on the server.
1041 *
1042 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001043 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * have unhashed the old state_owner for us, and that we can
1045 * therefore safely retry using a new one. We should still warn
1046 * the user though...
1047 */
1048 if (status == -NFS4ERR_BAD_SEQID) {
1049 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1050 exception.retry = 1;
1051 continue;
1052 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001053 /*
1054 * BAD_STATEID on OPEN means that the server cancelled our
1055 * state before it received the OPEN_CONFIRM.
1056 * Recover by retrying the request as per the discussion
1057 * on Page 181 of RFC3530.
1058 */
1059 if (status == -NFS4ERR_BAD_STATEID) {
1060 exception.retry = 1;
1061 continue;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1064 status, &exception));
1065 } while (exception.retry);
1066 return res;
1067}
1068
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001069static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1070 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001072 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001074 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 .iap = sattr,
1076 .server = server,
1077 .bitmask = server->attr_bitmask,
1078 };
1079 struct nfs_setattrres res = {
1080 .fattr = fattr,
1081 .server = server,
1082 };
1083 struct rpc_message msg = {
1084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1085 .rpc_argp = &arg,
1086 .rpc_resp = &res,
1087 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001088 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001089 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Trond Myklebust0e574af2005-10-27 22:12:38 -04001091 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001093 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1094 /* Use that stateid */
1095 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001097 nfs4_copy_stateid(&arg.stateid, state, current->files);
1098 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1100
Trond Myklebust65e43082005-08-16 11:49:44 -04001101 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001102 if (status == 0 && state != NULL)
1103 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001104 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001107static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1108 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001110 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct nfs4_exception exception = { };
1112 int err;
1113 do {
1114 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001115 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 &exception);
1117 } while (exception.retry);
1118 return err;
1119}
1120
1121struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001122 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct inode *inode;
1124 struct nfs4_state *state;
1125 struct nfs_closeargs arg;
1126 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001127 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001128 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129};
1130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001131static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001132{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001133 struct nfs4_closedata *calldata = data;
1134 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001135
1136 nfs4_put_open_state(calldata->state);
1137 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001138 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001139 dput(calldata->path.dentry);
1140 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001141 kfree(calldata);
1142}
1143
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001144static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001146 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct nfs_server *server = NFS_SERVER(calldata->inode);
1149
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001150 if (RPC_ASSASSINATED(task))
1151 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* hmm. we are done with the inode, and in the process of freeing
1153 * the state_owner. we keep this around to process errors
1154 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001155 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 switch (task->tk_status) {
1157 case 0:
1158 memcpy(&state->stateid, &calldata->res.stateid,
1159 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001160 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 case -NFS4ERR_STALE_STATEID:
1163 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
1165 default:
1166 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1167 rpc_restart_call(task);
1168 return;
1169 }
1170 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001171 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001174static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001176 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001177 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct rpc_message msg = {
1179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1180 .rpc_argp = &calldata->arg,
1181 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001182 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001184 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001186 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001187 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001188 /* Recalculate the new open mode in case someone reopened the file
1189 * while we were waiting in line to be scheduled.
1190 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001191 spin_lock(&state->owner->so_lock);
1192 spin_lock(&calldata->inode->i_lock);
1193 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001194 if (state->n_rdwr == 0) {
1195 if (state->n_rdonly == 0)
1196 mode &= ~FMODE_READ;
1197 if (state->n_wronly == 0)
1198 mode &= ~FMODE_WRITE;
1199 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001200 nfs4_state_set_mode_locked(state, mode);
1201 spin_unlock(&calldata->inode->i_lock);
1202 spin_unlock(&state->owner->so_lock);
1203 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001204 /* Note: exit _without_ calling nfs4_close_done */
1205 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001206 return;
1207 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001208 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001209 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001211 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001212 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001213 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001216static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001217 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001218 .rpc_call_done = nfs4_close_done,
1219 .rpc_release = nfs4_free_closedata,
1220};
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * It is possible for data to be read/written from a mem-mapped file
1224 * after the sys_close call (which hits the vfs layer as a flush).
1225 * This means that we can't safely call nfsv4 close on a file until
1226 * the inode is cleared. This in turn means that we are not good
1227 * NFSv4 citizens - we do not indicate to the server to update the file's
1228 * share state even when we are done with one of the three share
1229 * stateid's in the inode.
1230 *
1231 * NOTE: Caller must be holding the sp->so_owner semaphore!
1232 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001233int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001235 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001237 struct nfs4_state_owner *sp = state->owner;
1238 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001239 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Trond Myklebust95121352005-10-18 14:20:12 -07001241 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001243 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001244 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001246 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001247 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001249 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001250 if (calldata->arg.seqid == NULL)
1251 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001252 calldata->arg.bitmask = server->attr_bitmask;
1253 calldata->res.fattr = &calldata->fattr;
1254 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001255 calldata->path.mnt = mntget(path->mnt);
1256 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001257
Trond Myklebustb39e6252007-06-11 23:05:07 -04001258 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1259 if (IS_ERR(task))
1260 return PTR_ERR(task);
1261 rpc_put_task(task);
1262 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001263out_free_calldata:
1264 kfree(calldata);
1265out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001266 nfs4_put_open_state(state);
1267 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001268 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001271static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001272{
1273 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001274 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001275
Trond Myklebust1b45c462007-07-03 13:04:56 -04001276 /* If the open_intent is for execute, we have an extra check to make */
1277 if (nd->intent.open.flags & FMODE_EXEC) {
1278 ret = _nfs4_do_access(state->inode,
1279 state->owner->so_cred,
1280 nd->intent.open.flags);
1281 if (ret < 0)
1282 goto out_close;
1283 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001284 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001285 if (!IS_ERR(filp)) {
1286 struct nfs_open_context *ctx;
1287 ctx = (struct nfs_open_context *)filp->private_data;
1288 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001289 return 0;
1290 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001291 ret = PTR_ERR(filp);
1292out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001293 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001294 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001295}
1296
1297struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1299{
Trond Myklebustad389da2007-06-05 12:30:00 -04001300 struct path path = {
1301 .mnt = nd->mnt,
1302 .dentry = dentry,
1303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct iattr attr;
1305 struct rpc_cred *cred;
1306 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001307 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (nd->flags & LOOKUP_CREATE) {
1310 attr.ia_mode = nd->intent.open.create_mode;
1311 attr.ia_valid = ATTR_MODE;
1312 if (!IS_POSIXACL(dir))
1313 attr.ia_mode &= ~current->fs->umask;
1314 } else {
1315 attr.ia_valid = 0;
1316 BUG_ON(nd->intent.open.flags & O_CREAT);
1317 }
1318
David Howells1f163412006-08-22 20:06:11 -04001319 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001321 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001322 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001324 if (IS_ERR(state)) {
1325 if (PTR_ERR(state) == -ENOENT)
1326 d_add(dentry, NULL);
1327 return (struct dentry *)state;
1328 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001329 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001330 if (res != NULL)
1331 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001332 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001333 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001337nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebustad389da2007-06-05 12:30:00 -04001339 struct path path = {
1340 .mnt = nd->mnt,
1341 .dentry = dentry,
1342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 struct rpc_cred *cred;
1344 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
David Howells1f163412006-08-22 20:06:11 -04001346 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (IS_ERR(cred))
1348 return PTR_ERR(cred);
1349 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1350 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001351 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001353 if (IS_ERR(state)) {
1354 switch (PTR_ERR(state)) {
1355 case -EPERM:
1356 case -EACCES:
1357 case -EDQUOT:
1358 case -ENOSPC:
1359 case -EROFS:
1360 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1361 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001362 default:
1363 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001364 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001365 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001367 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 1;
1369 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001370 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001371out_drop:
1372 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
1376
1377static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1378{
1379 struct nfs4_server_caps_res res = {};
1380 struct rpc_message msg = {
1381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1382 .rpc_argp = fhandle,
1383 .rpc_resp = &res,
1384 };
1385 int status;
1386
1387 status = rpc_call_sync(server->client, &msg, 0);
1388 if (status == 0) {
1389 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1390 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1391 server->caps |= NFS_CAP_ACLS;
1392 if (res.has_links != 0)
1393 server->caps |= NFS_CAP_HARDLINKS;
1394 if (res.has_symlinks != 0)
1395 server->caps |= NFS_CAP_SYMLINKS;
1396 server->acl_bitmask = res.acl_bitmask;
1397 }
1398 return status;
1399}
1400
Trond Myklebust55a97592006-06-09 09:34:19 -04001401int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct nfs4_exception exception = { };
1404 int err;
1405 do {
1406 err = nfs4_handle_exception(server,
1407 _nfs4_server_capabilities(server, fhandle),
1408 &exception);
1409 } while (exception.retry);
1410 return err;
1411}
1412
1413static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1414 struct nfs_fsinfo *info)
1415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct nfs4_lookup_root_arg args = {
1417 .bitmask = nfs4_fattr_bitmap,
1418 };
1419 struct nfs4_lookup_res res = {
1420 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001421 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .fh = fhandle,
1423 };
1424 struct rpc_message msg = {
1425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1426 .rpc_argp = &args,
1427 .rpc_resp = &res,
1428 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001429 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return rpc_call_sync(server->client, &msg, 0);
1431}
1432
1433static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1434 struct nfs_fsinfo *info)
1435{
1436 struct nfs4_exception exception = { };
1437 int err;
1438 do {
1439 err = nfs4_handle_exception(server,
1440 _nfs4_lookup_root(server, fhandle, info),
1441 &exception);
1442 } while (exception.retry);
1443 return err;
1444}
1445
David Howells54ceac42006-08-22 20:06:13 -04001446/*
1447 * get the file handle for the "/" directory on the server
1448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001450 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int status;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (status == 0)
1456 status = nfs4_server_capabilities(server, fhandle);
1457 if (status == 0)
1458 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001459 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Manoj Naik6b97fd32006-06-09 09:34:29 -04001462/*
1463 * Get locations and (maybe) other attributes of a referral.
1464 * Note that we'll actually follow the referral later when
1465 * we detect fsid mismatch in inode revalidation
1466 */
1467static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1468{
1469 int status = -ENOMEM;
1470 struct page *page = NULL;
1471 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001472
1473 page = alloc_page(GFP_KERNEL);
1474 if (page == NULL)
1475 goto out;
1476 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1477 if (locations == NULL)
1478 goto out;
1479
Trond Myklebustc228fd32007-01-13 02:28:11 -05001480 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001481 if (status != 0)
1482 goto out;
1483 /* Make sure server returned a different fsid for the referral */
1484 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1485 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1486 status = -EIO;
1487 goto out;
1488 }
1489
1490 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1491 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1492 if (!fattr->mode)
1493 fattr->mode = S_IFDIR;
1494 memset(fhandle, 0, sizeof(struct nfs_fh));
1495out:
1496 if (page)
1497 __free_page(page);
1498 if (locations)
1499 kfree(locations);
1500 return status;
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1504{
1505 struct nfs4_getattr_arg args = {
1506 .fh = fhandle,
1507 .bitmask = server->attr_bitmask,
1508 };
1509 struct nfs4_getattr_res res = {
1510 .fattr = fattr,
1511 .server = server,
1512 };
1513 struct rpc_message msg = {
1514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1515 .rpc_argp = &args,
1516 .rpc_resp = &res,
1517 };
1518
Trond Myklebust0e574af2005-10-27 22:12:38 -04001519 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return rpc_call_sync(server->client, &msg, 0);
1521}
1522
1523static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1524{
1525 struct nfs4_exception exception = { };
1526 int err;
1527 do {
1528 err = nfs4_handle_exception(server,
1529 _nfs4_proc_getattr(server, fhandle, fattr),
1530 &exception);
1531 } while (exception.retry);
1532 return err;
1533}
1534
1535/*
1536 * The file is not closed if it is opened due to the a request to change
1537 * the size of the file. The open call will not be needed once the
1538 * VFS layer lookup-intents are implemented.
1539 *
1540 * Close is called when the inode is destroyed.
1541 * If we haven't opened the file for O_WRONLY, we
1542 * need to in the size_change case to obtain a stateid.
1543 *
1544 * Got race?
1545 * Because OPEN is always done by name in nfsv4, it is
1546 * possible that we opened a different file by the same
1547 * name. We can recognize this race condition, but we
1548 * can't do anything about it besides returning an error.
1549 *
1550 * This will be fixed with VFS changes (lookup-intent).
1551 */
1552static int
1553nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1554 struct iattr *sattr)
1555{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001556 struct rpc_cred *cred;
1557 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001558 struct nfs_open_context *ctx;
1559 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int status;
1561
Trond Myklebust0e574af2005-10-27 22:12:38 -04001562 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Howells1f163412006-08-22 20:06:11 -04001564 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001565 if (IS_ERR(cred))
1566 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001567
1568 /* Search for an existing open(O_WRITE) file */
1569 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1570 if (ctx != NULL)
1571 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001572
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001573 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001574 if (status == 0)
1575 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001576 if (ctx != NULL)
1577 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001578 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return status;
1580}
1581
David Howells2b3de442006-08-22 20:06:09 -04001582static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1583 struct qstr *name, struct nfs_fh *fhandle,
1584 struct nfs_fattr *fattr)
1585{
1586 int status;
1587 struct nfs4_lookup_arg args = {
1588 .bitmask = server->attr_bitmask,
1589 .dir_fh = dirfh,
1590 .name = name,
1591 };
1592 struct nfs4_lookup_res res = {
1593 .server = server,
1594 .fattr = fattr,
1595 .fh = fhandle,
1596 };
1597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1599 .rpc_argp = &args,
1600 .rpc_resp = &res,
1601 };
1602
1603 nfs_fattr_init(fattr);
1604
1605 dprintk("NFS call lookupfh %s\n", name->name);
1606 status = rpc_call_sync(server->client, &msg, 0);
1607 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001608 return status;
1609}
1610
1611static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1612 struct qstr *name, struct nfs_fh *fhandle,
1613 struct nfs_fattr *fattr)
1614{
1615 struct nfs4_exception exception = { };
1616 int err;
1617 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001618 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1619 /* FIXME: !!!! */
1620 if (err == -NFS4ERR_MOVED) {
1621 err = -EREMOTE;
1622 break;
1623 }
1624 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001625 } while (exception.retry);
1626 return err;
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1630 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1631{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001632 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001635 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001636 if (status == -NFS4ERR_MOVED)
1637 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 dprintk("NFS reply lookup: %d\n", status);
1639 return status;
1640}
1641
1642static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1643{
1644 struct nfs4_exception exception = { };
1645 int err;
1646 do {
1647 err = nfs4_handle_exception(NFS_SERVER(dir),
1648 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1649 &exception);
1650 } while (exception.retry);
1651 return err;
1652}
1653
1654static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1655{
1656 struct nfs4_accessargs args = {
1657 .fh = NFS_FH(inode),
1658 };
1659 struct nfs4_accessres res = { 0 };
1660 struct rpc_message msg = {
1661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1662 .rpc_argp = &args,
1663 .rpc_resp = &res,
1664 .rpc_cred = entry->cred,
1665 };
1666 int mode = entry->mask;
1667 int status;
1668
1669 /*
1670 * Determine which access bits we want to ask for...
1671 */
1672 if (mode & MAY_READ)
1673 args.access |= NFS4_ACCESS_READ;
1674 if (S_ISDIR(inode->i_mode)) {
1675 if (mode & MAY_WRITE)
1676 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1677 if (mode & MAY_EXEC)
1678 args.access |= NFS4_ACCESS_LOOKUP;
1679 } else {
1680 if (mode & MAY_WRITE)
1681 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1682 if (mode & MAY_EXEC)
1683 args.access |= NFS4_ACCESS_EXECUTE;
1684 }
1685 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1686 if (!status) {
1687 entry->mask = 0;
1688 if (res.access & NFS4_ACCESS_READ)
1689 entry->mask |= MAY_READ;
1690 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1691 entry->mask |= MAY_WRITE;
1692 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1693 entry->mask |= MAY_EXEC;
1694 }
1695 return status;
1696}
1697
1698static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1699{
1700 struct nfs4_exception exception = { };
1701 int err;
1702 do {
1703 err = nfs4_handle_exception(NFS_SERVER(inode),
1704 _nfs4_proc_access(inode, entry),
1705 &exception);
1706 } while (exception.retry);
1707 return err;
1708}
1709
1710/*
1711 * TODO: For the time being, we don't try to get any attributes
1712 * along with any of the zero-copy operations READ, READDIR,
1713 * READLINK, WRITE.
1714 *
1715 * In the case of the first three, we want to put the GETATTR
1716 * after the read-type operation -- this is because it is hard
1717 * to predict the length of a GETATTR response in v4, and thus
1718 * align the READ data correctly. This means that the GETATTR
1719 * may end up partially falling into the page cache, and we should
1720 * shift it into the 'tail' of the xdr_buf before processing.
1721 * To do this efficiently, we need to know the total length
1722 * of data received, which doesn't seem to be available outside
1723 * of the RPC layer.
1724 *
1725 * In the case of WRITE, we also want to put the GETATTR after
1726 * the operation -- in this case because we want to make sure
1727 * we get the post-operation mtime and size. This means that
1728 * we can't use xdr_encode_pages() as written: we need a variant
1729 * of it which would leave room in the 'tail' iovec.
1730 *
1731 * Both of these changes to the XDR layer would in fact be quite
1732 * minor, but I decided to leave them for a subsequent patch.
1733 */
1734static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1735 unsigned int pgbase, unsigned int pglen)
1736{
1737 struct nfs4_readlink args = {
1738 .fh = NFS_FH(inode),
1739 .pgbase = pgbase,
1740 .pglen = pglen,
1741 .pages = &page,
1742 };
1743 struct rpc_message msg = {
1744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1745 .rpc_argp = &args,
1746 .rpc_resp = NULL,
1747 };
1748
1749 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1750}
1751
1752static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1753 unsigned int pgbase, unsigned int pglen)
1754{
1755 struct nfs4_exception exception = { };
1756 int err;
1757 do {
1758 err = nfs4_handle_exception(NFS_SERVER(inode),
1759 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1760 &exception);
1761 } while (exception.retry);
1762 return err;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/*
1766 * Got race?
1767 * We will need to arrange for the VFS layer to provide an atomic open.
1768 * Until then, this create/open method is prone to inefficiency and race
1769 * conditions due to the lookup, create, and open VFS calls from sys_open()
1770 * placed on the wire.
1771 *
1772 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1773 * The file will be opened again in the subsequent VFS open call
1774 * (nfs4_proc_file_open).
1775 *
1776 * The open for read will just hang around to be used by any process that
1777 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1778 */
1779
1780static int
1781nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001782 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebustad389da2007-06-05 12:30:00 -04001784 struct path path = {
1785 .mnt = nd->mnt,
1786 .dentry = dentry,
1787 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct nfs4_state *state;
1789 struct rpc_cred *cred;
1790 int status = 0;
1791
David Howells1f163412006-08-22 20:06:11 -04001792 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (IS_ERR(cred)) {
1794 status = PTR_ERR(cred);
1795 goto out;
1796 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001797 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 put_rpccred(cred);
1799 if (IS_ERR(state)) {
1800 status = PTR_ERR(state);
1801 goto out;
1802 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001803 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (flags & O_EXCL) {
1805 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001806 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001807 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001808 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001809 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001810 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001811 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001812 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001813 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001814 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815out:
1816 return status;
1817}
1818
1819static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1820{
Trond Myklebust16e42952005-10-27 22:12:44 -04001821 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct nfs4_remove_arg args = {
1823 .fh = NFS_FH(dir),
1824 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001825 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001827 struct nfs_fattr dir_attr;
1828 struct nfs4_remove_res res = {
1829 .server = server,
1830 .dir_attr = &dir_attr,
1831 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct rpc_message msg = {
1833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1834 .rpc_argp = &args,
1835 .rpc_resp = &res,
1836 };
1837 int status;
1838
Trond Myklebust16e42952005-10-27 22:12:44 -04001839 nfs_fattr_init(res.dir_attr);
1840 status = rpc_call_sync(server->client, &msg, 0);
1841 if (status == 0) {
1842 update_changeattr(dir, &res.cinfo);
1843 nfs_post_op_update_inode(dir, res.dir_attr);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return status;
1846}
1847
1848static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1849{
1850 struct nfs4_exception exception = { };
1851 int err;
1852 do {
1853 err = nfs4_handle_exception(NFS_SERVER(dir),
1854 _nfs4_proc_remove(dir, name),
1855 &exception);
1856 } while (exception.retry);
1857 return err;
1858}
1859
1860struct unlink_desc {
1861 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001862 struct nfs4_remove_res res;
1863 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864};
1865
1866static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1867 struct qstr *name)
1868{
Trond Myklebust16e42952005-10-27 22:12:44 -04001869 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct unlink_desc *up;
1871
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001872 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (!up)
1874 return -ENOMEM;
1875
1876 up->args.fh = NFS_FH(dir->d_inode);
1877 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001878 up->args.bitmask = server->attr_bitmask;
1879 up->res.server = server;
1880 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1883 msg->rpc_argp = &up->args;
1884 msg->rpc_resp = &up->res;
1885 return 0;
1886}
1887
1888static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1889{
1890 struct rpc_message *msg = &task->tk_msg;
1891 struct unlink_desc *up;
1892
1893 if (msg->rpc_resp != NULL) {
1894 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001895 update_changeattr(dir->d_inode, &up->res.cinfo);
1896 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 kfree(up);
1898 msg->rpc_resp = NULL;
1899 msg->rpc_argp = NULL;
1900 }
1901 return 0;
1902}
1903
1904static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1905 struct inode *new_dir, struct qstr *new_name)
1906{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001907 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct nfs4_rename_arg arg = {
1909 .old_dir = NFS_FH(old_dir),
1910 .new_dir = NFS_FH(new_dir),
1911 .old_name = old_name,
1912 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001913 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001915 struct nfs_fattr old_fattr, new_fattr;
1916 struct nfs4_rename_res res = {
1917 .server = server,
1918 .old_fattr = &old_fattr,
1919 .new_fattr = &new_fattr,
1920 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1923 .rpc_argp = &arg,
1924 .rpc_resp = &res,
1925 };
1926 int status;
1927
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001928 nfs_fattr_init(res.old_fattr);
1929 nfs_fattr_init(res.new_fattr);
1930 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 if (!status) {
1933 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001934 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001936 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 return status;
1939}
1940
1941static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1942 struct inode *new_dir, struct qstr *new_name)
1943{
1944 struct nfs4_exception exception = { };
1945 int err;
1946 do {
1947 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1948 _nfs4_proc_rename(old_dir, old_name,
1949 new_dir, new_name),
1950 &exception);
1951 } while (exception.retry);
1952 return err;
1953}
1954
1955static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1956{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001957 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct nfs4_link_arg arg = {
1959 .fh = NFS_FH(inode),
1960 .dir_fh = NFS_FH(dir),
1961 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001962 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001964 struct nfs_fattr fattr, dir_attr;
1965 struct nfs4_link_res res = {
1966 .server = server,
1967 .fattr = &fattr,
1968 .dir_attr = &dir_attr,
1969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct rpc_message msg = {
1971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1972 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001973 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 };
1975 int status;
1976
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001977 nfs_fattr_init(res.fattr);
1978 nfs_fattr_init(res.dir_attr);
1979 status = rpc_call_sync(server->client, &msg, 0);
1980 if (!status) {
1981 update_changeattr(dir, &res.cinfo);
1982 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04001983 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 return status;
1987}
1988
1989static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1990{
1991 struct nfs4_exception exception = { };
1992 int err;
1993 do {
1994 err = nfs4_handle_exception(NFS_SERVER(inode),
1995 _nfs4_proc_link(inode, dir, name),
1996 &exception);
1997 } while (exception.retry);
1998 return err;
1999}
2000
Chuck Lever4f390c12006-08-22 20:06:22 -04002001static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002002 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002005 struct nfs_fh fhandle;
2006 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs4_create_arg arg = {
2008 .dir_fh = NFS_FH(dir),
2009 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002010 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 .attrs = sattr,
2012 .ftype = NF4LNK,
2013 .bitmask = server->attr_bitmask,
2014 };
2015 struct nfs4_create_res res = {
2016 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002017 .fh = &fhandle,
2018 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002019 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 };
2021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2023 .rpc_argp = &arg,
2024 .rpc_resp = &res,
2025 };
2026 int status;
2027
Chuck Lever94a6d752006-08-22 20:06:23 -04002028 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002030
Chuck Lever94a6d752006-08-22 20:06:23 -04002031 arg.u.symlink.pages = &page;
2032 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002033 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002034 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002037 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002039 nfs_post_op_update_inode(dir, res.dir_fattr);
2040 status = nfs_instantiate(dentry, &fhandle, &fattr);
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return status;
2043}
2044
Chuck Lever4f390c12006-08-22 20:06:22 -04002045static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002046 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct nfs4_exception exception = { };
2049 int err;
2050 do {
2051 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002052 _nfs4_proc_symlink(dir, dentry, page,
2053 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 &exception);
2055 } while (exception.retry);
2056 return err;
2057}
2058
2059static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2060 struct iattr *sattr)
2061{
2062 struct nfs_server *server = NFS_SERVER(dir);
2063 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002064 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct nfs4_create_arg arg = {
2066 .dir_fh = NFS_FH(dir),
2067 .server = server,
2068 .name = &dentry->d_name,
2069 .attrs = sattr,
2070 .ftype = NF4DIR,
2071 .bitmask = server->attr_bitmask,
2072 };
2073 struct nfs4_create_res res = {
2074 .server = server,
2075 .fh = &fhandle,
2076 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002077 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 };
2079 struct rpc_message msg = {
2080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2081 .rpc_argp = &arg,
2082 .rpc_resp = &res,
2083 };
2084 int status;
2085
Trond Myklebust0e574af2005-10-27 22:12:38 -04002086 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002087 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2090 if (!status) {
2091 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002092 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 status = nfs_instantiate(dentry, &fhandle, &fattr);
2094 }
2095 return status;
2096}
2097
2098static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2099 struct iattr *sattr)
2100{
2101 struct nfs4_exception exception = { };
2102 int err;
2103 do {
2104 err = nfs4_handle_exception(NFS_SERVER(dir),
2105 _nfs4_proc_mkdir(dir, dentry, sattr),
2106 &exception);
2107 } while (exception.retry);
2108 return err;
2109}
2110
2111static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2112 u64 cookie, struct page *page, unsigned int count, int plus)
2113{
2114 struct inode *dir = dentry->d_inode;
2115 struct nfs4_readdir_arg args = {
2116 .fh = NFS_FH(dir),
2117 .pages = &page,
2118 .pgbase = 0,
2119 .count = count,
2120 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2121 };
2122 struct nfs4_readdir_res res;
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2125 .rpc_argp = &args,
2126 .rpc_resp = &res,
2127 .rpc_cred = cred,
2128 };
2129 int status;
2130
Trond Myklebusteadf4592005-06-22 17:16:39 +00002131 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2132 dentry->d_parent->d_name.name,
2133 dentry->d_name.name,
2134 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2136 res.pgbase = args.pgbase;
2137 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2138 if (status == 0)
2139 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002140 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return status;
2142}
2143
2144static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2145 u64 cookie, struct page *page, unsigned int count, int plus)
2146{
2147 struct nfs4_exception exception = { };
2148 int err;
2149 do {
2150 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2151 _nfs4_proc_readdir(dentry, cred, cookie,
2152 page, count, plus),
2153 &exception);
2154 } while (exception.retry);
2155 return err;
2156}
2157
2158static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2159 struct iattr *sattr, dev_t rdev)
2160{
2161 struct nfs_server *server = NFS_SERVER(dir);
2162 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002163 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct nfs4_create_arg arg = {
2165 .dir_fh = NFS_FH(dir),
2166 .server = server,
2167 .name = &dentry->d_name,
2168 .attrs = sattr,
2169 .bitmask = server->attr_bitmask,
2170 };
2171 struct nfs4_create_res res = {
2172 .server = server,
2173 .fh = &fh,
2174 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002175 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 };
2177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2179 .rpc_argp = &arg,
2180 .rpc_resp = &res,
2181 };
2182 int status;
2183 int mode = sattr->ia_mode;
2184
Trond Myklebust0e574af2005-10-27 22:12:38 -04002185 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002186 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2189 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2190 if (S_ISFIFO(mode))
2191 arg.ftype = NF4FIFO;
2192 else if (S_ISBLK(mode)) {
2193 arg.ftype = NF4BLK;
2194 arg.u.device.specdata1 = MAJOR(rdev);
2195 arg.u.device.specdata2 = MINOR(rdev);
2196 }
2197 else if (S_ISCHR(mode)) {
2198 arg.ftype = NF4CHR;
2199 arg.u.device.specdata1 = MAJOR(rdev);
2200 arg.u.device.specdata2 = MINOR(rdev);
2201 }
2202 else
2203 arg.ftype = NF4SOCK;
2204
2205 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2206 if (status == 0) {
2207 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002208 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 status = nfs_instantiate(dentry, &fh, &fattr);
2210 }
2211 return status;
2212}
2213
2214static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2215 struct iattr *sattr, dev_t rdev)
2216{
2217 struct nfs4_exception exception = { };
2218 int err;
2219 do {
2220 err = nfs4_handle_exception(NFS_SERVER(dir),
2221 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2222 &exception);
2223 } while (exception.retry);
2224 return err;
2225}
2226
2227static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2228 struct nfs_fsstat *fsstat)
2229{
2230 struct nfs4_statfs_arg args = {
2231 .fh = fhandle,
2232 .bitmask = server->attr_bitmask,
2233 };
2234 struct rpc_message msg = {
2235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2236 .rpc_argp = &args,
2237 .rpc_resp = fsstat,
2238 };
2239
Trond Myklebust0e574af2005-10-27 22:12:38 -04002240 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return rpc_call_sync(server->client, &msg, 0);
2242}
2243
2244static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2245{
2246 struct nfs4_exception exception = { };
2247 int err;
2248 do {
2249 err = nfs4_handle_exception(server,
2250 _nfs4_proc_statfs(server, fhandle, fsstat),
2251 &exception);
2252 } while (exception.retry);
2253 return err;
2254}
2255
2256static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2257 struct nfs_fsinfo *fsinfo)
2258{
2259 struct nfs4_fsinfo_arg args = {
2260 .fh = fhandle,
2261 .bitmask = server->attr_bitmask,
2262 };
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2265 .rpc_argp = &args,
2266 .rpc_resp = fsinfo,
2267 };
2268
2269 return rpc_call_sync(server->client, &msg, 0);
2270}
2271
2272static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2273{
2274 struct nfs4_exception exception = { };
2275 int err;
2276
2277 do {
2278 err = nfs4_handle_exception(server,
2279 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2280 &exception);
2281 } while (exception.retry);
2282 return err;
2283}
2284
2285static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2286{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002287 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2289}
2290
2291static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2292 struct nfs_pathconf *pathconf)
2293{
2294 struct nfs4_pathconf_arg args = {
2295 .fh = fhandle,
2296 .bitmask = server->attr_bitmask,
2297 };
2298 struct rpc_message msg = {
2299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2300 .rpc_argp = &args,
2301 .rpc_resp = pathconf,
2302 };
2303
2304 /* None of the pathconf attributes are mandatory to implement */
2305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2306 memset(pathconf, 0, sizeof(*pathconf));
2307 return 0;
2308 }
2309
Trond Myklebust0e574af2005-10-27 22:12:38 -04002310 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return rpc_call_sync(server->client, &msg, 0);
2312}
2313
2314static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2315 struct nfs_pathconf *pathconf)
2316{
2317 struct nfs4_exception exception = { };
2318 int err;
2319
2320 do {
2321 err = nfs4_handle_exception(server,
2322 _nfs4_proc_pathconf(server, fhandle, pathconf),
2323 &exception);
2324 } while (exception.retry);
2325 return err;
2326}
2327
Trond Myklebustec06c092006-03-20 13:44:27 -05002328static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Trond Myklebustec06c092006-03-20 13:44:27 -05002330 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Trond Myklebustec06c092006-03-20 13:44:27 -05002332 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002334 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002337 renew_lease(server, data->timestamp);
2338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Trond Myklebustec06c092006-03-20 13:44:27 -05002341static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 struct rpc_message msg = {
2344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2345 .rpc_argp = &data->args,
2346 .rpc_resp = &data->res,
2347 .rpc_cred = data->cred,
2348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 data->timestamp = jiffies;
2351
Trond Myklebustec06c092006-03-20 13:44:27 -05002352 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Trond Myklebust788e7a82006-03-20 13:44:27 -05002355static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct inode *inode = data->inode;
2358
2359 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2360 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002361 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002363 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002365 nfs_post_op_update_inode(inode, data->res.fattr);
2366 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Trond Myklebust788e7a82006-03-20 13:44:27 -05002370static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct rpc_message msg = {
2373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2374 .rpc_argp = &data->args,
2375 .rpc_resp = &data->res,
2376 .rpc_cred = data->cred,
2377 };
2378 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002379 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (how & FLUSH_STABLE) {
2383 if (!NFS_I(inode)->ncommit)
2384 stable = NFS_FILE_SYNC;
2385 else
2386 stable = NFS_DATA_SYNC;
2387 } else
2388 stable = NFS_UNSTABLE;
2389 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002390 data->args.bitmask = server->attr_bitmask;
2391 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 data->timestamp = jiffies;
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002396 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Trond Myklebust788e7a82006-03-20 13:44:27 -05002399static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct inode *inode = data->inode;
2402
2403 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2404 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002405 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002407 if (task->tk_status >= 0)
2408 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebust788e7a82006-03-20 13:44:27 -05002412static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct rpc_message msg = {
2415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2416 .rpc_argp = &data->args,
2417 .rpc_resp = &data->res,
2418 .rpc_cred = data->cred,
2419 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002420 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002422 data->args.bitmask = server->attr_bitmask;
2423 data->res.server = server;
2424
Trond Myklebust788e7a82006-03-20 13:44:27 -05002425 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428/*
2429 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2430 * standalone procedure for queueing an asynchronous RENEW.
2431 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002432static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
David Howellsadfa6f92006-08-22 20:06:08 -04002434 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002435 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (task->tk_status < 0) {
2438 switch (task->tk_status) {
2439 case -NFS4ERR_STALE_CLIENTID:
2440 case -NFS4ERR_EXPIRED:
2441 case -NFS4ERR_CB_PATH_DOWN:
2442 nfs4_schedule_state_recovery(clp);
2443 }
2444 return;
2445 }
2446 spin_lock(&clp->cl_lock);
2447 if (time_before(clp->cl_last_renewal,timestamp))
2448 clp->cl_last_renewal = timestamp;
2449 spin_unlock(&clp->cl_lock);
2450}
2451
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002452static const struct rpc_call_ops nfs4_renew_ops = {
2453 .rpc_call_done = nfs4_renew_done,
2454};
2455
David Howellsadfa6f92006-08-22 20:06:08 -04002456int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 struct rpc_message msg = {
2459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2460 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002461 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 };
2463
2464 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002465 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
David Howellsadfa6f92006-08-22 20:06:08 -04002468int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 struct rpc_message msg = {
2471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2472 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002473 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 };
2475 unsigned long now = jiffies;
2476 int status;
2477
2478 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2479 if (status < 0)
2480 return status;
2481 spin_lock(&clp->cl_lock);
2482 if (time_before(clp->cl_last_renewal,now))
2483 clp->cl_last_renewal = now;
2484 spin_unlock(&clp->cl_lock);
2485 return 0;
2486}
2487
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002488static inline int nfs4_server_supports_acls(struct nfs_server *server)
2489{
2490 return (server->caps & NFS_CAP_ACLS)
2491 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2492 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2493}
2494
2495/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2496 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2497 * the stack.
2498 */
2499#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2500
2501static void buf_to_pages(const void *buf, size_t buflen,
2502 struct page **pages, unsigned int *pgbase)
2503{
2504 const void *p = buf;
2505
2506 *pgbase = offset_in_page(buf);
2507 p -= *pgbase;
2508 while (p < buf + buflen) {
2509 *(pages++) = virt_to_page(p);
2510 p += PAGE_CACHE_SIZE;
2511 }
2512}
2513
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002514struct nfs4_cached_acl {
2515 int cached;
2516 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002517 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002518};
2519
2520static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002521{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002522 struct nfs_inode *nfsi = NFS_I(inode);
2523
2524 spin_lock(&inode->i_lock);
2525 kfree(nfsi->nfs4_acl);
2526 nfsi->nfs4_acl = acl;
2527 spin_unlock(&inode->i_lock);
2528}
2529
2530static void nfs4_zap_acl_attr(struct inode *inode)
2531{
2532 nfs4_set_cached_acl(inode, NULL);
2533}
2534
2535static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2536{
2537 struct nfs_inode *nfsi = NFS_I(inode);
2538 struct nfs4_cached_acl *acl;
2539 int ret = -ENOENT;
2540
2541 spin_lock(&inode->i_lock);
2542 acl = nfsi->nfs4_acl;
2543 if (acl == NULL)
2544 goto out;
2545 if (buf == NULL) /* user is just asking for length */
2546 goto out_len;
2547 if (acl->cached == 0)
2548 goto out;
2549 ret = -ERANGE; /* see getxattr(2) man page */
2550 if (acl->len > buflen)
2551 goto out;
2552 memcpy(buf, acl->data, acl->len);
2553out_len:
2554 ret = acl->len;
2555out:
2556 spin_unlock(&inode->i_lock);
2557 return ret;
2558}
2559
2560static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2561{
2562 struct nfs4_cached_acl *acl;
2563
2564 if (buf && acl_len <= PAGE_SIZE) {
2565 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2566 if (acl == NULL)
2567 goto out;
2568 acl->cached = 1;
2569 memcpy(acl->data, buf, acl_len);
2570 } else {
2571 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2572 if (acl == NULL)
2573 goto out;
2574 acl->cached = 0;
2575 }
2576 acl->len = acl_len;
2577out:
2578 nfs4_set_cached_acl(inode, acl);
2579}
2580
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002581static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002582{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002583 struct page *pages[NFS4ACL_MAXPAGES];
2584 struct nfs_getaclargs args = {
2585 .fh = NFS_FH(inode),
2586 .acl_pages = pages,
2587 .acl_len = buflen,
2588 };
2589 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002590 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002591 struct rpc_message msg = {
2592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2593 .rpc_argp = &args,
2594 .rpc_resp = &resp_len,
2595 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002596 struct page *localpage = NULL;
2597 int ret;
2598
2599 if (buflen < PAGE_SIZE) {
2600 /* As long as we're doing a round trip to the server anyway,
2601 * let's be prepared for a page of acl data. */
2602 localpage = alloc_page(GFP_KERNEL);
2603 resp_buf = page_address(localpage);
2604 if (localpage == NULL)
2605 return -ENOMEM;
2606 args.acl_pages[0] = localpage;
2607 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002608 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002609 } else {
2610 resp_buf = buf;
2611 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2612 }
2613 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2614 if (ret)
2615 goto out_free;
2616 if (resp_len > args.acl_len)
2617 nfs4_write_cached_acl(inode, NULL, resp_len);
2618 else
2619 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2620 if (buf) {
2621 ret = -ERANGE;
2622 if (resp_len > buflen)
2623 goto out_free;
2624 if (localpage)
2625 memcpy(buf, resp_buf, resp_len);
2626 }
2627 ret = resp_len;
2628out_free:
2629 if (localpage)
2630 __free_page(localpage);
2631 return ret;
2632}
2633
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002634static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2635{
2636 struct nfs4_exception exception = { };
2637 ssize_t ret;
2638 do {
2639 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2640 if (ret >= 0)
2641 break;
2642 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2643 } while (exception.retry);
2644 return ret;
2645}
2646
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002647static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2648{
2649 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002650 int ret;
2651
2652 if (!nfs4_server_supports_acls(server))
2653 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002654 ret = nfs_revalidate_inode(server, inode);
2655 if (ret < 0)
2656 return ret;
2657 ret = nfs4_read_cached_acl(inode, buf, buflen);
2658 if (ret != -ENOENT)
2659 return ret;
2660 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002661}
2662
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002663static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002664{
2665 struct nfs_server *server = NFS_SERVER(inode);
2666 struct page *pages[NFS4ACL_MAXPAGES];
2667 struct nfs_setaclargs arg = {
2668 .fh = NFS_FH(inode),
2669 .acl_pages = pages,
2670 .acl_len = buflen,
2671 };
2672 struct rpc_message msg = {
2673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2674 .rpc_argp = &arg,
2675 .rpc_resp = NULL,
2676 };
2677 int ret;
2678
2679 if (!nfs4_server_supports_acls(server))
2680 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002681 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002682 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002683 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002684 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002685 return ret;
2686}
2687
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002688static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2689{
2690 struct nfs4_exception exception = { };
2691 int err;
2692 do {
2693 err = nfs4_handle_exception(NFS_SERVER(inode),
2694 __nfs4_proc_set_acl(inode, buf, buflen),
2695 &exception);
2696 } while (exception.retry);
2697 return err;
2698}
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002701nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
David Howells7539bba2006-08-22 20:06:09 -04002703 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 if (!clp || task->tk_status >= 0)
2706 return 0;
2707 switch(task->tk_status) {
2708 case -NFS4ERR_STALE_CLIENTID:
2709 case -NFS4ERR_STALE_STATEID:
2710 case -NFS4ERR_EXPIRED:
2711 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2712 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002713 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 rpc_wake_up_task(task);
2715 task->tk_status = 0;
2716 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002718 nfs_inc_server_stats((struct nfs_server *) server,
2719 NFSIOS_DELAY);
2720 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2722 task->tk_status = 0;
2723 return -EAGAIN;
2724 case -NFS4ERR_OLD_STATEID:
2725 task->tk_status = 0;
2726 return -EAGAIN;
2727 }
2728 task->tk_status = nfs4_map_errors(task->tk_status);
2729 return 0;
2730}
2731
Trond Myklebust433fbe42006-01-03 09:55:22 +01002732static int nfs4_wait_bit_interruptible(void *word)
2733{
2734 if (signal_pending(current))
2735 return -ERESTARTSYS;
2736 schedule();
2737 return 0;
2738}
2739
David Howellsadfa6f92006-08-22 20:06:08 -04002740static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002743 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 might_sleep();
2746
Trond Myklebuste1485822006-12-13 16:43:13 -05002747 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002750 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2751 nfs4_wait_bit_interruptible,
2752 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002754
2755 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return res;
2757}
2758
2759static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2760{
2761 sigset_t oldset;
2762 int res = 0;
2763
2764 might_sleep();
2765
2766 if (*timeout <= 0)
2767 *timeout = NFS4_POLL_RETRY_MIN;
2768 if (*timeout > NFS4_POLL_RETRY_MAX)
2769 *timeout = NFS4_POLL_RETRY_MAX;
2770 rpc_clnt_sigmask(clnt, &oldset);
2771 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002772 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (signalled())
2774 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002775 } else
2776 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 rpc_clnt_sigunmask(clnt, &oldset);
2778 *timeout <<= 1;
2779 return res;
2780}
2781
2782/* This is the error handling routine for processes that are allowed
2783 * to sleep.
2784 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002785static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
David Howells7539bba2006-08-22 20:06:09 -04002787 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 int ret = errorcode;
2789
2790 exception->retry = 0;
2791 switch(errorcode) {
2792 case 0:
2793 return 0;
2794 case -NFS4ERR_STALE_CLIENTID:
2795 case -NFS4ERR_STALE_STATEID:
2796 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002797 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 ret = nfs4_wait_clnt_recover(server->client, clp);
2799 if (ret == 0)
2800 exception->retry = 1;
2801 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002802 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 case -NFS4ERR_GRACE:
2804 case -NFS4ERR_DELAY:
2805 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002806 if (ret != 0)
2807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002809 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811 /* We failed to handle the error */
2812 return nfs4_map_errors(ret);
2813}
2814
David Howellsadfa6f92006-08-22 20:06:08 -04002815int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 nfs4_verifier sc_verifier;
2818 struct nfs4_setclientid setclientid = {
2819 .sc_verifier = &sc_verifier,
2820 .sc_prog = program,
2821 };
2822 struct rpc_message msg = {
2823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2824 .rpc_argp = &setclientid,
2825 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002826 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 };
Al Virobc4785c2006-10-19 23:28:51 -07002828 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 int loop = 0;
2830 int status;
2831
Al Virobc4785c2006-10-19 23:28:51 -07002832 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2834 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2835
2836 for(;;) {
2837 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2838 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002839 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002840 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 clp->cl_id_uniquifier);
2842 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2843 sizeof(setclientid.sc_netid), "tcp");
2844 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2845 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2846 clp->cl_ipaddr, port >> 8, port & 255);
2847
2848 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2849 if (status != -NFS4ERR_CLID_INUSE)
2850 break;
2851 if (signalled())
2852 break;
2853 if (loop++ & 1)
2854 ssleep(clp->cl_lease_time + 1);
2855 else
2856 if (++clp->cl_id_uniquifier == 0)
2857 break;
2858 }
2859 return status;
2860}
2861
David Howellsadfa6f92006-08-22 20:06:08 -04002862static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct nfs_fsinfo fsinfo;
2865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2867 .rpc_argp = clp,
2868 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002869 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 };
2871 unsigned long now;
2872 int status;
2873
2874 now = jiffies;
2875 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2876 if (status == 0) {
2877 spin_lock(&clp->cl_lock);
2878 clp->cl_lease_time = fsinfo.lease_time * HZ;
2879 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002880 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 spin_unlock(&clp->cl_lock);
2882 }
2883 return status;
2884}
2885
David Howellsadfa6f92006-08-22 20:06:08 -04002886int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002887{
2888 long timeout;
2889 int err;
2890 do {
2891 err = _nfs4_proc_setclientid_confirm(clp, cred);
2892 switch (err) {
2893 case 0:
2894 return err;
2895 case -NFS4ERR_RESOURCE:
2896 /* The IBM lawyers misread another document! */
2897 case -NFS4ERR_DELAY:
2898 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2899 }
2900 } while (err == 0);
2901 return err;
2902}
2903
Trond Myklebustfe650402006-01-03 09:55:18 +01002904struct nfs4_delegreturndata {
2905 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002906 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002907 struct nfs_fh fh;
2908 nfs4_stateid stateid;
2909 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002910 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002911 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002912 int rpc_status;
2913};
2914
2915static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Trond Myklebustfe650402006-01-03 09:55:18 +01002917 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002920 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002921 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002922 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002924 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002925 rpc_call_setup(task, &msg, 0);
2926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Trond Myklebustfe650402006-01-03 09:55:18 +01002928static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2929{
2930 struct nfs4_delegreturndata *data = calldata;
2931 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002932 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002933 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002934}
2935
2936static void nfs4_delegreturn_release(void *calldata)
2937{
2938 struct nfs4_delegreturndata *data = calldata;
2939
2940 put_rpccred(data->cred);
2941 kfree(calldata);
2942}
2943
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002944static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002945 .rpc_call_prepare = nfs4_delegreturn_prepare,
2946 .rpc_call_done = nfs4_delegreturn_done,
2947 .rpc_release = nfs4_delegreturn_release,
2948};
2949
2950static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2951{
2952 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002953 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002954 struct rpc_task *task;
2955 int status;
2956
2957 data = kmalloc(sizeof(*data), GFP_KERNEL);
2958 if (data == NULL)
2959 return -ENOMEM;
2960 data->args.fhandle = &data->fh;
2961 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002962 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002963 nfs_copy_fh(&data->fh, NFS_FH(inode));
2964 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002965 data->res.fattr = &data->fattr;
2966 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002967 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002968 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 data->rpc_status = 0;
2970
2971 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002972 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01002973 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002974 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002975 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002976 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002977 if (status == 0)
2978 nfs_post_op_update_inode(inode, &data->fattr);
2979 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002980 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002981 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2985{
2986 struct nfs_server *server = NFS_SERVER(inode);
2987 struct nfs4_exception exception = { };
2988 int err;
2989 do {
2990 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2991 switch (err) {
2992 case -NFS4ERR_STALE_STATEID:
2993 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 case 0:
2995 return 0;
2996 }
2997 err = nfs4_handle_exception(server, err, &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
3002#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3003#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3004
3005/*
3006 * sleep, with exponential backoff, and retry the LOCK operation.
3007 */
3008static unsigned long
3009nfs4_set_lock_task_retry(unsigned long timeout)
3010{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003011 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 timeout <<= 1;
3013 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3014 return NFS4_LOCK_MAXTIMEOUT;
3015 return timeout;
3016}
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3019{
3020 struct inode *inode = state->inode;
3021 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003022 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003023 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003025 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003027 struct nfs_lockt_res res = {
3028 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 };
3030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3032 .rpc_argp = &arg,
3033 .rpc_resp = &res,
3034 .rpc_cred = state->owner->so_cred,
3035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_lock_state *lsp;
3037 int status;
3038
3039 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003040 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003041 status = nfs4_set_lock_state(state, request);
3042 if (status != 0)
3043 goto out;
3044 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003045 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003047 switch (status) {
3048 case 0:
3049 request->fl_type = F_UNLCK;
3050 break;
3051 case -NFS4ERR_DENIED:
3052 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003054 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 up_read(&clp->cl_sem);
3057 return status;
3058}
3059
3060static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064
3065 do {
3066 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3067 _nfs4_proc_getlk(state, cmd, request),
3068 &exception);
3069 } while (exception.retry);
3070 return err;
3071}
3072
3073static int do_vfs_lock(struct file *file, struct file_lock *fl)
3074{
3075 int res = 0;
3076 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3077 case FL_POSIX:
3078 res = posix_lock_file_wait(file, fl);
3079 break;
3080 case FL_FLOCK:
3081 res = flock_lock_file_wait(file, fl);
3082 break;
3083 default:
3084 BUG();
3085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return res;
3087}
3088
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003089struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003090 struct nfs_locku_args arg;
3091 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003092 struct nfs4_lock_state *lsp;
3093 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003094 struct file_lock fl;
3095 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003096 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003097};
3098
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003099static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3100 struct nfs_open_context *ctx,
3101 struct nfs4_lock_state *lsp,
3102 struct nfs_seqid *seqid)
3103{
3104 struct nfs4_unlockdata *p;
3105 struct inode *inode = lsp->ls_state->inode;
3106
3107 p = kmalloc(sizeof(*p), GFP_KERNEL);
3108 if (p == NULL)
3109 return NULL;
3110 p->arg.fh = NFS_FH(inode);
3111 p->arg.fl = &p->fl;
3112 p->arg.seqid = seqid;
3113 p->arg.stateid = &lsp->ls_stateid;
3114 p->lsp = lsp;
3115 atomic_inc(&lsp->ls_count);
3116 /* Ensure we don't close file until we're done freeing locks! */
3117 p->ctx = get_nfs_open_context(ctx);
3118 memcpy(&p->fl, fl, sizeof(p->fl));
3119 p->server = NFS_SERVER(inode);
3120 return p;
3121}
3122
Trond Myklebust06f814a2006-01-03 09:55:07 +01003123static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003124{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003125 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003126 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003127 nfs4_put_lock_state(calldata->lsp);
3128 put_nfs_open_context(calldata->ctx);
3129 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003130}
3131
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003132static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003133{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003134 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003135
Trond Myklebust06f814a2006-01-03 09:55:07 +01003136 if (RPC_ASSASSINATED(task))
3137 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003138 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003139 switch (task->tk_status) {
3140 case 0:
3141 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003142 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003143 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003144 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003145 break;
3146 case -NFS4ERR_STALE_STATEID:
3147 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003148 break;
3149 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003150 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003151 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003152 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003153}
3154
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003155static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003156{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003157 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct rpc_message msg = {
3159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003160 .rpc_argp = &calldata->arg,
3161 .rpc_resp = &calldata->res,
3162 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003165 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003166 return;
3167 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003168 /* Note: exit _without_ running nfs4_locku_done */
3169 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003170 return;
3171 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003172 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003173 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003177 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003179 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003180};
3181
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003182static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3183 struct nfs_open_context *ctx,
3184 struct nfs4_lock_state *lsp,
3185 struct nfs_seqid *seqid)
3186{
3187 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003188
3189 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3190 if (data == NULL) {
3191 nfs_free_seqid(seqid);
3192 return ERR_PTR(-ENOMEM);
3193 }
3194
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003195 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003196}
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3199{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003200 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003201 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003202 struct rpc_task *task;
3203 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Trond Myklebust9b073572006-06-29 16:38:34 -04003205 status = nfs4_set_lock_state(state, request);
3206 /* Unlock _before_ we do the RPC call */
3207 request->fl_flags |= FL_EXISTS;
3208 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3209 goto out;
3210 if (status != 0)
3211 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003212 /* Is this a delegated lock? */
3213 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003214 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003215 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003216 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003217 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003218 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003219 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003220 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3221 status = PTR_ERR(task);
3222 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003223 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003224 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003225 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003226out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003227 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003230struct nfs4_lockdata {
3231 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003232 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003233 struct nfs4_lock_state *lsp;
3234 struct nfs_open_context *ctx;
3235 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003236 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003237 int rpc_status;
3238 int cancelled;
3239};
3240
3241static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3242 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3243{
3244 struct nfs4_lockdata *p;
3245 struct inode *inode = lsp->ls_state->inode;
3246 struct nfs_server *server = NFS_SERVER(inode);
3247
3248 p = kzalloc(sizeof(*p), GFP_KERNEL);
3249 if (p == NULL)
3250 return NULL;
3251
3252 p->arg.fh = NFS_FH(inode);
3253 p->arg.fl = &p->fl;
3254 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3255 if (p->arg.lock_seqid == NULL)
3256 goto out_free;
3257 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003258 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003259 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003260 p->lsp = lsp;
3261 atomic_inc(&lsp->ls_count);
3262 p->ctx = get_nfs_open_context(ctx);
3263 memcpy(&p->fl, fl, sizeof(p->fl));
3264 return p;
3265out_free:
3266 kfree(p);
3267 return NULL;
3268}
3269
3270static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3271{
3272 struct nfs4_lockdata *data = calldata;
3273 struct nfs4_state *state = data->lsp->ls_state;
3274 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3277 .rpc_argp = &data->arg,
3278 .rpc_resp = &data->res,
3279 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003282 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3283 return;
3284 dprintk("%s: begin!\n", __FUNCTION__);
3285 /* Do we need to do an open_to_lock_owner? */
3286 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3287 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3288 if (data->arg.open_seqid == NULL) {
3289 data->rpc_status = -ENOMEM;
3290 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003291 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003292 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003293 data->arg.open_stateid = &state->stateid;
3294 data->arg.new_lock_owner = 1;
3295 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003296 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003297 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003298out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003299 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3300}
3301
3302static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3303{
3304 struct nfs4_lockdata *data = calldata;
3305
3306 dprintk("%s: begin!\n", __FUNCTION__);
3307
3308 data->rpc_status = task->tk_status;
3309 if (RPC_ASSASSINATED(task))
3310 goto out;
3311 if (data->arg.new_lock_owner != 0) {
3312 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3313 if (data->rpc_status == 0)
3314 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3315 else
3316 goto out;
3317 }
3318 if (data->rpc_status == 0) {
3319 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3320 sizeof(data->lsp->ls_stateid.data));
3321 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003322 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003323 }
3324 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3325out:
3326 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3327}
3328
3329static void nfs4_lock_release(void *calldata)
3330{
3331 struct nfs4_lockdata *data = calldata;
3332
3333 dprintk("%s: begin!\n", __FUNCTION__);
3334 if (data->arg.open_seqid != NULL)
3335 nfs_free_seqid(data->arg.open_seqid);
3336 if (data->cancelled != 0) {
3337 struct rpc_task *task;
3338 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3339 data->arg.lock_seqid);
3340 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003341 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003342 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3343 } else
3344 nfs_free_seqid(data->arg.lock_seqid);
3345 nfs4_put_lock_state(data->lsp);
3346 put_nfs_open_context(data->ctx);
3347 kfree(data);
3348 dprintk("%s: done!\n", __FUNCTION__);
3349}
3350
3351static const struct rpc_call_ops nfs4_lock_ops = {
3352 .rpc_call_prepare = nfs4_lock_prepare,
3353 .rpc_call_done = nfs4_lock_done,
3354 .rpc_release = nfs4_lock_release,
3355};
3356
3357static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3358{
3359 struct nfs4_lockdata *data;
3360 struct rpc_task *task;
3361 int ret;
3362
3363 dprintk("%s: begin!\n", __FUNCTION__);
3364 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3365 fl->fl_u.nfs4_fl.owner);
3366 if (data == NULL)
3367 return -ENOMEM;
3368 if (IS_SETLKW(cmd))
3369 data->arg.block = 1;
3370 if (reclaim != 0)
3371 data->arg.reclaim = 1;
3372 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3373 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003374 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003375 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003376 ret = nfs4_wait_for_completion_rpc_task(task);
3377 if (ret == 0) {
3378 ret = data->rpc_status;
3379 if (ret == -NFS4ERR_DENIED)
3380 ret = -EAGAIN;
3381 } else
3382 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003383 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003384 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
3388static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3389{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003390 struct nfs_server *server = NFS_SERVER(state->inode);
3391 struct nfs4_exception exception = { };
3392 int err;
3393
3394 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003395 /* Cache the lock if possible... */
3396 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3397 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003398 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3399 if (err != -NFS4ERR_DELAY)
3400 break;
3401 nfs4_handle_exception(server, err, &exception);
3402 } while (exception.retry);
3403 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
3406static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3407{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003408 struct nfs_server *server = NFS_SERVER(state->inode);
3409 struct nfs4_exception exception = { };
3410 int err;
3411
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003412 err = nfs4_set_lock_state(state, request);
3413 if (err != 0)
3414 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003415 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003416 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3417 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003418 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3419 if (err != -NFS4ERR_DELAY)
3420 break;
3421 nfs4_handle_exception(server, err, &exception);
3422 } while (exception.retry);
3423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
3426static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3427{
David Howellsadfa6f92006-08-22 20:06:08 -04003428 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003429 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 int status;
3431
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003432 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003433 status = nfs4_set_lock_state(state, request);
3434 if (status != 0)
3435 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003436 request->fl_flags |= FL_ACCESS;
3437 status = do_vfs_lock(request->fl_file, request);
3438 if (status < 0)
3439 goto out;
3440 down_read(&clp->cl_sem);
3441 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3442 struct nfs_inode *nfsi = NFS_I(state->inode);
3443 /* Yes: cache locks! */
3444 down_read(&nfsi->rwsem);
3445 /* ...but avoid races with delegation recall... */
3446 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3447 request->fl_flags = fl_flags & ~FL_SLEEP;
3448 status = do_vfs_lock(request->fl_file, request);
3449 up_read(&nfsi->rwsem);
3450 goto out_unlock;
3451 }
3452 up_read(&nfsi->rwsem);
3453 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003454 status = _nfs4_do_setlk(state, cmd, request, 0);
3455 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003456 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003457 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003458 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003459 if (do_vfs_lock(request->fl_file, request) < 0)
3460 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003461out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003463out:
3464 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return status;
3466}
3467
3468static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3469{
3470 struct nfs4_exception exception = { };
3471 int err;
3472
3473 do {
3474 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3475 _nfs4_proc_setlk(state, cmd, request),
3476 &exception);
3477 } while (exception.retry);
3478 return err;
3479}
3480
3481static int
3482nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3483{
3484 struct nfs_open_context *ctx;
3485 struct nfs4_state *state;
3486 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3487 int status;
3488
3489 /* verify open state */
3490 ctx = (struct nfs_open_context *)filp->private_data;
3491 state = ctx->state;
3492
3493 if (request->fl_start < 0 || request->fl_end < 0)
3494 return -EINVAL;
3495
3496 if (IS_GETLK(cmd))
3497 return nfs4_proc_getlk(state, F_GETLK, request);
3498
3499 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3500 return -EINVAL;
3501
3502 if (request->fl_type == F_UNLCK)
3503 return nfs4_proc_unlck(state, cmd, request);
3504
3505 do {
3506 status = nfs4_proc_setlk(state, cmd, request);
3507 if ((status != -EAGAIN) || IS_SETLK(cmd))
3508 break;
3509 timeout = nfs4_set_lock_task_retry(timeout);
3510 status = -ERESTARTSYS;
3511 if (signalled())
3512 break;
3513 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 return status;
3515}
3516
Trond Myklebust888e6942005-11-04 15:38:11 -05003517int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3518{
3519 struct nfs_server *server = NFS_SERVER(state->inode);
3520 struct nfs4_exception exception = { };
3521 int err;
3522
3523 err = nfs4_set_lock_state(state, fl);
3524 if (err != 0)
3525 goto out;
3526 do {
3527 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3528 if (err != -NFS4ERR_DELAY)
3529 break;
3530 err = nfs4_handle_exception(server, err, &exception);
3531 } while (exception.retry);
3532out:
3533 return err;
3534}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003535
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003536#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3537
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003538int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3539 size_t buflen, int flags)
3540{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003541 struct inode *inode = dentry->d_inode;
3542
3543 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3544 return -EOPNOTSUPP;
3545
3546 if (!S_ISREG(inode->i_mode) &&
3547 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3548 return -EPERM;
3549
3550 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003551}
3552
3553/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3554 * and that's what we'll do for e.g. user attributes that haven't been set.
3555 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3556 * attributes in kernel-managed attribute namespaces. */
3557ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3558 size_t buflen)
3559{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003560 struct inode *inode = dentry->d_inode;
3561
3562 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3563 return -EOPNOTSUPP;
3564
3565 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003566}
3567
3568ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3569{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003570 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003571
J. Bruce Fields096455a2006-03-20 23:23:42 -05003572 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3573 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003574 if (buf && buflen < len)
3575 return -ERANGE;
3576 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003577 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3578 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003579}
3580
Trond Myklebustc228fd32007-01-13 02:28:11 -05003581int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003582 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003583{
3584 struct nfs_server *server = NFS_SERVER(dir);
3585 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003586 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3587 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003588 };
3589 struct nfs4_fs_locations_arg args = {
3590 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003591 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003592 .page = page,
3593 .bitmask = bitmask,
3594 };
3595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3597 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003598 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003599 };
3600 int status;
3601
3602 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003603 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003604 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003605 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003606 status = rpc_call_sync(server->client, &msg, 0);
3607 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3608 return status;
3609}
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3612 .recover_open = nfs4_open_reclaim,
3613 .recover_lock = nfs4_lock_reclaim,
3614};
3615
3616struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3617 .recover_open = nfs4_open_expired,
3618 .recover_lock = nfs4_lock_expired,
3619};
3620
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003621static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003622 .permission = nfs_permission,
3623 .getattr = nfs_getattr,
3624 .setattr = nfs_setattr,
3625 .getxattr = nfs4_getxattr,
3626 .setxattr = nfs4_setxattr,
3627 .listxattr = nfs4_listxattr,
3628};
3629
David Howells509de812006-08-22 20:06:11 -04003630const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 .version = 4, /* protocol version */
3632 .dentry_ops = &nfs4_dentry_operations,
3633 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003634 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 .getroot = nfs4_proc_get_root,
3636 .getattr = nfs4_proc_getattr,
3637 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003638 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 .lookup = nfs4_proc_lookup,
3640 .access = nfs4_proc_access,
3641 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 .create = nfs4_proc_create,
3643 .remove = nfs4_proc_remove,
3644 .unlink_setup = nfs4_proc_unlink_setup,
3645 .unlink_done = nfs4_proc_unlink_done,
3646 .rename = nfs4_proc_rename,
3647 .link = nfs4_proc_link,
3648 .symlink = nfs4_proc_symlink,
3649 .mkdir = nfs4_proc_mkdir,
3650 .rmdir = nfs4_proc_remove,
3651 .readdir = nfs4_proc_readdir,
3652 .mknod = nfs4_proc_mknod,
3653 .statfs = nfs4_proc_statfs,
3654 .fsinfo = nfs4_proc_fsinfo,
3655 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003656 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 .decode_dirent = nfs4_decode_dirent,
3658 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003659 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003661 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003663 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003664 .file_open = nfs_open,
3665 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003667 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668};
3669
3670/*
3671 * Local variables:
3672 * c-basic-offset: 8
3673 * End:
3674 */