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