Whamcloud - gitweb
LU-14734 osd-ldiskfs: enable large_dir automatically
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
index b28dd17..3b46322 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/osd/osd_handler.c
  *
 
 #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>
@@ -211,14 +209,14 @@ osd_idc_add(const struct lu_env *env, struct osd_device *osd,
                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;
@@ -286,6 +284,76 @@ osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
        return idc;
 }
 
+static void osd_idc_dump_lma(const struct lu_env *env,
+                               struct osd_device *osd,
+                               unsigned long ino,
+                               bool check_in_oi)
+{
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
+       const struct lu_fid *fid;
+       struct osd_inode_id lid;
+       struct inode *inode;
+       int rc;
+
+       inode = osd_ldiskfs_iget(osd_sb(osd), ino);
+       if (IS_ERR(inode)) {
+               CERROR("%s: can't get inode %lu: rc = %d\n",
+                      osd->od_svname, ino, (int)PTR_ERR(inode));
+               return;
+       }
+       if (is_bad_inode(inode)) {
+               CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
+               goto put;
+       }
+       rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
+       if (rc) {
+               CERROR("%s: can't get LMA for %lu: rc = %d\n",
+                      osd->od_svname, ino, rc);
+               goto put;
+       }
+       fid = &loa->loa_lma.lma_self_fid;
+       LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
+                     PFID(fid), ino, (unsigned)inode->i_generation);
+       if (!check_in_oi)
+               goto put;
+       rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
+       if (rc) {
+               CERROR("%s: can't lookup "DFID": rc = %d\n",
+                      osd->od_svname, PFID(fid), rc);
+               goto put;
+       }
+       LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
+                     PFID(fid), lid.oii_ino, lid.oii_gen);
+put:
+       iput(inode);
+}
+
+static void osd_idc_dump_debug(const struct lu_env *env,
+                               struct osd_device *osd,
+                               const struct lu_fid *fid,
+                               unsigned long ino1,
+                               unsigned long ino2)
+{
+       struct osd_inode_id lid;
+
+       int rc;
+
+       rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
+       if (!rc) {
+               LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
+                       osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
+               osd_idc_dump_lma(env, osd, lid.oii_ino, false);
+       } else {
+               CERROR("%s: can't lookup "DFID": rc = %d\n",
+                      osd->od_svname, PFID(fid), rc);
+       }
+       if (ino1)
+               osd_idc_dump_lma(env, osd, ino1, true);
+       if (ino2)
+               osd_idc_dump_lma(env, osd, ino2, true);
+}
+
 /*
  * lookup mapping for given FID and fill it from the given object.
  * the object is lolcal by definition.
@@ -303,7 +371,12 @@ static int osd_idc_find_and_init(const struct lu_env *env,
                if (obj->oo_inode == NULL)
                        return 0;
                if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
-                       LASSERT(idc->oic_lid.oii_ino == 0);
+                       if (idc->oic_lid.oii_ino) {
+                               osd_idc_dump_debug(env, osd, fid,
+                                                  idc->oic_lid.oii_ino,
+                                                  obj->oo_inode->i_ino);
+                               return -EINVAL;
+                       }
                        idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
                        idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
                }
@@ -393,12 +466,11 @@ int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
                lustre_loa_swab(loa, true);
                /* Check LMA compatibility */
                if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
-                       CWARN("%s: unsupported incompat LMA feature(s) %#x "
-                             "for fid = "DFID", ino = %lu\n",
+                       rc = -EOPNOTSUPP;
+                       CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
                              osd_ino2name(inode),
                              lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
-                             PFID(&lma->lma_self_fid), inode->i_ino);
-                       rc = -EOPNOTSUPP;
+                             PFID(&lma->lma_self_fid), inode->i_ino, rc);
                }
        } else if (rc == 0) {
                rc = -ENODATA;
@@ -423,7 +495,7 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
         */
         /* 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));
@@ -443,10 +515,11 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
                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);
+               rc = -ENOENT;
+               CWARN("%s: bad inode: ino = %u: rc = %d\n",
+                     osd_dev2name(dev), id->oii_ino, rc);
                iput(inode);
-               inode = ERR_PTR(-ENOENT);
+               inode = ERR_PTR(rc);
        } else if ((rc = osd_attach_jinode(inode))) {
                iput(inode);
                inode = ERR_PTR(rc);
@@ -479,12 +552,13 @@ 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) {
                        fid = &loa->loa_lma.lma_self_fid;
                } else if (rc2 == -ENODATA) {
-                       if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
+                       if (unlikely(is_root_inode(parent))) {
                                fid = &info->oti_fid3;
                                lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
                        } else if (!osd->od_is_ost && osd->od_index == 0) {
@@ -495,19 +569,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;
@@ -530,7 +603,7 @@ osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
        if (!rc) {
                *fid = loa->loa_lma.lma_self_fid;
        } else if (rc == -ENODATA) {
-               if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
+               if (unlikely(is_root_inode(inode)))
                        lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
                else
                        lu_igif_build(fid, inode->i_ino, inode->i_generation);
@@ -559,7 +632,7 @@ static struct inode *osd_iget_check(struct osd_thread_info *info,
         */
 
 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))
@@ -855,19 +928,16 @@ 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;
 };
 
 /**
- * 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
@@ -887,7 +957,6 @@ static int osd_stripe_dir_filldir(void *buf,
        struct lu_fid *fid = &oti->oti_fid3;
        struct osd_inode_id *id = &oti->oti_id3;
        struct osd_device *dev = oclb->oclb_dev;
-       struct osd_idmap_cache *oic = oclb->oclb_oic;
        struct inode *inode;
 
        oclb->oclb_items++;
@@ -910,10 +979,7 @@ static int osd_stripe_dir_filldir(void *buf,
 
        iput(inode);
        osd_add_oi_cache(oti, dev, id, fid);
-       oic->oic_fid = *fid;
-       oic->oic_lid = *id;
-       oic->oic_dev = dev;
-       osd_oii_insert(dev, oic, true);
+       osd_oii_insert(dev, fid, id, true);
        oclb->oclb_found = true;
 
        return 1;
@@ -956,20 +1022,16 @@ static int osd_stripe_dir_filldir(void *buf,
  *    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 inode *inode)
 {
        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;
@@ -1008,40 +1070,24 @@ 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;
-#ifdef HAVE_ITERATE_SHARED
-               rc = fops->iterate_shared(filp, &oclb.ctx);
-#else
-               rc = fops->iterate(filp, &oclb.ctx);
-#endif
-               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)
-               CDEBUG(D_LFSCK, "%s: fail to check LMV EA, inode = %lu/%u,"
-                      DFID": rc = %d\n", osd_ino2name(inode),
-                      inode->i_ino, inode->i_generation,
-                      PFID(&oic->oic_fid), rc);
+               CDEBUG(D_LFSCK,
+                      "%s: cannot check LMV, ino = %lu/%u: rc = %d\n",
+                      osd_ino2name(inode), inode->i_ino, inode->i_generation,
+                      rc);
        else
                rc = 0;
 
@@ -1074,7 +1120,13 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
 
        LINVRNT(osd_invariant(obj));
        LASSERT(obj->oo_inode == NULL);
-       LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
+
+       if (fid_is_sane(fid) == 0) {
+               CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
+                      PFID(fid));
+               dump_stack();
+               RETURN(-EINVAL);
+       }
 
        dev = osd_dev(ldev);
        scrub = &dev->od_scrub.os_scrub;
@@ -1128,6 +1180,12 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
                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);
 
@@ -1183,18 +1241,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)
+               if (IS_ERR_OR_NULL(inode) || result) {
+                       osd_oii_insert(dev, fid, id, 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);
+               osd_oii_insert(dev, fid, id, true);
                goto found;
        }
 
@@ -1211,19 +1270,21 @@ trigger:
 
 join:
        rc1 = osd_scrub_start(env, dev, flags);
-       LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
-                     "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
+       CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
+                    "%s: trigger OI scrub by RPC for "DFID"/%u with flags %#x: rc = %d\n",
+                    osd_name(dev), PFID(fid), id->oii_ino, flags, rc1);
        if (rc1 && rc1 != -EALREADY)
                GOTO(out, result = -EREMCHG);
 
-       if (IS_ERR_OR_NULL(inode) || result)
+       if (IS_ERR_OR_NULL(inode) || result) {
+               osd_oii_insert(dev, fid, id, 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);
+       osd_oii_insert(dev, fid, id, true);
        goto found;
 
 check_lma:
@@ -1290,6 +1351,19 @@ 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
@@ -1307,6 +1381,8 @@ check_lma:
 
        if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
                result = -EREMCHG;
+               osd_scrub_refresh_mapping(info, dev, fid, id, DTO_INDEX_DELETE,
+                                         true, 0, NULL);
                goto trigger;
        }
 
@@ -1341,7 +1417,7 @@ found:
 
        if (S_ISDIR(inode->i_mode) &&
            (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
-               osd_check_lmv(info, dev, inode, oic);
+               osd_check_lmv(info, dev, inode);
 
        result = osd_attach_jinode(inode);
        if (result)
@@ -1392,6 +1468,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;
@@ -1421,6 +1504,7 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
                        result = 0;
                }
        }
+       obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
 
        LINVRNT(osd_invariant(obj));
        return result;
@@ -1447,8 +1531,6 @@ static int osd_oxc_get(struct osd_object *obj, const char *name,
        size_t namelen = strlen(name);
        int rc;
 
-       ENTRY;
-
        rcu_read_lock();
        list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
                if (namelen == tmp->oxe_namelen &&
@@ -1475,7 +1557,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();
 
@@ -1575,11 +1656,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);
 }
 
 /*
@@ -1601,16 +1681,6 @@ static void osd_index_fini(struct osd_object *o)
        }
 }
 
-/*
- * 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;
-}
-
 enum {
        OSD_TXN_OI_DELETE_CREDITS    = 20,
        OSD_TXN_INODE_DELETE_CREDITS = 20
@@ -1697,9 +1767,10 @@ static int osd_param_is_not_sane(const struct osd_device *dev,
 static void osd_trans_commit_cb(struct super_block *sb,
                                struct ldiskfs_journal_cb_entry *jcb, int error)
 {
-       struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
+       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 osd_device *osd = osd_dev(lud);
        struct dt_txn_commit_cb *dcb, *tmp;
 
        LASSERT(oh->ot_handle == NULL);
@@ -1719,17 +1790,13 @@ 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);
+       if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
+               wake_up(&osd->od_commit_cb_done);
        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)
 {
@@ -1765,6 +1832,7 @@ static struct thandle *osd_trans_create(const struct lu_env *env,
        th->th_dev = d;
        th->th_result = 0;
        oh->ot_credits = 0;
+       oh->oh_declared_ext = 0;
        INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
        INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
        INIT_LIST_HEAD(&oh->ot_trunc_locks);
@@ -1787,7 +1855,7 @@ 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;
 
-       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",
@@ -1846,7 +1914,7 @@ static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
 
        LASSERT(current->journal_info == NULL);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh != NULL);
        LASSERT(oh->ot_handle == NULL);
 
@@ -1864,7 +1932,7 @@ static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
                 */
                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));
@@ -1907,7 +1975,7 @@ static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
                oh->ot_handle = jh;
                LASSERT(oti->oti_txns == 0);
 
-               lu_device_get(&d->dd_lu_dev);
+               atomic_inc(&dev->od_commit_cb_in_flight);
                lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
                              "osd-tx", th);
                oti->oti_txns++;
@@ -1970,12 +2038,12 @@ static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
        struct osd_device *osd = osd_dt_dev(th->th_dev);
        struct qsd_instance *qsd = osd_def_qsd(osd);
        struct lquota_trans *qtrans;
-       struct list_head truncates = LIST_HEAD_INIT(truncates);
+       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;
 
@@ -2018,13 +2086,13 @@ static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
                if (!rc)
                        rc = rc2;
 
-               osd_process_truncates(&truncates);
+               osd_process_truncates(env, &truncates);
        } else {
                osd_trans_stop_cb(oh, th->th_result);
                OBD_FREE_PTR(oh);
        }
 
-       osd_trunc_unlock_all(&truncates);
+       osd_trunc_unlock_all(env, &truncates);
 
        /* inform the quota slave device that the transaction is stopping */
        qsd_op_end(env, qsd, qtrans);
@@ -2048,6 +2116,7 @@ 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);
 
+       LASSERT(oti->oti_ins_cache_depth > 0);
        oti->oti_ins_cache_depth--;
        /* reset OI cache for safety */
        if (oti->oti_ins_cache_depth == 0)
@@ -2060,8 +2129,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);
@@ -2100,6 +2169,9 @@ static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
        if (!inode)
                return;
 
+       if (osd_has_index(obj) &&  obj->oo_dt.do_index_ops == &osd_index_iam_ops)
+               ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
+
        uid = i_uid_read(inode);
        gid = i_gid_read(inode);
        projid = i_projid_read(inode);
@@ -2171,7 +2243,7 @@ static int osd_object_print(const struct lu_env *env, void *cookie,
  * 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);
@@ -2196,8 +2268,11 @@ int osd_statfs(const struct lu_env *env, struct dt_device *d,
                goto out;
 
        statfs_pack(sfs, ksfs);
-       if (unlikely(sb->s_flags & MS_RDONLY))
-               sfs->os_state |= OS_STATE_READONLY;
+       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
@@ -2211,7 +2286,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;
@@ -2267,12 +2342,12 @@ static void osd_conf_get(const struct lu_env *env,
         */
        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.
+        * 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 >> 2;
+       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;
@@ -2297,8 +2372,8 @@ static void osd_conf_get(const struct lu_env *env,
 #endif
                param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
 
-       if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE - ea_overhead)
-               param->ddp_max_ea_size = OBD_MAX_EA_SIZE - ea_overhead;
+       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
@@ -2348,16 +2423,17 @@ static void osd_conf_get(const struct lu_env *env,
                                        OBD_CKSUM_T10IP512 :
                                        OBD_CKSUM_T10IP4K;
                        } else {
-                               CERROR("%s: unsupported checksum type of "
-                                      "T10PI type '%s'",
+                               CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
                                       d->od_svname, name);
                        }
 
                } else {
-                       CERROR("%s: unsupported T10PI type '%s'",
+                       CERROR("%s: unsupported T10PI type '%s'\n",
                               d->od_svname, name);
                }
        }
+
+       param->ddp_has_lseek_data_hole = true;
 }
 
 static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
@@ -2412,10 +2488,14 @@ static int osd_commit_async(const struct lu_env *env,
        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.
@@ -2428,35 +2508,8 @@ static int osd_ro(const struct lu_env *env, struct dt_device *d)
 
        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);
-       }
-
-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);
 }
@@ -2599,29 +2652,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 = 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;
@@ -2642,10 +2684,71 @@ static void osd_inode_getattr(const struct lu_env *env,
                attr->la_flags |= LUSTRE_PROJINHERIT_FL;
 }
 
+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;
@@ -2661,9 +2764,17 @@ static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
                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,
@@ -2746,7 +2857,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,
@@ -2773,9 +2884,9 @@ static int osd_declare_attr_set(const struct lu_env *env,
                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,
+                       CDEBUG(D_QUOTA,
+                              "%s: enforce quota on UID %u, GID %u (quota space is %lld)\n",
+                              osd_ino2name(obj->oo_inode), attr->la_uid,
                               attr->la_gid, bspace);
 
                /* USERQUOTA */
@@ -2788,9 +2899,10 @@ static int osd_declare_attr_set(const struct lu_env *env,
                        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),
+               rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
                                          attr->la_gid, enforce, GRPQUOTA,
                                          ignore_edquot);
                if (rc)
@@ -2826,11 +2938,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;
@@ -2860,6 +2972,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
@@ -2874,7 +2993,8 @@ static int osd_inode_setattr(const struct lu_env *env,
 }
 
 #ifdef HAVE_PROJECT_QUOTA
-static int osd_transfer_project(struct inode *inode, __u32 projid)
+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);
@@ -2906,9 +3026,18 @@ static int osd_transfer_project(struct inode *inode, __u32 projid)
 
        raw_inode = ldiskfs_raw_inode(&iloc);
        if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
-               err = -EOVERFLOW;
-               brelse(iloc.bh);
-               return err;
+               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);
 
@@ -2925,7 +3054,8 @@ static int osd_transfer_project(struct inode *inode, __u32 projid)
 }
 #endif
 
-static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
+static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
+                             struct thandle *handle)
 {
        int rc;
 
@@ -2933,7 +3063,12 @@ static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
            (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
                struct iattr iattr;
 
-               ll_vfs_dq_init(inode);
+               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);
+
+               dquot_initialize(inode);
                iattr.ia_valid = 0;
                if (attr->la_valid & LA_UID)
                        iattr.ia_valid |= ATTR_UID;
@@ -2942,11 +3077,10 @@ 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 "
-                              "filesystem?\n", inode->i_sb->s_id, rc);
+                       CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
+                              osd_ino2name(inode), rc);
                        return rc;
                }
        }
@@ -2955,14 +3089,13 @@ 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 = osd_transfer_project(inode, attr->la_projid);
+               rc = osd_transfer_project(inode, attr->la_projid, handle);
 #else
                rc = -ENOTSUPP;
 #endif
                if (rc) {
-                       CERROR("%s: quota transfer failed: rc = %d. Is project "
-                              "enforcement enabled on the ldiskfs "
-                              "filesystem?\n", inode->i_sb->s_id, rc);
+                       CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
+                              osd_ino2name(inode), rc);
                        return rc;
                }
        }
@@ -3006,7 +3139,7 @@ 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);
+               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);
@@ -3016,7 +3149,7 @@ static int osd_attr_set(const struct lu_env *env,
 
        inode = obj->oo_inode;
 
-       rc = osd_quota_transfer(inode, attr);
+       rc = osd_quota_transfer(inode, attr, handle);
        if (rc)
                return rc;
 
@@ -3026,7 +3159,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);
@@ -3046,6 +3181,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) {
@@ -3061,7 +3199,6 @@ 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;
 }
@@ -3082,12 +3219,21 @@ static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
        struct osd_thandle *oth;
        struct dt_object *parent = NULL;
        struct inode *inode;
-       uid_t owner[2] = {0, 0};
+       struct iattr iattr = {
+               .ia_valid = ATTR_UID | ATTR_GID |
+                           ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
+               .ia_ctime.tv_sec = attr->la_ctime,
+               .ia_mtime.tv_sec = attr->la_mtime,
+               .ia_atime.tv_sec = attr->la_atime,
+               .ia_uid = GLOBAL_ROOT_UID,
+               .ia_gid = GLOBAL_ROOT_GID,
+       };
+       const struct osd_timespec omit = { .tv_nsec = UTIME_OMIT };
 
        if (attr->la_valid & LA_UID)
-               owner[0] = attr->la_uid;
+               iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
        if (attr->la_valid & LA_GID)
-               owner[1] = attr->la_gid;
+               iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
 
        LINVRNT(osd_invariant(obj));
        LASSERT(obj->oo_inode == NULL);
@@ -3107,10 +3253,18 @@ static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
            !dt_object_remote(hint->dah_parent))
                parent = hint->dah_parent;
 
+       /* if a time component is not valid set it to UTIME_OMIT */
+       if (!(attr->la_valid & LA_CTIME))
+               iattr.ia_ctime = omit;
+       if (!(attr->la_valid & LA_MTIME))
+               iattr.ia_mtime = omit;
+       if (!(attr->la_valid & LA_ATIME))
+               iattr.ia_atime = omit;
+
        inode = ldiskfs_create_inode(oth->ot_handle,
                                     parent ? osd_dt_obj(parent)->oo_inode :
                                              osd_sb(osd)->s_root->d_inode,
-                                    mode, owner);
+                                    mode, &iattr);
        if (!IS_ERR(inode)) {
                /* Do not update file c/mtime in ldiskfs. */
                inode->i_flags |= S_NOCMTIME;
@@ -3152,6 +3306,8 @@ 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);
+       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;
@@ -3279,7 +3435,6 @@ static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
        return result;
 }
 
-
 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)
@@ -3298,7 +3453,8 @@ static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
 }
 
 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;
@@ -3315,7 +3471,7 @@ static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
        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;
 
@@ -3329,7 +3485,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;
@@ -3366,7 +3522,7 @@ static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
        }
 
        if (likely(result == 0)) {
-               osd_attr_init(info, obj, attr, dof);
+               osd_attr_init(info, obj, attr, dof, th);
                osd_object_init0(obj);
        }
 
@@ -3394,13 +3550,29 @@ static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
 
        LASSERT(obj->oo_inode != NULL);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OI_ENOSPC))
+               return -ENOSPC;
+
+       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;
@@ -3441,13 +3613,15 @@ static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
                              struct thandle *handle)
 {
        struct osd_thandle *oh;
+       struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
+       int credits;
        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);
 
        /*
@@ -3455,16 +3629,32 @@ static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
         * 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.
+        *
+        * The default ACL initialization may consume an additional 16 blocks
+        */
+       credits = osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
+                 ((dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
+
+       /**
+        * While ldiskfs_new_inode() calls ldiskfs_init_acl() we have to add
+        * credits for possible default ACL creation in new inode
         */
-       osd_trans_declare_op(env, oh, OSD_OT_CREATE,
-                            osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
-                            (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
+       if (hint && hint->dah_acl_len)
+               credits += osd_calc_bkmap_credits(sb, NULL, 0, -1,
+                               (hint->dah_acl_len + sb->s_blocksize - 1) >>
+                               sb->s_blocksize_bits);
+
+       osd_trans_declare_op(env, oh, OSD_OT_CREATE, credits);
+
        /*
         * 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)),
@@ -3502,7 +3692,7 @@ static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
        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,
@@ -3553,7 +3743,7 @@ static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
 
        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));
@@ -3569,13 +3759,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);
@@ -3734,6 +3926,9 @@ static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
        __u32 saved_nlink = dir->i_nlink;
        int rc;
 
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_DOTDOT_ENOSPC))
+               return -ENOSPC;
+
        dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
        osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
 
@@ -3774,6 +3969,15 @@ static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
        struct osd_thread_info *info = osd_oti_get(env);
        struct inode *local;
        struct osd_thandle *oh;
+       struct iattr iattr = {
+               .ia_valid = ATTR_UID | ATTR_GID |
+                           ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
+               .ia_ctime.tv_nsec = UTIME_OMIT,
+               .ia_mtime.tv_nsec = UTIME_OMIT,
+               .ia_atime.tv_nsec = UTIME_OMIT,
+               .ia_uid = GLOBAL_ROOT_UID,
+               .ia_gid = GLOBAL_ROOT_GID,
+       };
        int rc;
 
        ENTRY;
@@ -3782,8 +3986,8 @@ static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
        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,
-                                    NULL);
+       local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode,
+                                    type, &iattr);
        if (IS_ERR(local)) {
                CERROR("%s: create local error %d\n", osd_name(osd),
                       (int)PTR_ERR(local));
@@ -3802,9 +4006,9 @@ static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
         * debugging if we need to determine where this symlink came from.
         */
        if (S_ISLNK(type)) {
-               CLASSERT(LDISKFS_N_BLOCKS * 4 >= FID_LEN + 1);
-               rc = snprintf((char *)LDISKFS_I(local)->i_data,
-                             LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
+               BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
+               rc = scnprintf((char *)LDISKFS_I(local)->i_data,
+                              LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
 
                i_size_write(local, rc);
                LDISKFS_I(local)->i_disksize = rc;
@@ -3815,11 +4019,10 @@ static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
 #ifdef HAVE_PROJECT_QUOTA
        if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
            i_projid_read(pobj->oo_inode) != 0) {
-               rc = osd_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 "
-                              "filesystem?\n", local->i_sb->s_id, rc);
+                       CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
+                              osd_ino2name(local), rc);
                        RETURN(ERR_PTR(rc));
                }
        }
@@ -3886,20 +4089,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);
 
@@ -3987,8 +4186,21 @@ static int osd_create(const struct lu_env *env, struct dt_object *dt,
                        obj->oo_dt.do_body_ops = &osd_body_ops;
        }
 
-       if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY))
+       if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY)) {
+               struct inode *inode = obj->oo_inode;
+
                result = __osd_oi_insert(env, obj, fid, th);
+               if (result && inode) {
+                       spin_lock(&obj->oo_guard);
+                       clear_nlink(inode);
+                       spin_unlock(&obj->oo_guard);
+                       osd_dirty_inode(inode, I_DIRTY_DATASYNC);
+                       ldiskfs_set_inode_state(inode,
+                                               LDISKFS_STATE_LUSTRE_DESTROY);
+                       iput(inode);
+                       obj->oo_inode = NULL;
+               }
+       }
 
        /*
         * a small optimization - dt_insert() isn't usually applied
@@ -4012,19 +4224,21 @@ static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
                               struct thandle *handle)
 {
        struct osd_thandle *oh;
+       int rc;
 
        /* 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,
                             osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
 
-       osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev), osd_dt_obj(dt));
+       rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
+                                  osd_dt_obj(dt));
 
-       return 0;
+       return rc;
 }
 
 /*
@@ -4046,7 +4260,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);
@@ -4075,7 +4289,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);
@@ -4094,7 +4308,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,
@@ -4122,7 +4336,10 @@ 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);
@@ -4148,7 +4365,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);
@@ -4273,7 +4490,7 @@ 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) {
@@ -4411,11 +4628,11 @@ static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
                        RETURN(fl);
 
                /* Remove old PFID EA entry firstly. */
-               ll_vfs_dq_init(inode);
-               rc = osd_removexattr(dentry, inode, 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);
+                       /* XATTR_NAME_FID is already absent */
+                       rc = 0;
                } else if (rc) {
                        RETURN(rc);
                }
@@ -4492,7 +4709,7 @@ static int osd_xattr_handle_linkea(const struct lu_env *env,
 
        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);
@@ -4565,7 +4782,7 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
                 * Version is set after all inode operations are finished,
                 * so we should mark it dirty here
                 */
-               ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
 
                RETURN(0);
        }
@@ -4661,7 +4878,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,
@@ -4715,10 +4932,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 = osd_removexattr(dentry, inode, name);
+               rc = ll_vfs_removexattr(dentry, inode, name);
        }
 
        osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
@@ -4736,21 +4953,12 @@ static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
 {
        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;
+       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);
 }
@@ -4760,6 +4968,10 @@ static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
        return 0;
 }
 
+static bool osd_check_stale(struct dt_object *dt)
+{
+       return false;
+}
 /*
  * Index operations.
  */
@@ -4948,6 +5160,7 @@ static const struct dt_object_operations osd_obj_ops = {
        .do_xattr_list          = osd_xattr_list,
        .do_object_sync         = osd_object_sync,
        .do_invalidate          = osd_invalidate,
+       .do_check_stale         = osd_check_stale,
 };
 
 static const struct dt_object_operations osd_obj_otable_it_ops = {
@@ -4962,7 +5175,7 @@ static int osd_index_declare_iam_delete(const struct lu_env *env,
 {
        struct osd_thandle *oh;
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle == NULL);
 
        /* Recycle  may cause additional three blocks to be changed. */
@@ -5010,7 +5223,7 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
        if (unlikely(ipd == NULL))
                RETURN(-ENOMEM);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_handle != NULL);
        LASSERT(oh->ot_handle->h_transaction != NULL);
 
@@ -5041,7 +5254,7 @@ static int osd_index_declare_ea_delete(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);
 
        credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
@@ -5166,7 +5379,7 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
        LASSERT(oh->ot_handle != NULL);
        LASSERT(oh->ot_handle->h_transaction != NULL);
 
-       ll_vfs_dq_init(dir);
+       dquot_initialize(dir);
        dentry = osd_child_dentry_get(env, obj,
                                      (char *)key, strlen((char *)key));
 
@@ -5203,11 +5416,21 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
        } else {
                rc = PTR_ERR(bh);
        }
+
+       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));
@@ -5294,7 +5517,7 @@ static int osd_index_declare_iam_insert(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);
 
        osd_trans_declare_op(env, oh, OSD_OT_INSERT,
@@ -5344,7 +5567,7 @@ static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
        if (unlikely(ipd == NULL))
                RETURN(-ENOMEM);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       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)) {
@@ -5394,14 +5617,13 @@ static int __osd_ea_add_rec(struct osd_thread_info *info,
        LASSERT(pobj->oo_inode);
 
        ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
-       if (unlikely(pobj->oo_inode ==
-                    osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
+       if (unlikely(osd_object_is_root(pobj)))
                ldp->edp_magic = 0;
        else
                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)) {
@@ -5542,6 +5764,10 @@ static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
                                              hlock, th);
                }
        }
+       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
@@ -5552,11 +5778,9 @@ static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
 
 static int
 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
-                     struct osd_idmap_cache *oic)
+                     const struct lu_fid *fid, struct osd_inode_id *id)
 {
        struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
-       struct lu_fid *fid = &oic->oic_fid;
-       struct osd_inode_id *id = &oic->oic_lid;
        struct inode *inode = NULL;
        int once = 0;
        bool insert;
@@ -5567,8 +5791,7 @@ osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
        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 ||
@@ -5609,7 +5832,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). */
@@ -5620,7 +5843,7 @@ trigger:
                        }
                }
 
-               rc = osd_oii_insert(dev, oic, insert);
+               rc = osd_oii_insert(dev, fid, id, insert);
                /*
                 * There is race condition between osd_oi_lookup and OI scrub.
                 * The OI scrub finished just after osd_oi_lookup() failure.
@@ -5633,7 +5856,7 @@ trigger:
                if (!S_ISDIR(inode->i_mode))
                        rc = 0;
                else
-                       rc = osd_check_lmv(oti, dev, inode, oic);
+                       rc = osd_check_lmv(oti, dev, inode);
 
                GOTO(out, rc);
        }
@@ -5641,10 +5864,9 @@ trigger:
        if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
                rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
                                     SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
-               CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
-                      "%s: trigger partial OI scrub for RPC inconsistency "
-                      "checking FID "DFID": rc = %d\n",
-                      osd_dev2name(dev), PFID(fid), rc);
+               CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
+                            "%s: trigger partial OI scrub for RPC inconsistency, checking FID "DFID"/%u: rc = %d\n",
+                            osd_dev2name(dev), PFID(fid), id->oii_ino, rc);
                if (rc == 0 || rc == -EALREADY)
                        goto again;
        }
@@ -5652,18 +5874,17 @@ trigger:
        GOTO(out, rc);
 
 out:
-       if (inode)
-               iput(inode);
+       iput(inode);
 
        RETURN(rc);
 }
 
 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
                               struct osd_device *dev,
-                              struct osd_idmap_cache *oic,
                               struct lu_fid *fid, __u32 ino)
 {
        struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
+       struct osd_idmap_cache *oic = &oti->oti_cache;
        struct inode *inode;
        int rc;
 
@@ -5854,13 +6075,12 @@ static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
        if (!IS_ERR(bh)) {
                struct osd_thread_info *oti = osd_oti_get(env);
                struct osd_inode_id *id = &oti->oti_id;
-               struct osd_idmap_cache *oic = &oti->oti_cache;
                struct osd_device *dev = osd_obj2dev(obj);
 
                ino = le32_to_cpu(de->inode);
                if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
                        brelse(bh);
-                       rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
+                       rc = osd_fail_fid_lookup(oti, dev, fid, ino);
                        GOTO(out, rc);
                }
 
@@ -5888,19 +6108,24 @@ static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
                        osd_id_gen(id, ino, OSD_OII_NOGEN);
                }
 
-               if (rc != 0 || osd_remote_fid(env, dev, fid)) {
-                       fid_zero(&oic->oic_fid);
-
+               if (rc != 0 || osd_remote_fid(env, dev, fid))
                        GOTO(out, rc);
-               }
 
-               osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
-               rc = osd_consistency_check(oti, dev, oic);
-               if (rc == -ENOENT)
-                       fid_zero(&oic->oic_fid);
-               else
+               rc = osd_consistency_check(oti, dev, fid, id);
+               if (rc != -ENOENT) {
                        /* Other error should not affect lookup result. */
                        rc = 0;
+
+                       /* Normal file mapping should be added into OI cache
+                        * after FID in LMA check, but for local files like
+                        * hsm_actions, their FIDs are not stored in OI files,
+                        * see osd_initial_OI_scrub(), and here is the only
+                        * place to load mapping into OI cache.
+                        */
+                       if (!fid_is_namespace_visible(fid))
+                               osd_add_oi_cache(osd_oti_get(env),
+                                                osd_obj2dev(obj), id, fid);
+               }
        } else {
                rc = PTR_ERR(bh);
        }
@@ -5935,7 +6160,7 @@ 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];
@@ -6095,6 +6320,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);
 }
 
@@ -6376,36 +6602,23 @@ static const struct dt_index_operations osd_index_iam_ops = {
        }
 };
 
-
-/**
- * Creates or initializes iterator context.
- *
- * \retval struct osd_it_ea, iterator structure on success
- *
- */
-static struct dt_it *osd_it_ea_init(const struct lu_env *env,
-                                   struct dt_object *dt,
-                                   __u32 attr)
+struct osd_it_ea *osd_it_dir_init(const struct lu_env *env,
+                                 struct inode *inode, __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;
 
        ENTRY;
 
-       if (!dt_object_exists(dt) || obj->oo_destroyed)
-               RETURN(ERR_PTR(-ENOENT));
-
        OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
        if (oie == NULL)
                RETURN(ERR_PTR(-ENOMEM));
        obj_dentry = &oie->oie_dentry;
 
-       obj_dentry->d_inode = obj->oo_inode;
-       obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
+       obj_dentry->d_inode = inode;
+       obj_dentry->d_sb = inode->i_sb;
        obj_dentry->d_name.hash = 0;
 
        oie->oie_rd_dirent       = 0;
@@ -6419,7 +6632,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 = NULL;
 
        file = &oie->oie_file;
 
@@ -6429,14 +6642,56 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
        else
                file->f_mode    = FMODE_32BITHASH;
        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_mapping         = inode->i_mapping;
+       file->f_op              = inode->i_fop;
+       file->f_inode = inode;
 
+       RETURN(oie);
+}
+
+/**
+ * Creates or initializes iterator context.
+ *
+ * \retval struct osd_it_ea, iterator structure on success
+ *
+ */
+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 lu_object *lo = &dt->do_lu;
+       struct osd_it_ea *oie;
+
+       ENTRY;
+
+       if (!dt_object_exists(dt) || obj->oo_destroyed)
+               RETURN(ERR_PTR(-ENOENT));
+
+       oie = osd_it_dir_init(env, obj->oo_inode, attr);
+       if (IS_ERR(oie))
+               RETURN((struct dt_it *)oie);
+
+       oie->oie_obj = obj;
        lu_object_get(lo);
        RETURN((struct dt_it *)oie);
 }
 
+void osd_it_dir_fini(const struct lu_env *env, struct osd_it_ea *oie,
+                    struct inode *inode)
+{
+       struct osd_thread_info *info = osd_oti_get(env);
+
+       ENTRY;
+       oie->oie_file.f_op->release(inode, &oie->oie_file);
+       if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
+               OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
+       else
+               info->oti_it_ea_buf_used = 0;
+       OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
+       EXIT;
+}
+
 /**
  * Destroy or finishes iterator context.
  *
@@ -6444,19 +6699,13 @@ 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;
 
        ENTRY;
-       oie->oie_file.f_op->release(inode, &oie->oie_file);
+       osd_it_dir_fini(env, (struct osd_it_ea *)di, inode);
        osd_object_put(env, obj);
-       if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
-               OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
-       else
-               info->oti_it_ea_buf_used = 0;
-       OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
        EXIT;
 }
 
@@ -6493,13 +6742,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.
  *
@@ -6537,7 +6784,8 @@ static int osd_ldiskfs_filldir(void *buf,
 
        /* "." is just the object itself. */
        if (namelen == 1 && name[0] == '.') {
-               *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
+               if (obj != NULL)
+                       *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);
                if (osd_fid_unpack(fid, rec) != 0)
@@ -6548,7 +6796,8 @@ static int osd_ldiskfs_filldir(void *buf,
        d_type &= ~LDISKFS_DIRENT_LUFID;
 
        /* NOT export local root. */
-       if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
+       if (obj != NULL &&
+           unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
                ino = obj->oo_inode->i_ino;
                *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
        }
@@ -6566,7 +6815,7 @@ static int osd_ldiskfs_filldir(void *buf,
 }
 
 /**
- * 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
@@ -6575,19 +6824,15 @@ static int osd_ldiskfs_filldir(void *buf,
  * \retval -ve on error
  * \retval +1 reach the end of entry
  */
-static int osd_ldiskfs_it_fill(const struct lu_env *env,
-                              const struct dt_it *di)
+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;
        struct osd_filldir_cbs buf = {
-#ifdef HAVE_DIR_CONTEXT
                .ctx.actor = osd_ldiskfs_filldir,
-#endif
                .it = it
        };
 
@@ -6595,30 +6840,27 @@ static int osd_ldiskfs_it_fill(const struct lu_env *env,
        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 {
-               down_read(&obj->oo_ext_idx_sem);
+       if (obj) {
+               if (obj->oo_hl_head != NULL) {
+                       hlock = osd_oti_get(env)->oti_hlock;
+                       ldiskfs_htree_lock(hlock, obj->oo_hl_head,
+                                          obj->oo_inode,
+                                          LDISKFS_HLOCK_READDIR);
+               } else {
+                       down_read(&obj->oo_ext_idx_sem);
+               }
        }
 
-#ifdef HAVE_DIR_CONTEXT
-       buf.ctx.pos = filp->f_pos;
-#ifdef HAVE_ITERATE_SHARED
-       rc = inode->i_fop->iterate_shared(filp, &buf.ctx);
-#else
-       rc = inode->i_fop->iterate(filp, &buf.ctx);
-#endif
-       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)
+               GOTO(unlock, rc);
 
-       if (hlock != NULL)
-               ldiskfs_htree_unlock(hlock);
-       else
-               up_read(&obj->oo_ext_idx_sem);
+       filp->f_flags |= O_NOATIME;
+       filp->f_mode |= FMODE_NONOTIFY;
+       rc = iterate_dir(filp, &buf.ctx);
+       if (rc)
+               GOTO(unlock, rc);
 
        if (it->oie_rd_dirent == 0) {
                /*
@@ -6632,12 +6874,19 @@ static int osd_ldiskfs_it_fill(const struct lu_env *env,
                it->oie_dirent = it->oie_buf;
                it->oie_it_dirent = 1;
        }
+unlock:
+       if (obj) {
+               if (hlock != NULL)
+                       ldiskfs_htree_unlock(hlock);
+               else
+                       up_read(&obj->oo_ext_idx_sem);
+       }
 
        RETURN(rc);
 }
 
 /**
- * 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.
  *
@@ -6700,6 +6949,11 @@ static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
        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) >=
@@ -6768,18 +7022,17 @@ 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!
         */
        if (rc != 0)
-               CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
-                      "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
-                      osd_ino2name(inode),
-                      dir->i_ino, dir->i_generation, namelen,
-                      dentry->d_name.name, PFID(fid), rc);
+               CDEBUG(D_LFSCK,
+                      "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
+                      osd_ino2name(inode), dir->i_ino, dir->i_generation,
+                      namelen, dentry->d_name.name, PFID(fid), rc);
 
        RETURN(rc);
 }
@@ -7190,8 +7443,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);
                }
        }
 
@@ -7201,15 +7452,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);
 }
 
@@ -7250,7 +7492,7 @@ static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
 }
 
 /**
- * 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.
  *
@@ -7366,11 +7608,17 @@ static void osd_key_fini(const struct lu_context *ctx,
        if (info->oti_dio_pages) {
                int i;
                for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
-                       if (info->oti_dio_pages[i])
-                               __free_page(info->oti_dio_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(info->oti_dio_pages,
-                        sizeof(struct page *) * PTLRPC_MAX_BRW_PAGES);
+               OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
+                                        PTLRPC_MAX_BRW_PAGES);
        }
 
        if (info->oti_inode != NULL)
@@ -7384,7 +7632,7 @@ static void osd_key_fini(const struct lu_context *ctx,
        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;
        }
@@ -7426,7 +7674,7 @@ static int osd_device_init(const struct lu_env *env, struct lu_device *d,
 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;
+       int rc = 0;
 
        ENTRY;
 
@@ -7440,13 +7688,8 @@ 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) {
                /*
@@ -7503,6 +7746,8 @@ static void osd_umount(const struct lu_env *env, struct osd_device *o)
        if (o->od_mnt != NULL) {
                shrink_dcache_sb(osd_sb(o));
                osd_sync(env, &o->od_dt_dev);
+               wait_event(o->od_commit_cb_done,
+                         !atomic_read(&o->od_commit_cb_in_flight));
 
                mntput(o->od_mnt);
                o->od_mnt = NULL;
@@ -7511,21 +7756,35 @@ static void osd_umount(const struct lu_env *env, struct osd_device *o)
        EXIT;
 }
 
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
+# ifndef LDISKFS_HAS_INCOMPAT_FEATURE
+/* Newer kernels provide the ldiskfs_set_feature_largedir() wrapper already,
+ * which calls ldiskfs_update_dynamic_rev() to update ancient filesystems.
+ * All ldiskfs filesystems are already v2, so it is a no-op and unnecessary.
+ * This avoids maintaining patches to export this otherwise-useless function.
+ */
+void ldiskfs_update_dynamic_rev(struct super_block *sb)
+{
+       /* do nothing */
+}
+# endif
+#endif
+
 static int osd_mount(const struct lu_env *env,
                     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;
+       unsigned long page, s_flags = 0, lmd_flags = 0;
        struct page *__page;
        struct file_system_type *type;
        char *options = NULL;
-       char *str;
+       const 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;
+       int rc = 0, force_over_1024tb = 0;
 
        ENTRY;
 
@@ -7536,11 +7795,9 @@ static int osd_mount(const struct lu_env *env,
                RETURN(-E2BIG);
        strcpy(o->od_mntdev, dev);
 
-       str = lustre_cfg_string(cfg, 2);
-       s_flags = simple_strtoul(str, NULL, 0);
-       str = strstr(str, ":");
-       if (str)
-               lmd_flags = simple_strtoul(str + 1, NULL, 0);
+       str = lustre_cfg_buf(cfg, 2);
+       sscanf(str, "%lu:%lu", &s_flags, &lmd_flags);
+
        opts = lustre_cfg_string(cfg, 3);
 #ifdef __BIG_ENDIAN
        if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
@@ -7552,23 +7809,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)
@@ -7578,7 +7834,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",
@@ -7590,9 +7846,11 @@ static int osd_mount(const struct lu_env *env,
                        "force_over_128tb",
                        "force_over_256tb",
                        "force_over_512tb",
+                       "force_over_1024tb",
+                       "resetoi",
                        NULL
                };
-               strcat(options, opts);
+               strncat(options, opts, PAGE_SIZE);
                for (rc = 0, str = options; sout[rc]; ) {
                        char *op = strstr(str, sout[rc]);
 
@@ -7612,13 +7870,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) {
@@ -7637,33 +7895,18 @@ 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);
-
-                       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);
+               LCONSOLE_WARN("%s: not support dev_rdonly on this device\n",
+                             name);
 
-               GOTO(out_mnt, rc = -EROFS);
+               GOTO(out_mnt, rc = -EOPNOTSUPP);
        }
 
        if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
@@ -7671,6 +7914,7 @@ static int osd_mount(const struct lu_env *env,
                GOTO(out_mnt, rc = -EINVAL);
        }
 
+#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
 #ifdef LDISKFS_MOUNT_DIRDATA
        if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
                LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
@@ -7680,6 +7924,15 @@ static int osd_mount(const struct lu_env *env,
                      "downgrade to Lustre-1.x again, you can enable it via "
                      "'tune2fs -O dirdata device'\n", name, dev);
 #endif
+       /* enable large_dir on MDTs to avoid REMOTE_PARENT_DIR overflow,
+        * and on very large OSTs to avoid object directory overflow */
+       if (unlikely(!ldiskfs_has_feature_largedir(o->od_mnt->mnt_sb) &&
+                    !strstr(name, "MGS"))) {
+               ldiskfs_set_feature_largedir(o->od_mnt->mnt_sb);
+               LCONSOLE_INFO("%s: enable 'large_dir' feature on device '%s'\n",
+                             name, dev);
+       }
+#endif
        inode = osd_sb(o)->s_root->d_inode;
        lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
        rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
@@ -7720,6 +7973,10 @@ static struct lu_device *osd_device_fini(const struct lu_env *env,
        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);
+       if (o->od_extent_bytes_percpu)
+               free_percpu(o->od_extent_bytes_percpu);
        osd_obj_map_fini(o);
        osd_umount(env, o);
 
@@ -7732,8 +7989,10 @@ static int osd_device_init0(const struct lu_env *env,
 {
        struct lu_device *l = osd2lu_dev(o);
        struct osd_thread_info *info;
-       int rc;
        int cplen = 0;
+       char *opts = NULL;
+       bool restored = false;
+       int rc;
 
        /* if the module was re-loaded, env can loose its keys */
        rc = lu_env_refill((struct lu_env *)env);
@@ -7753,11 +8012,16 @@ static int osd_device_init0(const struct lu_env *env,
        spin_lock_init(&o->od_lock);
        o->od_index_backup_policy = LIBP_NONE;
        o->od_t10_type = 0;
+       init_waitqueue_head(&o->od_commit_cb_done);
 
        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;
+       /* default fallocate to unwritten extents: LU-14326/LU-14333 */
+       o->od_fallocate_zero_blocks = 0;
 
        cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
                        sizeof(o->od_svname));
@@ -7778,6 +8042,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);
@@ -7791,10 +8059,14 @@ static int osd_device_init0(const struct lu_env *env,
        if (rc != 0)
                GOTO(out_site, rc);
 
+       opts = lustre_cfg_string(cfg, 3);
+       if (opts && strstr(opts, "resetoi"))
+               restored = true;
+
        INIT_LIST_HEAD(&o->od_ios_list);
        /* setup scrub, including OI files initialization */
        o->od_in_init = 1;
-       rc = osd_scrub_setup(env, o);
+       rc = osd_scrub_setup(env, o, restored);
        o->od_in_init = 0;
        if (rc < 0)
                GOTO(out_site, rc);
@@ -7836,6 +8108,12 @@ static int osd_device_init0(const struct lu_env *env,
                GOTO(out_procfs, rc);
        }
 
+       o->od_extent_bytes_percpu = alloc_percpu(unsigned int);
+       if (!o->od_extent_bytes_percpu) {
+               rc = -ENOMEM;
+               GOTO(out_procfs, rc);
+       }
+
        RETURN(0);
 
 out_procfs:
@@ -7891,10 +8169,8 @@ static struct lu_device *osd_device_free(const struct lu_env *env,
        /* 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);
@@ -8066,10 +8342,18 @@ 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);
 }
@@ -8088,6 +8372,7 @@ const struct lu_device_operations osd_lu_ops = {
        .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 = {
@@ -8116,17 +8401,16 @@ 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,
 };
 
@@ -8159,10 +8443,11 @@ 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();
@@ -8172,12 +8457,11 @@ 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");
+       priv_security_file_alloc =
+               (void *)cfs_kallsyms_lookup_name("security_file_alloc");
 #endif
 
-       rc = class_register_type(&osd_obd_device_ops, NULL, true, NULL,
+       rc = class_register_type(&osd_obd_device_ops, NULL, true,
                                 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
        if (rc) {
                lu_kmem_fini(ldiskfs_caches);