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) 2012, 2013, 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>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <linux/module.h>
47 /* LUSTRE_VERSION_CODE */
48 #include <lustre_ver.h>
50 * struct OBD_{ALLOC,FREE}*()
54 #include <obd_support.h>
57 #include <lustre_fid.h>
58 #include <dt_object.h>
61 /* osd_lookup(), struct osd_thread_info */
62 #include "osd_internal.h"
63 #include "osd_scrub.h"
65 static unsigned int osd_oi_count = OSD_OI_FID_NR;
66 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
67 "Number of Object Index containers to be created, "
68 "it's only valid for new filesystem.");
70 /** to serialize concurrent OI index initialization */
71 static struct mutex oi_init_lock;
73 static struct dt_index_features oi_feat = {
74 .dif_flags = DT_IND_UPDATE,
75 .dif_recsize_min = sizeof(struct osd_inode_id),
76 .dif_recsize_max = sizeof(struct osd_inode_id),
80 #define OSD_OI_NAME_BASE "oi.16"
82 static void osd_oi_table_put(struct osd_thread_info *info,
83 struct osd_oi **oi_table, unsigned oi_count)
85 struct iam_container *bag;
88 for (i = 0; i < oi_count; i++) {
89 if (oi_table[i] == NULL)
92 LASSERT(oi_table[i]->oi_inode != NULL);
94 bag = &(oi_table[i]->oi_dir.od_container);
95 if (bag->ic_object == oi_table[i]->oi_inode)
96 iam_container_fini(bag);
97 iput(oi_table[i]->oi_inode);
98 oi_table[i]->oi_inode = NULL;
99 OBD_FREE_PTR(oi_table[i]);
104 static int osd_oi_index_create_one(struct osd_thread_info *info,
105 struct osd_device *osd, const char *name,
106 struct dt_index_features *feat)
108 const struct lu_env *env = info->oti_env;
109 struct osd_inode_id *id = &info->oti_id;
110 struct buffer_head *bh;
112 struct ldiskfs_dir_entry_2 *de;
113 struct dentry *dentry;
114 struct super_block *sb = osd_sb(osd);
115 struct inode *dir = sb->s_root->d_inode;
119 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
120 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
122 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
124 inode = osd_iget(info, osd, id);
125 if (!IS_ERR(inode)) {
127 inode = ERR_PTR(-EEXIST);
129 return PTR_ERR(inode);
132 jh = ldiskfs_journal_start_sb(sb, 100);
136 inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
138 ldiskfs_journal_stop(jh);
139 return PTR_ERR(inode);
142 if (feat->dif_flags & DT_IND_VARKEY)
143 rc = iam_lvar_create(inode, feat->dif_keysize_max,
144 feat->dif_ptrsize, feat->dif_recsize_max,
147 rc = iam_lfix_create(inode, feat->dif_keysize_max,
148 feat->dif_ptrsize, feat->dif_recsize_max,
150 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
151 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
152 ldiskfs_journal_stop(jh);
157 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
158 struct osd_device *osd,
160 struct dt_index_features *f,
163 struct dentry *dentry;
167 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
169 return (void *) dentry;
171 if (dentry->d_inode) {
172 LASSERT(!is_bad_inode(dentry->d_inode));
173 inode = dentry->d_inode;
174 atomic_inc(&inode->i_count);
181 shrink_dcache_parent(osd_sb(osd)->s_root);
183 return ERR_PTR(-ENOENT);
185 rc = osd_oi_index_create_one(info, osd, name, f);
189 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
191 return (void *) dentry;
193 if (dentry->d_inode) {
194 LASSERT(!is_bad_inode(dentry->d_inode));
195 inode = dentry->d_inode;
196 atomic_inc(&inode->i_count);
201 return ERR_PTR(-ENOENT);
205 * Open an OI(Ojbect Index) container.
207 * \param name Name of OI container
208 * \param objp Pointer of returned OI
211 * \retval -ve failure
213 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
214 char *name, struct osd_oi **oi_slot, bool create)
216 struct osd_directory *dir;
217 struct iam_container *bag;
224 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
225 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
227 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
229 RETURN(PTR_ERR(inode));
231 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
232 /* 'What the @fid is' is not imporatant, because these objects
233 * have no OI mappings, and only are visible inside the OSD.*/
234 lu_igif_build(&info->oti_fid, inode->i_ino, inode->i_generation);
235 rc = osd_ea_fid_set(info, inode, &info->oti_fid, LMAC_NOT_IN_OI, 0);
241 GOTO(out_inode, rc = -ENOMEM);
243 oi->oi_inode = inode;
246 bag = &dir->od_container;
247 rc = iam_container_init(bag, &dir->od_descr, inode);
251 rc = iam_container_setup(bag);
253 GOTO(out_container, rc);
259 iam_container_fini(bag);
268 * Open OI(Object Index) table.
269 * If \a oi_count is zero, which means caller doesn't know how many OIs there
270 * will be, this function can either return 0 for new filesystem, or number
271 * of OIs on existed filesystem.
273 * If \a oi_count is non-zero, which means caller does know number of OIs on
274 * filesystem, this function should return the exactly same number on
275 * success, or error code in failure.
277 * \param oi_count Number of expected OI containers
278 * \param create Create OIs if doesn't exist
280 * \retval +ve number of opened OI containers
281 * \retval 0 no OI containers found
282 * \retval -ve failure
285 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
286 struct osd_oi **oi_table, unsigned oi_count, bool create)
288 struct scrub_file *sf = &osd->od_scrub.os_file;
294 /* NB: oi_count != 0 means that we have already created/known all OIs
295 * and have known exact number of OIs. */
296 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
298 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
301 if (oi_table[i] != NULL) {
306 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
307 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
313 if (rc == -ENOENT && create == false) {
318 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
322 CERROR("%.16s: can't open %s: rc = %d\n",
323 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
325 CERROR("%.16s: expect to open total %d OI files.\n",
326 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
332 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
339 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
341 struct osd_scrub *scrub = &osd->od_scrub;
342 struct scrub_file *sf = &scrub->os_file;
347 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
351 mutex_lock(&oi_init_lock);
352 /* try to open existing multiple OIs first */
353 rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
358 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
361 osd_scrub_file_reset(scrub,
362 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
364 osd_oi_count = sf->sf_oi_count;
368 /* if previous failed then try found single OI from old filesystem */
369 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
370 if (rc == 0) { /* found single OI from old filesystem */
371 if (sf->sf_success_count == 0)
372 /* XXX: There is one corner case that if the OI_scrub
373 * file crashed or lost and we regard it upgrade,
374 * then we allow IGIF lookup to bypass OI files.
376 * The risk is that osd_fid_lookup() may found
377 * a wrong inode with the given IGIF especially
378 * when the MDT has performed file-level backup
379 * and restored after former upgrading from 1.8
380 * to 2.x. Fortunately, the osd_fid_lookup()can
381 * verify the inode to decrease the risk. */
382 osd_scrub_file_reset(scrub,
383 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
386 } else if (rc != -ENOENT) {
387 CERROR("%.16s: can't open %s: rc = %d\n",
388 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
389 OSD_OI_NAME_BASE, rc);
393 if (sf->sf_oi_count > 0) {
396 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
397 for (i = 0; i < osd_oi_count; i++)
398 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
399 osd_scrub_file_reset(scrub,
400 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
403 sf->sf_oi_count = osd_oi_count;
406 rc = osd_scrub_file_store(scrub);
408 osd_oi_table_put(info, oi, sf->sf_oi_count);
412 /* No OIs exist, new filesystem, create OI objects */
413 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
414 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
420 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
422 LASSERT((rc & (rc - 1)) == 0);
423 osd->od_oi_table = oi;
424 osd->od_oi_count = rc;
425 if (sf->sf_oi_count != rc) {
426 sf->sf_oi_count = rc;
427 rc = osd_scrub_file_store(scrub);
429 osd_oi_table_put(info, oi, sf->sf_oi_count);
430 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
437 mutex_unlock(&oi_init_lock);
441 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
443 if (unlikely(osd->od_oi_table == NULL))
446 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
448 OBD_FREE(osd->od_oi_table,
449 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
450 osd->od_oi_table = NULL;
453 static inline int fid_is_fs_root(const struct lu_fid *fid)
455 /* Map root inode to special local object FID */
456 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
457 fid_oid(fid) == OSD_FS_ROOT_OID));
460 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
461 struct osd_oi *oi, struct dt_rec *rec,
462 const struct dt_key *key)
464 struct iam_container *bag;
465 struct iam_iterator *it = &oti->oti_idx_it;
466 struct iam_path_descr *ipd;
471 LASSERT(oi->oi_inode);
473 bag = &oi->oi_dir.od_container;
474 ipd = osd_idx_ipd_get(oti->oti_env, bag);
478 /* got ipd now we can start iterator. */
479 iam_it_init(it, bag, 0, ipd);
481 rc = iam_it_get(it, (struct iam_key *)key);
483 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
486 osd_ipd_put(oti->oti_env, bag, ipd);
488 LINVRNT(osd_invariant(obj));
493 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
494 const struct lu_fid *fid, enum oi_check_flags flags)
498 if (flags & OI_KNOWN_ON_OST)
501 if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
502 fid_is_llog(fid)) || fid_is_name_llog(fid) ||
506 if (fid_is_idif(fid) || fid_is_last_id(fid))
509 if (!(flags & OI_CHECK_FLD))
518 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
519 const struct lu_fid *fid, struct osd_inode_id *id)
521 struct lu_fid *oi_fid = &info->oti_fid2;
524 fid_cpu_to_be(oi_fid, fid);
525 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
526 (const struct dt_key *)oi_fid);
528 osd_id_unpack(id, id);
530 } else if (rc == 0) {
536 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
537 const struct lu_fid *fid, struct osd_inode_id *id,
538 enum oi_check_flags flags)
540 if (unlikely(fid_is_last_id(fid)))
541 return osd_obj_spec_lookup(info, osd, fid, id);
543 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
544 return osd_obj_map_lookup(info, osd, fid, id);
546 if (fid_is_fs_root(fid)) {
547 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
548 osd_sb(osd)->s_root->d_inode->i_generation);
552 if (unlikely(fid_is_acct(fid)))
553 return osd_acct_obj_lookup(info, osd, fid, id);
555 if (!osd->od_igif_inoi && fid_is_igif(fid)) {
556 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
560 return __osd_oi_lookup(info, osd, fid, id);
563 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
564 const struct dt_rec *rec, const struct dt_key *key,
565 struct thandle *th, bool insert)
567 struct iam_container *bag;
568 struct iam_path_descr *ipd;
569 struct osd_thandle *oh;
574 LASSERT(oi->oi_inode);
575 ll_vfs_dq_init(oi->oi_inode);
577 bag = &oi->oi_dir.od_container;
578 ipd = osd_idx_ipd_get(oti->oti_env, bag);
579 if (unlikely(ipd == NULL))
582 oh = container_of0(th, struct osd_thandle, ot_super);
583 LASSERT(oh->ot_handle != NULL);
584 LASSERT(oh->ot_handle->h_transaction != NULL);
586 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
587 (const struct iam_rec *)rec, ipd);
589 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)key,
590 (const struct iam_rec *)rec, ipd);
591 osd_ipd_put(oti->oti_env, bag, ipd);
592 LINVRNT(osd_invariant(obj));
596 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
597 const struct lu_fid *fid, const struct osd_inode_id *id,
598 struct thandle *th, enum oi_check_flags flags)
600 struct lu_fid *oi_fid = &info->oti_fid2;
601 struct osd_inode_id *oi_id = &info->oti_id2;
604 if (unlikely(fid_is_last_id(fid)))
605 return osd_obj_spec_insert(info, osd, fid, id, th);
607 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
608 return osd_obj_map_insert(info, osd, fid, id, th);
610 fid_cpu_to_be(oi_fid, fid);
611 osd_id_pack(oi_id, id);
612 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
613 (const struct dt_rec *)oi_id,
614 (const struct dt_key *)oi_fid, th, true);
617 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
622 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
626 if (unlikely(osd_id_eq(id, oi_id)))
629 /* Check whether the mapping for oi_id is valid or not. */
630 inode = osd_iget(info, osd, oi_id);
633 if (rc == -ENOENT || rc == -ESTALE)
638 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
646 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
647 lu_fid_eq(fid, &lma->lma_self_fid)) {
648 CERROR("%.16s: the FID "DFID" is used by two objects: "
650 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
651 PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
652 id->oii_ino, id->oii_gen);
657 osd_id_pack(oi_id, id);
658 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
659 (const struct dt_rec *)oi_id,
660 (const struct dt_key *)oi_fid, th, false);
665 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
666 rc = osd_obj_spec_insert(info, osd, fid, id, th);
670 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
671 const struct dt_key *key, struct thandle *handle)
673 struct iam_container *bag;
674 struct iam_path_descr *ipd;
675 struct osd_thandle *oh;
680 LASSERT(oi->oi_inode);
681 ll_vfs_dq_init(oi->oi_inode);
683 bag = &oi->oi_dir.od_container;
684 ipd = osd_idx_ipd_get(oti->oti_env, bag);
685 if (unlikely(ipd == NULL))
688 oh = container_of0(handle, struct osd_thandle, ot_super);
689 LASSERT(oh->ot_handle != NULL);
690 LASSERT(oh->ot_handle->h_transaction != NULL);
692 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
693 osd_ipd_put(oti->oti_env, bag, ipd);
694 LINVRNT(osd_invariant(obj));
698 int osd_oi_delete(struct osd_thread_info *info,
699 struct osd_device *osd, const struct lu_fid *fid,
700 struct thandle *th, enum oi_check_flags flags)
702 struct lu_fid *oi_fid = &info->oti_fid2;
704 /* clear idmap cache */
705 if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
706 fid_zero(&info->oti_cache.oic_fid);
708 if (fid_is_last_id(fid))
711 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
712 return osd_obj_map_delete(info, osd, fid, th);
714 fid_cpu_to_be(oi_fid, fid);
715 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
716 (const struct dt_key *)oi_fid, th);
719 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
720 const struct lu_fid *fid, const struct osd_inode_id *id,
721 struct thandle *th, enum oi_check_flags flags)
723 struct lu_fid *oi_fid = &info->oti_fid2;
724 struct osd_inode_id *oi_id = &info->oti_id2;
727 if (unlikely(fid_is_last_id(fid)))
728 return osd_obj_spec_update(info, osd, fid, id, th);
730 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
731 return osd_obj_map_update(info, osd, fid, id, th);
733 fid_cpu_to_be(oi_fid, fid);
734 osd_id_pack(oi_id, id);
735 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
736 (const struct dt_rec *)oi_id,
737 (const struct dt_key *)oi_fid, th, false);
741 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
742 rc = osd_obj_spec_update(info, osd, fid, id, th);
746 int osd_oi_mod_init(void)
748 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
749 osd_oi_count = OSD_OI_FID_NR;
751 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
752 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
753 osd_oi_count, size_roundup_power2(osd_oi_count));
754 osd_oi_count = size_roundup_power2(osd_oi_count);
757 mutex_init(&oi_init_lock);