Whamcloud - gitweb
LU-13636 osd: create agent inode with explicit owner
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
index 118bec0..c561425 100644 (file)
 
 #define DEBUG_SUBSYSTEM S_OSD
 
+#include <linux/fs_struct.h>
 #include <linux/kallsyms.h>
 #include <linux/module.h>
 #include <linux/user_namespace.h>
-#ifdef HAVE_UIDGID_HEADER
-# include <linux/uidgid.h>
-#endif
+#include <linux/uidgid.h>
 
 /* prerequisite for linux/xattr.h */
 #include <linux/types.h>
@@ -77,6 +76,9 @@
 
 #include <lustre_linkea.h>
 
+/* Maximum EA size is limited by LNET_MTU for remote objects */
+#define OSD_MAX_EA_SIZE 1048364
+
 int ldiskfs_pdo = 1;
 module_param(ldiskfs_pdo, int, 0644);
 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
@@ -137,12 +139,12 @@ int osd_trans_declare_op2rb[] = {
 
 static int osd_has_index(const struct osd_object *obj)
 {
-        return obj->oo_dt.do_index_ops != NULL;
+       return obj->oo_dt.do_index_ops != NULL;
 }
 
 static int osd_object_invariant(const struct lu_object *l)
 {
-        return osd_invariant(osd_obj(l));
+       return osd_invariant(osd_obj(l));
 }
 
 /*
@@ -150,18 +152,19 @@ static int osd_object_invariant(const struct lu_object *l)
  */
 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
 {
-        struct osd_thread_info *oti = osd_oti_get(env);
-        return oti->oti_w_locks > 0 && o->oo_owner == env;
+       struct osd_thread_info *oti = osd_oti_get(env);
+
+       return oti->oti_w_locks > 0 && o->oo_owner == env;
 }
 
 /*
  * Concurrency: doesn't access mutable data
  */
 static int osd_root_get(const struct lu_env *env,
-                        struct dt_device *dev, struct lu_fid *f)
+                       struct dt_device *dev, struct lu_fid *f)
 {
-        lu_local_obj_fid(f, OSD_FS_ROOT_OID);
-        return 0;
+       lu_local_obj_fid(f, OSD_FS_ROOT_OID);
+       return 0;
 }
 
 /*
@@ -179,9 +182,10 @@ static struct osd_idmap_cache *
 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
             const struct lu_fid *fid)
 {
-       struct osd_thread_info  *oti   = osd_oti_get(env);
-       struct osd_idmap_cache  *idc    = oti->oti_ins_cache;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_idmap_cache *idc = oti->oti_ins_cache;
        int i;
+
        for (i = 0; i < oti->oti_ins_cache_used; i++) {
                if (!lu_fid_eq(&idc[i].oic_fid, fid))
                        continue;
@@ -198,22 +202,22 @@ static struct osd_idmap_cache *
 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
            const struct lu_fid *fid)
 {
-       struct osd_thread_info  *oti   = osd_oti_get(env);
-       struct osd_idmap_cache  *idc;
+       struct osd_thread_info *oti   = osd_oti_get(env);
+       struct osd_idmap_cache *idc;
        int i;
 
        if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
                i = oti->oti_ins_cache_size * 2;
                if (i == 0)
                        i = OSD_INS_CACHE_SIZE;
-               OBD_ALLOC(idc, sizeof(*idc) * i);
+               OBD_ALLOC_PTR_ARRAY(idc, i);
                if (idc == NULL)
                        return ERR_PTR(-ENOMEM);
                if (oti->oti_ins_cache != NULL) {
                        memcpy(idc, oti->oti_ins_cache,
                               oti->oti_ins_cache_used * sizeof(*idc));
-                       OBD_FREE(oti->oti_ins_cache,
-                                oti->oti_ins_cache_used * sizeof(*idc));
+                       OBD_FREE_PTR_ARRAY(oti->oti_ins_cache,
+                                          oti->oti_ins_cache_used);
                }
                oti->oti_ins_cache = idc;
                oti->oti_ins_cache_size = i;
@@ -248,10 +252,16 @@ osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
        if (idc != NULL)
                return idc;
 
+       CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
+              osd->od_svname, PFID(fid));
+
        /* new mapping is needed */
        idc = osd_idc_add(env, osd, fid);
-       if (IS_ERR(idc))
+       if (IS_ERR(idc)) {
+               CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
+                      osd->od_svname, PFID(fid), PTR_ERR(idc));
                return idc;
+       }
 
        /* initialize it */
        rc = osd_remote_fid(env, osd, fid);
@@ -283,8 +293,8 @@ static int osd_idc_find_and_init(const struct lu_env *env,
                                 struct osd_device *osd,
                                 struct osd_object *obj)
 {
-       const struct lu_fid     *fid = lu_object_fid(&obj->oo_dt.do_lu);
-       struct osd_idmap_cache  *idc;
+       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
+       struct osd_idmap_cache *idc;
 
        idc = osd_idc_find(env, osd, fid);
        LASSERT(!IS_ERR(idc));
@@ -299,10 +309,16 @@ static int osd_idc_find_and_init(const struct lu_env *env,
                return 0;
        }
 
+       CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
+              osd->od_svname, PFID(fid));
+
        /* new mapping is needed */
        idc = osd_idc_add(env, osd, fid);
-       if (IS_ERR(idc))
+       if (IS_ERR(idc)) {
+               CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
+                      osd->od_svname, PFID(fid), PTR_ERR(idc));
                return PTR_ERR(idc);
+       }
 
        if (obj->oo_inode != NULL) {
                idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
@@ -320,14 +336,14 @@ static int osd_idc_find_and_init(const struct lu_env *env,
  * life-cycle.
  */
 static struct lu_object *osd_object_alloc(const struct lu_env *env,
-                                          const struct lu_object_header *hdr,
-                                          struct lu_device *d)
+                                         const struct lu_object_header *hdr,
+                                         struct lu_device *d)
 {
-        struct osd_object *mo;
+       struct osd_object *mo;
 
-        OBD_ALLOC_PTR(mo);
-        if (mo != NULL) {
-                struct lu_object *l;
+       OBD_ALLOC_PTR(mo);
+       if (mo != NULL) {
+               struct lu_object *l;
                struct lu_object_header *h;
                struct osd_device *o = osd_dev(d);
 
@@ -354,10 +370,9 @@ static struct lu_object *osd_object_alloc(const struct lu_env *env,
                init_rwsem(&mo->oo_ext_idx_sem);
                spin_lock_init(&mo->oo_guard);
                INIT_LIST_HEAD(&mo->oo_xattr_list);
-                return l;
-        } else {
-                return NULL;
-        }
+               return l;
+       }
+       return NULL;
 }
 
 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
@@ -400,12 +415,14 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
        int rc;
        struct inode *inode = NULL;
 
-       /* if we look for an inode withing a running
-        * transaction, then we risk to deadlock */
-       /* osd_dirent_check_repair() breaks this */
-       /*LASSERT(current->journal_info == NULL);*/
+       /*
+        * if we look for an inode withing a running
+        * transaction, then we risk to deadlock
+        * osd_dirent_check_repair() breaks this
+        */
+        /* LASSERT(current->journal_info == NULL); */
 
-       inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
+       inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
        if (IS_ERR(inode)) {
                CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
                       id->oii_ino, PTR_ERR(inode));
@@ -417,14 +434,16 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
                iput(inode);
                inode = ERR_PTR(-ESTALE);
        } else if (inode->i_nlink == 0) {
-               /* due to parallel readdir and unlink,
-               * we can have dead inode here. */
+               /*
+                * due to parallel readdir and unlink,
+                * we can have dead inode here.
+                */
                CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
                iput(inode);
                inode = ERR_PTR(-ESTALE);
        } else if (is_bad_inode(inode)) {
                CWARN("%s: bad inode: ino = %u\n",
-               osd_dev2name(dev), id->oii_ino);
+                     osd_dev2name(dev), id->oii_ino);
                iput(inode);
                inode = ERR_PTR(-ENOENT);
        } else if ((rc = osd_attach_jinode(inode))) {
@@ -435,11 +454,13 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
                if (id->oii_gen == OSD_OII_NOGEN)
                        osd_id_gen(id, inode->i_ino, inode->i_generation);
 
-               /* Do not update file c/mtime in ldiskfs.
+               /*
+                * Do not update file c/mtime in ldiskfs.
                 * NB: we don't have any lock to protect this because we don't
                 * have reference on osd_object now, but contention with
                 * another lookup + attr_set can't happen in the tiny window
-                * between if (...) and set S_NOCMTIME. */
+                * between if (...) and set S_NOCMTIME.
+                */
                if (!(inode->i_flags & S_NOCMTIME))
                        inode->i_flags |= S_NOCMTIME;
        }
@@ -457,6 +478,7 @@ int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
                struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
                struct inode *parent = child->d_parent->d_inode;
                struct lu_fid *fid = NULL;
+               char fidstr[FID_LEN + 1] = "unknown";
 
                rc2 = osd_get_lma(info, parent, child->d_parent, loa);
                if (!rc2) {
@@ -473,19 +495,18 @@ int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
                }
 
                if (fid != NULL)
-                       CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
-                             "maximum entry limit\n",
-                             osd_name(osd), parent->i_ino, PFID(fid),
-                             rc == -ENOSPC ? "has reached" : "is approaching");
-               else
-                       CWARN("%s: directory (inode: %lu, FID: unknown) %s "
-                             "maximum entry limit\n",
-                             osd_name(osd), parent->i_ino,
-                             rc == -ENOSPC ? "has reached" : "is approaching");
+                       snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
+
+               /* below message is checked in sanity.sh test_129 */
+               if (rc == -ENOSPC) {
+                       CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
+                             osd_name(osd), parent->i_ino, fidstr);
+               } else {
+                       rc = 0; /* ignore such error now */
+                       CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
+                             osd_name(osd), parent->i_ino, fidstr);
+               }
 
-               /* ignore such error now */
-               if (rc == -ENOBUFS)
-                       rc = 0;
        }
 
        return rc;
@@ -527,14 +548,17 @@ static struct inode *osd_iget_check(struct osd_thread_info *info,
 {
        struct inode *inode;
        int rc = 0;
+
        ENTRY;
 
-       /* The cached OI mapping is trustable. If we cannot locate the inode
+       /*
+        * The cached OI mapping is trustable. If we cannot locate the inode
         * via the cached OI mapping, then return the failure to the caller
-        * directly without further OI checking. */
+        * directly without further OI checking.
+        */
 
 again:
-       inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
+       inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
        if (IS_ERR(inode)) {
                rc = PTR_ERR(inode);
                if (!trusted && (rc == -ENOENT || rc == -ESTALE))
@@ -588,7 +612,8 @@ check_oi:
                LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
 
                rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
-               /* XXX: There are four possible cases:
+               /*
+                * XXX: There are four possible cases:
                 *      1. rc = 0.
                 *         Backup/restore caused the OI invalid.
                 *      2. rc = 0.
@@ -607,16 +632,21 @@ check_oi:
                 *      if the OI files are consistent but may contain
                 *      stale OI mappings because of case 2, if iget()
                 *      returns -ENOENT or -ESTALE, then it should be
-                *      the case 2. */
+                *      the case 2.
+                */
                if (rc != 0)
-                       /* If the OI mapping was in OI file before the
+                       /*
+                        * If the OI mapping was in OI file before the
                         * osd_iget_check(), but now, it is disappear,
                         * then it must be removed by race. That is a
-                        * normal race case. */
+                        * normal race case.
+                        */
                        GOTO(put, rc);
 
-               /* It is the OI scrub updated the OI mapping by race.
-                * The new OI mapping must be valid. */
+               /*
+                * It is the OI scrub updated the OI mapping by race.
+                * The new OI mapping must be valid.
+                */
                if (saved_ino != id->oii_ino ||
                    (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
                        if (!IS_ERR(inode))
@@ -629,10 +659,12 @@ check_oi:
                if (IS_ERR(inode)) {
                        if (dev->od_scrub.os_scrub.os_file.sf_flags &
                            SF_INCONSISTENT)
-                               /* It still can be the case 2, but we cannot
+                               /*
+                                * It still can be the case 2, but we cannot
                                 * distinguish it from the case 1. So return
                                 * -EREMCHG to block current operation until
-                                *  OI scrub rebuilt the OI mappings. */
+                                *  OI scrub rebuilt the OI mappings.
+                                */
                                rc = -EREMCHG;
                        else
                                rc = -ENOENT;
@@ -648,11 +680,13 @@ check_oi:
                if (id->oii_gen == OSD_OII_NOGEN)
                        osd_id_gen(id, inode->i_ino, inode->i_generation);
 
-               /* Do not update file c/mtime in ldiskfs.
+               /*
+                * Do not update file c/mtime in ldiskfs.
                 * NB: we don't have any lock to protect this because we don't
                 * have reference on osd_object now, but contention with
                 * another lookup + attr_set can't happen in the tiny window
-                * between if (...) and set S_NOCMTIME. */
+                * between if (...) and set S_NOCMTIME.
+                */
                if (!(inode->i_flags & S_NOCMTIME))
                        inode->i_flags |= S_NOCMTIME;
        }
@@ -671,29 +705,30 @@ put:
 }
 
 /**
- * \retval +v: new filter_fid, does not contain self-fid
- * \retval 0:  filter_fid_old, contains self-fid
+ * \retval +v: new filter_fid does not contain self-fid
+ * \retval 0:  filter_fid_18_23, contains self-fid
  * \retval -v: other failure cases
  */
 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
                 struct dentry *dentry, struct lu_fid *fid)
 {
-       struct filter_fid_old   *ff     = &info->oti_ff;
-       struct ost_id           *ostid  = &info->oti_ostid;
-       int                      rc;
+       struct filter_fid *ff = &info->oti_ff;
+       struct ost_id *ostid = &info->oti_ostid;
+       int rc;
 
        rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
-       if (rc == sizeof(*ff)) {
-               rc = 0;
-               ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
-               rc = ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
+       if (rc == sizeof(struct filter_fid_18_23)) {
+               struct filter_fid_18_23 *ff_old = (void *)ff;
+
+               ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
+               rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
                /*
                 * XXX: use 0 as the index for compatibility, the caller will
-                *      handle index related issues when necessary.
+                * handle index related issues when necessary.
                 */
                if (!rc)
                        ostid_to_fid(fid, ostid, 0);
-       } else if (rc == sizeof(struct filter_fid)) {
+       } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
                rc = 1;
        } else if (rc >= 0) {
                rc = -EINVAL;
@@ -707,7 +742,7 @@ static int osd_lma_self_repair(struct osd_thread_info *info,
                               const struct lu_fid *fid, __u32 compat)
 {
        handle_t *jh;
-       int       rc;
+       int rc;
 
        LASSERT(current->journal_info == NULL);
 
@@ -730,15 +765,16 @@ static int osd_lma_self_repair(struct osd_thread_info *info,
 
 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
 {
-       struct osd_thread_info  *info   = osd_oti_get(env);
-       struct osd_device       *osd    = osd_obj2dev(obj);
-       struct lustre_ost_attrs *loa    = &info->oti_ost_attrs;
-       struct lustre_mdt_attrs *lma    = &loa->loa_lma;
-       struct inode            *inode  = obj->oo_inode;
-       struct dentry           *dentry = &info->oti_obj_dentry;
-       struct lu_fid           *fid    = NULL;
-       const struct lu_fid     *rfid   = lu_object_fid(&obj->oo_dt.do_lu);
-       int                      rc;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct osd_device *osd = osd_obj2dev(obj);
+       struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
+       struct lustre_mdt_attrs *lma = &loa->loa_lma;
+       struct inode *inode = obj->oo_inode;
+       struct dentry *dentry = &info->oti_obj_dentry;
+       struct lu_fid *fid = NULL;
+       const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
+       int rc;
+
        ENTRY;
 
        rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
@@ -746,12 +782,14 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
        if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
                fid = &lma->lma_self_fid;
                rc = osd_get_idif(info, inode, dentry, fid);
-               if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
-                       /* For the given OST-object, if it has neither LMA nor
+               if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
+                       /*
+                        * For the given OST-object, if it has neither LMA nor
                         * FID in XATTR_NAME_FID, then the given FID (which is
                         * contained in the @obj, from client RPC for locating
                         * the OST-object) is trusted. We use it to generate
-                        * the LMA. */
+                        * the LMA.
+                        */
                        osd_lma_self_repair(info, osd, inode, rfid,
                                            LMAC_FID_ON_OST);
                        RETURN(0);
@@ -789,10 +827,12 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
                        struct lu_fid   *fid1 = &info->oti_fid3;
                        __u32            idx  = fid_idif_ost_idx(rfid);
 
-                       /* For old IDIF, the OST index is not part of the IDIF,
+                       /*
+                        * For old IDIF, the OST index is not part of the IDIF,
                         * Means that different OSTs may have the same IDIFs.
                         * Under such case, we need to make some compatible
-                        * check to make sure to trigger OI scrub properly. */
+                        * check to make sure to trigger OI scrub properly.
+                        */
                        if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
                                /* Given @rfid is new, LMA is old. */
                                fid_to_ostid(fid, oi);
@@ -814,19 +854,17 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
 }
 
 struct osd_check_lmv_buf {
-#ifdef HAVE_DIR_CONTEXT
        /* please keep it as first member */
-       struct dir_context       ctx;
-#endif
-       struct osd_thread_info  *oclb_info;
-       struct osd_device       *oclb_dev;
-       struct osd_idmap_cache  *oclb_oic;
-       int                      oclb_items;
-       bool                     oclb_found;
+       struct dir_context ctx;
+       struct osd_thread_info *oclb_info;
+       struct osd_device *oclb_dev;
+       struct osd_idmap_cache *oclb_oic;
+       int oclb_items;
+       bool oclb_found;
 };
 
 /**
- * It is called internally by ->readdir() to filter out the
+ * It is called internally by ->iterate*() to filter out the
  * local slave object's FID of the striped directory.
  *
  * \retval     1 found the local slave's FID
@@ -839,7 +877,7 @@ static int osd_stripe_dir_filldir(struct dir_context *buf,
 static int osd_stripe_dir_filldir(void *buf,
 #endif
                                  const char *name, int namelen,
-                                 loff_t offset, __u64 ino, unsigned d_type)
+                                 loff_t offset, __u64 ino, unsigned int d_type)
 {
        struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
        struct osd_thread_info *oti = oclb->oclb_info;
@@ -878,7 +916,8 @@ static int osd_stripe_dir_filldir(void *buf,
        return 1;
 }
 
-/* When lookup item under striped directory, we need to locate the master
+/*
+ * When lookup item under striped directory, we need to locate the master
  * MDT-object of the striped directory firstly, then the client will send
  * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
  * and the item's name. If the system is restored from MDT file level backup,
@@ -911,25 +950,24 @@ static int osd_stripe_dir_filldir(void *buf,
  *    slave MDT-objects information are stored as sub-directories with the name
  *    "${FID}:${index}". Then when find the local slave MDT-object, its OI
  *    mapping will be recorded. Then subsequent osd_fid_lookup() will know
- *    the correct OI mapping for the slave MDT-object. */
+ *    the correct OI mapping for the slave MDT-object.
+ */
 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
                         struct inode *inode, struct osd_idmap_cache *oic)
 {
        struct lu_buf *buf = &oti->oti_big_buf;
        struct dentry *dentry = &oti->oti_obj_dentry;
-       struct file *filp = &oti->oti_file;
-       const struct file_operations *fops;
+       struct file *filp;
        struct lmv_mds_md_v1 *lmv1;
        struct osd_check_lmv_buf oclb = {
-#ifdef HAVE_DIR_CONTEXT
                .ctx.actor = osd_stripe_dir_filldir,
-#endif
                .oclb_info = oti,
                .oclb_dev = dev,
                .oclb_oic = oic,
                .oclb_found = false,
        };
        int rc = 0;
+
        ENTRY;
 
 again:
@@ -964,29 +1002,17 @@ again:
        if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
                GOTO(out, rc = 0);
 
-       fops = inode->i_fop;
-       dentry->d_inode = inode;
-       dentry->d_sb = inode->i_sb;
-       filp->f_pos = 0;
-       filp->f_path.dentry = dentry;
-       filp->f_mode = FMODE_64BITHASH;
-       filp->f_mapping = inode->i_mapping;
-       filp->f_op = fops;
-       filp->private_data = NULL;
-       set_file_inode(filp, inode);
+       filp = osd_quasi_file(oti->oti_env, inode);
+       rc = osd_security_file_alloc(filp);
+       if (rc)
+               goto out;
 
        do {
                oclb.oclb_items = 0;
-#ifdef HAVE_DIR_CONTEXT
-               oclb.ctx.pos = filp->f_pos;
-               rc = fops->iterate(filp, &oclb.ctx);
-               filp->f_pos = oclb.ctx.pos;
-#else
-               rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
-#endif
+               rc = iterate_dir(filp, &oclb.ctx);
        } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
                 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
-       fops->release(inode, filp);
+       inode->i_fop->release(inode, filp);
 
 out:
        if (rc < 0)
@@ -1021,6 +1047,7 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
        bool trusted = true;
        bool updated = false;
        bool checked = false;
+
        ENTRY;
 
        LINVRNT(osd_invariant(obj));
@@ -1037,11 +1064,13 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
        if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
                RETURN(-ENOENT);
 
-       /* For the object is created as locking anchor, or for the object to
+       /*
+        * For the object is created as locking anchor, or for the object to
         * be created on disk. No need to osd_oi_lookup() at here because FID
         * shouldn't never be re-used, if it's really a duplicate FID from
         * unexpected reason, we should be able to detect it later by calling
-        * do_create->osd_oi_insert(). */
+        * do_create->osd_oi_insert().
+        */
        if (conf && conf->loc_flags & LOC_F_NEW)
                GOTO(out, result = 0);
 
@@ -1059,8 +1088,10 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
                        goto iget;
        }
 
-       /* The OI mapping in the OI file can be updated by the OI scrub
-        * when we locate the inode via FID. So it may be not trustable. */
+       /*
+        * The OI mapping in the OI file can be updated by the OI scrub
+        * when we locate the inode via FID. So it may be not trustable.
+        */
        trusted = false;
 
        /* Search order: 3. OI files. */
@@ -1068,13 +1099,19 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
        if (result == -ENOENT) {
                if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
                    fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
-                   !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
+                   !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
                                      sf->sf_oi_bitmap))
                        GOTO(out, result = 0);
 
                goto trigger;
        }
 
+       /* -ESTALE is returned if inode of OST object doesn't exist */
+       if (result == -ESTALE &&
+           fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
+               GOTO(out, result = 0);
+       }
+
        if (result)
                GOTO(out, result);
 
@@ -1102,7 +1139,8 @@ iget:
                GOTO(out, result);
 
 trigger:
-       /* We still have chance to get the valid inode: for the
+       /*
+        * We still have chance to get the valid inode: for the
         * object which is referenced by remote name entry, the
         * object on the local MDT will be linked under the dir
         * of "/REMOTE_PARENT_DIR" with its FID string as name.
@@ -1116,8 +1154,9 @@ trigger:
         * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
         * only happened for the RPC from other MDT during the
         * OI scrub, or for the client side RPC with FID only,
-        * such as FID to path, or from old connected client. */
-       if (!remote && !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
+        * such as FID to path, or from old connected client.
+        */
+       if (!remote) {
                rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
                if (!rc1) {
                        remote = true;
@@ -1128,17 +1167,19 @@ trigger:
                }
        }
 
-       if (thread_is_running(&scrub->os_thread)) {
+       if (scrub->os_running) {
                if (scrub->os_partial_scan && !scrub->os_in_join)
                        goto join;
 
-               if (IS_ERR_OR_NULL(inode) || result)
+               osd_add_oi_cache(info, dev, id, fid);
+               if (IS_ERR_OR_NULL(inode) || result) {
+                       osd_oii_insert(dev, oic, result == -ENOENT);
                        GOTO(out, result = -EINPROGRESS);
+               }
 
                LASSERT(remote);
                LASSERT(obj->oo_inode == inode);
 
-               osd_add_oi_cache(info, dev, id, fid);
                osd_oii_insert(dev, oic, true);
                goto found;
        }
@@ -1161,13 +1202,15 @@ join:
        if (rc1 && rc1 != -EALREADY)
                GOTO(out, result = -EREMCHG);
 
-       if (IS_ERR_OR_NULL(inode) || result)
+       osd_add_oi_cache(info, dev, id, fid);
+       if (IS_ERR_OR_NULL(inode) || result) {
+               osd_oii_insert(dev, oic, result == -ENOENT);
                GOTO(out, result = -EINPROGRESS);
+       }
 
        LASSERT(remote);
        LASSERT(obj->oo_inode == inode);
 
-       osd_add_oi_cache(info, dev, id, fid);
        osd_oii_insert(dev, oic, true);
        goto found;
 
@@ -1190,30 +1233,38 @@ check_lma:
        saved_gen = inode->i_generation;
 
        if (unlikely(result == -ENODATA)) {
-               /* If the OI scrub updated the OI mapping by race, it
-                * must be valid. Trust the inode that has no LMA EA. */
+               /*
+                * If the OI scrub updated the OI mapping by race, it
+                * must be valid. Trust the inode that has no LMA EA.
+                */
                if (updated)
                        goto found;
 
                result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
                if (!result) {
-                       /* The OI mapping is still there, the inode is still
-                        * valid. It is just becaues the inode has no LMA EA. */
+                       /*
+                        * The OI mapping is still there, the inode is still
+                        * valid. It is just becaues the inode has no LMA EA.
+                        */
                        if (saved_ino == id->oii_ino &&
                            saved_gen == id->oii_gen)
                                goto found;
 
-                       /* It is the OI scrub updated the OI mapping by race.
-                        * The new OI mapping must be valid. */
+                       /*
+                        * It is the OI scrub updated the OI mapping by race.
+                        * The new OI mapping must be valid.
+                        */
                        trusted = true;
                        updated = true;
                        goto iget;
                }
 
-               /* "result == -ENOENT" means that the OI mappinghas been
+               /*
+                * "result == -ENOENT" means that the OI mappinghas been
                 * removed by race, so the inode belongs to other object.
                 *
-                * Others error can be returned  directly. */
+                * Others error can be returned  directly.
+                */
                if (result == -ENOENT) {
                        LASSERT(trusted);
 
@@ -1227,9 +1278,24 @@ check_lma:
 
        LASSERT(!updated);
 
+       /*
+        * if two OST objects map to the same inode, and inode mode is
+        * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
+        * reserved by precreate, and not written yet, in this case, don't
+        * set inode for the object whose FID mismatch, so that it can create
+        * inode and not block precreate.
+        */
+       if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
+           inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
+               obj->oo_inode = NULL;
+               GOTO(out, result = 0);
+       }
+
        result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
-       /* "result == -ENOENT" means the cached OI mapping has been removed
-        * from the OI file by race, above inode belongs to other object. */
+       /*
+        * "result == -ENOENT" means the cached OI mapping has been removed
+        * from the OI file by race, above inode belongs to other object.
+        */
        if (result == -ENOENT) {
                LASSERT(trusted);
 
@@ -1245,8 +1311,10 @@ check_lma:
                goto trigger;
        }
 
-       /* It is the OI scrub updated the OI mapping by race.
-        * The new OI mapping must be valid. */
+       /*
+        * It is the OI scrub updated the OI mapping by race.
+        * The new OI mapping must be valid.
+        */
        trusted = true;
        updated = true;
        goto iget;
@@ -1307,10 +1375,10 @@ out:
  */
 static void osd_object_init0(struct osd_object *obj)
 {
-        LASSERT(obj->oo_inode != NULL);
-        obj->oo_dt.do_body_ops = &osd_body_ops;
-        obj->oo_dt.do_lu.lo_header->loh_attr |=
-                (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
+       LASSERT(obj->oo_inode != NULL);
+       obj->oo_dt.do_body_ops = &osd_body_ops;
+       obj->oo_dt.do_lu.lo_header->loh_attr |=
+               (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
 }
 
 /*
@@ -1325,6 +1393,13 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
 
        LINVRNT(osd_invariant(obj));
 
+       if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
+           cfs_fail_val == 2) {
+               struct osd_thread_info *info = osd_oti_get(env);
+               struct osd_idmap_cache *oic = &info->oti_cache;
+               /* invalidate thread cache */
+               memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
+       }
        if (fid_is_otable_it(&l->lo_header->loh_fid)) {
                obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
                l->lo_header->loh_attr |= LOHA_EXISTS;
@@ -1344,21 +1419,26 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
                result = osd_get_lma(oti, obj->oo_inode,
                                     &oti->oti_obj_dentry, loa);
                if (!result) {
-                       /* Convert LMAI flags to lustre LMA flags
-                        * and cache it to oo_lma_flags */
+                       /*
+                        * Convert LMAI flags to lustre LMA flags
+                        * and cache it to oo_lma_flags
+                        */
                        obj->oo_lma_flags =
                                lma_to_lustre_flags(loa->loa_lma.lma_incompat);
                } else if (result == -ENODATA) {
                        result = 0;
                }
        }
+       obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
 
        LINVRNT(osd_invariant(obj));
        return result;
 }
 
-/* The first part of oxe_buf is xattr name, and is '\0' terminated.
- * The left part is for value, binary mode. */
+/*
+ * The first part of oxe_buf is xattr name, and is '\0' terminated.
+ * The left part is for value, binary mode.
+ */
 struct osd_xattr_entry {
        struct list_head        oxe_list;
        size_t                  oxe_len;
@@ -1375,7 +1455,6 @@ static int osd_oxc_get(struct osd_object *obj, const char *name,
        struct osd_xattr_entry *oxe = NULL;
        size_t namelen = strlen(name);
        int rc;
-       ENTRY;
 
        rcu_read_lock();
        list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
@@ -1403,7 +1482,6 @@ static int osd_oxc_get(struct osd_object *obj, const char *name,
                GOTO(out, rc = -ERANGE);
 
        memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
-       EXIT;
 out:
        rcu_read_unlock();
 
@@ -1503,11 +1581,10 @@ static void osd_object_free(const struct lu_env *env, struct lu_object *l)
        dt_object_fini(&obj->oo_dt);
        if (obj->oo_hl_head != NULL)
                ldiskfs_htree_lock_head_free(obj->oo_hl_head);
+       /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
        OBD_FREE_PTR(obj);
-       if (unlikely(h)) {
-               lu_object_header_fini(h);
-               OBD_FREE_PTR(h);
-       }
+       if (unlikely(h))
+               lu_object_header_free(h);
 }
 
 /*
@@ -1516,32 +1593,22 @@ static void osd_object_free(const struct lu_env *env, struct lu_object *l)
  */
 static void osd_index_fini(struct osd_object *o)
 {
-        struct iam_container *bag;
-
-        if (o->oo_dir != NULL) {
-                bag = &o->oo_dir->od_container;
-                if (o->oo_inode != NULL) {
-                        if (bag->ic_object == o->oo_inode)
-                                iam_container_fini(bag);
-                }
-                OBD_FREE_PTR(o->oo_dir);
-                o->oo_dir = NULL;
-        }
-}
+       struct iam_container *bag;
 
-/*
- * Concurrency: no concurrent access is possible that late in object
- * life-cycle (for all existing callers, that is. New callers have to provide
- * their own locking.)
- */
-static int osd_inode_unlinked(const struct inode *inode)
-{
-        return inode->i_nlink == 0;
+       if (o->oo_dir != NULL) {
+               bag = &o->oo_dir->od_container;
+               if (o->oo_inode != NULL) {
+                       if (bag->ic_object == o->oo_inode)
+                               iam_container_fini(bag);
+               }
+               OBD_FREE_PTR(o->oo_dir);
+               o->oo_dir = NULL;
+       }
 }
 
 enum {
-        OSD_TXN_OI_DELETE_CREDITS    = 20,
-        OSD_TXN_INODE_DELETE_CREDITS = 20
+       OSD_TXN_OI_DELETE_CREDITS    = 20,
+       OSD_TXN_INODE_DELETE_CREDITS = 20
 };
 
 /*
@@ -1585,9 +1652,9 @@ static void __osd_th_check_slow(void *oth, struct osd_device *dev,
 
        if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
                CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
-                      oth, now, alloced, started, closed);
-                libcfs_debug_dumpstack(NULL);
-        }
+                               oth, now, alloced, started, closed);
+               libcfs_debug_dumpstack(NULL);
+       }
 }
 
 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                              \
@@ -1623,20 +1690,19 @@ static int osd_param_is_not_sane(const struct osd_device *dev,
  * Concurrency: shouldn't matter.
  */
 static void osd_trans_commit_cb(struct super_block *sb,
-                                struct ldiskfs_journal_cb_entry *jcb, int error)
+                               struct ldiskfs_journal_cb_entry *jcb, int error)
 {
-        struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
-        struct thandle     *th  = &oh->ot_super;
-        struct lu_device   *lud = &th->th_dev->dd_lu_dev;
-        struct dt_txn_commit_cb *dcb, *tmp;
+       struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
+       struct thandle *th = &oh->ot_super;
+       struct lu_device *lud = &th->th_dev->dd_lu_dev;
+       struct dt_txn_commit_cb *dcb, *tmp;
 
-        LASSERT(oh->ot_handle == NULL);
-
-        if (error)
-                CERROR("transaction @0x%p commit error: %d\n", th, error);
+       LASSERT(oh->ot_handle == NULL);
 
-        dt_txn_hook_commit(th);
+       if (error)
+               CERROR("transaction @0x%p commit error: %d\n", th, error);
 
+       OBD_FAIL_TIMEOUT(OBD_FAIL_OST_DELAY_TRANS, 40);
        /* call per-transaction callbacks if any */
        list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
                                 dcb_linkage) {
@@ -1648,24 +1714,20 @@ static void osd_trans_commit_cb(struct super_block *sb,
        }
 
        lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
-        lu_device_put(lud);
-        th->th_dev = NULL;
+       lu_device_put(lud);
+       th->th_dev = NULL;
 
        OBD_FREE_PTR(oh);
 }
 
-#ifndef HAVE_SB_START_WRITE
-# define sb_start_write(sb) do {} while (0)
-# define sb_end_write(sb) do {} while (0)
-#endif
-
 static struct thandle *osd_trans_create(const struct lu_env *env,
                                        struct dt_device *d)
 {
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct osd_iobuf        *iobuf = &oti->oti_iobuf;
-       struct osd_thandle      *oh;
-       struct thandle          *th;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_iobuf *iobuf = &oti->oti_iobuf;
+       struct osd_thandle *oh;
+       struct thandle *th;
+
        ENTRY;
 
        if (d->dd_rdonly) {
@@ -1681,37 +1743,41 @@ static struct thandle *osd_trans_create(const struct lu_env *env,
 
        sb_start_write(osd_sb(osd_dt_dev(d)));
 
-       OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
-       if (oh != NULL) {
-               oh->ot_quota_trans = &oti->oti_quota_trans;
-               memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
-               th = &oh->ot_super;
-               th->th_dev = d;
-               th->th_result = 0;
-               oh->ot_credits = 0;
-               INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
-               INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
-               osd_th_alloced(oh);
-
-               memset(oti->oti_declare_ops, 0,
-                      sizeof(oti->oti_declare_ops));
-               memset(oti->oti_declare_ops_cred, 0,
-                      sizeof(oti->oti_declare_ops_cred));
-               memset(oti->oti_declare_ops_used, 0,
-                      sizeof(oti->oti_declare_ops_used));
-       } else {
+       OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
+       if (!oh) {
                sb_end_write(osd_sb(osd_dt_dev(d)));
-               th = ERR_PTR(-ENOMEM);
+               RETURN(ERR_PTR(-ENOMEM));
        }
+
+       oh->ot_quota_trans = &oti->oti_quota_trans;
+       memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
+       th = &oh->ot_super;
+       th->th_dev = d;
+       th->th_result = 0;
+       oh->ot_credits = 0;
+       INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
+       INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
+       INIT_LIST_HEAD(&oh->ot_trunc_locks);
+       osd_th_alloced(oh);
+
+       memset(oti->oti_declare_ops, 0,
+              sizeof(oti->oti_declare_ops));
+       memset(oti->oti_declare_ops_cred, 0,
+              sizeof(oti->oti_declare_ops_cred));
+       memset(oti->oti_declare_ops_used, 0,
+              sizeof(oti->oti_declare_ops_used));
+
+       oti->oti_ins_cache_depth++;
+
        RETURN(th);
 }
 
 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
 {
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct osd_thandle      *oh;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_thandle *oh;
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh != NULL);
 
        CWARN("  create: %u/%u/%u, destroy: %u/%u/%u\n",
@@ -1760,33 +1826,35 @@ void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
                           struct thandle *th)
 {
-        struct osd_thread_info *oti = osd_oti_get(env);
-        struct osd_device  *dev = osd_dt_dev(d);
-        handle_t           *jh;
-        struct osd_thandle *oh;
-        int rc;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_device *dev = osd_dt_dev(d);
+       handle_t *jh;
+       struct osd_thandle *oh;
+       int rc;
 
-        ENTRY;
+       ENTRY;
 
-        LASSERT(current->journal_info == NULL);
+       LASSERT(current->journal_info == NULL);
 
-        oh = container_of0(th, struct osd_thandle, ot_super);
-        LASSERT(oh != NULL);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oh != NULL);
+       LASSERT(oh->ot_handle == NULL);
 
-        rc = dt_txn_hook_start(env, d, th);
-        if (rc != 0)
-                GOTO(out, rc);
+       rc = dt_txn_hook_start(env, d, th);
+       if (rc != 0)
+               GOTO(out, rc);
 
        if (unlikely(osd_param_is_not_sane(dev, th))) {
                static unsigned long last_printed;
                static int last_credits;
 
-               /* don't make noise on a tiny testing systems
-                * actual credits misuse will be caught anyway */
+               /*
+                * don't make noise on a tiny testing systems
+                * actual credits misuse will be caught anyway
+                */
                if (last_credits != oh->ot_credits &&
                    time_after(jiffies, last_printed +
-                              msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
+                              cfs_time_seconds(60)) &&
                    osd_transaction_size(dev) > 512) {
                        CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
                              oh->ot_credits, osd_transaction_size(dev));
@@ -1795,51 +1863,59 @@ static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
                        last_credits = oh->ot_credits;
                        last_printed = jiffies;
                }
-               /* XXX Limit the credits to 'max_transaction_buffers', and
+               /*
+                * XXX Limit the credits to 'max_transaction_buffers', and
                 *     let the underlying filesystem to catch the error if
                 *     we really need so many credits.
                 *
                 *     This should be removed when we can calculate the
-                *     credits precisely. */
+                *     credits precisely.
+                */
                oh->ot_credits = osd_transaction_size(dev);
        } else if (ldiskfs_track_declares_assert != 0) {
-               /* reserve few credits to prevent an assertion in JBD
+               /*
+                * reserve few credits to prevent an assertion in JBD
                 * our debugging mechanism will be able to detected
                 * overuse. this can help to debug single-update
-                * transactions */
+                * transactions
+                */
                oh->ot_credits += 10;
                if (unlikely(osd_param_is_not_sane(dev, th)))
                        oh->ot_credits = osd_transaction_size(dev);
        }
 
-        /*
-         * XXX temporary stuff. Some abstraction layer should
-         * be used.
-         */
-        jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
-        osd_th_started(oh);
-        if (!IS_ERR(jh)) {
-                oh->ot_handle = jh;
-                LASSERT(oti->oti_txns == 0);
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
+               GOTO(out, rc = -EIO);
+
+       /*
+        * XXX temporary stuff. Some abstraction layer should
+        * be used.
+        */
+       jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
+       osd_th_started(oh);
+       if (!IS_ERR(jh)) {
+               oh->ot_handle = jh;
+               LASSERT(oti->oti_txns == 0);
 
-                lu_device_get(&d->dd_lu_dev);
+               lu_device_get(&d->dd_lu_dev);
                lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
                              "osd-tx", th);
-                oti->oti_txns++;
-                rc = 0;
-        } else {
-                rc = PTR_ERR(jh);
-        }
+               oti->oti_txns++;
+               rc = 0;
+       } else {
+               rc = PTR_ERR(jh);
+       }
 out:
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static int osd_seq_exists(const struct lu_env *env,
                          struct osd_device *osd, u64 seq)
 {
-       struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
-       struct seq_server_site  *ss = osd_seq_site(osd);
-       int                     rc;
+       struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
+       struct seq_server_site *ss = osd_seq_site(osd);
+       int rc;
+
        ENTRY;
 
        LASSERT(ss != NULL);
@@ -1858,8 +1934,8 @@ static int osd_seq_exists(const struct lu_env *env,
 
 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
 {
-       struct dt_txn_commit_cb *dcb;
-       struct dt_txn_commit_cb *tmp;
+       struct dt_txn_commit_cb *dcb;
+       struct dt_txn_commit_cb *tmp;
 
        /* call per-transaction stop callbacks if any */
        list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
@@ -1878,39 +1954,45 @@ static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
                          struct thandle *th)
 {
-       int                     rc = 0, remove_agents = 0;
-       struct osd_thandle     *oh;
        struct osd_thread_info *oti = osd_oti_get(env);
-       struct osd_iobuf       *iobuf = &oti->oti_iobuf;
-       struct osd_device      *osd = osd_dt_dev(th->th_dev);
-       struct qsd_instance    *qsd = osd->od_quota_slave;
-       struct lquota_trans    *qtrans;
+       struct osd_thandle *oh;
+       struct osd_iobuf *iobuf = &oti->oti_iobuf;
+       struct osd_device *osd = osd_dt_dev(th->th_dev);
+       struct qsd_instance *qsd = osd_def_qsd(osd);
+       struct lquota_trans *qtrans;
+       LIST_HEAD(truncates);
+       int rc = 0, remove_agents = 0;
+
        ENTRY;
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        remove_agents = oh->ot_remove_agents;
 
        qtrans = oh->ot_quota_trans;
        oh->ot_quota_trans = NULL;
 
+       /* move locks to local list, stop tx, execute truncates */
+       list_splice(&oh->ot_trunc_locks, &truncates);
+
        if (oh->ot_handle != NULL) {
                int rc2;
-                handle_t *hdl = oh->ot_handle;
 
-                /*
-                 * add commit callback
-                 * notice we don't do this in osd_trans_start()
-                 * as underlying transaction can change during truncate
-                 */
-                ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
-                                         &oh->ot_jcb);
+               handle_t *hdl = oh->ot_handle;
 
-                LASSERT(oti->oti_txns == 1);
-                oti->oti_txns--;
+               /*
+                * add commit callback
+                * notice we don't do this in osd_trans_start()
+                * as underlying transaction can change during truncate
+                */
+               ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
+                                            &oh->ot_jcb);
 
-                rc = dt_txn_hook_stop(env, th);
-                if (rc != 0)
+               LASSERT(oti->oti_txns == 1);
+               oti->oti_txns--;
+
+               rc = dt_txn_hook_stop(env, th);
+               if (rc != 0)
                        CERROR("%s: failed in transaction hook: rc = %d\n",
                               osd_name(osd), rc);
 
@@ -1925,15 +2007,20 @@ static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
                               osd_name(osd), rc2);
                if (!rc)
                        rc = rc2;
+
+               osd_process_truncates(&truncates);
        } else {
                osd_trans_stop_cb(oh, th->th_result);
                OBD_FREE_PTR(oh);
        }
 
+       osd_trunc_unlock_all(env, &truncates);
+
        /* inform the quota slave device that the transaction is stopping */
        qsd_op_end(env, qsd, qtrans);
 
-       /* as we want IO to journal and data IO be concurrent, we don't block
+       /*
+        * as we want IO to journal and data IO be concurrent, we don't block
         * awaiting data IO completion in osd_do_bio(), instead we wait here
         * once transaction is submitted to the journal. all reqular requests
         * don't do direct IO (except read/write), thus this wait_event becomes
@@ -1951,8 +2038,10 @@ static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
        if (unlikely(remove_agents != 0))
                osd_process_scheduled_agent_removals(env, osd);
 
+       oti->oti_ins_cache_depth--;
        /* reset OI cache for safety */
-       oti->oti_ins_cache_used = 0;
+       if (oti->oti_ins_cache_depth == 0)
+               oti->oti_ins_cache_used = 0;
 
        sb_end_write(osd_sb(osd));
 
@@ -1961,8 +2050,8 @@ static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
 
 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
 {
-       struct osd_thandle *oh = container_of0(th, struct osd_thandle,
-                                              ot_super);
+       struct osd_thandle *oh = container_of(th, struct osd_thandle,
+                                             ot_super);
 
        LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
        LASSERT(&dcb->dcb_func != NULL);
@@ -1983,108 +2072,129 @@ static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
  */
 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
 {
-        struct osd_object *obj   = osd_obj(l);
-        struct inode      *inode = obj->oo_inode;
+       struct osd_object *obj = osd_obj(l);
+       struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
+       struct inode *inode = obj->oo_inode;
+       __u64 projid;
+       qid_t uid;
+       qid_t gid;
+
+       LINVRNT(osd_invariant(obj));
 
-        LINVRNT(osd_invariant(obj));
+       /*
+        * If object is unlinked remove fid->ino mapping from object index.
+        */
 
-        /*
-         * If object is unlinked remove fid->ino mapping from object index.
-         */
+       osd_index_fini(obj);
 
-        osd_index_fini(obj);
-        if (inode != NULL) {
-               struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
-               qid_t                    uid = i_uid_read(inode);
-               qid_t                    gid = i_gid_read(inode);
+       if (!inode)
+               return;
 
-               obj->oo_inode = NULL;
-               iput(inode);
-               if (!obj->oo_header && qsd) {
-                       struct osd_thread_info  *info = osd_oti_get(env);
-                       struct lquota_id_info   *qi = &info->oti_qi;
+       if (osd_has_index(obj) &&  obj->oo_dt.do_index_ops == &osd_index_iam_ops)
+               ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
 
-                       /* Release granted quota to master if necessary */
-                       qi->lqi_id.qid_uid = uid;
-                       qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
+       uid = i_uid_read(inode);
+       gid = i_gid_read(inode);
+       projid = i_projid_read(inode);
 
-                       qi->lqi_id.qid_uid = gid;
-                       qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
+       obj->oo_inode = NULL;
+       iput(inode);
 
-                       qi->lqi_id.qid_uid = i_projid_read(inode);
-                       qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
-               }
-        }
+       /* do not rebalance quota if the caller needs to release memory
+        * otherwise qsd_refresh_usage() may went into a new ldiskfs
+        * transaction and risk to deadlock - LU-12178 */
+       if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
+               return;
+
+       if (!obj->oo_header && qsd) {
+               struct osd_thread_info *info = osd_oti_get(env);
+               struct lquota_id_info *qi = &info->oti_qi;
+
+               /* Release granted quota to master if necessary */
+               qi->lqi_id.qid_uid = uid;
+               qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
+
+               qi->lqi_id.qid_uid = gid;
+               qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
+
+               qi->lqi_id.qid_uid = projid;
+               qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
+       }
 }
 
 /*
  * Concurrency: ->loo_object_release() is called under site spin-lock.
  */
 static void osd_object_release(const struct lu_env *env,
-                               struct lu_object *l)
+                              struct lu_object *l)
 {
        struct osd_object *o = osd_obj(l);
-       /* nobody should be releasing a non-destroyed object with nlink=0
+
+       /*
+        * nobody should be releasing a non-destroyed object with nlink=0
         * the API allows this, but ldiskfs doesn't like and then report
-        * this inode as deleted */
-       if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
-               LBUG();
+        * this inode as deleted
+        */
+       LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
+                 o->oo_inode->i_nlink == 0));
 }
 
 /*
  * Concurrency: shouldn't matter.
  */
 static int osd_object_print(const struct lu_env *env, void *cookie,
-                            lu_printer_t p, const struct lu_object *l)
+                           lu_printer_t p, const struct lu_object *l)
 {
-        struct osd_object *o = osd_obj(l);
-        struct iam_descr  *d;
+       struct osd_object *o = osd_obj(l);
+       struct iam_descr *d;
 
-        if (o->oo_dir != NULL)
-                d = o->oo_dir->od_container.ic_descr;
-        else
-                d = NULL;
+       if (o->oo_dir != NULL)
+               d = o->oo_dir->od_container.ic_descr;
+       else
+               d = NULL;
        return (*p)(env, cookie,
                    LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
-                    o, o->oo_inode,
-                    o->oo_inode ? o->oo_inode->i_ino : 0UL,
-                    o->oo_inode ? o->oo_inode->i_generation : 0,
-                    d ? d->id_ops->id_name : "plain");
+                   o, o->oo_inode,
+                   o->oo_inode ? o->oo_inode->i_ino : 0UL,
+                   o->oo_inode ? o->oo_inode->i_generation : 0,
+                   d ? d->id_ops->id_name : "plain");
 }
 
 /*
  * Concurrency: shouldn't matter.
  */
 int osd_statfs(const struct lu_env *env, struct dt_device *d,
-               struct obd_statfs *sfs)
+               struct obd_statfs *sfs, struct obd_statfs_info *info)
 {
-       struct osd_device       *osd = osd_dt_dev(d);
-       struct super_block      *sb = osd_sb(osd);
-       struct kstatfs          *ksfs;
-       __u64                    reserved;
-       int                      result = 0;
+       struct osd_device *osd = osd_dt_dev(d);
+       struct super_block *sb = osd_sb(osd);
+       struct kstatfs *ksfs;
+       __u64 reserved;
+       int result = 0;
 
        if (unlikely(osd->od_mnt == NULL))
                return -EINPROGRESS;
 
-        /* osd_lproc.c call this without env, allocate ksfs for that case */
-        if (unlikely(env == NULL)) {
-                OBD_ALLOC_PTR(ksfs);
-                if (ksfs == NULL)
-                        return -ENOMEM;
-        } else {
-                ksfs = &osd_oti_get(env)->oti_ksfs;
-        }
+       /* osd_lproc.c call this without env, allocate ksfs for that case */
+       if (unlikely(env == NULL)) {
+               OBD_ALLOC_PTR(ksfs);
+               if (ksfs == NULL)
+                       return -ENOMEM;
+       } else {
+               ksfs = &osd_oti_get(env)->oti_ksfs;
+       }
 
        result = sb->s_op->statfs(sb->s_root, ksfs);
        if (result)
                goto out;
 
        statfs_pack(sfs, ksfs);
-       if (unlikely(sb->s_flags & MS_RDONLY))
-               sfs->os_state |= OS_STATE_READONLY;
-       if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
-                                        LDISKFS_FEATURE_INCOMPAT_EXTENTS))
+       if (unlikely(sb->s_flags & SB_RDONLY))
+               sfs->os_state |= OS_STATFS_READONLY;
+
+       sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
+
+       if (ldiskfs_has_feature_extents(sb))
                sfs->os_maxbytes = sb->s_maxbytes;
        else
                sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
@@ -2097,7 +2207,7 @@ int osd_statfs(const struct lu_env *env, struct dt_device *d,
         *
         * Reserve 0.78% of total space, at least 8MB for small filesystems.
         */
-       CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
+       BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
        reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
        if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
                reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
@@ -2127,62 +2237,128 @@ out:
  * Concurrency: doesn't access mutable data.
  */
 static void osd_conf_get(const struct lu_env *env,
-                         const struct dt_device *dev,
-                         struct dt_device_param *param)
+                        const struct dt_device *dev,
+                        struct dt_device_param *param)
 {
-        struct super_block *sb = osd_sb(osd_dt_dev(dev));
-       int                ea_overhead;
+       struct osd_device *d = osd_dt_dev(dev);
+       struct super_block *sb = osd_sb(d);
+       struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
+       const char *name;
+       int ea_overhead;
 
-        /*
-         * XXX should be taken from not-yet-existing fs abstraction layer.
-         */
-        param->ddp_max_name_len = LDISKFS_NAME_LEN;
-        param->ddp_max_nlink    = LDISKFS_LINK_MAX;
+       /*
+        * XXX should be taken from not-yet-existing fs abstraction layer.
+        */
+       param->ddp_max_name_len = LDISKFS_NAME_LEN;
+       param->ddp_max_nlink    = LDISKFS_LINK_MAX;
        param->ddp_symlink_max  = sb->s_blocksize;
-       param->ddp_mount_type     = LDD_MT_LDISKFS;
-       if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
+       param->ddp_mount_type   = LDD_MT_LDISKFS;
+       if (ldiskfs_has_feature_extents(sb))
                param->ddp_maxbytes = sb->s_maxbytes;
        else
                param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
-       /* inode are statically allocated, so per-inode space consumption
-        * is the space consumed by the directory entry */
+       /*
+        * inode are statically allocated, so per-inode space consumption
+        * is the space consumed by the directory entry
+        */
        param->ddp_inodespace     = PER_OBJ_USAGE;
-       /* EXT_INIT_MAX_LEN is the theoretical maximum extent size  (32k blocks
-        * = 128MB) which is unlikely to be hit in real life. Report a smaller
-        * maximum length to not under count the actual number of extents
-        * needed for writing a file. */
-       param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
+       /*
+        * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
+        * is 128MB) which is unlikely to be hit in real life. Report a smaller
+        * maximum length to not under-count the actual number of extents
+        * needed for writing a file if there are sub-optimal block allocations.
+        */
+       param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
        /* worst-case extent insertion metadata overhead */
        param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
-       param->ddp_mntopts      = 0;
-        if (test_opt(sb, XATTR_USER))
-                param->ddp_mntopts |= MNTOPT_USERXATTR;
-        if (test_opt(sb, POSIX_ACL))
-                param->ddp_mntopts |= MNTOPT_ACL;
+       param->ddp_mntopts = 0;
+       if (test_opt(sb, XATTR_USER))
+               param->ddp_mntopts |= MNTOPT_USERXATTR;
+       if (test_opt(sb, POSIX_ACL))
+               param->ddp_mntopts |= MNTOPT_ACL;
 
-       /* LOD might calculate the max stripe count based on max_ea_size,
+       /*
+        * LOD might calculate the max stripe count based on max_ea_size,
         * so we need take account in the overhead as well,
-        * xattr_header + magic + xattr_entry_head */
+        * xattr_header + magic + xattr_entry_head
+        */
        ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
                      LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
 
 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
-       if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
+       if (ldiskfs_has_feature_ea_inode(sb))
                param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
                                                                ea_overhead;
        else
 #endif
                param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
 
-       /* Preferred RPC size for efficient disk IO.  4MB shows good
+       if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
+               param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
+
+       /*
+        * Preferred RPC size for efficient disk IO.  4MB shows good
         * all-around performance for ldiskfs, but use bigalloc chunk size
-        * by default if larger. */
+        * by default if larger.
+        */
 #if defined(LDISKFS_CLUSTER_SIZE)
        if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
                param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
        else
 #endif
                param->ddp_brw_size = DT_DEF_BRW_SIZE;
+
+       param->ddp_t10_cksum_type = 0;
+       if (bi) {
+               unsigned short interval = blk_integrity_interval(bi);
+               name = blk_integrity_name(bi);
+               /*
+                * Expected values:
+                * T10-DIF-TYPE1-CRC
+                * T10-DIF-TYPE3-CRC
+                * T10-DIF-TYPE1-IP
+                * T10-DIF-TYPE3-IP
+                */
+               if (strncmp(name, "T10-DIF-TYPE",
+                           sizeof("T10-DIF-TYPE") - 1) == 0) {
+                       /* also skip "1/3-" at end */
+                       const int type_off = sizeof("T10-DIF-TYPE.");
+                       char type_number = name[type_off - 2];
+
+                       if (interval != 512 && interval != 4096) {
+                               CERROR("%s: unsupported T10PI sector size %u\n",
+                                      d->od_svname, interval);
+                       } else if (type_number != '1' && type_number != '3') {
+                               CERROR("%s: unsupported T10PI type %s\n",
+                                      d->od_svname, name);
+                       } else if (strcmp(name + type_off, "CRC") == 0) {
+                               d->od_t10_type = type_number == '1' ?
+                                       OSD_T10_TYPE1_CRC : OSD_T10_TYPE3_CRC;
+                               param->ddp_t10_cksum_type = interval == 512 ?
+                                       OBD_CKSUM_T10CRC512 :
+                                       OBD_CKSUM_T10CRC4K;
+                       } else if (strcmp(name + type_off, "IP") == 0) {
+                               d->od_t10_type = type_number == '1' ?
+                                       OSD_T10_TYPE1_IP : OSD_T10_TYPE3_IP;
+                               param->ddp_t10_cksum_type = interval == 512 ?
+                                       OBD_CKSUM_T10IP512 :
+                                       OBD_CKSUM_T10IP4K;
+                       } else {
+                               CERROR("%s: unsupported checksum type of "
+                                      "T10PI type '%s'",
+                                      d->od_svname, name);
+                       }
+
+               } else {
+                       CERROR("%s: unsupported T10PI type '%s'",
+                              d->od_svname, name);
+               }
+       }
+}
+
+static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
+{
+       return osd_sb(osd_dt_dev(d));
 }
 
 /*
@@ -2191,10 +2367,12 @@ static void osd_conf_get(const struct lu_env *env,
 static int osd_sync(const struct lu_env *env, struct dt_device *d)
 {
        int rc;
+       struct super_block *s = osd_sb(osd_dt_dev(d));
+       ENTRY;
 
-       CDEBUG(D_CACHE, "%s: syncing OSD\n", osd_dt_dev(d)->od_svname);
-
-       rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
+       down_read(&s->s_umount);
+       rc = s->s_op->sync_fs(s, 1);
+       up_read(&s->s_umount);
 
        CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
               rc);
@@ -2215,19 +2393,29 @@ static int osd_sync(const struct lu_env *env, struct dt_device *d)
  * \see dt_device_operations
  */
 static int osd_commit_async(const struct lu_env *env,
-                            struct dt_device *d)
+                           struct dt_device *d)
 {
-        struct super_block *s = osd_sb(osd_dt_dev(d));
-        ENTRY;
+       struct super_block *s = osd_sb(osd_dt_dev(d));
+       int rc;
+
+       ENTRY;
 
        CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
-        RETURN(s->s_op->sync_fs(s, 0));
+       down_read(&s->s_umount);
+       rc = s->s_op->sync_fs(s, 0);
+       up_read(&s->s_umount);
+
+       RETURN(rc);
 }
 
-/* Our own copy of the set readonly functions if present, or NU if not. */
-static int (*priv_dev_set_rdonly)(struct block_device *bdev);
-static int (*priv_dev_check_rdonly)(struct block_device *bdev);
-/* static int (*priv_dev_clear_rdonly)(struct block_device *bdev); */
+static int (*priv_security_file_alloc)(struct file *file);
+
+int osd_security_file_alloc(struct file *file)
+{
+       if (priv_security_file_alloc)
+               return priv_security_file_alloc(file);
+       return 0;
+}
 
 /*
  * Concurrency: shouldn't matter.
@@ -2237,37 +2425,11 @@ static int osd_ro(const struct lu_env *env, struct dt_device *d)
        struct super_block *sb = osd_sb(osd_dt_dev(d));
        struct block_device *dev = sb->s_bdev;
        int rc = -EOPNOTSUPP;
-       ENTRY;
-
-       if (priv_dev_set_rdonly) {
-               struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
-
-               rc = 0;
-               CERROR("*** setting %s read-only ***\n",
-                      osd_dt_dev(d)->od_svname);
 
-               if (sb->s_op->freeze_fs) {
-                       rc = sb->s_op->freeze_fs(sb);
-                       if (rc)
-                               goto out;
-               }
-
-               if (jdev && (jdev != dev)) {
-                       CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
-                              (long)jdev);
-                       priv_dev_set_rdonly(jdev);
-               }
-               CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
-               priv_dev_set_rdonly(dev);
-
-               if (sb->s_op->unfreeze_fs)
-                       sb->s_op->unfreeze_fs(sb);
-       }
+       ENTRY;
 
-out:
-       if (rc)
-               CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
-                      osd_dt_dev(d)->od_svname, (long)dev, rc);
+       CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
+              osd_dt_dev(d)->od_svname, (long)dev, rc);
 
        RETURN(rc);
 }
@@ -2331,20 +2493,21 @@ const int osd_dto_credits_noquota[DTO_NR] = {
 };
 
 static const struct dt_device_operations osd_dt_ops = {
-        .dt_root_get       = osd_root_get,
-        .dt_statfs         = osd_statfs,
-        .dt_trans_create   = osd_trans_create,
-        .dt_trans_start    = osd_trans_start,
-        .dt_trans_stop     = osd_trans_stop,
-        .dt_trans_cb_add   = osd_trans_cb_add,
-        .dt_conf_get       = osd_conf_get,
-        .dt_sync           = osd_sync,
-        .dt_ro             = osd_ro,
-        .dt_commit_async   = osd_commit_async,
+       .dt_root_get       = osd_root_get,
+       .dt_statfs         = osd_statfs,
+       .dt_trans_create   = osd_trans_create,
+       .dt_trans_start    = osd_trans_start,
+       .dt_trans_stop     = osd_trans_stop,
+       .dt_trans_cb_add   = osd_trans_cb_add,
+       .dt_conf_get       = osd_conf_get,
+       .dt_mnt_sb_get     = osd_mnt_sb_get,
+       .dt_sync           = osd_sync,
+       .dt_ro             = osd_ro,
+       .dt_commit_async   = osd_commit_async,
 };
 
 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
-                         unsigned role)
+                         unsigned int role)
 {
        struct osd_object *obj = osd_dt_obj(dt);
        struct osd_thread_info *oti = osd_oti_get(env);
@@ -2359,7 +2522,7 @@ static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
 }
 
 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
-                          unsigned role)
+                          unsigned int role)
 {
        struct osd_object *obj = osd_dt_obj(dt);
        struct osd_thread_info *oti = osd_oti_get(env);
@@ -2409,29 +2572,18 @@ static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
        return obj->oo_owner == env;
 }
 
-static struct timespec *osd_inode_time(const struct lu_env *env,
-                                      struct inode *inode, __u64 seconds)
-{
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct timespec         *t   = &oti->oti_time;
-
-       t->tv_sec = seconds;
-       t->tv_nsec = 0;
-       *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
-       return t;
-}
-
 static void osd_inode_getattr(const struct lu_env *env,
                              struct inode *inode, struct lu_attr *attr)
 {
        attr->la_valid  |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
                           LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
                           LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
-                          LA_BLKSIZE | LA_TYPE;
+                          LA_BLKSIZE | LA_TYPE | LA_BTIME;
 
-       attr->la_atime   = LTIME_S(inode->i_atime);
-       attr->la_mtime   = LTIME_S(inode->i_mtime);
-       attr->la_ctime   = LTIME_S(inode->i_ctime);
+       attr->la_atime = inode->i_atime.tv_sec;
+       attr->la_mtime = inode->i_mtime.tv_sec;
+       attr->la_ctime = inode->i_ctime.tv_sec;
+       attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
        attr->la_mode    = inode->i_mode;
        attr->la_size    = i_size_read(inode);
        attr->la_blocks  = inode->i_blocks;
@@ -2452,11 +2604,71 @@ static void osd_inode_getattr(const struct lu_env *env,
                attr->la_flags |= LUSTRE_PROJINHERIT_FL;
 }
 
-static int osd_attr_get(const struct lu_env *env,
-                       struct dt_object *dt,
+static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
+                           u64 *count)
+{
+       struct osd_object *obj = osd_dt_obj(dt);
+       const struct dt_it_ops *iops;
+       struct dt_it *it;
+       int rc;
+
+       ENTRY;
+
+       LASSERT(S_ISDIR(obj->oo_inode->i_mode));
+       LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
+
+       if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
+               *count = obj->oo_dirent_count;
+               RETURN(0);
+       }
+
+       /* directory not initialized yet */
+       if (!dt->do_index_ops) {
+               *count = 0;
+               RETURN(0);
+       }
+
+       iops = &dt->do_index_ops->dio_it;
+       it = iops->init(env, dt, LUDA_64BITHASH);
+       if (IS_ERR(it))
+               RETURN(PTR_ERR(it));
+
+       rc = iops->load(env, it, 0);
+       if (rc < 0) {
+               if (rc == -ENODATA) {
+                       rc = 0;
+                       *count = 0;
+               }
+               GOTO(out, rc);
+       }
+       if (rc > 0)
+               rc = iops->next(env, it);
+
+       for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
+               if (rc == -ESTALE)
+                       continue;
+
+               if (iops->key_size(env, it) == 0)
+                       continue;
+
+               (*count)++;
+       }
+       if (rc == 1) {
+               obj->oo_dirent_count = *count;
+               rc = 0;
+       }
+out:
+       iops->put(env, it);
+       iops->fini(env, it);
+
+       RETURN(rc);
+}
+
+static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
                        struct lu_attr *attr)
 {
        struct osd_object *obj = osd_dt_obj(dt);
+       int rc = 0;
 
        if (unlikely(!dt_object_exists(dt)))
                return -ENOENT;
@@ -2468,18 +2680,28 @@ static int osd_attr_get(const struct lu_env *env,
 
        spin_lock(&obj->oo_guard);
        osd_inode_getattr(env, obj->oo_inode, attr);
-       if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
+       if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
+               attr->la_valid |= LA_FLAGS;
                attr->la_flags |= LUSTRE_ORPHAN_FL;
+       }
+       if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
+               attr->la_valid |= LA_FLAGS;
+               attr->la_flags |= LUSTRE_ENCRYPT_FL;
+       }
        spin_unlock(&obj->oo_guard);
 
-       return 0;
+       if (S_ISDIR(obj->oo_inode->i_mode) &&
+           fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
+               rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
+
+       return rc;
 }
 
 static int osd_declare_attr_qid(const struct lu_env *env,
                                struct osd_object *obj,
                                struct osd_thandle *oh, long long bspace,
                                qid_t old_id, qid_t new_id, bool enforce,
-                               unsigned type)
+                               unsigned int type, bool ignore_edquot)
 {
        int rc;
        struct osd_thread_info *info = osd_oti_get(env);
@@ -2494,16 +2716,16 @@ static int osd_declare_attr_qid(const struct lu_env *env,
        qi->lqi_space      = 1;
        /* Reserve credits for the new id */
        rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
        if (rc)
                RETURN(rc);
 
        /* and one less inode for the current id */
        qi->lqi_id.qid_uid = old_id;
-       qi->lqi_space      = -1;
+       qi->lqi_space = -1;
        rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
        if (rc)
                RETURN(rc);
@@ -2519,7 +2741,7 @@ static int osd_declare_attr_qid(const struct lu_env *env,
         * to save credit reservation.
         */
        rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
        if (rc)
                RETURN(rc);
@@ -2528,24 +2750,25 @@ static int osd_declare_attr_qid(const struct lu_env *env,
        qi->lqi_id.qid_uid = old_id;
        qi->lqi_space      = -bspace;
        rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
 
        RETURN(rc);
 }
 
 static int osd_declare_attr_set(const struct lu_env *env,
-                                struct dt_object *dt,
-                                const struct lu_attr *attr,
-                                struct thandle *handle)
+                               struct dt_object *dt,
+                               const struct lu_attr *attr,
+                               struct thandle *handle)
 {
-       struct osd_thandle     *oh;
-       struct osd_object      *obj;
-       qid_t                   uid;
-       qid_t                   gid;
-       long long               bspace;
-       int                     rc = 0;
-       bool                    enforce;
+       struct osd_thandle *oh;
+       struct osd_object *obj;
+       qid_t uid;
+       qid_t gid;
+       long long bspace;
+       int rc = 0;
+       bool enforce;
+
        ENTRY;
 
        LASSERT(dt != NULL);
@@ -2554,7 +2777,7 @@ static int osd_declare_attr_set(const struct lu_env *env,
        obj = osd_dt_obj(dt);
        LASSERT(osd_invariant(obj));
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
@@ -2566,30 +2789,42 @@ static int osd_declare_attr_set(const struct lu_env *env,
        if (attr == NULL || obj->oo_inode == NULL)
                RETURN(rc);
 
-       bspace   = obj->oo_inode->i_blocks;
-       bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
+       bspace   = obj->oo_inode->i_blocks << 9;
        bspace   = toqb(bspace);
 
-       /* Changing ownership is always preformed by super user, it should not
-        * fail with EDQUOT.
+       /*
+        * Changing ownership is always preformed by super user, it should not
+        * fail with EDQUOT unless required explicitly.
         *
         * We still need to call the osd_declare_qid() to calculate the journal
         * credits for updating quota accounting files and to trigger quota
-        * space adjustment once the operation is completed.*/
+        * space adjustment once the operation is completed.
+        */
        if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
+               bool ignore_edquot = !(attr->la_flags & LUSTRE_SET_SYNC_FL);
+
+               if (!ignore_edquot)
+                       CDEBUG(D_QUOTA, "%s: enforce quota on UID %u, GID %u"
+                              "(the quota space is %lld)\n",
+                              obj->oo_inode->i_sb->s_id, attr->la_uid,
+                              attr->la_gid, bspace);
+
                /* USERQUOTA */
                uid = i_uid_read(obj->oo_inode);
                enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
                rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
-                                         attr->la_uid, enforce, USRQUOTA);
+                                         attr->la_uid, enforce, USRQUOTA,
+                                         true);
                if (rc)
                        RETURN(rc);
 
                gid = i_gid_read(obj->oo_inode);
+               CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
+                      attr->la_uid, gid, attr->la_gid);
                enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
-               rc = osd_declare_attr_qid(env, obj, oh, bspace,
-                                         i_gid_read(obj->oo_inode),
-                                         attr->la_gid, enforce, GRPQUOTA);
+               rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
+                                         attr->la_gid, enforce, GRPQUOTA,
+                                         ignore_edquot);
                if (rc)
                        RETURN(rc);
 
@@ -2597,11 +2832,12 @@ static int osd_declare_attr_set(const struct lu_env *env,
 #ifdef HAVE_PROJECT_QUOTA
        if (attr->la_valid & LA_PROJID) {
                __u32 projid = i_projid_read(obj->oo_inode);
+
                enforce = (attr->la_valid & LA_PROJID) &&
                                        (attr->la_projid != projid);
                rc = osd_declare_attr_qid(env, obj, oh, bspace,
                                          (qid_t)projid, (qid_t)attr->la_projid,
-                                         enforce, PRJQUOTA);
+                                         enforce, PRJQUOTA, true);
                if (rc)
                        RETURN(rc);
        }
@@ -2622,11 +2858,11 @@ static int osd_inode_setattr(const struct lu_env *env,
                return 0;
 
        if (bits & LA_ATIME)
-               inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
+               inode->i_atime = osd_inode_time(inode, attr->la_atime);
        if (bits & LA_CTIME)
-               inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
+               inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
        if (bits & LA_MTIME)
-               inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
+               inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
        if (bits & LA_SIZE) {
                spin_lock(&inode->i_lock);
                LDISKFS_I(inode)->i_disksize = attr->la_size;
@@ -2634,8 +2870,10 @@ static int osd_inode_setattr(const struct lu_env *env,
                spin_unlock(&inode->i_lock);
        }
 
-       /* OSD should not change "i_blocks" which is used by quota.
-        * "i_blocks" should be changed by ldiskfs only. */
+       /*
+        * OSD should not change "i_blocks" which is used by quota.
+        * "i_blocks" should be changed by ldiskfs only.
+        */
        if (bits & LA_MODE)
                inode->i_mode = (inode->i_mode & S_IFMT) |
                                (attr->la_mode & ~S_IFMT);
@@ -2654,6 +2892,13 @@ static int osd_inode_setattr(const struct lu_env *env,
                /* always keep S_NOCMTIME */
                inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
                                 S_NOCMTIME;
+#if defined(S_ENCRYPTED)
+               /* Always remove S_ENCRYPTED, because ldiskfs must not be
+                * aware of encryption status. It is just stored into LMA
+                * so that it can be forwared to client side.
+                */
+               inode->i_flags &= ~S_ENCRYPTED;
+#endif
                /*
                 * Ext4 did not transfer inherit flags from
                 * @inode->i_flags to raw inode i_flags when writing
@@ -2667,15 +2912,83 @@ static int osd_inode_setattr(const struct lu_env *env,
        return 0;
 }
 
-static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
+#ifdef HAVE_PROJECT_QUOTA
+static int osd_transfer_project(struct inode *inode, __u32 projid,
+                               struct thandle *handle)
+{
+       struct super_block *sb = inode->i_sb;
+       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
+       int err;
+       kprojid_t kprojid;
+       struct ldiskfs_iloc iloc;
+       struct ldiskfs_inode *raw_inode;
+       struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
+
+       if (!ldiskfs_has_feature_project(sb)) {
+               LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
+                       == LDISKFS_DEF_PROJID);
+               if (projid != LDISKFS_DEF_PROJID)
+                       return -EOPNOTSUPP;
+               else
+                       return 0;
+       }
+
+       if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
+               return -EOPNOTSUPP;
+
+       kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
+       if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
+               return 0;
+
+       err = ldiskfs_get_inode_loc(inode, &iloc);
+       if (err)
+               return err;
+
+       raw_inode = ldiskfs_raw_inode(&iloc);
+       if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
+               struct osd_thandle *oh = container_of(handle,
+                                                     struct osd_thandle,
+                                                     ot_super);
+               /**
+                * try to expand inode size automatically.
+                */
+               ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
+               if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
+                       err = -EOVERFLOW;
+                       brelse(iloc.bh);
+                       return err;
+               }
+       }
+       brelse(iloc.bh);
+
+       dquot_initialize(inode);
+       transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
+       if (transfer_to[PRJQUOTA]) {
+               err = __dquot_transfer(inode, transfer_to);
+               dqput(transfer_to[PRJQUOTA]);
+               if (err)
+                       return err;
+       }
+
+       return err;
+}
+#endif
+
+static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
+                             struct thandle *handle)
 {
        int rc;
 
        if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
            (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
-               struct iattr    iattr;
+               struct iattr iattr;
+
+               CDEBUG(D_QUOTA,
+                      "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
+                      inode->i_ino, i_uid_read(inode), attr->la_uid,
+                      i_gid_read(inode), attr->la_gid);
 
-               ll_vfs_dq_init(inode);
+               dquot_initialize(inode);
                iattr.ia_valid = 0;
                if (attr->la_valid & LA_UID)
                        iattr.ia_valid |= ATTR_UID;
@@ -2684,7 +2997,7 @@ static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
                iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
                iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
 
-               rc = ll_vfs_dq_transfer(inode, &iattr);
+               rc = dquot_transfer(inode, &iattr);
                if (rc) {
                        CERROR("%s: quota transfer failed: rc = %d. Is quota "
                               "enforcement enabled on the ldiskfs "
@@ -2697,7 +3010,7 @@ static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
        if (attr->la_valid & LA_PROJID &&
            attr->la_projid != i_projid_read(inode)) {
 #ifdef HAVE_PROJECT_QUOTA
-               rc = ldiskfs_transfer_project(inode, attr->la_projid);
+               rc = osd_transfer_project(inode, attr->la_projid, handle);
 #else
                rc = -ENOTSUPP;
 #endif
@@ -2717,7 +3030,7 @@ static int osd_attr_set(const struct lu_env *env,
                        struct thandle *handle)
 {
        struct osd_object *obj = osd_dt_obj(dt);
-       struct inode      *inode;
+       struct inode *inode;
        int rc;
 
        if (!dt_object_exists(dt))
@@ -2731,14 +3044,14 @@ static int osd_attr_set(const struct lu_env *env,
 
        if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
            !osd_obj2dev(obj)->od_is_ost) {
-               struct osd_thread_info  *oti  = osd_oti_get(env);
-               const struct lu_fid     *fid0 = lu_object_fid(&dt->do_lu);
-               struct lu_fid           *fid1 = &oti->oti_fid;
-               struct osd_inode_id     *id   = &oti->oti_id;
-               struct iam_path_descr   *ipd;
-               struct iam_container    *bag;
-               struct osd_thandle      *oh;
-               int                      rc;
+               struct osd_thread_info *oti = osd_oti_get(env);
+               const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
+               struct lu_fid *fid1 = &oti->oti_fid;
+               struct osd_inode_id *id = &oti->oti_id;
+               struct iam_path_descr *ipd;
+               struct iam_container *bag;
+               struct osd_thandle *oh;
+               int rc;
 
                fid_cpu_to_be(fid1, fid0);
                memset(id, 1, sizeof(*id));
@@ -2748,16 +3061,17 @@ static int osd_attr_set(const struct lu_env *env,
                if (unlikely(ipd == NULL))
                        RETURN(-ENOMEM);
 
-               oh = container_of0(handle, struct osd_thandle, ot_super);
-               rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
+               oh = container_of(handle, struct osd_thandle, ot_super);
+               rc = iam_update(oh->ot_handle, bag,
+                               (const struct iam_key *)fid1,
                                (const struct iam_rec *)id, ipd);
                osd_ipd_put(env, bag, ipd);
                return(rc > 0 ? 0 : rc);
        }
 
-        inode = obj->oo_inode;
+       inode = obj->oo_inode;
 
-       rc = osd_quota_transfer(inode, attr);
+       rc = osd_quota_transfer(inode, attr, handle);
        if (rc)
                return rc;
 
@@ -2767,7 +3081,9 @@ static int osd_attr_set(const struct lu_env *env,
        if (rc != 0)
                GOTO(out, rc);
 
-       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+       osd_dirty_inode(inode, I_DIRTY_DATASYNC);
+
+       osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
 
        if (!(attr->la_valid & LA_FLAGS))
                GOTO(out, rc);
@@ -2787,6 +3103,9 @@ static int osd_attr_set(const struct lu_env *env,
                lma->lma_incompat |=
                        lustre_to_lma_flags(attr->la_flags);
                lustre_lma_swab(lma);
+
+               osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
+
                rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
                                     lma, sizeof(*lma), XATTR_REPLACE);
                if (rc != 0) {
@@ -2802,72 +3121,80 @@ static int osd_attr_set(const struct lu_env *env,
                osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
        }
 out:
-       osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
 
-        return rc;
+       return rc;
 }
 
 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
                                           struct osd_object *obj,
                                           const char *name, const int namelen)
 {
-        return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
+       return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
 }
 
 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
                      umode_t mode, struct dt_allocation_hint *hint,
-                     struct thandle *th)
+                     struct thandle *th, struct lu_attr *attr)
 {
-        int result;
-        struct osd_device  *osd = osd_obj2dev(obj);
-        struct osd_thandle *oth;
-        struct dt_object   *parent = NULL;
-        struct inode       *inode;
+       int result;
+       struct osd_device *osd = osd_obj2dev(obj);
+       struct osd_thandle *oth;
+       struct dt_object *parent = NULL;
+       struct inode *inode;
+       uid_t owner[2] = {0, 0};
 
-        LINVRNT(osd_invariant(obj));
-        LASSERT(obj->oo_inode == NULL);
-        LASSERT(obj->oo_hl_head == NULL);
+       if (attr->la_valid & LA_UID)
+               owner[0] = attr->la_uid;
+       if (attr->la_valid & LA_GID)
+               owner[1] = attr->la_gid;
 
-        if (S_ISDIR(mode) && ldiskfs_pdo) {
-                obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
-                if (obj->oo_hl_head == NULL)
-                        return -ENOMEM;
-        }
+       LINVRNT(osd_invariant(obj));
+       LASSERT(obj->oo_inode == NULL);
+       LASSERT(obj->oo_hl_head == NULL);
+
+       if (S_ISDIR(mode) && ldiskfs_pdo) {
+               obj->oo_hl_head =
+                       ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
+               if (obj->oo_hl_head == NULL)
+                       return -ENOMEM;
+       }
 
-        oth = container_of(th, struct osd_thandle, ot_super);
-        LASSERT(oth->ot_handle->h_transaction != NULL);
+       oth = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oth->ot_handle->h_transaction != NULL);
 
        if (hint != NULL && hint->dah_parent != NULL &&
            !dt_object_remote(hint->dah_parent))
                parent = hint->dah_parent;
 
-        inode = ldiskfs_create_inode(oth->ot_handle,
-                                     parent ? osd_dt_obj(parent)->oo_inode :
-                                              osd_sb(osd)->s_root->d_inode,
-                                     mode);
-        if (!IS_ERR(inode)) {
+       inode = ldiskfs_create_inode(oth->ot_handle,
+                                    parent ? osd_dt_obj(parent)->oo_inode :
+                                             osd_sb(osd)->s_root->d_inode,
+                                    mode, owner);
+       if (!IS_ERR(inode)) {
                /* Do not update file c/mtime in ldiskfs. */
                inode->i_flags |= S_NOCMTIME;
 
-               /* For new created object, it must be consistent,
-                * and it is unnecessary to scrub against it. */
+               /*
+                * For new created object, it must be consistent,
+                * and it is unnecessary to scrub against it.
+                */
                ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
 
-                obj->oo_inode = inode;
-                result = 0;
-        } else {
-                if (obj->oo_hl_head != NULL) {
-                        ldiskfs_htree_lock_head_free(obj->oo_hl_head);
-                        obj->oo_hl_head = NULL;
-                }
-                result = PTR_ERR(inode);
-        }
-        LINVRNT(osd_invariant(obj));
-        return result;
+               obj->oo_inode = inode;
+               result = 0;
+       } else {
+               if (obj->oo_hl_head != NULL) {
+                       ldiskfs_htree_lock_head_free(obj->oo_hl_head);
+                       obj->oo_hl_head = NULL;
+               }
+               result = PTR_ERR(inode);
+       }
+       LINVRNT(osd_invariant(obj));
+       return result;
 }
 
 enum {
-        OSD_NAME_LEN = 255
+       OSD_NAME_LEN = 255
 };
 
 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
@@ -2884,68 +3211,71 @@ static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
 
        oth = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oth->ot_handle->h_transaction != NULL);
-       result = osd_mkfile(info, obj, mode, hint, th);
+       if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
+               obj->oo_dirent_count = 0;
+       result = osd_mkfile(info, obj, mode, hint, th, attr);
 
        return result;
 }
 
 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
-                        struct lu_attr *attr,
-                        struct dt_allocation_hint *hint,
-                        struct dt_object_format *dof,
-                        struct thandle *th)
-{
-        int result;
-        struct osd_thandle *oth;
-        const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
-
-        __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
-
-        LASSERT(S_ISREG(attr->la_mode));
-
-        oth = container_of(th, struct osd_thandle, ot_super);
-        LASSERT(oth->ot_handle->h_transaction != NULL);
-
-        result = osd_mkfile(info, obj, mode, hint, th);
-        if (result == 0) {
-                LASSERT(obj->oo_inode != NULL);
-                if (feat->dif_flags & DT_IND_VARKEY)
-                        result = iam_lvar_create(obj->oo_inode,
-                                                 feat->dif_keysize_max,
-                                                 feat->dif_ptrsize,
-                                                 feat->dif_recsize_max,
-                                                 oth->ot_handle);
-                else
-                        result = iam_lfix_create(obj->oo_inode,
-                                                 feat->dif_keysize_max,
-                                                 feat->dif_ptrsize,
-                                                 feat->dif_recsize_max,
-                                                 oth->ot_handle);
-
-        }
-        return result;
+                       struct lu_attr *attr,
+                       struct dt_allocation_hint *hint,
+                       struct dt_object_format *dof,
+                       struct thandle *th)
+{
+       int result;
+       struct osd_thandle *oth;
+       const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
+
+       __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
+
+       LASSERT(S_ISREG(attr->la_mode));
+
+       oth = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oth->ot_handle->h_transaction != NULL);
+
+       result = osd_mkfile(info, obj, mode, hint, th, attr);
+       if (result == 0) {
+               LASSERT(obj->oo_inode != NULL);
+               if (feat->dif_flags & DT_IND_VARKEY)
+                       result = iam_lvar_create(obj->oo_inode,
+                                                feat->dif_keysize_max,
+                                                feat->dif_ptrsize,
+                                                feat->dif_recsize_max,
+                                                oth->ot_handle);
+               else
+                       result = iam_lfix_create(obj->oo_inode,
+                                                feat->dif_keysize_max,
+                                                feat->dif_ptrsize,
+                                                feat->dif_recsize_max,
+                                                oth->ot_handle);
+       }
+       return result;
 }
 
 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
-                     struct lu_attr *attr,
-                     struct dt_allocation_hint *hint,
-                     struct dt_object_format *dof,
-                     struct thandle *th)
+                    struct lu_attr *attr,
+                    struct dt_allocation_hint *hint,
+                    struct dt_object_format *dof,
+                    struct thandle *th)
 {
-        LASSERT(S_ISREG(attr->la_mode));
-        return osd_mkfile(info, obj, (attr->la_mode &
-                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
+       LASSERT(S_ISREG(attr->la_mode));
+       return osd_mkfile(info, obj, (attr->la_mode &
+                        (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
+                         attr);
 }
 
 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
-                     struct lu_attr *attr,
-                     struct dt_allocation_hint *hint,
-                     struct dt_object_format *dof,
-                     struct thandle *th)
+                    struct lu_attr *attr,
+                    struct dt_allocation_hint *hint,
+                    struct dt_object_format *dof,
+                    struct thandle *th)
 {
-        LASSERT(S_ISLNK(attr->la_mode));
-        return osd_mkfile(info, obj, (attr->la_mode &
-                              (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
+       LASSERT(S_ISLNK(attr->la_mode));
+       return osd_mkfile(info, obj, (attr->la_mode &
+                        (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
+                         attr);
 }
 
 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
@@ -2959,55 +3289,55 @@ static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
 
        LINVRNT(osd_invariant(obj));
        LASSERT(obj->oo_inode == NULL);
-        LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
-                S_ISFIFO(mode) || S_ISSOCK(mode));
+       LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
+               S_ISFIFO(mode) || S_ISSOCK(mode));
 
-        result = osd_mkfile(info, obj, mode, hint, th);
-        if (result == 0) {
-                LASSERT(obj->oo_inode != NULL);
+       result = osd_mkfile(info, obj, mode, hint, th, attr);
+       if (result == 0) {
+               LASSERT(obj->oo_inode != NULL);
                /*
                 * This inode should be marked dirty for i_rdev.  Currently
                 * that is done in the osd_attr_init().
                 */
                init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
                                   attr->la_rdev);
-        }
-        LINVRNT(osd_invariant(obj));
-        return result;
+       }
+       LINVRNT(osd_invariant(obj));
+       return result;
 }
 
 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
-                              struct lu_attr *,
-                              struct dt_allocation_hint *hint,
-                              struct dt_object_format *dof,
-                              struct thandle *);
+                             struct lu_attr *,
+                             struct dt_allocation_hint *hint,
+                             struct dt_object_format *dof,
+                             struct thandle *);
 
 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
 {
-        osd_obj_type_f result;
-
-        switch (type) {
-        case DFT_DIR:
-                result = osd_mkdir;
-                break;
-        case DFT_REGULAR:
-                result = osd_mkreg;
-                break;
-        case DFT_SYM:
-                result = osd_mksym;
-                break;
-        case DFT_NODE:
-                result = osd_mknod;
-                break;
-        case DFT_INDEX:
-                result = osd_mk_index;
-                break;
-
-        default:
-                LBUG();
-                break;
-        }
-        return result;
+       osd_obj_type_f result;
+
+       switch (type) {
+       case DFT_DIR:
+               result = osd_mkdir;
+               break;
+       case DFT_REGULAR:
+               result = osd_mkreg;
+               break;
+       case DFT_SYM:
+               result = osd_mksym;
+               break;
+       case DFT_NODE:
+               result = osd_mknod;
+               break;
+       case DFT_INDEX:
+               result = osd_mk_index;
+               break;
+
+       default:
+               LBUG();
+               break;
+       }
+       return result;
 }
 
 
@@ -3015,37 +3345,39 @@ static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
                        struct dt_object *parent, struct dt_object *child,
                        umode_t child_mode)
 {
-        LASSERT(ah);
+       LASSERT(ah);
 
-        ah->dah_parent = parent;
-        ah->dah_mode = child_mode;
+       ah->dah_parent = parent;
+       ah->dah_mode = child_mode;
 
        if (parent != NULL && !dt_object_remote(parent)) {
                /* will help to find FID->ino at dt_insert("..") */
                struct osd_object *pobj = osd_dt_obj(parent);
+
                osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
        }
 }
 
 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
-                         struct lu_attr *attr, struct dt_object_format *dof)
+                         struct lu_attr *attr, struct dt_object_format *dof,
+                         struct thandle *handle)
 {
-       struct inode   *inode = obj->oo_inode;
-       __u64           valid = attr->la_valid;
-       int             result;
+       struct inode *inode = obj->oo_inode;
+       __u64 valid = attr->la_valid;
+       int result;
 
        attr->la_valid &= ~(LA_TYPE | LA_MODE);
 
-        if (dof->dof_type != DFT_NODE)
-                attr->la_valid &= ~LA_RDEV;
-        if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
-                attr->la_valid &= ~LA_ATIME;
-        if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
-                attr->la_valid &= ~LA_CTIME;
-        if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
-                attr->la_valid &= ~LA_MTIME;
+       if (dof->dof_type != DFT_NODE)
+               attr->la_valid &= ~LA_RDEV;
+       if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
+               attr->la_valid &= ~LA_ATIME;
+       if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
+               attr->la_valid &= ~LA_CTIME;
+       if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
+               attr->la_valid &= ~LA_MTIME;
 
-       result = osd_quota_transfer(inode, attr);
+       result = osd_quota_transfer(inode, attr, handle);
        if (result)
                return;
 
@@ -3059,7 +3391,7 @@ static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
                 * enabled on ldiskfs (lquota takes care of it).
                 */
                LASSERTF(result == 0, "%d\n", result);
-               ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
        }
 
        attr->la_valid = valid;
@@ -3074,8 +3406,8 @@ static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
                        struct lu_attr *attr, struct dt_allocation_hint *hint,
                        struct dt_object_format *dof, struct thandle *th)
 {
-       int     result;
-       __u32   umask;
+       int result;
+       __u32 umask;
 
        osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
 
@@ -3088,13 +3420,15 @@ static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
        if (likely(obj->oo_inode != NULL)) {
                LASSERT(obj->oo_inode->i_state & I_NEW);
 
-               /* Unlock the inode before attr initialization to avoid
-                * unnecessary dqget operations. LU-6378 */
+               /*
+                * Unlock the inode before attr initialization to avoid
+                * unnecessary dqget operations. LU-6378
+                */
                unlock_new_inode(obj->oo_inode);
        }
 
        if (likely(result == 0)) {
-               osd_attr_init(info, obj, attr, dof);
+               osd_attr_init(info, obj, attr, dof, th);
                osd_object_init0(obj);
        }
 
@@ -3118,17 +3452,30 @@ static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
        struct osd_inode_id    *id   = &info->oti_id;
        struct osd_device      *osd  = osd_obj2dev(obj);
        struct osd_thandle     *oh;
-       int                     rc;
+       int rc;
 
        LASSERT(obj->oo_inode != NULL);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle);
        osd_trans_exec_op(env, th, OSD_OT_INSERT);
 
        osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
        rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
                           OI_CHECK_FLD, NULL);
+       if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
+               struct lu_fid next_fid = *fid;
+
+               /* insert next object in advance, and map to the same inode */
+               next_fid.f_oid++;
+               if (next_fid.f_oid != 0) {
+                       osd_trans_exec_op(env, th, OSD_OT_INSERT);
+                       osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
+                                     OI_CHECK_FLD, NULL);
+                       osd_trans_exec_check(env, th, OSD_OT_INSERT);
+               }
+       }
+
        osd_trans_exec_check(env, th, OSD_OT_INSERT);
 
        return rc;
@@ -3137,7 +3484,7 @@ static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
                   u64 seq, struct lu_seq_range *range)
 {
-       struct seq_server_site  *ss = osd_seq_site(osd);
+       struct seq_server_site *ss = osd_seq_site(osd);
 
        if (fid_seq_is_idif(seq)) {
                fld_range_set_ost(range);
@@ -3148,8 +3495,10 @@ int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
        if (!fid_seq_in_fldb(seq)) {
                fld_range_set_mdt(range);
                if (ss != NULL)
-                       /* FIXME: If ss is NULL, it suppose not get lsr_index
-                        * at all */
+                       /*
+                        * FIXME: If ss is NULL, it suppose not get lsr_index
+                        * at all
+                        */
                        range->lsr_index = ss->ss_node_id;
                return 0;
        }
@@ -3168,24 +3517,38 @@ static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
 {
        struct osd_thandle *oh;
        int rc;
+
        ENTRY;
 
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
-       /* EA object consumes more credits than regular object: osd_mk_index
+       /*
+        * EA object consumes more credits than regular object: osd_mk_index
         * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
         * leaf_node, could involves the block, block bitmap, groups, GDT
-        * change for each block, so add 4 * 2 credits in that case. */
+        * change for each block, so add 4 * 2 credits in that case.
+        */
        osd_trans_declare_op(env, oh, OSD_OT_CREATE,
                             osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
                             (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
-       /* Reuse idle OI block may cause additional one OI block
-        * to be changed. */
+       /*
+        * Reuse idle OI block may cause additional one OI block
+        * to be changed.
+        */
        osd_trans_declare_op(env, oh, OSD_OT_INSERT,
                             osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
+       if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
+               osd_trans_declare_op(env, oh, OSD_OT_INSERT,
+                            osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
+
+       /* will help to find FID->ino mapping at dt_insert() */
+       rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
+                                  osd_dt_obj(dt));
+       if (rc != 0)
+               RETURN(rc);
 
        if (!attr)
                RETURN(0);
@@ -3196,10 +3559,6 @@ static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
        if (rc != 0)
                RETURN(rc);
 
-       /* will help to find FID->ino mapping at dt_insert() */
-       rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
-                                  osd_dt_obj(dt));
-
        RETURN(rc);
 }
 
@@ -3211,16 +3570,17 @@ static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
                               struct thandle *th)
 {
-       struct osd_object  *obj = osd_dt_obj(dt);
-       struct inode       *inode = obj->oo_inode;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
        struct osd_thandle *oh;
-       int                 rc;
+       int rc;
+
        ENTRY;
 
        if (inode == NULL)
                RETURN(-ENOENT);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
@@ -3230,8 +3590,10 @@ static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
        if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
                oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
 
-       /* Recycle idle OI leaf may cause additional three OI blocks
-        * to be changed. */
+       /*
+        * Recycle idle OI leaf may cause additional three OI blocks
+        * to be changed.
+        */
        if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
                osd_trans_declare_op(env, oh, OSD_OT_DELETE,
                             osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
@@ -3248,8 +3610,10 @@ static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
        if (rc)
                RETURN(rc);
 
-       /* will help to find FID->ino when this object is being
-        * added to PENDING/ */
+       /*
+        * will help to find FID->ino when this object is being
+        * added to PENDING
+        */
        rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
 
        RETURN(rc);
@@ -3264,9 +3628,10 @@ static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
        struct osd_device *osd = osd_obj2dev(obj);
        struct osd_thandle *oh;
        int result;
+
        ENTRY;
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle);
        LASSERT(inode);
        LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
@@ -3282,13 +3647,15 @@ static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
        }
 
        if (S_ISDIR(inode->i_mode)) {
-               LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
-                       inode->i_nlink == 2);
+               if (inode->i_nlink > 2)
+                       CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
+                              osd_name(osd), PFID(fid), inode->i_ino,
+                              inode->i_nlink);
 
                spin_lock(&obj->oo_guard);
                clear_nlink(inode);
                spin_unlock(&obj->oo_guard);
-               ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
        }
 
        osd_trans_exec_op(env, th, OSD_OT_DESTROY);
@@ -3323,9 +3690,10 @@ static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
                   const struct lu_fid *fid, __u32 compat, __u32 incompat)
 {
-       struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
-       struct lustre_mdt_attrs *lma = &loa->loa_lma;
+       struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
+       struct lustre_mdt_attrs *lma = &loa->loa_lma;
        int rc;
+
        ENTRY;
 
        if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
@@ -3337,10 +3705,12 @@ int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
        lustre_loa_init(loa, fid, compat, incompat);
        lustre_loa_swab(loa, false);
 
-       /* For the OST device with 256 bytes inode size by default,
+       /*
+        * For the OST device with 256 bytes inode size by default,
         * the PFID EA will be stored together with LMA EA to avoid
         * performance trouble. Otherwise the PFID EA can be stored
-        * independently. LU-8998 */
+        * independently. LU-8998
+        */
        if ((compat & LMAC_FID_ON_OST) &&
            LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
                rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
@@ -3348,8 +3718,10 @@ int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
        else
                rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
                                     sizeof(*lma), XATTR_CREATE);
-       /* LMA may already exist, but we need to check that all the
-        * desired compat/incompat flags have been added. */
+       /*
+        * LMA may already exist, but we need to check that all the
+        * desired compat/incompat flags have been added.
+        */
        if (unlikely(rc == -EEXIST)) {
                rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
                                     XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
@@ -3417,7 +3789,8 @@ static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
                          struct osd_inode_id *id)
 {
        struct osd_thread_info *info  = osd_oti_get(env);
-       struct inode           *inode;
+       struct inode *inode;
+
        ENTRY;
 
        osd_id_gen(id, ino, OSD_OII_NOGEN);
@@ -3449,7 +3822,8 @@ static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
 
        rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
                                    dir, dot_ldp, dot_dot_ldp);
-       /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
+       /*
+        * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
         * the subseqent ref_add() will increase the dir->i_nlink
         * as 3. That is incorrect for new created directory.
         *
@@ -3461,7 +3835,8 @@ static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
         * becuase if the nlink attr is not 2, then there will be
         * ref_add() called following the dt_insert(..), such call
         * will make both the in-RAM and on-disk dir-inode's nlink
-        * attr to be set as 2. LU-7447 */
+        * attr to be set as 2. LU-7447
+        */
        set_nlink(dir, saved_nlink);
        return rc;
 }
@@ -3476,34 +3851,51 @@ static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
                                                  __u32 type,
                                                  struct thandle *th)
 {
-       struct osd_thread_info  *info = osd_oti_get(env);
-       struct inode            *local;
-       struct osd_thandle      *oh;
-       int                     rc;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct inode *local;
+       struct osd_thandle *oh;
+       uid_t own[2] = {0, 0};
+       int rc;
+
        ENTRY;
 
        LASSERT(th);
        oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle->h_transaction != NULL);
 
-       local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
+       local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type, own);
        if (IS_ERR(local)) {
                CERROR("%s: create local error %d\n", osd_name(osd),
                       (int)PTR_ERR(local));
                RETURN(local);
        }
 
-       /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
-        * correct gid on remote file, not agent here */
+       /*
+        * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
+        * correct gid on remote file, not agent here
+        */
        local->i_gid = current_fsgid();
        ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
+
+       /* e2fsck doesn't like empty symlinks.  Store remote FID as symlink.
+        * That gives e2fsck something to look at and be happy, and allows
+        * debugging if we need to determine where this symlink came from.
+        */
+       if (S_ISLNK(type)) {
+               BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
+               rc = snprintf((char *)LDISKFS_I(local)->i_data,
+                             LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
+
+               i_size_write(local, rc);
+               LDISKFS_I(local)->i_disksize = rc;
+       }
        unlock_new_inode(local);
 
        /* Agent inode should not have project ID */
 #ifdef HAVE_PROJECT_QUOTA
        if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
            i_projid_read(pobj->oo_inode) != 0) {
-               rc = ldiskfs_transfer_project(local, 0);
+               rc = osd_transfer_project(local, 0, th);
                if (rc) {
                        CERROR("%s: quota transfer failed: rc = %d. Is project "
                               "quota enforcement enabled on the ldiskfs "
@@ -3549,7 +3941,7 @@ static int osd_schedule_agent_inode_removal(const struct lu_env *env,
                                            struct osd_thandle *oh,
                                            __u32 ino)
 {
-       struct osd_device      *osd = osd_dt_dev(oh->ot_super.th_dev);
+       struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
        struct osd_obj_orphan *oor;
 
        OBD_ALLOC_PTR(oor);
@@ -3574,20 +3966,16 @@ static int osd_process_scheduled_agent_removals(const struct lu_env *env,
        struct osd_thread_info *info = osd_oti_get(env);
        struct osd_obj_orphan *oor, *tmp;
        struct osd_inode_id id;
-       struct list_head list;
+       LIST_HEAD(list);
        struct inode *inode;
        struct lu_fid fid;
        handle_t *jh;
        __u32 ino;
 
-       INIT_LIST_HEAD(&list);
-
        spin_lock(&osd->od_osfs_lock);
        list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
-               if (oor->oor_env == env) {
-                       list_del(&oor->oor_list);
-                       list_add(&oor->oor_list, &list);
-               }
+               if (oor->oor_env == env)
+                       list_move(&oor->oor_list, &list);
        }
        spin_unlock(&osd->od_osfs_lock);
 
@@ -3630,10 +4018,10 @@ static int osd_create(const struct lu_env *env, struct dt_object *dt,
                      struct lu_attr *attr, struct dt_allocation_hint *hint,
                      struct dt_object_format *dof, struct thandle *th)
 {
-       const struct lu_fid     *fid    = lu_object_fid(&dt->do_lu);
-       struct osd_object       *obj    = osd_dt_obj(dt);
-       struct osd_thread_info  *info   = osd_oti_get(env);
-       int                      result, on_ost = 0;
+       const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_thread_info *info = osd_oti_get(env);
+       int result, on_ost = 0;
 
        ENTRY;
 
@@ -3646,8 +4034,10 @@ static int osd_create(const struct lu_env *env, struct dt_object *dt,
        LASSERT(th != NULL);
 
        if (unlikely(fid_is_acct(fid)))
-               /* Quota files can't be created from the kernel any more,
-                * 'tune2fs -O quota' will take care of creating them */
+               /*
+                * Quota files can't be created from the kernel any more,
+                * 'tune2fs -O quota' will take care of creating them
+                */
                RETURN(-EPERM);
 
        result = __osd_create(info, obj, attr, hint, dof, th);
@@ -3676,11 +4066,14 @@ static int osd_create(const struct lu_env *env, struct dt_object *dt,
        if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY))
                result = __osd_oi_insert(env, obj, fid, th);
 
-       /* a small optimization - dt_insert() isn't usually applied
+       /*
+        * a small optimization - dt_insert() isn't usually applied
         * to OST objects, so we don't need to cache OI mapping for
-        * OST objects */
+        * OST objects
+        */
        if (result == 0 && on_ost == 0) {
                struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
+
                result = osd_idc_find_and_init(env, osd, obj);
                LASSERT(result == 0);
        }
@@ -3699,7 +4092,7 @@ static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
        /* it's possible that object doesn't exist yet */
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
@@ -3716,10 +4109,10 @@ static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
                       struct thandle *th)
 {
-       struct osd_object  *obj = osd_dt_obj(dt);
-       struct inode       *inode = obj->oo_inode;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
        struct osd_thandle *oh;
-       int                 rc = 0;
+       int rc = 0;
 
        if (!dt_object_exists(dt) || obj->oo_destroyed)
                return -ENOENT;
@@ -3729,7 +4122,7 @@ static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
        LASSERT(osd_is_write_locked(env, obj));
        LASSERT(th != NULL);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle != NULL);
 
        osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
@@ -3758,7 +4151,7 @@ static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
        }
        spin_unlock(&obj->oo_guard);
 
-       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+       osd_dirty_inode(inode, I_DIRTY_DATASYNC);
        LINVRNT(osd_invariant(obj));
 
        osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
@@ -3777,7 +4170,7 @@ static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
        LASSERT(!dt_object_remote(dt));
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
@@ -3792,10 +4185,10 @@ static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
                       struct thandle *th)
 {
-       struct osd_object       *obj = osd_dt_obj(dt);
-       struct inode            *inode = obj->oo_inode;
-       struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
-       struct osd_thandle      *oh;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
+       struct osd_thandle *oh;
 
        if (!dt_object_exists(dt))
                return -ENOENT;
@@ -3805,15 +4198,20 @@ static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
        LASSERT(osd_is_write_locked(env, obj));
        LASSERT(th != NULL);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
+               return -EIO;
+
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle != NULL);
 
        osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
 
        spin_lock(&obj->oo_guard);
-       /* That can be result of upgrade from old Lustre version and
+       /*
+        * That can be result of upgrade from old Lustre version and
         * applied only to local files.  Just skip this ref_del call.
-        * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
+        * ext4_unlink() only treats this as a warning, don't LASSERT here.
+        */
        if (inode->i_nlink == 0) {
                CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
                             D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
@@ -3829,7 +4227,7 @@ static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
        ldiskfs_dec_count(oh->ot_handle, inode);
        spin_unlock(&obj->oo_guard);
 
-       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+       osd_dirty_inode(inode, I_DIRTY_DATASYNC);
        LINVRNT(osd_invariant(obj));
 
        osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
@@ -3847,8 +4245,8 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
        struct inode           *inode  = obj->oo_inode;
        struct osd_thread_info *info   = osd_oti_get(env);
        struct dentry          *dentry = &info->oti_obj_dentry;
-       bool                    cache_xattr = false;
-       int                     rc;
+       bool cache_xattr = false;
+       int rc;
 
        LASSERT(buf);
 
@@ -3856,8 +4254,10 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
        if (strcmp(name, XATTR_NAME_VERSION) == 0) {
                dt_obj_version_t *ver = buf->lb_buf;
 
-               /* for version we are just using xattr API but change inode
-                * field instead */
+               /*
+                * for version we are just using xattr API but change inode
+                * field instead
+                */
                if (buf->lb_len == 0)
                        return sizeof(dt_obj_version_t);
 
@@ -3877,7 +4277,9 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
 
        LASSERT(!dt_object_remote(dt));
        LASSERT(inode->i_op != NULL);
+#ifdef HAVE_IOP_XATTR
        LASSERT(inode->i_op->getxattr != NULL);
+#endif
 
        if (strcmp(name, XATTR_NAME_LOV) == 0 ||
            strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
@@ -3940,9 +4342,9 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
 }
 
 static int osd_declare_xattr_set(const struct lu_env *env,
-                                 struct dt_object *dt,
-                                 const struct lu_buf *buf, const char *name,
-                                 int fl, struct thandle *handle)
+                                struct dt_object *dt,
+                                const struct lu_buf *buf, const char *name,
+                                int fl, struct thandle *handle)
 {
        struct osd_thandle *oh;
        int credits = 0;
@@ -3950,13 +4352,15 @@ static int osd_declare_xattr_set(const struct lu_env *env,
 
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        if (strcmp(name, XATTR_NAME_LMA) == 0) {
-               /* For non-upgrading case, the LMA is set first and
+               /*
+                * For non-upgrading case, the LMA is set first and
                 * usually fit inode. But for upgrade case, the LMA
-                * may be in another separated EA block. */
+                * may be in another separated EA block.
+                */
                if (dt_object_exists(dt)) {
                        if (fl == LU_XATTR_REPLACE)
                                credits = 1;
@@ -3973,12 +4377,14 @@ static int osd_declare_xattr_set(const struct lu_env *env,
                else
                        goto upgrade;
        } else {
-               /* If some name entry resides on remote MDT, then will create
+               /*
+                * If some name entry resides on remote MDT, then will create
                 * agent entry under remote parent. On the other hand, if the
                 * remote entry will be removed, then related agent entry may
                 * need to be removed from the remote parent. So there may be
                 * kinds of cases, let's declare enough credits. The credits
-                * for create agent entry is enough for remove case. */
+                * for create agent entry is enough for remove case.
+                */
                if (strcmp(name, XATTR_NAME_LINK) == 0) {
                        credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
                        if (dt_object_exists(dt))
@@ -3992,8 +4398,10 @@ upgrade:
                        ssize_t buflen;
 
                        if (buf->lb_buf == NULL && dt_object_exists(dt)) {
-                               /* learn xattr size from osd_xattr_get if
-                                  attribute has not been read yet */
+                               /*
+                                * learn xattr size from osd_xattr_get if
+                                * attribute has not been read yet
+                                */
                                buflen = __osd_xattr_get(
                                    osd_dt_obj(dt)->oo_inode,
                                    &osd_oti_get(env)->oti_obj_dentry,
@@ -4036,6 +4444,7 @@ static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
        struct ost_layout *ol = &ff->ff_layout;
        int flags = XATTR_REPLACE;
        int rc;
+
        ENTRY;
 
        if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
@@ -4072,15 +4481,17 @@ static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
                if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
                        RETURN(fl);
 
-               /* Old client does not send stripe information,
-                * then store the PFID EA on disk separatedly. */
+               /*
+                * Old client does not send stripe information,
+                * then store the PFID EA on disk separatedly.
+                */
                if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
                             ol->ol_stripe_size == 0))
                        RETURN(fl);
 
                /* Remove old PFID EA entry firstly. */
-               ll_vfs_dq_init(inode);
-               rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
+               dquot_initialize(inode);
+               rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
                if (rc == -ENODATA) {
                        if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
                                RETURN(rc);
@@ -4157,9 +4568,10 @@ static int osd_xattr_handle_linkea(const struct lu_env *env,
        struct osd_thandle *oh;
        int rc;
        bool remote = false;
+
        ENTRY;
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle != NULL);
 
        rc = linkea_init_with_rec(&ldata);
@@ -4208,6 +4620,7 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
        int fs_flags = 0;
        int len;
        int rc;
+
        ENTRY;
 
        LASSERT(handle);
@@ -4217,17 +4630,21 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
        if (strcmp(name, XATTR_NAME_VERSION) == 0) {
                dt_obj_version_t *version = buf->lb_buf;
 
-               /* for version we are just using xattr API but change inode
-                * field instead */
+               /*
+                * for version we are just using xattr API but change inode
+                * field instead
+                */
                LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
 
                CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
                       *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
 
                LDISKFS_I(inode)->i_fs_version = *version;
-               /* Version is set after all inode operations are finished,
-                * so we should mark it dirty here */
-               ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+               /*
+                * Version is set after all inode operations are finished,
+                * so we should mark it dirty here
+                */
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
 
                RETURN(0);
        }
@@ -4238,10 +4655,12 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
        len = buf->lb_len;
        osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
 
-       /* For the OST device with 256 bytes inode size by default,
+       /*
+        * For the OST device with 256 bytes inode size by default,
         * the PFID EA will be stored together with LMA EA to avoid
         * performance trouble. Otherwise the PFID EA can be stored
-        * independently. LU-8998 */
+        * independently. LU-8998
+        */
        if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
            (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
                LASSERT(buf->lb_buf);
@@ -4294,10 +4713,10 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
                          const struct lu_buf *buf)
 {
-       struct osd_object      *obj    = osd_dt_obj(dt);
-       struct inode           *inode  = obj->oo_inode;
-       struct osd_thread_info *info   = osd_oti_get(env);
-       struct dentry          *dentry = &info->oti_obj_dentry;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct dentry *dentry = &info->oti_obj_dentry;
 
        if (!dt_object_exists(dt))
                return -ENOENT;
@@ -4321,7 +4740,7 @@ static int osd_declare_xattr_del(const struct lu_env *env,
        LASSERT(!dt_object_remote(dt));
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
@@ -4341,19 +4760,21 @@ static int osd_declare_xattr_del(const struct lu_env *env,
 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
                         const char *name, struct thandle *handle)
 {
-       struct osd_object      *obj    = osd_dt_obj(dt);
-       struct inode           *inode  = obj->oo_inode;
-       struct osd_thread_info *info   = osd_oti_get(env);
-       struct dentry          *dentry = &info->oti_obj_dentry;
-       int                     rc;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct dentry *dentry = &info->oti_obj_dentry;
+       int rc;
 
        if (!dt_object_exists(dt))
                return -ENOENT;
 
        LASSERT(!dt_object_remote(dt));
        LASSERT(inode->i_op != NULL);
-       LASSERT(inode->i_op->removexattr != NULL);
        LASSERT(handle != NULL);
+#ifdef HAVE_IOP_XATTR
+       LASSERT(inode->i_op->removexattr != NULL);
+#endif
 
        osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
 
@@ -4373,10 +4794,10 @@ static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
                                obj->oo_pfid_in_lma = 0;
                }
        } else {
-               ll_vfs_dq_init(inode);
+               dquot_initialize(inode);
                dentry->d_inode = inode;
                dentry->d_sb = inode->i_sb;
-               rc = inode->i_op->removexattr(dentry, name);
+               rc = ll_vfs_removexattr(dentry, inode, name);
        }
 
        osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
@@ -4392,23 +4813,14 @@ static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
                           __u64 start, __u64 end)
 {
-       struct osd_object       *obj    = osd_dt_obj(dt);
-       struct inode            *inode  = obj->oo_inode;
-       struct osd_thread_info  *info   = osd_oti_get(env);
-       struct dentry           *dentry = &info->oti_obj_dentry;
-       struct file             *file   = &info->oti_file;
-       int                     rc;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct file *file = osd_quasi_file(env, inode);
+       int rc;
 
        ENTRY;
 
-       dentry->d_inode = inode;
-       dentry->d_sb = inode->i_sb;
-       file->f_path.dentry = dentry;
-       file->f_mapping = inode->i_mapping;
-       file->f_op = inode->i_fop;
-       set_file_inode(file, inode);
-
-       rc = ll_vfs_fsync_range(file, start, end, 0);
+       rc = vfs_fsync_range(file, start, end, 0);
 
        RETURN(rc);
 }
@@ -4423,53 +4835,51 @@ static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
  */
 
 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
-                           const struct dt_index_features *feat)
-{
-        struct iam_descr *descr;
-
-        if (osd_object_is_root(o))
-                return feat == &dt_directory_features;
-
-        LASSERT(o->oo_dir != NULL);
-
-        descr = o->oo_dir->od_container.ic_descr;
-        if (feat == &dt_directory_features) {
-                if (descr->id_rec_size == sizeof(struct osd_fid_pack))
-                        return 1;
-                else
-                        return 0;
-        } else {
-                return
-                        feat->dif_keysize_min <= descr->id_key_size &&
-                        descr->id_key_size <= feat->dif_keysize_max &&
-                        feat->dif_recsize_min <= descr->id_rec_size &&
-                        descr->id_rec_size <= feat->dif_recsize_max &&
-                        !(feat->dif_flags & (DT_IND_VARKEY |
-                                             DT_IND_VARREC | DT_IND_NONUNQ)) &&
-                        ergo(feat->dif_flags & DT_IND_UPDATE,
-                             1 /* XXX check that object (and file system) is
-                                * writable */);
-        }
+                              const struct dt_index_features *feat)
+{
+       struct iam_descr *descr;
+
+       if (osd_object_is_root(o))
+               return feat == &dt_directory_features;
+
+       LASSERT(o->oo_dir != NULL);
+
+       descr = o->oo_dir->od_container.ic_descr;
+       if (feat == &dt_directory_features) {
+               if (descr->id_rec_size == sizeof(struct osd_fid_pack))
+                       return 1;
+               else
+                       return 0;
+       } else {
+               return feat->dif_keysize_min <= descr->id_key_size &&
+                      descr->id_key_size <= feat->dif_keysize_max &&
+                      feat->dif_recsize_min <= descr->id_rec_size &&
+                      descr->id_rec_size <= feat->dif_recsize_max &&
+                      !(feat->dif_flags & (DT_IND_VARKEY |
+                                           DT_IND_VARREC | DT_IND_NONUNQ)) &&
+                      ergo(feat->dif_flags & DT_IND_UPDATE,
+                           1 /* XXX check that object (and fs) is writable */);
+       }
 }
 
 static int osd_iam_container_init(const struct lu_env *env,
-                                  struct osd_object *obj,
-                                  struct osd_directory *dir)
+                                 struct osd_object *obj,
+                                 struct osd_directory *dir)
 {
-        struct iam_container *bag = &dir->od_container;
-        int result;
+       struct iam_container *bag = &dir->od_container;
+       int result;
 
-        result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
-        if (result != 0)
-                return result;
+       result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
+       if (result != 0)
+               return result;
 
-        result = iam_container_setup(bag);
-        if (result == 0)
-                obj->oo_dt.do_index_ops = &osd_index_iam_ops;
-        else
-                iam_container_fini(bag);
+       result = iam_container_setup(bag);
+       if (result == 0)
+               obj->oo_dt.do_index_ops = &osd_index_iam_ops;
+       else
+               iam_container_fini(bag);
 
-        return result;
+       return result;
 }
 
 
@@ -4477,23 +4887,23 @@ static int osd_iam_container_init(const struct lu_env *env,
  * Concurrency: no external locking is necessary.
  */
 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
-                         const struct dt_index_features *feat)
+                        const struct dt_index_features *feat)
 {
-       int                      result;
-       int                      skip_iam = 0;
-       struct osd_object       *obj = osd_dt_obj(dt);
+       int result;
+       int skip_iam = 0;
+       struct osd_object *obj = osd_dt_obj(dt);
 
-        LINVRNT(osd_invariant(obj));
+       LINVRNT(osd_invariant(obj));
 
-        if (osd_object_is_root(obj)) {
-                dt->do_index_ops = &osd_index_ea_ops;
-                result = 0;
+       if (osd_object_is_root(obj)) {
+               dt->do_index_ops = &osd_index_ea_ops;
+               result = 0;
        } else if (feat == &dt_directory_features) {
-                dt->do_index_ops = &osd_index_ea_ops;
+               dt->do_index_ops = &osd_index_ea_ops;
                if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
-                        result = 0;
-                else
-                        result = -ENOTDIR;
+                       result = 0;
+               else
+                       result = -ENOTDIR;
                skip_iam = 1;
        } else if (unlikely(feat == &dt_otable_features)) {
                dt->do_index_ops = &osd_otable_ops;
@@ -4557,20 +4967,20 @@ static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
 
 unlock:
                        up_write(&obj->oo_ext_idx_sem);
-                } else {
-                        result = -ENOMEM;
-                }
-        } else {
-                result = 0;
-        }
+               } else {
+                       result = -ENOMEM;
+               }
+       } else {
+               result = 0;
+       }
 
        if (result == 0 && skip_iam == 0) {
-                if (!osd_iam_index_probe(env, obj, feat))
-                        result = -ENOTDIR;
-        }
-        LINVRNT(osd_invariant(obj));
+               if (!osd_iam_index_probe(env, obj, feat))
+                       result = -ENOTDIR;
+       }
+       LINVRNT(osd_invariant(obj));
 
-        return result;
+       return result;
 }
 
 static int osd_otable_it_attr_get(const struct lu_env *env,
@@ -4616,14 +5026,14 @@ static const struct dt_object_operations osd_obj_otable_it_ops = {
 };
 
 static int osd_index_declare_iam_delete(const struct lu_env *env,
-                                        struct dt_object *dt,
-                                        const struct dt_key *key,
-                                        struct thandle *handle)
+                                       struct dt_object *dt,
+                                       const struct dt_key *key,
+                                       struct thandle *handle)
 {
-        struct osd_thandle    *oh;
+       struct osd_thandle *oh;
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
        /* Recycle  may cause additional three blocks to be changed. */
        osd_trans_declare_op(env, oh, OSD_OT_DELETE,
@@ -4648,11 +5058,12 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
                                struct thandle *handle)
 {
        struct osd_thread_info *oti = osd_oti_get(env);
-       struct osd_object      *obj = osd_dt_obj(dt);
-       struct osd_thandle     *oh;
-       struct iam_path_descr  *ipd;
-       struct iam_container   *bag = &obj->oo_dir->od_container;
-       int                     rc;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_thandle *oh;
+       struct iam_path_descr *ipd;
+       struct iam_container *bag = &obj->oo_dir->od_container;
+       int rc;
+
        ENTRY;
 
        if (!dt_object_exists(dt))
@@ -4665,13 +5076,13 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
 
        osd_trans_exec_op(env, handle, OSD_OT_DELETE);
 
-        ipd = osd_idx_ipd_get(env, bag);
-        if (unlikely(ipd == NULL))
-                RETURN(-ENOMEM);
+       ipd = osd_idx_ipd_get(env, bag);
+       if (unlikely(ipd == NULL))
+               RETURN(-ENOMEM);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle != NULL);
-        LASSERT(oh->ot_handle->h_transaction != NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle != NULL);
+       LASSERT(oh->ot_handle->h_transaction != NULL);
 
        if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
                /* swab quota uid/gid provided by caller */
@@ -4679,11 +5090,11 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
                key = (const struct dt_key *)&oti->oti_quota_id;
        }
 
-        rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
-        osd_ipd_put(env, bag, ipd);
-        LINVRNT(osd_invariant(obj));
+       rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
+       osd_ipd_put(env, bag, ipd);
+       LINVRNT(osd_invariant(obj));
        osd_trans_exec_check(env, handle, OSD_OT_DELETE);
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static int osd_index_declare_ea_delete(const struct lu_env *env,
@@ -4692,14 +5103,15 @@ static int osd_index_declare_ea_delete(const struct lu_env *env,
                                       struct thandle *handle)
 {
        struct osd_thandle *oh;
-       struct inode       *inode;
-       int                 rc, credits;
+       struct inode *inode;
+       int rc, credits;
+
        ENTRY;
 
        LASSERT(!dt_object_remote(dt));
        LASSERT(handle != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
@@ -4716,33 +5128,36 @@ static int osd_index_declare_ea_delete(const struct lu_env *env,
 }
 
 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
-                                          struct dt_rec *fid)
+                                         struct dt_rec *fid)
 {
-        struct osd_fid_pack *rec;
-        int                  rc = -ENODATA;
+       struct osd_fid_pack *rec;
+       int rc = -ENODATA;
 
-        if (de->file_type & LDISKFS_DIRENT_LUFID) {
-                rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
-                rc = osd_fid_unpack((struct lu_fid *)fid, rec);
+       if (de->file_type & LDISKFS_DIRENT_LUFID) {
+               rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
+               rc = osd_fid_unpack((struct lu_fid *)fid, rec);
                if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
                        rc = -EINVAL;
-        }
+       }
        return rc;
 }
 
 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
                          const struct lu_fid *fid)
 {
-       struct seq_server_site  *ss = osd_seq_site(osd);
+       struct seq_server_site *ss = osd_seq_site(osd);
+
        ENTRY;
 
        /* FID seqs not in FLDB, must be local seq */
        if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
                RETURN(0);
 
-       /* If FLD is not being initialized yet, it only happens during the
+       /*
+        * If FLD is not being initialized yet, it only happens during the
         * initialization, likely during mgs initialization, and we assume
-        * this is local FID. */
+        * this is local FID.
+        */
        if (ss == NULL || ss->ss_server_fld == NULL)
                RETURN(0);
 
@@ -4770,8 +5185,10 @@ static void osd_take_care_of_agent(const struct lu_env *env,
                if (IS_ERR(idc) || idc->oic_remote)
                        schedule = 1;
        } else if (rc == -ENODATA) {
-               /* can't get FID, postpone to the end of the
-                * transaction when iget() is safe */
+               /*
+                * can't get FID, postpone to the end of the
+                * transaction when iget() is safe
+                */
                schedule = 1;
        } else {
                CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
@@ -4794,15 +5211,16 @@ static void osd_take_care_of_agent(const struct lu_env *env,
 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
                               const struct dt_key *key, struct thandle *handle)
 {
-       struct osd_object          *obj = osd_dt_obj(dt);
-       struct inode               *dir = obj->oo_inode;
-       struct dentry              *dentry;
-       struct osd_thandle         *oh;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *dir = obj->oo_inode;
+       struct dentry *dentry;
+       struct osd_thandle *oh;
        struct ldiskfs_dir_entry_2 *de = NULL;
-       struct buffer_head         *bh;
-       struct htree_lock          *hlock = NULL;
-       struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
-       int                        rc;
+       struct buffer_head *bh;
+       struct htree_lock *hlock = NULL;
+       struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
+       int rc;
+
        ENTRY;
 
        if (!dt_object_exists(dt))
@@ -4814,25 +5232,26 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
 
        osd_trans_exec_op(env, handle, OSD_OT_DELETE);
 
-        oh = container_of(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle != NULL);
-        LASSERT(oh->ot_handle->h_transaction != NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle != NULL);
+       LASSERT(oh->ot_handle->h_transaction != NULL);
 
-       ll_vfs_dq_init(dir);
-        dentry = osd_child_dentry_get(env, obj,
-                                      (char *)key, strlen((char *)key));
+       dquot_initialize(dir);
+       dentry = osd_child_dentry_get(env, obj,
+                                     (char *)key, strlen((char *)key));
 
-        if (obj->oo_hl_head != NULL) {
-                hlock = osd_oti_get(env)->oti_hlock;
-                ldiskfs_htree_lock(hlock, obj->oo_hl_head,
-                                   dir, LDISKFS_HLOCK_DEL);
-        } else {
+       if (obj->oo_hl_head != NULL) {
+               hlock = osd_oti_get(env)->oti_hlock;
+               ldiskfs_htree_lock(hlock, obj->oo_hl_head,
+                                  dir, LDISKFS_HLOCK_DEL);
+       } else {
                down_write(&obj->oo_ext_idx_sem);
-        }
+       }
 
-        bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
+       bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
        if (!IS_ERR(bh)) {
-               /* If this is not the ".." entry, it might be a remote DNE
+               /*
+                * If this is not the ".." entry, it might be a remote DNE
                 * entry and  we need to check if the FID is for a remote
                 * MDT.  If the FID is  not in the directory entry (e.g.
                 * upgraded 1.8 filesystem without dirdata enabled) then
@@ -4845,7 +5264,8 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
                 * the entry first might leak the agent inode afterward. The
                 * reverse would need filesystem abort in case of error deleting
                 * the entry after the agent had been removed, or leave a
-                * dangling entry pointing at a random inode. */
+                * dangling entry pointing at a random inode.
+                */
                if (strcmp((char *)key, dotdot) != 0)
                        osd_take_care_of_agent(env, osd, oh, de);
                rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
@@ -4853,17 +5273,26 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
        } else {
                rc = PTR_ERR(bh);
        }
-        if (hlock != NULL)
-                ldiskfs_htree_unlock(hlock);
-        else
-               up_write(&obj->oo_ext_idx_sem);
 
+       if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
+           obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
+               /* NB, dirent count may not be accurate, because it's counted
+                * without lock.
+                */
+               if (obj->oo_dirent_count)
+                       obj->oo_dirent_count--;
+               else
+                       obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
+       }
+       if (hlock != NULL)
+               ldiskfs_htree_unlock(hlock);
+       else
+               up_write(&obj->oo_ext_idx_sem);
        GOTO(out, rc);
-
 out:
-        LASSERT(osd_invariant(obj));
+       LASSERT(osd_invariant(obj));
        osd_trans_exec_check(env, handle, OSD_OT_DELETE);
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /**
@@ -4880,13 +5309,14 @@ out:
 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
                                struct dt_rec *rec, const struct dt_key *key)
 {
-       struct osd_object      *obj = osd_dt_obj(dt);
-       struct iam_path_descr  *ipd;
-       struct iam_container   *bag = &obj->oo_dir->od_container;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct iam_path_descr *ipd;
+       struct iam_container *bag = &obj->oo_dir->od_container;
        struct osd_thread_info *oti = osd_oti_get(env);
-       struct iam_iterator    *it = &oti->oti_idx_it;
-       struct iam_rec         *iam_rec;
-       int                     rc;
+       struct iam_iterator *it = &oti->oti_idx_it;
+       struct iam_rec *iam_rec;
+       int rc;
+
        ENTRY;
 
        if (!dt_object_exists(dt))
@@ -4896,12 +5326,12 @@ static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
        LASSERT(!dt_object_remote(dt));
        LASSERT(bag->ic_object == obj->oo_inode);
 
-        ipd = osd_idx_ipd_get(env, bag);
-        if (IS_ERR(ipd))
-                RETURN(-ENOMEM);
+       ipd = osd_idx_ipd_get(env, bag);
+       if (IS_ERR(ipd))
+               RETURN(-ENOMEM);
 
-        /* got ipd now we can start iterator. */
-        iam_it_init(it, bag, 0, ipd);
+       /* got ipd now we can start iterator. */
+       iam_it_init(it, bag, 0, ipd);
 
        if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
                /* swab quota uid/gid provided by caller */
@@ -4909,43 +5339,43 @@ static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
                key = (const struct dt_key *)&oti->oti_quota_id;
        }
 
-        rc = iam_it_get(it, (struct iam_key *)key);
-        if (rc >= 0) {
-                if (S_ISDIR(obj->oo_inode->i_mode))
-                        iam_rec = (struct iam_rec *)oti->oti_ldp;
-                else
-                        iam_rec = (struct iam_rec *) rec;
+       rc = iam_it_get(it, (struct iam_key *)key);
+       if (rc >= 0) {
+               if (S_ISDIR(obj->oo_inode->i_mode))
+                       iam_rec = (struct iam_rec *)oti->oti_ldp;
+               else
+                       iam_rec = (struct iam_rec *)rec;
 
-                iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
+               iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
 
-                if (S_ISDIR(obj->oo_inode->i_mode))
-                        osd_fid_unpack((struct lu_fid *) rec,
-                                       (struct osd_fid_pack *)iam_rec);
+               if (S_ISDIR(obj->oo_inode->i_mode))
+                       osd_fid_unpack((struct lu_fid *)rec,
+                                      (struct osd_fid_pack *)iam_rec);
                else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
                        osd_quota_unpack(obj, rec);
-        }
+       }
 
-        iam_it_put(it);
-        iam_it_fini(it);
-        osd_ipd_put(env, bag, ipd);
+       iam_it_put(it);
+       iam_it_fini(it);
+       osd_ipd_put(env, bag, ipd);
 
-        LINVRNT(osd_invariant(obj));
+       LINVRNT(osd_invariant(obj));
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static int osd_index_declare_iam_insert(const struct lu_env *env,
-                                        struct dt_object *dt,
-                                        const struct dt_rec *rec,
-                                        const struct dt_key *key,
-                                        struct thandle *handle)
+                                       struct dt_object *dt,
+                                       const struct dt_rec *rec,
+                                       const struct dt_key *key,
+                                       struct thandle *handle)
 {
-        struct osd_thandle *oh;
+       struct osd_thandle *oh;
 
-        LASSERT(handle != NULL);
+       LASSERT(handle != NULL);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
        osd_trans_declare_op(env, oh, OSD_OT_INSERT,
                             osd_dto_credits_noquota[DTO_INDEX_INSERT]);
@@ -4966,16 +5396,16 @@ static int osd_index_declare_iam_insert(const struct lu_env *env,
  */
 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
                                const struct dt_rec *rec,
-                               const struct dt_key *key, struct thandle *th,
-                               int ignore_quota)
+                               const struct dt_key *key, struct thandle *th)
 {
-       struct osd_object     *obj = osd_dt_obj(dt);
+       struct osd_object *obj = osd_dt_obj(dt);
        struct iam_path_descr *ipd;
-       struct osd_thandle    *oh;
-       struct iam_container  *bag;
+       struct osd_thandle *oh;
+       struct iam_container *bag;
        struct osd_thread_info *oti = osd_oti_get(env);
-       struct iam_rec         *iam_rec;
-       int                     rc;
+       struct iam_rec *iam_rec;
+       int rc;
+
        ENTRY;
 
        if (!dt_object_exists(dt))
@@ -4990,16 +5420,17 @@ static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
 
        osd_trans_exec_op(env, th, OSD_OT_INSERT);
 
-        ipd = osd_idx_ipd_get(env, bag);
-        if (unlikely(ipd == NULL))
-                RETURN(-ENOMEM);
+       ipd = osd_idx_ipd_get(env, bag);
+       if (unlikely(ipd == NULL))
+               RETURN(-ENOMEM);
 
-        oh = container_of0(th, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle != NULL);
-        LASSERT(oh->ot_handle->h_transaction != NULL);
+       oh = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle != NULL);
+       LASSERT(oh->ot_handle->h_transaction != NULL);
        if (S_ISDIR(obj->oo_inode->i_mode)) {
                iam_rec = (struct iam_rec *)oti->oti_ldp;
-               osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
+               osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
+                            &oti->oti_fid);
        } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
                /* pack quota uid/gid */
                oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
@@ -5011,12 +5442,12 @@ static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
                iam_rec = (struct iam_rec *)rec;
        }
 
-        rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
-                        iam_rec, ipd);
-        osd_ipd_put(env, bag, ipd);
-        LINVRNT(osd_invariant(obj));
+       rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
+                       iam_rec, ipd);
+       osd_ipd_put(env, bag, ipd);
+       LINVRNT(osd_invariant(obj));
        osd_trans_exec_check(env, th, OSD_OT_INSERT);
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /**
@@ -5032,14 +5463,14 @@ static int __osd_ea_add_rec(struct osd_thread_info *info,
                            const char *name, const struct lu_fid *fid,
                            struct htree_lock *hlock, struct thandle *th)
 {
-        struct ldiskfs_dentry_param *ldp;
-        struct dentry               *child;
-        struct osd_thandle          *oth;
-        int                          rc;
+       struct ldiskfs_dentry_param *ldp;
+       struct dentry *child;
+       struct osd_thandle *oth;
+       int rc;
 
-        oth = container_of(th, struct osd_thandle, ot_super);
-        LASSERT(oth->ot_handle != NULL);
-        LASSERT(oth->ot_handle->h_transaction != NULL);
+       oth = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oth->ot_handle != NULL);
+       LASSERT(oth->ot_handle->h_transaction != NULL);
        LASSERT(pobj->oo_inode);
 
        ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
@@ -5050,7 +5481,7 @@ static int __osd_ea_add_rec(struct osd_thread_info *info,
                osd_get_ldiskfs_dirent_param(ldp, fid);
        child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
        child->d_fsdata = (void *)ldp;
-       ll_vfs_dq_init(pobj->oo_inode);
+       dquot_initialize(pobj->oo_inode);
        rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
                                   child, cinode, hlock);
        if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
@@ -5099,22 +5530,22 @@ static int osd_add_dot_dotdot(struct osd_thread_info *info,
                              const struct lu_fid *dot_dot_fid,
                              struct thandle *th)
 {
-        struct inode                *inode = dir->oo_inode;
-        struct osd_thandle          *oth;
-        int result = 0;
+       struct inode *inode = dir->oo_inode;
+       struct osd_thandle *oth;
+       int result = 0;
 
-        oth = container_of(th, struct osd_thandle, ot_super);
-        LASSERT(oth->ot_handle->h_transaction != NULL);
-        LASSERT(S_ISDIR(dir->oo_inode->i_mode));
+       oth = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oth->ot_handle->h_transaction != NULL);
+       LASSERT(S_ISDIR(dir->oo_inode->i_mode));
 
-        if (strcmp(name, dot) == 0) {
-                if (dir->oo_compat_dot_created) {
-                        result = -EEXIST;
-                } else {
+       if (strcmp(name, dot) == 0) {
+               if (dir->oo_compat_dot_created) {
+                       result = -EEXIST;
+               } else {
                        LASSERT(inode->i_ino == parent_dir->i_ino);
-                        dir->oo_compat_dot_created = 1;
-                        result = 0;
-                }
+                       dir->oo_compat_dot_created = 1;
+                       result = 0;
+               }
        } else if (strcmp(name, dotdot) == 0) {
                if (!dir->oo_compat_dot_created)
                        return -EINVAL;
@@ -5153,31 +5584,31 @@ static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
                          struct inode *cinode, const char *name,
                          const struct lu_fid *fid, struct thandle *th)
 {
-        struct osd_thread_info *info   = osd_oti_get(env);
-        struct htree_lock      *hlock;
-        int                     rc;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct htree_lock *hlock;
+       int rc;
 
-        hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
+       hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
 
-        if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
-                                                   name[2] =='\0'))) {
-                if (hlock != NULL) {
-                        ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
-                                           pobj->oo_inode, 0);
-                } else {
+       if (name[0] == '.' && (name[1] == '\0' ||
+                              (name[1] == '.' && name[2] == '\0'))) {
+               if (hlock != NULL) {
+                       ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
+                                          pobj->oo_inode, 0);
+               } else {
                        down_write(&pobj->oo_ext_idx_sem);
                }
 
                rc = osd_add_dot_dotdot(info, pobj, cinode, name,
                                        lu_object_fid(&pobj->oo_dt.do_lu),
-                                        fid, th);
-        } else {
-                if (hlock != NULL) {
-                        ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
-                                           pobj->oo_inode, LDISKFS_HLOCK_ADD);
-                } else {
+                                       fid, th);
+       } else {
+               if (hlock != NULL) {
+                       ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
+                                          pobj->oo_inode, LDISKFS_HLOCK_ADD);
+               } else {
                        down_write(&pobj->oo_ext_idx_sem);
-                }
+               }
 
                if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
                        struct lu_fid *tfid = &info->oti_fid;
@@ -5190,13 +5621,17 @@ static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
                        rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
                                              hlock, th);
                }
-        }
-        if (hlock != NULL)
-                ldiskfs_htree_unlock(hlock);
-        else
+       }
+       if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
+           && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
+               pobj->oo_dirent_count++;
+
+       if (hlock != NULL)
+               ldiskfs_htree_unlock(hlock);
+       else
                up_write(&pobj->oo_ext_idx_sem);
 
-        return rc;
+       return rc;
 }
 
 static int
@@ -5210,19 +5645,18 @@ osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
        int once = 0;
        bool insert;
        int rc;
+
        ENTRY;
 
        if (!fid_is_norm(fid) && !fid_is_igif(fid))
                RETURN(0);
 
-       if (thread_is_running(&scrub->os_thread) &&
-           scrub->os_pos_current > id->oii_ino)
+       if (scrub->os_running && scrub->os_pos_current > id->oii_ino)
                RETURN(0);
 
        if (dev->od_auto_scrub_interval == AS_NEVER ||
-           cfs_time_before(ktime_get_real_seconds(),
-                           scrub->os_file.sf_time_last_complete +
-                           dev->od_auto_scrub_interval))
+           ktime_get_real_seconds() <
+           scrub->os_file.sf_time_last_complete + dev->od_auto_scrub_interval)
                RETURN(0);
 
 again:
@@ -5246,8 +5680,10 @@ again:
                if (gen != OSD_OII_NOGEN)
                        goto trigger;
 
-               /* The inode may has been reused by others, we do not know,
-                * leave it to be handled by subsequent osd_fid_lookup(). */
+               /*
+                * The inode may has been reused by others, we do not know,
+                * leave it to be handled by subsequent osd_fid_lookup().
+                */
                GOTO(out, rc = 0);
        } else if (rc || osd_id_eq(id, &oti->oti_id)) {
                GOTO(out, rc);
@@ -5256,7 +5692,7 @@ again:
        insert = false;
 
 trigger:
-       if (thread_is_running(&scrub->os_thread)) {
+       if (scrub->os_running) {
                if (inode == NULL) {
                        inode = osd_iget(oti, dev, id);
                        /* The inode has been removed (by race maybe). */
@@ -5268,10 +5704,12 @@ trigger:
                }
 
                rc = osd_oii_insert(dev, oic, insert);
-               /* There is race condition between osd_oi_lookup and OI scrub.
+               /*
+                * There is race condition between osd_oi_lookup and OI scrub.
                 * The OI scrub finished just after osd_oi_lookup() failure.
                 * Under such case, it is unnecessary to trigger OI scrub again,
-                * but try to call osd_oi_lookup() again. */
+                * but try to call osd_oi_lookup() again.
+                */
                if (unlikely(rc == -EAGAIN))
                        goto again;
 
@@ -5364,12 +5802,13 @@ static int osd_get_pfid_from_linkea(const struct lu_env *env,
                                    struct osd_object *obj,
                                    struct lu_fid *fid)
 {
-       struct osd_thread_info  *oti    = osd_oti_get(env);
-       struct lu_buf           *buf    = &oti->oti_big_buf;
-       struct dentry           *dentry = &oti->oti_obj_dentry;
-       struct inode            *inode  = obj->oo_inode;
-       struct linkea_data       ldata  = { NULL };
-       int                      rc;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct lu_buf *buf = &oti->oti_big_buf;
+       struct dentry *dentry = &oti->oti_obj_dentry;
+       struct inode *inode = obj->oo_inode;
+       struct linkea_data ldata = { NULL };
+       int rc;
+
        ENTRY;
 
        fid_zero(fid);
@@ -5427,6 +5866,7 @@ static int osd_verify_ent_by_linkea(const struct lu_env *env,
        struct lu_name cname = { .ln_name = name,
                                 .ln_namelen = namelen };
        int rc;
+
        ENTRY;
 
 again:
@@ -5468,14 +5908,15 @@ again:
 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
                             struct dt_rec *rec, const struct dt_key *key)
 {
-       struct inode                    *dir    = obj->oo_inode;
-       struct dentry                   *dentry;
-       struct ldiskfs_dir_entry_2      *de;
-       struct buffer_head              *bh;
-       struct lu_fid                   *fid = (struct lu_fid *) rec;
-       struct htree_lock               *hlock = NULL;
-       int                             ino;
-       int                             rc;
+       struct inode *dir = obj->oo_inode;
+       struct dentry *dentry;
+       struct ldiskfs_dir_entry_2 *de;
+       struct buffer_head *bh;
+       struct lu_fid *fid = (struct lu_fid *)rec;
+       struct htree_lock *hlock = NULL;
+       int ino;
+       int rc;
+
        ENTRY;
 
        LASSERT(dir->i_op != NULL);
@@ -5511,12 +5952,14 @@ static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
                /* done with de, release bh */
                brelse(bh);
                if (rc != 0) {
-                       if (unlikely(ino == osd_remote_parent_ino(dev))) {
+                       if (unlikely(is_remote_parent_ino(dev, ino))) {
                                const char *name = (const char *)key;
 
-                               /* If the parent is on remote MDT, and there
+                               /*
+                                * If the parent is on remote MDT, and there
                                 * is no FID-in-dirent, then we have to get
-                                * the parent FID from the linkEA.  */
+                                * the parent FID from the linkEA.
+                                */
                                if (likely(strlen(name) == 2 &&
                                           name[0] == '.' && name[1] == '.'))
                                        rc = osd_get_pfid_from_linkea(env, obj,
@@ -5561,12 +6004,13 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
                                       const struct dt_key *key,
                                       struct thandle *handle)
 {
-       struct osd_thandle      *oh;
-       struct osd_device       *osd   = osd_dev(dt->do_lu.lo_dev);
-       struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
-       const struct lu_fid     *fid    = rec1->rec_fid;
-       int                      credits, rc = 0;
-       struct osd_idmap_cache  *idc;
+       struct osd_thandle *oh;
+       struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
+       struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
+       const struct lu_fid *fid = rec1->rec_fid;
+       int credits, rc = 0;
+       struct osd_idmap_cache *idc;
+
        ENTRY;
 
        LASSERT(!dt_object_remote(dt));
@@ -5574,21 +6018,25 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
        LASSERT(fid != NULL);
        LASSERT(rec1->rec_type != 0);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
 
-       /* we can't call iget() while a transactions is running
+       /*
+        * we can't call iget() while a transactions is running
         * (this can lead to a deadlock), but we need to know
         * inum and object type. so we find this information at
-        * declaration and cache in per-thread info */
+        * declaration and cache in per-thread info
+        */
        idc = osd_idc_find_or_init(env, osd, fid);
        if (IS_ERR(idc))
                RETURN(PTR_ERR(idc));
        if (idc->oic_remote) {
-               /* a reference to remote inode is represented by an
-                * agent inode which we have to create */
+               /*
+                * a reference to remote inode is represented by an
+                * agent inode which we have to create
+                */
                credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
                credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
        }
@@ -5598,9 +6046,11 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
        if (osd_dt_obj(dt)->oo_inode != NULL) {
                struct inode *inode = osd_dt_obj(dt)->oo_inode;
 
-               /* We ignore block quota on meta pool (MDTs), so needn't
+               /*
+                * We ignore block quota on meta pool (MDTs), so needn't
                 * calculate how many blocks will be consumed by this index
-                * insert */
+                * insert
+                */
                rc = osd_declare_inode_qid(env, i_uid_read(inode),
                                           i_gid_read(inode),
                                           i_projid_read(inode), 0,
@@ -5610,7 +6060,8 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
                        RETURN(rc);
 
 #ifdef HAVE_PROJECT_QUOTA
-               /* Reserve credits for local agent inode to transfer
+               /*
+                * Reserve credits for local agent inode to transfer
                 * to 0, quota enforcement is ignored in this case.
                 */
                if (idc->oic_remote &&
@@ -5618,11 +6069,10 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
                    i_projid_read(inode) != 0)
                        rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
                                                  0, i_projid_read(inode),
-                                                 0, false, PRJQUOTA);
+                                                 0, false, PRJQUOTA, true);
 #endif
        }
 
-
        RETURN(rc);
 }
 
@@ -5639,18 +6089,18 @@ static int osd_index_declare_ea_insert(const struct lu_env *env,
  */
 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
                               const struct dt_rec *rec,
-                              const struct dt_key *key, struct thandle *th,
-                              int ignore_quota)
-{
-       struct osd_object       *obj = osd_dt_obj(dt);
-       struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
-       struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
-       const struct lu_fid     *fid    = rec1->rec_fid;
-       const char              *name = (const char *)key;
-       struct osd_thread_info  *oti   = osd_oti_get(env);
-       struct inode            *child_inode = NULL;
-       struct osd_idmap_cache  *idc;
-       int                     rc;
+                              const struct dt_key *key, struct thandle *th)
+{
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
+       struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
+       const struct lu_fid *fid = rec1->rec_fid;
+       const char *name = (const char *)key;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct inode *child_inode = NULL;
+       struct osd_idmap_cache *idc;
+       int rc;
+
        ENTRY;
 
        if (!dt_object_exists(dt))
@@ -5666,17 +6116,24 @@ static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
 
        idc = osd_idc_find(env, osd, fid);
        if (unlikely(idc == NULL)) {
-               /* this dt_insert() wasn't declared properly, so
-                * FID is missing in OI cache. we better do not
-                * lookup FID in FLDB/OI and don't risk to deadlock,
-                * but in some special cases (lfsck testing, etc)
-                * it's much simpler than fixing a caller */
-               CERROR("%s: "DFID" wasn't declared for insert\n",
-                      osd_name(osd), PFID(fid));
-               dump_stack();
                idc = osd_idc_find_or_init(env, osd, fid);
-               if (IS_ERR(idc))
+               if (IS_ERR(idc)) {
+                       /*
+                        * this dt_insert() wasn't declared properly, so
+                        * FID is missing in OI cache. we better do not
+                        * lookup FID in FLDB/OI and don't risk to deadlock,
+                        * but in some special cases (lfsck testing, etc)
+                        * it's much simpler than fixing a caller.
+                        *
+                        * normally this error should be placed after the first
+                        * find, but migrate may attach source stripes to
+                        * target, which doesn't create stripes.
+                        */
+                       CERROR("%s: "DFID" wasn't declared for insert\n",
+                              osd_name(osd), PFID(fid));
+                       dump_stack();
                        RETURN(PTR_ERR(idc));
+               }
        }
 
        if (idc->oic_remote) {
@@ -5701,6 +6158,7 @@ static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
                child_inode = oti->oti_inode;
                if (unlikely(child_inode == NULL)) {
                        struct ldiskfs_inode_info *lii;
+
                        OBD_ALLOC_PTR(lii);
                        if (lii == NULL)
                                RETURN(-ENOMEM);
@@ -5720,6 +6178,7 @@ static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
                iput(child_inode);
        LASSERT(osd_invariant(obj));
        osd_trans_exec_check(env, th, OSD_OT_INSERT);
+
        RETURN(rc);
 }
 
@@ -5733,11 +6192,11 @@ static struct dt_it *osd_it_iam_init(const struct lu_env *env,
                                     struct dt_object *dt,
                                     __u32 unused)
 {
-       struct osd_it_iam      *it;
-       struct osd_object      *obj = osd_dt_obj(dt);
-       struct lu_object       *lo  = &dt->do_lu;
-       struct iam_path_descr  *ipd;
-       struct iam_container   *bag = &obj->oo_dir->od_container;
+       struct osd_it_iam *it;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct lu_object *lo = &dt->do_lu;
+       struct iam_path_descr *ipd;
+       struct iam_container *bag = &obj->oo_dir->od_container;
 
        if (!dt_object_exists(dt))
                return ERR_PTR(-ENOENT);
@@ -5764,8 +6223,8 @@ static struct dt_it *osd_it_iam_init(const struct lu_env *env,
  */
 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
 {
-       struct osd_it_iam       *it  = (struct osd_it_iam *)di;
-       struct osd_object       *obj = it->oi_obj;
+       struct osd_it_iam *it  = (struct osd_it_iam *)di;
+       struct osd_object *obj = it->oi_obj;
 
        iam_it_fini(&it->oi_it);
        osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
@@ -5785,10 +6244,10 @@ static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
  */
 
 static int osd_it_iam_get(const struct lu_env *env,
-                          struct dt_it *di, const struct dt_key *key)
+                         struct dt_it *di, const struct dt_key *key)
 {
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct osd_it_iam       *it = (struct osd_it_iam *)di;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
        if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
                /* swab quota uid/gid */
@@ -5796,7 +6255,7 @@ static int osd_it_iam_get(const struct lu_env *env,
                key = (struct dt_key *)&oti->oti_quota_id;
        }
 
-        return iam_it_get(&it->oi_it, (const struct iam_key *)key);
+       return iam_it_get(&it->oi_it, (const struct iam_key *)key);
 }
 
 /**
@@ -5806,9 +6265,9 @@ static int osd_it_iam_get(const struct lu_env *env,
  */
 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
-        iam_it_put(&it->oi_it);
+       iam_it_put(&it->oi_it);
 }
 
 /**
@@ -5823,9 +6282,9 @@ static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
 
 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
-        return iam_it_next(&it->oi_it);
+       return iam_it_next(&it->oi_it);
 }
 
 /**
@@ -5833,12 +6292,12 @@ static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
  */
 
 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
-                                 const struct dt_it *di)
+                                    const struct dt_it *di)
 {
        struct osd_thread_info *oti = osd_oti_get(env);
-       struct osd_it_iam      *it = (struct osd_it_iam *)di;
-       struct osd_object      *obj = it->oi_obj;
-       struct dt_key          *key;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_object *obj = it->oi_obj;
+       struct dt_key *key;
 
        key = (struct dt_key *)iam_it_key_get(&it->oi_it);
 
@@ -5857,9 +6316,9 @@ static struct dt_key *osd_it_iam_key(const struct lu_env *env,
 
 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
-        return iam_it_key_size(&it->oi_it);
+       return iam_it_key_size(&it->oi_it);
 }
 
 static inline void
@@ -5904,21 +6363,22 @@ osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
  * Return pointer to the record under iterator.
  */
 static int osd_it_iam_rec(const struct lu_env *env,
-                          const struct dt_it *di,
-                          struct dt_rec *dtrec, __u32 attr)
+                         const struct dt_it *di,
+                         struct dt_rec *dtrec, __u32 attr)
 {
-       struct osd_it_iam      *it   = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
        struct osd_thread_info *info = osd_oti_get(env);
+
        ENTRY;
 
        if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
                const struct osd_fid_pack *rec;
-               struct lu_fid             *fid = &info->oti_fid;
-               struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
-               char                      *name;
-               int                        namelen;
-               __u64                      hash;
-               int                        rc;
+               struct lu_fid *fid = &info->oti_fid;
+               struct lu_dirent *lde = (struct lu_dirent *)dtrec;
+               char *name;
+               int namelen;
+               __u64 hash;
+               int rc;
 
                name = (char *)iam_it_key_get(&it->oi_it);
                if (IS_ERR(name))
@@ -5956,9 +6416,9 @@ static int osd_it_iam_rec(const struct lu_env *env,
  */
 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
-        return iam_it_store(&it->oi_it);
+       return iam_it_store(&it->oi_it);
 }
 
 /**
@@ -5973,31 +6433,31 @@ static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
  */
 
 static int osd_it_iam_load(const struct lu_env *env,
-                           const struct dt_it *di, __u64 hash)
+                          const struct dt_it *di, __u64 hash)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_it_iam *it = (struct osd_it_iam *)di;
 
-        return iam_it_load(&it->oi_it, hash);
+       return iam_it_load(&it->oi_it, hash);
 }
 
 static const struct dt_index_operations osd_index_iam_ops = {
-        .dio_lookup         = osd_index_iam_lookup,
-        .dio_declare_insert = osd_index_declare_iam_insert,
-        .dio_insert         = osd_index_iam_insert,
-        .dio_declare_delete = osd_index_declare_iam_delete,
-        .dio_delete         = osd_index_iam_delete,
-        .dio_it     = {
-                .init     = osd_it_iam_init,
-                .fini     = osd_it_iam_fini,
-                .get      = osd_it_iam_get,
-                .put      = osd_it_iam_put,
-                .next     = osd_it_iam_next,
-                .key      = osd_it_iam_key,
-                .key_size = osd_it_iam_key_size,
-                .rec      = osd_it_iam_rec,
-                .store    = osd_it_iam_store,
-                .load     = osd_it_iam_load
-        }
+       .dio_lookup         = osd_index_iam_lookup,
+       .dio_declare_insert = osd_index_declare_iam_insert,
+       .dio_insert         = osd_index_iam_insert,
+       .dio_declare_delete = osd_index_declare_iam_delete,
+       .dio_delete         = osd_index_iam_delete,
+       .dio_it     = {
+               .init     = osd_it_iam_init,
+               .fini     = osd_it_iam_fini,
+               .get      = osd_it_iam_get,
+               .put      = osd_it_iam_put,
+               .next     = osd_it_iam_next,
+               .key      = osd_it_iam_key,
+               .key_size = osd_it_iam_key_size,
+               .rec      = osd_it_iam_rec,
+               .store    = osd_it_iam_store,
+               .load     = osd_it_iam_load
+       }
 };
 
 
@@ -6011,12 +6471,13 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
                                    struct dt_object *dt,
                                    __u32 attr)
 {
-       struct osd_object       *obj  = osd_dt_obj(dt);
-       struct osd_thread_info  *info = osd_oti_get(env);
-       struct osd_it_ea        *oie;
-       struct file             *file;
-       struct lu_object        *lo   = &dt->do_lu;
-       struct dentry           *obj_dentry;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct osd_it_ea *oie;
+       struct file *file;
+       struct lu_object *lo = &dt->do_lu;
+       struct dentry *obj_dentry;
+
        ENTRY;
 
        if (!dt_object_exists(dt) || obj->oo_destroyed)
@@ -6042,7 +6503,7 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
                if (oie->oie_buf == NULL)
                        RETURN(ERR_PTR(-ENOMEM));
        }
-       oie->oie_obj             = obj;
+       oie->oie_obj = obj;
 
        file = &oie->oie_file;
 
@@ -6054,10 +6515,10 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
        file->f_path.dentry     = obj_dentry;
        file->f_mapping         = obj->oo_inode->i_mapping;
        file->f_op              = obj->oo_inode->i_fop;
-       set_file_inode(file, obj->oo_inode);
+       file->f_inode = obj->oo_inode;
 
        lu_object_get(lo);
-       RETURN((struct dt_it *) oie);
+       RETURN((struct dt_it *)oie);
 }
 
 /**
@@ -6067,10 +6528,10 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
  */
 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
 {
-       struct osd_thread_info  *info = osd_oti_get(env);
-       struct osd_it_ea        *oie    = (struct osd_it_ea *)di;
-       struct osd_object       *obj    = oie->oie_obj;
-       struct inode            *inode  = obj->oo_inode;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct osd_it_ea *oie = (struct osd_it_ea *)di;
+       struct osd_object *obj = oie->oie_obj;
+       struct inode *inode = obj->oo_inode;
 
        ENTRY;
        oie->oie_file.f_op->release(inode, &oie->oie_file);
@@ -6094,18 +6555,18 @@ static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
  */
 static int osd_it_ea_get(const struct lu_env *env,
-                         struct dt_it *di, const struct dt_key *key)
+                        struct dt_it *di, const struct dt_key *key)
 {
-        struct osd_it_ea     *it   = (struct osd_it_ea *)di;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
 
-        ENTRY;
-        LASSERT(((const char *)key)[0] == '\0');
-        it->oie_file.f_pos      = 0;
-        it->oie_rd_dirent       = 0;
-        it->oie_it_dirent       = 0;
-        it->oie_dirent          = NULL;
+       ENTRY;
+       LASSERT(((const char *)key)[0] == '\0');
+       it->oie_file.f_pos = 0;
+       it->oie_rd_dirent = 0;
+       it->oie_it_dirent = 0;
+       it->oie_dirent = NULL;
 
-        RETURN(+1);
+       RETURN(+1);
 }
 
 /**
@@ -6116,13 +6577,11 @@ static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
 }
 
 struct osd_filldir_cbs {
-#ifdef HAVE_DIR_CONTEXT
        struct dir_context ctx;
-#endif
        struct osd_it_ea  *it;
 };
 /**
- * It is called internally by ->readdir(). It fills the
+ * It is called internally by ->iterate*(). It fills the
  * iterator's in-memory data structure with required
  * information i.e. name, namelen, rec_size etc.
  *
@@ -6138,31 +6597,31 @@ static int osd_ldiskfs_filldir(struct dir_context *buf,
 static int osd_ldiskfs_filldir(void *buf,
 #endif
                               const char *name, int namelen,
-                              loff_t offset, __u64 ino, unsigned d_type)
-{
-       struct osd_it_ea        *it   =
-               ((struct osd_filldir_cbs *)buf)->it;
-       struct osd_object       *obj  = it->oie_obj;
-        struct osd_it_ea_dirent *ent  = it->oie_dirent;
-        struct lu_fid           *fid  = &ent->oied_fid;
-        struct osd_fid_pack     *rec;
-        ENTRY;
-
-        /* this should never happen */
-        if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
-                CERROR("ldiskfs return invalid namelen %d\n", namelen);
-                RETURN(-EIO);
-        }
-
-        if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
-            OSD_IT_EA_BUFSIZE)
-                RETURN(1);
+                              loff_t offset, __u64 ino, unsigned int d_type)
+{
+       struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
+       struct osd_object *obj = it->oie_obj;
+       struct osd_it_ea_dirent *ent = it->oie_dirent;
+       struct lu_fid *fid = &ent->oied_fid;
+       struct osd_fid_pack *rec;
+
+       ENTRY;
+
+/* this should never happen */
+       if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
+               CERROR("ldiskfs return invalid namelen %d\n", namelen);
+               RETURN(-EIO);
+       }
+
+       if ((void *)ent - it->oie_buf + sizeof(*ent) + namelen >
+           OSD_IT_EA_BUFSIZE)
+               RETURN(1);
 
        /* "." is just the object itself. */
        if (namelen == 1 && name[0] == '.') {
                *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
        } else if (d_type & LDISKFS_DIRENT_LUFID) {
-               rec = (struct osd_fid_pack*) (name + namelen + 1);
+               rec = (struct osd_fid_pack *)(name + namelen + 1);
                if (osd_fid_unpack(fid, rec) != 0)
                        fid_zero(fid);
        } else {
@@ -6176,20 +6635,20 @@ static int osd_ldiskfs_filldir(void *buf,
                *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
        }
 
-        ent->oied_ino     = ino;
-        ent->oied_off     = offset;
-        ent->oied_namelen = namelen;
-        ent->oied_type    = d_type;
+       ent->oied_ino     = ino;
+       ent->oied_off     = offset;
+       ent->oied_namelen = namelen;
+       ent->oied_type    = d_type;
 
-        memcpy(ent->oied_name, name, namelen);
+       memcpy(ent->oied_name, name, namelen);
 
-        it->oie_rd_dirent++;
-        it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
-        RETURN(0);
+       it->oie_rd_dirent++;
+       it->oie_dirent = (void *)ent + cfs_size_round(sizeof(*ent) + namelen);
+       RETURN(0);
 }
 
 /**
- * Calls ->readdir() to load a directory entry at a time
+ * Calls ->iterate*() to load a directory entry at a time
  * and stored it in iterator's in-memory data structure.
  *
  * \param di iterator's in memory structure
@@ -6199,49 +6658,52 @@ static int osd_ldiskfs_filldir(void *buf,
  * \retval +1 reach the end of entry
  */
 static int osd_ldiskfs_it_fill(const struct lu_env *env,
-                               const struct dt_it *di)
-{
-        struct osd_it_ea   *it    = (struct osd_it_ea *)di;
-        struct osd_object  *obj   = it->oie_obj;
-        struct inode       *inode = obj->oo_inode;
-        struct htree_lock  *hlock = NULL;
-       struct file        *filp  = &it->oie_file;
-       int                 rc = 0;
+                              const struct dt_it *di)
+{
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
+       struct osd_object *obj = it->oie_obj;
+       struct inode *inode = obj->oo_inode;
+       struct htree_lock *hlock = NULL;
+       struct file *filp = &it->oie_file;
+       int rc = 0;
        struct osd_filldir_cbs buf = {
-#ifdef HAVE_DIR_CONTEXT
                .ctx.actor = osd_ldiskfs_filldir,
-#endif
                .it = it
        };
 
-        ENTRY;
-        it->oie_dirent = it->oie_buf;
-        it->oie_rd_dirent = 0;
+       ENTRY;
+       it->oie_dirent = it->oie_buf;
+       it->oie_rd_dirent = 0;
 
-        if (obj->oo_hl_head != NULL) {
-                hlock = osd_oti_get(env)->oti_hlock;
-                ldiskfs_htree_lock(hlock, obj->oo_hl_head,
-                                   inode, LDISKFS_HLOCK_READDIR);
-        } else {
+       if (obj->oo_hl_head != NULL) {
+               hlock = osd_oti_get(env)->oti_hlock;
+               ldiskfs_htree_lock(hlock, obj->oo_hl_head,
+                                  inode, LDISKFS_HLOCK_READDIR);
+       } else {
                down_read(&obj->oo_ext_idx_sem);
-        }
+       }
 
-#ifdef HAVE_DIR_CONTEXT
-       buf.ctx.pos = filp->f_pos;
-       rc = inode->i_fop->iterate(filp, &buf.ctx);
-       filp->f_pos = buf.ctx.pos;
-#else
-       rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
-#endif
+       filp->f_cred = current_cred();
+       rc = osd_security_file_alloc(filp);
+       if (rc)
+               RETURN(rc);
+
+       filp->f_flags |= O_NOATIME;
+       filp->f_mode |= FMODE_NONOTIFY;
+       rc = iterate_dir(filp, &buf.ctx);
+       if (rc)
+               RETURN(rc);
 
-        if (hlock != NULL)
-                ldiskfs_htree_unlock(hlock);
-        else
+       if (hlock != NULL)
+               ldiskfs_htree_unlock(hlock);
+       else
                up_read(&obj->oo_ext_idx_sem);
 
        if (it->oie_rd_dirent == 0) {
-               /*If it does not get any dirent, it means it has been reached
-                *to the end of the dir */
+               /*
+                * If it does not get any dirent, it means it has been reached
+                * to the end of the dir
+                */
                it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
                if (rc == 0)
                        rc = 1;
@@ -6254,7 +6716,7 @@ static int osd_ldiskfs_it_fill(const struct lu_env *env,
 }
 
 /**
- * It calls osd_ldiskfs_it_fill() which will use ->readdir()
+ * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
  * to load a directory entry at a time and stored it in
  * iterator's in-memory data structure.
  *
@@ -6266,26 +6728,26 @@ static int osd_ldiskfs_it_fill(const struct lu_env *env,
  */
 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
 {
-        struct osd_it_ea *it = (struct osd_it_ea *)di;
-        int rc;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
+       int rc;
 
-        ENTRY;
+       ENTRY;
 
-        if (it->oie_it_dirent < it->oie_rd_dirent) {
-                it->oie_dirent =
-                        (void *) it->oie_dirent +
-                        cfs_size_round(sizeof(struct osd_it_ea_dirent) +
-                                       it->oie_dirent->oied_namelen);
-                it->oie_it_dirent++;
-                RETURN(0);
-        } else {
+       if (it->oie_it_dirent < it->oie_rd_dirent) {
+               it->oie_dirent =
+                       (void *)it->oie_dirent +
+                       cfs_size_round(sizeof(struct osd_it_ea_dirent) +
+                                      it->oie_dirent->oied_namelen);
+               it->oie_it_dirent++;
+               rc = 0;
+       } else {
                if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
-                        rc = +1;
-                else
-                        rc = osd_ldiskfs_it_fill(env, di);
-        }
+                       rc = 1;
+               else
+                       rc = osd_ldiskfs_it_fill(env, di);
+       }
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /**
@@ -6296,15 +6758,15 @@ static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
  * \retval key i.e. struct dt_key on success
  */
 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
-                                    const struct dt_it *di)
+                                   const struct dt_it *di)
 {
-        struct osd_it_ea *it = (struct osd_it_ea *)di;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
 
-        return (struct dt_key *)it->oie_dirent->oied_name;
+       return (struct dt_key *)it->oie_dirent->oied_name;
 }
 
 /**
- * Returns the key's size at current position from iterator's in memory structure.
+ * Returns key's size at current position from iterator's in memory structure.
  *
  * \param di iterator's in memory structure
  *
@@ -6312,11 +6774,16 @@ static struct dt_key *osd_it_ea_key(const struct lu_env *env,
  */
 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
 {
-        struct osd_it_ea *it = (struct osd_it_ea *)di;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
 
-        return it->oie_dirent->oied_namelen;
+       return it->oie_dirent->oied_namelen;
 }
 
+#if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
+#undef LDISKFS_DIR_REC_LEN
+#define LDISKFS_DIR_REC_LEN(de)                LDISKFS_DIR_ENTRY_LEN_((de))
+#endif
+
 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
 {
        if (LDISKFS_DIR_REC_LEN(de) >=
@@ -6328,7 +6795,7 @@ static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
 
 static inline bool
 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
-                    unsigned blocksize, bool dotdot)
+                    unsigned int blocksize, bool dotdot)
 {
        if (dotdot)
                return osd_dotdot_has_space(de);
@@ -6347,17 +6814,17 @@ osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
                    struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
                    bool dotdot)
 {
-       struct inode                *dir        = dentry->d_parent->d_inode;
-       struct inode                *inode      = dentry->d_inode;
-       struct osd_fid_pack         *rec;
+       struct inode *dir = dentry->d_parent->d_inode;
+       struct inode *inode = dentry->d_inode;
+       struct osd_fid_pack *rec;
        struct ldiskfs_dentry_param *ldp;
-       int                          namelen    = dentry->d_name.len;
-       int                          rc;
-       struct osd_thread_info     *info        = osd_oti_get(env);
+       int namelen = dentry->d_name.len;
+       int rc;
+       struct osd_thread_info *info = osd_oti_get(env);
+
        ENTRY;
 
-       if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
-                                         LDISKFS_FEATURE_INCOMPAT_DIRDATA))
+       if (!ldiskfs_has_feature_dirdata(inode->i_sb))
                RETURN(0);
 
        /* There is enough space to hold the FID-in-dirent. */
@@ -6385,10 +6852,12 @@ osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
        ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
        osd_get_ldiskfs_dirent_param(ldp, fid);
        dentry->d_fsdata = (void *)ldp;
-       ll_vfs_dq_init(dir);
+       dquot_initialize(dir);
        rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
-       /* It is too bad, we cannot reinsert the name entry back.
-        * That means we lose it! */
+       /*
+        * It is too bad, we cannot reinsert the name entry back.
+        * That means we lose it!
+        */
        if (rc != 0)
                CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
                       "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
@@ -6404,24 +6873,25 @@ osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
                        struct osd_it_ea *it, struct lu_fid *fid,
                        struct osd_inode_id *id, __u32 *attr)
 {
-       struct osd_thread_info     *info        = osd_oti_get(env);
-       struct lustre_mdt_attrs    *lma         = &info->oti_ost_attrs.loa_lma;
-       struct osd_device          *dev         = osd_obj2dev(obj);
-       struct super_block         *sb          = osd_sb(dev);
-       const char                 *devname     = osd_name(dev);
-       struct osd_it_ea_dirent    *ent         = it->oie_dirent;
-       struct inode               *dir         = obj->oo_inode;
-       struct htree_lock          *hlock       = NULL;
-       struct buffer_head         *bh          = NULL;
-       handle_t                   *jh          = NULL;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
+       struct osd_device *dev = osd_obj2dev(obj);
+       struct super_block *sb = osd_sb(dev);
+       const char *devname = osd_name(dev);
+       struct osd_it_ea_dirent *ent = it->oie_dirent;
+       struct inode *dir = obj->oo_inode;
+       struct htree_lock *hlock = NULL;
+       struct buffer_head *bh = NULL;
+       handle_t *jh = NULL;
        struct ldiskfs_dir_entry_2 *de;
-       struct dentry              *dentry;
-       struct inode               *inode;
+       struct dentry *dentry;
+       struct inode *inode;
        const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
-       int                         credits;
-       int                         rc;
-       bool                        dotdot      = false;
-       bool                        dirty       = false;
+       int credits;
+       int rc;
+       bool dotdot = false;
+       bool dirty = false;
+
        ENTRY;
 
        if (ent->oied_name[0] == '.') {
@@ -6437,8 +6907,10 @@ osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
        if (IS_ERR(inode)) {
                rc = PTR_ERR(inode);
                if (rc == -ENOENT || rc == -ESTALE) {
-                       /* Maybe dangling name entry, or
-                        * corrupted directory entry. */
+                       /*
+                        * Maybe dangling name entry, or
+                        * corrupted directory entry.
+                        */
                        *attr |= LUDA_UNKNOWN;
                        rc = 0;
                } else {
@@ -6461,7 +6933,8 @@ osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
        else if (rc != 0)
                GOTO(out, rc);
 
-       /* We need to ensure that the name entry is still valid.
+       /*
+        * We need to ensure that the name entry is still valid.
         * Because it may be removed or renamed by other already.
         *
         * The unlink or rename operation will start journal before PDO lock,
@@ -6473,7 +6946,8 @@ osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
         * We may need to remove the name entry firstly, then insert back.
         * One credit is for user quota file update.
         * One credit is for group quota file update.
-        * Two credits are for dirty inode. */
+        * Two credits are for dirty inode.
+        */
        credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
                  osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
 
@@ -6495,10 +6969,12 @@ again:
 
                if (obj->oo_hl_head != NULL) {
                        hlock = osd_oti_get(env)->oti_hlock;
-                       /* "0" means exclusive lock for the whole directory.
+                       /*
+                        * "0" means exclusive lock for the whole directory.
                         * We need to prevent others access such name entry
                         * during the delete + insert. Neither HLOCK_ADD nor
-                        * HLOCK_DEL cannot guarantee the atomicity. */
+                        * HLOCK_DEL cannot guarantee the atomicity.
+                        */
                        ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
                } else {
                        down_write(&obj->oo_ext_idx_sem);
@@ -6520,12 +6996,14 @@ again:
                GOTO(out, rc = 0);
        }
 
-       /* For dotdot entry, if there is not enough space to hold the
+       /*
+        * For dotdot entry, if there is not enough space to hold the
         * FID-in-dirent, just keep them there. It only happens when the
         * device upgraded from 1.8 or restored from MDT file-level backup.
         * For the whole directory, only dotdot entry have no FID-in-dirent
         * and needs to get FID from LMA when readdir, it will not affect the
-        * performance much. */
+        * performance much.
+        */
        if (dotdot && !osd_dotdot_has_space(de)) {
                *attr |= LUDA_UNKNOWN;
 
@@ -6542,8 +7020,10 @@ again:
                        if (likely(dotdot &&
                                   fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
                                   fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
-                               /* It must be REMOTE_PARENT_DIR and as the
-                                * 'dotdot' entry of remote directory */
+                               /*
+                                * It must be REMOTE_PARENT_DIR and as the
+                                * 'dotdot' entry of remote directory
+                                */
                                *attr |= LUDA_IGNORE;
                        } else {
                                CDEBUG(D_LFSCK, "%s: expect remote agent "
@@ -6566,9 +7046,11 @@ again:
                if (rc == -ENOENT ||
                    (rc == -ENODATA &&
                     !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
-                       /* linkEA does not recognize the dirent entry,
+                       /*
+                        * linkEA does not recognize the dirent entry,
                         * it may because the dirent entry corruption
-                        * and points to other's inode. */
+                        * and points to other's inode.
+                        */
                        CDEBUG(D_LFSCK, "%s: the target inode does not "
                               "recognize the dirent, dir = %lu/%u, "
                               " name = %.*s, ino = %llu, "
@@ -6593,8 +7075,10 @@ again:
        }
 
        if (lma != NULL) {
-               /* linkEA recognizes the dirent entry, the FID-in-LMA is
-                * valid, trusted, in spite of fid_is_sane(fid) or not. */
+               /*
+                * linkEA recognizes the dirent entry, the FID-in-LMA is
+                * valid, trusted, in spite of fid_is_sane(fid) or not.
+                */
                if (*attr & LUDA_VERIFY_DRYRUN) {
                        *fid = lma->lma_self_fid;
                        *attr |= LUDA_REPAIR;
@@ -6658,8 +7142,10 @@ again:
 
                dirty = true;
                if (unlikely(fid_is_sane(fid))) {
-                       /* FID-in-dirent exists, but FID-in-LMA is lost.
-                        * Trust the FID-in-dirent, and add FID-in-LMA. */
+                       /*
+                        * FID-in-dirent exists, but FID-in-LMA is lost.
+                        * Trust the FID-in-dirent, and add FID-in-LMA.
+                        */
                        rc = osd_ea_fid_set(info, inode, fid, 0, 0);
                        if (rc == 0)
                                *attr |= LUDA_REPAIR;
@@ -6673,8 +7159,10 @@ again:
                                       ent->oied_ino, PFID(fid), rc);
                } else if (dev->od_index == 0) {
                        lu_igif_build(fid, inode->i_ino, inode->i_generation);
-                       /* It is probably IGIF object. Only aappend the
-                        * FID-in-dirent. OI scrub will process FID-in-LMA. */
+                       /*
+                        * It is probably IGIF object. Only aappend the
+                        * FID-in-dirent. OI scrub will process FID-in-LMA.
+                        */
                        rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
                                                 bh, de, hlock, dotdot);
                        if (rc == 0)
@@ -6734,20 +7222,23 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
        struct osd_device      *dev   = osd_obj2dev(obj);
        struct osd_thread_info *oti   = osd_oti_get(env);
        struct osd_inode_id    *id    = &oti->oti_id;
-       struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
+       struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
        struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
-       __u32                   ino   = it->oie_dirent->oied_ino;
-       int                     rc    = 0;
+       __u32 ino = it->oie_dirent->oied_ino;
+       int rc = 0;
+
        ENTRY;
 
-       LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
+       LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
 
        if (attr & LUDA_VERIFY) {
-               if (unlikely(ino == osd_remote_parent_ino(dev))) {
+               if (unlikely(is_remote_parent_ino(dev, ino))) {
                        attr |= LUDA_IGNORE;
-                       /* If the parent is on remote MDT, and there
+                       /*
+                        * If the parent is on remote MDT, and there
                         * is no FID-in-dirent, then we have to get
-                        * the parent FID from the linkEA.  */
+                        * the parent FID from the linkEA.
+                        */
                        if (!fid_is_sane(fid) &&
                            it->oie_dirent->oied_namelen == 2 &&
                            it->oie_dirent->oied_name[0] == '.' &&
@@ -6764,14 +7255,17 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
                attr &= ~LU_DIRENT_ATTRS_MASK;
                if (!fid_is_sane(fid)) {
                        bool is_dotdot = false;
+
                        if (it->oie_dirent->oied_namelen == 2 &&
                            it->oie_dirent->oied_name[0] == '.' &&
                            it->oie_dirent->oied_name[1] == '.')
                                is_dotdot = true;
-                       /* If the parent is on remote MDT, and there
+                       /*
+                        * If the parent is on remote MDT, and there
                         * is no FID-in-dirent, then we have to get
-                        * the parent FID from the linkEA.  */
-                       if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
+                        * the parent FID from the linkEA.
+                        */
+                       if (is_remote_parent_ino(dev, ino) && is_dotdot) {
                                rc = osd_get_pfid_from_linkea(env, obj, fid);
                        } else {
                                if (is_dotdot == false &&
@@ -6780,8 +7274,6 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
 
                                rc = osd_ea_fid_get(env, obj, ino, fid, id);
                        }
-               } else {
-                       osd_id_gen(id, ino, OSD_OII_NOGEN);
                }
        }
 
@@ -6791,15 +7283,6 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
                           it->oie_dirent->oied_namelen,
                           it->oie_dirent->oied_type, attr);
 
-       if (rc < 0)
-               RETURN(rc);
-
-       if (osd_remote_fid(env, dev, fid))
-               RETURN(0);
-
-       if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
-               osd_add_oi_cache(oti, dev, id, fid);
-
        RETURN(rc > 0 ? 0 : rc);
 }
 
@@ -6834,13 +7317,13 @@ static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
  */
 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
 {
-        struct osd_it_ea *it = (struct osd_it_ea *)di;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
 
-        return it->oie_dirent->oied_off;
+       return it->oie_dirent->oied_off;
 }
 
 /**
- * It calls osd_ldiskfs_it_fill() which will use ->readdir()
+ * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
  * to load a directory entry at a time and stored it i inn,
  * in iterator's in-memory data structure.
  *
@@ -6850,22 +7333,22 @@ static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
  * \retval -ve on error
  */
 static int osd_it_ea_load(const struct lu_env *env,
-                          const struct dt_it *di, __u64 hash)
+                         const struct dt_it *di, __u64 hash)
 {
-        struct osd_it_ea *it = (struct osd_it_ea *)di;
-        int rc;
+       struct osd_it_ea *it = (struct osd_it_ea *)di;
+       int rc;
 
-        ENTRY;
-        it->oie_file.f_pos = hash;
+       ENTRY;
+       it->oie_file.f_pos = hash;
 
-        rc =  osd_ldiskfs_it_fill(env, di);
+       rc =  osd_ldiskfs_it_fill(env, di);
        if (rc > 0)
                rc = -ENODATA;
 
-        if (rc == 0)
-                rc = +1;
+       if (rc == 0)
+               rc = 1;
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /**
@@ -6879,18 +7362,18 @@ static int osd_it_ea_load(const struct lu_env *env,
 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
                               struct dt_rec *rec, const struct dt_key *key)
 {
-        struct osd_object *obj = osd_dt_obj(dt);
-        int rc = 0;
+       struct osd_object *obj = osd_dt_obj(dt);
+       int rc = 0;
 
-        ENTRY;
+       ENTRY;
 
-        LASSERT(S_ISDIR(obj->oo_inode->i_mode));
-        LINVRNT(osd_invariant(obj));
+       LASSERT(S_ISDIR(obj->oo_inode->i_mode));
+       LINVRNT(osd_invariant(obj));
 
-        rc = osd_ea_lookup_rec(env, obj, rec, key);
-        if (rc == 0)
-                rc = +1;
-        RETURN(rc);
+       rc = osd_ea_lookup_rec(env, obj, rec, key);
+       if (rc == 0)
+               rc = 1;
+       RETURN(rc);
 }
 
 /**
@@ -6919,39 +7402,55 @@ static const struct dt_index_operations osd_index_ea_ops = {
 };
 
 static void *osd_key_init(const struct lu_context *ctx,
-                          struct lu_context_key *key)
+                         struct lu_context_key *key)
 {
-        struct osd_thread_info *info;
+       struct osd_thread_info *info;
 
-        OBD_ALLOC_PTR(info);
-        if (info == NULL)
-                return ERR_PTR(-ENOMEM);
+       OBD_ALLOC_PTR(info);
+       if (info == NULL)
+               return ERR_PTR(-ENOMEM);
 
-        OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
-        if (info->oti_it_ea_buf == NULL)
-                goto out_free_info;
+       OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
+       if (info->oti_it_ea_buf == NULL)
+               goto out_free_info;
 
-        info->oti_env = container_of(ctx, struct lu_env, le_ctx);
+       info->oti_env = container_of(ctx, struct lu_env, le_ctx);
 
-        info->oti_hlock = ldiskfs_htree_lock_alloc();
-        if (info->oti_hlock == NULL)
-                goto out_free_ea;
+       info->oti_hlock = ldiskfs_htree_lock_alloc();
+       if (info->oti_hlock == NULL)
+               goto out_free_ea;
 
-        return info;
+       return info;
 
- out_free_ea:
-        OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
- out_free_info:
-        OBD_FREE_PTR(info);
-        return ERR_PTR(-ENOMEM);
+out_free_ea:
+       OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
+out_free_info:
+       OBD_FREE_PTR(info);
+       return ERR_PTR(-ENOMEM);
 }
 
 static void osd_key_fini(const struct lu_context *ctx,
-                         struct lu_context_key *key, void* data)
+                        struct lu_context_key *key, void *data)
 {
        struct osd_thread_info *info = data;
        struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
-       struct osd_idmap_cache  *idc = info->oti_ins_cache;
+       struct osd_idmap_cache *idc = info->oti_ins_cache;
+
+       if (info->oti_dio_pages) {
+               int i;
+               for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
+                       struct page *page = info->oti_dio_pages[i];
+                       if (page) {
+                               LASSERT(PagePrivate2(page));
+                               LASSERT(PageLocked(page));
+                               ClearPagePrivate2(page);
+                               unlock_page(page);
+                               __free_page(page);
+                       }
+               }
+               OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
+                                        PTLRPC_MAX_BRW_PAGES);
+       }
 
        if (info->oti_inode != NULL)
                OBD_FREE_PTR(lli);
@@ -6960,10 +7459,11 @@ static void osd_key_fini(const struct lu_context *ctx,
        OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
        lu_buf_free(&info->oti_iobuf.dr_pg_buf);
        lu_buf_free(&info->oti_iobuf.dr_bl_buf);
+       lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
        lu_buf_free(&info->oti_big_buf);
        if (idc != NULL) {
                LASSERT(info->oti_ins_cache_size > 0);
-               OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
+               OBD_FREE_PTR_ARRAY(idc, info->oti_ins_cache_size);
                info->oti_ins_cache = NULL;
                info->oti_ins_cache_size = 0;
        }
@@ -6971,41 +7471,42 @@ static void osd_key_fini(const struct lu_context *ctx,
 }
 
 static void osd_key_exit(const struct lu_context *ctx,
-                         struct lu_context_key *key, void *data)
+                        struct lu_context_key *key, void *data)
 {
-        struct osd_thread_info *info = data;
+       struct osd_thread_info *info = data;
 
-        LASSERT(info->oti_r_locks == 0);
-        LASSERT(info->oti_w_locks == 0);
-        LASSERT(info->oti_txns    == 0);
+       LASSERT(info->oti_r_locks == 0);
+       LASSERT(info->oti_w_locks == 0);
+       LASSERT(info->oti_txns    == 0);
 }
 
 /* type constructor/destructor: osd_type_init, osd_type_fini */
 LU_TYPE_INIT_FINI(osd, &osd_key);
 
 struct lu_context_key osd_key = {
-        .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
-        .lct_init = osd_key_init,
-        .lct_fini = osd_key_fini,
-        .lct_exit = osd_key_exit
+       .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
+       .lct_init = osd_key_init,
+       .lct_fini = osd_key_fini,
+       .lct_exit = osd_key_exit
 };
 
 
 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
-                           const char *name, struct lu_device *next)
+                          const char *name, struct lu_device *next)
 {
        struct osd_device *osd = osd_dev(d);
 
-       if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
-           >= sizeof(osd->od_svname))
+       if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
+           sizeof(osd->od_svname))
                return -E2BIG;
        return osd_procfs_init(osd, name);
 }
 
 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
 {
-       struct seq_server_site  *ss = osd_seq_site(osd);
-       int                     rc;
+       struct seq_server_site *ss = osd_seq_site(osd);
+       int rc = 0;
+
        ENTRY;
 
        if (osd->od_is_ost || osd->od_cl_seq != NULL)
@@ -7018,18 +7519,15 @@ static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
        if (osd->od_cl_seq == NULL)
                RETURN(-ENOMEM);
 
-       rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
-                            osd->od_svname, ss->ss_server_seq);
-       if (rc != 0) {
-               OBD_FREE_PTR(osd->od_cl_seq);
-               osd->od_cl_seq = NULL;
-               RETURN(rc);
-       }
+       seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
+                       osd->od_svname, ss->ss_server_seq);
 
        if (ss->ss_node_id == 0) {
-               /* If the OSD on the sequence controller(MDT0), then allocate
+               /*
+                * If the OSD on the sequence controller(MDT0), then allocate
                 * sequence here, otherwise allocate sequence after connected
-                * to MDT0 (see mdt_register_lwp_callback()). */
+                * to MDT0 (see mdt_register_lwp_callback()).
+                */
                rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
                                   &osd->od_cl_seq->lcs_space, env);
        }
@@ -7052,10 +7550,17 @@ static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
        ENTRY;
 
        /* shutdown quota slave instance associated with the device */
-       if (o->od_quota_slave != NULL) {
-               struct qsd_instance *qsd = o->od_quota_slave;
+       if (o->od_quota_slave_md != NULL) {
+               struct qsd_instance *qsd = o->od_quota_slave_md;
+
+               o->od_quota_slave_md = NULL;
+               qsd_fini(env, qsd);
+       }
+
+       if (o->od_quota_slave_dt != NULL) {
+               struct qsd_instance *qsd = o->od_quota_slave_dt;
 
-               o->od_quota_slave = NULL;
+               o->od_quota_slave_dt = NULL;
                qsd_fini(env, qsd);
        }
 
@@ -7081,21 +7586,22 @@ static void osd_umount(const struct lu_env *env, struct osd_device *o)
 }
 
 static int osd_mount(const struct lu_env *env,
-                     struct osd_device *o, struct lustre_cfg *cfg)
+                    struct osd_device *o, struct lustre_cfg *cfg)
 {
-       const char              *name  = lustre_cfg_string(cfg, 0);
-       const char              *dev  = lustre_cfg_string(cfg, 1);
-       const char              *opts;
-       unsigned long            page, s_flags, lmd_flags = 0;
-       struct page             *__page;
+       const char *name = lustre_cfg_string(cfg, 0);
+       const char *dev = lustre_cfg_string(cfg, 1);
+       const char *opts;
+       unsigned long page, s_flags, lmd_flags = 0;
+       struct page *__page;
        struct file_system_type *type;
-       char                    *options = NULL;
-       char                    *str;
-       struct osd_thread_info  *info = osd_oti_get(env);
-       struct lu_fid           *fid = &info->oti_fid;
-       struct inode            *inode;
-       int                      rc = 0, force_over_512tb = 0;
-        ENTRY;
+       char *options = NULL;
+       char *str;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lu_fid *fid = &info->oti_fid;
+       struct inode *inode;
+       int rc = 0, force_over_1024tb = 0;
+
+       ENTRY;
 
        if (o->od_mnt != NULL)
                RETURN(0);
@@ -7120,23 +7626,22 @@ static int osd_mount(const struct lu_env *env,
 #endif
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
        if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
-               CWARN("force_over_128tb option is deprecated. "
-                     "Filesystems less than 512TB can be created without any "
-                     "force options. Use force_over_512tb option for "
-                     "filesystems greater than 512TB.\n");
+               CWARN("force_over_128tb option is deprecated.  Filesystems smaller than 1024TB can be created without any force option. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
        }
 #endif
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
        if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
-               CWARN("force_over_256tb option is deprecated. "
-                     "Filesystems less than 512TB can be created without any "
-                     "force options. Use force_over_512tb option for "
-                     "filesystems greater than 512TB.\n");
+               CWARN("force_over_256tb option is deprecated.  Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
+       }
+#endif
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
+       if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
+               CWARN("force_over_512tb option is deprecated.  Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
        }
 #endif
 
-       if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
-               force_over_512tb = 1;
+       if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
+               force_over_1024tb = 1;
 
        __page = alloc_page(GFP_KERNEL);
        if (__page == NULL)
@@ -7146,7 +7651,7 @@ static int osd_mount(const struct lu_env *env,
        *options = '\0';
        if (opts != NULL) {
                /* strip out the options for back compatiblity */
-               static char *sout[] = {
+               static const char * const sout[] = {
                        "mballoc",
                        "iopen",
                        "noiopen",
@@ -7158,11 +7663,13 @@ static int osd_mount(const struct lu_env *env,
                        "force_over_128tb",
                        "force_over_256tb",
                        "force_over_512tb",
+                       "force_over_1024tb",
                        NULL
                };
-               strcat(options, opts);
+               strncat(options, opts, PAGE_SIZE);
                for (rc = 0, str = options; sout[rc]; ) {
                        char *op = strstr(str, sout[rc]);
+
                        if (op == NULL) {
                                rc++;
                                str = options;
@@ -7179,13 +7686,13 @@ static int osd_mount(const struct lu_env *env,
                                ;
                }
        } else {
-               strncat(options, "user_xattr,acl", 14);
+               strncat(options, "user_xattr,acl", PAGE_SIZE);
        }
 
        /* Glom up mount options */
        if (*options != '\0')
-               strcat(options, ",");
-       strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
+               strncat(options, ",", PAGE_SIZE);
+       strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
 
        type = get_fs_type("ldiskfs");
        if (!type) {
@@ -7204,44 +7711,27 @@ static int osd_mount(const struct lu_env *env,
        }
 
        if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
-                                osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
-                                force_over_512tb == 0) {
-               CERROR("%s: device %s LDISKFS does not support filesystems "
-                      "greater than 512TB and can cause data corruption. "
-                      "Use \"force_over_512tb\" mount option to override.\n",
+                                osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
+                                force_over_1024tb == 0) {
+               CERROR("%s: device %s LDISKFS has not been tested on filesystems larger than 1024TB and may cause data corruption. Use 'force_over_1024tb' mount option to override.\n",
                       name, dev);
                GOTO(out_mnt, rc = -EINVAL);
        }
 
        if (lmd_flags & LMD_FLG_DEV_RDONLY) {
-               if (priv_dev_set_rdonly) {
-                       priv_dev_set_rdonly(osd_sb(o)->s_bdev);
-                       o->od_dt_dev.dd_rdonly = 1;
-                       LCONSOLE_WARN("%s: set dev_rdonly on this device\n",
-                                     name);
-               } else {
-                       LCONSOLE_WARN("%s: not support dev_rdonly on this device",
-                                     name);
+               LCONSOLE_WARN("%s: not support dev_rdonly on this device",
+                             name);
 
-                       GOTO(out_mnt, rc = -EOPNOTSUPP);
-               }
-       } else if (priv_dev_check_rdonly &&
-                  priv_dev_check_rdonly(osd_sb(o)->s_bdev)) {
-               CERROR("%s: underlying device %s is marked as "
-                      "read-only. Setup failed\n", name, dev);
-
-               GOTO(out_mnt, rc = -EROFS);
+               GOTO(out_mnt, rc = -EOPNOTSUPP);
        }
 
-       if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
-                                       LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
+       if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
                CERROR("%s: device %s is mounted w/o journal\n", name, dev);
                GOTO(out_mnt, rc = -EINVAL);
        }
 
 #ifdef LDISKFS_MOUNT_DIRDATA
-       if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
-                                        LDISKFS_FEATURE_INCOMPAT_DIRDATA))
+       if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
                LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
        else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
                CWARN("%s: device %s was upgraded from Lustre-1.x without "
@@ -7260,6 +7750,12 @@ static int osd_mount(const struct lu_env *env,
        if (lmd_flags & LMD_FLG_NOSCRUB)
                o->od_auto_scrub_interval = AS_NEVER;
 
+       if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
+               /* do not use pagecache with flash-backed storage */
+               o->od_writethrough_cache = 0;
+               o->od_read_cache = 0;
+       }
+
        GOTO(out, rc = 0);
 
 out_mnt:
@@ -7277,10 +7773,14 @@ static struct lu_device *osd_device_fini(const struct lu_env *env,
                                         struct lu_device *d)
 {
        struct osd_device *o = osd_dev(d);
+
        ENTRY;
 
+       osd_index_backup(env, o, false);
        osd_shutdown(env, o);
        osd_procfs_fini(o);
+       if (o->od_oi_table != NULL)
+               osd_oi_fini(osd_oti_get(env), o);
        osd_obj_map_fini(o);
        osd_umount(env, o);
 
@@ -7291,13 +7791,13 @@ static int osd_device_init0(const struct lu_env *env,
                            struct osd_device *o,
                            struct lustre_cfg *cfg)
 {
-       struct lu_device        *l = osd2lu_dev(o);
+       struct lu_device *l = osd2lu_dev(o);
        struct osd_thread_info *info;
-       int                     rc;
-       int                     cplen = 0;
+       int rc;
+       int cplen = 0;
 
        /* if the module was re-loaded, env can loose its keys */
-       rc = lu_env_refill((struct lu_env *) env);
+       rc = lu_env_refill((struct lu_env *)env);
        if (rc)
                GOTO(out, rc);
        info = osd_oti_get(env);
@@ -7313,10 +7813,13 @@ static int osd_device_init0(const struct lu_env *env,
        INIT_LIST_HEAD(&o->od_index_restore_list);
        spin_lock_init(&o->od_lock);
        o->od_index_backup_policy = LIBP_NONE;
+       o->od_t10_type = 0;
 
        o->od_read_cache = 1;
        o->od_writethrough_cache = 1;
        o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
+       o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
+       o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
        o->od_auto_scrub_interval = AS_DEFAULT;
 
        cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
@@ -7338,6 +7841,10 @@ static int osd_device_init0(const struct lu_env *env,
        if (rc != 0)
                GOTO(out, rc);
 
+       /* Can only check block device after mount */
+       o->od_nonrotational =
+               blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
+
        rc = osd_obj_map_init(env, o);
        if (rc != 0)
                GOTO(out_mnt, rc);
@@ -7370,11 +7877,29 @@ static int osd_device_init0(const struct lu_env *env,
        LASSERT(l->ld_site->ls_linkage.prev != NULL);
 
        /* initialize quota slave instance */
-       o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
-                                    o->od_proc_entry);
-       if (IS_ERR(o->od_quota_slave)) {
-               rc = PTR_ERR(o->od_quota_slave);
-               o->od_quota_slave = NULL;
+       /* currently it's no need to prepare qsd_instance_md for OST */
+       if (!o->od_is_ost) {
+               o->od_quota_slave_md = qsd_init(env, o->od_svname,
+                                               &o->od_dt_dev,
+                                               o->od_proc_entry, true);
+               if (IS_ERR(o->od_quota_slave_md)) {
+                       rc = PTR_ERR(o->od_quota_slave_md);
+                       o->od_quota_slave_md = NULL;
+                       GOTO(out_procfs, rc);
+               }
+       }
+
+       o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
+                                       o->od_proc_entry, false);
+
+       if (IS_ERR(o->od_quota_slave_dt)) {
+               if (o->od_quota_slave_md != NULL) {
+                       qsd_fini(env, o->od_quota_slave_md);
+                       o->od_quota_slave_md = NULL;
+               }
+
+               rc = PTR_ERR(o->od_quota_slave_dt);
+               o->od_quota_slave_dt = NULL;
                GOTO(out_procfs, rc);
        }
 
@@ -7395,11 +7920,11 @@ out:
 }
 
 static struct lu_device *osd_device_alloc(const struct lu_env *env,
-                                          struct lu_device_type *t,
-                                          struct lustre_cfg *cfg)
+                                         struct lu_device_type *t,
+                                         struct lustre_cfg *cfg)
 {
        struct osd_device *o;
-       int                rc;
+       int rc;
 
        OBD_ALLOC_PTR(o);
        if (o == NULL)
@@ -7407,8 +7932,10 @@ static struct lu_device *osd_device_alloc(const struct lu_env *env,
 
        rc = dt_device_init(&o->od_dt_dev, t);
        if (rc == 0) {
-               /* Because the ctx might be revived in dt_device_init,
-                * refill the env here */
+               /*
+                * Because the ctx might be revived in dt_device_init,
+                * refill the env here
+                */
                lu_env_refill((struct lu_env *)env);
                rc = osd_device_init0(env, o, cfg);
                if (rc)
@@ -7422,29 +7949,30 @@ static struct lu_device *osd_device_alloc(const struct lu_env *env,
 }
 
 static struct lu_device *osd_device_free(const struct lu_env *env,
-                                         struct lu_device *d)
+                                        struct lu_device *d)
 {
-        struct osd_device *o = osd_dev(d);
-        ENTRY;
+       struct osd_device *o = osd_dev(d);
+
+       ENTRY;
 
        /* XXX: make osd top device in order to release reference */
        d->ld_site->ls_top_dev = d;
        lu_site_purge(env, d->ld_site, -1);
-       if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
-               LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
-               lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
-       }
+       lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
+                     D_ERROR, lu_cdebug_printer);
        lu_site_fini(&o->od_site);
-        dt_device_fini(&o->od_dt_dev);
-        OBD_FREE_PTR(o);
-        RETURN(NULL);
+       dt_device_fini(&o->od_dt_dev);
+       OBD_FREE_PTR(o);
+       RETURN(NULL);
 }
 
 static int osd_process_config(const struct lu_env *env,
-                              struct lu_device *d, struct lustre_cfg *cfg)
+                             struct lu_device *d, struct lustre_cfg *cfg)
 {
-       struct osd_device               *o = osd_dev(d);
-       int                             rc;
+       struct osd_device *o = osd_dev(d);
+       ssize_t count;
+       int rc;
+
        ENTRY;
 
        switch (cfg->lcfg_command) {
@@ -7452,25 +7980,25 @@ static int osd_process_config(const struct lu_env *env,
                rc = osd_mount(env, o, cfg);
                break;
        case LCFG_CLEANUP:
-               /* For the case LCFG_PRE_CLEANUP is not called in advance,
-                * that may happend if hit failure during mount process. */
+               /*
+                * For the case LCFG_PRE_CLEANUP is not called in advance,
+                * that may happend if hit failure during mount process.
+                */
                osd_index_backup(env, o, false);
                lu_dev_del_linkage(d->ld_site, d);
                rc = osd_shutdown(env, o);
                break;
        case LCFG_PARAM:
                LASSERT(&o->od_dt_dev);
-               rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
-                                             cfg, &o->od_dt_dev);
-               if (rc > 0 || rc == -ENOSYS) {
-                       rc = class_process_proc_param(PARAM_OST,
-                                                     lprocfs_osd_obd_vars,
-                                                     cfg, &o->od_dt_dev);
-                       if (rc > 0)
-                               rc = 0;
-               }
+               count  = class_modify_config(cfg, PARAM_OSD,
+                                            &o->od_dt_dev.dd_kobj);
+               if (count < 0)
+                       count = class_modify_config(cfg, PARAM_OST,
+                                                   &o->od_dt_dev.dd_kobj);
+               rc = count > 0 ? 0 : count;
                break;
        case LCFG_PRE_CLEANUP:
+               osd_scrub_stop(o);
                osd_index_backup(env, o,
                                 o->od_index_backup_policy != LIBP_NONE);
                rc = 0;
@@ -7483,18 +8011,24 @@ static int osd_process_config(const struct lu_env *env,
 }
 
 static int osd_recovery_complete(const struct lu_env *env,
-                                 struct lu_device *d)
+                                struct lu_device *d)
 {
-       struct osd_device       *osd = osd_dev(d);
-       int                      rc = 0;
+       struct osd_device *osd = osd_dev(d);
+       int rc = 0;
+
        ENTRY;
 
-       if (osd->od_quota_slave == NULL)
+       if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
                RETURN(0);
 
-       /* start qsd instance on recovery completion, this notifies the quota
-        * slave code that we are about to process new requests now */
-       rc = qsd_start(env, osd->od_quota_slave);
+       /*
+        * start qsd instance on recovery completion, this notifies the quota
+        * slave code that we are about to process new requests now
+        */
+       rc = qsd_start(env, osd->od_quota_slave_dt);
+       if (rc == 0 && osd->od_quota_slave_md != NULL)
+               rc = qsd_start(env, osd->od_quota_slave_md);
+
        RETURN(rc);
 }
 
@@ -7505,9 +8039,10 @@ static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
                           struct obd_device *obd, struct obd_uuid *cluuid,
                           struct obd_connect_data *data, void *localdata)
 {
-       struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
-       struct lustre_handle  conn;
-       int                   rc;
+       struct osd_device *osd = osd_dev(obd->obd_lu_dev);
+       struct lustre_handle conn;
+       int rc;
+
        ENTRY;
 
        CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
@@ -7533,7 +8068,8 @@ static int osd_obd_disconnect(struct obd_export *exp)
 {
        struct obd_device *obd = exp->exp_obd;
        struct osd_device *osd = osd_dev(obd->obd_lu_dev);
-       int                rc, release = 0;
+       int rc, release = 0;
+
        ENTRY;
 
        /* Only disconnect the underlying layers on the final disconnect. */
@@ -7553,19 +8089,28 @@ static int osd_obd_disconnect(struct obd_export *exp)
 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
                       struct lu_device *dev)
 {
-       struct osd_device       *osd    = osd_dev(dev);
-       struct lr_server_data   *lsd    =
+       struct osd_device *osd = osd_dev(dev);
+       struct lr_server_data *lsd =
                        &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
-       int                      result = 0;
+       int result = 0;
+
        ENTRY;
 
-       if (osd->od_quota_slave != NULL) {
-               /* set up quota slave objects */
-               result = qsd_prepare(env, osd->od_quota_slave);
+       if (osd->od_quota_slave_md != NULL) {
+               /* set up quota slave objects for inode */
+               result = qsd_prepare(env, osd->od_quota_slave_md);
                if (result != 0)
                        RETURN(result);
        }
 
+       if (osd->od_quota_slave_dt != NULL) {
+               /* set up quota slave objects for block */
+               result = qsd_prepare(env, osd->od_quota_slave_dt);
+               if (result != 0)
+                       RETURN(result);
+       }
+
+
        if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
                if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
@@ -7586,49 +8131,58 @@ static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
        RETURN(result);
 }
 
-static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
-                        struct lu_fid *fid, struct md_op_data *op_data)
+/**
+ * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
+ *
+ * Allocate FID.
+ *
+ * see include/lu_object.h for the details.
+ */
+static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
+                        struct lu_fid *fid, struct lu_object *parent,
+                        const struct lu_name *name)
 {
-       struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
+       struct osd_device *osd = osd_dev(d);
 
        return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
 }
 
 static const struct lu_object_operations osd_lu_obj_ops = {
-        .loo_object_init      = osd_object_init,
-        .loo_object_delete    = osd_object_delete,
-        .loo_object_release   = osd_object_release,
-        .loo_object_free      = osd_object_free,
-        .loo_object_print     = osd_object_print,
-        .loo_object_invariant = osd_object_invariant
+       .loo_object_init      = osd_object_init,
+       .loo_object_delete    = osd_object_delete,
+       .loo_object_release   = osd_object_release,
+       .loo_object_free      = osd_object_free,
+       .loo_object_print     = osd_object_print,
+       .loo_object_invariant = osd_object_invariant
 };
 
 const struct lu_device_operations osd_lu_ops = {
-        .ldo_object_alloc      = osd_object_alloc,
-        .ldo_process_config    = osd_process_config,
-        .ldo_recovery_complete = osd_recovery_complete,
-        .ldo_prepare           = osd_prepare,
+       .ldo_object_alloc      = osd_object_alloc,
+       .ldo_process_config    = osd_process_config,
+       .ldo_recovery_complete = osd_recovery_complete,
+       .ldo_prepare           = osd_prepare,
+       .ldo_fid_alloc         = osd_fid_alloc,
 };
 
 static const struct lu_device_type_operations osd_device_type_ops = {
-        .ldto_init = osd_type_init,
-        .ldto_fini = osd_type_fini,
+       .ldto_init = osd_type_init,
+       .ldto_fini = osd_type_fini,
 
-        .ldto_start = osd_type_start,
-        .ldto_stop  = osd_type_stop,
+       .ldto_start = osd_type_start,
+       .ldto_stop  = osd_type_stop,
 
-        .ldto_device_alloc = osd_device_alloc,
-        .ldto_device_free  = osd_device_free,
+       .ldto_device_alloc = osd_device_alloc,
+       .ldto_device_free  = osd_device_free,
 
-        .ldto_device_init    = osd_device_init,
-        .ldto_device_fini    = osd_device_fini
+       .ldto_device_init = osd_device_init,
+       .ldto_device_fini = osd_device_fini
 };
 
 static struct lu_device_type osd_device_type = {
-        .ldt_tags     = LU_DEVICE_DT,
+       .ldt_tags     = LU_DEVICE_DT,
        .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
-        .ldt_ops      = &osd_device_type_ops,
-        .ldt_ctx_tags = LCT_LOCAL,
+       .ldt_ops      = &osd_device_type_ops,
+       .ldt_ctx_tags = LCT_LOCAL,
 };
 
 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
@@ -7636,28 +8190,53 @@ static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
        struct osd_device *osd = osd_dev(obd->obd_lu_dev);
        struct super_block *sb = osd_sb(osd);
 
-       return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
+       return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
 }
 
 /*
  * lprocfs legacy support.
  */
-static struct obd_ops osd_obd_device_ops = {
+static const struct obd_ops osd_obd_device_ops = {
        .o_owner = THIS_MODULE,
        .o_connect      = osd_obd_connect,
        .o_disconnect   = osd_obd_disconnect,
-       .o_fid_alloc    = osd_fid_alloc,
        .o_health_check = osd_health_check,
 };
 
+static ssize_t track_declares_assert_show(struct kobject *kobj,
+                                  struct attribute *attr,
+                                  char *buf)
+{
+       return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
+}
+
+static ssize_t track_declares_assert_store(struct kobject *kobj,
+                                          struct attribute *attr,
+                                          const char *buffer, size_t count)
+{
+       bool track_declares_assert;
+       int rc;
+
+       rc = kstrtobool(buffer, &track_declares_assert);
+       if (rc)
+               return rc;
+
+       ldiskfs_track_declares_assert = track_declares_assert;
+
+       return count;
+}
+LUSTRE_RW_ATTR(track_declares_assert);
+
 static int __init osd_init(void)
 {
+       struct kobject *kobj;
        int rc;
 
-       CLASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
+       BUILD_BUG_ON(BH_DXLock >=
+                    sizeof(((struct buffer_head *)0)->b_state) * 8);
 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
        /* please, try to keep osd_thread_info smaller than a page */
-       CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
+       BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
 #endif
 
        osd_oi_mod_init();
@@ -7667,22 +8246,40 @@ static int __init osd_init(void)
                return rc;
 
 #ifdef CONFIG_KALLSYMS
-       priv_dev_set_rdonly = (void *)kallsyms_lookup_name("dev_set_rdonly");
-       priv_dev_check_rdonly = (void *)kallsyms_lookup_name("dev_check_rdonly");
-       /* Clear readonly is unused at this time */
-       /*priv_dev_clear_rdonly = (void *)kallsyms_lookup_name("dev_clear_rdonly");*/
+       priv_security_file_alloc =
+               (void *)kallsyms_lookup_name("security_file_alloc");
 #endif
 
-       rc = class_register_type(&osd_obd_device_ops, NULL, true,
-                                lprocfs_osd_module_vars,
+       rc = class_register_type(&osd_obd_device_ops, NULL, true, NULL,
                                 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
-       if (rc)
+       if (rc) {
                lu_kmem_fini(ldiskfs_caches);
+               return rc;
+       }
+
+       kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
+       if (kobj) {
+               rc = sysfs_create_file(kobj,
+                                      &lustre_attr_track_declares_assert.attr);
+               kobject_put(kobj);
+               if (rc) {
+                       CWARN("osd-ldiskfs: track_declares_assert failed to register with sysfs\n");
+                       rc = 0;
+               }
+       }
        return rc;
 }
 
 static void __exit osd_exit(void)
 {
+       struct kobject *kobj;
+
+       kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
+       if (kobj) {
+               sysfs_remove_file(kobj,
+                                 &lustre_attr_track_declares_assert.attr);
+               kobject_put(kobj);
+       }
        class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
        lu_kmem_fini(ldiskfs_caches);
 }