Whamcloud - gitweb
179c32357954ec9c53f3ca226705c48bd1f525b6
[fs/lustre-release.git] / lustre / obdclass / local_storage.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2013, Intel Corporation.
24  */
25 /*
26  * lustre/obdclass/local_storage.c
27  *
28  * Local storage for file/objects with fid generation. Works on top of OSD.
29  *
30  * Author: Mikhail Pershin <mike.pershin@intel.com>
31  */
32
33 #define DEBUG_SUBSYSTEM S_CLASS
34
35 #include "local_storage.h"
36
37 /* all initialized local storages on this node are linked on this */
38 static CFS_LIST_HEAD(ls_list_head);
39 static DEFINE_MUTEX(ls_list_mutex);
40
41 static int ls_object_init(const struct lu_env *env, struct lu_object *o,
42                           const struct lu_object_conf *unused)
43 {
44         struct ls_device        *ls;
45         struct lu_object        *below;
46         struct lu_device        *under;
47
48         ENTRY;
49
50         ls = container_of0(o->lo_dev, struct ls_device, ls_top_dev.dd_lu_dev);
51         under = &ls->ls_osd->dd_lu_dev;
52         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
53         if (below == NULL)
54                 RETURN(-ENOMEM);
55
56         lu_object_add(o, below);
57
58         RETURN(0);
59 }
60
61 static void ls_object_free(const struct lu_env *env, struct lu_object *o)
62 {
63         struct ls_object        *obj = lu2ls_obj(o);
64         struct lu_object_header *h = o->lo_header;
65
66         dt_object_fini(&obj->ls_obj);
67         lu_object_header_fini(h);
68         OBD_FREE_PTR(obj);
69 }
70
71 struct lu_object_operations ls_lu_obj_ops = {
72         .loo_object_init  = ls_object_init,
73         .loo_object_free  = ls_object_free,
74 };
75
76 struct lu_object *ls_object_alloc(const struct lu_env *env,
77                                   const struct lu_object_header *_h,
78                                   struct lu_device *d)
79 {
80         struct lu_object_header *h;
81         struct ls_object        *o;
82         struct lu_object        *l;
83
84         LASSERT(_h == NULL);
85
86         OBD_ALLOC_PTR(o);
87         if (o != NULL) {
88                 l = &o->ls_obj.do_lu;
89                 h = &o->ls_header;
90
91                 lu_object_header_init(h);
92                 dt_object_init(&o->ls_obj, h, d);
93                 lu_object_add_top(h, l);
94
95                 l->lo_ops = &ls_lu_obj_ops;
96
97                 return l;
98         } else {
99                 return NULL;
100         }
101 }
102
103 static struct lu_device_operations ls_lu_dev_ops = {
104         .ldo_object_alloc =     ls_object_alloc
105 };
106
107 static struct ls_device *__ls_find_dev(struct dt_device *dev)
108 {
109         struct ls_device *ls, *ret = NULL;
110
111         cfs_list_for_each_entry(ls, &ls_list_head, ls_linkage) {
112                 if (ls->ls_osd == dev) {
113                         cfs_atomic_inc(&ls->ls_refcount);
114                         ret = ls;
115                         break;
116                 }
117         }
118         return ret;
119 }
120
121 struct ls_device *ls_find_dev(struct dt_device *dev)
122 {
123         struct ls_device *ls;
124
125         mutex_lock(&ls_list_mutex);
126         ls = __ls_find_dev(dev);
127         mutex_unlock(&ls_list_mutex);
128
129         return ls;
130 }
131
132 static struct lu_device_type_operations ls_device_type_ops = {
133         .ldto_start = NULL,
134         .ldto_stop  = NULL,
135 };
136
137 static struct lu_device_type ls_lu_type = {
138         .ldt_name = "local_storage",
139         .ldt_ops  = &ls_device_type_ops,
140 };
141
142 struct ls_device *ls_device_get(struct dt_device *dev)
143 {
144         struct ls_device *ls;
145
146         ENTRY;
147
148         mutex_lock(&ls_list_mutex);
149         ls = __ls_find_dev(dev);
150         if (ls)
151                 GOTO(out_ls, ls);
152
153         /* not found, then create */
154         OBD_ALLOC_PTR(ls);
155         if (ls == NULL)
156                 GOTO(out_ls, ls = ERR_PTR(-ENOMEM));
157
158         cfs_atomic_set(&ls->ls_refcount, 1);
159         CFS_INIT_LIST_HEAD(&ls->ls_los_list);
160         mutex_init(&ls->ls_los_mutex);
161
162         ls->ls_osd = dev;
163
164         LASSERT(dev->dd_lu_dev.ld_site);
165         lu_device_init(&ls->ls_top_dev.dd_lu_dev, &ls_lu_type);
166         ls->ls_top_dev.dd_lu_dev.ld_ops = &ls_lu_dev_ops;
167         ls->ls_top_dev.dd_lu_dev.ld_site = dev->dd_lu_dev.ld_site;
168
169         /* finally add ls to the list */
170         cfs_list_add(&ls->ls_linkage, &ls_list_head);
171 out_ls:
172         mutex_unlock(&ls_list_mutex);
173         RETURN(ls);
174 }
175
176 void ls_device_put(const struct lu_env *env, struct ls_device *ls)
177 {
178         LASSERT(env);
179         if (!cfs_atomic_dec_and_test(&ls->ls_refcount))
180                 return;
181
182         mutex_lock(&ls_list_mutex);
183         if (cfs_atomic_read(&ls->ls_refcount) == 0) {
184                 LASSERT(cfs_list_empty(&ls->ls_los_list));
185                 cfs_list_del(&ls->ls_linkage);
186                 lu_site_purge(env, ls->ls_top_dev.dd_lu_dev.ld_site, ~0);
187                 lu_device_fini(&ls->ls_top_dev.dd_lu_dev);
188                 OBD_FREE_PTR(ls);
189         }
190         mutex_unlock(&ls_list_mutex);
191 }
192
193 /**
194  * local file fid generation
195  */
196 int local_object_fid_generate(const struct lu_env *env,
197                               struct local_oid_storage *los,
198                               struct lu_fid *fid)
199 {
200         LASSERT(los->los_dev);
201         LASSERT(los->los_obj);
202
203         /* take next OID */
204
205         /* to make it unique after reboot we store
206          * the latest generated fid atomically with
207          * object creation see local_object_create() */
208
209         mutex_lock(&los->los_id_lock);
210         fid->f_seq = los->los_seq;
211         fid->f_oid = los->los_last_oid++;
212         fid->f_ver = 0;
213         mutex_unlock(&los->los_id_lock);
214
215         return 0;
216 }
217
218 int local_object_declare_create(const struct lu_env *env,
219                                 struct local_oid_storage *los,
220                                 struct dt_object *o, struct lu_attr *attr,
221                                 struct dt_object_format *dof,
222                                 struct thandle *th)
223 {
224         struct dt_thread_info   *dti = dt_info(env);
225         int                      rc;
226
227         ENTRY;
228
229         /* update fid generation file */
230         if (los != NULL) {
231                 LASSERT(dt_object_exists(los->los_obj));
232                 rc = dt_declare_record_write(env, los->los_obj,
233                                              sizeof(struct los_ondisk), 0, th);
234                 if (rc)
235                         RETURN(rc);
236         }
237
238         rc = dt_declare_create(env, o, attr, NULL, dof, th);
239         if (rc)
240                 RETURN(rc);
241
242         dti->dti_lb.lb_buf = NULL;
243         dti->dti_lb.lb_len = sizeof(dti->dti_lma);
244         rc = dt_declare_xattr_set(env, o, &dti->dti_lb, XATTR_NAME_LMA, 0, th);
245
246         RETURN(rc);
247 }
248
249 int local_object_create(const struct lu_env *env,
250                         struct local_oid_storage *los,
251                         struct dt_object *o, struct lu_attr *attr,
252                         struct dt_object_format *dof, struct thandle *th)
253 {
254         struct dt_thread_info   *dti = dt_info(env);
255         struct los_ondisk        losd;
256         int                      rc;
257
258         ENTRY;
259
260         rc = dt_create(env, o, attr, NULL, dof, th);
261         if (rc)
262                 RETURN(rc);
263
264         if (los == NULL)
265                 RETURN(rc);
266
267         LASSERT(los->los_obj);
268         LASSERT(dt_object_exists(los->los_obj));
269
270         /* many threads can be updated this, serialize
271          * them here to avoid the race where one thread
272          * takes the value first, but writes it last */
273         mutex_lock(&los->los_id_lock);
274
275         /* update local oid number on disk so that
276          * we know the last one used after reboot */
277         losd.lso_magic = cpu_to_le32(LOS_MAGIC);
278         losd.lso_next_oid = cpu_to_le32(los->los_last_oid);
279
280         dti->dti_off = 0;
281         dti->dti_lb.lb_buf = &losd;
282         dti->dti_lb.lb_len = sizeof(losd);
283         rc = dt_record_write(env, los->los_obj, &dti->dti_lb, &dti->dti_off,
284                              th);
285         mutex_unlock(&los->los_id_lock);
286
287         RETURN(rc);
288 }
289
290 /*
291  * Create local named object (file, directory or index) in parent directory.
292  */
293 struct dt_object *__local_file_create(const struct lu_env *env,
294                                       const struct lu_fid *fid,
295                                       struct local_oid_storage *los,
296                                       struct ls_device *ls,
297                                       struct dt_object *parent,
298                                       const char *name, struct lu_attr *attr,
299                                       struct dt_object_format *dof)
300 {
301         struct dt_thread_info   *dti = dt_info(env);
302         struct dt_object        *dto;
303         struct thandle          *th;
304         int                      rc;
305
306         dto = ls_locate(env, ls, fid);
307         if (unlikely(IS_ERR(dto)))
308                 RETURN(dto);
309
310         LASSERT(dto != NULL);
311         if (dt_object_exists(dto))
312                 GOTO(out, rc = -EEXIST);
313
314         th = dt_trans_create(env, ls->ls_osd);
315         if (IS_ERR(th))
316                 GOTO(out, rc = PTR_ERR(th));
317
318         rc = local_object_declare_create(env, los, dto, attr, dof, th);
319         if (rc)
320                 GOTO(trans_stop, rc);
321
322         if (dti->dti_dof.dof_type == DFT_DIR) {
323                 dt_declare_ref_add(env, dto, th);
324                 dt_declare_ref_add(env, parent, th);
325         }
326
327         rc = dt_declare_insert(env, parent, (void *)fid, (void *)name, th);
328         if (rc)
329                 GOTO(trans_stop, rc);
330
331         rc = dt_trans_start_local(env, ls->ls_osd, th);
332         if (rc)
333                 GOTO(trans_stop, rc);
334
335         dt_write_lock(env, dto, 0);
336         if (dt_object_exists(dto))
337                 GOTO(unlock, rc = 0);
338
339         CDEBUG(D_OTHER, "create new object "DFID"\n",
340                PFID(lu_object_fid(&dto->do_lu)));
341         rc = local_object_create(env, los, dto, attr, dof, th);
342         if (rc)
343                 GOTO(unlock, rc);
344         LASSERT(dt_object_exists(dto));
345
346         if (dti->dti_dof.dof_type == DFT_DIR) {
347                 if (!dt_try_as_dir(env, dto))
348                         GOTO(destroy, rc = -ENOTDIR);
349                 /* Add "." and ".." for newly created dir */
350                 rc = dt_insert(env, dto, (void *)fid, (void *)".", th,
351                                BYPASS_CAPA, 1);
352                 if (rc)
353                         GOTO(destroy, rc);
354                 dt_ref_add(env, dto, th);
355                 rc = dt_insert(env, dto, (void *)lu_object_fid(&parent->do_lu),
356                                (void *)"..", th, BYPASS_CAPA, 1);
357                 if (rc)
358                         GOTO(destroy, rc);
359         }
360
361         dt_write_lock(env, parent, 0);
362         rc = dt_insert(env, parent, (const struct dt_rec *)fid,
363                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
364         if (dti->dti_dof.dof_type == DFT_DIR)
365                 dt_ref_add(env, parent, th);
366         dt_write_unlock(env, parent);
367         if (rc)
368                 GOTO(destroy, rc);
369 destroy:
370         if (rc)
371                 dt_destroy(env, dto, th);
372 unlock:
373         dt_write_unlock(env, dto);
374 trans_stop:
375         dt_trans_stop(env, ls->ls_osd, th);
376 out:
377         if (rc) {
378                 lu_object_put_nocache(env, &dto->do_lu);
379                 dto = ERR_PTR(rc);
380         }
381         RETURN(dto);
382 }
383
384 /*
385  * Look up and create (if it does not exist) a local named file or directory in
386  * parent directory.
387  */
388 struct dt_object *local_file_find_or_create(const struct lu_env *env,
389                                             struct local_oid_storage *los,
390                                             struct dt_object *parent,
391                                             const char *name, __u32 mode)
392 {
393         struct dt_thread_info   *dti = dt_info(env);
394         struct dt_object        *dto;
395         int                      rc;
396
397         LASSERT(parent);
398
399         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
400         if (rc == 0)
401                 /* name is found, get the object */
402                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
403         else if (rc != -ENOENT)
404                 dto = ERR_PTR(rc);
405         else {
406                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
407                 if (rc < 0) {
408                         dto = ERR_PTR(rc);
409                 } else {
410                         /* create the object */
411                         dti->dti_attr.la_valid  = LA_MODE;
412                         dti->dti_attr.la_mode   = mode;
413                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
414                         dto = __local_file_create(env, &dti->dti_fid, los,
415                                                   dt2ls_dev(los->los_dev),
416                                                   parent, name, &dti->dti_attr,
417                                                   &dti->dti_dof);
418                 }
419         }
420         return dto;
421 }
422 EXPORT_SYMBOL(local_file_find_or_create);
423
424 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
425                                                      struct dt_device *dt,
426                                                      const struct lu_fid *fid,
427                                                      struct dt_object *parent,
428                                                      const char *name,
429                                                      __u32 mode)
430 {
431         struct dt_thread_info   *dti = dt_info(env);
432         struct dt_object        *dto;
433         int                      rc;
434
435         LASSERT(parent);
436
437         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
438         if (rc == 0) {
439                 dto = dt_locate(env, dt, &dti->dti_fid);
440         } else if (rc != -ENOENT) {
441                 dto = ERR_PTR(rc);
442         } else {
443                 struct ls_device *ls;
444
445                 ls = ls_device_get(dt);
446                 if (IS_ERR(ls)) {
447                         dto = ERR_PTR(PTR_ERR(ls));
448                 } else {
449                         /* create the object */
450                         dti->dti_attr.la_valid  = LA_MODE;
451                         dti->dti_attr.la_mode   = mode;
452                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
453                         dto = __local_file_create(env, fid, NULL, ls, parent,
454                                                   name, &dti->dti_attr,
455                                                   &dti->dti_dof);
456                         /* ls_device_put() will finalize the ls device, we
457                          * have to open the object in other device stack */
458                         if (!IS_ERR(dto)) {
459                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
460                                 lu_object_put_nocache(env, &dto->do_lu);
461                                 dto = dt_locate(env, dt, &dti->dti_fid);
462                         }
463                         ls_device_put(env, ls);
464                 }
465         }
466         return dto;
467 }
468 EXPORT_SYMBOL(local_file_find_or_create_with_fid);
469
470 /*
471  * Look up and create (if it does not exist) a local named index file in parent
472  * directory.
473  */
474 struct dt_object *local_index_find_or_create(const struct lu_env *env,
475                                              struct local_oid_storage *los,
476                                              struct dt_object *parent,
477                                              const char *name, __u32 mode,
478                                              const struct dt_index_features *ft)
479 {
480         struct dt_thread_info   *dti = dt_info(env);
481         struct dt_object        *dto;
482         int                      rc;
483
484         LASSERT(parent);
485
486         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
487         if (rc == 0) {
488                 /* name is found, get the object */
489                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
490         } else if (rc != -ENOENT) {
491                 dto = ERR_PTR(rc);
492         } else {
493                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
494                 if (rc < 0) {
495                         dto = ERR_PTR(rc);
496                 } else {
497                         /* create the object */
498                         dti->dti_attr.la_valid          = LA_MODE;
499                         dti->dti_attr.la_mode           = mode;
500                         dti->dti_dof.dof_type           = DFT_INDEX;
501                         dti->dti_dof.u.dof_idx.di_feat  = ft;
502                         dto = __local_file_create(env, &dti->dti_fid, los,
503                                                   dt2ls_dev(los->los_dev),
504                                                   parent, name, &dti->dti_attr,
505                                                   &dti->dti_dof);
506                 }
507         }
508         return dto;
509
510 }
511 EXPORT_SYMBOL(local_index_find_or_create);
512
513 struct dt_object *
514 local_index_find_or_create_with_fid(const struct lu_env *env,
515                                     struct dt_device *dt,
516                                     const struct lu_fid *fid,
517                                     struct dt_object *parent,
518                                     const char *name, __u32 mode,
519                                     const struct dt_index_features *ft)
520 {
521         struct dt_thread_info   *dti = dt_info(env);
522         struct dt_object        *dto;
523         int                      rc;
524
525         LASSERT(parent);
526
527         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
528         if (rc == 0) {
529                 /* name is found, get the object */
530                 if (!lu_fid_eq(fid, &dti->dti_fid))
531                         dto = ERR_PTR(-EINVAL);
532                 else
533                         dto = dt_locate(env, dt, fid);
534         } else if (rc != -ENOENT) {
535                 dto = ERR_PTR(rc);
536         } else {
537                 struct ls_device *ls;
538
539                 ls = ls_device_get(dt);
540                 if (IS_ERR(ls)) {
541                         dto = ERR_PTR(PTR_ERR(ls));
542                 } else {
543                         /* create the object */
544                         dti->dti_attr.la_valid          = LA_MODE;
545                         dti->dti_attr.la_mode           = mode;
546                         dti->dti_dof.dof_type           = DFT_INDEX;
547                         dti->dti_dof.u.dof_idx.di_feat  = ft;
548                         dto = __local_file_create(env, fid, NULL, ls, parent,
549                                                   name, &dti->dti_attr,
550                                                   &dti->dti_dof);
551                         /* ls_device_put() will finalize the ls device, we
552                          * have to open the object in other device stack */
553                         if (!IS_ERR(dto)) {
554                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
555                                 lu_object_put_nocache(env, &dto->do_lu);
556                                 dto = dt_locate(env, dt, &dti->dti_fid);
557                         }
558                         ls_device_put(env, ls);
559                 }
560         }
561         return dto;
562 }
563 EXPORT_SYMBOL(local_index_find_or_create_with_fid);
564
565 static int local_object_declare_unlink(const struct lu_env *env,
566                                        struct dt_device *dt,
567                                        struct dt_object *p,
568                                        struct dt_object *c, const char *name,
569                                        struct thandle *th)
570 {
571         int rc;
572
573         rc = dt_declare_delete(env, p, (const struct dt_key *)name, th);
574         if (rc < 0)
575                 return rc;
576
577         rc = dt_declare_ref_del(env, c, th);
578         if (rc < 0)
579                 return rc;
580
581         return dt_declare_destroy(env, c, th);
582 }
583
584 int local_object_unlink(const struct lu_env *env, struct dt_device *dt,
585                         struct dt_object *parent, const char *name)
586 {
587         struct dt_thread_info   *dti = dt_info(env);
588         struct dt_object        *dto;
589         struct thandle          *th;
590         int                      rc;
591
592         ENTRY;
593
594         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
595         if (rc == -ENOENT)
596                 RETURN(0);
597         else if (rc < 0)
598                 RETURN(rc);
599
600         dto = dt_locate(env, dt, &dti->dti_fid);
601         if (unlikely(IS_ERR(dto)))
602                 RETURN(PTR_ERR(dto));
603
604         th = dt_trans_create(env, dt);
605         if (IS_ERR(th))
606                 GOTO(out, rc = PTR_ERR(th));
607
608         rc = local_object_declare_unlink(env, dt, parent, dto, name, th);
609         if (rc < 0)
610                 GOTO(stop, rc);
611
612         rc = dt_trans_start_local(env, dt, th);
613         if (rc < 0)
614                 GOTO(stop, rc);
615
616         dt_write_lock(env, dto, 0);
617         rc = dt_delete(env, parent, (struct dt_key *)name, th, BYPASS_CAPA);
618         if (rc < 0)
619                 GOTO(unlock, rc);
620
621         rc = dt_ref_del(env, dto, th);
622         if (rc < 0) {
623                 rc = dt_insert(env, parent,
624                                (const struct dt_rec *)&dti->dti_fid,
625                                (const struct dt_key *)name, th, BYPASS_CAPA, 1);
626                 GOTO(unlock, rc);
627         }
628
629         rc = dt_destroy(env, dto, th);
630 unlock:
631         dt_write_unlock(env, dto);
632 stop:
633         dt_trans_stop(env, dt, th);
634 out:
635         lu_object_put_nocache(env, &dto->do_lu);
636         return rc;
637 }
638 EXPORT_SYMBOL(local_object_unlink);
639
640 struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
641 {
642         struct local_oid_storage *los, *ret = NULL;
643
644         cfs_list_for_each_entry(los, &ls->ls_los_list, los_list) {
645                 if (los->los_seq == seq) {
646                         cfs_atomic_inc(&los->los_refcount);
647                         ret = los;
648                         break;
649                 }
650         }
651         return ret;
652 }
653
654 void dt_los_put(struct local_oid_storage *los)
655 {
656         if (cfs_atomic_dec_and_test(&los->los_refcount))
657                 /* should never happen, only local_oid_storage_fini should
658                  * drop refcount to zero */
659                 LBUG();
660         return;
661 }
662
663 /**
664  * Initialize local OID storage for required sequence.
665  * That may be needed for services that uses local files and requires
666  * dynamic OID allocation for them.
667  *
668  * Per each sequence we have an object with 'first_fid' identificator
669  * containing the counter for OIDs of locally created files with that
670  * sequence.
671  *
672  * It is used now by llog subsystem and MGS for NID tables
673  *
674  * Function gets first_fid to create counter object.
675  * All dynamic fids will be generated with the same sequence and incremented
676  * OIDs
677  *
678  * Returned local_oid_storage is in-memory representaion of OID storage
679  */
680 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
681                            const struct lu_fid *first_fid,
682                            struct local_oid_storage **los)
683 {
684         struct dt_thread_info   *dti = dt_info(env);
685         struct ls_device        *ls;
686         struct los_ondisk        losd;
687         struct dt_object        *root = NULL;
688         struct dt_object        *o = NULL;
689         struct thandle          *th;
690         int                      rc;
691
692         ENTRY;
693
694         ls = ls_device_get(dev);
695         if (IS_ERR(ls))
696                 RETURN(PTR_ERR(ls));
697
698         mutex_lock(&ls->ls_los_mutex);
699         *los = dt_los_find(ls, fid_seq(first_fid));
700         if (*los != NULL)
701                 GOTO(out, rc = 0);
702
703         /* not found, then create */
704         OBD_ALLOC_PTR(*los);
705         if (*los == NULL)
706                 GOTO(out, rc = -ENOMEM);
707
708         cfs_atomic_set(&(*los)->los_refcount, 1);
709         mutex_init(&(*los)->los_id_lock);
710         (*los)->los_dev = &ls->ls_top_dev;
711         cfs_atomic_inc(&ls->ls_refcount);
712         cfs_list_add(&(*los)->los_list, &ls->ls_los_list);
713
714         rc = dt_root_get(env, dev, &dti->dti_fid);
715         if (rc)
716                 GOTO(out_los, rc);
717
718         root = ls_locate(env, ls, &dti->dti_fid);
719         if (IS_ERR(root))
720                 GOTO(out_los, rc = PTR_ERR(root));
721
722         /* initialize data allowing to generate new fids,
723          * literally we need a sequence */
724         snprintf(dti->dti_buf, sizeof(dti->dti_buf), "seq-%Lx-lastid",
725                  fid_seq(first_fid));
726         rc = dt_lookup_dir(env, root, dti->dti_buf, &dti->dti_fid);
727         if (rc == -ENOENT)
728                 dti->dti_fid = *first_fid;
729         else if (rc < 0)
730                 GOTO(out_los, rc);
731
732         o = ls_locate(env, ls, &dti->dti_fid);
733         if (IS_ERR(o))
734                 GOTO(out_los, rc = PTR_ERR(o));
735         LASSERT(fid_seq(&dti->dti_fid) == fid_seq(first_fid));
736         if (!dt_object_exists(o)) {
737                 LASSERT(rc == -ENOENT);
738
739                 th = dt_trans_create(env, dev);
740                 if (IS_ERR(th))
741                         GOTO(out_los, rc = PTR_ERR(th));
742
743                 dti->dti_attr.la_valid = LA_MODE | LA_TYPE;
744                 dti->dti_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
745                 dti->dti_dof.dof_type = dt_mode_to_dft(S_IFREG);
746
747                 rc = dt_declare_create(env, o, &dti->dti_attr, NULL,
748                                        &dti->dti_dof, th);
749                 if (rc)
750                         GOTO(out_trans, rc);
751
752                 rc = dt_declare_insert(env, root,
753                                        (const struct dt_rec *)&dti->dti_fid,
754                                        (const struct dt_key *)dti->dti_buf,
755                                        th);
756                 if (rc)
757                         GOTO(out_trans, rc);
758
759                 rc = dt_declare_record_write(env, o, sizeof(losd), 0, th);
760                 if (rc)
761                         GOTO(out_trans, rc);
762
763                 rc = dt_trans_start_local(env, dev, th);
764                 if (rc)
765                         GOTO(out_trans, rc);
766
767                 dt_write_lock(env, root, 0);
768                 dt_write_lock(env, o, 0);
769                 if (dt_object_exists(o))
770                         GOTO(out_lock, rc = 0);
771
772                 rc = dt_create(env, o, &dti->dti_attr, NULL, &dti->dti_dof,
773                                th);
774                 if (rc)
775                         GOTO(out_lock, rc);
776
777                 losd.lso_magic = cpu_to_le32(LOS_MAGIC);
778                 losd.lso_next_oid = cpu_to_le32(fid_oid(first_fid) + 1);
779
780                 dti->dti_off = 0;
781                 dti->dti_lb.lb_buf = &losd;
782                 dti->dti_lb.lb_len = sizeof(losd);
783                 rc = dt_record_write(env, o, &dti->dti_lb, &dti->dti_off, th);
784                 if (rc)
785                         GOTO(out_lock, rc);
786                 rc = dt_insert(env, root,
787                                (const struct dt_rec *)&dti->dti_fid,
788                                (const struct dt_key *)dti->dti_buf,
789                                th, BYPASS_CAPA, 1);
790                 if (rc)
791                         GOTO(out_lock, rc);
792 out_lock:
793                 dt_write_unlock(env, o);
794                 dt_write_unlock(env, root);
795 out_trans:
796                 dt_trans_stop(env, dev, th);
797         } else {
798                 dti->dti_off = 0;
799                 dti->dti_lb.lb_buf = &losd;
800                 dti->dti_lb.lb_len = sizeof(losd);
801                 dt_read_lock(env, o, 0);
802                 rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
803                 dt_read_unlock(env, o);
804                 if (rc == 0 && le32_to_cpu(losd.lso_magic) != LOS_MAGIC) {
805                         CERROR("local storage file "DFID" is corrupted\n",
806                                PFID(first_fid));
807                         rc = -EINVAL;
808                 }
809         }
810 out_los:
811         if (root != NULL && !IS_ERR(root))
812                 lu_object_put_nocache(env, &root->do_lu);
813
814         if (rc != 0) {
815                 cfs_list_del(&(*los)->los_list);
816                 cfs_atomic_dec(&ls->ls_refcount);
817                 OBD_FREE_PTR(*los);
818                 *los = NULL;
819                 if (o != NULL && !IS_ERR(o))
820                         lu_object_put_nocache(env, &o->do_lu);
821         } else {
822                 (*los)->los_seq = fid_seq(first_fid);
823                 (*los)->los_last_oid = le32_to_cpu(losd.lso_next_oid);
824                 (*los)->los_obj = o;
825         }
826 out:
827         mutex_unlock(&ls->ls_los_mutex);
828         ls_device_put(env, ls);
829         return rc;
830 }
831 EXPORT_SYMBOL(local_oid_storage_init);
832
833 void local_oid_storage_fini(const struct lu_env *env,
834                             struct local_oid_storage *los)
835 {
836         struct ls_device *ls;
837
838         if (!cfs_atomic_dec_and_test(&los->los_refcount))
839                 return;
840
841         LASSERT(env);
842         LASSERT(los->los_dev);
843         ls = dt2ls_dev(los->los_dev);
844
845         mutex_lock(&ls->ls_los_mutex);
846         if (cfs_atomic_read(&los->los_refcount) == 0) {
847                 if (los->los_obj)
848                         lu_object_put_nocache(env, &los->los_obj->do_lu);
849                 cfs_list_del(&los->los_list);
850                 OBD_FREE_PTR(los);
851         }
852         mutex_unlock(&ls->ls_los_mutex);
853         ls_device_put(env, ls);
854 }
855 EXPORT_SYMBOL(local_oid_storage_fini);