1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
42 * Author: Nikita Danilov <nikita@clusterfs.com>
46 * oi uses two mechanisms to implement fid->cookie mapping:
48 * - persistent index, where cookie is a record and fid is a key, and
50 * - algorithmic mapping for "igif" fids.
55 # define EXPORT_SYMTAB
57 #define DEBUG_SUBSYSTEM S_MDS
59 #include <linux/module.h>
61 /* LUSTRE_VERSION_CODE */
62 #include <lustre_ver.h>
64 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
71 #include <lustre_fid.h>
74 /* osd_lookup(), struct osd_thread_info */
75 #include "osd_internal.h"
77 #include "dt_object.h"
79 #define OSD_OI_FID_NR (1UL << OSD_OI_FID_OID_BITS)
80 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
82 static unsigned int osd_oi_count = OSD_OI_FID_NR;
83 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
84 "Number of Object Index containers to be created, "
85 "it's only valid for new filesystem.");
87 /** to serialize concurrent OI index initialization */
88 static cfs_mutex_t oi_init_lock;
90 static struct dt_index_features oi_feat = {
91 .dif_flags = DT_IND_UPDATE,
92 .dif_recsize_min = sizeof(struct osd_inode_id),
93 .dif_recsize_max = sizeof(struct osd_inode_id),
97 #define OSD_OI_NAME_BASE "oi.16"
100 * Open an OI(Ojbect Index) container.
102 * \param name Name of OI container
103 * \param objp Pointer of returned OI
106 * \retval -ve failure
109 osd_oi_open(struct osd_thread_info *info,
110 struct dt_device *dev, char *name, struct dt_object **objp)
112 const struct lu_env *env = info->oti_env;
113 struct dt_object *obj;
116 obj = dt_store_open(env, dev, "", name, &info->oti_fid);
120 oi_feat.dif_keysize_min = sizeof(info->oti_fid);
121 oi_feat.dif_keysize_max = sizeof(info->oti_fid);
123 rc = obj->do_ops->do_index_try(env, obj, &oi_feat);
125 lu_object_put(info->oti_env, &obj->do_lu);
126 CERROR("%s: wrong index %s: rc = %d\n",
127 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
137 osd_oi_table_put(struct osd_thread_info *info,
138 struct osd_oi *oi_table, unsigned oi_count)
142 for (i = 0; i < oi_count; i++) {
143 LASSERT(oi_table[i].oi_dir != NULL);
145 lu_object_put(info->oti_env, &oi_table[i].oi_dir->do_lu);
146 oi_table[i].oi_dir = NULL;
151 * Open OI(Object Index) table.
152 * If \a oi_count is zero, which means caller doesn't know how many OIs there
153 * will be, this function can either return 0 for new filesystem, or number
154 * of OIs on existed filesystem.
156 * If \a oi_count is non-zero, which means caller does know number of OIs on
157 * filesystem, this function should return the exactly same number on
158 * success, or error code in failure.
160 * \param oi_count Number of expected OI containers
161 * \param try_all Try to open all OIs even see failures
163 * \retval +ve number of opened OI containers
164 * \retval 0 no OI containers found
165 * \retval -ve failure
168 osd_oi_table_open(struct osd_thread_info *info, struct dt_device *dev,
169 struct osd_oi *oi_table, unsigned oi_count, int try_all)
175 /* NB: oi_count != 0 means that we have already created/known all OIs
176 * and have known exact number of OIs. */
177 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
179 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
182 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
183 rc = osd_oi_open(info, dev, name, &oi_table[i].oi_dir);
192 if (rc == -ENOENT && oi_count == 0)
195 CERROR("%s: can't open %s: rc = %d\n",
196 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
199 CERROR("%s: expect to open total %d OI files.\n",
200 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
210 osd_oi_table_put(info, oi_table, count);
217 static int osd_oi_table_create(struct osd_thread_info *info,
218 struct dt_device *dev,
219 struct md_device *mdev, int oi_count)
221 const struct lu_env *env;
222 struct md_object *mdo;
226 for (i = 0; i < oi_count; ++i) {
229 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
231 lu_local_obj_fid(&info->oti_fid, OSD_OI_FID_OID_FIRST + i);
232 oi_feat.dif_keysize_min = sizeof(info->oti_fid);
233 oi_feat.dif_keysize_max = sizeof(info->oti_fid);
235 mdo = llo_store_create_index(env, mdev, dev, "", name,
236 &info->oti_fid, &oi_feat);
238 CERROR("Failed to create OI[%d] on %s: %d\n",
239 i, dev->dd_lu_dev.ld_obd->obd_name,
241 RETURN(PTR_ERR(mdo));
244 lu_object_put(env, &mdo->mo_lu);
249 int osd_oi_init(struct osd_thread_info *info,
250 struct osd_oi **oi_table,
251 struct dt_device *dev,
252 struct md_device *mdev)
257 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
261 cfs_mutex_lock(&oi_init_lock);
263 rc = osd_oi_table_open(info, dev, oi, 0, 0);
267 rc = osd_oi_open(info, dev, OSD_OI_NAME_BASE, &oi[0].oi_dir);
268 if (rc == 0) { /* found single OI from old filesystem */
274 CERROR("%s: can't open %s: rc = %d\n",
275 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
279 /* create OI objects */
280 rc = osd_oi_table_create(info, dev, mdev, osd_oi_count);
284 rc = osd_oi_table_open(info, dev, oi, osd_oi_count, 0);
285 LASSERT(rc == osd_oi_count || rc < 0);
289 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
291 LASSERT((rc & (rc - 1)) == 0);
295 cfs_mutex_unlock(&oi_init_lock);
299 void osd_oi_fini(struct osd_thread_info *info,
300 struct osd_oi **oi_table, unsigned oi_count)
302 struct osd_oi *oi = *oi_table;
304 osd_oi_table_put(info, oi, oi_count);
306 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
310 int osd_oi_lookup(struct osd_thread_info *info, struct osd_oi *oi,
311 const struct lu_fid *fid, struct osd_inode_id *id)
313 struct lu_fid *oi_fid = &info->oti_fid;
316 if (osd_fid_is_igif(fid)) {
317 lu_igif_to_id(fid, id);
320 struct dt_object *idx;
321 const struct dt_key *key;
323 if (!fid_is_norm(fid))
327 fid_cpu_to_be(oi_fid, fid);
328 key = (struct dt_key *) oi_fid;
329 rc = idx->do_index_ops->dio_lookup(info->oti_env, idx,
330 (struct dt_rec *)id, key,
333 id->oii_ino = be32_to_cpu(id->oii_ino);
334 id->oii_gen = be32_to_cpu(id->oii_gen);
342 int osd_oi_insert(struct osd_thread_info *info, struct osd_oi *oi,
343 const struct lu_fid *fid, const struct osd_inode_id *id0,
344 struct thandle *th, int ignore_quota)
346 struct lu_fid *oi_fid = &info->oti_fid;
347 struct dt_object *idx;
348 struct osd_inode_id *id;
349 const struct dt_key *key;
351 if (!fid_is_norm(fid))
355 fid_cpu_to_be(oi_fid, fid);
356 key = (struct dt_key *) oi_fid;
359 id->oii_ino = cpu_to_be32(id0->oii_ino);
360 id->oii_gen = cpu_to_be32(id0->oii_gen);
361 return idx->do_index_ops->dio_insert(info->oti_env, idx,
363 key, th, BYPASS_CAPA,
367 int osd_oi_delete(struct osd_thread_info *info,
368 struct osd_oi *oi, const struct lu_fid *fid,
371 struct lu_fid *oi_fid = &info->oti_fid;
372 struct dt_object *idx;
373 const struct dt_key *key;
375 if (!fid_is_norm(fid))
379 fid_cpu_to_be(oi_fid, fid);
380 key = (struct dt_key *) oi_fid;
381 return idx->do_index_ops->dio_delete(info->oti_env, idx,
382 key, th, BYPASS_CAPA);
385 int osd_oi_mod_init()
387 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
388 osd_oi_count = OSD_OI_FID_NR;
390 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
391 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
392 osd_oi_count, size_roundup_power2(osd_oi_count));
393 osd_oi_count = size_roundup_power2(osd_oi_count);
396 cfs_mutex_init(&oi_init_lock);