Whamcloud - gitweb
26a3137f2b3ae79225cf45aad7dced3e9779aba0
[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         obd_id                   lastid;
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         lastid = cpu_to_le64(los->los_last_oid);
278
279         dti->dti_off = 0;
280         dti->dti_lb.lb_buf = &lastid;
281         dti->dti_lb.lb_len = sizeof(lastid);
282         rc = dt_record_write(env, los->los_obj, &dti->dti_lb, &dti->dti_off,
283                              th);
284         mutex_unlock(&los->los_id_lock);
285
286         RETURN(rc);
287 }
288
289 /*
290  * Create local named object (file, directory or index) in parent directory.
291  */
292 struct dt_object *__local_file_create(const struct lu_env *env,
293                                       const struct lu_fid *fid,
294                                       struct local_oid_storage *los,
295                                       struct ls_device *ls,
296                                       struct dt_object *parent,
297                                       const char *name, struct lu_attr *attr,
298                                       struct dt_object_format *dof)
299 {
300         struct dt_thread_info   *dti = dt_info(env);
301         struct dt_object        *dto;
302         struct thandle          *th;
303         int                      rc;
304
305         dto = ls_locate(env, ls, fid);
306         if (unlikely(IS_ERR(dto)))
307                 RETURN(dto);
308
309         LASSERT(dto != NULL);
310         if (dt_object_exists(dto))
311                 GOTO(out, rc = -EEXIST);
312
313         th = dt_trans_create(env, ls->ls_osd);
314         if (IS_ERR(th))
315                 GOTO(out, rc = PTR_ERR(th));
316
317         rc = local_object_declare_create(env, los, dto, attr, dof, th);
318         if (rc)
319                 GOTO(trans_stop, rc);
320
321         if (dti->dti_dof.dof_type == DFT_DIR) {
322                 dt_declare_ref_add(env, dto, th);
323                 dt_declare_ref_add(env, parent, th);
324         }
325
326         rc = dt_declare_insert(env, parent, (void *)fid, (void *)name, th);
327         if (rc)
328                 GOTO(trans_stop, rc);
329
330         rc = dt_trans_start_local(env, ls->ls_osd, th);
331         if (rc)
332                 GOTO(trans_stop, rc);
333
334         dt_write_lock(env, dto, 0);
335         if (dt_object_exists(dto))
336                 GOTO(unlock, rc = 0);
337
338         CDEBUG(D_OTHER, "create new object "DFID"\n",
339                PFID(lu_object_fid(&dto->do_lu)));
340         rc = local_object_create(env, los, dto, attr, dof, th);
341         if (rc)
342                 GOTO(unlock, rc);
343         LASSERT(dt_object_exists(dto));
344
345         if (dti->dti_dof.dof_type == DFT_DIR) {
346                 if (!dt_try_as_dir(env, dto))
347                         GOTO(destroy, rc = -ENOTDIR);
348                 /* Add "." and ".." for newly created dir */
349                 rc = dt_insert(env, dto, (void *)fid, (void *)".", th,
350                                BYPASS_CAPA, 1);
351                 if (rc)
352                         GOTO(destroy, rc);
353                 dt_ref_add(env, dto, th);
354                 rc = dt_insert(env, dto, (void *)lu_object_fid(&parent->do_lu),
355                                (void *)"..", th, BYPASS_CAPA, 1);
356                 if (rc)
357                         GOTO(destroy, rc);
358         }
359
360         dt_write_lock(env, parent, 0);
361         rc = dt_insert(env, parent, (const struct dt_rec *)fid,
362                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
363         if (dti->dti_dof.dof_type == DFT_DIR)
364                 dt_ref_add(env, parent, th);
365         dt_write_unlock(env, parent);
366         if (rc)
367                 GOTO(destroy, rc);
368 destroy:
369         if (rc)
370                 dt_destroy(env, dto, th);
371 unlock:
372         dt_write_unlock(env, dto);
373 trans_stop:
374         dt_trans_stop(env, ls->ls_osd, th);
375 out:
376         if (rc) {
377                 lu_object_put_nocache(env, &dto->do_lu);
378                 dto = ERR_PTR(rc);
379         }
380         RETURN(dto);
381 }
382
383 /*
384  * Look up and create (if it does not exist) a local named file or directory in
385  * parent directory.
386  */
387 struct dt_object *local_file_find_or_create(const struct lu_env *env,
388                                             struct local_oid_storage *los,
389                                             struct dt_object *parent,
390                                             const char *name, __u32 mode)
391 {
392         struct dt_thread_info   *dti = dt_info(env);
393         struct dt_object        *dto;
394         int                      rc;
395
396         LASSERT(parent);
397
398         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
399         if (rc == 0)
400                 /* name is found, get the object */
401                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
402         else if (rc != -ENOENT)
403                 dto = ERR_PTR(rc);
404         else {
405                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
406                 if (rc < 0) {
407                         dto = ERR_PTR(rc);
408                 } else {
409                         /* create the object */
410                         dti->dti_attr.la_valid  = LA_MODE;
411                         dti->dti_attr.la_mode   = mode;
412                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
413                         dto = __local_file_create(env, &dti->dti_fid, los,
414                                                   dt2ls_dev(los->los_dev),
415                                                   parent, name, &dti->dti_attr,
416                                                   &dti->dti_dof);
417                 }
418         }
419         return dto;
420 }
421 EXPORT_SYMBOL(local_file_find_or_create);
422
423 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
424                                                      struct dt_device *dt,
425                                                      const struct lu_fid *fid,
426                                                      struct dt_object *parent,
427                                                      const char *name,
428                                                      __u32 mode)
429 {
430         struct dt_thread_info   *dti = dt_info(env);
431         struct dt_object        *dto;
432         int                      rc;
433
434         LASSERT(parent);
435
436         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
437         if (rc == 0) {
438                 dto = dt_locate(env, dt, &dti->dti_fid);
439         } else if (rc != -ENOENT) {
440                 dto = ERR_PTR(rc);
441         } else {
442                 struct ls_device *ls;
443
444                 ls = ls_device_get(dt);
445                 if (IS_ERR(ls)) {
446                         dto = ERR_PTR(PTR_ERR(ls));
447                 } else {
448                         /* create the object */
449                         dti->dti_attr.la_valid  = LA_MODE;
450                         dti->dti_attr.la_mode   = mode;
451                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
452                         dto = __local_file_create(env, fid, NULL, ls, parent,
453                                                   name, &dti->dti_attr,
454                                                   &dti->dti_dof);
455                         /* ls_device_put() will finalize the ls device, we
456                          * have to open the object in other device stack */
457                         if (!IS_ERR(dto)) {
458                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
459                                 lu_object_put_nocache(env, &dto->do_lu);
460                                 dto = dt_locate(env, dt, &dti->dti_fid);
461                         }
462                         ls_device_put(env, ls);
463                 }
464         }
465         return dto;
466 }
467 EXPORT_SYMBOL(local_file_find_or_create_with_fid);
468
469 /*
470  * Look up and create (if it does not exist) a local named index file in parent
471  * directory.
472  */
473 struct dt_object *local_index_find_or_create(const struct lu_env *env,
474                                              struct local_oid_storage *los,
475                                              struct dt_object *parent,
476                                              const char *name, __u32 mode,
477                                              const struct dt_index_features *ft)
478 {
479         struct dt_thread_info   *dti = dt_info(env);
480         struct dt_object        *dto;
481         int                      rc;
482
483         LASSERT(parent);
484
485         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
486         if (rc == 0) {
487                 /* name is found, get the object */
488                 dto = ls_locate(env, dt2ls_dev(los->los_dev), &dti->dti_fid);
489         } else if (rc != -ENOENT) {
490                 dto = ERR_PTR(rc);
491         } else {
492                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
493                 if (rc < 0) {
494                         dto = ERR_PTR(rc);
495                 } else {
496                         /* create the object */
497                         dti->dti_attr.la_valid          = LA_MODE;
498                         dti->dti_attr.la_mode           = mode;
499                         dti->dti_dof.dof_type           = DFT_INDEX;
500                         dti->dti_dof.u.dof_idx.di_feat  = ft;
501                         dto = __local_file_create(env, &dti->dti_fid, los,
502                                                   dt2ls_dev(los->los_dev),
503                                                   parent, name, &dti->dti_attr,
504                                                   &dti->dti_dof);
505                 }
506         }
507         return dto;
508
509 }
510 EXPORT_SYMBOL(local_index_find_or_create);
511
512 struct dt_object *
513 local_index_find_or_create_with_fid(const struct lu_env *env,
514                                     struct dt_device *dt,
515                                     const struct lu_fid *fid,
516                                     struct dt_object *parent,
517                                     const char *name, __u32 mode,
518                                     const struct dt_index_features *ft)
519 {
520         struct dt_thread_info   *dti = dt_info(env);
521         struct dt_object        *dto;
522         int                      rc;
523
524         LASSERT(parent);
525
526         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
527         if (rc == 0) {
528                 /* name is found, get the object */
529                 if (!lu_fid_eq(fid, &dti->dti_fid))
530                         dto = ERR_PTR(-EINVAL);
531                 else
532                         dto = dt_locate(env, dt, fid);
533         } else if (rc != -ENOENT) {
534                 dto = ERR_PTR(rc);
535         } else {
536                 struct ls_device *ls;
537
538                 ls = ls_device_get(dt);
539                 if (IS_ERR(ls)) {
540                         dto = ERR_PTR(PTR_ERR(ls));
541                 } else {
542                         /* create the object */
543                         dti->dti_attr.la_valid          = LA_MODE;
544                         dti->dti_attr.la_mode           = mode;
545                         dti->dti_dof.dof_type           = DFT_INDEX;
546                         dti->dti_dof.u.dof_idx.di_feat  = ft;
547                         dto = __local_file_create(env, fid, NULL, ls, parent,
548                                                   name, &dti->dti_attr,
549                                                   &dti->dti_dof);
550                         /* ls_device_put() will finalize the ls device, we
551                          * have to open the object in other device stack */
552                         if (!IS_ERR(dto)) {
553                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
554                                 lu_object_put_nocache(env, &dto->do_lu);
555                                 dto = dt_locate(env, dt, &dti->dti_fid);
556                         }
557                         ls_device_put(env, ls);
558                 }
559         }
560         return dto;
561 }
562 EXPORT_SYMBOL(local_index_find_or_create_with_fid);
563
564 static int local_object_declare_unlink(const struct lu_env *env,
565                                        struct dt_device *dt,
566                                        struct dt_object *p,
567                                        struct dt_object *c, const char *name,
568                                        struct thandle *th)
569 {
570         int rc;
571
572         rc = dt_declare_delete(env, p, (const struct dt_key *)name, th);
573         if (rc < 0)
574                 return rc;
575
576         rc = dt_declare_ref_del(env, c, th);
577         if (rc < 0)
578                 return rc;
579
580         return dt_declare_destroy(env, c, th);
581 }
582
583 int local_object_unlink(const struct lu_env *env, struct dt_device *dt,
584                         struct dt_object *parent, const char *name)
585 {
586         struct dt_thread_info   *dti = dt_info(env);
587         struct dt_object        *dto;
588         struct thandle          *th;
589         int                      rc;
590
591         ENTRY;
592
593         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
594         if (rc == -ENOENT)
595                 RETURN(0);
596         else if (rc < 0)
597                 RETURN(rc);
598
599         dto = dt_locate(env, dt, &dti->dti_fid);
600         if (unlikely(IS_ERR(dto)))
601                 RETURN(PTR_ERR(dto));
602
603         th = dt_trans_create(env, dt);
604         if (IS_ERR(th))
605                 GOTO(out, rc = PTR_ERR(th));
606
607         rc = local_object_declare_unlink(env, dt, parent, dto, name, th);
608         if (rc < 0)
609                 GOTO(stop, rc);
610
611         rc = dt_trans_start_local(env, dt, th);
612         if (rc < 0)
613                 GOTO(stop, rc);
614
615         dt_write_lock(env, dto, 0);
616         rc = dt_delete(env, parent, (struct dt_key *)name, th, BYPASS_CAPA);
617         if (rc < 0)
618                 GOTO(unlock, rc);
619
620         rc = dt_ref_del(env, dto, th);
621         if (rc < 0) {
622                 rc = dt_insert(env, parent,
623                                (const struct dt_rec *)&dti->dti_fid,
624                                (const struct dt_key *)name, th, BYPASS_CAPA, 1);
625                 GOTO(unlock, rc);
626         }
627
628         rc = dt_destroy(env, dto, th);
629 unlock:
630         dt_write_unlock(env, dto);
631 stop:
632         dt_trans_stop(env, dt, th);
633 out:
634         lu_object_put_nocache(env, &dto->do_lu);
635         return rc;
636 }
637 EXPORT_SYMBOL(local_object_unlink);
638
639 struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
640 {
641         struct local_oid_storage *los, *ret = NULL;
642
643         cfs_list_for_each_entry(los, &ls->ls_los_list, los_list) {
644                 if (los->los_seq == seq) {
645                         cfs_atomic_inc(&los->los_refcount);
646                         ret = los;
647                         break;
648                 }
649         }
650         return ret;
651 }
652
653 void dt_los_put(struct local_oid_storage *los)
654 {
655         if (cfs_atomic_dec_and_test(&los->los_refcount))
656                 /* should never happen, only local_oid_storage_fini should
657                  * drop refcount to zero */
658                 LBUG();
659         return;
660 }
661
662 /* after Lustre 2.3 release there may be old file to store last generated FID
663  * If such file exists then we have to read its content
664  */
665 int lastid_compat_check(const struct lu_env *env, struct dt_device *dev,
666                         __u64 lastid_seq, __u32 *first_oid, struct ls_device *ls)
667 {
668         struct dt_thread_info   *dti = dt_info(env);
669         struct dt_object        *root = NULL;
670         struct los_ondisk        losd;
671         struct dt_object        *o = NULL;
672         int                      rc = 0;
673
674         rc = dt_root_get(env, dev, &dti->dti_fid);
675         if (rc)
676                 return rc;
677
678         root = ls_locate(env, ls, &dti->dti_fid);
679         if (IS_ERR(root))
680                 return PTR_ERR(root);
681
682         /* find old last_id file */
683         snprintf(dti->dti_buf, sizeof(dti->dti_buf), "seq-"LPX64"-lastid",
684                  lastid_seq);
685         rc = dt_lookup_dir(env, root, dti->dti_buf, &dti->dti_fid);
686         lu_object_put_nocache(env, &root->do_lu);
687         if (rc == -ENOENT) {
688                 /* old llog lastid accessed by FID only */
689                 if (lastid_seq != FID_SEQ_LLOG)
690                         return 0;
691                 dti->dti_fid.f_seq = FID_SEQ_LLOG;
692                 dti->dti_fid.f_oid = 1;
693                 dti->dti_fid.f_ver = 0;
694                 o = ls_locate(env, ls, &dti->dti_fid);
695                 if (IS_ERR(o))
696                         return PTR_ERR(o);
697
698                 if (!dt_object_exists(o)) {
699                         lu_object_put_nocache(env, &o->do_lu);
700                         return 0;
701                 }
702                 CDEBUG(D_INFO, "Found old llog lastid file\n");
703         } else if (rc < 0) {
704                 return rc;
705         } else {
706                 CDEBUG(D_INFO, "Found old lastid file for sequence "LPX64"\n",
707                        lastid_seq);
708                 o = ls_locate(env, ls, &dti->dti_fid);
709                 if (IS_ERR(o))
710                         return PTR_ERR(o);
711         }
712         /* let's read seq-NNNNNN-lastid file value */
713         LASSERT(dt_object_exists(o));
714         dti->dti_off = 0;
715         dti->dti_lb.lb_buf = &losd;
716         dti->dti_lb.lb_len = sizeof(losd);
717         dt_read_lock(env, o, 0);
718         rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
719         dt_read_unlock(env, o);
720         lu_object_put_nocache(env, &o->do_lu);
721         if (rc == 0 && le32_to_cpu(losd.lso_magic) != LOS_MAGIC) {
722                 CERROR("%s: wrong content of seq-"LPX64"-lastid file, magic %x\n",
723                        o->do_lu.lo_dev->ld_obd->obd_name, lastid_seq,
724                        le32_to_cpu(losd.lso_magic));
725                 return -EINVAL;
726         } else if (rc < 0) {
727                 CERROR("%s: failed to read seq-"LPX64"-lastid: rc = %d\n",
728                        o->do_lu.lo_dev->ld_obd->obd_name, lastid_seq, rc);
729                 return rc;
730         }
731         *first_oid = le32_to_cpu(losd.lso_next_oid);
732         return rc;
733 }
734
735 /**
736  * Initialize local OID storage for required sequence.
737  * That may be needed for services that uses local files and requires
738  * dynamic OID allocation for them.
739  *
740  * Per each sequence we have an object with 'first_fid' identificator
741  * containing the counter for OIDs of locally created files with that
742  * sequence.
743  *
744  * It is used now by llog subsystem and MGS for NID tables
745  *
746  * Function gets first_fid to create counter object.
747  * All dynamic fids will be generated with the same sequence and incremented
748  * OIDs
749  *
750  * Returned local_oid_storage is in-memory representaion of OID storage
751  */
752 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
753                            const struct lu_fid *first_fid,
754                            struct local_oid_storage **los)
755 {
756         struct dt_thread_info   *dti = dt_info(env);
757         struct ls_device        *ls;
758         obd_id                   lastid;
759         struct dt_object        *o = NULL;
760         struct thandle          *th;
761         __u32                    first_oid = fid_oid(first_fid);
762         int                      rc = 0;
763
764         ENTRY;
765
766         ls = ls_device_get(dev);
767         if (IS_ERR(ls))
768                 RETURN(PTR_ERR(ls));
769
770         mutex_lock(&ls->ls_los_mutex);
771         *los = dt_los_find(ls, fid_seq(first_fid));
772         if (*los != NULL)
773                 GOTO(out, rc = 0);
774
775         /* not found, then create */
776         OBD_ALLOC_PTR(*los);
777         if (*los == NULL)
778                 GOTO(out, rc = -ENOMEM);
779
780         cfs_atomic_set(&(*los)->los_refcount, 1);
781         mutex_init(&(*los)->los_id_lock);
782         (*los)->los_dev = &ls->ls_top_dev;
783         cfs_atomic_inc(&ls->ls_refcount);
784         cfs_list_add(&(*los)->los_list, &ls->ls_los_list);
785
786         /* Use {seq, 0, 0} to create the LAST_ID file for every
787          * sequence.  OIDs start at LUSTRE_FID_INIT_OID.
788          */
789         dti->dti_fid.f_seq = fid_seq(first_fid);
790         dti->dti_fid.f_oid = LUSTRE_FID_LASTID_OID;
791         dti->dti_fid.f_ver = 0;
792         o = ls_locate(env, ls, &dti->dti_fid);
793         if (IS_ERR(o))
794                 GOTO(out_los, rc = PTR_ERR(o));
795
796         if (!dt_object_exists(o)) {
797                 rc = lastid_compat_check(env, dev, fid_seq(first_fid),
798                                          &first_oid, ls);
799                 if (rc < 0)
800                         GOTO(out_los, rc);
801
802                 th = dt_trans_create(env, dev);
803                 if (IS_ERR(th))
804                         GOTO(out_los, rc = PTR_ERR(th));
805
806                 dti->dti_attr.la_valid = LA_MODE | LA_TYPE;
807                 dti->dti_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
808                 dti->dti_dof.dof_type = dt_mode_to_dft(S_IFREG);
809
810                 rc = dt_declare_create(env, o, &dti->dti_attr, NULL,
811                                        &dti->dti_dof, th);
812                 if (rc)
813                         GOTO(out_trans, rc);
814
815                 rc = dt_declare_record_write(env, o, sizeof(lastid), 0, th);
816                 if (rc)
817                         GOTO(out_trans, rc);
818
819                 rc = dt_trans_start_local(env, dev, th);
820                 if (rc)
821                         GOTO(out_trans, rc);
822
823                 dt_write_lock(env, o, 0);
824                 if (dt_object_exists(o))
825                         GOTO(out_lock, rc = 0);
826
827                 rc = dt_create(env, o, &dti->dti_attr, NULL, &dti->dti_dof,
828                                th);
829                 if (rc)
830                         GOTO(out_lock, rc);
831
832                 lastid = cpu_to_le64(first_oid);
833
834                 dti->dti_off = 0;
835                 dti->dti_lb.lb_buf = &lastid;
836                 dti->dti_lb.lb_len = sizeof(lastid);
837                 rc = dt_record_write(env, o, &dti->dti_lb, &dti->dti_off, th);
838                 if (rc)
839                         GOTO(out_lock, rc);
840 out_lock:
841                 dt_write_unlock(env, o);
842 out_trans:
843                 dt_trans_stop(env, dev, th);
844         } else {
845                 dti->dti_off = 0;
846                 dti->dti_lb.lb_buf = &lastid;
847                 dti->dti_lb.lb_len = sizeof(lastid);
848                 dt_read_lock(env, o, 0);
849                 rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
850                 dt_read_unlock(env, o);
851                 if (rc == 0 && le64_to_cpu(lastid) > OBIF_MAX_OID) {
852                         CERROR("%s: bad oid "LPU64" is read from LAST_ID\n",
853                                o->do_lu.lo_dev->ld_obd->obd_name,
854                                le64_to_cpu(lastid));
855                         rc = -EINVAL;
856                 }
857         }
858 out_los:
859         if (rc != 0) {
860                 cfs_list_del(&(*los)->los_list);
861                 cfs_atomic_dec(&ls->ls_refcount);
862                 OBD_FREE_PTR(*los);
863                 *los = NULL;
864                 if (o != NULL && !IS_ERR(o))
865                         lu_object_put_nocache(env, &o->do_lu);
866         } else {
867                 (*los)->los_seq = fid_seq(first_fid);
868                 (*los)->los_last_oid = le64_to_cpu(lastid);
869                 (*los)->los_obj = o;
870                 /* read value should not be less than initial one */
871                 LASSERTF((*los)->los_last_oid >= first_oid, "%u < %u\n",
872                          (*los)->los_last_oid, first_oid);
873         }
874 out:
875         mutex_unlock(&ls->ls_los_mutex);
876         ls_device_put(env, ls);
877         return rc;
878 }
879 EXPORT_SYMBOL(local_oid_storage_init);
880
881 void local_oid_storage_fini(const struct lu_env *env,
882                             struct local_oid_storage *los)
883 {
884         struct ls_device *ls;
885
886         if (!cfs_atomic_dec_and_test(&los->los_refcount))
887                 return;
888
889         LASSERT(env);
890         LASSERT(los->los_dev);
891         ls = dt2ls_dev(los->los_dev);
892
893         mutex_lock(&ls->ls_los_mutex);
894         if (cfs_atomic_read(&los->los_refcount) == 0) {
895                 if (los->los_obj)
896                         lu_object_put_nocache(env, &los->los_obj->do_lu);
897                 cfs_list_del(&los->los_list);
898                 OBD_FREE_PTR(los);
899         }
900         mutex_unlock(&ls->ls_los_mutex);
901         ls_device_put(env, ls);
902 }
903 EXPORT_SYMBOL(local_oid_storage_fini);