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