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/
31 * lustre/osd/osd_compat.c
33 * on-disk structure for managing /O
35 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
38 /* prerequisite for linux/xattr.h */
39 #include <linux/types.h>
40 /* prerequisite for linux/xattr.h */
42 /* XATTR_{REPLACE,CREATE} */
43 #include <linux/xattr.h>
46 * struct OBD_{ALLOC,FREE}*()
49 #include <obd_support.h>
51 #include "osd_internal.h"
54 static void osd_push_ctxt(const struct osd_device *dev,
55 struct lvfs_run_ctxt *newctxt,
56 struct lvfs_run_ctxt *save)
58 OBD_SET_CTXT_MAGIC(newctxt);
59 newctxt->pwdmnt = dev->od_mnt;
60 newctxt->pwd = dev->od_mnt->mnt_root;
61 newctxt->umask = current_umask();
64 push_ctxt(save, newctxt);
67 struct dentry *osd_lookup_one_len_common(struct osd_device *dev,
69 struct dentry *base, int len,
70 enum oi_check_flags flags)
72 struct dentry *dchild;
75 * We can't use inode_is_locked() directly since we can't know
76 * if the current thread context took the lock earlier or if
77 * another thread context took the lock. OI_LOCKED tells us
78 * if the current thread context has already taken the lock.
80 if (!(flags & OI_LOCKED)) {
81 /* If another thread took this lock already we will
82 * just have to wait until the other thread is done.
84 inode_lock(base->d_inode);
85 dchild = lookup_one_len(name, base, len);
86 inode_unlock(base->d_inode);
88 /* This thread context already has taken the lock.
89 * Other threads will have to wait until we are done.
91 dchild = lookup_one_len(name, base, len);
96 if (dchild->d_inode && unlikely(is_bad_inode(dchild->d_inode))) {
97 CERROR("%s: bad inode returned %lu/%u: rc = -ENOENT\n",
98 osd_name(dev), dchild->d_inode->i_ino,
99 dchild->d_inode->i_generation);
101 dchild = ERR_PTR(-ENOENT);
108 * osd_lookup_one_len_unlocked
110 * @dev: obd device we are searching
111 * @name: pathname component to lookup
112 * @base: base directory to lookup from
113 * @len: maximum length @len should be interpreted to
115 * Unlike osd_lookup_one_len, this should be called without the parent
116 * i_mutex held, and will take the i_mutex itself.
118 struct dentry *osd_lookup_one_len_unlocked(struct osd_device *dev,
120 struct dentry *base, int len)
122 return osd_lookup_one_len_common(dev, name, base, len, ~OI_LOCKED);
126 * osd_lookup_one_len - lookup single pathname component
128 * @dev: obd device we are searching
129 * @name: pathname component to lookup
130 * @base: base directory to lookup from
131 * @len: maximum length @len should be interpreted to
133 * The caller must hold inode lock
135 struct dentry *osd_lookup_one_len(struct osd_device *dev, const char *name,
136 struct dentry *base, int len)
138 return osd_lookup_one_len_common(dev, name, base, len, OI_LOCKED);
141 /* utility to make a directory */
142 static struct dentry *
143 simple_mkdir(const struct lu_env *env, struct osd_device *osd,
144 struct dentry *dir, const struct lu_fid *fid,
145 const char *name, __u32 compat, int mode, bool *created)
147 struct osd_thread_info *info = osd_oti_get(env);
148 struct lu_fid *tfid = &info->oti_fid3;
150 struct dentry *dchild;
155 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
156 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
157 dchild = osd_lookup_one_len_unlocked(osd, name, dir, strlen(name));
161 inode = dchild->d_inode;
163 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
164 int old_mode = inode->i_mode;
169 if (!S_ISDIR(old_mode)) {
170 CERROR("found %s (%lu/%u) is mode %o\n", name,
171 inode->i_ino, inode->i_generation, old_mode);
172 GOTO(out_err, err = -ENOTDIR);
175 if (unlikely(osd->od_dt_dev.dd_rdonly))
178 /* Fixup directory permissions if necessary */
179 if ((old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
181 "fixing permissions on %s from %o to %o\n",
182 name, old_mode, mode);
183 inode->i_mode = (mode & S_IALLUGO) |
184 (old_mode & ~S_IALLUGO);
185 mark_inode_dirty(inode);
188 err = osd_get_lma(info, inode, &info->oti_obj_dentry,
189 &info->oti_ost_attrs);
196 if ((fid && !lu_fid_eq(fid, &lma->lma_self_fid)) ||
197 lma->lma_compat != compat)
203 err = vfs_mkdir(&init_user_ns, dir->d_inode, dchild, mode);
207 inode = dchild->d_inode;
215 lu_igif_build(tfid, inode->i_ino, inode->i_generation);
216 err = osd_ea_fid_set(info, inode, tfid, compat, 0);
227 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
229 struct lr_server_data lsd;
230 struct dentry *dlast;
233 int count = OBJ_SUBDIR_COUNT;
237 dlast = osd_lookup_one_len_unlocked(osd, LAST_RCVD, osd_sb(osd)->s_root,
240 return PTR_ERR(dlast);
241 else if (dlast->d_inode == NULL)
245 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
246 if (rc == sizeof(lsd)) {
248 "read last_rcvd header, uuid = %s, subdir count = %d\n",
249 lsd.lsd_uuid, lsd.lsd_subdir_count);
250 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
251 count = le16_to_cpu(lsd.lsd_subdir_count);
252 } else if (rc != 0) {
253 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
265 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
267 struct lvfs_run_ctxt new;
268 struct lvfs_run_ctxt save;
269 struct dentry *parent;
270 struct osd_mdobj_map *omm;
272 struct osd_thread_info *info = osd_oti_get(env);
273 struct lu_fid *fid = &info->oti_fid3;
278 OBD_ALLOC_PTR(dev->od_mdt_map);
279 if (dev->od_mdt_map == NULL)
282 omm = dev->od_mdt_map;
284 parent = osd_sb(dev)->s_root;
285 osd_push_ctxt(dev, &new, &save);
287 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
288 d = simple_mkdir(env, dev, parent, fid, REMOTE_PARENT_DIR,
289 LMAC_NOT_IN_OI, 0755, NULL);
291 GOTO(cleanup, rc = PTR_ERR(d));
293 omm->omm_remote_parent = d;
295 GOTO(cleanup, rc = 0);
298 pop_ctxt(&save, &new);
300 if (omm->omm_remote_parent != NULL)
301 dput(omm->omm_remote_parent);
303 dev->od_mdt_map = NULL;
308 static void osd_mdt_fini(struct osd_device *osd)
310 struct osd_mdobj_map *omm = osd->od_mdt_map;
315 if (omm->omm_remote_parent)
316 dput(omm->omm_remote_parent);
319 osd->od_ost_map = NULL;
322 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
323 struct osd_object *obj, struct osd_thandle *oh)
325 struct osd_mdobj_map *omm = osd->od_mdt_map;
326 struct osd_thread_info *oti = osd_oti_get(env);
327 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
328 char *name = oti->oti_name;
329 struct osd_thread_info *info = osd_oti_get(env);
330 struct dentry *dentry;
331 struct dentry *parent;
334 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
338 * Set REMOTE_PARENT in lma, so other process like unlink or lfsck
339 * can identify this object quickly
341 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
342 &oti->oti_ost_attrs);
346 lma->lma_incompat |= LMAI_REMOTE_PARENT;
347 lustre_lma_swab(lma);
348 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
349 sizeof(*lma), XATTR_REPLACE);
353 parent = omm->omm_remote_parent;
354 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
355 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
357 inode_lock(parent->d_inode);
358 rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
359 obj->oo_inode, NULL);
360 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
361 osd_ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
362 else if (unlikely(rc == -EEXIST))
365 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
366 CDEBUG(D_INODE, "%s: create agent entry for %s: rc = %d\n",
367 osd_name(osd), name, rc);
368 mark_inode_dirty(parent->d_inode);
369 inode_unlock(parent->d_inode);
373 int osd_delete_from_remote_parent(const struct lu_env *env,
374 struct osd_device *osd,
375 struct osd_object *obj,
376 struct osd_thandle *oh, bool destroy)
378 struct osd_mdobj_map *omm = osd->od_mdt_map;
379 struct osd_thread_info *oti = osd_oti_get(env);
380 struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
381 char *name = oti->oti_name;
382 struct dentry *dentry;
383 struct dentry *parent;
384 struct ldiskfs_dir_entry_2 *de;
385 struct buffer_head *bh;
388 parent = omm->omm_remote_parent;
389 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
390 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
392 inode_lock(parent->d_inode);
393 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
396 inode_unlock(parent->d_inode);
398 if (unlikely(rc == -ENOENT))
401 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode,
403 if (!rc && S_ISDIR(obj->oo_inode->i_mode))
404 osd_ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
405 mark_inode_dirty(parent->d_inode);
406 inode_unlock(parent->d_inode);
408 CDEBUG(D_INODE, "%s: remove agent entry for %s: rc = %d\n",
409 osd_name(osd), name, rc);
414 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
419 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
420 &oti->oti_ost_attrs);
424 /* Get rid of REMOTE_PARENT flag from incompat */
425 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
426 lustre_lma_swab(lma);
427 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
428 sizeof(*lma), XATTR_REPLACE);
430 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
434 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
435 struct osd_device *osd,
436 const struct lu_fid *fid,
437 struct osd_inode_id *id)
439 struct osd_mdobj_map *omm = osd->od_mdt_map;
440 char *name = oti->oti_name;
441 struct dentry *parent;
442 struct dentry *dentry;
443 struct ldiskfs_dir_entry_2 *de;
444 struct buffer_head *bh;
449 if (unlikely(osd->od_is_ost))
452 parent = omm->omm_remote_parent;
453 sprintf(name, DFID_NOBRACE, PFID(fid));
454 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
456 inode_lock(parent->d_inode);
457 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
464 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
466 inode = osd_iget(oti, osd, id);
476 inode_unlock(parent->d_inode);
478 osd_add_oi_cache(oti, osd, id, fid);
483 * directory structure on legacy OST:
485 * O/<seq>/d0-31/<objid>
492 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
494 struct lvfs_run_ctxt new;
495 struct lvfs_run_ctxt save;
498 bool created = false;
502 OBD_ALLOC_PTR(dev->od_ost_map);
503 if (dev->od_ost_map == NULL)
506 /* to get subdir count from last_rcvd */
507 rc = osd_last_rcvd_subdir_count(dev);
509 GOTO(cleanup_alloc, rc);
511 dev->od_ost_map->om_subdir_count = rc;
512 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
513 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
514 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
516 osd_push_ctxt(dev, &new, &save);
517 d = simple_mkdir(env, dev, osd_sb(dev)->s_root, NULL, "O",
518 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, &created);
520 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
523 /* It is quite probably that the device is new formatted. */
524 dev->od_maybe_new = 1;
526 dev->od_ost_map->om_root = d;
528 pop_ctxt(&save, &new);
532 pop_ctxt(&save, &new);
534 OBD_FREE_PTR(dev->od_ost_map);
538 static void osd_seq_free(struct osd_obj_seq *osd_seq)
542 if (osd_seq->oos_dirs) {
543 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
544 if (osd_seq->oos_dirs[j])
545 dput(osd_seq->oos_dirs[j]);
547 OBD_FREE_PTR_ARRAY(osd_seq->oos_dirs,
548 osd_seq->oos_subdir_count);
551 if (osd_seq->oos_root)
552 dput(osd_seq->oos_root);
554 OBD_FREE_PTR(osd_seq);
557 static void osd_ost_fini(struct osd_device *osd)
559 struct osd_obj_seq *osd_seq;
560 struct osd_obj_seq *tmp;
561 struct osd_obj_map *map = osd->od_ost_map;
568 write_lock(&map->om_seq_list_lock);
569 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
571 list_del_init(&osd_seq->oos_seq_list);
572 write_unlock(&map->om_seq_list_lock);
573 osd_seq_free(osd_seq);
574 write_lock(&map->om_seq_list_lock);
576 write_unlock(&map->om_seq_list_lock);
580 osd->od_ost_map = NULL;
584 static int osd_index_backup_dir_init(const struct lu_env *env,
585 struct osd_device *dev)
587 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
588 struct lvfs_run_ctxt new;
589 struct lvfs_run_ctxt save;
590 struct dentry *dentry;
595 lu_local_obj_fid(fid, INDEX_BACKUP_OID);
596 osd_push_ctxt(dev, &new, &save);
597 dentry = simple_mkdir(env, dev, osd_sb(dev)->s_root, fid,
598 INDEX_BACKUP_DIR, LMAC_NOT_IN_OI, 0755, NULL);
599 if (IS_ERR(dentry)) {
600 rc = PTR_ERR(dentry);
602 dev->od_index_backup_inode = igrab(dentry->d_inode);
605 pop_ctxt(&save, &new);
610 static void osd_index_backup_dir_fini(struct osd_device *dev)
612 iput(dev->od_index_backup_inode);
613 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 dquot_initialize(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)
729 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
730 if (rc == -ENODATA) {
731 rc = osd_get_idif(info, inode, dentry, oi_fid);
732 if (rc > 0 || rc == -ENODATA) {
743 * If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
744 * either a crashed object or a uninitialized one. Replace it.
746 if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
747 CERROR("%s: the FID "DFID" is used by two objects: "
748 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
749 oi_id->oii_ino, oi_id->oii_gen,
750 id->oii_ino, id->oii_gen);
751 GOTO(out, rc = -EEXIST);
754 if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
755 __u32 idx1 = fid_idif_ost_idx(fid);
756 __u32 idx2 = fid_idif_ost_idx(oi_fid);
757 struct ost_id *ostid = &info->oti_ostid;
758 struct lu_fid *tfid = &info->oti_fid3;
760 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
761 "invalid given FID "DFID", not match the "
762 "device index %u\n", PFID(fid), osd->od_index);
765 if (idx1 == 0 && idx2 == osd->od_index) {
766 fid_to_ostid(fid, ostid);
767 ostid_to_fid(tfid, ostid, idx2);
768 if (lu_fid_eq(tfid, oi_fid)) {
769 CERROR("%s: the FID "DFID" is used by "
770 "two objects(2): %u/%u %u/%u\n",
771 osd_name(osd), PFID(fid),
772 oi_id->oii_ino, oi_id->oii_gen,
773 id->oii_ino, id->oii_gen);
775 GOTO(out, rc = -EEXIST);
777 } else if (idx2 == 0 && idx1 == osd->od_index) {
778 fid_to_ostid(oi_fid, ostid);
779 ostid_to_fid(tfid, ostid, idx1);
780 if (lu_fid_eq(tfid, fid)) {
781 CERROR("%s: the FID "DFID" is used by "
782 "two objects(2): %u/%u %u/%u\n",
783 osd_name(osd), PFID(fid),
784 oi_id->oii_ino, oi_id->oii_gen,
785 id->oii_ino, id->oii_gen);
787 GOTO(out, rc = -EEXIST);
795 * There may be temporary inconsistency: On one hand, the new
796 * object may be referenced by multiple entries, which is out
797 * of our control unless we traverse the whole /O completely,
798 * which is non-flat order and inefficient, should be avoided;
799 * On the other hand, the old object may become orphan if it
800 * is still valid. Since it was referenced by an invalid entry,
801 * making it as invisible temporary may be not worse. OI scrub
802 * will process it later.
804 rc = osd_ldiskfs_journal_get_write_access(th, parent->i_sb, bh,
809 de->inode = cpu_to_le32(id->oii_ino);
810 rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
817 inode_unlock(parent);
821 int osd_obj_del_entry(struct osd_thread_info *info, struct osd_device *osd,
822 struct dentry *dird, char *name, int namelen,
825 struct ldiskfs_dir_entry_2 *de;
826 struct buffer_head *bh;
827 struct dentry *child;
828 struct inode *dir = dird->d_inode;
834 LASSERT(th->h_transaction != NULL);
836 child = &info->oti_child_dentry;
837 child->d_name.hash = 0;
838 child->d_name.name = name;
839 child->d_name.len = namelen;
840 child->d_parent = dird;
841 child->d_inode = NULL;
843 dquot_initialize(dir);
845 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
849 rc = ldiskfs_delete_entry(th, dir, de, bh);
857 static int osd_obj_add_entry(struct osd_thread_info *info,
858 struct osd_device *osd,
859 struct dentry *dir, char *name,
860 const struct osd_inode_id *id,
863 struct dentry *child;
869 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
873 LASSERT(th->h_transaction != NULL);
875 inode = info->oti_inode;
876 if (unlikely(inode == NULL)) {
877 struct ldiskfs_inode_info *lii;
882 inode = info->oti_inode = &lii->vfs_inode;
885 inode->i_sb = osd_sb(osd);
886 osd_id_to_inode(inode, id);
887 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
889 child = &info->oti_child_dentry;
890 child->d_name.hash = 0;
891 child->d_name.name = name;
892 child->d_name.len = strlen(name);
893 child->d_parent = dir;
894 child->d_inode = inode;
896 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
899 dquot_initialize(dir->d_inode);
900 inode_lock(dir->d_inode);
901 rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
902 inode_unlock(dir->d_inode);
908 * Use %llu for legacy OST sequences, but use %llx for new
909 * sequences names, so that the O/{seq}/dN/{oid} more closely
910 * follows the DFID/PFID format. This makes it easier to map from
911 * debug messages to objects in the future, and the legacy space
912 * of FID_SEQ_OST_MDT0 will be unused in the future.
914 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
916 snprintf(seq_name, name_size,
917 (fid_seq_is_rsvd(seq) ||
918 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
919 fid_seq_is_idif(seq) ? 0 : seq);
922 /* external locking is required */
923 static int osd_seq_load_locked(struct osd_thread_info *info,
924 struct osd_device *osd,
925 struct osd_obj_seq *osd_seq)
927 struct osd_obj_map *map = osd->od_ost_map;
928 struct dentry *seq_dir;
935 if (osd_seq->oos_root != NULL)
939 LASSERT(map->om_root);
941 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
943 seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
944 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
946 GOTO(out_err, rc = PTR_ERR(seq_dir));
947 else if (seq_dir->d_inode == NULL)
948 GOTO(out_put, rc = -EFAULT);
950 osd_seq->oos_root = seq_dir;
952 LASSERT(osd_seq->oos_dirs == NULL);
953 OBD_ALLOC_PTR_ARRAY(osd_seq->oos_dirs, osd_seq->oos_subdir_count);
954 if (osd_seq->oos_dirs == NULL)
955 GOTO(out_put, rc = -ENOMEM);
957 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
960 snprintf(dir_name, sizeof(dir_name), "d%u", i);
961 dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
962 dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
965 GOTO(out_free, rc = PTR_ERR(dir));
966 } else if (dir->d_inode == NULL) {
968 GOTO(out_free, rc = -EFAULT);
971 osd_seq->oos_dirs[i] = dir;
976 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
977 if (osd_seq->oos_dirs[i] != NULL)
978 dput(osd_seq->oos_dirs[i]);
980 OBD_FREE_PTR_ARRAY(osd_seq->oos_dirs,
981 osd_seq->oos_subdir_count);
984 osd_seq->oos_root = NULL;
990 struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
991 struct osd_device *osd, u64 seq)
993 struct osd_obj_map *map;
994 struct osd_obj_seq *osd_seq;
999 map = osd->od_ost_map;
1001 LASSERT(map->om_root);
1003 osd_seq = osd_seq_find(map, seq);
1004 if (likely(osd_seq != NULL))
1007 /* Serializing init process */
1008 mutex_lock(&map->om_dir_init_mutex);
1010 /* Check whether the seq has been added */
1011 read_lock(&map->om_seq_list_lock);
1012 osd_seq = osd_seq_find_locked(map, seq);
1013 if (osd_seq != NULL) {
1014 read_unlock(&map->om_seq_list_lock);
1015 GOTO(cleanup, rc = 0);
1017 read_unlock(&map->om_seq_list_lock);
1019 OBD_ALLOC_PTR(osd_seq);
1020 if (osd_seq == NULL)
1021 GOTO(cleanup, rc = -ENOMEM);
1023 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
1024 osd_seq->oos_seq = seq;
1026 * Init subdir count to be 32, but each seq can have
1027 * different subdir count
1029 osd_seq->oos_subdir_count = map->om_subdir_count;
1030 rc = osd_seq_load_locked(info, osd, osd_seq);
1034 write_lock(&map->om_seq_list_lock);
1035 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
1036 write_unlock(&map->om_seq_list_lock);
1039 mutex_unlock(&map->om_dir_init_mutex);
1041 if (osd_seq != NULL)
1042 OBD_FREE_PTR(osd_seq);
1043 RETURN(ERR_PTR(rc));
1049 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
1050 const struct lu_fid *fid, struct osd_inode_id *id)
1052 struct osd_obj_map *map;
1053 struct osd_obj_seq *osd_seq;
1054 struct dentry *d_seq;
1055 struct dentry *child;
1056 struct ost_id *ostid = &info->oti_ostid;
1059 struct ldiskfs_dir_entry_2 *de;
1060 struct buffer_head *bh;
1062 struct inode *inode;
1066 /* on the very first lookup we find and open directories */
1067 map = dev->od_ost_map;
1069 LASSERT(map->om_root);
1071 fid_to_ostid(fid, ostid);
1072 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
1073 if (IS_ERR(osd_seq))
1074 RETURN(PTR_ERR(osd_seq));
1076 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1077 d_seq = osd_seq->oos_dirs[dirn];
1080 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1082 child = &info->oti_child_dentry;
1083 child->d_parent = d_seq;
1084 child->d_name.hash = 0;
1085 child->d_name.name = name;
1086 /* XXX: we can use rc from sprintf() instead of strlen() */
1087 child->d_name.len = strlen(name);
1089 dir = d_seq->d_inode;
1091 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
1095 RETURN(PTR_ERR(bh));
1097 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1100 inode = osd_iget(info, dev, id);
1101 if (IS_ERR(inode)) {
1102 int rc = PTR_ERR(inode);
1104 RETURN(rc == -ENOENT ? -ESTALE : rc);
1111 int osd_obj_map_insert(struct osd_thread_info *info,
1112 struct osd_device *osd,
1113 const struct lu_fid *fid,
1114 const struct osd_inode_id *id,
1117 struct osd_obj_map *map;
1118 struct osd_obj_seq *osd_seq;
1120 struct ost_id *ostid = &info->oti_ostid;
1127 map = osd->od_ost_map;
1130 /* map fid to seq:objid */
1131 fid_to_ostid(fid, ostid);
1133 oid = ostid_id(ostid);
1134 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1135 if (IS_ERR(osd_seq))
1136 RETURN(PTR_ERR(osd_seq));
1138 dirn = oid & (osd_seq->oos_subdir_count - 1);
1139 d = osd_seq->oos_dirs[dirn];
1142 osd_oid_name(name, sizeof(name), fid, oid);
1145 rc = osd_obj_add_entry(info, osd, d, name, id, th);
1146 if (rc == -EEXIST) {
1147 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1148 if (unlikely(rc == -ENOENT))
1151 if (unlikely(rc == 1))
1158 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1159 const struct lu_fid *fid, handle_t *th)
1161 struct osd_obj_map *map;
1162 struct osd_obj_seq *osd_seq;
1164 struct ost_id *ostid = &info->oti_ostid;
1170 map = osd->od_ost_map;
1173 /* map fid to seq:objid */
1174 fid_to_ostid(fid, ostid);
1176 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1177 if (IS_ERR(osd_seq))
1178 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1180 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1181 d = osd_seq->oos_dirs[dirn];
1184 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1185 rc = osd_obj_del_entry(info, osd, d, name, strlen(name), th);
1190 int osd_obj_map_update(struct osd_thread_info *info,
1191 struct osd_device *osd,
1192 const struct lu_fid *fid,
1193 const struct osd_inode_id *id,
1196 struct osd_obj_seq *osd_seq;
1198 struct ost_id *ostid = &info->oti_ostid;
1204 fid_to_ostid(fid, ostid);
1205 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1206 if (IS_ERR(osd_seq))
1207 RETURN(PTR_ERR(osd_seq));
1209 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1210 d = osd_seq->oos_dirs[dirn];
1213 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1214 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1219 int osd_obj_map_recover(struct osd_thread_info *info,
1220 struct osd_device *osd,
1221 struct inode *src_parent,
1222 struct dentry *src_child,
1223 const struct lu_fid *fid)
1225 struct osd_obj_seq *osd_seq;
1226 struct dentry *tgt_parent;
1227 struct dentry *tgt_child = &info->oti_child_dentry;
1229 struct inode *inode = src_child->d_inode;
1230 struct ost_id *ostid = &info->oti_ostid;
1232 struct ldiskfs_dir_entry_2 *de;
1233 struct buffer_head *bh;
1240 if (fid_is_last_id(fid)) {
1241 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1242 if (IS_ERR(osd_seq))
1243 RETURN(PTR_ERR(osd_seq));
1245 tgt_parent = osd_seq->oos_root;
1246 tgt_child->d_name.name = "LAST_ID";
1247 tgt_child->d_name.len = strlen("LAST_ID");
1249 fid_to_ostid(fid, ostid);
1250 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1251 if (IS_ERR(osd_seq))
1252 RETURN(PTR_ERR(osd_seq));
1254 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1255 tgt_parent = osd_seq->oos_dirs[dirn];
1256 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1257 tgt_child->d_name.name = name;
1258 tgt_child->d_name.len = strlen(name);
1260 LASSERT(tgt_parent != NULL);
1262 dir = tgt_parent->d_inode;
1263 tgt_child->d_name.hash = 0;
1264 tgt_child->d_parent = tgt_parent;
1265 tgt_child->d_inode = inode;
1267 /* The non-initialized src_child may be destroyed. */
1268 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1269 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1270 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1271 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1273 RETURN(PTR_ERR(jh));
1275 dquot_initialize(src_parent);
1276 dquot_initialize(dir);
1279 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1282 * XXX: If some other object occupied the same slot. And If such
1283 * inode is zero-sized and with SUID+SGID, then means it is
1284 * a new created one. Maybe we can remove it and insert the
1285 * original one back to the /O/<seq>/d<x>. But there are
1286 * something to be considered:
1288 * 1) The OST-object under /lost+found has crashed LMA.
1289 * So it should not conflict with the current one.
1291 * 2) There are race conditions that: someone may just want
1292 * to modify the current one. Even if the OI scrub takes
1293 * the object lock when remove the current one, it still
1294 * cause the modification to be lost becasue the target
1295 * has been removed when the RPC service thread waiting
1298 * So keep it there before we have suitable solution.
1302 ldiskfs_journal_stop(jh);
1305 /* If the src object has never been modified, then remove it. */
1306 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1307 inode->i_mode & S_ISGID) {
1308 rc = vfs_unlink(&init_user_ns, src_parent, src_child);
1309 if (unlikely(rc == -ENOENT))
1316 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1318 if (unlikely(IS_ERR(bh)))
1319 GOTO(unlock, rc = PTR_ERR(bh));
1321 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1326 rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1332 ldiskfs_journal_stop(jh);
1336 static struct dentry *
1337 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1338 const struct lu_fid *fid, char **name)
1340 struct dentry *root = ERR_PTR(-ENOENT);
1342 if (fid_is_last_id(fid)) {
1343 struct osd_obj_seq *osd_seq;
1345 /* on creation of LAST_ID we create O/<seq> hierarchy */
1346 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1347 if (IS_ERR(osd_seq))
1348 RETURN((struct dentry *)osd_seq);
1351 root = osd_seq->oos_root;
1353 *name = osd_lf_fid2name(fid);
1355 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1356 else if ((*name)[0])
1357 root = osd_sb(osd)->s_root;
1363 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1364 const struct lu_fid *fid, const struct osd_inode_id *id,
1367 struct dentry *root;
1373 root = osd_object_spec_find(info, osd, fid, &name);
1374 if (!IS_ERR(root)) {
1375 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1385 int osd_obj_spec_insert(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_add_entry(info, osd, root, name, id, th);
1407 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1408 const struct lu_fid *fid, struct osd_inode_id *id,
1409 enum oi_check_flags flags)
1411 struct dentry *root;
1412 struct dentry *dentry;
1413 struct inode *inode;
1419 if (fid_is_last_id(fid)) {
1420 struct osd_obj_seq *osd_seq;
1422 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1423 if (IS_ERR(osd_seq))
1424 RETURN(PTR_ERR(osd_seq));
1425 root = osd_seq->oos_root;
1428 root = osd_sb(osd)->s_root;
1429 name = osd_lf_fid2name(fid);
1430 if (name == NULL || strlen(name) == 0)
1434 dentry = osd_lookup_one_len_common(osd, name, root, strlen(name),
1436 if (!IS_ERR(dentry)) {
1437 inode = dentry->d_inode;
1439 if (is_bad_inode(inode)) {
1442 osd_id_gen(id, inode->i_ino,
1443 inode->i_generation);
1448 * if dentry is accessible after osd_compat_spec_insert it
1449 * will still contain NULL inode, so don't keep it in cache
1451 d_invalidate(dentry);
1458 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1459 bool bio_integrity_enabled(struct bio *bio)
1461 struct blk_integrity *bi = blk_get_integrity(bio_get_disk(bio));
1463 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
1466 if (!bio_sectors(bio))
1469 /* Already protected? */
1470 if (bio_integrity(bio))
1476 if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
1477 (bi->flags & BLK_INTEGRITY_VERIFY))
1480 if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
1481 (bi->flags & BLK_INTEGRITY_GENERATE))