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, 2014, 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();
69 newctxt->umask = current_umask();
72 push_ctxt(save, newctxt);
75 /* utility to make a directory */
76 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
77 const char *name, int mode, int fix)
79 struct dentry *dchild;
83 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
84 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
85 dchild = ll_lookup_one_len(name, dir, strlen(name));
89 if (dchild->d_inode) {
90 int old_mode = dchild->d_inode->i_mode;
91 if (!S_ISDIR(old_mode)) {
92 CERROR("found %s (%lu/%u) is mode %o\n", name,
93 dchild->d_inode->i_ino,
94 dchild->d_inode->i_generation, old_mode);
95 GOTO(out_err, err = -ENOTDIR);
98 /* Fixup directory permissions if necessary */
99 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
101 "fixing permissions on %s from %o to %o\n",
102 name, old_mode, mode);
103 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
104 (old_mode & ~S_IALLUGO);
105 mark_inode_dirty(dchild->d_inode);
107 GOTO(out_up, dchild);
110 err = vfs_mkdir(dir->d_inode, dchild, mode);
118 dchild = ERR_PTR(err);
123 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
125 struct lr_server_data lsd;
126 struct dentry *dlast;
129 int count = OBJ_SUBDIR_COUNT;
133 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
136 return PTR_ERR(dlast);
137 else if (dlast->d_inode == NULL)
141 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
142 if (rc == sizeof(lsd)) {
143 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
144 "subdir count = %d\n", lsd.lsd_uuid,
145 lsd.lsd_subdir_count);
146 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
147 count = le16_to_cpu(lsd.lsd_subdir_count);
148 } else if (rc != 0) {
149 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
161 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
162 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
164 struct lvfs_run_ctxt new;
165 struct lvfs_run_ctxt save;
166 struct dentry *parent;
167 struct osd_mdobj_map *omm;
169 struct osd_thread_info *info = osd_oti_get(env);
170 struct lu_fid *fid = &info->oti_fid3;
174 OBD_ALLOC_PTR(dev->od_mdt_map);
175 if (dev->od_mdt_map == NULL)
178 omm = dev->od_mdt_map;
180 parent = osd_sb(dev)->s_root;
181 osd_push_ctxt(dev, &new, &save);
183 d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
186 GOTO(cleanup, rc = PTR_ERR(d));
188 omm->omm_remote_parent = d;
190 /* Set LMA for remote parent inode */
191 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
192 rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
197 pop_ctxt(&save, &new);
199 if (omm->omm_remote_parent != NULL)
200 dput(omm->omm_remote_parent);
202 dev->od_mdt_map = NULL;
207 static void osd_mdt_fini(struct osd_device *osd)
209 struct osd_mdobj_map *omm = osd->od_mdt_map;
214 if (omm->omm_remote_parent)
215 dput(omm->omm_remote_parent);
218 osd->od_ost_map = NULL;
221 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
222 struct osd_object *obj, struct osd_thandle *oh)
224 struct osd_mdobj_map *omm = osd->od_mdt_map;
225 struct osd_thread_info *oti = osd_oti_get(env);
226 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
227 char *name = oti->oti_name;
228 struct dentry *dentry;
229 struct dentry *parent;
232 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
233 * can identify this object quickly */
234 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
238 lma->lma_incompat |= LMAI_REMOTE_PARENT;
239 lustre_lma_swab(lma);
240 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
241 sizeof(*lma), XATTR_REPLACE);
245 parent = omm->omm_remote_parent;
246 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
247 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
249 mutex_lock(&parent->d_inode->i_mutex);
250 rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
252 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
253 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
254 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
255 mark_inode_dirty(parent->d_inode);
256 mutex_unlock(&parent->d_inode->i_mutex);
260 int osd_delete_from_remote_parent(const struct lu_env *env,
261 struct osd_device *osd,
262 struct osd_object *obj,
263 struct osd_thandle *oh)
265 struct osd_mdobj_map *omm = osd->od_mdt_map;
266 struct osd_thread_info *oti = osd_oti_get(env);
267 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
268 char *name = oti->oti_name;
269 struct dentry *dentry;
270 struct dentry *parent;
271 struct ldiskfs_dir_entry_2 *de;
272 struct buffer_head *bh;
275 /* Check lma to see whether it is remote object */
276 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
280 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
283 parent = omm->omm_remote_parent;
284 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
285 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
287 mutex_lock(&parent->d_inode->i_mutex);
288 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
291 mutex_unlock(&parent->d_inode->i_mutex);
294 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
295 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
296 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
297 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
298 mark_inode_dirty(parent->d_inode);
299 mutex_unlock(&parent->d_inode->i_mutex);
302 /* Get rid of REMOTE_PARENT flag from incompat */
303 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
304 lustre_lma_swab(lma);
305 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
306 sizeof(*lma), XATTR_REPLACE);
310 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
311 struct osd_device *osd,
312 const struct lu_fid *fid,
313 struct osd_inode_id *id)
315 struct osd_mdobj_map *omm = osd->od_mdt_map;
316 char *name = oti->oti_name;
317 struct dentry *parent;
318 struct dentry *dentry;
319 struct ldiskfs_dir_entry_2 *de;
320 struct buffer_head *bh;
324 parent = omm->omm_remote_parent;
325 sprintf(name, DFID_NOBRACE, PFID(fid));
326 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
328 mutex_lock(&parent->d_inode->i_mutex);
329 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
335 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
338 mutex_unlock(&parent->d_inode->i_mutex);
340 osd_add_oi_cache(oti, osd, id, fid);
345 * directory structure on legacy OST:
347 * O/<seq>/d0-31/<objid>
354 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
356 struct lvfs_run_ctxt new;
357 struct lvfs_run_ctxt save;
358 struct dentry *rootd = osd_sb(dev)->s_root;
360 struct osd_thread_info *info = osd_oti_get(env);
362 struct lu_fid *fid = &info->oti_fid3;
366 OBD_ALLOC_PTR(dev->od_ost_map);
367 if (dev->od_ost_map == NULL)
370 /* to get subdir count from last_rcvd */
371 rc = osd_last_rcvd_subdir_count(dev);
373 GOTO(cleanup_alloc, rc);
375 dev->od_ost_map->om_subdir_count = rc;
378 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
379 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
380 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
382 osd_push_ctxt(dev, &new, &save);
384 d = ll_lookup_one_len("O", rootd, strlen("O"));
386 GOTO(cleanup_ctxt, 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_ctxt, 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);
409 GOTO(cleanup_dentry, rc);
411 pop_ctxt(&save, &new);
417 pop_ctxt(&save, &new);
419 OBD_FREE_PTR(dev->od_ost_map);
423 static void osd_seq_free(struct osd_obj_map *map,
424 struct osd_obj_seq *osd_seq)
428 list_del_init(&osd_seq->oos_seq_list);
430 if (osd_seq->oos_dirs) {
431 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
432 if (osd_seq->oos_dirs[j])
433 dput(osd_seq->oos_dirs[j]);
435 OBD_FREE(osd_seq->oos_dirs,
436 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
439 if (osd_seq->oos_root)
440 dput(osd_seq->oos_root);
442 OBD_FREE_PTR(osd_seq);
445 static void osd_ost_fini(struct osd_device *osd)
447 struct osd_obj_seq *osd_seq;
448 struct osd_obj_seq *tmp;
449 struct osd_obj_map *map = osd->od_ost_map;
455 write_lock(&map->om_seq_list_lock);
456 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
458 osd_seq_free(map, osd_seq);
460 write_unlock(&map->om_seq_list_lock);
464 osd->od_ost_map = NULL;
468 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
473 /* prepare structures for OST */
474 rc = osd_ost_init(env, dev);
478 /* prepare structures for MDS */
479 rc = osd_mdt_init(env, dev);
486 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
488 struct osd_obj_seq *osd_seq;
490 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
491 if (osd_seq->oos_seq == seq)
497 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
499 struct osd_obj_seq *osd_seq;
501 read_lock(&map->om_seq_list_lock);
502 osd_seq = osd_seq_find_locked(map, seq);
503 read_unlock(&map->om_seq_list_lock);
507 void osd_obj_map_fini(struct osd_device *dev)
514 * Update the specified OI mapping.
516 * \retval 1, changed nothing
517 * \retval 0, changed successfully
518 * \retval -ve, on error
520 static int osd_obj_update_entry(struct osd_thread_info *info,
521 struct osd_device *osd,
522 struct dentry *dir, const char *name,
523 const struct lu_fid *fid,
524 const struct osd_inode_id *id,
527 struct inode *parent = dir->d_inode;
528 struct dentry *child;
529 struct ldiskfs_dir_entry_2 *de;
530 struct buffer_head *bh;
532 struct dentry *dentry = &info->oti_obj_dentry;
533 struct osd_inode_id *oi_id = &info->oti_id3;
534 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
535 struct lu_fid *oi_fid = &lma->lma_self_fid;
540 LASSERT(th->h_transaction != NULL);
542 child = &info->oti_child_dentry;
543 child->d_parent = dir;
544 child->d_name.hash = 0;
545 child->d_name.name = name;
546 child->d_name.len = strlen(name);
548 ll_vfs_dq_init(parent);
549 mutex_lock(&parent->i_mutex);
550 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
552 GOTO(out, rc = -ENOENT);
554 if (le32_to_cpu(de->inode) == id->oii_ino)
557 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
558 inode = osd_iget(info, osd, oi_id);
561 if (rc == -ENOENT || rc == -ESTALE)
566 rc = osd_get_lma(info, inode, dentry, lma);
567 if (rc == -ENODATA) {
568 rc = osd_get_idif(info, inode, dentry, oi_fid);
576 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
577 * either a crashed object or a uninitialized one. Replace it. */
578 if (rc == -ENODATA || oi_fid == NULL)
584 if (lu_fid_eq(fid, oi_fid)) {
585 CERROR("%s: the FID "DFID" is used by two objects: "
586 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
587 oi_id->oii_ino, oi_id->oii_gen,
588 id->oii_ino, id->oii_gen);
589 GOTO(out, rc = -EEXIST);
593 /* There may be temporary inconsistency: On one hand, the new
594 * object may be referenced by multiple entries, which is out
595 * of our control unless we traverse the whole /O completely,
596 * which is non-flat order and inefficient, should be avoided;
597 * On the other hand, the old object may become orphan if it
598 * is still valid. Since it was referenced by an invalid entry,
599 * making it as invisible temporary may be not worse. OI scrub
600 * will process it later. */
601 rc = ldiskfs_journal_get_write_access(th, bh);
605 de->inode = cpu_to_le32(id->oii_ino);
606 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
612 mutex_unlock(&parent->i_mutex);
616 static int osd_obj_del_entry(struct osd_thread_info *info,
617 struct osd_device *osd,
618 struct dentry *dird, char *name,
621 struct ldiskfs_dir_entry_2 *de;
622 struct buffer_head *bh;
623 struct dentry *child;
624 struct inode *dir = dird->d_inode;
629 LASSERT(th->h_transaction != NULL);
632 child = &info->oti_child_dentry;
633 child->d_name.hash = 0;
634 child->d_name.name = name;
635 child->d_name.len = strlen(name);
636 child->d_parent = dird;
637 child->d_inode = NULL;
640 mutex_lock(&dir->i_mutex);
642 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
644 rc = ldiskfs_delete_entry(th, dir, de, bh);
647 mutex_unlock(&dir->i_mutex);
652 static int osd_obj_add_entry(struct osd_thread_info *info,
653 struct osd_device *osd,
654 struct dentry *dir, char *name,
655 const struct osd_inode_id *id,
658 struct dentry *child;
664 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
668 LASSERT(th->h_transaction != NULL);
670 inode = info->oti_inode;
671 if (unlikely(inode == NULL)) {
672 OBD_ALLOC_PTR(inode);
675 info->oti_inode = inode;
678 inode->i_sb = osd_sb(osd);
679 osd_id_to_inode(inode, id);
680 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
682 child = &info->oti_child_dentry;
683 child->d_name.hash = 0;
684 child->d_name.name = name;
685 child->d_name.len = strlen(name);
686 child->d_parent = dir;
687 child->d_inode = inode;
689 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
692 ll_vfs_dq_init(dir->d_inode);
693 mutex_lock(&dir->d_inode->i_mutex);
694 rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
695 mutex_unlock(&dir->d_inode->i_mutex);
701 * Use LPU64 for legacy OST sequences, but use LPX64i for new
702 * sequences names, so that the O/{seq}/dN/{oid} more closely
703 * follows the DFID/PFID format. This makes it easier to map from
704 * debug messages to objects in the future, and the legacy space
705 * of FID_SEQ_OST_MDT0 will be unused in the future.
707 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
709 snprintf(seq_name, name_size,
710 (fid_seq_is_rsvd(seq) ||
711 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
712 fid_seq_is_idif(seq) ? 0 : seq);
715 static inline void osd_oid_name(char *name, size_t name_size,
716 const struct lu_fid *fid, u64 id)
718 snprintf(name, name_size,
719 (fid_seq_is_rsvd(fid_seq(fid)) ||
720 fid_seq_is_mdt0(fid_seq(fid)) ||
721 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
724 /* external locking is required */
725 static int osd_seq_load_locked(struct osd_thread_info *info,
726 struct osd_device *osd,
727 struct osd_obj_seq *osd_seq)
729 struct osd_obj_map *map = osd->od_ost_map;
730 struct dentry *seq_dir;
732 struct lu_fid *fid = &info->oti_fid3;
738 if (osd_seq->oos_root != NULL)
742 LASSERT(map->om_root);
744 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
746 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
748 GOTO(out_err, rc = PTR_ERR(seq_dir));
749 else if (seq_dir->d_inode == NULL)
750 GOTO(out_put, rc = -EFAULT);
752 inode = seq_dir->d_inode;
753 osd_seq->oos_root = seq_dir;
755 /* 'What the @fid is' is not imporatant, because the object
756 * has no OI mapping, and only is visible inside the OSD.*/
757 lu_igif_build(fid, inode->i_ino, inode->i_generation);
758 rc = osd_ea_fid_set(info, inode, fid,
759 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
763 LASSERT(osd_seq->oos_dirs == NULL);
764 OBD_ALLOC(osd_seq->oos_dirs,
765 sizeof(seq_dir) * osd_seq->oos_subdir_count);
766 if (osd_seq->oos_dirs == NULL)
767 GOTO(out_put, rc = -ENOMEM);
769 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
772 snprintf(dir_name, sizeof(dir_name), "d%u", i);
773 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
776 GOTO(out_free, rc = PTR_ERR(dir));
777 } else if (dir->d_inode == NULL) {
779 GOTO(out_free, rc = -EFAULT);
782 inode = dir->d_inode;
783 osd_seq->oos_dirs[i] = dir;
785 /* 'What the @fid is' is not imporatant, because the object
786 * has no OI mapping, and only is visible inside the OSD.*/
787 lu_igif_build(fid, inode->i_ino, inode->i_generation);
788 rc = osd_ea_fid_set(info, inode, fid,
789 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
796 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
797 if (osd_seq->oos_dirs[i] != NULL)
798 dput(osd_seq->oos_dirs[i]);
800 OBD_FREE(osd_seq->oos_dirs,
801 sizeof(seq_dir) * osd_seq->oos_subdir_count);
804 osd_seq->oos_root = NULL;
810 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
811 struct osd_device *osd, u64 seq)
813 struct osd_obj_map *map;
814 struct osd_obj_seq *osd_seq;
818 map = osd->od_ost_map;
820 LASSERT(map->om_root);
822 osd_seq = osd_seq_find(map, seq);
823 if (likely(osd_seq != NULL))
826 /* Serializing init process */
827 mutex_lock(&map->om_dir_init_mutex);
829 /* Check whether the seq has been added */
830 read_lock(&map->om_seq_list_lock);
831 osd_seq = osd_seq_find_locked(map, seq);
832 if (osd_seq != NULL) {
833 read_unlock(&map->om_seq_list_lock);
834 GOTO(cleanup, rc = 0);
836 read_unlock(&map->om_seq_list_lock);
838 OBD_ALLOC_PTR(osd_seq);
840 GOTO(cleanup, rc = -ENOMEM);
842 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
843 osd_seq->oos_seq = seq;
844 /* Init subdir count to be 32, but each seq can have
845 * different subdir count */
846 osd_seq->oos_subdir_count = map->om_subdir_count;
847 rc = osd_seq_load_locked(info, osd, osd_seq);
851 write_lock(&map->om_seq_list_lock);
852 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
853 write_unlock(&map->om_seq_list_lock);
856 mutex_unlock(&map->om_dir_init_mutex);
859 OBD_FREE_PTR(osd_seq);
866 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
867 const struct lu_fid *fid, struct osd_inode_id *id)
869 struct osd_obj_map *map;
870 struct osd_obj_seq *osd_seq;
871 struct dentry *d_seq;
872 struct dentry *child;
873 struct ost_id *ostid = &info->oti_ostid;
876 struct ldiskfs_dir_entry_2 *de;
877 struct buffer_head *bh;
882 /* on the very first lookup we find and open directories */
884 map = dev->od_ost_map;
886 LASSERT(map->om_root);
888 fid_to_ostid(fid, ostid);
889 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
891 RETURN(PTR_ERR(osd_seq));
893 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
894 d_seq = osd_seq->oos_dirs[dirn];
897 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
899 child = &info->oti_child_dentry;
900 child->d_parent = d_seq;
901 child->d_name.hash = 0;
902 child->d_name.name = name;
903 /* XXX: we can use rc from sprintf() instead of strlen() */
904 child->d_name.len = strlen(name);
906 dir = d_seq->d_inode;
907 mutex_lock(&dir->i_mutex);
908 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
909 mutex_unlock(&dir->i_mutex);
914 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
917 inode = osd_iget(info, dev, id);
919 int rc = PTR_ERR(inode);
921 RETURN(rc == -ENOENT ? -ESTALE : rc);
928 int osd_obj_map_insert(struct osd_thread_info *info,
929 struct osd_device *osd,
930 const struct lu_fid *fid,
931 const struct osd_inode_id *id,
934 struct osd_obj_map *map;
935 struct osd_obj_seq *osd_seq;
937 struct ost_id *ostid = &info->oti_ostid;
943 map = osd->od_ost_map;
946 /* map fid to seq:objid */
947 fid_to_ostid(fid, ostid);
949 oid = ostid_id(ostid);
950 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
952 RETURN(PTR_ERR(osd_seq));
954 dirn = oid & (osd_seq->oos_subdir_count - 1);
955 d = osd_seq->oos_dirs[dirn];
958 osd_oid_name(name, sizeof(name), fid, oid);
961 rc = osd_obj_add_entry(info, osd, d, name, id, th);
963 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
964 if (unlikely(rc == -ENOENT))
967 if (unlikely(rc == 1))
974 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
975 const struct lu_fid *fid, handle_t *th)
977 struct osd_obj_map *map;
978 struct osd_obj_seq *osd_seq;
980 struct ost_id *ostid = &info->oti_ostid;
985 map = osd->od_ost_map;
988 /* map fid to seq:objid */
989 fid_to_ostid(fid, ostid);
991 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
993 GOTO(cleanup, rc = PTR_ERR(osd_seq));
995 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
996 d = osd_seq->oos_dirs[dirn];
999 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1000 rc = osd_obj_del_entry(info, osd, d, name, th);
1005 int osd_obj_map_update(struct osd_thread_info *info,
1006 struct osd_device *osd,
1007 const struct lu_fid *fid,
1008 const struct osd_inode_id *id,
1011 struct osd_obj_seq *osd_seq;
1013 struct ost_id *ostid = &info->oti_ostid;
1018 fid_to_ostid(fid, ostid);
1019 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1020 if (IS_ERR(osd_seq))
1021 RETURN(PTR_ERR(osd_seq));
1023 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1024 d = osd_seq->oos_dirs[dirn];
1027 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1028 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1033 int osd_obj_map_recover(struct osd_thread_info *info,
1034 struct osd_device *osd,
1035 struct inode *src_parent,
1036 struct dentry *src_child,
1037 const struct lu_fid *fid)
1039 struct osd_obj_seq *osd_seq;
1040 struct dentry *tgt_parent;
1041 struct dentry *tgt_child = &info->oti_child_dentry;
1043 struct inode *inode = src_child->d_inode;
1044 struct ost_id *ostid = &info->oti_ostid;
1046 struct ldiskfs_dir_entry_2 *de;
1047 struct buffer_head *bh;
1053 if (fid_is_last_id(fid)) {
1054 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1055 if (IS_ERR(osd_seq))
1056 RETURN(PTR_ERR(osd_seq));
1058 tgt_parent = osd_seq->oos_root;
1059 tgt_child->d_name.name = "LAST_ID";
1060 tgt_child->d_name.len = strlen("LAST_ID");
1062 fid_to_ostid(fid, ostid);
1063 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1064 if (IS_ERR(osd_seq))
1065 RETURN(PTR_ERR(osd_seq));
1067 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1068 tgt_parent = osd_seq->oos_dirs[dirn];
1069 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1070 tgt_child->d_name.name = name;
1071 tgt_child->d_name.len = strlen(name);
1073 LASSERT(tgt_parent != NULL);
1075 dir = tgt_parent->d_inode;
1076 tgt_child->d_name.hash = 0;
1077 tgt_child->d_parent = tgt_parent;
1078 tgt_child->d_inode = inode;
1080 /* The non-initialized src_child may be destroyed. */
1081 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1082 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1083 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1084 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1086 RETURN(PTR_ERR(jh));
1088 ll_vfs_dq_init(src_parent);
1089 ll_vfs_dq_init(dir);
1091 mutex_lock(&src_parent->i_mutex);
1092 mutex_lock(&dir->i_mutex);
1093 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1095 /* XXX: If some other object occupied the same slot. And If such
1096 * inode is zero-sized and with SUID+SGID, then means it is
1097 * a new created one. Maybe we can remove it and insert the
1098 * original one back to the /O/<seq>/d<x>. But there are
1099 * something to be considered:
1101 * 1) The OST-object under /lost+found has crashed LMA.
1102 * So it should not conflict with the current one.
1104 * 2) There are race conditions that: someone may just want
1105 * to modify the current one. Even if the OI scrub takes
1106 * the object lock when remove the current one, it still
1107 * cause the modification to be lost becasue the target
1108 * has been removed when the RPC service thread waiting
1111 * So keep it there before we have suitable solution. */
1113 mutex_unlock(&dir->i_mutex);
1114 mutex_unlock(&src_parent->i_mutex);
1115 ldiskfs_journal_stop(jh);
1118 /* If the src object has never been modified, then remove it. */
1119 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1120 inode->i_mode & S_ISGID) {
1121 rc = ll_vfs_unlink(src_parent, src_child);
1122 if (unlikely(rc == -ENOENT))
1128 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1130 if (unlikely(bh == NULL))
1131 GOTO(unlock, rc = -ENOENT);
1133 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1138 rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1143 mutex_unlock(&dir->i_mutex);
1144 mutex_unlock(&src_parent->i_mutex);
1145 ldiskfs_journal_stop(jh);
1149 static struct dentry *
1150 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1151 const struct lu_fid *fid, char **name)
1153 struct dentry *root = ERR_PTR(-ENOENT);
1155 if (fid_is_last_id(fid)) {
1156 struct osd_obj_seq *osd_seq;
1158 /* on creation of LAST_ID we create O/<seq> hierarchy */
1159 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1160 if (IS_ERR(osd_seq))
1161 RETURN((struct dentry *)osd_seq);
1164 root = osd_seq->oos_root;
1166 *name = osd_lf_fid2name(fid);
1168 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1169 else if ((*name)[0])
1170 root = osd_sb(osd)->s_root;
1176 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1177 const struct lu_fid *fid, const struct osd_inode_id *id,
1180 struct dentry *root;
1185 root = osd_object_spec_find(info, osd, fid, &name);
1186 if (!IS_ERR(root)) {
1187 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1197 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1198 const struct lu_fid *fid, const struct osd_inode_id *id,
1201 struct dentry *root;
1206 root = osd_object_spec_find(info, osd, fid, &name);
1207 if (!IS_ERR(root)) {
1208 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1218 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1219 const struct lu_fid *fid, struct osd_inode_id *id)
1221 struct dentry *root;
1222 struct dentry *dentry;
1223 struct inode *inode;
1228 if (fid_is_last_id(fid)) {
1229 struct osd_obj_seq *osd_seq;
1231 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1232 if (IS_ERR(osd_seq))
1233 RETURN(PTR_ERR(osd_seq));
1234 root = osd_seq->oos_root;
1237 root = osd_sb(osd)->s_root;
1238 name = osd_lf_fid2name(fid);
1239 if (name == NULL || strlen(name) == 0)
1243 dentry = ll_lookup_one_len(name, root, strlen(name));
1244 if (!IS_ERR(dentry)) {
1245 inode = dentry->d_inode;
1247 if (is_bad_inode(inode)) {
1250 osd_id_gen(id, inode->i_ino,
1251 inode->i_generation);
1255 /* if dentry is accessible after osd_compat_spec_insert it
1256 * will still contain NULL inode, so don't keep it in cache */
1257 d_invalidate(dentry);