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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Author: Nikita Danilov <nikita@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <linux/module.h>
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
46 * struct OBD_{ALLOC,FREE}*()
50 #include <obd_support.h>
53 #include <lustre_fid.h>
54 #include <dt_object.h>
57 /* osd_lookup(), struct osd_thread_info */
58 #include "osd_internal.h"
59 #include "osd_scrub.h"
61 static unsigned int osd_oi_count = OSD_OI_FID_NR;
62 module_param(osd_oi_count, int, 0444);
63 MODULE_PARM_DESC(osd_oi_count, "Number of Object Index containers to be created, it's only valid for new filesystem.");
65 /** to serialize concurrent OI index initialization */
66 static struct mutex oi_init_lock;
68 static struct dt_index_features oi_feat = {
69 .dif_flags = DT_IND_UPDATE,
70 .dif_recsize_min = sizeof(struct osd_inode_id),
71 .dif_recsize_max = sizeof(struct osd_inode_id),
75 #define OSD_OI_NAME_BASE "oi.16"
77 static void osd_oi_table_put(struct osd_thread_info *info,
78 struct osd_oi **oi_table, unsigned oi_count)
80 struct iam_container *bag;
83 for (i = 0; i < oi_count; i++) {
84 if (oi_table[i] == NULL)
87 LASSERT(oi_table[i]->oi_inode != NULL);
89 bag = &(oi_table[i]->oi_dir.od_container);
90 if (bag->ic_object == oi_table[i]->oi_inode)
91 iam_container_fini(bag);
92 iput(oi_table[i]->oi_inode);
93 oi_table[i]->oi_inode = NULL;
94 OBD_FREE_PTR(oi_table[i]);
99 static int osd_oi_index_create_one(struct osd_thread_info *info,
100 struct osd_device *osd, const char *name,
101 struct dt_index_features *feat)
103 const struct lu_env *env = info->oti_env;
104 struct osd_inode_id *id = &info->oti_id;
105 struct buffer_head *bh;
107 struct ldiskfs_dir_entry_2 *de;
108 struct dentry *dentry;
109 struct super_block *sb = osd_sb(osd);
110 struct inode *dir = sb->s_root->d_inode;
114 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
115 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, NULL);
117 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
119 inode = osd_iget(info, osd, id);
120 if (!IS_ERR(inode)) {
122 inode = ERR_PTR(-EEXIST);
124 return PTR_ERR(inode);
127 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, 100);
131 inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
133 ldiskfs_journal_stop(jh);
134 return PTR_ERR(inode);
137 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
138 unlock_new_inode(inode);
140 if (feat->dif_flags & DT_IND_VARKEY)
141 rc = iam_lvar_create(inode, feat->dif_keysize_max,
142 feat->dif_ptrsize, feat->dif_recsize_max,
145 rc = iam_lfix_create(inode, feat->dif_keysize_max,
146 feat->dif_ptrsize, feat->dif_recsize_max,
148 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
149 rc = osd_ldiskfs_add_entry(info, osd, jh, dentry, inode, NULL);
150 ldiskfs_journal_stop(jh);
155 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
156 struct osd_device *osd,
158 struct dt_index_features *f,
161 struct dentry *dentry;
165 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
167 return (void *) dentry;
169 if (dentry->d_inode) {
170 LASSERT(!is_bad_inode(dentry->d_inode));
171 inode = dentry->d_inode;
172 atomic_inc(&inode->i_count);
179 shrink_dcache_parent(osd_sb(osd)->s_root);
181 return ERR_PTR(-ENOENT);
183 rc = osd_oi_index_create_one(info, osd, name, f);
187 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
189 return (void *) dentry;
191 if (dentry->d_inode) {
192 LASSERT(!is_bad_inode(dentry->d_inode));
193 inode = dentry->d_inode;
194 atomic_inc(&inode->i_count);
199 return ERR_PTR(-ENOENT);
203 * Open an OI(Ojbect Index) container.
205 * \param name Name of OI container
206 * \param objp Pointer of returned OI
209 * \retval -ve failure
211 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
212 char *name, struct osd_oi **oi_slot, bool create)
214 struct osd_directory *dir;
215 struct iam_container *bag;
222 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
223 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
225 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
227 RETURN(PTR_ERR(inode));
229 /* 'What the @fid is' is not imporatant, because these objects
230 * have no OI mappings, and only are visible inside the OSD.*/
231 lu_igif_build(&info->oti_fid, inode->i_ino, inode->i_generation);
232 rc = osd_ea_fid_set(info, inode, &info->oti_fid, LMAC_NOT_IN_OI, 0);
238 GOTO(out_inode, rc = -ENOMEM);
240 oi->oi_inode = inode;
243 bag = &dir->od_container;
244 rc = iam_container_init(bag, &dir->od_descr, inode);
248 rc = iam_container_setup(bag);
250 GOTO(out_container, rc);
256 iam_container_fini(bag);
265 * Open OI(Object Index) table.
266 * If \a oi_count is zero, which means caller doesn't know how many OIs there
267 * will be, this function can either return 0 for new filesystem, or number
268 * of OIs on existed filesystem.
270 * If \a oi_count is non-zero, which means caller does know number of OIs on
271 * filesystem, this function should return the exactly same number on
272 * success, or error code in failure.
274 * \param oi_count Number of expected OI containers
275 * \param create Create OIs if doesn't exist
277 * \retval +ve number of opened OI containers
278 * \retval 0 no OI containers found
279 * \retval -ve failure
282 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
283 struct osd_oi **oi_table, unsigned oi_count, bool create)
285 struct scrub_file *sf = &osd->od_scrub.os_file;
291 /* NB: oi_count != 0 means that we have already created/known all OIs
292 * and have known exact number of OIs. */
293 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
295 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
298 if (oi_table[i] != NULL) {
303 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
304 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
310 if (rc == -ENOENT && create == false) {
315 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
319 CERROR("%s: can't open %s: rc = %d\n",
320 osd_dev2name(osd), name, rc);
322 CERROR("%s: expect to open total %d OI files.\n",
323 osd_dev2name(osd), oi_count);
328 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
335 static int osd_remove_oi_one(struct dentry *parent, const char *name,
338 struct dentry *child;
341 child = ll_lookup_one_len(name, parent, namelen);
345 rc = ll_vfs_unlink(parent->d_inode, child);
349 return rc == -ENOENT ? 0 : rc;
352 static int osd_remove_ois(struct osd_thread_info *info, struct osd_device *osd)
359 for (i = 0; i < osd->od_scrub.os_file.sf_oi_count; i++) {
360 namelen = snprintf(name, sizeof(name), "%s.%d",
361 OSD_OI_NAME_BASE, i);
362 rc = osd_remove_oi_one(osd_sb(osd)->s_root, name, namelen);
364 CERROR("%s: fail to remove the stale OI file %s: "
365 "rc = %d\n", osd_dev2name(osd), name, rc);
370 namelen = snprintf(name, sizeof(name), "%s", OSD_OI_NAME_BASE);
371 rc = osd_remove_oi_one(osd_sb(osd)->s_root, name, namelen);
373 CERROR("%s: fail to remove the stale OI file %s: rc = %d\n",
374 osd_dev2name(osd), name, rc);
379 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd,
382 struct osd_scrub *scrub = &osd->od_scrub;
383 struct scrub_file *sf = &scrub->os_file;
389 rc = osd_remove_ois(info, osd);
394 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
398 mutex_lock(&oi_init_lock);
399 /* try to open existing multiple OIs first */
400 rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
405 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
408 osd_scrub_file_reset(scrub,
409 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
411 osd_oi_count = sf->sf_oi_count;
415 /* if previous failed then try found single OI from old filesystem */
416 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
417 if (rc == 0) { /* found single OI from old filesystem */
418 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
419 if (sf->sf_success_count == 0)
420 /* XXX: There is one corner case that if the OI_scrub
421 * file crashed or lost and we regard it upgrade,
422 * then we allow IGIF lookup to bypass OI files.
424 * The risk is that osd_fid_lookup() may found
425 * a wrong inode with the given IGIF especially
426 * when the MDT has performed file-level backup
427 * and restored after former upgrading from 1.8
428 * to 2.x. Fortunately, the osd_fid_lookup()can
429 * verify the inode to decrease the risk. */
430 osd_scrub_file_reset(scrub,
431 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
434 } else if (rc != -ENOENT) {
435 CERROR("%s: can't open %s: rc = %d\n",
436 osd_dev2name(osd), OSD_OI_NAME_BASE, rc);
440 if (sf->sf_oi_count > 0) {
443 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
444 for (i = 0; i < osd_oi_count; i++)
445 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
446 osd_scrub_file_reset(scrub,
447 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
450 sf->sf_oi_count = osd_oi_count;
453 rc = osd_scrub_file_store(scrub);
455 osd_oi_table_put(info, oi, sf->sf_oi_count);
459 /* No OIs exist, new filesystem, create OI objects */
460 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
461 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
467 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
469 LASSERT((rc & (rc - 1)) == 0);
470 osd->od_oi_table = oi;
471 osd->od_oi_count = rc;
472 if (sf->sf_oi_count != rc) {
473 sf->sf_oi_count = rc;
474 rc = osd_scrub_file_store(scrub);
476 osd_oi_table_put(info, oi, sf->sf_oi_count);
477 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
484 mutex_unlock(&oi_init_lock);
488 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
490 if (unlikely(osd->od_oi_table == NULL))
493 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
495 OBD_FREE(osd->od_oi_table,
496 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
497 osd->od_oi_table = NULL;
500 static inline int fid_is_fs_root(const struct lu_fid *fid)
502 /* Map root inode to special local object FID */
503 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
504 fid_oid(fid) == OSD_FS_ROOT_OID));
507 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
508 struct osd_oi *oi, struct dt_rec *rec,
509 const struct dt_key *key)
511 struct iam_container *bag;
512 struct iam_iterator *it = &oti->oti_idx_it;
513 struct iam_path_descr *ipd;
518 LASSERT(oi->oi_inode);
520 bag = &oi->oi_dir.od_container;
521 ipd = osd_idx_ipd_get(oti->oti_env, bag);
525 /* got ipd now we can start iterator. */
526 iam_it_init(it, bag, 0, ipd);
528 rc = iam_it_get(it, (struct iam_key *)key);
530 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
533 osd_ipd_put(oti->oti_env, bag, ipd);
535 LINVRNT(osd_invariant(obj));
540 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
541 const struct lu_fid *fid, enum oi_check_flags flags)
543 struct lu_seq_range *range = &info->oti_seq_range;
547 if (flags & OI_KNOWN_ON_OST)
550 if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
551 fid_is_llog(fid)) || fid_is_name_llog(fid) ||
555 if (fid_is_idif(fid) || fid_is_last_id(fid))
558 if (!(flags & OI_CHECK_FLD))
561 if (osd_seq_site(osd)->ss_server_fld == NULL)
564 rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
566 /* During upgrade, OST FLDB might not be loaded because
567 * OST FLDB is not created until 2.6, so if some DNE
568 * filesystem upgrade from 2.5 to 2.7/2.8, they will
569 * not be able to find the sequence from local FLDB
570 * cache see fld_index_init(). */
571 if (rc == -ENOENT && osd->od_is_ost)
575 CERROR("%s: lookup FLD "DFID": rc = %d\n",
576 osd_name(osd), PFID(fid), rc);
580 if (fld_range_is_ost(range))
586 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
587 const struct lu_fid *fid, struct osd_inode_id *id)
589 struct lu_fid *oi_fid = &info->oti_fid2;
592 fid_cpu_to_be(oi_fid, fid);
593 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
594 (const struct dt_key *)oi_fid);
596 osd_id_unpack(id, id);
598 } else if (rc == 0) {
604 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
605 const struct lu_fid *fid, struct osd_inode_id *id,
606 enum oi_check_flags flags)
608 if (unlikely(fid_is_last_id(fid)))
609 return osd_obj_spec_lookup(info, osd, fid, id);
611 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
612 return osd_obj_map_lookup(info, osd, fid, id);
615 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
617 if (fid_is_fs_root(fid)) {
618 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
619 osd_sb(osd)->s_root->d_inode->i_generation);
622 if (unlikely(fid_is_acct(fid)))
623 return osd_acct_obj_lookup(info, osd, fid, id);
625 /* For other special FIDs, try OI first, then do spec lookup */
626 rc = __osd_oi_lookup(info, osd, fid, id);
628 return osd_obj_spec_lookup(info, osd, fid, id);
632 if (!osd->od_igif_inoi && fid_is_igif(fid)) {
633 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
637 return __osd_oi_lookup(info, osd, fid, id);
640 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
641 const struct dt_rec *rec, const struct dt_key *key,
642 handle_t *th, bool insert)
644 struct iam_container *bag;
645 struct iam_path_descr *ipd;
650 LASSERT(oi->oi_inode);
651 ll_vfs_dq_init(oi->oi_inode);
653 bag = &oi->oi_dir.od_container;
654 ipd = osd_idx_ipd_get(oti->oti_env, bag);
655 if (unlikely(ipd == NULL))
659 LASSERT(th->h_transaction != NULL);
661 rc = iam_insert(th, bag, (const struct iam_key *)key,
662 (const struct iam_rec *)rec, ipd);
664 rc = iam_update(th, bag, (const struct iam_key *)key,
665 (const struct iam_rec *)rec, ipd);
666 osd_ipd_put(oti->oti_env, bag, ipd);
667 LINVRNT(osd_invariant(obj));
671 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
672 const struct lu_fid *fid, const struct osd_inode_id *id,
673 handle_t *th, enum oi_check_flags flags, bool *exist)
675 struct lu_fid *oi_fid = &info->oti_fid2;
676 struct osd_inode_id *oi_id = &info->oti_id2;
679 if (unlikely(fid_is_last_id(fid)))
680 return osd_obj_spec_insert(info, osd, fid, id, th);
682 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
683 return osd_obj_map_insert(info, osd, fid, id, th);
685 fid_cpu_to_be(oi_fid, fid);
686 osd_id_pack(oi_id, id);
687 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
688 (const struct dt_rec *)oi_id,
689 (const struct dt_key *)oi_fid, th, true);
692 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
697 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
701 if (unlikely(osd_id_eq(id, oi_id)))
704 /* Check whether the mapping for oi_id is valid or not. */
705 inode = osd_iget(info, osd, oi_id);
708 if (rc == -ENOENT || rc == -ESTALE)
713 /* The EA inode should NOT be in OI, old OI scrub may added
714 * such OI mapping by wrong, replace it. */
715 if (unlikely(osd_is_ea_inode(inode))) {
720 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
721 &info->oti_ost_attrs);
729 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
730 lu_fid_eq(fid, &lma->lma_self_fid)) {
731 CERROR("%s: the FID "DFID" is used by two objects: "
732 "%u/%u %u/%u\n", osd_dev2name(osd),
733 PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
734 id->oii_ino, id->oii_gen);
739 osd_id_pack(oi_id, id);
740 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
741 (const struct dt_rec *)oi_id,
742 (const struct dt_key *)oi_fid, th, false);
750 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
751 rc = osd_obj_spec_insert(info, osd, fid, id, th);
755 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
756 const struct dt_key *key, handle_t *th)
758 struct iam_container *bag;
759 struct iam_path_descr *ipd;
764 LASSERT(oi->oi_inode);
765 ll_vfs_dq_init(oi->oi_inode);
767 bag = &oi->oi_dir.od_container;
768 ipd = osd_idx_ipd_get(oti->oti_env, bag);
769 if (unlikely(ipd == NULL))
773 LASSERT(th->h_transaction != NULL);
775 rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
776 osd_ipd_put(oti->oti_env, bag, ipd);
777 LINVRNT(osd_invariant(obj));
781 int osd_oi_delete(struct osd_thread_info *info,
782 struct osd_device *osd, const struct lu_fid *fid,
783 handle_t *th, enum oi_check_flags flags)
785 struct lu_fid *oi_fid = &info->oti_fid2;
787 /* clear idmap cache */
788 if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
789 fid_zero(&info->oti_cache.oic_fid);
791 if (fid_is_last_id(fid))
794 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
795 return osd_obj_map_delete(info, osd, fid, th);
797 fid_cpu_to_be(oi_fid, fid);
798 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
799 (const struct dt_key *)oi_fid, th);
802 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
803 const struct lu_fid *fid, const struct osd_inode_id *id,
804 handle_t *th, enum oi_check_flags flags)
806 struct lu_fid *oi_fid = &info->oti_fid2;
807 struct osd_inode_id *oi_id = &info->oti_id2;
810 if (unlikely(fid_is_last_id(fid)))
811 return osd_obj_spec_update(info, osd, fid, id, th);
813 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
814 return osd_obj_map_update(info, osd, fid, id, th);
816 fid_cpu_to_be(oi_fid, fid);
817 osd_id_pack(oi_id, id);
818 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
819 (const struct dt_rec *)oi_id,
820 (const struct dt_key *)oi_fid, th, false);
824 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
825 rc = osd_obj_spec_update(info, osd, fid, id, th);
829 int osd_oi_mod_init(void)
831 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
832 osd_oi_count = OSD_OI_FID_NR;
834 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
835 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
836 osd_oi_count, size_roundup_power2(osd_oi_count));
837 osd_oi_count = size_roundup_power2(osd_oi_count);
840 mutex_init(&oi_init_lock);