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