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"
73 #include "osd_scrub.h"
75 static unsigned int osd_oi_count = OSD_OI_FID_NR;
76 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
77 "Number of Object Index containers to be created, "
78 "it's only valid for new filesystem.");
80 /** to serialize concurrent OI index initialization */
81 static struct mutex oi_init_lock;
83 static struct dt_index_features oi_feat = {
84 .dif_flags = DT_IND_UPDATE,
85 .dif_recsize_min = sizeof(struct osd_inode_id),
86 .dif_recsize_max = sizeof(struct osd_inode_id),
90 #define OSD_OI_NAME_BASE "oi.16"
92 static void osd_oi_table_put(struct osd_thread_info *info,
93 struct osd_oi **oi_table, unsigned oi_count)
95 struct iam_container *bag;
98 for (i = 0; i < oi_count; i++) {
99 if (oi_table[i] == NULL)
102 LASSERT(oi_table[i]->oi_inode != NULL);
104 bag = &(oi_table[i]->oi_dir.od_container);
105 if (bag->ic_object == oi_table[i]->oi_inode)
106 iam_container_fini(bag);
107 iput(oi_table[i]->oi_inode);
108 oi_table[i]->oi_inode = NULL;
109 OBD_FREE_PTR(oi_table[i]);
114 static int osd_oi_index_create_one(struct osd_thread_info *info,
115 struct osd_device *osd, const char *name,
116 struct dt_index_features *feat)
118 const struct lu_env *env = info->oti_env;
119 struct osd_inode_id *id = &info->oti_id;
120 struct buffer_head *bh;
122 struct ldiskfs_dir_entry_2 *de;
123 struct dentry *dentry;
124 struct super_block *sb = osd_sb(osd);
125 struct inode *dir = sb->s_root->d_inode;
129 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
130 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
132 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
134 inode = osd_iget(info, osd, id);
135 if (!IS_ERR(inode)) {
137 inode = ERR_PTR(-EEXIST);
139 return PTR_ERR(inode);
142 jh = ldiskfs_journal_start_sb(sb, 100);
146 inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
148 ldiskfs_journal_stop(jh);
149 return PTR_ERR(inode);
152 if (feat->dif_flags & DT_IND_VARKEY)
153 rc = iam_lvar_create(inode, feat->dif_keysize_max,
154 feat->dif_ptrsize, feat->dif_recsize_max,
157 rc = iam_lfix_create(inode, feat->dif_keysize_max,
158 feat->dif_ptrsize, feat->dif_recsize_max,
160 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
161 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
162 ldiskfs_journal_stop(jh);
167 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
168 struct osd_device *osd,
170 struct dt_index_features *f,
173 struct dentry *dentry;
177 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
179 return (void *) dentry;
181 if (dentry->d_inode) {
182 LASSERT(!is_bad_inode(dentry->d_inode));
183 inode = dentry->d_inode;
184 atomic_inc(&inode->i_count);
191 shrink_dcache_parent(osd_sb(osd)->s_root);
193 return ERR_PTR(-ENOENT);
195 rc = osd_oi_index_create_one(info, osd, name, f);
199 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
201 return (void *) dentry;
203 if (dentry->d_inode) {
204 LASSERT(!is_bad_inode(dentry->d_inode));
205 inode = dentry->d_inode;
206 atomic_inc(&inode->i_count);
211 return ERR_PTR(-ENOENT);
215 * Open an OI(Ojbect Index) container.
217 * \param name Name of OI container
218 * \param objp Pointer of returned OI
221 * \retval -ve failure
223 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
224 char *name, struct osd_oi **oi_slot, bool create)
226 struct osd_directory *dir;
227 struct iam_container *bag;
234 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
235 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
237 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
239 RETURN(PTR_ERR(inode));
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_rec *iam_rec;
446 struct iam_path_descr *ipd;
451 LASSERT(oi->oi_inode);
453 bag = &oi->oi_dir.od_container;
454 ipd = osd_idx_ipd_get(oti->oti_env, bag);
458 /* got ipd now we can start iterator. */
459 iam_it_init(it, bag, 0, ipd);
461 rc = iam_it_get(it, (struct iam_key *)key);
463 if (S_ISDIR(oi->oi_inode->i_mode))
464 iam_rec = (struct iam_rec *)oti->oti_ldp;
466 iam_rec = (struct iam_rec *)rec;
468 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
469 if (S_ISDIR(oi->oi_inode->i_mode))
470 osd_fid_unpack((struct lu_fid *)rec,
471 (struct osd_fid_pack *)iam_rec);
475 osd_ipd_put(oti->oti_env, bag, ipd);
477 LINVRNT(osd_invariant(obj));
482 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
483 const struct lu_fid *fid, struct osd_inode_id *id)
485 struct lu_fid *oi_fid = &info->oti_fid2;
488 fid_cpu_to_be(oi_fid, fid);
489 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
490 (const struct dt_key *)oi_fid);
492 osd_id_unpack(id, id);
494 } else if (rc == 0) {
500 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
501 const struct lu_fid *fid, struct osd_inode_id *id)
505 if ((fid_is_idif(fid) && !fid_is_last_id(fid)) ||
508 /* FIXME: actually for all of the OST object */
509 rc = osd_obj_map_lookup(info, osd, fid, id);
510 } else if (fid_is_igif(fid)) {
511 lu_igif_to_id(fid, id);
512 } else if (fid_is_fs_root(fid)) {
513 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
514 osd_sb(osd)->s_root->d_inode->i_generation);
516 if (unlikely(fid_is_acct(fid)))
517 return osd_acct_obj_lookup(info, osd, fid, id);
518 else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE) ||
520 return osd_obj_spec_lookup(info, osd, fid, id);
522 rc = __osd_oi_lookup(info, osd, fid, id);
527 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
528 const struct dt_rec *rec, const struct dt_key *key,
531 struct iam_container *bag;
532 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
533 struct iam_path_descr *ipd;
534 struct osd_thandle *oh;
539 LASSERT(oi->oi_inode);
540 ll_vfs_dq_init(oi->oi_inode);
542 bag = &oi->oi_dir.od_container;
543 ipd = osd_idx_ipd_get(oti->oti_env, bag);
544 if (unlikely(ipd == NULL))
547 oh = container_of0(th, struct osd_thandle, ot_super);
548 LASSERT(oh->ot_handle != NULL);
549 LASSERT(oh->ot_handle->h_transaction != NULL);
550 if (S_ISDIR(oi->oi_inode->i_mode))
551 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
554 iam_rec = (struct iam_rec *) rec;
555 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
557 osd_ipd_put(oti->oti_env, bag, ipd);
558 LINVRNT(osd_invariant(obj));
562 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
563 const struct lu_fid *fid, const struct osd_inode_id *id,
566 struct lu_fid *oi_fid = &info->oti_fid2;
567 struct osd_inode_id *oi_id = &info->oti_id2;
569 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
572 if ((fid_is_idif(fid) && !fid_is_last_id(fid)) ||
574 return osd_obj_map_insert(info, osd, fid, id, th);
576 /* Server mount should not depends on OI files */
577 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE) ||
579 return osd_obj_spec_insert(info, osd, fid, id, th);
581 fid_cpu_to_be(oi_fid, fid);
582 osd_id_pack(oi_id, id);
583 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
584 (const struct dt_rec *)oi_id,
585 (const struct dt_key *)oi_fid, th);
588 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
589 const struct dt_key *key, struct thandle *handle)
591 struct iam_container *bag;
592 struct iam_path_descr *ipd;
593 struct osd_thandle *oh;
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))
606 oh = container_of0(handle, struct osd_thandle, ot_super);
607 LASSERT(oh->ot_handle != NULL);
608 LASSERT(oh->ot_handle->h_transaction != NULL);
610 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
611 osd_ipd_put(oti->oti_env, bag, ipd);
612 LINVRNT(osd_invariant(obj));
616 int osd_oi_delete(struct osd_thread_info *info,
617 struct osd_device *osd, const struct lu_fid *fid,
620 struct lu_fid *oi_fid = &info->oti_fid2;
622 if (fid_is_igif(fid) || fid_is_last_id(fid))
625 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
627 if (fid_is_idif(fid) || fid_is_llog(fid))
628 return osd_obj_map_delete(info, osd, fid, th);
630 fid_cpu_to_be(oi_fid, fid);
631 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
632 (const struct dt_key *)oi_fid, th);
635 int osd_oi_mod_init(void)
637 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
638 osd_oi_count = OSD_OI_FID_NR;
640 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
641 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
642 osd_oi_count, size_roundup_power2(osd_oi_count));
643 osd_oi_count = size_roundup_power2(osd_oi_count);
646 mutex_init(&oi_init_lock);