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

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

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