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.
53 # define EXPORT_SYMTAB
55 #define DEBUG_SUBSYSTEM S_MDS
57 #include <linux/module.h>
59 /* LUSTRE_VERSION_CODE */
60 #include <lustre_ver.h>
62 * struct OBD_{ALLOC,FREE}*()
66 #include <obd_support.h>
69 #include <lustre_fid.h>
72 /* osd_lookup(), struct osd_thread_info */
73 #include "osd_internal.h"
75 #include "dt_object.h"
77 #define OSD_OI_FID_NR (1UL << OSD_OI_FID_OID_BITS)
78 #define OSD_OI_FID_NR_MAX (1UL << OSD_OI_FID_OID_BITS_MAX)
80 static unsigned int osd_oi_count = OSD_OI_FID_NR;
81 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
82 "Number of Object Index containers to be created, "
83 "it's only valid for new filesystem.");
85 /** to serialize concurrent OI index initialization */
86 static cfs_mutex_t oi_init_lock;
88 static struct dt_index_features oi_feat = {
89 .dif_flags = DT_IND_UPDATE,
90 .dif_recsize_min = sizeof(struct osd_inode_id),
91 .dif_recsize_max = sizeof(struct osd_inode_id),
95 #define OSD_OI_NAME_BASE "oi.16"
97 static void osd_oi_table_put(struct osd_thread_info *info,
98 struct osd_oi **oi_table, unsigned oi_count)
100 struct iam_container *bag;
103 for (i = 0; i < oi_count; i++) {
104 LASSERT(oi_table[i] != NULL);
105 LASSERT(oi_table[i]->oi_inode != NULL);
107 bag = &(oi_table[i]->oi_dir.od_container);
108 if (bag->ic_object == oi_table[i]->oi_inode)
109 iam_container_fini(bag);
110 iput(oi_table[i]->oi_inode);
111 oi_table[i]->oi_inode = NULL;
112 OBD_FREE_PTR(oi_table[i]);
116 static int osd_oi_index_create_one(struct osd_thread_info *info,
117 struct osd_device *osd, const char *name,
118 struct dt_index_features *feat)
120 const struct lu_env *env = info->oti_env;
121 struct osd_inode_id *id = &info->oti_id;
122 struct buffer_head *bh;
124 struct ldiskfs_dir_entry_2 *de;
125 struct dentry *dentry;
130 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
132 dir = osd_sb(osd)->s_root->d_inode;
133 bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
137 id->oii_ino = le32_to_cpu(de->inode);
138 id->oii_gen = OSD_OII_NOGEN;
140 inode = osd_iget(info, osd, id);
141 if (!IS_ERR(inode)) {
145 RETURN(PTR_ERR(inode));
148 jh = ldiskfs_journal_start_sb(osd_sb(osd), 100);
149 LASSERT(!IS_ERR(jh));
151 inode = ldiskfs_create_inode(jh, osd_sb(osd)->s_root->d_inode,
152 (S_IFREG | S_IRUGO | S_IWUSR));
153 LASSERT(!IS_ERR(inode));
155 if (feat->dif_flags & DT_IND_VARKEY)
156 rc = iam_lvar_create(inode, feat->dif_keysize_max,
157 feat->dif_ptrsize, feat->dif_recsize_max,
160 rc = iam_lfix_create(inode, feat->dif_keysize_max,
161 feat->dif_ptrsize, feat->dif_recsize_max,
164 dentry = osd_child_dentry_by_inode(env, osd_sb(osd)->s_root->d_inode,
166 rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
169 ldiskfs_journal_stop(jh);
175 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
176 struct osd_device *osd,
178 struct dt_index_features *f,
181 struct dentry *dentry;
185 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
187 return (void *) dentry;
189 if (dentry->d_inode) {
190 LASSERT(!is_bad_inode(dentry->d_inode));
191 inode = dentry->d_inode;
192 atomic_inc(&inode->i_count);
199 shrink_dcache_parent(osd_sb(osd)->s_root);
201 return ERR_PTR(-ENOENT);
203 rc = osd_oi_index_create_one(info, osd, name, f);
207 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
209 return (void *) dentry;
211 if (dentry->d_inode) {
212 LASSERT(!is_bad_inode(dentry->d_inode));
213 inode = dentry->d_inode;
214 atomic_inc(&inode->i_count);
219 return ERR_PTR(-ENOENT);
223 * Open an OI(Ojbect Index) container.
225 * \param name Name of OI container
226 * \param objp Pointer of returned OI
229 * \retval -ve failure
231 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
232 char *name, struct osd_oi **oi_slot, bool create)
234 struct osd_directory *dir;
235 struct iam_container *bag;
242 oi_feat.dif_keysize_min = sizeof(struct lu_fid);
243 oi_feat.dif_keysize_max = sizeof(struct lu_fid);
245 inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
247 RETURN(PTR_ERR(inode));
251 GOTO(out_inode, rc = -ENOMEM);
253 oi->oi_inode = inode;
256 bag = &dir->od_container;
257 rc = iam_container_init(bag, &dir->od_descr, inode);
261 rc = iam_container_setup(bag);
263 GOTO(out_container, rc);
269 iam_container_fini(bag);
278 * Open OI(Object Index) table.
279 * If \a oi_count is zero, which means caller doesn't know how many OIs there
280 * will be, this function can either return 0 for new filesystem, or number
281 * of OIs on existed filesystem.
283 * If \a oi_count is non-zero, which means caller does know number of OIs on
284 * filesystem, this function should return the exactly same number on
285 * success, or error code in failure.
287 * \param oi_count Number of expected OI containers
288 * \param create Create OIs if doesn't exist
290 * \retval +ve number of opened OI containers
291 * \retval 0 no OI containers found
292 * \retval -ve failure
295 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
296 struct osd_oi **oi_table, unsigned oi_count, bool create)
298 struct dt_device *dev = &osd->od_dt_dev;
303 /* NB: oi_count != 0 means that we have already created/known all OIs
304 * and have known exact number of OIs. */
305 LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
307 for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
310 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
311 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
317 if (rc == -ENOENT && oi_count == 0)
320 CERROR("%s: can't open %s: rc = %d\n",
321 dev->dd_lu_dev.ld_obd->obd_name, name, rc);
323 CERROR("%s: expect to open total %d OI files.\n",
324 dev->dd_lu_dev.ld_obd->obd_name, oi_count);
330 osd_oi_table_put(info, oi_table, count);
337 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
339 struct dt_device *dev = &osd->od_dt_dev;
343 OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
347 cfs_mutex_lock(&oi_init_lock);
348 /* try to open existing multiple OIs first */
349 rc = osd_oi_table_open(info, osd, oi, 0, false);
353 /* if previous failed then try found single OI from old filesystem */
354 rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
355 if (rc == 0) { /* found single OI from old filesystem */
358 } else if (rc != -ENOENT) {
359 CERROR("%s: can't open %s: rc = %d\n",
360 dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
364 /* No OIs exist, new filesystem, create OI objects */
365 rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
366 LASSERT(ergo(rc >= 0, rc == osd_oi_count));
369 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
371 LASSERT((rc & (rc - 1)) == 0);
372 osd->od_oi_table = oi;
373 osd->od_oi_count = rc;
377 cfs_mutex_unlock(&oi_init_lock);
381 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
383 osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
385 OBD_FREE(osd->od_oi_table,
386 sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
387 osd->od_oi_table = NULL;
390 static inline int fid_is_fs_root(const struct lu_fid *fid)
392 /* Map root inode to special local object FID */
393 return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
394 fid_oid(fid) == OSD_FS_ROOT_OID));
397 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
398 struct osd_oi *oi, struct dt_rec *rec,
399 const struct dt_key *key)
401 struct iam_container *bag;
402 struct iam_iterator *it = &oti->oti_idx_it;
403 struct iam_rec *iam_rec;
404 struct iam_path_descr *ipd;
409 LASSERT(oi->oi_inode);
411 bag = &oi->oi_dir.od_container;
412 ipd = osd_idx_ipd_get(oti->oti_env, bag);
416 /* got ipd now we can start iterator. */
417 iam_it_init(it, bag, 0, ipd);
419 rc = iam_it_get(it, (struct iam_key *)key);
421 if (S_ISDIR(oi->oi_inode->i_mode))
422 iam_rec = (struct iam_rec *)oti->oti_ldp;
424 iam_rec = (struct iam_rec *)rec;
426 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
427 if (S_ISDIR(oi->oi_inode->i_mode))
428 osd_fid_unpack((struct lu_fid *)rec,
429 (struct osd_fid_pack *)iam_rec);
433 osd_ipd_put(oti->oti_env, bag, ipd);
435 LINVRNT(osd_invariant(obj));
440 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
441 const struct lu_fid *fid, struct osd_inode_id *id)
443 struct lu_fid *oi_fid = &info->oti_fid;
444 const struct dt_key *key;
447 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG) {
449 rc = osd_compat_objid_lookup(info, osd, fid, id);
450 } else if (fid_is_igif(fid)) {
451 lu_igif_to_id(fid, id);
453 } else if (fid_is_fs_root(fid)) {
454 struct inode *inode = osd_sb(osd)->s_root->d_inode;
456 id->oii_ino = inode->i_ino;
457 id->oii_gen = inode->i_generation;
459 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
460 return osd_compat_spec_lookup(info, osd, fid, id);
462 fid_cpu_to_be(oi_fid, fid);
463 key = (struct dt_key *)oi_fid;
465 rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid),
466 (struct dt_rec *)id, key);
469 id->oii_ino = be32_to_cpu(id->oii_ino);
470 id->oii_gen = be32_to_cpu(id->oii_gen);
472 } else if (rc == 0) {
479 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
480 const struct dt_rec *rec, const struct dt_key *key,
481 struct thandle *th, int ignore_quota)
483 struct iam_container *bag;
484 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
485 struct iam_path_descr *ipd;
486 struct osd_thandle *oh;
488 #ifdef HAVE_QUOTA_SUPPORT
489 cfs_cap_t save = cfs_curproc_cap_pack();
494 LASSERT(oi->oi_inode);
496 bag = &oi->oi_dir.od_container;
497 ipd = osd_idx_ipd_get(oti->oti_env, bag);
498 if (unlikely(ipd == NULL))
501 oh = container_of0(th, struct osd_thandle, ot_super);
502 LASSERT(oh->ot_handle != NULL);
503 LASSERT(oh->ot_handle->h_transaction != NULL);
504 #ifdef HAVE_QUOTA_SUPPORT
506 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
508 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
510 if (S_ISDIR(oi->oi_inode->i_mode))
511 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
514 iam_rec = (struct iam_rec *) rec;
515 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
517 #ifdef HAVE_QUOTA_SUPPORT
518 cfs_curproc_cap_unpack(save);
520 osd_ipd_put(oti->oti_env, bag, ipd);
521 LINVRNT(osd_invariant(obj));
525 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
526 const struct lu_fid *fid, const struct osd_inode_id *id0,
527 struct thandle *th, int ignore_quota)
529 struct lu_fid *oi_fid = &info->oti_fid;
530 struct osd_inode_id *id;
531 const struct dt_key *key;
533 if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
536 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
537 return osd_compat_objid_insert(info, osd, fid, id0, th);
539 /* Server mount should not depends on OI files */
540 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
541 return osd_compat_spec_insert(info, osd, fid, id0, th);
543 fid_cpu_to_be(oi_fid, fid);
544 key = (struct dt_key *)oi_fid;
547 id->oii_ino = cpu_to_be32(id0->oii_ino);
548 id->oii_gen = cpu_to_be32(id0->oii_gen);
550 return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
551 (struct dt_rec *)id, key, th, ignore_quota);
554 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
555 const struct dt_key *key, struct thandle *handle)
557 struct iam_container *bag;
558 struct iam_path_descr *ipd;
559 struct osd_thandle *oh;
565 bag = &oi->oi_dir.od_container;
566 ipd = osd_idx_ipd_get(oti->oti_env, bag);
567 if (unlikely(ipd == NULL))
570 oh = container_of0(handle, struct osd_thandle, ot_super);
571 LASSERT(oh->ot_handle != NULL);
572 LASSERT(oh->ot_handle->h_transaction != NULL);
574 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
575 osd_ipd_put(oti->oti_env, bag, ipd);
576 LINVRNT(osd_invariant(obj));
580 int osd_oi_delete(struct osd_thread_info *info,
581 struct osd_device *osd, const struct lu_fid *fid,
584 struct lu_fid *oi_fid = &info->oti_fid;
585 const struct dt_key *key;
587 LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
589 if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
590 return osd_compat_objid_delete(info, osd, fid, th);
592 fid_cpu_to_be(oi_fid, fid);
593 key = (struct dt_key *)oi_fid;
595 return osd_oi_iam_delete(info, osd_fid2oi(osd, fid), key, th);
598 int osd_oi_mod_init()
600 if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
601 osd_oi_count = OSD_OI_FID_NR;
603 if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
604 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
605 osd_oi_count, size_roundup_power2(osd_oi_count));
606 osd_oi_count = size_roundup_power2(osd_oi_count);
609 cfs_mutex_init(&oi_init_lock);