Whamcloud - gitweb
LU-2211 quota: cap how long a thread can wait for quota
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
index 00e3408..819d963 100644 (file)
@@ -72,8 +72,7 @@
 
 /* llo_* api support */
 #include <md_object.h>
-/* dt_acct_features */
-#include <lquota.h>
+#include <lustre_quota.h>
 
 #ifdef HAVE_LDISKFS_PDO
 int ldiskfs_pdo = 1;
@@ -104,51 +103,6 @@ static int osd_object_invariant(const struct lu_object *l)
         return osd_invariant(osd_obj(l));
 }
 
-#ifdef HAVE_QUOTA_SUPPORT
-static inline void
-osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save, bool is_md)
-{
-       struct md_ucred *uc;
-        struct cred     *tc;
-
-       if (!is_md)
-               /* OFD support */
-               return;
-
-       uc = md_ucred(env);
-
-        LASSERT(uc != NULL);
-
-        save->oc_uid = current_fsuid();
-        save->oc_gid = current_fsgid();
-        save->oc_cap = current_cap();
-        if ((tc = prepare_creds())) {
-                tc->fsuid         = uc->mu_fsuid;
-                tc->fsgid         = uc->mu_fsgid;
-                commit_creds(tc);
-        }
-        /* XXX not suboptimal */
-        cfs_curproc_cap_unpack(uc->mu_cap);
-}
-
-static inline void
-osd_pop_ctxt(struct osd_ctxt *save, bool is_md)
-{
-        struct cred *tc;
-
-       if (!is_md)
-               /* OFD support */
-               return;
-
-        if ((tc = prepare_creds())) {
-                tc->fsuid         = save->oc_uid;
-                tc->fsgid         = save->oc_gid;
-                tc->cap_effective = save->oc_cap;
-                commit_creds(tc);
-        }
-}
-#endif
-
 /*
  * Concurrency: doesn't matter
  */
@@ -176,51 +130,6 @@ static int osd_root_get(const struct lu_env *env,
         return 0;
 }
 
-static inline int osd_qid_type(struct osd_thandle *oh, int i)
-{
-        return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
-}
-
-static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
-{
-        oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
-}
-
-void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
-                     int type, uid_t id, struct inode *inode)
-{
-#ifdef CONFIG_QUOTA
-        int i, allocated = 0;
-        struct osd_object *obj;
-
-        LASSERT(dt != NULL);
-        LASSERT(oh != NULL);
-        LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
-                 oh->ot_id_cnt);
-
-        /* id entry is allocated in the quota file */
-        if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
-                allocated = 1;
-
-        for (i = 0; i < oh->ot_id_cnt; i++) {
-                if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
-                        return;
-        }
-
-        if (unlikely(i >= OSD_MAX_UGID_CNT)) {
-                CERROR("more than %d uid/gids for a transaction?\n", i);
-                return;
-        }
-
-        oh->ot_id_array[i] = id;
-        osd_qid_set_type(oh, i, type);
-        oh->ot_id_cnt++;
-        obj = osd_dt_obj(dt);
-        oh->ot_credits += (allocated || id == 0) ?
-                1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
-#endif
-}
-
 /*
  * OSD object methods.
  */
@@ -241,11 +150,7 @@ static struct lu_object *osd_object_alloc(const struct lu_env *env,
 
                 l = &mo->oo_dt.do_lu;
                 dt_object_init(&mo->oo_dt, NULL, d);
-                if (osd_dev(d)->od_iop_mode)
-                        mo->oo_dt.do_ops = &osd_obj_ea_ops;
-                else
-                        mo->oo_dt.do_ops = &osd_obj_ops;
-
+               mo->oo_dt.do_ops = &osd_obj_ea_ops;
                 l->lo_ops = &osd_lu_obj_ops;
                 cfs_init_rwsem(&mo->oo_sem);
                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
@@ -366,12 +271,10 @@ osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
                return inode;
 
        rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
+       if (rc == -ENODATA)
+               return inode;
+
        if (rc != 0) {
-               if (rc == -ENODATA) {
-                       CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
-                              inode->i_ino, PFID(fid));
-                       rc = -EREMCHG;
-               }
                iput(inode);
                return ERR_PTR(rc);
        }
@@ -380,8 +283,9 @@ osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
                CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
                       PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
                iput(inode);
-               return ERR_PTR(EREMCHG);
+               return ERR_PTR(-EREMCHG);
        }
+
        return inode;
 }
 
@@ -411,27 +315,26 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
        info = osd_oti_get(env);
        LASSERT(info);
        oic = &info->oti_cache;
-       id  = &oic->oic_lid;
 
        if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
                RETURN(-ENOENT);
 
-       if (fid_is_norm(fid)) {
-               /* Search order: 1. per-thread cache. */
-               if (lu_fid_eq(fid, &oic->oic_fid)) {
-                       goto iget;
-               } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
-                       /* Search order: 2. OI scrub pending list. */
-                       result = osd_oii_lookup(dev, fid, id);
-                       if (result == 0)
-                               goto iget;
-               }
+       /* Search order: 1. per-thread cache. */
+       if (lu_fid_eq(fid, &oic->oic_fid)) {
+               id = &oic->oic_lid;
+               goto iget;
+       }
 
-               if (sf->sf_flags & SF_INCONSISTENT)
-                       verify = 1;
+       id = &info->oti_id;
+       if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
+               /* Search order: 2. OI scrub pending list. */
+               result = osd_oii_lookup(dev, fid, id);
+               if (result == 0)
+                       goto iget;
        }
 
-       fid_zero(&oic->oic_fid);
+       if (sf->sf_flags & SF_INCONSISTENT)
+               verify = 1;
 
        /*
         * Objects are created as locking anchors or place holders for objects
@@ -465,13 +368,14 @@ iget:
        if (IS_ERR(inode)) {
                result = PTR_ERR(inode);
                if (result == -ENOENT || result == -ESTALE) {
+                       fid_zero(&oic->oic_fid);
                        result = 0;
                } else if (result == -EREMCHG) {
 
 trigger:
                        if (thread_is_running(&scrub->os_thread)) {
                                result = -EINPROGRESS;
-                       } else if (!scrub->os_no_scrub) {
+                       } else if (!dev->od_noscrub) {
                                result = osd_scrub_start(dev);
                                LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
                                               "for "DFID", rc = %d [1]\n",
@@ -489,10 +393,9 @@ trigger:
 
         obj->oo_inode = inode;
         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
-        if (dev->od_iop_mode) {
-                obj->oo_compat_dot_created = 1;
-                obj->oo_compat_dotdot_created = 1;
-        }
+
+       obj->oo_compat_dot_created = 1;
+       obj->oo_compat_dotdot_created = 1;
 
         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
                GOTO(out, result = 0);
@@ -683,12 +586,12 @@ static void __osd_th_check_slow(void *oth, struct osd_device *dev,
 /*
  * Concurrency: doesn't access mutable data.
  */
-static int osd_param_is_sane(const struct osd_device *dev,
-                             const struct thandle *th)
+static int osd_param_is_not_sane(const struct osd_device *dev,
+                                const struct thandle *th)
 {
-        struct osd_thandle *oh;
-        oh = container_of0(th, struct osd_thandle, ot_super);
-        return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
+       struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
+
+       return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
 }
 
 /*
@@ -746,6 +649,8 @@ static struct thandle *osd_trans_create(const struct lu_env *env,
         th = ERR_PTR(-ENOMEM);
         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
         if (oh != NULL) {
+               oh->ot_quota_trans = &oti->oti_quota_trans;
+               memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
                 th = &oh->ot_super;
                 th->th_dev = d;
                 th->th_result = 0;
@@ -782,30 +687,48 @@ int osd_trans_start(const struct lu_env *env, struct dt_device *d,
         if (rc != 0)
                 GOTO(out, rc);
 
-        if (!osd_param_is_sane(dev, th)) {
+       if (unlikely(osd_param_is_not_sane(dev, th))) {
+               static unsigned long last_printed;
+               static int last_credits;
+
                CWARN("%.16s: too many transaction credits (%d > %d)\n",
                      LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
                      oh->ot_credits,
                      osd_journal(dev)->j_max_transaction_buffers);
-                /* XXX Limit the credits to 'max_transaction_buffers', and
-                 *     let the underlying filesystem to catch the error if
-                 *     we really need so many credits.
-                 *
-                 *     This should be removed when we can calculate the
-                 *     credits precisely. */
-                oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
 #ifdef OSD_TRACK_DECLARES
-                CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
-                       oh->ot_declare_attr_set, oh->ot_declare_punch,
-                       oh->ot_declare_xattr_set);
-                CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
-                       oh->ot_declare_create, oh->ot_declare_ref_add,
-                       oh->ot_declare_ref_del, oh->ot_declare_write);
-                CERROR("  insert: %d, delete: %d, destroy: %d\n",
-                       oh->ot_declare_insert, oh->ot_declare_delete,
-                       oh->ot_declare_destroy);
+               CWARN("  create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
+                     oh->ot_declare_create, oh->ot_declare_create_cred,
+                     oh->ot_declare_delete, oh->ot_declare_delete_cred,
+                     oh->ot_declare_destroy, oh->ot_declare_destroy_cred);
+               CWARN("  attr_set: %u/%u, xattr_set: %u/%u\n",
+                     oh->ot_declare_attr_set, oh->ot_declare_attr_set_cred,
+                     oh->ot_declare_xattr_set, oh->ot_declare_xattr_set_cred);
+               CWARN("  write: %u/%u, punch: %u/%u, quota %u/%u\n",
+                     oh->ot_declare_write, oh->ot_declare_write_cred,
+                     oh->ot_declare_punch, oh->ot_declare_punch_cred,
+                     oh->ot_declare_quota, oh->ot_declare_quota_cred);
+               CWARN("  insert: %u/%u, delete: %u/%u\n",
+                     oh->ot_declare_insert, oh->ot_declare_insert_cred,
+                     oh->ot_declare_delete, oh->ot_declare_destroy_cred);
+               CWARN("  ref_add: %u/%u, ref_del: %u/%u\n",
+                     oh->ot_declare_ref_add, oh->ot_declare_ref_add_cred,
+                     oh->ot_declare_ref_del, oh->ot_declare_ref_del_cred);
+
+               if (last_credits != oh->ot_credits &&
+                   time_after(jiffies, last_printed + 60 * HZ)) {
+                       libcfs_debug_dumpstack(NULL);
+                       last_credits = oh->ot_credits;
+                       last_printed = jiffies;
+               }
 #endif
-        }
+               /* XXX Limit the credits to 'max_transaction_buffers', and
+                *     let the underlying filesystem to catch the error if
+                *     we really need so many credits.
+                *
+                *     This should be removed when we can calculate the
+                *     credits precisely. */
+               oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
+       }
 
         /*
          * XXX temporary stuff. Some abstraction layer should
@@ -822,22 +745,6 @@ int osd_trans_start(const struct lu_env *env, struct dt_device *d,
                 lu_device_get(&d->dd_lu_dev);
                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
                                              "osd-tx", th);
-
-                /*
-                 * XXX: current rule is that we first start tx,
-                 *      then lock object(s), but we can't use
-                 *      this rule for data (due to locking specifics
-                 *      in ldiskfs). also in long-term we'd like to
-                 *      use usually-used (locks;tx) ordering. so,
-                 *      UGLY thing is that we'll use one ordering for
-                 *      data (ofd) and reverse ordering for metadata
-                 *      (mdd). then at some point we'll fix the latter
-                 */
-               if (dev->od_is_md) {
-                        LASSERT(oti->oti_r_locks == 0);
-                        LASSERT(oti->oti_w_locks == 0);
-                }
-
                 oti->oti_txns++;
                 rc = 0;
         } else {
@@ -856,16 +763,20 @@ static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
         struct osd_thandle     *oh;
         struct osd_thread_info *oti = osd_oti_get(env);
         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
-
+       struct qsd_instance    *qsd = oti->oti_dev->od_quota_slave;
         ENTRY;
 
         oh = container_of0(th, struct osd_thandle, ot_super);
 
+       if (qsd != NULL)
+               /* inform the quota slave device that the transaction is
+                * stopping */
+               qsd_op_end(env, qsd, oh->ot_quota_trans);
+       oh->ot_quota_trans = NULL;
+
         if (oh->ot_handle != NULL) {
                 handle_t *hdl = oh->ot_handle;
 
-                hdl->h_sync = th->th_sync;
-
                 /*
                  * add commit callback
                  * notice we don't do this in osd_trans_start()
@@ -876,23 +787,13 @@ static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
 
                 LASSERT(oti->oti_txns == 1);
                 oti->oti_txns--;
-                /*
-                 * XXX: current rule is that we first start tx,
-                 *      then lock object(s), but we can't use
-                 *      this rule for data (due to locking specifics
-                 *      in ldiskfs). also in long-term we'd like to
-                 *      use usually-used (locks;tx) ordering. so,
-                 *      UGLY thing is that we'll use one ordering for
-                 *      data (ofd) and reverse ordering for metadata
-                 *      (mdd). then at some point we'll fix the latter
-                 */
-               if (osd_dt_dev(th->th_dev)->od_is_md) {
-                        LASSERT(oti->oti_r_locks == 0);
-                        LASSERT(oti->oti_w_locks == 0);
-                }
                 rc = dt_txn_hook_stop(env, th);
                 if (rc != 0)
                         CERROR("Failure in transaction hook: %d\n", rc);
+
+               /* hook functions might modify th_sync */
+               hdl->h_sync = th->th_sync;
+
                 oh->ot_handle = NULL;
                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
                                   rc = ldiskfs_journal_stop(hdl));
@@ -951,8 +852,24 @@ static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
 
         osd_index_fini(obj);
         if (inode != NULL) {
+               struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
+               qid_t                    uid = inode->i_uid;
+               qid_t                    gid = inode->i_gid;
+
                 iput(inode);
                 obj->oo_inode = NULL;
+
+               if (qsd != NULL) {
+                       struct osd_thread_info  *info = osd_oti_get(env);
+                       struct lquota_id_info   *qi = &info->oti_qi;
+
+                       /* Release granted quota to master if necessary */
+                       qi->lqi_id.qid_uid = uid;
+                       qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
+
+                       qi->lqi_id.qid_uid = gid;
+                       qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
+               }
         }
 }
 
@@ -977,7 +894,8 @@ static int osd_object_print(const struct lu_env *env, void *cookie,
                 d = o->oo_dir->od_container.ic_descr;
         else
                 d = NULL;
-        return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
+       return (*p)(env, cookie,
+                   LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
                     o, o->oo_inode,
                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
                     o->oo_inode ? o->oo_inode->i_generation : 0,
@@ -995,7 +913,7 @@ int osd_statfs(const struct lu_env *env, struct dt_device *d,
         struct kstatfs     *ksfs;
         int result = 0;
 
-       if (unlikely(osd->od_mount == NULL))
+       if (unlikely(osd->od_mnt == NULL))
                return -EINPROGRESS;
 
         /* osd_lproc.c call this without env, allocate ksfs for that case */
@@ -1014,6 +932,8 @@ int osd_statfs(const struct lu_env *env, struct dt_device *d,
                if (likely(result == 0)) { /* N.B. statfs can't really fail */
                        osd->od_osfs_age = cfs_time_current_64();
                        statfs_pack(&osd->od_statfs, ksfs);
+                       if (sb->s_flags & MS_RDONLY)
+                               sfs->os_state = OS_STATE_READONLY;
                }
        }
 
@@ -1027,6 +947,17 @@ int osd_statfs(const struct lu_env *env, struct dt_device *d,
         return result;
 }
 
+/**
+ * Estimate space needed for file creations. We assume the largest filename
+ * which is 2^64 - 1, hence a filename of 20 chars.
+ * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
+ */
+#ifdef __LDISKFS_DIR_REC_LEN
+#define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
+#else
+#define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
+#endif
+
 /*
  * Concurrency: doesn't access mutable data.
  */
@@ -1039,9 +970,20 @@ static void osd_conf_get(const struct lu_env *env,
         /*
          * XXX should be taken from not-yet-existing fs abstraction layer.
          */
+       param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
         param->ddp_max_name_len = LDISKFS_NAME_LEN;
         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
        param->ddp_block_shift  = sb->s_blocksize_bits;
+       param->ddp_mount_type     = LDD_MT_LDISKFS;
+       param->ddp_maxbytes       = sb->s_maxbytes;
+       /* Overhead estimate should be fairly accurate, so we really take a tiny
+        * error margin which also avoids fragmenting the filesystem too much */
+       param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
+       /* inode are statically allocated, so per-inode space consumption
+        * is the space consumed by the directory entry */
+       param->ddp_inodespace     = PER_OBJ_USAGE;
+       /* per-fragment overhead to be used by the client code */
+       param->ddp_grant_frag     = 6 * LDISKFS_BLOCK_SIZE(sb);
         param->ddp_mntopts      = 0;
         if (test_opt(sb, XATTR_USER))
                 param->ddp_mntopts |= MNTOPT_USERXATTR;
@@ -1075,7 +1017,7 @@ static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t
  */
 static int osd_sync(const struct lu_env *env, struct dt_device *d)
 {
-        CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
+       CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
 }
 
@@ -1097,7 +1039,7 @@ static int osd_commit_async(const struct lu_env *env,
         struct super_block *s = osd_sb(osd_dt_dev(d));
         ENTRY;
 
-        CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
+       CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
         RETURN(s->s_op->sync_fs(s, 0));
 }
 
@@ -1111,7 +1053,7 @@ static int osd_ro(const struct lu_env *env, struct dt_device *d)
         int rc;
         ENTRY;
 
-        CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
+       CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
 
         rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
         RETURN(rc);
@@ -1135,25 +1077,6 @@ static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
 }
 
 /**
- * Concurrency: serialization provided by callers.
- */
-static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
-                               struct dt_quota_ctxt *ctxt, void *data)
-{
-        struct obd_device *obd = (void *)ctxt;
-        struct vfsmount *mnt = (struct vfsmount *)data;
-        ENTRY;
-
-        obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
-        OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
-        obd->obd_lvfs_ctxt.pwdmnt = mnt;
-        obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
-        obd->obd_lvfs_ctxt.fs = get_ds();
-
-        EXIT;
-}
-
-/**
  * Note: we do not count into QUOTA here.
  * If we mount with --data_journal we may need more.
  */
@@ -1221,7 +1144,6 @@ static const struct dt_device_operations osd_dt_ops = {
         .dt_ro             = osd_ro,
         .dt_commit_async   = osd_commit_async,
         .dt_init_capa_ctxt = osd_init_capa_ctxt,
-        .dt_init_quota_ctxt= osd_init_quota_ctxt,
 };
 
 static void osd_object_read_lock(const struct lu_env *env,
@@ -1452,35 +1374,139 @@ static int osd_declare_attr_set(const struct lu_env *env,
                                 const struct lu_attr *attr,
                                 struct thandle *handle)
 {
-        struct osd_thandle *oh;
-        struct osd_object *obj;
+       struct osd_thandle     *oh;
+       struct osd_object      *obj;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lquota_id_info  *qi = &info->oti_qi;
+       long long               bspace;
+       int                     rc = 0;
+       bool                    allocated;
+       ENTRY;
 
-        LASSERT(dt != NULL);
-        LASSERT(handle != NULL);
+       LASSERT(dt != NULL);
+       LASSERT(handle != NULL);
 
-        obj = osd_dt_obj(dt);
-        LASSERT(osd_invariant(obj));
+       obj = osd_dt_obj(dt);
+       LASSERT(osd_invariant(obj));
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of0(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, attr_set);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
+       OSD_DECLARE_OP(oh, attr_set,
+                      osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
 
-        if (attr && attr->la_valid & LA_UID) {
-                if (obj->oo_inode)
-                        osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
-                                        obj->oo_inode);
-                osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
-        }
-        if (attr && attr->la_valid & LA_GID) {
-                if (obj->oo_inode)
-                        osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
-                                        obj->oo_inode);
-                osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
-        }
+       if (attr == NULL || obj->oo_inode == NULL)
+               RETURN(rc);
 
-        return 0;
+       bspace   = obj->oo_inode->i_blocks;
+       bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
+       bspace   = toqb(bspace);
+
+       /* Changing ownership is always preformed by super user, it should not
+        * fail with EDQUOT.
+        *
+        * We still need to call the osd_declare_qid() to calculate the journal
+        * credits for updating quota accounting files and to trigger quota
+        * space adjustment once the operation is completed.*/
+       if ((attr->la_valid & LA_UID) != 0 &&
+            attr->la_uid != obj->oo_inode->i_uid) {
+               qi->lqi_type = USRQUOTA;
+
+               /* inode accounting */
+               qi->lqi_is_blk = false;
+
+               /* one more inode for the new owner ... */
+               qi->lqi_id.qid_uid = attr->la_uid;
+               qi->lqi_space      = 1;
+               allocated = (attr->la_uid == 0) ? true : false;
+               rc = osd_declare_qid(env, oh, qi, allocated, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* and one less inode for the current uid */
+               qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
+               qi->lqi_space      = -1;
+               rc = osd_declare_qid(env, oh, qi, true, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* block accounting */
+               qi->lqi_is_blk = true;
+
+               /* more blocks for the new owner ... */
+               qi->lqi_id.qid_uid = attr->la_uid;
+               qi->lqi_space      = bspace;
+               allocated = (attr->la_uid == 0) ? true : false;
+               rc = osd_declare_qid(env, oh, qi, allocated, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* and finally less blocks for the current owner */
+               qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
+               qi->lqi_space      = -bspace;
+               rc = osd_declare_qid(env, oh, qi, true, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+       }
+
+       if (attr->la_valid & LA_GID &&
+           attr->la_gid != obj->oo_inode->i_gid) {
+               qi->lqi_type = GRPQUOTA;
+
+               /* inode accounting */
+               qi->lqi_is_blk = false;
+
+               /* one more inode for the new group owner ... */
+               qi->lqi_id.qid_gid = attr->la_gid;
+               qi->lqi_space      = 1;
+               allocated = (attr->la_gid == 0) ? true : false;
+               rc = osd_declare_qid(env, oh, qi, allocated, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* and one less inode for the current gid */
+               qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
+               qi->lqi_space      = -1;
+               rc = osd_declare_qid(env, oh, qi, true, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* block accounting */
+               qi->lqi_is_blk = true;
+
+               /* more blocks for the new owner ... */
+               qi->lqi_id.qid_gid = attr->la_gid;
+               qi->lqi_space      = bspace;
+               allocated = (attr->la_gid == 0) ? true : false;
+               rc = osd_declare_qid(env, oh, qi, allocated, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+
+               /* and finally less blocks for the current owner */
+               qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
+               qi->lqi_space      = -bspace;
+               rc = osd_declare_qid(env, oh, qi, true, NULL);
+               if (rc == -EDQUOT || rc == -EINPROGRESS)
+                       rc = 0;
+               if (rc)
+                       RETURN(rc);
+       }
+
+       RETURN(rc);
 }
 
 static int osd_inode_setattr(const struct lu_env *env,
@@ -1575,34 +1601,12 @@ static int osd_attr_set(const struct lu_env *env,
         OSD_EXEC_OP(handle, attr_set);
 
         inode = obj->oo_inode;
-       if (!osd_dt_dev(handle->th_dev)->od_is_md) {
-               /* OFD support */
-               rc = osd_quota_transfer(inode, attr);
-               if (rc)
-                       return rc;
-       } else {
-#ifdef HAVE_QUOTA_SUPPORT
-               if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
-                   (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
-                       struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
-                       struct           iattr iattr;
-                       int              rc;
-
-                       iattr.ia_valid = 0;
-                       if (attr->la_valid & LA_UID)
-                               iattr.ia_valid |= ATTR_UID;
-                       if (attr->la_valid & LA_GID)
-                               iattr.ia_valid |= ATTR_GID;
-                       iattr.ia_uid = attr->la_uid;
-                       iattr.ia_gid = attr->la_gid;
-                       osd_push_ctxt(env, save, 1);
-                       rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
-                       osd_pop_ctxt(save, 1);
-                       if (rc != 0)
-                               return rc;
-               }
-#endif
-       }
+       ll_vfs_dq_init(inode);
+
+       rc = osd_quota_transfer(inode, attr);
+       if (rc)
+               return rc;
+
         cfs_spin_lock(&obj->oo_guard);
         rc = osd_inode_setattr(env, inode, attr);
         cfs_spin_unlock(&obj->oo_guard);
@@ -1629,9 +1633,6 @@ 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;
-#ifdef HAVE_QUOTA_SUPPORT
-        struct osd_ctxt    *save = &info->oti_ctxt;
-#endif
 
         LINVRNT(osd_invariant(obj));
         LASSERT(obj->oo_inode == NULL);
@@ -1649,16 +1650,10 @@ static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
         if (hint && hint->dah_parent)
                 parent = hint->dah_parent;
 
-#ifdef HAVE_QUOTA_SUPPORT
-       osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
-#endif
         inode = ldiskfs_create_inode(oth->ot_handle,
                                      parent ? osd_dt_obj(parent)->oo_inode :
                                               osd_sb(osd)->s_root->d_inode,
                                      mode);
-#ifdef HAVE_QUOTA_SUPPORT
-       osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
-#endif
         if (!IS_ERR(inode)) {
                 /* Do not update file c/mtime in ldiskfs.
                  * NB: don't need any lock because no contention at this
@@ -1690,7 +1685,6 @@ static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
 {
         int result;
         struct osd_thandle *oth;
-        struct osd_device *osd = osd_obj2dev(obj);
         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
 
         LASSERT(S_ISDIR(attr->la_mode));
@@ -1698,16 +1692,7 @@ static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
         oth = container_of(th, struct osd_thandle, ot_super);
         LASSERT(oth->ot_handle->h_transaction != NULL);
         result = osd_mkfile(info, obj, mode, hint, th);
-        if (result == 0 && osd->od_iop_mode == 0) {
-                LASSERT(obj->oo_inode != NULL);
-                /*
-                 * XXX uh-oh... call low-level iam function directly.
-                 */
 
-                result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
-                                         sizeof (struct osd_fid_pack),
-                                         oth->ot_handle);
-        }
         return result;
 }
 
@@ -1833,7 +1818,8 @@ static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
 
 
 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
-                        struct dt_object *parent, cfs_umode_t child_mode)
+                       struct dt_object *parent, struct dt_object *child,
+                       cfs_umode_t child_mode)
 {
         LASSERT(ah);
 
@@ -1860,16 +1846,9 @@ static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
                 attr->la_valid &= ~LA_MTIME;
 
-       if (!osd_obj2dev(obj)->od_is_md) {
-               /* OFD support */
-               result = osd_quota_transfer(inode, attr);
-               if (result)
-                       return;
-       } else {
-#ifdef HAVE_QUOTA_SUPPORT
-               attr->la_valid &= ~(LA_UID | LA_GID);
-#endif
-       }
+       result = osd_quota_transfer(inode, attr);
+       if (result)
+               return;
 
         if (attr->la_valid != 0) {
                 result = osd_inode_setattr(info->oti_env, inode, attr);
@@ -1935,53 +1914,52 @@ static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
 
         LASSERT(obj->oo_inode != NULL);
 
-       if (osd->od_is_md) {
-               struct md_ucred *uc = md_ucred(env);
-               LASSERT(uc != NULL);
-       }
-
        osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
        return osd_oi_insert(info, osd, fid, id, th);
 }
 
 static int osd_declare_object_create(const struct lu_env *env,
-                                     struct dt_object *dt,
-                                     struct lu_attr *attr,
-                                     struct dt_allocation_hint *hint,
-                                     struct dt_object_format *dof,
-                                     struct thandle *handle)
+                                    struct dt_object *dt,
+                                    struct lu_attr *attr,
+                                    struct dt_allocation_hint *hint,
+                                    struct dt_object_format *dof,
+                                    struct thandle *handle)
 {
-        struct osd_thandle *oh;
+       struct osd_thandle      *oh;
+       int                      rc;
+       ENTRY;
 
-        LASSERT(handle != NULL);
+       LASSERT(handle != NULL);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of0(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, create);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
-        /* XXX: So far, only normal fid needs be inserted into the oi,
-         *      things could be changed later. Revise following code then. */
-        if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
-                OSD_DECLARE_OP(oh, insert);
-                oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
-        }
-        /* If this is directory, then we expect . and .. to be inserted as
-         * well. The one directory block always needs to be created for the
-         * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
-         * block), there is no danger of needing a tree for the first block.
-         */
-        if (attr && S_ISDIR(attr->la_mode)) {
-                OSD_DECLARE_OP(oh, insert);
-                OSD_DECLARE_OP(oh, insert);
-                oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
-        }
+       OSD_DECLARE_OP(oh, create, osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
+       /* XXX: So far, only normal fid needs be inserted into the oi,
+        *      things could be changed later. Revise following code then. */
+       if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
+               /* Reuse idle OI block may cause additional one OI block
+                * to be changed. */
+               OSD_DECLARE_OP(oh, insert,
+                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
+       }
+       /* If this is directory, then we expect . and .. to be inserted as
+        * well. The one directory block always needs to be created for the
+        * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
+        * block), there is no danger of needing a tree for the first block.
+        */
+       if (attr && S_ISDIR(attr->la_mode)) {
+               OSD_DECLARE_OP(oh, insert,
+                              osd_dto_credits_noquota[DTO_WRITE_BASE]);
+               OSD_DECLARE_OP(oh, insert, 0);
+       }
 
-        if (attr) {
-                osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
-                osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
-        }
-        return 0;
+       if (!attr)
+               RETURN(0);
+
+       rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
+                                  false, false, NULL, false);
+       RETURN(rc);
 }
 
 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
@@ -2024,28 +2002,36 @@ static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
  * Concurrency: must be locked
  */
 static int osd_declare_object_destroy(const struct lu_env *env,
-                                      struct dt_object *dt,
-                                      struct thandle *th)
+                                     struct dt_object *dt,
+                                     struct thandle *th)
 {
-        struct osd_object  *obj = osd_dt_obj(dt);
-        struct inode       *inode = obj->oo_inode;
-        struct osd_thandle *oh;
-
-        ENTRY;
-
-        oh = container_of0(th, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
-        LASSERT(inode);
-
-        OSD_DECLARE_OP(oh, destroy);
-        OSD_DECLARE_OP(oh, delete);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
-        oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
-
-        osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
-        osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
+       struct osd_object  *obj = osd_dt_obj(dt);
+       struct inode       *inode = obj->oo_inode;
+       struct osd_thandle *oh;
+       int                 rc;
+       ENTRY;
 
-        RETURN(0);
+       oh = container_of0(th, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
+       LASSERT(inode);
+
+       OSD_DECLARE_OP(oh, delete, osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
+       /* XXX: So far, only normal fid needs to be inserted into the OI,
+        *      so only normal fid needs to be removed from the OI also.
+        * Recycle idle OI leaf may cause additional three OI blocks
+        * to be changed. */
+       OSD_DECLARE_OP(oh, destroy, fid_is_norm(lu_object_fid(&dt->do_lu)) ?
+                       osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3 : 0);
+
+       /* one less inode */
+       rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
+                                  false, true, NULL, false);
+       if (rc)
+               RETURN(rc);
+       /* data to be truncated */
+       rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
+                                  true, true, NULL, false);
+       RETURN(rc);
 }
 
 static int osd_object_destroy(const struct lu_env *env,
@@ -2118,6 +2104,7 @@ static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
         if (fl & LU_XATTR_CREATE)
                 fs_flags |= XATTR_CREATE;
 
+       ll_vfs_dq_init(inode);
         dentry->d_inode = inode;
         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
                                    buf->lb_len, fs_flags);
@@ -2249,10 +2236,9 @@ static int osd_declare_object_ref_add(const struct lu_env *env,
         oh = container_of0(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, ref_add);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
+       OSD_DECLARE_OP(oh, ref_add, osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
 
-        return 0;
+       return 0;
 }
 
 /*
@@ -2308,10 +2294,9 @@ static int osd_declare_object_ref_del(const struct lu_env *env,
         oh = container_of0(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, ref_del);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
+       OSD_DECLARE_OP(oh, ref_del, osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
 
-        return 0;
+       return 0;
 }
 
 /*
@@ -2382,7 +2367,6 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
 
         LASSERT(dt_object_exists(dt));
         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
-        LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
 
         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
                 return -EACCES;
@@ -2397,22 +2381,18 @@ static int osd_declare_xattr_set(const struct lu_env *env,
                                  const struct lu_buf *buf, const char *name,
                                  int fl, struct thandle *handle)
 {
-        struct osd_thandle *oh;
+       struct osd_thandle *oh;
 
-        LASSERT(handle != NULL);
+       LASSERT(handle != NULL);
 
-        if (strcmp(name, XATTR_NAME_VERSION) == 0) {
-                /* no credits for version */
-                return 0;
-        }
-
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of0(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, xattr_set);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
+       OSD_DECLARE_OP(oh, xattr_set, strcmp(name, XATTR_NAME_VERSION) == 0 ?
+                      osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
+                      osd_dto_credits_noquota[DTO_XATTR_SET]);
 
-        return 0;
+       return 0;
 }
 
 /*
@@ -2492,10 +2472,9 @@ static int osd_declare_xattr_del(const struct lu_env *env,
         oh = container_of0(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, xattr_set);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
+       OSD_DECLARE_OP(oh, xattr_set, osd_dto_credits_noquota[DTO_XATTR_SET]);
 
-        return 0;
+       return 0;
 }
 
 /*
@@ -2521,6 +2500,7 @@ static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
 
         OSD_EXEC_OP(handle, xattr_set);
 
+       ll_vfs_dq_init(inode);
         dentry->d_inode = inode;
         rc = inode->i_op->removexattr(dentry, name);
         return rc;
@@ -2690,17 +2670,6 @@ static int osd_iam_container_init(const struct lu_env *env,
                 return result;
 
         result = iam_container_setup(bag);
-        if (result != 0)
-                goto out;
-
-        if (osd_obj2dev(obj)->od_iop_mode) {
-                u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
-
-                bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
-                                                ptr, 0, &result);
-        }
-
- out:
         if (result == 0)
                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
         else
@@ -2719,7 +2688,6 @@ static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
        int                      result;
        int                      skip_iam = 0;
        struct osd_object       *obj = osd_dt_obj(dt);
-       struct osd_device       *osd = osd_obj2dev(obj);
 
         LINVRNT(osd_invariant(obj));
         LASSERT(dt_object_exists(dt));
@@ -2727,7 +2695,7 @@ static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
         if (osd_object_is_root(obj)) {
                 dt->do_index_ops = &osd_index_ea_ops;
                 result = 0;
-        } else if (feat == &dt_directory_features && osd->od_iop_mode) {
+       } else if (feat == &dt_directory_features) {
                 dt->do_index_ops = &osd_index_ea_ops;
                 if (S_ISDIR(obj->oo_inode->i_mode))
                         result = 0;
@@ -2782,6 +2750,9 @@ static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
         }
         LINVRNT(osd_invariant(obj));
 
+       if (is_quota_glb_feat(feat))
+               result = osd_quota_migration(env, dt, feat);
+
         return result;
 }
 
@@ -2873,10 +2844,9 @@ static int osd_index_declare_iam_delete(const struct lu_env *env,
         oh = container_of0(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, delete);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
+       OSD_DECLARE_OP(oh, delete, osd_dto_credits_noquota[DTO_INDEX_DELETE]);
 
-        return 0;
+       return 0;
 }
 
 /**
@@ -2896,11 +2866,12 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
                                 struct thandle *handle,
                                 struct lustre_capa *capa)
 {
-        struct osd_object     *obj = osd_dt_obj(dt);
-        struct osd_thandle    *oh;
-        struct iam_path_descr *ipd;
-        struct iam_container  *bag = &obj->oo_dir->od_container;
-        int                    rc;
+        struct osd_thread_info *oti = osd_oti_get(env);
+        struct osd_object      *obj = osd_dt_obj(dt);
+        struct osd_thandle     *oh;
+        struct iam_path_descr  *ipd;
+        struct iam_container   *bag = &obj->oo_dir->od_container;
+        int                     rc;
 
         ENTRY;
 
@@ -2922,6 +2893,12 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
         LASSERT(oh->ot_handle != NULL);
         LASSERT(oh->ot_handle->h_transaction != NULL);
 
+       if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
+               /* swab quota uid/gid provided by caller */
+               oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
+               key = (const struct dt_key *)&oti->oti_quota_id;
+       }
+
         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
         osd_ipd_put(env, bag, ipd);
         LINVRNT(osd_invariant(obj));
@@ -2929,28 +2906,29 @@ static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
 }
 
 static int osd_index_declare_ea_delete(const struct lu_env *env,
-                                       struct dt_object *dt,
-                                       const struct dt_key *key,
-                                       struct thandle *handle)
+                                      struct dt_object *dt,
+                                      const struct dt_key *key,
+                                      struct thandle *handle)
 {
-        struct osd_thandle *oh;
+       struct osd_thandle *oh;
+       struct inode       *inode;
+       int                 rc;
+       ENTRY;
 
-        LASSERT(dt_object_exists(dt));
-        LASSERT(handle != NULL);
+       LASSERT(dt_object_exists(dt));
+       LASSERT(handle != NULL);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of0(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, delete);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
+       OSD_DECLARE_OP(oh, delete, osd_dto_credits_noquota[DTO_INDEX_DELETE]);
 
-        LASSERT(osd_dt_obj(dt)->oo_inode);
-        osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
-                        osd_dt_obj(dt)->oo_inode);
-        osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
-                        osd_dt_obj(dt)->oo_inode);
+       inode = osd_dt_obj(dt)->oo_inode;
+       LASSERT(inode);
 
-        return 0;
+       rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
+                                  true, true, NULL, false);
+       RETURN(rc);
 }
 
 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
@@ -3005,6 +2983,7 @@ static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
                 RETURN(-EACCES);
 
+       ll_vfs_dq_init(dir);
         dentry = osd_child_dentry_get(env, obj,
                                       (char *)key, strlen((char *)key));
 
@@ -3072,6 +3051,12 @@ static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
         /* got ipd now we can start iterator. */
         iam_it_init(it, bag, 0, ipd);
 
+       if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
+               /* swab quota uid/gid provided by caller */
+               oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
+               key = (const struct dt_key *)&oti->oti_quota_id;
+       }
+
         rc = iam_it_get(it, (struct iam_key *)key);
         if (rc >= 0) {
                 if (S_ISDIR(obj->oo_inode->i_mode))
@@ -3080,10 +3065,14 @@ static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
                         iam_rec = (struct iam_rec *) rec;
 
                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
+
                 if (S_ISDIR(obj->oo_inode->i_mode))
                         osd_fid_unpack((struct lu_fid *) rec,
                                        (struct osd_fid_pack *)iam_rec);
+               else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
+                       osd_quota_unpack(obj, rec);
         }
+
         iam_it_put(it);
         iam_it_fini(it);
         osd_ipd_put(env, bag, ipd);
@@ -3107,10 +3096,9 @@ static int osd_index_declare_iam_insert(const struct lu_env *env,
         oh = container_of0(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, insert);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
+       OSD_DECLARE_OP(oh, insert, osd_dto_credits_noquota[DTO_INDEX_INSERT]);
 
-        return 0;
+       return 0;
 }
 
 /**
@@ -3133,11 +3121,8 @@ static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
         struct iam_path_descr *ipd;
         struct osd_thandle    *oh;
         struct iam_container  *bag = &obj->oo_dir->od_container;
-#ifdef HAVE_QUOTA_SUPPORT
-        cfs_cap_t              save = cfs_curproc_cap_pack();
-#endif
         struct osd_thread_info *oti = osd_oti_get(env);
-        struct iam_rec         *iam_rec = (struct iam_rec *)oti->oti_ldp;
+        struct iam_rec         *iam_rec;
         int                     rc;
 
         ENTRY;
@@ -3159,21 +3144,22 @@ static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
         oh = container_of0(th, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle != NULL);
         LASSERT(oh->ot_handle->h_transaction != NULL);
-#ifdef HAVE_QUOTA_SUPPORT
-        if (ignore_quota)
-                cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
-        else
-                cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
-#endif
-        if (S_ISDIR(obj->oo_inode->i_mode))
-                osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
-        else
-                iam_rec = (struct iam_rec *) rec;
+       if (S_ISDIR(obj->oo_inode->i_mode)) {
+               iam_rec = (struct iam_rec *)oti->oti_ldp;
+               osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
+       } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
+               /* pack quota uid/gid */
+               oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
+               key = (const struct dt_key *)&oti->oti_quota_id;
+               /* pack quota record */
+               rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
+               iam_rec = (struct iam_rec *)rec;
+       } else {
+               iam_rec = (struct iam_rec *)rec;
+       }
+
         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
                         iam_rec, ipd);
-#ifdef HAVE_QUOTA_SUPPORT
-        cfs_curproc_cap_unpack(save);
-#endif
         osd_ipd_put(env, bag, ipd);
         LINVRNT(osd_invariant(obj));
         RETURN(rc);
@@ -3216,6 +3202,8 @@ static int __osd_ea_add_rec(struct osd_thread_info *info,
         } else {
                 child->d_fsdata = NULL;
         }
+       LASSERT(pobj->oo_inode);
+       ll_vfs_dq_init(pobj->oo_inode);
         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
 
         RETURN(rc);
@@ -3331,7 +3319,7 @@ static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
         return rc;
 }
 
-static int
+static void
 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
                      struct osd_idmap_cache *oic)
 {
@@ -3342,13 +3330,16 @@ osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
        int                  rc;
        ENTRY;
 
+       if (!fid_is_norm(fid) && !fid_is_igif(fid))
+               RETURN_EXIT;
+
 again:
        rc = osd_oi_lookup(oti, dev, fid, id);
        if (rc != 0 && rc != -ENOENT)
-               RETURN(rc);
+               RETURN_EXIT;
 
        if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
-               RETURN(0);
+               RETURN_EXIT;
 
        if (thread_is_running(&scrub->os_thread)) {
                rc = osd_oii_insert(dev, oic, rc == -ENOENT);
@@ -3359,10 +3350,10 @@ again:
                if (unlikely(rc == -EAGAIN))
                        goto again;
 
-               RETURN(rc);
+               RETURN_EXIT;
        }
 
-       if (!scrub->os_no_scrub && ++once == 1) {
+       if (!dev->od_noscrub && ++once == 1) {
                CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
                       PFID(fid));
                rc = osd_scrub_start(dev);
@@ -3374,7 +3365,7 @@ again:
                        goto again;
        }
 
-       RETURN(rc = -EREMCHG);
+       EXIT;
 }
 
 /**
@@ -3427,16 +3418,17 @@ static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
                        rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
                else
                        osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
-
-               if (rc != 0 || !fid_is_norm(fid))
+               if (rc != 0) {
+                       fid_zero(&oic->oic_fid);
                        GOTO(out, rc);
+               }
 
                oic->oic_fid = *fid;
                if ((scrub->os_pos_current <= ino) &&
                    (sf->sf_flags & SF_INCONSISTENT ||
                     ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
                                      sf->sf_oi_bitmap)))
-                       rc = osd_consistency_check(oti, dev, oic);
+                       osd_consistency_check(oti, dev, oic);
        } else {
                rc = -ENOENT;
        }
@@ -3468,7 +3460,17 @@ struct osd_object *osd_object_find(const struct lu_env *env,
         struct lu_object  *luch;
         struct lu_object  *lo;
 
-        luch = lu_object_find(env, ludev, fid, NULL);
+       /*
+        * at this point topdev might not exist yet
+        * (i.e. MGS is preparing profiles). so we can
+        * not rely on topdev and instead lookup with
+        * our device passed as topdev. this can't work
+        * if the object isn't cached yet (as osd doesn't
+        * allocate lu_header). IOW, the object must be
+        * in the cache, otherwise lu_object_alloc() crashes
+        * -bzzz
+        */
+       luch = lu_object_find_at(env, ludev, fid, NULL);
         if (!IS_ERR(luch)) {
                 if (lu_object_exists(luch)) {
                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
@@ -3509,29 +3511,33 @@ static inline void osd_object_put(const struct lu_env *env,
 }
 
 static int osd_index_declare_ea_insert(const struct lu_env *env,
-                                       struct dt_object *dt,
-                                       const struct dt_rec *rec,
-                                       const struct dt_key *key,
-                                       struct thandle *handle)
-{
-        struct osd_thandle *oh;
+                                      struct dt_object *dt,
+                                      const struct dt_rec *rec,
+                                      const struct dt_key *key,
+                                      struct thandle *handle)
+{
+       struct osd_thandle *oh;
+       struct inode       *inode;
+       int                 rc;
+       ENTRY;
 
-        LASSERT(dt_object_exists(dt));
-        LASSERT(handle != NULL);
+       LASSERT(dt_object_exists(dt));
+       LASSERT(handle != NULL);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of0(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        OSD_DECLARE_OP(oh, insert);
-        oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
+       OSD_DECLARE_OP(oh, insert, osd_dto_credits_noquota[DTO_INDEX_INSERT]);
 
-        LASSERT(osd_dt_obj(dt)->oo_inode);
-        osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
-                        osd_dt_obj(dt)->oo_inode);
-        osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
-                        osd_dt_obj(dt)->oo_inode);
+       inode = osd_dt_obj(dt)->oo_inode;
+       LASSERT(inode);
 
-        return 0;
+       /* We ignore block quota on meta pool (MDTs), so needn't
+        * calculate how many blocks will be consumed by this index
+        * insert */
+       rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
+                                  true, true, NULL, false);
+       RETURN(rc);
 }
 
 /**
@@ -3554,9 +3560,6 @@ static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
         struct lu_fid     *fid   = (struct lu_fid *) rec;
         const char        *name  = (const char *)key;
         struct osd_object *child;
-#ifdef HAVE_QUOTA_SUPPORT
-        cfs_cap_t          save  = cfs_curproc_cap_pack();
-#endif
         int                rc;
 
         ENTRY;
@@ -3570,16 +3573,7 @@ static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
 
         child = osd_object_find(env, dt, fid);
         if (!IS_ERR(child)) {
-#ifdef HAVE_QUOTA_SUPPORT
-                if (ignore_quota)
-                        cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
-                else
-                        cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
-#endif
                 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
-#ifdef HAVE_QUOTA_SUPPORT
-                cfs_curproc_cap_unpack(save);
-#endif
                 osd_object_put(env, child);
         } else {
                 rc = PTR_ERR(child);
@@ -3652,7 +3646,14 @@ static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
 static int osd_it_iam_get(const struct lu_env *env,
                           struct dt_it *di, const struct dt_key *key)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_thread_info  *oti = osd_oti_get(env);
+       struct osd_it_iam       *it = (struct osd_it_iam *)di;
+
+       if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
+               /* swab quota uid/gid */
+               oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
+               key = (struct dt_key *)&oti->oti_quota_id;
+       }
 
         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
 }
@@ -3694,9 +3695,20 @@ static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
                                  const struct dt_it *di)
 {
-        struct osd_it_iam *it = (struct osd_it_iam *)di;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_it_iam      *it = (struct osd_it_iam *)di;
+       struct osd_object      *obj = it->oi_obj;
+       struct dt_key          *key;
 
-        return (struct dt_key *)iam_it_key_get(&it->oi_it);
+       key = (struct dt_key *)iam_it_key_get(&it->oi_it);
+
+       if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
+               /* swab quota uid/gid */
+               oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
+               key = (struct dt_key *)&oti->oti_quota_id;
+       }
+
+       return key;
 }
 
 /**
@@ -3789,6 +3801,10 @@ static int osd_it_iam_rec(const struct lu_env *env,
                /* IAM does not store object type in IAM index (dir) */
                osd_it_pack_dirent(lde, fid, hash, name, namelen,
                                   0, LUDA_FID);
+       } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
+               iam_reccpy(&it->oi_it.ii_path.ip_leaf,
+                          (struct iam_rec *)dtrec);
+               osd_quota_unpack(it->oi_obj, dtrec);
        } else {
                iam_reccpy(&it->oi_it.ii_path.ip_leaf,
                           (struct iam_rec *)dtrec);
@@ -3846,6 +3862,7 @@ static const struct dt_index_operations osd_index_iam_ops = {
         }
 };
 
+
 /**
  * Creates or initializes iterator context.
  *
@@ -3877,9 +3894,9 @@ static struct dt_it *osd_it_ea_init(const struct lu_env *env,
         it->oie_file.f_pos      = 0;
         it->oie_file.f_dentry   = obj_dentry;
         if (attr & LUDA_64BITHASH)
-                it->oie_file.f_flags = O_64BITHASH;
+               it->oie_file.f_mode |= FMODE_64BITHASH;
         else
-                it->oie_file.f_flags = O_32BITHASH;
+               it->oie_file.f_mode |= FMODE_32BITHASH;
         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
         it->oie_file.f_op         = obj->oo_inode->i_fop;
         it->oie_file.private_data = NULL;
@@ -4064,7 +4081,7 @@ static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
                 it->oie_it_dirent++;
                 RETURN(0);
         } else {
-                if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
+               if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
                         rc = +1;
                 else
                         rc = osd_ldiskfs_it_fill(env, di);
@@ -4133,8 +4150,10 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
 
        if (!fid_is_sane(fid)) {
                rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
-               if (rc != 0)
+               if (rc != 0) {
+                       fid_zero(&oic->oic_fid);
                        RETURN(rc);
+               }
        } else {
                osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
        }
@@ -4143,15 +4162,11 @@ static inline int osd_it_ea_rec(const struct lu_env *env,
                           it->oie_dirent->oied_name,
                           it->oie_dirent->oied_namelen,
                           it->oie_dirent->oied_type, attr);
-
-       if (!fid_is_norm(fid))
-               RETURN(0);
-
        oic->oic_fid = *fid;
        if ((scrub->os_pos_current <= ino) &&
            (sf->sf_flags & SF_INCONSISTENT ||
             ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
-               rc = osd_consistency_check(oti, dev, oic);
+               osd_consistency_check(oti, dev, oic);
 
        RETURN(rc);
 }
@@ -4221,7 +4236,6 @@ static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
                 return -EACCES;
 
         rc = osd_ea_lookup_rec(env, obj, rec, key);
-
         if (rc == 0)
                 rc = +1;
         RETURN(rc);
@@ -4343,51 +4357,89 @@ static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
 static int osd_mount(const struct lu_env *env,
                      struct osd_device *o, struct lustre_cfg *cfg)
 {
-        struct lustre_mount_info *lmi;
-        const char               *dev  = lustre_cfg_string(cfg, 0);
-        struct lustre_disk_data  *ldd;
-        struct lustre_sb_info    *lsi;
-        int                       rc = 0;
-
+       const char              *name  = lustre_cfg_string(cfg, 0);
+       const char              *dev  = lustre_cfg_string(cfg, 1);
+       const char              *opts;
+       unsigned long            page, s_flags, lmd_flags = 0;
+       struct page             *__page;
+       struct file_system_type *type;
+       char                    *options = NULL;
+       char                    *str;
+       int                       rc = 0;
         ENTRY;
 
+       if (o->od_mnt != NULL)
+               RETURN(0);
+
         o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
         if (o->od_fsops == NULL) {
                 CERROR("Can't find fsfilt_ldiskfs\n");
                 RETURN(-ENOTSUPP);
         }
 
-        if (o->od_mount != NULL) {
-                CERROR("Already mounted (%s)\n", dev);
-                RETURN(-EEXIST);
-        }
+       OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
+       if (__page == NULL)
+               GOTO(out, rc = -ENOMEM);
+
+       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);
+       opts = lustre_cfg_string(cfg, 3);
+       page = (unsigned long)cfs_page_address(__page);
+       options = (char *)page;
+       *options = '\0';
+       if (opts == NULL)
+               strcat(options, "user_xattr,acl");
+       else
+               strcat(options, opts);
 
-        /* get mount */
-        lmi = server_get_mount(dev);
-        if (lmi == NULL) {
-                CERROR("Cannot get mount info for %s!\n", dev);
-                RETURN(-EFAULT);
-        }
+       /* Glom up mount options */
+       if (*options != '\0')
+               strcat(options, ",");
+       strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
 
-        LASSERT(lmi != NULL);
-        /* save lustre_mount_info in dt_device */
-        o->od_mount = lmi;
-        o->od_mnt = lmi->lmi_mnt;
+       type = get_fs_type("ldiskfs");
+       if (!type) {
+               CERROR("%s: cannot find ldiskfs module\n", name);
+               GOTO(out, rc = -ENODEV);
+       }
 
-        lsi = s2lsi(lmi->lmi_sb);
-        ldd = lsi->lsi_ldd;
+       o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
+       cfs_module_put(type->owner);
 
-        if (ldd->ldd_flags & LDD_F_IAM_DIR) {
-                o->od_iop_mode = 0;
-                LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
-        } else
-                o->od_iop_mode = 1;
+       if (IS_ERR(o->od_mnt)) {
+               rc = PTR_ERR(o->od_mnt);
+               CERROR("%s: can't mount %s: %d\n", name, dev, rc);
+               o->od_mnt = NULL;
+               GOTO(out, rc);
+       }
 
-        if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
-                rc = osd_compat_init(o);
-                if (rc)
-                        CERROR("%s: can't initialize compats: %d\n", dev, rc);
-        }
+       if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
+               CERROR("%s: underlying device %s is marked as read-only. "
+                      "Setup failed\n", name, dev);
+               mntput(o->od_mnt);
+               o->od_mnt = NULL;
+               GOTO(out, rc = -EROFS);
+       }
+
+       if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
+           LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
+               CERROR("%s: device %s is mounted w/o journal\n", name, dev);
+               mntput(o->od_mnt);
+               o->od_mnt = NULL;
+               GOTO(out, rc = -EINVAL);
+       }
+
+       if (lmd_flags & LMD_FLG_NOSCRUB)
+               o->od_noscrub = 1;
+
+out:
+       if (__page)
+               OBD_PAGE_FREE(__page);
+       if (rc)
+               fsfilt_put_ops(o->od_fsops);
 
         RETURN(rc);
 }
@@ -4398,6 +4450,8 @@ static struct lu_device *osd_device_fini(const struct lu_env *env,
         int rc;
         ENTRY;
 
+       rc = osd_shutdown(env, osd_dev(d));
+
         osd_compat_fini(osd_dev(d));
 
         shrink_dcache_sb(osd_sb(osd_dev(d)));
@@ -4409,46 +4463,129 @@ static struct lu_device *osd_device_fini(const struct lu_env *env,
                 RETURN (ERR_PTR(rc));
         }
 
-        if (osd_dev(d)->od_mount)
-                server_put_mount(osd_dev(d)->od_mount->lmi_name,
-                                 osd_dev(d)->od_mount->lmi_mnt);
-        osd_dev(d)->od_mount = NULL;
+       if (osd_dev(d)->od_mnt) {
+               mntput(osd_dev(d)->od_mnt);
+               osd_dev(d)->od_mnt = NULL;
+       }
 
         RETURN(NULL);
 }
 
+static int osd_device_init0(const struct lu_env *env,
+                           struct osd_device *o,
+                           struct lustre_cfg *cfg)
+{
+       struct lu_device        *l = osd2lu_dev(o);
+       struct osd_thread_info *info;
+       int                     rc;
+
+       /* if the module was re-loaded, env can loose its keys */
+       rc = lu_env_refill((struct lu_env *) env);
+       if (rc)
+               GOTO(out, rc);
+       info = osd_oti_get(env);
+       LASSERT(info);
+
+       l->ld_ops = &osd_lu_ops;
+       o->od_dt_dev.dd_ops = &osd_dt_ops;
+
+       cfs_spin_lock_init(&o->od_osfs_lock);
+       cfs_mutex_init(&o->od_otable_mutex);
+       o->od_osfs_age = cfs_time_shift_64(-1000);
+
+       o->od_capa_hash = init_capa_hash();
+       if (o->od_capa_hash == NULL)
+               GOTO(out, rc = -ENOMEM);
+
+       o->od_read_cache = 1;
+       o->od_writethrough_cache = 1;
+       o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
+
+       rc = osd_mount(env, o, cfg);
+       if (rc)
+               GOTO(out_capa, rc);
+
+       /* setup scrub, including OI files initialization */
+       rc = osd_scrub_setup(env, o);
+       if (rc < 0)
+               GOTO(out_mnt, rc);
+
+       strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
+                       sizeof(o->od_svname) - 1);
+
+       rc = osd_compat_init(o);
+       if (rc != 0)
+               GOTO(out_scrub, rc);
+
+       rc = lu_site_init(&o->od_site, l);
+       if (rc)
+               GOTO(out_compat, rc);
+       o->od_site.ls_bottom_dev = l;
+
+       rc = lu_site_init_finish(&o->od_site);
+       if (rc)
+               GOTO(out_site, rc);
+
+       rc = osd_procfs_init(o, o->od_svname);
+       if (rc != 0) {
+               CERROR("%s: can't initialize procfs: rc = %d\n",
+                      o->od_svname, rc);
+               GOTO(out_site, rc);
+       }
+
+       LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
+
+       /* initialize quota slave instance */
+       o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
+                                    o->od_proc_entry);
+       if (IS_ERR(o->od_quota_slave)) {
+               rc = PTR_ERR(o->od_quota_slave);
+               o->od_quota_slave = NULL;
+               GOTO(out_procfs, rc);
+       }
+
+       RETURN(0);
+out_procfs:
+       osd_procfs_fini(o);
+out_site:
+       lu_site_fini(&o->od_site);
+out_compat:
+       osd_compat_fini(o);
+out_scrub:
+       osd_scrub_cleanup(env, o);
+out_mnt:
+       osd_oi_fini(info, o);
+       osd_shutdown(env, o);
+       mntput(o->od_mnt);
+       o->od_mnt = NULL;
+out_capa:
+       cleanup_capa_hash(o->od_capa_hash);
+out:
+       RETURN(rc);
+}
+
 static struct lu_device *osd_device_alloc(const struct lu_env *env,
                                           struct lu_device_type *t,
                                           struct lustre_cfg *cfg)
 {
-        struct lu_device  *l;
-        struct osd_device *o;
-
-        OBD_ALLOC_PTR(o);
-        if (o != NULL) {
-                int result;
-
-                result = dt_device_init(&o->od_dt_dev, t);
-                if (result == 0) {
-                        l = osd2lu_dev(o);
-                        l->ld_ops = &osd_lu_ops;
-                        o->od_dt_dev.dd_ops = &osd_dt_ops;
-                        cfs_spin_lock_init(&o->od_osfs_lock);
-                       cfs_mutex_init(&o->od_otable_mutex);
-                        o->od_osfs_age = cfs_time_shift_64(-1000);
-                        o->od_capa_hash = init_capa_hash();
-                        if (o->od_capa_hash == NULL) {
-                                dt_device_fini(&o->od_dt_dev);
-                                l = ERR_PTR(-ENOMEM);
-                        }
-                } else
-                        l = ERR_PTR(result);
+       struct osd_device *o;
+       int                rc;
 
-                if (IS_ERR(l))
-                        OBD_FREE_PTR(o);
-        } else
-                l = ERR_PTR(-ENOMEM);
-        return l;
+       OBD_ALLOC_PTR(o);
+       if (o == NULL)
+               return ERR_PTR(-ENOMEM);
+
+       rc = dt_device_init(&o->od_dt_dev, t);
+       if (rc == 0) {
+               rc = osd_device_init0(env, o, cfg);
+               if (rc)
+                       dt_device_fini(&o->od_dt_dev);
+       }
+
+       if (unlikely(rc != 0))
+               OBD_FREE_PTR(o);
+
+       return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
 }
 
 static struct lu_device *osd_device_free(const struct lu_env *env,
@@ -4458,6 +4595,14 @@ static struct lu_device *osd_device_free(const struct lu_env *env,
         ENTRY;
 
         cleanup_capa_hash(o->od_capa_hash);
+       /* 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_fini(&o->od_site);
         dt_device_fini(&o->od_dt_dev);
         OBD_FREE_PTR(o);
         RETURN(NULL);
@@ -4476,8 +4621,8 @@ static int osd_process_config(const struct lu_env *env,
                 break;
         case LCFG_CLEANUP:
                lu_dev_del_linkage(d->ld_site, d);
-                err = osd_shutdown(env, o);
-                break;
+               err = osd_shutdown(env, o);
+               break;
         default:
                 err = -ENOSYS;
         }
@@ -4488,53 +4633,92 @@ static int osd_process_config(const struct lu_env *env,
 static int osd_recovery_complete(const struct lu_env *env,
                                  struct lu_device *d)
 {
-        RETURN(0);
+       struct osd_device       *osd = osd_dev(d);
+       int                      rc = 0;
+       ENTRY;
+
+       if (osd->od_quota_slave == NULL)
+               RETURN(0);
+
+       /* start qsd instance on recovery completion, this notifies the quota
+        * slave code that we are about to process new requests now */
+       rc = qsd_start(env, osd->od_quota_slave);
+       RETURN(rc);
+}
+
+/*
+ * we use exports to track all osd users
+ */
+static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
+                          struct obd_device *obd, struct obd_uuid *cluuid,
+                          struct obd_connect_data *data, void *localdata)
+{
+       struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
+       struct lustre_handle  conn;
+       int                   rc;
+       ENTRY;
+
+       CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
+
+       rc = class_connect(&conn, obd, cluuid);
+       if (rc)
+               RETURN(rc);
+
+       *exp = class_conn2export(&conn);
+
+       cfs_spin_lock(&osd->od_osfs_lock);
+       osd->od_connects++;
+       cfs_spin_unlock(&osd->od_osfs_lock);
+
+       RETURN(0);
+}
+
+/*
+ * once last export (we don't count self-export) disappeared
+ * osd can be released
+ */
+static int osd_obd_disconnect(struct obd_export *exp)
+{
+       struct obd_device *obd = exp->exp_obd;
+       struct osd_device *osd = osd_dev(obd->obd_lu_dev);
+       int                rc, release = 0;
+       ENTRY;
+
+       /* Only disconnect the underlying layers on the final disconnect. */
+       cfs_spin_lock(&osd->od_osfs_lock);
+       osd->od_connects--;
+       if (osd->od_connects == 0)
+               release = 1;
+       cfs_spin_unlock(&osd->od_osfs_lock);
+
+       rc = class_disconnect(exp); /* bz 9811 */
+
+       if (rc == 0 && release)
+               class_manual_cleanup(obd);
+       RETURN(rc);
 }
 
 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
                        struct lu_device *dev)
 {
        struct osd_device *osd = osd_dev(dev);
-       int                result;
+       int                result = 0;
        ENTRY;
 
-       /* 1. setup scrub, including OI files initialization */
-       result = osd_scrub_setup(env, osd);
-        if (result < 0)
-                RETURN(result);
-
-       /* 2. setup quota slave instance */
-       osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
-                                      osd->od_proc_entry);
-       if (IS_ERR(osd->od_quota_slave)) {
-               result = PTR_ERR(osd->od_quota_slave);
-               osd->od_quota_slave = NULL;
-               RETURN(result);
+       if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
+               /* MDT/MDD still use old infrastructure to create
+                * special files */
+               result = llo_local_objects_setup(env, lu2md_dev(pdev),
+                                                lu2dt_dev(dev));
+               if (result)
+                       RETURN(result);
        }
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
-       /* Unfortunately, the current MDD implementation relies on some specific
-        * code to be executed in the OSD layer. Since OFD now also uses the OSD
-        * module, we need a way to skip the metadata-specific code when running
-        * with OFD.
-        * The hack here is to check the type of the parent device which is
-        * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
-        * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
-        * layer and still relies on lvfs. This hack won't work any more when
-        * LOD is landed since LOD is of DT type.
-        * This code should be removed once the orion MDT changes (LOD/OSP, ...)
-        * have been landed */
-       osd->od_is_md = lu_device_is_md(pdev);
-#else
-#warning "all is_md checks must be removed from osd-ldiskfs"
-#endif
-
-        if (!osd->od_is_md)
-                RETURN(0);
+       if (osd->od_quota_slave != NULL)
+               /* set up quota slave objects */
+               result = qsd_prepare(env, osd->od_quota_slave);
 
-        /* 3. setup local objects */
-        result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
-        RETURN(result);
+       RETURN(result);
 }
 
 static const struct lu_object_operations osd_lu_obj_ops = {
@@ -4567,9 +4751,9 @@ static const struct lu_device_type_operations osd_device_type_ops = {
         .ldto_device_fini    = osd_device_fini
 };
 
-static struct lu_device_type osd_device_type = {
+struct lu_device_type osd_device_type = {
         .ldt_tags     = LU_DEVICE_DT,
-        .ldt_name     = LUSTRE_OSD_NAME,
+       .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
         .ldt_ops      = &osd_device_type_ops,
         .ldt_ctx_tags = LCT_LOCAL,
 };
@@ -4578,7 +4762,9 @@ static struct lu_device_type osd_device_type = {
  * lprocfs legacy support.
  */
 static struct obd_ops osd_obd_device_ops = {
-        .o_owner = THIS_MODULE
+       .o_owner = THIS_MODULE,
+       .o_connect      = osd_obd_connect,
+       .o_disconnect   = osd_obd_disconnect
 };
 
 static int __init osd_mod_init(void)
@@ -4588,16 +4774,16 @@ static int __init osd_mod_init(void)
         osd_oi_mod_init();
         lprocfs_osd_init_vars(&lvars);
         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
-                                   LUSTRE_OSD_NAME, &osd_device_type);
+                                  LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
 }
 
 static void __exit osd_mod_exit(void)
 {
-        class_unregister_type(LUSTRE_OSD_NAME);
+       class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
 }
 
 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
-MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
+MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
 MODULE_LICENSE("GPL");
 
 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);