4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_OSD
46 #include <linux/module.h>
47 #include <linux/user_namespace.h>
48 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
52 /* LUSTRE_VERSION_CODE */
53 #include <lustre_ver.h>
54 /* prerequisite for linux/xattr.h */
55 #include <linux/types.h>
56 /* prerequisite for linux/xattr.h */
58 /* XATTR_{REPLACE,CREATE} */
59 #include <linux/xattr.h>
61 #include <ldiskfs/ldiskfs.h>
62 #include <ldiskfs/xattr.h>
63 #include <ldiskfs/ldiskfs_extents.h>
66 * struct OBD_{ALLOC,FREE}*()
69 #include <obd_support.h>
70 /* struct ptlrpc_thread */
71 #include <lustre_net.h>
72 #include <lustre_fid.h>
74 #include <lustre_param.h>
76 #include "osd_internal.h"
77 #include "osd_dynlocks.h"
79 /* llo_* api support */
80 #include <md_object.h>
81 #include <lustre_quota.h>
83 #include <lustre_linkea.h>
86 module_param(ldiskfs_pdo, int, 0644);
87 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
89 int ldiskfs_track_declares_assert;
90 module_param(ldiskfs_track_declares_assert, int, 0644);
91 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
93 /* Slab to allocate dynlocks */
94 struct kmem_cache *dynlock_cachep;
96 /* Slab to allocate osd_it_ea */
97 struct kmem_cache *osd_itea_cachep;
99 static struct lu_kmem_descr ldiskfs_caches[] = {
101 .ckd_cache = &dynlock_cachep,
102 .ckd_name = "dynlock_cache",
103 .ckd_size = sizeof(struct dynlock_handle)
106 .ckd_cache = &osd_itea_cachep,
107 .ckd_name = "osd_itea_cache",
108 .ckd_size = sizeof(struct osd_it_ea)
115 static const char dot[] = ".";
116 static const char dotdot[] = "..";
117 static const char remote_obj_dir[] = "REM_OBJ_DIR";
119 static const struct lu_object_operations osd_lu_obj_ops;
120 static const struct dt_object_operations osd_obj_ops;
121 static const struct dt_object_operations osd_obj_ea_ops;
122 static const struct dt_object_operations osd_obj_otable_it_ops;
123 static const struct dt_index_operations osd_index_iam_ops;
124 static const struct dt_index_operations osd_index_ea_ops;
126 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
127 const struct lu_fid *fid);
128 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
129 struct osd_device *osd);
131 int osd_trans_declare_op2rb[] = {
132 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
133 [OSD_OT_PUNCH] = OSD_OT_MAX,
134 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
135 [OSD_OT_CREATE] = OSD_OT_DESTROY,
136 [OSD_OT_DESTROY] = OSD_OT_CREATE,
137 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
138 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
139 [OSD_OT_WRITE] = OSD_OT_WRITE,
140 [OSD_OT_INSERT] = OSD_OT_DELETE,
141 [OSD_OT_DELETE] = OSD_OT_INSERT,
142 [OSD_OT_QUOTA] = OSD_OT_MAX,
145 static int osd_has_index(const struct osd_object *obj)
147 return obj->oo_dt.do_index_ops != NULL;
150 static int osd_object_invariant(const struct lu_object *l)
152 return osd_invariant(osd_obj(l));
156 * Concurrency: doesn't matter
160 * Concurrency: doesn't matter
162 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
164 struct osd_thread_info *oti = osd_oti_get(env);
165 return oti->oti_w_locks > 0 && o->oo_owner == env;
169 * Concurrency: doesn't access mutable data
171 static int osd_root_get(const struct lu_env *env,
172 struct dt_device *dev, struct lu_fid *f)
174 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
179 * the following set of functions are used to maintain per-thread
180 * cache of FID->ino mapping. this mechanism is needed to resolve
181 * FID to inode at dt_insert() which in turn stores ino in the
182 * directory entries to keep ldiskfs compatible with ext[34].
183 * due to locking-originated restrictions we can't lookup ino
184 * using LU cache (deadlock is possible). lookup using OI is quite
185 * expensive. so instead we maintain this cache and methods like
186 * dt_create() fill it. so in the majority of cases dt_insert() is
187 * able to find needed mapping in lockless manner.
189 static struct osd_idmap_cache *
190 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
191 const struct lu_fid *fid)
193 struct osd_thread_info *oti = osd_oti_get(env);
194 struct osd_idmap_cache *idc = oti->oti_ins_cache;
196 for (i = 0; i < oti->oti_ins_cache_used; i++) {
197 if (!lu_fid_eq(&idc[i].oic_fid, fid))
199 if (idc[i].oic_dev != osd)
208 static struct osd_idmap_cache *
209 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
210 const struct lu_fid *fid)
212 struct osd_thread_info *oti = osd_oti_get(env);
213 struct osd_idmap_cache *idc;
216 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
217 i = oti->oti_ins_cache_size * 2;
219 i = OSD_INS_CACHE_SIZE;
220 OBD_ALLOC(idc, sizeof(*idc) * i);
222 return ERR_PTR(-ENOMEM);
223 if (oti->oti_ins_cache != NULL) {
224 memcpy(idc, oti->oti_ins_cache,
225 oti->oti_ins_cache_used * sizeof(*idc));
226 OBD_FREE(oti->oti_ins_cache,
227 oti->oti_ins_cache_used * sizeof(*idc));
229 oti->oti_ins_cache = idc;
230 oti->oti_ins_cache_size = i;
233 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
236 idc->oic_lid.oii_ino = 0;
237 idc->oic_lid.oii_gen = 0;
244 * lookup mapping for the given fid in the cache, initialize a
245 * new one if not found. the initialization checks whether the
246 * object is local or remote. for local objects, OI is used to
247 * learn ino/generation. the function is used when the caller
248 * has no information about the object, e.g. at dt_insert().
250 static struct osd_idmap_cache *
251 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
252 const struct lu_fid *fid)
254 struct osd_idmap_cache *idc;
257 idc = osd_idc_find(env, osd, fid);
258 LASSERT(!IS_ERR(idc));
262 /* new mapping is needed */
263 idc = osd_idc_add(env, osd, fid);
268 rc = osd_remote_fid(env, osd, fid);
269 if (unlikely(rc < 0))
273 /* the object is local, lookup in OI */
274 /* XXX: probably cheaper to lookup in LU first? */
275 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
277 if (unlikely(rc < 0)) {
278 CERROR("can't lookup: rc = %d\n", rc);
282 /* the object is remote */
290 * lookup mapping for given FID and fill it from the given object.
291 * the object is lolcal by definition.
293 static int osd_idc_find_and_init(const struct lu_env *env,
294 struct osd_device *osd,
295 struct osd_object *obj)
297 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
298 struct osd_idmap_cache *idc;
300 idc = osd_idc_find(env, osd, fid);
301 LASSERT(!IS_ERR(idc));
303 if (obj->oo_inode == NULL)
305 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
306 LASSERT(idc->oic_lid.oii_ino == 0);
307 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
308 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
313 /* new mapping is needed */
314 idc = osd_idc_add(env, osd, fid);
318 if (obj->oo_inode != NULL) {
319 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
320 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
326 * OSD object methods.
330 * Concurrency: no concurrent access is possible that early in object
333 static struct lu_object *osd_object_alloc(const struct lu_env *env,
334 const struct lu_object_header *hdr,
337 struct osd_object *mo;
343 l = &mo->oo_dt.do_lu;
344 dt_object_init(&mo->oo_dt, NULL, d);
345 mo->oo_dt.do_ops = &osd_obj_ea_ops;
346 l->lo_ops = &osd_lu_obj_ops;
347 init_rwsem(&mo->oo_sem);
348 init_rwsem(&mo->oo_ext_idx_sem);
349 spin_lock_init(&mo->oo_guard);
356 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
357 struct dentry *dentry, struct lustre_mdt_attrs *lma)
361 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
362 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
363 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
365 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
366 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
368 lustre_lma_swab(lma);
369 /* Check LMA compatibility */
370 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
371 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
372 "for fid = "DFID", ino = %lu\n",
373 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
374 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
375 PFID(&lma->lma_self_fid), inode->i_ino);
378 } else if (rc == 0) {
386 * retrieve object from backend ext fs.
388 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
389 struct osd_inode_id *id)
391 struct inode *inode = NULL;
393 /* if we look for an inode withing a running
394 * transaction, then we risk to deadlock */
395 /* osd_dirent_check_repair() breaks this */
396 /*LASSERT(current->journal_info == NULL);*/
398 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
400 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
401 id->oii_ino, PTR_ERR(inode));
402 } else if (id->oii_gen != OSD_OII_NOGEN &&
403 inode->i_generation != id->oii_gen) {
404 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
405 "i_generation = %u\n",
406 id->oii_ino, id->oii_gen, inode->i_generation);
408 inode = ERR_PTR(-ESTALE);
409 } else if (inode->i_nlink == 0) {
410 /* due to parallel readdir and unlink,
411 * we can have dead inode here. */
412 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
414 inode = ERR_PTR(-ESTALE);
415 } else if (is_bad_inode(inode)) {
416 CWARN("%.16s: bad inode: ino = %u\n",
417 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
419 inode = ERR_PTR(-ENOENT);
421 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
422 if (id->oii_gen == OSD_OII_NOGEN)
423 osd_id_gen(id, inode->i_ino, inode->i_generation);
425 /* Do not update file c/mtime in ldiskfs.
426 * NB: we don't have any lock to protect this because we don't
427 * have reference on osd_object now, but contention with
428 * another lookup + attr_set can't happen in the tiny window
429 * between if (...) and set S_NOCMTIME. */
430 if (!(inode->i_flags & S_NOCMTIME))
431 inode->i_flags |= S_NOCMTIME;
436 int osd_ldiskfs_add_entry(struct osd_thread_info *info,
437 handle_t *handle, struct dentry *child,
438 struct inode *inode, struct htree_lock *hlock)
442 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
443 if (rc == -ENOBUFS || rc == -ENOSPC) {
444 char fidbuf[FID_LEN + 1];
445 struct lustre_mdt_attrs lma;
446 struct lu_fid fid = { };
448 struct dentry *p_dentry = child->d_parent;
450 rc2 = osd_get_lma(info, p_dentry->d_inode, p_dentry,
453 fid = lma.lma_self_fid;
454 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
455 } else if (rc2 == -ENODATA) {
456 if (unlikely(p_dentry->d_inode ==
457 inode->i_sb->s_root->d_inode))
458 lu_local_obj_fid(&fid, OSD_FS_ROOT_OID);
459 else if (info->oti_dev && !info->oti_dev->od_is_ost &&
460 fid_seq_is_mdt0(fid_seq(&fid)))
461 lu_igif_build(&fid, p_dentry->d_inode->i_ino,
462 p_dentry->d_inode->i_generation);
463 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
465 snprintf(fidbuf, FID_LEN, "%s", "unknown");
469 errstr = "has reached";
471 errstr = "is approaching";
472 CWARN("%.16s: directory (inode: %lu FID: %s) %s maximum entry limit\n",
473 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
474 p_dentry->d_inode->i_ino, fidbuf, errstr);
475 /* ignore such error now */
483 static struct inode *
484 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
485 struct osd_inode_id *id, struct lu_fid *fid)
487 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
491 inode = osd_iget(info, dev, id);
495 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
497 *fid = lma->lma_self_fid;
498 } else if (rc == -ENODATA) {
499 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
500 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
502 lu_igif_build(fid, inode->i_ino, inode->i_generation);
510 static struct inode *osd_iget_check(struct osd_thread_info *info,
511 struct osd_device *dev,
512 const struct lu_fid *fid,
513 struct osd_inode_id *id,
520 /* The cached OI mapping is trustable. If we cannot locate the inode
521 * via the cached OI mapping, then return the failure to the caller
522 * directly without further OI checking. */
524 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
527 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
528 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
537 if (is_bad_inode(inode)) {
540 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
548 if (id->oii_gen != OSD_OII_NOGEN &&
549 inode->i_generation != id->oii_gen) {
552 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
553 "oii_gen = %u, i_generation = %u\n",
554 id->oii_ino, id->oii_gen, inode->i_generation);
562 if (inode->i_nlink == 0) {
565 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
573 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
577 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
579 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
580 /* XXX: There are four possible cases:
582 * Backup/restore caused the OI invalid.
584 * Someone unlinked the object but NOT removed
585 * the OI mapping, such as mount target device
586 * as ldiskfs, and modify something directly.
588 * Someone just removed the object between the
589 * former oi_lookup and the iget. It is normal.
590 * 4. Other failure cases.
592 * Generally, when the device is mounted, it will
593 * auto check whether the system is restored from
594 * file-level backup or not. We trust such detect
595 * to distinguish the 1st case from the 2nd case. */
597 if (!IS_ERR(inode) && inode->i_generation != 0 &&
598 inode->i_generation == id->oii_gen)
599 /* "id->oii_gen != OSD_OII_NOGEN" is for
600 * "@cached == false" case. */
605 /* If the OI mapping was in OI file before the
606 * osd_iget_check(), but now, it is disappear,
607 * then it must be removed by race. That is a
608 * normal race case. */
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 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
770 const struct lu_fid *fid,
771 const struct lu_object_conf *conf)
773 struct osd_thread_info *info;
774 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
775 struct osd_device *dev;
776 struct osd_idmap_cache *oic;
777 struct osd_inode_id *id;
779 struct osd_scrub *scrub;
780 struct scrub_file *sf;
784 bool triggered = false;
787 LINVRNT(osd_invariant(obj));
788 LASSERT(obj->oo_inode == NULL);
789 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
792 scrub = &dev->od_scrub;
793 sf = &scrub->os_file;
794 info = osd_oti_get(env);
796 oic = &info->oti_cache;
798 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
801 /* For the object is created as locking anchor, or for the object to
802 * be created on disk. No need to osd_oi_lookup() at here because FID
803 * shouldn't never be re-used, if it's really a duplicate FID from
804 * unexpected reason, we should be able to detect it later by calling
805 * do_create->osd_oi_insert(). */
806 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
807 GOTO(out, result = 0);
809 /* Search order: 1. per-thread cache. */
810 if (lu_fid_eq(fid, &oic->oic_fid) &&
811 likely(oic->oic_dev == dev)) {
817 if (!list_empty(&scrub->os_inconsistent_items)) {
818 /* Search order: 2. OI scrub pending list. */
819 result = osd_oii_lookup(dev, fid, id);
825 /* Search order: 3. OI files. */
826 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
827 if (result == -ENOENT) {
828 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
829 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
830 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
832 GOTO(out, result = 0);
841 inode = osd_iget_check(info, dev, fid, id, cached);
843 result = PTR_ERR(inode);
844 if (result == -ENOENT || result == -ESTALE)
845 GOTO(out, result = -ENOENT);
847 if (result == -EREMCHG) {
850 if (unlikely(triggered))
851 GOTO(out, result = saved);
854 if (thread_is_running(&scrub->os_thread)) {
855 result = -EINPROGRESS;
856 } else if (!dev->od_noscrub) {
857 result = osd_scrub_start(dev, SS_AUTO_FULL |
858 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
859 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
860 "for "DFID", rc = %d [1]\n",
861 osd_name(dev), PFID(fid), result);
862 if (result == 0 || result == -EALREADY)
863 result = -EINPROGRESS;
870 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD))
873 /* We still have chance to get the valid inode: for the
874 * object which is referenced by remote name entry, the
875 * object on the local MDT will be linked under the dir
876 * of "/REMOTE_PARENT_DIR" with its FID string as name.
878 * We do not know whether the object for the given FID
879 * is referenced by some remote name entry or not, and
880 * especially for DNE II, a multiple-linked object may
881 * have many name entries reside on many MDTs.
883 * To simplify the operation, OSD will not distinguish
884 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
885 * only happened for the RPC from other MDT during the
886 * OI scrub, or for the client side RPC with FID only,
887 * such as FID to path, or from old connected client. */
889 result = osd_lookup_in_remote_parent(info, dev,
902 obj->oo_inode = inode;
903 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
905 result = osd_check_lma(env, obj);
907 if (result == -ENODATA) {
909 result = osd_oi_lookup(info, dev, fid, id,
912 /* result == -ENOENT means that the OI
913 * mapping has been removed by race,
914 * the target inode belongs to other
917 * Others error also can be returned
920 obj->oo_inode = NULL;
923 /* result == 0 means the cached OI
924 * mapping is still in the OI file,
925 * the target the inode is valid. */
928 /* The current OI mapping is from the OI file,
929 * since the inode has been found via
930 * osd_iget_check(), no need recheck OI. */
937 obj->oo_inode = NULL;
938 if (result != -EREMCHG)
942 result = osd_oi_lookup(info, dev, fid, id,
944 /* result == -ENOENT means the cached OI mapping
945 * has been removed from the OI file by race,
946 * above target inode belongs to other object.
948 * Others error also can be returned directly. */
952 /* result == 0, goto trigger */
954 /* The current OI mapping is from the OI file,
955 * since the inode has been found via
956 * osd_iget_check(), no need recheck OI. */
963 obj->oo_compat_dot_created = 1;
964 obj->oo_compat_dotdot_created = 1;
966 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
967 GOTO(out, result = 0);
969 LASSERT(obj->oo_hl_head == NULL);
970 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
971 if (obj->oo_hl_head == NULL) {
972 obj->oo_inode = NULL;
974 GOTO(out, result = -ENOMEM);
976 GOTO(out, result = 0);
979 if (result != 0 && cached)
980 fid_zero(&oic->oic_fid);
982 LINVRNT(osd_invariant(obj));
987 * Concurrency: shouldn't matter.
989 static void osd_object_init0(struct osd_object *obj)
991 LASSERT(obj->oo_inode != NULL);
992 obj->oo_dt.do_body_ops = &osd_body_ops;
993 obj->oo_dt.do_lu.lo_header->loh_attr |=
994 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
998 * Concurrency: no concurrent access is possible that early in object
1001 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1002 const struct lu_object_conf *conf)
1004 struct osd_object *obj = osd_obj(l);
1007 LINVRNT(osd_invariant(obj));
1009 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1010 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1011 l->lo_header->loh_attr |= LOHA_EXISTS;
1015 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1016 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1017 if (result == 0 && obj->oo_inode != NULL) {
1018 struct osd_thread_info *oti = osd_oti_get(env);
1019 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1021 osd_object_init0(obj);
1022 result = osd_get_lma(oti, obj->oo_inode,
1023 &oti->oti_obj_dentry, lma);
1025 /* Convert LMAI flags to lustre LMA flags
1026 * and cache it to oo_lma_flags */
1028 lma_to_lustre_flags(lma->lma_incompat);
1029 } else if (result == -ENODATA) {
1034 LINVRNT(osd_invariant(obj));
1039 * Concurrency: no concurrent access is possible that late in object
1042 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1044 struct osd_object *obj = osd_obj(l);
1046 LINVRNT(osd_invariant(obj));
1048 dt_object_fini(&obj->oo_dt);
1049 if (obj->oo_hl_head != NULL)
1050 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1055 * Concurrency: no concurrent access is possible that late in object
1058 static void osd_index_fini(struct osd_object *o)
1060 struct iam_container *bag;
1062 if (o->oo_dir != NULL) {
1063 bag = &o->oo_dir->od_container;
1064 if (o->oo_inode != NULL) {
1065 if (bag->ic_object == o->oo_inode)
1066 iam_container_fini(bag);
1068 OBD_FREE_PTR(o->oo_dir);
1074 * Concurrency: no concurrent access is possible that late in object
1075 * life-cycle (for all existing callers, that is. New callers have to provide
1076 * their own locking.)
1078 static int osd_inode_unlinked(const struct inode *inode)
1080 return inode->i_nlink == 0;
1084 OSD_TXN_OI_DELETE_CREDITS = 20,
1085 OSD_TXN_INODE_DELETE_CREDITS = 20
1092 #if OSD_THANDLE_STATS
1094 * Set time when the handle is allocated
1096 static void osd_th_alloced(struct osd_thandle *oth)
1098 oth->oth_alloced = cfs_time_current();
1102 * Set time when the handle started
1104 static void osd_th_started(struct osd_thandle *oth)
1106 oth->oth_started = cfs_time_current();
1110 * Helper function to convert time interval to microseconds packed in
1113 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1117 cfs_duration_usec(cfs_time_sub(end, start), &val);
1118 return val.tv_sec * 1000000 + val.tv_usec;
1122 * Check whether the we deal with this handle for too long.
1124 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1125 cfs_time_t alloced, cfs_time_t started,
1128 cfs_time_t now = cfs_time_current();
1130 LASSERT(dev != NULL);
1132 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1133 interval_to_usec(alloced, started));
1134 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1135 interval_to_usec(started, closed));
1136 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1137 interval_to_usec(closed, now));
1139 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1140 CWARN("transaction handle %p was open for too long: "
1141 "now "CFS_TIME_T" ,"
1142 "alloced "CFS_TIME_T" ,"
1143 "started "CFS_TIME_T" ,"
1144 "closed "CFS_TIME_T"\n",
1145 oth, now, alloced, started, closed);
1146 libcfs_debug_dumpstack(NULL);
1150 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1152 cfs_time_t __closed = cfs_time_current(); \
1153 cfs_time_t __alloced = oth->oth_alloced; \
1154 cfs_time_t __started = oth->oth_started; \
1157 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1160 #else /* OSD_THANDLE_STATS */
1162 #define osd_th_alloced(h) do {} while(0)
1163 #define osd_th_started(h) do {} while(0)
1164 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1166 #endif /* OSD_THANDLE_STATS */
1169 * Concurrency: doesn't access mutable data.
1171 static int osd_param_is_not_sane(const struct osd_device *dev,
1172 const struct thandle *th)
1174 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1176 return oh->ot_credits > osd_transaction_size(dev);
1180 * Concurrency: shouldn't matter.
1182 static void osd_trans_commit_cb(struct super_block *sb,
1183 struct ldiskfs_journal_cb_entry *jcb, int error)
1185 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1186 struct thandle *th = &oh->ot_super;
1187 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1188 struct dt_txn_commit_cb *dcb, *tmp;
1190 LASSERT(oh->ot_handle == NULL);
1193 CERROR("transaction @0x%p commit error: %d\n", th, error);
1195 dt_txn_hook_commit(th);
1197 /* call per-transaction callbacks if any */
1198 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1200 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1201 "commit callback entry: magic=%x name='%s'\n",
1202 dcb->dcb_magic, dcb->dcb_name);
1203 list_del_init(&dcb->dcb_linkage);
1204 dcb->dcb_func(NULL, th, dcb, error);
1207 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1211 lu_context_exit(&th->th_ctx);
1212 lu_context_fini(&th->th_ctx);
1216 #ifndef HAVE_SB_START_WRITE
1217 # define sb_start_write(sb) do {} while (0)
1218 # define sb_end_write(sb) do {} while (0)
1221 static struct thandle *osd_trans_create(const struct lu_env *env,
1222 struct dt_device *d)
1224 struct osd_thread_info *oti = osd_oti_get(env);
1225 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1226 struct osd_thandle *oh;
1230 /* on pending IO in this thread should left from prev. request */
1231 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1233 sb_start_write(osd_sb(osd_dt_dev(d)));
1235 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1237 oh->ot_quota_trans = &oti->oti_quota_trans;
1238 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1242 th->th_tags = LCT_TX_HANDLE;
1244 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1245 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1248 memset(oti->oti_declare_ops, 0,
1249 sizeof(oti->oti_declare_ops));
1250 memset(oti->oti_declare_ops_cred, 0,
1251 sizeof(oti->oti_declare_ops_cred));
1252 memset(oti->oti_declare_ops_used, 0,
1253 sizeof(oti->oti_declare_ops_used));
1255 sb_end_write(osd_sb(osd_dt_dev(d)));
1256 th = ERR_PTR(-ENOMEM);
1261 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1263 struct osd_thread_info *oti = osd_oti_get(env);
1264 struct osd_thandle *oh;
1266 oh = container_of0(th, struct osd_thandle, ot_super);
1267 LASSERT(oh != NULL);
1269 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1270 oti->oti_declare_ops[OSD_OT_CREATE],
1271 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1272 oti->oti_declare_ops_used[OSD_OT_CREATE],
1273 oti->oti_declare_ops[OSD_OT_DESTROY],
1274 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1275 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1276 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1277 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1278 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1279 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1280 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1281 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1282 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1283 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1284 oti->oti_declare_ops[OSD_OT_WRITE],
1285 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1286 oti->oti_declare_ops_used[OSD_OT_WRITE],
1287 oti->oti_declare_ops[OSD_OT_PUNCH],
1288 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1289 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1290 oti->oti_declare_ops[OSD_OT_QUOTA],
1291 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1292 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1293 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1294 oti->oti_declare_ops[OSD_OT_INSERT],
1295 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1296 oti->oti_declare_ops_used[OSD_OT_INSERT],
1297 oti->oti_declare_ops[OSD_OT_DELETE],
1298 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1299 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1300 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1301 oti->oti_declare_ops[OSD_OT_REF_ADD],
1302 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1303 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1304 oti->oti_declare_ops[OSD_OT_REF_DEL],
1305 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1306 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1310 * Concurrency: shouldn't matter.
1312 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1315 struct osd_thread_info *oti = osd_oti_get(env);
1316 struct osd_device *dev = osd_dt_dev(d);
1318 struct osd_thandle *oh;
1323 LASSERT(current->journal_info == NULL);
1325 oh = container_of0(th, struct osd_thandle, ot_super);
1326 LASSERT(oh != NULL);
1327 LASSERT(oh->ot_handle == NULL);
1329 rc = dt_txn_hook_start(env, d, th);
1333 if (unlikely(osd_param_is_not_sane(dev, th))) {
1334 static unsigned long last_printed;
1335 static int last_credits;
1337 /* don't make noise on a tiny testing systems
1338 * actual credits misuse will be caught anyway */
1339 if (last_credits != oh->ot_credits &&
1340 time_after(jiffies, last_printed +
1341 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1342 osd_transaction_size(dev) > 512) {
1343 osd_trans_dump_creds(env, th);
1344 libcfs_debug_dumpstack(NULL);
1345 last_credits = oh->ot_credits;
1346 last_printed = jiffies;
1348 /* XXX Limit the credits to 'max_transaction_buffers', and
1349 * let the underlying filesystem to catch the error if
1350 * we really need so many credits.
1352 * This should be removed when we can calculate the
1353 * credits precisely. */
1354 oh->ot_credits = osd_transaction_size(dev);
1358 * XXX temporary stuff. Some abstraction layer should
1361 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1365 LASSERT(oti->oti_txns == 0);
1366 lu_context_init(&th->th_ctx, th->th_tags);
1367 lu_context_enter(&th->th_ctx);
1369 lu_device_get(&d->dd_lu_dev);
1370 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1381 static int osd_seq_exists(const struct lu_env *env,
1382 struct osd_device *osd, u64 seq)
1384 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1385 struct seq_server_site *ss = osd_seq_site(osd);
1389 LASSERT(ss != NULL);
1390 LASSERT(ss->ss_server_fld != NULL);
1392 rc = osd_fld_lookup(env, osd, seq, range);
1395 CERROR("%s: can't lookup FLD sequence "LPX64
1396 ": rc = %d\n", osd_name(osd), seq, rc);
1400 RETURN(ss->ss_node_id == range->lsr_index);
1403 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1405 struct dt_txn_commit_cb *dcb;
1406 struct dt_txn_commit_cb *tmp;
1408 /* call per-transaction stop callbacks if any */
1409 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1411 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1412 "commit callback entry: magic=%x name='%s'\n",
1413 dcb->dcb_magic, dcb->dcb_name);
1414 list_del_init(&dcb->dcb_linkage);
1415 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1420 * Concurrency: shouldn't matter.
1422 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1425 int rc = 0, remove_agents = 0;
1426 struct osd_thandle *oh;
1427 struct osd_thread_info *oti = osd_oti_get(env);
1428 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1429 struct osd_device *osd = osd_dt_dev(th->th_dev);
1430 struct qsd_instance *qsd = osd->od_quota_slave;
1431 struct lquota_trans *qtrans;
1434 oh = container_of0(th, struct osd_thandle, ot_super);
1436 /* reset OI cache for safety */
1437 oti->oti_ins_cache_used = 0;
1439 remove_agents = oh->ot_remove_agents;
1441 qtrans = oh->ot_quota_trans;
1442 oh->ot_quota_trans = NULL;
1444 if (oh->ot_handle != NULL) {
1445 handle_t *hdl = oh->ot_handle;
1448 * add commit callback
1449 * notice we don't do this in osd_trans_start()
1450 * as underlying transaction can change during truncate
1452 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1455 LASSERT(oti->oti_txns == 1);
1458 rc = dt_txn_hook_stop(env, th);
1460 CERROR("%s: failed in transaction hook: rc = %d\n",
1463 osd_trans_stop_cb(oh, rc);
1464 /* hook functions might modify th_sync */
1465 hdl->h_sync = th->th_sync;
1467 oh->ot_handle = NULL;
1468 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1470 CERROR("%s: failed to stop transaction: rc = %d\n",
1473 osd_trans_stop_cb(oh, th->th_result);
1477 /* inform the quota slave device that the transaction is stopping */
1478 qsd_op_end(env, qsd, qtrans);
1480 /* as we want IO to journal and data IO be concurrent, we don't block
1481 * awaiting data IO completion in osd_do_bio(), instead we wait here
1482 * once transaction is submitted to the journal. all reqular requests
1483 * don't do direct IO (except read/write), thus this wait_event becomes
1486 * IMPORTANT: we have to wait till any IO submited by the thread is
1487 * completed otherwise iobuf may be corrupted by different request
1489 wait_event(iobuf->dr_wait,
1490 atomic_read(&iobuf->dr_numreqs) == 0);
1491 osd_fini_iobuf(osd, iobuf);
1493 rc = iobuf->dr_error;
1495 if (unlikely(remove_agents != 0))
1496 osd_process_scheduled_agent_removals(env, osd);
1498 sb_end_write(osd_sb(osd));
1503 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1505 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1508 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1509 LASSERT(&dcb->dcb_func != NULL);
1510 if (dcb->dcb_flags & DCB_TRANS_STOP)
1511 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1513 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1519 * Called just before object is freed. Releases all resources except for
1520 * object itself (that is released by osd_object_free()).
1522 * Concurrency: no concurrent access is possible that late in object
1525 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1527 struct osd_object *obj = osd_obj(l);
1528 struct inode *inode = obj->oo_inode;
1530 LINVRNT(osd_invariant(obj));
1533 * If object is unlinked remove fid->ino mapping from object index.
1536 osd_index_fini(obj);
1537 if (inode != NULL) {
1538 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1539 qid_t uid = i_uid_read(inode);
1540 qid_t gid = i_gid_read(inode);
1543 obj->oo_inode = NULL;
1546 struct osd_thread_info *info = osd_oti_get(env);
1547 struct lquota_id_info *qi = &info->oti_qi;
1549 /* Release granted quota to master if necessary */
1550 qi->lqi_id.qid_uid = uid;
1551 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1553 qi->lqi_id.qid_uid = gid;
1554 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1560 * Concurrency: ->loo_object_release() is called under site spin-lock.
1562 static void osd_object_release(const struct lu_env *env,
1563 struct lu_object *l)
1565 struct osd_object *o = osd_obj(l);
1566 /* nobody should be releasing a non-destroyed object with nlink=0
1567 * the API allows this, but ldiskfs doesn't like and then report
1568 * this inode as deleted */
1569 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1574 * Concurrency: shouldn't matter.
1576 static int osd_object_print(const struct lu_env *env, void *cookie,
1577 lu_printer_t p, const struct lu_object *l)
1579 struct osd_object *o = osd_obj(l);
1580 struct iam_descr *d;
1582 if (o->oo_dir != NULL)
1583 d = o->oo_dir->od_container.ic_descr;
1586 return (*p)(env, cookie,
1587 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1589 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1590 o->oo_inode ? o->oo_inode->i_generation : 0,
1591 d ? d->id_ops->id_name : "plain");
1595 * Concurrency: shouldn't matter.
1597 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1598 struct obd_statfs *sfs)
1600 struct osd_device *osd = osd_dt_dev(d);
1601 struct super_block *sb = osd_sb(osd);
1602 struct kstatfs *ksfs;
1606 if (unlikely(osd->od_mnt == NULL))
1607 return -EINPROGRESS;
1609 /* osd_lproc.c call this without env, allocate ksfs for that case */
1610 if (unlikely(env == NULL)) {
1611 OBD_ALLOC_PTR(ksfs);
1615 ksfs = &osd_oti_get(env)->oti_ksfs;
1618 result = sb->s_op->statfs(sb->s_root, ksfs);
1622 statfs_pack(sfs, ksfs);
1623 if (unlikely(sb->s_flags & MS_RDONLY))
1624 sfs->os_state = OS_STATE_READONLY;
1625 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1626 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1627 sfs->os_maxbytes = sb->s_maxbytes;
1629 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1632 * Reserve some space so to avoid fragmenting the filesystem too much.
1633 * Fragmentation not only impacts performance, but can also increase
1634 * metadata overhead significantly, causing grant calculation to be
1637 * Reserve 0.78% of total space, at least 8MB for small filesystems.
1639 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
1640 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
1641 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
1642 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
1644 sfs->os_blocks -= reserved;
1645 sfs->os_bfree -= min(reserved, sfs->os_bfree);
1646 sfs->os_bavail -= min(reserved, sfs->os_bavail);
1649 if (unlikely(env == NULL))
1655 * Estimate space needed for file creations. We assume the largest filename
1656 * which is 2^64 - 1, hence a filename of 20 chars.
1657 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1659 #ifdef __LDISKFS_DIR_REC_LEN
1660 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1662 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1666 * Concurrency: doesn't access mutable data.
1668 static void osd_conf_get(const struct lu_env *env,
1669 const struct dt_device *dev,
1670 struct dt_device_param *param)
1672 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1676 * XXX should be taken from not-yet-existing fs abstraction layer.
1678 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1679 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1680 param->ddp_symlink_max = sb->s_blocksize;
1681 param->ddp_mount_type = LDD_MT_LDISKFS;
1682 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1683 param->ddp_maxbytes = sb->s_maxbytes;
1685 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1686 /* inode are statically allocated, so per-inode space consumption
1687 * is the space consumed by the directory entry */
1688 param->ddp_inodespace = PER_OBJ_USAGE;
1689 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
1690 * = 128MB) which is unlikely to be hit in real life. Report a smaller
1691 * maximum length to not under count the actual number of extents
1692 * needed for writing a file. */
1693 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
1694 /* worst-case extent insertion metadata overhead */
1695 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
1696 param->ddp_mntopts = 0;
1697 if (test_opt(sb, XATTR_USER))
1698 param->ddp_mntopts |= MNTOPT_USERXATTR;
1699 if (test_opt(sb, POSIX_ACL))
1700 param->ddp_mntopts |= MNTOPT_ACL;
1702 /* LOD might calculate the max stripe count based on max_ea_size,
1703 * so we need take account in the overhead as well,
1704 * xattr_header + magic + xattr_entry_head */
1705 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1706 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1708 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1709 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1710 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1714 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1718 * Concurrency: shouldn't matter.
1720 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1724 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1726 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1728 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1729 LUSTRE_OSD_LDISKFS_NAME, rc);
1735 * Start commit for OSD device.
1737 * An implementation of dt_commit_async method for OSD device.
1738 * Asychronously starts underlayng fs sync and thereby a transaction
1741 * \param env environment
1742 * \param d dt device
1744 * \see dt_device_operations
1746 static int osd_commit_async(const struct lu_env *env,
1747 struct dt_device *d)
1749 struct super_block *s = osd_sb(osd_dt_dev(d));
1752 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1753 RETURN(s->s_op->sync_fs(s, 0));
1757 * Concurrency: shouldn't matter.
1760 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1762 struct super_block *sb = osd_sb(osd_dt_dev(d));
1763 struct block_device *dev = sb->s_bdev;
1764 #ifdef HAVE_DEV_SET_RDONLY
1765 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1768 int rc = -EOPNOTSUPP;
1772 #ifdef HAVE_DEV_SET_RDONLY
1773 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1775 if (jdev && (jdev != dev)) {
1776 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1778 dev_set_rdonly(jdev);
1780 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1781 dev_set_rdonly(dev);
1783 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1784 osd_dt_dev(d)->od_svname, (long)dev, rc);
1790 * Note: we do not count into QUOTA here.
1791 * If we mount with --data_journal we may need more.
1793 const int osd_dto_credits_noquota[DTO_NR] = {
1796 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1797 * SINGLEDATA_TRANS_BLOCKS(8)
1798 * XXX Note: maybe iam need more, since iam have more level than
1801 [DTO_INDEX_INSERT] = 16,
1804 * just modify a single entry, probably merge few within a block
1806 [DTO_INDEX_DELETE] = 1,
1810 [DTO_INDEX_UPDATE] = 16,
1812 * 4(inode, inode bits, groups, GDT)
1813 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1815 [DTO_OBJECT_CREATE] = 4,
1817 * 4(inode, inode bits, groups, GDT)
1818 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1820 [DTO_OBJECT_DELETE] = 4,
1822 * Attr set credits (inode)
1824 [DTO_ATTR_SET_BASE] = 1,
1826 * Xattr set. The same as xattr of EXT3.
1827 * DATA_TRANS_BLOCKS(14)
1828 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1829 * are also counted in. Do not know why?
1831 [DTO_XATTR_SET] = 14,
1833 * credits for inode change during write.
1835 [DTO_WRITE_BASE] = 3,
1837 * credits for single block write.
1839 [DTO_WRITE_BLOCK] = 14,
1841 * Attr set credits for chown.
1842 * This is extra credits for setattr, and it is null without quota
1844 [DTO_ATTR_SET_CHOWN] = 0
1847 static const struct dt_device_operations osd_dt_ops = {
1848 .dt_root_get = osd_root_get,
1849 .dt_statfs = osd_statfs,
1850 .dt_trans_create = osd_trans_create,
1851 .dt_trans_start = osd_trans_start,
1852 .dt_trans_stop = osd_trans_stop,
1853 .dt_trans_cb_add = osd_trans_cb_add,
1854 .dt_conf_get = osd_conf_get,
1855 .dt_sync = osd_sync,
1857 .dt_commit_async = osd_commit_async,
1860 static void osd_object_read_lock(const struct lu_env *env,
1861 struct dt_object *dt, unsigned role)
1863 struct osd_object *obj = osd_dt_obj(dt);
1864 struct osd_thread_info *oti = osd_oti_get(env);
1866 LINVRNT(osd_invariant(obj));
1868 LASSERT(obj->oo_owner != env);
1869 down_read_nested(&obj->oo_sem, role);
1871 LASSERT(obj->oo_owner == NULL);
1875 static void osd_object_write_lock(const struct lu_env *env,
1876 struct dt_object *dt, unsigned role)
1878 struct osd_object *obj = osd_dt_obj(dt);
1879 struct osd_thread_info *oti = osd_oti_get(env);
1881 LINVRNT(osd_invariant(obj));
1883 LASSERT(obj->oo_owner != env);
1884 down_write_nested(&obj->oo_sem, role);
1886 LASSERT(obj->oo_owner == NULL);
1887 obj->oo_owner = env;
1891 static void osd_object_read_unlock(const struct lu_env *env,
1892 struct dt_object *dt)
1894 struct osd_object *obj = osd_dt_obj(dt);
1895 struct osd_thread_info *oti = osd_oti_get(env);
1897 LINVRNT(osd_invariant(obj));
1899 LASSERT(oti->oti_r_locks > 0);
1901 up_read(&obj->oo_sem);
1904 static void osd_object_write_unlock(const struct lu_env *env,
1905 struct dt_object *dt)
1907 struct osd_object *obj = osd_dt_obj(dt);
1908 struct osd_thread_info *oti = osd_oti_get(env);
1910 LINVRNT(osd_invariant(obj));
1912 LASSERT(obj->oo_owner == env);
1913 LASSERT(oti->oti_w_locks > 0);
1915 obj->oo_owner = NULL;
1916 up_write(&obj->oo_sem);
1919 static int osd_object_write_locked(const struct lu_env *env,
1920 struct dt_object *dt)
1922 struct osd_object *obj = osd_dt_obj(dt);
1924 LINVRNT(osd_invariant(obj));
1926 return obj->oo_owner == env;
1929 static struct timespec *osd_inode_time(const struct lu_env *env,
1930 struct inode *inode, __u64 seconds)
1932 struct osd_thread_info *oti = osd_oti_get(env);
1933 struct timespec *t = &oti->oti_time;
1935 t->tv_sec = seconds;
1937 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1941 static void osd_inode_getattr(const struct lu_env *env,
1942 struct inode *inode, struct lu_attr *attr)
1944 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1945 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1946 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1949 attr->la_atime = LTIME_S(inode->i_atime);
1950 attr->la_mtime = LTIME_S(inode->i_mtime);
1951 attr->la_ctime = LTIME_S(inode->i_ctime);
1952 attr->la_mode = inode->i_mode;
1953 attr->la_size = i_size_read(inode);
1954 attr->la_blocks = inode->i_blocks;
1955 attr->la_uid = i_uid_read(inode);
1956 attr->la_gid = i_gid_read(inode);
1957 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
1958 attr->la_nlink = inode->i_nlink;
1959 attr->la_rdev = inode->i_rdev;
1960 attr->la_blksize = 1 << inode->i_blkbits;
1961 attr->la_blkbits = inode->i_blkbits;
1964 static int osd_attr_get(const struct lu_env *env,
1965 struct dt_object *dt,
1966 struct lu_attr *attr)
1968 struct osd_object *obj = osd_dt_obj(dt);
1970 if (unlikely(!dt_object_exists(dt)))
1972 if (unlikely(obj->oo_destroyed))
1975 LASSERT(!dt_object_remote(dt));
1976 LINVRNT(osd_invariant(obj));
1978 spin_lock(&obj->oo_guard);
1979 osd_inode_getattr(env, obj->oo_inode, attr);
1980 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
1981 attr->la_flags |= LUSTRE_ORPHAN_FL;
1982 spin_unlock(&obj->oo_guard);
1987 static int osd_declare_attr_set(const struct lu_env *env,
1988 struct dt_object *dt,
1989 const struct lu_attr *attr,
1990 struct thandle *handle)
1992 struct osd_thandle *oh;
1993 struct osd_object *obj;
1994 struct osd_thread_info *info = osd_oti_get(env);
1995 struct lquota_id_info *qi = &info->oti_qi;
2003 LASSERT(dt != NULL);
2004 LASSERT(handle != NULL);
2006 obj = osd_dt_obj(dt);
2007 LASSERT(osd_invariant(obj));
2009 oh = container_of0(handle, struct osd_thandle, ot_super);
2010 LASSERT(oh->ot_handle == NULL);
2012 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2013 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2015 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2016 osd_dto_credits_noquota[DTO_XATTR_SET]);
2018 if (attr == NULL || obj->oo_inode == NULL)
2021 bspace = obj->oo_inode->i_blocks;
2022 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2023 bspace = toqb(bspace);
2025 /* Changing ownership is always preformed by super user, it should not
2028 * We still need to call the osd_declare_qid() to calculate the journal
2029 * credits for updating quota accounting files and to trigger quota
2030 * space adjustment once the operation is completed.*/
2031 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2033 uid = i_uid_read(obj->oo_inode);
2034 qi->lqi_type = USRQUOTA;
2035 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2036 /* inode accounting */
2037 qi->lqi_is_blk = false;
2039 /* one more inode for the new uid ... */
2040 qi->lqi_id.qid_uid = attr->la_uid;
2042 /* Reserve credits for the new uid */
2043 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2044 if (rc == -EDQUOT || rc == -EINPROGRESS)
2049 /* and one less inode for the current uid */
2050 qi->lqi_id.qid_uid = uid;
2052 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2053 if (rc == -EDQUOT || rc == -EINPROGRESS)
2058 /* block accounting */
2059 qi->lqi_is_blk = true;
2061 /* more blocks for the new uid ... */
2062 qi->lqi_id.qid_uid = attr->la_uid;
2063 qi->lqi_space = bspace;
2065 * Credits for the new uid has been reserved, re-use "obj"
2066 * to save credit reservation.
2068 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2069 if (rc == -EDQUOT || rc == -EINPROGRESS)
2074 /* and finally less blocks for the current uid */
2075 qi->lqi_id.qid_uid = uid;
2076 qi->lqi_space = -bspace;
2077 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2078 if (rc == -EDQUOT || rc == -EINPROGRESS)
2084 gid = i_gid_read(obj->oo_inode);
2085 qi->lqi_type = GRPQUOTA;
2086 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2088 /* inode accounting */
2089 qi->lqi_is_blk = false;
2091 /* one more inode for the new gid ... */
2092 qi->lqi_id.qid_gid = attr->la_gid;
2094 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2095 if (rc == -EDQUOT || rc == -EINPROGRESS)
2100 /* and one less inode for the current gid */
2101 qi->lqi_id.qid_gid = gid;
2103 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2104 if (rc == -EDQUOT || rc == -EINPROGRESS)
2109 /* block accounting */
2110 qi->lqi_is_blk = true;
2112 /* more blocks for the new gid ... */
2113 qi->lqi_id.qid_gid = attr->la_gid;
2114 qi->lqi_space = bspace;
2115 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2116 if (rc == -EDQUOT || rc == -EINPROGRESS)
2121 /* and finally less blocks for the current gid */
2122 qi->lqi_id.qid_gid = gid;
2123 qi->lqi_space = -bspace;
2124 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2125 if (rc == -EDQUOT || rc == -EINPROGRESS)
2134 static int osd_inode_setattr(const struct lu_env *env,
2135 struct inode *inode, const struct lu_attr *attr)
2137 __u64 bits = attr->la_valid;
2139 /* Only allow set size for regular file */
2140 if (!S_ISREG(inode->i_mode))
2141 bits &= ~(LA_SIZE | LA_BLOCKS);
2146 if (bits & LA_ATIME)
2147 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2148 if (bits & LA_CTIME)
2149 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2150 if (bits & LA_MTIME)
2151 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2152 if (bits & LA_SIZE) {
2153 LDISKFS_I(inode)->i_disksize = attr->la_size;
2154 i_size_write(inode, attr->la_size);
2158 /* OSD should not change "i_blocks" which is used by quota.
2159 * "i_blocks" should be changed by ldiskfs only. */
2160 if (bits & LA_BLOCKS)
2161 inode->i_blocks = attr->la_blocks;
2164 inode->i_mode = (inode->i_mode & S_IFMT) |
2165 (attr->la_mode & ~S_IFMT);
2167 i_uid_write(inode, attr->la_uid);
2169 i_gid_write(inode, attr->la_gid);
2170 if (bits & LA_NLINK)
2171 set_nlink(inode, attr->la_nlink);
2173 inode->i_rdev = attr->la_rdev;
2175 if (bits & LA_FLAGS) {
2176 /* always keep S_NOCMTIME */
2177 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2183 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2185 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2186 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2190 ll_vfs_dq_init(inode);
2192 if (attr->la_valid & LA_UID)
2193 iattr.ia_valid |= ATTR_UID;
2194 if (attr->la_valid & LA_GID)
2195 iattr.ia_valid |= ATTR_GID;
2196 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2197 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2199 rc = ll_vfs_dq_transfer(inode, &iattr);
2201 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2202 "enforcement enabled on the ldiskfs "
2203 "filesystem?\n", inode->i_sb->s_id, rc);
2210 static int osd_attr_set(const struct lu_env *env,
2211 struct dt_object *dt,
2212 const struct lu_attr *attr,
2213 struct thandle *handle)
2215 struct osd_object *obj = osd_dt_obj(dt);
2216 struct inode *inode;
2219 if (!dt_object_exists(dt))
2222 LASSERT(handle != NULL);
2223 LASSERT(!dt_object_remote(dt));
2224 LASSERT(osd_invariant(obj));
2226 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2228 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2229 struct osd_thread_info *oti = osd_oti_get(env);
2230 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2231 struct lu_fid *fid1 = &oti->oti_fid;
2232 struct osd_inode_id *id = &oti->oti_id;
2233 struct iam_path_descr *ipd;
2234 struct iam_container *bag;
2235 struct osd_thandle *oh;
2238 fid_cpu_to_be(fid1, fid0);
2239 memset(id, 1, sizeof(*id));
2240 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2241 fid0)->oi_dir.od_container;
2242 ipd = osd_idx_ipd_get(env, bag);
2243 if (unlikely(ipd == NULL))
2246 oh = container_of0(handle, struct osd_thandle, ot_super);
2247 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2248 (const struct iam_rec *)id, ipd);
2249 osd_ipd_put(env, bag, ipd);
2250 return(rc > 0 ? 0 : rc);
2253 inode = obj->oo_inode;
2255 rc = osd_quota_transfer(inode, attr);
2259 spin_lock(&obj->oo_guard);
2260 rc = osd_inode_setattr(env, inode, attr);
2261 spin_unlock(&obj->oo_guard);
2265 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2267 if (!(attr->la_valid & LA_FLAGS))
2270 /* Let's check if there are extra flags need to be set into LMA */
2271 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2272 struct osd_thread_info *info = osd_oti_get(env);
2273 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2275 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2279 lma->lma_incompat |=
2280 lustre_to_lma_flags(attr->la_flags);
2281 lustre_lma_swab(lma);
2282 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2283 lma, sizeof(*lma), XATTR_REPLACE);
2285 struct osd_device *osd = osd_obj2dev(obj);
2287 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2288 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2289 lma->lma_incompat, rc);
2292 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2294 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2297 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2302 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2303 struct osd_object *obj,
2304 const char *name, const int namelen)
2306 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2309 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2310 umode_t mode, struct dt_allocation_hint *hint,
2314 struct osd_device *osd = osd_obj2dev(obj);
2315 struct osd_thandle *oth;
2316 struct dt_object *parent = NULL;
2317 struct inode *inode;
2319 LINVRNT(osd_invariant(obj));
2320 LASSERT(obj->oo_inode == NULL);
2321 LASSERT(obj->oo_hl_head == NULL);
2323 if (S_ISDIR(mode) && ldiskfs_pdo) {
2324 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2325 if (obj->oo_hl_head == NULL)
2329 oth = container_of(th, struct osd_thandle, ot_super);
2330 LASSERT(oth->ot_handle->h_transaction != NULL);
2332 if (hint != NULL && hint->dah_parent != NULL &&
2333 !dt_object_remote(hint->dah_parent))
2334 parent = hint->dah_parent;
2336 inode = ldiskfs_create_inode(oth->ot_handle,
2337 parent ? osd_dt_obj(parent)->oo_inode :
2338 osd_sb(osd)->s_root->d_inode,
2340 if (!IS_ERR(inode)) {
2341 /* Do not update file c/mtime in ldiskfs. */
2342 inode->i_flags |= S_NOCMTIME;
2344 /* For new created object, it must be consistent,
2345 * and it is unnecessary to scrub against it. */
2346 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2348 obj->oo_inode = inode;
2351 if (obj->oo_hl_head != NULL) {
2352 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2353 obj->oo_hl_head = NULL;
2355 result = PTR_ERR(inode);
2357 LINVRNT(osd_invariant(obj));
2365 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2366 struct lu_attr *attr,
2367 struct dt_allocation_hint *hint,
2368 struct dt_object_format *dof,
2372 struct osd_thandle *oth;
2373 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2375 LASSERT(S_ISDIR(attr->la_mode));
2377 oth = container_of(th, struct osd_thandle, ot_super);
2378 LASSERT(oth->ot_handle->h_transaction != NULL);
2379 result = osd_mkfile(info, obj, mode, hint, th);
2384 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2385 struct lu_attr *attr,
2386 struct dt_allocation_hint *hint,
2387 struct dt_object_format *dof,
2391 struct osd_thandle *oth;
2392 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2394 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2396 LASSERT(S_ISREG(attr->la_mode));
2398 oth = container_of(th, struct osd_thandle, ot_super);
2399 LASSERT(oth->ot_handle->h_transaction != NULL);
2401 result = osd_mkfile(info, obj, mode, hint, th);
2403 LASSERT(obj->oo_inode != NULL);
2404 if (feat->dif_flags & DT_IND_VARKEY)
2405 result = iam_lvar_create(obj->oo_inode,
2406 feat->dif_keysize_max,
2408 feat->dif_recsize_max,
2411 result = iam_lfix_create(obj->oo_inode,
2412 feat->dif_keysize_max,
2414 feat->dif_recsize_max,
2421 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2422 struct lu_attr *attr,
2423 struct dt_allocation_hint *hint,
2424 struct dt_object_format *dof,
2427 LASSERT(S_ISREG(attr->la_mode));
2428 return osd_mkfile(info, obj, (attr->la_mode &
2429 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2432 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2433 struct lu_attr *attr,
2434 struct dt_allocation_hint *hint,
2435 struct dt_object_format *dof,
2438 LASSERT(S_ISLNK(attr->la_mode));
2439 return osd_mkfile(info, obj, (attr->la_mode &
2440 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2443 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2444 struct lu_attr *attr,
2445 struct dt_allocation_hint *hint,
2446 struct dt_object_format *dof,
2449 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2452 LINVRNT(osd_invariant(obj));
2453 LASSERT(obj->oo_inode == NULL);
2454 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2455 S_ISFIFO(mode) || S_ISSOCK(mode));
2457 result = osd_mkfile(info, obj, mode, hint, th);
2459 LASSERT(obj->oo_inode != NULL);
2461 * This inode should be marked dirty for i_rdev. Currently
2462 * that is done in the osd_attr_init().
2464 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2467 LINVRNT(osd_invariant(obj));
2471 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2473 struct dt_allocation_hint *hint,
2474 struct dt_object_format *dof,
2477 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2479 osd_obj_type_f result;
2495 result = osd_mk_index;
2506 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2507 struct dt_object *parent, struct dt_object *child,
2512 ah->dah_parent = parent;
2513 ah->dah_mode = child_mode;
2515 if (parent != NULL && !dt_object_remote(parent)) {
2516 /* will help to find FID->ino at dt_insert("..") */
2517 struct osd_object *pobj = osd_dt_obj(parent);
2518 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2522 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2523 struct lu_attr *attr, struct dt_object_format *dof)
2525 struct inode *inode = obj->oo_inode;
2526 __u64 valid = attr->la_valid;
2529 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2531 if (dof->dof_type != DFT_NODE)
2532 attr->la_valid &= ~LA_RDEV;
2533 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2534 attr->la_valid &= ~LA_ATIME;
2535 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2536 attr->la_valid &= ~LA_CTIME;
2537 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2538 attr->la_valid &= ~LA_MTIME;
2540 result = osd_quota_transfer(inode, attr);
2544 if (attr->la_valid != 0) {
2545 result = osd_inode_setattr(info->oti_env, inode, attr);
2547 * The osd_inode_setattr() should always succeed here. The
2548 * only error that could be returned is EDQUOT when we are
2549 * trying to change the UID or GID of the inode. However, this
2550 * should not happen since quota enforcement is no longer
2551 * enabled on ldiskfs (lquota takes care of it).
2553 LASSERTF(result == 0, "%d\n", result);
2554 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2557 attr->la_valid = valid;
2561 * Helper function for osd_object_create()
2563 * \retval 0, on success
2565 static int __osd_object_create(struct osd_thread_info *info,
2566 struct osd_object *obj, struct lu_attr *attr,
2567 struct dt_allocation_hint *hint,
2568 struct dt_object_format *dof,
2574 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2576 /* we drop umask so that permissions we pass are not affected */
2577 umask = current->fs->umask;
2578 current->fs->umask = 0;
2580 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2582 if (likely(obj->oo_inode != NULL)) {
2583 LASSERT(obj->oo_inode->i_state & I_NEW);
2585 /* Unlock the inode before attr initialization to avoid
2586 * unnecessary dqget operations. LU-6378 */
2587 unlock_new_inode(obj->oo_inode);
2590 if (likely(result == 0)) {
2591 osd_attr_init(info, obj, attr, dof);
2592 osd_object_init0(obj);
2595 /* restore previous umask value */
2596 current->fs->umask = umask;
2598 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2604 * Helper function for osd_object_create()
2606 * \retval 0, on success
2608 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2609 const struct lu_fid *fid, struct thandle *th)
2611 struct osd_thread_info *info = osd_oti_get(env);
2612 struct osd_inode_id *id = &info->oti_id;
2613 struct osd_device *osd = osd_obj2dev(obj);
2614 struct osd_thandle *oh;
2617 LASSERT(obj->oo_inode != NULL);
2619 oh = container_of0(th, struct osd_thandle, ot_super);
2620 LASSERT(oh->ot_handle);
2621 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2623 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2624 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2625 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2630 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2631 u64 seq, struct lu_seq_range *range)
2633 struct seq_server_site *ss = osd_seq_site(osd);
2635 if (fid_seq_is_idif(seq)) {
2636 fld_range_set_ost(range);
2637 range->lsr_index = idif_ost_idx(seq);
2641 if (!fid_seq_in_fldb(seq)) {
2642 fld_range_set_mdt(range);
2644 /* FIXME: If ss is NULL, it suppose not get lsr_index
2646 range->lsr_index = ss->ss_node_id;
2650 LASSERT(ss != NULL);
2651 fld_range_set_any(range);
2652 /* OSD will only do local fld lookup */
2653 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2657 * Concurrency: no external locking is necessary.
2659 static int osd_declare_object_create(const struct lu_env *env,
2660 struct dt_object *dt,
2661 struct lu_attr *attr,
2662 struct dt_allocation_hint *hint,
2663 struct dt_object_format *dof,
2664 struct thandle *handle)
2666 struct osd_thandle *oh;
2670 LASSERT(handle != NULL);
2672 oh = container_of0(handle, struct osd_thandle, ot_super);
2673 LASSERT(oh->ot_handle == NULL);
2675 /* EA object consumes more credits than regular object: osd_mk_index
2676 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2677 * leaf_node, could involves the block, block bitmap, groups, GDT
2678 * change for each block, so add 4 * 2 credits in that case. */
2679 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2680 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2681 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2682 /* Reuse idle OI block may cause additional one OI block
2684 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2685 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2690 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2691 osd_dt_obj(dt), false, NULL, false);
2695 /* will help to find FID->ino mapping at dt_insert() */
2696 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
2702 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2703 struct lu_attr *attr,
2704 struct dt_allocation_hint *hint,
2705 struct dt_object_format *dof, struct thandle *th)
2707 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2708 struct osd_object *obj = osd_dt_obj(dt);
2709 struct osd_thread_info *info = osd_oti_get(env);
2713 if (dt_object_exists(dt))
2716 LINVRNT(osd_invariant(obj));
2717 LASSERT(!dt_object_remote(dt));
2718 LASSERT(osd_write_locked(env, obj));
2719 LASSERT(th != NULL);
2721 if (unlikely(fid_is_acct(fid)))
2722 /* Quota files can't be created from the kernel any more,
2723 * 'tune2fs -O quota' will take care of creating them */
2726 result = __osd_object_create(info, obj, attr, hint, dof, th);
2728 result = __osd_oi_insert(env, obj, fid, th);
2729 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2730 obj->oo_dt.do_body_ops = &osd_body_ops;
2732 LASSERT(ergo(result == 0,
2733 dt_object_exists(dt) && !dt_object_remote(dt)));
2735 LASSERT(osd_invariant(obj));
2740 * Called to destroy on-disk representation of the object
2742 * Concurrency: must be locked
2744 static int osd_declare_object_destroy(const struct lu_env *env,
2745 struct dt_object *dt,
2748 struct osd_object *obj = osd_dt_obj(dt);
2749 struct inode *inode = obj->oo_inode;
2750 struct osd_thandle *oh;
2757 oh = container_of0(th, struct osd_thandle, ot_super);
2758 LASSERT(oh->ot_handle == NULL);
2760 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2761 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2762 /* Recycle idle OI leaf may cause additional three OI blocks
2764 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2765 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2766 /* one less inode */
2767 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2768 -1, oh, obj, false, NULL, false);
2771 /* data to be truncated */
2772 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2773 0, oh, obj, true, NULL, false);
2777 /* will help to find FID->ino when this object is being
2778 * added to PENDING/ */
2779 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
2784 static int osd_object_destroy(const struct lu_env *env,
2785 struct dt_object *dt,
2788 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2789 struct osd_object *obj = osd_dt_obj(dt);
2790 struct inode *inode = obj->oo_inode;
2791 struct osd_device *osd = osd_obj2dev(obj);
2792 struct osd_thandle *oh;
2796 oh = container_of0(th, struct osd_thandle, ot_super);
2797 LASSERT(oh->ot_handle);
2799 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2801 if (unlikely(fid_is_acct(fid)))
2804 if (S_ISDIR(inode->i_mode)) {
2805 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2806 inode->i_nlink == 2);
2807 /* it will check/delete the inode from remote parent,
2808 * how to optimize it? unlink performance impaction XXX */
2809 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2810 if (result != 0 && result != -ENOENT) {
2811 CERROR("%s: delete inode "DFID": rc = %d\n",
2812 osd_name(osd), PFID(fid), result);
2814 spin_lock(&obj->oo_guard);
2816 spin_unlock(&obj->oo_guard);
2817 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2820 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2822 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2823 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2826 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2827 /* XXX: add to ext3 orphan list */
2828 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2830 /* not needed in the cache anymore */
2831 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2832 obj->oo_destroyed = 1;
2838 * Put the fid into lustre_mdt_attrs, and then place the structure
2839 * inode's ea. This fid should not be altered during the life time
2842 * \retval +ve, on success
2843 * \retval -ve, on error
2845 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2847 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2848 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2850 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2854 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2857 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2860 lustre_lma_init(lma, fid, compat, incompat);
2861 lustre_lma_swab(lma);
2863 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2865 /* LMA may already exist, but we need to check that all the
2866 * desired compat/incompat flags have been added. */
2867 if (unlikely(rc == -EEXIST)) {
2868 if (compat == 0 && incompat == 0)
2871 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2872 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2877 lustre_lma_swab(lma);
2878 if (!(~lma->lma_compat & compat) &&
2879 !(~lma->lma_incompat & incompat))
2882 lma->lma_compat |= compat;
2883 lma->lma_incompat |= incompat;
2884 lustre_lma_swab(lma);
2885 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2886 sizeof(*lma), XATTR_REPLACE);
2893 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2894 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2895 * To have compatilibility with 1.8 ldiskfs driver we need to have
2896 * magic number at start of fid data.
2897 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2900 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2901 const struct lu_fid *fid)
2903 if (!fid_is_namespace_visible(fid) ||
2904 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2905 param->edp_magic = 0;
2909 param->edp_magic = LDISKFS_LUFID_MAGIC;
2910 param->edp_len = sizeof(struct lu_fid) + 1;
2911 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2915 * Try to read the fid from inode ea into dt_rec.
2917 * \param fid object fid.
2919 * \retval 0 on success
2921 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2922 __u32 ino, struct lu_fid *fid,
2923 struct osd_inode_id *id)
2925 struct osd_thread_info *info = osd_oti_get(env);
2926 struct inode *inode;
2929 osd_id_gen(id, ino, OSD_OII_NOGEN);
2930 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2932 RETURN(PTR_ERR(inode));
2938 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2940 struct inode *parent_dir,
2941 const struct lu_fid *dot_fid,
2942 const struct lu_fid *dot_dot_fid,
2943 struct osd_thandle *oth)
2945 struct ldiskfs_dentry_param *dot_ldp;
2946 struct ldiskfs_dentry_param *dot_dot_ldp;
2947 __u32 saved_nlink = dir->i_nlink;
2950 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2951 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2953 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2954 dot_ldp->edp_magic = 0;
2956 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2957 dir, dot_ldp, dot_dot_ldp);
2958 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2959 * the subseqent ref_add() will increase the dir->i_nlink
2960 * as 3. That is incorrect for new created directory.
2962 * It looks like hack, because we want to make the OSD API
2963 * to be order-independent for new created directory object
2964 * between dt_insert(..) and ref_add() operations.
2966 * Here, we only restore the in-RAM dir-inode's nlink attr,
2967 * becuase if the nlink attr is not 2, then there will be
2968 * ref_add() called following the dt_insert(..), such call
2969 * will make both the in-RAM and on-disk dir-inode's nlink
2970 * attr to be set as 2. LU-7447 */
2971 set_nlink(dir, saved_nlink);
2976 * Create an local agent inode for remote entry
2978 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2979 struct osd_device *osd,
2980 struct osd_object *pobj,
2981 const struct lu_fid *fid,
2985 struct osd_thread_info *info = osd_oti_get(env);
2986 struct inode *local;
2987 struct osd_thandle *oh;
2992 oh = container_of(th, struct osd_thandle, ot_super);
2993 LASSERT(oh->ot_handle->h_transaction != NULL);
2995 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2996 if (IS_ERR(local)) {
2997 CERROR("%s: create local error %d\n", osd_name(osd),
2998 (int)PTR_ERR(local));
3002 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3003 unlock_new_inode(local);
3005 /* Set special LMA flag for local agent inode */
3006 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3008 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3009 osd_name(osd), PFID(fid), rc);
3010 RETURN(ERR_PTR(rc));
3016 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3017 lu_object_fid(&pobj->oo_dt.do_lu),
3020 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3021 osd_name(osd), PFID(fid), rc);
3022 RETURN(ERR_PTR(rc));
3029 * when direntry is deleted, we have to take care of possible agent inode
3030 * referenced by that. unfortunately we can't do this at that point:
3031 * iget() within a running transaction leads to deadlock and we better do
3032 * not call that every delete declaration to save performance. so we put
3033 * a potention agent inode on a list and process that once the transaction
3034 * is over. Notice it's not any worse in terms of real orphans as regular
3035 * object destroy doesn't put inodes on the on-disk orphan list. this should
3036 * be addressed separately
3038 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3039 struct osd_thandle *oh,
3042 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3043 struct osd_obj_orphan *oor;
3050 oor->oor_env = (struct lu_env *)env;
3051 spin_lock(&osd->od_osfs_lock);
3052 list_add(&oor->oor_list, &osd->od_orphan_list);
3053 spin_unlock(&osd->od_osfs_lock);
3055 oh->ot_remove_agents = 1;
3061 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3062 struct osd_device *osd)
3064 struct osd_thread_info *info = osd_oti_get(env);
3065 struct osd_obj_orphan *oor, *tmp;
3066 struct osd_inode_id id;
3067 struct list_head list;
3068 struct inode *inode;
3073 INIT_LIST_HEAD(&list);
3075 spin_lock(&osd->od_osfs_lock);
3076 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3077 if (oor->oor_env == env) {
3078 list_del(&oor->oor_list);
3079 list_add(&oor->oor_list, &list);
3082 spin_unlock(&osd->od_osfs_lock);
3084 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3088 list_del(&oor->oor_list);
3091 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3092 inode = osd_iget_fid(info, osd, &id, &fid);
3096 if (!osd_remote_fid(env, osd, &fid)) {
3101 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3103 mark_inode_dirty(inode);
3104 ldiskfs_journal_stop(jh);
3112 * OSD layer object create function for interoperability mode (b11826).
3113 * This is mostly similar to osd_object_create(). Only difference being, fid is
3114 * inserted into inode ea here.
3116 * \retval 0, on success
3117 * \retval -ve, on error
3119 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3120 struct lu_attr *attr,
3121 struct dt_allocation_hint *hint,
3122 struct dt_object_format *dof,
3125 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3126 struct osd_object *obj = osd_dt_obj(dt);
3127 struct osd_thread_info *info = osd_oti_get(env);
3128 int result, on_ost = 0;
3132 if (dt_object_exists(dt))
3135 LASSERT(osd_invariant(obj));
3136 LASSERT(!dt_object_remote(dt));
3137 LASSERT(osd_write_locked(env, obj));
3138 LASSERT(th != NULL);
3140 if (unlikely(fid_is_acct(fid)))
3141 /* Quota files can't be created from the kernel any more,
3142 * 'tune2fs -O quota' will take care of creating them */
3145 result = __osd_object_create(info, obj, attr, hint, dof, th);
3147 if (fid_is_idif(fid) &&
3148 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3149 struct lu_fid *tfid = &info->oti_fid;
3150 struct ost_id *oi = &info->oti_ostid;
3152 fid_to_ostid(fid, oi);
3153 ostid_to_fid(tfid, oi, 0);
3155 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3156 LMAC_FID_ON_OST, 0);
3158 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3160 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3161 on_ost ? LMAC_FID_ON_OST : 0,
3164 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3165 obj->oo_dt.do_body_ops = &osd_body_ops;
3169 result = __osd_oi_insert(env, obj, fid, th);
3171 /* a small optimization - dt_insert() isn't usually applied
3172 * to OST objects, so we don't need to cache OI mapping for
3174 if (result == 0 && on_ost == 0) {
3175 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3176 result = osd_idc_find_and_init(env, osd, obj);
3177 LASSERT(result == 0);
3180 LASSERT(ergo(result == 0,
3181 dt_object_exists(dt) && !dt_object_remote(dt)));
3182 LINVRNT(osd_invariant(obj));
3186 static int osd_declare_object_ref_add(const struct lu_env *env,
3187 struct dt_object *dt,
3188 struct thandle *handle)
3190 struct osd_thandle *oh;
3192 /* it's possible that object doesn't exist yet */
3193 LASSERT(handle != NULL);
3195 oh = container_of0(handle, struct osd_thandle, ot_super);
3196 LASSERT(oh->ot_handle == NULL);
3198 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3199 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3205 * Concurrency: @dt is write locked.
3207 static int osd_object_ref_add(const struct lu_env *env,
3208 struct dt_object *dt, struct thandle *th)
3210 struct osd_object *obj = osd_dt_obj(dt);
3211 struct inode *inode = obj->oo_inode;
3212 struct osd_thandle *oh;
3215 if (!dt_object_exists(dt) || obj->oo_destroyed)
3218 LINVRNT(osd_invariant(obj));
3219 LASSERT(!dt_object_remote(dt));
3220 LASSERT(osd_write_locked(env, obj));
3221 LASSERT(th != NULL);
3223 oh = container_of0(th, struct osd_thandle, ot_super);
3224 LASSERT(oh->ot_handle != NULL);
3226 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3228 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3229 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3231 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3232 * (65000) subdirectories by storing "1" in i_nlink if the link count
3233 * would otherwise overflow. Directory tranversal tools understand
3234 * that (st_nlink == 1) indicates that the filesystem dose not track
3235 * hard links count on the directory, and will not abort subdirectory
3236 * scanning early once (st_nlink - 2) subdirs have been found.
3238 * This also has to properly handle the case of inodes with nlink == 0
3239 * in case they are being linked into the PENDING directory
3241 spin_lock(&obj->oo_guard);
3242 if (unlikely(inode->i_nlink == 0))
3243 /* inc_nlink from 0 may cause WARN_ON */
3244 set_nlink(inode, 1);
3246 ldiskfs_inc_count(oh->ot_handle, inode);
3247 if (!S_ISDIR(inode->i_mode))
3248 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3250 spin_unlock(&obj->oo_guard);
3252 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3253 LINVRNT(osd_invariant(obj));
3255 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3260 static int osd_declare_object_ref_del(const struct lu_env *env,
3261 struct dt_object *dt,
3262 struct thandle *handle)
3264 struct osd_thandle *oh;
3266 if (!dt_object_exists(dt))
3269 LASSERT(!dt_object_remote(dt));
3270 LASSERT(handle != NULL);
3272 oh = container_of0(handle, struct osd_thandle, ot_super);
3273 LASSERT(oh->ot_handle == NULL);
3275 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3276 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3282 * Concurrency: @dt is write locked.
3284 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3287 struct osd_object *obj = osd_dt_obj(dt);
3288 struct inode *inode = obj->oo_inode;
3289 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3290 struct osd_thandle *oh;
3292 if (!dt_object_exists(dt))
3295 LINVRNT(osd_invariant(obj));
3296 LASSERT(!dt_object_remote(dt));
3297 LASSERT(osd_write_locked(env, obj));
3298 LASSERT(th != NULL);
3300 oh = container_of0(th, struct osd_thandle, ot_super);
3301 LASSERT(oh->ot_handle != NULL);
3303 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3305 spin_lock(&obj->oo_guard);
3306 /* That can be result of upgrade from old Lustre version and
3307 * applied only to local files. Just skip this ref_del call.
3308 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3309 if (inode->i_nlink == 0) {
3310 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3311 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3312 ", maybe an upgraded file? (LU-3915)\n",
3313 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3314 spin_unlock(&obj->oo_guard);
3318 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3319 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3321 ldiskfs_dec_count(oh->ot_handle, inode);
3322 spin_unlock(&obj->oo_guard);
3324 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3325 LINVRNT(osd_invariant(obj));
3327 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3333 * Get the 64-bit version for an inode.
3335 static int osd_object_version_get(const struct lu_env *env,
3336 struct dt_object *dt, dt_obj_version_t *ver)
3338 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3340 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3341 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3342 *ver = LDISKFS_I(inode)->i_fs_version;
3347 * Concurrency: @dt is read locked.
3349 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3350 struct lu_buf *buf, const char *name)
3352 struct osd_object *obj = osd_dt_obj(dt);
3353 struct inode *inode = obj->oo_inode;
3354 struct osd_thread_info *info = osd_oti_get(env);
3355 struct dentry *dentry = &info->oti_obj_dentry;
3357 /* version get is not real XATTR but uses xattr API */
3358 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3359 /* for version we are just using xattr API but change inode
3361 if (buf->lb_len == 0)
3362 return sizeof(dt_obj_version_t);
3364 if (buf->lb_len < sizeof(dt_obj_version_t))
3367 osd_object_version_get(env, dt, buf->lb_buf);
3369 return sizeof(dt_obj_version_t);
3372 if (!dt_object_exists(dt))
3375 LASSERT(!dt_object_remote(dt));
3376 LASSERT(inode->i_op != NULL);
3377 LASSERT(inode->i_op->getxattr != NULL);
3379 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3383 static int osd_declare_xattr_set(const struct lu_env *env,
3384 struct dt_object *dt,
3385 const struct lu_buf *buf, const char *name,
3386 int fl, struct thandle *handle)
3388 struct osd_thandle *oh;
3390 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3392 LASSERT(handle != NULL);
3394 oh = container_of0(handle, struct osd_thandle, ot_super);
3395 LASSERT(oh->ot_handle == NULL);
3397 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3398 /* For non-upgrading case, the LMA is set first and
3399 * usually fit inode. But for upgrade case, the LMA
3400 * may be in another separated EA block. */
3401 if (!dt_object_exists(dt))
3403 else if (fl == LU_XATTR_REPLACE)
3407 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3411 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3416 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3417 /* learn xattr size from osd_xattr_get if
3418 attribute has not been read yet */
3419 buflen = __osd_xattr_get(
3420 osd_dt_obj(dt)->oo_inode,
3421 &osd_oti_get(env)->oti_obj_dentry,
3426 buflen = buf->lb_len;
3429 if (buflen > sb->s_blocksize) {
3430 credits += osd_calc_bkmap_credits(
3432 (buflen + sb->s_blocksize - 1) >>
3433 sb->s_blocksize_bits);
3437 * xattr set may involve inode quota change, reserve credits for
3438 * dquot_initialize()
3440 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3443 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3449 * Set the 64-bit version for object
3451 static void osd_object_version_set(const struct lu_env *env,
3452 struct dt_object *dt,
3453 dt_obj_version_t *new_version)
3455 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3457 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3458 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3460 LDISKFS_I(inode)->i_fs_version = *new_version;
3461 /** Version is set after all inode operations are finished,
3462 * so we should mark it dirty here */
3463 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3467 * Concurrency: @dt is write locked.
3469 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3470 const struct lu_buf *buf, const char *name, int fl,
3471 struct thandle *handle)
3473 struct osd_object *obj = osd_dt_obj(dt);
3474 struct inode *inode = obj->oo_inode;
3475 struct osd_thread_info *info = osd_oti_get(env);
3480 LASSERT(handle != NULL);
3482 /* version set is not real XATTR */
3483 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3484 /* for version we are just using xattr API but change inode
3486 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3487 osd_object_version_set(env, dt, buf->lb_buf);
3488 return sizeof(dt_obj_version_t);
3491 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3492 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3494 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3495 if (fl & LU_XATTR_REPLACE)
3496 fs_flags |= XATTR_REPLACE;
3498 if (fl & LU_XATTR_CREATE)
3499 fs_flags |= XATTR_CREATE;
3501 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3502 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3505 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3509 lma->lma_incompat |= LMAI_STRIPED;
3510 lustre_lma_swab(lma);
3511 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3512 sizeof(*lma), XATTR_REPLACE);
3517 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3518 strcmp(name, XATTR_NAME_LINK) == 0)
3521 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3523 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3529 * Concurrency: @dt is read locked.
3531 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3532 const struct lu_buf *buf)
3534 struct osd_object *obj = osd_dt_obj(dt);
3535 struct inode *inode = obj->oo_inode;
3536 struct osd_thread_info *info = osd_oti_get(env);
3537 struct dentry *dentry = &info->oti_obj_dentry;
3539 if (!dt_object_exists(dt))
3542 LASSERT(!dt_object_remote(dt));
3543 LASSERT(inode->i_op != NULL);
3544 LASSERT(inode->i_op->listxattr != NULL);
3546 dentry->d_inode = inode;
3547 dentry->d_sb = inode->i_sb;
3548 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3551 static int osd_declare_xattr_del(const struct lu_env *env,
3552 struct dt_object *dt, const char *name,
3553 struct thandle *handle)
3555 struct osd_thandle *oh;
3556 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3558 LASSERT(!dt_object_remote(dt));
3559 LASSERT(handle != NULL);
3561 oh = container_of0(handle, struct osd_thandle, ot_super);
3562 LASSERT(oh->ot_handle == NULL);
3564 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3565 osd_dto_credits_noquota[DTO_XATTR_SET]);
3567 * xattr del may involve inode quota change, reserve credits for
3568 * dquot_initialize()
3570 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3576 * Concurrency: @dt is write locked.
3578 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3579 const char *name, struct thandle *handle)
3581 struct osd_object *obj = osd_dt_obj(dt);
3582 struct inode *inode = obj->oo_inode;
3583 struct osd_thread_info *info = osd_oti_get(env);
3584 struct dentry *dentry = &info->oti_obj_dentry;
3587 if (!dt_object_exists(dt))
3590 LASSERT(!dt_object_remote(dt));
3591 LASSERT(inode->i_op != NULL);
3592 LASSERT(inode->i_op->removexattr != NULL);
3593 LASSERT(handle != NULL);
3595 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3597 ll_vfs_dq_init(inode);
3598 dentry->d_inode = inode;
3599 dentry->d_sb = inode->i_sb;
3600 rc = inode->i_op->removexattr(dentry, name);
3601 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3605 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3606 __u64 start, __u64 end)
3608 struct osd_object *obj = osd_dt_obj(dt);
3609 struct inode *inode = obj->oo_inode;
3610 struct osd_thread_info *info = osd_oti_get(env);
3611 struct dentry *dentry = &info->oti_obj_dentry;
3612 struct file *file = &info->oti_file;
3617 dentry->d_inode = inode;
3618 dentry->d_sb = inode->i_sb;
3619 file->f_path.dentry = dentry;
3620 file->f_mapping = inode->i_mapping;
3621 file->f_op = inode->i_fop;
3622 set_file_inode(file, inode);
3624 rc = ll_vfs_fsync_range(file, start, end, 0);
3633 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3634 const struct dt_index_features *feat)
3636 struct iam_descr *descr;
3638 if (osd_object_is_root(o))
3639 return feat == &dt_directory_features;
3641 LASSERT(o->oo_dir != NULL);
3643 descr = o->oo_dir->od_container.ic_descr;
3644 if (feat == &dt_directory_features) {
3645 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3651 feat->dif_keysize_min <= descr->id_key_size &&
3652 descr->id_key_size <= feat->dif_keysize_max &&
3653 feat->dif_recsize_min <= descr->id_rec_size &&
3654 descr->id_rec_size <= feat->dif_recsize_max &&
3655 !(feat->dif_flags & (DT_IND_VARKEY |
3656 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3657 ergo(feat->dif_flags & DT_IND_UPDATE,
3658 1 /* XXX check that object (and file system) is
3663 static int osd_iam_container_init(const struct lu_env *env,
3664 struct osd_object *obj,
3665 struct osd_directory *dir)
3667 struct iam_container *bag = &dir->od_container;
3670 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3674 result = iam_container_setup(bag);
3676 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3678 iam_container_fini(bag);
3685 * Concurrency: no external locking is necessary.
3687 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3688 const struct dt_index_features *feat)
3692 struct osd_object *obj = osd_dt_obj(dt);
3694 LINVRNT(osd_invariant(obj));
3696 if (osd_object_is_root(obj)) {
3697 dt->do_index_ops = &osd_index_ea_ops;
3699 } else if (feat == &dt_directory_features) {
3700 dt->do_index_ops = &osd_index_ea_ops;
3701 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
3706 } else if (unlikely(feat == &dt_otable_features)) {
3707 dt->do_index_ops = &osd_otable_ops;
3709 } else if (unlikely(feat == &dt_acct_features)) {
3710 dt->do_index_ops = &osd_acct_index_ops;
3713 } else if (!osd_has_index(obj)) {
3714 struct osd_directory *dir;
3719 spin_lock(&obj->oo_guard);
3720 if (obj->oo_dir == NULL)
3724 * Concurrent thread allocated container data.
3727 spin_unlock(&obj->oo_guard);
3729 * Now, that we have container data, serialize its
3732 down_write(&obj->oo_ext_idx_sem);
3734 * recheck under lock.
3736 if (!osd_has_index(obj))
3737 result = osd_iam_container_init(env, obj,
3741 up_write(&obj->oo_ext_idx_sem);
3749 if (result == 0 && skip_iam == 0) {
3750 if (!osd_iam_index_probe(env, obj, feat))
3753 LINVRNT(osd_invariant(obj));
3755 if (result == 0 && feat == &dt_quota_glb_features &&
3756 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3757 result = osd_quota_migration(env, dt);
3762 static int osd_otable_it_attr_get(const struct lu_env *env,
3763 struct dt_object *dt,
3764 struct lu_attr *attr)
3770 static const struct dt_object_operations osd_obj_ops = {
3771 .do_read_lock = osd_object_read_lock,
3772 .do_write_lock = osd_object_write_lock,
3773 .do_read_unlock = osd_object_read_unlock,
3774 .do_write_unlock = osd_object_write_unlock,
3775 .do_write_locked = osd_object_write_locked,
3776 .do_attr_get = osd_attr_get,
3777 .do_declare_attr_set = osd_declare_attr_set,
3778 .do_attr_set = osd_attr_set,
3779 .do_ah_init = osd_ah_init,
3780 .do_declare_create = osd_declare_object_create,
3781 .do_create = osd_object_create,
3782 .do_declare_destroy = osd_declare_object_destroy,
3783 .do_destroy = osd_object_destroy,
3784 .do_index_try = osd_index_try,
3785 .do_declare_ref_add = osd_declare_object_ref_add,
3786 .do_ref_add = osd_object_ref_add,
3787 .do_declare_ref_del = osd_declare_object_ref_del,
3788 .do_ref_del = osd_object_ref_del,
3789 .do_xattr_get = osd_xattr_get,
3790 .do_declare_xattr_set = osd_declare_xattr_set,
3791 .do_xattr_set = osd_xattr_set,
3792 .do_declare_xattr_del = osd_declare_xattr_del,
3793 .do_xattr_del = osd_xattr_del,
3794 .do_xattr_list = osd_xattr_list,
3795 .do_object_sync = osd_object_sync,
3799 * dt_object_operations for interoperability mode
3800 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3802 static const struct dt_object_operations osd_obj_ea_ops = {
3803 .do_read_lock = osd_object_read_lock,
3804 .do_write_lock = osd_object_write_lock,
3805 .do_read_unlock = osd_object_read_unlock,
3806 .do_write_unlock = osd_object_write_unlock,
3807 .do_write_locked = osd_object_write_locked,
3808 .do_attr_get = osd_attr_get,
3809 .do_declare_attr_set = osd_declare_attr_set,
3810 .do_attr_set = osd_attr_set,
3811 .do_ah_init = osd_ah_init,
3812 .do_declare_create = osd_declare_object_create,
3813 .do_create = osd_object_ea_create,
3814 .do_declare_destroy = osd_declare_object_destroy,
3815 .do_destroy = osd_object_destroy,
3816 .do_index_try = osd_index_try,
3817 .do_declare_ref_add = osd_declare_object_ref_add,
3818 .do_ref_add = osd_object_ref_add,
3819 .do_declare_ref_del = osd_declare_object_ref_del,
3820 .do_ref_del = osd_object_ref_del,
3821 .do_xattr_get = osd_xattr_get,
3822 .do_declare_xattr_set = osd_declare_xattr_set,
3823 .do_xattr_set = osd_xattr_set,
3824 .do_declare_xattr_del = osd_declare_xattr_del,
3825 .do_xattr_del = osd_xattr_del,
3826 .do_xattr_list = osd_xattr_list,
3827 .do_object_sync = osd_object_sync,
3830 static const struct dt_object_operations osd_obj_otable_it_ops = {
3831 .do_attr_get = osd_otable_it_attr_get,
3832 .do_index_try = osd_index_try,
3835 static int osd_index_declare_iam_delete(const struct lu_env *env,
3836 struct dt_object *dt,
3837 const struct dt_key *key,
3838 struct thandle *handle)
3840 struct osd_thandle *oh;
3842 oh = container_of0(handle, struct osd_thandle, ot_super);
3843 LASSERT(oh->ot_handle == NULL);
3845 /* Recycle may cause additional three blocks to be changed. */
3846 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3847 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3853 * delete a (key, value) pair from index \a dt specified by \a key
3855 * \param dt osd index object
3856 * \param key key for index
3857 * \param rec record reference
3858 * \param handle transaction handler
3861 * \retval -ve failure
3863 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3864 const struct dt_key *key,
3865 struct thandle *handle)
3867 struct osd_thread_info *oti = osd_oti_get(env);
3868 struct osd_object *obj = osd_dt_obj(dt);
3869 struct osd_thandle *oh;
3870 struct iam_path_descr *ipd;
3871 struct iam_container *bag = &obj->oo_dir->od_container;
3875 if (!dt_object_exists(dt))
3878 LINVRNT(osd_invariant(obj));
3879 LASSERT(!dt_object_remote(dt));
3880 LASSERT(bag->ic_object == obj->oo_inode);
3881 LASSERT(handle != NULL);
3883 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3885 ipd = osd_idx_ipd_get(env, bag);
3886 if (unlikely(ipd == NULL))
3889 oh = container_of0(handle, struct osd_thandle, ot_super);
3890 LASSERT(oh->ot_handle != NULL);
3891 LASSERT(oh->ot_handle->h_transaction != NULL);
3893 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3894 /* swab quota uid/gid provided by caller */
3895 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3896 key = (const struct dt_key *)&oti->oti_quota_id;
3899 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3900 osd_ipd_put(env, bag, ipd);
3901 LINVRNT(osd_invariant(obj));
3902 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3906 static int osd_index_declare_ea_delete(const struct lu_env *env,
3907 struct dt_object *dt,
3908 const struct dt_key *key,
3909 struct thandle *handle)
3911 struct osd_thandle *oh;
3912 struct inode *inode;
3916 LASSERT(!dt_object_remote(dt));
3917 LASSERT(handle != NULL);
3919 oh = container_of0(handle, struct osd_thandle, ot_super);
3920 LASSERT(oh->ot_handle == NULL);
3922 /* due to DNE we may need to remove an agent inode */
3923 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3924 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3925 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3927 inode = osd_dt_obj(dt)->oo_inode;
3931 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3932 0, oh, osd_dt_obj(dt), true, NULL, false);
3936 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3939 struct osd_fid_pack *rec;
3942 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3943 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3944 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3945 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3951 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3952 const struct lu_fid *fid)
3954 struct seq_server_site *ss = osd_seq_site(osd);
3957 /* FID seqs not in FLDB, must be local seq */
3958 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3961 /* If FLD is not being initialized yet, it only happens during the
3962 * initialization, likely during mgs initialization, and we assume
3963 * this is local FID. */
3964 if (ss == NULL || ss->ss_server_fld == NULL)
3967 /* Only check the local FLDB here */
3968 if (osd_seq_exists(env, osd, fid_seq(fid)))
3975 * Index delete function for interoperability mode (b11826).
3976 * It will remove the directory entry added by osd_index_ea_insert().
3977 * This entry is needed to maintain name->fid mapping.
3979 * \param key, key i.e. file entry to be deleted
3981 * \retval 0, on success
3982 * \retval -ve, on error
3984 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3985 const struct dt_key *key, struct thandle *handle)
3987 struct osd_object *obj = osd_dt_obj(dt);
3988 struct inode *dir = obj->oo_inode;
3989 struct dentry *dentry;
3990 struct osd_thandle *oh;
3991 struct ldiskfs_dir_entry_2 *de = NULL;
3992 struct buffer_head *bh;
3993 struct htree_lock *hlock = NULL;
3994 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3995 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3999 if (!dt_object_exists(dt))
4002 LINVRNT(osd_invariant(obj));
4003 LASSERT(!dt_object_remote(dt));
4004 LASSERT(handle != NULL);
4006 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4008 oh = container_of(handle, struct osd_thandle, ot_super);
4009 LASSERT(oh->ot_handle != NULL);
4010 LASSERT(oh->ot_handle->h_transaction != NULL);
4012 ll_vfs_dq_init(dir);
4013 dentry = osd_child_dentry_get(env, obj,
4014 (char *)key, strlen((char *)key));
4016 if (obj->oo_hl_head != NULL) {
4017 hlock = osd_oti_get(env)->oti_hlock;
4018 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4019 dir, LDISKFS_HLOCK_DEL);
4021 down_write(&obj->oo_ext_idx_sem);
4024 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4026 /* If this is not the ".." entry, it might be a remote DNE
4027 * entry and we need to check if the FID is for a remote
4028 * MDT. If the FID is not in the directory entry (e.g.
4029 * upgraded 1.8 filesystem without dirdata enabled) then
4030 * we need to get the FID from the LMA. For a remote directory
4031 * there HAS to be an LMA, it cannot be an IGIF inode in this
4034 * Delete the entry before the agent inode in order to
4035 * simplify error handling. At worst an error after deleting
4036 * the entry first might leak the agent inode afterward. The
4037 * reverse would need filesystem abort in case of error deleting
4038 * the entry after the agent had been removed, or leave a
4039 * dangling entry pointing at a random inode. */
4040 if (strcmp((char *)key, dotdot) != 0) {
4041 LASSERT(de != NULL);
4042 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4043 if (rc == -ENODATA) {
4044 /* can't get FID, postpone to the end of the
4045 * transaction when iget() is safe */
4046 osd_schedule_agent_inode_removal(env, oh,
4047 le32_to_cpu(de->inode));
4048 } else if (rc == 0 &&
4049 unlikely(osd_remote_fid(env, osd, fid))) {
4050 osd_schedule_agent_inode_removal(env, oh,
4051 le32_to_cpu(de->inode));
4054 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4060 ldiskfs_htree_unlock(hlock);
4062 up_write(&obj->oo_ext_idx_sem);
4067 /* For inode on the remote MDT, .. will point to
4068 * /Agent directory, Check whether it needs to delete
4069 * from agent directory */
4070 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4071 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
4073 if (rc != 0 && rc != -ENOENT) {
4074 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4075 osd_name(osd), PFID(fid), rc);
4085 LASSERT(osd_invariant(obj));
4086 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4091 * Lookup index for \a key and copy record to \a rec.
4093 * \param dt osd index object
4094 * \param key key for index
4095 * \param rec record reference
4097 * \retval +ve success : exact mach
4098 * \retval 0 return record with key not greater than \a key
4099 * \retval -ve failure
4101 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4102 struct dt_rec *rec, const struct dt_key *key)
4104 struct osd_object *obj = osd_dt_obj(dt);
4105 struct iam_path_descr *ipd;
4106 struct iam_container *bag = &obj->oo_dir->od_container;
4107 struct osd_thread_info *oti = osd_oti_get(env);
4108 struct iam_iterator *it = &oti->oti_idx_it;
4109 struct iam_rec *iam_rec;
4113 if (!dt_object_exists(dt))
4116 LASSERT(osd_invariant(obj));
4117 LASSERT(!dt_object_remote(dt));
4118 LASSERT(bag->ic_object == obj->oo_inode);
4120 ipd = osd_idx_ipd_get(env, bag);
4124 /* got ipd now we can start iterator. */
4125 iam_it_init(it, bag, 0, ipd);
4127 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4128 /* swab quota uid/gid provided by caller */
4129 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4130 key = (const struct dt_key *)&oti->oti_quota_id;
4133 rc = iam_it_get(it, (struct iam_key *)key);
4135 if (S_ISDIR(obj->oo_inode->i_mode))
4136 iam_rec = (struct iam_rec *)oti->oti_ldp;
4138 iam_rec = (struct iam_rec *) rec;
4140 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4142 if (S_ISDIR(obj->oo_inode->i_mode))
4143 osd_fid_unpack((struct lu_fid *) rec,
4144 (struct osd_fid_pack *)iam_rec);
4145 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4146 osd_quota_unpack(obj, rec);
4151 osd_ipd_put(env, bag, ipd);
4153 LINVRNT(osd_invariant(obj));
4158 static int osd_index_declare_iam_insert(const struct lu_env *env,
4159 struct dt_object *dt,
4160 const struct dt_rec *rec,
4161 const struct dt_key *key,
4162 struct thandle *handle)
4164 struct osd_thandle *oh;
4166 LASSERT(handle != NULL);
4168 oh = container_of0(handle, struct osd_thandle, ot_super);
4169 LASSERT(oh->ot_handle == NULL);
4171 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4172 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4178 * Inserts (key, value) pair in \a dt index object.
4180 * \param dt osd index object
4181 * \param key key for index
4182 * \param rec record reference
4183 * \param th transaction handler
4186 * \retval -ve failure
4188 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4189 const struct dt_rec *rec,
4190 const struct dt_key *key, struct thandle *th,
4193 struct osd_object *obj = osd_dt_obj(dt);
4194 struct iam_path_descr *ipd;
4195 struct osd_thandle *oh;
4196 struct iam_container *bag;
4197 struct osd_thread_info *oti = osd_oti_get(env);
4198 struct iam_rec *iam_rec;
4202 if (!dt_object_exists(dt))
4205 LINVRNT(osd_invariant(obj));
4206 LASSERT(!dt_object_remote(dt));
4208 bag = &obj->oo_dir->od_container;
4209 LASSERT(bag->ic_object == obj->oo_inode);
4210 LASSERT(th != NULL);
4212 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4214 ipd = osd_idx_ipd_get(env, bag);
4215 if (unlikely(ipd == NULL))
4218 oh = container_of0(th, struct osd_thandle, ot_super);
4219 LASSERT(oh->ot_handle != NULL);
4220 LASSERT(oh->ot_handle->h_transaction != NULL);
4221 if (S_ISDIR(obj->oo_inode->i_mode)) {
4222 iam_rec = (struct iam_rec *)oti->oti_ldp;
4223 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4224 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4225 /* pack quota uid/gid */
4226 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4227 key = (const struct dt_key *)&oti->oti_quota_id;
4228 /* pack quota record */
4229 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4230 iam_rec = (struct iam_rec *)rec;
4232 iam_rec = (struct iam_rec *)rec;
4235 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4237 osd_ipd_put(env, bag, ipd);
4238 LINVRNT(osd_invariant(obj));
4239 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4244 * Calls ldiskfs_add_entry() to add directory entry
4245 * into the directory. This is required for
4246 * interoperability mode (b11826)
4248 * \retval 0, on success
4249 * \retval -ve, on error
4251 static int __osd_ea_add_rec(struct osd_thread_info *info,
4252 struct osd_object *pobj, struct inode *cinode,
4253 const char *name, const struct lu_fid *fid,
4254 struct htree_lock *hlock, struct thandle *th)
4256 struct ldiskfs_dentry_param *ldp;
4257 struct dentry *child;
4258 struct osd_thandle *oth;
4261 oth = container_of(th, struct osd_thandle, ot_super);
4262 LASSERT(oth->ot_handle != NULL);
4263 LASSERT(oth->ot_handle->h_transaction != NULL);
4264 LASSERT(pobj->oo_inode);
4266 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4267 if (unlikely(pobj->oo_inode ==
4268 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4271 osd_get_ldiskfs_dirent_param(ldp, fid);
4272 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4273 child->d_fsdata = (void *)ldp;
4274 ll_vfs_dq_init(pobj->oo_inode);
4275 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4277 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4278 struct ldiskfs_dir_entry_2 *de;
4279 struct buffer_head *bh;
4282 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4285 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4288 if (S_ISDIR(cinode->i_mode))
4289 de->file_type = LDISKFS_DIRENT_LUFID |
4290 LDISKFS_FT_REG_FILE;
4292 de->file_type = LDISKFS_DIRENT_LUFID |
4294 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4305 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4306 * into the directory.Also sets flags into osd object to
4307 * indicate dot and dotdot are created. This is required for
4308 * interoperability mode (b11826)
4310 * \param dir directory for dot and dotdot fixup.
4311 * \param obj child object for linking
4313 * \retval 0, on success
4314 * \retval -ve, on error
4316 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4317 struct osd_object *dir,
4318 struct inode *parent_dir, const char *name,
4319 const struct lu_fid *dot_fid,
4320 const struct lu_fid *dot_dot_fid,
4323 struct inode *inode = dir->oo_inode;
4324 struct osd_thandle *oth;
4327 oth = container_of(th, struct osd_thandle, ot_super);
4328 LASSERT(oth->ot_handle->h_transaction != NULL);
4329 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4331 if (strcmp(name, dot) == 0) {
4332 if (dir->oo_compat_dot_created) {
4335 LASSERT(inode->i_ino == parent_dir->i_ino);
4336 dir->oo_compat_dot_created = 1;
4339 } else if (strcmp(name, dotdot) == 0) {
4340 if (!dir->oo_compat_dot_created)
4342 /* in case of rename, dotdot is already created */
4343 if (dir->oo_compat_dotdot_created) {
4344 return __osd_ea_add_rec(info, dir, parent_dir, name,
4345 dot_dot_fid, NULL, th);
4348 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4349 struct lu_fid tfid = *dot_dot_fid;
4352 result = osd_add_dot_dotdot_internal(info,
4353 dir->oo_inode, parent_dir, dot_fid,
4356 result = osd_add_dot_dotdot_internal(info,
4357 dir->oo_inode, parent_dir, dot_fid,
4362 dir->oo_compat_dotdot_created = 1;
4370 * It will call the appropriate osd_add* function and return the
4371 * value, return by respective functions.
4373 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4374 struct inode *cinode, const char *name,
4375 const struct lu_fid *fid, struct thandle *th)
4377 struct osd_thread_info *info = osd_oti_get(env);
4378 struct htree_lock *hlock;
4381 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4383 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4385 if (hlock != NULL) {
4386 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4389 down_write(&pobj->oo_ext_idx_sem);
4392 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4393 lu_object_fid(&pobj->oo_dt.do_lu),
4396 if (hlock != NULL) {
4397 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4398 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4400 down_write(&pobj->oo_ext_idx_sem);
4403 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4404 struct lu_fid *tfid = &info->oti_fid;
4408 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4411 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4416 ldiskfs_htree_unlock(hlock);
4418 up_write(&pobj->oo_ext_idx_sem);
4424 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4425 struct osd_idmap_cache *oic)
4427 struct osd_scrub *scrub = &dev->od_scrub;
4428 struct lu_fid *fid = &oic->oic_fid;
4429 struct osd_inode_id *id = &oti->oti_id;
4434 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4437 if (scrub->os_pos_current > id->oii_ino)
4441 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4442 if (rc == -ENOENT) {
4443 struct inode *inode;
4446 inode = osd_iget(oti, dev, &oic->oic_lid);
4448 /* The inode has been removed (by race maybe). */
4449 if (IS_ERR(inode)) {
4450 rc = PTR_ERR(inode);
4452 RETURN(rc == -ESTALE ? -ENOENT : rc);
4456 /* The OI mapping is lost. */
4457 if (id->oii_gen != OSD_OII_NOGEN)
4460 /* The inode may has been reused by others, we do not know,
4461 * leave it to be handled by subsequent osd_fid_lookup(). */
4463 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4468 if (thread_is_running(&scrub->os_thread)) {
4469 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4470 /* There is race condition between osd_oi_lookup and OI scrub.
4471 * The OI scrub finished just after osd_oi_lookup() failure.
4472 * Under such case, it is unnecessary to trigger OI scrub again,
4473 * but try to call osd_oi_lookup() again. */
4474 if (unlikely(rc == -EAGAIN))
4480 if (!dev->od_noscrub && ++once == 1) {
4481 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4483 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4484 "for "DFID", rc = %d [2]\n",
4485 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4487 if (rc == 0 || rc == -EALREADY)
4494 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4495 struct osd_device *dev,
4496 struct osd_idmap_cache *oic,
4497 struct lu_fid *fid, __u32 ino)
4499 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4500 struct inode *inode;
4503 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4504 inode = osd_iget(oti, dev, &oic->oic_lid);
4505 if (IS_ERR(inode)) {
4506 fid_zero(&oic->oic_fid);
4507 return PTR_ERR(inode);
4510 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4513 fid_zero(&oic->oic_fid);
4515 *fid = oic->oic_fid = lma->lma_self_fid;
4519 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4520 struct osd_inode_id *id, const struct lu_fid *fid)
4522 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4523 id->oii_ino, id->oii_gen, info);
4524 info->oti_cache.oic_lid = *id;
4525 info->oti_cache.oic_fid = *fid;
4526 info->oti_cache.oic_dev = osd;
4530 * Get parent FID from the linkEA.
4532 * For a directory which parent resides on remote MDT, to satisfy the
4533 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4534 * the other hand, to make the lookup(..) on the directory can return
4535 * the real parent FID, we append the real parent FID after its ".."
4536 * name entry in the /REMOTE_PARENT_DIR.
4538 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4539 * backup. So after the restore, we cannot get the right parent FID from
4540 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4541 * we have stored the real parent FID in the directory object's linkEA,
4542 * we can parse the linkEA for the real parent FID.
4544 * \param[in] env pointer to the thread context
4545 * \param[in] obj pointer to the object to be handled
4546 * \param[out]fid pointer to the buffer to hold the parent FID
4548 * \retval 0 for getting the real parent FID successfully
4549 * \retval negative error number on failure
4551 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4552 struct osd_object *obj,
4555 struct osd_thread_info *oti = osd_oti_get(env);
4556 struct lu_buf *buf = &oti->oti_big_buf;
4557 struct dentry *dentry = &oti->oti_obj_dentry;
4558 struct inode *inode = obj->oo_inode;
4559 struct linkea_data ldata = { NULL };
4564 if (!S_ISDIR(inode->i_mode))
4568 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4569 buf->lb_buf, buf->lb_len);
4570 if (rc == -ERANGE) {
4571 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4574 lu_buf_realloc(buf, rc);
4575 if (buf->lb_buf == NULL)
4582 if (unlikely(rc == 0))
4588 if (unlikely(buf->lb_buf == NULL)) {
4589 lu_buf_realloc(buf, rc);
4590 if (buf->lb_buf == NULL)
4597 rc = linkea_init(&ldata);
4599 linkea_first_entry(&ldata);
4600 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4607 * Calls ->lookup() to find dentry. From dentry get inode and
4608 * read inode's ea to get fid. This is required for interoperability
4611 * \retval 0, on success
4612 * \retval -ve, on error
4614 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4615 struct dt_rec *rec, const struct dt_key *key)
4617 struct inode *dir = obj->oo_inode;
4618 struct dentry *dentry;
4619 struct ldiskfs_dir_entry_2 *de;
4620 struct buffer_head *bh;
4621 struct lu_fid *fid = (struct lu_fid *) rec;
4622 struct htree_lock *hlock = NULL;
4627 LASSERT(dir->i_op != NULL);
4628 LASSERT(dir->i_op->lookup != NULL);
4630 dentry = osd_child_dentry_get(env, obj,
4631 (char *)key, strlen((char *)key));
4633 if (obj->oo_hl_head != NULL) {
4634 hlock = osd_oti_get(env)->oti_hlock;
4635 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4636 dir, LDISKFS_HLOCK_LOOKUP);
4638 down_read(&obj->oo_ext_idx_sem);
4641 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4643 struct osd_thread_info *oti = osd_oti_get(env);
4644 struct osd_inode_id *id = &oti->oti_id;
4645 struct osd_idmap_cache *oic = &oti->oti_cache;
4646 struct osd_device *dev = osd_obj2dev(obj);
4648 ino = le32_to_cpu(de->inode);
4649 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4651 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4655 rc = osd_get_fid_from_dentry(de, rec);
4657 /* done with de, release bh */
4660 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4661 const char *name = (const char *)key;
4663 /* If the parent is on remote MDT, and there
4664 * is no FID-in-dirent, then we have to get
4665 * the parent FID from the linkEA. */
4666 if (likely(strlen(name) == 2 &&
4667 name[0] == '.' && name[1] == '.'))
4668 rc = osd_get_pfid_from_linkea(env, obj,
4671 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4674 osd_id_gen(id, ino, OSD_OII_NOGEN);
4677 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4678 fid_zero(&oic->oic_fid);
4683 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4684 rc = osd_consistency_check(oti, dev, oic);
4686 fid_zero(&oic->oic_fid);
4695 ldiskfs_htree_unlock(hlock);
4697 up_read(&obj->oo_ext_idx_sem);
4702 * Put the osd object once done with it.
4704 * \param obj osd object that needs to be put
4706 static inline void osd_object_put(const struct lu_env *env,
4707 struct osd_object *obj)
4709 lu_object_put(env, &obj->oo_dt.do_lu);
4712 static int osd_index_declare_ea_insert(const struct lu_env *env,
4713 struct dt_object *dt,
4714 const struct dt_rec *rec,
4715 const struct dt_key *key,
4716 struct thandle *handle)
4718 struct osd_thandle *oh;
4719 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4720 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4721 const struct lu_fid *fid = rec1->rec_fid;
4722 int credits, rc = 0;
4723 struct osd_idmap_cache *idc;
4726 LASSERT(!dt_object_remote(dt));
4727 LASSERT(handle != NULL);
4728 LASSERT(fid != NULL);
4729 LASSERT(rec1->rec_type != 0);
4731 oh = container_of0(handle, struct osd_thandle, ot_super);
4732 LASSERT(oh->ot_handle == NULL);
4734 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4736 /* we can't call iget() while a transactions is running
4737 * (this can lead to a deadlock), but we need to know
4738 * inum and object type. so we find this information at
4739 * declaration and cache in per-thread info */
4740 idc = osd_idc_find_or_init(env, osd, fid);
4742 RETURN(PTR_ERR(idc));
4743 if (idc->oic_remote) {
4744 /* a reference to remote inode is represented by an
4745 * agent inode which we have to create */
4746 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4747 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4750 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4752 if (osd_dt_obj(dt)->oo_inode != NULL) {
4753 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4755 /* We ignore block quota on meta pool (MDTs), so needn't
4756 * calculate how many blocks will be consumed by this index
4758 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4759 i_gid_read(inode), 0, oh,
4760 osd_dt_obj(dt), true, NULL, false);
4767 * Index add function for interoperability mode (b11826).
4768 * It will add the directory entry.This entry is needed to
4769 * maintain name->fid mapping.
4771 * \param key it is key i.e. file entry to be inserted
4772 * \param rec it is value of given key i.e. fid
4774 * \retval 0, on success
4775 * \retval -ve, on error
4777 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4778 const struct dt_rec *rec,
4779 const struct dt_key *key, struct thandle *th,
4782 struct osd_object *obj = osd_dt_obj(dt);
4783 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4784 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4785 const struct lu_fid *fid = rec1->rec_fid;
4786 const char *name = (const char *)key;
4787 struct osd_thread_info *oti = osd_oti_get(env);
4788 struct inode *child_inode = NULL;
4789 struct osd_idmap_cache *idc;
4793 if (!dt_object_exists(dt))
4796 LASSERT(osd_invariant(obj));
4797 LASSERT(!dt_object_remote(dt));
4798 LASSERT(th != NULL);
4800 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4802 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4804 idc = osd_idc_find(env, osd, fid);
4805 if (unlikely(idc == NULL)) {
4806 /* this dt_insert() wasn't declared properly, so
4807 * FID is missing in OI cache. we better do not
4808 * lookup FID in FLDB/OI and don't risk to deadlock,
4809 * but in some special cases (lfsck testing, etc)
4810 * it's much simpler than fixing a caller */
4811 CERROR("%s: "DFID" wasn't declared for insert\n",
4812 osd_name(osd), PFID(fid));
4814 idc = osd_idc_find_or_init(env, osd, fid);
4816 RETURN(PTR_ERR(idc));
4819 if (idc->oic_remote) {
4820 /* Insert remote entry */
4821 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4822 struct osd_mdobj_map *omm = osd->od_mdt_map;
4823 struct osd_thandle *oh;
4825 /* If parent on remote MDT, we need put this object
4827 oh = container_of(th, typeof(*oh), ot_super);
4828 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4830 CERROR("%s: add "DFID" error: rc = %d\n",
4832 PFID(lu_object_fid(&dt->do_lu)), rc);
4836 child_inode = igrab(omm->omm_remote_parent->d_inode);
4838 child_inode = osd_create_local_agent_inode(env, osd,
4839 obj, fid, rec1->rec_type & S_IFMT, th);
4840 if (IS_ERR(child_inode))
4841 RETURN(PTR_ERR(child_inode));
4844 /* Insert local entry */
4845 if (unlikely(idc->oic_lid.oii_ino == 0)) {
4846 /* for a reason OI cache wasn't filled properly */
4847 CERROR("%s: OIC for "DFID" isn't filled\n",
4848 osd_name(osd), PFID(fid));
4851 child_inode = oti->oti_inode;
4852 if (unlikely(child_inode == NULL)) {
4853 struct ldiskfs_inode_info *lii;
4857 child_inode = oti->oti_inode = &lii->vfs_inode;
4859 child_inode->i_sb = osd_sb(osd);
4860 child_inode->i_ino = idc->oic_lid.oii_ino;
4861 child_inode->i_mode = rec1->rec_type & S_IFMT;
4864 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4866 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4867 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4869 if (child_inode && child_inode != oti->oti_inode)
4871 LASSERT(osd_invariant(obj));
4872 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4877 * Initialize osd Iterator for given osd index object.
4879 * \param dt osd index object
4882 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4883 struct dt_object *dt,
4886 struct osd_it_iam *it;
4887 struct osd_object *obj = osd_dt_obj(dt);
4888 struct lu_object *lo = &dt->do_lu;
4889 struct iam_path_descr *ipd;
4890 struct iam_container *bag = &obj->oo_dir->od_container;
4892 if (!dt_object_exists(dt))
4893 return ERR_PTR(-ENOENT);
4897 return ERR_PTR(-ENOMEM);
4899 ipd = osd_it_ipd_get(env, bag);
4900 if (likely(ipd != NULL)) {
4904 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4905 return (struct dt_it *)it;
4908 return ERR_PTR(-ENOMEM);
4913 * free given Iterator.
4916 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4918 struct osd_it_iam *it = (struct osd_it_iam *)di;
4919 struct osd_object *obj = it->oi_obj;
4921 iam_it_fini(&it->oi_it);
4922 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4923 lu_object_put(env, &obj->oo_dt.do_lu);
4928 * Move Iterator to record specified by \a key
4930 * \param di osd iterator
4931 * \param key key for index
4933 * \retval +ve di points to record with least key not larger than key
4934 * \retval 0 di points to exact matched key
4935 * \retval -ve failure
4938 static int osd_it_iam_get(const struct lu_env *env,
4939 struct dt_it *di, const struct dt_key *key)
4941 struct osd_thread_info *oti = osd_oti_get(env);
4942 struct osd_it_iam *it = (struct osd_it_iam *)di;
4944 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4945 /* swab quota uid/gid */
4946 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4947 key = (struct dt_key *)&oti->oti_quota_id;
4950 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4956 * \param di osd iterator
4958 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4960 struct osd_it_iam *it = (struct osd_it_iam *)di;
4962 iam_it_put(&it->oi_it);
4966 * Move iterator by one record
4968 * \param di osd iterator
4970 * \retval +1 end of container reached
4972 * \retval -ve failure
4975 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4977 struct osd_it_iam *it = (struct osd_it_iam *)di;
4979 return iam_it_next(&it->oi_it);
4983 * Return pointer to the key under iterator.
4986 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4987 const struct dt_it *di)
4989 struct osd_thread_info *oti = osd_oti_get(env);
4990 struct osd_it_iam *it = (struct osd_it_iam *)di;
4991 struct osd_object *obj = it->oi_obj;
4994 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4996 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4997 /* swab quota uid/gid */
4998 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4999 key = (struct dt_key *)&oti->oti_quota_id;
5006 * Return size of key under iterator (in bytes)
5009 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5011 struct osd_it_iam *it = (struct osd_it_iam *)di;
5013 return iam_it_key_size(&it->oi_it);
5017 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5019 /* check if file type is required */
5020 if (ent->lde_attrs & LUDA_TYPE) {
5021 struct luda_type *lt;
5022 int align = sizeof(*lt) - 1;
5024 len = (len + align) & ~align;
5025 lt = (struct luda_type *)(ent->lde_name + len);
5026 lt->lt_type = cpu_to_le16(DTTOIF(type));
5029 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5033 * build lu direct from backend fs dirent.
5037 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5038 char *name, __u16 namelen, __u16 type, __u32 attr)
5040 ent->lde_attrs = attr | LUDA_FID;
5041 fid_cpu_to_le(&ent->lde_fid, fid);
5043 ent->lde_hash = cpu_to_le64(offset);
5044 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5046 strncpy(ent->lde_name, name, namelen);
5047 ent->lde_name[namelen] = '\0';
5048 ent->lde_namelen = cpu_to_le16(namelen);
5050 /* append lustre attributes */
5051 osd_it_append_attrs(ent, namelen, type);
5055 * Return pointer to the record under iterator.
5057 static int osd_it_iam_rec(const struct lu_env *env,
5058 const struct dt_it *di,
5059 struct dt_rec *dtrec, __u32 attr)
5061 struct osd_it_iam *it = (struct osd_it_iam *)di;
5062 struct osd_thread_info *info = osd_oti_get(env);
5065 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5066 const struct osd_fid_pack *rec;
5067 struct lu_fid *fid = &info->oti_fid;
5068 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5074 name = (char *)iam_it_key_get(&it->oi_it);
5076 RETURN(PTR_ERR(name));
5078 namelen = iam_it_key_size(&it->oi_it);
5080 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5082 RETURN(PTR_ERR(rec));
5084 rc = osd_fid_unpack(fid, rec);
5088 hash = iam_it_store(&it->oi_it);
5090 /* IAM does not store object type in IAM index (dir) */
5091 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5093 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5094 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5095 (struct iam_rec *)dtrec);
5096 osd_quota_unpack(it->oi_obj, dtrec);
5098 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5099 (struct iam_rec *)dtrec);
5106 * Returns cookie for current Iterator position.
5108 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5110 struct osd_it_iam *it = (struct osd_it_iam *)di;
5112 return iam_it_store(&it->oi_it);
5116 * Restore iterator from cookie.
5118 * \param di osd iterator
5119 * \param hash Iterator location cookie
5121 * \retval +ve di points to record with least key not larger than key.
5122 * \retval 0 di points to exact matched key
5123 * \retval -ve failure
5126 static int osd_it_iam_load(const struct lu_env *env,
5127 const struct dt_it *di, __u64 hash)
5129 struct osd_it_iam *it = (struct osd_it_iam *)di;
5131 return iam_it_load(&it->oi_it, hash);
5134 static const struct dt_index_operations osd_index_iam_ops = {
5135 .dio_lookup = osd_index_iam_lookup,
5136 .dio_declare_insert = osd_index_declare_iam_insert,
5137 .dio_insert = osd_index_iam_insert,
5138 .dio_declare_delete = osd_index_declare_iam_delete,
5139 .dio_delete = osd_index_iam_delete,
5141 .init = osd_it_iam_init,
5142 .fini = osd_it_iam_fini,
5143 .get = osd_it_iam_get,
5144 .put = osd_it_iam_put,
5145 .next = osd_it_iam_next,
5146 .key = osd_it_iam_key,
5147 .key_size = osd_it_iam_key_size,
5148 .rec = osd_it_iam_rec,
5149 .store = osd_it_iam_store,
5150 .load = osd_it_iam_load
5156 * Creates or initializes iterator context.
5158 * \retval struct osd_it_ea, iterator structure on success
5161 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5162 struct dt_object *dt,
5165 struct osd_object *obj = osd_dt_obj(dt);
5166 struct osd_thread_info *info = osd_oti_get(env);
5167 struct osd_it_ea *oie;
5169 struct lu_object *lo = &dt->do_lu;
5170 struct dentry *obj_dentry;
5173 if (!dt_object_exists(dt) || obj->oo_destroyed)
5174 RETURN(ERR_PTR(-ENOENT));
5176 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5178 RETURN(ERR_PTR(-ENOMEM));
5179 obj_dentry = &oie->oie_dentry;
5181 obj_dentry->d_inode = obj->oo_inode;
5182 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5183 obj_dentry->d_name.hash = 0;
5185 oie->oie_rd_dirent = 0;
5186 oie->oie_it_dirent = 0;
5187 oie->oie_dirent = NULL;
5188 if (unlikely(!info->oti_it_ea_buf_used)) {
5189 oie->oie_buf = info->oti_it_ea_buf;
5190 info->oti_it_ea_buf_used = 1;
5192 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5193 if (oie->oie_buf == NULL)
5194 RETURN(ERR_PTR(-ENOMEM));
5198 file = &oie->oie_file;
5200 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5201 if (attr & LUDA_64BITHASH)
5202 file->f_mode = FMODE_64BITHASH;
5204 file->f_mode = FMODE_32BITHASH;
5205 file->f_path.dentry = obj_dentry;
5206 file->f_mapping = obj->oo_inode->i_mapping;
5207 file->f_op = obj->oo_inode->i_fop;
5208 set_file_inode(file, obj->oo_inode);
5211 RETURN((struct dt_it *) oie);
5215 * Destroy or finishes iterator context.
5217 * \param di iterator structure to be destroyed
5219 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5221 struct osd_thread_info *info = osd_oti_get(env);
5222 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5223 struct osd_object *obj = oie->oie_obj;
5224 struct inode *inode = obj->oo_inode;
5227 oie->oie_file.f_op->release(inode, &oie->oie_file);
5228 lu_object_put(env, &obj->oo_dt.do_lu);
5229 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5230 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5232 info->oti_it_ea_buf_used = 0;
5233 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5238 * It position the iterator at given key, so that next lookup continues from
5239 * that key Or it is similar to dio_it->load() but based on a key,
5240 * rather than file position.
5242 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5245 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5247 static int osd_it_ea_get(const struct lu_env *env,
5248 struct dt_it *di, const struct dt_key *key)
5250 struct osd_it_ea *it = (struct osd_it_ea *)di;
5253 LASSERT(((const char *)key)[0] == '\0');
5254 it->oie_file.f_pos = 0;
5255 it->oie_rd_dirent = 0;
5256 it->oie_it_dirent = 0;
5257 it->oie_dirent = NULL;
5265 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5269 struct osd_filldir_cbs {
5270 #ifdef HAVE_DIR_CONTEXT
5271 struct dir_context ctx;
5273 struct osd_it_ea *it;
5276 * It is called internally by ->readdir(). It fills the
5277 * iterator's in-memory data structure with required
5278 * information i.e. name, namelen, rec_size etc.
5280 * \param buf in which information to be filled in.
5281 * \param name name of the file in given dir
5283 * \retval 0 on success
5284 * \retval 1 on buffer full
5286 #ifdef HAVE_FILLDIR_USE_CTX
5287 static int osd_ldiskfs_filldir(struct dir_context *buf,
5288 const char *name, int namelen,
5290 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5292 loff_t offset, __u64 ino,
5295 struct osd_it_ea *it =
5296 ((struct osd_filldir_cbs *)buf)->it;
5297 struct osd_object *obj = it->oie_obj;
5298 struct osd_it_ea_dirent *ent = it->oie_dirent;
5299 struct lu_fid *fid = &ent->oied_fid;
5300 struct osd_fid_pack *rec;
5303 /* this should never happen */
5304 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5305 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5309 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5313 /* "." is just the object itself. */
5314 if (namelen == 1 && name[0] == '.') {
5315 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5316 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5317 rec = (struct osd_fid_pack*) (name + namelen + 1);
5318 if (osd_fid_unpack(fid, rec) != 0)
5323 d_type &= ~LDISKFS_DIRENT_LUFID;
5325 /* NOT export local root. */
5326 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5327 ino = obj->oo_inode->i_ino;
5328 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5331 ent->oied_ino = ino;
5332 ent->oied_off = offset;
5333 ent->oied_namelen = namelen;
5334 ent->oied_type = d_type;
5336 memcpy(ent->oied_name, name, namelen);
5338 it->oie_rd_dirent++;
5339 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5344 * Calls ->readdir() to load a directory entry at a time
5345 * and stored it in iterator's in-memory data structure.
5347 * \param di iterator's in memory structure
5349 * \retval 0 on success
5350 * \retval -ve on error
5351 * \retval +1 reach the end of entry
5353 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5354 const struct dt_it *di)
5356 struct osd_it_ea *it = (struct osd_it_ea *)di;
5357 struct osd_object *obj = it->oie_obj;
5358 struct inode *inode = obj->oo_inode;
5359 struct htree_lock *hlock = NULL;
5360 struct file *filp = &it->oie_file;
5362 struct osd_filldir_cbs buf = {
5363 #ifdef HAVE_DIR_CONTEXT
5364 .ctx.actor = osd_ldiskfs_filldir,
5370 it->oie_dirent = it->oie_buf;
5371 it->oie_rd_dirent = 0;
5373 if (obj->oo_hl_head != NULL) {
5374 hlock = osd_oti_get(env)->oti_hlock;
5375 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5376 inode, LDISKFS_HLOCK_READDIR);
5378 down_read(&obj->oo_ext_idx_sem);
5381 #ifdef HAVE_DIR_CONTEXT
5382 buf.ctx.pos = filp->f_pos;
5383 rc = inode->i_fop->iterate(filp, &buf.ctx);
5384 filp->f_pos = buf.ctx.pos;
5386 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5390 ldiskfs_htree_unlock(hlock);
5392 up_read(&obj->oo_ext_idx_sem);
5394 if (it->oie_rd_dirent == 0) {
5395 /*If it does not get any dirent, it means it has been reached
5396 *to the end of the dir */
5397 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5401 it->oie_dirent = it->oie_buf;
5402 it->oie_it_dirent = 1;
5409 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5410 * to load a directory entry at a time and stored it in
5411 * iterator's in-memory data structure.
5413 * \param di iterator's in memory structure
5415 * \retval +ve iterator reached to end
5416 * \retval 0 iterator not reached to end
5417 * \retval -ve on error
5419 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5421 struct osd_it_ea *it = (struct osd_it_ea *)di;
5426 if (it->oie_it_dirent < it->oie_rd_dirent) {
5428 (void *) it->oie_dirent +
5429 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5430 it->oie_dirent->oied_namelen);
5431 it->oie_it_dirent++;
5434 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5437 rc = osd_ldiskfs_it_fill(env, di);
5444 * Returns the key at current position from iterator's in memory structure.
5446 * \param di iterator's in memory structure
5448 * \retval key i.e. struct dt_key on success
5450 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5451 const struct dt_it *di)
5453 struct osd_it_ea *it = (struct osd_it_ea *)di;
5455 return (struct dt_key *)it->oie_dirent->oied_name;
5459 * Returns the key's size at current position from iterator's in memory structure.
5461 * \param di iterator's in memory structure
5463 * \retval key_size i.e. struct dt_key on success
5465 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5467 struct osd_it_ea *it = (struct osd_it_ea *)di;
5469 return it->oie_dirent->oied_namelen;
5473 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5475 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5476 "dot_dotdot = %d\n", dot_dotdot);
5478 if (LDISKFS_DIR_REC_LEN(de) >=
5479 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5486 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5487 unsigned blocksize, int dot_dotdot)
5490 return osd_dot_dotdot_has_space(de, dot_dotdot);
5492 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5493 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5500 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5501 struct dentry *dentry, const struct lu_fid *fid,
5502 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5503 struct htree_lock *hlock, int dot_dotdot)
5505 struct inode *dir = dentry->d_parent->d_inode;
5506 struct inode *inode = dentry->d_inode;
5507 struct osd_fid_pack *rec;
5508 struct ldiskfs_dentry_param *ldp;
5509 int namelen = dentry->d_name.len;
5511 struct osd_thread_info *info = osd_oti_get(env);
5514 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5515 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5518 /* There is enough space to hold the FID-in-dirent. */
5519 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5521 rc = ldiskfs_journal_get_write_access(jh, bh);
5525 de->name[namelen] = 0;
5526 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5527 rec->fp_len = sizeof(struct lu_fid) + 1;
5528 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5529 de->file_type |= LDISKFS_DIRENT_LUFID;
5530 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5535 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5537 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5541 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5542 osd_get_ldiskfs_dirent_param(ldp, fid);
5543 dentry->d_fsdata = (void *)ldp;
5544 ll_vfs_dq_init(dir);
5545 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5546 /* It is too bad, we cannot reinsert the name entry back.
5547 * That means we lose it! */
5549 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5550 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5551 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5552 dir->i_ino, dir->i_generation, namelen,
5553 dentry->d_name.name, PFID(fid), rc);
5559 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5560 struct osd_it_ea *it, struct lu_fid *fid,
5561 struct osd_inode_id *id, __u32 *attr)
5563 struct osd_thread_info *info = osd_oti_get(env);
5564 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5565 struct osd_device *dev = osd_obj2dev(obj);
5566 struct super_block *sb = osd_sb(dev);
5567 const char *devname =
5568 LDISKFS_SB(sb)->s_es->s_volume_name;
5569 struct osd_it_ea_dirent *ent = it->oie_dirent;
5570 struct inode *dir = obj->oo_inode;
5571 struct htree_lock *hlock = NULL;
5572 struct buffer_head *bh = NULL;
5573 handle_t *jh = NULL;
5574 struct ldiskfs_dir_entry_2 *de;
5575 struct dentry *dentry;
5576 struct inode *inode;
5583 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5584 inode = osd_iget(info, dev, id);
5585 if (IS_ERR(inode)) {
5586 rc = PTR_ERR(inode);
5587 if (rc == -ENOENT || rc == -ESTALE) {
5588 *attr |= LUDA_UNKNOWN;
5591 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5592 "check_repair, dir = %lu/%u, name = %.*s: "
5594 devname, dir->i_ino, dir->i_generation,
5595 ent->oied_namelen, ent->oied_name, rc);
5601 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5603 rc = osd_get_lma(info, inode, dentry, lma);
5609 if (ent->oied_name[0] == '.') {
5610 if (ent->oied_namelen == 1)
5612 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5616 /* We need to ensure that the name entry is still valid.
5617 * Because it may be removed or renamed by other already.
5619 * The unlink or rename operation will start journal before PDO lock,
5620 * so to avoid deadlock, here we need to start journal handle before
5621 * related PDO lock also. But because we do not know whether there
5622 * will be something to be repaired before PDO lock, we just start
5623 * journal without conditions.
5625 * We may need to remove the name entry firstly, then insert back.
5626 * One credit is for user quota file update.
5627 * One credit is for group quota file update.
5628 * Two credits are for dirty inode. */
5629 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5630 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5632 if (dev->od_dirent_journal != 0) {
5635 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5638 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5639 "check_repair, dir = %lu/%u, credits = %d, "
5640 "name = %.*s: rc = %d\n",
5641 devname, dir->i_ino, dir->i_generation, credits,
5642 ent->oied_namelen, ent->oied_name, rc);
5644 GOTO(out_inode, rc);
5647 if (obj->oo_hl_head != NULL) {
5648 hlock = osd_oti_get(env)->oti_hlock;
5649 /* "0" means exclusive lock for the whole directory.
5650 * We need to prevent others access such name entry
5651 * during the delete + insert. Neither HLOCK_ADD nor
5652 * HLOCK_DEL cannot guarantee the atomicity. */
5653 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5655 down_write(&obj->oo_ext_idx_sem);
5658 if (obj->oo_hl_head != NULL) {
5659 hlock = osd_oti_get(env)->oti_hlock;
5660 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5661 LDISKFS_HLOCK_LOOKUP);
5663 down_read(&obj->oo_ext_idx_sem);
5667 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5668 /* For dot/dotdot entry, if there is not enough space to hold the
5669 * FID-in-dirent, just keep them there. It only happens when the
5670 * device upgraded from 1.8 or restored from MDT file-level backup.
5671 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5672 * and needs to get FID from LMA when readdir, it will not affect the
5673 * performance much. */
5674 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5675 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5676 *attr |= LUDA_IGNORE;
5682 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5683 struct lu_fid *tfid = &lma->lma_self_fid;
5685 *attr |= LUDA_IGNORE;
5686 /* It must be REMOTE_PARENT_DIR and as the
5687 * dotdot entry of remote directory */
5688 if (unlikely(dot_dotdot != 2 ||
5689 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5690 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5691 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5692 "parent directory, but got %.*s under "
5693 "dir = %lu/%u with the FID "DFID"\n",
5694 devname, ent->oied_namelen,
5695 ent->oied_name, dir->i_ino,
5696 dir->i_generation, PFID(tfid));
5698 GOTO(out, rc = -EIO);
5704 if (fid_is_sane(fid)) {
5705 /* FID-in-dirent is valid. */
5706 if (lu_fid_eq(fid, &lma->lma_self_fid))
5709 /* Do not repair under dryrun mode. */
5710 if (*attr & LUDA_VERIFY_DRYRUN) {
5711 *attr |= LUDA_REPAIR;
5718 dev->od_dirent_journal = 1;
5719 if (hlock != NULL) {
5720 ldiskfs_htree_unlock(hlock);
5723 up_read(&obj->oo_ext_idx_sem);
5729 *fid = lma->lma_self_fid;
5731 /* Update the FID-in-dirent. */
5732 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5735 *attr |= LUDA_REPAIR;
5737 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5738 "in the dirent, dir = %lu/%u, "
5739 "name = %.*s, "DFID": rc = %d\n",
5740 devname, dir->i_ino, dir->i_generation,
5741 ent->oied_namelen, ent->oied_name,
5744 /* Do not repair under dryrun mode. */
5745 if (*attr & LUDA_VERIFY_DRYRUN) {
5746 *fid = lma->lma_self_fid;
5747 *attr |= LUDA_REPAIR;
5754 dev->od_dirent_journal = 1;
5755 if (hlock != NULL) {
5756 ldiskfs_htree_unlock(hlock);
5759 up_read(&obj->oo_ext_idx_sem);
5765 *fid = lma->lma_self_fid;
5767 /* Append the FID-in-dirent. */
5768 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5771 *attr |= LUDA_REPAIR;
5773 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5774 "after the dirent, dir = %lu/%u, "
5775 "name = %.*s, "DFID": rc = %d\n",
5776 devname, dir->i_ino, dir->i_generation,
5777 ent->oied_namelen, ent->oied_name,
5781 /* Do not repair under dryrun mode. */
5782 if (*attr & LUDA_VERIFY_DRYRUN) {
5783 if (fid_is_sane(fid)) {
5784 *attr |= LUDA_REPAIR;
5786 lu_igif_build(fid, inode->i_ino,
5787 inode->i_generation);
5788 *attr |= LUDA_UPGRADE;
5796 dev->od_dirent_journal = 1;
5797 if (hlock != NULL) {
5798 ldiskfs_htree_unlock(hlock);
5801 up_read(&obj->oo_ext_idx_sem);
5808 if (unlikely(fid_is_sane(fid))) {
5809 /* FID-in-dirent exists, but FID-in-LMA is lost.
5810 * Trust the FID-in-dirent, and add FID-in-LMA. */
5811 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5813 *attr |= LUDA_REPAIR;
5815 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5816 "update dirent, dir = %lu/%u, "
5817 "name = %.*s, "DFID": rc = %d\n",
5818 devname, dir->i_ino, dir->i_generation,
5819 ent->oied_namelen, ent->oied_name,
5822 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5823 /* It is probably IGIF object. Only aappend the
5824 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5825 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5828 *attr |= LUDA_UPGRADE;
5830 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5831 "after the dirent, dir = %lu/%u, "
5832 "name = %.*s, "DFID": rc = %d\n",
5833 devname, dir->i_ino, dir->i_generation,
5834 ent->oied_namelen, ent->oied_name,
5843 if (hlock != NULL) {
5844 ldiskfs_htree_unlock(hlock);
5846 if (dev->od_dirent_journal != 0)
5847 up_write(&obj->oo_ext_idx_sem);
5849 up_read(&obj->oo_ext_idx_sem);
5853 ldiskfs_journal_stop(jh);
5857 if (rc >= 0 && !dirty)
5858 dev->od_dirent_journal = 0;
5864 * Returns the value at current position from iterator's in memory structure.
5866 * \param di struct osd_it_ea, iterator's in memory structure
5867 * \param attr attr requested for dirent.
5868 * \param lde lustre dirent
5870 * \retval 0 no error and \param lde has correct lustre dirent.
5871 * \retval -ve on error
5873 static inline int osd_it_ea_rec(const struct lu_env *env,
5874 const struct dt_it *di,
5875 struct dt_rec *dtrec, __u32 attr)
5877 struct osd_it_ea *it = (struct osd_it_ea *)di;
5878 struct osd_object *obj = it->oie_obj;
5879 struct osd_device *dev = osd_obj2dev(obj);
5880 struct osd_thread_info *oti = osd_oti_get(env);
5881 struct osd_inode_id *id = &oti->oti_id;
5882 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5883 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5884 __u32 ino = it->oie_dirent->oied_ino;
5888 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5890 if (attr & LUDA_VERIFY) {
5891 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5892 attr |= LUDA_IGNORE;
5893 /* If the parent is on remote MDT, and there
5894 * is no FID-in-dirent, then we have to get
5895 * the parent FID from the linkEA. */
5896 if (!fid_is_sane(fid) &&
5897 it->oie_dirent->oied_namelen == 2 &&
5898 it->oie_dirent->oied_name[0] == '.' &&
5899 it->oie_dirent->oied_name[1] == '.')
5900 osd_get_pfid_from_linkea(env, obj, fid);
5902 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5906 if (!fid_is_sane(fid)) {
5907 attr &= ~LUDA_IGNORE;
5908 attr |= LUDA_UNKNOWN;
5911 attr &= ~LU_DIRENT_ATTRS_MASK;
5912 if (!fid_is_sane(fid)) {
5913 bool is_dotdot = false;
5914 if (it->oie_dirent->oied_namelen == 2 &&
5915 it->oie_dirent->oied_name[0] == '.' &&
5916 it->oie_dirent->oied_name[1] == '.')
5918 /* If the parent is on remote MDT, and there
5919 * is no FID-in-dirent, then we have to get
5920 * the parent FID from the linkEA. */
5921 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5922 rc = osd_get_pfid_from_linkea(env, obj, fid);
5924 if (is_dotdot == false &&
5925 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5928 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5931 osd_id_gen(id, ino, OSD_OII_NOGEN);
5935 /* Pack the entry anyway, at least the offset is right. */
5936 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5937 it->oie_dirent->oied_name,
5938 it->oie_dirent->oied_namelen,
5939 it->oie_dirent->oied_type, attr);
5944 if (osd_remote_fid(env, dev, fid))
5947 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5948 osd_add_oi_cache(oti, dev, id, fid);
5950 RETURN(rc > 0 ? 0 : rc);
5954 * Returns the record size size at current position.
5956 * This function will return record(lu_dirent) size in bytes.
5958 * \param[in] env execution environment
5959 * \param[in] di iterator's in memory structure
5960 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5961 * calculate the lu_dirent size.
5963 * \retval record size(in bytes & in memory) of the current lu_dirent
5966 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5969 struct osd_it_ea *it = (struct osd_it_ea *)di;
5971 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5975 * Returns a cookie for current position of the iterator head, so that
5976 * user can use this cookie to load/start the iterator next time.
5978 * \param di iterator's in memory structure
5980 * \retval cookie for current position, on success
5982 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5984 struct osd_it_ea *it = (struct osd_it_ea *)di;
5986 return it->oie_dirent->oied_off;
5990 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5991 * to load a directory entry at a time and stored it i inn,
5992 * in iterator's in-memory data structure.
5994 * \param di struct osd_it_ea, iterator's in memory structure
5996 * \retval +ve on success
5997 * \retval -ve on error
5999 static int osd_it_ea_load(const struct lu_env *env,
6000 const struct dt_it *di, __u64 hash)
6002 struct osd_it_ea *it = (struct osd_it_ea *)di;
6006 it->oie_file.f_pos = hash;
6008 rc = osd_ldiskfs_it_fill(env, di);
6019 * Index lookup function for interoperability mode (b11826).
6021 * \param key, key i.e. file name to be searched
6023 * \retval +ve, on success
6024 * \retval -ve, on error
6026 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6027 struct dt_rec *rec, const struct dt_key *key)
6029 struct osd_object *obj = osd_dt_obj(dt);
6034 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6035 LINVRNT(osd_invariant(obj));
6037 rc = osd_ea_lookup_rec(env, obj, rec, key);
6044 * Index and Iterator operations for interoperability
6045 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6047 static const struct dt_index_operations osd_index_ea_ops = {
6048 .dio_lookup = osd_index_ea_lookup,
6049 .dio_declare_insert = osd_index_declare_ea_insert,
6050 .dio_insert = osd_index_ea_insert,
6051 .dio_declare_delete = osd_index_declare_ea_delete,
6052 .dio_delete = osd_index_ea_delete,
6054 .init = osd_it_ea_init,
6055 .fini = osd_it_ea_fini,
6056 .get = osd_it_ea_get,
6057 .put = osd_it_ea_put,
6058 .next = osd_it_ea_next,
6059 .key = osd_it_ea_key,
6060 .key_size = osd_it_ea_key_size,
6061 .rec = osd_it_ea_rec,
6062 .rec_size = osd_it_ea_rec_size,
6063 .store = osd_it_ea_store,
6064 .load = osd_it_ea_load
6068 static void *osd_key_init(const struct lu_context *ctx,
6069 struct lu_context_key *key)
6071 struct osd_thread_info *info;
6073 OBD_ALLOC_PTR(info);
6075 return ERR_PTR(-ENOMEM);
6077 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6078 if (info->oti_it_ea_buf == NULL)
6081 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6083 info->oti_hlock = ldiskfs_htree_lock_alloc();
6084 if (info->oti_hlock == NULL)
6090 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6093 return ERR_PTR(-ENOMEM);
6096 static void osd_key_fini(const struct lu_context *ctx,
6097 struct lu_context_key *key, void* data)
6099 struct osd_thread_info *info = data;
6100 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6101 struct osd_idmap_cache *idc = info->oti_ins_cache;
6103 if (info->oti_inode != NULL)
6105 if (info->oti_hlock != NULL)
6106 ldiskfs_htree_lock_free(info->oti_hlock);
6107 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6108 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6109 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6110 lu_buf_free(&info->oti_big_buf);
6112 LASSERT(info->oti_ins_cache_size > 0);
6113 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6114 info->oti_ins_cache = NULL;
6115 info->oti_ins_cache_size = 0;
6120 static void osd_key_exit(const struct lu_context *ctx,
6121 struct lu_context_key *key, void *data)
6123 struct osd_thread_info *info = data;
6125 LASSERT(info->oti_r_locks == 0);
6126 LASSERT(info->oti_w_locks == 0);
6127 LASSERT(info->oti_txns == 0);
6130 /* type constructor/destructor: osd_type_init, osd_type_fini */
6131 LU_TYPE_INIT_FINI(osd, &osd_key);
6133 struct lu_context_key osd_key = {
6134 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6135 .lct_init = osd_key_init,
6136 .lct_fini = osd_key_fini,
6137 .lct_exit = osd_key_exit
6141 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6142 const char *name, struct lu_device *next)
6144 struct osd_device *osd = osd_dev(d);
6146 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6147 >= sizeof(osd->od_svname))
6149 return osd_procfs_init(osd, name);
6152 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6154 struct seq_server_site *ss = osd_seq_site(osd);
6158 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6161 if (unlikely(ss == NULL))
6164 OBD_ALLOC_PTR(osd->od_cl_seq);
6165 if (osd->od_cl_seq == NULL)
6168 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6169 osd->od_svname, ss->ss_server_seq);
6171 OBD_FREE_PTR(osd->od_cl_seq);
6172 osd->od_cl_seq = NULL;
6176 if (ss->ss_node_id == 0) {
6177 /* If the OSD on the sequence controller(MDT0), then allocate
6178 * sequence here, otherwise allocate sequence after connected
6179 * to MDT0 (see mdt_register_lwp_callback()). */
6180 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6181 &osd->od_cl_seq->lcs_space, env);
6187 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6189 if (osd->od_cl_seq == NULL)
6192 seq_client_fini(osd->od_cl_seq);
6193 OBD_FREE_PTR(osd->od_cl_seq);
6194 osd->od_cl_seq = NULL;
6197 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6201 /* shutdown quota slave instance associated with the device */
6202 if (o->od_quota_slave != NULL) {
6203 qsd_fini(env, o->od_quota_slave);
6204 o->od_quota_slave = NULL;
6207 osd_fid_fini(env, o);
6212 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6216 if (o->od_mnt != NULL) {
6217 shrink_dcache_sb(osd_sb(o));
6218 osd_sync(env, &o->od_dt_dev);
6227 static int osd_mount(const struct lu_env *env,
6228 struct osd_device *o, struct lustre_cfg *cfg)
6230 const char *name = lustre_cfg_string(cfg, 0);
6231 const char *dev = lustre_cfg_string(cfg, 1);
6233 unsigned long page, s_flags, lmd_flags = 0;
6234 struct page *__page;
6235 struct file_system_type *type;
6236 char *options = NULL;
6238 struct osd_thread_info *info = osd_oti_get(env);
6239 struct lu_fid *fid = &info->oti_fid;
6240 struct inode *inode;
6241 int rc = 0, force_over_256tb = 0;
6244 if (o->od_mnt != NULL)
6247 if (strlen(dev) >= sizeof(o->od_mntdev))
6249 strcpy(o->od_mntdev, dev);
6251 str = lustre_cfg_string(cfg, 2);
6252 s_flags = simple_strtoul(str, NULL, 0);
6253 str = strstr(str, ":");
6255 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6256 opts = lustre_cfg_string(cfg, 3);
6258 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6259 CERROR("%s: device %s extents feature is not guaranteed to "
6260 "work on big-endian systems. Use \"bigendian_extents\" "
6261 "mount option to override.\n", name, dev);
6265 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6266 CWARN("force_over_128tb option is depricated."
6267 "Filesystems less then 256TB can be created without any"
6268 "force options. Use force_over_256tb option for"
6269 "filesystems greather then 256TB.\n");
6272 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL)
6273 force_over_256tb = 1;
6275 __page = alloc_page(GFP_KERNEL);
6277 GOTO(out, rc = -ENOMEM);
6278 page = (unsigned long)page_address(__page);
6279 options = (char *)page;
6282 /* strip out the options for back compatiblity */
6283 static char *sout[] = {
6290 /* strip out option we processed in osd */
6291 "bigendian_extents",
6292 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(3,0,53,0)
6293 #warning "remove force_over_128 option"
6295 "force_over_128tb (deprecated)",
6300 strcat(options, opts);
6301 for (rc = 0, str = options; sout[rc]; ) {
6302 char *op = strstr(str, sout[rc]);
6308 if (op == options || *(op - 1) == ',') {
6309 str = op + strlen(sout[rc]);
6310 if (*str == ',' || *str == '\0') {
6311 *str == ',' ? str++ : str;
6312 memmove(op, str, strlen(str) + 1);
6315 for (str = op; *str != ',' && *str != '\0'; str++)
6319 strncat(options, "user_xattr,acl", 14);
6322 /* Glom up mount options */
6323 if (*options != '\0')
6324 strcat(options, ",");
6325 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6327 type = get_fs_type("ldiskfs");
6329 CERROR("%s: cannot find ldiskfs module\n", name);
6330 GOTO(out, rc = -ENODEV);
6333 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6334 module_put(type->owner);
6336 if (IS_ERR(o->od_mnt)) {
6337 rc = PTR_ERR(o->od_mnt);
6339 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6343 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (64ULL << 30) &&
6344 force_over_256tb == 0) {
6345 CERROR("%s: device %s LDISKFS does not support filesystems "
6346 "greater than 256TB and can cause data corruption. "
6347 "Use \"force_over_256tb\" mount option to override.\n",
6349 GOTO(out, rc = -EINVAL);
6352 #ifdef HAVE_DEV_SET_RDONLY
6353 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6354 CERROR("%s: underlying device %s is marked as read-only. "
6355 "Setup failed\n", name, dev);
6356 GOTO(out_mnt, rc = -EROFS);
6360 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6361 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6362 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6363 GOTO(out_mnt, rc = -EINVAL);
6366 #ifdef LDISKFS_MOUNT_DIRDATA
6367 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6368 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6369 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6370 else if (!o->od_is_ost)
6371 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6372 "enabling the dirdata feature. If you do not want to "
6373 "downgrade to Lustre-1.x again, you can enable it via "
6374 "'tune2fs -O dirdata device'\n", name, dev);
6376 inode = osd_sb(o)->s_root->d_inode;
6377 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6378 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6380 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6384 if (lmd_flags & LMD_FLG_NOSCRUB)
6395 __free_page(__page);
6400 static struct lu_device *osd_device_fini(const struct lu_env *env,
6401 struct lu_device *d)
6403 struct osd_device *o = osd_dev(d);
6406 osd_shutdown(env, o);
6408 osd_scrub_cleanup(env, o);
6409 osd_obj_map_fini(o);
6415 static int osd_device_init0(const struct lu_env *env,
6416 struct osd_device *o,
6417 struct lustre_cfg *cfg)
6419 struct lu_device *l = osd2lu_dev(o);
6420 struct osd_thread_info *info;
6424 /* if the module was re-loaded, env can loose its keys */
6425 rc = lu_env_refill((struct lu_env *) env);
6428 info = osd_oti_get(env);
6431 l->ld_ops = &osd_lu_ops;
6432 o->od_dt_dev.dd_ops = &osd_dt_ops;
6434 spin_lock_init(&o->od_osfs_lock);
6435 mutex_init(&o->od_otable_mutex);
6436 INIT_LIST_HEAD(&o->od_orphan_list);
6438 o->od_read_cache = 1;
6439 o->od_writethrough_cache = 1;
6440 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6442 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6443 sizeof(o->od_svname));
6444 if (cplen >= sizeof(o->od_svname)) {
6449 o->od_index = -1; /* -1 means index is invalid */
6450 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6451 if (rc == LDD_F_SV_TYPE_OST)
6454 o->od_full_scrub_ratio = OFSR_DEFAULT;
6455 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6456 rc = osd_mount(env, o, cfg);
6460 rc = osd_obj_map_init(env, o);
6464 rc = lu_site_init(&o->od_site, l);
6466 GOTO(out_compat, rc);
6467 o->od_site.ls_bottom_dev = l;
6469 rc = lu_site_init_finish(&o->od_site);
6473 INIT_LIST_HEAD(&o->od_ios_list);
6474 /* setup scrub, including OI files initialization */
6475 rc = osd_scrub_setup(env, o);
6479 rc = osd_procfs_init(o, o->od_svname);
6481 CERROR("%s: can't initialize procfs: rc = %d\n",
6483 GOTO(out_scrub, rc);
6486 LASSERT(l->ld_site->ls_linkage.next != NULL);
6487 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6489 /* initialize quota slave instance */
6490 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6492 if (IS_ERR(o->od_quota_slave)) {
6493 rc = PTR_ERR(o->od_quota_slave);
6494 o->od_quota_slave = NULL;
6495 GOTO(out_procfs, rc);
6503 osd_scrub_cleanup(env, o);
6505 lu_site_fini(&o->od_site);
6507 osd_obj_map_fini(o);
6514 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6515 struct lu_device_type *t,
6516 struct lustre_cfg *cfg)
6518 struct osd_device *o;
6523 return ERR_PTR(-ENOMEM);
6525 rc = dt_device_init(&o->od_dt_dev, t);
6527 /* Because the ctx might be revived in dt_device_init,
6528 * refill the env here */
6529 lu_env_refill((struct lu_env *)env);
6530 rc = osd_device_init0(env, o, cfg);
6532 dt_device_fini(&o->od_dt_dev);
6535 if (unlikely(rc != 0))
6538 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6541 static struct lu_device *osd_device_free(const struct lu_env *env,
6542 struct lu_device *d)
6544 struct osd_device *o = osd_dev(d);
6547 /* XXX: make osd top device in order to release reference */
6548 d->ld_site->ls_top_dev = d;
6549 lu_site_purge(env, d->ld_site, -1);
6550 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6551 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6552 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6554 lu_site_fini(&o->od_site);
6555 dt_device_fini(&o->od_dt_dev);
6560 static int osd_process_config(const struct lu_env *env,
6561 struct lu_device *d, struct lustre_cfg *cfg)
6563 struct osd_device *o = osd_dev(d);
6567 switch (cfg->lcfg_command) {
6569 rc = osd_mount(env, o, cfg);
6572 lu_dev_del_linkage(d->ld_site, d);
6573 rc = osd_shutdown(env, o);
6576 LASSERT(&o->od_dt_dev);
6577 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6578 cfg, &o->od_dt_dev);
6579 if (rc > 0 || rc == -ENOSYS)
6580 rc = class_process_proc_param(PARAM_OST,
6581 lprocfs_osd_obd_vars,
6582 cfg, &o->od_dt_dev);
6591 static int osd_recovery_complete(const struct lu_env *env,
6592 struct lu_device *d)
6594 struct osd_device *osd = osd_dev(d);
6598 if (osd->od_quota_slave == NULL)
6601 /* start qsd instance on recovery completion, this notifies the quota
6602 * slave code that we are about to process new requests now */
6603 rc = qsd_start(env, osd->od_quota_slave);
6608 * we use exports to track all osd users
6610 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6611 struct obd_device *obd, struct obd_uuid *cluuid,
6612 struct obd_connect_data *data, void *localdata)
6614 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6615 struct lustre_handle conn;
6619 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6621 rc = class_connect(&conn, obd, cluuid);
6625 *exp = class_conn2export(&conn);
6627 spin_lock(&osd->od_osfs_lock);
6629 spin_unlock(&osd->od_osfs_lock);
6635 * once last export (we don't count self-export) disappeared
6636 * osd can be released
6638 static int osd_obd_disconnect(struct obd_export *exp)
6640 struct obd_device *obd = exp->exp_obd;
6641 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6642 int rc, release = 0;
6645 /* Only disconnect the underlying layers on the final disconnect. */
6646 spin_lock(&osd->od_osfs_lock);
6648 if (osd->od_connects == 0)
6650 spin_unlock(&osd->od_osfs_lock);
6652 rc = class_disconnect(exp); /* bz 9811 */
6654 if (rc == 0 && release)
6655 class_manual_cleanup(obd);
6659 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6660 struct lu_device *dev)
6662 struct osd_device *osd = osd_dev(dev);
6663 struct lr_server_data *lsd =
6664 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6668 if (osd->od_quota_slave != NULL) {
6669 /* set up quota slave objects */
6670 result = qsd_prepare(env, osd->od_quota_slave);
6675 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6676 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6677 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6678 osd->od_index_in_idif = 1;
6680 osd->od_index_in_idif = 0;
6681 result = osd_register_proc_index_in_idif(osd);
6686 osd->od_index_in_idif = 1;
6690 result = osd_fid_init(env, osd);
6695 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6696 struct lu_fid *fid, struct md_op_data *op_data)
6698 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6700 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6703 static const struct lu_object_operations osd_lu_obj_ops = {
6704 .loo_object_init = osd_object_init,
6705 .loo_object_delete = osd_object_delete,
6706 .loo_object_release = osd_object_release,
6707 .loo_object_free = osd_object_free,
6708 .loo_object_print = osd_object_print,
6709 .loo_object_invariant = osd_object_invariant
6712 const struct lu_device_operations osd_lu_ops = {
6713 .ldo_object_alloc = osd_object_alloc,
6714 .ldo_process_config = osd_process_config,
6715 .ldo_recovery_complete = osd_recovery_complete,
6716 .ldo_prepare = osd_prepare,
6719 static const struct lu_device_type_operations osd_device_type_ops = {
6720 .ldto_init = osd_type_init,
6721 .ldto_fini = osd_type_fini,
6723 .ldto_start = osd_type_start,
6724 .ldto_stop = osd_type_stop,
6726 .ldto_device_alloc = osd_device_alloc,
6727 .ldto_device_free = osd_device_free,
6729 .ldto_device_init = osd_device_init,
6730 .ldto_device_fini = osd_device_fini
6733 static struct lu_device_type osd_device_type = {
6734 .ldt_tags = LU_DEVICE_DT,
6735 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6736 .ldt_ops = &osd_device_type_ops,
6737 .ldt_ctx_tags = LCT_LOCAL,
6740 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6742 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6743 struct super_block *sb = osd_sb(osd);
6745 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6749 * lprocfs legacy support.
6751 static struct obd_ops osd_obd_device_ops = {
6752 .o_owner = THIS_MODULE,
6753 .o_connect = osd_obd_connect,
6754 .o_disconnect = osd_obd_disconnect,
6755 .o_fid_alloc = osd_fid_alloc,
6756 .o_health_check = osd_health_check,
6759 static int __init osd_init(void)
6763 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6764 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6765 /* please, try to keep osd_thread_info smaller than a page */
6766 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6771 rc = lu_kmem_init(ldiskfs_caches);
6775 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6776 lprocfs_osd_module_vars,
6777 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6779 lu_kmem_fini(ldiskfs_caches);
6783 static void __exit osd_exit(void)
6785 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6786 lu_kmem_fini(ldiskfs_caches);
6789 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6790 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6791 MODULE_VERSION(LUSTRE_VERSION_STRING);
6792 MODULE_LICENSE("GPL");
6794 module_init(osd_init);
6795 module_exit(osd_exit);