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>
69 /* osd_lookup(), struct osd_thread_info */
70 #include "osd_internal.h"
72 #include "dt_object.h"
74 #define OSD_OI_FID_NR (1UL << OSD_OI_FID_OID_BITS)
75 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
77 static unsigned int osd_oi_count = OSD_OI_FID_NR;
78 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
79 "Number of Object Index containers to be created, "
80 "it's only valid for new filesystem.");
82 /** to serialize concurrent OI index initialization */
83 static cfs_mutex_t oi_init_lock;
85 static struct dt_index_features oi_feat = {
86 .dif_flags = DT_IND_UPDATE,
87 .dif_recsize_min = sizeof(struct osd_inode_id),
88 .dif_recsize_max = sizeof(struct osd_inode_id),
92 #define OSD_OI_NAME_BASE "oi.16"
94 static void osd_oi_table_put(struct osd_thread_info *info,
95 struct osd_oi **oi_table, unsigned oi_count)
97 struct iam_container *bag;
100 for (i = 0; i < oi_count; i++) {
101 LASSERT(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]);
113 static int osd_oi_index_create_one(struct osd_thread_info *info,
114 struct osd_device *osd, const char *name,
115 struct dt_index_features *feat)
117 const struct lu_env *env = info->oti_env;
118 struct osd_inode_id *id = &info->oti_id;
119 struct buffer_head *bh;
121 struct ldiskfs_dir_entry_2 *de;
122 struct dentry *dentry;
127 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
129 dir = osd_sb(osd)->s_root->d_inode;
130 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
134 id->oii_ino = le32_to_cpu(de->inode);
135 id->oii_gen = OSD_OII_NOGEN;
137 inode = osd_iget(info, osd, id);
138 if (!IS_ERR(inode)) {
142 RETURN(PTR_ERR(inode));
145 jh = ldiskfs_journal_start_sb(osd_sb(osd), 100);
146 LASSERT(!IS_ERR(jh));
148 inode = ldiskfs_create_inode(jh, osd_sb(osd)->s_root->d_inode,
149 (S_IFREG | S_IRUGO | S_IWUSR));
150 LASSERT(!IS_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,
161 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
163 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
166 ldiskfs_journal_stop(jh);
172 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
173 struct osd_device *osd,
175 struct dt_index_features *f,
178 struct dentry *dentry;
182 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
184 return (void *) dentry;
186 if (dentry->d_inode) {
187 LASSERT(!is_bad_inode(dentry->d_inode));
188 inode = dentry->d_inode;
189 atomic_inc(&inode->i_count);
196 shrink_dcache_parent(osd_sb(osd)->s_root);
198 return ERR_PTR(-ENOENT);
200 rc = osd_oi_index_create_one(info, osd, name, f);
204 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
206 return (void *) dentry;
208 if (dentry->d_inode) {
209 LASSERT(!is_bad_inode(dentry->d_inode));
210 inode = dentry->d_inode;
211 atomic_inc(&inode->i_count);
216 return ERR_PTR(-ENOENT);
220 * Open an OI(Ojbect Index) container.
222 * \param name Name of OI container
223 * \param objp Pointer of returned OI
226 * \retval -ve failure
228 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
229 char *name, struct osd_oi **oi_slot, bool create)
231 struct osd_directory *dir;
232 struct iam_container *bag;
239 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
240 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
242 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
244 RETURN(PTR_ERR(inode));
248 GOTO(out_inode, rc = -ENOMEM);
250 oi->oi_inode = inode;
253 bag = &dir->od_container;
254 rc = iam_container_init(bag, &dir->od_descr, inode);
258 rc = iam_container_setup(bag);
260 GOTO(out_container, rc);
266 iam_container_fini(bag);
275 * Open OI(Object Index) table.
276 * If \a oi_count is zero, which means caller doesn't know how many OIs there
277 * will be, this function can either return 0 for new filesystem, or number
278 * of OIs on existed filesystem.
280 * If \a oi_count is non-zero, which means caller does know number of OIs on
281 * filesystem, this function should return the exactly same number on
282 * success, or error code in failure.
284 * \param oi_count Number of expected OI containers
285 * \param create Create OIs if doesn't exist
287 * \retval +ve number of opened OI containers
288 * \retval 0 no OI containers found
289 * \retval -ve failure
292 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
293 struct osd_oi **oi_table, unsigned oi_count, bool create)
295 struct dt_device *dev = &osd->od_dt_dev;
300 /* NB: oi_count != 0 means that we have already created/known all OIs
301 * and have known exact number of OIs. */
302 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
304 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
307 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
308 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
314 if (rc == -ENOENT && oi_count == 0)
317 CERROR("%s: can't open %s: rc = %d\n",
318 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
320 CERROR("%s: expect to open total %d OI files.\n",
321 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
327 osd_oi_table_put(info, oi_table, count);
334 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
336 struct dt_device *dev = &osd->od_dt_dev;
340 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
344 cfs_mutex_lock(&oi_init_lock);
345 /* try to open existing multiple OIs first */
346 rc = osd_oi_table_open(info, osd, oi, 0, false);
350 /* if previous failed then try found single OI from old filesystem */
351 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
352 if (rc == 0) { /* found single OI from old filesystem */
355 } else if (rc != -ENOENT) {
356 CERROR("%s: can't open %s: rc = %d\n",
357 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
361 /* No OIs exist, new filesystem, create OI objects */
362 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
363 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
366 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
368 LASSERT((rc & (rc - 1)) == 0);
369 osd->od_oi_table = oi;
370 osd->od_oi_count = rc;
374 cfs_mutex_unlock(&oi_init_lock);
378 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
380 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
382 OBD_FREE(osd->od_oi_table,
383 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
384 osd->od_oi_table = NULL;
387 static inline int fid_is_fs_root(const struct lu_fid *fid)
389 /* Map root inode to special local object FID */
390 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
391 fid_oid(fid) == OSD_FS_ROOT_OID));
394 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
395 struct osd_oi *oi, struct dt_rec *rec,
396 const struct dt_key *key)
398 struct iam_container *bag;
399 struct iam_iterator *it = &oti->oti_idx_it;
400 struct iam_rec *iam_rec;
401 struct iam_path_descr *ipd;
406 LASSERT(oi->oi_inode);
408 bag = &oi->oi_dir.od_container;
409 ipd = osd_idx_ipd_get(oti->oti_env, bag);
413 /* got ipd now we can start iterator. */
414 iam_it_init(it, bag, 0, ipd);
416 rc = iam_it_get(it, (struct iam_key *)key);
418 if (S_ISDIR(oi->oi_inode->i_mode))
419 iam_rec = (struct iam_rec *)oti->oti_ldp;
421 iam_rec = (struct iam_rec *)rec;
423 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
424 if (S_ISDIR(oi->oi_inode->i_mode))
425 osd_fid_unpack((struct lu_fid *)rec,
426 (struct osd_fid_pack *)iam_rec);
430 osd_ipd_put(oti->oti_env, bag, ipd);
432 LINVRNT(osd_invariant(obj));
437 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
438 const struct lu_fid *fid, struct osd_inode_id *id)
440 struct lu_fid *oi_fid = &info->oti_fid;
441 const struct dt_key *key;
444 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG) {
446 rc = osd_compat_objid_lookup(info, osd, fid, id);
447 } else if (fid_is_igif(fid)) {
448 lu_igif_to_id(fid, id);
450 } else if (fid_is_fs_root(fid)) {
451 struct inode *inode = osd_sb(osd)->s_root->d_inode;
453 id->oii_ino = inode->i_ino;
454 id->oii_gen = inode->i_generation;
456 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
457 return osd_compat_spec_lookup(info, osd, fid, id);
459 fid_cpu_to_be(oi_fid, fid);
460 key = (struct dt_key *)oi_fid;
462 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid),
463 (struct dt_rec *)id, key);
466 id->oii_ino = be32_to_cpu(id->oii_ino);
467 id->oii_gen = be32_to_cpu(id->oii_gen);
469 } else if (rc == 0) {
476 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
477 const struct dt_rec *rec, const struct dt_key *key,
478 struct thandle *th, int ignore_quota)
480 struct iam_container *bag;
481 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
482 struct iam_path_descr *ipd;
483 struct osd_thandle *oh;
485 #ifdef HAVE_QUOTA_SUPPORT
486 cfs_cap_t save = cfs_curproc_cap_pack();
491 LASSERT(oi->oi_inode);
493 bag = &oi->oi_dir.od_container;
494 ipd = osd_idx_ipd_get(oti->oti_env, bag);
495 if (unlikely(ipd == NULL))
498 oh = container_of0(th, struct osd_thandle, ot_super);
499 LASSERT(oh->ot_handle != NULL);
500 LASSERT(oh->ot_handle->h_transaction != NULL);
501 #ifdef HAVE_QUOTA_SUPPORT
503 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
505 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
507 if (S_ISDIR(oi->oi_inode->i_mode))
508 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
511 iam_rec = (struct iam_rec *) rec;
512 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
514 #ifdef HAVE_QUOTA_SUPPORT
515 cfs_curproc_cap_unpack(save);
517 osd_ipd_put(oti->oti_env, bag, ipd);
518 LINVRNT(osd_invariant(obj));
522 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
523 const struct lu_fid *fid, const struct osd_inode_id *id0,
524 struct thandle *th, int ignore_quota)
526 struct lu_fid *oi_fid = &info->oti_fid;
527 struct osd_inode_id *id;
528 const struct dt_key *key;
530 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
533 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
534 return osd_compat_objid_insert(info, osd, fid, id0, th);
536 /* Server mount should not depends on OI files */
537 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
538 return osd_compat_spec_insert(info, osd, fid, id0, th);
540 fid_cpu_to_be(oi_fid, fid);
541 key = (struct dt_key *)oi_fid;
544 id->oii_ino = cpu_to_be32(id0->oii_ino);
545 id->oii_gen = cpu_to_be32(id0->oii_gen);
547 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
548 (struct dt_rec *)id, key, th, ignore_quota);
551 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
552 const struct dt_key *key, struct thandle *handle)
554 struct iam_container *bag;
555 struct iam_path_descr *ipd;
556 struct osd_thandle *oh;
562 bag = &oi->oi_dir.od_container;
563 ipd = osd_idx_ipd_get(oti->oti_env, bag);
564 if (unlikely(ipd == NULL))
567 oh = container_of0(handle, struct osd_thandle, ot_super);
568 LASSERT(oh->ot_handle != NULL);
569 LASSERT(oh->ot_handle->h_transaction != NULL);
571 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
572 osd_ipd_put(oti->oti_env, bag, ipd);
573 LINVRNT(osd_invariant(obj));
577 int osd_oi_delete(struct osd_thread_info *info,
578 struct osd_device *osd, const struct lu_fid *fid,
581 struct lu_fid *oi_fid = &info->oti_fid;
582 const struct dt_key *key;
584 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
586 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
587 return osd_compat_objid_delete(info, osd, fid, th);
589 fid_cpu_to_be(oi_fid, fid);
590 key = (struct dt_key *)oi_fid;
592 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid), key, th);
595 int osd_oi_mod_init()
597 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
598 osd_oi_count = OSD_OI_FID_NR;
600 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
601 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
602 osd_oi_count, size_roundup_power2(osd_oi_count));
603 osd_oi_count = size_roundup_power2(osd_oi_count);
606 cfs_mutex_init(&oi_init_lock);