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);
73 /* utility to make a directory */
74 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
75 const char *name, int mode, int fix)
77 struct dentry *dchild;
81 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
82 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
83 dchild = ll_lookup_one_len(name, dir, strlen(name));
87 if (dchild->d_inode) {
88 int old_mode = dchild->d_inode->i_mode;
89 if (!S_ISDIR(old_mode)) {
90 CERROR("found %s (%lu/%u) is mode %o\n", name,
91 dchild->d_inode->i_ino,
92 dchild->d_inode->i_generation, old_mode);
93 GOTO(out_err, err = -ENOTDIR);
96 /* Fixup directory permissions if necessary */
97 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
99 "fixing permissions on %s from %o to %o\n",
100 name, old_mode, mode);
101 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
102 (old_mode & ~S_IALLUGO);
103 mark_inode_dirty(dchild->d_inode);
105 GOTO(out_up, dchild);
108 err = vfs_mkdir(dir->d_inode, dchild, mode);
116 dchild = ERR_PTR(err);
121 int osd_last_rcvd_subdir_count(struct osd_device *osd)
123 struct lr_server_data lsd;
124 struct dentry *dlast;
127 int count = OBJ_SUBDIR_COUNT;
131 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
134 return PTR_ERR(dlast);
135 else if (dlast->d_inode == NULL)
139 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
140 if (rc == sizeof(lsd)) {
141 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
142 "subdir count = %d\n", lsd.lsd_uuid,
143 lsd.lsd_subdir_count);
144 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
145 count = le16_to_cpu(lsd.lsd_subdir_count);
146 } else if (rc != 0) {
147 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
159 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
160 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
162 struct lvfs_run_ctxt new;
163 struct lvfs_run_ctxt save;
164 struct dentry *parent;
165 struct osd_mdobj_map *omm;
167 struct osd_thread_info *info = osd_oti_get(env);
168 struct lu_fid *fid = &info->oti_fid3;
172 OBD_ALLOC_PTR(dev->od_mdt_map);
173 if (dev->od_mdt_map == NULL)
176 omm = dev->od_mdt_map;
178 parent = osd_sb(dev)->s_root;
179 osd_push_ctxt(dev, &new, &save);
181 d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
184 GOTO(cleanup, rc = PTR_ERR(d));
186 omm->omm_remote_parent = d;
188 /* Set LMA for remote parent inode */
189 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
190 rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
195 pop_ctxt(&save, &new);
197 if (omm->omm_remote_parent != NULL)
198 dput(omm->omm_remote_parent);
200 dev->od_mdt_map = NULL;
205 static void osd_mdt_fini(struct osd_device *osd)
207 struct osd_mdobj_map *omm = osd->od_mdt_map;
212 if (omm->omm_remote_parent)
213 dput(omm->omm_remote_parent);
216 osd->od_ost_map = NULL;
219 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
220 struct osd_object *obj, struct osd_thandle *oh)
222 struct osd_mdobj_map *omm = osd->od_mdt_map;
223 struct osd_thread_info *oti = osd_oti_get(env);
224 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
225 char *name = oti->oti_name;
226 struct dentry *dentry;
227 struct dentry *parent;
230 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
231 * can identify this object quickly */
232 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
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(oh->ot_handle, dentry, obj->oo_inode,
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_mdt_attrs;
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, lma);
278 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
281 parent = omm->omm_remote_parent;
282 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
283 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
285 mutex_lock(&parent->d_inode->i_mutex);
286 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
289 mutex_unlock(&parent->d_inode->i_mutex);
292 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
293 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
294 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
295 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
296 mark_inode_dirty(parent->d_inode);
297 mutex_unlock(&parent->d_inode->i_mutex);
300 /* Get rid of REMOTE_PARENT flag from incompat */
301 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
302 lustre_lma_swab(lma);
303 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
304 sizeof(*lma), XATTR_REPLACE);
308 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
309 struct osd_device *osd,
310 const struct lu_fid *fid,
311 struct osd_inode_id *id)
313 struct osd_mdobj_map *omm = osd->od_mdt_map;
314 char *name = oti->oti_name;
315 struct dentry *parent;
316 struct dentry *dentry;
317 struct ldiskfs_dir_entry_2 *de;
318 struct buffer_head *bh;
322 parent = omm->omm_remote_parent;
323 sprintf(name, DFID_NOBRACE, PFID(fid));
324 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
326 mutex_lock(&parent->d_inode->i_mutex);
327 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);
336 mutex_unlock(&parent->d_inode->i_mutex);
338 osd_add_oi_cache(oti, osd, id, fid);
343 * directory structure on legacy OST:
345 * O/<seq>/d0-31/<objid>
352 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
354 struct lvfs_run_ctxt new;
355 struct lvfs_run_ctxt save;
356 struct dentry *rootd = osd_sb(dev)->s_root;
358 struct osd_thread_info *info = osd_oti_get(env);
360 struct lu_fid *fid = &info->oti_fid3;
364 OBD_ALLOC_PTR(dev->od_ost_map);
365 if (dev->od_ost_map == NULL)
368 /* to get subdir count from last_rcvd */
369 rc = osd_last_rcvd_subdir_count(dev);
371 OBD_FREE_PTR(dev->od_ost_map);
375 dev->od_ost_map->om_subdir_count = rc;
378 CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
379 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
380 sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
382 osd_push_ctxt(dev, &new, &save);
384 d = ll_lookup_one_len("O", rootd, strlen("O"));
386 GOTO(cleanup, rc = PTR_ERR(d));
387 if (d->d_inode == NULL) {
389 /* The lookup() may be called again inside simple_mkdir().
390 * Since the repeated lookup() only be called for "/O" at
391 * mount time, it will not affect the whole performance. */
392 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
394 GOTO(cleanup, rc = PTR_ERR(d));
396 /* It is quite probably that the device is new formatted. */
397 dev->od_maybe_new = 1;
401 dev->od_ost_map->om_root = d;
403 /* 'What the @fid is' is not imporatant, because the object
404 * has no OI mapping, and only is visible inside the OSD.*/
405 lu_igif_build(fid, inode->i_ino, inode->i_generation);
406 rc = osd_ea_fid_set(info, inode, fid,
407 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
412 pop_ctxt(&save, &new);
414 OBD_FREE_PTR(dev->od_ost_map);
420 static void osd_seq_free(struct osd_obj_map *map,
421 struct osd_obj_seq *osd_seq)
425 cfs_list_del_init(&osd_seq->oos_seq_list);
427 if (osd_seq->oos_dirs) {
428 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
429 if (osd_seq->oos_dirs[j])
430 dput(osd_seq->oos_dirs[j]);
432 OBD_FREE(osd_seq->oos_dirs,
433 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
436 if (osd_seq->oos_root)
437 dput(osd_seq->oos_root);
439 OBD_FREE_PTR(osd_seq);
442 static void osd_ost_fini(struct osd_device *osd)
444 struct osd_obj_seq *osd_seq;
445 struct osd_obj_seq *tmp;
446 struct osd_obj_map *map = osd->od_ost_map;
452 write_lock(&map->om_seq_list_lock);
453 cfs_list_for_each_entry_safe(osd_seq, tmp,
456 osd_seq_free(map, osd_seq);
458 write_unlock(&map->om_seq_list_lock);
462 osd->od_ost_map = NULL;
466 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
471 /* prepare structures for OST */
472 rc = osd_ost_init(env, dev);
476 /* prepare structures for MDS */
477 rc = osd_mdt_init(env, dev);
482 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
484 struct osd_obj_seq *osd_seq;
486 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
487 if (osd_seq->oos_seq == seq)
493 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
495 struct osd_obj_seq *osd_seq;
497 read_lock(&map->om_seq_list_lock);
498 osd_seq = osd_seq_find_locked(map, seq);
499 read_unlock(&map->om_seq_list_lock);
503 void osd_obj_map_fini(struct osd_device *dev)
510 * Update the specified OI mapping.
512 * \retval 1, changed nothing
513 * \retval 0, changed successfully
514 * \retval -ve, on error
516 static int osd_obj_update_entry(struct osd_thread_info *info,
517 struct osd_device *osd,
518 struct dentry *dir, const char *name,
519 const struct lu_fid *fid,
520 const struct osd_inode_id *id,
523 struct inode *parent = dir->d_inode;
524 struct dentry *child;
525 struct ldiskfs_dir_entry_2 *de;
526 struct buffer_head *bh;
528 struct dentry *dentry = &info->oti_obj_dentry;
529 struct osd_inode_id *oi_id = &info->oti_id3;
530 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
531 struct lu_fid *oi_fid = &lma->lma_self_fid;
536 LASSERT(th->h_transaction != NULL);
538 child = &info->oti_child_dentry;
539 child->d_parent = dir;
540 child->d_name.hash = 0;
541 child->d_name.name = name;
542 child->d_name.len = strlen(name);
544 ll_vfs_dq_init(parent);
545 mutex_lock(&parent->i_mutex);
546 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
548 GOTO(out, rc = -ENOENT);
550 if (le32_to_cpu(de->inode) == id->oii_ino)
553 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
554 inode = osd_iget(info, osd, oi_id);
557 if (rc == -ENOENT || rc == -ESTALE)
562 rc = osd_get_lma(info, inode, dentry, lma);
563 if (rc == -ENODATA) {
564 rc = osd_get_idif(info, inode, dentry, oi_fid);
572 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
573 * either a crashed object or a uninitialized one. Replace it. */
574 if (rc == -ENODATA || oi_fid == NULL)
580 if (lu_fid_eq(fid, oi_fid)) {
581 CERROR("%s: the FID "DFID" is used by two objects: "
582 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
583 oi_id->oii_ino, oi_id->oii_gen,
584 id->oii_ino, id->oii_gen);
585 GOTO(out, rc = -EEXIST);
589 /* There may be temporary inconsistency: On one hand, the new
590 * object may be referenced by multiple entries, which is out
591 * of our control unless we traverse the whole /O completely,
592 * which is non-flat order and inefficient, should be avoided;
593 * On the other hand, the old object may become orphan if it
594 * is still valid. Since it was referenced by an invalid entry,
595 * making it as invisible temporary may be not worse. OI scrub
596 * will process it later. */
597 rc = ldiskfs_journal_get_write_access(th, bh);
601 de->inode = cpu_to_le32(id->oii_ino);
602 rc = ldiskfs_journal_dirty_metadata(th, bh);
608 mutex_unlock(&parent->i_mutex);
612 static int osd_obj_del_entry(struct osd_thread_info *info,
613 struct osd_device *osd,
614 struct dentry *dird, char *name,
617 struct ldiskfs_dir_entry_2 *de;
618 struct buffer_head *bh;
619 struct dentry *child;
620 struct inode *dir = dird->d_inode;
625 LASSERT(th->h_transaction != NULL);
628 child = &info->oti_child_dentry;
629 child->d_name.hash = 0;
630 child->d_name.name = name;
631 child->d_name.len = strlen(name);
632 child->d_parent = dird;
633 child->d_inode = NULL;
636 mutex_lock(&dir->i_mutex);
638 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
640 rc = ldiskfs_delete_entry(th, dir, de, bh);
643 mutex_unlock(&dir->i_mutex);
648 int osd_obj_add_entry(struct osd_thread_info *info,
649 struct osd_device *osd,
650 struct dentry *dir, char *name,
651 const struct osd_inode_id *id,
654 struct dentry *child;
660 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
664 LASSERT(th->h_transaction != NULL);
666 inode = &info->oti_inode;
667 inode->i_sb = osd_sb(osd);
668 osd_id_to_inode(inode, id);
669 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
671 child = &info->oti_child_dentry;
672 child->d_name.hash = 0;
673 child->d_name.name = name;
674 child->d_name.len = strlen(name);
675 child->d_parent = dir;
676 child->d_inode = inode;
678 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
681 ll_vfs_dq_init(dir->d_inode);
682 mutex_lock(&dir->d_inode->i_mutex);
683 rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
684 mutex_unlock(&dir->d_inode->i_mutex);
690 * Use LPU64 for legacy OST sequences, but use LPX64i for new
691 * sequences names, so that the O/{seq}/dN/{oid} more closely
692 * follows the DFID/PFID format. This makes it easier to map from
693 * debug messages to objects in the future, and the legacy space
694 * of FID_SEQ_OST_MDT0 will be unused in the future.
696 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
698 snprintf(seq_name, name_size,
699 (fid_seq_is_rsvd(seq) ||
700 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
701 fid_seq_is_idif(seq) ? 0 : seq);
704 static inline void osd_oid_name(char *name, size_t name_size,
705 const struct lu_fid *fid, obd_id id)
707 snprintf(name, name_size,
708 (fid_seq_is_rsvd(fid_seq(fid)) ||
709 fid_seq_is_mdt0(fid_seq(fid)) ||
710 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
713 /* external locking is required */
714 static int osd_seq_load_locked(struct osd_thread_info *info,
715 struct osd_device *osd,
716 struct osd_obj_seq *osd_seq)
718 struct osd_obj_map *map = osd->od_ost_map;
719 struct dentry *seq_dir;
721 struct lu_fid *fid = &info->oti_fid3;
727 if (osd_seq->oos_root != NULL)
731 LASSERT(map->om_root);
733 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
735 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
737 GOTO(out_err, rc = PTR_ERR(seq_dir));
738 else if (seq_dir->d_inode == NULL)
739 GOTO(out_put, rc = -EFAULT);
741 inode = seq_dir->d_inode;
742 osd_seq->oos_root = seq_dir;
744 /* 'What the @fid is' is not imporatant, because the object
745 * has no OI mapping, and only is visible inside the OSD.*/
746 lu_igif_build(fid, inode->i_ino, inode->i_generation);
747 rc = osd_ea_fid_set(info, inode, fid,
748 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
752 LASSERT(osd_seq->oos_dirs == NULL);
753 OBD_ALLOC(osd_seq->oos_dirs,
754 sizeof(seq_dir) * osd_seq->oos_subdir_count);
755 if (osd_seq->oos_dirs == NULL)
756 GOTO(out_put, rc = -ENOMEM);
758 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
761 snprintf(dir_name, sizeof(dir_name), "d%u", i);
762 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
765 GOTO(out_free, rc = PTR_ERR(dir));
766 } else if (dir->d_inode == NULL) {
768 GOTO(out_free, rc = -EFAULT);
771 inode = dir->d_inode;
772 osd_seq->oos_dirs[i] = dir;
774 /* 'What the @fid is' is not imporatant, because the object
775 * has no OI mapping, and only is visible inside the OSD.*/
776 lu_igif_build(fid, inode->i_ino, inode->i_generation);
777 rc = osd_ea_fid_set(info, inode, fid,
778 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
785 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
786 if (osd_seq->oos_dirs[i] != NULL)
787 dput(osd_seq->oos_dirs[i]);
789 OBD_FREE(osd_seq->oos_dirs,
790 sizeof(seq_dir) * osd_seq->oos_subdir_count);
793 osd_seq->oos_root = NULL;
799 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
800 struct osd_device *osd, obd_seq seq)
802 struct osd_obj_map *map;
803 struct osd_obj_seq *osd_seq;
807 map = osd->od_ost_map;
809 LASSERT(map->om_root);
811 osd_seq = osd_seq_find(map, seq);
812 if (likely(osd_seq != NULL))
815 /* Serializing init process */
816 down(&map->om_dir_init_sem);
818 /* Check whether the seq has been added */
819 read_lock(&map->om_seq_list_lock);
820 osd_seq = osd_seq_find_locked(map, seq);
821 if (osd_seq != NULL) {
822 read_unlock(&map->om_seq_list_lock);
823 GOTO(cleanup, rc = 0);
825 read_unlock(&map->om_seq_list_lock);
827 OBD_ALLOC_PTR(osd_seq);
829 GOTO(cleanup, rc = -ENOMEM);
831 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
832 osd_seq->oos_seq = seq;
833 /* Init subdir count to be 32, but each seq can have
834 * different subdir count */
835 osd_seq->oos_subdir_count = map->om_subdir_count;
836 rc = osd_seq_load_locked(info, osd, osd_seq);
840 write_lock(&map->om_seq_list_lock);
841 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
842 write_unlock(&map->om_seq_list_lock);
845 up(&map->om_dir_init_sem);
848 OBD_FREE_PTR(osd_seq);
855 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
856 const struct lu_fid *fid, struct osd_inode_id *id)
858 struct osd_obj_map *map;
859 struct osd_obj_seq *osd_seq;
860 struct dentry *d_seq;
861 struct dentry *child;
862 struct ost_id *ostid = &info->oti_ostid;
865 struct ldiskfs_dir_entry_2 *de;
866 struct buffer_head *bh;
871 /* on the very first lookup we find and open directories */
873 map = dev->od_ost_map;
875 LASSERT(map->om_root);
877 fid_to_ostid(fid, ostid);
878 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
880 RETURN(PTR_ERR(osd_seq));
882 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
883 d_seq = osd_seq->oos_dirs[dirn];
886 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
888 child = &info->oti_child_dentry;
889 child->d_parent = d_seq;
890 child->d_name.hash = 0;
891 child->d_name.name = name;
892 /* XXX: we can use rc from sprintf() instead of strlen() */
893 child->d_name.len = strlen(name);
895 dir = d_seq->d_inode;
896 mutex_lock(&dir->i_mutex);
897 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
898 mutex_unlock(&dir->i_mutex);
903 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
906 inode = osd_iget(info, dev, id);
908 RETURN(PTR_ERR(inode));
914 int osd_obj_map_insert(struct osd_thread_info *info,
915 struct osd_device *osd,
916 const struct lu_fid *fid,
917 const struct osd_inode_id *id,
920 struct osd_obj_map *map;
921 struct osd_obj_seq *osd_seq;
923 struct ost_id *ostid = &info->oti_ostid;
929 map = osd->od_ost_map;
932 /* map fid to seq:objid */
933 fid_to_ostid(fid, ostid);
935 oid = ostid_id(ostid);
936 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
938 RETURN(PTR_ERR(osd_seq));
940 dirn = oid & (osd_seq->oos_subdir_count - 1);
941 d = osd_seq->oos_dirs[dirn];
944 osd_oid_name(name, sizeof(name), fid, oid);
947 rc = osd_obj_add_entry(info, osd, d, name, id, th);
949 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
950 if (unlikely(rc == -ENOENT))
953 if (unlikely(rc == 1))
960 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
961 const struct lu_fid *fid, handle_t *th)
963 struct osd_obj_map *map;
964 struct osd_obj_seq *osd_seq;
966 struct ost_id *ostid = &info->oti_ostid;
971 map = osd->od_ost_map;
974 /* map fid to seq:objid */
975 fid_to_ostid(fid, ostid);
977 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
979 GOTO(cleanup, rc = PTR_ERR(osd_seq));
981 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
982 d = osd_seq->oos_dirs[dirn];
985 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
986 rc = osd_obj_del_entry(info, osd, d, name, th);
991 int osd_obj_map_update(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_seq *osd_seq;
999 struct ost_id *ostid = &info->oti_ostid;
1004 fid_to_ostid(fid, ostid);
1005 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1006 if (IS_ERR(osd_seq))
1007 RETURN(PTR_ERR(osd_seq));
1009 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1010 d = osd_seq->oos_dirs[dirn];
1013 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1014 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1019 int osd_obj_map_recover(struct osd_thread_info *info,
1020 struct osd_device *osd,
1021 struct inode *src_parent,
1022 struct dentry *src_child,
1023 const struct lu_fid *fid)
1025 struct osd_obj_seq *osd_seq;
1026 struct dentry *tgt_parent;
1027 struct dentry *tgt_child = &info->oti_child_dentry;
1029 struct inode *inode = src_child->d_inode;
1030 struct ost_id *ostid = &info->oti_ostid;
1032 struct ldiskfs_dir_entry_2 *de;
1033 struct buffer_head *bh;
1039 if (fid_is_last_id(fid)) {
1040 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1041 if (IS_ERR(osd_seq))
1042 RETURN(PTR_ERR(osd_seq));
1044 tgt_parent = osd_seq->oos_root;
1045 tgt_child->d_name.name = "LAST_ID";
1046 tgt_child->d_name.len = strlen("LAST_ID");
1048 fid_to_ostid(fid, ostid);
1049 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1050 if (IS_ERR(osd_seq))
1051 RETURN(PTR_ERR(osd_seq));
1053 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1054 tgt_parent = osd_seq->oos_dirs[dirn];
1055 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1056 tgt_child->d_name.name = name;
1057 tgt_child->d_name.len = strlen(name);
1059 LASSERT(tgt_parent != NULL);
1061 dir = tgt_parent->d_inode;
1062 tgt_child->d_name.hash = 0;
1063 tgt_child->d_parent = tgt_parent;
1064 tgt_child->d_inode = inode;
1066 /* The non-initialized src_child may be destroyed. */
1067 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1068 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1069 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1070 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1072 RETURN(PTR_ERR(jh));
1074 ll_vfs_dq_init(src_parent);
1075 ll_vfs_dq_init(dir);
1077 mutex_lock(&src_parent->i_mutex);
1078 mutex_lock(&dir->i_mutex);
1079 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1081 /* XXX: If some other object occupied the same slot. And If such
1082 * inode is zero-sized and with SUID+SGID, then means it is
1083 * a new created one. Maybe we can remove it and insert the
1084 * original one back to the /O/<seq>/d<x>. But there are
1085 * something to be considered:
1087 * 1) The OST-object under /lost+found has crashed LMA.
1088 * So it should not conflict with the current one.
1090 * 2) There are race conditions that: someone may just want
1091 * to modify the current one. Even if the OI scrub takes
1092 * the object lock when remove the current one, it still
1093 * cause the modification to be lost becasue the target
1094 * has been removed when the RPC service thread waiting
1097 * So keep it there before we have suitable solution. */
1099 mutex_unlock(&dir->i_mutex);
1100 mutex_unlock(&src_parent->i_mutex);
1101 ldiskfs_journal_stop(jh);
1104 /* If the src object has never been modified, then remove it. */
1105 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1106 inode->i_mode & S_ISGID) {
1107 rc = vfs_unlink(src_parent, src_child);
1108 if (unlikely(rc == -ENOENT))
1114 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1116 if (unlikely(bh == NULL))
1117 GOTO(unlock, rc = -ENOENT);
1119 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1124 rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1129 mutex_unlock(&dir->i_mutex);
1130 mutex_unlock(&src_parent->i_mutex);
1131 ldiskfs_journal_stop(jh);
1135 static struct dentry *
1136 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1137 const struct lu_fid *fid, char **name)
1139 struct dentry *root = ERR_PTR(-ENOENT);
1141 if (fid_is_last_id(fid)) {
1142 struct osd_obj_seq *osd_seq;
1144 /* on creation of LAST_ID we create O/<seq> hierarchy */
1145 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1146 if (IS_ERR(osd_seq))
1147 RETURN((struct dentry *)osd_seq);
1150 root = osd_seq->oos_root;
1152 *name = osd_lf_fid2name(fid);
1154 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1155 else if ((*name)[0])
1156 root = osd_sb(osd)->s_root;
1162 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1163 const struct lu_fid *fid, const struct osd_inode_id *id,
1166 struct dentry *root;
1171 root = osd_object_spec_find(info, osd, fid, &name);
1172 if (!IS_ERR(root)) {
1173 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1183 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1184 const struct lu_fid *fid, const struct osd_inode_id *id,
1187 struct dentry *root;
1192 root = osd_object_spec_find(info, osd, fid, &name);
1193 if (!IS_ERR(root)) {
1194 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1204 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1205 const struct lu_fid *fid, struct osd_inode_id *id)
1207 struct dentry *root;
1208 struct dentry *dentry;
1209 struct inode *inode;
1214 if (fid_is_last_id(fid)) {
1215 struct osd_obj_seq *osd_seq;
1217 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1218 if (IS_ERR(osd_seq))
1219 RETURN(PTR_ERR(osd_seq));
1220 root = osd_seq->oos_root;
1223 root = osd_sb(osd)->s_root;
1224 name = osd_lf_fid2name(fid);
1225 if (name == NULL || strlen(name) == 0)
1229 dentry = ll_lookup_one_len(name, root, strlen(name));
1230 if (!IS_ERR(dentry)) {
1231 inode = dentry->d_inode;
1233 if (is_bad_inode(inode)) {
1236 osd_id_gen(id, inode->i_ino,
1237 inode->i_generation);
1241 /* if dentry is accessible after osd_compat_spec_insert it
1242 * will still contain NULL inode, so don't keep it in cache */
1243 d_invalidate(dentry);