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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, 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_handler.c
34 * Top-level entry points into osd module
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
40 #define DEBUG_SUBSYSTEM S_OSD
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #ifdef HAVE_UIDGID_HEADER
45 # include <linux/uidgid.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
57 #include <ldiskfs/ldiskfs.h>
58 #include <ldiskfs/xattr.h>
59 #include <ldiskfs/ldiskfs_extents.h>
62 * struct OBD_{ALLOC,FREE}*()
65 #include <obd_support.h>
66 /* struct ptlrpc_thread */
67 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include <lustre_param.h>
72 #include "osd_internal.h"
73 #include "osd_dynlocks.h"
75 /* llo_* api support */
76 #include <md_object.h>
77 #include <lustre_quota.h>
79 #include <lustre_linkea.h>
82 module_param(ldiskfs_pdo, int, 0644);
83 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
85 int ldiskfs_track_declares_assert;
86 module_param(ldiskfs_track_declares_assert, int, 0644);
87 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
89 /* Slab to allocate dynlocks */
90 struct kmem_cache *dynlock_cachep;
92 /* Slab to allocate osd_it_ea */
93 struct kmem_cache *osd_itea_cachep;
95 static struct lu_kmem_descr ldiskfs_caches[] = {
97 .ckd_cache = &dynlock_cachep,
98 .ckd_name = "dynlock_cache",
99 .ckd_size = sizeof(struct dynlock_handle)
102 .ckd_cache = &osd_itea_cachep,
103 .ckd_name = "osd_itea_cache",
104 .ckd_size = sizeof(struct osd_it_ea)
111 static const char dot[] = ".";
112 static const char dotdot[] = "..";
113 static const char remote_obj_dir[] = "REM_OBJ_DIR";
115 static const struct lu_object_operations osd_lu_obj_ops;
116 static const struct dt_object_operations osd_obj_ops;
117 static const struct dt_object_operations osd_obj_ea_ops;
118 static const struct dt_object_operations osd_obj_otable_it_ops;
119 static const struct dt_index_operations osd_index_iam_ops;
120 static const struct dt_index_operations osd_index_ea_ops;
122 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
123 const struct lu_fid *fid);
124 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
125 struct osd_device *osd);
127 int osd_trans_declare_op2rb[] = {
128 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
129 [OSD_OT_PUNCH] = OSD_OT_MAX,
130 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
131 [OSD_OT_CREATE] = OSD_OT_DESTROY,
132 [OSD_OT_DESTROY] = OSD_OT_CREATE,
133 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
134 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
135 [OSD_OT_WRITE] = OSD_OT_WRITE,
136 [OSD_OT_INSERT] = OSD_OT_DELETE,
137 [OSD_OT_DELETE] = OSD_OT_INSERT,
138 [OSD_OT_QUOTA] = OSD_OT_MAX,
141 static int osd_has_index(const struct osd_object *obj)
143 return obj->oo_dt.do_index_ops != NULL;
146 static int osd_object_invariant(const struct lu_object *l)
148 return osd_invariant(osd_obj(l));
152 * Concurrency: doesn't matter
156 * Concurrency: doesn't matter
158 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
160 struct osd_thread_info *oti = osd_oti_get(env);
161 return oti->oti_w_locks > 0 && o->oo_owner == env;
165 * Concurrency: doesn't access mutable data
167 static int osd_root_get(const struct lu_env *env,
168 struct dt_device *dev, struct lu_fid *f)
170 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
175 * the following set of functions are used to maintain per-thread
176 * cache of FID->ino mapping. this mechanism is needed to resolve
177 * FID to inode at dt_insert() which in turn stores ino in the
178 * directory entries to keep ldiskfs compatible with ext[34].
179 * due to locking-originated restrictions we can't lookup ino
180 * using LU cache (deadlock is possible). lookup using OI is quite
181 * expensive. so instead we maintain this cache and methods like
182 * dt_create() fill it. so in the majority of cases dt_insert() is
183 * able to find needed mapping in lockless manner.
185 static struct osd_idmap_cache *
186 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
187 const struct lu_fid *fid)
189 struct osd_thread_info *oti = osd_oti_get(env);
190 struct osd_idmap_cache *idc = oti->oti_ins_cache;
192 for (i = 0; i < oti->oti_ins_cache_used; i++) {
193 if (!lu_fid_eq(&idc[i].oic_fid, fid))
195 if (idc[i].oic_dev != osd)
204 static struct osd_idmap_cache *
205 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
206 const struct lu_fid *fid)
208 struct osd_thread_info *oti = osd_oti_get(env);
209 struct osd_idmap_cache *idc;
212 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
213 i = oti->oti_ins_cache_size * 2;
215 i = OSD_INS_CACHE_SIZE;
216 OBD_ALLOC(idc, sizeof(*idc) * i);
218 return ERR_PTR(-ENOMEM);
219 if (oti->oti_ins_cache != NULL) {
220 memcpy(idc, oti->oti_ins_cache,
221 oti->oti_ins_cache_used * sizeof(*idc));
222 OBD_FREE(oti->oti_ins_cache,
223 oti->oti_ins_cache_used * sizeof(*idc));
225 oti->oti_ins_cache = idc;
226 oti->oti_ins_cache_size = i;
229 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
232 idc->oic_lid.oii_ino = 0;
233 idc->oic_lid.oii_gen = 0;
240 * lookup mapping for the given fid in the cache, initialize a
241 * new one if not found. the initialization checks whether the
242 * object is local or remote. for local objects, OI is used to
243 * learn ino/generation. the function is used when the caller
244 * has no information about the object, e.g. at dt_insert().
246 static struct osd_idmap_cache *
247 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
248 const struct lu_fid *fid)
250 struct osd_idmap_cache *idc;
253 idc = osd_idc_find(env, osd, fid);
254 LASSERT(!IS_ERR(idc));
258 /* new mapping is needed */
259 idc = osd_idc_add(env, osd, fid);
264 rc = osd_remote_fid(env, osd, fid);
265 if (unlikely(rc < 0))
269 /* the object is local, lookup in OI */
270 /* XXX: probably cheaper to lookup in LU first? */
271 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
273 if (unlikely(rc < 0)) {
274 CERROR("can't lookup: rc = %d\n", rc);
278 /* the object is remote */
286 * lookup mapping for given FID and fill it from the given object.
287 * the object is lolcal by definition.
289 static int osd_idc_find_and_init(const struct lu_env *env,
290 struct osd_device *osd,
291 struct osd_object *obj)
293 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
294 struct osd_idmap_cache *idc;
296 idc = osd_idc_find(env, osd, fid);
297 LASSERT(!IS_ERR(idc));
299 if (obj->oo_inode == NULL)
301 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
302 LASSERT(idc->oic_lid.oii_ino == 0);
303 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
304 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
309 /* new mapping is needed */
310 idc = osd_idc_add(env, osd, fid);
314 if (obj->oo_inode != NULL) {
315 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
316 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
322 * OSD object methods.
326 * Concurrency: no concurrent access is possible that early in object
329 static struct lu_object *osd_object_alloc(const struct lu_env *env,
330 const struct lu_object_header *hdr,
333 struct osd_object *mo;
339 l = &mo->oo_dt.do_lu;
340 dt_object_init(&mo->oo_dt, NULL, d);
341 mo->oo_dt.do_ops = &osd_obj_ea_ops;
342 l->lo_ops = &osd_lu_obj_ops;
343 init_rwsem(&mo->oo_sem);
344 init_rwsem(&mo->oo_ext_idx_sem);
345 spin_lock_init(&mo->oo_guard);
346 INIT_LIST_HEAD(&mo->oo_xattr_list);
353 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
354 struct dentry *dentry, struct lustre_mdt_attrs *lma)
358 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
359 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
360 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
362 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
363 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
365 lustre_lma_swab(lma);
366 /* Check LMA compatibility */
367 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
368 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
369 "for fid = "DFID", ino = %lu\n",
370 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
371 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
372 PFID(&lma->lma_self_fid), inode->i_ino);
375 } else if (rc == 0) {
383 * retrieve object from backend ext fs.
385 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
386 struct osd_inode_id *id)
388 struct inode *inode = NULL;
390 /* if we look for an inode withing a running
391 * transaction, then we risk to deadlock */
392 /* osd_dirent_check_repair() breaks this */
393 /*LASSERT(current->journal_info == NULL);*/
395 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
397 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
398 id->oii_ino, PTR_ERR(inode));
399 } else if (id->oii_gen != OSD_OII_NOGEN &&
400 inode->i_generation != id->oii_gen) {
401 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
402 "i_generation = %u\n",
403 id->oii_ino, id->oii_gen, inode->i_generation);
405 inode = ERR_PTR(-ESTALE);
406 } else if (inode->i_nlink == 0) {
407 /* due to parallel readdir and unlink,
408 * we can have dead inode here. */
409 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
411 inode = ERR_PTR(-ESTALE);
412 } else if (is_bad_inode(inode)) {
413 CWARN("%.16s: bad inode: ino = %u\n",
414 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
416 inode = ERR_PTR(-ENOENT);
418 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
419 if (id->oii_gen == OSD_OII_NOGEN)
420 osd_id_gen(id, inode->i_ino, inode->i_generation);
422 /* Do not update file c/mtime in ldiskfs.
423 * NB: we don't have any lock to protect this because we don't
424 * have reference on osd_object now, but contention with
425 * another lookup + attr_set can't happen in the tiny window
426 * between if (...) and set S_NOCMTIME. */
427 if (!(inode->i_flags & S_NOCMTIME))
428 inode->i_flags |= S_NOCMTIME;
433 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
434 handle_t *handle, struct dentry *child,
435 struct inode *inode, struct htree_lock *hlock)
439 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
440 if (rc == -ENOBUFS || rc == -ENOSPC) {
441 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
442 struct inode *parent = child->d_parent->d_inode;
443 struct lu_fid *fid = NULL;
445 rc2 = osd_get_lma(info, parent, child->d_parent, lma);
447 fid = &lma->lma_self_fid;
448 } else if (rc2 == -ENODATA) {
449 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
450 fid = &info->oti_fid3;
451 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
452 } else if (!osd->od_is_ost && osd->od_index == 0) {
453 fid = &info->oti_fid3;
454 lu_igif_build(fid, parent->i_ino,
455 parent->i_generation);
460 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
461 "maximum entry limit\n",
462 osd_name(osd), parent->i_ino, PFID(fid),
463 rc == -ENOSPC ? "has reached" : "is approaching");
465 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
466 "maximum entry limit\n",
467 osd_name(osd), parent->i_ino,
468 rc == -ENOSPC ? "has reached" : "is approaching");
470 /* ignore such error now */
479 static struct inode *
480 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
481 struct osd_inode_id *id, struct lu_fid *fid)
483 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
487 inode = osd_iget(info, dev, id);
491 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
493 *fid = lma->lma_self_fid;
494 } else if (rc == -ENODATA) {
495 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
496 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
498 lu_igif_build(fid, inode->i_ino, inode->i_generation);
506 static struct inode *osd_iget_check(struct osd_thread_info *info,
507 struct osd_device *dev,
508 const struct lu_fid *fid,
509 struct osd_inode_id *id,
516 /* The cached OI mapping is trustable. If we cannot locate the inode
517 * via the cached OI mapping, then return the failure to the caller
518 * directly without further OI checking. */
520 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
523 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
524 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
533 if (is_bad_inode(inode)) {
536 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
544 if (id->oii_gen != OSD_OII_NOGEN &&
545 inode->i_generation != id->oii_gen) {
548 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
549 "oii_gen = %u, i_generation = %u\n",
550 id->oii_ino, id->oii_gen, inode->i_generation);
558 if (inode->i_nlink == 0) {
561 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
569 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
573 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
575 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
576 /* XXX: There are four possible cases:
578 * Backup/restore caused the OI invalid.
580 * Someone unlinked the object but NOT removed
581 * the OI mapping, such as mount target device
582 * as ldiskfs, and modify something directly.
584 * Someone just removed the object between the
585 * former oi_lookup and the iget. It is normal.
586 * 4. Other failure cases.
588 * Generally, when the device is mounted, it will
589 * auto check whether the system is restored from
590 * file-level backup or not. We trust such detect
591 * to distinguish the 1st case from the 2nd case:
592 * if the OI files are consistent but may contain
593 * stale OI mappings because of case 2, if iget()
594 * returns -ENOENT or -ESTALE, then it should be
597 /* If the OI mapping was in OI file before the
598 * osd_iget_check(), but now, it is disappear,
599 * then it must be removed by race. That is a
600 * normal race case. */
603 if ((!IS_ERR(inode) && inode->i_generation != 0 &&
604 inode->i_generation == id->oii_gen) ||
605 (IS_ERR(inode) && !(dev->od_scrub.os_file.sf_flags &
611 if (id->oii_gen == OSD_OII_NOGEN)
612 osd_id_gen(id, inode->i_ino, inode->i_generation);
614 /* Do not update file c/mtime in ldiskfs.
615 * NB: we don't have any lock to protect this because we don't
616 * have reference on osd_object now, but contention with
617 * another lookup + attr_set can't happen in the tiny window
618 * between if (...) and set S_NOCMTIME. */
619 if (!(inode->i_flags & S_NOCMTIME))
620 inode->i_flags |= S_NOCMTIME;
637 * \retval +v: new filter_fid, does not contain self-fid
638 * \retval 0: filter_fid_old, contains self-fid
639 * \retval -v: other failure cases
641 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
642 struct dentry *dentry, struct lu_fid *fid)
644 struct filter_fid_old *ff = &info->oti_ff;
645 struct ost_id *ostid = &info->oti_ostid;
648 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
649 if (rc == sizeof(*ff)) {
651 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
652 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
653 /* XXX: use 0 as the index for compatibility, the caller will
654 * handle index related issues when necessarry. */
655 ostid_to_fid(fid, ostid, 0);
656 } else if (rc == sizeof(struct filter_fid)) {
658 } else if (rc >= 0) {
665 static int osd_lma_self_repair(struct osd_thread_info *info,
666 struct osd_device *osd, struct inode *inode,
667 const struct lu_fid *fid, __u32 compat)
672 LASSERT(current->journal_info == NULL);
674 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
675 osd_dto_credits_noquota[DTO_XATTR_SET]);
678 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
683 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
685 CWARN("%s: cannot self repair the LMA: rc = %d\n",
687 ldiskfs_journal_stop(jh);
691 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
693 struct osd_thread_info *info = osd_oti_get(env);
694 struct osd_device *osd = osd_obj2dev(obj);
695 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
696 struct inode *inode = obj->oo_inode;
697 struct dentry *dentry = &info->oti_obj_dentry;
698 struct lu_fid *fid = NULL;
699 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
703 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
704 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
705 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
706 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
707 fid = &lma->lma_self_fid;
708 rc = osd_get_idif(info, inode, dentry, fid);
709 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
710 /* For the given OST-object, if it has neither LMA nor
711 * FID in XATTR_NAME_FID, then the given FID (which is
712 * contained in the @obj, from client RPC for locating
713 * the OST-object) is trusted. We use it to generate
715 osd_lma_self_repair(info, osd, inode, rfid,
726 lustre_lma_swab(lma);
727 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
728 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
729 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
730 "fid = "DFID", ino = %lu\n", osd_name(osd),
731 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
732 PFID(rfid), inode->i_ino);
735 fid = &lma->lma_self_fid;
739 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
740 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
741 struct ost_id *oi = &info->oti_ostid;
742 struct lu_fid *fid1 = &info->oti_fid3;
743 __u32 idx = fid_idif_ost_idx(rfid);
745 /* For old IDIF, the OST index is not part of the IDIF,
746 * Means that different OSTs may have the same IDIFs.
747 * Under such case, we need to make some compatible
748 * check to make sure to trigger OI scrub properly. */
749 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
750 /* Given @rfid is new, LMA is old. */
751 fid_to_ostid(fid, oi);
752 ostid_to_fid(fid1, oi, idx);
753 if (lu_fid_eq(fid1, rfid)) {
754 if (osd->od_index_in_idif)
755 osd_lma_self_repair(info, osd,
769 struct osd_check_lmv_buf {
770 #ifdef HAVE_DIR_CONTEXT
771 /* please keep it as first member */
772 struct dir_context ctx;
774 struct osd_thread_info *oclb_info;
775 struct osd_device *oclb_dev;
776 struct osd_idmap_cache *oclb_oic;
780 * It is called internally by ->readdir() to filter out the
781 * local slave object's FID of the striped directory.
783 * \retval 1 found the local slave's FID
784 * \retval 0 continue to check next item
785 * \retval -ve for failure
787 #ifdef HAVE_FILLDIR_USE_CTX
788 static int osd_stripe_dir_filldir(struct dir_context *buf,
790 static int osd_stripe_dir_filldir(void *buf,
792 const char *name, int namelen,
793 loff_t offset, __u64 ino, unsigned d_type)
795 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
796 struct osd_thread_info *oti = oclb->oclb_info;
797 struct lu_fid *fid = &oti->oti_fid3;
798 struct osd_inode_id *id = &oti->oti_id3;
799 struct osd_device *dev = oclb->oclb_dev;
800 struct osd_idmap_cache *oic = oclb->oclb_oic;
808 sscanf(name + 1, SFID, RFID(fid));
809 if (!fid_is_sane(fid))
812 if (osd_remote_fid(oti->oti_env, dev, fid))
815 osd_id_gen(id, ino, OSD_OII_NOGEN);
816 inode = osd_iget(oti, dev, id);
818 return PTR_ERR(inode);
821 osd_add_oi_cache(oti, dev, id, fid);
825 rc = osd_oii_insert(dev, oic, true);
827 return rc == 0 ? 1 : rc;
830 /* When lookup item under striped directory, we need to locate the master
831 * MDT-object of the striped directory firstly, then the client will send
832 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
833 * and the item's name. If the system is restored from MDT file level backup,
834 * then before the OI scrub completely built the OI files, the OI mappings of
835 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
836 * not a problem for the master MDT-object. Because when locate the master
837 * MDT-object, we will do name based lookup (for the striped directory itself)
838 * firstly, during such process we can setup the correct OI mapping for the
839 * master MDT-object. But it will be trouble for the slave MDT-object. Because
840 * the client will not trigger name based lookup on the MDT to locate the slave
841 * MDT-object before locating item under the striped directory, then when
842 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
843 * is invalid and does not know what the right OI mapping is, then the MDT has
844 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
845 * the OI file, related OI mapping is unknown yet, please try again later. And
846 * then client will re-try the RPC again and again until related OI mapping has
847 * been updated. That is quite inefficient.
849 * To resolve above trouble, we will handle it as the following two cases:
851 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
852 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
853 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
854 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
855 * directly. Please check osd_fid_lookup().
857 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
858 * Under such case, during lookup the master MDT-object, we will lookup the
859 * slave MDT-object via readdir against the master MDT-object, because the
860 * slave MDT-objects information are stored as sub-directories with the name
861 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
862 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
863 * the correct OI mapping for the slave MDT-object. */
864 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
865 struct inode *inode, struct osd_idmap_cache *oic)
867 struct lu_buf *buf = &oti->oti_big_buf;
868 struct dentry *dentry = &oti->oti_obj_dentry;
869 struct file *filp = &oti->oti_file;
870 const struct file_operations *fops;
871 struct lmv_mds_md_v1 *lmv1;
872 struct osd_check_lmv_buf oclb = {
873 #ifdef HAVE_DIR_CONTEXT
874 .ctx.actor = osd_stripe_dir_filldir,
884 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
887 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
889 lu_buf_realloc(buf, rc);
890 if (buf->lb_buf == NULL)
891 GOTO(out, rc = -ENOMEM);
897 if (unlikely(rc == 0 || rc == -ENODATA))
903 if (unlikely(buf->lb_buf == NULL)) {
904 lu_buf_realloc(buf, rc);
905 if (buf->lb_buf == NULL)
906 GOTO(out, rc = -ENOMEM);
912 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
916 dentry->d_inode = inode;
917 dentry->d_sb = inode->i_sb;
919 filp->f_path.dentry = dentry;
920 filp->f_mode = FMODE_64BITHASH;
921 filp->f_mapping = inode->i_mapping;
923 filp->private_data = NULL;
924 set_file_inode(filp, inode);
926 #ifdef HAVE_DIR_CONTEXT
927 oclb.ctx.pos = filp->f_pos;
928 rc = fops->iterate(filp, &oclb.ctx);
929 filp->f_pos = oclb.ctx.pos;
931 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
933 fops->release(inode, filp);
937 CDEBUG(D_LFSCK, "%.16s: fail to check LMV EA, inode = %lu/%u,"
939 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
940 inode->i_ino, inode->i_generation,
941 PFID(&oic->oic_fid), rc);
948 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
949 const struct lu_fid *fid,
950 const struct lu_object_conf *conf)
952 struct osd_thread_info *info;
953 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
954 struct osd_device *dev;
955 struct osd_idmap_cache *oic;
956 struct osd_inode_id *id;
957 struct osd_inode_id *tid;
958 struct inode *inode = NULL;
959 struct osd_scrub *scrub;
960 struct scrub_file *sf;
961 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT |
971 LINVRNT(osd_invariant(obj));
972 LASSERT(obj->oo_inode == NULL);
973 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
976 scrub = &dev->od_scrub;
977 sf = &scrub->os_file;
978 info = osd_oti_get(env);
980 oic = &info->oti_cache;
982 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
985 /* For the object is created as locking anchor, or for the object to
986 * be created on disk. No need to osd_oi_lookup() at here because FID
987 * shouldn't never be re-used, if it's really a duplicate FID from
988 * unexpected reason, we should be able to detect it later by calling
989 * do_create->osd_oi_insert(). */
990 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
991 GOTO(out, result = 0);
993 /* Search order: 1. per-thread cache. */
994 if (lu_fid_eq(fid, &oic->oic_fid) &&
995 likely(oic->oic_dev == dev)) {
1001 if (!list_empty(&scrub->os_inconsistent_items)) {
1002 /* Search order: 2. OI scrub pending list. */
1003 result = osd_oii_lookup(dev, fid, id);
1009 /* Search order: 3. OI files. */
1010 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1011 if (result == -ENOENT) {
1012 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1013 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1014 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1016 GOTO(out, result = 0);
1025 inode = osd_iget_check(info, dev, fid, id, cached);
1026 if (IS_ERR(inode)) {
1027 result = PTR_ERR(inode);
1028 if (result == -ENOENT || result == -ESTALE)
1029 GOTO(out, result = 0);
1031 if (result == -EREMCHG) {
1034 /* We still have chance to get the valid inode: for the
1035 * object which is referenced by remote name entry, the
1036 * object on the local MDT will be linked under the dir
1037 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1039 * We do not know whether the object for the given FID
1040 * is referenced by some remote name entry or not, and
1041 * especially for DNE II, a multiple-linked object may
1042 * have many name entries reside on many MDTs.
1044 * To simplify the operation, OSD will not distinguish
1045 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1046 * only happened for the RPC from other MDT during the
1047 * OI scrub, or for the client side RPC with FID only,
1048 * such as FID to path, or from old connected client. */
1050 !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1051 rc1 = osd_lookup_in_remote_parent(info, dev,
1056 flags |= SS_AUTO_PARTIAL;
1057 flags &= ~SS_AUTO_FULL;
1062 if (thread_is_running(&scrub->os_thread)) {
1063 if (scrub->os_partial_scan &&
1064 !scrub->os_in_join) {
1067 if (inode != NULL && !IS_ERR(inode)) {
1070 osd_add_oi_cache(info, dev, id,
1072 osd_oii_insert(dev, oic, true);
1074 result = -EINPROGRESS;
1077 } else if (!dev->od_noscrub) {
1080 rc1 = osd_scrub_start(dev, flags);
1081 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
1082 "for the "DFID" with flags 0x%x,"
1083 " rc = %d\n", osd_name(dev),
1084 PFID(fid), flags, rc1);
1085 if (rc1 == 0 || rc1 == -EALREADY) {
1086 if (inode != NULL && !IS_ERR(inode)) {
1089 osd_add_oi_cache(info, dev, id,
1091 osd_oii_insert(dev, oic, true);
1093 result = -EINPROGRESS;
1103 if (inode == NULL || IS_ERR(inode))
1105 } else if (remote) {
1109 obj->oo_inode = inode;
1110 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
1112 result = osd_check_lma(env, obj);
1116 tid = &info->oti_id3;
1119 if (result == -ENODATA) {
1121 /* The current OI mapping is from the OI file,
1122 * since the inode has been found via
1123 * osd_iget_check(), no need recheck OI. */
1126 result = osd_oi_lookup(info, dev, fid, tid, OI_CHECK_FLD);
1128 LASSERTF(tid->oii_ino == id->oii_ino &&
1129 tid->oii_gen == id->oii_gen,
1130 "OI mapping changed(1): %u/%u => %u/%u",
1131 tid->oii_ino, tid->oii_gen,
1132 id->oii_ino, id->oii_gen);
1134 LASSERTF(tid->oii_ino == inode->i_ino &&
1135 tid->oii_gen == inode->i_generation,
1136 "locate wrong inode(1): %u/%u => %ld/%u",
1137 tid->oii_ino, tid->oii_gen,
1138 inode->i_ino, inode->i_generation);
1140 /* "result == 0" means the cached OI mapping is still in
1141 * the OI file, so the target the inode is valid. */
1145 /* "result == -ENOENT" means that the OI mappinghas been removed
1146 * by race, the target inode belongs to other object.
1148 * Others error can be returned directly. */
1149 if (result == -ENOENT)
1153 saved_ino = inode->i_ino;
1154 saved_gen = inode->i_generation;
1157 obj->oo_inode = NULL;
1159 if (result != -EREMCHG)
1163 /* The current OI mapping is from the OI file,
1164 * since the inode has been found via
1165 * osd_iget_check(), no need recheck OI. */
1168 result = osd_oi_lookup(info, dev, fid, tid, OI_CHECK_FLD);
1169 /* "result == -ENOENT" means the cached OI mapping has been removed from
1170 * the OI file by race, above target inode belongs to other object.
1172 * Others error can be returned directly. */
1174 GOTO(out, result = (result == -ENOENT ? 0 : result));
1176 LASSERTF(tid->oii_ino == id->oii_ino && tid->oii_gen == id->oii_gen,
1177 "OI mapping changed(2): %u/%u => %u/%u",
1178 tid->oii_ino, tid->oii_gen, id->oii_ino, id->oii_gen);
1180 LASSERTF(tid->oii_ino == saved_ino && tid->oii_gen == saved_gen,
1181 "locate wrong inode(2): %u/%u => %u/%u",
1182 tid->oii_ino, tid->oii_gen, saved_ino, saved_gen);
1187 obj->oo_compat_dot_created = 1;
1188 obj->oo_compat_dotdot_created = 1;
1190 if (S_ISDIR(inode->i_mode) &&
1191 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1192 osd_check_lmv(info, dev, inode, oic);
1195 GOTO(out, result = 0);
1197 LASSERT(obj->oo_hl_head == NULL);
1198 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1199 if (obj->oo_hl_head == NULL) {
1200 obj->oo_inode = NULL;
1202 GOTO(out, result = -ENOMEM);
1204 GOTO(out, result = 0);
1207 if (result != 0 && cached)
1208 fid_zero(&oic->oic_fid);
1210 LINVRNT(osd_invariant(obj));
1215 * Concurrency: shouldn't matter.
1217 static void osd_object_init0(struct osd_object *obj)
1219 LASSERT(obj->oo_inode != NULL);
1220 obj->oo_dt.do_body_ops = &osd_body_ops;
1221 obj->oo_dt.do_lu.lo_header->loh_attr |=
1222 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1226 * Concurrency: no concurrent access is possible that early in object
1229 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1230 const struct lu_object_conf *conf)
1232 struct osd_object *obj = osd_obj(l);
1235 LINVRNT(osd_invariant(obj));
1237 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1238 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1239 l->lo_header->loh_attr |= LOHA_EXISTS;
1243 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1244 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1245 if (result == 0 && obj->oo_inode != NULL) {
1246 struct osd_thread_info *oti = osd_oti_get(env);
1247 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1249 osd_object_init0(obj);
1250 result = osd_get_lma(oti, obj->oo_inode,
1251 &oti->oti_obj_dentry, lma);
1253 /* Convert LMAI flags to lustre LMA flags
1254 * and cache it to oo_lma_flags */
1256 lma_to_lustre_flags(lma->lma_incompat);
1257 } else if (result == -ENODATA) {
1262 LINVRNT(osd_invariant(obj));
1266 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1267 * The left part is for value, binary mode. */
1268 struct osd_xattr_entry {
1269 struct list_head oxe_list;
1273 struct rcu_head oxe_rcu;
1277 static int osd_oxc_get(struct osd_object *obj, const char *name,
1280 struct osd_xattr_entry *tmp;
1281 struct osd_xattr_entry *oxe = NULL;
1282 size_t namelen = strlen(name);
1287 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1288 if (namelen == tmp->oxe_namelen &&
1289 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1296 GOTO(out, rc = -ENOENT);
1298 if (!oxe->oxe_exist)
1299 GOTO(out, rc = -ENODATA);
1302 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1305 if (buf->lb_buf == NULL)
1308 if (buf->lb_len < rc)
1309 GOTO(out, rc = -ERANGE);
1311 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1319 static void osd_oxc_free(struct rcu_head *head)
1321 struct osd_xattr_entry *oxe;
1323 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1324 OBD_FREE(oxe, oxe->oxe_len);
1327 static void osd_oxc_add(struct osd_object *obj, const char *name,
1328 const char *buf, int buflen)
1330 struct osd_xattr_entry *oxe;
1331 struct osd_xattr_entry *old = NULL;
1332 struct osd_xattr_entry *tmp;
1333 size_t namelen = strlen(name);
1334 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1336 OBD_ALLOC(oxe, len);
1340 INIT_LIST_HEAD(&oxe->oxe_list);
1342 oxe->oxe_namelen = namelen;
1343 memcpy(oxe->oxe_buf, name, namelen);
1345 LASSERT(buf != NULL);
1346 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1347 oxe->oxe_exist = true;
1349 oxe->oxe_exist = false;
1352 /* this should be rarely called, just remove old and add new */
1353 spin_lock(&obj->oo_guard);
1354 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1355 if (namelen == tmp->oxe_namelen &&
1356 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1362 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1363 call_rcu(&old->oxe_rcu, osd_oxc_free);
1365 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1367 spin_unlock(&obj->oo_guard);
1370 static void osd_oxc_del(struct osd_object *obj, const char *name)
1372 struct osd_xattr_entry *oxe;
1373 size_t namelen = strlen(name);
1375 spin_lock(&obj->oo_guard);
1376 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1377 if (namelen == oxe->oxe_namelen &&
1378 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1379 list_del_rcu(&oxe->oxe_list);
1380 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1384 spin_unlock(&obj->oo_guard);
1387 static void osd_oxc_fini(struct osd_object *obj)
1389 struct osd_xattr_entry *oxe, *next;
1391 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1392 list_del(&oxe->oxe_list);
1393 OBD_FREE(oxe, oxe->oxe_len);
1398 * Concurrency: no concurrent access is possible that late in object
1401 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1403 struct osd_object *obj = osd_obj(l);
1405 LINVRNT(osd_invariant(obj));
1408 dt_object_fini(&obj->oo_dt);
1409 if (obj->oo_hl_head != NULL)
1410 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1415 * Concurrency: no concurrent access is possible that late in object
1418 static void osd_index_fini(struct osd_object *o)
1420 struct iam_container *bag;
1422 if (o->oo_dir != NULL) {
1423 bag = &o->oo_dir->od_container;
1424 if (o->oo_inode != NULL) {
1425 if (bag->ic_object == o->oo_inode)
1426 iam_container_fini(bag);
1428 OBD_FREE_PTR(o->oo_dir);
1434 * Concurrency: no concurrent access is possible that late in object
1435 * life-cycle (for all existing callers, that is. New callers have to provide
1436 * their own locking.)
1438 static int osd_inode_unlinked(const struct inode *inode)
1440 return inode->i_nlink == 0;
1444 OSD_TXN_OI_DELETE_CREDITS = 20,
1445 OSD_TXN_INODE_DELETE_CREDITS = 20
1452 #if OSD_THANDLE_STATS
1454 * Set time when the handle is allocated
1456 static void osd_th_alloced(struct osd_thandle *oth)
1458 oth->oth_alloced = cfs_time_current();
1462 * Set time when the handle started
1464 static void osd_th_started(struct osd_thandle *oth)
1466 oth->oth_started = cfs_time_current();
1470 * Helper function to convert time interval to microseconds packed in
1473 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1477 cfs_duration_usec(cfs_time_sub(end, start), &val);
1478 return val.tv_sec * 1000000 + val.tv_usec;
1482 * Check whether the we deal with this handle for too long.
1484 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1485 cfs_time_t alloced, cfs_time_t started,
1488 cfs_time_t now = cfs_time_current();
1490 LASSERT(dev != NULL);
1492 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1493 interval_to_usec(alloced, started));
1494 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1495 interval_to_usec(started, closed));
1496 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1497 interval_to_usec(closed, now));
1499 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1500 CWARN("transaction handle %p was open for too long: "
1501 "now "CFS_TIME_T" ,"
1502 "alloced "CFS_TIME_T" ,"
1503 "started "CFS_TIME_T" ,"
1504 "closed "CFS_TIME_T"\n",
1505 oth, now, alloced, started, closed);
1506 libcfs_debug_dumpstack(NULL);
1510 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1512 cfs_time_t __closed = cfs_time_current(); \
1513 cfs_time_t __alloced = oth->oth_alloced; \
1514 cfs_time_t __started = oth->oth_started; \
1517 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1520 #else /* OSD_THANDLE_STATS */
1522 #define osd_th_alloced(h) do {} while(0)
1523 #define osd_th_started(h) do {} while(0)
1524 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1526 #endif /* OSD_THANDLE_STATS */
1529 * Concurrency: doesn't access mutable data.
1531 static int osd_param_is_not_sane(const struct osd_device *dev,
1532 const struct thandle *th)
1534 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1536 return oh->ot_credits > osd_transaction_size(dev);
1540 * Concurrency: shouldn't matter.
1542 static void osd_trans_commit_cb(struct super_block *sb,
1543 struct ldiskfs_journal_cb_entry *jcb, int error)
1545 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1546 struct thandle *th = &oh->ot_super;
1547 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1548 struct dt_txn_commit_cb *dcb, *tmp;
1550 LASSERT(oh->ot_handle == NULL);
1553 CERROR("transaction @0x%p commit error: %d\n", th, error);
1555 dt_txn_hook_commit(th);
1557 /* call per-transaction callbacks if any */
1558 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1560 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1561 "commit callback entry: magic=%x name='%s'\n",
1562 dcb->dcb_magic, dcb->dcb_name);
1563 list_del_init(&dcb->dcb_linkage);
1564 dcb->dcb_func(NULL, th, dcb, error);
1567 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1571 lu_context_exit(&th->th_ctx);
1572 lu_context_fini(&th->th_ctx);
1576 #ifndef HAVE_SB_START_WRITE
1577 # define sb_start_write(sb) do {} while (0)
1578 # define sb_end_write(sb) do {} while (0)
1581 static struct thandle *osd_trans_create(const struct lu_env *env,
1582 struct dt_device *d)
1584 struct osd_thread_info *oti = osd_oti_get(env);
1585 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1586 struct osd_thandle *oh;
1590 /* on pending IO in this thread should left from prev. request */
1591 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1593 sb_start_write(osd_sb(osd_dt_dev(d)));
1595 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1597 oh->ot_quota_trans = &oti->oti_quota_trans;
1598 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1602 th->th_tags = LCT_TX_HANDLE;
1604 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1605 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1608 memset(oti->oti_declare_ops, 0,
1609 sizeof(oti->oti_declare_ops));
1610 memset(oti->oti_declare_ops_cred, 0,
1611 sizeof(oti->oti_declare_ops_cred));
1612 memset(oti->oti_declare_ops_used, 0,
1613 sizeof(oti->oti_declare_ops_used));
1615 sb_end_write(osd_sb(osd_dt_dev(d)));
1616 th = ERR_PTR(-ENOMEM);
1621 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1623 struct osd_thread_info *oti = osd_oti_get(env);
1624 struct osd_thandle *oh;
1626 oh = container_of0(th, struct osd_thandle, ot_super);
1627 LASSERT(oh != NULL);
1629 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1630 oti->oti_declare_ops[OSD_OT_CREATE],
1631 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1632 oti->oti_declare_ops_used[OSD_OT_CREATE],
1633 oti->oti_declare_ops[OSD_OT_DESTROY],
1634 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1635 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1636 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1637 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1638 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1639 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1640 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1641 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1642 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1643 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1644 oti->oti_declare_ops[OSD_OT_WRITE],
1645 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1646 oti->oti_declare_ops_used[OSD_OT_WRITE],
1647 oti->oti_declare_ops[OSD_OT_PUNCH],
1648 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1649 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1650 oti->oti_declare_ops[OSD_OT_QUOTA],
1651 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1652 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1653 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1654 oti->oti_declare_ops[OSD_OT_INSERT],
1655 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1656 oti->oti_declare_ops_used[OSD_OT_INSERT],
1657 oti->oti_declare_ops[OSD_OT_DELETE],
1658 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1659 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1660 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1661 oti->oti_declare_ops[OSD_OT_REF_ADD],
1662 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1663 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1664 oti->oti_declare_ops[OSD_OT_REF_DEL],
1665 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1666 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1670 * Concurrency: shouldn't matter.
1672 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1675 struct osd_thread_info *oti = osd_oti_get(env);
1676 struct osd_device *dev = osd_dt_dev(d);
1678 struct osd_thandle *oh;
1683 LASSERT(current->journal_info == NULL);
1685 oh = container_of0(th, struct osd_thandle, ot_super);
1686 LASSERT(oh != NULL);
1687 LASSERT(oh->ot_handle == NULL);
1689 rc = dt_txn_hook_start(env, d, th);
1693 if (unlikely(osd_param_is_not_sane(dev, th))) {
1694 static unsigned long last_printed;
1695 static int last_credits;
1697 /* don't make noise on a tiny testing systems
1698 * actual credits misuse will be caught anyway */
1699 if (last_credits != oh->ot_credits &&
1700 time_after(jiffies, last_printed +
1701 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1702 osd_transaction_size(dev) > 512) {
1703 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1704 oh->ot_credits, osd_transaction_size(dev));
1705 osd_trans_dump_creds(env, th);
1706 libcfs_debug_dumpstack(NULL);
1707 last_credits = oh->ot_credits;
1708 last_printed = jiffies;
1710 /* XXX Limit the credits to 'max_transaction_buffers', and
1711 * let the underlying filesystem to catch the error if
1712 * we really need so many credits.
1714 * This should be removed when we can calculate the
1715 * credits precisely. */
1716 oh->ot_credits = osd_transaction_size(dev);
1720 * XXX temporary stuff. Some abstraction layer should
1723 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1727 LASSERT(oti->oti_txns == 0);
1728 lu_context_init(&th->th_ctx, th->th_tags);
1729 lu_context_enter(&th->th_ctx);
1731 lu_device_get(&d->dd_lu_dev);
1732 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1743 static int osd_seq_exists(const struct lu_env *env,
1744 struct osd_device *osd, u64 seq)
1746 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1747 struct seq_server_site *ss = osd_seq_site(osd);
1751 LASSERT(ss != NULL);
1752 LASSERT(ss->ss_server_fld != NULL);
1754 rc = osd_fld_lookup(env, osd, seq, range);
1757 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1758 osd_name(osd), seq, rc);
1762 RETURN(ss->ss_node_id == range->lsr_index);
1765 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1767 struct dt_txn_commit_cb *dcb;
1768 struct dt_txn_commit_cb *tmp;
1770 /* call per-transaction stop callbacks if any */
1771 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1773 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1774 "commit callback entry: magic=%x name='%s'\n",
1775 dcb->dcb_magic, dcb->dcb_name);
1776 list_del_init(&dcb->dcb_linkage);
1777 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1782 * Concurrency: shouldn't matter.
1784 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1787 int rc = 0, remove_agents = 0;
1788 struct osd_thandle *oh;
1789 struct osd_thread_info *oti = osd_oti_get(env);
1790 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1791 struct osd_device *osd = osd_dt_dev(th->th_dev);
1792 struct qsd_instance *qsd = osd->od_quota_slave;
1793 struct lquota_trans *qtrans;
1796 oh = container_of0(th, struct osd_thandle, ot_super);
1798 /* reset OI cache for safety */
1799 oti->oti_ins_cache_used = 0;
1801 remove_agents = oh->ot_remove_agents;
1803 qtrans = oh->ot_quota_trans;
1804 oh->ot_quota_trans = NULL;
1806 if (oh->ot_handle != NULL) {
1808 handle_t *hdl = oh->ot_handle;
1811 * add commit callback
1812 * notice we don't do this in osd_trans_start()
1813 * as underlying transaction can change during truncate
1815 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1818 LASSERT(oti->oti_txns == 1);
1821 rc = dt_txn_hook_stop(env, th);
1823 CERROR("%s: failed in transaction hook: rc = %d\n",
1826 osd_trans_stop_cb(oh, rc);
1827 /* hook functions might modify th_sync */
1828 hdl->h_sync = th->th_sync;
1830 oh->ot_handle = NULL;
1831 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1833 CERROR("%s: failed to stop transaction: rc = %d\n",
1834 osd_name(osd), rc2);
1838 osd_trans_stop_cb(oh, th->th_result);
1842 /* inform the quota slave device that the transaction is stopping */
1843 qsd_op_end(env, qsd, qtrans);
1845 /* as we want IO to journal and data IO be concurrent, we don't block
1846 * awaiting data IO completion in osd_do_bio(), instead we wait here
1847 * once transaction is submitted to the journal. all reqular requests
1848 * don't do direct IO (except read/write), thus this wait_event becomes
1851 * IMPORTANT: we have to wait till any IO submited by the thread is
1852 * completed otherwise iobuf may be corrupted by different request
1854 wait_event(iobuf->dr_wait,
1855 atomic_read(&iobuf->dr_numreqs) == 0);
1856 osd_fini_iobuf(osd, iobuf);
1858 rc = iobuf->dr_error;
1860 if (unlikely(remove_agents != 0))
1861 osd_process_scheduled_agent_removals(env, osd);
1863 sb_end_write(osd_sb(osd));
1868 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1870 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1873 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1874 LASSERT(&dcb->dcb_func != NULL);
1875 if (dcb->dcb_flags & DCB_TRANS_STOP)
1876 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1878 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1884 * Called just before object is freed. Releases all resources except for
1885 * object itself (that is released by osd_object_free()).
1887 * Concurrency: no concurrent access is possible that late in object
1890 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1892 struct osd_object *obj = osd_obj(l);
1893 struct inode *inode = obj->oo_inode;
1895 LINVRNT(osd_invariant(obj));
1898 * If object is unlinked remove fid->ino mapping from object index.
1901 osd_index_fini(obj);
1902 if (inode != NULL) {
1903 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1904 qid_t uid = i_uid_read(inode);
1905 qid_t gid = i_gid_read(inode);
1908 obj->oo_inode = NULL;
1911 struct osd_thread_info *info = osd_oti_get(env);
1912 struct lquota_id_info *qi = &info->oti_qi;
1914 /* Release granted quota to master if necessary */
1915 qi->lqi_id.qid_uid = uid;
1916 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1918 qi->lqi_id.qid_uid = gid;
1919 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1925 * Concurrency: ->loo_object_release() is called under site spin-lock.
1927 static void osd_object_release(const struct lu_env *env,
1928 struct lu_object *l)
1930 struct osd_object *o = osd_obj(l);
1931 /* nobody should be releasing a non-destroyed object with nlink=0
1932 * the API allows this, but ldiskfs doesn't like and then report
1933 * this inode as deleted */
1934 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1939 * Concurrency: shouldn't matter.
1941 static int osd_object_print(const struct lu_env *env, void *cookie,
1942 lu_printer_t p, const struct lu_object *l)
1944 struct osd_object *o = osd_obj(l);
1945 struct iam_descr *d;
1947 if (o->oo_dir != NULL)
1948 d = o->oo_dir->od_container.ic_descr;
1951 return (*p)(env, cookie,
1952 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1954 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1955 o->oo_inode ? o->oo_inode->i_generation : 0,
1956 d ? d->id_ops->id_name : "plain");
1960 * Concurrency: shouldn't matter.
1962 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1963 struct obd_statfs *sfs)
1965 struct osd_device *osd = osd_dt_dev(d);
1966 struct super_block *sb = osd_sb(osd);
1967 struct kstatfs *ksfs;
1971 if (unlikely(osd->od_mnt == NULL))
1972 return -EINPROGRESS;
1974 /* osd_lproc.c call this without env, allocate ksfs for that case */
1975 if (unlikely(env == NULL)) {
1976 OBD_ALLOC_PTR(ksfs);
1980 ksfs = &osd_oti_get(env)->oti_ksfs;
1983 result = sb->s_op->statfs(sb->s_root, ksfs);
1987 statfs_pack(sfs, ksfs);
1988 if (unlikely(sb->s_flags & MS_RDONLY))
1989 sfs->os_state |= OS_STATE_READONLY;
1990 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1991 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1992 sfs->os_maxbytes = sb->s_maxbytes;
1994 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1997 * Reserve some space so to avoid fragmenting the filesystem too much.
1998 * Fragmentation not only impacts performance, but can also increase
1999 * metadata overhead significantly, causing grant calculation to be
2002 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2004 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2005 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2006 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2007 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2009 sfs->os_blocks -= reserved;
2010 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2011 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2014 if (unlikely(env == NULL))
2020 * Estimate space needed for file creations. We assume the largest filename
2021 * which is 2^64 - 1, hence a filename of 20 chars.
2022 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2024 #ifdef __LDISKFS_DIR_REC_LEN
2025 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2027 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2031 * Concurrency: doesn't access mutable data.
2033 static void osd_conf_get(const struct lu_env *env,
2034 const struct dt_device *dev,
2035 struct dt_device_param *param)
2037 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2041 * XXX should be taken from not-yet-existing fs abstraction layer.
2043 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2044 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2045 param->ddp_symlink_max = sb->s_blocksize;
2046 param->ddp_mount_type = LDD_MT_LDISKFS;
2047 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2048 param->ddp_maxbytes = sb->s_maxbytes;
2050 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2051 /* inode are statically allocated, so per-inode space consumption
2052 * is the space consumed by the directory entry */
2053 param->ddp_inodespace = PER_OBJ_USAGE;
2054 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2055 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2056 * maximum length to not under count the actual number of extents
2057 * needed for writing a file. */
2058 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2059 /* worst-case extent insertion metadata overhead */
2060 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2061 param->ddp_mntopts = 0;
2062 if (test_opt(sb, XATTR_USER))
2063 param->ddp_mntopts |= MNTOPT_USERXATTR;
2064 if (test_opt(sb, POSIX_ACL))
2065 param->ddp_mntopts |= MNTOPT_ACL;
2067 /* LOD might calculate the max stripe count based on max_ea_size,
2068 * so we need take account in the overhead as well,
2069 * xattr_header + magic + xattr_entry_head */
2070 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2071 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2073 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2074 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2075 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2079 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2083 * Concurrency: shouldn't matter.
2085 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2089 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2091 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2093 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2094 LUSTRE_OSD_LDISKFS_NAME, rc);
2100 * Start commit for OSD device.
2102 * An implementation of dt_commit_async method for OSD device.
2103 * Asychronously starts underlayng fs sync and thereby a transaction
2106 * \param env environment
2107 * \param d dt device
2109 * \see dt_device_operations
2111 static int osd_commit_async(const struct lu_env *env,
2112 struct dt_device *d)
2114 struct super_block *s = osd_sb(osd_dt_dev(d));
2117 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2118 RETURN(s->s_op->sync_fs(s, 0));
2122 * Concurrency: shouldn't matter.
2125 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2127 struct super_block *sb = osd_sb(osd_dt_dev(d));
2128 struct block_device *dev = sb->s_bdev;
2129 #ifdef HAVE_DEV_SET_RDONLY
2130 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2133 int rc = -EOPNOTSUPP;
2137 #ifdef HAVE_DEV_SET_RDONLY
2138 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2140 if (sb->s_op->freeze_fs) {
2141 rc = sb->s_op->freeze_fs(sb);
2146 if (jdev && (jdev != dev)) {
2147 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2149 dev_set_rdonly(jdev);
2151 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2152 dev_set_rdonly(dev);
2154 if (sb->s_op->unfreeze_fs)
2155 sb->s_op->unfreeze_fs(sb);
2160 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2161 osd_dt_dev(d)->od_svname, (long)dev, rc);
2167 * Note: we do not count into QUOTA here.
2168 * If we mount with --data_journal we may need more.
2170 const int osd_dto_credits_noquota[DTO_NR] = {
2173 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2174 * SINGLEDATA_TRANS_BLOCKS(8)
2175 * XXX Note: maybe iam need more, since iam have more level than
2178 [DTO_INDEX_INSERT] = 16,
2181 * just modify a single entry, probably merge few within a block
2183 [DTO_INDEX_DELETE] = 1,
2187 [DTO_INDEX_UPDATE] = 16,
2189 * 4(inode, inode bits, groups, GDT)
2190 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2192 [DTO_OBJECT_CREATE] = 4,
2194 * 4(inode, inode bits, groups, GDT)
2195 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2197 [DTO_OBJECT_DELETE] = 4,
2199 * Attr set credits (inode)
2201 [DTO_ATTR_SET_BASE] = 1,
2203 * Xattr set. The same as xattr of EXT3.
2204 * DATA_TRANS_BLOCKS(14)
2205 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2206 * are also counted in. Do not know why?
2208 [DTO_XATTR_SET] = 14,
2210 * credits for inode change during write.
2212 [DTO_WRITE_BASE] = 3,
2214 * credits for single block write.
2216 [DTO_WRITE_BLOCK] = 14,
2218 * Attr set credits for chown.
2219 * This is extra credits for setattr, and it is null without quota
2221 [DTO_ATTR_SET_CHOWN] = 0
2224 static const struct dt_device_operations osd_dt_ops = {
2225 .dt_root_get = osd_root_get,
2226 .dt_statfs = osd_statfs,
2227 .dt_trans_create = osd_trans_create,
2228 .dt_trans_start = osd_trans_start,
2229 .dt_trans_stop = osd_trans_stop,
2230 .dt_trans_cb_add = osd_trans_cb_add,
2231 .dt_conf_get = osd_conf_get,
2232 .dt_sync = osd_sync,
2234 .dt_commit_async = osd_commit_async,
2237 static void osd_object_read_lock(const struct lu_env *env,
2238 struct dt_object *dt, unsigned role)
2240 struct osd_object *obj = osd_dt_obj(dt);
2241 struct osd_thread_info *oti = osd_oti_get(env);
2243 LINVRNT(osd_invariant(obj));
2245 LASSERT(obj->oo_owner != env);
2246 down_read_nested(&obj->oo_sem, role);
2248 LASSERT(obj->oo_owner == NULL);
2252 static void osd_object_write_lock(const struct lu_env *env,
2253 struct dt_object *dt, unsigned role)
2255 struct osd_object *obj = osd_dt_obj(dt);
2256 struct osd_thread_info *oti = osd_oti_get(env);
2258 LINVRNT(osd_invariant(obj));
2260 LASSERT(obj->oo_owner != env);
2261 down_write_nested(&obj->oo_sem, role);
2263 LASSERT(obj->oo_owner == NULL);
2264 obj->oo_owner = env;
2268 static void osd_object_read_unlock(const struct lu_env *env,
2269 struct dt_object *dt)
2271 struct osd_object *obj = osd_dt_obj(dt);
2272 struct osd_thread_info *oti = osd_oti_get(env);
2274 LINVRNT(osd_invariant(obj));
2276 LASSERT(oti->oti_r_locks > 0);
2278 up_read(&obj->oo_sem);
2281 static void osd_object_write_unlock(const struct lu_env *env,
2282 struct dt_object *dt)
2284 struct osd_object *obj = osd_dt_obj(dt);
2285 struct osd_thread_info *oti = osd_oti_get(env);
2287 LINVRNT(osd_invariant(obj));
2289 LASSERT(obj->oo_owner == env);
2290 LASSERT(oti->oti_w_locks > 0);
2292 obj->oo_owner = NULL;
2293 up_write(&obj->oo_sem);
2296 static int osd_object_write_locked(const struct lu_env *env,
2297 struct dt_object *dt)
2299 struct osd_object *obj = osd_dt_obj(dt);
2301 LINVRNT(osd_invariant(obj));
2303 return obj->oo_owner == env;
2306 static struct timespec *osd_inode_time(const struct lu_env *env,
2307 struct inode *inode, __u64 seconds)
2309 struct osd_thread_info *oti = osd_oti_get(env);
2310 struct timespec *t = &oti->oti_time;
2312 t->tv_sec = seconds;
2314 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2318 static void osd_inode_getattr(const struct lu_env *env,
2319 struct inode *inode, struct lu_attr *attr)
2321 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2322 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2323 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2326 attr->la_atime = LTIME_S(inode->i_atime);
2327 attr->la_mtime = LTIME_S(inode->i_mtime);
2328 attr->la_ctime = LTIME_S(inode->i_ctime);
2329 attr->la_mode = inode->i_mode;
2330 attr->la_size = i_size_read(inode);
2331 attr->la_blocks = inode->i_blocks;
2332 attr->la_uid = i_uid_read(inode);
2333 attr->la_gid = i_gid_read(inode);
2334 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2335 attr->la_nlink = inode->i_nlink;
2336 attr->la_rdev = inode->i_rdev;
2337 attr->la_blksize = 1 << inode->i_blkbits;
2338 attr->la_blkbits = inode->i_blkbits;
2341 static int osd_attr_get(const struct lu_env *env,
2342 struct dt_object *dt,
2343 struct lu_attr *attr)
2345 struct osd_object *obj = osd_dt_obj(dt);
2347 if (unlikely(!dt_object_exists(dt)))
2349 if (unlikely(obj->oo_destroyed))
2352 LASSERT(!dt_object_remote(dt));
2353 LINVRNT(osd_invariant(obj));
2355 spin_lock(&obj->oo_guard);
2356 osd_inode_getattr(env, obj->oo_inode, attr);
2357 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2358 attr->la_flags |= LUSTRE_ORPHAN_FL;
2359 spin_unlock(&obj->oo_guard);
2364 static int osd_declare_attr_set(const struct lu_env *env,
2365 struct dt_object *dt,
2366 const struct lu_attr *attr,
2367 struct thandle *handle)
2369 struct osd_thandle *oh;
2370 struct osd_object *obj;
2371 struct osd_thread_info *info = osd_oti_get(env);
2372 struct lquota_id_info *qi = &info->oti_qi;
2380 LASSERT(dt != NULL);
2381 LASSERT(handle != NULL);
2383 obj = osd_dt_obj(dt);
2384 LASSERT(osd_invariant(obj));
2386 oh = container_of0(handle, struct osd_thandle, ot_super);
2387 LASSERT(oh->ot_handle == NULL);
2389 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2390 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2392 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2393 osd_dto_credits_noquota[DTO_XATTR_SET]);
2395 if (attr == NULL || obj->oo_inode == NULL)
2398 bspace = obj->oo_inode->i_blocks;
2399 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2400 bspace = toqb(bspace);
2402 /* Changing ownership is always preformed by super user, it should not
2405 * We still need to call the osd_declare_qid() to calculate the journal
2406 * credits for updating quota accounting files and to trigger quota
2407 * space adjustment once the operation is completed.*/
2408 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2410 uid = i_uid_read(obj->oo_inode);
2411 qi->lqi_type = USRQUOTA;
2412 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2413 /* inode accounting */
2414 qi->lqi_is_blk = false;
2416 /* one more inode for the new uid ... */
2417 qi->lqi_id.qid_uid = attr->la_uid;
2419 /* Reserve credits for the new uid */
2420 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2421 if (rc == -EDQUOT || rc == -EINPROGRESS)
2426 /* and one less inode for the current uid */
2427 qi->lqi_id.qid_uid = uid;
2429 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2430 if (rc == -EDQUOT || rc == -EINPROGRESS)
2435 /* block accounting */
2436 qi->lqi_is_blk = true;
2438 /* more blocks for the new uid ... */
2439 qi->lqi_id.qid_uid = attr->la_uid;
2440 qi->lqi_space = bspace;
2442 * Credits for the new uid has been reserved, re-use "obj"
2443 * to save credit reservation.
2445 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2446 if (rc == -EDQUOT || rc == -EINPROGRESS)
2451 /* and finally less blocks for the current uid */
2452 qi->lqi_id.qid_uid = uid;
2453 qi->lqi_space = -bspace;
2454 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2455 if (rc == -EDQUOT || rc == -EINPROGRESS)
2461 gid = i_gid_read(obj->oo_inode);
2462 qi->lqi_type = GRPQUOTA;
2463 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2465 /* inode accounting */
2466 qi->lqi_is_blk = false;
2468 /* one more inode for the new gid ... */
2469 qi->lqi_id.qid_gid = attr->la_gid;
2471 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2472 if (rc == -EDQUOT || rc == -EINPROGRESS)
2477 /* and one less inode for the current gid */
2478 qi->lqi_id.qid_gid = gid;
2480 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2481 if (rc == -EDQUOT || rc == -EINPROGRESS)
2486 /* block accounting */
2487 qi->lqi_is_blk = true;
2489 /* more blocks for the new gid ... */
2490 qi->lqi_id.qid_gid = attr->la_gid;
2491 qi->lqi_space = bspace;
2492 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2493 if (rc == -EDQUOT || rc == -EINPROGRESS)
2498 /* and finally less blocks for the current gid */
2499 qi->lqi_id.qid_gid = gid;
2500 qi->lqi_space = -bspace;
2501 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2502 if (rc == -EDQUOT || rc == -EINPROGRESS)
2511 static int osd_inode_setattr(const struct lu_env *env,
2512 struct inode *inode, const struct lu_attr *attr)
2514 __u64 bits = attr->la_valid;
2516 /* Only allow set size for regular file */
2517 if (!S_ISREG(inode->i_mode))
2518 bits &= ~(LA_SIZE | LA_BLOCKS);
2523 if (bits & LA_ATIME)
2524 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2525 if (bits & LA_CTIME)
2526 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2527 if (bits & LA_MTIME)
2528 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2529 if (bits & LA_SIZE) {
2530 spin_lock(&inode->i_lock);
2531 LDISKFS_I(inode)->i_disksize = attr->la_size;
2532 i_size_write(inode, attr->la_size);
2533 spin_unlock(&inode->i_lock);
2536 /* OSD should not change "i_blocks" which is used by quota.
2537 * "i_blocks" should be changed by ldiskfs only. */
2539 inode->i_mode = (inode->i_mode & S_IFMT) |
2540 (attr->la_mode & ~S_IFMT);
2542 i_uid_write(inode, attr->la_uid);
2544 i_gid_write(inode, attr->la_gid);
2545 if (bits & LA_NLINK)
2546 set_nlink(inode, attr->la_nlink);
2548 inode->i_rdev = attr->la_rdev;
2550 if (bits & LA_FLAGS) {
2551 /* always keep S_NOCMTIME */
2552 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2558 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2560 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2561 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2565 ll_vfs_dq_init(inode);
2567 if (attr->la_valid & LA_UID)
2568 iattr.ia_valid |= ATTR_UID;
2569 if (attr->la_valid & LA_GID)
2570 iattr.ia_valid |= ATTR_GID;
2571 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2572 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2574 rc = ll_vfs_dq_transfer(inode, &iattr);
2576 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2577 "enforcement enabled on the ldiskfs "
2578 "filesystem?\n", inode->i_sb->s_id, rc);
2585 static int osd_attr_set(const struct lu_env *env,
2586 struct dt_object *dt,
2587 const struct lu_attr *attr,
2588 struct thandle *handle)
2590 struct osd_object *obj = osd_dt_obj(dt);
2591 struct inode *inode;
2594 if (!dt_object_exists(dt))
2597 LASSERT(handle != NULL);
2598 LASSERT(!dt_object_remote(dt));
2599 LASSERT(osd_invariant(obj));
2601 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2603 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2604 struct osd_thread_info *oti = osd_oti_get(env);
2605 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2606 struct lu_fid *fid1 = &oti->oti_fid;
2607 struct osd_inode_id *id = &oti->oti_id;
2608 struct iam_path_descr *ipd;
2609 struct iam_container *bag;
2610 struct osd_thandle *oh;
2613 fid_cpu_to_be(fid1, fid0);
2614 memset(id, 1, sizeof(*id));
2615 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2616 fid0)->oi_dir.od_container;
2617 ipd = osd_idx_ipd_get(env, bag);
2618 if (unlikely(ipd == NULL))
2621 oh = container_of0(handle, struct osd_thandle, ot_super);
2622 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2623 (const struct iam_rec *)id, ipd);
2624 osd_ipd_put(env, bag, ipd);
2625 return(rc > 0 ? 0 : rc);
2628 inode = obj->oo_inode;
2630 rc = osd_quota_transfer(inode, attr);
2634 spin_lock(&obj->oo_guard);
2635 rc = osd_inode_setattr(env, inode, attr);
2636 spin_unlock(&obj->oo_guard);
2640 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2642 if (!(attr->la_valid & LA_FLAGS))
2645 /* Let's check if there are extra flags need to be set into LMA */
2646 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2647 struct osd_thread_info *info = osd_oti_get(env);
2648 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2650 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2654 lma->lma_incompat |=
2655 lustre_to_lma_flags(attr->la_flags);
2656 lustre_lma_swab(lma);
2657 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2658 lma, sizeof(*lma), XATTR_REPLACE);
2660 struct osd_device *osd = osd_obj2dev(obj);
2662 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2663 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2664 lma->lma_incompat, rc);
2667 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2669 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2672 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2677 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2678 struct osd_object *obj,
2679 const char *name, const int namelen)
2681 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2684 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2685 umode_t mode, struct dt_allocation_hint *hint,
2689 struct osd_device *osd = osd_obj2dev(obj);
2690 struct osd_thandle *oth;
2691 struct dt_object *parent = NULL;
2692 struct inode *inode;
2694 LINVRNT(osd_invariant(obj));
2695 LASSERT(obj->oo_inode == NULL);
2696 LASSERT(obj->oo_hl_head == NULL);
2698 if (S_ISDIR(mode) && ldiskfs_pdo) {
2699 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2700 if (obj->oo_hl_head == NULL)
2704 oth = container_of(th, struct osd_thandle, ot_super);
2705 LASSERT(oth->ot_handle->h_transaction != NULL);
2707 if (hint != NULL && hint->dah_parent != NULL &&
2708 !dt_object_remote(hint->dah_parent))
2709 parent = hint->dah_parent;
2711 inode = ldiskfs_create_inode(oth->ot_handle,
2712 parent ? osd_dt_obj(parent)->oo_inode :
2713 osd_sb(osd)->s_root->d_inode,
2715 if (!IS_ERR(inode)) {
2716 /* Do not update file c/mtime in ldiskfs. */
2717 inode->i_flags |= S_NOCMTIME;
2719 /* For new created object, it must be consistent,
2720 * and it is unnecessary to scrub against it. */
2721 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2723 obj->oo_inode = inode;
2726 if (obj->oo_hl_head != NULL) {
2727 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2728 obj->oo_hl_head = NULL;
2730 result = PTR_ERR(inode);
2732 LINVRNT(osd_invariant(obj));
2740 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2741 struct lu_attr *attr,
2742 struct dt_allocation_hint *hint,
2743 struct dt_object_format *dof,
2747 struct osd_thandle *oth;
2748 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2750 LASSERT(S_ISDIR(attr->la_mode));
2752 oth = container_of(th, struct osd_thandle, ot_super);
2753 LASSERT(oth->ot_handle->h_transaction != NULL);
2754 result = osd_mkfile(info, obj, mode, hint, th);
2759 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2760 struct lu_attr *attr,
2761 struct dt_allocation_hint *hint,
2762 struct dt_object_format *dof,
2766 struct osd_thandle *oth;
2767 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2769 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2771 LASSERT(S_ISREG(attr->la_mode));
2773 oth = container_of(th, struct osd_thandle, ot_super);
2774 LASSERT(oth->ot_handle->h_transaction != NULL);
2776 result = osd_mkfile(info, obj, mode, hint, th);
2778 LASSERT(obj->oo_inode != NULL);
2779 if (feat->dif_flags & DT_IND_VARKEY)
2780 result = iam_lvar_create(obj->oo_inode,
2781 feat->dif_keysize_max,
2783 feat->dif_recsize_max,
2786 result = iam_lfix_create(obj->oo_inode,
2787 feat->dif_keysize_max,
2789 feat->dif_recsize_max,
2796 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2797 struct lu_attr *attr,
2798 struct dt_allocation_hint *hint,
2799 struct dt_object_format *dof,
2802 LASSERT(S_ISREG(attr->la_mode));
2803 return osd_mkfile(info, obj, (attr->la_mode &
2804 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2807 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2808 struct lu_attr *attr,
2809 struct dt_allocation_hint *hint,
2810 struct dt_object_format *dof,
2813 LASSERT(S_ISLNK(attr->la_mode));
2814 return osd_mkfile(info, obj, (attr->la_mode &
2815 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2818 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2819 struct lu_attr *attr,
2820 struct dt_allocation_hint *hint,
2821 struct dt_object_format *dof,
2824 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2827 LINVRNT(osd_invariant(obj));
2828 LASSERT(obj->oo_inode == NULL);
2829 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2830 S_ISFIFO(mode) || S_ISSOCK(mode));
2832 result = osd_mkfile(info, obj, mode, hint, th);
2834 LASSERT(obj->oo_inode != NULL);
2836 * This inode should be marked dirty for i_rdev. Currently
2837 * that is done in the osd_attr_init().
2839 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2842 LINVRNT(osd_invariant(obj));
2846 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2848 struct dt_allocation_hint *hint,
2849 struct dt_object_format *dof,
2852 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2854 osd_obj_type_f result;
2870 result = osd_mk_index;
2881 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2882 struct dt_object *parent, struct dt_object *child,
2887 ah->dah_parent = parent;
2888 ah->dah_mode = child_mode;
2890 if (parent != NULL && !dt_object_remote(parent)) {
2891 /* will help to find FID->ino at dt_insert("..") */
2892 struct osd_object *pobj = osd_dt_obj(parent);
2893 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2897 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2898 struct lu_attr *attr, struct dt_object_format *dof)
2900 struct inode *inode = obj->oo_inode;
2901 __u64 valid = attr->la_valid;
2904 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2906 if (dof->dof_type != DFT_NODE)
2907 attr->la_valid &= ~LA_RDEV;
2908 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2909 attr->la_valid &= ~LA_ATIME;
2910 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2911 attr->la_valid &= ~LA_CTIME;
2912 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2913 attr->la_valid &= ~LA_MTIME;
2915 result = osd_quota_transfer(inode, attr);
2919 if (attr->la_valid != 0) {
2920 result = osd_inode_setattr(info->oti_env, inode, attr);
2922 * The osd_inode_setattr() should always succeed here. The
2923 * only error that could be returned is EDQUOT when we are
2924 * trying to change the UID or GID of the inode. However, this
2925 * should not happen since quota enforcement is no longer
2926 * enabled on ldiskfs (lquota takes care of it).
2928 LASSERTF(result == 0, "%d\n", result);
2929 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2932 attr->la_valid = valid;
2936 * Helper function for osd_object_create()
2938 * \retval 0, on success
2940 static int __osd_object_create(struct osd_thread_info *info,
2941 struct osd_object *obj, struct lu_attr *attr,
2942 struct dt_allocation_hint *hint,
2943 struct dt_object_format *dof,
2949 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2951 /* we drop umask so that permissions we pass are not affected */
2952 umask = current->fs->umask;
2953 current->fs->umask = 0;
2955 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2957 if (likely(obj->oo_inode != NULL)) {
2958 LASSERT(obj->oo_inode->i_state & I_NEW);
2960 /* Unlock the inode before attr initialization to avoid
2961 * unnecessary dqget operations. LU-6378 */
2962 unlock_new_inode(obj->oo_inode);
2965 if (likely(result == 0)) {
2966 osd_attr_init(info, obj, attr, dof);
2967 osd_object_init0(obj);
2970 /* restore previous umask value */
2971 current->fs->umask = umask;
2973 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2979 * Helper function for osd_object_create()
2981 * \retval 0, on success
2983 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2984 const struct lu_fid *fid, struct thandle *th)
2986 struct osd_thread_info *info = osd_oti_get(env);
2987 struct osd_inode_id *id = &info->oti_id;
2988 struct osd_device *osd = osd_obj2dev(obj);
2989 struct osd_thandle *oh;
2992 LASSERT(obj->oo_inode != NULL);
2994 oh = container_of0(th, struct osd_thandle, ot_super);
2995 LASSERT(oh->ot_handle);
2996 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2998 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2999 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3000 OI_CHECK_FLD, NULL);
3001 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3006 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3007 u64 seq, struct lu_seq_range *range)
3009 struct seq_server_site *ss = osd_seq_site(osd);
3011 if (fid_seq_is_idif(seq)) {
3012 fld_range_set_ost(range);
3013 range->lsr_index = idif_ost_idx(seq);
3017 if (!fid_seq_in_fldb(seq)) {
3018 fld_range_set_mdt(range);
3020 /* FIXME: If ss is NULL, it suppose not get lsr_index
3022 range->lsr_index = ss->ss_node_id;
3026 LASSERT(ss != NULL);
3027 fld_range_set_any(range);
3028 /* OSD will only do local fld lookup */
3029 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3033 * Concurrency: no external locking is necessary.
3035 static int osd_declare_object_create(const struct lu_env *env,
3036 struct dt_object *dt,
3037 struct lu_attr *attr,
3038 struct dt_allocation_hint *hint,
3039 struct dt_object_format *dof,
3040 struct thandle *handle)
3042 struct osd_thandle *oh;
3046 LASSERT(handle != NULL);
3048 oh = container_of0(handle, struct osd_thandle, ot_super);
3049 LASSERT(oh->ot_handle == NULL);
3051 /* EA object consumes more credits than regular object: osd_mk_index
3052 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3053 * leaf_node, could involves the block, block bitmap, groups, GDT
3054 * change for each block, so add 4 * 2 credits in that case. */
3055 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3056 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3057 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3058 /* Reuse idle OI block may cause additional one OI block
3060 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3061 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3066 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3067 osd_dt_obj(dt), false, NULL, false);
3071 /* will help to find FID->ino mapping at dt_insert() */
3072 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3078 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3079 struct lu_attr *attr,
3080 struct dt_allocation_hint *hint,
3081 struct dt_object_format *dof, struct thandle *th)
3083 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3084 struct osd_object *obj = osd_dt_obj(dt);
3085 struct osd_thread_info *info = osd_oti_get(env);
3089 if (dt_object_exists(dt))
3092 LINVRNT(osd_invariant(obj));
3093 LASSERT(!dt_object_remote(dt));
3094 LASSERT(osd_write_locked(env, obj));
3095 LASSERT(th != NULL);
3097 if (unlikely(fid_is_acct(fid)))
3098 /* Quota files can't be created from the kernel any more,
3099 * 'tune2fs -O quota' will take care of creating them */
3102 result = __osd_object_create(info, obj, attr, hint, dof, th);
3104 result = __osd_oi_insert(env, obj, fid, th);
3105 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3106 obj->oo_dt.do_body_ops = &osd_body_ops;
3108 LASSERT(ergo(result == 0,
3109 dt_object_exists(dt) && !dt_object_remote(dt)));
3111 LASSERT(osd_invariant(obj));
3116 * Called to destroy on-disk representation of the object
3118 * Concurrency: must be locked
3120 static int osd_declare_object_destroy(const struct lu_env *env,
3121 struct dt_object *dt,
3124 struct osd_object *obj = osd_dt_obj(dt);
3125 struct inode *inode = obj->oo_inode;
3126 struct osd_thandle *oh;
3133 oh = container_of0(th, struct osd_thandle, ot_super);
3134 LASSERT(oh->ot_handle == NULL);
3136 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3137 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3138 /* Recycle idle OI leaf may cause additional three OI blocks
3140 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3141 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3142 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3143 /* one less inode */
3144 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3145 -1, oh, obj, false, NULL, false);
3148 /* data to be truncated */
3149 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3150 0, oh, obj, true, NULL, false);
3154 /* will help to find FID->ino when this object is being
3155 * added to PENDING/ */
3156 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3161 static int osd_object_destroy(const struct lu_env *env,
3162 struct dt_object *dt,
3165 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3166 struct osd_object *obj = osd_dt_obj(dt);
3167 struct inode *inode = obj->oo_inode;
3168 struct osd_device *osd = osd_obj2dev(obj);
3169 struct osd_thandle *oh;
3173 oh = container_of0(th, struct osd_thandle, ot_super);
3174 LASSERT(oh->ot_handle);
3176 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3178 if (unlikely(fid_is_acct(fid)))
3181 if (S_ISDIR(inode->i_mode)) {
3182 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3183 inode->i_nlink == 2);
3184 /* it will check/delete the inode from remote parent,
3185 * how to optimize it? unlink performance impaction XXX */
3186 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3188 CERROR("%s: delete inode "DFID": rc = %d\n",
3189 osd_name(osd), PFID(fid), result);
3191 spin_lock(&obj->oo_guard);
3193 spin_unlock(&obj->oo_guard);
3194 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3197 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3199 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3201 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3202 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3203 oh->ot_handle, OI_CHECK_FLD);
3205 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3206 /* XXX: add to ext3 orphan list */
3207 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3209 /* not needed in the cache anymore */
3210 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3211 obj->oo_destroyed = 1;
3217 * Put the fid into lustre_mdt_attrs, and then place the structure
3218 * inode's ea. This fid should not be altered during the life time
3221 * \retval +ve, on success
3222 * \retval -ve, on error
3224 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3226 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3227 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3229 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3233 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3236 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3239 lustre_lma_init(lma, fid, compat, incompat);
3240 lustre_lma_swab(lma);
3242 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3244 /* LMA may already exist, but we need to check that all the
3245 * desired compat/incompat flags have been added. */
3246 if (unlikely(rc == -EEXIST)) {
3247 if (compat == 0 && incompat == 0)
3250 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3251 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3256 lustre_lma_swab(lma);
3257 if (!(~lma->lma_compat & compat) &&
3258 !(~lma->lma_incompat & incompat))
3261 lma->lma_compat |= compat;
3262 lma->lma_incompat |= incompat;
3263 lustre_lma_swab(lma);
3264 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3265 sizeof(*lma), XATTR_REPLACE);
3272 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3273 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3274 * To have compatilibility with 1.8 ldiskfs driver we need to have
3275 * magic number at start of fid data.
3276 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3279 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3280 const struct lu_fid *fid)
3282 if (!fid_is_namespace_visible(fid) ||
3283 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3284 param->edp_magic = 0;
3288 param->edp_magic = LDISKFS_LUFID_MAGIC;
3289 param->edp_len = sizeof(struct lu_fid) + 1;
3290 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3294 * Try to read the fid from inode ea into dt_rec.
3296 * \param fid object fid.
3298 * \retval 0 on success
3300 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3301 __u32 ino, struct lu_fid *fid,
3302 struct osd_inode_id *id)
3304 struct osd_thread_info *info = osd_oti_get(env);
3305 struct inode *inode;
3308 osd_id_gen(id, ino, OSD_OII_NOGEN);
3309 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3311 RETURN(PTR_ERR(inode));
3317 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3319 struct inode *parent_dir,
3320 const struct lu_fid *dot_fid,
3321 const struct lu_fid *dot_dot_fid,
3322 struct osd_thandle *oth)
3324 struct ldiskfs_dentry_param *dot_ldp;
3325 struct ldiskfs_dentry_param *dot_dot_ldp;
3326 __u32 saved_nlink = dir->i_nlink;
3329 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3330 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3332 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3333 dot_ldp->edp_magic = 0;
3335 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3336 dir, dot_ldp, dot_dot_ldp);
3337 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3338 * the subseqent ref_add() will increase the dir->i_nlink
3339 * as 3. That is incorrect for new created directory.
3341 * It looks like hack, because we want to make the OSD API
3342 * to be order-independent for new created directory object
3343 * between dt_insert(..) and ref_add() operations.
3345 * Here, we only restore the in-RAM dir-inode's nlink attr,
3346 * becuase if the nlink attr is not 2, then there will be
3347 * ref_add() called following the dt_insert(..), such call
3348 * will make both the in-RAM and on-disk dir-inode's nlink
3349 * attr to be set as 2. LU-7447 */
3350 set_nlink(dir, saved_nlink);
3355 * Create an local agent inode for remote entry
3357 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3358 struct osd_device *osd,
3359 struct osd_object *pobj,
3360 const struct lu_fid *fid,
3364 struct osd_thread_info *info = osd_oti_get(env);
3365 struct inode *local;
3366 struct osd_thandle *oh;
3371 oh = container_of(th, struct osd_thandle, ot_super);
3372 LASSERT(oh->ot_handle->h_transaction != NULL);
3374 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3375 if (IS_ERR(local)) {
3376 CERROR("%s: create local error %d\n", osd_name(osd),
3377 (int)PTR_ERR(local));
3381 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3382 * correct gid on remote file, not agent here */
3383 local->i_gid = current_fsgid();
3384 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3385 unlock_new_inode(local);
3387 /* Set special LMA flag for local agent inode */
3388 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3390 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3391 osd_name(osd), PFID(fid), rc);
3392 RETURN(ERR_PTR(rc));
3398 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3399 lu_object_fid(&pobj->oo_dt.do_lu),
3402 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3403 osd_name(osd), PFID(fid), rc);
3404 RETURN(ERR_PTR(rc));
3411 * when direntry is deleted, we have to take care of possible agent inode
3412 * referenced by that. unfortunately we can't do this at that point:
3413 * iget() within a running transaction leads to deadlock and we better do
3414 * not call that every delete declaration to save performance. so we put
3415 * a potention agent inode on a list and process that once the transaction
3416 * is over. Notice it's not any worse in terms of real orphans as regular
3417 * object destroy doesn't put inodes on the on-disk orphan list. this should
3418 * be addressed separately
3420 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3421 struct osd_thandle *oh,
3424 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3425 struct osd_obj_orphan *oor;
3432 oor->oor_env = (struct lu_env *)env;
3433 spin_lock(&osd->od_osfs_lock);
3434 list_add(&oor->oor_list, &osd->od_orphan_list);
3435 spin_unlock(&osd->od_osfs_lock);
3437 oh->ot_remove_agents = 1;
3443 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3444 struct osd_device *osd)
3446 struct osd_thread_info *info = osd_oti_get(env);
3447 struct osd_obj_orphan *oor, *tmp;
3448 struct osd_inode_id id;
3449 struct list_head list;
3450 struct inode *inode;
3455 INIT_LIST_HEAD(&list);
3457 spin_lock(&osd->od_osfs_lock);
3458 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3459 if (oor->oor_env == env) {
3460 list_del(&oor->oor_list);
3461 list_add(&oor->oor_list, &list);
3464 spin_unlock(&osd->od_osfs_lock);
3466 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3470 list_del(&oor->oor_list);
3473 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3474 inode = osd_iget_fid(info, osd, &id, &fid);
3478 if (!osd_remote_fid(env, osd, &fid)) {
3483 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3485 mark_inode_dirty(inode);
3486 ldiskfs_journal_stop(jh);
3494 * OSD layer object create function for interoperability mode (b11826).
3495 * This is mostly similar to osd_object_create(). Only difference being, fid is
3496 * inserted into inode ea here.
3498 * \retval 0, on success
3499 * \retval -ve, on error
3501 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3502 struct lu_attr *attr,
3503 struct dt_allocation_hint *hint,
3504 struct dt_object_format *dof,
3507 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3508 struct osd_object *obj = osd_dt_obj(dt);
3509 struct osd_thread_info *info = osd_oti_get(env);
3510 int result, on_ost = 0;
3514 if (dt_object_exists(dt))
3517 LASSERT(osd_invariant(obj));
3518 LASSERT(!dt_object_remote(dt));
3519 LASSERT(osd_write_locked(env, obj));
3520 LASSERT(th != NULL);
3522 if (unlikely(fid_is_acct(fid)))
3523 /* Quota files can't be created from the kernel any more,
3524 * 'tune2fs -O quota' will take care of creating them */
3527 result = __osd_object_create(info, obj, attr, hint, dof, th);
3529 if (fid_is_idif(fid) &&
3530 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3531 struct lu_fid *tfid = &info->oti_fid;
3532 struct ost_id *oi = &info->oti_ostid;
3534 fid_to_ostid(fid, oi);
3535 ostid_to_fid(tfid, oi, 0);
3537 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3538 LMAC_FID_ON_OST, 0);
3540 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3542 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3543 on_ost ? LMAC_FID_ON_OST : 0,
3546 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3547 obj->oo_dt.do_body_ops = &osd_body_ops;
3551 result = __osd_oi_insert(env, obj, fid, th);
3553 /* a small optimization - dt_insert() isn't usually applied
3554 * to OST objects, so we don't need to cache OI mapping for
3556 if (result == 0 && on_ost == 0) {
3557 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3558 result = osd_idc_find_and_init(env, osd, obj);
3559 LASSERT(result == 0);
3562 LASSERT(ergo(result == 0,
3563 dt_object_exists(dt) && !dt_object_remote(dt)));
3564 LINVRNT(osd_invariant(obj));
3568 static int osd_declare_object_ref_add(const struct lu_env *env,
3569 struct dt_object *dt,
3570 struct thandle *handle)
3572 struct osd_thandle *oh;
3574 /* it's possible that object doesn't exist yet */
3575 LASSERT(handle != NULL);
3577 oh = container_of0(handle, struct osd_thandle, ot_super);
3578 LASSERT(oh->ot_handle == NULL);
3580 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3581 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3587 * Concurrency: @dt is write locked.
3589 static int osd_object_ref_add(const struct lu_env *env,
3590 struct dt_object *dt, struct thandle *th)
3592 struct osd_object *obj = osd_dt_obj(dt);
3593 struct inode *inode = obj->oo_inode;
3594 struct osd_thandle *oh;
3597 if (!dt_object_exists(dt) || obj->oo_destroyed)
3600 LINVRNT(osd_invariant(obj));
3601 LASSERT(!dt_object_remote(dt));
3602 LASSERT(osd_write_locked(env, obj));
3603 LASSERT(th != NULL);
3605 oh = container_of0(th, struct osd_thandle, ot_super);
3606 LASSERT(oh->ot_handle != NULL);
3608 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3610 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3611 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3613 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3614 * (65000) subdirectories by storing "1" in i_nlink if the link count
3615 * would otherwise overflow. Directory tranversal tools understand
3616 * that (st_nlink == 1) indicates that the filesystem dose not track
3617 * hard links count on the directory, and will not abort subdirectory
3618 * scanning early once (st_nlink - 2) subdirs have been found.
3620 * This also has to properly handle the case of inodes with nlink == 0
3621 * in case they are being linked into the PENDING directory
3623 spin_lock(&obj->oo_guard);
3624 if (unlikely(inode->i_nlink == 0))
3625 /* inc_nlink from 0 may cause WARN_ON */
3626 set_nlink(inode, 1);
3628 ldiskfs_inc_count(oh->ot_handle, inode);
3629 if (!S_ISDIR(inode->i_mode))
3630 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3632 spin_unlock(&obj->oo_guard);
3634 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3635 LINVRNT(osd_invariant(obj));
3637 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3642 static int osd_declare_object_ref_del(const struct lu_env *env,
3643 struct dt_object *dt,
3644 struct thandle *handle)
3646 struct osd_thandle *oh;
3648 if (!dt_object_exists(dt))
3651 LASSERT(!dt_object_remote(dt));
3652 LASSERT(handle != NULL);
3654 oh = container_of0(handle, struct osd_thandle, ot_super);
3655 LASSERT(oh->ot_handle == NULL);
3657 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3658 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3664 * Concurrency: @dt is write locked.
3666 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3669 struct osd_object *obj = osd_dt_obj(dt);
3670 struct inode *inode = obj->oo_inode;
3671 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3672 struct osd_thandle *oh;
3674 if (!dt_object_exists(dt))
3677 LINVRNT(osd_invariant(obj));
3678 LASSERT(!dt_object_remote(dt));
3679 LASSERT(osd_write_locked(env, obj));
3680 LASSERT(th != NULL);
3682 oh = container_of0(th, struct osd_thandle, ot_super);
3683 LASSERT(oh->ot_handle != NULL);
3685 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3687 spin_lock(&obj->oo_guard);
3688 /* That can be result of upgrade from old Lustre version and
3689 * applied only to local files. Just skip this ref_del call.
3690 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3691 if (inode->i_nlink == 0) {
3692 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3693 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3694 ", maybe an upgraded file? (LU-3915)\n",
3695 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3696 spin_unlock(&obj->oo_guard);
3700 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3701 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3703 ldiskfs_dec_count(oh->ot_handle, inode);
3704 spin_unlock(&obj->oo_guard);
3706 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3707 LINVRNT(osd_invariant(obj));
3709 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3715 * Get the 64-bit version for an inode.
3717 static int osd_object_version_get(const struct lu_env *env,
3718 struct dt_object *dt, dt_obj_version_t *ver)
3720 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3722 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3723 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3724 *ver = LDISKFS_I(inode)->i_fs_version;
3729 * Concurrency: @dt is read locked.
3731 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3732 struct lu_buf *buf, const char *name)
3734 struct osd_object *obj = osd_dt_obj(dt);
3735 struct inode *inode = obj->oo_inode;
3736 struct osd_thread_info *info = osd_oti_get(env);
3737 struct dentry *dentry = &info->oti_obj_dentry;
3738 bool cache_xattr = false;
3741 /* version get is not real XATTR but uses xattr API */
3742 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3743 /* for version we are just using xattr API but change inode
3745 if (buf->lb_len == 0)
3746 return sizeof(dt_obj_version_t);
3748 if (buf->lb_len < sizeof(dt_obj_version_t))
3751 osd_object_version_get(env, dt, buf->lb_buf);
3753 return sizeof(dt_obj_version_t);
3756 if (!dt_object_exists(dt))
3759 LASSERT(!dt_object_remote(dt));
3760 LASSERT(inode->i_op != NULL);
3761 LASSERT(inode->i_op->getxattr != NULL);
3763 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3764 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3768 rc = osd_oxc_get(obj, name, buf);
3773 rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3775 if (rc == -ENOENT || rc == -ENODATA)
3776 osd_oxc_add(obj, name, NULL, 0);
3777 else if (rc > 0 && buf->lb_buf != NULL)
3778 osd_oxc_add(obj, name, buf->lb_buf, rc);
3784 static int osd_declare_xattr_set(const struct lu_env *env,
3785 struct dt_object *dt,
3786 const struct lu_buf *buf, const char *name,
3787 int fl, struct thandle *handle)
3789 struct osd_thandle *oh;
3791 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3793 LASSERT(handle != NULL);
3795 oh = container_of0(handle, struct osd_thandle, ot_super);
3796 LASSERT(oh->ot_handle == NULL);
3798 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3799 /* For non-upgrading case, the LMA is set first and
3800 * usually fit inode. But for upgrade case, the LMA
3801 * may be in another separated EA block. */
3802 if (!dt_object_exists(dt))
3804 else if (fl == LU_XATTR_REPLACE)
3808 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3812 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3817 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3818 /* learn xattr size from osd_xattr_get if
3819 attribute has not been read yet */
3820 buflen = __osd_xattr_get(
3821 osd_dt_obj(dt)->oo_inode,
3822 &osd_oti_get(env)->oti_obj_dentry,
3827 buflen = buf->lb_len;
3830 if (buflen > sb->s_blocksize) {
3831 credits += osd_calc_bkmap_credits(
3833 (buflen + sb->s_blocksize - 1) >>
3834 sb->s_blocksize_bits);
3838 * xattr set may involve inode quota change, reserve credits for
3839 * dquot_initialize()
3841 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3844 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3850 * Set the 64-bit version for object
3852 static void osd_object_version_set(const struct lu_env *env,
3853 struct dt_object *dt,
3854 dt_obj_version_t *new_version)
3856 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3858 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3859 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3861 LDISKFS_I(inode)->i_fs_version = *new_version;
3862 /** Version is set after all inode operations are finished,
3863 * so we should mark it dirty here */
3864 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3868 * Concurrency: @dt is write locked.
3870 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3871 const struct lu_buf *buf, const char *name, int fl,
3872 struct thandle *handle)
3874 struct osd_object *obj = osd_dt_obj(dt);
3875 struct inode *inode = obj->oo_inode;
3876 struct osd_thread_info *info = osd_oti_get(env);
3881 LASSERT(handle != NULL);
3883 /* version set is not real XATTR */
3884 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3885 /* for version we are just using xattr API but change inode
3887 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3888 osd_object_version_set(env, dt, buf->lb_buf);
3889 return sizeof(dt_obj_version_t);
3892 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3893 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3895 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3896 if (fl & LU_XATTR_REPLACE)
3897 fs_flags |= XATTR_REPLACE;
3899 if (fl & LU_XATTR_CREATE)
3900 fs_flags |= XATTR_CREATE;
3902 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3903 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3905 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3909 lma->lma_incompat |= LMAI_STRIPED;
3910 lustre_lma_swab(lma);
3911 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3912 sizeof(*lma), XATTR_REPLACE);
3917 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3919 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3922 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3923 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3924 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3930 * Concurrency: @dt is read locked.
3932 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3933 const struct lu_buf *buf)
3935 struct osd_object *obj = osd_dt_obj(dt);
3936 struct inode *inode = obj->oo_inode;
3937 struct osd_thread_info *info = osd_oti_get(env);
3938 struct dentry *dentry = &info->oti_obj_dentry;
3940 if (!dt_object_exists(dt))
3943 LASSERT(!dt_object_remote(dt));
3944 LASSERT(inode->i_op != NULL);
3945 LASSERT(inode->i_op->listxattr != NULL);
3947 dentry->d_inode = inode;
3948 dentry->d_sb = inode->i_sb;
3949 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3952 static int osd_declare_xattr_del(const struct lu_env *env,
3953 struct dt_object *dt, const char *name,
3954 struct thandle *handle)
3956 struct osd_thandle *oh;
3957 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3959 LASSERT(!dt_object_remote(dt));
3960 LASSERT(handle != NULL);
3962 oh = container_of0(handle, struct osd_thandle, ot_super);
3963 LASSERT(oh->ot_handle == NULL);
3965 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3966 osd_dto_credits_noquota[DTO_XATTR_SET]);
3968 * xattr del may involve inode quota change, reserve credits for
3969 * dquot_initialize()
3971 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3977 * Concurrency: @dt is write locked.
3979 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3980 const char *name, struct thandle *handle)
3982 struct osd_object *obj = osd_dt_obj(dt);
3983 struct inode *inode = obj->oo_inode;
3984 struct osd_thread_info *info = osd_oti_get(env);
3985 struct dentry *dentry = &info->oti_obj_dentry;
3988 if (!dt_object_exists(dt))
3991 LASSERT(!dt_object_remote(dt));
3992 LASSERT(inode->i_op != NULL);
3993 LASSERT(inode->i_op->removexattr != NULL);
3994 LASSERT(handle != NULL);
3996 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3998 ll_vfs_dq_init(inode);
3999 dentry->d_inode = inode;
4000 dentry->d_sb = inode->i_sb;
4001 rc = inode->i_op->removexattr(dentry, name);
4002 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4005 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4006 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4007 osd_oxc_del(obj, name);
4012 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4013 __u64 start, __u64 end)
4015 struct osd_object *obj = osd_dt_obj(dt);
4016 struct inode *inode = obj->oo_inode;
4017 struct osd_thread_info *info = osd_oti_get(env);
4018 struct dentry *dentry = &info->oti_obj_dentry;
4019 struct file *file = &info->oti_file;
4024 dentry->d_inode = inode;
4025 dentry->d_sb = inode->i_sb;
4026 file->f_path.dentry = dentry;
4027 file->f_mapping = inode->i_mapping;
4028 file->f_op = inode->i_fop;
4029 set_file_inode(file, inode);
4031 rc = ll_vfs_fsync_range(file, start, end, 0);
4036 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4045 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4046 const struct dt_index_features *feat)
4048 struct iam_descr *descr;
4050 if (osd_object_is_root(o))
4051 return feat == &dt_directory_features;
4053 LASSERT(o->oo_dir != NULL);
4055 descr = o->oo_dir->od_container.ic_descr;
4056 if (feat == &dt_directory_features) {
4057 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4063 feat->dif_keysize_min <= descr->id_key_size &&
4064 descr->id_key_size <= feat->dif_keysize_max &&
4065 feat->dif_recsize_min <= descr->id_rec_size &&
4066 descr->id_rec_size <= feat->dif_recsize_max &&
4067 !(feat->dif_flags & (DT_IND_VARKEY |
4068 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4069 ergo(feat->dif_flags & DT_IND_UPDATE,
4070 1 /* XXX check that object (and file system) is
4075 static int osd_iam_container_init(const struct lu_env *env,
4076 struct osd_object *obj,
4077 struct osd_directory *dir)
4079 struct iam_container *bag = &dir->od_container;
4082 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4086 result = iam_container_setup(bag);
4088 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4090 iam_container_fini(bag);
4097 * Concurrency: no external locking is necessary.
4099 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4100 const struct dt_index_features *feat)
4104 struct osd_object *obj = osd_dt_obj(dt);
4106 LINVRNT(osd_invariant(obj));
4108 if (osd_object_is_root(obj)) {
4109 dt->do_index_ops = &osd_index_ea_ops;
4111 } else if (feat == &dt_directory_features) {
4112 dt->do_index_ops = &osd_index_ea_ops;
4113 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4118 } else if (unlikely(feat == &dt_otable_features)) {
4119 dt->do_index_ops = &osd_otable_ops;
4121 } else if (unlikely(feat == &dt_acct_features)) {
4122 dt->do_index_ops = &osd_acct_index_ops;
4125 } else if (!osd_has_index(obj)) {
4126 struct osd_directory *dir;
4131 spin_lock(&obj->oo_guard);
4132 if (obj->oo_dir == NULL)
4136 * Concurrent thread allocated container data.
4139 spin_unlock(&obj->oo_guard);
4141 * Now, that we have container data, serialize its
4144 down_write(&obj->oo_ext_idx_sem);
4146 * recheck under lock.
4148 if (!osd_has_index(obj))
4149 result = osd_iam_container_init(env, obj,
4153 up_write(&obj->oo_ext_idx_sem);
4161 if (result == 0 && skip_iam == 0) {
4162 if (!osd_iam_index_probe(env, obj, feat))
4165 LINVRNT(osd_invariant(obj));
4167 if (result == 0 && feat == &dt_quota_glb_features &&
4168 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4169 result = osd_quota_migration(env, dt);
4174 static int osd_otable_it_attr_get(const struct lu_env *env,
4175 struct dt_object *dt,
4176 struct lu_attr *attr)
4182 static const struct dt_object_operations osd_obj_ops = {
4183 .do_read_lock = osd_object_read_lock,
4184 .do_write_lock = osd_object_write_lock,
4185 .do_read_unlock = osd_object_read_unlock,
4186 .do_write_unlock = osd_object_write_unlock,
4187 .do_write_locked = osd_object_write_locked,
4188 .do_attr_get = osd_attr_get,
4189 .do_declare_attr_set = osd_declare_attr_set,
4190 .do_attr_set = osd_attr_set,
4191 .do_ah_init = osd_ah_init,
4192 .do_declare_create = osd_declare_object_create,
4193 .do_create = osd_object_create,
4194 .do_declare_destroy = osd_declare_object_destroy,
4195 .do_destroy = osd_object_destroy,
4196 .do_index_try = osd_index_try,
4197 .do_declare_ref_add = osd_declare_object_ref_add,
4198 .do_ref_add = osd_object_ref_add,
4199 .do_declare_ref_del = osd_declare_object_ref_del,
4200 .do_ref_del = osd_object_ref_del,
4201 .do_xattr_get = osd_xattr_get,
4202 .do_declare_xattr_set = osd_declare_xattr_set,
4203 .do_xattr_set = osd_xattr_set,
4204 .do_declare_xattr_del = osd_declare_xattr_del,
4205 .do_xattr_del = osd_xattr_del,
4206 .do_xattr_list = osd_xattr_list,
4207 .do_object_sync = osd_object_sync,
4208 .do_invalidate = osd_invalidate,
4212 * dt_object_operations for interoperability mode
4213 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4215 static const struct dt_object_operations osd_obj_ea_ops = {
4216 .do_read_lock = osd_object_read_lock,
4217 .do_write_lock = osd_object_write_lock,
4218 .do_read_unlock = osd_object_read_unlock,
4219 .do_write_unlock = osd_object_write_unlock,
4220 .do_write_locked = osd_object_write_locked,
4221 .do_attr_get = osd_attr_get,
4222 .do_declare_attr_set = osd_declare_attr_set,
4223 .do_attr_set = osd_attr_set,
4224 .do_ah_init = osd_ah_init,
4225 .do_declare_create = osd_declare_object_create,
4226 .do_create = osd_object_ea_create,
4227 .do_declare_destroy = osd_declare_object_destroy,
4228 .do_destroy = osd_object_destroy,
4229 .do_index_try = osd_index_try,
4230 .do_declare_ref_add = osd_declare_object_ref_add,
4231 .do_ref_add = osd_object_ref_add,
4232 .do_declare_ref_del = osd_declare_object_ref_del,
4233 .do_ref_del = osd_object_ref_del,
4234 .do_xattr_get = osd_xattr_get,
4235 .do_declare_xattr_set = osd_declare_xattr_set,
4236 .do_xattr_set = osd_xattr_set,
4237 .do_declare_xattr_del = osd_declare_xattr_del,
4238 .do_xattr_del = osd_xattr_del,
4239 .do_xattr_list = osd_xattr_list,
4240 .do_object_sync = osd_object_sync,
4241 .do_invalidate = osd_invalidate,
4244 static const struct dt_object_operations osd_obj_otable_it_ops = {
4245 .do_attr_get = osd_otable_it_attr_get,
4246 .do_index_try = osd_index_try,
4249 static int osd_index_declare_iam_delete(const struct lu_env *env,
4250 struct dt_object *dt,
4251 const struct dt_key *key,
4252 struct thandle *handle)
4254 struct osd_thandle *oh;
4256 oh = container_of0(handle, struct osd_thandle, ot_super);
4257 LASSERT(oh->ot_handle == NULL);
4259 /* Recycle may cause additional three blocks to be changed. */
4260 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4261 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4267 * delete a (key, value) pair from index \a dt specified by \a key
4269 * \param dt osd index object
4270 * \param key key for index
4271 * \param rec record reference
4272 * \param handle transaction handler
4275 * \retval -ve failure
4277 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4278 const struct dt_key *key,
4279 struct thandle *handle)
4281 struct osd_thread_info *oti = osd_oti_get(env);
4282 struct osd_object *obj = osd_dt_obj(dt);
4283 struct osd_thandle *oh;
4284 struct iam_path_descr *ipd;
4285 struct iam_container *bag = &obj->oo_dir->od_container;
4289 if (!dt_object_exists(dt))
4292 LINVRNT(osd_invariant(obj));
4293 LASSERT(!dt_object_remote(dt));
4294 LASSERT(bag->ic_object == obj->oo_inode);
4295 LASSERT(handle != NULL);
4297 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4299 ipd = osd_idx_ipd_get(env, bag);
4300 if (unlikely(ipd == NULL))
4303 oh = container_of0(handle, struct osd_thandle, ot_super);
4304 LASSERT(oh->ot_handle != NULL);
4305 LASSERT(oh->ot_handle->h_transaction != NULL);
4307 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4308 /* swab quota uid/gid provided by caller */
4309 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4310 key = (const struct dt_key *)&oti->oti_quota_id;
4313 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4314 osd_ipd_put(env, bag, ipd);
4315 LINVRNT(osd_invariant(obj));
4316 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4320 static int osd_index_declare_ea_delete(const struct lu_env *env,
4321 struct dt_object *dt,
4322 const struct dt_key *key,
4323 struct thandle *handle)
4325 struct osd_thandle *oh;
4326 struct inode *inode;
4330 LASSERT(!dt_object_remote(dt));
4331 LASSERT(handle != NULL);
4333 oh = container_of0(handle, struct osd_thandle, ot_super);
4334 LASSERT(oh->ot_handle == NULL);
4336 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4337 if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4338 /* '..' to a remote object has a local representative */
4339 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4340 /* to reset LMAI_REMOTE_PARENT */
4343 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4345 inode = osd_dt_obj(dt)->oo_inode;
4349 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4350 0, oh, osd_dt_obj(dt), true, NULL, false);
4354 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4357 struct osd_fid_pack *rec;
4360 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4361 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4362 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4363 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4369 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4370 const struct lu_fid *fid)
4372 struct seq_server_site *ss = osd_seq_site(osd);
4375 /* FID seqs not in FLDB, must be local seq */
4376 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4379 /* If FLD is not being initialized yet, it only happens during the
4380 * initialization, likely during mgs initialization, and we assume
4381 * this is local FID. */
4382 if (ss == NULL || ss->ss_server_fld == NULL)
4385 /* Only check the local FLDB here */
4386 if (osd_seq_exists(env, osd, fid_seq(fid)))
4393 * Index delete function for interoperability mode (b11826).
4394 * It will remove the directory entry added by osd_index_ea_insert().
4395 * This entry is needed to maintain name->fid mapping.
4397 * \param key, key i.e. file entry to be deleted
4399 * \retval 0, on success
4400 * \retval -ve, on error
4402 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4403 const struct dt_key *key, struct thandle *handle)
4405 struct osd_object *obj = osd_dt_obj(dt);
4406 struct inode *dir = obj->oo_inode;
4407 struct dentry *dentry;
4408 struct osd_thandle *oh;
4409 struct ldiskfs_dir_entry_2 *de = NULL;
4410 struct buffer_head *bh;
4411 struct htree_lock *hlock = NULL;
4412 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4413 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4417 if (!dt_object_exists(dt))
4420 LINVRNT(osd_invariant(obj));
4421 LASSERT(!dt_object_remote(dt));
4422 LASSERT(handle != NULL);
4424 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4426 oh = container_of(handle, struct osd_thandle, ot_super);
4427 LASSERT(oh->ot_handle != NULL);
4428 LASSERT(oh->ot_handle->h_transaction != NULL);
4430 ll_vfs_dq_init(dir);
4431 dentry = osd_child_dentry_get(env, obj,
4432 (char *)key, strlen((char *)key));
4434 if (obj->oo_hl_head != NULL) {
4435 hlock = osd_oti_get(env)->oti_hlock;
4436 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4437 dir, LDISKFS_HLOCK_DEL);
4439 down_write(&obj->oo_ext_idx_sem);
4442 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4444 /* If this is not the ".." entry, it might be a remote DNE
4445 * entry and we need to check if the FID is for a remote
4446 * MDT. If the FID is not in the directory entry (e.g.
4447 * upgraded 1.8 filesystem without dirdata enabled) then
4448 * we need to get the FID from the LMA. For a remote directory
4449 * there HAS to be an LMA, it cannot be an IGIF inode in this
4452 * Delete the entry before the agent inode in order to
4453 * simplify error handling. At worst an error after deleting
4454 * the entry first might leak the agent inode afterward. The
4455 * reverse would need filesystem abort in case of error deleting
4456 * the entry after the agent had been removed, or leave a
4457 * dangling entry pointing at a random inode. */
4458 if (strcmp((char *)key, dotdot) != 0) {
4459 LASSERT(de != NULL);
4460 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4461 if (rc == -ENODATA) {
4462 /* can't get FID, postpone to the end of the
4463 * transaction when iget() is safe */
4464 osd_schedule_agent_inode_removal(env, oh,
4465 le32_to_cpu(de->inode));
4466 } else if (rc == 0 &&
4467 unlikely(osd_remote_fid(env, osd, fid))) {
4468 osd_schedule_agent_inode_removal(env, oh,
4469 le32_to_cpu(de->inode));
4472 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4478 ldiskfs_htree_unlock(hlock);
4480 up_write(&obj->oo_ext_idx_sem);
4485 /* For inode on the remote MDT, .. will point to
4486 * /Agent directory, Check whether it needs to delete
4487 * from agent directory */
4488 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4491 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4494 /* Sigh, the entry has been deleted, and
4495 * it is not easy to revert it back, so
4496 * let's keep this error private, and let
4497 * LFSCK fix it. XXX */
4498 CERROR("%s: delete remote parent "DFID": rc = %d\n",
4499 osd_name(osd), PFID(fid), ret);
4502 LASSERT(osd_invariant(obj));
4503 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4508 * Lookup index for \a key and copy record to \a rec.
4510 * \param dt osd index object
4511 * \param key key for index
4512 * \param rec record reference
4514 * \retval +ve success : exact mach
4515 * \retval 0 return record with key not greater than \a key
4516 * \retval -ve failure
4518 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4519 struct dt_rec *rec, const struct dt_key *key)
4521 struct osd_object *obj = osd_dt_obj(dt);
4522 struct iam_path_descr *ipd;
4523 struct iam_container *bag = &obj->oo_dir->od_container;
4524 struct osd_thread_info *oti = osd_oti_get(env);
4525 struct iam_iterator *it = &oti->oti_idx_it;
4526 struct iam_rec *iam_rec;
4530 if (!dt_object_exists(dt))
4533 LASSERT(osd_invariant(obj));
4534 LASSERT(!dt_object_remote(dt));
4535 LASSERT(bag->ic_object == obj->oo_inode);
4537 ipd = osd_idx_ipd_get(env, bag);
4541 /* got ipd now we can start iterator. */
4542 iam_it_init(it, bag, 0, ipd);
4544 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4545 /* swab quota uid/gid provided by caller */
4546 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4547 key = (const struct dt_key *)&oti->oti_quota_id;
4550 rc = iam_it_get(it, (struct iam_key *)key);
4552 if (S_ISDIR(obj->oo_inode->i_mode))
4553 iam_rec = (struct iam_rec *)oti->oti_ldp;
4555 iam_rec = (struct iam_rec *) rec;
4557 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4559 if (S_ISDIR(obj->oo_inode->i_mode))
4560 osd_fid_unpack((struct lu_fid *) rec,
4561 (struct osd_fid_pack *)iam_rec);
4562 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4563 osd_quota_unpack(obj, rec);
4568 osd_ipd_put(env, bag, ipd);
4570 LINVRNT(osd_invariant(obj));
4575 static int osd_index_declare_iam_insert(const struct lu_env *env,
4576 struct dt_object *dt,
4577 const struct dt_rec *rec,
4578 const struct dt_key *key,
4579 struct thandle *handle)
4581 struct osd_thandle *oh;
4583 LASSERT(handle != NULL);
4585 oh = container_of0(handle, struct osd_thandle, ot_super);
4586 LASSERT(oh->ot_handle == NULL);
4588 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4589 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4595 * Inserts (key, value) pair in \a dt index object.
4597 * \param dt osd index object
4598 * \param key key for index
4599 * \param rec record reference
4600 * \param th transaction handler
4603 * \retval -ve failure
4605 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4606 const struct dt_rec *rec,
4607 const struct dt_key *key, struct thandle *th,
4610 struct osd_object *obj = osd_dt_obj(dt);
4611 struct iam_path_descr *ipd;
4612 struct osd_thandle *oh;
4613 struct iam_container *bag;
4614 struct osd_thread_info *oti = osd_oti_get(env);
4615 struct iam_rec *iam_rec;
4619 if (!dt_object_exists(dt))
4622 LINVRNT(osd_invariant(obj));
4623 LASSERT(!dt_object_remote(dt));
4625 bag = &obj->oo_dir->od_container;
4626 LASSERT(bag->ic_object == obj->oo_inode);
4627 LASSERT(th != NULL);
4629 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4631 ipd = osd_idx_ipd_get(env, bag);
4632 if (unlikely(ipd == NULL))
4635 oh = container_of0(th, struct osd_thandle, ot_super);
4636 LASSERT(oh->ot_handle != NULL);
4637 LASSERT(oh->ot_handle->h_transaction != NULL);
4638 if (S_ISDIR(obj->oo_inode->i_mode)) {
4639 iam_rec = (struct iam_rec *)oti->oti_ldp;
4640 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4641 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4642 /* pack quota uid/gid */
4643 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4644 key = (const struct dt_key *)&oti->oti_quota_id;
4645 /* pack quota record */
4646 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4647 iam_rec = (struct iam_rec *)rec;
4649 iam_rec = (struct iam_rec *)rec;
4652 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4654 osd_ipd_put(env, bag, ipd);
4655 LINVRNT(osd_invariant(obj));
4656 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4661 * Calls ldiskfs_add_entry() to add directory entry
4662 * into the directory. This is required for
4663 * interoperability mode (b11826)
4665 * \retval 0, on success
4666 * \retval -ve, on error
4668 static int __osd_ea_add_rec(struct osd_thread_info *info,
4669 struct osd_object *pobj, struct inode *cinode,
4670 const char *name, const struct lu_fid *fid,
4671 struct htree_lock *hlock, struct thandle *th)
4673 struct ldiskfs_dentry_param *ldp;
4674 struct dentry *child;
4675 struct osd_thandle *oth;
4678 oth = container_of(th, struct osd_thandle, ot_super);
4679 LASSERT(oth->ot_handle != NULL);
4680 LASSERT(oth->ot_handle->h_transaction != NULL);
4681 LASSERT(pobj->oo_inode);
4683 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4684 if (unlikely(pobj->oo_inode ==
4685 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4688 osd_get_ldiskfs_dirent_param(ldp, fid);
4689 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4690 child->d_fsdata = (void *)ldp;
4691 ll_vfs_dq_init(pobj->oo_inode);
4692 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4693 child, cinode, hlock);
4694 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4695 struct ldiskfs_dir_entry_2 *de;
4696 struct buffer_head *bh;
4699 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4702 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4705 if (S_ISDIR(cinode->i_mode))
4706 de->file_type = LDISKFS_DIRENT_LUFID |
4707 LDISKFS_FT_REG_FILE;
4709 de->file_type = LDISKFS_DIRENT_LUFID |
4711 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4722 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4723 * into the directory.Also sets flags into osd object to
4724 * indicate dot and dotdot are created. This is required for
4725 * interoperability mode (b11826)
4727 * \param dir directory for dot and dotdot fixup.
4728 * \param obj child object for linking
4730 * \retval 0, on success
4731 * \retval -ve, on error
4733 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4734 struct osd_object *dir,
4735 struct inode *parent_dir, const char *name,
4736 const struct lu_fid *dot_fid,
4737 const struct lu_fid *dot_dot_fid,
4740 struct inode *inode = dir->oo_inode;
4741 struct osd_thandle *oth;
4744 oth = container_of(th, struct osd_thandle, ot_super);
4745 LASSERT(oth->ot_handle->h_transaction != NULL);
4746 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4748 if (strcmp(name, dot) == 0) {
4749 if (dir->oo_compat_dot_created) {
4752 LASSERT(inode->i_ino == parent_dir->i_ino);
4753 dir->oo_compat_dot_created = 1;
4756 } else if (strcmp(name, dotdot) == 0) {
4757 if (!dir->oo_compat_dot_created)
4759 /* in case of rename, dotdot is already created */
4760 if (dir->oo_compat_dotdot_created) {
4761 return __osd_ea_add_rec(info, dir, parent_dir, name,
4762 dot_dot_fid, NULL, th);
4765 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4766 struct lu_fid tfid = *dot_dot_fid;
4769 result = osd_add_dot_dotdot_internal(info,
4770 dir->oo_inode, parent_dir, dot_fid,
4773 result = osd_add_dot_dotdot_internal(info,
4774 dir->oo_inode, parent_dir, dot_fid,
4779 dir->oo_compat_dotdot_created = 1;
4787 * It will call the appropriate osd_add* function and return the
4788 * value, return by respective functions.
4790 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4791 struct inode *cinode, const char *name,
4792 const struct lu_fid *fid, struct thandle *th)
4794 struct osd_thread_info *info = osd_oti_get(env);
4795 struct htree_lock *hlock;
4798 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4800 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4802 if (hlock != NULL) {
4803 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4806 down_write(&pobj->oo_ext_idx_sem);
4809 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4810 lu_object_fid(&pobj->oo_dt.do_lu),
4813 if (hlock != NULL) {
4814 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4815 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4817 down_write(&pobj->oo_ext_idx_sem);
4820 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4821 struct lu_fid *tfid = &info->oti_fid;
4825 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4828 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4833 ldiskfs_htree_unlock(hlock);
4835 up_write(&pobj->oo_ext_idx_sem);
4841 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4842 struct osd_idmap_cache *oic)
4844 struct osd_scrub *scrub = &dev->od_scrub;
4845 struct lu_fid *fid = &oic->oic_fid;
4846 struct osd_inode_id *id = &oic->oic_lid;
4847 struct inode *inode = NULL;
4853 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4856 if (scrub->os_pos_current > id->oii_ino)
4860 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4861 if (rc == -ENOENT) {
4862 __u32 gen = id->oii_gen;
4868 inode = osd_iget(oti, dev, id);
4869 /* The inode has been removed (by race maybe). */
4870 if (IS_ERR(inode)) {
4871 rc = PTR_ERR(inode);
4873 RETURN(rc == -ESTALE ? -ENOENT : rc);
4876 /* The OI mapping is lost. */
4877 if (gen != OSD_OII_NOGEN)
4881 /* The inode may has been reused by others, we do not know,
4882 * leave it to be handled by subsequent osd_fid_lookup(). */
4884 } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4891 if (thread_is_running(&scrub->os_thread)) {
4892 if (inode == NULL) {
4893 inode = osd_iget(oti, dev, id);
4894 /* The inode has been removed (by race maybe). */
4895 if (IS_ERR(inode)) {
4896 rc = PTR_ERR(inode);
4898 RETURN(rc == -ESTALE ? -ENOENT : rc);
4902 rc = osd_oii_insert(dev, oic, insert);
4903 /* There is race condition between osd_oi_lookup and OI scrub.
4904 * The OI scrub finished just after osd_oi_lookup() failure.
4905 * Under such case, it is unnecessary to trigger OI scrub again,
4906 * but try to call osd_oi_lookup() again. */
4907 if (unlikely(rc == -EAGAIN))
4910 if (!S_ISDIR(inode->i_mode))
4913 rc = osd_check_lmv(oti, dev, inode, oic);
4919 if (!dev->od_noscrub && ++once == 1) {
4920 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4922 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4923 "%.16s: trigger partial OI scrub for RPC inconsistency "
4924 "checking FID "DFID": rc = %d\n",
4925 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4927 if (rc == 0 || rc == -EALREADY)
4937 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4938 struct osd_device *dev,
4939 struct osd_idmap_cache *oic,
4940 struct lu_fid *fid, __u32 ino)
4942 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4943 struct inode *inode;
4946 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4947 inode = osd_iget(oti, dev, &oic->oic_lid);
4948 if (IS_ERR(inode)) {
4949 fid_zero(&oic->oic_fid);
4950 return PTR_ERR(inode);
4953 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4956 fid_zero(&oic->oic_fid);
4958 *fid = oic->oic_fid = lma->lma_self_fid;
4962 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4963 struct osd_inode_id *id, const struct lu_fid *fid)
4965 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4966 id->oii_ino, id->oii_gen, info);
4967 info->oti_cache.oic_lid = *id;
4968 info->oti_cache.oic_fid = *fid;
4969 info->oti_cache.oic_dev = osd;
4973 * Get parent FID from the linkEA.
4975 * For a directory which parent resides on remote MDT, to satisfy the
4976 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4977 * the other hand, to make the lookup(..) on the directory can return
4978 * the real parent FID, we append the real parent FID after its ".."
4979 * name entry in the /REMOTE_PARENT_DIR.
4981 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4982 * backup. So after the restore, we cannot get the right parent FID from
4983 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4984 * we have stored the real parent FID in the directory object's linkEA,
4985 * we can parse the linkEA for the real parent FID.
4987 * \param[in] env pointer to the thread context
4988 * \param[in] obj pointer to the object to be handled
4989 * \param[out]fid pointer to the buffer to hold the parent FID
4991 * \retval 0 for getting the real parent FID successfully
4992 * \retval negative error number on failure
4994 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4995 struct osd_object *obj,
4998 struct osd_thread_info *oti = osd_oti_get(env);
4999 struct lu_buf *buf = &oti->oti_big_buf;
5000 struct dentry *dentry = &oti->oti_obj_dentry;
5001 struct inode *inode = obj->oo_inode;
5002 struct linkea_data ldata = { NULL };
5007 if (!S_ISDIR(inode->i_mode))
5011 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5012 buf->lb_buf, buf->lb_len);
5013 if (rc == -ERANGE) {
5014 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5017 lu_buf_realloc(buf, rc);
5018 if (buf->lb_buf == NULL)
5025 if (unlikely(rc == 0))
5031 if (unlikely(buf->lb_buf == NULL)) {
5032 lu_buf_realloc(buf, rc);
5033 if (buf->lb_buf == NULL)
5040 rc = linkea_init_with_rec(&ldata);
5042 linkea_first_entry(&ldata);
5043 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5049 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5050 struct inode *inode,
5051 const struct lu_fid *pfid,
5052 const char *name, const int namelen)
5054 struct osd_thread_info *oti = osd_oti_get(env);
5055 struct lu_buf *buf = &oti->oti_big_buf;
5056 struct dentry *dentry = &oti->oti_obj_dentry;
5057 struct linkea_data ldata = { NULL };
5058 struct lu_name cname = { .ln_name = name,
5059 .ln_namelen = namelen };
5064 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5065 buf->lb_buf, buf->lb_len);
5067 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5072 if (unlikely(rc == 0))
5075 if (buf->lb_len < rc) {
5076 lu_buf_realloc(buf, rc);
5077 if (buf->lb_buf == NULL)
5084 rc = linkea_init_with_rec(&ldata);
5086 rc = linkea_links_find(&ldata, &cname, pfid);
5092 * Calls ->lookup() to find dentry. From dentry get inode and
5093 * read inode's ea to get fid. This is required for interoperability
5096 * \retval 0, on success
5097 * \retval -ve, on error
5099 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5100 struct dt_rec *rec, const struct dt_key *key)
5102 struct inode *dir = obj->oo_inode;
5103 struct dentry *dentry;
5104 struct ldiskfs_dir_entry_2 *de;
5105 struct buffer_head *bh;
5106 struct lu_fid *fid = (struct lu_fid *) rec;
5107 struct htree_lock *hlock = NULL;
5112 LASSERT(dir->i_op != NULL);
5113 LASSERT(dir->i_op->lookup != NULL);
5115 dentry = osd_child_dentry_get(env, obj,
5116 (char *)key, strlen((char *)key));
5118 if (obj->oo_hl_head != NULL) {
5119 hlock = osd_oti_get(env)->oti_hlock;
5120 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5121 dir, LDISKFS_HLOCK_LOOKUP);
5123 down_read(&obj->oo_ext_idx_sem);
5126 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5128 struct osd_thread_info *oti = osd_oti_get(env);
5129 struct osd_inode_id *id = &oti->oti_id;
5130 struct osd_idmap_cache *oic = &oti->oti_cache;
5131 struct osd_device *dev = osd_obj2dev(obj);
5133 ino = le32_to_cpu(de->inode);
5134 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5136 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5140 rc = osd_get_fid_from_dentry(de, rec);
5142 /* done with de, release bh */
5145 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5146 const char *name = (const char *)key;
5148 /* If the parent is on remote MDT, and there
5149 * is no FID-in-dirent, then we have to get
5150 * the parent FID from the linkEA. */
5151 if (likely(strlen(name) == 2 &&
5152 name[0] == '.' && name[1] == '.'))
5153 rc = osd_get_pfid_from_linkea(env, obj,
5156 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5159 osd_id_gen(id, ino, OSD_OII_NOGEN);
5162 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5163 fid_zero(&oic->oic_fid);
5168 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5169 rc = osd_consistency_check(oti, dev, oic);
5171 fid_zero(&oic->oic_fid);
5180 ldiskfs_htree_unlock(hlock);
5182 up_read(&obj->oo_ext_idx_sem);
5187 * Put the osd object once done with it.
5189 * \param obj osd object that needs to be put
5191 static inline void osd_object_put(const struct lu_env *env,
5192 struct osd_object *obj)
5194 lu_object_put(env, &obj->oo_dt.do_lu);
5197 static int osd_index_declare_ea_insert(const struct lu_env *env,
5198 struct dt_object *dt,
5199 const struct dt_rec *rec,
5200 const struct dt_key *key,
5201 struct thandle *handle)
5203 struct osd_thandle *oh;
5204 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5205 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5206 const struct lu_fid *fid = rec1->rec_fid;
5207 int credits, rc = 0;
5208 struct osd_idmap_cache *idc;
5211 LASSERT(!dt_object_remote(dt));
5212 LASSERT(handle != NULL);
5213 LASSERT(fid != NULL);
5214 LASSERT(rec1->rec_type != 0);
5216 oh = container_of0(handle, struct osd_thandle, ot_super);
5217 LASSERT(oh->ot_handle == NULL);
5219 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5221 /* we can't call iget() while a transactions is running
5222 * (this can lead to a deadlock), but we need to know
5223 * inum and object type. so we find this information at
5224 * declaration and cache in per-thread info */
5225 idc = osd_idc_find_or_init(env, osd, fid);
5227 RETURN(PTR_ERR(idc));
5228 if (idc->oic_remote) {
5229 /* a reference to remote inode is represented by an
5230 * agent inode which we have to create */
5231 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5232 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5235 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5237 if (osd_dt_obj(dt)->oo_inode != NULL) {
5238 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5240 /* We ignore block quota on meta pool (MDTs), so needn't
5241 * calculate how many blocks will be consumed by this index
5243 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5244 i_gid_read(inode), 0, oh,
5245 osd_dt_obj(dt), true, NULL, false);
5252 * Index add function for interoperability mode (b11826).
5253 * It will add the directory entry.This entry is needed to
5254 * maintain name->fid mapping.
5256 * \param key it is key i.e. file entry to be inserted
5257 * \param rec it is value of given key i.e. fid
5259 * \retval 0, on success
5260 * \retval -ve, on error
5262 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5263 const struct dt_rec *rec,
5264 const struct dt_key *key, struct thandle *th,
5267 struct osd_object *obj = osd_dt_obj(dt);
5268 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5269 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5270 const struct lu_fid *fid = rec1->rec_fid;
5271 const char *name = (const char *)key;
5272 struct osd_thread_info *oti = osd_oti_get(env);
5273 struct inode *child_inode = NULL;
5274 struct osd_idmap_cache *idc;
5278 if (!dt_object_exists(dt))
5281 LASSERT(osd_invariant(obj));
5282 LASSERT(!dt_object_remote(dt));
5283 LASSERT(th != NULL);
5285 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5287 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5289 idc = osd_idc_find(env, osd, fid);
5290 if (unlikely(idc == NULL)) {
5291 /* this dt_insert() wasn't declared properly, so
5292 * FID is missing in OI cache. we better do not
5293 * lookup FID in FLDB/OI and don't risk to deadlock,
5294 * but in some special cases (lfsck testing, etc)
5295 * it's much simpler than fixing a caller */
5296 CERROR("%s: "DFID" wasn't declared for insert\n",
5297 osd_name(osd), PFID(fid));
5299 idc = osd_idc_find_or_init(env, osd, fid);
5301 RETURN(PTR_ERR(idc));
5304 if (idc->oic_remote) {
5305 /* Insert remote entry */
5306 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5307 struct osd_mdobj_map *omm = osd->od_mdt_map;
5308 struct osd_thandle *oh;
5310 /* If parent on remote MDT, we need put this object
5312 oh = container_of(th, typeof(*oh), ot_super);
5313 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5315 CERROR("%s: add "DFID" error: rc = %d\n",
5317 PFID(lu_object_fid(&dt->do_lu)), rc);
5321 child_inode = igrab(omm->omm_remote_parent->d_inode);
5323 child_inode = osd_create_local_agent_inode(env, osd,
5324 obj, fid, rec1->rec_type & S_IFMT, th);
5325 if (IS_ERR(child_inode))
5326 RETURN(PTR_ERR(child_inode));
5329 /* Insert local entry */
5330 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5331 /* for a reason OI cache wasn't filled properly */
5332 CERROR("%s: OIC for "DFID" isn't filled\n",
5333 osd_name(osd), PFID(fid));
5336 child_inode = oti->oti_inode;
5337 if (unlikely(child_inode == NULL)) {
5338 struct ldiskfs_inode_info *lii;
5342 child_inode = oti->oti_inode = &lii->vfs_inode;
5344 child_inode->i_sb = osd_sb(osd);
5345 child_inode->i_ino = idc->oic_lid.oii_ino;
5346 child_inode->i_mode = rec1->rec_type & S_IFMT;
5349 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5351 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5352 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5354 if (child_inode && child_inode != oti->oti_inode)
5356 LASSERT(osd_invariant(obj));
5357 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5362 * Initialize osd Iterator for given osd index object.
5364 * \param dt osd index object
5367 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5368 struct dt_object *dt,
5371 struct osd_it_iam *it;
5372 struct osd_object *obj = osd_dt_obj(dt);
5373 struct lu_object *lo = &dt->do_lu;
5374 struct iam_path_descr *ipd;
5375 struct iam_container *bag = &obj->oo_dir->od_container;
5377 if (!dt_object_exists(dt))
5378 return ERR_PTR(-ENOENT);
5382 return ERR_PTR(-ENOMEM);
5384 ipd = osd_it_ipd_get(env, bag);
5385 if (likely(ipd != NULL)) {
5389 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5390 return (struct dt_it *)it;
5393 return ERR_PTR(-ENOMEM);
5398 * free given Iterator.
5401 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5403 struct osd_it_iam *it = (struct osd_it_iam *)di;
5404 struct osd_object *obj = it->oi_obj;
5406 iam_it_fini(&it->oi_it);
5407 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5408 lu_object_put(env, &obj->oo_dt.do_lu);
5413 * Move Iterator to record specified by \a key
5415 * \param di osd iterator
5416 * \param key key for index
5418 * \retval +ve di points to record with least key not larger than key
5419 * \retval 0 di points to exact matched key
5420 * \retval -ve failure
5423 static int osd_it_iam_get(const struct lu_env *env,
5424 struct dt_it *di, const struct dt_key *key)
5426 struct osd_thread_info *oti = osd_oti_get(env);
5427 struct osd_it_iam *it = (struct osd_it_iam *)di;
5429 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5430 /* swab quota uid/gid */
5431 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5432 key = (struct dt_key *)&oti->oti_quota_id;
5435 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5441 * \param di osd iterator
5443 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5445 struct osd_it_iam *it = (struct osd_it_iam *)di;
5447 iam_it_put(&it->oi_it);
5451 * Move iterator by one record
5453 * \param di osd iterator
5455 * \retval +1 end of container reached
5457 * \retval -ve failure
5460 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5462 struct osd_it_iam *it = (struct osd_it_iam *)di;
5464 return iam_it_next(&it->oi_it);
5468 * Return pointer to the key under iterator.
5471 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5472 const struct dt_it *di)
5474 struct osd_thread_info *oti = osd_oti_get(env);
5475 struct osd_it_iam *it = (struct osd_it_iam *)di;
5476 struct osd_object *obj = it->oi_obj;
5479 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5481 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5482 /* swab quota uid/gid */
5483 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5484 key = (struct dt_key *)&oti->oti_quota_id;
5491 * Return size of key under iterator (in bytes)
5494 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5496 struct osd_it_iam *it = (struct osd_it_iam *)di;
5498 return iam_it_key_size(&it->oi_it);
5502 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5504 /* check if file type is required */
5505 if (ent->lde_attrs & LUDA_TYPE) {
5506 struct luda_type *lt;
5507 int align = sizeof(*lt) - 1;
5509 len = (len + align) & ~align;
5510 lt = (struct luda_type *)(ent->lde_name + len);
5511 lt->lt_type = cpu_to_le16(DTTOIF(type));
5514 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5518 * build lu direct from backend fs dirent.
5522 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5523 char *name, __u16 namelen, __u16 type, __u32 attr)
5525 ent->lde_attrs = attr | LUDA_FID;
5526 fid_cpu_to_le(&ent->lde_fid, fid);
5528 ent->lde_hash = cpu_to_le64(offset);
5529 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5531 strncpy(ent->lde_name, name, namelen);
5532 ent->lde_name[namelen] = '\0';
5533 ent->lde_namelen = cpu_to_le16(namelen);
5535 /* append lustre attributes */
5536 osd_it_append_attrs(ent, namelen, type);
5540 * Return pointer to the record under iterator.
5542 static int osd_it_iam_rec(const struct lu_env *env,
5543 const struct dt_it *di,
5544 struct dt_rec *dtrec, __u32 attr)
5546 struct osd_it_iam *it = (struct osd_it_iam *)di;
5547 struct osd_thread_info *info = osd_oti_get(env);
5550 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5551 const struct osd_fid_pack *rec;
5552 struct lu_fid *fid = &info->oti_fid;
5553 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5559 name = (char *)iam_it_key_get(&it->oi_it);
5561 RETURN(PTR_ERR(name));
5563 namelen = iam_it_key_size(&it->oi_it);
5565 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5567 RETURN(PTR_ERR(rec));
5569 rc = osd_fid_unpack(fid, rec);
5573 hash = iam_it_store(&it->oi_it);
5575 /* IAM does not store object type in IAM index (dir) */
5576 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5578 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5579 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5580 (struct iam_rec *)dtrec);
5581 osd_quota_unpack(it->oi_obj, dtrec);
5583 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5584 (struct iam_rec *)dtrec);
5591 * Returns cookie for current Iterator position.
5593 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5595 struct osd_it_iam *it = (struct osd_it_iam *)di;
5597 return iam_it_store(&it->oi_it);
5601 * Restore iterator from cookie.
5603 * \param di osd iterator
5604 * \param hash Iterator location cookie
5606 * \retval +ve di points to record with least key not larger than key.
5607 * \retval 0 di points to exact matched key
5608 * \retval -ve failure
5611 static int osd_it_iam_load(const struct lu_env *env,
5612 const struct dt_it *di, __u64 hash)
5614 struct osd_it_iam *it = (struct osd_it_iam *)di;
5616 return iam_it_load(&it->oi_it, hash);
5619 static const struct dt_index_operations osd_index_iam_ops = {
5620 .dio_lookup = osd_index_iam_lookup,
5621 .dio_declare_insert = osd_index_declare_iam_insert,
5622 .dio_insert = osd_index_iam_insert,
5623 .dio_declare_delete = osd_index_declare_iam_delete,
5624 .dio_delete = osd_index_iam_delete,
5626 .init = osd_it_iam_init,
5627 .fini = osd_it_iam_fini,
5628 .get = osd_it_iam_get,
5629 .put = osd_it_iam_put,
5630 .next = osd_it_iam_next,
5631 .key = osd_it_iam_key,
5632 .key_size = osd_it_iam_key_size,
5633 .rec = osd_it_iam_rec,
5634 .store = osd_it_iam_store,
5635 .load = osd_it_iam_load
5641 * Creates or initializes iterator context.
5643 * \retval struct osd_it_ea, iterator structure on success
5646 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5647 struct dt_object *dt,
5650 struct osd_object *obj = osd_dt_obj(dt);
5651 struct osd_thread_info *info = osd_oti_get(env);
5652 struct osd_it_ea *oie;
5654 struct lu_object *lo = &dt->do_lu;
5655 struct dentry *obj_dentry;
5658 if (!dt_object_exists(dt) || obj->oo_destroyed)
5659 RETURN(ERR_PTR(-ENOENT));
5661 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5663 RETURN(ERR_PTR(-ENOMEM));
5664 obj_dentry = &oie->oie_dentry;
5666 obj_dentry->d_inode = obj->oo_inode;
5667 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5668 obj_dentry->d_name.hash = 0;
5670 oie->oie_rd_dirent = 0;
5671 oie->oie_it_dirent = 0;
5672 oie->oie_dirent = NULL;
5673 if (unlikely(!info->oti_it_ea_buf_used)) {
5674 oie->oie_buf = info->oti_it_ea_buf;
5675 info->oti_it_ea_buf_used = 1;
5677 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5678 if (oie->oie_buf == NULL)
5679 RETURN(ERR_PTR(-ENOMEM));
5683 file = &oie->oie_file;
5685 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5686 if (attr & LUDA_64BITHASH)
5687 file->f_mode = FMODE_64BITHASH;
5689 file->f_mode = FMODE_32BITHASH;
5690 file->f_path.dentry = obj_dentry;
5691 file->f_mapping = obj->oo_inode->i_mapping;
5692 file->f_op = obj->oo_inode->i_fop;
5693 set_file_inode(file, obj->oo_inode);
5696 RETURN((struct dt_it *) oie);
5700 * Destroy or finishes iterator context.
5702 * \param di iterator structure to be destroyed
5704 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5706 struct osd_thread_info *info = osd_oti_get(env);
5707 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5708 struct osd_object *obj = oie->oie_obj;
5709 struct inode *inode = obj->oo_inode;
5712 oie->oie_file.f_op->release(inode, &oie->oie_file);
5713 lu_object_put(env, &obj->oo_dt.do_lu);
5714 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5715 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5717 info->oti_it_ea_buf_used = 0;
5718 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5723 * It position the iterator at given key, so that next lookup continues from
5724 * that key Or it is similar to dio_it->load() but based on a key,
5725 * rather than file position.
5727 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5730 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5732 static int osd_it_ea_get(const struct lu_env *env,
5733 struct dt_it *di, const struct dt_key *key)
5735 struct osd_it_ea *it = (struct osd_it_ea *)di;
5738 LASSERT(((const char *)key)[0] == '\0');
5739 it->oie_file.f_pos = 0;
5740 it->oie_rd_dirent = 0;
5741 it->oie_it_dirent = 0;
5742 it->oie_dirent = NULL;
5750 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5754 struct osd_filldir_cbs {
5755 #ifdef HAVE_DIR_CONTEXT
5756 struct dir_context ctx;
5758 struct osd_it_ea *it;
5761 * It is called internally by ->readdir(). It fills the
5762 * iterator's in-memory data structure with required
5763 * information i.e. name, namelen, rec_size etc.
5765 * \param buf in which information to be filled in.
5766 * \param name name of the file in given dir
5768 * \retval 0 on success
5769 * \retval 1 on buffer full
5771 #ifdef HAVE_FILLDIR_USE_CTX
5772 static int osd_ldiskfs_filldir(struct dir_context *buf,
5774 static int osd_ldiskfs_filldir(void *buf,
5776 const char *name, int namelen,
5777 loff_t offset, __u64 ino, unsigned d_type)
5779 struct osd_it_ea *it =
5780 ((struct osd_filldir_cbs *)buf)->it;
5781 struct osd_object *obj = it->oie_obj;
5782 struct osd_it_ea_dirent *ent = it->oie_dirent;
5783 struct lu_fid *fid = &ent->oied_fid;
5784 struct osd_fid_pack *rec;
5787 /* this should never happen */
5788 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5789 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5793 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5797 /* "." is just the object itself. */
5798 if (namelen == 1 && name[0] == '.') {
5799 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5800 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5801 rec = (struct osd_fid_pack*) (name + namelen + 1);
5802 if (osd_fid_unpack(fid, rec) != 0)
5807 d_type &= ~LDISKFS_DIRENT_LUFID;
5809 /* NOT export local root. */
5810 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5811 ino = obj->oo_inode->i_ino;
5812 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5815 ent->oied_ino = ino;
5816 ent->oied_off = offset;
5817 ent->oied_namelen = namelen;
5818 ent->oied_type = d_type;
5820 memcpy(ent->oied_name, name, namelen);
5822 it->oie_rd_dirent++;
5823 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5828 * Calls ->readdir() to load a directory entry at a time
5829 * and stored it in iterator's in-memory data structure.
5831 * \param di iterator's in memory structure
5833 * \retval 0 on success
5834 * \retval -ve on error
5835 * \retval +1 reach the end of entry
5837 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5838 const struct dt_it *di)
5840 struct osd_it_ea *it = (struct osd_it_ea *)di;
5841 struct osd_object *obj = it->oie_obj;
5842 struct inode *inode = obj->oo_inode;
5843 struct htree_lock *hlock = NULL;
5844 struct file *filp = &it->oie_file;
5846 struct osd_filldir_cbs buf = {
5847 #ifdef HAVE_DIR_CONTEXT
5848 .ctx.actor = osd_ldiskfs_filldir,
5854 it->oie_dirent = it->oie_buf;
5855 it->oie_rd_dirent = 0;
5857 if (obj->oo_hl_head != NULL) {
5858 hlock = osd_oti_get(env)->oti_hlock;
5859 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5860 inode, LDISKFS_HLOCK_READDIR);
5862 down_read(&obj->oo_ext_idx_sem);
5865 #ifdef HAVE_DIR_CONTEXT
5866 buf.ctx.pos = filp->f_pos;
5867 rc = inode->i_fop->iterate(filp, &buf.ctx);
5868 filp->f_pos = buf.ctx.pos;
5870 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5874 ldiskfs_htree_unlock(hlock);
5876 up_read(&obj->oo_ext_idx_sem);
5878 if (it->oie_rd_dirent == 0) {
5879 /*If it does not get any dirent, it means it has been reached
5880 *to the end of the dir */
5881 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5885 it->oie_dirent = it->oie_buf;
5886 it->oie_it_dirent = 1;
5893 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5894 * to load a directory entry at a time and stored it in
5895 * iterator's in-memory data structure.
5897 * \param di iterator's in memory structure
5899 * \retval +ve iterator reached to end
5900 * \retval 0 iterator not reached to end
5901 * \retval -ve on error
5903 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5905 struct osd_it_ea *it = (struct osd_it_ea *)di;
5910 if (it->oie_it_dirent < it->oie_rd_dirent) {
5912 (void *) it->oie_dirent +
5913 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5914 it->oie_dirent->oied_namelen);
5915 it->oie_it_dirent++;
5918 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5921 rc = osd_ldiskfs_it_fill(env, di);
5928 * Returns the key at current position from iterator's in memory structure.
5930 * \param di iterator's in memory structure
5932 * \retval key i.e. struct dt_key on success
5934 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5935 const struct dt_it *di)
5937 struct osd_it_ea *it = (struct osd_it_ea *)di;
5939 return (struct dt_key *)it->oie_dirent->oied_name;
5943 * Returns the key's size at current position from iterator's in memory structure.
5945 * \param di iterator's in memory structure
5947 * \retval key_size i.e. struct dt_key on success
5949 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5951 struct osd_it_ea *it = (struct osd_it_ea *)di;
5953 return it->oie_dirent->oied_namelen;
5956 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
5958 if (LDISKFS_DIR_REC_LEN(de) >=
5959 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
5966 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5967 unsigned blocksize, bool dotdot)
5970 return osd_dotdot_has_space(de);
5972 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5973 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5980 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
5981 handle_t *jh, struct dentry *dentry,
5982 const struct lu_fid *fid, struct buffer_head *bh,
5983 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
5986 struct inode *dir = dentry->d_parent->d_inode;
5987 struct inode *inode = dentry->d_inode;
5988 struct osd_fid_pack *rec;
5989 struct ldiskfs_dentry_param *ldp;
5990 int namelen = dentry->d_name.len;
5992 struct osd_thread_info *info = osd_oti_get(env);
5995 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5996 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5999 /* There is enough space to hold the FID-in-dirent. */
6000 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6001 rc = ldiskfs_journal_get_write_access(jh, bh);
6005 de->name[namelen] = 0;
6006 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6007 rec->fp_len = sizeof(struct lu_fid) + 1;
6008 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6009 de->file_type |= LDISKFS_DIRENT_LUFID;
6010 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6017 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6021 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6022 osd_get_ldiskfs_dirent_param(ldp, fid);
6023 dentry->d_fsdata = (void *)ldp;
6024 ll_vfs_dq_init(dir);
6025 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6026 /* It is too bad, we cannot reinsert the name entry back.
6027 * That means we lose it! */
6029 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
6030 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6031 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
6032 dir->i_ino, dir->i_generation, namelen,
6033 dentry->d_name.name, PFID(fid), rc);
6039 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6040 struct osd_it_ea *it, struct lu_fid *fid,
6041 struct osd_inode_id *id, __u32 *attr)
6043 struct osd_thread_info *info = osd_oti_get(env);
6044 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
6045 struct osd_device *dev = osd_obj2dev(obj);
6046 struct super_block *sb = osd_sb(dev);
6047 const char *devname = osd_name(dev);
6048 struct osd_it_ea_dirent *ent = it->oie_dirent;
6049 struct inode *dir = obj->oo_inode;
6050 struct htree_lock *hlock = NULL;
6051 struct buffer_head *bh = NULL;
6052 handle_t *jh = NULL;
6053 struct ldiskfs_dir_entry_2 *de;
6054 struct dentry *dentry;
6055 struct inode *inode;
6056 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6059 bool dotdot = false;
6063 if (ent->oied_name[0] == '.') {
6064 if (ent->oied_namelen == 1)
6067 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6071 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6072 inode = osd_iget(info, dev, id);
6073 if (IS_ERR(inode)) {
6074 rc = PTR_ERR(inode);
6075 if (rc == -ENOENT || rc == -ESTALE) {
6076 /* Maybe dangling name entry, or
6077 * corrupted directory entry. */
6078 *attr |= LUDA_UNKNOWN;
6081 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6082 "check_repair, dir = %lu/%u, name = %.*s, "
6083 "ino = %llu, rc = %d\n",
6084 devname, dir->i_ino, dir->i_generation,
6085 ent->oied_namelen, ent->oied_name,
6092 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6094 rc = osd_get_lma(info, inode, dentry, lma);
6095 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6100 /* We need to ensure that the name entry is still valid.
6101 * Because it may be removed or renamed by other already.
6103 * The unlink or rename operation will start journal before PDO lock,
6104 * so to avoid deadlock, here we need to start journal handle before
6105 * related PDO lock also. But because we do not know whether there
6106 * will be something to be repaired before PDO lock, we just start
6107 * journal without conditions.
6109 * We may need to remove the name entry firstly, then insert back.
6110 * One credit is for user quota file update.
6111 * One credit is for group quota file update.
6112 * Two credits are for dirty inode. */
6113 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6114 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6116 if (dev->od_dirent_journal != 0) {
6119 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6122 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6123 "check_repair, dir = %lu/%u, credits = %d, "
6124 "name = %.*s, ino = %llu: rc = %d\n",
6125 devname, dir->i_ino, dir->i_generation, credits,
6126 ent->oied_namelen, ent->oied_name,
6129 GOTO(out_inode, rc);
6132 if (obj->oo_hl_head != NULL) {
6133 hlock = osd_oti_get(env)->oti_hlock;
6134 /* "0" means exclusive lock for the whole directory.
6135 * We need to prevent others access such name entry
6136 * during the delete + insert. Neither HLOCK_ADD nor
6137 * HLOCK_DEL cannot guarantee the atomicity. */
6138 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6140 down_write(&obj->oo_ext_idx_sem);
6143 if (obj->oo_hl_head != NULL) {
6144 hlock = osd_oti_get(env)->oti_hlock;
6145 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6146 LDISKFS_HLOCK_LOOKUP);
6148 down_read(&obj->oo_ext_idx_sem);
6152 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6153 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6154 *attr |= LUDA_IGNORE;
6159 /* For dotdot entry, if there is not enough space to hold the
6160 * FID-in-dirent, just keep them there. It only happens when the
6161 * device upgraded from 1.8 or restored from MDT file-level backup.
6162 * For the whole directory, only dotdot entry have no FID-in-dirent
6163 * and needs to get FID from LMA when readdir, it will not affect the
6164 * performance much. */
6165 if (dotdot && !osd_dotdot_has_space(de)) {
6166 *attr |= LUDA_UNKNOWN;
6172 if (lu_fid_eq(fid, &lma->lma_self_fid))
6175 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6176 struct lu_fid *tfid = &lma->lma_self_fid;
6178 if (likely(dotdot &&
6179 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6180 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6181 /* It must be REMOTE_PARENT_DIR and as the
6182 * 'dotdot' entry of remote directory */
6183 *attr |= LUDA_IGNORE;
6185 CDEBUG(D_LFSCK, "%s: expect remote agent "
6186 "parent directory, but got %.*s under "
6187 "dir = %lu/%u with the FID "DFID"\n",
6188 devname, ent->oied_namelen,
6189 ent->oied_name, dir->i_ino,
6190 dir->i_generation, PFID(tfid));
6192 *attr |= LUDA_UNKNOWN;
6199 if (!fid_is_zero(fid)) {
6200 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6202 if (rc == -ENOENT ||
6204 !(dev->od_scrub.os_file.sf_flags & SF_UPGRADE))) {
6205 /* linkEA does not recognize the dirent entry,
6206 * it may because the dirent entry corruption
6207 * and points to other's inode. */
6208 CDEBUG(D_LFSCK, "%s: the target inode does not "
6209 "recognize the dirent, dir = %lu/%u, "
6210 " name = %.*s, ino = %llu, "
6211 DFID": rc = %d\n", devname, dir->i_ino,
6212 dir->i_generation, ent->oied_namelen,
6213 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6214 *attr |= LUDA_UNKNOWN;
6219 if (rc && rc != -ENODATA) {
6220 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6221 "dir = %lu/%u, name = %.*s, ino = %llu, "
6222 DFID": rc = %d\n", devname, dir->i_ino,
6223 dir->i_generation, ent->oied_namelen,
6224 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6225 *attr |= LUDA_UNKNOWN;
6232 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6233 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6234 if (*attr & LUDA_VERIFY_DRYRUN) {
6235 *fid = lma->lma_self_fid;
6236 *attr |= LUDA_REPAIR;
6243 dev->od_dirent_journal = 1;
6244 if (hlock != NULL) {
6245 ldiskfs_htree_unlock(hlock);
6248 up_read(&obj->oo_ext_idx_sem);
6254 *fid = lma->lma_self_fid;
6256 /* Update or append the FID-in-dirent. */
6257 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6258 bh, de, hlock, dotdot);
6260 *attr |= LUDA_REPAIR;
6262 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6263 "the dirent, dir = %lu/%u, name = %.*s, "
6264 "ino = %llu, "DFID": rc = %d\n",
6265 devname, dir->i_ino, dir->i_generation,
6266 ent->oied_namelen, ent->oied_name,
6267 ent->oied_ino, PFID(fid), rc);
6269 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6270 if (*attr & LUDA_VERIFY_DRYRUN) {
6271 if (fid_is_sane(fid)) {
6272 *attr |= LUDA_REPAIR;
6273 } else if (dev->od_index == 0) {
6274 lu_igif_build(fid, inode->i_ino,
6275 inode->i_generation);
6276 *attr |= LUDA_UPGRADE;
6284 dev->od_dirent_journal = 1;
6285 if (hlock != NULL) {
6286 ldiskfs_htree_unlock(hlock);
6289 up_read(&obj->oo_ext_idx_sem);
6296 if (unlikely(fid_is_sane(fid))) {
6297 /* FID-in-dirent exists, but FID-in-LMA is lost.
6298 * Trust the FID-in-dirent, and add FID-in-LMA. */
6299 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6301 *attr |= LUDA_REPAIR;
6303 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6304 "update dirent, dir = %lu/%u, "
6305 "name = %.*s, ino = %llu, "
6307 devname, dir->i_ino, dir->i_generation,
6308 ent->oied_namelen, ent->oied_name,
6309 ent->oied_ino, PFID(fid), rc);
6310 } else if (dev->od_index == 0) {
6311 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6312 /* It is probably IGIF object. Only aappend the
6313 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6314 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6315 bh, de, hlock, dotdot);
6317 *attr |= LUDA_UPGRADE;
6319 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6320 "after the dirent, dir = %lu/%u, "
6321 "name = %.*s, ino = %llu, "
6323 devname, dir->i_ino, dir->i_generation,
6324 ent->oied_namelen, ent->oied_name,
6325 ent->oied_ino, PFID(fid), rc);
6334 if (hlock != NULL) {
6335 ldiskfs_htree_unlock(hlock);
6337 if (dev->od_dirent_journal != 0)
6338 up_write(&obj->oo_ext_idx_sem);
6340 up_read(&obj->oo_ext_idx_sem);
6344 ldiskfs_journal_stop(jh);
6348 if (rc >= 0 && !dirty)
6349 dev->od_dirent_journal = 0;
6355 * Returns the value at current position from iterator's in memory structure.
6357 * \param di struct osd_it_ea, iterator's in memory structure
6358 * \param attr attr requested for dirent.
6359 * \param lde lustre dirent
6361 * \retval 0 no error and \param lde has correct lustre dirent.
6362 * \retval -ve on error
6364 static inline int osd_it_ea_rec(const struct lu_env *env,
6365 const struct dt_it *di,
6366 struct dt_rec *dtrec, __u32 attr)
6368 struct osd_it_ea *it = (struct osd_it_ea *)di;
6369 struct osd_object *obj = it->oie_obj;
6370 struct osd_device *dev = osd_obj2dev(obj);
6371 struct osd_thread_info *oti = osd_oti_get(env);
6372 struct osd_inode_id *id = &oti->oti_id;
6373 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6374 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6375 __u32 ino = it->oie_dirent->oied_ino;
6379 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6381 if (attr & LUDA_VERIFY) {
6382 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6383 attr |= LUDA_IGNORE;
6384 /* If the parent is on remote MDT, and there
6385 * is no FID-in-dirent, then we have to get
6386 * the parent FID from the linkEA. */
6387 if (!fid_is_sane(fid) &&
6388 it->oie_dirent->oied_namelen == 2 &&
6389 it->oie_dirent->oied_name[0] == '.' &&
6390 it->oie_dirent->oied_name[1] == '.')
6391 osd_get_pfid_from_linkea(env, obj, fid);
6393 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6397 if (!fid_is_sane(fid))
6398 attr |= LUDA_UNKNOWN;
6400 attr &= ~LU_DIRENT_ATTRS_MASK;
6401 if (!fid_is_sane(fid)) {
6402 bool is_dotdot = false;
6403 if (it->oie_dirent->oied_namelen == 2 &&
6404 it->oie_dirent->oied_name[0] == '.' &&
6405 it->oie_dirent->oied_name[1] == '.')
6407 /* If the parent is on remote MDT, and there
6408 * is no FID-in-dirent, then we have to get
6409 * the parent FID from the linkEA. */
6410 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6411 rc = osd_get_pfid_from_linkea(env, obj, fid);
6413 if (is_dotdot == false &&
6414 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6417 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6420 osd_id_gen(id, ino, OSD_OII_NOGEN);
6424 /* Pack the entry anyway, at least the offset is right. */
6425 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6426 it->oie_dirent->oied_name,
6427 it->oie_dirent->oied_namelen,
6428 it->oie_dirent->oied_type, attr);
6433 if (osd_remote_fid(env, dev, fid))
6436 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6437 osd_add_oi_cache(oti, dev, id, fid);
6439 RETURN(rc > 0 ? 0 : rc);
6443 * Returns the record size size at current position.
6445 * This function will return record(lu_dirent) size in bytes.
6447 * \param[in] env execution environment
6448 * \param[in] di iterator's in memory structure
6449 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6450 * calculate the lu_dirent size.
6452 * \retval record size(in bytes & in memory) of the current lu_dirent
6455 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6458 struct osd_it_ea *it = (struct osd_it_ea *)di;
6460 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6464 * Returns a cookie for current position of the iterator head, so that
6465 * user can use this cookie to load/start the iterator next time.
6467 * \param di iterator's in memory structure
6469 * \retval cookie for current position, on success
6471 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6473 struct osd_it_ea *it = (struct osd_it_ea *)di;
6475 return it->oie_dirent->oied_off;
6479 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6480 * to load a directory entry at a time and stored it i inn,
6481 * in iterator's in-memory data structure.
6483 * \param di struct osd_it_ea, iterator's in memory structure
6485 * \retval +ve on success
6486 * \retval -ve on error
6488 static int osd_it_ea_load(const struct lu_env *env,
6489 const struct dt_it *di, __u64 hash)
6491 struct osd_it_ea *it = (struct osd_it_ea *)di;
6495 it->oie_file.f_pos = hash;
6497 rc = osd_ldiskfs_it_fill(env, di);
6508 * Index lookup function for interoperability mode (b11826).
6510 * \param key, key i.e. file name to be searched
6512 * \retval +ve, on success
6513 * \retval -ve, on error
6515 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6516 struct dt_rec *rec, const struct dt_key *key)
6518 struct osd_object *obj = osd_dt_obj(dt);
6523 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6524 LINVRNT(osd_invariant(obj));
6526 rc = osd_ea_lookup_rec(env, obj, rec, key);
6533 * Index and Iterator operations for interoperability
6534 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6536 static const struct dt_index_operations osd_index_ea_ops = {
6537 .dio_lookup = osd_index_ea_lookup,
6538 .dio_declare_insert = osd_index_declare_ea_insert,
6539 .dio_insert = osd_index_ea_insert,
6540 .dio_declare_delete = osd_index_declare_ea_delete,
6541 .dio_delete = osd_index_ea_delete,
6543 .init = osd_it_ea_init,
6544 .fini = osd_it_ea_fini,
6545 .get = osd_it_ea_get,
6546 .put = osd_it_ea_put,
6547 .next = osd_it_ea_next,
6548 .key = osd_it_ea_key,
6549 .key_size = osd_it_ea_key_size,
6550 .rec = osd_it_ea_rec,
6551 .rec_size = osd_it_ea_rec_size,
6552 .store = osd_it_ea_store,
6553 .load = osd_it_ea_load
6557 static void *osd_key_init(const struct lu_context *ctx,
6558 struct lu_context_key *key)
6560 struct osd_thread_info *info;
6562 OBD_ALLOC_PTR(info);
6564 return ERR_PTR(-ENOMEM);
6566 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6567 if (info->oti_it_ea_buf == NULL)
6570 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6572 info->oti_hlock = ldiskfs_htree_lock_alloc();
6573 if (info->oti_hlock == NULL)
6579 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6582 return ERR_PTR(-ENOMEM);
6585 static void osd_key_fini(const struct lu_context *ctx,
6586 struct lu_context_key *key, void* data)
6588 struct osd_thread_info *info = data;
6589 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6590 struct osd_idmap_cache *idc = info->oti_ins_cache;
6592 if (info->oti_inode != NULL)
6594 if (info->oti_hlock != NULL)
6595 ldiskfs_htree_lock_free(info->oti_hlock);
6596 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6597 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6598 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6599 lu_buf_free(&info->oti_big_buf);
6601 LASSERT(info->oti_ins_cache_size > 0);
6602 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6603 info->oti_ins_cache = NULL;
6604 info->oti_ins_cache_size = 0;
6609 static void osd_key_exit(const struct lu_context *ctx,
6610 struct lu_context_key *key, void *data)
6612 struct osd_thread_info *info = data;
6614 LASSERT(info->oti_r_locks == 0);
6615 LASSERT(info->oti_w_locks == 0);
6616 LASSERT(info->oti_txns == 0);
6619 /* type constructor/destructor: osd_type_init, osd_type_fini */
6620 LU_TYPE_INIT_FINI(osd, &osd_key);
6622 struct lu_context_key osd_key = {
6623 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6624 .lct_init = osd_key_init,
6625 .lct_fini = osd_key_fini,
6626 .lct_exit = osd_key_exit
6630 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6631 const char *name, struct lu_device *next)
6633 struct osd_device *osd = osd_dev(d);
6635 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6636 >= sizeof(osd->od_svname))
6638 return osd_procfs_init(osd, name);
6641 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6643 struct seq_server_site *ss = osd_seq_site(osd);
6647 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6650 if (unlikely(ss == NULL))
6653 OBD_ALLOC_PTR(osd->od_cl_seq);
6654 if (osd->od_cl_seq == NULL)
6657 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6658 osd->od_svname, ss->ss_server_seq);
6660 OBD_FREE_PTR(osd->od_cl_seq);
6661 osd->od_cl_seq = NULL;
6665 if (ss->ss_node_id == 0) {
6666 /* If the OSD on the sequence controller(MDT0), then allocate
6667 * sequence here, otherwise allocate sequence after connected
6668 * to MDT0 (see mdt_register_lwp_callback()). */
6669 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6670 &osd->od_cl_seq->lcs_space, env);
6676 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6678 if (osd->od_cl_seq == NULL)
6681 seq_client_fini(osd->od_cl_seq);
6682 OBD_FREE_PTR(osd->od_cl_seq);
6683 osd->od_cl_seq = NULL;
6686 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6690 /* shutdown quota slave instance associated with the device */
6691 if (o->od_quota_slave != NULL) {
6692 qsd_fini(env, o->od_quota_slave);
6693 o->od_quota_slave = NULL;
6696 osd_fid_fini(env, o);
6701 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6705 if (o->od_mnt != NULL) {
6706 shrink_dcache_sb(osd_sb(o));
6707 osd_sync(env, &o->od_dt_dev);
6716 static int osd_mount(const struct lu_env *env,
6717 struct osd_device *o, struct lustre_cfg *cfg)
6719 const char *name = lustre_cfg_string(cfg, 0);
6720 const char *dev = lustre_cfg_string(cfg, 1);
6722 unsigned long page, s_flags, lmd_flags = 0;
6723 struct page *__page;
6724 struct file_system_type *type;
6725 char *options = NULL;
6727 struct osd_thread_info *info = osd_oti_get(env);
6728 struct lu_fid *fid = &info->oti_fid;
6729 struct inode *inode;
6730 int rc = 0, force_over_256tb = 0;
6733 if (o->od_mnt != NULL)
6736 if (strlen(dev) >= sizeof(o->od_mntdev))
6738 strcpy(o->od_mntdev, dev);
6740 str = lustre_cfg_string(cfg, 2);
6741 s_flags = simple_strtoul(str, NULL, 0);
6742 str = strstr(str, ":");
6744 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6745 opts = lustre_cfg_string(cfg, 3);
6747 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6748 CERROR("%s: device %s extents feature is not guaranteed to "
6749 "work on big-endian systems. Use \"bigendian_extents\" "
6750 "mount option to override.\n", name, dev);
6754 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6755 CWARN("force_over_128tb option is depricated."
6756 "Filesystems less then 256TB can be created without any"
6757 "force options. Use force_over_256tb option for"
6758 "filesystems greather then 256TB.\n");
6761 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL)
6762 force_over_256tb = 1;
6764 __page = alloc_page(GFP_KERNEL);
6766 GOTO(out, rc = -ENOMEM);
6767 page = (unsigned long)page_address(__page);
6768 options = (char *)page;
6771 /* strip out the options for back compatiblity */
6772 static char *sout[] = {
6779 /* strip out option we processed in osd */
6780 "bigendian_extents",
6781 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(3,0,53,0)
6782 #warning "remove force_over_128 option"
6784 "force_over_128tb (deprecated)",
6789 strcat(options, opts);
6790 for (rc = 0, str = options; sout[rc]; ) {
6791 char *op = strstr(str, sout[rc]);
6797 if (op == options || *(op - 1) == ',') {
6798 str = op + strlen(sout[rc]);
6799 if (*str == ',' || *str == '\0') {
6800 *str == ',' ? str++ : str;
6801 memmove(op, str, strlen(str) + 1);
6804 for (str = op; *str != ',' && *str != '\0'; str++)
6808 strncat(options, "user_xattr,acl", 14);
6811 /* Glom up mount options */
6812 if (*options != '\0')
6813 strcat(options, ",");
6814 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6816 type = get_fs_type("ldiskfs");
6818 CERROR("%s: cannot find ldiskfs module\n", name);
6819 GOTO(out, rc = -ENODEV);
6822 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6823 module_put(type->owner);
6825 if (IS_ERR(o->od_mnt)) {
6826 rc = PTR_ERR(o->od_mnt);
6828 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6832 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (64ULL << 30) &&
6833 force_over_256tb == 0) {
6834 CERROR("%s: device %s LDISKFS does not support filesystems "
6835 "greater than 256TB and can cause data corruption. "
6836 "Use \"force_over_256tb\" mount option to override.\n",
6838 GOTO(out, rc = -EINVAL);
6841 #ifdef HAVE_DEV_SET_RDONLY
6842 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6843 CERROR("%s: underlying device %s is marked as read-only. "
6844 "Setup failed\n", name, dev);
6845 GOTO(out_mnt, rc = -EROFS);
6849 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6850 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6851 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6852 GOTO(out_mnt, rc = -EINVAL);
6855 #ifdef LDISKFS_MOUNT_DIRDATA
6856 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6857 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6858 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6859 else if (!o->od_is_ost)
6860 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6861 "enabling the dirdata feature. If you do not want to "
6862 "downgrade to Lustre-1.x again, you can enable it via "
6863 "'tune2fs -O dirdata device'\n", name, dev);
6865 inode = osd_sb(o)->s_root->d_inode;
6866 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6867 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6869 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6873 if (lmd_flags & LMD_FLG_NOSCRUB)
6884 __free_page(__page);
6889 static struct lu_device *osd_device_fini(const struct lu_env *env,
6890 struct lu_device *d)
6892 struct osd_device *o = osd_dev(d);
6895 osd_shutdown(env, o);
6897 osd_scrub_cleanup(env, o);
6898 osd_obj_map_fini(o);
6904 static int osd_device_init0(const struct lu_env *env,
6905 struct osd_device *o,
6906 struct lustre_cfg *cfg)
6908 struct lu_device *l = osd2lu_dev(o);
6909 struct osd_thread_info *info;
6913 /* if the module was re-loaded, env can loose its keys */
6914 rc = lu_env_refill((struct lu_env *) env);
6917 info = osd_oti_get(env);
6920 l->ld_ops = &osd_lu_ops;
6921 o->od_dt_dev.dd_ops = &osd_dt_ops;
6923 spin_lock_init(&o->od_osfs_lock);
6924 mutex_init(&o->od_otable_mutex);
6925 INIT_LIST_HEAD(&o->od_orphan_list);
6927 o->od_read_cache = 1;
6928 o->od_writethrough_cache = 1;
6929 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6931 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6932 sizeof(o->od_svname));
6933 if (cplen >= sizeof(o->od_svname)) {
6938 o->od_index = -1; /* -1 means index is invalid */
6939 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6940 if (rc == LDD_F_SV_TYPE_OST)
6943 o->od_full_scrub_ratio = OFSR_DEFAULT;
6944 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6945 rc = osd_mount(env, o, cfg);
6949 rc = osd_obj_map_init(env, o);
6953 rc = lu_site_init(&o->od_site, l);
6955 GOTO(out_compat, rc);
6956 o->od_site.ls_bottom_dev = l;
6958 rc = lu_site_init_finish(&o->od_site);
6962 INIT_LIST_HEAD(&o->od_ios_list);
6963 /* setup scrub, including OI files initialization */
6964 rc = osd_scrub_setup(env, o);
6968 rc = osd_procfs_init(o, o->od_svname);
6970 CERROR("%s: can't initialize procfs: rc = %d\n",
6972 GOTO(out_scrub, rc);
6975 LASSERT(l->ld_site->ls_linkage.next != NULL);
6976 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6978 /* initialize quota slave instance */
6979 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6981 if (IS_ERR(o->od_quota_slave)) {
6982 rc = PTR_ERR(o->od_quota_slave);
6983 o->od_quota_slave = NULL;
6984 GOTO(out_procfs, rc);
6992 osd_scrub_cleanup(env, o);
6994 lu_site_fini(&o->od_site);
6996 osd_obj_map_fini(o);
7003 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7004 struct lu_device_type *t,
7005 struct lustre_cfg *cfg)
7007 struct osd_device *o;
7012 return ERR_PTR(-ENOMEM);
7014 rc = dt_device_init(&o->od_dt_dev, t);
7016 /* Because the ctx might be revived in dt_device_init,
7017 * refill the env here */
7018 lu_env_refill((struct lu_env *)env);
7019 rc = osd_device_init0(env, o, cfg);
7021 dt_device_fini(&o->od_dt_dev);
7024 if (unlikely(rc != 0))
7027 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7030 static struct lu_device *osd_device_free(const struct lu_env *env,
7031 struct lu_device *d)
7033 struct osd_device *o = osd_dev(d);
7036 /* XXX: make osd top device in order to release reference */
7037 d->ld_site->ls_top_dev = d;
7038 lu_site_purge(env, d->ld_site, -1);
7039 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7040 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7041 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7043 lu_site_fini(&o->od_site);
7044 dt_device_fini(&o->od_dt_dev);
7049 static int osd_process_config(const struct lu_env *env,
7050 struct lu_device *d, struct lustre_cfg *cfg)
7052 struct osd_device *o = osd_dev(d);
7056 switch (cfg->lcfg_command) {
7058 rc = osd_mount(env, o, cfg);
7061 lu_dev_del_linkage(d->ld_site, d);
7062 rc = osd_shutdown(env, o);
7065 LASSERT(&o->od_dt_dev);
7066 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7067 cfg, &o->od_dt_dev);
7068 if (rc > 0 || rc == -ENOSYS)
7069 rc = class_process_proc_param(PARAM_OST,
7070 lprocfs_osd_obd_vars,
7071 cfg, &o->od_dt_dev);
7080 static int osd_recovery_complete(const struct lu_env *env,
7081 struct lu_device *d)
7083 struct osd_device *osd = osd_dev(d);
7087 if (osd->od_quota_slave == NULL)
7090 /* start qsd instance on recovery completion, this notifies the quota
7091 * slave code that we are about to process new requests now */
7092 rc = qsd_start(env, osd->od_quota_slave);
7097 * we use exports to track all osd users
7099 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7100 struct obd_device *obd, struct obd_uuid *cluuid,
7101 struct obd_connect_data *data, void *localdata)
7103 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7104 struct lustre_handle conn;
7108 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7110 rc = class_connect(&conn, obd, cluuid);
7114 *exp = class_conn2export(&conn);
7116 spin_lock(&osd->od_osfs_lock);
7118 spin_unlock(&osd->od_osfs_lock);
7124 * once last export (we don't count self-export) disappeared
7125 * osd can be released
7127 static int osd_obd_disconnect(struct obd_export *exp)
7129 struct obd_device *obd = exp->exp_obd;
7130 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7131 int rc, release = 0;
7134 /* Only disconnect the underlying layers on the final disconnect. */
7135 spin_lock(&osd->od_osfs_lock);
7137 if (osd->od_connects == 0)
7139 spin_unlock(&osd->od_osfs_lock);
7141 rc = class_disconnect(exp); /* bz 9811 */
7143 if (rc == 0 && release)
7144 class_manual_cleanup(obd);
7148 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7149 struct lu_device *dev)
7151 struct osd_device *osd = osd_dev(dev);
7152 struct lr_server_data *lsd =
7153 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7157 if (osd->od_quota_slave != NULL) {
7158 /* set up quota slave objects */
7159 result = qsd_prepare(env, osd->od_quota_slave);
7164 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7165 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7166 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7167 osd->od_index_in_idif = 1;
7169 osd->od_index_in_idif = 0;
7170 result = osd_register_proc_index_in_idif(osd);
7175 osd->od_index_in_idif = 1;
7179 result = osd_fid_init(env, osd);
7184 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7185 struct lu_fid *fid, struct md_op_data *op_data)
7187 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7189 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7192 static const struct lu_object_operations osd_lu_obj_ops = {
7193 .loo_object_init = osd_object_init,
7194 .loo_object_delete = osd_object_delete,
7195 .loo_object_release = osd_object_release,
7196 .loo_object_free = osd_object_free,
7197 .loo_object_print = osd_object_print,
7198 .loo_object_invariant = osd_object_invariant
7201 const struct lu_device_operations osd_lu_ops = {
7202 .ldo_object_alloc = osd_object_alloc,
7203 .ldo_process_config = osd_process_config,
7204 .ldo_recovery_complete = osd_recovery_complete,
7205 .ldo_prepare = osd_prepare,
7208 static const struct lu_device_type_operations osd_device_type_ops = {
7209 .ldto_init = osd_type_init,
7210 .ldto_fini = osd_type_fini,
7212 .ldto_start = osd_type_start,
7213 .ldto_stop = osd_type_stop,
7215 .ldto_device_alloc = osd_device_alloc,
7216 .ldto_device_free = osd_device_free,
7218 .ldto_device_init = osd_device_init,
7219 .ldto_device_fini = osd_device_fini
7222 static struct lu_device_type osd_device_type = {
7223 .ldt_tags = LU_DEVICE_DT,
7224 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7225 .ldt_ops = &osd_device_type_ops,
7226 .ldt_ctx_tags = LCT_LOCAL,
7229 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7231 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7232 struct super_block *sb = osd_sb(osd);
7234 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7238 * lprocfs legacy support.
7240 static struct obd_ops osd_obd_device_ops = {
7241 .o_owner = THIS_MODULE,
7242 .o_connect = osd_obd_connect,
7243 .o_disconnect = osd_obd_disconnect,
7244 .o_fid_alloc = osd_fid_alloc,
7245 .o_health_check = osd_health_check,
7248 static int __init osd_init(void)
7252 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7253 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7254 /* please, try to keep osd_thread_info smaller than a page */
7255 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7260 rc = lu_kmem_init(ldiskfs_caches);
7264 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7265 lprocfs_osd_module_vars,
7266 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7268 lu_kmem_fini(ldiskfs_caches);
7272 static void __exit osd_exit(void)
7274 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7275 lu_kmem_fini(ldiskfs_caches);
7278 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7279 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7280 MODULE_VERSION(LUSTRE_VERSION_STRING);
7281 MODULE_LICENSE("GPL");
7283 module_init(osd_init);
7284 module_exit(osd_exit);