X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Fobdclass%2Fdt_object.c;h=23fa33acc820f4b668e2f66b261503d135f581d5;hb=9dce07db5dc5ff3e0ffa57289ac095966a88adcc;hp=c429bd2f4e0ed959c0b5ae12515bd5724cbf7cc7;hpb=6869932b552ac705f411de3362f01bd50c1f6f7d;p=fs%2Flustre-release.git diff --git a/lustre/obdclass/dt_object.c b/lustre/obdclass/dt_object.c index c429bd2..23fa33ac 100644 --- a/lustre/obdclass/dt_object.c +++ b/lustre/obdclass/dt_object.c @@ -52,6 +52,27 @@ /* fid_be_to_cpu() */ #include +struct dt_find_hint { + struct lu_fid *dfh_fid; + struct dt_device *dfh_dt; + struct dt_object *dfh_o; +}; + +struct dt_thread_info { + char dti_buf[DT_MAX_PATH]; + struct dt_find_hint dti_dfh; +}; + +/* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */ +LU_KEY_INIT(dt_global, struct dt_thread_info); +LU_KEY_FINI(dt_global, struct dt_thread_info); + +static struct lu_context_key dt_key = { + .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD, + .lct_init = dt_global_key_init, + .lct_fini = dt_global_key_fini +}; + /* no lock is necessary to protect the list, because call-backs * are added during system startup. Please refer to "struct dt_device". */ @@ -75,7 +96,8 @@ int dt_txn_hook_start(const struct lu_env *env, result = 0; list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) { - if (cb->dtc_txn_start == NULL) + if (cb->dtc_txn_start == NULL || + !(cb->dtc_tag & env->le_ctx.lc_tags)) continue; result = cb->dtc_txn_start(env, param, cb->dtc_cookie); if (result < 0) @@ -93,7 +115,8 @@ int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn) result = 0; list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) { - if (cb->dtc_txn_stop == NULL) + if (cb->dtc_txn_stop == NULL || + !(cb->dtc_tag & env->le_ctx.lc_tags)) continue; result = cb->dtc_txn_stop(env, txn, cb->dtc_cookie); if (result < 0) @@ -111,7 +134,8 @@ int dt_txn_hook_commit(const struct lu_env *env, struct thandle *txn) result = 0; list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) { - if (cb->dtc_txn_commit == NULL) + if (cb->dtc_txn_commit == NULL || + !(cb->dtc_tag & env->le_ctx.lc_tags)) continue; result = cb->dtc_txn_commit(env, txn, cb->dtc_cookie); if (result < 0) @@ -157,72 +181,256 @@ int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj) } EXPORT_SYMBOL(dt_try_as_dir); -extern struct lu_context_key lu_global_key; +enum dt_format_type dt_mode_to_dft(__u32 mode) +{ + enum dt_format_type result; + + switch (mode & S_IFMT) { + case S_IFDIR: + result = DFT_DIR; + break; + case S_IFREG: + result = DFT_REGULAR; + break; + case S_IFLNK: + result = DFT_SYM; + break; + case S_IFCHR: + case S_IFBLK: + case S_IFIFO: + case S_IFSOCK: + result = DFT_NODE; + break; + default: + LBUG(); + break; + } + return result; +} + +EXPORT_SYMBOL(dt_mode_to_dft); +/** + * lookup fid for object named \a name in directory \a dir. + */ static int dt_lookup(const struct lu_env *env, struct dt_object *dir, const char *name, struct lu_fid *fid) { - struct lu_fid_pack *pack = lu_context_key_get(&env->le_ctx, - &lu_global_key); - struct dt_rec *rec = (struct dt_rec *)pack; + struct dt_rec *rec = (struct dt_rec *)fid; const struct dt_key *key = (const struct dt_key *)name; int result; if (dt_try_as_dir(env, dir)) { result = dir->do_index_ops->dio_lookup(env, dir, rec, key, BYPASS_CAPA); - if (result == 0) - result = fid_unpack(pack, fid); + if (result > 0) + result = 0; + else if (result == 0) + result = -ENOENT; } else result = -ENOTDIR; return result; } -static struct dt_object *dt_locate(const struct lu_env *env, - struct dt_device *dev, - const struct lu_fid *fid) +/** + * get object for given \a fid. + */ +struct dt_object *dt_locate(const struct lu_env *env, + struct dt_device *dev, + const struct lu_fid *fid) { struct lu_object *obj; struct dt_object *dt; - obj = lu_object_find(env, dev->dd_lu_dev.ld_site, fid); + obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL); if (!IS_ERR(obj)) { obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type); LASSERT(obj != NULL); dt = container_of(obj, struct dt_object, do_lu); } else - dt = (void *)obj; + dt = (struct dt_object *)obj; return dt; } +EXPORT_SYMBOL(dt_locate); -struct dt_object *dt_store_open(const struct lu_env *env, - struct dt_device *dt, const char *name, - struct lu_fid *fid) +/** + * find a object named \a entry in given \a dfh->dfh_o directory. + */ +static int dt_find_entry(const struct lu_env *env, const char *entry, void *data) +{ + struct dt_find_hint *dfh = data; + struct dt_device *dt = dfh->dfh_dt; + struct lu_fid *fid = dfh->dfh_fid; + struct dt_object *obj = dfh->dfh_o; + int result; + + result = dt_lookup(env, obj, entry, fid); + lu_object_put(env, &obj->do_lu); + if (result == 0) { + obj = dt_locate(env, dt, fid); + if (IS_ERR(obj)) + result = PTR_ERR(obj); + } + dfh->dfh_o = obj; + return result; +} + +/** + * Abstract function which parses path name. This function feeds + * path component to \a entry_func. + */ +int dt_path_parser(const struct lu_env *env, + char *path, dt_entry_func_t entry_func, + void *data) +{ + char *e; + int rc = 0; + + while (1) { + e = strsep(&path, "/"); + if (e == NULL) + break; + + if (e[0] == 0) { + if (!path || path[0] == '\0') + break; + continue; + } + rc = entry_func(env, e, data); + if (rc) + break; + } + + return rc; +} + +static struct dt_object *dt_store_resolve(const struct lu_env *env, + struct dt_device *dt, + const char *path, + struct lu_fid *fid) { + struct dt_thread_info *info = lu_context_key_get(&env->le_ctx, + &dt_key); + struct dt_find_hint *dfh = &info->dti_dfh; + struct dt_object *obj; + char *local = info->dti_buf; int result; - struct dt_object *root; - struct dt_object *child; + dfh->dfh_dt = dt; + dfh->dfh_fid = fid; + + strncpy(local, path, DT_MAX_PATH); + local[DT_MAX_PATH - 1] = '\0'; result = dt->dd_ops->dt_root_get(env, dt, fid); if (result == 0) { - root = dt_locate(env, dt, fid); - if (!IS_ERR(root)) { - result = dt_lookup(env, root, name, fid); - if (result == 0) - child = dt_locate(env, dt, fid); + obj = dt_locate(env, dt, fid); + if (!IS_ERR(obj)) { + dfh->dfh_o = obj; + result = dt_path_parser(env, local, dt_find_entry, dfh); + if (result != 0) + obj = ERR_PTR(result); else - child = ERR_PTR(result); - lu_object_put(env, &root->do_lu); - } else { - CERROR("No root\n"); - child = (void *)root; + obj = dfh->dfh_o; } - } else - child = ERR_PTR(result); - return child; + } else { + obj = ERR_PTR(result); + } + return obj; +} + +static struct dt_object *dt_reg_open(const struct lu_env *env, + struct dt_device *dt, + struct dt_object *p, + const char *name, + struct lu_fid *fid) +{ + struct dt_object *o; + int result; + + result = dt_lookup(env, p, name, fid); + if (result == 0){ + o = dt_locate(env, dt, fid); + } + else + o = ERR_PTR(result); + + return o; +} + +/** + * Open dt object named \a filename from \a dirname directory. + * \param dt dt device + * \param fid on success, object fid is stored in *fid + */ +struct dt_object *dt_store_open(const struct lu_env *env, + struct dt_device *dt, + const char *dirname, + const char *filename, + struct lu_fid *fid) +{ + struct dt_object *file; + struct dt_object *dir; + + dir = dt_store_resolve(env, dt, dirname, fid); + if (!IS_ERR(dir)) { + file = dt_reg_open(env, dt, dir, + filename, fid); + lu_object_put(env, &dir->do_lu); + } else { + file = dir; + } + return file; } EXPORT_SYMBOL(dt_store_open); +/* dt class init function. */ +int dt_global_init(void) +{ + int result; + + LU_CONTEXT_KEY_INIT(&dt_key); + result = lu_context_key_register(&dt_key); + return result; +} + +void dt_global_fini(void) +{ + lu_context_key_degister(&dt_key); +} + +int dt_record_read(const struct lu_env *env, struct dt_object *dt, + struct lu_buf *buf, loff_t *pos) +{ + int rc; + + LASSERTF(dt != NULL, "dt is NULL when we want to read record\n"); + + rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA); + + if (rc == buf->lb_len) + rc = 0; + else if (rc >= 0) + rc = -EFAULT; + return rc; +} +EXPORT_SYMBOL(dt_record_read); + +int dt_record_write(const struct lu_env *env, struct dt_object *dt, + const struct lu_buf *buf, loff_t *pos, struct thandle *th) +{ + int rc; + + LASSERTF(dt != NULL, "dt is NULL when we want to write record\n"); + LASSERT(th != NULL); + rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1); + if (rc == buf->lb_len) + rc = 0; + else if (rc >= 0) + rc = -EFAULT; + return rc; +} +EXPORT_SYMBOL(dt_record_write); + const struct dt_index_features dt_directory_features; EXPORT_SYMBOL(dt_directory_features);