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