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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_compat.c
38 * on-disk structure for managing /O
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
53 * struct OBD_{ALLOC,FREE}*()
56 #include <obd_support.h>
58 #include "osd_internal.h"
61 static void osd_push_ctxt(const struct osd_device *dev,
62 struct lvfs_run_ctxt *newctxt,
63 struct lvfs_run_ctxt *save)
65 OBD_SET_CTXT_MAGIC(newctxt);
66 newctxt->pwdmnt = dev->od_mnt;
67 newctxt->pwd = dev->od_mnt->mnt_root;
68 newctxt->fs = get_ds();
70 push_ctxt(save, newctxt);
73 /* utility to make a directory */
74 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
75 const char *name, int mode, int fix)
77 struct dentry *dchild;
81 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
82 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
83 dchild = ll_lookup_one_len(name, dir, strlen(name));
87 if (dchild->d_inode) {
88 int old_mode = dchild->d_inode->i_mode;
89 if (!S_ISDIR(old_mode)) {
90 CERROR("found %s (%lu/%u) is mode %o\n", name,
91 dchild->d_inode->i_ino,
92 dchild->d_inode->i_generation, old_mode);
93 GOTO(out_err, err = -ENOTDIR);
96 /* Fixup directory permissions if necessary */
97 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
99 "fixing permissions on %s from %o to %o\n",
100 name, old_mode, mode);
101 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
102 (old_mode & ~S_IALLUGO);
103 mark_inode_dirty(dchild->d_inode);
105 GOTO(out_up, dchild);
108 err = vfs_mkdir(dir->d_inode, dchild, mode);
116 dchild = ERR_PTR(err);
121 int osd_last_rcvd_subdir_count(struct osd_device *osd)
123 struct lr_server_data lsd;
124 struct dentry *dlast;
127 int count = OBJ_SUBDIR_COUNT;
131 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
134 return PTR_ERR(dlast);
135 else if (dlast->d_inode == NULL)
139 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
140 if (rc == sizeof(lsd)) {
141 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
142 "subdir count = %d\n", lsd.lsd_uuid,
143 lsd.lsd_subdir_count);
144 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
145 count = le16_to_cpu(lsd.lsd_subdir_count);
146 } else if (rc != 0) {
147 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
159 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
160 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
162 struct lvfs_run_ctxt new;
163 struct lvfs_run_ctxt save;
164 struct dentry *parent;
165 struct osd_mdobj_map *omm;
167 struct osd_thread_info *info = osd_oti_get(env);
168 struct lu_fid *fid = &info->oti_fid3;
172 OBD_ALLOC_PTR(dev->od_mdt_map);
173 if (dev->od_mdt_map == NULL)
176 omm = dev->od_mdt_map;
178 parent = osd_sb(dev)->s_root;
179 osd_push_ctxt(dev, &new, &save);
181 d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
184 GOTO(cleanup, rc = PTR_ERR(d));
186 omm->omm_remote_parent = d;
188 /* Set LMA for remote parent inode */
189 lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
190 rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
195 pop_ctxt(&save, &new);
197 if (omm->omm_remote_parent != NULL)
198 dput(omm->omm_remote_parent);
200 dev->od_mdt_map = NULL;
205 static void osd_mdt_fini(struct osd_device *osd)
207 struct osd_mdobj_map *omm = osd->od_mdt_map;
212 if (omm->omm_remote_parent)
213 dput(omm->omm_remote_parent);
216 osd->od_ost_map = NULL;
219 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
220 struct osd_object *obj, struct osd_thandle *oh)
222 struct osd_mdobj_map *omm = osd->od_mdt_map;
223 struct osd_thread_info *oti = osd_oti_get(env);
224 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
225 char *name = oti->oti_name;
226 struct dentry *dentry;
227 struct dentry *parent;
230 /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
231 * can identify this object quickly */
232 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
236 lma->lma_incompat |= LMAI_REMOTE_PARENT;
237 lustre_lma_swab(lma);
238 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
239 sizeof(*lma), XATTR_REPLACE);
243 parent = omm->omm_remote_parent;
244 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
245 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
247 mutex_lock(&parent->d_inode->i_mutex);
248 rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
250 CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
251 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
252 ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
253 mark_inode_dirty(parent->d_inode);
254 mutex_unlock(&parent->d_inode->i_mutex);
258 int osd_delete_from_remote_parent(const struct lu_env *env,
259 struct osd_device *osd,
260 struct osd_object *obj,
261 struct osd_thandle *oh)
263 struct osd_mdobj_map *omm = osd->od_mdt_map;
264 struct osd_thread_info *oti = osd_oti_get(env);
265 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
266 char *name = oti->oti_name;
267 struct dentry *dentry;
268 struct dentry *parent;
269 struct ldiskfs_dir_entry_2 *de;
270 struct buffer_head *bh;
273 /* Check lma to see whether it is remote object */
274 rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
278 if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
281 parent = omm->omm_remote_parent;
282 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
283 dentry = osd_child_dentry_by_inode(env, parent->d_inode,
285 mutex_lock(&parent->d_inode->i_mutex);
286 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
289 mutex_unlock(&parent->d_inode->i_mutex);
292 CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
293 name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
294 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
295 ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
296 mark_inode_dirty(parent->d_inode);
297 mutex_unlock(&parent->d_inode->i_mutex);
300 /* Get rid of REMOTE_PARENT flag from incompat */
301 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
302 lustre_lma_swab(lma);
303 rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
304 sizeof(*lma), XATTR_REPLACE);
308 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
309 struct osd_device *osd,
310 const struct lu_fid *fid,
311 struct osd_inode_id *id)
313 struct osd_mdobj_map *omm = osd->od_mdt_map;
314 char *name = oti->oti_name;
315 struct dentry *parent;
316 struct dentry *dentry;
317 struct ldiskfs_dir_entry_2 *de;
318 struct buffer_head *bh;
322 parent = omm->omm_remote_parent;
323 sprintf(name, DFID_NOBRACE, PFID(fid));
324 dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
326 mutex_lock(&parent->d_inode->i_mutex);
327 bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
333 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
336 mutex_unlock(&parent->d_inode->i_mutex);
338 osd_add_oi_cache(oti, osd, id, fid);
343 * directory structure on legacy OST:
345 * O/<seq>/d0-31/<objid>
352 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
354 struct lvfs_run_ctxt new;
355 struct lvfs_run_ctxt save;
356 struct dentry *rootd = osd_sb(dev)->s_root;
358 struct osd_thread_info *info = osd_oti_get(env);
360 struct lu_fid *fid = &info->oti_fid3;
364 OBD_ALLOC_PTR(dev->od_ost_map);
365 if (dev->od_ost_map == NULL)
368 /* to get subdir count from last_rcvd */
369 rc = osd_last_rcvd_subdir_count(dev);
371 OBD_FREE_PTR(dev->od_ost_map);
375 dev->od_ost_map->om_subdir_count = rc;
378 INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
379 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
380 mutex_init(&dev->od_ost_map->om_dir_init_mutex);
382 osd_push_ctxt(dev, &new, &save);
384 d = ll_lookup_one_len("O", rootd, strlen("O"));
386 GOTO(cleanup, rc = PTR_ERR(d));
387 if (d->d_inode == NULL) {
389 /* The lookup() may be called again inside simple_mkdir().
390 * Since the repeated lookup() only be called for "/O" at
391 * mount time, it will not affect the whole performance. */
392 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
394 GOTO(cleanup, rc = PTR_ERR(d));
396 /* It is quite probably that the device is new formatted. */
397 dev->od_maybe_new = 1;
401 dev->od_ost_map->om_root = d;
403 /* 'What the @fid is' is not imporatant, because the object
404 * has no OI mapping, and only is visible inside the OSD.*/
405 lu_igif_build(fid, inode->i_ino, inode->i_generation);
406 rc = osd_ea_fid_set(info, inode, fid,
407 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
412 pop_ctxt(&save, &new);
414 OBD_FREE_PTR(dev->od_ost_map);
420 static void osd_seq_free(struct osd_obj_map *map,
421 struct osd_obj_seq *osd_seq)
425 list_del_init(&osd_seq->oos_seq_list);
427 if (osd_seq->oos_dirs) {
428 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
429 if (osd_seq->oos_dirs[j])
430 dput(osd_seq->oos_dirs[j]);
432 OBD_FREE(osd_seq->oos_dirs,
433 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
436 if (osd_seq->oos_root)
437 dput(osd_seq->oos_root);
439 OBD_FREE_PTR(osd_seq);
442 static void osd_ost_fini(struct osd_device *osd)
444 struct osd_obj_seq *osd_seq;
445 struct osd_obj_seq *tmp;
446 struct osd_obj_map *map = osd->od_ost_map;
452 write_lock(&map->om_seq_list_lock);
453 list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
455 osd_seq_free(map, osd_seq);
457 write_unlock(&map->om_seq_list_lock);
461 osd->od_ost_map = NULL;
465 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
470 /* prepare structures for OST */
471 rc = osd_ost_init(env, dev);
475 /* prepare structures for MDS */
476 rc = osd_mdt_init(env, dev);
481 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
483 struct osd_obj_seq *osd_seq;
485 list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
486 if (osd_seq->oos_seq == seq)
492 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
494 struct osd_obj_seq *osd_seq;
496 read_lock(&map->om_seq_list_lock);
497 osd_seq = osd_seq_find_locked(map, seq);
498 read_unlock(&map->om_seq_list_lock);
502 void osd_obj_map_fini(struct osd_device *dev)
509 * Update the specified OI mapping.
511 * \retval 1, changed nothing
512 * \retval 0, changed successfully
513 * \retval -ve, on error
515 static int osd_obj_update_entry(struct osd_thread_info *info,
516 struct osd_device *osd,
517 struct dentry *dir, const char *name,
518 const struct lu_fid *fid,
519 const struct osd_inode_id *id,
522 struct inode *parent = dir->d_inode;
523 struct dentry *child;
524 struct ldiskfs_dir_entry_2 *de;
525 struct buffer_head *bh;
527 struct dentry *dentry = &info->oti_obj_dentry;
528 struct osd_inode_id *oi_id = &info->oti_id3;
529 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
530 struct lu_fid *oi_fid = &lma->lma_self_fid;
535 LASSERT(th->h_transaction != NULL);
537 child = &info->oti_child_dentry;
538 child->d_parent = dir;
539 child->d_name.hash = 0;
540 child->d_name.name = name;
541 child->d_name.len = strlen(name);
543 ll_vfs_dq_init(parent);
544 mutex_lock(&parent->i_mutex);
545 bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
547 GOTO(out, rc = -ENOENT);
549 if (le32_to_cpu(de->inode) == id->oii_ino)
552 osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
553 inode = osd_iget(info, osd, oi_id);
556 if (rc == -ENOENT || rc == -ESTALE)
561 rc = osd_get_lma(info, inode, dentry, lma);
562 if (rc == -ENODATA) {
563 rc = osd_get_idif(info, inode, dentry, oi_fid);
571 /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
572 * either a crashed object or a uninitialized one. Replace it. */
573 if (rc == -ENODATA || oi_fid == NULL)
579 if (lu_fid_eq(fid, oi_fid)) {
580 CERROR("%s: the FID "DFID" is used by two objects: "
581 "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
582 oi_id->oii_ino, oi_id->oii_gen,
583 id->oii_ino, id->oii_gen);
584 GOTO(out, rc = -EEXIST);
588 /* There may be temporary inconsistency: On one hand, the new
589 * object may be referenced by multiple entries, which is out
590 * of our control unless we traverse the whole /O completely,
591 * which is non-flat order and inefficient, should be avoided;
592 * On the other hand, the old object may become orphan if it
593 * is still valid. Since it was referenced by an invalid entry,
594 * making it as invisible temporary may be not worse. OI scrub
595 * will process it later. */
596 rc = ldiskfs_journal_get_write_access(th, bh);
600 de->inode = cpu_to_le32(id->oii_ino);
601 rc = ldiskfs_journal_dirty_metadata(th, bh);
607 mutex_unlock(&parent->i_mutex);
611 static int osd_obj_del_entry(struct osd_thread_info *info,
612 struct osd_device *osd,
613 struct dentry *dird, char *name,
616 struct ldiskfs_dir_entry_2 *de;
617 struct buffer_head *bh;
618 struct dentry *child;
619 struct inode *dir = dird->d_inode;
624 LASSERT(th->h_transaction != NULL);
627 child = &info->oti_child_dentry;
628 child->d_name.hash = 0;
629 child->d_name.name = name;
630 child->d_name.len = strlen(name);
631 child->d_parent = dird;
632 child->d_inode = NULL;
635 mutex_lock(&dir->i_mutex);
637 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
639 rc = ldiskfs_delete_entry(th, dir, de, bh);
642 mutex_unlock(&dir->i_mutex);
647 int osd_obj_add_entry(struct osd_thread_info *info,
648 struct osd_device *osd,
649 struct dentry *dir, char *name,
650 const struct osd_inode_id *id,
653 struct dentry *child;
659 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
663 LASSERT(th->h_transaction != NULL);
665 inode = &info->oti_inode;
666 inode->i_sb = osd_sb(osd);
667 osd_id_to_inode(inode, id);
668 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
670 child = &info->oti_child_dentry;
671 child->d_name.hash = 0;
672 child->d_name.name = name;
673 child->d_name.len = strlen(name);
674 child->d_parent = dir;
675 child->d_inode = inode;
677 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
680 ll_vfs_dq_init(dir->d_inode);
681 mutex_lock(&dir->d_inode->i_mutex);
682 rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
683 mutex_unlock(&dir->d_inode->i_mutex);
689 * Use LPU64 for legacy OST sequences, but use LPX64i for new
690 * sequences names, so that the O/{seq}/dN/{oid} more closely
691 * follows the DFID/PFID format. This makes it easier to map from
692 * debug messages to objects in the future, and the legacy space
693 * of FID_SEQ_OST_MDT0 will be unused in the future.
695 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
697 snprintf(seq_name, name_size,
698 (fid_seq_is_rsvd(seq) ||
699 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
700 fid_seq_is_idif(seq) ? 0 : seq);
703 static inline void osd_oid_name(char *name, size_t name_size,
704 const struct lu_fid *fid, obd_id id)
706 snprintf(name, name_size,
707 (fid_seq_is_rsvd(fid_seq(fid)) ||
708 fid_seq_is_mdt0(fid_seq(fid)) ||
709 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
712 /* external locking is required */
713 static int osd_seq_load_locked(struct osd_thread_info *info,
714 struct osd_device *osd,
715 struct osd_obj_seq *osd_seq)
717 struct osd_obj_map *map = osd->od_ost_map;
718 struct dentry *seq_dir;
720 struct lu_fid *fid = &info->oti_fid3;
726 if (osd_seq->oos_root != NULL)
730 LASSERT(map->om_root);
732 osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
734 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
736 GOTO(out_err, rc = PTR_ERR(seq_dir));
737 else if (seq_dir->d_inode == NULL)
738 GOTO(out_put, rc = -EFAULT);
740 inode = seq_dir->d_inode;
741 osd_seq->oos_root = seq_dir;
743 /* 'What the @fid is' is not imporatant, because the object
744 * has no OI mapping, and only is visible inside the OSD.*/
745 lu_igif_build(fid, inode->i_ino, inode->i_generation);
746 rc = osd_ea_fid_set(info, inode, fid,
747 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
751 LASSERT(osd_seq->oos_dirs == NULL);
752 OBD_ALLOC(osd_seq->oos_dirs,
753 sizeof(seq_dir) * osd_seq->oos_subdir_count);
754 if (osd_seq->oos_dirs == NULL)
755 GOTO(out_put, rc = -ENOMEM);
757 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
760 snprintf(dir_name, sizeof(dir_name), "d%u", i);
761 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
764 GOTO(out_free, rc = PTR_ERR(dir));
765 } else if (dir->d_inode == NULL) {
767 GOTO(out_free, rc = -EFAULT);
770 inode = dir->d_inode;
771 osd_seq->oos_dirs[i] = dir;
773 /* 'What the @fid is' is not imporatant, because the object
774 * has no OI mapping, and only is visible inside the OSD.*/
775 lu_igif_build(fid, inode->i_ino, inode->i_generation);
776 rc = osd_ea_fid_set(info, inode, fid,
777 LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
784 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
785 if (osd_seq->oos_dirs[i] != NULL)
786 dput(osd_seq->oos_dirs[i]);
788 OBD_FREE(osd_seq->oos_dirs,
789 sizeof(seq_dir) * osd_seq->oos_subdir_count);
792 osd_seq->oos_root = NULL;
798 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
799 struct osd_device *osd, obd_seq seq)
801 struct osd_obj_map *map;
802 struct osd_obj_seq *osd_seq;
806 map = osd->od_ost_map;
808 LASSERT(map->om_root);
810 osd_seq = osd_seq_find(map, seq);
811 if (likely(osd_seq != NULL))
814 /* Serializing init process */
815 mutex_lock(&map->om_dir_init_mutex);
817 /* Check whether the seq has been added */
818 read_lock(&map->om_seq_list_lock);
819 osd_seq = osd_seq_find_locked(map, seq);
820 if (osd_seq != NULL) {
821 read_unlock(&map->om_seq_list_lock);
822 GOTO(cleanup, rc = 0);
824 read_unlock(&map->om_seq_list_lock);
826 OBD_ALLOC_PTR(osd_seq);
828 GOTO(cleanup, rc = -ENOMEM);
830 INIT_LIST_HEAD(&osd_seq->oos_seq_list);
831 osd_seq->oos_seq = seq;
832 /* Init subdir count to be 32, but each seq can have
833 * different subdir count */
834 osd_seq->oos_subdir_count = map->om_subdir_count;
835 rc = osd_seq_load_locked(info, osd, osd_seq);
839 write_lock(&map->om_seq_list_lock);
840 list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
841 write_unlock(&map->om_seq_list_lock);
844 mutex_unlock(&map->om_dir_init_mutex);
847 OBD_FREE_PTR(osd_seq);
854 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
855 const struct lu_fid *fid, struct osd_inode_id *id)
857 struct osd_obj_map *map;
858 struct osd_obj_seq *osd_seq;
859 struct dentry *d_seq;
860 struct dentry *child;
861 struct ost_id *ostid = &info->oti_ostid;
864 struct ldiskfs_dir_entry_2 *de;
865 struct buffer_head *bh;
870 /* on the very first lookup we find and open directories */
872 map = dev->od_ost_map;
874 LASSERT(map->om_root);
876 fid_to_ostid(fid, ostid);
877 osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
879 RETURN(PTR_ERR(osd_seq));
881 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
882 d_seq = osd_seq->oos_dirs[dirn];
885 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
887 child = &info->oti_child_dentry;
888 child->d_parent = d_seq;
889 child->d_name.hash = 0;
890 child->d_name.name = name;
891 /* XXX: we can use rc from sprintf() instead of strlen() */
892 child->d_name.len = strlen(name);
894 dir = d_seq->d_inode;
895 mutex_lock(&dir->i_mutex);
896 bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
897 mutex_unlock(&dir->i_mutex);
902 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
905 inode = osd_iget(info, dev, id);
907 RETURN(PTR_ERR(inode));
913 int osd_obj_map_insert(struct osd_thread_info *info,
914 struct osd_device *osd,
915 const struct lu_fid *fid,
916 const struct osd_inode_id *id,
919 struct osd_obj_map *map;
920 struct osd_obj_seq *osd_seq;
922 struct ost_id *ostid = &info->oti_ostid;
928 map = osd->od_ost_map;
931 /* map fid to seq:objid */
932 fid_to_ostid(fid, ostid);
934 oid = ostid_id(ostid);
935 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
937 RETURN(PTR_ERR(osd_seq));
939 dirn = oid & (osd_seq->oos_subdir_count - 1);
940 d = osd_seq->oos_dirs[dirn];
943 osd_oid_name(name, sizeof(name), fid, oid);
946 rc = osd_obj_add_entry(info, osd, d, name, id, th);
948 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
949 if (unlikely(rc == -ENOENT))
952 if (unlikely(rc == 1))
959 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
960 const struct lu_fid *fid, handle_t *th)
962 struct osd_obj_map *map;
963 struct osd_obj_seq *osd_seq;
965 struct ost_id *ostid = &info->oti_ostid;
970 map = osd->od_ost_map;
973 /* map fid to seq:objid */
974 fid_to_ostid(fid, ostid);
976 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
978 GOTO(cleanup, rc = PTR_ERR(osd_seq));
980 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
981 d = osd_seq->oos_dirs[dirn];
984 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
985 rc = osd_obj_del_entry(info, osd, d, name, th);
990 int osd_obj_map_update(struct osd_thread_info *info,
991 struct osd_device *osd,
992 const struct lu_fid *fid,
993 const struct osd_inode_id *id,
996 struct osd_obj_seq *osd_seq;
998 struct ost_id *ostid = &info->oti_ostid;
1003 fid_to_ostid(fid, ostid);
1004 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1005 if (IS_ERR(osd_seq))
1006 RETURN(PTR_ERR(osd_seq));
1008 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1009 d = osd_seq->oos_dirs[dirn];
1012 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1013 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1018 int osd_obj_map_recover(struct osd_thread_info *info,
1019 struct osd_device *osd,
1020 struct inode *src_parent,
1021 struct dentry *src_child,
1022 const struct lu_fid *fid)
1024 struct osd_obj_seq *osd_seq;
1025 struct dentry *tgt_parent;
1026 struct dentry *tgt_child = &info->oti_child_dentry;
1028 struct inode *inode = src_child->d_inode;
1029 struct ost_id *ostid = &info->oti_ostid;
1031 struct ldiskfs_dir_entry_2 *de;
1032 struct buffer_head *bh;
1038 if (fid_is_last_id(fid)) {
1039 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1040 if (IS_ERR(osd_seq))
1041 RETURN(PTR_ERR(osd_seq));
1043 tgt_parent = osd_seq->oos_root;
1044 tgt_child->d_name.name = "LAST_ID";
1045 tgt_child->d_name.len = strlen("LAST_ID");
1047 fid_to_ostid(fid, ostid);
1048 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1049 if (IS_ERR(osd_seq))
1050 RETURN(PTR_ERR(osd_seq));
1052 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1053 tgt_parent = osd_seq->oos_dirs[dirn];
1054 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1055 tgt_child->d_name.name = name;
1056 tgt_child->d_name.len = strlen(name);
1058 LASSERT(tgt_parent != NULL);
1060 dir = tgt_parent->d_inode;
1061 tgt_child->d_name.hash = 0;
1062 tgt_child->d_parent = tgt_parent;
1063 tgt_child->d_inode = inode;
1065 /* The non-initialized src_child may be destroyed. */
1066 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1067 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1068 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1069 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1071 RETURN(PTR_ERR(jh));
1073 ll_vfs_dq_init(src_parent);
1074 ll_vfs_dq_init(dir);
1076 mutex_lock(&src_parent->i_mutex);
1077 mutex_lock(&dir->i_mutex);
1078 bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1080 /* XXX: If some other object occupied the same slot. And If such
1081 * inode is zero-sized and with SUID+SGID, then means it is
1082 * a new created one. Maybe we can remove it and insert the
1083 * original one back to the /O/<seq>/d<x>. But there are
1084 * something to be considered:
1086 * 1) The OST-object under /lost+found has crashed LMA.
1087 * So it should not conflict with the current one.
1089 * 2) There are race conditions that: someone may just want
1090 * to modify the current one. Even if the OI scrub takes
1091 * the object lock when remove the current one, it still
1092 * cause the modification to be lost becasue the target
1093 * has been removed when the RPC service thread waiting
1096 * So keep it there before we have suitable solution. */
1098 mutex_unlock(&dir->i_mutex);
1099 mutex_unlock(&src_parent->i_mutex);
1100 ldiskfs_journal_stop(jh);
1103 /* If the src object has never been modified, then remove it. */
1104 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1105 inode->i_mode & S_ISGID) {
1106 rc = ll_vfs_unlink(src_parent, src_child);
1107 if (unlikely(rc == -ENOENT))
1113 bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1115 if (unlikely(bh == NULL))
1116 GOTO(unlock, rc = -ENOENT);
1118 rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1123 rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1128 mutex_unlock(&dir->i_mutex);
1129 mutex_unlock(&src_parent->i_mutex);
1130 ldiskfs_journal_stop(jh);
1134 static struct dentry *
1135 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1136 const struct lu_fid *fid, char **name)
1138 struct dentry *root = ERR_PTR(-ENOENT);
1140 if (fid_is_last_id(fid)) {
1141 struct osd_obj_seq *osd_seq;
1143 /* on creation of LAST_ID we create O/<seq> hierarchy */
1144 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1145 if (IS_ERR(osd_seq))
1146 RETURN((struct dentry *)osd_seq);
1149 root = osd_seq->oos_root;
1151 *name = osd_lf_fid2name(fid);
1153 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1154 else if ((*name)[0])
1155 root = osd_sb(osd)->s_root;
1161 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1162 const struct lu_fid *fid, const struct osd_inode_id *id,
1165 struct dentry *root;
1170 root = osd_object_spec_find(info, osd, fid, &name);
1171 if (!IS_ERR(root)) {
1172 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1182 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1183 const struct lu_fid *fid, const struct osd_inode_id *id,
1186 struct dentry *root;
1191 root = osd_object_spec_find(info, osd, fid, &name);
1192 if (!IS_ERR(root)) {
1193 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1203 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1204 const struct lu_fid *fid, struct osd_inode_id *id)
1206 struct dentry *root;
1207 struct dentry *dentry;
1208 struct inode *inode;
1213 if (fid_is_last_id(fid)) {
1214 struct osd_obj_seq *osd_seq;
1216 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1217 if (IS_ERR(osd_seq))
1218 RETURN(PTR_ERR(osd_seq));
1219 root = osd_seq->oos_root;
1222 root = osd_sb(osd)->s_root;
1223 name = osd_lf_fid2name(fid);
1224 if (name == NULL || strlen(name) == 0)
1228 dentry = ll_lookup_one_len(name, root, strlen(name));
1229 if (!IS_ERR(dentry)) {
1230 inode = dentry->d_inode;
1232 if (is_bad_inode(inode)) {
1235 osd_id_gen(id, inode->i_ino,
1236 inode->i_generation);
1240 /* if dentry is accessible after osd_compat_spec_insert it
1241 * will still contain NULL inode, so don't keep it in cache */
1242 d_invalidate(dentry);