Whamcloud - gitweb
LU-7991 quota: project quota against ZFS backend
[fs/lustre-release.git] / lustre / osd-zfs / osd_oi.c
index 5383858..010e023 100644 (file)
@@ -39,7 +39,6 @@
 
 #define DEBUG_SUBSYSTEM S_OSD
 
-#include <lustre_ver.h>
 #include <libcfs/libcfs.h>
 #include <obd_support.h>
 #include <lustre_net.h>
@@ -88,8 +87,6 @@ static const struct named_oid oids[] = {
        { .oid = FLD_INDEX_OID,        .name = "fld" },
        { .oid = MDD_LOV_OBJ_OID,      .name = LOV_OBJID },
        { .oid = OFD_HEALTH_CHECK_OID, .name = HEALTH_CHECK },
-       { .oid = ACCT_USER_OID,        .name = "acct_usr_inode" },
-       { .oid = ACCT_GROUP_OID,       .name = "acct_grp_inode" },
        { .oid = REPLY_DATA_OID,       .name = REPLY_DATA },
        { .oid = 0 }
 };
@@ -148,6 +145,9 @@ osd_oi_create(const struct lu_env *env, struct osd_device *o,
        if (rc == 0)
                return -EEXIST;
 
+       if (o->od_dt_dev.dd_rdonly)
+               return -EROFS;
+
        /* create fid-to-dnode index */
        tx = dmu_tx_create(o->od_os);
        if (tx == NULL)
@@ -156,7 +156,6 @@ osd_oi_create(const struct lu_env *env, struct osd_device *o,
        dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
        dmu_tx_hold_bonus(tx, parent);
        dmu_tx_hold_zap(tx, parent, TRUE, name);
-       LASSERT(tx->tx_objset->os_sa);
        dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
 
        rc = -dmu_tx_assign(tx, TXG_WAIT);
@@ -165,19 +164,19 @@ osd_oi_create(const struct lu_env *env, struct osd_device *o,
                return rc;
        }
 
-       oid = zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
-                              DMU_OT_DIRECTORY_CONTENTS,
-                              14, /* == ZFS fzap_default_block_shift */
-                              DN_MAX_INDBLKSHIFT, /* indirect block shift */
-                              DMU_OT_SA, DN_MAX_BONUSLEN, tx);
+       oid = osd_zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
+                                  DMU_OT_DIRECTORY_CONTENTS,
+                                  14, /* == ZFS fzap_default_block_shift */
+                                  DN_MAX_INDBLKSHIFT,
+                                  0, tx);
 
        rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
        if (rc)
                goto commit;
+       memset(la, 0, sizeof(*la));
        la->la_valid = LA_MODE | LA_UID | LA_GID;
        la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
-       la->la_uid = la->la_gid = 0;
-       rc = __osd_attr_init(env, o, sa_hdl, tx, la, parent);
+       rc = __osd_attr_init(env, o, NULL, sa_hdl, tx, la, parent, NULL);
        sa_handle_destroy(sa_hdl);
        if (rc)
                goto commit;
@@ -261,6 +260,14 @@ int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
 
        rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
        if (rc != 0) {
+               /* During upgrade, OST FLDB might not be loaded because
+                * OST FLDB is not created until 2.6, so if some DNE
+                * filesystem upgrade from 2.5 to 2.7/2.8, they will
+                * not be able to find the sequence from local FLDB
+                * cache see fld_index_init(). */
+               if (rc == -ENOENT && osd->od_is_ost)
+                       RETURN(1);
+
                if (rc != -ENOENT)
                        CERROR("%s: "DFID" lookup failed: rc = %d\n",
                               osd_name(osd), PFID(fid), rc);
@@ -423,7 +430,7 @@ static void osd_fid2str(char *buf, const struct lu_fid *fid)
  */
 static uint64_t
 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
-                   char *buf)
+                   char *buf, dnode_t **zdn)
 {
        struct osd_oi *oi;
 
@@ -431,16 +438,23 @@ osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
        oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
        if (buf)
                osd_fid2str(buf, fid);
+       if (zdn)
+               *zdn = oi->oi_dn;
 
        return oi->oi_zapid;
 }
 
 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
-                           const struct lu_fid *fid, char *buf, int bufsize)
+                           const struct lu_fid *fid, char *buf, int bufsize,
+                           dnode_t **zdn)
 {
        uint64_t zapid;
 
        LASSERT(fid);
+       LASSERT(!fid_is_acct(fid));
+
+       if (zdn != NULL)
+               *zdn = NULL;
 
        if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
                zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
@@ -452,13 +466,11 @@ uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
                        zapid = osd->od_root;
                        if (buf)
                                strncpy(buf, name, bufsize);
-                       if (fid_is_acct(fid))
-                               zapid = MASTER_NODE_OBJ;
                } else {
-                       zapid = osd_get_idx_for_fid(osd, fid, buf);
+                       zapid = osd_get_idx_for_fid(osd, fid, buf, NULL);
                }
        } else {
-               zapid = osd_get_idx_for_fid(osd, fid, buf);
+               zapid = osd_get_idx_for_fid(osd, fid, buf, zdn);
        }
 
        return zapid;
@@ -476,25 +488,23 @@ int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
 {
        struct osd_thread_info  *info = osd_oti_get(env);
        char                    *buf = info->oti_buf;
-       uint64_t                zapid;
+       dnode_t *zdn;
+       uint64_t zapid;
        int                     rc = 0;
        ENTRY;
 
        if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
                RETURN(-ENOENT);
 
-       if (unlikely(fid_is_acct(fid))) {
-               if (fid_oid(fid) == ACCT_USER_OID)
-                       *oid = dev->od_iusr_oid;
-               else
-                       *oid = dev->od_igrp_oid;
-       } else if (unlikely(fid_is_fs_root(fid))) {
+       LASSERT(!fid_is_acct(fid));
+
+       if (unlikely(fid_is_fs_root(fid))) {
                *oid = dev->od_root;
        } else {
                zapid = osd_get_name_n_idx(env, dev, fid, buf,
-                                          sizeof(info->oti_buf));
-               rc = -zap_lookup(dev->od_os, zapid, buf,
-                               8, 1, &info->oti_zde);
+                                          sizeof(info->oti_buf), &zdn);
+               rc = osd_zap_lookup(dev, zapid, zdn, buf,
+                                   8, 1, &info->oti_zde);
                if (rc)
                        RETURN(rc);
                *oid = info->oti_zde.lzd_reg.zde_dnode;
@@ -519,8 +529,8 @@ osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
 
        oi = o->od_oi_table[key];
        if (oi) {
-               if (oi->oi_db)
-                       sa_buf_rele(oi->oi_db, osd_obj_tag);
+               if (oi->oi_dn)
+                       osd_dnode_rele(oi->oi_dn);
                OBD_FREE_PTR(oi);
                o->od_oi_table[key] = NULL;
        }
@@ -550,7 +560,7 @@ osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
        }
 
        o->od_oi_table[key] = oi;
-       __osd_obj2dbuf(env, o->od_os, oi->oi_zapid, &oi->oi_db);
+       __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
 
        return 0;
 }
@@ -658,31 +668,13 @@ static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
 static int
 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
 {
-       uint64_t         odb, sdb;
-       int              rc;
+       uint64_t sdb;
+       int rc;
        ENTRY;
 
        rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
-       if (rc)
-               RETURN(rc);
-
-       o->od_O_id = sdb;
-
-       /* Create on-disk indexes to maintain per-UID/GID inode usage.
-        * Those new indexes are created in the top-level ZAP outside the
-        * namespace in order not to confuse ZPL which might interpret those
-        * indexes as directories and assume the values are object IDs */
-       rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
-                       oid2name(ACCT_USER_OID), &odb);
-       if (rc)
-               RETURN(rc);
-       o->od_iusr_oid = odb;
-
-       rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
-                       oid2name(ACCT_GROUP_OID), &odb);
-       if (rc)
-               RETURN(rc);
-       o->od_igrp_oid = odb;
+       if (!rc)
+               o->od_O_id = sdb;
 
        RETURN(rc);
 }
@@ -764,3 +756,143 @@ int osd_options_init(void)
 
        return 0;
 }
+
+/*
+ * the following set of functions are used to maintain per-thread
+ * cache of FID->ino mapping. this mechanism is used to avoid
+ * expensive LU/OI lookups.
+ */
+struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
+                                    struct osd_device *osd,
+                                    const struct lu_fid *fid)
+{
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_idmap_cache *idc = oti->oti_ins_cache;
+       int i;
+
+       for (i = 0; i < oti->oti_ins_cache_used; i++) {
+               if (!lu_fid_eq(&idc[i].oic_fid, fid))
+                       continue;
+               if (idc[i].oic_dev != osd)
+                       continue;
+
+               return idc + i;
+       }
+
+       return NULL;
+}
+
+struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
+                                   struct osd_device *osd,
+                                   const struct lu_fid *fid)
+{
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_idmap_cache *idc;
+       int i;
+
+       if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
+               i = oti->oti_ins_cache_size * 2;
+               LASSERT(i < 1000);
+               if (i == 0)
+                       i = OSD_INS_CACHE_SIZE;
+               OBD_ALLOC(idc, sizeof(*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));
+               }
+               oti->oti_ins_cache = idc;
+               oti->oti_ins_cache_size = i;
+       }
+
+       idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
+       idc->oic_fid = *fid;
+       idc->oic_dev = osd;
+       idc->oic_dnode = 0;
+       idc->oic_remote = 0;
+
+       return idc;
+}
+
+/**
+ * Lookup mapping for the given fid in the cache
+ *
+ * Initialize a new one if not found. the initialization checks whether
+ * the object is local or remote. for the local objects, OI is used to
+ * learn dnode#. the function is used when the caller has no information
+ * about the object, e.g. at dt_insert().
+ */
+struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
+                                            struct osd_device *osd,
+                                            const struct lu_fid *fid)
+{
+       struct osd_idmap_cache *idc;
+       int rc;
+
+       LASSERT(!fid_is_acct(fid));
+
+       idc = osd_idc_find(env, osd, fid);
+       if (idc != NULL)
+               return idc;
+
+       /* new mapping is needed */
+       idc = osd_idc_add(env, osd, fid);
+       if (IS_ERR(idc))
+               return idc;
+
+       /* initialize it */
+       rc = osd_remote_fid(env, osd, fid);
+       if (unlikely(rc < 0))
+               return ERR_PTR(rc);
+
+       if (rc == 0) {
+               /* the object is local, lookup in OI */
+               uint64_t dnode;
+
+               rc = osd_fid_lookup(env, osd, fid, &dnode);
+               if (unlikely(rc < 0)) {
+                       CERROR("%s: can't lookup: rc = %d\n",
+                              osd->od_svname, rc);
+                       return ERR_PTR(rc);
+               }
+               LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
+               idc->oic_dnode = dnode;
+       } else {
+               /* the object is remote */
+               idc->oic_remote = 1;
+       }
+
+       return idc;
+}
+
+/*
+ * lookup mapping for given FID and fill it from the given object.
+ * the object is local by definition.
+ */
+int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
+                         struct osd_object *obj)
+{
+       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
+       struct osd_idmap_cache *idc;
+
+       idc = osd_idc_find(env, osd, fid);
+       if (idc != NULL) {
+               if (obj->oo_dn == NULL)
+                       return 0;
+               idc->oic_dnode = obj->oo_dn->dn_object;
+               return 0;
+       }
+
+       /* new mapping is needed */
+       idc = osd_idc_add(env, osd, fid);
+       if (IS_ERR(idc))
+               return PTR_ERR(idc);
+
+       if (obj->oo_dn)
+               idc->oic_dnode = obj->oo_dn->dn_object;
+
+       return 0;
+}