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