4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/md_local_object.c
36 * Lustre Local Object create APIs
37 * 'create on first mount' facility. Files registed under llo module will
38 * be created on first mount.
40 * Author: Pravin Shelar <pravin.shelar@sun.com>
43 #define DEBUG_SUBSYSTEM S_CLASS
45 #include <obd_support.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48 #include <lu_object.h>
49 #include <libcfs/list.h>
50 #include <md_object.h>
53 /** List head to hold list of objects to be created. */
54 static cfs_list_t llo_lobj_list;
56 /** Lock to protect list manipulations */
57 static struct mutex llo_lock;
60 * Structure used to maintain state of path parsing.
61 * \see llo_find_entry, llo_store_resolve
63 struct llo_find_hint {
64 struct lu_fid *lfh_cfid;
65 struct md_device *lfh_md;
66 struct md_object *lfh_pobj;
70 * Thread Local storage for this module.
72 struct llo_thread_info {
73 /** buffer to resolve path */
74 char lti_buf[DT_MAX_PATH];
75 /** used for path resolve */
76 struct lu_fid lti_fid;
77 /** used to pass child object fid */
78 struct lu_fid lti_cfid;
79 struct llo_find_hint lti_lfh;
80 struct md_op_spec lti_spc;
81 struct md_attr lti_ma;
82 struct lu_name lti_lname;
85 LU_KEY_INIT(llod_global, struct llo_thread_info);
86 LU_KEY_FINI(llod_global, struct llo_thread_info);
88 static struct lu_context_key llod_key = {
89 .lct_tags = LCT_MD_THREAD,
90 .lct_init = llod_global_key_init,
91 .lct_fini = llod_global_key_fini
94 static inline struct llo_thread_info * llo_env_info(const struct lu_env *env)
96 return lu_context_key_get(&env->le_ctx, &llod_key);
100 * Search md object for given fid.
102 static struct md_object *llo_locate(const struct lu_env *env,
103 struct md_device *md,
104 const struct lu_fid *fid)
106 struct lu_object *obj;
107 struct md_object *mdo;
109 obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
111 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
112 LASSERT(obj != NULL);
113 mdo = (struct md_object *) obj;
115 mdo = (struct md_object *)obj;
120 * Lookup FID for object named \a name in directory \a pobj.
122 static int llo_lookup(const struct lu_env *env,
123 struct md_object *pobj,
127 struct llo_thread_info *info = llo_env_info(env);
128 struct lu_name *lname = &info->lti_lname;
129 struct md_op_spec *spec = &info->lti_spc;
131 spec->sp_feat = NULL;
132 spec->sp_cr_flags = 0;
133 spec->sp_cr_lookup = 0;
134 spec->sp_cr_mode = 0;
136 lname->ln_name = name;
137 lname->ln_namelen = strlen(name);
139 return mdo_lookup(env, pobj, lname, fid, spec);
143 * Function to look up path component, this is passed to parsing
144 * function. \see llo_store_resolve
146 * \retval rc returns error code for lookup or locate operation
148 * pointer to object is returned in data (lfh->lfh_pobj)
150 static int llo_find_entry(const struct lu_env *env,
151 const char *name, void *data)
153 struct llo_find_hint *lfh = data;
154 struct md_device *md = lfh->lfh_md;
155 struct lu_fid *fid = lfh->lfh_cfid;
156 struct md_object *obj = lfh->lfh_pobj;
159 /* lookup fid for object */
160 result = llo_lookup(env, obj, name, fid);
161 lu_object_put(env, &obj->mo_lu);
164 /* get md object for fid that we got in lookup */
165 obj = llo_locate(env, md, fid);
167 result = PTR_ERR(obj);
174 static struct md_object *llo_reg_open(const struct lu_env *env,
175 struct md_device *md,
183 result = llo_lookup(env, p, name, fid);
185 o = llo_locate(env, md, fid);
193 * Resolve given \a path, on success function returns
194 * md object for last directory and \a fid points to
197 struct md_object *llo_store_resolve(const struct lu_env *env,
198 struct md_device *md,
199 struct dt_device *dt,
203 struct llo_thread_info *info = llo_env_info(env);
204 struct llo_find_hint *lfh = &info->lti_lfh;
205 char *local = info->lti_buf;
206 struct md_object *obj;
209 strncpy(local, path, DT_MAX_PATH);
210 local[DT_MAX_PATH - 1] = '\0';
214 /* start path resolution from backend fs root. */
215 result = dt->dd_ops->dt_root_get(env, dt, fid);
217 /* get md object for root */
218 obj = llo_locate(env, md, fid);
220 /* start path parser from root md */
222 result = dt_path_parser(env, local, llo_find_entry, lfh);
224 obj = ERR_PTR(result);
229 obj = ERR_PTR(result);
233 EXPORT_SYMBOL(llo_store_resolve);
236 * Returns md object for \a objname in given \a dirname.
238 struct md_object *llo_store_open(const struct lu_env *env,
239 struct md_device *md,
240 struct dt_device *dt,
245 struct md_object *obj;
246 struct md_object *dir;
248 /* search md object for parent dir */
249 dir = llo_store_resolve(env, md, dt, dirname, fid);
251 obj = llo_reg_open(env, md, dir, objname, fid);
252 lu_object_put(env, &dir->mo_lu);
258 EXPORT_SYMBOL(llo_store_open);
260 static struct md_object *llo_create_obj(const struct lu_env *env,
261 struct md_device *md,
262 struct md_object *dir,
264 const struct lu_fid *fid,
265 const struct dt_index_features *feat)
267 struct llo_thread_info *info = llo_env_info(env);
268 struct md_object *mdo;
269 struct md_attr *ma = &info->lti_ma;
270 struct md_op_spec *spec = &info->lti_spc;
271 struct lu_name *lname = &info->lti_lname;
272 struct lu_attr *la = &ma->ma_attr;
275 mdo = llo_locate(env, md, fid);
279 lname->ln_name = objname;
280 lname->ln_namelen = strlen(objname);
282 spec->sp_feat = feat;
283 spec->sp_cr_flags = 0;
284 spec->sp_cr_lookup = 1;
285 spec->sp_cr_mode = 0;
287 if (feat == &dt_directory_features)
288 la->la_mode = S_IFDIR | S_IXUGO;
290 la->la_mode = S_IFREG;
292 la->la_mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
293 la->la_uid = la->la_gid = 0;
294 la->la_valid = LA_MODE | LA_UID | LA_GID;
299 rc = mdo_create(env, dir, lname, mdo, spec, ma);
302 lu_object_put(env, &mdo->mo_lu);
310 * Create md object, object could be diretcory or
311 * special index defined by \a feat in \a directory.
314 * \param dirname parent directory
315 * \param objname file name
316 * \param fid object fid
317 * \param feat index features required for directory create
320 struct md_object *llo_store_create_index(const struct lu_env *env,
321 struct md_device *md,
322 struct dt_device *dt,
325 const struct lu_fid *fid,
326 const struct dt_index_features *feat)
328 struct llo_thread_info *info = llo_env_info(env);
329 struct md_object *obj;
330 struct md_object *dir;
331 struct lu_fid *ignore = &info->lti_fid;
333 dir = llo_store_resolve(env, md, dt, dirname, ignore);
335 obj = llo_create_obj(env, md, dir, objname, fid, feat);
336 lu_object_put(env, &dir->mo_lu);
343 EXPORT_SYMBOL(llo_store_create_index);
346 * Create md object for regular file in \a directory.
349 * \param dirname parent directory
350 * \param objname file name
351 * \param fid object fid.
354 struct md_object *llo_store_create(const struct lu_env *env,
355 struct md_device *md,
356 struct dt_device *dt,
359 const struct lu_fid *fid)
361 return llo_store_create_index(env, md, dt, dirname,
365 EXPORT_SYMBOL(llo_store_create);
368 * Register object for 'create on first mount' facility.
369 * objects are created in order of registration.
372 void llo_local_obj_register(struct lu_local_obj_desc *llod)
374 mutex_lock(&llo_lock);
375 cfs_list_add_tail(&llod->llod_linkage, &llo_lobj_list);
376 mutex_unlock(&llo_lock);
379 EXPORT_SYMBOL(llo_local_obj_register);
381 void llo_local_obj_unregister(struct lu_local_obj_desc *llod)
383 mutex_lock(&llo_lock);
384 cfs_list_del(&llod->llod_linkage);
385 mutex_unlock(&llo_lock);
388 EXPORT_SYMBOL(llo_local_obj_unregister);
391 * Created registed objects.
394 int llo_local_objects_setup(const struct lu_env *env,
395 struct md_device * md,
396 struct dt_device *dt)
398 struct llo_thread_info *info = llo_env_info(env);
400 struct lu_local_obj_desc *scan;
401 struct md_object *mdo;
405 fid = &info->lti_cfid;
406 mutex_lock(&llo_lock);
408 cfs_list_for_each_entry(scan, &llo_lobj_list, llod_linkage) {
409 lu_local_obj_fid(fid, scan->llod_oid);
412 dir = scan->llod_dir;
414 if (scan->llod_is_index)
415 mdo = llo_store_create_index(env, md, dt ,
416 dir, scan->llod_name,
420 mdo = llo_store_create(env, md, dt,
421 dir, scan->llod_name,
423 if (IS_ERR(mdo) && PTR_ERR(mdo) != -EEXIST) {
425 CERROR("creating obj [%s] fid = "DFID" rc = %d\n",
426 scan->llod_name, PFID(fid), rc);
431 lu_object_put(env, &mdo->mo_lu);
435 mutex_unlock(&llo_lock);
439 EXPORT_SYMBOL(llo_local_objects_setup);
441 int llo_global_init(void)
445 CFS_INIT_LIST_HEAD(&llo_lobj_list);
446 mutex_init(&llo_lock);
448 LU_CONTEXT_KEY_INIT(&llod_key);
449 result = lu_context_key_register(&llod_key);
453 void llo_global_fini(void)
455 lu_context_key_degister(&llod_key);
456 LASSERT(cfs_list_empty(&llo_lobj_list));