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);
91 if (dchild->d_inode && unlikely(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 return osd_lookup_one_len_unlocked(name, base, len);
114 /* utility to make a directory */
115 static struct dentry *
116 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
117 struct dentry *dir, const struct lu_fid *fid,
118 const char *name, __u32 compat, int mode, bool *created)
120 struct osd_thread_info *info = osd_oti_get(env);
121 struct lu_fid *tfid = &info->oti_fid3;
123 struct dentry *dchild;
128 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
129 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
130 dchild = osd_lookup_one_len_unlocked(name, dir, strlen(name));
134 inode = dchild->d_inode;
136 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
137 int old_mode = inode->i_mode;
142 if (!S_ISDIR(old_mode)) {
143 CERROR("found %s (%lu/%u) is mode %o\n", name,
144 inode->i_ino, inode->i_generation, old_mode);
145 GOTO(out_err, err = -ENOTDIR);
148 if (unlikely(osd->od_dt_dev.dd_rdonly))
151 /* Fixup directory permissions if necessary */
152 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
154 "fixing permissions on %s from %o to %o\n",
155 name, old_mode, mode);
156 inode->i_mode = (mode & S_IALLUGO) |
157 (old_mode & ~S_IALLUGO);
158 mark_inode_dirty(inode);
161 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
162 &info->oti_ost_attrs);
169 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
170 lma->lma_compat != compat)
176 err = vfs_mkdir(dir->d_inode, dchild, mode);
180 inode = dchild->d_inode;
188 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
189 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
200 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
202 struct lr_server_data lsd;
203 struct dentry *dlast;
206 int count = OBJ_SUBDIR_COUNT;
210 dlast = osd_lookup_one_len_unlocked(LAST_RCVD, osd_sb(osd)->s_root,
213 return PTR_ERR(dlast);
214 else if (dlast->d_inode == NULL)
218 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
219 if (rc == sizeof(lsd)) {
221 "read last_rcvd header, uuid = %s, subdir count = %d\n",
222 lsd.lsd_uuid, lsd.lsd_subdir_count);
223 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
224 count = le16_to_cpu(lsd.lsd_subdir_count);
225 } else if (rc != 0) {
226 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
238 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
240 struct lvfs_run_ctxt new;
241 struct lvfs_run_ctxt save;
242 struct dentry *parent;
243 struct osd_mdobj_map *omm;
245 struct osd_thread_info *info = osd_oti_get(env);
246 struct lu_fid *fid = &info->oti_fid3;
251 OBD_ALLOC_PTR(dev->od_mdt_map);
252 if (dev->od_mdt_map == NULL)
255 omm = dev->od_mdt_map;
257 parent = osd_sb(dev)->s_root;
258 osd_push_ctxt(dev, &new, &save);
260 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
261 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
262 LMAC_NOT_IN_OI, 0755, NULL);
264 GOTO(cleanup, rc = PTR_ERR(d));
266 omm->omm_remote_parent = d;
268 GOTO(cleanup, rc = 0);
271 pop_ctxt(&save, &new);
273 if (omm->omm_remote_parent != NULL)
274 dput(omm->omm_remote_parent);
276 dev->od_mdt_map = NULL;
281 static void osd_mdt_fini(struct osd_device *osd)
283 struct osd_mdobj_map *omm = osd->od_mdt_map;
288 if (omm->omm_remote_parent)
289 dput(omm->omm_remote_parent);
292 osd->od_ost_map = NULL;
295 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
296 struct osd_object *obj, struct osd_thandle *oh)
298 struct osd_mdobj_map *omm = osd->od_mdt_map;
299 struct osd_thread_info *oti = osd_oti_get(env);
300 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
301 char *name = oti->oti_name;
302 struct osd_thread_info *info = osd_oti_get(env);
303 struct dentry *dentry;
304 struct dentry *parent;
307 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
311 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
312 * can identify this object quickly
314 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
315 &oti->oti_ost_attrs);
319 lma->lma_incompat |= LMAI_REMOTE_PARENT;
320 lustre_lma_swab(lma);
321 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
322 sizeof(*lma), XATTR_REPLACE);
326 parent = omm->omm_remote_parent;
327 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
328 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
330 inode_lock(parent->d_inode);
331 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
332 obj->oo_inode, NULL);
333 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
334 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
335 else if (unlikely(rc == -EEXIST))
338 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
339 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
340 osd_name(osd), name, rc);
341 mark_inode_dirty(parent->d_inode);
342 inode_unlock(parent->d_inode);
346 int osd_delete_from_remote_parent(const struct lu_env *env,
347 struct osd_device *osd,
348 struct osd_object *obj,
349 struct osd_thandle *oh, bool destroy)
351 struct osd_mdobj_map *omm = osd->od_mdt_map;
352 struct osd_thread_info *oti = osd_oti_get(env);
353 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
354 char *name = oti->oti_name;
355 struct dentry *dentry;
356 struct dentry *parent;
357 struct ldiskfs_dir_entry_2 *de;
358 struct buffer_head *bh;
361 parent = omm->omm_remote_parent;
362 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
363 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
365 inode_lock(parent->d_inode);
366 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
369 inode_unlock(parent->d_inode);
371 if (unlikely(rc == -ENOENT))
374 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
376 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
377 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
378 mark_inode_dirty(parent->d_inode);
379 inode_unlock(parent->d_inode);
381 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
382 osd_name(osd), name, rc);
387 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
392 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
393 &oti->oti_ost_attrs);
397 /* Get rid of REMOTE_PARENT flag from incompat */
398 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
399 lustre_lma_swab(lma);
400 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
401 sizeof(*lma), XATTR_REPLACE);
403 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
407 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
408 struct osd_device *osd,
409 const struct lu_fid *fid,
410 struct osd_inode_id *id)
412 struct osd_mdobj_map *omm = osd->od_mdt_map;
413 char *name = oti->oti_name;
414 struct dentry *parent;
415 struct dentry *dentry;
416 struct ldiskfs_dir_entry_2 *de;
417 struct buffer_head *bh;
422 if (unlikely(osd->od_is_ost))
425 parent = omm->omm_remote_parent;
426 sprintf(name, DFID_NOBRACE, PFID(fid));
427 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
429 inode_lock(parent->d_inode);
430 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
437 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
439 inode = osd_iget(oti, osd, id);
449 inode_unlock(parent->d_inode);
451 osd_add_oi_cache(oti, osd, id, fid);
456 * directory structure on legacy OST:
458 * O/<seq>/d0-31/<objid>
465 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
467 struct lvfs_run_ctxt new;
468 struct lvfs_run_ctxt save;
471 bool created = false;
475 OBD_ALLOC_PTR(dev->od_ost_map);
476 if (dev->od_ost_map == NULL)
479 /* to get subdir count from last_rcvd */
480 rc = osd_last_rcvd_subdir_count(dev);
482 GOTO(cleanup_alloc, rc);
484 dev->od_ost_map->om_subdir_count = rc;
485 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
486 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
487 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
489 osd_push_ctxt(dev, &new, &save);
490 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
491 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
493 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
496 /* It is quite probably that the device is new formatted. */
497 dev->od_maybe_new = 1;
499 dev->od_ost_map->om_root = d;
501 pop_ctxt(&save, &new);
505 pop_ctxt(&save, &new);
507 OBD_FREE_PTR(dev->od_ost_map);
511 static void osd_seq_free(struct osd_obj_seq *osd_seq)
515 if (osd_seq->oos_dirs) {
516 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
517 if (osd_seq->oos_dirs[j])
518 dput(osd_seq->oos_dirs[j]);
520 OBD_FREE(osd_seq->oos_dirs,
521 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
524 if (osd_seq->oos_root)
525 dput(osd_seq->oos_root);
527 OBD_FREE_PTR(osd_seq);
530 static void osd_ost_fini(struct osd_device *osd)
532 struct osd_obj_seq *osd_seq;
533 struct osd_obj_seq *tmp;
534 struct osd_obj_map *map = osd->od_ost_map;
541 write_lock(&map->om_seq_list_lock);
542 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
544 list_del_init(&osd_seq->oos_seq_list);
545 write_unlock(&map->om_seq_list_lock);
546 osd_seq_free(osd_seq);
547 write_lock(&map->om_seq_list_lock);
549 write_unlock(&map->om_seq_list_lock);
553 osd->od_ost_map = NULL;
557 static int osd_index_backup_dir_init(const struct lu_env *env,
558 struct osd_device *dev)
560 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
561 struct lvfs_run_ctxt new;
562 struct lvfs_run_ctxt save;
563 struct dentry *dentry;
568 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
569 osd_push_ctxt(dev, &new, &save);
570 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
571 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
572 if (IS_ERR(dentry)) {
573 rc = PTR_ERR(dentry);
575 dev->od_index_backup_inode = igrab(dentry->d_inode);
578 pop_ctxt(&save, &new);
583 static void osd_index_backup_dir_fini(struct osd_device *dev)
585 if (dev->od_index_backup_inode) {
586 iput(dev->od_index_backup_inode);
587 dev->od_index_backup_inode = NULL;
591 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
594 bool mdt_init = false;
598 rc = osd_ost_init(env, dev);
602 if (!dev->od_is_ost) {
603 rc = osd_mdt_init(env, dev);
612 rc = osd_index_backup_dir_init(env, dev);
622 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
624 struct osd_obj_seq *osd_seq;
626 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
627 if (osd_seq->oos_seq == seq)
633 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
635 struct osd_obj_seq *osd_seq;
637 read_lock(&map->om_seq_list_lock);
638 osd_seq = osd_seq_find_locked(map, seq);
639 read_unlock(&map->om_seq_list_lock);
643 void osd_obj_map_fini(struct osd_device *dev)
645 osd_index_backup_dir_fini(dev);
651 * Update the specified OI mapping.
653 * \retval 1, changed nothing
654 * \retval 0, changed successfully
655 * \retval -ve, on error
657 static int osd_obj_update_entry(struct osd_thread_info *info,
658 struct osd_device *osd,
659 struct dentry *dir, const char *name,
660 const struct lu_fid *fid,
661 const struct osd_inode_id *id,
664 struct inode *parent = dir->d_inode;
665 struct dentry *child;
666 struct ldiskfs_dir_entry_2 *de;
667 struct buffer_head *bh;
669 struct dentry *dentry = &info->oti_obj_dentry;
670 struct osd_inode_id *oi_id = &info->oti_id3;
671 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
672 struct lu_fid *oi_fid = &lma->lma_self_fid;
678 LASSERT(th->h_transaction != NULL);
680 child = &info->oti_child_dentry;
681 child->d_parent = dir;
682 child->d_name.hash = 0;
683 child->d_name.name = name;
684 child->d_name.len = strlen(name);
686 dquot_initialize(parent);
688 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
690 GOTO(out, rc = PTR_ERR(bh));
692 if (le32_to_cpu(de->inode) == id->oii_ino)
695 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
696 inode = osd_iget(info, osd, oi_id);
699 if (rc == -ENOENT || rc == -ESTALE)
705 * The EA inode should NOT be in OI, old OI scrub may added
706 * such OI mapping by wrong, replace it.
708 if (unlikely(osd_is_ea_inode(inode))) {
713 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
714 if (rc == -ENODATA) {
715 rc = osd_get_idif(info, inode, dentry, oi_fid);
716 if (rc > 0 || rc == -ENODATA) {
727 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
728 * either a crashed object or a uninitialized one. Replace it.
730 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
731 CERROR("%s: the FID "DFID" is used by two objects: "
732 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
733 oi_id->oii_ino, oi_id->oii_gen,
734 id->oii_ino, id->oii_gen);
735 GOTO(out, rc = -EEXIST);
738 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
739 __u32 idx1 = fid_idif_ost_idx(fid);
740 __u32 idx2 = fid_idif_ost_idx(oi_fid);
741 struct ost_id *ostid = &info->oti_ostid;
742 struct lu_fid *tfid = &info->oti_fid3;
744 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
745 "invalid given FID "DFID", not match the "
746 "device index %u\n", PFID(fid), osd->od_index);
749 if (idx1 == 0 && idx2 == osd->od_index) {
750 fid_to_ostid(fid, ostid);
751 ostid_to_fid(tfid, ostid, idx2);
752 if (lu_fid_eq(tfid, oi_fid)) {
753 CERROR("%s: the FID "DFID" is used by "
754 "two objects(2): %u/%u %u/%u\n",
755 osd_name(osd), PFID(fid),
756 oi_id->oii_ino, oi_id->oii_gen,
757 id->oii_ino, id->oii_gen);
759 GOTO(out, rc = -EEXIST);
761 } else if (idx2 == 0 && idx1 == osd->od_index) {
762 fid_to_ostid(oi_fid, ostid);
763 ostid_to_fid(tfid, ostid, idx1);
764 if (lu_fid_eq(tfid, fid)) {
765 CERROR("%s: the FID "DFID" is used by "
766 "two objects(2): %u/%u %u/%u\n",
767 osd_name(osd), PFID(fid),
768 oi_id->oii_ino, oi_id->oii_gen,
769 id->oii_ino, id->oii_gen);
771 GOTO(out, rc = -EEXIST);
779 * There may be temporary inconsistency: On one hand, the new
780 * object may be referenced by multiple entries, which is out
781 * of our control unless we traverse the whole /O completely,
782 * which is non-flat order and inefficient, should be avoided;
783 * On the other hand, the old object may become orphan if it
784 * is still valid. Since it was referenced by an invalid entry,
785 * making it as invisible temporary may be not worse. OI scrub
786 * will process it later.
788 rc = ldiskfs_journal_get_write_access(th, bh);
792 de->inode = cpu_to_le32(id->oii_ino);
793 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
800 inode_unlock(parent);
804 static int osd_obj_del_entry(struct osd_thread_info *info,
805 struct osd_device *osd,
806 struct dentry *dird, char *name,
809 struct ldiskfs_dir_entry_2 *de;
810 struct buffer_head *bh;
811 struct dentry *child;
812 struct inode *dir = dird->d_inode;
818 LASSERT(th->h_transaction != NULL);
820 child = &info->oti_child_dentry;
821 child->d_name.hash = 0;
822 child->d_name.name = name;
823 child->d_name.len = strlen(name);
824 child->d_parent = dird;
825 child->d_inode = NULL;
827 dquot_initialize(dir);
829 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
833 rc = ldiskfs_delete_entry(th, dir, de, bh);
841 static int osd_obj_add_entry(struct osd_thread_info *info,
842 struct osd_device *osd,
843 struct dentry *dir, char *name,
844 const struct osd_inode_id *id,
847 struct dentry *child;
853 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
857 LASSERT(th->h_transaction != NULL);
859 inode = info->oti_inode;
860 if (unlikely(inode == NULL)) {
861 struct ldiskfs_inode_info *lii;
866 inode = info->oti_inode = &lii->vfs_inode;
869 inode->i_sb = osd_sb(osd);
870 osd_id_to_inode(inode, id);
871 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
873 child = &info->oti_child_dentry;
874 child->d_name.hash = 0;
875 child->d_name.name = name;
876 child->d_name.len = strlen(name);
877 child->d_parent = dir;
878 child->d_inode = inode;
880 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
883 dquot_initialize(dir->d_inode);
884 inode_lock(dir->d_inode);
885 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
886 inode_unlock(dir->d_inode);
892 * Use %llu for legacy OST sequences, but use %llx for new
893 * sequences names, so that the O/{seq}/dN/{oid} more closely
894 * follows the DFID/PFID format. This makes it easier to map from
895 * debug messages to objects in the future, and the legacy space
896 * of FID_SEQ_OST_MDT0 will be unused in the future.
898 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
900 snprintf(seq_name, name_size,
901 (fid_seq_is_rsvd(seq) ||
902 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
903 fid_seq_is_idif(seq) ? 0 : seq);
906 static inline void osd_oid_name(char *name, size_t name_size,
907 const struct lu_fid *fid, u64 id)
909 snprintf(name, name_size,
910 (fid_seq_is_rsvd(fid_seq(fid)) ||
911 fid_seq_is_mdt0(fid_seq(fid)) ||
912 fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
915 /* external locking is required */
916 static int osd_seq_load_locked(struct osd_thread_info *info,
917 struct osd_device *osd,
918 struct osd_obj_seq *osd_seq)
920 struct osd_obj_map *map = osd->od_ost_map;
921 struct dentry *seq_dir;
928 if (osd_seq->oos_root != NULL)
932 LASSERT(map->om_root);
934 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
936 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
937 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
939 GOTO(out_err, rc = PTR_ERR(seq_dir));
940 else if (seq_dir->d_inode == NULL)
941 GOTO(out_put, rc = -EFAULT);
943 osd_seq->oos_root = seq_dir;
945 LASSERT(osd_seq->oos_dirs == NULL);
946 OBD_ALLOC(osd_seq->oos_dirs,
947 sizeof(seq_dir) * osd_seq->oos_subdir_count);
948 if (osd_seq->oos_dirs == NULL)
949 GOTO(out_put, rc = -ENOMEM);
951 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
954 snprintf(dir_name, sizeof(dir_name), "d%u", i);
955 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
956 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
959 GOTO(out_free, rc = PTR_ERR(dir));
960 } else if (dir->d_inode == NULL) {
962 GOTO(out_free, rc = -EFAULT);
965 osd_seq->oos_dirs[i] = dir;
970 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
971 if (osd_seq->oos_dirs[i] != NULL)
972 dput(osd_seq->oos_dirs[i]);
974 OBD_FREE(osd_seq->oos_dirs,
975 sizeof(seq_dir) * osd_seq->oos_subdir_count);
978 osd_seq->oos_root = NULL;
984 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
985 struct osd_device *osd, u64 seq)
987 struct osd_obj_map *map;
988 struct osd_obj_seq *osd_seq;
993 map = osd->od_ost_map;
995 LASSERT(map->om_root);
997 osd_seq = osd_seq_find(map, seq);
998 if (likely(osd_seq != NULL))
1001 /* Serializing init process */
1002 mutex_lock(&map->om_dir_init_mutex);
1004 /* Check whether the seq has been added */
1005 read_lock(&map->om_seq_list_lock);
1006 osd_seq = osd_seq_find_locked(map, seq);
1007 if (osd_seq != NULL) {
1008 read_unlock(&map->om_seq_list_lock);
1009 GOTO(cleanup, rc = 0);
1011 read_unlock(&map->om_seq_list_lock);
1013 OBD_ALLOC_PTR(osd_seq);
1014 if (osd_seq == NULL)
1015 GOTO(cleanup, rc = -ENOMEM);
1017 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
1018 osd_seq->oos_seq = seq;
1020 * Init subdir count to be 32, but each seq can have
1021 * different subdir count
1023 osd_seq->oos_subdir_count = map->om_subdir_count;
1024 rc = osd_seq_load_locked(info, osd, osd_seq);
1028 write_lock(&map->om_seq_list_lock);
1029 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
1030 write_unlock(&map->om_seq_list_lock);
1033 mutex_unlock(&map->om_dir_init_mutex);
1035 if (osd_seq != NULL)
1036 OBD_FREE_PTR(osd_seq);
1037 RETURN(ERR_PTR(rc));
1043 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
1044 const struct lu_fid *fid, struct osd_inode_id *id)
1046 struct osd_obj_map *map;
1047 struct osd_obj_seq *osd_seq;
1048 struct dentry *d_seq;
1049 struct dentry *child;
1050 struct ost_id *ostid = &info->oti_ostid;
1053 struct ldiskfs_dir_entry_2 *de;
1054 struct buffer_head *bh;
1056 struct inode *inode;
1060 /* on the very first lookup we find and open directories */
1061 map = dev->od_ost_map;
1063 LASSERT(map->om_root);
1065 fid_to_ostid(fid, ostid);
1066 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1067 if (IS_ERR(osd_seq))
1068 RETURN(PTR_ERR(osd_seq));
1070 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1071 d_seq = osd_seq->oos_dirs[dirn];
1074 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1076 child = &info->oti_child_dentry;
1077 child->d_parent = d_seq;
1078 child->d_name.hash = 0;
1079 child->d_name.name = name;
1080 /* XXX: we can use rc from sprintf() instead of strlen() */
1081 child->d_name.len = strlen(name);
1083 dir = d_seq->d_inode;
1085 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1089 RETURN(PTR_ERR(bh));
1091 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1094 inode = osd_iget(info, dev, id);
1095 if (IS_ERR(inode)) {
1096 int rc = PTR_ERR(inode);
1098 RETURN(rc == -ENOENT ? -ESTALE : rc);
1105 int osd_obj_map_insert(struct osd_thread_info *info,
1106 struct osd_device *osd,
1107 const struct lu_fid *fid,
1108 const struct osd_inode_id *id,
1111 struct osd_obj_map *map;
1112 struct osd_obj_seq *osd_seq;
1114 struct ost_id *ostid = &info->oti_ostid;
1121 map = osd->od_ost_map;
1124 /* map fid to seq:objid */
1125 fid_to_ostid(fid, ostid);
1127 oid = ostid_id(ostid);
1128 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1129 if (IS_ERR(osd_seq))
1130 RETURN(PTR_ERR(osd_seq));
1132 dirn = oid & (osd_seq->oos_subdir_count - 1);
1133 d = osd_seq->oos_dirs[dirn];
1136 osd_oid_name(name, sizeof(name), fid, oid);
1139 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1140 if (rc == -EEXIST) {
1141 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1142 if (unlikely(rc == -ENOENT))
1145 if (unlikely(rc == 1))
1152 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1153 const struct lu_fid *fid, handle_t *th)
1155 struct osd_obj_map *map;
1156 struct osd_obj_seq *osd_seq;
1158 struct ost_id *ostid = &info->oti_ostid;
1164 map = osd->od_ost_map;
1167 /* map fid to seq:objid */
1168 fid_to_ostid(fid, ostid);
1170 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1171 if (IS_ERR(osd_seq))
1172 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1174 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1175 d = osd_seq->oos_dirs[dirn];
1178 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1179 rc = osd_obj_del_entry(info, osd, d, name, th);
1184 int osd_obj_map_update(struct osd_thread_info *info,
1185 struct osd_device *osd,
1186 const struct lu_fid *fid,
1187 const struct osd_inode_id *id,
1190 struct osd_obj_seq *osd_seq;
1192 struct ost_id *ostid = &info->oti_ostid;
1198 fid_to_ostid(fid, ostid);
1199 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1200 if (IS_ERR(osd_seq))
1201 RETURN(PTR_ERR(osd_seq));
1203 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1204 d = osd_seq->oos_dirs[dirn];
1207 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1208 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1213 int osd_obj_map_recover(struct osd_thread_info *info,
1214 struct osd_device *osd,
1215 struct inode *src_parent,
1216 struct dentry *src_child,
1217 const struct lu_fid *fid)
1219 struct osd_obj_seq *osd_seq;
1220 struct dentry *tgt_parent;
1221 struct dentry *tgt_child = &info->oti_child_dentry;
1223 struct inode *inode = src_child->d_inode;
1224 struct ost_id *ostid = &info->oti_ostid;
1226 struct ldiskfs_dir_entry_2 *de;
1227 struct buffer_head *bh;
1234 if (fid_is_last_id(fid)) {
1235 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1236 if (IS_ERR(osd_seq))
1237 RETURN(PTR_ERR(osd_seq));
1239 tgt_parent = osd_seq->oos_root;
1240 tgt_child->d_name.name = "LAST_ID";
1241 tgt_child->d_name.len = strlen("LAST_ID");
1243 fid_to_ostid(fid, ostid);
1244 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1245 if (IS_ERR(osd_seq))
1246 RETURN(PTR_ERR(osd_seq));
1248 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1249 tgt_parent = osd_seq->oos_dirs[dirn];
1250 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1251 tgt_child->d_name.name = name;
1252 tgt_child->d_name.len = strlen(name);
1254 LASSERT(tgt_parent != NULL);
1256 dir = tgt_parent->d_inode;
1257 tgt_child->d_name.hash = 0;
1258 tgt_child->d_parent = tgt_parent;
1259 tgt_child->d_inode = inode;
1261 /* The non-initialized src_child may be destroyed. */
1262 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1263 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1264 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1265 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1267 RETURN(PTR_ERR(jh));
1269 dquot_initialize(src_parent);
1270 dquot_initialize(dir);
1272 inode_lock(src_parent);
1274 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1277 * XXX: If some other object occupied the same slot. And If such
1278 * inode is zero-sized and with SUID+SGID, then means it is
1279 * a new created one. Maybe we can remove it and insert the
1280 * original one back to the /O/<seq>/d<x>. But there are
1281 * something to be considered:
1283 * 1) The OST-object under /lost+found has crashed LMA.
1284 * So it should not conflict with the current one.
1286 * 2) There are race conditions that: someone may just want
1287 * to modify the current one. Even if the OI scrub takes
1288 * the object lock when remove the current one, it still
1289 * cause the modification to be lost becasue the target
1290 * has been removed when the RPC service thread waiting
1293 * So keep it there before we have suitable solution.
1297 inode_unlock(src_parent);
1298 ldiskfs_journal_stop(jh);
1301 /* If the src object has never been modified, then remove it. */
1302 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1303 inode->i_mode & S_ISGID) {
1304 rc = ll_vfs_unlink(src_parent, src_child);
1305 if (unlikely(rc == -ENOENT))
1312 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1314 if (unlikely(IS_ERR(bh)))
1315 GOTO(unlock, rc = PTR_ERR(bh));
1317 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1322 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1328 inode_unlock(src_parent);
1329 ldiskfs_journal_stop(jh);
1333 static struct dentry *
1334 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1335 const struct lu_fid *fid, char **name)
1337 struct dentry *root = ERR_PTR(-ENOENT);
1339 if (fid_is_last_id(fid)) {
1340 struct osd_obj_seq *osd_seq;
1342 /* on creation of LAST_ID we create O/<seq> hierarchy */
1343 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1344 if (IS_ERR(osd_seq))
1345 RETURN((struct dentry *)osd_seq);
1348 root = osd_seq->oos_root;
1350 *name = osd_lf_fid2name(fid);
1352 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1353 else if ((*name)[0])
1354 root = osd_sb(osd)->s_root;
1360 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1361 const struct lu_fid *fid, const struct osd_inode_id *id,
1364 struct dentry *root;
1370 root = osd_object_spec_find(info, osd, fid, &name);
1371 if (!IS_ERR(root)) {
1372 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1382 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1383 const struct lu_fid *fid, const struct osd_inode_id *id,
1386 struct dentry *root;
1392 root = osd_object_spec_find(info, osd, fid, &name);
1393 if (!IS_ERR(root)) {
1394 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1404 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1405 const struct lu_fid *fid, struct osd_inode_id *id)
1407 struct dentry *root;
1408 struct dentry *dentry;
1409 struct inode *inode;
1415 if (fid_is_last_id(fid)) {
1416 struct osd_obj_seq *osd_seq;
1418 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1419 if (IS_ERR(osd_seq))
1420 RETURN(PTR_ERR(osd_seq));
1421 root = osd_seq->oos_root;
1424 root = osd_sb(osd)->s_root;
1425 name = osd_lf_fid2name(fid);
1426 if (name == NULL || strlen(name) == 0)
1430 dentry = osd_lookup_one_len_unlocked(name, root, strlen(name));
1431 if (!IS_ERR(dentry)) {
1432 inode = dentry->d_inode;
1434 if (is_bad_inode(inode)) {
1437 osd_id_gen(id, inode->i_ino,
1438 inode->i_generation);
1443 * if dentry is accessible after osd_compat_spec_insert it
1444 * will still contain NULL inode, so don't keep it in cache
1446 d_invalidate(dentry);
1453 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1454 bool bio_integrity_enabled(struct bio *bio)
1456 struct blk_integrity *bi = blk_get_integrity(bio_get_disk(bio));
1458 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1461 if (!bio_sectors(bio))
1464 /* Already protected? */
1465 if (bio_integrity(bio))
1471 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1472 (bi->flags & BLK_INTEGRITY_VERIFY))
1475 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1476 (bi->flags & BLK_INTEGRITY_GENERATE))