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 struct dentry *osd_lookup_one_len_unlocked(const char *name,
80 struct dentry *base, int len)
82 struct dentry *dchild;
84 inode_lock(base->d_inode);
85 dchild = lookup_one_len(name, base, len);
86 inode_unlock(base->d_inode);
88 if (IS_ERR(dchild) || dchild->d_inode == NULL)
91 if (is_bad_inode(dchild->d_inode)) {
92 CERROR("bad inode returned %lu/%u\n",
93 dchild->d_inode->i_ino, dchild->d_inode->i_generation);
95 dchild = ERR_PTR(-ENOENT);
102 * osd_ios_lookup_one_len - lookup single pathname component
104 * @name: pathname component to lookup
105 * @base: base directory to lookup from
106 * @len: maximum length @len should be interpreted to
108 struct dentry *osd_ios_lookup_one_len(const char *name, struct dentry *base,
111 struct dentry *dentry;
113 dentry = osd_lookup_one_len_unlocked(name, base, len);
114 if (IS_ERR(dentry)) {
115 int rc = PTR_ERR(dentry);
118 CERROR("Fail to find %.*s in %.*s (%lu/%u): rc = %d\n",
119 len, name, base->d_name.len,
120 base->d_name.name, base->d_inode->i_ino,
121 base->d_inode->i_generation, rc);
129 /* utility to make a directory */
130 static struct dentry *
131 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
132 struct dentry *dir, const struct lu_fid *fid,
133 const char *name, __u32 compat, int mode, bool *created)
135 struct osd_thread_info *info = osd_oti_get(env);
136 struct lu_fid *tfid = &info->oti_fid3;
138 struct dentry *dchild;
143 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
144 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
145 dchild = osd_lookup_one_len_unlocked(name, dir, strlen(name));
149 inode = dchild->d_inode;
151 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
152 int old_mode = inode->i_mode;
157 if (!S_ISDIR(old_mode)) {
158 CERROR("found %s (%lu/%u) is mode %o\n", name,
159 inode->i_ino, inode->i_generation, old_mode);
160 GOTO(out_err, err = -ENOTDIR);
163 if (unlikely(osd->od_dt_dev.dd_rdonly))
166 /* Fixup directory permissions if necessary */
167 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
169 "fixing permissions on %s from %o to %o\n",
170 name, old_mode, mode);
171 inode->i_mode = (mode & S_IALLUGO) |
172 (old_mode & ~S_IALLUGO);
173 mark_inode_dirty(inode);
176 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
177 &info->oti_ost_attrs);
184 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
185 lma->lma_compat != compat)
191 err = vfs_mkdir(dir->d_inode, dchild, mode);
195 inode = dchild->d_inode;
203 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
204 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
215 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
217 struct lr_server_data lsd;
218 struct dentry *dlast;
221 int count = OBJ_SUBDIR_COUNT;
225 dlast = osd_lookup_one_len_unlocked(LAST_RCVD, osd_sb(osd)->s_root,
228 return PTR_ERR(dlast);
229 else if (dlast->d_inode == NULL)
233 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
234 if (rc == sizeof(lsd)) {
236 "read last_rcvd header, uuid = %s, subdir count = %d\n",
237 lsd.lsd_uuid, lsd.lsd_subdir_count);
238 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
239 count = le16_to_cpu(lsd.lsd_subdir_count);
240 } else if (rc != 0) {
241 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
253 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
255 struct lvfs_run_ctxt new;
256 struct lvfs_run_ctxt save;
257 struct dentry *parent;
258 struct osd_mdobj_map *omm;
260 struct osd_thread_info *info = osd_oti_get(env);
261 struct lu_fid *fid = &info->oti_fid3;
266 OBD_ALLOC_PTR(dev->od_mdt_map);
267 if (dev->od_mdt_map == NULL)
270 omm = dev->od_mdt_map;
272 parent = osd_sb(dev)->s_root;
273 osd_push_ctxt(dev, &new, &save);
275 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
276 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
277 LMAC_NOT_IN_OI, 0755, NULL);
279 GOTO(cleanup, rc = PTR_ERR(d));
281 omm->omm_remote_parent = d;
283 GOTO(cleanup, rc = 0);
286 pop_ctxt(&save, &new);
288 if (omm->omm_remote_parent != NULL)
289 dput(omm->omm_remote_parent);
291 dev->od_mdt_map = NULL;
296 static void osd_mdt_fini(struct osd_device *osd)
298 struct osd_mdobj_map *omm = osd->od_mdt_map;
303 if (omm->omm_remote_parent)
304 dput(omm->omm_remote_parent);
307 osd->od_ost_map = NULL;
310 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
311 struct osd_object *obj, struct osd_thandle *oh)
313 struct osd_mdobj_map *omm = osd->od_mdt_map;
314 struct osd_thread_info *oti = osd_oti_get(env);
315 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
316 char *name = oti->oti_name;
317 struct osd_thread_info *info = osd_oti_get(env);
318 struct dentry *dentry;
319 struct dentry *parent;
322 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
326 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
327 * can identify this object quickly
329 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
330 &oti->oti_ost_attrs);
334 lma->lma_incompat |= LMAI_REMOTE_PARENT;
335 lustre_lma_swab(lma);
336 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
337 sizeof(*lma), XATTR_REPLACE);
341 parent = omm->omm_remote_parent;
342 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
343 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
345 inode_lock(parent->d_inode);
346 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
347 obj->oo_inode, NULL);
348 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
349 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
350 else if (unlikely(rc == -EEXIST))
353 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
354 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
355 osd_name(osd), name, rc);
356 mark_inode_dirty(parent->d_inode);
357 inode_unlock(parent->d_inode);
361 int osd_delete_from_remote_parent(const struct lu_env *env,
362 struct osd_device *osd,
363 struct osd_object *obj,
364 struct osd_thandle *oh, bool destroy)
366 struct osd_mdobj_map *omm = osd->od_mdt_map;
367 struct osd_thread_info *oti = osd_oti_get(env);
368 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
369 char *name = oti->oti_name;
370 struct dentry *dentry;
371 struct dentry *parent;
372 struct ldiskfs_dir_entry_2 *de;
373 struct buffer_head *bh;
376 parent = omm->omm_remote_parent;
377 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
378 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
380 inode_lock(parent->d_inode);
381 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
384 inode_unlock(parent->d_inode);
386 if (unlikely(rc == -ENOENT))
389 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
391 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
392 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
393 mark_inode_dirty(parent->d_inode);
394 inode_unlock(parent->d_inode);
396 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
397 osd_name(osd), name, rc);
402 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
407 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
408 &oti->oti_ost_attrs);
412 /* Get rid of REMOTE_PARENT flag from incompat */
413 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
414 lustre_lma_swab(lma);
415 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
416 sizeof(*lma), XATTR_REPLACE);
418 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
422 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
423 struct osd_device *osd,
424 const struct lu_fid *fid,
425 struct osd_inode_id *id)
427 struct osd_mdobj_map *omm = osd->od_mdt_map;
428 char *name = oti->oti_name;
429 struct dentry *parent;
430 struct dentry *dentry;
431 struct ldiskfs_dir_entry_2 *de;
432 struct buffer_head *bh;
437 if (unlikely(osd->od_is_ost))
440 parent = omm->omm_remote_parent;
441 sprintf(name, DFID_NOBRACE, PFID(fid));
442 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
444 inode_lock(parent->d_inode);
445 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
452 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
454 inode = osd_iget(oti, osd, id);
464 inode_unlock(parent->d_inode);
466 osd_add_oi_cache(oti, osd, id, fid);
471 * directory structure on legacy OST:
473 * O/<seq>/d0-31/<objid>
480 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
482 struct lvfs_run_ctxt new;
483 struct lvfs_run_ctxt save;
486 bool created = false;
490 OBD_ALLOC_PTR(dev->od_ost_map);
491 if (dev->od_ost_map == NULL)
494 /* to get subdir count from last_rcvd */
495 rc = osd_last_rcvd_subdir_count(dev);
497 GOTO(cleanup_alloc, rc);
499 dev->od_ost_map->om_subdir_count = rc;
500 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
501 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
502 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
504 osd_push_ctxt(dev, &new, &save);
505 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
506 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
508 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
511 /* It is quite probably that the device is new formatted. */
512 dev->od_maybe_new = 1;
514 dev->od_ost_map->om_root = d;
516 pop_ctxt(&save, &new);
520 pop_ctxt(&save, &new);
522 OBD_FREE_PTR(dev->od_ost_map);
526 static void osd_seq_free(struct osd_obj_seq *osd_seq)
530 if (osd_seq->oos_dirs) {
531 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
532 if (osd_seq->oos_dirs[j])
533 dput(osd_seq->oos_dirs[j]);
535 OBD_FREE(osd_seq->oos_dirs,
536 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
539 if (osd_seq->oos_root)
540 dput(osd_seq->oos_root);
542 OBD_FREE_PTR(osd_seq);
545 static void osd_ost_fini(struct osd_device *osd)
547 struct osd_obj_seq *osd_seq;
548 struct osd_obj_seq *tmp;
549 struct osd_obj_map *map = osd->od_ost_map;
556 write_lock(&map->om_seq_list_lock);
557 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
559 list_del_init(&osd_seq->oos_seq_list);
560 write_unlock(&map->om_seq_list_lock);
561 osd_seq_free(osd_seq);
562 write_lock(&map->om_seq_list_lock);
564 write_unlock(&map->om_seq_list_lock);
568 osd->od_ost_map = NULL;
572 static int osd_index_backup_dir_init(const struct lu_env *env,
573 struct osd_device *dev)
575 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
576 struct lvfs_run_ctxt new;
577 struct lvfs_run_ctxt save;
578 struct dentry *dentry;
583 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
584 osd_push_ctxt(dev, &new, &save);
585 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
586 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
587 if (IS_ERR(dentry)) {
588 rc = PTR_ERR(dentry);
590 dev->od_index_backup_inode = igrab(dentry->d_inode);
593 pop_ctxt(&save, &new);
598 static void osd_index_backup_dir_fini(struct osd_device *dev)
600 if (dev->od_index_backup_inode) {
601 iput(dev->od_index_backup_inode);
602 dev->od_index_backup_inode = NULL;
606 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
609 bool mdt_init = false;
613 rc = osd_ost_init(env, dev);
617 if (!dev->od_is_ost) {
618 rc = osd_mdt_init(env, dev);
627 rc = osd_index_backup_dir_init(env, dev);
637 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
639 struct osd_obj_seq *osd_seq;
641 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
642 if (osd_seq->oos_seq == seq)
648 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
650 struct osd_obj_seq *osd_seq;
652 read_lock(&map->om_seq_list_lock);
653 osd_seq = osd_seq_find_locked(map, seq);
654 read_unlock(&map->om_seq_list_lock);
658 void osd_obj_map_fini(struct osd_device *dev)
660 osd_index_backup_dir_fini(dev);
666 * Update the specified OI mapping.
668 * \retval 1, changed nothing
669 * \retval 0, changed successfully
670 * \retval -ve, on error
672 static int osd_obj_update_entry(struct osd_thread_info *info,
673 struct osd_device *osd,
674 struct dentry *dir, const char *name,
675 const struct lu_fid *fid,
676 const struct osd_inode_id *id,
679 struct inode *parent = dir->d_inode;
680 struct dentry *child;
681 struct ldiskfs_dir_entry_2 *de;
682 struct buffer_head *bh;
684 struct dentry *dentry = &info->oti_obj_dentry;
685 struct osd_inode_id *oi_id = &info->oti_id3;
686 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
687 struct lu_fid *oi_fid = &lma->lma_self_fid;
693 LASSERT(th->h_transaction != NULL);
695 child = &info->oti_child_dentry;
696 child->d_parent = dir;
697 child->d_name.hash = 0;
698 child->d_name.name = name;
699 child->d_name.len = strlen(name);
701 dquot_initialize(parent);
703 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
705 GOTO(out, rc = PTR_ERR(bh));
707 if (le32_to_cpu(de->inode) == id->oii_ino)
710 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
711 inode = osd_iget(info, osd, oi_id);
714 if (rc == -ENOENT || rc == -ESTALE)
720 * The EA inode should NOT be in OI, old OI scrub may added
721 * such OI mapping by wrong, replace it.
723 if (unlikely(osd_is_ea_inode(inode))) {
728 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
729 if (rc == -ENODATA) {
730 rc = osd_get_idif(info, inode, dentry, oi_fid);
731 if (rc > 0 || rc == -ENODATA) {
742 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
743 * either a crashed object or a uninitialized one. Replace it.
745 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
746 CERROR("%s: the FID "DFID" is used by two objects: "
747 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
748 oi_id->oii_ino, oi_id->oii_gen,
749 id->oii_ino, id->oii_gen);
750 GOTO(out, rc = -EEXIST);
753 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
754 __u32 idx1 = fid_idif_ost_idx(fid);
755 __u32 idx2 = fid_idif_ost_idx(oi_fid);
756 struct ost_id *ostid = &info->oti_ostid;
757 struct lu_fid *tfid = &info->oti_fid3;
759 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
760 "invalid given FID "DFID", not match the "
761 "device index %u\n", PFID(fid), osd->od_index);
764 if (idx1 == 0 && idx2 == osd->od_index) {
765 fid_to_ostid(fid, ostid);
766 ostid_to_fid(tfid, ostid, idx2);
767 if (lu_fid_eq(tfid, oi_fid)) {
768 CERROR("%s: the FID "DFID" is used by "
769 "two objects(2): %u/%u %u/%u\n",
770 osd_name(osd), PFID(fid),
771 oi_id->oii_ino, oi_id->oii_gen,
772 id->oii_ino, id->oii_gen);
774 GOTO(out, rc = -EEXIST);
776 } else if (idx2 == 0 && idx1 == osd->od_index) {
777 fid_to_ostid(oi_fid, ostid);
778 ostid_to_fid(tfid, ostid, idx1);
779 if (lu_fid_eq(tfid, fid)) {
780 CERROR("%s: the FID "DFID" is used by "
781 "two objects(2): %u/%u %u/%u\n",
782 osd_name(osd), PFID(fid),
783 oi_id->oii_ino, oi_id->oii_gen,
784 id->oii_ino, id->oii_gen);
786 GOTO(out, rc = -EEXIST);
794 * There may be temporary inconsistency: On one hand, the new
795 * object may be referenced by multiple entries, which is out
796 * of our control unless we traverse the whole /O completely,
797 * which is non-flat order and inefficient, should be avoided;
798 * On the other hand, the old object may become orphan if it
799 * is still valid. Since it was referenced by an invalid entry,
800 * making it as invisible temporary may be not worse. OI scrub
801 * will process it later.
803 rc = ldiskfs_journal_get_write_access(th, bh);
807 de->inode = cpu_to_le32(id->oii_ino);
808 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
815 inode_unlock(parent);
819 static int osd_obj_del_entry(struct osd_thread_info *info,
820 struct osd_device *osd,
821 struct dentry *dird, char *name,
824 struct ldiskfs_dir_entry_2 *de;
825 struct buffer_head *bh;
826 struct dentry *child;
827 struct inode *dir = dird->d_inode;
833 LASSERT(th->h_transaction != NULL);
835 child = &info->oti_child_dentry;
836 child->d_name.hash = 0;
837 child->d_name.name = name;
838 child->d_name.len = strlen(name);
839 child->d_parent = dird;
840 child->d_inode = NULL;
842 dquot_initialize(dir);
844 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
848 rc = ldiskfs_delete_entry(th, dir, de, bh);
856 static int osd_obj_add_entry(struct osd_thread_info *info,
857 struct osd_device *osd,
858 struct dentry *dir, char *name,
859 const struct osd_inode_id *id,
862 struct dentry *child;
868 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
872 LASSERT(th->h_transaction != NULL);
874 inode = info->oti_inode;
875 if (unlikely(inode == NULL)) {
876 struct ldiskfs_inode_info *lii;
881 inode = info->oti_inode = &lii->vfs_inode;
884 inode->i_sb = osd_sb(osd);
885 osd_id_to_inode(inode, id);
886 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
888 child = &info->oti_child_dentry;
889 child->d_name.hash = 0;
890 child->d_name.name = name;
891 child->d_name.len = strlen(name);
892 child->d_parent = dir;
893 child->d_inode = inode;
895 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
898 dquot_initialize(dir->d_inode);
899 inode_lock(dir->d_inode);
900 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
901 inode_unlock(dir->d_inode);
907 * Use %llu for legacy OST sequences, but use %llx for new
908 * sequences names, so that the O/{seq}/dN/{oid} more closely
909 * follows the DFID/PFID format. This makes it easier to map from
910 * debug messages to objects in the future, and the legacy space
911 * of FID_SEQ_OST_MDT0 will be unused in the future.
913 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
915 snprintf(seq_name, name_size,
916 (fid_seq_is_rsvd(seq) ||
917 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
918 fid_seq_is_idif(seq) ? 0 : seq);
921 static inline void osd_oid_name(char *name, size_t name_size,
922 const struct lu_fid *fid, u64 id)
924 snprintf(name, name_size,
925 (fid_seq_is_rsvd(fid_seq(fid)) ||
926 fid_seq_is_mdt0(fid_seq(fid)) ||
927 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
930 /* external locking is required */
931 static int osd_seq_load_locked(struct osd_thread_info *info,
932 struct osd_device *osd,
933 struct osd_obj_seq *osd_seq)
935 struct osd_obj_map *map = osd->od_ost_map;
936 struct dentry *seq_dir;
943 if (osd_seq->oos_root != NULL)
947 LASSERT(map->om_root);
949 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
951 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
952 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
954 GOTO(out_err, rc = PTR_ERR(seq_dir));
955 else if (seq_dir->d_inode == NULL)
956 GOTO(out_put, rc = -EFAULT);
958 osd_seq->oos_root = seq_dir;
960 LASSERT(osd_seq->oos_dirs == NULL);
961 OBD_ALLOC(osd_seq->oos_dirs,
962 sizeof(seq_dir) * osd_seq->oos_subdir_count);
963 if (osd_seq->oos_dirs == NULL)
964 GOTO(out_put, rc = -ENOMEM);
966 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
969 snprintf(dir_name, sizeof(dir_name), "d%u", i);
970 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
971 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
974 GOTO(out_free, rc = PTR_ERR(dir));
975 } else if (dir->d_inode == NULL) {
977 GOTO(out_free, rc = -EFAULT);
980 osd_seq->oos_dirs[i] = dir;
985 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
986 if (osd_seq->oos_dirs[i] != NULL)
987 dput(osd_seq->oos_dirs[i]);
989 OBD_FREE(osd_seq->oos_dirs,
990 sizeof(seq_dir) * osd_seq->oos_subdir_count);
993 osd_seq->oos_root = NULL;
999 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
1000 struct osd_device *osd, u64 seq)
1002 struct osd_obj_map *map;
1003 struct osd_obj_seq *osd_seq;
1008 map = osd->od_ost_map;
1010 LASSERT(map->om_root);
1012 osd_seq = osd_seq_find(map, seq);
1013 if (likely(osd_seq != NULL))
1016 /* Serializing init process */
1017 mutex_lock(&map->om_dir_init_mutex);
1019 /* Check whether the seq has been added */
1020 read_lock(&map->om_seq_list_lock);
1021 osd_seq = osd_seq_find_locked(map, seq);
1022 if (osd_seq != NULL) {
1023 read_unlock(&map->om_seq_list_lock);
1024 GOTO(cleanup, rc = 0);
1026 read_unlock(&map->om_seq_list_lock);
1028 OBD_ALLOC_PTR(osd_seq);
1029 if (osd_seq == NULL)
1030 GOTO(cleanup, rc = -ENOMEM);
1032 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
1033 osd_seq->oos_seq = seq;
1035 * Init subdir count to be 32, but each seq can have
1036 * different subdir count
1038 osd_seq->oos_subdir_count = map->om_subdir_count;
1039 rc = osd_seq_load_locked(info, osd, osd_seq);
1043 write_lock(&map->om_seq_list_lock);
1044 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
1045 write_unlock(&map->om_seq_list_lock);
1048 mutex_unlock(&map->om_dir_init_mutex);
1050 if (osd_seq != NULL)
1051 OBD_FREE_PTR(osd_seq);
1052 RETURN(ERR_PTR(rc));
1058 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
1059 const struct lu_fid *fid, struct osd_inode_id *id)
1061 struct osd_obj_map *map;
1062 struct osd_obj_seq *osd_seq;
1063 struct dentry *d_seq;
1064 struct dentry *child;
1065 struct ost_id *ostid = &info->oti_ostid;
1068 struct ldiskfs_dir_entry_2 *de;
1069 struct buffer_head *bh;
1071 struct inode *inode;
1075 /* on the very first lookup we find and open directories */
1076 map = dev->od_ost_map;
1078 LASSERT(map->om_root);
1080 fid_to_ostid(fid, ostid);
1081 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1082 if (IS_ERR(osd_seq))
1083 RETURN(PTR_ERR(osd_seq));
1085 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1086 d_seq = osd_seq->oos_dirs[dirn];
1089 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1091 child = &info->oti_child_dentry;
1092 child->d_parent = d_seq;
1093 child->d_name.hash = 0;
1094 child->d_name.name = name;
1095 /* XXX: we can use rc from sprintf() instead of strlen() */
1096 child->d_name.len = strlen(name);
1098 dir = d_seq->d_inode;
1100 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1104 RETURN(PTR_ERR(bh));
1106 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1109 inode = osd_iget(info, dev, id);
1110 if (IS_ERR(inode)) {
1111 int rc = PTR_ERR(inode);
1113 RETURN(rc == -ENOENT ? -ESTALE : rc);
1120 int osd_obj_map_insert(struct osd_thread_info *info,
1121 struct osd_device *osd,
1122 const struct lu_fid *fid,
1123 const struct osd_inode_id *id,
1126 struct osd_obj_map *map;
1127 struct osd_obj_seq *osd_seq;
1129 struct ost_id *ostid = &info->oti_ostid;
1136 map = osd->od_ost_map;
1139 /* map fid to seq:objid */
1140 fid_to_ostid(fid, ostid);
1142 oid = ostid_id(ostid);
1143 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1144 if (IS_ERR(osd_seq))
1145 RETURN(PTR_ERR(osd_seq));
1147 dirn = oid & (osd_seq->oos_subdir_count - 1);
1148 d = osd_seq->oos_dirs[dirn];
1151 osd_oid_name(name, sizeof(name), fid, oid);
1154 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1155 if (rc == -EEXIST) {
1156 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1157 if (unlikely(rc == -ENOENT))
1160 if (unlikely(rc == 1))
1167 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1168 const struct lu_fid *fid, handle_t *th)
1170 struct osd_obj_map *map;
1171 struct osd_obj_seq *osd_seq;
1173 struct ost_id *ostid = &info->oti_ostid;
1179 map = osd->od_ost_map;
1182 /* map fid to seq:objid */
1183 fid_to_ostid(fid, ostid);
1185 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1186 if (IS_ERR(osd_seq))
1187 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1189 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1190 d = osd_seq->oos_dirs[dirn];
1193 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1194 rc = osd_obj_del_entry(info, osd, d, name, th);
1199 int osd_obj_map_update(struct osd_thread_info *info,
1200 struct osd_device *osd,
1201 const struct lu_fid *fid,
1202 const struct osd_inode_id *id,
1205 struct osd_obj_seq *osd_seq;
1207 struct ost_id *ostid = &info->oti_ostid;
1213 fid_to_ostid(fid, ostid);
1214 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1215 if (IS_ERR(osd_seq))
1216 RETURN(PTR_ERR(osd_seq));
1218 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1219 d = osd_seq->oos_dirs[dirn];
1222 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1223 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1228 int osd_obj_map_recover(struct osd_thread_info *info,
1229 struct osd_device *osd,
1230 struct inode *src_parent,
1231 struct dentry *src_child,
1232 const struct lu_fid *fid)
1234 struct osd_obj_seq *osd_seq;
1235 struct dentry *tgt_parent;
1236 struct dentry *tgt_child = &info->oti_child_dentry;
1238 struct inode *inode = src_child->d_inode;
1239 struct ost_id *ostid = &info->oti_ostid;
1241 struct ldiskfs_dir_entry_2 *de;
1242 struct buffer_head *bh;
1249 if (fid_is_last_id(fid)) {
1250 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1251 if (IS_ERR(osd_seq))
1252 RETURN(PTR_ERR(osd_seq));
1254 tgt_parent = osd_seq->oos_root;
1255 tgt_child->d_name.name = "LAST_ID";
1256 tgt_child->d_name.len = strlen("LAST_ID");
1258 fid_to_ostid(fid, ostid);
1259 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1260 if (IS_ERR(osd_seq))
1261 RETURN(PTR_ERR(osd_seq));
1263 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1264 tgt_parent = osd_seq->oos_dirs[dirn];
1265 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1266 tgt_child->d_name.name = name;
1267 tgt_child->d_name.len = strlen(name);
1269 LASSERT(tgt_parent != NULL);
1271 dir = tgt_parent->d_inode;
1272 tgt_child->d_name.hash = 0;
1273 tgt_child->d_parent = tgt_parent;
1274 tgt_child->d_inode = inode;
1276 /* The non-initialized src_child may be destroyed. */
1277 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1278 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1279 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1280 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1282 RETURN(PTR_ERR(jh));
1284 dquot_initialize(src_parent);
1285 dquot_initialize(dir);
1287 inode_lock(src_parent);
1289 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1292 * XXX: If some other object occupied the same slot. And If such
1293 * inode is zero-sized and with SUID+SGID, then means it is
1294 * a new created one. Maybe we can remove it and insert the
1295 * original one back to the /O/<seq>/d<x>. But there are
1296 * something to be considered:
1298 * 1) The OST-object under /lost+found has crashed LMA.
1299 * So it should not conflict with the current one.
1301 * 2) There are race conditions that: someone may just want
1302 * to modify the current one. Even if the OI scrub takes
1303 * the object lock when remove the current one, it still
1304 * cause the modification to be lost becasue the target
1305 * has been removed when the RPC service thread waiting
1308 * So keep it there before we have suitable solution.
1312 inode_unlock(src_parent);
1313 ldiskfs_journal_stop(jh);
1316 /* If the src object has never been modified, then remove it. */
1317 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1318 inode->i_mode & S_ISGID) {
1319 rc = ll_vfs_unlink(src_parent, src_child);
1320 if (unlikely(rc == -ENOENT))
1327 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1329 if (unlikely(IS_ERR(bh)))
1330 GOTO(unlock, rc = PTR_ERR(bh));
1332 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1337 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1343 inode_unlock(src_parent);
1344 ldiskfs_journal_stop(jh);
1348 static struct dentry *
1349 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1350 const struct lu_fid *fid, char **name)
1352 struct dentry *root = ERR_PTR(-ENOENT);
1354 if (fid_is_last_id(fid)) {
1355 struct osd_obj_seq *osd_seq;
1357 /* on creation of LAST_ID we create O/<seq> hierarchy */
1358 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1359 if (IS_ERR(osd_seq))
1360 RETURN((struct dentry *)osd_seq);
1363 root = osd_seq->oos_root;
1365 *name = osd_lf_fid2name(fid);
1367 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1368 else if ((*name)[0])
1369 root = osd_sb(osd)->s_root;
1375 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1376 const struct lu_fid *fid, const struct osd_inode_id *id,
1379 struct dentry *root;
1385 root = osd_object_spec_find(info, osd, fid, &name);
1386 if (!IS_ERR(root)) {
1387 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1397 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1398 const struct lu_fid *fid, const struct osd_inode_id *id,
1401 struct dentry *root;
1407 root = osd_object_spec_find(info, osd, fid, &name);
1408 if (!IS_ERR(root)) {
1409 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1419 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1420 const struct lu_fid *fid, struct osd_inode_id *id)
1422 struct dentry *root;
1423 struct dentry *dentry;
1424 struct inode *inode;
1430 if (fid_is_last_id(fid)) {
1431 struct osd_obj_seq *osd_seq;
1433 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1434 if (IS_ERR(osd_seq))
1435 RETURN(PTR_ERR(osd_seq));
1436 root = osd_seq->oos_root;
1439 root = osd_sb(osd)->s_root;
1440 name = osd_lf_fid2name(fid);
1441 if (name == NULL || strlen(name) == 0)
1445 dentry = osd_lookup_one_len_unlocked(name, root, strlen(name));
1446 if (!IS_ERR(dentry)) {
1447 inode = dentry->d_inode;
1449 if (is_bad_inode(inode)) {
1452 osd_id_gen(id, inode->i_ino,
1453 inode->i_generation);
1458 * if dentry is accessible after osd_compat_spec_insert it
1459 * will still contain NULL inode, so don't keep it in cache
1461 d_invalidate(dentry);
1468 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1469 bool bio_integrity_enabled(struct bio *bio)
1471 struct blk_integrity *bi = blk_get_integrity(bio_get_disk(bio));
1473 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1476 if (!bio_sectors(bio))
1479 /* Already protected? */
1480 if (bio_integrity(bio))
1486 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1487 (bi->flags & BLK_INTEGRITY_VERIFY))
1490 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1491 (bi->flags & BLK_INTEGRITY_GENERATE))