blob: 26dcd77abb070d11e0a773cf7ee20f319543c2cb [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"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define NFSDBG_FACILITY NFSDBG_PROC
58
Trond Myklebust2066fe82006-09-15 08:30:46 -040059#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NFS4_POLL_RETRY_MAX (15*HZ)
61
Trond Myklebustcdd4e682006-01-03 09:55:12 +010062struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010063static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070065static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Trond Myklebust99367812007-07-17 21:52:41 -040066static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
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",
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 __func__, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 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 */
Peter Staubach4e769b92007-08-03 15:07:10 -0400177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
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 */
Peter Staubach4e769b92007-08-03 15:07:10 -0400189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
194}
195
Trond Myklebust65de8722008-12-23 15:21:44 -0500196static int nfs4_wait_bit_killable(void *word)
197{
198 if (fatal_signal_pending(current))
199 return -ERESTARTSYS;
200 schedule();
201 return 0;
202}
203
204static int nfs4_wait_clnt_recover(struct nfs_client *clp)
205{
206 int res;
207
208 might_sleep();
209
Trond Myklebust65de8722008-12-23 15:21:44 -0500210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
211 nfs4_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500212 return res;
213}
214
215static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216{
217 int res = 0;
218
219 might_sleep();
220
221 if (*timeout <= 0)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
227 res = -ERESTARTSYS;
228 *timeout <<= 1;
229 return res;
230}
231
232/* This is the error handling routine for processes that are allowed
233 * to sleep.
234 */
235static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
236{
237 struct nfs_client *clp = server->nfs_client;
238 int ret = errorcode;
239
240 exception->retry = 0;
241 switch(errorcode) {
242 case 0:
243 return 0;
244 case -NFS4ERR_STALE_CLIENTID:
245 case -NFS4ERR_STALE_STATEID:
246 case -NFS4ERR_EXPIRED:
247 nfs4_schedule_state_recovery(clp);
248 ret = nfs4_wait_clnt_recover(clp);
249 if (ret == 0)
250 exception->retry = 1;
251 break;
252 case -NFS4ERR_FILE_OPEN:
253 case -NFS4ERR_GRACE:
254 case -NFS4ERR_DELAY:
255 ret = nfs4_delay(server->client, &exception->timeout);
256 if (ret != 0)
257 break;
258 case -NFS4ERR_OLD_STATEID:
259 exception->retry = 1;
260 }
261 /* We failed to handle the error */
262 return nfs4_map_errors(ret);
263}
264
265
Trond Myklebust26e976a2006-01-03 09:55:21 +0100266static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
David Howells7539bba2006-08-22 20:06:09 -0400268 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_lock(&clp->cl_lock);
270 if (time_before(clp->cl_last_renewal,timestamp))
271 clp->cl_last_renewal = timestamp;
272 spin_unlock(&clp->cl_lock);
273}
274
Trond Myklebust38478b22006-05-25 01:40:57 -0400275static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Trond Myklebust38478b22006-05-25 01:40:57 -0400277 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Trond Myklebust38478b22006-05-25 01:40:57 -0400279 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400280 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
281 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400282 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400283 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400284 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100287struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400288 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100289 struct nfs_openargs o_arg;
290 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100291 struct nfs_open_confirmargs c_arg;
292 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100293 struct nfs_fattr f_attr;
294 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400295 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100296 struct dentry *dir;
297 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400298 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100299 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100300 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400301 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100302 int rpc_status;
303 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100304};
305
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400306
307static void nfs4_init_opendata_res(struct nfs4_opendata *p)
308{
309 p->o_res.f_attr = &p->f_attr;
310 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400311 p->o_res.seqid = p->o_arg.seqid;
312 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400313 p->o_res.server = p->o_arg.server;
314 nfs_fattr_init(&p->f_attr);
315 nfs_fattr_init(&p->dir_attr);
316}
317
Trond Myklebustad389da2007-06-05 12:30:00 -0400318static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100319 struct nfs4_state_owner *sp, int flags,
320 const struct iattr *attrs)
321{
Trond Myklebustad389da2007-06-05 12:30:00 -0400322 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100323 struct inode *dir = parent->d_inode;
324 struct nfs_server *server = NFS_SERVER(dir);
325 struct nfs4_opendata *p;
326
327 p = kzalloc(sizeof(*p), GFP_KERNEL);
328 if (p == NULL)
329 goto err;
330 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
331 if (p->o_arg.seqid == NULL)
332 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400333 p->path.mnt = mntget(path->mnt);
334 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100335 p->dir = parent;
336 p->owner = sp;
337 atomic_inc(&sp->so_count);
338 p->o_arg.fh = NFS_FH(dir);
339 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400340 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400341 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400342 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100343 p->o_arg.server = server;
344 p->o_arg.bitmask = server->attr_bitmask;
345 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100346 if (flags & O_EXCL) {
347 u32 *s = (u32 *) p->o_arg.u.verifier.data;
348 s[0] = jiffies;
349 s[1] = current->pid;
350 } else if (flags & O_CREAT) {
351 p->o_arg.u.attrs = &p->attrs;
352 memcpy(&p->attrs, attrs, sizeof(p->attrs));
353 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100354 p->c_arg.fh = &p->o_res.fh;
355 p->c_arg.stateid = &p->o_res.stateid;
356 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400357 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400358 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100359 return p;
360err_free:
361 kfree(p);
362err:
363 dput(parent);
364 return NULL;
365}
366
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400367static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100368{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400369 struct nfs4_opendata *p = container_of(kref,
370 struct nfs4_opendata, kref);
371
372 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400373 if (p->state != NULL)
374 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400375 nfs4_put_state_owner(p->owner);
376 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700377 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400378 kfree(p);
379}
380
381static void nfs4_opendata_put(struct nfs4_opendata *p)
382{
383 if (p != NULL)
384 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100385}
386
Trond Myklebust06f814a2006-01-03 09:55:07 +0100387static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
388{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100389 int ret;
390
Trond Myklebust06f814a2006-01-03 09:55:07 +0100391 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100392 return ret;
393}
394
Trond Myklebust6ee41262007-07-08 14:11:36 -0400395static int can_open_cached(struct nfs4_state *state, int mode)
396{
397 int ret = 0;
398 switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
399 case FMODE_READ:
400 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400401 break;
402 case FMODE_WRITE:
403 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400404 break;
405 case FMODE_READ|FMODE_WRITE:
406 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
407 }
408 return ret;
409}
410
Trond Myklebustaac00a82007-07-05 19:02:21 -0400411static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
412{
413 if ((delegation->type & open_flags) != open_flags)
414 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500415 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400416 return 0;
417 return 1;
418}
419
Trond Myklebust003707c2007-07-05 18:07:55 -0400420static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
Trond Myklebuste7616922006-01-03 09:55:13 +0100421{
422 switch (open_flags) {
423 case FMODE_WRITE:
424 state->n_wronly++;
425 break;
426 case FMODE_READ:
427 state->n_rdonly++;
428 break;
429 case FMODE_READ|FMODE_WRITE:
430 state->n_rdwr++;
431 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400432 nfs4_state_set_mode_locked(state, state->state | open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100433}
434
Trond Myklebust003707c2007-07-05 18:07:55 -0400435static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
436{
437 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
438 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
439 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
440 switch (open_flags) {
441 case FMODE_READ:
442 set_bit(NFS_O_RDONLY_STATE, &state->flags);
443 break;
444 case FMODE_WRITE:
445 set_bit(NFS_O_WRONLY_STATE, &state->flags);
446 break;
447 case FMODE_READ|FMODE_WRITE:
448 set_bit(NFS_O_RDWR_STATE, &state->flags);
449 }
450}
451
452static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
453{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400455 nfs_set_open_stateid_locked(state, stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400456 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400457}
458
Trond Myklebust34310432008-12-23 15:21:38 -0500459static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, int open_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400461 /*
462 * Protect the call to nfs4_state_set_mode_locked and
463 * serialise the stateid update
464 */
465 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400466 if (deleg_stateid != NULL) {
467 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
468 set_bit(NFS_DELEGATED_STATE, &state->flags);
469 }
470 if (open_stateid != NULL)
471 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400472 write_sequnlock(&state->seqlock);
473 spin_lock(&state->owner->so_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100474 update_open_stateflags(state, open_flags);
Trond Myklebustec073422005-10-20 14:22:47 -0700475 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Trond Myklebust34310432008-12-23 15:21:38 -0500478static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, int open_flags)
479{
480 struct nfs_inode *nfsi = NFS_I(state->inode);
481 struct nfs_delegation *deleg_cur;
482 int ret = 0;
483
484 open_flags &= (FMODE_READ|FMODE_WRITE);
485
486 rcu_read_lock();
487 deleg_cur = rcu_dereference(nfsi->delegation);
488 if (deleg_cur == NULL)
489 goto no_delegation;
490
491 spin_lock(&deleg_cur->lock);
492 if (nfsi->delegation != deleg_cur ||
493 (deleg_cur->type & open_flags) != open_flags)
494 goto no_delegation_unlock;
495
496 if (delegation == NULL)
497 delegation = &deleg_cur->stateid;
498 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
499 goto no_delegation_unlock;
500
501 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, open_flags);
502 ret = 1;
503no_delegation_unlock:
504 spin_unlock(&deleg_cur->lock);
505no_delegation:
506 rcu_read_unlock();
507
508 if (!ret && open_stateid != NULL) {
509 __update_open_stateid(state, open_stateid, NULL, open_flags);
510 ret = 1;
511 }
512
513 return ret;
514}
515
516
Trond Myklebustaac00a82007-07-05 19:02:21 -0400517static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
518{
519 struct nfs_delegation *delegation;
520
521 rcu_read_lock();
522 delegation = rcu_dereference(NFS_I(inode)->delegation);
523 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
524 rcu_read_unlock();
525 return;
526 }
527 rcu_read_unlock();
528 nfs_inode_return_delegation(inode);
529}
530
Trond Myklebust6ee41262007-07-08 14:11:36 -0400531static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400532{
533 struct nfs4_state *state = opendata->state;
534 struct nfs_inode *nfsi = NFS_I(state->inode);
535 struct nfs_delegation *delegation;
536 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
537 nfs4_stateid stateid;
538 int ret = -EAGAIN;
539
Trond Myklebustaac00a82007-07-05 19:02:21 -0400540 for (;;) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400541 if (can_open_cached(state, open_mode)) {
542 spin_lock(&state->owner->so_lock);
543 if (can_open_cached(state, open_mode)) {
544 update_open_stateflags(state, open_mode);
545 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400546 goto out_return_state;
547 }
548 spin_unlock(&state->owner->so_lock);
549 }
Trond Myklebust34310432008-12-23 15:21:38 -0500550 rcu_read_lock();
551 delegation = rcu_dereference(nfsi->delegation);
552 if (delegation == NULL ||
553 !can_open_delegated(delegation, open_mode)) {
554 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400555 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500556 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400557 /* Save the delegation */
558 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
559 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400560 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400561 if (ret != 0)
562 goto out;
563 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500564
565 /* Try to update the stateid using the delegation */
566 if (update_open_stateid(state, NULL, &stateid, open_mode))
567 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400568 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400569out:
570 return ERR_PTR(ret);
571out_return_state:
572 atomic_inc(&state->count);
573 return state;
574}
575
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100576static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
577{
578 struct inode *inode;
579 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400580 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400581 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100582
Trond Myklebustaac00a82007-07-05 19:02:21 -0400583 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400584 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400585 goto out;
586 }
587
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400588 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100589 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400590 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100591 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400592 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500593 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400594 goto err;
595 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100596 state = nfs4_get_open_state(inode, data->owner);
597 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400598 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400599 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400600 int delegation_flags = 0;
601
Trond Myklebust003707c2007-07-05 18:07:55 -0400602 rcu_read_lock();
603 delegation = rcu_dereference(NFS_I(inode)->delegation);
604 if (delegation)
605 delegation_flags = delegation->flags;
606 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500607 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400608 nfs_inode_set_delegation(state->inode,
609 data->owner->so_cred,
610 &data->o_res);
611 else
612 nfs_inode_reclaim_delegation(state->inode,
613 data->owner->so_cred,
614 &data->o_res);
615 }
Trond Myklebust34310432008-12-23 15:21:38 -0500616
617 update_open_stateid(state, &data->o_res.stateid, NULL,
618 data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100619 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400620out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100621 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400622err_put_inode:
623 iput(inode);
624err:
625 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100626}
627
Trond Myklebust864472e2006-01-03 09:55:15 +0100628static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
629{
630 struct nfs_inode *nfsi = NFS_I(state->inode);
631 struct nfs_open_context *ctx;
632
633 spin_lock(&state->inode->i_lock);
634 list_for_each_entry(ctx, &nfsi->open_files, list) {
635 if (ctx->state != state)
636 continue;
637 get_nfs_open_context(ctx);
638 spin_unlock(&state->inode->i_lock);
639 return ctx;
640 }
641 spin_unlock(&state->inode->i_lock);
642 return ERR_PTR(-ENOENT);
643}
644
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400645static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
646{
647 struct nfs4_opendata *opendata;
648
649 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
650 if (opendata == NULL)
651 return ERR_PTR(-ENOMEM);
652 opendata->state = state;
653 atomic_inc(&state->count);
654 return opendata;
655}
656
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400657static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100658{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400659 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100660 int ret;
661
662 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400663 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
664 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
665 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100666 ret = _nfs4_proc_open(opendata);
667 if (ret != 0)
668 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400669 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400670 if (IS_ERR(newstate))
671 return PTR_ERR(newstate);
672 nfs4_close_state(&opendata->path, newstate, openflags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400673 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100674 return 0;
675}
676
677static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
678{
Trond Myklebust864472e2006-01-03 09:55:15 +0100679 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100680 int ret;
681
682 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400683 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100684 smp_rmb();
685 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400686 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100687 if (ret != 0)
688 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400689 if (newstate != state)
690 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100691 }
692 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400693 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100694 if (ret != 0)
695 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400696 if (newstate != state)
697 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100698 }
699 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400700 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100701 if (ret != 0)
702 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400703 if (newstate != state)
704 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100705 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400706 /*
707 * We may have performed cached opens for all three recoveries.
708 * Check if we need to update the current stateid.
709 */
710 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
711 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400712 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400713 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
714 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400715 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400716 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100717 return 0;
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * OPEN_RECLAIM:
722 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400724static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400726 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100727 struct nfs4_opendata *opendata;
728 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int status;
730
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400731 opendata = nfs4_open_recoverdata_alloc(ctx, state);
732 if (IS_ERR(opendata))
733 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100734 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
735 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400736 rcu_read_lock();
737 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500738 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400739 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400740 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100741 opendata->o_arg.u.delegation_type = delegation_type;
742 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400743 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return status;
745}
746
Trond Myklebust539cd032007-06-05 11:46:42 -0400747static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct nfs_server *server = NFS_SERVER(state->inode);
750 struct nfs4_exception exception = { };
751 int err;
752 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400753 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000754 if (err != -NFS4ERR_DELAY)
755 break;
756 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } while (exception.retry);
758 return err;
759}
760
Trond Myklebust864472e2006-01-03 09:55:15 +0100761static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
762{
763 struct nfs_open_context *ctx;
764 int ret;
765
766 ctx = nfs4_state_find_open_context(state);
767 if (IS_ERR(ctx))
768 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400769 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100770 put_nfs_open_context(ctx);
771 return ret;
772}
773
Trond Myklebust13437e12007-07-06 15:10:43 -0400774static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100777 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400779 opendata = nfs4_open_recoverdata_alloc(ctx, state);
780 if (IS_ERR(opendata))
781 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400783 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100785 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400786 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Trond Myklebust13437e12007-07-06 15:10:43 -0400790int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400793 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int err;
795 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400796 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 switch (err) {
798 case 0:
799 return err;
800 case -NFS4ERR_STALE_CLIENTID:
801 case -NFS4ERR_STALE_STATEID:
802 case -NFS4ERR_EXPIRED:
803 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400804 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return err;
806 }
807 err = nfs4_handle_exception(server, err, &exception);
808 } while (exception.retry);
809 return err;
810}
811
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100812static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
813{
814 struct nfs4_opendata *data = calldata;
815
816 data->rpc_status = task->tk_status;
817 if (RPC_ASSASSINATED(task))
818 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100819 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
821 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -0500822 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +0100823 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400824 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100825 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100826}
827
828static void nfs4_open_confirm_release(void *calldata)
829{
830 struct nfs4_opendata *data = calldata;
831 struct nfs4_state *state = NULL;
832
833 /* If this request hasn't been cancelled, do nothing */
834 if (data->cancelled == 0)
835 goto out_free;
836 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400837 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100838 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100839 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400840 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400841 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100842out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400843 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100844}
845
846static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100847 .rpc_call_done = nfs4_open_confirm_done,
848 .rpc_release = nfs4_open_confirm_release,
849};
850
851/*
852 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
853 */
854static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
855{
856 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
857 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400858 struct rpc_message msg = {
859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
860 .rpc_argp = &data->c_arg,
861 .rpc_resp = &data->c_res,
862 .rpc_cred = data->owner->so_cred,
863 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400864 struct rpc_task_setup task_setup_data = {
865 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400866 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400867 .callback_ops = &nfs4_open_confirm_ops,
868 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500869 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400870 .flags = RPC_TASK_ASYNC,
871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int status;
873
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400875 data->rpc_done = 0;
876 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400877 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -0400878 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500879 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100880 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100881 status = nfs4_wait_for_completion_rpc_task(task);
882 if (status != 0) {
883 data->cancelled = 1;
884 smp_wmb();
885 } else
886 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500887 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return status;
889}
890
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100891static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100893 struct nfs4_opendata *data = calldata;
894 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400895
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100896 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
897 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 /*
899 * Check if we still need to send an OPEN call, or if we can use
900 * a delegation instead.
901 */
902 if (data->state != NULL) {
903 struct nfs_delegation *delegation;
904
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905 if (can_open_cached(data->state, data->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL)))
906 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 rcu_read_lock();
908 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
909 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -0500910 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400912 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913 }
914 rcu_read_unlock();
915 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100916 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400917 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100918 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400919 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400920 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400921 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
922 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100923 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400924 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925 return;
926out_no_action:
927 task->tk_action = NULL;
928
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100931static void nfs4_open_done(struct rpc_task *task, void *calldata)
932{
933 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100935 data->rpc_status = task->tk_status;
936 if (RPC_ASSASSINATED(task))
937 return;
938 if (task->tk_status == 0) {
939 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700940 case S_IFREG:
941 break;
942 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100943 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700944 break;
945 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100946 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700947 break;
948 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100949 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700950 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100951 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400952 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
953 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700954 }
Trond Myklebust3e309912007-07-07 13:19:59 -0400955 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100956}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700957
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100958static void nfs4_open_release(void *calldata)
959{
960 struct nfs4_opendata *data = calldata;
961 struct nfs4_state *state = NULL;
962
963 /* If this request hasn't been cancelled, do nothing */
964 if (data->cancelled == 0)
965 goto out_free;
966 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400967 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100968 goto out_free;
969 /* In case we need an open_confirm, no cleanup! */
970 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
971 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100972 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400973 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400974 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100975out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400976 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100977}
978
979static const struct rpc_call_ops nfs4_open_ops = {
980 .rpc_call_prepare = nfs4_open_prepare,
981 .rpc_call_done = nfs4_open_done,
982 .rpc_release = nfs4_open_release,
983};
984
985/*
986 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
987 */
988static int _nfs4_proc_open(struct nfs4_opendata *data)
989{
990 struct inode *dir = data->dir->d_inode;
991 struct nfs_server *server = NFS_SERVER(dir);
992 struct nfs_openargs *o_arg = &data->o_arg;
993 struct nfs_openres *o_res = &data->o_res;
994 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400995 struct rpc_message msg = {
996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
997 .rpc_argp = o_arg,
998 .rpc_resp = o_res,
999 .rpc_cred = data->owner->so_cred,
1000 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001001 struct rpc_task_setup task_setup_data = {
1002 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001003 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001004 .callback_ops = &nfs4_open_ops,
1005 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001006 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001007 .flags = RPC_TASK_ASYNC,
1008 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 int status;
1010
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001011 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001012 data->rpc_done = 0;
1013 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001014 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001015 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001016 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001017 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001018 status = nfs4_wait_for_completion_rpc_task(task);
1019 if (status != 0) {
1020 data->cancelled = 1;
1021 smp_wmb();
1022 } else
1023 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001024 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001025 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001026 return status;
1027
Trond Myklebust99367812007-07-17 21:52:41 -04001028 if (o_res->fh.size == 0)
1029 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1030
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001031 if (o_arg->open_flags & O_CREAT) {
1032 update_changeattr(dir, &o_res->cinfo);
1033 nfs_post_op_update_inode(dir, o_res->dir_attr);
1034 } else
1035 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001037 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001042 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Trond Myklebust10afec92007-05-14 17:16:04 -04001046static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001047{
David Howells7539bba2006-08-22 20:06:09 -04001048 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001049 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001050
Trond Myklebust6b309542006-09-14 14:03:14 -04001051 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001052 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001053 if (ret != 0)
1054 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001055 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1056 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001057 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001058 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001059 }
1060 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/*
1064 * OPEN_EXPIRED:
1065 * reclaim state on the server after a network partition.
1066 * Assumes caller holds the appropriate lock
1067 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001068static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001071 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001073 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1074 if (IS_ERR(opendata))
1075 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001076 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001077 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001078 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001079 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001080 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Trond Myklebust539cd032007-06-05 11:46:42 -04001083static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001084{
Trond Myklebust539cd032007-06-05 11:46:42 -04001085 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001086 struct nfs4_exception exception = { };
1087 int err;
1088
1089 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001090 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001091 if (err == -NFS4ERR_DELAY)
1092 nfs4_handle_exception(server, err, &exception);
1093 } while (exception.retry);
1094 return err;
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001100 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 ctx = nfs4_state_find_open_context(state);
1103 if (IS_ERR(ctx))
1104 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001105 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 put_nfs_open_context(ctx);
1107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001111 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1112 * fields corresponding to attributes that were used to store the verifier.
1113 * Make sure we clobber those fields in the later setattr call
1114 */
1115static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1116{
1117 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1118 !(sattr->ia_valid & ATTR_ATIME_SET))
1119 sattr->ia_valid |= ATTR_ATIME;
1120
1121 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1122 !(sattr->ia_valid & ATTR_MTIME_SET))
1123 sattr->ia_valid |= ATTR_MTIME;
1124}
1125
1126/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001129static 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 -07001130{
1131 struct nfs4_state_owner *sp;
1132 struct nfs4_state *state = NULL;
1133 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001134 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001135 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 status = -ENOMEM;
1139 if (!(sp = nfs4_get_state_owner(server, cred))) {
1140 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1141 goto out_err;
1142 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001143 status = nfs4_recover_expired_lease(server);
1144 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001145 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146 if (path->dentry->d_inode != NULL)
1147 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
Trond Myklebust58d97142006-01-03 09:55:24 +01001148 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001149 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001151 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Trond Myklebustaac00a82007-07-05 19:02:21 -04001153 if (path->dentry->d_inode != NULL)
1154 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1155
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001156 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001158 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001160 if (opendata->o_arg.open_flags & O_EXCL)
1161 nfs4_exclusive_attrset(opendata, sattr);
1162
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001163 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001164 status = PTR_ERR(state);
1165 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001166 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001167 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *res = state;
1170 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001171err_opendata_put:
1172 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173err_put_state_owner:
1174 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 *res = NULL;
1177 return status;
1178}
1179
1180
Trond Myklebustad389da2007-06-05 12:30:00 -04001181static 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 -07001182{
1183 struct nfs4_exception exception = { };
1184 struct nfs4_state *res;
1185 int status;
1186
1187 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001188 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (status == 0)
1190 break;
1191 /* NOTE: BAD_SEQID means the server and client disagree about the
1192 * book-keeping w.r.t. state-changing operations
1193 * (OPEN/CLOSE/LOCK/LOCKU...)
1194 * It is actually a sign of a bug on the client or on the server.
1195 *
1196 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001197 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 * have unhashed the old state_owner for us, and that we can
1199 * therefore safely retry using a new one. We should still warn
1200 * the user though...
1201 */
1202 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001203 printk(KERN_WARNING "NFS: v4 server %s "
1204 " returned a bad sequence-id error!\n",
1205 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 exception.retry = 1;
1207 continue;
1208 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001209 /*
1210 * BAD_STATEID on OPEN means that the server cancelled our
1211 * state before it received the OPEN_CONFIRM.
1212 * Recover by retrying the request as per the discussion
1213 * on Page 181 of RFC3530.
1214 */
1215 if (status == -NFS4ERR_BAD_STATEID) {
1216 exception.retry = 1;
1217 continue;
1218 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001219 if (status == -EAGAIN) {
1220 /* We must have found a delegation */
1221 exception.retry = 1;
1222 continue;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1225 status, &exception));
1226 } while (exception.retry);
1227 return res;
1228}
1229
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001230static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1231 struct nfs_fattr *fattr, struct iattr *sattr,
1232 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001234 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001236 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .iap = sattr,
1238 .server = server,
1239 .bitmask = server->attr_bitmask,
1240 };
1241 struct nfs_setattrres res = {
1242 .fattr = fattr,
1243 .server = server,
1244 };
1245 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1247 .rpc_argp = &arg,
1248 .rpc_resp = &res,
1249 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001251 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001252 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Trond Myklebust0e574af2005-10-27 22:12:38 -04001254 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001256 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1257 /* Use that stateid */
1258 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001259 nfs4_copy_stateid(&arg.stateid, state, current->files);
1260 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1262
Trond Myklebust65e43082005-08-16 11:49:44 -04001263 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001264 if (status == 0 && state != NULL)
1265 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001269static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1270 struct nfs_fattr *fattr, struct iattr *sattr,
1271 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001273 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct nfs4_exception exception = { };
1275 int err;
1276 do {
1277 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001278 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 &exception);
1280 } while (exception.retry);
1281 return err;
1282}
1283
1284struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001285 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct inode *inode;
1287 struct nfs4_state *state;
1288 struct nfs_closeargs arg;
1289 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001290 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001291 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292};
1293
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001294static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001295{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001296 struct nfs4_closedata *calldata = data;
1297 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001298
1299 nfs4_put_open_state(calldata->state);
1300 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001301 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001302 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001303 kfree(calldata);
1304}
1305
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001306static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001308 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct nfs_server *server = NFS_SERVER(calldata->inode);
1311
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001312 if (RPC_ASSASSINATED(task))
1313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* hmm. we are done with the inode, and in the process of freeing
1315 * the state_owner. we keep this around to process errors
1316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 switch (task->tk_status) {
1318 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001319 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001320 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case -NFS4ERR_STALE_STATEID:
1323 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 default:
1326 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1327 rpc_restart_call(task);
1328 return;
1329 }
1330 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001331 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001334static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001336 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001337 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001338 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001339
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001340 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001341 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001342
Trond Myklebust003707c2007-07-05 18:07:55 -04001343 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001344 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001345 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001346 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001347 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001348 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1349 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1350 }
1351 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001352 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1353 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1354 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001355 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001356 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001357 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001358 /* Note: exit _without_ calling nfs4_close_done */
1359 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001360 return;
1361 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001362 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001363 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001364 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust45328c32007-07-26 17:47:34 -04001365 calldata->arg.open_flags = FMODE_READ;
1366 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001367 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust45328c32007-07-26 17:47:34 -04001368 calldata->arg.open_flags = FMODE_WRITE;
1369 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001370 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001371 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001374static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001375 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001376 .rpc_call_done = nfs4_close_done,
1377 .rpc_release = nfs4_free_closedata,
1378};
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*
1381 * It is possible for data to be read/written from a mem-mapped file
1382 * after the sys_close call (which hits the vfs layer as a flush).
1383 * This means that we can't safely call nfsv4 close on a file until
1384 * the inode is cleared. This in turn means that we are not good
1385 * NFSv4 citizens - we do not indicate to the server to update the file's
1386 * share state even when we are done with one of the three share
1387 * stateid's in the inode.
1388 *
1389 * NOTE: Caller must be holding the sp->so_owner semaphore!
1390 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001391int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001393 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001395 struct nfs4_state_owner *sp = state->owner;
1396 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001397 struct rpc_message msg = {
1398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1399 .rpc_cred = state->owner->so_cred,
1400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001401 struct rpc_task_setup task_setup_data = {
1402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001404 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001405 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001406 .flags = RPC_TASK_ASYNC,
1407 };
Trond Myklebust95121352005-10-18 14:20:12 -07001408 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Trond Myklebust95121352005-10-18 14:20:12 -07001410 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001412 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001413 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001415 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001416 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001418 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001419 if (calldata->arg.seqid == NULL)
1420 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001421 calldata->arg.bitmask = server->attr_bitmask;
1422 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001423 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001424 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001425 calldata->path.mnt = mntget(path->mnt);
1426 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001427
Trond Myklebust5138fde2007-07-14 15:40:01 -04001428 msg.rpc_argp = &calldata->arg,
1429 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001430 task_setup_data.callback_data = calldata;
1431 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001432 if (IS_ERR(task))
1433 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001434 status = 0;
1435 if (wait)
1436 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001437 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001438 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001439out_free_calldata:
1440 kfree(calldata);
1441out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001442 nfs4_put_open_state(state);
1443 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001447static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001448{
1449 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001450 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001451
Trond Myklebust1b45c462007-07-03 13:04:56 -04001452 /* If the open_intent is for execute, we have an extra check to make */
1453 if (nd->intent.open.flags & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001454 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001455 state->owner->so_cred,
1456 nd->intent.open.flags);
1457 if (ret < 0)
1458 goto out_close;
1459 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001460 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001461 if (!IS_ERR(filp)) {
1462 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001463 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001464 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001465 return 0;
1466 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001467 ret = PTR_ERR(filp);
1468out_close:
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001469 nfs4_close_sync(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001470 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001471}
1472
1473struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1475{
Trond Myklebustad389da2007-06-05 12:30:00 -04001476 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001477 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001478 .dentry = dentry,
1479 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001480 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct iattr attr;
1482 struct rpc_cred *cred;
1483 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001484 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (nd->flags & LOOKUP_CREATE) {
1487 attr.ia_mode = nd->intent.open.create_mode;
1488 attr.ia_valid = ATTR_MODE;
1489 if (!IS_POSIXACL(dir))
1490 attr.ia_mode &= ~current->fs->umask;
1491 } else {
1492 attr.ia_valid = 0;
1493 BUG_ON(nd->intent.open.flags & O_CREAT);
1494 }
1495
Trond Myklebust98a8e322008-03-12 12:25:28 -04001496 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001498 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001499 parent = dentry->d_parent;
1500 /* Protect against concurrent sillydeletes */
1501 nfs_block_sillyrename(parent);
Trond Myklebustad389da2007-06-05 12:30:00 -04001502 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001504 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001505 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001506 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001507 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1508 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001509 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001510 return (struct dentry *)state;
1511 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001513 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001514 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001515 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001516 nfs_unblock_sillyrename(parent);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001517 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001518 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001522nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Trond Myklebustad389da2007-06-05 12:30:00 -04001524 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001525 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001526 .dentry = dentry,
1527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 struct rpc_cred *cred;
1529 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Trond Myklebust98a8e322008-03-12 12:25:28 -04001531 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (IS_ERR(cred))
1533 return PTR_ERR(cred);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001534 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001536 if (IS_ERR(state)) {
1537 switch (PTR_ERR(state)) {
1538 case -EPERM:
1539 case -EACCES:
1540 case -EDQUOT:
1541 case -ENOSPC:
1542 case -EROFS:
1543 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1544 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001545 default:
1546 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001547 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001548 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001550 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001551 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 1;
1553 }
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001554 nfs4_close_sync(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001555out_drop:
1556 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
1558}
1559
1560
1561static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1562{
1563 struct nfs4_server_caps_res res = {};
1564 struct rpc_message msg = {
1565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1566 .rpc_argp = fhandle,
1567 .rpc_resp = &res,
1568 };
1569 int status;
1570
1571 status = rpc_call_sync(server->client, &msg, 0);
1572 if (status == 0) {
1573 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1574 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1575 server->caps |= NFS_CAP_ACLS;
1576 if (res.has_links != 0)
1577 server->caps |= NFS_CAP_HARDLINKS;
1578 if (res.has_symlinks != 0)
1579 server->caps |= NFS_CAP_SYMLINKS;
1580 server->acl_bitmask = res.acl_bitmask;
1581 }
1582 return status;
1583}
1584
Trond Myklebust55a97592006-06-09 09:34:19 -04001585int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 struct nfs4_exception exception = { };
1588 int err;
1589 do {
1590 err = nfs4_handle_exception(server,
1591 _nfs4_server_capabilities(server, fhandle),
1592 &exception);
1593 } while (exception.retry);
1594 return err;
1595}
1596
1597static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1598 struct nfs_fsinfo *info)
1599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 struct nfs4_lookup_root_arg args = {
1601 .bitmask = nfs4_fattr_bitmap,
1602 };
1603 struct nfs4_lookup_res res = {
1604 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001605 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 .fh = fhandle,
1607 };
1608 struct rpc_message msg = {
1609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1610 .rpc_argp = &args,
1611 .rpc_resp = &res,
1612 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001613 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return rpc_call_sync(server->client, &msg, 0);
1615}
1616
1617static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1618 struct nfs_fsinfo *info)
1619{
1620 struct nfs4_exception exception = { };
1621 int err;
1622 do {
1623 err = nfs4_handle_exception(server,
1624 _nfs4_lookup_root(server, fhandle, info),
1625 &exception);
1626 } while (exception.retry);
1627 return err;
1628}
1629
David Howells54ceac42006-08-22 20:06:13 -04001630/*
1631 * get the file handle for the "/" directory on the server
1632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001634 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int status;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (status == 0)
1640 status = nfs4_server_capabilities(server, fhandle);
1641 if (status == 0)
1642 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001643 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Manoj Naik6b97fd32006-06-09 09:34:29 -04001646/*
1647 * Get locations and (maybe) other attributes of a referral.
1648 * Note that we'll actually follow the referral later when
1649 * we detect fsid mismatch in inode revalidation
1650 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001651static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04001652{
1653 int status = -ENOMEM;
1654 struct page *page = NULL;
1655 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001656
1657 page = alloc_page(GFP_KERNEL);
1658 if (page == NULL)
1659 goto out;
1660 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1661 if (locations == NULL)
1662 goto out;
1663
Trond Myklebustc228fd32007-01-13 02:28:11 -05001664 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001665 if (status != 0)
1666 goto out;
1667 /* Make sure server returned a different fsid for the referral */
1668 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001669 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001670 status = -EIO;
1671 goto out;
1672 }
1673
1674 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1675 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1676 if (!fattr->mode)
1677 fattr->mode = S_IFDIR;
1678 memset(fhandle, 0, sizeof(struct nfs_fh));
1679out:
1680 if (page)
1681 __free_page(page);
1682 if (locations)
1683 kfree(locations);
1684 return status;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1688{
1689 struct nfs4_getattr_arg args = {
1690 .fh = fhandle,
1691 .bitmask = server->attr_bitmask,
1692 };
1693 struct nfs4_getattr_res res = {
1694 .fattr = fattr,
1695 .server = server,
1696 };
1697 struct rpc_message msg = {
1698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1699 .rpc_argp = &args,
1700 .rpc_resp = &res,
1701 };
1702
Trond Myklebust0e574af2005-10-27 22:12:38 -04001703 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return rpc_call_sync(server->client, &msg, 0);
1705}
1706
1707static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1708{
1709 struct nfs4_exception exception = { };
1710 int err;
1711 do {
1712 err = nfs4_handle_exception(server,
1713 _nfs4_proc_getattr(server, fhandle, fattr),
1714 &exception);
1715 } while (exception.retry);
1716 return err;
1717}
1718
1719/*
1720 * The file is not closed if it is opened due to the a request to change
1721 * the size of the file. The open call will not be needed once the
1722 * VFS layer lookup-intents are implemented.
1723 *
1724 * Close is called when the inode is destroyed.
1725 * If we haven't opened the file for O_WRONLY, we
1726 * need to in the size_change case to obtain a stateid.
1727 *
1728 * Got race?
1729 * Because OPEN is always done by name in nfsv4, it is
1730 * possible that we opened a different file by the same
1731 * name. We can recognize this race condition, but we
1732 * can't do anything about it besides returning an error.
1733 *
1734 * This will be fixed with VFS changes (lookup-intent).
1735 */
1736static int
1737nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1738 struct iattr *sattr)
1739{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001740 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001741 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001742 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 int status;
1744
Trond Myklebust0e574af2005-10-27 22:12:38 -04001745 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Trond Myklebustd5308382005-11-04 15:33:38 -05001747 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001748 if (sattr->ia_valid & ATTR_FILE) {
1749 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001750
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001751 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11001752 if (ctx) {
1753 cred = ctx->cred;
1754 state = ctx->state;
1755 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001756 }
1757
1758 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001759 if (status == 0)
1760 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return status;
1762}
1763
Trond Myklebust56659e92007-07-17 21:52:39 -04001764static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1765 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001766 struct nfs_fattr *fattr)
1767{
1768 int status;
1769 struct nfs4_lookup_arg args = {
1770 .bitmask = server->attr_bitmask,
1771 .dir_fh = dirfh,
1772 .name = name,
1773 };
1774 struct nfs4_lookup_res res = {
1775 .server = server,
1776 .fattr = fattr,
1777 .fh = fhandle,
1778 };
1779 struct rpc_message msg = {
1780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1781 .rpc_argp = &args,
1782 .rpc_resp = &res,
1783 };
1784
1785 nfs_fattr_init(fattr);
1786
1787 dprintk("NFS call lookupfh %s\n", name->name);
1788 status = rpc_call_sync(server->client, &msg, 0);
1789 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001790 return status;
1791}
1792
1793static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1794 struct qstr *name, struct nfs_fh *fhandle,
1795 struct nfs_fattr *fattr)
1796{
1797 struct nfs4_exception exception = { };
1798 int err;
1799 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001800 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1801 /* FIXME: !!!! */
1802 if (err == -NFS4ERR_MOVED) {
1803 err = -EREMOTE;
1804 break;
1805 }
1806 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001807 } while (exception.retry);
1808 return err;
1809}
1810
Trond Myklebust56659e92007-07-17 21:52:39 -04001811static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1813{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001814 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001817 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001818 if (status == -NFS4ERR_MOVED)
1819 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 dprintk("NFS reply lookup: %d\n", status);
1821 return status;
1822}
1823
1824static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1825{
1826 struct nfs4_exception exception = { };
1827 int err;
1828 do {
1829 err = nfs4_handle_exception(NFS_SERVER(dir),
1830 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1831 &exception);
1832 } while (exception.retry);
1833 return err;
1834}
1835
1836static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1837{
Trond Myklebust76b32992007-08-10 17:45:11 -04001838 struct nfs_server *server = NFS_SERVER(inode);
1839 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct nfs4_accessargs args = {
1841 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04001842 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 };
Trond Myklebust76b32992007-08-10 17:45:11 -04001844 struct nfs4_accessres res = {
1845 .server = server,
1846 .fattr = &fattr,
1847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct rpc_message msg = {
1849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1850 .rpc_argp = &args,
1851 .rpc_resp = &res,
1852 .rpc_cred = entry->cred,
1853 };
1854 int mode = entry->mask;
1855 int status;
1856
1857 /*
1858 * Determine which access bits we want to ask for...
1859 */
1860 if (mode & MAY_READ)
1861 args.access |= NFS4_ACCESS_READ;
1862 if (S_ISDIR(inode->i_mode)) {
1863 if (mode & MAY_WRITE)
1864 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1865 if (mode & MAY_EXEC)
1866 args.access |= NFS4_ACCESS_LOOKUP;
1867 } else {
1868 if (mode & MAY_WRITE)
1869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1870 if (mode & MAY_EXEC)
1871 args.access |= NFS4_ACCESS_EXECUTE;
1872 }
Trond Myklebust76b32992007-08-10 17:45:11 -04001873 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1875 if (!status) {
1876 entry->mask = 0;
1877 if (res.access & NFS4_ACCESS_READ)
1878 entry->mask |= MAY_READ;
1879 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1880 entry->mask |= MAY_WRITE;
1881 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1882 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04001883 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 return status;
1886}
1887
1888static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1889{
1890 struct nfs4_exception exception = { };
1891 int err;
1892 do {
1893 err = nfs4_handle_exception(NFS_SERVER(inode),
1894 _nfs4_proc_access(inode, entry),
1895 &exception);
1896 } while (exception.retry);
1897 return err;
1898}
1899
1900/*
1901 * TODO: For the time being, we don't try to get any attributes
1902 * along with any of the zero-copy operations READ, READDIR,
1903 * READLINK, WRITE.
1904 *
1905 * In the case of the first three, we want to put the GETATTR
1906 * after the read-type operation -- this is because it is hard
1907 * to predict the length of a GETATTR response in v4, and thus
1908 * align the READ data correctly. This means that the GETATTR
1909 * may end up partially falling into the page cache, and we should
1910 * shift it into the 'tail' of the xdr_buf before processing.
1911 * To do this efficiently, we need to know the total length
1912 * of data received, which doesn't seem to be available outside
1913 * of the RPC layer.
1914 *
1915 * In the case of WRITE, we also want to put the GETATTR after
1916 * the operation -- in this case because we want to make sure
1917 * we get the post-operation mtime and size. This means that
1918 * we can't use xdr_encode_pages() as written: we need a variant
1919 * of it which would leave room in the 'tail' iovec.
1920 *
1921 * Both of these changes to the XDR layer would in fact be quite
1922 * minor, but I decided to leave them for a subsequent patch.
1923 */
1924static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1925 unsigned int pgbase, unsigned int pglen)
1926{
1927 struct nfs4_readlink args = {
1928 .fh = NFS_FH(inode),
1929 .pgbase = pgbase,
1930 .pglen = pglen,
1931 .pages = &page,
1932 };
1933 struct rpc_message msg = {
1934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1935 .rpc_argp = &args,
1936 .rpc_resp = NULL,
1937 };
1938
1939 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1940}
1941
1942static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1943 unsigned int pgbase, unsigned int pglen)
1944{
1945 struct nfs4_exception exception = { };
1946 int err;
1947 do {
1948 err = nfs4_handle_exception(NFS_SERVER(inode),
1949 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1950 &exception);
1951 } while (exception.retry);
1952 return err;
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/*
1956 * Got race?
1957 * We will need to arrange for the VFS layer to provide an atomic open.
1958 * Until then, this create/open method is prone to inefficiency and race
1959 * conditions due to the lookup, create, and open VFS calls from sys_open()
1960 * placed on the wire.
1961 *
1962 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1963 * The file will be opened again in the subsequent VFS open call
1964 * (nfs4_proc_file_open).
1965 *
1966 * The open for read will just hang around to be used by any process that
1967 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1968 */
1969
1970static int
1971nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001972 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebustad389da2007-06-05 12:30:00 -04001974 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001975 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001976 .dentry = dentry,
1977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct nfs4_state *state;
1979 struct rpc_cred *cred;
1980 int status = 0;
1981
Trond Myklebust98a8e322008-03-12 12:25:28 -04001982 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (IS_ERR(cred)) {
1984 status = PTR_ERR(cred);
1985 goto out;
1986 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001987 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001988 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (IS_ERR(state)) {
1990 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001991 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001993 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04001994 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (flags & O_EXCL) {
1996 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001997 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001998 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001999 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002000 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002001 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002002 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002003 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002004 else
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002005 nfs4_close_sync(&path, state, flags);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002006out_putcred:
2007 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008out:
2009 return status;
2010}
2011
2012static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2013{
Trond Myklebust16e42952005-10-27 22:12:44 -04002014 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002015 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002017 .name.len = name->len,
2018 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002019 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002021 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002022 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002023 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2026 .rpc_argp = &args,
2027 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 };
2029 int status;
2030
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002031 nfs_fattr_init(&res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002032 status = rpc_call_sync(server->client, &msg, 0);
2033 if (status == 0) {
2034 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002035 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return status;
2038}
2039
2040static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2041{
2042 struct nfs4_exception exception = { };
2043 int err;
2044 do {
2045 err = nfs4_handle_exception(NFS_SERVER(dir),
2046 _nfs4_proc_remove(dir, name),
2047 &exception);
2048 } while (exception.retry);
2049 return err;
2050}
2051
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002052static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002054 struct nfs_server *server = NFS_SERVER(dir);
2055 struct nfs_removeargs *args = msg->rpc_argp;
2056 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002058 args->bitmask = server->attr_bitmask;
2059 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002063static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002065 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2066
2067 if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
2068 return 0;
2069 update_changeattr(dir, &res->cinfo);
2070 nfs_post_op_update_inode(dir, &res->dir_attr);
2071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2075 struct inode *new_dir, struct qstr *new_name)
2076{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002077 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct nfs4_rename_arg arg = {
2079 .old_dir = NFS_FH(old_dir),
2080 .new_dir = NFS_FH(new_dir),
2081 .old_name = old_name,
2082 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002083 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002085 struct nfs_fattr old_fattr, new_fattr;
2086 struct nfs4_rename_res res = {
2087 .server = server,
2088 .old_fattr = &old_fattr,
2089 .new_fattr = &new_fattr,
2090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2093 .rpc_argp = &arg,
2094 .rpc_resp = &res,
2095 };
2096 int status;
2097
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002098 nfs_fattr_init(res.old_fattr);
2099 nfs_fattr_init(res.new_fattr);
2100 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 if (!status) {
2103 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002104 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002106 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 return status;
2109}
2110
2111static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2112 struct inode *new_dir, struct qstr *new_name)
2113{
2114 struct nfs4_exception exception = { };
2115 int err;
2116 do {
2117 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2118 _nfs4_proc_rename(old_dir, old_name,
2119 new_dir, new_name),
2120 &exception);
2121 } while (exception.retry);
2122 return err;
2123}
2124
2125static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2126{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002127 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs4_link_arg arg = {
2129 .fh = NFS_FH(inode),
2130 .dir_fh = NFS_FH(dir),
2131 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002132 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002134 struct nfs_fattr fattr, dir_attr;
2135 struct nfs4_link_res res = {
2136 .server = server,
2137 .fattr = &fattr,
2138 .dir_attr = &dir_attr,
2139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2142 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002143 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 };
2145 int status;
2146
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002147 nfs_fattr_init(res.fattr);
2148 nfs_fattr_init(res.dir_attr);
2149 status = rpc_call_sync(server->client, &msg, 0);
2150 if (!status) {
2151 update_changeattr(dir, &res.cinfo);
2152 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002153 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 return status;
2157}
2158
2159static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2160{
2161 struct nfs4_exception exception = { };
2162 int err;
2163 do {
2164 err = nfs4_handle_exception(NFS_SERVER(inode),
2165 _nfs4_proc_link(inode, dir, name),
2166 &exception);
2167 } while (exception.retry);
2168 return err;
2169}
2170
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002171struct nfs4_createdata {
2172 struct rpc_message msg;
2173 struct nfs4_create_arg arg;
2174 struct nfs4_create_res res;
2175 struct nfs_fh fh;
2176 struct nfs_fattr fattr;
2177 struct nfs_fattr dir_fattr;
2178};
2179
2180static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2181 struct qstr *name, struct iattr *sattr, u32 ftype)
2182{
2183 struct nfs4_createdata *data;
2184
2185 data = kzalloc(sizeof(*data), GFP_KERNEL);
2186 if (data != NULL) {
2187 struct nfs_server *server = NFS_SERVER(dir);
2188
2189 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2190 data->msg.rpc_argp = &data->arg;
2191 data->msg.rpc_resp = &data->res;
2192 data->arg.dir_fh = NFS_FH(dir);
2193 data->arg.server = server;
2194 data->arg.name = name;
2195 data->arg.attrs = sattr;
2196 data->arg.ftype = ftype;
2197 data->arg.bitmask = server->attr_bitmask;
2198 data->res.server = server;
2199 data->res.fh = &data->fh;
2200 data->res.fattr = &data->fattr;
2201 data->res.dir_fattr = &data->dir_fattr;
2202 nfs_fattr_init(data->res.fattr);
2203 nfs_fattr_init(data->res.dir_fattr);
2204 }
2205 return data;
2206}
2207
2208static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2209{
2210 int status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
2211 if (status == 0) {
2212 update_changeattr(dir, &data->res.dir_cinfo);
2213 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2214 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2215 }
2216 return status;
2217}
2218
2219static void nfs4_free_createdata(struct nfs4_createdata *data)
2220{
2221 kfree(data);
2222}
2223
Chuck Lever4f390c12006-08-22 20:06:22 -04002224static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002225 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002227 struct nfs4_createdata *data;
2228 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Chuck Lever94a6d752006-08-22 20:06:23 -04002230 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002231 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002232
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002233 status = -ENOMEM;
2234 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2235 if (data == NULL)
2236 goto out;
2237
2238 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2239 data->arg.u.symlink.pages = &page;
2240 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002242 status = nfs4_do_create(dir, dentry, data);
2243
2244 nfs4_free_createdata(data);
2245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return status;
2247}
2248
Chuck Lever4f390c12006-08-22 20:06:22 -04002249static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002250 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct nfs4_exception exception = { };
2253 int err;
2254 do {
2255 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002256 _nfs4_proc_symlink(dir, dentry, page,
2257 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 &exception);
2259 } while (exception.retry);
2260 return err;
2261}
2262
2263static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2264 struct iattr *sattr)
2265{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002266 struct nfs4_createdata *data;
2267 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002269 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2270 if (data == NULL)
2271 goto out;
2272
2273 status = nfs4_do_create(dir, dentry, data);
2274
2275 nfs4_free_createdata(data);
2276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return status;
2278}
2279
2280static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2281 struct iattr *sattr)
2282{
2283 struct nfs4_exception exception = { };
2284 int err;
2285 do {
2286 err = nfs4_handle_exception(NFS_SERVER(dir),
2287 _nfs4_proc_mkdir(dir, dentry, sattr),
2288 &exception);
2289 } while (exception.retry);
2290 return err;
2291}
2292
2293static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2294 u64 cookie, struct page *page, unsigned int count, int plus)
2295{
2296 struct inode *dir = dentry->d_inode;
2297 struct nfs4_readdir_arg args = {
2298 .fh = NFS_FH(dir),
2299 .pages = &page,
2300 .pgbase = 0,
2301 .count = count,
2302 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2303 };
2304 struct nfs4_readdir_res res;
2305 struct rpc_message msg = {
2306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2307 .rpc_argp = &args,
2308 .rpc_resp = &res,
2309 .rpc_cred = cred,
2310 };
2311 int status;
2312
Harvey Harrison3110ff82008-05-02 13:42:44 -07002313 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002314 dentry->d_parent->d_name.name,
2315 dentry->d_name.name,
2316 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2318 res.pgbase = args.pgbase;
2319 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2320 if (status == 0)
2321 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002322
2323 nfs_invalidate_atime(dir);
2324
Harvey Harrison3110ff82008-05-02 13:42:44 -07002325 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return status;
2327}
2328
2329static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2330 u64 cookie, struct page *page, unsigned int count, int plus)
2331{
2332 struct nfs4_exception exception = { };
2333 int err;
2334 do {
2335 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2336 _nfs4_proc_readdir(dentry, cred, cookie,
2337 page, count, plus),
2338 &exception);
2339 } while (exception.retry);
2340 return err;
2341}
2342
2343static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2344 struct iattr *sattr, dev_t rdev)
2345{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002346 struct nfs4_createdata *data;
2347 int mode = sattr->ia_mode;
2348 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2351 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002352
2353 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2354 if (data == NULL)
2355 goto out;
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002358 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002360 data->arg.ftype = NF4BLK;
2361 data->arg.u.device.specdata1 = MAJOR(rdev);
2362 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002365 data->arg.ftype = NF4CHR;
2366 data->arg.u.device.specdata1 = MAJOR(rdev);
2367 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002370 status = nfs4_do_create(dir, dentry, data);
2371
2372 nfs4_free_createdata(data);
2373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return status;
2375}
2376
2377static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2378 struct iattr *sattr, dev_t rdev)
2379{
2380 struct nfs4_exception exception = { };
2381 int err;
2382 do {
2383 err = nfs4_handle_exception(NFS_SERVER(dir),
2384 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2385 &exception);
2386 } while (exception.retry);
2387 return err;
2388}
2389
2390static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2391 struct nfs_fsstat *fsstat)
2392{
2393 struct nfs4_statfs_arg args = {
2394 .fh = fhandle,
2395 .bitmask = server->attr_bitmask,
2396 };
2397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2399 .rpc_argp = &args,
2400 .rpc_resp = fsstat,
2401 };
2402
Trond Myklebust0e574af2005-10-27 22:12:38 -04002403 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return rpc_call_sync(server->client, &msg, 0);
2405}
2406
2407static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2408{
2409 struct nfs4_exception exception = { };
2410 int err;
2411 do {
2412 err = nfs4_handle_exception(server,
2413 _nfs4_proc_statfs(server, fhandle, fsstat),
2414 &exception);
2415 } while (exception.retry);
2416 return err;
2417}
2418
2419static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *fsinfo)
2421{
2422 struct nfs4_fsinfo_arg args = {
2423 .fh = fhandle,
2424 .bitmask = server->attr_bitmask,
2425 };
2426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2428 .rpc_argp = &args,
2429 .rpc_resp = fsinfo,
2430 };
2431
2432 return rpc_call_sync(server->client, &msg, 0);
2433}
2434
2435static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2436{
2437 struct nfs4_exception exception = { };
2438 int err;
2439
2440 do {
2441 err = nfs4_handle_exception(server,
2442 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2443 &exception);
2444 } while (exception.retry);
2445 return err;
2446}
2447
2448static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2449{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002450 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2452}
2453
2454static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2455 struct nfs_pathconf *pathconf)
2456{
2457 struct nfs4_pathconf_arg args = {
2458 .fh = fhandle,
2459 .bitmask = server->attr_bitmask,
2460 };
2461 struct rpc_message msg = {
2462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2463 .rpc_argp = &args,
2464 .rpc_resp = pathconf,
2465 };
2466
2467 /* None of the pathconf attributes are mandatory to implement */
2468 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2469 memset(pathconf, 0, sizeof(*pathconf));
2470 return 0;
2471 }
2472
Trond Myklebust0e574af2005-10-27 22:12:38 -04002473 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return rpc_call_sync(server->client, &msg, 0);
2475}
2476
2477static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2478 struct nfs_pathconf *pathconf)
2479{
2480 struct nfs4_exception exception = { };
2481 int err;
2482
2483 do {
2484 err = nfs4_handle_exception(server,
2485 _nfs4_proc_pathconf(server, fhandle, pathconf),
2486 &exception);
2487 } while (exception.retry);
2488 return err;
2489}
2490
Trond Myklebustec06c092006-03-20 13:44:27 -05002491static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Trond Myklebustec06c092006-03-20 13:44:27 -05002493 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Trond Myklebustec06c092006-03-20 13:44:27 -05002495 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002497 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002499
2500 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002502 renew_lease(server, data->timestamp);
2503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002506static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002509 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Trond Myklebust788e7a82006-03-20 13:44:27 -05002512static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct inode *inode = data->inode;
2515
2516 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2517 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002518 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002520 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002522 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002523 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002527static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002529 struct nfs_server *server = NFS_SERVER(data->inode);
2530
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002531 data->args.bitmask = server->attr_bitmask;
2532 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 data->timestamp = jiffies;
2534
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002535 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Trond Myklebust788e7a82006-03-20 13:44:27 -05002538static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct inode *inode = data->inode;
2541
2542 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2543 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002544 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002546 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002550static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002552 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002554 data->args.bitmask = server->attr_bitmask;
2555 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002556 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559/*
2560 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2561 * standalone procedure for queueing an asynchronous RENEW.
2562 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002563static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
David Howellsadfa6f92006-08-22 20:06:08 -04002565 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002566 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 if (task->tk_status < 0) {
2569 switch (task->tk_status) {
2570 case -NFS4ERR_STALE_CLIENTID:
2571 case -NFS4ERR_EXPIRED:
2572 case -NFS4ERR_CB_PATH_DOWN:
2573 nfs4_schedule_state_recovery(clp);
2574 }
2575 return;
2576 }
2577 spin_lock(&clp->cl_lock);
2578 if (time_before(clp->cl_last_renewal,timestamp))
2579 clp->cl_last_renewal = timestamp;
2580 spin_unlock(&clp->cl_lock);
2581}
2582
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002583static const struct rpc_call_ops nfs4_renew_ops = {
2584 .rpc_call_done = nfs4_renew_done,
2585};
2586
David Howellsadfa6f92006-08-22 20:06:08 -04002587int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 struct rpc_message msg = {
2590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2591 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002592 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 };
2594
2595 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002596 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
David Howellsadfa6f92006-08-22 20:06:08 -04002599int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 struct rpc_message msg = {
2602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2603 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002604 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 };
2606 unsigned long now = jiffies;
2607 int status;
2608
2609 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2610 if (status < 0)
2611 return status;
2612 spin_lock(&clp->cl_lock);
2613 if (time_before(clp->cl_last_renewal,now))
2614 clp->cl_last_renewal = now;
2615 spin_unlock(&clp->cl_lock);
2616 return 0;
2617}
2618
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002619static inline int nfs4_server_supports_acls(struct nfs_server *server)
2620{
2621 return (server->caps & NFS_CAP_ACLS)
2622 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2623 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2624}
2625
2626/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2627 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2628 * the stack.
2629 */
2630#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2631
2632static void buf_to_pages(const void *buf, size_t buflen,
2633 struct page **pages, unsigned int *pgbase)
2634{
2635 const void *p = buf;
2636
2637 *pgbase = offset_in_page(buf);
2638 p -= *pgbase;
2639 while (p < buf + buflen) {
2640 *(pages++) = virt_to_page(p);
2641 p += PAGE_CACHE_SIZE;
2642 }
2643}
2644
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002645struct nfs4_cached_acl {
2646 int cached;
2647 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002648 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002649};
2650
2651static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002652{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002653 struct nfs_inode *nfsi = NFS_I(inode);
2654
2655 spin_lock(&inode->i_lock);
2656 kfree(nfsi->nfs4_acl);
2657 nfsi->nfs4_acl = acl;
2658 spin_unlock(&inode->i_lock);
2659}
2660
2661static void nfs4_zap_acl_attr(struct inode *inode)
2662{
2663 nfs4_set_cached_acl(inode, NULL);
2664}
2665
2666static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2667{
2668 struct nfs_inode *nfsi = NFS_I(inode);
2669 struct nfs4_cached_acl *acl;
2670 int ret = -ENOENT;
2671
2672 spin_lock(&inode->i_lock);
2673 acl = nfsi->nfs4_acl;
2674 if (acl == NULL)
2675 goto out;
2676 if (buf == NULL) /* user is just asking for length */
2677 goto out_len;
2678 if (acl->cached == 0)
2679 goto out;
2680 ret = -ERANGE; /* see getxattr(2) man page */
2681 if (acl->len > buflen)
2682 goto out;
2683 memcpy(buf, acl->data, acl->len);
2684out_len:
2685 ret = acl->len;
2686out:
2687 spin_unlock(&inode->i_lock);
2688 return ret;
2689}
2690
2691static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2692{
2693 struct nfs4_cached_acl *acl;
2694
2695 if (buf && acl_len <= PAGE_SIZE) {
2696 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2697 if (acl == NULL)
2698 goto out;
2699 acl->cached = 1;
2700 memcpy(acl->data, buf, acl_len);
2701 } else {
2702 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2703 if (acl == NULL)
2704 goto out;
2705 acl->cached = 0;
2706 }
2707 acl->len = acl_len;
2708out:
2709 nfs4_set_cached_acl(inode, acl);
2710}
2711
Trond Myklebust16b42892006-08-24 12:27:15 -04002712static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002713{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002714 struct page *pages[NFS4ACL_MAXPAGES];
2715 struct nfs_getaclargs args = {
2716 .fh = NFS_FH(inode),
2717 .acl_pages = pages,
2718 .acl_len = buflen,
2719 };
2720 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002721 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002722 struct rpc_message msg = {
2723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2724 .rpc_argp = &args,
2725 .rpc_resp = &resp_len,
2726 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002727 struct page *localpage = NULL;
2728 int ret;
2729
2730 if (buflen < PAGE_SIZE) {
2731 /* As long as we're doing a round trip to the server anyway,
2732 * let's be prepared for a page of acl data. */
2733 localpage = alloc_page(GFP_KERNEL);
2734 resp_buf = page_address(localpage);
2735 if (localpage == NULL)
2736 return -ENOMEM;
2737 args.acl_pages[0] = localpage;
2738 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002739 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002740 } else {
2741 resp_buf = buf;
2742 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2743 }
2744 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2745 if (ret)
2746 goto out_free;
2747 if (resp_len > args.acl_len)
2748 nfs4_write_cached_acl(inode, NULL, resp_len);
2749 else
2750 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2751 if (buf) {
2752 ret = -ERANGE;
2753 if (resp_len > buflen)
2754 goto out_free;
2755 if (localpage)
2756 memcpy(buf, resp_buf, resp_len);
2757 }
2758 ret = resp_len;
2759out_free:
2760 if (localpage)
2761 __free_page(localpage);
2762 return ret;
2763}
2764
Trond Myklebust16b42892006-08-24 12:27:15 -04002765static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2766{
2767 struct nfs4_exception exception = { };
2768 ssize_t ret;
2769 do {
2770 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2771 if (ret >= 0)
2772 break;
2773 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2774 } while (exception.retry);
2775 return ret;
2776}
2777
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002778static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2779{
2780 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002781 int ret;
2782
2783 if (!nfs4_server_supports_acls(server))
2784 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002785 ret = nfs_revalidate_inode(server, inode);
2786 if (ret < 0)
2787 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04002788 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2789 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002790 ret = nfs4_read_cached_acl(inode, buf, buflen);
2791 if (ret != -ENOENT)
2792 return ret;
2793 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002794}
2795
Trond Myklebust16b42892006-08-24 12:27:15 -04002796static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002797{
2798 struct nfs_server *server = NFS_SERVER(inode);
2799 struct page *pages[NFS4ACL_MAXPAGES];
2800 struct nfs_setaclargs arg = {
2801 .fh = NFS_FH(inode),
2802 .acl_pages = pages,
2803 .acl_len = buflen,
2804 };
2805 struct rpc_message msg = {
2806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2807 .rpc_argp = &arg,
2808 .rpc_resp = NULL,
2809 };
2810 int ret;
2811
2812 if (!nfs4_server_supports_acls(server))
2813 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002814 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002815 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002816 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustf41f7412008-06-11 17:39:04 -04002817 nfs_access_zap_cache(inode);
2818 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002819 return ret;
2820}
2821
Trond Myklebust16b42892006-08-24 12:27:15 -04002822static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2823{
2824 struct nfs4_exception exception = { };
2825 int err;
2826 do {
2827 err = nfs4_handle_exception(NFS_SERVER(inode),
2828 __nfs4_proc_set_acl(inode, buf, buflen),
2829 &exception);
2830 } while (exception.retry);
2831 return err;
2832}
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002835nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
David Howells7539bba2006-08-22 20:06:09 -04002837 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 if (!clp || task->tk_status >= 0)
2840 return 0;
2841 switch(task->tk_status) {
2842 case -NFS4ERR_STALE_CLIENTID:
2843 case -NFS4ERR_STALE_STATEID:
2844 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05002845 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002847 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05002848 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 task->tk_status = 0;
2850 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04002852 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05002853 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2855 task->tk_status = 0;
2856 return -EAGAIN;
2857 case -NFS4ERR_OLD_STATEID:
2858 task->tk_status = 0;
2859 return -EAGAIN;
2860 }
2861 task->tk_status = nfs4_map_errors(task->tk_status);
2862 return 0;
2863}
2864
David Howellsadfa6f92006-08-22 20:06:08 -04002865int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 nfs4_verifier sc_verifier;
2868 struct nfs4_setclientid setclientid = {
2869 .sc_verifier = &sc_verifier,
2870 .sc_prog = program,
2871 };
2872 struct rpc_message msg = {
2873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2874 .rpc_argp = &setclientid,
2875 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002876 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 };
Al Virobc4785c2006-10-19 23:28:51 -07002878 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int loop = 0;
2880 int status;
2881
Al Virobc4785c2006-10-19 23:28:51 -07002882 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2884 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2885
2886 for(;;) {
2887 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05002888 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05002889 clp->cl_ipaddr,
2890 rpc_peeraddr2str(clp->cl_rpcclient,
2891 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05002892 rpc_peeraddr2str(clp->cl_rpcclient,
2893 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04002894 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 clp->cl_id_uniquifier);
2896 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05002897 sizeof(setclientid.sc_netid),
2898 rpc_peeraddr2str(clp->cl_rpcclient,
2899 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05002901 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 clp->cl_ipaddr, port >> 8, port & 255);
2903
2904 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2905 if (status != -NFS4ERR_CLID_INUSE)
2906 break;
2907 if (signalled())
2908 break;
2909 if (loop++ & 1)
2910 ssleep(clp->cl_lease_time + 1);
2911 else
2912 if (++clp->cl_id_uniquifier == 0)
2913 break;
2914 }
2915 return status;
2916}
2917
David Howellsadfa6f92006-08-22 20:06:08 -04002918static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 struct nfs_fsinfo fsinfo;
2921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2923 .rpc_argp = clp,
2924 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002925 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 };
2927 unsigned long now;
2928 int status;
2929
2930 now = jiffies;
2931 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2932 if (status == 0) {
2933 spin_lock(&clp->cl_lock);
2934 clp->cl_lease_time = fsinfo.lease_time * HZ;
2935 clp->cl_last_renewal = now;
2936 spin_unlock(&clp->cl_lock);
2937 }
2938 return status;
2939}
2940
David Howellsadfa6f92006-08-22 20:06:08 -04002941int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002942{
Benny Halevy77e03672008-06-24 20:25:57 +03002943 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05002944 int err;
2945 do {
2946 err = _nfs4_proc_setclientid_confirm(clp, cred);
2947 switch (err) {
2948 case 0:
2949 return err;
2950 case -NFS4ERR_RESOURCE:
2951 /* The IBM lawyers misread another document! */
2952 case -NFS4ERR_DELAY:
2953 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2954 }
2955 } while (err == 0);
2956 return err;
2957}
2958
Trond Myklebustfe650402006-01-03 09:55:18 +01002959struct nfs4_delegreturndata {
2960 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002961 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002962 struct nfs_fh fh;
2963 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002964 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002965 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002966 int rpc_status;
2967};
2968
Trond Myklebustfe650402006-01-03 09:55:18 +01002969static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2970{
2971 struct nfs4_delegreturndata *data = calldata;
2972 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002973 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002974 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002975}
2976
2977static void nfs4_delegreturn_release(void *calldata)
2978{
Trond Myklebustfe650402006-01-03 09:55:18 +01002979 kfree(calldata);
2980}
2981
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002982static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002983 .rpc_call_done = nfs4_delegreturn_done,
2984 .rpc_release = nfs4_delegreturn_release,
2985};
2986
Trond Myklebuste6f81072008-01-24 18:14:34 -05002987static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01002988{
2989 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002990 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002991 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2994 .rpc_cred = cred,
2995 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002996 struct rpc_task_setup task_setup_data = {
2997 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002998 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002999 .callback_ops = &nfs4_delegreturn_ops,
3000 .flags = RPC_TASK_ASYNC,
3001 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003002 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003003
3004 data = kmalloc(sizeof(*data), GFP_KERNEL);
3005 if (data == NULL)
3006 return -ENOMEM;
3007 data->args.fhandle = &data->fh;
3008 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003009 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003010 nfs_copy_fh(&data->fh, NFS_FH(inode));
3011 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003012 data->res.fattr = &data->fattr;
3013 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003014 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003015 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003016 data->rpc_status = 0;
3017
Trond Myklebustc970aa82007-07-14 15:39:59 -04003018 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003019 msg.rpc_argp = &data->args,
3020 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003021 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003022 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003023 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003024 if (!issync)
3025 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003026 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003027 if (status != 0)
3028 goto out;
3029 status = data->rpc_status;
3030 if (status != 0)
3031 goto out;
3032 nfs_refresh_inode(inode, &data->fattr);
3033out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003034 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003035 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Trond Myklebuste6f81072008-01-24 18:14:34 -05003038int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 struct nfs_server *server = NFS_SERVER(inode);
3041 struct nfs4_exception exception = { };
3042 int err;
3043 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003044 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 switch (err) {
3046 case -NFS4ERR_STALE_STATEID:
3047 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 case 0:
3049 return 0;
3050 }
3051 err = nfs4_handle_exception(server, err, &exception);
3052 } while (exception.retry);
3053 return err;
3054}
3055
3056#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3057#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3058
3059/*
3060 * sleep, with exponential backoff, and retry the LOCK operation.
3061 */
3062static unsigned long
3063nfs4_set_lock_task_retry(unsigned long timeout)
3064{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003065 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 timeout <<= 1;
3067 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3068 return NFS4_LOCK_MAXTIMEOUT;
3069 return timeout;
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3073{
3074 struct inode *inode = state->inode;
3075 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003076 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003077 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003079 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003081 struct nfs_lockt_res res = {
3082 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
3084 struct rpc_message msg = {
3085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3086 .rpc_argp = &arg,
3087 .rpc_resp = &res,
3088 .rpc_cred = state->owner->so_cred,
3089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct nfs4_lock_state *lsp;
3091 int status;
3092
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003093 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003094 status = nfs4_set_lock_state(state, request);
3095 if (status != 0)
3096 goto out;
3097 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003098 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003100 switch (status) {
3101 case 0:
3102 request->fl_type = F_UNLCK;
3103 break;
3104 case -NFS4ERR_DENIED:
3105 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003107 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003108out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return status;
3110}
3111
3112static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3113{
3114 struct nfs4_exception exception = { };
3115 int err;
3116
3117 do {
3118 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3119 _nfs4_proc_getlk(state, cmd, request),
3120 &exception);
3121 } while (exception.retry);
3122 return err;
3123}
3124
3125static int do_vfs_lock(struct file *file, struct file_lock *fl)
3126{
3127 int res = 0;
3128 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3129 case FL_POSIX:
3130 res = posix_lock_file_wait(file, fl);
3131 break;
3132 case FL_FLOCK:
3133 res = flock_lock_file_wait(file, fl);
3134 break;
3135 default:
3136 BUG();
3137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return res;
3139}
3140
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003141struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003142 struct nfs_locku_args arg;
3143 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003144 struct nfs4_lock_state *lsp;
3145 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003146 struct file_lock fl;
3147 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003148 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003149};
3150
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003151static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3152 struct nfs_open_context *ctx,
3153 struct nfs4_lock_state *lsp,
3154 struct nfs_seqid *seqid)
3155{
3156 struct nfs4_unlockdata *p;
3157 struct inode *inode = lsp->ls_state->inode;
3158
3159 p = kmalloc(sizeof(*p), GFP_KERNEL);
3160 if (p == NULL)
3161 return NULL;
3162 p->arg.fh = NFS_FH(inode);
3163 p->arg.fl = &p->fl;
3164 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003165 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003166 p->arg.stateid = &lsp->ls_stateid;
3167 p->lsp = lsp;
3168 atomic_inc(&lsp->ls_count);
3169 /* Ensure we don't close file until we're done freeing locks! */
3170 p->ctx = get_nfs_open_context(ctx);
3171 memcpy(&p->fl, fl, sizeof(p->fl));
3172 p->server = NFS_SERVER(inode);
3173 return p;
3174}
3175
Trond Myklebust06f814a2006-01-03 09:55:07 +01003176static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003177{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003179 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003180 nfs4_put_lock_state(calldata->lsp);
3181 put_nfs_open_context(calldata->ctx);
3182 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003183}
3184
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003185static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003186{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003187 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003188
Trond Myklebust06f814a2006-01-03 09:55:07 +01003189 if (RPC_ASSASSINATED(task))
3190 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003191 switch (task->tk_status) {
3192 case 0:
3193 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003194 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003195 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003196 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003197 break;
3198 case -NFS4ERR_STALE_STATEID:
3199 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003200 break;
3201 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003202 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003203 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003204 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003205}
3206
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003207static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003208{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003209 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003211 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003212 return;
3213 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003214 /* Note: exit _without_ running nfs4_locku_done */
3215 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003216 return;
3217 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003218 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003219 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003222static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003223 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003225 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003226};
3227
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003228static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3229 struct nfs_open_context *ctx,
3230 struct nfs4_lock_state *lsp,
3231 struct nfs_seqid *seqid)
3232{
3233 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3236 .rpc_cred = ctx->cred,
3237 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003238 struct rpc_task_setup task_setup_data = {
3239 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003240 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003241 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003242 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003243 .flags = RPC_TASK_ASYNC,
3244 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003245
Frank Filz137d6ac2007-07-09 15:32:29 -07003246 /* Ensure this is an unlock - when canceling a lock, the
3247 * canceled lock is passed in, and it won't be an unlock.
3248 */
3249 fl->fl_type = F_UNLCK;
3250
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003251 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3252 if (data == NULL) {
3253 nfs_free_seqid(seqid);
3254 return ERR_PTR(-ENOMEM);
3255 }
3256
Trond Myklebust5138fde2007-07-14 15:40:01 -04003257 msg.rpc_argp = &data->arg,
3258 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003259 task_setup_data.callback_data = data;
3260 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3264{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003265 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003266 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003267 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003268 struct rpc_task *task;
3269 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003270 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Trond Myklebust9b073572006-06-29 16:38:34 -04003272 status = nfs4_set_lock_state(state, request);
3273 /* Unlock _before_ we do the RPC call */
3274 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003275 down_read(&nfsi->rwsem);
3276 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3277 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003278 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003279 }
3280 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003281 if (status != 0)
3282 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003283 /* Is this a delegated lock? */
3284 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003285 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003286 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003287 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003288 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003289 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003290 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003291 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003292 status = PTR_ERR(task);
3293 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003294 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003295 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003296 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003297out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003298 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003299 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003302struct nfs4_lockdata {
3303 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003304 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003305 struct nfs4_lock_state *lsp;
3306 struct nfs_open_context *ctx;
3307 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003308 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003309 int rpc_status;
3310 int cancelled;
3311};
3312
3313static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3314 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3315{
3316 struct nfs4_lockdata *p;
3317 struct inode *inode = lsp->ls_state->inode;
3318 struct nfs_server *server = NFS_SERVER(inode);
3319
3320 p = kzalloc(sizeof(*p), GFP_KERNEL);
3321 if (p == NULL)
3322 return NULL;
3323
3324 p->arg.fh = NFS_FH(inode);
3325 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003326 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3327 if (p->arg.open_seqid == NULL)
3328 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003329 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3330 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003331 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003332 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003333 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003334 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003335 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003336 p->lsp = lsp;
3337 atomic_inc(&lsp->ls_count);
3338 p->ctx = get_nfs_open_context(ctx);
3339 memcpy(&p->fl, fl, sizeof(p->fl));
3340 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003341out_free_seqid:
3342 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003343out_free:
3344 kfree(p);
3345 return NULL;
3346}
3347
3348static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3349{
3350 struct nfs4_lockdata *data = calldata;
3351 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Harvey Harrison3110ff82008-05-02 13:42:44 -07003353 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003354 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3355 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003356 /* Do we need to do an open_to_lock_owner? */
3357 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003358 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3359 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003360 data->arg.open_stateid = &state->stateid;
3361 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003362 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003363 } else
3364 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003365 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003366 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003367 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003368}
3369
3370static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3371{
3372 struct nfs4_lockdata *data = calldata;
3373
Harvey Harrison3110ff82008-05-02 13:42:44 -07003374 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003375
3376 data->rpc_status = task->tk_status;
3377 if (RPC_ASSASSINATED(task))
3378 goto out;
3379 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003380 if (data->rpc_status == 0)
3381 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3382 else
3383 goto out;
3384 }
3385 if (data->rpc_status == 0) {
3386 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3387 sizeof(data->lsp->ls_stateid.data));
3388 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003389 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003390 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003391out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003392 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003393}
3394
3395static void nfs4_lock_release(void *calldata)
3396{
3397 struct nfs4_lockdata *data = calldata;
3398
Harvey Harrison3110ff82008-05-02 13:42:44 -07003399 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003400 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003401 if (data->cancelled != 0) {
3402 struct rpc_task *task;
3403 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3404 data->arg.lock_seqid);
3405 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003406 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003407 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003408 } else
3409 nfs_free_seqid(data->arg.lock_seqid);
3410 nfs4_put_lock_state(data->lsp);
3411 put_nfs_open_context(data->ctx);
3412 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003413 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003414}
3415
3416static const struct rpc_call_ops nfs4_lock_ops = {
3417 .rpc_call_prepare = nfs4_lock_prepare,
3418 .rpc_call_done = nfs4_lock_done,
3419 .rpc_release = nfs4_lock_release,
3420};
3421
3422static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3423{
3424 struct nfs4_lockdata *data;
3425 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003426 struct rpc_message msg = {
3427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3428 .rpc_cred = state->owner->so_cred,
3429 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003430 struct rpc_task_setup task_setup_data = {
3431 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003432 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003433 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003434 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003435 .flags = RPC_TASK_ASYNC,
3436 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003437 int ret;
3438
Harvey Harrison3110ff82008-05-02 13:42:44 -07003439 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003440 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003441 fl->fl_u.nfs4_fl.owner);
3442 if (data == NULL)
3443 return -ENOMEM;
3444 if (IS_SETLKW(cmd))
3445 data->arg.block = 1;
3446 if (reclaim != 0)
3447 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003448 msg.rpc_argp = &data->arg,
3449 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003450 task_setup_data.callback_data = data;
3451 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003452 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003453 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003454 ret = nfs4_wait_for_completion_rpc_task(task);
3455 if (ret == 0) {
3456 ret = data->rpc_status;
3457 if (ret == -NFS4ERR_DENIED)
3458 ret = -EAGAIN;
3459 } else
3460 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003461 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003462 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
3466static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3467{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003468 struct nfs_server *server = NFS_SERVER(state->inode);
3469 struct nfs4_exception exception = { };
3470 int err;
3471
3472 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003473 /* Cache the lock if possible... */
3474 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3475 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003476 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3477 if (err != -NFS4ERR_DELAY)
3478 break;
3479 nfs4_handle_exception(server, err, &exception);
3480 } while (exception.retry);
3481 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
3484static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3485{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003486 struct nfs_server *server = NFS_SERVER(state->inode);
3487 struct nfs4_exception exception = { };
3488 int err;
3489
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003490 err = nfs4_set_lock_state(state, request);
3491 if (err != 0)
3492 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003493 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003494 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3495 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003496 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3497 if (err != -NFS4ERR_DELAY)
3498 break;
3499 nfs4_handle_exception(server, err, &exception);
3500 } while (exception.retry);
3501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3505{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003506 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003507 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 int status;
3509
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003510 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003511 status = nfs4_set_lock_state(state, request);
3512 if (status != 0)
3513 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003514 request->fl_flags |= FL_ACCESS;
3515 status = do_vfs_lock(request->fl_file, request);
3516 if (status < 0)
3517 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003518 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003519 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003520 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003521 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003522 request->fl_flags = fl_flags & ~FL_SLEEP;
3523 status = do_vfs_lock(request->fl_file, request);
3524 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003525 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003526 status = _nfs4_do_setlk(state, cmd, request, 0);
3527 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003528 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003529 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003530 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003531 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003532 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003533out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003534 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003535out:
3536 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return status;
3538}
3539
3540static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3541{
3542 struct nfs4_exception exception = { };
3543 int err;
3544
3545 do {
3546 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3547 _nfs4_proc_setlk(state, cmd, request),
3548 &exception);
3549 } while (exception.retry);
3550 return err;
3551}
3552
3553static int
3554nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3555{
3556 struct nfs_open_context *ctx;
3557 struct nfs4_state *state;
3558 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3559 int status;
3560
3561 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003562 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 state = ctx->state;
3564
3565 if (request->fl_start < 0 || request->fl_end < 0)
3566 return -EINVAL;
3567
3568 if (IS_GETLK(cmd))
3569 return nfs4_proc_getlk(state, F_GETLK, request);
3570
3571 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3572 return -EINVAL;
3573
3574 if (request->fl_type == F_UNLCK)
3575 return nfs4_proc_unlck(state, cmd, request);
3576
3577 do {
3578 status = nfs4_proc_setlk(state, cmd, request);
3579 if ((status != -EAGAIN) || IS_SETLK(cmd))
3580 break;
3581 timeout = nfs4_set_lock_task_retry(timeout);
3582 status = -ERESTARTSYS;
3583 if (signalled())
3584 break;
3585 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return status;
3587}
3588
Trond Myklebust888e6942005-11-04 15:38:11 -05003589int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3590{
3591 struct nfs_server *server = NFS_SERVER(state->inode);
3592 struct nfs4_exception exception = { };
3593 int err;
3594
3595 err = nfs4_set_lock_state(state, fl);
3596 if (err != 0)
3597 goto out;
3598 do {
3599 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3600 if (err != -NFS4ERR_DELAY)
3601 break;
3602 err = nfs4_handle_exception(server, err, &exception);
3603 } while (exception.retry);
3604out:
3605 return err;
3606}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003607
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003608#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3609
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003610int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3611 size_t buflen, int flags)
3612{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003613 struct inode *inode = dentry->d_inode;
3614
3615 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3616 return -EOPNOTSUPP;
3617
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003618 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003619}
3620
3621/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3622 * and that's what we'll do for e.g. user attributes that haven't been set.
3623 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3624 * attributes in kernel-managed attribute namespaces. */
3625ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3626 size_t buflen)
3627{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003628 struct inode *inode = dentry->d_inode;
3629
3630 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3631 return -EOPNOTSUPP;
3632
3633 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003634}
3635
3636ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3637{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003638 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003639
J. Bruce Fields096455a2006-03-20 23:23:42 -05003640 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3641 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003642 if (buf && buflen < len)
3643 return -ERANGE;
3644 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003645 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3646 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003647}
3648
Trond Myklebust56659e92007-07-17 21:52:39 -04003649int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003650 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003651{
3652 struct nfs_server *server = NFS_SERVER(dir);
3653 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003654 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3655 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003656 };
3657 struct nfs4_fs_locations_arg args = {
3658 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003659 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003660 .page = page,
3661 .bitmask = bitmask,
3662 };
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3665 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003666 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003667 };
3668 int status;
3669
Harvey Harrison3110ff82008-05-02 13:42:44 -07003670 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003671 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003672 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003673 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003674 status = rpc_call_sync(server->client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003675 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003676 return status;
3677}
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003680 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003681 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .recover_open = nfs4_open_reclaim,
3683 .recover_lock = nfs4_lock_reclaim,
3684};
3685
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003686struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003687 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003688 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .recover_open = nfs4_open_expired,
3690 .recover_lock = nfs4_lock_expired,
3691};
3692
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003693static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003694 .permission = nfs_permission,
3695 .getattr = nfs_getattr,
3696 .setattr = nfs_setattr,
3697 .getxattr = nfs4_getxattr,
3698 .setxattr = nfs4_setxattr,
3699 .listxattr = nfs4_listxattr,
3700};
3701
David Howells509de812006-08-22 20:06:11 -04003702const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 .version = 4, /* protocol version */
3704 .dentry_ops = &nfs4_dentry_operations,
3705 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003706 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .getroot = nfs4_proc_get_root,
3708 .getattr = nfs4_proc_getattr,
3709 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003710 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .lookup = nfs4_proc_lookup,
3712 .access = nfs4_proc_access,
3713 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 .create = nfs4_proc_create,
3715 .remove = nfs4_proc_remove,
3716 .unlink_setup = nfs4_proc_unlink_setup,
3717 .unlink_done = nfs4_proc_unlink_done,
3718 .rename = nfs4_proc_rename,
3719 .link = nfs4_proc_link,
3720 .symlink = nfs4_proc_symlink,
3721 .mkdir = nfs4_proc_mkdir,
3722 .rmdir = nfs4_proc_remove,
3723 .readdir = nfs4_proc_readdir,
3724 .mknod = nfs4_proc_mknod,
3725 .statfs = nfs4_proc_statfs,
3726 .fsinfo = nfs4_proc_fsinfo,
3727 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003728 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 .decode_dirent = nfs4_decode_dirent,
3730 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003731 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003733 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003735 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738};
3739
3740/*
3741 * Local variables:
3742 * c-basic-offset: 8
3743 * End:
3744 */