blob: 368b75b3bcba6070c340eefc9f18efc1a3d230ff [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"
54
55#define NFSDBG_FACILITY NFSDBG_PROC
56
57#define NFS4_POLL_RETRY_MIN (1*HZ)
58#define NFS4_POLL_RETRY_MAX (15*HZ)
59
Trond Myklebust0a8838f2005-10-18 14:20:14 -070060static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070062static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66extern struct rpc_procinfo nfs4_procedures[];
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Prevent leaks of NFSv4 errors into userland */
69int nfs4_map_errors(int err)
70{
71 if (err < -1000) {
72 dprintk("%s could not handle NFSv4 error %d\n",
73 __FUNCTION__, -err);
74 return -EIO;
75 }
76 return err;
77}
78
79/*
80 * This is our standard bitmap for GETATTR requests.
81 */
82const u32 nfs4_fattr_bitmap[2] = {
83 FATTR4_WORD0_TYPE
84 | FATTR4_WORD0_CHANGE
85 | FATTR4_WORD0_SIZE
86 | FATTR4_WORD0_FSID
87 | FATTR4_WORD0_FILEID,
88 FATTR4_WORD1_MODE
89 | FATTR4_WORD1_NUMLINKS
90 | FATTR4_WORD1_OWNER
91 | FATTR4_WORD1_OWNER_GROUP
92 | FATTR4_WORD1_RAWDEV
93 | FATTR4_WORD1_SPACE_USED
94 | FATTR4_WORD1_TIME_ACCESS
95 | FATTR4_WORD1_TIME_METADATA
96 | FATTR4_WORD1_TIME_MODIFY
97};
98
99const u32 nfs4_statfs_bitmap[2] = {
100 FATTR4_WORD0_FILES_AVAIL
101 | FATTR4_WORD0_FILES_FREE
102 | FATTR4_WORD0_FILES_TOTAL,
103 FATTR4_WORD1_SPACE_AVAIL
104 | FATTR4_WORD1_SPACE_FREE
105 | FATTR4_WORD1_SPACE_TOTAL
106};
107
Trond Myklebust4ce79712005-06-22 17:16:21 +0000108const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 FATTR4_WORD0_MAXLINK
110 | FATTR4_WORD0_MAXNAME,
111 0
112};
113
114const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
115 | FATTR4_WORD0_MAXREAD
116 | FATTR4_WORD0_MAXWRITE
117 | FATTR4_WORD0_LEASE_TIME,
118 0
119};
120
121static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
122 struct nfs4_readdir_arg *readdir)
123{
124 u32 *start, *p;
125
126 BUG_ON(readdir->count < 80);
127 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000128 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
130 return;
131 }
132
133 readdir->cookie = 0;
134 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
135 if (cookie == 2)
136 return;
137
138 /*
139 * NFSv4 servers do not return entries for '.' and '..'
140 * Therefore, we fake these entries here. We let '.'
141 * have cookie 0 and '..' have cookie 1. Note that
142 * when talking to the server, we always send cookie 0
143 * instead of 1 or 2.
144 */
145 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
146
147 if (cookie == 0) {
148 *p++ = xdr_one; /* next */
149 *p++ = xdr_zero; /* cookie, first word */
150 *p++ = xdr_one; /* cookie, second word */
151 *p++ = xdr_one; /* entry len */
152 memcpy(p, ".\0\0\0", 4); /* entry */
153 p++;
154 *p++ = xdr_one; /* bitmap length */
155 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
156 *p++ = htonl(8); /* attribute buffer length */
157 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
158 }
159
160 *p++ = xdr_one; /* next */
161 *p++ = xdr_zero; /* cookie, first word */
162 *p++ = xdr_two; /* cookie, second word */
163 *p++ = xdr_two; /* entry len */
164 memcpy(p, "..\0\0", 4); /* entry */
165 p++;
166 *p++ = xdr_one; /* bitmap length */
167 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
168 *p++ = htonl(8); /* attribute buffer length */
169 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
170
171 readdir->pgbase = (char *)p - (char *)start;
172 readdir->count -= readdir->pgbase;
173 kunmap_atomic(start, KM_USER0);
174}
175
176static void
177renew_lease(struct nfs_server *server, unsigned long timestamp)
178{
179 struct nfs4_client *clp = server->nfs4_state;
180 spin_lock(&clp->cl_lock);
181 if (time_before(clp->cl_last_renewal,timestamp))
182 clp->cl_last_renewal = timestamp;
183 spin_unlock(&clp->cl_lock);
184}
185
186static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
187{
188 struct nfs_inode *nfsi = NFS_I(inode);
189
Trond Myklebustdecf4912005-10-27 22:12:39 -0400190 spin_lock(&inode->i_lock);
191 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400194 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Trond Myklebust95121352005-10-18 14:20:12 -0700197/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100198static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100199 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700200{
201 struct rpc_task *task;
202
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100203 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700204 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700205 rpc_execute(task);
206 return 0;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
210{
211 struct inode *inode = state->inode;
212
213 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500214 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700215 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500217 memcpy(&state->stateid, stateid, sizeof(state->stateid));
218 if ((open_flags & FMODE_WRITE))
219 state->nwriters++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (open_flags & FMODE_READ)
221 state->nreaders++;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500222 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700224 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/*
228 * OPEN_RECLAIM:
229 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
232{
233 struct inode *inode = state->inode;
234 struct nfs_server *server = NFS_SERVER(inode);
235 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
236 struct nfs_openargs o_arg = {
237 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .id = sp->so_id,
239 .open_flags = state->state,
240 .clientid = server->nfs4_state->cl_clientid,
241 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
242 .bitmask = server->attr_bitmask,
243 };
244 struct nfs_openres o_res = {
245 .server = server, /* Grrr */
246 };
247 struct rpc_message msg = {
248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
249 .rpc_argp = &o_arg,
250 .rpc_resp = &o_res,
251 .rpc_cred = sp->so_cred,
252 };
253 int status;
254
255 if (delegation != NULL) {
256 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
257 memcpy(&state->stateid, &delegation->stateid,
258 sizeof(state->stateid));
259 set_bit(NFS_DELEGATED_STATE, &state->flags);
260 return 0;
261 }
262 o_arg.u.delegation_type = delegation->type;
263 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700264 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
265 if (o_arg.seqid == NULL)
266 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700268 /* Confirm the sequence as being established */
269 nfs_confirm_seqid(&sp->so_seqid, status);
270 nfs_increment_open_seqid(status, o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (status == 0) {
272 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
273 if (o_res.delegation_type != 0) {
274 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
275 /* Did the server issue an immediate delegation recall? */
276 if (o_res.do_recall)
277 nfs_async_inode_return_delegation(inode, &o_res.stateid);
278 }
279 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700280 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 clear_bit(NFS_DELEGATED_STATE, &state->flags);
282 /* Ensure we update the inode attributes */
283 NFS_CACHEINV(inode);
284 return status;
285}
286
287static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
288{
289 struct nfs_server *server = NFS_SERVER(state->inode);
290 struct nfs4_exception exception = { };
291 int err;
292 do {
293 err = _nfs4_open_reclaim(sp, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000294 if (err != -NFS4ERR_DELAY)
295 break;
296 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 } while (exception.retry);
298 return err;
299}
300
301static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
302{
303 struct nfs4_state_owner *sp = state->owner;
304 struct inode *inode = dentry->d_inode;
305 struct nfs_server *server = NFS_SERVER(inode);
306 struct dentry *parent = dget_parent(dentry);
307 struct nfs_openargs arg = {
308 .fh = NFS_FH(parent->d_inode),
309 .clientid = server->nfs4_state->cl_clientid,
310 .name = &dentry->d_name,
311 .id = sp->so_id,
312 .server = server,
313 .bitmask = server->attr_bitmask,
314 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
315 };
316 struct nfs_openres res = {
317 .server = server,
318 };
319 struct rpc_message msg = {
320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
321 .rpc_argp = &arg,
322 .rpc_resp = &res,
323 .rpc_cred = sp->so_cred,
324 };
325 int status = 0;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
328 goto out;
329 if (state->state == 0)
330 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700331 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
332 status = -ENOMEM;
333 if (arg.seqid == NULL)
334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 arg.open_flags = state->state;
336 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
337 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700338 nfs_increment_open_seqid(status, arg.seqid);
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700339 if (status != 0)
340 goto out_free;
341 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
342 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
343 sp, &res.stateid, arg.seqid);
344 if (status != 0)
345 goto out_free;
346 }
347 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (status >= 0) {
349 memcpy(state->stateid.data, res.stateid.data,
350 sizeof(state->stateid.data));
351 clear_bit(NFS_DELEGATED_STATE, &state->flags);
352 }
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700353out_free:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700354 nfs_free_seqid(arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dput(parent);
357 return status;
358}
359
360int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
361{
362 struct nfs4_exception exception = { };
363 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
364 int err;
365 do {
366 err = _nfs4_open_delegation_recall(dentry, state);
367 switch (err) {
368 case 0:
369 return err;
370 case -NFS4ERR_STALE_CLIENTID:
371 case -NFS4ERR_STALE_STATEID:
372 case -NFS4ERR_EXPIRED:
373 /* Don't recall a delegation if it was lost */
374 nfs4_schedule_state_recovery(server->nfs4_state);
375 return err;
376 }
377 err = nfs4_handle_exception(server, err, &exception);
378 } while (exception.retry);
379 return err;
380}
381
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700382static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct nfs_open_confirmargs arg = {
385 .fh = fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700386 .seqid = seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .stateid = *stateid,
388 };
389 struct nfs_open_confirmres res;
390 struct rpc_message msg = {
391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
392 .rpc_argp = &arg,
393 .rpc_resp = &res,
394 .rpc_cred = sp->so_cred,
395 };
396 int status;
397
398 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700399 /* Confirm the sequence as being established */
400 nfs_confirm_seqid(&sp->so_seqid, status);
401 nfs_increment_open_seqid(status, seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (status >= 0)
403 memcpy(stateid, &res.stateid, sizeof(*stateid));
404 return status;
405}
406
407static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
408{
409 struct nfs_server *server = NFS_SERVER(dir);
410 struct rpc_message msg = {
411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
412 .rpc_argp = o_arg,
413 .rpc_resp = o_res,
414 .rpc_cred = sp->so_cred,
415 };
416 int status;
417
418 /* Update sequence id. The caller must serialize! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 o_arg->id = sp->so_id;
420 o_arg->clientid = sp->so_client->cl_clientid;
421
422 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700423 if (status == 0) {
424 /* OPEN on anything except a regular file is disallowed in NFSv4 */
425 switch (o_res->f_attr->mode & S_IFMT) {
426 case S_IFREG:
427 break;
428 case S_IFLNK:
429 status = -ELOOP;
430 break;
431 case S_IFDIR:
432 status = -EISDIR;
433 break;
434 default:
435 status = -ENOTDIR;
436 }
437 }
438
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700439 nfs_increment_open_seqid(status, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (status != 0)
441 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400442 if (o_arg->open_flags & O_CREAT) {
443 update_changeattr(dir, &o_res->cinfo);
444 nfs_post_op_update_inode(dir, o_res->dir_attr);
445 } else
446 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
448 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700449 sp, &o_res->stateid, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (status != 0)
451 goto out;
452 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700453 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
455 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
456out:
457 return status;
458}
459
460static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
461{
462 struct nfs_access_entry cache;
463 int mask = 0;
464 int status;
465
466 if (openflags & FMODE_READ)
467 mask |= MAY_READ;
468 if (openflags & FMODE_WRITE)
469 mask |= MAY_WRITE;
470 status = nfs_access_get_cached(inode, cred, &cache);
471 if (status == 0)
472 goto out;
473
474 /* Be clever: ask server to check for all possible rights */
475 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
476 cache.cred = cred;
477 cache.jiffies = jiffies;
478 status = _nfs4_proc_access(inode, &cache);
479 if (status != 0)
480 return status;
481 nfs_access_add_cache(inode, &cache);
482out:
483 if ((cache.mask & mask) == mask)
484 return 0;
485 return -EACCES;
486}
487
488/*
489 * OPEN_EXPIRED:
490 * reclaim state on the server after a network partition.
491 * Assumes caller holds the appropriate lock
492 */
493static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
494{
495 struct dentry *parent = dget_parent(dentry);
496 struct inode *dir = parent->d_inode;
497 struct inode *inode = state->inode;
498 struct nfs_server *server = NFS_SERVER(dir);
499 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400500 struct nfs_fattr f_attr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct nfs_openargs o_arg = {
502 .fh = NFS_FH(dir),
503 .open_flags = state->state,
504 .name = &dentry->d_name,
505 .bitmask = server->attr_bitmask,
506 .claim = NFS4_OPEN_CLAIM_NULL,
507 };
508 struct nfs_openres o_res = {
509 .f_attr = &f_attr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400510 .dir_attr = &dir_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .server = server,
512 };
513 int status = 0;
514
515 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
516 status = _nfs4_do_access(inode, sp->so_cred, state->state);
517 if (status < 0)
518 goto out;
519 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
520 set_bit(NFS_DELEGATED_STATE, &state->flags);
521 goto out;
522 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700523 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
524 status = -ENOMEM;
525 if (o_arg.seqid == NULL)
526 goto out;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400527 nfs_fattr_init(&f_attr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400528 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
530 if (status != 0)
531 goto out_nodeleg;
532 /* Check if files differ */
533 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
534 goto out_stale;
535 /* Has the file handle changed? */
536 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
537 /* Verify if the change attributes are the same */
538 if (f_attr.change_attr != NFS_I(inode)->change_attr)
539 goto out_stale;
540 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
541 goto out_stale;
542 /* Lets just pretend that this is the same file */
543 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
544 NFS_I(inode)->fileid = f_attr.fileid;
545 }
546 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
547 if (o_res.delegation_type != 0) {
548 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
549 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
550 else
551 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
552 }
553out_nodeleg:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700554 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 clear_bit(NFS_DELEGATED_STATE, &state->flags);
556out:
557 dput(parent);
558 return status;
559out_stale:
560 status = -ESTALE;
561 /* Invalidate the state owner so we don't ever use it again */
562 nfs4_drop_state_owner(sp);
563 d_drop(dentry);
564 /* Should we be trying to close that stateid? */
565 goto out_nodeleg;
566}
567
Trond Myklebust202b50d2005-06-22 17:16:29 +0000568static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
569{
570 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
571 struct nfs4_exception exception = { };
572 int err;
573
574 do {
575 err = _nfs4_open_expired(sp, state, dentry);
576 if (err == -NFS4ERR_DELAY)
577 nfs4_handle_exception(server, err, &exception);
578 } while (exception.retry);
579 return err;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
583{
584 struct nfs_inode *nfsi = NFS_I(state->inode);
585 struct nfs_open_context *ctx;
586 int status;
587
588 spin_lock(&state->inode->i_lock);
589 list_for_each_entry(ctx, &nfsi->open_files, list) {
590 if (ctx->state != state)
591 continue;
592 get_nfs_open_context(ctx);
593 spin_unlock(&state->inode->i_lock);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000594 status = nfs4_do_open_expired(sp, state, ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 put_nfs_open_context(ctx);
596 return status;
597 }
598 spin_unlock(&state->inode->i_lock);
599 return -ENOENT;
600}
601
602/*
603 * Returns an nfs4_state + an extra reference to the inode
604 */
605static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
606{
607 struct nfs_delegation *delegation;
608 struct nfs_server *server = NFS_SERVER(inode);
609 struct nfs4_client *clp = server->nfs4_state;
610 struct nfs_inode *nfsi = NFS_I(inode);
611 struct nfs4_state_owner *sp = NULL;
612 struct nfs4_state *state = NULL;
613 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
614 int err;
615
616 /* Protect against reboot recovery - NOTE ORDER! */
617 down_read(&clp->cl_sem);
618 /* Protect against delegation recall */
619 down_read(&nfsi->rwsem);
620 delegation = NFS_I(inode)->delegation;
621 err = -ENOENT;
622 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
623 goto out_err;
624 err = -ENOMEM;
625 if (!(sp = nfs4_get_state_owner(server, cred))) {
626 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
627 goto out_err;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 state = nfs4_get_open_state(inode, sp);
630 if (state == NULL)
631 goto out_err;
632
633 err = -ENOENT;
634 if ((state->state & open_flags) == open_flags) {
635 spin_lock(&inode->i_lock);
636 if (open_flags & FMODE_READ)
637 state->nreaders++;
638 if (open_flags & FMODE_WRITE)
639 state->nwriters++;
640 spin_unlock(&inode->i_lock);
641 goto out_ok;
642 } else if (state->state != 0)
643 goto out_err;
644
645 lock_kernel();
646 err = _nfs4_do_access(inode, cred, open_flags);
647 unlock_kernel();
648 if (err != 0)
649 goto out_err;
650 set_bit(NFS_DELEGATED_STATE, &state->flags);
651 update_open_stateid(state, &delegation->stateid, open_flags);
652out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 nfs4_put_state_owner(sp);
654 up_read(&nfsi->rwsem);
655 up_read(&clp->cl_sem);
656 igrab(inode);
657 *res = state;
658 return 0;
659out_err:
660 if (sp != NULL) {
661 if (state != NULL)
662 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 nfs4_put_state_owner(sp);
664 }
665 up_read(&nfsi->rwsem);
666 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700667 if (err != -EACCES)
668 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return err;
670}
671
672static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
673{
674 struct nfs4_exception exception = { };
675 struct nfs4_state *res;
676 int err;
677
678 do {
679 err = _nfs4_open_delegated(inode, flags, cred, &res);
680 if (err == 0)
681 break;
682 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
683 err, &exception));
684 } while (exception.retry);
685 return res;
686}
687
688/*
689 * Returns an nfs4_state + an referenced inode
690 */
691static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
692{
693 struct nfs4_state_owner *sp;
694 struct nfs4_state *state = NULL;
695 struct nfs_server *server = NFS_SERVER(dir);
696 struct nfs4_client *clp = server->nfs4_state;
697 struct inode *inode = NULL;
698 int status;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400699 struct nfs_fattr f_attr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct nfs_openargs o_arg = {
701 .fh = NFS_FH(dir),
702 .open_flags = flags,
703 .name = &dentry->d_name,
704 .server = server,
705 .bitmask = server->attr_bitmask,
706 .claim = NFS4_OPEN_CLAIM_NULL,
707 };
708 struct nfs_openres o_res = {
709 .f_attr = &f_attr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400710 .dir_attr = &dir_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .server = server,
712 };
713
714 /* Protect against reboot recovery conflicts */
715 down_read(&clp->cl_sem);
716 status = -ENOMEM;
717 if (!(sp = nfs4_get_state_owner(server, cred))) {
718 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
719 goto out_err;
720 }
721 if (flags & O_EXCL) {
722 u32 *p = (u32 *) o_arg.u.verifier.data;
723 p[0] = jiffies;
724 p[1] = current->pid;
725 } else
726 o_arg.u.attrs = sattr;
727 /* Serialization for the sequence id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700729 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
730 if (o_arg.seqid == NULL)
731 return -ENOMEM;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400732 nfs_fattr_init(&f_attr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400733 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
735 if (status != 0)
736 goto out_err;
737
738 status = -ENOMEM;
739 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
740 if (!inode)
741 goto out_err;
742 state = nfs4_get_open_state(inode, sp);
743 if (!state)
744 goto out_err;
745 update_open_stateid(state, &o_res.stateid, flags);
746 if (o_res.delegation_type != 0)
747 nfs_inode_set_delegation(inode, cred, &o_res);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700748 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 nfs4_put_state_owner(sp);
750 up_read(&clp->cl_sem);
751 *res = state;
752 return 0;
753out_err:
754 if (sp != NULL) {
755 if (state != NULL)
756 nfs4_put_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700757 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 nfs4_put_state_owner(sp);
759 }
760 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
761 up_read(&clp->cl_sem);
762 if (inode != NULL)
763 iput(inode);
764 *res = NULL;
765 return status;
766}
767
768
769static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
770{
771 struct nfs4_exception exception = { };
772 struct nfs4_state *res;
773 int status;
774
775 do {
776 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
777 if (status == 0)
778 break;
779 /* NOTE: BAD_SEQID means the server and client disagree about the
780 * book-keeping w.r.t. state-changing operations
781 * (OPEN/CLOSE/LOCK/LOCKU...)
782 * It is actually a sign of a bug on the client or on the server.
783 *
784 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700785 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * have unhashed the old state_owner for us, and that we can
787 * therefore safely retry using a new one. We should still warn
788 * the user though...
789 */
790 if (status == -NFS4ERR_BAD_SEQID) {
791 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
792 exception.retry = 1;
793 continue;
794 }
Trond Myklebust550f5742005-10-18 14:20:21 -0700795 /*
796 * BAD_STATEID on OPEN means that the server cancelled our
797 * state before it received the OPEN_CONFIRM.
798 * Recover by retrying the request as per the discussion
799 * on Page 181 of RFC3530.
800 */
801 if (status == -NFS4ERR_BAD_STATEID) {
802 exception.retry = 1;
803 continue;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
806 status, &exception));
807 } while (exception.retry);
808 return res;
809}
810
811static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
812 struct nfs_fh *fhandle, struct iattr *sattr,
813 struct nfs4_state *state)
814{
815 struct nfs_setattrargs arg = {
816 .fh = fhandle,
817 .iap = sattr,
818 .server = server,
819 .bitmask = server->attr_bitmask,
820 };
821 struct nfs_setattrres res = {
822 .fattr = fattr,
823 .server = server,
824 };
825 struct rpc_message msg = {
826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
827 .rpc_argp = &arg,
828 .rpc_resp = &res,
829 };
Trond Myklebust65e43082005-08-16 11:49:44 -0400830 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Trond Myklebust0e574af2005-10-27 22:12:38 -0400832 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000834 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000836 nfs4_copy_stateid(&arg.stateid, state, current->files);
837 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
839
Trond Myklebust65e43082005-08-16 11:49:44 -0400840 status = rpc_call_sync(server->client, &msg, 0);
841 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
845 struct nfs_fh *fhandle, struct iattr *sattr,
846 struct nfs4_state *state)
847{
848 struct nfs4_exception exception = { };
849 int err;
850 do {
851 err = nfs4_handle_exception(server,
852 _nfs4_do_setattr(server, fattr, fhandle, sattr,
853 state),
854 &exception);
855 } while (exception.retry);
856 return err;
857}
858
859struct nfs4_closedata {
860 struct inode *inode;
861 struct nfs4_state *state;
862 struct nfs_closeargs arg;
863 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -0400864 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100867static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -0700868{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100869 struct nfs4_closedata *calldata = data;
870 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -0700871
872 nfs4_put_open_state(calldata->state);
873 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700874 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -0700875 kfree(calldata);
876}
877
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100878static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100880 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct nfs_server *server = NFS_SERVER(calldata->inode);
883
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100884 if (RPC_ASSASSINATED(task))
885 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* hmm. we are done with the inode, and in the process of freeing
887 * the state_owner. we keep this around to process errors
888 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700889 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 switch (task->tk_status) {
891 case 0:
892 memcpy(&state->stateid, &calldata->res.stateid,
893 sizeof(state->stateid));
894 break;
895 case -NFS4ERR_STALE_STATEID:
896 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 nfs4_schedule_state_recovery(server->nfs4_state);
898 break;
899 default:
900 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
901 rpc_restart_call(task);
902 return;
903 }
904 }
Trond Myklebust516a6af2005-10-27 22:12:41 -0400905 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100908static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100910 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -0700911 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct rpc_message msg = {
913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
914 .rpc_argp = &calldata->arg,
915 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -0700916 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 };
Trond Myklebust4cecb762005-11-04 15:32:58 -0500918 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -0700919
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100920 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -0700921 return;
Trond Myklebust95121352005-10-18 14:20:12 -0700922 /* Recalculate the new open mode in case someone reopened the file
923 * while we were waiting in line to be scheduled.
924 */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500925 spin_lock(&state->owner->so_lock);
926 spin_lock(&calldata->inode->i_lock);
927 mode = old_mode = state->state;
928 if (state->nreaders == 0)
929 mode &= ~FMODE_READ;
930 if (state->nwriters == 0)
931 mode &= ~FMODE_WRITE;
932 nfs4_state_set_mode_locked(state, mode);
933 spin_unlock(&calldata->inode->i_lock);
934 spin_unlock(&state->owner->so_lock);
935 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100936 /* Note: exit _without_ calling nfs4_close_done */
937 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -0700938 return;
939 }
Trond Myklebust516a6af2005-10-27 22:12:41 -0400940 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -0700941 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -0700943 calldata->arg.open_flags = mode;
944 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100947static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100948 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100949 .rpc_call_done = nfs4_close_done,
950 .rpc_release = nfs4_free_closedata,
951};
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
954 * It is possible for data to be read/written from a mem-mapped file
955 * after the sys_close call (which hits the vfs layer as a flush).
956 * This means that we can't safely call nfsv4 close on a file until
957 * the inode is cleared. This in turn means that we are not good
958 * NFSv4 citizens - we do not indicate to the server to update the file's
959 * share state even when we are done with one of the three share
960 * stateid's in the inode.
961 *
962 * NOTE: Caller must be holding the sp->so_owner semaphore!
963 */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500964int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Trond Myklebust516a6af2005-10-27 22:12:41 -0400966 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -0700968 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Trond Myklebust95121352005-10-18 14:20:12 -0700970 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -0700972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 calldata->inode = inode;
974 calldata->state = state;
975 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -0700976 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700978 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700979 if (calldata->arg.seqid == NULL)
980 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -0400981 calldata->arg.bitmask = server->attr_bitmask;
982 calldata->res.fattr = &calldata->fattr;
983 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -0700984
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100985 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -0700986 if (status == 0)
987 goto out;
988
989 nfs_free_seqid(calldata->arg.seqid);
990out_free_calldata:
991 kfree(calldata);
992out:
993 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trond Myklebust02a913a2005-10-18 14:20:17 -0700996static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
997{
998 struct file *filp;
999
1000 filp = lookup_instantiate_filp(nd, dentry, NULL);
1001 if (!IS_ERR(filp)) {
1002 struct nfs_open_context *ctx;
1003 ctx = (struct nfs_open_context *)filp->private_data;
1004 ctx->state = state;
1005 } else
1006 nfs4_close_state(state, nd->intent.open.flags);
1007}
1008
1009struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1011{
1012 struct iattr attr;
1013 struct rpc_cred *cred;
1014 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001015 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (nd->flags & LOOKUP_CREATE) {
1018 attr.ia_mode = nd->intent.open.create_mode;
1019 attr.ia_valid = ATTR_MODE;
1020 if (!IS_POSIXACL(dir))
1021 attr.ia_mode &= ~current->fs->umask;
1022 } else {
1023 attr.ia_valid = 0;
1024 BUG_ON(nd->intent.open.flags & O_CREAT);
1025 }
1026
1027 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1028 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001029 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1031 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001032 if (IS_ERR(state)) {
1033 if (PTR_ERR(state) == -ENOENT)
1034 d_add(dentry, NULL);
1035 return (struct dentry *)state;
1036 }
1037 res = d_add_unique(dentry, state->inode);
1038 if (res != NULL)
1039 dentry = res;
1040 nfs4_intent_set_file(nd, dentry, state);
1041 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001045nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct rpc_cred *cred;
1048 struct nfs4_state *state;
1049 struct inode *inode;
1050
1051 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1052 if (IS_ERR(cred))
1053 return PTR_ERR(cred);
1054 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1055 if (IS_ERR(state))
1056 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1057 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001058 if (IS_ERR(state)) {
1059 switch (PTR_ERR(state)) {
1060 case -EPERM:
1061 case -EACCES:
1062 case -EDQUOT:
1063 case -ENOSPC:
1064 case -EROFS:
1065 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1066 return 1;
1067 case -ENOENT:
1068 if (dentry->d_inode == NULL)
1069 return 1;
1070 }
1071 goto out_drop;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 inode = state->inode;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001074 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001076 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 1;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001080out_drop:
1081 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
1085
1086static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1087{
1088 struct nfs4_server_caps_res res = {};
1089 struct rpc_message msg = {
1090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1091 .rpc_argp = fhandle,
1092 .rpc_resp = &res,
1093 };
1094 int status;
1095
1096 status = rpc_call_sync(server->client, &msg, 0);
1097 if (status == 0) {
1098 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1099 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1100 server->caps |= NFS_CAP_ACLS;
1101 if (res.has_links != 0)
1102 server->caps |= NFS_CAP_HARDLINKS;
1103 if (res.has_symlinks != 0)
1104 server->caps |= NFS_CAP_SYMLINKS;
1105 server->acl_bitmask = res.acl_bitmask;
1106 }
1107 return status;
1108}
1109
1110static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1111{
1112 struct nfs4_exception exception = { };
1113 int err;
1114 do {
1115 err = nfs4_handle_exception(server,
1116 _nfs4_server_capabilities(server, fhandle),
1117 &exception);
1118 } while (exception.retry);
1119 return err;
1120}
1121
1122static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1123 struct nfs_fsinfo *info)
1124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct nfs4_lookup_root_arg args = {
1126 .bitmask = nfs4_fattr_bitmap,
1127 };
1128 struct nfs4_lookup_res res = {
1129 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001130 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 .fh = fhandle,
1132 };
1133 struct rpc_message msg = {
1134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1135 .rpc_argp = &args,
1136 .rpc_resp = &res,
1137 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001138 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return rpc_call_sync(server->client, &msg, 0);
1140}
1141
1142static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1143 struct nfs_fsinfo *info)
1144{
1145 struct nfs4_exception exception = { };
1146 int err;
1147 do {
1148 err = nfs4_handle_exception(server,
1149 _nfs4_lookup_root(server, fhandle, info),
1150 &exception);
1151 } while (exception.retry);
1152 return err;
1153}
1154
1155static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1156 struct nfs_fsinfo *info)
1157{
1158 struct nfs_fattr * fattr = info->fattr;
1159 unsigned char * p;
1160 struct qstr q;
1161 struct nfs4_lookup_arg args = {
1162 .dir_fh = fhandle,
1163 .name = &q,
1164 .bitmask = nfs4_fattr_bitmap,
1165 };
1166 struct nfs4_lookup_res res = {
1167 .server = server,
1168 .fattr = fattr,
1169 .fh = fhandle,
1170 };
1171 struct rpc_message msg = {
1172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1173 .rpc_argp = &args,
1174 .rpc_resp = &res,
1175 };
1176 int status;
1177
1178 /*
1179 * Now we do a separate LOOKUP for each component of the mount path.
1180 * The LOOKUPs are done separately so that we can conveniently
1181 * catch an ERR_WRONGSEC if it occurs along the way...
1182 */
1183 status = nfs4_lookup_root(server, fhandle, info);
1184 if (status)
1185 goto out;
1186
1187 p = server->mnt_path;
1188 for (;;) {
1189 struct nfs4_exception exception = { };
1190
1191 while (*p == '/')
1192 p++;
1193 if (!*p)
1194 break;
1195 q.name = p;
1196 while (*p && (*p != '/'))
1197 p++;
1198 q.len = p - q.name;
1199
1200 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001201 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 status = nfs4_handle_exception(server,
1203 rpc_call_sync(server->client, &msg, 0),
1204 &exception);
1205 } while (exception.retry);
1206 if (status == 0)
1207 continue;
1208 if (status == -ENOENT) {
1209 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1210 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1211 }
1212 break;
1213 }
1214 if (status == 0)
1215 status = nfs4_server_capabilities(server, fhandle);
1216 if (status == 0)
1217 status = nfs4_do_fsinfo(server, fhandle, info);
1218out:
1219 return status;
1220}
1221
1222static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1223{
1224 struct nfs4_getattr_arg args = {
1225 .fh = fhandle,
1226 .bitmask = server->attr_bitmask,
1227 };
1228 struct nfs4_getattr_res res = {
1229 .fattr = fattr,
1230 .server = server,
1231 };
1232 struct rpc_message msg = {
1233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1234 .rpc_argp = &args,
1235 .rpc_resp = &res,
1236 };
1237
Trond Myklebust0e574af2005-10-27 22:12:38 -04001238 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return rpc_call_sync(server->client, &msg, 0);
1240}
1241
1242static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1243{
1244 struct nfs4_exception exception = { };
1245 int err;
1246 do {
1247 err = nfs4_handle_exception(server,
1248 _nfs4_proc_getattr(server, fhandle, fattr),
1249 &exception);
1250 } while (exception.retry);
1251 return err;
1252}
1253
1254/*
1255 * The file is not closed if it is opened due to the a request to change
1256 * the size of the file. The open call will not be needed once the
1257 * VFS layer lookup-intents are implemented.
1258 *
1259 * Close is called when the inode is destroyed.
1260 * If we haven't opened the file for O_WRONLY, we
1261 * need to in the size_change case to obtain a stateid.
1262 *
1263 * Got race?
1264 * Because OPEN is always done by name in nfsv4, it is
1265 * possible that we opened a different file by the same
1266 * name. We can recognize this race condition, but we
1267 * can't do anything about it besides returning an error.
1268 *
1269 * This will be fixed with VFS changes (lookup-intent).
1270 */
1271static int
1272nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1273 struct iattr *sattr)
1274{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001275 struct rpc_cred *cred;
1276 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001277 struct nfs_open_context *ctx;
1278 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int status;
1280
Trond Myklebust0e574af2005-10-27 22:12:38 -04001281 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001283 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1284 if (IS_ERR(cred))
1285 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001286
1287 /* Search for an existing open(O_WRITE) file */
1288 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1289 if (ctx != NULL)
1290 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1293 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001294 if (status == 0)
1295 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001296 if (ctx != NULL)
1297 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001298 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return status;
1300}
1301
1302static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1303 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1304{
1305 int status;
1306 struct nfs_server *server = NFS_SERVER(dir);
1307 struct nfs4_lookup_arg args = {
1308 .bitmask = server->attr_bitmask,
1309 .dir_fh = NFS_FH(dir),
1310 .name = name,
1311 };
1312 struct nfs4_lookup_res res = {
1313 .server = server,
1314 .fattr = fattr,
1315 .fh = fhandle,
1316 };
1317 struct rpc_message msg = {
1318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1319 .rpc_argp = &args,
1320 .rpc_resp = &res,
1321 };
1322
Trond Myklebust0e574af2005-10-27 22:12:38 -04001323 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 dprintk("NFS call lookup %s\n", name->name);
1326 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1327 dprintk("NFS reply lookup: %d\n", status);
1328 return status;
1329}
1330
1331static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1332{
1333 struct nfs4_exception exception = { };
1334 int err;
1335 do {
1336 err = nfs4_handle_exception(NFS_SERVER(dir),
1337 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1338 &exception);
1339 } while (exception.retry);
1340 return err;
1341}
1342
1343static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1344{
1345 struct nfs4_accessargs args = {
1346 .fh = NFS_FH(inode),
1347 };
1348 struct nfs4_accessres res = { 0 };
1349 struct rpc_message msg = {
1350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1351 .rpc_argp = &args,
1352 .rpc_resp = &res,
1353 .rpc_cred = entry->cred,
1354 };
1355 int mode = entry->mask;
1356 int status;
1357
1358 /*
1359 * Determine which access bits we want to ask for...
1360 */
1361 if (mode & MAY_READ)
1362 args.access |= NFS4_ACCESS_READ;
1363 if (S_ISDIR(inode->i_mode)) {
1364 if (mode & MAY_WRITE)
1365 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1366 if (mode & MAY_EXEC)
1367 args.access |= NFS4_ACCESS_LOOKUP;
1368 } else {
1369 if (mode & MAY_WRITE)
1370 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1371 if (mode & MAY_EXEC)
1372 args.access |= NFS4_ACCESS_EXECUTE;
1373 }
1374 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1375 if (!status) {
1376 entry->mask = 0;
1377 if (res.access & NFS4_ACCESS_READ)
1378 entry->mask |= MAY_READ;
1379 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1380 entry->mask |= MAY_WRITE;
1381 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1382 entry->mask |= MAY_EXEC;
1383 }
1384 return status;
1385}
1386
1387static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1388{
1389 struct nfs4_exception exception = { };
1390 int err;
1391 do {
1392 err = nfs4_handle_exception(NFS_SERVER(inode),
1393 _nfs4_proc_access(inode, entry),
1394 &exception);
1395 } while (exception.retry);
1396 return err;
1397}
1398
1399/*
1400 * TODO: For the time being, we don't try to get any attributes
1401 * along with any of the zero-copy operations READ, READDIR,
1402 * READLINK, WRITE.
1403 *
1404 * In the case of the first three, we want to put the GETATTR
1405 * after the read-type operation -- this is because it is hard
1406 * to predict the length of a GETATTR response in v4, and thus
1407 * align the READ data correctly. This means that the GETATTR
1408 * may end up partially falling into the page cache, and we should
1409 * shift it into the 'tail' of the xdr_buf before processing.
1410 * To do this efficiently, we need to know the total length
1411 * of data received, which doesn't seem to be available outside
1412 * of the RPC layer.
1413 *
1414 * In the case of WRITE, we also want to put the GETATTR after
1415 * the operation -- in this case because we want to make sure
1416 * we get the post-operation mtime and size. This means that
1417 * we can't use xdr_encode_pages() as written: we need a variant
1418 * of it which would leave room in the 'tail' iovec.
1419 *
1420 * Both of these changes to the XDR layer would in fact be quite
1421 * minor, but I decided to leave them for a subsequent patch.
1422 */
1423static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1424 unsigned int pgbase, unsigned int pglen)
1425{
1426 struct nfs4_readlink args = {
1427 .fh = NFS_FH(inode),
1428 .pgbase = pgbase,
1429 .pglen = pglen,
1430 .pages = &page,
1431 };
1432 struct rpc_message msg = {
1433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1434 .rpc_argp = &args,
1435 .rpc_resp = NULL,
1436 };
1437
1438 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1439}
1440
1441static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1442 unsigned int pgbase, unsigned int pglen)
1443{
1444 struct nfs4_exception exception = { };
1445 int err;
1446 do {
1447 err = nfs4_handle_exception(NFS_SERVER(inode),
1448 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1449 &exception);
1450 } while (exception.retry);
1451 return err;
1452}
1453
1454static int _nfs4_proc_read(struct nfs_read_data *rdata)
1455{
1456 int flags = rdata->flags;
1457 struct inode *inode = rdata->inode;
1458 struct nfs_fattr *fattr = rdata->res.fattr;
1459 struct nfs_server *server = NFS_SERVER(inode);
1460 struct rpc_message msg = {
1461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1462 .rpc_argp = &rdata->args,
1463 .rpc_resp = &rdata->res,
1464 .rpc_cred = rdata->cred,
1465 };
1466 unsigned long timestamp = jiffies;
1467 int status;
1468
1469 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1470 (long long) rdata->args.offset);
1471
Trond Myklebust0e574af2005-10-27 22:12:38 -04001472 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 status = rpc_call_sync(server->client, &msg, flags);
1474 if (!status)
1475 renew_lease(server, timestamp);
1476 dprintk("NFS reply read: %d\n", status);
1477 return status;
1478}
1479
1480static int nfs4_proc_read(struct nfs_read_data *rdata)
1481{
1482 struct nfs4_exception exception = { };
1483 int err;
1484 do {
1485 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1486 _nfs4_proc_read(rdata),
1487 &exception);
1488 } while (exception.retry);
1489 return err;
1490}
1491
1492static int _nfs4_proc_write(struct nfs_write_data *wdata)
1493{
1494 int rpcflags = wdata->flags;
1495 struct inode *inode = wdata->inode;
1496 struct nfs_fattr *fattr = wdata->res.fattr;
1497 struct nfs_server *server = NFS_SERVER(inode);
1498 struct rpc_message msg = {
1499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1500 .rpc_argp = &wdata->args,
1501 .rpc_resp = &wdata->res,
1502 .rpc_cred = wdata->cred,
1503 };
1504 int status;
1505
1506 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1507 (long long) wdata->args.offset);
1508
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001509 wdata->args.bitmask = server->attr_bitmask;
1510 wdata->res.server = server;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001511 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 status = rpc_call_sync(server->client, &msg, rpcflags);
1513 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001514 if (status < 0)
1515 return status;
1516 nfs_post_op_update_inode(inode, fattr);
1517 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520static int nfs4_proc_write(struct nfs_write_data *wdata)
1521{
1522 struct nfs4_exception exception = { };
1523 int err;
1524 do {
1525 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1526 _nfs4_proc_write(wdata),
1527 &exception);
1528 } while (exception.retry);
1529 return err;
1530}
1531
1532static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1533{
1534 struct inode *inode = cdata->inode;
1535 struct nfs_fattr *fattr = cdata->res.fattr;
1536 struct nfs_server *server = NFS_SERVER(inode);
1537 struct rpc_message msg = {
1538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1539 .rpc_argp = &cdata->args,
1540 .rpc_resp = &cdata->res,
1541 .rpc_cred = cdata->cred,
1542 };
1543 int status;
1544
1545 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1546 (long long) cdata->args.offset);
1547
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001548 cdata->args.bitmask = server->attr_bitmask;
1549 cdata->res.server = server;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001550 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 status = rpc_call_sync(server->client, &msg, 0);
1552 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001553 if (status >= 0)
1554 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return status;
1556}
1557
1558static int nfs4_proc_commit(struct nfs_write_data *cdata)
1559{
1560 struct nfs4_exception exception = { };
1561 int err;
1562 do {
1563 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1564 _nfs4_proc_commit(cdata),
1565 &exception);
1566 } while (exception.retry);
1567 return err;
1568}
1569
1570/*
1571 * Got race?
1572 * We will need to arrange for the VFS layer to provide an atomic open.
1573 * Until then, this create/open method is prone to inefficiency and race
1574 * conditions due to the lookup, create, and open VFS calls from sys_open()
1575 * placed on the wire.
1576 *
1577 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1578 * The file will be opened again in the subsequent VFS open call
1579 * (nfs4_proc_file_open).
1580 *
1581 * The open for read will just hang around to be used by any process that
1582 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1583 */
1584
1585static int
1586nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001587 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 struct nfs4_state *state;
1590 struct rpc_cred *cred;
1591 int status = 0;
1592
1593 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1594 if (IS_ERR(cred)) {
1595 status = PTR_ERR(cred);
1596 goto out;
1597 }
1598 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1599 put_rpccred(cred);
1600 if (IS_ERR(state)) {
1601 status = PTR_ERR(state);
1602 goto out;
1603 }
1604 d_instantiate(dentry, state->inode);
1605 if (flags & O_EXCL) {
1606 struct nfs_fattr fattr;
1607 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1608 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001609 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001610 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001611 }
1612 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1613 nfs4_intent_set_file(nd, dentry, state);
1614 else
1615 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616out:
1617 return status;
1618}
1619
1620static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1621{
Trond Myklebust16e42952005-10-27 22:12:44 -04001622 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct nfs4_remove_arg args = {
1624 .fh = NFS_FH(dir),
1625 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001626 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001628 struct nfs_fattr dir_attr;
1629 struct nfs4_remove_res res = {
1630 .server = server,
1631 .dir_attr = &dir_attr,
1632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct rpc_message msg = {
1634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1635 .rpc_argp = &args,
1636 .rpc_resp = &res,
1637 };
1638 int status;
1639
Trond Myklebust16e42952005-10-27 22:12:44 -04001640 nfs_fattr_init(res.dir_attr);
1641 status = rpc_call_sync(server->client, &msg, 0);
1642 if (status == 0) {
1643 update_changeattr(dir, &res.cinfo);
1644 nfs_post_op_update_inode(dir, res.dir_attr);
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return status;
1647}
1648
1649static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1650{
1651 struct nfs4_exception exception = { };
1652 int err;
1653 do {
1654 err = nfs4_handle_exception(NFS_SERVER(dir),
1655 _nfs4_proc_remove(dir, name),
1656 &exception);
1657 } while (exception.retry);
1658 return err;
1659}
1660
1661struct unlink_desc {
1662 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001663 struct nfs4_remove_res res;
1664 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665};
1666
1667static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1668 struct qstr *name)
1669{
Trond Myklebust16e42952005-10-27 22:12:44 -04001670 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct unlink_desc *up;
1672
1673 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1674 if (!up)
1675 return -ENOMEM;
1676
1677 up->args.fh = NFS_FH(dir->d_inode);
1678 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001679 up->args.bitmask = server->attr_bitmask;
1680 up->res.server = server;
1681 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1684 msg->rpc_argp = &up->args;
1685 msg->rpc_resp = &up->res;
1686 return 0;
1687}
1688
1689static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1690{
1691 struct rpc_message *msg = &task->tk_msg;
1692 struct unlink_desc *up;
1693
1694 if (msg->rpc_resp != NULL) {
1695 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001696 update_changeattr(dir->d_inode, &up->res.cinfo);
1697 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 kfree(up);
1699 msg->rpc_resp = NULL;
1700 msg->rpc_argp = NULL;
1701 }
1702 return 0;
1703}
1704
1705static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1706 struct inode *new_dir, struct qstr *new_name)
1707{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001708 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct nfs4_rename_arg arg = {
1710 .old_dir = NFS_FH(old_dir),
1711 .new_dir = NFS_FH(new_dir),
1712 .old_name = old_name,
1713 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001714 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001716 struct nfs_fattr old_fattr, new_fattr;
1717 struct nfs4_rename_res res = {
1718 .server = server,
1719 .old_fattr = &old_fattr,
1720 .new_fattr = &new_fattr,
1721 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct rpc_message msg = {
1723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1724 .rpc_argp = &arg,
1725 .rpc_resp = &res,
1726 };
1727 int status;
1728
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001729 nfs_fattr_init(res.old_fattr);
1730 nfs_fattr_init(res.new_fattr);
1731 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (!status) {
1734 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001735 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001737 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 return status;
1740}
1741
1742static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1743 struct inode *new_dir, struct qstr *new_name)
1744{
1745 struct nfs4_exception exception = { };
1746 int err;
1747 do {
1748 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1749 _nfs4_proc_rename(old_dir, old_name,
1750 new_dir, new_name),
1751 &exception);
1752 } while (exception.retry);
1753 return err;
1754}
1755
1756static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1757{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001758 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 struct nfs4_link_arg arg = {
1760 .fh = NFS_FH(inode),
1761 .dir_fh = NFS_FH(dir),
1762 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001763 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001765 struct nfs_fattr fattr, dir_attr;
1766 struct nfs4_link_res res = {
1767 .server = server,
1768 .fattr = &fattr,
1769 .dir_attr = &dir_attr,
1770 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct rpc_message msg = {
1772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1773 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001774 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 };
1776 int status;
1777
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001778 nfs_fattr_init(res.fattr);
1779 nfs_fattr_init(res.dir_attr);
1780 status = rpc_call_sync(server->client, &msg, 0);
1781 if (!status) {
1782 update_changeattr(dir, &res.cinfo);
1783 nfs_post_op_update_inode(dir, res.dir_attr);
1784 nfs_refresh_inode(inode, res.fattr);
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 return status;
1788}
1789
1790static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1791{
1792 struct nfs4_exception exception = { };
1793 int err;
1794 do {
1795 err = nfs4_handle_exception(NFS_SERVER(inode),
1796 _nfs4_proc_link(inode, dir, name),
1797 &exception);
1798 } while (exception.retry);
1799 return err;
1800}
1801
1802static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1803 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1804 struct nfs_fattr *fattr)
1805{
1806 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001807 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct nfs4_create_arg arg = {
1809 .dir_fh = NFS_FH(dir),
1810 .server = server,
1811 .name = name,
1812 .attrs = sattr,
1813 .ftype = NF4LNK,
1814 .bitmask = server->attr_bitmask,
1815 };
1816 struct nfs4_create_res res = {
1817 .server = server,
1818 .fh = fhandle,
1819 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001820 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 };
1822 struct rpc_message msg = {
1823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1824 .rpc_argp = &arg,
1825 .rpc_resp = &res,
1826 };
1827 int status;
1828
1829 if (path->len > NFS4_MAXPATHLEN)
1830 return -ENAMETOOLONG;
1831 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001832 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001833 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1836 if (!status)
1837 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001838 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return status;
1840}
1841
1842static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1843 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1844 struct nfs_fattr *fattr)
1845{
1846 struct nfs4_exception exception = { };
1847 int err;
1848 do {
1849 err = nfs4_handle_exception(NFS_SERVER(dir),
1850 _nfs4_proc_symlink(dir, name, path, sattr,
1851 fhandle, fattr),
1852 &exception);
1853 } while (exception.retry);
1854 return err;
1855}
1856
1857static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1858 struct iattr *sattr)
1859{
1860 struct nfs_server *server = NFS_SERVER(dir);
1861 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001862 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct nfs4_create_arg arg = {
1864 .dir_fh = NFS_FH(dir),
1865 .server = server,
1866 .name = &dentry->d_name,
1867 .attrs = sattr,
1868 .ftype = NF4DIR,
1869 .bitmask = server->attr_bitmask,
1870 };
1871 struct nfs4_create_res res = {
1872 .server = server,
1873 .fh = &fhandle,
1874 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001875 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 };
1877 struct rpc_message msg = {
1878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1879 .rpc_argp = &arg,
1880 .rpc_resp = &res,
1881 };
1882 int status;
1883
Trond Myklebust0e574af2005-10-27 22:12:38 -04001884 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001885 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1888 if (!status) {
1889 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001890 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 status = nfs_instantiate(dentry, &fhandle, &fattr);
1892 }
1893 return status;
1894}
1895
1896static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1897 struct iattr *sattr)
1898{
1899 struct nfs4_exception exception = { };
1900 int err;
1901 do {
1902 err = nfs4_handle_exception(NFS_SERVER(dir),
1903 _nfs4_proc_mkdir(dir, dentry, sattr),
1904 &exception);
1905 } while (exception.retry);
1906 return err;
1907}
1908
1909static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1910 u64 cookie, struct page *page, unsigned int count, int plus)
1911{
1912 struct inode *dir = dentry->d_inode;
1913 struct nfs4_readdir_arg args = {
1914 .fh = NFS_FH(dir),
1915 .pages = &page,
1916 .pgbase = 0,
1917 .count = count,
1918 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1919 };
1920 struct nfs4_readdir_res res;
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1923 .rpc_argp = &args,
1924 .rpc_resp = &res,
1925 .rpc_cred = cred,
1926 };
1927 int status;
1928
Trond Myklebusteadf4592005-06-22 17:16:39 +00001929 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1930 dentry->d_parent->d_name.name,
1931 dentry->d_name.name,
1932 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 lock_kernel();
1934 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1935 res.pgbase = args.pgbase;
1936 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1937 if (status == 0)
1938 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1939 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00001940 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return status;
1942}
1943
1944static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1945 u64 cookie, struct page *page, unsigned int count, int plus)
1946{
1947 struct nfs4_exception exception = { };
1948 int err;
1949 do {
1950 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1951 _nfs4_proc_readdir(dentry, cred, cookie,
1952 page, count, plus),
1953 &exception);
1954 } while (exception.retry);
1955 return err;
1956}
1957
1958static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1959 struct iattr *sattr, dev_t rdev)
1960{
1961 struct nfs_server *server = NFS_SERVER(dir);
1962 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001963 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct nfs4_create_arg arg = {
1965 .dir_fh = NFS_FH(dir),
1966 .server = server,
1967 .name = &dentry->d_name,
1968 .attrs = sattr,
1969 .bitmask = server->attr_bitmask,
1970 };
1971 struct nfs4_create_res res = {
1972 .server = server,
1973 .fh = &fh,
1974 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001975 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 };
1977 struct rpc_message msg = {
1978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1979 .rpc_argp = &arg,
1980 .rpc_resp = &res,
1981 };
1982 int status;
1983 int mode = sattr->ia_mode;
1984
Trond Myklebust0e574af2005-10-27 22:12:38 -04001985 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001986 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1989 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1990 if (S_ISFIFO(mode))
1991 arg.ftype = NF4FIFO;
1992 else if (S_ISBLK(mode)) {
1993 arg.ftype = NF4BLK;
1994 arg.u.device.specdata1 = MAJOR(rdev);
1995 arg.u.device.specdata2 = MINOR(rdev);
1996 }
1997 else if (S_ISCHR(mode)) {
1998 arg.ftype = NF4CHR;
1999 arg.u.device.specdata1 = MAJOR(rdev);
2000 arg.u.device.specdata2 = MINOR(rdev);
2001 }
2002 else
2003 arg.ftype = NF4SOCK;
2004
2005 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2006 if (status == 0) {
2007 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002008 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 status = nfs_instantiate(dentry, &fh, &fattr);
2010 }
2011 return status;
2012}
2013
2014static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2015 struct iattr *sattr, dev_t rdev)
2016{
2017 struct nfs4_exception exception = { };
2018 int err;
2019 do {
2020 err = nfs4_handle_exception(NFS_SERVER(dir),
2021 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2022 &exception);
2023 } while (exception.retry);
2024 return err;
2025}
2026
2027static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2028 struct nfs_fsstat *fsstat)
2029{
2030 struct nfs4_statfs_arg args = {
2031 .fh = fhandle,
2032 .bitmask = server->attr_bitmask,
2033 };
2034 struct rpc_message msg = {
2035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2036 .rpc_argp = &args,
2037 .rpc_resp = fsstat,
2038 };
2039
Trond Myklebust0e574af2005-10-27 22:12:38 -04002040 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return rpc_call_sync(server->client, &msg, 0);
2042}
2043
2044static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2045{
2046 struct nfs4_exception exception = { };
2047 int err;
2048 do {
2049 err = nfs4_handle_exception(server,
2050 _nfs4_proc_statfs(server, fhandle, fsstat),
2051 &exception);
2052 } while (exception.retry);
2053 return err;
2054}
2055
2056static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2057 struct nfs_fsinfo *fsinfo)
2058{
2059 struct nfs4_fsinfo_arg args = {
2060 .fh = fhandle,
2061 .bitmask = server->attr_bitmask,
2062 };
2063 struct rpc_message msg = {
2064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2065 .rpc_argp = &args,
2066 .rpc_resp = fsinfo,
2067 };
2068
2069 return rpc_call_sync(server->client, &msg, 0);
2070}
2071
2072static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2073{
2074 struct nfs4_exception exception = { };
2075 int err;
2076
2077 do {
2078 err = nfs4_handle_exception(server,
2079 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2080 &exception);
2081 } while (exception.retry);
2082 return err;
2083}
2084
2085static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2086{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002087 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2089}
2090
2091static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2092 struct nfs_pathconf *pathconf)
2093{
2094 struct nfs4_pathconf_arg args = {
2095 .fh = fhandle,
2096 .bitmask = server->attr_bitmask,
2097 };
2098 struct rpc_message msg = {
2099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2100 .rpc_argp = &args,
2101 .rpc_resp = pathconf,
2102 };
2103
2104 /* None of the pathconf attributes are mandatory to implement */
2105 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2106 memset(pathconf, 0, sizeof(*pathconf));
2107 return 0;
2108 }
2109
Trond Myklebust0e574af2005-10-27 22:12:38 -04002110 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return rpc_call_sync(server->client, &msg, 0);
2112}
2113
2114static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2115 struct nfs_pathconf *pathconf)
2116{
2117 struct nfs4_exception exception = { };
2118 int err;
2119
2120 do {
2121 err = nfs4_handle_exception(server,
2122 _nfs4_proc_pathconf(server, fhandle, pathconf),
2123 &exception);
2124 } while (exception.retry);
2125 return err;
2126}
2127
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002128static void nfs4_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002130 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct inode *inode = data->inode;
2132
2133 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2134 rpc_restart_call(task);
2135 return;
2136 }
2137 if (task->tk_status > 0)
2138 renew_lease(NFS_SERVER(inode), data->timestamp);
2139 /* Call back common NFS readpage processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002140 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002143static const struct rpc_call_ops nfs4_read_ops = {
2144 .rpc_call_done = nfs4_read_done,
2145 .rpc_release = nfs_readdata_release,
2146};
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148static void
2149nfs4_proc_read_setup(struct nfs_read_data *data)
2150{
2151 struct rpc_task *task = &data->task;
2152 struct rpc_message msg = {
2153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2154 .rpc_argp = &data->args,
2155 .rpc_resp = &data->res,
2156 .rpc_cred = data->cred,
2157 };
2158 struct inode *inode = data->inode;
2159 int flags;
2160
2161 data->timestamp = jiffies;
2162
2163 /* N.B. Do we need to test? Never called for swapfile inode */
2164 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2165
2166 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002167 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 rpc_call_setup(task, &msg, 0);
2169}
2170
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002171static void nfs4_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002173 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct inode *inode = data->inode;
2175
2176 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2177 rpc_restart_call(task);
2178 return;
2179 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002180 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002182 nfs_post_op_update_inode(inode, data->res.fattr);
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002185 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002188static const struct rpc_call_ops nfs4_write_ops = {
2189 .rpc_call_done = nfs4_write_done,
2190 .rpc_release = nfs_writedata_release,
2191};
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193static void
2194nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2195{
2196 struct rpc_task *task = &data->task;
2197 struct rpc_message msg = {
2198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2199 .rpc_argp = &data->args,
2200 .rpc_resp = &data->res,
2201 .rpc_cred = data->cred,
2202 };
2203 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002204 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 int stable;
2206 int flags;
2207
2208 if (how & FLUSH_STABLE) {
2209 if (!NFS_I(inode)->ncommit)
2210 stable = NFS_FILE_SYNC;
2211 else
2212 stable = NFS_DATA_SYNC;
2213 } else
2214 stable = NFS_UNSTABLE;
2215 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002216 data->args.bitmask = server->attr_bitmask;
2217 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 data->timestamp = jiffies;
2220
2221 /* Set the initial flags for the task. */
2222 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2223
2224 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002225 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 rpc_call_setup(task, &msg, 0);
2227}
2228
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002229static void nfs4_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002231 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct inode *inode = data->inode;
2233
2234 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2235 rpc_restart_call(task);
2236 return;
2237 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002238 if (task->tk_status >= 0)
2239 nfs_post_op_update_inode(inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002241 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244static const struct rpc_call_ops nfs4_commit_ops = {
2245 .rpc_call_done = nfs4_commit_done,
2246 .rpc_release = nfs_commit_release,
2247};
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249static void
2250nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2251{
2252 struct rpc_task *task = &data->task;
2253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2255 .rpc_argp = &data->args,
2256 .rpc_resp = &data->res,
2257 .rpc_cred = data->cred,
2258 };
2259 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002260 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 int flags;
2262
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002263 data->args.bitmask = server->attr_bitmask;
2264 data->res.server = server;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /* Set the initial flags for the task. */
2267 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2268
2269 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002270 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 rpc_call_setup(task, &msg, 0);
2272}
2273
2274/*
2275 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2276 * standalone procedure for queueing an asynchronous RENEW.
2277 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002278static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002281 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 if (task->tk_status < 0) {
2284 switch (task->tk_status) {
2285 case -NFS4ERR_STALE_CLIENTID:
2286 case -NFS4ERR_EXPIRED:
2287 case -NFS4ERR_CB_PATH_DOWN:
2288 nfs4_schedule_state_recovery(clp);
2289 }
2290 return;
2291 }
2292 spin_lock(&clp->cl_lock);
2293 if (time_before(clp->cl_last_renewal,timestamp))
2294 clp->cl_last_renewal = timestamp;
2295 spin_unlock(&clp->cl_lock);
2296}
2297
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002298static const struct rpc_call_ops nfs4_renew_ops = {
2299 .rpc_call_done = nfs4_renew_done,
2300};
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302int
2303nfs4_proc_async_renew(struct nfs4_client *clp)
2304{
2305 struct rpc_message msg = {
2306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2307 .rpc_argp = clp,
2308 .rpc_cred = clp->cl_cred,
2309 };
2310
2311 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002312 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315int
2316nfs4_proc_renew(struct nfs4_client *clp)
2317{
2318 struct rpc_message msg = {
2319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2320 .rpc_argp = clp,
2321 .rpc_cred = clp->cl_cred,
2322 };
2323 unsigned long now = jiffies;
2324 int status;
2325
2326 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2327 if (status < 0)
2328 return status;
2329 spin_lock(&clp->cl_lock);
2330 if (time_before(clp->cl_last_renewal,now))
2331 clp->cl_last_renewal = now;
2332 spin_unlock(&clp->cl_lock);
2333 return 0;
2334}
2335
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002336static inline int nfs4_server_supports_acls(struct nfs_server *server)
2337{
2338 return (server->caps & NFS_CAP_ACLS)
2339 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2340 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2341}
2342
2343/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2344 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2345 * the stack.
2346 */
2347#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2348
2349static void buf_to_pages(const void *buf, size_t buflen,
2350 struct page **pages, unsigned int *pgbase)
2351{
2352 const void *p = buf;
2353
2354 *pgbase = offset_in_page(buf);
2355 p -= *pgbase;
2356 while (p < buf + buflen) {
2357 *(pages++) = virt_to_page(p);
2358 p += PAGE_CACHE_SIZE;
2359 }
2360}
2361
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002362struct nfs4_cached_acl {
2363 int cached;
2364 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002365 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002366};
2367
2368static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002369{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002370 struct nfs_inode *nfsi = NFS_I(inode);
2371
2372 spin_lock(&inode->i_lock);
2373 kfree(nfsi->nfs4_acl);
2374 nfsi->nfs4_acl = acl;
2375 spin_unlock(&inode->i_lock);
2376}
2377
2378static void nfs4_zap_acl_attr(struct inode *inode)
2379{
2380 nfs4_set_cached_acl(inode, NULL);
2381}
2382
2383static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2384{
2385 struct nfs_inode *nfsi = NFS_I(inode);
2386 struct nfs4_cached_acl *acl;
2387 int ret = -ENOENT;
2388
2389 spin_lock(&inode->i_lock);
2390 acl = nfsi->nfs4_acl;
2391 if (acl == NULL)
2392 goto out;
2393 if (buf == NULL) /* user is just asking for length */
2394 goto out_len;
2395 if (acl->cached == 0)
2396 goto out;
2397 ret = -ERANGE; /* see getxattr(2) man page */
2398 if (acl->len > buflen)
2399 goto out;
2400 memcpy(buf, acl->data, acl->len);
2401out_len:
2402 ret = acl->len;
2403out:
2404 spin_unlock(&inode->i_lock);
2405 return ret;
2406}
2407
2408static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2409{
2410 struct nfs4_cached_acl *acl;
2411
2412 if (buf && acl_len <= PAGE_SIZE) {
2413 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2414 if (acl == NULL)
2415 goto out;
2416 acl->cached = 1;
2417 memcpy(acl->data, buf, acl_len);
2418 } else {
2419 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2420 if (acl == NULL)
2421 goto out;
2422 acl->cached = 0;
2423 }
2424 acl->len = acl_len;
2425out:
2426 nfs4_set_cached_acl(inode, acl);
2427}
2428
2429static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2430{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002431 struct page *pages[NFS4ACL_MAXPAGES];
2432 struct nfs_getaclargs args = {
2433 .fh = NFS_FH(inode),
2434 .acl_pages = pages,
2435 .acl_len = buflen,
2436 };
2437 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002438 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002439 struct rpc_message msg = {
2440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2441 .rpc_argp = &args,
2442 .rpc_resp = &resp_len,
2443 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002444 struct page *localpage = NULL;
2445 int ret;
2446
2447 if (buflen < PAGE_SIZE) {
2448 /* As long as we're doing a round trip to the server anyway,
2449 * let's be prepared for a page of acl data. */
2450 localpage = alloc_page(GFP_KERNEL);
2451 resp_buf = page_address(localpage);
2452 if (localpage == NULL)
2453 return -ENOMEM;
2454 args.acl_pages[0] = localpage;
2455 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002456 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002457 } else {
2458 resp_buf = buf;
2459 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2460 }
2461 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2462 if (ret)
2463 goto out_free;
2464 if (resp_len > args.acl_len)
2465 nfs4_write_cached_acl(inode, NULL, resp_len);
2466 else
2467 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2468 if (buf) {
2469 ret = -ERANGE;
2470 if (resp_len > buflen)
2471 goto out_free;
2472 if (localpage)
2473 memcpy(buf, resp_buf, resp_len);
2474 }
2475 ret = resp_len;
2476out_free:
2477 if (localpage)
2478 __free_page(localpage);
2479 return ret;
2480}
2481
2482static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2483{
2484 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002485 int ret;
2486
2487 if (!nfs4_server_supports_acls(server))
2488 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002489 ret = nfs_revalidate_inode(server, inode);
2490 if (ret < 0)
2491 return ret;
2492 ret = nfs4_read_cached_acl(inode, buf, buflen);
2493 if (ret != -ENOENT)
2494 return ret;
2495 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002496}
2497
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002498static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2499{
2500 struct nfs_server *server = NFS_SERVER(inode);
2501 struct page *pages[NFS4ACL_MAXPAGES];
2502 struct nfs_setaclargs arg = {
2503 .fh = NFS_FH(inode),
2504 .acl_pages = pages,
2505 .acl_len = buflen,
2506 };
2507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2509 .rpc_argp = &arg,
2510 .rpc_resp = NULL,
2511 };
2512 int ret;
2513
2514 if (!nfs4_server_supports_acls(server))
2515 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002516 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002517 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2518 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002519 if (ret == 0)
2520 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002521 return ret;
2522}
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002525nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 struct nfs4_client *clp = server->nfs4_state;
2528
2529 if (!clp || task->tk_status >= 0)
2530 return 0;
2531 switch(task->tk_status) {
2532 case -NFS4ERR_STALE_CLIENTID:
2533 case -NFS4ERR_STALE_STATEID:
2534 case -NFS4ERR_EXPIRED:
2535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2536 nfs4_schedule_state_recovery(clp);
2537 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2538 rpc_wake_up_task(task);
2539 task->tk_status = 0;
2540 return -EAGAIN;
2541 case -NFS4ERR_GRACE:
2542 case -NFS4ERR_DELAY:
2543 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2544 task->tk_status = 0;
2545 return -EAGAIN;
2546 case -NFS4ERR_OLD_STATEID:
2547 task->tk_status = 0;
2548 return -EAGAIN;
2549 }
2550 task->tk_status = nfs4_map_errors(task->tk_status);
2551 return 0;
2552}
2553
2554static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2555{
2556 DEFINE_WAIT(wait);
2557 sigset_t oldset;
2558 int interruptible, res = 0;
2559
2560 might_sleep();
2561
2562 rpc_clnt_sigmask(clnt, &oldset);
2563 interruptible = TASK_UNINTERRUPTIBLE;
2564 if (clnt->cl_intr)
2565 interruptible = TASK_INTERRUPTIBLE;
2566 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2567 nfs4_schedule_state_recovery(clp);
2568 if (clnt->cl_intr && signalled())
2569 res = -ERESTARTSYS;
2570 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2571 schedule();
2572 finish_wait(&clp->cl_waitq, &wait);
2573 rpc_clnt_sigunmask(clnt, &oldset);
2574 return res;
2575}
2576
2577static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2578{
2579 sigset_t oldset;
2580 int res = 0;
2581
2582 might_sleep();
2583
2584 if (*timeout <= 0)
2585 *timeout = NFS4_POLL_RETRY_MIN;
2586 if (*timeout > NFS4_POLL_RETRY_MAX)
2587 *timeout = NFS4_POLL_RETRY_MAX;
2588 rpc_clnt_sigmask(clnt, &oldset);
2589 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002590 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (signalled())
2592 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002593 } else
2594 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 rpc_clnt_sigunmask(clnt, &oldset);
2596 *timeout <<= 1;
2597 return res;
2598}
2599
2600/* This is the error handling routine for processes that are allowed
2601 * to sleep.
2602 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002603int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct nfs4_client *clp = server->nfs4_state;
2606 int ret = errorcode;
2607
2608 exception->retry = 0;
2609 switch(errorcode) {
2610 case 0:
2611 return 0;
2612 case -NFS4ERR_STALE_CLIENTID:
2613 case -NFS4ERR_STALE_STATEID:
2614 case -NFS4ERR_EXPIRED:
2615 ret = nfs4_wait_clnt_recover(server->client, clp);
2616 if (ret == 0)
2617 exception->retry = 1;
2618 break;
2619 case -NFS4ERR_GRACE:
2620 case -NFS4ERR_DELAY:
2621 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002622 if (ret != 0)
2623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002625 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627 /* We failed to handle the error */
2628 return nfs4_map_errors(ret);
2629}
2630
2631int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2632{
2633 nfs4_verifier sc_verifier;
2634 struct nfs4_setclientid setclientid = {
2635 .sc_verifier = &sc_verifier,
2636 .sc_prog = program,
2637 };
2638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2640 .rpc_argp = &setclientid,
2641 .rpc_resp = clp,
2642 .rpc_cred = clp->cl_cred,
2643 };
2644 u32 *p;
2645 int loop = 0;
2646 int status;
2647
2648 p = (u32*)sc_verifier.data;
2649 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2650 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2651
2652 for(;;) {
2653 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2654 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2655 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2656 clp->cl_cred->cr_ops->cr_name,
2657 clp->cl_id_uniquifier);
2658 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2659 sizeof(setclientid.sc_netid), "tcp");
2660 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2661 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2662 clp->cl_ipaddr, port >> 8, port & 255);
2663
2664 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2665 if (status != -NFS4ERR_CLID_INUSE)
2666 break;
2667 if (signalled())
2668 break;
2669 if (loop++ & 1)
2670 ssleep(clp->cl_lease_time + 1);
2671 else
2672 if (++clp->cl_id_uniquifier == 0)
2673 break;
2674 }
2675 return status;
2676}
2677
2678int
2679nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2680{
2681 struct nfs_fsinfo fsinfo;
2682 struct rpc_message msg = {
2683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2684 .rpc_argp = clp,
2685 .rpc_resp = &fsinfo,
2686 .rpc_cred = clp->cl_cred,
2687 };
2688 unsigned long now;
2689 int status;
2690
2691 now = jiffies;
2692 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2693 if (status == 0) {
2694 spin_lock(&clp->cl_lock);
2695 clp->cl_lease_time = fsinfo.lease_time * HZ;
2696 clp->cl_last_renewal = now;
2697 spin_unlock(&clp->cl_lock);
2698 }
2699 return status;
2700}
2701
2702static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2703{
2704 struct nfs4_delegreturnargs args = {
2705 .fhandle = NFS_FH(inode),
2706 .stateid = stateid,
2707 };
2708 struct rpc_message msg = {
2709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2710 .rpc_argp = &args,
2711 .rpc_cred = cred,
2712 };
2713
2714 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2715}
2716
2717int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2718{
2719 struct nfs_server *server = NFS_SERVER(inode);
2720 struct nfs4_exception exception = { };
2721 int err;
2722 do {
2723 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2724 switch (err) {
2725 case -NFS4ERR_STALE_STATEID:
2726 case -NFS4ERR_EXPIRED:
2727 nfs4_schedule_state_recovery(server->nfs4_state);
2728 case 0:
2729 return 0;
2730 }
2731 err = nfs4_handle_exception(server, err, &exception);
2732 } while (exception.retry);
2733 return err;
2734}
2735
2736#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2737#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2738
2739/*
2740 * sleep, with exponential backoff, and retry the LOCK operation.
2741 */
2742static unsigned long
2743nfs4_set_lock_task_retry(unsigned long timeout)
2744{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002745 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 timeout <<= 1;
2747 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2748 return NFS4_LOCK_MAXTIMEOUT;
2749 return timeout;
2750}
2751
2752static inline int
2753nfs4_lck_type(int cmd, struct file_lock *request)
2754{
2755 /* set lock type */
2756 switch (request->fl_type) {
2757 case F_RDLCK:
2758 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2759 case F_WRLCK:
2760 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2761 case F_UNLCK:
2762 return NFS4_WRITE_LT;
2763 }
2764 BUG();
2765 return 0;
2766}
2767
2768static inline uint64_t
2769nfs4_lck_length(struct file_lock *request)
2770{
2771 if (request->fl_end == OFFSET_MAX)
2772 return ~(uint64_t)0;
2773 return request->fl_end - request->fl_start + 1;
2774}
2775
2776static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2777{
2778 struct inode *inode = state->inode;
2779 struct nfs_server *server = NFS_SERVER(inode);
2780 struct nfs4_client *clp = server->nfs4_state;
2781 struct nfs_lockargs arg = {
2782 .fh = NFS_FH(inode),
2783 .type = nfs4_lck_type(cmd, request),
2784 .offset = request->fl_start,
2785 .length = nfs4_lck_length(request),
2786 };
2787 struct nfs_lockres res = {
2788 .server = server,
2789 };
2790 struct rpc_message msg = {
2791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2792 .rpc_argp = &arg,
2793 .rpc_resp = &res,
2794 .rpc_cred = state->owner->so_cred,
2795 };
2796 struct nfs_lowner nlo;
2797 struct nfs4_lock_state *lsp;
2798 int status;
2799
2800 down_read(&clp->cl_sem);
2801 nlo.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002802 status = nfs4_set_lock_state(state, request);
2803 if (status != 0)
2804 goto out;
2805 lsp = request->fl_u.nfs4_fl.owner;
2806 nlo.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 arg.u.lockt = &nlo;
2808 status = rpc_call_sync(server->client, &msg, 0);
2809 if (!status) {
2810 request->fl_type = F_UNLCK;
2811 } else if (status == -NFS4ERR_DENIED) {
2812 int64_t len, start, end;
2813 start = res.u.denied.offset;
2814 len = res.u.denied.length;
2815 end = start + len - 1;
2816 if (end < 0 || len == 0)
2817 request->fl_end = OFFSET_MAX;
2818 else
2819 request->fl_end = (loff_t)end;
2820 request->fl_start = (loff_t)start;
2821 request->fl_type = F_WRLCK;
2822 if (res.u.denied.type & 1)
2823 request->fl_type = F_RDLCK;
2824 request->fl_pid = 0;
2825 status = 0;
2826 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002827out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 up_read(&clp->cl_sem);
2829 return status;
2830}
2831
2832static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2833{
2834 struct nfs4_exception exception = { };
2835 int err;
2836
2837 do {
2838 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2839 _nfs4_proc_getlk(state, cmd, request),
2840 &exception);
2841 } while (exception.retry);
2842 return err;
2843}
2844
2845static int do_vfs_lock(struct file *file, struct file_lock *fl)
2846{
2847 int res = 0;
2848 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2849 case FL_POSIX:
2850 res = posix_lock_file_wait(file, fl);
2851 break;
2852 case FL_FLOCK:
2853 res = flock_lock_file_wait(file, fl);
2854 break;
2855 default:
2856 BUG();
2857 }
2858 if (res < 0)
2859 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2860 __FUNCTION__);
2861 return res;
2862}
2863
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002864struct nfs4_unlockdata {
2865 struct nfs_lockargs arg;
2866 struct nfs_locku_opargs luargs;
2867 struct nfs_lockres res;
2868 struct nfs4_lock_state *lsp;
2869 struct nfs_open_context *ctx;
2870 atomic_t refcount;
2871 struct completion completion;
2872};
2873
2874static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002876 if (atomic_dec_and_test(&calldata->refcount)) {
2877 nfs_free_seqid(calldata->luargs.seqid);
2878 nfs4_put_lock_state(calldata->lsp);
2879 put_nfs_open_context(calldata->ctx);
2880 kfree(calldata);
2881 }
2882}
2883
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002884static void nfs4_locku_complete(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002885{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002886 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002887 complete(&calldata->completion);
2888 nfs4_locku_release_calldata(calldata);
2889}
2890
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002891static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002892{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002893 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002894
2895 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2896 switch (task->tk_status) {
2897 case 0:
2898 memcpy(calldata->lsp->ls_stateid.data,
2899 calldata->res.u.stateid.data,
2900 sizeof(calldata->lsp->ls_stateid.data));
2901 break;
2902 case -NFS4ERR_STALE_STATEID:
2903 case -NFS4ERR_EXPIRED:
2904 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2905 break;
2906 default:
2907 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2908 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002909 }
2910 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002911}
2912
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002913static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002914{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002915 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 struct rpc_message msg = {
2917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002918 .rpc_argp = &calldata->arg,
2919 .rpc_resp = &calldata->res,
2920 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 };
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002922 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002924 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2925 if (status != 0)
2926 return;
2927 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002928 /* Note: exit _without_ running nfs4_locku_done */
2929 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002930 return;
2931 }
2932 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002935static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002936 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002937 .rpc_call_done = nfs4_locku_done,
2938 .rpc_release = nfs4_locku_complete,
2939};
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2942{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002943 struct nfs4_unlockdata *calldata;
2944 struct inode *inode = state->inode;
2945 struct nfs_server *server = NFS_SERVER(inode);
2946 struct nfs4_lock_state *lsp;
2947 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05002949 /* Is this a delegated lock? */
2950 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
2951 return do_vfs_lock(request->fl_file, request);
2952
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002953 status = nfs4_set_lock_state(state, request);
2954 if (status != 0)
2955 return status;
2956 lsp = request->fl_u.nfs4_fl.owner;
2957 /* We might have lost the locks! */
2958 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2959 return 0;
2960 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2961 if (calldata == NULL)
2962 return -ENOMEM;
2963 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2964 if (calldata->luargs.seqid == NULL) {
2965 kfree(calldata);
2966 return -ENOMEM;
2967 }
2968 calldata->luargs.stateid = &lsp->ls_stateid;
2969 calldata->arg.fh = NFS_FH(inode);
2970 calldata->arg.type = nfs4_lck_type(cmd, request);
2971 calldata->arg.offset = request->fl_start;
2972 calldata->arg.length = nfs4_lck_length(request);
2973 calldata->arg.u.locku = &calldata->luargs;
2974 calldata->res.server = server;
2975 calldata->lsp = lsp;
2976 atomic_inc(&lsp->ls_count);
2977
2978 /* Ensure we don't close file until we're done freeing locks! */
2979 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2980
2981 atomic_set(&calldata->refcount, 2);
2982 init_completion(&calldata->completion);
2983
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002984 status = nfs4_call_async(NFS_SERVER(inode)->client, &nfs4_locku_ops, calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002985 if (status == 0)
2986 wait_for_completion_interruptible(&calldata->completion);
2987 do_vfs_lock(request->fl_file, request);
2988 nfs4_locku_release_calldata(calldata);
2989 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2993{
2994 struct inode *inode = state->inode;
2995 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002996 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06735b32005-10-18 14:20:15 -07002997 struct nfs_lock_opargs largs = {
2998 .lock_stateid = &lsp->ls_stateid,
2999 .open_stateid = &state->stateid,
3000 .lock_owner = {
3001 .clientid = server->nfs4_state->cl_clientid,
3002 .id = lsp->ls_id,
3003 },
3004 .reclaim = reclaim,
3005 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct nfs_lockargs arg = {
3007 .fh = NFS_FH(inode),
3008 .type = nfs4_lck_type(cmd, request),
3009 .offset = request->fl_start,
3010 .length = nfs4_lck_length(request),
Trond Myklebust06735b32005-10-18 14:20:15 -07003011 .u = {
3012 .lock = &largs,
3013 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 };
3015 struct nfs_lockres res = {
3016 .server = server,
3017 };
3018 struct rpc_message msg = {
3019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3020 .rpc_argp = &arg,
3021 .rpc_resp = &res,
3022 .rpc_cred = state->owner->so_cred,
3023 };
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003024 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Trond Myklebust06735b32005-10-18 14:20:15 -07003026 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3027 if (largs.lock_seqid == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003028 return -ENOMEM;
3029 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_state_owner *owner = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Trond Myklebust06735b32005-10-18 14:20:15 -07003032 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3033 if (largs.open_seqid == NULL)
3034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 largs.new_lock_owner = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust06735b32005-10-18 14:20:15 -07003037 /* increment open seqid on success, and seqid mutating errors */
3038 if (largs.new_lock_owner != 0) {
3039 nfs_increment_open_seqid(status, largs.open_seqid);
3040 if (status == 0)
3041 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3042 }
3043 nfs_free_seqid(largs.open_seqid);
3044 } else
3045 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3046 /* increment lock seqid on success, and seqid mutating errors*/
3047 nfs_increment_lock_seqid(status, largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 /* save the returned stateid. */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003049 if (status == 0) {
Trond Myklebust06735b32005-10-18 14:20:15 -07003050 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3051 sizeof(lsp->ls_stateid.data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003052 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3053 } else if (status == -NFS4ERR_DENIED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 status = -EAGAIN;
Trond Myklebust06735b32005-10-18 14:20:15 -07003055out:
3056 nfs_free_seqid(largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
3060static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3061{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003062 struct nfs_server *server = NFS_SERVER(state->inode);
3063 struct nfs4_exception exception = { };
3064 int err;
3065
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003066 /* Cache the lock if possible... */
3067 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3068 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003069 do {
3070 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3071 if (err != -NFS4ERR_DELAY)
3072 break;
3073 nfs4_handle_exception(server, err, &exception);
3074 } while (exception.retry);
3075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
3078static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3079{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003080 struct nfs_server *server = NFS_SERVER(state->inode);
3081 struct nfs4_exception exception = { };
3082 int err;
3083
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003084 err = nfs4_set_lock_state(state, request);
3085 if (err != 0)
3086 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003087 do {
3088 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3089 if (err != -NFS4ERR_DELAY)
3090 break;
3091 nfs4_handle_exception(server, err, &exception);
3092 } while (exception.retry);
3093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
3096static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3097{
3098 struct nfs4_client *clp = state->owner->so_client;
3099 int status;
3100
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003101 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003102 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003103 /* Yes: cache locks! */
3104 status = do_vfs_lock(request->fl_file, request);
3105 /* ...but avoid races with delegation recall... */
3106 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003107 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
Trond Myklebustff604062005-11-25 17:10:01 -05003109 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003110 status = nfs4_set_lock_state(state, request);
3111 if (status != 0)
3112 goto out;
3113 status = _nfs4_do_setlk(state, cmd, request, 0);
3114 if (status != 0)
3115 goto out;
3116 /* Note: we always want to sleep here! */
3117 request->fl_flags |= FL_SLEEP;
3118 if (do_vfs_lock(request->fl_file, request) < 0)
3119 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 up_read(&clp->cl_sem);
3122 return status;
3123}
3124
3125static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
3129
3130 do {
3131 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3132 _nfs4_proc_setlk(state, cmd, request),
3133 &exception);
3134 } while (exception.retry);
3135 return err;
3136}
3137
3138static int
3139nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3140{
3141 struct nfs_open_context *ctx;
3142 struct nfs4_state *state;
3143 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3144 int status;
3145
3146 /* verify open state */
3147 ctx = (struct nfs_open_context *)filp->private_data;
3148 state = ctx->state;
3149
3150 if (request->fl_start < 0 || request->fl_end < 0)
3151 return -EINVAL;
3152
3153 if (IS_GETLK(cmd))
3154 return nfs4_proc_getlk(state, F_GETLK, request);
3155
3156 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3157 return -EINVAL;
3158
3159 if (request->fl_type == F_UNLCK)
3160 return nfs4_proc_unlck(state, cmd, request);
3161
3162 do {
3163 status = nfs4_proc_setlk(state, cmd, request);
3164 if ((status != -EAGAIN) || IS_SETLK(cmd))
3165 break;
3166 timeout = nfs4_set_lock_task_retry(timeout);
3167 status = -ERESTARTSYS;
3168 if (signalled())
3169 break;
3170 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return status;
3172}
3173
Trond Myklebust888e6942005-11-04 15:38:11 -05003174int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3175{
3176 struct nfs_server *server = NFS_SERVER(state->inode);
3177 struct nfs4_exception exception = { };
3178 int err;
3179
3180 err = nfs4_set_lock_state(state, fl);
3181 if (err != 0)
3182 goto out;
3183 do {
3184 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3185 if (err != -NFS4ERR_DELAY)
3186 break;
3187 err = nfs4_handle_exception(server, err, &exception);
3188 } while (exception.retry);
3189out:
3190 return err;
3191}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003192
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003193#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3194
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003195int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3196 size_t buflen, int flags)
3197{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003198 struct inode *inode = dentry->d_inode;
3199
3200 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3201 return -EOPNOTSUPP;
3202
3203 if (!S_ISREG(inode->i_mode) &&
3204 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3205 return -EPERM;
3206
3207 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003208}
3209
3210/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3211 * and that's what we'll do for e.g. user attributes that haven't been set.
3212 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3213 * attributes in kernel-managed attribute namespaces. */
3214ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3215 size_t buflen)
3216{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003217 struct inode *inode = dentry->d_inode;
3218
3219 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3220 return -EOPNOTSUPP;
3221
3222 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003223}
3224
3225ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3226{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003227 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003228
3229 if (buf && buflen < len)
3230 return -ERANGE;
3231 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003232 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3233 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003234}
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3237 .recover_open = nfs4_open_reclaim,
3238 .recover_lock = nfs4_lock_reclaim,
3239};
3240
3241struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3242 .recover_open = nfs4_open_expired,
3243 .recover_lock = nfs4_lock_expired,
3244};
3245
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003246static struct inode_operations nfs4_file_inode_operations = {
3247 .permission = nfs_permission,
3248 .getattr = nfs_getattr,
3249 .setattr = nfs_setattr,
3250 .getxattr = nfs4_getxattr,
3251 .setxattr = nfs4_setxattr,
3252 .listxattr = nfs4_listxattr,
3253};
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255struct nfs_rpc_ops nfs_v4_clientops = {
3256 .version = 4, /* protocol version */
3257 .dentry_ops = &nfs4_dentry_operations,
3258 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003259 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 .getroot = nfs4_proc_get_root,
3261 .getattr = nfs4_proc_getattr,
3262 .setattr = nfs4_proc_setattr,
3263 .lookup = nfs4_proc_lookup,
3264 .access = nfs4_proc_access,
3265 .readlink = nfs4_proc_readlink,
3266 .read = nfs4_proc_read,
3267 .write = nfs4_proc_write,
3268 .commit = nfs4_proc_commit,
3269 .create = nfs4_proc_create,
3270 .remove = nfs4_proc_remove,
3271 .unlink_setup = nfs4_proc_unlink_setup,
3272 .unlink_done = nfs4_proc_unlink_done,
3273 .rename = nfs4_proc_rename,
3274 .link = nfs4_proc_link,
3275 .symlink = nfs4_proc_symlink,
3276 .mkdir = nfs4_proc_mkdir,
3277 .rmdir = nfs4_proc_remove,
3278 .readdir = nfs4_proc_readdir,
3279 .mknod = nfs4_proc_mknod,
3280 .statfs = nfs4_proc_statfs,
3281 .fsinfo = nfs4_proc_fsinfo,
3282 .pathconf = nfs4_proc_pathconf,
3283 .decode_dirent = nfs4_decode_dirent,
3284 .read_setup = nfs4_proc_read_setup,
3285 .write_setup = nfs4_proc_write_setup,
3286 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003287 .file_open = nfs_open,
3288 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003290 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291};
3292
3293/*
3294 * Local variables:
3295 * c-basic-offset: 8
3296 * End:
3297 */