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