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