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