blob: 6ca2795ccd9c7164f77c2a601841d1637af28550 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Chuck Lever006ea732006-03-20 13:44:14 -050054#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_PROC
57
Trond Myklebust2066fe82006-09-15 08:30:46 -040058#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFS4_POLL_RETRY_MAX (15*HZ)
60
Trond Myklebustcdd4e682006-01-03 09:55:12 +010061struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010062static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070066static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
David Howellsadfa6f92006-08-22 20:06:08 -040067static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
Trond Myklebustaac00a82007-07-05 19:02:21 -040068static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags);
Trond Myklebust99367812007-07-17 21:52:41 -040069static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Prevent leaks of NFSv4 errors into userland */
73int nfs4_map_errors(int err)
74{
75 if (err < -1000) {
76 dprintk("%s could not handle NFSv4 error %d\n",
77 __FUNCTION__, -err);
78 return -EIO;
79 }
80 return err;
81}
82
83/*
84 * This is our standard bitmap for GETATTR requests.
85 */
86const u32 nfs4_fattr_bitmap[2] = {
87 FATTR4_WORD0_TYPE
88 | FATTR4_WORD0_CHANGE
89 | FATTR4_WORD0_SIZE
90 | FATTR4_WORD0_FSID
91 | FATTR4_WORD0_FILEID,
92 FATTR4_WORD1_MODE
93 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER
95 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_RAWDEV
97 | FATTR4_WORD1_SPACE_USED
98 | FATTR4_WORD1_TIME_ACCESS
99 | FATTR4_WORD1_TIME_METADATA
100 | FATTR4_WORD1_TIME_MODIFY
101};
102
103const u32 nfs4_statfs_bitmap[2] = {
104 FATTR4_WORD0_FILES_AVAIL
105 | FATTR4_WORD0_FILES_FREE
106 | FATTR4_WORD0_FILES_TOTAL,
107 FATTR4_WORD1_SPACE_AVAIL
108 | FATTR4_WORD1_SPACE_FREE
109 | FATTR4_WORD1_SPACE_TOTAL
110};
111
Trond Myklebust4ce79712005-06-22 17:16:21 +0000112const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 FATTR4_WORD0_MAXLINK
114 | FATTR4_WORD0_MAXNAME,
115 0
116};
117
118const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
119 | FATTR4_WORD0_MAXREAD
120 | FATTR4_WORD0_MAXWRITE
121 | FATTR4_WORD0_LEASE_TIME,
122 0
123};
124
Manoj Naik830b8e32006-06-09 09:34:25 -0400125const u32 nfs4_fs_locations_bitmap[2] = {
126 FATTR4_WORD0_TYPE
127 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_SIZE
129 | FATTR4_WORD0_FSID
130 | FATTR4_WORD0_FILEID
131 | FATTR4_WORD0_FS_LOCATIONS,
132 FATTR4_WORD1_MODE
133 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER
135 | FATTR4_WORD1_OWNER_GROUP
136 | FATTR4_WORD1_RAWDEV
137 | FATTR4_WORD1_SPACE_USED
138 | FATTR4_WORD1_TIME_ACCESS
139 | FATTR4_WORD1_TIME_METADATA
140 | FATTR4_WORD1_TIME_MODIFY
141 | FATTR4_WORD1_MOUNTED_ON_FILEID
142};
143
Al Virobc4785c2006-10-19 23:28:51 -0700144static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct nfs4_readdir_arg *readdir)
146{
Al Viro0dbb4c62006-10-19 23:28:49 -0700147 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 BUG_ON(readdir->count < 80);
150 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000151 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
153 return;
154 }
155
156 readdir->cookie = 0;
157 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
158 if (cookie == 2)
159 return;
160
161 /*
162 * NFSv4 servers do not return entries for '.' and '..'
163 * Therefore, we fake these entries here. We let '.'
164 * have cookie 0 and '..' have cookie 1. Note that
165 * when talking to the server, we always send cookie 0
166 * instead of 1 or 2.
167 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700168 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (cookie == 0) {
171 *p++ = xdr_one; /* next */
172 *p++ = xdr_zero; /* cookie, first word */
173 *p++ = xdr_one; /* cookie, second word */
174 *p++ = xdr_one; /* entry len */
175 memcpy(p, ".\0\0\0", 4); /* entry */
176 p++;
177 *p++ = xdr_one; /* bitmap length */
178 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
179 *p++ = htonl(8); /* attribute buffer length */
180 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
181 }
182
183 *p++ = xdr_one; /* next */
184 *p++ = xdr_zero; /* cookie, first word */
185 *p++ = xdr_two; /* cookie, second word */
186 *p++ = xdr_two; /* entry len */
187 memcpy(p, "..\0\0", 4); /* entry */
188 p++;
189 *p++ = xdr_one; /* bitmap length */
190 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
191 *p++ = htonl(8); /* attribute buffer length */
192 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
193
194 readdir->pgbase = (char *)p - (char *)start;
195 readdir->count -= readdir->pgbase;
196 kunmap_atomic(start, KM_USER0);
197}
198
Trond Myklebust26e976a2006-01-03 09:55:21 +0100199static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David Howells7539bba2006-08-22 20:06:09 -0400201 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_lock(&clp->cl_lock);
203 if (time_before(clp->cl_last_renewal,timestamp))
204 clp->cl_last_renewal = timestamp;
205 spin_unlock(&clp->cl_lock);
206}
207
Trond Myklebust38478b22006-05-25 01:40:57 -0400208static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Trond Myklebust38478b22006-05-25 01:40:57 -0400210 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Trond Myklebust38478b22006-05-25 01:40:57 -0400212 spin_lock(&dir->i_lock);
213 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
215 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400216 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100219struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400220 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100221 struct nfs_openargs o_arg;
222 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100223 struct nfs_open_confirmargs c_arg;
224 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100225 struct nfs_fattr f_attr;
226 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400227 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100228 struct dentry *dir;
229 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400230 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100231 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100232 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400233 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100234 int rpc_status;
235 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100236};
237
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400238
239static void nfs4_init_opendata_res(struct nfs4_opendata *p)
240{
241 p->o_res.f_attr = &p->f_attr;
242 p->o_res.dir_attr = &p->dir_attr;
243 p->o_res.server = p->o_arg.server;
244 nfs_fattr_init(&p->f_attr);
245 nfs_fattr_init(&p->dir_attr);
246}
247
Trond Myklebustad389da2007-06-05 12:30:00 -0400248static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100249 struct nfs4_state_owner *sp, int flags,
250 const struct iattr *attrs)
251{
Trond Myklebustad389da2007-06-05 12:30:00 -0400252 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100253 struct inode *dir = parent->d_inode;
254 struct nfs_server *server = NFS_SERVER(dir);
255 struct nfs4_opendata *p;
256
257 p = kzalloc(sizeof(*p), GFP_KERNEL);
258 if (p == NULL)
259 goto err;
260 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
261 if (p->o_arg.seqid == NULL)
262 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400263 p->path.mnt = mntget(path->mnt);
264 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100265 p->dir = parent;
266 p->owner = sp;
267 atomic_inc(&sp->so_count);
268 p->o_arg.fh = NFS_FH(dir);
269 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400270 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400271 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400272 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100273 p->o_arg.server = server;
274 p->o_arg.bitmask = server->attr_bitmask;
275 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100276 if (flags & O_EXCL) {
277 u32 *s = (u32 *) p->o_arg.u.verifier.data;
278 s[0] = jiffies;
279 s[1] = current->pid;
280 } else if (flags & O_CREAT) {
281 p->o_arg.u.attrs = &p->attrs;
282 memcpy(&p->attrs, attrs, sizeof(p->attrs));
283 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100284 p->c_arg.fh = &p->o_res.fh;
285 p->c_arg.stateid = &p->o_res.stateid;
286 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400287 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400288 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100289 return p;
290err_free:
291 kfree(p);
292err:
293 dput(parent);
294 return NULL;
295}
296
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400297static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100298{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400299 struct nfs4_opendata *p = container_of(kref,
300 struct nfs4_opendata, kref);
301
302 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400303 if (p->state != NULL)
304 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400305 nfs4_put_state_owner(p->owner);
306 dput(p->dir);
307 dput(p->path.dentry);
308 mntput(p->path.mnt);
309 kfree(p);
310}
311
312static void nfs4_opendata_put(struct nfs4_opendata *p)
313{
314 if (p != NULL)
315 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100316}
317
Trond Myklebust06f814a2006-01-03 09:55:07 +0100318static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
319{
320 sigset_t oldset;
321 int ret;
322
323 rpc_clnt_sigmask(task->tk_client, &oldset);
324 ret = rpc_wait_for_completion_task(task);
325 rpc_clnt_sigunmask(task->tk_client, &oldset);
326 return ret;
327}
328
Trond Myklebust6ee41262007-07-08 14:11:36 -0400329static int can_open_cached(struct nfs4_state *state, int mode)
330{
331 int ret = 0;
332 switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
333 case FMODE_READ:
334 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
335 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
336 break;
337 case FMODE_WRITE:
338 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
339 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
340 break;
341 case FMODE_READ|FMODE_WRITE:
342 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
343 }
344 return ret;
345}
346
Trond Myklebustaac00a82007-07-05 19:02:21 -0400347static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
348{
349 if ((delegation->type & open_flags) != open_flags)
350 return 0;
351 if (delegation->flags & NFS_DELEGATION_NEED_RECLAIM)
352 return 0;
353 return 1;
354}
355
Trond Myklebust003707c2007-07-05 18:07:55 -0400356static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
Trond Myklebuste7616922006-01-03 09:55:13 +0100357{
358 switch (open_flags) {
359 case FMODE_WRITE:
360 state->n_wronly++;
361 break;
362 case FMODE_READ:
363 state->n_rdonly++;
364 break;
365 case FMODE_READ|FMODE_WRITE:
366 state->n_rdwr++;
367 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400368 nfs4_state_set_mode_locked(state, state->state | open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100369}
370
Trond Myklebust003707c2007-07-05 18:07:55 -0400371static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
372{
373 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
374 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
375 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
376 switch (open_flags) {
377 case FMODE_READ:
378 set_bit(NFS_O_RDONLY_STATE, &state->flags);
379 break;
380 case FMODE_WRITE:
381 set_bit(NFS_O_WRONLY_STATE, &state->flags);
382 break;
383 case FMODE_READ|FMODE_WRITE:
384 set_bit(NFS_O_RDWR_STATE, &state->flags);
385 }
386}
387
388static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
389{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400390 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400391 nfs_set_open_stateid_locked(state, stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400392 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400393}
394
395static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400398 /*
399 * Protect the call to nfs4_state_set_mode_locked and
400 * serialise the stateid update
401 */
402 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400403 if (deleg_stateid != NULL) {
404 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
405 set_bit(NFS_DELEGATED_STATE, &state->flags);
406 }
407 if (open_stateid != NULL)
408 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400409 write_sequnlock(&state->seqlock);
410 spin_lock(&state->owner->so_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100411 update_open_stateflags(state, open_flags);
Trond Myklebustec073422005-10-20 14:22:47 -0700412 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Trond Myklebustaac00a82007-07-05 19:02:21 -0400415static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
416{
417 struct nfs_delegation *delegation;
418
419 rcu_read_lock();
420 delegation = rcu_dereference(NFS_I(inode)->delegation);
421 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
422 rcu_read_unlock();
423 return;
424 }
425 rcu_read_unlock();
426 nfs_inode_return_delegation(inode);
427}
428
Trond Myklebust6ee41262007-07-08 14:11:36 -0400429static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400430{
431 struct nfs4_state *state = opendata->state;
432 struct nfs_inode *nfsi = NFS_I(state->inode);
433 struct nfs_delegation *delegation;
434 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
435 nfs4_stateid stateid;
436 int ret = -EAGAIN;
437
438 rcu_read_lock();
439 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400440 for (;;) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400441 if (can_open_cached(state, open_mode)) {
442 spin_lock(&state->owner->so_lock);
443 if (can_open_cached(state, open_mode)) {
444 update_open_stateflags(state, open_mode);
445 spin_unlock(&state->owner->so_lock);
446 rcu_read_unlock();
447 goto out_return_state;
448 }
449 spin_unlock(&state->owner->so_lock);
450 }
451 if (delegation == NULL)
452 break;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400453 if (!can_open_delegated(delegation, open_mode))
454 break;
455 /* Save the delegation */
456 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
457 rcu_read_unlock();
458 lock_kernel();
459 ret = _nfs4_do_access(state->inode, state->owner->so_cred, open_mode);
460 unlock_kernel();
461 if (ret != 0)
462 goto out;
463 ret = -EAGAIN;
464 rcu_read_lock();
465 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400466 /* If no delegation, try a cached open */
Trond Myklebustaac00a82007-07-05 19:02:21 -0400467 if (delegation == NULL)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400468 continue;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400469 /* Is the delegation still valid? */
470 if (memcmp(stateid.data, delegation->stateid.data, sizeof(stateid.data)) != 0)
471 continue;
472 rcu_read_unlock();
473 update_open_stateid(state, NULL, &stateid, open_mode);
474 goto out_return_state;
475 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400476 rcu_read_unlock();
477out:
478 return ERR_PTR(ret);
479out_return_state:
480 atomic_inc(&state->count);
481 return state;
482}
483
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100484static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
485{
486 struct inode *inode;
487 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400488 struct nfs_delegation *delegation;
489 nfs4_stateid *deleg_stateid = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400490 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100491
Trond Myklebustaac00a82007-07-05 19:02:21 -0400492 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400493 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400494 goto out;
495 }
496
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400497 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100498 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400499 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100500 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400501 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500502 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400503 goto err;
504 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100505 state = nfs4_get_open_state(inode, data->owner);
506 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400507 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400508 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400509 int delegation_flags = 0;
510
Trond Myklebust003707c2007-07-05 18:07:55 -0400511 rcu_read_lock();
512 delegation = rcu_dereference(NFS_I(inode)->delegation);
513 if (delegation)
514 delegation_flags = delegation->flags;
515 rcu_read_unlock();
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400516 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
517 nfs_inode_set_delegation(state->inode,
518 data->owner->so_cred,
519 &data->o_res);
520 else
521 nfs_inode_reclaim_delegation(state->inode,
522 data->owner->so_cred,
523 &data->o_res);
524 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400525 rcu_read_lock();
526 delegation = rcu_dereference(NFS_I(inode)->delegation);
527 if (delegation != NULL)
528 deleg_stateid = &delegation->stateid;
529 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
530 rcu_read_unlock();
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100531 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400532out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100533 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400534err_put_inode:
535 iput(inode);
536err:
537 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100538}
539
Trond Myklebust864472e2006-01-03 09:55:15 +0100540static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
541{
542 struct nfs_inode *nfsi = NFS_I(state->inode);
543 struct nfs_open_context *ctx;
544
545 spin_lock(&state->inode->i_lock);
546 list_for_each_entry(ctx, &nfsi->open_files, list) {
547 if (ctx->state != state)
548 continue;
549 get_nfs_open_context(ctx);
550 spin_unlock(&state->inode->i_lock);
551 return ctx;
552 }
553 spin_unlock(&state->inode->i_lock);
554 return ERR_PTR(-ENOENT);
555}
556
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400557static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
558{
559 struct nfs4_opendata *opendata;
560
561 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
562 if (opendata == NULL)
563 return ERR_PTR(-ENOMEM);
564 opendata->state = state;
565 atomic_inc(&state->count);
566 return opendata;
567}
568
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400569static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100570{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400571 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100572 int ret;
573
574 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400575 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
576 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
577 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100578 ret = _nfs4_proc_open(opendata);
579 if (ret != 0)
580 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400581 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400582 if (IS_ERR(newstate))
583 return PTR_ERR(newstate);
584 nfs4_close_state(&opendata->path, newstate, openflags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400585 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100586 return 0;
587}
588
589static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
590{
Trond Myklebust864472e2006-01-03 09:55:15 +0100591 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100592 int ret;
593
594 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400595 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100596 smp_rmb();
597 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400598 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100599 if (ret != 0)
600 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400601 if (newstate != state)
602 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100603 }
604 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400605 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100606 if (ret != 0)
607 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400608 if (newstate != state)
609 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100610 }
611 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400612 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100613 if (ret != 0)
614 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400615 if (newstate != state)
616 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100617 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400618 /*
619 * We may have performed cached opens for all three recoveries.
620 * Check if we need to update the current stateid.
621 */
622 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
623 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400624 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400625 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
626 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400627 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400628 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100629 return 0;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * OPEN_RECLAIM:
634 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400636static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400638 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100639 struct nfs4_opendata *opendata;
640 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int status;
642
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400643 opendata = nfs4_open_recoverdata_alloc(ctx, state);
644 if (IS_ERR(opendata))
645 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100646 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
647 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400648 rcu_read_lock();
649 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
650 if (delegation != NULL && (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) != 0)
651 delegation_type = delegation->flags;
652 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100653 opendata->o_arg.u.delegation_type = delegation_type;
654 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400655 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return status;
657}
658
Trond Myklebust539cd032007-06-05 11:46:42 -0400659static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct nfs_server *server = NFS_SERVER(state->inode);
662 struct nfs4_exception exception = { };
663 int err;
664 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400665 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000666 if (err != -NFS4ERR_DELAY)
667 break;
668 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 } while (exception.retry);
670 return err;
671}
672
Trond Myklebust864472e2006-01-03 09:55:15 +0100673static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
674{
675 struct nfs_open_context *ctx;
676 int ret;
677
678 ctx = nfs4_state_find_open_context(state);
679 if (IS_ERR(ctx))
680 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400681 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100682 put_nfs_open_context(ctx);
683 return ret;
684}
685
Trond Myklebust13437e12007-07-06 15:10:43 -0400686static 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 -0700687{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400691 opendata = nfs4_open_recoverdata_alloc(ctx, state);
692 if (IS_ERR(opendata))
693 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100694 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400695 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100696 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100697 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400698 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Trond Myklebust13437e12007-07-06 15:10:43 -0400702int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400705 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int err;
707 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400708 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 switch (err) {
710 case 0:
711 return err;
712 case -NFS4ERR_STALE_CLIENTID:
713 case -NFS4ERR_STALE_STATEID:
714 case -NFS4ERR_EXPIRED:
715 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400716 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return err;
718 }
719 err = nfs4_handle_exception(server, err, &exception);
720 } while (exception.retry);
721 return err;
722}
723
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100724static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100726 struct nfs4_opendata *data = calldata;
727 struct rpc_message msg = {
728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
729 .rpc_argp = &data->c_arg,
730 .rpc_resp = &data->c_res,
731 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100733 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100734 rpc_call_setup(task, &msg, 0);
735}
736
737static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
738{
739 struct nfs4_opendata *data = calldata;
740
741 data->rpc_status = task->tk_status;
742 if (RPC_ASSASSINATED(task))
743 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100744 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100745 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
746 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100747 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400748 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100749 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100750 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400751 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100752}
753
754static void nfs4_open_confirm_release(void *calldata)
755{
756 struct nfs4_opendata *data = calldata;
757 struct nfs4_state *state = NULL;
758
759 /* If this request hasn't been cancelled, do nothing */
760 if (data->cancelled == 0)
761 goto out_free;
762 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400763 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100764 goto out_free;
765 nfs_confirm_seqid(&data->owner->so_seqid, 0);
766 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400767 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400768 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100769out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100771}
772
773static const struct rpc_call_ops nfs4_open_confirm_ops = {
774 .rpc_call_prepare = nfs4_open_confirm_prepare,
775 .rpc_call_done = nfs4_open_confirm_done,
776 .rpc_release = nfs4_open_confirm_release,
777};
778
779/*
780 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
781 */
782static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
783{
784 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
785 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int status;
787
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400788 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400789 data->rpc_done = 0;
790 data->rpc_status = 0;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100791 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500792 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100793 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100794 status = nfs4_wait_for_completion_rpc_task(task);
795 if (status != 0) {
796 data->cancelled = 1;
797 smp_wmb();
798 } else
799 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500800 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return status;
802}
803
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100804static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100806 struct nfs4_opendata *data = calldata;
807 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct rpc_message msg = {
809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100810 .rpc_argp = &data->o_arg,
811 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .rpc_cred = sp->so_cred,
813 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100814
815 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
816 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400817 /*
818 * Check if we still need to send an OPEN call, or if we can use
819 * a delegation instead.
820 */
821 if (data->state != NULL) {
822 struct nfs_delegation *delegation;
823
Trond Myklebust6ee41262007-07-08 14:11:36 -0400824 if (can_open_cached(data->state, data->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL)))
825 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400826 rcu_read_lock();
827 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
828 if (delegation != NULL &&
829 (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) {
830 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400831 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400832 }
833 rcu_read_unlock();
834 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100835 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400836 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100837 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400838 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100839 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400840 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
841 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100842 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100843 rpc_call_setup(task, &msg, 0);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400844 return;
845out_no_action:
846 task->tk_action = NULL;
847
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100850static void nfs4_open_done(struct rpc_task *task, void *calldata)
851{
852 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100854 data->rpc_status = task->tk_status;
855 if (RPC_ASSASSINATED(task))
856 return;
857 if (task->tk_status == 0) {
858 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700859 case S_IFREG:
860 break;
861 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100862 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700863 break;
864 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100865 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700866 break;
867 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100868 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700869 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100870 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400871 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
872 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700873 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100874 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
Trond Myklebust3e309912007-07-07 13:19:59 -0400875 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100876}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700877
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100878static void nfs4_open_release(void *calldata)
879{
880 struct nfs4_opendata *data = calldata;
881 struct nfs4_state *state = NULL;
882
883 /* If this request hasn't been cancelled, do nothing */
884 if (data->cancelled == 0)
885 goto out_free;
886 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400887 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100888 goto out_free;
889 /* In case we need an open_confirm, no cleanup! */
890 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
891 goto out_free;
892 nfs_confirm_seqid(&data->owner->so_seqid, 0);
893 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400894 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400895 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100896out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100898}
899
900static const struct rpc_call_ops nfs4_open_ops = {
901 .rpc_call_prepare = nfs4_open_prepare,
902 .rpc_call_done = nfs4_open_done,
903 .rpc_release = nfs4_open_release,
904};
905
906/*
907 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
908 */
909static int _nfs4_proc_open(struct nfs4_opendata *data)
910{
911 struct inode *dir = data->dir->d_inode;
912 struct nfs_server *server = NFS_SERVER(dir);
913 struct nfs_openargs *o_arg = &data->o_arg;
914 struct nfs_openres *o_res = &data->o_res;
915 struct rpc_task *task;
916 int status;
917
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400918 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400919 data->rpc_done = 0;
920 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400921 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100922 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500923 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100924 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100925 status = nfs4_wait_for_completion_rpc_task(task);
926 if (status != 0) {
927 data->cancelled = 1;
928 smp_wmb();
929 } else
930 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500931 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -0400932 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100933 return status;
934
Trond Myklebust99367812007-07-17 21:52:41 -0400935 if (o_res->fh.size == 0)
936 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
937
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400938 if (o_arg->open_flags & O_CREAT) {
939 update_changeattr(dir, &o_res->cinfo);
940 nfs_post_op_update_inode(dir, o_res->dir_attr);
941 } else
942 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100944 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100946 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -0400949 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
954{
955 struct nfs_access_entry cache;
956 int mask = 0;
957 int status;
958
959 if (openflags & FMODE_READ)
960 mask |= MAY_READ;
961 if (openflags & FMODE_WRITE)
962 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400963 if (openflags & FMODE_EXEC)
964 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 status = nfs_access_get_cached(inode, cred, &cache);
966 if (status == 0)
967 goto out;
968
969 /* Be clever: ask server to check for all possible rights */
970 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
971 cache.cred = cred;
972 cache.jiffies = jiffies;
973 status = _nfs4_proc_access(inode, &cache);
974 if (status != 0)
975 return status;
976 nfs_access_add_cache(inode, &cache);
977out:
978 if ((cache.mask & mask) == mask)
979 return 0;
980 return -EACCES;
981}
982
Trond Myklebust10afec92007-05-14 17:16:04 -0400983static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100984{
David Howells7539bba2006-08-22 20:06:09 -0400985 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400986 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100987
Trond Myklebust6b309542006-09-14 14:03:14 -0400988 for (;;) {
989 ret = nfs4_wait_clnt_recover(server->client, clp);
990 if (ret != 0)
991 return ret;
992 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
993 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100994 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400995 }
996 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/*
1000 * OPEN_EXPIRED:
1001 * reclaim state on the server after a network partition.
1002 * Assumes caller holds the appropriate lock
1003 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001004static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001007 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001009 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1010 if (IS_ERR(opendata))
1011 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001012 ret = nfs4_open_recover(opendata, state);
1013 if (ret == -ESTALE) {
1014 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -04001015 nfs4_drop_state_owner(state->owner);
1016 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001018 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Trond Myklebust539cd032007-06-05 11:46:42 -04001022static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001023{
Trond Myklebust539cd032007-06-05 11:46:42 -04001024 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001025 struct nfs4_exception exception = { };
1026 int err;
1027
1028 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001029 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001030 if (err == -NFS4ERR_DELAY)
1031 nfs4_handle_exception(server, err, &exception);
1032 } while (exception.retry);
1033 return err;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001039 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Trond Myklebust864472e2006-01-03 09:55:15 +01001041 ctx = nfs4_state_find_open_context(state);
1042 if (IS_ERR(ctx))
1043 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001044 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001045 put_nfs_open_context(ctx);
1046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001050 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1051 * fields corresponding to attributes that were used to store the verifier.
1052 * Make sure we clobber those fields in the later setattr call
1053 */
1054static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1055{
1056 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1057 !(sattr->ia_valid & ATTR_ATIME_SET))
1058 sattr->ia_valid |= ATTR_ATIME;
1059
1060 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1061 !(sattr->ia_valid & ATTR_MTIME_SET))
1062 sattr->ia_valid |= ATTR_MTIME;
1063}
1064
1065/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001068static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct nfs4_state_owner *sp;
1071 struct nfs4_state *state = NULL;
1072 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -04001073 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001074 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 status = -ENOMEM;
1079 if (!(sp = nfs4_get_state_owner(server, cred))) {
1080 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1081 goto out_err;
1082 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001083 status = nfs4_recover_expired_lease(server);
1084 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001085 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 if (path->dentry->d_inode != NULL)
1087 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
Trond Myklebust58d97142006-01-03 09:55:24 +01001088 down_read(&clp->cl_sem);
1089 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001090 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001091 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -04001092 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 if (path->dentry->d_inode != NULL)
1095 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1096
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001099 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001101 if (opendata->o_arg.open_flags & O_EXCL)
1102 nfs4_exclusive_attrset(opendata, sattr);
1103
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001104 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 status = PTR_ERR(state);
1106 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001107 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001108 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 nfs4_put_state_owner(sp);
1110 up_read(&clp->cl_sem);
1111 *res = state;
1112 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001113err_opendata_put:
1114 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001115err_release_rwsem:
1116 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001117err_put_state_owner:
1118 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *res = NULL;
1121 return status;
1122}
1123
1124
Trond Myklebustad389da2007-06-05 12:30:00 -04001125static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct nfs4_exception exception = { };
1128 struct nfs4_state *res;
1129 int status;
1130
1131 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001132 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (status == 0)
1134 break;
1135 /* NOTE: BAD_SEQID means the server and client disagree about the
1136 * book-keeping w.r.t. state-changing operations
1137 * (OPEN/CLOSE/LOCK/LOCKU...)
1138 * It is actually a sign of a bug on the client or on the server.
1139 *
1140 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001141 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * have unhashed the old state_owner for us, and that we can
1143 * therefore safely retry using a new one. We should still warn
1144 * the user though...
1145 */
1146 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001147 printk(KERN_WARNING "NFS: v4 server %s "
1148 " returned a bad sequence-id error!\n",
1149 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 exception.retry = 1;
1151 continue;
1152 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001153 /*
1154 * BAD_STATEID on OPEN means that the server cancelled our
1155 * state before it received the OPEN_CONFIRM.
1156 * Recover by retrying the request as per the discussion
1157 * on Page 181 of RFC3530.
1158 */
1159 if (status == -NFS4ERR_BAD_STATEID) {
1160 exception.retry = 1;
1161 continue;
1162 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163 if (status == -EAGAIN) {
1164 /* We must have found a delegation */
1165 exception.retry = 1;
1166 continue;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1169 status, &exception));
1170 } while (exception.retry);
1171 return res;
1172}
1173
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001174static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1175 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001177 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001179 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 .iap = sattr,
1181 .server = server,
1182 .bitmask = server->attr_bitmask,
1183 };
1184 struct nfs_setattrres res = {
1185 .fattr = fattr,
1186 .server = server,
1187 };
1188 struct rpc_message msg = {
1189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1190 .rpc_argp = &arg,
1191 .rpc_resp = &res,
1192 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001193 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001194 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Trond Myklebust0e574af2005-10-27 22:12:38 -04001196 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001198 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1199 /* Use that stateid */
1200 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001202 nfs4_copy_stateid(&arg.stateid, state, current->files);
1203 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1205
Trond Myklebust65e43082005-08-16 11:49:44 -04001206 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001207 if (status == 0 && state != NULL)
1208 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001209 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001212static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1213 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001215 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct nfs4_exception exception = { };
1217 int err;
1218 do {
1219 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001220 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 &exception);
1222 } while (exception.retry);
1223 return err;
1224}
1225
1226struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001227 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct inode *inode;
1229 struct nfs4_state *state;
1230 struct nfs_closeargs arg;
1231 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001232 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001233 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234};
1235
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001236static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001237{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001238 struct nfs4_closedata *calldata = data;
1239 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001240
1241 nfs4_put_open_state(calldata->state);
1242 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001243 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001244 dput(calldata->path.dentry);
1245 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001246 kfree(calldata);
1247}
1248
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001249static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001251 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct nfs_server *server = NFS_SERVER(calldata->inode);
1254
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001255 if (RPC_ASSASSINATED(task))
1256 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* hmm. we are done with the inode, and in the process of freeing
1258 * the state_owner. we keep this around to process errors
1259 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001260 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 switch (task->tk_status) {
1262 case 0:
Trond Myklebust003707c2007-07-05 18:07:55 -04001263 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001264 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 case -NFS4ERR_STALE_STATEID:
1267 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 break;
1269 default:
1270 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1271 rpc_restart_call(task);
1272 return;
1273 }
1274 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001275 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001278static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001280 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001281 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct rpc_message msg = {
1283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1284 .rpc_argp = &calldata->arg,
1285 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001286 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 };
Trond Myklebust003707c2007-07-05 18:07:55 -04001288 int clear_rd, clear_wr, clear_rdwr;
1289 int mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001290
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001291 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001292 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001293
1294 mode = FMODE_READ|FMODE_WRITE;
1295 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001296 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001297 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001298 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001299 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001300 mode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001301 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1302 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1303 }
1304 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001305 mode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001306 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1307 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1308 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001309 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001310 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001311 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001312 /* Note: exit _without_ calling nfs4_close_done */
1313 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001314 return;
1315 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001316 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001317 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001319 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001320 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001321 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001324static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001325 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001326 .rpc_call_done = nfs4_close_done,
1327 .rpc_release = nfs4_free_closedata,
1328};
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/*
1331 * It is possible for data to be read/written from a mem-mapped file
1332 * after the sys_close call (which hits the vfs layer as a flush).
1333 * This means that we can't safely call nfsv4 close on a file until
1334 * the inode is cleared. This in turn means that we are not good
1335 * NFSv4 citizens - we do not indicate to the server to update the file's
1336 * share state even when we are done with one of the three share
1337 * stateid's in the inode.
1338 *
1339 * NOTE: Caller must be holding the sp->so_owner semaphore!
1340 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001341int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001343 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001345 struct nfs4_state_owner *sp = state->owner;
1346 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001347 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Trond Myklebust95121352005-10-18 14:20:12 -07001349 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001351 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001352 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001354 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001355 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001357 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001358 if (calldata->arg.seqid == NULL)
1359 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001360 calldata->arg.bitmask = server->attr_bitmask;
1361 calldata->res.fattr = &calldata->fattr;
1362 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001363 calldata->path.mnt = mntget(path->mnt);
1364 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001365
Trond Myklebustb39e6252007-06-11 23:05:07 -04001366 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1367 if (IS_ERR(task))
1368 return PTR_ERR(task);
1369 rpc_put_task(task);
1370 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001371out_free_calldata:
1372 kfree(calldata);
1373out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001374 nfs4_put_open_state(state);
1375 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001376 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001379static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001380{
1381 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001382 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001383
Trond Myklebust1b45c462007-07-03 13:04:56 -04001384 /* If the open_intent is for execute, we have an extra check to make */
1385 if (nd->intent.open.flags & FMODE_EXEC) {
1386 ret = _nfs4_do_access(state->inode,
1387 state->owner->so_cred,
1388 nd->intent.open.flags);
1389 if (ret < 0)
1390 goto out_close;
1391 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001392 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001393 if (!IS_ERR(filp)) {
1394 struct nfs_open_context *ctx;
1395 ctx = (struct nfs_open_context *)filp->private_data;
1396 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001397 return 0;
1398 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001399 ret = PTR_ERR(filp);
1400out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001401 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001402 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001403}
1404
1405struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1407{
Trond Myklebustad389da2007-06-05 12:30:00 -04001408 struct path path = {
1409 .mnt = nd->mnt,
1410 .dentry = dentry,
1411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct iattr attr;
1413 struct rpc_cred *cred;
1414 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001415 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (nd->flags & LOOKUP_CREATE) {
1418 attr.ia_mode = nd->intent.open.create_mode;
1419 attr.ia_valid = ATTR_MODE;
1420 if (!IS_POSIXACL(dir))
1421 attr.ia_mode &= ~current->fs->umask;
1422 } else {
1423 attr.ia_valid = 0;
1424 BUG_ON(nd->intent.open.flags & O_CREAT);
1425 }
1426
David Howells1f163412006-08-22 20:06:11 -04001427 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001429 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001430 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001432 if (IS_ERR(state)) {
1433 if (PTR_ERR(state) == -ENOENT)
1434 d_add(dentry, NULL);
1435 return (struct dentry *)state;
1436 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001438 if (res != NULL)
1439 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001440 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001441 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001445nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Trond Myklebustad389da2007-06-05 12:30:00 -04001447 struct path path = {
1448 .mnt = nd->mnt,
1449 .dentry = dentry,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct rpc_cred *cred;
1452 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
David Howells1f163412006-08-22 20:06:11 -04001454 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (IS_ERR(cred))
1456 return PTR_ERR(cred);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001457 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001459 if (IS_ERR(state)) {
1460 switch (PTR_ERR(state)) {
1461 case -EPERM:
1462 case -EACCES:
1463 case -EDQUOT:
1464 case -ENOSPC:
1465 case -EROFS:
1466 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1467 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001468 default:
1469 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001470 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001471 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001473 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 1;
1475 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001476 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001477out_drop:
1478 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
1481
1482
1483static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1484{
1485 struct nfs4_server_caps_res res = {};
1486 struct rpc_message msg = {
1487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1488 .rpc_argp = fhandle,
1489 .rpc_resp = &res,
1490 };
1491 int status;
1492
1493 status = rpc_call_sync(server->client, &msg, 0);
1494 if (status == 0) {
1495 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1496 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1497 server->caps |= NFS_CAP_ACLS;
1498 if (res.has_links != 0)
1499 server->caps |= NFS_CAP_HARDLINKS;
1500 if (res.has_symlinks != 0)
1501 server->caps |= NFS_CAP_SYMLINKS;
1502 server->acl_bitmask = res.acl_bitmask;
1503 }
1504 return status;
1505}
1506
Trond Myklebust55a97592006-06-09 09:34:19 -04001507int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct nfs4_exception exception = { };
1510 int err;
1511 do {
1512 err = nfs4_handle_exception(server,
1513 _nfs4_server_capabilities(server, fhandle),
1514 &exception);
1515 } while (exception.retry);
1516 return err;
1517}
1518
1519static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1520 struct nfs_fsinfo *info)
1521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct nfs4_lookup_root_arg args = {
1523 .bitmask = nfs4_fattr_bitmap,
1524 };
1525 struct nfs4_lookup_res res = {
1526 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001527 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 .fh = fhandle,
1529 };
1530 struct rpc_message msg = {
1531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1532 .rpc_argp = &args,
1533 .rpc_resp = &res,
1534 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001535 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return rpc_call_sync(server->client, &msg, 0);
1537}
1538
1539static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1540 struct nfs_fsinfo *info)
1541{
1542 struct nfs4_exception exception = { };
1543 int err;
1544 do {
1545 err = nfs4_handle_exception(server,
1546 _nfs4_lookup_root(server, fhandle, info),
1547 &exception);
1548 } while (exception.retry);
1549 return err;
1550}
1551
David Howells54ceac42006-08-22 20:06:13 -04001552/*
1553 * get the file handle for the "/" directory on the server
1554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001556 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int status;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (status == 0)
1562 status = nfs4_server_capabilities(server, fhandle);
1563 if (status == 0)
1564 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001565 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Manoj Naik6b97fd32006-06-09 09:34:29 -04001568/*
1569 * Get locations and (maybe) other attributes of a referral.
1570 * Note that we'll actually follow the referral later when
1571 * we detect fsid mismatch in inode revalidation
1572 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001573static 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 -04001574{
1575 int status = -ENOMEM;
1576 struct page *page = NULL;
1577 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001578
1579 page = alloc_page(GFP_KERNEL);
1580 if (page == NULL)
1581 goto out;
1582 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1583 if (locations == NULL)
1584 goto out;
1585
Trond Myklebustc228fd32007-01-13 02:28:11 -05001586 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001587 if (status != 0)
1588 goto out;
1589 /* Make sure server returned a different fsid for the referral */
1590 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1591 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1592 status = -EIO;
1593 goto out;
1594 }
1595
1596 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1597 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1598 if (!fattr->mode)
1599 fattr->mode = S_IFDIR;
1600 memset(fhandle, 0, sizeof(struct nfs_fh));
1601out:
1602 if (page)
1603 __free_page(page);
1604 if (locations)
1605 kfree(locations);
1606 return status;
1607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1610{
1611 struct nfs4_getattr_arg args = {
1612 .fh = fhandle,
1613 .bitmask = server->attr_bitmask,
1614 };
1615 struct nfs4_getattr_res res = {
1616 .fattr = fattr,
1617 .server = server,
1618 };
1619 struct rpc_message msg = {
1620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1621 .rpc_argp = &args,
1622 .rpc_resp = &res,
1623 };
1624
Trond Myklebust0e574af2005-10-27 22:12:38 -04001625 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return rpc_call_sync(server->client, &msg, 0);
1627}
1628
1629static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1630{
1631 struct nfs4_exception exception = { };
1632 int err;
1633 do {
1634 err = nfs4_handle_exception(server,
1635 _nfs4_proc_getattr(server, fhandle, fattr),
1636 &exception);
1637 } while (exception.retry);
1638 return err;
1639}
1640
1641/*
1642 * The file is not closed if it is opened due to the a request to change
1643 * the size of the file. The open call will not be needed once the
1644 * VFS layer lookup-intents are implemented.
1645 *
1646 * Close is called when the inode is destroyed.
1647 * If we haven't opened the file for O_WRONLY, we
1648 * need to in the size_change case to obtain a stateid.
1649 *
1650 * Got race?
1651 * Because OPEN is always done by name in nfsv4, it is
1652 * possible that we opened a different file by the same
1653 * name. We can recognize this race condition, but we
1654 * can't do anything about it besides returning an error.
1655 *
1656 * This will be fixed with VFS changes (lookup-intent).
1657 */
1658static int
1659nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1660 struct iattr *sattr)
1661{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001662 struct rpc_cred *cred;
1663 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001664 struct nfs_open_context *ctx;
1665 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int status;
1667
Trond Myklebust0e574af2005-10-27 22:12:38 -04001668 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David Howells1f163412006-08-22 20:06:11 -04001670 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001671 if (IS_ERR(cred))
1672 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001673
1674 /* Search for an existing open(O_WRITE) file */
1675 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1676 if (ctx != NULL)
1677 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001678
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001679 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001680 if (status == 0)
1681 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001682 if (ctx != NULL)
1683 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001684 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return status;
1686}
1687
Trond Myklebust56659e92007-07-17 21:52:39 -04001688static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1689 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001690 struct nfs_fattr *fattr)
1691{
1692 int status;
1693 struct nfs4_lookup_arg args = {
1694 .bitmask = server->attr_bitmask,
1695 .dir_fh = dirfh,
1696 .name = name,
1697 };
1698 struct nfs4_lookup_res res = {
1699 .server = server,
1700 .fattr = fattr,
1701 .fh = fhandle,
1702 };
1703 struct rpc_message msg = {
1704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1705 .rpc_argp = &args,
1706 .rpc_resp = &res,
1707 };
1708
1709 nfs_fattr_init(fattr);
1710
1711 dprintk("NFS call lookupfh %s\n", name->name);
1712 status = rpc_call_sync(server->client, &msg, 0);
1713 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001714 return status;
1715}
1716
1717static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1718 struct qstr *name, struct nfs_fh *fhandle,
1719 struct nfs_fattr *fattr)
1720{
1721 struct nfs4_exception exception = { };
1722 int err;
1723 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001724 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1725 /* FIXME: !!!! */
1726 if (err == -NFS4ERR_MOVED) {
1727 err = -EREMOTE;
1728 break;
1729 }
1730 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001731 } while (exception.retry);
1732 return err;
1733}
1734
Trond Myklebust56659e92007-07-17 21:52:39 -04001735static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1737{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001738 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001741 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001742 if (status == -NFS4ERR_MOVED)
1743 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 dprintk("NFS reply lookup: %d\n", status);
1745 return status;
1746}
1747
1748static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1749{
1750 struct nfs4_exception exception = { };
1751 int err;
1752 do {
1753 err = nfs4_handle_exception(NFS_SERVER(dir),
1754 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1755 &exception);
1756 } while (exception.retry);
1757 return err;
1758}
1759
1760static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1761{
1762 struct nfs4_accessargs args = {
1763 .fh = NFS_FH(inode),
1764 };
1765 struct nfs4_accessres res = { 0 };
1766 struct rpc_message msg = {
1767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1768 .rpc_argp = &args,
1769 .rpc_resp = &res,
1770 .rpc_cred = entry->cred,
1771 };
1772 int mode = entry->mask;
1773 int status;
1774
1775 /*
1776 * Determine which access bits we want to ask for...
1777 */
1778 if (mode & MAY_READ)
1779 args.access |= NFS4_ACCESS_READ;
1780 if (S_ISDIR(inode->i_mode)) {
1781 if (mode & MAY_WRITE)
1782 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1783 if (mode & MAY_EXEC)
1784 args.access |= NFS4_ACCESS_LOOKUP;
1785 } else {
1786 if (mode & MAY_WRITE)
1787 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1788 if (mode & MAY_EXEC)
1789 args.access |= NFS4_ACCESS_EXECUTE;
1790 }
1791 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1792 if (!status) {
1793 entry->mask = 0;
1794 if (res.access & NFS4_ACCESS_READ)
1795 entry->mask |= MAY_READ;
1796 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1797 entry->mask |= MAY_WRITE;
1798 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1799 entry->mask |= MAY_EXEC;
1800 }
1801 return status;
1802}
1803
1804static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1805{
1806 struct nfs4_exception exception = { };
1807 int err;
1808 do {
1809 err = nfs4_handle_exception(NFS_SERVER(inode),
1810 _nfs4_proc_access(inode, entry),
1811 &exception);
1812 } while (exception.retry);
1813 return err;
1814}
1815
1816/*
1817 * TODO: For the time being, we don't try to get any attributes
1818 * along with any of the zero-copy operations READ, READDIR,
1819 * READLINK, WRITE.
1820 *
1821 * In the case of the first three, we want to put the GETATTR
1822 * after the read-type operation -- this is because it is hard
1823 * to predict the length of a GETATTR response in v4, and thus
1824 * align the READ data correctly. This means that the GETATTR
1825 * may end up partially falling into the page cache, and we should
1826 * shift it into the 'tail' of the xdr_buf before processing.
1827 * To do this efficiently, we need to know the total length
1828 * of data received, which doesn't seem to be available outside
1829 * of the RPC layer.
1830 *
1831 * In the case of WRITE, we also want to put the GETATTR after
1832 * the operation -- in this case because we want to make sure
1833 * we get the post-operation mtime and size. This means that
1834 * we can't use xdr_encode_pages() as written: we need a variant
1835 * of it which would leave room in the 'tail' iovec.
1836 *
1837 * Both of these changes to the XDR layer would in fact be quite
1838 * minor, but I decided to leave them for a subsequent patch.
1839 */
1840static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1841 unsigned int pgbase, unsigned int pglen)
1842{
1843 struct nfs4_readlink args = {
1844 .fh = NFS_FH(inode),
1845 .pgbase = pgbase,
1846 .pglen = pglen,
1847 .pages = &page,
1848 };
1849 struct rpc_message msg = {
1850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1851 .rpc_argp = &args,
1852 .rpc_resp = NULL,
1853 };
1854
1855 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1856}
1857
1858static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1859 unsigned int pgbase, unsigned int pglen)
1860{
1861 struct nfs4_exception exception = { };
1862 int err;
1863 do {
1864 err = nfs4_handle_exception(NFS_SERVER(inode),
1865 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1866 &exception);
1867 } while (exception.retry);
1868 return err;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/*
1872 * Got race?
1873 * We will need to arrange for the VFS layer to provide an atomic open.
1874 * Until then, this create/open method is prone to inefficiency and race
1875 * conditions due to the lookup, create, and open VFS calls from sys_open()
1876 * placed on the wire.
1877 *
1878 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1879 * The file will be opened again in the subsequent VFS open call
1880 * (nfs4_proc_file_open).
1881 *
1882 * The open for read will just hang around to be used by any process that
1883 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1884 */
1885
1886static int
1887nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001888 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Trond Myklebustad389da2007-06-05 12:30:00 -04001890 struct path path = {
1891 .mnt = nd->mnt,
1892 .dentry = dentry,
1893 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct nfs4_state *state;
1895 struct rpc_cred *cred;
1896 int status = 0;
1897
David Howells1f163412006-08-22 20:06:11 -04001898 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (IS_ERR(cred)) {
1900 status = PTR_ERR(cred);
1901 goto out;
1902 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001903 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 put_rpccred(cred);
1905 if (IS_ERR(state)) {
1906 status = PTR_ERR(state);
1907 goto out;
1908 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001909 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (flags & O_EXCL) {
1911 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001912 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001913 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001914 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001915 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001916 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001917 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001918 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001919 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001920 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921out:
1922 return status;
1923}
1924
1925static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1926{
Trond Myklebust16e42952005-10-27 22:12:44 -04001927 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001928 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001930 .name.len = name->len,
1931 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001932 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001934 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001935 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04001936 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1939 .rpc_argp = &args,
1940 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 };
1942 int status;
1943
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001944 nfs_fattr_init(&res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001945 status = rpc_call_sync(server->client, &msg, 0);
1946 if (status == 0) {
1947 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001948 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return status;
1951}
1952
1953static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1954{
1955 struct nfs4_exception exception = { };
1956 int err;
1957 do {
1958 err = nfs4_handle_exception(NFS_SERVER(dir),
1959 _nfs4_proc_remove(dir, name),
1960 &exception);
1961 } while (exception.retry);
1962 return err;
1963}
1964
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001965static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001967 struct nfs_server *server = NFS_SERVER(dir);
1968 struct nfs_removeargs *args = msg->rpc_argp;
1969 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001971 args->bitmask = server->attr_bitmask;
1972 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001976static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001978 struct nfs_removeres *res = task->tk_msg.rpc_resp;
1979
1980 if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
1981 return 0;
1982 update_changeattr(dir, &res->cinfo);
1983 nfs_post_op_update_inode(dir, &res->dir_attr);
1984 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1988 struct inode *new_dir, struct qstr *new_name)
1989{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001990 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct nfs4_rename_arg arg = {
1992 .old_dir = NFS_FH(old_dir),
1993 .new_dir = NFS_FH(new_dir),
1994 .old_name = old_name,
1995 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001996 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001998 struct nfs_fattr old_fattr, new_fattr;
1999 struct nfs4_rename_res res = {
2000 .server = server,
2001 .old_fattr = &old_fattr,
2002 .new_fattr = &new_fattr,
2003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct rpc_message msg = {
2005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2006 .rpc_argp = &arg,
2007 .rpc_resp = &res,
2008 };
2009 int status;
2010
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002011 nfs_fattr_init(res.old_fattr);
2012 nfs_fattr_init(res.new_fattr);
2013 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (!status) {
2016 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002017 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002019 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 return status;
2022}
2023
2024static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2025 struct inode *new_dir, struct qstr *new_name)
2026{
2027 struct nfs4_exception exception = { };
2028 int err;
2029 do {
2030 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2031 _nfs4_proc_rename(old_dir, old_name,
2032 new_dir, new_name),
2033 &exception);
2034 } while (exception.retry);
2035 return err;
2036}
2037
2038static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2039{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002040 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 struct nfs4_link_arg arg = {
2042 .fh = NFS_FH(inode),
2043 .dir_fh = NFS_FH(dir),
2044 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002045 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002047 struct nfs_fattr fattr, dir_attr;
2048 struct nfs4_link_res res = {
2049 .server = server,
2050 .fattr = &fattr,
2051 .dir_attr = &dir_attr,
2052 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct rpc_message msg = {
2054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2055 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002056 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 };
2058 int status;
2059
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002060 nfs_fattr_init(res.fattr);
2061 nfs_fattr_init(res.dir_attr);
2062 status = rpc_call_sync(server->client, &msg, 0);
2063 if (!status) {
2064 update_changeattr(dir, &res.cinfo);
2065 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002066 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 return status;
2070}
2071
2072static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2073{
2074 struct nfs4_exception exception = { };
2075 int err;
2076 do {
2077 err = nfs4_handle_exception(NFS_SERVER(inode),
2078 _nfs4_proc_link(inode, dir, name),
2079 &exception);
2080 } while (exception.retry);
2081 return err;
2082}
2083
Chuck Lever4f390c12006-08-22 20:06:22 -04002084static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002085 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002088 struct nfs_fh fhandle;
2089 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct nfs4_create_arg arg = {
2091 .dir_fh = NFS_FH(dir),
2092 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002093 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 .attrs = sattr,
2095 .ftype = NF4LNK,
2096 .bitmask = server->attr_bitmask,
2097 };
2098 struct nfs4_create_res res = {
2099 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002100 .fh = &fhandle,
2101 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002102 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 };
2104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2106 .rpc_argp = &arg,
2107 .rpc_resp = &res,
2108 };
2109 int status;
2110
Chuck Lever94a6d752006-08-22 20:06:23 -04002111 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002113
Chuck Lever94a6d752006-08-22 20:06:23 -04002114 arg.u.symlink.pages = &page;
2115 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002116 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002117 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002120 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002122 nfs_post_op_update_inode(dir, res.dir_fattr);
2123 status = nfs_instantiate(dentry, &fhandle, &fattr);
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return status;
2126}
2127
Chuck Lever4f390c12006-08-22 20:06:22 -04002128static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002129 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 struct nfs4_exception exception = { };
2132 int err;
2133 do {
2134 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002135 _nfs4_proc_symlink(dir, dentry, page,
2136 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 &exception);
2138 } while (exception.retry);
2139 return err;
2140}
2141
2142static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2143 struct iattr *sattr)
2144{
2145 struct nfs_server *server = NFS_SERVER(dir);
2146 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002147 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct nfs4_create_arg arg = {
2149 .dir_fh = NFS_FH(dir),
2150 .server = server,
2151 .name = &dentry->d_name,
2152 .attrs = sattr,
2153 .ftype = NF4DIR,
2154 .bitmask = server->attr_bitmask,
2155 };
2156 struct nfs4_create_res res = {
2157 .server = server,
2158 .fh = &fhandle,
2159 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002160 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 };
2162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2164 .rpc_argp = &arg,
2165 .rpc_resp = &res,
2166 };
2167 int status;
2168
Trond Myklebust0e574af2005-10-27 22:12:38 -04002169 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002170 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2173 if (!status) {
2174 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002175 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 status = nfs_instantiate(dentry, &fhandle, &fattr);
2177 }
2178 return status;
2179}
2180
2181static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2182 struct iattr *sattr)
2183{
2184 struct nfs4_exception exception = { };
2185 int err;
2186 do {
2187 err = nfs4_handle_exception(NFS_SERVER(dir),
2188 _nfs4_proc_mkdir(dir, dentry, sattr),
2189 &exception);
2190 } while (exception.retry);
2191 return err;
2192}
2193
2194static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2195 u64 cookie, struct page *page, unsigned int count, int plus)
2196{
2197 struct inode *dir = dentry->d_inode;
2198 struct nfs4_readdir_arg args = {
2199 .fh = NFS_FH(dir),
2200 .pages = &page,
2201 .pgbase = 0,
2202 .count = count,
2203 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2204 };
2205 struct nfs4_readdir_res res;
2206 struct rpc_message msg = {
2207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2208 .rpc_argp = &args,
2209 .rpc_resp = &res,
2210 .rpc_cred = cred,
2211 };
2212 int status;
2213
Trond Myklebusteadf4592005-06-22 17:16:39 +00002214 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2215 dentry->d_parent->d_name.name,
2216 dentry->d_name.name,
2217 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2219 res.pgbase = args.pgbase;
2220 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2221 if (status == 0)
2222 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002223 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return status;
2225}
2226
2227static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2228 u64 cookie, struct page *page, unsigned int count, int plus)
2229{
2230 struct nfs4_exception exception = { };
2231 int err;
2232 do {
2233 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2234 _nfs4_proc_readdir(dentry, cred, cookie,
2235 page, count, plus),
2236 &exception);
2237 } while (exception.retry);
2238 return err;
2239}
2240
2241static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2242 struct iattr *sattr, dev_t rdev)
2243{
2244 struct nfs_server *server = NFS_SERVER(dir);
2245 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002246 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct nfs4_create_arg arg = {
2248 .dir_fh = NFS_FH(dir),
2249 .server = server,
2250 .name = &dentry->d_name,
2251 .attrs = sattr,
2252 .bitmask = server->attr_bitmask,
2253 };
2254 struct nfs4_create_res res = {
2255 .server = server,
2256 .fh = &fh,
2257 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002258 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 };
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2262 .rpc_argp = &arg,
2263 .rpc_resp = &res,
2264 };
2265 int status;
2266 int mode = sattr->ia_mode;
2267
Trond Myklebust0e574af2005-10-27 22:12:38 -04002268 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002269 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2272 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2273 if (S_ISFIFO(mode))
2274 arg.ftype = NF4FIFO;
2275 else if (S_ISBLK(mode)) {
2276 arg.ftype = NF4BLK;
2277 arg.u.device.specdata1 = MAJOR(rdev);
2278 arg.u.device.specdata2 = MINOR(rdev);
2279 }
2280 else if (S_ISCHR(mode)) {
2281 arg.ftype = NF4CHR;
2282 arg.u.device.specdata1 = MAJOR(rdev);
2283 arg.u.device.specdata2 = MINOR(rdev);
2284 }
2285 else
2286 arg.ftype = NF4SOCK;
2287
2288 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2289 if (status == 0) {
2290 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002291 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 status = nfs_instantiate(dentry, &fh, &fattr);
2293 }
2294 return status;
2295}
2296
2297static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2298 struct iattr *sattr, dev_t rdev)
2299{
2300 struct nfs4_exception exception = { };
2301 int err;
2302 do {
2303 err = nfs4_handle_exception(NFS_SERVER(dir),
2304 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2305 &exception);
2306 } while (exception.retry);
2307 return err;
2308}
2309
2310static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2311 struct nfs_fsstat *fsstat)
2312{
2313 struct nfs4_statfs_arg args = {
2314 .fh = fhandle,
2315 .bitmask = server->attr_bitmask,
2316 };
2317 struct rpc_message msg = {
2318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2319 .rpc_argp = &args,
2320 .rpc_resp = fsstat,
2321 };
2322
Trond Myklebust0e574af2005-10-27 22:12:38 -04002323 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return rpc_call_sync(server->client, &msg, 0);
2325}
2326
2327static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2328{
2329 struct nfs4_exception exception = { };
2330 int err;
2331 do {
2332 err = nfs4_handle_exception(server,
2333 _nfs4_proc_statfs(server, fhandle, fsstat),
2334 &exception);
2335 } while (exception.retry);
2336 return err;
2337}
2338
2339static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2340 struct nfs_fsinfo *fsinfo)
2341{
2342 struct nfs4_fsinfo_arg args = {
2343 .fh = fhandle,
2344 .bitmask = server->attr_bitmask,
2345 };
2346 struct rpc_message msg = {
2347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2348 .rpc_argp = &args,
2349 .rpc_resp = fsinfo,
2350 };
2351
2352 return rpc_call_sync(server->client, &msg, 0);
2353}
2354
2355static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2356{
2357 struct nfs4_exception exception = { };
2358 int err;
2359
2360 do {
2361 err = nfs4_handle_exception(server,
2362 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2363 &exception);
2364 } while (exception.retry);
2365 return err;
2366}
2367
2368static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2369{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002370 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2372}
2373
2374static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2375 struct nfs_pathconf *pathconf)
2376{
2377 struct nfs4_pathconf_arg args = {
2378 .fh = fhandle,
2379 .bitmask = server->attr_bitmask,
2380 };
2381 struct rpc_message msg = {
2382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2383 .rpc_argp = &args,
2384 .rpc_resp = pathconf,
2385 };
2386
2387 /* None of the pathconf attributes are mandatory to implement */
2388 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2389 memset(pathconf, 0, sizeof(*pathconf));
2390 return 0;
2391 }
2392
Trond Myklebust0e574af2005-10-27 22:12:38 -04002393 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return rpc_call_sync(server->client, &msg, 0);
2395}
2396
2397static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2398 struct nfs_pathconf *pathconf)
2399{
2400 struct nfs4_exception exception = { };
2401 int err;
2402
2403 do {
2404 err = nfs4_handle_exception(server,
2405 _nfs4_proc_pathconf(server, fhandle, pathconf),
2406 &exception);
2407 } while (exception.retry);
2408 return err;
2409}
2410
Trond Myklebustec06c092006-03-20 13:44:27 -05002411static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Trond Myklebustec06c092006-03-20 13:44:27 -05002413 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebustec06c092006-03-20 13:44:27 -05002415 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002417 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002420 renew_lease(server, data->timestamp);
2421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Trond Myklebustec06c092006-03-20 13:44:27 -05002424static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2428 .rpc_argp = &data->args,
2429 .rpc_resp = &data->res,
2430 .rpc_cred = data->cred,
2431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 data->timestamp = jiffies;
2434
Trond Myklebustec06c092006-03-20 13:44:27 -05002435 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Trond Myklebust788e7a82006-03-20 13:44:27 -05002438static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct inode *inode = data->inode;
2441
2442 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2443 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002444 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002446 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002448 nfs_post_op_update_inode(inode, data->res.fattr);
2449 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Trond Myklebust788e7a82006-03-20 13:44:27 -05002453static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct rpc_message msg = {
2456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2457 .rpc_argp = &data->args,
2458 .rpc_resp = &data->res,
2459 .rpc_cred = data->cred,
2460 };
2461 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002462 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if (how & FLUSH_STABLE) {
2466 if (!NFS_I(inode)->ncommit)
2467 stable = NFS_FILE_SYNC;
2468 else
2469 stable = NFS_DATA_SYNC;
2470 } else
2471 stable = NFS_UNSTABLE;
2472 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002473 data->args.bitmask = server->attr_bitmask;
2474 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 data->timestamp = jiffies;
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002479 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Trond Myklebust788e7a82006-03-20 13:44:27 -05002482static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct inode *inode = data->inode;
2485
2486 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2487 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002488 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002490 if (task->tk_status >= 0)
2491 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
Trond Myklebust788e7a82006-03-20 13:44:27 -05002495static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct rpc_message msg = {
2498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2499 .rpc_argp = &data->args,
2500 .rpc_resp = &data->res,
2501 .rpc_cred = data->cred,
2502 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002503 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002505 data->args.bitmask = server->attr_bitmask;
2506 data->res.server = server;
2507
Trond Myklebust788e7a82006-03-20 13:44:27 -05002508 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511/*
2512 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2513 * standalone procedure for queueing an asynchronous RENEW.
2514 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002515static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
David Howellsadfa6f92006-08-22 20:06:08 -04002517 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002518 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 if (task->tk_status < 0) {
2521 switch (task->tk_status) {
2522 case -NFS4ERR_STALE_CLIENTID:
2523 case -NFS4ERR_EXPIRED:
2524 case -NFS4ERR_CB_PATH_DOWN:
2525 nfs4_schedule_state_recovery(clp);
2526 }
2527 return;
2528 }
2529 spin_lock(&clp->cl_lock);
2530 if (time_before(clp->cl_last_renewal,timestamp))
2531 clp->cl_last_renewal = timestamp;
2532 spin_unlock(&clp->cl_lock);
2533}
2534
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002535static const struct rpc_call_ops nfs4_renew_ops = {
2536 .rpc_call_done = nfs4_renew_done,
2537};
2538
David Howellsadfa6f92006-08-22 20:06:08 -04002539int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 struct rpc_message msg = {
2542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2543 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002544 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 };
2546
2547 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002548 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
David Howellsadfa6f92006-08-22 20:06:08 -04002551int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2555 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002556 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 };
2558 unsigned long now = jiffies;
2559 int status;
2560
2561 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2562 if (status < 0)
2563 return status;
2564 spin_lock(&clp->cl_lock);
2565 if (time_before(clp->cl_last_renewal,now))
2566 clp->cl_last_renewal = now;
2567 spin_unlock(&clp->cl_lock);
2568 return 0;
2569}
2570
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002571static inline int nfs4_server_supports_acls(struct nfs_server *server)
2572{
2573 return (server->caps & NFS_CAP_ACLS)
2574 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2575 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2576}
2577
2578/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2579 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2580 * the stack.
2581 */
2582#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2583
2584static void buf_to_pages(const void *buf, size_t buflen,
2585 struct page **pages, unsigned int *pgbase)
2586{
2587 const void *p = buf;
2588
2589 *pgbase = offset_in_page(buf);
2590 p -= *pgbase;
2591 while (p < buf + buflen) {
2592 *(pages++) = virt_to_page(p);
2593 p += PAGE_CACHE_SIZE;
2594 }
2595}
2596
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002597struct nfs4_cached_acl {
2598 int cached;
2599 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002600 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002601};
2602
2603static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002604{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002605 struct nfs_inode *nfsi = NFS_I(inode);
2606
2607 spin_lock(&inode->i_lock);
2608 kfree(nfsi->nfs4_acl);
2609 nfsi->nfs4_acl = acl;
2610 spin_unlock(&inode->i_lock);
2611}
2612
2613static void nfs4_zap_acl_attr(struct inode *inode)
2614{
2615 nfs4_set_cached_acl(inode, NULL);
2616}
2617
2618static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2619{
2620 struct nfs_inode *nfsi = NFS_I(inode);
2621 struct nfs4_cached_acl *acl;
2622 int ret = -ENOENT;
2623
2624 spin_lock(&inode->i_lock);
2625 acl = nfsi->nfs4_acl;
2626 if (acl == NULL)
2627 goto out;
2628 if (buf == NULL) /* user is just asking for length */
2629 goto out_len;
2630 if (acl->cached == 0)
2631 goto out;
2632 ret = -ERANGE; /* see getxattr(2) man page */
2633 if (acl->len > buflen)
2634 goto out;
2635 memcpy(buf, acl->data, acl->len);
2636out_len:
2637 ret = acl->len;
2638out:
2639 spin_unlock(&inode->i_lock);
2640 return ret;
2641}
2642
2643static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2644{
2645 struct nfs4_cached_acl *acl;
2646
2647 if (buf && acl_len <= PAGE_SIZE) {
2648 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2649 if (acl == NULL)
2650 goto out;
2651 acl->cached = 1;
2652 memcpy(acl->data, buf, acl_len);
2653 } else {
2654 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2655 if (acl == NULL)
2656 goto out;
2657 acl->cached = 0;
2658 }
2659 acl->len = acl_len;
2660out:
2661 nfs4_set_cached_acl(inode, acl);
2662}
2663
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002664static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002665{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002666 struct page *pages[NFS4ACL_MAXPAGES];
2667 struct nfs_getaclargs args = {
2668 .fh = NFS_FH(inode),
2669 .acl_pages = pages,
2670 .acl_len = buflen,
2671 };
2672 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002673 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002674 struct rpc_message msg = {
2675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2676 .rpc_argp = &args,
2677 .rpc_resp = &resp_len,
2678 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002679 struct page *localpage = NULL;
2680 int ret;
2681
2682 if (buflen < PAGE_SIZE) {
2683 /* As long as we're doing a round trip to the server anyway,
2684 * let's be prepared for a page of acl data. */
2685 localpage = alloc_page(GFP_KERNEL);
2686 resp_buf = page_address(localpage);
2687 if (localpage == NULL)
2688 return -ENOMEM;
2689 args.acl_pages[0] = localpage;
2690 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002691 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002692 } else {
2693 resp_buf = buf;
2694 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2695 }
2696 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2697 if (ret)
2698 goto out_free;
2699 if (resp_len > args.acl_len)
2700 nfs4_write_cached_acl(inode, NULL, resp_len);
2701 else
2702 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2703 if (buf) {
2704 ret = -ERANGE;
2705 if (resp_len > buflen)
2706 goto out_free;
2707 if (localpage)
2708 memcpy(buf, resp_buf, resp_len);
2709 }
2710 ret = resp_len;
2711out_free:
2712 if (localpage)
2713 __free_page(localpage);
2714 return ret;
2715}
2716
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002717static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2718{
2719 struct nfs4_exception exception = { };
2720 ssize_t ret;
2721 do {
2722 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2723 if (ret >= 0)
2724 break;
2725 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2726 } while (exception.retry);
2727 return ret;
2728}
2729
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002730static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2731{
2732 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002733 int ret;
2734
2735 if (!nfs4_server_supports_acls(server))
2736 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002737 ret = nfs_revalidate_inode(server, inode);
2738 if (ret < 0)
2739 return ret;
2740 ret = nfs4_read_cached_acl(inode, buf, buflen);
2741 if (ret != -ENOENT)
2742 return ret;
2743 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002744}
2745
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002746static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002747{
2748 struct nfs_server *server = NFS_SERVER(inode);
2749 struct page *pages[NFS4ACL_MAXPAGES];
2750 struct nfs_setaclargs arg = {
2751 .fh = NFS_FH(inode),
2752 .acl_pages = pages,
2753 .acl_len = buflen,
2754 };
2755 struct rpc_message msg = {
2756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2757 .rpc_argp = &arg,
2758 .rpc_resp = NULL,
2759 };
2760 int ret;
2761
2762 if (!nfs4_server_supports_acls(server))
2763 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002764 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002765 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002766 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002767 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002768 return ret;
2769}
2770
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002771static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2772{
2773 struct nfs4_exception exception = { };
2774 int err;
2775 do {
2776 err = nfs4_handle_exception(NFS_SERVER(inode),
2777 __nfs4_proc_set_acl(inode, buf, buflen),
2778 &exception);
2779 } while (exception.retry);
2780 return err;
2781}
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002784nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
David Howells7539bba2006-08-22 20:06:09 -04002786 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 if (!clp || task->tk_status >= 0)
2789 return 0;
2790 switch(task->tk_status) {
2791 case -NFS4ERR_STALE_CLIENTID:
2792 case -NFS4ERR_STALE_STATEID:
2793 case -NFS4ERR_EXPIRED:
2794 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2795 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002796 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 rpc_wake_up_task(task);
2798 task->tk_status = 0;
2799 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002801 nfs_inc_server_stats((struct nfs_server *) server,
2802 NFSIOS_DELAY);
2803 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2805 task->tk_status = 0;
2806 return -EAGAIN;
2807 case -NFS4ERR_OLD_STATEID:
2808 task->tk_status = 0;
2809 return -EAGAIN;
2810 }
2811 task->tk_status = nfs4_map_errors(task->tk_status);
2812 return 0;
2813}
2814
Trond Myklebust433fbe42006-01-03 09:55:22 +01002815static int nfs4_wait_bit_interruptible(void *word)
2816{
2817 if (signal_pending(current))
2818 return -ERESTARTSYS;
2819 schedule();
2820 return 0;
2821}
2822
David Howellsadfa6f92006-08-22 20:06:08 -04002823static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002826 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 might_sleep();
2829
Trond Myklebuste1485822006-12-13 16:43:13 -05002830 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002833 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2834 nfs4_wait_bit_interruptible,
2835 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002837
2838 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return res;
2840}
2841
2842static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2843{
2844 sigset_t oldset;
2845 int res = 0;
2846
2847 might_sleep();
2848
2849 if (*timeout <= 0)
2850 *timeout = NFS4_POLL_RETRY_MIN;
2851 if (*timeout > NFS4_POLL_RETRY_MAX)
2852 *timeout = NFS4_POLL_RETRY_MAX;
2853 rpc_clnt_sigmask(clnt, &oldset);
2854 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002855 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (signalled())
2857 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002858 } else
2859 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 rpc_clnt_sigunmask(clnt, &oldset);
2861 *timeout <<= 1;
2862 return res;
2863}
2864
2865/* This is the error handling routine for processes that are allowed
2866 * to sleep.
2867 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002868static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
David Howells7539bba2006-08-22 20:06:09 -04002870 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 int ret = errorcode;
2872
2873 exception->retry = 0;
2874 switch(errorcode) {
2875 case 0:
2876 return 0;
2877 case -NFS4ERR_STALE_CLIENTID:
2878 case -NFS4ERR_STALE_STATEID:
2879 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002880 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 ret = nfs4_wait_clnt_recover(server->client, clp);
2882 if (ret == 0)
2883 exception->retry = 1;
2884 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002885 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 case -NFS4ERR_GRACE:
2887 case -NFS4ERR_DELAY:
2888 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002889 if (ret != 0)
2890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002892 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894 /* We failed to handle the error */
2895 return nfs4_map_errors(ret);
2896}
2897
David Howellsadfa6f92006-08-22 20:06:08 -04002898int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 nfs4_verifier sc_verifier;
2901 struct nfs4_setclientid setclientid = {
2902 .sc_verifier = &sc_verifier,
2903 .sc_prog = program,
2904 };
2905 struct rpc_message msg = {
2906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2907 .rpc_argp = &setclientid,
2908 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002909 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 };
Al Virobc4785c2006-10-19 23:28:51 -07002911 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 int loop = 0;
2913 int status;
2914
Al Virobc4785c2006-10-19 23:28:51 -07002915 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2917 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2918
2919 for(;;) {
2920 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2921 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002922 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002923 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 clp->cl_id_uniquifier);
2925 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2926 sizeof(setclientid.sc_netid), "tcp");
2927 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2928 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2929 clp->cl_ipaddr, port >> 8, port & 255);
2930
2931 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2932 if (status != -NFS4ERR_CLID_INUSE)
2933 break;
2934 if (signalled())
2935 break;
2936 if (loop++ & 1)
2937 ssleep(clp->cl_lease_time + 1);
2938 else
2939 if (++clp->cl_id_uniquifier == 0)
2940 break;
2941 }
2942 return status;
2943}
2944
David Howellsadfa6f92006-08-22 20:06:08 -04002945static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct nfs_fsinfo fsinfo;
2948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2950 .rpc_argp = clp,
2951 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002952 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 };
2954 unsigned long now;
2955 int status;
2956
2957 now = jiffies;
2958 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2959 if (status == 0) {
2960 spin_lock(&clp->cl_lock);
2961 clp->cl_lease_time = fsinfo.lease_time * HZ;
2962 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002963 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 spin_unlock(&clp->cl_lock);
2965 }
2966 return status;
2967}
2968
David Howellsadfa6f92006-08-22 20:06:08 -04002969int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002970{
2971 long timeout;
2972 int err;
2973 do {
2974 err = _nfs4_proc_setclientid_confirm(clp, cred);
2975 switch (err) {
2976 case 0:
2977 return err;
2978 case -NFS4ERR_RESOURCE:
2979 /* The IBM lawyers misread another document! */
2980 case -NFS4ERR_DELAY:
2981 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2982 }
2983 } while (err == 0);
2984 return err;
2985}
2986
Trond Myklebustfe650402006-01-03 09:55:18 +01002987struct nfs4_delegreturndata {
2988 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002989 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002990 struct nfs_fh fh;
2991 nfs4_stateid stateid;
2992 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002993 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002994 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002995 int rpc_status;
2996};
2997
2998static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Trond Myklebustfe650402006-01-03 09:55:18 +01003000 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct rpc_message msg = {
3002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01003003 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01003004 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01003005 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01003007 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01003008 rpc_call_setup(task, &msg, 0);
3009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Trond Myklebustfe650402006-01-03 09:55:18 +01003011static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3012{
3013 struct nfs4_delegreturndata *data = calldata;
3014 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003015 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003016 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003017}
3018
3019static void nfs4_delegreturn_release(void *calldata)
3020{
3021 struct nfs4_delegreturndata *data = calldata;
3022
3023 put_rpccred(data->cred);
3024 kfree(calldata);
3025}
3026
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003027static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003028 .rpc_call_prepare = nfs4_delegreturn_prepare,
3029 .rpc_call_done = nfs4_delegreturn_done,
3030 .rpc_release = nfs4_delegreturn_release,
3031};
3032
3033static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3034{
3035 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003036 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003037 struct rpc_task *task;
3038 int status;
3039
3040 data = kmalloc(sizeof(*data), GFP_KERNEL);
3041 if (data == NULL)
3042 return -ENOMEM;
3043 data->args.fhandle = &data->fh;
3044 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003045 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003046 nfs_copy_fh(&data->fh, NFS_FH(inode));
3047 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003048 data->res.fattr = &data->fattr;
3049 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01003050 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003051 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003052 data->rpc_status = 0;
3053
3054 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003055 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003056 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003057 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01003058 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01003059 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003060 if (status == 0)
3061 nfs_post_op_update_inode(inode, &data->fattr);
3062 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003063 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003064 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
3067int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3068{
3069 struct nfs_server *server = NFS_SERVER(inode);
3070 struct nfs4_exception exception = { };
3071 int err;
3072 do {
3073 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3074 switch (err) {
3075 case -NFS4ERR_STALE_STATEID:
3076 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 case 0:
3078 return 0;
3079 }
3080 err = nfs4_handle_exception(server, err, &exception);
3081 } while (exception.retry);
3082 return err;
3083}
3084
3085#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3086#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3087
3088/*
3089 * sleep, with exponential backoff, and retry the LOCK operation.
3090 */
3091static unsigned long
3092nfs4_set_lock_task_retry(unsigned long timeout)
3093{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003094 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 timeout <<= 1;
3096 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3097 return NFS4_LOCK_MAXTIMEOUT;
3098 return timeout;
3099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3102{
3103 struct inode *inode = state->inode;
3104 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003105 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003106 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003108 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003110 struct nfs_lockt_res res = {
3111 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 };
3113 struct rpc_message msg = {
3114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3115 .rpc_argp = &arg,
3116 .rpc_resp = &res,
3117 .rpc_cred = state->owner->so_cred,
3118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct nfs4_lock_state *lsp;
3120 int status;
3121
3122 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003123 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003124 status = nfs4_set_lock_state(state, request);
3125 if (status != 0)
3126 goto out;
3127 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003128 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003130 switch (status) {
3131 case 0:
3132 request->fl_type = F_UNLCK;
3133 break;
3134 case -NFS4ERR_DENIED:
3135 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003137 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003138out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 up_read(&clp->cl_sem);
3140 return status;
3141}
3142
3143static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3144{
3145 struct nfs4_exception exception = { };
3146 int err;
3147
3148 do {
3149 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3150 _nfs4_proc_getlk(state, cmd, request),
3151 &exception);
3152 } while (exception.retry);
3153 return err;
3154}
3155
3156static int do_vfs_lock(struct file *file, struct file_lock *fl)
3157{
3158 int res = 0;
3159 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3160 case FL_POSIX:
3161 res = posix_lock_file_wait(file, fl);
3162 break;
3163 case FL_FLOCK:
3164 res = flock_lock_file_wait(file, fl);
3165 break;
3166 default:
3167 BUG();
3168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return res;
3170}
3171
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003172struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003173 struct nfs_locku_args arg;
3174 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003175 struct nfs4_lock_state *lsp;
3176 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003177 struct file_lock fl;
3178 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003179 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003180};
3181
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003182static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3183 struct nfs_open_context *ctx,
3184 struct nfs4_lock_state *lsp,
3185 struct nfs_seqid *seqid)
3186{
3187 struct nfs4_unlockdata *p;
3188 struct inode *inode = lsp->ls_state->inode;
3189
3190 p = kmalloc(sizeof(*p), GFP_KERNEL);
3191 if (p == NULL)
3192 return NULL;
3193 p->arg.fh = NFS_FH(inode);
3194 p->arg.fl = &p->fl;
3195 p->arg.seqid = seqid;
3196 p->arg.stateid = &lsp->ls_stateid;
3197 p->lsp = lsp;
3198 atomic_inc(&lsp->ls_count);
3199 /* Ensure we don't close file until we're done freeing locks! */
3200 p->ctx = get_nfs_open_context(ctx);
3201 memcpy(&p->fl, fl, sizeof(p->fl));
3202 p->server = NFS_SERVER(inode);
3203 return p;
3204}
3205
Trond Myklebust06f814a2006-01-03 09:55:07 +01003206static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003207{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003208 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003209 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003210 nfs4_put_lock_state(calldata->lsp);
3211 put_nfs_open_context(calldata->ctx);
3212 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003213}
3214
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003215static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003216{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003217 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003218
Trond Myklebust06f814a2006-01-03 09:55:07 +01003219 if (RPC_ASSASSINATED(task))
3220 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003221 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003222 switch (task->tk_status) {
3223 case 0:
3224 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003225 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003226 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003227 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003228 break;
3229 case -NFS4ERR_STALE_STATEID:
3230 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003231 break;
3232 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003233 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003234 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003235 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003236}
3237
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003238static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003239{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003240 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 struct rpc_message msg = {
3242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003243 .rpc_argp = &calldata->arg,
3244 .rpc_resp = &calldata->res,
3245 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003248 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003249 return;
3250 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003251 /* Note: exit _without_ running nfs4_locku_done */
3252 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003253 return;
3254 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003255 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003256 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003259static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003260 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003261 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003262 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003263};
3264
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003265static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3266 struct nfs_open_context *ctx,
3267 struct nfs4_lock_state *lsp,
3268 struct nfs_seqid *seqid)
3269{
3270 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003271
Frank Filz137d6ac2007-07-09 15:32:29 -07003272 /* Ensure this is an unlock - when canceling a lock, the
3273 * canceled lock is passed in, and it won't be an unlock.
3274 */
3275 fl->fl_type = F_UNLCK;
3276
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003277 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3278 if (data == NULL) {
3279 nfs_free_seqid(seqid);
3280 return ERR_PTR(-ENOMEM);
3281 }
3282
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003283 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003284}
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3287{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003288 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003289 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003290 struct rpc_task *task;
3291 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Trond Myklebust9b073572006-06-29 16:38:34 -04003293 status = nfs4_set_lock_state(state, request);
3294 /* Unlock _before_ we do the RPC call */
3295 request->fl_flags |= FL_EXISTS;
3296 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3297 goto out;
3298 if (status != 0)
3299 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003300 /* Is this a delegated lock? */
3301 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003302 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003303 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003304 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003305 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003306 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003307 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003308 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3309 status = PTR_ERR(task);
3310 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003311 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003312 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003313 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003314out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003318struct nfs4_lockdata {
3319 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003320 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003321 struct nfs4_lock_state *lsp;
3322 struct nfs_open_context *ctx;
3323 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003324 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003325 int rpc_status;
3326 int cancelled;
3327};
3328
3329static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3330 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3331{
3332 struct nfs4_lockdata *p;
3333 struct inode *inode = lsp->ls_state->inode;
3334 struct nfs_server *server = NFS_SERVER(inode);
3335
3336 p = kzalloc(sizeof(*p), GFP_KERNEL);
3337 if (p == NULL)
3338 return NULL;
3339
3340 p->arg.fh = NFS_FH(inode);
3341 p->arg.fl = &p->fl;
3342 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3343 if (p->arg.lock_seqid == NULL)
3344 goto out_free;
3345 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003346 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003347 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003348 p->lsp = lsp;
3349 atomic_inc(&lsp->ls_count);
3350 p->ctx = get_nfs_open_context(ctx);
3351 memcpy(&p->fl, fl, sizeof(p->fl));
3352 return p;
3353out_free:
3354 kfree(p);
3355 return NULL;
3356}
3357
3358static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3359{
3360 struct nfs4_lockdata *data = calldata;
3361 struct nfs4_state *state = data->lsp->ls_state;
3362 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3365 .rpc_argp = &data->arg,
3366 .rpc_resp = &data->res,
3367 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003370 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3371 return;
3372 dprintk("%s: begin!\n", __FUNCTION__);
3373 /* Do we need to do an open_to_lock_owner? */
3374 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3375 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3376 if (data->arg.open_seqid == NULL) {
3377 data->rpc_status = -ENOMEM;
3378 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003379 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003380 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003381 data->arg.open_stateid = &state->stateid;
3382 data->arg.new_lock_owner = 1;
3383 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003384 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003385 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003386out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003387 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3388}
3389
3390static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3391{
3392 struct nfs4_lockdata *data = calldata;
3393
3394 dprintk("%s: begin!\n", __FUNCTION__);
3395
3396 data->rpc_status = task->tk_status;
3397 if (RPC_ASSASSINATED(task))
3398 goto out;
3399 if (data->arg.new_lock_owner != 0) {
3400 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3401 if (data->rpc_status == 0)
3402 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3403 else
3404 goto out;
3405 }
3406 if (data->rpc_status == 0) {
3407 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3408 sizeof(data->lsp->ls_stateid.data));
3409 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003410 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003411 }
3412 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3413out:
3414 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3415}
3416
3417static void nfs4_lock_release(void *calldata)
3418{
3419 struct nfs4_lockdata *data = calldata;
3420
3421 dprintk("%s: begin!\n", __FUNCTION__);
3422 if (data->arg.open_seqid != NULL)
3423 nfs_free_seqid(data->arg.open_seqid);
3424 if (data->cancelled != 0) {
3425 struct rpc_task *task;
3426 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3427 data->arg.lock_seqid);
3428 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003429 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003430 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3431 } else
3432 nfs_free_seqid(data->arg.lock_seqid);
3433 nfs4_put_lock_state(data->lsp);
3434 put_nfs_open_context(data->ctx);
3435 kfree(data);
3436 dprintk("%s: done!\n", __FUNCTION__);
3437}
3438
3439static const struct rpc_call_ops nfs4_lock_ops = {
3440 .rpc_call_prepare = nfs4_lock_prepare,
3441 .rpc_call_done = nfs4_lock_done,
3442 .rpc_release = nfs4_lock_release,
3443};
3444
3445static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3446{
3447 struct nfs4_lockdata *data;
3448 struct rpc_task *task;
3449 int ret;
3450
3451 dprintk("%s: begin!\n", __FUNCTION__);
3452 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3453 fl->fl_u.nfs4_fl.owner);
3454 if (data == NULL)
3455 return -ENOMEM;
3456 if (IS_SETLKW(cmd))
3457 data->arg.block = 1;
3458 if (reclaim != 0)
3459 data->arg.reclaim = 1;
3460 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3461 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003462 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003463 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003464 ret = nfs4_wait_for_completion_rpc_task(task);
3465 if (ret == 0) {
3466 ret = data->rpc_status;
3467 if (ret == -NFS4ERR_DENIED)
3468 ret = -EAGAIN;
3469 } else
3470 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003471 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003472 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
3476static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3477{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003478 struct nfs_server *server = NFS_SERVER(state->inode);
3479 struct nfs4_exception exception = { };
3480 int err;
3481
3482 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003483 /* Cache the lock if possible... */
3484 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3485 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003486 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3487 if (err != -NFS4ERR_DELAY)
3488 break;
3489 nfs4_handle_exception(server, err, &exception);
3490 } while (exception.retry);
3491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
3494static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3495{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003496 struct nfs_server *server = NFS_SERVER(state->inode);
3497 struct nfs4_exception exception = { };
3498 int err;
3499
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003500 err = nfs4_set_lock_state(state, request);
3501 if (err != 0)
3502 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003503 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003504 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3505 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003506 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3507 if (err != -NFS4ERR_DELAY)
3508 break;
3509 nfs4_handle_exception(server, err, &exception);
3510 } while (exception.retry);
3511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
3514static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3515{
David Howellsadfa6f92006-08-22 20:06:08 -04003516 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003517 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 int status;
3519
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003520 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003521 status = nfs4_set_lock_state(state, request);
3522 if (status != 0)
3523 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003524 request->fl_flags |= FL_ACCESS;
3525 status = do_vfs_lock(request->fl_file, request);
3526 if (status < 0)
3527 goto out;
3528 down_read(&clp->cl_sem);
3529 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3530 struct nfs_inode *nfsi = NFS_I(state->inode);
3531 /* Yes: cache locks! */
3532 down_read(&nfsi->rwsem);
3533 /* ...but avoid races with delegation recall... */
3534 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3535 request->fl_flags = fl_flags & ~FL_SLEEP;
3536 status = do_vfs_lock(request->fl_file, request);
3537 up_read(&nfsi->rwsem);
3538 goto out_unlock;
3539 }
3540 up_read(&nfsi->rwsem);
3541 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003542 status = _nfs4_do_setlk(state, cmd, request, 0);
3543 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003544 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003545 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003546 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003547 if (do_vfs_lock(request->fl_file, request) < 0)
3548 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003549out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003551out:
3552 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return status;
3554}
3555
3556static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3557{
3558 struct nfs4_exception exception = { };
3559 int err;
3560
3561 do {
3562 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3563 _nfs4_proc_setlk(state, cmd, request),
3564 &exception);
3565 } while (exception.retry);
3566 return err;
3567}
3568
3569static int
3570nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3571{
3572 struct nfs_open_context *ctx;
3573 struct nfs4_state *state;
3574 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3575 int status;
3576
3577 /* verify open state */
3578 ctx = (struct nfs_open_context *)filp->private_data;
3579 state = ctx->state;
3580
3581 if (request->fl_start < 0 || request->fl_end < 0)
3582 return -EINVAL;
3583
3584 if (IS_GETLK(cmd))
3585 return nfs4_proc_getlk(state, F_GETLK, request);
3586
3587 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3588 return -EINVAL;
3589
3590 if (request->fl_type == F_UNLCK)
3591 return nfs4_proc_unlck(state, cmd, request);
3592
3593 do {
3594 status = nfs4_proc_setlk(state, cmd, request);
3595 if ((status != -EAGAIN) || IS_SETLK(cmd))
3596 break;
3597 timeout = nfs4_set_lock_task_retry(timeout);
3598 status = -ERESTARTSYS;
3599 if (signalled())
3600 break;
3601 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return status;
3603}
3604
Trond Myklebust888e6942005-11-04 15:38:11 -05003605int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3606{
3607 struct nfs_server *server = NFS_SERVER(state->inode);
3608 struct nfs4_exception exception = { };
3609 int err;
3610
3611 err = nfs4_set_lock_state(state, fl);
3612 if (err != 0)
3613 goto out;
3614 do {
3615 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3616 if (err != -NFS4ERR_DELAY)
3617 break;
3618 err = nfs4_handle_exception(server, err, &exception);
3619 } while (exception.retry);
3620out:
3621 return err;
3622}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003623
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003624#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3625
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003626int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3627 size_t buflen, int flags)
3628{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003629 struct inode *inode = dentry->d_inode;
3630
3631 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3632 return -EOPNOTSUPP;
3633
3634 if (!S_ISREG(inode->i_mode) &&
3635 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3636 return -EPERM;
3637
3638 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003639}
3640
3641/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3642 * and that's what we'll do for e.g. user attributes that haven't been set.
3643 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3644 * attributes in kernel-managed attribute namespaces. */
3645ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3646 size_t buflen)
3647{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003648 struct inode *inode = dentry->d_inode;
3649
3650 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3651 return -EOPNOTSUPP;
3652
3653 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003654}
3655
3656ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3657{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003659
J. Bruce Fields096455a2006-03-20 23:23:42 -05003660 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3661 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003662 if (buf && buflen < len)
3663 return -ERANGE;
3664 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003665 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3666 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003667}
3668
Trond Myklebust56659e92007-07-17 21:52:39 -04003669int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003670 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003671{
3672 struct nfs_server *server = NFS_SERVER(dir);
3673 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003674 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3675 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003676 };
3677 struct nfs4_fs_locations_arg args = {
3678 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003679 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003680 .page = page,
3681 .bitmask = bitmask,
3682 };
3683 struct rpc_message msg = {
3684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3685 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003686 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003687 };
3688 int status;
3689
3690 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003691 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003692 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003693 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003694 status = rpc_call_sync(server->client, &msg, 0);
3695 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3696 return status;
3697}
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3700 .recover_open = nfs4_open_reclaim,
3701 .recover_lock = nfs4_lock_reclaim,
3702};
3703
3704struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3705 .recover_open = nfs4_open_expired,
3706 .recover_lock = nfs4_lock_expired,
3707};
3708
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003709static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003710 .permission = nfs_permission,
3711 .getattr = nfs_getattr,
3712 .setattr = nfs_setattr,
3713 .getxattr = nfs4_getxattr,
3714 .setxattr = nfs4_setxattr,
3715 .listxattr = nfs4_listxattr,
3716};
3717
David Howells509de812006-08-22 20:06:11 -04003718const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 .version = 4, /* protocol version */
3720 .dentry_ops = &nfs4_dentry_operations,
3721 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003722 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 .getroot = nfs4_proc_get_root,
3724 .getattr = nfs4_proc_getattr,
3725 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003726 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 .lookup = nfs4_proc_lookup,
3728 .access = nfs4_proc_access,
3729 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 .create = nfs4_proc_create,
3731 .remove = nfs4_proc_remove,
3732 .unlink_setup = nfs4_proc_unlink_setup,
3733 .unlink_done = nfs4_proc_unlink_done,
3734 .rename = nfs4_proc_rename,
3735 .link = nfs4_proc_link,
3736 .symlink = nfs4_proc_symlink,
3737 .mkdir = nfs4_proc_mkdir,
3738 .rmdir = nfs4_proc_remove,
3739 .readdir = nfs4_proc_readdir,
3740 .mknod = nfs4_proc_mknod,
3741 .statfs = nfs4_proc_statfs,
3742 .fsinfo = nfs4_proc_fsinfo,
3743 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003744 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 .decode_dirent = nfs4_decode_dirent,
3746 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003747 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003749 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003751 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003752 .file_open = nfs_open,
3753 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003755 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756};
3757
3758/*
3759 * Local variables:
3760 * c-basic-offset: 8
3761 * End:
3762 */