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;
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 inode = dchild->d_inode;
91 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
92 int old_mode = inode->i_mode;
97 if (!S_ISDIR(old_mode)) {
98 CERROR("found %s (%lu/%u) is mode %o\n", name,
99 inode->i_ino, inode->i_generation, old_mode);
100 GOTO(out_err, err = -ENOTDIR);
103 if (unlikely(osd->od_dt_dev.dd_rdonly))
106 /* Fixup directory permissions if necessary */
107 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
109 "fixing permissions on %s from %o to %o\n",
110 name, old_mode, mode);
111 inode->i_mode = (mode & S_IALLUGO) |
112 (old_mode & ~S_IALLUGO);
113 mark_inode_dirty(inode);
116 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
117 &info->oti_ost_attrs);
124 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
125 lma->lma_compat != compat)
131 err = vfs_mkdir(dir->d_inode, dchild, mode);
135 inode = dchild->d_inode;
143 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
144 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
155 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
157 struct lr_server_data lsd;
158 struct dentry *dlast;
161 int count = OBJ_SUBDIR_COUNT;
165 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
168 return PTR_ERR(dlast);
169 else if (dlast->d_inode == NULL)
173 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
174 if (rc == sizeof(lsd)) {
176 "read last_rcvd header, uuid = %s, subdir count = %d\n",
177 lsd.lsd_uuid, lsd.lsd_subdir_count);
178 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
179 count = le16_to_cpu(lsd.lsd_subdir_count);
180 } else if (rc != 0) {
181 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
193 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
195 struct lvfs_run_ctxt new;
196 struct lvfs_run_ctxt save;
197 struct dentry *parent;
198 struct osd_mdobj_map *omm;
200 struct osd_thread_info *info = osd_oti_get(env);
201 struct lu_fid *fid = &info->oti_fid3;
206 OBD_ALLOC_PTR(dev->od_mdt_map);
207 if (dev->od_mdt_map == NULL)
210 omm = dev->od_mdt_map;
212 parent = osd_sb(dev)->s_root;
213 osd_push_ctxt(dev, &new, &save);
215 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
216 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
217 LMAC_NOT_IN_OI, 0755, NULL);
219 GOTO(cleanup, rc = PTR_ERR(d));
221 omm->omm_remote_parent = d;
223 GOTO(cleanup, rc = 0);
226 pop_ctxt(&save, &new);
228 if (omm->omm_remote_parent != NULL)
229 dput(omm->omm_remote_parent);
231 dev->od_mdt_map = NULL;
236 static void osd_mdt_fini(struct osd_device *osd)
238 struct osd_mdobj_map *omm = osd->od_mdt_map;
243 if (omm->omm_remote_parent)
244 dput(omm->omm_remote_parent);
247 osd->od_ost_map = NULL;
250 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
251 struct osd_object *obj, struct osd_thandle *oh)
253 struct osd_mdobj_map *omm = osd->od_mdt_map;
254 struct osd_thread_info *oti = osd_oti_get(env);
255 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
256 char *name = oti->oti_name;
257 struct osd_thread_info *info = osd_oti_get(env);
258 struct dentry *dentry;
259 struct dentry *parent;
262 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
266 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
267 * can identify this object quickly
269 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
270 &oti->oti_ost_attrs);
274 lma->lma_incompat |= LMAI_REMOTE_PARENT;
275 lustre_lma_swab(lma);
276 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
277 sizeof(*lma), XATTR_REPLACE);
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 inode_lock(parent->d_inode);
286 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
287 obj->oo_inode, NULL);
288 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
289 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
290 else if (unlikely(rc == -EEXIST))
293 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
294 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
295 osd_name(osd), name, rc);
296 mark_inode_dirty(parent->d_inode);
297 inode_unlock(parent->d_inode);
301 int osd_delete_from_remote_parent(const struct lu_env *env,
302 struct osd_device *osd,
303 struct osd_object *obj,
304 struct osd_thandle *oh, bool destroy)
306 struct osd_mdobj_map *omm = osd->od_mdt_map;
307 struct osd_thread_info *oti = osd_oti_get(env);
308 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
309 char *name = oti->oti_name;
310 struct dentry *dentry;
311 struct dentry *parent;
312 struct ldiskfs_dir_entry_2 *de;
313 struct buffer_head *bh;
316 parent = omm->omm_remote_parent;
317 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
318 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
320 inode_lock(parent->d_inode);
321 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
324 inode_unlock(parent->d_inode);
326 if (unlikely(rc == -ENOENT))
329 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
331 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
332 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
333 mark_inode_dirty(parent->d_inode);
334 inode_unlock(parent->d_inode);
336 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
337 osd_name(osd), name, rc);
342 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
347 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
348 &oti->oti_ost_attrs);
352 /* Get rid of REMOTE_PARENT flag from incompat */
353 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
354 lustre_lma_swab(lma);
355 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
356 sizeof(*lma), XATTR_REPLACE);
358 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
362 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
363 struct osd_device *osd,
364 const struct lu_fid *fid,
365 struct osd_inode_id *id)
367 struct osd_mdobj_map *omm = osd->od_mdt_map;
368 char *name = oti->oti_name;
369 struct dentry *parent;
370 struct dentry *dentry;
371 struct ldiskfs_dir_entry_2 *de;
372 struct buffer_head *bh;
377 if (unlikely(osd->od_is_ost))
380 parent = omm->omm_remote_parent;
381 sprintf(name, DFID_NOBRACE, PFID(fid));
382 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
384 inode_lock(parent->d_inode);
385 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
392 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
394 inode = osd_iget(oti, osd, id);
404 inode_unlock(parent->d_inode);
406 osd_add_oi_cache(oti, osd, id, fid);
411 * directory structure on legacy OST:
413 * O/<seq>/d0-31/<objid>
420 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
422 struct lvfs_run_ctxt new;
423 struct lvfs_run_ctxt save;
426 bool created = false;
430 OBD_ALLOC_PTR(dev->od_ost_map);
431 if (dev->od_ost_map == NULL)
434 /* to get subdir count from last_rcvd */
435 rc = osd_last_rcvd_subdir_count(dev);
437 GOTO(cleanup_alloc, rc);
439 dev->od_ost_map->om_subdir_count = rc;
440 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
441 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
442 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
444 osd_push_ctxt(dev, &new, &save);
445 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
446 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
448 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
451 /* It is quite probably that the device is new formatted. */
452 dev->od_maybe_new = 1;
454 dev->od_ost_map->om_root = d;
456 pop_ctxt(&save, &new);
460 pop_ctxt(&save, &new);
462 OBD_FREE_PTR(dev->od_ost_map);
466 static void osd_seq_free(struct osd_obj_seq *osd_seq)
470 if (osd_seq->oos_dirs) {
471 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
472 if (osd_seq->oos_dirs[j])
473 dput(osd_seq->oos_dirs[j]);
475 OBD_FREE(osd_seq->oos_dirs,
476 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
479 if (osd_seq->oos_root)
480 dput(osd_seq->oos_root);
482 OBD_FREE_PTR(osd_seq);
485 static void osd_ost_fini(struct osd_device *osd)
487 struct osd_obj_seq *osd_seq;
488 struct osd_obj_seq *tmp;
489 struct osd_obj_map *map = osd->od_ost_map;
496 write_lock(&map->om_seq_list_lock);
497 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
499 list_del_init(&osd_seq->oos_seq_list);
500 write_unlock(&map->om_seq_list_lock);
501 osd_seq_free(osd_seq);
502 write_lock(&map->om_seq_list_lock);
504 write_unlock(&map->om_seq_list_lock);
508 osd->od_ost_map = NULL;
512 static int osd_index_backup_dir_init(const struct lu_env *env,
513 struct osd_device *dev)
515 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
516 struct lvfs_run_ctxt new;
517 struct lvfs_run_ctxt save;
518 struct dentry *dentry;
523 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
524 osd_push_ctxt(dev, &new, &save);
525 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
526 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
527 if (IS_ERR(dentry)) {
528 rc = PTR_ERR(dentry);
530 dev->od_index_backup_inode = igrab(dentry->d_inode);
533 pop_ctxt(&save, &new);
538 static void osd_index_backup_dir_fini(struct osd_device *dev)
540 if (dev->od_index_backup_inode) {
541 iput(dev->od_index_backup_inode);
542 dev->od_index_backup_inode = NULL;
546 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
549 bool mdt_init = false;
553 rc = osd_ost_init(env, dev);
557 if (!dev->od_is_ost) {
558 rc = osd_mdt_init(env, dev);
567 rc = osd_index_backup_dir_init(env, dev);
577 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
579 struct osd_obj_seq *osd_seq;
581 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
582 if (osd_seq->oos_seq == seq)
588 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
590 struct osd_obj_seq *osd_seq;
592 read_lock(&map->om_seq_list_lock);
593 osd_seq = osd_seq_find_locked(map, seq);
594 read_unlock(&map->om_seq_list_lock);
598 void osd_obj_map_fini(struct osd_device *dev)
600 osd_index_backup_dir_fini(dev);
606 * Update the specified OI mapping.
608 * \retval 1, changed nothing
609 * \retval 0, changed successfully
610 * \retval -ve, on error
612 static int osd_obj_update_entry(struct osd_thread_info *info,
613 struct osd_device *osd,
614 struct dentry *dir, const char *name,
615 const struct lu_fid *fid,
616 const struct osd_inode_id *id,
619 struct inode *parent = dir->d_inode;
620 struct dentry *child;
621 struct ldiskfs_dir_entry_2 *de;
622 struct buffer_head *bh;
624 struct dentry *dentry = &info->oti_obj_dentry;
625 struct osd_inode_id *oi_id = &info->oti_id3;
626 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
627 struct lu_fid *oi_fid = &lma->lma_self_fid;
633 LASSERT(th->h_transaction != NULL);
635 child = &info->oti_child_dentry;
636 child->d_parent = dir;
637 child->d_name.hash = 0;
638 child->d_name.name = name;
639 child->d_name.len = strlen(name);
641 ll_vfs_dq_init(parent);
643 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
645 GOTO(out, rc = PTR_ERR(bh));
647 if (le32_to_cpu(de->inode) == id->oii_ino)
650 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
651 inode = osd_iget(info, osd, oi_id);
654 if (rc == -ENOENT || rc == -ESTALE)
660 * The EA inode should NOT be in OI, old OI scrub may added
661 * such OI mapping by wrong, replace it.
663 if (unlikely(osd_is_ea_inode(inode))) {
668 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
669 if (rc == -ENODATA) {
670 rc = osd_get_idif(info, inode, dentry, oi_fid);
671 if (rc > 0 || rc == -ENODATA) {
682 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
683 * either a crashed object or a uninitialized one. Replace it.
685 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
686 CERROR("%s: the FID "DFID" is used by two objects: "
687 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
688 oi_id->oii_ino, oi_id->oii_gen,
689 id->oii_ino, id->oii_gen);
690 GOTO(out, rc = -EEXIST);
693 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
694 __u32 idx1 = fid_idif_ost_idx(fid);
695 __u32 idx2 = fid_idif_ost_idx(oi_fid);
696 struct ost_id *ostid = &info->oti_ostid;
697 struct lu_fid *tfid = &info->oti_fid3;
699 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
700 "invalid given FID "DFID", not match the "
701 "device index %u\n", PFID(fid), osd->od_index);
704 if (idx1 == 0 && idx2 == osd->od_index) {
705 fid_to_ostid(fid, ostid);
706 ostid_to_fid(tfid, ostid, idx2);
707 if (lu_fid_eq(tfid, oi_fid)) {
708 CERROR("%s: the FID "DFID" is used by "
709 "two objects(2): %u/%u %u/%u\n",
710 osd_name(osd), PFID(fid),
711 oi_id->oii_ino, oi_id->oii_gen,
712 id->oii_ino, id->oii_gen);
714 GOTO(out, rc = -EEXIST);
716 } else if (idx2 == 0 && idx1 == osd->od_index) {
717 fid_to_ostid(oi_fid, ostid);
718 ostid_to_fid(tfid, ostid, idx1);
719 if (lu_fid_eq(tfid, fid)) {
720 CERROR("%s: the FID "DFID" is used by "
721 "two objects(2): %u/%u %u/%u\n",
722 osd_name(osd), PFID(fid),
723 oi_id->oii_ino, oi_id->oii_gen,
724 id->oii_ino, id->oii_gen);
726 GOTO(out, rc = -EEXIST);
734 * There may be temporary inconsistency: On one hand, the new
735 * object may be referenced by multiple entries, which is out
736 * of our control unless we traverse the whole /O completely,
737 * which is non-flat order and inefficient, should be avoided;
738 * On the other hand, the old object may become orphan if it
739 * is still valid. Since it was referenced by an invalid entry,
740 * making it as invisible temporary may be not worse. OI scrub
741 * will process it later.
743 rc = ldiskfs_journal_get_write_access(th, bh);
747 de->inode = cpu_to_le32(id->oii_ino);
748 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
755 inode_unlock(parent);
759 static int osd_obj_del_entry(struct osd_thread_info *info,
760 struct osd_device *osd,
761 struct dentry *dird, char *name,
764 struct ldiskfs_dir_entry_2 *de;
765 struct buffer_head *bh;
766 struct dentry *child;
767 struct inode *dir = dird->d_inode;
773 LASSERT(th->h_transaction != NULL);
775 child = &info->oti_child_dentry;
776 child->d_name.hash = 0;
777 child->d_name.name = name;
778 child->d_name.len = strlen(name);
779 child->d_parent = dird;
780 child->d_inode = NULL;
784 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
788 rc = ldiskfs_delete_entry(th, dir, de, bh);
796 static int osd_obj_add_entry(struct osd_thread_info *info,
797 struct osd_device *osd,
798 struct dentry *dir, char *name,
799 const struct osd_inode_id *id,
802 struct dentry *child;
808 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
812 LASSERT(th->h_transaction != NULL);
814 inode = info->oti_inode;
815 if (unlikely(inode == NULL)) {
816 struct ldiskfs_inode_info *lii;
821 inode = info->oti_inode = &lii->vfs_inode;
824 inode->i_sb = osd_sb(osd);
825 osd_id_to_inode(inode, id);
826 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
828 child = &info->oti_child_dentry;
829 child->d_name.hash = 0;
830 child->d_name.name = name;
831 child->d_name.len = strlen(name);
832 child->d_parent = dir;
833 child->d_inode = inode;
835 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
838 ll_vfs_dq_init(dir->d_inode);
839 inode_lock(dir->d_inode);
840 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
841 inode_unlock(dir->d_inode);
847 * Use %llu for legacy OST sequences, but use %llx for new
848 * sequences names, so that the O/{seq}/dN/{oid} more closely
849 * follows the DFID/PFID format. This makes it easier to map from
850 * debug messages to objects in the future, and the legacy space
851 * of FID_SEQ_OST_MDT0 will be unused in the future.
853 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
855 snprintf(seq_name, name_size,
856 (fid_seq_is_rsvd(seq) ||
857 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
858 fid_seq_is_idif(seq) ? 0 : seq);
861 static inline void osd_oid_name(char *name, size_t name_size,
862 const struct lu_fid *fid, u64 id)
864 snprintf(name, name_size,
865 (fid_seq_is_rsvd(fid_seq(fid)) ||
866 fid_seq_is_mdt0(fid_seq(fid)) ||
867 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
870 /* external locking is required */
871 static int osd_seq_load_locked(struct osd_thread_info *info,
872 struct osd_device *osd,
873 struct osd_obj_seq *osd_seq)
875 struct osd_obj_map *map = osd->od_ost_map;
876 struct dentry *seq_dir;
883 if (osd_seq->oos_root != NULL)
887 LASSERT(map->om_root);
889 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
891 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
892 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
894 GOTO(out_err, rc = PTR_ERR(seq_dir));
895 else if (seq_dir->d_inode == NULL)
896 GOTO(out_put, rc = -EFAULT);
898 osd_seq->oos_root = seq_dir;
900 LASSERT(osd_seq->oos_dirs == NULL);
901 OBD_ALLOC(osd_seq->oos_dirs,
902 sizeof(seq_dir) * osd_seq->oos_subdir_count);
903 if (osd_seq->oos_dirs == NULL)
904 GOTO(out_put, rc = -ENOMEM);
906 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
909 snprintf(dir_name, sizeof(dir_name), "d%u", i);
910 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
911 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
914 GOTO(out_free, rc = PTR_ERR(dir));
915 } else if (dir->d_inode == NULL) {
917 GOTO(out_free, rc = -EFAULT);
920 osd_seq->oos_dirs[i] = dir;
925 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
926 if (osd_seq->oos_dirs[i] != NULL)
927 dput(osd_seq->oos_dirs[i]);
929 OBD_FREE(osd_seq->oos_dirs,
930 sizeof(seq_dir) * osd_seq->oos_subdir_count);
933 osd_seq->oos_root = NULL;
939 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
940 struct osd_device *osd, u64 seq)
942 struct osd_obj_map *map;
943 struct osd_obj_seq *osd_seq;
948 map = osd->od_ost_map;
950 LASSERT(map->om_root);
952 osd_seq = osd_seq_find(map, seq);
953 if (likely(osd_seq != NULL))
956 /* Serializing init process */
957 mutex_lock(&map->om_dir_init_mutex);
959 /* Check whether the seq has been added */
960 read_lock(&map->om_seq_list_lock);
961 osd_seq = osd_seq_find_locked(map, seq);
962 if (osd_seq != NULL) {
963 read_unlock(&map->om_seq_list_lock);
964 GOTO(cleanup, rc = 0);
966 read_unlock(&map->om_seq_list_lock);
968 OBD_ALLOC_PTR(osd_seq);
970 GOTO(cleanup, rc = -ENOMEM);
972 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
973 osd_seq->oos_seq = seq;
975 * Init subdir count to be 32, but each seq can have
976 * different subdir count
978 osd_seq->oos_subdir_count = map->om_subdir_count;
979 rc = osd_seq_load_locked(info, osd, osd_seq);
983 write_lock(&map->om_seq_list_lock);
984 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
985 write_unlock(&map->om_seq_list_lock);
988 mutex_unlock(&map->om_dir_init_mutex);
991 OBD_FREE_PTR(osd_seq);
998 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
999 const struct lu_fid *fid, struct osd_inode_id *id)
1001 struct osd_obj_map *map;
1002 struct osd_obj_seq *osd_seq;
1003 struct dentry *d_seq;
1004 struct dentry *child;
1005 struct ost_id *ostid = &info->oti_ostid;
1008 struct ldiskfs_dir_entry_2 *de;
1009 struct buffer_head *bh;
1011 struct inode *inode;
1015 /* on the very first lookup we find and open directories */
1016 map = dev->od_ost_map;
1018 LASSERT(map->om_root);
1020 fid_to_ostid(fid, ostid);
1021 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1022 if (IS_ERR(osd_seq))
1023 RETURN(PTR_ERR(osd_seq));
1025 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1026 d_seq = osd_seq->oos_dirs[dirn];
1029 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1031 child = &info->oti_child_dentry;
1032 child->d_parent = d_seq;
1033 child->d_name.hash = 0;
1034 child->d_name.name = name;
1035 /* XXX: we can use rc from sprintf() instead of strlen() */
1036 child->d_name.len = strlen(name);
1038 dir = d_seq->d_inode;
1040 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1044 RETURN(PTR_ERR(bh));
1046 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1049 inode = osd_iget(info, dev, id);
1050 if (IS_ERR(inode)) {
1051 int rc = PTR_ERR(inode);
1053 RETURN(rc == -ENOENT ? -ESTALE : rc);
1060 int osd_obj_map_insert(struct osd_thread_info *info,
1061 struct osd_device *osd,
1062 const struct lu_fid *fid,
1063 const struct osd_inode_id *id,
1066 struct osd_obj_map *map;
1067 struct osd_obj_seq *osd_seq;
1069 struct ost_id *ostid = &info->oti_ostid;
1076 map = osd->od_ost_map;
1079 /* map fid to seq:objid */
1080 fid_to_ostid(fid, ostid);
1082 oid = ostid_id(ostid);
1083 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1084 if (IS_ERR(osd_seq))
1085 RETURN(PTR_ERR(osd_seq));
1087 dirn = oid & (osd_seq->oos_subdir_count - 1);
1088 d = osd_seq->oos_dirs[dirn];
1091 osd_oid_name(name, sizeof(name), fid, oid);
1094 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1095 if (rc == -EEXIST) {
1096 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1097 if (unlikely(rc == -ENOENT))
1100 if (unlikely(rc == 1))
1107 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1108 const struct lu_fid *fid, handle_t *th)
1110 struct osd_obj_map *map;
1111 struct osd_obj_seq *osd_seq;
1113 struct ost_id *ostid = &info->oti_ostid;
1119 map = osd->od_ost_map;
1122 /* map fid to seq:objid */
1123 fid_to_ostid(fid, ostid);
1125 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1126 if (IS_ERR(osd_seq))
1127 GOTO(cleanup, rc = 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_del_entry(info, osd, d, name, th);
1139 int osd_obj_map_update(struct osd_thread_info *info,
1140 struct osd_device *osd,
1141 const struct lu_fid *fid,
1142 const struct osd_inode_id *id,
1145 struct osd_obj_seq *osd_seq;
1147 struct ost_id *ostid = &info->oti_ostid;
1153 fid_to_ostid(fid, ostid);
1154 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1155 if (IS_ERR(osd_seq))
1156 RETURN(PTR_ERR(osd_seq));
1158 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1159 d = osd_seq->oos_dirs[dirn];
1162 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1163 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1168 int osd_obj_map_recover(struct osd_thread_info *info,
1169 struct osd_device *osd,
1170 struct inode *src_parent,
1171 struct dentry *src_child,
1172 const struct lu_fid *fid)
1174 struct osd_obj_seq *osd_seq;
1175 struct dentry *tgt_parent;
1176 struct dentry *tgt_child = &info->oti_child_dentry;
1178 struct inode *inode = src_child->d_inode;
1179 struct ost_id *ostid = &info->oti_ostid;
1181 struct ldiskfs_dir_entry_2 *de;
1182 struct buffer_head *bh;
1189 if (fid_is_last_id(fid)) {
1190 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1191 if (IS_ERR(osd_seq))
1192 RETURN(PTR_ERR(osd_seq));
1194 tgt_parent = osd_seq->oos_root;
1195 tgt_child->d_name.name = "LAST_ID";
1196 tgt_child->d_name.len = strlen("LAST_ID");
1198 fid_to_ostid(fid, ostid);
1199 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1200 if (IS_ERR(osd_seq))
1201 RETURN(PTR_ERR(osd_seq));
1203 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1204 tgt_parent = osd_seq->oos_dirs[dirn];
1205 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1206 tgt_child->d_name.name = name;
1207 tgt_child->d_name.len = strlen(name);
1209 LASSERT(tgt_parent != NULL);
1211 dir = tgt_parent->d_inode;
1212 tgt_child->d_name.hash = 0;
1213 tgt_child->d_parent = tgt_parent;
1214 tgt_child->d_inode = inode;
1216 /* The non-initialized src_child may be destroyed. */
1217 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1218 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1219 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1220 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1222 RETURN(PTR_ERR(jh));
1224 ll_vfs_dq_init(src_parent);
1225 ll_vfs_dq_init(dir);
1227 inode_lock(src_parent);
1229 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1232 * XXX: If some other object occupied the same slot. And If such
1233 * inode is zero-sized and with SUID+SGID, then means it is
1234 * a new created one. Maybe we can remove it and insert the
1235 * original one back to the /O/<seq>/d<x>. But there are
1236 * something to be considered:
1238 * 1) The OST-object under /lost+found has crashed LMA.
1239 * So it should not conflict with the current one.
1241 * 2) There are race conditions that: someone may just want
1242 * to modify the current one. Even if the OI scrub takes
1243 * the object lock when remove the current one, it still
1244 * cause the modification to be lost becasue the target
1245 * has been removed when the RPC service thread waiting
1248 * So keep it there before we have suitable solution.
1252 inode_unlock(src_parent);
1253 ldiskfs_journal_stop(jh);
1256 /* If the src object has never been modified, then remove it. */
1257 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1258 inode->i_mode & S_ISGID) {
1259 rc = ll_vfs_unlink(src_parent, src_child);
1260 if (unlikely(rc == -ENOENT))
1266 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1268 if (unlikely(IS_ERR(bh)))
1269 GOTO(unlock, rc = PTR_ERR(bh));
1271 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1276 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1282 inode_unlock(src_parent);
1283 ldiskfs_journal_stop(jh);
1287 static struct dentry *
1288 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1289 const struct lu_fid *fid, char **name)
1291 struct dentry *root = ERR_PTR(-ENOENT);
1293 if (fid_is_last_id(fid)) {
1294 struct osd_obj_seq *osd_seq;
1296 /* on creation of LAST_ID we create O/<seq> hierarchy */
1297 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1298 if (IS_ERR(osd_seq))
1299 RETURN((struct dentry *)osd_seq);
1302 root = osd_seq->oos_root;
1304 *name = osd_lf_fid2name(fid);
1306 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1307 else if ((*name)[0])
1308 root = osd_sb(osd)->s_root;
1314 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1315 const struct lu_fid *fid, const struct osd_inode_id *id,
1318 struct dentry *root;
1324 root = osd_object_spec_find(info, osd, fid, &name);
1325 if (!IS_ERR(root)) {
1326 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1336 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1337 const struct lu_fid *fid, const struct osd_inode_id *id,
1340 struct dentry *root;
1346 root = osd_object_spec_find(info, osd, fid, &name);
1347 if (!IS_ERR(root)) {
1348 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1358 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1359 const struct lu_fid *fid, struct osd_inode_id *id)
1361 struct dentry *root;
1362 struct dentry *dentry;
1363 struct inode *inode;
1369 if (fid_is_last_id(fid)) {
1370 struct osd_obj_seq *osd_seq;
1372 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1373 if (IS_ERR(osd_seq))
1374 RETURN(PTR_ERR(osd_seq));
1375 root = osd_seq->oos_root;
1378 root = osd_sb(osd)->s_root;
1379 name = osd_lf_fid2name(fid);
1380 if (name == NULL || strlen(name) == 0)
1384 dentry = ll_lookup_one_len(name, root, strlen(name));
1385 if (!IS_ERR(dentry)) {
1386 inode = dentry->d_inode;
1388 if (is_bad_inode(inode)) {
1391 osd_id_gen(id, inode->i_ino,
1392 inode->i_generation);
1397 * if dentry is accessible after osd_compat_spec_insert it
1398 * will still contain NULL inode, so don't keep it in cache
1400 d_invalidate(dentry);
1407 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1408 bool bio_integrity_enabled(struct bio *bio)
1410 # ifdef HAVE_BI_BDEV
1411 struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
1413 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
1416 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1419 if (!bio_sectors(bio))
1422 /* Already protected? */
1423 if (bio_integrity(bio))
1429 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1430 (bi->flags & BLK_INTEGRITY_VERIFY))
1433 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1434 (bi->flags & BLK_INTEGRITY_GENERATE))