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"
74 static unsigned int osd_oi_count = OSD_OI_FID_NR;
75 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
76 "Number of Object Index containers to be created, "
77 "it's only valid for new filesystem.");
79 /** to serialize concurrent OI index initialization */
80 static cfs_mutex_t oi_init_lock;
82 static struct dt_index_features oi_feat = {
83 .dif_flags = DT_IND_UPDATE,
84 .dif_recsize_min = sizeof(struct osd_inode_id),
85 .dif_recsize_max = sizeof(struct osd_inode_id),
89 #define OSD_OI_NAME_BASE "oi.16"
91 static void osd_oi_table_put(struct osd_thread_info *info,
92 struct osd_oi **oi_table, unsigned oi_count)
94 struct iam_container *bag;
97 for (i = 0; i < oi_count; i++) {
98 LASSERT(oi_table[i] != NULL);
99 LASSERT(oi_table[i]->oi_inode != NULL);
101 bag = &(oi_table[i]->oi_dir.od_container);
102 if (bag->ic_object == oi_table[i]->oi_inode)
103 iam_container_fini(bag);
104 iput(oi_table[i]->oi_inode);
105 oi_table[i]->oi_inode = NULL;
106 OBD_FREE_PTR(oi_table[i]);
110 static int osd_oi_index_create_one(struct osd_thread_info *info,
111 struct osd_device *osd, const char *name,
112 struct dt_index_features *feat)
114 const struct lu_env *env = info->oti_env;
115 struct osd_inode_id *id = &info->oti_id;
116 struct buffer_head *bh;
118 struct ldiskfs_dir_entry_2 *de;
119 struct dentry *dentry;
120 struct super_block *sb = osd_sb(osd);
121 struct inode *dir = sb->s_root->d_inode;
125 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
126 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
128 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
130 inode = osd_iget(info, osd, id);
131 if (!IS_ERR(inode)) {
133 inode = ERR_PTR(-EEXIST);
135 return PTR_ERR(inode);
138 jh = ldiskfs_journal_start_sb(sb, 100);
142 inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
144 ldiskfs_journal_stop(jh);
145 return PTR_ERR(inode);
148 if (feat->dif_flags & DT_IND_VARKEY)
149 rc = iam_lvar_create(inode, feat->dif_keysize_max,
150 feat->dif_ptrsize, feat->dif_recsize_max,
153 rc = iam_lfix_create(inode, feat->dif_keysize_max,
154 feat->dif_ptrsize, feat->dif_recsize_max,
156 dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
157 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
158 ldiskfs_journal_stop(jh);
163 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
164 struct osd_device *osd,
166 struct dt_index_features *f,
169 struct dentry *dentry;
173 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
175 return (void *) dentry;
177 if (dentry->d_inode) {
178 LASSERT(!is_bad_inode(dentry->d_inode));
179 inode = dentry->d_inode;
180 atomic_inc(&inode->i_count);
187 shrink_dcache_parent(osd_sb(osd)->s_root);
189 return ERR_PTR(-ENOENT);
191 rc = osd_oi_index_create_one(info, osd, name, f);
195 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
197 return (void *) dentry;
199 if (dentry->d_inode) {
200 LASSERT(!is_bad_inode(dentry->d_inode));
201 inode = dentry->d_inode;
202 atomic_inc(&inode->i_count);
207 return ERR_PTR(-ENOENT);
211 * Open an OI(Ojbect Index) container.
213 * \param name Name of OI container
214 * \param objp Pointer of returned OI
217 * \retval -ve failure
219 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
220 char *name, struct osd_oi **oi_slot, bool create)
222 struct osd_directory *dir;
223 struct iam_container *bag;
230 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
231 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
233 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
235 RETURN(PTR_ERR(inode));
239 GOTO(out_inode, rc = -ENOMEM);
241 oi->oi_inode = inode;
244 bag = &dir->od_container;
245 rc = iam_container_init(bag, &dir->od_descr, inode);
249 rc = iam_container_setup(bag);
251 GOTO(out_container, rc);
257 iam_container_fini(bag);
266 * Open OI(Object Index) table.
267 * If \a oi_count is zero, which means caller doesn't know how many OIs there
268 * will be, this function can either return 0 for new filesystem, or number
269 * of OIs on existed filesystem.
271 * If \a oi_count is non-zero, which means caller does know number of OIs on
272 * filesystem, this function should return the exactly same number on
273 * success, or error code in failure.
275 * \param oi_count Number of expected OI containers
276 * \param create Create OIs if doesn't exist
278 * \retval +ve number of opened OI containers
279 * \retval 0 no OI containers found
280 * \retval -ve failure
283 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
284 struct osd_oi **oi_table, unsigned oi_count, bool create)
286 struct dt_device *dev = &osd->od_dt_dev;
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 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
299 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
305 if (rc == -ENOENT && oi_count == 0)
308 CERROR("%s: can't open %s: rc = %d\n",
309 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
311 CERROR("%s: expect to open total %d OI files.\n",
312 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
318 osd_oi_table_put(info, oi_table, count);
325 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
327 struct dt_device *dev = &osd->od_dt_dev;
331 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
335 cfs_mutex_lock(&oi_init_lock);
336 /* try to open existing multiple OIs first */
337 rc = osd_oi_table_open(info, osd, oi, 0, false);
341 /* if previous failed then try found single OI from old filesystem */
342 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
343 if (rc == 0) { /* found single OI from old filesystem */
346 } else if (rc != -ENOENT) {
347 CERROR("%s: can't open %s: rc = %d\n",
348 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
352 /* No OIs exist, new filesystem, create OI objects */
353 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
354 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
357 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
359 LASSERT((rc & (rc - 1)) == 0);
360 osd->od_oi_table = oi;
361 osd->od_oi_count = rc;
365 cfs_mutex_unlock(&oi_init_lock);
369 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
371 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
373 OBD_FREE(osd->od_oi_table,
374 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
375 osd->od_oi_table = NULL;
378 static inline int fid_is_fs_root(const struct lu_fid *fid)
380 /* Map root inode to special local object FID */
381 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
382 fid_oid(fid) == OSD_FS_ROOT_OID));
385 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
386 struct osd_oi *oi, struct dt_rec *rec,
387 const struct dt_key *key)
389 struct iam_container *bag;
390 struct iam_iterator *it = &oti->oti_idx_it;
391 struct iam_rec *iam_rec;
392 struct iam_path_descr *ipd;
397 LASSERT(oi->oi_inode);
399 bag = &oi->oi_dir.od_container;
400 ipd = osd_idx_ipd_get(oti->oti_env, bag);
404 /* got ipd now we can start iterator. */
405 iam_it_init(it, bag, 0, ipd);
407 rc = iam_it_get(it, (struct iam_key *)key);
409 if (S_ISDIR(oi->oi_inode->i_mode))
410 iam_rec = (struct iam_rec *)oti->oti_ldp;
412 iam_rec = (struct iam_rec *)rec;
414 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
415 if (S_ISDIR(oi->oi_inode->i_mode))
416 osd_fid_unpack((struct lu_fid *)rec,
417 (struct osd_fid_pack *)iam_rec);
421 osd_ipd_put(oti->oti_env, bag, ipd);
423 LINVRNT(osd_invariant(obj));
428 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
429 const struct lu_fid *fid, struct osd_inode_id *id)
431 struct lu_fid *oi_fid = &info->oti_fid2;
434 fid_cpu_to_be(oi_fid, fid);
435 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
436 (const struct dt_key *)oi_fid);
438 osd_id_unpack(id, id);
440 } else if (rc == 0) {
446 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
447 const struct lu_fid *fid, struct osd_inode_id *id)
451 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG) {
453 rc = osd_compat_objid_lookup(info, osd, fid, id);
454 } else if (fid_is_igif(fid)) {
455 lu_igif_to_id(fid, id);
456 } else if (fid_is_fs_root(fid)) {
457 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
458 osd_sb(osd)->s_root->d_inode->i_generation);
460 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
461 return osd_compat_spec_lookup(info, osd, fid, id);
463 rc = __osd_oi_lookup(info, osd, fid, id);
468 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
469 const struct dt_rec *rec, const struct dt_key *key,
472 struct iam_container *bag;
473 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
474 struct iam_path_descr *ipd;
475 struct osd_thandle *oh;
477 #ifdef HAVE_QUOTA_SUPPORT
478 cfs_cap_t save = cfs_curproc_cap_pack();
483 LASSERT(oi->oi_inode);
485 bag = &oi->oi_dir.od_container;
486 ipd = osd_idx_ipd_get(oti->oti_env, bag);
487 if (unlikely(ipd == NULL))
490 oh = container_of0(th, struct osd_thandle, ot_super);
491 LASSERT(oh->ot_handle != NULL);
492 LASSERT(oh->ot_handle->h_transaction != NULL);
493 #ifdef HAVE_QUOTA_SUPPORT
494 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
496 if (S_ISDIR(oi->oi_inode->i_mode))
497 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
500 iam_rec = (struct iam_rec *) rec;
501 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
503 #ifdef HAVE_QUOTA_SUPPORT
504 cfs_curproc_cap_unpack(save);
506 osd_ipd_put(oti->oti_env, bag, ipd);
507 LINVRNT(osd_invariant(obj));
511 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
512 const struct lu_fid *fid, const struct osd_inode_id *id,
515 struct lu_fid *oi_fid = &info->oti_fid2;
516 struct osd_inode_id *oi_id = &info->oti_id2;
518 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
521 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
522 return osd_compat_objid_insert(info, osd, fid, id, th);
524 /* Server mount should not depends on OI files */
525 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
526 return osd_compat_spec_insert(info, osd, fid, id, th);
528 fid_cpu_to_be(oi_fid, fid);
529 osd_id_pack(oi_id, id);
530 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
531 (const struct dt_rec *)oi_id,
532 (const struct dt_key *)oi_fid, th);
535 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
536 const struct dt_key *key, struct thandle *handle)
538 struct iam_container *bag;
539 struct iam_path_descr *ipd;
540 struct osd_thandle *oh;
546 bag = &oi->oi_dir.od_container;
547 ipd = osd_idx_ipd_get(oti->oti_env, bag);
548 if (unlikely(ipd == NULL))
551 oh = container_of0(handle, struct osd_thandle, ot_super);
552 LASSERT(oh->ot_handle != NULL);
553 LASSERT(oh->ot_handle->h_transaction != NULL);
555 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
556 osd_ipd_put(oti->oti_env, bag, ipd);
557 LINVRNT(osd_invariant(obj));
561 int osd_oi_delete(struct osd_thread_info *info,
562 struct osd_device *osd, const struct lu_fid *fid,
565 struct lu_fid *oi_fid = &info->oti_fid2;
567 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
569 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
570 return osd_compat_objid_delete(info, osd, fid, th);
572 fid_cpu_to_be(oi_fid, fid);
573 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
574 (const struct dt_key *)oi_fid, th);
577 int osd_oi_mod_init(void)
579 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
580 osd_oi_count = OSD_OI_FID_NR;
582 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
583 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
584 osd_oi_count, size_roundup_power2(osd_oi_count));
585 osd_oi_count = size_roundup_power2(osd_oi_count);
588 cfs_mutex_init(&oi_init_lock);