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