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_lookup_one_len_unlocked
72 * @name: pathname component to lookup
73 * @base: base directory to lookup from
74 * @len: maximum length @len should be interpreted to
76 * This should be called without the parent
77 * i_mutex held, and will take the i_mutex itself.
79 * Unlike osd_lookup_one_len dentry with NULL d_inode is valid
81 struct dentry *osd_lookup_one_len_unlocked(const char *name,
82 struct dentry *base, int len)
84 struct dentry *dchild;
86 inode_lock(base->d_inode);
87 dchild = lookup_one_len(name, base, len);
88 inode_unlock(base->d_inode);
90 if (IS_ERR(dchild) || dchild->d_inode == NULL)
93 if (is_bad_inode(dchild->d_inode)) {
94 CERROR("bad inode returned %lu/%u\n",
95 dchild->d_inode->i_ino, dchild->d_inode->i_generation);
97 dchild = ERR_PTR(-ENOENT);
104 * osd_ios_lookup_one_len - lookup single pathname component
106 * @name: pathname component to lookup
107 * @base: base directory to lookup from
108 * @len: maximum length @len should be interpreted to
110 * Treat found dentry with NULL d_inode as an -ENOENT error so LFSCK
111 * can repair the file.
113 struct dentry *osd_ios_lookup_one_len(const char *name, struct dentry *base,
116 struct dentry *dentry;
118 dentry = osd_lookup_one_len_unlocked(name, base, len);
119 if (IS_ERR(dentry)) {
120 int rc = PTR_ERR(dentry);
123 CERROR("Fail to find %.*s in %.*s (%lu/%u): rc = %d\n",
124 len, name, base->d_name.len,
125 base->d_name.name, base->d_inode->i_ino,
126 base->d_inode->i_generation, rc);
131 if (dentry->d_inode == NULL) {
133 return ERR_PTR(-ENOENT);
139 /* utility to make a directory */
140 static struct dentry *
141 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
142 struct dentry *dir, const struct lu_fid *fid,
143 const char *name, __u32 compat, int mode, bool *created)
145 struct osd_thread_info *info = osd_oti_get(env);
146 struct lu_fid *tfid = &info->oti_fid3;
148 struct dentry *dchild;
153 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
154 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
155 dchild = osd_lookup_one_len_unlocked(name, dir, strlen(name));
159 inode = dchild->d_inode;
161 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
162 int old_mode = inode->i_mode;
167 if (!S_ISDIR(old_mode)) {
168 CERROR("found %s (%lu/%u) is mode %o\n", name,
169 inode->i_ino, inode->i_generation, old_mode);
170 GOTO(out_err, err = -ENOTDIR);
173 if (unlikely(osd->od_dt_dev.dd_rdonly))
176 /* Fixup directory permissions if necessary */
177 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
179 "fixing permissions on %s from %o to %o\n",
180 name, old_mode, mode);
181 inode->i_mode = (mode & S_IALLUGO) |
182 (old_mode & ~S_IALLUGO);
183 mark_inode_dirty(inode);
186 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
187 &info->oti_ost_attrs);
194 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
195 lma->lma_compat != compat)
201 err = vfs_mkdir(dir->d_inode, dchild, mode);
205 inode = dchild->d_inode;
213 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
214 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
225 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
227 struct lr_server_data lsd;
228 struct dentry *dlast;
231 int count = OBJ_SUBDIR_COUNT;
235 dlast = osd_lookup_one_len_unlocked(LAST_RCVD, osd_sb(osd)->s_root,
238 return PTR_ERR(dlast);
239 else if (dlast->d_inode == NULL)
243 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
244 if (rc == sizeof(lsd)) {
246 "read last_rcvd header, uuid = %s, subdir count = %d\n",
247 lsd.lsd_uuid, lsd.lsd_subdir_count);
248 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
249 count = le16_to_cpu(lsd.lsd_subdir_count);
250 } else if (rc != 0) {
251 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
263 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
265 struct lvfs_run_ctxt new;
266 struct lvfs_run_ctxt save;
267 struct dentry *parent;
268 struct osd_mdobj_map *omm;
270 struct osd_thread_info *info = osd_oti_get(env);
271 struct lu_fid *fid = &info->oti_fid3;
276 OBD_ALLOC_PTR(dev->od_mdt_map);
277 if (dev->od_mdt_map == NULL)
280 omm = dev->od_mdt_map;
282 parent = osd_sb(dev)->s_root;
283 osd_push_ctxt(dev, &new, &save);
285 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
286 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
287 LMAC_NOT_IN_OI, 0755, NULL);
289 GOTO(cleanup, rc = PTR_ERR(d));
291 omm->omm_remote_parent = d;
293 GOTO(cleanup, rc = 0);
296 pop_ctxt(&save, &new);
298 if (omm->omm_remote_parent != NULL)
299 dput(omm->omm_remote_parent);
301 dev->od_mdt_map = NULL;
306 static void osd_mdt_fini(struct osd_device *osd)
308 struct osd_mdobj_map *omm = osd->od_mdt_map;
313 if (omm->omm_remote_parent)
314 dput(omm->omm_remote_parent);
317 osd->od_ost_map = NULL;
320 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
321 struct osd_object *obj, struct osd_thandle *oh)
323 struct osd_mdobj_map *omm = osd->od_mdt_map;
324 struct osd_thread_info *oti = osd_oti_get(env);
325 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
326 char *name = oti->oti_name;
327 struct osd_thread_info *info = osd_oti_get(env);
328 struct dentry *dentry;
329 struct dentry *parent;
332 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
336 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
337 * can identify this object quickly
339 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
340 &oti->oti_ost_attrs);
344 lma->lma_incompat |= LMAI_REMOTE_PARENT;
345 lustre_lma_swab(lma);
346 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
347 sizeof(*lma), XATTR_REPLACE);
351 parent = omm->omm_remote_parent;
352 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
353 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
355 inode_lock(parent->d_inode);
356 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
357 obj->oo_inode, NULL);
358 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
359 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
360 else if (unlikely(rc == -EEXIST))
363 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
364 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
365 osd_name(osd), name, rc);
366 mark_inode_dirty(parent->d_inode);
367 inode_unlock(parent->d_inode);
371 int osd_delete_from_remote_parent(const struct lu_env *env,
372 struct osd_device *osd,
373 struct osd_object *obj,
374 struct osd_thandle *oh, bool destroy)
376 struct osd_mdobj_map *omm = osd->od_mdt_map;
377 struct osd_thread_info *oti = osd_oti_get(env);
378 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
379 char *name = oti->oti_name;
380 struct dentry *dentry;
381 struct dentry *parent;
382 struct ldiskfs_dir_entry_2 *de;
383 struct buffer_head *bh;
386 parent = omm->omm_remote_parent;
387 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
388 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
390 inode_lock(parent->d_inode);
391 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
394 inode_unlock(parent->d_inode);
396 if (unlikely(rc == -ENOENT))
399 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
401 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
402 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
403 mark_inode_dirty(parent->d_inode);
404 inode_unlock(parent->d_inode);
406 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
407 osd_name(osd), name, rc);
412 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
417 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
418 &oti->oti_ost_attrs);
422 /* Get rid of REMOTE_PARENT flag from incompat */
423 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
424 lustre_lma_swab(lma);
425 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
426 sizeof(*lma), XATTR_REPLACE);
428 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
432 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
433 struct osd_device *osd,
434 const struct lu_fid *fid,
435 struct osd_inode_id *id)
437 struct osd_mdobj_map *omm = osd->od_mdt_map;
438 char *name = oti->oti_name;
439 struct dentry *parent;
440 struct dentry *dentry;
441 struct ldiskfs_dir_entry_2 *de;
442 struct buffer_head *bh;
447 if (unlikely(osd->od_is_ost))
450 parent = omm->omm_remote_parent;
451 sprintf(name, DFID_NOBRACE, PFID(fid));
452 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
454 inode_lock(parent->d_inode);
455 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
462 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
464 inode = osd_iget(oti, osd, id);
474 inode_unlock(parent->d_inode);
476 osd_add_oi_cache(oti, osd, id, fid);
481 * directory structure on legacy OST:
483 * O/<seq>/d0-31/<objid>
490 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
492 struct lvfs_run_ctxt new;
493 struct lvfs_run_ctxt save;
496 bool created = false;
500 OBD_ALLOC_PTR(dev->od_ost_map);
501 if (dev->od_ost_map == NULL)
504 /* to get subdir count from last_rcvd */
505 rc = osd_last_rcvd_subdir_count(dev);
507 GOTO(cleanup_alloc, rc);
509 dev->od_ost_map->om_subdir_count = rc;
510 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
511 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
512 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
514 osd_push_ctxt(dev, &new, &save);
515 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
516 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
518 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
521 /* It is quite probably that the device is new formatted. */
522 dev->od_maybe_new = 1;
524 dev->od_ost_map->om_root = d;
526 pop_ctxt(&save, &new);
530 pop_ctxt(&save, &new);
532 OBD_FREE_PTR(dev->od_ost_map);
536 static void osd_seq_free(struct osd_obj_seq *osd_seq)
540 if (osd_seq->oos_dirs) {
541 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
542 if (osd_seq->oos_dirs[j])
543 dput(osd_seq->oos_dirs[j]);
545 OBD_FREE(osd_seq->oos_dirs,
546 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
549 if (osd_seq->oos_root)
550 dput(osd_seq->oos_root);
552 OBD_FREE_PTR(osd_seq);
555 static void osd_ost_fini(struct osd_device *osd)
557 struct osd_obj_seq *osd_seq;
558 struct osd_obj_seq *tmp;
559 struct osd_obj_map *map = osd->od_ost_map;
566 write_lock(&map->om_seq_list_lock);
567 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
569 list_del_init(&osd_seq->oos_seq_list);
570 write_unlock(&map->om_seq_list_lock);
571 osd_seq_free(osd_seq);
572 write_lock(&map->om_seq_list_lock);
574 write_unlock(&map->om_seq_list_lock);
578 osd->od_ost_map = NULL;
582 static int osd_index_backup_dir_init(const struct lu_env *env,
583 struct osd_device *dev)
585 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
586 struct lvfs_run_ctxt new;
587 struct lvfs_run_ctxt save;
588 struct dentry *dentry;
593 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
594 osd_push_ctxt(dev, &new, &save);
595 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
596 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
597 if (IS_ERR(dentry)) {
598 rc = PTR_ERR(dentry);
600 dev->od_index_backup_inode = igrab(dentry->d_inode);
603 pop_ctxt(&save, &new);
608 static void osd_index_backup_dir_fini(struct osd_device *dev)
610 if (dev->od_index_backup_inode) {
611 iput(dev->od_index_backup_inode);
612 dev->od_index_backup_inode = NULL;
616 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
619 bool mdt_init = false;
623 rc = osd_ost_init(env, dev);
627 if (!dev->od_is_ost) {
628 rc = osd_mdt_init(env, dev);
637 rc = osd_index_backup_dir_init(env, dev);
647 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
649 struct osd_obj_seq *osd_seq;
651 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
652 if (osd_seq->oos_seq == seq)
658 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
660 struct osd_obj_seq *osd_seq;
662 read_lock(&map->om_seq_list_lock);
663 osd_seq = osd_seq_find_locked(map, seq);
664 read_unlock(&map->om_seq_list_lock);
668 void osd_obj_map_fini(struct osd_device *dev)
670 osd_index_backup_dir_fini(dev);
676 * Update the specified OI mapping.
678 * \retval 1, changed nothing
679 * \retval 0, changed successfully
680 * \retval -ve, on error
682 static int osd_obj_update_entry(struct osd_thread_info *info,
683 struct osd_device *osd,
684 struct dentry *dir, const char *name,
685 const struct lu_fid *fid,
686 const struct osd_inode_id *id,
689 struct inode *parent = dir->d_inode;
690 struct dentry *child;
691 struct ldiskfs_dir_entry_2 *de;
692 struct buffer_head *bh;
694 struct dentry *dentry = &info->oti_obj_dentry;
695 struct osd_inode_id *oi_id = &info->oti_id3;
696 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
697 struct lu_fid *oi_fid = &lma->lma_self_fid;
703 LASSERT(th->h_transaction != NULL);
705 child = &info->oti_child_dentry;
706 child->d_parent = dir;
707 child->d_name.hash = 0;
708 child->d_name.name = name;
709 child->d_name.len = strlen(name);
711 ll_vfs_dq_init(parent);
713 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
715 GOTO(out, rc = PTR_ERR(bh));
717 if (le32_to_cpu(de->inode) == id->oii_ino)
720 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
721 inode = osd_iget(info, osd, oi_id);
724 if (rc == -ENOENT || rc == -ESTALE)
730 * The EA inode should NOT be in OI, old OI scrub may added
731 * such OI mapping by wrong, replace it.
733 if (unlikely(osd_is_ea_inode(inode))) {
738 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
739 if (rc == -ENODATA) {
740 rc = osd_get_idif(info, inode, dentry, oi_fid);
741 if (rc > 0 || rc == -ENODATA) {
752 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
753 * either a crashed object or a uninitialized one. Replace it.
755 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
756 CERROR("%s: the FID "DFID" is used by two objects: "
757 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
758 oi_id->oii_ino, oi_id->oii_gen,
759 id->oii_ino, id->oii_gen);
760 GOTO(out, rc = -EEXIST);
763 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
764 __u32 idx1 = fid_idif_ost_idx(fid);
765 __u32 idx2 = fid_idif_ost_idx(oi_fid);
766 struct ost_id *ostid = &info->oti_ostid;
767 struct lu_fid *tfid = &info->oti_fid3;
769 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
770 "invalid given FID "DFID", not match the "
771 "device index %u\n", PFID(fid), osd->od_index);
774 if (idx1 == 0 && idx2 == osd->od_index) {
775 fid_to_ostid(fid, ostid);
776 ostid_to_fid(tfid, ostid, idx2);
777 if (lu_fid_eq(tfid, oi_fid)) {
778 CERROR("%s: the FID "DFID" is used by "
779 "two objects(2): %u/%u %u/%u\n",
780 osd_name(osd), PFID(fid),
781 oi_id->oii_ino, oi_id->oii_gen,
782 id->oii_ino, id->oii_gen);
784 GOTO(out, rc = -EEXIST);
786 } else if (idx2 == 0 && idx1 == osd->od_index) {
787 fid_to_ostid(oi_fid, ostid);
788 ostid_to_fid(tfid, ostid, idx1);
789 if (lu_fid_eq(tfid, fid)) {
790 CERROR("%s: the FID "DFID" is used by "
791 "two objects(2): %u/%u %u/%u\n",
792 osd_name(osd), PFID(fid),
793 oi_id->oii_ino, oi_id->oii_gen,
794 id->oii_ino, id->oii_gen);
796 GOTO(out, rc = -EEXIST);
804 * There may be temporary inconsistency: On one hand, the new
805 * object may be referenced by multiple entries, which is out
806 * of our control unless we traverse the whole /O completely,
807 * which is non-flat order and inefficient, should be avoided;
808 * On the other hand, the old object may become orphan if it
809 * is still valid. Since it was referenced by an invalid entry,
810 * making it as invisible temporary may be not worse. OI scrub
811 * will process it later.
813 rc = ldiskfs_journal_get_write_access(th, bh);
817 de->inode = cpu_to_le32(id->oii_ino);
818 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
825 inode_unlock(parent);
829 static int osd_obj_del_entry(struct osd_thread_info *info,
830 struct osd_device *osd,
831 struct dentry *dird, char *name,
834 struct ldiskfs_dir_entry_2 *de;
835 struct buffer_head *bh;
836 struct dentry *child;
837 struct inode *dir = dird->d_inode;
843 LASSERT(th->h_transaction != NULL);
845 child = &info->oti_child_dentry;
846 child->d_name.hash = 0;
847 child->d_name.name = name;
848 child->d_name.len = strlen(name);
849 child->d_parent = dird;
850 child->d_inode = NULL;
854 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
858 rc = ldiskfs_delete_entry(th, dir, de, bh);
866 static int osd_obj_add_entry(struct osd_thread_info *info,
867 struct osd_device *osd,
868 struct dentry *dir, char *name,
869 const struct osd_inode_id *id,
872 struct dentry *child;
878 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
882 LASSERT(th->h_transaction != NULL);
884 inode = info->oti_inode;
885 if (unlikely(inode == NULL)) {
886 struct ldiskfs_inode_info *lii;
891 inode = info->oti_inode = &lii->vfs_inode;
894 inode->i_sb = osd_sb(osd);
895 osd_id_to_inode(inode, id);
896 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
898 child = &info->oti_child_dentry;
899 child->d_name.hash = 0;
900 child->d_name.name = name;
901 child->d_name.len = strlen(name);
902 child->d_parent = dir;
903 child->d_inode = inode;
905 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
908 ll_vfs_dq_init(dir->d_inode);
909 inode_lock(dir->d_inode);
910 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
911 inode_unlock(dir->d_inode);
917 * Use %llu for legacy OST sequences, but use %llx for new
918 * sequences names, so that the O/{seq}/dN/{oid} more closely
919 * follows the DFID/PFID format. This makes it easier to map from
920 * debug messages to objects in the future, and the legacy space
921 * of FID_SEQ_OST_MDT0 will be unused in the future.
923 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
925 snprintf(seq_name, name_size,
926 (fid_seq_is_rsvd(seq) ||
927 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
928 fid_seq_is_idif(seq) ? 0 : seq);
931 static inline void osd_oid_name(char *name, size_t name_size,
932 const struct lu_fid *fid, u64 id)
934 snprintf(name, name_size,
935 (fid_seq_is_rsvd(fid_seq(fid)) ||
936 fid_seq_is_mdt0(fid_seq(fid)) ||
937 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
940 /* external locking is required */
941 static int osd_seq_load_locked(struct osd_thread_info *info,
942 struct osd_device *osd,
943 struct osd_obj_seq *osd_seq)
945 struct osd_obj_map *map = osd->od_ost_map;
946 struct dentry *seq_dir;
953 if (osd_seq->oos_root != NULL)
957 LASSERT(map->om_root);
959 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
961 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
962 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
964 GOTO(out_err, rc = PTR_ERR(seq_dir));
965 else if (seq_dir->d_inode == NULL)
966 GOTO(out_put, rc = -EFAULT);
968 osd_seq->oos_root = seq_dir;
970 LASSERT(osd_seq->oos_dirs == NULL);
971 OBD_ALLOC(osd_seq->oos_dirs,
972 sizeof(seq_dir) * osd_seq->oos_subdir_count);
973 if (osd_seq->oos_dirs == NULL)
974 GOTO(out_put, rc = -ENOMEM);
976 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
979 snprintf(dir_name, sizeof(dir_name), "d%u", i);
980 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
981 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
984 GOTO(out_free, rc = PTR_ERR(dir));
985 } else if (dir->d_inode == NULL) {
987 GOTO(out_free, rc = -EFAULT);
990 osd_seq->oos_dirs[i] = dir;
995 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
996 if (osd_seq->oos_dirs[i] != NULL)
997 dput(osd_seq->oos_dirs[i]);
999 OBD_FREE(osd_seq->oos_dirs,
1000 sizeof(seq_dir) * osd_seq->oos_subdir_count);
1003 osd_seq->oos_root = NULL;
1009 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
1010 struct osd_device *osd, u64 seq)
1012 struct osd_obj_map *map;
1013 struct osd_obj_seq *osd_seq;
1018 map = osd->od_ost_map;
1020 LASSERT(map->om_root);
1022 osd_seq = osd_seq_find(map, seq);
1023 if (likely(osd_seq != NULL))
1026 /* Serializing init process */
1027 mutex_lock(&map->om_dir_init_mutex);
1029 /* Check whether the seq has been added */
1030 read_lock(&map->om_seq_list_lock);
1031 osd_seq = osd_seq_find_locked(map, seq);
1032 if (osd_seq != NULL) {
1033 read_unlock(&map->om_seq_list_lock);
1034 GOTO(cleanup, rc = 0);
1036 read_unlock(&map->om_seq_list_lock);
1038 OBD_ALLOC_PTR(osd_seq);
1039 if (osd_seq == NULL)
1040 GOTO(cleanup, rc = -ENOMEM);
1042 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
1043 osd_seq->oos_seq = seq;
1045 * Init subdir count to be 32, but each seq can have
1046 * different subdir count
1048 osd_seq->oos_subdir_count = map->om_subdir_count;
1049 rc = osd_seq_load_locked(info, osd, osd_seq);
1053 write_lock(&map->om_seq_list_lock);
1054 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
1055 write_unlock(&map->om_seq_list_lock);
1058 mutex_unlock(&map->om_dir_init_mutex);
1060 if (osd_seq != NULL)
1061 OBD_FREE_PTR(osd_seq);
1062 RETURN(ERR_PTR(rc));
1068 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
1069 const struct lu_fid *fid, struct osd_inode_id *id)
1071 struct osd_obj_map *map;
1072 struct osd_obj_seq *osd_seq;
1073 struct dentry *d_seq;
1074 struct dentry *child;
1075 struct ost_id *ostid = &info->oti_ostid;
1078 struct ldiskfs_dir_entry_2 *de;
1079 struct buffer_head *bh;
1081 struct inode *inode;
1085 /* on the very first lookup we find and open directories */
1086 map = dev->od_ost_map;
1088 LASSERT(map->om_root);
1090 fid_to_ostid(fid, ostid);
1091 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1092 if (IS_ERR(osd_seq))
1093 RETURN(PTR_ERR(osd_seq));
1095 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1096 d_seq = osd_seq->oos_dirs[dirn];
1099 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1101 child = &info->oti_child_dentry;
1102 child->d_parent = d_seq;
1103 child->d_name.hash = 0;
1104 child->d_name.name = name;
1105 /* XXX: we can use rc from sprintf() instead of strlen() */
1106 child->d_name.len = strlen(name);
1108 dir = d_seq->d_inode;
1110 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1114 RETURN(PTR_ERR(bh));
1116 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1119 inode = osd_iget(info, dev, id);
1120 if (IS_ERR(inode)) {
1121 int rc = PTR_ERR(inode);
1123 RETURN(rc == -ENOENT ? -ESTALE : rc);
1130 int osd_obj_map_insert(struct osd_thread_info *info,
1131 struct osd_device *osd,
1132 const struct lu_fid *fid,
1133 const struct osd_inode_id *id,
1136 struct osd_obj_map *map;
1137 struct osd_obj_seq *osd_seq;
1139 struct ost_id *ostid = &info->oti_ostid;
1146 map = osd->od_ost_map;
1149 /* map fid to seq:objid */
1150 fid_to_ostid(fid, ostid);
1152 oid = ostid_id(ostid);
1153 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1154 if (IS_ERR(osd_seq))
1155 RETURN(PTR_ERR(osd_seq));
1157 dirn = oid & (osd_seq->oos_subdir_count - 1);
1158 d = osd_seq->oos_dirs[dirn];
1161 osd_oid_name(name, sizeof(name), fid, oid);
1164 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1165 if (rc == -EEXIST) {
1166 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1167 if (unlikely(rc == -ENOENT))
1170 if (unlikely(rc == 1))
1177 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1178 const struct lu_fid *fid, handle_t *th)
1180 struct osd_obj_map *map;
1181 struct osd_obj_seq *osd_seq;
1183 struct ost_id *ostid = &info->oti_ostid;
1189 map = osd->od_ost_map;
1192 /* map fid to seq:objid */
1193 fid_to_ostid(fid, ostid);
1195 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1196 if (IS_ERR(osd_seq))
1197 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1199 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1200 d = osd_seq->oos_dirs[dirn];
1203 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1204 rc = osd_obj_del_entry(info, osd, d, name, th);
1209 int osd_obj_map_update(struct osd_thread_info *info,
1210 struct osd_device *osd,
1211 const struct lu_fid *fid,
1212 const struct osd_inode_id *id,
1215 struct osd_obj_seq *osd_seq;
1217 struct ost_id *ostid = &info->oti_ostid;
1223 fid_to_ostid(fid, ostid);
1224 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1225 if (IS_ERR(osd_seq))
1226 RETURN(PTR_ERR(osd_seq));
1228 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1229 d = osd_seq->oos_dirs[dirn];
1232 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1233 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1238 int osd_obj_map_recover(struct osd_thread_info *info,
1239 struct osd_device *osd,
1240 struct inode *src_parent,
1241 struct dentry *src_child,
1242 const struct lu_fid *fid)
1244 struct osd_obj_seq *osd_seq;
1245 struct dentry *tgt_parent;
1246 struct dentry *tgt_child = &info->oti_child_dentry;
1248 struct inode *inode = src_child->d_inode;
1249 struct ost_id *ostid = &info->oti_ostid;
1251 struct ldiskfs_dir_entry_2 *de;
1252 struct buffer_head *bh;
1259 if (fid_is_last_id(fid)) {
1260 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1261 if (IS_ERR(osd_seq))
1262 RETURN(PTR_ERR(osd_seq));
1264 tgt_parent = osd_seq->oos_root;
1265 tgt_child->d_name.name = "LAST_ID";
1266 tgt_child->d_name.len = strlen("LAST_ID");
1268 fid_to_ostid(fid, ostid);
1269 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1270 if (IS_ERR(osd_seq))
1271 RETURN(PTR_ERR(osd_seq));
1273 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1274 tgt_parent = osd_seq->oos_dirs[dirn];
1275 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1276 tgt_child->d_name.name = name;
1277 tgt_child->d_name.len = strlen(name);
1279 LASSERT(tgt_parent != NULL);
1281 dir = tgt_parent->d_inode;
1282 tgt_child->d_name.hash = 0;
1283 tgt_child->d_parent = tgt_parent;
1284 tgt_child->d_inode = inode;
1286 /* The non-initialized src_child may be destroyed. */
1287 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1288 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1289 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1290 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1292 RETURN(PTR_ERR(jh));
1294 ll_vfs_dq_init(src_parent);
1295 ll_vfs_dq_init(dir);
1297 inode_lock(src_parent);
1299 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1302 * XXX: If some other object occupied the same slot. And If such
1303 * inode is zero-sized and with SUID+SGID, then means it is
1304 * a new created one. Maybe we can remove it and insert the
1305 * original one back to the /O/<seq>/d<x>. But there are
1306 * something to be considered:
1308 * 1) The OST-object under /lost+found has crashed LMA.
1309 * So it should not conflict with the current one.
1311 * 2) There are race conditions that: someone may just want
1312 * to modify the current one. Even if the OI scrub takes
1313 * the object lock when remove the current one, it still
1314 * cause the modification to be lost becasue the target
1315 * has been removed when the RPC service thread waiting
1318 * So keep it there before we have suitable solution.
1322 inode_unlock(src_parent);
1323 ldiskfs_journal_stop(jh);
1326 /* If the src object has never been modified, then remove it. */
1327 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1328 inode->i_mode & S_ISGID) {
1329 rc = ll_vfs_unlink(src_parent, src_child);
1330 if (unlikely(rc == -ENOENT))
1337 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1339 if (unlikely(IS_ERR(bh)))
1340 GOTO(unlock, rc = PTR_ERR(bh));
1342 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1347 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1353 inode_unlock(src_parent);
1354 ldiskfs_journal_stop(jh);
1358 static struct dentry *
1359 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1360 const struct lu_fid *fid, char **name)
1362 struct dentry *root = ERR_PTR(-ENOENT);
1364 if (fid_is_last_id(fid)) {
1365 struct osd_obj_seq *osd_seq;
1367 /* on creation of LAST_ID we create O/<seq> hierarchy */
1368 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1369 if (IS_ERR(osd_seq))
1370 RETURN((struct dentry *)osd_seq);
1373 root = osd_seq->oos_root;
1375 *name = osd_lf_fid2name(fid);
1377 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1378 else if ((*name)[0])
1379 root = osd_sb(osd)->s_root;
1385 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1386 const struct lu_fid *fid, const struct osd_inode_id *id,
1389 struct dentry *root;
1395 root = osd_object_spec_find(info, osd, fid, &name);
1396 if (!IS_ERR(root)) {
1397 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1407 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1408 const struct lu_fid *fid, const struct osd_inode_id *id,
1411 struct dentry *root;
1417 root = osd_object_spec_find(info, osd, fid, &name);
1418 if (!IS_ERR(root)) {
1419 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1429 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1430 const struct lu_fid *fid, struct osd_inode_id *id)
1432 struct dentry *root;
1433 struct dentry *dentry;
1434 struct inode *inode;
1440 if (fid_is_last_id(fid)) {
1441 struct osd_obj_seq *osd_seq;
1443 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1444 if (IS_ERR(osd_seq))
1445 RETURN(PTR_ERR(osd_seq));
1446 root = osd_seq->oos_root;
1449 root = osd_sb(osd)->s_root;
1450 name = osd_lf_fid2name(fid);
1451 if (name == NULL || strlen(name) == 0)
1455 dentry = osd_lookup_one_len_unlocked(name, root, strlen(name));
1456 if (!IS_ERR(dentry)) {
1457 inode = dentry->d_inode;
1459 if (is_bad_inode(inode)) {
1462 osd_id_gen(id, inode->i_ino,
1463 inode->i_generation);
1468 * if dentry is accessible after osd_compat_spec_insert it
1469 * will still contain NULL inode, so don't keep it in cache
1471 d_invalidate(dentry);
1478 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1479 bool bio_integrity_enabled(struct bio *bio)
1481 struct blk_integrity *bi = blk_get_integrity(bio_get_disk(bio));
1483 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1486 if (!bio_sectors(bio))
1489 /* Already protected? */
1490 if (bio_integrity(bio))
1496 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1497 (bi->flags & BLK_INTEGRITY_VERIFY))
1500 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1501 (bi->flags & BLK_INTEGRITY_GENERATE))