Whamcloud - gitweb
919c2840fd194930bdf4c6206c57a4491e08e78f
[fs/lustre-release.git] / lustre / obdclass / md_local_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/md_local_object.c
37  *
38  * Lustre Local Object create APIs
39  * 'create on first mount' facility. Files registed under llo module will
40  * be created on first mount.
41  *
42  * Author: Pravin Shelar  <pravin.shelar@sun.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_CLASS
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49
50 #include <obd_support.h>
51 #include <lustre_disk.h>
52 #include <lustre_fid.h>
53 #include <lu_object.h>
54 #include <libcfs/list.h>
55 #include <md_object.h>
56
57
58 /** List head to hold list of objects to be created. */
59 static struct list_head llo_lobj_list;
60
61 /** Lock to protect list manipulations */
62 static struct mutex     llo_lock;
63
64 /**
65  * Structure used to maintain state of path parsing.
66  * \see llo_find_entry, llo_store_resolve
67  */
68 struct llo_find_hint {
69         struct lu_fid        *lfh_cfid;
70         struct md_device     *lfh_md;
71         struct md_object     *lfh_pobj;
72 };
73
74 /**
75  * Thread Local storage for this module.
76  */
77 struct llo_thread_info {
78         /** buffer to resolve path */
79         char                    lti_buf[DT_MAX_PATH];
80         /** used for path resolve */
81         struct lu_fid           lti_fid;
82         /** used to pass child object fid */
83         struct lu_fid           lti_cfid;
84         struct llo_find_hint    lti_lfh;
85         struct md_op_spec       lti_spc;
86         struct md_attr          lti_ma;
87         struct lu_name          lti_lname;
88 };
89
90 LU_KEY_INIT(llod_global, struct llo_thread_info);
91 LU_KEY_FINI(llod_global, struct llo_thread_info);
92
93 static struct lu_context_key llod_key = {
94         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
95         .lct_init = llod_global_key_init,
96         .lct_fini = llod_global_key_fini
97 };
98
99 static inline struct llo_thread_info * llo_env_info(const struct lu_env *env)
100 {
101         return lu_context_key_get(&env->le_ctx,  &llod_key);
102 }
103
104 /**
105  * Search md object for given fid.
106  */
107 static struct md_object *llo_locate(const struct lu_env *env,
108                                     struct md_device *md,
109                                     const struct lu_fid *fid)
110 {
111         struct lu_object *obj;
112         struct md_object *mdo;
113
114         obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
115         if (!IS_ERR(obj)) {
116                 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
117                 LASSERT(obj != NULL);
118                 mdo = (struct md_object *) obj;
119         } else
120                 mdo = (struct md_object *)obj;
121         return mdo;
122 }
123
124 /**
125  * Lookup FID for object named \a name in directory \a pobj.
126  */
127 static int llo_lookup(const struct lu_env  *env,
128                       struct md_object *pobj,
129                       const char *name,
130                       struct lu_fid *fid)
131 {
132         struct llo_thread_info *info = llo_env_info(env);
133         struct lu_name          *lname = &info->lti_lname;
134         struct md_op_spec       *spec = &info->lti_spc;
135
136         spec->sp_feat = NULL;
137         spec->sp_cr_flags = 0;
138         spec->sp_cr_lookup = 1;
139         spec->sp_cr_mode = 0;
140         spec->sp_ck_split = 0;
141
142         lname->ln_name = name;
143         lname->ln_namelen = strlen(name);
144
145         return mdo_lookup(env, pobj, lname, fid, spec);
146 }
147
148 /**
149  * Function to look up path component, this is passed to parsing
150  * function. \see llo_store_resolve
151  */
152 static int llo_find_entry(const struct lu_env  *env,
153                           const char *name, void *data)
154 {
155         struct llo_find_hint    *lfh = data;
156         struct md_device        *md = lfh->lfh_md;
157         struct lu_fid           *fid = lfh->lfh_cfid;
158         struct md_object        *obj = lfh->lfh_pobj;
159         int                     result;
160
161         /* lookup fid for object */
162         result = llo_lookup(env, obj, name, fid);
163         lu_object_put(env, &obj->mo_lu);
164
165         if (result == 0) {
166                 /* get md object for fid that we got in lookup */
167                 obj = llo_locate(env, md, fid);
168                 if (IS_ERR(obj))
169                         result = PTR_ERR(obj);
170         }
171
172         lfh->lfh_pobj = obj;
173         return result;
174 }
175
176 static struct md_object *llo_reg_open(const struct lu_env *env,
177                                       struct md_device *md,
178                                       struct md_object *p,
179                                       const char *name,
180                                       struct lu_fid *fid)
181 {
182         struct md_object *o;
183         int result;
184
185         result = llo_lookup(env, p, name, fid);
186         if (result == 0)
187                 o = llo_locate(env, md, fid);
188         else
189                 o = ERR_PTR(result);
190
191         return o;
192 }
193
194 /**
195  * Resolve given \a path, on success function returns
196  * md object for last directory and \a fid points to
197  * its fid.
198  */
199 struct md_object *llo_store_resolve(const struct lu_env *env,
200                                     struct md_device *md,
201                                     struct dt_device *dt,
202                                     const char *path,
203                                     struct lu_fid *fid)
204 {
205         struct llo_thread_info *info = llo_env_info(env);
206         struct llo_find_hint *lfh = &info->lti_lfh;
207         char *local = info->lti_buf;
208         struct md_object        *obj = lfh->lfh_pobj;
209         int result;
210
211         strncpy(local, path, DT_MAX_PATH);
212         local[DT_MAX_PATH - 1] = '\0';
213
214         lfh->lfh_md = md;
215         lfh->lfh_cfid = fid;
216         /* start path resolution from backend fs root. */
217         result = dt->dd_ops->dt_root_get(env, dt, fid);
218         if (result == 0) {
219                 /* get md object for root */
220                 obj = llo_locate(env, md, fid);
221                 if (!IS_ERR(obj)) {
222                         /* start path parser from root md */
223                         lfh->lfh_pobj = obj;
224                         result = dt_path_parser(env, local, llo_find_entry, lfh);
225                         if (result != 0)
226                                 obj = ERR_PTR(result);
227                 }
228         } else {
229                 obj = ERR_PTR(result);
230         }
231         return obj;
232 }
233 EXPORT_SYMBOL(llo_store_resolve);
234
235 /**
236  * Returns md object for \a objname in given \a dirname.
237  */
238 struct md_object *llo_store_open(const struct lu_env *env,
239                                  struct md_device *md,
240                                  struct dt_device *dt,
241                                  const char *dirname,
242                                  const char *objname,
243                                  struct lu_fid *fid)
244 {
245         struct md_object *obj;
246         struct md_object *dir;
247
248         /* search md object for parent dir */
249         dir = llo_store_resolve(env, md, dt, dirname, fid);
250         if (!IS_ERR(dir)) {
251                 obj = llo_reg_open(env, md, dir, objname, fid);
252                 lu_object_put(env, &dir->mo_lu);
253         } else
254                 obj = dir;
255
256         return obj;
257 }
258 EXPORT_SYMBOL(llo_store_open);
259
260 static struct md_object *llo_create_obj(const struct lu_env *env,
261                                         struct md_device *md,
262                                         struct md_object *dir,
263                                         const char *objname,
264                                         const struct lu_fid *fid,
265                                         const struct dt_index_features *feat)
266 {
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;
273         int rc;
274
275         mdo = llo_locate(env, md, fid);
276         if (IS_ERR(mdo))
277                 return mdo;
278
279         lname->ln_name = objname;
280         lname->ln_namelen = strlen(objname);
281
282         spec->sp_feat = feat;
283         spec->sp_cr_flags = 0;
284         spec->sp_cr_lookup = 1;
285         spec->sp_cr_mode = 0;
286         spec->sp_ck_split = 0;
287
288         if (feat == &dt_directory_features)
289                 la->la_mode = S_IFDIR;
290         else
291                 la->la_mode = S_IFREG;
292
293         la->la_mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
294         la->la_uid = la->la_gid = 0;
295         la->la_valid = LA_MODE | LA_UID | LA_GID;
296
297         ma->ma_valid = 0;
298         ma->ma_need = 0;
299
300         rc = mdo_create(env, dir, lname, mdo, spec, ma);
301
302         if (rc) {
303                 lu_object_put(env, &mdo->mo_lu);
304                 mdo = ERR_PTR(rc);
305         }
306
307         return mdo;
308 }
309
310 /**
311  * Create md object, object could be diretcory or
312  * special index defined by \a feat in \a directory.
313  *
314  *       \param  md       device
315  *       \param  dir      parent directory
316  *       \param  objname  file name
317  *       \param  fid      object fid
318  *       \param  feat     index features required for directory create
319  */
320
321 struct md_object *llo_store_create_index(const struct lu_env *env,
322                                          struct md_device *md,
323                                          struct dt_device *dt,
324                                          const char *dirname,
325                                          const char *objname,
326                                          const struct lu_fid *fid,
327                                          const struct dt_index_features *feat)
328 {
329         struct llo_thread_info *info = llo_env_info(env);
330         struct md_object *obj;
331         struct md_object *dir;
332         struct lu_fid *ignore = &info->lti_fid;
333
334         dir = llo_store_resolve(env, md, dt, dirname, ignore);
335         if (!IS_ERR(dir)) {
336                 obj = llo_create_obj(env, md, dir, objname, fid, feat);
337                 lu_object_put(env, &dir->mo_lu);
338         } else {
339                 obj = dir;
340         }
341         return obj;
342 }
343
344 EXPORT_SYMBOL(llo_store_create_index);
345
346 /**
347  * Create md object for regular file in \a directory.
348  *
349  *       \param  md       device
350  *       \param  dir      parent directory
351  *       \param  objname  file name
352  *       \param  fid      object fid.
353  */
354
355 struct md_object *llo_store_create(const struct lu_env *env,
356                                    struct md_device *md,
357                                    struct dt_device *dt,
358                                    const char *dirname,
359                                    const char *objname,
360                                    const struct lu_fid *fid)
361 {
362         return llo_store_create_index(env, md, dt, dirname,
363                                       objname, fid, NULL);
364 }
365
366 EXPORT_SYMBOL(llo_store_create);
367
368 /**
369  * Register object for 'create on first mount' facility.
370  */
371
372 int llo_local_obj_register(struct lu_local_obj_desc *llod)
373 {
374         mutex_lock(&llo_lock);
375         list_add(&llod->llod_linkage, &llo_lobj_list);
376         mutex_unlock(&llo_lock);
377
378         return 0;
379 }
380
381 EXPORT_SYMBOL(llo_local_obj_register);
382
383 /**
384  * Created registed objects.
385  */
386
387 int llo_local_objects_setup(const struct lu_env *env,
388                              struct md_device * md,
389                              struct dt_device *dt)
390 {
391         struct llo_thread_info *info = llo_env_info(env);
392         struct lu_fid *fid;
393         struct lu_local_obj_desc *scan;
394         struct md_object *mdo;
395         int rc = 0;
396
397         fid = &info->lti_cfid;
398
399         mutex_lock(&llo_lock);
400
401         list_for_each_entry(scan, &llo_lobj_list, llod_linkage) {
402
403                 lu_local_obj_fid(fid, scan->llod_oid);
404
405                 if (scan->llod_is_index)
406                         mdo = llo_store_create_index(env, md, dt ,
407                                                      "", scan->llod_name,
408                                                      fid,
409                                                      scan->llod_feat);
410                 else
411                         mdo = llo_store_create(env, md, dt,
412                                                "", scan->llod_name,
413                                                fid);
414                 if (IS_ERR(mdo) && PTR_ERR(mdo) != -EEXIST) {
415                         rc = PTR_ERR(mdo);
416                         CERROR("creating obj [%s] fid = "DFID" rc = %d\n",
417                                scan->llod_name, PFID(fid), rc);
418                         goto out;
419                 }
420
421                 if (!IS_ERR(mdo))
422                         lu_object_put(env, &mdo->mo_lu);
423         }
424
425 out:
426         mutex_unlock(&llo_lock);
427         return rc;
428 }
429
430 EXPORT_SYMBOL(llo_local_objects_setup);
431
432 int llo_global_init(void)
433 {
434         int result;
435
436         CFS_INIT_LIST_HEAD(&llo_lobj_list);
437         mutex_init(&llo_lock);
438
439         LU_CONTEXT_KEY_INIT(&llod_key);
440         result = lu_context_key_register(&llod_key);
441         return result;
442 }
443
444 void llo_global_fini(void)
445 {
446         lu_context_key_degister(&llod_key);
447 }