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