Whamcloud - gitweb
LU-7899 osd: batch EA updates
[fs/lustre-release.git] / lustre / osd-zfs / osd_oi.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_oi.c
33  * OI functions to map fid to dnode
34  *
35  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36  * Author: Mike Pershin <tappro@whamcloud.com>
37  * Author: Di Wang <di.wang@intel.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_OSD
41
42 #include <lustre_ver.h>
43 #include <libcfs/libcfs.h>
44 #include <obd_support.h>
45 #include <lustre_net.h>
46 #include <obd.h>
47 #include <obd_class.h>
48 #include <lustre_disk.h>
49 #include <lustre_fid.h>
50
51 #include "osd_internal.h"
52
53 #include <sys/dnode.h>
54 #include <sys/dbuf.h>
55 #include <sys/spa.h>
56 #include <sys/stat.h>
57 #include <sys/zap.h>
58 #include <sys/spa_impl.h>
59 #include <sys/zfs_znode.h>
60 #include <sys/dmu_tx.h>
61 #include <sys/dmu_objset.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/sa_impl.h>
64 #include <sys/txg.h>
65
66 #define OSD_OI_FID_NR         (1UL << 7)
67 #define OSD_OI_FID_NR_MAX     (1UL << OSD_OI_FID_OID_BITS_MAX)
68 unsigned int osd_oi_count = OSD_OI_FID_NR;
69
70
71 /*
72  * zfs osd maintains names for known fids in the name hierarchy
73  * so that one can mount filesystem with regular ZFS stack and
74  * access files
75  */
76 struct named_oid {
77         unsigned long    oid;
78         char            *name;
79 };
80
81 static const struct named_oid oids[] = {
82         { .oid = LAST_RECV_OID,        .name = LAST_RCVD },
83         { .oid = OFD_LAST_GROUP_OID,   .name = "LAST_GROUP" },
84         { .oid = LLOG_CATALOGS_OID,    .name = "CATALOGS" },
85         { .oid = MGS_CONFIGS_OID,      /*MOUNT_CONFIGS_DIR*/ },
86         { .oid = FID_SEQ_SRV_OID,      .name = "seq_srv" },
87         { .oid = FID_SEQ_CTL_OID,      .name = "seq_ctl" },
88         { .oid = FLD_INDEX_OID,        .name = "fld" },
89         { .oid = MDD_LOV_OBJ_OID,      .name = LOV_OBJID },
90         { .oid = OFD_HEALTH_CHECK_OID, .name = HEALTH_CHECK },
91         { .oid = REPLY_DATA_OID,       .name = REPLY_DATA },
92         { .oid = 0 }
93 };
94
95 static char *oid2name(const unsigned long oid)
96 {
97         int i = 0;
98
99         while (oids[i].oid) {
100                 if (oids[i].oid == oid)
101                         return oids[i].name;
102                 i++;
103         }
104         return NULL;
105 }
106
107 /**
108  * Lookup an existing OI by the given name.
109  */
110 static int
111 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
112               uint64_t parent, const char *name, struct osd_oi *oi)
113 {
114         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
115         int                      rc;
116
117         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
118         if (rc)
119                 return rc;
120
121         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
122         if (rc >= sizeof(oi->oi_name))
123                 return -E2BIG;
124
125         oi->oi_zapid = zde->zde_dnode;
126
127         return 0;
128 }
129
130 /**
131  * Create a new OI with the given name.
132  */
133 static int
134 osd_oi_create(const struct lu_env *env, struct osd_device *o,
135               uint64_t parent, const char *name, uint64_t *child)
136 {
137         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
138         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
139         sa_handle_t             *sa_hdl = NULL;
140         dmu_tx_t                *tx;
141         uint64_t                 oid;
142         int                      rc;
143
144         /* verify it doesn't already exist */
145         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
146         if (rc == 0)
147                 return -EEXIST;
148
149         if (o->od_dt_dev.dd_rdonly)
150                 return -EROFS;
151
152         /* create fid-to-dnode index */
153         tx = dmu_tx_create(o->od_os);
154         if (tx == NULL)
155                 return -ENOMEM;
156
157         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
158         dmu_tx_hold_bonus(tx, parent);
159         dmu_tx_hold_zap(tx, parent, TRUE, name);
160         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
161
162         rc = -dmu_tx_assign(tx, TXG_WAIT);
163         if (rc) {
164                 dmu_tx_abort(tx);
165                 return rc;
166         }
167
168         oid = osd_zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
169                                    DMU_OT_DIRECTORY_CONTENTS,
170                                    14, /* == ZFS fzap_default_block_shift */
171                                    DN_MAX_INDBLKSHIFT,
172                                    0, tx);
173
174         rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
175         if (rc)
176                 goto commit;
177         la->la_valid = LA_MODE | LA_UID | LA_GID;
178         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
179         la->la_uid = la->la_gid = 0;
180         rc = __osd_attr_init(env, o, sa_hdl, tx, la, parent, NULL);
181         sa_handle_destroy(sa_hdl);
182         if (rc)
183                 goto commit;
184
185         zde->zde_dnode = oid;
186         zde->zde_pad = 0;
187         zde->zde_type = IFTODT(S_IFDIR);
188
189         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
190
191 commit:
192         if (rc)
193                 dmu_object_free(o->od_os, oid, tx);
194         dmu_tx_commit(tx);
195
196         if (rc == 0)
197                 *child = oid;
198
199         return rc;
200 }
201
202 static int
203 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
204                       uint64_t parent, const char *name, uint64_t *child)
205 {
206         struct osd_oi   oi;
207         int             rc;
208
209         rc = osd_oi_lookup(env, o, parent, name, &oi);
210         if (rc == 0)
211                 *child = oi.oi_zapid;
212         else if (rc == -ENOENT)
213                 rc = osd_oi_create(env, o, parent, name, child);
214
215         return rc;
216 }
217
218 /**
219  * Lookup the target index/flags of the fid, so it will know where
220  * the object is located (tgt index) and it is MDT or OST object.
221  */
222 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
223                    u64 seq, struct lu_seq_range *range)
224 {
225         struct seq_server_site  *ss = osd_seq_site(osd);
226
227         if (fid_seq_is_idif(seq)) {
228                 fld_range_set_ost(range);
229                 range->lsr_index = idif_ost_idx(seq);
230                 return 0;
231         }
232
233         if (!fid_seq_in_fldb(seq)) {
234                 fld_range_set_mdt(range);
235                 if (ss != NULL)
236                         /* FIXME: If ss is NULL, it suppose not get lsr_index
237                          * at all */
238                         range->lsr_index = ss->ss_node_id;
239                 return 0;
240         }
241
242         LASSERT(ss != NULL);
243         fld_range_set_any(range);
244         /* OSD will only do local fld lookup */
245         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
246 }
247
248 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
249                   const struct lu_fid *fid)
250 {
251         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
252         int                     rc;
253         ENTRY;
254
255         if (fid_is_idif(fid))
256                 RETURN(1);
257
258         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
259                      fid_is_name_llog(fid) || fid_is_quota(fid))
260                 RETURN(0);
261
262         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
263         if (rc != 0) {
264                 /* During upgrade, OST FLDB might not be loaded because
265                  * OST FLDB is not created until 2.6, so if some DNE
266                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
267                  * not be able to find the sequence from local FLDB
268                  * cache see fld_index_init(). */
269                 if (rc == -ENOENT && osd->od_is_ost)
270                         RETURN(1);
271
272                 if (rc != -ENOENT)
273                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
274                                osd_name(osd), PFID(fid), rc);
275                 RETURN(0);
276         }
277
278         if (fld_range_is_ost(range))
279                 RETURN(1);
280
281         RETURN(0);
282 }
283
284 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
285                                            u64 seq)
286 {
287         struct osd_seq *osd_seq;
288
289         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
290                 if (osd_seq->os_seq == seq)
291                         return osd_seq;
292         }
293         return NULL;
294 }
295
296 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
297 {
298         struct osd_seq *osd_seq;
299
300         read_lock(&seq_list->osl_seq_list_lock);
301         osd_seq = osd_seq_find_locked(seq_list, seq);
302         read_unlock(&seq_list->osl_seq_list_lock);
303
304         return osd_seq;
305 }
306
307 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
308                                            struct osd_device *osd, u64 seq)
309 {
310         struct osd_seq_list     *seq_list = &osd->od_seq_list;
311         struct osd_seq          *osd_seq;
312         char                    *key = osd_oti_get(env)->oti_buf;
313         char                    *seq_name = osd_oti_get(env)->oti_str;
314         struct osd_oi           oi;
315         uint64_t                sdb, odb;
316         int                     i;
317         int                     rc = 0;
318         ENTRY;
319
320         osd_seq = osd_seq_find(seq_list, seq);
321         if (osd_seq != NULL)
322                 RETURN(osd_seq);
323
324         down(&seq_list->osl_seq_init_sem);
325         /* Check again, in case some one else already add it
326          * to the list */
327         osd_seq = osd_seq_find(seq_list, seq);
328         if (osd_seq != NULL)
329                 GOTO(out, rc = 0);
330
331         OBD_ALLOC_PTR(osd_seq);
332         if (osd_seq == NULL)
333                 GOTO(out, rc = -ENOMEM);
334
335         INIT_LIST_HEAD(&osd_seq->os_seq_list);
336         osd_seq->os_seq = seq;
337
338         /* Init subdir count to be 32, but each seq can have
339          * different subdir count */
340         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
341         OBD_ALLOC(osd_seq->os_compat_dirs,
342                   sizeof(uint64_t) * osd_seq->os_subdir_count);
343         if (osd_seq->os_compat_dirs == NULL)
344                 GOTO(out, rc = -ENOMEM);
345
346         oi.oi_zapid = osd->od_O_id;
347         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
348                 fid_seq_is_mdt0(seq)) ?  "%llu" : "%llx",
349                 fid_seq_is_idif(seq) ? 0 : seq);
350
351         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
352         if (rc != 0) {
353                 CERROR("%s: Can not create %s : rc = %d\n",
354                        osd_name(osd), seq_name, rc);
355                 GOTO(out, rc);
356         }
357
358         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
359                 sprintf(key, "d%d", i);
360                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
361                 if (rc)
362                         GOTO(out, rc);
363                 osd_seq->os_compat_dirs[i] = sdb;
364         }
365
366         write_lock(&seq_list->osl_seq_list_lock);
367         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
368         write_unlock(&seq_list->osl_seq_list_lock);
369 out:
370         up(&seq_list->osl_seq_init_sem);
371         if (rc != 0) {
372                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
373                         OBD_FREE(osd_seq->os_compat_dirs,
374                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
375                 if (osd_seq != NULL)
376                         OBD_FREE_PTR(osd_seq);
377                 osd_seq = ERR_PTR(rc);
378         }
379         RETURN(osd_seq);
380 }
381
382 /*
383  * objects w/o a natural reference (unlike a file on a MDS)
384  * are put under a special hierarchy /O/<seq>/d0..dXX
385  * this function returns a directory specific fid belongs to
386  */
387 static uint64_t
388 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
389                         const struct lu_fid *fid, char *buf, int bufsize)
390 {
391         struct osd_seq  *osd_seq;
392         unsigned long   b;
393         u64             id;
394         int             rc;
395
396         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
397         if (IS_ERR(osd_seq)) {
398                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
399                        PFID(fid));
400                 return PTR_ERR(osd_seq);
401         }
402
403         if (fid_is_last_id(fid)) {
404                 id = 0;
405         } else {
406                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
407                 LASSERT(rc == 0); /* we should not get here with IGIF */
408                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
409         }
410
411         b = id % OSD_OST_MAP_SIZE;
412         LASSERT(osd_seq->os_compat_dirs[b]);
413
414         if (buf)
415                 snprintf(buf, bufsize, "%llu", id);
416
417         return osd_seq->os_compat_dirs[b];
418 }
419
420 /* XXX: f_ver is not counted, but may differ too */
421 static void osd_fid2str(char *buf, const struct lu_fid *fid)
422 {
423         sprintf(buf, DFID_NOBRACE, PFID(fid));
424 }
425
426 /*
427  * Determine the zap object id which is being used as the OI for the
428  * given fid.  The lowest N bits in the sequence ID are used as the
429  * index key.  On failure 0 is returned which zfs treats internally
430  * as an invalid object id.
431  */
432 static uint64_t
433 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
434                     char *buf, dnode_t **zdn)
435 {
436         struct osd_oi *oi;
437
438         LASSERT(osd->od_oi_table != NULL);
439         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
440         if (buf)
441                 osd_fid2str(buf, fid);
442         if (zdn)
443                 *zdn = oi->oi_dn;
444
445         return oi->oi_zapid;
446 }
447
448 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
449                             const struct lu_fid *fid, char *buf, int bufsize,
450                             dnode_t **zdn)
451 {
452         uint64_t zapid;
453
454         LASSERT(fid);
455         LASSERT(!fid_is_acct(fid));
456
457         if (zdn != NULL)
458                 *zdn = NULL;
459
460         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
461                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
462         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
463                 /* special objects with fixed known fids get their name */
464                 char *name = oid2name(fid_oid(fid));
465
466                 if (name) {
467                         zapid = osd->od_root;
468                         if (buf)
469                                 strncpy(buf, name, bufsize);
470                 } else {
471                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL);
472                 }
473         } else {
474                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn);
475         }
476
477         return zapid;
478 }
479
480 static inline int fid_is_fs_root(const struct lu_fid *fid)
481 {
482         /* Map root inode to special local object FID */
483         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
484                 fid_oid(fid) == OSD_FS_ROOT_OID;
485 }
486
487 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
488                    const struct lu_fid *fid, uint64_t *oid)
489 {
490         struct osd_thread_info  *info = osd_oti_get(env);
491         char                    *buf = info->oti_buf;
492         dnode_t *zdn;
493         uint64_t zapid;
494         int                     rc = 0;
495         ENTRY;
496
497         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
498                 RETURN(-ENOENT);
499
500         LASSERT(!fid_is_acct(fid));
501
502         if (unlikely(fid_is_fs_root(fid))) {
503                 *oid = dev->od_root;
504         } else {
505                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
506                                            sizeof(info->oti_buf), &zdn);
507                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
508                                     8, 1, &info->oti_zde);
509                 if (rc)
510                         RETURN(rc);
511                 *oid = info->oti_zde.lzd_reg.zde_dnode;
512         }
513
514         if (rc == 0)
515                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
516                                  ZIO_PRIORITY_ASYNC_READ);
517
518         RETURN(rc);
519 }
520
521 /**
522  * Close an entry in a specific slot.
523  */
524 static void
525 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
526 {
527         struct osd_oi *oi;
528
529         LASSERT(key < o->od_oi_count);
530
531         oi = o->od_oi_table[key];
532         if (oi) {
533                 if (oi->oi_dn)
534                         osd_dnode_rele(oi->oi_dn);
535                 OBD_FREE_PTR(oi);
536                 o->od_oi_table[key] = NULL;
537         }
538 }
539
540 /**
541  * Allocate and open a new entry in the specified unused slot.
542  */
543 static int
544 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
545                  char *name, int key)
546 {
547         struct osd_oi *oi;
548         int rc;
549
550         LASSERT(key < o->od_oi_count);
551         LASSERT(o->od_oi_table[key] == NULL);
552
553         OBD_ALLOC_PTR(oi);
554         if (oi == NULL)
555                 return -ENOMEM;
556
557         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
558         if (rc) {
559                 OBD_FREE_PTR(oi);
560                 return rc;
561         }
562
563         o->od_oi_table[key] = oi;
564         __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
565
566         return 0;
567 }
568
569 /**
570  * Depopulate the OI table.
571  */
572 static void
573 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
574 {
575         int i;
576
577         for (i = 0; i < o->od_oi_count; i++)
578                 osd_oi_remove_table(env, o, i);
579 }
580
581 /**
582  * Populate the OI table based.
583  */
584 static int
585 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
586 {
587         char name[16];
588         int  i, rc = 0;
589         ENTRY;
590
591         for (i = 0; i < count; i++) {
592                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
593                 rc = osd_oi_add_table(env, o, name, i);
594                 if (rc) {
595                         osd_oi_close_table(env, o);
596                         break;
597                 }
598         }
599
600         RETURN(rc);
601 }
602
603 /**
604  * Determine if the type and number of OIs used by this file system.
605  */
606 static int
607 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
608 {
609         uint64_t        root_oid = o->od_root;
610         struct osd_oi   oi;
611         char            name[16];
612         int             rc;
613         ENTRY;
614
615         /*
616          * Check for multiple OIs and determine the count.  There is no
617          * gap handling, if an OI is missing the wrong size can be returned.
618          * The only safeguard is that we know the number of OIs must be a
619          * power of two and this is checked for basic sanity.
620          */
621         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
622                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
623                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
624                 if (rc == 0)
625                         continue;
626
627                 if (rc == -ENOENT) {
628                         if (*count == 0)
629                                 break;
630
631                         if ((*count & (*count - 1)) != 0)
632                                 RETURN(-EDOM);
633
634                         RETURN(0);
635                 }
636
637                 RETURN(rc);
638         }
639
640         /*
641          * No OIs exist, this must be a new filesystem.
642          */
643         *count = 0;
644
645         RETURN(0);
646 }
647
648 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
649 {
650         struct osd_seq_list     *osl = &osd->od_seq_list;
651         struct osd_seq          *osd_seq, *tmp;
652
653         write_lock(&osl->osl_seq_list_lock);
654         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
655                                  os_seq_list) {
656                 list_del(&osd_seq->os_seq_list);
657                 OBD_FREE(osd_seq->os_compat_dirs,
658                          sizeof(uint64_t) * osd_seq->os_subdir_count);
659                 OBD_FREE(osd_seq, sizeof(*osd_seq));
660         }
661         write_unlock(&osl->osl_seq_list_lock);
662
663         return;
664 }
665
666 /**
667  * Create /O subdirectory to map legacy OST objects for compatibility.
668  */
669 static int
670 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
671 {
672         uint64_t sdb;
673         int rc;
674         ENTRY;
675
676         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
677         if (!rc)
678                 o->od_O_id = sdb;
679
680         RETURN(rc);
681 }
682
683 /**
684  * Initialize the OIs by either opening or creating them as needed.
685  */
686 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
687 {
688         char    *key = osd_oti_get(env)->oti_buf;
689         int      i, rc, count = 0;
690         ENTRY;
691
692         rc = osd_oi_probe(env, o, &count);
693         if (rc)
694                 RETURN(rc);
695
696         if (count == 0) {
697                 uint64_t odb, sdb;
698
699                 count = osd_oi_count;
700                 odb = o->od_root;
701
702                 for (i = 0; i < count; i++) {
703                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
704                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
705                         if (rc)
706                                 RETURN(rc);
707                 }
708         }
709
710         rc = osd_oi_init_compat(env, o);
711         if (rc)
712                 RETURN(rc);
713
714         LASSERT((count & (count - 1)) == 0);
715         o->od_oi_count = count;
716         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
717         if (o->od_oi_table == NULL)
718                 RETURN(-ENOMEM);
719
720         rc = osd_oi_open_table(env, o, count);
721         if (rc) {
722                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
723                 o->od_oi_table = NULL;
724         }
725
726         RETURN(rc);
727 }
728
729 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
730 {
731         ENTRY;
732
733         osd_ost_seq_fini(env, o);
734
735         if (o->od_oi_table != NULL) {
736                 (void) osd_oi_close_table(env, o);
737                 OBD_FREE(o->od_oi_table,
738                          sizeof(struct osd_oi *) * o->od_oi_count);
739                 o->od_oi_table = NULL;
740                 o->od_oi_count = 0;
741         }
742
743         EXIT;
744 }
745
746 int osd_options_init(void)
747 {
748         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
749         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
750                 osd_oi_count = OSD_OI_FID_NR;
751
752         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
753                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
754                         osd_oi_count, size_roundup_power2(osd_oi_count));
755                 osd_oi_count = size_roundup_power2(osd_oi_count);
756         }
757
758         return 0;
759 }
760
761 /*
762  * the following set of functions are used to maintain per-thread
763  * cache of FID->ino mapping. this mechanism is used to avoid
764  * expensive LU/OI lookups.
765  */
766 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
767                                      struct osd_device *osd,
768                                      const struct lu_fid *fid)
769 {
770         struct osd_thread_info *oti = osd_oti_get(env);
771         struct osd_idmap_cache *idc = oti->oti_ins_cache;
772         int i;
773
774         for (i = 0; i < oti->oti_ins_cache_used; i++) {
775                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
776                         continue;
777                 if (idc[i].oic_dev != osd)
778                         continue;
779
780                 return idc + i;
781         }
782
783         return NULL;
784 }
785
786 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
787                                     struct osd_device *osd,
788                                     const struct lu_fid *fid)
789 {
790         struct osd_thread_info *oti = osd_oti_get(env);
791         struct osd_idmap_cache *idc;
792         int i;
793
794         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
795                 i = oti->oti_ins_cache_size * 2;
796                 LASSERT(i < 1000);
797                 if (i == 0)
798                         i = OSD_INS_CACHE_SIZE;
799                 OBD_ALLOC(idc, sizeof(*idc) * i);
800                 if (idc == NULL)
801                         return ERR_PTR(-ENOMEM);
802                 if (oti->oti_ins_cache != NULL) {
803                         memcpy(idc, oti->oti_ins_cache,
804                                oti->oti_ins_cache_used * sizeof(*idc));
805                         OBD_FREE(oti->oti_ins_cache,
806                                  oti->oti_ins_cache_used * sizeof(*idc));
807                 }
808                 oti->oti_ins_cache = idc;
809                 oti->oti_ins_cache_size = i;
810         }
811
812         idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
813         idc->oic_fid = *fid;
814         idc->oic_dev = osd;
815         idc->oic_dnode = 0;
816         idc->oic_remote = 0;
817
818         return idc;
819 }
820
821 /**
822  * Lookup mapping for the given fid in the cache
823  *
824  * Initialize a new one if not found. the initialization checks whether
825  * the object is local or remote. for the local objects, OI is used to
826  * learn dnode#. the function is used when the caller has no information
827  * about the object, e.g. at dt_insert().
828  */
829 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
830                                              struct osd_device *osd,
831                                              const struct lu_fid *fid)
832 {
833         struct osd_idmap_cache *idc;
834         int rc;
835
836         LASSERT(!fid_is_acct(fid));
837
838         idc = osd_idc_find(env, osd, fid);
839         if (idc != NULL)
840                 return idc;
841
842         /* new mapping is needed */
843         idc = osd_idc_add(env, osd, fid);
844         if (IS_ERR(idc))
845                 return idc;
846
847         /* initialize it */
848         rc = osd_remote_fid(env, osd, fid);
849         if (unlikely(rc < 0))
850                 return ERR_PTR(rc);
851
852         if (rc == 0) {
853                 /* the object is local, lookup in OI */
854                 uint64_t dnode;
855
856                 rc = osd_fid_lookup(env, osd, fid, &dnode);
857                 if (unlikely(rc < 0)) {
858                         CERROR("%s: can't lookup: rc = %d\n",
859                                osd->od_svname, rc);
860                         return ERR_PTR(rc);
861                 }
862                 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
863                 idc->oic_dnode = dnode;
864         } else {
865                 /* the object is remote */
866                 idc->oic_remote = 1;
867         }
868
869         return idc;
870 }
871
872 /*
873  * lookup mapping for given FID and fill it from the given object.
874  * the object is local by definition.
875  */
876 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
877                           struct osd_object *obj)
878 {
879         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
880         struct osd_idmap_cache *idc;
881
882         idc = osd_idc_find(env, osd, fid);
883         if (idc != NULL) {
884                 if (obj->oo_dn == NULL)
885                         return 0;
886                 idc->oic_dnode = obj->oo_dn->dn_object;
887                 return 0;
888         }
889
890         /* new mapping is needed */
891         idc = osd_idc_add(env, osd, fid);
892         if (IS_ERR(idc))
893                 return PTR_ERR(idc);
894
895         if (obj->oo_dn)
896                 idc->oic_dnode = obj->oo_dn->dn_object;
897
898         return 0;
899 }