4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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.
52 #define DEBUG_SUBSYSTEM S_MDS
54 #include <linux/module.h>
56 /* LUSTRE_VERSION_CODE */
57 #include <lustre_ver.h>
59 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
66 #include <lustre_fid.h>
67 #include <dt_object.h>
70 /* osd_lookup(), struct osd_thread_info */
71 #include "osd_internal.h"
72 #include "osd_scrub.h"
74 static unsigned int osd_oi_count = OSD_OI_FID_NR;
75 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
76 "Number of Object Index containers to be created, "
77 "it's only valid for new filesystem.");
79 /** to serialize concurrent OI index initialization */
80 static struct mutex oi_init_lock;
82 static struct dt_index_features oi_feat = {
83 .dif_flags = DT_IND_UPDATE,
84 .dif_recsize_min = sizeof(struct osd_inode_id),
85 .dif_recsize_max = sizeof(struct osd_inode_id),
89 #define OSD_OI_NAME_BASE "oi.16"
91 static void osd_oi_table_put(struct osd_thread_info *info,
92 struct osd_oi **oi_table, unsigned oi_count)
94 struct iam_container *bag;
97 for (i = 0; i < oi_count; i++) {
98 if (oi_table[i] == NULL)
101 LASSERT(oi_table[i]->oi_inode != NULL);
103 bag = &(oi_table[i]->oi_dir.od_container);
104 if (bag->ic_object == oi_table[i]->oi_inode)
105 iam_container_fini(bag);
106 iput(oi_table[i]->oi_inode);
107 oi_table[i]->oi_inode = NULL;
108 OBD_FREE_PTR(oi_table[i]);
113 static int osd_oi_index_create_one(struct osd_thread_info *info,
114 struct osd_device *osd, const char *name,
115 struct dt_index_features *feat)
117 const struct lu_env *env = info->oti_env;
118 struct osd_inode_id *id = &info->oti_id;
119 struct buffer_head *bh;
121 struct ldiskfs_dir_entry_2 *de;
122 struct dentry *dentry;
123 struct super_block *sb = osd_sb(osd);
124 struct inode *dir = sb->s_root->d_inode;
128 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
129 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
131 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
133 inode = osd_iget(info, osd, id);
134 if (!IS_ERR(inode)) {
136 inode = ERR_PTR(-EEXIST);
138 return PTR_ERR(inode);
141 jh = ldiskfs_journal_start_sb(sb, 100);
145 inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
147 ldiskfs_journal_stop(jh);
148 return PTR_ERR(inode);
151 if (feat->dif_flags & DT_IND_VARKEY)
152 rc = iam_lvar_create(inode, feat->dif_keysize_max,
153 feat->dif_ptrsize, feat->dif_recsize_max,
156 rc = iam_lfix_create(inode, feat->dif_keysize_max,
157 feat->dif_ptrsize, feat->dif_recsize_max,
159 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
160 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
161 ldiskfs_journal_stop(jh);
166 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
167 struct osd_device *osd,
169 struct dt_index_features *f,
172 struct dentry *dentry;
176 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
178 return (void *) dentry;
180 if (dentry->d_inode) {
181 LASSERT(!is_bad_inode(dentry->d_inode));
182 inode = dentry->d_inode;
183 atomic_inc(&inode->i_count);
190 shrink_dcache_parent(osd_sb(osd)->s_root);
192 return ERR_PTR(-ENOENT);
194 rc = osd_oi_index_create_one(info, osd, name, f);
198 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
200 return (void *) dentry;
202 if (dentry->d_inode) {
203 LASSERT(!is_bad_inode(dentry->d_inode));
204 inode = dentry->d_inode;
205 atomic_inc(&inode->i_count);
210 return ERR_PTR(-ENOENT);
214 * Open an OI(Ojbect Index) container.
216 * \param name Name of OI container
217 * \param objp Pointer of returned OI
220 * \retval -ve failure
222 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
223 char *name, struct osd_oi **oi_slot, bool create)
225 struct osd_directory *dir;
226 struct iam_container *bag;
233 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
234 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
236 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
238 RETURN(PTR_ERR(inode));
240 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
243 GOTO(out_inode, rc = -ENOMEM);
245 oi->oi_inode = inode;
248 bag = &dir->od_container;
249 rc = iam_container_init(bag, &dir->od_descr, inode);
253 rc = iam_container_setup(bag);
255 GOTO(out_container, rc);
261 iam_container_fini(bag);
270 * Open OI(Object Index) table.
271 * If \a oi_count is zero, which means caller doesn't know how many OIs there
272 * will be, this function can either return 0 for new filesystem, or number
273 * of OIs on existed filesystem.
275 * If \a oi_count is non-zero, which means caller does know number of OIs on
276 * filesystem, this function should return the exactly same number on
277 * success, or error code in failure.
279 * \param oi_count Number of expected OI containers
280 * \param create Create OIs if doesn't exist
282 * \retval +ve number of opened OI containers
283 * \retval 0 no OI containers found
284 * \retval -ve failure
287 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
288 struct osd_oi **oi_table, unsigned oi_count, bool create)
290 struct scrub_file *sf = &osd->od_scrub.os_file;
296 /* NB: oi_count != 0 means that we have already created/known all OIs
297 * and have known exact number of OIs. */
298 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
300 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
303 if (oi_table[i] != NULL) {
308 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
309 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
315 if (rc == -ENOENT && create == false) {
320 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
324 CERROR("%.16s: can't open %s: rc = %d\n",
325 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
327 CERROR("%.16s: expect to open total %d OI files.\n",
328 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
334 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
341 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
343 struct osd_scrub *scrub = &osd->od_scrub;
344 struct scrub_file *sf = &scrub->os_file;
349 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
353 mutex_lock(&oi_init_lock);
354 /* try to open existing multiple OIs first */
355 rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
360 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
363 osd_scrub_file_reset(scrub,
364 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
366 osd_oi_count = sf->sf_oi_count;
370 /* if previous failed then try found single OI from old filesystem */
371 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
372 if (rc == 0) { /* found single OI from old filesystem */
374 } else if (rc != -ENOENT) {
375 CERROR("%.16s: can't open %s: rc = %d\n",
376 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
377 OSD_OI_NAME_BASE, rc);
381 if (sf->sf_oi_count > 0) {
384 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
385 for (i = 0; i < osd_oi_count; i++)
386 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
387 osd_scrub_file_reset(scrub,
388 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
391 sf->sf_oi_count = osd_oi_count;
394 rc = osd_scrub_file_store(scrub);
396 osd_oi_table_put(info, oi, sf->sf_oi_count);
400 /* No OIs exist, new filesystem, create OI objects */
401 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
402 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
408 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
410 LASSERT((rc & (rc - 1)) == 0);
411 osd->od_oi_table = oi;
412 osd->od_oi_count = rc;
416 mutex_unlock(&oi_init_lock);
420 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
422 if (unlikely(osd->od_oi_table == NULL))
425 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
427 OBD_FREE(osd->od_oi_table,
428 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
429 osd->od_oi_table = NULL;
432 static inline int fid_is_fs_root(const struct lu_fid *fid)
434 /* Map root inode to special local object FID */
435 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
436 fid_oid(fid) == OSD_FS_ROOT_OID));
439 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
440 struct osd_oi *oi, struct dt_rec *rec,
441 const struct dt_key *key)
443 struct iam_container *bag;
444 struct iam_iterator *it = &oti->oti_idx_it;
445 struct iam_path_descr *ipd;
450 LASSERT(oi->oi_inode);
452 bag = &oi->oi_dir.od_container;
453 ipd = osd_idx_ipd_get(oti->oti_env, bag);
457 /* got ipd now we can start iterator. */
458 iam_it_init(it, bag, 0, ipd);
460 rc = iam_it_get(it, (struct iam_key *)key);
462 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
465 osd_ipd_put(oti->oti_env, bag, ipd);
467 LINVRNT(osd_invariant(obj));
472 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
473 const struct lu_fid *fid)
475 struct lu_seq_range *range = &info->oti_seq_range;
479 if (fid_is_idif(fid) || fid_is_last_id(fid))
482 rc = osd_fld_lookup(info->oti_env, osd, fid, range);
484 CERROR("%s: Can not lookup fld for "DFID"\n",
485 osd2lu_dev(osd)->ld_obd->obd_name, PFID(fid));
489 CDEBUG(D_INFO, "fid "DFID" range "DRANGE"\n", PFID(fid),
492 if (range->lsr_flags == LU_SEQ_RANGE_OST)
498 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
499 const struct lu_fid *fid, struct osd_inode_id *id)
501 struct lu_fid *oi_fid = &info->oti_fid2;
504 fid_cpu_to_be(oi_fid, fid);
505 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
506 (const struct dt_key *)oi_fid);
508 osd_id_unpack(id, id);
510 } else if (rc == 0) {
516 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
517 const struct lu_fid *fid, struct osd_inode_id *id,
522 if ((!fid_is_last_id(fid) && check_fld &&
523 fid_is_on_ost(info, osd, fid)) ||
526 /* FIXME: actually for all of the OST object */
527 rc = osd_obj_map_lookup(info, osd, fid, id);
528 } else if (fid_is_igif(fid)) {
529 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
530 } else if (fid_is_fs_root(fid)) {
531 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
532 osd_sb(osd)->s_root->d_inode->i_generation);
534 if (unlikely(fid_is_acct(fid)))
535 return osd_acct_obj_lookup(info, osd, fid, id);
536 else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE) ||
538 return osd_obj_spec_lookup(info, osd, fid, id);
540 rc = __osd_oi_lookup(info, osd, fid, id);
545 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
546 const struct dt_rec *rec, const struct dt_key *key,
549 struct iam_container *bag;
550 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
551 struct iam_path_descr *ipd;
552 struct osd_thandle *oh;
557 LASSERT(oi->oi_inode);
558 ll_vfs_dq_init(oi->oi_inode);
560 bag = &oi->oi_dir.od_container;
561 ipd = osd_idx_ipd_get(oti->oti_env, bag);
562 if (unlikely(ipd == NULL))
565 oh = container_of0(th, struct osd_thandle, ot_super);
566 LASSERT(oh->ot_handle != NULL);
567 LASSERT(oh->ot_handle->h_transaction != NULL);
568 if (S_ISDIR(oi->oi_inode->i_mode))
569 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
572 iam_rec = (struct iam_rec *) rec;
573 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
575 osd_ipd_put(oti->oti_env, bag, ipd);
576 LINVRNT(osd_invariant(obj));
580 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
581 const struct lu_fid *fid, const struct osd_inode_id *id,
584 struct lu_fid *oi_fid = &info->oti_fid2;
585 struct osd_inode_id *oi_id = &info->oti_id2;
587 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
590 if ((fid_is_on_ost(info, osd, fid) && !fid_is_last_id(fid)) ||
592 return osd_obj_map_insert(info, osd, fid, id, th);
594 /* Server mount should not depends on OI files */
595 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE) ||
597 return osd_obj_spec_insert(info, osd, fid, id, th);
599 fid_cpu_to_be(oi_fid, fid);
600 osd_id_pack(oi_id, id);
601 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
602 (const struct dt_rec *)oi_id,
603 (const struct dt_key *)oi_fid, th);
606 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
607 const struct dt_key *key, struct thandle *handle)
609 struct iam_container *bag;
610 struct iam_path_descr *ipd;
611 struct osd_thandle *oh;
616 LASSERT(oi->oi_inode);
617 ll_vfs_dq_init(oi->oi_inode);
619 bag = &oi->oi_dir.od_container;
620 ipd = osd_idx_ipd_get(oti->oti_env, bag);
621 if (unlikely(ipd == NULL))
624 oh = container_of0(handle, struct osd_thandle, ot_super);
625 LASSERT(oh->ot_handle != NULL);
626 LASSERT(oh->ot_handle->h_transaction != NULL);
628 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
629 osd_ipd_put(oti->oti_env, bag, ipd);
630 LINVRNT(osd_invariant(obj));
634 int osd_oi_delete(struct osd_thread_info *info,
635 struct osd_device *osd, const struct lu_fid *fid,
638 struct lu_fid *oi_fid = &info->oti_fid2;
640 if (fid_is_igif(fid) || fid_is_last_id(fid))
643 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
645 if (fid_is_on_ost(info, osd, fid) || fid_is_llog(fid))
646 return osd_obj_map_delete(info, osd, fid, th);
648 fid_cpu_to_be(oi_fid, fid);
649 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
650 (const struct dt_key *)oi_fid, th);
653 int osd_oi_mod_init(void)
655 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
656 osd_oi_count = OSD_OI_FID_NR;
658 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
659 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
660 osd_oi_count, size_roundup_power2(osd_oi_count));
661 osd_oi_count = size_roundup_power2(osd_oi_count);
664 mutex_init(&oi_init_lock);