]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - fs/nfs/nfs4proc.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[~shefty/rdma-dev.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 #include "pnfs.h"
59
60 #define NFSDBG_FACILITY         NFSDBG_PROC
61
62 #define NFS4_POLL_RETRY_MIN     (HZ/10)
63 #define NFS4_POLL_RETRY_MAX     (15*HZ)
64
65 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66
67 struct nfs4_opendata;
68 static int _nfs4_proc_open(struct nfs4_opendata *data);
69 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
70 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
71 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
72 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
74 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
75                             struct nfs_fattr *fattr, struct iattr *sattr,
76                             struct nfs4_state *state);
77
78 /* Prevent leaks of NFSv4 errors into userland */
79 static int nfs4_map_errors(int err)
80 {
81         if (err >= -1000)
82                 return err;
83         switch (err) {
84         case -NFS4ERR_RESOURCE:
85                 return -EREMOTEIO;
86         default:
87                 dprintk("%s could not handle NFSv4 error %d\n",
88                                 __func__, -err);
89                 break;
90         }
91         return -EIO;
92 }
93
94 /*
95  * This is our standard bitmap for GETATTR requests.
96  */
97 const u32 nfs4_fattr_bitmap[2] = {
98         FATTR4_WORD0_TYPE
99         | FATTR4_WORD0_CHANGE
100         | FATTR4_WORD0_SIZE
101         | FATTR4_WORD0_FSID
102         | FATTR4_WORD0_FILEID,
103         FATTR4_WORD1_MODE
104         | FATTR4_WORD1_NUMLINKS
105         | FATTR4_WORD1_OWNER
106         | FATTR4_WORD1_OWNER_GROUP
107         | FATTR4_WORD1_RAWDEV
108         | FATTR4_WORD1_SPACE_USED
109         | FATTR4_WORD1_TIME_ACCESS
110         | FATTR4_WORD1_TIME_METADATA
111         | FATTR4_WORD1_TIME_MODIFY
112 };
113
114 const u32 nfs4_statfs_bitmap[2] = {
115         FATTR4_WORD0_FILES_AVAIL
116         | FATTR4_WORD0_FILES_FREE
117         | FATTR4_WORD0_FILES_TOTAL,
118         FATTR4_WORD1_SPACE_AVAIL
119         | FATTR4_WORD1_SPACE_FREE
120         | FATTR4_WORD1_SPACE_TOTAL
121 };
122
123 const u32 nfs4_pathconf_bitmap[2] = {
124         FATTR4_WORD0_MAXLINK
125         | FATTR4_WORD0_MAXNAME,
126         0
127 };
128
129 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
130                         | FATTR4_WORD0_MAXREAD
131                         | FATTR4_WORD0_MAXWRITE
132                         | FATTR4_WORD0_LEASE_TIME,
133                         FATTR4_WORD1_TIME_DELTA
134                         | FATTR4_WORD1_FS_LAYOUT_TYPES
135 };
136
137 const u32 nfs4_fs_locations_bitmap[2] = {
138         FATTR4_WORD0_TYPE
139         | FATTR4_WORD0_CHANGE
140         | FATTR4_WORD0_SIZE
141         | FATTR4_WORD0_FSID
142         | FATTR4_WORD0_FILEID
143         | FATTR4_WORD0_FS_LOCATIONS,
144         FATTR4_WORD1_MODE
145         | FATTR4_WORD1_NUMLINKS
146         | FATTR4_WORD1_OWNER
147         | FATTR4_WORD1_OWNER_GROUP
148         | FATTR4_WORD1_RAWDEV
149         | FATTR4_WORD1_SPACE_USED
150         | FATTR4_WORD1_TIME_ACCESS
151         | FATTR4_WORD1_TIME_METADATA
152         | FATTR4_WORD1_TIME_MODIFY
153         | FATTR4_WORD1_MOUNTED_ON_FILEID
154 };
155
156 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
157                 struct nfs4_readdir_arg *readdir)
158 {
159         __be32 *start, *p;
160
161         BUG_ON(readdir->count < 80);
162         if (cookie > 2) {
163                 readdir->cookie = cookie;
164                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
165                 return;
166         }
167
168         readdir->cookie = 0;
169         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
170         if (cookie == 2)
171                 return;
172         
173         /*
174          * NFSv4 servers do not return entries for '.' and '..'
175          * Therefore, we fake these entries here.  We let '.'
176          * have cookie 0 and '..' have cookie 1.  Note that
177          * when talking to the server, we always send cookie 0
178          * instead of 1 or 2.
179          */
180         start = p = kmap_atomic(*readdir->pages, KM_USER0);
181         
182         if (cookie == 0) {
183                 *p++ = xdr_one;                                  /* next */
184                 *p++ = xdr_zero;                   /* cookie, first word */
185                 *p++ = xdr_one;                   /* cookie, second word */
186                 *p++ = xdr_one;                             /* entry len */
187                 memcpy(p, ".\0\0\0", 4);                        /* entry */
188                 p++;
189                 *p++ = xdr_one;                         /* bitmap length */
190                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
191                 *p++ = htonl(8);              /* attribute buffer length */
192                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
193         }
194         
195         *p++ = xdr_one;                                  /* next */
196         *p++ = xdr_zero;                   /* cookie, first word */
197         *p++ = xdr_two;                   /* cookie, second word */
198         *p++ = xdr_two;                             /* entry len */
199         memcpy(p, "..\0\0", 4);                         /* entry */
200         p++;
201         *p++ = xdr_one;                         /* bitmap length */
202         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
203         *p++ = htonl(8);              /* attribute buffer length */
204         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
205
206         readdir->pgbase = (char *)p - (char *)start;
207         readdir->count -= readdir->pgbase;
208         kunmap_atomic(start, KM_USER0);
209 }
210
211 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
212 {
213         int res;
214
215         might_sleep();
216
217         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
218                         nfs_wait_bit_killable, TASK_KILLABLE);
219         return res;
220 }
221
222 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
223 {
224         int res = 0;
225
226         might_sleep();
227
228         if (*timeout <= 0)
229                 *timeout = NFS4_POLL_RETRY_MIN;
230         if (*timeout > NFS4_POLL_RETRY_MAX)
231                 *timeout = NFS4_POLL_RETRY_MAX;
232         schedule_timeout_killable(*timeout);
233         if (fatal_signal_pending(current))
234                 res = -ERESTARTSYS;
235         *timeout <<= 1;
236         return res;
237 }
238
239 /* This is the error handling routine for processes that are allowed
240  * to sleep.
241  */
242 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
243 {
244         struct nfs_client *clp = server->nfs_client;
245         struct nfs4_state *state = exception->state;
246         int ret = errorcode;
247
248         exception->retry = 0;
249         switch(errorcode) {
250                 case 0:
251                         return 0;
252                 case -NFS4ERR_ADMIN_REVOKED:
253                 case -NFS4ERR_BAD_STATEID:
254                 case -NFS4ERR_OPENMODE:
255                         if (state == NULL)
256                                 break;
257                         nfs4_state_mark_reclaim_nograce(clp, state);
258                         goto do_state_recovery;
259                 case -NFS4ERR_STALE_STATEID:
260                 case -NFS4ERR_STALE_CLIENTID:
261                 case -NFS4ERR_EXPIRED:
262                         goto do_state_recovery;
263 #if defined(CONFIG_NFS_V4_1)
264                 case -NFS4ERR_BADSESSION:
265                 case -NFS4ERR_BADSLOT:
266                 case -NFS4ERR_BAD_HIGH_SLOT:
267                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268                 case -NFS4ERR_DEADSESSION:
269                 case -NFS4ERR_SEQ_FALSE_RETRY:
270                 case -NFS4ERR_SEQ_MISORDERED:
271                         dprintk("%s ERROR: %d Reset session\n", __func__,
272                                 errorcode);
273                         nfs4_schedule_state_recovery(clp);
274                         exception->retry = 1;
275                         break;
276 #endif /* defined(CONFIG_NFS_V4_1) */
277                 case -NFS4ERR_FILE_OPEN:
278                         if (exception->timeout > HZ) {
279                                 /* We have retried a decent amount, time to
280                                  * fail
281                                  */
282                                 ret = -EBUSY;
283                                 break;
284                         }
285                 case -NFS4ERR_GRACE:
286                 case -NFS4ERR_DELAY:
287                 case -EKEYEXPIRED:
288                         ret = nfs4_delay(server->client, &exception->timeout);
289                         if (ret != 0)
290                                 break;
291                 case -NFS4ERR_OLD_STATEID:
292                         exception->retry = 1;
293         }
294         /* We failed to handle the error */
295         return nfs4_map_errors(ret);
296 do_state_recovery:
297         nfs4_schedule_state_recovery(clp);
298         ret = nfs4_wait_clnt_recover(clp);
299         if (ret == 0)
300                 exception->retry = 1;
301         return ret;
302 }
303
304
305 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
306 {
307         spin_lock(&clp->cl_lock);
308         if (time_before(clp->cl_last_renewal,timestamp))
309                 clp->cl_last_renewal = timestamp;
310         spin_unlock(&clp->cl_lock);
311 }
312
313 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
314 {
315         do_renew_lease(server->nfs_client, timestamp);
316 }
317
318 #if defined(CONFIG_NFS_V4_1)
319
320 /*
321  * nfs4_free_slot - free a slot and efficiently update slot table.
322  *
323  * freeing a slot is trivially done by clearing its respective bit
324  * in the bitmap.
325  * If the freed slotid equals highest_used_slotid we want to update it
326  * so that the server would be able to size down the slot table if needed,
327  * otherwise we know that the highest_used_slotid is still in use.
328  * When updating highest_used_slotid there may be "holes" in the bitmap
329  * so we need to scan down from highest_used_slotid to 0 looking for the now
330  * highest slotid in use.
331  * If none found, highest_used_slotid is set to -1.
332  *
333  * Must be called while holding tbl->slot_tbl_lock
334  */
335 static void
336 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
337 {
338         int free_slotid = free_slot - tbl->slots;
339         int slotid = free_slotid;
340
341         BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
342         /* clear used bit in bitmap */
343         __clear_bit(slotid, tbl->used_slots);
344
345         /* update highest_used_slotid when it is freed */
346         if (slotid == tbl->highest_used_slotid) {
347                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
348                 if (slotid < tbl->max_slots)
349                         tbl->highest_used_slotid = slotid;
350                 else
351                         tbl->highest_used_slotid = -1;
352         }
353         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
354                 free_slotid, tbl->highest_used_slotid);
355 }
356
357 /*
358  * Signal state manager thread if session is drained
359  */
360 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
361 {
362         struct rpc_task *task;
363
364         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
365                 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
366                 if (task)
367                         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
368                 return;
369         }
370
371         if (ses->fc_slot_table.highest_used_slotid != -1)
372                 return;
373
374         dprintk("%s COMPLETE: Session Drained\n", __func__);
375         complete(&ses->complete);
376 }
377
378 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
379 {
380         struct nfs4_slot_table *tbl;
381
382         tbl = &res->sr_session->fc_slot_table;
383         if (!res->sr_slot) {
384                 /* just wake up the next guy waiting since
385                  * we may have not consumed a slot after all */
386                 dprintk("%s: No slot\n", __func__);
387                 return;
388         }
389
390         spin_lock(&tbl->slot_tbl_lock);
391         nfs4_free_slot(tbl, res->sr_slot);
392         nfs41_check_drain_session_complete(res->sr_session);
393         spin_unlock(&tbl->slot_tbl_lock);
394         res->sr_slot = NULL;
395 }
396
397 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
398 {
399         unsigned long timestamp;
400         struct nfs_client *clp;
401
402         /*
403          * sr_status remains 1 if an RPC level error occurred. The server
404          * may or may not have processed the sequence operation..
405          * Proceed as if the server received and processed the sequence
406          * operation.
407          */
408         if (res->sr_status == 1)
409                 res->sr_status = NFS_OK;
410
411         /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
412         if (!res->sr_slot)
413                 goto out;
414
415         /* Check the SEQUENCE operation status */
416         switch (res->sr_status) {
417         case 0:
418                 /* Update the slot's sequence and clientid lease timer */
419                 ++res->sr_slot->seq_nr;
420                 timestamp = res->sr_renewal_time;
421                 clp = res->sr_session->clp;
422                 do_renew_lease(clp, timestamp);
423                 /* Check sequence flags */
424                 if (atomic_read(&clp->cl_count) > 1)
425                         nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
426                 break;
427         case -NFS4ERR_DELAY:
428                 /* The server detected a resend of the RPC call and
429                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
430                  * of RFC5661.
431                  */
432                 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
433                         __func__,
434                         res->sr_slot - res->sr_session->fc_slot_table.slots,
435                         res->sr_slot->seq_nr);
436                 goto out_retry;
437         default:
438                 /* Just update the slot sequence no. */
439                 ++res->sr_slot->seq_nr;
440         }
441 out:
442         /* The session may be reset by one of the error handlers. */
443         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
444         nfs41_sequence_free_slot(res);
445         return 1;
446 out_retry:
447         if (!rpc_restart_call(task))
448                 goto out;
449         rpc_delay(task, NFS4_POLL_RETRY_MAX);
450         return 0;
451 }
452
453 static int nfs4_sequence_done(struct rpc_task *task,
454                                struct nfs4_sequence_res *res)
455 {
456         if (res->sr_session == NULL)
457                 return 1;
458         return nfs41_sequence_done(task, res);
459 }
460
461 /*
462  * nfs4_find_slot - efficiently look for a free slot
463  *
464  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
465  * If found, we mark the slot as used, update the highest_used_slotid,
466  * and respectively set up the sequence operation args.
467  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
468  *
469  * Note: must be called with under the slot_tbl_lock.
470  */
471 static u8
472 nfs4_find_slot(struct nfs4_slot_table *tbl)
473 {
474         int slotid;
475         u8 ret_id = NFS4_MAX_SLOT_TABLE;
476         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
477
478         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
479                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
480                 tbl->max_slots);
481         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
482         if (slotid >= tbl->max_slots)
483                 goto out;
484         __set_bit(slotid, tbl->used_slots);
485         if (slotid > tbl->highest_used_slotid)
486                 tbl->highest_used_slotid = slotid;
487         ret_id = slotid;
488 out:
489         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
490                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
491         return ret_id;
492 }
493
494 static int nfs41_setup_sequence(struct nfs4_session *session,
495                                 struct nfs4_sequence_args *args,
496                                 struct nfs4_sequence_res *res,
497                                 int cache_reply,
498                                 struct rpc_task *task)
499 {
500         struct nfs4_slot *slot;
501         struct nfs4_slot_table *tbl;
502         u8 slotid;
503
504         dprintk("--> %s\n", __func__);
505         /* slot already allocated? */
506         if (res->sr_slot != NULL)
507                 return 0;
508
509         tbl = &session->fc_slot_table;
510
511         spin_lock(&tbl->slot_tbl_lock);
512         if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
513             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
514                 /*
515                  * The state manager will wait until the slot table is empty.
516                  * Schedule the reset thread
517                  */
518                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
519                 spin_unlock(&tbl->slot_tbl_lock);
520                 dprintk("%s Schedule Session Reset\n", __func__);
521                 return -EAGAIN;
522         }
523
524         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
525             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
526                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
527                 spin_unlock(&tbl->slot_tbl_lock);
528                 dprintk("%s enforce FIFO order\n", __func__);
529                 return -EAGAIN;
530         }
531
532         slotid = nfs4_find_slot(tbl);
533         if (slotid == NFS4_MAX_SLOT_TABLE) {
534                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535                 spin_unlock(&tbl->slot_tbl_lock);
536                 dprintk("<-- %s: no free slots\n", __func__);
537                 return -EAGAIN;
538         }
539         spin_unlock(&tbl->slot_tbl_lock);
540
541         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
542         slot = tbl->slots + slotid;
543         args->sa_session = session;
544         args->sa_slotid = slotid;
545         args->sa_cache_this = cache_reply;
546
547         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
548
549         res->sr_session = session;
550         res->sr_slot = slot;
551         res->sr_renewal_time = jiffies;
552         res->sr_status_flags = 0;
553         /*
554          * sr_status is only set in decode_sequence, and so will remain
555          * set to 1 if an rpc level failure occurs.
556          */
557         res->sr_status = 1;
558         return 0;
559 }
560
561 int nfs4_setup_sequence(const struct nfs_server *server,
562                         struct nfs4_sequence_args *args,
563                         struct nfs4_sequence_res *res,
564                         int cache_reply,
565                         struct rpc_task *task)
566 {
567         struct nfs4_session *session = nfs4_get_session(server);
568         int ret = 0;
569
570         if (session == NULL) {
571                 args->sa_session = NULL;
572                 res->sr_session = NULL;
573                 goto out;
574         }
575
576         dprintk("--> %s clp %p session %p sr_slot %td\n",
577                 __func__, session->clp, session, res->sr_slot ?
578                         res->sr_slot - session->fc_slot_table.slots : -1);
579
580         ret = nfs41_setup_sequence(session, args, res, cache_reply,
581                                    task);
582 out:
583         dprintk("<-- %s status=%d\n", __func__, ret);
584         return ret;
585 }
586
587 struct nfs41_call_sync_data {
588         const struct nfs_server *seq_server;
589         struct nfs4_sequence_args *seq_args;
590         struct nfs4_sequence_res *seq_res;
591         int cache_reply;
592 };
593
594 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
595 {
596         struct nfs41_call_sync_data *data = calldata;
597
598         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
599
600         if (nfs4_setup_sequence(data->seq_server, data->seq_args,
601                                 data->seq_res, data->cache_reply, task))
602                 return;
603         rpc_call_start(task);
604 }
605
606 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
607 {
608         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
609         nfs41_call_sync_prepare(task, calldata);
610 }
611
612 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
613 {
614         struct nfs41_call_sync_data *data = calldata;
615
616         nfs41_sequence_done(task, data->seq_res);
617 }
618
619 struct rpc_call_ops nfs41_call_sync_ops = {
620         .rpc_call_prepare = nfs41_call_sync_prepare,
621         .rpc_call_done = nfs41_call_sync_done,
622 };
623
624 struct rpc_call_ops nfs41_call_priv_sync_ops = {
625         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
626         .rpc_call_done = nfs41_call_sync_done,
627 };
628
629 static int nfs4_call_sync_sequence(struct nfs_server *server,
630                                    struct rpc_message *msg,
631                                    struct nfs4_sequence_args *args,
632                                    struct nfs4_sequence_res *res,
633                                    int cache_reply,
634                                    int privileged)
635 {
636         int ret;
637         struct rpc_task *task;
638         struct nfs41_call_sync_data data = {
639                 .seq_server = server,
640                 .seq_args = args,
641                 .seq_res = res,
642                 .cache_reply = cache_reply,
643         };
644         struct rpc_task_setup task_setup = {
645                 .rpc_client = server->client,
646                 .rpc_message = msg,
647                 .callback_ops = &nfs41_call_sync_ops,
648                 .callback_data = &data
649         };
650
651         res->sr_slot = NULL;
652         if (privileged)
653                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
654         task = rpc_run_task(&task_setup);
655         if (IS_ERR(task))
656                 ret = PTR_ERR(task);
657         else {
658                 ret = task->tk_status;
659                 rpc_put_task(task);
660         }
661         return ret;
662 }
663
664 int _nfs4_call_sync_session(struct nfs_server *server,
665                             struct rpc_message *msg,
666                             struct nfs4_sequence_args *args,
667                             struct nfs4_sequence_res *res,
668                             int cache_reply)
669 {
670         return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
671 }
672
673 #else
674 static int nfs4_sequence_done(struct rpc_task *task,
675                                struct nfs4_sequence_res *res)
676 {
677         return 1;
678 }
679 #endif /* CONFIG_NFS_V4_1 */
680
681 int _nfs4_call_sync(struct nfs_server *server,
682                     struct rpc_message *msg,
683                     struct nfs4_sequence_args *args,
684                     struct nfs4_sequence_res *res,
685                     int cache_reply)
686 {
687         args->sa_session = res->sr_session = NULL;
688         return rpc_call_sync(server->client, msg, 0);
689 }
690
691 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
692         (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
693                         &(res)->seq_res, (cache_reply))
694
695 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
696 {
697         struct nfs_inode *nfsi = NFS_I(dir);
698
699         spin_lock(&dir->i_lock);
700         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
701         if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
702                 nfs_force_lookup_revalidate(dir);
703         nfsi->change_attr = cinfo->after;
704         spin_unlock(&dir->i_lock);
705 }
706
707 struct nfs4_opendata {
708         struct kref kref;
709         struct nfs_openargs o_arg;
710         struct nfs_openres o_res;
711         struct nfs_open_confirmargs c_arg;
712         struct nfs_open_confirmres c_res;
713         struct nfs_fattr f_attr;
714         struct nfs_fattr dir_attr;
715         struct path path;
716         struct dentry *dir;
717         struct nfs4_state_owner *owner;
718         struct nfs4_state *state;
719         struct iattr attrs;
720         unsigned long timestamp;
721         unsigned int rpc_done : 1;
722         int rpc_status;
723         int cancelled;
724 };
725
726
727 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
728 {
729         p->o_res.f_attr = &p->f_attr;
730         p->o_res.dir_attr = &p->dir_attr;
731         p->o_res.seqid = p->o_arg.seqid;
732         p->c_res.seqid = p->c_arg.seqid;
733         p->o_res.server = p->o_arg.server;
734         nfs_fattr_init(&p->f_attr);
735         nfs_fattr_init(&p->dir_attr);
736 }
737
738 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
739                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
740                 const struct iattr *attrs,
741                 gfp_t gfp_mask)
742 {
743         struct dentry *parent = dget_parent(path->dentry);
744         struct inode *dir = parent->d_inode;
745         struct nfs_server *server = NFS_SERVER(dir);
746         struct nfs4_opendata *p;
747
748         p = kzalloc(sizeof(*p), gfp_mask);
749         if (p == NULL)
750                 goto err;
751         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
752         if (p->o_arg.seqid == NULL)
753                 goto err_free;
754         path_get(path);
755         p->path = *path;
756         p->dir = parent;
757         p->owner = sp;
758         atomic_inc(&sp->so_count);
759         p->o_arg.fh = NFS_FH(dir);
760         p->o_arg.open_flags = flags;
761         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
762         p->o_arg.clientid = server->nfs_client->cl_clientid;
763         p->o_arg.id = sp->so_owner_id.id;
764         p->o_arg.name = &p->path.dentry->d_name;
765         p->o_arg.server = server;
766         p->o_arg.bitmask = server->attr_bitmask;
767         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
768         if (flags & O_CREAT) {
769                 u32 *s;
770
771                 p->o_arg.u.attrs = &p->attrs;
772                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
773                 s = (u32 *) p->o_arg.u.verifier.data;
774                 s[0] = jiffies;
775                 s[1] = current->pid;
776         }
777         p->c_arg.fh = &p->o_res.fh;
778         p->c_arg.stateid = &p->o_res.stateid;
779         p->c_arg.seqid = p->o_arg.seqid;
780         nfs4_init_opendata_res(p);
781         kref_init(&p->kref);
782         return p;
783 err_free:
784         kfree(p);
785 err:
786         dput(parent);
787         return NULL;
788 }
789
790 static void nfs4_opendata_free(struct kref *kref)
791 {
792         struct nfs4_opendata *p = container_of(kref,
793                         struct nfs4_opendata, kref);
794
795         nfs_free_seqid(p->o_arg.seqid);
796         if (p->state != NULL)
797                 nfs4_put_open_state(p->state);
798         nfs4_put_state_owner(p->owner);
799         dput(p->dir);
800         path_put(&p->path);
801         kfree(p);
802 }
803
804 static void nfs4_opendata_put(struct nfs4_opendata *p)
805 {
806         if (p != NULL)
807                 kref_put(&p->kref, nfs4_opendata_free);
808 }
809
810 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
811 {
812         int ret;
813
814         ret = rpc_wait_for_completion_task(task);
815         return ret;
816 }
817
818 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
819 {
820         int ret = 0;
821
822         if (open_mode & O_EXCL)
823                 goto out;
824         switch (mode & (FMODE_READ|FMODE_WRITE)) {
825                 case FMODE_READ:
826                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
827                                 && state->n_rdonly != 0;
828                         break;
829                 case FMODE_WRITE:
830                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
831                                 && state->n_wronly != 0;
832                         break;
833                 case FMODE_READ|FMODE_WRITE:
834                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
835                                 && state->n_rdwr != 0;
836         }
837 out:
838         return ret;
839 }
840
841 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
842 {
843         if ((delegation->type & fmode) != fmode)
844                 return 0;
845         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
846                 return 0;
847         nfs_mark_delegation_referenced(delegation);
848         return 1;
849 }
850
851 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
852 {
853         switch (fmode) {
854                 case FMODE_WRITE:
855                         state->n_wronly++;
856                         break;
857                 case FMODE_READ:
858                         state->n_rdonly++;
859                         break;
860                 case FMODE_READ|FMODE_WRITE:
861                         state->n_rdwr++;
862         }
863         nfs4_state_set_mode_locked(state, state->state | fmode);
864 }
865
866 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
867 {
868         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
869                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
870         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
871         switch (fmode) {
872                 case FMODE_READ:
873                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
874                         break;
875                 case FMODE_WRITE:
876                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
877                         break;
878                 case FMODE_READ|FMODE_WRITE:
879                         set_bit(NFS_O_RDWR_STATE, &state->flags);
880         }
881 }
882
883 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
884 {
885         write_seqlock(&state->seqlock);
886         nfs_set_open_stateid_locked(state, stateid, fmode);
887         write_sequnlock(&state->seqlock);
888 }
889
890 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
891 {
892         /*
893          * Protect the call to nfs4_state_set_mode_locked and
894          * serialise the stateid update
895          */
896         write_seqlock(&state->seqlock);
897         if (deleg_stateid != NULL) {
898                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
899                 set_bit(NFS_DELEGATED_STATE, &state->flags);
900         }
901         if (open_stateid != NULL)
902                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
903         write_sequnlock(&state->seqlock);
904         spin_lock(&state->owner->so_lock);
905         update_open_stateflags(state, fmode);
906         spin_unlock(&state->owner->so_lock);
907 }
908
909 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
910 {
911         struct nfs_inode *nfsi = NFS_I(state->inode);
912         struct nfs_delegation *deleg_cur;
913         int ret = 0;
914
915         fmode &= (FMODE_READ|FMODE_WRITE);
916
917         rcu_read_lock();
918         deleg_cur = rcu_dereference(nfsi->delegation);
919         if (deleg_cur == NULL)
920                 goto no_delegation;
921
922         spin_lock(&deleg_cur->lock);
923         if (nfsi->delegation != deleg_cur ||
924             (deleg_cur->type & fmode) != fmode)
925                 goto no_delegation_unlock;
926
927         if (delegation == NULL)
928                 delegation = &deleg_cur->stateid;
929         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
930                 goto no_delegation_unlock;
931
932         nfs_mark_delegation_referenced(deleg_cur);
933         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
934         ret = 1;
935 no_delegation_unlock:
936         spin_unlock(&deleg_cur->lock);
937 no_delegation:
938         rcu_read_unlock();
939
940         if (!ret && open_stateid != NULL) {
941                 __update_open_stateid(state, open_stateid, NULL, fmode);
942                 ret = 1;
943         }
944
945         return ret;
946 }
947
948
949 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
950 {
951         struct nfs_delegation *delegation;
952
953         rcu_read_lock();
954         delegation = rcu_dereference(NFS_I(inode)->delegation);
955         if (delegation == NULL || (delegation->type & fmode) == fmode) {
956                 rcu_read_unlock();
957                 return;
958         }
959         rcu_read_unlock();
960         nfs_inode_return_delegation(inode);
961 }
962
963 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
964 {
965         struct nfs4_state *state = opendata->state;
966         struct nfs_inode *nfsi = NFS_I(state->inode);
967         struct nfs_delegation *delegation;
968         int open_mode = opendata->o_arg.open_flags & O_EXCL;
969         fmode_t fmode = opendata->o_arg.fmode;
970         nfs4_stateid stateid;
971         int ret = -EAGAIN;
972
973         for (;;) {
974                 if (can_open_cached(state, fmode, open_mode)) {
975                         spin_lock(&state->owner->so_lock);
976                         if (can_open_cached(state, fmode, open_mode)) {
977                                 update_open_stateflags(state, fmode);
978                                 spin_unlock(&state->owner->so_lock);
979                                 goto out_return_state;
980                         }
981                         spin_unlock(&state->owner->so_lock);
982                 }
983                 rcu_read_lock();
984                 delegation = rcu_dereference(nfsi->delegation);
985                 if (delegation == NULL ||
986                     !can_open_delegated(delegation, fmode)) {
987                         rcu_read_unlock();
988                         break;
989                 }
990                 /* Save the delegation */
991                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
992                 rcu_read_unlock();
993                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
994                 if (ret != 0)
995                         goto out;
996                 ret = -EAGAIN;
997
998                 /* Try to update the stateid using the delegation */
999                 if (update_open_stateid(state, NULL, &stateid, fmode))
1000                         goto out_return_state;
1001         }
1002 out:
1003         return ERR_PTR(ret);
1004 out_return_state:
1005         atomic_inc(&state->count);
1006         return state;
1007 }
1008
1009 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1010 {
1011         struct inode *inode;
1012         struct nfs4_state *state = NULL;
1013         struct nfs_delegation *delegation;
1014         int ret;
1015
1016         if (!data->rpc_done) {
1017                 state = nfs4_try_open_cached(data);
1018                 goto out;
1019         }
1020
1021         ret = -EAGAIN;
1022         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1023                 goto err;
1024         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1025         ret = PTR_ERR(inode);
1026         if (IS_ERR(inode))
1027                 goto err;
1028         ret = -ENOMEM;
1029         state = nfs4_get_open_state(inode, data->owner);
1030         if (state == NULL)
1031                 goto err_put_inode;
1032         if (data->o_res.delegation_type != 0) {
1033                 int delegation_flags = 0;
1034
1035                 rcu_read_lock();
1036                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1037                 if (delegation)
1038                         delegation_flags = delegation->flags;
1039                 rcu_read_unlock();
1040                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1041                         nfs_inode_set_delegation(state->inode,
1042                                         data->owner->so_cred,
1043                                         &data->o_res);
1044                 else
1045                         nfs_inode_reclaim_delegation(state->inode,
1046                                         data->owner->so_cred,
1047                                         &data->o_res);
1048         }
1049
1050         update_open_stateid(state, &data->o_res.stateid, NULL,
1051                         data->o_arg.fmode);
1052         iput(inode);
1053 out:
1054         return state;
1055 err_put_inode:
1056         iput(inode);
1057 err:
1058         return ERR_PTR(ret);
1059 }
1060
1061 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1062 {
1063         struct nfs_inode *nfsi = NFS_I(state->inode);
1064         struct nfs_open_context *ctx;
1065
1066         spin_lock(&state->inode->i_lock);
1067         list_for_each_entry(ctx, &nfsi->open_files, list) {
1068                 if (ctx->state != state)
1069                         continue;
1070                 get_nfs_open_context(ctx);
1071                 spin_unlock(&state->inode->i_lock);
1072                 return ctx;
1073         }
1074         spin_unlock(&state->inode->i_lock);
1075         return ERR_PTR(-ENOENT);
1076 }
1077
1078 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1079 {
1080         struct nfs4_opendata *opendata;
1081
1082         opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1083         if (opendata == NULL)
1084                 return ERR_PTR(-ENOMEM);
1085         opendata->state = state;
1086         atomic_inc(&state->count);
1087         return opendata;
1088 }
1089
1090 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1091 {
1092         struct nfs4_state *newstate;
1093         int ret;
1094
1095         opendata->o_arg.open_flags = 0;
1096         opendata->o_arg.fmode = fmode;
1097         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1098         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1099         nfs4_init_opendata_res(opendata);
1100         ret = _nfs4_recover_proc_open(opendata);
1101         if (ret != 0)
1102                 return ret; 
1103         newstate = nfs4_opendata_to_nfs4_state(opendata);
1104         if (IS_ERR(newstate))
1105                 return PTR_ERR(newstate);
1106         nfs4_close_state(&opendata->path, newstate, fmode);
1107         *res = newstate;
1108         return 0;
1109 }
1110
1111 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1112 {
1113         struct nfs4_state *newstate;
1114         int ret;
1115
1116         /* memory barrier prior to reading state->n_* */
1117         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1118         smp_rmb();
1119         if (state->n_rdwr != 0) {
1120                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1121                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1122                 if (ret != 0)
1123                         return ret;
1124                 if (newstate != state)
1125                         return -ESTALE;
1126         }
1127         if (state->n_wronly != 0) {
1128                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1129                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1130                 if (ret != 0)
1131                         return ret;
1132                 if (newstate != state)
1133                         return -ESTALE;
1134         }
1135         if (state->n_rdonly != 0) {
1136                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1137                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1138                 if (ret != 0)
1139                         return ret;
1140                 if (newstate != state)
1141                         return -ESTALE;
1142         }
1143         /*
1144          * We may have performed cached opens for all three recoveries.
1145          * Check if we need to update the current stateid.
1146          */
1147         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1148             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1149                 write_seqlock(&state->seqlock);
1150                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1151                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1152                 write_sequnlock(&state->seqlock);
1153         }
1154         return 0;
1155 }
1156
1157 /*
1158  * OPEN_RECLAIM:
1159  *      reclaim state on the server after a reboot.
1160  */
1161 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1162 {
1163         struct nfs_delegation *delegation;
1164         struct nfs4_opendata *opendata;
1165         fmode_t delegation_type = 0;
1166         int status;
1167
1168         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1169         if (IS_ERR(opendata))
1170                 return PTR_ERR(opendata);
1171         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1172         opendata->o_arg.fh = NFS_FH(state->inode);
1173         rcu_read_lock();
1174         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1175         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1176                 delegation_type = delegation->type;
1177         rcu_read_unlock();
1178         opendata->o_arg.u.delegation_type = delegation_type;
1179         status = nfs4_open_recover(opendata, state);
1180         nfs4_opendata_put(opendata);
1181         return status;
1182 }
1183
1184 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1185 {
1186         struct nfs_server *server = NFS_SERVER(state->inode);
1187         struct nfs4_exception exception = { };
1188         int err;
1189         do {
1190                 err = _nfs4_do_open_reclaim(ctx, state);
1191                 if (err != -NFS4ERR_DELAY)
1192                         break;
1193                 nfs4_handle_exception(server, err, &exception);
1194         } while (exception.retry);
1195         return err;
1196 }
1197
1198 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1199 {
1200         struct nfs_open_context *ctx;
1201         int ret;
1202
1203         ctx = nfs4_state_find_open_context(state);
1204         if (IS_ERR(ctx))
1205                 return PTR_ERR(ctx);
1206         ret = nfs4_do_open_reclaim(ctx, state);
1207         put_nfs_open_context(ctx);
1208         return ret;
1209 }
1210
1211 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1212 {
1213         struct nfs4_opendata *opendata;
1214         int ret;
1215
1216         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1217         if (IS_ERR(opendata))
1218                 return PTR_ERR(opendata);
1219         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1220         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1221                         sizeof(opendata->o_arg.u.delegation.data));
1222         ret = nfs4_open_recover(opendata, state);
1223         nfs4_opendata_put(opendata);
1224         return ret;
1225 }
1226
1227 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1228 {
1229         struct nfs4_exception exception = { };
1230         struct nfs_server *server = NFS_SERVER(state->inode);
1231         int err;
1232         do {
1233                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1234                 switch (err) {
1235                         case 0:
1236                         case -ENOENT:
1237                         case -ESTALE:
1238                                 goto out;
1239                         case -NFS4ERR_BADSESSION:
1240                         case -NFS4ERR_BADSLOT:
1241                         case -NFS4ERR_BAD_HIGH_SLOT:
1242                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1243                         case -NFS4ERR_DEADSESSION:
1244                                 nfs4_schedule_state_recovery(
1245                                         server->nfs_client);
1246                                 goto out;
1247                         case -NFS4ERR_STALE_CLIENTID:
1248                         case -NFS4ERR_STALE_STATEID:
1249                         case -NFS4ERR_EXPIRED:
1250                                 /* Don't recall a delegation if it was lost */
1251                                 nfs4_schedule_state_recovery(server->nfs_client);
1252                                 goto out;
1253                         case -ERESTARTSYS:
1254                                 /*
1255                                  * The show must go on: exit, but mark the
1256                                  * stateid as needing recovery.
1257                                  */
1258                         case -NFS4ERR_ADMIN_REVOKED:
1259                         case -NFS4ERR_BAD_STATEID:
1260                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1261                         case -EKEYEXPIRED:
1262                                 /*
1263                                  * User RPCSEC_GSS context has expired.
1264                                  * We cannot recover this stateid now, so
1265                                  * skip it and allow recovery thread to
1266                                  * proceed.
1267                                  */
1268                         case -ENOMEM:
1269                                 err = 0;
1270                                 goto out;
1271                 }
1272                 err = nfs4_handle_exception(server, err, &exception);
1273         } while (exception.retry);
1274 out:
1275         return err;
1276 }
1277
1278 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1279 {
1280         struct nfs4_opendata *data = calldata;
1281
1282         data->rpc_status = task->tk_status;
1283         if (data->rpc_status == 0) {
1284                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1285                                 sizeof(data->o_res.stateid.data));
1286                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1287                 renew_lease(data->o_res.server, data->timestamp);
1288                 data->rpc_done = 1;
1289         }
1290 }
1291
1292 static void nfs4_open_confirm_release(void *calldata)
1293 {
1294         struct nfs4_opendata *data = calldata;
1295         struct nfs4_state *state = NULL;
1296
1297         /* If this request hasn't been cancelled, do nothing */
1298         if (data->cancelled == 0)
1299                 goto out_free;
1300         /* In case of error, no cleanup! */
1301         if (!data->rpc_done)
1302                 goto out_free;
1303         state = nfs4_opendata_to_nfs4_state(data);
1304         if (!IS_ERR(state))
1305                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1306 out_free:
1307         nfs4_opendata_put(data);
1308 }
1309
1310 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1311         .rpc_call_done = nfs4_open_confirm_done,
1312         .rpc_release = nfs4_open_confirm_release,
1313 };
1314
1315 /*
1316  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1317  */
1318 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1319 {
1320         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1321         struct rpc_task *task;
1322         struct  rpc_message msg = {
1323                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1324                 .rpc_argp = &data->c_arg,
1325                 .rpc_resp = &data->c_res,
1326                 .rpc_cred = data->owner->so_cred,
1327         };
1328         struct rpc_task_setup task_setup_data = {
1329                 .rpc_client = server->client,
1330                 .rpc_message = &msg,
1331                 .callback_ops = &nfs4_open_confirm_ops,
1332                 .callback_data = data,
1333                 .workqueue = nfsiod_workqueue,
1334                 .flags = RPC_TASK_ASYNC,
1335         };
1336         int status;
1337
1338         kref_get(&data->kref);
1339         data->rpc_done = 0;
1340         data->rpc_status = 0;
1341         data->timestamp = jiffies;
1342         task = rpc_run_task(&task_setup_data);
1343         if (IS_ERR(task))
1344                 return PTR_ERR(task);
1345         status = nfs4_wait_for_completion_rpc_task(task);
1346         if (status != 0) {
1347                 data->cancelled = 1;
1348                 smp_wmb();
1349         } else
1350                 status = data->rpc_status;
1351         rpc_put_task(task);
1352         return status;
1353 }
1354
1355 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1356 {
1357         struct nfs4_opendata *data = calldata;
1358         struct nfs4_state_owner *sp = data->owner;
1359
1360         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1361                 return;
1362         /*
1363          * Check if we still need to send an OPEN call, or if we can use
1364          * a delegation instead.
1365          */
1366         if (data->state != NULL) {
1367                 struct nfs_delegation *delegation;
1368
1369                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1370                         goto out_no_action;
1371                 rcu_read_lock();
1372                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1373                 if (delegation != NULL &&
1374                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1375                         rcu_read_unlock();
1376                         goto out_no_action;
1377                 }
1378                 rcu_read_unlock();
1379         }
1380         /* Update sequence id. */
1381         data->o_arg.id = sp->so_owner_id.id;
1382         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1383         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1384                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1385                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1386         }
1387         data->timestamp = jiffies;
1388         if (nfs4_setup_sequence(data->o_arg.server,
1389                                 &data->o_arg.seq_args,
1390                                 &data->o_res.seq_res, 1, task))
1391                 return;
1392         rpc_call_start(task);
1393         return;
1394 out_no_action:
1395         task->tk_action = NULL;
1396
1397 }
1398
1399 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1400 {
1401         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1402         nfs4_open_prepare(task, calldata);
1403 }
1404
1405 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1406 {
1407         struct nfs4_opendata *data = calldata;
1408
1409         data->rpc_status = task->tk_status;
1410
1411         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1412                 return;
1413
1414         if (task->tk_status == 0) {
1415                 switch (data->o_res.f_attr->mode & S_IFMT) {
1416                         case S_IFREG:
1417                                 break;
1418                         case S_IFLNK:
1419                                 data->rpc_status = -ELOOP;
1420                                 break;
1421                         case S_IFDIR:
1422                                 data->rpc_status = -EISDIR;
1423                                 break;
1424                         default:
1425                                 data->rpc_status = -ENOTDIR;
1426                 }
1427                 renew_lease(data->o_res.server, data->timestamp);
1428                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1429                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1430         }
1431         data->rpc_done = 1;
1432 }
1433
1434 static void nfs4_open_release(void *calldata)
1435 {
1436         struct nfs4_opendata *data = calldata;
1437         struct nfs4_state *state = NULL;
1438
1439         /* If this request hasn't been cancelled, do nothing */
1440         if (data->cancelled == 0)
1441                 goto out_free;
1442         /* In case of error, no cleanup! */
1443         if (data->rpc_status != 0 || !data->rpc_done)
1444                 goto out_free;
1445         /* In case we need an open_confirm, no cleanup! */
1446         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1447                 goto out_free;
1448         state = nfs4_opendata_to_nfs4_state(data);
1449         if (!IS_ERR(state))
1450                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1451 out_free:
1452         nfs4_opendata_put(data);
1453 }
1454
1455 static const struct rpc_call_ops nfs4_open_ops = {
1456         .rpc_call_prepare = nfs4_open_prepare,
1457         .rpc_call_done = nfs4_open_done,
1458         .rpc_release = nfs4_open_release,
1459 };
1460
1461 static const struct rpc_call_ops nfs4_recover_open_ops = {
1462         .rpc_call_prepare = nfs4_recover_open_prepare,
1463         .rpc_call_done = nfs4_open_done,
1464         .rpc_release = nfs4_open_release,
1465 };
1466
1467 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1468 {
1469         struct inode *dir = data->dir->d_inode;
1470         struct nfs_server *server = NFS_SERVER(dir);
1471         struct nfs_openargs *o_arg = &data->o_arg;
1472         struct nfs_openres *o_res = &data->o_res;
1473         struct rpc_task *task;
1474         struct rpc_message msg = {
1475                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1476                 .rpc_argp = o_arg,
1477                 .rpc_resp = o_res,
1478                 .rpc_cred = data->owner->so_cred,
1479         };
1480         struct rpc_task_setup task_setup_data = {
1481                 .rpc_client = server->client,
1482                 .rpc_message = &msg,
1483                 .callback_ops = &nfs4_open_ops,
1484                 .callback_data = data,
1485                 .workqueue = nfsiod_workqueue,
1486                 .flags = RPC_TASK_ASYNC,
1487         };
1488         int status;
1489
1490         kref_get(&data->kref);
1491         data->rpc_done = 0;
1492         data->rpc_status = 0;
1493         data->cancelled = 0;
1494         if (isrecover)
1495                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1496         task = rpc_run_task(&task_setup_data);
1497         if (IS_ERR(task))
1498                 return PTR_ERR(task);
1499         status = nfs4_wait_for_completion_rpc_task(task);
1500         if (status != 0) {
1501                 data->cancelled = 1;
1502                 smp_wmb();
1503         } else
1504                 status = data->rpc_status;
1505         rpc_put_task(task);
1506
1507         return status;
1508 }
1509
1510 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1511 {
1512         struct inode *dir = data->dir->d_inode;
1513         struct nfs_openres *o_res = &data->o_res;
1514         int status;
1515
1516         status = nfs4_run_open_task(data, 1);
1517         if (status != 0 || !data->rpc_done)
1518                 return status;
1519
1520         nfs_refresh_inode(dir, o_res->dir_attr);
1521
1522         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1523                 status = _nfs4_proc_open_confirm(data);
1524                 if (status != 0)
1525                         return status;
1526         }
1527
1528         return status;
1529 }
1530
1531 /*
1532  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1533  */
1534 static int _nfs4_proc_open(struct nfs4_opendata *data)
1535 {
1536         struct inode *dir = data->dir->d_inode;
1537         struct nfs_server *server = NFS_SERVER(dir);
1538         struct nfs_openargs *o_arg = &data->o_arg;
1539         struct nfs_openres *o_res = &data->o_res;
1540         int status;
1541
1542         status = nfs4_run_open_task(data, 0);
1543         if (status != 0 || !data->rpc_done)
1544                 return status;
1545
1546         if (o_arg->open_flags & O_CREAT) {
1547                 update_changeattr(dir, &o_res->cinfo);
1548                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1549         } else
1550                 nfs_refresh_inode(dir, o_res->dir_attr);
1551         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1552                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1553         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1554                 status = _nfs4_proc_open_confirm(data);
1555                 if (status != 0)
1556                         return status;
1557         }
1558         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1559                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1560         return 0;
1561 }
1562
1563 static int nfs4_recover_expired_lease(struct nfs_server *server)
1564 {
1565         struct nfs_client *clp = server->nfs_client;
1566         unsigned int loop;
1567         int ret;
1568
1569         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1570                 ret = nfs4_wait_clnt_recover(clp);
1571                 if (ret != 0)
1572                         break;
1573                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1574                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1575                         break;
1576                 nfs4_schedule_state_recovery(clp);
1577                 ret = -EIO;
1578         }
1579         return ret;
1580 }
1581
1582 /*
1583  * OPEN_EXPIRED:
1584  *      reclaim state on the server after a network partition.
1585  *      Assumes caller holds the appropriate lock
1586  */
1587 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1588 {
1589         struct nfs4_opendata *opendata;
1590         int ret;
1591
1592         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1593         if (IS_ERR(opendata))
1594                 return PTR_ERR(opendata);
1595         ret = nfs4_open_recover(opendata, state);
1596         if (ret == -ESTALE)
1597                 d_drop(ctx->path.dentry);
1598         nfs4_opendata_put(opendata);
1599         return ret;
1600 }
1601
1602 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1603 {
1604         struct nfs_server *server = NFS_SERVER(state->inode);
1605         struct nfs4_exception exception = { };
1606         int err;
1607
1608         do {
1609                 err = _nfs4_open_expired(ctx, state);
1610                 switch (err) {
1611                 default:
1612                         goto out;
1613                 case -NFS4ERR_GRACE:
1614                 case -NFS4ERR_DELAY:
1615                         nfs4_handle_exception(server, err, &exception);
1616                         err = 0;
1617                 }
1618         } while (exception.retry);
1619 out:
1620         return err;
1621 }
1622
1623 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1624 {
1625         struct nfs_open_context *ctx;
1626         int ret;
1627
1628         ctx = nfs4_state_find_open_context(state);
1629         if (IS_ERR(ctx))
1630                 return PTR_ERR(ctx);
1631         ret = nfs4_do_open_expired(ctx, state);
1632         put_nfs_open_context(ctx);
1633         return ret;
1634 }
1635
1636 /*
1637  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1638  * fields corresponding to attributes that were used to store the verifier.
1639  * Make sure we clobber those fields in the later setattr call
1640  */
1641 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1642 {
1643         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1644             !(sattr->ia_valid & ATTR_ATIME_SET))
1645                 sattr->ia_valid |= ATTR_ATIME;
1646
1647         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1648             !(sattr->ia_valid & ATTR_MTIME_SET))
1649                 sattr->ia_valid |= ATTR_MTIME;
1650 }
1651
1652 /*
1653  * Returns a referenced nfs4_state
1654  */
1655 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)
1656 {
1657         struct nfs4_state_owner  *sp;
1658         struct nfs4_state     *state = NULL;
1659         struct nfs_server       *server = NFS_SERVER(dir);
1660         struct nfs4_opendata *opendata;
1661         int status;
1662
1663         /* Protect against reboot recovery conflicts */
1664         status = -ENOMEM;
1665         if (!(sp = nfs4_get_state_owner(server, cred))) {
1666                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1667                 goto out_err;
1668         }
1669         status = nfs4_recover_expired_lease(server);
1670         if (status != 0)
1671                 goto err_put_state_owner;
1672         if (path->dentry->d_inode != NULL)
1673                 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1674         status = -ENOMEM;
1675         opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1676         if (opendata == NULL)
1677                 goto err_put_state_owner;
1678
1679         if (path->dentry->d_inode != NULL)
1680                 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1681
1682         status = _nfs4_proc_open(opendata);
1683         if (status != 0)
1684                 goto err_opendata_put;
1685
1686         state = nfs4_opendata_to_nfs4_state(opendata);
1687         status = PTR_ERR(state);
1688         if (IS_ERR(state))
1689                 goto err_opendata_put;
1690         if (server->caps & NFS_CAP_POSIX_LOCK)
1691                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1692
1693         if (opendata->o_arg.open_flags & O_EXCL) {
1694                 nfs4_exclusive_attrset(opendata, sattr);
1695
1696                 nfs_fattr_init(opendata->o_res.f_attr);
1697                 status = nfs4_do_setattr(state->inode, cred,
1698                                 opendata->o_res.f_attr, sattr,
1699                                 state);
1700                 if (status == 0)
1701                         nfs_setattr_update_inode(state->inode, sattr);
1702                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1703         }
1704         nfs4_opendata_put(opendata);
1705         nfs4_put_state_owner(sp);
1706         *res = state;
1707         return 0;
1708 err_opendata_put:
1709         nfs4_opendata_put(opendata);
1710 err_put_state_owner:
1711         nfs4_put_state_owner(sp);
1712 out_err:
1713         *res = NULL;
1714         return status;
1715 }
1716
1717
1718 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)
1719 {
1720         struct nfs4_exception exception = { };
1721         struct nfs4_state *res;
1722         int status;
1723
1724         do {
1725                 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1726                 if (status == 0)
1727                         break;
1728                 /* NOTE: BAD_SEQID means the server and client disagree about the
1729                  * book-keeping w.r.t. state-changing operations
1730                  * (OPEN/CLOSE/LOCK/LOCKU...)
1731                  * It is actually a sign of a bug on the client or on the server.
1732                  *
1733                  * If we receive a BAD_SEQID error in the particular case of
1734                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1735                  * have unhashed the old state_owner for us, and that we can
1736                  * therefore safely retry using a new one. We should still warn
1737                  * the user though...
1738                  */
1739                 if (status == -NFS4ERR_BAD_SEQID) {
1740                         printk(KERN_WARNING "NFS: v4 server %s "
1741                                         " returned a bad sequence-id error!\n",
1742                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1743                         exception.retry = 1;
1744                         continue;
1745                 }
1746                 /*
1747                  * BAD_STATEID on OPEN means that the server cancelled our
1748                  * state before it received the OPEN_CONFIRM.
1749                  * Recover by retrying the request as per the discussion
1750                  * on Page 181 of RFC3530.
1751                  */
1752                 if (status == -NFS4ERR_BAD_STATEID) {
1753                         exception.retry = 1;
1754                         continue;
1755                 }
1756                 if (status == -EAGAIN) {
1757                         /* We must have found a delegation */
1758                         exception.retry = 1;
1759                         continue;
1760                 }
1761                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1762                                         status, &exception));
1763         } while (exception.retry);
1764         return res;
1765 }
1766
1767 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1768                             struct nfs_fattr *fattr, struct iattr *sattr,
1769                             struct nfs4_state *state)
1770 {
1771         struct nfs_server *server = NFS_SERVER(inode);
1772         struct nfs_setattrargs  arg = {
1773                 .fh             = NFS_FH(inode),
1774                 .iap            = sattr,
1775                 .server         = server,
1776                 .bitmask = server->attr_bitmask,
1777         };
1778         struct nfs_setattrres  res = {
1779                 .fattr          = fattr,
1780                 .server         = server,
1781         };
1782         struct rpc_message msg = {
1783                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1784                 .rpc_argp       = &arg,
1785                 .rpc_resp       = &res,
1786                 .rpc_cred       = cred,
1787         };
1788         unsigned long timestamp = jiffies;
1789         int status;
1790
1791         nfs_fattr_init(fattr);
1792
1793         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1794                 /* Use that stateid */
1795         } else if (state != NULL) {
1796                 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1797         } else
1798                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1799
1800         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1801         if (status == 0 && state != NULL)
1802                 renew_lease(server, timestamp);
1803         return status;
1804 }
1805
1806 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1807                            struct nfs_fattr *fattr, struct iattr *sattr,
1808                            struct nfs4_state *state)
1809 {
1810         struct nfs_server *server = NFS_SERVER(inode);
1811         struct nfs4_exception exception = { };
1812         int err;
1813         do {
1814                 err = nfs4_handle_exception(server,
1815                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1816                                 &exception);
1817         } while (exception.retry);
1818         return err;
1819 }
1820
1821 struct nfs4_closedata {
1822         struct path path;
1823         struct inode *inode;
1824         struct nfs4_state *state;
1825         struct nfs_closeargs arg;
1826         struct nfs_closeres res;
1827         struct nfs_fattr fattr;
1828         unsigned long timestamp;
1829 };
1830
1831 static void nfs4_free_closedata(void *data)
1832 {
1833         struct nfs4_closedata *calldata = data;
1834         struct nfs4_state_owner *sp = calldata->state->owner;
1835
1836         nfs4_put_open_state(calldata->state);
1837         nfs_free_seqid(calldata->arg.seqid);
1838         nfs4_put_state_owner(sp);
1839         path_put(&calldata->path);
1840         kfree(calldata);
1841 }
1842
1843 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1844                 fmode_t fmode)
1845 {
1846         spin_lock(&state->owner->so_lock);
1847         if (!(fmode & FMODE_READ))
1848                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1849         if (!(fmode & FMODE_WRITE))
1850                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1851         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1852         spin_unlock(&state->owner->so_lock);
1853 }
1854
1855 static void nfs4_close_done(struct rpc_task *task, void *data)
1856 {
1857         struct nfs4_closedata *calldata = data;
1858         struct nfs4_state *state = calldata->state;
1859         struct nfs_server *server = NFS_SERVER(calldata->inode);
1860
1861         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1862                 return;
1863         /* hmm. we are done with the inode, and in the process of freeing
1864          * the state_owner. we keep this around to process errors
1865          */
1866         switch (task->tk_status) {
1867                 case 0:
1868                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1869                         renew_lease(server, calldata->timestamp);
1870                         nfs4_close_clear_stateid_flags(state,
1871                                         calldata->arg.fmode);
1872                         break;
1873                 case -NFS4ERR_STALE_STATEID:
1874                 case -NFS4ERR_OLD_STATEID:
1875                 case -NFS4ERR_BAD_STATEID:
1876                 case -NFS4ERR_EXPIRED:
1877                         if (calldata->arg.fmode == 0)
1878                                 break;
1879                 default:
1880                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1881                                 rpc_restart_call_prepare(task);
1882         }
1883         nfs_release_seqid(calldata->arg.seqid);
1884         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1885 }
1886
1887 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1888 {
1889         struct nfs4_closedata *calldata = data;
1890         struct nfs4_state *state = calldata->state;
1891         int call_close = 0;
1892
1893         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1894                 return;
1895
1896         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1897         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1898         spin_lock(&state->owner->so_lock);
1899         /* Calculate the change in open mode */
1900         if (state->n_rdwr == 0) {
1901                 if (state->n_rdonly == 0) {
1902                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1903                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1904                         calldata->arg.fmode &= ~FMODE_READ;
1905                 }
1906                 if (state->n_wronly == 0) {
1907                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1908                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1909                         calldata->arg.fmode &= ~FMODE_WRITE;
1910                 }
1911         }
1912         spin_unlock(&state->owner->so_lock);
1913
1914         if (!call_close) {
1915                 /* Note: exit _without_ calling nfs4_close_done */
1916                 task->tk_action = NULL;
1917                 return;
1918         }
1919
1920         if (calldata->arg.fmode == 0)
1921                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1922
1923         nfs_fattr_init(calldata->res.fattr);
1924         calldata->timestamp = jiffies;
1925         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1926                                 &calldata->arg.seq_args, &calldata->res.seq_res,
1927                                 1, task))
1928                 return;
1929         rpc_call_start(task);
1930 }
1931
1932 static const struct rpc_call_ops nfs4_close_ops = {
1933         .rpc_call_prepare = nfs4_close_prepare,
1934         .rpc_call_done = nfs4_close_done,
1935         .rpc_release = nfs4_free_closedata,
1936 };
1937
1938 /* 
1939  * It is possible for data to be read/written from a mem-mapped file 
1940  * after the sys_close call (which hits the vfs layer as a flush).
1941  * This means that we can't safely call nfsv4 close on a file until 
1942  * the inode is cleared. This in turn means that we are not good
1943  * NFSv4 citizens - we do not indicate to the server to update the file's 
1944  * share state even when we are done with one of the three share 
1945  * stateid's in the inode.
1946  *
1947  * NOTE: Caller must be holding the sp->so_owner semaphore!
1948  */
1949 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
1950 {
1951         struct nfs_server *server = NFS_SERVER(state->inode);
1952         struct nfs4_closedata *calldata;
1953         struct nfs4_state_owner *sp = state->owner;
1954         struct rpc_task *task;
1955         struct rpc_message msg = {
1956                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1957                 .rpc_cred = state->owner->so_cred,
1958         };
1959         struct rpc_task_setup task_setup_data = {
1960                 .rpc_client = server->client,
1961                 .rpc_message = &msg,
1962                 .callback_ops = &nfs4_close_ops,
1963                 .workqueue = nfsiod_workqueue,
1964                 .flags = RPC_TASK_ASYNC,
1965         };
1966         int status = -ENOMEM;
1967
1968         calldata = kzalloc(sizeof(*calldata), gfp_mask);
1969         if (calldata == NULL)
1970                 goto out;
1971         calldata->inode = state->inode;
1972         calldata->state = state;
1973         calldata->arg.fh = NFS_FH(state->inode);
1974         calldata->arg.stateid = &state->open_stateid;
1975         /* Serialization for the sequence id */
1976         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
1977         if (calldata->arg.seqid == NULL)
1978                 goto out_free_calldata;
1979         calldata->arg.fmode = 0;
1980         calldata->arg.bitmask = server->cache_consistency_bitmask;
1981         calldata->res.fattr = &calldata->fattr;
1982         calldata->res.seqid = calldata->arg.seqid;
1983         calldata->res.server = server;
1984         path_get(path);
1985         calldata->path = *path;
1986
1987         msg.rpc_argp = &calldata->arg,
1988         msg.rpc_resp = &calldata->res,
1989         task_setup_data.callback_data = calldata;
1990         task = rpc_run_task(&task_setup_data);
1991         if (IS_ERR(task))
1992                 return PTR_ERR(task);
1993         status = 0;
1994         if (wait)
1995                 status = rpc_wait_for_completion_task(task);
1996         rpc_put_task(task);
1997         return status;
1998 out_free_calldata:
1999         kfree(calldata);
2000 out:
2001         nfs4_put_open_state(state);
2002         nfs4_put_state_owner(sp);
2003         return status;
2004 }
2005
2006 static struct inode *
2007 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2008 {
2009         struct nfs4_state *state;
2010
2011         /* Protect against concurrent sillydeletes */
2012         state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2013         if (IS_ERR(state))
2014                 return ERR_CAST(state);
2015         ctx->state = state;
2016         return igrab(state->inode);
2017 }
2018
2019 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2020 {
2021         if (ctx->state == NULL)
2022                 return;
2023         if (is_sync)
2024                 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2025         else
2026                 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2027 }
2028
2029 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2030 {
2031         struct nfs4_server_caps_arg args = {
2032                 .fhandle = fhandle,
2033         };
2034         struct nfs4_server_caps_res res = {};
2035         struct rpc_message msg = {
2036                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2037                 .rpc_argp = &args,
2038                 .rpc_resp = &res,
2039         };
2040         int status;
2041
2042         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2043         if (status == 0) {
2044                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2045                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2046                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2047                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2048                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2049                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2050                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2051                         server->caps |= NFS_CAP_ACLS;
2052                 if (res.has_links != 0)
2053                         server->caps |= NFS_CAP_HARDLINKS;
2054                 if (res.has_symlinks != 0)
2055                         server->caps |= NFS_CAP_SYMLINKS;
2056                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2057                         server->caps |= NFS_CAP_FILEID;
2058                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2059                         server->caps |= NFS_CAP_MODE;
2060                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2061                         server->caps |= NFS_CAP_NLINK;
2062                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2063                         server->caps |= NFS_CAP_OWNER;
2064                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2065                         server->caps |= NFS_CAP_OWNER_GROUP;
2066                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2067                         server->caps |= NFS_CAP_ATIME;
2068                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2069                         server->caps |= NFS_CAP_CTIME;
2070                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2071                         server->caps |= NFS_CAP_MTIME;
2072
2073                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2074                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2075                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2076                 server->acl_bitmask = res.acl_bitmask;
2077         }
2078
2079         return status;
2080 }
2081
2082 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2083 {
2084         struct nfs4_exception exception = { };
2085         int err;
2086         do {
2087                 err = nfs4_handle_exception(server,
2088                                 _nfs4_server_capabilities(server, fhandle),
2089                                 &exception);
2090         } while (exception.retry);
2091         return err;
2092 }
2093
2094 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2095                 struct nfs_fsinfo *info)
2096 {
2097         struct nfs4_lookup_root_arg args = {
2098                 .bitmask = nfs4_fattr_bitmap,
2099         };
2100         struct nfs4_lookup_res res = {
2101                 .server = server,
2102                 .fattr = info->fattr,
2103                 .fh = fhandle,
2104         };
2105         struct rpc_message msg = {
2106                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2107                 .rpc_argp = &args,
2108                 .rpc_resp = &res,
2109         };
2110
2111         nfs_fattr_init(info->fattr);
2112         return nfs4_call_sync(server, &msg, &args, &res, 0);
2113 }
2114
2115 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2116                 struct nfs_fsinfo *info)
2117 {
2118         struct nfs4_exception exception = { };
2119         int err;
2120         do {
2121                 err = nfs4_handle_exception(server,
2122                                 _nfs4_lookup_root(server, fhandle, info),
2123                                 &exception);
2124         } while (exception.retry);
2125         return err;
2126 }
2127
2128 /*
2129  * get the file handle for the "/" directory on the server
2130  */
2131 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2132                               struct nfs_fsinfo *info)
2133 {
2134         int status;
2135
2136         status = nfs4_lookup_root(server, fhandle, info);
2137         if (status == 0)
2138                 status = nfs4_server_capabilities(server, fhandle);
2139         if (status == 0)
2140                 status = nfs4_do_fsinfo(server, fhandle, info);
2141         return nfs4_map_errors(status);
2142 }
2143
2144 /*
2145  * Get locations and (maybe) other attributes of a referral.
2146  * Note that we'll actually follow the referral later when
2147  * we detect fsid mismatch in inode revalidation
2148  */
2149 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2150 {
2151         int status = -ENOMEM;
2152         struct page *page = NULL;
2153         struct nfs4_fs_locations *locations = NULL;
2154
2155         page = alloc_page(GFP_KERNEL);
2156         if (page == NULL)
2157                 goto out;
2158         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2159         if (locations == NULL)
2160                 goto out;
2161
2162         status = nfs4_proc_fs_locations(dir, name, locations, page);
2163         if (status != 0)
2164                 goto out;
2165         /* Make sure server returned a different fsid for the referral */
2166         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2167                 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2168                 status = -EIO;
2169                 goto out;
2170         }
2171
2172         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2173         fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2174         if (!fattr->mode)
2175                 fattr->mode = S_IFDIR;
2176         memset(fhandle, 0, sizeof(struct nfs_fh));
2177 out:
2178         if (page)
2179                 __free_page(page);
2180         kfree(locations);
2181         return status;
2182 }
2183
2184 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2185 {
2186         struct nfs4_getattr_arg args = {
2187                 .fh = fhandle,
2188                 .bitmask = server->attr_bitmask,
2189         };
2190         struct nfs4_getattr_res res = {
2191                 .fattr = fattr,
2192                 .server = server,
2193         };
2194         struct rpc_message msg = {
2195                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2196                 .rpc_argp = &args,
2197                 .rpc_resp = &res,
2198         };
2199         
2200         nfs_fattr_init(fattr);
2201         return nfs4_call_sync(server, &msg, &args, &res, 0);
2202 }
2203
2204 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2205 {
2206         struct nfs4_exception exception = { };
2207         int err;
2208         do {
2209                 err = nfs4_handle_exception(server,
2210                                 _nfs4_proc_getattr(server, fhandle, fattr),
2211                                 &exception);
2212         } while (exception.retry);
2213         return err;
2214 }
2215
2216 /* 
2217  * The file is not closed if it is opened due to the a request to change
2218  * the size of the file. The open call will not be needed once the
2219  * VFS layer lookup-intents are implemented.
2220  *
2221  * Close is called when the inode is destroyed.
2222  * If we haven't opened the file for O_WRONLY, we
2223  * need to in the size_change case to obtain a stateid.
2224  *
2225  * Got race?
2226  * Because OPEN is always done by name in nfsv4, it is
2227  * possible that we opened a different file by the same
2228  * name.  We can recognize this race condition, but we
2229  * can't do anything about it besides returning an error.
2230  *
2231  * This will be fixed with VFS changes (lookup-intent).
2232  */
2233 static int
2234 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2235                   struct iattr *sattr)
2236 {
2237         struct inode *inode = dentry->d_inode;
2238         struct rpc_cred *cred = NULL;
2239         struct nfs4_state *state = NULL;
2240         int status;
2241
2242         nfs_fattr_init(fattr);
2243         
2244         /* Search for an existing open(O_WRITE) file */
2245         if (sattr->ia_valid & ATTR_FILE) {
2246                 struct nfs_open_context *ctx;
2247
2248                 ctx = nfs_file_open_context(sattr->ia_file);
2249                 if (ctx) {
2250                         cred = ctx->cred;
2251                         state = ctx->state;
2252                 }
2253         }
2254
2255         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2256         if (status == 0)
2257                 nfs_setattr_update_inode(inode, sattr);
2258         return status;
2259 }
2260
2261 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2262                 const struct qstr *name, struct nfs_fh *fhandle,
2263                 struct nfs_fattr *fattr)
2264 {
2265         int                    status;
2266         struct nfs4_lookup_arg args = {
2267                 .bitmask = server->attr_bitmask,
2268                 .dir_fh = dirfh,
2269                 .name = name,
2270         };
2271         struct nfs4_lookup_res res = {
2272                 .server = server,
2273                 .fattr = fattr,
2274                 .fh = fhandle,
2275         };
2276         struct rpc_message msg = {
2277                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2278                 .rpc_argp = &args,
2279                 .rpc_resp = &res,
2280         };
2281
2282         nfs_fattr_init(fattr);
2283
2284         dprintk("NFS call  lookupfh %s\n", name->name);
2285         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2286         dprintk("NFS reply lookupfh: %d\n", status);
2287         return status;
2288 }
2289
2290 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2291                               struct qstr *name, struct nfs_fh *fhandle,
2292                               struct nfs_fattr *fattr)
2293 {
2294         struct nfs4_exception exception = { };
2295         int err;
2296         do {
2297                 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2298                 /* FIXME: !!!! */
2299                 if (err == -NFS4ERR_MOVED) {
2300                         err = -EREMOTE;
2301                         break;
2302                 }
2303                 err = nfs4_handle_exception(server, err, &exception);
2304         } while (exception.retry);
2305         return err;
2306 }
2307
2308 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2309                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310 {
2311         int status;
2312         
2313         dprintk("NFS call  lookup %s\n", name->name);
2314         status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2315         if (status == -NFS4ERR_MOVED)
2316                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2317         dprintk("NFS reply lookup: %d\n", status);
2318         return status;
2319 }
2320
2321 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2322 {
2323         struct nfs4_exception exception = { };
2324         int err;
2325         do {
2326                 err = nfs4_handle_exception(NFS_SERVER(dir),
2327                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2328                                 &exception);
2329         } while (exception.retry);
2330         return err;
2331 }
2332
2333 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2334 {
2335         struct nfs_server *server = NFS_SERVER(inode);
2336         struct nfs4_accessargs args = {
2337                 .fh = NFS_FH(inode),
2338                 .bitmask = server->attr_bitmask,
2339         };
2340         struct nfs4_accessres res = {
2341                 .server = server,
2342         };
2343         struct rpc_message msg = {
2344                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2345                 .rpc_argp = &args,
2346                 .rpc_resp = &res,
2347                 .rpc_cred = entry->cred,
2348         };
2349         int mode = entry->mask;
2350         int status;
2351
2352         /*
2353          * Determine which access bits we want to ask for...
2354          */
2355         if (mode & MAY_READ)
2356                 args.access |= NFS4_ACCESS_READ;
2357         if (S_ISDIR(inode->i_mode)) {
2358                 if (mode & MAY_WRITE)
2359                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2360                 if (mode & MAY_EXEC)
2361                         args.access |= NFS4_ACCESS_LOOKUP;
2362         } else {
2363                 if (mode & MAY_WRITE)
2364                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2365                 if (mode & MAY_EXEC)
2366                         args.access |= NFS4_ACCESS_EXECUTE;
2367         }
2368
2369         res.fattr = nfs_alloc_fattr();
2370         if (res.fattr == NULL)
2371                 return -ENOMEM;
2372
2373         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2374         if (!status) {
2375                 entry->mask = 0;
2376                 if (res.access & NFS4_ACCESS_READ)
2377                         entry->mask |= MAY_READ;
2378                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2379                         entry->mask |= MAY_WRITE;
2380                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2381                         entry->mask |= MAY_EXEC;
2382                 nfs_refresh_inode(inode, res.fattr);
2383         }
2384         nfs_free_fattr(res.fattr);
2385         return status;
2386 }
2387
2388 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2389 {
2390         struct nfs4_exception exception = { };
2391         int err;
2392         do {
2393                 err = nfs4_handle_exception(NFS_SERVER(inode),
2394                                 _nfs4_proc_access(inode, entry),
2395                                 &exception);
2396         } while (exception.retry);
2397         return err;
2398 }
2399
2400 /*
2401  * TODO: For the time being, we don't try to get any attributes
2402  * along with any of the zero-copy operations READ, READDIR,
2403  * READLINK, WRITE.
2404  *
2405  * In the case of the first three, we want to put the GETATTR
2406  * after the read-type operation -- this is because it is hard
2407  * to predict the length of a GETATTR response in v4, and thus
2408  * align the READ data correctly.  This means that the GETATTR
2409  * may end up partially falling into the page cache, and we should
2410  * shift it into the 'tail' of the xdr_buf before processing.
2411  * To do this efficiently, we need to know the total length
2412  * of data received, which doesn't seem to be available outside
2413  * of the RPC layer.
2414  *
2415  * In the case of WRITE, we also want to put the GETATTR after
2416  * the operation -- in this case because we want to make sure
2417  * we get the post-operation mtime and size.  This means that
2418  * we can't use xdr_encode_pages() as written: we need a variant
2419  * of it which would leave room in the 'tail' iovec.
2420  *
2421  * Both of these changes to the XDR layer would in fact be quite
2422  * minor, but I decided to leave them for a subsequent patch.
2423  */
2424 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2425                 unsigned int pgbase, unsigned int pglen)
2426 {
2427         struct nfs4_readlink args = {
2428                 .fh       = NFS_FH(inode),
2429                 .pgbase   = pgbase,
2430                 .pglen    = pglen,
2431                 .pages    = &page,
2432         };
2433         struct nfs4_readlink_res res;
2434         struct rpc_message msg = {
2435                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2436                 .rpc_argp = &args,
2437                 .rpc_resp = &res,
2438         };
2439
2440         return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2441 }
2442
2443 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2444                 unsigned int pgbase, unsigned int pglen)
2445 {
2446         struct nfs4_exception exception = { };
2447         int err;
2448         do {
2449                 err = nfs4_handle_exception(NFS_SERVER(inode),
2450                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2451                                 &exception);
2452         } while (exception.retry);
2453         return err;
2454 }
2455
2456 /*
2457  * Got race?
2458  * We will need to arrange for the VFS layer to provide an atomic open.
2459  * Until then, this create/open method is prone to inefficiency and race
2460  * conditions due to the lookup, create, and open VFS calls from sys_open()
2461  * placed on the wire.
2462  *
2463  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2464  * The file will be opened again in the subsequent VFS open call
2465  * (nfs4_proc_file_open).
2466  *
2467  * The open for read will just hang around to be used by any process that
2468  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2469  */
2470
2471 static int
2472 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2473                  int flags, struct nfs_open_context *ctx)
2474 {
2475         struct path my_path = {
2476                 .dentry = dentry,
2477         };
2478         struct path *path = &my_path;
2479         struct nfs4_state *state;
2480         struct rpc_cred *cred = NULL;
2481         fmode_t fmode = 0;
2482         int status = 0;
2483
2484         if (ctx != NULL) {
2485                 cred = ctx->cred;
2486                 path = &ctx->path;
2487                 fmode = ctx->mode;
2488         }
2489         state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2490         d_drop(dentry);
2491         if (IS_ERR(state)) {
2492                 status = PTR_ERR(state);
2493                 goto out;
2494         }
2495         d_add(dentry, igrab(state->inode));
2496         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2497         if (ctx != NULL)
2498                 ctx->state = state;
2499         else
2500                 nfs4_close_sync(path, state, fmode);
2501 out:
2502         return status;
2503 }
2504
2505 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2506 {
2507         struct nfs_server *server = NFS_SERVER(dir);
2508         struct nfs_removeargs args = {
2509                 .fh = NFS_FH(dir),
2510                 .name.len = name->len,
2511                 .name.name = name->name,
2512                 .bitmask = server->attr_bitmask,
2513         };
2514         struct nfs_removeres res = {
2515                 .server = server,
2516         };
2517         struct rpc_message msg = {
2518                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2519                 .rpc_argp = &args,
2520                 .rpc_resp = &res,
2521         };
2522         int status = -ENOMEM;
2523
2524         res.dir_attr = nfs_alloc_fattr();
2525         if (res.dir_attr == NULL)
2526                 goto out;
2527
2528         status = nfs4_call_sync(server, &msg, &args, &res, 1);
2529         if (status == 0) {
2530                 update_changeattr(dir, &res.cinfo);
2531                 nfs_post_op_update_inode(dir, res.dir_attr);
2532         }
2533         nfs_free_fattr(res.dir_attr);
2534 out:
2535         return status;
2536 }
2537
2538 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2539 {
2540         struct nfs4_exception exception = { };
2541         int err;
2542         do {
2543                 err = nfs4_handle_exception(NFS_SERVER(dir),
2544                                 _nfs4_proc_remove(dir, name),
2545                                 &exception);
2546         } while (exception.retry);
2547         return err;
2548 }
2549
2550 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2551 {
2552         struct nfs_server *server = NFS_SERVER(dir);
2553         struct nfs_removeargs *args = msg->rpc_argp;
2554         struct nfs_removeres *res = msg->rpc_resp;
2555
2556         args->bitmask = server->cache_consistency_bitmask;
2557         res->server = server;
2558         res->seq_res.sr_slot = NULL;
2559         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2560 }
2561
2562 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2563 {
2564         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2565
2566         if (!nfs4_sequence_done(task, &res->seq_res))
2567                 return 0;
2568         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2569                 return 0;
2570         update_changeattr(dir, &res->cinfo);
2571         nfs_post_op_update_inode(dir, res->dir_attr);
2572         return 1;
2573 }
2574
2575 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2576 {
2577         struct nfs_server *server = NFS_SERVER(dir);
2578         struct nfs_renameargs *arg = msg->rpc_argp;
2579         struct nfs_renameres *res = msg->rpc_resp;
2580
2581         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2582         arg->bitmask = server->attr_bitmask;
2583         res->server = server;
2584 }
2585
2586 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2587                                  struct inode *new_dir)
2588 {
2589         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2590
2591         if (!nfs4_sequence_done(task, &res->seq_res))
2592                 return 0;
2593         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2594                 return 0;
2595
2596         update_changeattr(old_dir, &res->old_cinfo);
2597         nfs_post_op_update_inode(old_dir, res->old_fattr);
2598         update_changeattr(new_dir, &res->new_cinfo);
2599         nfs_post_op_update_inode(new_dir, res->new_fattr);
2600         return 1;
2601 }
2602
2603 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2604                 struct inode *new_dir, struct qstr *new_name)
2605 {
2606         struct nfs_server *server = NFS_SERVER(old_dir);
2607         struct nfs_renameargs arg = {
2608                 .old_dir = NFS_FH(old_dir),
2609                 .new_dir = NFS_FH(new_dir),
2610                 .old_name = old_name,
2611                 .new_name = new_name,
2612                 .bitmask = server->attr_bitmask,
2613         };
2614         struct nfs_renameres res = {
2615                 .server = server,
2616         };
2617         struct rpc_message msg = {
2618                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2619                 .rpc_argp = &arg,
2620                 .rpc_resp = &res,
2621         };
2622         int status = -ENOMEM;
2623         
2624         res.old_fattr = nfs_alloc_fattr();
2625         res.new_fattr = nfs_alloc_fattr();
2626         if (res.old_fattr == NULL || res.new_fattr == NULL)
2627                 goto out;
2628
2629         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2630         if (!status) {
2631                 update_changeattr(old_dir, &res.old_cinfo);
2632                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2633                 update_changeattr(new_dir, &res.new_cinfo);
2634                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2635         }
2636 out:
2637         nfs_free_fattr(res.new_fattr);
2638         nfs_free_fattr(res.old_fattr);
2639         return status;
2640 }
2641
2642 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2643                 struct inode *new_dir, struct qstr *new_name)
2644 {
2645         struct nfs4_exception exception = { };
2646         int err;
2647         do {
2648                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2649                                 _nfs4_proc_rename(old_dir, old_name,
2650                                         new_dir, new_name),
2651                                 &exception);
2652         } while (exception.retry);
2653         return err;
2654 }
2655
2656 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2657 {
2658         struct nfs_server *server = NFS_SERVER(inode);
2659         struct nfs4_link_arg arg = {
2660                 .fh     = NFS_FH(inode),
2661                 .dir_fh = NFS_FH(dir),
2662                 .name   = name,
2663                 .bitmask = server->attr_bitmask,
2664         };
2665         struct nfs4_link_res res = {
2666                 .server = server,
2667         };
2668         struct rpc_message msg = {
2669                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2670                 .rpc_argp = &arg,
2671                 .rpc_resp = &res,
2672         };
2673         int status = -ENOMEM;
2674
2675         res.fattr = nfs_alloc_fattr();
2676         res.dir_attr = nfs_alloc_fattr();
2677         if (res.fattr == NULL || res.dir_attr == NULL)
2678                 goto out;
2679
2680         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2681         if (!status) {
2682                 update_changeattr(dir, &res.cinfo);
2683                 nfs_post_op_update_inode(dir, res.dir_attr);
2684                 nfs_post_op_update_inode(inode, res.fattr);
2685         }
2686 out:
2687         nfs_free_fattr(res.dir_attr);
2688         nfs_free_fattr(res.fattr);
2689         return status;
2690 }
2691
2692 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2693 {
2694         struct nfs4_exception exception = { };
2695         int err;
2696         do {
2697                 err = nfs4_handle_exception(NFS_SERVER(inode),
2698                                 _nfs4_proc_link(inode, dir, name),
2699                                 &exception);
2700         } while (exception.retry);
2701         return err;
2702 }
2703
2704 struct nfs4_createdata {
2705         struct rpc_message msg;
2706         struct nfs4_create_arg arg;
2707         struct nfs4_create_res res;
2708         struct nfs_fh fh;
2709         struct nfs_fattr fattr;
2710         struct nfs_fattr dir_fattr;
2711 };
2712
2713 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2714                 struct qstr *name, struct iattr *sattr, u32 ftype)
2715 {
2716         struct nfs4_createdata *data;
2717
2718         data = kzalloc(sizeof(*data), GFP_KERNEL);
2719         if (data != NULL) {
2720                 struct nfs_server *server = NFS_SERVER(dir);
2721
2722                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2723                 data->msg.rpc_argp = &data->arg;
2724                 data->msg.rpc_resp = &data->res;
2725                 data->arg.dir_fh = NFS_FH(dir);
2726                 data->arg.server = server;
2727                 data->arg.name = name;
2728                 data->arg.attrs = sattr;
2729                 data->arg.ftype = ftype;
2730                 data->arg.bitmask = server->attr_bitmask;
2731                 data->res.server = server;
2732                 data->res.fh = &data->fh;
2733                 data->res.fattr = &data->fattr;
2734                 data->res.dir_fattr = &data->dir_fattr;
2735                 nfs_fattr_init(data->res.fattr);
2736                 nfs_fattr_init(data->res.dir_fattr);
2737         }
2738         return data;
2739 }
2740
2741 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2742 {
2743         int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2744                                     &data->arg, &data->res, 1);
2745         if (status == 0) {
2746                 update_changeattr(dir, &data->res.dir_cinfo);
2747                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2748                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2749         }
2750         return status;
2751 }
2752
2753 static void nfs4_free_createdata(struct nfs4_createdata *data)
2754 {
2755         kfree(data);
2756 }
2757
2758 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2759                 struct page *page, unsigned int len, struct iattr *sattr)
2760 {
2761         struct nfs4_createdata *data;
2762         int status = -ENAMETOOLONG;
2763
2764         if (len > NFS4_MAXPATHLEN)
2765                 goto out;
2766
2767         status = -ENOMEM;
2768         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2769         if (data == NULL)
2770                 goto out;
2771
2772         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2773         data->arg.u.symlink.pages = &page;
2774         data->arg.u.symlink.len = len;
2775         
2776         status = nfs4_do_create(dir, dentry, data);
2777
2778         nfs4_free_createdata(data);
2779 out:
2780         return status;
2781 }
2782
2783 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2784                 struct page *page, unsigned int len, struct iattr *sattr)
2785 {
2786         struct nfs4_exception exception = { };
2787         int err;
2788         do {
2789                 err = nfs4_handle_exception(NFS_SERVER(dir),
2790                                 _nfs4_proc_symlink(dir, dentry, page,
2791                                                         len, sattr),
2792                                 &exception);
2793         } while (exception.retry);
2794         return err;
2795 }
2796
2797 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2798                 struct iattr *sattr)
2799 {
2800         struct nfs4_createdata *data;
2801         int status = -ENOMEM;
2802
2803         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2804         if (data == NULL)
2805                 goto out;
2806
2807         status = nfs4_do_create(dir, dentry, data);
2808
2809         nfs4_free_createdata(data);
2810 out:
2811         return status;
2812 }
2813
2814 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2815                 struct iattr *sattr)
2816 {
2817         struct nfs4_exception exception = { };
2818         int err;
2819         do {
2820                 err = nfs4_handle_exception(NFS_SERVER(dir),
2821                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2822                                 &exception);
2823         } while (exception.retry);
2824         return err;
2825 }
2826
2827 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2828                 u64 cookie, struct page **pages, unsigned int count, int plus)
2829 {
2830         struct inode            *dir = dentry->d_inode;
2831         struct nfs4_readdir_arg args = {
2832                 .fh = NFS_FH(dir),
2833                 .pages = pages,
2834                 .pgbase = 0,
2835                 .count = count,
2836                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2837                 .plus = plus,
2838         };
2839         struct nfs4_readdir_res res;
2840         struct rpc_message msg = {
2841                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2842                 .rpc_argp = &args,
2843                 .rpc_resp = &res,
2844                 .rpc_cred = cred,
2845         };
2846         int                     status;
2847
2848         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2849                         dentry->d_parent->d_name.name,
2850                         dentry->d_name.name,
2851                         (unsigned long long)cookie);
2852         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2853         res.pgbase = args.pgbase;
2854         status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2855         if (status >= 0) {
2856                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2857                 status += args.pgbase;
2858         }
2859
2860         nfs_invalidate_atime(dir);
2861
2862         dprintk("%s: returns %d\n", __func__, status);
2863         return status;
2864 }
2865
2866 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2867                 u64 cookie, struct page **pages, unsigned int count, int plus)
2868 {
2869         struct nfs4_exception exception = { };
2870         int err;
2871         do {
2872                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2873                                 _nfs4_proc_readdir(dentry, cred, cookie,
2874                                         pages, count, plus),
2875                                 &exception);
2876         } while (exception.retry);
2877         return err;
2878 }
2879
2880 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2881                 struct iattr *sattr, dev_t rdev)
2882 {
2883         struct nfs4_createdata *data;
2884         int mode = sattr->ia_mode;
2885         int status = -ENOMEM;
2886
2887         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2888         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2889
2890         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2891         if (data == NULL)
2892                 goto out;
2893
2894         if (S_ISFIFO(mode))
2895                 data->arg.ftype = NF4FIFO;
2896         else if (S_ISBLK(mode)) {
2897                 data->arg.ftype = NF4BLK;
2898                 data->arg.u.device.specdata1 = MAJOR(rdev);
2899                 data->arg.u.device.specdata2 = MINOR(rdev);
2900         }
2901         else if (S_ISCHR(mode)) {
2902                 data->arg.ftype = NF4CHR;
2903                 data->arg.u.device.specdata1 = MAJOR(rdev);
2904                 data->arg.u.device.specdata2 = MINOR(rdev);
2905         }
2906         
2907         status = nfs4_do_create(dir, dentry, data);
2908
2909         nfs4_free_createdata(data);
2910 out:
2911         return status;
2912 }
2913
2914 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2915                 struct iattr *sattr, dev_t rdev)
2916 {
2917         struct nfs4_exception exception = { };
2918         int err;
2919         do {
2920                 err = nfs4_handle_exception(NFS_SERVER(dir),
2921                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2922                                 &exception);
2923         } while (exception.retry);
2924         return err;
2925 }
2926
2927 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2928                  struct nfs_fsstat *fsstat)
2929 {
2930         struct nfs4_statfs_arg args = {
2931                 .fh = fhandle,
2932                 .bitmask = server->attr_bitmask,
2933         };
2934         struct nfs4_statfs_res res = {
2935                 .fsstat = fsstat,
2936         };
2937         struct rpc_message msg = {
2938                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2939                 .rpc_argp = &args,
2940                 .rpc_resp = &res,
2941         };
2942
2943         nfs_fattr_init(fsstat->fattr);
2944         return  nfs4_call_sync(server, &msg, &args, &res, 0);
2945 }
2946
2947 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2948 {
2949         struct nfs4_exception exception = { };
2950         int err;
2951         do {
2952                 err = nfs4_handle_exception(server,
2953                                 _nfs4_proc_statfs(server, fhandle, fsstat),
2954                                 &exception);
2955         } while (exception.retry);
2956         return err;
2957 }
2958
2959 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2960                 struct nfs_fsinfo *fsinfo)
2961 {
2962         struct nfs4_fsinfo_arg args = {
2963                 .fh = fhandle,
2964                 .bitmask = server->attr_bitmask,
2965         };
2966         struct nfs4_fsinfo_res res = {
2967                 .fsinfo = fsinfo,
2968         };
2969         struct rpc_message msg = {
2970                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2971                 .rpc_argp = &args,
2972                 .rpc_resp = &res,
2973         };
2974
2975         return nfs4_call_sync(server, &msg, &args, &res, 0);
2976 }
2977
2978 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2979 {
2980         struct nfs4_exception exception = { };
2981         int err;
2982
2983         do {
2984                 err = nfs4_handle_exception(server,
2985                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2986                                 &exception);
2987         } while (exception.retry);
2988         return err;
2989 }
2990
2991 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2992 {
2993         nfs_fattr_init(fsinfo->fattr);
2994         return nfs4_do_fsinfo(server, fhandle, fsinfo);
2995 }
2996
2997 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2998                 struct nfs_pathconf *pathconf)
2999 {
3000         struct nfs4_pathconf_arg args = {
3001                 .fh = fhandle,
3002                 .bitmask = server->attr_bitmask,
3003         };
3004         struct nfs4_pathconf_res res = {
3005                 .pathconf = pathconf,
3006         };
3007         struct rpc_message msg = {
3008                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3009                 .rpc_argp = &args,
3010                 .rpc_resp = &res,
3011         };
3012
3013         /* None of the pathconf attributes are mandatory to implement */
3014         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3015                 memset(pathconf, 0, sizeof(*pathconf));
3016                 return 0;
3017         }
3018
3019         nfs_fattr_init(pathconf->fattr);
3020         return nfs4_call_sync(server, &msg, &args, &res, 0);
3021 }
3022
3023 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3024                 struct nfs_pathconf *pathconf)
3025 {
3026         struct nfs4_exception exception = { };
3027         int err;
3028
3029         do {
3030                 err = nfs4_handle_exception(server,
3031                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3032                                 &exception);
3033         } while (exception.retry);
3034         return err;
3035 }
3036
3037 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3038 {
3039         struct nfs_server *server = NFS_SERVER(data->inode);
3040
3041         dprintk("--> %s\n", __func__);
3042
3043         if (!nfs4_sequence_done(task, &data->res.seq_res))
3044                 return -EAGAIN;
3045
3046         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3047                 nfs_restart_rpc(task, server->nfs_client);
3048                 return -EAGAIN;
3049         }
3050
3051         nfs_invalidate_atime(data->inode);
3052         if (task->tk_status > 0)
3053                 renew_lease(server, data->timestamp);
3054         return 0;
3055 }
3056
3057 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3058 {
3059         data->timestamp   = jiffies;
3060         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3061 }
3062
3063 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3064 {
3065         struct inode *inode = data->inode;
3066         
3067         if (!nfs4_sequence_done(task, &data->res.seq_res))
3068                 return -EAGAIN;
3069
3070         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3071                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3072                 return -EAGAIN;
3073         }
3074         if (task->tk_status >= 0) {
3075                 renew_lease(NFS_SERVER(inode), data->timestamp);
3076                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3077         }
3078         return 0;
3079 }
3080
3081 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3082 {
3083         struct nfs_server *server = NFS_SERVER(data->inode);
3084
3085         data->args.bitmask = server->cache_consistency_bitmask;
3086         data->res.server = server;
3087         data->timestamp   = jiffies;
3088
3089         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3090 }
3091
3092 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3093 {
3094         struct inode *inode = data->inode;
3095         
3096         if (!nfs4_sequence_done(task, &data->res.seq_res))
3097                 return -EAGAIN;
3098
3099         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3100                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3101                 return -EAGAIN;
3102         }
3103         nfs_refresh_inode(inode, data->res.fattr);
3104         return 0;
3105 }
3106
3107 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3108 {
3109         struct nfs_server *server = NFS_SERVER(data->inode);
3110         
3111         data->args.bitmask = server->cache_consistency_bitmask;
3112         data->res.server = server;
3113         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3114 }
3115
3116 struct nfs4_renewdata {
3117         struct nfs_client       *client;
3118         unsigned long           timestamp;
3119 };
3120
3121 /*
3122  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3123  * standalone procedure for queueing an asynchronous RENEW.
3124  */
3125 static void nfs4_renew_release(void *calldata)
3126 {
3127         struct nfs4_renewdata *data = calldata;
3128         struct nfs_client *clp = data->client;
3129
3130         if (atomic_read(&clp->cl_count) > 1)
3131                 nfs4_schedule_state_renewal(clp);
3132         nfs_put_client(clp);
3133         kfree(data);
3134 }
3135
3136 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3137 {
3138         struct nfs4_renewdata *data = calldata;
3139         struct nfs_client *clp = data->client;
3140         unsigned long timestamp = data->timestamp;
3141
3142         if (task->tk_status < 0) {
3143                 /* Unless we're shutting down, schedule state recovery! */
3144                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3145                         nfs4_schedule_state_recovery(clp);
3146                 return;
3147         }
3148         do_renew_lease(clp, timestamp);
3149 }
3150
3151 static const struct rpc_call_ops nfs4_renew_ops = {
3152         .rpc_call_done = nfs4_renew_done,
3153         .rpc_release = nfs4_renew_release,
3154 };
3155
3156 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3157 {
3158         struct rpc_message msg = {
3159                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3160                 .rpc_argp       = clp,
3161                 .rpc_cred       = cred,
3162         };
3163         struct nfs4_renewdata *data;
3164
3165         if (!atomic_inc_not_zero(&clp->cl_count))
3166                 return -EIO;
3167         data = kmalloc(sizeof(*data), GFP_KERNEL);
3168         if (data == NULL)
3169                 return -ENOMEM;
3170         data->client = clp;
3171         data->timestamp = jiffies;
3172         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3173                         &nfs4_renew_ops, data);
3174 }
3175
3176 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3177 {
3178         struct rpc_message msg = {
3179                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3180                 .rpc_argp       = clp,
3181                 .rpc_cred       = cred,
3182         };
3183         unsigned long now = jiffies;
3184         int status;
3185
3186         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3187         if (status < 0)
3188                 return status;
3189         do_renew_lease(clp, now);
3190         return 0;
3191 }
3192
3193 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3194 {
3195         return (server->caps & NFS_CAP_ACLS)
3196                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3197                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3198 }
3199
3200 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3201  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3202  * the stack.
3203  */
3204 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3205
3206 static void buf_to_pages(const void *buf, size_t buflen,
3207                 struct page **pages, unsigned int *pgbase)
3208 {
3209         const void *p = buf;
3210
3211         *pgbase = offset_in_page(buf);
3212         p -= *pgbase;
3213         while (p < buf + buflen) {
3214                 *(pages++) = virt_to_page(p);
3215                 p += PAGE_CACHE_SIZE;
3216         }
3217 }
3218
3219 struct nfs4_cached_acl {
3220         int cached;
3221         size_t len;
3222         char data[0];
3223 };
3224
3225 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3226 {
3227         struct nfs_inode *nfsi = NFS_I(inode);
3228
3229         spin_lock(&inode->i_lock);
3230         kfree(nfsi->nfs4_acl);
3231         nfsi->nfs4_acl = acl;
3232         spin_unlock(&inode->i_lock);
3233 }
3234
3235 static void nfs4_zap_acl_attr(struct inode *inode)
3236 {
3237         nfs4_set_cached_acl(inode, NULL);
3238 }
3239
3240 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3241 {
3242         struct nfs_inode *nfsi = NFS_I(inode);
3243         struct nfs4_cached_acl *acl;
3244         int ret = -ENOENT;
3245
3246         spin_lock(&inode->i_lock);
3247         acl = nfsi->nfs4_acl;
3248         if (acl == NULL)
3249                 goto out;
3250         if (buf == NULL) /* user is just asking for length */
3251                 goto out_len;
3252         if (acl->cached == 0)
3253                 goto out;
3254         ret = -ERANGE; /* see getxattr(2) man page */
3255         if (acl->len > buflen)
3256                 goto out;
3257         memcpy(buf, acl->data, acl->len);
3258 out_len:
3259         ret = acl->len;
3260 out:
3261         spin_unlock(&inode->i_lock);
3262         return ret;
3263 }
3264
3265 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3266 {
3267         struct nfs4_cached_acl *acl;
3268
3269         if (buf && acl_len <= PAGE_SIZE) {
3270                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3271                 if (acl == NULL)
3272                         goto out;
3273                 acl->cached = 1;
3274                 memcpy(acl->data, buf, acl_len);
3275         } else {
3276                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3277                 if (acl == NULL)
3278                         goto out;
3279                 acl->cached = 0;
3280         }
3281         acl->len = acl_len;
3282 out:
3283         nfs4_set_cached_acl(inode, acl);
3284 }
3285
3286 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3287 {
3288         struct page *pages[NFS4ACL_MAXPAGES];
3289         struct nfs_getaclargs args = {
3290                 .fh = NFS_FH(inode),
3291                 .acl_pages = pages,
3292                 .acl_len = buflen,
3293         };
3294         struct nfs_getaclres res = {
3295                 .acl_len = buflen,
3296         };
3297         void *resp_buf;
3298         struct rpc_message msg = {
3299                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3300                 .rpc_argp = &args,
3301                 .rpc_resp = &res,
3302         };
3303         struct page *localpage = NULL;
3304         int ret;
3305
3306         if (buflen < PAGE_SIZE) {
3307                 /* As long as we're doing a round trip to the server anyway,
3308                  * let's be prepared for a page of acl data. */
3309                 localpage = alloc_page(GFP_KERNEL);
3310                 resp_buf = page_address(localpage);
3311                 if (localpage == NULL)
3312                         return -ENOMEM;
3313                 args.acl_pages[0] = localpage;
3314                 args.acl_pgbase = 0;
3315                 args.acl_len = PAGE_SIZE;
3316         } else {
3317                 resp_buf = buf;
3318                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3319         }
3320         ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3321         if (ret)
3322                 goto out_free;
3323         if (res.acl_len > args.acl_len)
3324                 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3325         else
3326                 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3327         if (buf) {
3328                 ret = -ERANGE;
3329                 if (res.acl_len > buflen)
3330                         goto out_free;
3331                 if (localpage)
3332                         memcpy(buf, resp_buf, res.acl_len);
3333         }
3334         ret = res.acl_len;
3335 out_free:
3336         if (localpage)
3337                 __free_page(localpage);
3338         return ret;
3339 }
3340
3341 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3342 {
3343         struct nfs4_exception exception = { };
3344         ssize_t ret;
3345         do {
3346                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3347                 if (ret >= 0)
3348                         break;
3349                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3350         } while (exception.retry);
3351         return ret;
3352 }
3353
3354 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3355 {
3356         struct nfs_server *server = NFS_SERVER(inode);
3357         int ret;
3358
3359         if (!nfs4_server_supports_acls(server))
3360                 return -EOPNOTSUPP;
3361         ret = nfs_revalidate_inode(server, inode);
3362         if (ret < 0)
3363                 return ret;
3364         ret = nfs4_read_cached_acl(inode, buf, buflen);
3365         if (ret != -ENOENT)
3366                 return ret;
3367         return nfs4_get_acl_uncached(inode, buf, buflen);
3368 }
3369
3370 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3371 {
3372         struct nfs_server *server = NFS_SERVER(inode);
3373         struct page *pages[NFS4ACL_MAXPAGES];
3374         struct nfs_setaclargs arg = {
3375                 .fh             = NFS_FH(inode),
3376                 .acl_pages      = pages,
3377                 .acl_len        = buflen,
3378         };
3379         struct nfs_setaclres res;
3380         struct rpc_message msg = {
3381                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3382                 .rpc_argp       = &arg,
3383                 .rpc_resp       = &res,
3384         };
3385         int ret;
3386
3387         if (!nfs4_server_supports_acls(server))
3388                 return -EOPNOTSUPP;
3389         nfs_inode_return_delegation(inode);
3390         buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3391         ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3392         nfs_access_zap_cache(inode);
3393         nfs_zap_acl_cache(inode);
3394         return ret;
3395 }
3396
3397 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3398 {
3399         struct nfs4_exception exception = { };
3400         int err;
3401         do {
3402                 err = nfs4_handle_exception(NFS_SERVER(inode),
3403                                 __nfs4_proc_set_acl(inode, buf, buflen),
3404                                 &exception);
3405         } while (exception.retry);
3406         return err;
3407 }
3408
3409 static int
3410 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3411 {
3412         struct nfs_client *clp = server->nfs_client;
3413
3414         if (task->tk_status >= 0)
3415                 return 0;
3416         switch(task->tk_status) {
3417                 case -NFS4ERR_ADMIN_REVOKED:
3418                 case -NFS4ERR_BAD_STATEID:
3419                 case -NFS4ERR_OPENMODE:
3420                         if (state == NULL)
3421                                 break;