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, 2015, 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_OSD
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->d_name, &de, NULL, 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 = osd_journal_start_sb(sb, LDISKFS_HT_MISC, 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 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
143 unlock_new_inode(inode);
145 if (feat->dif_flags & DT_IND_VARKEY)
146 rc = iam_lvar_create(inode, feat->dif_keysize_max,
147 feat->dif_ptrsize, feat->dif_recsize_max,
150 rc = iam_lfix_create(inode, feat->dif_keysize_max,
151 feat->dif_ptrsize, feat->dif_recsize_max,
153 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
154 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
155 ldiskfs_journal_stop(jh);
160 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
161 struct osd_device *osd,
163 struct dt_index_features *f,
166 struct dentry *dentry;
170 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
172 return (void *) dentry;
174 if (dentry->d_inode) {
175 LASSERT(!is_bad_inode(dentry->d_inode));
176 inode = dentry->d_inode;
177 atomic_inc(&inode->i_count);
184 shrink_dcache_parent(osd_sb(osd)->s_root);
186 return ERR_PTR(-ENOENT);
188 rc = osd_oi_index_create_one(info, osd, name, f);
192 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
194 return (void *) dentry;
196 if (dentry->d_inode) {
197 LASSERT(!is_bad_inode(dentry->d_inode));
198 inode = dentry->d_inode;
199 atomic_inc(&inode->i_count);
204 return ERR_PTR(-ENOENT);
208 * Open an OI(Ojbect Index) container.
210 * \param name Name of OI container
211 * \param objp Pointer of returned OI
214 * \retval -ve failure
216 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
217 char *name, struct osd_oi **oi_slot, bool create)
219 struct osd_directory *dir;
220 struct iam_container *bag;
227 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
228 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
230 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
232 RETURN(PTR_ERR(inode));
234 /* 'What the @fid is' is not imporatant, because these objects
235 * have no OI mappings, and only are visible inside the OSD.*/
236 lu_igif_build(&info->oti_fid, inode->i_ino, inode->i_generation);
237 rc = osd_ea_fid_set(info, inode, &info->oti_fid, LMAC_NOT_IN_OI, 0);
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 static int osd_remove_oi_one(struct dentry *parent, const char *name,
344 struct dentry *child;
347 child = ll_lookup_one_len(name, parent, namelen);
351 rc = ll_vfs_unlink(parent->d_inode, child);
355 return rc == -ENOENT ? 0 : rc;
358 static int osd_remove_ois(struct osd_thread_info *info, struct osd_device *osd)
365 for (i = 0; i < osd->od_scrub.os_file.sf_oi_count; i++) {
366 namelen = snprintf(name, sizeof(name), "%s.%d",
367 OSD_OI_NAME_BASE, i);
368 rc = osd_remove_oi_one(osd_sb(osd)->s_root, name, namelen);
370 CERROR("%.16s: fail to remove the stale OI file %s: "
372 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
378 namelen = snprintf(name, sizeof(name), "%s", OSD_OI_NAME_BASE);
379 rc = osd_remove_oi_one(osd_sb(osd)->s_root, name, namelen);
381 CERROR("%.16s: fail to remove the stale OI file %s: rc = %d\n",
382 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
387 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd,
390 struct osd_scrub *scrub = &osd->od_scrub;
391 struct scrub_file *sf = &scrub->os_file;
397 rc = osd_remove_ois(info, osd);
402 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
406 mutex_lock(&oi_init_lock);
407 /* try to open existing multiple OIs first */
408 rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
413 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
416 osd_scrub_file_reset(scrub,
417 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
419 osd_oi_count = sf->sf_oi_count;
423 /* if previous failed then try found single OI from old filesystem */
424 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
425 if (rc == 0) { /* found single OI from old filesystem */
426 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
427 if (sf->sf_success_count == 0)
428 /* XXX: There is one corner case that if the OI_scrub
429 * file crashed or lost and we regard it upgrade,
430 * then we allow IGIF lookup to bypass OI files.
432 * The risk is that osd_fid_lookup() may found
433 * a wrong inode with the given IGIF especially
434 * when the MDT has performed file-level backup
435 * and restored after former upgrading from 1.8
436 * to 2.x. Fortunately, the osd_fid_lookup()can
437 * verify the inode to decrease the risk. */
438 osd_scrub_file_reset(scrub,
439 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
442 } else if (rc != -ENOENT) {
443 CERROR("%.16s: can't open %s: rc = %d\n",
444 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
445 OSD_OI_NAME_BASE, rc);
449 if (sf->sf_oi_count > 0) {
452 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
453 for (i = 0; i < osd_oi_count; i++)
454 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
455 osd_scrub_file_reset(scrub,
456 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
459 sf->sf_oi_count = osd_oi_count;
462 rc = osd_scrub_file_store(scrub);
464 osd_oi_table_put(info, oi, sf->sf_oi_count);
468 /* No OIs exist, new filesystem, create OI objects */
469 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
470 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
476 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
478 LASSERT((rc & (rc - 1)) == 0);
479 osd->od_oi_table = oi;
480 osd->od_oi_count = rc;
481 if (sf->sf_oi_count != rc) {
482 sf->sf_oi_count = rc;
483 rc = osd_scrub_file_store(scrub);
485 osd_oi_table_put(info, oi, sf->sf_oi_count);
486 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
493 mutex_unlock(&oi_init_lock);
497 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
499 if (unlikely(osd->od_oi_table == NULL))
502 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
504 OBD_FREE(osd->od_oi_table,
505 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
506 osd->od_oi_table = NULL;
509 static inline int fid_is_fs_root(const struct lu_fid *fid)
511 /* Map root inode to special local object FID */
512 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
513 fid_oid(fid) == OSD_FS_ROOT_OID));
516 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
517 struct osd_oi *oi, struct dt_rec *rec,
518 const struct dt_key *key)
520 struct iam_container *bag;
521 struct iam_iterator *it = &oti->oti_idx_it;
522 struct iam_path_descr *ipd;
527 LASSERT(oi->oi_inode);
529 bag = &oi->oi_dir.od_container;
530 ipd = osd_idx_ipd_get(oti->oti_env, bag);
534 /* got ipd now we can start iterator. */
535 iam_it_init(it, bag, 0, ipd);
537 rc = iam_it_get(it, (struct iam_key *)key);
539 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
542 osd_ipd_put(oti->oti_env, bag, ipd);
544 LINVRNT(osd_invariant(obj));
549 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
550 const struct lu_fid *fid, enum oi_check_flags flags)
552 struct lu_seq_range *range = &info->oti_seq_range;
556 if (flags & OI_KNOWN_ON_OST)
559 if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
560 fid_is_llog(fid)) || fid_is_name_llog(fid) ||
564 if (fid_is_idif(fid) || fid_is_last_id(fid))
567 if (!(flags & OI_CHECK_FLD))
570 if (osd_seq_site(osd)->ss_server_fld == NULL)
573 rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
576 CERROR("%s: lookup FLD "DFID": rc = %d\n",
577 osd_name(osd), PFID(fid), rc);
581 if (fld_range_is_ost(range))
587 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
588 const struct lu_fid *fid, struct osd_inode_id *id)
590 struct lu_fid *oi_fid = &info->oti_fid2;
593 fid_cpu_to_be(oi_fid, fid);
594 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
595 (const struct dt_key *)oi_fid);
597 osd_id_unpack(id, id);
599 } else if (rc == 0) {
605 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
606 const struct lu_fid *fid, struct osd_inode_id *id,
607 enum oi_check_flags flags)
609 if (unlikely(fid_is_last_id(fid)))
610 return osd_obj_spec_lookup(info, osd, fid, id);
612 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
613 return osd_obj_map_lookup(info, osd, fid, id);
616 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
618 if (fid_is_fs_root(fid)) {
619 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
620 osd_sb(osd)->s_root->d_inode->i_generation);
623 if (unlikely(fid_is_acct(fid)))
624 return osd_acct_obj_lookup(info, osd, fid, id);
626 /* For other special FIDs, try OI first, then do spec lookup */
627 rc = __osd_oi_lookup(info, osd, fid, id);
629 return osd_obj_spec_lookup(info, osd, fid, id);
633 if (!osd->od_igif_inoi && fid_is_igif(fid)) {
634 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
638 return __osd_oi_lookup(info, osd, fid, id);
641 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
642 const struct dt_rec *rec, const struct dt_key *key,
643 handle_t *th, bool insert)
645 struct iam_container *bag;
646 struct iam_path_descr *ipd;
651 LASSERT(oi->oi_inode);
652 ll_vfs_dq_init(oi->oi_inode);
654 bag = &oi->oi_dir.od_container;
655 ipd = osd_idx_ipd_get(oti->oti_env, bag);
656 if (unlikely(ipd == NULL))
660 LASSERT(th->h_transaction != NULL);
662 rc = iam_insert(th, bag, (const struct iam_key *)key,
663 (const struct iam_rec *)rec, ipd);
665 rc = iam_update(th, bag, (const struct iam_key *)key,
666 (const struct iam_rec *)rec, ipd);
667 osd_ipd_put(oti->oti_env, bag, ipd);
668 LINVRNT(osd_invariant(obj));
672 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
673 const struct lu_fid *fid, const struct osd_inode_id *id,
674 handle_t *th, enum oi_check_flags flags)
676 struct lu_fid *oi_fid = &info->oti_fid2;
677 struct osd_inode_id *oi_id = &info->oti_id2;
680 if (unlikely(fid_is_last_id(fid)))
681 return osd_obj_spec_insert(info, osd, fid, id, th);
683 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
684 return osd_obj_map_insert(info, osd, fid, id, th);
686 fid_cpu_to_be(oi_fid, fid);
687 osd_id_pack(oi_id, id);
688 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
689 (const struct dt_rec *)oi_id,
690 (const struct dt_key *)oi_fid, th, true);
693 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
698 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
702 if (unlikely(osd_id_eq(id, oi_id)))
705 /* Check whether the mapping for oi_id is valid or not. */
706 inode = osd_iget(info, osd, oi_id);
709 if (rc == -ENOENT || rc == -ESTALE)
714 /* The EA inode should NOT be in OI, old OI scrub may added
715 * such OI mapping by wrong, replace it. */
716 if (unlikely(osd_is_ea_inode(inode))) {
721 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
729 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
730 lu_fid_eq(fid, &lma->lma_self_fid)) {
731 CERROR("%.16s: the FID "DFID" is used by two objects: "
733 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
734 PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
735 id->oii_ino, id->oii_gen);
740 osd_id_pack(oi_id, id);
741 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
742 (const struct dt_rec *)oi_id,
743 (const struct dt_key *)oi_fid, th, false);
748 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
749 rc = osd_obj_spec_insert(info, osd, fid, id, th);
753 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
754 const struct dt_key *key, handle_t *th)
756 struct iam_container *bag;
757 struct iam_path_descr *ipd;
762 LASSERT(oi->oi_inode);
763 ll_vfs_dq_init(oi->oi_inode);
765 bag = &oi->oi_dir.od_container;
766 ipd = osd_idx_ipd_get(oti->oti_env, bag);
767 if (unlikely(ipd == NULL))
771 LASSERT(th->h_transaction != NULL);
773 rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
774 osd_ipd_put(oti->oti_env, bag, ipd);
775 LINVRNT(osd_invariant(obj));
779 int osd_oi_delete(struct osd_thread_info *info,
780 struct osd_device *osd, const struct lu_fid *fid,
781 handle_t *th, enum oi_check_flags flags)
783 struct lu_fid *oi_fid = &info->oti_fid2;
785 /* clear idmap cache */
786 if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
787 fid_zero(&info->oti_cache.oic_fid);
789 if (fid_is_last_id(fid))
792 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
793 return osd_obj_map_delete(info, osd, fid, th);
795 fid_cpu_to_be(oi_fid, fid);
796 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
797 (const struct dt_key *)oi_fid, th);
800 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
801 const struct lu_fid *fid, const struct osd_inode_id *id,
802 handle_t *th, enum oi_check_flags flags)
804 struct lu_fid *oi_fid = &info->oti_fid2;
805 struct osd_inode_id *oi_id = &info->oti_id2;
808 if (unlikely(fid_is_last_id(fid)))
809 return osd_obj_spec_update(info, osd, fid, id, th);
811 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
812 return osd_obj_map_update(info, osd, fid, id, th);
814 fid_cpu_to_be(oi_fid, fid);
815 osd_id_pack(oi_id, id);
816 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
817 (const struct dt_rec *)oi_id,
818 (const struct dt_key *)oi_fid, th, false);
822 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
823 rc = osd_obj_spec_update(info, osd, fid, id, th);
827 int osd_oi_mod_init(void)
829 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
830 osd_oi_count = OSD_OI_FID_NR;
832 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
833 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
834 osd_oi_count, size_roundup_power2(osd_oi_count));
835 osd_oi_count = size_roundup_power2(osd_oi_count);
838 mutex_init(&oi_init_lock);