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