~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfs/nfs4proc.c

Version: ~ [ linux-5.5 ] ~ [ linux-5.4.15 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.98 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.167 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.211 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.211 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/file.h>
 42 #include <linux/string.h>
 43 #include <linux/ratelimit.h>
 44 #include <linux/printk.h>
 45 #include <linux/slab.h>
 46 #include <linux/sunrpc/clnt.h>
 47 #include <linux/nfs.h>
 48 #include <linux/nfs4.h>
 49 #include <linux/nfs_fs.h>
 50 #include <linux/nfs_page.h>
 51 #include <linux/nfs_mount.h>
 52 #include <linux/namei.h>
 53 #include <linux/mount.h>
 54 #include <linux/module.h>
 55 #include <linux/nfs_idmap.h>
 56 #include <linux/xattr.h>
 57 #include <linux/utsname.h>
 58 #include <linux/freezer.h>
 59 
 60 #include "nfs4_fs.h"
 61 #include "delegation.h"
 62 #include "internal.h"
 63 #include "iostat.h"
 64 #include "callback.h"
 65 #include "pnfs.h"
 66 #include "netns.h"
 67 #include "nfs4session.h"
 68 #include "fscache.h"
 69 
 70 #include "nfs4trace.h"
 71 
 72 #define NFSDBG_FACILITY         NFSDBG_PROC
 73 
 74 #define NFS4_POLL_RETRY_MIN     (HZ/10)
 75 #define NFS4_POLL_RETRY_MAX     (15*HZ)
 76 
 77 struct nfs4_opendata;
 78 static int _nfs4_proc_open(struct nfs4_opendata *data);
 79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
 80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
 81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
 82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
 83 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
 84 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
 85 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
 86                             struct nfs_fattr *fattr, struct iattr *sattr,
 87                             struct nfs4_state *state, struct nfs4_label *ilabel,
 88                             struct nfs4_label *olabel);
 89 #ifdef CONFIG_NFS_V4_1
 90 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
 91                 struct rpc_cred *);
 92 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
 93                 struct rpc_cred *);
 94 #endif
 95 
 96 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
 97 static inline struct nfs4_label *
 98 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
 99         struct iattr *sattr, struct nfs4_label *label)
100 {
101         int err;
102 
103         if (label == NULL)
104                 return NULL;
105 
106         if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107                 return NULL;
108 
109         err = security_dentry_init_security(dentry, sattr->ia_mode,
110                                 &dentry->d_name, (void **)&label->label, &label->len);
111         if (err == 0)
112                 return label;
113 
114         return NULL;
115 }
116 static inline void
117 nfs4_label_release_security(struct nfs4_label *label)
118 {
119         if (label)
120                 security_release_secctx(label->label, label->len);
121 }
122 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123 {
124         if (label)
125                 return server->attr_bitmask;
126 
127         return server->attr_bitmask_nl;
128 }
129 #else
130 static inline struct nfs4_label *
131 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132         struct iattr *sattr, struct nfs4_label *l)
133 { return NULL; }
134 static inline void
135 nfs4_label_release_security(struct nfs4_label *label)
136 { return; }
137 static inline u32 *
138 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 { return server->attr_bitmask; }
140 #endif
141 
142 /* Prevent leaks of NFSv4 errors into userland */
143 static int nfs4_map_errors(int err)
144 {
145         if (err >= -1000)
146                 return err;
147         switch (err) {
148         case -NFS4ERR_RESOURCE:
149         case -NFS4ERR_LAYOUTTRYLATER:
150         case -NFS4ERR_RECALLCONFLICT:
151                 return -EREMOTEIO;
152         case -NFS4ERR_WRONGSEC:
153         case -NFS4ERR_WRONG_CRED:
154                 return -EPERM;
155         case -NFS4ERR_BADOWNER:
156         case -NFS4ERR_BADNAME:
157                 return -EINVAL;
158         case -NFS4ERR_SHARE_DENIED:
159                 return -EACCES;
160         case -NFS4ERR_MINOR_VERS_MISMATCH:
161                 return -EPROTONOSUPPORT;
162         case -NFS4ERR_ACCESS:
163                 return -EACCES;
164         case -NFS4ERR_FILE_OPEN:
165                 return -EBUSY;
166         default:
167                 dprintk("%s could not handle NFSv4 error %d\n",
168                                 __func__, -err);
169                 break;
170         }
171         return -EIO;
172 }
173 
174 /*
175  * This is our standard bitmap for GETATTR requests.
176  */
177 const u32 nfs4_fattr_bitmap[3] = {
178         FATTR4_WORD0_TYPE
179         | FATTR4_WORD0_CHANGE
180         | FATTR4_WORD0_SIZE
181         | FATTR4_WORD0_FSID
182         | FATTR4_WORD0_FILEID,
183         FATTR4_WORD1_MODE
184         | FATTR4_WORD1_NUMLINKS
185         | FATTR4_WORD1_OWNER
186         | FATTR4_WORD1_OWNER_GROUP
187         | FATTR4_WORD1_RAWDEV
188         | FATTR4_WORD1_SPACE_USED
189         | FATTR4_WORD1_TIME_ACCESS
190         | FATTR4_WORD1_TIME_METADATA
191         | FATTR4_WORD1_TIME_MODIFY,
192 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
193         FATTR4_WORD2_SECURITY_LABEL
194 #endif
195 };
196 
197 static const u32 nfs4_pnfs_open_bitmap[3] = {
198         FATTR4_WORD0_TYPE
199         | FATTR4_WORD0_CHANGE
200         | FATTR4_WORD0_SIZE
201         | FATTR4_WORD0_FSID
202         | FATTR4_WORD0_FILEID,
203         FATTR4_WORD1_MODE
204         | FATTR4_WORD1_NUMLINKS
205         | FATTR4_WORD1_OWNER
206         | FATTR4_WORD1_OWNER_GROUP
207         | FATTR4_WORD1_RAWDEV
208         | FATTR4_WORD1_SPACE_USED
209         | FATTR4_WORD1_TIME_ACCESS
210         | FATTR4_WORD1_TIME_METADATA
211         | FATTR4_WORD1_TIME_MODIFY,
212         FATTR4_WORD2_MDSTHRESHOLD
213 };
214 
215 static const u32 nfs4_open_noattr_bitmap[3] = {
216         FATTR4_WORD0_TYPE
217         | FATTR4_WORD0_CHANGE
218         | FATTR4_WORD0_FILEID,
219 };
220 
221 const u32 nfs4_statfs_bitmap[3] = {
222         FATTR4_WORD0_FILES_AVAIL
223         | FATTR4_WORD0_FILES_FREE
224         | FATTR4_WORD0_FILES_TOTAL,
225         FATTR4_WORD1_SPACE_AVAIL
226         | FATTR4_WORD1_SPACE_FREE
227         | FATTR4_WORD1_SPACE_TOTAL
228 };
229 
230 const u32 nfs4_pathconf_bitmap[3] = {
231         FATTR4_WORD0_MAXLINK
232         | FATTR4_WORD0_MAXNAME,
233         0
234 };
235 
236 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
237                         | FATTR4_WORD0_MAXREAD
238                         | FATTR4_WORD0_MAXWRITE
239                         | FATTR4_WORD0_LEASE_TIME,
240                         FATTR4_WORD1_TIME_DELTA
241                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
242                         FATTR4_WORD2_LAYOUT_BLKSIZE
243 };
244 
245 const u32 nfs4_fs_locations_bitmap[3] = {
246         FATTR4_WORD0_CHANGE
247         | FATTR4_WORD0_SIZE
248         | FATTR4_WORD0_FSID
249         | FATTR4_WORD0_FILEID
250         | FATTR4_WORD0_FS_LOCATIONS,
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 long nfs4_update_delay(long *timeout)
316 {
317         long ret;
318         if (!timeout)
319                 return NFS4_POLL_RETRY_MAX;
320         if (*timeout <= 0)
321                 *timeout = NFS4_POLL_RETRY_MIN;
322         if (*timeout > NFS4_POLL_RETRY_MAX)
323                 *timeout = NFS4_POLL_RETRY_MAX;
324         ret = *timeout;
325         *timeout <<= 1;
326         return ret;
327 }
328 
329 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
330 {
331         int res = 0;
332 
333         might_sleep();
334 
335         freezable_schedule_timeout_killable_unsafe(
336                 nfs4_update_delay(timeout));
337         if (fatal_signal_pending(current))
338                 res = -ERESTARTSYS;
339         return res;
340 }
341 
342 /* This is the error handling routine for processes that are allowed
343  * to sleep.
344  */
345 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
346 {
347         struct nfs_client *clp = server->nfs_client;
348         struct nfs4_state *state = exception->state;
349         struct inode *inode = exception->inode;
350         int ret = errorcode;
351 
352         exception->retry = 0;
353         switch(errorcode) {
354                 case 0:
355                         return 0;
356                 case -NFS4ERR_OPENMODE:
357                         if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
358                                 nfs4_inode_return_delegation(inode);
359                                 exception->retry = 1;
360                                 return 0;
361                         }
362                         if (state == NULL)
363                                 break;
364                         ret = nfs4_schedule_stateid_recovery(server, state);
365                         if (ret < 0)
366                                 break;
367                         goto wait_on_recovery;
368                 case -NFS4ERR_DELEG_REVOKED:
369                 case -NFS4ERR_ADMIN_REVOKED:
370                 case -NFS4ERR_BAD_STATEID:
371                         if (state == NULL)
372                                 break;
373                         ret = nfs4_schedule_stateid_recovery(server, state);
374                         if (ret < 0)
375                                 break;
376                         goto wait_on_recovery;
377                 case -NFS4ERR_EXPIRED:
378                         if (state != NULL) {
379                                 ret = nfs4_schedule_stateid_recovery(server, state);
380                                 if (ret < 0)
381                                         break;
382                         }
383                 case -NFS4ERR_STALE_STATEID:
384                 case -NFS4ERR_STALE_CLIENTID:
385                         nfs4_schedule_lease_recovery(clp);
386                         goto wait_on_recovery;
387                 case -NFS4ERR_MOVED:
388                         ret = nfs4_schedule_migration_recovery(server);
389                         if (ret < 0)
390                                 break;
391                         goto wait_on_recovery;
392                 case -NFS4ERR_LEASE_MOVED:
393                         nfs4_schedule_lease_moved_recovery(clp);
394                         goto wait_on_recovery;
395 #if defined(CONFIG_NFS_V4_1)
396                 case -NFS4ERR_BADSESSION:
397                 case -NFS4ERR_BADSLOT:
398                 case -NFS4ERR_BAD_HIGH_SLOT:
399                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
400                 case -NFS4ERR_DEADSESSION:
401                 case -NFS4ERR_SEQ_FALSE_RETRY:
402                 case -NFS4ERR_SEQ_MISORDERED:
403                         dprintk("%s ERROR: %d Reset session\n", __func__,
404                                 errorcode);
405                         nfs4_schedule_session_recovery(clp->cl_session, errorcode);
406                         goto wait_on_recovery;
407 #endif /* defined(CONFIG_NFS_V4_1) */
408                 case -NFS4ERR_FILE_OPEN:
409                         if (exception->timeout > HZ) {
410                                 /* We have retried a decent amount, time to
411                                  * fail
412                                  */
413                                 ret = -EBUSY;
414                                 break;
415                         }
416                 case -NFS4ERR_GRACE:
417                 case -NFS4ERR_DELAY:
418                         ret = nfs4_delay(server->client, &exception->timeout);
419                         if (ret != 0)
420                                 break;
421                 case -NFS4ERR_RETRY_UNCACHED_REP:
422                 case -NFS4ERR_OLD_STATEID:
423                         exception->retry = 1;
424                         break;
425                 case -NFS4ERR_BADOWNER:
426                         /* The following works around a Linux server bug! */
427                 case -NFS4ERR_BADNAME:
428                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430                                 exception->retry = 1;
431                                 printk(KERN_WARNING "NFS: v4 server %s "
432                                                 "does not accept raw "
433                                                 "uid/gids. "
434                                                 "Reenabling the idmapper.\n",
435                                                 server->nfs_client->cl_hostname);
436                         }
437         }
438         /* We failed to handle the error */
439         return nfs4_map_errors(ret);
440 wait_on_recovery:
441         ret = nfs4_wait_clnt_recover(clp);
442         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
443                 return -EIO;
444         if (ret == 0)
445                 exception->retry = 1;
446         return ret;
447 }
448 
449 /*
450  * Return 'true' if 'clp' is using an rpc_client that is integrity protected
451  * or 'false' otherwise.
452  */
453 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
454 {
455         rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
456 
457         if (flavor == RPC_AUTH_GSS_KRB5I ||
458             flavor == RPC_AUTH_GSS_KRB5P)
459                 return true;
460 
461         return false;
462 }
463 
464 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
465 {
466         spin_lock(&clp->cl_lock);
467         if (time_before(clp->cl_last_renewal,timestamp))
468                 clp->cl_last_renewal = timestamp;
469         spin_unlock(&clp->cl_lock);
470 }
471 
472 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
473 {
474         do_renew_lease(server->nfs_client, timestamp);
475 }
476 
477 struct nfs4_call_sync_data {
478         const struct nfs_server *seq_server;
479         struct nfs4_sequence_args *seq_args;
480         struct nfs4_sequence_res *seq_res;
481 };
482 
483 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
484                                struct nfs4_sequence_res *res, int cache_reply)
485 {
486         args->sa_slot = NULL;
487         args->sa_cache_this = cache_reply;
488         args->sa_privileged = 0;
489 
490         res->sr_slot = NULL;
491 }
492 
493 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
494 {
495         args->sa_privileged = 1;
496 }
497 
498 static int nfs40_setup_sequence(const struct nfs_server *server,
499                                 struct nfs4_sequence_args *args,
500                                 struct nfs4_sequence_res *res,
501                                 struct rpc_task *task)
502 {
503         struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
504         struct nfs4_slot *slot;
505 
506         /* slot already allocated? */
507         if (res->sr_slot != NULL)
508                 goto out_start;
509 
510         spin_lock(&tbl->slot_tbl_lock);
511         if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
512                 goto out_sleep;
513 
514         slot = nfs4_alloc_slot(tbl);
515         if (IS_ERR(slot)) {
516                 if (slot == ERR_PTR(-ENOMEM))
517                         task->tk_timeout = HZ >> 2;
518                 goto out_sleep;
519         }
520         spin_unlock(&tbl->slot_tbl_lock);
521 
522         args->sa_slot = slot;
523         res->sr_slot = slot;
524 
525 out_start:
526         rpc_call_start(task);
527         return 0;
528 
529 out_sleep:
530         if (args->sa_privileged)
531                 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
532                                 NULL, RPC_PRIORITY_PRIVILEGED);
533         else
534                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535         spin_unlock(&tbl->slot_tbl_lock);
536         return -EAGAIN;
537 }
538 
539 static int nfs40_sequence_done(struct rpc_task *task,
540                                struct nfs4_sequence_res *res)
541 {
542         struct nfs4_slot *slot = res->sr_slot;
543         struct nfs4_slot_table *tbl;
544 
545         if (slot == NULL)
546                 goto out;
547 
548         tbl = slot->table;
549         spin_lock(&tbl->slot_tbl_lock);
550         if (!nfs41_wake_and_assign_slot(tbl, slot))
551                 nfs4_free_slot(tbl, slot);
552         spin_unlock(&tbl->slot_tbl_lock);
553 
554         res->sr_slot = NULL;
555 out:
556         return 1;
557 }
558 
559 #if defined(CONFIG_NFS_V4_1)
560 
561 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
562 {
563         struct nfs4_session *session;
564         struct nfs4_slot_table *tbl;
565         struct nfs4_slot *slot = res->sr_slot;
566         bool send_new_highest_used_slotid = false;
567 
568         tbl = slot->table;
569         session = tbl->session;
570 
571         spin_lock(&tbl->slot_tbl_lock);
572         /* Be nice to the server: try to ensure that the last transmitted
573          * value for highest_user_slotid <= target_highest_slotid
574          */
575         if (tbl->highest_used_slotid > tbl->target_highest_slotid)
576                 send_new_highest_used_slotid = true;
577 
578         if (nfs41_wake_and_assign_slot(tbl, slot)) {
579                 send_new_highest_used_slotid = false;
580                 goto out_unlock;
581         }
582         nfs4_free_slot(tbl, slot);
583 
584         if (tbl->highest_used_slotid != NFS4_NO_SLOT)
585                 send_new_highest_used_slotid = false;
586 out_unlock:
587         spin_unlock(&tbl->slot_tbl_lock);
588         res->sr_slot = NULL;
589         if (send_new_highest_used_slotid)
590                 nfs41_server_notify_highest_slotid_update(session->clp);
591 }
592 
593 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
594 {
595         struct nfs4_session *session;
596         struct nfs4_slot *slot = res->sr_slot;
597         struct nfs_client *clp;
598         bool interrupted = false;
599         int ret = 1;
600 
601         if (slot == NULL)
602                 goto out_noaction;
603         /* don't increment the sequence number if the task wasn't sent */
604         if (!RPC_WAS_SENT(task))
605                 goto out;
606 
607         session = slot->table->session;
608 
609         if (slot->interrupted) {
610                 slot->interrupted = 0;
611                 interrupted = true;
612         }
613 
614         trace_nfs4_sequence_done(session, res);
615         /* Check the SEQUENCE operation status */
616         switch (res->sr_status) {
617         case 0:
618                 /* Update the slot's sequence and clientid lease timer */
619                 ++slot->seq_nr;
620                 clp = session->clp;
621                 do_renew_lease(clp, res->sr_timestamp);
622                 /* Check sequence flags */
623                 if (res->sr_status_flags != 0)
624                         nfs4_schedule_lease_recovery(clp);
625                 nfs41_update_target_slotid(slot->table, slot, res);
626                 break;
627         case 1:
628                 /*
629                  * sr_status remains 1 if an RPC level error occurred.
630                  * The server may or may not have processed the sequence
631                  * operation..
632                  * Mark the slot as having hosted an interrupted RPC call.
633                  */
634                 slot->interrupted = 1;
635                 goto out;
636         case -NFS4ERR_DELAY:
637                 /* The server detected a resend of the RPC call and
638                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
639                  * of RFC5661.
640                  */
641                 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
642                         __func__,
643                         slot->slot_nr,
644                         slot->seq_nr);
645                 goto out_retry;
646         case -NFS4ERR_BADSLOT:
647                 /*
648                  * The slot id we used was probably retired. Try again
649                  * using a different slot id.
650                  */
651                 goto retry_nowait;
652         case -NFS4ERR_SEQ_MISORDERED:
653                 /*
654                  * Was the last operation on this sequence interrupted?
655                  * If so, retry after bumping the sequence number.
656                  */
657                 if (interrupted) {
658                         ++slot->seq_nr;
659                         goto retry_nowait;
660                 }
661                 /*
662                  * Could this slot have been previously retired?
663                  * If so, then the server may be expecting seq_nr = 1!
664                  */
665                 if (slot->seq_nr != 1) {
666                         slot->seq_nr = 1;
667                         goto retry_nowait;
668                 }
669                 break;
670         case -NFS4ERR_SEQ_FALSE_RETRY:
671                 ++slot->seq_nr;
672                 goto retry_nowait;
673         default:
674                 /* Just update the slot sequence no. */
675                 ++slot->seq_nr;
676         }
677 out:
678         /* The session may be reset by one of the error handlers. */
679         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
680         nfs41_sequence_free_slot(res);
681 out_noaction:
682         return ret;
683 retry_nowait:
684         if (rpc_restart_call_prepare(task)) {
685                 task->tk_status = 0;
686                 ret = 0;
687         }
688         goto out;
689 out_retry:
690         if (!rpc_restart_call(task))
691                 goto out;
692         rpc_delay(task, NFS4_POLL_RETRY_MAX);
693         return 0;
694 }
695 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
696 
697 static int nfs4_sequence_done(struct rpc_task *task,
698                                struct nfs4_sequence_res *res)
699 {
700         if (res->sr_slot == NULL)
701                 return 1;
702         if (!res->sr_slot->table->session)
703                 return nfs40_sequence_done(task, res);
704         return nfs41_sequence_done(task, res);
705 }
706 
707 int nfs41_setup_sequence(struct nfs4_session *session,
708                                 struct nfs4_sequence_args *args,
709                                 struct nfs4_sequence_res *res,
710                                 struct rpc_task *task)
711 {
712         struct nfs4_slot *slot;
713         struct nfs4_slot_table *tbl;
714 
715         dprintk("--> %s\n", __func__);
716         /* slot already allocated? */
717         if (res->sr_slot != NULL)
718                 goto out_success;
719 
720         tbl = &session->fc_slot_table;
721 
722         task->tk_timeout = 0;
723 
724         spin_lock(&tbl->slot_tbl_lock);
725         if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
726             !args->sa_privileged) {
727                 /* The state manager will wait until the slot table is empty */
728                 dprintk("%s session is draining\n", __func__);
729                 goto out_sleep;
730         }
731 
732         slot = nfs4_alloc_slot(tbl);
733         if (IS_ERR(slot)) {
734                 /* If out of memory, try again in 1/4 second */
735                 if (slot == ERR_PTR(-ENOMEM))
736                         task->tk_timeout = HZ >> 2;
737                 dprintk("<-- %s: no free slots\n", __func__);
738                 goto out_sleep;
739         }
740         spin_unlock(&tbl->slot_tbl_lock);
741 
742         args->sa_slot = slot;
743 
744         dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
745                         slot->slot_nr, slot->seq_nr);
746 
747         res->sr_slot = slot;
748         res->sr_timestamp = jiffies;
749         res->sr_status_flags = 0;
750         /*
751          * sr_status is only set in decode_sequence, and so will remain
752          * set to 1 if an rpc level failure occurs.
753          */
754         res->sr_status = 1;
755         trace_nfs4_setup_sequence(session, args);
756 out_success:
757         rpc_call_start(task);
758         return 0;
759 out_sleep:
760         /* Privileged tasks are queued with top priority */
761         if (args->sa_privileged)
762                 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
763                                 NULL, RPC_PRIORITY_PRIVILEGED);
764         else
765                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
766         spin_unlock(&tbl->slot_tbl_lock);
767         return -EAGAIN;
768 }
769 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
770 
771 static int nfs4_setup_sequence(const struct nfs_server *server,
772                                struct nfs4_sequence_args *args,
773                                struct nfs4_sequence_res *res,
774                                struct rpc_task *task)
775 {
776         struct nfs4_session *session = nfs4_get_session(server);
777         int ret = 0;
778 
779         if (!session)
780                 return nfs40_setup_sequence(server, args, res, task);
781 
782         dprintk("--> %s clp %p session %p sr_slot %u\n",
783                 __func__, session->clp, session, res->sr_slot ?
784                         res->sr_slot->slot_nr : NFS4_NO_SLOT);
785 
786         ret = nfs41_setup_sequence(session, args, res, task);
787 
788         dprintk("<-- %s status=%d\n", __func__, ret);
789         return ret;
790 }
791 
792 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
793 {
794         struct nfs4_call_sync_data *data = calldata;
795         struct nfs4_session *session = nfs4_get_session(data->seq_server);
796 
797         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
798 
799         nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
800 }
801 
802 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
803 {
804         struct nfs4_call_sync_data *data = calldata;
805 
806         nfs41_sequence_done(task, data->seq_res);
807 }
808 
809 static const struct rpc_call_ops nfs41_call_sync_ops = {
810         .rpc_call_prepare = nfs41_call_sync_prepare,
811         .rpc_call_done = nfs41_call_sync_done,
812 };
813 
814 #else   /* !CONFIG_NFS_V4_1 */
815 
816 static int nfs4_setup_sequence(const struct nfs_server *server,
817                                struct nfs4_sequence_args *args,
818                                struct nfs4_sequence_res *res,
819                                struct rpc_task *task)
820 {
821         return nfs40_setup_sequence(server, args, res, task);
822 }
823 
824 static int nfs4_sequence_done(struct rpc_task *task,
825                                struct nfs4_sequence_res *res)
826 {
827         return nfs40_sequence_done(task, res);
828 }
829 
830 #endif  /* !CONFIG_NFS_V4_1 */
831 
832 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
833 {
834         struct nfs4_call_sync_data *data = calldata;
835         nfs4_setup_sequence(data->seq_server,
836                                 data->seq_args, data->seq_res, task);
837 }
838 
839 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
840 {
841         struct nfs4_call_sync_data *data = calldata;
842         nfs4_sequence_done(task, data->seq_res);
843 }
844 
845 static const struct rpc_call_ops nfs40_call_sync_ops = {
846         .rpc_call_prepare = nfs40_call_sync_prepare,
847         .rpc_call_done = nfs40_call_sync_done,
848 };
849 
850 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
851                                    struct nfs_server *server,
852                                    struct rpc_message *msg,
853                                    struct nfs4_sequence_args *args,
854                                    struct nfs4_sequence_res *res)
855 {
856         int ret;
857         struct rpc_task *task;
858         struct nfs_client *clp = server->nfs_client;
859         struct nfs4_call_sync_data data = {
860                 .seq_server = server,
861                 .seq_args = args,
862                 .seq_res = res,
863         };
864         struct rpc_task_setup task_setup = {
865                 .rpc_client = clnt,
866                 .rpc_message = msg,
867                 .callback_ops = clp->cl_mvops->call_sync_ops,
868                 .callback_data = &data
869         };
870 
871         task = rpc_run_task(&task_setup);
872         if (IS_ERR(task))
873                 ret = PTR_ERR(task);
874         else {
875                 ret = task->tk_status;
876                 rpc_put_task(task);
877         }
878         return ret;
879 }
880 
881 int nfs4_call_sync(struct rpc_clnt *clnt,
882                    struct nfs_server *server,
883                    struct rpc_message *msg,
884                    struct nfs4_sequence_args *args,
885                    struct nfs4_sequence_res *res,
886                    int cache_reply)
887 {
888         nfs4_init_sequence(args, res, cache_reply);
889         return nfs4_call_sync_sequence(clnt, server, msg, args, res);
890 }
891 
892 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
893 {
894         struct nfs_inode *nfsi = NFS_I(dir);
895 
896         spin_lock(&dir->i_lock);
897         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
898         if (!cinfo->atomic || cinfo->before != dir->i_version)
899                 nfs_force_lookup_revalidate(dir);
900         dir->i_version = cinfo->after;
901         nfs_fscache_invalidate(dir);
902         spin_unlock(&dir->i_lock);
903 }
904 
905 struct nfs4_opendata {
906         struct kref kref;
907         struct nfs_openargs o_arg;
908         struct nfs_openres o_res;
909         struct nfs_open_confirmargs c_arg;
910         struct nfs_open_confirmres c_res;
911         struct nfs4_string owner_name;
912         struct nfs4_string group_name;
913         struct nfs_fattr f_attr;
914         struct nfs4_label *f_label;
915         struct dentry *dir;
916         struct dentry *dentry;
917         struct nfs4_state_owner *owner;
918         struct nfs4_state *state;
919         struct iattr attrs;
920         unsigned long timestamp;
921         unsigned int rpc_done : 1;
922         unsigned int file_created : 1;
923         unsigned int is_recover : 1;
924         int rpc_status;
925         int cancelled;
926 };
927 
928 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
929                 int err, struct nfs4_exception *exception)
930 {
931         if (err != -EINVAL)
932                 return false;
933         if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
934                 return false;
935         server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
936         exception->retry = 1;
937         return true;
938 }
939 
940 static enum open_claim_type4
941 nfs4_map_atomic_open_claim(struct nfs_server *server,
942                 enum open_claim_type4 claim)
943 {
944         if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
945                 return claim;
946         switch (claim) {
947         default:
948                 return claim;
949         case NFS4_OPEN_CLAIM_FH:
950                 return NFS4_OPEN_CLAIM_NULL;
951         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
952                 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
953         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
954                 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
955         }
956 }
957 
958 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
959 {
960         p->o_res.f_attr = &p->f_attr;
961         p->o_res.f_label = p->f_label;
962         p->o_res.seqid = p->o_arg.seqid;
963         p->c_res.seqid = p->c_arg.seqid;
964         p->o_res.server = p->o_arg.server;
965         p->o_res.access_request = p->o_arg.access;
966         nfs_fattr_init(&p->f_attr);
967         nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
968 }
969 
970 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
971                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
972                 const struct iattr *attrs,
973                 struct nfs4_label *label,
974                 enum open_claim_type4 claim,
975                 gfp_t gfp_mask)
976 {
977         struct dentry *parent = dget_parent(dentry);
978         struct inode *dir = parent->d_inode;
979         struct nfs_server *server = NFS_SERVER(dir);
980         struct nfs4_opendata *p;
981 
982         p = kzalloc(sizeof(*p), gfp_mask);
983         if (p == NULL)
984                 goto err;
985 
986         p->f_label = nfs4_label_alloc(server, gfp_mask);
987         if (IS_ERR(p->f_label))
988                 goto err_free_p;
989 
990         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
991         if (p->o_arg.seqid == NULL)
992                 goto err_free_label;
993         nfs_sb_active(dentry->d_sb);
994         p->dentry = dget(dentry);
995         p->dir = parent;
996         p->owner = sp;
997         atomic_inc(&sp->so_count);
998         p->o_arg.open_flags = flags;
999         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1000         /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1001          * will return permission denied for all bits until close */
1002         if (!(flags & O_EXCL)) {
1003                 /* ask server to check for all possible rights as results
1004                  * are cached */
1005                 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1006                                   NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1007         }
1008         p->o_arg.clientid = server->nfs_client->cl_clientid;
1009         p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1010         p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1011         p->o_arg.name = &dentry->d_name;
1012         p->o_arg.server = server;
1013         p->o_arg.bitmask = nfs4_bitmask(server, label);
1014         p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1015         p->o_arg.label = label;
1016         p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1017         switch (p->o_arg.claim) {
1018         case NFS4_OPEN_CLAIM_NULL:
1019         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1020         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1021                 p->o_arg.fh = NFS_FH(dir);
1022                 break;
1023         case NFS4_OPEN_CLAIM_PREVIOUS:
1024         case NFS4_OPEN_CLAIM_FH:
1025         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1026         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1027                 p->o_arg.fh = NFS_FH(dentry->d_inode);
1028         }
1029         if (attrs != NULL && attrs->ia_valid != 0) {
1030                 __u32 verf[2];
1031 
1032                 p->o_arg.u.attrs = &p->attrs;
1033                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1034 
1035                 verf[0] = jiffies;
1036                 verf[1] = current->pid;
1037                 memcpy(p->o_arg.u.verifier.data, verf,
1038                                 sizeof(p->o_arg.u.verifier.data));
1039         }
1040         p->c_arg.fh = &p->o_res.fh;
1041         p->c_arg.stateid = &p->o_res.stateid;
1042         p->c_arg.seqid = p->o_arg.seqid;
1043         nfs4_init_opendata_res(p);
1044         kref_init(&p->kref);
1045         return p;
1046 
1047 err_free_label:
1048         nfs4_label_free(p->f_label);
1049 err_free_p:
1050         kfree(p);
1051 err:
1052         dput(parent);
1053         return NULL;
1054 }
1055 
1056 static void nfs4_opendata_free(struct kref *kref)
1057 {
1058         struct nfs4_opendata *p = container_of(kref,
1059                         struct nfs4_opendata, kref);
1060         struct super_block *sb = p->dentry->d_sb;
1061 
1062         nfs_free_seqid(p->o_arg.seqid);
1063         if (p->state != NULL)
1064                 nfs4_put_open_state(p->state);
1065         nfs4_put_state_owner(p->owner);
1066 
1067         nfs4_label_free(p->f_label);
1068 
1069         dput(p->dir);
1070         dput(p->dentry);
1071         nfs_sb_deactive(sb);
1072         nfs_fattr_free_names(&p->f_attr);
1073         kfree(p->f_attr.mdsthreshold);
1074         kfree(p);
1075 }
1076 
1077 static void nfs4_opendata_put(struct nfs4_opendata *p)
1078 {
1079         if (p != NULL)
1080                 kref_put(&p->kref, nfs4_opendata_free);
1081 }
1082 
1083 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1084 {
1085         int ret;
1086 
1087         ret = rpc_wait_for_completion_task(task);
1088         return ret;
1089 }
1090 
1091 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1092 {
1093         int ret = 0;
1094 
1095         if (open_mode & (O_EXCL|O_TRUNC))
1096                 goto out;
1097         switch (mode & (FMODE_READ|FMODE_WRITE)) {
1098                 case FMODE_READ:
1099                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1100                                 && state->n_rdonly != 0;
1101                         break;
1102                 case FMODE_WRITE:
1103                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1104                                 && state->n_wronly != 0;
1105                         break;
1106                 case FMODE_READ|FMODE_WRITE:
1107                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1108                                 && state->n_rdwr != 0;
1109         }
1110 out:
1111         return ret;
1112 }
1113 
1114 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1115 {
1116         if (delegation == NULL)
1117                 return 0;
1118         if ((delegation->type & fmode) != fmode)
1119                 return 0;
1120         if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1121                 return 0;
1122         nfs_mark_delegation_referenced(delegation);
1123         return 1;
1124 }
1125 
1126 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1127 {
1128         switch (fmode) {
1129                 case FMODE_WRITE:
1130                         state->n_wronly++;
1131                         break;
1132                 case FMODE_READ:
1133                         state->n_rdonly++;
1134                         break;
1135                 case FMODE_READ|FMODE_WRITE:
1136                         state->n_rdwr++;
1137         }
1138         nfs4_state_set_mode_locked(state, state->state | fmode);
1139 }
1140 
1141 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1142 {
1143         struct nfs_client *clp = state->owner->so_server->nfs_client;
1144         bool need_recover = false;
1145 
1146         if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1147                 need_recover = true;
1148         if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1149                 need_recover = true;
1150         if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1151                 need_recover = true;
1152         if (need_recover)
1153                 nfs4_state_mark_reclaim_nograce(clp, state);
1154 }
1155 
1156 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1157                 nfs4_stateid *stateid)
1158 {
1159         if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1160                 return true;
1161         if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1162                 nfs_test_and_clear_all_open_stateid(state);
1163                 return true;
1164         }
1165         if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1166                 return true;
1167         return false;
1168 }
1169 
1170 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1171                 nfs4_stateid *stateid, fmode_t fmode)
1172 {
1173         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1174         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1175         case FMODE_WRITE:
1176                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1177                 break;
1178         case FMODE_READ:
1179                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1180                 break;
1181         case 0:
1182                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1183                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1184                 clear_bit(NFS_OPEN_STATE, &state->flags);
1185         }
1186         if (stateid == NULL)
1187                 return;
1188         if (!nfs_need_update_open_stateid(state, stateid))
1189                 return;
1190         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1191                 nfs4_stateid_copy(&state->stateid, stateid);
1192         nfs4_stateid_copy(&state->open_stateid, stateid);
1193 }
1194 
1195 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1196 {
1197         write_seqlock(&state->seqlock);
1198         nfs_clear_open_stateid_locked(state, stateid, fmode);
1199         write_sequnlock(&state->seqlock);
1200         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1201                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1202 }
1203 
1204 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1205 {
1206         switch (fmode) {
1207                 case FMODE_READ:
1208                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
1209                         break;
1210                 case FMODE_WRITE:
1211                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
1212                         break;
1213                 case FMODE_READ|FMODE_WRITE:
1214                         set_bit(NFS_O_RDWR_STATE, &state->flags);
1215         }
1216         if (!nfs_need_update_open_stateid(state, stateid))
1217                 return;
1218         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1219                 nfs4_stateid_copy(&state->stateid, stateid);
1220         nfs4_stateid_copy(&state->open_stateid, stateid);
1221 }
1222 
1223 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1224 {
1225         /*
1226          * Protect the call to nfs4_state_set_mode_locked and
1227          * serialise the stateid update
1228          */
1229         spin_lock(&state->owner->so_lock);
1230         write_seqlock(&state->seqlock);
1231         if (deleg_stateid != NULL) {
1232                 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1233                 set_bit(NFS_DELEGATED_STATE, &state->flags);
1234         }
1235         if (open_stateid != NULL)
1236                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1237         write_sequnlock(&state->seqlock);
1238         update_open_stateflags(state, fmode);
1239         spin_unlock(&state->owner->so_lock);
1240 }
1241 
1242 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1243 {
1244         struct nfs_inode *nfsi = NFS_I(state->inode);
1245         struct nfs_delegation *deleg_cur;
1246         int ret = 0;
1247 
1248         fmode &= (FMODE_READ|FMODE_WRITE);
1249 
1250         rcu_read_lock();
1251         deleg_cur = rcu_dereference(nfsi->delegation);
1252         if (deleg_cur == NULL)
1253                 goto no_delegation;
1254 
1255         spin_lock(&deleg_cur->lock);
1256         if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1257            test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1258             (deleg_cur->type & fmode) != fmode)
1259                 goto no_delegation_unlock;
1260 
1261         if (delegation == NULL)
1262                 delegation = &deleg_cur->stateid;
1263         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1264                 goto no_delegation_unlock;
1265 
1266         nfs_mark_delegation_referenced(deleg_cur);
1267         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1268         ret = 1;
1269 no_delegation_unlock:
1270         spin_unlock(&deleg_cur->lock);
1271 no_delegation:
1272         rcu_read_unlock();
1273 
1274         if (!ret && open_stateid != NULL) {
1275                 __update_open_stateid(state, open_stateid, NULL, fmode);
1276                 ret = 1;
1277         }
1278         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1279                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1280 
1281         return ret;
1282 }
1283 
1284 
1285 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1286 {
1287         struct nfs_delegation *delegation;
1288 
1289         rcu_read_lock();
1290         delegation = rcu_dereference(NFS_I(inode)->delegation);
1291         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1292                 rcu_read_unlock();
1293                 return;
1294         }
1295         rcu_read_unlock();
1296         nfs4_inode_return_delegation(inode);
1297 }
1298 
1299 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1300 {
1301         struct nfs4_state *state = opendata->state;
1302         struct nfs_inode *nfsi = NFS_I(state->inode);
1303         struct nfs_delegation *delegation;
1304         int open_mode = opendata->o_arg.open_flags;
1305         fmode_t fmode = opendata->o_arg.fmode;
1306         nfs4_stateid stateid;
1307         int ret = -EAGAIN;
1308 
1309         for (;;) {
1310                 spin_lock(&state->owner->so_lock);
1311                 if (can_open_cached(state, fmode, open_mode)) {
1312                         update_open_stateflags(state, fmode);
1313                         spin_unlock(&state->owner->so_lock);
1314                         goto out_return_state;
1315                 }
1316                 spin_unlock(&state->owner->so_lock);
1317                 rcu_read_lock();
1318                 delegation = rcu_dereference(nfsi->delegation);
1319                 if (!can_open_delegated(delegation, fmode)) {
1320                         rcu_read_unlock();
1321                         break;
1322                 }
1323                 /* Save the delegation */
1324                 nfs4_stateid_copy(&stateid, &delegation->stateid);
1325                 rcu_read_unlock();
1326                 nfs_release_seqid(opendata->o_arg.seqid);
1327                 if (!opendata->is_recover) {
1328                         ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1329                         if (ret != 0)
1330                                 goto out;
1331                 }
1332                 ret = -EAGAIN;
1333 
1334                 /* Try to update the stateid using the delegation */
1335                 if (update_open_stateid(state, NULL, &stateid, fmode))
1336                         goto out_return_state;
1337         }
1338 out:
1339         return ERR_PTR(ret);
1340 out_return_state:
1341         atomic_inc(&state->count);
1342         return state;
1343 }
1344 
1345 static void
1346 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1347 {
1348         struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1349         struct nfs_delegation *delegation;
1350         int delegation_flags = 0;
1351 
1352         rcu_read_lock();
1353         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1354         if (delegation)
1355                 delegation_flags = delegation->flags;
1356         rcu_read_unlock();
1357         if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1358                 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1359                                    "returning a delegation for "
1360                                    "OPEN(CLAIM_DELEGATE_CUR)\n",
1361                                    clp->cl_hostname);
1362         } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1363                 nfs_inode_set_delegation(state->inode,
1364                                          data->owner->so_cred,
1365                                          &data->o_res);
1366         else
1367                 nfs_inode_reclaim_delegation(state->inode,
1368                                              data->owner->so_cred,
1369                                              &data->o_res);
1370 }
1371 
1372 /*
1373  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1374  * and update the nfs4_state.
1375  */
1376 static struct nfs4_state *
1377 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1378 {
1379         struct inode *inode = data->state->inode;
1380         struct nfs4_state *state = data->state;
1381         int ret;
1382 
1383         if (!data->rpc_done) {
1384                 if (data->rpc_status) {
1385                         ret = data->rpc_status;
1386                         goto err;
1387                 }
1388                 /* cached opens have already been processed */
1389                 goto update;
1390         }
1391 
1392         ret = nfs_refresh_inode(inode, &data->f_attr);
1393         if (ret)
1394                 goto err;
1395 
1396         if (data->o_res.delegation_type != 0)
1397                 nfs4_opendata_check_deleg(data, state);
1398 update:
1399         update_open_stateid(state, &data->o_res.stateid, NULL,
1400                             data->o_arg.fmode);
1401         atomic_inc(&state->count);
1402 
1403         return state;
1404 err:
1405         return ERR_PTR(ret);
1406 
1407 }
1408 
1409 static struct nfs4_state *
1410 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1411 {
1412         struct inode *inode;
1413         struct nfs4_state *state = NULL;
1414         int ret;
1415 
1416         if (!data->rpc_done) {
1417                 state = nfs4_try_open_cached(data);
1418                 goto out;
1419         }
1420 
1421         ret = -EAGAIN;
1422         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1423                 goto err;
1424         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1425         ret = PTR_ERR(inode);
1426         if (IS_ERR(inode))
1427                 goto err;
1428         ret = -ENOMEM;
1429         state = nfs4_get_open_state(inode, data->owner);
1430         if (state == NULL)
1431                 goto err_put_inode;
1432         if (data->o_res.delegation_type != 0)
1433                 nfs4_opendata_check_deleg(data, state);
1434         update_open_stateid(state, &data->o_res.stateid, NULL,
1435                         data->o_arg.fmode);
1436         iput(inode);
1437 out:
1438         nfs_release_seqid(data->o_arg.seqid);
1439         return state;
1440 err_put_inode:
1441         iput(inode);
1442 err:
1443         return ERR_PTR(ret);
1444 }
1445 
1446 static struct nfs4_state *
1447 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1448 {
1449         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1450                 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1451         return _nfs4_opendata_to_nfs4_state(data);
1452 }
1453 
1454 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1455 {
1456         struct nfs_inode *nfsi = NFS_I(state->inode);
1457         struct nfs_open_context *ctx;
1458 
1459         spin_lock(&state->inode->i_lock);
1460         list_for_each_entry(ctx, &nfsi->open_files, list) {
1461                 if (ctx->state != state)
1462                         continue;
1463                 get_nfs_open_context(ctx);
1464                 spin_unlock(&state->inode->i_lock);
1465                 return ctx;
1466         }
1467         spin_unlock(&state->inode->i_lock);
1468         return ERR_PTR(-ENOENT);
1469 }
1470 
1471 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1472                 struct nfs4_state *state, enum open_claim_type4 claim)
1473 {
1474         struct nfs4_opendata *opendata;
1475 
1476         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1477                         NULL, NULL, claim, GFP_NOFS);
1478         if (opendata == NULL)
1479                 return ERR_PTR(-ENOMEM);
1480         opendata->state = state;
1481         atomic_inc(&state->count);
1482         return opendata;
1483 }
1484 
1485 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1486 {
1487         struct nfs4_state *newstate;
1488         int ret;
1489 
1490         opendata->o_arg.open_flags = 0;
1491         opendata->o_arg.fmode = fmode;
1492         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1493         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1494         nfs4_init_opendata_res(opendata);
1495         ret = _nfs4_recover_proc_open(opendata);
1496         if (ret != 0)
1497                 return ret; 
1498         newstate = nfs4_opendata_to_nfs4_state(opendata);
1499         if (IS_ERR(newstate))
1500                 return PTR_ERR(newstate);
1501         nfs4_close_state(newstate, fmode);
1502         *res = newstate;
1503         return 0;
1504 }
1505 
1506 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1507 {
1508         struct nfs4_state *newstate;
1509         int ret;
1510 
1511         /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1512         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1513         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1514         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1515         /* memory barrier prior to reading state->n_* */
1516         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1517         clear_bit(NFS_OPEN_STATE, &state->flags);
1518         smp_rmb();
1519         if (state->n_rdwr != 0) {
1520                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1521                 if (ret != 0)
1522                         return ret;
1523                 if (newstate != state)
1524                         return -ESTALE;
1525         }
1526         if (state->n_wronly != 0) {
1527                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1528                 if (ret != 0)
1529                         return ret;
1530                 if (newstate != state)
1531                         return -ESTALE;
1532         }
1533         if (state->n_rdonly != 0) {
1534                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1535                 if (ret != 0)
1536                         return ret;
1537                 if (newstate != state)
1538                         return -ESTALE;
1539         }
1540         /*
1541          * We may have performed cached opens for all three recoveries.
1542          * Check if we need to update the current stateid.
1543          */
1544         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1545             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1546                 write_seqlock(&state->seqlock);
1547                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1548                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1549                 write_sequnlock(&state->seqlock);
1550         }
1551         return 0;
1552 }
1553 
1554 /*
1555  * OPEN_RECLAIM:
1556  *      reclaim state on the server after a reboot.
1557  */
1558 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1559 {
1560         struct nfs_delegation *delegation;
1561         struct nfs4_opendata *opendata;
1562         fmode_t delegation_type = 0;
1563         int status;
1564 
1565         opendata = nfs4_open_recoverdata_alloc(ctx, state,
1566                         NFS4_OPEN_CLAIM_PREVIOUS);
1567         if (IS_ERR(opendata))
1568                 return PTR_ERR(opendata);
1569         rcu_read_lock();
1570         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1571         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1572                 delegation_type = delegation->type;
1573         rcu_read_unlock();
1574         opendata->o_arg.u.delegation_type = delegation_type;
1575         status = nfs4_open_recover(opendata, state);
1576         nfs4_opendata_put(opendata);
1577         return status;
1578 }
1579 
1580 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1581 {
1582         struct nfs_server *server = NFS_SERVER(state->inode);
1583         struct nfs4_exception exception = { };
1584         int err;
1585         do {
1586                 err = _nfs4_do_open_reclaim(ctx, state);
1587                 trace_nfs4_open_reclaim(ctx, 0, err);
1588                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1589                         continue;
1590                 if (err != -NFS4ERR_DELAY)
1591                         break;
1592                 nfs4_handle_exception(server, err, &exception);
1593         } while (exception.retry);
1594         return err;
1595 }
1596 
1597 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1598 {
1599         struct nfs_open_context *ctx;
1600         int ret;
1601 
1602         ctx = nfs4_state_find_open_context(state);
1603         if (IS_ERR(ctx))
1604                 return -EAGAIN;
1605         ret = nfs4_do_open_reclaim(ctx, state);
1606         put_nfs_open_context(ctx);
1607         return ret;
1608 }
1609 
1610 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1611 {
1612         switch (err) {
1613                 default:
1614                         printk(KERN_ERR "NFS: %s: unhandled error "
1615                                         "%d.\n", __func__, err);
1616                 case 0:
1617                 case -ENOENT:
1618                 case -ESTALE:
1619                         break;
1620                 case -NFS4ERR_BADSESSION:
1621                 case -NFS4ERR_BADSLOT:
1622                 case -NFS4ERR_BAD_HIGH_SLOT:
1623                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1624                 case -NFS4ERR_DEADSESSION:
1625                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1626                         nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1627                         return -EAGAIN;
1628                 case -NFS4ERR_STALE_CLIENTID:
1629                 case -NFS4ERR_STALE_STATEID:
1630                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1631                 case -NFS4ERR_EXPIRED:
1632                         /* Don't recall a delegation if it was lost */
1633                         nfs4_schedule_lease_recovery(server->nfs_client);
1634                         return -EAGAIN;
1635                 case -NFS4ERR_MOVED:
1636                         nfs4_schedule_migration_recovery(server);
1637                         return -EAGAIN;
1638                 case -NFS4ERR_LEASE_MOVED:
1639                         nfs4_schedule_lease_moved_recovery(server->nfs_client);
1640                         return -EAGAIN;
1641                 case -NFS4ERR_DELEG_REVOKED:
1642                 case -NFS4ERR_ADMIN_REVOKED:
1643                 case -NFS4ERR_BAD_STATEID:
1644                 case -NFS4ERR_OPENMODE:
1645                         nfs_inode_find_state_and_recover(state->inode,
1646                                         stateid);
1647                         nfs4_schedule_stateid_recovery(server, state);
1648                         return -EAGAIN;
1649                 case -NFS4ERR_DELAY:
1650                 case -NFS4ERR_GRACE:
1651                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1652                         ssleep(1);
1653                         return -EAGAIN;
1654                 case -ENOMEM:
1655                 case -NFS4ERR_DENIED:
1656                         /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1657                         return 0;
1658         }
1659         return err;
1660 }
1661 
1662 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1663 {
1664         struct nfs_server *server = NFS_SERVER(state->inode);
1665         struct nfs4_opendata *opendata;
1666         int err;
1667 
1668         opendata = nfs4_open_recoverdata_alloc(ctx, state,
1669                         NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1670         if (IS_ERR(opendata))
1671                 return PTR_ERR(opendata);
1672         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1673         err = nfs4_open_recover(opendata, state);
1674         nfs4_opendata_put(opendata);
1675         return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1676 }
1677 
1678 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1679 {
1680         struct nfs4_opendata *data = calldata;
1681 
1682         nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1683                                 &data->c_res.seq_res, task);
1684 }
1685 
1686 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1687 {
1688         struct nfs4_opendata *data = calldata;
1689 
1690         nfs40_sequence_done(task, &data->c_res.seq_res);
1691 
1692         data->rpc_status = task->tk_status;
1693         if (data->rpc_status == 0) {
1694                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1695                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1696                 renew_lease(data->o_res.server, data->timestamp);
1697                 data->rpc_done = 1;
1698         }
1699 }
1700 
1701 static void nfs4_open_confirm_release(void *calldata)
1702 {
1703         struct nfs4_opendata *data = calldata;
1704         struct nfs4_state *state = NULL;
1705 
1706         /* If this request hasn't been cancelled, do nothing */
1707         if (data->cancelled == 0)
1708                 goto out_free;
1709         /* In case of error, no cleanup! */
1710         if (!data->rpc_done)
1711                 goto out_free;
1712         state = nfs4_opendata_to_nfs4_state(data);
1713         if (!IS_ERR(state))
1714                 nfs4_close_state(state, data->o_arg.fmode);
1715 out_free:
1716         nfs4_opendata_put(data);
1717 }
1718 
1719 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1720         .rpc_call_prepare = nfs4_open_confirm_prepare,
1721         .rpc_call_done = nfs4_open_confirm_done,
1722         .rpc_release = nfs4_open_confirm_release,
1723 };
1724 
1725 /*
1726  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1727  */
1728 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1729 {
1730         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1731         struct rpc_task *task;
1732         struct  rpc_message msg = {
1733                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1734                 .rpc_argp = &data->c_arg,
1735                 .rpc_resp = &data->c_res,
1736                 .rpc_cred = data->owner->so_cred,
1737         };
1738         struct rpc_task_setup task_setup_data = {
1739                 .rpc_client = server->client,
1740                 .rpc_message = &msg,
1741                 .callback_ops = &nfs4_open_confirm_ops,
1742                 .callback_data = data,
1743                 .workqueue = nfsiod_workqueue,
1744                 .flags = RPC_TASK_ASYNC,
1745         };
1746         int status;
1747 
1748         nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1749         kref_get(&data->kref);
1750         data->rpc_done = 0;
1751         data->rpc_status = 0;
1752         data->timestamp = jiffies;
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         return status;
1764 }
1765 
1766 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1767 {
1768         struct nfs4_opendata *data = calldata;
1769         struct nfs4_state_owner *sp = data->owner;
1770         struct nfs_client *clp = sp->so_server->nfs_client;
1771 
1772         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1773                 goto out_wait;
1774         /*
1775          * Check if we still need to send an OPEN call, or if we can use
1776          * a delegation instead.
1777          */
1778         if (data->state != NULL) {
1779                 struct nfs_delegation *delegation;
1780 
1781                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1782                         goto out_no_action;
1783                 rcu_read_lock();
1784                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1785                 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1786                     data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1787                     can_open_delegated(delegation, data->o_arg.fmode))
1788                         goto unlock_no_action;
1789                 rcu_read_unlock();
1790         }
1791         /* Update client id. */
1792         data->o_arg.clientid = clp->cl_clientid;
1793         switch (data->o_arg.claim) {
1794         case NFS4_OPEN_CLAIM_PREVIOUS:
1795         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1796         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1797                 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1798         case NFS4_OPEN_CLAIM_FH:
1799                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1800                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1801         }
1802         data->timestamp = jiffies;
1803         if (nfs4_setup_sequence(data->o_arg.server,
1804                                 &data->o_arg.seq_args,
1805                                 &data->o_res.seq_res,
1806                                 task) != 0)
1807                 nfs_release_seqid(data->o_arg.seqid);
1808 
1809         /* Set the create mode (note dependency on the session type) */
1810         data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1811         if (data->o_arg.open_flags & O_EXCL) {
1812                 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1813                 if (nfs4_has_persistent_session(clp))
1814                         data->o_arg.createmode = NFS4_CREATE_GUARDED;
1815                 else if (clp->cl_mvops->minor_version > 0)
1816                         data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1817         }
1818         return;
1819 unlock_no_action:
1820         rcu_read_unlock();
1821 out_no_action:
1822         task->tk_action = NULL;
1823 out_wait:
1824         nfs4_sequence_done(task, &data->o_res.seq_res);
1825 }
1826 
1827 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1828 {
1829         struct nfs4_opendata *data = calldata;
1830 
1831         data->rpc_status = task->tk_status;
1832 
1833         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1834                 return;
1835 
1836         if (task->tk_status == 0) {
1837                 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1838                         switch (data->o_res.f_attr->mode & S_IFMT) {
1839                         case S_IFREG:
1840                                 break;
1841                         case S_IFLNK:
1842                                 data->rpc_status = -ELOOP;
1843                                 break;
1844                         case S_IFDIR:
1845                                 data->rpc_status = -EISDIR;
1846                                 break;
1847                         default:
1848                                 data->rpc_status = -ENOTDIR;
1849                         }
1850                 }
1851                 renew_lease(data->o_res.server, data->timestamp);
1852                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1853                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1854         }
1855         data->rpc_done = 1;
1856 }
1857 
1858 static void nfs4_open_release(void *calldata)
1859 {
1860         struct nfs4_opendata *data = calldata;
1861         struct nfs4_state *state = NULL;
1862 
1863         /* If this request hasn't been cancelled, do nothing */
1864         if (data->cancelled == 0)
1865                 goto out_free;
1866         /* In case of error, no cleanup! */
1867         if (data->rpc_status != 0 || !data->rpc_done)
1868                 goto out_free;
1869         /* In case we need an open_confirm, no cleanup! */
1870         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1871                 goto out_free;
1872         state = nfs4_opendata_to_nfs4_state(data);
1873         if (!IS_ERR(state))
1874                 nfs4_close_state(state, data->o_arg.fmode);
1875 out_free:
1876         nfs4_opendata_put(data);
1877 }
1878 
1879 static const struct rpc_call_ops nfs4_open_ops = {
1880         .rpc_call_prepare = nfs4_open_prepare,
1881         .rpc_call_done = nfs4_open_done,
1882         .rpc_release = nfs4_open_release,
1883 };
1884 
1885 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1886 {
1887         struct inode *dir = data->dir->d_inode;
1888         struct nfs_server *server = NFS_SERVER(dir);
1889         struct nfs_openargs *o_arg = &data->o_arg;
1890         struct nfs_openres *o_res = &data->o_res;
1891         struct rpc_task *task;
1892         struct rpc_message msg = {
1893                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1894                 .rpc_argp = o_arg,
1895                 .rpc_resp = o_res,
1896                 .rpc_cred = data->owner->so_cred,
1897         };
1898         struct rpc_task_setup task_setup_data = {
1899                 .rpc_client = server->client,
1900                 .rpc_message = &msg,
1901                 .callback_ops = &nfs4_open_ops,
1902                 .callback_data = data,
1903                 .workqueue = nfsiod_workqueue,
1904                 .flags = RPC_TASK_ASYNC,
1905         };
1906         int status;
1907 
1908         nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1909         kref_get(&data->kref);
1910         data->rpc_done = 0;
1911         data->rpc_status = 0;
1912         data->cancelled = 0;
1913         data->is_recover = 0;
1914         if (isrecover) {
1915                 nfs4_set_sequence_privileged(&o_arg->seq_args);
1916                 data->is_recover = 1;
1917         }
1918         task = rpc_run_task(&task_setup_data);
1919         if (IS_ERR(task))
1920                 return PTR_ERR(task);
1921         status = nfs4_wait_for_completion_rpc_task(task);
1922         if (status != 0) {
1923                 data->cancelled = 1;
1924                 smp_wmb();
1925         } else
1926                 status = data->rpc_status;
1927         rpc_put_task(task);
1928 
1929         return status;
1930 }
1931 
1932 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1933 {
1934         struct inode *dir = data->dir->d_inode;
1935         struct nfs_openres *o_res = &data->o_res;
1936         int status;
1937 
1938         status = nfs4_run_open_task(data, 1);
1939         if (status != 0 || !data->rpc_done)
1940                 return status;
1941 
1942         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1943 
1944         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1945                 status = _nfs4_proc_open_confirm(data);
1946                 if (status != 0)
1947                         return status;
1948         }
1949 
1950         return status;
1951 }
1952 
1953 /*
1954  * Additional permission checks in order to distinguish between an
1955  * open for read, and an open for execute. This works around the
1956  * fact that NFSv4 OPEN treats read and execute permissions as being
1957  * the same.
1958  * Note that in the non-execute case, we want to turn off permission
1959  * checking if we just created a new file (POSIX open() semantics).
1960  */
1961 static int nfs4_opendata_access(struct rpc_cred *cred,
1962                                 struct nfs4_opendata *opendata,
1963                                 struct nfs4_state *state, fmode_t fmode,
1964                                 int openflags)
1965 {
1966         struct nfs_access_entry cache;
1967         u32 mask;
1968 
1969         /* access call failed or for some reason the server doesn't
1970          * support any access modes -- defer access call until later */
1971         if (opendata->o_res.access_supported == 0)
1972                 return 0;
1973 
1974         mask = 0;
1975         /*
1976          * Use openflags to check for exec, because fmode won't
1977          * always have FMODE_EXEC set when file open for exec.
1978          */
1979         if (openflags & __FMODE_EXEC) {
1980                 /* ONLY check for exec rights */
1981                 mask = MAY_EXEC;
1982         } else if ((fmode & FMODE_READ) && !opendata->file_created)
1983                 mask = MAY_READ;
1984 
1985         cache.cred = cred;
1986         cache.jiffies = jiffies;
1987         nfs_access_set_mask(&cache, opendata->o_res.access_result);
1988         nfs_access_add_cache(state->inode, &cache);
1989 
1990         if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1991                 return 0;
1992 
1993         /* even though OPEN succeeded, access is denied. Close the file */
1994         nfs4_close_state(state, fmode);
1995         return -EACCES;
1996 }
1997 
1998 /*
1999  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2000  */
2001 static int _nfs4_proc_open(struct nfs4_opendata *data)
2002 {
2003         struct inode *dir = data->dir->d_inode;
2004         struct nfs_server *server = NFS_SERVER(dir);
2005         struct nfs_openargs *o_arg = &data->o_arg;
2006         struct nfs_openres *o_res = &data->o_res;
2007         int status;
2008 
2009         status = nfs4_run_open_task(data, 0);
2010         if (!data->rpc_done)
2011                 return status;
2012         if (status != 0) {
2013                 if (status == -NFS4ERR_BADNAME &&
2014                                 !(o_arg->open_flags & O_CREAT))
2015                         return -ENOENT;
2016                 return status;
2017         }
2018 
2019         nfs_fattr_map_and_free_names(server, &data->f_attr);
2020 
2021         if (o_arg->open_flags & O_CREAT) {
2022                 update_changeattr(dir, &o_res->cinfo);
2023                 if (o_arg->open_flags & O_EXCL)
2024                         data->file_created = 1;
2025                 else if (o_res->cinfo.before != o_res->cinfo.after)
2026                         data->file_created = 1;
2027         }
2028         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2029                 server->caps &= ~NFS_CAP_POSIX_LOCK;
2030         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2031                 status = _nfs4_proc_open_confirm(data);
2032                 if (status != 0)
2033                         return status;
2034         }
2035         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2036                 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2037         return 0;
2038 }
2039 
2040 static int nfs4_recover_expired_lease(struct nfs_server *server)
2041 {
2042         return nfs4_client_recover_expired_lease(server->nfs_client);
2043 }
2044 
2045 /*
2046  * OPEN_EXPIRED:
2047  *      reclaim state on the server after a network partition.
2048  *      Assumes caller holds the appropriate lock
2049  */
2050 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2051 {
2052         struct nfs4_opendata *opendata;
2053         int ret;
2054 
2055         opendata = nfs4_open_recoverdata_alloc(ctx, state,
2056                         NFS4_OPEN_CLAIM_FH);
2057         if (IS_ERR(opendata))
2058                 return PTR_ERR(opendata);
2059         ret = nfs4_open_recover(opendata, state);
2060         if (ret == -ESTALE)
2061                 d_drop(ctx->dentry);
2062         nfs4_opendata_put(opendata);
2063         return ret;
2064 }
2065 
2066 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2067 {
2068         struct nfs_server *server = NFS_SERVER(state->inode);
2069         struct nfs4_exception exception = { };
2070         int err;
2071 
2072         do {
2073                 err = _nfs4_open_expired(ctx, state);
2074                 trace_nfs4_open_expired(ctx, 0, err);
2075                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2076                         continue;
2077                 switch (err) {
2078                 default:
2079                         goto out;
2080                 case -NFS4ERR_GRACE:
2081                 case -NFS4ERR_DELAY:
2082                         nfs4_handle_exception(server, err, &exception);
2083                         err = 0;
2084                 }
2085         } while (exception.retry);
2086 out:
2087         return err;
2088 }
2089 
2090 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2091 {
2092         struct nfs_open_context *ctx;
2093         int ret;
2094 
2095         ctx = nfs4_state_find_open_context(state);
2096         if (IS_ERR(ctx))
2097                 return -EAGAIN;
2098         ret = nfs4_do_open_expired(ctx, state);
2099         put_nfs_open_context(ctx);
2100         return ret;
2101 }
2102 
2103 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2104 {
2105         nfs_remove_bad_delegation(state->inode);
2106         write_seqlock(&state->seqlock);
2107         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2108         write_sequnlock(&state->seqlock);
2109         clear_bit(NFS_DELEGATED_STATE, &state->flags);
2110 }
2111 
2112 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2113 {
2114         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2115                 nfs_finish_clear_delegation_stateid(state);
2116 }
2117 
2118 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2119 {
2120         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2121         nfs40_clear_delegation_stateid(state);
2122         return nfs4_open_expired(sp, state);
2123 }
2124 
2125 #if defined(CONFIG_NFS_V4_1)
2126 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2127 {
2128         struct nfs_server *server = NFS_SERVER(state->inode);
2129         nfs4_stateid stateid;
2130         struct nfs_delegation *delegation;
2131         struct rpc_cred *cred;
2132         int status;
2133 
2134         /* Get the delegation credential for use by test/free_stateid */
2135         rcu_read_lock();
2136         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2137         if (delegation == NULL) {
2138                 rcu_read_unlock();
2139                 return;
2140         }
2141 
2142         nfs4_stateid_copy(&stateid, &delegation->stateid);
2143         cred = get_rpccred(delegation->cred);
2144         rcu_read_unlock();
2145         status = nfs41_test_stateid(server, &stateid, cred);
2146         trace_nfs4_test_delegation_stateid(state, NULL, status);
2147 
2148         if (status != NFS_OK) {
2149                 /* Free the stateid unless the server explicitly
2150                  * informs us the stateid is unrecognized. */
2151                 if (status != -NFS4ERR_BAD_STATEID)
2152                         nfs41_free_stateid(server, &stateid, cred);
2153                 nfs_finish_clear_delegation_stateid(state);
2154         }
2155 
2156         put_rpccred(cred);
2157 }
2158 
2159 /**
2160  * nfs41_check_open_stateid - possibly free an open stateid
2161  *
2162  * @state: NFSv4 state for an inode
2163  *
2164  * Returns NFS_OK if recovery for this stateid is now finished.
2165  * Otherwise a negative NFS4ERR value is returned.
2166  */
2167 static int nfs41_check_open_stateid(struct nfs4_state *state)
2168 {
2169         struct nfs_server *server = NFS_SERVER(state->inode);
2170         nfs4_stateid *stateid = &state->open_stateid;
2171         struct rpc_cred *cred = state->owner->so_cred;
2172         int status;
2173 
2174         /* If a state reset has been done, test_stateid is unneeded */
2175         if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2176             (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2177             (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2178                 return -NFS4ERR_BAD_STATEID;
2179 
2180         status = nfs41_test_stateid(server, stateid, cred);
2181         trace_nfs4_test_open_stateid(state, NULL, status);
2182         if (status != NFS_OK) {
2183                 /* Free the stateid unless the server explicitly
2184                  * informs us the stateid is unrecognized. */
2185                 if (status != -NFS4ERR_BAD_STATEID)
2186                         nfs41_free_stateid(server, stateid, cred);
2187 
2188                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2189                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2190                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2191                 clear_bit(NFS_OPEN_STATE, &state->flags);
2192         }
2193         return status;
2194 }
2195 
2196 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2197 {
2198         int status;
2199 
2200         nfs41_check_delegation_stateid(state);
2201         status = nfs41_check_open_stateid(state);
2202         if (status != NFS_OK)
2203                 status = nfs4_open_expired(sp, state);
2204         return status;
2205 }
2206 #endif
2207 
2208 /*
2209  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2210  * fields corresponding to attributes that were used to store the verifier.
2211  * Make sure we clobber those fields in the later setattr call
2212  */
2213 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2214 {
2215         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2216             !(sattr->ia_valid & ATTR_ATIME_SET))
2217                 sattr->ia_valid |= ATTR_ATIME;
2218 
2219         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2220             !(sattr->ia_valid & ATTR_MTIME_SET))
2221                 sattr->ia_valid |= ATTR_MTIME;
2222 }
2223 
2224 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2225                 fmode_t fmode,
2226                 int flags,
2227                 struct nfs_open_context *ctx)
2228 {
2229         struct nfs4_state_owner *sp = opendata->owner;
2230         struct nfs_server *server = sp->so_server;
2231         struct dentry *dentry;
2232         struct nfs4_state *state;
2233         unsigned int seq;
2234         int ret;
2235 
2236         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2237 
2238         ret = _nfs4_proc_open(opendata);
2239         if (ret != 0)
2240                 goto out;
2241 
2242         state = nfs4_opendata_to_nfs4_state(opendata);
2243         ret = PTR_ERR(state);
2244         if (IS_ERR(state))
2245                 goto out;
2246         if (server->caps & NFS_CAP_POSIX_LOCK)
2247                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2248 
2249         dentry = opendata->dentry;
2250         if (dentry->d_inode == NULL) {
2251                 /* FIXME: Is this d_drop() ever needed? */
2252                 d_drop(dentry);
2253                 dentry = d_add_unique(dentry, igrab(state->inode));
2254                 if (dentry == NULL) {
2255                         dentry = opendata->dentry;
2256                 } else {
2257                         dput(ctx->dentry);
2258                         ctx->dentry = dentry;
2259                 }
2260                 nfs_set_verifier(dentry,
2261                                 nfs_save_change_attribute(opendata->dir->d_inode));
2262         }
2263 
2264         ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2265         if (ret != 0)
2266                 goto out;
2267 
2268         ctx->state = state;
2269         if (dentry->d_inode == state->inode) {
2270                 nfs_inode_attach_open_context(ctx);
2271                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2272                         nfs4_schedule_stateid_recovery(server, state);
2273         }
2274 out:
2275         return ret;
2276 }
2277 
2278 /*
2279  * Returns a referenced nfs4_state
2280  */
2281 static int _nfs4_do_open(struct inode *dir,
2282                         struct nfs_open_context *ctx,
2283                         int flags,
2284                         struct iattr *sattr,
2285                         struct nfs4_label *label,
2286                         int *opened)
2287 {
2288         struct nfs4_state_owner  *sp;
2289         struct nfs4_state     *state = NULL;
2290         struct nfs_server       *server = NFS_SERVER(dir);
2291         struct nfs4_opendata *opendata;
2292         struct dentry *dentry = ctx->dentry;
2293         struct rpc_cred *cred = ctx->cred;
2294         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2295         fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2296         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2297         struct nfs4_label *olabel = NULL;
2298         int status;
2299 
2300         /* Protect against reboot recovery conflicts */
2301         status = -ENOMEM;
2302         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2303         if (sp == NULL) {
2304                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2305                 goto out_err;
2306         }
2307         status = nfs4_recover_expired_lease(server);
2308         if (status != 0)
2309                 goto err_put_state_owner;
2310         if (dentry->d_inode != NULL)
2311                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2312         status = -ENOMEM;
2313         if (dentry->d_inode)
2314                 claim = NFS4_OPEN_CLAIM_FH;
2315         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2316                         label, claim, GFP_KERNEL);
2317         if (opendata == NULL)
2318                 goto err_put_state_owner;
2319 
2320         if (label) {
2321                 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2322                 if (IS_ERR(olabel)) {
2323                         status = PTR_ERR(olabel);
2324                         goto err_opendata_put;
2325                 }
2326         }
2327 
2328         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2329                 if (!opendata->f_attr.mdsthreshold) {
2330                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2331                         if (!opendata->f_attr.mdsthreshold)
2332                                 goto err_free_label;
2333                 }
2334                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2335         }
2336         if (dentry->d_inode != NULL)
2337                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2338 
2339         status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2340         if (status != 0)
2341                 goto err_free_label;
2342         state = ctx->state;
2343 
2344         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2345             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2346                 nfs4_exclusive_attrset(opendata, sattr);
2347 
2348                 nfs_fattr_init(opendata->o_res.f_attr);
2349                 status = nfs4_do_setattr(state->inode, cred,
2350                                 opendata->o_res.f_attr, sattr,
2351                                 state, label, olabel);
2352                 if (status == 0) {
2353                         nfs_setattr_update_inode(state->inode, sattr);
2354                         nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2355                         nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2356                 }
2357         }
2358         if (opendata->file_created)
2359                 *opened |= FILE_CREATED;
2360 
2361         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2362                 *ctx_th = opendata->f_attr.mdsthreshold;
2363                 opendata->f_attr.mdsthreshold = NULL;
2364         }
2365 
2366         nfs4_label_free(olabel);
2367 
2368         nfs4_opendata_put(opendata);
2369         nfs4_put_state_owner(sp);
2370         return 0;
2371 err_free_label:
2372         nfs4_label_free(olabel);
2373 err_opendata_put:
2374         nfs4_opendata_put(opendata);
2375 err_put_state_owner:
2376         nfs4_put_state_owner(sp);
2377 out_err:
2378         return status;
2379 }
2380 
2381 
2382 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2383                                         struct nfs_open_context *ctx,
2384                                         int flags,
2385                                         struct iattr *sattr,
2386                                         struct nfs4_label *label,
2387                                         int *opened)
2388 {
2389         struct nfs_server *server = NFS_SERVER(dir);
2390         struct nfs4_exception exception = { };
2391         struct nfs4_state *res;
2392         int status;
2393 
2394         do {
2395                 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2396                 res = ctx->state;
2397                 trace_nfs4_open_file(ctx, flags, status);
2398                 if (status == 0)
2399                         break;
2400                 /* NOTE: BAD_SEQID means the server and client disagree about the
2401                  * book-keeping w.r.t. state-changing operations
2402                  * (OPEN/CLOSE/LOCK/LOCKU...)
2403                  * It is actually a sign of a bug on the client or on the server.
2404                  *
2405                  * If we receive a BAD_SEQID error in the particular case of
2406                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
2407                  * have unhashed the old state_owner for us, and that we can
2408                  * therefore safely retry using a new one. We should still warn
2409                  * the user though...
2410                  */
2411                 if (status == -NFS4ERR_BAD_SEQID) {
2412                         pr_warn_ratelimited("NFS: v4 server %s "
2413                                         " returned a bad sequence-id error!\n",
2414                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
2415                         exception.retry = 1;
2416                         continue;
2417                 }
2418                 /*
2419                  * BAD_STATEID on OPEN means that the server cancelled our
2420                  * state before it received the OPEN_CONFIRM.
2421                  * Recover by retrying the request as per the discussion
2422                  * on Page 181 of RFC3530.
2423                  */
2424                 if (status == -NFS4ERR_BAD_STATEID) {
2425                         exception.retry = 1;
2426                         continue;
2427                 }
2428                 if (status == -EAGAIN) {
2429                         /* We must have found a delegation */
2430                         exception.retry = 1;
2431                         continue;
2432                 }
2433                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2434                         continue;
2435                 res = ERR_PTR(nfs4_handle_exception(server,
2436                                         status, &exception));
2437         } while (exception.retry);
2438         return res;
2439 }
2440 
2441 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2442                             struct nfs_fattr *fattr, struct iattr *sattr,
2443                             struct nfs4_state *state, struct nfs4_label *ilabel,
2444                             struct nfs4_label *olabel)
2445 {
2446         struct nfs_server *server = NFS_SERVER(inode);
2447         struct nfs_setattrargs  arg = {
2448                 .fh             = NFS_FH(inode),
2449                 .iap            = sattr,
2450                 .server         = server,
2451                 .bitmask = server->attr_bitmask,
2452                 .label          = ilabel,
2453         };
2454         struct nfs_setattrres  res = {
2455                 .fattr          = fattr,
2456                 .label          = olabel,
2457                 .server         = server,
2458         };
2459         struct rpc_message msg = {
2460                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2461                 .rpc_argp       = &arg,
2462                 .rpc_resp       = &res,
2463                 .rpc_cred       = cred,
2464         };
2465         unsigned long timestamp = jiffies;
2466         fmode_t fmode;
2467         bool truncate;
2468         int status;
2469 
2470         arg.bitmask = nfs4_bitmask(server, ilabel);
2471         if (ilabel)
2472                 arg.bitmask = nfs4_bitmask(server, olabel);
2473 
2474         nfs_fattr_init(fattr);
2475 
2476         /* Servers should only apply open mode checks for file size changes */
2477         truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2478         fmode = truncate ? FMODE_WRITE : FMODE_READ;
2479 
2480         if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2481                 /* Use that stateid */
2482         } else if (truncate && state != NULL) {
2483                 struct nfs_lockowner lockowner = {
2484                         .l_owner = current->files,
2485                         .l_pid = current->tgid,
2486                 };
2487                 if (!nfs4_valid_open_stateid(state))
2488                         return -EBADF;
2489                 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2490                                 &lockowner) == -EIO)
2491                         return -EBADF;
2492         } else
2493                 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2494 
2495         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2496         if (status == 0 && state != NULL)
2497                 renew_lease(server, timestamp);
2498         return status;
2499 }
2500 
2501 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2502                            struct nfs_fattr *fattr, struct iattr *sattr,
2503                            struct nfs4_state *state, struct nfs4_label *ilabel,
2504                            struct nfs4_label *olabel)
2505 {
2506         struct nfs_server *server = NFS_SERVER(inode);
2507         struct nfs4_exception exception = {
2508                 .state = state,
2509                 .inode = inode,
2510         };
2511         int err;
2512         do {
2513                 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2514                 trace_nfs4_setattr(inode, err);
2515                 switch (err) {
2516                 case -NFS4ERR_OPENMODE:
2517                         if (!(sattr->ia_valid & ATTR_SIZE)) {
2518                                 pr_warn_once("NFSv4: server %s is incorrectly "
2519                                                 "applying open mode checks to "
2520                                                 "a SETATTR that is not "
2521                                                 "changing file size.\n",
2522                                                 server->nfs_client->cl_hostname);
2523                         }
2524                         if (state && !(state->state & FMODE_WRITE)) {
2525                                 err = -EBADF;
2526                                 if (sattr->ia_valid & ATTR_OPEN)
2527                                         err = -EACCES;
2528                                 goto out;
2529                         }
2530                 }
2531                 err = nfs4_handle_exception(server, err, &exception);
2532         } while (exception.retry);
2533 out:
2534         return err;
2535 }
2536 
2537 struct nfs4_closedata {
2538         struct inode *inode;
2539         struct nfs4_state *state;
2540         struct nfs_closeargs arg;
2541         struct nfs_closeres res;
2542         struct nfs_fattr fattr;
2543         unsigned long timestamp;
2544         bool roc;
2545         u32 roc_barrier;
2546 };
2547 
2548 static void nfs4_free_closedata(void *data)
2549 {
2550         struct nfs4_closedata *calldata = data;
2551         struct nfs4_state_owner *sp = calldata->state->owner;
2552         struct super_block *sb = calldata->state->inode->i_sb;
2553 
2554         if (calldata->roc)
2555                 pnfs_roc_release(calldata->state->inode);
2556         nfs4_put_open_state(calldata->state);
2557         nfs_free_seqid(calldata->arg.seqid);
2558         nfs4_put_state_owner(sp);
2559         nfs_sb_deactive(sb);
2560         kfree(calldata);
2561 }
2562 
2563 static void nfs4_close_done(struct rpc_task *task, void *data)
2564 {
2565         struct nfs4_closedata *calldata = data;
2566         struct nfs4_state *state = calldata->state;
2567         struct nfs_server *server = NFS_SERVER(calldata->inode);
2568         nfs4_stateid *res_stateid = NULL;
2569 
2570         dprintk("%s: begin!\n", __func__);
2571         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2572                 return;
2573         trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2574         /* hmm. we are done with the inode, and in the process of freeing
2575          * the state_owner. we keep this around to process errors
2576          */
2577         switch (task->tk_status) {
2578                 case 0:
2579                         res_stateid = &calldata->res.stateid;
2580                         if (calldata->arg.fmode == 0 && calldata->roc)
2581                                 pnfs_roc_set_barrier(state->inode,
2582                                                      calldata->roc_barrier);
2583                         renew_lease(server, calldata->timestamp);
2584                         break;
2585                 case -NFS4ERR_ADMIN_REVOKED:
2586                 case -NFS4ERR_STALE_STATEID:
2587                 case -NFS4ERR_OLD_STATEID:
2588                 case -NFS4ERR_BAD_STATEID:
2589                 case -NFS4ERR_EXPIRED:
2590                         if (calldata->arg.fmode == 0)
2591                                 break;
2592                 default:
2593                         if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2594                                 rpc_restart_call_prepare(task);
2595                                 goto out_release;
2596                         }
2597         }
2598         nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2599 out_release:
2600         nfs_release_seqid(calldata->arg.seqid);
2601         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2602         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2603 }
2604 
2605 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2606 {
2607         struct nfs4_closedata *calldata = data;
2608         struct nfs4_state *state = calldata->state;
2609         struct inode *inode = calldata->inode;
2610         bool is_rdonly, is_wronly, is_rdwr;
2611         int call_close = 0;
2612 
2613         dprintk("%s: begin!\n", __func__);
2614         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2615                 goto out_wait;
2616 
2617         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2618         spin_lock(&state->owner->so_lock);
2619         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2620         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2621         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2622         /* Calculate the change in open mode */
2623         calldata->arg.fmode = 0;
2624         if (state->n_rdwr == 0) {
2625                 if (state->n_rdonly == 0)
2626                         call_close |= is_rdonly;
2627                 else if (is_rdonly)
2628                         calldata->arg.fmode |= FMODE_READ;
2629                 if (state->n_wronly == 0)
2630                         call_close |= is_wronly;
2631                 else if (is_wronly)
2632                         calldata->arg.fmode |= FMODE_WRITE;
2633                 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
2634                         call_close |= is_rdwr;
2635         } else if (is_rdwr)
2636                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2637 
2638         if (!nfs4_valid_open_stateid(state))
2639                 call_close = 0;
2640         spin_unlock(&state->owner->so_lock);
2641 
2642         if (!call_close) {
2643                 /* Note: exit _without_ calling nfs4_close_done */
2644                 goto out_no_action;
2645         }
2646 
2647         if (calldata->arg.fmode == 0) {
2648                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2649                 if (calldata->roc &&
2650                     pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2651                         nfs_release_seqid(calldata->arg.seqid);
2652                         goto out_wait;
2653                     }
2654         }
2655 
2656         nfs_fattr_init(calldata->res.fattr);
2657         calldata->timestamp = jiffies;
2658         if (nfs4_setup_sequence(NFS_SERVER(inode),
2659                                 &calldata->arg.seq_args,
2660                                 &calldata->res.seq_res,
2661                                 task) != 0)
2662                 nfs_release_seqid(calldata->arg.seqid);
2663         dprintk("%s: done!\n", __func__);
2664         return;
2665 out_no_action:
2666         task->tk_action = NULL;
2667 out_wait:
2668         nfs4_sequence_done(task, &calldata->res.seq_res);
2669 }
2670 
2671 static const struct rpc_call_ops nfs4_close_ops = {
2672         .rpc_call_prepare = nfs4_close_prepare,
2673         .rpc_call_done = nfs4_close_done,
2674         .rpc_release = nfs4_free_closedata,
2675 };
2676 
2677 static bool nfs4_state_has_opener(struct nfs4_state *state)
2678 {
2679         /* first check existing openers */
2680         if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2681             state->n_rdonly != 0)
2682                 return true;
2683 
2684         if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2685             state->n_wronly != 0)
2686                 return true;
2687 
2688         if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2689             state->n_rdwr != 0)
2690                 return true;
2691 
2692         return false;
2693 }
2694 
2695 static bool nfs4_roc(struct inode *inode)
2696 {
2697         struct nfs_inode *nfsi = NFS_I(inode);
2698         struct nfs_open_context *ctx;
2699         struct nfs4_state *state;
2700 
2701         spin_lock(&inode->i_lock);
2702         list_for_each_entry(ctx, &nfsi->open_files, list) {
2703                 state = ctx->state;
2704                 if (state == NULL)
2705                         continue;
2706                 if (nfs4_state_has_opener(state)) {
2707                         spin_unlock(&inode->i_lock);
2708                         return false;
2709                 }
2710         }
2711         spin_unlock(&inode->i_lock);
2712 
2713         if (nfs4_check_delegation(inode, FMODE_READ))
2714                 return false;
2715 
2716         return pnfs_roc(inode);
2717 }
2718 
2719 /* 
2720  * It is possible for data to be read/written from a mem-mapped file 
2721  * after the sys_close call (which hits the vfs layer as a flush).
2722  * This means that we can't safely call nfsv4 close on a file until 
2723  * the inode is cleared. This in turn means that we are not good
2724  * NFSv4 citizens - we do not indicate to the server to update the file's 
2725  * share state even when we are done with one of the three share 
2726  * stateid's in the inode.
2727  *
2728  * NOTE: Caller must be holding the sp->so_owner semaphore!
2729  */
2730 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2731 {
2732         struct nfs_server *server = NFS_SERVER(state->inode);
2733         struct nfs4_closedata *calldata;
2734         struct nfs4_state_owner *sp = state->owner;
2735         struct rpc_task *task;
2736         struct rpc_message msg = {
2737                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2738                 .rpc_cred = state->owner->so_cred,
2739         };
2740         struct rpc_task_setup task_setup_data = {
2741                 .rpc_client = server->client,
2742                 .rpc_message = &msg,
2743                 .callback_ops = &nfs4_close_ops,
2744                 .workqueue = nfsiod_workqueue,
2745                 .flags = RPC_TASK_ASYNC,
2746         };
2747         int status = -ENOMEM;
2748 
2749         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2750                 &task_setup_data.rpc_client, &msg);
2751 
2752         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2753         if (calldata == NULL)
2754                 goto out;
2755         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2756         calldata->inode = state->inode;
2757         calldata->state = state;
2758         calldata->arg.fh = NFS_FH(state->inode);
2759         calldata->arg.stateid = &state->open_stateid;
2760         /* Serialization for the sequence id */
2761         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2762         if (calldata->arg.seqid == NULL)
2763                 goto out_free_calldata;
2764         calldata->arg.fmode = 0;
2765         calldata->arg.bitmask = server->cache_consistency_bitmask;
2766         calldata->res.fattr = &calldata->fattr;
2767         calldata->res.seqid = calldata->arg.seqid;
2768         calldata->res.server = server;
2769         calldata->roc = nfs4_roc(state->inode);
2770         nfs_sb_active(calldata->inode->i_sb);
2771 
2772         msg.rpc_argp = &calldata->arg;
2773         msg.rpc_resp = &calldata->res;
2774         task_setup_data.callback_data = calldata;
2775         task = rpc_run_task(&task_setup_data);
2776         if (IS_ERR(task))
2777                 return PTR_ERR(task);
2778         status = 0;
2779         if (wait)
2780                 status = rpc_wait_for_completion_task(task);
2781         rpc_put_task(task);
2782         return status;
2783 out_free_calldata:
2784         kfree(calldata);
2785 out:
2786         nfs4_put_open_state(state);
2787         nfs4_put_state_owner(sp);
2788         return status;
2789 }
2790 
2791 static struct inode *
2792 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2793                 int open_flags, struct iattr *attr, int *opened)
2794 {
2795         struct nfs4_state *state;
2796         struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2797 
2798         label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2799 
2800         /* Protect against concurrent sillydeletes */
2801         state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2802 
2803         nfs4_label_release_security(label);
2804 
2805         if (IS_ERR(state))
2806                 return ERR_CAST(state);
2807         return state->inode;
2808 }
2809 
2810 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2811 {
2812         if (ctx->state == NULL)
2813                 return;
2814         if (is_sync)
2815                 nfs4_close_sync(ctx->state, ctx->mode);
2816         else
2817                 nfs4_close_state(ctx->state, ctx->mode);
2818 }
2819 
2820 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2821 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2822 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2823 
2824 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2825 {
2826         struct nfs4_server_caps_arg args = {
2827                 .fhandle = fhandle,
2828         };
2829         struct nfs4_server_caps_res res = {};
2830         struct rpc_message msg = {
2831                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2832                 .rpc_argp = &args,
2833                 .rpc_resp = &res,
2834         };
2835         int status;
2836 
2837         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2838         if (status == 0) {
2839                 /* Sanity check the server answers */
2840                 switch (server->nfs_client->cl_minorversion) {
2841                 case 0:
2842                         res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2843                         res.attr_bitmask[2] = 0;
2844                         break;
2845                 case 1:
2846                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2847                         break;
2848                 case 2:
2849                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2850                 }
2851                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2852                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2853                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2854                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2855                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2856                                 NFS_CAP_CTIME|NFS_CAP_MTIME|
2857                                 NFS_CAP_SECURITY_LABEL);
2858                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2859                                 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2860                         server->caps |= NFS_CAP_ACLS;
2861                 if (res.has_links != 0)
2862                         server->caps |= NFS_CAP_HARDLINKS;
2863                 if (res.has_symlinks != 0)
2864                         server->caps |= NFS_CAP_SYMLINKS;
2865                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2866                         server->caps |= NFS_CAP_FILEID;
2867                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2868                         server->caps |= NFS_CAP_MODE;
2869                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2870                         server->caps |= NFS_CAP_NLINK;
2871                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2872                         server->caps |= NFS_CAP_OWNER;
2873                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2874                         server->caps |= NFS_CAP_OWNER_GROUP;
2875                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2876                         server->caps |= NFS_CAP_ATIME;
2877                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2878                         server->caps |= NFS_CAP_CTIME;
2879                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2880                         server->caps |= NFS_CAP_MTIME;
2881 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2882                 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2883                         server->caps |= NFS_CAP_SECURITY_LABEL;
2884 #endif
2885                 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2886                                 sizeof(server->attr_bitmask));
2887                 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2888 
2889                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2890                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2891                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2892                 server->cache_consistency_bitmask[2] = 0;
2893                 server->acl_bitmask = res.acl_bitmask;
2894                 server->fh_expire_type = res.fh_expire_type;
2895         }
2896 
2897         return status;
2898 }
2899 
2900 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2901 {
2902         struct nfs4_exception exception = { };
2903         int err;
2904         do {
2905                 err = nfs4_handle_exception(server,
2906                                 _nfs4_server_capabilities(server, fhandle),
2907                                 &exception);
2908         } while (exception.retry);
2909         return err;
2910 }
2911 
2912 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2913                 struct nfs_fsinfo *info)
2914 {
2915         u32 bitmask[3];
2916         struct nfs4_lookup_root_arg args = {
2917                 .bitmask = bitmask,
2918         };
2919         struct nfs4_lookup_res res = {
2920                 .server = server,
2921                 .fattr = info->fattr,
2922                 .fh = fhandle,
2923         };
2924         struct rpc_message msg = {
2925                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2926                 .rpc_argp = &args,
2927                 .rpc_resp = &res,
2928         };
2929 
2930         bitmask[0] = nfs4_fattr_bitmap[0];
2931         bitmask[1] = nfs4_fattr_bitmap[1];
2932         /*
2933          * Process the label in the upcoming getfattr
2934          */
2935         bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2936 
2937         nfs_fattr_init(info->fattr);
2938         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2939 }
2940 
2941 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2942                 struct nfs_fsinfo *info)
2943 {
2944         struct nfs4_exception exception = { };
2945         int err;
2946         do {
2947                 err = _nfs4_lookup_root(server, fhandle, info);
2948                 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2949                 switch (err) {
2950                 case 0:
2951                 case -NFS4ERR_WRONGSEC:
2952                         goto out;
2953                 default:
2954                         err = nfs4_handle_exception(server, err, &exception);
2955                 }
2956         } while (exception.retry);
2957 out:
2958         return err;
2959 }
2960 
2961 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2962                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2963 {
2964         struct rpc_auth_create_args auth_args = {
2965                 .pseudoflavor = flavor,
2966         };
2967         struct rpc_auth *auth;
2968         int ret;
2969 
2970         auth = rpcauth_create(&auth_args, server->client);
2971         if (IS_ERR(auth)) {
2972                 ret = -EACCES;
2973                 goto out;
2974         }
2975         ret = nfs4_lookup_root(server, fhandle, info);
2976 out:
2977         return ret;
2978 }
2979 
2980 /*
2981  * Retry pseudoroot lookup with various security flavors.  We do this when:
2982  *
2983  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2984  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2985  *
2986  * Returns zero on success, or a negative NFS4ERR value, or a
2987  * negative errno value.
2988  */
2989 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2990                               struct nfs_fsinfo *info)
2991 {
2992         /* Per 3530bis 15.33.5 */
2993         static const rpc_authflavor_t flav_array[] = {
2994                 RPC_AUTH_GSS_KRB5P,
2995                 RPC_AUTH_GSS_KRB5I,
2996                 RPC_AUTH_GSS_KRB5,
2997                 RPC_AUTH_UNIX,                  /* courtesy */
2998                 RPC_AUTH_NULL,
2999         };
3000         int status = -EPERM;
3001         size_t i;
3002 
3003         if (server->auth_info.flavor_len > 0) {
3004                 /* try each flavor specified by user */
3005                 for (i = 0; i < server->auth_info.flavor_len; i++) {
3006                         status = nfs4_lookup_root_sec(server, fhandle, info,
3007                                                 server->auth_info.flavors[i]);
3008                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3009                                 continue;
3010                         break;
3011                 }
3012         } else {
3013                 /* no flavors specified by user, try default list */
3014                 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3015                         status = nfs4_lookup_root_sec(server, fhandle, info,
3016                                                       flav_array[i]);
3017                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3018                                 continue;
3019                         break;
3020                 }
3021         }
3022 
3023         /*
3024          * -EACCESS could mean that the user doesn't have correct permissions
3025          * to access the mount.  It could also mean that we tried to mount
3026          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
3027          * existing mount programs don't handle -EACCES very well so it should
3028          * be mapped to -EPERM instead.
3029          */
3030         if (status == -EACCES)
3031                 status = -EPERM;
3032         return status;
3033 }
3034 
3035 static int nfs4_do_find_root_sec(struct nfs_server *server,
3036                 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3037 {
3038         int mv = server->nfs_client->cl_minorversion;
3039         return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3040 }
3041 
3042 /**
3043  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3044  * @server: initialized nfs_server handle
3045  * @fhandle: we fill in the pseudo-fs root file handle
3046  * @info: we fill in an FSINFO struct
3047  * @auth_probe: probe the auth flavours
3048  *
3049  * Returns zero on success, or a negative errno.
3050  */
3051 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3052                          struct nfs_fsinfo *info,
3053                          bool auth_probe)
3054 {
3055         int status = 0;
3056 
3057         if (!auth_probe)
3058                 status = nfs4_lookup_root(server, fhandle, info);
3059 
3060         if (auth_probe || status == NFS4ERR_WRONGSEC)
3061                 status = nfs4_do_find_root_sec(server, fhandle, info);
3062 
3063         if (status == 0)
3064                 status = nfs4_server_capabilities(server, fhandle);
3065         if (status == 0)
3066                 status = nfs4_do_fsinfo(server, fhandle, info);
3067 
3068         return nfs4_map_errors(status);
3069 }
3070 
3071 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3072                               struct nfs_fsinfo *info)
3073 {
3074         int error;
3075         struct nfs_fattr *fattr = info->fattr;
3076         struct nfs4_label *label = NULL;
3077 
3078         error = nfs4_server_capabilities(server, mntfh);
3079         if (error < 0) {
3080                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3081                 return error;
3082         }
3083 
3084         label = nfs4_label_alloc(server, GFP_KERNEL);
3085         if (IS_ERR(label))
3086                 return PTR_ERR(label);
3087 
3088         error = nfs4_proc_getattr(server, mntfh, fattr, label);
3089         if (error < 0) {
3090                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3091                 goto err_free_label;
3092         }
3093 
3094         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3095             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3096                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3097 
3098 err_free_label:
3099         nfs4_label_free(label);
3100 
3101         return error;
3102 }
3103 
3104 /*
3105  * Get locations and (maybe) other attributes of a referral.
3106  * Note that we'll actually follow the referral later when
3107  * we detect fsid mismatch in inode revalidation
3108  */
3109 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3110                              const struct qstr *name, struct nfs_fattr *fattr,
3111                              struct nfs_fh *fhandle)
3112 {
3113         int status = -ENOMEM;
3114         struct page *page = NULL;
3115         struct nfs4_fs_locations *locations = NULL;
3116 
3117         page = alloc_page(GFP_KERNEL);
3118         if (page == NULL)
3119                 goto out;
3120         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3121         if (locations == NULL)
3122                 goto out;
3123 
3124         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3125         if (status != 0)
3126                 goto out;
3127 
3128         /*
3129          * If the fsid didn't change, this is a migration event, not a
3130          * referral.  Cause us to drop into the exception handler, which
3131          * will kick off migration recovery.
3132          */
3133         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3134                 dprintk("%s: server did not return a different fsid for"
3135                         " a referral at %s\n", __func__, name->name);
3136                 status = -NFS4ERR_MOVED;
3137                 goto out;
3138         }
3139         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3140         nfs_fixup_referral_attributes(&locations->fattr);
3141 
3142         /* replace the lookup nfs_fattr with the locations nfs_fattr */
3143         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3144         memset(fhandle, 0, sizeof(struct nfs_fh));
3145 out:
3146         if (page)
3147                 __free_page(page);
3148         kfree(locations);
3149         return status;
3150 }
3151 
3152 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3153                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3154 {
3155         struct nfs4_getattr_arg args = {
3156                 .fh = fhandle,
3157                 .bitmask = server->attr_bitmask,
3158         };
3159         struct nfs4_getattr_res res = {
3160                 .fattr = fattr,
3161                 .label = label,
3162                 .server = server,
3163         };
3164         struct rpc_message msg = {
3165                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3166                 .rpc_argp = &args,
3167                 .rpc_resp = &res,
3168         };
3169 
3170         args.bitmask = nfs4_bitmask(server, label);
3171 
3172         nfs_fattr_init(fattr);
3173         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3174 }
3175 
3176 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3177                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3178 {
3179         struct nfs4_exception exception = { };
3180         int err;
3181         do {
3182                 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3183                 trace_nfs4_getattr(server, fhandle, fattr, err);
3184                 err = nfs4_handle_exception(server, err,
3185                                 &exception);
3186         } while (exception.retry);
3187         return err;
3188 }
3189 
3190 /* 
3191  * The file is not closed if it is opened due to the a request to change
3192  * the size of the file. The open call will not be needed once the
3193  * VFS layer lookup-intents are implemented.
3194  *
3195  * Close is called when the inode is destroyed.
3196  * If we haven't opened the file for O_WRONLY, we
3197  * need to in the size_change case to obtain a stateid.
3198  *
3199  * Got race?
3200  * Because OPEN is always done by name in nfsv4, it is
3201  * possible that we opened a different file by the same
3202  * name.  We can recognize this race condition, but we
3203  * can't do anything about it besides returning an error.
3204  *
3205  * This will be fixed with VFS changes (lookup-intent).
3206  */
3207 static int
3208 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3209                   struct iattr *sattr)
3210 {
3211         struct inode *inode = dentry->d_inode;
3212         struct rpc_cred *cred = NULL;
3213         struct nfs4_state *state = NULL;
3214         struct nfs4_label *label = NULL;
3215         int status;
3216 
3217         if (pnfs_ld_layoutret_on_setattr(inode) &&
3218             sattr->ia_valid & ATTR_SIZE &&
3219             sattr->ia_size < i_size_read(inode))
3220                 pnfs_commit_and_return_layout(inode);
3221 
3222         nfs_fattr_init(fattr);
3223         
3224         /* Deal with open(O_TRUNC) */
3225         if (sattr->ia_valid & ATTR_OPEN)
3226                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3227 
3228         /* Optimization: if the end result is no change, don't RPC */
3229         if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3230                 return 0;
3231 
3232         /* Search for an existing open(O_WRITE) file */
3233         if (sattr->ia_valid & ATTR_FILE) {
3234                 struct nfs_open_context *ctx;
3235 
3236                 ctx = nfs_file_open_context(sattr->ia_file);
3237                 if (ctx) {
3238                         cred = ctx->cred;
3239                         state = ctx->state;
3240                 }
3241         }
3242 
3243         label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3244         if (IS_ERR(label))
3245                 return PTR_ERR(label);
3246 
3247         status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3248         if (status == 0) {
3249                 nfs_setattr_update_inode(inode, sattr);
3250                 nfs_setsecurity(inode, fattr, label);
3251         }
3252         nfs4_label_free(label);
3253         return status;
3254 }
3255 
3256 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3257                 const struct qstr *name, struct nfs_fh *fhandle,
3258                 struct nfs_fattr *fattr, struct nfs4_label *label)
3259 {
3260         struct nfs_server *server = NFS_SERVER(dir);
3261         int                    status;
3262         struct nfs4_lookup_arg args = {
3263                 .bitmask = server->attr_bitmask,
3264                 .dir_fh = NFS_FH(dir),
3265                 .name = name,
3266         };
3267         struct nfs4_lookup_res res = {
3268                 .server = server,
3269                 .fattr = fattr,
3270                 .label = label,
3271                 .fh = fhandle,
3272         };
3273         struct rpc_message msg = {
3274                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3275                 .rpc_argp = &args,
3276                 .rpc_resp = &res,
3277         };
3278 
3279         args.bitmask = nfs4_bitmask(server, label);
3280 
3281         nfs_fattr_init(fattr);
3282 
3283         dprintk("NFS call  lookup %s\n", name->name);
3284         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3285         dprintk("NFS reply lookup: %d\n", status);
3286         return status;
3287 }
3288 
3289 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3290 {
3291         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3292                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3293         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3294         fattr->nlink = 2;
3295 }
3296 
3297 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3298                                    struct qstr *name, struct nfs_fh *fhandle,
3299                                    struct nfs_fattr *fattr, struct nfs4_label *label)
3300 {
3301         struct nfs4_exception exception = { };
3302         struct rpc_clnt *client = *clnt;
3303         int err;
3304         do {
3305                 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3306                 trace_nfs4_lookup(dir, name, err);
3307                 switch (err) {
3308                 case -NFS4ERR_BADNAME:
3309                         err = -ENOENT;
3310                         goto out;
3311                 case -NFS4ERR_MOVED:
3312                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3313                         goto out;
3314                 case -NFS4ERR_WRONGSEC:
3315                         err = -EPERM;
3316                         if (client != *clnt)
3317                                 goto out;
3318                         client = nfs4_negotiate_security(client, dir, name);
3319                         if (IS_ERR(client))
3320                                 return PTR_ERR(client);
3321 
3322                         exception.retry = 1;
3323                         break;
3324                 default:
3325                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3326                 }
3327         } while (exception.retry);
3328 
3329 out:
3330         if (err == 0)
3331                 *clnt = client;
3332         else if (client != *clnt)
3333                 rpc_shutdown_client(client);
3334 
3335         return err;
3336 }
3337 
3338 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3339                             struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3340                             struct nfs4_label *label)
3341 {
3342         int status;
3343         struct rpc_clnt *client = NFS_CLIENT(dir);
3344 
3345         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3346         if (client != NFS_CLIENT(dir)) {
3347                 rpc_shutdown_client(client);
3348                 nfs_fixup_secinfo_attributes(fattr);
3349         }
3350         return status;
3351 }
3352 
3353 struct rpc_clnt *
3354 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3355                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3356 {
3357         struct rpc_clnt *client = NFS_CLIENT(dir);
3358         int status;
3359 
3360         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3361         if (status < 0)
3362                 return ERR_PTR(status);
3363         return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3364 }
3365 
3366 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3367 {
3368         struct nfs_server *server = NFS_SERVER(inode);
3369         struct nfs4_accessargs args = {
3370                 .fh = NFS_FH(inode),
3371                 .bitmask = server->cache_consistency_bitmask,
3372         };
3373         struct nfs4_accessres res = {
3374                 .server = server,
3375         };
3376         struct rpc_message msg = {
3377                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3378                 .rpc_argp = &args,
3379                 .rpc_resp = &res,
3380                 .rpc_cred = entry->cred,
3381         };
3382         int mode = entry->mask;
3383         int status = 0;
3384 
3385         /*
3386          * Determine which access bits we want to ask for...
3387          */
3388         if (mode & MAY_READ)
3389                 args.access |= NFS4_ACCESS_READ;
3390         if (S_ISDIR(inode->i_mode)) {
3391                 if (mode & MAY_WRITE)
3392                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3393                 if (mode & MAY_EXEC)
3394                         args.access |= NFS4_ACCESS_LOOKUP;
3395         } else {
3396                 if (mode & MAY_WRITE)
3397                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3398                 if (mode & MAY_EXEC)
3399                         args.access |= NFS4_ACCESS_EXECUTE;
3400         }
3401 
3402         res.fattr = nfs_alloc_fattr();
3403         if (res.fattr == NULL)
3404                 return -ENOMEM;
3405 
3406         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3407         if (!status) {
3408                 nfs_access_set_mask(entry, res.access);
3409                 nfs_refresh_inode(inode, res.fattr);
3410         }
3411         nfs_free_fattr(res.fattr);
3412         return status;
3413 }
3414 
3415 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3416 {
3417         struct nfs4_exception exception = { };
3418         int err;
3419         do {
3420                 err = _nfs4_proc_access(inode, entry);
3421                 trace_nfs4_access(inode, err);
3422                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3423                                 &exception);
3424         } while (exception.retry);
3425         return err;
3426 }
3427 
3428 /*
3429  * TODO: For the time being, we don't try to get any attributes
3430  * along with any of the zero-copy operations READ, READDIR,
3431  * READLINK, WRITE.
3432  *
3433  * In the case of the first three, we want to put the GETATTR
3434  * after the read-type operation -- this is because it is hard
3435  * to predict the length of a GETATTR response in v4, and thus
3436  * align the READ data correctly.  This means that the GETATTR
3437  * may end up partially falling into the page cache, and we should
3438  * shift it into the 'tail' of the xdr_buf before processing.
3439  * To do this efficiently, we need to know the total length
3440  * of data received, which doesn't seem to be available outside
3441  * of the RPC layer.
3442  *
3443  * In the case of WRITE, we also want to put the GETATTR after
3444  * the operation -- in this case because we want to make sure
3445  * we get the post-operation mtime and size.
3446  *
3447  * Both of these changes to the XDR layer would in fact be quite
3448  * minor, but I decided to leave them for a subsequent patch.
3449  */
3450 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3451                 unsigned int pgbase, unsigned int pglen)
3452 {
3453         struct nfs4_readlink args = {
3454                 .fh       = NFS_FH(inode),
3455                 .pgbase   = pgbase,
3456                 .pglen    = pglen,
3457                 .pages    = &page,
3458         };
3459         struct nfs4_readlink_res res;
3460         struct rpc_message msg = {
3461                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3462                 .rpc_argp = &args,
3463                 .rpc_resp = &res,
3464         };
3465 
3466         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3467 }
3468 
3469 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3470                 unsigned int pgbase, unsigned int pglen)
3471 {
3472         struct nfs4_exception exception = { };
3473         int err;
3474         do {
3475                 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3476                 trace_nfs4_readlink(inode, err);
3477                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3478                                 &exception);
3479         } while (exception.retry);
3480         return err;
3481 }
3482 
3483 /*
3484  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
3485  */
3486 static int
3487 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3488                  int flags)
3489 {
3490         struct nfs4_label l, *ilabel = NULL;
3491         struct nfs_open_context *ctx;
3492         struct nfs4_state *state;
3493         int opened = 0;
3494         int status = 0;
3495 
3496         ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3497         if (IS_ERR(ctx))
3498                 return PTR_ERR(ctx);
3499 
3500         ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3501 
3502         sattr->ia_mode &= ~current_umask();
3503         state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3504         if (IS_ERR(state)) {
3505                 status = PTR_ERR(state);
3506                 goto out;
3507         }
3508 out:
3509         nfs4_label_release_security(ilabel);
3510         put_nfs_open_context(ctx);
3511         return status;
3512 }
3513 
3514 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3515 {
3516         struct nfs_server *server = NFS_SERVER(dir);
3517         struct nfs_removeargs args = {
3518                 .fh = NFS_FH(dir),
3519                 .name = *name,
3520         };
3521         struct nfs_removeres res = {
3522                 .server = server,
3523         };
3524         struct rpc_message msg = {
3525                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3526                 .rpc_argp = &args,
3527                 .rpc_resp = &res,
3528         };
3529         int status;
3530 
3531         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3532         if (status == 0)
3533                 update_changeattr(dir, &res.cinfo);
3534         return status;
3535 }
3536 
3537 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3538 {
3539         struct nfs4_exception exception = { };
3540         int err;
3541         do {
3542                 err = _nfs4_proc_remove(dir, name);
3543                 trace_nfs4_remove(dir, name, err);
3544                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3545                                 &exception);
3546         } while (exception.retry);
3547         return err;
3548 }
3549 
3550 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3551 {
3552         struct nfs_server *server = NFS_SERVER(dir);
3553         struct nfs_removeargs *args = msg->rpc_argp;
3554         struct nfs_removeres *res = msg->rpc_resp;
3555 
3556         res->server = server;
3557         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3558         nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3559 
3560         nfs_fattr_init(res->dir_attr);
3561 }
3562 
3563 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3564 {
3565         nfs4_setup_sequence(NFS_SERVER(data->dir),
3566                         &data->args.seq_args,
3567                         &data->res.seq_res,
3568                         task);
3569 }
3570 
3571 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3572 {
3573         struct nfs_unlinkdata *data = task->tk_calldata;
3574         struct nfs_removeres *res = &data->res;
3575 
3576         if (!nfs4_sequence_done(task, &res->seq_res))
3577                 return 0;
3578         if (nfs4_async_handle_error(task, res->server, NULL,
3579                                     &data->timeout) == -EAGAIN)
3580                 return 0;
3581         update_changeattr(dir, &res->cinfo);
3582         return 1;
3583 }
3584 
3585 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3586 {
3587         struct nfs_server *server = NFS_SERVER(dir);
3588         struct nfs_renameargs *arg = msg->rpc_argp;
3589         struct nfs_renameres *res = msg->rpc_resp;
3590 
3591         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3592         res->server = server;
3593         nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3594 }
3595 
3596 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3597 {
3598         nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3599                         &data->args.seq_args,
3600                         &data->res.seq_res,
3601                         task);
3602 }
3603 
3604 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3605                                  struct inode *new_dir)
3606 {
3607         struct nfs_renamedata *data = task->tk_calldata;
3608         struct nfs_renameres *res = &data->res;
3609 
3610         if (!nfs4_sequence_done(task, &res->seq_res))
3611                 return 0;
3612         if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
3613                 return 0;
3614 
3615         update_changeattr(old_dir, &res->old_cinfo);
3616         update_changeattr(new_dir, &res->new_cinfo);
3617         return 1;
3618 }
3619 
3620 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3621 {
3622         struct nfs_server *server = NFS_SERVER(inode);
3623         struct nfs4_link_arg arg = {
3624                 .fh     = NFS_FH(inode),
3625                 .dir_fh = NFS_FH(dir),
3626                 .name   = name,
3627                 .bitmask = server->attr_bitmask,
3628         };
3629         struct nfs4_link_res res = {
3630                 .server = server,
3631                 .label = NULL,
3632         };
3633         struct rpc_message msg = {
3634                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3635                 .rpc_argp = &arg,
3636                 .rpc_resp = &res,
3637         };
3638         int status = -ENOMEM;
3639 
3640         res.fattr = nfs_alloc_fattr();
3641         if (res.fattr == NULL)
3642                 goto out;
3643 
3644         res.label = nfs4_label_alloc(server, GFP_KERNEL);
3645         if (IS_ERR(res.label)) {
3646                 status = PTR_ERR(res.label);
3647                 goto out;
3648         }
3649         arg.bitmask = nfs4_bitmask(server, res.label);
3650 
3651         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3652         if (!status) {
3653                 update_changeattr(dir, &res.cinfo);
3654                 status = nfs_post_op_update_inode(inode, res.fattr);
3655                 if (!status)
3656                         nfs_setsecurity(inode, res.fattr, res.label);
3657         }
3658 
3659 
3660         nfs4_label_free(res.label);
3661 
3662 out:
3663         nfs_free_fattr(res.fattr);
3664         return status;
3665 }
3666 
3667 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3668 {
3669         struct nfs4_exception exception = { };
3670         int err;
3671         do {
3672                 err = nfs4_handle_exception(NFS_SERVER(inode),
3673                                 _nfs4_proc_link(inode, dir, name),
3674                                 &exception);
3675         } while (exception.retry);
3676         return err;
3677 }
3678 
3679 struct nfs4_createdata {
3680         struct rpc_message msg;
3681         struct nfs4_create_arg arg;
3682         struct nfs4_create_res res;
3683         struct nfs_fh fh;
3684         struct nfs_fattr fattr;
3685         struct nfs4_label *label;
3686 };
3687 
3688 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3689                 struct qstr *name, struct iattr *sattr, u32 ftype)
3690 {
3691         struct nfs4_createdata *data;
3692 
3693         data = kzalloc(sizeof(*data), GFP_KERNEL);
3694         if (data != NULL) {
3695                 struct nfs_server *server = NFS_SERVER(dir);
3696 
3697                 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3698                 if (IS_ERR(data->label))
3699                         goto out_free;
3700 
3701                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3702                 data->msg.rpc_argp = &data->arg;
3703                 data->msg.rpc_resp = &data->res;
3704                 data->arg.dir_fh = NFS_FH(dir);
3705                 data->arg.server = server;
3706                 data->arg.name = name;
3707                 data->arg.attrs = sattr;
3708                 data->arg.ftype = ftype;
3709                 data->arg.bitmask = nfs4_bitmask(server, data->label);
3710                 data->res.server = server;
3711                 data->res.fh = &data->fh;
3712                 data->res.fattr = &data->fattr;
3713                 data->res.label = data->label;
3714                 nfs_fattr_init(data->res.fattr);
3715         }
3716         return data;
3717 out_free:
3718         kfree(data);
3719         return NULL;
3720 }
3721 
3722 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3723 {
3724         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3725                                     &data->arg.seq_args, &data->res.seq_res, 1);
3726         if (status == 0) {
3727                 update_changeattr(dir, &data->res.dir_cinfo);
3728                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3729         }
3730         return status;
3731 }
3732 
3733 static void nfs4_free_createdata(struct nfs4_createdata *data)
3734 {
3735         nfs4_label_free(data->label);
3736         kfree(data);
3737 }
3738 
3739 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3740                 struct page *page, unsigned int len, struct iattr *sattr,
3741                 struct nfs4_label *label)
3742 {
3743         struct nfs4_createdata *data;
3744         int status = -ENAMETOOLONG;
3745 
3746         if (len > NFS4_MAXPATHLEN)
3747                 goto out;
3748 
3749         status = -ENOMEM;
3750         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3751         if (data == NULL)
3752                 goto out;
3753 
3754         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3755         data->arg.u.symlink.pages = &page;
3756         data->arg.u.symlink.len = len;
3757         data->arg.label = label;
3758         
3759         status = nfs4_do_create(dir, dentry, data);
3760 
3761         nfs4_free_createdata(data);
3762 out:
3763         return status;
3764 }
3765 
3766 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3767                 struct page *page, unsigned int len, struct iattr *sattr)
3768 {
3769         struct nfs4_exception exception = { };
3770         struct nfs4_label l, *label = NULL;
3771         int err;
3772 
3773         label = nfs4_label_init_security(dir, dentry, sattr, &l);
3774 
3775         do {
3776                 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3777                 trace_nfs4_symlink(dir, &dentry->d_name, err);
3778                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3779                                 &exception);
3780         } while (exception.retry);
3781 
3782         nfs4_label_release_security(label);
3783         return err;
3784 }
3785 
3786 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3787                 struct iattr *sattr, struct nfs4_label *label)
3788 {
3789         struct nfs4_createdata *data;
3790         int status = -ENOMEM;
3791 
3792         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3793         if (data == NULL)
3794                 goto out;
3795 
3796         data->arg.label = label;
3797         status = nfs4_do_create(dir, dentry, data);
3798 
3799         nfs4_free_createdata(data);
3800 out:
3801         return status;
3802 }
3803 
3804 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3805                 struct iattr *sattr)
3806 {
3807         struct nfs4_exception exception = { };
3808         struct nfs4_label l, *label = NULL;
3809         int err;
3810 
3811         label = nfs4_label_init_security(dir, dentry, sattr, &l);
3812 
3813         sattr->ia_mode &= ~current_umask();
3814         do {
3815                 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3816                 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3817                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3818                                 &exception);
3819         } while (exception.retry);
3820         nfs4_label_release_security(label);
3821 
3822         return err;
3823 }
3824 
3825 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3826                 u64 cookie, struct page **pages, unsigned int count, int plus)
3827 {
3828         struct inode            *dir = dentry->d_inode;
3829         struct nfs4_readdir_arg args = {
3830                 .fh = NFS_FH(dir),
3831                 .pages = pages,
3832                 .pgbase = 0,
3833                 .count = count,
3834                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3835                 .plus = plus,
3836         };
3837         struct nfs4_readdir_res res;
3838         struct rpc_message msg = {
3839                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3840                 .rpc_argp = &args,
3841                 .rpc_resp = &res,
3842                 .rpc_cred = cred,
3843         };
3844         int                     status;
3845 
3846         dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3847                         dentry,
3848                         (unsigned long long)cookie);
3849         nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3850         res.pgbase = args.pgbase;
3851         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3852         if (status >= 0) {
3853                 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3854                 status += args.pgbase;
3855         }
3856 
3857         nfs_invalidate_atime(dir);
3858 
3859         dprintk("%s: returns %d\n", __func__, status);
3860         return status;
3861 }
3862 
3863 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3864                 u64 cookie, struct page **pages, unsigned int count, int plus)
3865 {
3866         struct nfs4_exception exception = { };
3867         int err;
3868         do {
3869                 err = _nfs4_proc_readdir(dentry, cred, cookie,
3870                                 pages, count, plus);
3871                 trace_nfs4_readdir(dentry->d_inode, err);
3872                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3873                                 &exception);
3874         } while (exception.retry);
3875         return err;
3876 }
3877 
3878 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3879                 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3880 {
3881         struct nfs4_createdata *data;
3882         int mode = sattr->ia_mode;
3883         int status = -ENOMEM;
3884 
3885         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3886         if (data == NULL)
3887                 goto out;
3888 
3889         if (S_ISFIFO(mode))
3890                 data->arg.ftype = NF4FIFO;
3891         else if (S_ISBLK(mode)) {
3892                 data->arg.ftype = NF4BLK;
3893                 data->arg.u.device.specdata1 = MAJOR(rdev);
3894                 data->arg.u.device.specdata2 = MINOR(rdev);
3895         }
3896         else if (S_ISCHR(mode)) {
3897                 data->arg.ftype = NF4CHR;
3898                 data->arg.u.device.specdata1 = MAJOR(rdev);
3899                 data->arg.u.device.specdata2 = MINOR(rdev);
3900         } else if (!S_ISSOCK(mode)) {
3901                 status = -EINVAL;
3902                 goto out_free;
3903         }
3904 
3905         data->arg.label = label;
3906         status = nfs4_do_create(dir, dentry, data);
3907 out_free:
3908         nfs4_free_createdata(data);
3909 out:
3910         return status;
3911 }
3912 
3913 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3914                 struct iattr *sattr, dev_t rdev)
3915 {
3916         struct nfs4_exception exception = { };
3917         struct nfs4_label l, *label = NULL;
3918         int err;
3919 
3920         label = nfs4_label_init_security(dir, dentry, sattr, &l);
3921 
3922         sattr->ia_mode &= ~current_umask();
3923         do {
3924                 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3925                 trace_nfs4_mknod(dir, &dentry->d_name, err);
3926                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3927                                 &exception);
3928         } while (exception.retry);
3929 
3930         nfs4_label_release_security(label);
3931 
3932         return err;
3933 }
3934 
3935 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3936                  struct nfs_fsstat *fsstat)
3937 {
3938         struct nfs4_statfs_arg args = {
3939                 .fh = fhandle,
3940                 .bitmask = server->attr_bitmask,
3941         };
3942         struct nfs4_statfs_res res = {
3943                 .fsstat = fsstat,
3944         };
3945         struct rpc_message msg = {
3946                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3947                 .rpc_argp = &args,
3948                 .rpc_resp = &res,
3949         };
3950 
3951         nfs_fattr_init(fsstat->fattr);
3952         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3953 }
3954 
3955 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3956 {
3957         struct nfs4_exception exception = { };
3958         int err;
3959         do {
3960                 err = nfs4_handle_exception(server,
3961                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3962                                 &exception);
3963         } while (exception.retry);
3964         return err;
3965 }
3966 
3967 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3968                 struct nfs_fsinfo *fsinfo)
3969 {
3970         struct nfs4_fsinfo_arg args = {
3971                 .fh = fhandle,
3972                 .bitmask = server->attr_bitmask,
3973         };
3974         struct nfs4_fsinfo_res res = {
3975                 .fsinfo = fsinfo,
3976         };
3977         struct rpc_message msg = {
3978                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3979                 .rpc_argp = &args,
3980                 .rpc_resp = &res,
3981         };
3982 
3983         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3984 }
3985 
3986 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3987 {
3988         struct nfs4_exception exception = { };
3989         unsigned long now = jiffies;
3990         int err;
3991 
3992         do {
3993                 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3994                 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3995                 if (err == 0) {
3996                         struct nfs_client *clp = server->nfs_client;
3997 
3998                         spin_lock(&clp->cl_lock);
3999                         clp->cl_lease_time = fsinfo->lease_time * HZ;
4000                         clp->cl_last_renewal = now;
4001                         spin_unlock(&clp->cl_lock);
4002                         break;
4003                 }
4004                 err = nfs4_handle_exception(server, err, &exception);
4005         } while (exception.retry);
4006         return err;
4007 }
4008 
4009 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4010 {
4011         int error;
4012 
4013         nfs_fattr_init(fsinfo->fattr);
4014         error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4015         if (error == 0) {
4016                 /* block layout checks this! */
4017                 server->pnfs_blksize = fsinfo->blksize;
4018                 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
4019         }
4020 
4021         return error;
4022 }
4023 
4024 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4025                 struct nfs_pathconf *pathconf)
4026 {
4027         struct nfs4_pathconf_arg args = {
4028                 .fh = fhandle,
4029                 .bitmask = server->attr_bitmask,
4030         };
4031         struct nfs4_pathconf_res res = {
4032                 .pathconf = pathconf,
4033         };
4034         struct rpc_message msg = {
4035                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4036                 .rpc_argp = &args,
4037                 .rpc_resp = &res,
4038         };
4039 
4040         /* None of the pathconf attributes are mandatory to implement */
4041         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4042                 memset(pathconf, 0, sizeof(*pathconf));
4043                 return 0;
4044         }
4045 
4046         nfs_fattr_init(pathconf->fattr);
4047         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4048 }
4049 
4050 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4051                 struct nfs_pathconf *pathconf)
4052 {
4053         struct nfs4_exception exception = { };
4054         int err;
4055 
4056         do {
4057                 err = nfs4_handle_exception(server,
4058                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
4059                                 &exception);
4060         } while (exception.retry);
4061         return err;
4062 }
4063 
4064 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4065                 const struct nfs_open_context *ctx,
4066                 const struct nfs_lock_context *l_ctx,
4067                 fmode_t fmode)
4068 {
4069         const struct nfs_lockowner *lockowner = NULL;
4070 
4071         if (l_ctx != NULL)
4072                 lockowner = &l_ctx->lockowner;
4073         return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4074 }
4075 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4076 
4077 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4078                 const struct nfs_open_context *ctx,
4079                 const struct nfs_lock_context *l_ctx,
4080                 fmode_t fmode)
4081 {
4082         nfs4_stateid current_stateid;
4083 
4084         /* If the current stateid represents a lost lock, then exit */
4085         if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4086                 return true;
4087         return nfs4_stateid_match(stateid, &current_stateid);
4088 }
4089 
4090 static bool nfs4_error_stateid_expired(int err)
4091 {
4092         switch (err) {
4093         case -NFS4ERR_DELEG_REVOKED:
4094         case -NFS4ERR_ADMIN_REVOKED:
4095         case -NFS4ERR_BAD_STATEID:
4096         case -NFS4ERR_STALE_STATEID:
4097         case -NFS4ERR_OLD_STATEID:
4098         case -NFS4ERR_OPENMODE:
4099         case -NFS4ERR_EXPIRED:
4100                 return true;
4101         }
4102         return false;
4103 }
4104 
4105 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4106 {
4107         nfs_invalidate_atime(hdr->inode);
4108 }
4109 
4110 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4111 {
4112         struct nfs_server *server = NFS_SERVER(hdr->inode);
4113 
4114         trace_nfs4_read(hdr, task->tk_status);
4115         if (nfs4_async_handle_error(task, server,
4116                                     hdr->args.context->state,
4117                                     NULL) == -EAGAIN) {
4118                 rpc_restart_call_prepare(task);
4119                 return -EAGAIN;
4120         }
4121 
4122         __nfs4_read_done_cb(hdr);
4123         if (task->tk_status > 0)
4124                 renew_lease(server, hdr->timestamp);
4125         return 0;
4126 }
4127 
4128 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4129                 struct nfs_pgio_args *args)
4130 {
4131 
4132         if (!nfs4_error_stateid_expired(task->tk_status) ||
4133                 nfs4_stateid_is_current(&args->stateid,
4134                                 args->context,
4135                                 args->lock_context,
4136                                 FMODE_READ))
4137                 return false;
4138         rpc_restart_call_prepare(task);
4139         return true;
4140 }
4141 
4142 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4143 {
4144 
4145         dprintk("--> %s\n", __func__);
4146 
4147         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4148                 return -EAGAIN;
4149         if (nfs4_read_stateid_changed(task, &hdr->args))
4150                 return -EAGAIN;
4151         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4152                                     nfs4_read_done_cb(task, hdr);
4153 }
4154 
4155 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4156                                  struct rpc_message *msg)
4157 {
4158         hdr->timestamp   = jiffies;
4159         hdr->pgio_done_cb = nfs4_read_done_cb;
4160         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4161         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4162 }
4163 
4164 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4165                                       struct nfs_pgio_header *hdr)
4166 {
4167         if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4168                         &hdr->args.seq_args,
4169                         &hdr->res.seq_res,
4170                         task))
4171                 return 0;
4172         if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4173                                 hdr->args.lock_context,
4174                                 hdr->rw_ops->rw_mode) == -EIO)
4175                 return -EIO;
4176         if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4177                 return -EIO;
4178         return 0;
4179 }
4180 
4181 static int nfs4_write_done_cb(struct rpc_task *task,
4182                               struct nfs_pgio_header *hdr)
4183 {
4184         struct inode *inode = hdr->inode;
4185 
4186         trace_nfs4_write(hdr, task->tk_status);
4187         if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4188                                     hdr->args.context->state,
4189                                     NULL) == -EAGAIN) {
4190                 rpc_restart_call_prepare(task);
4191                 return -EAGAIN;
4192         }
4193         if (task->tk_status >= 0) {
4194                 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4195                 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
4196         }
4197         return 0;
4198 }
4199 
4200 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4201                 struct nfs_pgio_args *args)
4202 {
4203 
4204         if (!nfs4_error_stateid_expired(task->tk_status) ||
4205                 nfs4_stateid_is_current(&args->stateid,
4206                                 args->context,
4207                                 args->lock_context,
4208                                 FMODE_WRITE))
4209                 return false;
4210         rpc_restart_call_prepare(task);
4211         return true;
4212 }
4213 
4214 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4215 {
4216         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4217                 return -EAGAIN;
4218         if (nfs4_write_stateid_changed(task, &hdr->args))
4219                 return -EAGAIN;
4220         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4221                 nfs4_write_done_cb(task, hdr);
4222 }
4223 
4224 static
4225 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4226 {
4227         /* Don't request attributes for pNFS or O_DIRECT writes */
4228         if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4229                 return false;
4230         /* Otherwise, request attributes if and only if we don't hold
4231          * a delegation
4232          */
4233         return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4234 }
4235 
4236 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4237                                   struct rpc_message *msg)
4238 {
4239         struct nfs_server *server = NFS_SERVER(hdr->inode);
4240 
4241         if (!nfs4_write_need_cache_consistency_data(hdr)) {
4242                 hdr->args.bitmask = NULL;
4243                 hdr->res.fattr = NULL;
4244         } else
4245                 hdr->args.bitmask = server->cache_consistency_bitmask;
4246 
4247         if (!hdr->pgio_done_cb)
4248                 hdr->pgio_done_cb = nfs4_write_done_cb;
4249         hdr->res.server = server;
4250         hdr->timestamp   = jiffies;
4251 
4252         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4253         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4254 }
4255 
4256 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4257 {
4258         nfs4_setup_sequence(NFS_SERVER(data->inode),
4259                         &data->args.seq_args,
4260                         &data->res.seq_res,
4261                         task);
4262 }
4263 
4264 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4265 {
4266         struct inode *inode = data->inode;
4267 
4268         trace_nfs4_commit(data, task->tk_status);
4269         if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4270                                     NULL, NULL) == -EAGAIN) {
4271                 rpc_restart_call_prepare(task);
4272                 return -EAGAIN;
4273         }
4274         return 0;
4275 }
4276 
4277 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4278 {
4279         if (!nfs4_sequence_done(task, &data->res.seq_res))
4280                 return -EAGAIN;
4281         return data->commit_done_cb(task, data);
4282 }
4283 
4284 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4285 {
4286         struct nfs_server *server = NFS_SERVER(data->inode);
4287 
4288         if (data->commit_done_cb == NULL)
4289                 data->commit_done_cb = nfs4_commit_done_cb;
4290         data->res.server = server;
4291         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4292         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4293 }
4294 
4295 struct nfs4_renewdata {
4296         struct nfs_client       *client;
4297         unsigned long           timestamp;
4298 };
4299 
4300 /*
4301  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4302  * standalone procedure for queueing an asynchronous RENEW.
4303  */
4304 static void nfs4_renew_release(void *calldata)
4305 {
4306         struct nfs4_renewdata *data = calldata;
4307         struct nfs_client *clp = data->client;
4308 
4309         if (atomic_read(&clp->cl_count) > 1)
4310                 nfs4_schedule_state_renewal(clp);
4311         nfs_put_client(clp);
4312         kfree(data);
4313 }
4314 
4315 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4316 {
4317         struct nfs4_renewdata *data = calldata;
4318         struct nfs_client *clp = data->client;
4319         unsigned long timestamp = data->timestamp;
4320 
4321         trace_nfs4_renew_async(clp, task->tk_status);
4322         switch (task->tk_status) {
4323         case 0:
4324                 break;
4325         case -NFS4ERR_LEASE_MOVED:
4326                 nfs4_schedule_lease_moved_recovery(clp);
4327                 break;
4328         default:
4329                 /* Unless we're shutting down, schedule state recovery! */
4330                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4331                         return;
4332                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4333                         nfs4_schedule_lease_recovery(clp);
4334                         return;
4335                 }
4336                 nfs4_schedule_path_down_recovery(clp);
4337         }
4338         do_renew_lease(clp, timestamp);
4339 }
4340 
4341 static const struct rpc_call_ops nfs4_renew_ops = {
4342         .rpc_call_done = nfs4_renew_done,
4343         .rpc_release = nfs4_renew_release,
4344 };
4345 
4346 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4347 {
4348         struct rpc_message msg = {
4349                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4350                 .rpc_argp       = clp,
4351                 .rpc_cred       = cred,
4352         };
4353         struct nfs4_renewdata *data;
4354 
4355         if (renew_flags == 0)
4356                 return 0;
4357         if (!atomic_inc_not_zero(&clp->cl_count))
4358                 return -EIO;
4359         data = kmalloc(sizeof(*data), GFP_NOFS);
4360         if (data == NULL)
4361                 return -ENOMEM;
4362         data->client = clp;
4363         data->timestamp = jiffies;
4364         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4365                         &nfs4_renew_ops, data);
4366 }
4367 
4368 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4369 {
4370         struct rpc_message msg = {
4371                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4372                 .rpc_argp       = clp,
4373                 .rpc_cred       = cred,
4374         };
4375         unsigned long now = jiffies;
4376         int status;
4377 
4378         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4379         if (status < 0)
4380                 return status;
4381         do_renew_lease(clp, now);
4382         return 0;
4383 }
4384 
4385 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4386 {
4387         return server->caps & NFS_CAP_ACLS;
4388 }
4389 
4390 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4391  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4392  * the stack.
4393  */
4394 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4395 
4396 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4397                 struct page **pages, unsigned int *pgbase)
4398 {
4399         struct page *newpage, **spages;
4400         int rc = 0;
4401         size_t len;
4402         spages = pages;
4403 
4404         do {
4405                 len = min_t(size_t, PAGE_SIZE, buflen);
4406                 newpage = alloc_page(GFP_KERNEL);
4407 
4408                 if (newpage == NULL)
4409                         goto unwind;
4410                 memcpy(page_address(newpage), buf, len);
4411                 buf += len;
4412                 buflen -= len;
4413                 *pages++ = newpage;
4414                 rc++;
4415         } while (buflen != 0);
4416 
4417         return rc;
4418 
4419 unwind:
4420         for(; rc > 0; rc--)
4421                 __free_page(spages[rc-1]);
4422         return -ENOMEM;
4423 }
4424 
4425 struct nfs4_cached_acl {
4426         int cached;
4427         size_t len;
4428         char data[0];
4429 };
4430 
4431 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4432 {
4433         struct nfs_inode *nfsi = NFS_I(inode);
4434 
4435         spin_lock(&inode->i_lock);
4436         kfree(nfsi->nfs4_acl);
4437         nfsi->nfs4_acl = acl;
4438         spin_unlock(&inode->i_lock);
4439 }
4440 
4441 static void nfs4_zap_acl_attr(struct inode *inode)
4442 {
4443         nfs4_set_cached_acl(inode, NULL);
4444 }
4445 
4446 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4447 {
4448         struct nfs_inode *nfsi = NFS_I(inode);
4449         struct nfs4_cached_acl *acl;
4450         int ret = -ENOENT;
4451 
4452         spin_lock(&inode->i_lock);
4453         acl = nfsi->nfs4_acl;
4454         if (acl == NULL)
4455                 goto out;
4456         if (buf == NULL) /* user is just asking for length */
4457                 goto out_len;
4458         if (acl->cached == 0)
4459                 goto out;
4460         ret = -ERANGE; /* see getxattr(2) man page */
4461         if (acl->len > buflen)
4462                 goto out;
4463         memcpy(buf, acl->data, acl->len);
4464 out_len:
4465         ret = acl->len;
4466 out:
4467         spin_unlock(&inode->i_lock);
4468         return ret;
4469 }
4470 
4471 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4472 {
4473         struct nfs4_cached_acl *acl;
4474         size_t buflen = sizeof(*acl) + acl_len;
4475 
4476         if (buflen <= PAGE_SIZE) {
4477                 acl = kmalloc(buflen, GFP_KERNEL);
4478                 if (acl == NULL)
4479                         goto out;
4480                 acl->cached = 1;
4481                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4482         } else {
4483                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4484                 if (acl == NULL)
4485                         goto out;
4486                 acl->cached = 0;
4487         }
4488         acl->len = acl_len;
4489 out:
4490         nfs4_set_cached_acl(inode, acl);
4491 }
4492 
4493 /*
4494  * The getxattr API returns the required buffer length when called with a
4495  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4496  * the required buf.  On a NULL buf, we send a page of data to the server
4497  * guessing that the ACL request can be serviced by a page. If so, we cache
4498  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4499  * the cache. If not so, we throw away the page, and cache the required
4500  * length. The next getxattr call will then produce another round trip to
4501  * the server, this time with the input buf of the required size.
4502  */
4503 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4504 {
4505         struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4506         struct nfs_getaclargs args = {
4507                 .fh = NFS_FH(inode),
4508                 .acl_pages = pages,
4509                 .acl_len = buflen,
4510         };
4511         struct nfs_getaclres res = {
4512                 .acl_len = buflen,
4513         };
4514         struct rpc_message msg = {
4515                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4516                 .rpc_argp = &args,
4517                 .rpc_resp = &res,
4518         };
4519         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4520         int ret = -ENOMEM, i;
4521 
4522         /* As long as we're doing a round trip to the server anyway,
4523          * let's be prepared for a page of acl data. */
4524         if (npages == 0)
4525                 npages = 1;
4526         if (npages > ARRAY_SIZE(pages))
4527                 return -ERANGE;
4528 
4529         for (i = 0; i < npages; i++) {
4530                 pages[i] = alloc_page(GFP_KERNEL);
4531                 if (!pages[i])
4532                         goto out_free;
4533         }
4534 
4535         /* for decoding across pages */
4536         res.acl_scratch = alloc_page(GFP_KERNEL);
4537         if (!res.acl_scratch)
4538                 goto out_free;
4539 
4540         args.acl_len = npages * PAGE_SIZE;
4541         args.acl_pgbase = 0;
4542 
4543         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
4544                 __func__, buf, buflen, npages, args.acl_len);
4545         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4546                              &msg, &args.seq_args, &res.seq_res, 0);
4547         if (ret)
4548                 goto out_free;
4549 
4550         /* Handle the case where the passed-in buffer is too short */
4551         if (res.acl_flags & NFS4_ACL_TRUNC) {
4552                 /* Did the user only issue a request for the acl length? */
4553                 if (buf == NULL)
4554                         goto out_ok;
4555                 ret = -ERANGE;
4556                 goto out_free;
4557         }
4558         nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4559         if (buf) {
4560                 if (res.acl_len > buflen) {
4561                         ret = -ERANGE;
4562                         goto out_free;
4563                 }
4564                 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4565         }
4566 out_ok:
4567         ret = res.acl_len;
4568 out_free:
4569         for (i = 0; i < npages; i++)
4570                 if (pages[i])
4571                         __free_page(pages[i]);
4572         if (res.acl_scratch)
4573                 __free_page(res.acl_scratch);
4574         return ret;
4575 }
4576 
4577 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4578 {
4579         struct nfs4_exception exception = { };
4580         ssize_t ret;
4581         do {
4582                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4583                 trace_nfs4_get_acl(inode, ret);
4584                 if (ret >= 0)
4585                         break;
4586                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4587         } while (exception.retry);
4588         return ret;
4589 }
4590 
4591 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4592 {
4593         struct nfs_server *server = NFS_SERVER(inode);
4594         int ret;
4595 
4596         if (!nfs4_server_supports_acls(server))
4597                 return -EOPNOTSUPP;
4598         ret = nfs_revalidate_inode(server, inode);
4599         if (ret < 0)
4600                 return ret;
4601         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4602                 nfs_zap_acl_cache(inode);
4603         ret = nfs4_read_cached_acl(inode, buf, buflen);
4604         if (ret != -ENOENT)
4605                 /* -ENOENT is returned if there is no ACL or if there is an ACL
4606                  * but no cached acl data, just the acl length */
4607                 return ret;
4608         return nfs4_get_acl_uncached(inode, buf, buflen);
4609 }
4610 
4611 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4612 {
4613         struct nfs_server *server = NFS_SERVER(inode);
4614         struct page *pages[NFS4ACL_MAXPAGES];
4615         struct nfs_setaclargs arg = {
4616                 .fh             = NFS_FH(inode),
4617                 .acl_pages      = pages,
4618                 .acl_len        = buflen,
4619         };
4620         struct nfs_setaclres res;
4621         struct rpc_message msg = {
4622                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4623                 .rpc_argp       = &arg,
4624                 .rpc_resp       = &res,
4625         };
4626         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4627         int ret, i;
4628 
4629         if (!nfs4_server_supports_acls(server))
4630                 return -EOPNOTSUPP;
4631         if (npages > ARRAY_SIZE(pages))
4632                 return -ERANGE;
4633         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4634         if (i < 0)
4635                 return i;
4636         nfs4_inode_return_delegation(inode);
4637         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4638 
4639         /*
4640          * Free each page after tx, so the only ref left is
4641          * held by the network stack
4642          */
4643         for (; i > 0; i--)
4644                 put_page(pages[i-1]);
4645 
4646         /*
4647          * Acl update can result in inode attribute update.
4648          * so mark the attribute cache invalid.
4649          */
4650         spin_lock(&inode->i_lock);
4651         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4652         spin_unlock(&inode->i_lock);
4653         nfs_access_zap_cache(inode);
4654         nfs_zap_acl_cache(inode);
4655         return ret;
4656 }
4657 
4658 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4659 {
4660         struct nfs4_exception exception = { };
4661         int err;
4662         do {
4663                 err = __nfs4_proc_set_acl(inode, buf, buflen);
4664                 trace_nfs4_set_acl(inode, err);
4665                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4666                                 &exception);
4667         } while (exception.retry);
4668         return err;
4669 }
4670 
4671 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4672 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4673                                         size_t buflen)
4674 {
4675         struct nfs_server *server = NFS_SERVER(inode);
4676         struct nfs_fattr fattr;
4677         struct nfs4_label label = {0, 0, buflen, buf};
4678 
4679         u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4680         struct nfs4_getattr_arg arg = {
4681                 .fh             = NFS_FH(inode),
4682                 .bitmask        = bitmask,
4683         };
4684         struct nfs4_getattr_res res = {
4685                 .fattr          = &fattr,
4686                 .label          = &label,
4687                 .server         = server,
4688         };
4689         struct rpc_message msg = {
4690                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4691                 .rpc_argp       = &arg,
4692                 .rpc_resp       = &res,
4693         };
4694         int ret;
4695 
4696         nfs_fattr_init(&fattr);
4697 
4698         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4699         if (ret)
4700                 return ret;
4701         if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4702                 return -ENOENT;
4703         if (buflen < label.len)
4704                 return -ERANGE;
4705         return 0;
4706 }
4707 
4708 static int nfs4_get_security_label(struct inode *inode, void *buf,
4709                                         size_t buflen)
4710 {
4711         struct nfs4_exception exception = { };
4712         int err;
4713 
4714         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4715                 return -EOPNOTSUPP;
4716 
4717         do {
4718                 err = _nfs4_get_security_label(inode, buf, buflen);
4719                 trace_nfs4_get_security_label(inode, err);
4720                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4721                                 &exception);
4722         } while (exception.retry);
4723         return err;
4724 }
4725 
4726 static int _nfs4_do_set_security_label(struct inode *inode,
4727                 struct nfs4_label *ilabel,
4728                 struct nfs_fattr *fattr,
4729                 struct nfs4_label *olabel)
4730 {
4731 
4732         struct iattr sattr = {0};
4733         struct nfs_server *server = NFS_SERVER(inode);
4734         const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4735         struct nfs_setattrargs arg = {
4736                 .fh             = NFS_FH(inode),
4737                 .iap            = &sattr,
4738                 .server         = server,
4739                 .bitmask        = bitmask,
4740                 .label          = ilabel,
4741         };
4742         struct nfs_setattrres res = {
4743                 .fattr          = fattr,
4744                 .label          = olabel,
4745                 .server         = server,
4746         };
4747         struct rpc_message msg = {
4748                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4749                 .rpc_argp       = &arg,
4750                 .rpc_resp       = &res,
4751         };
4752         int status;
4753 
4754         nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4755 
4756         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4757         if (status)
4758                 dprintk("%s failed: %d\n", __func__, status);
4759 
4760         return status;
4761 }
4762 
4763 static int nfs4_do_set_security_label(struct inode *inode,
4764                 struct nfs4_label *ilabel,
4765                 struct nfs_fattr *fattr,
4766                 struct nfs4_label *olabel)
4767 {
4768         struct nfs4_exception exception = { };
4769         int err;
4770 
4771         do {
4772                 err = _nfs4_do_set_security_label(inode, ilabel,
4773                                 fattr, olabel);
4774                 trace_nfs4_set_security_label(inode, err);
4775                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4776                                 &exception);
4777         } while (exception.retry);
4778         return err;
4779 }
4780 
4781 static int
4782 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4783 {
4784         struct nfs4_label ilabel, *olabel = NULL;
4785         struct nfs_fattr fattr;
4786         struct rpc_cred *cred;
4787         struct inode *inode = dentry->d_inode;
4788         int status;
4789 
4790         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4791                 return -EOPNOTSUPP;
4792 
4793         nfs_fattr_init(&fattr);
4794 
4795         ilabel.pi = 0;
4796         ilabel.lfs = 0;
4797         ilabel.label = (char *)buf;
4798         ilabel.len = buflen;
4799 
4800         cred = rpc_lookup_cred();
4801         if (IS_ERR(cred))
4802                 return PTR_ERR(cred);
4803 
4804         olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4805         if (IS_ERR(olabel)) {
4806                 status = -PTR_ERR(olabel);
4807                 goto out;
4808         }
4809 
4810         status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4811         if (status == 0)
4812                 nfs_setsecurity(inode, &fattr, olabel);
4813 
4814         nfs4_label_free(olabel);
4815 out:
4816         put_rpccred(cred);
4817         return status;
4818 }
4819 #endif  /* CONFIG_NFS_V4_SECURITY_LABEL */
4820 
4821 
4822 static int
4823 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4824                         struct nfs4_state *state, long *timeout)
4825 {
4826         struct nfs_client *clp = server->nfs_client;
4827 
4828         if (task->tk_status >= 0)
4829                 return 0;
4830         switch(task->tk_status) {
4831                 case -NFS4ERR_DELEG_REVOKED:
4832                 case -NFS4ERR_ADMIN_REVOKED:
4833                 case -NFS4ERR_BAD_STATEID:
4834                 case -NFS4ERR_OPENMODE:
4835                         if (state == NULL)
4836                                 break;
4837                         if (nfs4_schedule_stateid_recovery(server, state) < 0)
4838                                 goto recovery_failed;
4839                         goto wait_on_recovery;
4840                 case -NFS4ERR_EXPIRED:
4841                         if (state != NULL) {
4842                                 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4843                                         goto recovery_failed;
4844                         }
4845                 case -NFS4ERR_STALE_STATEID:
4846                 case -NFS4ERR_STALE_CLIENTID:
4847                         nfs4_schedule_lease_recovery(clp);
4848                         goto wait_on_recovery;
4849                 case -NFS4ERR_MOVED:
4850                         if (nfs4_schedule_migration_recovery(server) < 0)
4851                                 goto recovery_failed;
4852                         goto wait_on_recovery;
4853                 case -NFS4ERR_LEASE_MOVED:
4854                         nfs4_schedule_lease_moved_recovery(clp);
4855                         goto wait_on_recovery;
4856 #if defined(CONFIG_NFS_V4_1)
4857                 case -NFS4ERR_BADSESSION:
4858                 case -NFS4ERR_BADSLOT:
4859                 case -NFS4ERR_BAD_HIGH_SLOT:
4860                 case -NFS4ERR_DEADSESSION:
4861                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4862                 case -NFS4ERR_SEQ_FALSE_RETRY:
4863                 case -NFS4ERR_SEQ_MISORDERED:
4864                         dprintk("%s ERROR %d, Reset session\n", __func__,
4865                                 task->tk_status);
4866                         nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4867                         goto wait_on_recovery;
4868 #endif /* CONFIG_NFS_V4_1 */
4869                 case -NFS4ERR_DELAY:
4870                         nfs_inc_server_stats(server, NFSIOS_DELAY);
4871                         rpc_delay(task, nfs4_update_delay(timeout));
4872                         goto restart_call;
4873                 case -NFS4ERR_GRACE:
4874                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
4875                 case -NFS4ERR_RETRY_UNCACHED_REP:
4876                 case -NFS4ERR_OLD_STATEID:
4877                         goto restart_call;
4878         }
4879         task->tk_status = nfs4_map_errors(task->tk_status);
4880         return 0;
4881 recovery_failed:
4882         task->tk_status = -EIO;
4883         return 0;
4884 wait_on_recovery:
4885         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4886         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4887                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4888         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4889                 goto recovery_failed;
4890 restart_call:
4891         task->tk_status = 0;
4892         return -EAGAIN;
4893 }
4894 
4895 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4896                                     nfs4_verifier *bootverf)
4897 {
4898         __be32 verf[2];
4899 
4900         if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4901                 /* An impossible timestamp guarantees this value
4902                  * will never match a generated boot time. */
4903                 verf[0] = 0;
4904                 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4905         } else {
4906                 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4907                 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4908                 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4909         }
4910         memcpy(bootverf->data, verf, sizeof(bootverf->data));
4911 }
4912 
4913 static unsigned int
4914 nfs4_init_nonuniform_client_string(struct nfs_client *clp,
4915                                    char *buf, size_t len)
4916 {
4917         unsigned int result;
4918 
4919         if (clp->cl_owner_id != NULL)
4920                 return strlcpy(buf, clp->cl_owner_id, len);
4921 
4922         rcu_read_lock();
4923         result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4924                                 clp->cl_ipaddr,
4925                                 rpc_peeraddr2str(clp->cl_rpcclient,
4926                                                         RPC_DISPLAY_ADDR),
4927                                 rpc_peeraddr2str(clp->cl_rpcclient,
4928                                                         RPC_DISPLAY_PROTO));
4929         rcu_read_unlock();
4930         clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4931         return result;
4932 }
4933 
4934 static unsigned int
4935 nfs4_init_uniform_client_string(struct nfs_client *clp,
4936                                 char *buf, size_t len)
4937 {
4938         const char *nodename = clp->cl_rpcclient->cl_nodename;
4939         unsigned int result;
4940 
4941         if (clp->cl_owner_id != NULL)
4942                 return strlcpy(buf, clp->cl_owner_id, len);
4943 
4944         if (nfs4_client_id_uniquifier[0] != '\0')
4945                 result = scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4946                                 clp->rpc_ops->version,
4947                                 clp->cl_minorversion,
4948                                 nfs4_client_id_uniquifier,
4949                                 nodename);
4950         else
4951                 result = scnprintf(buf, len, "Linux NFSv%u.%u %s",
4952                                 clp->rpc_ops->version, clp->cl_minorversion,
4953                                 nodename);
4954         clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4955         return result;
4956 }
4957 
4958 /*
4959  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4960  * services.  Advertise one based on the address family of the
4961  * clientaddr.
4962  */
4963 static unsigned int
4964 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4965 {
4966         if (strchr(clp->cl_ipaddr, ':') != NULL)
4967                 return scnprintf(buf, len, "tcp6");
4968         else
4969                 return scnprintf(buf, len, "tcp");
4970 }
4971 
4972 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4973 {
4974         struct nfs4_setclientid *sc = calldata;
4975 
4976         if (task->tk_status == 0)
4977                 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4978 }
4979 
4980 static const struct rpc_call_ops nfs4_setclientid_ops = {
4981         .rpc_call_done = nfs4_setclientid_done,
4982 };
4983 
4984 /**
4985  * nfs4_proc_setclientid - Negotiate client ID
4986  * @clp: state data structure
4987  * @program: RPC program for NFSv4 callback service
4988  * @port: IP port number for NFS4 callback service
4989  * @cred: RPC credential to use for this call
4990  * @res: where to place the result
4991  *
4992  * Returns zero, a negative errno, or a negative NFS4ERR status code.
4993  */
4994 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4995                 unsigned short port, struct rpc_cred *cred,
4996                 struct nfs4_setclientid_res *res)
4997 {
4998         nfs4_verifier sc_verifier;
4999         struct nfs4_setclientid setclientid = {
5000                 .sc_verifier = &sc_verifier,
5001                 .sc_prog = program,
5002                 .sc_cb_ident = clp->cl_cb_ident,
5003         };
5004         struct rpc_message msg = {
5005                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5006                 .rpc_argp = &setclientid,
5007                 .rpc_resp = res,
5008                 .rpc_cred = cred,
5009         };
5010         struct rpc_task *task;
5011         struct rpc_task_setup task_setup_data = {
5012                 .rpc_client = clp->cl_rpcclient,
5013                 .rpc_message = &msg,
5014                 .callback_ops = &nfs4_setclientid_ops,
5015                 .callback_data = &setclientid,
5016                 .flags = RPC_TASK_TIMEOUT,
5017         };
5018         int status;
5019 
5020         /* nfs_client_id4 */
5021         nfs4_init_boot_verifier(clp, &sc_verifier);
5022         if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5023                 setclientid.sc_name_len =
5024                                 nfs4_init_uniform_client_string(clp,
5025                                                 setclientid.sc_name,
5026                                                 sizeof(setclientid.sc_name));
5027         else
5028                 setclientid.sc_name_len =
5029                                 nfs4_init_nonuniform_client_string(clp,
5030                                                 setclientid.sc_name,
5031                                                 sizeof(setclientid.sc_name));
5032         /* cb_client4 */
5033         setclientid.sc_netid_len =
5034                                 nfs4_init_callback_netid(clp,
5035                                                 setclientid.sc_netid,
5036                                                 sizeof(setclientid.sc_netid));
5037         setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5038                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5039                                 clp->cl_ipaddr, port >> 8, port & 255);
5040 
5041         dprintk("NFS call  setclientid auth=%s, '%.*s'\n",
5042                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5043                 setclientid.sc_name_len, setclientid.sc_name);
5044         task = rpc_run_task(&task_setup_data);
5045         if (IS_ERR(task)) {
5046                 status = PTR_ERR(task);
5047                 goto out;
5048         }
5049         status = task->tk_status;
5050         if (setclientid.sc_cred) {
5051                 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5052                 put_rpccred(setclientid.sc_cred);
5053         }
5054         rpc_put_task(task);
5055 out:
5056         trace_nfs4_setclientid(clp, status);
5057         dprintk("NFS reply setclientid: %d\n", status);
5058         return status;
5059 }
5060 
5061 /**
5062  * nfs4_proc_setclientid_confirm - Confirm client ID
5063  * @clp: state data structure
5064  * @res: result of a previous SETCLIENTID
5065  * @cred: RPC credential to use for this call
5066  *
5067  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5068  */
5069 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5070                 struct nfs4_setclientid_res *arg,
5071                 struct rpc_cred *cred)
5072 {
5073         struct rpc_message msg = {
5074                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5075                 .rpc_argp = arg,
5076                 .rpc_cred = cred,
5077         };
5078         int status;
5079 
5080         dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
5081                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5082                 clp->cl_clientid);
5083         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5084         trace_nfs4_setclientid_confirm(clp, status);
5085         dprintk("NFS reply setclientid_confirm: %d\n", status);
5086         return status;
5087 }
5088 
5089 struct nfs4_delegreturndata {
5090         struct nfs4_delegreturnargs args;
5091         struct nfs4_delegreturnres res;
5092         struct nfs_fh fh;
5093         nfs4_stateid stateid;
5094         unsigned long timestamp;
5095         struct nfs_fattr fattr;
5096         int rpc_status;
5097         struct inode *inode;
5098         bool roc;
5099         u32 roc_barrier;
5100 };
5101 
5102 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5103 {
5104         struct nfs4_delegreturndata *data = calldata;
5105 
5106         if (!nfs4_sequence_done(task, &data->res.seq_res))
5107                 return;
5108 
5109         trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5110         switch (task->tk_status) {
5111         case 0:
5112                 renew_lease(data->res.server, data->timestamp);
5113         case -NFS4ERR_ADMIN_REVOKED:
5114         case -NFS4ERR_DELEG_REVOKED:
5115         case -NFS4ERR_BAD_STATEID:
5116         case -NFS4ERR_OLD_STATEID:
5117         case -NFS4ERR_STALE_STATEID:
5118         case -NFS4ERR_EXPIRED:
5119                 task->tk_status = 0;
5120                 if (data->roc)
5121                         pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5122                 break;
5123         default:
5124                 if (nfs4_async_handle_error(task, data->res.server,
5125                                             NULL, NULL) == -EAGAIN) {
5126                         rpc_restart_call_prepare(task);
5127                         return;
5128                 }
5129         }
5130         data->rpc_status = task->tk_status;
5131 }
5132 
5133 static void nfs4_delegreturn_release(void *calldata)
5134 {
5135         struct nfs4_delegreturndata *data = calldata;
5136         struct inode *inode = data->inode;
5137 
5138         if (inode) {
5139                 if (data->roc)
5140                         pnfs_roc_release(inode);
5141                 nfs_iput_and_deactive(inode);
5142         }
5143         kfree(calldata);
5144 }
5145 
5146 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5147 {
5148         struct nfs4_delegreturndata *d_data;
5149 
5150         d_data = (struct nfs4_delegreturndata *)data;
5151 
5152         if (d_data->roc &&
5153             pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5154                 return;
5155 
5156         nfs4_setup_sequence(d_data->res.server,
5157                         &d_data->args.seq_args,
5158                         &d_data->res.seq_res,
5159                         task);
5160 }
5161 
5162 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5163         .rpc_call_prepare = nfs4_delegreturn_prepare,
5164         .rpc_call_done = nfs4_delegreturn_done,
5165         .rpc_release = nfs4_delegreturn_release,
5166 };
5167 
5168 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5169 {
5170         struct nfs4_delegreturndata *data;
5171         struct nfs_server *server = NFS_SERVER(inode);
5172         struct rpc_task *task;
5173         struct rpc_message msg = {
5174                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5175                 .rpc_cred = cred,
5176         };
5177         struct rpc_task_setup task_setup_data = {
5178                 .rpc_client = server->client,
5179                 .rpc_message = &msg,
5180                 .callback_ops = &nfs4_delegreturn_ops,
5181                 .flags = RPC_TASK_ASYNC,
5182         };
5183         int status = 0;
5184 
5185         data = kzalloc(sizeof(*data), GFP_NOFS);
5186         if (data == NULL)
5187                 return -ENOMEM;
5188         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5189         data->args.fhandle = &data->fh;
5190         data->args.stateid = &data->stateid;
5191         data->args.bitmask = server->cache_consistency_bitmask;
5192         nfs_copy_fh(&data->fh, NFS_FH(inode));
5193         nfs4_stateid_copy(&data->stateid, stateid);
5194         data->res.fattr = &data->fattr;
5195         data->res.server = server;
5196         nfs_fattr_init(data->res.fattr);
5197         data->timestamp = jiffies;
5198         data->rpc_status = 0;
5199         data->inode = nfs_igrab_and_active(inode);
5200         if (data->inode)
5201                 data->roc = nfs4_roc(inode);
5202 
5203         task_setup_data.callback_data = data;
5204         msg.rpc_argp = &data->args;
5205         msg.rpc_resp = &data->res;
5206         task = rpc_run_task(&task_setup_data);
5207         if (IS_ERR(task))
5208                 return PTR_ERR(task);
5209         if (!issync)
5210                 goto out;
5211         status = nfs4_wait_for_completion_rpc_task(task);
5212         if (status != 0)
5213                 goto out;
5214         status = data->rpc_status;
5215         if (status == 0)
5216                 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5217         else
5218                 nfs_refresh_inode(inode, &data->fattr);
5219 out:
5220         rpc_put_task(task);
5221         return status;
5222 }
5223 
5224 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5225 {
5226         struct nfs_server *server = NFS_SERVER(inode);
5227         struct nfs4_exception exception = { };
5228         int err;
5229         do {
5230                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5231                 trace_nfs4_delegreturn(inode, err);
5232                 switch (err) {
5233                         case -NFS4ERR_STALE_STATEID:
5234                         case -NFS4ERR_EXPIRED:
5235                         case 0:
5236                                 return 0;
5237                 }
5238                 err = nfs4_handle_exception(server, err, &exception);
5239         } while (exception.retry);
5240         return err;
5241 }
5242 
5243 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5244 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5245 
5246 /* 
5247  * sleep, with exponential backoff, and retry the LOCK operation. 
5248  */
5249 static unsigned long
5250 nfs4_set_lock_task_retry(unsigned long timeout)
5251 {
5252         freezable_schedule_timeout_killable_unsafe(timeout);
5253         timeout <<= 1;
5254         if (timeout > NFS4_LOCK_MAXTIMEOUT)
5255                 return NFS4_LOCK_MAXTIMEOUT;
5256         return timeout;
5257 }
5258 
5259 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5260 {
5261         struct inode *inode = state->inode;
5262         struct nfs_server *server = NFS_SERVER(inode);
5263         struct nfs_client *clp = server->nfs_client;
5264         struct nfs_lockt_args arg = {
5265                 .fh = NFS_FH(inode),
5266                 .fl = request,
5267         };
5268         struct nfs_lockt_res res = {
5269                 .denied = request,
5270         };
5271         struct rpc_message msg = {
5272                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5273                 .rpc_argp       = &arg,
5274                 .rpc_resp       = &res,
5275                 .rpc_cred       = state->owner->so_cred,
5276         };
5277         struct nfs4_lock_state *lsp;
5278         int status;
5279 
5280         arg.lock_owner.clientid = clp->cl_clientid;
5281         status = nfs4_set_lock_state(state, request);
5282         if (status != 0)
5283                 goto out;
5284         lsp = request->fl_u.nfs4_fl.owner;
5285         arg.lock_owner.id = lsp->ls_seqid.owner_id;
5286         arg.lock_owner.s_dev = server->s_dev;
5287         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5288         switch (status) {
5289                 case 0:
5290                         request->fl_type = F_UNLCK;
5291                         break;
5292                 case -NFS4ERR_DENIED:
5293                         status = 0;
5294         }
5295         request->fl_ops->fl_release_private(request);
5296         request->fl_ops = NULL;
5297 out:
5298         return status;
5299 }
5300 
5301 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5302 {
5303         struct nfs4_exception exception = { };
5304         int err;
5305 
5306         do {
5307                 err = _nfs4_proc_getlk(state, cmd, request);
5308                 trace_nfs4_get_lock(request, state, cmd, err);
5309                 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5310                                 &exception);
5311         } while (exception.retry);
5312         return err;
5313 }
5314 
5315 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5316 {
5317         int res = 0;
5318         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5319                 case FL_POSIX:
5320                         res = posix_lock_file_wait(file, fl);
5321                         break;
5322                 case FL_FLOCK:
5323                         res = flock_lock_file_wait(file, fl);
5324                         break;
5325                 default:
5326                         BUG();
5327         }
5328         return res;
5329 }
5330 
5331 struct nfs4_unlockdata {
5332         struct nfs_locku_args arg;
5333         struct nfs_locku_res res;
5334         struct nfs4_lock_state *lsp;
5335         struct nfs_open_context *ctx;
5336         struct file_lock fl;
5337         const struct nfs_server *server;
5338         unsigned long timestamp;
5339 };
5340 
5341 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5342                 struct nfs_open_context *ctx,
5343                 struct nfs4_lock_state *lsp,
5344                 struct nfs_seqid *seqid)
5345 {
5346         struct nfs4_unlockdata *p;
5347         struct inode *inode = lsp->ls_state->inode;
5348 
5349         p = kzalloc(sizeof(*p), GFP_NOFS);
5350         if (p == NULL)
5351                 return NULL;
5352         p->arg.fh = NFS_FH(inode);
5353         p->arg.fl = &p->fl;
5354         p->arg.seqid = seqid;
5355         p->res.seqid = seqid;
5356         p->arg.stateid = &lsp->ls_stateid;
5357         p->lsp = lsp;
5358         atomic_inc(&lsp->ls_count);
5359         /* Ensure we don't close file until we're done freeing locks! */
5360         p->ctx = get_nfs_open_context(ctx);
5361         memcpy(&p->fl, fl, sizeof(p->fl));
5362         p->server = NFS_SERVER(inode);
5363         return p;
5364 }
5365 
5366 static void nfs4_locku_release_calldata(void *data)
5367 {
5368         struct nfs4_unlockdata *calldata = data;
5369         nfs_free_seqid(calldata->arg.seqid);
5370         nfs4_put_lock_state(calldata->lsp);
5371         put_nfs_open_context(calldata->ctx);
5372         kfree(calldata);
5373 }
5374 
5375 static void nfs4_locku_done(struct rpc_task *task, void *data)
5376 {
5377         struct nfs4_unlockdata *calldata = data;
5378 
5379         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5380                 return;
5381         switch (task->tk_status) {
5382                 case 0:
5383                         nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5384                                         &calldata->res.stateid);
5385                         renew_lease(calldata->server, calldata->timestamp);
5386                         break;
5387                 case -NFS4ERR_BAD_STATEID:
5388                 case -NFS4ERR_OLD_STATEID:
5389                 case -NFS4ERR_STALE_STATEID:
5390                 case -NFS4ERR_EXPIRED:
5391                         break;
5392                 default:
5393                         if (nfs4_async_handle_error(task, calldata->server,
5394                                                     NULL, NULL) == -EAGAIN)
5395                                 rpc_restart_call_prepare(task);
5396         }
5397         nfs_release_seqid(calldata->arg.seqid);
5398 }
5399 
5400 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5401 {
5402         struct nfs4_unlockdata *calldata = data;
5403 
5404         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5405                 goto out_wait;
5406         if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5407                 /* Note: exit _without_ running nfs4_locku_done */
5408                 goto out_no_action;
5409         }
5410         calldata->timestamp = jiffies;
5411         if (nfs4_setup_sequence(calldata->server,
5412                                 &calldata->arg.seq_args,
5413                                 &calldata->res.seq_res,
5414                                 task) != 0)
5415                 nfs_release_seqid(calldata->arg.seqid);
5416         return;
5417 out_no_action:
5418         task->tk_action = NULL;
5419 out_wait:
5420         nfs4_sequence_done(task, &calldata->res.seq_res);
5421 }
5422 
5423 static const struct rpc_call_ops nfs4_locku_ops = {
5424         .rpc_call_prepare = nfs4_locku_prepare,
5425         .rpc_call_done = nfs4_locku_done,
5426         .rpc_release = nfs4_locku_release_calldata,
5427 };
5428 
5429 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5430                 struct nfs_open_context *ctx,
5431                 struct nfs4_lock_state *lsp,
5432                 struct nfs_seqid *seqid)
5433 {
5434         struct nfs4_unlockdata *data;
5435         struct rpc_message msg = {
5436                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5437                 .rpc_cred = ctx->cred,
5438         };
5439         struct rpc_task_setup task_setup_data = {
5440                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5441                 .rpc_message = &msg,
5442                 .callback_ops = &nfs4_locku_ops,
5443                 .workqueue = nfsiod_workqueue,
5444                 .flags = RPC_TASK_ASYNC,
5445         };
5446 
5447         nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5448                 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5449 
5450         /* Ensure this is an unlock - when canceling a lock, the
5451          * canceled lock is passed in, and it won't be an unlock.
5452          */
5453         fl->fl_type = F_UNLCK;
5454 
5455         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5456         if (data == NULL) {
5457                 nfs_free_seqid(seqid);
5458                 return ERR_PTR(-ENOMEM);
5459         }
5460 
5461         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5462         msg.rpc_argp = &data->arg;
5463         msg.rpc_resp = &data->res;
5464         task_setup_data.callback_data = data;
5465         return rpc_run_task(&task_setup_data);
5466 }
5467 
5468 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5469 {
5470         struct inode *inode = state->inode;
5471         struct nfs4_state_owner *sp = state->owner;
5472         struct nfs_inode *nfsi = NFS_I(inode);
5473         struct nfs_seqid *seqid;
5474         struct nfs4_lock_state *lsp;
5475         struct rpc_task *task;
5476         int status = 0;
5477         unsigned char fl_flags = request->fl_flags;
5478 
5479         status = nfs4_set_lock_state(state, request);
5480         /* Unlock _before_ we do the RPC call */
5481         request->fl_flags |= FL_EXISTS;
5482         /* Exclude nfs_delegation_claim_locks() */
5483         mutex_lock(&sp->so_delegreturn_mutex);
5484         /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5485         down_read(&nfsi->rwsem);
5486         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5487                 up_read(&nfsi->rwsem);
5488                 mutex_unlock(&sp->so_delegreturn_mutex);
5489                 goto out;
5490         }
5491         up_read(&nfsi->rwsem);
5492         mutex_unlock(&sp->so_delegreturn_mutex);
5493         if (status != 0)
5494                 goto out;
5495         /* Is this a delegated lock? */
5496         lsp = request->fl_u.nfs4_fl.owner;
5497         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5498                 goto out;
5499         seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5500         status = -ENOMEM;
5501         if (seqid == NULL)
5502                 goto out;
5503         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5504         status = PTR_ERR(task);
5505         if (IS_ERR(task))
5506                 goto out;
5507         status = nfs4_wait_for_completion_rpc_task(task);
5508         rpc_put_task(task);
5509 out:
5510         request->fl_flags = fl_flags;
5511         trace_nfs4_unlock(request, state, F_SETLK, status);
5512         return status;
5513 }
5514 
5515 struct nfs4_lockdata {
5516         struct nfs_lock_args arg;
5517         struct nfs_lock_res res;
5518         struct nfs4_lock_state *lsp;
5519         struct nfs_open_context *ctx;
5520         struct file_lock fl;
5521         unsigned long timestamp;
5522         int rpc_status;
5523         int cancelled;
5524         struct nfs_server *server;
5525 };
5526 
5527 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5528                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5529                 gfp_t gfp_mask)
5530 {
5531         struct nfs4_lockdata *p;
5532         struct inode *inode = lsp->ls_state->inode;
5533         struct nfs_server *server = NFS_SERVER(inode);
5534 
5535         p = kzalloc(sizeof(*p), gfp_mask);
5536         if (p == NULL)
5537                 return NULL;
5538 
5539         p->arg.fh = NFS_FH(inode);
5540         p->arg.fl = &p->fl;
5541         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5542         if (p->arg.open_seqid == NULL)
5543                 goto out_free;
5544         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5545         if (p->arg.lock_seqid == NULL)
5546                 goto out_free_seqid;
5547         p->arg.lock_stateid = &lsp->ls_stateid;
5548         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5549         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5550         p->arg.lock_owner.s_dev = server->s_dev;
5551         p->res.lock_seqid = p->arg.lock_seqid;
5552         p->lsp = lsp;
5553         p->server = server;
5554         atomic_inc(&lsp->ls_count);
5555         p->ctx = get_nfs_open_context(ctx);
5556         get_file(fl->fl_file);
5557         memcpy(&p->fl, fl, sizeof(p->fl));
5558         return p;
5559 out_free_seqid:
5560         nfs_free_seqid(p->arg.open_seqid);
5561 out_free:
5562         kfree(p);
5563         return NULL;
5564 }
5565 
5566 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5567 {
5568         struct nfs4_lockdata *data = calldata;
5569         struct nfs4_state *state = data->lsp->ls_state;
5570 
5571         dprintk("%s: begin!\n", __func__);
5572         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5573                 goto out_wait;
5574         /* Do we need to do an open_to_lock_owner? */
5575         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5576                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5577                         goto out_release_lock_seqid;
5578                 }
5579                 data->arg.open_stateid = &state->open_stateid;
5580                 data->arg.new_lock_owner = 1;
5581                 data->res.open_seqid = data->arg.open_seqid;
5582         } else
5583                 data->arg.new_lock_owner = 0;
5584         if (!nfs4_valid_open_stateid(state)) {
5585                 data->rpc_status = -EBADF;
5586                 task->tk_action = NULL;
5587                 goto out_release_open_seqid;
5588         }
5589         data->timestamp = jiffies;
5590         if (nfs4_setup_sequence(data->server,
5591                                 &data->arg.seq_args,
5592                                 &data->res.seq_res,
5593                                 task) == 0)
5594                 return;
5595 out_release_open_seqid:
5596         nfs_release_seqid(data->arg.open_seqid);
5597 out_release_lock_seqid:
5598         nfs_release_seqid(data->arg.lock_seqid);
5599 out_wait:
5600         nfs4_sequence_done(task, &data->res.seq_res);
5601         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5602 }
5603 
5604 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5605 {
5606         struct nfs4_lockdata *data = calldata;
5607 
5608         dprintk("%s: begin!\n", __func__);
5609 
5610         if (!nfs4_sequence_done(task, &data->res.seq_res))
5611                 return;
5612 
5613         data->rpc_status = task->tk_status;
5614         if (data->arg.new_lock_owner != 0) {
5615                 if (data->rpc_status == 0)
5616                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5617                 else
5618                         goto out;
5619         }
5620         if (data->rpc_status == 0) {
5621