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) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_compat.c
38 * on-disk structure for managing /O
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
53 * struct OBD_{ALLOC,FREE}*()
56 #include <obd_support.h>
58 #include "osd_internal.h"
61 static void osd_push_ctxt(const struct osd_device *dev,
62 struct lvfs_run_ctxt *newctxt,
63 struct lvfs_run_ctxt *save)
65 OBD_SET_CTXT_MAGIC(newctxt);
66 newctxt->pwdmnt = dev->od_mnt;
67 newctxt->pwd = dev->od_mnt->mnt_root;
68 newctxt->fs = get_ds();
70 push_ctxt(save, newctxt, NULL);
73 static void osd_pop_ctxt(const struct osd_device *dev,
74 struct lvfs_run_ctxt *new,
75 struct lvfs_run_ctxt *save)
77 pop_ctxt(save, new, NULL);
80 /* utility to make a directory */
81 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
82 const char *name, int mode, int fix)
84 struct dentry *dchild;
88 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
89 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
90 dchild = ll_lookup_one_len(name, dir, strlen(name));
94 if (dchild->d_inode) {
95 int old_mode = dchild->d_inode->i_mode;
96 if (!S_ISDIR(old_mode)) {
97 CERROR("found %s (%lu/%u) is mode %o\n", name,
98 dchild->d_inode->i_ino,
99 dchild->d_inode->i_generation, old_mode);
100 GOTO(out_err, err = -ENOTDIR);
103 /* Fixup directory permissions if necessary */
104 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
106 "fixing permissions on %s from %o to %o\n",
107 name, old_mode, mode);
108 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
109 (old_mode & ~S_IALLUGO);
110 mark_inode_dirty(dchild->d_inode);
112 GOTO(out_up, dchild);
115 err = vfs_mkdir(dir->d_inode, dchild, mode);
123 dchild = ERR_PTR(err);
128 int osd_last_rcvd_subdir_count(struct osd_device *osd)
130 struct lr_server_data lsd;
131 struct dentry *dlast;
134 int count = OBJ_SUBDIR_COUNT;
138 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
141 return PTR_ERR(dlast);
142 else if (dlast->d_inode == NULL)
146 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
147 if (rc == sizeof(lsd)) {
148 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
149 "subdir count = %d\n", lsd.lsd_uuid,
150 lsd.lsd_subdir_count);
151 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
152 count = le16_to_cpu(lsd.lsd_subdir_count);
153 } else if (rc != 0) {
154 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
166 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
167 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
169 struct lvfs_run_ctxt new;
170 struct lvfs_run_ctxt save;
171 struct dentry *parent;
172 struct osd_mdobj_map *omm;
174 struct osd_thread_info *info = osd_oti_get(env);
175 struct lu_fid *fid = &info->oti_fid3;
179 OBD_ALLOC_PTR(dev->od_mdt_map);
180 if (dev->od_mdt_map == NULL)
183 omm = dev->od_mdt_map;
185 parent = osd_sb(dev)->s_root;
186 osd_push_ctxt(dev, &new, &save);
188 d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
191 GOTO(cleanup, rc = PTR_ERR(d));
193 ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
194 omm->omm_remote_parent = d;
196 /* Set LMA for remote parent inode */
197 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
198 rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
203 pop_ctxt(&save, &new, NULL);
205 if (omm->omm_remote_parent != NULL)
206 dput(omm->omm_remote_parent);
208 dev->od_mdt_map = NULL;
213 static void osd_mdt_fini(struct osd_device *osd)
215 struct osd_mdobj_map *omm = osd->od_mdt_map;
220 if (omm->omm_remote_parent)
221 dput(omm->omm_remote_parent);
224 osd->od_ost_map = NULL;
227 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
228 struct osd_object *obj, struct osd_thandle *oh)
230 struct osd_mdobj_map *omm = osd->od_mdt_map;
231 struct osd_thread_info *oti = osd_oti_get(env);
232 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
233 char *name = oti->oti_name;
234 struct dentry *dentry;
235 struct dentry *parent;
238 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
239 * can identify this object quickly */
240 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
244 lma->lma_incompat |= LMAI_REMOTE_PARENT;
245 lustre_lma_swab(lma);
246 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
247 sizeof(*lma), XATTR_REPLACE);
251 parent = omm->omm_remote_parent;
252 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
253 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
255 mutex_lock(&parent->d_inode->i_mutex);
256 rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
258 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
259 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
260 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
261 mark_inode_dirty(parent->d_inode);
262 mutex_unlock(&parent->d_inode->i_mutex);
266 int osd_delete_from_remote_parent(const struct lu_env *env,
267 struct osd_device *osd,
268 struct osd_object *obj,
269 struct osd_thandle *oh)
271 struct osd_mdobj_map *omm = osd->od_mdt_map;
272 struct osd_thread_info *oti = osd_oti_get(env);
273 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
274 char *name = oti->oti_name;
275 struct dentry *dentry;
276 struct dentry *parent;
277 struct ldiskfs_dir_entry_2 *de;
278 struct buffer_head *bh;
281 /* Check lma to see whether it is remote object */
282 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
286 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
289 parent = omm->omm_remote_parent;
290 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
291 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
293 mutex_lock(&parent->d_inode->i_mutex);
294 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
297 mutex_unlock(&parent->d_inode->i_mutex);
300 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
301 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
302 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
303 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
304 mark_inode_dirty(parent->d_inode);
305 mutex_unlock(&parent->d_inode->i_mutex);
308 /* Get rid of REMOTE_PARENT flag from incompat */
309 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
310 lustre_lma_swab(lma);
311 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
312 sizeof(*lma), XATTR_REPLACE);
316 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
317 struct osd_device *osd,
318 const struct lu_fid *fid,
319 struct osd_inode_id *id)
321 struct osd_mdobj_map *omm = osd->od_mdt_map;
322 char *name = oti->oti_name;
323 struct dentry *parent;
324 struct dentry *dentry;
325 struct ldiskfs_dir_entry_2 *de;
326 struct buffer_head *bh;
330 parent = omm->omm_remote_parent;
331 sprintf(name, DFID_NOBRACE, PFID(fid));
332 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
334 mutex_lock(&parent->d_inode->i_mutex);
335 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
341 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
344 mutex_unlock(&parent->d_inode->i_mutex);
346 osd_add_oi_cache(oti, osd, id, fid);
351 * directory structure on legacy OST:
353 * O/<seq>/d0-31/<objid>
360 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
362 struct lvfs_run_ctxt new;
363 struct lvfs_run_ctxt save;
364 struct dentry *rootd = osd_sb(dev)->s_root;
366 struct osd_thread_info *info = osd_oti_get(env);
368 struct lu_fid *fid = &info->oti_fid3;
372 OBD_ALLOC_PTR(dev->od_ost_map);
373 if (dev->od_ost_map == NULL)
376 /* to get subdir count from last_rcvd */
377 rc = osd_last_rcvd_subdir_count(dev);
379 OBD_FREE_PTR(dev->od_ost_map);
383 dev->od_ost_map->om_subdir_count = rc;
386 CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
387 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
388 sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
390 osd_push_ctxt(dev, &new, &save);
392 d = ll_lookup_one_len("O", rootd, strlen("O"));
394 GOTO(cleanup, rc = PTR_ERR(d));
395 if (d->d_inode == NULL) {
397 /* The lookup() may be called again inside simple_mkdir().
398 * Since the repeated lookup() only be called for "/O" at
399 * mount time, it will not affect the whole performance. */
400 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
402 GOTO(cleanup, rc = PTR_ERR(d));
404 /* It is quite probably that the device is new formatted. */
405 dev->od_maybe_new = 1;
409 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
410 dev->od_ost_map->om_root = d;
412 /* 'What the @fid is' is not imporatant, because the object
413 * has no OI mapping, and only is visible inside the OSD.*/
414 lu_igif_build(fid, inode->i_ino, inode->i_generation);
415 rc = osd_ea_fid_set(info, inode, fid,
416 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
421 osd_pop_ctxt(dev, &new, &save);
423 OBD_FREE_PTR(dev->od_ost_map);
429 static void osd_seq_free(struct osd_obj_map *map,
430 struct osd_obj_seq *osd_seq)
434 cfs_list_del_init(&osd_seq->oos_seq_list);
436 if (osd_seq->oos_dirs) {
437 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
438 if (osd_seq->oos_dirs[j])
439 dput(osd_seq->oos_dirs[j]);
441 OBD_FREE(osd_seq->oos_dirs,
442 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
445 if (osd_seq->oos_root)
446 dput(osd_seq->oos_root);
448 OBD_FREE_PTR(osd_seq);
451 static void osd_ost_fini(struct osd_device *osd)
453 struct osd_obj_seq *osd_seq;
454 struct osd_obj_seq *tmp;
455 struct osd_obj_map *map = osd->od_ost_map;
461 write_lock(&map->om_seq_list_lock);
462 cfs_list_for_each_entry_safe(osd_seq, tmp,
465 osd_seq_free(map, osd_seq);
467 write_unlock(&map->om_seq_list_lock);
471 osd->od_ost_map = NULL;
475 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
480 /* prepare structures for OST */
481 rc = osd_ost_init(env, dev);
485 /* prepare structures for MDS */
486 rc = osd_mdt_init(env, dev);
491 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
493 struct osd_obj_seq *osd_seq;
495 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
496 if (osd_seq->oos_seq == seq)
502 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
504 struct osd_obj_seq *osd_seq;
506 read_lock(&map->om_seq_list_lock);
507 osd_seq = osd_seq_find_locked(map, seq);
508 read_unlock(&map->om_seq_list_lock);
512 void osd_obj_map_fini(struct osd_device *dev)
519 * Update the specified OI mapping.
521 * \retval 1, changed nothing
522 * \retval 0, changed successfully
523 * \retval -ve, on error
525 static int osd_obj_update_entry(struct osd_thread_info *info,
526 struct osd_device *osd,
527 struct dentry *dir, const char *name,
528 const struct lu_fid *fid,
529 const struct osd_inode_id *id,
532 struct inode *parent = dir->d_inode;
533 struct dentry *child;
534 struct ldiskfs_dir_entry_2 *de;
535 struct buffer_head *bh;
537 struct dentry *dentry = &info->oti_obj_dentry;
538 struct osd_inode_id *oi_id = &info->oti_id3;
539 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
540 struct lu_fid *oi_fid = &lma->lma_self_fid;
545 LASSERT(th->h_transaction != NULL);
547 child = &info->oti_child_dentry;
548 child->d_parent = dir;
549 child->d_name.hash = 0;
550 child->d_name.name = name;
551 child->d_name.len = strlen(name);
553 ll_vfs_dq_init(parent);
554 mutex_lock(&parent->i_mutex);
555 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
557 GOTO(out, rc = -ENOENT);
559 if (le32_to_cpu(de->inode) == id->oii_ino)
562 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
563 inode = osd_iget(info, osd, oi_id);
566 if (rc == -ENOENT || rc == -ESTALE)
571 rc = osd_get_lma(info, inode, dentry, lma);
572 if (rc == -ENODATA) {
573 rc = osd_get_idif(info, inode, dentry, oi_fid);
581 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
582 * either a crashed object or a uninitialized one. Replace it. */
583 if (rc == -ENODATA || oi_fid == NULL)
589 if (lu_fid_eq(fid, oi_fid)) {
590 CERROR("%s: the FID "DFID" is used by two objects: "
591 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
592 oi_id->oii_ino, oi_id->oii_gen,
593 id->oii_ino, id->oii_gen);
594 GOTO(out, rc = -EEXIST);
598 /* There may be temporary inconsistency: On one hand, the new
599 * object may be referenced by multiple entries, which is out
600 * of our control unless we traverse the whole /O completely,
601 * which is non-flat order and inefficient, should be avoided;
602 * On the other hand, the old object may become orphan if it
603 * is still valid. Since it was referenced by an invalid entry,
604 * making it as invisible temporary may be not worse. OI scrub
605 * will process it later. */
606 rc = ldiskfs_journal_get_write_access(th, bh);
610 de->inode = cpu_to_le32(id->oii_ino);
611 rc = ldiskfs_journal_dirty_metadata(th, bh);
617 mutex_unlock(&parent->i_mutex);
621 static int osd_obj_del_entry(struct osd_thread_info *info,
622 struct osd_device *osd,
623 struct dentry *dird, char *name,
626 struct ldiskfs_dir_entry_2 *de;
627 struct buffer_head *bh;
628 struct dentry *child;
629 struct inode *dir = dird->d_inode;
634 LASSERT(th->h_transaction != NULL);
637 child = &info->oti_child_dentry;
638 child->d_name.hash = 0;
639 child->d_name.name = name;
640 child->d_name.len = strlen(name);
641 child->d_parent = dird;
642 child->d_inode = NULL;
645 mutex_lock(&dir->i_mutex);
647 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
649 rc = ldiskfs_delete_entry(th, dir, de, bh);
652 mutex_unlock(&dir->i_mutex);
657 int osd_obj_add_entry(struct osd_thread_info *info,
658 struct osd_device *osd,
659 struct dentry *dir, char *name,
660 const struct osd_inode_id *id,
663 struct dentry *child;
669 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
673 LASSERT(th->h_transaction != NULL);
675 inode = &info->oti_inode;
676 inode->i_sb = osd_sb(osd);
677 osd_id_to_inode(inode, id);
678 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
680 child = &info->oti_child_dentry;
681 child->d_name.hash = 0;
682 child->d_name.name = name;
683 child->d_name.len = strlen(name);
684 child->d_parent = dir;
685 child->d_inode = inode;
687 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
690 ll_vfs_dq_init(dir->d_inode);
691 mutex_lock(&dir->d_inode->i_mutex);
692 rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
693 mutex_unlock(&dir->d_inode->i_mutex);
699 * Use LPU64 for legacy OST sequences, but use LPX64i for new
700 * sequences names, so that the O/{seq}/dN/{oid} more closely
701 * follows the DFID/PFID format. This makes it easier to map from
702 * debug messages to objects in the future, and the legacy space
703 * of FID_SEQ_OST_MDT0 will be unused in the future.
705 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
707 snprintf(seq_name, name_size,
708 (fid_seq_is_rsvd(seq) ||
709 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
710 fid_seq_is_idif(seq) ? 0 : seq);
713 static inline void osd_oid_name(char *name, size_t name_size,
714 const struct lu_fid *fid, obd_id id)
716 snprintf(name, name_size,
717 (fid_seq_is_rsvd(fid_seq(fid)) ||
718 fid_seq_is_mdt0(fid_seq(fid)) ||
719 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
722 /* external locking is required */
723 static int osd_seq_load_locked(struct osd_thread_info *info,
724 struct osd_device *osd,
725 struct osd_obj_seq *osd_seq)
727 struct osd_obj_map *map = osd->od_ost_map;
728 struct dentry *seq_dir;
730 struct lu_fid *fid = &info->oti_fid3;
736 if (osd_seq->oos_root != NULL)
740 LASSERT(map->om_root);
742 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
744 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
746 GOTO(out_err, rc = PTR_ERR(seq_dir));
747 else if (seq_dir->d_inode == NULL)
748 GOTO(out_put, rc = -EFAULT);
750 inode = seq_dir->d_inode;
751 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
752 osd_seq->oos_root = seq_dir;
754 /* 'What the @fid is' is not imporatant, because the object
755 * has no OI mapping, and only is visible inside the OSD.*/
756 lu_igif_build(fid, inode->i_ino, inode->i_generation);
757 rc = osd_ea_fid_set(info, inode, fid,
758 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
762 LASSERT(osd_seq->oos_dirs == NULL);
763 OBD_ALLOC(osd_seq->oos_dirs,
764 sizeof(seq_dir) * osd_seq->oos_subdir_count);
765 if (osd_seq->oos_dirs == NULL)
766 GOTO(out_put, rc = -ENOMEM);
768 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
771 snprintf(dir_name, sizeof(dir_name), "d%u", i);
772 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
775 GOTO(out_free, rc = PTR_ERR(dir));
776 } else if (dir->d_inode == NULL) {
778 GOTO(out_free, rc = -EFAULT);
781 inode = dir->d_inode;
782 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
783 osd_seq->oos_dirs[i] = dir;
785 /* 'What the @fid is' is not imporatant, because the object
786 * has no OI mapping, and only is visible inside the OSD.*/
787 lu_igif_build(fid, inode->i_ino, inode->i_generation);
788 rc = osd_ea_fid_set(info, inode, fid,
789 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
796 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
797 if (osd_seq->oos_dirs[i] != NULL)
798 dput(osd_seq->oos_dirs[i]);
800 OBD_FREE(osd_seq->oos_dirs,
801 sizeof(seq_dir) * osd_seq->oos_subdir_count);
804 osd_seq->oos_root = NULL;
810 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
811 struct osd_device *osd, obd_seq seq)
813 struct osd_obj_map *map;
814 struct osd_obj_seq *osd_seq;
818 map = osd->od_ost_map;
820 LASSERT(map->om_root);
822 osd_seq = osd_seq_find(map, seq);
823 if (likely(osd_seq != NULL))
826 /* Serializing init process */
827 down(&map->om_dir_init_sem);
829 /* Check whether the seq has been added */
830 read_lock(&map->om_seq_list_lock);
831 osd_seq = osd_seq_find_locked(map, seq);
832 if (osd_seq != NULL) {
833 read_unlock(&map->om_seq_list_lock);
834 GOTO(cleanup, rc = 0);
836 read_unlock(&map->om_seq_list_lock);
838 OBD_ALLOC_PTR(osd_seq);
840 GOTO(cleanup, rc = -ENOMEM);
842 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
843 osd_seq->oos_seq = seq;
844 /* Init subdir count to be 32, but each seq can have
845 * different subdir count */
846 osd_seq->oos_subdir_count = map->om_subdir_count;
847 rc = osd_seq_load_locked(info, osd, osd_seq);
851 write_lock(&map->om_seq_list_lock);
852 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
853 write_unlock(&map->om_seq_list_lock);
856 up(&map->om_dir_init_sem);
859 OBD_FREE_PTR(osd_seq);
866 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
867 const struct lu_fid *fid, struct osd_inode_id *id)
869 struct osd_obj_map *map;
870 struct osd_obj_seq *osd_seq;
871 struct dentry *d_seq;
872 struct dentry *child;
873 struct ost_id *ostid = &info->oti_ostid;
876 struct ldiskfs_dir_entry_2 *de;
877 struct buffer_head *bh;
882 /* on the very first lookup we find and open directories */
884 map = dev->od_ost_map;
886 LASSERT(map->om_root);
888 fid_to_ostid(fid, ostid);
889 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
891 RETURN(PTR_ERR(osd_seq));
893 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
894 d_seq = osd_seq->oos_dirs[dirn];
897 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
899 child = &info->oti_child_dentry;
900 child->d_parent = d_seq;
901 child->d_name.hash = 0;
902 child->d_name.name = name;
903 /* XXX: we can use rc from sprintf() instead of strlen() */
904 child->d_name.len = strlen(name);
906 dir = d_seq->d_inode;
907 mutex_lock(&dir->i_mutex);
908 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
909 mutex_unlock(&dir->i_mutex);
914 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
917 inode = osd_iget(info, dev, id);
919 RETURN(PTR_ERR(inode));
925 int osd_obj_map_insert(struct osd_thread_info *info,
926 struct osd_device *osd,
927 const struct lu_fid *fid,
928 const struct osd_inode_id *id,
931 struct osd_obj_map *map;
932 struct osd_obj_seq *osd_seq;
934 struct ost_id *ostid = &info->oti_ostid;
940 map = osd->od_ost_map;
943 /* map fid to seq:objid */
944 fid_to_ostid(fid, ostid);
946 oid = ostid_id(ostid);
947 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
949 RETURN(PTR_ERR(osd_seq));
951 dirn = oid & (osd_seq->oos_subdir_count - 1);
952 d = osd_seq->oos_dirs[dirn];
955 osd_oid_name(name, sizeof(name), fid, oid);
958 rc = osd_obj_add_entry(info, osd, d, name, id, th);
960 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
961 if (unlikely(rc == -ENOENT))
964 if (unlikely(rc == 1))
971 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
972 const struct lu_fid *fid, handle_t *th)
974 struct osd_obj_map *map;
975 struct osd_obj_seq *osd_seq;
977 struct ost_id *ostid = &info->oti_ostid;
982 map = osd->od_ost_map;
985 /* map fid to seq:objid */
986 fid_to_ostid(fid, ostid);
988 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
990 GOTO(cleanup, rc = PTR_ERR(osd_seq));
992 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
993 d = osd_seq->oos_dirs[dirn];
996 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
997 rc = osd_obj_del_entry(info, osd, d, name, th);
1002 int osd_obj_map_update(struct osd_thread_info *info,
1003 struct osd_device *osd,
1004 const struct lu_fid *fid,
1005 const struct osd_inode_id *id,
1008 struct osd_obj_seq *osd_seq;
1010 struct ost_id *ostid = &info->oti_ostid;
1015 fid_to_ostid(fid, ostid);
1016 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1017 if (IS_ERR(osd_seq))
1018 RETURN(PTR_ERR(osd_seq));
1020 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1021 d = osd_seq->oos_dirs[dirn];
1024 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1025 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1030 int osd_obj_map_recover(struct osd_thread_info *info,
1031 struct osd_device *osd,
1032 struct inode *src_parent,
1033 struct dentry *src_child,
1034 const struct lu_fid *fid)
1036 struct osd_obj_seq *osd_seq;
1037 struct dentry *tgt_parent;
1038 struct dentry *tgt_child = &info->oti_child_dentry;
1040 struct inode *inode = src_child->d_inode;
1041 struct ost_id *ostid = &info->oti_ostid;
1043 struct ldiskfs_dir_entry_2 *de;
1044 struct buffer_head *bh;
1050 if (fid_is_last_id(fid)) {
1051 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1052 if (IS_ERR(osd_seq))
1053 RETURN(PTR_ERR(osd_seq));
1055 tgt_parent = osd_seq->oos_root;
1056 tgt_child->d_name.name = "LAST_ID";
1057 tgt_child->d_name.len = strlen("LAST_ID");
1059 fid_to_ostid(fid, ostid);
1060 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1061 if (IS_ERR(osd_seq))
1062 RETURN(PTR_ERR(osd_seq));
1064 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1065 tgt_parent = osd_seq->oos_dirs[dirn];
1066 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1067 tgt_child->d_name.name = name;
1068 tgt_child->d_name.len = strlen(name);
1070 LASSERT(tgt_parent != NULL);
1072 dir = tgt_parent->d_inode;
1073 tgt_child->d_name.hash = 0;
1074 tgt_child->d_parent = tgt_parent;
1075 tgt_child->d_inode = inode;
1077 /* The non-initialized src_child may be destroyed. */
1078 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1079 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1080 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1081 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1083 RETURN(PTR_ERR(jh));
1085 ll_vfs_dq_init(src_parent);
1086 ll_vfs_dq_init(dir);
1088 mutex_lock(&src_parent->i_mutex);
1089 mutex_lock(&dir->i_mutex);
1090 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1092 /* XXX: If some other object occupied the same slot. And If such
1093 * inode is zero-sized and with SUID+SGID, then means it is
1094 * a new created one. Maybe we can remove it and insert the
1095 * original one back to the /O/<seq>/d<x>. But there are
1096 * something to be considered:
1098 * 1) The OST-object under /lost+found has crashed LMA.
1099 * So it should not conflict with the current one.
1101 * 2) There are race conditions that: someone may just want
1102 * to modify the current one. Even if the OI scrub takes
1103 * the object lock when remove the current one, it still
1104 * cause the modification to be lost becasue the target
1105 * has been removed when the RPC service thread waiting
1108 * So keep it there before we have suitable solution. */
1110 mutex_unlock(&dir->i_mutex);
1111 mutex_unlock(&src_parent->i_mutex);
1112 ldiskfs_journal_stop(jh);
1115 /* If the src object has never been modified, then remove it. */
1116 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1117 inode->i_mode & S_ISGID) {
1118 rc = vfs_unlink(src_parent, src_child);
1119 if (unlikely(rc == -ENOENT))
1125 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1127 if (unlikely(bh == NULL))
1128 GOTO(unlock, rc = -ENOENT);
1130 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1135 rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1140 mutex_unlock(&dir->i_mutex);
1141 mutex_unlock(&src_parent->i_mutex);
1142 ldiskfs_journal_stop(jh);
1146 static struct dentry *
1147 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1148 const struct lu_fid *fid, char **name)
1150 struct dentry *root = ERR_PTR(-ENOENT);
1152 if (fid_is_last_id(fid)) {
1153 struct osd_obj_seq *osd_seq;
1155 /* on creation of LAST_ID we create O/<seq> hierarchy */
1156 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1157 if (IS_ERR(osd_seq))
1158 RETURN((struct dentry *)osd_seq);
1161 root = osd_seq->oos_root;
1163 *name = osd_lf_fid2name(fid);
1165 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1166 else if ((*name)[0])
1167 root = osd_sb(osd)->s_root;
1173 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1174 const struct lu_fid *fid, const struct osd_inode_id *id,
1177 struct dentry *root;
1182 root = osd_object_spec_find(info, osd, fid, &name);
1183 if (!IS_ERR(root)) {
1184 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1194 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1195 const struct lu_fid *fid, const struct osd_inode_id *id,
1198 struct dentry *root;
1203 root = osd_object_spec_find(info, osd, fid, &name);
1204 if (!IS_ERR(root)) {
1205 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1215 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1216 const struct lu_fid *fid, struct osd_inode_id *id)
1218 struct dentry *root;
1219 struct dentry *dentry;
1220 struct inode *inode;
1225 if (fid_is_last_id(fid)) {
1226 struct osd_obj_seq *osd_seq;
1228 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1229 if (IS_ERR(osd_seq))
1230 RETURN(PTR_ERR(osd_seq));
1231 root = osd_seq->oos_root;
1234 root = osd_sb(osd)->s_root;
1235 name = osd_lf_fid2name(fid);
1236 if (name == NULL || strlen(name) == 0)
1240 dentry = ll_lookup_one_len(name, root, strlen(name));
1241 if (!IS_ERR(dentry)) {
1242 inode = dentry->d_inode;
1244 if (is_bad_inode(inode)) {
1247 osd_id_gen(id, inode->i_ino,
1248 inode->i_generation);
1252 /* if dentry is accessible after osd_compat_spec_insert it
1253 * will still contain NULL inode, so don't keep it in cache */
1254 d_invalidate(dentry);