Whamcloud - gitweb
LU-9679 llite: Discard LUSTRE_FPRIVATE()
[fs/lustre-release.git] / lustre / llite / file.c
index cf71134..7c683b7 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2016, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -42,9 +42,7 @@
 #include <linux/file.h>
 #include <linux/sched.h>
 #include <linux/user_namespace.h>
-#ifdef HAVE_UIDGID_HEADER
-# include <linux/uidgid.h>
-#endif
+#include <linux/uidgid.h>
 
 #include <uapi/linux/lustre/lustre_ioctl.h>
 #include <lustre_swab.h>
 #include "llite_internal.h"
 #include "vvp_internal.h"
 
+struct split_param {
+       struct inode    *sp_inode;
+       __u16           sp_mirror_id;
+};
+
+struct pcc_param {
+       __u64   pa_data_version;
+       __u32   pa_archive_id;
+       __u32   pa_layout_gen;
+};
+
 static int
 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
 
@@ -68,6 +77,7 @@ static struct ll_file_data *ll_file_data_get(void)
                return NULL;
 
        fd->fd_write_failed = false;
+       pcc_file_init(&fd->fd_pcc_file);
 
        return fd;
 }
@@ -94,12 +104,15 @@ static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
        op_data->op_attr.ia_mtime = inode->i_mtime;
        op_data->op_attr.ia_ctime = inode->i_ctime;
        op_data->op_attr.ia_size = i_size_read(inode);
-       op_data->op_attr.ia_valid |= ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
-                                    ATTR_MTIME | ATTR_MTIME_SET |
-                                    ATTR_CTIME | ATTR_CTIME_SET;
+       op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
+                                     ATTR_MTIME | ATTR_MTIME_SET |
+                                     ATTR_CTIME);
+       op_data->op_xvalid |= OP_XVALID_CTIME_SET;
        op_data->op_attr_blocks = inode->i_blocks;
        op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
-       op_data->op_handle = och->och_fh;
+       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
+               op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
+       op_data->op_open_handle = och->och_open_handle;
 
        if (och->och_flags & FMODE_WRITE &&
            ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
@@ -131,8 +144,7 @@ static int ll_close_inode_openhandle(struct inode *inode,
 
        if (class_exp2obd(md_exp) == NULL) {
                CERROR("%s: invalid MDC connection handle closing "DFID"\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid));
+                      ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
                GOTO(out, rc = 0);
        }
 
@@ -144,20 +156,64 @@ static int ll_close_inode_openhandle(struct inode *inode,
 
        ll_prepare_close(inode, op_data, och);
        switch (bias) {
-       case MDS_CLOSE_LAYOUT_SWAP:
+       case MDS_CLOSE_LAYOUT_MERGE:
+               /* merge blocks from the victim inode */
+               op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
+               op_data->op_attr.ia_valid |= ATTR_SIZE;
+               op_data->op_xvalid |= OP_XVALID_BLOCKS;
+               /* fallthrough */
+       case MDS_CLOSE_LAYOUT_SPLIT:
+       case MDS_CLOSE_LAYOUT_SWAP: {
+               struct split_param *sp = data;
+
                LASSERT(data != NULL);
-               op_data->op_bias |= MDS_CLOSE_LAYOUT_SWAP;
+               op_data->op_bias |= bias;
                op_data->op_data_version = 0;
                op_data->op_lease_handle = och->och_lease_handle;
-               op_data->op_fid2 = *ll_inode2fid(data);
+               if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
+                       op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
+                       op_data->op_mirror_id = sp->sp_mirror_id;
+               } else {
+                       op_data->op_fid2 = *ll_inode2fid(data);
+               }
+               break;
+       }
+
+       case MDS_CLOSE_RESYNC_DONE: {
+               struct ll_ioc_lease *ioc = data;
+
+               LASSERT(data != NULL);
+               op_data->op_attr_blocks +=
+                       ioc->lil_count * op_data->op_attr_blocks;
+               op_data->op_attr.ia_valid |= ATTR_SIZE;
+               op_data->op_xvalid |= OP_XVALID_BLOCKS;
+               op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
+
+               op_data->op_lease_handle = och->och_lease_handle;
+               op_data->op_data = &ioc->lil_ids[0];
+               op_data->op_data_size =
+                       ioc->lil_count * sizeof(ioc->lil_ids[0]);
+               break;
+       }
+
+       case MDS_PCC_ATTACH: {
+               struct pcc_param *param = data;
+
+               LASSERT(data != NULL);
+               op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
+               op_data->op_archive_id = param->pa_archive_id;
+               op_data->op_data_version = param->pa_data_version;
+               op_data->op_lease_handle = och->och_lease_handle;
                break;
+       }
 
        case MDS_HSM_RELEASE:
                LASSERT(data != NULL);
                op_data->op_bias |= MDS_HSM_RELEASE;
                op_data->op_data_version = *(__u64 *)data;
                op_data->op_lease_handle = och->och_lease_handle;
-               op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
+               op_data->op_attr.ia_valid |= ATTR_SIZE;
+               op_data->op_xvalid |= OP_XVALID_BLOCKS;
                break;
 
        default:
@@ -165,18 +221,28 @@ static int ll_close_inode_openhandle(struct inode *inode,
                break;
        }
 
+       if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
+               op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
+       if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
+               op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
+
        rc = md_close(md_exp, op_data, och->och_mod, &req);
        if (rc != 0 && rc != -EINTR)
                CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
                       md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
 
-       if (rc == 0 &&
-           op_data->op_bias & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)) {
+       if (rc == 0 && op_data->op_bias & bias) {
                struct mdt_body *body;
 
                body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
                if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
                        rc = -EBUSY;
+
+               if (bias & MDS_PCC_ATTACH) {
+                       struct pcc_param *param = data;
+
+                       param->pa_layout_gen = body->mbo_layout_gen;
+               }
        }
 
        ll_finish_md_op_data(op_data);
@@ -184,7 +250,7 @@ static int ll_close_inode_openhandle(struct inode *inode,
 out:
 
        md_clear_open_replay_data(md_exp, och);
-       och->och_fh.cookie = DEAD_HANDLE_MAGIC;
+       och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
        OBD_FREE_PTR(och);
 
        ptlrpc_req_finished(req);       /* This is close request */
@@ -239,7 +305,7 @@ static int ll_md_close(struct inode *inode, struct file *file)
                .l_inodebits    = { MDS_INODELOCK_OPEN },
        };
        __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
-       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data *fd = file->private_data;
        struct ll_inode_info *lli = ll_i2info(inode);
        struct lustre_handle lockh;
        enum ldlm_mode lockmode;
@@ -286,12 +352,14 @@ static int ll_md_close(struct inode *inode, struct file *file)
        }
        mutex_unlock(&lli->lli_och_mutex);
 
-       if (!md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
+       /* LU-4398: do not cache write open lock if the file has exec bit */
+       if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
+           !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
                           LDLM_IBITS, &policy, lockmode, &lockh))
                rc = ll_md_real_close(inode, fd->fd_omode);
 
 out:
-       LUSTRE_FPRIVATE(file) = NULL;
+       file->private_data = NULL;
        ll_file_data_put(fd);
 
        RETURN(rc);
@@ -304,19 +372,19 @@ out:
  */
 int ll_file_release(struct inode *inode, struct file *file)
 {
-        struct ll_file_data *fd;
-        struct ll_sb_info *sbi = ll_i2sbi(inode);
-        struct ll_inode_info *lli = ll_i2info(inode);
-        int rc;
-        ENTRY;
+       struct ll_file_data *fd;
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       struct ll_inode_info *lli = ll_i2info(inode);
+       ktime_t kstart = ktime_get();
+       int rc;
+
+       ENTRY;
 
        CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
               PFID(ll_inode2fid(inode)), inode);
 
-       if (inode->i_sb->s_root != file_dentry(file))
-                ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
-        fd = LUSTRE_FPRIVATE(file);
-        LASSERT(fd != NULL);
+       fd = file->private_data;
+       LASSERT(fd != NULL);
 
        /* The last ref on @file, maybe not the the owner pid of statahead,
         * because parent and child process can share the same file handle. */
@@ -324,11 +392,13 @@ int ll_file_release(struct inode *inode, struct file *file)
                ll_deauthorize_statahead(inode, fd);
 
        if (inode->i_sb->s_root == file_dentry(file)) {
-               LUSTRE_FPRIVATE(file) = NULL;
+               file->private_data = NULL;
                ll_file_data_put(fd);
-               RETURN(0);
+               GOTO(out, rc = 0);
        }
 
+       pcc_file_release(inode, file);
+
        if (!S_ISDIR(inode->i_mode)) {
                if (lli->lli_clob != NULL)
                        lov_read_and_clear_async_rc(lli->lli_clob);
@@ -340,15 +410,114 @@ int ll_file_release(struct inode *inode, struct file *file)
        if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
                libcfs_debug_dumplog();
 
+out:
+       if (!rc && inode->i_sb->s_root != file_dentry(file))
+               ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
+                                  ktime_us_delta(ktime_get(), kstart));
        RETURN(rc);
 }
 
+static inline int ll_dom_readpage(void *data, struct page *page)
+{
+       struct niobuf_local *lnb = data;
+       void *kaddr;
+
+       kaddr = ll_kmap_atomic(page, KM_USER0);
+       memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
+       if (lnb->lnb_len < PAGE_SIZE)
+               memset(kaddr + lnb->lnb_len, 0,
+                      PAGE_SIZE - lnb->lnb_len);
+       flush_dcache_page(page);
+       SetPageUptodate(page);
+       ll_kunmap_atomic(kaddr, KM_USER0);
+       unlock_page(page);
+
+       return 0;
+}
+
+void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
+                       struct lookup_intent *it)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct cl_object *obj = lli->lli_clob;
+       struct address_space *mapping = inode->i_mapping;
+       struct page *vmpage;
+       struct niobuf_remote *rnb;
+       struct mdt_body *body;
+       char *data;
+       unsigned long index, start;
+       struct niobuf_local lnb;
+
+       ENTRY;
+
+       if (obj == NULL)
+               RETURN_EXIT;
+
+       if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
+                                      RCL_SERVER))
+               RETURN_EXIT;
+
+       rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
+       if (rnb == NULL || rnb->rnb_len == 0)
+               RETURN_EXIT;
+
+       /* LU-11595: Server may return whole file and that is OK always or
+        * it may return just file tail and its offset must be aligned with
+        * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
+        * smaller then offset may be not aligned and that data is just ignored.
+        */
+       if (rnb->rnb_offset & ~PAGE_MASK)
+               RETURN_EXIT;
+
+       /* Server returns whole file or just file tail if it fills in reply
+        * buffer, in both cases total size should be equal to the file size.
+        */
+       body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
+       if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size) {
+               CERROR("%s: server returns off/len %llu/%u but size %llu\n",
+                      ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
+                      rnb->rnb_len, body->mbo_dom_size);
+               RETURN_EXIT;
+       }
+
+       CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
+              rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
+
+       data = (char *)rnb + sizeof(*rnb);
+
+       lnb.lnb_file_offset = rnb->rnb_offset;
+       start = lnb.lnb_file_offset >> PAGE_SHIFT;
+       index = 0;
+       LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
+       lnb.lnb_page_offset = 0;
+       do {
+               lnb.lnb_data = data + (index << PAGE_SHIFT);
+               lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
+               if (lnb.lnb_len > PAGE_SIZE)
+                       lnb.lnb_len = PAGE_SIZE;
+
+               vmpage = read_cache_page(mapping, index + start,
+                                        ll_dom_readpage, &lnb);
+               if (IS_ERR(vmpage)) {
+                       CWARN("%s: cannot fill page %lu for "DFID
+                             " with data: rc = %li\n",
+                             ll_i2sbi(inode)->ll_fsname, index + start,
+                             PFID(lu_object_fid(&obj->co_lu)),
+                             PTR_ERR(vmpage));
+                       break;
+               }
+               put_page(vmpage);
+               index++;
+       } while (rnb->rnb_len > (index << PAGE_SHIFT));
+       EXIT;
+}
+
 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
                                struct lookup_intent *itp)
 {
        struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
        struct dentry *parent = de->d_parent;
-       const char *name = NULL;
+       char *name = NULL;
        int len = 0;
        struct md_op_data *op_data;
        struct ptlrpc_request *req = NULL;
@@ -360,21 +529,43 @@ static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
 
        /* if server supports open-by-fid, or file name is invalid, don't pack
         * name in open request */
-       if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
-           lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
-               name = de->d_name.name;
+       if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
+           !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
+retry:
                len = de->d_name.len;
+               name = kmalloc(len + 1, GFP_NOFS);
+               if (!name)
+                       RETURN(-ENOMEM);
+
+               /* race here */
+               spin_lock(&de->d_lock);
+               if (len != de->d_name.len) {
+                       spin_unlock(&de->d_lock);
+                       kfree(name);
+                       goto retry;
+               }
+               memcpy(name, de->d_name.name, len);
+               name[len] = '\0';
+               spin_unlock(&de->d_lock);
+
+               if (!lu_name_is_valid_2(name, len)) {
+                       kfree(name);
+                       RETURN(-ESTALE);
+               }
        }
 
        op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
                                     name, len, 0, LUSTRE_OPC_ANY, NULL);
-       if (IS_ERR(op_data))
+       if (IS_ERR(op_data)) {
+               kfree(name);
                RETURN(PTR_ERR(op_data));
+       }
        op_data->op_data = lmm;
        op_data->op_data_size = lmmsize;
 
        rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
                            &ll_md_blocking_ast, 0);
+       kfree(name);
        ll_finish_md_op_data(op_data);
        if (rc == -ESTALE) {
                /* reason for keep own exit path - don`t flood log
@@ -397,8 +588,30 @@ static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
        }
 
        rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
-       if (!rc && itp->it_lock_mode)
+
+       if (!rc && itp->it_lock_mode) {
+               struct lustre_handle handle = {.cookie = itp->it_lock_handle};
+               struct ldlm_lock *lock;
+               bool has_dom_bit = false;
+
+               /* If we got a lock back and it has a LOOKUP bit set,
+                * make sure the dentry is marked as valid so we can find it.
+                * We don't need to care about actual hashing since other bits
+                * of kernel will deal with that later.
+                */
+               lock = ldlm_handle2lock(&handle);
+               if (lock) {
+                       has_dom_bit = ldlm_has_dom(lock);
+                       if (lock->l_policy_data.l_inodebits.bits &
+                           MDS_INODELOCK_LOOKUP)
+                               d_lustre_revalidate(de);
+
+                       LDLM_LOCK_PUT(lock);
+               }
                ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
+               if (has_dom_bit)
+                       ll_dom_finish_open(de->d_inode, req, itp);
+       }
 
 out:
        ptlrpc_req_finished(req);
@@ -423,7 +636,7 @@ static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
        struct mdt_body *body;
 
        body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
-       och->och_fh = body->mbo_handle;
+       och->och_open_handle = body->mbo_open_handle;
        och->och_fid = body->mbo_fid1;
        och->och_lease_handle.cookie = it->it_lock_handle;
        och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
@@ -438,7 +651,7 @@ static int ll_local_open(struct file *file, struct lookup_intent *it,
        struct inode *inode = file_inode(file);
        ENTRY;
 
-       LASSERT(!LUSTRE_FPRIVATE(file));
+       LASSERT(!file->private_data);
 
        LASSERT(fd != NULL);
 
@@ -450,7 +663,7 @@ static int ll_local_open(struct file *file, struct lookup_intent *it,
                        RETURN(rc);
        }
 
-       LUSTRE_FPRIVATE(file) = fd;
+       file->private_data = fd;
        ll_readahead_init(inode, &fd->fd_ras);
        fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
 
@@ -482,6 +695,7 @@ int ll_file_open(struct inode *inode, struct file *file)
        struct obd_client_handle **och_p = NULL;
        __u64 *och_usecount = NULL;
        struct ll_file_data *fd;
+       ktime_t kstart = ktime_get();
        int rc = 0;
        ENTRY;
 
@@ -493,89 +707,93 @@ int ll_file_open(struct inode *inode, struct file *file)
 
        fd = ll_file_data_get();
        if (fd == NULL)
-               GOTO(out_openerr, rc = -ENOMEM);
+               GOTO(out_nofiledata, rc = -ENOMEM);
 
        fd->fd_file = file;
        if (S_ISDIR(inode->i_mode))
                ll_authorize_statahead(inode, fd);
 
        if (inode->i_sb->s_root == file_dentry(file)) {
-                LUSTRE_FPRIVATE(file) = fd;
-                RETURN(0);
-        }
+               file->private_data = fd;
+               RETURN(0);
+       }
 
        if (!it || !it->it_disposition) {
-                /* Convert f_flags into access mode. We cannot use file->f_mode,
-                 * because everything but O_ACCMODE mask was stripped from
-                 * there */
-                if ((oit.it_flags + 1) & O_ACCMODE)
-                        oit.it_flags++;
-                if (file->f_flags & O_TRUNC)
-                        oit.it_flags |= FMODE_WRITE;
-
-                /* kernel only call f_op->open in dentry_open.  filp_open calls
-                 * dentry_open after call to open_namei that checks permissions.
-                 * Only nfsd_open call dentry_open directly without checking
-                 * permissions and because of that this code below is safe. */
-                if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
-                        oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
-
-                /* We do not want O_EXCL here, presumably we opened the file
-                 * already? XXX - NFS implications? */
-                oit.it_flags &= ~O_EXCL;
-
-                /* bug20584, if "it_flags" contains O_CREAT, the file will be
-                 * created if necessary, then "IT_CREAT" should be set to keep
-                 * consistent with it */
-                if (oit.it_flags & O_CREAT)
-                        oit.it_op |= IT_CREAT;
-
-                it = &oit;
-        }
+               /* Convert f_flags into access mode. We cannot use file->f_mode,
+                * because everything but O_ACCMODE mask was stripped from
+                * there */
+               if ((oit.it_flags + 1) & O_ACCMODE)
+                       oit.it_flags++;
+               if (file->f_flags & O_TRUNC)
+                       oit.it_flags |= FMODE_WRITE;
+
+               /* kernel only call f_op->open in dentry_open.  filp_open calls
+                * dentry_open after call to open_namei that checks permissions.
+                * Only nfsd_open call dentry_open directly without checking
+                * permissions and because of that this code below is safe.
+                */
+               if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
+                       oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
+
+               /* We do not want O_EXCL here, presumably we opened the file
+                * already? XXX - NFS implications? */
+               oit.it_flags &= ~O_EXCL;
+
+               /* bug20584, if "it_flags" contains O_CREAT, the file will be
+                * created if necessary, then "IT_CREAT" should be set to keep
+                * consistent with it */
+               if (oit.it_flags & O_CREAT)
+                       oit.it_op |= IT_CREAT;
+
+               it = &oit;
+       }
 
 restart:
-        /* Let's see if we have file open on MDS already. */
-        if (it->it_flags & FMODE_WRITE) {
-                och_p = &lli->lli_mds_write_och;
-                och_usecount = &lli->lli_open_fd_write_count;
-        } else if (it->it_flags & FMODE_EXEC) {
-                och_p = &lli->lli_mds_exec_och;
-                och_usecount = &lli->lli_open_fd_exec_count;
-         } else {
-                och_p = &lli->lli_mds_read_och;
-                och_usecount = &lli->lli_open_fd_read_count;
-        }
+       /* Let's see if we have file open on MDS already. */
+       if (it->it_flags & FMODE_WRITE) {
+               och_p = &lli->lli_mds_write_och;
+               och_usecount = &lli->lli_open_fd_write_count;
+       } else if (it->it_flags & FMODE_EXEC) {
+               och_p = &lli->lli_mds_exec_och;
+               och_usecount = &lli->lli_open_fd_exec_count;
+       } else {
+               och_p = &lli->lli_mds_read_och;
+               och_usecount = &lli->lli_open_fd_read_count;
+       }
 
        mutex_lock(&lli->lli_och_mutex);
-        if (*och_p) { /* Open handle is present */
-                if (it_disposition(it, DISP_OPEN_OPEN)) {
-                        /* Well, there's extra open request that we do not need,
-                           let's close it somehow. This will decref request. */
-                        rc = it_open_error(DISP_OPEN_OPEN, it);
-                        if (rc) {
+       if (*och_p) { /* Open handle is present */
+               if (it_disposition(it, DISP_OPEN_OPEN)) {
+                       /* Well, there's extra open request that we do not need,
+                        * let's close it somehow. This will decref request. */
+                       rc = it_open_error(DISP_OPEN_OPEN, it);
+                       if (rc) {
                                mutex_unlock(&lli->lli_och_mutex);
-                                GOTO(out_openerr, rc);
-                        }
+                               GOTO(out_openerr, rc);
+                       }
 
                        ll_release_openhandle(file_dentry(file), it);
-                }
-                (*och_usecount)++;
+               }
+               (*och_usecount)++;
 
-                rc = ll_local_open(file, it, fd, NULL);
-                if (rc) {
-                        (*och_usecount)--;
+               rc = ll_local_open(file, it, fd, NULL);
+               if (rc) {
+                       (*och_usecount)--;
                        mutex_unlock(&lli->lli_och_mutex);
-                        GOTO(out_openerr, rc);
-                }
-        } else {
-                LASSERT(*och_usecount == 0);
+                       GOTO(out_openerr, rc);
+               }
+       } else {
+               LASSERT(*och_usecount == 0);
                if (!it->it_disposition) {
-                       struct ll_dentry_data *ldd = ll_d2d(file->f_path.dentry);
-                        /* We cannot just request lock handle now, new ELC code
-                           means that one of other OPEN locks for this file
-                           could be cancelled, and since blocking ast handler
-                           would attempt to grab och_mutex as well, that would
-                           result in a deadlock */
+                       struct dentry *dentry = file_dentry(file);
+                       struct ll_dentry_data *ldd;
+
+                       /* We cannot just request lock handle now, new ELC code
+                        * means that one of other OPEN locks for this file
+                        * could be cancelled, and since blocking ast handler
+                        * would attempt to grab och_mutex as well, that would
+                        * result in a deadlock
+                        */
                        mutex_unlock(&lli->lli_och_mutex);
                        /*
                         * Normally called under two situations:
@@ -592,34 +810,37 @@ restart:
                         *  lookup path only, since ll_iget_for_nfs always calls
                         *  ll_d_init().
                         */
+                       ldd = ll_d2d(dentry);
                        if (ldd && ldd->lld_nfs_dentry) {
                                ldd->lld_nfs_dentry = 0;
-                               it->it_flags |= MDS_OPEN_LOCK;
+                               if (!filename_is_volatile(dentry->d_name.name,
+                                                         dentry->d_name.len,
+                                                         NULL))
+                                       it->it_flags |= MDS_OPEN_LOCK;
                        }
 
-                        /*
+                       /*
                         * Always specify MDS_OPEN_BY_FID because we don't want
                         * to get file with different fid.
                         */
                        it->it_flags |= MDS_OPEN_BY_FID;
-                       rc = ll_intent_file_open(file_dentry(file), NULL, 0,
-                                                it);
-                        if (rc)
-                                GOTO(out_openerr, rc);
+                       rc = ll_intent_file_open(dentry, NULL, 0, it);
+                       if (rc)
+                               GOTO(out_openerr, rc);
 
-                        goto restart;
-                }
-                OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
-                if (!*och_p)
-                        GOTO(out_och_free, rc = -ENOMEM);
+                       goto restart;
+               }
+               OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
+               if (!*och_p)
+                       GOTO(out_och_free, rc = -ENOMEM);
 
-                (*och_usecount)++;
+               (*och_usecount)++;
 
-                /* md_intent_lock() didn't get a request ref if there was an
-                 * open error, so don't do cleanup on the request here
-                 * (bug 3430) */
-                /* XXX (green): Should not we bail out on any error here, not
-                 * just open error? */
+               /* md_intent_lock() didn't get a request ref if there was an
+                * open error, so don't do cleanup on the request here
+                * (bug 3430) */
+               /* XXX (green): Should not we bail out on any error here, not
+                * just open error? */
                rc = it_open_error(DISP_OPEN_OPEN, it);
                if (rc != 0)
                        GOTO(out_och_free, rc);
@@ -632,42 +853,53 @@ restart:
                if (rc)
                        GOTO(out_och_free, rc);
        }
+
+       rc = pcc_file_open(inode, file);
+       if (rc)
+               GOTO(out_och_free, rc);
+
        mutex_unlock(&lli->lli_och_mutex);
-        fd = NULL;
 
-        /* Must do this outside lli_och_mutex lock to prevent deadlock where
-           different kind of OPEN lock for this same inode gets cancelled
-           by ldlm_cancel_lru */
-        if (!S_ISREG(inode->i_mode))
-                GOTO(out_och_free, rc);
+       /* lockless for direct IO so that it can do IO in parallel */
+       if (file->f_flags & O_DIRECT)
+               fd->fd_flags |= LL_FILE_LOCKLESS_IO;
+       fd = NULL;
 
+       /* Must do this outside lli_och_mutex lock to prevent deadlock where
+          different kind of OPEN lock for this same inode gets cancelled
+          by ldlm_cancel_lru */
+       if (!S_ISREG(inode->i_mode))
+               GOTO(out_och_free, rc);
        cl_lov_delay_create_clear(&file->f_flags);
        GOTO(out_och_free, rc);
 
 out_och_free:
-        if (rc) {
-                if (och_p && *och_p) {
-                        OBD_FREE(*och_p, sizeof (struct obd_client_handle));
-                        *och_p = NULL; /* OBD_FREE writes some magic there */
-                        (*och_usecount)--;
-                }
+       if (rc) {
+               if (och_p && *och_p) {
+                       OBD_FREE(*och_p, sizeof(struct obd_client_handle));
+                       *och_p = NULL; /* OBD_FREE writes some magic there */
+                       (*och_usecount)--;
+               }
                mutex_unlock(&lli->lli_och_mutex);
 
 out_openerr:
                if (lli->lli_opendir_key == fd)
                        ll_deauthorize_statahead(inode, fd);
+
                if (fd != NULL)
                        ll_file_data_put(fd);
-        } else {
-                ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
-        }
+       } else {
+               ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
+                                  ktime_us_delta(ktime_get(), kstart));
+       }
 
+out_nofiledata:
        if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
                ptlrpc_req_finished(it->it_request);
                it_clear_disposition(it, DISP_ENQ_OPEN_REF);
        }
 
-        return rc;
+       return rc;
 }
 
 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
@@ -699,10 +931,10 @@ static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
  * if it has an open lock in cache already.
  */
 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
-                               struct lustre_handle *old_handle)
+                               struct lustre_handle *old_open_handle)
 {
        struct ll_inode_info *lli = ll_i2info(inode);
-       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data *fd = file->private_data;
        struct obd_client_handle **och_p;
        __u64 *och_usecount;
        int rc = 0;
@@ -732,7 +964,7 @@ static int ll_lease_och_acquire(struct inode *inode, struct file *file,
                *och_p = NULL;
        }
 
-       *old_handle = fd->fd_och->och_fh;
+       *old_open_handle = fd->fd_och->och_open_handle;
 
        EXIT;
 out_unlock:
@@ -746,7 +978,7 @@ out_unlock:
 static int ll_lease_och_release(struct inode *inode, struct file *file)
 {
        struct ll_inode_info *lli = ll_i2info(inode);
-       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data *fd = file->private_data;
        struct obd_client_handle **och_p;
        struct obd_client_handle *old_och = NULL;
        __u64 *och_usecount;
@@ -793,7 +1025,7 @@ ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
        struct ll_sb_info *sbi = ll_i2sbi(inode);
        struct md_op_data *op_data;
        struct ptlrpc_request *req = NULL;
-       struct lustre_handle old_handle = { 0 };
+       struct lustre_handle old_open_handle = { 0 };
        struct obd_client_handle *och = NULL;
        int rc;
        int rc2;
@@ -806,7 +1038,7 @@ ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
                if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
                        RETURN(ERR_PTR(-EPERM));
 
-               rc = ll_lease_och_acquire(inode, file, &old_handle);
+               rc = ll_lease_och_acquire(inode, file, &old_open_handle);
                if (rc)
                        RETURN(ERR_PTR(rc));
        }
@@ -821,7 +1053,7 @@ ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
                GOTO(out, rc = PTR_ERR(op_data));
 
        /* To tell the MDT this openhandle is from the same owner */
-       op_data->op_handle = old_handle;
+       op_data->op_open_handle = old_open_handle;
 
        it.it_flags = fmode | open_flags;
        it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
@@ -847,7 +1079,9 @@ ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
                GOTO(out_release_it, rc);
 
        LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
-       ll_och_fill(sbi->ll_md_exp, &it, och);
+       rc = ll_och_fill(sbi->ll_md_exp, &it, och);
+       if (rc)
+               GOTO(out_release_it, rc);
 
        if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
                GOTO(out_close, rc = -EOPNOTSUPP);
@@ -877,8 +1111,7 @@ out_close:
        rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
        if (rc2 < 0)
                CERROR("%s: error closing file "DFID": %d\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&ll_i2info(inode)->lli_fid), rc2);
+                      sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
        och = NULL; /* och has been freed in ll_close_inode_openhandle() */
 out_release_it:
        ll_intent_release(&it);
@@ -922,7 +1155,7 @@ static int ll_swap_layouts_close(struct obd_client_handle *och,
        ENTRY;
 
        CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
-              ll_get_fsname(inode->i_sb, NULL, 0), PFID(fid1));
+              ll_i2sbi(inode)->ll_fsname, PFID(fid1));
 
        rc = ll_check_swap_layouts_validity(inode, inode2);
        if (rc < 0)
@@ -935,7 +1168,7 @@ static int ll_swap_layouts_close(struct obd_client_handle *och,
        if (rc == 0)
                GOTO(out_free_och, rc = -EINVAL);
 
-       /* Close the file and swap layouts between inode & inode2.
+       /* Close the file and {swap,merge} layouts between inode & inode2.
         * NB: lease lock handle is released in mdc_close_layout_swap_pack()
         * because we still need it to pack l_remote_handle to MDT. */
        rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
@@ -954,8 +1187,10 @@ out_free_och:
  * Release lease and close the file.
  * It will check if the lease has ever broken.
  */
-static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
-                         bool *lease_broken)
+static int ll_lease_close_intent(struct obd_client_handle *och,
+                                struct inode *inode,
+                                bool *lease_broken, enum mds_op_bias bias,
+                                void *data)
 {
        struct ldlm_lock *lock;
        bool cancelled = true;
@@ -970,19 +1205,71 @@ static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
                LDLM_LOCK_PUT(lock);
        }
 
-       CDEBUG(D_INODE, "lease for "DFID" broken? %d\n",
-              PFID(&ll_i2info(inode)->lli_fid), cancelled);
-
-       if (!cancelled)
-               ldlm_cli_cancel(&och->och_lease_handle, 0);
+       CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
+              PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
 
        if (lease_broken != NULL)
                *lease_broken = cancelled;
 
-       rc = ll_close_inode_openhandle(inode, och, 0, NULL);
+       if (!cancelled && !bias)
+               ldlm_cli_cancel(&och->och_lease_handle, 0);
+
+       if (cancelled) { /* no need to excute intent */
+               bias = 0;
+               data = NULL;
+       }
+
+       rc = ll_close_inode_openhandle(inode, och, bias, data);
        RETURN(rc);
 }
 
+static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
+                         bool *lease_broken)
+{
+       return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
+}
+
+/**
+ * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
+ */
+static int ll_lease_file_resync(struct obd_client_handle *och,
+                               struct inode *inode, unsigned long arg)
+{
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       struct md_op_data *op_data;
+       struct ll_ioc_lease_id ioc;
+       __u64 data_version_unused;
+       int rc;
+       ENTRY;
+
+       op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
+                                    LUSTRE_OPC_ANY, NULL);
+       if (IS_ERR(op_data))
+               RETURN(PTR_ERR(op_data));
+
+       if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
+                          sizeof(ioc)))
+               RETURN(-EFAULT);
+
+       /* before starting file resync, it's necessary to clean up page cache
+        * in client memory, otherwise once the layout version is increased,
+        * writing back cached data will be denied the OSTs. */
+       rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
+       if (rc)
+               GOTO(out, rc);
+
+       op_data->op_lease_handle = och->och_lease_handle;
+       op_data->op_mirror_id = ioc.lil_mirror_id;
+       rc = md_file_resync(sbi->ll_md_exp, op_data);
+       if (rc)
+               GOTO(out, rc);
+
+       EXIT;
+out:
+       ll_finish_md_op_data(op_data);
+       return rc;
+}
+
 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
 {
        struct ll_inode_info *lli = ll_i2info(inode);
@@ -1007,24 +1294,28 @@ int ll_merge_attr(const struct lu_env *env, struct inode *inode)
         * if it's at least 'mdd.*.atime_diff' older.
         * All in all, the atime in Lustre does not strictly comply with
         * POSIX. Solving this problem needs to send an RPC to MDT for each
-        * read, this will hurt performance. */
-       if (LTIME_S(inode->i_atime) < lli->lli_atime || lli->lli_update_atime) {
-               LTIME_S(inode->i_atime) = lli->lli_atime;
-               lli->lli_update_atime = 0;
-       }
-       LTIME_S(inode->i_mtime) = lli->lli_mtime;
-       LTIME_S(inode->i_ctime) = lli->lli_ctime;
+        * read, this will hurt performance.
+        */
+       if (ll_file_test_and_clear_flag(lli, LLIF_UPDATE_ATIME) ||
+           inode->i_atime.tv_sec < lli->lli_atime)
+               inode->i_atime.tv_sec = lli->lli_atime;
+
+       inode->i_mtime.tv_sec = lli->lli_mtime;
+       inode->i_ctime.tv_sec = lli->lli_ctime;
 
-       atime = LTIME_S(inode->i_atime);
-       mtime = LTIME_S(inode->i_mtime);
-       ctime = LTIME_S(inode->i_ctime);
+       mtime = inode->i_mtime.tv_sec;
+       atime = inode->i_atime.tv_sec;
+       ctime = inode->i_ctime.tv_sec;
 
        cl_object_attr_lock(obj);
-       rc = cl_object_attr_get(env, obj, attr);
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
+               rc = -EINVAL;
+       else
+               rc = cl_object_attr_get(env, obj, attr);
        cl_object_attr_unlock(obj);
 
        if (rc != 0)
-               GOTO(out_size_unlock, rc);
+               GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
 
        if (atime < attr->cat_atime)
                atime = attr->cat_atime;
@@ -1041,9 +1332,9 @@ int ll_merge_attr(const struct lu_env *env, struct inode *inode)
        i_size_write(inode, attr->cat_size);
        inode->i_blocks = attr->cat_blocks;
 
-       LTIME_S(inode->i_atime) = atime;
-       LTIME_S(inode->i_mtime) = mtime;
-       LTIME_S(inode->i_ctime) = ctime;
+       inode->i_mtime.tv_sec = mtime;
+       inode->i_atime.tv_sec = atime;
+       inode->i_ctime.tv_sec = ctime;
 
 out_size_unlock:
        ll_inode_size_unlock(inode);
@@ -1051,6 +1342,32 @@ out_size_unlock:
        RETURN(rc);
 }
 
+/**
+ * Set designated mirror for I/O.
+ *
+ * So far only read, write, and truncated can support to issue I/O to
+ * designated mirror.
+ */
+void ll_io_set_mirror(struct cl_io *io, const struct file *file)
+{
+       struct ll_file_data *fd = file->private_data;
+
+       /* clear layout version for generic(non-resync) I/O in case it carries
+        * stale layout version due to I/O restart */
+       io->ci_layout_version = 0;
+
+       /* FLR: disable non-delay for designated mirror I/O because obviously
+        * only one mirror is available */
+       if (fd->fd_designated_mirror > 0) {
+               io->ci_ndelay = 0;
+               io->ci_designated_mirror = fd->fd_designated_mirror;
+               io->ci_layout_version = fd->fd_layout_version;
+       }
+
+       CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
+              file->f_path.dentry->d_name.name, io->ci_designated_mirror);
+}
+
 static bool file_is_noatime(const struct file *file)
 {
        const struct vfsmount *mnt = file->f_path.mnt;
@@ -1072,29 +1389,33 @@ static bool file_is_noatime(const struct file *file)
        if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
                return true;
 
-       if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
+       if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
                return true;
 
        return false;
 }
 
-static int ll_file_io_ptask(struct cfs_ptask *ptask);
-
-static void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot)
+void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
+               struct vvp_io_args *args)
 {
        struct inode *inode = file_inode(file);
+       struct ll_file_data *fd  = file->private_data;
+
+       io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
+       io->ci_lock_no_expand = fd->ll_lock_no_expand;
 
-       memset(&io->u.ci_rw.rw_iter, 0, sizeof(io->u.ci_rw.rw_iter));
-       init_sync_kiocb(&io->u.ci_rw.rw_iocb, file);
-       io->u.ci_rw.rw_file = file;
-       io->u.ci_rw.rw_ptask = ll_file_io_ptask;
-       io->u.ci_rw.rw_nonblock = !!(file->f_flags & O_NONBLOCK);
        if (iot == CIT_WRITE) {
-               io->u.ci_rw.rw_append = !!(file->f_flags & O_APPEND);
-               io->u.ci_rw.rw_sync   = !!(file->f_flags & O_SYNC ||
+               io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
+               io->u.ci_wr.wr_sync   = !!(file->f_flags & O_SYNC ||
                                           file->f_flags & O_DIRECT ||
                                           IS_SYNC(inode));
+#ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
+               io->u.ci_wr.wr_sync  |= !!(args &&
+                                          args->via_io_subtype == IO_NORMAL &&
+                                          args->u.normal.via_iocb->ki_flags & IOCB_DSYNC);
+#endif
        }
+
        io->ci_obj = ll_i2info(inode)->lli_clob;
        io->ci_lockreq = CILR_MAYBE;
        if (ll_file_nolock(file)) {
@@ -1104,94 +1425,44 @@ static void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot)
                io->ci_lockreq = CILR_MANDATORY;
        }
        io->ci_noatime = file_is_noatime(file);
-       if (ll_i2sbi(inode)->ll_flags & LL_SBI_PIO)
-               io->ci_pio = !io->u.ci_rw.rw_append;
-       else
-               io->ci_pio = 0;
-}
-
-static int ll_file_io_ptask(struct cfs_ptask *ptask)
-{
-       struct cl_io_pt *pt = ptask->pt_cbdata;
-       struct file *file = pt->cip_file;
-       struct lu_env *env;
-       struct cl_io *io;
-       loff_t pos = pt->cip_pos;
-       int rc;
-       __u16 refcheck;
-       ENTRY;
+       io->ci_async_readahead = false;
 
-       env = cl_env_get(&refcheck);
-       if (IS_ERR(env))
-               RETURN(PTR_ERR(env));
-
-       CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
-               file_dentry(file)->d_name.name,
-               pt->cip_iot == CIT_READ ? "read" : "write",
-               pos, pos + pt->cip_count);
+       /* FLR: only use non-delay I/O for read as there is only one
+        * avaliable mirror for write. */
+       io->ci_ndelay = !(iot == CIT_WRITE);
 
-restart:
-       io = vvp_env_thread_io(env);
-       ll_io_init(io, file, pt->cip_iot);
-       io->u.ci_rw.rw_iter = pt->cip_iter;
-       io->u.ci_rw.rw_iocb = pt->cip_iocb;
-       io->ci_pio = 0; /* It's already in parallel task */
+       ll_io_set_mirror(io, file);
+}
 
-       rc = cl_io_rw_init(env, io, pt->cip_iot, pos,
-                          pt->cip_count - pt->cip_result);
-       if (!rc) {
-               struct vvp_io *vio = vvp_env_io(env);
+static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
+                       __u64 count)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       enum obd_heat_type sample_type;
+       enum obd_heat_type iobyte_type;
+       __u64 now = ktime_get_real_seconds();
 
-               vio->vui_io_subtype = IO_NORMAL;
-               vio->vui_fd = LUSTRE_FPRIVATE(file);
+       if (!ll_sbi_has_file_heat(sbi) ||
+           lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
+               return;
 
-               ll_cl_add(file, env, io, LCC_RW);
-               rc = cl_io_loop(env, io);
-               ll_cl_remove(file, env);
+       if (iot == CIT_READ) {
+               sample_type = OBD_HEAT_READSAMPLE;
+               iobyte_type = OBD_HEAT_READBYTE;
+       } else if (iot == CIT_WRITE) {
+               sample_type = OBD_HEAT_WRITESAMPLE;
+               iobyte_type = OBD_HEAT_WRITEBYTE;
        } else {
-               /* cl_io_rw_init() handled IO */
-               rc = io->ci_result;
-       }
-
-       if (OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_PTASK_IO_FAIL, 0)) {
-               if (io->ci_nob > 0)
-                       io->ci_nob /= 2;
-               rc = -EIO;
-       }
-
-       if (io->ci_nob > 0) {
-               pt->cip_result += io->ci_nob;
-               iov_iter_advance(&pt->cip_iter, io->ci_nob);
-               pos += io->ci_nob;
-               pt->cip_iocb.ki_pos = pos;
-#ifdef HAVE_KIOCB_KI_LEFT
-               pt->cip_iocb.ki_left = pt->cip_count - pt->cip_result;
-#elif defined(HAVE_KI_NBYTES)
-               pt->cip_iocb.ki_nbytes = pt->cip_count - pt->cip_result;
-#endif
-       }
-
-       cl_io_fini(env, io);
-
-       if ((rc == 0 || rc == -ENODATA) &&
-           pt->cip_result < pt->cip_count &&
-           io->ci_need_restart) {
-               CDEBUG(D_VFSTRACE,
-                       "%s: restart %s range: [%llu, %llu) ret: %zd, rc: %d\n",
-                       file_dentry(file)->d_name.name,
-                       pt->cip_iot == CIT_READ ? "read" : "write",
-                       pos, pos + pt->cip_count - pt->cip_result,
-                       pt->cip_result, rc);
-               goto restart;
+               return;
        }
 
-       CDEBUG(D_VFSTRACE, "%s: %s ret: %zd, rc: %d\n",
-               file_dentry(file)->d_name.name,
-               pt->cip_iot == CIT_READ ? "read" : "write",
-               pt->cip_result, rc);
-
-       cl_env_put(env, &refcheck);
-       RETURN(pt->cip_result > 0 ? 0 : rc);
+       spin_lock(&lli->lli_heat_lock);
+       obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
+                    sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
+       obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
+                    sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
+       spin_unlock(&lli->lli_heat_lock);
 }
 
 static ssize_t
@@ -1199,45 +1470,44 @@ ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
                   struct file *file, enum cl_io_type iot,
                   loff_t *ppos, size_t count)
 {
-       struct range_lock       range;
        struct vvp_io           *vio = vvp_env_io(env);
        struct inode            *inode = file_inode(file);
        struct ll_inode_info    *lli = ll_i2info(inode);
-       struct ll_file_data     *fd  = LUSTRE_FPRIVATE(file);
+       struct ll_file_data     *fd  = file->private_data;
+       struct range_lock       range;
        struct cl_io            *io;
-       loff_t                  pos = *ppos;
        ssize_t                 result = 0;
        int                     rc = 0;
+       unsigned                retried = 0;
+       unsigned                ignore_lockless = 0;
 
        ENTRY;
 
-       CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
+       CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
                file_dentry(file)->d_name.name,
-               iot == CIT_READ ? "read" : "write", pos, pos + count);
+               iot == CIT_READ ? "read" : "write", *ppos, count);
 
 restart:
        io = vvp_env_thread_io(env);
-       ll_io_init(io, file, iot);
-       if (args->via_io_subtype == IO_NORMAL) {
-               io->u.ci_rw.rw_iter = *args->u.normal.via_iter;
-               io->u.ci_rw.rw_iocb = *args->u.normal.via_iocb;
-       } else {
-               io->ci_pio = 0;
-       }
+       ll_io_init(io, file, iot, args);
+       io->ci_ignore_lockless = ignore_lockless;
+       io->ci_ndelay_tried = retried;
 
-       if (cl_io_rw_init(env, io, iot, pos, count) == 0) {
+       if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
                bool range_locked = false;
 
                if (file->f_flags & O_APPEND)
                        range_lock_init(&range, 0, LUSTRE_EOF);
                else
-                       range_lock_init(&range, pos, pos + count - 1);
+                       range_lock_init(&range, *ppos, *ppos + count - 1);
 
-               vio->vui_fd  = LUSTRE_FPRIVATE(file);
+               vio->vui_fd  = file->private_data;
                vio->vui_io_subtype = args->via_io_subtype;
 
                switch (vio->vui_io_subtype) {
                case IO_NORMAL:
+                       vio->vui_iter = args->u.normal.via_iter;
+                       vio->vui_iocb = args->u.normal.via_iocb;
                        /* Direct IO reads must also take range lock,
                         * or multiple reads will try to work on the same pages
                         * See LU-6227 for details. */
@@ -1263,16 +1533,7 @@ restart:
                }
 
                ll_cl_add(file, env, io, LCC_RW);
-               if (io->ci_pio && iot == CIT_WRITE && !IS_NOSEC(inode) &&
-                   !lli->lli_inode_locked) {
-                       inode_lock(inode);
-                       lli->lli_inode_locked = 1;
-               }
                rc = cl_io_loop(env, io);
-               if (lli->lli_inode_locked) {
-                       lli->lli_inode_locked = 0;
-                       inode_unlock(inode);
-               }
                ll_cl_remove(file, env);
 
                if (range_locked) {
@@ -1288,30 +1549,30 @@ restart:
        if (io->ci_nob > 0) {
                result += io->ci_nob;
                count  -= io->ci_nob;
+               *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
 
-               if (args->via_io_subtype == IO_NORMAL) {
-                       iov_iter_advance(args->u.normal.via_iter, io->ci_nob);
-                       pos += io->ci_nob;
-                       args->u.normal.via_iocb->ki_pos = pos;
-#ifdef HAVE_KIOCB_KI_LEFT
-                       args->u.normal.via_iocb->ki_left = count;
-#elif defined(HAVE_KI_NBYTES)
-                       args->u.normal.via_iocb->ki_nbytes = count;
-#endif
-               } else {
-                       /* for splice */
-                       pos = io->u.ci_rw.rw_range.cir_pos;
-               }
+               /* prepare IO restart */
+               if (count > 0 && args->via_io_subtype == IO_NORMAL)
+                       args->u.normal.via_iter = vio->vui_iter;
        }
 out:
        cl_io_fini(env, io);
 
-       if ((rc == 0 || rc == -ENODATA) && count > 0 && io->ci_need_restart) {
+       CDEBUG(D_VFSTRACE,
+              "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
+              file->f_path.dentry->d_name.name,
+              iot, rc, result, io->ci_need_restart);
+
+       if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
+           count > 0 && io->ci_need_restart) {
                CDEBUG(D_VFSTRACE,
-                       "%s: restart %s range: [%llu, %llu) ret: %zd, rc: %d\n",
-                       file_dentry(file)->d_name.name,
-                       iot == CIT_READ ? "read" : "write",
-                       pos, pos + count, result, rc);
+                      "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
+                      file_dentry(file)->d_name.name,
+                      iot == CIT_READ ? "read" : "write",
+                      *ppos, count, result, rc);
+               /* preserve the tried count for FLR */
+               retried = io->ci_ndelay_tried;
+               ignore_lockless = io->ci_ignore_lockless;
                goto restart;
        }
 
@@ -1335,11 +1596,9 @@ out:
                }
        }
 
-       CDEBUG(D_VFSTRACE, "%s: %s *ppos: %llu, pos: %llu, ret: %zd, rc: %d\n",
-               file_dentry(file)->d_name.name,
-               iot == CIT_READ ? "read" : "write", *ppos, pos, result, rc);
-
-       *ppos = pos;
+       CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
+       if (result > 0)
+               ll_heat_add(inode, iot, result);
 
        RETURN(result > 0 ? result : rc);
 }
@@ -1400,9 +1659,11 @@ ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
        if (result == -ENODATA)
                result = 0;
 
-       if (result > 0)
+       if (result > 0) {
+               ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
                ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
-                               LPROC_LL_READ_BYTES, result);
+                                  LPROC_LL_READ_BYTES, result);
+       }
 
        return result;
 }
@@ -1414,9 +1675,32 @@ static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
        struct lu_env *env;
        struct vvp_io_args *args;
+       struct file *file = iocb->ki_filp;
        ssize_t result;
        ssize_t rc2;
        __u16 refcheck;
+       ktime_t kstart = ktime_get();
+       bool cached;
+
+       if (!iov_iter_count(to))
+               return 0;
+
+       /**
+        * Currently when PCC read failed, we do not fall back to the
+        * normal read path, just return the error.
+        * The resaon is that: for RW-PCC, the file data may be modified
+        * in the PCC and inconsistent with the data on OSTs (or file
+        * data has been removed from the Lustre file system), at this
+        * time, fallback to the normal read path may read the wrong
+        * data.
+        * TODO: for RO-PCC (readonly PCC), fall back to normal read
+        * path: read data from data copy on OSTs.
+        */
+       result = pcc_file_read_iter(iocb, to, &cached);
+       if (cached)
+               GOTO(out, result);
+
+       ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
 
        result = ll_do_fast_read(iocb, to);
        if (result < 0 || iov_iter_count(to) == 0)
@@ -1430,7 +1714,7 @@ static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
        args->u.normal.via_iter = to;
        args->u.normal.via_iocb = iocb;
 
-       rc2 = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
+       rc2 = ll_file_io_generic(env, args, file, CIT_READ,
                                 &iocb->ki_pos, iov_iter_count(to));
        if (rc2 > 0)
                result += rc2;
@@ -1439,18 +1723,123 @@ static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 
        cl_env_put(env, &refcheck);
 out:
+       if (result > 0) {
+               ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
+                                 file->private_data, iocb->ki_pos, result,
+                                 READ);
+               ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
+                                  ktime_us_delta(ktime_get(), kstart));
+       }
+
        return result;
 }
 
-/*
- * Write to a file (through the page cache).
+/**
+ * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
+ * If a page is already in the page cache and dirty (and some other things -
+ * See ll_tiny_write_begin for the instantiation of these rules), then we can
+ * write to it without doing a full I/O, because Lustre already knows about it
+ * and will write it out.  This saves a lot of processing time.
+ *
+ * All writes here are within one page, so exclusion is handled by the page
+ * lock on the vm page.  We do not do tiny writes for writes which touch
+ * multiple pages because it's very unlikely multiple sequential pages are
+ * are already dirty.
+ *
+ * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
+ * and are unlikely to be to already dirty pages.
+ *
+ * Attribute updates are important here, we do them in ll_tiny_write_end.
+ */
+static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
+{
+       ssize_t count = iov_iter_count(iter);
+       struct  file *file = iocb->ki_filp;
+       struct  inode *inode = file_inode(file);
+       bool    lock_inode = !IS_NOSEC(inode);
+       ssize_t result = 0;
+
+       ENTRY;
+
+       /* Restrict writes to single page and < PAGE_SIZE.  See comment at top
+        * of function for why.
+        */
+       if (count >= PAGE_SIZE ||
+           (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
+               RETURN(0);
+
+       if (unlikely(lock_inode))
+               inode_lock(inode);
+       result = __generic_file_write_iter(iocb, iter);
+
+       if (unlikely(lock_inode))
+               inode_unlock(inode);
+
+       /* If the page is not already dirty, ll_tiny_write_begin returns
+        * -ENODATA.  We continue on to normal write.
+        */
+       if (result == -ENODATA)
+               result = 0;
+
+       if (result > 0) {
+               ll_heat_add(inode, CIT_WRITE, result);
+               ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
+                                  result);
+               ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
+       }
+
+       CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
+
+       RETURN(result);
+}
+
+/*
+ * Write to a file (through the page cache).
  */
 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 {
        struct vvp_io_args *args;
        struct lu_env *env;
-       ssize_t result;
+       ssize_t rc_tiny = 0, rc_normal;
+       struct file *file = iocb->ki_filp;
        __u16 refcheck;
+       bool cached;
+       ktime_t kstart = ktime_get();
+       int result;
+
+       ENTRY;
+
+       if (!iov_iter_count(from))
+               GOTO(out, rc_normal = 0);
+
+       /**
+        * When PCC write failed, we usually do not fall back to the normal
+        * write path, just return the error. But there is a special case when
+        * returned error code is -ENOSPC due to running out of space on PCC HSM
+        * bakcend. At this time, it will fall back to normal I/O path and
+        * retry the I/O. As the file is in HSM released state, it will restore
+        * the file data to OSTs first and redo the write again. And the
+        * restore process will revoke the layout lock and detach the file
+        * from PCC cache automatically.
+        */
+       result = pcc_file_write_iter(iocb, from, &cached);
+       if (cached && result != -ENOSPC && result != -EDQUOT)
+               GOTO(out, rc_normal = result);
+
+       /* NB: we can't do direct IO for tiny writes because they use the page
+        * cache, we can't do sync writes because tiny writes can't flush
+        * pages, and we can't do append writes because we can't guarantee the
+        * required DLM locks are held to protect file size.
+        */
+       if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
+           !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
+               rc_tiny = ll_do_tiny_write(iocb, from);
+
+       /* In case of error, go on and try normal write - Only stop if tiny
+        * write completed I/O.
+        */
+       if (iov_iter_count(from) == 0)
+               GOTO(out, rc_normal = rc_tiny);
 
        env = cl_env_get(&refcheck);
        if (IS_ERR(env))
@@ -1460,10 +1849,29 @@ static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
        args->u.normal.via_iter = from;
        args->u.normal.via_iocb = iocb;
 
-       result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
-                                   &iocb->ki_pos, iov_iter_count(from));
+       rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
+                                      &iocb->ki_pos, iov_iter_count(from));
+
+       /* On success, combine bytes written. */
+       if (rc_tiny >= 0 && rc_normal > 0)
+               rc_normal += rc_tiny;
+       /* On error, only return error from normal write if tiny write did not
+        * write any bytes.  Otherwise return bytes written by tiny write.
+        */
+       else if (rc_tiny > 0)
+               rc_normal = rc_tiny;
+
        cl_env_put(env, &refcheck);
-       return result;
+out:
+       if (rc_normal > 0) {
+               ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
+                                 file->private_data, iocb->ki_pos,
+                                 rc_normal, WRITE);
+               ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
+                                  ktime_us_delta(ktime_get(), kstart));
+       }
+
+       RETURN(rc_normal);
 }
 
 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
@@ -1510,6 +1918,9 @@ static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
        if (result)
                RETURN(result);
 
+       if (!iov_count)
+               RETURN(0);
+
 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
        iov_iter_init(&to, READ, iov, nr_segs, iov_count);
 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
@@ -1527,8 +1938,12 @@ static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
        struct iovec   iov = { .iov_base = buf, .iov_len = count };
        struct kiocb   kiocb;
        ssize_t        result;
+
        ENTRY;
 
+       if (!count)
+               RETURN(0);
+
        init_sync_kiocb(&kiocb, file);
        kiocb.ki_pos = *ppos;
 #ifdef HAVE_KIOCB_KI_LEFT
@@ -1559,6 +1974,9 @@ static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
        if (result)
                RETURN(result);
 
+       if (!iov_count)
+               RETURN(0);
+
 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
        iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
@@ -1573,31 +1991,27 @@ static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 static ssize_t ll_file_write(struct file *file, const char __user *buf,
                             size_t count, loff_t *ppos)
 {
-       struct lu_env *env;
        struct iovec   iov = { .iov_base = (void __user *)buf,
                               .iov_len = count };
-        struct kiocb  *kiocb;
-        ssize_t        result;
-       __u16          refcheck;
-        ENTRY;
+       struct kiocb   kiocb;
+       ssize_t        result;
 
-        env = cl_env_get(&refcheck);
-        if (IS_ERR(env))
-                RETURN(PTR_ERR(env));
+       ENTRY;
 
-       kiocb = &ll_env_info(env)->lti_kiocb;
-        init_sync_kiocb(kiocb, file);
-        kiocb->ki_pos = *ppos;
+       if (!count)
+               RETURN(0);
+
+       init_sync_kiocb(&kiocb, file);
+       kiocb.ki_pos = *ppos;
 #ifdef HAVE_KIOCB_KI_LEFT
-       kiocb->ki_left = count;
+       kiocb.ki_left = count;
 #elif defined(HAVE_KI_NBYTES)
-       kiocb->ki_nbytes = count;
+       kiocb.ki_nbytes = count;
 #endif
 
-       result = ll_file_aio_write(kiocb, &iov, 1, kiocb->ki_pos);
-       *ppos = kiocb->ki_pos;
+       result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
+       *ppos = kiocb.ki_pos;
 
-       cl_env_put(env, &refcheck);
        RETURN(result);
 }
 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
@@ -1609,23 +2023,37 @@ static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
                                    struct pipe_inode_info *pipe, size_t count,
                                    unsigned int flags)
 {
-        struct lu_env      *env;
-        struct vvp_io_args *args;
-        ssize_t             result;
-       __u16               refcheck;
-        ENTRY;
+       struct lu_env *env;
+       struct vvp_io_args *args;
+       ssize_t result;
+       __u16 refcheck;
+       bool cached;
 
-        env = cl_env_get(&refcheck);
-        if (IS_ERR(env))
-                RETURN(PTR_ERR(env));
+       ENTRY;
+
+       result = pcc_file_splice_read(in_file, ppos, pipe,
+                                     count, flags, &cached);
+       if (cached)
+               RETURN(result);
+
+       ll_ras_enter(in_file, *ppos, count);
+
+       env = cl_env_get(&refcheck);
+       if (IS_ERR(env))
+               RETURN(PTR_ERR(env));
 
        args = ll_env_args(env, IO_SPLICE);
-        args->u.splice.via_pipe = pipe;
-        args->u.splice.via_flags = flags;
+       args->u.splice.via_pipe = pipe;
+       args->u.splice.via_flags = flags;
+
+       result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
+       cl_env_put(env, &refcheck);
 
-        result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
-        cl_env_put(env, &refcheck);
-        RETURN(result);
+       if (result > 0)
+               ll_rw_stats_tally(ll_i2sbi(file_inode(in_file)), current->pid,
+                                 in_file->private_data, *ppos, result,
+                                 READ);
+       RETURN(result);
 }
 
 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
@@ -1638,6 +2066,12 @@ int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
        int rc;
        ENTRY;
 
+       if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
+           le32_to_cpu(LOV_MAGIC_MAGIC)) {
+               /* this code will only exist for big-endian systems */
+               lustre_swab_lov_user_md(lum, 0);
+       }
+
        ll_inode_size_lock(inode);
        rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
        if (rc < 0)
@@ -1697,16 +2131,18 @@ int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
 
        if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
            lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
-           lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1))
+           lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
+           lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
                GOTO(out, rc = -EPROTO);
 
-        /*
-         * This is coming from the MDS, so is probably in
-         * little endian.  We convert it to host endian before
-         * passing it to userspace.
-         */
-        if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
-               int stripe_count;
+       /*
+        * This is coming from the MDS, so is probably in
+        * little endian.  We convert it to host endian before
+        * passing it to userspace.
+        */
+       if ((lmm->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
+           __swab32(LOV_MAGIC_MAGIC)) {
+               int stripe_count = 0;
 
                if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
                    lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
@@ -1716,34 +2152,26 @@ int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
                                stripe_count = 0;
                }
 
-                /* if function called for directory - we should
-                 * avoid swab not existent lsm objects */
-                if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
-                       lustre_swab_lov_user_md_v1(
-                                       (struct lov_user_md_v1 *)lmm);
-                       if (S_ISREG(body->mbo_mode))
-                               lustre_swab_lov_user_md_objects(
-                                   ((struct lov_user_md_v1 *)lmm)->lmm_objects,
-                                   stripe_count);
-               } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
-                       lustre_swab_lov_user_md_v3(
-                                       (struct lov_user_md_v3 *)lmm);
-                       if (S_ISREG(body->mbo_mode))
-                               lustre_swab_lov_user_md_objects(
-                                ((struct lov_user_md_v3 *)lmm)->lmm_objects,
-                                stripe_count);
-               } else if (lmm->lmm_magic ==
-                          cpu_to_le32(LOV_MAGIC_COMP_V1)) {
-                       lustre_swab_lov_comp_md_v1(
-                                       (struct lov_comp_md_v1 *)lmm);
-               }
-        }
+               lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
+
+               /* if function called for directory - we should
+                * avoid swab not existent lsm objects */
+               if (lmm->lmm_magic == LOV_MAGIC_V1 && S_ISREG(body->mbo_mode))
+                       lustre_swab_lov_user_md_objects(
+                               ((struct lov_user_md_v1 *)lmm)->lmm_objects,
+                               stripe_count);
+               else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
+                        S_ISREG(body->mbo_mode))
+                       lustre_swab_lov_user_md_objects(
+                               ((struct lov_user_md_v3 *)lmm)->lmm_objects,
+                               stripe_count);
+       }
 
 out:
-        *lmmp = lmm;
-        *lmm_size = lmmsize;
-        *request = req;
-        return rc;
+       *lmmp = lmm;
+       *lmm_size = lmmsize;
+       *request = req;
+       return rc;
 }
 
 static int ll_lov_setea(struct inode *inode, struct file *file,
@@ -1823,16 +2251,17 @@ static int ll_lov_setstripe(struct inode *inode, struct file *file,
        cl_lov_delay_create_clear(&file->f_flags);
 
 out:
-       OBD_FREE(klum, lum_size);
+       OBD_FREE_LARGE(klum, lum_size);
        RETURN(rc);
 }
 
+
 static int
 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
 {
        struct ll_inode_info *lli = ll_i2info(inode);
        struct cl_object *obj = lli->lli_clob;
-       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data *fd = file->private_data;
        struct ll_grouplock grouplock;
        int rc;
        ENTRY;
@@ -1842,18 +2271,28 @@ ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
                RETURN(-EINVAL);
        }
 
-        if (ll_file_nolock(file))
-                RETURN(-EOPNOTSUPP);
+       if (ll_file_nolock(file))
+               RETURN(-EOPNOTSUPP);
+retry:
+       if (file->f_flags & O_NONBLOCK) {
+               if (!mutex_trylock(&lli->lli_group_mutex))
+                       RETURN(-EAGAIN);
+       } else
+               mutex_lock(&lli->lli_group_mutex);
 
-       spin_lock(&lli->lli_lock);
        if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
                CWARN("group lock already existed with gid %lu\n",
                      fd->fd_grouplock.lg_gid);
-               spin_unlock(&lli->lli_lock);
-               RETURN(-EINVAL);
+               GOTO(out, rc = -EINVAL);
+       }
+       if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
+               if (file->f_flags & O_NONBLOCK)
+                       GOTO(out, rc = -EAGAIN);
+               mutex_unlock(&lli->lli_group_mutex);
+               wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
+               GOTO(retry, rc = 0);
        }
        LASSERT(fd->fd_grouplock.lg_lock == NULL);
-       spin_unlock(&lli->lli_lock);
 
        /**
         * XXX: group lock needs to protect all OST objects while PFL
@@ -1866,73 +2305,84 @@ ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
                struct cl_layout cl = {
                        .cl_is_composite = false,
                };
+               struct lu_extent ext = {
+                       .e_start = 0,
+                       .e_end = OBD_OBJECT_EOF,
+               };
 
                env = cl_env_get(&refcheck);
                if (IS_ERR(env))
-                       RETURN(PTR_ERR(env));
+                       GOTO(out, rc = PTR_ERR(env));
 
                rc = cl_object_layout_get(env, obj, &cl);
                if (!rc && cl.cl_is_composite)
-                       rc = ll_layout_write_intent(inode, 0, OBD_OBJECT_EOF);
+                       rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
+                                                   &ext);
 
                cl_env_put(env, &refcheck);
                if (rc)
-                       RETURN(rc);
+                       GOTO(out, rc);
        }
 
        rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
                              arg, (file->f_flags & O_NONBLOCK), &grouplock);
-       if (rc)
-               RETURN(rc);
 
-       spin_lock(&lli->lli_lock);
-       if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
-               spin_unlock(&lli->lli_lock);
-               CERROR("another thread just won the race\n");
-               cl_put_grouplock(&grouplock);
-               RETURN(-EINVAL);
-       }
+       if (rc)
+               GOTO(out, rc);
 
        fd->fd_flags |= LL_FILE_GROUP_LOCKED;
        fd->fd_grouplock = grouplock;
-       spin_unlock(&lli->lli_lock);
+       if (lli->lli_group_users == 0)
+               lli->lli_group_gid = grouplock.lg_gid;
+       lli->lli_group_users++;
 
        CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
-       RETURN(0);
+out:
+       mutex_unlock(&lli->lli_group_mutex);
+
+       RETURN(rc);
 }
 
 static int ll_put_grouplock(struct inode *inode, struct file *file,
                            unsigned long arg)
 {
        struct ll_inode_info   *lli = ll_i2info(inode);
-       struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data    *fd = file->private_data;
        struct ll_grouplock     grouplock;
+       int                     rc;
        ENTRY;
 
-       spin_lock(&lli->lli_lock);
+       mutex_lock(&lli->lli_group_mutex);
        if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
-               spin_unlock(&lli->lli_lock);
-                CWARN("no group lock held\n");
-                RETURN(-EINVAL);
-        }
+               CWARN("no group lock held\n");
+               GOTO(out, rc = -EINVAL);
+       }
 
        LASSERT(fd->fd_grouplock.lg_lock != NULL);
 
        if (fd->fd_grouplock.lg_gid != arg) {
                CWARN("group lock %lu doesn't match current id %lu\n",
                      arg, fd->fd_grouplock.lg_gid);
-               spin_unlock(&lli->lli_lock);
-               RETURN(-EINVAL);
+               GOTO(out, rc = -EINVAL);
        }
 
        grouplock = fd->fd_grouplock;
        memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
        fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
-       spin_unlock(&lli->lli_lock);
 
        cl_put_grouplock(&grouplock);
+
+       lli->lli_group_users--;
+       if (lli->lli_group_users == 0) {
+               lli->lli_group_gid = 0;
+               wake_up_var(&lli->lli_group_users);
+       }
        CDEBUG(D_INFO, "group lock %lu released\n", arg);
-       RETURN(0);
+       GOTO(out, rc = 0);
+out:
+       mutex_unlock(&lli->lli_group_mutex);
+
+       RETURN(rc);
 }
 
 /**
@@ -1967,7 +2417,9 @@ int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
         if (!och)
                 GOTO(out, rc = -ENOMEM);
 
-       ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
+       rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
+       if (rc)
+               GOTO(out, rc);
 
        rc = ll_close_inode_openhandle(inode, och, 0, NULL);
 out:
@@ -2083,18 +2535,8 @@ gf_free:
        RETURN(rc);
 }
 
-/*
- * Read the data_version for inode.
- *
- * This value is computed using stripe object version on OST.
- * Version is computed using server side locking.
- *
- * @param flags if do sync on the OST side;
- *             0: no sync
- *             LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
- *             LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
- */
-int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
+static int
+ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
 {
        struct cl_object *obj = ll_i2info(inode)->lli_clob;
        struct lu_env *env;
@@ -2104,11 +2546,12 @@ int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
 
        ENTRY;
 
+       ioc->idv_version = 0;
+       ioc->idv_layout_version = UINT_MAX;
+
        /* If no file object initialized, we consider its version is 0. */
-       if (obj == NULL) {
-               *data_version = 0;
+       if (obj == NULL)
                RETURN(0);
-       }
 
        env = cl_env_get(&refcheck);
        if (IS_ERR(env))
@@ -2117,7 +2560,8 @@ int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
        io = vvp_env_thread_io(env);
        io->ci_obj = obj;
        io->u.ci_data_version.dv_data_version = 0;
-       io->u.ci_data_version.dv_flags = flags;
+       io->u.ci_data_version.dv_layout_version = UINT_MAX;
+       io->u.ci_data_version.dv_flags = ioc->idv_flags;
 
 restart:
        if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
@@ -2125,7 +2569,8 @@ restart:
        else
                result = io->ci_result;
 
-       *data_version = io->u.ci_data_version.dv_data_version;
+       ioc->idv_version = io->u.ci_data_version.dv_data_version;
+       ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
 
        cl_io_fini(env, io);
 
@@ -2138,6 +2583,29 @@ restart:
 }
 
 /*
+ * Read the data_version for inode.
+ *
+ * This value is computed using stripe object version on OST.
+ * Version is computed using server side locking.
+ *
+ * @param flags if do sync on the OST side;
+ *             0: no sync
+ *             LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
+ *             LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
+ */
+int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
+{
+       struct ioc_data_version ioc = { .idv_flags = flags };
+       int rc;
+
+       rc = ll_ioc_data_version(inode, &ioc);
+       if (!rc)
+               *data_version = ioc.idv_version;
+
+       return rc;
+}
+
+/*
  * Trigger a HSM release request for the provided inode.
  */
 int ll_hsm_release(struct inode *inode)
@@ -2150,7 +2618,7 @@ int ll_hsm_release(struct inode *inode)
        ENTRY;
 
        CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
-              ll_get_fsname(inode->i_sb, NULL, 0),
+              ll_i2sbi(inode)->ll_fsname,
               PFID(&ll_i2info(inode)->lli_fid));
 
        och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
@@ -2166,9 +2634,15 @@ int ll_hsm_release(struct inode *inode)
        if (IS_ERR(env))
                GOTO(out, rc = PTR_ERR(env));
 
-       ll_merge_attr(env, inode);
+       rc = ll_merge_attr(env, inode);
        cl_env_put(env, &refcheck);
 
+       /* If error happen, we have the wrong size for a file.
+        * Don't release it.
+        */
+       if (rc != 0)
+               GOTO(out, rc);
+
        /* Release the file.
         * NB: lease lock handle is released in mdc_hsm_release_pack() because
         * we still need it to pack l_remote_handle to MDT. */
@@ -2301,8 +2775,9 @@ free:
 
 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
 {
-       struct md_op_data       *op_data;
-       int                      rc;
+       struct obd_export *exp = ll_i2mdexp(inode);
+       struct md_op_data *op_data;
+       int rc;
        ENTRY;
 
        /* Detect out-of range masks */
@@ -2315,18 +2790,20 @@ int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
            !cfs_capable(CFS_CAP_SYS_ADMIN))
                RETURN(-EPERM);
 
-       /* Detect out-of range archive id */
-       if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
-           (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
-               RETURN(-EINVAL);
+       if (!exp_connect_archive_id_array(exp)) {
+               /* Detect out-of range archive id */
+               if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
+                   (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
+                       RETURN(-EINVAL);
+       }
 
        op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
                                     LUSTRE_OPC_ANY, hss);
        if (IS_ERR(op_data))
                RETURN(PTR_ERR(op_data));
 
-       rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
-                          sizeof(*op_data), op_data, NULL);
+       rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
+                          op_data, NULL);
 
        ll_finish_md_op_data(op_data);
 
@@ -2377,7 +2854,7 @@ static int ll_hsm_import(struct inode *inode, struct file *file,
 
        inode_lock(inode);
 
-       rc = ll_setattr_raw(file_dentry(file), attr, true);
+       rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
        if (rc == -ENODATA)
                rc = 0;
 
@@ -2405,7 +2882,7 @@ static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
        struct iattr ia = {
                .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
                            ATTR_MTIME | ATTR_MTIME_SET |
-                           ATTR_CTIME | ATTR_CTIME_SET,
+                           ATTR_CTIME,
                .ia_atime = {
                        .tv_sec = lfu->lfu_atime_sec,
                        .tv_nsec = lfu->lfu_atime_nsec,
@@ -2429,12 +2906,194 @@ static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
                RETURN(-EINVAL);
 
        inode_lock(inode);
-       rc = ll_setattr_raw(file_dentry(file), &ia, false);
+       rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
+                           false);
        inode_unlock(inode);
 
        RETURN(rc);
 }
 
+static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
+{
+       switch (mode) {
+       case MODE_READ_USER:
+               return CLM_READ;
+       case MODE_WRITE_USER:
+               return CLM_WRITE;
+       default:
+               return -EINVAL;
+       }
+}
+
+static const char *const user_lockname[] = LOCK_MODE_NAMES;
+
+/* Used to allow the upper layers of the client to request an LDLM lock
+ * without doing an actual read or write.
+ *
+ * Used for ladvise lockahead to manually request specific locks.
+ *
+ * \param[in] file     file this ladvise lock request is on
+ * \param[in] ladvise  ladvise struct describing this lock request
+ *
+ * \retval 0           success, no detailed result available (sync requests
+ *                     and requests sent to the server [not handled locally]
+ *                     cannot return detailed results)
+ * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
+ *                                      see definitions for details.
+ * \retval negative    negative errno on error
+ */
+int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
+{
+       struct lu_env *env = NULL;
+       struct cl_io *io  = NULL;
+       struct cl_lock *lock = NULL;
+       struct cl_lock_descr *descr = NULL;
+       struct dentry *dentry = file->f_path.dentry;
+       struct inode *inode = dentry->d_inode;
+       enum cl_lock_mode cl_mode;
+       off_t start = ladvise->lla_start;
+       off_t end = ladvise->lla_end;
+       int result;
+       __u16 refcheck;
+
+       ENTRY;
+
+       CDEBUG(D_VFSTRACE, "Lock request: file=%.*s, inode=%p, mode=%s "
+              "start=%llu, end=%llu\n", dentry->d_name.len,
+              dentry->d_name.name, dentry->d_inode,
+              user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
+              (__u64) end);
+
+       cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
+       if (cl_mode < 0)
+               GOTO(out, result = cl_mode);
+
+       /* Get IO environment */
+       result = cl_io_get(inode, &env, &io, &refcheck);
+       if (result <= 0)
+               GOTO(out, result);
+
+       result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
+       if (result > 0) {
+               /*
+                * nothing to do for this io. This currently happens when
+                * stripe sub-object's are not yet created.
+                */
+               result = io->ci_result;
+       } else if (result == 0) {
+               lock = vvp_env_lock(env);
+               descr = &lock->cll_descr;
+
+               descr->cld_obj   = io->ci_obj;
+               /* Convert byte offsets to pages */
+               descr->cld_start = cl_index(io->ci_obj, start);
+               descr->cld_end   = cl_index(io->ci_obj, end);
+               descr->cld_mode  = cl_mode;
+               /* CEF_MUST is used because we do not want to convert a
+                * lockahead request to a lockless lock */
+               descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
+                                      CEF_NONBLOCK;
+
+               if (ladvise->lla_peradvice_flags & LF_ASYNC)
+                       descr->cld_enq_flags |= CEF_SPECULATIVE;
+
+               result = cl_lock_request(env, io, lock);
+
+               /* On success, we need to release the lock */
+               if (result >= 0)
+                       cl_lock_release(env, lock);
+       }
+       cl_io_fini(env, io);
+       cl_env_put(env, &refcheck);
+
+       /* -ECANCELED indicates a matching lock with a different extent
+        * was already present, and -EEXIST indicates a matching lock
+        * on exactly the same extent was already present.
+        * We convert them to positive values for userspace to make
+        * recognizing true errors easier.
+        * Note we can only return these detailed results on async requests,
+        * as sync requests look the same as i/o requests for locking. */
+       if (result == -ECANCELED)
+               result = LLA_RESULT_DIFFERENT;
+       else if (result == -EEXIST)
+               result = LLA_RESULT_SAME;
+
+out:
+       RETURN(result);
+}
+static const char *const ladvise_names[] = LU_LADVISE_NAMES;
+
+static int ll_ladvise_sanity(struct inode *inode,
+                            struct llapi_lu_ladvise *ladvise)
+{
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       enum lu_ladvise_type advice = ladvise->lla_advice;
+       /* Note the peradvice flags is a 32 bit field, so per advice flags must
+        * be in the first 32 bits of enum ladvise_flags */
+       __u32 flags = ladvise->lla_peradvice_flags;
+       /* 3 lines at 80 characters per line, should be plenty */
+       int rc = 0;
+
+       if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
+               rc = -EINVAL;
+               CDEBUG(D_VFSTRACE, "%s: advice with value '%d' not recognized,"
+                      "last supported advice is %s (value '%d'): rc = %d\n",
+                      sbi->ll_fsname, advice,
+                      ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
+               GOTO(out, rc);
+       }
+
+       /* Per-advice checks */
+       switch (advice) {
+       case LU_LADVISE_LOCKNOEXPAND:
+               if (flags & ~LF_LOCKNOEXPAND_MASK) {
+                       rc = -EINVAL;
+                       CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
+                              "rc = %d\n", sbi->ll_fsname, flags,
+                              ladvise_names[advice], rc);
+                       GOTO(out, rc);
+               }
+               break;
+       case LU_LADVISE_LOCKAHEAD:
+               /* Currently only READ and WRITE modes can be requested */
+               if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
+                   ladvise->lla_lockahead_mode == 0) {
+                       rc = -EINVAL;
+                       CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
+                              "rc = %d\n", sbi->ll_fsname,
+                              ladvise->lla_lockahead_mode,
+                              ladvise_names[advice], rc);
+                       GOTO(out, rc);
+               }
+               /* fallthrough */
+       case LU_LADVISE_WILLREAD:
+       case LU_LADVISE_DONTNEED:
+       default:
+               /* Note fall through above - These checks apply to all advices
+                * except LOCKNOEXPAND */
+               if (flags & ~LF_DEFAULT_MASK) {
+                       rc = -EINVAL;
+                       CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
+                              "rc = %d\n", sbi->ll_fsname, flags,
+                              ladvise_names[advice], rc);
+                       GOTO(out, rc);
+               }
+               if (ladvise->lla_start >= ladvise->lla_end) {
+                       rc = -EINVAL;
+                       CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
+                              "for %s: rc = %d\n", sbi->ll_fsname,
+                              ladvise->lla_start, ladvise->lla_end,
+                              ladvise_names[advice], rc);
+                       GOTO(out, rc);
+               }
+               break;
+       }
+
+out:
+       return rc;
+}
+#undef ERRSIZE
+
 /*
  * Give file access advices
  *
@@ -2484,6 +3143,15 @@ static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
        RETURN(rc);
 }
 
+static int ll_lock_noexpand(struct file *file, int flags)
+{
+       struct ll_file_data *fd = file->private_data;
+
+       fd->ll_lock_no_expand = !(flags & LF_UNSET);
+
+       return 0;
+}
+
 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
                        unsigned long arg)
 {
@@ -2494,6 +3162,9 @@ int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
                           sizeof(fsxattr)))
                RETURN(-EFAULT);
 
+       fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
+       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
+               fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
        fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
        if (copy_to_user((struct fsxattr __user *)arg,
                         &fsxattr, sizeof(fsxattr)))
@@ -2502,6 +3173,30 @@ int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
        RETURN(0);
 }
 
+int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
+{
+       /*
+        * Project Quota ID state is only allowed to change from within the init
+        * namespace. Enforce that restriction only if we are trying to change
+        * the quota ID state. Everything else is allowed in user namespaces.
+        */
+       if (current_user_ns() == &init_user_ns)
+               return 0;
+
+       if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
+               return -EINVAL;
+
+       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT)) {
+               if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
+                       return -EINVAL;
+       } else {
+               if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
                        unsigned long arg)
 {
@@ -2510,53 +3205,335 @@ int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
        struct ptlrpc_request *req = NULL;
        int rc = 0;
        struct fsxattr fsxattr;
+       struct cl_object *obj;
+       struct iattr *attr;
+       int flags;
 
-       /* only root could change project ID */
-       if (!cfs_capable(CFS_CAP_SYS_ADMIN))
-               RETURN(-EPERM);
+       if (copy_from_user(&fsxattr,
+                          (const struct fsxattr __user *)arg,
+                          sizeof(fsxattr)))
+               RETURN(-EFAULT);
+
+       rc = ll_ioctl_check_project(inode, &fsxattr);
+       if (rc)
+               RETURN(rc);
 
        op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
                                     LUSTRE_OPC_ANY, NULL);
        if (IS_ERR(op_data))
                RETURN(PTR_ERR(op_data));
 
-       if (copy_from_user(&fsxattr,
-                          (const struct fsxattr __user *)arg,
-                          sizeof(fsxattr)))
-               GOTO(out_fsxattr1, rc = -EFAULT);
-
+       flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
+       op_data->op_attr_flags = ll_inode_to_ext_flags(flags);
+       if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
+               op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
        op_data->op_projid = fsxattr.fsx_projid;
-       op_data->op_attr.ia_valid |= MDS_ATTR_PROJID;
+       op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
        rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
                        0, &req);
        ptlrpc_req_finished(req);
+       if (rc)
+               GOTO(out_fsxattr, rc);
+       ll_update_inode_flags(inode, op_data->op_attr_flags);
+       obj = ll_i2info(inode)->lli_clob;
+       if (obj == NULL)
+               GOTO(out_fsxattr, rc);
 
-out_fsxattr1:
+       OBD_ALLOC_PTR(attr);
+       if (attr == NULL)
+               GOTO(out_fsxattr, rc = -ENOMEM);
+
+       rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS,
+                           fsxattr.fsx_xflags);
+       OBD_FREE_PTR(attr);
+out_fsxattr:
        ll_finish_md_op_data(op_data);
        RETURN(rc);
+}
+
+static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
+                                unsigned long arg)
+{
+       struct inode            *inode = file_inode(file);
+       struct ll_file_data     *fd = file->private_data;
+       struct ll_inode_info    *lli = ll_i2info(inode);
+       struct obd_client_handle *och = NULL;
+       struct split_param sp;
+       struct pcc_param param;
+       bool lease_broken = false;
+       fmode_t fmode = 0;
+       enum mds_op_bias bias = 0;
+       struct file *layout_file = NULL;
+       void *data = NULL;
+       size_t data_size = 0;
+       bool attached = false;
+       long rc, rc2 = 0;
+
+       ENTRY;
+
+       mutex_lock(&lli->lli_och_mutex);
+       if (fd->fd_lease_och != NULL) {
+               och = fd->fd_lease_och;
+               fd->fd_lease_och = NULL;
+       }
+       mutex_unlock(&lli->lli_och_mutex);
+
+       if (och == NULL)
+               RETURN(-ENOLCK);
+
+       fmode = och->och_flags;
+
+       switch (ioc->lil_flags) {
+       case LL_LEASE_RESYNC_DONE:
+               if (ioc->lil_count > IOC_IDS_MAX)
+                       GOTO(out_lease_close, rc = -EINVAL);
+
+               data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
+               OBD_ALLOC(data, data_size);
+               if (!data)
+                       GOTO(out_lease_close, rc = -ENOMEM);
+
+               if (copy_from_user(data, (void __user *)arg, data_size))
+                       GOTO(out_lease_close, rc = -EFAULT);
+
+               bias = MDS_CLOSE_RESYNC_DONE;
+               break;
+       case LL_LEASE_LAYOUT_MERGE: {
+               int fd;
+
+               if (ioc->lil_count != 1)
+                       GOTO(out_lease_close, rc = -EINVAL);
+
+               arg += sizeof(*ioc);
+               if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
+                       GOTO(out_lease_close, rc = -EFAULT);
+
+               layout_file = fget(fd);
+               if (!layout_file)
+                       GOTO(out_lease_close, rc = -EBADF);
+
+               if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
+                               (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
+                       GOTO(out_lease_close, rc = -EPERM);
+
+               data = file_inode(layout_file);
+               bias = MDS_CLOSE_LAYOUT_MERGE;
+               break;
+       }
+       case LL_LEASE_LAYOUT_SPLIT: {
+               int fdv;
+               int mirror_id;
+
+               if (ioc->lil_count != 2)
+                       GOTO(out_lease_close, rc = -EINVAL);
+
+               arg += sizeof(*ioc);
+               if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
+                       GOTO(out_lease_close, rc = -EFAULT);
+
+               arg += sizeof(__u32);
+               if (copy_from_user(&mirror_id, (void __user *)arg,
+                                  sizeof(__u32)))
+                       GOTO(out_lease_close, rc = -EFAULT);
+
+               layout_file = fget(fdv);
+               if (!layout_file)
+                       GOTO(out_lease_close, rc = -EBADF);
+
+               sp.sp_inode = file_inode(layout_file);
+               sp.sp_mirror_id = (__u16)mirror_id;
+               data = &sp;
+               bias = MDS_CLOSE_LAYOUT_SPLIT;
+               break;
+       }
+       case LL_LEASE_PCC_ATTACH:
+               if (ioc->lil_count != 1)
+                       RETURN(-EINVAL);
 
+               arg += sizeof(*ioc);
+               if (copy_from_user(&param.pa_archive_id, (void __user *)arg,
+                                  sizeof(__u32)))
+                       GOTO(out_lease_close, rc2 = -EFAULT);
 
+               rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
+               if (rc2)
+                       GOTO(out_lease_close, rc2);
+
+               attached = true;
+               /* Grab latest data version */
+               rc2 = ll_data_version(inode, &param.pa_data_version,
+                                    LL_DV_WR_FLUSH);
+               if (rc2)
+                       GOTO(out_lease_close, rc2);
+
+               data = &param;
+               bias = MDS_PCC_ATTACH;
+               break;
+       default:
+               /* without close intent */
+               break;
+       }
+
+out_lease_close:
+       rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
+       if (rc < 0)
+               GOTO(out, rc);
+
+       rc = ll_lease_och_release(inode, file);
+       if (rc < 0)
+               GOTO(out, rc);
+
+       if (lease_broken)
+               fmode = 0;
+       EXIT;
+
+out:
+       switch (ioc->lil_flags) {
+       case LL_LEASE_RESYNC_DONE:
+               if (data)
+                       OBD_FREE(data, data_size);
+               break;
+       case LL_LEASE_LAYOUT_MERGE:
+       case LL_LEASE_LAYOUT_SPLIT:
+               if (layout_file)
+                       fput(layout_file);
+               break;
+       case LL_LEASE_PCC_ATTACH:
+               if (!rc)
+                       rc = rc2;
+               rc = pcc_readwrite_attach_fini(file, inode,
+                                              param.pa_layout_gen,
+                                              lease_broken, rc,
+                                              attached);
+               break;
+       }
+
+       if (!rc)
+               rc = ll_lease_type_from_fmode(fmode);
+       RETURN(rc);
+}
+
+static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
+                             unsigned long arg)
+{
+       struct inode *inode = file_inode(file);
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_file_data *fd = file->private_data;
+       struct obd_client_handle *och = NULL;
+       __u64 open_flags = 0;
+       bool lease_broken;
+       fmode_t fmode;
+       long rc;
+       ENTRY;
+
+       switch (ioc->lil_mode) {
+       case LL_LEASE_WRLCK:
+               if (!(file->f_mode & FMODE_WRITE))
+                       RETURN(-EPERM);
+               fmode = FMODE_WRITE;
+               break;
+       case LL_LEASE_RDLCK:
+               if (!(file->f_mode & FMODE_READ))
+                       RETURN(-EPERM);
+               fmode = FMODE_READ;
+               break;
+       case LL_LEASE_UNLCK:
+               RETURN(ll_file_unlock_lease(file, ioc, arg));
+       default:
+               RETURN(-EINVAL);
+       }
+
+       CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
+
+       /* apply for lease */
+       if (ioc->lil_flags & LL_LEASE_RESYNC)
+               open_flags = MDS_OPEN_RESYNC;
+       och = ll_lease_open(inode, file, fmode, open_flags);
+       if (IS_ERR(och))
+               RETURN(PTR_ERR(och));
+
+       if (ioc->lil_flags & LL_LEASE_RESYNC) {
+               rc = ll_lease_file_resync(och, inode, arg);
+               if (rc) {
+                       ll_lease_close(och, inode, NULL);
+                       RETURN(rc);
+               }
+               rc = ll_layout_refresh(inode, &fd->fd_layout_version);
+               if (rc) {
+                       ll_lease_close(och, inode, NULL);
+                       RETURN(rc);
+               }
+       }
+
+       rc = 0;
+       mutex_lock(&lli->lli_och_mutex);
+       if (fd->fd_lease_och == NULL) {
+               fd->fd_lease_och = och;
+               och = NULL;
+       }
+       mutex_unlock(&lli->lli_och_mutex);
+       if (och != NULL) {
+               /* impossible now that only excl is supported for now */
+               ll_lease_close(och, inode, &lease_broken);
+               rc = -EBUSY;
+       }
+       RETURN(rc);
+}
+
+static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       __u64 now = ktime_get_real_seconds();
+       int i;
+
+       spin_lock(&lli->lli_heat_lock);
+       heat->lh_flags = lli->lli_heat_flags;
+       for (i = 0; i < heat->lh_count; i++)
+               heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
+                                               now, sbi->ll_heat_decay_weight,
+                                               sbi->ll_heat_period_second);
+       spin_unlock(&lli->lli_heat_lock);
+}
+
+static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       int rc = 0;
+
+       spin_lock(&lli->lli_heat_lock);
+       if (flags & LU_HEAT_FLAG_CLEAR)
+               obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
+
+       if (flags & LU_HEAT_FLAG_OFF)
+               lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
+       else
+               lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
+
+       spin_unlock(&lli->lli_heat_lock);
+
+       RETURN(rc);
 }
 
 static long
 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct inode            *inode = file_inode(file);
-       struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data     *fd = file->private_data;
        int                      flags, rc;
        ENTRY;
 
        CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
               PFID(ll_inode2fid(inode)), inode, cmd);
-        ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
+       ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
 
-        /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
-        if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
-                RETURN(-ENOTTY);
+       /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
+       if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
+               RETURN(-ENOTTY);
 
-        switch(cmd) {
-        case LL_IOC_GETFLAGS:
-                /* Get the current value of the file flags */
+       switch (cmd) {
+       case LL_IOC_GETFLAGS:
+               /* Get the current value of the file flags */
                return put_user(fd->fd_flags, (int __user *)arg);
         case LL_IOC_SETFLAGS:
         case LL_IOC_CLRFLAGS:
@@ -2609,9 +3586,6 @@ ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                        struct ll_inode_info            *lli;
                        struct obd_client_handle        *och = NULL;
 
-                       if (lsl.sl_flags != SWAP_LAYOUTS_CLOSE)
-                               GOTO(out, rc = -EINVAL);
-
                        lli = ll_i2info(inode);
                        mutex_lock(&lli->lli_och_mutex);
                        if (fd->fd_lease_och != NULL) {
@@ -2633,12 +3607,18 @@ out:
        case LL_IOC_LOV_GETSTRIPE:
        case LL_IOC_LOV_GETSTRIPE_NEW:
                RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
-        case FSFILT_IOC_GETFLAGS:
-        case FSFILT_IOC_SETFLAGS:
-                RETURN(ll_iocontrol(inode, file, cmd, arg));
-        case FSFILT_IOC_GETVERSION_OLD:
-        case FSFILT_IOC_GETVERSION:
+       case FS_IOC_GETFLAGS:
+       case FS_IOC_SETFLAGS:
+               RETURN(ll_iocontrol(inode, file, cmd, arg));
+       case FSFILT_IOC_GETVERSION:
+       case FS_IOC_GETVERSION:
                RETURN(put_user(inode->i_generation, (int __user *)arg));
+       /* We need to special case any other ioctls we want to handle,
+        * to send them to the MDS/OST as appropriate and to properly
+        * network encode the arg field. */
+       case FS_IOC_SETVERSION:
+               RETURN(-ENOTSUPP);
+
         case LL_IOC_GROUP_LOCK:
                 RETURN(ll_get_grouplock(inode, file, arg));
         case LL_IOC_GROUP_UNLOCK:
@@ -2646,12 +3626,6 @@ out:
         case IOC_OBD_STATFS:
                RETURN(ll_obd_statfs(inode, (void __user *)arg));
 
-        /* We need to special case any other ioctls we want to handle,
-         * to send them to the MDS/OST as appropriate and to properly
-         * network encode the arg field.
-        case FSFILT_IOC_SETVERSION_OLD:
-        case FSFILT_IOC_SETVERSION:
-        */
        case LL_IOC_FLUSHCTX:
                RETURN(ll_flush_ctx(inode));
        case LL_IOC_PATH2FID: {
@@ -2674,7 +3648,7 @@ out:
                        RETURN(-EFAULT);
 
                idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
-               rc = ll_data_version(inode, &idv.idv_version, idv.idv_flags);
+               rc = ll_ioc_data_version(inode, &idv);
 
                if (rc == 0 &&
                    copy_to_user((char __user *)arg, &idv, sizeof(idv)))
@@ -2767,72 +3741,19 @@ out:
                ll_finish_md_op_data(op_data);
                OBD_FREE_PTR(hca);
                RETURN(rc);
-       }
-       case LL_IOC_SET_LEASE: {
-               struct ll_inode_info *lli = ll_i2info(inode);
-               struct obd_client_handle *och = NULL;
-               bool lease_broken;
-               fmode_t fmode;
-
-               switch (arg) {
-               case LL_LEASE_WRLCK:
-                       if (!(file->f_mode & FMODE_WRITE))
-                               RETURN(-EPERM);
-                       fmode = FMODE_WRITE;
-                       break;
-               case LL_LEASE_RDLCK:
-                       if (!(file->f_mode & FMODE_READ))
-                               RETURN(-EPERM);
-                       fmode = FMODE_READ;
-                       break;
-               case LL_LEASE_UNLCK:
-                       mutex_lock(&lli->lli_och_mutex);
-                       if (fd->fd_lease_och != NULL) {
-                               och = fd->fd_lease_och;
-                               fd->fd_lease_och = NULL;
-                       }
-                       mutex_unlock(&lli->lli_och_mutex);
-
-                       if (och == NULL)
-                               RETURN(-ENOLCK);
-
-                       fmode = och->och_flags;
-                       rc = ll_lease_close(och, inode, &lease_broken);
-                       if (rc < 0)
-                               RETURN(rc);
-
-                       rc = ll_lease_och_release(inode, file);
-                       if (rc < 0)
-                               RETURN(rc);
-
-                       if (lease_broken)
-                               fmode = 0;
-
-                       RETURN(ll_lease_type_from_fmode(fmode));
-               default:
-                       RETURN(-EINVAL);
-               }
+       }
+       case LL_IOC_SET_LEASE_OLD: {
+               struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
 
-               CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
+               RETURN(ll_file_set_lease(file, &ioc, 0));
+       }
+       case LL_IOC_SET_LEASE: {
+               struct ll_ioc_lease ioc;
 
-               /* apply for lease */
-               och = ll_lease_open(inode, file, fmode, 0);
-               if (IS_ERR(och))
-                       RETURN(PTR_ERR(och));
+               if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
+                       RETURN(-EFAULT);
 
-               rc = 0;
-               mutex_lock(&lli->lli_och_mutex);
-               if (fd->fd_lease_och == NULL) {
-                       fd->fd_lease_och = och;
-                       och = NULL;
-               }
-               mutex_unlock(&lli->lli_och_mutex);
-               if (och != NULL) {
-                       /* impossible now that only excl is supported for now */
-                       ll_lease_close(och, inode, &lease_broken);
-                       rc = -EBUSY;
-               }
-               RETURN(rc);
+               RETURN(ll_file_set_lease(file, &ioc, arg));
        }
        case LL_IOC_GET_LEASE: {
                struct ll_inode_info *lli = ll_i2info(inode);
@@ -2885,61 +3806,175 @@ out:
                RETURN(ll_file_futimes_3(file, &lfu));
        }
        case LL_IOC_LADVISE: {
-               struct llapi_ladvise_hdr *ladvise_hdr;
+               struct llapi_ladvise_hdr *k_ladvise_hdr;
+               struct llapi_ladvise_hdr __user *u_ladvise_hdr;
                int i;
                int num_advise;
-               int alloc_size = sizeof(*ladvise_hdr);
+               int alloc_size = sizeof(*k_ladvise_hdr);
 
                rc = 0;
-               OBD_ALLOC_PTR(ladvise_hdr);
-               if (ladvise_hdr == NULL)
+               u_ladvise_hdr = (void __user *)arg;
+               OBD_ALLOC_PTR(k_ladvise_hdr);
+               if (k_ladvise_hdr == NULL)
                        RETURN(-ENOMEM);
 
-               if (copy_from_user(ladvise_hdr,
-                                  (const struct llapi_ladvise_hdr __user *)arg,
-                                  alloc_size))
+               if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
                        GOTO(out_ladvise, rc = -EFAULT);
 
-               if (ladvise_hdr->lah_magic != LADVISE_MAGIC ||
-                   ladvise_hdr->lah_count < 1)
+               if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
+                   k_ladvise_hdr->lah_count < 1)
                        GOTO(out_ladvise, rc = -EINVAL);
 
-               num_advise = ladvise_hdr->lah_count;
+               num_advise = k_ladvise_hdr->lah_count;
                if (num_advise >= LAH_COUNT_MAX)
                        GOTO(out_ladvise, rc = -EFBIG);
 
-               OBD_FREE_PTR(ladvise_hdr);
-               alloc_size = offsetof(typeof(*ladvise_hdr),
+               OBD_FREE_PTR(k_ladvise_hdr);
+               alloc_size = offsetof(typeof(*k_ladvise_hdr),
                                      lah_advise[num_advise]);
-               OBD_ALLOC(ladvise_hdr, alloc_size);
-               if (ladvise_hdr == NULL)
+               OBD_ALLOC(k_ladvise_hdr, alloc_size);
+               if (k_ladvise_hdr == NULL)
                        RETURN(-ENOMEM);
 
                /*
                 * TODO: submit multiple advices to one server in a single RPC
                 */
-               if (copy_from_user(ladvise_hdr,
-                                  (const struct llapi_ladvise_hdr __user *)arg,
-                                  alloc_size))
+               if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
                        GOTO(out_ladvise, rc = -EFAULT);
 
                for (i = 0; i < num_advise; i++) {
-                       rc = ll_ladvise(inode, file, ladvise_hdr->lah_flags,
-                                       &ladvise_hdr->lah_advise[i]);
+                       struct llapi_lu_ladvise *k_ladvise =
+                                       &k_ladvise_hdr->lah_advise[i];
+                       struct llapi_lu_ladvise __user *u_ladvise =
+                                       &u_ladvise_hdr->lah_advise[i];
+
+                       rc = ll_ladvise_sanity(inode, k_ladvise);
                        if (rc)
+                               GOTO(out_ladvise, rc);
+
+                       switch (k_ladvise->lla_advice) {
+                       case LU_LADVISE_LOCKNOEXPAND:
+                               rc = ll_lock_noexpand(file,
+                                              k_ladvise->lla_peradvice_flags);
+                               GOTO(out_ladvise, rc);
+                       case LU_LADVISE_LOCKAHEAD:
+
+                               rc = ll_file_lock_ahead(file, k_ladvise);
+
+                               if (rc < 0)
+                                       GOTO(out_ladvise, rc);
+
+                               if (put_user(rc,
+                                            &u_ladvise->lla_lockahead_result))
+                                       GOTO(out_ladvise, rc = -EFAULT);
                                break;
+                       default:
+                               rc = ll_ladvise(inode, file,
+                                               k_ladvise_hdr->lah_flags,
+                                               k_ladvise);
+                               if (rc)
+                                       GOTO(out_ladvise, rc);
+                               break;
+                       }
+
                }
 
 out_ladvise:
-               OBD_FREE(ladvise_hdr, alloc_size);
+               OBD_FREE(k_ladvise_hdr, alloc_size);
                RETURN(rc);
        }
+       case LL_IOC_FLR_SET_MIRROR: {
+               /* mirror I/O must be direct to avoid polluting page cache
+                * by stale data. */
+               if (!(file->f_flags & O_DIRECT))
+                       RETURN(-EINVAL);
+
+               fd->fd_designated_mirror = (__u32)arg;
+               RETURN(0);
+       }
        case LL_IOC_FSGETXATTR:
                RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
        case LL_IOC_FSSETXATTR:
                RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
        case BLKSSZGET:
                RETURN(put_user(PAGE_SIZE, (int __user *)arg));
+       case LL_IOC_HEAT_GET: {
+               struct lu_heat uheat;
+               struct lu_heat *heat;
+               int size;
+
+               if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
+                       RETURN(-EFAULT);
+
+               if (uheat.lh_count > OBD_HEAT_COUNT)
+                       uheat.lh_count = OBD_HEAT_COUNT;
+
+               size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
+               OBD_ALLOC(heat, size);
+               if (heat == NULL)
+                       RETURN(-ENOMEM);
+
+               heat->lh_count = uheat.lh_count;
+               ll_heat_get(inode, heat);
+               rc = copy_to_user((char __user *)arg, heat, size);
+               OBD_FREE(heat, size);
+               RETURN(rc ? -EFAULT : 0);
+       }
+       case LL_IOC_HEAT_SET: {
+               __u64 flags;
+
+               if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
+                       RETURN(-EFAULT);
+
+               rc = ll_heat_set(inode, flags);
+               RETURN(rc);
+       }
+       case LL_IOC_PCC_DETACH: {
+               struct lu_pcc_detach *detach;
+
+               OBD_ALLOC_PTR(detach);
+               if (detach == NULL)
+                       RETURN(-ENOMEM);
+
+               if (copy_from_user(detach,
+                                  (const struct lu_pcc_detach __user *)arg,
+                                  sizeof(*detach)))
+                       GOTO(out_detach_free, rc = -EFAULT);
+
+               if (!S_ISREG(inode->i_mode))
+                       GOTO(out_detach_free, rc = -EINVAL);
+
+               if (!inode_owner_or_capable(inode))
+                       GOTO(out_detach_free, rc = -EPERM);
+
+               rc = pcc_ioctl_detach(inode, detach->pccd_opt);
+out_detach_free:
+               OBD_FREE_PTR(detach);
+               RETURN(rc);
+       }
+       case LL_IOC_PCC_STATE: {
+               struct lu_pcc_state __user *ustate =
+                       (struct lu_pcc_state __user *)arg;
+               struct lu_pcc_state *state;
+
+               OBD_ALLOC_PTR(state);
+               if (state == NULL)
+                       RETURN(-ENOMEM);
+
+               if (copy_from_user(state, ustate, sizeof(*state)))
+                       GOTO(out_state, rc = -EFAULT);
+
+               rc = pcc_ioctl_state(file, inode, state);
+               if (rc)
+                       GOTO(out_state, rc);
+
+               if (copy_to_user(ustate, state, sizeof(*state)))
+                       GOTO(out_state, rc = -EFAULT);
+
+out_state:
+               OBD_FREE_PTR(state);
+               RETURN(rc);
+       }
        default:
                RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
                                     (void __user *)arg));
@@ -3017,6 +4052,7 @@ static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
 {
        struct inode *inode = file_inode(file);
        loff_t retval, eof = 0;
+       ktime_t kstart = ktime_get();
 
        ENTRY;
        retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
@@ -3024,7 +4060,6 @@ static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
        CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
               PFID(ll_inode2fid(inode)), inode, retval, retval,
               origin);
-       ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
 
        if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
                retval = ll_glimpse_size(inode);
@@ -3034,7 +4069,10 @@ static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
        }
 
        retval = ll_generic_file_llseek_size(file, offset, origin,
-                                         ll_file_maxbytes(inode), eof);
+                                            ll_file_maxbytes(inode), eof);
+       if (retval >= 0)
+               ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
+                                  ktime_us_delta(ktime_get(), kstart));
        RETURN(retval);
 }
 
@@ -3042,7 +4080,7 @@ static int ll_flush(struct file *file, fl_owner_t id)
 {
        struct inode *inode = file_inode(file);
        struct ll_inode_info *lli = ll_i2info(inode);
-       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       struct ll_file_data *fd = file->private_data;
        int rc, err;
 
        LASSERT(!S_ISDIR(inode->i_mode));
@@ -3118,43 +4156,25 @@ int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
  * file_dentry() as is done otherwise.
  */
 
-#ifdef HAVE_FILE_FSYNC_4ARGS
 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
 {
        struct dentry *dentry = file_dentry(file);
-       bool lock_inode;
-#elif defined(HAVE_FILE_FSYNC_2ARGS)
-int ll_fsync(struct file *file, int datasync)
-{
-       struct dentry *dentry = file_dentry(file);
-       loff_t start = 0;
-       loff_t end = LLONG_MAX;
-#else
-int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
-{
-       loff_t start = 0;
-       loff_t end = LLONG_MAX;
-#endif
        struct inode *inode = dentry->d_inode;
        struct ll_inode_info *lli = ll_i2info(inode);
        struct ptlrpc_request *req;
+       ktime_t kstart = ktime_get();
        int rc, err;
+
        ENTRY;
 
-       CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
-              PFID(ll_inode2fid(inode)), inode);
-       ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
+       CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), start %lld, end %lld,"
+              "datasync %d\n",
+              PFID(ll_inode2fid(inode)), inode, start, end, datasync);
 
-#ifdef HAVE_FILE_FSYNC_4ARGS
-       rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
-       lock_inode = !lli->lli_inode_locked;
-       if (lock_inode)
-               inode_lock(inode);
-#else
        /* fsync's caller has already called _fdata{sync,write}, we want
         * that IO to finish before calling the osc and mdc sync methods */
-       rc = filemap_fdatawait(inode->i_mapping);
-#endif
+       rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
+       inode_lock(inode);
 
        /* catch async errors that were recorded back when async writeback
         * failed for pages in this mapping. */
@@ -3177,9 +4197,16 @@ int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
                ptlrpc_req_finished(req);
 
        if (S_ISREG(inode->i_mode)) {
-               struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+               struct ll_file_data *fd = file->private_data;
+               bool cached;
 
-               err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
+               /* Sync metadata on MDT first, and then sync the cached data
+                * on PCC.
+                */
+               err = pcc_fsync(file, start, end, datasync, &cached);
+               if (!cached)
+                       err = cl_sync_file_range(inode, start, end,
+                                                CL_FSYNC_ALL, 0);
                if (rc == 0 && err < 0)
                        rc = err;
                if (rc < 0)
@@ -3188,10 +4215,11 @@ int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
                        fd->fd_write_failed = false;
        }
 
-#ifdef HAVE_FILE_FSYNC_4ARGS
-       if (lock_inode)
-               inode_unlock(inode);
-#endif
+       inode_unlock(inode);
+
+       if (!rc)
+               ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
+                                  ktime_us_delta(ktime_get(), kstart));
        RETURN(rc);
 }
 
@@ -3209,6 +4237,7 @@ ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
        struct lustre_handle lockh = { 0 };
        union ldlm_policy_data flock = { { 0 } };
        int fl_type = file_lock->fl_type;
+       ktime_t kstart = ktime_get();
        __u64 flags = 0;
        int rc;
        int rc2 = 0;
@@ -3217,23 +4246,22 @@ ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
        CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
               PFID(ll_inode2fid(inode)), file_lock);
 
-        ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
-
-        if (file_lock->fl_flags & FL_FLOCK) {
-                LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
-                /* flocks are whole-file locks */
-                flock.l_flock.end = OFFSET_MAX;
-                /* For flocks owner is determined by the local file desctiptor*/
-                flock.l_flock.owner = (unsigned long)file_lock->fl_file;
-        } else if (file_lock->fl_flags & FL_POSIX) {
-                flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
-                flock.l_flock.start = file_lock->fl_start;
-                flock.l_flock.end = file_lock->fl_end;
-        } else {
-                RETURN(-EINVAL);
-        }
-        flock.l_flock.pid = file_lock->fl_pid;
+       if (file_lock->fl_flags & FL_FLOCK) {
+               LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
+               /* flocks are whole-file locks */
+               flock.l_flock.end = OFFSET_MAX;
+               /* For flocks owner is determined by the local file desctiptor*/
+               flock.l_flock.owner = (unsigned long)file_lock->fl_file;
+       } else if (file_lock->fl_flags & FL_POSIX) {
+               flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
+               flock.l_flock.start = file_lock->fl_start;
+               flock.l_flock.end = file_lock->fl_end;
+       } else {
+               RETURN(-EINVAL);
+       }
+       flock.l_flock.pid = file_lock->fl_pid;
 
+#if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
        /* Somewhat ugly workaround for svc lockd.
         * lockd installs custom fl_lmops->lm_compare_owner that checks
         * for the fl_owner to be the same (which it always is on local node
@@ -3243,6 +4271,7 @@ ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
         * pointer space for current->files are not intersecting */
        if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
                flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
+#endif
 
        switch (fl_type) {
         case F_RDLCK:
@@ -3335,7 +4364,10 @@ ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
 
        ll_finish_md_op_data(op_data);
 
-        RETURN(rc);
+       if (!rc)
+               ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
+                                  ktime_us_delta(ktime_get(), kstart));
+       RETURN(rc);
 }
 
 int ll_get_fid_by_name(struct inode *parent, const char *name,
@@ -3372,48 +4404,60 @@ out_req:
        RETURN(rc);
 }
 
-int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
-              const char *name, int namelen)
+int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
+              const char *name)
 {
-       struct dentry         *dchild = NULL;
-       struct inode          *child_inode = NULL;
-       struct md_op_data     *op_data;
+       struct dentry *dchild = NULL;
+       struct inode *child_inode = NULL;
+       struct md_op_data *op_data;
        struct ptlrpc_request *request = NULL;
        struct obd_client_handle *och = NULL;
-       struct qstr           qstr;
-       struct mdt_body         *body;
-       int                    rc;
-       __u64                   data_version = 0;
+       struct qstr qstr;
+       struct mdt_body *body;
+       __u64 data_version = 0;
+       size_t namelen = strlen(name);
+       int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
+       int rc;
        ENTRY;
 
-       CDEBUG(D_VFSTRACE, "migrate %s under "DFID" to MDT%04x\n",
-              name, PFID(ll_inode2fid(parent)), mdtidx);
+       CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
+              PFID(ll_inode2fid(parent)), name,
+              lum->lum_stripe_offset, lum->lum_stripe_count);
 
-       op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
-                                    0, LUSTRE_OPC_ANY, NULL);
-       if (IS_ERR(op_data))
-               RETURN(PTR_ERR(op_data));
+       if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
+           lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
+               lustre_swab_lmv_user_md(lum);
 
        /* Get child FID first */
        qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
        qstr.name = name;
        qstr.len = namelen;
        dchild = d_lookup(file_dentry(file), &qstr);
-       if (dchild != NULL) {
-               if (dchild->d_inode != NULL)
+       if (dchild) {
+               if (dchild->d_inode)
                        child_inode = igrab(dchild->d_inode);
                dput(dchild);
        }
 
-       if (child_inode == NULL) {
-               rc = ll_get_fid_by_name(parent, name, namelen,
-                                       &op_data->op_fid3, &child_inode);
-               if (rc != 0)
-                       GOTO(out_free, rc);
+       if (!child_inode) {
+               rc = ll_get_fid_by_name(parent, name, namelen, NULL,
+                                       &child_inode);
+               if (rc)
+                       RETURN(rc);
        }
 
-       if (child_inode == NULL)
-               GOTO(out_free, rc = -EINVAL);
+       if (!child_inode)
+               RETURN(-ENOENT);
+
+       if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
+             OBD_CONNECT2_DIR_MIGRATE)) {
+               if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
+                   ll_dir_striped(child_inode)) {
+                       CERROR("%s: MDT doesn't support stripe directory "
+                              "migration!\n", ll_i2sbi(parent)->ll_fsname);
+                       GOTO(out_iput, rc = -EOPNOTSUPP);
+               }
+       }
 
        /*
         * lfs migrate command needs to be blocked on the client
@@ -3423,24 +4467,24 @@ int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
        if (child_inode == parent->i_sb->s_root->d_inode)
                GOTO(out_iput, rc = -EINVAL);
 
+       op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
+                                    child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
+       if (IS_ERR(op_data))
+               GOTO(out_iput, rc = PTR_ERR(op_data));
+
        inode_lock(child_inode);
        op_data->op_fid3 = *ll_inode2fid(child_inode);
        if (!fid_is_sane(&op_data->op_fid3)) {
                CERROR("%s: migrate %s, but FID "DFID" is insane\n",
-                      ll_get_fsname(parent->i_sb, NULL, 0), name,
+                      ll_i2sbi(parent)->ll_fsname, name,
                       PFID(&op_data->op_fid3));
                GOTO(out_unlock, rc = -EINVAL);
        }
 
-       rc = ll_get_mdt_idx_by_fid(ll_i2sbi(parent), &op_data->op_fid3);
-       if (rc < 0)
-               GOTO(out_unlock, rc);
+       op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
+       op_data->op_data = lum;
+       op_data->op_data_size = lumlen;
 
-       if (rc == mdtidx) {
-               CDEBUG(D_INFO, "%s: "DFID" is already on MDT%04x\n", name,
-                      PFID(&op_data->op_fid3), mdtidx);
-               GOTO(out_unlock, rc = 0);
-       }
 again:
        if (S_ISREG(child_inode->i_mode)) {
                och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
@@ -3455,32 +4499,34 @@ again:
                if (rc != 0)
                        GOTO(out_close, rc);
 
-               op_data->op_handle = och->och_fh;
-               op_data->op_data = och->och_mod;
+               op_data->op_open_handle = och->och_open_handle;
                op_data->op_data_version = data_version;
                op_data->op_lease_handle = och->och_lease_handle;
-               op_data->op_bias |= MDS_RENAME_MIGRATE;
+               op_data->op_bias |= MDS_CLOSE_MIGRATE;
+
+               spin_lock(&och->och_mod->mod_open_req->rq_lock);
+               och->och_mod->mod_open_req->rq_replay = 0;
+               spin_unlock(&och->och_mod->mod_open_req->rq_lock);
        }
 
-       op_data->op_mds = mdtidx;
-       op_data->op_cli_flags = CLI_MIGRATE;
-       rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name,
-                      namelen, name, namelen, &request);
+       rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
+                      name, namelen, &request);
        if (rc == 0) {
                LASSERT(request != NULL);
                ll_update_times(request, parent);
+       }
 
+       if (rc == 0 || rc == -EAGAIN) {
                body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
                LASSERT(body != NULL);
 
                /* If the server does release layout lock, then we cleanup
                 * the client och here, otherwise release it in out_close: */
-               if (och != NULL &&
-                   body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
+               if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
                        obd_mod_put(och->och_mod);
                        md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
                                                  och);
-                       och->och_fh.cookie = DEAD_HANDLE_MAGIC;
+                       och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
                        OBD_FREE_PTR(och);
                        och = NULL;
                }
@@ -3491,30 +4537,40 @@ again:
                request = NULL;
        }
 
-       /* Try again if the file layout has changed. */
+       /* Try again if the lease has cancelled. */
        if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
                goto again;
 
 out_close:
-       if (och != NULL) /* close the file */
+       if (och)
                ll_lease_close(och, child_inode, NULL);
-       if (rc == 0)
+       if (!rc)
                clear_nlink(child_inode);
 out_unlock:
        inode_unlock(child_inode);
+       ll_finish_md_op_data(op_data);
 out_iput:
        iput(child_inode);
-out_free:
-       ll_finish_md_op_data(op_data);
        RETURN(rc);
 }
 
 static int
 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
 {
-        ENTRY;
+       struct ll_file_data *fd = file->private_data;
+       ENTRY;
 
-        RETURN(-ENOSYS);
+       /*
+        * In order to avoid flood of warning messages, only print one message
+        * for one file. And the entire message rate on the client is limited
+        * by CDEBUG_LIMIT too.
+        */
+       if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
+               fd->fd_flags |= LL_FILE_FLOCK_WARNING;
+               CDEBUG_LIMIT(D_TTY | D_CONSOLE,
+                            "flock disabled, mount with '-o [local]flock' to enable\r\n");
+       }
+       RETURN(-ENOSYS);
 }
 
 /**
@@ -3546,7 +4602,7 @@ int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
                ldlm_lockname[mode]);
 
        flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
-       for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
+       for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
                policy.l_inodebits.bits = *bits & (1 << i);
                if (policy.l_inodebits.bits == 0)
                        continue;
@@ -3594,8 +4650,7 @@ static int ll_inode_revalidate_fini(struct inode *inode, int rc)
                /* If it is striped directory, and there is bad stripe
                 * Let's revalidate the dentry again, instead of returning
                 * error */
-               if (S_ISDIR(inode->i_mode) &&
-                   ll_i2info(inode)->lli_lsm_md != NULL)
+               if (ll_dir_striped(inode))
                        return 0;
 
                /* This path cannot be hit for regular files unless in
@@ -3606,112 +4661,79 @@ static int ll_inode_revalidate_fini(struct inode *inode, int rc)
        } else if (rc != 0) {
                CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
                             "%s: revalidate FID "DFID" error: rc = %d\n",
-                            ll_get_fsname(inode->i_sb, NULL, 0),
+                            ll_i2sbi(inode)->ll_fsname,
                             PFID(ll_inode2fid(inode)), rc);
        }
 
        return rc;
 }
 
-static int __ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
+static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
 {
-        struct inode *inode = dentry->d_inode;
-        struct ptlrpc_request *req = NULL;
-        struct obd_export *exp;
-        int rc = 0;
-        ENTRY;
-
-        LASSERT(inode != NULL);
+       struct inode *inode = dentry->d_inode;
+       struct obd_export *exp = ll_i2mdexp(inode);
+       struct lookup_intent oit = {
+               .it_op = op,
+       };
+       struct ptlrpc_request *req = NULL;
+       struct md_op_data *op_data;
+       int rc = 0;
+       ENTRY;
 
        CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
               PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
 
-        exp = ll_i2mdexp(inode);
-
-        /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
-         *      But under CMD case, it caused some lock issues, should be fixed
-         *      with new CMD ibits lock. See bug 12718 */
-       if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) {
-                struct lookup_intent oit = { .it_op = IT_GETATTR };
-                struct md_op_data *op_data;
-
-                if (ibits == MDS_INODELOCK_LOOKUP)
-                        oit.it_op = IT_LOOKUP;
-
-                /* Call getattr by fid, so do not provide name at all. */
-                op_data = ll_prep_md_op_data(NULL, dentry->d_inode,
-                                             dentry->d_inode, NULL, 0, 0,
-                                             LUSTRE_OPC_ANY, NULL);
-                if (IS_ERR(op_data))
-                        RETURN(PTR_ERR(op_data));
-
-               rc = md_intent_lock(exp, op_data, &oit, &req,
-                                   &ll_md_blocking_ast, 0);
-                ll_finish_md_op_data(op_data);
-                if (rc < 0) {
-                        rc = ll_inode_revalidate_fini(inode, rc);
-                        GOTO (out, rc);
-                }
-
-                rc = ll_revalidate_it_finish(req, &oit, dentry);
-                if (rc != 0) {
-                        ll_intent_release(&oit);
-                        GOTO(out, rc);
-                }
-
-                /* Unlinked? Unhash dentry, so it is not picked up later by
-                   do_lookup() -> ll_revalidate_it(). We cannot use d_drop
-                   here to preserve get_cwd functionality on 2.6.
-                   Bug 10503 */
-               if (!dentry->d_inode->i_nlink) {
-                       ll_lock_dcache(inode);
-                       d_lustre_invalidate(dentry, 0);
-                       ll_unlock_dcache(inode);
-               }
+       /* Call getattr by fid, so do not provide name at all. */
+       op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
+                                    LUSTRE_OPC_ANY, NULL);
+       if (IS_ERR(op_data))
+               RETURN(PTR_ERR(op_data));
 
-                ll_lookup_finish_locks(&oit, dentry);
-        } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
-               struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
-               u64 valid = OBD_MD_FLGETATTR;
-               struct md_op_data *op_data;
-               int ealen = 0;
+       rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
+       ll_finish_md_op_data(op_data);
+       if (rc < 0) {
+               rc = ll_inode_revalidate_fini(inode, rc);
+               GOTO(out, rc);
+       }
 
-               if (S_ISREG(inode->i_mode)) {
-                       rc = ll_get_default_mdsize(sbi, &ealen);
-                       if (rc)
-                               RETURN(rc);
-                       valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
-               }
+       rc = ll_revalidate_it_finish(req, &oit, dentry);
+       if (rc != 0) {
+               ll_intent_release(&oit);
+               GOTO(out, rc);
+       }
 
-                op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
-                                             0, ealen, LUSTRE_OPC_ANY,
-                                             NULL);
-                if (IS_ERR(op_data))
-                        RETURN(PTR_ERR(op_data));
-
-                op_data->op_valid = valid;
-                rc = md_getattr(sbi->ll_md_exp, op_data, &req);
-                ll_finish_md_op_data(op_data);
-                if (rc) {
-                        rc = ll_inode_revalidate_fini(inode, rc);
-                        RETURN(rc);
-                }
+       /* Unlinked? Unhash dentry, so it is not picked up later by
+        * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
+        * here to preserve get_cwd functionality on 2.6.
+        * Bug 10503 */
+       if (!dentry->d_inode->i_nlink) {
+               spin_lock(&inode->i_lock);
+               d_lustre_invalidate(dentry, 0);
+               spin_unlock(&inode->i_lock);
+       }
 
-                rc = ll_prep_inode(&inode, req, NULL, NULL);
-        }
+       ll_lookup_finish_locks(&oit, dentry);
 out:
-        ptlrpc_req_finished(req);
-        return rc;
+       ptlrpc_req_finished(req);
+
+       return rc;
 }
 
 static int ll_merge_md_attr(struct inode *inode)
 {
+       struct ll_inode_info *lli = ll_i2info(inode);
        struct cl_attr attr = { 0 };
        int rc;
 
-       LASSERT(ll_i2info(inode)->lli_lsm_md != NULL);
+       LASSERT(lli->lli_lsm_md != NULL);
+
+       if (!lmv_dir_striped(lli->lli_lsm_md))
+               RETURN(0);
+
+       down_read(&lli->lli_lsm_sem);
        rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
                           &attr, ll_md_blocking_ast);
+       up_read(&lli->lli_lsm_sem);
        if (rc != 0)
                RETURN(rc);
 
@@ -3726,30 +4748,25 @@ static int ll_merge_md_attr(struct inode *inode)
        RETURN(0);
 }
 
-static int
-ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
+int ll_getattr_dentry(struct dentry *de, struct kstat *stat)
 {
-       struct inode    *inode = dentry->d_inode;
-       int              rc;
-       ENTRY;
+       struct inode *inode = de->d_inode;
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       struct ll_inode_info *lli = ll_i2info(inode);
+       ktime_t kstart = ktime_get();
+       int rc;
 
-       rc = __ll_inode_revalidate(dentry, ibits);
-       if (rc != 0)
+       rc = ll_inode_revalidate(de, IT_GETATTR);
+       if (rc < 0)
                RETURN(rc);
 
-       /* if object isn't regular file, don't validate size */
-       if (!S_ISREG(inode->i_mode)) {
-               if (S_ISDIR(inode->i_mode) &&
-                   ll_i2info(inode)->lli_lsm_md != NULL) {
-                       rc = ll_merge_md_attr(inode);
-                       if (rc != 0)
-                               RETURN(rc);
-               }
+       if (S_ISREG(inode->i_mode)) {
+               bool cached;
+
+               rc = pcc_inode_getattr(inode, &cached);
+               if (cached && rc < 0)
+                       RETURN(rc);
 
-               LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_atime;
-               LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_mtime;
-               LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_ctime;
-       } else {
                /* In case of restore, the MDT has the right size and has
                 * already send it back without granting the layout lock,
                 * inode is up-to-date so glimpse is useless.
@@ -3757,45 +4774,23 @@ ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
                 * restore the MDT holds the layout lock so the glimpse will
                 * block up to the end of restore (getattr will block)
                 */
-               if (!ll_file_test_flag(ll_i2info(inode), LLIF_FILE_RESTORING))
+               if (!cached && !ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
                        rc = ll_glimpse_size(inode);
-       }
-       RETURN(rc);
-}
-
-static inline dev_t ll_compat_encode_dev(dev_t dev)
-{
-       /* The compat_sys_*stat*() syscalls will fail unless the
-        * device majors and minors are both less than 256. Note that
-        * the value returned here will be passed through
-        * old_encode_dev() in cp_compat_stat(). And so we are not
-        * trying to return a valid compat (u16) device number, just
-        * one that will pass the old_valid_dev() check. */
-
-       return MKDEV(MAJOR(dev) & 0xff, MINOR(dev) & 0xff);
-}
-
-#ifdef HAVE_INODEOPS_ENHANCED_GETATTR
-int ll_getattr(const struct path *path, struct kstat *stat,
-              u32 request_mask, unsigned int flags)
-
-{
-       struct dentry *de = path->dentry;
-#else
-int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
-{
-#endif
-        struct inode *inode = de->d_inode;
-        struct ll_sb_info *sbi = ll_i2sbi(inode);
-        struct ll_inode_info *lli = ll_i2info(inode);
-        int res = 0;
-
-       res = ll_inode_revalidate(de, MDS_INODELOCK_UPDATE |
-                                     MDS_INODELOCK_LOOKUP);
-        ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
+                       if (rc < 0)
+                               RETURN(rc);
+               }
+       } else {
+               /* If object isn't regular a file then don't validate size. */
+               if (ll_dir_striped(inode)) {
+                       rc = ll_merge_md_attr(inode);
+                       if (rc < 0)
+                               RETURN(rc);
+               }
 
-        if (res)
-                return res;
+               inode->i_atime.tv_sec = lli->lli_atime;
+               inode->i_mtime.tv_sec = lli->lli_mtime;
+               inode->i_ctime.tv_sec = lli->lli_ctime;
+       }
 
        OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
 
@@ -3821,7 +4816,22 @@ int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
        stat->size = i_size_read(inode);
        stat->blocks = inode->i_blocks;
 
-        return 0;
+       ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
+                          ktime_us_delta(ktime_get(), kstart));
+
+       return 0;
+}
+
+#ifdef HAVE_INODEOPS_ENHANCED_GETATTR
+int ll_getattr(const struct path *path, struct kstat *stat,
+              u32 request_mask, unsigned int flags)
+{
+       struct dentry *de = path->dentry;
+#else
+int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
+{
+#endif
+       return ll_getattr_dentry(de, stat);
 }
 
 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
@@ -3877,99 +4887,66 @@ struct posix_acl *ll_get_acl(struct inode *inode, int type)
 }
 
 #ifdef HAVE_IOP_SET_ACL
-#ifdef CONFIG_FS_POSIX_ACL
+#ifdef CONFIG_LUSTRE_FS_POSIX_ACL
 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
+       struct ll_sb_info *sbi = ll_i2sbi(inode);
+       struct ptlrpc_request *req = NULL;
        const char *name = NULL;
        char *value = NULL;
-       size_t size = 0;
+       size_t value_size = 0;
        int rc = 0;
        ENTRY;
 
        switch (type) {
        case ACL_TYPE_ACCESS:
-               if (acl) {
-                       rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
-                       if (rc)
-                               GOTO(out, rc);
-               }
                name = XATTR_NAME_POSIX_ACL_ACCESS;
+               if (acl)
+                       rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
                break;
+
        case ACL_TYPE_DEFAULT:
-               if (!S_ISDIR(inode->i_mode))
-                       GOTO(out, rc = acl ? -EACCES : 0);
                name = XATTR_NAME_POSIX_ACL_DEFAULT;
+               if (!S_ISDIR(inode->i_mode))
+                       rc = acl ? -EACCES : 0;
                break;
+
        default:
-               GOTO(out, rc = -EINVAL);
+               rc = -EINVAL;
+               break;
        }
+       if (rc)
+               return rc;
 
        if (acl) {
-               size = posix_acl_xattr_size(acl->a_count);
-               value = kmalloc(size, GFP_NOFS);
+               value_size = posix_acl_xattr_size(acl->a_count);
+               value = kmalloc(value_size, GFP_NOFS);
                if (value == NULL)
                        GOTO(out, rc = -ENOMEM);
 
-               rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
+               rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
                if (rc < 0)
-                       GOTO(out_free, rc);
+                       GOTO(out_value, rc);
        }
 
-       /* dentry is only used for *.lov attributes so it's safe to be NULL */
-       rc = __vfs_setxattr(NULL, inode, name, value, size, XATTR_CREATE);
-out_free:
+       rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
+                        value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
+                        name, value, value_size, 0, 0, &req);
+
+       ptlrpc_req_finished(req);
+out_value:
        kfree(value);
 out:
-       if (!rc)
-               set_cached_acl(inode, type, acl);
-       else
+       if (rc)
                forget_cached_acl(inode, type);
+       else
+               set_cached_acl(inode, type, acl);
        RETURN(rc);
 }
-#endif /* CONFIG_FS_POSIX_ACL */
+#endif /* CONFIG_LUSTRE_FS_POSIX_ACL */
 #endif /* HAVE_IOP_SET_ACL */
 
-#ifndef HAVE_GENERIC_PERMISSION_2ARGS
-static int
-# ifdef HAVE_GENERIC_PERMISSION_4ARGS
-ll_check_acl(struct inode *inode, int mask, unsigned int flags)
-# else
-ll_check_acl(struct inode *inode, int mask)
-# endif
-{
-# ifdef CONFIG_FS_POSIX_ACL
-       struct posix_acl *acl;
-       int rc;
-       ENTRY;
-
-#  ifdef HAVE_GENERIC_PERMISSION_4ARGS
-       if (flags & IPERM_FLAG_RCU)
-               return -ECHILD;
-#  endif
-       acl = ll_get_acl(inode, ACL_TYPE_ACCESS);
-
-       if (!acl)
-               RETURN(-EAGAIN);
-
-       rc = posix_acl_permission(inode, acl, mask);
-       posix_acl_release(acl);
-
-       RETURN(rc);
-# else /* !CONFIG_FS_POSIX_ACL */
-       return -EAGAIN;
-# endif /* CONFIG_FS_POSIX_ACL */
-}
-#endif /* HAVE_GENERIC_PERMISSION_2ARGS */
-
-#ifdef HAVE_GENERIC_PERMISSION_4ARGS
-int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
-#else
-# ifdef HAVE_INODE_PERMISION_2ARGS
 int ll_inode_permission(struct inode *inode, int mask)
-# else
-int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
-# endif
-#endif
 {
        int rc = 0;
        struct ll_sb_info *sbi;
@@ -3978,22 +4955,17 @@ int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
        const struct cred *old_cred = NULL;
        cfs_cap_t cap;
        bool squash_id = false;
+       ktime_t kstart = ktime_get();
        ENTRY;
 
-#ifdef MAY_NOT_BLOCK
        if (mask & MAY_NOT_BLOCK)
                return -ECHILD;
-#elif defined(HAVE_GENERIC_PERMISSION_4ARGS)
-       if (flags & IPERM_FLAG_RCU)
-               return -ECHILD;
-#endif
 
        /* as root inode are NOT getting validated in lookup operation,
         * need to do it before permission check. */
 
         if (inode == inode->i_sb->s_root->d_inode) {
-               rc = __ll_inode_revalidate(inode->i_sb->s_root,
-                                          MDS_INODELOCK_LOOKUP);
+               rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
                 if (rc)
                         RETURN(rc);
         }
@@ -4029,14 +5001,17 @@ int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
                old_cred = override_creds(cred);
        }
 
-       ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM, 1);
-       rc = ll_generic_permission(inode, mask, flags, ll_check_acl);
+       rc = generic_permission(inode, mask);
        /* restore current process's credentials and FS capability */
        if (squash_id) {
                revert_creds(old_cred);
                put_cred(cred);
        }
 
+       if (!rc)
+               ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
+                                  ktime_us_delta(ktime_get(), kstart));
+
        RETURN(rc);
 }
 
@@ -4195,7 +5170,6 @@ static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
 {
        struct ll_sb_info *sbi = ll_i2sbi(inode);
        struct ptlrpc_request *req;
-       struct mdt_body *body;
        void *lvbdata;
        void *lmm;
        int lmmsize;
@@ -4215,18 +5189,20 @@ static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
         * layout here. Please note that we can't use the LVB buffer in
         * completion AST because it doesn't have a large enough buffer */
        rc = ll_get_default_mdsize(sbi, &lmmsize);
-       if (rc == 0)
-               rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
-                               OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
-                               lmmsize, 0, &req);
        if (rc < 0)
                RETURN(rc);
 
-       body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
-       if (body == NULL)
-               GOTO(out, rc = -EPROTO);
+       rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
+                        XATTR_NAME_LOV, lmmsize, &req);
+       if (rc < 0) {
+               if (rc == -ENODATA)
+                       GOTO(out, rc = 0); /* empty layout */
+               else
+                       RETURN(rc);
+       }
 
-       lmmsize = body->mbo_eadatasize;
+       lmmsize = rc;
+       rc = 0;
        if (lmmsize == 0) /* empty layout */
                GOTO(out, rc = 0);
 
@@ -4323,8 +5299,7 @@ out:
        /* wait for IO to complete if it's still being used. */
        if (wait_layout) {
                CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid), inode);
+                      sbi->ll_fsname, PFID(&lli->lli_fid), inode);
 
                memset(&conf, 0, sizeof conf);
                conf.coc_opc = OBJECT_CONF_WAIT;
@@ -4334,8 +5309,7 @@ out:
                        rc = -EAGAIN;
 
                CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
-                      ll_get_fsname(inode->i_sb, NULL, 0),
-                      PFID(&lli->lli_fid), rc);
+                      sbi->ll_fsname, PFID(&lli->lli_fid), rc);
        }
        RETURN(rc);
 }
@@ -4373,8 +5347,7 @@ static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
                it.it_flags = FMODE_WRITE;
 
        LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
-                         ll_get_fsname(inode->i_sb, NULL, 0),
-                         PFID(&lli->lli_fid), inode);
+                         sbi->ll_fsname, PFID(&lli->lli_fid), inode);
 
        rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
                            &ll_md_blocking_ast, 0);
@@ -4433,7 +5406,8 @@ int ll_layout_refresh(struct inode *inode, __u32 *gen)
                /* mostly layout lock is caching on the local side, so try to
                 * match it before grabbing layout lock mutex. */
                mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
-                                      LCK_CR | LCK_CW | LCK_PR | LCK_PW);
+                                      LCK_CR | LCK_CW | LCK_PR |
+                                      LCK_PW | LCK_EX);
                if (mode != 0) { /* hit cached lock */
                        rc = ll_layout_lock_set(&lockh, mode, inode);
                        if (rc == -EAGAIN)
@@ -4457,19 +5431,20 @@ int ll_layout_refresh(struct inode *inode, __u32 *gen)
  * Issue layout intent RPC indicating where in a file an IO is about to write.
  *
  * \param[in] inode    file inode.
- * \param[in] start    start offset of fille in bytes where an IO is about to
- *                     write.
- * \param[in] end      exclusive end offset in bytes of the write range.
+ * \param[in] ext      write range with start offset of fille in bytes where
+ *                     an IO is about to write, and exclusive end offset in
+ *                     bytes.
  *
  * \retval 0   on success
  * \retval < 0 error code
  */
-int ll_layout_write_intent(struct inode *inode, __u64 start, __u64 end)
+int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
+                          struct lu_extent *ext)
 {
        struct layout_intent intent = {
-               .li_opc = LAYOUT_INTENT_WRITE,
-               .li_start = start,
-               .li_end = end,
+               .li_opc = opc,
+               .li_extent.e_start = ext->e_start,
+               .li_extent.e_end = ext->e_end,
        };
        int rc;
        ENTRY;