blob: 5878930b4c3b6a1ba798093095e484c3f50439c4 [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 Myklebust9e33bed2008-12-23 15:21:46 -050065static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
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 */
WANG Cong46f72f52008-12-30 16:35:55 -050070static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
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_clnt_recover(struct nfs_client *clp)
197{
198 int res;
199
200 might_sleep();
201
Trond Myklebuste005e802008-12-23 15:21:48 -0500202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400203 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500204 return res;
205}
206
207static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
208{
209 int res = 0;
210
211 might_sleep();
212
213 if (*timeout <= 0)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
219 res = -ERESTARTSYS;
220 *timeout <<= 1;
221 return res;
222}
223
224/* This is the error handling routine for processes that are allowed
225 * to sleep.
226 */
227static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
228{
229 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500230 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500231 int ret = errorcode;
232
233 exception->retry = 0;
234 switch(errorcode) {
235 case 0:
236 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
240 if (state == NULL)
241 break;
242 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
248 if (ret == 0)
249 exception->retry = 1;
250 break;
251 case -NFS4ERR_FILE_OPEN:
252 case -NFS4ERR_GRACE:
253 case -NFS4ERR_DELAY:
254 ret = nfs4_delay(server->client, &exception->timeout);
255 if (ret != 0)
256 break;
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
259 }
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
262}
263
264
Trond Myklebust26e976a2006-01-03 09:55:21 +0100265static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David Howells7539bba2006-08-22 20:06:09 -0400267 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
272}
273
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400274#if defined(CONFIG_NFS_V4_1)
275
276int _nfs4_call_sync_session(struct nfs_server *server,
277 struct rpc_message *msg,
278 struct nfs4_sequence_args *args,
279 struct nfs4_sequence_res *res,
280 int cache_reply)
281{
282 /* in preparation for setting up the sequence op */
283 return rpc_call_sync(server->client, msg, 0);
284}
285
286#endif /* CONFIG_NFS_V4_1 */
287
288int _nfs4_call_sync(struct nfs_server *server,
289 struct rpc_message *msg,
290 struct nfs4_sequence_args *args,
291 struct nfs4_sequence_res *res,
292 int cache_reply)
293{
Benny Halevyf3752972009-04-01 09:22:04 -0400294 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400295 return rpc_call_sync(server->client, msg, 0);
296}
297
298#define nfs4_call_sync(server, msg, args, res, cache_reply) \
299 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
300 &(res)->seq_res, (cache_reply))
301
Trond Myklebust38478b22006-05-25 01:40:57 -0400302static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Trond Myklebust38478b22006-05-25 01:40:57 -0400304 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Trond Myklebust38478b22006-05-25 01:40:57 -0400306 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400307 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
308 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400309 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400310 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400311 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100314struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400315 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100316 struct nfs_openargs o_arg;
317 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100318 struct nfs_open_confirmargs c_arg;
319 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100320 struct nfs_fattr f_attr;
321 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400322 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100323 struct dentry *dir;
324 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400325 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100326 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100327 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400328 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100329 int rpc_status;
330 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100331};
332
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400333
334static void nfs4_init_opendata_res(struct nfs4_opendata *p)
335{
336 p->o_res.f_attr = &p->f_attr;
337 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400338 p->o_res.seqid = p->o_arg.seqid;
339 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400340 p->o_res.server = p->o_arg.server;
341 nfs_fattr_init(&p->f_attr);
342 nfs_fattr_init(&p->dir_attr);
343}
344
Trond Myklebustad389da2007-06-05 12:30:00 -0400345static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500346 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100347 const struct iattr *attrs)
348{
Trond Myklebustad389da2007-06-05 12:30:00 -0400349 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100350 struct inode *dir = parent->d_inode;
351 struct nfs_server *server = NFS_SERVER(dir);
352 struct nfs4_opendata *p;
353
354 p = kzalloc(sizeof(*p), GFP_KERNEL);
355 if (p == NULL)
356 goto err;
357 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
358 if (p->o_arg.seqid == NULL)
359 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400360 p->path.mnt = mntget(path->mnt);
361 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100362 p->dir = parent;
363 p->owner = sp;
364 atomic_inc(&sp->so_count);
365 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500366 p->o_arg.open_flags = flags;
367 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400368 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400369 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400370 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100371 p->o_arg.server = server;
372 p->o_arg.bitmask = server->attr_bitmask;
373 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400374 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100375 if (flags & O_EXCL) {
376 u32 *s = (u32 *) p->o_arg.u.verifier.data;
377 s[0] = jiffies;
378 s[1] = current->pid;
379 } else if (flags & O_CREAT) {
380 p->o_arg.u.attrs = &p->attrs;
381 memcpy(&p->attrs, attrs, sizeof(p->attrs));
382 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100383 p->c_arg.fh = &p->o_res.fh;
384 p->c_arg.stateid = &p->o_res.stateid;
385 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400386 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400387 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100388 return p;
389err_free:
390 kfree(p);
391err:
392 dput(parent);
393 return NULL;
394}
395
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400396static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100397{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400398 struct nfs4_opendata *p = container_of(kref,
399 struct nfs4_opendata, kref);
400
401 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400402 if (p->state != NULL)
403 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400404 nfs4_put_state_owner(p->owner);
405 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700406 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400407 kfree(p);
408}
409
410static void nfs4_opendata_put(struct nfs4_opendata *p)
411{
412 if (p != NULL)
413 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100414}
415
Trond Myklebust06f814a2006-01-03 09:55:07 +0100416static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
417{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100418 int ret;
419
Trond Myklebust06f814a2006-01-03 09:55:07 +0100420 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100421 return ret;
422}
423
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500424static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400425{
426 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500427
428 if (open_mode & O_EXCL)
429 goto out;
430 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400431 case FMODE_READ:
432 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400433 break;
434 case FMODE_WRITE:
435 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400436 break;
437 case FMODE_READ|FMODE_WRITE:
438 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
439 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500440out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400441 return ret;
442}
443
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500444static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400445{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500446 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400447 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500448 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400449 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500450 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400451 return 1;
452}
453
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500454static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100455{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500456 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100457 case FMODE_WRITE:
458 state->n_wronly++;
459 break;
460 case FMODE_READ:
461 state->n_rdonly++;
462 break;
463 case FMODE_READ|FMODE_WRITE:
464 state->n_rdwr++;
465 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500466 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100467}
468
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500469static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400470{
471 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
472 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
473 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500474 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400475 case FMODE_READ:
476 set_bit(NFS_O_RDONLY_STATE, &state->flags);
477 break;
478 case FMODE_WRITE:
479 set_bit(NFS_O_WRONLY_STATE, &state->flags);
480 break;
481 case FMODE_READ|FMODE_WRITE:
482 set_bit(NFS_O_RDWR_STATE, &state->flags);
483 }
484}
485
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500486static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400487{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400488 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500489 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400490 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400491}
492
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500493static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400495 /*
496 * Protect the call to nfs4_state_set_mode_locked and
497 * serialise the stateid update
498 */
499 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400500 if (deleg_stateid != NULL) {
501 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
502 set_bit(NFS_DELEGATED_STATE, &state->flags);
503 }
504 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500505 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400506 write_sequnlock(&state->seqlock);
507 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500508 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700509 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500512static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500513{
514 struct nfs_inode *nfsi = NFS_I(state->inode);
515 struct nfs_delegation *deleg_cur;
516 int ret = 0;
517
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500518 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500519
520 rcu_read_lock();
521 deleg_cur = rcu_dereference(nfsi->delegation);
522 if (deleg_cur == NULL)
523 goto no_delegation;
524
525 spin_lock(&deleg_cur->lock);
526 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500527 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500528 goto no_delegation_unlock;
529
530 if (delegation == NULL)
531 delegation = &deleg_cur->stateid;
532 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
533 goto no_delegation_unlock;
534
Trond Myklebustb7391f42008-12-23 15:21:52 -0500535 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500536 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500537 ret = 1;
538no_delegation_unlock:
539 spin_unlock(&deleg_cur->lock);
540no_delegation:
541 rcu_read_unlock();
542
543 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500544 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500545 ret = 1;
546 }
547
548 return ret;
549}
550
551
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500552static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400553{
554 struct nfs_delegation *delegation;
555
556 rcu_read_lock();
557 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500558 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400559 rcu_read_unlock();
560 return;
561 }
562 rcu_read_unlock();
563 nfs_inode_return_delegation(inode);
564}
565
Trond Myklebust6ee41262007-07-08 14:11:36 -0400566static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400567{
568 struct nfs4_state *state = opendata->state;
569 struct nfs_inode *nfsi = NFS_I(state->inode);
570 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500571 int open_mode = opendata->o_arg.open_flags & O_EXCL;
572 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400573 nfs4_stateid stateid;
574 int ret = -EAGAIN;
575
Trond Myklebustaac00a82007-07-05 19:02:21 -0400576 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500577 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400578 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500579 if (can_open_cached(state, fmode, open_mode)) {
580 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400581 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400582 goto out_return_state;
583 }
584 spin_unlock(&state->owner->so_lock);
585 }
Trond Myklebust34310432008-12-23 15:21:38 -0500586 rcu_read_lock();
587 delegation = rcu_dereference(nfsi->delegation);
588 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500589 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500590 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400591 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500592 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400593 /* Save the delegation */
594 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
595 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400596 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400597 if (ret != 0)
598 goto out;
599 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500600
601 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500602 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500603 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400604 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400605out:
606 return ERR_PTR(ret);
607out_return_state:
608 atomic_inc(&state->count);
609 return state;
610}
611
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100612static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
613{
614 struct inode *inode;
615 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400616 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400617 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100618
Trond Myklebustaac00a82007-07-05 19:02:21 -0400619 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400620 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400621 goto out;
622 }
623
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400624 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100625 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400626 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100627 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400628 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500629 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400630 goto err;
631 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100632 state = nfs4_get_open_state(inode, data->owner);
633 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400634 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400635 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400636 int delegation_flags = 0;
637
Trond Myklebust003707c2007-07-05 18:07:55 -0400638 rcu_read_lock();
639 delegation = rcu_dereference(NFS_I(inode)->delegation);
640 if (delegation)
641 delegation_flags = delegation->flags;
642 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500643 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400644 nfs_inode_set_delegation(state->inode,
645 data->owner->so_cred,
646 &data->o_res);
647 else
648 nfs_inode_reclaim_delegation(state->inode,
649 data->owner->so_cred,
650 &data->o_res);
651 }
Trond Myklebust34310432008-12-23 15:21:38 -0500652
653 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500654 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100655 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400656out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100657 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400658err_put_inode:
659 iput(inode);
660err:
661 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100662}
663
Trond Myklebust864472e2006-01-03 09:55:15 +0100664static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
665{
666 struct nfs_inode *nfsi = NFS_I(state->inode);
667 struct nfs_open_context *ctx;
668
669 spin_lock(&state->inode->i_lock);
670 list_for_each_entry(ctx, &nfsi->open_files, list) {
671 if (ctx->state != state)
672 continue;
673 get_nfs_open_context(ctx);
674 spin_unlock(&state->inode->i_lock);
675 return ctx;
676 }
677 spin_unlock(&state->inode->i_lock);
678 return ERR_PTR(-ENOENT);
679}
680
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400681static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
682{
683 struct nfs4_opendata *opendata;
684
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500685 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400686 if (opendata == NULL)
687 return ERR_PTR(-ENOMEM);
688 opendata->state = state;
689 atomic_inc(&state->count);
690 return opendata;
691}
692
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500693static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100694{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400695 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100696 int ret;
697
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500698 opendata->o_arg.open_flags = 0;
699 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400700 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
701 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
702 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100703 ret = _nfs4_proc_open(opendata);
704 if (ret != 0)
705 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400706 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400707 if (IS_ERR(newstate))
708 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500709 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400710 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100711 return 0;
712}
713
714static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
715{
Trond Myklebust864472e2006-01-03 09:55:15 +0100716 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100717 int ret;
718
719 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400720 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100721 smp_rmb();
722 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400723 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100724 if (ret != 0)
725 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400726 if (newstate != state)
727 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100728 }
729 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400730 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100731 if (ret != 0)
732 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400733 if (newstate != state)
734 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100735 }
736 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100738 if (ret != 0)
739 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400740 if (newstate != state)
741 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100742 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400743 /*
744 * We may have performed cached opens for all three recoveries.
745 * Check if we need to update the current stateid.
746 */
747 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
748 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400749 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400750 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
751 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400752 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400753 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100754 return 0;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * OPEN_RECLAIM:
759 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400761static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400763 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100764 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int status;
767
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400768 opendata = nfs4_open_recoverdata_alloc(ctx, state);
769 if (IS_ERR(opendata))
770 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100771 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
772 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400773 rcu_read_lock();
774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500775 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400776 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400777 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100778 opendata->o_arg.u.delegation_type = delegation_type;
779 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400780 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return status;
782}
783
Trond Myklebust539cd032007-06-05 11:46:42 -0400784static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct nfs_server *server = NFS_SERVER(state->inode);
787 struct nfs4_exception exception = { };
788 int err;
789 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400790 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000791 if (err != -NFS4ERR_DELAY)
792 break;
793 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 } while (exception.retry);
795 return err;
796}
797
Trond Myklebust864472e2006-01-03 09:55:15 +0100798static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
799{
800 struct nfs_open_context *ctx;
801 int ret;
802
803 ctx = nfs4_state_find_open_context(state);
804 if (IS_ERR(ctx))
805 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400806 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100807 put_nfs_open_context(ctx);
808 return ret;
809}
810
Trond Myklebust13437e12007-07-06 15:10:43 -0400811static 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 -0700812{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100814 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400816 opendata = nfs4_open_recoverdata_alloc(ctx, state);
817 if (IS_ERR(opendata))
818 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400820 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100822 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400823 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100824 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Trond Myklebust13437e12007-07-06 15:10:43 -0400827int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400830 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int err;
832 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400833 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 switch (err) {
835 case 0:
836 return err;
837 case -NFS4ERR_STALE_CLIENTID:
838 case -NFS4ERR_STALE_STATEID:
839 case -NFS4ERR_EXPIRED:
840 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400841 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
843 }
844 err = nfs4_handle_exception(server, err, &exception);
845 } while (exception.retry);
846 return err;
847}
848
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100849static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
850{
851 struct nfs4_opendata *data = calldata;
852
853 data->rpc_status = task->tk_status;
854 if (RPC_ASSASSINATED(task))
855 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100856 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100857 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
858 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -0500859 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +0100860 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400861 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100862 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100863}
864
865static void nfs4_open_confirm_release(void *calldata)
866{
867 struct nfs4_opendata *data = calldata;
868 struct nfs4_state *state = NULL;
869
870 /* If this request hasn't been cancelled, do nothing */
871 if (data->cancelled == 0)
872 goto out_free;
873 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400874 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100875 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100876 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400877 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100879out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100881}
882
883static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100884 .rpc_call_done = nfs4_open_confirm_done,
885 .rpc_release = nfs4_open_confirm_release,
886};
887
888/*
889 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
890 */
891static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
892{
893 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
894 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400895 struct rpc_message msg = {
896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
897 .rpc_argp = &data->c_arg,
898 .rpc_resp = &data->c_res,
899 .rpc_cred = data->owner->so_cred,
900 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400901 struct rpc_task_setup task_setup_data = {
902 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400903 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400904 .callback_ops = &nfs4_open_confirm_ops,
905 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500906 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400907 .flags = RPC_TASK_ASYNC,
908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int status;
910
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400912 data->rpc_done = 0;
913 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400914 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -0400915 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500916 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100917 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100918 status = nfs4_wait_for_completion_rpc_task(task);
919 if (status != 0) {
920 data->cancelled = 1;
921 smp_wmb();
922 } else
923 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500924 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return status;
926}
927
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100928static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100930 struct nfs4_opendata *data = calldata;
931 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400932
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100933 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
934 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 /*
936 * Check if we still need to send an OPEN call, or if we can use
937 * a delegation instead.
938 */
939 if (data->state != NULL) {
940 struct nfs_delegation *delegation;
941
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944 rcu_read_lock();
945 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
946 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -0500947 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400949 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 }
951 rcu_read_unlock();
952 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100953 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400954 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100955 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400956 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400958 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
959 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100960 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400961 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962 return;
963out_no_action:
964 task->tk_action = NULL;
965
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100966}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100968static void nfs4_open_done(struct rpc_task *task, void *calldata)
969{
970 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100972 data->rpc_status = task->tk_status;
973 if (RPC_ASSASSINATED(task))
974 return;
975 if (task->tk_status == 0) {
976 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700977 case S_IFREG:
978 break;
979 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100980 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700981 break;
982 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100983 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700984 break;
985 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100986 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700987 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100988 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400989 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
990 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700991 }
Trond Myklebust3e309912007-07-07 13:19:59 -0400992 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700994
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100995static void nfs4_open_release(void *calldata)
996{
997 struct nfs4_opendata *data = calldata;
998 struct nfs4_state *state = NULL;
999
1000 /* If this request hasn't been cancelled, do nothing */
1001 if (data->cancelled == 0)
1002 goto out_free;
1003 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001004 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001005 goto out_free;
1006 /* In case we need an open_confirm, no cleanup! */
1007 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1008 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001010 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001012out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001013 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014}
1015
1016static const struct rpc_call_ops nfs4_open_ops = {
1017 .rpc_call_prepare = nfs4_open_prepare,
1018 .rpc_call_done = nfs4_open_done,
1019 .rpc_release = nfs4_open_release,
1020};
1021
1022/*
1023 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1024 */
1025static int _nfs4_proc_open(struct nfs4_opendata *data)
1026{
1027 struct inode *dir = data->dir->d_inode;
1028 struct nfs_server *server = NFS_SERVER(dir);
1029 struct nfs_openargs *o_arg = &data->o_arg;
1030 struct nfs_openres *o_res = &data->o_res;
1031 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001032 struct rpc_message msg = {
1033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1034 .rpc_argp = o_arg,
1035 .rpc_resp = o_res,
1036 .rpc_cred = data->owner->so_cred,
1037 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001038 struct rpc_task_setup task_setup_data = {
1039 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001040 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001041 .callback_ops = &nfs4_open_ops,
1042 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001043 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001044 .flags = RPC_TASK_ASYNC,
1045 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001046 int status;
1047
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001048 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001049 data->rpc_done = 0;
1050 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001051 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001052 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001053 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001054 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055 status = nfs4_wait_for_completion_rpc_task(task);
1056 if (status != 0) {
1057 data->cancelled = 1;
1058 smp_wmb();
1059 } else
1060 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001061 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001062 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001063 return status;
1064
Trond Myklebust99367812007-07-17 21:52:41 -04001065 if (o_res->fh.size == 0)
1066 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1067
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001068 if (o_arg->open_flags & O_CREAT) {
1069 update_changeattr(dir, &o_res->cinfo);
1070 nfs_post_op_update_inode(dir, o_res->dir_attr);
1071 } else
1072 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001074 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001079 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Trond Myklebust10afec92007-05-14 17:16:04 -04001083static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001084{
David Howells7539bba2006-08-22 20:06:09 -04001085 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001086 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001087
Trond Myklebust6b309542006-09-14 14:03:14 -04001088 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001089 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001090 if (ret != 0)
1091 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001092 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1093 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001094 break;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001095 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001096 }
1097 return 0;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/*
1101 * OPEN_EXPIRED:
1102 * reclaim state on the server after a network partition.
1103 * Assumes caller holds the appropriate lock
1104 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001105static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001107 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001110 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1111 if (IS_ERR(opendata))
1112 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001114 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001115 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001116 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Trond Myklebust539cd032007-06-05 11:46:42 -04001120static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001121{
Trond Myklebust539cd032007-06-05 11:46:42 -04001122 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001123 struct nfs4_exception exception = { };
1124 int err;
1125
1126 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001127 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001128 if (err != -NFS4ERR_DELAY)
1129 break;
1130 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001131 } while (exception.retry);
1132 return err;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001138 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 ctx = nfs4_state_find_open_context(state);
1141 if (IS_ERR(ctx))
1142 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001143 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 put_nfs_open_context(ctx);
1145 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001149 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1150 * fields corresponding to attributes that were used to store the verifier.
1151 * Make sure we clobber those fields in the later setattr call
1152 */
1153static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1154{
1155 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1156 !(sattr->ia_valid & ATTR_ATIME_SET))
1157 sattr->ia_valid |= ATTR_ATIME;
1158
1159 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1160 !(sattr->ia_valid & ATTR_MTIME_SET))
1161 sattr->ia_valid |= ATTR_MTIME;
1162}
1163
1164/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001165 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct nfs4_state_owner *sp;
1170 struct nfs4_state *state = NULL;
1171 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001173 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 status = -ENOMEM;
1177 if (!(sp = nfs4_get_state_owner(server, cred))) {
1178 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1179 goto out_err;
1180 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001181 status = nfs4_recover_expired_lease(server);
1182 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001183 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001184 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001186 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001189 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 if (path->dentry->d_inode != NULL)
1192 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1193
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001194 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001196 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001198 if (opendata->o_arg.open_flags & O_EXCL)
1199 nfs4_exclusive_attrset(opendata, sattr);
1200
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001201 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 status = PTR_ERR(state);
1203 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001204 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *res = state;
1208 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001209err_opendata_put:
1210 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211err_put_state_owner:
1212 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *res = NULL;
1215 return status;
1216}
1217
1218
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct nfs4_exception exception = { };
1222 struct nfs4_state *res;
1223 int status;
1224
1225 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (status == 0)
1228 break;
1229 /* NOTE: BAD_SEQID means the server and client disagree about the
1230 * book-keeping w.r.t. state-changing operations
1231 * (OPEN/CLOSE/LOCK/LOCKU...)
1232 * It is actually a sign of a bug on the client or on the server.
1233 *
1234 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001235 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * have unhashed the old state_owner for us, and that we can
1237 * therefore safely retry using a new one. We should still warn
1238 * the user though...
1239 */
1240 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001241 printk(KERN_WARNING "NFS: v4 server %s "
1242 " returned a bad sequence-id error!\n",
1243 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 exception.retry = 1;
1245 continue;
1246 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001247 /*
1248 * BAD_STATEID on OPEN means that the server cancelled our
1249 * state before it received the OPEN_CONFIRM.
1250 * Recover by retrying the request as per the discussion
1251 * on Page 181 of RFC3530.
1252 */
1253 if (status == -NFS4ERR_BAD_STATEID) {
1254 exception.retry = 1;
1255 continue;
1256 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001257 if (status == -EAGAIN) {
1258 /* We must have found a delegation */
1259 exception.retry = 1;
1260 continue;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1263 status, &exception));
1264 } while (exception.retry);
1265 return res;
1266}
1267
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001268static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1269 struct nfs_fattr *fattr, struct iattr *sattr,
1270 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001272 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001274 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 .iap = sattr,
1276 .server = server,
1277 .bitmask = server->attr_bitmask,
1278 };
1279 struct nfs_setattrres res = {
1280 .fattr = fattr,
1281 .server = server,
1282 };
1283 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1285 .rpc_argp = &arg,
1286 .rpc_resp = &res,
1287 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001289 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001290 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Trond Myklebust0e574af2005-10-27 22:12:38 -04001292 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001294 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1295 /* Use that stateid */
1296 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001297 nfs4_copy_stateid(&arg.stateid, state, current->files);
1298 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1300
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001301 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001302 if (status == 0 && state != NULL)
1303 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001304 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001307static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1308 struct nfs_fattr *fattr, struct iattr *sattr,
1309 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001311 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct nfs4_exception exception = { };
1313 int err;
1314 do {
1315 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001316 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 &exception);
1318 } while (exception.retry);
1319 return err;
1320}
1321
1322struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001323 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct inode *inode;
1325 struct nfs4_state *state;
1326 struct nfs_closeargs arg;
1327 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001328 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001329 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330};
1331
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001332static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001333{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001334 struct nfs4_closedata *calldata = data;
1335 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001336
1337 nfs4_put_open_state(calldata->state);
1338 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001339 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001340 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001341 kfree(calldata);
1342}
1343
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001344static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001346 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct nfs_server *server = NFS_SERVER(calldata->inode);
1349
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001350 if (RPC_ASSASSINATED(task))
1351 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* hmm. we are done with the inode, and in the process of freeing
1353 * the state_owner. we keep this around to process errors
1354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 switch (task->tk_status) {
1356 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001357 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001361 case -NFS4ERR_OLD_STATEID:
1362 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001367 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 rpc_restart_call(task);
1369 return;
1370 }
1371 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001372 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001375static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001377 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001378 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001379 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001380
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001382 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001383
Trond Myklebust003707c2007-07-05 18:07:55 -04001384 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001385 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001386 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001387 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001388 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001389 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1390 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1391 }
1392 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001393 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1395 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001396 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001397 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001398 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001399 /* Note: exit _without_ calling nfs4_close_done */
1400 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001401 return;
1402 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001403 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001404 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001406 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001407 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001410 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001411 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001415static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001416 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001417 .rpc_call_done = nfs4_close_done,
1418 .rpc_release = nfs4_free_closedata,
1419};
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/*
1422 * It is possible for data to be read/written from a mem-mapped file
1423 * after the sys_close call (which hits the vfs layer as a flush).
1424 * This means that we can't safely call nfsv4 close on a file until
1425 * the inode is cleared. This in turn means that we are not good
1426 * NFSv4 citizens - we do not indicate to the server to update the file's
1427 * share state even when we are done with one of the three share
1428 * stateid's in the inode.
1429 *
1430 * NOTE: Caller must be holding the sp->so_owner semaphore!
1431 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001432int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001434 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001436 struct nfs4_state_owner *sp = state->owner;
1437 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 struct rpc_message msg = {
1439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1440 .rpc_cred = state->owner->so_cred,
1441 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001442 struct rpc_task_setup task_setup_data = {
1443 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001444 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001445 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001446 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001447 .flags = RPC_TASK_ASYNC,
1448 };
Trond Myklebust95121352005-10-18 14:20:12 -07001449 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Trond Myklebust95121352005-10-18 14:20:12 -07001451 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001453 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001454 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001456 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001459 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001460 if (calldata->arg.seqid == NULL)
1461 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001463 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001464 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001465 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001466 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001467 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001468 calldata->path.mnt = mntget(path->mnt);
1469 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001470
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471 msg.rpc_argp = &calldata->arg,
1472 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001473 task_setup_data.callback_data = calldata;
1474 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001475 if (IS_ERR(task))
1476 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001477 status = 0;
1478 if (wait)
1479 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001480 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001481 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001482out_free_calldata:
1483 kfree(calldata);
1484out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001485 nfs4_put_open_state(state);
1486 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001487 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001490static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001491{
1492 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001493 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001494
Trond Myklebust1b45c462007-07-03 13:04:56 -04001495 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001497 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001498 state->owner->so_cred,
1499 nd->intent.open.flags);
1500 if (ret < 0)
1501 goto out_close;
1502 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001503 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001504 if (!IS_ERR(filp)) {
1505 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001506 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001507 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001508 return 0;
1509 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001510 ret = PTR_ERR(filp);
1511out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001512 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001513 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001514}
1515
1516struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1518{
Trond Myklebustad389da2007-06-05 12:30:00 -04001519 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001520 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001521 .dentry = dentry,
1522 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001523 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct iattr attr;
1525 struct rpc_cred *cred;
1526 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001527 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (nd->flags & LOOKUP_CREATE) {
1531 attr.ia_mode = nd->intent.open.create_mode;
1532 attr.ia_valid = ATTR_MODE;
1533 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001534 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 } else {
1536 attr.ia_valid = 0;
1537 BUG_ON(nd->intent.open.flags & O_CREAT);
1538 }
1539
Trond Myklebust98a8e322008-03-12 12:25:28 -04001540 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001542 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001543 parent = dentry->d_parent;
1544 /* Protect against concurrent sillydeletes */
1545 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001546 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001548 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001549 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001550 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001551 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1552 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001553 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001554 return (struct dentry *)state;
1555 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001557 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001558 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001559 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001560 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001562 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001566nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Trond Myklebustad389da2007-06-05 12:30:00 -04001568 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001569 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001570 .dentry = dentry,
1571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct rpc_cred *cred;
1573 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001574 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Trond Myklebust98a8e322008-03-12 12:25:28 -04001576 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (IS_ERR(cred))
1578 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001579 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001581 if (IS_ERR(state)) {
1582 switch (PTR_ERR(state)) {
1583 case -EPERM:
1584 case -EACCES:
1585 case -EDQUOT:
1586 case -ENOSPC:
1587 case -EROFS:
1588 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1589 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001590 default:
1591 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001592 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001593 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001595 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001596 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return 1;
1598 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001599 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001600out_drop:
1601 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
1603}
1604
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001605void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1606{
1607 if (ctx->state == NULL)
1608 return;
1609 if (is_sync)
1610 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1611 else
1612 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1616{
Benny Halevy43652ad2009-04-01 09:21:54 -04001617 struct nfs4_server_caps_arg args = {
1618 .fhandle = fhandle,
1619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct nfs4_server_caps_res res = {};
1621 struct rpc_message msg = {
1622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001623 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 .rpc_resp = &res,
1625 };
1626 int status;
1627
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001628 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (status == 0) {
1630 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1631 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1632 server->caps |= NFS_CAP_ACLS;
1633 if (res.has_links != 0)
1634 server->caps |= NFS_CAP_HARDLINKS;
1635 if (res.has_symlinks != 0)
1636 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001637 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1638 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1639 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 server->acl_bitmask = res.acl_bitmask;
1641 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return status;
1644}
1645
Trond Myklebust55a97592006-06-09 09:34:19 -04001646int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 struct nfs4_exception exception = { };
1649 int err;
1650 do {
1651 err = nfs4_handle_exception(server,
1652 _nfs4_server_capabilities(server, fhandle),
1653 &exception);
1654 } while (exception.retry);
1655 return err;
1656}
1657
1658static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1659 struct nfs_fsinfo *info)
1660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct nfs4_lookup_root_arg args = {
1662 .bitmask = nfs4_fattr_bitmap,
1663 };
1664 struct nfs4_lookup_res res = {
1665 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001666 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 .fh = fhandle,
1668 };
1669 struct rpc_message msg = {
1670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1671 .rpc_argp = &args,
1672 .rpc_resp = &res,
1673 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001674 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001675 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1679 struct nfs_fsinfo *info)
1680{
1681 struct nfs4_exception exception = { };
1682 int err;
1683 do {
1684 err = nfs4_handle_exception(server,
1685 _nfs4_lookup_root(server, fhandle, info),
1686 &exception);
1687 } while (exception.retry);
1688 return err;
1689}
1690
David Howells54ceac42006-08-22 20:06:13 -04001691/*
1692 * get the file handle for the "/" directory on the server
1693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001695 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int status;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (status == 0)
1701 status = nfs4_server_capabilities(server, fhandle);
1702 if (status == 0)
1703 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001704 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Manoj Naik6b97fd32006-06-09 09:34:29 -04001707/*
1708 * Get locations and (maybe) other attributes of a referral.
1709 * Note that we'll actually follow the referral later when
1710 * we detect fsid mismatch in inode revalidation
1711 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001712static 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 -04001713{
1714 int status = -ENOMEM;
1715 struct page *page = NULL;
1716 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001717
1718 page = alloc_page(GFP_KERNEL);
1719 if (page == NULL)
1720 goto out;
1721 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1722 if (locations == NULL)
1723 goto out;
1724
Trond Myklebustc228fd32007-01-13 02:28:11 -05001725 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001726 if (status != 0)
1727 goto out;
1728 /* Make sure server returned a different fsid for the referral */
1729 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001730 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 -04001731 status = -EIO;
1732 goto out;
1733 }
1734
1735 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1736 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1737 if (!fattr->mode)
1738 fattr->mode = S_IFDIR;
1739 memset(fhandle, 0, sizeof(struct nfs_fh));
1740out:
1741 if (page)
1742 __free_page(page);
1743 if (locations)
1744 kfree(locations);
1745 return status;
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1749{
1750 struct nfs4_getattr_arg args = {
1751 .fh = fhandle,
1752 .bitmask = server->attr_bitmask,
1753 };
1754 struct nfs4_getattr_res res = {
1755 .fattr = fattr,
1756 .server = server,
1757 };
1758 struct rpc_message msg = {
1759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1760 .rpc_argp = &args,
1761 .rpc_resp = &res,
1762 };
1763
Trond Myklebust0e574af2005-10-27 22:12:38 -04001764 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001765 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1769{
1770 struct nfs4_exception exception = { };
1771 int err;
1772 do {
1773 err = nfs4_handle_exception(server,
1774 _nfs4_proc_getattr(server, fhandle, fattr),
1775 &exception);
1776 } while (exception.retry);
1777 return err;
1778}
1779
1780/*
1781 * The file is not closed if it is opened due to the a request to change
1782 * the size of the file. The open call will not be needed once the
1783 * VFS layer lookup-intents are implemented.
1784 *
1785 * Close is called when the inode is destroyed.
1786 * If we haven't opened the file for O_WRONLY, we
1787 * need to in the size_change case to obtain a stateid.
1788 *
1789 * Got race?
1790 * Because OPEN is always done by name in nfsv4, it is
1791 * possible that we opened a different file by the same
1792 * name. We can recognize this race condition, but we
1793 * can't do anything about it besides returning an error.
1794 *
1795 * This will be fixed with VFS changes (lookup-intent).
1796 */
1797static int
1798nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1799 struct iattr *sattr)
1800{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001801 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001802 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001803 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 int status;
1805
Trond Myklebust0e574af2005-10-27 22:12:38 -04001806 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Trond Myklebustd5308382005-11-04 15:33:38 -05001808 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001809 if (sattr->ia_valid & ATTR_FILE) {
1810 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001811
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001812 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11001813 if (ctx) {
1814 cred = ctx->cred;
1815 state = ctx->state;
1816 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001817 }
1818
1819 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001820 if (status == 0)
1821 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return status;
1823}
1824
Trond Myklebust56659e92007-07-17 21:52:39 -04001825static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1826 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001827 struct nfs_fattr *fattr)
1828{
1829 int status;
1830 struct nfs4_lookup_arg args = {
1831 .bitmask = server->attr_bitmask,
1832 .dir_fh = dirfh,
1833 .name = name,
1834 };
1835 struct nfs4_lookup_res res = {
1836 .server = server,
1837 .fattr = fattr,
1838 .fh = fhandle,
1839 };
1840 struct rpc_message msg = {
1841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1842 .rpc_argp = &args,
1843 .rpc_resp = &res,
1844 };
1845
1846 nfs_fattr_init(fattr);
1847
1848 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001849 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04001850 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001851 return status;
1852}
1853
1854static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1855 struct qstr *name, struct nfs_fh *fhandle,
1856 struct nfs_fattr *fattr)
1857{
1858 struct nfs4_exception exception = { };
1859 int err;
1860 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001861 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1862 /* FIXME: !!!! */
1863 if (err == -NFS4ERR_MOVED) {
1864 err = -EREMOTE;
1865 break;
1866 }
1867 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001868 } while (exception.retry);
1869 return err;
1870}
1871
Trond Myklebust56659e92007-07-17 21:52:39 -04001872static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1874{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001875 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001878 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001879 if (status == -NFS4ERR_MOVED)
1880 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 dprintk("NFS reply lookup: %d\n", status);
1882 return status;
1883}
1884
1885static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1886{
1887 struct nfs4_exception exception = { };
1888 int err;
1889 do {
1890 err = nfs4_handle_exception(NFS_SERVER(dir),
1891 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1892 &exception);
1893 } while (exception.retry);
1894 return err;
1895}
1896
1897static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1898{
Trond Myklebust76b32992007-08-10 17:45:11 -04001899 struct nfs_server *server = NFS_SERVER(inode);
1900 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct nfs4_accessargs args = {
1902 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04001903 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 };
Trond Myklebust76b32992007-08-10 17:45:11 -04001905 struct nfs4_accessres res = {
1906 .server = server,
1907 .fattr = &fattr,
1908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct rpc_message msg = {
1910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1911 .rpc_argp = &args,
1912 .rpc_resp = &res,
1913 .rpc_cred = entry->cred,
1914 };
1915 int mode = entry->mask;
1916 int status;
1917
1918 /*
1919 * Determine which access bits we want to ask for...
1920 */
1921 if (mode & MAY_READ)
1922 args.access |= NFS4_ACCESS_READ;
1923 if (S_ISDIR(inode->i_mode)) {
1924 if (mode & MAY_WRITE)
1925 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1926 if (mode & MAY_EXEC)
1927 args.access |= NFS4_ACCESS_LOOKUP;
1928 } else {
1929 if (mode & MAY_WRITE)
1930 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1931 if (mode & MAY_EXEC)
1932 args.access |= NFS4_ACCESS_EXECUTE;
1933 }
Trond Myklebust76b32992007-08-10 17:45:11 -04001934 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001935 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!status) {
1937 entry->mask = 0;
1938 if (res.access & NFS4_ACCESS_READ)
1939 entry->mask |= MAY_READ;
1940 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1941 entry->mask |= MAY_WRITE;
1942 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1943 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04001944 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 return status;
1947}
1948
1949static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1950{
1951 struct nfs4_exception exception = { };
1952 int err;
1953 do {
1954 err = nfs4_handle_exception(NFS_SERVER(inode),
1955 _nfs4_proc_access(inode, entry),
1956 &exception);
1957 } while (exception.retry);
1958 return err;
1959}
1960
1961/*
1962 * TODO: For the time being, we don't try to get any attributes
1963 * along with any of the zero-copy operations READ, READDIR,
1964 * READLINK, WRITE.
1965 *
1966 * In the case of the first three, we want to put the GETATTR
1967 * after the read-type operation -- this is because it is hard
1968 * to predict the length of a GETATTR response in v4, and thus
1969 * align the READ data correctly. This means that the GETATTR
1970 * may end up partially falling into the page cache, and we should
1971 * shift it into the 'tail' of the xdr_buf before processing.
1972 * To do this efficiently, we need to know the total length
1973 * of data received, which doesn't seem to be available outside
1974 * of the RPC layer.
1975 *
1976 * In the case of WRITE, we also want to put the GETATTR after
1977 * the operation -- in this case because we want to make sure
1978 * we get the post-operation mtime and size. This means that
1979 * we can't use xdr_encode_pages() as written: we need a variant
1980 * of it which would leave room in the 'tail' iovec.
1981 *
1982 * Both of these changes to the XDR layer would in fact be quite
1983 * minor, but I decided to leave them for a subsequent patch.
1984 */
1985static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1986 unsigned int pgbase, unsigned int pglen)
1987{
1988 struct nfs4_readlink args = {
1989 .fh = NFS_FH(inode),
1990 .pgbase = pgbase,
1991 .pglen = pglen,
1992 .pages = &page,
1993 };
Benny Halevyf50c7002009-04-01 09:21:55 -04001994 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct rpc_message msg = {
1996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1997 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04001998 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 };
2000
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002001 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
2004static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2005 unsigned int pgbase, unsigned int pglen)
2006{
2007 struct nfs4_exception exception = { };
2008 int err;
2009 do {
2010 err = nfs4_handle_exception(NFS_SERVER(inode),
2011 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2012 &exception);
2013 } while (exception.retry);
2014 return err;
2015}
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/*
2018 * Got race?
2019 * We will need to arrange for the VFS layer to provide an atomic open.
2020 * Until then, this create/open method is prone to inefficiency and race
2021 * conditions due to the lookup, create, and open VFS calls from sys_open()
2022 * placed on the wire.
2023 *
2024 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2025 * The file will be opened again in the subsequent VFS open call
2026 * (nfs4_proc_file_open).
2027 *
2028 * The open for read will just hang around to be used by any process that
2029 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2030 */
2031
2032static int
2033nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002034 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Trond Myklebustad389da2007-06-05 12:30:00 -04002036 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002037 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002038 .dentry = dentry,
2039 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs4_state *state;
2041 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002042 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int status = 0;
2044
Trond Myklebust98a8e322008-03-12 12:25:28 -04002045 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (IS_ERR(cred)) {
2047 status = PTR_ERR(cred);
2048 goto out;
2049 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002050 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002051 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (IS_ERR(state)) {
2053 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002054 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002056 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002057 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (flags & O_EXCL) {
2059 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002060 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002061 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002062 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002063 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002064 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002065 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002066 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002067 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002068 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002069out_putcred:
2070 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071out:
2072 return status;
2073}
2074
2075static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2076{
Trond Myklebust16e42952005-10-27 22:12:44 -04002077 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002078 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002080 .name.len = name->len,
2081 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002082 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002084 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002085 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2089 .rpc_argp = &args,
2090 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 };
2092 int status;
2093
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002094 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002095 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002096 if (status == 0) {
2097 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002098 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return status;
2101}
2102
2103static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2104{
2105 struct nfs4_exception exception = { };
2106 int err;
2107 do {
2108 err = nfs4_handle_exception(NFS_SERVER(dir),
2109 _nfs4_proc_remove(dir, name),
2110 &exception);
2111 } while (exception.retry);
2112 return err;
2113}
2114
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002115static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002117 struct nfs_server *server = NFS_SERVER(dir);
2118 struct nfs_removeargs *args = msg->rpc_argp;
2119 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Trond Myklebusta65318b2009-03-11 14:10:28 -04002121 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002122 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002126static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002128 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2129
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002130 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002131 return 0;
2132 update_changeattr(dir, &res->cinfo);
2133 nfs_post_op_update_inode(dir, &res->dir_attr);
2134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2138 struct inode *new_dir, struct qstr *new_name)
2139{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002140 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct nfs4_rename_arg arg = {
2142 .old_dir = NFS_FH(old_dir),
2143 .new_dir = NFS_FH(new_dir),
2144 .old_name = old_name,
2145 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002146 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002148 struct nfs_fattr old_fattr, new_fattr;
2149 struct nfs4_rename_res res = {
2150 .server = server,
2151 .old_fattr = &old_fattr,
2152 .new_fattr = &new_fattr,
2153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct rpc_message msg = {
2155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2156 .rpc_argp = &arg,
2157 .rpc_resp = &res,
2158 };
2159 int status;
2160
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002161 nfs_fattr_init(res.old_fattr);
2162 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002163 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (!status) {
2166 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002167 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002169 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171 return status;
2172}
2173
2174static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2175 struct inode *new_dir, struct qstr *new_name)
2176{
2177 struct nfs4_exception exception = { };
2178 int err;
2179 do {
2180 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2181 _nfs4_proc_rename(old_dir, old_name,
2182 new_dir, new_name),
2183 &exception);
2184 } while (exception.retry);
2185 return err;
2186}
2187
2188static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2189{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002190 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct nfs4_link_arg arg = {
2192 .fh = NFS_FH(inode),
2193 .dir_fh = NFS_FH(dir),
2194 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002195 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002197 struct nfs_fattr fattr, dir_attr;
2198 struct nfs4_link_res res = {
2199 .server = server,
2200 .fattr = &fattr,
2201 .dir_attr = &dir_attr,
2202 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 struct rpc_message msg = {
2204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2205 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002206 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 };
2208 int status;
2209
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002210 nfs_fattr_init(res.fattr);
2211 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002212 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002213 if (!status) {
2214 update_changeattr(dir, &res.cinfo);
2215 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002216 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 return status;
2220}
2221
2222static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2223{
2224 struct nfs4_exception exception = { };
2225 int err;
2226 do {
2227 err = nfs4_handle_exception(NFS_SERVER(inode),
2228 _nfs4_proc_link(inode, dir, name),
2229 &exception);
2230 } while (exception.retry);
2231 return err;
2232}
2233
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002234struct nfs4_createdata {
2235 struct rpc_message msg;
2236 struct nfs4_create_arg arg;
2237 struct nfs4_create_res res;
2238 struct nfs_fh fh;
2239 struct nfs_fattr fattr;
2240 struct nfs_fattr dir_fattr;
2241};
2242
2243static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2244 struct qstr *name, struct iattr *sattr, u32 ftype)
2245{
2246 struct nfs4_createdata *data;
2247
2248 data = kzalloc(sizeof(*data), GFP_KERNEL);
2249 if (data != NULL) {
2250 struct nfs_server *server = NFS_SERVER(dir);
2251
2252 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2253 data->msg.rpc_argp = &data->arg;
2254 data->msg.rpc_resp = &data->res;
2255 data->arg.dir_fh = NFS_FH(dir);
2256 data->arg.server = server;
2257 data->arg.name = name;
2258 data->arg.attrs = sattr;
2259 data->arg.ftype = ftype;
2260 data->arg.bitmask = server->attr_bitmask;
2261 data->res.server = server;
2262 data->res.fh = &data->fh;
2263 data->res.fattr = &data->fattr;
2264 data->res.dir_fattr = &data->dir_fattr;
2265 nfs_fattr_init(data->res.fattr);
2266 nfs_fattr_init(data->res.dir_fattr);
2267 }
2268 return data;
2269}
2270
2271static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2272{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002273 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2274 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002275 if (status == 0) {
2276 update_changeattr(dir, &data->res.dir_cinfo);
2277 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2278 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2279 }
2280 return status;
2281}
2282
2283static void nfs4_free_createdata(struct nfs4_createdata *data)
2284{
2285 kfree(data);
2286}
2287
Chuck Lever4f390c12006-08-22 20:06:22 -04002288static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002289 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002291 struct nfs4_createdata *data;
2292 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Chuck Lever94a6d752006-08-22 20:06:23 -04002294 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002295 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002296
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002297 status = -ENOMEM;
2298 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2299 if (data == NULL)
2300 goto out;
2301
2302 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2303 data->arg.u.symlink.pages = &page;
2304 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002306 status = nfs4_do_create(dir, dentry, data);
2307
2308 nfs4_free_createdata(data);
2309out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return status;
2311}
2312
Chuck Lever4f390c12006-08-22 20:06:22 -04002313static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002314 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 struct nfs4_exception exception = { };
2317 int err;
2318 do {
2319 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002320 _nfs4_proc_symlink(dir, dentry, page,
2321 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 &exception);
2323 } while (exception.retry);
2324 return err;
2325}
2326
2327static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2328 struct iattr *sattr)
2329{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002330 struct nfs4_createdata *data;
2331 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002333 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2334 if (data == NULL)
2335 goto out;
2336
2337 status = nfs4_do_create(dir, dentry, data);
2338
2339 nfs4_free_createdata(data);
2340out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return status;
2342}
2343
2344static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2345 struct iattr *sattr)
2346{
2347 struct nfs4_exception exception = { };
2348 int err;
2349 do {
2350 err = nfs4_handle_exception(NFS_SERVER(dir),
2351 _nfs4_proc_mkdir(dir, dentry, sattr),
2352 &exception);
2353 } while (exception.retry);
2354 return err;
2355}
2356
2357static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2358 u64 cookie, struct page *page, unsigned int count, int plus)
2359{
2360 struct inode *dir = dentry->d_inode;
2361 struct nfs4_readdir_arg args = {
2362 .fh = NFS_FH(dir),
2363 .pages = &page,
2364 .pgbase = 0,
2365 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002366 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 };
2368 struct nfs4_readdir_res res;
2369 struct rpc_message msg = {
2370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2371 .rpc_argp = &args,
2372 .rpc_resp = &res,
2373 .rpc_cred = cred,
2374 };
2375 int status;
2376
Harvey Harrison3110ff82008-05-02 13:42:44 -07002377 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002378 dentry->d_parent->d_name.name,
2379 dentry->d_name.name,
2380 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2382 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002383 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (status == 0)
2385 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002386
2387 nfs_invalidate_atime(dir);
2388
Harvey Harrison3110ff82008-05-02 13:42:44 -07002389 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return status;
2391}
2392
2393static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2394 u64 cookie, struct page *page, unsigned int count, int plus)
2395{
2396 struct nfs4_exception exception = { };
2397 int err;
2398 do {
2399 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2400 _nfs4_proc_readdir(dentry, cred, cookie,
2401 page, count, plus),
2402 &exception);
2403 } while (exception.retry);
2404 return err;
2405}
2406
2407static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2408 struct iattr *sattr, dev_t rdev)
2409{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002410 struct nfs4_createdata *data;
2411 int mode = sattr->ia_mode;
2412 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2415 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002416
2417 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2418 if (data == NULL)
2419 goto out;
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002422 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002424 data->arg.ftype = NF4BLK;
2425 data->arg.u.device.specdata1 = MAJOR(rdev);
2426 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002429 data->arg.ftype = NF4CHR;
2430 data->arg.u.device.specdata1 = MAJOR(rdev);
2431 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002434 status = nfs4_do_create(dir, dentry, data);
2435
2436 nfs4_free_createdata(data);
2437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return status;
2439}
2440
2441static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2442 struct iattr *sattr, dev_t rdev)
2443{
2444 struct nfs4_exception exception = { };
2445 int err;
2446 do {
2447 err = nfs4_handle_exception(NFS_SERVER(dir),
2448 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2449 &exception);
2450 } while (exception.retry);
2451 return err;
2452}
2453
2454static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2455 struct nfs_fsstat *fsstat)
2456{
2457 struct nfs4_statfs_arg args = {
2458 .fh = fhandle,
2459 .bitmask = server->attr_bitmask,
2460 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002461 struct nfs4_statfs_res res = {
2462 .fsstat = fsstat,
2463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct rpc_message msg = {
2465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2466 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002467 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 };
2469
Trond Myklebust0e574af2005-10-27 22:12:38 -04002470 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002471 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2475{
2476 struct nfs4_exception exception = { };
2477 int err;
2478 do {
2479 err = nfs4_handle_exception(server,
2480 _nfs4_proc_statfs(server, fhandle, fsstat),
2481 &exception);
2482 } while (exception.retry);
2483 return err;
2484}
2485
2486static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2487 struct nfs_fsinfo *fsinfo)
2488{
2489 struct nfs4_fsinfo_arg args = {
2490 .fh = fhandle,
2491 .bitmask = server->attr_bitmask,
2492 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002493 struct nfs4_fsinfo_res res = {
2494 .fsinfo = fsinfo,
2495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct rpc_message msg = {
2497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2498 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 };
2501
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002502 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2506{
2507 struct nfs4_exception exception = { };
2508 int err;
2509
2510 do {
2511 err = nfs4_handle_exception(server,
2512 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2513 &exception);
2514 } while (exception.retry);
2515 return err;
2516}
2517
2518static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2519{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002520 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2522}
2523
2524static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2525 struct nfs_pathconf *pathconf)
2526{
2527 struct nfs4_pathconf_arg args = {
2528 .fh = fhandle,
2529 .bitmask = server->attr_bitmask,
2530 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002531 struct nfs4_pathconf_res res = {
2532 .pathconf = pathconf,
2533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 struct rpc_message msg = {
2535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2536 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002537 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 };
2539
2540 /* None of the pathconf attributes are mandatory to implement */
2541 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2542 memset(pathconf, 0, sizeof(*pathconf));
2543 return 0;
2544 }
2545
Trond Myklebust0e574af2005-10-27 22:12:38 -04002546 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002547 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2551 struct nfs_pathconf *pathconf)
2552{
2553 struct nfs4_exception exception = { };
2554 int err;
2555
2556 do {
2557 err = nfs4_handle_exception(server,
2558 _nfs4_proc_pathconf(server, fhandle, pathconf),
2559 &exception);
2560 } while (exception.retry);
2561 return err;
2562}
2563
Trond Myklebustec06c092006-03-20 13:44:27 -05002564static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Trond Myklebustec06c092006-03-20 13:44:27 -05002566 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002568 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002570 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002572
2573 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002575 renew_lease(server, data->timestamp);
2576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002579static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002582 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Trond Myklebust788e7a82006-03-20 13:44:27 -05002585static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 struct inode *inode = data->inode;
2588
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002589 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002591 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002593 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002595 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002596 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002600static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002602 struct nfs_server *server = NFS_SERVER(data->inode);
2603
Trond Myklebusta65318b2009-03-11 14:10:28 -04002604 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002605 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 data->timestamp = jiffies;
2607
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Trond Myklebust788e7a82006-03-20 13:44:27 -05002611static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct inode *inode = data->inode;
2614
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002615 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002617 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002619 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002623static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002625 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Trond Myklebusta65318b2009-03-11 14:10:28 -04002627 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002628 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002629 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632/*
2633 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2634 * standalone procedure for queueing an asynchronous RENEW.
2635 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002636static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
David Howellsadfa6f92006-08-22 20:06:08 -04002638 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002639 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002642 /* Unless we're shutting down, schedule state recovery! */
2643 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2644 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return;
2646 }
2647 spin_lock(&clp->cl_lock);
2648 if (time_before(clp->cl_last_renewal,timestamp))
2649 clp->cl_last_renewal = timestamp;
2650 spin_unlock(&clp->cl_lock);
2651}
2652
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002653static const struct rpc_call_ops nfs4_renew_ops = {
2654 .rpc_call_done = nfs4_renew_done,
2655};
2656
David Howellsadfa6f92006-08-22 20:06:08 -04002657int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2661 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002662 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
2664
2665 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002666 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
David Howellsadfa6f92006-08-22 20:06:08 -04002669int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct rpc_message msg = {
2672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2673 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002674 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 };
2676 unsigned long now = jiffies;
2677 int status;
2678
2679 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2680 if (status < 0)
2681 return status;
2682 spin_lock(&clp->cl_lock);
2683 if (time_before(clp->cl_last_renewal,now))
2684 clp->cl_last_renewal = now;
2685 spin_unlock(&clp->cl_lock);
2686 return 0;
2687}
2688
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002689static inline int nfs4_server_supports_acls(struct nfs_server *server)
2690{
2691 return (server->caps & NFS_CAP_ACLS)
2692 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2693 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2694}
2695
2696/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2697 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2698 * the stack.
2699 */
2700#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2701
2702static void buf_to_pages(const void *buf, size_t buflen,
2703 struct page **pages, unsigned int *pgbase)
2704{
2705 const void *p = buf;
2706
2707 *pgbase = offset_in_page(buf);
2708 p -= *pgbase;
2709 while (p < buf + buflen) {
2710 *(pages++) = virt_to_page(p);
2711 p += PAGE_CACHE_SIZE;
2712 }
2713}
2714
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002715struct nfs4_cached_acl {
2716 int cached;
2717 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002718 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002719};
2720
2721static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002722{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002723 struct nfs_inode *nfsi = NFS_I(inode);
2724
2725 spin_lock(&inode->i_lock);
2726 kfree(nfsi->nfs4_acl);
2727 nfsi->nfs4_acl = acl;
2728 spin_unlock(&inode->i_lock);
2729}
2730
2731static void nfs4_zap_acl_attr(struct inode *inode)
2732{
2733 nfs4_set_cached_acl(inode, NULL);
2734}
2735
2736static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2737{
2738 struct nfs_inode *nfsi = NFS_I(inode);
2739 struct nfs4_cached_acl *acl;
2740 int ret = -ENOENT;
2741
2742 spin_lock(&inode->i_lock);
2743 acl = nfsi->nfs4_acl;
2744 if (acl == NULL)
2745 goto out;
2746 if (buf == NULL) /* user is just asking for length */
2747 goto out_len;
2748 if (acl->cached == 0)
2749 goto out;
2750 ret = -ERANGE; /* see getxattr(2) man page */
2751 if (acl->len > buflen)
2752 goto out;
2753 memcpy(buf, acl->data, acl->len);
2754out_len:
2755 ret = acl->len;
2756out:
2757 spin_unlock(&inode->i_lock);
2758 return ret;
2759}
2760
2761static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2762{
2763 struct nfs4_cached_acl *acl;
2764
2765 if (buf && acl_len <= PAGE_SIZE) {
2766 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2767 if (acl == NULL)
2768 goto out;
2769 acl->cached = 1;
2770 memcpy(acl->data, buf, acl_len);
2771 } else {
2772 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2773 if (acl == NULL)
2774 goto out;
2775 acl->cached = 0;
2776 }
2777 acl->len = acl_len;
2778out:
2779 nfs4_set_cached_acl(inode, acl);
2780}
2781
Trond Myklebust16b42892006-08-24 12:27:15 -04002782static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002783{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002784 struct page *pages[NFS4ACL_MAXPAGES];
2785 struct nfs_getaclargs args = {
2786 .fh = NFS_FH(inode),
2787 .acl_pages = pages,
2788 .acl_len = buflen,
2789 };
Benny Halevy663c79b2009-04-01 09:21:59 -04002790 struct nfs_getaclres res = {
2791 .acl_len = buflen,
2792 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002793 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2796 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04002797 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002798 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002799 struct page *localpage = NULL;
2800 int ret;
2801
2802 if (buflen < PAGE_SIZE) {
2803 /* As long as we're doing a round trip to the server anyway,
2804 * let's be prepared for a page of acl data. */
2805 localpage = alloc_page(GFP_KERNEL);
2806 resp_buf = page_address(localpage);
2807 if (localpage == NULL)
2808 return -ENOMEM;
2809 args.acl_pages[0] = localpage;
2810 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04002811 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002812 } else {
2813 resp_buf = buf;
2814 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2815 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002816 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002817 if (ret)
2818 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04002819 if (res.acl_len > args.acl_len)
2820 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002821 else
Benny Halevy663c79b2009-04-01 09:21:59 -04002822 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002823 if (buf) {
2824 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04002825 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002826 goto out_free;
2827 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04002828 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002829 }
Benny Halevy663c79b2009-04-01 09:21:59 -04002830 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002831out_free:
2832 if (localpage)
2833 __free_page(localpage);
2834 return ret;
2835}
2836
Trond Myklebust16b42892006-08-24 12:27:15 -04002837static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2838{
2839 struct nfs4_exception exception = { };
2840 ssize_t ret;
2841 do {
2842 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2843 if (ret >= 0)
2844 break;
2845 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2846 } while (exception.retry);
2847 return ret;
2848}
2849
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002850static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2851{
2852 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002853 int ret;
2854
2855 if (!nfs4_server_supports_acls(server))
2856 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002857 ret = nfs_revalidate_inode(server, inode);
2858 if (ret < 0)
2859 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04002860 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2861 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002862 ret = nfs4_read_cached_acl(inode, buf, buflen);
2863 if (ret != -ENOENT)
2864 return ret;
2865 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002866}
2867
Trond Myklebust16b42892006-08-24 12:27:15 -04002868static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002869{
2870 struct nfs_server *server = NFS_SERVER(inode);
2871 struct page *pages[NFS4ACL_MAXPAGES];
2872 struct nfs_setaclargs arg = {
2873 .fh = NFS_FH(inode),
2874 .acl_pages = pages,
2875 .acl_len = buflen,
2876 };
Benny Halevy73c403a2009-04-01 09:22:01 -04002877 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002878 struct rpc_message msg = {
2879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2880 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04002881 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002882 };
2883 int ret;
2884
2885 if (!nfs4_server_supports_acls(server))
2886 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002887 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002888 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002889 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04002890 nfs_access_zap_cache(inode);
2891 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002892 return ret;
2893}
2894
Trond Myklebust16b42892006-08-24 12:27:15 -04002895static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2896{
2897 struct nfs4_exception exception = { };
2898 int err;
2899 do {
2900 err = nfs4_handle_exception(NFS_SERVER(inode),
2901 __nfs4_proc_set_acl(inode, buf, buflen),
2902 &exception);
2903 } while (exception.retry);
2904 return err;
2905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002908nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
David Howells7539bba2006-08-22 20:06:09 -04002910 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 if (!clp || task->tk_status >= 0)
2913 return 0;
2914 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002915 case -NFS4ERR_ADMIN_REVOKED:
2916 case -NFS4ERR_BAD_STATEID:
2917 case -NFS4ERR_OPENMODE:
2918 if (state == NULL)
2919 break;
2920 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 case -NFS4ERR_STALE_CLIENTID:
2922 case -NFS4ERR_STALE_STATEID:
2923 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05002924 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05002926 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05002927 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 task->tk_status = 0;
2929 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04002931 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05002932 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2934 task->tk_status = 0;
2935 return -EAGAIN;
2936 case -NFS4ERR_OLD_STATEID:
2937 task->tk_status = 0;
2938 return -EAGAIN;
2939 }
2940 task->tk_status = nfs4_map_errors(task->tk_status);
2941 return 0;
2942}
2943
David Howellsadfa6f92006-08-22 20:06:08 -04002944int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 nfs4_verifier sc_verifier;
2947 struct nfs4_setclientid setclientid = {
2948 .sc_verifier = &sc_verifier,
2949 .sc_prog = program,
2950 };
2951 struct rpc_message msg = {
2952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2953 .rpc_argp = &setclientid,
2954 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002955 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 };
Al Virobc4785c2006-10-19 23:28:51 -07002957 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 int loop = 0;
2959 int status;
2960
Al Virobc4785c2006-10-19 23:28:51 -07002961 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2963 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2964
2965 for(;;) {
2966 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05002967 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05002968 clp->cl_ipaddr,
2969 rpc_peeraddr2str(clp->cl_rpcclient,
2970 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05002971 rpc_peeraddr2str(clp->cl_rpcclient,
2972 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04002973 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 clp->cl_id_uniquifier);
2975 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05002976 sizeof(setclientid.sc_netid),
2977 rpc_peeraddr2str(clp->cl_rpcclient,
2978 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05002980 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 clp->cl_ipaddr, port >> 8, port & 255);
2982
2983 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2984 if (status != -NFS4ERR_CLID_INUSE)
2985 break;
2986 if (signalled())
2987 break;
2988 if (loop++ & 1)
2989 ssleep(clp->cl_lease_time + 1);
2990 else
2991 if (++clp->cl_id_uniquifier == 0)
2992 break;
2993 }
2994 return status;
2995}
2996
David Howellsadfa6f92006-08-22 20:06:08 -04002997static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 struct nfs_fsinfo fsinfo;
3000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3002 .rpc_argp = clp,
3003 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003004 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 };
3006 unsigned long now;
3007 int status;
3008
3009 now = jiffies;
3010 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3011 if (status == 0) {
3012 spin_lock(&clp->cl_lock);
3013 clp->cl_lease_time = fsinfo.lease_time * HZ;
3014 clp->cl_last_renewal = now;
3015 spin_unlock(&clp->cl_lock);
3016 }
3017 return status;
3018}
3019
David Howellsadfa6f92006-08-22 20:06:08 -04003020int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003021{
Benny Halevy77e03672008-06-24 20:25:57 +03003022 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003023 int err;
3024 do {
3025 err = _nfs4_proc_setclientid_confirm(clp, cred);
3026 switch (err) {
3027 case 0:
3028 return err;
3029 case -NFS4ERR_RESOURCE:
3030 /* The IBM lawyers misread another document! */
3031 case -NFS4ERR_DELAY:
3032 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3033 }
3034 } while (err == 0);
3035 return err;
3036}
3037
Trond Myklebustfe650402006-01-03 09:55:18 +01003038struct nfs4_delegreturndata {
3039 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003040 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003041 struct nfs_fh fh;
3042 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003043 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003044 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003045 int rpc_status;
3046};
3047
Trond Myklebustfe650402006-01-03 09:55:18 +01003048static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3049{
3050 struct nfs4_delegreturndata *data = calldata;
3051 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003052 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003053 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003054}
3055
3056static void nfs4_delegreturn_release(void *calldata)
3057{
Trond Myklebustfe650402006-01-03 09:55:18 +01003058 kfree(calldata);
3059}
3060
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003061static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003062 .rpc_call_done = nfs4_delegreturn_done,
3063 .rpc_release = nfs4_delegreturn_release,
3064};
3065
Trond Myklebuste6f81072008-01-24 18:14:34 -05003066static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003067{
3068 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003069 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003070 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003071 struct rpc_message msg = {
3072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3073 .rpc_cred = cred,
3074 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003075 struct rpc_task_setup task_setup_data = {
3076 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003077 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003078 .callback_ops = &nfs4_delegreturn_ops,
3079 .flags = RPC_TASK_ASYNC,
3080 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003081 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003082
3083 data = kmalloc(sizeof(*data), GFP_KERNEL);
3084 if (data == NULL)
3085 return -ENOMEM;
3086 data->args.fhandle = &data->fh;
3087 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003088 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003089 nfs_copy_fh(&data->fh, NFS_FH(inode));
3090 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003091 data->res.fattr = &data->fattr;
3092 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003093 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003094 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003095 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003096 data->rpc_status = 0;
3097
Trond Myklebustc970aa82007-07-14 15:39:59 -04003098 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003099 msg.rpc_argp = &data->args,
3100 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003101 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003102 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003103 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003104 if (!issync)
3105 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003106 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003107 if (status != 0)
3108 goto out;
3109 status = data->rpc_status;
3110 if (status != 0)
3111 goto out;
3112 nfs_refresh_inode(inode, &data->fattr);
3113out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003114 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
Trond Myklebuste6f81072008-01-24 18:14:34 -05003118int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct nfs_server *server = NFS_SERVER(inode);
3121 struct nfs4_exception exception = { };
3122 int err;
3123 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003124 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 switch (err) {
3126 case -NFS4ERR_STALE_STATEID:
3127 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 case 0:
3129 return 0;
3130 }
3131 err = nfs4_handle_exception(server, err, &exception);
3132 } while (exception.retry);
3133 return err;
3134}
3135
3136#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3137#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3138
3139/*
3140 * sleep, with exponential backoff, and retry the LOCK operation.
3141 */
3142static unsigned long
3143nfs4_set_lock_task_retry(unsigned long timeout)
3144{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003145 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 timeout <<= 1;
3147 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3148 return NFS4_LOCK_MAXTIMEOUT;
3149 return timeout;
3150}
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3153{
3154 struct inode *inode = state->inode;
3155 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003156 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003157 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003159 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003161 struct nfs_lockt_res res = {
3162 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 };
3164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3166 .rpc_argp = &arg,
3167 .rpc_resp = &res,
3168 .rpc_cred = state->owner->so_cred,
3169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct nfs4_lock_state *lsp;
3171 int status;
3172
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003173 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003174 status = nfs4_set_lock_state(state, request);
3175 if (status != 0)
3176 goto out;
3177 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003178 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003179 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003180 switch (status) {
3181 case 0:
3182 request->fl_type = F_UNLCK;
3183 break;
3184 case -NFS4ERR_DENIED:
3185 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003187 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003188out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return status;
3190}
3191
3192static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3193{
3194 struct nfs4_exception exception = { };
3195 int err;
3196
3197 do {
3198 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3199 _nfs4_proc_getlk(state, cmd, request),
3200 &exception);
3201 } while (exception.retry);
3202 return err;
3203}
3204
3205static int do_vfs_lock(struct file *file, struct file_lock *fl)
3206{
3207 int res = 0;
3208 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3209 case FL_POSIX:
3210 res = posix_lock_file_wait(file, fl);
3211 break;
3212 case FL_FLOCK:
3213 res = flock_lock_file_wait(file, fl);
3214 break;
3215 default:
3216 BUG();
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return res;
3219}
3220
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003221struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003222 struct nfs_locku_args arg;
3223 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003224 struct nfs4_lock_state *lsp;
3225 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003226 struct file_lock fl;
3227 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003228 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003229};
3230
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003231static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3232 struct nfs_open_context *ctx,
3233 struct nfs4_lock_state *lsp,
3234 struct nfs_seqid *seqid)
3235{
3236 struct nfs4_unlockdata *p;
3237 struct inode *inode = lsp->ls_state->inode;
3238
3239 p = kmalloc(sizeof(*p), GFP_KERNEL);
3240 if (p == NULL)
3241 return NULL;
3242 p->arg.fh = NFS_FH(inode);
3243 p->arg.fl = &p->fl;
3244 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003245 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003246 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003247 p->arg.stateid = &lsp->ls_stateid;
3248 p->lsp = lsp;
3249 atomic_inc(&lsp->ls_count);
3250 /* Ensure we don't close file until we're done freeing locks! */
3251 p->ctx = get_nfs_open_context(ctx);
3252 memcpy(&p->fl, fl, sizeof(p->fl));
3253 p->server = NFS_SERVER(inode);
3254 return p;
3255}
3256
Trond Myklebust06f814a2006-01-03 09:55:07 +01003257static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003258{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003259 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003260 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003261 nfs4_put_lock_state(calldata->lsp);
3262 put_nfs_open_context(calldata->ctx);
3263 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003264}
3265
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003266static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003267{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003268 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003269
Trond Myklebust06f814a2006-01-03 09:55:07 +01003270 if (RPC_ASSASSINATED(task))
3271 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003272 switch (task->tk_status) {
3273 case 0:
3274 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003275 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003276 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003277 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003278 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003279 case -NFS4ERR_BAD_STATEID:
3280 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003281 case -NFS4ERR_STALE_STATEID:
3282 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003283 break;
3284 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003285 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003286 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003287 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003288}
3289
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003290static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003291{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003292 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003294 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003295 return;
3296 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003297 /* Note: exit _without_ running nfs4_locku_done */
3298 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003299 return;
3300 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003301 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003302 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003305static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003306 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003307 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003308 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003309};
3310
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003311static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3312 struct nfs_open_context *ctx,
3313 struct nfs4_lock_state *lsp,
3314 struct nfs_seqid *seqid)
3315{
3316 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3319 .rpc_cred = ctx->cred,
3320 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003321 struct rpc_task_setup task_setup_data = {
3322 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003323 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003324 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003325 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003326 .flags = RPC_TASK_ASYNC,
3327 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003328
Frank Filz137d6ac2007-07-09 15:32:29 -07003329 /* Ensure this is an unlock - when canceling a lock, the
3330 * canceled lock is passed in, and it won't be an unlock.
3331 */
3332 fl->fl_type = F_UNLCK;
3333
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003334 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3335 if (data == NULL) {
3336 nfs_free_seqid(seqid);
3337 return ERR_PTR(-ENOMEM);
3338 }
3339
Trond Myklebust5138fde2007-07-14 15:40:01 -04003340 msg.rpc_argp = &data->arg,
3341 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003342 task_setup_data.callback_data = data;
3343 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3347{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003348 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003349 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003350 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003351 struct rpc_task *task;
3352 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003353 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Trond Myklebust9b073572006-06-29 16:38:34 -04003355 status = nfs4_set_lock_state(state, request);
3356 /* Unlock _before_ we do the RPC call */
3357 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003358 down_read(&nfsi->rwsem);
3359 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3360 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003361 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003362 }
3363 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003364 if (status != 0)
3365 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003366 /* Is this a delegated lock? */
3367 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003368 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003369 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003370 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003371 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003372 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003373 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003374 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003375 status = PTR_ERR(task);
3376 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003377 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003378 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003379 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003380out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003381 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003385struct nfs4_lockdata {
3386 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003387 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003388 struct nfs4_lock_state *lsp;
3389 struct nfs_open_context *ctx;
3390 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003391 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003392 int rpc_status;
3393 int cancelled;
3394};
3395
3396static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3397 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3398{
3399 struct nfs4_lockdata *p;
3400 struct inode *inode = lsp->ls_state->inode;
3401 struct nfs_server *server = NFS_SERVER(inode);
3402
3403 p = kzalloc(sizeof(*p), GFP_KERNEL);
3404 if (p == NULL)
3405 return NULL;
3406
3407 p->arg.fh = NFS_FH(inode);
3408 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003409 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3410 if (p->arg.open_seqid == NULL)
3411 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003412 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3413 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003414 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003415 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003416 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003417 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003418 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003419 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003420 p->lsp = lsp;
3421 atomic_inc(&lsp->ls_count);
3422 p->ctx = get_nfs_open_context(ctx);
3423 memcpy(&p->fl, fl, sizeof(p->fl));
3424 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003425out_free_seqid:
3426 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003427out_free:
3428 kfree(p);
3429 return NULL;
3430}
3431
3432static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3433{
3434 struct nfs4_lockdata *data = calldata;
3435 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Harvey Harrison3110ff82008-05-02 13:42:44 -07003437 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003438 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3439 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003440 /* Do we need to do an open_to_lock_owner? */
3441 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003442 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3443 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003444 data->arg.open_stateid = &state->stateid;
3445 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003446 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003447 } else
3448 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003449 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003450 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003451 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003452}
3453
3454static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3455{
3456 struct nfs4_lockdata *data = calldata;
3457
Harvey Harrison3110ff82008-05-02 13:42:44 -07003458 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003459
3460 data->rpc_status = task->tk_status;
3461 if (RPC_ASSASSINATED(task))
3462 goto out;
3463 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003464 if (data->rpc_status == 0)
3465 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3466 else
3467 goto out;
3468 }
3469 if (data->rpc_status == 0) {
3470 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3471 sizeof(data->lsp->ls_stateid.data));
3472 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003473 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003474 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003475out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003476 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003477}
3478
3479static void nfs4_lock_release(void *calldata)
3480{
3481 struct nfs4_lockdata *data = calldata;
3482
Harvey Harrison3110ff82008-05-02 13:42:44 -07003483 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003484 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003485 if (data->cancelled != 0) {
3486 struct rpc_task *task;
3487 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3488 data->arg.lock_seqid);
3489 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003490 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003491 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003492 } else
3493 nfs_free_seqid(data->arg.lock_seqid);
3494 nfs4_put_lock_state(data->lsp);
3495 put_nfs_open_context(data->ctx);
3496 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003497 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003498}
3499
3500static const struct rpc_call_ops nfs4_lock_ops = {
3501 .rpc_call_prepare = nfs4_lock_prepare,
3502 .rpc_call_done = nfs4_lock_done,
3503 .rpc_release = nfs4_lock_release,
3504};
3505
3506static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3507{
3508 struct nfs4_lockdata *data;
3509 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003510 struct rpc_message msg = {
3511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3512 .rpc_cred = state->owner->so_cred,
3513 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003514 struct rpc_task_setup task_setup_data = {
3515 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003516 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003517 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003518 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003519 .flags = RPC_TASK_ASYNC,
3520 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003521 int ret;
3522
Harvey Harrison3110ff82008-05-02 13:42:44 -07003523 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003524 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003525 fl->fl_u.nfs4_fl.owner);
3526 if (data == NULL)
3527 return -ENOMEM;
3528 if (IS_SETLKW(cmd))
3529 data->arg.block = 1;
3530 if (reclaim != 0)
3531 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003532 msg.rpc_argp = &data->arg,
3533 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003534 task_setup_data.callback_data = data;
3535 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003536 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003537 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003538 ret = nfs4_wait_for_completion_rpc_task(task);
3539 if (ret == 0) {
3540 ret = data->rpc_status;
3541 if (ret == -NFS4ERR_DENIED)
3542 ret = -EAGAIN;
3543 } else
3544 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003545 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003546 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003547 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
3550static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3551{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003552 struct nfs_server *server = NFS_SERVER(state->inode);
3553 struct nfs4_exception exception = { };
3554 int err;
3555
3556 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003557 /* Cache the lock if possible... */
3558 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3559 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003560 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3561 if (err != -NFS4ERR_DELAY)
3562 break;
3563 nfs4_handle_exception(server, err, &exception);
3564 } while (exception.retry);
3565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
3568static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3569{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003570 struct nfs_server *server = NFS_SERVER(state->inode);
3571 struct nfs4_exception exception = { };
3572 int err;
3573
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003574 err = nfs4_set_lock_state(state, request);
3575 if (err != 0)
3576 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003577 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003578 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3579 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003580 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3581 if (err != -NFS4ERR_DELAY)
3582 break;
3583 nfs4_handle_exception(server, err, &exception);
3584 } while (exception.retry);
3585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3589{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003590 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003591 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 int status;
3593
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003594 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003595 status = nfs4_set_lock_state(state, request);
3596 if (status != 0)
3597 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003598 request->fl_flags |= FL_ACCESS;
3599 status = do_vfs_lock(request->fl_file, request);
3600 if (status < 0)
3601 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003602 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003603 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003604 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003605 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003606 request->fl_flags = fl_flags & ~FL_SLEEP;
3607 status = do_vfs_lock(request->fl_file, request);
3608 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003609 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003610 status = _nfs4_do_setlk(state, cmd, request, 0);
3611 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003612 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003613 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003614 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003615 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003616 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003617out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003618 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003619out:
3620 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return status;
3622}
3623
3624static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3625{
3626 struct nfs4_exception exception = { };
3627 int err;
3628
3629 do {
3630 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3631 _nfs4_proc_setlk(state, cmd, request),
3632 &exception);
3633 } while (exception.retry);
3634 return err;
3635}
3636
3637static int
3638nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3639{
3640 struct nfs_open_context *ctx;
3641 struct nfs4_state *state;
3642 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3643 int status;
3644
3645 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003646 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 state = ctx->state;
3648
3649 if (request->fl_start < 0 || request->fl_end < 0)
3650 return -EINVAL;
3651
3652 if (IS_GETLK(cmd))
3653 return nfs4_proc_getlk(state, F_GETLK, request);
3654
3655 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3656 return -EINVAL;
3657
3658 if (request->fl_type == F_UNLCK)
3659 return nfs4_proc_unlck(state, cmd, request);
3660
3661 do {
3662 status = nfs4_proc_setlk(state, cmd, request);
3663 if ((status != -EAGAIN) || IS_SETLK(cmd))
3664 break;
3665 timeout = nfs4_set_lock_task_retry(timeout);
3666 status = -ERESTARTSYS;
3667 if (signalled())
3668 break;
3669 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 return status;
3671}
3672
Trond Myklebust888e6942005-11-04 15:38:11 -05003673int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3674{
3675 struct nfs_server *server = NFS_SERVER(state->inode);
3676 struct nfs4_exception exception = { };
3677 int err;
3678
3679 err = nfs4_set_lock_state(state, fl);
3680 if (err != 0)
3681 goto out;
3682 do {
3683 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3684 if (err != -NFS4ERR_DELAY)
3685 break;
3686 err = nfs4_handle_exception(server, err, &exception);
3687 } while (exception.retry);
3688out:
3689 return err;
3690}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003691
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003692#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3693
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003694int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3695 size_t buflen, int flags)
3696{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003697 struct inode *inode = dentry->d_inode;
3698
3699 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3700 return -EOPNOTSUPP;
3701
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003702 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003703}
3704
3705/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3706 * and that's what we'll do for e.g. user attributes that haven't been set.
3707 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3708 * attributes in kernel-managed attribute namespaces. */
3709ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3710 size_t buflen)
3711{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003712 struct inode *inode = dentry->d_inode;
3713
3714 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3715 return -EOPNOTSUPP;
3716
3717 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003718}
3719
3720ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3721{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003722 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003723
J. Bruce Fields096455a2006-03-20 23:23:42 -05003724 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3725 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003726 if (buf && buflen < len)
3727 return -ERANGE;
3728 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3730 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003731}
3732
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003733static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3734{
3735 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3736 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3737 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3738 return;
3739
3740 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3741 NFS_ATTR_FATTR_NLINK;
3742 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3743 fattr->nlink = 2;
3744}
3745
Trond Myklebust56659e92007-07-17 21:52:39 -04003746int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003747 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003748{
3749 struct nfs_server *server = NFS_SERVER(dir);
3750 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003751 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3752 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003753 };
3754 struct nfs4_fs_locations_arg args = {
3755 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003756 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003757 .page = page,
3758 .bitmask = bitmask,
3759 };
Benny Halevy22958462009-04-01 09:22:02 -04003760 struct nfs4_fs_locations_res res = {
3761 .fs_locations = fs_locations,
3762 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003763 struct rpc_message msg = {
3764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3765 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003766 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003767 };
3768 int status;
3769
Harvey Harrison3110ff82008-05-02 13:42:44 -07003770 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003771 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003772 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003773 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003774 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003775 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003776 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003777 return status;
3778}
3779
Andy Adamson557134a2009-04-01 09:21:53 -04003780#ifdef CONFIG_NFS_V4_1
3781/* Destroy the slot table */
3782static void nfs4_destroy_slot_table(struct nfs4_session *session)
3783{
3784 if (session->fc_slot_table.slots == NULL)
3785 return;
3786 kfree(session->fc_slot_table.slots);
3787 session->fc_slot_table.slots = NULL;
3788 return;
3789}
3790
3791struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3792{
3793 struct nfs4_session *session;
3794 struct nfs4_slot_table *tbl;
3795
3796 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3797 if (!session)
3798 return NULL;
3799 tbl = &session->fc_slot_table;
3800 spin_lock_init(&tbl->slot_tbl_lock);
3801 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3802 session->clp = clp;
3803 return session;
3804}
3805
3806void nfs4_destroy_session(struct nfs4_session *session)
3807{
3808 nfs4_destroy_slot_table(session);
3809 kfree(session);
3810}
3811
3812#endif /* CONFIG_NFS_V4_1 */
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003815 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003816 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 .recover_open = nfs4_open_reclaim,
3818 .recover_lock = nfs4_lock_reclaim,
3819};
3820
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003821struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003822 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003823 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 .recover_open = nfs4_open_expired,
3825 .recover_lock = nfs4_lock_expired,
3826};
3827
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003828static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003829 .permission = nfs_permission,
3830 .getattr = nfs_getattr,
3831 .setattr = nfs_setattr,
3832 .getxattr = nfs4_getxattr,
3833 .setxattr = nfs4_setxattr,
3834 .listxattr = nfs4_listxattr,
3835};
3836
David Howells509de812006-08-22 20:06:11 -04003837const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 .version = 4, /* protocol version */
3839 .dentry_ops = &nfs4_dentry_operations,
3840 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003841 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 .getroot = nfs4_proc_get_root,
3843 .getattr = nfs4_proc_getattr,
3844 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003845 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 .lookup = nfs4_proc_lookup,
3847 .access = nfs4_proc_access,
3848 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 .create = nfs4_proc_create,
3850 .remove = nfs4_proc_remove,
3851 .unlink_setup = nfs4_proc_unlink_setup,
3852 .unlink_done = nfs4_proc_unlink_done,
3853 .rename = nfs4_proc_rename,
3854 .link = nfs4_proc_link,
3855 .symlink = nfs4_proc_symlink,
3856 .mkdir = nfs4_proc_mkdir,
3857 .rmdir = nfs4_proc_remove,
3858 .readdir = nfs4_proc_readdir,
3859 .mknod = nfs4_proc_mknod,
3860 .statfs = nfs4_proc_statfs,
3861 .fsinfo = nfs4_proc_fsinfo,
3862 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003863 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 .decode_dirent = nfs4_decode_dirent,
3865 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003866 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003868 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003870 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003873 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874};
3875
3876/*
3877 * Local variables:
3878 * c-basic-offset: 8
3879 * End:
3880 */