Whamcloud - gitweb
d17ae6ff66da8d52b8f34cfc43157c6eab7b9dec
[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 = ACCT_USER_OID,        .name = "acct_usr_inode" },
92         { .oid = ACCT_GROUP_OID,       .name = "acct_grp_inode" },
93         { .oid = REPLY_DATA_OID,       .name = REPLY_DATA },
94         { .oid = 0 }
95 };
96
97 static char *oid2name(const unsigned long oid)
98 {
99         int i = 0;
100
101         while (oids[i].oid) {
102                 if (oids[i].oid == oid)
103                         return oids[i].name;
104                 i++;
105         }
106         return NULL;
107 }
108
109 /**
110  * Lookup an existing OI by the given name.
111  */
112 static int
113 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
114               uint64_t parent, const char *name, struct osd_oi *oi)
115 {
116         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
117         int                      rc;
118
119         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
120         if (rc)
121                 return rc;
122
123         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
124         if (rc >= sizeof(oi->oi_name))
125                 return -E2BIG;
126
127         oi->oi_zapid = zde->zde_dnode;
128
129         return 0;
130 }
131
132 /**
133  * Create a new OI with the given name.
134  */
135 static int
136 osd_oi_create(const struct lu_env *env, struct osd_device *o,
137               uint64_t parent, const char *name, uint64_t *child)
138 {
139         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
140         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
141         sa_handle_t             *sa_hdl = NULL;
142         dmu_tx_t                *tx;
143         uint64_t                 oid;
144         int                      rc;
145
146         /* verify it doesn't already exist */
147         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
148         if (rc == 0)
149                 return -EEXIST;
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         LASSERT(tx->tx_objset->os_sa);
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 = 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, /* indirect block shift */
172                                DMU_OT_SA, DN_MAX_BONUSLEN, 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);
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                 if (rc != -ENOENT)
265                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
266                                osd_name(osd), PFID(fid), rc);
267                 RETURN(0);
268         }
269
270         if (fld_range_is_ost(range))
271                 RETURN(1);
272
273         RETURN(0);
274 }
275
276 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
277                                            u64 seq)
278 {
279         struct osd_seq *osd_seq;
280
281         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
282                 if (osd_seq->os_seq == seq)
283                         return osd_seq;
284         }
285         return NULL;
286 }
287
288 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
289 {
290         struct osd_seq *osd_seq;
291
292         read_lock(&seq_list->osl_seq_list_lock);
293         osd_seq = osd_seq_find_locked(seq_list, seq);
294         read_unlock(&seq_list->osl_seq_list_lock);
295
296         return osd_seq;
297 }
298
299 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
300                                            struct osd_device *osd, u64 seq)
301 {
302         struct osd_seq_list     *seq_list = &osd->od_seq_list;
303         struct osd_seq          *osd_seq;
304         char                    *key = osd_oti_get(env)->oti_buf;
305         char                    *seq_name = osd_oti_get(env)->oti_str;
306         struct osd_oi           oi;
307         uint64_t                sdb, odb;
308         int                     i;
309         int                     rc = 0;
310         ENTRY;
311
312         osd_seq = osd_seq_find(seq_list, seq);
313         if (osd_seq != NULL)
314                 RETURN(osd_seq);
315
316         down(&seq_list->osl_seq_init_sem);
317         /* Check again, in case some one else already add it
318          * to the list */
319         osd_seq = osd_seq_find(seq_list, seq);
320         if (osd_seq != NULL)
321                 GOTO(out, rc = 0);
322
323         OBD_ALLOC_PTR(osd_seq);
324         if (osd_seq == NULL)
325                 GOTO(out, rc = -ENOMEM);
326
327         INIT_LIST_HEAD(&osd_seq->os_seq_list);
328         osd_seq->os_seq = seq;
329
330         /* Init subdir count to be 32, but each seq can have
331          * different subdir count */
332         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
333         OBD_ALLOC(osd_seq->os_compat_dirs,
334                   sizeof(uint64_t) * osd_seq->os_subdir_count);
335         if (osd_seq->os_compat_dirs == NULL)
336                 GOTO(out, rc = -ENOMEM);
337
338         oi.oi_zapid = osd->od_O_id;
339         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
340                 fid_seq_is_mdt0(seq)) ?  "%llu" : "%llx",
341                 fid_seq_is_idif(seq) ? 0 : seq);
342
343         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
344         if (rc != 0) {
345                 CERROR("%s: Can not create %s : rc = %d\n",
346                        osd_name(osd), seq_name, rc);
347                 GOTO(out, rc);
348         }
349
350         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
351                 sprintf(key, "d%d", i);
352                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
353                 if (rc)
354                         GOTO(out, rc);
355                 osd_seq->os_compat_dirs[i] = sdb;
356         }
357
358         write_lock(&seq_list->osl_seq_list_lock);
359         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
360         write_unlock(&seq_list->osl_seq_list_lock);
361 out:
362         up(&seq_list->osl_seq_init_sem);
363         if (rc != 0) {
364                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
365                         OBD_FREE(osd_seq->os_compat_dirs,
366                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
367                 if (osd_seq != NULL)
368                         OBD_FREE_PTR(osd_seq);
369                 osd_seq = ERR_PTR(rc);
370         }
371         RETURN(osd_seq);
372 }
373
374 /*
375  * objects w/o a natural reference (unlike a file on a MDS)
376  * are put under a special hierarchy /O/<seq>/d0..dXX
377  * this function returns a directory specific fid belongs to
378  */
379 static uint64_t
380 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
381                         const struct lu_fid *fid, char *buf, int bufsize)
382 {
383         struct osd_seq  *osd_seq;
384         unsigned long   b;
385         u64             id;
386         int             rc;
387
388         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
389         if (IS_ERR(osd_seq)) {
390                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
391                        PFID(fid));
392                 return PTR_ERR(osd_seq);
393         }
394
395         if (fid_is_last_id(fid)) {
396                 id = 0;
397         } else {
398                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
399                 LASSERT(rc == 0); /* we should not get here with IGIF */
400                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
401         }
402
403         b = id % OSD_OST_MAP_SIZE;
404         LASSERT(osd_seq->os_compat_dirs[b]);
405
406         if (buf)
407                 snprintf(buf, bufsize, "%llu", id);
408
409         return osd_seq->os_compat_dirs[b];
410 }
411
412 /* XXX: f_ver is not counted, but may differ too */
413 static void osd_fid2str(char *buf, const struct lu_fid *fid)
414 {
415         sprintf(buf, DFID_NOBRACE, PFID(fid));
416 }
417
418 /*
419  * Determine the zap object id which is being used as the OI for the
420  * given fid.  The lowest N bits in the sequence ID are used as the
421  * index key.  On failure 0 is returned which zfs treats internally
422  * as an invalid object id.
423  */
424 static uint64_t
425 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
426                     char *buf)
427 {
428         struct osd_oi *oi;
429
430         LASSERT(osd->od_oi_table != NULL);
431         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
432         if (buf)
433                 osd_fid2str(buf, fid);
434
435         return oi->oi_zapid;
436 }
437
438 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
439                             const struct lu_fid *fid, char *buf, int bufsize)
440 {
441         uint64_t zapid;
442
443         LASSERT(fid);
444
445         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
446                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
447         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
448                 /* special objects with fixed known fids get their name */
449                 char *name = oid2name(fid_oid(fid));
450
451                 if (name) {
452                         zapid = osd->od_root;
453                         if (buf)
454                                 strncpy(buf, name, bufsize);
455                         if (fid_is_acct(fid))
456                                 zapid = MASTER_NODE_OBJ;
457                 } else {
458                         zapid = osd_get_idx_for_fid(osd, fid, buf);
459                 }
460         } else {
461                 zapid = osd_get_idx_for_fid(osd, fid, buf);
462         }
463
464         return zapid;
465 }
466
467 static inline int fid_is_fs_root(const struct lu_fid *fid)
468 {
469         /* Map root inode to special local object FID */
470         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
471                 fid_oid(fid) == OSD_FS_ROOT_OID;
472 }
473
474 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
475                    const struct lu_fid *fid, uint64_t *oid)
476 {
477         struct osd_thread_info  *info = osd_oti_get(env);
478         char                    *buf = info->oti_buf;
479         uint64_t                zapid;
480         int                     rc = 0;
481         ENTRY;
482
483         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
484                 RETURN(-ENOENT);
485
486         if (unlikely(fid_is_acct(fid))) {
487                 if (fid_oid(fid) == ACCT_USER_OID)
488                         *oid = dev->od_iusr_oid;
489                 else
490                         *oid = dev->od_igrp_oid;
491         } else if (unlikely(fid_is_fs_root(fid))) {
492                 *oid = dev->od_root;
493         } else {
494                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
495                                            sizeof(info->oti_buf));
496                 rc = -zap_lookup(dev->od_os, zapid, buf,
497                                 8, 1, &info->oti_zde);
498                 if (rc)
499                         RETURN(rc);
500                 *oid = info->oti_zde.lzd_reg.zde_dnode;
501         }
502
503         if (rc == 0)
504                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
505                                  ZIO_PRIORITY_ASYNC_READ);
506
507         RETURN(rc);
508 }
509
510 /**
511  * Close an entry in a specific slot.
512  */
513 static void
514 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
515 {
516         struct osd_oi *oi;
517
518         LASSERT(key < o->od_oi_count);
519
520         oi = o->od_oi_table[key];
521         if (oi) {
522                 if (oi->oi_db)
523                         sa_buf_rele(oi->oi_db, osd_obj_tag);
524                 OBD_FREE_PTR(oi);
525                 o->od_oi_table[key] = NULL;
526         }
527 }
528
529 /**
530  * Allocate and open a new entry in the specified unused slot.
531  */
532 static int
533 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
534                  char *name, int key)
535 {
536         struct osd_oi *oi;
537         int rc;
538
539         LASSERT(key < o->od_oi_count);
540         LASSERT(o->od_oi_table[key] == NULL);
541
542         OBD_ALLOC_PTR(oi);
543         if (oi == NULL)
544                 return -ENOMEM;
545
546         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
547         if (rc) {
548                 OBD_FREE_PTR(oi);
549                 return rc;
550         }
551
552         o->od_oi_table[key] = oi;
553         __osd_obj2dbuf(env, o->od_os, oi->oi_zapid, &oi->oi_db);
554
555         return 0;
556 }
557
558 /**
559  * Depopulate the OI table.
560  */
561 static void
562 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
563 {
564         int i;
565
566         for (i = 0; i < o->od_oi_count; i++)
567                 osd_oi_remove_table(env, o, i);
568 }
569
570 /**
571  * Populate the OI table based.
572  */
573 static int
574 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
575 {
576         char name[16];
577         int  i, rc = 0;
578         ENTRY;
579
580         for (i = 0; i < count; i++) {
581                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
582                 rc = osd_oi_add_table(env, o, name, i);
583                 if (rc) {
584                         osd_oi_close_table(env, o);
585                         break;
586                 }
587         }
588
589         RETURN(rc);
590 }
591
592 /**
593  * Determine if the type and number of OIs used by this file system.
594  */
595 static int
596 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
597 {
598         uint64_t        root_oid = o->od_root;
599         struct osd_oi   oi;
600         char            name[16];
601         int             rc;
602         ENTRY;
603
604         /*
605          * Check for multiple OIs and determine the count.  There is no
606          * gap handling, if an OI is missing the wrong size can be returned.
607          * The only safeguard is that we know the number of OIs must be a
608          * power of two and this is checked for basic sanity.
609          */
610         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
611                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
612                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
613                 if (rc == 0)
614                         continue;
615
616                 if (rc == -ENOENT) {
617                         if (*count == 0)
618                                 break;
619
620                         if ((*count & (*count - 1)) != 0)
621                                 RETURN(-EDOM);
622
623                         RETURN(0);
624                 }
625
626                 RETURN(rc);
627         }
628
629         /*
630          * No OIs exist, this must be a new filesystem.
631          */
632         *count = 0;
633
634         RETURN(0);
635 }
636
637 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
638 {
639         struct osd_seq_list     *osl = &osd->od_seq_list;
640         struct osd_seq          *osd_seq, *tmp;
641
642         write_lock(&osl->osl_seq_list_lock);
643         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
644                                  os_seq_list) {
645                 list_del(&osd_seq->os_seq_list);
646                 OBD_FREE(osd_seq->os_compat_dirs,
647                          sizeof(uint64_t) * osd_seq->os_subdir_count);
648                 OBD_FREE(osd_seq, sizeof(*osd_seq));
649         }
650         write_unlock(&osl->osl_seq_list_lock);
651
652         return;
653 }
654
655 /**
656  * Create /O subdirectory to map legacy OST objects for compatibility.
657  */
658 static int
659 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
660 {
661         uint64_t         odb, sdb;
662         int              rc;
663         ENTRY;
664
665         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
666         if (rc)
667                 RETURN(rc);
668
669         o->od_O_id = sdb;
670
671         /* Create on-disk indexes to maintain per-UID/GID inode usage.
672          * Those new indexes are created in the top-level ZAP outside the
673          * namespace in order not to confuse ZPL which might interpret those
674          * indexes as directories and assume the values are object IDs */
675         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
676                         oid2name(ACCT_USER_OID), &odb);
677         if (rc)
678                 RETURN(rc);
679         o->od_iusr_oid = odb;
680
681         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
682                         oid2name(ACCT_GROUP_OID), &odb);
683         if (rc)
684                 RETURN(rc);
685         o->od_igrp_oid = odb;
686
687         RETURN(rc);
688 }
689
690 /**
691  * Initialize the OIs by either opening or creating them as needed.
692  */
693 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
694 {
695         char    *key = osd_oti_get(env)->oti_buf;
696         int      i, rc, count = 0;
697         ENTRY;
698
699         rc = osd_oi_probe(env, o, &count);
700         if (rc)
701                 RETURN(rc);
702
703         if (count == 0) {
704                 uint64_t odb, sdb;
705
706                 count = osd_oi_count;
707                 odb = o->od_root;
708
709                 for (i = 0; i < count; i++) {
710                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
711                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
712                         if (rc)
713                                 RETURN(rc);
714                 }
715         }
716
717         rc = osd_oi_init_compat(env, o);
718         if (rc)
719                 RETURN(rc);
720
721         LASSERT((count & (count - 1)) == 0);
722         o->od_oi_count = count;
723         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
724         if (o->od_oi_table == NULL)
725                 RETURN(-ENOMEM);
726
727         rc = osd_oi_open_table(env, o, count);
728         if (rc) {
729                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
730                 o->od_oi_table = NULL;
731         }
732
733         RETURN(rc);
734 }
735
736 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
737 {
738         ENTRY;
739
740         osd_ost_seq_fini(env, o);
741
742         if (o->od_oi_table != NULL) {
743                 (void) osd_oi_close_table(env, o);
744                 OBD_FREE(o->od_oi_table,
745                          sizeof(struct osd_oi *) * o->od_oi_count);
746                 o->od_oi_table = NULL;
747                 o->od_oi_count = 0;
748         }
749
750         EXIT;
751 }
752
753 int osd_options_init(void)
754 {
755         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
756         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
757                 osd_oi_count = OSD_OI_FID_NR;
758
759         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
760                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
761                         osd_oi_count, size_roundup_power2(osd_oi_count));
762                 osd_oi_count = size_roundup_power2(osd_oi_count);
763         }
764
765         return 0;
766 }
767
768 /*
769  * the following set of functions are used to maintain per-thread
770  * cache of FID->ino mapping. this mechanism is used to avoid
771  * expensive LU/OI lookups.
772  */
773 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
774                                      struct osd_device *osd,
775                                      const struct lu_fid *fid)
776 {
777         struct osd_thread_info *oti = osd_oti_get(env);
778         struct osd_idmap_cache *idc = oti->oti_ins_cache;
779         int i;
780
781         for (i = 0; i < oti->oti_ins_cache_used; i++) {
782                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
783                         continue;
784                 if (idc[i].oic_dev != osd)
785                         continue;
786
787                 return idc + i;
788         }
789
790         return NULL;
791 }
792
793 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
794                                     struct osd_device *osd,
795                                     const struct lu_fid *fid)
796 {
797         struct osd_thread_info *oti = osd_oti_get(env);
798         struct osd_idmap_cache *idc;
799         int i;
800
801         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
802                 i = oti->oti_ins_cache_size * 2;
803                 LASSERT(i < 1000);
804                 if (i == 0)
805                         i = OSD_INS_CACHE_SIZE;
806                 OBD_ALLOC(idc, sizeof(*idc) * i);
807                 if (idc == NULL)
808                         return ERR_PTR(-ENOMEM);
809                 if (oti->oti_ins_cache != NULL) {
810                         memcpy(idc, oti->oti_ins_cache,
811                                oti->oti_ins_cache_used * sizeof(*idc));
812                         OBD_FREE(oti->oti_ins_cache,
813                                  oti->oti_ins_cache_used * sizeof(*idc));
814                 }
815                 oti->oti_ins_cache = idc;
816                 oti->oti_ins_cache_size = i;
817         }
818
819         idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
820         idc->oic_fid = *fid;
821         idc->oic_dev = osd;
822         idc->oic_dnode = 0;
823         idc->oic_remote = 0;
824
825         return idc;
826 }
827
828 /**
829  * Lookup mapping for the given fid in the cache
830  *
831  * Initialize a new one if not found. the initialization checks whether
832  * the object is local or remote. for the local objects, OI is used to
833  * learn dnode#. the function is used when the caller has no information
834  * about the object, e.g. at dt_insert().
835  */
836 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
837                                              struct osd_device *osd,
838                                              const struct lu_fid *fid)
839 {
840         struct osd_idmap_cache *idc;
841         int rc;
842
843         idc = osd_idc_find(env, osd, fid);
844         if (idc != NULL)
845                 return idc;
846
847         /* new mapping is needed */
848         idc = osd_idc_add(env, osd, fid);
849         if (IS_ERR(idc))
850                 return idc;
851
852         /* initialize it */
853         rc = osd_remote_fid(env, osd, fid);
854         if (unlikely(rc < 0))
855                 return ERR_PTR(rc);
856
857         if (rc == 0) {
858                 /* the object is local, lookup in OI */
859                 uint64_t dnode;
860
861                 rc = osd_fid_lookup(env, osd, fid, &dnode);
862                 if (unlikely(rc < 0)) {
863                         CERROR("%s: can't lookup: rc = %d\n",
864                                osd->od_svname, rc);
865                         return ERR_PTR(rc);
866                 }
867                 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
868                 idc->oic_dnode = dnode;
869         } else {
870                 /* the object is remote */
871                 idc->oic_remote = 1;
872         }
873
874         return idc;
875 }
876
877 /*
878  * lookup mapping for given FID and fill it from the given object.
879  * the object is local by definition.
880  */
881 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
882                           struct osd_object *obj)
883 {
884         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
885         struct osd_idmap_cache *idc;
886
887         idc = osd_idc_find(env, osd, fid);
888         if (idc != NULL) {
889                 if (obj->oo_db == NULL)
890                         return 0;
891                 idc->oic_dnode = obj->oo_db->db_object;
892                 return 0;
893         }
894
895         /* new mapping is needed */
896         idc = osd_idc_add(env, osd, fid);
897         if (IS_ERR(idc))
898                 return PTR_ERR(idc);
899
900         if (obj->oo_db)
901                 idc->oic_dnode = obj->oo_db->db_object;
902
903         return 0;
904 }