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 = KERNEL_DS;
63 newctxt->umask = current_umask();
66 push_ctxt(save, newctxt);
70 * osd_ios_lookup_one_len - lookup single pathname component
72 * @name: pathname component to lookup
73 * @base: base directory to lookup from
74 * @len: maximum length @len should be interpreted to
76 * Treat found dentry with NULL d_inode as an -ENOENT error so LFSCK
77 * can repair the file.
79 struct dentry *osd_ios_lookup_one_len(const char *name, struct dentry *base,
82 struct dentry *dentry;
84 dentry = ll_lookup_one_len(name, base, len);
86 int rc = PTR_ERR(dentry);
89 CERROR("Fail to find %.*s in %.*s (%lu/%u): rc = %d\n",
90 len, name, base->d_name.len,
91 base->d_name.name, base->d_inode->i_ino,
92 base->d_inode->i_generation, rc);
97 if (dentry->d_inode == NULL) {
99 return ERR_PTR(-ENOENT);
105 /* utility to make a directory */
106 static struct dentry *
107 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
108 struct dentry *dir, const struct lu_fid *fid,
109 const char *name, __u32 compat, int mode, bool *created)
111 struct osd_thread_info *info = osd_oti_get(env);
112 struct lu_fid *tfid = &info->oti_fid3;
114 struct dentry *dchild;
119 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
120 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
121 dchild = ll_lookup_one_len(name, dir, strlen(name));
125 inode = dchild->d_inode;
127 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
128 int old_mode = inode->i_mode;
133 if (!S_ISDIR(old_mode)) {
134 CERROR("found %s (%lu/%u) is mode %o\n", name,
135 inode->i_ino, inode->i_generation, old_mode);
136 GOTO(out_err, err = -ENOTDIR);
139 if (unlikely(osd->od_dt_dev.dd_rdonly))
142 /* Fixup directory permissions if necessary */
143 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
145 "fixing permissions on %s from %o to %o\n",
146 name, old_mode, mode);
147 inode->i_mode = (mode & S_IALLUGO) |
148 (old_mode & ~S_IALLUGO);
149 mark_inode_dirty(inode);
152 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
153 &info->oti_ost_attrs);
160 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
161 lma->lma_compat != compat)
167 err = vfs_mkdir(dir->d_inode, dchild, mode);
171 inode = dchild->d_inode;
179 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
180 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
191 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
193 struct lr_server_data lsd;
194 struct dentry *dlast;
197 int count = OBJ_SUBDIR_COUNT;
201 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
204 return PTR_ERR(dlast);
205 else if (dlast->d_inode == NULL)
209 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
210 if (rc == sizeof(lsd)) {
212 "read last_rcvd header, uuid = %s, subdir count = %d\n",
213 lsd.lsd_uuid, lsd.lsd_subdir_count);
214 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
215 count = le16_to_cpu(lsd.lsd_subdir_count);
216 } else if (rc != 0) {
217 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
229 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
231 struct lvfs_run_ctxt new;
232 struct lvfs_run_ctxt save;
233 struct dentry *parent;
234 struct osd_mdobj_map *omm;
236 struct osd_thread_info *info = osd_oti_get(env);
237 struct lu_fid *fid = &info->oti_fid3;
242 OBD_ALLOC_PTR(dev->od_mdt_map);
243 if (dev->od_mdt_map == NULL)
246 omm = dev->od_mdt_map;
248 parent = osd_sb(dev)->s_root;
249 osd_push_ctxt(dev, &new, &save);
251 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
252 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
253 LMAC_NOT_IN_OI, 0755, NULL);
255 GOTO(cleanup, rc = PTR_ERR(d));
257 omm->omm_remote_parent = d;
259 GOTO(cleanup, rc = 0);
262 pop_ctxt(&save, &new);
264 if (omm->omm_remote_parent != NULL)
265 dput(omm->omm_remote_parent);
267 dev->od_mdt_map = NULL;
272 static void osd_mdt_fini(struct osd_device *osd)
274 struct osd_mdobj_map *omm = osd->od_mdt_map;
279 if (omm->omm_remote_parent)
280 dput(omm->omm_remote_parent);
283 osd->od_ost_map = NULL;
286 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
287 struct osd_object *obj, struct osd_thandle *oh)
289 struct osd_mdobj_map *omm = osd->od_mdt_map;
290 struct osd_thread_info *oti = osd_oti_get(env);
291 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
292 char *name = oti->oti_name;
293 struct osd_thread_info *info = osd_oti_get(env);
294 struct dentry *dentry;
295 struct dentry *parent;
298 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
302 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
303 * can identify this object quickly
305 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
306 &oti->oti_ost_attrs);
310 lma->lma_incompat |= LMAI_REMOTE_PARENT;
311 lustre_lma_swab(lma);
312 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
313 sizeof(*lma), XATTR_REPLACE);
317 parent = omm->omm_remote_parent;
318 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
319 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
321 inode_lock(parent->d_inode);
322 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
323 obj->oo_inode, NULL);
324 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
325 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
326 else if (unlikely(rc == -EEXIST))
329 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
330 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
331 osd_name(osd), name, rc);
332 mark_inode_dirty(parent->d_inode);
333 inode_unlock(parent->d_inode);
337 int osd_delete_from_remote_parent(const struct lu_env *env,
338 struct osd_device *osd,
339 struct osd_object *obj,
340 struct osd_thandle *oh, bool destroy)
342 struct osd_mdobj_map *omm = osd->od_mdt_map;
343 struct osd_thread_info *oti = osd_oti_get(env);
344 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
345 char *name = oti->oti_name;
346 struct dentry *dentry;
347 struct dentry *parent;
348 struct ldiskfs_dir_entry_2 *de;
349 struct buffer_head *bh;
352 parent = omm->omm_remote_parent;
353 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
354 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
356 inode_lock(parent->d_inode);
357 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
360 inode_unlock(parent->d_inode);
362 if (unlikely(rc == -ENOENT))
365 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
367 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
368 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
369 mark_inode_dirty(parent->d_inode);
370 inode_unlock(parent->d_inode);
372 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
373 osd_name(osd), name, rc);
378 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
383 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
384 &oti->oti_ost_attrs);
388 /* Get rid of REMOTE_PARENT flag from incompat */
389 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
390 lustre_lma_swab(lma);
391 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
392 sizeof(*lma), XATTR_REPLACE);
394 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
398 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
399 struct osd_device *osd,
400 const struct lu_fid *fid,
401 struct osd_inode_id *id)
403 struct osd_mdobj_map *omm = osd->od_mdt_map;
404 char *name = oti->oti_name;
405 struct dentry *parent;
406 struct dentry *dentry;
407 struct ldiskfs_dir_entry_2 *de;
408 struct buffer_head *bh;
413 if (unlikely(osd->od_is_ost))
416 parent = omm->omm_remote_parent;
417 sprintf(name, DFID_NOBRACE, PFID(fid));
418 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
420 inode_lock(parent->d_inode);
421 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
428 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
430 inode = osd_iget(oti, osd, id);
440 inode_unlock(parent->d_inode);
442 osd_add_oi_cache(oti, osd, id, fid);
447 * directory structure on legacy OST:
449 * O/<seq>/d0-31/<objid>
456 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
458 struct lvfs_run_ctxt new;
459 struct lvfs_run_ctxt save;
462 bool created = false;
466 OBD_ALLOC_PTR(dev->od_ost_map);
467 if (dev->od_ost_map == NULL)
470 /* to get subdir count from last_rcvd */
471 rc = osd_last_rcvd_subdir_count(dev);
473 GOTO(cleanup_alloc, rc);
475 dev->od_ost_map->om_subdir_count = rc;
476 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
477 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
478 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
480 osd_push_ctxt(dev, &new, &save);
481 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
482 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
484 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
487 /* It is quite probably that the device is new formatted. */
488 dev->od_maybe_new = 1;
490 dev->od_ost_map->om_root = d;
492 pop_ctxt(&save, &new);
496 pop_ctxt(&save, &new);
498 OBD_FREE_PTR(dev->od_ost_map);
502 static void osd_seq_free(struct osd_obj_seq *osd_seq)
506 if (osd_seq->oos_dirs) {
507 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
508 if (osd_seq->oos_dirs[j])
509 dput(osd_seq->oos_dirs[j]);
511 OBD_FREE(osd_seq->oos_dirs,
512 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
515 if (osd_seq->oos_root)
516 dput(osd_seq->oos_root);
518 OBD_FREE_PTR(osd_seq);
521 static void osd_ost_fini(struct osd_device *osd)
523 struct osd_obj_seq *osd_seq;
524 struct osd_obj_seq *tmp;
525 struct osd_obj_map *map = osd->od_ost_map;
532 write_lock(&map->om_seq_list_lock);
533 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
535 list_del_init(&osd_seq->oos_seq_list);
536 write_unlock(&map->om_seq_list_lock);
537 osd_seq_free(osd_seq);
538 write_lock(&map->om_seq_list_lock);
540 write_unlock(&map->om_seq_list_lock);
544 osd->od_ost_map = NULL;
548 static int osd_index_backup_dir_init(const struct lu_env *env,
549 struct osd_device *dev)
551 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
552 struct lvfs_run_ctxt new;
553 struct lvfs_run_ctxt save;
554 struct dentry *dentry;
559 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
560 osd_push_ctxt(dev, &new, &save);
561 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
562 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
563 if (IS_ERR(dentry)) {
564 rc = PTR_ERR(dentry);
566 dev->od_index_backup_inode = igrab(dentry->d_inode);
569 pop_ctxt(&save, &new);
574 static void osd_index_backup_dir_fini(struct osd_device *dev)
576 if (dev->od_index_backup_inode) {
577 iput(dev->od_index_backup_inode);
578 dev->od_index_backup_inode = NULL;
582 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
585 bool mdt_init = false;
589 rc = osd_ost_init(env, dev);
593 if (!dev->od_is_ost) {
594 rc = osd_mdt_init(env, dev);
603 rc = osd_index_backup_dir_init(env, dev);
613 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
615 struct osd_obj_seq *osd_seq;
617 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
618 if (osd_seq->oos_seq == seq)
624 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
626 struct osd_obj_seq *osd_seq;
628 read_lock(&map->om_seq_list_lock);
629 osd_seq = osd_seq_find_locked(map, seq);
630 read_unlock(&map->om_seq_list_lock);
634 void osd_obj_map_fini(struct osd_device *dev)
636 osd_index_backup_dir_fini(dev);
642 * Update the specified OI mapping.
644 * \retval 1, changed nothing
645 * \retval 0, changed successfully
646 * \retval -ve, on error
648 static int osd_obj_update_entry(struct osd_thread_info *info,
649 struct osd_device *osd,
650 struct dentry *dir, const char *name,
651 const struct lu_fid *fid,
652 const struct osd_inode_id *id,
655 struct inode *parent = dir->d_inode;
656 struct dentry *child;
657 struct ldiskfs_dir_entry_2 *de;
658 struct buffer_head *bh;
660 struct dentry *dentry = &info->oti_obj_dentry;
661 struct osd_inode_id *oi_id = &info->oti_id3;
662 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
663 struct lu_fid *oi_fid = &lma->lma_self_fid;
669 LASSERT(th->h_transaction != NULL);
671 child = &info->oti_child_dentry;
672 child->d_parent = dir;
673 child->d_name.hash = 0;
674 child->d_name.name = name;
675 child->d_name.len = strlen(name);
677 ll_vfs_dq_init(parent);
679 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
681 GOTO(out, rc = PTR_ERR(bh));
683 if (le32_to_cpu(de->inode) == id->oii_ino)
686 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
687 inode = osd_iget(info, osd, oi_id);
690 if (rc == -ENOENT || rc == -ESTALE)
696 * The EA inode should NOT be in OI, old OI scrub may added
697 * such OI mapping by wrong, replace it.
699 if (unlikely(osd_is_ea_inode(inode))) {
704 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
705 if (rc == -ENODATA) {
706 rc = osd_get_idif(info, inode, dentry, oi_fid);
707 if (rc > 0 || rc == -ENODATA) {
718 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
719 * either a crashed object or a uninitialized one. Replace it.
721 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
722 CERROR("%s: the FID "DFID" is used by two objects: "
723 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
724 oi_id->oii_ino, oi_id->oii_gen,
725 id->oii_ino, id->oii_gen);
726 GOTO(out, rc = -EEXIST);
729 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
730 __u32 idx1 = fid_idif_ost_idx(fid);
731 __u32 idx2 = fid_idif_ost_idx(oi_fid);
732 struct ost_id *ostid = &info->oti_ostid;
733 struct lu_fid *tfid = &info->oti_fid3;
735 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
736 "invalid given FID "DFID", not match the "
737 "device index %u\n", PFID(fid), osd->od_index);
740 if (idx1 == 0 && idx2 == osd->od_index) {
741 fid_to_ostid(fid, ostid);
742 ostid_to_fid(tfid, ostid, idx2);
743 if (lu_fid_eq(tfid, oi_fid)) {
744 CERROR("%s: the FID "DFID" is used by "
745 "two objects(2): %u/%u %u/%u\n",
746 osd_name(osd), PFID(fid),
747 oi_id->oii_ino, oi_id->oii_gen,
748 id->oii_ino, id->oii_gen);
750 GOTO(out, rc = -EEXIST);
752 } else if (idx2 == 0 && idx1 == osd->od_index) {
753 fid_to_ostid(oi_fid, ostid);
754 ostid_to_fid(tfid, ostid, idx1);
755 if (lu_fid_eq(tfid, fid)) {
756 CERROR("%s: the FID "DFID" is used by "
757 "two objects(2): %u/%u %u/%u\n",
758 osd_name(osd), PFID(fid),
759 oi_id->oii_ino, oi_id->oii_gen,
760 id->oii_ino, id->oii_gen);
762 GOTO(out, rc = -EEXIST);
770 * There may be temporary inconsistency: On one hand, the new
771 * object may be referenced by multiple entries, which is out
772 * of our control unless we traverse the whole /O completely,
773 * which is non-flat order and inefficient, should be avoided;
774 * On the other hand, the old object may become orphan if it
775 * is still valid. Since it was referenced by an invalid entry,
776 * making it as invisible temporary may be not worse. OI scrub
777 * will process it later.
779 rc = ldiskfs_journal_get_write_access(th, bh);
783 de->inode = cpu_to_le32(id->oii_ino);
784 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
791 inode_unlock(parent);
795 static int osd_obj_del_entry(struct osd_thread_info *info,
796 struct osd_device *osd,
797 struct dentry *dird, char *name,
800 struct ldiskfs_dir_entry_2 *de;
801 struct buffer_head *bh;
802 struct dentry *child;
803 struct inode *dir = dird->d_inode;
809 LASSERT(th->h_transaction != NULL);
811 child = &info->oti_child_dentry;
812 child->d_name.hash = 0;
813 child->d_name.name = name;
814 child->d_name.len = strlen(name);
815 child->d_parent = dird;
816 child->d_inode = NULL;
820 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
824 rc = ldiskfs_delete_entry(th, dir, de, bh);
832 static int osd_obj_add_entry(struct osd_thread_info *info,
833 struct osd_device *osd,
834 struct dentry *dir, char *name,
835 const struct osd_inode_id *id,
838 struct dentry *child;
844 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
848 LASSERT(th->h_transaction != NULL);
850 inode = info->oti_inode;
851 if (unlikely(inode == NULL)) {
852 struct ldiskfs_inode_info *lii;
857 inode = info->oti_inode = &lii->vfs_inode;
860 inode->i_sb = osd_sb(osd);
861 osd_id_to_inode(inode, id);
862 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
864 child = &info->oti_child_dentry;
865 child->d_name.hash = 0;
866 child->d_name.name = name;
867 child->d_name.len = strlen(name);
868 child->d_parent = dir;
869 child->d_inode = inode;
871 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
874 ll_vfs_dq_init(dir->d_inode);
875 inode_lock(dir->d_inode);
876 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
877 inode_unlock(dir->d_inode);
883 * Use %llu for legacy OST sequences, but use %llx for new
884 * sequences names, so that the O/{seq}/dN/{oid} more closely
885 * follows the DFID/PFID format. This makes it easier to map from
886 * debug messages to objects in the future, and the legacy space
887 * of FID_SEQ_OST_MDT0 will be unused in the future.
889 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
891 snprintf(seq_name, name_size,
892 (fid_seq_is_rsvd(seq) ||
893 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
894 fid_seq_is_idif(seq) ? 0 : seq);
897 static inline void osd_oid_name(char *name, size_t name_size,
898 const struct lu_fid *fid, u64 id)
900 snprintf(name, name_size,
901 (fid_seq_is_rsvd(fid_seq(fid)) ||
902 fid_seq_is_mdt0(fid_seq(fid)) ||
903 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
906 /* external locking is required */
907 static int osd_seq_load_locked(struct osd_thread_info *info,
908 struct osd_device *osd,
909 struct osd_obj_seq *osd_seq)
911 struct osd_obj_map *map = osd->od_ost_map;
912 struct dentry *seq_dir;
919 if (osd_seq->oos_root != NULL)
923 LASSERT(map->om_root);
925 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
927 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
928 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
930 GOTO(out_err, rc = PTR_ERR(seq_dir));
931 else if (seq_dir->d_inode == NULL)
932 GOTO(out_put, rc = -EFAULT);
934 osd_seq->oos_root = seq_dir;
936 LASSERT(osd_seq->oos_dirs == NULL);
937 OBD_ALLOC(osd_seq->oos_dirs,
938 sizeof(seq_dir) * osd_seq->oos_subdir_count);
939 if (osd_seq->oos_dirs == NULL)
940 GOTO(out_put, rc = -ENOMEM);
942 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
945 snprintf(dir_name, sizeof(dir_name), "d%u", i);
946 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
947 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
950 GOTO(out_free, rc = PTR_ERR(dir));
951 } else if (dir->d_inode == NULL) {
953 GOTO(out_free, rc = -EFAULT);
956 osd_seq->oos_dirs[i] = dir;
961 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
962 if (osd_seq->oos_dirs[i] != NULL)
963 dput(osd_seq->oos_dirs[i]);
965 OBD_FREE(osd_seq->oos_dirs,
966 sizeof(seq_dir) * osd_seq->oos_subdir_count);
969 osd_seq->oos_root = NULL;
975 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
976 struct osd_device *osd, u64 seq)
978 struct osd_obj_map *map;
979 struct osd_obj_seq *osd_seq;
984 map = osd->od_ost_map;
986 LASSERT(map->om_root);
988 osd_seq = osd_seq_find(map, seq);
989 if (likely(osd_seq != NULL))
992 /* Serializing init process */
993 mutex_lock(&map->om_dir_init_mutex);
995 /* Check whether the seq has been added */
996 read_lock(&map->om_seq_list_lock);
997 osd_seq = osd_seq_find_locked(map, seq);
998 if (osd_seq != NULL) {
999 read_unlock(&map->om_seq_list_lock);
1000 GOTO(cleanup, rc = 0);
1002 read_unlock(&map->om_seq_list_lock);
1004 OBD_ALLOC_PTR(osd_seq);
1005 if (osd_seq == NULL)
1006 GOTO(cleanup, rc = -ENOMEM);
1008 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
1009 osd_seq->oos_seq = seq;
1011 * Init subdir count to be 32, but each seq can have
1012 * different subdir count
1014 osd_seq->oos_subdir_count = map->om_subdir_count;
1015 rc = osd_seq_load_locked(info, osd, osd_seq);
1019 write_lock(&map->om_seq_list_lock);
1020 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
1021 write_unlock(&map->om_seq_list_lock);
1024 mutex_unlock(&map->om_dir_init_mutex);
1026 if (osd_seq != NULL)
1027 OBD_FREE_PTR(osd_seq);
1028 RETURN(ERR_PTR(rc));
1034 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
1035 const struct lu_fid *fid, struct osd_inode_id *id)
1037 struct osd_obj_map *map;
1038 struct osd_obj_seq *osd_seq;
1039 struct dentry *d_seq;
1040 struct dentry *child;
1041 struct ost_id *ostid = &info->oti_ostid;
1044 struct ldiskfs_dir_entry_2 *de;
1045 struct buffer_head *bh;
1047 struct inode *inode;
1051 /* on the very first lookup we find and open directories */
1052 map = dev->od_ost_map;
1054 LASSERT(map->om_root);
1056 fid_to_ostid(fid, ostid);
1057 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1058 if (IS_ERR(osd_seq))
1059 RETURN(PTR_ERR(osd_seq));
1061 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1062 d_seq = osd_seq->oos_dirs[dirn];
1065 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1067 child = &info->oti_child_dentry;
1068 child->d_parent = d_seq;
1069 child->d_name.hash = 0;
1070 child->d_name.name = name;
1071 /* XXX: we can use rc from sprintf() instead of strlen() */
1072 child->d_name.len = strlen(name);
1074 dir = d_seq->d_inode;
1076 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1080 RETURN(PTR_ERR(bh));
1082 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1085 inode = osd_iget(info, dev, id);
1086 if (IS_ERR(inode)) {
1087 int rc = PTR_ERR(inode);
1089 RETURN(rc == -ENOENT ? -ESTALE : rc);
1096 int osd_obj_map_insert(struct osd_thread_info *info,
1097 struct osd_device *osd,
1098 const struct lu_fid *fid,
1099 const struct osd_inode_id *id,
1102 struct osd_obj_map *map;
1103 struct osd_obj_seq *osd_seq;
1105 struct ost_id *ostid = &info->oti_ostid;
1112 map = osd->od_ost_map;
1115 /* map fid to seq:objid */
1116 fid_to_ostid(fid, ostid);
1118 oid = ostid_id(ostid);
1119 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1120 if (IS_ERR(osd_seq))
1121 RETURN(PTR_ERR(osd_seq));
1123 dirn = oid & (osd_seq->oos_subdir_count - 1);
1124 d = osd_seq->oos_dirs[dirn];
1127 osd_oid_name(name, sizeof(name), fid, oid);
1130 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1131 if (rc == -EEXIST) {
1132 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1133 if (unlikely(rc == -ENOENT))
1136 if (unlikely(rc == 1))
1143 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1144 const struct lu_fid *fid, handle_t *th)
1146 struct osd_obj_map *map;
1147 struct osd_obj_seq *osd_seq;
1149 struct ost_id *ostid = &info->oti_ostid;
1155 map = osd->od_ost_map;
1158 /* map fid to seq:objid */
1159 fid_to_ostid(fid, ostid);
1161 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1162 if (IS_ERR(osd_seq))
1163 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1165 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1166 d = osd_seq->oos_dirs[dirn];
1169 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1170 rc = osd_obj_del_entry(info, osd, d, name, th);
1175 int osd_obj_map_update(struct osd_thread_info *info,
1176 struct osd_device *osd,
1177 const struct lu_fid *fid,
1178 const struct osd_inode_id *id,
1181 struct osd_obj_seq *osd_seq;
1183 struct ost_id *ostid = &info->oti_ostid;
1189 fid_to_ostid(fid, ostid);
1190 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1191 if (IS_ERR(osd_seq))
1192 RETURN(PTR_ERR(osd_seq));
1194 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1195 d = osd_seq->oos_dirs[dirn];
1198 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1199 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1204 int osd_obj_map_recover(struct osd_thread_info *info,
1205 struct osd_device *osd,
1206 struct inode *src_parent,
1207 struct dentry *src_child,
1208 const struct lu_fid *fid)
1210 struct osd_obj_seq *osd_seq;
1211 struct dentry *tgt_parent;
1212 struct dentry *tgt_child = &info->oti_child_dentry;
1214 struct inode *inode = src_child->d_inode;
1215 struct ost_id *ostid = &info->oti_ostid;
1217 struct ldiskfs_dir_entry_2 *de;
1218 struct buffer_head *bh;
1225 if (fid_is_last_id(fid)) {
1226 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1227 if (IS_ERR(osd_seq))
1228 RETURN(PTR_ERR(osd_seq));
1230 tgt_parent = osd_seq->oos_root;
1231 tgt_child->d_name.name = "LAST_ID";
1232 tgt_child->d_name.len = strlen("LAST_ID");
1234 fid_to_ostid(fid, ostid);
1235 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1236 if (IS_ERR(osd_seq))
1237 RETURN(PTR_ERR(osd_seq));
1239 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1240 tgt_parent = osd_seq->oos_dirs[dirn];
1241 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1242 tgt_child->d_name.name = name;
1243 tgt_child->d_name.len = strlen(name);
1245 LASSERT(tgt_parent != NULL);
1247 dir = tgt_parent->d_inode;
1248 tgt_child->d_name.hash = 0;
1249 tgt_child->d_parent = tgt_parent;
1250 tgt_child->d_inode = inode;
1252 /* The non-initialized src_child may be destroyed. */
1253 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1254 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1255 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1256 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1258 RETURN(PTR_ERR(jh));
1260 ll_vfs_dq_init(src_parent);
1261 ll_vfs_dq_init(dir);
1263 inode_lock(src_parent);
1265 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1268 * XXX: If some other object occupied the same slot. And If such
1269 * inode is zero-sized and with SUID+SGID, then means it is
1270 * a new created one. Maybe we can remove it and insert the
1271 * original one back to the /O/<seq>/d<x>. But there are
1272 * something to be considered:
1274 * 1) The OST-object under /lost+found has crashed LMA.
1275 * So it should not conflict with the current one.
1277 * 2) There are race conditions that: someone may just want
1278 * to modify the current one. Even if the OI scrub takes
1279 * the object lock when remove the current one, it still
1280 * cause the modification to be lost becasue the target
1281 * has been removed when the RPC service thread waiting
1284 * So keep it there before we have suitable solution.
1288 inode_unlock(src_parent);
1289 ldiskfs_journal_stop(jh);
1292 /* If the src object has never been modified, then remove it. */
1293 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1294 inode->i_mode & S_ISGID) {
1295 rc = ll_vfs_unlink(src_parent, src_child);
1296 if (unlikely(rc == -ENOENT))
1303 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1305 if (unlikely(IS_ERR(bh)))
1306 GOTO(unlock, rc = PTR_ERR(bh));
1308 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1313 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1319 inode_unlock(src_parent);
1320 ldiskfs_journal_stop(jh);
1324 static struct dentry *
1325 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1326 const struct lu_fid *fid, char **name)
1328 struct dentry *root = ERR_PTR(-ENOENT);
1330 if (fid_is_last_id(fid)) {
1331 struct osd_obj_seq *osd_seq;
1333 /* on creation of LAST_ID we create O/<seq> hierarchy */
1334 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1335 if (IS_ERR(osd_seq))
1336 RETURN((struct dentry *)osd_seq);
1339 root = osd_seq->oos_root;
1341 *name = osd_lf_fid2name(fid);
1343 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1344 else if ((*name)[0])
1345 root = osd_sb(osd)->s_root;
1351 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1352 const struct lu_fid *fid, const struct osd_inode_id *id,
1355 struct dentry *root;
1361 root = osd_object_spec_find(info, osd, fid, &name);
1362 if (!IS_ERR(root)) {
1363 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1373 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1374 const struct lu_fid *fid, const struct osd_inode_id *id,
1377 struct dentry *root;
1383 root = osd_object_spec_find(info, osd, fid, &name);
1384 if (!IS_ERR(root)) {
1385 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1395 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1396 const struct lu_fid *fid, struct osd_inode_id *id)
1398 struct dentry *root;
1399 struct dentry *dentry;
1400 struct inode *inode;
1406 if (fid_is_last_id(fid)) {
1407 struct osd_obj_seq *osd_seq;
1409 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1410 if (IS_ERR(osd_seq))
1411 RETURN(PTR_ERR(osd_seq));
1412 root = osd_seq->oos_root;
1415 root = osd_sb(osd)->s_root;
1416 name = osd_lf_fid2name(fid);
1417 if (name == NULL || strlen(name) == 0)
1421 dentry = ll_lookup_one_len(name, root, strlen(name));
1422 if (!IS_ERR(dentry)) {
1423 inode = dentry->d_inode;
1425 if (is_bad_inode(inode)) {
1428 osd_id_gen(id, inode->i_ino,
1429 inode->i_generation);
1434 * if dentry is accessible after osd_compat_spec_insert it
1435 * will still contain NULL inode, so don't keep it in cache
1437 d_invalidate(dentry);
1444 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1445 bool bio_integrity_enabled(struct bio *bio)
1447 struct blk_integrity *bi = blk_get_integrity(bio_get_disk(bio));
1449 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1452 if (!bio_sectors(bio))
1455 /* Already protected? */
1456 if (bio_integrity(bio))
1462 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1463 (bi->flags & BLK_INTEGRITY_VERIFY))
1466 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1467 (bi->flags & BLK_INTEGRITY_GENERATE))