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, Whamcloud, Inc.
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 cfs_mutex_t 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 dt_device *dev = &osd->od_dt_dev;
291 struct scrub_file *sf = &osd->od_scrub.os_file;
297 /* NB: oi_count != 0 means that we have already created/known all OIs
298 * and have known exact number of OIs. */
299 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
301 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
304 if (oi_table[i] != NULL) {
309 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
310 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
316 if (rc == -ENOENT && create == false) {
321 ldiskfs_set_bit(i, sf->sf_oi_bitmap);
325 CERROR("%s: can't open %s: rc = %d\n",
326 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
328 CERROR("%s: expect to open total %d OI files.\n",
329 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
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 dt_device *dev = &osd->od_dt_dev;
344 struct osd_scrub *scrub = &osd->od_scrub;
345 struct scrub_file *sf = &scrub->os_file;
350 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
354 cfs_mutex_lock(&oi_init_lock);
355 /* try to open existing multiple OIs first */
356 rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
361 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
364 osd_scrub_file_reset(scrub,
365 LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
367 osd_oi_count = sf->sf_oi_count;
371 /* if previous failed then try found single OI from old filesystem */
372 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
373 if (rc == 0) { /* found single OI from old filesystem */
375 } else if (rc != -ENOENT) {
376 CERROR("%s: can't open %s: rc = %d\n",
377 dev->dd_lu_dev.ld_obd->obd_name, 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 cfs_mutex_unlock(&oi_init_lock);
420 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
422 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
424 OBD_FREE(osd->od_oi_table,
425 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
426 osd->od_oi_table = NULL;
429 static inline int fid_is_fs_root(const struct lu_fid *fid)
431 /* Map root inode to special local object FID */
432 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
433 fid_oid(fid) == OSD_FS_ROOT_OID));
436 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
437 struct osd_oi *oi, struct dt_rec *rec,
438 const struct dt_key *key)
440 struct iam_container *bag;
441 struct iam_iterator *it = &oti->oti_idx_it;
442 struct iam_rec *iam_rec;
443 struct iam_path_descr *ipd;
448 LASSERT(oi->oi_inode);
450 bag = &oi->oi_dir.od_container;
451 ipd = osd_idx_ipd_get(oti->oti_env, bag);
455 /* got ipd now we can start iterator. */
456 iam_it_init(it, bag, 0, ipd);
458 rc = iam_it_get(it, (struct iam_key *)key);
460 if (S_ISDIR(oi->oi_inode->i_mode))
461 iam_rec = (struct iam_rec *)oti->oti_ldp;
463 iam_rec = (struct iam_rec *)rec;
465 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
466 if (S_ISDIR(oi->oi_inode->i_mode))
467 osd_fid_unpack((struct lu_fid *)rec,
468 (struct osd_fid_pack *)iam_rec);
472 osd_ipd_put(oti->oti_env, bag, ipd);
474 LINVRNT(osd_invariant(obj));
479 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
480 const struct lu_fid *fid, struct osd_inode_id *id)
482 struct lu_fid *oi_fid = &info->oti_fid2;
485 fid_cpu_to_be(oi_fid, fid);
486 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
487 (const struct dt_key *)oi_fid);
489 osd_id_unpack(id, id);
491 } else if (rc == 0) {
497 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
498 const struct lu_fid *fid, struct osd_inode_id *id)
502 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG) {
504 rc = osd_compat_objid_lookup(info, osd, fid, id);
505 } else if (fid_is_igif(fid)) {
506 lu_igif_to_id(fid, id);
507 } else if (fid_is_fs_root(fid)) {
508 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
509 osd_sb(osd)->s_root->d_inode->i_generation);
511 if (unlikely(fid_is_acct(fid)))
512 return osd_acct_obj_lookup(info, osd, fid, id);
514 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
515 return osd_compat_spec_lookup(info, osd, fid, id);
517 rc = __osd_oi_lookup(info, osd, fid, id);
522 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
523 const struct dt_rec *rec, const struct dt_key *key,
526 struct iam_container *bag;
527 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
528 struct iam_path_descr *ipd;
529 struct osd_thandle *oh;
531 #ifdef HAVE_QUOTA_SUPPORT
532 cfs_cap_t save = cfs_curproc_cap_pack();
537 LASSERT(oi->oi_inode);
539 bag = &oi->oi_dir.od_container;
540 ipd = osd_idx_ipd_get(oti->oti_env, bag);
541 if (unlikely(ipd == NULL))
544 oh = container_of0(th, struct osd_thandle, ot_super);
545 LASSERT(oh->ot_handle != NULL);
546 LASSERT(oh->ot_handle->h_transaction != NULL);
547 #ifdef HAVE_QUOTA_SUPPORT
548 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
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 #ifdef HAVE_QUOTA_SUPPORT
558 cfs_curproc_cap_unpack(save);
560 osd_ipd_put(oti->oti_env, bag, ipd);
561 LINVRNT(osd_invariant(obj));
565 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
566 const struct lu_fid *fid, const struct osd_inode_id *id,
569 struct lu_fid *oi_fid = &info->oti_fid2;
570 struct osd_inode_id *oi_id = &info->oti_id2;
572 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
575 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
576 return osd_compat_objid_insert(info, osd, fid, id, th);
578 /* Server mount should not depends on OI files */
579 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
580 return osd_compat_spec_insert(info, osd, fid, id, th);
582 fid_cpu_to_be(oi_fid, fid);
583 osd_id_pack(oi_id, id);
584 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
585 (const struct dt_rec *)oi_id,
586 (const struct dt_key *)oi_fid, th);
589 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
590 const struct dt_key *key, struct thandle *handle)
592 struct iam_container *bag;
593 struct iam_path_descr *ipd;
594 struct osd_thandle *oh;
600 bag = &oi->oi_dir.od_container;
601 ipd = osd_idx_ipd_get(oti->oti_env, bag);
602 if (unlikely(ipd == NULL))
605 oh = container_of0(handle, struct osd_thandle, ot_super);
606 LASSERT(oh->ot_handle != NULL);
607 LASSERT(oh->ot_handle->h_transaction != NULL);
609 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
610 osd_ipd_put(oti->oti_env, bag, ipd);
611 LINVRNT(osd_invariant(obj));
615 int osd_oi_delete(struct osd_thread_info *info,
616 struct osd_device *osd, const struct lu_fid *fid,
619 struct lu_fid *oi_fid = &info->oti_fid2;
621 if (fid_is_igif(fid))
624 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
626 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
627 return osd_compat_objid_delete(info, osd, fid, th);
629 fid_cpu_to_be(oi_fid, fid);
630 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
631 (const struct dt_key *)oi_fid, th);
634 int osd_oi_mod_init(void)
636 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
637 osd_oi_count = OSD_OI_FID_NR;
639 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
640 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
641 osd_oi_count, size_roundup_power2(osd_oi_count));
642 osd_oi_count = size_roundup_power2(osd_oi_count);
645 cfs_mutex_init(&oi_init_lock);