Whamcloud - gitweb
LU-2129 llog: protect llog write against concurrent read
[fs/lustre-release.git] / lustre / obdclass / local_storage.c
index 26d6e18..3dd76e6 100644 (file)
@@ -139,8 +139,7 @@ static struct lu_device_type ls_lu_type = {
        .ldt_ops  = &ls_device_type_ops,
 };
 
-static struct ls_device *ls_device_get(const struct lu_env *env,
-                                      struct dt_device *dev)
+struct ls_device *ls_device_get(struct dt_device *dev)
 {
        struct ls_device *ls;
 
@@ -174,7 +173,7 @@ out_ls:
        RETURN(ls);
 }
 
-static void ls_device_put(const struct lu_env *env, struct ls_device *ls)
+void ls_device_put(const struct lu_env *env, struct ls_device *ls)
 {
        LASSERT(env);
        if (!cfs_atomic_dec_and_test(&ls->ls_refcount))
@@ -461,7 +460,7 @@ struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
        } else {
                struct ls_device *ls;
 
-               ls = ls_device_get(env, dt);
+               ls = ls_device_get(dt);
                if (IS_ERR(ls)) {
                        dto = ERR_PTR(PTR_ERR(ls));
                } else {
@@ -555,7 +554,7 @@ local_index_find_or_create_with_fid(const struct lu_env *env,
        } else {
                struct ls_device *ls;
 
-               ls = ls_device_get(env, dt);
+               ls = ls_device_get(dt);
                if (IS_ERR(ls)) {
                        dto = ERR_PTR(PTR_ERR(ls));
                } else {
@@ -581,7 +580,7 @@ local_index_find_or_create_with_fid(const struct lu_env *env,
 }
 EXPORT_SYMBOL(local_index_find_or_create_with_fid);
 
-static struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
+struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
 {
        struct local_oid_storage *los, *ret = NULL;
 
@@ -595,6 +594,15 @@ static struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
        return ret;
 }
 
+void dt_los_put(struct local_oid_storage *los)
+{
+       if (cfs_atomic_dec_and_test(&los->los_refcount))
+               /* should never happen, only local_oid_storage_fini should
+                * drop refcount to zero */
+               LBUG();
+       return;
+}
+
 /**
  * Initialize local OID storage for required sequence.
  * That may be needed for services that uses local files and requires
@@ -619,14 +627,14 @@ int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
        struct dt_thread_info   *dti = dt_info(env);
        struct ls_device        *ls;
        struct los_ondisk        losd;
-       struct dt_object        *o;
        struct dt_object        *root = NULL;
+       struct dt_object        *o = NULL;
        struct thandle          *th;
        int                      rc;
 
        ENTRY;
 
-       ls = ls_device_get(env, dev);
+       ls = ls_device_get(dev);
        if (IS_ERR(ls))
                RETURN(PTR_ERR(ls));
 
@@ -646,12 +654,6 @@ int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
        cfs_atomic_inc(&ls->ls_refcount);
        cfs_list_add(&(*los)->los_list, &ls->ls_los_list);
 
-       /* initialize data allowing to generate new fids,
-        * literally we need a sequence */
-       o = ls_locate(env, ls, first_fid);
-       if (IS_ERR(o))
-               GOTO(out_los, rc = PTR_ERR(o));
-
        rc = dt_root_get(env, dev, &dti->dti_fid);
        if (rc)
                GOTO(out_los, rc);
@@ -660,11 +662,25 @@ int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
        if (IS_ERR(root))
                GOTO(out_los, rc = PTR_ERR(root));
 
-       if (dt_try_as_dir(env, root) == 0)
-               GOTO(out_los, rc = -ENOTDIR);
+       snprintf(dti->dti_buf, sizeof(dti->dti_buf), "seq-%Lx-lastid",
+                fid_seq(first_fid));
+       rc = dt_lookup_dir(env, root, dti->dti_buf, &dti->dti_fid);
+       if (rc != 0 && rc != -ENOENT)
+               GOTO(out_los, rc);
+
+       /* initialize data allowing to generate new fids,
+        * literally we need a sequence */
+       if (rc == 0)
+               o = ls_locate(env, ls, &dti->dti_fid);
+       else
+               o = ls_locate(env, ls, first_fid);
+       if (IS_ERR(o))
+               GOTO(out_los, rc = PTR_ERR(o));
 
        dt_write_lock(env, o, 0);
        if (!dt_object_exists(o)) {
+               LASSERT(rc == -ENOENT);
+
                th = dt_trans_create(env, dev);
                if (IS_ERR(th))
                        GOTO(out_lock, rc = PTR_ERR(th));
@@ -678,8 +694,6 @@ int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
                if (rc)
                        GOTO(out_trans, rc);
 
-               snprintf(dti->dti_buf, sizeof(dti->dti_buf),
-                       "seq-%Lx-lastid", fid_seq(first_fid));
                rc = dt_declare_insert(env, root,
                                       (const struct dt_rec *)lu_object_fid(&o->do_lu),
                                       (const struct dt_key *)dti->dti_buf,
@@ -726,12 +740,22 @@ int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
                rc = dt_record_write(env, o, &dti->dti_lb, &dti->dti_off, th);
                if (rc)
                        GOTO(out_trans, rc);
-               rc = dt_insert(env, root,
-                              (const struct dt_rec *)lu_object_fid(&o->do_lu),
-                              (const struct dt_key *)dti->dti_buf, th,
-                              BYPASS_CAPA, 1);
-               if (rc)
-                       GOTO(out_trans, rc);
+#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 3, 90, 0)
+#error "fix this before release"
+#endif
+               /*
+                * there is one technical debt left in Orion:
+                * proper hanlding of named vs no-name objects.
+                * Llog objects have name always as they are placed in O/d/...
+                */
+               if (fid_seq(lu_object_fid(&o->do_lu)) != FID_SEQ_LLOG) {
+                       rc = dt_insert(env, root,
+                                      (const struct dt_rec *)first_fid,
+                                      (const struct dt_key *)dti->dti_buf,
+                                      th, BYPASS_CAPA, 1);
+                       if (rc)
+                               GOTO(out_trans, rc);
+               }
 out_trans:
                dt_trans_stop(env, dev, th);
        } else {
@@ -748,12 +772,15 @@ out_trans:
 out_lock:
        dt_write_unlock(env, o);
 out_los:
-       if (root)
+       if (root != NULL && !IS_ERR(root))
                lu_object_put_nocache(env, &root->do_lu);
-       if (rc) {
+
+       if (rc != 0) {
+               cfs_list_del(&(*los)->los_list);
+               cfs_atomic_dec(&ls->ls_refcount);
                OBD_FREE_PTR(*los);
                *los = NULL;
-               if (o)
+               if (o != NULL && !IS_ERR(o))
                        lu_object_put_nocache(env, &o->do_lu);
        } else {
                (*los)->los_seq = fid_seq(first_fid);