Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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, 2017, 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 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_of(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_PRE(obj, sizeof(*obj), "kfreed");
69         kfree_rcu(obj, ls_header.loh_rcu);
70 }
71
72 static const struct lu_object_operations ls_lu_obj_ops = {
73         .loo_object_init  = ls_object_init,
74         .loo_object_free  = ls_object_free,
75 };
76
77 static struct lu_object *ls_object_alloc(const struct lu_env *env,
78                                          const struct lu_object_header *_h,
79                                          struct lu_device *d)
80 {
81         struct lu_object_header *h;
82         struct ls_object        *o;
83         struct lu_object        *l;
84
85         LASSERT(_h == NULL);
86
87         OBD_ALLOC_PTR(o);
88         if (o != NULL) {
89                 l = &o->ls_obj.do_lu;
90                 h = &o->ls_header;
91
92                 lu_object_header_init(h);
93                 dt_object_init(&o->ls_obj, h, d);
94                 lu_object_add_top(h, l);
95
96                 l->lo_ops = &ls_lu_obj_ops;
97
98                 return l;
99         } else {
100                 return NULL;
101         }
102 }
103
104 static const struct lu_device_operations ls_lu_dev_ops = {
105         .ldo_object_alloc =     ls_object_alloc
106 };
107
108 static const struct lu_device_type_operations ls_device_type_ops = {
109         .ldto_start = NULL,
110         .ldto_stop  = NULL,
111 };
112
113 static struct lu_device_type ls_lu_type = {
114         .ldt_name = "local_storage",
115         .ldt_ops  = &ls_device_type_ops,
116 };
117
118 static
119 struct ls_device *ls_device_init(struct dt_device *dev)
120 {
121         struct ls_device *ls;
122
123         ENTRY;
124
125         OBD_ALLOC_PTR(ls);
126         if (ls == NULL)
127                 GOTO(out_ls, ls = ERR_PTR(-ENOMEM));
128
129         kref_init(&ls->ls_refcount);
130         INIT_LIST_HEAD(&ls->ls_los_list);
131         mutex_init(&ls->ls_los_mutex);
132
133         ls->ls_osd = dev;
134
135         LASSERT(dev->dd_lu_dev.ld_site);
136         lu_device_init(&ls->ls_top_dev.dd_lu_dev, &ls_lu_type);
137         ls->ls_top_dev.dd_lu_dev.ld_ops = &ls_lu_dev_ops;
138         ls->ls_top_dev.dd_lu_dev.ld_site = dev->dd_lu_dev.ld_site;
139
140         /* finally add ls to the list */
141         list_add(&ls->ls_linkage, &ls_list_head);
142 out_ls:
143         RETURN(ls);
144 }
145
146 struct ls_device *ls_device_find_or_init(struct dt_device *dev)
147 {
148         struct ls_device *ls, *ret = NULL;
149
150         ENTRY;
151
152         mutex_lock(&ls_list_mutex);
153         /* find */
154         list_for_each_entry(ls, &ls_list_head, ls_linkage) {
155                 if (ls->ls_osd == dev) {
156                         kref_get(&ls->ls_refcount);
157                         ret = ls;
158                         break;
159                 }
160         }
161         /* found */
162         if (ret)
163                 GOTO(out_ls, ret);
164
165         /* not found, then create */
166         ls = ls_device_init(dev);
167 out_ls:
168         mutex_unlock(&ls_list_mutex);
169         RETURN(ls);
170 }
171
172 static void ls_device_put_free(struct kref *kref)
173 {
174         struct ls_device *ls = container_of(kref, struct ls_device,
175                                             ls_refcount);
176
177         LASSERT(list_empty(&ls->ls_los_list));
178         list_del(&ls->ls_linkage);
179         mutex_unlock(&ls_list_mutex);
180 }
181
182 void ls_device_put(const struct lu_env *env, struct ls_device *ls)
183 {
184         LASSERT(env);
185         if (kref_put_mutex(&ls->ls_refcount, ls_device_put_free,
186                            &ls_list_mutex)) {
187                 lu_site_purge(env, ls->ls_top_dev.dd_lu_dev.ld_site, ~0);
188                 lu_device_fini(&ls->ls_top_dev.dd_lu_dev);
189                 OBD_FREE_PTR(ls);
190         }
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, false))
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, DT_SRC_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);
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);
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, DT_SRC_PARENT);
406         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
407                        (const struct dt_key *)name, th);
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                 dt_object_put_nocache(env, dto);
423                 dto = ERR_PTR(rc);
424         }
425         RETURN(dto);
426 }
427
428 struct dt_object *local_file_find(const struct lu_env *env,
429                                   struct local_oid_storage *los,
430                                   struct dt_object *parent,
431                                   const char *name)
432 {
433         struct dt_thread_info   *dti = dt_info(env);
434         struct dt_object        *dto;
435         int                      rc;
436
437         LASSERT(parent);
438
439         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
440         if (!rc)
441                 dto = ls_locate(env, dt2ls_dev(los->los_dev),
442                                 &dti->dti_fid, NULL);
443         else
444                 dto = ERR_PTR(rc);
445
446         return dto;
447 }
448 EXPORT_SYMBOL(local_file_find);
449
450 /*
451  * Look up and create (if it does not exist) a local named file or directory in
452  * parent directory.
453  */
454 struct dt_object *local_file_find_or_create(const struct lu_env *env,
455                                             struct local_oid_storage *los,
456                                             struct dt_object *parent,
457                                             const char *name, __u32 mode)
458 {
459         struct dt_thread_info   *dti = dt_info(env);
460         struct dt_object        *dto;
461         int                      rc;
462
463         dto = local_file_find(env, los, parent, name);
464         if (!IS_ERR(dto) || PTR_ERR(dto) != -ENOENT)
465                 return dto;
466
467         rc = local_object_fid_generate(env, los, &dti->dti_fid);
468         if (rc)
469                 return ERR_PTR(rc);
470
471         /* create the object */
472         dti->dti_attr.la_valid = LA_MODE;
473         dti->dti_attr.la_mode = mode;
474         dti->dti_dof.dof_type = dt_mode_to_dft(mode & S_IFMT);
475         dto = __local_file_create(env, &dti->dti_fid, los,
476                                   dt2ls_dev(los->los_dev), parent, name,
477                                   &dti->dti_attr, &dti->dti_dof);
478         return dto;
479 }
480 EXPORT_SYMBOL(local_file_find_or_create);
481
482 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
483                                                      struct dt_device *dt,
484                                                      const struct lu_fid *fid,
485                                                      struct dt_object *parent,
486                                                      const char *name,
487                                                      __u32 mode)
488 {
489         struct dt_thread_info   *dti = dt_info(env);
490         struct dt_object        *dto;
491         int                      rc;
492
493         LASSERT(parent);
494
495         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
496         if (rc == 0) {
497                 dto = dt_locate(env, dt, &dti->dti_fid);
498         } else if (rc != -ENOENT) {
499                 dto = ERR_PTR(rc);
500         } else {
501                 struct ls_device *ls;
502
503                 ls = ls_device_find_or_init(dt);
504                 if (IS_ERR(ls)) {
505                         dto = ERR_CAST(ls);
506                 } else {
507                         /* create the object */
508                         dti->dti_attr.la_valid  = LA_MODE;
509                         dti->dti_attr.la_mode   = mode;
510                         dti->dti_dof.dof_type   = dt_mode_to_dft(mode & S_IFMT);
511                         dto = __local_file_create(env, fid, NULL, ls, parent,
512                                                   name, &dti->dti_attr,
513                                                   &dti->dti_dof);
514                         /* ls_device_put() will finalize the ls device, we
515                          * have to open the object in other device stack */
516                         if (!IS_ERR(dto)) {
517                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
518                                 dt_object_put_nocache(env, dto);
519                                 dto = dt_locate(env, dt, &dti->dti_fid);
520                         }
521                         ls_device_put(env, ls);
522                 }
523         }
524         return dto;
525 }
526 EXPORT_SYMBOL(local_file_find_or_create_with_fid);
527
528 /*
529  * Look up and create (if it does not exist) a local named index file in parent
530  * directory.
531  */
532 struct dt_object *local_index_find_or_create(const struct lu_env *env,
533                                              struct local_oid_storage *los,
534                                              struct dt_object *parent,
535                                              const char *name, __u32 mode,
536                                              const struct dt_index_features *ft)
537 {
538         struct dt_thread_info   *dti = dt_info(env);
539         struct dt_object        *dto;
540         int                      rc;
541
542         LASSERT(parent);
543
544         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
545         if (rc == 0) {
546                 /* name is found, get the object */
547                 dto = ls_locate(env, dt2ls_dev(los->los_dev),
548                                 &dti->dti_fid, NULL);
549         } else if (rc != -ENOENT) {
550                 dto = ERR_PTR(rc);
551         } else {
552                 rc = local_object_fid_generate(env, los, &dti->dti_fid);
553                 if (rc < 0) {
554                         dto = ERR_PTR(rc);
555                 } else {
556                         /* create the object */
557                         dti->dti_attr.la_valid          = LA_MODE;
558                         dti->dti_attr.la_mode           = mode;
559                         dti->dti_dof.dof_type           = DFT_INDEX;
560                         dti->dti_dof.u.dof_idx.di_feat  = ft;
561                         dto = __local_file_create(env, &dti->dti_fid, los,
562                                                   dt2ls_dev(los->los_dev),
563                                                   parent, name, &dti->dti_attr,
564                                                   &dti->dti_dof);
565                 }
566         }
567         return dto;
568
569 }
570 EXPORT_SYMBOL(local_index_find_or_create);
571
572 struct dt_object *
573 local_index_find_or_create_with_fid(const struct lu_env *env,
574                                     struct dt_device *dt,
575                                     const struct lu_fid *fid,
576                                     struct dt_object *parent,
577                                     const char *name, __u32 mode,
578                                     const struct dt_index_features *ft)
579 {
580         struct dt_thread_info   *dti = dt_info(env);
581         struct dt_object        *dto;
582         int                      rc;
583
584         LASSERT(parent);
585
586         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
587         if (rc == 0) {
588                 /* name is found, get the object */
589                 if (!lu_fid_eq(fid, &dti->dti_fid))
590                         dto = ERR_PTR(-EINVAL);
591                 else
592                         dto = dt_locate(env, dt, fid);
593         } else if (rc != -ENOENT) {
594                 dto = ERR_PTR(rc);
595         } else {
596                 struct ls_device *ls;
597
598                 ls = ls_device_find_or_init(dt);
599                 if (IS_ERR(ls)) {
600                         dto = ERR_CAST(ls);
601                 } else {
602                         /* create the object */
603                         dti->dti_attr.la_valid          = LA_MODE;
604                         dti->dti_attr.la_mode           = mode;
605                         dti->dti_dof.dof_type           = DFT_INDEX;
606                         dti->dti_dof.u.dof_idx.di_feat  = ft;
607                         dto = __local_file_create(env, fid, NULL, ls, parent,
608                                                   name, &dti->dti_attr,
609                                                   &dti->dti_dof);
610                         /* ls_device_put() will finalize the ls device, we
611                          * have to open the object in other device stack */
612                         if (!IS_ERR(dto)) {
613                                 dti->dti_fid = dto->do_lu.lo_header->loh_fid;
614                                 dt_object_put_nocache(env, dto);
615                                 dto = dt_locate(env, dt, &dti->dti_fid);
616                         }
617                         ls_device_put(env, ls);
618                 }
619         }
620         return dto;
621 }
622 EXPORT_SYMBOL(local_index_find_or_create_with_fid);
623
624 static int local_object_declare_unlink(const struct lu_env *env,
625                                        struct dt_device *dt,
626                                        struct dt_object *p,
627                                        struct dt_object *c, const char *name,
628                                        struct thandle *th)
629 {
630         int rc;
631
632         rc = dt_declare_delete(env, p, (const struct dt_key *)name, th);
633         if (rc < 0)
634                 return rc;
635
636         if (S_ISDIR(p->do_lu.lo_header->loh_attr)) {
637                 rc = dt_declare_ref_del(env, p, th);
638                 if (rc < 0)
639                         return rc;
640         }
641
642         rc = dt_declare_ref_del(env, c, th);
643         if (rc < 0)
644                 return rc;
645
646         return dt_declare_destroy(env, c, th);
647 }
648
649 int local_object_unlink(const struct lu_env *env, struct dt_device *dt,
650                         struct dt_object *parent, const char *name)
651 {
652         struct dt_thread_info   *dti = dt_info(env);
653         struct dt_object        *dto;
654         struct thandle          *th;
655         int                      rc;
656
657         ENTRY;
658
659         rc = dt_lookup_dir(env, parent, name, &dti->dti_fid);
660         if (rc == -ENOENT)
661                 RETURN(0);
662         else if (rc < 0)
663                 RETURN(rc);
664
665         dto = dt_locate(env, dt, &dti->dti_fid);
666         if (unlikely(IS_ERR(dto)))
667                 RETURN(PTR_ERR(dto));
668
669         th = dt_trans_create(env, dt);
670         if (IS_ERR(th))
671                 GOTO(out, rc = PTR_ERR(th));
672
673         rc = local_object_declare_unlink(env, dt, parent, dto, name, th);
674         if (rc < 0)
675                 GOTO(stop, rc);
676
677         rc = dt_trans_start_local(env, dt, th);
678         if (rc < 0)
679                 GOTO(stop, rc);
680
681         if (S_ISDIR(dto->do_lu.lo_header->loh_attr)) {
682                 dt_write_lock(env, parent, 0);
683                 rc = dt_ref_del(env, parent, th);
684                 dt_write_unlock(env, parent);
685                 if (rc)
686                         GOTO(stop, rc);
687         }
688
689         dt_write_lock(env, dto, 0);
690         rc = dt_delete(env, parent, (struct dt_key *)name, th);
691         if (rc < 0)
692                 GOTO(unlock, rc);
693
694         rc = dt_ref_del(env, dto, th);
695         if (rc < 0) {
696                 struct dt_insert_rec *rec = &dti->dti_dt_rec;
697
698                 rec->rec_fid = &dti->dti_fid;
699                 rec->rec_type = dto->do_lu.lo_header->loh_attr;
700                 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
701                                (const struct dt_key *)name, th);
702                 GOTO(unlock, rc);
703         }
704
705         rc = dt_destroy(env, dto, th);
706 unlock:
707         dt_write_unlock(env, dto);
708 stop:
709         dt_trans_stop(env, dt, th);
710 out:
711         dt_object_put_nocache(env, dto);
712         return rc;
713 }
714 EXPORT_SYMBOL(local_object_unlink);
715
716 struct local_oid_storage *dt_los_find(struct ls_device *ls, __u64 seq)
717 {
718         struct local_oid_storage *los, *ret = NULL;
719
720         list_for_each_entry(los, &ls->ls_los_list, los_list) {
721                 if (los->los_seq == seq) {
722                         atomic_inc(&los->los_refcount);
723                         ret = los;
724                         break;
725                 }
726         }
727         return ret;
728 }
729
730 void dt_los_put(struct local_oid_storage *los)
731 {
732         if (atomic_dec_and_test(&los->los_refcount))
733                 /* should never happen, only local_oid_storage_fini should
734                  * drop refcount to zero */
735                 LBUG();
736 }
737
738 /* after Lustre 2.3 release there may be old file to store last generated FID
739  * If such file exists then we have to read its content
740  */
741 static int lastid_compat_check(const struct lu_env *env, struct dt_device *dev,
742                                __u64 lastid_seq, __u32 *first_oid,
743                                struct ls_device *ls)
744 {
745         struct dt_thread_info   *dti = dt_info(env);
746         struct dt_object        *root = NULL;
747         struct los_ondisk        losd;
748         struct dt_object        *o = NULL;
749         int                      rc = 0;
750
751         rc = dt_root_get(env, dev, &dti->dti_fid);
752         if (rc)
753                 return rc;
754
755         root = ls_locate(env, ls, &dti->dti_fid, NULL);
756         if (IS_ERR(root))
757                 return PTR_ERR(root);
758
759         /* find old last_id file */
760         snprintf(dti->dti_buf, sizeof(dti->dti_buf), "seq-%#llx-lastid",
761                  lastid_seq);
762         rc = dt_lookup_dir(env, root, dti->dti_buf, &dti->dti_fid);
763         dt_object_put_nocache(env, root);
764         if (rc == -ENOENT) {
765                 /* old llog lastid accessed by FID only */
766                 if (lastid_seq != FID_SEQ_LLOG)
767                         return 0;
768                 dti->dti_fid.f_seq = FID_SEQ_LLOG;
769                 dti->dti_fid.f_oid = 1;
770                 dti->dti_fid.f_ver = 0;
771                 o = ls_locate(env, ls, &dti->dti_fid, NULL);
772                 if (IS_ERR(o))
773                         return PTR_ERR(o);
774
775                 if (!dt_object_exists(o)) {
776                         dt_object_put_nocache(env, o);
777                         return 0;
778                 }
779                 CDEBUG(D_INFO, "Found old llog lastid file\n");
780         } else if (rc < 0) {
781                 return rc;
782         } else {
783                 CDEBUG(D_INFO, "Found old lastid file for sequence %#llx\n",
784                        lastid_seq);
785                 o = ls_locate(env, ls, &dti->dti_fid, NULL);
786                 if (IS_ERR(o))
787                         return PTR_ERR(o);
788         }
789         /* let's read seq-NNNNNN-lastid file value */
790         LASSERT(dt_object_exists(o));
791         dti->dti_off = 0;
792         dti->dti_lb.lb_buf = &losd;
793         dti->dti_lb.lb_len = sizeof(losd);
794         dt_read_lock(env, o, 0);
795         rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
796         dt_read_unlock(env, o);
797         if (rc == 0 && le32_to_cpu(losd.lso_magic) != LOS_MAGIC) {
798                 CERROR("%s: wrong content of seq-%#llx-lastid file, magic %x\n",
799                        o->do_lu.lo_dev->ld_obd->obd_name, lastid_seq,
800                        le32_to_cpu(losd.lso_magic));
801                 rc = -EINVAL;
802         } else if (rc < 0) {
803                 CERROR("%s: failed to read seq-%#llx-lastid: rc = %d\n",
804                        o->do_lu.lo_dev->ld_obd->obd_name, lastid_seq, rc);
805         }
806         dt_object_put_nocache(env, o);
807         if (rc == 0)
808                 *first_oid = le32_to_cpu(losd.lso_next_oid);
809         return rc;
810 }
811
812
813 /**
814  * Initialize local OID storage for required sequence.
815  * That may be needed for services that uses local files and requires
816  * dynamic OID allocation for them.
817  *
818  * Per each sequence we have an object with 'first_fid' identificator
819  * containing the counter for OIDs of locally created files with that
820  * sequence.
821  *
822  * It is used now by llog subsystem and MGS for NID tables
823  *
824  * Function gets first_fid to create counter object.
825  * All dynamic fids will be generated with the same sequence and incremented
826  * OIDs
827  *
828  * Returned local_oid_storage is in-memory representaion of OID storage
829  */
830 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
831                            const struct lu_fid *first_fid,
832                            struct local_oid_storage **los)
833 {
834         struct dt_thread_info   *dti = dt_info(env);
835         struct ls_device        *ls;
836         u64                      lastid;
837         struct dt_object        *o = NULL;
838         struct thandle          *th;
839         __u32                    first_oid = fid_oid(first_fid);
840         int                      rc = 0;
841
842         ENTRY;
843
844         ls = ls_device_find_or_init(dev);
845         if (IS_ERR(ls))
846                 RETURN(PTR_ERR(ls));
847
848         mutex_lock(&ls->ls_los_mutex);
849         *los = dt_los_find(ls, fid_seq(first_fid));
850         if (*los != NULL)
851                 GOTO(out, rc = 0);
852
853         /* not found, then create */
854         OBD_ALLOC_PTR(*los);
855         if (*los == NULL)
856                 GOTO(out, rc = -ENOMEM);
857
858         atomic_set(&(*los)->los_refcount, 1);
859         mutex_init(&(*los)->los_id_lock);
860         (*los)->los_dev = &ls->ls_top_dev;
861         kref_get(&ls->ls_refcount);
862         list_add(&(*los)->los_list, &ls->ls_los_list);
863
864         /* Use {seq, 0, 0} to create the LAST_ID file for every
865          * sequence.  OIDs start at LUSTRE_FID_INIT_OID.
866          */
867         dti->dti_fid.f_seq = fid_seq(first_fid);
868         dti->dti_fid.f_oid = LUSTRE_FID_LASTID_OID;
869         dti->dti_fid.f_ver = 0;
870         o = ls_locate(env, ls, &dti->dti_fid, NULL);
871         if (IS_ERR(o))
872                 GOTO(out_los, rc = PTR_ERR(o));
873
874         if (!dt_object_exists(o)) {
875                 rc = lastid_compat_check(env, dev, fid_seq(first_fid),
876                                          &first_oid, ls);
877                 if (rc < 0)
878                         GOTO(out_los, rc);
879
880                 th = dt_trans_create(env, dev);
881                 if (IS_ERR(th))
882                         GOTO(out_los, rc = PTR_ERR(th));
883
884                 dti->dti_attr.la_valid = LA_MODE | LA_TYPE;
885                 dti->dti_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
886                 dti->dti_dof.dof_type = dt_mode_to_dft(S_IFREG);
887
888                 rc = dt_declare_create(env, o, &dti->dti_attr, NULL,
889                                        &dti->dti_dof, th);
890                 if (rc)
891                         GOTO(out_trans, rc);
892
893                 lastid = cpu_to_le64(first_oid);
894
895                 dti->dti_off = 0;
896                 dti->dti_lb.lb_buf = &lastid;
897                 dti->dti_lb.lb_len = sizeof(lastid);
898                 rc = dt_declare_record_write(env, o, &dti->dti_lb, dti->dti_off,
899                                              th);
900                 if (rc)
901                         GOTO(out_trans, rc);
902
903                 rc = dt_trans_start_local(env, dev, th);
904                 if (rc)
905                         GOTO(out_trans, rc);
906
907                 dt_write_lock(env, o, 0);
908                 if (dt_object_exists(o))
909                         GOTO(out_lock, rc = 0);
910
911                 rc = dt_create(env, o, &dti->dti_attr, NULL, &dti->dti_dof,
912                                th);
913                 if (rc)
914                         GOTO(out_lock, rc);
915
916                 rc = dt_record_write(env, o, &dti->dti_lb, &dti->dti_off, th);
917                 if (rc)
918                         GOTO(out_lock, rc);
919 out_lock:
920                 dt_write_unlock(env, o);
921 out_trans:
922                 dt_trans_stop(env, dev, th);
923         } else {
924                 dti->dti_off = 0;
925                 dti->dti_lb.lb_buf = &lastid;
926                 dti->dti_lb.lb_len = sizeof(lastid);
927                 dt_read_lock(env, o, 0);
928                 rc = dt_record_read(env, o, &dti->dti_lb, &dti->dti_off);
929                 dt_read_unlock(env, o);
930                 if (rc == 0 && le64_to_cpu(lastid) > OBIF_MAX_OID) {
931                         CERROR("%s: bad oid %llu is read from LAST_ID\n",
932                                o->do_lu.lo_dev->ld_obd->obd_name,
933                                le64_to_cpu(lastid));
934                         rc = -EINVAL;
935                 }
936         }
937 out_los:
938         if (rc != 0) {
939                 list_del(&(*los)->los_list);
940                 ls_device_put(env, ls);
941                 OBD_FREE_PTR(*los);
942                 *los = NULL;
943                 if (o != NULL && !IS_ERR(o))
944                         dt_object_put_nocache(env, o);
945         } else {
946                 (*los)->los_seq = fid_seq(first_fid);
947                 (*los)->los_last_oid = le64_to_cpu(lastid);
948                 (*los)->los_obj = o;
949                 /* Read value should not be less than initial one
950                  * but possible after upgrade from older fs.
951                  * In this case just switch to the first_oid in memory and
952                  * it will be updated on disk with first object generated */
953                 if ((*los)->los_last_oid < first_oid)
954                         (*los)->los_last_oid = first_oid;
955         }
956 out:
957         mutex_unlock(&ls->ls_los_mutex);
958         ls_device_put(env, ls);
959         return rc;
960 }
961 EXPORT_SYMBOL(local_oid_storage_init);
962
963 void local_oid_storage_fini(const struct lu_env *env,
964                             struct local_oid_storage *los)
965 {
966         struct ls_device *ls;
967
968         LASSERT(env);
969         LASSERT(los->los_dev);
970         ls = dt2ls_dev(los->los_dev);
971
972         /* Take the mutex before decreasing the reference to avoid race
973          * conditions as described in LU-4721. */
974         mutex_lock(&ls->ls_los_mutex);
975         if (!atomic_dec_and_test(&los->los_refcount)) {
976                 mutex_unlock(&ls->ls_los_mutex);
977                 return;
978         }
979
980         if (los->los_obj)
981                 dt_object_put_nocache(env, los->los_obj);
982         list_del(&los->los_list);
983         OBD_FREE_PTR(los);
984         mutex_unlock(&ls->ls_los_mutex);
985         ls_device_put(env, ls);
986 }
987 EXPORT_SYMBOL(local_oid_storage_fini);