]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/nfs/nfs4proc.c
NFSv4: Handle NFS4ERR_GRACE when recovering an expired lease.
[karo-tx-linux.git] / 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/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
51
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54 #include "internal.h"
55 #include "iostat.h"
56 #include "callback.h"
57
58 #define NFSDBG_FACILITY         NFSDBG_PROC
59
60 #define NFS4_POLL_RETRY_MIN     (HZ/10)
61 #define NFS4_POLL_RETRY_MAX     (15*HZ)
62
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
64
65 struct nfs4_opendata;
66 static int _nfs4_proc_open(struct nfs4_opendata *data);
67 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
68 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
69 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71
72 /* Prevent leaks of NFSv4 errors into userland */
73 static int nfs4_map_errors(int err)
74 {
75         if (err >= -1000)
76                 return err;
77         switch (err) {
78         case -NFS4ERR_RESOURCE:
79                 return -EREMOTEIO;
80         default:
81                 dprintk("%s could not handle NFSv4 error %d\n",
82                                 __func__, -err);
83                 break;
84         }
85         return -EIO;
86 }
87
88 /*
89  * This is our standard bitmap for GETATTR requests.
90  */
91 const u32 nfs4_fattr_bitmap[2] = {
92         FATTR4_WORD0_TYPE
93         | FATTR4_WORD0_CHANGE
94         | FATTR4_WORD0_SIZE
95         | FATTR4_WORD0_FSID
96         | FATTR4_WORD0_FILEID,
97         FATTR4_WORD1_MODE
98         | FATTR4_WORD1_NUMLINKS
99         | FATTR4_WORD1_OWNER
100         | FATTR4_WORD1_OWNER_GROUP
101         | FATTR4_WORD1_RAWDEV
102         | FATTR4_WORD1_SPACE_USED
103         | FATTR4_WORD1_TIME_ACCESS
104         | FATTR4_WORD1_TIME_METADATA
105         | FATTR4_WORD1_TIME_MODIFY
106 };
107
108 const u32 nfs4_statfs_bitmap[2] = {
109         FATTR4_WORD0_FILES_AVAIL
110         | FATTR4_WORD0_FILES_FREE
111         | FATTR4_WORD0_FILES_TOTAL,
112         FATTR4_WORD1_SPACE_AVAIL
113         | FATTR4_WORD1_SPACE_FREE
114         | FATTR4_WORD1_SPACE_TOTAL
115 };
116
117 const u32 nfs4_pathconf_bitmap[2] = {
118         FATTR4_WORD0_MAXLINK
119         | FATTR4_WORD0_MAXNAME,
120         0
121 };
122
123 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124                         | FATTR4_WORD0_MAXREAD
125                         | FATTR4_WORD0_MAXWRITE
126                         | FATTR4_WORD0_LEASE_TIME,
127                         0
128 };
129
130 const u32 nfs4_fs_locations_bitmap[2] = {
131         FATTR4_WORD0_TYPE
132         | FATTR4_WORD0_CHANGE
133         | FATTR4_WORD0_SIZE
134         | FATTR4_WORD0_FSID
135         | FATTR4_WORD0_FILEID
136         | FATTR4_WORD0_FS_LOCATIONS,
137         FATTR4_WORD1_MODE
138         | FATTR4_WORD1_NUMLINKS
139         | FATTR4_WORD1_OWNER
140         | FATTR4_WORD1_OWNER_GROUP
141         | FATTR4_WORD1_RAWDEV
142         | FATTR4_WORD1_SPACE_USED
143         | FATTR4_WORD1_TIME_ACCESS
144         | FATTR4_WORD1_TIME_METADATA
145         | FATTR4_WORD1_TIME_MODIFY
146         | FATTR4_WORD1_MOUNTED_ON_FILEID
147 };
148
149 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
150                 struct nfs4_readdir_arg *readdir)
151 {
152         __be32 *start, *p;
153
154         BUG_ON(readdir->count < 80);
155         if (cookie > 2) {
156                 readdir->cookie = cookie;
157                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
158                 return;
159         }
160
161         readdir->cookie = 0;
162         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
163         if (cookie == 2)
164                 return;
165         
166         /*
167          * NFSv4 servers do not return entries for '.' and '..'
168          * Therefore, we fake these entries here.  We let '.'
169          * have cookie 0 and '..' have cookie 1.  Note that
170          * when talking to the server, we always send cookie 0
171          * instead of 1 or 2.
172          */
173         start = p = kmap_atomic(*readdir->pages, KM_USER0);
174         
175         if (cookie == 0) {
176                 *p++ = xdr_one;                                  /* next */
177                 *p++ = xdr_zero;                   /* cookie, first word */
178                 *p++ = xdr_one;                   /* cookie, second word */
179                 *p++ = xdr_one;                             /* entry len */
180                 memcpy(p, ".\0\0\0", 4);                        /* entry */
181                 p++;
182                 *p++ = xdr_one;                         /* bitmap length */
183                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
184                 *p++ = htonl(8);              /* attribute buffer length */
185                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
186         }
187         
188         *p++ = xdr_one;                                  /* next */
189         *p++ = xdr_zero;                   /* cookie, first word */
190         *p++ = xdr_two;                   /* cookie, second word */
191         *p++ = xdr_two;                             /* entry len */
192         memcpy(p, "..\0\0", 4);                         /* entry */
193         p++;
194         *p++ = xdr_one;                         /* bitmap length */
195         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
196         *p++ = htonl(8);              /* attribute buffer length */
197         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
198
199         readdir->pgbase = (char *)p - (char *)start;
200         readdir->count -= readdir->pgbase;
201         kunmap_atomic(start, KM_USER0);
202 }
203
204 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
205 {
206         int res;
207
208         might_sleep();
209
210         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
211                         nfs_wait_bit_killable, TASK_KILLABLE);
212         return res;
213 }
214
215 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216 {
217         int res = 0;
218
219         might_sleep();
220
221         if (*timeout <= 0)
222                 *timeout = NFS4_POLL_RETRY_MIN;
223         if (*timeout > NFS4_POLL_RETRY_MAX)
224                 *timeout = NFS4_POLL_RETRY_MAX;
225         schedule_timeout_killable(*timeout);
226         if (fatal_signal_pending(current))
227                 res = -ERESTARTSYS;
228         *timeout <<= 1;
229         return res;
230 }
231
232 /* This is the error handling routine for processes that are allowed
233  * to sleep.
234  */
235 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
236 {
237         struct nfs_client *clp = server->nfs_client;
238         struct nfs4_state *state = exception->state;
239         int ret = errorcode;
240
241         exception->retry = 0;
242         switch(errorcode) {
243                 case 0:
244                         return 0;
245                 case -NFS4ERR_ADMIN_REVOKED:
246                 case -NFS4ERR_BAD_STATEID:
247                 case -NFS4ERR_OPENMODE:
248                         if (state == NULL)
249                                 break;
250                         nfs4_state_mark_reclaim_nograce(clp, state);
251                 case -NFS4ERR_STALE_CLIENTID:
252                 case -NFS4ERR_STALE_STATEID:
253                 case -NFS4ERR_EXPIRED:
254                         nfs4_schedule_state_recovery(clp);
255                         ret = nfs4_wait_clnt_recover(clp);
256                         if (ret == 0)
257                                 exception->retry = 1;
258 #if !defined(CONFIG_NFS_V4_1)
259                         break;
260 #else /* !defined(CONFIG_NFS_V4_1) */
261                         if (!nfs4_has_session(server->nfs_client))
262                                 break;
263                         /* FALLTHROUGH */
264                 case -NFS4ERR_BADSESSION:
265                 case -NFS4ERR_BADSLOT:
266                 case -NFS4ERR_BAD_HIGH_SLOT:
267                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268                 case -NFS4ERR_DEADSESSION:
269                 case -NFS4ERR_SEQ_FALSE_RETRY:
270                 case -NFS4ERR_SEQ_MISORDERED:
271                         dprintk("%s ERROR: %d Reset session\n", __func__,
272                                 errorcode);
273                         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
274                         exception->retry = 1;
275                         /* FALLTHROUGH */
276 #endif /* !defined(CONFIG_NFS_V4_1) */
277                 case -NFS4ERR_FILE_OPEN:
278                 case -NFS4ERR_GRACE:
279                 case -NFS4ERR_DELAY:
280                         ret = nfs4_delay(server->client, &exception->timeout);
281                         if (ret != 0)
282                                 break;
283                 case -NFS4ERR_OLD_STATEID:
284                         exception->retry = 1;
285         }
286         /* We failed to handle the error */
287         return nfs4_map_errors(ret);
288 }
289
290
291 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
292 {
293         struct nfs_client *clp = server->nfs_client;
294         spin_lock(&clp->cl_lock);
295         if (time_before(clp->cl_last_renewal,timestamp))
296                 clp->cl_last_renewal = timestamp;
297         spin_unlock(&clp->cl_lock);
298 }
299
300 #if defined(CONFIG_NFS_V4_1)
301
302 /*
303  * nfs4_free_slot - free a slot and efficiently update slot table.
304  *
305  * freeing a slot is trivially done by clearing its respective bit
306  * in the bitmap.
307  * If the freed slotid equals highest_used_slotid we want to update it
308  * so that the server would be able to size down the slot table if needed,
309  * otherwise we know that the highest_used_slotid is still in use.
310  * When updating highest_used_slotid there may be "holes" in the bitmap
311  * so we need to scan down from highest_used_slotid to 0 looking for the now
312  * highest slotid in use.
313  * If none found, highest_used_slotid is set to -1.
314  */
315 static void
316 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
317 {
318         int slotid = free_slotid;
319
320         spin_lock(&tbl->slot_tbl_lock);
321         /* clear used bit in bitmap */
322         __clear_bit(slotid, tbl->used_slots);
323
324         /* update highest_used_slotid when it is freed */
325         if (slotid == tbl->highest_used_slotid) {
326                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
327                 if (slotid >= 0 && slotid < tbl->max_slots)
328                         tbl->highest_used_slotid = slotid;
329                 else
330                         tbl->highest_used_slotid = -1;
331         }
332         rpc_wake_up_next(&tbl->slot_tbl_waitq);
333         spin_unlock(&tbl->slot_tbl_lock);
334         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
335                 free_slotid, tbl->highest_used_slotid);
336 }
337
338 void nfs41_sequence_free_slot(const struct nfs_client *clp,
339                               struct nfs4_sequence_res *res)
340 {
341         struct nfs4_slot_table *tbl;
342
343         if (!nfs4_has_session(clp)) {
344                 dprintk("%s: No session\n", __func__);
345                 return;
346         }
347         tbl = &clp->cl_session->fc_slot_table;
348         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
349                 dprintk("%s: No slot\n", __func__);
350                 /* just wake up the next guy waiting since
351                  * we may have not consumed a slot after all */
352                 rpc_wake_up_next(&tbl->slot_tbl_waitq);
353                 return;
354         }
355         nfs4_free_slot(tbl, res->sr_slotid);
356         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
357 }
358
359 static void nfs41_sequence_done(struct nfs_client *clp,
360                                 struct nfs4_sequence_res *res,
361                                 int rpc_status)
362 {
363         unsigned long timestamp;
364         struct nfs4_slot_table *tbl;
365         struct nfs4_slot *slot;
366
367         /*
368          * sr_status remains 1 if an RPC level error occurred. The server
369          * may or may not have processed the sequence operation..
370          * Proceed as if the server received and processed the sequence
371          * operation.
372          */
373         if (res->sr_status == 1)
374                 res->sr_status = NFS_OK;
375
376         /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
377         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
378                 goto out;
379
380         tbl = &clp->cl_session->fc_slot_table;
381         slot = tbl->slots + res->sr_slotid;
382
383         if (res->sr_status == 0) {
384                 /* Update the slot's sequence and clientid lease timer */
385                 ++slot->seq_nr;
386                 timestamp = res->sr_renewal_time;
387                 spin_lock(&clp->cl_lock);
388                 if (time_before(clp->cl_last_renewal, timestamp))
389                         clp->cl_last_renewal = timestamp;
390                 spin_unlock(&clp->cl_lock);
391                 return;
392         }
393 out:
394         /* The session may be reset by one of the error handlers. */
395         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
396         nfs41_sequence_free_slot(clp, res);
397 }
398
399 /*
400  * nfs4_find_slot - efficiently look for a free slot
401  *
402  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
403  * If found, we mark the slot as used, update the highest_used_slotid,
404  * and respectively set up the sequence operation args.
405  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
406  *
407  * Note: must be called with under the slot_tbl_lock.
408  */
409 static u8
410 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
411 {
412         int slotid;
413         u8 ret_id = NFS4_MAX_SLOT_TABLE;
414         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
415
416         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
417                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
418                 tbl->max_slots);
419         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
420         if (slotid >= tbl->max_slots)
421                 goto out;
422         __set_bit(slotid, tbl->used_slots);
423         if (slotid > tbl->highest_used_slotid)
424                 tbl->highest_used_slotid = slotid;
425         ret_id = slotid;
426 out:
427         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
428                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
429         return ret_id;
430 }
431
432 static int nfs4_recover_session(struct nfs4_session *session)
433 {
434         struct nfs_client *clp = session->clp;
435         unsigned int loop;
436         int ret;
437
438         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
439                 ret = nfs4_wait_clnt_recover(clp);
440                 if (ret != 0)
441                         break;
442                 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
443                         break;
444                 nfs4_schedule_state_manager(clp);
445                 ret = -EIO;
446         }
447         return ret;
448 }
449
450 static int nfs41_setup_sequence(struct nfs4_session *session,
451                                 struct nfs4_sequence_args *args,
452                                 struct nfs4_sequence_res *res,
453                                 int cache_reply,
454                                 struct rpc_task *task)
455 {
456         struct nfs4_slot *slot;
457         struct nfs4_slot_table *tbl;
458         int status = 0;
459         u8 slotid;
460
461         dprintk("--> %s\n", __func__);
462         /* slot already allocated? */
463         if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
464                 return 0;
465
466         memset(res, 0, sizeof(*res));
467         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
468         tbl = &session->fc_slot_table;
469
470         spin_lock(&tbl->slot_tbl_lock);
471         if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
472                 if (tbl->highest_used_slotid != -1) {
473                         rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
474                         spin_unlock(&tbl->slot_tbl_lock);
475                         dprintk("<-- %s: Session reset: draining\n", __func__);
476                         return -EAGAIN;
477                 }
478
479                 /* The slot table is empty; start the reset thread */
480                 dprintk("%s Session Reset\n", __func__);
481                 spin_unlock(&tbl->slot_tbl_lock);
482                 status = nfs4_recover_session(session);
483                 if (status)
484                         return status;
485                 spin_lock(&tbl->slot_tbl_lock);
486         }
487
488         slotid = nfs4_find_slot(tbl, task);
489         if (slotid == NFS4_MAX_SLOT_TABLE) {
490                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
491                 spin_unlock(&tbl->slot_tbl_lock);
492                 dprintk("<-- %s: no free slots\n", __func__);
493                 return -EAGAIN;
494         }
495         spin_unlock(&tbl->slot_tbl_lock);
496
497         slot = tbl->slots + slotid;
498         args->sa_session = session;
499         args->sa_slotid = slotid;
500         args->sa_cache_this = cache_reply;
501
502         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
503
504         res->sr_session = session;
505         res->sr_slotid = slotid;
506         res->sr_renewal_time = jiffies;
507         /*
508          * sr_status is only set in decode_sequence, and so will remain
509          * set to 1 if an rpc level failure occurs.
510          */
511         res->sr_status = 1;
512         return 0;
513 }
514
515 int nfs4_setup_sequence(struct nfs_client *clp,
516                         struct nfs4_sequence_args *args,
517                         struct nfs4_sequence_res *res,
518                         int cache_reply,
519                         struct rpc_task *task)
520 {
521         int ret = 0;
522
523         dprintk("--> %s clp %p session %p sr_slotid %d\n",
524                 __func__, clp, clp->cl_session, res->sr_slotid);
525
526         if (!nfs4_has_session(clp))
527                 goto out;
528         ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
529                                    task);
530         if (ret != -EAGAIN) {
531                 /* terminate rpc task */
532                 task->tk_status = ret;
533                 task->tk_action = NULL;
534         }
535 out:
536         dprintk("<-- %s status=%d\n", __func__, ret);
537         return ret;
538 }
539
540 struct nfs41_call_sync_data {
541         struct nfs_client *clp;
542         struct nfs4_sequence_args *seq_args;
543         struct nfs4_sequence_res *seq_res;
544         int cache_reply;
545 };
546
547 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
548 {
549         struct nfs41_call_sync_data *data = calldata;
550
551         dprintk("--> %s data->clp->cl_session %p\n", __func__,
552                 data->clp->cl_session);
553         if (nfs4_setup_sequence(data->clp, data->seq_args,
554                                 data->seq_res, data->cache_reply, task))
555                 return;
556         rpc_call_start(task);
557 }
558
559 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
560 {
561         struct nfs41_call_sync_data *data = calldata;
562
563         nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
564         nfs41_sequence_free_slot(data->clp, data->seq_res);
565 }
566
567 struct rpc_call_ops nfs41_call_sync_ops = {
568         .rpc_call_prepare = nfs41_call_sync_prepare,
569         .rpc_call_done = nfs41_call_sync_done,
570 };
571
572 static int nfs4_call_sync_sequence(struct nfs_client *clp,
573                                    struct rpc_clnt *clnt,
574                                    struct rpc_message *msg,
575                                    struct nfs4_sequence_args *args,
576                                    struct nfs4_sequence_res *res,
577                                    int cache_reply)
578 {
579         int ret;
580         struct rpc_task *task;
581         struct nfs41_call_sync_data data = {
582                 .clp = clp,
583                 .seq_args = args,
584                 .seq_res = res,
585                 .cache_reply = cache_reply,
586         };
587         struct rpc_task_setup task_setup = {
588                 .rpc_client = clnt,
589                 .rpc_message = msg,
590                 .callback_ops = &nfs41_call_sync_ops,
591                 .callback_data = &data
592         };
593
594         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
595         task = rpc_run_task(&task_setup);
596         if (IS_ERR(task))
597                 ret = PTR_ERR(task);
598         else {
599                 ret = task->tk_status;
600                 rpc_put_task(task);
601         }
602         return ret;
603 }
604
605 int _nfs4_call_sync_session(struct nfs_server *server,
606                             struct rpc_message *msg,
607                             struct nfs4_sequence_args *args,
608                             struct nfs4_sequence_res *res,
609                             int cache_reply)
610 {
611         return nfs4_call_sync_sequence(server->nfs_client, server->client,
612                                        msg, args, res, cache_reply);
613 }
614
615 #endif /* CONFIG_NFS_V4_1 */
616
617 int _nfs4_call_sync(struct nfs_server *server,
618                     struct rpc_message *msg,
619                     struct nfs4_sequence_args *args,
620                     struct nfs4_sequence_res *res,
621                     int cache_reply)
622 {
623         args->sa_session = res->sr_session = NULL;
624         return rpc_call_sync(server->client, msg, 0);
625 }
626
627 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
628         (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
629                         &(res)->seq_res, (cache_reply))
630
631 static void nfs4_sequence_done(const struct nfs_server *server,
632                                struct nfs4_sequence_res *res, int rpc_status)
633 {
634 #ifdef CONFIG_NFS_V4_1
635         if (nfs4_has_session(server->nfs_client))
636                 nfs41_sequence_done(server->nfs_client, res, rpc_status);
637 #endif /* CONFIG_NFS_V4_1 */
638 }
639
640 /* no restart, therefore free slot here */
641 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
642                                          struct nfs4_sequence_res *res,
643                                          int rpc_status)
644 {
645         nfs4_sequence_done(server, res, rpc_status);
646         nfs4_sequence_free_slot(server->nfs_client, res);
647 }
648
649 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
650 {
651         struct nfs_inode *nfsi = NFS_I(dir);
652
653         spin_lock(&dir->i_lock);
654         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
655         if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
656                 nfs_force_lookup_revalidate(dir);
657         nfsi->change_attr = cinfo->after;
658         spin_unlock(&dir->i_lock);
659 }
660
661 struct nfs4_opendata {
662         struct kref kref;
663         struct nfs_openargs o_arg;
664         struct nfs_openres o_res;
665         struct nfs_open_confirmargs c_arg;
666         struct nfs_open_confirmres c_res;
667         struct nfs_fattr f_attr;
668         struct nfs_fattr dir_attr;
669         struct path path;
670         struct dentry *dir;
671         struct nfs4_state_owner *owner;
672         struct nfs4_state *state;
673         struct iattr attrs;
674         unsigned long timestamp;
675         unsigned int rpc_done : 1;
676         int rpc_status;
677         int cancelled;
678 };
679
680
681 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
682 {
683         p->o_res.f_attr = &p->f_attr;
684         p->o_res.dir_attr = &p->dir_attr;
685         p->o_res.seqid = p->o_arg.seqid;
686         p->c_res.seqid = p->c_arg.seqid;
687         p->o_res.server = p->o_arg.server;
688         nfs_fattr_init(&p->f_attr);
689         nfs_fattr_init(&p->dir_attr);
690         p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
691 }
692
693 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
694                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
695                 const struct iattr *attrs)
696 {
697         struct dentry *parent = dget_parent(path->dentry);
698         struct inode *dir = parent->d_inode;
699         struct nfs_server *server = NFS_SERVER(dir);
700         struct nfs4_opendata *p;
701
702         p = kzalloc(sizeof(*p), GFP_KERNEL);
703         if (p == NULL)
704                 goto err;
705         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
706         if (p->o_arg.seqid == NULL)
707                 goto err_free;
708         p->path.mnt = mntget(path->mnt);
709         p->path.dentry = dget(path->dentry);
710         p->dir = parent;
711         p->owner = sp;
712         atomic_inc(&sp->so_count);
713         p->o_arg.fh = NFS_FH(dir);
714         p->o_arg.open_flags = flags;
715         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
716         p->o_arg.clientid = server->nfs_client->cl_clientid;
717         p->o_arg.id = sp->so_owner_id.id;
718         p->o_arg.name = &p->path.dentry->d_name;
719         p->o_arg.server = server;
720         p->o_arg.bitmask = server->attr_bitmask;
721         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
722         if (flags & O_EXCL) {
723                 u32 *s = (u32 *) p->o_arg.u.verifier.data;
724                 s[0] = jiffies;
725                 s[1] = current->pid;
726         } else if (flags & O_CREAT) {
727                 p->o_arg.u.attrs = &p->attrs;
728                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
729         }
730         p->c_arg.fh = &p->o_res.fh;
731         p->c_arg.stateid = &p->o_res.stateid;
732         p->c_arg.seqid = p->o_arg.seqid;
733         nfs4_init_opendata_res(p);
734         kref_init(&p->kref);
735         return p;
736 err_free:
737         kfree(p);
738 err:
739         dput(parent);
740         return NULL;
741 }
742
743 static void nfs4_opendata_free(struct kref *kref)
744 {
745         struct nfs4_opendata *p = container_of(kref,
746                         struct nfs4_opendata, kref);
747
748         nfs_free_seqid(p->o_arg.seqid);
749         if (p->state != NULL)
750                 nfs4_put_open_state(p->state);
751         nfs4_put_state_owner(p->owner);
752         dput(p->dir);
753         path_put(&p->path);
754         kfree(p);
755 }
756
757 static void nfs4_opendata_put(struct nfs4_opendata *p)
758 {
759         if (p != NULL)
760                 kref_put(&p->kref, nfs4_opendata_free);
761 }
762
763 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
764 {
765         int ret;
766
767         ret = rpc_wait_for_completion_task(task);
768         return ret;
769 }
770
771 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
772 {
773         int ret = 0;
774
775         if (open_mode & O_EXCL)
776                 goto out;
777         switch (mode & (FMODE_READ|FMODE_WRITE)) {
778                 case FMODE_READ:
779                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
780                         break;
781                 case FMODE_WRITE:
782                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
783                         break;
784                 case FMODE_READ|FMODE_WRITE:
785                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
786         }
787 out:
788         return ret;
789 }
790
791 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
792 {
793         if ((delegation->type & fmode) != fmode)
794                 return 0;
795         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
796                 return 0;
797         nfs_mark_delegation_referenced(delegation);
798         return 1;
799 }
800
801 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
802 {
803         switch (fmode) {
804                 case FMODE_WRITE:
805                         state->n_wronly++;
806                         break;
807                 case FMODE_READ:
808                         state->n_rdonly++;
809                         break;
810                 case FMODE_READ|FMODE_WRITE:
811                         state->n_rdwr++;
812         }
813         nfs4_state_set_mode_locked(state, state->state | fmode);
814 }
815
816 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
817 {
818         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
819                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
820         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
821         switch (fmode) {
822                 case FMODE_READ:
823                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
824                         break;
825                 case FMODE_WRITE:
826                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
827                         break;
828                 case FMODE_READ|FMODE_WRITE:
829                         set_bit(NFS_O_RDWR_STATE, &state->flags);
830         }
831 }
832
833 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
834 {
835         write_seqlock(&state->seqlock);
836         nfs_set_open_stateid_locked(state, stateid, fmode);
837         write_sequnlock(&state->seqlock);
838 }
839
840 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
841 {
842         /*
843          * Protect the call to nfs4_state_set_mode_locked and
844          * serialise the stateid update
845          */
846         write_seqlock(&state->seqlock);
847         if (deleg_stateid != NULL) {
848                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
849                 set_bit(NFS_DELEGATED_STATE, &state->flags);
850         }
851         if (open_stateid != NULL)
852                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
853         write_sequnlock(&state->seqlock);
854         spin_lock(&state->owner->so_lock);
855         update_open_stateflags(state, fmode);
856         spin_unlock(&state->owner->so_lock);
857 }
858
859 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
860 {
861         struct nfs_inode *nfsi = NFS_I(state->inode);
862         struct nfs_delegation *deleg_cur;
863         int ret = 0;
864
865         fmode &= (FMODE_READ|FMODE_WRITE);
866
867         rcu_read_lock();
868         deleg_cur = rcu_dereference(nfsi->delegation);
869         if (deleg_cur == NULL)
870                 goto no_delegation;
871
872         spin_lock(&deleg_cur->lock);
873         if (nfsi->delegation != deleg_cur ||
874             (deleg_cur->type & fmode) != fmode)
875                 goto no_delegation_unlock;
876
877         if (delegation == NULL)
878                 delegation = &deleg_cur->stateid;
879         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
880                 goto no_delegation_unlock;
881
882         nfs_mark_delegation_referenced(deleg_cur);
883         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
884         ret = 1;
885 no_delegation_unlock:
886         spin_unlock(&deleg_cur->lock);
887 no_delegation:
888         rcu_read_unlock();
889
890         if (!ret && open_stateid != NULL) {
891                 __update_open_stateid(state, open_stateid, NULL, fmode);
892                 ret = 1;
893         }
894
895         return ret;
896 }
897
898
899 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
900 {
901         struct nfs_delegation *delegation;
902
903         rcu_read_lock();
904         delegation = rcu_dereference(NFS_I(inode)->delegation);
905         if (delegation == NULL || (delegation->type & fmode) == fmode) {
906                 rcu_read_unlock();
907                 return;
908         }
909         rcu_read_unlock();
910         nfs_inode_return_delegation(inode);
911 }
912
913 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
914 {
915         struct nfs4_state *state = opendata->state;
916         struct nfs_inode *nfsi = NFS_I(state->inode);
917         struct nfs_delegation *delegation;
918         int open_mode = opendata->o_arg.open_flags & O_EXCL;
919         fmode_t fmode = opendata->o_arg.fmode;
920         nfs4_stateid stateid;
921         int ret = -EAGAIN;
922
923         for (;;) {
924                 if (can_open_cached(state, fmode, open_mode)) {
925                         spin_lock(&state->owner->so_lock);
926                         if (can_open_cached(state, fmode, open_mode)) {
927                                 update_open_stateflags(state, fmode);
928                                 spin_unlock(&state->owner->so_lock);
929                                 goto out_return_state;
930                         }
931                         spin_unlock(&state->owner->so_lock);
932                 }
933                 rcu_read_lock();
934                 delegation = rcu_dereference(nfsi->delegation);
935                 if (delegation == NULL ||
936                     !can_open_delegated(delegation, fmode)) {
937                         rcu_read_unlock();
938                         break;
939                 }
940                 /* Save the delegation */
941                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
942                 rcu_read_unlock();
943                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
944                 if (ret != 0)
945                         goto out;
946                 ret = -EAGAIN;
947
948                 /* Try to update the stateid using the delegation */
949                 if (update_open_stateid(state, NULL, &stateid, fmode))
950                         goto out_return_state;
951         }
952 out:
953         return ERR_PTR(ret);
954 out_return_state:
955         atomic_inc(&state->count);
956         return state;
957 }
958
959 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
960 {
961         struct inode *inode;
962         struct nfs4_state *state = NULL;
963         struct nfs_delegation *delegation;
964         int ret;
965
966         if (!data->rpc_done) {
967                 state = nfs4_try_open_cached(data);
968                 goto out;
969         }
970
971         ret = -EAGAIN;
972         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
973                 goto err;
974         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
975         ret = PTR_ERR(inode);
976         if (IS_ERR(inode))
977                 goto err;
978         ret = -ENOMEM;
979         state = nfs4_get_open_state(inode, data->owner);
980         if (state == NULL)
981                 goto err_put_inode;
982         if (data->o_res.delegation_type != 0) {
983                 int delegation_flags = 0;
984
985                 rcu_read_lock();
986                 delegation = rcu_dereference(NFS_I(inode)->delegation);
987                 if (delegation)
988                         delegation_flags = delegation->flags;
989                 rcu_read_unlock();
990                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
991                         nfs_inode_set_delegation(state->inode,
992                                         data->owner->so_cred,
993                                         &data->o_res);
994                 else
995                         nfs_inode_reclaim_delegation(state->inode,
996                                         data->owner->so_cred,
997                                         &data->o_res);
998         }
999
1000         update_open_stateid(state, &data->o_res.stateid, NULL,
1001                         data->o_arg.fmode);
1002         iput(inode);
1003 out:
1004         return state;
1005 err_put_inode:
1006         iput(inode);
1007 err:
1008         return ERR_PTR(ret);
1009 }
1010
1011 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1012 {
1013         struct nfs_inode *nfsi = NFS_I(state->inode);
1014         struct nfs_open_context *ctx;
1015
1016         spin_lock(&state->inode->i_lock);
1017         list_for_each_entry(ctx, &nfsi->open_files, list) {
1018                 if (ctx->state != state)
1019                         continue;
1020                 get_nfs_open_context(ctx);
1021                 spin_unlock(&state->inode->i_lock);
1022                 return ctx;
1023         }
1024         spin_unlock(&state->inode->i_lock);
1025         return ERR_PTR(-ENOENT);
1026 }
1027
1028 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1029 {
1030         struct nfs4_opendata *opendata;
1031
1032         opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1033         if (opendata == NULL)
1034                 return ERR_PTR(-ENOMEM);
1035         opendata->state = state;
1036         atomic_inc(&state->count);
1037         return opendata;
1038 }
1039
1040 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1041 {
1042         struct nfs4_state *newstate;
1043         int ret;
1044
1045         opendata->o_arg.open_flags = 0;
1046         opendata->o_arg.fmode = fmode;
1047         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1048         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1049         nfs4_init_opendata_res(opendata);
1050         ret = _nfs4_proc_open(opendata);
1051         if (ret != 0)
1052                 return ret; 
1053         newstate = nfs4_opendata_to_nfs4_state(opendata);
1054         if (IS_ERR(newstate))
1055                 return PTR_ERR(newstate);
1056         nfs4_close_state(&opendata->path, newstate, fmode);
1057         *res = newstate;
1058         return 0;
1059 }
1060
1061 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1062 {
1063         struct nfs4_state *newstate;
1064         int ret;
1065
1066         /* memory barrier prior to reading state->n_* */
1067         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1068         smp_rmb();
1069         if (state->n_rdwr != 0) {
1070                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1071                 if (ret != 0)
1072                         return ret;
1073                 if (newstate != state)
1074                         return -ESTALE;
1075         }
1076         if (state->n_wronly != 0) {
1077                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1078                 if (ret != 0)
1079                         return ret;
1080                 if (newstate != state)
1081                         return -ESTALE;
1082         }
1083         if (state->n_rdonly != 0) {
1084                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1085                 if (ret != 0)
1086                         return ret;
1087                 if (newstate != state)
1088                         return -ESTALE;
1089         }
1090         /*
1091          * We may have performed cached opens for all three recoveries.
1092          * Check if we need to update the current stateid.
1093          */
1094         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1095             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1096                 write_seqlock(&state->seqlock);
1097                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1098                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1099                 write_sequnlock(&state->seqlock);
1100         }
1101         return 0;
1102 }
1103
1104 /*
1105  * OPEN_RECLAIM:
1106  *      reclaim state on the server after a reboot.
1107  */
1108 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1109 {
1110         struct nfs_delegation *delegation;
1111         struct nfs4_opendata *opendata;
1112         fmode_t delegation_type = 0;
1113         int status;
1114
1115         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1116         if (IS_ERR(opendata))
1117                 return PTR_ERR(opendata);
1118         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1119         opendata->o_arg.fh = NFS_FH(state->inode);
1120         rcu_read_lock();
1121         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1122         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1123                 delegation_type = delegation->type;
1124         rcu_read_unlock();
1125         opendata->o_arg.u.delegation_type = delegation_type;
1126         status = nfs4_open_recover(opendata, state);
1127         nfs4_opendata_put(opendata);
1128         return status;
1129 }
1130
1131 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1132 {
1133         struct nfs_server *server = NFS_SERVER(state->inode);
1134         struct nfs4_exception exception = { };
1135         int err;
1136         do {
1137                 err = _nfs4_do_open_reclaim(ctx, state);
1138                 if (err != -NFS4ERR_DELAY)
1139                         break;
1140                 nfs4_handle_exception(server, err, &exception);
1141         } while (exception.retry);
1142         return err;
1143 }
1144
1145 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1146 {
1147         struct nfs_open_context *ctx;
1148         int ret;
1149
1150         ctx = nfs4_state_find_open_context(state);
1151         if (IS_ERR(ctx))
1152                 return PTR_ERR(ctx);
1153         ret = nfs4_do_open_reclaim(ctx, state);
1154         put_nfs_open_context(ctx);
1155         return ret;
1156 }
1157
1158 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1159 {
1160         struct nfs4_opendata *opendata;
1161         int ret;
1162
1163         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1164         if (IS_ERR(opendata))
1165                 return PTR_ERR(opendata);
1166         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1167         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1168                         sizeof(opendata->o_arg.u.delegation.data));
1169         ret = nfs4_open_recover(opendata, state);
1170         nfs4_opendata_put(opendata);
1171         return ret;
1172 }
1173
1174 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1175 {
1176         struct nfs4_exception exception = { };
1177         struct nfs_server *server = NFS_SERVER(state->inode);
1178         int err;
1179         do {
1180                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1181                 switch (err) {
1182                         case 0:
1183                         case -ENOENT:
1184                         case -ESTALE:
1185                                 goto out;
1186                         case -NFS4ERR_STALE_CLIENTID:
1187                         case -NFS4ERR_STALE_STATEID:
1188                         case -NFS4ERR_EXPIRED:
1189                                 /* Don't recall a delegation if it was lost */
1190                                 nfs4_schedule_state_recovery(server->nfs_client);
1191                                 goto out;
1192                         case -ERESTARTSYS:
1193                                 /*
1194                                  * The show must go on: exit, but mark the
1195                                  * stateid as needing recovery.
1196                                  */
1197                         case -NFS4ERR_ADMIN_REVOKED:
1198                         case -NFS4ERR_BAD_STATEID:
1199                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1200                         case -ENOMEM:
1201                                 err = 0;
1202                                 goto out;
1203                 }
1204                 err = nfs4_handle_exception(server, err, &exception);
1205         } while (exception.retry);
1206 out:
1207         return err;
1208 }
1209
1210 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1211 {
1212         struct nfs4_opendata *data = calldata;
1213
1214         data->rpc_status = task->tk_status;
1215         if (RPC_ASSASSINATED(task))
1216                 return;
1217         if (data->rpc_status == 0) {
1218                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1219                                 sizeof(data->o_res.stateid.data));
1220                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1221                 renew_lease(data->o_res.server, data->timestamp);
1222                 data->rpc_done = 1;
1223         }
1224 }
1225
1226 static void nfs4_open_confirm_release(void *calldata)
1227 {
1228         struct nfs4_opendata *data = calldata;
1229         struct nfs4_state *state = NULL;
1230
1231         /* If this request hasn't been cancelled, do nothing */
1232         if (data->cancelled == 0)
1233                 goto out_free;
1234         /* In case of error, no cleanup! */
1235         if (!data->rpc_done)
1236                 goto out_free;
1237         state = nfs4_opendata_to_nfs4_state(data);
1238         if (!IS_ERR(state))
1239                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1240 out_free:
1241         nfs4_opendata_put(data);
1242 }
1243
1244 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1245         .rpc_call_done = nfs4_open_confirm_done,
1246         .rpc_release = nfs4_open_confirm_release,
1247 };
1248
1249 /*
1250  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1251  */
1252 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1253 {
1254         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1255         struct rpc_task *task;
1256         struct  rpc_message msg = {
1257                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1258                 .rpc_argp = &data->c_arg,
1259                 .rpc_resp = &data->c_res,
1260                 .rpc_cred = data->owner->so_cred,
1261         };
1262         struct rpc_task_setup task_setup_data = {
1263                 .rpc_client = server->client,
1264                 .rpc_message = &msg,
1265                 .callback_ops = &nfs4_open_confirm_ops,
1266                 .callback_data = data,
1267                 .workqueue = nfsiod_workqueue,
1268                 .flags = RPC_TASK_ASYNC,
1269         };
1270         int status;
1271
1272         kref_get(&data->kref);
1273         data->rpc_done = 0;
1274         data->rpc_status = 0;
1275         data->timestamp = jiffies;
1276         task = rpc_run_task(&task_setup_data);
1277         if (IS_ERR(task))
1278                 return PTR_ERR(task);
1279         status = nfs4_wait_for_completion_rpc_task(task);
1280         if (status != 0) {
1281                 data->cancelled = 1;
1282                 smp_wmb();
1283         } else
1284                 status = data->rpc_status;
1285         rpc_put_task(task);
1286         return status;
1287 }
1288
1289 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1290 {
1291         struct nfs4_opendata *data = calldata;
1292         struct nfs4_state_owner *sp = data->owner;
1293
1294         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1295                 return;
1296         /*
1297          * Check if we still need to send an OPEN call, or if we can use
1298          * a delegation instead.
1299          */
1300         if (data->state != NULL) {
1301                 struct nfs_delegation *delegation;
1302
1303                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1304                         goto out_no_action;
1305                 rcu_read_lock();
1306                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1307                 if (delegation != NULL &&
1308                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1309                         rcu_read_unlock();
1310                         goto out_no_action;
1311                 }
1312                 rcu_read_unlock();
1313         }
1314         /* Update sequence id. */
1315         data->o_arg.id = sp->so_owner_id.id;
1316         data->o_arg.clientid = sp->so_client->cl_clientid;
1317         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1318                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1319                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1320         }
1321         data->timestamp = jiffies;
1322         if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1323                                 &data->o_arg.seq_args,
1324                                 &data->o_res.seq_res, 1, task))
1325                 return;
1326         rpc_call_start(task);
1327         return;
1328 out_no_action:
1329         task->tk_action = NULL;
1330
1331 }
1332
1333 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1334 {
1335         struct nfs4_opendata *data = calldata;
1336
1337         data->rpc_status = task->tk_status;
1338
1339         nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1340                                      task->tk_status);
1341
1342         if (RPC_ASSASSINATED(task))
1343                 return;
1344         if (task->tk_status == 0) {
1345                 switch (data->o_res.f_attr->mode & S_IFMT) {
1346                         case S_IFREG:
1347                                 break;
1348                         case S_IFLNK:
1349                                 data->rpc_status = -ELOOP;
1350                                 break;
1351                         case S_IFDIR:
1352                                 data->rpc_status = -EISDIR;
1353                                 break;
1354                         default:
1355                                 data->rpc_status = -ENOTDIR;
1356                 }
1357                 renew_lease(data->o_res.server, data->timestamp);
1358                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1359                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1360         }
1361         data->rpc_done = 1;
1362 }
1363
1364 static void nfs4_open_release(void *calldata)
1365 {
1366         struct nfs4_opendata *data = calldata;
1367         struct nfs4_state *state = NULL;
1368
1369         /* If this request hasn't been cancelled, do nothing */
1370         if (data->cancelled == 0)
1371                 goto out_free;
1372         /* In case of error, no cleanup! */
1373         if (data->rpc_status != 0 || !data->rpc_done)
1374                 goto out_free;
1375         /* In case we need an open_confirm, no cleanup! */
1376         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1377                 goto out_free;
1378         state = nfs4_opendata_to_nfs4_state(data);
1379         if (!IS_ERR(state))
1380                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1381 out_free:
1382         nfs4_opendata_put(data);
1383 }
1384
1385 static const struct rpc_call_ops nfs4_open_ops = {
1386         .rpc_call_prepare = nfs4_open_prepare,
1387         .rpc_call_done = nfs4_open_done,
1388         .rpc_release = nfs4_open_release,
1389 };
1390
1391 /*
1392  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1393  */
1394 static int _nfs4_proc_open(struct nfs4_opendata *data)
1395 {
1396         struct inode *dir = data->dir->d_inode;
1397         struct nfs_server *server = NFS_SERVER(dir);
1398         struct nfs_openargs *o_arg = &data->o_arg;
1399         struct nfs_openres *o_res = &data->o_res;
1400         struct rpc_task *task;
1401         struct rpc_message msg = {
1402                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1403                 .rpc_argp = o_arg,
1404                 .rpc_resp = o_res,
1405                 .rpc_cred = data->owner->so_cred,
1406         };
1407         struct rpc_task_setup task_setup_data = {
1408                 .rpc_client = server->client,
1409                 .rpc_message = &msg,
1410                 .callback_ops = &nfs4_open_ops,
1411                 .callback_data = data,
1412                 .workqueue = nfsiod_workqueue,
1413                 .flags = RPC_TASK_ASYNC,
1414         };
1415         int status;
1416
1417         kref_get(&data->kref);
1418         data->rpc_done = 0;
1419         data->rpc_status = 0;
1420         data->cancelled = 0;
1421         task = rpc_run_task(&task_setup_data);
1422         if (IS_ERR(task))
1423                 return PTR_ERR(task);
1424         status = nfs4_wait_for_completion_rpc_task(task);
1425         if (status != 0) {
1426                 data->cancelled = 1;
1427                 smp_wmb();
1428         } else
1429                 status = data->rpc_status;
1430         rpc_put_task(task);
1431         if (status != 0 || !data->rpc_done)
1432                 return status;
1433
1434         if (o_res->fh.size == 0)
1435                 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1436
1437         if (o_arg->open_flags & O_CREAT) {
1438                 update_changeattr(dir, &o_res->cinfo);
1439                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1440         } else
1441                 nfs_refresh_inode(dir, o_res->dir_attr);
1442         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1443                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1444         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1445                 status = _nfs4_proc_open_confirm(data);
1446                 if (status != 0)
1447                         return status;
1448         }
1449         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1450                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1451         return 0;
1452 }
1453
1454 static int nfs4_recover_expired_lease(struct nfs_server *server)
1455 {
1456         struct nfs_client *clp = server->nfs_client;
1457         unsigned int loop;
1458         int ret;
1459
1460         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1461                 ret = nfs4_wait_clnt_recover(clp);
1462                 if (ret != 0)
1463                         break;
1464                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1465                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1466                         break;
1467                 nfs4_schedule_state_recovery(clp);
1468                 ret = -EIO;
1469         }
1470         return ret;
1471 }
1472
1473 /*
1474  * OPEN_EXPIRED:
1475  *      reclaim state on the server after a network partition.
1476  *      Assumes caller holds the appropriate lock
1477  */
1478 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1479 {
1480         struct nfs4_opendata *opendata;
1481         int ret;
1482
1483         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1484         if (IS_ERR(opendata))
1485                 return PTR_ERR(opendata);
1486         ret = nfs4_open_recover(opendata, state);
1487         if (ret == -ESTALE)
1488                 d_drop(ctx->path.dentry);
1489         nfs4_opendata_put(opendata);
1490         return ret;
1491 }
1492
1493 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1494 {
1495         struct nfs_server *server = NFS_SERVER(state->inode);
1496         struct nfs4_exception exception = { };
1497         int err;
1498
1499         do {
1500                 err = _nfs4_open_expired(ctx, state);
1501                 switch (err) {
1502                 default:
1503                         goto out;
1504                 case -NFS4ERR_GRACE:
1505                 case -NFS4ERR_DELAY:
1506                         nfs4_handle_exception(server, err, &exception);
1507                         err = 0;
1508                 }
1509         } while (exception.retry);
1510 out:
1511         return err;
1512 }
1513
1514 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1515 {
1516         struct nfs_open_context *ctx;
1517         int ret;
1518
1519         ctx = nfs4_state_find_open_context(state);
1520         if (IS_ERR(ctx))
1521                 return PTR_ERR(ctx);
1522         ret = nfs4_do_open_expired(ctx, state);
1523         put_nfs_open_context(ctx);
1524         return ret;
1525 }
1526
1527 /*
1528  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1529  * fields corresponding to attributes that were used to store the verifier.
1530  * Make sure we clobber those fields in the later setattr call
1531  */
1532 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1533 {
1534         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1535             !(sattr->ia_valid & ATTR_ATIME_SET))
1536                 sattr->ia_valid |= ATTR_ATIME;
1537
1538         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1539             !(sattr->ia_valid & ATTR_MTIME_SET))
1540                 sattr->ia_valid |= ATTR_MTIME;
1541 }
1542
1543 /*
1544  * Returns a referenced nfs4_state
1545  */
1546 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1547 {
1548         struct nfs4_state_owner  *sp;
1549         struct nfs4_state     *state = NULL;
1550         struct nfs_server       *server = NFS_SERVER(dir);
1551         struct nfs4_opendata *opendata;
1552         int status;
1553
1554         /* Protect against reboot recovery conflicts */
1555         status = -ENOMEM;
1556         if (!(sp = nfs4_get_state_owner(server, cred))) {
1557                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1558                 goto out_err;
1559         }
1560         status = nfs4_recover_expired_lease(server);
1561         if (status != 0)
1562                 goto err_put_state_owner;
1563         if (path->dentry->d_inode != NULL)
1564                 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1565         status = -ENOMEM;
1566         opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1567         if (opendata == NULL)
1568                 goto err_put_state_owner;
1569
1570         if (path->dentry->d_inode != NULL)
1571                 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1572
1573         status = _nfs4_proc_open(opendata);
1574         if (status != 0)
1575                 goto err_opendata_put;
1576
1577         if (opendata->o_arg.open_flags & O_EXCL)
1578                 nfs4_exclusive_attrset(opendata, sattr);
1579
1580         state = nfs4_opendata_to_nfs4_state(opendata);
1581         status = PTR_ERR(state);
1582         if (IS_ERR(state))
1583                 goto err_opendata_put;
1584         if (server->caps & NFS_CAP_POSIX_LOCK)
1585                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1586         nfs4_opendata_put(opendata);
1587         nfs4_put_state_owner(sp);
1588         *res = state;
1589         return 0;
1590 err_opendata_put:
1591         nfs4_opendata_put(opendata);
1592 err_put_state_owner:
1593         nfs4_put_state_owner(sp);
1594 out_err:
1595         *res = NULL;
1596         return status;
1597 }
1598
1599
1600 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1601 {
1602         struct nfs4_exception exception = { };
1603         struct nfs4_state *res;
1604         int status;
1605
1606         do {
1607                 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1608                 if (status == 0)
1609                         break;
1610                 /* NOTE: BAD_SEQID means the server and client disagree about the
1611                  * book-keeping w.r.t. state-changing operations
1612                  * (OPEN/CLOSE/LOCK/LOCKU...)
1613                  * It is actually a sign of a bug on the client or on the server.
1614                  *
1615                  * If we receive a BAD_SEQID error in the particular case of
1616                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1617                  * have unhashed the old state_owner for us, and that we can
1618                  * therefore safely retry using a new one. We should still warn
1619                  * the user though...
1620                  */
1621                 if (status == -NFS4ERR_BAD_SEQID) {
1622                         printk(KERN_WARNING "NFS: v4 server %s "
1623                                         " returned a bad sequence-id error!\n",
1624                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1625                         exception.retry = 1;
1626                         continue;
1627                 }
1628                 /*
1629                  * BAD_STATEID on OPEN means that the server cancelled our
1630                  * state before it received the OPEN_CONFIRM.
1631                  * Recover by retrying the request as per the discussion
1632                  * on Page 181 of RFC3530.
1633                  */
1634                 if (status == -NFS4ERR_BAD_STATEID) {
1635                         exception.retry = 1;
1636                         continue;
1637                 }
1638                 if (status == -EAGAIN) {
1639                         /* We must have found a delegation */
1640                         exception.retry = 1;
1641                         continue;
1642                 }
1643                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1644                                         status, &exception));
1645         } while (exception.retry);
1646         return res;
1647 }
1648
1649 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1650                             struct nfs_fattr *fattr, struct iattr *sattr,
1651                             struct nfs4_state *state)
1652 {
1653         struct nfs_server *server = NFS_SERVER(inode);
1654         struct nfs_setattrargs  arg = {
1655                 .fh             = NFS_FH(inode),
1656                 .iap            = sattr,
1657                 .server         = server,
1658                 .bitmask = server->attr_bitmask,
1659         };
1660         struct nfs_setattrres  res = {
1661                 .fattr          = fattr,
1662                 .server         = server,
1663         };
1664         struct rpc_message msg = {
1665                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1666                 .rpc_argp       = &arg,
1667                 .rpc_resp       = &res,
1668                 .rpc_cred       = cred,
1669         };
1670         unsigned long timestamp = jiffies;
1671         int status;
1672
1673         nfs_fattr_init(fattr);
1674
1675         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1676                 /* Use that stateid */
1677         } else if (state != NULL) {
1678                 nfs4_copy_stateid(&arg.stateid, state, current->files);
1679         } else
1680                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1681
1682         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1683         if (status == 0 && state != NULL)
1684                 renew_lease(server, timestamp);
1685         return status;
1686 }
1687
1688 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1689                            struct nfs_fattr *fattr, struct iattr *sattr,
1690                            struct nfs4_state *state)
1691 {
1692         struct nfs_server *server = NFS_SERVER(inode);
1693         struct nfs4_exception exception = { };
1694         int err;
1695         do {
1696                 err = nfs4_handle_exception(server,
1697                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1698                                 &exception);
1699         } while (exception.retry);
1700         return err;
1701 }
1702
1703 struct nfs4_closedata {
1704         struct path path;
1705         struct inode *inode;
1706         struct nfs4_state *state;
1707         struct nfs_closeargs arg;
1708         struct nfs_closeres res;
1709         struct nfs_fattr fattr;
1710         unsigned long timestamp;
1711 };
1712
1713 static void nfs4_free_closedata(void *data)
1714 {
1715         struct nfs4_closedata *calldata = data;
1716         struct nfs4_state_owner *sp = calldata->state->owner;
1717
1718         nfs4_put_open_state(calldata->state);
1719         nfs_free_seqid(calldata->arg.seqid);
1720         nfs4_put_state_owner(sp);
1721         path_put(&calldata->path);
1722         kfree(calldata);
1723 }
1724
1725 static void nfs4_close_done(struct rpc_task *task, void *data)
1726 {
1727         struct nfs4_closedata *calldata = data;
1728         struct nfs4_state *state = calldata->state;
1729         struct nfs_server *server = NFS_SERVER(calldata->inode);
1730
1731         nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1732         if (RPC_ASSASSINATED(task))
1733                 return;
1734         /* hmm. we are done with the inode, and in the process of freeing
1735          * the state_owner. we keep this around to process errors
1736          */
1737         switch (task->tk_status) {
1738                 case 0:
1739                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1740                         renew_lease(server, calldata->timestamp);
1741                         break;
1742                 case -NFS4ERR_STALE_STATEID:
1743                 case -NFS4ERR_OLD_STATEID:
1744                 case -NFS4ERR_BAD_STATEID:
1745                 case -NFS4ERR_EXPIRED:
1746                         if (calldata->arg.fmode == 0)
1747                                 break;
1748                 default:
1749                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1750                                 nfs4_restart_rpc(task, server->nfs_client);
1751                                 return;
1752                         }
1753         }
1754         nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1755         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1756 }
1757
1758 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1759 {
1760         struct nfs4_closedata *calldata = data;
1761         struct nfs4_state *state = calldata->state;
1762         int clear_rd, clear_wr, clear_rdwr;
1763
1764         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1765                 return;
1766
1767         clear_rd = clear_wr = clear_rdwr = 0;
1768         spin_lock(&state->owner->so_lock);
1769         /* Calculate the change in open mode */
1770         if (state->n_rdwr == 0) {
1771                 if (state->n_rdonly == 0) {
1772                         clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1773                         clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1774                 }
1775                 if (state->n_wronly == 0) {
1776                         clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1777                         clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778                 }
1779         }
1780         spin_unlock(&state->owner->so_lock);
1781         if (!clear_rd && !clear_wr && !clear_rdwr) {
1782                 /* Note: exit _without_ calling nfs4_close_done */
1783                 task->tk_action = NULL;
1784                 return;
1785         }
1786         nfs_fattr_init(calldata->res.fattr);
1787         if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1788                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1789                 calldata->arg.fmode = FMODE_READ;
1790         } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1791                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1792                 calldata->arg.fmode = FMODE_WRITE;
1793         }
1794         calldata->timestamp = jiffies;
1795         if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1796                                 &calldata->arg.seq_args, &calldata->res.seq_res,
1797                                 1, task))
1798                 return;
1799         rpc_call_start(task);
1800 }
1801
1802 static const struct rpc_call_ops nfs4_close_ops = {
1803         .rpc_call_prepare = nfs4_close_prepare,
1804         .rpc_call_done = nfs4_close_done,
1805         .rpc_release = nfs4_free_closedata,
1806 };
1807
1808 /* 
1809  * It is possible for data to be read/written from a mem-mapped file 
1810  * after the sys_close call (which hits the vfs layer as a flush).
1811  * This means that we can't safely call nfsv4 close on a file until 
1812  * the inode is cleared. This in turn means that we are not good
1813  * NFSv4 citizens - we do not indicate to the server to update the file's 
1814  * share state even when we are done with one of the three share 
1815  * stateid's in the inode.
1816  *
1817  * NOTE: Caller must be holding the sp->so_owner semaphore!
1818  */
1819 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1820 {
1821         struct nfs_server *server = NFS_SERVER(state->inode);
1822         struct nfs4_closedata *calldata;
1823         struct nfs4_state_owner *sp = state->owner;
1824         struct rpc_task *task;
1825         struct rpc_message msg = {
1826                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1827                 .rpc_cred = state->owner->so_cred,
1828         };
1829         struct rpc_task_setup task_setup_data = {
1830                 .rpc_client = server->client,
1831                 .rpc_message = &msg,
1832                 .callback_ops = &nfs4_close_ops,
1833                 .workqueue = nfsiod_workqueue,
1834                 .flags = RPC_TASK_ASYNC,
1835         };
1836         int status = -ENOMEM;
1837
1838         calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1839         if (calldata == NULL)
1840                 goto out;
1841         calldata->inode = state->inode;
1842         calldata->state = state;
1843         calldata->arg.fh = NFS_FH(state->inode);
1844         calldata->arg.stateid = &state->open_stateid;
1845         if (nfs4_has_session(server->nfs_client))
1846                 memset(calldata->arg.stateid->data, 0, 4);    /* clear seqid */
1847         /* Serialization for the sequence id */
1848         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1849         if (calldata->arg.seqid == NULL)
1850                 goto out_free_calldata;
1851         calldata->arg.fmode = 0;
1852         calldata->arg.bitmask = server->cache_consistency_bitmask;
1853         calldata->res.fattr = &calldata->fattr;
1854         calldata->res.seqid = calldata->arg.seqid;
1855         calldata->res.server = server;
1856         calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1857         calldata->path.mnt = mntget(path->mnt);
1858         calldata->path.dentry = dget(path->dentry);
1859
1860         msg.rpc_argp = &calldata->arg,
1861         msg.rpc_resp = &calldata->res,
1862         task_setup_data.callback_data = calldata;
1863         task = rpc_run_task(&task_setup_data);
1864         if (IS_ERR(task))
1865                 return PTR_ERR(task);
1866         status = 0;
1867         if (wait)
1868                 status = rpc_wait_for_completion_task(task);
1869         rpc_put_task(task);
1870         return status;
1871 out_free_calldata:
1872         kfree(calldata);
1873 out:
1874         nfs4_put_open_state(state);
1875         nfs4_put_state_owner(sp);
1876         return status;
1877 }
1878
1879 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1880 {
1881         struct file *filp;
1882         int ret;
1883
1884         /* If the open_intent is for execute, we have an extra check to make */
1885         if (fmode & FMODE_EXEC) {
1886                 ret = nfs_may_open(state->inode,
1887                                 state->owner->so_cred,
1888                                 nd->intent.open.flags);
1889                 if (ret < 0)
1890                         goto out_close;
1891         }
1892         filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1893         if (!IS_ERR(filp)) {
1894                 struct nfs_open_context *ctx;
1895                 ctx = nfs_file_open_context(filp);
1896                 ctx->state = state;
1897                 return 0;
1898         }
1899         ret = PTR_ERR(filp);
1900 out_close:
1901         nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1902         return ret;
1903 }
1904
1905 struct dentry *
1906 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1907 {
1908         struct path path = {
1909                 .mnt = nd->path.mnt,
1910                 .dentry = dentry,
1911         };
1912         struct dentry *parent;
1913         struct iattr attr;
1914         struct rpc_cred *cred;
1915         struct nfs4_state *state;
1916         struct dentry *res;
1917         fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1918
1919         if (nd->flags & LOOKUP_CREATE) {
1920                 attr.ia_mode = nd->intent.open.create_mode;
1921                 attr.ia_valid = ATTR_MODE;
1922                 if (!IS_POSIXACL(dir))
1923                         attr.ia_mode &= ~current_umask();
1924         } else {
1925                 attr.ia_valid = 0;
1926                 BUG_ON(nd->intent.open.flags & O_CREAT);
1927         }
1928
1929         cred = rpc_lookup_cred();
1930         if (IS_ERR(cred))
1931                 return (struct dentry *)cred;
1932         parent = dentry->d_parent;
1933         /* Protect against concurrent sillydeletes */
1934         nfs_block_sillyrename(parent);
1935         state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1936         put_rpccred(cred);
1937         if (IS_ERR(state)) {
1938                 if (PTR_ERR(state) == -ENOENT) {
1939                         d_add(dentry, NULL);
1940                         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1941                 }
1942                 nfs_unblock_sillyrename(parent);
1943                 return (struct dentry *)state;
1944         }
1945         res = d_add_unique(dentry, igrab(state->inode));
1946         if (res != NULL)
1947                 path.dentry = res;
1948         nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1949         nfs_unblock_sillyrename(parent);
1950         nfs4_intent_set_file(nd, &path, state, fmode);
1951         return res;
1952 }
1953
1954 int
1955 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1956 {
1957         struct path path = {
1958                 .mnt = nd->path.mnt,
1959                 .dentry = dentry,
1960         };
1961         struct rpc_cred *cred;
1962         struct nfs4_state *state;
1963         fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1964
1965         cred = rpc_lookup_cred();
1966         if (IS_ERR(cred))
1967                 return PTR_ERR(cred);
1968         state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1969         put_rpccred(cred);
1970         if (IS_ERR(state)) {
1971                 switch (PTR_ERR(state)) {
1972                         case -EPERM:
1973                         case -EACCES:
1974                         case -EDQUOT:
1975                         case -ENOSPC:
1976                         case -EROFS:
1977                                 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1978                                 return 1;
1979                         default:
1980                                 goto out_drop;
1981                 }
1982         }
1983         if (state->inode == dentry->d_inode) {
1984                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1985                 nfs4_intent_set_file(nd, &path, state, fmode);
1986                 return 1;
1987         }
1988         nfs4_close_sync(&path, state, fmode);
1989 out_drop:
1990         d_drop(dentry);
1991         return 0;
1992 }
1993
1994 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1995 {
1996         if (ctx->state == NULL)
1997                 return;
1998         if (is_sync)
1999                 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2000         else
2001                 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2002 }
2003
2004 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2005 {
2006         struct nfs4_server_caps_arg args = {
2007                 .fhandle = fhandle,
2008         };
2009         struct nfs4_server_caps_res res = {};
2010         struct rpc_message msg = {
2011                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2012                 .rpc_argp = &args,
2013                 .rpc_resp = &res,
2014         };
2015         int status;
2016
2017         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2018         if (status == 0) {
2019                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2020                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2021                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2022                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2023                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2024                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2025                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2026                         server->caps |= NFS_CAP_ACLS;
2027                 if (res.has_links != 0)
2028                         server->caps |= NFS_CAP_HARDLINKS;
2029                 if (res.has_symlinks != 0)
2030                         server->caps |= NFS_CAP_SYMLINKS;
2031                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2032                         server->caps |= NFS_CAP_FILEID;
2033                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2034                         server->caps |= NFS_CAP_MODE;
2035                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2036                         server->caps |= NFS_CAP_NLINK;
2037                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2038                         server->caps |= NFS_CAP_OWNER;
2039                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2040                         server->caps |= NFS_CAP_OWNER_GROUP;
2041                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2042                         server->caps |= NFS_CAP_ATIME;
2043                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2044                         server->caps |= NFS_CAP_CTIME;
2045                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2046                         server->caps |= NFS_CAP_MTIME;
2047
2048                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2049                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2050                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2051                 server->acl_bitmask = res.acl_bitmask;
2052         }
2053
2054         return status;
2055 }
2056
2057 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2058 {
2059         struct nfs4_exception exception = { };
2060         int err;
2061         do {
2062                 err = nfs4_handle_exception(server,
2063                                 _nfs4_server_capabilities(server, fhandle),
2064                                 &exception);
2065         } while (exception.retry);
2066         return err;
2067 }
2068
2069 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2070                 struct nfs_fsinfo *info)
2071 {
2072         struct nfs4_lookup_root_arg args = {
2073                 .bitmask = nfs4_fattr_bitmap,
2074         };
2075         struct nfs4_lookup_res res = {
2076                 .server = server,
2077                 .fattr = info->fattr,
2078                 .fh = fhandle,
2079         };
2080         struct rpc_message msg = {
2081                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2082                 .rpc_argp = &args,
2083                 .rpc_resp = &res,
2084         };
2085
2086         nfs_fattr_init(info->fattr);
2087         return nfs4_call_sync(server, &msg, &args, &res, 0);
2088 }
2089
2090 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2091                 struct nfs_fsinfo *info)
2092 {
2093         struct nfs4_exception exception = { };
2094         int err;
2095         do {
2096                 err = nfs4_handle_exception(server,
2097                                 _nfs4_lookup_root(server, fhandle, info),
2098                                 &exception);
2099         } while (exception.retry);
2100         return err;
2101 }
2102
2103 /*
2104  * get the file handle for the "/" directory on the server
2105  */
2106 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2107                               struct nfs_fsinfo *info)
2108 {
2109         int status;
2110
2111         status = nfs4_lookup_root(server, fhandle, info);
2112         if (status == 0)
2113                 status = nfs4_server_capabilities(server, fhandle);
2114         if (status == 0)
2115                 status = nfs4_do_fsinfo(server, fhandle, info);
2116         return nfs4_map_errors(status);
2117 }
2118
2119 /*
2120  * Get locations and (maybe) other attributes of a referral.
2121  * Note that we'll actually follow the referral later when
2122  * we detect fsid mismatch in inode revalidation
2123  */
2124 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2125 {
2126         int status = -ENOMEM;
2127         struct page *page = NULL;
2128         struct nfs4_fs_locations *locations = NULL;
2129
2130         page = alloc_page(GFP_KERNEL);
2131         if (page == NULL)
2132                 goto out;
2133         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2134         if (locations == NULL)
2135                 goto out;
2136
2137         status = nfs4_proc_fs_locations(dir, name, locations, page);
2138         if (status != 0)
2139                 goto out;
2140         /* Make sure server returned a different fsid for the referral */
2141         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2142                 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2143                 status = -EIO;
2144                 goto out;
2145         }
2146
2147         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2148         fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2149         if (!fattr->mode)
2150                 fattr->mode = S_IFDIR;
2151         memset(fhandle, 0, sizeof(struct nfs_fh));
2152 out:
2153         if (page)
2154                 __free_page(page);
2155         if (locations)
2156                 kfree(locations);
2157         return status;
2158 }
2159
2160 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2161 {
2162         struct nfs4_getattr_arg args = {
2163                 .fh = fhandle,
2164                 .bitmask = server->attr_bitmask,
2165         };
2166         struct nfs4_getattr_res res = {
2167                 .fattr = fattr,
2168                 .server = server,
2169         };
2170         struct rpc_message msg = {
2171                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2172                 .rpc_argp = &args,
2173                 .rpc_resp = &res,
2174         };
2175         
2176         nfs_fattr_init(fattr);
2177         return nfs4_call_sync(server, &msg, &args, &res, 0);
2178 }
2179
2180 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2181 {
2182         struct nfs4_exception exception = { };
2183         int err;
2184         do {
2185                 err = nfs4_handle_exception(server,
2186                                 _nfs4_proc_getattr(server, fhandle, fattr),
2187                                 &exception);
2188         } while (exception.retry);
2189         return err;
2190 }
2191
2192 /* 
2193  * The file is not closed if it is opened due to the a request to change
2194  * the size of the file. The open call will not be needed once the
2195  * VFS layer lookup-intents are implemented.
2196  *
2197  * Close is called when the inode is destroyed.
2198  * If we haven't opened the file for O_WRONLY, we
2199  * need to in the size_change case to obtain a stateid.
2200  *
2201  * Got race?
2202  * Because OPEN is always done by name in nfsv4, it is
2203  * possible that we opened a different file by the same
2204  * name.  We can recognize this race condition, but we
2205  * can't do anything about it besides returning an error.
2206  *
2207  * This will be fixed with VFS changes (lookup-intent).
2208  */
2209 static int
2210 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2211                   struct iattr *sattr)
2212 {
2213         struct inode *inode = dentry->d_inode;
2214         struct rpc_cred *cred = NULL;
2215         struct nfs4_state *state = NULL;
2216         int status;
2217
2218         nfs_fattr_init(fattr);
2219         
2220         /* Search for an existing open(O_WRITE) file */
2221         if (sattr->ia_valid & ATTR_FILE) {
2222                 struct nfs_open_context *ctx;
2223
2224                 ctx = nfs_file_open_context(sattr->ia_file);
2225                 if (ctx) {
2226                         cred = ctx->cred;
2227                         state = ctx->state;
2228                 }
2229         }
2230
2231         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2232         if (status == 0)
2233                 nfs_setattr_update_inode(inode, sattr);
2234         return status;
2235 }
2236
2237 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2238                 const struct qstr *name, struct nfs_fh *fhandle,
2239                 struct nfs_fattr *fattr)
2240 {
2241         int                    status;
2242         struct nfs4_lookup_arg args = {
2243                 .bitmask = server->attr_bitmask,
2244                 .dir_fh = dirfh,
2245                 .name = name,
2246         };
2247         struct nfs4_lookup_res res = {
2248                 .server = server,
2249                 .fattr = fattr,
2250                 .fh = fhandle,
2251         };
2252         struct rpc_message msg = {
2253                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2254                 .rpc_argp = &args,
2255                 .rpc_resp = &res,
2256         };
2257
2258         nfs_fattr_init(fattr);
2259
2260         dprintk("NFS call  lookupfh %s\n", name->name);
2261         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2262         dprintk("NFS reply lookupfh: %d\n", status);
2263         return status;
2264 }
2265
2266 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2267                               struct qstr *name, struct nfs_fh *fhandle,
2268                               struct nfs_fattr *fattr)
2269 {
2270         struct nfs4_exception exception = { };
2271         int err;
2272         do {
2273                 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2274                 /* FIXME: !!!! */
2275                 if (err == -NFS4ERR_MOVED) {
2276                         err = -EREMOTE;
2277                         break;
2278                 }
2279                 err = nfs4_handle_exception(server, err, &exception);
2280         } while (exception.retry);
2281         return err;
2282 }
2283
2284 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2285                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2286 {
2287         int status;
2288         
2289         dprintk("NFS call  lookup %s\n", name->name);
2290         status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2291         if (status == -NFS4ERR_MOVED)
2292                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2293         dprintk("NFS reply lookup: %d\n", status);
2294         return status;
2295 }
2296
2297 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2298 {
2299         struct nfs4_exception exception = { };
2300         int err;
2301         do {
2302                 err = nfs4_handle_exception(NFS_SERVER(dir),
2303                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2304                                 &exception);
2305         } while (exception.retry);
2306         return err;
2307 }
2308
2309 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2310 {
2311         struct nfs_server *server = NFS_SERVER(inode);
2312         struct nfs_fattr fattr;
2313         struct nfs4_accessargs args = {
2314                 .fh = NFS_FH(inode),
2315                 .bitmask = server->attr_bitmask,
2316         };
2317         struct nfs4_accessres res = {
2318                 .server = server,
2319                 .fattr = &fattr,
2320         };
2321         struct rpc_message msg = {
2322                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2323                 .rpc_argp = &args,
2324                 .rpc_resp = &res,
2325                 .rpc_cred = entry->cred,
2326         };
2327         int mode = entry->mask;
2328         int status;
2329
2330         /*
2331          * Determine which access bits we want to ask for...
2332          */
2333         if (mode & MAY_READ)
2334                 args.access |= NFS4_ACCESS_READ;
2335         if (S_ISDIR(inode->i_mode)) {
2336                 if (mode & MAY_WRITE)
2337                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2338                 if (mode & MAY_EXEC)
2339                         args.access |= NFS4_ACCESS_LOOKUP;
2340         } else {
2341                 if (mode & MAY_WRITE)
2342                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2343                 if (mode & MAY_EXEC)
2344                         args.access |= NFS4_ACCESS_EXECUTE;
2345         }
2346         nfs_fattr_init(&fattr);
2347         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2348         if (!status) {
2349                 entry->mask = 0;
2350                 if (res.access & NFS4_ACCESS_READ)
2351                         entry->mask |= MAY_READ;
2352                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2353                         entry->mask |= MAY_WRITE;
2354                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2355                         entry->mask |= MAY_EXEC;
2356                 nfs_refresh_inode(inode, &fattr);
2357         }
2358         return status;
2359 }
2360
2361 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2362 {
2363         struct nfs4_exception exception = { };
2364         int err;
2365         do {
2366                 err = nfs4_handle_exception(NFS_SERVER(inode),
2367                                 _nfs4_proc_access(inode, entry),
2368                                 &exception);
2369         } while (exception.retry);
2370         return err;
2371 }
2372
2373 /*
2374  * TODO: For the time being, we don't try to get any attributes
2375  * along with any of the zero-copy operations READ, READDIR,
2376  * READLINK, WRITE.
2377  *
2378  * In the case of the first three, we want to put the GETATTR
2379  * after the read-type operation -- this is because it is hard
2380  * to predict the length of a GETATTR response in v4, and thus
2381  * align the READ data correctly.  This means that the GETATTR
2382  * may end up partially falling into the page cache, and we should
2383  * shift it into the 'tail' of the xdr_buf before processing.
2384  * To do this efficiently, we need to know the total length
2385  * of data received, which doesn't seem to be available outside
2386  * of the RPC layer.
2387  *
2388  * In the case of WRITE, we also want to put the GETATTR after
2389  * the operation -- in this case because we want to make sure
2390  * we get the post-operation mtime and size.  This means that
2391  * we can't use xdr_encode_pages() as written: we need a variant
2392  * of it which would leave room in the 'tail' iovec.
2393  *
2394  * Both of these changes to the XDR layer would in fact be quite
2395  * minor, but I decided to leave them for a subsequent patch.
2396  */
2397 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2398                 unsigned int pgbase, unsigned int pglen)
2399 {
2400         struct nfs4_readlink args = {
2401                 .fh       = NFS_FH(inode),
2402                 .pgbase   = pgbase,
2403                 .pglen    = pglen,
2404                 .pages    = &page,
2405         };
2406         struct nfs4_readlink_res res;
2407         struct rpc_message msg = {
2408                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2409                 .rpc_argp = &args,
2410                 .rpc_resp = &res,
2411         };
2412
2413         return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2414 }
2415
2416 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2417                 unsigned int pgbase, unsigned int pglen)
2418 {
2419         struct nfs4_exception exception = { };
2420         int err;
2421         do {
2422                 err = nfs4_handle_exception(NFS_SERVER(inode),
2423                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2424                                 &exception);
2425         } while (exception.retry);
2426         return err;
2427 }
2428
2429 /*
2430  * Got race?
2431  * We will need to arrange for the VFS layer to provide an atomic open.
2432  * Until then, this create/open method is prone to inefficiency and race
2433  * conditions due to the lookup, create, and open VFS calls from sys_open()
2434  * placed on the wire.
2435  *
2436  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2437  * The file will be opened again in the subsequent VFS open call
2438  * (nfs4_proc_file_open).
2439  *
2440  * The open for read will just hang around to be used by any process that
2441  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2442  */
2443
2444 static int
2445 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2446                  int flags, struct nameidata *nd)
2447 {
2448         struct path path = {
2449                 .mnt = nd->path.mnt,
2450                 .dentry = dentry,
2451         };
2452         struct nfs4_state *state;
2453         struct rpc_cred *cred;
2454         fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2455         int status = 0;
2456
2457         cred = rpc_lookup_cred();
2458         if (IS_ERR(cred)) {
2459                 status = PTR_ERR(cred);
2460                 goto out;
2461         }
2462         state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2463         d_drop(dentry);
2464         if (IS_ERR(state)) {
2465                 status = PTR_ERR(state);
2466                 goto out_putcred;
2467         }
2468         d_add(dentry, igrab(state->inode));
2469         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2470         if (flags & O_EXCL) {
2471                 struct nfs_fattr fattr;
2472                 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2473                 if (status == 0)
2474                         nfs_setattr_update_inode(state->inode, sattr);
2475                 nfs_post_op_update_inode(state->inode, &fattr);
2476         }
2477         if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2478                 status = nfs4_intent_set_file(nd, &path, state, fmode);
2479         else
2480                 nfs4_close_sync(&path, state, fmode);
2481 out_putcred:
2482         put_rpccred(cred);
2483 out:
2484         return status;
2485 }
2486
2487 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2488 {
2489         struct nfs_server *server = NFS_SERVER(dir);
2490         struct nfs_removeargs args = {
2491                 .fh = NFS_FH(dir),
2492                 .name.len = name->len,
2493                 .name.name = name->name,
2494                 .bitmask = server->attr_bitmask,
2495         };
2496         struct nfs_removeres res = {
2497                 .server = server,
2498         };
2499         struct rpc_message msg = {
2500                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2501                 .rpc_argp = &args,
2502                 .rpc_resp = &res,
2503         };
2504         int                     status;
2505
2506         nfs_fattr_init(&res.dir_attr);
2507         status = nfs4_call_sync(server, &msg, &args, &res, 1);
2508         if (status == 0) {
2509                 update_changeattr(dir, &res.cinfo);
2510                 nfs_post_op_update_inode(dir, &res.dir_attr);
2511         }
2512         return status;
2513 }
2514
2515 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2516 {
2517         struct nfs4_exception exception = { };
2518         int err;
2519         do {
2520                 err = nfs4_handle_exception(NFS_SERVER(dir),
2521                                 _nfs4_proc_remove(dir, name),
2522                                 &exception);
2523         } while (exception.retry);
2524         return err;
2525 }
2526
2527 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2528 {
2529         struct nfs_server *server = NFS_SERVER(dir);
2530         struct nfs_removeargs *args = msg->rpc_argp;
2531         struct nfs_removeres *res = msg->rpc_resp;
2532
2533         args->bitmask = server->cache_consistency_bitmask;
2534         res->server = server;
2535         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2536 }
2537
2538 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2539 {
2540         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2541
2542         nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2543         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2544                 return 0;
2545         nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2546         update_changeattr(dir, &res->cinfo);
2547         nfs_post_op_update_inode(dir, &res->dir_attr);
2548         return 1;
2549 }
2550
2551 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2552                 struct inode *new_dir, struct qstr *new_name)
2553 {
2554         struct nfs_server *server = NFS_SERVER(old_dir);
2555         struct nfs4_rename_arg arg = {
2556                 .old_dir = NFS_FH(old_dir),
2557                 .new_dir = NFS_FH(new_dir),
2558                 .old_name = old_name,
2559                 .new_name = new_name,
2560                 .bitmask = server->attr_bitmask,
2561         };
2562         struct nfs_fattr old_fattr, new_fattr;
2563         struct nfs4_rename_res res = {
2564                 .server = server,
2565                 .old_fattr = &old_fattr,
2566                 .new_fattr = &new_fattr,
2567         };
2568         struct rpc_message msg = {
2569                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2570                 .rpc_argp = &arg,
2571                 .rpc_resp = &res,
2572         };
2573         int                     status;
2574         
2575         nfs_fattr_init(res.old_fattr);
2576         nfs_fattr_init(res.new_fattr);
2577         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2578
2579         if (!status) {
2580                 update_changeattr(old_dir, &res.old_cinfo);
2581                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2582                 update_changeattr(new_dir, &res.new_cinfo);
2583                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2584         }
2585         return status;
2586 }
2587
2588 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2589                 struct inode *new_dir, struct qstr *new_name)
2590 {
2591         struct nfs4_exception exception = { };
2592         int err;
2593         do {
2594                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2595                                 _nfs4_proc_rename(old_dir, old_name,
2596                                         new_dir, new_name),
2597                                 &exception);
2598         } while (exception.retry);
2599         return err;
2600 }
2601
2602 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2603 {
2604         struct nfs_server *server = NFS_SERVER(inode);
2605         struct nfs4_link_arg arg = {
2606                 .fh     = NFS_FH(inode),
2607                 .dir_fh = NFS_FH(dir),
2608                 .name   = name,
2609                 .bitmask = server->attr_bitmask,
2610         };
2611         struct nfs_fattr fattr, dir_attr;
2612         struct nfs4_link_res res = {
2613                 .server = server,
2614                 .fattr = &fattr,
2615                 .dir_attr = &dir_attr,
2616         };
2617         struct rpc_message msg = {
2618                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2619                 .rpc_argp = &arg,
2620                 .rpc_resp = &res,
2621         };
2622         int                     status;
2623
2624         nfs_fattr_init(res.fattr);
2625         nfs_fattr_init(res.dir_attr);
2626         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2627         if (!status) {
2628                 update_changeattr(dir, &res.cinfo);
2629                 nfs_post_op_update_inode(dir, res.dir_attr);
2630                 nfs_post_op_update_inode(inode, res.fattr);
2631         }
2632
2633         return status;
2634 }
2635
2636 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2637 {
2638         struct nfs4_exception exception = { };
2639         int err;
2640         do {
2641                 err = nfs4_handle_exception(NFS_SERVER(inode),
2642                                 _nfs4_proc_link(inode, dir, name),
2643                                 &exception);
2644         } while (exception.retry);
2645         return err;
2646 }
2647
2648 struct nfs4_createdata {
2649         struct rpc_message msg;
2650         struct nfs4_create_arg arg;
2651         struct nfs4_create_res res;
2652         struct nfs_fh fh;
2653         struct nfs_fattr fattr;
2654         struct nfs_fattr dir_fattr;
2655 };
2656
2657 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2658                 struct qstr *name, struct iattr *sattr, u32 ftype)
2659 {
2660         struct nfs4_createdata *data;
2661
2662         data = kzalloc(sizeof(*data), GFP_KERNEL);
2663         if (data != NULL) {
2664                 struct nfs_server *server = NFS_SERVER(dir);
2665
2666                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2667                 data->msg.rpc_argp = &data->arg;
2668                 data->msg.rpc_resp = &data->res;
2669                 data->arg.dir_fh = NFS_FH(dir);
2670                 data->arg.server = server;
2671                 data->arg.name = name;
2672                 data->arg.attrs = sattr;
2673                 data->arg.ftype = ftype;
2674                 data->arg.bitmask = server->attr_bitmask;
2675                 data->res.server = server;
2676                 data->res.fh = &data->fh;
2677                 data->res.fattr = &data->fattr;
2678                 data->res.dir_fattr = &data->dir_fattr;
2679                 nfs_fattr_init(data->res.fattr);
2680                 nfs_fattr_init(data->res.dir_fattr);
2681         }
2682         return data;
2683 }
2684
2685 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2686 {
2687         int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2688                                     &data->arg, &data->res, 1);
2689         if (status == 0) {
2690                 update_changeattr(dir, &data->res.dir_cinfo);
2691                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2692                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2693         }
2694         return status;
2695 }
2696
2697 static void nfs4_free_createdata(struct nfs4_createdata *data)
2698 {
2699         kfree(data);
2700 }
2701
2702 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2703                 struct page *page, unsigned int len, struct iattr *sattr)
2704 {
2705         struct nfs4_createdata *data;
2706         int status = -ENAMETOOLONG;
2707
2708         if (len > NFS4_MAXPATHLEN)
2709                 goto out;
2710
2711         status = -ENOMEM;
2712         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2713         if (data == NULL)
2714                 goto out;
2715
2716         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2717         data->arg.u.symlink.pages = &page;
2718         data->arg.u.symlink.len = len;
2719         
2720         status = nfs4_do_create(dir, dentry, data);
2721
2722         nfs4_free_createdata(data);
2723 out:
2724         return status;
2725 }
2726
2727 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2728                 struct page *page, unsigned int len, struct iattr *sattr)
2729 {
2730         struct nfs4_exception exception = { };
2731         int err;
2732         do {
2733                 err = nfs4_handle_exception(NFS_SERVER(dir),
2734                                 _nfs4_proc_symlink(dir, dentry, page,
2735                                                         len, sattr),
2736                                 &exception);
2737         } while (exception.retry);
2738         return err;
2739 }
2740
2741 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2742                 struct iattr *sattr)
2743 {
2744         struct nfs4_createdata *data;
2745         int status = -ENOMEM;
2746
2747         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2748         if (data == NULL)
2749                 goto out;
2750
2751         status = nfs4_do_create(dir, dentry, data);
2752
2753         nfs4_free_createdata(data);
2754 out:
2755         return status;
2756 }
2757
2758 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2759                 struct iattr *sattr)
2760 {
2761         struct nfs4_exception exception = { };
2762         int err;
2763         do {
2764                 err = nfs4_handle_exception(NFS_SERVER(dir),
2765                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2766                                 &exception);
2767         } while (exception.retry);
2768         return err;
2769 }
2770
2771 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2772                   u64 cookie, struct page *page, unsigned int count, int plus)
2773 {
2774         struct inode            *dir = dentry->d_inode;
2775         struct nfs4_readdir_arg args = {
2776                 .fh = NFS_FH(dir),
2777                 .pages = &page,
2778                 .pgbase = 0,
2779                 .count = count,
2780                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2781         };
2782         struct nfs4_readdir_res res;
2783         struct rpc_message msg = {
2784                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2785                 .rpc_argp = &args,
2786                 .rpc_resp = &res,
2787                 .rpc_cred = cred,
2788         };
2789         int                     status;
2790
2791         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2792                         dentry->d_parent->d_name.name,
2793                         dentry->d_name.name,
2794                         (unsigned long long)cookie);
2795         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2796         res.pgbase = args.pgbase;
2797         status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2798         if (status == 0)
2799                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2800
2801         nfs_invalidate_atime(dir);
2802
2803         dprintk("%s: returns %d\n", __func__, status);
2804         return status;
2805 }
2806
2807 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2808                   u64 cookie, struct page *page, unsigned int count, int plus)
2809 {
2810         struct nfs4_exception exception = { };
2811         int err;
2812         do {
2813                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2814                                 _nfs4_proc_readdir(dentry, cred, cookie,
2815                                         page, count, plus),
2816                                 &exception);
2817         } while (exception.retry);
2818         return err;
2819 }
2820
2821 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2822                 struct iattr *sattr, dev_t rdev)
2823 {
2824         struct nfs4_createdata *data;
2825         int mode = sattr->ia_mode;
2826         int status = -ENOMEM;
2827
2828         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2829         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2830
2831         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2832         if (data == NULL)
2833                 goto out;
2834
2835         if (S_ISFIFO(mode))
2836                 data->arg.ftype = NF4FIFO;
2837         else if (S_ISBLK(mode)) {
2838                 data->arg.ftype = NF4BLK;
2839                 data->arg.u.device.specdata1 = MAJOR(rdev);
2840                 data->arg.u.device.specdata2 = MINOR(rdev);
2841         }
2842         else if (S_ISCHR(mode)) {
2843                 data->arg.ftype = NF4CHR;
2844                 data->arg.u.device.specdata1 = MAJOR(rdev);
2845                 data->arg.u.device.specdata2 = MINOR(rdev);
2846         }
2847         
2848         status = nfs4_do_create(dir, dentry, data);
2849
2850         nfs4_free_createdata(data);
2851 out:
2852         return status;
2853 }
2854
2855 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2856                 struct iattr *sattr, dev_t rdev)
2857 {
2858         struct nfs4_exception exception = { };
2859         int err;
2860         do {
2861                 err = nfs4_handle_exception(NFS_SERVER(dir),
2862                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2863                                 &exception);
2864         } while (exception.retry);
2865         return err;
2866 }
2867
2868 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2869                  struct nfs_fsstat *fsstat)
2870 {
2871         struct nfs4_statfs_arg args = {
2872                 .fh = fhandle,
2873                 .bitmask = server->attr_bitmask,
2874         };
2875         struct nfs4_statfs_res res = {
2876                 .fsstat = fsstat,
2877         };
2878         struct rpc_message msg = {
2879                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2880                 .rpc_argp = &args,
2881                 .rpc_resp = &res,
2882         };
2883
2884         nfs_fattr_init(fsstat->fattr);
2885         return  nfs4_call_sync(server, &msg, &args, &res, 0);
2886 }
2887
2888 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2889 {
2890         struct nfs4_exception exception = { };
2891         int err;
2892         do {
2893                 err = nfs4_handle_exception(server,
2894                                 _nfs4_proc_statfs(server, fhandle, fsstat),
2895                                 &exception);
2896         } while (exception.retry);
2897         return err;
2898 }
2899
2900 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2901                 struct nfs_fsinfo *fsinfo)
2902 {
2903         struct nfs4_fsinfo_arg args = {
2904                 .fh = fhandle,
2905                 .bitmask = server->attr_bitmask,
2906         };
2907         struct nfs4_fsinfo_res res = {
2908                 .fsinfo = fsinfo,
2909         };
2910         struct rpc_message msg = {
2911                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2912                 .rpc_argp = &args,
2913                 .rpc_resp = &res,
2914         };
2915
2916         return nfs4_call_sync(server, &msg, &args, &res, 0);
2917 }
2918
2919 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2920 {
2921         struct nfs4_exception exception = { };
2922         int err;
2923
2924         do {
2925                 err = nfs4_handle_exception(server,
2926                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2927                                 &exception);
2928         } while (exception.retry);
2929         return err;
2930 }
2931
2932 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2933 {
2934         nfs_fattr_init(fsinfo->fattr);
2935         return nfs4_do_fsinfo(server, fhandle, fsinfo);
2936 }
2937
2938 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2939                 struct nfs_pathconf *pathconf)
2940 {
2941         struct nfs4_pathconf_arg args = {
2942                 .fh = fhandle,
2943                 .bitmask = server->attr_bitmask,
2944         };
2945         struct nfs4_pathconf_res res = {
2946                 .pathconf = pathconf,
2947         };
2948         struct rpc_message msg = {
2949                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2950                 .rpc_argp = &args,
2951                 .rpc_resp = &res,
2952         };
2953
2954         /* None of the pathconf attributes are mandatory to implement */
2955         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2956                 memset(pathconf, 0, sizeof(*pathconf));
2957                 return 0;
2958         }
2959
2960         nfs_fattr_init(pathconf->fattr);
2961         return nfs4_call_sync(server, &msg, &args, &res, 0);
2962 }
2963
2964 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2965                 struct nfs_pathconf *pathconf)
2966 {
2967         struct nfs4_exception exception = { };
2968         int err;
2969
2970         do {
2971                 err = nfs4_handle_exception(server,
2972                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
2973                                 &exception);
2974         } while (exception.retry);
2975         return err;
2976 }
2977
2978 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2979 {
2980         struct nfs_server *server = NFS_SERVER(data->inode);
2981
2982         dprintk("--> %s\n", __func__);
2983
2984         /* nfs4_sequence_free_slot called in the read rpc_call_done */
2985         nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2986
2987         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2988                 nfs4_restart_rpc(task, server->nfs_client);
2989                 return -EAGAIN;
2990         }
2991
2992         nfs_invalidate_atime(data->inode);
2993         if (task->tk_status > 0)
2994                 renew_lease(server, data->timestamp);
2995         return 0;
2996 }
2997
2998 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2999 {
3000         data->timestamp   = jiffies;
3001         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3002 }
3003
3004 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3005 {
3006         struct inode *inode = data->inode;
3007         
3008         /* slot is freed in nfs_writeback_done */
3009         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3010                            task->tk_status);
3011
3012         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3013                 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3014                 return -EAGAIN;
3015         }
3016         if (task->tk_status >= 0) {
3017                 renew_lease(NFS_SERVER(inode), data->timestamp);
3018                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3019         }
3020         return 0;
3021 }
3022
3023 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3024 {
3025         struct nfs_server *server = NFS_SERVER(data->inode);
3026
3027         data->args.bitmask = server->cache_consistency_bitmask;
3028         data->res.server = server;
3029         data->timestamp   = jiffies;
3030
3031         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3032 }
3033
3034 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3035 {
3036         struct inode *inode = data->inode;
3037         
3038         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3039                            task->tk_status);
3040         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3041                 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3042                 return -EAGAIN;
3043         }
3044         nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3045                                 &data->res.seq_res);
3046         nfs_refresh_inode(inode, data->res.fattr);
3047         return 0;
3048 }
3049
3050 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3051 {
3052         struct nfs_server *server = NFS_SERVER(data->inode);
3053         
3054         data->args.bitmask = server->cache_consistency_bitmask;
3055         data->res.server = server;
3056         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3057 }
3058
3059 /*
3060  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3061  * standalone procedure for queueing an asynchronous RENEW.
3062  */
3063 static void nfs4_renew_done(struct rpc_task *task, void *data)
3064 {
3065         struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3066         unsigned long timestamp = (unsigned long)data;
3067
3068         if (task->tk_status < 0) {
3069                 /* Unless we're shutting down, schedule state recovery! */
3070                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3071                         nfs4_schedule_state_recovery(clp);
3072                 return;
3073         }
3074         spin_lock(&clp->cl_lock);
3075         if (time_before(clp->cl_last_renewal,timestamp))
3076                 clp->cl_last_renewal = timestamp;
3077         spin_unlock(&clp->cl_lock);
3078 }
3079
3080 static const struct rpc_call_ops nfs4_renew_ops = {
3081         .rpc_call_done = nfs4_renew_done,
3082 };
3083
3084 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3085 {
3086         struct rpc_message msg = {
3087                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3088                 .rpc_argp       = clp,
3089                 .rpc_cred       = cred,
3090         };
3091
3092         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3093                         &nfs4_renew_ops, (void *)jiffies);
3094 }
3095
3096 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3097 {
3098         struct rpc_message msg = {
3099                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3100                 .rpc_argp       = clp,
3101                 .rpc_cred       = cred,
3102         };
3103         unsigned long now = jiffies;
3104         int status;
3105
3106         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3107         if (status < 0)
3108                 return status;
3109         spin_lock(&clp->cl_lock);
3110         if (time_before(clp->cl_last_renewal,now))
3111                 clp->cl_last_renewal = now;
3112         spin_unlock(&clp->cl_lock);
3113         return 0;
3114 }
3115
3116 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3117 {
3118         return (server->caps & NFS_CAP_ACLS)
3119                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3120                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3121 }
3122
3123 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3124  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3125  * the stack.
3126  */
3127 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3128
3129 static void buf_to_pages(const void *buf, size_t buflen,
3130                 struct page **pages, unsigned int *pgbase)
3131 {
3132         const void *p = buf;
3133
3134         *pgbase = offset_in_page(buf);
3135         p -= *pgbase;
3136         while (p < buf + buflen) {
3137                 *(pages++) = virt_to_page(p);
3138                 p += PAGE_CACHE_SIZE;
3139         }
3140 }
3141
3142 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3143                 struct page **pages, unsigned int *pgbase)
3144 {
3145         struct page *newpage, **spages;
3146         int rc = 0;
3147         size_t len;
3148         spages = pages;
3149
3150         do {
3151                 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3152                 newpage = alloc_page(GFP_KERNEL);
3153
3154                 if (newpage == NULL)
3155                         goto unwind;
3156                 memcpy(page_address(newpage), buf, len);
3157                 buf += len;
3158                 buflen -= len;
3159                 *pages++ = newpage;
3160                 rc++;
3161         } while (buflen != 0);
3162
3163         return rc;
3164
3165 unwind:
3166         for(; rc > 0; rc--)
3167                 __free_page(spages[rc-1]);
3168         return -ENOMEM;
3169 }
3170
3171 struct nfs4_cached_acl {
3172         int cached;
3173         size_t len;
3174         char data[0];
3175 };
3176
3177 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3178 {
3179         struct nfs_inode *nfsi = NFS_I(inode);
3180
3181         spin_lock(&inode->i_lock);
3182         kfree(nfsi->nfs4_acl);
3183         nfsi->nfs4_acl = acl;
3184         spin_unlock(&inode->i_lock);
3185 }
3186
3187 static void nfs4_zap_acl_attr(struct inode *inode)
3188 {
3189         nfs4_set_cached_acl(inode, NULL);
3190 }
3191
3192 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3193 {
3194         struct nfs_inode *nfsi = NFS_I(inode);
3195         struct nfs4_cached_acl *acl;
3196         int ret = -ENOENT;
3197
3198         spin_lock(&inode->i_lock);
3199         acl = nfsi->nfs4_acl;
3200         if (acl == NULL)
3201                 goto out;
3202         if (buf == NULL) /* user is just asking for length */
3203                 goto out_len;
3204         if (acl->cached == 0)
3205                 goto out;
3206         ret = -ERANGE; /* see getxattr(2) man page */
3207         if (acl->len > buflen)
3208                 goto out;
3209         memcpy(buf, acl->data, acl->len);
3210 out_len:
3211         ret = acl->len;
3212 out:
3213         spin_unlock(&inode->i_lock);
3214         return ret;
3215 }
3216
3217 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3218 {
3219         struct nfs4_cached_acl *acl;
3220
3221         if (buf && acl_len <= PAGE_SIZE) {
3222                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3223                 if (acl == NULL)
3224                         goto out;
3225                 acl->cached = 1;
3226                 memcpy(acl->data, buf, acl_len);
3227         } else {
3228                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3229                 if (acl == NULL)
3230                         goto out;
3231                 acl->cached = 0;
3232         }
3233         acl->len = acl_len;
3234 out:
3235         nfs4_set_cached_acl(inode, acl);
3236 }
3237
3238 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3239 {
3240         struct page *pages[NFS4ACL_MAXPAGES];
3241         struct nfs_getaclargs args = {
3242                 .fh = NFS_FH(inode),
3243                 .acl_pages = pages,
3244                 .acl_len = buflen,
3245         };
3246         struct nfs_getaclres res = {
3247                 .acl_len = buflen,
3248         };
3249         void *resp_buf;
3250         struct rpc_message msg = {
3251                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3252                 .rpc_argp = &args,
3253                 .rpc_resp = &res,
3254         };
3255         struct page *localpage = NULL;
3256         int ret;
3257
3258         if (buflen < PAGE_SIZE) {
3259                 /* As long as we're doing a round trip to the server anyway,
3260                  * let's be prepared for a page of acl data. */
3261                 localpage = alloc_page(GFP_KERNEL);
3262                 resp_buf = page_address(localpage);
3263                 if (localpage == NULL)
3264                         return -ENOMEM;
3265                 args.acl_pages[0] = localpage;
3266                 args.acl_pgbase = 0;
3267                 args.acl_len = PAGE_SIZE;
3268         } else {
3269                 resp_buf = buf;
3270                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3271         }
3272         ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3273         if (ret)
3274                 goto out_free;
3275         if (res.acl_len > args.acl_len)
3276                 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3277         else
3278                 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3279         if (buf) {
3280                 ret = -ERANGE;
3281                 if (res.acl_len > buflen)
3282                         goto out_free;
3283                 if (localpage)
3284                         memcpy(buf, resp_buf, res.acl_len);
3285         }
3286         ret = res.acl_len;
3287 out_free:
3288         if (localpage)
3289                 __free_page(localpage);
3290         return ret;
3291 }
3292
3293 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3294 {
3295         struct nfs4_exception exception = { };
3296         ssize_t ret;
3297         do {
3298                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3299                 if (ret >= 0)
3300                         break;
3301                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3302         } while (exception.retry);
3303         return ret;
3304 }
3305
3306 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3307 {
3308         struct nfs_server *server = NFS_SERVER(inode);
3309         int ret;
3310
3311         if (!nfs4_server_supports_acls(server))
3312                 return -EOPNOTSUPP;
3313         ret = nfs_revalidate_inode(server, inode);
3314         if (ret < 0)
3315                 return ret;
3316         ret = nfs4_read_cached_acl(inode, buf, buflen);
3317         if (ret != -ENOENT)
3318                 return ret;
3319         return nfs4_get_acl_uncached(inode, buf, buflen);
3320 }
3321
3322 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3323 {
3324         struct nfs_server *server = NFS_SERVER(inode);
3325         struct page *pages[NFS4ACL_MAXPAGES];
3326         struct nfs_setaclargs arg = {
3327                 .fh             = NFS_FH(inode),
3328                 .acl_pages      = pages,
3329                 .acl_len        = buflen,
3330         };
3331         struct nfs_setaclres res;
3332         struct rpc_message msg = {
3333                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3334                 .rpc_argp       = &arg,
3335                 .rpc_resp       = &res,
3336         };
3337         int ret, i;
3338
3339         if (!nfs4_server_supports_acls(server))
3340                 return -EOPNOTSUPP;
3341         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3342         if (i < 0)
3343                 return i;
3344         nfs_inode_return_delegation(inode);
3345         ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3346
3347         /*
3348          * Free each page after tx, so the only ref left is
3349          * held by the network stack
3350          */
3351         for (; i > 0; i--)
3352                 put_page(pages[i-1]);
3353
3354         nfs_access_zap_cache(inode);
3355         nfs_zap_acl_cache(inode);
3356         return ret;
3357 }
3358
3359 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3360 {
3361         struct nfs4_exception exception = { };
3362         int err;
3363         do {
3364                 err = nfs4_handle_exception(NFS_SERVER(inode),
3365                                 __nfs4_proc_set_acl(inode, buf, buflen),
3366                                 &exception);
3367         } while (exception.retry);
3368         return err;
3369 }
3370
3371 static int
3372 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3373 {
3374         if (!clp || task->tk_status >= 0)
3375                 return 0;
3376         switch(task->tk_status) {
3377                 case -NFS4ERR_ADMIN_REVOKED:
3378                 case -NFS4ERR_BAD_STATEID:
3379                 case -NFS4ERR_OPENMODE:
3380                         if (state == NULL)
3381                                 break;
3382                         nfs4_state_mark_reclaim_nograce(clp, state);
3383                 case -NFS4ERR_STALE_CLIENTID:
3384                 case -NFS4ERR_STALE_STATEID:
3385                 case -NFS4ERR_EXPIRED:
3386                         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3387                         nfs4_schedule_state_recovery(clp);
3388                         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3389                                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3390                         task->tk_status = 0;
3391                         return -EAGAIN;
3392 #if defined(CONFIG_NFS_V4_1)
3393                 case -NFS4ERR_BADSESSION:
3394                 case -NFS4ERR_BADSLOT:
3395                 case -NFS4ERR_BAD_HIGH_SLOT:
3396                 case -NFS4ERR_DEADSESSION:
3397                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3398                 case -NFS4ERR_SEQ_FALSE_RETRY:
3399                 case -NFS4ERR_SEQ_MISORDERED:
3400                         dprintk("%s ERROR %d, Reset session\n", __func__,
3401                                 task->tk_status);
3402                         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3403                         task->tk_status = 0;
3404                         return -EAGAIN;
3405 #endif /* CONFIG_NFS_V4_1 */
3406                 case -NFS4ERR_DELAY:
3407                         if (server)
3408                                 nfs_inc_server_stats(server, NFSIOS_DELAY);
3409                 case -NFS4ERR_GRACE:
3410                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
3411                         task->tk_status = 0;
3412                         return -EAGAIN;
3413                 case -NFS4ERR_OLD_STATEID:
3414                         task->tk_status = 0;
3415                         return -EAGAIN;
3416         }
3417         task->tk_status = nfs4_map_errors(task->tk_status);
3418         return 0;
3419 }
3420
3421 static int
3422 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3423 {
3424         return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3425 }
3426
3427 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3428 {
3429         nfs4_verifier sc_verifier;
3430         struct nfs4_setclientid setclientid = {
3431                 .sc_verifier = &sc_verifier,
3432                 .sc_prog = program,
3433         };
3434         struct rpc_message msg = {
3435                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3436                 .rpc_argp = &setclientid,
3437                 .rpc_resp = clp,
3438                 .rpc_cred = cred,
3439         };
3440         __be32 *p;
3441         int loop = 0;
3442         int status;
3443
3444         p = (__be32*)sc_verifier.data;
3445         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3446         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3447
3448         for(;;) {
3449                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3450                                 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3451                                 clp->cl_ipaddr,
3452                                 rpc_peeraddr2str(clp->cl_rpcclient,
3453                                                         RPC_DISPLAY_ADDR),
3454                                 rpc_peeraddr2str(clp->cl_rpcclient,
3455                                                         RPC_DISPLAY_PROTO),
3456                                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3457                                 clp->cl_id_uniquifier);
3458                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3459                                 sizeof(setclientid.sc_netid),
3460                                 rpc_peeraddr2str(clp->cl_rpcclient,
3461                                                         RPC_DISPLAY_NETID));
3462                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3463                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3464                                 clp->cl_ipaddr, port >> 8, port & 255);
3465
3466                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3467                 if (status != -NFS4ERR_CLID_INUSE)
3468                         break;
3469                 if (signalled())
3470                         break;
3471                 if (loop++ & 1)
3472                         ssleep(clp->cl_lease_time + 1);
3473                 else
3474                         if (++clp->cl_id_uniquifier == 0)
3475                                 break;
3476         }
3477         return status;
3478 }
3479
3480 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3481 {
3482         struct nfs_fsinfo fsinfo;
3483         struct rpc_message msg = {
3484                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3485                 .rpc_argp = clp,
3486                 .rpc_resp = &fsinfo,
3487                 .rpc_cred = cred,
3488         };
3489         unsigned long now;
3490         int status;
3491
3492         now = jiffies;
3493         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3494         if (status == 0) {
3495                 spin_lock(&clp->cl_lock);
3496                 clp->cl_lease_time = fsinfo.lease_time * HZ;
3497                 clp->cl_last_renewal = now;
3498                 spin_unlock(&clp->cl_lock);
3499         }
3500         return status;
3501 }
3502
3503 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3504 {
3505         long timeout = 0;
3506         int err;
3507         do {
3508                 err = _nfs4_proc_setclientid_confirm(clp, cred);
3509                 switch (err) {
3510                         case 0:
3511                                 return err;
3512                         case -NFS4ERR_RESOURCE:
3513                                 /* The IBM lawyers misread another document! */
3514                         case -NFS4ERR_DELAY:
3515                                 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3516                 }
3517         } while (err == 0);
3518         return err;
3519 }
3520
3521 struct nfs4_delegreturndata {
3522         struct nfs4_delegreturnargs args;
3523         struct nfs4_delegreturnres res;
3524         struct nfs_fh fh;
3525         nfs4_stateid stateid;
3526         unsigned long timestamp;
3527         struct nfs_fattr fattr;
3528         int rpc_status;
3529 };
3530
3531 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3532 {
3533         struct nfs4_delegreturndata *data = calldata;
3534
3535         nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3536                                      task->tk_status);
3537
3538         data->rpc_status = task->tk_status;
3539         if (data->rpc_status == 0)
3540                 renew_lease(data->res.server, data->timestamp);
3541 }
3542
3543 static void nfs4_delegreturn_release(void *calldata)
3544 {
3545         kfree(calldata);
3546 }
3547
3548 #if defined(CONFIG_NFS_V4_1)
3549 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3550 {
3551         struct nfs4_delegreturndata *d_data;
3552
3553         d_data = (struct nfs4_delegreturndata *)data;
3554
3555         if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3556                                 &d_data->args.seq_args,
3557                                 &d_data->res.seq_res, 1, task))
3558                 return;
3559         rpc_call_start(task);
3560 }
3561 #endif /* CONFIG_NFS_V4_1 */
3562
3563 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3564 #if defined(CONFIG_NFS_V4_1)
3565         .rpc_call_prepare = nfs4_delegreturn_prepare,
3566 #endif /* CONFIG_NFS_V4_1 */
3567         .rpc_call_done = nfs4_delegreturn_done,
3568         .rpc_release = nfs4_delegreturn_release,
3569 };
3570
3571 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3572 {
3573         struct nfs4_delegreturndata *data;
3574         struct nfs_server *server = NFS_SERVER(inode);
3575         struct rpc_task *task;
3576         struct rpc_message msg = {
3577                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3578                 .rpc_cred = cred,
3579         };
3580         struct rpc_task_setup task_setup_data = {
3581                 .rpc_client = server->client,
3582                 .rpc_message = &msg,
3583                 .callback_ops = &nfs4_delegreturn_ops,
3584                 .flags = RPC_TASK_ASYNC,
3585         };
3586         int status = 0;
3587
3588         data = kzalloc(sizeof(*data), GFP_KERNEL);
3589         if (data == NULL)
3590                 return -ENOMEM;
3591         data->args.fhandle = &data->fh;
3592         data->args.stateid = &data->stateid;
3593         data->args.bitmask = server->attr_bitmask;
3594         nfs_copy_fh(&data->fh, NFS_FH(inode));
3595         memcpy(&data->stateid, stateid, sizeof(data->stateid));
3596         data->res.fattr = &data->fattr;
3597         data->res.server = server;
3598         data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3599         nfs_fattr_init(data->res.fattr);
3600         data->timestamp = jiffies;
3601         data->rpc_status = 0;
3602
3603         task_setup_data.callback_data = data;
3604         msg.rpc_argp = &data->args,
3605         msg.rpc_resp = &data->res,
3606         task = rpc_run_task(&task_setup_data);
3607         if (IS_ERR(task))
3608                 return PTR_ERR(task);
3609         if (!issync)
3610                 goto out;
3611         status = nfs4_wait_for_completion_rpc_task(task);
3612         if (status != 0)
3613                 goto out;
3614         status = data->rpc_status;
3615         if (status != 0)
3616                 goto out;
3617         nfs_refresh_inode(inode, &data->fattr);
3618 out:
3619         rpc_put_task(task);
3620         return status;
3621 }
3622
3623 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3624 {
3625         struct nfs_server *server = NFS_SERVER(inode);
3626         struct nfs4_exception exception = { };
3627         int err;
3628         do {
3629                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3630                 switch (err) {
3631                         case -NFS4ERR_STALE_STATEID:
3632                         case -NFS4ERR_EXPIRED:
3633                         case 0:
3634                                 return 0;
3635                 }
3636                 err = nfs4_handle_exception(server, err, &exception);
3637         } while (exception.retry);
3638         return err;
3639 }
3640
3641 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3642 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3643
3644 /* 
3645  * sleep, with exponential backoff, and retry the LOCK operation. 
3646  */
3647 static unsigned long
3648 nfs4_set_lock_task_retry(unsigned long timeout)
3649 {
3650         schedule_timeout_killable(timeout);
3651         timeout <<= 1;
3652         if (timeout > NFS4_LOCK_MAXTIMEOUT)
3653                 return NFS4_LOCK_MAXTIMEOUT;
3654         return timeout;
3655 }
3656
3657 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3658 {
3659         struct inode *inode = state->inode;
3660         struct nfs_server *server = NFS_SERVER(inode);
3661         struct nfs_client *clp = server->nfs_client;
3662         struct nfs_lockt_args arg = {
3663                 .fh = NFS_FH(inode),
3664                 .fl = request,
3665         };
3666         struct nfs_lockt_res res = {
3667                 .denied = request,
3668         };
3669         struct rpc_message msg = {
3670                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3671                 .rpc_argp       = &arg,
3672                 .rpc_resp       = &res,
3673                 .rpc_cred       = state->owner->so_cred,
3674         };
3675         struct nfs4_lock_state *lsp;
3676         int status;
3677
3678         arg.lock_owner.clientid = clp->cl_clientid;
3679         status = nfs4_set_lock_state(state, request);
3680         if (status != 0)
3681                 goto out;
3682         lsp = request->fl_u.nfs4_fl.owner;
3683         arg.lock_owner.id = lsp->ls_id.id;
3684         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3685         switch (status) {
3686                 case 0:
3687                         request->fl_type = F_UNLCK;
3688                         break;
3689                 case -NFS4ERR_DENIED:
3690                         status = 0;
3691         }
3692         request->fl_ops->fl_release_private(request);
3693 out:
3694         return status;
3695 }
3696
3697 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3698 {
3699         struct nfs4_exception exception = { };
3700         int err;
3701
3702         do {
3703                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3704                                 _nfs4_proc_getlk(state, cmd, request),
3705                                 &exception);
3706         } while (exception.retry);
3707         return err;
3708 }
3709
3710 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3711 {
3712         int res = 0;
3713         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3714                 case FL_POSIX:
3715                         res = posix_lock_file_wait(file, fl);
3716                         break;
3717                 case FL_FLOCK:
3718                         res = flock_lock_file_wait(file, fl);
3719                         break;
3720                 default:
3721                         BUG();
3722         }
3723         return res;
3724 }
3725
3726 struct nfs4_unlockdata {
3727         struct nfs_locku_args arg;
3728         struct nfs_locku_res res;
3729         struct nfs4_lock_state *lsp;
3730         struct nfs_open_context *ctx;
3731         struct file_lock fl;
3732         const struct nfs_server *server;
3733         unsigned long timestamp;
3734 };
3735
3736 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3737                 struct nfs_open_context *ctx,
3738                 struct nfs4_lock_state *lsp,
3739                 struct nfs_seqid *seqid)
3740 {
3741         struct nfs4_unlockdata *p;
3742         struct inode *inode = lsp->ls_state->inode;
3743
3744         p = kzalloc(sizeof(*p), GFP_KERNEL);
3745         if (p == NULL)
3746                 return NULL;
3747         p->arg.fh = NFS_FH(inode);
3748         p->arg.fl = &p->fl;
3749         p->arg.seqid = seqid;
3750         p->res.seqid = seqid;
3751         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3752         p->arg.stateid = &lsp->ls_stateid;
3753         p->lsp = lsp;
3754         atomic_inc(&lsp->ls_count);
3755         /* Ensure we don't close file until we're done freeing locks! */
3756         p->ctx = get_nfs_open_context(ctx);
3757         memcpy(&p->fl, fl, sizeof(p->fl));
3758         p->server = NFS_SERVER(inode);
3759         return p;
3760 }
3761
3762 static void nfs4_locku_release_calldata(void *data)
3763 {
3764         struct nfs4_unlockdata *calldata = data;
3765         nfs_free_seqid(calldata->arg.seqid);
3766         nfs4_put_lock_state(calldata->lsp);
3767         put_nfs_open_context(calldata->ctx);
3768         kfree(calldata);
3769 }
3770
3771 static void nfs4_locku_done(struct rpc_task *task, void *data)
3772 {
3773         struct nfs4_unlockdata *calldata = data;
3774
3775         nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3776                            task->tk_status);
3777         if (RPC_ASSASSINATED(task))
3778                 return;
3779         switch (task->tk_status) {
3780                 case 0:
3781                         memcpy(calldata->lsp->ls_stateid.data,
3782                                         calldata->res.stateid.data,
3783                                         sizeof(calldata->lsp->ls_stateid.data));
3784                         renew_lease(calldata->server, calldata->timestamp);
3785                         break;
3786                 case -NFS4ERR_BAD_STATEID:
3787                 case -NFS4ERR_OLD_STATEID:
3788                 case -NFS4ERR_STALE_STATEID:
3789                 case -NFS4ERR_EXPIRED:
3790                         break;
3791                 default:
3792                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3793                                 nfs4_restart_rpc(task,
3794                                                 calldata->server->nfs_client);
3795         }
3796         nfs4_sequence_free_slot(calldata->server->nfs_client,
3797                                 &calldata->res.seq_res);
3798 }
3799
3800 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3801 {
3802         struct nfs4_unlockdata *calldata = data;
3803
3804         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3805                 return;
3806         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3807                 /* Note: exit _without_ running nfs4_locku_done */
3808                 task->tk_action = NULL;
3809                 return;
3810         }
3811         calldata->timestamp = jiffies;
3812         if (nfs4_setup_sequence(calldata->server->nfs_client,
3813                                 &calldata->arg.seq_args,
3814                                 &calldata->res.seq_res, 1, task))
3815                 return;
3816         rpc_call_start(task);
3817 }
3818
3819 static const struct rpc_call_ops nfs4_locku_ops = {
3820         .rpc_call_prepare = nfs4_locku_prepare,
3821         .rpc_call_done = nfs4_locku_done,
3822         .rpc_release = nfs4_locku_release_calldata,
3823 };
3824
3825 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3826                 struct nfs_open_context *ctx,
3827                 struct nfs4_lock_state *lsp,
3828                 struct nfs_seqid *seqid)
3829 {
3830         struct nfs4_unlockdata *data;
3831         struct rpc_message msg = {
3832                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3833                 .rpc_cred = ctx->cred,
3834         };
3835         struct rpc_task_setup task_setup_data = {
3836                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3837                 .rpc_message = &msg,
3838                 .callback_ops = &nfs4_locku_ops,
3839                 .workqueue = nfsiod_workqueue,
3840                 .flags = RPC_TASK_ASYNC,
3841         };
3842
3843         /* Ensure this is an unlock - when canceling a lock, the
3844          * canceled lock is passed in, and it won't be an unlock.
3845          */
3846         fl->fl_type = F_UNLCK;
3847
3848         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3849         if (data == NULL) {
3850                 nfs_free_seqid(seqid);
3851                 return ERR_PTR(-ENOMEM);
3852         }
3853
3854         msg.rpc_argp = &data->arg,
3855         msg.rpc_resp = &data->res,
3856         task_setup_data.callback_data = data;
3857         return rpc_run_task(&task_setup_data);
3858 }
3859
3860 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3861 {
3862         struct nfs_inode *nfsi = NFS_I(state->inode);
3863         struct nfs_seqid *seqid;
3864         struct nfs4_lock_state *lsp;
3865         struct rpc_task *task;
3866         int status = 0;
3867         unsigned char fl_flags = request->fl_flags;
3868
3869         status = nfs4_set_lock_state(state, request);
3870         /* Unlock _before_ we do the RPC call */
3871         request->fl_flags |= FL_EXISTS;
3872         down_read(&nfsi->rwsem);
3873         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3874                 up_read(&nfsi->rwsem);
3875                 goto out;
3876         }
3877         up_read(&nfsi->rwsem);
3878         if (status != 0)
3879                 goto out;
3880         /* Is this a delegated lock? */
3881         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3882                 goto out;
3883         lsp = request->fl_u.nfs4_fl.owner;
3884         seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3885         status = -ENOMEM;
3886         if (seqid == NULL)
3887                 goto out;
3888         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3889         status = PTR_ERR(task);
3890         if (IS_ERR(task))
3891                 goto out;
3892         status = nfs4_wait_for_completion_rpc_task(task);
3893         rpc_put_task(task);
3894 out:
3895         request->fl_flags = fl_flags;
3896         return status;
3897 }
3898
3899 struct nfs4_lockdata {
3900         struct nfs_lock_args arg;
3901         struct nfs_lock_res res;
3902         struct nfs4_lock_state *lsp;
3903         struct nfs_open_context *ctx;
3904         struct file_lock fl;
3905         unsigned long timestamp;
3906         int rpc_status;
3907         int cancelled;
3908         struct nfs_server *server;
3909 };
3910
3911 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3912                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3913 {
3914         struct nfs4_lockdata *p;
3915         struct inode *inode = lsp->ls_state->inode;
3916         struct nfs_server *server = NFS_SERVER(inode);
3917
3918         p = kzalloc(sizeof(*p), GFP_KERNEL);
3919         if (p == NULL)
3920                 return NULL;
3921
3922         p->arg.fh = NFS_FH(inode);
3923         p->arg.fl = &p->fl;
3924         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3925         if (p->arg.open_seqid == NULL)
3926                 goto out_free;
3927         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3928         if (p->arg.lock_seqid == NULL)
3929                 goto out_free_seqid;
3930         p->arg.lock_stateid = &lsp->ls_stateid;
3931         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3932         p->arg.lock_owner.id = lsp->ls_id.id;
3933         p->res.lock_seqid = p->arg.lock_seqid;
3934         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3935         p->lsp = lsp;
3936         p->server = server;
3937         atomic_inc(&lsp->ls_count);
3938         p->ctx = get_nfs_open_context(ctx);
3939         memcpy(&p->fl, fl, sizeof(p->fl));
3940         return p;
3941 out_free_seqid:
3942         nfs_free_seqid(p->arg.open_seqid);
3943 out_free:
3944         kfree(p);
3945         return NULL;
3946 }
3947
3948 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3949 {
3950         struct nfs4_lockdata *data = calldata;
3951         struct nfs4_state *state = data->lsp->ls_state;
3952
3953         dprintk("%s: begin!\n", __func__);
3954         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3955                 return;
3956         /* Do we need to do an open_to_lock_owner? */
3957         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3958                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3959                         return;
3960                 data->arg.open_stateid = &state->stateid;
3961                 data->arg.new_lock_owner = 1;
3962                 data->res.open_seqid = data->arg.open_seqid;
3963         } else
3964                 data->arg.new_lock_owner = 0;
3965         data->timestamp = jiffies;
3966         if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3967                                 &data->res.seq_res, 1, task))
3968                 return;
3969         rpc_call_start(task);
3970         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3971 }
3972
3973 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3974 {
3975         struct nfs4_lockdata *data = calldata;
3976
3977         dprintk("%s: begin!\n", __func__);
3978
3979         nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3980                                      task->tk_status);
3981
3982         data->rpc_status = task->tk_status;
3983         if (RPC_ASSASSINATED(task))
3984                 goto out;
3985         if (data->arg.new_lock_owner != 0) {
3986                 if (data->rpc_status == 0)
3987                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3988                 else
3989                         goto out;
3990         }
3991         if (data->rpc_status == 0) {
3992                 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3993                                         sizeof(data->lsp->ls_stateid.data));
3994                 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3995                 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3996         }
3997 out:
3998         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3999 }
4000
4001 static void nfs4_lock_release(void *calldata)
4002 {
4003         struct nfs4_lockdata *data = calldata;
4004
4005         dprintk("%s: begin!\n", __func__);
4006         nfs_free_seqid(data->arg.open_seqid);
4007         if (data->cancelled != 0) {
4008                 struct rpc_task *task;
4009                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4010                                 data->arg.lock_seqid);
4011                 if (!IS_ERR(task))
4012                         rpc_put_task(task);
4013                 dprintk("%s: cancelling lock!\n", __func__);
4014         } else
4015                 nfs_free_seqid(data->arg.lock_seqid);
4016         nfs4_put_lock_state(data->lsp);
4017         put_nfs_open_context(data->ctx);
4018         kfree(data);
4019         dprintk("%s: done!\n", __func__);
4020 }
4021
4022 static const struct rpc_call_ops nfs4_lock_ops = {
4023         .rpc_call_prepare = nfs4_lock_prepare,
4024         .rpc_call_done = nfs4_lock_done,
4025         .rpc_release = nfs4_lock_release,
4026 };
4027
4028 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4029 {
4030         struct nfs_client *clp = server->nfs_client;
4031         struct nfs4_state *state = lsp->ls_state;
4032
4033         switch (error) {
4034         case -NFS4ERR_ADMIN_REVOKED:
4035         case -NFS4ERR_BAD_STATEID:
4036         case -NFS4ERR_EXPIRED:
4037                 if (new_lock_owner != 0 ||
4038                    (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4039                         nfs4_state_mark_reclaim_nograce(clp, state);
4040                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4041         };
4042 }
4043
4044 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4045 {
4046         struct nfs4_lockdata *data;
4047         struct rpc_task *task;
4048         struct rpc_message msg = {
4049                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4050                 .rpc_cred = state->owner->so_cred,
4051         };
4052         struct rpc_task_setup task_setup_data = {
4053                 .rpc_client = NFS_CLIENT(state->inode),
4054                 .rpc_message = &msg,
4055                 .callback_ops = &nfs4_lock_ops,
4056                 .workqueue = nfsiod_workqueue,
4057                 .flags = RPC_TASK_ASYNC,
4058         };
4059         int ret;
4060
4061         dprintk("%s: begin!\n", __func__);
4062         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4063                         fl->fl_u.nfs4_fl.owner);
4064         if (data == NULL)
4065                 return -ENOMEM;
4066         if (IS_SETLKW(cmd))
4067                 data->arg.block = 1;
4068         if (reclaim != 0)
4069                 data->arg.reclaim = 1;
4070         msg.rpc_argp = &data->arg,
4071         msg.rpc_resp = &data->res,
4072         task_setup_data.callback_data = data;
4073         task = rpc_run_task(&task_setup_data);
4074         if (IS_ERR(task))
4075                 return PTR_ERR(task);
4076         ret = nfs4_wait_for_completion_rpc_task(task);
4077         if (ret == 0) {
4078                 ret = data->rpc_status;
4079                 if (ret)
4080                         nfs4_handle_setlk_error(data->server, data->lsp,
4081                                         data->arg.new_lock_owner, ret);
4082         } else
4083                 data->cancelled = 1;
4084         rpc_put_task(task);
4085         dprintk("%s: done, ret = %d!\n", __func__, ret);
4086         return ret;
4087 }
4088
4089 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4090 {
4091         struct nfs_server *server = NFS_SERVER(state->inode);
4092         struct nfs4_exception exception = { };
4093         int err;
4094
4095         do {
4096                 /* Cache the lock if possible... */
4097                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4098                         return 0;
4099                 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4100                 if (err != -NFS4ERR_DELAY)
4101                         break;
4102                 nfs4_handle_exception(server, err, &exception);
4103         } while (exception.retry);
4104         return err;
4105 }
4106
4107 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4108 {
4109         struct nfs_server *server = NFS_SERVER(state->inode);
4110         struct nfs4_exception exception = { };
4111         int err;
4112
4113         err = nfs4_set_lock_state(state, request);
4114         if (err != 0)
4115                 return err;
4116         do {
4117                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4118                         return 0;
4119                 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4120                 switch (err) {
4121                 default:
4122                         goto out;
4123                 case -NFS4ERR_GRACE:
4124                 case -NFS4ERR_DELAY:
4125                         nfs4_handle_exception(server, err, &exception);
4126                         err = 0;
4127                 }
4128         } while (exception.retry);
4129 out:
4130         return err;
4131 }
4132
4133 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4134 {
4135         struct nfs_inode *nfsi = NFS_I(state->inode);
4136         unsigned char fl_flags = request->fl_flags;
4137         int status = -ENOLCK;
4138
4139         if ((fl_flags & FL_POSIX) &&
4140                         !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4141                 goto out;
4142         /* Is this a delegated open? */
4143         status = nfs4_set_lock_state(state, request);
4144         if (status != 0)
4145                 goto out;
4146         request->fl_flags |= FL_ACCESS;
4147         status = do_vfs_lock(request->fl_file, request);
4148         if (status < 0)
4149                 goto out;
4150         down_read(&nfsi->rwsem);
4151         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4152                 /* Yes: cache locks! */
4153                 /* ...but avoid races with delegation recall... */
4154                 request->fl_flags = fl_flags & ~FL_SLEEP;
4155                 status = do_vfs_lock(request->fl_file, request);
4156                 goto out_unlock;
4157         }
4158         status = _nfs4_do_setlk(state, cmd, request, 0);
4159         if (status != 0)
4160                 goto out_unlock;
4161         /* Note: we always want to sleep here! */
4162         request->fl_flags = fl_flags | FL_SLEEP;
4163         if (do_vfs_lock(request->fl_file, request) < 0)
4164                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4165 out_unlock:
4166         up_read(&nfsi->rwsem);
4167 out:
4168         request->fl_flags = fl_flags;
4169         return status;
4170 }
4171
4172 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4173 {
4174         struct nfs4_exception exception = { };
4175         int err;
4176
4177         do {
4178                 err = _nfs4_proc_setlk(state, cmd, request);
4179                 if (err == -NFS4ERR_DENIED)
4180                         err = -EAGAIN;
4181                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4182                                 err, &exception);
4183         } while (exception.retry);
4184         return err;
4185 }
4186
4187 static int
4188 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4189 {
4190         struct nfs_open_context *ctx;
4191         struct nfs4_state *state;
4192         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4193         int status;
4194
4195         /* verify open state */
4196         ctx = nfs_file_open_context(filp);
4197         state = ctx->state;
4198
4199         if (request->fl_start < 0 || request->fl_end < 0)
4200                 return -EINVAL;
4201
4202         if (IS_GETLK(cmd)) {
4203                 if (state != NULL)
4204                         return nfs4_proc_getlk(state, F_GETLK, request);
4205                 return 0;
4206         }
4207
4208         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4209                 return -EINVAL;
4210
4211         if (request->fl_type == F_UNLCK) {
4212                 if (state != NULL)
4213                         return nfs4_proc_unlck(state, cmd, request);
4214                 return 0;
4215         }
4216
4217         if (state == NULL)
4218                 return -ENOLCK;
4219         do {
4220                 status = nfs4_proc_setlk(state, cmd, request);
4221                 if ((status != -EAGAIN) || IS_SETLK(cmd))
4222                         break;
4223                 timeout = nfs4_set_lock_task_retry(timeout);
4224                 status = -ERESTARTSYS;
4225                 if (signalled())
4226                         break;
4227         } while(status < 0);
4228         return status;
4229 }
4230
4231 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4232 {
4233         struct nfs_server *server = NFS_SERVER(state->inode);
4234         struct nfs4_exception exception = { };
4235         int err;
4236
4237         err = nfs4_set_lock_state(state, fl);
4238         if (err != 0)
4239                 goto out;
4240         do {
4241                 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4242                 switch (err) {
4243                         default:
4244                                 printk(KERN_ERR "%s: unhandled error %d.\n",
4245                                                 __func__, err);
4246                         case 0:
4247                         case -ESTALE:
4248                                 goto out;
4249                         case -NFS4ERR_EXPIRED:
4250                         case -NFS4ERR_STALE_CLIENTID:
4251                         case -NFS4ERR_STALE_STATEID:
4252                                 nfs4_schedule_state_recovery(server->nfs_client);
4253                                 goto out;
4254                         case -ERESTARTSYS:
4255                                 /*
4256                                  * The show must go on: exit, but mark the
4257                                  * stateid as needing recovery.
4258                                  */
4259                         case -NFS4ERR_ADMIN_REVOKED:
4260                         case -NFS4ERR_BAD_STATEID:
4261                         case -NFS4ERR_OPENMODE:
4262                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4263                                 err = 0;
4264                                 goto out;
4265                         case -ENOMEM:
4266                         case -NFS4ERR_DENIED:
4267                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4268                                 err = 0;
4269                                 goto out;
4270                         case -NFS4ERR_DELAY:
4271                                 break;
4272                 }
4273                 err = nfs4_handle_exception(server, err, &exception);
4274         } while (exception.retry);
4275 out:
4276         return err;
4277 }
4278
4279 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4280
4281 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4282                 size_t buflen, int flags)
4283 {
4284         struct inode *inode = dentry->d_inode;
4285
4286         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4287                 return -EOPNOTSUPP;
4288
4289         return nfs4_proc_set_acl(inode, buf, buflen);
4290 }
4291
4292 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4293  * and that's what we'll do for e.g. user attributes that haven't been set.
4294  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4295  * attributes in kernel-managed attribute namespaces. */
4296 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4297                 size_t buflen)
4298 {
4299         struct inode *inode = dentry->d_inode;
4300
4301         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4302                 return -EOPNOTSUPP;
4303
4304         return nfs4_proc_get_acl(inode, buf, buflen);
4305 }
4306
4307 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4308 {
4309         size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4310
4311         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4312                 return 0;
4313         if (buf && buflen < len)
4314                 return -ERANGE;
4315         if (buf)
4316                 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4317         return len;
4318 }
4319
4320 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4321 {
4322         if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4323                 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4324                 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4325                 return;
4326
4327         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4328                 NFS_ATTR_FATTR_NLINK;
4329         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4330         fattr->nlink = 2;
4331 }
4332
4333 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4334                 struct nfs4_fs_locations *fs_locations, struct page *page)
4335 {
4336         struct nfs_server *server = NFS_SERVER(dir);
4337         u32 bitmask[2] = {
4338                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4339                 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4340         };
4341         struct nfs4_fs_locations_arg args = {
4342                 .dir_fh = NFS_FH(dir),
4343                 .name = name,
4344                 .page = page,
4345                 .bitmask = bitmask,
4346         };
4347         struct nfs4_fs_locations_res res = {
4348                 .fs_locations = fs_locations,
4349         };
4350         struct rpc_message msg = {
4351                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4352                 .rpc_argp = &args,
4353                 .rpc_resp = &res,
4354         };
4355         int status;
4356
4357         dprintk("%s: start\n", __func__);
4358         nfs_fattr_init(&fs_locations->fattr);
4359         fs_locations->server = server;
4360         fs_locations->nlocations = 0;
4361         status = nfs4_call_sync(server, &msg, &args, &res, 0);
4362         nfs_fixup_referral_attributes(&fs_locations->fattr);
4363         dprintk("%s: returned status = %d\n", __func__, status);
4364         return status;
4365 }
4366
4367 #ifdef CONFIG_NFS_V4_1
4368 /*
4369  * nfs4_proc_exchange_id()
4370  *
4371  * Since the clientid has expired, all compounds using sessions
4372  * associated with the stale clientid will be returning
4373  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4374  * be in some phase of session reset.
4375  */
4376 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4377 {
4378         nfs4_verifier verifier;
4379         struct nfs41_exchange_id_args args = {
4380                 .client = clp,
4381                 .flags = clp->cl_exchange_flags,
4382         };
4383         struct nfs41_exchange_id_res res = {
4384                 .client = clp,
4385         };
4386         int status;
4387         struct rpc_message msg = {
4388                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4389                 .rpc_argp = &args,
4390                 .rpc_resp = &res,
4391                 .rpc_cred = cred,
4392         };
4393         __be32 *p;
4394
4395         dprintk("--> %s\n", __func__);
4396         BUG_ON(clp == NULL);
4397
4398         p = (u32 *)verifier.data;
4399         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4400         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4401         args.verifier = &verifier;
4402
4403         while (1) {
4404                 args.id_len = scnprintf(args.id, sizeof(args.id),
4405                                         "%s/%s %u",
4406                                         clp->cl_ipaddr,
4407                                         rpc_peeraddr2str(clp->cl_rpcclient,
4408                                                          RPC_DISPLAY_ADDR),
4409                                         clp->cl_id_uniquifier);
4410
4411                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4412
4413                 if (status != NFS4ERR_CLID_INUSE)
4414                         break;
4415
4416                 if (signalled())
4417                         break;
4418
4419                 if (++clp->cl_id_uniquifier == 0)
4420                         break;
4421         }
4422
4423         dprintk("<-- %s status= %d\n", __func__, status);
4424         return status;
4425 }
4426
4427 struct nfs4_get_lease_time_data {
4428         struct nfs4_get_lease_time_args *args;
4429         struct nfs4_get_lease_time_res *res;
4430         struct nfs_client *clp;
4431 };
4432
4433 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4434                                         void *calldata)
4435 {
4436         int ret;
4437         struct nfs4_get_lease_time_data *data =
4438                         (struct nfs4_get_lease_time_data *)calldata;
4439
4440         dprintk("--> %s\n", __func__);
4441         /* just setup sequence, do not trigger session recovery
4442            since we're invoked within one */
4443         ret = nfs41_setup_sequence(data->clp->cl_session,
4444                                         &data->args->la_seq_args,
4445                                         &data->res->lr_seq_res, 0, task);
4446
4447         BUG_ON(ret == -EAGAIN);
4448         rpc_call_start(task);
4449         dprintk("<-- %s\n", __func__);
4450 }
4451
4452 /*
4453  * Called from nfs4_state_manager thread for session setup, so don't recover
4454  * from sequence operation or clientid errors.
4455  */
4456 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4457 {
4458         struct nfs4_get_lease_time_data *data =
4459                         (struct nfs4_get_lease_time_data *)calldata;
4460
4461         dprintk("--> %s\n", __func__);
4462         nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4463         switch (task->tk_status) {
4464         case -NFS4ERR_DELAY:
4465         case -NFS4ERR_GRACE:
4466                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4467                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4468                 task->tk_status = 0;
4469                 nfs4_restart_rpc(task, data->clp);
4470                 return;
4471         }
4472         nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4473         dprintk("<-- %s\n", __func__);
4474 }
4475
4476 struct rpc_call_ops nfs4_get_lease_time_ops = {
4477         .rpc_call_prepare = nfs4_get_lease_time_prepare,
4478         .rpc_call_done = nfs4_get_lease_time_done,
4479 };
4480
4481 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4482 {
4483         struct rpc_task *task;
4484         struct nfs4_get_lease_time_args args;
4485         struct nfs4_get_lease_time_res res = {
4486                 .lr_fsinfo = fsinfo,
4487         };
4488         struct nfs4_get_lease_time_data data = {
4489                 .args = &args,
4490                 .res = &res,
4491                 .clp = clp,
4492         };
4493         struct rpc_message msg = {
4494                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4495                 .rpc_argp = &args,
4496                 .rpc_resp = &res,
4497         };
4498         struct rpc_task_setup task_setup = {
4499                 .rpc_client = clp->cl_rpcclient,
4500                 .rpc_message = &msg,
4501                 .callback_ops = &nfs4_get_lease_time_ops,
4502                 .callback_data = &data
4503         };
4504         int status;
4505
4506         res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4507         dprintk("--> %s\n", __func__);
4508         task = rpc_run_task(&task_setup);
4509
4510         if (IS_ERR(task))
4511                 status = PTR_ERR(task);
4512         else {
4513                 status = task->tk_status;
4514                 rpc_put_task(task);
4515         }
4516         dprintk("<-- %s return %d\n", __func__, status);
4517
4518         return status;
4519 }
4520
4521 /*
4522  * Reset a slot table
4523  */
4524 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4525                 int old_max_slots, int ivalue)
4526 {
4527         int i;
4528         int ret = 0;
4529
4530         dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4531
4532         /*
4533          * Until we have dynamic slot table adjustment, insist
4534          * upon the same slot table size
4535          */
4536         if (max_slots != old_max_slots) {
4537                 dprintk("%s reset slot table does't match old\n",
4538                         __func__);
4539                 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4540                 goto out;
4541         }
4542         spin_lock(&tbl->slot_tbl_lock);
4543         for (i = 0; i < max_slots; ++i)
4544                 tbl->slots[i].seq_nr = ivalue;
4545         tbl->highest_used_slotid = -1;
4546         spin_unlock(&tbl->slot_tbl_lock);
4547         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4548                 tbl, tbl->slots, tbl->max_slots);
4549 out:
4550         dprintk("<-- %s: return %d\n", __func__, ret);
4551         return ret;
4552 }
4553
4554 /*
4555  * Reset the forechannel and backchannel slot tables
4556  */
4557 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4558 {
4559         int status;
4560
4561         status = nfs4_reset_slot_table(&session->fc_slot_table,
4562                         session->fc_attrs.max_reqs,
4563                         session->fc_slot_table.max_slots,
4564                         1);
4565         if (status)
4566                 return status;
4567
4568         status = nfs4_reset_slot_table(&session->bc_slot_table,
4569                         session->bc_attrs.max_reqs,
4570                         session->bc_slot_table.max_slots,
4571                         0);
4572         return status;
4573 }
4574
4575 /* Destroy the slot table */
4576 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4577 {
4578         if (session->fc_slot_table.slots != NULL) {
4579                 kfree(session->fc_slot_table.slots);
4580                 session->fc_slot_table.slots = NULL;
4581         }
4582         if (session->bc_slot_table.slots != NULL) {
4583                 kfree(session->bc_slot_table.slots);
4584                 session->bc_slot_table.slots = NULL;
4585         }
4586         return;
4587 }
4588
4589 /*
4590  * Initialize slot table
4591  */
4592 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4593                 int max_slots, int ivalue)
4594 {
4595         int i;
4596         struct nfs4_slot *slot;
4597         int ret = -ENOMEM;
4598
4599         BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4600
4601         dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4602
4603         slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4604         if (!slot)
4605                 goto out;
4606         for (i = 0; i < max_slots; ++i)
4607                 slot[i].seq_nr = ivalue;
4608         ret = 0;
4609
4610         spin_lock(&tbl->slot_tbl_lock);
4611         if (tbl->slots != NULL) {
4612                 spin_unlock(&tbl->slot_tbl_lock);
4613                 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4614                         __func__, tbl, tbl->slots);
4615                 WARN_ON(1);
4616                 goto out_free;
4617         }
4618         tbl->max_slots = max_slots;
4619         tbl->slots = slot;
4620         tbl->highest_used_slotid = -1;  /* no slot is currently used */
4621         spin_unlock(&tbl->slot_tbl_lock);
4622         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4623                 tbl, tbl->slots, tbl->max_slots);
4624 out:
4625         dprintk("<-- %s: return %d\n", __func__, ret);
4626         return ret;
4627
4628 out_free:
4629         kfree(slot);
4630         goto out;
4631 }
4632
4633 /*
4634  * Initialize the forechannel and backchannel tables
4635  */
4636 static int nfs4_init_slot_tables(struct nfs4_session *session)
4637 {
4638         int status;
4639
4640         status = nfs4_init_slot_table(&session->fc_slot_table,
4641                         session->fc_attrs.max_reqs, 1);
4642         if (status)
4643                 return status;
4644
4645         status = nfs4_init_slot_table(&session->bc_slot_table,
4646                         session->bc_attrs.max_reqs, 0);
4647         if (status)
4648                 nfs4_destroy_slot_tables(session);
4649
4650         return status;
4651 }
4652
4653 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4654 {
4655         struct nfs4_session *session;
4656         struct nfs4_slot_table *tbl;
4657
4658         session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4659         if (!session)
4660                 return NULL;
4661
4662         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4663         /*
4664          * The create session reply races with the server back
4665          * channel probe. Mark the client NFS_CS_SESSION_INITING
4666          * so that the client back channel can find the
4667          * nfs_client struct
4668          */
4669         clp->cl_cons_state = NFS_CS_SESSION_INITING;
4670
4671         tbl = &session->fc_slot_table;
4672         spin_lock_init(&tbl->slot_tbl_lock);
4673         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4674
4675         tbl = &session->bc_slot_table;
4676         spin_lock_init(&tbl->slot_tbl_lock);
4677         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4678
4679         session->clp = clp;
4680         return session;
4681 }
4682
4683 void nfs4_destroy_session(struct nfs4_session *session)
4684 {
4685         nfs4_proc_destroy_session(session);
4686         dprintk("%s Destroy backchannel for xprt %p\n",
4687                 __func__, session->clp->cl_rpcclient->cl_xprt);
4688         xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4689                                 NFS41_BC_MIN_CALLBACKS);
4690         nfs4_destroy_slot_tables(session);
4691         kfree(session);
4692 }
4693
4694 /*
4695  * Initialize the values to be used by the client in CREATE_SESSION
4696  * If nfs4_init_session set the fore channel request and response sizes,
4697  * use them.
4698  *
4699  * Set the back channel max_resp_sz_cached to zero to force the client to
4700  * always set csa_cachethis to FALSE because the current implementation
4701  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4702  */
4703 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4704 {
4705         struct nfs4_session *session = args->client->cl_session;
4706         unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4707                      mxresp_sz = session->fc_attrs.max_resp_sz;
4708
4709         if (mxrqst_sz == 0)
4710                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4711         if (mxresp_sz == 0)
4712                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4713         /* Fore channel attributes */
4714         args->fc_attrs.headerpadsz = 0;
4715         args->fc_attrs.max_rqst_sz = mxrqst_sz;
4716         args->fc_attrs.max_resp_sz = mxresp_sz;
4717         args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4718         args->fc_attrs.max_ops = NFS4_MAX_OPS;
4719         args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4720
4721         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4722                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4723                 __func__,
4724                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4725                 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4726                 args->fc_attrs.max_reqs);
4727
4728         /* Back channel attributes */
4729         args->bc_attrs.headerpadsz = 0;
4730         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4731         args->bc_attrs.max_resp_sz = PAGE_SIZE;
4732         args->bc_attrs.max_resp_sz_cached = 0;
4733         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4734         args->bc_attrs.max_reqs = 1;
4735
4736         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4737                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4738                 __func__,
4739                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4740                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4741                 args->bc_attrs.max_reqs);
4742 }
4743
4744 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4745 {
4746         if (rcvd <= sent)
4747                 return 0;
4748         printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4749                 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4750         return -EINVAL;
4751 }
4752
4753 #define _verify_fore_channel_attr(_name_) \
4754         _verify_channel_attr("fore", #_name_, \
4755                              args->fc_attrs._name_, \
4756                              session->fc_attrs._name_)
4757
4758 #define _verify_back_channel_attr(_name_) \
4759         _verify_channel_attr("back", #_name_, \
4760                              args->bc_attrs._name_, \
4761                              session->bc_attrs._name_)
4762
4763 /*
4764  * The server is not allowed to increase the fore channel header pad size,
4765  * maximum response size, or maximum number of operations.
4766  *
4767  * The back channel attributes are only negotiatied down: We send what the
4768  * (back channel) server insists upon.
4769  */
4770 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4771                                      struct nfs4_session *session)
4772 {
4773         int ret = 0;
4774
4775         ret |= _verify_fore_channel_attr(headerpadsz);
4776         ret |= _verify_fore_channel_attr(max_resp_sz);
4777         ret |= _verify_fore_channel_attr(max_ops);
4778
4779         ret |= _verify_back_channel_attr(headerpadsz);
4780         ret |= _verify_back_channel_attr(max_rqst_sz);
4781         ret |= _verify_back_channel_attr(max_resp_sz);
4782         ret |= _verify_back_channel_attr(max_resp_sz_cached);
4783         ret |= _verify_back_channel_attr(max_ops);
4784         ret |= _verify_back_channel_attr(max_reqs);
4785
4786         return ret;
4787 }
4788
4789 static int _nfs4_proc_create_session(struct nfs_client *clp)
4790 {
4791         struct nfs4_session *session = clp->cl_session;
4792         struct nfs41_create_session_args args = {
4793                 .client = clp,
4794                 .cb_program = NFS4_CALLBACK,
4795         };
4796         struct nfs41_create_session_res res = {
4797                 .client = clp,
4798         };
4799         struct rpc_message msg = {
4800                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4801                 .rpc_argp = &args,
4802                 .rpc_resp = &res,
4803         };
4804         int status;
4805
4806         nfs4_init_channel_attrs(&args);
4807         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4808
4809         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4810
4811         if (!status)
4812                 /* Verify the session's negotiated channel_attrs values */
4813                 status = nfs4_verify_channel_attrs(&args, session);
4814         if (!status) {
4815                 /* Increment the clientid slot sequence id */
4816                 clp->cl_seqid++;
4817         }
4818
4819         return status;
4820 }
4821
4822 /*
4823  * Issues a CREATE_SESSION operation to the server.
4824  * It is the responsibility of the caller to verify the session is
4825  * expired before calling this routine.
4826  */
4827 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4828 {
4829         int status;
4830         unsigned *ptr;
4831         struct nfs_fsinfo fsinfo;
4832         struct nfs4_session *session = clp->cl_session;
4833
4834         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4835
4836         status = _nfs4_proc_create_session(clp);
4837         if (status)
4838                 goto out;
4839
4840         /* Init or reset the fore channel */
4841         if (reset)
4842                 status = nfs4_reset_slot_tables(session);
4843         else
4844                 status = nfs4_init_slot_tables(session);
4845         dprintk("fore channel slot table initialization returned %d\n", status);
4846         if (status)
4847                 goto out;
4848
4849         ptr = (unsigned *)&session->sess_id.data[0];
4850         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4851                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4852
4853         if (reset)
4854                 /* Lease time is aleady set */
4855                 goto out;
4856
4857         /* Get the lease time */
4858         status = nfs4_proc_get_lease_time(clp, &fsinfo);
4859         if (status == 0) {
4860                 /* Update lease time and schedule renewal */
4861                 spin_lock(&clp->cl_lock);
4862                 clp->cl_lease_time = fsinfo.lease_time * HZ;
4863                 clp->cl_last_renewal = jiffies;
4864                 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4865                 spin_unlock(&clp->cl_lock);
4866
4867                 nfs4_schedule_state_renewal(clp);
4868         }
4869 out:
4870         dprintk("<-- %s\n", __func__);
4871         return status;
4872 }
4873
4874 /*
4875  * Issue the over-the-wire RPC DESTROY_SESSION.
4876  * The caller must serialize access to this routine.
4877  */
4878 int nfs4_proc_destroy_session(struct nfs4_session *session)
4879 {
4880         int status = 0;
4881         struct rpc_message msg;
4882
4883         dprintk("--> nfs4_proc_destroy_session\n");
4884
4885         /* session is still being setup */
4886         if (session->clp->cl_cons_state != NFS_CS_READY)
4887                 return status;
4888
4889         msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4890         msg.rpc_argp = session;
4891         msg.rpc_resp = NULL;
4892         msg.rpc_cred = NULL;
4893         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4894
4895         if (status)
4896                 printk(KERN_WARNING
4897                         "Got error %d from the server on DESTROY_SESSION. "
4898                         "Session has been destroyed regardless...\n", status);
4899
4900         dprintk("<-- nfs4_proc_destroy_session\n");
4901         return status;
4902 }
4903
4904 int nfs4_init_session(struct nfs_server *server)
4905 {
4906         struct nfs_client *clp = server->nfs_client;
4907         int ret;
4908
4909         if (!nfs4_has_session(clp))
4910                 return 0;
4911
4912         clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4913         clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4914         ret = nfs4_recover_expired_lease(server);
4915         if (!ret)
4916                 ret = nfs4_check_client_ready(clp);
4917         return ret;
4918 }
4919
4920 /*
4921  * Renew the cl_session lease.
4922  */
4923 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4924 {
4925         struct nfs4_sequence_args args;
4926         struct nfs4_sequence_res res;
4927
4928         struct rpc_message msg = {
4929                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4930                 .rpc_argp = &args,
4931                 .rpc_resp = &res,
4932                 .rpc_cred = cred,
4933         };
4934
4935         args.sa_cache_this = 0;
4936
4937         return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4938                                        &res, 0);
4939 }
4940
4941 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4942 {
4943         struct nfs_client *clp = (struct nfs_client *)data;
4944
4945         nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4946
4947         if (task->tk_status < 0) {
4948                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4949
4950                 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4951                                                                 == -EAGAIN) {
4952                         nfs4_restart_rpc(task, clp);
4953                         return;
4954                 }
4955         }
4956         nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4957         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4958
4959         kfree(task->tk_msg.rpc_argp);
4960         kfree(task->tk_msg.rpc_resp);
4961
4962         dprintk("<-- %s\n", __func__);
4963 }
4964
4965 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4966 {
4967         struct nfs_client *clp;
4968         struct nfs4_sequence_args *args;
4969         struct nfs4_sequence_res *res;
4970
4971         clp = (struct nfs_client *)data;
4972         args = task->tk_msg.rpc_argp;
4973         res = task->tk_msg.rpc_resp;
4974
4975         if (nfs4_setup_sequence(clp, args, res, 0, task))
4976                 return;
4977         rpc_call_start(task);
4978 }
4979
4980 static const struct rpc_call_ops nfs41_sequence_ops = {
4981         .rpc_call_done = nfs41_sequence_call_done,
4982         .rpc_call_prepare = nfs41_sequence_prepare,
4983 };
4984
4985 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4986                                      struct rpc_cred *cred)
4987 {
4988         struct nfs4_sequence_args *args;
4989         struct nfs4_sequence_res *res;
4990         struct rpc_message msg = {
4991                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4992                 .rpc_cred = cred,
4993         };
4994
4995         args = kzalloc(sizeof(*args), GFP_KERNEL);
4996         if (!args)
4997                 return -ENOMEM;
4998         res = kzalloc(sizeof(*res), GFP_KERNEL);
4999         if (!res) {
5000                 kfree(args);
5001                 return -ENOMEM;
5002         }
5003         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5004         msg.rpc_argp = args;
5005         msg.rpc_resp = res;
5006
5007         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5008                               &nfs41_sequence_ops, (void *)clp);
5009 }
5010
5011 #endif /* CONFIG_NFS_V4_1 */
5012
5013 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5014         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5015         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5016         .recover_open   = nfs4_open_reclaim,
5017         .recover_lock   = nfs4_lock_reclaim,
5018         .establish_clid = nfs4_init_clientid,
5019         .get_clid_cred  = nfs4_get_setclientid_cred,
5020 };
5021
5022 #if defined(CONFIG_NFS_V4_1)
5023 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5024         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5025         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5026         .recover_open   = nfs4_open_reclaim,
5027         .recover_lock   = nfs4_lock_reclaim,
5028         .establish_clid = nfs4_proc_exchange_id,
5029         .get_clid_cred  = nfs4_get_exchange_id_cred,
5030 };
5031 #endif /* CONFIG_NFS_V4_1 */
5032
5033 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5034         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5035         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5036         .recover_open   = nfs4_open_expired,
5037         .recover_lock   = nfs4_lock_expired,
5038         .establish_clid = nfs4_init_clientid,
5039         .get_clid_cred  = nfs4_get_setclientid_cred,
5040 };
5041
5042 #if defined(CONFIG_NFS_V4_1)
5043 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5044         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5045         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5046         .recover_open   = nfs4_open_expired,
5047         .recover_lock   = nfs4_lock_expired,
5048         .establish_clid = nfs4_proc_exchange_id,
5049         .get_clid_cred  = nfs4_get_exchange_id_cred,
5050 };
5051 #endif /* CONFIG_NFS_V4_1 */
5052
5053 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5054         .sched_state_renewal = nfs4_proc_async_renew,
5055         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5056         .renew_lease = nfs4_proc_renew,
5057 };
5058
5059 #if defined(CONFIG_NFS_V4_1)
5060 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5061         .sched_state_renewal = nfs41_proc_async_sequence,
5062         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5063         .renew_lease = nfs4_proc_sequence,
5064 };
5065 #endif
5066
5067 /*
5068  * Per minor version reboot and network partition recovery ops
5069  */
5070
5071 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5072         &nfs40_reboot_recovery_ops,
5073 #if defined(CONFIG_NFS_V4_1)
5074         &nfs41_reboot_recovery_ops,
5075 #endif
5076 };
5077
5078 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5079         &nfs40_nograce_recovery_ops,
5080 #if defined(CONFIG_NFS_V4_1)
5081         &nfs41_nograce_recovery_ops,
5082 #endif
5083 };
5084
5085 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5086         &nfs40_state_renewal_ops,
5087 #if defined(CONFIG_NFS_V4_1)
5088         &nfs41_state_renewal_ops,
5089 #endif
5090 };
5091
5092 static const struct inode_operations nfs4_file_inode_operations = {
5093         .permission     = nfs_permission,
5094         .getattr        = nfs_getattr,
5095         .setattr        = nfs_setattr,
5096         .getxattr       = nfs4_getxattr,
5097         .setxattr       = nfs4_setxattr,
5098         .listxattr      = nfs4_listxattr,
5099 };
5100
5101 const struct nfs_rpc_ops nfs_v4_clientops = {
5102         .version        = 4,                    /* protocol version */
5103         .dentry_ops     = &nfs4_dentry_operations,
5104         .dir_inode_ops  = &nfs4_dir_inode_operations,
5105         .file_inode_ops = &nfs4_file_inode_operations,
5106         .getroot        = nfs4_proc_get_root,
5107         .getattr        = nfs4_proc_getattr,
5108         .setattr        = nfs4_proc_setattr,
5109         .lookupfh       = nfs4_proc_lookupfh,
5110         .lookup         = nfs4_proc_lookup,
5111         .access         = nfs4_proc_access,
5112         .readlink       = nfs4_proc_readlink,
5113         .create         = nfs4_proc_create,
5114         .remove         = nfs4_proc_remove,
5115         .unlink_setup   = nfs4_proc_unlink_setup,
5116         .unlink_done    = nfs4_proc_unlink_done,
5117         .rename         = nfs4_proc_rename,
5118         .link           = nfs4_proc_link,
5119         .symlink        = nfs4_proc_symlink,
5120         .mkdir          = nfs4_proc_mkdir,
5121         .rmdir          = nfs4_proc_remove,
5122         .readdir        = nfs4_proc_readdir,
5123         .mknod          = nfs4_proc_mknod,
5124         .statfs         = nfs4_proc_statfs,
5125         .fsinfo         = nfs4_proc_fsinfo,
5126         .pathconf       = nfs4_proc_pathconf,
5127         .set_capabilities = nfs4_server_capabilities,
5128         .decode_dirent  = nfs4_decode_dirent,
5129         .read_setup     = nfs4_proc_read_setup,
5130         .read_done      = nfs4_read_done,
5131         .write_setup    = nfs4_proc_write_setup,
5132         .write_done     = nfs4_write_done,
5133         .commit_setup   = nfs4_proc_commit_setup,
5134         .commit_done    = nfs4_commit_done,
5135         .lock           = nfs4_proc_lock,
5136         .clear_acl_cache = nfs4_zap_acl_attr,
5137         .close_context  = nfs4_close_context,
5138 };
5139
5140 /*
5141  * Local variables:
5142  *  c-basic-offset: 8
5143  * End:
5144  */