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 if (unlikely(osd->od_is_ost))
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,
342 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
344 inode = osd_iget(oti, osd, id);
354 mutex_unlock(&parent->d_inode->i_mutex);
356 osd_add_oi_cache(oti, osd, id, fid);
361 * directory structure on legacy OST:
363 * O/<seq>/d0-31/<objid>
370 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
372 struct lvfs_run_ctxt new;
373 struct lvfs_run_ctxt save;
374 struct dentry *rootd = osd_sb(dev)->s_root;
376 struct osd_thread_info *info = osd_oti_get(env);
378 struct lu_fid *fid = &info->oti_fid3;
382 OBD_ALLOC_PTR(dev->od_ost_map);
383 if (dev->od_ost_map == NULL)
386 /* to get subdir count from last_rcvd */
387 rc = osd_last_rcvd_subdir_count(dev);
389 GOTO(cleanup_alloc, rc);
391 dev->od_ost_map->om_subdir_count = rc;
394 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
395 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
396 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
398 osd_push_ctxt(dev, &new, &save);
400 d = ll_lookup_one_len("O", rootd, strlen("O"));
402 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
403 if (d->d_inode == NULL) {
405 /* The lookup() may be called again inside simple_mkdir().
406 * Since the repeated lookup() only be called for "/O" at
407 * mount time, it will not affect the whole performance. */
408 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
410 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
412 /* It is quite probably that the device is new formatted. */
413 dev->od_maybe_new = 1;
417 dev->od_ost_map->om_root = d;
419 /* 'What the @fid is' is not imporatant, because the object
420 * has no OI mapping, and only is visible inside the OSD.*/
421 lu_igif_build(fid, inode->i_ino, inode->i_generation);
422 rc = osd_ea_fid_set(info, inode, fid,
423 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
425 GOTO(cleanup_dentry, rc);
427 pop_ctxt(&save, &new);
433 pop_ctxt(&save, &new);
435 OBD_FREE_PTR(dev->od_ost_map);
439 static void osd_seq_free(struct osd_obj_seq *osd_seq)
443 if (osd_seq->oos_dirs) {
444 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
445 if (osd_seq->oos_dirs[j])
446 dput(osd_seq->oos_dirs[j]);
448 OBD_FREE(osd_seq->oos_dirs,
449 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
452 if (osd_seq->oos_root)
453 dput(osd_seq->oos_root);
455 OBD_FREE_PTR(osd_seq);
458 static void osd_ost_fini(struct osd_device *osd)
460 struct osd_obj_seq *osd_seq;
461 struct osd_obj_seq *tmp;
462 struct osd_obj_map *map = osd->od_ost_map;
468 write_lock(&map->om_seq_list_lock);
469 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
471 list_del_init(&osd_seq->oos_seq_list);
472 write_unlock(&map->om_seq_list_lock);
473 osd_seq_free(osd_seq);
474 write_lock(&map->om_seq_list_lock);
476 write_unlock(&map->om_seq_list_lock);
480 osd->od_ost_map = NULL;
484 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
489 /* prepare structures for OST */
490 rc = osd_ost_init(env, dev);
494 if (!dev->od_is_ost) {
495 rc = osd_mdt_init(env, dev);
503 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
505 struct osd_obj_seq *osd_seq;
507 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
508 if (osd_seq->oos_seq == seq)
514 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
516 struct osd_obj_seq *osd_seq;
518 read_lock(&map->om_seq_list_lock);
519 osd_seq = osd_seq_find_locked(map, seq);
520 read_unlock(&map->om_seq_list_lock);
524 void osd_obj_map_fini(struct osd_device *dev)
531 * Update the specified OI mapping.
533 * \retval 1, changed nothing
534 * \retval 0, changed successfully
535 * \retval -ve, on error
537 static int osd_obj_update_entry(struct osd_thread_info *info,
538 struct osd_device *osd,
539 struct dentry *dir, const char *name,
540 const struct lu_fid *fid,
541 const struct osd_inode_id *id,
544 struct inode *parent = dir->d_inode;
545 struct dentry *child;
546 struct ldiskfs_dir_entry_2 *de;
547 struct buffer_head *bh;
549 struct dentry *dentry = &info->oti_obj_dentry;
550 struct osd_inode_id *oi_id = &info->oti_id3;
551 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
552 struct lu_fid *oi_fid = &lma->lma_self_fid;
557 LASSERT(th->h_transaction != NULL);
559 child = &info->oti_child_dentry;
560 child->d_parent = dir;
561 child->d_name.hash = 0;
562 child->d_name.name = name;
563 child->d_name.len = strlen(name);
565 ll_vfs_dq_init(parent);
566 mutex_lock(&parent->i_mutex);
567 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
569 GOTO(out, rc = PTR_ERR(bh));
571 if (le32_to_cpu(de->inode) == id->oii_ino)
574 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
575 inode = osd_iget(info, osd, oi_id);
578 if (rc == -ENOENT || rc == -ESTALE)
583 /* The EA inode should NOT be in OI, old OI scrub may added
584 * such OI mapping by wrong, replace it. */
585 if (unlikely(osd_is_ea_inode(inode))) {
590 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
591 if (rc == -ENODATA) {
592 rc = osd_get_idif(info, inode, dentry, oi_fid);
593 if (rc > 0 || rc == -ENODATA) {
603 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
604 * either a crashed object or a uninitialized one. Replace it. */
605 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
606 CERROR("%s: the FID "DFID" is used by two objects: "
607 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
608 oi_id->oii_ino, oi_id->oii_gen,
609 id->oii_ino, id->oii_gen);
610 GOTO(out, rc = -EEXIST);
613 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
614 __u32 idx1 = fid_idif_ost_idx(fid);
615 __u32 idx2 = fid_idif_ost_idx(oi_fid);
616 struct ost_id *ostid = &info->oti_ostid;
617 struct lu_fid *tfid = &info->oti_fid3;
619 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
620 "invalid given FID "DFID", not match the "
621 "device index %u\n", PFID(fid), osd->od_index);
624 if (idx1 == 0 && idx2 == osd->od_index) {
625 fid_to_ostid(fid, ostid);
626 ostid_to_fid(tfid, ostid, idx2);
627 if (lu_fid_eq(tfid, oi_fid)) {
628 CERROR("%s: the FID "DFID" is used by "
629 "two objects(2): %u/%u %u/%u\n",
630 osd_name(osd), PFID(fid),
631 oi_id->oii_ino, oi_id->oii_gen,
632 id->oii_ino, id->oii_gen);
634 GOTO(out, rc = -EEXIST);
636 } else if (idx2 == 0 && idx1 == osd->od_index) {
637 fid_to_ostid(oi_fid, ostid);
638 ostid_to_fid(tfid, ostid, idx1);
639 if (lu_fid_eq(tfid, fid)) {
640 CERROR("%s: the FID "DFID" is used by "
641 "two objects(2): %u/%u %u/%u\n",
642 osd_name(osd), PFID(fid),
643 oi_id->oii_ino, oi_id->oii_gen,
644 id->oii_ino, id->oii_gen);
646 GOTO(out, rc = -EEXIST);
653 /* There may be temporary inconsistency: On one hand, the new
654 * object may be referenced by multiple entries, which is out
655 * of our control unless we traverse the whole /O completely,
656 * which is non-flat order and inefficient, should be avoided;
657 * On the other hand, the old object may become orphan if it
658 * is still valid. Since it was referenced by an invalid entry,
659 * making it as invisible temporary may be not worse. OI scrub
660 * will process it later. */
661 rc = ldiskfs_journal_get_write_access(th, bh);
665 de->inode = cpu_to_le32(id->oii_ino);
666 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
673 mutex_unlock(&parent->i_mutex);
677 static int osd_obj_del_entry(struct osd_thread_info *info,
678 struct osd_device *osd,
679 struct dentry *dird, char *name,
682 struct ldiskfs_dir_entry_2 *de;
683 struct buffer_head *bh;
684 struct dentry *child;
685 struct inode *dir = dird->d_inode;
690 LASSERT(th->h_transaction != NULL);
693 child = &info->oti_child_dentry;
694 child->d_name.hash = 0;
695 child->d_name.name = name;
696 child->d_name.len = strlen(name);
697 child->d_parent = dird;
698 child->d_inode = NULL;
701 mutex_lock(&dir->i_mutex);
702 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
706 rc = ldiskfs_delete_entry(th, dir, de, bh);
709 mutex_unlock(&dir->i_mutex);
714 static int osd_obj_add_entry(struct osd_thread_info *info,
715 struct osd_device *osd,
716 struct dentry *dir, char *name,
717 const struct osd_inode_id *id,
720 struct dentry *child;
726 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
730 LASSERT(th->h_transaction != NULL);
732 inode = info->oti_inode;
733 if (unlikely(inode == NULL)) {
734 struct ldiskfs_inode_info *lii;
738 inode = info->oti_inode = &lii->vfs_inode;
741 inode->i_sb = osd_sb(osd);
742 osd_id_to_inode(inode, id);
743 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
745 child = &info->oti_child_dentry;
746 child->d_name.hash = 0;
747 child->d_name.name = name;
748 child->d_name.len = strlen(name);
749 child->d_parent = dir;
750 child->d_inode = inode;
752 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
755 ll_vfs_dq_init(dir->d_inode);
756 mutex_lock(&dir->d_inode->i_mutex);
757 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
758 mutex_unlock(&dir->d_inode->i_mutex);
764 * Use %llu for legacy OST sequences, but use %llx for new
765 * sequences names, so that the O/{seq}/dN/{oid} more closely
766 * follows the DFID/PFID format. This makes it easier to map from
767 * debug messages to objects in the future, and the legacy space
768 * of FID_SEQ_OST_MDT0 will be unused in the future.
770 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
772 snprintf(seq_name, name_size,
773 (fid_seq_is_rsvd(seq) ||
774 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
775 fid_seq_is_idif(seq) ? 0 : seq);
778 static inline void osd_oid_name(char *name, size_t name_size,
779 const struct lu_fid *fid, u64 id)
781 snprintf(name, name_size,
782 (fid_seq_is_rsvd(fid_seq(fid)) ||
783 fid_seq_is_mdt0(fid_seq(fid)) ||
784 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
787 /* external locking is required */
788 static int osd_seq_load_locked(struct osd_thread_info *info,
789 struct osd_device *osd,
790 struct osd_obj_seq *osd_seq)
792 struct osd_obj_map *map = osd->od_ost_map;
793 struct dentry *seq_dir;
795 struct lu_fid *fid = &info->oti_fid3;
801 if (osd_seq->oos_root != NULL)
805 LASSERT(map->om_root);
807 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
809 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
811 GOTO(out_err, rc = PTR_ERR(seq_dir));
812 else if (seq_dir->d_inode == NULL)
813 GOTO(out_put, rc = -EFAULT);
815 inode = seq_dir->d_inode;
816 osd_seq->oos_root = seq_dir;
818 /* 'What the @fid is' is not imporatant, because the object
819 * has no OI mapping, and only is visible inside the OSD.*/
820 lu_igif_build(fid, inode->i_ino, inode->i_generation);
821 rc = osd_ea_fid_set(info, inode, fid,
822 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
826 LASSERT(osd_seq->oos_dirs == NULL);
827 OBD_ALLOC(osd_seq->oos_dirs,
828 sizeof(seq_dir) * osd_seq->oos_subdir_count);
829 if (osd_seq->oos_dirs == NULL)
830 GOTO(out_put, rc = -ENOMEM);
832 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
835 snprintf(dir_name, sizeof(dir_name), "d%u", i);
836 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
839 GOTO(out_free, rc = PTR_ERR(dir));
840 } else if (dir->d_inode == NULL) {
842 GOTO(out_free, rc = -EFAULT);
845 inode = dir->d_inode;
846 osd_seq->oos_dirs[i] = dir;
848 /* 'What the @fid is' is not imporatant, because the object
849 * has no OI mapping, and only is visible inside the OSD.*/
850 lu_igif_build(fid, inode->i_ino, inode->i_generation);
851 rc = osd_ea_fid_set(info, inode, fid,
852 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
859 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
860 if (osd_seq->oos_dirs[i] != NULL)
861 dput(osd_seq->oos_dirs[i]);
863 OBD_FREE(osd_seq->oos_dirs,
864 sizeof(seq_dir) * osd_seq->oos_subdir_count);
867 osd_seq->oos_root = NULL;
873 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
874 struct osd_device *osd, u64 seq)
876 struct osd_obj_map *map;
877 struct osd_obj_seq *osd_seq;
881 map = osd->od_ost_map;
883 LASSERT(map->om_root);
885 osd_seq = osd_seq_find(map, seq);
886 if (likely(osd_seq != NULL))
889 /* Serializing init process */
890 mutex_lock(&map->om_dir_init_mutex);
892 /* Check whether the seq has been added */
893 read_lock(&map->om_seq_list_lock);
894 osd_seq = osd_seq_find_locked(map, seq);
895 if (osd_seq != NULL) {
896 read_unlock(&map->om_seq_list_lock);
897 GOTO(cleanup, rc = 0);
899 read_unlock(&map->om_seq_list_lock);
901 OBD_ALLOC_PTR(osd_seq);
903 GOTO(cleanup, rc = -ENOMEM);
905 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
906 osd_seq->oos_seq = seq;
907 /* Init subdir count to be 32, but each seq can have
908 * different subdir count */
909 osd_seq->oos_subdir_count = map->om_subdir_count;
910 rc = osd_seq_load_locked(info, osd, osd_seq);
914 write_lock(&map->om_seq_list_lock);
915 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
916 write_unlock(&map->om_seq_list_lock);
919 mutex_unlock(&map->om_dir_init_mutex);
922 OBD_FREE_PTR(osd_seq);
929 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
930 const struct lu_fid *fid, struct osd_inode_id *id)
932 struct osd_obj_map *map;
933 struct osd_obj_seq *osd_seq;
934 struct dentry *d_seq;
935 struct dentry *child;
936 struct ost_id *ostid = &info->oti_ostid;
939 struct ldiskfs_dir_entry_2 *de;
940 struct buffer_head *bh;
945 /* on the very first lookup we find and open directories */
947 map = dev->od_ost_map;
949 LASSERT(map->om_root);
951 fid_to_ostid(fid, ostid);
952 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
954 RETURN(PTR_ERR(osd_seq));
956 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
957 d_seq = osd_seq->oos_dirs[dirn];
960 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
962 child = &info->oti_child_dentry;
963 child->d_parent = d_seq;
964 child->d_name.hash = 0;
965 child->d_name.name = name;
966 /* XXX: we can use rc from sprintf() instead of strlen() */
967 child->d_name.len = strlen(name);
969 dir = d_seq->d_inode;
970 mutex_lock(&dir->i_mutex);
971 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
972 mutex_unlock(&dir->i_mutex);
977 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
980 inode = osd_iget(info, dev, id);
982 int rc = PTR_ERR(inode);
984 RETURN(rc == -ENOENT ? -ESTALE : rc);
991 int osd_obj_map_insert(struct osd_thread_info *info,
992 struct osd_device *osd,
993 const struct lu_fid *fid,
994 const struct osd_inode_id *id,
997 struct osd_obj_map *map;
998 struct osd_obj_seq *osd_seq;
1000 struct ost_id *ostid = &info->oti_ostid;
1006 map = osd->od_ost_map;
1009 /* map fid to seq:objid */
1010 fid_to_ostid(fid, ostid);
1012 oid = ostid_id(ostid);
1013 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1014 if (IS_ERR(osd_seq))
1015 RETURN(PTR_ERR(osd_seq));
1017 dirn = oid & (osd_seq->oos_subdir_count - 1);
1018 d = osd_seq->oos_dirs[dirn];
1021 osd_oid_name(name, sizeof(name), fid, oid);
1024 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1025 if (rc == -EEXIST) {
1026 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1027 if (unlikely(rc == -ENOENT))
1030 if (unlikely(rc == 1))
1037 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1038 const struct lu_fid *fid, handle_t *th)
1040 struct osd_obj_map *map;
1041 struct osd_obj_seq *osd_seq;
1043 struct ost_id *ostid = &info->oti_ostid;
1048 map = osd->od_ost_map;
1051 /* map fid to seq:objid */
1052 fid_to_ostid(fid, ostid);
1054 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1055 if (IS_ERR(osd_seq))
1056 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1058 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1059 d = osd_seq->oos_dirs[dirn];
1062 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1063 rc = osd_obj_del_entry(info, osd, d, name, th);
1068 int osd_obj_map_update(struct osd_thread_info *info,
1069 struct osd_device *osd,
1070 const struct lu_fid *fid,
1071 const struct osd_inode_id *id,
1074 struct osd_obj_seq *osd_seq;
1076 struct ost_id *ostid = &info->oti_ostid;
1081 fid_to_ostid(fid, ostid);
1082 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1083 if (IS_ERR(osd_seq))
1084 RETURN(PTR_ERR(osd_seq));
1086 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1087 d = osd_seq->oos_dirs[dirn];
1090 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1091 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1096 int osd_obj_map_recover(struct osd_thread_info *info,
1097 struct osd_device *osd,
1098 struct inode *src_parent,
1099 struct dentry *src_child,
1100 const struct lu_fid *fid)
1102 struct osd_obj_seq *osd_seq;
1103 struct dentry *tgt_parent;
1104 struct dentry *tgt_child = &info->oti_child_dentry;
1106 struct inode *inode = src_child->d_inode;
1107 struct ost_id *ostid = &info->oti_ostid;
1109 struct ldiskfs_dir_entry_2 *de;
1110 struct buffer_head *bh;
1116 if (fid_is_last_id(fid)) {
1117 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1118 if (IS_ERR(osd_seq))
1119 RETURN(PTR_ERR(osd_seq));
1121 tgt_parent = osd_seq->oos_root;
1122 tgt_child->d_name.name = "LAST_ID";
1123 tgt_child->d_name.len = strlen("LAST_ID");
1125 fid_to_ostid(fid, ostid);
1126 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1127 if (IS_ERR(osd_seq))
1128 RETURN(PTR_ERR(osd_seq));
1130 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1131 tgt_parent = osd_seq->oos_dirs[dirn];
1132 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1133 tgt_child->d_name.name = name;
1134 tgt_child->d_name.len = strlen(name);
1136 LASSERT(tgt_parent != NULL);
1138 dir = tgt_parent->d_inode;
1139 tgt_child->d_name.hash = 0;
1140 tgt_child->d_parent = tgt_parent;
1141 tgt_child->d_inode = inode;
1143 /* The non-initialized src_child may be destroyed. */
1144 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1145 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1146 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1147 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1149 RETURN(PTR_ERR(jh));
1151 ll_vfs_dq_init(src_parent);
1152 ll_vfs_dq_init(dir);
1154 mutex_lock(&src_parent->i_mutex);
1155 mutex_lock(&dir->i_mutex);
1156 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1158 /* XXX: If some other object occupied the same slot. And If such
1159 * inode is zero-sized and with SUID+SGID, then means it is
1160 * a new created one. Maybe we can remove it and insert the
1161 * original one back to the /O/<seq>/d<x>. But there are
1162 * something to be considered:
1164 * 1) The OST-object under /lost+found has crashed LMA.
1165 * So it should not conflict with the current one.
1167 * 2) There are race conditions that: someone may just want
1168 * to modify the current one. Even if the OI scrub takes
1169 * the object lock when remove the current one, it still
1170 * cause the modification to be lost becasue the target
1171 * has been removed when the RPC service thread waiting
1174 * So keep it there before we have suitable solution. */
1176 mutex_unlock(&dir->i_mutex);
1177 mutex_unlock(&src_parent->i_mutex);
1178 ldiskfs_journal_stop(jh);
1181 /* If the src object has never been modified, then remove it. */
1182 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1183 inode->i_mode & S_ISGID) {
1184 rc = ll_vfs_unlink(src_parent, src_child);
1185 if (unlikely(rc == -ENOENT))
1191 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1193 if (unlikely(IS_ERR(bh)))
1194 GOTO(unlock, rc = PTR_ERR(bh));
1196 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1201 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1206 mutex_unlock(&dir->i_mutex);
1207 mutex_unlock(&src_parent->i_mutex);
1208 ldiskfs_journal_stop(jh);
1212 static struct dentry *
1213 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1214 const struct lu_fid *fid, char **name)
1216 struct dentry *root = ERR_PTR(-ENOENT);
1218 if (fid_is_last_id(fid)) {
1219 struct osd_obj_seq *osd_seq;
1221 /* on creation of LAST_ID we create O/<seq> hierarchy */
1222 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1223 if (IS_ERR(osd_seq))
1224 RETURN((struct dentry *)osd_seq);
1227 root = osd_seq->oos_root;
1229 *name = osd_lf_fid2name(fid);
1231 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1232 else if ((*name)[0])
1233 root = osd_sb(osd)->s_root;
1239 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1240 const struct lu_fid *fid, const struct osd_inode_id *id,
1243 struct dentry *root;
1248 root = osd_object_spec_find(info, osd, fid, &name);
1249 if (!IS_ERR(root)) {
1250 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1260 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1261 const struct lu_fid *fid, const struct osd_inode_id *id,
1264 struct dentry *root;
1269 root = osd_object_spec_find(info, osd, fid, &name);
1270 if (!IS_ERR(root)) {
1271 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1281 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1282 const struct lu_fid *fid, struct osd_inode_id *id)
1284 struct dentry *root;
1285 struct dentry *dentry;
1286 struct inode *inode;
1291 if (fid_is_last_id(fid)) {
1292 struct osd_obj_seq *osd_seq;
1294 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1295 if (IS_ERR(osd_seq))
1296 RETURN(PTR_ERR(osd_seq));
1297 root = osd_seq->oos_root;
1300 root = osd_sb(osd)->s_root;
1301 name = osd_lf_fid2name(fid);
1302 if (name == NULL || strlen(name) == 0)
1306 dentry = ll_lookup_one_len(name, root, strlen(name));
1307 if (!IS_ERR(dentry)) {
1308 inode = dentry->d_inode;
1310 if (is_bad_inode(inode)) {
1313 osd_id_gen(id, inode->i_ino,
1314 inode->i_generation);
1318 /* if dentry is accessible after osd_compat_spec_insert it
1319 * will still contain NULL inode, so don't keep it in cache */
1320 d_invalidate(dentry);