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