Whamcloud - gitweb
- land b_hd_ver_recov
[fs/lustre-release.git] / lustre / obdclass / dt_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/dt_object.c
37  *
38  * Dt Object.
39  * Generic functions from dt_object.h
40  *
41  * Author: Nikita Danilov <nikita@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_CLASS
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48
49 #include <obd.h>
50 #include <dt_object.h>
51 #include <libcfs/list.h>
52 /* fid_be_to_cpu() */
53 #include <lustre_fid.h>
54
55 struct dt_find_hint {
56         struct lu_fid        *dfh_fid;
57         struct dt_device     *dfh_dt;
58         struct dt_object     *dfh_o;
59 };
60
61 struct dt_thread_info {
62         char                    dti_buf[DT_MAX_PATH];
63         struct lu_fid_pack      dti_pack;
64         struct dt_find_hint     dti_dfh;
65 };
66
67 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
68 LU_KEY_INIT(dt_global, struct dt_thread_info);
69 LU_KEY_FINI(dt_global, struct dt_thread_info);
70
71 static struct lu_context_key dt_key = {
72         .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD,
73         .lct_init = dt_global_key_init,
74         .lct_fini = dt_global_key_fini
75 };
76
77 /* no lock is necessary to protect the list, because call-backs
78  * are added during system startup. Please refer to "struct dt_device".
79  */
80 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
81 {
82         list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
83 }
84 EXPORT_SYMBOL(dt_txn_callback_add);
85
86 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
87 {
88         list_del_init(&cb->dtc_linkage);
89 }
90 EXPORT_SYMBOL(dt_txn_callback_del);
91
92 int dt_txn_hook_start(const struct lu_env *env,
93                       struct dt_device *dev, struct txn_param *param)
94 {
95         int result;
96         struct dt_txn_callback *cb;
97
98         result = 0;
99         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
100                 if (cb->dtc_txn_start == NULL ||
101                     !(cb->dtc_tag & env->le_ctx.lc_tags))
102                         continue;
103                 result = cb->dtc_txn_start(env, param, cb->dtc_cookie);
104                 if (result < 0)
105                         break;
106         }
107         return result;
108 }
109 EXPORT_SYMBOL(dt_txn_hook_start);
110
111 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
112 {
113         struct dt_device       *dev = txn->th_dev;
114         struct dt_txn_callback *cb;
115         int                     result;
116
117         result = 0;
118         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
119                 if (cb->dtc_txn_stop == NULL ||
120                     !(cb->dtc_tag & env->le_ctx.lc_tags))
121                         continue;
122                 result = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
123                 if (result < 0)
124                         break;
125         }
126         return result;
127 }
128 EXPORT_SYMBOL(dt_txn_hook_stop);
129
130 int dt_txn_hook_commit(const struct lu_env *env, struct thandle *txn)
131 {
132         struct dt_device       *dev = txn->th_dev;
133         struct dt_txn_callback *cb;
134         int                     result;
135
136         result = 0;
137         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
138                 if (cb->dtc_txn_commit == NULL ||
139                     !(cb->dtc_tag & env->le_ctx.lc_tags))
140                         continue;
141                 result = cb->dtc_txn_commit(env, txn, cb->dtc_cookie);
142                 if (result < 0)
143                         break;
144         }
145         return result;
146 }
147 EXPORT_SYMBOL(dt_txn_hook_commit);
148
149 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
150 {
151
152         CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
153         return lu_device_init(&dev->dd_lu_dev, t);
154 }
155 EXPORT_SYMBOL(dt_device_init);
156
157 void dt_device_fini(struct dt_device *dev)
158 {
159         lu_device_fini(&dev->dd_lu_dev);
160 }
161 EXPORT_SYMBOL(dt_device_fini);
162
163 int dt_object_init(struct dt_object *obj,
164                    struct lu_object_header *h, struct lu_device *d)
165
166 {
167         return lu_object_init(&obj->do_lu, h, d);
168 }
169 EXPORT_SYMBOL(dt_object_init);
170
171 void dt_object_fini(struct dt_object *obj)
172 {
173         lu_object_fini(&obj->do_lu);
174 }
175 EXPORT_SYMBOL(dt_object_fini);
176
177 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
178 {
179         if (obj->do_index_ops == NULL)
180                 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
181         return obj->do_index_ops != NULL;
182 }
183 EXPORT_SYMBOL(dt_try_as_dir);
184
185 enum dt_format_type dt_mode_to_dft(__u32 mode)
186 {
187         enum dt_format_type result;
188
189         switch (mode & S_IFMT) {
190         case S_IFDIR:
191                 result = DFT_DIR;
192                 break;
193         case S_IFREG:
194                 result = DFT_REGULAR;
195                 break;
196         case S_IFLNK:
197                 result = DFT_SYM;
198                 break;
199         case S_IFCHR:
200         case S_IFBLK:
201         case S_IFIFO:
202         case S_IFSOCK:
203                 result = DFT_NODE;
204                 break;
205         default:
206                 LBUG();
207                 break;
208         }
209         return result;
210 }
211
212 EXPORT_SYMBOL(dt_mode_to_dft);
213 /**
214  * lookup fid for object named \a name in directory \a dir.
215  */
216
217 static int dt_lookup(const struct lu_env *env, struct dt_object *dir,
218                      const char *name, struct lu_fid *fid)
219 {
220         struct dt_thread_info *info = lu_context_key_get(&env->le_ctx,
221                                                          &dt_key);
222         struct lu_fid_pack *pack = &info->dti_pack;
223         struct dt_rec       *rec = (struct dt_rec *)pack;
224         const struct dt_key *key = (const struct dt_key *)name;
225         int result;
226
227         if (dt_try_as_dir(env, dir)) {
228                 result = dir->do_index_ops->dio_lookup(env, dir, rec, key,
229                                                        BYPASS_CAPA);
230                 if (result > 0)
231                         result = fid_unpack(pack, fid);
232                 else if (result == 0)
233                         result = -ENOENT;
234         } else
235                 result = -ENOTDIR;
236         return result;
237 }
238
239 /**
240  * get object for given \a fid.
241  */
242 struct dt_object *dt_locate(const struct lu_env *env,
243                             struct dt_device *dev,
244                             const struct lu_fid *fid)
245 {
246         struct lu_object *obj;
247         struct dt_object *dt;
248
249         obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL);
250         if (!IS_ERR(obj)) {
251                 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
252                 LASSERT(obj != NULL);
253                 dt = container_of(obj, struct dt_object, do_lu);
254         } else
255                 dt = (struct dt_object *)obj;
256         return dt;
257 }
258 EXPORT_SYMBOL(dt_locate);
259
260 /**
261  * find a object named \a entry in given \a dfh->dfh_o directory.
262  */
263 static int dt_find_entry(const struct lu_env *env, const char *entry, void *data)
264 {
265         struct dt_find_hint  *dfh = data;
266         struct dt_device     *dt = dfh->dfh_dt;
267         struct lu_fid        *fid = dfh->dfh_fid;
268         struct dt_object     *obj = dfh->dfh_o;
269         int                   result;
270
271         result = dt_lookup(env, obj, entry, fid);
272         lu_object_put(env, &obj->do_lu);
273         if (result == 0) {
274                 obj = dt_locate(env, dt, fid);
275                 if (IS_ERR(obj))
276                         result = PTR_ERR(obj);
277         }
278         dfh->dfh_o = obj;
279         return result;
280 }
281
282 /**
283  * Abstract function which parses path name. This function feeds
284  * path component to \a entry_func.
285  */
286 int dt_path_parser(const struct lu_env *env,
287                    char *path, dt_entry_func_t entry_func,
288                    void *data)
289 {
290         char *e;
291         int rc = 0;
292
293         while (1) {
294                 e = strsep(&path, "/");
295                 if (e == NULL)
296                         break;
297
298                 if (e[0] == 0) {
299                         if (!path || path[0] == '\0')
300                                 break;
301                         continue;
302                 }
303                 rc = entry_func(env, e, data);
304                 if (rc)
305                         break;
306         }
307
308         return rc;
309 }
310
311 static struct dt_object *dt_store_resolve(const struct lu_env *env,
312                                           struct dt_device *dt,
313                                           const char *path,
314                                           struct lu_fid *fid)
315 {
316         struct dt_thread_info *info = lu_context_key_get(&env->le_ctx,
317                                                          &dt_key);
318         struct dt_find_hint *dfh = &info->dti_dfh;
319         struct dt_object     *obj;
320         char *local = info->dti_buf;
321         int result;
322
323         dfh->dfh_dt = dt;
324         dfh->dfh_fid = fid;
325
326         strncpy(local, path, DT_MAX_PATH);
327         local[DT_MAX_PATH - 1] = '\0';
328
329         result = dt->dd_ops->dt_root_get(env, dt, fid);
330         if (result == 0) {
331                 obj = dt_locate(env, dt, fid);
332                 if (!IS_ERR(obj)) {
333                         dfh->dfh_o = obj;
334                         result = dt_path_parser(env, local, dt_find_entry, dfh);
335                         if (result != 0)
336                                 obj = ERR_PTR(result);
337                         else
338                                 obj = dfh->dfh_o;
339                 }
340         } else {
341                 obj = ERR_PTR(result);
342         }
343         return obj;
344 }
345
346 static struct dt_object *dt_reg_open(const struct lu_env *env,
347                                      struct dt_device *dt,
348                                      struct dt_object *p,
349                                      const char *name,
350                                      struct lu_fid *fid)
351 {
352         struct dt_object *o;
353         int result;
354
355         result = dt_lookup(env, p, name, fid);
356         if (result == 0){
357                 o = dt_locate(env, dt, fid);
358         }
359         else
360                 o = ERR_PTR(result);
361
362         return o;
363 }
364
365 /**
366  * Open dt object named \a filename from \a dirname directory.
367  *      \param  dt      dt device
368  *      \param  fid     on success, object fid is stored in *fid
369  */
370 struct dt_object *dt_store_open(const struct lu_env *env,
371                                 struct dt_device *dt,
372                                 const char *dirname,
373                                 const char *filename,
374                                 struct lu_fid *fid)
375 {
376         struct dt_object *file;
377         struct dt_object *dir;
378
379         dir = dt_store_resolve(env, dt, dirname, fid);
380         if (!IS_ERR(dir)) {
381                 file = dt_reg_open(env, dt, dir,
382                                    filename, fid);
383                 lu_object_put(env, &dir->do_lu);
384         } else {
385                 file = dir;
386         }
387         return file;
388 }
389 EXPORT_SYMBOL(dt_store_open);
390
391 /* dt class init function. */
392 int dt_global_init(void)
393 {
394         int result;
395
396         LU_CONTEXT_KEY_INIT(&dt_key);
397         result = lu_context_key_register(&dt_key);
398         return result;
399 }
400
401 void dt_global_fini(void)
402 {
403         lu_context_key_degister(&dt_key);
404 }
405
406 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
407                    struct lu_buf *buf, loff_t *pos)
408 {
409         int rc;
410
411         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
412
413         rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
414
415         if (rc == buf->lb_len)
416                 rc = 0;
417         else if (rc >= 0)
418                 rc = -EFAULT;
419         return rc;
420 }
421 EXPORT_SYMBOL(dt_record_read);
422
423 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
424                     const struct lu_buf *buf, loff_t *pos, struct thandle *th)
425 {
426         int rc;
427
428         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
429         LASSERT(th != NULL);
430         rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
431         if (rc == buf->lb_len)
432                 rc = 0;
433         else if (rc >= 0)
434                 rc = -EFAULT;
435         return rc;
436 }
437 EXPORT_SYMBOL(dt_record_write);
438
439 const struct dt_index_features dt_directory_features;
440 EXPORT_SYMBOL(dt_directory_features);