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, 2015, 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 osd_thread_info *info = osd_oti_get(env);
229 struct dentry *dentry;
230 struct dentry *parent;
233 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
234 * can identify this object quickly */
235 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
239 lma->lma_incompat |= LMAI_REMOTE_PARENT;
240 lustre_lma_swab(lma);
241 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
242 sizeof(*lma), XATTR_REPLACE);
246 parent = omm->omm_remote_parent;
247 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
248 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
250 mutex_lock(&parent->d_inode->i_mutex);
251 rc = osd_ldiskfs_add_entry(info, oh->ot_handle, dentry,
252 obj->oo_inode, NULL);
253 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
254 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
255 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
256 mark_inode_dirty(parent->d_inode);
257 mutex_unlock(&parent->d_inode->i_mutex);
261 int osd_delete_from_remote_parent(const struct lu_env *env,
262 struct osd_device *osd,
263 struct osd_object *obj,
264 struct osd_thandle *oh)
266 struct osd_mdobj_map *omm = osd->od_mdt_map;
267 struct osd_thread_info *oti = osd_oti_get(env);
268 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
269 char *name = oti->oti_name;
270 struct dentry *dentry;
271 struct dentry *parent;
272 struct ldiskfs_dir_entry_2 *de;
273 struct buffer_head *bh;
276 /* Check lma to see whether it is remote object */
277 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
281 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
284 parent = omm->omm_remote_parent;
285 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
286 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
288 mutex_lock(&parent->d_inode->i_mutex);
289 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
292 mutex_unlock(&parent->d_inode->i_mutex);
295 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
296 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
297 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
298 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
299 mark_inode_dirty(parent->d_inode);
300 mutex_unlock(&parent->d_inode->i_mutex);
303 /* Get rid of REMOTE_PARENT flag from incompat */
304 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
305 lustre_lma_swab(lma);
306 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
307 sizeof(*lma), XATTR_REPLACE);
311 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
312 struct osd_device *osd,
313 const struct lu_fid *fid,
314 struct osd_inode_id *id)
316 struct osd_mdobj_map *omm = osd->od_mdt_map;
317 char *name = oti->oti_name;
318 struct dentry *parent;
319 struct dentry *dentry;
320 struct ldiskfs_dir_entry_2 *de;
321 struct buffer_head *bh;
325 parent = omm->omm_remote_parent;
326 sprintf(name, DFID_NOBRACE, PFID(fid));
327 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
329 mutex_lock(&parent->d_inode->i_mutex);
330 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
336 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
339 mutex_unlock(&parent->d_inode->i_mutex);
341 osd_add_oi_cache(oti, osd, id, fid);
346 * directory structure on legacy OST:
348 * O/<seq>/d0-31/<objid>
355 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
357 struct lvfs_run_ctxt new;
358 struct lvfs_run_ctxt save;
359 struct dentry *rootd = osd_sb(dev)->s_root;
361 struct osd_thread_info *info = osd_oti_get(env);
363 struct lu_fid *fid = &info->oti_fid3;
367 OBD_ALLOC_PTR(dev->od_ost_map);
368 if (dev->od_ost_map == NULL)
371 /* to get subdir count from last_rcvd */
372 rc = osd_last_rcvd_subdir_count(dev);
374 GOTO(cleanup_alloc, rc);
376 dev->od_ost_map->om_subdir_count = rc;
379 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
380 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
381 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
383 osd_push_ctxt(dev, &new, &save);
385 d = ll_lookup_one_len("O", rootd, strlen("O"));
387 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
388 if (d->d_inode == NULL) {
390 /* The lookup() may be called again inside simple_mkdir().
391 * Since the repeated lookup() only be called for "/O" at
392 * mount time, it will not affect the whole performance. */
393 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
395 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
397 /* It is quite probably that the device is new formatted. */
398 dev->od_maybe_new = 1;
402 dev->od_ost_map->om_root = d;
404 /* 'What the @fid is' is not imporatant, because the object
405 * has no OI mapping, and only is visible inside the OSD.*/
406 lu_igif_build(fid, inode->i_ino, inode->i_generation);
407 rc = osd_ea_fid_set(info, inode, fid,
408 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
410 GOTO(cleanup_dentry, rc);
412 pop_ctxt(&save, &new);
418 pop_ctxt(&save, &new);
420 OBD_FREE_PTR(dev->od_ost_map);
424 static void osd_seq_free(struct osd_obj_seq *osd_seq)
428 if (osd_seq->oos_dirs) {
429 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
430 if (osd_seq->oos_dirs[j])
431 dput(osd_seq->oos_dirs[j]);
433 OBD_FREE(osd_seq->oos_dirs,
434 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
437 if (osd_seq->oos_root)
438 dput(osd_seq->oos_root);
440 OBD_FREE_PTR(osd_seq);
443 static void osd_ost_fini(struct osd_device *osd)
445 struct osd_obj_seq *osd_seq;
446 struct osd_obj_seq *tmp;
447 struct osd_obj_map *map = osd->od_ost_map;
453 write_lock(&map->om_seq_list_lock);
454 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
456 list_del_init(&osd_seq->oos_seq_list);
457 write_unlock(&map->om_seq_list_lock);
458 osd_seq_free(osd_seq);
459 write_lock(&map->om_seq_list_lock);
461 write_unlock(&map->om_seq_list_lock);
465 osd->od_ost_map = NULL;
469 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
474 /* prepare structures for OST */
475 rc = osd_ost_init(env, dev);
479 /* prepare structures for MDS */
480 rc = osd_mdt_init(env, dev);
487 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
489 struct osd_obj_seq *osd_seq;
491 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
492 if (osd_seq->oos_seq == seq)
498 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
500 struct osd_obj_seq *osd_seq;
502 read_lock(&map->om_seq_list_lock);
503 osd_seq = osd_seq_find_locked(map, seq);
504 read_unlock(&map->om_seq_list_lock);
508 void osd_obj_map_fini(struct osd_device *dev)
515 * Update the specified OI mapping.
517 * \retval 1, changed nothing
518 * \retval 0, changed successfully
519 * \retval -ve, on error
521 static int osd_obj_update_entry(struct osd_thread_info *info,
522 struct osd_device *osd,
523 struct dentry *dir, const char *name,
524 const struct lu_fid *fid,
525 const struct osd_inode_id *id,
528 struct inode *parent = dir->d_inode;
529 struct dentry *child;
530 struct ldiskfs_dir_entry_2 *de;
531 struct buffer_head *bh;
533 struct dentry *dentry = &info->oti_obj_dentry;
534 struct osd_inode_id *oi_id = &info->oti_id3;
535 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
536 struct lu_fid *oi_fid = &lma->lma_self_fid;
541 LASSERT(th->h_transaction != NULL);
543 child = &info->oti_child_dentry;
544 child->d_parent = dir;
545 child->d_name.hash = 0;
546 child->d_name.name = name;
547 child->d_name.len = strlen(name);
549 ll_vfs_dq_init(parent);
550 mutex_lock(&parent->i_mutex);
551 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
553 GOTO(out, rc = PTR_ERR(bh));
555 if (le32_to_cpu(de->inode) == id->oii_ino)
558 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
559 inode = osd_iget(info, osd, oi_id);
562 if (rc == -ENOENT || rc == -ESTALE)
567 /* The EA inode should NOT be in OI, old OI scrub may added
568 * such OI mapping by wrong, replace it. */
569 if (unlikely(osd_is_ea_inode(inode))) {
574 rc = osd_get_lma(info, inode, dentry, lma);
575 if (rc == -ENODATA) {
576 rc = osd_get_idif(info, inode, dentry, oi_fid);
577 if (rc > 0 || rc == -ENODATA) {
587 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
588 * either a crashed object or a uninitialized one. Replace it. */
589 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
590 CERROR("%s: the FID "DFID" is used by two objects: "
591 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
592 oi_id->oii_ino, oi_id->oii_gen,
593 id->oii_ino, id->oii_gen);
594 GOTO(out, rc = -EEXIST);
597 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
598 __u32 idx1 = fid_idif_ost_idx(fid);
599 __u32 idx2 = fid_idif_ost_idx(oi_fid);
600 struct ost_id *ostid = &info->oti_ostid;
601 struct lu_fid *tfid = &info->oti_fid3;
603 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
604 "invalid given FID "DFID", not match the "
605 "device index %u\n", PFID(fid), osd->od_index);
608 if (idx1 == 0 && idx2 == osd->od_index) {
609 fid_to_ostid(fid, ostid);
610 ostid_to_fid(tfid, ostid, idx2);
611 if (lu_fid_eq(tfid, oi_fid)) {
612 CERROR("%s: the FID "DFID" is used by "
613 "two objects(2): %u/%u %u/%u\n",
614 osd_name(osd), PFID(fid),
615 oi_id->oii_ino, oi_id->oii_gen,
616 id->oii_ino, id->oii_gen);
618 GOTO(out, rc = -EEXIST);
620 } else if (idx2 == 0 && idx1 == osd->od_index) {
621 fid_to_ostid(oi_fid, ostid);
622 ostid_to_fid(tfid, ostid, idx1);
623 if (lu_fid_eq(tfid, fid)) {
624 CERROR("%s: the FID "DFID" is used by "
625 "two objects(2): %u/%u %u/%u\n",
626 osd_name(osd), PFID(fid),
627 oi_id->oii_ino, oi_id->oii_gen,
628 id->oii_ino, id->oii_gen);
630 GOTO(out, rc = -EEXIST);
637 /* There may be temporary inconsistency: On one hand, the new
638 * object may be referenced by multiple entries, which is out
639 * of our control unless we traverse the whole /O completely,
640 * which is non-flat order and inefficient, should be avoided;
641 * On the other hand, the old object may become orphan if it
642 * is still valid. Since it was referenced by an invalid entry,
643 * making it as invisible temporary may be not worse. OI scrub
644 * will process it later. */
645 rc = ldiskfs_journal_get_write_access(th, bh);
649 de->inode = cpu_to_le32(id->oii_ino);
650 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
657 mutex_unlock(&parent->i_mutex);
661 static int osd_obj_del_entry(struct osd_thread_info *info,
662 struct osd_device *osd,
663 struct dentry *dird, char *name,
666 struct ldiskfs_dir_entry_2 *de;
667 struct buffer_head *bh;
668 struct dentry *child;
669 struct inode *dir = dird->d_inode;
674 LASSERT(th->h_transaction != NULL);
677 child = &info->oti_child_dentry;
678 child->d_name.hash = 0;
679 child->d_name.name = name;
680 child->d_name.len = strlen(name);
681 child->d_parent = dird;
682 child->d_inode = NULL;
685 mutex_lock(&dir->i_mutex);
686 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
690 rc = ldiskfs_delete_entry(th, dir, de, bh);
693 mutex_unlock(&dir->i_mutex);
698 static int osd_obj_add_entry(struct osd_thread_info *info,
699 struct osd_device *osd,
700 struct dentry *dir, char *name,
701 const struct osd_inode_id *id,
704 struct dentry *child;
710 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
714 LASSERT(th->h_transaction != NULL);
716 inode = info->oti_inode;
717 if (unlikely(inode == NULL)) {
718 struct ldiskfs_inode_info *lii;
722 inode = info->oti_inode = &lii->vfs_inode;
725 inode->i_sb = osd_sb(osd);
726 osd_id_to_inode(inode, id);
727 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
729 child = &info->oti_child_dentry;
730 child->d_name.hash = 0;
731 child->d_name.name = name;
732 child->d_name.len = strlen(name);
733 child->d_parent = dir;
734 child->d_inode = inode;
736 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
739 ll_vfs_dq_init(dir->d_inode);
740 mutex_lock(&dir->d_inode->i_mutex);
741 rc = osd_ldiskfs_add_entry(info, th, child, inode, NULL);
742 mutex_unlock(&dir->d_inode->i_mutex);
748 * Use LPU64 for legacy OST sequences, but use LPX64i for new
749 * sequences names, so that the O/{seq}/dN/{oid} more closely
750 * follows the DFID/PFID format. This makes it easier to map from
751 * debug messages to objects in the future, and the legacy space
752 * of FID_SEQ_OST_MDT0 will be unused in the future.
754 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
756 snprintf(seq_name, name_size,
757 (fid_seq_is_rsvd(seq) ||
758 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
759 fid_seq_is_idif(seq) ? 0 : seq);
762 static inline void osd_oid_name(char *name, size_t name_size,
763 const struct lu_fid *fid, u64 id)
765 snprintf(name, name_size,
766 (fid_seq_is_rsvd(fid_seq(fid)) ||
767 fid_seq_is_mdt0(fid_seq(fid)) ||
768 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
771 /* external locking is required */
772 static int osd_seq_load_locked(struct osd_thread_info *info,
773 struct osd_device *osd,
774 struct osd_obj_seq *osd_seq)
776 struct osd_obj_map *map = osd->od_ost_map;
777 struct dentry *seq_dir;
779 struct lu_fid *fid = &info->oti_fid3;
785 if (osd_seq->oos_root != NULL)
789 LASSERT(map->om_root);
791 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
793 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
795 GOTO(out_err, rc = PTR_ERR(seq_dir));
796 else if (seq_dir->d_inode == NULL)
797 GOTO(out_put, rc = -EFAULT);
799 inode = seq_dir->d_inode;
800 osd_seq->oos_root = seq_dir;
802 /* 'What the @fid is' is not imporatant, because the object
803 * has no OI mapping, and only is visible inside the OSD.*/
804 lu_igif_build(fid, inode->i_ino, inode->i_generation);
805 rc = osd_ea_fid_set(info, inode, fid,
806 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
810 LASSERT(osd_seq->oos_dirs == NULL);
811 OBD_ALLOC(osd_seq->oos_dirs,
812 sizeof(seq_dir) * osd_seq->oos_subdir_count);
813 if (osd_seq->oos_dirs == NULL)
814 GOTO(out_put, rc = -ENOMEM);
816 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
819 snprintf(dir_name, sizeof(dir_name), "d%u", i);
820 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
823 GOTO(out_free, rc = PTR_ERR(dir));
824 } else if (dir->d_inode == NULL) {
826 GOTO(out_free, rc = -EFAULT);
829 inode = dir->d_inode;
830 osd_seq->oos_dirs[i] = dir;
832 /* 'What the @fid is' is not imporatant, because the object
833 * has no OI mapping, and only is visible inside the OSD.*/
834 lu_igif_build(fid, inode->i_ino, inode->i_generation);
835 rc = osd_ea_fid_set(info, inode, fid,
836 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
843 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
844 if (osd_seq->oos_dirs[i] != NULL)
845 dput(osd_seq->oos_dirs[i]);
847 OBD_FREE(osd_seq->oos_dirs,
848 sizeof(seq_dir) * osd_seq->oos_subdir_count);
851 osd_seq->oos_root = NULL;
857 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
858 struct osd_device *osd, u64 seq)
860 struct osd_obj_map *map;
861 struct osd_obj_seq *osd_seq;
865 map = osd->od_ost_map;
867 LASSERT(map->om_root);
869 osd_seq = osd_seq_find(map, seq);
870 if (likely(osd_seq != NULL))
873 /* Serializing init process */
874 mutex_lock(&map->om_dir_init_mutex);
876 /* Check whether the seq has been added */
877 read_lock(&map->om_seq_list_lock);
878 osd_seq = osd_seq_find_locked(map, seq);
879 if (osd_seq != NULL) {
880 read_unlock(&map->om_seq_list_lock);
881 GOTO(cleanup, rc = 0);
883 read_unlock(&map->om_seq_list_lock);
885 OBD_ALLOC_PTR(osd_seq);
887 GOTO(cleanup, rc = -ENOMEM);
889 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
890 osd_seq->oos_seq = seq;
891 /* Init subdir count to be 32, but each seq can have
892 * different subdir count */
893 osd_seq->oos_subdir_count = map->om_subdir_count;
894 rc = osd_seq_load_locked(info, osd, osd_seq);
898 write_lock(&map->om_seq_list_lock);
899 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
900 write_unlock(&map->om_seq_list_lock);
903 mutex_unlock(&map->om_dir_init_mutex);
906 OBD_FREE_PTR(osd_seq);
913 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
914 const struct lu_fid *fid, struct osd_inode_id *id)
916 struct osd_obj_map *map;
917 struct osd_obj_seq *osd_seq;
918 struct dentry *d_seq;
919 struct dentry *child;
920 struct ost_id *ostid = &info->oti_ostid;
923 struct ldiskfs_dir_entry_2 *de;
924 struct buffer_head *bh;
929 /* on the very first lookup we find and open directories */
931 map = dev->od_ost_map;
933 LASSERT(map->om_root);
935 fid_to_ostid(fid, ostid);
936 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
938 RETURN(PTR_ERR(osd_seq));
940 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
941 d_seq = osd_seq->oos_dirs[dirn];
944 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
946 child = &info->oti_child_dentry;
947 child->d_parent = d_seq;
948 child->d_name.hash = 0;
949 child->d_name.name = name;
950 /* XXX: we can use rc from sprintf() instead of strlen() */
951 child->d_name.len = strlen(name);
953 dir = d_seq->d_inode;
954 mutex_lock(&dir->i_mutex);
955 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
956 mutex_unlock(&dir->i_mutex);
961 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
964 inode = osd_iget(info, dev, id);
966 int rc = PTR_ERR(inode);
968 RETURN(rc == -ENOENT ? -ESTALE : rc);
975 int osd_obj_map_insert(struct osd_thread_info *info,
976 struct osd_device *osd,
977 const struct lu_fid *fid,
978 const struct osd_inode_id *id,
981 struct osd_obj_map *map;
982 struct osd_obj_seq *osd_seq;
984 struct ost_id *ostid = &info->oti_ostid;
990 map = osd->od_ost_map;
993 /* map fid to seq:objid */
994 fid_to_ostid(fid, ostid);
996 oid = ostid_id(ostid);
997 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
999 RETURN(PTR_ERR(osd_seq));
1001 dirn = oid & (osd_seq->oos_subdir_count - 1);
1002 d = osd_seq->oos_dirs[dirn];
1005 osd_oid_name(name, sizeof(name), fid, oid);
1008 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1009 if (rc == -EEXIST) {
1010 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1011 if (unlikely(rc == -ENOENT))
1014 if (unlikely(rc == 1))
1021 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1022 const struct lu_fid *fid, handle_t *th)
1024 struct osd_obj_map *map;
1025 struct osd_obj_seq *osd_seq;
1027 struct ost_id *ostid = &info->oti_ostid;
1032 map = osd->od_ost_map;
1035 /* map fid to seq:objid */
1036 fid_to_ostid(fid, ostid);
1038 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1039 if (IS_ERR(osd_seq))
1040 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1042 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1043 d = osd_seq->oos_dirs[dirn];
1046 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1047 rc = osd_obj_del_entry(info, osd, d, name, th);
1052 int osd_obj_map_update(struct osd_thread_info *info,
1053 struct osd_device *osd,
1054 const struct lu_fid *fid,
1055 const struct osd_inode_id *id,
1058 struct osd_obj_seq *osd_seq;
1060 struct ost_id *ostid = &info->oti_ostid;
1065 fid_to_ostid(fid, ostid);
1066 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1067 if (IS_ERR(osd_seq))
1068 RETURN(PTR_ERR(osd_seq));
1070 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1071 d = osd_seq->oos_dirs[dirn];
1074 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1075 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1080 int osd_obj_map_recover(struct osd_thread_info *info,
1081 struct osd_device *osd,
1082 struct inode *src_parent,
1083 struct dentry *src_child,
1084 const struct lu_fid *fid)
1086 struct osd_obj_seq *osd_seq;
1087 struct dentry *tgt_parent;
1088 struct dentry *tgt_child = &info->oti_child_dentry;
1090 struct inode *inode = src_child->d_inode;
1091 struct ost_id *ostid = &info->oti_ostid;
1093 struct ldiskfs_dir_entry_2 *de;
1094 struct buffer_head *bh;
1100 if (fid_is_last_id(fid)) {
1101 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1102 if (IS_ERR(osd_seq))
1103 RETURN(PTR_ERR(osd_seq));
1105 tgt_parent = osd_seq->oos_root;
1106 tgt_child->d_name.name = "LAST_ID";
1107 tgt_child->d_name.len = strlen("LAST_ID");
1109 fid_to_ostid(fid, ostid);
1110 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1111 if (IS_ERR(osd_seq))
1112 RETURN(PTR_ERR(osd_seq));
1114 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1115 tgt_parent = osd_seq->oos_dirs[dirn];
1116 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1117 tgt_child->d_name.name = name;
1118 tgt_child->d_name.len = strlen(name);
1120 LASSERT(tgt_parent != NULL);
1122 dir = tgt_parent->d_inode;
1123 tgt_child->d_name.hash = 0;
1124 tgt_child->d_parent = tgt_parent;
1125 tgt_child->d_inode = inode;
1127 /* The non-initialized src_child may be destroyed. */
1128 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1129 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1130 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1131 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1133 RETURN(PTR_ERR(jh));
1135 ll_vfs_dq_init(src_parent);
1136 ll_vfs_dq_init(dir);
1138 mutex_lock(&src_parent->i_mutex);
1139 mutex_lock(&dir->i_mutex);
1140 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1142 /* XXX: If some other object occupied the same slot. And If such
1143 * inode is zero-sized and with SUID+SGID, then means it is
1144 * a new created one. Maybe we can remove it and insert the
1145 * original one back to the /O/<seq>/d<x>. But there are
1146 * something to be considered:
1148 * 1) The OST-object under /lost+found has crashed LMA.
1149 * So it should not conflict with the current one.
1151 * 2) There are race conditions that: someone may just want
1152 * to modify the current one. Even if the OI scrub takes
1153 * the object lock when remove the current one, it still
1154 * cause the modification to be lost becasue the target
1155 * has been removed when the RPC service thread waiting
1158 * So keep it there before we have suitable solution. */
1160 mutex_unlock(&dir->i_mutex);
1161 mutex_unlock(&src_parent->i_mutex);
1162 ldiskfs_journal_stop(jh);
1165 /* If the src object has never been modified, then remove it. */
1166 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1167 inode->i_mode & S_ISGID) {
1168 rc = ll_vfs_unlink(src_parent, src_child);
1169 if (unlikely(rc == -ENOENT))
1175 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1177 if (unlikely(IS_ERR(bh)))
1178 GOTO(unlock, rc = PTR_ERR(bh));
1180 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1185 rc = osd_ldiskfs_add_entry(info, jh, tgt_child, inode, NULL);
1190 mutex_unlock(&dir->i_mutex);
1191 mutex_unlock(&src_parent->i_mutex);
1192 ldiskfs_journal_stop(jh);
1196 static struct dentry *
1197 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1198 const struct lu_fid *fid, char **name)
1200 struct dentry *root = ERR_PTR(-ENOENT);
1202 if (fid_is_last_id(fid)) {
1203 struct osd_obj_seq *osd_seq;
1205 /* on creation of LAST_ID we create O/<seq> hierarchy */
1206 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1207 if (IS_ERR(osd_seq))
1208 RETURN((struct dentry *)osd_seq);
1211 root = osd_seq->oos_root;
1213 *name = osd_lf_fid2name(fid);
1215 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1216 else if ((*name)[0])
1217 root = osd_sb(osd)->s_root;
1223 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1224 const struct lu_fid *fid, const struct osd_inode_id *id,
1227 struct dentry *root;
1232 root = osd_object_spec_find(info, osd, fid, &name);
1233 if (!IS_ERR(root)) {
1234 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1244 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1245 const struct lu_fid *fid, const struct osd_inode_id *id,
1248 struct dentry *root;
1253 root = osd_object_spec_find(info, osd, fid, &name);
1254 if (!IS_ERR(root)) {
1255 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1265 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1266 const struct lu_fid *fid, struct osd_inode_id *id)
1268 struct dentry *root;
1269 struct dentry *dentry;
1270 struct inode *inode;
1275 if (fid_is_last_id(fid)) {
1276 struct osd_obj_seq *osd_seq;
1278 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1279 if (IS_ERR(osd_seq))
1280 RETURN(PTR_ERR(osd_seq));
1281 root = osd_seq->oos_root;
1284 root = osd_sb(osd)->s_root;
1285 name = osd_lf_fid2name(fid);
1286 if (name == NULL || strlen(name) == 0)
1290 dentry = ll_lookup_one_len(name, root, strlen(name));
1291 if (!IS_ERR(dentry)) {
1292 inode = dentry->d_inode;
1294 if (is_bad_inode(inode)) {
1297 osd_id_gen(id, inode->i_ino,
1298 inode->i_generation);
1302 /* if dentry is accessible after osd_compat_spec_insert it
1303 * will still contain NULL inode, so don't keep it in cache */
1304 d_invalidate(dentry);