1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
42 * Author: Nikita Danilov <nikita@clusterfs.com>
46 * oi uses two mechanisms to implement fid->cookie mapping:
48 * - persistent index, where cookie is a record and fid is a key, and
50 * - algorithmic mapping for "igif" fids.
55 # define EXPORT_SYMTAB
57 #define DEBUG_SUBSYSTEM S_MDS
59 #include <linux/module.h>
61 /* LUSTRE_VERSION_CODE */
62 #include <lustre_ver.h>
64 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
71 #include <lustre_fid.h>
74 /* osd_lookup(), struct osd_thread_info */
75 #include "osd_internal.h"
77 #include "dt_object.h"
79 #define OSD_OI_FID_NR (1UL << OSD_OI_FID_OID_BITS)
80 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
82 static unsigned int osd_oi_count = OSD_OI_FID_NR;
83 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
84 "Number of Object Index containers to be created, "
85 "it's only valid for new filesystem.");
87 /** to serialize concurrent OI index initialization */
88 static cfs_mutex_t oi_init_lock;
90 static struct dt_index_features oi_feat = {
91 .dif_flags = DT_IND_UPDATE,
92 .dif_recsize_min = sizeof(struct osd_inode_id),
93 .dif_recsize_max = sizeof(struct osd_inode_id),
97 #define OSD_OI_NAME_BASE "oi.16"
99 static void osd_oi_table_put(struct osd_thread_info *info,
100 struct osd_oi **oi_table, unsigned oi_count)
102 struct iam_container *bag;
105 for (i = 0; i < oi_count; i++) {
106 LASSERT(oi_table[i] != NULL);
107 LASSERT(oi_table[i]->oi_inode != NULL);
109 bag = &(oi_table[i]->oi_dir.od_container);
110 if (bag->ic_object == oi_table[i]->oi_inode)
111 iam_container_fini(bag);
112 iput(oi_table[i]->oi_inode);
113 oi_table[i]->oi_inode = NULL;
114 OBD_FREE_PTR(oi_table[i]);
118 static int osd_oi_index_create_one(struct osd_thread_info *info,
119 struct osd_device *osd, const char *name,
120 struct dt_index_features *feat)
122 const struct lu_env *env = info->oti_env;
123 struct osd_inode_id *id = &info->oti_id;
124 struct buffer_head *bh;
126 struct ldiskfs_dir_entry_2 *de;
127 struct dentry *dentry;
132 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
134 dir = osd_sb(osd)->s_root->d_inode;
135 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
139 id->oii_ino = le32_to_cpu(de->inode);
140 id->oii_gen = OSD_OII_NOGEN;
142 inode = osd_iget(info, osd, id);
143 if (!IS_ERR(inode)) {
147 RETURN(PTR_ERR(inode));
150 jh = ldiskfs_journal_start_sb(osd_sb(osd), 100);
151 LASSERT(!IS_ERR(jh));
153 inode = ldiskfs_create_inode(jh, osd_sb(osd)->s_root->d_inode,
154 (S_IFREG | S_IRUGO | S_IWUSR));
155 LASSERT(!IS_ERR(inode));
157 if (feat->dif_flags & DT_IND_VARKEY)
158 rc = iam_lvar_create(inode, feat->dif_keysize_max,
159 feat->dif_ptrsize, feat->dif_recsize_max,
162 rc = iam_lfix_create(inode, feat->dif_keysize_max,
163 feat->dif_ptrsize, feat->dif_recsize_max,
166 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
168 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
171 ldiskfs_journal_stop(jh);
177 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
178 struct osd_device *osd,
180 struct dt_index_features *f,
183 struct dentry *dentry;
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);
201 shrink_dcache_parent(osd_sb(osd)->s_root);
203 return ERR_PTR(-ENOENT);
205 rc = osd_oi_index_create_one(info, osd, name, f);
209 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
211 return (void *) dentry;
213 if (dentry->d_inode) {
214 LASSERT(!is_bad_inode(dentry->d_inode));
215 inode = dentry->d_inode;
216 atomic_inc(&inode->i_count);
221 return ERR_PTR(-ENOENT);
225 * Open an OI(Ojbect Index) container.
227 * \param name Name of OI container
228 * \param objp Pointer of returned OI
231 * \retval -ve failure
233 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
234 char *name, struct osd_oi **oi_slot, bool create)
236 struct osd_directory *dir;
237 struct iam_container *bag;
244 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
245 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
247 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
249 RETURN(PTR_ERR(inode));
253 GOTO(out_inode, rc = -ENOMEM);
255 oi->oi_inode = inode;
258 bag = &dir->od_container;
259 rc = iam_container_init(bag, &dir->od_descr, inode);
263 rc = iam_container_setup(bag);
265 GOTO(out_container, rc);
271 iam_container_fini(bag);
280 * Open OI(Object Index) table.
281 * If \a oi_count is zero, which means caller doesn't know how many OIs there
282 * will be, this function can either return 0 for new filesystem, or number
283 * of OIs on existed filesystem.
285 * If \a oi_count is non-zero, which means caller does know number of OIs on
286 * filesystem, this function should return the exactly same number on
287 * success, or error code in failure.
289 * \param oi_count Number of expected OI containers
290 * \param create Create OIs if doesn't exist
292 * \retval +ve number of opened OI containers
293 * \retval 0 no OI containers found
294 * \retval -ve failure
297 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
298 struct osd_oi **oi_table, unsigned oi_count, bool create)
300 struct dt_device *dev = &osd->od_dt_dev;
305 /* NB: oi_count != 0 means that we have already created/known all OIs
306 * and have known exact number of OIs. */
307 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
309 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
312 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
313 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
319 if (rc == -ENOENT && oi_count == 0)
322 CERROR("%s: can't open %s: rc = %d\n",
323 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
325 CERROR("%s: expect to open total %d OI files.\n",
326 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
332 osd_oi_table_put(info, oi_table, count);
339 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
341 struct dt_device *dev = &osd->od_dt_dev;
345 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
349 cfs_mutex_lock(&oi_init_lock);
350 /* try to open existing multiple OIs first */
351 rc = osd_oi_table_open(info, osd, oi, 0, false);
355 /* if previous failed then try found single OI from old filesystem */
356 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
357 if (rc == 0) { /* found single OI from old filesystem */
360 } else if (rc != -ENOENT) {
361 CERROR("%s: can't open %s: rc = %d\n",
362 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
366 /* No OIs exist, new filesystem, create OI objects */
367 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
368 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
371 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
373 LASSERT((rc & (rc - 1)) == 0);
374 osd->od_oi_table = oi;
375 osd->od_oi_count = rc;
379 cfs_mutex_unlock(&oi_init_lock);
383 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
385 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
387 OBD_FREE(osd->od_oi_table,
388 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
389 osd->od_oi_table = NULL;
392 static inline int fid_is_fs_root(const struct lu_fid *fid)
394 /* Map root inode to special local object FID */
395 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
396 fid_oid(fid) == OSD_FS_ROOT_OID));
399 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
400 struct osd_oi *oi, struct dt_rec *rec,
401 const struct dt_key *key)
403 struct iam_container *bag;
404 struct iam_iterator *it = &oti->oti_idx_it;
405 struct iam_rec *iam_rec;
406 struct iam_path_descr *ipd;
411 LASSERT(oi->oi_inode);
413 bag = &oi->oi_dir.od_container;
414 ipd = osd_idx_ipd_get(oti->oti_env, bag);
418 /* got ipd now we can start iterator. */
419 iam_it_init(it, bag, 0, ipd);
421 rc = iam_it_get(it, (struct iam_key *)key);
423 if (S_ISDIR(oi->oi_inode->i_mode))
424 iam_rec = (struct iam_rec *)oti->oti_ldp;
426 iam_rec = (struct iam_rec *)rec;
428 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
429 if (S_ISDIR(oi->oi_inode->i_mode))
430 osd_fid_unpack((struct lu_fid *)rec,
431 (struct osd_fid_pack *)iam_rec);
435 osd_ipd_put(oti->oti_env, bag, ipd);
437 LINVRNT(osd_invariant(obj));
442 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
443 const struct lu_fid *fid, struct osd_inode_id *id)
445 struct lu_fid *oi_fid = &info->oti_fid;
446 const struct dt_key *key;
449 if (osd_fid_is_igif(fid)) {
450 lu_igif_to_id(fid, id);
453 if (!fid_is_norm(fid))
456 fid_cpu_to_be(oi_fid, fid);
457 key = (struct dt_key *) oi_fid;
459 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid),
460 (struct dt_rec *)id, key);
463 id->oii_ino = be32_to_cpu(id->oii_ino);
464 id->oii_gen = be32_to_cpu(id->oii_gen);
466 } else if (rc == 0) {
473 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
474 const struct dt_rec *rec, const struct dt_key *key,
475 struct thandle *th, int ignore_quota)
477 struct iam_container *bag;
478 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
479 struct iam_path_descr *ipd;
480 struct osd_thandle *oh;
482 #ifdef HAVE_QUOTA_SUPPORT
483 cfs_cap_t save = cfs_curproc_cap_pack();
488 LASSERT(oi->oi_inode);
490 bag = &oi->oi_dir.od_container;
491 ipd = osd_idx_ipd_get(oti->oti_env, bag);
492 if (unlikely(ipd == NULL))
495 oh = container_of0(th, struct osd_thandle, ot_super);
496 LASSERT(oh->ot_handle != NULL);
497 LASSERT(oh->ot_handle->h_transaction != NULL);
498 #ifdef HAVE_QUOTA_SUPPORT
500 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
502 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
504 if (S_ISDIR(oi->oi_inode->i_mode))
505 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
508 iam_rec = (struct iam_rec *) rec;
509 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
511 #ifdef HAVE_QUOTA_SUPPORT
512 cfs_curproc_cap_unpack(save);
514 osd_ipd_put(oti->oti_env, bag, ipd);
515 LINVRNT(osd_invariant(obj));
519 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
520 const struct lu_fid *fid, const struct osd_inode_id *id0,
521 struct thandle *th, int ignore_quota)
523 struct lu_fid *oi_fid = &info->oti_fid;
524 struct osd_inode_id *id;
525 const struct dt_key *key;
527 if (!fid_is_norm(fid))
530 fid_cpu_to_be(oi_fid, fid);
531 key = (struct dt_key *)oi_fid;
534 id->oii_ino = cpu_to_be32(id0->oii_ino);
535 id->oii_gen = cpu_to_be32(id0->oii_gen);
537 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
538 (struct dt_rec *)id, key, th, ignore_quota);
541 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
542 const struct dt_key *key, struct thandle *handle)
544 struct iam_container *bag;
545 struct iam_path_descr *ipd;
546 struct osd_thandle *oh;
552 bag = &oi->oi_dir.od_container;
553 ipd = osd_idx_ipd_get(oti->oti_env, bag);
554 if (unlikely(ipd == NULL))
557 oh = container_of0(handle, struct osd_thandle, ot_super);
558 LASSERT(oh->ot_handle != NULL);
559 LASSERT(oh->ot_handle->h_transaction != NULL);
561 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
562 osd_ipd_put(oti->oti_env, bag, ipd);
563 LINVRNT(osd_invariant(obj));
567 int osd_oi_delete(struct osd_thread_info *info,
568 struct osd_device *osd, const struct lu_fid *fid,
571 struct lu_fid *oi_fid = &info->oti_fid;
572 const struct dt_key *key;
574 if (!fid_is_norm(fid))
577 fid_cpu_to_be(oi_fid, fid);
578 key = (struct dt_key *)oi_fid;
580 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid), key, th);
583 int osd_oi_mod_init()
585 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
586 osd_oi_count = OSD_OI_FID_NR;
588 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
589 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
590 osd_oi_count, size_roundup_power2(osd_oi_count));
591 osd_oi_count = size_roundup_power2(osd_oi_count);
594 cfs_mutex_init(&oi_init_lock);