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