blob: e6a9322a321f4eb4801b7172c92bf4ec8c140292 [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 */
198static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
199 rpc_action tk_exit, void *calldata)
200{
201 struct rpc_task *task;
202
203 if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
204 return -ENOMEM;
205
206 task->tk_calldata = calldata;
207 task->tk_action = tk_begin;
208 rpc_execute(task);
209 return 0;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
213{
214 struct inode *inode = state->inode;
215
216 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500217 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700218 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500220 memcpy(&state->stateid, stateid, sizeof(state->stateid));
221 if ((open_flags & FMODE_WRITE))
222 state->nwriters++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (open_flags & FMODE_READ)
224 state->nreaders++;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500225 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700227 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/*
231 * OPEN_RECLAIM:
232 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
235{
236 struct inode *inode = state->inode;
237 struct nfs_server *server = NFS_SERVER(inode);
238 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
239 struct nfs_openargs o_arg = {
240 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .id = sp->so_id,
242 .open_flags = state->state,
243 .clientid = server->nfs4_state->cl_clientid,
244 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
245 .bitmask = server->attr_bitmask,
246 };
247 struct nfs_openres o_res = {
248 .server = server, /* Grrr */
249 };
250 struct rpc_message msg = {
251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
252 .rpc_argp = &o_arg,
253 .rpc_resp = &o_res,
254 .rpc_cred = sp->so_cred,
255 };
256 int status;
257
258 if (delegation != NULL) {
259 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
260 memcpy(&state->stateid, &delegation->stateid,
261 sizeof(state->stateid));
262 set_bit(NFS_DELEGATED_STATE, &state->flags);
263 return 0;
264 }
265 o_arg.u.delegation_type = delegation->type;
266 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700267 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
268 if (o_arg.seqid == NULL)
269 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700271 /* Confirm the sequence as being established */
272 nfs_confirm_seqid(&sp->so_seqid, status);
273 nfs_increment_open_seqid(status, o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (status == 0) {
275 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
276 if (o_res.delegation_type != 0) {
277 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
278 /* Did the server issue an immediate delegation recall? */
279 if (o_res.do_recall)
280 nfs_async_inode_return_delegation(inode, &o_res.stateid);
281 }
282 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700283 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 clear_bit(NFS_DELEGATED_STATE, &state->flags);
285 /* Ensure we update the inode attributes */
286 NFS_CACHEINV(inode);
287 return status;
288}
289
290static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
291{
292 struct nfs_server *server = NFS_SERVER(state->inode);
293 struct nfs4_exception exception = { };
294 int err;
295 do {
296 err = _nfs4_open_reclaim(sp, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000297 if (err != -NFS4ERR_DELAY)
298 break;
299 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } while (exception.retry);
301 return err;
302}
303
304static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
305{
306 struct nfs4_state_owner *sp = state->owner;
307 struct inode *inode = dentry->d_inode;
308 struct nfs_server *server = NFS_SERVER(inode);
309 struct dentry *parent = dget_parent(dentry);
310 struct nfs_openargs arg = {
311 .fh = NFS_FH(parent->d_inode),
312 .clientid = server->nfs4_state->cl_clientid,
313 .name = &dentry->d_name,
314 .id = sp->so_id,
315 .server = server,
316 .bitmask = server->attr_bitmask,
317 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
318 };
319 struct nfs_openres res = {
320 .server = server,
321 };
322 struct rpc_message msg = {
323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
324 .rpc_argp = &arg,
325 .rpc_resp = &res,
326 .rpc_cred = sp->so_cred,
327 };
328 int status = 0;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
331 goto out;
332 if (state->state == 0)
333 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700334 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
335 status = -ENOMEM;
336 if (arg.seqid == NULL)
337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 arg.open_flags = state->state;
339 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
340 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700341 nfs_increment_open_seqid(status, arg.seqid);
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700342 if (status != 0)
343 goto out_free;
344 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
345 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
346 sp, &res.stateid, arg.seqid);
347 if (status != 0)
348 goto out_free;
349 }
350 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (status >= 0) {
352 memcpy(state->stateid.data, res.stateid.data,
353 sizeof(state->stateid.data));
354 clear_bit(NFS_DELEGATED_STATE, &state->flags);
355 }
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700356out_free:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700357 nfs_free_seqid(arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dput(parent);
360 return status;
361}
362
363int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
364{
365 struct nfs4_exception exception = { };
366 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
367 int err;
368 do {
369 err = _nfs4_open_delegation_recall(dentry, state);
370 switch (err) {
371 case 0:
372 return err;
373 case -NFS4ERR_STALE_CLIENTID:
374 case -NFS4ERR_STALE_STATEID:
375 case -NFS4ERR_EXPIRED:
376 /* Don't recall a delegation if it was lost */
377 nfs4_schedule_state_recovery(server->nfs4_state);
378 return err;
379 }
380 err = nfs4_handle_exception(server, err, &exception);
381 } while (exception.retry);
382 return err;
383}
384
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700385static 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 -0700386{
387 struct nfs_open_confirmargs arg = {
388 .fh = fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700389 .seqid = seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .stateid = *stateid,
391 };
392 struct nfs_open_confirmres res;
393 struct rpc_message msg = {
394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
395 .rpc_argp = &arg,
396 .rpc_resp = &res,
397 .rpc_cred = sp->so_cred,
398 };
399 int status;
400
401 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700402 /* Confirm the sequence as being established */
403 nfs_confirm_seqid(&sp->so_seqid, status);
404 nfs_increment_open_seqid(status, seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (status >= 0)
406 memcpy(stateid, &res.stateid, sizeof(*stateid));
407 return status;
408}
409
410static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
411{
412 struct nfs_server *server = NFS_SERVER(dir);
413 struct rpc_message msg = {
414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
415 .rpc_argp = o_arg,
416 .rpc_resp = o_res,
417 .rpc_cred = sp->so_cred,
418 };
419 int status;
420
421 /* Update sequence id. The caller must serialize! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 o_arg->id = sp->so_id;
423 o_arg->clientid = sp->so_client->cl_clientid;
424
425 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700426 if (status == 0) {
427 /* OPEN on anything except a regular file is disallowed in NFSv4 */
428 switch (o_res->f_attr->mode & S_IFMT) {
429 case S_IFREG:
430 break;
431 case S_IFLNK:
432 status = -ELOOP;
433 break;
434 case S_IFDIR:
435 status = -EISDIR;
436 break;
437 default:
438 status = -ENOTDIR;
439 }
440 }
441
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700442 nfs_increment_open_seqid(status, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (status != 0)
444 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400445 if (o_arg->open_flags & O_CREAT) {
446 update_changeattr(dir, &o_res->cinfo);
447 nfs_post_op_update_inode(dir, o_res->dir_attr);
448 } else
449 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
451 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700452 sp, &o_res->stateid, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (status != 0)
454 goto out;
455 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700456 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
458 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
459out:
460 return status;
461}
462
463static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
464{
465 struct nfs_access_entry cache;
466 int mask = 0;
467 int status;
468
469 if (openflags & FMODE_READ)
470 mask |= MAY_READ;
471 if (openflags & FMODE_WRITE)
472 mask |= MAY_WRITE;
473 status = nfs_access_get_cached(inode, cred, &cache);
474 if (status == 0)
475 goto out;
476
477 /* Be clever: ask server to check for all possible rights */
478 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
479 cache.cred = cred;
480 cache.jiffies = jiffies;
481 status = _nfs4_proc_access(inode, &cache);
482 if (status != 0)
483 return status;
484 nfs_access_add_cache(inode, &cache);
485out:
486 if ((cache.mask & mask) == mask)
487 return 0;
488 return -EACCES;
489}
490
491/*
492 * OPEN_EXPIRED:
493 * reclaim state on the server after a network partition.
494 * Assumes caller holds the appropriate lock
495 */
496static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
497{
498 struct dentry *parent = dget_parent(dentry);
499 struct inode *dir = parent->d_inode;
500 struct inode *inode = state->inode;
501 struct nfs_server *server = NFS_SERVER(dir);
502 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400503 struct nfs_fattr f_attr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct nfs_openargs o_arg = {
505 .fh = NFS_FH(dir),
506 .open_flags = state->state,
507 .name = &dentry->d_name,
508 .bitmask = server->attr_bitmask,
509 .claim = NFS4_OPEN_CLAIM_NULL,
510 };
511 struct nfs_openres o_res = {
512 .f_attr = &f_attr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400513 .dir_attr = &dir_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .server = server,
515 };
516 int status = 0;
517
518 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
519 status = _nfs4_do_access(inode, sp->so_cred, state->state);
520 if (status < 0)
521 goto out;
522 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
523 set_bit(NFS_DELEGATED_STATE, &state->flags);
524 goto out;
525 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700526 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
527 status = -ENOMEM;
528 if (o_arg.seqid == NULL)
529 goto out;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400530 nfs_fattr_init(&f_attr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400531 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
533 if (status != 0)
534 goto out_nodeleg;
535 /* Check if files differ */
536 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
537 goto out_stale;
538 /* Has the file handle changed? */
539 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
540 /* Verify if the change attributes are the same */
541 if (f_attr.change_attr != NFS_I(inode)->change_attr)
542 goto out_stale;
543 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
544 goto out_stale;
545 /* Lets just pretend that this is the same file */
546 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
547 NFS_I(inode)->fileid = f_attr.fileid;
548 }
549 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
550 if (o_res.delegation_type != 0) {
551 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
552 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
553 else
554 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
555 }
556out_nodeleg:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700557 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 clear_bit(NFS_DELEGATED_STATE, &state->flags);
559out:
560 dput(parent);
561 return status;
562out_stale:
563 status = -ESTALE;
564 /* Invalidate the state owner so we don't ever use it again */
565 nfs4_drop_state_owner(sp);
566 d_drop(dentry);
567 /* Should we be trying to close that stateid? */
568 goto out_nodeleg;
569}
570
Trond Myklebust202b50d2005-06-22 17:16:29 +0000571static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
572{
573 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
574 struct nfs4_exception exception = { };
575 int err;
576
577 do {
578 err = _nfs4_open_expired(sp, state, dentry);
579 if (err == -NFS4ERR_DELAY)
580 nfs4_handle_exception(server, err, &exception);
581 } while (exception.retry);
582 return err;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
586{
587 struct nfs_inode *nfsi = NFS_I(state->inode);
588 struct nfs_open_context *ctx;
589 int status;
590
591 spin_lock(&state->inode->i_lock);
592 list_for_each_entry(ctx, &nfsi->open_files, list) {
593 if (ctx->state != state)
594 continue;
595 get_nfs_open_context(ctx);
596 spin_unlock(&state->inode->i_lock);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000597 status = nfs4_do_open_expired(sp, state, ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 put_nfs_open_context(ctx);
599 return status;
600 }
601 spin_unlock(&state->inode->i_lock);
602 return -ENOENT;
603}
604
605/*
606 * Returns an nfs4_state + an extra reference to the inode
607 */
608static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
609{
610 struct nfs_delegation *delegation;
611 struct nfs_server *server = NFS_SERVER(inode);
612 struct nfs4_client *clp = server->nfs4_state;
613 struct nfs_inode *nfsi = NFS_I(inode);
614 struct nfs4_state_owner *sp = NULL;
615 struct nfs4_state *state = NULL;
616 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
617 int err;
618
619 /* Protect against reboot recovery - NOTE ORDER! */
620 down_read(&clp->cl_sem);
621 /* Protect against delegation recall */
622 down_read(&nfsi->rwsem);
623 delegation = NFS_I(inode)->delegation;
624 err = -ENOENT;
625 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
626 goto out_err;
627 err = -ENOMEM;
628 if (!(sp = nfs4_get_state_owner(server, cred))) {
629 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
630 goto out_err;
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 state = nfs4_get_open_state(inode, sp);
633 if (state == NULL)
634 goto out_err;
635
636 err = -ENOENT;
637 if ((state->state & open_flags) == open_flags) {
638 spin_lock(&inode->i_lock);
639 if (open_flags & FMODE_READ)
640 state->nreaders++;
641 if (open_flags & FMODE_WRITE)
642 state->nwriters++;
643 spin_unlock(&inode->i_lock);
644 goto out_ok;
645 } else if (state->state != 0)
646 goto out_err;
647
648 lock_kernel();
649 err = _nfs4_do_access(inode, cred, open_flags);
650 unlock_kernel();
651 if (err != 0)
652 goto out_err;
653 set_bit(NFS_DELEGATED_STATE, &state->flags);
654 update_open_stateid(state, &delegation->stateid, open_flags);
655out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 nfs4_put_state_owner(sp);
657 up_read(&nfsi->rwsem);
658 up_read(&clp->cl_sem);
659 igrab(inode);
660 *res = state;
661 return 0;
662out_err:
663 if (sp != NULL) {
664 if (state != NULL)
665 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 nfs4_put_state_owner(sp);
667 }
668 up_read(&nfsi->rwsem);
669 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700670 if (err != -EACCES)
671 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return err;
673}
674
675static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
676{
677 struct nfs4_exception exception = { };
678 struct nfs4_state *res;
679 int err;
680
681 do {
682 err = _nfs4_open_delegated(inode, flags, cred, &res);
683 if (err == 0)
684 break;
685 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
686 err, &exception));
687 } while (exception.retry);
688 return res;
689}
690
691/*
692 * Returns an nfs4_state + an referenced inode
693 */
694static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
695{
696 struct nfs4_state_owner *sp;
697 struct nfs4_state *state = NULL;
698 struct nfs_server *server = NFS_SERVER(dir);
699 struct nfs4_client *clp = server->nfs4_state;
700 struct inode *inode = NULL;
701 int status;
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400702 struct nfs_fattr f_attr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct nfs_openargs o_arg = {
704 .fh = NFS_FH(dir),
705 .open_flags = flags,
706 .name = &dentry->d_name,
707 .server = server,
708 .bitmask = server->attr_bitmask,
709 .claim = NFS4_OPEN_CLAIM_NULL,
710 };
711 struct nfs_openres o_res = {
712 .f_attr = &f_attr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400713 .dir_attr = &dir_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 .server = server,
715 };
716
717 /* Protect against reboot recovery conflicts */
718 down_read(&clp->cl_sem);
719 status = -ENOMEM;
720 if (!(sp = nfs4_get_state_owner(server, cred))) {
721 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
722 goto out_err;
723 }
724 if (flags & O_EXCL) {
725 u32 *p = (u32 *) o_arg.u.verifier.data;
726 p[0] = jiffies;
727 p[1] = current->pid;
728 } else
729 o_arg.u.attrs = sattr;
730 /* Serialization for the sequence id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700732 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
733 if (o_arg.seqid == NULL)
734 return -ENOMEM;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400735 nfs_fattr_init(&f_attr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400736 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
738 if (status != 0)
739 goto out_err;
740
741 status = -ENOMEM;
742 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
743 if (!inode)
744 goto out_err;
745 state = nfs4_get_open_state(inode, sp);
746 if (!state)
747 goto out_err;
748 update_open_stateid(state, &o_res.stateid, flags);
749 if (o_res.delegation_type != 0)
750 nfs_inode_set_delegation(inode, cred, &o_res);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700751 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 nfs4_put_state_owner(sp);
753 up_read(&clp->cl_sem);
754 *res = state;
755 return 0;
756out_err:
757 if (sp != NULL) {
758 if (state != NULL)
759 nfs4_put_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700760 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 nfs4_put_state_owner(sp);
762 }
763 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
764 up_read(&clp->cl_sem);
765 if (inode != NULL)
766 iput(inode);
767 *res = NULL;
768 return status;
769}
770
771
772static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
773{
774 struct nfs4_exception exception = { };
775 struct nfs4_state *res;
776 int status;
777
778 do {
779 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
780 if (status == 0)
781 break;
782 /* NOTE: BAD_SEQID means the server and client disagree about the
783 * book-keeping w.r.t. state-changing operations
784 * (OPEN/CLOSE/LOCK/LOCKU...)
785 * It is actually a sign of a bug on the client or on the server.
786 *
787 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700788 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * have unhashed the old state_owner for us, and that we can
790 * therefore safely retry using a new one. We should still warn
791 * the user though...
792 */
793 if (status == -NFS4ERR_BAD_SEQID) {
794 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
795 exception.retry = 1;
796 continue;
797 }
Trond Myklebust550f5742005-10-18 14:20:21 -0700798 /*
799 * BAD_STATEID on OPEN means that the server cancelled our
800 * state before it received the OPEN_CONFIRM.
801 * Recover by retrying the request as per the discussion
802 * on Page 181 of RFC3530.
803 */
804 if (status == -NFS4ERR_BAD_STATEID) {
805 exception.retry = 1;
806 continue;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
809 status, &exception));
810 } while (exception.retry);
811 return res;
812}
813
814static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
815 struct nfs_fh *fhandle, struct iattr *sattr,
816 struct nfs4_state *state)
817{
818 struct nfs_setattrargs arg = {
819 .fh = fhandle,
820 .iap = sattr,
821 .server = server,
822 .bitmask = server->attr_bitmask,
823 };
824 struct nfs_setattrres res = {
825 .fattr = fattr,
826 .server = server,
827 };
828 struct rpc_message msg = {
829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
830 .rpc_argp = &arg,
831 .rpc_resp = &res,
832 };
Trond Myklebust65e43082005-08-16 11:49:44 -0400833 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Trond Myklebust0e574af2005-10-27 22:12:38 -0400835 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000837 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000839 nfs4_copy_stateid(&arg.stateid, state, current->files);
840 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
842
Trond Myklebust65e43082005-08-16 11:49:44 -0400843 status = rpc_call_sync(server->client, &msg, 0);
844 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
848 struct nfs_fh *fhandle, struct iattr *sattr,
849 struct nfs4_state *state)
850{
851 struct nfs4_exception exception = { };
852 int err;
853 do {
854 err = nfs4_handle_exception(server,
855 _nfs4_do_setattr(server, fattr, fhandle, sattr,
856 state),
857 &exception);
858 } while (exception.retry);
859 return err;
860}
861
862struct nfs4_closedata {
863 struct inode *inode;
864 struct nfs4_state *state;
865 struct nfs_closeargs arg;
866 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -0400867 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868};
869
Trond Myklebust95121352005-10-18 14:20:12 -0700870static void nfs4_free_closedata(struct nfs4_closedata *calldata)
871{
872 struct nfs4_state *state = calldata->state;
873 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -0700874
875 nfs4_put_open_state(calldata->state);
876 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700877 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -0700878 kfree(calldata);
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static void nfs4_close_done(struct rpc_task *task)
882{
883 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
884 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct nfs_server *server = NFS_SERVER(calldata->inode);
886
887 /* hmm. we are done with the inode, and in the process of freeing
888 * the state_owner. we keep this around to process errors
889 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700890 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 switch (task->tk_status) {
892 case 0:
893 memcpy(&state->stateid, &calldata->res.stateid,
894 sizeof(state->stateid));
895 break;
896 case -NFS4ERR_STALE_STATEID:
897 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nfs4_schedule_state_recovery(server->nfs4_state);
899 break;
900 default:
901 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
902 rpc_restart_call(task);
903 return;
904 }
905 }
Trond Myklebust516a6af2005-10-27 22:12:41 -0400906 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -0700907 nfs4_free_closedata(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Trond Myklebust95121352005-10-18 14:20:12 -0700910static void nfs4_close_begin(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Trond Myklebust95121352005-10-18 14:20:12 -0700912 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
913 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct rpc_message msg = {
915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
916 .rpc_argp = &calldata->arg,
917 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -0700918 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 };
Trond Myklebust4cecb762005-11-04 15:32:58 -0500920 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -0700921 int status;
922
923 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
924 if (status != 0)
925 return;
Trond Myklebust95121352005-10-18 14:20:12 -0700926 /* Recalculate the new open mode in case someone reopened the file
927 * while we were waiting in line to be scheduled.
928 */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500929 spin_lock(&state->owner->so_lock);
930 spin_lock(&calldata->inode->i_lock);
931 mode = old_mode = state->state;
932 if (state->nreaders == 0)
933 mode &= ~FMODE_READ;
934 if (state->nwriters == 0)
935 mode &= ~FMODE_WRITE;
936 nfs4_state_set_mode_locked(state, mode);
937 spin_unlock(&calldata->inode->i_lock);
938 spin_unlock(&state->owner->so_lock);
939 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust95121352005-10-18 14:20:12 -0700940 nfs4_free_closedata(calldata);
941 task->tk_exit = NULL;
942 rpc_exit(task, 0);
943 return;
944 }
Trond Myklebust516a6af2005-10-27 22:12:41 -0400945 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -0700946 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -0700948 calldata->arg.open_flags = mode;
949 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*
953 * It is possible for data to be read/written from a mem-mapped file
954 * after the sys_close call (which hits the vfs layer as a flush).
955 * This means that we can't safely call nfsv4 close on a file until
956 * the inode is cleared. This in turn means that we are not good
957 * NFSv4 citizens - we do not indicate to the server to update the file's
958 * share state even when we are done with one of the three share
959 * stateid's in the inode.
960 *
961 * NOTE: Caller must be holding the sp->so_owner semaphore!
962 */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500963int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Trond Myklebust516a6af2005-10-27 22:12:41 -0400965 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -0700967 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Trond Myklebust95121352005-10-18 14:20:12 -0700969 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -0700971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 calldata->inode = inode;
973 calldata->state = state;
974 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -0700975 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700977 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700978 if (calldata->arg.seqid == NULL)
979 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -0400980 calldata->arg.bitmask = server->attr_bitmask;
981 calldata->res.fattr = &calldata->fattr;
982 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -0700983
Trond Myklebust516a6af2005-10-27 22:12:41 -0400984 status = nfs4_call_async(server->client, nfs4_close_begin,
Trond Myklebust95121352005-10-18 14:20:12 -0700985 nfs4_close_done, calldata);
986 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 Myklebust0e574af2005-10-27 22:12:38 -04001509 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 status = rpc_call_sync(server->client, &msg, rpcflags);
1511 dprintk("NFS reply write: %d\n", status);
1512 return status;
1513}
1514
1515static int nfs4_proc_write(struct nfs_write_data *wdata)
1516{
1517 struct nfs4_exception exception = { };
1518 int err;
1519 do {
1520 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1521 _nfs4_proc_write(wdata),
1522 &exception);
1523 } while (exception.retry);
1524 return err;
1525}
1526
1527static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1528{
1529 struct inode *inode = cdata->inode;
1530 struct nfs_fattr *fattr = cdata->res.fattr;
1531 struct nfs_server *server = NFS_SERVER(inode);
1532 struct rpc_message msg = {
1533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1534 .rpc_argp = &cdata->args,
1535 .rpc_resp = &cdata->res,
1536 .rpc_cred = cdata->cred,
1537 };
1538 int status;
1539
1540 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1541 (long long) cdata->args.offset);
1542
Trond Myklebust0e574af2005-10-27 22:12:38 -04001543 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 status = rpc_call_sync(server->client, &msg, 0);
1545 dprintk("NFS reply commit: %d\n", status);
1546 return status;
1547}
1548
1549static int nfs4_proc_commit(struct nfs_write_data *cdata)
1550{
1551 struct nfs4_exception exception = { };
1552 int err;
1553 do {
1554 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1555 _nfs4_proc_commit(cdata),
1556 &exception);
1557 } while (exception.retry);
1558 return err;
1559}
1560
1561/*
1562 * Got race?
1563 * We will need to arrange for the VFS layer to provide an atomic open.
1564 * Until then, this create/open method is prone to inefficiency and race
1565 * conditions due to the lookup, create, and open VFS calls from sys_open()
1566 * placed on the wire.
1567 *
1568 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1569 * The file will be opened again in the subsequent VFS open call
1570 * (nfs4_proc_file_open).
1571 *
1572 * The open for read will just hang around to be used by any process that
1573 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1574 */
1575
1576static int
1577nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001578 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct nfs4_state *state;
1581 struct rpc_cred *cred;
1582 int status = 0;
1583
1584 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1585 if (IS_ERR(cred)) {
1586 status = PTR_ERR(cred);
1587 goto out;
1588 }
1589 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1590 put_rpccred(cred);
1591 if (IS_ERR(state)) {
1592 status = PTR_ERR(state);
1593 goto out;
1594 }
1595 d_instantiate(dentry, state->inode);
1596 if (flags & O_EXCL) {
1597 struct nfs_fattr fattr;
1598 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1599 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001600 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001601 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001602 }
1603 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1604 nfs4_intent_set_file(nd, dentry, state);
1605 else
1606 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607out:
1608 return status;
1609}
1610
1611static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1612{
Trond Myklebust16e42952005-10-27 22:12:44 -04001613 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct nfs4_remove_arg args = {
1615 .fh = NFS_FH(dir),
1616 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001617 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001619 struct nfs_fattr dir_attr;
1620 struct nfs4_remove_res res = {
1621 .server = server,
1622 .dir_attr = &dir_attr,
1623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct rpc_message msg = {
1625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1626 .rpc_argp = &args,
1627 .rpc_resp = &res,
1628 };
1629 int status;
1630
Trond Myklebust16e42952005-10-27 22:12:44 -04001631 nfs_fattr_init(res.dir_attr);
1632 status = rpc_call_sync(server->client, &msg, 0);
1633 if (status == 0) {
1634 update_changeattr(dir, &res.cinfo);
1635 nfs_post_op_update_inode(dir, res.dir_attr);
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return status;
1638}
1639
1640static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1641{
1642 struct nfs4_exception exception = { };
1643 int err;
1644 do {
1645 err = nfs4_handle_exception(NFS_SERVER(dir),
1646 _nfs4_proc_remove(dir, name),
1647 &exception);
1648 } while (exception.retry);
1649 return err;
1650}
1651
1652struct unlink_desc {
1653 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001654 struct nfs4_remove_res res;
1655 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656};
1657
1658static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1659 struct qstr *name)
1660{
Trond Myklebust16e42952005-10-27 22:12:44 -04001661 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct unlink_desc *up;
1663
1664 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1665 if (!up)
1666 return -ENOMEM;
1667
1668 up->args.fh = NFS_FH(dir->d_inode);
1669 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001670 up->args.bitmask = server->attr_bitmask;
1671 up->res.server = server;
1672 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1675 msg->rpc_argp = &up->args;
1676 msg->rpc_resp = &up->res;
1677 return 0;
1678}
1679
1680static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1681{
1682 struct rpc_message *msg = &task->tk_msg;
1683 struct unlink_desc *up;
1684
1685 if (msg->rpc_resp != NULL) {
1686 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001687 update_changeattr(dir->d_inode, &up->res.cinfo);
1688 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 kfree(up);
1690 msg->rpc_resp = NULL;
1691 msg->rpc_argp = NULL;
1692 }
1693 return 0;
1694}
1695
1696static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1697 struct inode *new_dir, struct qstr *new_name)
1698{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001699 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 struct nfs4_rename_arg arg = {
1701 .old_dir = NFS_FH(old_dir),
1702 .new_dir = NFS_FH(new_dir),
1703 .old_name = old_name,
1704 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001705 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001707 struct nfs_fattr old_fattr, new_fattr;
1708 struct nfs4_rename_res res = {
1709 .server = server,
1710 .old_fattr = &old_fattr,
1711 .new_fattr = &new_fattr,
1712 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct rpc_message msg = {
1714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1715 .rpc_argp = &arg,
1716 .rpc_resp = &res,
1717 };
1718 int status;
1719
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001720 nfs_fattr_init(res.old_fattr);
1721 nfs_fattr_init(res.new_fattr);
1722 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (!status) {
1725 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001726 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001728 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 return status;
1731}
1732
1733static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1734 struct inode *new_dir, struct qstr *new_name)
1735{
1736 struct nfs4_exception exception = { };
1737 int err;
1738 do {
1739 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1740 _nfs4_proc_rename(old_dir, old_name,
1741 new_dir, new_name),
1742 &exception);
1743 } while (exception.retry);
1744 return err;
1745}
1746
1747static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1748{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001749 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct nfs4_link_arg arg = {
1751 .fh = NFS_FH(inode),
1752 .dir_fh = NFS_FH(dir),
1753 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001754 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001756 struct nfs_fattr fattr, dir_attr;
1757 struct nfs4_link_res res = {
1758 .server = server,
1759 .fattr = &fattr,
1760 .dir_attr = &dir_attr,
1761 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct rpc_message msg = {
1763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1764 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001765 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 };
1767 int status;
1768
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001769 nfs_fattr_init(res.fattr);
1770 nfs_fattr_init(res.dir_attr);
1771 status = rpc_call_sync(server->client, &msg, 0);
1772 if (!status) {
1773 update_changeattr(dir, &res.cinfo);
1774 nfs_post_op_update_inode(dir, res.dir_attr);
1775 nfs_refresh_inode(inode, res.fattr);
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 return status;
1779}
1780
1781static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1782{
1783 struct nfs4_exception exception = { };
1784 int err;
1785 do {
1786 err = nfs4_handle_exception(NFS_SERVER(inode),
1787 _nfs4_proc_link(inode, dir, name),
1788 &exception);
1789 } while (exception.retry);
1790 return err;
1791}
1792
1793static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1794 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1795 struct nfs_fattr *fattr)
1796{
1797 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001798 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 struct nfs4_create_arg arg = {
1800 .dir_fh = NFS_FH(dir),
1801 .server = server,
1802 .name = name,
1803 .attrs = sattr,
1804 .ftype = NF4LNK,
1805 .bitmask = server->attr_bitmask,
1806 };
1807 struct nfs4_create_res res = {
1808 .server = server,
1809 .fh = fhandle,
1810 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001811 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 };
1813 struct rpc_message msg = {
1814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1815 .rpc_argp = &arg,
1816 .rpc_resp = &res,
1817 };
1818 int status;
1819
1820 if (path->len > NFS4_MAXPATHLEN)
1821 return -ENAMETOOLONG;
1822 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001823 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001824 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1827 if (!status)
1828 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001829 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
1833static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1834 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1835 struct nfs_fattr *fattr)
1836{
1837 struct nfs4_exception exception = { };
1838 int err;
1839 do {
1840 err = nfs4_handle_exception(NFS_SERVER(dir),
1841 _nfs4_proc_symlink(dir, name, path, sattr,
1842 fhandle, fattr),
1843 &exception);
1844 } while (exception.retry);
1845 return err;
1846}
1847
1848static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1849 struct iattr *sattr)
1850{
1851 struct nfs_server *server = NFS_SERVER(dir);
1852 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001853 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct nfs4_create_arg arg = {
1855 .dir_fh = NFS_FH(dir),
1856 .server = server,
1857 .name = &dentry->d_name,
1858 .attrs = sattr,
1859 .ftype = NF4DIR,
1860 .bitmask = server->attr_bitmask,
1861 };
1862 struct nfs4_create_res res = {
1863 .server = server,
1864 .fh = &fhandle,
1865 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001866 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 };
1868 struct rpc_message msg = {
1869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1870 .rpc_argp = &arg,
1871 .rpc_resp = &res,
1872 };
1873 int status;
1874
Trond Myklebust0e574af2005-10-27 22:12:38 -04001875 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001876 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1879 if (!status) {
1880 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001881 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 status = nfs_instantiate(dentry, &fhandle, &fattr);
1883 }
1884 return status;
1885}
1886
1887static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1888 struct iattr *sattr)
1889{
1890 struct nfs4_exception exception = { };
1891 int err;
1892 do {
1893 err = nfs4_handle_exception(NFS_SERVER(dir),
1894 _nfs4_proc_mkdir(dir, dentry, sattr),
1895 &exception);
1896 } while (exception.retry);
1897 return err;
1898}
1899
1900static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1901 u64 cookie, struct page *page, unsigned int count, int plus)
1902{
1903 struct inode *dir = dentry->d_inode;
1904 struct nfs4_readdir_arg args = {
1905 .fh = NFS_FH(dir),
1906 .pages = &page,
1907 .pgbase = 0,
1908 .count = count,
1909 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1910 };
1911 struct nfs4_readdir_res res;
1912 struct rpc_message msg = {
1913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1914 .rpc_argp = &args,
1915 .rpc_resp = &res,
1916 .rpc_cred = cred,
1917 };
1918 int status;
1919
Trond Myklebusteadf4592005-06-22 17:16:39 +00001920 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1921 dentry->d_parent->d_name.name,
1922 dentry->d_name.name,
1923 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 lock_kernel();
1925 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1926 res.pgbase = args.pgbase;
1927 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1928 if (status == 0)
1929 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1930 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00001931 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return status;
1933}
1934
1935static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1936 u64 cookie, struct page *page, unsigned int count, int plus)
1937{
1938 struct nfs4_exception exception = { };
1939 int err;
1940 do {
1941 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1942 _nfs4_proc_readdir(dentry, cred, cookie,
1943 page, count, plus),
1944 &exception);
1945 } while (exception.retry);
1946 return err;
1947}
1948
1949static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1950 struct iattr *sattr, dev_t rdev)
1951{
1952 struct nfs_server *server = NFS_SERVER(dir);
1953 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001954 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs4_create_arg arg = {
1956 .dir_fh = NFS_FH(dir),
1957 .server = server,
1958 .name = &dentry->d_name,
1959 .attrs = sattr,
1960 .bitmask = server->attr_bitmask,
1961 };
1962 struct nfs4_create_res res = {
1963 .server = server,
1964 .fh = &fh,
1965 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001966 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 };
1968 struct rpc_message msg = {
1969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1970 .rpc_argp = &arg,
1971 .rpc_resp = &res,
1972 };
1973 int status;
1974 int mode = sattr->ia_mode;
1975
Trond Myklebust0e574af2005-10-27 22:12:38 -04001976 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001977 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1980 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1981 if (S_ISFIFO(mode))
1982 arg.ftype = NF4FIFO;
1983 else if (S_ISBLK(mode)) {
1984 arg.ftype = NF4BLK;
1985 arg.u.device.specdata1 = MAJOR(rdev);
1986 arg.u.device.specdata2 = MINOR(rdev);
1987 }
1988 else if (S_ISCHR(mode)) {
1989 arg.ftype = NF4CHR;
1990 arg.u.device.specdata1 = MAJOR(rdev);
1991 arg.u.device.specdata2 = MINOR(rdev);
1992 }
1993 else
1994 arg.ftype = NF4SOCK;
1995
1996 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1997 if (status == 0) {
1998 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001999 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 status = nfs_instantiate(dentry, &fh, &fattr);
2001 }
2002 return status;
2003}
2004
2005static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2006 struct iattr *sattr, dev_t rdev)
2007{
2008 struct nfs4_exception exception = { };
2009 int err;
2010 do {
2011 err = nfs4_handle_exception(NFS_SERVER(dir),
2012 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2013 &exception);
2014 } while (exception.retry);
2015 return err;
2016}
2017
2018static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2019 struct nfs_fsstat *fsstat)
2020{
2021 struct nfs4_statfs_arg args = {
2022 .fh = fhandle,
2023 .bitmask = server->attr_bitmask,
2024 };
2025 struct rpc_message msg = {
2026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2027 .rpc_argp = &args,
2028 .rpc_resp = fsstat,
2029 };
2030
Trond Myklebust0e574af2005-10-27 22:12:38 -04002031 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return rpc_call_sync(server->client, &msg, 0);
2033}
2034
2035static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2036{
2037 struct nfs4_exception exception = { };
2038 int err;
2039 do {
2040 err = nfs4_handle_exception(server,
2041 _nfs4_proc_statfs(server, fhandle, fsstat),
2042 &exception);
2043 } while (exception.retry);
2044 return err;
2045}
2046
2047static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2048 struct nfs_fsinfo *fsinfo)
2049{
2050 struct nfs4_fsinfo_arg args = {
2051 .fh = fhandle,
2052 .bitmask = server->attr_bitmask,
2053 };
2054 struct rpc_message msg = {
2055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2056 .rpc_argp = &args,
2057 .rpc_resp = fsinfo,
2058 };
2059
2060 return rpc_call_sync(server->client, &msg, 0);
2061}
2062
2063static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2064{
2065 struct nfs4_exception exception = { };
2066 int err;
2067
2068 do {
2069 err = nfs4_handle_exception(server,
2070 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2071 &exception);
2072 } while (exception.retry);
2073 return err;
2074}
2075
2076static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2077{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002078 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2080}
2081
2082static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2083 struct nfs_pathconf *pathconf)
2084{
2085 struct nfs4_pathconf_arg args = {
2086 .fh = fhandle,
2087 .bitmask = server->attr_bitmask,
2088 };
2089 struct rpc_message msg = {
2090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2091 .rpc_argp = &args,
2092 .rpc_resp = pathconf,
2093 };
2094
2095 /* None of the pathconf attributes are mandatory to implement */
2096 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2097 memset(pathconf, 0, sizeof(*pathconf));
2098 return 0;
2099 }
2100
Trond Myklebust0e574af2005-10-27 22:12:38 -04002101 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return rpc_call_sync(server->client, &msg, 0);
2103}
2104
2105static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2106 struct nfs_pathconf *pathconf)
2107{
2108 struct nfs4_exception exception = { };
2109 int err;
2110
2111 do {
2112 err = nfs4_handle_exception(server,
2113 _nfs4_proc_pathconf(server, fhandle, pathconf),
2114 &exception);
2115 } while (exception.retry);
2116 return err;
2117}
2118
2119static void
2120nfs4_read_done(struct rpc_task *task)
2121{
2122 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
2123 struct inode *inode = data->inode;
2124
2125 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2126 rpc_restart_call(task);
2127 return;
2128 }
2129 if (task->tk_status > 0)
2130 renew_lease(NFS_SERVER(inode), data->timestamp);
2131 /* Call back common NFS readpage processing */
2132 nfs_readpage_result(task);
2133}
2134
2135static void
2136nfs4_proc_read_setup(struct nfs_read_data *data)
2137{
2138 struct rpc_task *task = &data->task;
2139 struct rpc_message msg = {
2140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2141 .rpc_argp = &data->args,
2142 .rpc_resp = &data->res,
2143 .rpc_cred = data->cred,
2144 };
2145 struct inode *inode = data->inode;
2146 int flags;
2147
2148 data->timestamp = jiffies;
2149
2150 /* N.B. Do we need to test? Never called for swapfile inode */
2151 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2152
2153 /* Finalize the task. */
2154 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2155 rpc_call_setup(task, &msg, 0);
2156}
2157
2158static void
2159nfs4_write_done(struct rpc_task *task)
2160{
2161 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2162 struct inode *inode = data->inode;
2163
2164 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2165 rpc_restart_call(task);
2166 return;
2167 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002168 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002170 nfs_post_op_update_inode(inode, data->res.fattr);
2171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* Call back common NFS writeback processing */
2173 nfs_writeback_done(task);
2174}
2175
2176static void
2177nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2178{
2179 struct rpc_task *task = &data->task;
2180 struct rpc_message msg = {
2181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2182 .rpc_argp = &data->args,
2183 .rpc_resp = &data->res,
2184 .rpc_cred = data->cred,
2185 };
2186 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002187 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int stable;
2189 int flags;
2190
2191 if (how & FLUSH_STABLE) {
2192 if (!NFS_I(inode)->ncommit)
2193 stable = NFS_FILE_SYNC;
2194 else
2195 stable = NFS_DATA_SYNC;
2196 } else
2197 stable = NFS_UNSTABLE;
2198 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002199 data->args.bitmask = server->attr_bitmask;
2200 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 data->timestamp = jiffies;
2203
2204 /* Set the initial flags for the task. */
2205 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2206
2207 /* Finalize the task. */
2208 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2209 rpc_call_setup(task, &msg, 0);
2210}
2211
2212static void
2213nfs4_commit_done(struct rpc_task *task)
2214{
2215 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2216 struct inode *inode = data->inode;
2217
2218 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2219 rpc_restart_call(task);
2220 return;
2221 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002222 if (task->tk_status >= 0)
2223 nfs_post_op_update_inode(inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* Call back common NFS writeback processing */
2225 nfs_commit_done(task);
2226}
2227
2228static void
2229nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2230{
2231 struct rpc_task *task = &data->task;
2232 struct rpc_message msg = {
2233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2234 .rpc_argp = &data->args,
2235 .rpc_resp = &data->res,
2236 .rpc_cred = data->cred,
2237 };
2238 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002239 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int flags;
2241
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002242 data->args.bitmask = server->attr_bitmask;
2243 data->res.server = server;
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Set the initial flags for the task. */
2246 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2247
2248 /* Finalize the task. */
2249 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2250 rpc_call_setup(task, &msg, 0);
2251}
2252
2253/*
2254 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2255 * standalone procedure for queueing an asynchronous RENEW.
2256 */
2257static void
2258renew_done(struct rpc_task *task)
2259{
2260 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2261 unsigned long timestamp = (unsigned long)task->tk_calldata;
2262
2263 if (task->tk_status < 0) {
2264 switch (task->tk_status) {
2265 case -NFS4ERR_STALE_CLIENTID:
2266 case -NFS4ERR_EXPIRED:
2267 case -NFS4ERR_CB_PATH_DOWN:
2268 nfs4_schedule_state_recovery(clp);
2269 }
2270 return;
2271 }
2272 spin_lock(&clp->cl_lock);
2273 if (time_before(clp->cl_last_renewal,timestamp))
2274 clp->cl_last_renewal = timestamp;
2275 spin_unlock(&clp->cl_lock);
2276}
2277
2278int
2279nfs4_proc_async_renew(struct nfs4_client *clp)
2280{
2281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2283 .rpc_argp = clp,
2284 .rpc_cred = clp->cl_cred,
2285 };
2286
2287 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2288 renew_done, (void *)jiffies);
2289}
2290
2291int
2292nfs4_proc_renew(struct nfs4_client *clp)
2293{
2294 struct rpc_message msg = {
2295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2296 .rpc_argp = clp,
2297 .rpc_cred = clp->cl_cred,
2298 };
2299 unsigned long now = jiffies;
2300 int status;
2301
2302 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2303 if (status < 0)
2304 return status;
2305 spin_lock(&clp->cl_lock);
2306 if (time_before(clp->cl_last_renewal,now))
2307 clp->cl_last_renewal = now;
2308 spin_unlock(&clp->cl_lock);
2309 return 0;
2310}
2311
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002312static inline int nfs4_server_supports_acls(struct nfs_server *server)
2313{
2314 return (server->caps & NFS_CAP_ACLS)
2315 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2316 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2317}
2318
2319/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2320 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2321 * the stack.
2322 */
2323#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2324
2325static void buf_to_pages(const void *buf, size_t buflen,
2326 struct page **pages, unsigned int *pgbase)
2327{
2328 const void *p = buf;
2329
2330 *pgbase = offset_in_page(buf);
2331 p -= *pgbase;
2332 while (p < buf + buflen) {
2333 *(pages++) = virt_to_page(p);
2334 p += PAGE_CACHE_SIZE;
2335 }
2336}
2337
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002338struct nfs4_cached_acl {
2339 int cached;
2340 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002341 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002342};
2343
2344static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002345{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002346 struct nfs_inode *nfsi = NFS_I(inode);
2347
2348 spin_lock(&inode->i_lock);
2349 kfree(nfsi->nfs4_acl);
2350 nfsi->nfs4_acl = acl;
2351 spin_unlock(&inode->i_lock);
2352}
2353
2354static void nfs4_zap_acl_attr(struct inode *inode)
2355{
2356 nfs4_set_cached_acl(inode, NULL);
2357}
2358
2359static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2360{
2361 struct nfs_inode *nfsi = NFS_I(inode);
2362 struct nfs4_cached_acl *acl;
2363 int ret = -ENOENT;
2364
2365 spin_lock(&inode->i_lock);
2366 acl = nfsi->nfs4_acl;
2367 if (acl == NULL)
2368 goto out;
2369 if (buf == NULL) /* user is just asking for length */
2370 goto out_len;
2371 if (acl->cached == 0)
2372 goto out;
2373 ret = -ERANGE; /* see getxattr(2) man page */
2374 if (acl->len > buflen)
2375 goto out;
2376 memcpy(buf, acl->data, acl->len);
2377out_len:
2378 ret = acl->len;
2379out:
2380 spin_unlock(&inode->i_lock);
2381 return ret;
2382}
2383
2384static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2385{
2386 struct nfs4_cached_acl *acl;
2387
2388 if (buf && acl_len <= PAGE_SIZE) {
2389 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2390 if (acl == NULL)
2391 goto out;
2392 acl->cached = 1;
2393 memcpy(acl->data, buf, acl_len);
2394 } else {
2395 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2396 if (acl == NULL)
2397 goto out;
2398 acl->cached = 0;
2399 }
2400 acl->len = acl_len;
2401out:
2402 nfs4_set_cached_acl(inode, acl);
2403}
2404
2405static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2406{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002407 struct page *pages[NFS4ACL_MAXPAGES];
2408 struct nfs_getaclargs args = {
2409 .fh = NFS_FH(inode),
2410 .acl_pages = pages,
2411 .acl_len = buflen,
2412 };
2413 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002414 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002415 struct rpc_message msg = {
2416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2417 .rpc_argp = &args,
2418 .rpc_resp = &resp_len,
2419 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002420 struct page *localpage = NULL;
2421 int ret;
2422
2423 if (buflen < PAGE_SIZE) {
2424 /* As long as we're doing a round trip to the server anyway,
2425 * let's be prepared for a page of acl data. */
2426 localpage = alloc_page(GFP_KERNEL);
2427 resp_buf = page_address(localpage);
2428 if (localpage == NULL)
2429 return -ENOMEM;
2430 args.acl_pages[0] = localpage;
2431 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002432 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002433 } else {
2434 resp_buf = buf;
2435 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2436 }
2437 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2438 if (ret)
2439 goto out_free;
2440 if (resp_len > args.acl_len)
2441 nfs4_write_cached_acl(inode, NULL, resp_len);
2442 else
2443 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2444 if (buf) {
2445 ret = -ERANGE;
2446 if (resp_len > buflen)
2447 goto out_free;
2448 if (localpage)
2449 memcpy(buf, resp_buf, resp_len);
2450 }
2451 ret = resp_len;
2452out_free:
2453 if (localpage)
2454 __free_page(localpage);
2455 return ret;
2456}
2457
2458static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2459{
2460 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002461 int ret;
2462
2463 if (!nfs4_server_supports_acls(server))
2464 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002465 ret = nfs_revalidate_inode(server, inode);
2466 if (ret < 0)
2467 return ret;
2468 ret = nfs4_read_cached_acl(inode, buf, buflen);
2469 if (ret != -ENOENT)
2470 return ret;
2471 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002472}
2473
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002474static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2475{
2476 struct nfs_server *server = NFS_SERVER(inode);
2477 struct page *pages[NFS4ACL_MAXPAGES];
2478 struct nfs_setaclargs arg = {
2479 .fh = NFS_FH(inode),
2480 .acl_pages = pages,
2481 .acl_len = buflen,
2482 };
2483 struct rpc_message msg = {
2484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2485 .rpc_argp = &arg,
2486 .rpc_resp = NULL,
2487 };
2488 int ret;
2489
2490 if (!nfs4_server_supports_acls(server))
2491 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002492 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002493 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2494 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002495 if (ret == 0)
2496 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002497 return ret;
2498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002501nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 struct nfs4_client *clp = server->nfs4_state;
2504
2505 if (!clp || task->tk_status >= 0)
2506 return 0;
2507 switch(task->tk_status) {
2508 case -NFS4ERR_STALE_CLIENTID:
2509 case -NFS4ERR_STALE_STATEID:
2510 case -NFS4ERR_EXPIRED:
2511 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2512 nfs4_schedule_state_recovery(clp);
2513 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2514 rpc_wake_up_task(task);
2515 task->tk_status = 0;
2516 return -EAGAIN;
2517 case -NFS4ERR_GRACE:
2518 case -NFS4ERR_DELAY:
2519 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2520 task->tk_status = 0;
2521 return -EAGAIN;
2522 case -NFS4ERR_OLD_STATEID:
2523 task->tk_status = 0;
2524 return -EAGAIN;
2525 }
2526 task->tk_status = nfs4_map_errors(task->tk_status);
2527 return 0;
2528}
2529
2530static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2531{
2532 DEFINE_WAIT(wait);
2533 sigset_t oldset;
2534 int interruptible, res = 0;
2535
2536 might_sleep();
2537
2538 rpc_clnt_sigmask(clnt, &oldset);
2539 interruptible = TASK_UNINTERRUPTIBLE;
2540 if (clnt->cl_intr)
2541 interruptible = TASK_INTERRUPTIBLE;
2542 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2543 nfs4_schedule_state_recovery(clp);
2544 if (clnt->cl_intr && signalled())
2545 res = -ERESTARTSYS;
2546 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2547 schedule();
2548 finish_wait(&clp->cl_waitq, &wait);
2549 rpc_clnt_sigunmask(clnt, &oldset);
2550 return res;
2551}
2552
2553static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2554{
2555 sigset_t oldset;
2556 int res = 0;
2557
2558 might_sleep();
2559
2560 if (*timeout <= 0)
2561 *timeout = NFS4_POLL_RETRY_MIN;
2562 if (*timeout > NFS4_POLL_RETRY_MAX)
2563 *timeout = NFS4_POLL_RETRY_MAX;
2564 rpc_clnt_sigmask(clnt, &oldset);
2565 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002566 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (signalled())
2568 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002569 } else
2570 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 rpc_clnt_sigunmask(clnt, &oldset);
2572 *timeout <<= 1;
2573 return res;
2574}
2575
2576/* This is the error handling routine for processes that are allowed
2577 * to sleep.
2578 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002579int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct nfs4_client *clp = server->nfs4_state;
2582 int ret = errorcode;
2583
2584 exception->retry = 0;
2585 switch(errorcode) {
2586 case 0:
2587 return 0;
2588 case -NFS4ERR_STALE_CLIENTID:
2589 case -NFS4ERR_STALE_STATEID:
2590 case -NFS4ERR_EXPIRED:
2591 ret = nfs4_wait_clnt_recover(server->client, clp);
2592 if (ret == 0)
2593 exception->retry = 1;
2594 break;
2595 case -NFS4ERR_GRACE:
2596 case -NFS4ERR_DELAY:
2597 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002598 if (ret != 0)
2599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002601 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603 /* We failed to handle the error */
2604 return nfs4_map_errors(ret);
2605}
2606
2607int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2608{
2609 nfs4_verifier sc_verifier;
2610 struct nfs4_setclientid setclientid = {
2611 .sc_verifier = &sc_verifier,
2612 .sc_prog = program,
2613 };
2614 struct rpc_message msg = {
2615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2616 .rpc_argp = &setclientid,
2617 .rpc_resp = clp,
2618 .rpc_cred = clp->cl_cred,
2619 };
2620 u32 *p;
2621 int loop = 0;
2622 int status;
2623
2624 p = (u32*)sc_verifier.data;
2625 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2626 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2627
2628 for(;;) {
2629 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2630 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2631 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2632 clp->cl_cred->cr_ops->cr_name,
2633 clp->cl_id_uniquifier);
2634 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2635 sizeof(setclientid.sc_netid), "tcp");
2636 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2637 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2638 clp->cl_ipaddr, port >> 8, port & 255);
2639
2640 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2641 if (status != -NFS4ERR_CLID_INUSE)
2642 break;
2643 if (signalled())
2644 break;
2645 if (loop++ & 1)
2646 ssleep(clp->cl_lease_time + 1);
2647 else
2648 if (++clp->cl_id_uniquifier == 0)
2649 break;
2650 }
2651 return status;
2652}
2653
2654int
2655nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2656{
2657 struct nfs_fsinfo fsinfo;
2658 struct rpc_message msg = {
2659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2660 .rpc_argp = clp,
2661 .rpc_resp = &fsinfo,
2662 .rpc_cred = clp->cl_cred,
2663 };
2664 unsigned long now;
2665 int status;
2666
2667 now = jiffies;
2668 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2669 if (status == 0) {
2670 spin_lock(&clp->cl_lock);
2671 clp->cl_lease_time = fsinfo.lease_time * HZ;
2672 clp->cl_last_renewal = now;
2673 spin_unlock(&clp->cl_lock);
2674 }
2675 return status;
2676}
2677
2678static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2679{
2680 struct nfs4_delegreturnargs args = {
2681 .fhandle = NFS_FH(inode),
2682 .stateid = stateid,
2683 };
2684 struct rpc_message msg = {
2685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2686 .rpc_argp = &args,
2687 .rpc_cred = cred,
2688 };
2689
2690 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2691}
2692
2693int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2694{
2695 struct nfs_server *server = NFS_SERVER(inode);
2696 struct nfs4_exception exception = { };
2697 int err;
2698 do {
2699 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2700 switch (err) {
2701 case -NFS4ERR_STALE_STATEID:
2702 case -NFS4ERR_EXPIRED:
2703 nfs4_schedule_state_recovery(server->nfs4_state);
2704 case 0:
2705 return 0;
2706 }
2707 err = nfs4_handle_exception(server, err, &exception);
2708 } while (exception.retry);
2709 return err;
2710}
2711
2712#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2713#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2714
2715/*
2716 * sleep, with exponential backoff, and retry the LOCK operation.
2717 */
2718static unsigned long
2719nfs4_set_lock_task_retry(unsigned long timeout)
2720{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002721 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 timeout <<= 1;
2723 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2724 return NFS4_LOCK_MAXTIMEOUT;
2725 return timeout;
2726}
2727
2728static inline int
2729nfs4_lck_type(int cmd, struct file_lock *request)
2730{
2731 /* set lock type */
2732 switch (request->fl_type) {
2733 case F_RDLCK:
2734 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2735 case F_WRLCK:
2736 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2737 case F_UNLCK:
2738 return NFS4_WRITE_LT;
2739 }
2740 BUG();
2741 return 0;
2742}
2743
2744static inline uint64_t
2745nfs4_lck_length(struct file_lock *request)
2746{
2747 if (request->fl_end == OFFSET_MAX)
2748 return ~(uint64_t)0;
2749 return request->fl_end - request->fl_start + 1;
2750}
2751
2752static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2753{
2754 struct inode *inode = state->inode;
2755 struct nfs_server *server = NFS_SERVER(inode);
2756 struct nfs4_client *clp = server->nfs4_state;
2757 struct nfs_lockargs arg = {
2758 .fh = NFS_FH(inode),
2759 .type = nfs4_lck_type(cmd, request),
2760 .offset = request->fl_start,
2761 .length = nfs4_lck_length(request),
2762 };
2763 struct nfs_lockres res = {
2764 .server = server,
2765 };
2766 struct rpc_message msg = {
2767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2768 .rpc_argp = &arg,
2769 .rpc_resp = &res,
2770 .rpc_cred = state->owner->so_cred,
2771 };
2772 struct nfs_lowner nlo;
2773 struct nfs4_lock_state *lsp;
2774 int status;
2775
2776 down_read(&clp->cl_sem);
2777 nlo.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002778 status = nfs4_set_lock_state(state, request);
2779 if (status != 0)
2780 goto out;
2781 lsp = request->fl_u.nfs4_fl.owner;
2782 nlo.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 arg.u.lockt = &nlo;
2784 status = rpc_call_sync(server->client, &msg, 0);
2785 if (!status) {
2786 request->fl_type = F_UNLCK;
2787 } else if (status == -NFS4ERR_DENIED) {
2788 int64_t len, start, end;
2789 start = res.u.denied.offset;
2790 len = res.u.denied.length;
2791 end = start + len - 1;
2792 if (end < 0 || len == 0)
2793 request->fl_end = OFFSET_MAX;
2794 else
2795 request->fl_end = (loff_t)end;
2796 request->fl_start = (loff_t)start;
2797 request->fl_type = F_WRLCK;
2798 if (res.u.denied.type & 1)
2799 request->fl_type = F_RDLCK;
2800 request->fl_pid = 0;
2801 status = 0;
2802 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 up_read(&clp->cl_sem);
2805 return status;
2806}
2807
2808static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2809{
2810 struct nfs4_exception exception = { };
2811 int err;
2812
2813 do {
2814 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2815 _nfs4_proc_getlk(state, cmd, request),
2816 &exception);
2817 } while (exception.retry);
2818 return err;
2819}
2820
2821static int do_vfs_lock(struct file *file, struct file_lock *fl)
2822{
2823 int res = 0;
2824 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2825 case FL_POSIX:
2826 res = posix_lock_file_wait(file, fl);
2827 break;
2828 case FL_FLOCK:
2829 res = flock_lock_file_wait(file, fl);
2830 break;
2831 default:
2832 BUG();
2833 }
2834 if (res < 0)
2835 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2836 __FUNCTION__);
2837 return res;
2838}
2839
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002840struct nfs4_unlockdata {
2841 struct nfs_lockargs arg;
2842 struct nfs_locku_opargs luargs;
2843 struct nfs_lockres res;
2844 struct nfs4_lock_state *lsp;
2845 struct nfs_open_context *ctx;
2846 atomic_t refcount;
2847 struct completion completion;
2848};
2849
2850static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002852 if (atomic_dec_and_test(&calldata->refcount)) {
2853 nfs_free_seqid(calldata->luargs.seqid);
2854 nfs4_put_lock_state(calldata->lsp);
2855 put_nfs_open_context(calldata->ctx);
2856 kfree(calldata);
2857 }
2858}
2859
2860static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
2861{
2862 complete(&calldata->completion);
2863 nfs4_locku_release_calldata(calldata);
2864}
2865
2866static void nfs4_locku_done(struct rpc_task *task)
2867{
2868 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2869
2870 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2871 switch (task->tk_status) {
2872 case 0:
2873 memcpy(calldata->lsp->ls_stateid.data,
2874 calldata->res.u.stateid.data,
2875 sizeof(calldata->lsp->ls_stateid.data));
2876 break;
2877 case -NFS4ERR_STALE_STATEID:
2878 case -NFS4ERR_EXPIRED:
2879 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2880 break;
2881 default:
2882 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2883 rpc_restart_call(task);
2884 return;
2885 }
2886 }
2887 nfs4_locku_complete(calldata);
2888}
2889
2890static void nfs4_locku_begin(struct rpc_task *task)
2891{
2892 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct rpc_message msg = {
2894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002895 .rpc_argp = &calldata->arg,
2896 .rpc_resp = &calldata->res,
2897 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 };
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002899 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002901 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2902 if (status != 0)
2903 return;
2904 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2905 nfs4_locku_complete(calldata);
2906 task->tk_exit = NULL;
2907 rpc_exit(task, 0);
2908 return;
2909 }
2910 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
2913static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2914{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002915 struct nfs4_unlockdata *calldata;
2916 struct inode *inode = state->inode;
2917 struct nfs_server *server = NFS_SERVER(inode);
2918 struct nfs4_lock_state *lsp;
2919 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002921 status = nfs4_set_lock_state(state, request);
2922 if (status != 0)
2923 return status;
2924 lsp = request->fl_u.nfs4_fl.owner;
2925 /* We might have lost the locks! */
2926 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2927 return 0;
2928 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2929 if (calldata == NULL)
2930 return -ENOMEM;
2931 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2932 if (calldata->luargs.seqid == NULL) {
2933 kfree(calldata);
2934 return -ENOMEM;
2935 }
2936 calldata->luargs.stateid = &lsp->ls_stateid;
2937 calldata->arg.fh = NFS_FH(inode);
2938 calldata->arg.type = nfs4_lck_type(cmd, request);
2939 calldata->arg.offset = request->fl_start;
2940 calldata->arg.length = nfs4_lck_length(request);
2941 calldata->arg.u.locku = &calldata->luargs;
2942 calldata->res.server = server;
2943 calldata->lsp = lsp;
2944 atomic_inc(&lsp->ls_count);
2945
2946 /* Ensure we don't close file until we're done freeing locks! */
2947 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2948
2949 atomic_set(&calldata->refcount, 2);
2950 init_completion(&calldata->completion);
2951
2952 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
2953 nfs4_locku_done, calldata);
2954 if (status == 0)
2955 wait_for_completion_interruptible(&calldata->completion);
2956 do_vfs_lock(request->fl_file, request);
2957 nfs4_locku_release_calldata(calldata);
2958 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2962{
2963 struct inode *inode = state->inode;
2964 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002965 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06735b32005-10-18 14:20:15 -07002966 struct nfs_lock_opargs largs = {
2967 .lock_stateid = &lsp->ls_stateid,
2968 .open_stateid = &state->stateid,
2969 .lock_owner = {
2970 .clientid = server->nfs4_state->cl_clientid,
2971 .id = lsp->ls_id,
2972 },
2973 .reclaim = reclaim,
2974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct nfs_lockargs arg = {
2976 .fh = NFS_FH(inode),
2977 .type = nfs4_lck_type(cmd, request),
2978 .offset = request->fl_start,
2979 .length = nfs4_lck_length(request),
Trond Myklebust06735b32005-10-18 14:20:15 -07002980 .u = {
2981 .lock = &largs,
2982 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
2984 struct nfs_lockres res = {
2985 .server = server,
2986 };
2987 struct rpc_message msg = {
2988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2989 .rpc_argp = &arg,
2990 .rpc_resp = &res,
2991 .rpc_cred = state->owner->so_cred,
2992 };
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002993 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Trond Myklebust06735b32005-10-18 14:20:15 -07002995 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2996 if (largs.lock_seqid == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002997 return -ENOMEM;
2998 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct nfs4_state_owner *owner = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Trond Myklebust06735b32005-10-18 14:20:15 -07003001 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3002 if (largs.open_seqid == NULL)
3003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 largs.new_lock_owner = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust06735b32005-10-18 14:20:15 -07003006 /* increment open seqid on success, and seqid mutating errors */
3007 if (largs.new_lock_owner != 0) {
3008 nfs_increment_open_seqid(status, largs.open_seqid);
3009 if (status == 0)
3010 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3011 }
3012 nfs_free_seqid(largs.open_seqid);
3013 } else
3014 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3015 /* increment lock seqid on success, and seqid mutating errors*/
3016 nfs_increment_lock_seqid(status, largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 /* save the returned stateid. */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003018 if (status == 0) {
Trond Myklebust06735b32005-10-18 14:20:15 -07003019 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3020 sizeof(lsp->ls_stateid.data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003021 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3022 } else if (status == -NFS4ERR_DENIED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 status = -EAGAIN;
Trond Myklebust06735b32005-10-18 14:20:15 -07003024out:
3025 nfs_free_seqid(largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return status;
3027}
3028
3029static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3030{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003031 struct nfs_server *server = NFS_SERVER(state->inode);
3032 struct nfs4_exception exception = { };
3033 int err;
3034
3035 do {
3036 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3037 if (err != -NFS4ERR_DELAY)
3038 break;
3039 nfs4_handle_exception(server, err, &exception);
3040 } while (exception.retry);
3041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
3044static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3045{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003046 struct nfs_server *server = NFS_SERVER(state->inode);
3047 struct nfs4_exception exception = { };
3048 int err;
3049
3050 do {
3051 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3052 if (err != -NFS4ERR_DELAY)
3053 break;
3054 nfs4_handle_exception(server, err, &exception);
3055 } while (exception.retry);
3056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3060{
3061 struct nfs4_client *clp = state->owner->so_client;
3062 int status;
3063
3064 down_read(&clp->cl_sem);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003065 status = nfs4_set_lock_state(state, request);
3066 if (status == 0)
3067 status = _nfs4_do_setlk(state, cmd, request, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (status == 0) {
3069 /* Note: we always want to sleep here! */
3070 request->fl_flags |= FL_SLEEP;
3071 if (do_vfs_lock(request->fl_file, request) < 0)
3072 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3073 }
3074 up_read(&clp->cl_sem);
3075 return status;
3076}
3077
3078static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3079{
3080 struct nfs4_exception exception = { };
3081 int err;
3082
3083 do {
3084 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3085 _nfs4_proc_setlk(state, cmd, request),
3086 &exception);
3087 } while (exception.retry);
3088 return err;
3089}
3090
3091static int
3092nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3093{
3094 struct nfs_open_context *ctx;
3095 struct nfs4_state *state;
3096 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3097 int status;
3098
3099 /* verify open state */
3100 ctx = (struct nfs_open_context *)filp->private_data;
3101 state = ctx->state;
3102
3103 if (request->fl_start < 0 || request->fl_end < 0)
3104 return -EINVAL;
3105
3106 if (IS_GETLK(cmd))
3107 return nfs4_proc_getlk(state, F_GETLK, request);
3108
3109 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3110 return -EINVAL;
3111
3112 if (request->fl_type == F_UNLCK)
3113 return nfs4_proc_unlck(state, cmd, request);
3114
3115 do {
3116 status = nfs4_proc_setlk(state, cmd, request);
3117 if ((status != -EAGAIN) || IS_SETLK(cmd))
3118 break;
3119 timeout = nfs4_set_lock_task_retry(timeout);
3120 status = -ERESTARTSYS;
3121 if (signalled())
3122 break;
3123 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return status;
3125}
3126
Trond Myklebust888e6942005-11-04 15:38:11 -05003127int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3128{
3129 struct nfs_server *server = NFS_SERVER(state->inode);
3130 struct nfs4_exception exception = { };
3131 int err;
3132
3133 err = nfs4_set_lock_state(state, fl);
3134 if (err != 0)
3135 goto out;
3136 do {
3137 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3138 if (err != -NFS4ERR_DELAY)
3139 break;
3140 err = nfs4_handle_exception(server, err, &exception);
3141 } while (exception.retry);
3142out:
3143 return err;
3144}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003145
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003146#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3147
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003148int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3149 size_t buflen, int flags)
3150{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003151 struct inode *inode = dentry->d_inode;
3152
3153 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3154 return -EOPNOTSUPP;
3155
3156 if (!S_ISREG(inode->i_mode) &&
3157 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3158 return -EPERM;
3159
3160 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003161}
3162
3163/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3164 * and that's what we'll do for e.g. user attributes that haven't been set.
3165 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3166 * attributes in kernel-managed attribute namespaces. */
3167ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3168 size_t buflen)
3169{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003170 struct inode *inode = dentry->d_inode;
3171
3172 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3173 return -EOPNOTSUPP;
3174
3175 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003176}
3177
3178ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3179{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003180 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003181
3182 if (buf && buflen < len)
3183 return -ERANGE;
3184 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003185 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3186 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003187}
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3190 .recover_open = nfs4_open_reclaim,
3191 .recover_lock = nfs4_lock_reclaim,
3192};
3193
3194struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3195 .recover_open = nfs4_open_expired,
3196 .recover_lock = nfs4_lock_expired,
3197};
3198
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003199static struct inode_operations nfs4_file_inode_operations = {
3200 .permission = nfs_permission,
3201 .getattr = nfs_getattr,
3202 .setattr = nfs_setattr,
3203 .getxattr = nfs4_getxattr,
3204 .setxattr = nfs4_setxattr,
3205 .listxattr = nfs4_listxattr,
3206};
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208struct nfs_rpc_ops nfs_v4_clientops = {
3209 .version = 4, /* protocol version */
3210 .dentry_ops = &nfs4_dentry_operations,
3211 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003212 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 .getroot = nfs4_proc_get_root,
3214 .getattr = nfs4_proc_getattr,
3215 .setattr = nfs4_proc_setattr,
3216 .lookup = nfs4_proc_lookup,
3217 .access = nfs4_proc_access,
3218 .readlink = nfs4_proc_readlink,
3219 .read = nfs4_proc_read,
3220 .write = nfs4_proc_write,
3221 .commit = nfs4_proc_commit,
3222 .create = nfs4_proc_create,
3223 .remove = nfs4_proc_remove,
3224 .unlink_setup = nfs4_proc_unlink_setup,
3225 .unlink_done = nfs4_proc_unlink_done,
3226 .rename = nfs4_proc_rename,
3227 .link = nfs4_proc_link,
3228 .symlink = nfs4_proc_symlink,
3229 .mkdir = nfs4_proc_mkdir,
3230 .rmdir = nfs4_proc_remove,
3231 .readdir = nfs4_proc_readdir,
3232 .mknod = nfs4_proc_mknod,
3233 .statfs = nfs4_proc_statfs,
3234 .fsinfo = nfs4_proc_fsinfo,
3235 .pathconf = nfs4_proc_pathconf,
3236 .decode_dirent = nfs4_decode_dirent,
3237 .read_setup = nfs4_proc_read_setup,
3238 .write_setup = nfs4_proc_write_setup,
3239 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003240 .file_open = nfs_open,
3241 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003243 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244};
3245
3246/*
3247 * Local variables:
3248 * c-basic-offset: 8
3249 * End:
3250 */