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 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 */
373 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
374 if (sf->sf_success_count == 0)
375 /* XXX: There is one corner case that if the OI_scrub
376 * file crashed or lost and we regard it upgrade,
377 * then we allow IGIF lookup to bypass OI files.
379 * The risk is that osd_fid_lookup() may found
380 * a wrong inode with the given IGIF especially
381 * when the MDT has performed file-level backup
382 * and restored after former upgrading from 1.8
383 * to 2.x. Fortunately, the osd_fid_lookup()can
384 * verify the inode to decrease the risk. */
385 osd_scrub_file_reset(scrub,
386 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
389 } else if (rc != -ENOENT) {
390 CERROR("%.16s: can't open %s: rc = %d\n",
391 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
392 OSD_OI_NAME_BASE, rc);
396 if (sf->sf_oi_count > 0) {
399 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
400 for (i = 0; i < osd_oi_count; i++)
401 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
402 osd_scrub_file_reset(scrub,
403 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
406 sf->sf_oi_count = osd_oi_count;
409 rc = osd_scrub_file_store(scrub);
411 osd_oi_table_put(info, oi, sf->sf_oi_count);
415 /* No OIs exist, new filesystem, create OI objects */
416 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
417 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
423 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
425 LASSERT((rc & (rc - 1)) == 0);
426 osd->od_oi_table = oi;
427 osd->od_oi_count = rc;
428 if (sf->sf_oi_count != rc) {
429 sf->sf_oi_count = rc;
430 rc = osd_scrub_file_store(scrub);
432 osd_oi_table_put(info, oi, sf->sf_oi_count);
433 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
440 mutex_unlock(&oi_init_lock);
444 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
446 if (unlikely(osd->od_oi_table == NULL))
449 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
451 OBD_FREE(osd->od_oi_table,
452 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
453 osd->od_oi_table = NULL;
456 static inline int fid_is_fs_root(const struct lu_fid *fid)
458 /* Map root inode to special local object FID */
459 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
460 fid_oid(fid) == OSD_FS_ROOT_OID));
463 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
464 struct osd_oi *oi, struct dt_rec *rec,
465 const struct dt_key *key)
467 struct iam_container *bag;
468 struct iam_iterator *it = &oti->oti_idx_it;
469 struct iam_path_descr *ipd;
474 LASSERT(oi->oi_inode);
476 bag = &oi->oi_dir.od_container;
477 ipd = osd_idx_ipd_get(oti->oti_env, bag);
481 /* got ipd now we can start iterator. */
482 iam_it_init(it, bag, 0, ipd);
484 rc = iam_it_get(it, (struct iam_key *)key);
486 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
489 osd_ipd_put(oti->oti_env, bag, ipd);
491 LINVRNT(osd_invariant(obj));
496 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
497 const struct lu_fid *fid, enum oi_check_flags flags)
499 struct lu_seq_range *range = &info->oti_seq_range;
503 if (flags & OI_KNOWN_ON_OST)
506 if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
507 fid_is_llog(fid)) || fid_is_name_llog(fid) ||
511 if (fid_is_idif(fid) || fid_is_last_id(fid))
514 if (!(flags & OI_CHECK_FLD))
517 if (osd_seq_site(osd)->ss_server_fld == NULL)
520 rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
523 CERROR("%s: lookup FLD "DFID": rc = %d\n",
524 osd_name(osd), PFID(fid), rc);
528 if (fld_range_is_ost(range))
534 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
535 const struct lu_fid *fid, struct osd_inode_id *id)
537 struct lu_fid *oi_fid = &info->oti_fid2;
540 fid_cpu_to_be(oi_fid, fid);
541 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
542 (const struct dt_key *)oi_fid);
544 osd_id_unpack(id, id);
546 } else if (rc == 0) {
552 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
553 const struct lu_fid *fid, struct osd_inode_id *id,
554 enum oi_check_flags flags)
556 if (unlikely(fid_is_last_id(fid)))
557 return osd_obj_spec_lookup(info, osd, fid, id);
559 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
560 return osd_obj_map_lookup(info, osd, fid, id);
563 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
565 if (fid_is_fs_root(fid)) {
566 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
567 osd_sb(osd)->s_root->d_inode->i_generation);
570 if (unlikely(fid_is_acct(fid)))
571 return osd_acct_obj_lookup(info, osd, fid, id);
573 /* For other special FIDs, try OI first, then do spec lookup */
574 rc = __osd_oi_lookup(info, osd, fid, id);
576 return osd_obj_spec_lookup(info, osd, fid, id);
580 if (!osd->od_igif_inoi && fid_is_igif(fid)) {
581 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
585 return __osd_oi_lookup(info, osd, fid, id);
588 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
589 const struct dt_rec *rec, const struct dt_key *key,
590 handle_t *th, bool insert)
592 struct iam_container *bag;
593 struct iam_path_descr *ipd;
598 LASSERT(oi->oi_inode);
599 ll_vfs_dq_init(oi->oi_inode);
601 bag = &oi->oi_dir.od_container;
602 ipd = osd_idx_ipd_get(oti->oti_env, bag);
603 if (unlikely(ipd == NULL))
607 LASSERT(th->h_transaction != NULL);
609 rc = iam_insert(th, bag, (const struct iam_key *)key,
610 (const struct iam_rec *)rec, ipd);
612 rc = iam_update(th, bag, (const struct iam_key *)key,
613 (const struct iam_rec *)rec, ipd);
614 osd_ipd_put(oti->oti_env, bag, ipd);
615 LINVRNT(osd_invariant(obj));
619 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
620 const struct lu_fid *fid, const struct osd_inode_id *id,
621 handle_t *th, enum oi_check_flags flags)
623 struct lu_fid *oi_fid = &info->oti_fid2;
624 struct osd_inode_id *oi_id = &info->oti_id2;
627 if (unlikely(fid_is_last_id(fid)))
628 return osd_obj_spec_insert(info, osd, fid, id, th);
630 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
631 return osd_obj_map_insert(info, osd, fid, id, th);
633 fid_cpu_to_be(oi_fid, fid);
634 osd_id_pack(oi_id, id);
635 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
636 (const struct dt_rec *)oi_id,
637 (const struct dt_key *)oi_fid, th, true);
640 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
645 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
649 if (unlikely(osd_id_eq(id, oi_id)))
652 /* Check whether the mapping for oi_id is valid or not. */
653 inode = osd_iget(info, osd, oi_id);
656 if (rc == -ENOENT || rc == -ESTALE)
661 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
669 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
670 lu_fid_eq(fid, &lma->lma_self_fid)) {
671 CERROR("%.16s: the FID "DFID" is used by two objects: "
673 LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
674 PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
675 id->oii_ino, id->oii_gen);
680 osd_id_pack(oi_id, id);
681 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
682 (const struct dt_rec *)oi_id,
683 (const struct dt_key *)oi_fid, th, false);
688 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
689 rc = osd_obj_spec_insert(info, osd, fid, id, th);
693 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
694 const struct dt_key *key, handle_t *th)
696 struct iam_container *bag;
697 struct iam_path_descr *ipd;
702 LASSERT(oi->oi_inode);
703 ll_vfs_dq_init(oi->oi_inode);
705 bag = &oi->oi_dir.od_container;
706 ipd = osd_idx_ipd_get(oti->oti_env, bag);
707 if (unlikely(ipd == NULL))
711 LASSERT(th->h_transaction != NULL);
713 rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
714 osd_ipd_put(oti->oti_env, bag, ipd);
715 LINVRNT(osd_invariant(obj));
719 int osd_oi_delete(struct osd_thread_info *info,
720 struct osd_device *osd, const struct lu_fid *fid,
721 handle_t *th, enum oi_check_flags flags)
723 struct lu_fid *oi_fid = &info->oti_fid2;
725 /* clear idmap cache */
726 if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
727 fid_zero(&info->oti_cache.oic_fid);
729 if (fid_is_last_id(fid))
732 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
733 return osd_obj_map_delete(info, osd, fid, th);
735 fid_cpu_to_be(oi_fid, fid);
736 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
737 (const struct dt_key *)oi_fid, th);
740 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
741 const struct lu_fid *fid, const struct osd_inode_id *id,
742 handle_t *th, enum oi_check_flags flags)
744 struct lu_fid *oi_fid = &info->oti_fid2;
745 struct osd_inode_id *oi_id = &info->oti_id2;
748 if (unlikely(fid_is_last_id(fid)))
749 return osd_obj_spec_update(info, osd, fid, id, th);
751 if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
752 return osd_obj_map_update(info, osd, fid, id, th);
754 fid_cpu_to_be(oi_fid, fid);
755 osd_id_pack(oi_id, id);
756 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
757 (const struct dt_rec *)oi_id,
758 (const struct dt_key *)oi_fid, th, false);
762 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
763 rc = osd_obj_spec_update(info, osd, fid, id, th);
767 int osd_oi_mod_init(void)
769 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
770 osd_oi_count = OSD_OI_FID_NR;
772 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
773 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
774 osd_oi_count, size_roundup_power2(osd_oi_count));
775 osd_oi_count = size_roundup_power2(osd_oi_count);
778 mutex_init(&oi_init_lock);