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