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