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