NFSv4: Move nfs4_wait_clnt_recover and nfs4_client_recover_expired_lease
[linux-2.6-block.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
59
60 #include "nfs4_fs.h"
61 #include "delegation.h"
62 #include "internal.h"
63 #include "iostat.h"
64 #include "callback.h"
65 #include "pnfs.h"
66 #include "netns.h"
67
68 #define NFSDBG_FACILITY         NFSDBG_PROC
69
70 #define NFS4_POLL_RETRY_MIN     (HZ/10)
71 #define NFS4_POLL_RETRY_MAX     (15*HZ)
72
73 struct nfs4_opendata;
74 static int _nfs4_proc_open(struct nfs4_opendata *data);
75 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
76 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
77 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
78 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
79 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
80 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
81 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82                             struct nfs_fattr *fattr, struct iattr *sattr,
83                             struct nfs4_state *state);
84 #ifdef CONFIG_NFS_V4_1
85 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
86 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
87 #endif
88 /* Prevent leaks of NFSv4 errors into userland */
89 static int nfs4_map_errors(int err)
90 {
91         if (err >= -1000)
92                 return err;
93         switch (err) {
94         case -NFS4ERR_RESOURCE:
95                 return -EREMOTEIO;
96         case -NFS4ERR_WRONGSEC:
97                 return -EPERM;
98         case -NFS4ERR_BADOWNER:
99         case -NFS4ERR_BADNAME:
100                 return -EINVAL;
101         case -NFS4ERR_SHARE_DENIED:
102                 return -EACCES;
103         case -NFS4ERR_MINOR_VERS_MISMATCH:
104                 return -EPROTONOSUPPORT;
105         case -NFS4ERR_ACCESS:
106                 return -EACCES;
107         default:
108                 dprintk("%s could not handle NFSv4 error %d\n",
109                                 __func__, -err);
110                 break;
111         }
112         return -EIO;
113 }
114
115 /*
116  * This is our standard bitmap for GETATTR requests.
117  */
118 const u32 nfs4_fattr_bitmap[3] = {
119         FATTR4_WORD0_TYPE
120         | FATTR4_WORD0_CHANGE
121         | FATTR4_WORD0_SIZE
122         | FATTR4_WORD0_FSID
123         | FATTR4_WORD0_FILEID,
124         FATTR4_WORD1_MODE
125         | FATTR4_WORD1_NUMLINKS
126         | FATTR4_WORD1_OWNER
127         | FATTR4_WORD1_OWNER_GROUP
128         | FATTR4_WORD1_RAWDEV
129         | FATTR4_WORD1_SPACE_USED
130         | FATTR4_WORD1_TIME_ACCESS
131         | FATTR4_WORD1_TIME_METADATA
132         | FATTR4_WORD1_TIME_MODIFY
133 };
134
135 static const u32 nfs4_pnfs_open_bitmap[3] = {
136         FATTR4_WORD0_TYPE
137         | FATTR4_WORD0_CHANGE
138         | FATTR4_WORD0_SIZE
139         | FATTR4_WORD0_FSID
140         | FATTR4_WORD0_FILEID,
141         FATTR4_WORD1_MODE
142         | FATTR4_WORD1_NUMLINKS
143         | FATTR4_WORD1_OWNER
144         | FATTR4_WORD1_OWNER_GROUP
145         | FATTR4_WORD1_RAWDEV
146         | FATTR4_WORD1_SPACE_USED
147         | FATTR4_WORD1_TIME_ACCESS
148         | FATTR4_WORD1_TIME_METADATA
149         | FATTR4_WORD1_TIME_MODIFY,
150         FATTR4_WORD2_MDSTHRESHOLD
151 };
152
153 static const u32 nfs4_open_noattr_bitmap[3] = {
154         FATTR4_WORD0_TYPE
155         | FATTR4_WORD0_CHANGE
156         | FATTR4_WORD0_FILEID,
157 };
158
159 const u32 nfs4_statfs_bitmap[2] = {
160         FATTR4_WORD0_FILES_AVAIL
161         | FATTR4_WORD0_FILES_FREE
162         | FATTR4_WORD0_FILES_TOTAL,
163         FATTR4_WORD1_SPACE_AVAIL
164         | FATTR4_WORD1_SPACE_FREE
165         | FATTR4_WORD1_SPACE_TOTAL
166 };
167
168 const u32 nfs4_pathconf_bitmap[2] = {
169         FATTR4_WORD0_MAXLINK
170         | FATTR4_WORD0_MAXNAME,
171         0
172 };
173
174 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
175                         | FATTR4_WORD0_MAXREAD
176                         | FATTR4_WORD0_MAXWRITE
177                         | FATTR4_WORD0_LEASE_TIME,
178                         FATTR4_WORD1_TIME_DELTA
179                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
180                         FATTR4_WORD2_LAYOUT_BLKSIZE
181 };
182
183 const u32 nfs4_fs_locations_bitmap[2] = {
184         FATTR4_WORD0_TYPE
185         | FATTR4_WORD0_CHANGE
186         | FATTR4_WORD0_SIZE
187         | FATTR4_WORD0_FSID
188         | FATTR4_WORD0_FILEID
189         | FATTR4_WORD0_FS_LOCATIONS,
190         FATTR4_WORD1_MODE
191         | FATTR4_WORD1_NUMLINKS
192         | FATTR4_WORD1_OWNER
193         | FATTR4_WORD1_OWNER_GROUP
194         | FATTR4_WORD1_RAWDEV
195         | FATTR4_WORD1_SPACE_USED
196         | FATTR4_WORD1_TIME_ACCESS
197         | FATTR4_WORD1_TIME_METADATA
198         | FATTR4_WORD1_TIME_MODIFY
199         | FATTR4_WORD1_MOUNTED_ON_FILEID
200 };
201
202 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
203                 struct nfs4_readdir_arg *readdir)
204 {
205         __be32 *start, *p;
206
207         if (cookie > 2) {
208                 readdir->cookie = cookie;
209                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
210                 return;
211         }
212
213         readdir->cookie = 0;
214         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
215         if (cookie == 2)
216                 return;
217         
218         /*
219          * NFSv4 servers do not return entries for '.' and '..'
220          * Therefore, we fake these entries here.  We let '.'
221          * have cookie 0 and '..' have cookie 1.  Note that
222          * when talking to the server, we always send cookie 0
223          * instead of 1 or 2.
224          */
225         start = p = kmap_atomic(*readdir->pages);
226         
227         if (cookie == 0) {
228                 *p++ = xdr_one;                                  /* next */
229                 *p++ = xdr_zero;                   /* cookie, first word */
230                 *p++ = xdr_one;                   /* cookie, second word */
231                 *p++ = xdr_one;                             /* entry len */
232                 memcpy(p, ".\0\0\0", 4);                        /* entry */
233                 p++;
234                 *p++ = xdr_one;                         /* bitmap length */
235                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
236                 *p++ = htonl(8);              /* attribute buffer length */
237                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
238         }
239         
240         *p++ = xdr_one;                                  /* next */
241         *p++ = xdr_zero;                   /* cookie, first word */
242         *p++ = xdr_two;                   /* cookie, second word */
243         *p++ = xdr_two;                             /* entry len */
244         memcpy(p, "..\0\0", 4);                         /* entry */
245         p++;
246         *p++ = xdr_one;                         /* bitmap length */
247         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
248         *p++ = htonl(8);              /* attribute buffer length */
249         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
250
251         readdir->pgbase = (char *)p - (char *)start;
252         readdir->count -= readdir->pgbase;
253         kunmap_atomic(start);
254 }
255
256 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
257 {
258         int res = 0;
259
260         might_sleep();
261
262         if (*timeout <= 0)
263                 *timeout = NFS4_POLL_RETRY_MIN;
264         if (*timeout > NFS4_POLL_RETRY_MAX)
265                 *timeout = NFS4_POLL_RETRY_MAX;
266         freezable_schedule_timeout_killable(*timeout);
267         if (fatal_signal_pending(current))
268                 res = -ERESTARTSYS;
269         *timeout <<= 1;
270         return res;
271 }
272
273 /* This is the error handling routine for processes that are allowed
274  * to sleep.
275  */
276 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
277 {
278         struct nfs_client *clp = server->nfs_client;
279         struct nfs4_state *state = exception->state;
280         struct inode *inode = exception->inode;
281         int ret = errorcode;
282
283         exception->retry = 0;
284         switch(errorcode) {
285                 case 0:
286                         return 0;
287                 case -NFS4ERR_OPENMODE:
288                         if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
289                                 nfs4_inode_return_delegation(inode);
290                                 exception->retry = 1;
291                                 return 0;
292                         }
293                         if (state == NULL)
294                                 break;
295                         nfs4_schedule_stateid_recovery(server, state);
296                         goto wait_on_recovery;
297                 case -NFS4ERR_DELEG_REVOKED:
298                 case -NFS4ERR_ADMIN_REVOKED:
299                 case -NFS4ERR_BAD_STATEID:
300                         if (state == NULL)
301                                 break;
302                         nfs_remove_bad_delegation(state->inode);
303                         nfs4_schedule_stateid_recovery(server, state);
304                         goto wait_on_recovery;
305                 case -NFS4ERR_EXPIRED:
306                         if (state != NULL)
307                                 nfs4_schedule_stateid_recovery(server, state);
308                 case -NFS4ERR_STALE_STATEID:
309                 case -NFS4ERR_STALE_CLIENTID:
310                         nfs4_schedule_lease_recovery(clp);
311                         goto wait_on_recovery;
312 #if defined(CONFIG_NFS_V4_1)
313                 case -NFS4ERR_BADSESSION:
314                 case -NFS4ERR_BADSLOT:
315                 case -NFS4ERR_BAD_HIGH_SLOT:
316                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
317                 case -NFS4ERR_DEADSESSION:
318                 case -NFS4ERR_SEQ_FALSE_RETRY:
319                 case -NFS4ERR_SEQ_MISORDERED:
320                         dprintk("%s ERROR: %d Reset session\n", __func__,
321                                 errorcode);
322                         nfs4_schedule_session_recovery(clp->cl_session, errorcode);
323                         goto wait_on_recovery;
324 #endif /* defined(CONFIG_NFS_V4_1) */
325                 case -NFS4ERR_FILE_OPEN:
326                         if (exception->timeout > HZ) {
327                                 /* We have retried a decent amount, time to
328                                  * fail
329                                  */
330                                 ret = -EBUSY;
331                                 break;
332                         }
333                 case -NFS4ERR_GRACE:
334                 case -NFS4ERR_DELAY:
335                 case -EKEYEXPIRED:
336                         ret = nfs4_delay(server->client, &exception->timeout);
337                         if (ret != 0)
338                                 break;
339                 case -NFS4ERR_RETRY_UNCACHED_REP:
340                 case -NFS4ERR_OLD_STATEID:
341                         exception->retry = 1;
342                         break;
343                 case -NFS4ERR_BADOWNER:
344                         /* The following works around a Linux server bug! */
345                 case -NFS4ERR_BADNAME:
346                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348                                 exception->retry = 1;
349                                 printk(KERN_WARNING "NFS: v4 server %s "
350                                                 "does not accept raw "
351                                                 "uid/gids. "
352                                                 "Reenabling the idmapper.\n",
353                                                 server->nfs_client->cl_hostname);
354                         }
355         }
356         /* We failed to handle the error */
357         return nfs4_map_errors(ret);
358 wait_on_recovery:
359         ret = nfs4_wait_clnt_recover(clp);
360         if (ret == 0)
361                 exception->retry = 1;
362         return ret;
363 }
364
365
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
367 {
368         spin_lock(&clp->cl_lock);
369         if (time_before(clp->cl_last_renewal,timestamp))
370                 clp->cl_last_renewal = timestamp;
371         spin_unlock(&clp->cl_lock);
372 }
373
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
375 {
376         do_renew_lease(server->nfs_client, timestamp);
377 }
378
379 #if defined(CONFIG_NFS_V4_1)
380
381 /*
382  * nfs4_shrink_slot_table - free retired slots from the slot table
383  */
384 static void nfs4_shrink_slot_table(struct nfs4_slot_table  *tbl, u32 newsize)
385 {
386         struct nfs4_slot **p;
387         if (newsize >= tbl->max_slots)
388                 return;
389
390         p = &tbl->slots;
391         while (newsize--)
392                 p = &(*p)->next;
393         while (*p) {
394                 struct nfs4_slot *slot = *p;
395
396                 *p = slot->next;
397                 kfree(slot);
398                 tbl->max_slots--;
399         }
400 }
401
402 /*
403  * nfs4_free_slot - free a slot and efficiently update slot table.
404  *
405  * freeing a slot is trivially done by clearing its respective bit
406  * in the bitmap.
407  * If the freed slotid equals highest_used_slotid we want to update it
408  * so that the server would be able to size down the slot table if needed,
409  * otherwise we know that the highest_used_slotid is still in use.
410  * When updating highest_used_slotid there may be "holes" in the bitmap
411  * so we need to scan down from highest_used_slotid to 0 looking for the now
412  * highest slotid in use.
413  * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
414  *
415  * Must be called while holding tbl->slot_tbl_lock
416  */
417 static void
418 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
419 {
420         u32 slotid = slot->slot_nr;
421
422         /* clear used bit in bitmap */
423         __clear_bit(slotid, tbl->used_slots);
424
425         /* update highest_used_slotid when it is freed */
426         if (slotid == tbl->highest_used_slotid) {
427                 u32 new_max = find_last_bit(tbl->used_slots, slotid);
428                 if (new_max < slotid)
429                         tbl->highest_used_slotid = new_max;
430                 else {
431                         tbl->highest_used_slotid = NFS4_NO_SLOT;
432                         nfs4_session_drain_complete(tbl->session, tbl);
433                 }
434         }
435         dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
436                 slotid, tbl->highest_used_slotid);
437 }
438
439 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
440 {
441         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
442         return true;
443 }
444
445 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
446 {
447         struct nfs4_session *session;
448         struct nfs4_slot_table *tbl;
449
450         if (!res->sr_slot) {
451                 /* just wake up the next guy waiting since
452                  * we may have not consumed a slot after all */
453                 dprintk("%s: No slot\n", __func__);
454                 return;
455         }
456         tbl = res->sr_slot->table;
457         session = tbl->session;
458
459         spin_lock(&tbl->slot_tbl_lock);
460         nfs4_free_slot(tbl, res->sr_slot);
461         if (!nfs4_session_draining(session))
462                 rpc_wake_up_first(&tbl->slot_tbl_waitq,
463                                 nfs4_set_task_privileged, NULL);
464         spin_unlock(&tbl->slot_tbl_lock);
465         res->sr_slot = NULL;
466 }
467
468 /* Update the client's idea of target_highest_slotid */
469 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
470                 u32 target_highest_slotid)
471 {
472         unsigned int max_slotid, i;
473
474         if (tbl->target_highest_slotid == target_highest_slotid)
475                 return;
476         tbl->target_highest_slotid = target_highest_slotid;
477         tbl->generation++;
478
479         max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
480         for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
481                 rpc_wake_up_next(&tbl->slot_tbl_waitq);
482         tbl->max_slotid = max_slotid;
483 }
484
485 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
486                 u32 target_highest_slotid)
487 {
488         spin_lock(&tbl->slot_tbl_lock);
489         nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
490         spin_unlock(&tbl->slot_tbl_lock);
491 }
492
493 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
494                 u32 highest_slotid)
495 {
496         if (tbl->server_highest_slotid == highest_slotid)
497                 return;
498         if (tbl->highest_used_slotid > highest_slotid)
499                 return;
500         /* Deallocate slots */
501         nfs4_shrink_slot_table(tbl, highest_slotid + 1);
502         tbl->server_highest_slotid = highest_slotid;
503 }
504
505 static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
506                 struct nfs4_slot *slot,
507                 struct nfs4_sequence_res *res)
508 {
509         spin_lock(&tbl->slot_tbl_lock);
510         if (tbl->generation != slot->generation)
511                 goto out;
512         nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
513         nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
514 out:
515         spin_unlock(&tbl->slot_tbl_lock);
516 }
517
518 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
519 {
520         struct nfs4_session *session;
521         struct nfs4_slot *slot;
522         unsigned long timestamp;
523         struct nfs_client *clp;
524
525         /*
526          * sr_status remains 1 if an RPC level error occurred. The server
527          * may or may not have processed the sequence operation..
528          * Proceed as if the server received and processed the sequence
529          * operation.
530          */
531         if (res->sr_status == 1)
532                 res->sr_status = NFS_OK;
533
534         /* don't increment the sequence number if the task wasn't sent */
535         if (!RPC_WAS_SENT(task))
536                 goto out;
537
538         slot = res->sr_slot;
539         session = slot->table->session;
540
541         /* Check the SEQUENCE operation status */
542         switch (res->sr_status) {
543         case 0:
544                 /* Update the slot's sequence and clientid lease timer */
545                 ++slot->seq_nr;
546                 timestamp = slot->renewal_time;
547                 clp = session->clp;
548                 do_renew_lease(clp, timestamp);
549                 /* Check sequence flags */
550                 if (res->sr_status_flags != 0)
551                         nfs4_schedule_lease_recovery(clp);
552                 nfs41_update_target_slotid(slot->table, slot, res);
553                 break;
554         case -NFS4ERR_DELAY:
555                 /* The server detected a resend of the RPC call and
556                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
557                  * of RFC5661.
558                  */
559                 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
560                         __func__,
561                         slot->slot_nr,
562                         slot->seq_nr);
563                 goto out_retry;
564         default:
565                 /* Just update the slot sequence no. */
566                 ++slot->seq_nr;
567         }
568 out:
569         /* The session may be reset by one of the error handlers. */
570         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
571         nfs41_sequence_free_slot(res);
572         return 1;
573 out_retry:
574         if (!rpc_restart_call(task))
575                 goto out;
576         rpc_delay(task, NFS4_POLL_RETRY_MAX);
577         return 0;
578 }
579
580 static int nfs4_sequence_done(struct rpc_task *task,
581                                struct nfs4_sequence_res *res)
582 {
583         if (res->sr_slot == NULL)
584                 return 1;
585         return nfs41_sequence_done(task, res);
586 }
587
588 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table  *tbl,
589                 u32 slotid, u32 seq_init, gfp_t gfp_mask)
590 {
591         struct nfs4_slot *slot;
592
593         slot = kzalloc(sizeof(*slot), gfp_mask);
594         if (slot) {
595                 slot->table = tbl;
596                 slot->slot_nr = slotid;
597                 slot->seq_nr = seq_init;
598         }
599         return slot;
600 }
601
602 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table  *tbl,
603                 u32 slotid, u32 seq_init, gfp_t gfp_mask)
604 {
605         struct nfs4_slot **p, *slot;
606
607         p = &tbl->slots;
608         for (;;) {
609                 if (*p == NULL) {
610                         *p = nfs4_new_slot(tbl, tbl->max_slots,
611                                         seq_init, gfp_mask);
612                         if (*p == NULL)
613                                 break;
614                         tbl->max_slots++;
615                 }
616                 slot = *p;
617                 if (slot->slot_nr == slotid)
618                         return slot;
619                 p = &slot->next;
620         }
621         return ERR_PTR(-ENOMEM);
622 }
623
624 /*
625  * nfs4_alloc_slot - efficiently look for a free slot
626  *
627  * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
628  * If found, we mark the slot as used, update the highest_used_slotid,
629  * and respectively set up the sequence operation args.
630  *
631  * Note: must be called with under the slot_tbl_lock.
632  */
633 static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
634 {
635         struct nfs4_slot *ret = ERR_PTR(-EBUSY);
636         u32 slotid;
637
638         dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
639                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
640                 tbl->max_slotid + 1);
641         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
642         if (slotid > tbl->max_slotid)
643                 goto out;
644         ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
645         if (IS_ERR(ret))
646                 goto out;
647         __set_bit(slotid, tbl->used_slots);
648         if (slotid > tbl->highest_used_slotid ||
649                         tbl->highest_used_slotid == NFS4_NO_SLOT)
650                 tbl->highest_used_slotid = slotid;
651         ret->renewal_time = jiffies;
652         ret->generation = tbl->generation;
653
654 out:
655         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
656                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
657                 !IS_ERR(ret) ? ret->slot_nr : -1);
658         return ret;
659 }
660
661 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
662                 struct nfs4_sequence_res *res, int cache_reply)
663 {
664         args->sa_slot = NULL;
665         args->sa_cache_this = 0;
666         if (cache_reply)
667                 args->sa_cache_this = 1;
668         res->sr_slot = NULL;
669 }
670
671 int nfs41_setup_sequence(struct nfs4_session *session,
672                                 struct nfs4_sequence_args *args,
673                                 struct nfs4_sequence_res *res,
674                                 struct rpc_task *task)
675 {
676         struct nfs4_slot *slot;
677         struct nfs4_slot_table *tbl;
678
679         dprintk("--> %s\n", __func__);
680         /* slot already allocated? */
681         if (res->sr_slot != NULL)
682                 return 0;
683
684         tbl = &session->fc_slot_table;
685
686         task->tk_timeout = 0;
687
688         spin_lock(&tbl->slot_tbl_lock);
689         if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
690             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
691                 /* The state manager will wait until the slot table is empty */
692                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
693                 spin_unlock(&tbl->slot_tbl_lock);
694                 dprintk("%s session is draining\n", __func__);
695                 return -EAGAIN;
696         }
697
698         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
699             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
700                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
701                 spin_unlock(&tbl->slot_tbl_lock);
702                 dprintk("%s enforce FIFO order\n", __func__);
703                 return -EAGAIN;
704         }
705
706         slot = nfs4_alloc_slot(tbl);
707         if (IS_ERR(slot)) {
708                 /* If out of memory, try again in 1/4 second */
709                 if (slot == ERR_PTR(-ENOMEM))
710                         task->tk_timeout = HZ >> 2;
711                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
712                 spin_unlock(&tbl->slot_tbl_lock);
713                 dprintk("<-- %s: no free slots\n", __func__);
714                 return -EAGAIN;
715         }
716         spin_unlock(&tbl->slot_tbl_lock);
717
718         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
719
720         args->sa_slot = slot;
721
722         dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
723                         slot->slot_nr, slot->seq_nr);
724
725         res->sr_slot = slot;
726         res->sr_status_flags = 0;
727         /*
728          * sr_status is only set in decode_sequence, and so will remain
729          * set to 1 if an rpc level failure occurs.
730          */
731         res->sr_status = 1;
732         return 0;
733 }
734 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
735
736 int nfs4_setup_sequence(const struct nfs_server *server,
737                         struct nfs4_sequence_args *args,
738                         struct nfs4_sequence_res *res,
739                         struct rpc_task *task)
740 {
741         struct nfs4_session *session = nfs4_get_session(server);
742         int ret = 0;
743
744         if (session == NULL)
745                 goto out;
746
747         dprintk("--> %s clp %p session %p sr_slot %d\n",
748                 __func__, session->clp, session, res->sr_slot ?
749                         res->sr_slot->slot_nr : -1);
750
751         ret = nfs41_setup_sequence(session, args, res, task);
752 out:
753         dprintk("<-- %s status=%d\n", __func__, ret);
754         return ret;
755 }
756
757 struct nfs41_call_sync_data {
758         const struct nfs_server *seq_server;
759         struct nfs4_sequence_args *seq_args;
760         struct nfs4_sequence_res *seq_res;
761 };
762
763 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
764 {
765         struct nfs41_call_sync_data *data = calldata;
766
767         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
768
769         if (nfs4_setup_sequence(data->seq_server, data->seq_args,
770                                 data->seq_res, task))
771                 return;
772         rpc_call_start(task);
773 }
774
775 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
776 {
777         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
778         nfs41_call_sync_prepare(task, calldata);
779 }
780
781 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
782 {
783         struct nfs41_call_sync_data *data = calldata;
784
785         nfs41_sequence_done(task, data->seq_res);
786 }
787
788 static const struct rpc_call_ops nfs41_call_sync_ops = {
789         .rpc_call_prepare = nfs41_call_sync_prepare,
790         .rpc_call_done = nfs41_call_sync_done,
791 };
792
793 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
794         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
795         .rpc_call_done = nfs41_call_sync_done,
796 };
797
798 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
799                                    struct nfs_server *server,
800                                    struct rpc_message *msg,
801                                    struct nfs4_sequence_args *args,
802                                    struct nfs4_sequence_res *res,
803                                    int privileged)
804 {
805         int ret;
806         struct rpc_task *task;
807         struct nfs41_call_sync_data data = {
808                 .seq_server = server,
809                 .seq_args = args,
810                 .seq_res = res,
811         };
812         struct rpc_task_setup task_setup = {
813                 .rpc_client = clnt,
814                 .rpc_message = msg,
815                 .callback_ops = &nfs41_call_sync_ops,
816                 .callback_data = &data
817         };
818
819         if (privileged)
820                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
821         task = rpc_run_task(&task_setup);
822         if (IS_ERR(task))
823                 ret = PTR_ERR(task);
824         else {
825                 ret = task->tk_status;
826                 rpc_put_task(task);
827         }
828         return ret;
829 }
830
831 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
832                             struct nfs_server *server,
833                             struct rpc_message *msg,
834                             struct nfs4_sequence_args *args,
835                             struct nfs4_sequence_res *res,
836                             int cache_reply)
837 {
838         nfs41_init_sequence(args, res, cache_reply);
839         return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
840 }
841
842 #else
843 static inline
844 void nfs41_init_sequence(struct nfs4_sequence_args *args,
845                 struct nfs4_sequence_res *res, int cache_reply)
846 {
847 }
848
849 static int nfs4_sequence_done(struct rpc_task *task,
850                                struct nfs4_sequence_res *res)
851 {
852         return 1;
853 }
854 #endif /* CONFIG_NFS_V4_1 */
855
856 int _nfs4_call_sync(struct rpc_clnt *clnt,
857                     struct nfs_server *server,
858                     struct rpc_message *msg,
859                     struct nfs4_sequence_args *args,
860                     struct nfs4_sequence_res *res,
861                     int cache_reply)
862 {
863         nfs41_init_sequence(args, res, cache_reply);
864         return rpc_call_sync(clnt, msg, 0);
865 }
866
867 static inline
868 int nfs4_call_sync(struct rpc_clnt *clnt,
869                    struct nfs_server *server,
870                    struct rpc_message *msg,
871                    struct nfs4_sequence_args *args,
872                    struct nfs4_sequence_res *res,
873                    int cache_reply)
874 {
875         return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
876                                                 args, res, cache_reply);
877 }
878
879 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
880 {
881         struct nfs_inode *nfsi = NFS_I(dir);
882
883         spin_lock(&dir->i_lock);
884         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
885         if (!cinfo->atomic || cinfo->before != dir->i_version)
886                 nfs_force_lookup_revalidate(dir);
887         dir->i_version = cinfo->after;
888         spin_unlock(&dir->i_lock);
889 }
890
891 struct nfs4_opendata {
892         struct kref kref;
893         struct nfs_openargs o_arg;
894         struct nfs_openres o_res;
895         struct nfs_open_confirmargs c_arg;
896         struct nfs_open_confirmres c_res;
897         struct nfs4_string owner_name;
898         struct nfs4_string group_name;
899         struct nfs_fattr f_attr;
900         struct dentry *dir;
901         struct dentry *dentry;
902         struct nfs4_state_owner *owner;
903         struct nfs4_state *state;
904         struct iattr attrs;
905         unsigned long timestamp;
906         unsigned int rpc_done : 1;
907         int rpc_status;
908         int cancelled;
909 };
910
911
912 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
913 {
914         p->o_res.f_attr = &p->f_attr;
915         p->o_res.seqid = p->o_arg.seqid;
916         p->c_res.seqid = p->c_arg.seqid;
917         p->o_res.server = p->o_arg.server;
918         p->o_res.access_request = p->o_arg.access;
919         nfs_fattr_init(&p->f_attr);
920         nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
921 }
922
923 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
924                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
925                 const struct iattr *attrs,
926                 gfp_t gfp_mask)
927 {
928         struct dentry *parent = dget_parent(dentry);
929         struct inode *dir = parent->d_inode;
930         struct nfs_server *server = NFS_SERVER(dir);
931         struct nfs4_opendata *p;
932
933         p = kzalloc(sizeof(*p), gfp_mask);
934         if (p == NULL)
935                 goto err;
936         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
937         if (p->o_arg.seqid == NULL)
938                 goto err_free;
939         nfs_sb_active(dentry->d_sb);
940         p->dentry = dget(dentry);
941         p->dir = parent;
942         p->owner = sp;
943         atomic_inc(&sp->so_count);
944         p->o_arg.fh = NFS_FH(dir);
945         p->o_arg.open_flags = flags;
946         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
947         /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
948          * will return permission denied for all bits until close */
949         if (!(flags & O_EXCL)) {
950                 /* ask server to check for all possible rights as results
951                  * are cached */
952                 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
953                                   NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
954         }
955         p->o_arg.clientid = server->nfs_client->cl_clientid;
956         p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
957         p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
958         p->o_arg.name = &dentry->d_name;
959         p->o_arg.server = server;
960         p->o_arg.bitmask = server->attr_bitmask;
961         p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
962         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
963         if (attrs != NULL && attrs->ia_valid != 0) {
964                 __be32 verf[2];
965
966                 p->o_arg.u.attrs = &p->attrs;
967                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
968
969                 verf[0] = jiffies;
970                 verf[1] = current->pid;
971                 memcpy(p->o_arg.u.verifier.data, verf,
972                                 sizeof(p->o_arg.u.verifier.data));
973         }
974         p->c_arg.fh = &p->o_res.fh;
975         p->c_arg.stateid = &p->o_res.stateid;
976         p->c_arg.seqid = p->o_arg.seqid;
977         nfs4_init_opendata_res(p);
978         kref_init(&p->kref);
979         return p;
980 err_free:
981         kfree(p);
982 err:
983         dput(parent);
984         return NULL;
985 }
986
987 static void nfs4_opendata_free(struct kref *kref)
988 {
989         struct nfs4_opendata *p = container_of(kref,
990                         struct nfs4_opendata, kref);
991         struct super_block *sb = p->dentry->d_sb;
992
993         nfs_free_seqid(p->o_arg.seqid);
994         if (p->state != NULL)
995                 nfs4_put_open_state(p->state);
996         nfs4_put_state_owner(p->owner);
997         dput(p->dir);
998         dput(p->dentry);
999         nfs_sb_deactive(sb);
1000         nfs_fattr_free_names(&p->f_attr);
1001         kfree(p);
1002 }
1003
1004 static void nfs4_opendata_put(struct nfs4_opendata *p)
1005 {
1006         if (p != NULL)
1007                 kref_put(&p->kref, nfs4_opendata_free);
1008 }
1009
1010 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1011 {
1012         int ret;
1013
1014         ret = rpc_wait_for_completion_task(task);
1015         return ret;
1016 }
1017
1018 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1019 {
1020         int ret = 0;
1021
1022         if (open_mode & (O_EXCL|O_TRUNC))
1023                 goto out;
1024         switch (mode & (FMODE_READ|FMODE_WRITE)) {
1025                 case FMODE_READ:
1026                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1027                                 && state->n_rdonly != 0;
1028                         break;
1029                 case FMODE_WRITE:
1030                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1031                                 && state->n_wronly != 0;
1032                         break;
1033                 case FMODE_READ|FMODE_WRITE:
1034                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1035                                 && state->n_rdwr != 0;
1036         }
1037 out:
1038         return ret;
1039 }
1040
1041 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1042 {
1043         if (delegation == NULL)
1044                 return 0;
1045         if ((delegation->type & fmode) != fmode)
1046                 return 0;
1047         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1048                 return 0;
1049         nfs_mark_delegation_referenced(delegation);
1050         return 1;
1051 }
1052
1053 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1054 {
1055         switch (fmode) {
1056                 case FMODE_WRITE:
1057                         state->n_wronly++;
1058                         break;
1059                 case FMODE_READ:
1060                         state->n_rdonly++;
1061                         break;
1062                 case FMODE_READ|FMODE_WRITE:
1063                         state->n_rdwr++;
1064         }
1065         nfs4_state_set_mode_locked(state, state->state | fmode);
1066 }
1067
1068 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1069 {
1070         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1071                 nfs4_stateid_copy(&state->stateid, stateid);
1072         nfs4_stateid_copy(&state->open_stateid, stateid);
1073         switch (fmode) {
1074                 case FMODE_READ:
1075                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
1076                         break;
1077                 case FMODE_WRITE:
1078                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
1079                         break;
1080                 case FMODE_READ|FMODE_WRITE:
1081                         set_bit(NFS_O_RDWR_STATE, &state->flags);
1082         }
1083 }
1084
1085 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1086 {
1087         write_seqlock(&state->seqlock);
1088         nfs_set_open_stateid_locked(state, stateid, fmode);
1089         write_sequnlock(&state->seqlock);
1090 }
1091
1092 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1093 {
1094         /*
1095          * Protect the call to nfs4_state_set_mode_locked and
1096          * serialise the stateid update
1097          */
1098         write_seqlock(&state->seqlock);
1099         if (deleg_stateid != NULL) {
1100                 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1101                 set_bit(NFS_DELEGATED_STATE, &state->flags);
1102         }
1103         if (open_stateid != NULL)
1104                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1105         write_sequnlock(&state->seqlock);
1106         spin_lock(&state->owner->so_lock);
1107         update_open_stateflags(state, fmode);
1108         spin_unlock(&state->owner->so_lock);
1109 }
1110
1111 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1112 {
1113         struct nfs_inode *nfsi = NFS_I(state->inode);
1114         struct nfs_delegation *deleg_cur;
1115         int ret = 0;
1116
1117         fmode &= (FMODE_READ|FMODE_WRITE);
1118
1119         rcu_read_lock();
1120         deleg_cur = rcu_dereference(nfsi->delegation);
1121         if (deleg_cur == NULL)
1122                 goto no_delegation;
1123
1124         spin_lock(&deleg_cur->lock);
1125         if (nfsi->delegation != deleg_cur ||
1126             (deleg_cur->type & fmode) != fmode)
1127                 goto no_delegation_unlock;
1128
1129         if (delegation == NULL)
1130                 delegation = &deleg_cur->stateid;
1131         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1132                 goto no_delegation_unlock;
1133
1134         nfs_mark_delegation_referenced(deleg_cur);
1135         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1136         ret = 1;
1137 no_delegation_unlock:
1138         spin_unlock(&deleg_cur->lock);
1139 no_delegation:
1140         rcu_read_unlock();
1141
1142         if (!ret && open_stateid != NULL) {
1143                 __update_open_stateid(state, open_stateid, NULL, fmode);
1144                 ret = 1;
1145         }
1146
1147         return ret;
1148 }
1149
1150
1151 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1152 {
1153         struct nfs_delegation *delegation;
1154
1155         rcu_read_lock();
1156         delegation = rcu_dereference(NFS_I(inode)->delegation);
1157         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1158                 rcu_read_unlock();
1159                 return;
1160         }
1161         rcu_read_unlock();
1162         nfs4_inode_return_delegation(inode);
1163 }
1164
1165 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1166 {
1167         struct nfs4_state *state = opendata->state;
1168         struct nfs_inode *nfsi = NFS_I(state->inode);
1169         struct nfs_delegation *delegation;
1170         int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1171         fmode_t fmode = opendata->o_arg.fmode;
1172         nfs4_stateid stateid;
1173         int ret = -EAGAIN;
1174
1175         for (;;) {
1176                 if (can_open_cached(state, fmode, open_mode)) {
1177                         spin_lock(&state->owner->so_lock);
1178                         if (can_open_cached(state, fmode, open_mode)) {
1179                                 update_open_stateflags(state, fmode);
1180                                 spin_unlock(&state->owner->so_lock);
1181                                 goto out_return_state;
1182                         }
1183                         spin_unlock(&state->owner->so_lock);
1184                 }
1185                 rcu_read_lock();
1186                 delegation = rcu_dereference(nfsi->delegation);
1187                 if (!can_open_delegated(delegation, fmode)) {
1188                         rcu_read_unlock();
1189                         break;
1190                 }
1191                 /* Save the delegation */
1192                 nfs4_stateid_copy(&stateid, &delegation->stateid);
1193                 rcu_read_unlock();
1194                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1195                 if (ret != 0)
1196                         goto out;
1197                 ret = -EAGAIN;
1198
1199                 /* Try to update the stateid using the delegation */
1200                 if (update_open_stateid(state, NULL, &stateid, fmode))
1201                         goto out_return_state;
1202         }
1203 out:
1204         return ERR_PTR(ret);
1205 out_return_state:
1206         atomic_inc(&state->count);
1207         return state;
1208 }
1209
1210 static void
1211 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1212 {
1213         struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1214         struct nfs_delegation *delegation;
1215         int delegation_flags = 0;
1216
1217         rcu_read_lock();
1218         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1219         if (delegation)
1220                 delegation_flags = delegation->flags;
1221         rcu_read_unlock();
1222         if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1223                 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1224                                    "returning a delegation for "
1225                                    "OPEN(CLAIM_DELEGATE_CUR)\n",
1226                                    clp->cl_hostname);
1227         } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1228                 nfs_inode_set_delegation(state->inode,
1229                                          data->owner->so_cred,
1230                                          &data->o_res);
1231         else
1232                 nfs_inode_reclaim_delegation(state->inode,
1233                                              data->owner->so_cred,
1234                                              &data->o_res);
1235 }
1236
1237 /*
1238  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1239  * and update the nfs4_state.
1240  */
1241 static struct nfs4_state *
1242 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1243 {
1244         struct inode *inode = data->state->inode;
1245         struct nfs4_state *state = data->state;
1246         int ret;
1247
1248         if (!data->rpc_done) {
1249                 ret = data->rpc_status;
1250                 goto err;
1251         }
1252
1253         ret = -ESTALE;
1254         if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1255             !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1256             !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1257                 goto err;
1258
1259         ret = -ENOMEM;
1260         state = nfs4_get_open_state(inode, data->owner);
1261         if (state == NULL)
1262                 goto err;
1263
1264         ret = nfs_refresh_inode(inode, &data->f_attr);
1265         if (ret)
1266                 goto err;
1267
1268         if (data->o_res.delegation_type != 0)
1269                 nfs4_opendata_check_deleg(data, state);
1270         update_open_stateid(state, &data->o_res.stateid, NULL,
1271                             data->o_arg.fmode);
1272
1273         return state;
1274 err:
1275         return ERR_PTR(ret);
1276
1277 }
1278
1279 static struct nfs4_state *
1280 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1281 {
1282         struct inode *inode;
1283         struct nfs4_state *state = NULL;
1284         int ret;
1285
1286         if (!data->rpc_done) {
1287                 state = nfs4_try_open_cached(data);
1288                 goto out;
1289         }
1290
1291         ret = -EAGAIN;
1292         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1293                 goto err;
1294         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1295         ret = PTR_ERR(inode);
1296         if (IS_ERR(inode))
1297                 goto err;
1298         ret = -ENOMEM;
1299         state = nfs4_get_open_state(inode, data->owner);
1300         if (state == NULL)
1301                 goto err_put_inode;
1302         if (data->o_res.delegation_type != 0)
1303                 nfs4_opendata_check_deleg(data, state);
1304         update_open_stateid(state, &data->o_res.stateid, NULL,
1305                         data->o_arg.fmode);
1306         iput(inode);
1307 out:
1308         return state;
1309 err_put_inode:
1310         iput(inode);
1311 err:
1312         return ERR_PTR(ret);
1313 }
1314
1315 static struct nfs4_state *
1316 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1317 {
1318         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1319                 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1320         return _nfs4_opendata_to_nfs4_state(data);
1321 }
1322
1323 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1324 {
1325         struct nfs_inode *nfsi = NFS_I(state->inode);
1326         struct nfs_open_context *ctx;
1327
1328         spin_lock(&state->inode->i_lock);
1329         list_for_each_entry(ctx, &nfsi->open_files, list) {
1330                 if (ctx->state != state)
1331                         continue;
1332                 get_nfs_open_context(ctx);
1333                 spin_unlock(&state->inode->i_lock);
1334                 return ctx;
1335         }
1336         spin_unlock(&state->inode->i_lock);
1337         return ERR_PTR(-ENOENT);
1338 }
1339
1340 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1341 {
1342         struct nfs4_opendata *opendata;
1343
1344         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1345         if (opendata == NULL)
1346                 return ERR_PTR(-ENOMEM);
1347         opendata->state = state;
1348         atomic_inc(&state->count);
1349         return opendata;
1350 }
1351
1352 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1353 {
1354         struct nfs4_state *newstate;
1355         int ret;
1356
1357         opendata->o_arg.open_flags = 0;
1358         opendata->o_arg.fmode = fmode;
1359         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1360         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1361         nfs4_init_opendata_res(opendata);
1362         ret = _nfs4_recover_proc_open(opendata);
1363         if (ret != 0)
1364                 return ret; 
1365         newstate = nfs4_opendata_to_nfs4_state(opendata);
1366         if (IS_ERR(newstate))
1367                 return PTR_ERR(newstate);
1368         nfs4_close_state(newstate, fmode);
1369         *res = newstate;
1370         return 0;
1371 }
1372
1373 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1374 {
1375         struct nfs4_state *newstate;
1376         int ret;
1377
1378         /* memory barrier prior to reading state->n_* */
1379         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1380         smp_rmb();
1381         if (state->n_rdwr != 0) {
1382                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1383                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1384                 if (ret != 0)
1385                         return ret;
1386                 if (newstate != state)
1387                         return -ESTALE;
1388         }
1389         if (state->n_wronly != 0) {
1390                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1391                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1392                 if (ret != 0)
1393                         return ret;
1394                 if (newstate != state)
1395                         return -ESTALE;
1396         }
1397         if (state->n_rdonly != 0) {
1398                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1399                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1400                 if (ret != 0)
1401                         return ret;
1402                 if (newstate != state)
1403                         return -ESTALE;
1404         }
1405         /*
1406          * We may have performed cached opens for all three recoveries.
1407          * Check if we need to update the current stateid.
1408          */
1409         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1410             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1411                 write_seqlock(&state->seqlock);
1412                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1413                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1414                 write_sequnlock(&state->seqlock);
1415         }
1416         return 0;
1417 }
1418
1419 /*
1420  * OPEN_RECLAIM:
1421  *      reclaim state on the server after a reboot.
1422  */
1423 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1424 {
1425         struct nfs_delegation *delegation;
1426         struct nfs4_opendata *opendata;
1427         fmode_t delegation_type = 0;
1428         int status;
1429
1430         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1431         if (IS_ERR(opendata))
1432                 return PTR_ERR(opendata);
1433         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1434         opendata->o_arg.fh = NFS_FH(state->inode);
1435         rcu_read_lock();
1436         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1437         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1438                 delegation_type = delegation->type;
1439         rcu_read_unlock();
1440         opendata->o_arg.u.delegation_type = delegation_type;
1441         status = nfs4_open_recover(opendata, state);
1442         nfs4_opendata_put(opendata);
1443         return status;
1444 }
1445
1446 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1447 {
1448         struct nfs_server *server = NFS_SERVER(state->inode);
1449         struct nfs4_exception exception = { };
1450         int err;
1451         do {
1452                 err = _nfs4_do_open_reclaim(ctx, state);
1453                 if (err != -NFS4ERR_DELAY)
1454                         break;
1455                 nfs4_handle_exception(server, err, &exception);
1456         } while (exception.retry);
1457         return err;
1458 }
1459
1460 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1461 {
1462         struct nfs_open_context *ctx;
1463         int ret;
1464
1465         ctx = nfs4_state_find_open_context(state);
1466         if (IS_ERR(ctx))
1467                 return PTR_ERR(ctx);
1468         ret = nfs4_do_open_reclaim(ctx, state);
1469         put_nfs_open_context(ctx);
1470         return ret;
1471 }
1472
1473 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1474 {
1475         struct nfs4_opendata *opendata;
1476         int ret;
1477
1478         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1479         if (IS_ERR(opendata))
1480                 return PTR_ERR(opendata);
1481         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1482         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1483         ret = nfs4_open_recover(opendata, state);
1484         nfs4_opendata_put(opendata);
1485         return ret;
1486 }
1487
1488 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1489 {
1490         struct nfs4_exception exception = { };
1491         struct nfs_server *server = NFS_SERVER(state->inode);
1492         int err;
1493         do {
1494                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1495                 switch (err) {
1496                         case 0:
1497                         case -ENOENT:
1498                         case -ESTALE:
1499                                 goto out;
1500                         case -NFS4ERR_BADSESSION:
1501                         case -NFS4ERR_BADSLOT:
1502                         case -NFS4ERR_BAD_HIGH_SLOT:
1503                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1504                         case -NFS4ERR_DEADSESSION:
1505                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1506                                 goto out;
1507                         case -NFS4ERR_STALE_CLIENTID:
1508                         case -NFS4ERR_STALE_STATEID:
1509                         case -NFS4ERR_EXPIRED:
1510                                 /* Don't recall a delegation if it was lost */
1511                                 nfs4_schedule_lease_recovery(server->nfs_client);
1512                                 goto out;
1513                         case -ERESTARTSYS:
1514                                 /*
1515                                  * The show must go on: exit, but mark the
1516                                  * stateid as needing recovery.
1517                                  */
1518                         case -NFS4ERR_DELEG_REVOKED:
1519                         case -NFS4ERR_ADMIN_REVOKED:
1520                         case -NFS4ERR_BAD_STATEID:
1521                                 nfs_inode_find_state_and_recover(state->inode,
1522                                                 stateid);
1523                                 nfs4_schedule_stateid_recovery(server, state);
1524                         case -EKEYEXPIRED:
1525                                 /*
1526                                  * User RPCSEC_GSS context has expired.
1527                                  * We cannot recover this stateid now, so
1528                                  * skip it and allow recovery thread to
1529                                  * proceed.
1530                                  */
1531                         case -ENOMEM:
1532                                 err = 0;
1533                                 goto out;
1534                 }
1535                 err = nfs4_handle_exception(server, err, &exception);
1536         } while (exception.retry);
1537 out:
1538         return err;
1539 }
1540
1541 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1542 {
1543         struct nfs4_opendata *data = calldata;
1544
1545         data->rpc_status = task->tk_status;
1546         if (data->rpc_status == 0) {
1547                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1548                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1549                 renew_lease(data->o_res.server, data->timestamp);
1550                 data->rpc_done = 1;
1551         }
1552 }
1553
1554 static void nfs4_open_confirm_release(void *calldata)
1555 {
1556         struct nfs4_opendata *data = calldata;
1557         struct nfs4_state *state = NULL;
1558
1559         /* If this request hasn't been cancelled, do nothing */
1560         if (data->cancelled == 0)
1561                 goto out_free;
1562         /* In case of error, no cleanup! */
1563         if (!data->rpc_done)
1564                 goto out_free;
1565         state = nfs4_opendata_to_nfs4_state(data);
1566         if (!IS_ERR(state))
1567                 nfs4_close_state(state, data->o_arg.fmode);
1568 out_free:
1569         nfs4_opendata_put(data);
1570 }
1571
1572 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1573         .rpc_call_done = nfs4_open_confirm_done,
1574         .rpc_release = nfs4_open_confirm_release,
1575 };
1576
1577 /*
1578  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1579  */
1580 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1581 {
1582         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1583         struct rpc_task *task;
1584         struct  rpc_message msg = {
1585                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1586                 .rpc_argp = &data->c_arg,
1587                 .rpc_resp = &data->c_res,
1588                 .rpc_cred = data->owner->so_cred,
1589         };
1590         struct rpc_task_setup task_setup_data = {
1591                 .rpc_client = server->client,
1592                 .rpc_message = &msg,
1593                 .callback_ops = &nfs4_open_confirm_ops,
1594                 .callback_data = data,
1595                 .workqueue = nfsiod_workqueue,
1596                 .flags = RPC_TASK_ASYNC,
1597         };
1598         int status;
1599
1600         kref_get(&data->kref);
1601         data->rpc_done = 0;
1602         data->rpc_status = 0;
1603         data->timestamp = jiffies;
1604         task = rpc_run_task(&task_setup_data);
1605         if (IS_ERR(task))
1606                 return PTR_ERR(task);
1607         status = nfs4_wait_for_completion_rpc_task(task);
1608         if (status != 0) {
1609                 data->cancelled = 1;
1610                 smp_wmb();
1611         } else
1612                 status = data->rpc_status;
1613         rpc_put_task(task);
1614         return status;
1615 }
1616
1617 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1618 {
1619         struct nfs4_opendata *data = calldata;
1620         struct nfs4_state_owner *sp = data->owner;
1621
1622         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1623                 return;
1624         /*
1625          * Check if we still need to send an OPEN call, or if we can use
1626          * a delegation instead.
1627          */
1628         if (data->state != NULL) {
1629                 struct nfs_delegation *delegation;
1630
1631                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1632                         goto out_no_action;
1633                 rcu_read_lock();
1634                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1635                 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1636                     can_open_delegated(delegation, data->o_arg.fmode))
1637                         goto unlock_no_action;
1638                 rcu_read_unlock();
1639         }
1640         /* Update client id. */
1641         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1642         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1643                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1644                 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1645                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1646         }
1647         data->timestamp = jiffies;
1648         if (nfs4_setup_sequence(data->o_arg.server,
1649                                 &data->o_arg.seq_args,
1650                                 &data->o_res.seq_res,
1651                                 task) != 0)
1652                 nfs_release_seqid(data->o_arg.seqid);
1653         else
1654                 rpc_call_start(task);
1655         return;
1656 unlock_no_action:
1657         rcu_read_unlock();
1658 out_no_action:
1659         task->tk_action = NULL;
1660
1661 }
1662
1663 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1664 {
1665         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1666         nfs4_open_prepare(task, calldata);
1667 }
1668
1669 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1670 {
1671         struct nfs4_opendata *data = calldata;
1672
1673         data->rpc_status = task->tk_status;
1674
1675         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1676                 return;
1677
1678         if (task->tk_status == 0) {
1679                 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680                         switch (data->o_res.f_attr->mode & S_IFMT) {
1681                         case S_IFREG:
1682                                 break;
1683                         case S_IFLNK:
1684                                 data->rpc_status = -ELOOP;
1685                                 break;
1686                         case S_IFDIR:
1687                                 data->rpc_status = -EISDIR;
1688                                 break;
1689                         default:
1690                                 data->rpc_status = -ENOTDIR;
1691                         }
1692                 }
1693                 renew_lease(data->o_res.server, data->timestamp);
1694                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1696         }
1697         data->rpc_done = 1;
1698 }
1699
1700 static void nfs4_open_release(void *calldata)
1701 {
1702         struct nfs4_opendata *data = calldata;
1703         struct nfs4_state *state = NULL;
1704
1705         /* If this request hasn't been cancelled, do nothing */
1706         if (data->cancelled == 0)
1707                 goto out_free;
1708         /* In case of error, no cleanup! */
1709         if (data->rpc_status != 0 || !data->rpc_done)
1710                 goto out_free;
1711         /* In case we need an open_confirm, no cleanup! */
1712         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1713                 goto out_free;
1714         state = nfs4_opendata_to_nfs4_state(data);
1715         if (!IS_ERR(state))
1716                 nfs4_close_state(state, data->o_arg.fmode);
1717 out_free:
1718         nfs4_opendata_put(data);
1719 }
1720
1721 static const struct rpc_call_ops nfs4_open_ops = {
1722         .rpc_call_prepare = nfs4_open_prepare,
1723         .rpc_call_done = nfs4_open_done,
1724         .rpc_release = nfs4_open_release,
1725 };
1726
1727 static const struct rpc_call_ops nfs4_recover_open_ops = {
1728         .rpc_call_prepare = nfs4_recover_open_prepare,
1729         .rpc_call_done = nfs4_open_done,
1730         .rpc_release = nfs4_open_release,
1731 };
1732
1733 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1734 {
1735         struct inode *dir = data->dir->d_inode;
1736         struct nfs_server *server = NFS_SERVER(dir);
1737         struct nfs_openargs *o_arg = &data->o_arg;
1738         struct nfs_openres *o_res = &data->o_res;
1739         struct rpc_task *task;
1740         struct rpc_message msg = {
1741                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1742                 .rpc_argp = o_arg,
1743                 .rpc_resp = o_res,
1744                 .rpc_cred = data->owner->so_cred,
1745         };
1746         struct rpc_task_setup task_setup_data = {
1747                 .rpc_client = server->client,
1748                 .rpc_message = &msg,
1749                 .callback_ops = &nfs4_open_ops,
1750                 .callback_data = data,
1751                 .workqueue = nfsiod_workqueue,
1752                 .flags = RPC_TASK_ASYNC,
1753         };
1754         int status;
1755
1756         nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1757         kref_get(&data->kref);
1758         data->rpc_done = 0;
1759         data->rpc_status = 0;
1760         data->cancelled = 0;
1761         if (isrecover)
1762                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1763         task = rpc_run_task(&task_setup_data);
1764         if (IS_ERR(task))
1765                 return PTR_ERR(task);
1766         status = nfs4_wait_for_completion_rpc_task(task);
1767         if (status != 0) {
1768                 data->cancelled = 1;
1769                 smp_wmb();
1770         } else
1771                 status = data->rpc_status;
1772         rpc_put_task(task);
1773
1774         return status;
1775 }
1776
1777 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1778 {
1779         struct inode *dir = data->dir->d_inode;
1780         struct nfs_openres *o_res = &data->o_res;
1781         int status;
1782
1783         status = nfs4_run_open_task(data, 1);
1784         if (status != 0 || !data->rpc_done)
1785                 return status;
1786
1787         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1788
1789         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1790                 status = _nfs4_proc_open_confirm(data);
1791                 if (status != 0)
1792                         return status;
1793         }
1794
1795         return status;
1796 }
1797
1798 static int nfs4_opendata_access(struct rpc_cred *cred,
1799                                 struct nfs4_opendata *opendata,
1800                                 struct nfs4_state *state, fmode_t fmode)
1801 {
1802         struct nfs_access_entry cache;
1803         u32 mask;
1804
1805         /* access call failed or for some reason the server doesn't
1806          * support any access modes -- defer access call until later */
1807         if (opendata->o_res.access_supported == 0)
1808                 return 0;
1809
1810         mask = 0;
1811         /* don't check MAY_WRITE - a newly created file may not have
1812          * write mode bits, but POSIX allows the creating process to write */
1813         if (fmode & FMODE_READ)
1814                 mask |= MAY_READ;
1815         if (fmode & FMODE_EXEC)
1816                 mask |= MAY_EXEC;
1817
1818         cache.cred = cred;
1819         cache.jiffies = jiffies;
1820         nfs_access_set_mask(&cache, opendata->o_res.access_result);
1821         nfs_access_add_cache(state->inode, &cache);
1822
1823         if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1824                 return 0;
1825
1826         /* even though OPEN succeeded, access is denied. Close the file */
1827         nfs4_close_state(state, fmode);
1828         return -EACCES;
1829 }
1830
1831 /*
1832  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1833  */
1834 static int _nfs4_proc_open(struct nfs4_opendata *data)
1835 {
1836         struct inode *dir = data->dir->d_inode;
1837         struct nfs_server *server = NFS_SERVER(dir);
1838         struct nfs_openargs *o_arg = &data->o_arg;
1839         struct nfs_openres *o_res = &data->o_res;
1840         int status;
1841
1842         status = nfs4_run_open_task(data, 0);
1843         if (!data->rpc_done)
1844                 return status;
1845         if (status != 0) {
1846                 if (status == -NFS4ERR_BADNAME &&
1847                                 !(o_arg->open_flags & O_CREAT))
1848                         return -ENOENT;
1849                 return status;
1850         }
1851
1852         nfs_fattr_map_and_free_names(server, &data->f_attr);
1853
1854         if (o_arg->open_flags & O_CREAT)
1855                 update_changeattr(dir, &o_res->cinfo);
1856         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1857                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1858         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1859                 status = _nfs4_proc_open_confirm(data);
1860                 if (status != 0)
1861                         return status;
1862         }
1863         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1864                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1865         return 0;
1866 }
1867
1868 static int nfs4_recover_expired_lease(struct nfs_server *server)
1869 {
1870         return nfs4_client_recover_expired_lease(server->nfs_client);
1871 }
1872
1873 /*
1874  * OPEN_EXPIRED:
1875  *      reclaim state on the server after a network partition.
1876  *      Assumes caller holds the appropriate lock
1877  */
1878 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1879 {
1880         struct nfs4_opendata *opendata;
1881         int ret;
1882
1883         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1884         if (IS_ERR(opendata))
1885                 return PTR_ERR(opendata);
1886         ret = nfs4_open_recover(opendata, state);
1887         if (ret == -ESTALE)
1888                 d_drop(ctx->dentry);
1889         nfs4_opendata_put(opendata);
1890         return ret;
1891 }
1892
1893 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1894 {
1895         struct nfs_server *server = NFS_SERVER(state->inode);
1896         struct nfs4_exception exception = { };
1897         int err;
1898
1899         do {
1900                 err = _nfs4_open_expired(ctx, state);
1901                 switch (err) {
1902                 default:
1903                         goto out;
1904                 case -NFS4ERR_GRACE:
1905                 case -NFS4ERR_DELAY:
1906                         nfs4_handle_exception(server, err, &exception);
1907                         err = 0;
1908                 }
1909         } while (exception.retry);
1910 out:
1911         return err;
1912 }
1913
1914 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1915 {
1916         struct nfs_open_context *ctx;
1917         int ret;
1918
1919         ctx = nfs4_state_find_open_context(state);
1920         if (IS_ERR(ctx))
1921                 return PTR_ERR(ctx);
1922         ret = nfs4_do_open_expired(ctx, state);
1923         put_nfs_open_context(ctx);
1924         return ret;
1925 }
1926
1927 #if defined(CONFIG_NFS_V4_1)
1928 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1929 {
1930         struct nfs_server *server = NFS_SERVER(state->inode);
1931         nfs4_stateid *stateid = &state->stateid;
1932         int status;
1933
1934         /* If a state reset has been done, test_stateid is unneeded */
1935         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1936                 return;
1937
1938         status = nfs41_test_stateid(server, stateid);
1939         if (status != NFS_OK) {
1940                 /* Free the stateid unless the server explicitly
1941                  * informs us the stateid is unrecognized. */
1942                 if (status != -NFS4ERR_BAD_STATEID)
1943                         nfs41_free_stateid(server, stateid);
1944                 nfs_remove_bad_delegation(state->inode);
1945
1946                 write_seqlock(&state->seqlock);
1947                 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1948                 write_sequnlock(&state->seqlock);
1949                 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1950         }
1951 }
1952
1953 /**
1954  * nfs41_check_open_stateid - possibly free an open stateid
1955  *
1956  * @state: NFSv4 state for an inode
1957  *
1958  * Returns NFS_OK if recovery for this stateid is now finished.
1959  * Otherwise a negative NFS4ERR value is returned.
1960  */
1961 static int nfs41_check_open_stateid(struct nfs4_state *state)
1962 {
1963         struct nfs_server *server = NFS_SERVER(state->inode);
1964         nfs4_stateid *stateid = &state->open_stateid;
1965         int status;
1966
1967         /* If a state reset has been done, test_stateid is unneeded */
1968         if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1969             (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1970             (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1971                 return -NFS4ERR_BAD_STATEID;
1972
1973         status = nfs41_test_stateid(server, stateid);
1974         if (status != NFS_OK) {
1975                 /* Free the stateid unless the server explicitly
1976                  * informs us the stateid is unrecognized. */
1977                 if (status != -NFS4ERR_BAD_STATEID)
1978                         nfs41_free_stateid(server, stateid);
1979
1980                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1981                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1982                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1983         }
1984         return status;
1985 }
1986
1987 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1988 {
1989         int status;
1990
1991         nfs41_clear_delegation_stateid(state);
1992         status = nfs41_check_open_stateid(state);
1993         if (status != NFS_OK)
1994                 status = nfs4_open_expired(sp, state);
1995         return status;
1996 }
1997 #endif
1998
1999 /*
2000  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2001  * fields corresponding to attributes that were used to store the verifier.
2002  * Make sure we clobber those fields in the later setattr call
2003  */
2004 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2005 {
2006         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2007             !(sattr->ia_valid & ATTR_ATIME_SET))
2008                 sattr->ia_valid |= ATTR_ATIME;
2009
2010         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2011             !(sattr->ia_valid & ATTR_MTIME_SET))
2012                 sattr->ia_valid |= ATTR_MTIME;
2013 }
2014
2015 /*
2016  * Returns a referenced nfs4_state
2017  */
2018 static int _nfs4_do_open(struct inode *dir,
2019                         struct dentry *dentry,
2020                         fmode_t fmode,
2021                         int flags,
2022                         struct iattr *sattr,
2023                         struct rpc_cred *cred,
2024                         struct nfs4_state **res,
2025                         struct nfs4_threshold **ctx_th)
2026 {
2027         struct nfs4_state_owner  *sp;
2028         struct nfs4_state     *state = NULL;
2029         struct nfs_server       *server = NFS_SERVER(dir);
2030         struct nfs4_opendata *opendata;
2031         int status;
2032
2033         /* Protect against reboot recovery conflicts */
2034         status = -ENOMEM;
2035         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2036         if (sp == NULL) {
2037                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2038                 goto out_err;
2039         }
2040         status = nfs4_recover_expired_lease(server);
2041         if (status != 0)
2042                 goto err_put_state_owner;
2043         if (dentry->d_inode != NULL)
2044                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2045         status = -ENOMEM;
2046         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
2047         if (opendata == NULL)
2048                 goto err_put_state_owner;
2049
2050         if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2051                 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2052                 if (!opendata->f_attr.mdsthreshold)
2053                         goto err_opendata_put;
2054                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2055         }
2056         if (dentry->d_inode != NULL)
2057                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2058
2059         status = _nfs4_proc_open(opendata);
2060         if (status != 0)
2061                 goto err_opendata_put;
2062
2063         state = nfs4_opendata_to_nfs4_state(opendata);
2064         status = PTR_ERR(state);
2065         if (IS_ERR(state))
2066                 goto err_opendata_put;
2067         if (server->caps & NFS_CAP_POSIX_LOCK)
2068                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2069
2070         status = nfs4_opendata_access(cred, opendata, state, fmode);
2071         if (status != 0)
2072                 goto err_opendata_put;
2073
2074         if (opendata->o_arg.open_flags & O_EXCL) {
2075                 nfs4_exclusive_attrset(opendata, sattr);
2076
2077                 nfs_fattr_init(opendata->o_res.f_attr);
2078                 status = nfs4_do_setattr(state->inode, cred,
2079                                 opendata->o_res.f_attr, sattr,
2080                                 state);
2081                 if (status == 0)
2082                         nfs_setattr_update_inode(state->inode, sattr);
2083                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2084         }
2085
2086         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2087                 *ctx_th = opendata->f_attr.mdsthreshold;
2088         else
2089                 kfree(opendata->f_attr.mdsthreshold);
2090         opendata->f_attr.mdsthreshold = NULL;
2091
2092         nfs4_opendata_put(opendata);
2093         nfs4_put_state_owner(sp);
2094         *res = state;
2095         return 0;
2096 err_opendata_put:
2097         kfree(opendata->f_attr.mdsthreshold);
2098         nfs4_opendata_put(opendata);
2099 err_put_state_owner:
2100         nfs4_put_state_owner(sp);
2101 out_err:
2102         *res = NULL;
2103         return status;
2104 }
2105
2106
2107 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2108                                         struct dentry *dentry,
2109                                         fmode_t fmode,
2110                                         int flags,
2111                                         struct iattr *sattr,
2112                                         struct rpc_cred *cred,
2113                                         struct nfs4_threshold **ctx_th)
2114 {
2115         struct nfs4_exception exception = { };
2116         struct nfs4_state *res;
2117         int status;
2118
2119         fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2120         do {
2121                 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2122                                        &res, ctx_th);
2123                 if (status == 0)
2124                         break;
2125                 /* NOTE: BAD_SEQID means the server and client disagree about the
2126                  * book-keeping w.r.t. state-changing operations
2127                  * (OPEN/CLOSE/LOCK/LOCKU...)
2128                  * It is actually a sign of a bug on the client or on the server.
2129                  *
2130                  * If we receive a BAD_SEQID error in the particular case of
2131                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
2132                  * have unhashed the old state_owner for us, and that we can
2133                  * therefore safely retry using a new one. We should still warn
2134                  * the user though...
2135                  */
2136                 if (status == -NFS4ERR_BAD_SEQID) {
2137                         pr_warn_ratelimited("NFS: v4 server %s "
2138                                         " returned a bad sequence-id error!\n",
2139                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
2140                         exception.retry = 1;
2141                         continue;
2142                 }
2143                 /*
2144                  * BAD_STATEID on OPEN means that the server cancelled our
2145                  * state before it received the OPEN_CONFIRM.
2146                  * Recover by retrying the request as per the discussion
2147                  * on Page 181 of RFC3530.
2148                  */
2149                 if (status == -NFS4ERR_BAD_STATEID) {
2150                         exception.retry = 1;
2151                         continue;
2152                 }
2153                 if (status == -EAGAIN) {
2154                         /* We must have found a delegation */
2155                         exception.retry = 1;
2156                         continue;
2157                 }
2158                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2159                                         status, &exception));
2160         } while (exception.retry);
2161         return res;
2162 }
2163
2164 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2165                             struct nfs_fattr *fattr, struct iattr *sattr,
2166                             struct nfs4_state *state)
2167 {
2168         struct nfs_server *server = NFS_SERVER(inode);
2169         struct nfs_setattrargs  arg = {
2170                 .fh             = NFS_FH(inode),
2171                 .iap            = sattr,
2172                 .server         = server,
2173                 .bitmask = server->attr_bitmask,
2174         };
2175         struct nfs_setattrres  res = {
2176                 .fattr          = fattr,
2177                 .server         = server,
2178         };
2179         struct rpc_message msg = {
2180                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2181                 .rpc_argp       = &arg,
2182                 .rpc_resp       = &res,
2183                 .rpc_cred       = cred,
2184         };
2185         unsigned long timestamp = jiffies;
2186         int status;
2187
2188         nfs_fattr_init(fattr);
2189
2190         if (state != NULL) {
2191                 struct nfs_lockowner lockowner = {
2192                         .l_owner = current->files,
2193                         .l_pid = current->tgid,
2194                 };
2195                 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2196                                 &lockowner);
2197         } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2198                                 FMODE_WRITE)) {
2199                 /* Use that stateid */
2200         } else
2201                 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2202
2203         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2204         if (status == 0 && state != NULL)
2205                 renew_lease(server, timestamp);
2206         return status;
2207 }
2208
2209 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2210                            struct nfs_fattr *fattr, struct iattr *sattr,
2211                            struct nfs4_state *state)
2212 {
2213         struct nfs_server *server = NFS_SERVER(inode);
2214         struct nfs4_exception exception = {
2215                 .state = state,
2216                 .inode = inode,
2217         };
2218         int err;
2219         do {
2220                 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2221                 switch (err) {
2222                 case -NFS4ERR_OPENMODE:
2223                         if (state && !(state->state & FMODE_WRITE)) {
2224                                 err = -EBADF;
2225                                 if (sattr->ia_valid & ATTR_OPEN)
2226                                         err = -EACCES;
2227                                 goto out;
2228                         }
2229                 }
2230                 err = nfs4_handle_exception(server, err, &exception);
2231         } while (exception.retry);
2232 out:
2233         return err;
2234 }
2235
2236 struct nfs4_closedata {
2237         struct inode *inode;
2238         struct nfs4_state *state;
2239         struct nfs_closeargs arg;
2240         struct nfs_closeres res;
2241         struct nfs_fattr fattr;
2242         unsigned long timestamp;
2243         bool roc;
2244         u32 roc_barrier;
2245 };
2246
2247 static void nfs4_free_closedata(void *data)
2248 {
2249         struct nfs4_closedata *calldata = data;
2250         struct nfs4_state_owner *sp = calldata->state->owner;
2251         struct super_block *sb = calldata->state->inode->i_sb;
2252
2253         if (calldata->roc)
2254                 pnfs_roc_release(calldata->state->inode);
2255         nfs4_put_open_state(calldata->state);
2256         nfs_free_seqid(calldata->arg.seqid);
2257         nfs4_put_state_owner(sp);
2258         nfs_sb_deactive_async(sb);
2259         kfree(calldata);
2260 }
2261
2262 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2263                 fmode_t fmode)
2264 {
2265         spin_lock(&state->owner->so_lock);
2266         if (!(fmode & FMODE_READ))
2267                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2268         if (!(fmode & FMODE_WRITE))
2269                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2270         clear_bit(NFS_O_RDWR_STATE, &state->flags);
2271         spin_unlock(&state->owner->so_lock);
2272 }
2273
2274 static void nfs4_close_done(struct rpc_task *task, void *data)
2275 {
2276         struct nfs4_closedata *calldata = data;
2277         struct nfs4_state *state = calldata->state;
2278         struct nfs_server *server = NFS_SERVER(calldata->inode);
2279
2280         dprintk("%s: begin!\n", __func__);
2281         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2282                 return;
2283         /* hmm. we are done with the inode, and in the process of freeing
2284          * the state_owner. we keep this around to process errors
2285          */
2286         switch (task->tk_status) {
2287                 case 0:
2288                         if (calldata->roc)
2289                                 pnfs_roc_set_barrier(state->inode,
2290                                                      calldata->roc_barrier);
2291                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2292                         renew_lease(server, calldata->timestamp);
2293                         nfs4_close_clear_stateid_flags(state,
2294                                         calldata->arg.fmode);
2295                         break;
2296                 case -NFS4ERR_STALE_STATEID:
2297                 case -NFS4ERR_OLD_STATEID:
2298                 case -NFS4ERR_BAD_STATEID:
2299                 case -NFS4ERR_EXPIRED:
2300                         if (calldata->arg.fmode == 0)
2301                                 break;
2302                 default:
2303                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2304                                 rpc_restart_call_prepare(task);
2305         }
2306         nfs_release_seqid(calldata->arg.seqid);
2307         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2308         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2309 }
2310
2311 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2312 {
2313         struct nfs4_closedata *calldata = data;
2314         struct nfs4_state *state = calldata->state;
2315         struct inode *inode = calldata->inode;
2316         int call_close = 0;
2317
2318         dprintk("%s: begin!\n", __func__);
2319         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2320                 return;
2321
2322         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2323         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2324         spin_lock(&state->owner->so_lock);
2325         /* Calculate the change in open mode */
2326         if (state->n_rdwr == 0) {
2327                 if (state->n_rdonly == 0) {
2328                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2329                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2330                         calldata->arg.fmode &= ~FMODE_READ;
2331                 }
2332                 if (state->n_wronly == 0) {
2333                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2334                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2335                         calldata->arg.fmode &= ~FMODE_WRITE;
2336                 }
2337         }
2338         spin_unlock(&state->owner->so_lock);
2339
2340         if (!call_close) {
2341                 /* Note: exit _without_ calling nfs4_close_done */
2342                 task->tk_action = NULL;
2343                 goto out;
2344         }
2345
2346         if (calldata->arg.fmode == 0) {
2347                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2348                 if (calldata->roc &&
2349                     pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2350                         goto out;
2351         }
2352
2353         nfs_fattr_init(calldata->res.fattr);
2354         calldata->timestamp = jiffies;
2355         if (nfs4_setup_sequence(NFS_SERVER(inode),
2356                                 &calldata->arg.seq_args,
2357                                 &calldata->res.seq_res,
2358                                 task) != 0)
2359                 nfs_release_seqid(calldata->arg.seqid);
2360         else
2361                 rpc_call_start(task);
2362 out:
2363         dprintk("%s: done!\n", __func__);
2364 }
2365
2366 static const struct rpc_call_ops nfs4_close_ops = {
2367         .rpc_call_prepare = nfs4_close_prepare,
2368         .rpc_call_done = nfs4_close_done,
2369         .rpc_release = nfs4_free_closedata,
2370 };
2371
2372 /* 
2373  * It is possible for data to be read/written from a mem-mapped file 
2374  * after the sys_close call (which hits the vfs layer as a flush).
2375  * This means that we can't safely call nfsv4 close on a file until 
2376  * the inode is cleared. This in turn means that we are not good
2377  * NFSv4 citizens - we do not indicate to the server to update the file's 
2378  * share state even when we are done with one of the three share 
2379  * stateid's in the inode.
2380  *
2381  * NOTE: Caller must be holding the sp->so_owner semaphore!
2382  */
2383 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2384 {
2385         struct nfs_server *server = NFS_SERVER(state->inode);
2386         struct nfs4_closedata *calldata;
2387         struct nfs4_state_owner *sp = state->owner;
2388         struct rpc_task *task;
2389         struct rpc_message msg = {
2390                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2391                 .rpc_cred = state->owner->so_cred,
2392         };
2393         struct rpc_task_setup task_setup_data = {
2394                 .rpc_client = server->client,
2395                 .rpc_message = &msg,
2396                 .callback_ops = &nfs4_close_ops,
2397                 .workqueue = nfsiod_workqueue,
2398                 .flags = RPC_TASK_ASYNC,
2399         };
2400         int status = -ENOMEM;
2401
2402         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2403         if (calldata == NULL)
2404                 goto out;
2405         nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2406         calldata->inode = state->inode;
2407         calldata->state = state;
2408         calldata->arg.fh = NFS_FH(state->inode);
2409         calldata->arg.stateid = &state->open_stateid;
2410         /* Serialization for the sequence id */
2411         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2412         if (calldata->arg.seqid == NULL)
2413                 goto out_free_calldata;
2414         calldata->arg.fmode = 0;
2415         calldata->arg.bitmask = server->cache_consistency_bitmask;
2416         calldata->res.fattr = &calldata->fattr;
2417         calldata->res.seqid = calldata->arg.seqid;
2418         calldata->res.server = server;
2419         calldata->roc = pnfs_roc(state->inode);
2420         nfs_sb_active(calldata->inode->i_sb);
2421
2422         msg.rpc_argp = &calldata->arg;
2423         msg.rpc_resp = &calldata->res;
2424         task_setup_data.callback_data = calldata;
2425         task = rpc_run_task(&task_setup_data);
2426         if (IS_ERR(task))
2427                 return PTR_ERR(task);
2428         status = 0;
2429         if (wait)
2430                 status = rpc_wait_for_completion_task(task);
2431         rpc_put_task(task);
2432         return status;
2433 out_free_calldata:
2434         kfree(calldata);
2435 out:
2436         nfs4_put_open_state(state);
2437         nfs4_put_state_owner(sp);
2438         return status;
2439 }
2440
2441 static struct inode *
2442 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2443 {
2444         struct nfs4_state *state;
2445
2446         /* Protect against concurrent sillydeletes */
2447         state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2448                              ctx->cred, &ctx->mdsthreshold);
2449         if (IS_ERR(state))
2450                 return ERR_CAST(state);
2451         ctx->state = state;
2452         return igrab(state->inode);
2453 }
2454
2455 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2456 {
2457         if (ctx->state == NULL)
2458                 return;
2459         if (is_sync)
2460                 nfs4_close_sync(ctx->state, ctx->mode);
2461         else
2462                 nfs4_close_state(ctx->state, ctx->mode);
2463 }
2464
2465 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2466 {
2467         struct nfs4_server_caps_arg args = {
2468                 .fhandle = fhandle,
2469         };
2470         struct nfs4_server_caps_res res = {};
2471         struct rpc_message msg = {
2472                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2473                 .rpc_argp = &args,
2474                 .rpc_resp = &res,
2475         };
2476         int status;
2477
2478         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2479         if (status == 0) {
2480                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2481                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2482                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2483                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2484                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2485                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2486                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2487                         server->caps |= NFS_CAP_ACLS;
2488                 if (res.has_links != 0)
2489                         server->caps |= NFS_CAP_HARDLINKS;
2490                 if (res.has_symlinks != 0)
2491                         server->caps |= NFS_CAP_SYMLINKS;
2492                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2493                         server->caps |= NFS_CAP_FILEID;
2494                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2495                         server->caps |= NFS_CAP_MODE;
2496                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2497                         server->caps |= NFS_CAP_NLINK;
2498                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2499                         server->caps |= NFS_CAP_OWNER;
2500                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2501                         server->caps |= NFS_CAP_OWNER_GROUP;
2502                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2503                         server->caps |= NFS_CAP_ATIME;
2504                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2505                         server->caps |= NFS_CAP_CTIME;
2506                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2507                         server->caps |= NFS_CAP_MTIME;
2508
2509                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2510                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2511                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2512                 server->acl_bitmask = res.acl_bitmask;
2513                 server->fh_expire_type = res.fh_expire_type;
2514         }
2515
2516         return status;
2517 }
2518
2519 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2520 {
2521         struct nfs4_exception exception = { };
2522         int err;
2523         do {
2524                 err = nfs4_handle_exception(server,
2525                                 _nfs4_server_capabilities(server, fhandle),
2526                                 &exception);
2527         } while (exception.retry);
2528         return err;
2529 }
2530
2531 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2532                 struct nfs_fsinfo *info)
2533 {
2534         struct nfs4_lookup_root_arg args = {
2535                 .bitmask = nfs4_fattr_bitmap,
2536         };
2537         struct nfs4_lookup_res res = {
2538                 .server = server,
2539                 .fattr = info->fattr,
2540                 .fh = fhandle,
2541         };
2542         struct rpc_message msg = {
2543                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2544                 .rpc_argp = &args,
2545                 .rpc_resp = &res,
2546         };
2547
2548         nfs_fattr_init(info->fattr);
2549         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2550 }
2551
2552 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2553                 struct nfs_fsinfo *info)
2554 {
2555         struct nfs4_exception exception = { };
2556         int err;
2557         do {
2558                 err = _nfs4_lookup_root(server, fhandle, info);
2559                 switch (err) {
2560                 case 0:
2561                 case -NFS4ERR_WRONGSEC:
2562                         goto out;
2563                 default:
2564                         err = nfs4_handle_exception(server, err, &exception);
2565                 }
2566         } while (exception.retry);
2567 out:
2568         return err;
2569 }
2570
2571 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2572                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2573 {
2574         struct rpc_auth *auth;
2575         int ret;
2576
2577         auth = rpcauth_create(flavor, server->client);
2578         if (IS_ERR(auth)) {
2579                 ret = -EIO;
2580                 goto out;
2581         }
2582         ret = nfs4_lookup_root(server, fhandle, info);
2583 out:
2584         return ret;
2585 }
2586
2587 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2588                               struct nfs_fsinfo *info)
2589 {
2590         int i, len, status = 0;
2591         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2592
2593         len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2594         if (len < 0)
2595                 return len;
2596
2597         for (i = 0; i < len; i++) {
2598                 /* AUTH_UNIX is the default flavor if none was specified,
2599                  * thus has already been tried. */
2600                 if (flav_array[i] == RPC_AUTH_UNIX)
2601                         continue;
2602
2603                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2604                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2605                         continue;
2606                 break;
2607         }
2608         /*
2609          * -EACCESS could mean that the user doesn't have correct permissions
2610          * to access the mount.  It could also mean that we tried to mount
2611          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2612          * existing mount programs don't handle -EACCES very well so it should
2613          * be mapped to -EPERM instead.
2614          */
2615         if (status == -EACCES)
2616                 status = -EPERM;
2617         return status;
2618 }
2619
2620 /*
2621  * get the file handle for the "/" directory on the server
2622  */
2623 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2624                          struct nfs_fsinfo *info)
2625 {
2626         int minor_version = server->nfs_client->cl_minorversion;
2627         int status = nfs4_lookup_root(server, fhandle, info);
2628         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2629                 /*
2630                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2631                  * by nfs4_map_errors() as this function exits.
2632                  */
2633                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2634         if (status == 0)
2635                 status = nfs4_server_capabilities(server, fhandle);
2636         if (status == 0)
2637                 status = nfs4_do_fsinfo(server, fhandle, info);
2638         return nfs4_map_errors(status);
2639 }
2640
2641 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2642                               struct nfs_fsinfo *info)
2643 {
2644         int error;
2645         struct nfs_fattr *fattr = info->fattr;
2646
2647         error = nfs4_server_capabilities(server, mntfh);
2648         if (error < 0) {
2649                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2650                 return error;
2651         }
2652
2653         error = nfs4_proc_getattr(server, mntfh, fattr);
2654         if (error < 0) {
2655                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2656                 return error;
2657         }
2658
2659         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2660             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2661                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2662
2663         return error;
2664 }
2665
2666 /*
2667  * Get locations and (maybe) other attributes of a referral.
2668  * Note that we'll actually follow the referral later when
2669  * we detect fsid mismatch in inode revalidation
2670  */
2671 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2672                              const struct qstr *name, struct nfs_fattr *fattr,
2673                              struct nfs_fh *fhandle)
2674 {
2675         int status = -ENOMEM;
2676         struct page *page = NULL;
2677         struct nfs4_fs_locations *locations = NULL;
2678
2679         page = alloc_page(GFP_KERNEL);
2680         if (page == NULL)
2681                 goto out;
2682         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2683         if (locations == NULL)
2684                 goto out;
2685
2686         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2687         if (status != 0)
2688                 goto out;
2689         /* Make sure server returned a different fsid for the referral */
2690         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2691                 dprintk("%s: server did not return a different fsid for"
2692                         " a referral at %s\n", __func__, name->name);
2693                 status = -EIO;
2694                 goto out;
2695         }
2696         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2697         nfs_fixup_referral_attributes(&locations->fattr);
2698
2699         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2700         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2701         memset(fhandle, 0, sizeof(struct nfs_fh));
2702 out:
2703         if (page)
2704                 __free_page(page);
2705         kfree(locations);
2706         return status;
2707 }
2708
2709 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2710 {
2711         struct nfs4_getattr_arg args = {
2712                 .fh = fhandle,
2713                 .bitmask = server->attr_bitmask,
2714         };
2715         struct nfs4_getattr_res res = {
2716                 .fattr = fattr,
2717                 .server = server,
2718         };
2719         struct rpc_message msg = {
2720                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2721                 .rpc_argp = &args,
2722                 .rpc_resp = &res,
2723         };
2724         
2725         nfs_fattr_init(fattr);
2726         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2727 }
2728
2729 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2730 {
2731         struct nfs4_exception exception = { };
2732         int err;
2733         do {
2734                 err = nfs4_handle_exception(server,
2735                                 _nfs4_proc_getattr(server, fhandle, fattr),
2736                                 &exception);
2737         } while (exception.retry);
2738         return err;
2739 }
2740
2741 /* 
2742  * The file is not closed if it is opened due to the a request to change
2743  * the size of the file. The open call will not be needed once the
2744  * VFS layer lookup-intents are implemented.
2745  *
2746  * Close is called when the inode is destroyed.
2747  * If we haven't opened the file for O_WRONLY, we
2748  * need to in the size_change case to obtain a stateid.
2749  *
2750  * Got race?
2751  * Because OPEN is always done by name in nfsv4, it is
2752  * possible that we opened a different file by the same
2753  * name.  We can recognize this race condition, but we
2754  * can't do anything about it besides returning an error.
2755  *
2756  * This will be fixed with VFS changes (lookup-intent).
2757  */
2758 static int
2759 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2760                   struct iattr *sattr)
2761 {
2762         struct inode *inode = dentry->d_inode;
2763         struct rpc_cred *cred = NULL;
2764         struct nfs4_state *state = NULL;
2765         int status;
2766
2767         if (pnfs_ld_layoutret_on_setattr(inode))
2768                 pnfs_return_layout(inode);
2769
2770         nfs_fattr_init(fattr);
2771         
2772         /* Deal with open(O_TRUNC) */
2773         if (sattr->ia_valid & ATTR_OPEN)
2774                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2775
2776         /* Optimization: if the end result is no change, don't RPC */
2777         if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2778                 return 0;
2779
2780         /* Search for an existing open(O_WRITE) file */
2781         if (sattr->ia_valid & ATTR_FILE) {
2782                 struct nfs_open_context *ctx;
2783
2784                 ctx = nfs_file_open_context(sattr->ia_file);
2785                 if (ctx) {
2786                         cred = ctx->cred;
2787                         state = ctx->state;
2788                 }
2789         }
2790
2791         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2792         if (status == 0)
2793                 nfs_setattr_update_inode(inode, sattr);
2794         return status;
2795 }
2796
2797 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2798                 const struct qstr *name, struct nfs_fh *fhandle,
2799                 struct nfs_fattr *fattr)
2800 {
2801         struct nfs_server *server = NFS_SERVER(dir);
2802         int                    status;
2803         struct nfs4_lookup_arg args = {
2804                 .bitmask = server->attr_bitmask,
2805                 .dir_fh = NFS_FH(dir),
2806                 .name = name,
2807         };
2808         struct nfs4_lookup_res res = {
2809                 .server = server,
2810                 .fattr = fattr,
2811                 .fh = fhandle,
2812         };
2813         struct rpc_message msg = {
2814                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2815                 .rpc_argp = &args,
2816                 .rpc_resp = &res,
2817         };
2818
2819         nfs_fattr_init(fattr);
2820
2821         dprintk("NFS call  lookup %s\n", name->name);
2822         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2823         dprintk("NFS reply lookup: %d\n", status);
2824         return status;
2825 }
2826
2827 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2828 {
2829         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2830                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2831         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2832         fattr->nlink = 2;
2833 }
2834
2835 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2836                                    struct qstr *name, struct nfs_fh *fhandle,
2837                                    struct nfs_fattr *fattr)
2838 {
2839         struct nfs4_exception exception = { };
2840         struct rpc_clnt *client = *clnt;
2841         int err;
2842         do {
2843                 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2844                 switch (err) {
2845                 case -NFS4ERR_BADNAME:
2846                         err = -ENOENT;
2847                         goto out;
2848                 case -NFS4ERR_MOVED:
2849                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2850                         goto out;
2851                 case -NFS4ERR_WRONGSEC:
2852                         err = -EPERM;
2853                         if (client != *clnt)
2854                                 goto out;
2855
2856                         client = nfs4_create_sec_client(client, dir, name);
2857                         if (IS_ERR(client))
2858                                 return PTR_ERR(client);
2859
2860                         exception.retry = 1;
2861                         break;
2862                 default:
2863                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2864                 }
2865         } while (exception.retry);
2866
2867 out:
2868         if (err == 0)
2869                 *clnt = client;
2870         else if (client != *clnt)
2871                 rpc_shutdown_client(client);
2872
2873         return err;
2874 }
2875
2876 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2877                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2878 {
2879         int status;
2880         struct rpc_clnt *client = NFS_CLIENT(dir);
2881
2882         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2883         if (client != NFS_CLIENT(dir)) {
2884                 rpc_shutdown_client(client);
2885                 nfs_fixup_secinfo_attributes(fattr);
2886         }
2887         return status;
2888 }
2889
2890 struct rpc_clnt *
2891 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2892                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2893 {
2894         int status;
2895         struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2896
2897         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2898         if (status < 0) {
2899                 rpc_shutdown_client(client);
2900                 return ERR_PTR(status);
2901         }
2902         return client;
2903 }
2904
2905 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2906 {
2907         struct nfs_server *server = NFS_SERVER(inode);
2908         struct nfs4_accessargs args = {
2909                 .fh = NFS_FH(inode),
2910                 .bitmask = server->cache_consistency_bitmask,
2911         };
2912         struct nfs4_accessres res = {
2913                 .server = server,
2914         };
2915         struct rpc_message msg = {
2916                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2917                 .rpc_argp = &args,
2918                 .rpc_resp = &res,
2919                 .rpc_cred = entry->cred,
2920         };
2921         int mode = entry->mask;
2922         int status;
2923
2924         /*
2925          * Determine which access bits we want to ask for...
2926          */
2927         if (mode & MAY_READ)
2928                 args.access |= NFS4_ACCESS_READ;
2929         if (S_ISDIR(inode->i_mode)) {
2930                 if (mode & MAY_WRITE)
2931                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2932                 if (mode & MAY_EXEC)
2933                         args.access |= NFS4_ACCESS_LOOKUP;
2934         } else {
2935                 if (mode & MAY_WRITE)
2936                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2937                 if (mode & MAY_EXEC)
2938                         args.access |= NFS4_ACCESS_EXECUTE;
2939         }
2940
2941         res.fattr = nfs_alloc_fattr();
2942         if (res.fattr == NULL)
2943                 return -ENOMEM;
2944
2945         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2946         if (!status) {
2947                 nfs_access_set_mask(entry, res.access);
2948                 nfs_refresh_inode(inode, res.fattr);
2949         }
2950         nfs_free_fattr(res.fattr);
2951         return status;
2952 }
2953
2954 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2955 {
2956         struct nfs4_exception exception = { };
2957         int err;
2958         do {
2959                 err = nfs4_handle_exception(NFS_SERVER(inode),
2960                                 _nfs4_proc_access(inode, entry),
2961                                 &exception);
2962         } while (exception.retry);
2963         return err;
2964 }
2965
2966 /*
2967  * TODO: For the time being, we don't try to get any attributes
2968  * along with any of the zero-copy operations READ, READDIR,
2969  * READLINK, WRITE.
2970  *
2971  * In the case of the first three, we want to put the GETATTR
2972  * after the read-type operation -- this is because it is hard
2973  * to predict the length of a GETATTR response in v4, and thus
2974  * align the READ data correctly.  This means that the GETATTR
2975  * may end up partially falling into the page cache, and we should
2976  * shift it into the 'tail' of the xdr_buf before processing.
2977  * To do this efficiently, we need to know the total length
2978  * of data received, which doesn't seem to be available outside
2979  * of the RPC layer.
2980  *
2981  * In the case of WRITE, we also want to put the GETATTR after
2982  * the operation -- in this case because we want to make sure
2983  * we get the post-operation mtime and size.
2984  *
2985  * Both of these changes to the XDR layer would in fact be quite
2986  * minor, but I decided to leave them for a subsequent patch.
2987  */
2988 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2989                 unsigned int pgbase, unsigned int pglen)
2990 {
2991         struct nfs4_readlink args = {
2992                 .fh       = NFS_FH(inode),
2993                 .pgbase   = pgbase,
2994                 .pglen    = pglen,
2995                 .pages    = &page,
2996         };
2997         struct nfs4_readlink_res res;
2998         struct rpc_message msg = {
2999                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3000                 .rpc_argp = &args,
3001                 .rpc_resp = &res,
3002         };
3003
3004         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3005 }
3006
3007 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3008                 unsigned int pgbase, unsigned int pglen)
3009 {
3010         struct nfs4_exception exception = { };
3011         int err;
3012         do {
3013                 err = nfs4_handle_exception(NFS_SERVER(inode),
3014                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3015                                 &exception);
3016         } while (exception.retry);
3017         return err;
3018 }
3019
3020 /*
3021  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
3022  */
3023 static int
3024 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3025                  int flags)
3026 {
3027         struct nfs_open_context *ctx;
3028         struct nfs4_state *state;
3029         int status = 0;
3030
3031         ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3032         if (IS_ERR(ctx))
3033                 return PTR_ERR(ctx);
3034
3035         sattr->ia_mode &= ~current_umask();
3036         state = nfs4_do_open(dir, dentry, ctx->mode,
3037                         flags, sattr, ctx->cred,
3038                         &ctx->mdsthreshold);
3039         d_drop(dentry);
3040         if (IS_ERR(state)) {
3041                 status = PTR_ERR(state);
3042                 goto out;
3043         }
3044         d_add(dentry, igrab(state->inode));
3045         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
3046         ctx->state = state;
3047 out:
3048         put_nfs_open_context(ctx);
3049         return status;
3050 }
3051
3052 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3053 {
3054         struct nfs_server *server = NFS_SERVER(dir);
3055         struct nfs_removeargs args = {
3056                 .fh = NFS_FH(dir),
3057                 .name = *name,
3058         };
3059         struct nfs_removeres res = {
3060                 .server = server,
3061         };
3062         struct rpc_message msg = {
3063                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3064                 .rpc_argp = &args,
3065                 .rpc_resp = &res,
3066         };
3067         int status;
3068
3069         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3070         if (status == 0)
3071                 update_changeattr(dir, &res.cinfo);
3072         return status;
3073 }
3074
3075 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3076 {
3077         struct nfs4_exception exception = { };
3078         int err;
3079         do {
3080                 err = nfs4_handle_exception(NFS_SERVER(dir),
3081                                 _nfs4_proc_remove(dir, name),
3082                                 &exception);
3083         } while (exception.retry);
3084         return err;
3085 }
3086
3087 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3088 {
3089         struct nfs_server *server = NFS_SERVER(dir);
3090         struct nfs_removeargs *args = msg->rpc_argp;
3091         struct nfs_removeres *res = msg->rpc_resp;
3092
3093         res->server = server;
3094         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3095         nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3096 }
3097
3098 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3099 {
3100         if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3101                                 &data->args.seq_args,
3102                                 &data->res.seq_res,
3103                                 task))
3104                 return;
3105         rpc_call_start(task);
3106 }
3107
3108 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3109 {
3110         struct nfs_removeres *res = task->tk_msg.rpc_resp;
3111
3112         if (!nfs4_sequence_done(task, &res->seq_res))
3113                 return 0;
3114         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3115                 return 0;
3116         update_changeattr(dir, &res->cinfo);
3117         return 1;
3118 }
3119
3120 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3121 {
3122         struct nfs_server *server = NFS_SERVER(dir);
3123         struct nfs_renameargs *arg = msg->rpc_argp;
3124         struct nfs_renameres *res = msg->rpc_resp;
3125
3126         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3127         res->server = server;
3128         nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3129 }
3130
3131 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3132 {
3133         if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3134                                 &data->args.seq_args,
3135                                 &data->res.seq_res,
3136                                 task))
3137                 return;
3138         rpc_call_start(task);
3139 }
3140
3141 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3142                                  struct inode *new_dir)
3143 {
3144         struct nfs_renameres *res = task->tk_msg.rpc_resp;
3145
3146         if (!nfs4_sequence_done(task, &res->seq_res))
3147                 return 0;
3148         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3149                 return 0;
3150
3151         update_changeattr(old_dir, &res->old_cinfo);
3152         update_changeattr(new_dir, &res->new_cinfo);
3153         return 1;
3154 }
3155
3156 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3157                 struct inode *new_dir, struct qstr *new_name)
3158 {
3159         struct nfs_server *server = NFS_SERVER(old_dir);
3160         struct nfs_renameargs arg = {
3161                 .old_dir = NFS_FH(old_dir),
3162                 .new_dir = NFS_FH(new_dir),
3163                 .old_name = old_name,
3164                 .new_name = new_name,
3165         };
3166         struct nfs_renameres res = {
3167                 .server = server,
3168         };
3169         struct rpc_message msg = {
3170                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3171                 .rpc_argp = &arg,
3172                 .rpc_resp = &res,
3173         };
3174         int status = -ENOMEM;
3175         
3176         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3177         if (!status) {
3178                 update_changeattr(old_dir, &res.old_cinfo);
3179                 update_changeattr(new_dir, &res.new_cinfo);
3180         }
3181         return status;
3182 }
3183
3184 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3185                 struct inode *new_dir, struct qstr *new_name)
3186 {
3187         struct nfs4_exception exception = { };
3188         int err;
3189         do {
3190                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3191                                 _nfs4_proc_rename(old_dir, old_name,
3192                                         new_dir, new_name),
3193                                 &exception);
3194         } while (exception.retry);
3195         return err;
3196 }
3197
3198 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3199 {
3200         struct nfs_server *server = NFS_SERVER(inode);
3201         struct nfs4_link_arg arg = {
3202                 .fh     = NFS_FH(inode),
3203                 .dir_fh = NFS_FH(dir),
3204                 .name   = name,
3205                 .bitmask = server->attr_bitmask,
3206         };
3207         struct nfs4_link_res res = {
3208                 .server = server,
3209         };
3210         struct rpc_message msg = {
3211                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3212                 .rpc_argp = &arg,
3213                 .rpc_resp = &res,
3214         };
3215         int status = -ENOMEM;
3216
3217         res.fattr = nfs_alloc_fattr();
3218         if (res.fattr == NULL)
3219                 goto out;
3220
3221         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3222         if (!status) {
3223                 update_changeattr(dir, &res.cinfo);
3224                 nfs_post_op_update_inode(inode, res.fattr);
3225         }
3226 out:
3227         nfs_free_fattr(res.fattr);
3228         return status;
3229 }
3230
3231 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3232 {
3233         struct nfs4_exception exception = { };
3234         int err;
3235         do {
3236                 err = nfs4_handle_exception(NFS_SERVER(inode),
3237                                 _nfs4_proc_link(inode, dir, name),
3238                                 &exception);
3239         } while (exception.retry);
3240         return err;
3241 }
3242
3243 struct nfs4_createdata {
3244         struct rpc_message msg;
3245         struct nfs4_create_arg arg;
3246         struct nfs4_create_res res;
3247         struct nfs_fh fh;
3248         struct nfs_fattr fattr;
3249 };
3250
3251 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3252                 struct qstr *name, struct iattr *sattr, u32 ftype)
3253 {
3254         struct nfs4_createdata *data;
3255
3256         data = kzalloc(sizeof(*data), GFP_KERNEL);
3257         if (data != NULL) {
3258                 struct nfs_server *server = NFS_SERVER(dir);
3259
3260                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3261                 data->msg.rpc_argp = &data->arg;
3262                 data->msg.rpc_resp = &data->res;
3263                 data->arg.dir_fh = NFS_FH(dir);
3264                 data->arg.server = server;
3265                 data->arg.name = name;
3266                 data->arg.attrs = sattr;
3267                 data->arg.ftype = ftype;
3268                 data->arg.bitmask = server->attr_bitmask;
3269                 data->res.server = server;
3270                 data->res.fh = &data->fh;
3271                 data->res.fattr = &data->fattr;
3272                 nfs_fattr_init(data->res.fattr);
3273         }
3274         return data;
3275 }
3276
3277 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3278 {
3279         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3280                                     &data->arg.seq_args, &data->res.seq_res, 1);
3281         if (status == 0) {
3282                 update_changeattr(dir, &data->res.dir_cinfo);
3283                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3284         }
3285         return status;
3286 }
3287
3288 static void nfs4_free_createdata(struct nfs4_createdata *data)
3289 {
3290         kfree(data);
3291 }
3292
3293 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3294                 struct page *page, unsigned int len, struct iattr *sattr)
3295 {
3296         struct nfs4_createdata *data;
3297         int status = -ENAMETOOLONG;
3298
3299         if (len > NFS4_MAXPATHLEN)
3300                 goto out;
3301
3302         status = -ENOMEM;
3303         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3304         if (data == NULL)
3305                 goto out;
3306
3307         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3308         data->arg.u.symlink.pages = &page;
3309         data->arg.u.symlink.len = len;
3310         
3311         status = nfs4_do_create(dir, dentry, data);
3312
3313         nfs4_free_createdata(data);
3314 out:
3315         return status;
3316 }
3317
3318 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3319                 struct page *page, unsigned int len, struct iattr *sattr)
3320 {
3321         struct nfs4_exception exception = { };
3322         int err;
3323         do {
3324                 err = nfs4_handle_exception(NFS_SERVER(dir),
3325                                 _nfs4_proc_symlink(dir, dentry, page,
3326                                                         len, sattr),
3327                                 &exception);
3328         } while (exception.retry);
3329         return err;
3330 }
3331
3332 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3333                 struct iattr *sattr)
3334 {
3335         struct nfs4_createdata *data;
3336         int status = -ENOMEM;
3337
3338         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3339         if (data == NULL)
3340                 goto out;
3341
3342         status = nfs4_do_create(dir, dentry, data);
3343
3344         nfs4_free_createdata(data);
3345 out:
3346         return status;
3347 }
3348
3349 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3350                 struct iattr *sattr)
3351 {
3352         struct nfs4_exception exception = { };
3353         int err;
3354
3355         sattr->ia_mode &= ~current_umask();
3356         do {
3357                 err = nfs4_handle_exception(NFS_SERVER(dir),
3358                                 _nfs4_proc_mkdir(dir, dentry, sattr),
3359                                 &exception);
3360         } while (exception.retry);
3361         return err;
3362 }
3363
3364 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3365                 u64 cookie, struct page **pages, unsigned int count, int plus)
3366 {
3367         struct inode            *dir = dentry->d_inode;
3368         struct nfs4_readdir_arg args = {
3369                 .fh = NFS_FH(dir),
3370                 .pages = pages,
3371                 .pgbase = 0,
3372                 .count = count,
3373                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3374                 .plus = plus,
3375         };
3376         struct nfs4_readdir_res res;
3377         struct rpc_message msg = {
3378                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3379                 .rpc_argp = &args,
3380                 .rpc_resp = &res,
3381                 .rpc_cred = cred,
3382         };
3383         int                     status;
3384
3385         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3386                         dentry->d_parent->d_name.name,
3387                         dentry->d_name.name,
3388                         (unsigned long long)cookie);
3389         nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3390         res.pgbase = args.pgbase;
3391         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3392         if (status >= 0) {
3393                 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3394                 status += args.pgbase;
3395         }
3396
3397         nfs_invalidate_atime(dir);
3398
3399         dprintk("%s: returns %d\n", __func__, status);
3400         return status;
3401 }
3402
3403 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3404                 u64 cookie, struct page **pages, unsigned int count, int plus)
3405 {
3406         struct nfs4_exception exception = { };
3407         int err;
3408         do {
3409                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3410                                 _nfs4_proc_readdir(dentry, cred, cookie,
3411                                         pages, count, plus),
3412                                 &exception);
3413         } while (exception.retry);
3414         return err;
3415 }
3416
3417 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3418                 struct iattr *sattr, dev_t rdev)
3419 {
3420         struct nfs4_createdata *data;
3421         int mode = sattr->ia_mode;
3422         int status = -ENOMEM;
3423
3424         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3425         if (data == NULL)
3426                 goto out;
3427
3428         if (S_ISFIFO(mode))
3429                 data->arg.ftype = NF4FIFO;
3430         else if (S_ISBLK(mode)) {
3431                 data->arg.ftype = NF4BLK;
3432                 data->arg.u.device.specdata1 = MAJOR(rdev);
3433                 data->arg.u.device.specdata2 = MINOR(rdev);
3434         }
3435         else if (S_ISCHR(mode)) {
3436                 data->arg.ftype = NF4CHR;
3437                 data->arg.u.device.specdata1 = MAJOR(rdev);
3438                 data->arg.u.device.specdata2 = MINOR(rdev);
3439         } else if (!S_ISSOCK(mode)) {
3440                 status = -EINVAL;
3441                 goto out_free;
3442         }
3443         
3444         status = nfs4_do_create(dir, dentry, data);
3445 out_free:
3446         nfs4_free_createdata(data);
3447 out:
3448         return status;
3449 }
3450
3451 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3452                 struct iattr *sattr, dev_t rdev)
3453 {
3454         struct nfs4_exception exception = { };
3455         int err;
3456
3457         sattr->ia_mode &= ~current_umask();
3458         do {
3459                 err = nfs4_handle_exception(NFS_SERVER(dir),
3460                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3461                                 &exception);
3462         } while (exception.retry);
3463         return err;
3464 }
3465
3466 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3467                  struct nfs_fsstat *fsstat)
3468 {
3469         struct nfs4_statfs_arg args = {
3470                 .fh = fhandle,
3471                 .bitmask = server->attr_bitmask,
3472         };
3473         struct nfs4_statfs_res res = {
3474                 .fsstat = fsstat,
3475         };
3476         struct rpc_message msg = {
3477                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3478                 .rpc_argp = &args,
3479                 .rpc_resp = &res,
3480         };
3481
3482         nfs_fattr_init(fsstat->fattr);
3483         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3484 }
3485
3486 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3487 {
3488         struct nfs4_exception exception = { };
3489         int err;
3490         do {
3491                 err = nfs4_handle_exception(server,
3492                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3493                                 &exception);
3494         } while (exception.retry);
3495         return err;
3496 }
3497
3498 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3499                 struct nfs_fsinfo *fsinfo)
3500 {
3501         struct nfs4_fsinfo_arg args = {
3502                 .fh = fhandle,
3503                 .bitmask = server->attr_bitmask,
3504         };
3505         struct nfs4_fsinfo_res res = {
3506                 .fsinfo = fsinfo,
3507         };
3508         struct rpc_message msg = {
3509                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3510                 .rpc_argp = &args,
3511                 .rpc_resp = &res,
3512         };
3513
3514         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3515 }
3516
3517 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3518 {
3519         struct nfs4_exception exception = { };
3520         int err;
3521
3522         do {
3523                 err = nfs4_handle_exception(server,
3524                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3525                                 &exception);
3526         } while (exception.retry);
3527         return err;
3528 }
3529
3530 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3531 {
3532         int error;
3533
3534         nfs_fattr_init(fsinfo->fattr);
3535         error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3536         if (error == 0) {
3537                 /* block layout checks this! */
3538                 server->pnfs_blksize = fsinfo->blksize;
3539                 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3540         }
3541
3542         return error;
3543 }
3544
3545 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3546                 struct nfs_pathconf *pathconf)
3547 {
3548         struct nfs4_pathconf_arg args = {
3549                 .fh = fhandle,
3550                 .bitmask = server->attr_bitmask,
3551         };
3552         struct nfs4_pathconf_res res = {
3553                 .pathconf = pathconf,
3554         };
3555         struct rpc_message msg = {
3556                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3557                 .rpc_argp = &args,
3558                 .rpc_resp = &res,
3559         };
3560
3561         /* None of the pathconf attributes are mandatory to implement */
3562         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3563                 memset(pathconf, 0, sizeof(*pathconf));
3564                 return 0;
3565         }
3566
3567         nfs_fattr_init(pathconf->fattr);
3568         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3569 }
3570
3571 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3572                 struct nfs_pathconf *pathconf)
3573 {
3574         struct nfs4_exception exception = { };
3575         int err;
3576
3577         do {
3578                 err = nfs4_handle_exception(server,
3579                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3580                                 &exception);
3581         } while (exception.retry);
3582         return err;
3583 }
3584
3585 void __nfs4_read_done_cb(struct nfs_read_data *data)
3586 {
3587         nfs_invalidate_atime(data->header->inode);
3588 }
3589
3590 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3591 {
3592         struct nfs_server *server = NFS_SERVER(data->header->inode);
3593
3594         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3595                 rpc_restart_call_prepare(task);
3596                 return -EAGAIN;
3597         }
3598
3599         __nfs4_read_done_cb(data);
3600         if (task->tk_status > 0)
3601                 renew_lease(server, data->timestamp);
3602         return 0;
3603 }
3604
3605 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3606 {
3607
3608         dprintk("--> %s\n", __func__);
3609
3610         if (!nfs4_sequence_done(task, &data->res.seq_res))
3611                 return -EAGAIN;
3612
3613         return data->read_done_cb ? data->read_done_cb(task, data) :
3614                                     nfs4_read_done_cb(task, data);
3615 }
3616
3617 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3618 {
3619         data->timestamp   = jiffies;
3620         data->read_done_cb = nfs4_read_done_cb;
3621         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3622         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3623 }
3624
3625 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3626 {
3627         if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3628                                 &data->args.seq_args,
3629                                 &data->res.seq_res,
3630                                 task))
3631                 return;
3632         rpc_call_start(task);
3633 }
3634
3635 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3636 {
3637         struct inode *inode = data->header->inode;
3638         
3639         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3640                 rpc_restart_call_prepare(task);
3641                 return -EAGAIN;
3642         }
3643         if (task->tk_status >= 0) {
3644                 renew_lease(NFS_SERVER(inode), data->timestamp);
3645                 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3646         }
3647         return 0;
3648 }
3649
3650 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3651 {
3652         if (!nfs4_sequence_done(task, &data->res.seq_res))
3653                 return -EAGAIN;
3654         return data->write_done_cb ? data->write_done_cb(task, data) :
3655                 nfs4_write_done_cb(task, data);
3656 }
3657
3658 static
3659 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3660 {
3661         const struct nfs_pgio_header *hdr = data->header;
3662
3663         /* Don't request attributes for pNFS or O_DIRECT writes */
3664         if (data->ds_clp != NULL || hdr->dreq != NULL)
3665                 return false;
3666         /* Otherwise, request attributes if and only if we don't hold
3667          * a delegation
3668          */
3669         return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3670 }
3671
3672 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3673 {
3674         struct nfs_server *server = NFS_SERVER(data->header->inode);
3675
3676         if (!nfs4_write_need_cache_consistency_data(data)) {
3677                 data->args.bitmask = NULL;
3678                 data->res.fattr = NULL;
3679         } else
3680                 data->args.bitmask = server->cache_consistency_bitmask;
3681
3682         if (!data->write_done_cb)
3683                 data->write_done_cb = nfs4_write_done_cb;
3684         data->res.server = server;
3685         data->timestamp   = jiffies;
3686
3687         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3688         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3689 }
3690
3691 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3692 {
3693         if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3694                                 &data->args.seq_args,
3695                                 &data->res.seq_res,
3696                                 task))
3697                 return;
3698         rpc_call_start(task);
3699 }
3700
3701 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3702 {
3703         if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3704                                 &data->args.seq_args,
3705                                 &data->res.seq_res,
3706                                 task))
3707                 return;
3708         rpc_call_start(task);
3709 }
3710
3711 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3712 {
3713         struct inode *inode = data->inode;
3714
3715         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3716                 rpc_restart_call_prepare(task);
3717                 return -EAGAIN;
3718         }
3719         return 0;
3720 }
3721
3722 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3723 {
3724         if (!nfs4_sequence_done(task, &data->res.seq_res))
3725                 return -EAGAIN;
3726         return data->commit_done_cb(task, data);
3727 }
3728
3729 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3730 {
3731         struct nfs_server *server = NFS_SERVER(data->inode);
3732
3733         if (data->commit_done_cb == NULL)
3734                 data->commit_done_cb = nfs4_commit_done_cb;
3735         data->res.server = server;
3736         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3737         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3738 }
3739
3740 struct nfs4_renewdata {
3741         struct nfs_client       *client;
3742         unsigned long           timestamp;
3743 };
3744
3745 /*
3746  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3747  * standalone procedure for queueing an asynchronous RENEW.
3748  */
3749 static void nfs4_renew_release(void *calldata)
3750 {
3751         struct nfs4_renewdata *data = calldata;
3752         struct nfs_client *clp = data->client;
3753
3754         if (atomic_read(&clp->cl_count) > 1)
3755                 nfs4_schedule_state_renewal(clp);
3756         nfs_put_client(clp);
3757         kfree(data);
3758 }
3759
3760 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3761 {
3762         struct nfs4_renewdata *data = calldata;
3763         struct nfs_client *clp = data->client;
3764         unsigned long timestamp = data->timestamp;
3765
3766         if (task->tk_status < 0) {
3767                 /* Unless we're shutting down, schedule state recovery! */
3768                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3769                         return;
3770                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3771                         nfs4_schedule_lease_recovery(clp);
3772                         return;
3773                 }
3774                 nfs4_schedule_path_down_recovery(clp);
3775         }
3776         do_renew_lease(clp, timestamp);
3777 }
3778
3779 static const struct rpc_call_ops nfs4_renew_ops = {
3780         .rpc_call_done = nfs4_renew_done,
3781         .rpc_release = nfs4_renew_release,
3782 };
3783
3784 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3785 {
3786         struct rpc_message msg = {
3787                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3788                 .rpc_argp       = clp,
3789                 .rpc_cred       = cred,
3790         };
3791         struct nfs4_renewdata *data;
3792
3793         if (renew_flags == 0)
3794                 return 0;
3795         if (!atomic_inc_not_zero(&clp->cl_count))
3796                 return -EIO;
3797         data = kmalloc(sizeof(*data), GFP_NOFS);
3798         if (data == NULL)
3799                 return -ENOMEM;
3800         data->client = clp;
3801         data->timestamp = jiffies;
3802         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3803                         &nfs4_renew_ops, data);
3804 }
3805
3806 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3807 {
3808         struct rpc_message msg = {
3809                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3810                 .rpc_argp       = clp,
3811                 .rpc_cred       = cred,
3812         };
3813         unsigned long now = jiffies;
3814         int status;
3815
3816         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3817         if (status < 0)
3818                 return status;
3819         do_renew_lease(clp, now);
3820         return 0;
3821 }
3822
3823 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3824 {
3825         return (server->caps & NFS_CAP_ACLS)
3826                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3827                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3828 }
3829
3830 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3831  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3832  * the stack.
3833  */
3834 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3835
3836 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3837                 struct page **pages, unsigned int *pgbase)
3838 {
3839         struct page *newpage, **spages;
3840         int rc = 0;
3841         size_t len;
3842         spages = pages;
3843
3844         do {
3845                 len = min_t(size_t, PAGE_SIZE, buflen);
3846                 newpage = alloc_page(GFP_KERNEL);
3847
3848                 if (newpage == NULL)
3849                         goto unwind;
3850                 memcpy(page_address(newpage), buf, len);
3851                 buf += len;
3852                 buflen -= len;
3853                 *pages++ = newpage;
3854                 rc++;
3855         } while (buflen != 0);
3856
3857         return rc;
3858
3859 unwind:
3860         for(; rc > 0; rc--)
3861                 __free_page(spages[rc-1]);
3862         return -ENOMEM;
3863 }
3864
3865 struct nfs4_cached_acl {
3866         int cached;
3867         size_t len;
3868         char data[0];
3869 };
3870
3871 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3872 {
3873         struct nfs_inode *nfsi = NFS_I(inode);
3874
3875         spin_lock(&inode->i_lock);
3876         kfree(nfsi->nfs4_acl);
3877         nfsi->nfs4_acl = acl;
3878         spin_unlock(&inode->i_lock);
3879 }
3880
3881 static void nfs4_zap_acl_attr(struct inode *inode)
3882 {
3883         nfs4_set_cached_acl(inode, NULL);
3884 }
3885
3886 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3887 {
3888         struct nfs_inode *nfsi = NFS_I(inode);
3889         struct nfs4_cached_acl *acl;
3890         int ret = -ENOENT;
3891
3892         spin_lock(&inode->i_lock);
3893         acl = nfsi->nfs4_acl;
3894         if (acl == NULL)
3895                 goto out;
3896         if (buf == NULL) /* user is just asking for length */
3897                 goto out_len;
3898         if (acl->cached == 0)
3899                 goto out;
3900         ret = -ERANGE; /* see getxattr(2) man page */
3901         if (acl->len > buflen)
3902                 goto out;
3903         memcpy(buf, acl->data, acl->len);
3904 out_len:
3905         ret = acl->len;
3906 out:
3907         spin_unlock(&inode->i_lock);
3908         return ret;
3909 }
3910
3911 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3912 {
3913         struct nfs4_cached_acl *acl;
3914         size_t buflen = sizeof(*acl) + acl_len;
3915
3916         if (buflen <= PAGE_SIZE) {
3917                 acl = kmalloc(buflen, GFP_KERNEL);
3918                 if (acl == NULL)
3919                         goto out;
3920                 acl->cached = 1;
3921                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3922         } else {
3923                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3924                 if (acl == NULL)
3925                         goto out;
3926                 acl->cached = 0;
3927         }
3928         acl->len = acl_len;
3929 out:
3930         nfs4_set_cached_acl(inode, acl);
3931 }
3932
3933 /*
3934  * The getxattr API returns the required buffer length when called with a
3935  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3936  * the required buf.  On a NULL buf, we send a page of data to the server
3937  * guessing that the ACL request can be serviced by a page. If so, we cache
3938  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3939  * the cache. If not so, we throw away the page, and cache the required
3940  * length. The next getxattr call will then produce another round trip to
3941  * the server, this time with the input buf of the required size.
3942  */
3943 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3944 {
3945         struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3946         struct nfs_getaclargs args = {
3947                 .fh = NFS_FH(inode),
3948                 .acl_pages = pages,
3949                 .acl_len = buflen,
3950         };
3951         struct nfs_getaclres res = {
3952                 .acl_len = buflen,
3953         };
3954         struct rpc_message msg = {
3955                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3956                 .rpc_argp = &args,
3957                 .rpc_resp = &res,
3958         };
3959         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3960         int ret = -ENOMEM, i;
3961
3962         /* As long as we're doing a round trip to the server anyway,
3963          * let's be prepared for a page of acl data. */
3964         if (npages == 0)
3965                 npages = 1;
3966         if (npages > ARRAY_SIZE(pages))
3967                 return -ERANGE;
3968
3969         for (i = 0; i < npages; i++) {
3970                 pages[i] = alloc_page(GFP_KERNEL);
3971                 if (!pages[i])
3972                         goto out_free;
3973         }
3974
3975         /* for decoding across pages */
3976         res.acl_scratch = alloc_page(GFP_KERNEL);
3977         if (!res.acl_scratch)
3978                 goto out_free;
3979
3980         args.acl_len = npages * PAGE_SIZE;
3981         args.acl_pgbase = 0;
3982
3983         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
3984                 __func__, buf, buflen, npages, args.acl_len);
3985         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3986                              &msg, &args.seq_args, &res.seq_res, 0);
3987         if (ret)
3988                 goto out_free;
3989
3990         /* Handle the case where the passed-in buffer is too short */
3991         if (res.acl_flags & NFS4_ACL_TRUNC) {
3992                 /* Did the user only issue a request for the acl length? */
3993                 if (buf == NULL)
3994                         goto out_ok;
3995                 ret = -ERANGE;
3996                 goto out_free;
3997         }
3998         nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3999         if (buf)
4000                 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4001 out_ok:
4002         ret = res.acl_len;
4003 out_free:
4004         for (i = 0; i < npages; i++)
4005                 if (pages[i])
4006                         __free_page(pages[i]);
4007         if (res.acl_scratch)
4008                 __free_page(res.acl_scratch);
4009         return ret;
4010 }
4011
4012 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4013 {
4014         struct nfs4_exception exception = { };
4015         ssize_t ret;
4016         do {
4017                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4018                 if (ret >= 0)
4019                         break;
4020                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4021         } while (exception.retry);
4022         return ret;
4023 }
4024
4025 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4026 {
4027         struct nfs_server *server = NFS_SERVER(inode);
4028         int ret;
4029
4030         if (!nfs4_server_supports_acls(server))
4031                 return -EOPNOTSUPP;
4032         ret = nfs_revalidate_inode(server, inode);
4033         if (ret < 0)
4034                 return ret;
4035         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4036                 nfs_zap_acl_cache(inode);
4037         ret = nfs4_read_cached_acl(inode, buf, buflen);
4038         if (ret != -ENOENT)
4039                 /* -ENOENT is returned if there is no ACL or if there is an ACL
4040                  * but no cached acl data, just the acl length */
4041                 return ret;
4042         return nfs4_get_acl_uncached(inode, buf, buflen);
4043 }
4044
4045 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4046 {
4047         struct nfs_server *server = NFS_SERVER(inode);
4048         struct page *pages[NFS4ACL_MAXPAGES];
4049         struct nfs_setaclargs arg = {
4050                 .fh             = NFS_FH(inode),
4051                 .acl_pages      = pages,
4052                 .acl_len        = buflen,
4053         };
4054         struct nfs_setaclres res;
4055         struct rpc_message msg = {
4056                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4057                 .rpc_argp       = &arg,
4058                 .rpc_resp       = &res,
4059         };
4060         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4061         int ret, i;
4062
4063         if (!nfs4_server_supports_acls(server))
4064                 return -EOPNOTSUPP;
4065         if (npages > ARRAY_SIZE(pages))
4066                 return -ERANGE;
4067         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4068         if (i < 0)
4069                 return i;
4070         nfs4_inode_return_delegation(inode);
4071         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4072
4073         /*
4074          * Free each page after tx, so the only ref left is
4075          * held by the network stack
4076          */
4077         for (; i > 0; i--)
4078                 put_page(pages[i-1]);
4079
4080         /*
4081          * Acl update can result in inode attribute update.
4082          * so mark the attribute cache invalid.
4083          */
4084         spin_lock(&inode->i_lock);
4085         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4086         spin_unlock(&inode->i_lock);
4087         nfs_access_zap_cache(inode);
4088         nfs_zap_acl_cache(inode);
4089         return ret;
4090 }
4091
4092 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4093 {
4094         struct nfs4_exception exception = { };
4095         int err;
4096         do {
4097                 err = nfs4_handle_exception(NFS_SERVER(inode),
4098                                 __nfs4_proc_set_acl(inode, buf, buflen),
4099                                 &exception);
4100         } while (exception.retry);
4101         return err;
4102 }
4103
4104 static int
4105 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4106 {
4107         struct nfs_client *clp = server->nfs_client;
4108
4109         if (task->tk_status >= 0)
4110                 return 0;
4111         switch(task->tk_status) {
4112                 case -NFS4ERR_DELEG_REVOKED:
4113                 case -NFS4ERR_ADMIN_REVOKED:
4114                 case -NFS4ERR_BAD_STATEID:
4115                         if (state == NULL)
4116                                 break;
4117                         nfs_remove_bad_delegation(state->inode);
4118                 case -NFS4ERR_OPENMODE:
4119                         if (state == NULL)
4120                                 break;
4121                         nfs4_schedule_stateid_recovery(server, state);
4122                         goto wait_on_recovery;
4123                 case -NFS4ERR_EXPIRED:
4124                         if (state != NULL)
4125                                 nfs4_schedule_stateid_recovery(server, state);
4126                 case -NFS4ERR_STALE_STATEID:
4127                 case -NFS4ERR_STALE_CLIENTID:
4128                         nfs4_schedule_lease_recovery(clp);
4129                         goto wait_on_recovery;
4130 #if defined(CONFIG_NFS_V4_1)
4131                 case -NFS4ERR_BADSESSION:
4132                 case -NFS4ERR_BADSLOT:
4133                 case -NFS4ERR_BAD_HIGH_SLOT:
4134                 case -NFS4ERR_DEADSESSION:
4135                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4136                 case -NFS4ERR_SEQ_FALSE_RETRY:
4137                 case -NFS4ERR_SEQ_MISORDERED:
4138                         dprintk("%s ERROR %d, Reset session\n", __func__,
4139                                 task->tk_status);
4140                         nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4141                         task->tk_status = 0;
4142                         return -EAGAIN;
4143 #endif /* CONFIG_NFS_V4_1 */
4144                 case -NFS4ERR_DELAY:
4145                         nfs_inc_server_stats(server, NFSIOS_DELAY);
4146                 case -NFS4ERR_GRACE:
4147                 case -EKEYEXPIRED:
4148                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
4149                         task->tk_status = 0;
4150                         return -EAGAIN;
4151                 case -NFS4ERR_RETRY_UNCACHED_REP:
4152                 case -NFS4ERR_OLD_STATEID:
4153                         task->tk_status = 0;
4154                         return -EAGAIN;
4155         }
4156         task->tk_status = nfs4_map_errors(task->tk_status);
4157         return 0;
4158 wait_on_recovery:
4159         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4160         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4161                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4162         task->tk_status = 0;
4163         return -EAGAIN;
4164 }
4165
4166 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4167                                     nfs4_verifier *bootverf)
4168 {
4169         __be32 verf[2];
4170
4171         if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4172                 /* An impossible timestamp guarantees this value
4173                  * will never match a generated boot time. */
4174                 verf[0] = 0;
4175                 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4176         } else {
4177                 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4178                 verf[0] = (__be32)nn->boot_time.tv_sec;
4179                 verf[1] = (__be32)nn->boot_time.tv_nsec;
4180         }
4181         memcpy(bootverf->data, verf, sizeof(bootverf->data));
4182 }
4183
4184 static unsigned int
4185 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4186                                    char *buf, size_t len)
4187 {
4188         unsigned int result;
4189
4190         rcu_read_lock();
4191         result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4192                                 clp->cl_ipaddr,
4193                                 rpc_peeraddr2str(clp->cl_rpcclient,
4194                                                         RPC_DISPLAY_ADDR),
4195                                 rpc_peeraddr2str(clp->cl_rpcclient,
4196                                                         RPC_DISPLAY_PROTO));
4197         rcu_read_unlock();
4198         return result;
4199 }
4200
4201 static unsigned int
4202 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4203                                 char *buf, size_t len)
4204 {
4205         char *nodename = clp->cl_rpcclient->cl_nodename;
4206
4207         if (nfs4_client_id_uniquifier[0] != '\0')
4208                 nodename = nfs4_client_id_uniquifier;
4209         return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4210                                 clp->rpc_ops->version, clp->cl_minorversion,
4211                                 nodename);
4212 }
4213
4214 /**
4215  * nfs4_proc_setclientid - Negotiate client ID
4216  * @clp: state data structure
4217  * @program: RPC program for NFSv4 callback service
4218  * @port: IP port number for NFS4 callback service
4219  * @cred: RPC credential to use for this call
4220  * @res: where to place the result
4221  *
4222  * Returns zero, a negative errno, or a negative NFS4ERR status code.
4223  */
4224 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4225                 unsigned short port, struct rpc_cred *cred,
4226                 struct nfs4_setclientid_res *res)
4227 {
4228         nfs4_verifier sc_verifier;
4229         struct nfs4_setclientid setclientid = {
4230                 .sc_verifier = &sc_verifier,
4231                 .sc_prog = program,
4232                 .sc_cb_ident = clp->cl_cb_ident,
4233         };
4234         struct rpc_message msg = {
4235                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4236                 .rpc_argp = &setclientid,
4237                 .rpc_resp = res,
4238                 .rpc_cred = cred,
4239         };
4240         int status;
4241
4242         /* nfs_client_id4 */
4243         nfs4_init_boot_verifier(clp, &sc_verifier);
4244         if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4245                 setclientid.sc_name_len =
4246                                 nfs4_init_uniform_client_string(clp,
4247                                                 setclientid.sc_name,
4248                                                 sizeof(setclientid.sc_name));
4249         else
4250                 setclientid.sc_name_len =
4251                                 nfs4_init_nonuniform_client_string(clp,
4252                                                 setclientid.sc_name,
4253                                                 sizeof(setclientid.sc_name));
4254         /* cb_client4 */
4255         rcu_read_lock();
4256         setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4257                                 sizeof(setclientid.sc_netid),
4258                                 rpc_peeraddr2str(clp->cl_rpcclient,
4259                                                         RPC_DISPLAY_NETID));
4260         rcu_read_unlock();
4261         setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4262                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4263                                 clp->cl_ipaddr, port >> 8, port & 255);
4264
4265         dprintk("NFS call  setclientid auth=%s, '%.*s'\n",
4266                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4267                 setclientid.sc_name_len, setclientid.sc_name);
4268         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4269         dprintk("NFS reply setclientid: %d\n", status);
4270         return status;
4271 }
4272
4273 /**
4274  * nfs4_proc_setclientid_confirm - Confirm client ID
4275  * @clp: state data structure
4276  * @res: result of a previous SETCLIENTID
4277  * @cred: RPC credential to use for this call
4278  *
4279  * Returns zero, a negative errno, or a negative NFS4ERR status code.
4280  */
4281 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4282                 struct nfs4_setclientid_res *arg,
4283                 struct rpc_cred *cred)
4284 {
4285         struct nfs_fsinfo fsinfo;
4286         struct rpc_message msg = {
4287                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4288                 .rpc_argp = arg,
4289                 .rpc_resp = &fsinfo,
4290                 .rpc_cred = cred,
4291         };
4292         unsigned long now;
4293         int status;
4294
4295         dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
4296                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4297                 clp->cl_clientid);
4298         now = jiffies;
4299         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4300         if (status == 0) {
4301                 spin_lock(&clp->cl_lock);
4302                 clp->cl_lease_time = fsinfo.lease_time * HZ;
4303                 clp->cl_last_renewal = now;
4304                 spin_unlock(&clp->cl_lock);
4305         }
4306         dprintk("NFS reply setclientid_confirm: %d\n", status);
4307         return status;
4308 }
4309
4310 struct nfs4_delegreturndata {
4311         struct nfs4_delegreturnargs args;
4312         struct nfs4_delegreturnres res;
4313         struct nfs_fh fh;
4314         nfs4_stateid stateid;
4315         unsigned long timestamp;
4316         struct nfs_fattr fattr;
4317         int rpc_status;
4318 };
4319
4320 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4321 {
4322         struct nfs4_delegreturndata *data = calldata;
4323
4324         if (!nfs4_sequence_done(task, &data->res.seq_res))
4325                 return;
4326
4327         switch (task->tk_status) {
4328         case -NFS4ERR_STALE_STATEID:
4329         case -NFS4ERR_EXPIRED:
4330         case 0:
4331                 renew_lease(data->res.server, data->timestamp);
4332                 break;
4333         default:
4334                 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4335                                 -EAGAIN) {
4336                         rpc_restart_call_prepare(task);
4337                         return;
4338                 }
4339         }
4340         data->rpc_status = task->tk_status;
4341 }
4342
4343 static void nfs4_delegreturn_release(void *calldata)
4344 {
4345         kfree(calldata);
4346 }
4347
4348 #if defined(CONFIG_NFS_V4_1)
4349 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4350 {
4351         struct nfs4_delegreturndata *d_data;
4352
4353         d_data = (struct nfs4_delegreturndata *)data;
4354
4355         if (nfs4_setup_sequence(d_data->res.server,
4356                                 &d_data->args.seq_args,
4357                                 &d_data->res.seq_res, task))
4358                 return;
4359         rpc_call_start(task);
4360 }
4361 #endif /* CONFIG_NFS_V4_1 */
4362
4363 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4364 #if defined(CONFIG_NFS_V4_1)
4365         .rpc_call_prepare = nfs4_delegreturn_prepare,
4366 #endif /* CONFIG_NFS_V4_1 */
4367         .rpc_call_done = nfs4_delegreturn_done,
4368         .rpc_release = nfs4_delegreturn_release,
4369 };
4370
4371 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4372 {
4373         struct nfs4_delegreturndata *data;
4374         struct nfs_server *server = NFS_SERVER(inode);
4375         struct rpc_task *task;
4376         struct rpc_message msg = {
4377                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4378                 .rpc_cred = cred,
4379         };
4380         struct rpc_task_setup task_setup_data = {
4381                 .rpc_client = server->client,
4382                 .rpc_message = &msg,
4383                 .callback_ops = &nfs4_delegreturn_ops,
4384                 .flags = RPC_TASK_ASYNC,
4385         };
4386         int status = 0;
4387
4388         data = kzalloc(sizeof(*data), GFP_NOFS);
4389         if (data == NULL)
4390                 return -ENOMEM;
4391         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4392         data->args.fhandle = &data->fh;
4393         data->args.stateid = &data->stateid;
4394         data->args.bitmask = server->cache_consistency_bitmask;
4395         nfs_copy_fh(&data->fh, NFS_FH(inode));
4396         nfs4_stateid_copy(&data->stateid, stateid);
4397         data->res.fattr = &data->fattr;
4398         data->res.server = server;
4399         nfs_fattr_init(data->res.fattr);
4400         data->timestamp = jiffies;
4401         data->rpc_status = 0;
4402
4403         task_setup_data.callback_data = data;
4404         msg.rpc_argp = &data->args;
4405         msg.rpc_resp = &data->res;
4406         task = rpc_run_task(&task_setup_data);
4407         if (IS_ERR(task))
4408                 return PTR_ERR(task);
4409         if (!issync)
4410                 goto out;
4411         status = nfs4_wait_for_completion_rpc_task(task);
4412         if (status != 0)
4413                 goto out;
4414         status = data->rpc_status;
4415         if (status == 0)
4416                 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4417         else
4418                 nfs_refresh_inode(inode, &data->fattr);
4419 out:
4420         rpc_put_task(task);
4421         return status;
4422 }
4423
4424 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4425 {
4426         struct nfs_server *server = NFS_SERVER(inode);
4427         struct nfs4_exception exception = { };
4428         int err;
4429         do {
4430                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4431                 switch (err) {
4432                         case -NFS4ERR_STALE_STATEID:
4433                         case -NFS4ERR_EXPIRED:
4434                         case 0:
4435                                 return 0;
4436                 }
4437                 err = nfs4_handle_exception(server, err, &exception);
4438         } while (exception.retry);
4439         return err;
4440 }
4441
4442 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4443 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4444
4445 /* 
4446  * sleep, with exponential backoff, and retry the LOCK operation. 
4447  */
4448 static unsigned long
4449 nfs4_set_lock_task_retry(unsigned long timeout)
4450 {
4451         freezable_schedule_timeout_killable(timeout);
4452         timeout <<= 1;
4453         if (timeout > NFS4_LOCK_MAXTIMEOUT)
4454                 return NFS4_LOCK_MAXTIMEOUT;
4455         return timeout;
4456 }
4457
4458 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4459 {
4460         struct inode *inode = state->inode;
4461         struct nfs_server *server = NFS_SERVER(inode);
4462         struct nfs_client *clp = server->nfs_client;
4463         struct nfs_lockt_args arg = {
4464                 .fh = NFS_FH(inode),
4465                 .fl = request,
4466         };
4467         struct nfs_lockt_res res = {
4468                 .denied = request,
4469         };
4470         struct rpc_message msg = {
4471                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4472                 .rpc_argp       = &arg,
4473                 .rpc_resp       = &res,
4474                 .rpc_cred       = state->owner->so_cred,
4475         };
4476         struct nfs4_lock_state *lsp;
4477         int status;
4478
4479         arg.lock_owner.clientid = clp->cl_clientid;
4480         status = nfs4_set_lock_state(state, request);
4481         if (status != 0)
4482                 goto out;
4483         lsp = request->fl_u.nfs4_fl.owner;
4484         arg.lock_owner.id = lsp->ls_seqid.owner_id;
4485         arg.lock_owner.s_dev = server->s_dev;
4486         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4487         switch (status) {
4488                 case 0:
4489                         request->fl_type = F_UNLCK;
4490                         break;
4491                 case -NFS4ERR_DENIED:
4492                         status = 0;
4493         }
4494         request->fl_ops->fl_release_private(request);
4495 out:
4496         return status;
4497 }
4498
4499 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4500 {
4501         struct nfs4_exception exception = { };
4502         int err;
4503
4504         do {
4505                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4506                                 _nfs4_proc_getlk(state, cmd, request),
4507                                 &exception);
4508         } while (exception.retry);
4509         return err;
4510 }
4511
4512 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4513 {
4514         int res = 0;
4515         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4516                 case FL_POSIX:
4517                         res = posix_lock_file_wait(file, fl);
4518                         break;
4519                 case FL_FLOCK:
4520                         res = flock_lock_file_wait(file, fl);
4521                         break;
4522                 default:
4523                         BUG();
4524         }
4525         return res;
4526 }
4527
4528 struct nfs4_unlockdata {
4529         struct nfs_locku_args arg;
4530         struct nfs_locku_res res;
4531         struct nfs4_lock_state *lsp;
4532         struct nfs_open_context *ctx;
4533         struct file_lock fl;
4534         const struct nfs_server *server;
4535         unsigned long timestamp;
4536 };
4537
4538 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4539                 struct nfs_open_context *ctx,
4540                 struct nfs4_lock_state *lsp,
4541                 struct nfs_seqid *seqid)
4542 {
4543         struct nfs4_unlockdata *p;
4544         struct inode *inode = lsp->ls_state->inode;
4545
4546         p = kzalloc(sizeof(*p), GFP_NOFS);
4547         if (p == NULL)
4548                 return NULL;
4549         p->arg.fh = NFS_FH(inode);
4550         p->arg.fl = &p->fl;
4551         p->arg.seqid = seqid;
4552         p->res.seqid = seqid;
4553         p->arg.stateid = &lsp->ls_stateid;
4554         p->lsp = lsp;
4555         atomic_inc(&lsp->ls_count);
4556         /* Ensure we don't close file until we're done freeing locks! */
4557         p->ctx = get_nfs_open_context(ctx);
4558         memcpy(&p->fl, fl, sizeof(p->fl));
4559         p->server = NFS_SERVER(inode);
4560         return p;
4561 }
4562
4563 static void nfs4_locku_release_calldata(void *data)
4564 {
4565         struct nfs4_unlockdata *calldata = data;
4566         nfs_free_seqid(calldata->arg.seqid);
4567         nfs4_put_lock_state(calldata->lsp);
4568         put_nfs_open_context(calldata->ctx);
4569         kfree(calldata);
4570 }
4571
4572 static void nfs4_locku_done(struct rpc_task *task, void *data)
4573 {
4574         struct nfs4_unlockdata *calldata = data;
4575
4576         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4577                 return;
4578         switch (task->tk_status) {
4579                 case 0:
4580                         nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4581                                         &calldata->res.stateid);
4582                         renew_lease(calldata->server, calldata->timestamp);
4583                         break;
4584                 case -NFS4ERR_BAD_STATEID:
4585                 case -NFS4ERR_OLD_STATEID:
4586                 case -NFS4ERR_STALE_STATEID:
4587                 case -NFS4ERR_EXPIRED:
4588                         break;
4589                 default:
4590                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4591                                 rpc_restart_call_prepare(task);
4592         }
4593         nfs_release_seqid(calldata->arg.seqid);
4594 }
4595
4596 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4597 {
4598         struct nfs4_unlockdata *calldata = data;
4599
4600         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4601                 return;
4602         if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4603                 /* Note: exit _without_ running nfs4_locku_done */
4604                 task->tk_action = NULL;
4605                 return;
4606         }
4607         calldata->timestamp = jiffies;
4608         if (nfs4_setup_sequence(calldata->server,
4609                                 &calldata->arg.seq_args,
4610                                 &calldata->res.seq_res,
4611                                 task) != 0)
4612                 nfs_release_seqid(calldata->arg.seqid);
4613         else
4614                 rpc_call_start(task);
4615 }
4616
4617 static const struct rpc_call_ops nfs4_locku_ops = {
4618         .rpc_call_prepare = nfs4_locku_prepare,
4619         .rpc_call_done = nfs4_locku_done,
4620         .rpc_release = nfs4_locku_release_calldata,
4621 };
4622
4623 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4624                 struct nfs_open_context *ctx,
4625                 struct nfs4_lock_state *lsp,
4626                 struct nfs_seqid *seqid)
4627 {
4628         struct nfs4_unlockdata *data;
4629         struct rpc_message msg = {
4630                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4631                 .rpc_cred = ctx->cred,
4632         };
4633         struct rpc_task_setup task_setup_data = {
4634                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4635                 .rpc_message = &msg,
4636                 .callback_ops = &nfs4_locku_ops,
4637                 .workqueue = nfsiod_workqueue,
4638                 .flags = RPC_TASK_ASYNC,
4639         };
4640
4641         /* Ensure this is an unlock - when canceling a lock, the
4642          * canceled lock is passed in, and it won't be an unlock.
4643          */
4644         fl->fl_type = F_UNLCK;
4645
4646         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4647         if (data == NULL) {
4648                 nfs_free_seqid(seqid);
4649                 return ERR_PTR(-ENOMEM);
4650         }
4651
4652         nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4653         msg.rpc_argp = &data->arg;
4654         msg.rpc_resp = &data->res;
4655         task_setup_data.callback_data = data;
4656         return rpc_run_task(&task_setup_data);
4657 }
4658
4659 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4660 {
4661         struct nfs_inode *nfsi = NFS_I(state->inode);
4662         struct nfs_seqid *seqid;
4663         struct nfs4_lock_state *lsp;
4664         struct rpc_task *task;
4665         int status = 0;
4666         unsigned char fl_flags = request->fl_flags;
4667
4668         status = nfs4_set_lock_state(state, request);
4669         /* Unlock _before_ we do the RPC call */
4670         request->fl_flags |= FL_EXISTS;
4671         down_read(&nfsi->rwsem);
4672         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4673                 up_read(&nfsi->rwsem);
4674                 goto out;
4675         }
4676         up_read(&nfsi->rwsem);
4677         if (status != 0)
4678                 goto out;
4679         /* Is this a delegated lock? */
4680         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4681                 goto out;
4682         lsp = request->fl_u.nfs4_fl.owner;
4683         seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4684         status = -ENOMEM;
4685         if (seqid == NULL)
4686                 goto out;
4687         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4688         status = PTR_ERR(task);
4689         if (IS_ERR(task))
4690                 goto out;
4691         status = nfs4_wait_for_completion_rpc_task(task);
4692         rpc_put_task(task);
4693 out:
4694         request->fl_flags = fl_flags;
4695         return status;
4696 }
4697
4698 struct nfs4_lockdata {
4699         struct nfs_lock_args arg;
4700         struct nfs_lock_res res;
4701         struct nfs4_lock_state *lsp;
4702         struct nfs_open_context *ctx;
4703         struct file_lock fl;
4704         unsigned long timestamp;
4705         int rpc_status;
4706         int cancelled;
4707         struct nfs_server *server;
4708 };
4709
4710 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4711                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4712                 gfp_t gfp_mask)
4713 {
4714         struct nfs4_lockdata *p;
4715         struct inode *inode = lsp->ls_state->inode;
4716         struct nfs_server *server = NFS_SERVER(inode);
4717
4718         p = kzalloc(sizeof(*p), gfp_mask);
4719         if (p == NULL)
4720                 return NULL;
4721
4722         p->arg.fh = NFS_FH(inode);
4723         p->arg.fl = &p->fl;
4724         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4725         if (p->arg.open_seqid == NULL)
4726                 goto out_free;
4727         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4728         if (p->arg.lock_seqid == NULL)
4729                 goto out_free_seqid;
4730         p->arg.lock_stateid = &lsp->ls_stateid;
4731         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4732         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4733         p->arg.lock_owner.s_dev = server->s_dev;
4734         p->res.lock_seqid = p->arg.lock_seqid;
4735         p->lsp = lsp;
4736         p->server = server;
4737         atomic_inc(&lsp->ls_count);
4738         p->ctx = get_nfs_open_context(ctx);
4739         memcpy(&p->fl, fl, sizeof(p->fl));
4740         return p;
4741 out_free_seqid:
4742         nfs_free_seqid(p->arg.open_seqid);
4743 out_free:
4744         kfree(p);
4745         return NULL;
4746 }
4747
4748 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4749 {
4750         struct nfs4_lockdata *data = calldata;
4751         struct nfs4_state *state = data->lsp->ls_state;
4752
4753         dprintk("%s: begin!\n", __func__);
4754         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4755                 return;
4756         /* Do we need to do an open_to_lock_owner? */
4757         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4758                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4759                         goto out_release_lock_seqid;
4760                 data->arg.open_stateid = &state->stateid;
4761                 data->arg.new_lock_owner = 1;
4762                 data->res.open_seqid = data->arg.open_seqid;
4763         } else
4764                 data->arg.new_lock_owner = 0;
4765         data->timestamp = jiffies;
4766         if (nfs4_setup_sequence(data->server,
4767                                 &data->arg.seq_args,
4768                                 &data->res.seq_res,
4769                                 task) == 0) {
4770                 rpc_call_start(task);
4771                 return;
4772         }
4773         nfs_release_seqid(data->arg.open_seqid);
4774 out_release_lock_seqid:
4775         nfs_release_seqid(data->arg.lock_seqid);
4776         dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4777 }
4778
4779 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4780 {
4781         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4782         nfs4_lock_prepare(task, calldata);
4783 }
4784
4785 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4786 {
4787         struct nfs4_lockdata *data = calldata;
4788
4789         dprintk("%s: begin!\n", __func__);
4790
4791         if (!nfs4_sequence_done(task, &data->res.seq_res))
4792                 return;
4793
4794         data->rpc_status = task->tk_status;
4795         if (data->arg.new_lock_owner != 0) {
4796                 if (data->rpc_status == 0)
4797                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4798                 else
4799                         goto out;
4800         }
4801         if (data->rpc_status == 0) {
4802                 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4803                 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4804                 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4805         }
4806 out:
4807         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4808 }
4809
4810 static void nfs4_lock_release(void *calldata)
4811 {
4812         struct nfs4_lockdata *data = calldata;
4813
4814         dprintk("%s: begin!\n", __func__);
4815         nfs_free_seqid(data->arg.open_seqid);
4816         if (data->cancelled != 0) {
4817                 struct rpc_task *task;
4818                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4819                                 data->arg.lock_seqid);
4820                 if (!IS_ERR(task))
4821                         rpc_put_task_async(task);
4822                 dprintk("%s: cancelling lock!\n", __func__);
4823         } else
4824                 nfs_free_seqid(data->arg.lock_seqid);
4825         nfs4_put_lock_state(data->lsp);
4826         put_nfs_open_context(data->ctx);
4827         kfree(data);
4828         dprintk("%s: done!\n", __func__);
4829 }
4830
4831 static const struct rpc_call_ops nfs4_lock_ops = {
4832         .rpc_call_prepare = nfs4_lock_prepare,
4833         .rpc_call_done = nfs4_lock_done,
4834         .rpc_release = nfs4_lock_release,
4835 };
4836
4837 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4838         .rpc_call_prepare = nfs4_recover_lock_prepare,
4839         .rpc_call_done = nfs4_lock_done,
4840         .rpc_release = nfs4_lock_release,
4841 };
4842
4843 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4844 {
4845         switch (error) {
4846         case -NFS4ERR_ADMIN_REVOKED:
4847         case -NFS4ERR_BAD_STATEID:
4848                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4849                 if (new_lock_owner != 0 ||
4850                    test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4851                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4852                 break;
4853         case -NFS4ERR_STALE_STATEID:
4854                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4855         case -NFS4ERR_EXPIRED:
4856                 nfs4_schedule_lease_recovery(server->nfs_client);
4857         };
4858 }
4859
4860 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4861 {
4862         struct nfs4_lockdata *data;
4863         struct rpc_task *task;
4864         struct rpc_message msg = {
4865                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4866                 .rpc_cred = state->owner->so_cred,
4867         };
4868         struct rpc_task_setup task_setup_data = {
4869                 .rpc_client = NFS_CLIENT(state->inode),
4870                 .rpc_message = &msg,
4871                 .callback_ops = &nfs4_lock_ops,
4872                 .workqueue = nfsiod_workqueue,
4873                 .flags = RPC_TASK_ASYNC,
4874         };
4875         int ret;
4876
4877         dprintk("%s: begin!\n", __func__);
4878         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4879                         fl->fl_u.nfs4_fl.owner,
4880                         recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4881         if (data == NULL)
4882                 return -ENOMEM;
4883         if (IS_SETLKW(cmd))
4884                 data->arg.block = 1;
4885         if (recovery_type > NFS_LOCK_NEW) {
4886                 if (recovery_type == NFS_LOCK_RECLAIM)
4887                         data->arg.reclaim = NFS_LOCK_RECLAIM;
4888                 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4889         }
4890         nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4891         msg.rpc_argp = &data->arg;
4892         msg.rpc_resp = &data->res;
4893         task_setup_data.callback_data = data;
4894         task = rpc_run_task(&task_setup_data);
4895         if (IS_ERR(task))
4896                 return PTR_ERR(task);
4897         ret = nfs4_wait_for_completion_rpc_task(task);
4898         if (ret == 0) {
4899                 ret = data->rpc_status;
4900                 if (ret)
4901                         nfs4_handle_setlk_error(data->server, data->lsp,
4902                                         data->arg.new_lock_owner, ret);
4903         } else
4904                 data->cancelled = 1;
4905         rpc_put_task(task);
4906         dprintk("%s: done, ret = %d!\n", __func__, ret);
4907         return ret;
4908 }
4909
4910 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4911 {
4912         struct nfs_server *server = NFS_SERVER(state->inode);
4913         struct nfs4_exception exception = {
4914                 .inode = state->inode,
4915         };
4916         int err;
4917
4918         do {
4919                 /* Cache the lock if possible... */
4920                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4921                         return 0;
4922                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4923                 if (err != -NFS4ERR_DELAY)
4924                         break;
4925                 nfs4_handle_exception(server, err, &exception);
4926         } while (exception.retry);
4927         return err;
4928 }
4929
4930 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4931 {
4932         struct nfs_server *server = NFS_SERVER(state->inode);
4933         struct nfs4_exception exception = {
4934                 .inode = state->inode,
4935         };
4936         int err;
4937
4938         err = nfs4_set_lock_state(state, request);
4939         if (err != 0)
4940                 return err;
4941         do {
4942                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4943                         return 0;
4944                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4945                 switch (err) {
4946                 default:
4947                         goto out;
4948                 case -NFS4ERR_GRACE:
4949                 case -NFS4ERR_DELAY:
4950                         nfs4_handle_exception(server, err, &exception);
4951                         err = 0;
4952                 }
4953         } while (exception.retry);
4954 out:
4955         return err;
4956 }
4957
4958 #if defined(CONFIG_NFS_V4_1)
4959 /**
4960  * nfs41_check_expired_locks - possibly free a lock stateid
4961  *
4962  * @state: NFSv4 state for an inode
4963  *
4964  * Returns NFS_OK if recovery for this stateid is now finished.
4965  * Otherwise a negative NFS4ERR value is returned.
4966  */
4967 static int nfs41_check_expired_locks(struct nfs4_state *state)
4968 {
4969         int status, ret = -NFS4ERR_BAD_STATEID;
4970         struct nfs4_lock_state *lsp;
4971         struct nfs_server *server = NFS_SERVER(state->inode);
4972
4973         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4974                 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4975                         status = nfs41_test_stateid(server, &lsp->ls_stateid);
4976                         if (status != NFS_OK) {
4977                                 /* Free the stateid unless the server
4978                                  * informs us the stateid is unrecognized. */
4979                                 if (status != -NFS4ERR_BAD_STATEID)
4980                                         nfs41_free_stateid(server,
4981                                                         &lsp->ls_stateid);
4982                                 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4983                                 ret = status;
4984                         }
4985                 }
4986         };
4987
4988         return ret;
4989 }
4990
4991 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4992 {
4993         int status = NFS_OK;
4994
4995         if (test_bit(LK_STATE_IN_USE, &state->flags))
4996                 status = nfs41_check_expired_locks(state);
4997         if (status != NFS_OK)
4998                 status = nfs4_lock_expired(state, request);
4999         return status;
5000 }
5001 #endif
5002
5003 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5004 {
5005         struct nfs_inode *nfsi = NFS_I(state->inode);
5006         unsigned char fl_flags = request->fl_flags;
5007         int status = -ENOLCK;
5008
5009         if ((fl_flags & FL_POSIX) &&
5010                         !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5011                 goto out;
5012         /* Is this a delegated open? */
5013         status = nfs4_set_lock_state(state, request);
5014         if (status != 0)
5015                 goto out;
5016         request->fl_flags |= FL_ACCESS;
5017         status = do_vfs_lock(request->fl_file, request);
5018         if (status < 0)
5019                 goto out;
5020         down_read(&nfsi->rwsem);
5021         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5022                 /* Yes: cache locks! */
5023                 /* ...but avoid races with delegation recall... */
5024                 request->fl_flags = fl_flags & ~FL_SLEEP;
5025                 status = do_vfs_lock(request->fl_file, request);
5026                 goto out_unlock;
5027         }
5028         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5029         if (status != 0)
5030                 goto out_unlock;
5031         /* Note: we always want to sleep here! */
5032         request->fl_flags = fl_flags | FL_SLEEP;
5033         if (do_vfs_lock(request->fl_file, request) < 0)
5034                 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5035                         "manager!\n", __func__);
5036 out_unlock:
5037         up_read(&nfsi->rwsem);
5038 out:
5039         request->fl_flags = fl_flags;
5040         return status;
5041 }
5042
5043 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5044 {
5045         struct nfs4_exception exception = {
5046                 .state = state,
5047                 .inode = state->inode,
5048         };
5049         int err;
5050
5051         do {
5052                 err = _nfs4_proc_setlk(state, cmd, request);
5053                 if (err == -NFS4ERR_DENIED)
5054                         err = -EAGAIN;
5055                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5056                                 err, &exception);
5057         } while (exception.retry);
5058         return err;
5059 }
5060
5061 static int
5062 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5063 {
5064         struct nfs_open_context *ctx;
5065         struct nfs4_state *state;
5066         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5067         int status;
5068
5069         /* verify open state */
5070         ctx = nfs_file_open_context(filp);
5071         state = ctx->state;
5072
5073         if (request->fl_start < 0 || request->fl_end < 0)
5074                 return -EINVAL;
5075
5076         if (IS_GETLK(cmd)) {
5077                 if (state != NULL)
5078                         return nfs4_proc_getlk(state, F_GETLK, request);
5079                 return 0;
5080         }
5081
5082         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5083                 return -EINVAL;
5084
5085         if (request->fl_type == F_UNLCK) {
5086                 if (state != NULL)
5087                         return nfs4_proc_unlck(state, cmd, request);
5088                 return 0;
5089         }
5090
5091         if (state == NULL)
5092                 return -ENOLCK;
5093         /*
5094          * Don't rely on the VFS having checked the file open mode,
5095          * since it won't do this for flock() locks.
5096          */
5097         switch (request->fl_type) {
5098         case F_RDLCK:
5099                 if (!(filp->f_mode & FMODE_READ))
5100                         return -EBADF;
5101                 break;
5102         case F_WRLCK:
5103                 if (!(filp->f_mode & FMODE_WRITE))
5104                         return -EBADF;
5105         }
5106
5107         do {
5108                 status = nfs4_proc_setlk(state, cmd, request);
5109                 if ((status != -EAGAIN) || IS_SETLK(cmd))
5110                         break;
5111                 timeout = nfs4_set_lock_task_retry(timeout);
5112                 status = -ERESTARTSYS;
5113                 if (signalled())
5114                         break;
5115         } while(status < 0);
5116         return status;
5117 }
5118
5119 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5120 {
5121         struct nfs_server *server = NFS_SERVER(state->inode);
5122         struct nfs4_exception exception = { };
5123         int err;
5124
5125         err = nfs4_set_lock_state(state, fl);
5126         if (err != 0)
5127                 goto out;
5128         do {
5129                 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5130                 switch (err) {
5131                         default:
5132                                 printk(KERN_ERR "NFS: %s: unhandled error "
5133                                         "%d.\n", __func__, err);
5134                         case 0:
5135                         case -ESTALE:
5136                                 goto out;
5137                         case -NFS4ERR_EXPIRED:
5138                                 nfs4_schedule_stateid_recovery(server, state);
5139                         case -NFS4ERR_STALE_CLIENTID:
5140                         case -NFS4ERR_STALE_STATEID:
5141                                 nfs4_schedule_lease_recovery(server->nfs_client);
5142                                 goto out;
5143                         case -NFS4ERR_BADSESSION:
5144                         case -NFS4ERR_BADSLOT:
5145                         case -NFS4ERR_BAD_HIGH_SLOT:
5146                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5147                         case -NFS4ERR_DEADSESSION:
5148                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5149                                 goto out;
5150                         case -ERESTARTSYS:
5151                                 /*
5152                                  * The show must go on: exit, but mark the
5153                                  * stateid as needing recovery.
5154                                  */
5155                         case -NFS4ERR_DELEG_REVOKED:
5156                         case -NFS4ERR_ADMIN_REVOKED:
5157                         case -NFS4ERR_BAD_STATEID:
5158                         case -NFS4ERR_OPENMODE:
5159                                 nfs4_schedule_stateid_recovery(server, state);
5160                                 err = 0;
5161                                 goto out;
5162                         case -EKEYEXPIRED:
5163                                 /*
5164                                  * User RPCSEC_GSS context has expired.
5165                                  * We cannot recover this stateid now, so
5166                                  * skip it and allow recovery thread to
5167                                  * proceed.
5168                                  */
5169                                 err = 0;
5170                                 goto out;
5171                         case -ENOMEM:
5172                         case -NFS4ERR_DENIED:
5173                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5174                                 err = 0;
5175                                 goto out;
5176                         case -NFS4ERR_DELAY:
5177                                 break;
5178                 }
5179                 err = nfs4_handle_exception(server, err, &exception);
5180         } while (exception.retry);
5181 out:
5182         return err;
5183 }
5184
5185 struct nfs_release_lockowner_data {
5186         struct nfs4_lock_state *lsp;
5187         struct nfs_server *server;
5188         struct nfs_release_lockowner_args args;
5189 };
5190
5191 static void nfs4_release_lockowner_release(void *calldata)
5192 {
5193         struct nfs_release_lockowner_data *data = calldata;
5194         nfs4_free_lock_state(data->server, data->lsp);
5195         kfree(calldata);
5196 }
5197
5198 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5199         .rpc_release = nfs4_release_lockowner_release,
5200 };
5201
5202 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5203 {
5204         struct nfs_server *server = lsp->ls_state->owner->so_server;
5205         struct nfs_release_lockowner_data *data;
5206         struct rpc_message msg = {
5207                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5208         };
5209
5210         if (server->nfs_client->cl_mvops->minor_version != 0)
5211                 return -EINVAL;
5212         data = kmalloc(sizeof(*data), GFP_NOFS);
5213         if (!data)
5214                 return -ENOMEM;
5215         data->lsp = lsp;
5216         data->server = server;
5217         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5218         data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5219         data->args.lock_owner.s_dev = server->s_dev;
5220         msg.rpc_argp = &data->args;
5221         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5222         return 0;
5223 }
5224
5225 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5226
5227 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5228                                    const void *buf, size_t buflen,
5229                                    int flags, int type)
5230 {
5231         if (strcmp(key, "") != 0)
5232                 return -EINVAL;
5233
5234         return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5235 }
5236
5237 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5238                                    void *buf, size_t buflen, int type)
5239 {
5240         if (strcmp(key, "") != 0)
5241                 return -EINVAL;
5242
5243         return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5244 }
5245
5246 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5247                                        size_t list_len, const char *name,
5248                                        size_t name_len, int type)
5249 {
5250         size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5251
5252         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5253                 return 0;
5254
5255         if (list && len <= list_len)
5256                 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5257         return len;
5258 }
5259
5260 /*
5261  * nfs_fhget will use either the mounted_on_fileid or the fileid
5262  */
5263 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5264 {
5265         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5266                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5267               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5268               (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5269                 return;
5270
5271         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5272                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5273         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5274         fattr->nlink = 2;
5275 }
5276
5277 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5278                                    const struct qstr *name,
5279                                    struct nfs4_fs_locations *fs_locations,
5280                                    struct page *page)
5281 {
5282         struct nfs_server *server = NFS_SERVER(dir);
5283         u32 bitmask[2] = {
5284                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5285         };
5286         struct nfs4_fs_locations_arg args = {
5287                 .dir_fh = NFS_FH(dir),
5288                 .name = name,
5289                 .page = page,
5290                 .bitmask = bitmask,
5291         };
5292         struct nfs4_fs_locations_res res = {
5293                 .fs_locations = fs_locations,
5294         };
5295         struct rpc_message msg = {
5296                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5297                 .rpc_argp = &args,
5298                 .rpc_resp = &res,
5299         };
5300         int status;
5301
5302         dprintk("%s: start\n", __func__);
5303
5304         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5305          * is not supported */
5306         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5307                 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5308         else
5309                 bitmask[0] |= FATTR4_WORD0_FILEID;
5310
5311         nfs_fattr_init(&fs_locations->fattr);
5312         fs_locations->server = server;
5313         fs_locations->nlocations = 0;
5314         status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5315         dprintk("%s: returned status = %d\n", __func__, status);
5316         return status;
5317 }
5318
5319 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5320                            const struct qstr *name,
5321                            struct nfs4_fs_locations *fs_locations,
5322                            struct page *page)
5323 {
5324         struct nfs4_exception exception = { };
5325         int err;
5326         do {
5327                 err = nfs4_handle_exception(NFS_SERVER(dir),
5328                                 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5329                                 &exception);
5330         } while (exception.retry);
5331         return err;
5332 }
5333
5334 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5335 {
5336         int status;
5337         struct nfs4_secinfo_arg args = {
5338                 .dir_fh = NFS_FH(dir),
5339                 .name   = name,
5340         };
5341         struct nfs4_secinfo_res res = {
5342                 .flavors     = flavors,
5343         };
5344         struct rpc_message msg = {
5345                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5346                 .rpc_argp = &args,
5347                 .rpc_resp = &res,
5348         };
5349
5350         dprintk("NFS call  secinfo %s\n", name->name);
5351         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5352         dprintk("NFS reply  secinfo: %d\n", status);
5353         return status;
5354 }
5355
5356 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5357                       struct nfs4_secinfo_flavors *flavors)
5358 {
5359         struct nfs4_exception exception = { };
5360         int err;
5361         do {
5362                 err = nfs4_handle_exception(NFS_SERVER(dir),
5363                                 _nfs4_proc_secinfo(dir, name, flavors),
5364                                 &exception);
5365         } while (exception.retry);
5366         return err;
5367 }
5368
5369 #ifdef CONFIG_NFS_V4_1
5370 /*
5371  * Check the exchange flags returned by the server for invalid flags, having
5372  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5373  * DS flags set.
5374  */
5375 static int nfs4_check_cl_exchange_flags(u32 flags)
5376 {
5377         if (flags & ~EXCHGID4_FLAG_MASK_R)
5378                 goto out_inval;
5379         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5380             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5381                 goto out_inval;
5382         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5383                 goto out_inval;
5384         return NFS_OK;
5385 out_inval:
5386         return -NFS4ERR_INVAL;
5387 }
5388
5389 static bool
5390 nfs41_same_server_scope(struct nfs41_server_scope *a,
5391                         struct nfs41_server_scope *b)
5392 {
5393         if (a->server_scope_sz == b->server_scope_sz &&
5394             memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5395                 return true;
5396
5397         return false;
5398 }
5399
5400 /*
5401  * nfs4_proc_bind_conn_to_session()
5402  *
5403  * The 4.1 client currently uses the same TCP connection for the
5404  * fore and backchannel.
5405  */
5406 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5407 {
5408         int status;
5409         struct nfs41_bind_conn_to_session_res res;
5410         struct rpc_message msg = {
5411                 .rpc_proc =
5412                         &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5413                 .rpc_argp = clp,
5414                 .rpc_resp = &res,
5415                 .rpc_cred = cred,
5416         };
5417
5418         dprintk("--> %s\n", __func__);
5419
5420         res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5421         if (unlikely(res.session == NULL)) {
5422                 status = -ENOMEM;
5423                 goto out;
5424         }
5425
5426         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5427         if (status == 0) {
5428                 if (memcmp(res.session->sess_id.data,
5429                     clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5430                         dprintk("NFS: %s: Session ID mismatch\n", __func__);
5431                         status = -EIO;
5432                         goto out_session;
5433                 }
5434                 if (res.dir != NFS4_CDFS4_BOTH) {
5435                         dprintk("NFS: %s: Unexpected direction from server\n",
5436                                 __func__);
5437                         status = -EIO;
5438                         goto out_session;
5439                 }
5440                 if (res.use_conn_in_rdma_mode) {
5441                         dprintk("NFS: %s: Server returned RDMA mode = true\n",
5442                                 __func__);
5443                         status = -EIO;
5444                         goto out_session;
5445                 }
5446         }
5447 out_session:
5448         kfree(res.session);
5449 out:
5450         dprintk("<-- %s status= %d\n", __func__, status);
5451         return status;
5452 }
5453
5454 /*
5455  * nfs4_proc_exchange_id()
5456  *
5457  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5458  *
5459  * Since the clientid has expired, all compounds using sessions
5460  * associated with the stale clientid will be returning
5461  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5462  * be in some phase of session reset.
5463  */
5464 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5465 {
5466         nfs4_verifier verifier;
5467         struct nfs41_exchange_id_args args = {
5468                 .verifier = &verifier,
5469                 .client = clp,
5470                 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5471         };
5472         struct nfs41_exchange_id_res res = {
5473                 0
5474         };
5475         int status;
5476         struct rpc_message msg = {
5477                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5478                 .rpc_argp = &args,
5479                 .rpc_resp = &res,
5480                 .rpc_cred = cred,
5481         };
5482
5483         nfs4_init_boot_verifier(clp, &verifier);
5484         args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5485                                                         sizeof(args.id));
5486         dprintk("NFS call  exchange_id auth=%s, '%.*s'\n",
5487                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5488                 args.id_len, args.id);
5489
5490         res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5491                                         GFP_NOFS);
5492         if (unlikely(res.server_owner == NULL)) {
5493                 status = -ENOMEM;
5494                 goto out;
5495         }
5496
5497         res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5498                                         GFP_NOFS);
5499         if (unlikely(res.server_scope == NULL)) {
5500                 status = -ENOMEM;
5501                 goto out_server_owner;
5502         }
5503
5504         res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5505         if (unlikely(res.impl_id == NULL)) {
5506                 status = -ENOMEM;
5507                 goto out_server_scope;
5508         }
5509
5510         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5511         if (status == 0)
5512                 status = nfs4_check_cl_exchange_flags(res.flags);
5513
5514         if (status == 0) {
5515                 clp->cl_clientid = res.clientid;
5516                 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5517                 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5518                         clp->cl_seqid = res.seqid;
5519
5520                 kfree(clp->cl_serverowner);
5521                 clp->cl_serverowner = res.server_owner;
5522                 res.server_owner = NULL;
5523
5524                 /* use the most recent implementation id */
5525                 kfree(clp->cl_implid);
5526                 clp->cl_implid = res.impl_id;
5527
5528                 if (clp->cl_serverscope != NULL &&
5529                     !nfs41_same_server_scope(clp->cl_serverscope,
5530                                              res.server_scope)) {
5531                         dprintk("%s: server_scope mismatch detected\n",
5532                                 __func__);
5533                         set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5534                         kfree(clp->cl_serverscope);
5535                         clp->cl_serverscope = NULL;
5536                 }
5537
5538                 if (clp->cl_serverscope == NULL) {
5539                         clp->cl_serverscope = res.server_scope;
5540                         goto out;
5541                 }
5542         } else
5543                 kfree(res.impl_id);
5544
5545 out_server_owner:
5546         kfree(res.server_owner);
5547 out_server_scope:
5548         kfree(res.server_scope);
5549 out:
5550         if (clp->cl_implid != NULL)
5551                 dprintk("NFS reply exchange_id: Server Implementation ID: "
5552                         "domain: %s, name: %s, date: %llu,%u\n",
5553                         clp->cl_implid->domain, clp->cl_implid->name,
5554                         clp->cl_implid->date.seconds,
5555                         clp->cl_implid->date.nseconds);
5556         dprintk("NFS reply exchange_id: %d\n", status);
5557         return status;
5558 }
5559
5560 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5561                 struct rpc_cred *cred)
5562 {
5563         struct rpc_message msg = {
5564                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5565                 .rpc_argp = clp,
5566                 .rpc_cred = cred,
5567         };
5568         int status;
5569
5570         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5571         if (status)
5572                 dprintk("NFS: Got error %d from the server %s on "
5573                         "DESTROY_CLIENTID.", status, clp->cl_hostname);
5574         return status;
5575 }
5576
5577 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5578                 struct rpc_cred *cred)
5579 {
5580         unsigned int loop;
5581         int ret;
5582
5583         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5584                 ret = _nfs4_proc_destroy_clientid(clp, cred);
5585                 switch (ret) {
5586                 case -NFS4ERR_DELAY:
5587                 case -NFS4ERR_CLIENTID_BUSY:
5588                         ssleep(1);
5589                         break;
5590                 default:
5591                         return ret;
5592                 }
5593         }
5594         return 0;
5595 }
5596
5597 int nfs4_destroy_clientid(struct nfs_client *clp)
5598 {
5599         struct rpc_cred *cred;
5600         int ret = 0;
5601
5602         if (clp->cl_mvops->minor_version < 1)
5603                 goto out;
5604         if (clp->cl_exchange_flags == 0)
5605                 goto out;
5606         if (clp->cl_preserve_clid)
5607                 goto out;
5608         cred = nfs4_get_exchange_id_cred(clp);
5609         ret = nfs4_proc_destroy_clientid(clp, cred);
5610         if (cred)
5611                 put_rpccred(cred);
5612         switch (ret) {
5613         case 0:
5614         case -NFS4ERR_STALE_CLIENTID:
5615                 clp->cl_exchange_flags = 0;
5616         }
5617 out:
5618         return ret;
5619 }
5620
5621 struct nfs4_get_lease_time_data {
5622         struct nfs4_get_lease_time_args *args;
5623         struct nfs4_get_lease_time_res *res;
5624         struct nfs_client *clp;
5625 };
5626
5627 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5628                                         void *calldata)
5629 {
5630         int ret;
5631         struct nfs4_get_lease_time_data *data =
5632                         (struct nfs4_get_lease_time_data *)calldata;
5633
5634         dprintk("--> %s\n", __func__);
5635         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5636         /* just setup sequence, do not trigger session recovery
5637            since we're invoked within one */
5638         ret = nfs41_setup_sequence(data->clp->cl_session,
5639                                    &data->args->la_seq_args,
5640                                    &data->res->lr_seq_res, task);
5641
5642         if (ret != -EAGAIN)
5643                 rpc_call_start(task);
5644         dprintk("<-- %s\n", __func__);
5645 }
5646
5647 /*
5648  * Called from nfs4_state_manager thread for session setup, so don't recover
5649  * from sequence operation or clientid errors.
5650  */
5651 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5652 {
5653         struct nfs4_get_lease_time_data *data =
5654                         (struct nfs4_get_lease_time_data *)calldata;
5655
5656         dprintk("--> %s\n", __func__);
5657         if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5658                 return;
5659         switch (task->tk_status) {
5660         case -NFS4ERR_DELAY:
5661         case -NFS4ERR_GRACE:
5662                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5663                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5664                 task->tk_status = 0;
5665                 /* fall through */
5666         case -NFS4ERR_RETRY_UNCACHED_REP:
5667                 rpc_restart_call_prepare(task);
5668                 return;
5669         }
5670         dprintk("<-- %s\n", __func__);
5671 }
5672
5673 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5674         .rpc_call_prepare = nfs4_get_lease_time_prepare,
5675         .rpc_call_done = nfs4_get_lease_time_done,
5676 };
5677
5678 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5679 {
5680         struct rpc_task *task;
5681         struct nfs4_get_lease_time_args args;
5682         struct nfs4_get_lease_time_res res = {
5683                 .lr_fsinfo = fsinfo,
5684         };
5685         struct nfs4_get_lease_time_data data = {
5686                 .args = &args,
5687                 .res = &res,
5688                 .clp = clp,
5689         };
5690         struct rpc_message msg = {
5691                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5692                 .rpc_argp = &args,
5693                 .rpc_resp = &res,
5694         };
5695         struct rpc_task_setup task_setup = {
5696                 .rpc_client = clp->cl_rpcclient,
5697                 .rpc_message = &msg,
5698                 .callback_ops = &nfs4_get_lease_time_ops,
5699                 .callback_data = &data,
5700                 .flags = RPC_TASK_TIMEOUT,
5701         };
5702         int status;
5703
5704         nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5705         dprintk("--> %s\n", __func__);
5706         task = rpc_run_task(&task_setup);
5707
5708         if (IS_ERR(task))
5709                 status = PTR_ERR(task);
5710         else {
5711                 status = task->tk_status;
5712                 rpc_put_task(task);
5713         }
5714         dprintk("<-- %s return %d\n", __func__, status);
5715
5716         return status;
5717 }
5718
5719 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5720                  u32 max_reqs, u32 ivalue)
5721 {
5722         if (max_reqs <= tbl->max_slots)
5723                 return 0;
5724         if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
5725                 return 0;
5726         return -ENOMEM;
5727 }
5728
5729 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5730                 u32 server_highest_slotid,
5731                 u32 ivalue)
5732 {
5733         struct nfs4_slot **p;
5734
5735         nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5736         p = &tbl->slots;
5737         while (*p) {
5738                 (*p)->seq_nr = ivalue;
5739                 p = &(*p)->next;
5740         }
5741         tbl->highest_used_slotid = NFS4_NO_SLOT;
5742         tbl->target_highest_slotid = server_highest_slotid;
5743         tbl->server_highest_slotid = server_highest_slotid;
5744         tbl->max_slotid = server_highest_slotid;
5745 }
5746
5747 /*
5748  * (re)Initialise a slot table
5749  */
5750 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5751                 u32 max_reqs, u32 ivalue)
5752 {
5753         int ret;
5754
5755         dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5756                 max_reqs, tbl->max_slots);
5757
5758         if (max_reqs > NFS4_MAX_SLOT_TABLE)
5759                 max_reqs = NFS4_MAX_SLOT_TABLE;
5760
5761         ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5762         if (ret)
5763                 goto out;
5764
5765         spin_lock(&tbl->slot_tbl_lock);
5766         nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5767         spin_unlock(&tbl->slot_tbl_lock);
5768
5769         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5770                 tbl, tbl->slots, tbl->max_slots);
5771 out:
5772         dprintk("<-- %s: return %d\n", __func__, ret);
5773         return ret;
5774 }
5775
5776 /* Destroy the slot table */
5777 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5778 {
5779         nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5780         nfs4_shrink_slot_table(&session->bc_slot_table, 0);
5781 }
5782
5783 /*
5784  * Initialize or reset the forechannel and backchannel tables
5785  */
5786 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5787 {
5788         struct nfs4_slot_table *tbl;
5789         int status;
5790
5791         dprintk("--> %s\n", __func__);
5792         /* Fore channel */
5793         tbl = &ses->fc_slot_table;
5794         tbl->session = ses;
5795         status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5796         if (status) /* -ENOMEM */
5797                 return status;
5798         /* Back channel */
5799         tbl = &ses->bc_slot_table;
5800         tbl->session = ses;
5801         status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5802         if (status && tbl->slots == NULL)
5803                 /* Fore and back channel share a connection so get
5804                  * both slot tables or neither */
5805                 nfs4_destroy_slot_tables(ses);
5806         return status;
5807 }
5808
5809 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5810 {
5811         struct nfs4_session *session;
5812         struct nfs4_slot_table *tbl;
5813
5814         session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5815         if (!session)
5816                 return NULL;
5817
5818         tbl = &session->fc_slot_table;
5819         tbl->highest_used_slotid = NFS4_NO_SLOT;
5820         spin_lock_init(&tbl->slot_tbl_lock);
5821         rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5822         init_completion(&tbl->complete);
5823
5824         tbl = &session->bc_slot_table;
5825         tbl->highest_used_slotid = NFS4_NO_SLOT;
5826         spin_lock_init(&tbl->slot_tbl_lock);
5827         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5828         init_completion(&tbl->complete);
5829
5830         session->session_state = 1<<NFS4_SESSION_INITING;
5831
5832         session->clp = clp;
5833         return session;
5834 }
5835
5836 void nfs4_destroy_session(struct nfs4_session *session)
5837 {
5838         struct rpc_xprt *xprt;
5839         struct rpc_cred *cred;
5840
5841         cred = nfs4_get_exchange_id_cred(session->clp);
5842         nfs4_proc_destroy_session(session, cred);
5843         if (cred)
5844                 put_rpccred(cred);
5845
5846         rcu_read_lock();
5847         xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5848         rcu_read_unlock();
5849         dprintk("%s Destroy backchannel for xprt %p\n",
5850                 __func__, xprt);
5851         xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5852         nfs4_destroy_slot_tables(session);
5853         kfree(session);
5854 }
5855
5856 /*
5857  * Initialize the values to be used by the client in CREATE_SESSION
5858  * If nfs4_init_session set the fore channel request and response sizes,
5859  * use them.
5860  *
5861  * Set the back channel max_resp_sz_cached to zero to force the client to
5862  * always set csa_cachethis to FALSE because the current implementation
5863  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5864  */
5865 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5866 {
5867         struct nfs4_session *session = args->client->cl_session;
5868         unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5869                      mxresp_sz = session->fc_target_max_resp_sz;
5870
5871         if (mxrqst_sz == 0)
5872                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5873         if (mxresp_sz == 0)
5874                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5875         /* Fore channel attributes */
5876         args->fc_attrs.max_rqst_sz = mxrqst_sz;
5877         args->fc_attrs.max_resp_sz = mxresp_sz;
5878         args->fc_attrs.max_ops = NFS4_MAX_OPS;
5879         args->fc_attrs.max_reqs = max_session_slots;
5880
5881         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5882                 "max_ops=%u max_reqs=%u\n",
5883                 __func__,
5884                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5885                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5886
5887         /* Back channel attributes */
5888         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5889         args->bc_attrs.max_resp_sz = PAGE_SIZE;
5890         args->bc_attrs.max_resp_sz_cached = 0;
5891         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5892         args->bc_attrs.max_reqs = 1;
5893
5894         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5895                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5896                 __func__,
5897                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5898                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5899                 args->bc_attrs.max_reqs);
5900 }
5901
5902 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5903 {
5904         struct nfs4_channel_attrs *sent = &args->fc_attrs;
5905         struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5906
5907         if (rcvd->max_resp_sz > sent->max_resp_sz)
5908                 return -EINVAL;
5909         /*
5910          * Our requested max_ops is the minimum we need; we're not
5911          * prepared to break up compounds into smaller pieces than that.
5912          * So, no point even trying to continue if the server won't
5913          * cooperate:
5914          */
5915         if (rcvd->max_ops < sent->max_ops)
5916                 return -EINVAL;
5917         if (rcvd->max_reqs == 0)
5918                 return -EINVAL;
5919         if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5920                 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5921         return 0;
5922 }
5923
5924 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5925 {
5926         struct nfs4_channel_attrs *sent = &args->bc_attrs;
5927         struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5928
5929         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5930                 return -EINVAL;
5931         if (rcvd->max_resp_sz < sent->max_resp_sz)
5932                 return -EINVAL;
5933         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5934                 return -EINVAL;
5935         /* These would render the backchannel useless: */
5936         if (rcvd->max_ops != sent->max_ops)
5937                 return -EINVAL;
5938         if (rcvd->max_reqs != sent->max_reqs)
5939                 return -EINVAL;
5940         return 0;
5941 }
5942
5943 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5944                                      struct nfs4_session *session)
5945 {
5946         int ret;
5947
5948         ret = nfs4_verify_fore_channel_attrs(args, session);
5949         if (ret)
5950                 return ret;
5951         return nfs4_verify_back_channel_attrs(args, session);
5952 }
5953
5954 static int _nfs4_proc_create_session(struct nfs_client *clp,
5955                 struct rpc_cred *cred)
5956 {
5957         struct nfs4_session *session = clp->cl_session;
5958         struct nfs41_create_session_args args = {
5959                 .client = clp,
5960                 .cb_program = NFS4_CALLBACK,
5961         };
5962         struct nfs41_create_session_res res = {
5963                 .client = clp,
5964         };
5965         struct rpc_message msg = {
5966                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5967                 .rpc_argp = &args,
5968                 .rpc_resp = &res,
5969                 .rpc_cred = cred,
5970         };
5971         int status;
5972
5973         nfs4_init_channel_attrs(&args);
5974         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5975
5976         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5977
5978         if (!status) {
5979                 /* Verify the session's negotiated channel_attrs values */
5980                 status = nfs4_verify_channel_attrs(&args, session);
5981                 /* Increment the clientid slot sequence id */
5982                 clp->cl_seqid++;
5983         }
5984
5985         return status;
5986 }
5987
5988 /*
5989  * Issues a CREATE_SESSION operation to the server.
5990  * It is the responsibility of the caller to verify the session is
5991  * expired before calling this routine.
5992  */
5993 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5994 {
5995         int status;
5996         unsigned *ptr;
5997         struct nfs4_session *session = clp->cl_session;
5998
5999         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6000
6001         status = _nfs4_proc_create_session(clp, cred);
6002         if (status)
6003                 goto out;
6004
6005         /* Init or reset the session slot tables */
6006         status = nfs4_setup_session_slot_tables(session);
6007         dprintk("slot table setup returned %d\n", status);
6008         if (status)
6009                 goto out;
6010
6011         ptr = (unsigned *)&session->sess_id.data[0];
6012         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6013                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
6014 out:
6015         dprintk("<-- %s\n", __func__);
6016         return status;
6017 }
6018
6019 /*
6020  * Issue the over-the-wire RPC DESTROY_SESSION.
6021  * The caller must serialize access to this routine.
6022  */
6023 int nfs4_proc_destroy_session(struct nfs4_session *session,
6024                 struct rpc_cred *cred)
6025 {
6026         struct rpc_message msg = {
6027                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6028                 .rpc_argp = session,
6029                 .rpc_cred = cred,
6030         };
6031         int status = 0;
6032
6033         dprintk("--> nfs4_proc_destroy_session\n");
6034
6035         /* session is still being setup */
6036         if (session->clp->cl_cons_state != NFS_CS_READY)
6037                 return status;
6038
6039         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6040
6041         if (status)
6042                 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6043                         "Session has been destroyed regardless...\n", status);
6044
6045         dprintk("<-- nfs4_proc_destroy_session\n");
6046         return status;
6047 }
6048
6049 /*
6050  * With sessions, the client is not marked ready until after a
6051  * successful EXCHANGE_ID and CREATE_SESSION.
6052  *
6053  * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6054  * other versions of NFS can be tried.
6055  */
6056 static int nfs41_check_session_ready(struct nfs_client *clp)
6057 {
6058         int ret;
6059         
6060         if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6061                 ret = nfs4_client_recover_expired_lease(clp);
6062                 if (ret)
6063                         return ret;
6064         }
6065         if (clp->cl_cons_state < NFS_CS_READY)
6066                 return -EPROTONOSUPPORT;
6067         smp_rmb();
6068         return 0;
6069 }
6070
6071 int nfs4_init_session(struct nfs_server *server)
6072 {
6073         struct nfs_client *clp = server->nfs_client;
6074         struct nfs4_session *session;
6075         unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6076         unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
6077
6078         if (!nfs4_has_session(clp))
6079                 return 0;
6080
6081         if (server->rsize != 0)
6082                 target_max_resp_sz = server->rsize;
6083         target_max_resp_sz += nfs41_maxread_overhead;
6084
6085         if (server->wsize != 0)
6086                 target_max_rqst_sz = server->wsize;
6087         target_max_rqst_sz += nfs41_maxwrite_overhead;
6088
6089         session = clp->cl_session;
6090         spin_lock(&clp->cl_lock);
6091         if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6092                 /* Initialise targets and channel attributes */
6093                 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6094                 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6095                 session->fc_target_max_resp_sz = target_max_resp_sz;
6096                 session->fc_attrs.max_resp_sz = target_max_resp_sz;
6097         } else {
6098                 /* Just adjust the targets */
6099                 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6100                         session->fc_target_max_rqst_sz = target_max_rqst_sz;
6101                         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6102                 }
6103                 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6104                         session->fc_target_max_resp_sz = target_max_resp_sz;
6105                         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6106                 }
6107         }
6108         spin_unlock(&clp->cl_lock);
6109
6110         if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6111                 nfs4_schedule_lease_recovery(clp);
6112
6113         return nfs41_check_session_ready(clp);
6114 }
6115
6116 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6117 {
6118         struct nfs4_session *session = clp->cl_session;
6119         int ret;
6120
6121         spin_lock(&clp->cl_lock);
6122         if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6123                 /*
6124                  * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6125                  * DS lease to be equal to the MDS lease.
6126                  */
6127                 clp->cl_lease_time = lease_time;
6128                 clp->cl_last_renewal = jiffies;
6129         }
6130         spin_unlock(&clp->cl_lock);
6131
6132         ret = nfs41_check_session_ready(clp);
6133         if (ret)
6134                 return ret;
6135         /* Test for the DS role */
6136         if (!is_ds_client(clp))
6137                 return -ENODEV;
6138         return 0;
6139 }
6140 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6141
6142
6143 /*
6144  * Renew the cl_session lease.
6145  */
6146 struct nfs4_sequence_data {
6147         struct nfs_client *clp;
6148         struct nfs4_sequence_args args;
6149         struct nfs4_sequence_res res;
6150 };
6151
6152 static void nfs41_sequence_release(void *data)
6153 {
6154         struct nfs4_sequence_data *calldata = data;
6155         struct nfs_client *clp = calldata->clp;
6156
6157         if (atomic_read(&clp->cl_count) > 1)
6158                 nfs4_schedule_state_renewal(clp);
6159         nfs_put_client(clp);
6160         kfree(calldata);
6161 }
6162
6163 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6164 {
6165         switch(task->tk_status) {
6166         case -NFS4ERR_DELAY:
6167                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6168                 return -EAGAIN;
6169         default:
6170                 nfs4_schedule_lease_recovery(clp);
6171         }
6172         return 0;
6173 }
6174
6175 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6176 {
6177         struct nfs4_sequence_data *calldata = data;
6178         struct nfs_client *clp = calldata->clp;
6179
6180         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6181                 return;
6182
6183         if (task->tk_status < 0) {
6184                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6185                 if (atomic_read(&clp->cl_count) == 1)
6186                         goto out;
6187
6188                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6189                         rpc_restart_call_prepare(task);
6190                         return;
6191                 }
6192         }
6193         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6194 out:
6195         dprintk("<-- %s\n", __func__);
6196 }
6197
6198 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6199 {
6200         struct nfs4_sequence_data *calldata = data;
6201         struct nfs_client *clp = calldata->clp;
6202         struct nfs4_sequence_args *args;
6203         struct nfs4_sequence_res *res;
6204
6205         args = task->tk_msg.rpc_argp;
6206         res = task->tk_msg.rpc_resp;
6207
6208         if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6209                 return;
6210         rpc_call_start(task);
6211 }
6212
6213 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6214 {
6215         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6216         nfs41_sequence_prepare(task, data);
6217 }
6218
6219 static const struct rpc_call_ops nfs41_sequence_ops = {
6220         .rpc_call_done = nfs41_sequence_call_done,
6221         .rpc_call_prepare = nfs41_sequence_prepare,
6222         .rpc_release = nfs41_sequence_release,
6223 };
6224
6225 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6226         .rpc_call_done = nfs41_sequence_call_done,
6227         .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6228         .rpc_release = nfs41_sequence_release,
6229 };
6230
6231 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6232                                              const struct rpc_call_ops *seq_ops)
6233 {
6234         struct nfs4_sequence_data *calldata;
6235         struct rpc_message msg = {
6236                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6237                 .rpc_cred = cred,
6238         };
6239         struct rpc_task_setup task_setup_data = {
6240                 .rpc_client = clp->cl_rpcclient,
6241                 .rpc_message = &msg,
6242                 .callback_ops = seq_ops,
6243                 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6244         };
6245
6246         if (!atomic_inc_not_zero(&clp->cl_count))
6247                 return ERR_PTR(-EIO);
6248         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6249         if (calldata == NULL) {
6250                 nfs_put_client(clp);
6251                 return ERR_PTR(-ENOMEM);
6252         }
6253         nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6254         msg.rpc_argp = &calldata->args;
6255         msg.rpc_resp = &calldata->res;
6256         calldata->clp = clp;
6257         task_setup_data.callback_data = calldata;
6258
6259         return rpc_run_task(&task_setup_data);
6260 }
6261
6262 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6263 {
6264         struct rpc_task *task;
6265         int ret = 0;
6266
6267         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6268                 return 0;
6269         task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
6270         if (IS_ERR(task))
6271                 ret = PTR_ERR(task);
6272         else
6273                 rpc_put_task_async(task);
6274         dprintk("<-- %s status=%d\n", __func__, ret);
6275         return ret;
6276 }
6277
6278 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6279 {
6280         struct rpc_task *task;
6281         int ret;
6282
6283         task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
6284         if (IS_ERR(task)) {
6285                 ret = PTR_ERR(task);
6286                 goto out;
6287         }
6288         ret = rpc_wait_for_completion_task(task);
6289         if (!ret) {
6290                 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6291
6292                 if (task->tk_status == 0)
6293                         nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6294                 ret = task->tk_status;
6295         }
6296         rpc_put_task(task);
6297 out:
6298         dprintk("<-- %s status=%d\n", __func__, ret);
6299         return ret;
6300 }
6301
6302 struct nfs4_reclaim_complete_data {
6303         struct nfs_client *clp;
6304         struct nfs41_reclaim_complete_args arg;
6305         struct nfs41_reclaim_complete_res res;
6306 };
6307
6308 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6309 {
6310         struct nfs4_reclaim_complete_data *calldata = data;
6311
6312         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6313         if (nfs41_setup_sequence(calldata->clp->cl_session,
6314                                 &calldata->arg.seq_args,
6315                                 &calldata->res.seq_res, task))
6316                 return;
6317
6318         rpc_call_start(task);
6319 }
6320
6321 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6322 {
6323         switch(task->tk_status) {
6324         case 0:
6325         case -NFS4ERR_COMPLETE_ALREADY:
6326         case -NFS4ERR_WRONG_CRED: /* What to do here? */
6327                 break;
6328         case -NFS4ERR_DELAY:
6329                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6330                 /* fall through */
6331         case -NFS4ERR_RETRY_UNCACHED_REP:
6332                 return -EAGAIN;
6333         default:
6334                 nfs4_schedule_lease_recovery(clp);
6335         }
6336         return 0;
6337 }
6338
6339 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6340 {
6341         struct nfs4_reclaim_complete_data *calldata = data;
6342         struct nfs_client *clp = calldata->clp;
6343         struct nfs4_sequence_res *res = &calldata->res.seq_res;
6344
6345         dprintk("--> %s\n", __func__);
6346         if (!nfs41_sequence_done(task, res))
6347                 return;
6348
6349         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6350                 rpc_restart_call_prepare(task);
6351                 return;
6352         }
6353         dprintk("<-- %s\n", __func__);
6354 }
6355
6356 static void nfs4_free_reclaim_complete_data(void *data)
6357 {
6358         struct nfs4_reclaim_complete_data *calldata = data;
6359
6360         kfree(calldata);
6361 }
6362
6363 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6364         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6365         .rpc_call_done = nfs4_reclaim_complete_done,
6366         .rpc_release = nfs4_free_reclaim_complete_data,
6367 };
6368
6369 /*
6370  * Issue a global reclaim complete.
6371  */
6372 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6373 {
6374         struct nfs4_reclaim_complete_data *calldata;
6375         struct rpc_task *task;
6376         struct rpc_message msg = {
6377                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6378         };
6379         struct rpc_task_setup task_setup_data = {
6380                 .rpc_client = clp->cl_rpcclient,
6381                 .rpc_message = &msg,
6382                 .callback_ops = &nfs4_reclaim_complete_call_ops,
6383                 .flags = RPC_TASK_ASYNC,
6384         };
6385         int status = -ENOMEM;
6386
6387         dprintk("--> %s\n", __func__);
6388         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6389         if (calldata == NULL)
6390                 goto out;
6391         calldata->clp = clp;
6392         calldata->arg.one_fs = 0;
6393
6394         nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6395         msg.rpc_argp = &calldata->arg;
6396         msg.rpc_resp = &calldata->res;
6397         task_setup_data.callback_data = calldata;
6398         task = rpc_run_task(&task_setup_data);
6399         if (IS_ERR(task)) {
6400                 status = PTR_ERR(task);
6401                 goto out;
6402         }
6403         status = nfs4_wait_for_completion_rpc_task(task);
6404         if (status == 0)
6405                 status = task->tk_status;
6406         rpc_put_task(task);
6407         return 0;
6408 out:
6409         dprintk("<-- %s status=%d\n", __func__, status);
6410         return status;
6411 }
6412
6413 static void
6414 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6415 {
6416         struct nfs4_layoutget *lgp = calldata;
6417         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6418
6419         dprintk("--> %s\n", __func__);
6420         /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6421          * right now covering the LAYOUTGET we are about to send.
6422          * However, that is not so catastrophic, and there seems
6423          * to be no way to prevent it completely.
6424          */
6425         if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6426                                 &lgp->res.seq_res, task))
6427                 return;
6428         if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6429                                           NFS_I(lgp->args.inode)->layout,
6430                                           lgp->args.ctx->state)) {
6431                 rpc_exit(task, NFS4_OK);
6432                 return;
6433         }
6434         rpc_call_start(task);
6435 }
6436
6437 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6438 {
6439         struct nfs4_layoutget *lgp = calldata;
6440         struct inode *inode = lgp->args.inode;
6441         struct nfs_server *server = NFS_SERVER(inode);
6442         struct pnfs_layout_hdr *lo;
6443         struct nfs4_state *state = NULL;
6444
6445         dprintk("--> %s\n", __func__);
6446
6447         if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6448                 goto out;
6449
6450         switch (task->tk_status) {
6451         case 0:
6452                 goto out;
6453         case -NFS4ERR_LAYOUTTRYLATER:
6454         case -NFS4ERR_RECALLCONFLICT:
6455                 task->tk_status = -NFS4ERR_DELAY;
6456                 break;
6457         case -NFS4ERR_EXPIRED:
6458         case -NFS4ERR_BAD_STATEID:
6459                 spin_lock(&inode->i_lock);
6460                 lo = NFS_I(inode)->layout;
6461                 if (!lo || list_empty(&lo->plh_segs)) {
6462                         spin_unlock(&inode->i_lock);
6463                         /* If the open stateid was bad, then recover it. */
6464                         state = lgp->args.ctx->state;
6465                 } else {
6466                         LIST_HEAD(head);
6467
6468                         pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6469                         spin_unlock(&inode->i_lock);
6470                         /* Mark the bad layout state as invalid, then
6471                          * retry using the open stateid. */
6472                         pnfs_free_lseg_list(&head);
6473                 }
6474         }
6475         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6476                 rpc_restart_call_prepare(task);
6477 out:
6478         dprintk("<-- %s\n", __func__);
6479 }
6480
6481 static size_t max_response_pages(struct nfs_server *server)
6482 {
6483         u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6484         return nfs_page_array_len(0, max_resp_sz);
6485 }
6486
6487 static void nfs4_free_pages(struct page **pages, size_t size)
6488 {
6489         int i;
6490
6491         if (!pages)
6492                 return;
6493
6494         for (i = 0; i < size; i++) {
6495                 if (!pages[i])
6496                         break;
6497                 __free_page(pages[i]);
6498         }
6499         kfree(pages);
6500 }
6501
6502 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6503 {
6504         struct page **pages;
6505         int i;
6506
6507         pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6508         if (!pages) {
6509                 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6510                 return NULL;
6511         }
6512
6513         for (i = 0; i < size; i++) {
6514                 pages[i] = alloc_page(gfp_flags);
6515                 if (!pages[i]) {
6516                         dprintk("%s: failed to allocate page\n", __func__);
6517                         nfs4_free_pages(pages, size);
6518                         return NULL;
6519                 }
6520         }
6521
6522         return pages;
6523 }
6524
6525 static void nfs4_layoutget_release(void *calldata)
6526 {
6527         struct nfs4_layoutget *lgp = calldata;
6528         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6529         size_t max_pages = max_response_pages(server);
6530
6531         dprintk("--> %s\n", __func__);
6532         nfs4_free_pages(lgp->args.layout.pages, max_pages);
6533         put_nfs_open_context(lgp->args.ctx);
6534         kfree(calldata);
6535         dprintk("<-- %s\n", __func__);
6536 }
6537
6538 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6539         .rpc_call_prepare = nfs4_layoutget_prepare,
6540         .rpc_call_done = nfs4_layoutget_done,
6541         .rpc_release = nfs4_layoutget_release,
6542 };
6543
6544 struct pnfs_layout_segment *
6545 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6546 {
6547         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6548         size_t max_pages = max_response_pages(server);
6549         struct rpc_task *task;
6550         struct rpc_message msg = {
6551                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6552                 .rpc_argp = &lgp->args,
6553                 .rpc_resp = &lgp->res,
6554         };
6555         struct rpc_task_setup task_setup_data = {
6556                 .rpc_client = server->client,
6557                 .rpc_message = &msg,
6558                 .callback_ops = &nfs4_layoutget_call_ops,
6559                 .callback_data = lgp,
6560                 .flags = RPC_TASK_ASYNC,
6561         };
6562         struct pnfs_layout_segment *lseg = NULL;
6563         int status = 0;
6564
6565         dprintk("--> %s\n", __func__);
6566
6567         lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6568         if (!lgp->args.layout.pages) {
6569                 nfs4_layoutget_release(lgp);
6570                 return ERR_PTR(-ENOMEM);
6571         }
6572         lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6573
6574         lgp->res.layoutp = &lgp->args.layout;
6575         lgp->res.seq_res.sr_slot = NULL;
6576         nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6577         task = rpc_run_task(&task_setup_data);
6578         if (IS_ERR(task))
6579                 return ERR_CAST(task);
6580         status = nfs4_wait_for_completion_rpc_task(task);
6581         if (status == 0)
6582                 status = task->tk_status;
6583         if (status == 0)
6584                 lseg = pnfs_layout_process(lgp);
6585         rpc_put_task(task);
6586         dprintk("<-- %s status=%d\n", __func__, status);
6587         if (status)
6588                 return ERR_PTR(status);
6589         return lseg;
6590 }
6591
6592 static void
6593 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6594 {
6595         struct nfs4_layoutreturn *lrp = calldata;
6596
6597         dprintk("--> %s\n", __func__);
6598         if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6599                                 &lrp->res.seq_res, task))
6600                 return;
6601         rpc_call_start(task);
6602 }
6603
6604 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6605 {
6606         struct nfs4_layoutreturn *lrp = calldata;
6607         struct nfs_server *server;
6608
6609         dprintk("--> %s\n", __func__);
6610
6611         if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6612                 return;
6613
6614         server = NFS_SERVER(lrp->args.inode);
6615         if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6616                 rpc_restart_call_prepare(task);
6617                 return;
6618         }
6619         dprintk("<-- %s\n", __func__);
6620 }
6621
6622 static void nfs4_layoutreturn_release(void *calldata)
6623 {
6624         struct nfs4_layoutreturn *lrp = calldata;
6625         struct pnfs_layout_hdr *lo = lrp->args.layout;
6626
6627         dprintk("--> %s\n", __func__);
6628         spin_lock(&lo->plh_inode->i_lock);
6629         if (lrp->res.lrs_present)
6630                 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6631         lo->plh_block_lgets--;
6632         spin_unlock(&lo->plh_inode->i_lock);
6633         pnfs_put_layout_hdr(lrp->args.layout);
6634         kfree(calldata);
6635         dprintk("<-- %s\n", __func__);
6636 }
6637
6638 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6639         .rpc_call_prepare = nfs4_layoutreturn_prepare,
6640         .rpc_call_done = nfs4_layoutreturn_done,
6641         .rpc_release = nfs4_layoutreturn_release,
6642 };
6643
6644 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6645 {
6646         struct rpc_task *task;
6647         struct rpc_message msg = {
6648                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6649                 .rpc_argp = &lrp->args,
6650                 .rpc_resp = &lrp->res,
6651         };
6652         struct rpc_task_setup task_setup_data = {
6653                 .rpc_client = lrp->clp->cl_rpcclient,
6654                 .rpc_message = &msg,
6655                 .callback_ops = &nfs4_layoutreturn_call_ops,
6656                 .callback_data = lrp,
6657         };
6658         int status;
6659
6660         dprintk("--> %s\n", __func__);
6661         nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6662         task = rpc_run_task(&task_setup_data);
6663         if (IS_ERR(task))
6664                 return PTR_ERR(task);
6665         status = task->tk_status;
6666         dprintk("<-- %s status=%d\n", __func__, status);
6667         rpc_put_task(task);
6668         return status;
6669 }
6670
6671 /*
6672  * Retrieve the list of Data Server devices from the MDS.
6673  */
6674 static int _nfs4_getdevicelist(struct nfs_server *server,
6675                                     const struct nfs_fh *fh,
6676                                     struct pnfs_devicelist *devlist)
6677 {
6678         struct nfs4_getdevicelist_args args = {
6679                 .fh = fh,
6680                 .layoutclass = server->pnfs_curr_ld->id,
6681         };
6682         struct nfs4_getdevicelist_res res = {
6683                 .devlist = devlist,
6684         };
6685         struct rpc_message msg = {
6686                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6687                 .rpc_argp = &args,
6688                 .rpc_resp = &res,
6689         };
6690         int status;
6691
6692         dprintk("--> %s\n", __func__);
6693         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6694                                 &res.seq_res, 0);
6695         dprintk("<-- %s status=%d\n", __func__, status);
6696         return status;
6697 }
6698
6699 int nfs4_proc_getdevicelist(struct nfs_server *server,
6700                             const struct nfs_fh *fh,
6701                             struct pnfs_devicelist *devlist)
6702 {
6703         struct nfs4_exception exception = { };
6704         int err;
6705
6706         do {
6707                 err = nfs4_handle_exception(server,
6708                                 _nfs4_getdevicelist(server, fh, devlist),
6709                                 &exception);
6710         } while (exception.retry);
6711
6712         dprintk("%s: err=%d, num_devs=%u\n", __func__,
6713                 err, devlist->num_devs);
6714
6715         return err;
6716 }
6717 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6718
6719 static int
6720 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6721 {
6722         struct nfs4_getdeviceinfo_args args = {
6723                 .pdev = pdev,
6724         };
6725         struct nfs4_getdeviceinfo_res res = {
6726                 .pdev = pdev,
6727         };
6728         struct rpc_message msg = {
6729                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6730                 .rpc_argp = &args,
6731                 .rpc_resp = &res,
6732         };
6733         int status;
6734
6735         dprintk("--> %s\n", __func__);
6736         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6737         dprintk("<-- %s status=%d\n", __func__, status);
6738
6739         return status;
6740 }
6741
6742 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6743 {
6744         struct nfs4_exception exception = { };
6745         int err;
6746
6747         do {
6748                 err = nfs4_handle_exception(server,
6749                                         _nfs4_proc_getdeviceinfo(server, pdev),
6750                                         &exception);
6751         } while (exception.retry);
6752         return err;
6753 }
6754 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6755
6756 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6757 {
6758         struct nfs4_layoutcommit_data *data = calldata;
6759         struct nfs_server *server = NFS_SERVER(data->args.inode);
6760
6761         if (nfs4_setup_sequence(server, &data->args.seq_args,
6762                                 &data->res.seq_res, task))
6763                 return;
6764         rpc_call_start(task);
6765 }
6766
6767 static void
6768 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6769 {
6770         struct nfs4_layoutcommit_data *data = calldata;
6771         struct nfs_server *server = NFS_SERVER(data->args.inode);
6772
6773         if (!nfs4_sequence_done(task, &data->res.seq_res))
6774                 return;
6775
6776         switch (task->tk_status) { /* Just ignore these failures */
6777         case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6778         case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
6779         case -NFS4ERR_BADLAYOUT:     /* no layout */
6780         case -NFS4ERR_GRACE:        /* loca_recalim always false */
6781                 task->tk_status = 0;
6782                 break;
6783         case 0:
6784                 nfs_post_op_update_inode_force_wcc(data->args.inode,
6785                                                    data->res.fattr);
6786                 break;
6787         default:
6788                 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6789                         rpc_restart_call_prepare(task);
6790                         return;
6791                 }
6792         }
6793 }
6794
6795 static void nfs4_layoutcommit_release(void *calldata)
6796 {
6797         struct nfs4_layoutcommit_data *data = calldata;
6798         struct pnfs_layout_segment *lseg, *tmp;
6799         unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6800
6801         pnfs_cleanup_layoutcommit(data);
6802         /* Matched by references in pnfs_set_layoutcommit */
6803         list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6804                 list_del_init(&lseg->pls_lc_list);
6805                 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6806                                        &lseg->pls_flags))
6807                         pnfs_put_lseg(lseg);
6808         }
6809
6810         clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6811         smp_mb__after_clear_bit();
6812         wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6813
6814         put_rpccred(data->cred);
6815         kfree(data);
6816 }
6817
6818 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6819         .rpc_call_prepare = nfs4_layoutcommit_prepare,
6820         .rpc_call_done = nfs4_layoutcommit_done,
6821         .rpc_release = nfs4_layoutcommit_release,
6822 };
6823
6824 int
6825 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6826 {
6827         struct rpc_message msg = {
6828                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6829                 .rpc_argp = &data->args,
6830                 .rpc_resp = &data->res,
6831                 .rpc_cred = data->cred,
6832         };
6833         struct rpc_task_setup task_setup_data = {
6834                 .task = &data->task,
6835                 .rpc_client = NFS_CLIENT(data->args.inode),
6836                 .rpc_message = &msg,
6837                 .callback_ops = &nfs4_layoutcommit_ops,
6838                 .callback_data = data,
6839                 .flags = RPC_TASK_ASYNC,
6840         };
6841         struct rpc_task *task;
6842         int status = 0;
6843
6844         dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6845                 "lbw: %llu inode %lu\n",
6846                 data->task.tk_pid, sync,
6847                 data->args.lastbytewritten,
6848                 data->args.inode->i_ino);
6849
6850         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6851         task = rpc_run_task(&task_setup_data);
6852         if (IS_ERR(task))
6853                 return PTR_ERR(task);
6854         if (sync == false)
6855                 goto out;
6856         status = nfs4_wait_for_completion_rpc_task(task);
6857         if (status != 0)
6858                 goto out;
6859         status = task->tk_status;
6860 out:
6861         dprintk("%s: status %d\n", __func__, status);
6862         rpc_put_task(task);
6863         return status;
6864 }
6865
6866 static int
6867 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6868                     struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6869 {
6870         struct nfs41_secinfo_no_name_args args = {
6871                 .style = SECINFO_STYLE_CURRENT_FH,
6872         };
6873         struct nfs4_secinfo_res res = {
6874                 .flavors = flavors,
6875         };
6876         struct rpc_message msg = {
6877                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6878                 .rpc_argp = &args,
6879                 .rpc_resp = &res,
6880         };
6881         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6882 }
6883
6884 static int
6885 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6886                            struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6887 {
6888         struct nfs4_exception exception = { };
6889         int err;
6890         do {
6891                 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6892                 switch (err) {
6893                 case 0:
6894                 case -NFS4ERR_WRONGSEC:
6895                 case -NFS4ERR_NOTSUPP:
6896                         goto out;
6897                 default:
6898                         err = nfs4_handle_exception(server, err, &exception);
6899                 }
6900         } while (exception.retry);
6901 out:
6902         return err;
6903 }
6904
6905 static int
6906 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6907                     struct nfs_fsinfo *info)
6908 {
6909         int err;
6910         struct page *page;
6911         rpc_authflavor_t flavor;
6912         struct nfs4_secinfo_flavors *flavors;
6913
6914         page = alloc_page(GFP_KERNEL);
6915         if (!page) {
6916                 err = -ENOMEM;
6917                 goto out;
6918         }
6919
6920         flavors = page_address(page);
6921         err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6922
6923         /*
6924          * Fall back on "guess and check" method if
6925          * the server doesn't support SECINFO_NO_NAME
6926          */
6927         if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6928                 err = nfs4_find_root_sec(server, fhandle, info);
6929                 goto out_freepage;
6930         }
6931         if (err)
6932                 goto out_freepage;
6933
6934         flavor = nfs_find_best_sec(flavors);
6935         if (err == 0)
6936                 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6937
6938 out_freepage:
6939         put_page(page);
6940         if (err == -EACCES)
6941                 return -EPERM;
6942 out:
6943         return err;
6944 }
6945
6946 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6947 {
6948         int status;
6949         struct nfs41_test_stateid_args args = {
6950                 .stateid = stateid,
6951         };
6952         struct nfs41_test_stateid_res res;
6953         struct rpc_message msg = {
6954                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6955                 .rpc_argp = &args,
6956                 .rpc_resp = &res,
6957         };
6958
6959         dprintk("NFS call  test_stateid %p\n", stateid);
6960         nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6961         status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6962         if (status != NFS_OK) {
6963                 dprintk("NFS reply test_stateid: failed, %d\n", status);
6964                 return status;
6965         }
6966         dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6967         return -res.status;
6968 }
6969
6970 /**
6971  * nfs41_test_stateid - perform a TEST_STATEID operation
6972  *
6973  * @server: server / transport on which to perform the operation
6974  * @stateid: state ID to test
6975  *
6976  * Returns NFS_OK if the server recognizes that "stateid" is valid.
6977  * Otherwise a negative NFS4ERR value is returned if the operation
6978  * failed or the state ID is not currently valid.
6979  */
6980 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6981 {
6982         struct nfs4_exception exception = { };
6983         int err;
6984         do {
6985                 err = _nfs41_test_stateid(server, stateid);
6986                 if (err != -NFS4ERR_DELAY)
6987                         break;
6988                 nfs4_handle_exception(server, err, &exception);
6989         } while (exception.retry);
6990         return err;
6991 }
6992
6993 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6994 {
6995         struct nfs41_free_stateid_args args = {
6996                 .stateid = stateid,
6997         };
6998         struct nfs41_free_stateid_res res;
6999         struct rpc_message msg = {
7000                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7001                 .rpc_argp = &args,
7002                 .rpc_resp = &res,
7003         };
7004         int status;
7005
7006         dprintk("NFS call  free_stateid %p\n", stateid);
7007         nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
7008         status = nfs4_call_sync_sequence(server->client, server, &msg,
7009                                          &args.seq_args, &res.seq_res, 1);
7010         dprintk("NFS reply free_stateid: %d\n", status);
7011         return status;
7012 }
7013
7014 /**
7015  * nfs41_free_stateid - perform a FREE_STATEID operation
7016  *
7017  * @server: server / transport on which to perform the operation
7018  * @stateid: state ID to release
7019  *
7020  * Returns NFS_OK if the server freed "stateid".  Otherwise a
7021  * negative NFS4ERR value is returned.
7022  */
7023 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7024 {
7025         struct nfs4_exception exception = { };
7026         int err;
7027         do {
7028                 err = _nfs4_free_stateid(server, stateid);
7029                 if (err != -NFS4ERR_DELAY)
7030                         break;
7031                 nfs4_handle_exception(server, err, &exception);
7032         } while (exception.retry);
7033         return err;
7034 }
7035
7036 static bool nfs41_match_stateid(const nfs4_stateid *s1,
7037                 const nfs4_stateid *s2)
7038 {
7039         if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7040                 return false;
7041
7042         if (s1->seqid == s2->seqid)
7043                 return true;
7044         if (s1->seqid == 0 || s2->seqid == 0)
7045                 return true;
7046
7047         return false;
7048 }
7049
7050 #endif /* CONFIG_NFS_V4_1 */
7051
7052 static bool nfs4_match_stateid(const nfs4_stateid *s1,
7053                 const nfs4_stateid *s2)
7054 {
7055         return nfs4_stateid_match(s1, s2);
7056 }
7057
7058
7059 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7060         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7061         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7062         .recover_open   = nfs4_open_reclaim,
7063         .recover_lock   = nfs4_lock_reclaim,
7064         .establish_clid = nfs4_init_clientid,
7065         .get_clid_cred  = nfs4_get_setclientid_cred,
7066         .detect_trunking = nfs40_discover_server_trunking,
7067 };
7068
7069 #if defined(CONFIG_NFS_V4_1)
7070 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7071         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7072         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7073         .recover_open   = nfs4_open_reclaim,
7074         .recover_lock   = nfs4_lock_reclaim,
7075         .establish_clid = nfs41_init_clientid,
7076         .get_clid_cred  = nfs4_get_exchange_id_cred,
7077         .reclaim_complete = nfs41_proc_reclaim_complete,
7078         .detect_trunking = nfs41_discover_server_trunking,
7079 };
7080 #endif /* CONFIG_NFS_V4_1 */
7081
7082 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7083         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7084         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7085         .recover_open   = nfs4_open_expired,
7086         .recover_lock   = nfs4_lock_expired,
7087         .establish_clid = nfs4_init_clientid,
7088         .get_clid_cred  = nfs4_get_setclientid_cred,
7089 };
7090
7091 #if defined(CONFIG_NFS_V4_1)
7092 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7093         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7094         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7095         .recover_open   = nfs41_open_expired,
7096         .recover_lock   = nfs41_lock_expired,
7097         .establish_clid = nfs41_init_clientid,
7098         .get_clid_cred  = nfs4_get_exchange_id_cred,
7099 };
7100 #endif /* CONFIG_NFS_V4_1 */
7101
7102 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7103         .sched_state_renewal = nfs4_proc_async_renew,
7104         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7105         .renew_lease = nfs4_proc_renew,
7106 };
7107
7108 #if defined(CONFIG_NFS_V4_1)
7109 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7110         .sched_state_renewal = nfs41_proc_async_sequence,
7111         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7112         .renew_lease = nfs4_proc_sequence,
7113 };
7114 #endif
7115
7116 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7117         .minor_version = 0,
7118         .call_sync = _nfs4_call_sync,
7119         .match_stateid = nfs4_match_stateid,
7120         .find_root_sec = nfs4_find_root_sec,
7121         .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7122         .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7123         .state_renewal_ops = &nfs40_state_renewal_ops,
7124 };
7125
7126 #if defined(CONFIG_NFS_V4_1)
7127 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7128         .minor_version = 1,
7129         .call_sync = _nfs4_call_sync_session,
7130         .match_stateid = nfs41_match_stateid,
7131         .find_root_sec = nfs41_find_root_sec,
7132         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7133         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7134         .state_renewal_ops = &nfs41_state_renewal_ops,
7135 };
7136 #endif
7137
7138 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7139         [0] = &nfs_v4_0_minor_ops,
7140 #if defined(CONFIG_NFS_V4_1)
7141         [1] = &nfs_v4_1_minor_ops,
7142 #endif
7143 };
7144
7145 const struct inode_operations nfs4_dir_inode_operations = {
7146         .create         = nfs_create,
7147         .lookup         = nfs_lookup,
7148         .atomic_open    = nfs_atomic_open,
7149         .link           = nfs_link,
7150         .unlink         = nfs_unlink,
7151         .symlink        = nfs_symlink,
7152         .mkdir          = nfs_mkdir,
7153         .rmdir          = nfs_rmdir,
7154         .mknod          = nfs_mknod,
7155         .rename         = nfs_rename,
7156         .permission     = nfs_permission,
7157         .getattr        = nfs_getattr,
7158         .setattr        = nfs_setattr,
7159         .getxattr       = generic_getxattr,
7160         .setxattr       = generic_setxattr,
7161         .listxattr      = generic_listxattr,
7162         .removexattr    = generic_removexattr,
7163 };
7164
7165 static const struct inode_operations nfs4_file_inode_operations = {
7166         .permission     = nfs_permission,
7167         .getattr        = nfs_getattr,
7168         .setattr        = nfs_setattr,
7169         .getxattr       = generic_getxattr,
7170         .setxattr       = generic_setxattr,
7171         .listxattr      = generic_listxattr,
7172         .removexattr    = generic_removexattr,
7173 };
7174
7175 const struct nfs_rpc_ops nfs_v4_clientops = {
7176         .version        = 4,                    /* protocol version */
7177         .dentry_ops     = &nfs4_dentry_operations,
7178         .dir_inode_ops  = &nfs4_dir_inode_operations,
7179         .file_inode_ops = &nfs4_file_inode_operations,
7180         .file_ops       = &nfs4_file_operations,
7181         .getroot        = nfs4_proc_get_root,
7182         .submount       = nfs4_submount,
7183         .try_mount      = nfs4_try_mount,
7184         .getattr        = nfs4_proc_getattr,
7185         .setattr        = nfs4_proc_setattr,
7186         .lookup         = nfs4_proc_lookup,
7187         .access         = nfs4_proc_access,
7188         .readlink       = nfs4_proc_readlink,
7189         .create         = nfs4_proc_create,
7190         .remove         = nfs4_proc_remove,
7191         .unlink_setup   = nfs4_proc_unlink_setup,
7192         .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7193         .unlink_done    = nfs4_proc_unlink_done,
7194         .rename         = nfs4_proc_rename,
7195         .rename_setup   = nfs4_proc_rename_setup,
7196         .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7197         .rename_done    = nfs4_proc_rename_done,
7198         .link           = nfs4_proc_link,
7199         .symlink        = nfs4_proc_symlink,
7200         .mkdir          = nfs4_proc_mkdir,
7201         .rmdir          = nfs4_proc_remove,
7202         .readdir        = nfs4_proc_readdir,
7203         .mknod          = nfs4_proc_mknod,
7204         .statfs         = nfs4_proc_statfs,
7205         .fsinfo         = nfs4_proc_fsinfo,
7206         .pathconf       = nfs4_proc_pathconf,
7207         .set_capabilities = nfs4_server_capabilities,
7208         .decode_dirent  = nfs4_decode_dirent,
7209         .read_setup     = nfs4_proc_read_setup,
7210         .read_pageio_init = pnfs_pageio_init_read,
7211         .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7212         .read_done      = nfs4_read_done,
7213         .write_setup    = nfs4_proc_write_setup,
7214         .write_pageio_init = pnfs_pageio_init_write,
7215         .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7216         .write_done     = nfs4_write_done,
7217         .commit_setup   = nfs4_proc_commit_setup,
7218         .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7219         .commit_done    = nfs4_commit_done,
7220         .lock           = nfs4_proc_lock,
7221         .clear_acl_cache = nfs4_zap_acl_attr,
7222         .close_context  = nfs4_close_context,
7223         .open_context   = nfs4_atomic_open,
7224         .have_delegation = nfs4_have_delegation,
7225         .return_delegation = nfs4_inode_return_delegation,
7226         .alloc_client   = nfs4_alloc_client,
7227         .init_client    = nfs4_init_client,
7228         .free_client    = nfs4_free_client,
7229         .create_server  = nfs4_create_server,
7230         .clone_server   = nfs_clone_server,
7231 };
7232
7233 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7234         .prefix = XATTR_NAME_NFSV4_ACL,
7235         .list   = nfs4_xattr_list_nfs4_acl,
7236         .get    = nfs4_xattr_get_nfs4_acl,
7237         .set    = nfs4_xattr_set_nfs4_acl,
7238 };
7239
7240 const struct xattr_handler *nfs4_xattr_handlers[] = {
7241         &nfs4_xattr_nfs4_acl_handler,
7242         NULL
7243 };
7244
7245 /*
7246  * Local variables:
7247  *  c-basic-offset: 8
7248  * End:
7249  */