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