Whamcloud - gitweb
1922804145ed5bb02fc7d40f5902cb960b3f3b66
[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 Whamcloud, Inc.
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 CFS_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         cfs_mutex_lock(&ls_list_mutex);
126         ls = __ls_find_dev(dev);
127         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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         lustre_lma_init(&dti->dti_lma, lu_object_fid(&o->do_lu));
265         lustre_lma_swab(&dti->dti_lma);
266         dti->dti_lb.lb_buf = &dti->dti_lma;
267         dti->dti_lb.lb_len = sizeof(dti->dti_lma);
268         rc = dt_xattr_set(env, o, &dti->dti_lb, XATTR_NAME_LMA, 0, th,
269                           BYPASS_CAPA);
270
271         if (los == NULL)
272                 RETURN(rc);
273
274         LASSERT(los->los_obj);
275         LASSERT(dt_object_exists(los->los_obj));
276
277         /* many threads can be updated this, serialize
278          * them here to avoid the race where one thread
279          * takes the value first, but writes it last */
280         cfs_mutex_lock(&los->los_id_lock);
281
282         /* update local oid number on disk so that
283          * we know the last one used after reboot */
284         losd.lso_magic = cpu_to_le32(LOS_MAGIC);
285         losd.lso_next_oid = cpu_to_le32(los->los_last_oid);
286
287         dti->dti_off = 0;
288         dti->dti_lb.lb_buf = &losd;
289         dti->dti_lb.lb_len = sizeof(losd);
290         rc = dt_record_write(env, los->los_obj, &dti->dti_lb, &dti->dti_off,
291                              th);
292         cfs_mutex_unlock(&los->los_id_lock);
293
294         RETURN(rc);
295 }
296
297 /*
298  * Create local named object (file, directory or index) in parent directory.
299  */
300 struct dt_object *__local_file_create(const struct lu_env *env,
301                                       const struct lu_fid *fid,
302                                       struct local_oid_storage *los,
303                                       struct ls_device *ls,
304                                       struct dt_object *parent,
305                                       const char *name, struct lu_attr *attr,
306                                       struct dt_object_format *dof)
307 {
308         struct dt_thread_info   *dti = dt_info(env);
309         struct dt_object        *dto;
310         struct thandle          *th;
311         int                      rc;
312
313         dto = ls_locate(env, ls, fid);
314         if (unlikely(IS_ERR(dto)))
315                 RETURN(dto);
316
317         LASSERT(dto != NULL);
318         if (dt_object_exists(dto))
319                 GOTO(out, rc = -EEXIST);
320
321         th = dt_trans_create(env, ls->ls_osd);
322         if (IS_ERR(th))
323                 GOTO(out, rc = PTR_ERR(th));
324
325         rc = local_object_declare_create(env, los, dto, attr, dof, th);
326         if (rc)
327                 GOTO(trans_stop, rc);
328
329         if (dti->dti_dof.dof_type == DFT_DIR) {
330                 dt_declare_ref_add(env, dto, th);
331                 dt_declare_ref_add(env, parent, th);
332         }
333
334         rc = dt_declare_insert(env, parent, (void *)fid, (void *)name, th);
335         if (rc)
336                 GOTO(trans_stop, rc);
337
338         rc = dt_trans_start_local(env, ls->ls_osd, th);
339         if (rc)
340                 GOTO(trans_stop, rc);
341
342         dt_write_lock(env, dto, 0);
343         if (dt_object_exists(dto))
344                 GOTO(unlock, rc = 0);
345
346         CDEBUG(D_OTHER, "create new object "DFID"\n",
347                PFID(lu_object_fid(&dto->do_lu)));
348         rc = local_object_create(env, los, dto, attr, dof, th);
349         if (rc)
350                 GOTO(unlock, rc);
351         LASSERT(dt_object_exists(dto));
352
353         if (dti->dti_dof.dof_type == DFT_DIR) {
354                 if (!dt_try_as_dir(env, dto))
355                         GOTO(destroy, rc = -ENOTDIR);
356                 /* Add "." and ".." for newly created dir */
357                 rc = dt_insert(env, dto, (void *)fid, (void *)".", th,
358                                BYPASS_CAPA, 1);
359                 if (rc)
360                         GOTO(destroy, rc);
361                 dt_ref_add(env, dto, th);
362                 rc = dt_insert(env, dto, (void *)lu_object_fid(&parent->do_lu),
363                                (void *)"..", th, BYPASS_CAPA, 1);
364                 if (rc)
365                         GOTO(destroy, rc);
366         }
367
368         dt_write_lock(env, parent, 0);
369         rc = dt_insert(env, parent, (const struct dt_rec *)fid,
370                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
371         if (dti->dti_dof.dof_type == DFT_DIR)
372                 dt_ref_add(env, parent, th);
373         dt_write_unlock(env, parent);
374         if (rc)
375                 GOTO(destroy, rc);
376 destroy:
377         if (rc)
378                 dt_destroy(env, dto, th);
379 unlock:
380         dt_write_unlock(env, dto);
381 trans_stop:
382         dt_trans_stop(env, ls->ls_osd, th);
383 out:
384         if (rc) {
385                 lu_object_put_nocache(env, &dto->do_lu);
386                 dto = ERR_PTR(rc);
387         } else {
388                 struct lu_fid dti_fid;
389                 /* since local files FIDs are not in OI the directory entry
390                  * is used to get inode number/generation, we need to do lookup
391                  * again to cache this data after create */
392                 rc = dt_lookup_dir(env, parent, name, &dti_fid);
393                 LASSERT(rc == 0);
394         }
395         RETURN(dto);
396 }
397
398 /*
399  * Look up and create (if it does not exist) a local named file or directory in
400  * parent directory.
401  */
402 struct dt_object *local_file_find_or_create(const struct lu_env *env,
403                                             struct local_oid_storage *los,
404                                             struct dt_object *parent,
405                                             const char *name, __u32 mode)
406 {
407         struct dt_thread_info   *dti = dt_info(env);
408         struct dt_object        *dto;
409         int                      rc;
410
411         LASSERT(parent);
412
413         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
414         if (rc == 0)
415                 /* name is found, get the object */
416                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
417         else if (rc != -ENOENT)
418                 dto = ERR_PTR(rc);
419         else {
420                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
421                 if (rc < 0) {
422                         dto = ERR_PTR(rc);
423                 } else {
424                         /* create the object */
425                         dti->dti_attr.la_valid  = LA_MODE;
426                         dti->dti_attr.la_mode   = mode;
427                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
428                         dto = __local_file_create(env, &dti->dti_fid, los,
429                                                   dt2ls_dev(los->los_dev),
430                                                   parent, name, &dti->dti_attr,
431                                                   &dti->dti_dof);
432                 }
433         }
434         return dto;
435 }
436 EXPORT_SYMBOL(local_file_find_or_create);
437
438 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
439                                                      struct dt_device *dt,
440                                                      const struct lu_fid *fid,
441                                                      struct dt_object *parent,
442                                                      const char *name,
443                                                      __u32 mode)
444 {
445         struct dt_thread_info   *dti = dt_info(env);
446         struct dt_object        *dto;
447         int                      rc;
448
449         LASSERT(parent);
450
451         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
452         if (rc == 0) {
453                 /* name is found, get the object */
454                 if (!lu_fid_eq(fid, &dti->dti_fid))
455                         dto = ERR_PTR(-EINVAL);
456                 else
457                         dto = dt_locate(env, dt, fid);
458         } else if (rc != -ENOENT) {
459                 dto = ERR_PTR(rc);
460         } else {
461                 struct ls_device *ls;
462
463                 ls = ls_device_get(dt);
464                 if (IS_ERR(ls)) {
465                         dto = ERR_PTR(PTR_ERR(ls));
466                 } else {
467                         /* create the object */
468                         dti->dti_attr.la_valid  = LA_MODE;
469                         dti->dti_attr.la_mode   = mode;
470                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
471                         dto = __local_file_create(env, fid, NULL, ls, parent,
472                                                   name, &dti->dti_attr,
473                                                   &dti->dti_dof);
474                         /* ls_device_put() will finalize the ls device, we
475                          * have to open the object in other device stack */
476                         if (!IS_ERR(dto)) {
477                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
478                                 lu_object_put_nocache(env, &dto->do_lu);
479                                 dto = dt_locate(env, dt, &dti->dti_fid);
480                         }
481                         ls_device_put(env, ls);
482                 }
483         }
484         return dto;
485 }
486 EXPORT_SYMBOL(local_file_find_or_create_with_fid);
487
488 /*
489  * Look up and create (if it does not exist) a local named index file in parent
490  * directory.
491  */
492 struct dt_object *local_index_find_or_create(const struct lu_env *env,
493                                              struct local_oid_storage *los,
494                                              struct dt_object *parent,
495                                              const char *name, __u32 mode,
496                                              const struct dt_index_features *ft)
497 {
498         struct dt_thread_info   *dti = dt_info(env);
499         struct dt_object        *dto;
500         int                      rc;
501
502         LASSERT(parent);
503
504         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
505         if (rc == 0) {
506                 /* name is found, get the object */
507                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
508         } else if (rc != -ENOENT) {
509                 dto = ERR_PTR(rc);
510         } else {
511                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
512                 if (rc < 0) {
513                         dto = ERR_PTR(rc);
514                 } else {
515                         /* create the object */
516                         dti->dti_attr.la_valid          = LA_MODE;
517                         dti->dti_attr.la_mode           = mode;
518                         dti->dti_dof.dof_type           = DFT_INDEX;
519                         dti->dti_dof.u.dof_idx.di_feat  = ft;
520                         dto = __local_file_create(env, &dti->dti_fid, los,
521                                                   dt2ls_dev(los->los_dev),
522                                                   parent, name, &dti->dti_attr,
523                                                   &dti->dti_dof);
524                 }
525         }
526         return dto;
527
528 }
529 EXPORT_SYMBOL(local_index_find_or_create);
530
531 struct dt_object *
532 local_index_find_or_create_with_fid(const struct lu_env *env,
533                                     struct dt_device *dt,
534                                     const struct lu_fid *fid,
535                                     struct dt_object *parent,
536                                     const char *name, __u32 mode,
537                                     const struct dt_index_features *ft)
538 {
539         struct dt_thread_info   *dti = dt_info(env);
540         struct dt_object        *dto;
541         int                      rc;
542
543         LASSERT(parent);
544
545         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
546         if (rc == 0) {
547                 /* name is found, get the object */
548                 if (!lu_fid_eq(fid, &dti->dti_fid))
549                         dto = ERR_PTR(-EINVAL);
550                 else
551                         dto = dt_locate(env, dt, fid);
552         } else if (rc != -ENOENT) {
553                 dto = ERR_PTR(rc);
554         } else {
555                 struct ls_device *ls;
556
557                 ls = ls_device_get(dt);
558                 if (IS_ERR(ls)) {
559                         dto = ERR_PTR(PTR_ERR(ls));
560                 } else {
561                         /* create the object */
562                         dti->dti_attr.la_valid          = LA_MODE;
563                         dti->dti_attr.la_mode           = mode;
564                         dti->dti_dof.dof_type           = DFT_INDEX;
565                         dti->dti_dof.u.dof_idx.di_feat  = ft;
566                         dto = __local_file_create(env, fid, NULL, ls, parent,
567                                                   name, &dti->dti_attr,
568                                                   &dti->dti_dof);
569                         /* ls_device_put() will finalize the ls device, we
570                          * have to open the object in other device stack */
571                         if (!IS_ERR(dto)) {
572                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
573                                 lu_object_put_nocache(env, &dto->do_lu);
574                                 dto = dt_locate(env, dt, &dti->dti_fid);
575                         }
576                         ls_device_put(env, ls);
577                 }
578         }
579         return dto;
580 }
581 EXPORT_SYMBOL(local_index_find_or_create_with_fid);
582
583 struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
584 {
585         struct local_oid_storage *los, *ret = NULL;
586
587         cfs_list_for_each_entry(los, &ls->ls_los_list, los_list) {
588                 if (los->los_seq == seq) {
589                         cfs_atomic_inc(&los->los_refcount);
590                         ret = los;
591                         break;
592                 }
593         }
594         return ret;
595 }
596
597 void dt_los_put(struct local_oid_storage *los)
598 {
599         if (cfs_atomic_dec_and_test(&los->los_refcount))
600                 /* should never happen, only local_oid_storage_fini should
601                  * drop refcount to zero */
602                 LBUG();
603         return;
604 }
605
606 /**
607  * Initialize local OID storage for required sequence.
608  * That may be needed for services that uses local files and requires
609  * dynamic OID allocation for them.
610  *
611  * Per each sequence we have an object with 'first_fid' identificator
612  * containing the counter for OIDs of locally created files with that
613  * sequence.
614  *
615  * It is used now by llog subsystem and MGS for NID tables
616  *
617  * Function gets first_fid to create counter object.
618  * All dynamic fids will be generated with the same sequence and incremented
619  * OIDs
620  *
621  * Returned local_oid_storage is in-memory representaion of OID storage
622  */
623 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
624                            const struct lu_fid *first_fid,
625                            struct local_oid_storage **los)
626 {
627         struct dt_thread_info   *dti = dt_info(env);
628         struct ls_device        *ls;
629         struct los_ondisk        losd;
630         struct dt_object        *o;
631         struct dt_object        *root = NULL;
632         struct thandle          *th;
633         int                      rc;
634
635         ENTRY;
636
637         ls = ls_device_get(dev);
638         if (IS_ERR(ls))
639                 RETURN(PTR_ERR(ls));
640
641         cfs_mutex_lock(&ls->ls_los_mutex);
642         *los = dt_los_find(ls, fid_seq(first_fid));
643         if (*los != NULL)
644                 GOTO(out, rc = 0);
645
646         /* not found, then create */
647         OBD_ALLOC_PTR(*los);
648         if (*los == NULL)
649                 GOTO(out, rc = -ENOMEM);
650
651         cfs_atomic_set(&(*los)->los_refcount, 1);
652         cfs_mutex_init(&(*los)->los_id_lock);
653         (*los)->los_dev = &ls->ls_top_dev;
654         cfs_atomic_inc(&ls->ls_refcount);
655         cfs_list_add(&(*los)->los_list, &ls->ls_los_list);
656
657         /* initialize data allowing to generate new fids,
658          * literally we need a sequence */
659         o = ls_locate(env, ls, first_fid);
660         if (IS_ERR(o))
661                 GOTO(out_los, rc = PTR_ERR(o));
662
663         rc = dt_root_get(env, dev, &dti->dti_fid);
664         if (rc)
665                 GOTO(out_los, rc);
666
667         root = ls_locate(env, ls, &dti->dti_fid);
668         if (IS_ERR(root))
669                 GOTO(out_los, rc = PTR_ERR(root));
670
671         if (dt_try_as_dir(env, root) == 0)
672                 GOTO(out_los, rc = -ENOTDIR);
673
674         dt_write_lock(env, o, 0);
675         if (!dt_object_exists(o)) {
676                 th = dt_trans_create(env, dev);
677                 if (IS_ERR(th))
678                         GOTO(out_lock, rc = PTR_ERR(th));
679
680                 dti->dti_attr.la_valid = LA_MODE | LA_TYPE;
681                 dti->dti_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
682                 dti->dti_dof.dof_type = dt_mode_to_dft(S_IFREG);
683
684                 rc = dt_declare_create(env, o, &dti->dti_attr, NULL,
685                                        &dti->dti_dof, th);
686                 if (rc)
687                         GOTO(out_trans, rc);
688
689                 snprintf(dti->dti_buf, sizeof(dti->dti_buf),
690                         "seq-%Lx-lastid", fid_seq(first_fid));
691                 rc = dt_declare_insert(env, root,
692                                        (const struct dt_rec *)lu_object_fid(&o->do_lu),
693                                        (const struct dt_key *)dti->dti_buf,
694                                        th);
695                 if (rc)
696                         GOTO(out_trans, rc);
697
698                 dti->dti_lb.lb_buf = NULL;
699                 dti->dti_lb.lb_len = sizeof(dti->dti_lma);
700                 rc = dt_declare_xattr_set(env, o, &dti->dti_lb, XATTR_NAME_LMA,
701                                           0, th);
702                 if (rc)
703                         GOTO(out_trans, rc);
704
705                 rc = dt_declare_record_write(env, o, sizeof(losd), 0, th);
706                 if (rc)
707                         GOTO(out_trans, rc);
708
709                 rc = dt_trans_start_local(env, dev, th);
710                 if (rc)
711                         GOTO(out_trans, rc);
712
713                 LASSERT(!dt_object_exists(o));
714                 rc = dt_create(env, o, &dti->dti_attr, NULL, &dti->dti_dof, th);
715                 if (rc)
716                         GOTO(out_trans, rc);
717                 LASSERT(dt_object_exists(o));
718
719                 lustre_lma_init(&dti->dti_lma, lu_object_fid(&o->do_lu));
720                 lustre_lma_swab(&dti->dti_lma);
721                 dti->dti_lb.lb_buf = &dti->dti_lma;
722                 dti->dti_lb.lb_len = sizeof(dti->dti_lma);
723                 rc = dt_xattr_set(env, o, &dti->dti_lb, XATTR_NAME_LMA, 0,
724                                   th, BYPASS_CAPA);
725                 if (rc)
726                         GOTO(out_trans, rc);
727
728                 losd.lso_magic = cpu_to_le32(LOS_MAGIC);
729                 losd.lso_next_oid = cpu_to_le32(fid_oid(first_fid) + 1);
730
731                 dti->dti_off = 0;
732                 dti->dti_lb.lb_buf = &losd;
733                 dti->dti_lb.lb_len = sizeof(losd);
734                 rc = dt_record_write(env, o, &dti->dti_lb, &dti->dti_off, th);
735                 if (rc)
736                         GOTO(out_trans, rc);
737 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 3, 90, 0)
738 #error "fix this before release"
739 #endif
740                 /*
741                  * there is one technical debt left in Orion:
742                  * proper hanlding of named vs no-name objects.
743                  * Llog objects have name always as they are placed in O/d/...
744                  */
745                 if (fid_seq(lu_object_fid(&o->do_lu)) != FID_SEQ_LLOG) {
746                         rc = dt_insert(env, root,
747                                        (const struct dt_rec *)first_fid,
748                                        (const struct dt_key *)dti->dti_buf,
749                                        th, BYPASS_CAPA, 1);
750                         if (rc)
751                                 GOTO(out_trans, rc);
752                 }
753 out_trans:
754                 dt_trans_stop(env, dev, th);
755         } else {
756                 dti->dti_off = 0;
757                 dti->dti_lb.lb_buf = &losd;
758                 dti->dti_lb.lb_len = sizeof(losd);
759                 rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
760                 if (rc == 0 && le32_to_cpu(losd.lso_magic) != LOS_MAGIC) {
761                         CERROR("local storage file "DFID" is corrupted\n",
762                                PFID(first_fid));
763                         rc = -EINVAL;
764                 }
765         }
766 out_lock:
767         dt_write_unlock(env, o);
768 out_los:
769         if (root)
770                 lu_object_put_nocache(env, &root->do_lu);
771         if (rc) {
772                 OBD_FREE_PTR(*los);
773                 *los = NULL;
774                 if (o)
775                         lu_object_put_nocache(env, &o->do_lu);
776         } else {
777                 (*los)->los_seq = fid_seq(first_fid);
778                 (*los)->los_last_oid = le32_to_cpu(losd.lso_next_oid);
779                 (*los)->los_obj = o;
780         }
781 out:
782         cfs_mutex_unlock(&ls->ls_los_mutex);
783         ls_device_put(env, ls);
784         return rc;
785 }
786 EXPORT_SYMBOL(local_oid_storage_init);
787
788 void local_oid_storage_fini(const struct lu_env *env,
789                             struct local_oid_storage *los)
790 {
791         struct ls_device *ls;
792
793         if (!cfs_atomic_dec_and_test(&los->los_refcount))
794                 return;
795
796         LASSERT(env);
797         LASSERT(los->los_dev);
798         ls = dt2ls_dev(los->los_dev);
799
800         cfs_mutex_lock(&ls->ls_los_mutex);
801         if (cfs_atomic_read(&los->los_refcount) == 0) {
802                 if (los->los_obj)
803                         lu_object_put_nocache(env, &los->los_obj->do_lu);
804                 cfs_list_del(&los->los_list);
805                 OBD_FREE_PTR(los);
806         }
807         cfs_mutex_unlock(&ls->ls_los_mutex);
808         ls_device_put(env, ls);
809 }
810 EXPORT_SYMBOL(local_oid_storage_fini);