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, 2017, 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 /* prerequisite for linux/xattr.h */
40 #include <linux/types.h>
41 /* prerequisite for linux/xattr.h */
43 /* XATTR_{REPLACE,CREATE} */
44 #include <linux/xattr.h>
47 * struct OBD_{ALLOC,FREE}*()
50 #include <obd_support.h>
52 #include "osd_internal.h"
55 static void osd_push_ctxt(const struct osd_device *dev,
56 struct lvfs_run_ctxt *newctxt,
57 struct lvfs_run_ctxt *save)
59 OBD_SET_CTXT_MAGIC(newctxt);
60 newctxt->pwdmnt = dev->od_mnt;
61 newctxt->pwd = dev->od_mnt->mnt_root;
62 newctxt->fs = get_ds();
63 newctxt->umask = current_umask();
66 push_ctxt(save, newctxt);
69 /* utility to make a directory */
70 static struct dentry *
71 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
72 struct dentry *dir, const struct lu_fid *fid,
73 const char *name, __u32 compat, int mode, bool *created)
75 struct osd_thread_info *info = osd_oti_get(env);
76 struct lu_fid *tfid = &info->oti_fid3;
78 struct dentry *dchild;
82 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
83 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
84 dchild = ll_lookup_one_len(name, dir, strlen(name));
88 inode = dchild->d_inode;
90 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
91 int old_mode = inode->i_mode;
96 if (!S_ISDIR(old_mode)) {
97 CERROR("found %s (%lu/%u) is mode %o\n", name,
98 inode->i_ino, inode->i_generation, old_mode);
99 GOTO(out_err, err = -ENOTDIR);
102 if (unlikely(osd->od_dt_dev.dd_rdonly))
105 /* Fixup directory permissions if necessary */
106 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
108 "fixing permissions on %s from %o to %o\n",
109 name, old_mode, mode);
110 inode->i_mode = (mode & S_IALLUGO) |
111 (old_mode & ~S_IALLUGO);
112 mark_inode_dirty(inode);
115 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
116 &info->oti_ost_attrs);
123 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
124 lma->lma_compat != compat)
130 err = vfs_mkdir(dir->d_inode, dchild, mode);
134 inode = dchild->d_inode;
142 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
143 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
154 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
156 struct lr_server_data lsd;
157 struct dentry *dlast;
160 int count = OBJ_SUBDIR_COUNT;
164 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
167 return PTR_ERR(dlast);
168 else if (dlast->d_inode == NULL)
172 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
173 if (rc == sizeof(lsd)) {
174 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
175 "subdir count = %d\n", lsd.lsd_uuid,
176 lsd.lsd_subdir_count);
177 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
178 count = le16_to_cpu(lsd.lsd_subdir_count);
179 } else if (rc != 0) {
180 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
192 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
194 struct lvfs_run_ctxt new;
195 struct lvfs_run_ctxt save;
196 struct dentry *parent;
197 struct osd_mdobj_map *omm;
199 struct osd_thread_info *info = osd_oti_get(env);
200 struct lu_fid *fid = &info->oti_fid3;
204 OBD_ALLOC_PTR(dev->od_mdt_map);
205 if (dev->od_mdt_map == NULL)
208 omm = dev->od_mdt_map;
210 parent = osd_sb(dev)->s_root;
211 osd_push_ctxt(dev, &new, &save);
213 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
214 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
215 LMAC_NOT_IN_OI, 0755, NULL);
217 GOTO(cleanup, rc = PTR_ERR(d));
219 omm->omm_remote_parent = d;
221 GOTO(cleanup, rc = 0);
224 pop_ctxt(&save, &new);
226 if (omm->omm_remote_parent != NULL)
227 dput(omm->omm_remote_parent);
229 dev->od_mdt_map = NULL;
234 static void osd_mdt_fini(struct osd_device *osd)
236 struct osd_mdobj_map *omm = osd->od_mdt_map;
241 if (omm->omm_remote_parent)
242 dput(omm->omm_remote_parent);
245 osd->od_ost_map = NULL;
248 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
249 struct osd_object *obj, struct osd_thandle *oh)
251 struct osd_mdobj_map *omm = osd->od_mdt_map;
252 struct osd_thread_info *oti = osd_oti_get(env);
253 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
254 char *name = oti->oti_name;
255 struct osd_thread_info *info = osd_oti_get(env);
256 struct dentry *dentry;
257 struct dentry *parent;
260 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
263 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
264 * can identify this object quickly */
265 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
266 &oti->oti_ost_attrs);
270 lma->lma_incompat |= LMAI_REMOTE_PARENT;
271 lustre_lma_swab(lma);
272 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
273 sizeof(*lma), XATTR_REPLACE);
277 parent = omm->omm_remote_parent;
278 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
279 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
281 mutex_lock(&parent->d_inode->i_mutex);
282 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
283 obj->oo_inode, NULL);
284 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
285 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
286 else if (unlikely(rc == -EEXIST))
289 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
290 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
291 osd_name(osd), name, rc);
292 mark_inode_dirty(parent->d_inode);
293 mutex_unlock(&parent->d_inode->i_mutex);
297 int osd_delete_from_remote_parent(const struct lu_env *env,
298 struct osd_device *osd,
299 struct osd_object *obj,
300 struct osd_thandle *oh, bool destroy)
302 struct osd_mdobj_map *omm = osd->od_mdt_map;
303 struct osd_thread_info *oti = osd_oti_get(env);
304 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
305 char *name = oti->oti_name;
306 struct dentry *dentry;
307 struct dentry *parent;
308 struct ldiskfs_dir_entry_2 *de;
309 struct buffer_head *bh;
312 parent = omm->omm_remote_parent;
313 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
314 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
316 mutex_lock(&parent->d_inode->i_mutex);
317 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
320 mutex_unlock(&parent->d_inode->i_mutex);
322 if (unlikely(rc == -ENOENT))
325 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
327 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
328 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
329 mark_inode_dirty(parent->d_inode);
330 mutex_unlock(&parent->d_inode->i_mutex);
332 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
333 osd_name(osd), name, rc);
338 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
343 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
344 &oti->oti_ost_attrs);
348 /* Get rid of REMOTE_PARENT flag from incompat */
349 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
350 lustre_lma_swab(lma);
351 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
352 sizeof(*lma), XATTR_REPLACE);
354 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
358 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
359 struct osd_device *osd,
360 const struct lu_fid *fid,
361 struct osd_inode_id *id)
363 struct osd_mdobj_map *omm = osd->od_mdt_map;
364 char *name = oti->oti_name;
365 struct dentry *parent;
366 struct dentry *dentry;
367 struct ldiskfs_dir_entry_2 *de;
368 struct buffer_head *bh;
372 parent = omm->omm_remote_parent;
373 sprintf(name, DFID_NOBRACE, PFID(fid));
374 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
376 mutex_lock(&parent->d_inode->i_mutex);
377 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
384 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
386 inode = osd_iget(oti, osd, id);
396 mutex_unlock(&parent->d_inode->i_mutex);
398 osd_add_oi_cache(oti, osd, id, fid);
403 * directory structure on legacy OST:
405 * O/<seq>/d0-31/<objid>
412 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
414 struct lvfs_run_ctxt new;
415 struct lvfs_run_ctxt save;
418 bool created = false;
421 OBD_ALLOC_PTR(dev->od_ost_map);
422 if (dev->od_ost_map == NULL)
425 /* to get subdir count from last_rcvd */
426 rc = osd_last_rcvd_subdir_count(dev);
428 GOTO(cleanup_alloc, rc);
430 dev->od_ost_map->om_subdir_count = rc;
431 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
432 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
433 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
435 osd_push_ctxt(dev, &new, &save);
436 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
437 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
439 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
442 /* It is quite probably that the device is new formatted. */
443 dev->od_maybe_new = 1;
445 dev->od_ost_map->om_root = d;
447 pop_ctxt(&save, &new);
451 pop_ctxt(&save, &new);
453 OBD_FREE_PTR(dev->od_ost_map);
457 static void osd_seq_free(struct osd_obj_seq *osd_seq)
461 if (osd_seq->oos_dirs) {
462 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
463 if (osd_seq->oos_dirs[j])
464 dput(osd_seq->oos_dirs[j]);
466 OBD_FREE(osd_seq->oos_dirs,
467 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
470 if (osd_seq->oos_root)
471 dput(osd_seq->oos_root);
473 OBD_FREE_PTR(osd_seq);
476 static void osd_ost_fini(struct osd_device *osd)
478 struct osd_obj_seq *osd_seq;
479 struct osd_obj_seq *tmp;
480 struct osd_obj_map *map = osd->od_ost_map;
486 write_lock(&map->om_seq_list_lock);
487 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
489 list_del_init(&osd_seq->oos_seq_list);
490 write_unlock(&map->om_seq_list_lock);
491 osd_seq_free(osd_seq);
492 write_lock(&map->om_seq_list_lock);
494 write_unlock(&map->om_seq_list_lock);
498 osd->od_ost_map = NULL;
502 static int osd_index_backup_dir_init(const struct lu_env *env,
503 struct osd_device *dev)
505 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
506 struct lvfs_run_ctxt new;
507 struct lvfs_run_ctxt save;
508 struct dentry *dentry;
512 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
513 osd_push_ctxt(dev, &new, &save);
514 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
515 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
516 if (IS_ERR(dentry)) {
517 rc = PTR_ERR(dentry);
519 dev->od_index_backup_inode = igrab(dentry->d_inode);
522 pop_ctxt(&save, &new);
527 static void osd_index_backup_dir_fini(struct osd_device *dev)
529 if (dev->od_index_backup_inode) {
530 iput(dev->od_index_backup_inode);
531 dev->od_index_backup_inode = NULL;
535 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
538 bool ost_init = false;
539 bool mdt_init = false;
542 rc = osd_ost_init(env, dev);
547 rc = osd_mdt_init(env, dev);
550 rc = osd_index_backup_dir_init(env, dev);
563 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
565 struct osd_obj_seq *osd_seq;
567 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
568 if (osd_seq->oos_seq == seq)
574 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
576 struct osd_obj_seq *osd_seq;
578 read_lock(&map->om_seq_list_lock);
579 osd_seq = osd_seq_find_locked(map, seq);
580 read_unlock(&map->om_seq_list_lock);
584 void osd_obj_map_fini(struct osd_device *dev)
586 osd_index_backup_dir_fini(dev);
592 * Update the specified OI mapping.
594 * \retval 1, changed nothing
595 * \retval 0, changed successfully
596 * \retval -ve, on error
598 static int osd_obj_update_entry(struct osd_thread_info *info,
599 struct osd_device *osd,
600 struct dentry *dir, const char *name,
601 const struct lu_fid *fid,
602 const struct osd_inode_id *id,
605 struct inode *parent = dir->d_inode;
606 struct dentry *child;
607 struct ldiskfs_dir_entry_2 *de;
608 struct buffer_head *bh;
610 struct dentry *dentry = &info->oti_obj_dentry;
611 struct osd_inode_id *oi_id = &info->oti_id3;
612 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
613 struct lu_fid *oi_fid = &lma->lma_self_fid;
618 LASSERT(th->h_transaction != NULL);
620 child = &info->oti_child_dentry;
621 child->d_parent = dir;
622 child->d_name.hash = 0;
623 child->d_name.name = name;
624 child->d_name.len = strlen(name);
626 ll_vfs_dq_init(parent);
627 mutex_lock(&parent->i_mutex);
628 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
630 GOTO(out, rc = PTR_ERR(bh));
632 if (le32_to_cpu(de->inode) == id->oii_ino)
635 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
636 inode = osd_iget(info, osd, oi_id);
639 if (rc == -ENOENT || rc == -ESTALE)
644 /* The EA inode should NOT be in OI, old OI scrub may added
645 * such OI mapping by wrong, replace it. */
646 if (unlikely(osd_is_ea_inode(inode))) {
651 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
652 if (rc == -ENODATA) {
653 rc = osd_get_idif(info, inode, dentry, oi_fid);
654 if (rc > 0 || rc == -ENODATA) {
664 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
665 * either a crashed object or a uninitialized one. Replace it. */
666 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
667 CERROR("%s: the FID "DFID" is used by two objects: "
668 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
669 oi_id->oii_ino, oi_id->oii_gen,
670 id->oii_ino, id->oii_gen);
671 GOTO(out, rc = -EEXIST);
674 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
675 __u32 idx1 = fid_idif_ost_idx(fid);
676 __u32 idx2 = fid_idif_ost_idx(oi_fid);
677 struct ost_id *ostid = &info->oti_ostid;
678 struct lu_fid *tfid = &info->oti_fid3;
680 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
681 "invalid given FID "DFID", not match the "
682 "device index %u\n", PFID(fid), osd->od_index);
685 if (idx1 == 0 && idx2 == osd->od_index) {
686 fid_to_ostid(fid, ostid);
687 ostid_to_fid(tfid, ostid, idx2);
688 if (lu_fid_eq(tfid, oi_fid)) {
689 CERROR("%s: the FID "DFID" is used by "
690 "two objects(2): %u/%u %u/%u\n",
691 osd_name(osd), PFID(fid),
692 oi_id->oii_ino, oi_id->oii_gen,
693 id->oii_ino, id->oii_gen);
695 GOTO(out, rc = -EEXIST);
697 } else if (idx2 == 0 && idx1 == osd->od_index) {
698 fid_to_ostid(oi_fid, ostid);
699 ostid_to_fid(tfid, ostid, idx1);
700 if (lu_fid_eq(tfid, fid)) {
701 CERROR("%s: the FID "DFID" is used by "
702 "two objects(2): %u/%u %u/%u\n",
703 osd_name(osd), PFID(fid),
704 oi_id->oii_ino, oi_id->oii_gen,
705 id->oii_ino, id->oii_gen);
707 GOTO(out, rc = -EEXIST);
714 /* There may be temporary inconsistency: On one hand, the new
715 * object may be referenced by multiple entries, which is out
716 * of our control unless we traverse the whole /O completely,
717 * which is non-flat order and inefficient, should be avoided;
718 * On the other hand, the old object may become orphan if it
719 * is still valid. Since it was referenced by an invalid entry,
720 * making it as invisible temporary may be not worse. OI scrub
721 * will process it later. */
722 rc = ldiskfs_journal_get_write_access(th, bh);
726 de->inode = cpu_to_le32(id->oii_ino);
727 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
734 mutex_unlock(&parent->i_mutex);
738 static int osd_obj_del_entry(struct osd_thread_info *info,
739 struct osd_device *osd,
740 struct dentry *dird, char *name,
743 struct ldiskfs_dir_entry_2 *de;
744 struct buffer_head *bh;
745 struct dentry *child;
746 struct inode *dir = dird->d_inode;
751 LASSERT(th->h_transaction != NULL);
754 child = &info->oti_child_dentry;
755 child->d_name.hash = 0;
756 child->d_name.name = name;
757 child->d_name.len = strlen(name);
758 child->d_parent = dird;
759 child->d_inode = NULL;
762 mutex_lock(&dir->i_mutex);
763 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
767 rc = ldiskfs_delete_entry(th, dir, de, bh);
770 mutex_unlock(&dir->i_mutex);
775 static int osd_obj_add_entry(struct osd_thread_info *info,
776 struct osd_device *osd,
777 struct dentry *dir, char *name,
778 const struct osd_inode_id *id,
781 struct dentry *child;
787 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
791 LASSERT(th->h_transaction != NULL);
793 inode = info->oti_inode;
794 if (unlikely(inode == NULL)) {
795 struct ldiskfs_inode_info *lii;
799 inode = info->oti_inode = &lii->vfs_inode;
802 inode->i_sb = osd_sb(osd);
803 osd_id_to_inode(inode, id);
804 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
806 child = &info->oti_child_dentry;
807 child->d_name.hash = 0;
808 child->d_name.name = name;
809 child->d_name.len = strlen(name);
810 child->d_parent = dir;
811 child->d_inode = inode;
813 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
816 ll_vfs_dq_init(dir->d_inode);
817 mutex_lock(&dir->d_inode->i_mutex);
818 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
819 mutex_unlock(&dir->d_inode->i_mutex);
825 * Use %llu for legacy OST sequences, but use %llx for new
826 * sequences names, so that the O/{seq}/dN/{oid} more closely
827 * follows the DFID/PFID format. This makes it easier to map from
828 * debug messages to objects in the future, and the legacy space
829 * of FID_SEQ_OST_MDT0 will be unused in the future.
831 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
833 snprintf(seq_name, name_size,
834 (fid_seq_is_rsvd(seq) ||
835 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
836 fid_seq_is_idif(seq) ? 0 : seq);
839 static inline void osd_oid_name(char *name, size_t name_size,
840 const struct lu_fid *fid, u64 id)
842 snprintf(name, name_size,
843 (fid_seq_is_rsvd(fid_seq(fid)) ||
844 fid_seq_is_mdt0(fid_seq(fid)) ||
845 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
848 /* external locking is required */
849 static int osd_seq_load_locked(struct osd_thread_info *info,
850 struct osd_device *osd,
851 struct osd_obj_seq *osd_seq)
853 struct osd_obj_map *map = osd->od_ost_map;
854 struct dentry *seq_dir;
860 if (osd_seq->oos_root != NULL)
864 LASSERT(map->om_root);
866 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
868 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
869 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
871 GOTO(out_err, rc = PTR_ERR(seq_dir));
872 else if (seq_dir->d_inode == NULL)
873 GOTO(out_put, rc = -EFAULT);
875 osd_seq->oos_root = seq_dir;
877 LASSERT(osd_seq->oos_dirs == NULL);
878 OBD_ALLOC(osd_seq->oos_dirs,
879 sizeof(seq_dir) * osd_seq->oos_subdir_count);
880 if (osd_seq->oos_dirs == NULL)
881 GOTO(out_put, rc = -ENOMEM);
883 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
886 snprintf(dir_name, sizeof(dir_name), "d%u", i);
887 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
888 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
891 GOTO(out_free, rc = PTR_ERR(dir));
892 } else if (dir->d_inode == NULL) {
894 GOTO(out_free, rc = -EFAULT);
897 osd_seq->oos_dirs[i] = dir;
902 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
903 if (osd_seq->oos_dirs[i] != NULL)
904 dput(osd_seq->oos_dirs[i]);
906 OBD_FREE(osd_seq->oos_dirs,
907 sizeof(seq_dir) * osd_seq->oos_subdir_count);
910 osd_seq->oos_root = NULL;
916 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
917 struct osd_device *osd, u64 seq)
919 struct osd_obj_map *map;
920 struct osd_obj_seq *osd_seq;
924 map = osd->od_ost_map;
926 LASSERT(map->om_root);
928 osd_seq = osd_seq_find(map, seq);
929 if (likely(osd_seq != NULL))
932 /* Serializing init process */
933 mutex_lock(&map->om_dir_init_mutex);
935 /* Check whether the seq has been added */
936 read_lock(&map->om_seq_list_lock);
937 osd_seq = osd_seq_find_locked(map, seq);
938 if (osd_seq != NULL) {
939 read_unlock(&map->om_seq_list_lock);
940 GOTO(cleanup, rc = 0);
942 read_unlock(&map->om_seq_list_lock);
944 OBD_ALLOC_PTR(osd_seq);
946 GOTO(cleanup, rc = -ENOMEM);
948 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
949 osd_seq->oos_seq = seq;
950 /* Init subdir count to be 32, but each seq can have
951 * different subdir count */
952 osd_seq->oos_subdir_count = map->om_subdir_count;
953 rc = osd_seq_load_locked(info, osd, osd_seq);
957 write_lock(&map->om_seq_list_lock);
958 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
959 write_unlock(&map->om_seq_list_lock);
962 mutex_unlock(&map->om_dir_init_mutex);
965 OBD_FREE_PTR(osd_seq);
972 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
973 const struct lu_fid *fid, struct osd_inode_id *id)
975 struct osd_obj_map *map;
976 struct osd_obj_seq *osd_seq;
977 struct dentry *d_seq;
978 struct dentry *child;
979 struct ost_id *ostid = &info->oti_ostid;
982 struct ldiskfs_dir_entry_2 *de;
983 struct buffer_head *bh;
988 /* on the very first lookup we find and open directories */
990 map = dev->od_ost_map;
992 LASSERT(map->om_root);
994 fid_to_ostid(fid, ostid);
995 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
997 RETURN(PTR_ERR(osd_seq));
999 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1000 d_seq = osd_seq->oos_dirs[dirn];
1003 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1005 child = &info->oti_child_dentry;
1006 child->d_parent = d_seq;
1007 child->d_name.hash = 0;
1008 child->d_name.name = name;
1009 /* XXX: we can use rc from sprintf() instead of strlen() */
1010 child->d_name.len = strlen(name);
1012 dir = d_seq->d_inode;
1013 mutex_lock(&dir->i_mutex);
1014 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1015 mutex_unlock(&dir->i_mutex);
1018 RETURN(PTR_ERR(bh));
1020 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1023 inode = osd_iget(info, dev, id);
1024 if (IS_ERR(inode)) {
1025 int rc = PTR_ERR(inode);
1027 RETURN(rc == -ENOENT ? -ESTALE : rc);
1034 int osd_obj_map_insert(struct osd_thread_info *info,
1035 struct osd_device *osd,
1036 const struct lu_fid *fid,
1037 const struct osd_inode_id *id,
1040 struct osd_obj_map *map;
1041 struct osd_obj_seq *osd_seq;
1043 struct ost_id *ostid = &info->oti_ostid;
1049 map = osd->od_ost_map;
1052 /* map fid to seq:objid */
1053 fid_to_ostid(fid, ostid);
1055 oid = ostid_id(ostid);
1056 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1057 if (IS_ERR(osd_seq))
1058 RETURN(PTR_ERR(osd_seq));
1060 dirn = oid & (osd_seq->oos_subdir_count - 1);
1061 d = osd_seq->oos_dirs[dirn];
1064 osd_oid_name(name, sizeof(name), fid, oid);
1067 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1068 if (rc == -EEXIST) {
1069 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1070 if (unlikely(rc == -ENOENT))
1073 if (unlikely(rc == 1))
1080 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1081 const struct lu_fid *fid, handle_t *th)
1083 struct osd_obj_map *map;
1084 struct osd_obj_seq *osd_seq;
1086 struct ost_id *ostid = &info->oti_ostid;
1091 map = osd->od_ost_map;
1094 /* map fid to seq:objid */
1095 fid_to_ostid(fid, ostid);
1097 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1098 if (IS_ERR(osd_seq))
1099 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1101 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1102 d = osd_seq->oos_dirs[dirn];
1105 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1106 rc = osd_obj_del_entry(info, osd, d, name, th);
1111 int osd_obj_map_update(struct osd_thread_info *info,
1112 struct osd_device *osd,
1113 const struct lu_fid *fid,
1114 const struct osd_inode_id *id,
1117 struct osd_obj_seq *osd_seq;
1119 struct ost_id *ostid = &info->oti_ostid;
1124 fid_to_ostid(fid, ostid);
1125 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1126 if (IS_ERR(osd_seq))
1127 RETURN(PTR_ERR(osd_seq));
1129 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1130 d = osd_seq->oos_dirs[dirn];
1133 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1134 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1139 int osd_obj_map_recover(struct osd_thread_info *info,
1140 struct osd_device *osd,
1141 struct inode *src_parent,
1142 struct dentry *src_child,
1143 const struct lu_fid *fid)
1145 struct osd_obj_seq *osd_seq;
1146 struct dentry *tgt_parent;
1147 struct dentry *tgt_child = &info->oti_child_dentry;
1149 struct inode *inode = src_child->d_inode;
1150 struct ost_id *ostid = &info->oti_ostid;
1152 struct ldiskfs_dir_entry_2 *de;
1153 struct buffer_head *bh;
1159 if (fid_is_last_id(fid)) {
1160 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1161 if (IS_ERR(osd_seq))
1162 RETURN(PTR_ERR(osd_seq));
1164 tgt_parent = osd_seq->oos_root;
1165 tgt_child->d_name.name = "LAST_ID";
1166 tgt_child->d_name.len = strlen("LAST_ID");
1168 fid_to_ostid(fid, ostid);
1169 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1170 if (IS_ERR(osd_seq))
1171 RETURN(PTR_ERR(osd_seq));
1173 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1174 tgt_parent = osd_seq->oos_dirs[dirn];
1175 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1176 tgt_child->d_name.name = name;
1177 tgt_child->d_name.len = strlen(name);
1179 LASSERT(tgt_parent != NULL);
1181 dir = tgt_parent->d_inode;
1182 tgt_child->d_name.hash = 0;
1183 tgt_child->d_parent = tgt_parent;
1184 tgt_child->d_inode = inode;
1186 /* The non-initialized src_child may be destroyed. */
1187 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1188 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1189 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1190 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1192 RETURN(PTR_ERR(jh));
1194 ll_vfs_dq_init(src_parent);
1195 ll_vfs_dq_init(dir);
1197 mutex_lock(&src_parent->i_mutex);
1198 mutex_lock(&dir->i_mutex);
1199 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1201 /* XXX: If some other object occupied the same slot. And If such
1202 * inode is zero-sized and with SUID+SGID, then means it is
1203 * a new created one. Maybe we can remove it and insert the
1204 * original one back to the /O/<seq>/d<x>. But there are
1205 * something to be considered:
1207 * 1) The OST-object under /lost+found has crashed LMA.
1208 * So it should not conflict with the current one.
1210 * 2) There are race conditions that: someone may just want
1211 * to modify the current one. Even if the OI scrub takes
1212 * the object lock when remove the current one, it still
1213 * cause the modification to be lost becasue the target
1214 * has been removed when the RPC service thread waiting
1217 * So keep it there before we have suitable solution. */
1219 mutex_unlock(&dir->i_mutex);
1220 mutex_unlock(&src_parent->i_mutex);
1221 ldiskfs_journal_stop(jh);
1224 /* If the src object has never been modified, then remove it. */
1225 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1226 inode->i_mode & S_ISGID) {
1227 rc = ll_vfs_unlink(src_parent, src_child);
1228 if (unlikely(rc == -ENOENT))
1234 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1236 if (unlikely(IS_ERR(bh)))
1237 GOTO(unlock, rc = PTR_ERR(bh));
1239 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1244 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1249 mutex_unlock(&dir->i_mutex);
1250 mutex_unlock(&src_parent->i_mutex);
1251 ldiskfs_journal_stop(jh);
1255 static struct dentry *
1256 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1257 const struct lu_fid *fid, char **name)
1259 struct dentry *root = ERR_PTR(-ENOENT);
1261 if (fid_is_last_id(fid)) {
1262 struct osd_obj_seq *osd_seq;
1264 /* on creation of LAST_ID we create O/<seq> hierarchy */
1265 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1266 if (IS_ERR(osd_seq))
1267 RETURN((struct dentry *)osd_seq);
1270 root = osd_seq->oos_root;
1272 *name = osd_lf_fid2name(fid);
1274 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1275 else if ((*name)[0])
1276 root = osd_sb(osd)->s_root;
1282 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1283 const struct lu_fid *fid, const struct osd_inode_id *id,
1286 struct dentry *root;
1291 root = osd_object_spec_find(info, osd, fid, &name);
1292 if (!IS_ERR(root)) {
1293 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1303 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1304 const struct lu_fid *fid, const struct osd_inode_id *id,
1307 struct dentry *root;
1312 root = osd_object_spec_find(info, osd, fid, &name);
1313 if (!IS_ERR(root)) {
1314 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1324 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1325 const struct lu_fid *fid, struct osd_inode_id *id)
1327 struct dentry *root;
1328 struct dentry *dentry;
1329 struct inode *inode;
1334 if (fid_is_last_id(fid)) {
1335 struct osd_obj_seq *osd_seq;
1337 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1338 if (IS_ERR(osd_seq))
1339 RETURN(PTR_ERR(osd_seq));
1340 root = osd_seq->oos_root;
1343 root = osd_sb(osd)->s_root;
1344 name = osd_lf_fid2name(fid);
1345 if (name == NULL || strlen(name) == 0)
1349 dentry = ll_lookup_one_len(name, root, strlen(name));
1350 if (!IS_ERR(dentry)) {
1351 inode = dentry->d_inode;
1353 if (is_bad_inode(inode)) {
1356 osd_id_gen(id, inode->i_ino,
1357 inode->i_generation);
1361 /* if dentry is accessible after osd_compat_spec_insert it
1362 * will still contain NULL inode, so don't keep it in cache */
1363 d_invalidate(dentry);