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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd/osd_compat.c
34 * on-disk structure for managing /O
36 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 /* LUSTRE_VERSION_CODE */
40 #include <lustre_ver.h>
41 /* prerequisite for linux/xattr.h */
42 #include <linux/types.h>
43 /* prerequisite for linux/xattr.h */
45 /* XATTR_{REPLACE,CREATE} */
46 #include <linux/xattr.h>
49 * struct OBD_{ALLOC,FREE}*()
52 #include <obd_support.h>
54 #include "osd_internal.h"
57 static void osd_push_ctxt(const struct osd_device *dev,
58 struct lvfs_run_ctxt *newctxt,
59 struct lvfs_run_ctxt *save)
61 OBD_SET_CTXT_MAGIC(newctxt);
62 newctxt->pwdmnt = dev->od_mnt;
63 newctxt->pwd = dev->od_mnt->mnt_root;
64 newctxt->fs = get_ds();
65 newctxt->umask = current_umask();
68 push_ctxt(save, newctxt);
71 /* utility to make a directory */
72 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
73 const char *name, int mode, int fix)
75 struct dentry *dchild;
79 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
80 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
81 dchild = ll_lookup_one_len(name, dir, strlen(name));
85 if (dchild->d_inode) {
86 int old_mode = dchild->d_inode->i_mode;
87 if (!S_ISDIR(old_mode)) {
88 CERROR("found %s (%lu/%u) is mode %o\n", name,
89 dchild->d_inode->i_ino,
90 dchild->d_inode->i_generation, old_mode);
91 GOTO(out_err, err = -ENOTDIR);
94 /* Fixup directory permissions if necessary */
95 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
97 "fixing permissions on %s from %o to %o\n",
98 name, old_mode, mode);
99 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
100 (old_mode & ~S_IALLUGO);
101 mark_inode_dirty(dchild->d_inode);
103 GOTO(out_up, dchild);
106 err = vfs_mkdir(dir->d_inode, dchild, mode);
114 dchild = ERR_PTR(err);
119 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
121 struct lr_server_data lsd;
122 struct dentry *dlast;
125 int count = OBJ_SUBDIR_COUNT;
129 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
132 return PTR_ERR(dlast);
133 else if (dlast->d_inode == NULL)
137 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
138 if (rc == sizeof(lsd)) {
139 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
140 "subdir count = %d\n", lsd.lsd_uuid,
141 lsd.lsd_subdir_count);
142 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
143 count = le16_to_cpu(lsd.lsd_subdir_count);
144 } else if (rc != 0) {
145 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
157 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
158 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
160 struct lvfs_run_ctxt new;
161 struct lvfs_run_ctxt save;
162 struct dentry *parent;
163 struct osd_mdobj_map *omm;
165 struct osd_thread_info *info = osd_oti_get(env);
166 struct lu_fid *fid = &info->oti_fid3;
170 OBD_ALLOC_PTR(dev->od_mdt_map);
171 if (dev->od_mdt_map == NULL)
174 omm = dev->od_mdt_map;
176 parent = osd_sb(dev)->s_root;
177 osd_push_ctxt(dev, &new, &save);
179 d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
182 GOTO(cleanup, rc = PTR_ERR(d));
184 omm->omm_remote_parent = d;
186 /* Set LMA for remote parent inode */
187 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
188 rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
193 pop_ctxt(&save, &new);
195 if (omm->omm_remote_parent != NULL)
196 dput(omm->omm_remote_parent);
198 dev->od_mdt_map = NULL;
203 static void osd_mdt_fini(struct osd_device *osd)
205 struct osd_mdobj_map *omm = osd->od_mdt_map;
210 if (omm->omm_remote_parent)
211 dput(omm->omm_remote_parent);
214 osd->od_ost_map = NULL;
217 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
218 struct osd_object *obj, struct osd_thandle *oh)
220 struct osd_mdobj_map *omm = osd->od_mdt_map;
221 struct osd_thread_info *oti = osd_oti_get(env);
222 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
223 char *name = oti->oti_name;
224 struct osd_thread_info *info = osd_oti_get(env);
225 struct dentry *dentry;
226 struct dentry *parent;
229 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
230 * can identify this object quickly */
231 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
232 &oti->oti_ost_attrs);
236 lma->lma_incompat |= LMAI_REMOTE_PARENT;
237 lustre_lma_swab(lma);
238 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
239 sizeof(*lma), XATTR_REPLACE);
243 parent = omm->omm_remote_parent;
244 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
245 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
247 mutex_lock(&parent->d_inode->i_mutex);
248 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
249 obj->oo_inode, NULL);
250 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
251 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
252 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
253 mark_inode_dirty(parent->d_inode);
254 mutex_unlock(&parent->d_inode->i_mutex);
258 int osd_delete_from_remote_parent(const struct lu_env *env,
259 struct osd_device *osd,
260 struct osd_object *obj,
261 struct osd_thandle *oh)
263 struct osd_mdobj_map *omm = osd->od_mdt_map;
264 struct osd_thread_info *oti = osd_oti_get(env);
265 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
266 char *name = oti->oti_name;
267 struct dentry *dentry;
268 struct dentry *parent;
269 struct ldiskfs_dir_entry_2 *de;
270 struct buffer_head *bh;
273 /* Check lma to see whether it is remote object */
274 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
275 &oti->oti_ost_attrs);
277 /* No LMA if the directory is created before 2.0 */
283 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
286 parent = omm->omm_remote_parent;
287 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
288 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
290 mutex_lock(&parent->d_inode->i_mutex);
291 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
294 mutex_unlock(&parent->d_inode->i_mutex);
297 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
298 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
299 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
300 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
301 mark_inode_dirty(parent->d_inode);
302 mutex_unlock(&parent->d_inode->i_mutex);
305 /* Get rid of REMOTE_PARENT flag from incompat */
306 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
307 lustre_lma_swab(lma);
308 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
309 sizeof(*lma), XATTR_REPLACE);
313 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
314 struct osd_device *osd,
315 const struct lu_fid *fid,
316 struct osd_inode_id *id)
318 struct osd_mdobj_map *omm = osd->od_mdt_map;
319 char *name = oti->oti_name;
320 struct dentry *parent;
321 struct dentry *dentry;
322 struct ldiskfs_dir_entry_2 *de;
323 struct buffer_head *bh;
327 parent = omm->omm_remote_parent;
328 sprintf(name, DFID_NOBRACE, PFID(fid));
329 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
331 mutex_lock(&parent->d_inode->i_mutex);
332 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
339 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
341 inode = osd_iget(oti, osd, id);
351 mutex_unlock(&parent->d_inode->i_mutex);
353 osd_add_oi_cache(oti, osd, id, fid);
358 * directory structure on legacy OST:
360 * O/<seq>/d0-31/<objid>
367 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
369 struct lvfs_run_ctxt new;
370 struct lvfs_run_ctxt save;
371 struct dentry *rootd = osd_sb(dev)->s_root;
373 struct osd_thread_info *info = osd_oti_get(env);
375 struct lu_fid *fid = &info->oti_fid3;
379 OBD_ALLOC_PTR(dev->od_ost_map);
380 if (dev->od_ost_map == NULL)
383 /* to get subdir count from last_rcvd */
384 rc = osd_last_rcvd_subdir_count(dev);
386 GOTO(cleanup_alloc, rc);
388 dev->od_ost_map->om_subdir_count = rc;
391 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
392 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
393 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
395 osd_push_ctxt(dev, &new, &save);
397 d = ll_lookup_one_len("O", rootd, strlen("O"));
399 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
400 if (d->d_inode == NULL) {
402 /* The lookup() may be called again inside simple_mkdir().
403 * Since the repeated lookup() only be called for "/O" at
404 * mount time, it will not affect the whole performance. */
405 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
407 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
409 /* It is quite probably that the device is new formatted. */
410 dev->od_maybe_new = 1;
414 dev->od_ost_map->om_root = d;
416 /* 'What the @fid is' is not imporatant, because the object
417 * has no OI mapping, and only is visible inside the OSD.*/
418 lu_igif_build(fid, inode->i_ino, inode->i_generation);
419 rc = osd_ea_fid_set(info, inode, fid,
420 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
422 GOTO(cleanup_dentry, rc);
424 pop_ctxt(&save, &new);
430 pop_ctxt(&save, &new);
432 OBD_FREE_PTR(dev->od_ost_map);
436 static void osd_seq_free(struct osd_obj_seq *osd_seq)
440 if (osd_seq->oos_dirs) {
441 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
442 if (osd_seq->oos_dirs[j])
443 dput(osd_seq->oos_dirs[j]);
445 OBD_FREE(osd_seq->oos_dirs,
446 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
449 if (osd_seq->oos_root)
450 dput(osd_seq->oos_root);
452 OBD_FREE_PTR(osd_seq);
455 static void osd_ost_fini(struct osd_device *osd)
457 struct osd_obj_seq *osd_seq;
458 struct osd_obj_seq *tmp;
459 struct osd_obj_map *map = osd->od_ost_map;
465 write_lock(&map->om_seq_list_lock);
466 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
468 list_del_init(&osd_seq->oos_seq_list);
469 write_unlock(&map->om_seq_list_lock);
470 osd_seq_free(osd_seq);
471 write_lock(&map->om_seq_list_lock);
473 write_unlock(&map->om_seq_list_lock);
477 osd->od_ost_map = NULL;
481 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
486 /* prepare structures for OST */
487 rc = osd_ost_init(env, dev);
491 /* prepare structures for MDS */
492 rc = osd_mdt_init(env, dev);
499 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
501 struct osd_obj_seq *osd_seq;
503 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
504 if (osd_seq->oos_seq == seq)
510 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
512 struct osd_obj_seq *osd_seq;
514 read_lock(&map->om_seq_list_lock);
515 osd_seq = osd_seq_find_locked(map, seq);
516 read_unlock(&map->om_seq_list_lock);
520 void osd_obj_map_fini(struct osd_device *dev)
527 * Update the specified OI mapping.
529 * \retval 1, changed nothing
530 * \retval 0, changed successfully
531 * \retval -ve, on error
533 static int osd_obj_update_entry(struct osd_thread_info *info,
534 struct osd_device *osd,
535 struct dentry *dir, const char *name,
536 const struct lu_fid *fid,
537 const struct osd_inode_id *id,
540 struct inode *parent = dir->d_inode;
541 struct dentry *child;
542 struct ldiskfs_dir_entry_2 *de;
543 struct buffer_head *bh;
545 struct dentry *dentry = &info->oti_obj_dentry;
546 struct osd_inode_id *oi_id = &info->oti_id3;
547 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
548 struct lu_fid *oi_fid = &lma->lma_self_fid;
553 LASSERT(th->h_transaction != NULL);
555 child = &info->oti_child_dentry;
556 child->d_parent = dir;
557 child->d_name.hash = 0;
558 child->d_name.name = name;
559 child->d_name.len = strlen(name);
561 ll_vfs_dq_init(parent);
562 mutex_lock(&parent->i_mutex);
563 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
565 GOTO(out, rc = PTR_ERR(bh));
567 if (le32_to_cpu(de->inode) == id->oii_ino)
570 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
571 inode = osd_iget(info, osd, oi_id);
574 if (rc == -ENOENT || rc == -ESTALE)
579 /* The EA inode should NOT be in OI, old OI scrub may added
580 * such OI mapping by wrong, replace it. */
581 if (unlikely(osd_is_ea_inode(inode))) {
586 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
587 if (rc == -ENODATA) {
588 rc = osd_get_idif(info, inode, dentry, oi_fid);
589 if (rc > 0 || rc == -ENODATA) {
599 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
600 * either a crashed object or a uninitialized one. Replace it. */
601 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
602 CERROR("%s: the FID "DFID" is used by two objects: "
603 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
604 oi_id->oii_ino, oi_id->oii_gen,
605 id->oii_ino, id->oii_gen);
606 GOTO(out, rc = -EEXIST);
609 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
610 __u32 idx1 = fid_idif_ost_idx(fid);
611 __u32 idx2 = fid_idif_ost_idx(oi_fid);
612 struct ost_id *ostid = &info->oti_ostid;
613 struct lu_fid *tfid = &info->oti_fid3;
615 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
616 "invalid given FID "DFID", not match the "
617 "device index %u\n", PFID(fid), osd->od_index);
620 if (idx1 == 0 && idx2 == osd->od_index) {
621 fid_to_ostid(fid, ostid);
622 ostid_to_fid(tfid, ostid, idx2);
623 if (lu_fid_eq(tfid, oi_fid)) {
624 CERROR("%s: the FID "DFID" is used by "
625 "two objects(2): %u/%u %u/%u\n",
626 osd_name(osd), PFID(fid),
627 oi_id->oii_ino, oi_id->oii_gen,
628 id->oii_ino, id->oii_gen);
630 GOTO(out, rc = -EEXIST);
632 } else if (idx2 == 0 && idx1 == osd->od_index) {
633 fid_to_ostid(oi_fid, ostid);
634 ostid_to_fid(tfid, ostid, idx1);
635 if (lu_fid_eq(tfid, fid)) {
636 CERROR("%s: the FID "DFID" is used by "
637 "two objects(2): %u/%u %u/%u\n",
638 osd_name(osd), PFID(fid),
639 oi_id->oii_ino, oi_id->oii_gen,
640 id->oii_ino, id->oii_gen);
642 GOTO(out, rc = -EEXIST);
649 /* There may be temporary inconsistency: On one hand, the new
650 * object may be referenced by multiple entries, which is out
651 * of our control unless we traverse the whole /O completely,
652 * which is non-flat order and inefficient, should be avoided;
653 * On the other hand, the old object may become orphan if it
654 * is still valid. Since it was referenced by an invalid entry,
655 * making it as invisible temporary may be not worse. OI scrub
656 * will process it later. */
657 rc = ldiskfs_journal_get_write_access(th, bh);
661 de->inode = cpu_to_le32(id->oii_ino);
662 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
669 mutex_unlock(&parent->i_mutex);
673 static int osd_obj_del_entry(struct osd_thread_info *info,
674 struct osd_device *osd,
675 struct dentry *dird, char *name,
678 struct ldiskfs_dir_entry_2 *de;
679 struct buffer_head *bh;
680 struct dentry *child;
681 struct inode *dir = dird->d_inode;
686 LASSERT(th->h_transaction != NULL);
689 child = &info->oti_child_dentry;
690 child->d_name.hash = 0;
691 child->d_name.name = name;
692 child->d_name.len = strlen(name);
693 child->d_parent = dird;
694 child->d_inode = NULL;
697 mutex_lock(&dir->i_mutex);
698 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
702 rc = ldiskfs_delete_entry(th, dir, de, bh);
705 mutex_unlock(&dir->i_mutex);
710 static int osd_obj_add_entry(struct osd_thread_info *info,
711 struct osd_device *osd,
712 struct dentry *dir, char *name,
713 const struct osd_inode_id *id,
716 struct dentry *child;
722 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
726 LASSERT(th->h_transaction != NULL);
728 inode = info->oti_inode;
729 if (unlikely(inode == NULL)) {
730 struct ldiskfs_inode_info *lii;
734 inode = info->oti_inode = &lii->vfs_inode;
737 inode->i_sb = osd_sb(osd);
738 osd_id_to_inode(inode, id);
739 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
741 child = &info->oti_child_dentry;
742 child->d_name.hash = 0;
743 child->d_name.name = name;
744 child->d_name.len = strlen(name);
745 child->d_parent = dir;
746 child->d_inode = inode;
748 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
751 ll_vfs_dq_init(dir->d_inode);
752 mutex_lock(&dir->d_inode->i_mutex);
753 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
754 mutex_unlock(&dir->d_inode->i_mutex);
760 * Use %llu for legacy OST sequences, but use %llx for new
761 * sequences names, so that the O/{seq}/dN/{oid} more closely
762 * follows the DFID/PFID format. This makes it easier to map from
763 * debug messages to objects in the future, and the legacy space
764 * of FID_SEQ_OST_MDT0 will be unused in the future.
766 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
768 snprintf(seq_name, name_size,
769 (fid_seq_is_rsvd(seq) ||
770 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
771 fid_seq_is_idif(seq) ? 0 : seq);
774 static inline void osd_oid_name(char *name, size_t name_size,
775 const struct lu_fid *fid, u64 id)
777 snprintf(name, name_size,
778 (fid_seq_is_rsvd(fid_seq(fid)) ||
779 fid_seq_is_mdt0(fid_seq(fid)) ||
780 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
783 /* external locking is required */
784 static int osd_seq_load_locked(struct osd_thread_info *info,
785 struct osd_device *osd,
786 struct osd_obj_seq *osd_seq)
788 struct osd_obj_map *map = osd->od_ost_map;
789 struct dentry *seq_dir;
791 struct lu_fid *fid = &info->oti_fid3;
797 if (osd_seq->oos_root != NULL)
801 LASSERT(map->om_root);
803 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
805 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
807 GOTO(out_err, rc = PTR_ERR(seq_dir));
808 else if (seq_dir->d_inode == NULL)
809 GOTO(out_put, rc = -EFAULT);
811 inode = seq_dir->d_inode;
812 osd_seq->oos_root = seq_dir;
814 /* 'What the @fid is' is not imporatant, because the object
815 * has no OI mapping, and only is visible inside the OSD.*/
816 lu_igif_build(fid, inode->i_ino, inode->i_generation);
817 rc = osd_ea_fid_set(info, inode, fid,
818 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
822 LASSERT(osd_seq->oos_dirs == NULL);
823 OBD_ALLOC(osd_seq->oos_dirs,
824 sizeof(seq_dir) * osd_seq->oos_subdir_count);
825 if (osd_seq->oos_dirs == NULL)
826 GOTO(out_put, rc = -ENOMEM);
828 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
831 snprintf(dir_name, sizeof(dir_name), "d%u", i);
832 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
835 GOTO(out_free, rc = PTR_ERR(dir));
836 } else if (dir->d_inode == NULL) {
838 GOTO(out_free, rc = -EFAULT);
841 inode = dir->d_inode;
842 osd_seq->oos_dirs[i] = dir;
844 /* 'What the @fid is' is not imporatant, because the object
845 * has no OI mapping, and only is visible inside the OSD.*/
846 lu_igif_build(fid, inode->i_ino, inode->i_generation);
847 rc = osd_ea_fid_set(info, inode, fid,
848 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
855 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
856 if (osd_seq->oos_dirs[i] != NULL)
857 dput(osd_seq->oos_dirs[i]);
859 OBD_FREE(osd_seq->oos_dirs,
860 sizeof(seq_dir) * osd_seq->oos_subdir_count);
863 osd_seq->oos_root = NULL;
869 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
870 struct osd_device *osd, u64 seq)
872 struct osd_obj_map *map;
873 struct osd_obj_seq *osd_seq;
877 map = osd->od_ost_map;
879 LASSERT(map->om_root);
881 osd_seq = osd_seq_find(map, seq);
882 if (likely(osd_seq != NULL))
885 /* Serializing init process */
886 mutex_lock(&map->om_dir_init_mutex);
888 /* Check whether the seq has been added */
889 read_lock(&map->om_seq_list_lock);
890 osd_seq = osd_seq_find_locked(map, seq);
891 if (osd_seq != NULL) {
892 read_unlock(&map->om_seq_list_lock);
893 GOTO(cleanup, rc = 0);
895 read_unlock(&map->om_seq_list_lock);
897 OBD_ALLOC_PTR(osd_seq);
899 GOTO(cleanup, rc = -ENOMEM);
901 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
902 osd_seq->oos_seq = seq;
903 /* Init subdir count to be 32, but each seq can have
904 * different subdir count */
905 osd_seq->oos_subdir_count = map->om_subdir_count;
906 rc = osd_seq_load_locked(info, osd, osd_seq);
910 write_lock(&map->om_seq_list_lock);
911 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
912 write_unlock(&map->om_seq_list_lock);
915 mutex_unlock(&map->om_dir_init_mutex);
918 OBD_FREE_PTR(osd_seq);
925 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
926 const struct lu_fid *fid, struct osd_inode_id *id)
928 struct osd_obj_map *map;
929 struct osd_obj_seq *osd_seq;
930 struct dentry *d_seq;
931 struct dentry *child;
932 struct ost_id *ostid = &info->oti_ostid;
935 struct ldiskfs_dir_entry_2 *de;
936 struct buffer_head *bh;
941 /* on the very first lookup we find and open directories */
943 map = dev->od_ost_map;
945 LASSERT(map->om_root);
947 fid_to_ostid(fid, ostid);
948 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
950 RETURN(PTR_ERR(osd_seq));
952 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
953 d_seq = osd_seq->oos_dirs[dirn];
956 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
958 child = &info->oti_child_dentry;
959 child->d_parent = d_seq;
960 child->d_name.hash = 0;
961 child->d_name.name = name;
962 /* XXX: we can use rc from sprintf() instead of strlen() */
963 child->d_name.len = strlen(name);
965 dir = d_seq->d_inode;
966 mutex_lock(&dir->i_mutex);
967 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
968 mutex_unlock(&dir->i_mutex);
973 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
976 inode = osd_iget(info, dev, id);
978 int rc = PTR_ERR(inode);
980 RETURN(rc == -ENOENT ? -ESTALE : rc);
987 int osd_obj_map_insert(struct osd_thread_info *info,
988 struct osd_device *osd,
989 const struct lu_fid *fid,
990 const struct osd_inode_id *id,
993 struct osd_obj_map *map;
994 struct osd_obj_seq *osd_seq;
996 struct ost_id *ostid = &info->oti_ostid;
1002 map = osd->od_ost_map;
1005 /* map fid to seq:objid */
1006 fid_to_ostid(fid, ostid);
1008 oid = ostid_id(ostid);
1009 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1010 if (IS_ERR(osd_seq))
1011 RETURN(PTR_ERR(osd_seq));
1013 dirn = oid & (osd_seq->oos_subdir_count - 1);
1014 d = osd_seq->oos_dirs[dirn];
1017 osd_oid_name(name, sizeof(name), fid, oid);
1020 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1021 if (rc == -EEXIST) {
1022 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1023 if (unlikely(rc == -ENOENT))
1026 if (unlikely(rc == 1))
1033 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1034 const struct lu_fid *fid, handle_t *th)
1036 struct osd_obj_map *map;
1037 struct osd_obj_seq *osd_seq;
1039 struct ost_id *ostid = &info->oti_ostid;
1044 map = osd->od_ost_map;
1047 /* map fid to seq:objid */
1048 fid_to_ostid(fid, ostid);
1050 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1051 if (IS_ERR(osd_seq))
1052 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1054 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1055 d = osd_seq->oos_dirs[dirn];
1058 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1059 rc = osd_obj_del_entry(info, osd, d, name, th);
1064 int osd_obj_map_update(struct osd_thread_info *info,
1065 struct osd_device *osd,
1066 const struct lu_fid *fid,
1067 const struct osd_inode_id *id,
1070 struct osd_obj_seq *osd_seq;
1072 struct ost_id *ostid = &info->oti_ostid;
1077 fid_to_ostid(fid, ostid);
1078 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1079 if (IS_ERR(osd_seq))
1080 RETURN(PTR_ERR(osd_seq));
1082 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1083 d = osd_seq->oos_dirs[dirn];
1086 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1087 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1092 int osd_obj_map_recover(struct osd_thread_info *info,
1093 struct osd_device *osd,
1094 struct inode *src_parent,
1095 struct dentry *src_child,
1096 const struct lu_fid *fid)
1098 struct osd_obj_seq *osd_seq;
1099 struct dentry *tgt_parent;
1100 struct dentry *tgt_child = &info->oti_child_dentry;
1102 struct inode *inode = src_child->d_inode;
1103 struct ost_id *ostid = &info->oti_ostid;
1105 struct ldiskfs_dir_entry_2 *de;
1106 struct buffer_head *bh;
1112 if (fid_is_last_id(fid)) {
1113 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1114 if (IS_ERR(osd_seq))
1115 RETURN(PTR_ERR(osd_seq));
1117 tgt_parent = osd_seq->oos_root;
1118 tgt_child->d_name.name = "LAST_ID";
1119 tgt_child->d_name.len = strlen("LAST_ID");
1121 fid_to_ostid(fid, ostid);
1122 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1123 if (IS_ERR(osd_seq))
1124 RETURN(PTR_ERR(osd_seq));
1126 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1127 tgt_parent = osd_seq->oos_dirs[dirn];
1128 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1129 tgt_child->d_name.name = name;
1130 tgt_child->d_name.len = strlen(name);
1132 LASSERT(tgt_parent != NULL);
1134 dir = tgt_parent->d_inode;
1135 tgt_child->d_name.hash = 0;
1136 tgt_child->d_parent = tgt_parent;
1137 tgt_child->d_inode = inode;
1139 /* The non-initialized src_child may be destroyed. */
1140 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1141 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1142 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1143 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1145 RETURN(PTR_ERR(jh));
1147 ll_vfs_dq_init(src_parent);
1148 ll_vfs_dq_init(dir);
1150 mutex_lock(&src_parent->i_mutex);
1151 mutex_lock(&dir->i_mutex);
1152 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1154 /* XXX: If some other object occupied the same slot. And If such
1155 * inode is zero-sized and with SUID+SGID, then means it is
1156 * a new created one. Maybe we can remove it and insert the
1157 * original one back to the /O/<seq>/d<x>. But there are
1158 * something to be considered:
1160 * 1) The OST-object under /lost+found has crashed LMA.
1161 * So it should not conflict with the current one.
1163 * 2) There are race conditions that: someone may just want
1164 * to modify the current one. Even if the OI scrub takes
1165 * the object lock when remove the current one, it still
1166 * cause the modification to be lost becasue the target
1167 * has been removed when the RPC service thread waiting
1170 * So keep it there before we have suitable solution. */
1172 mutex_unlock(&dir->i_mutex);
1173 mutex_unlock(&src_parent->i_mutex);
1174 ldiskfs_journal_stop(jh);
1177 /* If the src object has never been modified, then remove it. */
1178 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1179 inode->i_mode & S_ISGID) {
1180 rc = ll_vfs_unlink(src_parent, src_child);
1181 if (unlikely(rc == -ENOENT))
1187 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1189 if (unlikely(IS_ERR(bh)))
1190 GOTO(unlock, rc = PTR_ERR(bh));
1192 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1197 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1202 mutex_unlock(&dir->i_mutex);
1203 mutex_unlock(&src_parent->i_mutex);
1204 ldiskfs_journal_stop(jh);
1208 static struct dentry *
1209 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1210 const struct lu_fid *fid, char **name)
1212 struct dentry *root = ERR_PTR(-ENOENT);
1214 if (fid_is_last_id(fid)) {
1215 struct osd_obj_seq *osd_seq;
1217 /* on creation of LAST_ID we create O/<seq> hierarchy */
1218 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1219 if (IS_ERR(osd_seq))
1220 RETURN((struct dentry *)osd_seq);
1223 root = osd_seq->oos_root;
1225 *name = osd_lf_fid2name(fid);
1227 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1228 else if ((*name)[0])
1229 root = osd_sb(osd)->s_root;
1235 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1236 const struct lu_fid *fid, const struct osd_inode_id *id,
1239 struct dentry *root;
1244 root = osd_object_spec_find(info, osd, fid, &name);
1245 if (!IS_ERR(root)) {
1246 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1256 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1257 const struct lu_fid *fid, const struct osd_inode_id *id,
1260 struct dentry *root;
1265 root = osd_object_spec_find(info, osd, fid, &name);
1266 if (!IS_ERR(root)) {
1267 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1277 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1278 const struct lu_fid *fid, struct osd_inode_id *id)
1280 struct dentry *root;
1281 struct dentry *dentry;
1282 struct inode *inode;
1287 if (fid_is_last_id(fid)) {
1288 struct osd_obj_seq *osd_seq;
1290 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1291 if (IS_ERR(osd_seq))
1292 RETURN(PTR_ERR(osd_seq));
1293 root = osd_seq->oos_root;
1296 root = osd_sb(osd)->s_root;
1297 name = osd_lf_fid2name(fid);
1298 if (name == NULL || strlen(name) == 0)
1302 dentry = ll_lookup_one_len(name, root, strlen(name));
1303 if (!IS_ERR(dentry)) {
1304 inode = dentry->d_inode;
1306 if (is_bad_inode(inode)) {
1309 osd_id_gen(id, inode->i_ino,
1310 inode->i_generation);
1314 /* if dentry is accessible after osd_compat_spec_insert it
1315 * will still contain NULL inode, so don't keep it in cache */
1316 d_invalidate(dentry);