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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * oi uses two mechanisms to implement fid->cookie mapping:
46 * - persistent index, where cookie is a record and fid is a key, and
48 * - algorithmic mapping for "igif" fids.
53 # define EXPORT_SYMTAB
55 #define DEBUG_SUBSYSTEM S_MDS
57 #include <linux/module.h>
59 /* LUSTRE_VERSION_CODE */
60 #include <lustre_ver.h>
62 * struct OBD_{ALLOC,FREE}*()
66 #include <obd_support.h>
69 #include <lustre_fid.h>
72 /* osd_lookup(), struct osd_thread_info */
73 #include "osd_internal.h"
75 #include "dt_object.h"
77 #define OSD_OI_FID_NR (1UL << OSD_OI_FID_OID_BITS)
78 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
80 static unsigned int osd_oi_num = OSD_OI_FID_NR;
81 CFS_MODULE_PARM(osd_oi_num, "i", int, 0444,
82 "Number of Object Index containers to be created, "
83 "it's only valid for new filesystem.");
85 /** to serialize concurrent OI index initialization */
86 static cfs_mutex_t oi_init_lock;
88 static struct dt_index_features oi_feat = {
89 .dif_flags = DT_IND_UPDATE,
90 .dif_recsize_min = sizeof(struct osd_inode_id),
91 .dif_recsize_max = sizeof(struct osd_inode_id),
95 #define OSD_OI_NAME_BASE "oi.16"
98 * Open an OI(Ojbect Index) container.
100 * \param name Name of OI container
101 * \param objp Pointer of returned OI
104 * \retval -ve failure
107 osd_oi_open(struct osd_thread_info *info,
108 struct dt_device *dev, char *name, struct dt_object **objp)
110 const struct lu_env *env = info->oti_env;
111 struct dt_object *obj;
114 obj = dt_store_open(env, dev, "", name, &info->oti_fid);
118 oi_feat.dif_keysize_min = sizeof(info->oti_fid);
119 oi_feat.dif_keysize_max = sizeof(info->oti_fid);
121 rc = obj->do_ops->do_index_try(env, obj, &oi_feat);
123 lu_object_put(info->oti_env, &obj->do_lu);
124 CERROR("%s: wrong index %s: rc = %d\n",
125 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
135 osd_oi_table_put(struct osd_thread_info *info,
136 struct osd_oi *oi_table, unsigned oi_count)
140 for (i = 0; i < oi_count; i++) {
141 LASSERT(oi_table[i].oi_dir != NULL);
143 lu_object_put(info->oti_env, &oi_table[i].oi_dir->do_lu);
144 oi_table[i].oi_dir = NULL;
149 * Open OI(Object Index) table.
150 * If \a oi_count is zero, which means caller doesn't know how many OIs there
151 * will be, this function can either return 0 for new filesystem, or number
152 * of OIs on existed filesystem.
154 * If \a oi_count is non-zero, which means caller does know number of OIs on
155 * filesystem, this function should return the exactly same number on
156 * success, or error code in failure.
158 * \param oi_count Number of expected OI containers
159 * \param try_all Try to open all OIs even see failures
161 * \retval +ve number of opened OI containers
162 * \retval 0 no OI containers found
163 * \retval -ve failure
166 osd_oi_table_open(struct osd_thread_info *info, struct dt_device *dev,
167 struct osd_oi *oi_table, unsigned oi_count, int try_all)
173 /* NB: oi_count != 0 means that we have already created/known all OIs
174 * and have known exact number of OIs. */
175 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
177 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
180 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
181 rc = osd_oi_open(info, dev, name, &oi_table[i].oi_dir);
190 if (rc == -ENOENT && oi_count == 0)
193 CERROR("%s: can't open %s: rc = %d\n",
194 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
197 CERROR("%s: expect to open total %d OI files.\n",
198 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
208 osd_oi_table_put(info, oi_table, count);
215 static int osd_oi_table_create(struct osd_thread_info *info,
216 struct dt_device *dev,
217 struct md_device *mdev, int oi_count)
219 const struct lu_env *env;
220 struct md_object *mdo;
224 for (i = 0; i < oi_count; ++i) {
227 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
229 lu_local_obj_fid(&info->oti_fid, OSD_OI_FID_OID_FIRST + i);
230 oi_feat.dif_keysize_min = sizeof(info->oti_fid);
231 oi_feat.dif_keysize_max = sizeof(info->oti_fid);
233 mdo = llo_store_create_index(env, mdev, dev, "", name,
234 &info->oti_fid, &oi_feat);
236 CERROR("Failed to create OI[%d] on %s: %d\n",
237 i, dev->dd_lu_dev.ld_obd->obd_name,
239 RETURN(PTR_ERR(mdo));
242 lu_object_put(env, &mdo->mo_lu);
247 int osd_oi_init(struct osd_thread_info *info,
248 struct osd_oi **oi_table,
249 struct dt_device *dev,
250 struct md_device *mdev)
255 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
259 cfs_mutex_lock(&oi_init_lock);
261 rc = osd_oi_table_open(info, dev, oi, 0, 0);
265 rc = osd_oi_open(info, dev, OSD_OI_NAME_BASE, &oi[0].oi_dir);
266 if (rc == 0) { /* found single OI from old filesystem */
272 CERROR("%s: can't open %s: rc = %d\n",
273 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
277 /* create OI objects */
278 rc = osd_oi_table_create(info, dev, mdev, osd_oi_num);
282 rc = osd_oi_table_open(info, dev, oi, osd_oi_num, 0);
283 LASSERT(rc == osd_oi_num || rc < 0);
287 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
291 cfs_mutex_unlock(&oi_init_lock);
295 void osd_oi_fini(struct osd_thread_info *info,
296 struct osd_oi **oi_table, unsigned oi_count)
298 struct osd_oi *oi = *oi_table;
300 osd_oi_table_put(info, oi, oi_count);
302 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
306 int osd_oi_lookup(struct osd_thread_info *info, struct osd_oi *oi,
307 const struct lu_fid *fid, struct osd_inode_id *id)
309 struct lu_fid *oi_fid = &info->oti_fid;
312 if (osd_fid_is_igif(fid)) {
313 lu_igif_to_id(fid, id);
316 struct dt_object *idx;
317 const struct dt_key *key;
319 if (!fid_is_norm(fid))
323 fid_cpu_to_be(oi_fid, fid);
324 key = (struct dt_key *) oi_fid;
325 rc = idx->do_index_ops->dio_lookup(info->oti_env, idx,
326 (struct dt_rec *)id, key,
329 id->oii_ino = be32_to_cpu(id->oii_ino);
330 id->oii_gen = be32_to_cpu(id->oii_gen);
338 int osd_oi_insert(struct osd_thread_info *info, struct osd_oi *oi,
339 const struct lu_fid *fid, const struct osd_inode_id *id0,
340 struct thandle *th, int ignore_quota)
342 struct lu_fid *oi_fid = &info->oti_fid;
343 struct dt_object *idx;
344 struct osd_inode_id *id;
345 const struct dt_key *key;
347 if (!fid_is_norm(fid))
351 fid_cpu_to_be(oi_fid, fid);
352 key = (struct dt_key *) oi_fid;
355 id->oii_ino = cpu_to_be32(id0->oii_ino);
356 id->oii_gen = cpu_to_be32(id0->oii_gen);
357 return idx->do_index_ops->dio_insert(info->oti_env, idx,
359 key, th, BYPASS_CAPA,
363 int osd_oi_delete(struct osd_thread_info *info,
364 struct osd_oi *oi, const struct lu_fid *fid,
367 struct lu_fid *oi_fid = &info->oti_fid;
368 struct dt_object *idx;
369 const struct dt_key *key;
371 if (!fid_is_norm(fid))
375 fid_cpu_to_be(oi_fid, fid);
376 key = (struct dt_key *) oi_fid;
377 return idx->do_index_ops->dio_delete(info->oti_env, idx,
378 key, th, BYPASS_CAPA);
381 int osd_oi_mod_init()
383 if (osd_oi_num == 0 || osd_oi_num > OSD_OI_FID_NR_MAX)
384 osd_oi_num = OSD_OI_FID_NR;
386 cfs_mutex_init(&oi_init_lock);