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