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