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, 2015, 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_mdt_attrs;
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, lma);
235 lma->lma_incompat |= LMAI_REMOTE_PARENT;
236 lustre_lma_swab(lma);
237 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
238 sizeof(*lma), XATTR_REPLACE);
242 parent = omm->omm_remote_parent;
243 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
244 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
246 mutex_lock(&parent->d_inode->i_mutex);
247 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
248 obj->oo_inode, NULL);
249 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
250 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
251 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
252 mark_inode_dirty(parent->d_inode);
253 mutex_unlock(&parent->d_inode->i_mutex);
257 int osd_delete_from_remote_parent(const struct lu_env *env,
258 struct osd_device *osd,
259 struct osd_object *obj,
260 struct osd_thandle *oh)
262 struct osd_mdobj_map *omm = osd->od_mdt_map;
263 struct osd_thread_info *oti = osd_oti_get(env);
264 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
265 char *name = oti->oti_name;
266 struct dentry *dentry;
267 struct dentry *parent;
268 struct ldiskfs_dir_entry_2 *de;
269 struct buffer_head *bh;
272 /* Check lma to see whether it is remote object */
273 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
277 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
280 parent = omm->omm_remote_parent;
281 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
282 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
284 mutex_lock(&parent->d_inode->i_mutex);
285 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
288 mutex_unlock(&parent->d_inode->i_mutex);
291 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
292 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
293 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
294 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
295 mark_inode_dirty(parent->d_inode);
296 mutex_unlock(&parent->d_inode->i_mutex);
299 /* Get rid of REMOTE_PARENT flag from incompat */
300 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
301 lustre_lma_swab(lma);
302 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
303 sizeof(*lma), XATTR_REPLACE);
307 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
308 struct osd_device *osd,
309 const struct lu_fid *fid,
310 struct osd_inode_id *id)
312 struct osd_mdobj_map *omm = osd->od_mdt_map;
313 char *name = oti->oti_name;
314 struct dentry *parent;
315 struct dentry *dentry;
316 struct ldiskfs_dir_entry_2 *de;
317 struct buffer_head *bh;
321 parent = omm->omm_remote_parent;
322 sprintf(name, DFID_NOBRACE, PFID(fid));
323 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
325 mutex_lock(&parent->d_inode->i_mutex);
326 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
333 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
335 inode = osd_iget(oti, osd, id);
345 mutex_unlock(&parent->d_inode->i_mutex);
347 osd_add_oi_cache(oti, osd, id, fid);
352 * directory structure on legacy OST:
354 * O/<seq>/d0-31/<objid>
361 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
363 struct lvfs_run_ctxt new;
364 struct lvfs_run_ctxt save;
365 struct dentry *rootd = osd_sb(dev)->s_root;
367 struct osd_thread_info *info = osd_oti_get(env);
369 struct lu_fid *fid = &info->oti_fid3;
373 OBD_ALLOC_PTR(dev->od_ost_map);
374 if (dev->od_ost_map == NULL)
377 /* to get subdir count from last_rcvd */
378 rc = osd_last_rcvd_subdir_count(dev);
380 GOTO(cleanup_alloc, rc);
382 dev->od_ost_map->om_subdir_count = rc;
385 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
386 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
387 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
389 osd_push_ctxt(dev, &new, &save);
391 d = ll_lookup_one_len("O", rootd, strlen("O"));
393 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
394 if (d->d_inode == NULL) {
396 /* The lookup() may be called again inside simple_mkdir().
397 * Since the repeated lookup() only be called for "/O" at
398 * mount time, it will not affect the whole performance. */
399 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
401 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
403 /* It is quite probably that the device is new formatted. */
404 dev->od_maybe_new = 1;
408 dev->od_ost_map->om_root = d;
410 /* 'What the @fid is' is not imporatant, because the object
411 * has no OI mapping, and only is visible inside the OSD.*/
412 lu_igif_build(fid, inode->i_ino, inode->i_generation);
413 rc = osd_ea_fid_set(info, inode, fid,
414 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
416 GOTO(cleanup_dentry, rc);
418 pop_ctxt(&save, &new);
424 pop_ctxt(&save, &new);
426 OBD_FREE_PTR(dev->od_ost_map);
430 static void osd_seq_free(struct osd_obj_seq *osd_seq)
434 if (osd_seq->oos_dirs) {
435 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
436 if (osd_seq->oos_dirs[j])
437 dput(osd_seq->oos_dirs[j]);
439 OBD_FREE(osd_seq->oos_dirs,
440 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
443 if (osd_seq->oos_root)
444 dput(osd_seq->oos_root);
446 OBD_FREE_PTR(osd_seq);
449 static void osd_ost_fini(struct osd_device *osd)
451 struct osd_obj_seq *osd_seq;
452 struct osd_obj_seq *tmp;
453 struct osd_obj_map *map = osd->od_ost_map;
459 write_lock(&map->om_seq_list_lock);
460 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
462 list_del_init(&osd_seq->oos_seq_list);
463 write_unlock(&map->om_seq_list_lock);
464 osd_seq_free(osd_seq);
465 write_lock(&map->om_seq_list_lock);
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);
493 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
495 struct osd_obj_seq *osd_seq;
497 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
498 if (osd_seq->oos_seq == seq)
504 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
506 struct osd_obj_seq *osd_seq;
508 read_lock(&map->om_seq_list_lock);
509 osd_seq = osd_seq_find_locked(map, seq);
510 read_unlock(&map->om_seq_list_lock);
514 void osd_obj_map_fini(struct osd_device *dev)
521 * Update the specified OI mapping.
523 * \retval 1, changed nothing
524 * \retval 0, changed successfully
525 * \retval -ve, on error
527 static int osd_obj_update_entry(struct osd_thread_info *info,
528 struct osd_device *osd,
529 struct dentry *dir, const char *name,
530 const struct lu_fid *fid,
531 const struct osd_inode_id *id,
534 struct inode *parent = dir->d_inode;
535 struct dentry *child;
536 struct ldiskfs_dir_entry_2 *de;
537 struct buffer_head *bh;
539 struct dentry *dentry = &info->oti_obj_dentry;
540 struct osd_inode_id *oi_id = &info->oti_id3;
541 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
542 struct lu_fid *oi_fid = &lma->lma_self_fid;
547 LASSERT(th->h_transaction != NULL);
549 child = &info->oti_child_dentry;
550 child->d_parent = dir;
551 child->d_name.hash = 0;
552 child->d_name.name = name;
553 child->d_name.len = strlen(name);
555 ll_vfs_dq_init(parent);
556 mutex_lock(&parent->i_mutex);
557 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
559 GOTO(out, rc = PTR_ERR(bh));
561 if (le32_to_cpu(de->inode) == id->oii_ino)
564 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
565 inode = osd_iget(info, osd, oi_id);
568 if (rc == -ENOENT || rc == -ESTALE)
573 /* The EA inode should NOT be in OI, old OI scrub may added
574 * such OI mapping by wrong, replace it. */
575 if (unlikely(osd_is_ea_inode(inode))) {
580 rc = osd_get_lma(info, inode, dentry, lma);
581 if (rc == -ENODATA) {
582 rc = osd_get_idif(info, inode, dentry, oi_fid);
583 if (rc > 0 || rc == -ENODATA) {
593 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
594 * either a crashed object or a uninitialized one. Replace it. */
595 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
596 CERROR("%s: the FID "DFID" is used by two objects: "
597 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
598 oi_id->oii_ino, oi_id->oii_gen,
599 id->oii_ino, id->oii_gen);
600 GOTO(out, rc = -EEXIST);
603 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
604 __u32 idx1 = fid_idif_ost_idx(fid);
605 __u32 idx2 = fid_idif_ost_idx(oi_fid);
606 struct ost_id *ostid = &info->oti_ostid;
607 struct lu_fid *tfid = &info->oti_fid3;
609 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
610 "invalid given FID "DFID", not match the "
611 "device index %u\n", PFID(fid), osd->od_index);
614 if (idx1 == 0 && idx2 == osd->od_index) {
615 fid_to_ostid(fid, ostid);
616 ostid_to_fid(tfid, ostid, idx2);
617 if (lu_fid_eq(tfid, oi_fid)) {
618 CERROR("%s: the FID "DFID" is used by "
619 "two objects(2): %u/%u %u/%u\n",
620 osd_name(osd), PFID(fid),
621 oi_id->oii_ino, oi_id->oii_gen,
622 id->oii_ino, id->oii_gen);
624 GOTO(out, rc = -EEXIST);
626 } else if (idx2 == 0 && idx1 == osd->od_index) {
627 fid_to_ostid(oi_fid, ostid);
628 ostid_to_fid(tfid, ostid, idx1);
629 if (lu_fid_eq(tfid, fid)) {
630 CERROR("%s: the FID "DFID" is used by "
631 "two objects(2): %u/%u %u/%u\n",
632 osd_name(osd), PFID(fid),
633 oi_id->oii_ino, oi_id->oii_gen,
634 id->oii_ino, id->oii_gen);
636 GOTO(out, rc = -EEXIST);
643 /* There may be temporary inconsistency: On one hand, the new
644 * object may be referenced by multiple entries, which is out
645 * of our control unless we traverse the whole /O completely,
646 * which is non-flat order and inefficient, should be avoided;
647 * On the other hand, the old object may become orphan if it
648 * is still valid. Since it was referenced by an invalid entry,
649 * making it as invisible temporary may be not worse. OI scrub
650 * will process it later. */
651 rc = ldiskfs_journal_get_write_access(th, bh);
655 de->inode = cpu_to_le32(id->oii_ino);
656 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
663 mutex_unlock(&parent->i_mutex);
667 static int osd_obj_del_entry(struct osd_thread_info *info,
668 struct osd_device *osd,
669 struct dentry *dird, char *name,
672 struct ldiskfs_dir_entry_2 *de;
673 struct buffer_head *bh;
674 struct dentry *child;
675 struct inode *dir = dird->d_inode;
680 LASSERT(th->h_transaction != NULL);
683 child = &info->oti_child_dentry;
684 child->d_name.hash = 0;
685 child->d_name.name = name;
686 child->d_name.len = strlen(name);
687 child->d_parent = dird;
688 child->d_inode = NULL;
691 mutex_lock(&dir->i_mutex);
692 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
696 rc = ldiskfs_delete_entry(th, dir, de, bh);
699 mutex_unlock(&dir->i_mutex);
704 static int osd_obj_add_entry(struct osd_thread_info *info,
705 struct osd_device *osd,
706 struct dentry *dir, char *name,
707 const struct osd_inode_id *id,
710 struct dentry *child;
716 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
720 LASSERT(th->h_transaction != NULL);
722 inode = info->oti_inode;
723 if (unlikely(inode == NULL)) {
724 struct ldiskfs_inode_info *lii;
728 inode = info->oti_inode = &lii->vfs_inode;
731 inode->i_sb = osd_sb(osd);
732 osd_id_to_inode(inode, id);
733 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
735 child = &info->oti_child_dentry;
736 child->d_name.hash = 0;
737 child->d_name.name = name;
738 child->d_name.len = strlen(name);
739 child->d_parent = dir;
740 child->d_inode = inode;
742 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
745 ll_vfs_dq_init(dir->d_inode);
746 mutex_lock(&dir->d_inode->i_mutex);
747 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
748 mutex_unlock(&dir->d_inode->i_mutex);
754 * Use %llu for legacy OST sequences, but use %llx for new
755 * sequences names, so that the O/{seq}/dN/{oid} more closely
756 * follows the DFID/PFID format. This makes it easier to map from
757 * debug messages to objects in the future, and the legacy space
758 * of FID_SEQ_OST_MDT0 will be unused in the future.
760 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
762 snprintf(seq_name, name_size,
763 (fid_seq_is_rsvd(seq) ||
764 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
765 fid_seq_is_idif(seq) ? 0 : seq);
768 static inline void osd_oid_name(char *name, size_t name_size,
769 const struct lu_fid *fid, u64 id)
771 snprintf(name, name_size,
772 (fid_seq_is_rsvd(fid_seq(fid)) ||
773 fid_seq_is_mdt0(fid_seq(fid)) ||
774 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
777 /* external locking is required */
778 static int osd_seq_load_locked(struct osd_thread_info *info,
779 struct osd_device *osd,
780 struct osd_obj_seq *osd_seq)
782 struct osd_obj_map *map = osd->od_ost_map;
783 struct dentry *seq_dir;
785 struct lu_fid *fid = &info->oti_fid3;
791 if (osd_seq->oos_root != NULL)
795 LASSERT(map->om_root);
797 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
799 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
801 GOTO(out_err, rc = PTR_ERR(seq_dir));
802 else if (seq_dir->d_inode == NULL)
803 GOTO(out_put, rc = -EFAULT);
805 inode = seq_dir->d_inode;
806 osd_seq->oos_root = seq_dir;
808 /* 'What the @fid is' is not imporatant, because the object
809 * has no OI mapping, and only is visible inside the OSD.*/
810 lu_igif_build(fid, inode->i_ino, inode->i_generation);
811 rc = osd_ea_fid_set(info, inode, fid,
812 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
816 LASSERT(osd_seq->oos_dirs == NULL);
817 OBD_ALLOC(osd_seq->oos_dirs,
818 sizeof(seq_dir) * osd_seq->oos_subdir_count);
819 if (osd_seq->oos_dirs == NULL)
820 GOTO(out_put, rc = -ENOMEM);
822 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
825 snprintf(dir_name, sizeof(dir_name), "d%u", i);
826 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
829 GOTO(out_free, rc = PTR_ERR(dir));
830 } else if (dir->d_inode == NULL) {
832 GOTO(out_free, rc = -EFAULT);
835 inode = dir->d_inode;
836 osd_seq->oos_dirs[i] = dir;
838 /* 'What the @fid is' is not imporatant, because the object
839 * has no OI mapping, and only is visible inside the OSD.*/
840 lu_igif_build(fid, inode->i_ino, inode->i_generation);
841 rc = osd_ea_fid_set(info, inode, fid,
842 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
849 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
850 if (osd_seq->oos_dirs[i] != NULL)
851 dput(osd_seq->oos_dirs[i]);
853 OBD_FREE(osd_seq->oos_dirs,
854 sizeof(seq_dir) * osd_seq->oos_subdir_count);
857 osd_seq->oos_root = NULL;
863 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
864 struct osd_device *osd, u64 seq)
866 struct osd_obj_map *map;
867 struct osd_obj_seq *osd_seq;
871 map = osd->od_ost_map;
873 LASSERT(map->om_root);
875 osd_seq = osd_seq_find(map, seq);
876 if (likely(osd_seq != NULL))
879 /* Serializing init process */
880 mutex_lock(&map->om_dir_init_mutex);
882 /* Check whether the seq has been added */
883 read_lock(&map->om_seq_list_lock);
884 osd_seq = osd_seq_find_locked(map, seq);
885 if (osd_seq != NULL) {
886 read_unlock(&map->om_seq_list_lock);
887 GOTO(cleanup, rc = 0);
889 read_unlock(&map->om_seq_list_lock);
891 OBD_ALLOC_PTR(osd_seq);
893 GOTO(cleanup, rc = -ENOMEM);
895 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
896 osd_seq->oos_seq = seq;
897 /* Init subdir count to be 32, but each seq can have
898 * different subdir count */
899 osd_seq->oos_subdir_count = map->om_subdir_count;
900 rc = osd_seq_load_locked(info, osd, osd_seq);
904 write_lock(&map->om_seq_list_lock);
905 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
906 write_unlock(&map->om_seq_list_lock);
909 mutex_unlock(&map->om_dir_init_mutex);
912 OBD_FREE_PTR(osd_seq);
919 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
920 const struct lu_fid *fid, struct osd_inode_id *id)
922 struct osd_obj_map *map;
923 struct osd_obj_seq *osd_seq;
924 struct dentry *d_seq;
925 struct dentry *child;
926 struct ost_id *ostid = &info->oti_ostid;
929 struct ldiskfs_dir_entry_2 *de;
930 struct buffer_head *bh;
935 /* on the very first lookup we find and open directories */
937 map = dev->od_ost_map;
939 LASSERT(map->om_root);
941 fid_to_ostid(fid, ostid);
942 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
944 RETURN(PTR_ERR(osd_seq));
946 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
947 d_seq = osd_seq->oos_dirs[dirn];
950 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
952 child = &info->oti_child_dentry;
953 child->d_parent = d_seq;
954 child->d_name.hash = 0;
955 child->d_name.name = name;
956 /* XXX: we can use rc from sprintf() instead of strlen() */
957 child->d_name.len = strlen(name);
959 dir = d_seq->d_inode;
960 mutex_lock(&dir->i_mutex);
961 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
962 mutex_unlock(&dir->i_mutex);
967 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
970 inode = osd_iget(info, dev, id);
972 int rc = PTR_ERR(inode);
974 RETURN(rc == -ENOENT ? -ESTALE : rc);
981 int osd_obj_map_insert(struct osd_thread_info *info,
982 struct osd_device *osd,
983 const struct lu_fid *fid,
984 const struct osd_inode_id *id,
987 struct osd_obj_map *map;
988 struct osd_obj_seq *osd_seq;
990 struct ost_id *ostid = &info->oti_ostid;
996 map = osd->od_ost_map;
999 /* map fid to seq:objid */
1000 fid_to_ostid(fid, ostid);
1002 oid = ostid_id(ostid);
1003 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1004 if (IS_ERR(osd_seq))
1005 RETURN(PTR_ERR(osd_seq));
1007 dirn = oid & (osd_seq->oos_subdir_count - 1);
1008 d = osd_seq->oos_dirs[dirn];
1011 osd_oid_name(name, sizeof(name), fid, oid);
1014 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1015 if (rc == -EEXIST) {
1016 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1017 if (unlikely(rc == -ENOENT))
1020 if (unlikely(rc == 1))
1027 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1028 const struct lu_fid *fid, handle_t *th)
1030 struct osd_obj_map *map;
1031 struct osd_obj_seq *osd_seq;
1033 struct ost_id *ostid = &info->oti_ostid;
1038 map = osd->od_ost_map;
1041 /* map fid to seq:objid */
1042 fid_to_ostid(fid, ostid);
1044 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1045 if (IS_ERR(osd_seq))
1046 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1048 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1049 d = osd_seq->oos_dirs[dirn];
1052 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1053 rc = osd_obj_del_entry(info, osd, d, name, th);
1058 int osd_obj_map_update(struct osd_thread_info *info,
1059 struct osd_device *osd,
1060 const struct lu_fid *fid,
1061 const struct osd_inode_id *id,
1064 struct osd_obj_seq *osd_seq;
1066 struct ost_id *ostid = &info->oti_ostid;
1071 fid_to_ostid(fid, ostid);
1072 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1073 if (IS_ERR(osd_seq))
1074 RETURN(PTR_ERR(osd_seq));
1076 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1077 d = osd_seq->oos_dirs[dirn];
1080 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1081 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1086 int osd_obj_map_recover(struct osd_thread_info *info,
1087 struct osd_device *osd,
1088 struct inode *src_parent,
1089 struct dentry *src_child,
1090 const struct lu_fid *fid)
1092 struct osd_obj_seq *osd_seq;
1093 struct dentry *tgt_parent;
1094 struct dentry *tgt_child = &info->oti_child_dentry;
1096 struct inode *inode = src_child->d_inode;
1097 struct ost_id *ostid = &info->oti_ostid;
1099 struct ldiskfs_dir_entry_2 *de;
1100 struct buffer_head *bh;
1106 if (fid_is_last_id(fid)) {
1107 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1108 if (IS_ERR(osd_seq))
1109 RETURN(PTR_ERR(osd_seq));
1111 tgt_parent = osd_seq->oos_root;
1112 tgt_child->d_name.name = "LAST_ID";
1113 tgt_child->d_name.len = strlen("LAST_ID");
1115 fid_to_ostid(fid, ostid);
1116 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1117 if (IS_ERR(osd_seq))
1118 RETURN(PTR_ERR(osd_seq));
1120 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1121 tgt_parent = osd_seq->oos_dirs[dirn];
1122 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1123 tgt_child->d_name.name = name;
1124 tgt_child->d_name.len = strlen(name);
1126 LASSERT(tgt_parent != NULL);
1128 dir = tgt_parent->d_inode;
1129 tgt_child->d_name.hash = 0;
1130 tgt_child->d_parent = tgt_parent;
1131 tgt_child->d_inode = inode;
1133 /* The non-initialized src_child may be destroyed. */
1134 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1135 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1136 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1137 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1139 RETURN(PTR_ERR(jh));
1141 ll_vfs_dq_init(src_parent);
1142 ll_vfs_dq_init(dir);
1144 mutex_lock(&src_parent->i_mutex);
1145 mutex_lock(&dir->i_mutex);
1146 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1148 /* XXX: If some other object occupied the same slot. And If such
1149 * inode is zero-sized and with SUID+SGID, then means it is
1150 * a new created one. Maybe we can remove it and insert the
1151 * original one back to the /O/<seq>/d<x>. But there are
1152 * something to be considered:
1154 * 1) The OST-object under /lost+found has crashed LMA.
1155 * So it should not conflict with the current one.
1157 * 2) There are race conditions that: someone may just want
1158 * to modify the current one. Even if the OI scrub takes
1159 * the object lock when remove the current one, it still
1160 * cause the modification to be lost becasue the target
1161 * has been removed when the RPC service thread waiting
1164 * So keep it there before we have suitable solution. */
1166 mutex_unlock(&dir->i_mutex);
1167 mutex_unlock(&src_parent->i_mutex);
1168 ldiskfs_journal_stop(jh);
1171 /* If the src object has never been modified, then remove it. */
1172 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1173 inode->i_mode & S_ISGID) {
1174 rc = ll_vfs_unlink(src_parent, src_child);
1175 if (unlikely(rc == -ENOENT))
1181 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1183 if (unlikely(IS_ERR(bh)))
1184 GOTO(unlock, rc = PTR_ERR(bh));
1186 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1191 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1196 mutex_unlock(&dir->i_mutex);
1197 mutex_unlock(&src_parent->i_mutex);
1198 ldiskfs_journal_stop(jh);
1202 static struct dentry *
1203 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1204 const struct lu_fid *fid, char **name)
1206 struct dentry *root = ERR_PTR(-ENOENT);
1208 if (fid_is_last_id(fid)) {
1209 struct osd_obj_seq *osd_seq;
1211 /* on creation of LAST_ID we create O/<seq> hierarchy */
1212 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1213 if (IS_ERR(osd_seq))
1214 RETURN((struct dentry *)osd_seq);
1217 root = osd_seq->oos_root;
1219 *name = osd_lf_fid2name(fid);
1221 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1222 else if ((*name)[0])
1223 root = osd_sb(osd)->s_root;
1229 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1230 const struct lu_fid *fid, const struct osd_inode_id *id,
1233 struct dentry *root;
1238 root = osd_object_spec_find(info, osd, fid, &name);
1239 if (!IS_ERR(root)) {
1240 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1250 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1251 const struct lu_fid *fid, const struct osd_inode_id *id,
1254 struct dentry *root;
1259 root = osd_object_spec_find(info, osd, fid, &name);
1260 if (!IS_ERR(root)) {
1261 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1271 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1272 const struct lu_fid *fid, struct osd_inode_id *id)
1274 struct dentry *root;
1275 struct dentry *dentry;
1276 struct inode *inode;
1281 if (fid_is_last_id(fid)) {
1282 struct osd_obj_seq *osd_seq;
1284 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1285 if (IS_ERR(osd_seq))
1286 RETURN(PTR_ERR(osd_seq));
1287 root = osd_seq->oos_root;
1290 root = osd_sb(osd)->s_root;
1291 name = osd_lf_fid2name(fid);
1292 if (name == NULL || strlen(name) == 0)
1296 dentry = ll_lookup_one_len(name, root, strlen(name));
1297 if (!IS_ERR(dentry)) {
1298 inode = dentry->d_inode;
1300 if (is_bad_inode(inode)) {
1303 osd_id_gen(id, inode->i_ino,
1304 inode->i_generation);
1308 /* if dentry is accessible after osd_compat_spec_insert it
1309 * will still contain NULL inode, so don't keep it in cache */
1310 d_invalidate(dentry);