]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/nfs/nfs4proc.c
NFSv4: Support recalling delegations by stateid part 2
[mirror_ubuntu-focal-kernel.git] / fs / nfs / nfs4proc.c
1 /*
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>
50 #include <linux/mount.h>
51
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54 #include "iostat.h"
55
56 #define NFSDBG_FACILITY NFSDBG_PROC
57
58 #define NFS4_POLL_RETRY_MIN (HZ/10)
59 #define NFS4_POLL_RETRY_MAX (15*HZ)
60
61 struct nfs4_opendata;
62 static int _nfs4_proc_open(struct nfs4_opendata *data);
63 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
64 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
65 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
66 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
67 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
68
69 /* Prevent leaks of NFSv4 errors into userland */
70 int nfs4_map_errors(int err)
71 {
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
74 __FUNCTION__, -err);
75 return -EIO;
76 }
77 return err;
78 }
79
80 /*
81 * This is our standard bitmap for GETATTR requests.
82 */
83 const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98 };
99
100 const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107 };
108
109 const u32 nfs4_pathconf_bitmap[2] = {
110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113 };
114
115 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120 };
121
122 const u32 nfs4_fs_locations_bitmap[2] = {
123 FATTR4_WORD0_TYPE
124 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_SIZE
126 | FATTR4_WORD0_FSID
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
139 };
140
141 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
142 struct nfs4_readdir_arg *readdir)
143 {
144 __be32 *start, *p;
145
146 BUG_ON(readdir->count < 80);
147 if (cookie > 2) {
148 readdir->cookie = cookie;
149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
150 return;
151 }
152
153 readdir->cookie = 0;
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
155 if (cookie == 2)
156 return;
157
158 /*
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
163 * instead of 1 or 2.
164 */
165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
166
167 if (cookie == 0) {
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
173 p++;
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
177 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
178 }
179
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
185 p++;
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
189 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
190
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
194 }
195
196 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
197 {
198 struct nfs_client *clp = server->nfs_client;
199 spin_lock(&clp->cl_lock);
200 if (time_before(clp->cl_last_renewal,timestamp))
201 clp->cl_last_renewal = timestamp;
202 spin_unlock(&clp->cl_lock);
203 }
204
205 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
206 {
207 struct nfs_inode *nfsi = NFS_I(dir);
208
209 spin_lock(&dir->i_lock);
210 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
211 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
212 nfsi->change_attr = cinfo->after;
213 spin_unlock(&dir->i_lock);
214 }
215
216 struct nfs4_opendata {
217 struct kref kref;
218 struct nfs_openargs o_arg;
219 struct nfs_openres o_res;
220 struct nfs_open_confirmargs c_arg;
221 struct nfs_open_confirmres c_res;
222 struct nfs_fattr f_attr;
223 struct nfs_fattr dir_attr;
224 struct path path;
225 struct dentry *dir;
226 struct nfs4_state_owner *owner;
227 struct iattr attrs;
228 unsigned long timestamp;
229 int rpc_status;
230 int cancelled;
231 };
232
233
234 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
235 {
236 p->o_res.f_attr = &p->f_attr;
237 p->o_res.dir_attr = &p->dir_attr;
238 p->o_res.server = p->o_arg.server;
239 nfs_fattr_init(&p->f_attr);
240 nfs_fattr_init(&p->dir_attr);
241 }
242
243 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
244 struct nfs4_state_owner *sp, int flags,
245 const struct iattr *attrs)
246 {
247 struct dentry *parent = dget_parent(path->dentry);
248 struct inode *dir = parent->d_inode;
249 struct nfs_server *server = NFS_SERVER(dir);
250 struct nfs4_opendata *p;
251
252 p = kzalloc(sizeof(*p), GFP_KERNEL);
253 if (p == NULL)
254 goto err;
255 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
256 if (p->o_arg.seqid == NULL)
257 goto err_free;
258 p->path.mnt = mntget(path->mnt);
259 p->path.dentry = dget(path->dentry);
260 p->dir = parent;
261 p->owner = sp;
262 atomic_inc(&sp->so_count);
263 p->o_arg.fh = NFS_FH(dir);
264 p->o_arg.open_flags = flags,
265 p->o_arg.clientid = server->nfs_client->cl_clientid;
266 p->o_arg.id = sp->so_owner_id.id;
267 p->o_arg.name = &p->path.dentry->d_name;
268 p->o_arg.server = server;
269 p->o_arg.bitmask = server->attr_bitmask;
270 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
271 if (flags & O_EXCL) {
272 u32 *s = (u32 *) p->o_arg.u.verifier.data;
273 s[0] = jiffies;
274 s[1] = current->pid;
275 } else if (flags & O_CREAT) {
276 p->o_arg.u.attrs = &p->attrs;
277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
278 }
279 p->c_arg.fh = &p->o_res.fh;
280 p->c_arg.stateid = &p->o_res.stateid;
281 p->c_arg.seqid = p->o_arg.seqid;
282 nfs4_init_opendata_res(p);
283 kref_init(&p->kref);
284 return p;
285 err_free:
286 kfree(p);
287 err:
288 dput(parent);
289 return NULL;
290 }
291
292 static void nfs4_opendata_free(struct kref *kref)
293 {
294 struct nfs4_opendata *p = container_of(kref,
295 struct nfs4_opendata, kref);
296
297 nfs_free_seqid(p->o_arg.seqid);
298 nfs4_put_state_owner(p->owner);
299 dput(p->dir);
300 dput(p->path.dentry);
301 mntput(p->path.mnt);
302 kfree(p);
303 }
304
305 static void nfs4_opendata_put(struct nfs4_opendata *p)
306 {
307 if (p != NULL)
308 kref_put(&p->kref, nfs4_opendata_free);
309 }
310
311 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
312 {
313 sigset_t oldset;
314 int ret;
315
316 rpc_clnt_sigmask(task->tk_client, &oldset);
317 ret = rpc_wait_for_completion_task(task);
318 rpc_clnt_sigunmask(task->tk_client, &oldset);
319 return ret;
320 }
321
322 static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
323 {
324 switch (open_flags) {
325 case FMODE_WRITE:
326 state->n_wronly++;
327 break;
328 case FMODE_READ:
329 state->n_rdonly++;
330 break;
331 case FMODE_READ|FMODE_WRITE:
332 state->n_rdwr++;
333 }
334 }
335
336 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
337 {
338 struct inode *inode = state->inode;
339
340 open_flags &= (FMODE_READ|FMODE_WRITE);
341 /* Protect against nfs4_find_state_byowner() */
342 spin_lock(&state->owner->so_lock);
343 spin_lock(&inode->i_lock);
344 memcpy(&state->stateid, stateid, sizeof(state->stateid));
345 update_open_stateflags(state, open_flags);
346 nfs4_state_set_mode_locked(state, state->state | open_flags);
347 spin_unlock(&inode->i_lock);
348 spin_unlock(&state->owner->so_lock);
349 }
350
351 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
352 {
353 struct inode *inode;
354 struct nfs4_state *state = NULL;
355
356 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
357 goto out;
358 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
359 if (IS_ERR(inode))
360 goto out;
361 state = nfs4_get_open_state(inode, data->owner);
362 if (state == NULL)
363 goto put_inode;
364 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
365 if (data->o_res.delegation_type != 0) {
366 struct nfs_inode *nfsi = NFS_I(inode);
367 int delegation_flags = 0;
368
369 if (nfsi->delegation)
370 delegation_flags = nfsi->delegation->flags;
371 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
372 nfs_inode_set_delegation(state->inode,
373 data->owner->so_cred,
374 &data->o_res);
375 else
376 nfs_inode_reclaim_delegation(state->inode,
377 data->owner->so_cred,
378 &data->o_res);
379 }
380 put_inode:
381 iput(inode);
382 out:
383 return state;
384 }
385
386 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
387 {
388 struct nfs_inode *nfsi = NFS_I(state->inode);
389 struct nfs_open_context *ctx;
390
391 spin_lock(&state->inode->i_lock);
392 list_for_each_entry(ctx, &nfsi->open_files, list) {
393 if (ctx->state != state)
394 continue;
395 get_nfs_open_context(ctx);
396 spin_unlock(&state->inode->i_lock);
397 return ctx;
398 }
399 spin_unlock(&state->inode->i_lock);
400 return ERR_PTR(-ENOENT);
401 }
402
403 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
404 {
405 struct nfs4_state *newstate;
406 int ret;
407
408 opendata->o_arg.open_flags = openflags;
409 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
410 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
411 nfs4_init_opendata_res(opendata);
412 ret = _nfs4_proc_open(opendata);
413 if (ret != 0)
414 return ret;
415 newstate = nfs4_opendata_to_nfs4_state(opendata);
416 if (newstate != NULL)
417 nfs4_close_state(&opendata->path, newstate, openflags);
418 *res = newstate;
419 return 0;
420 }
421
422 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
423 {
424 struct nfs4_state *newstate;
425 int ret;
426
427 /* memory barrier prior to reading state->n_* */
428 clear_bit(NFS_DELEGATED_STATE, &state->flags);
429 smp_rmb();
430 if (state->n_rdwr != 0) {
431 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
432 if (ret != 0)
433 return ret;
434 if (newstate != state)
435 return -ESTALE;
436 }
437 if (state->n_wronly != 0) {
438 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
439 if (ret != 0)
440 return ret;
441 if (newstate != state)
442 return -ESTALE;
443 }
444 if (state->n_rdonly != 0) {
445 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
446 if (ret != 0)
447 return ret;
448 if (newstate != state)
449 return -ESTALE;
450 }
451 return 0;
452 }
453
454 /*
455 * OPEN_RECLAIM:
456 * reclaim state on the server after a reboot.
457 */
458 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
459 {
460 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
461 struct nfs4_opendata *opendata;
462 int delegation_type = 0;
463 int status;
464
465 if (delegation != NULL) {
466 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
467 memcpy(&state->stateid, &delegation->stateid,
468 sizeof(state->stateid));
469 set_bit(NFS_DELEGATED_STATE, &state->flags);
470 return 0;
471 }
472 delegation_type = delegation->type;
473 }
474 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
475 if (opendata == NULL)
476 return -ENOMEM;
477 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
478 opendata->o_arg.fh = NFS_FH(state->inode);
479 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
480 opendata->o_arg.u.delegation_type = delegation_type;
481 status = nfs4_open_recover(opendata, state);
482 nfs4_opendata_put(opendata);
483 return status;
484 }
485
486 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
487 {
488 struct nfs_server *server = NFS_SERVER(state->inode);
489 struct nfs4_exception exception = { };
490 int err;
491 do {
492 err = _nfs4_do_open_reclaim(ctx, state);
493 if (err != -NFS4ERR_DELAY)
494 break;
495 nfs4_handle_exception(server, err, &exception);
496 } while (exception.retry);
497 return err;
498 }
499
500 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
501 {
502 struct nfs_open_context *ctx;
503 int ret;
504
505 ctx = nfs4_state_find_open_context(state);
506 if (IS_ERR(ctx))
507 return PTR_ERR(ctx);
508 ret = nfs4_do_open_reclaim(ctx, state);
509 put_nfs_open_context(ctx);
510 return ret;
511 }
512
513 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
514 {
515 struct nfs4_state_owner *sp = state->owner;
516 struct nfs4_opendata *opendata;
517 int ret;
518
519 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
520 if (opendata == NULL)
521 return -ENOMEM;
522 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
523 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
524 sizeof(opendata->o_arg.u.delegation.data));
525 ret = nfs4_open_recover(opendata, state);
526 nfs4_opendata_put(opendata);
527 return ret;
528 }
529
530 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
531 {
532 struct nfs4_exception exception = { };
533 struct nfs_server *server = NFS_SERVER(state->inode);
534 int err;
535 do {
536 err = _nfs4_open_delegation_recall(ctx, state, stateid);
537 switch (err) {
538 case 0:
539 return err;
540 case -NFS4ERR_STALE_CLIENTID:
541 case -NFS4ERR_STALE_STATEID:
542 case -NFS4ERR_EXPIRED:
543 /* Don't recall a delegation if it was lost */
544 nfs4_schedule_state_recovery(server->nfs_client);
545 return err;
546 }
547 err = nfs4_handle_exception(server, err, &exception);
548 } while (exception.retry);
549 return err;
550 }
551
552 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
553 {
554 struct nfs4_opendata *data = calldata;
555 struct rpc_message msg = {
556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
557 .rpc_argp = &data->c_arg,
558 .rpc_resp = &data->c_res,
559 .rpc_cred = data->owner->so_cred,
560 };
561 data->timestamp = jiffies;
562 rpc_call_setup(task, &msg, 0);
563 }
564
565 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
566 {
567 struct nfs4_opendata *data = calldata;
568
569 data->rpc_status = task->tk_status;
570 if (RPC_ASSASSINATED(task))
571 return;
572 if (data->rpc_status == 0) {
573 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
574 sizeof(data->o_res.stateid.data));
575 renew_lease(data->o_res.server, data->timestamp);
576 }
577 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
578 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
579 }
580
581 static void nfs4_open_confirm_release(void *calldata)
582 {
583 struct nfs4_opendata *data = calldata;
584 struct nfs4_state *state = NULL;
585
586 /* If this request hasn't been cancelled, do nothing */
587 if (data->cancelled == 0)
588 goto out_free;
589 /* In case of error, no cleanup! */
590 if (data->rpc_status != 0)
591 goto out_free;
592 nfs_confirm_seqid(&data->owner->so_seqid, 0);
593 state = nfs4_opendata_to_nfs4_state(data);
594 if (state != NULL)
595 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
596 out_free:
597 nfs4_opendata_put(data);
598 }
599
600 static const struct rpc_call_ops nfs4_open_confirm_ops = {
601 .rpc_call_prepare = nfs4_open_confirm_prepare,
602 .rpc_call_done = nfs4_open_confirm_done,
603 .rpc_release = nfs4_open_confirm_release,
604 };
605
606 /*
607 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
608 */
609 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
610 {
611 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
612 struct rpc_task *task;
613 int status;
614
615 kref_get(&data->kref);
616 /*
617 * If rpc_run_task() ends up calling ->rpc_release(), we
618 * want to ensure that it takes the 'error' code path.
619 */
620 data->rpc_status = -ENOMEM;
621 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
622 if (IS_ERR(task))
623 return PTR_ERR(task);
624 status = nfs4_wait_for_completion_rpc_task(task);
625 if (status != 0) {
626 data->cancelled = 1;
627 smp_wmb();
628 } else
629 status = data->rpc_status;
630 rpc_put_task(task);
631 return status;
632 }
633
634 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
635 {
636 struct nfs4_opendata *data = calldata;
637 struct nfs4_state_owner *sp = data->owner;
638 struct rpc_message msg = {
639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
640 .rpc_argp = &data->o_arg,
641 .rpc_resp = &data->o_res,
642 .rpc_cred = sp->so_cred,
643 };
644
645 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
646 return;
647 /* Update sequence id. */
648 data->o_arg.id = sp->so_owner_id.id;
649 data->o_arg.clientid = sp->so_client->cl_clientid;
650 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
651 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
652 data->timestamp = jiffies;
653 rpc_call_setup(task, &msg, 0);
654 }
655
656 static void nfs4_open_done(struct rpc_task *task, void *calldata)
657 {
658 struct nfs4_opendata *data = calldata;
659
660 data->rpc_status = task->tk_status;
661 if (RPC_ASSASSINATED(task))
662 return;
663 if (task->tk_status == 0) {
664 switch (data->o_res.f_attr->mode & S_IFMT) {
665 case S_IFREG:
666 break;
667 case S_IFLNK:
668 data->rpc_status = -ELOOP;
669 break;
670 case S_IFDIR:
671 data->rpc_status = -EISDIR;
672 break;
673 default:
674 data->rpc_status = -ENOTDIR;
675 }
676 renew_lease(data->o_res.server, data->timestamp);
677 }
678 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
679 }
680
681 static void nfs4_open_release(void *calldata)
682 {
683 struct nfs4_opendata *data = calldata;
684 struct nfs4_state *state = NULL;
685
686 /* If this request hasn't been cancelled, do nothing */
687 if (data->cancelled == 0)
688 goto out_free;
689 /* In case of error, no cleanup! */
690 if (data->rpc_status != 0)
691 goto out_free;
692 /* In case we need an open_confirm, no cleanup! */
693 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
694 goto out_free;
695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
696 state = nfs4_opendata_to_nfs4_state(data);
697 if (state != NULL)
698 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
699 out_free:
700 nfs4_opendata_put(data);
701 }
702
703 static const struct rpc_call_ops nfs4_open_ops = {
704 .rpc_call_prepare = nfs4_open_prepare,
705 .rpc_call_done = nfs4_open_done,
706 .rpc_release = nfs4_open_release,
707 };
708
709 /*
710 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
711 */
712 static int _nfs4_proc_open(struct nfs4_opendata *data)
713 {
714 struct inode *dir = data->dir->d_inode;
715 struct nfs_server *server = NFS_SERVER(dir);
716 struct nfs_openargs *o_arg = &data->o_arg;
717 struct nfs_openres *o_res = &data->o_res;
718 struct rpc_task *task;
719 int status;
720
721 kref_get(&data->kref);
722 /*
723 * If rpc_run_task() ends up calling ->rpc_release(), we
724 * want to ensure that it takes the 'error' code path.
725 */
726 data->rpc_status = -ENOMEM;
727 data->cancelled = 0;
728 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
729 if (IS_ERR(task))
730 return PTR_ERR(task);
731 status = nfs4_wait_for_completion_rpc_task(task);
732 if (status != 0) {
733 data->cancelled = 1;
734 smp_wmb();
735 } else
736 status = data->rpc_status;
737 rpc_put_task(task);
738 if (status != 0)
739 return status;
740
741 if (o_arg->open_flags & O_CREAT) {
742 update_changeattr(dir, &o_res->cinfo);
743 nfs_post_op_update_inode(dir, o_res->dir_attr);
744 } else
745 nfs_refresh_inode(dir, o_res->dir_attr);
746 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
747 status = _nfs4_proc_open_confirm(data);
748 if (status != 0)
749 return status;
750 }
751 nfs_confirm_seqid(&data->owner->so_seqid, 0);
752 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
753 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
754 return 0;
755 }
756
757 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
758 {
759 struct nfs_access_entry cache;
760 int mask = 0;
761 int status;
762
763 if (openflags & FMODE_READ)
764 mask |= MAY_READ;
765 if (openflags & FMODE_WRITE)
766 mask |= MAY_WRITE;
767 if (openflags & FMODE_EXEC)
768 mask |= MAY_EXEC;
769 status = nfs_access_get_cached(inode, cred, &cache);
770 if (status == 0)
771 goto out;
772
773 /* Be clever: ask server to check for all possible rights */
774 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
775 cache.cred = cred;
776 cache.jiffies = jiffies;
777 status = _nfs4_proc_access(inode, &cache);
778 if (status != 0)
779 return status;
780 nfs_access_add_cache(inode, &cache);
781 out:
782 if ((cache.mask & mask) == mask)
783 return 0;
784 return -EACCES;
785 }
786
787 static int nfs4_recover_expired_lease(struct nfs_server *server)
788 {
789 struct nfs_client *clp = server->nfs_client;
790 int ret;
791
792 for (;;) {
793 ret = nfs4_wait_clnt_recover(server->client, clp);
794 if (ret != 0)
795 return ret;
796 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
797 break;
798 nfs4_schedule_state_recovery(clp);
799 }
800 return 0;
801 }
802
803 /*
804 * OPEN_EXPIRED:
805 * reclaim state on the server after a network partition.
806 * Assumes caller holds the appropriate lock
807 */
808 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
809 {
810 struct inode *inode = state->inode;
811 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
812 struct nfs4_opendata *opendata;
813 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
814 int ret;
815
816 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
817 ret = _nfs4_do_access(inode, ctx->cred, openflags);
818 if (ret < 0)
819 return ret;
820 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
821 set_bit(NFS_DELEGATED_STATE, &state->flags);
822 return 0;
823 }
824 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
825 if (opendata == NULL)
826 return -ENOMEM;
827 ret = nfs4_open_recover(opendata, state);
828 if (ret == -ESTALE) {
829 /* Invalidate the state owner so we don't ever use it again */
830 nfs4_drop_state_owner(state->owner);
831 d_drop(ctx->path.dentry);
832 }
833 nfs4_opendata_put(opendata);
834 return ret;
835 }
836
837 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
838 {
839 struct nfs_server *server = NFS_SERVER(state->inode);
840 struct nfs4_exception exception = { };
841 int err;
842
843 do {
844 err = _nfs4_open_expired(ctx, state);
845 if (err == -NFS4ERR_DELAY)
846 nfs4_handle_exception(server, err, &exception);
847 } while (exception.retry);
848 return err;
849 }
850
851 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
852 {
853 struct nfs_open_context *ctx;
854 int ret;
855
856 ctx = nfs4_state_find_open_context(state);
857 if (IS_ERR(ctx))
858 return PTR_ERR(ctx);
859 ret = nfs4_do_open_expired(ctx, state);
860 put_nfs_open_context(ctx);
861 return ret;
862 }
863
864 /*
865 * Returns a referenced nfs4_state if there is an open delegation on the file
866 */
867 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
868 {
869 struct nfs_delegation *delegation;
870 struct nfs_server *server = NFS_SERVER(inode);
871 struct nfs_client *clp = server->nfs_client;
872 struct nfs_inode *nfsi = NFS_I(inode);
873 struct nfs4_state_owner *sp = NULL;
874 struct nfs4_state *state = NULL;
875 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
876 int err;
877
878 err = -ENOMEM;
879 if (!(sp = nfs4_get_state_owner(server, cred))) {
880 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
881 return err;
882 }
883 err = nfs4_recover_expired_lease(server);
884 if (err != 0)
885 goto out_put_state_owner;
886 /* Protect against reboot recovery - NOTE ORDER! */
887 down_read(&clp->cl_sem);
888 /* Protect against delegation recall */
889 down_read(&nfsi->rwsem);
890 delegation = NFS_I(inode)->delegation;
891 err = -ENOENT;
892 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
893 goto out_err;
894 err = -ENOMEM;
895 state = nfs4_get_open_state(inode, sp);
896 if (state == NULL)
897 goto out_err;
898
899 err = -ENOENT;
900 if ((state->state & open_flags) == open_flags) {
901 spin_lock(&inode->i_lock);
902 update_open_stateflags(state, open_flags);
903 spin_unlock(&inode->i_lock);
904 goto out_ok;
905 } else if (state->state != 0)
906 goto out_put_open_state;
907
908 lock_kernel();
909 err = _nfs4_do_access(inode, cred, open_flags);
910 unlock_kernel();
911 if (err != 0)
912 goto out_put_open_state;
913 set_bit(NFS_DELEGATED_STATE, &state->flags);
914 update_open_stateid(state, &delegation->stateid, open_flags);
915 out_ok:
916 nfs4_put_state_owner(sp);
917 up_read(&nfsi->rwsem);
918 up_read(&clp->cl_sem);
919 *res = state;
920 return 0;
921 out_put_open_state:
922 nfs4_put_open_state(state);
923 out_err:
924 up_read(&nfsi->rwsem);
925 up_read(&clp->cl_sem);
926 if (err != -EACCES)
927 nfs_inode_return_delegation(inode);
928 out_put_state_owner:
929 nfs4_put_state_owner(sp);
930 return err;
931 }
932
933 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
934 {
935 struct nfs4_exception exception = { };
936 struct nfs4_state *res = ERR_PTR(-EIO);
937 int err;
938
939 do {
940 err = _nfs4_open_delegated(inode, flags, cred, &res);
941 if (err == 0)
942 break;
943 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
944 err, &exception));
945 } while (exception.retry);
946 return res;
947 }
948
949 /*
950 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
951 * fields corresponding to attributes that were used to store the verifier.
952 * Make sure we clobber those fields in the later setattr call
953 */
954 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
955 {
956 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
957 !(sattr->ia_valid & ATTR_ATIME_SET))
958 sattr->ia_valid |= ATTR_ATIME;
959
960 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
961 !(sattr->ia_valid & ATTR_MTIME_SET))
962 sattr->ia_valid |= ATTR_MTIME;
963 }
964
965 /*
966 * Returns a referenced nfs4_state
967 */
968 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
969 {
970 struct nfs4_state_owner *sp;
971 struct nfs4_state *state = NULL;
972 struct nfs_server *server = NFS_SERVER(dir);
973 struct nfs_client *clp = server->nfs_client;
974 struct nfs4_opendata *opendata;
975 int status;
976
977 /* Protect against reboot recovery conflicts */
978 status = -ENOMEM;
979 if (!(sp = nfs4_get_state_owner(server, cred))) {
980 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
981 goto out_err;
982 }
983 status = nfs4_recover_expired_lease(server);
984 if (status != 0)
985 goto err_put_state_owner;
986 down_read(&clp->cl_sem);
987 status = -ENOMEM;
988 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
989 if (opendata == NULL)
990 goto err_release_rwsem;
991
992 status = _nfs4_proc_open(opendata);
993 if (status != 0)
994 goto err_opendata_put;
995
996 if (opendata->o_arg.open_flags & O_EXCL)
997 nfs4_exclusive_attrset(opendata, sattr);
998
999 status = -ENOMEM;
1000 state = nfs4_opendata_to_nfs4_state(opendata);
1001 if (state == NULL)
1002 goto err_opendata_put;
1003 nfs4_opendata_put(opendata);
1004 nfs4_put_state_owner(sp);
1005 up_read(&clp->cl_sem);
1006 *res = state;
1007 return 0;
1008 err_opendata_put:
1009 nfs4_opendata_put(opendata);
1010 err_release_rwsem:
1011 up_read(&clp->cl_sem);
1012 err_put_state_owner:
1013 nfs4_put_state_owner(sp);
1014 out_err:
1015 *res = NULL;
1016 return status;
1017 }
1018
1019
1020 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1021 {
1022 struct nfs4_exception exception = { };
1023 struct nfs4_state *res;
1024 int status;
1025
1026 do {
1027 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1028 if (status == 0)
1029 break;
1030 /* NOTE: BAD_SEQID means the server and client disagree about the
1031 * book-keeping w.r.t. state-changing operations
1032 * (OPEN/CLOSE/LOCK/LOCKU...)
1033 * It is actually a sign of a bug on the client or on the server.
1034 *
1035 * If we receive a BAD_SEQID error in the particular case of
1036 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1037 * have unhashed the old state_owner for us, and that we can
1038 * therefore safely retry using a new one. We should still warn
1039 * the user though...
1040 */
1041 if (status == -NFS4ERR_BAD_SEQID) {
1042 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1043 exception.retry = 1;
1044 continue;
1045 }
1046 /*
1047 * BAD_STATEID on OPEN means that the server cancelled our
1048 * state before it received the OPEN_CONFIRM.
1049 * Recover by retrying the request as per the discussion
1050 * on Page 181 of RFC3530.
1051 */
1052 if (status == -NFS4ERR_BAD_STATEID) {
1053 exception.retry = 1;
1054 continue;
1055 }
1056 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1057 status, &exception));
1058 } while (exception.retry);
1059 return res;
1060 }
1061
1062 static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1063 struct iattr *sattr, struct nfs4_state *state)
1064 {
1065 struct nfs_server *server = NFS_SERVER(inode);
1066 struct nfs_setattrargs arg = {
1067 .fh = NFS_FH(inode),
1068 .iap = sattr,
1069 .server = server,
1070 .bitmask = server->attr_bitmask,
1071 };
1072 struct nfs_setattrres res = {
1073 .fattr = fattr,
1074 .server = server,
1075 };
1076 struct rpc_message msg = {
1077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1078 .rpc_argp = &arg,
1079 .rpc_resp = &res,
1080 };
1081 unsigned long timestamp = jiffies;
1082 int status;
1083
1084 nfs_fattr_init(fattr);
1085
1086 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1087 /* Use that stateid */
1088 } else if (state != NULL) {
1089 msg.rpc_cred = state->owner->so_cred;
1090 nfs4_copy_stateid(&arg.stateid, state, current->files);
1091 } else
1092 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1093
1094 status = rpc_call_sync(server->client, &msg, 0);
1095 if (status == 0 && state != NULL)
1096 renew_lease(server, timestamp);
1097 return status;
1098 }
1099
1100 static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1101 struct iattr *sattr, struct nfs4_state *state)
1102 {
1103 struct nfs_server *server = NFS_SERVER(inode);
1104 struct nfs4_exception exception = { };
1105 int err;
1106 do {
1107 err = nfs4_handle_exception(server,
1108 _nfs4_do_setattr(inode, fattr, sattr, state),
1109 &exception);
1110 } while (exception.retry);
1111 return err;
1112 }
1113
1114 struct nfs4_closedata {
1115 struct path path;
1116 struct inode *inode;
1117 struct nfs4_state *state;
1118 struct nfs_closeargs arg;
1119 struct nfs_closeres res;
1120 struct nfs_fattr fattr;
1121 unsigned long timestamp;
1122 };
1123
1124 static void nfs4_free_closedata(void *data)
1125 {
1126 struct nfs4_closedata *calldata = data;
1127 struct nfs4_state_owner *sp = calldata->state->owner;
1128
1129 nfs4_put_open_state(calldata->state);
1130 nfs_free_seqid(calldata->arg.seqid);
1131 nfs4_put_state_owner(sp);
1132 dput(calldata->path.dentry);
1133 mntput(calldata->path.mnt);
1134 kfree(calldata);
1135 }
1136
1137 static void nfs4_close_done(struct rpc_task *task, void *data)
1138 {
1139 struct nfs4_closedata *calldata = data;
1140 struct nfs4_state *state = calldata->state;
1141 struct nfs_server *server = NFS_SERVER(calldata->inode);
1142
1143 if (RPC_ASSASSINATED(task))
1144 return;
1145 /* hmm. we are done with the inode, and in the process of freeing
1146 * the state_owner. we keep this around to process errors
1147 */
1148 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1149 switch (task->tk_status) {
1150 case 0:
1151 memcpy(&state->stateid, &calldata->res.stateid,
1152 sizeof(state->stateid));
1153 renew_lease(server, calldata->timestamp);
1154 break;
1155 case -NFS4ERR_STALE_STATEID:
1156 case -NFS4ERR_EXPIRED:
1157 break;
1158 default:
1159 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1160 rpc_restart_call(task);
1161 return;
1162 }
1163 }
1164 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1165 }
1166
1167 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1168 {
1169 struct nfs4_closedata *calldata = data;
1170 struct nfs4_state *state = calldata->state;
1171 struct rpc_message msg = {
1172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1173 .rpc_argp = &calldata->arg,
1174 .rpc_resp = &calldata->res,
1175 .rpc_cred = state->owner->so_cred,
1176 };
1177 int mode = 0, old_mode;
1178
1179 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1180 return;
1181 /* Recalculate the new open mode in case someone reopened the file
1182 * while we were waiting in line to be scheduled.
1183 */
1184 spin_lock(&state->owner->so_lock);
1185 spin_lock(&calldata->inode->i_lock);
1186 mode = old_mode = state->state;
1187 if (state->n_rdwr == 0) {
1188 if (state->n_rdonly == 0)
1189 mode &= ~FMODE_READ;
1190 if (state->n_wronly == 0)
1191 mode &= ~FMODE_WRITE;
1192 }
1193 nfs4_state_set_mode_locked(state, mode);
1194 spin_unlock(&calldata->inode->i_lock);
1195 spin_unlock(&state->owner->so_lock);
1196 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1197 /* Note: exit _without_ calling nfs4_close_done */
1198 task->tk_action = NULL;
1199 return;
1200 }
1201 nfs_fattr_init(calldata->res.fattr);
1202 if (mode != 0)
1203 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1204 calldata->arg.open_flags = mode;
1205 calldata->timestamp = jiffies;
1206 rpc_call_setup(task, &msg, 0);
1207 }
1208
1209 static const struct rpc_call_ops nfs4_close_ops = {
1210 .rpc_call_prepare = nfs4_close_prepare,
1211 .rpc_call_done = nfs4_close_done,
1212 .rpc_release = nfs4_free_closedata,
1213 };
1214
1215 /*
1216 * It is possible for data to be read/written from a mem-mapped file
1217 * after the sys_close call (which hits the vfs layer as a flush).
1218 * This means that we can't safely call nfsv4 close on a file until
1219 * the inode is cleared. This in turn means that we are not good
1220 * NFSv4 citizens - we do not indicate to the server to update the file's
1221 * share state even when we are done with one of the three share
1222 * stateid's in the inode.
1223 *
1224 * NOTE: Caller must be holding the sp->so_owner semaphore!
1225 */
1226 int nfs4_do_close(struct path *path, struct nfs4_state *state)
1227 {
1228 struct nfs_server *server = NFS_SERVER(state->inode);
1229 struct nfs4_closedata *calldata;
1230 struct nfs4_state_owner *sp = state->owner;
1231 struct rpc_task *task;
1232 int status = -ENOMEM;
1233
1234 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1235 if (calldata == NULL)
1236 goto out;
1237 calldata->inode = state->inode;
1238 calldata->state = state;
1239 calldata->arg.fh = NFS_FH(state->inode);
1240 calldata->arg.stateid = &state->stateid;
1241 /* Serialization for the sequence id */
1242 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1243 if (calldata->arg.seqid == NULL)
1244 goto out_free_calldata;
1245 calldata->arg.bitmask = server->attr_bitmask;
1246 calldata->res.fattr = &calldata->fattr;
1247 calldata->res.server = server;
1248 calldata->path.mnt = mntget(path->mnt);
1249 calldata->path.dentry = dget(path->dentry);
1250
1251 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1252 if (IS_ERR(task))
1253 return PTR_ERR(task);
1254 rpc_put_task(task);
1255 return 0;
1256 out_free_calldata:
1257 kfree(calldata);
1258 out:
1259 nfs4_put_open_state(state);
1260 nfs4_put_state_owner(sp);
1261 return status;
1262 }
1263
1264 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1265 {
1266 struct file *filp;
1267 int ret;
1268
1269 /* If the open_intent is for execute, we have an extra check to make */
1270 if (nd->intent.open.flags & FMODE_EXEC) {
1271 ret = _nfs4_do_access(state->inode,
1272 state->owner->so_cred,
1273 nd->intent.open.flags);
1274 if (ret < 0)
1275 goto out_close;
1276 }
1277 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1278 if (!IS_ERR(filp)) {
1279 struct nfs_open_context *ctx;
1280 ctx = (struct nfs_open_context *)filp->private_data;
1281 ctx->state = state;
1282 return 0;
1283 }
1284 ret = PTR_ERR(filp);
1285 out_close:
1286 nfs4_close_state(path, state, nd->intent.open.flags);
1287 return ret;
1288 }
1289
1290 struct dentry *
1291 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1292 {
1293 struct path path = {
1294 .mnt = nd->mnt,
1295 .dentry = dentry,
1296 };
1297 struct iattr attr;
1298 struct rpc_cred *cred;
1299 struct nfs4_state *state;
1300 struct dentry *res;
1301
1302 if (nd->flags & LOOKUP_CREATE) {
1303 attr.ia_mode = nd->intent.open.create_mode;
1304 attr.ia_valid = ATTR_MODE;
1305 if (!IS_POSIXACL(dir))
1306 attr.ia_mode &= ~current->fs->umask;
1307 } else {
1308 attr.ia_valid = 0;
1309 BUG_ON(nd->intent.open.flags & O_CREAT);
1310 }
1311
1312 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1313 if (IS_ERR(cred))
1314 return (struct dentry *)cred;
1315 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1316 put_rpccred(cred);
1317 if (IS_ERR(state)) {
1318 if (PTR_ERR(state) == -ENOENT)
1319 d_add(dentry, NULL);
1320 return (struct dentry *)state;
1321 }
1322 res = d_add_unique(dentry, igrab(state->inode));
1323 if (res != NULL)
1324 dentry = res;
1325 nfs4_intent_set_file(nd, &path, state);
1326 return res;
1327 }
1328
1329 int
1330 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1331 {
1332 struct path path = {
1333 .mnt = nd->mnt,
1334 .dentry = dentry,
1335 };
1336 struct rpc_cred *cred;
1337 struct nfs4_state *state;
1338
1339 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1340 if (IS_ERR(cred))
1341 return PTR_ERR(cred);
1342 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1343 if (IS_ERR(state))
1344 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1345 put_rpccred(cred);
1346 if (IS_ERR(state)) {
1347 switch (PTR_ERR(state)) {
1348 case -EPERM:
1349 case -EACCES:
1350 case -EDQUOT:
1351 case -ENOSPC:
1352 case -EROFS:
1353 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1354 return 1;
1355 default:
1356 goto out_drop;
1357 }
1358 }
1359 if (state->inode == dentry->d_inode) {
1360 nfs4_intent_set_file(nd, &path, state);
1361 return 1;
1362 }
1363 nfs4_close_state(&path, state, openflags);
1364 out_drop:
1365 d_drop(dentry);
1366 return 0;
1367 }
1368
1369
1370 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1371 {
1372 struct nfs4_server_caps_res res = {};
1373 struct rpc_message msg = {
1374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1375 .rpc_argp = fhandle,
1376 .rpc_resp = &res,
1377 };
1378 int status;
1379
1380 status = rpc_call_sync(server->client, &msg, 0);
1381 if (status == 0) {
1382 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1383 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1384 server->caps |= NFS_CAP_ACLS;
1385 if (res.has_links != 0)
1386 server->caps |= NFS_CAP_HARDLINKS;
1387 if (res.has_symlinks != 0)
1388 server->caps |= NFS_CAP_SYMLINKS;
1389 server->acl_bitmask = res.acl_bitmask;
1390 }
1391 return status;
1392 }
1393
1394 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1395 {
1396 struct nfs4_exception exception = { };
1397 int err;
1398 do {
1399 err = nfs4_handle_exception(server,
1400 _nfs4_server_capabilities(server, fhandle),
1401 &exception);
1402 } while (exception.retry);
1403 return err;
1404 }
1405
1406 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1407 struct nfs_fsinfo *info)
1408 {
1409 struct nfs4_lookup_root_arg args = {
1410 .bitmask = nfs4_fattr_bitmap,
1411 };
1412 struct nfs4_lookup_res res = {
1413 .server = server,
1414 .fattr = info->fattr,
1415 .fh = fhandle,
1416 };
1417 struct rpc_message msg = {
1418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1419 .rpc_argp = &args,
1420 .rpc_resp = &res,
1421 };
1422 nfs_fattr_init(info->fattr);
1423 return rpc_call_sync(server->client, &msg, 0);
1424 }
1425
1426 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1427 struct nfs_fsinfo *info)
1428 {
1429 struct nfs4_exception exception = { };
1430 int err;
1431 do {
1432 err = nfs4_handle_exception(server,
1433 _nfs4_lookup_root(server, fhandle, info),
1434 &exception);
1435 } while (exception.retry);
1436 return err;
1437 }
1438
1439 /*
1440 * get the file handle for the "/" directory on the server
1441 */
1442 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1443 struct nfs_fsinfo *info)
1444 {
1445 int status;
1446
1447 status = nfs4_lookup_root(server, fhandle, info);
1448 if (status == 0)
1449 status = nfs4_server_capabilities(server, fhandle);
1450 if (status == 0)
1451 status = nfs4_do_fsinfo(server, fhandle, info);
1452 return nfs4_map_errors(status);
1453 }
1454
1455 /*
1456 * Get locations and (maybe) other attributes of a referral.
1457 * Note that we'll actually follow the referral later when
1458 * we detect fsid mismatch in inode revalidation
1459 */
1460 static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1461 {
1462 int status = -ENOMEM;
1463 struct page *page = NULL;
1464 struct nfs4_fs_locations *locations = NULL;
1465
1466 page = alloc_page(GFP_KERNEL);
1467 if (page == NULL)
1468 goto out;
1469 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1470 if (locations == NULL)
1471 goto out;
1472
1473 status = nfs4_proc_fs_locations(dir, name, locations, page);
1474 if (status != 0)
1475 goto out;
1476 /* Make sure server returned a different fsid for the referral */
1477 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1478 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1479 status = -EIO;
1480 goto out;
1481 }
1482
1483 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1484 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1485 if (!fattr->mode)
1486 fattr->mode = S_IFDIR;
1487 memset(fhandle, 0, sizeof(struct nfs_fh));
1488 out:
1489 if (page)
1490 __free_page(page);
1491 if (locations)
1492 kfree(locations);
1493 return status;
1494 }
1495
1496 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1497 {
1498 struct nfs4_getattr_arg args = {
1499 .fh = fhandle,
1500 .bitmask = server->attr_bitmask,
1501 };
1502 struct nfs4_getattr_res res = {
1503 .fattr = fattr,
1504 .server = server,
1505 };
1506 struct rpc_message msg = {
1507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1508 .rpc_argp = &args,
1509 .rpc_resp = &res,
1510 };
1511
1512 nfs_fattr_init(fattr);
1513 return rpc_call_sync(server->client, &msg, 0);
1514 }
1515
1516 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1517 {
1518 struct nfs4_exception exception = { };
1519 int err;
1520 do {
1521 err = nfs4_handle_exception(server,
1522 _nfs4_proc_getattr(server, fhandle, fattr),
1523 &exception);
1524 } while (exception.retry);
1525 return err;
1526 }
1527
1528 /*
1529 * The file is not closed if it is opened due to the a request to change
1530 * the size of the file. The open call will not be needed once the
1531 * VFS layer lookup-intents are implemented.
1532 *
1533 * Close is called when the inode is destroyed.
1534 * If we haven't opened the file for O_WRONLY, we
1535 * need to in the size_change case to obtain a stateid.
1536 *
1537 * Got race?
1538 * Because OPEN is always done by name in nfsv4, it is
1539 * possible that we opened a different file by the same
1540 * name. We can recognize this race condition, but we
1541 * can't do anything about it besides returning an error.
1542 *
1543 * This will be fixed with VFS changes (lookup-intent).
1544 */
1545 static int
1546 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1547 struct iattr *sattr)
1548 {
1549 struct rpc_cred *cred;
1550 struct inode *inode = dentry->d_inode;
1551 struct nfs_open_context *ctx;
1552 struct nfs4_state *state = NULL;
1553 int status;
1554
1555 nfs_fattr_init(fattr);
1556
1557 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1558 if (IS_ERR(cred))
1559 return PTR_ERR(cred);
1560
1561 /* Search for an existing open(O_WRITE) file */
1562 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1563 if (ctx != NULL)
1564 state = ctx->state;
1565
1566 status = nfs4_do_setattr(inode, fattr, sattr, state);
1567 if (status == 0)
1568 nfs_setattr_update_inode(inode, sattr);
1569 if (ctx != NULL)
1570 put_nfs_open_context(ctx);
1571 put_rpccred(cred);
1572 return status;
1573 }
1574
1575 static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1576 struct qstr *name, struct nfs_fh *fhandle,
1577 struct nfs_fattr *fattr)
1578 {
1579 int status;
1580 struct nfs4_lookup_arg args = {
1581 .bitmask = server->attr_bitmask,
1582 .dir_fh = dirfh,
1583 .name = name,
1584 };
1585 struct nfs4_lookup_res res = {
1586 .server = server,
1587 .fattr = fattr,
1588 .fh = fhandle,
1589 };
1590 struct rpc_message msg = {
1591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1592 .rpc_argp = &args,
1593 .rpc_resp = &res,
1594 };
1595
1596 nfs_fattr_init(fattr);
1597
1598 dprintk("NFS call lookupfh %s\n", name->name);
1599 status = rpc_call_sync(server->client, &msg, 0);
1600 dprintk("NFS reply lookupfh: %d\n", status);
1601 return status;
1602 }
1603
1604 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1605 struct qstr *name, struct nfs_fh *fhandle,
1606 struct nfs_fattr *fattr)
1607 {
1608 struct nfs4_exception exception = { };
1609 int err;
1610 do {
1611 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1612 /* FIXME: !!!! */
1613 if (err == -NFS4ERR_MOVED) {
1614 err = -EREMOTE;
1615 break;
1616 }
1617 err = nfs4_handle_exception(server, err, &exception);
1618 } while (exception.retry);
1619 return err;
1620 }
1621
1622 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1623 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1624 {
1625 int status;
1626
1627 dprintk("NFS call lookup %s\n", name->name);
1628 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1629 if (status == -NFS4ERR_MOVED)
1630 status = nfs4_get_referral(dir, name, fattr, fhandle);
1631 dprintk("NFS reply lookup: %d\n", status);
1632 return status;
1633 }
1634
1635 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1636 {
1637 struct nfs4_exception exception = { };
1638 int err;
1639 do {
1640 err = nfs4_handle_exception(NFS_SERVER(dir),
1641 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1642 &exception);
1643 } while (exception.retry);
1644 return err;
1645 }
1646
1647 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1648 {
1649 struct nfs4_accessargs args = {
1650 .fh = NFS_FH(inode),
1651 };
1652 struct nfs4_accessres res = { 0 };
1653 struct rpc_message msg = {
1654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1655 .rpc_argp = &args,
1656 .rpc_resp = &res,
1657 .rpc_cred = entry->cred,
1658 };
1659 int mode = entry->mask;
1660 int status;
1661
1662 /*
1663 * Determine which access bits we want to ask for...
1664 */
1665 if (mode & MAY_READ)
1666 args.access |= NFS4_ACCESS_READ;
1667 if (S_ISDIR(inode->i_mode)) {
1668 if (mode & MAY_WRITE)
1669 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1670 if (mode & MAY_EXEC)
1671 args.access |= NFS4_ACCESS_LOOKUP;
1672 } else {
1673 if (mode & MAY_WRITE)
1674 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1675 if (mode & MAY_EXEC)
1676 args.access |= NFS4_ACCESS_EXECUTE;
1677 }
1678 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1679 if (!status) {
1680 entry->mask = 0;
1681 if (res.access & NFS4_ACCESS_READ)
1682 entry->mask |= MAY_READ;
1683 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1684 entry->mask |= MAY_WRITE;
1685 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1686 entry->mask |= MAY_EXEC;
1687 }
1688 return status;
1689 }
1690
1691 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1692 {
1693 struct nfs4_exception exception = { };
1694 int err;
1695 do {
1696 err = nfs4_handle_exception(NFS_SERVER(inode),
1697 _nfs4_proc_access(inode, entry),
1698 &exception);
1699 } while (exception.retry);
1700 return err;
1701 }
1702
1703 /*
1704 * TODO: For the time being, we don't try to get any attributes
1705 * along with any of the zero-copy operations READ, READDIR,
1706 * READLINK, WRITE.
1707 *
1708 * In the case of the first three, we want to put the GETATTR
1709 * after the read-type operation -- this is because it is hard
1710 * to predict the length of a GETATTR response in v4, and thus
1711 * align the READ data correctly. This means that the GETATTR
1712 * may end up partially falling into the page cache, and we should
1713 * shift it into the 'tail' of the xdr_buf before processing.
1714 * To do this efficiently, we need to know the total length
1715 * of data received, which doesn't seem to be available outside
1716 * of the RPC layer.
1717 *
1718 * In the case of WRITE, we also want to put the GETATTR after
1719 * the operation -- in this case because we want to make sure
1720 * we get the post-operation mtime and size. This means that
1721 * we can't use xdr_encode_pages() as written: we need a variant
1722 * of it which would leave room in the 'tail' iovec.
1723 *
1724 * Both of these changes to the XDR layer would in fact be quite
1725 * minor, but I decided to leave them for a subsequent patch.
1726 */
1727 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1728 unsigned int pgbase, unsigned int pglen)
1729 {
1730 struct nfs4_readlink args = {
1731 .fh = NFS_FH(inode),
1732 .pgbase = pgbase,
1733 .pglen = pglen,
1734 .pages = &page,
1735 };
1736 struct rpc_message msg = {
1737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1738 .rpc_argp = &args,
1739 .rpc_resp = NULL,
1740 };
1741
1742 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1743 }
1744
1745 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1746 unsigned int pgbase, unsigned int pglen)
1747 {
1748 struct nfs4_exception exception = { };
1749 int err;
1750 do {
1751 err = nfs4_handle_exception(NFS_SERVER(inode),
1752 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1753 &exception);
1754 } while (exception.retry);
1755 return err;
1756 }
1757
1758 /*
1759 * Got race?
1760 * We will need to arrange for the VFS layer to provide an atomic open.
1761 * Until then, this create/open method is prone to inefficiency and race
1762 * conditions due to the lookup, create, and open VFS calls from sys_open()
1763 * placed on the wire.
1764 *
1765 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1766 * The file will be opened again in the subsequent VFS open call
1767 * (nfs4_proc_file_open).
1768 *
1769 * The open for read will just hang around to be used by any process that
1770 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1771 */
1772
1773 static int
1774 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1775 int flags, struct nameidata *nd)
1776 {
1777 struct path path = {
1778 .mnt = nd->mnt,
1779 .dentry = dentry,
1780 };
1781 struct nfs4_state *state;
1782 struct rpc_cred *cred;
1783 int status = 0;
1784
1785 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
1786 if (IS_ERR(cred)) {
1787 status = PTR_ERR(cred);
1788 goto out;
1789 }
1790 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1791 put_rpccred(cred);
1792 if (IS_ERR(state)) {
1793 status = PTR_ERR(state);
1794 goto out;
1795 }
1796 d_instantiate(dentry, igrab(state->inode));
1797 if (flags & O_EXCL) {
1798 struct nfs_fattr fattr;
1799 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
1800 if (status == 0)
1801 nfs_setattr_update_inode(state->inode, sattr);
1802 nfs_post_op_update_inode(state->inode, &fattr);
1803 }
1804 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1805 status = nfs4_intent_set_file(nd, &path, state);
1806 else
1807 nfs4_close_state(&path, state, flags);
1808 out:
1809 return status;
1810 }
1811
1812 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1813 {
1814 struct nfs_server *server = NFS_SERVER(dir);
1815 struct nfs4_remove_arg args = {
1816 .fh = NFS_FH(dir),
1817 .name = name,
1818 .bitmask = server->attr_bitmask,
1819 };
1820 struct nfs_fattr dir_attr;
1821 struct nfs4_remove_res res = {
1822 .server = server,
1823 .dir_attr = &dir_attr,
1824 };
1825 struct rpc_message msg = {
1826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1827 .rpc_argp = &args,
1828 .rpc_resp = &res,
1829 };
1830 int status;
1831
1832 nfs_fattr_init(res.dir_attr);
1833 status = rpc_call_sync(server->client, &msg, 0);
1834 if (status == 0) {
1835 update_changeattr(dir, &res.cinfo);
1836 nfs_post_op_update_inode(dir, res.dir_attr);
1837 }
1838 return status;
1839 }
1840
1841 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1842 {
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
1846 err = nfs4_handle_exception(NFS_SERVER(dir),
1847 _nfs4_proc_remove(dir, name),
1848 &exception);
1849 } while (exception.retry);
1850 return err;
1851 }
1852
1853 struct unlink_desc {
1854 struct nfs4_remove_arg args;
1855 struct nfs4_remove_res res;
1856 struct nfs_fattr dir_attr;
1857 };
1858
1859 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1860 struct qstr *name)
1861 {
1862 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1863 struct unlink_desc *up;
1864
1865 up = kmalloc(sizeof(*up), GFP_KERNEL);
1866 if (!up)
1867 return -ENOMEM;
1868
1869 up->args.fh = NFS_FH(dir->d_inode);
1870 up->args.name = name;
1871 up->args.bitmask = server->attr_bitmask;
1872 up->res.server = server;
1873 up->res.dir_attr = &up->dir_attr;
1874
1875 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1876 msg->rpc_argp = &up->args;
1877 msg->rpc_resp = &up->res;
1878 return 0;
1879 }
1880
1881 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1882 {
1883 struct rpc_message *msg = &task->tk_msg;
1884 struct unlink_desc *up;
1885
1886 if (msg->rpc_resp != NULL) {
1887 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1888 update_changeattr(dir->d_inode, &up->res.cinfo);
1889 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1890 kfree(up);
1891 msg->rpc_resp = NULL;
1892 msg->rpc_argp = NULL;
1893 }
1894 return 0;
1895 }
1896
1897 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1898 struct inode *new_dir, struct qstr *new_name)
1899 {
1900 struct nfs_server *server = NFS_SERVER(old_dir);
1901 struct nfs4_rename_arg arg = {
1902 .old_dir = NFS_FH(old_dir),
1903 .new_dir = NFS_FH(new_dir),
1904 .old_name = old_name,
1905 .new_name = new_name,
1906 .bitmask = server->attr_bitmask,
1907 };
1908 struct nfs_fattr old_fattr, new_fattr;
1909 struct nfs4_rename_res res = {
1910 .server = server,
1911 .old_fattr = &old_fattr,
1912 .new_fattr = &new_fattr,
1913 };
1914 struct rpc_message msg = {
1915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1916 .rpc_argp = &arg,
1917 .rpc_resp = &res,
1918 };
1919 int status;
1920
1921 nfs_fattr_init(res.old_fattr);
1922 nfs_fattr_init(res.new_fattr);
1923 status = rpc_call_sync(server->client, &msg, 0);
1924
1925 if (!status) {
1926 update_changeattr(old_dir, &res.old_cinfo);
1927 nfs_post_op_update_inode(old_dir, res.old_fattr);
1928 update_changeattr(new_dir, &res.new_cinfo);
1929 nfs_post_op_update_inode(new_dir, res.new_fattr);
1930 }
1931 return status;
1932 }
1933
1934 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1935 struct inode *new_dir, struct qstr *new_name)
1936 {
1937 struct nfs4_exception exception = { };
1938 int err;
1939 do {
1940 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1941 _nfs4_proc_rename(old_dir, old_name,
1942 new_dir, new_name),
1943 &exception);
1944 } while (exception.retry);
1945 return err;
1946 }
1947
1948 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1949 {
1950 struct nfs_server *server = NFS_SERVER(inode);
1951 struct nfs4_link_arg arg = {
1952 .fh = NFS_FH(inode),
1953 .dir_fh = NFS_FH(dir),
1954 .name = name,
1955 .bitmask = server->attr_bitmask,
1956 };
1957 struct nfs_fattr fattr, dir_attr;
1958 struct nfs4_link_res res = {
1959 .server = server,
1960 .fattr = &fattr,
1961 .dir_attr = &dir_attr,
1962 };
1963 struct rpc_message msg = {
1964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1965 .rpc_argp = &arg,
1966 .rpc_resp = &res,
1967 };
1968 int status;
1969
1970 nfs_fattr_init(res.fattr);
1971 nfs_fattr_init(res.dir_attr);
1972 status = rpc_call_sync(server->client, &msg, 0);
1973 if (!status) {
1974 update_changeattr(dir, &res.cinfo);
1975 nfs_post_op_update_inode(dir, res.dir_attr);
1976 nfs_post_op_update_inode(inode, res.fattr);
1977 }
1978
1979 return status;
1980 }
1981
1982 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1983 {
1984 struct nfs4_exception exception = { };
1985 int err;
1986 do {
1987 err = nfs4_handle_exception(NFS_SERVER(inode),
1988 _nfs4_proc_link(inode, dir, name),
1989 &exception);
1990 } while (exception.retry);
1991 return err;
1992 }
1993
1994 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
1995 struct page *page, unsigned int len, struct iattr *sattr)
1996 {
1997 struct nfs_server *server = NFS_SERVER(dir);
1998 struct nfs_fh fhandle;
1999 struct nfs_fattr fattr, dir_fattr;
2000 struct nfs4_create_arg arg = {
2001 .dir_fh = NFS_FH(dir),
2002 .server = server,
2003 .name = &dentry->d_name,
2004 .attrs = sattr,
2005 .ftype = NF4LNK,
2006 .bitmask = server->attr_bitmask,
2007 };
2008 struct nfs4_create_res res = {
2009 .server = server,
2010 .fh = &fhandle,
2011 .fattr = &fattr,
2012 .dir_fattr = &dir_fattr,
2013 };
2014 struct rpc_message msg = {
2015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2016 .rpc_argp = &arg,
2017 .rpc_resp = &res,
2018 };
2019 int status;
2020
2021 if (len > NFS4_MAXPATHLEN)
2022 return -ENAMETOOLONG;
2023
2024 arg.u.symlink.pages = &page;
2025 arg.u.symlink.len = len;
2026 nfs_fattr_init(&fattr);
2027 nfs_fattr_init(&dir_fattr);
2028
2029 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2030 if (!status) {
2031 update_changeattr(dir, &res.dir_cinfo);
2032 nfs_post_op_update_inode(dir, res.dir_fattr);
2033 status = nfs_instantiate(dentry, &fhandle, &fattr);
2034 }
2035 return status;
2036 }
2037
2038 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2039 struct page *page, unsigned int len, struct iattr *sattr)
2040 {
2041 struct nfs4_exception exception = { };
2042 int err;
2043 do {
2044 err = nfs4_handle_exception(NFS_SERVER(dir),
2045 _nfs4_proc_symlink(dir, dentry, page,
2046 len, sattr),
2047 &exception);
2048 } while (exception.retry);
2049 return err;
2050 }
2051
2052 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2053 struct iattr *sattr)
2054 {
2055 struct nfs_server *server = NFS_SERVER(dir);
2056 struct nfs_fh fhandle;
2057 struct nfs_fattr fattr, dir_fattr;
2058 struct nfs4_create_arg arg = {
2059 .dir_fh = NFS_FH(dir),
2060 .server = server,
2061 .name = &dentry->d_name,
2062 .attrs = sattr,
2063 .ftype = NF4DIR,
2064 .bitmask = server->attr_bitmask,
2065 };
2066 struct nfs4_create_res res = {
2067 .server = server,
2068 .fh = &fhandle,
2069 .fattr = &fattr,
2070 .dir_fattr = &dir_fattr,
2071 };
2072 struct rpc_message msg = {
2073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2074 .rpc_argp = &arg,
2075 .rpc_resp = &res,
2076 };
2077 int status;
2078
2079 nfs_fattr_init(&fattr);
2080 nfs_fattr_init(&dir_fattr);
2081
2082 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2083 if (!status) {
2084 update_changeattr(dir, &res.dir_cinfo);
2085 nfs_post_op_update_inode(dir, res.dir_fattr);
2086 status = nfs_instantiate(dentry, &fhandle, &fattr);
2087 }
2088 return status;
2089 }
2090
2091 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2092 struct iattr *sattr)
2093 {
2094 struct nfs4_exception exception = { };
2095 int err;
2096 do {
2097 err = nfs4_handle_exception(NFS_SERVER(dir),
2098 _nfs4_proc_mkdir(dir, dentry, sattr),
2099 &exception);
2100 } while (exception.retry);
2101 return err;
2102 }
2103
2104 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2105 u64 cookie, struct page *page, unsigned int count, int plus)
2106 {
2107 struct inode *dir = dentry->d_inode;
2108 struct nfs4_readdir_arg args = {
2109 .fh = NFS_FH(dir),
2110 .pages = &page,
2111 .pgbase = 0,
2112 .count = count,
2113 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2114 };
2115 struct nfs4_readdir_res res;
2116 struct rpc_message msg = {
2117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2118 .rpc_argp = &args,
2119 .rpc_resp = &res,
2120 .rpc_cred = cred,
2121 };
2122 int status;
2123
2124 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2125 dentry->d_parent->d_name.name,
2126 dentry->d_name.name,
2127 (unsigned long long)cookie);
2128 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2129 res.pgbase = args.pgbase;
2130 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2131 if (status == 0)
2132 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2133 dprintk("%s: returns %d\n", __FUNCTION__, status);
2134 return status;
2135 }
2136
2137 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2138 u64 cookie, struct page *page, unsigned int count, int plus)
2139 {
2140 struct nfs4_exception exception = { };
2141 int err;
2142 do {
2143 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2144 _nfs4_proc_readdir(dentry, cred, cookie,
2145 page, count, plus),
2146 &exception);
2147 } while (exception.retry);
2148 return err;
2149 }
2150
2151 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2152 struct iattr *sattr, dev_t rdev)
2153 {
2154 struct nfs_server *server = NFS_SERVER(dir);
2155 struct nfs_fh fh;
2156 struct nfs_fattr fattr, dir_fattr;
2157 struct nfs4_create_arg arg = {
2158 .dir_fh = NFS_FH(dir),
2159 .server = server,
2160 .name = &dentry->d_name,
2161 .attrs = sattr,
2162 .bitmask = server->attr_bitmask,
2163 };
2164 struct nfs4_create_res res = {
2165 .server = server,
2166 .fh = &fh,
2167 .fattr = &fattr,
2168 .dir_fattr = &dir_fattr,
2169 };
2170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2172 .rpc_argp = &arg,
2173 .rpc_resp = &res,
2174 };
2175 int status;
2176 int mode = sattr->ia_mode;
2177
2178 nfs_fattr_init(&fattr);
2179 nfs_fattr_init(&dir_fattr);
2180
2181 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2182 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2183 if (S_ISFIFO(mode))
2184 arg.ftype = NF4FIFO;
2185 else if (S_ISBLK(mode)) {
2186 arg.ftype = NF4BLK;
2187 arg.u.device.specdata1 = MAJOR(rdev);
2188 arg.u.device.specdata2 = MINOR(rdev);
2189 }
2190 else if (S_ISCHR(mode)) {
2191 arg.ftype = NF4CHR;
2192 arg.u.device.specdata1 = MAJOR(rdev);
2193 arg.u.device.specdata2 = MINOR(rdev);
2194 }
2195 else
2196 arg.ftype = NF4SOCK;
2197
2198 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2199 if (status == 0) {
2200 update_changeattr(dir, &res.dir_cinfo);
2201 nfs_post_op_update_inode(dir, res.dir_fattr);
2202 status = nfs_instantiate(dentry, &fh, &fattr);
2203 }
2204 return status;
2205 }
2206
2207 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2208 struct iattr *sattr, dev_t rdev)
2209 {
2210 struct nfs4_exception exception = { };
2211 int err;
2212 do {
2213 err = nfs4_handle_exception(NFS_SERVER(dir),
2214 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2215 &exception);
2216 } while (exception.retry);
2217 return err;
2218 }
2219
2220 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsstat *fsstat)
2222 {
2223 struct nfs4_statfs_arg args = {
2224 .fh = fhandle,
2225 .bitmask = server->attr_bitmask,
2226 };
2227 struct rpc_message msg = {
2228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2229 .rpc_argp = &args,
2230 .rpc_resp = fsstat,
2231 };
2232
2233 nfs_fattr_init(fsstat->fattr);
2234 return rpc_call_sync(server->client, &msg, 0);
2235 }
2236
2237 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2238 {
2239 struct nfs4_exception exception = { };
2240 int err;
2241 do {
2242 err = nfs4_handle_exception(server,
2243 _nfs4_proc_statfs(server, fhandle, fsstat),
2244 &exception);
2245 } while (exception.retry);
2246 return err;
2247 }
2248
2249 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2250 struct nfs_fsinfo *fsinfo)
2251 {
2252 struct nfs4_fsinfo_arg args = {
2253 .fh = fhandle,
2254 .bitmask = server->attr_bitmask,
2255 };
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2258 .rpc_argp = &args,
2259 .rpc_resp = fsinfo,
2260 };
2261
2262 return rpc_call_sync(server->client, &msg, 0);
2263 }
2264
2265 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2266 {
2267 struct nfs4_exception exception = { };
2268 int err;
2269
2270 do {
2271 err = nfs4_handle_exception(server,
2272 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2273 &exception);
2274 } while (exception.retry);
2275 return err;
2276 }
2277
2278 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2279 {
2280 nfs_fattr_init(fsinfo->fattr);
2281 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2282 }
2283
2284 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2285 struct nfs_pathconf *pathconf)
2286 {
2287 struct nfs4_pathconf_arg args = {
2288 .fh = fhandle,
2289 .bitmask = server->attr_bitmask,
2290 };
2291 struct rpc_message msg = {
2292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2293 .rpc_argp = &args,
2294 .rpc_resp = pathconf,
2295 };
2296
2297 /* None of the pathconf attributes are mandatory to implement */
2298 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2299 memset(pathconf, 0, sizeof(*pathconf));
2300 return 0;
2301 }
2302
2303 nfs_fattr_init(pathconf->fattr);
2304 return rpc_call_sync(server->client, &msg, 0);
2305 }
2306
2307 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2308 struct nfs_pathconf *pathconf)
2309 {
2310 struct nfs4_exception exception = { };
2311 int err;
2312
2313 do {
2314 err = nfs4_handle_exception(server,
2315 _nfs4_proc_pathconf(server, fhandle, pathconf),
2316 &exception);
2317 } while (exception.retry);
2318 return err;
2319 }
2320
2321 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2322 {
2323 struct nfs_server *server = NFS_SERVER(data->inode);
2324
2325 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2326 rpc_restart_call(task);
2327 return -EAGAIN;
2328 }
2329 if (task->tk_status > 0)
2330 renew_lease(server, data->timestamp);
2331 return 0;
2332 }
2333
2334 static void nfs4_proc_read_setup(struct nfs_read_data *data)
2335 {
2336 struct rpc_message msg = {
2337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2338 .rpc_argp = &data->args,
2339 .rpc_resp = &data->res,
2340 .rpc_cred = data->cred,
2341 };
2342
2343 data->timestamp = jiffies;
2344
2345 rpc_call_setup(&data->task, &msg, 0);
2346 }
2347
2348 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2349 {
2350 struct inode *inode = data->inode;
2351
2352 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2353 rpc_restart_call(task);
2354 return -EAGAIN;
2355 }
2356 if (task->tk_status >= 0) {
2357 renew_lease(NFS_SERVER(inode), data->timestamp);
2358 nfs_post_op_update_inode(inode, data->res.fattr);
2359 }
2360 return 0;
2361 }
2362
2363 static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2364 {
2365 struct rpc_message msg = {
2366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2367 .rpc_argp = &data->args,
2368 .rpc_resp = &data->res,
2369 .rpc_cred = data->cred,
2370 };
2371 struct inode *inode = data->inode;
2372 struct nfs_server *server = NFS_SERVER(inode);
2373 int stable;
2374
2375 if (how & FLUSH_STABLE) {
2376 if (!NFS_I(inode)->ncommit)
2377 stable = NFS_FILE_SYNC;
2378 else
2379 stable = NFS_DATA_SYNC;
2380 } else
2381 stable = NFS_UNSTABLE;
2382 data->args.stable = stable;
2383 data->args.bitmask = server->attr_bitmask;
2384 data->res.server = server;
2385
2386 data->timestamp = jiffies;
2387
2388 /* Finalize the task. */
2389 rpc_call_setup(&data->task, &msg, 0);
2390 }
2391
2392 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2393 {
2394 struct inode *inode = data->inode;
2395
2396 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2397 rpc_restart_call(task);
2398 return -EAGAIN;
2399 }
2400 if (task->tk_status >= 0)
2401 nfs_post_op_update_inode(inode, data->res.fattr);
2402 return 0;
2403 }
2404
2405 static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2406 {
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2409 .rpc_argp = &data->args,
2410 .rpc_resp = &data->res,
2411 .rpc_cred = data->cred,
2412 };
2413 struct nfs_server *server = NFS_SERVER(data->inode);
2414
2415 data->args.bitmask = server->attr_bitmask;
2416 data->res.server = server;
2417
2418 rpc_call_setup(&data->task, &msg, 0);
2419 }
2420
2421 /*
2422 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2423 * standalone procedure for queueing an asynchronous RENEW.
2424 */
2425 static void nfs4_renew_done(struct rpc_task *task, void *data)
2426 {
2427 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2428 unsigned long timestamp = (unsigned long)data;
2429
2430 if (task->tk_status < 0) {
2431 switch (task->tk_status) {
2432 case -NFS4ERR_STALE_CLIENTID:
2433 case -NFS4ERR_EXPIRED:
2434 case -NFS4ERR_CB_PATH_DOWN:
2435 nfs4_schedule_state_recovery(clp);
2436 }
2437 return;
2438 }
2439 spin_lock(&clp->cl_lock);
2440 if (time_before(clp->cl_last_renewal,timestamp))
2441 clp->cl_last_renewal = timestamp;
2442 spin_unlock(&clp->cl_lock);
2443 }
2444
2445 static const struct rpc_call_ops nfs4_renew_ops = {
2446 .rpc_call_done = nfs4_renew_done,
2447 };
2448
2449 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2450 {
2451 struct rpc_message msg = {
2452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2453 .rpc_argp = clp,
2454 .rpc_cred = cred,
2455 };
2456
2457 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2458 &nfs4_renew_ops, (void *)jiffies);
2459 }
2460
2461 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2462 {
2463 struct rpc_message msg = {
2464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2465 .rpc_argp = clp,
2466 .rpc_cred = cred,
2467 };
2468 unsigned long now = jiffies;
2469 int status;
2470
2471 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2472 if (status < 0)
2473 return status;
2474 spin_lock(&clp->cl_lock);
2475 if (time_before(clp->cl_last_renewal,now))
2476 clp->cl_last_renewal = now;
2477 spin_unlock(&clp->cl_lock);
2478 return 0;
2479 }
2480
2481 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2482 {
2483 return (server->caps & NFS_CAP_ACLS)
2484 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2485 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2486 }
2487
2488 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2489 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2490 * the stack.
2491 */
2492 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2493
2494 static void buf_to_pages(const void *buf, size_t buflen,
2495 struct page **pages, unsigned int *pgbase)
2496 {
2497 const void *p = buf;
2498
2499 *pgbase = offset_in_page(buf);
2500 p -= *pgbase;
2501 while (p < buf + buflen) {
2502 *(pages++) = virt_to_page(p);
2503 p += PAGE_CACHE_SIZE;
2504 }
2505 }
2506
2507 struct nfs4_cached_acl {
2508 int cached;
2509 size_t len;
2510 char data[0];
2511 };
2512
2513 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2514 {
2515 struct nfs_inode *nfsi = NFS_I(inode);
2516
2517 spin_lock(&inode->i_lock);
2518 kfree(nfsi->nfs4_acl);
2519 nfsi->nfs4_acl = acl;
2520 spin_unlock(&inode->i_lock);
2521 }
2522
2523 static void nfs4_zap_acl_attr(struct inode *inode)
2524 {
2525 nfs4_set_cached_acl(inode, NULL);
2526 }
2527
2528 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2529 {
2530 struct nfs_inode *nfsi = NFS_I(inode);
2531 struct nfs4_cached_acl *acl;
2532 int ret = -ENOENT;
2533
2534 spin_lock(&inode->i_lock);
2535 acl = nfsi->nfs4_acl;
2536 if (acl == NULL)
2537 goto out;
2538 if (buf == NULL) /* user is just asking for length */
2539 goto out_len;
2540 if (acl->cached == 0)
2541 goto out;
2542 ret = -ERANGE; /* see getxattr(2) man page */
2543 if (acl->len > buflen)
2544 goto out;
2545 memcpy(buf, acl->data, acl->len);
2546 out_len:
2547 ret = acl->len;
2548 out:
2549 spin_unlock(&inode->i_lock);
2550 return ret;
2551 }
2552
2553 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2554 {
2555 struct nfs4_cached_acl *acl;
2556
2557 if (buf && acl_len <= PAGE_SIZE) {
2558 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2559 if (acl == NULL)
2560 goto out;
2561 acl->cached = 1;
2562 memcpy(acl->data, buf, acl_len);
2563 } else {
2564 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2565 if (acl == NULL)
2566 goto out;
2567 acl->cached = 0;
2568 }
2569 acl->len = acl_len;
2570 out:
2571 nfs4_set_cached_acl(inode, acl);
2572 }
2573
2574 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2575 {
2576 struct page *pages[NFS4ACL_MAXPAGES];
2577 struct nfs_getaclargs args = {
2578 .fh = NFS_FH(inode),
2579 .acl_pages = pages,
2580 .acl_len = buflen,
2581 };
2582 size_t resp_len = buflen;
2583 void *resp_buf;
2584 struct rpc_message msg = {
2585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2586 .rpc_argp = &args,
2587 .rpc_resp = &resp_len,
2588 };
2589 struct page *localpage = NULL;
2590 int ret;
2591
2592 if (buflen < PAGE_SIZE) {
2593 /* As long as we're doing a round trip to the server anyway,
2594 * let's be prepared for a page of acl data. */
2595 localpage = alloc_page(GFP_KERNEL);
2596 resp_buf = page_address(localpage);
2597 if (localpage == NULL)
2598 return -ENOMEM;
2599 args.acl_pages[0] = localpage;
2600 args.acl_pgbase = 0;
2601 resp_len = args.acl_len = PAGE_SIZE;
2602 } else {
2603 resp_buf = buf;
2604 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2605 }
2606 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2607 if (ret)
2608 goto out_free;
2609 if (resp_len > args.acl_len)
2610 nfs4_write_cached_acl(inode, NULL, resp_len);
2611 else
2612 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2613 if (buf) {
2614 ret = -ERANGE;
2615 if (resp_len > buflen)
2616 goto out_free;
2617 if (localpage)
2618 memcpy(buf, resp_buf, resp_len);
2619 }
2620 ret = resp_len;
2621 out_free:
2622 if (localpage)
2623 __free_page(localpage);
2624 return ret;
2625 }
2626
2627 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2628 {
2629 struct nfs4_exception exception = { };
2630 ssize_t ret;
2631 do {
2632 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2633 if (ret >= 0)
2634 break;
2635 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2636 } while (exception.retry);
2637 return ret;
2638 }
2639
2640 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2641 {
2642 struct nfs_server *server = NFS_SERVER(inode);
2643 int ret;
2644
2645 if (!nfs4_server_supports_acls(server))
2646 return -EOPNOTSUPP;
2647 ret = nfs_revalidate_inode(server, inode);
2648 if (ret < 0)
2649 return ret;
2650 ret = nfs4_read_cached_acl(inode, buf, buflen);
2651 if (ret != -ENOENT)
2652 return ret;
2653 return nfs4_get_acl_uncached(inode, buf, buflen);
2654 }
2655
2656 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2657 {
2658 struct nfs_server *server = NFS_SERVER(inode);
2659 struct page *pages[NFS4ACL_MAXPAGES];
2660 struct nfs_setaclargs arg = {
2661 .fh = NFS_FH(inode),
2662 .acl_pages = pages,
2663 .acl_len = buflen,
2664 };
2665 struct rpc_message msg = {
2666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2667 .rpc_argp = &arg,
2668 .rpc_resp = NULL,
2669 };
2670 int ret;
2671
2672 if (!nfs4_server_supports_acls(server))
2673 return -EOPNOTSUPP;
2674 nfs_inode_return_delegation(inode);
2675 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2676 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2677 nfs_zap_caches(inode);
2678 return ret;
2679 }
2680
2681 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2682 {
2683 struct nfs4_exception exception = { };
2684 int err;
2685 do {
2686 err = nfs4_handle_exception(NFS_SERVER(inode),
2687 __nfs4_proc_set_acl(inode, buf, buflen),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691 }
2692
2693 static int
2694 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2695 {
2696 struct nfs_client *clp = server->nfs_client;
2697
2698 if (!clp || task->tk_status >= 0)
2699 return 0;
2700 switch(task->tk_status) {
2701 case -NFS4ERR_STALE_CLIENTID:
2702 case -NFS4ERR_STALE_STATEID:
2703 case -NFS4ERR_EXPIRED:
2704 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2705 nfs4_schedule_state_recovery(clp);
2706 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2707 rpc_wake_up_task(task);
2708 task->tk_status = 0;
2709 return -EAGAIN;
2710 case -NFS4ERR_DELAY:
2711 nfs_inc_server_stats((struct nfs_server *) server,
2712 NFSIOS_DELAY);
2713 case -NFS4ERR_GRACE:
2714 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2715 task->tk_status = 0;
2716 return -EAGAIN;
2717 case -NFS4ERR_OLD_STATEID:
2718 task->tk_status = 0;
2719 return -EAGAIN;
2720 }
2721 task->tk_status = nfs4_map_errors(task->tk_status);
2722 return 0;
2723 }
2724
2725 static int nfs4_wait_bit_interruptible(void *word)
2726 {
2727 if (signal_pending(current))
2728 return -ERESTARTSYS;
2729 schedule();
2730 return 0;
2731 }
2732
2733 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2734 {
2735 sigset_t oldset;
2736 int res;
2737
2738 might_sleep();
2739
2740 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2741
2742 rpc_clnt_sigmask(clnt, &oldset);
2743 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2744 nfs4_wait_bit_interruptible,
2745 TASK_INTERRUPTIBLE);
2746 rpc_clnt_sigunmask(clnt, &oldset);
2747
2748 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2749 return res;
2750 }
2751
2752 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2753 {
2754 sigset_t oldset;
2755 int res = 0;
2756
2757 might_sleep();
2758
2759 if (*timeout <= 0)
2760 *timeout = NFS4_POLL_RETRY_MIN;
2761 if (*timeout > NFS4_POLL_RETRY_MAX)
2762 *timeout = NFS4_POLL_RETRY_MAX;
2763 rpc_clnt_sigmask(clnt, &oldset);
2764 if (clnt->cl_intr) {
2765 schedule_timeout_interruptible(*timeout);
2766 if (signalled())
2767 res = -ERESTARTSYS;
2768 } else
2769 schedule_timeout_uninterruptible(*timeout);
2770 rpc_clnt_sigunmask(clnt, &oldset);
2771 *timeout <<= 1;
2772 return res;
2773 }
2774
2775 /* This is the error handling routine for processes that are allowed
2776 * to sleep.
2777 */
2778 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2779 {
2780 struct nfs_client *clp = server->nfs_client;
2781 int ret = errorcode;
2782
2783 exception->retry = 0;
2784 switch(errorcode) {
2785 case 0:
2786 return 0;
2787 case -NFS4ERR_STALE_CLIENTID:
2788 case -NFS4ERR_STALE_STATEID:
2789 case -NFS4ERR_EXPIRED:
2790 nfs4_schedule_state_recovery(clp);
2791 ret = nfs4_wait_clnt_recover(server->client, clp);
2792 if (ret == 0)
2793 exception->retry = 1;
2794 break;
2795 case -NFS4ERR_FILE_OPEN:
2796 case -NFS4ERR_GRACE:
2797 case -NFS4ERR_DELAY:
2798 ret = nfs4_delay(server->client, &exception->timeout);
2799 if (ret != 0)
2800 break;
2801 case -NFS4ERR_OLD_STATEID:
2802 exception->retry = 1;
2803 }
2804 /* We failed to handle the error */
2805 return nfs4_map_errors(ret);
2806 }
2807
2808 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2809 {
2810 nfs4_verifier sc_verifier;
2811 struct nfs4_setclientid setclientid = {
2812 .sc_verifier = &sc_verifier,
2813 .sc_prog = program,
2814 };
2815 struct rpc_message msg = {
2816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2817 .rpc_argp = &setclientid,
2818 .rpc_resp = clp,
2819 .rpc_cred = cred,
2820 };
2821 __be32 *p;
2822 int loop = 0;
2823 int status;
2824
2825 p = (__be32*)sc_verifier.data;
2826 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2827 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2828
2829 for(;;) {
2830 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2831 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2832 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
2833 cred->cr_ops->cr_name,
2834 clp->cl_id_uniquifier);
2835 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2836 sizeof(setclientid.sc_netid), "tcp");
2837 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2838 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2839 clp->cl_ipaddr, port >> 8, port & 255);
2840
2841 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2842 if (status != -NFS4ERR_CLID_INUSE)
2843 break;
2844 if (signalled())
2845 break;
2846 if (loop++ & 1)
2847 ssleep(clp->cl_lease_time + 1);
2848 else
2849 if (++clp->cl_id_uniquifier == 0)
2850 break;
2851 }
2852 return status;
2853 }
2854
2855 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2856 {
2857 struct nfs_fsinfo fsinfo;
2858 struct rpc_message msg = {
2859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2860 .rpc_argp = clp,
2861 .rpc_resp = &fsinfo,
2862 .rpc_cred = cred,
2863 };
2864 unsigned long now;
2865 int status;
2866
2867 now = jiffies;
2868 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2869 if (status == 0) {
2870 spin_lock(&clp->cl_lock);
2871 clp->cl_lease_time = fsinfo.lease_time * HZ;
2872 clp->cl_last_renewal = now;
2873 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2874 spin_unlock(&clp->cl_lock);
2875 }
2876 return status;
2877 }
2878
2879 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2880 {
2881 long timeout;
2882 int err;
2883 do {
2884 err = _nfs4_proc_setclientid_confirm(clp, cred);
2885 switch (err) {
2886 case 0:
2887 return err;
2888 case -NFS4ERR_RESOURCE:
2889 /* The IBM lawyers misread another document! */
2890 case -NFS4ERR_DELAY:
2891 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2892 }
2893 } while (err == 0);
2894 return err;
2895 }
2896
2897 struct nfs4_delegreturndata {
2898 struct nfs4_delegreturnargs args;
2899 struct nfs4_delegreturnres res;
2900 struct nfs_fh fh;
2901 nfs4_stateid stateid;
2902 struct rpc_cred *cred;
2903 unsigned long timestamp;
2904 struct nfs_fattr fattr;
2905 int rpc_status;
2906 };
2907
2908 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
2909 {
2910 struct nfs4_delegreturndata *data = calldata;
2911 struct rpc_message msg = {
2912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2913 .rpc_argp = &data->args,
2914 .rpc_resp = &data->res,
2915 .rpc_cred = data->cred,
2916 };
2917 nfs_fattr_init(data->res.fattr);
2918 rpc_call_setup(task, &msg, 0);
2919 }
2920
2921 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2922 {
2923 struct nfs4_delegreturndata *data = calldata;
2924 data->rpc_status = task->tk_status;
2925 if (data->rpc_status == 0)
2926 renew_lease(data->res.server, data->timestamp);
2927 }
2928
2929 static void nfs4_delegreturn_release(void *calldata)
2930 {
2931 struct nfs4_delegreturndata *data = calldata;
2932
2933 put_rpccred(data->cred);
2934 kfree(calldata);
2935 }
2936
2937 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2938 .rpc_call_prepare = nfs4_delegreturn_prepare,
2939 .rpc_call_done = nfs4_delegreturn_done,
2940 .rpc_release = nfs4_delegreturn_release,
2941 };
2942
2943 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2944 {
2945 struct nfs4_delegreturndata *data;
2946 struct nfs_server *server = NFS_SERVER(inode);
2947 struct rpc_task *task;
2948 int status;
2949
2950 data = kmalloc(sizeof(*data), GFP_KERNEL);
2951 if (data == NULL)
2952 return -ENOMEM;
2953 data->args.fhandle = &data->fh;
2954 data->args.stateid = &data->stateid;
2955 data->args.bitmask = server->attr_bitmask;
2956 nfs_copy_fh(&data->fh, NFS_FH(inode));
2957 memcpy(&data->stateid, stateid, sizeof(data->stateid));
2958 data->res.fattr = &data->fattr;
2959 data->res.server = server;
2960 data->cred = get_rpccred(cred);
2961 data->timestamp = jiffies;
2962 data->rpc_status = 0;
2963
2964 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2965 if (IS_ERR(task))
2966 return PTR_ERR(task);
2967 status = nfs4_wait_for_completion_rpc_task(task);
2968 if (status == 0) {
2969 status = data->rpc_status;
2970 if (status == 0)
2971 nfs_post_op_update_inode(inode, &data->fattr);
2972 }
2973 rpc_put_task(task);
2974 return status;
2975 }
2976
2977 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2978 {
2979 struct nfs_server *server = NFS_SERVER(inode);
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2984 switch (err) {
2985 case -NFS4ERR_STALE_STATEID:
2986 case -NFS4ERR_EXPIRED:
2987 case 0:
2988 return 0;
2989 }
2990 err = nfs4_handle_exception(server, err, &exception);
2991 } while (exception.retry);
2992 return err;
2993 }
2994
2995 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2996 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2997
2998 /*
2999 * sleep, with exponential backoff, and retry the LOCK operation.
3000 */
3001 static unsigned long
3002 nfs4_set_lock_task_retry(unsigned long timeout)
3003 {
3004 schedule_timeout_interruptible(timeout);
3005 timeout <<= 1;
3006 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3007 return NFS4_LOCK_MAXTIMEOUT;
3008 return timeout;
3009 }
3010
3011 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3012 {
3013 struct inode *inode = state->inode;
3014 struct nfs_server *server = NFS_SERVER(inode);
3015 struct nfs_client *clp = server->nfs_client;
3016 struct nfs_lockt_args arg = {
3017 .fh = NFS_FH(inode),
3018 .fl = request,
3019 };
3020 struct nfs_lockt_res res = {
3021 .denied = request,
3022 };
3023 struct rpc_message msg = {
3024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3025 .rpc_argp = &arg,
3026 .rpc_resp = &res,
3027 .rpc_cred = state->owner->so_cred,
3028 };
3029 struct nfs4_lock_state *lsp;
3030 int status;
3031
3032 down_read(&clp->cl_sem);
3033 arg.lock_owner.clientid = clp->cl_clientid;
3034 status = nfs4_set_lock_state(state, request);
3035 if (status != 0)
3036 goto out;
3037 lsp = request->fl_u.nfs4_fl.owner;
3038 arg.lock_owner.id = lsp->ls_id.id;
3039 status = rpc_call_sync(server->client, &msg, 0);
3040 switch (status) {
3041 case 0:
3042 request->fl_type = F_UNLCK;
3043 break;
3044 case -NFS4ERR_DENIED:
3045 status = 0;
3046 }
3047 request->fl_ops->fl_release_private(request);
3048 out:
3049 up_read(&clp->cl_sem);
3050 return status;
3051 }
3052
3053 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3054 {
3055 struct nfs4_exception exception = { };
3056 int err;
3057
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3060 _nfs4_proc_getlk(state, cmd, request),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064 }
3065
3066 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3067 {
3068 int res = 0;
3069 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3070 case FL_POSIX:
3071 res = posix_lock_file_wait(file, fl);
3072 break;
3073 case FL_FLOCK:
3074 res = flock_lock_file_wait(file, fl);
3075 break;
3076 default:
3077 BUG();
3078 }
3079 return res;
3080 }
3081
3082 struct nfs4_unlockdata {
3083 struct nfs_locku_args arg;
3084 struct nfs_locku_res res;
3085 struct nfs4_lock_state *lsp;
3086 struct nfs_open_context *ctx;
3087 struct file_lock fl;
3088 const struct nfs_server *server;
3089 unsigned long timestamp;
3090 };
3091
3092 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3093 struct nfs_open_context *ctx,
3094 struct nfs4_lock_state *lsp,
3095 struct nfs_seqid *seqid)
3096 {
3097 struct nfs4_unlockdata *p;
3098 struct inode *inode = lsp->ls_state->inode;
3099
3100 p = kmalloc(sizeof(*p), GFP_KERNEL);
3101 if (p == NULL)
3102 return NULL;
3103 p->arg.fh = NFS_FH(inode);
3104 p->arg.fl = &p->fl;
3105 p->arg.seqid = seqid;
3106 p->arg.stateid = &lsp->ls_stateid;
3107 p->lsp = lsp;
3108 atomic_inc(&lsp->ls_count);
3109 /* Ensure we don't close file until we're done freeing locks! */
3110 p->ctx = get_nfs_open_context(ctx);
3111 memcpy(&p->fl, fl, sizeof(p->fl));
3112 p->server = NFS_SERVER(inode);
3113 return p;
3114 }
3115
3116 static void nfs4_locku_release_calldata(void *data)
3117 {
3118 struct nfs4_unlockdata *calldata = data;
3119 nfs_free_seqid(calldata->arg.seqid);
3120 nfs4_put_lock_state(calldata->lsp);
3121 put_nfs_open_context(calldata->ctx);
3122 kfree(calldata);
3123 }
3124
3125 static void nfs4_locku_done(struct rpc_task *task, void *data)
3126 {
3127 struct nfs4_unlockdata *calldata = data;
3128
3129 if (RPC_ASSASSINATED(task))
3130 return;
3131 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
3132 switch (task->tk_status) {
3133 case 0:
3134 memcpy(calldata->lsp->ls_stateid.data,
3135 calldata->res.stateid.data,
3136 sizeof(calldata->lsp->ls_stateid.data));
3137 renew_lease(calldata->server, calldata->timestamp);
3138 break;
3139 case -NFS4ERR_STALE_STATEID:
3140 case -NFS4ERR_EXPIRED:
3141 break;
3142 default:
3143 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3144 rpc_restart_call(task);
3145 }
3146 }
3147
3148 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3149 {
3150 struct nfs4_unlockdata *calldata = data;
3151 struct rpc_message msg = {
3152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3153 .rpc_argp = &calldata->arg,
3154 .rpc_resp = &calldata->res,
3155 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
3156 };
3157
3158 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3159 return;
3160 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3161 /* Note: exit _without_ running nfs4_locku_done */
3162 task->tk_action = NULL;
3163 return;
3164 }
3165 calldata->timestamp = jiffies;
3166 rpc_call_setup(task, &msg, 0);
3167 }
3168
3169 static const struct rpc_call_ops nfs4_locku_ops = {
3170 .rpc_call_prepare = nfs4_locku_prepare,
3171 .rpc_call_done = nfs4_locku_done,
3172 .rpc_release = nfs4_locku_release_calldata,
3173 };
3174
3175 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3176 struct nfs_open_context *ctx,
3177 struct nfs4_lock_state *lsp,
3178 struct nfs_seqid *seqid)
3179 {
3180 struct nfs4_unlockdata *data;
3181
3182 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3183 if (data == NULL) {
3184 nfs_free_seqid(seqid);
3185 return ERR_PTR(-ENOMEM);
3186 }
3187
3188 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3189 }
3190
3191 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3192 {
3193 struct nfs_seqid *seqid;
3194 struct nfs4_lock_state *lsp;
3195 struct rpc_task *task;
3196 int status = 0;
3197
3198 status = nfs4_set_lock_state(state, request);
3199 /* Unlock _before_ we do the RPC call */
3200 request->fl_flags |= FL_EXISTS;
3201 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3202 goto out;
3203 if (status != 0)
3204 goto out;
3205 /* Is this a delegated lock? */
3206 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3207 goto out;
3208 lsp = request->fl_u.nfs4_fl.owner;
3209 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3210 status = -ENOMEM;
3211 if (seqid == NULL)
3212 goto out;
3213 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3214 status = PTR_ERR(task);
3215 if (IS_ERR(task))
3216 goto out;
3217 status = nfs4_wait_for_completion_rpc_task(task);
3218 rpc_put_task(task);
3219 out:
3220 return status;
3221 }
3222
3223 struct nfs4_lockdata {
3224 struct nfs_lock_args arg;
3225 struct nfs_lock_res res;
3226 struct nfs4_lock_state *lsp;
3227 struct nfs_open_context *ctx;
3228 struct file_lock fl;
3229 unsigned long timestamp;
3230 int rpc_status;
3231 int cancelled;
3232 };
3233
3234 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3235 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3236 {
3237 struct nfs4_lockdata *p;
3238 struct inode *inode = lsp->ls_state->inode;
3239 struct nfs_server *server = NFS_SERVER(inode);
3240
3241 p = kzalloc(sizeof(*p), GFP_KERNEL);
3242 if (p == NULL)
3243 return NULL;
3244
3245 p->arg.fh = NFS_FH(inode);
3246 p->arg.fl = &p->fl;
3247 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3248 if (p->arg.lock_seqid == NULL)
3249 goto out_free;
3250 p->arg.lock_stateid = &lsp->ls_stateid;
3251 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3252 p->arg.lock_owner.id = lsp->ls_id.id;
3253 p->lsp = lsp;
3254 atomic_inc(&lsp->ls_count);
3255 p->ctx = get_nfs_open_context(ctx);
3256 memcpy(&p->fl, fl, sizeof(p->fl));
3257 return p;
3258 out_free:
3259 kfree(p);
3260 return NULL;
3261 }
3262
3263 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3264 {
3265 struct nfs4_lockdata *data = calldata;
3266 struct nfs4_state *state = data->lsp->ls_state;
3267 struct nfs4_state_owner *sp = state->owner;
3268 struct rpc_message msg = {
3269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3270 .rpc_argp = &data->arg,
3271 .rpc_resp = &data->res,
3272 .rpc_cred = sp->so_cred,
3273 };
3274
3275 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3276 return;
3277 dprintk("%s: begin!\n", __FUNCTION__);
3278 /* Do we need to do an open_to_lock_owner? */
3279 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3280 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3281 if (data->arg.open_seqid == NULL) {
3282 data->rpc_status = -ENOMEM;
3283 task->tk_action = NULL;
3284 goto out;
3285 }
3286 data->arg.open_stateid = &state->stateid;
3287 data->arg.new_lock_owner = 1;
3288 }
3289 data->timestamp = jiffies;
3290 rpc_call_setup(task, &msg, 0);
3291 out:
3292 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3293 }
3294
3295 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3296 {
3297 struct nfs4_lockdata *data = calldata;
3298
3299 dprintk("%s: begin!\n", __FUNCTION__);
3300
3301 data->rpc_status = task->tk_status;
3302 if (RPC_ASSASSINATED(task))
3303 goto out;
3304 if (data->arg.new_lock_owner != 0) {
3305 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3306 if (data->rpc_status == 0)
3307 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3308 else
3309 goto out;
3310 }
3311 if (data->rpc_status == 0) {
3312 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3313 sizeof(data->lsp->ls_stateid.data));
3314 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3315 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3316 }
3317 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3318 out:
3319 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3320 }
3321
3322 static void nfs4_lock_release(void *calldata)
3323 {
3324 struct nfs4_lockdata *data = calldata;
3325
3326 dprintk("%s: begin!\n", __FUNCTION__);
3327 if (data->arg.open_seqid != NULL)
3328 nfs_free_seqid(data->arg.open_seqid);
3329 if (data->cancelled != 0) {
3330 struct rpc_task *task;
3331 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3332 data->arg.lock_seqid);
3333 if (!IS_ERR(task))
3334 rpc_put_task(task);
3335 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3336 } else
3337 nfs_free_seqid(data->arg.lock_seqid);
3338 nfs4_put_lock_state(data->lsp);
3339 put_nfs_open_context(data->ctx);
3340 kfree(data);
3341 dprintk("%s: done!\n", __FUNCTION__);
3342 }
3343
3344 static const struct rpc_call_ops nfs4_lock_ops = {
3345 .rpc_call_prepare = nfs4_lock_prepare,
3346 .rpc_call_done = nfs4_lock_done,
3347 .rpc_release = nfs4_lock_release,
3348 };
3349
3350 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3351 {
3352 struct nfs4_lockdata *data;
3353 struct rpc_task *task;
3354 int ret;
3355
3356 dprintk("%s: begin!\n", __FUNCTION__);
3357 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3358 fl->fl_u.nfs4_fl.owner);
3359 if (data == NULL)
3360 return -ENOMEM;
3361 if (IS_SETLKW(cmd))
3362 data->arg.block = 1;
3363 if (reclaim != 0)
3364 data->arg.reclaim = 1;
3365 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3366 &nfs4_lock_ops, data);
3367 if (IS_ERR(task))
3368 return PTR_ERR(task);
3369 ret = nfs4_wait_for_completion_rpc_task(task);
3370 if (ret == 0) {
3371 ret = data->rpc_status;
3372 if (ret == -NFS4ERR_DENIED)
3373 ret = -EAGAIN;
3374 } else
3375 data->cancelled = 1;
3376 rpc_put_task(task);
3377 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3378 return ret;
3379 }
3380
3381 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3382 {
3383 struct nfs_server *server = NFS_SERVER(state->inode);
3384 struct nfs4_exception exception = { };
3385 int err;
3386
3387 do {
3388 /* Cache the lock if possible... */
3389 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3390 return 0;
3391 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3392 if (err != -NFS4ERR_DELAY)
3393 break;
3394 nfs4_handle_exception(server, err, &exception);
3395 } while (exception.retry);
3396 return err;
3397 }
3398
3399 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3400 {
3401 struct nfs_server *server = NFS_SERVER(state->inode);
3402 struct nfs4_exception exception = { };
3403 int err;
3404
3405 err = nfs4_set_lock_state(state, request);
3406 if (err != 0)
3407 return err;
3408 do {
3409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3410 return 0;
3411 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3412 if (err != -NFS4ERR_DELAY)
3413 break;
3414 nfs4_handle_exception(server, err, &exception);
3415 } while (exception.retry);
3416 return err;
3417 }
3418
3419 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3420 {
3421 struct nfs_client *clp = state->owner->so_client;
3422 unsigned char fl_flags = request->fl_flags;
3423 int status;
3424
3425 /* Is this a delegated open? */
3426 status = nfs4_set_lock_state(state, request);
3427 if (status != 0)
3428 goto out;
3429 request->fl_flags |= FL_ACCESS;
3430 status = do_vfs_lock(request->fl_file, request);
3431 if (status < 0)
3432 goto out;
3433 down_read(&clp->cl_sem);
3434 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3435 struct nfs_inode *nfsi = NFS_I(state->inode);
3436 /* Yes: cache locks! */
3437 down_read(&nfsi->rwsem);
3438 /* ...but avoid races with delegation recall... */
3439 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3440 request->fl_flags = fl_flags & ~FL_SLEEP;
3441 status = do_vfs_lock(request->fl_file, request);
3442 up_read(&nfsi->rwsem);
3443 goto out_unlock;
3444 }
3445 up_read(&nfsi->rwsem);
3446 }
3447 status = _nfs4_do_setlk(state, cmd, request, 0);
3448 if (status != 0)
3449 goto out_unlock;
3450 /* Note: we always want to sleep here! */
3451 request->fl_flags = fl_flags | FL_SLEEP;
3452 if (do_vfs_lock(request->fl_file, request) < 0)
3453 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3454 out_unlock:
3455 up_read(&clp->cl_sem);
3456 out:
3457 request->fl_flags = fl_flags;
3458 return status;
3459 }
3460
3461 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3462 {
3463 struct nfs4_exception exception = { };
3464 int err;
3465
3466 do {
3467 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3468 _nfs4_proc_setlk(state, cmd, request),
3469 &exception);
3470 } while (exception.retry);
3471 return err;
3472 }
3473
3474 static int
3475 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3476 {
3477 struct nfs_open_context *ctx;
3478 struct nfs4_state *state;
3479 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3480 int status;
3481
3482 /* verify open state */
3483 ctx = (struct nfs_open_context *)filp->private_data;
3484 state = ctx->state;
3485
3486 if (request->fl_start < 0 || request->fl_end < 0)
3487 return -EINVAL;
3488
3489 if (IS_GETLK(cmd))
3490 return nfs4_proc_getlk(state, F_GETLK, request);
3491
3492 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3493 return -EINVAL;
3494
3495 if (request->fl_type == F_UNLCK)
3496 return nfs4_proc_unlck(state, cmd, request);
3497
3498 do {
3499 status = nfs4_proc_setlk(state, cmd, request);
3500 if ((status != -EAGAIN) || IS_SETLK(cmd))
3501 break;
3502 timeout = nfs4_set_lock_task_retry(timeout);
3503 status = -ERESTARTSYS;
3504 if (signalled())
3505 break;
3506 } while(status < 0);
3507 return status;
3508 }
3509
3510 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3511 {
3512 struct nfs_server *server = NFS_SERVER(state->inode);
3513 struct nfs4_exception exception = { };
3514 int err;
3515
3516 err = nfs4_set_lock_state(state, fl);
3517 if (err != 0)
3518 goto out;
3519 do {
3520 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3521 if (err != -NFS4ERR_DELAY)
3522 break;
3523 err = nfs4_handle_exception(server, err, &exception);
3524 } while (exception.retry);
3525 out:
3526 return err;
3527 }
3528
3529 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3530
3531 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3532 size_t buflen, int flags)
3533 {
3534 struct inode *inode = dentry->d_inode;
3535
3536 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3537 return -EOPNOTSUPP;
3538
3539 if (!S_ISREG(inode->i_mode) &&
3540 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3541 return -EPERM;
3542
3543 return nfs4_proc_set_acl(inode, buf, buflen);
3544 }
3545
3546 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3547 * and that's what we'll do for e.g. user attributes that haven't been set.
3548 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3549 * attributes in kernel-managed attribute namespaces. */
3550 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3551 size_t buflen)
3552 {
3553 struct inode *inode = dentry->d_inode;
3554
3555 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3556 return -EOPNOTSUPP;
3557
3558 return nfs4_proc_get_acl(inode, buf, buflen);
3559 }
3560
3561 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3562 {
3563 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3564
3565 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3566 return 0;
3567 if (buf && buflen < len)
3568 return -ERANGE;
3569 if (buf)
3570 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3571 return len;
3572 }
3573
3574 int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
3575 struct nfs4_fs_locations *fs_locations, struct page *page)
3576 {
3577 struct nfs_server *server = NFS_SERVER(dir);
3578 u32 bitmask[2] = {
3579 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3580 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3581 };
3582 struct nfs4_fs_locations_arg args = {
3583 .dir_fh = NFS_FH(dir),
3584 .name = name,
3585 .page = page,
3586 .bitmask = bitmask,
3587 };
3588 struct rpc_message msg = {
3589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3590 .rpc_argp = &args,
3591 .rpc_resp = fs_locations,
3592 };
3593 int status;
3594
3595 dprintk("%s: start\n", __FUNCTION__);
3596 nfs_fattr_init(&fs_locations->fattr);
3597 fs_locations->server = server;
3598 fs_locations->nlocations = 0;
3599 status = rpc_call_sync(server->client, &msg, 0);
3600 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3601 return status;
3602 }
3603
3604 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3605 .recover_open = nfs4_open_reclaim,
3606 .recover_lock = nfs4_lock_reclaim,
3607 };
3608
3609 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3610 .recover_open = nfs4_open_expired,
3611 .recover_lock = nfs4_lock_expired,
3612 };
3613
3614 static const struct inode_operations nfs4_file_inode_operations = {
3615 .permission = nfs_permission,
3616 .getattr = nfs_getattr,
3617 .setattr = nfs_setattr,
3618 .getxattr = nfs4_getxattr,
3619 .setxattr = nfs4_setxattr,
3620 .listxattr = nfs4_listxattr,
3621 };
3622
3623 const struct nfs_rpc_ops nfs_v4_clientops = {
3624 .version = 4, /* protocol version */
3625 .dentry_ops = &nfs4_dentry_operations,
3626 .dir_inode_ops = &nfs4_dir_inode_operations,
3627 .file_inode_ops = &nfs4_file_inode_operations,
3628 .getroot = nfs4_proc_get_root,
3629 .getattr = nfs4_proc_getattr,
3630 .setattr = nfs4_proc_setattr,
3631 .lookupfh = nfs4_proc_lookupfh,
3632 .lookup = nfs4_proc_lookup,
3633 .access = nfs4_proc_access,
3634 .readlink = nfs4_proc_readlink,
3635 .create = nfs4_proc_create,
3636 .remove = nfs4_proc_remove,
3637 .unlink_setup = nfs4_proc_unlink_setup,
3638 .unlink_done = nfs4_proc_unlink_done,
3639 .rename = nfs4_proc_rename,
3640 .link = nfs4_proc_link,
3641 .symlink = nfs4_proc_symlink,
3642 .mkdir = nfs4_proc_mkdir,
3643 .rmdir = nfs4_proc_remove,
3644 .readdir = nfs4_proc_readdir,
3645 .mknod = nfs4_proc_mknod,
3646 .statfs = nfs4_proc_statfs,
3647 .fsinfo = nfs4_proc_fsinfo,
3648 .pathconf = nfs4_proc_pathconf,
3649 .set_capabilities = nfs4_server_capabilities,
3650 .decode_dirent = nfs4_decode_dirent,
3651 .read_setup = nfs4_proc_read_setup,
3652 .read_done = nfs4_read_done,
3653 .write_setup = nfs4_proc_write_setup,
3654 .write_done = nfs4_write_done,
3655 .commit_setup = nfs4_proc_commit_setup,
3656 .commit_done = nfs4_commit_done,
3657 .file_open = nfs_open,
3658 .file_release = nfs_release,
3659 .lock = nfs4_proc_lock,
3660 .clear_acl_cache = nfs4_zap_acl_attr,
3661 };
3662
3663 /*
3664 * Local variables:
3665 * c-basic-offset: 8
3666 * End:
3667 */