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