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