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>
65 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
69 /* struct ptlrpc_thread */
70 #include <lustre_net.h>
71 #include <lustre_fid.h>
73 #include <lustre_param.h>
75 #include "osd_internal.h"
76 #include "osd_dynlocks.h"
78 /* llo_* api support */
79 #include <md_object.h>
80 #include <lustre_quota.h>
82 #include <lustre_linkea.h>
85 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
86 "ldiskfs with parallel directory operations");
88 int ldiskfs_track_declares_assert;
89 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
90 "LBUG during tracking of declares");
92 /* Slab to allocate dynlocks */
93 struct kmem_cache *dynlock_cachep;
95 /* Slab to allocate osd_it_ea */
96 struct kmem_cache *osd_itea_cachep;
98 static struct lu_kmem_descr ldiskfs_caches[] = {
100 .ckd_cache = &dynlock_cachep,
101 .ckd_name = "dynlock_cache",
102 .ckd_size = sizeof(struct dynlock_handle)
105 .ckd_cache = &osd_itea_cachep,
106 .ckd_name = "osd_itea_cache",
107 .ckd_size = sizeof(struct osd_it_ea)
114 static const char dot[] = ".";
115 static const char dotdot[] = "..";
116 static const char remote_obj_dir[] = "REM_OBJ_DIR";
118 static const struct lu_object_operations osd_lu_obj_ops;
119 static const struct dt_object_operations osd_obj_ops;
120 static const struct dt_object_operations osd_obj_ea_ops;
121 static const struct dt_object_operations osd_obj_otable_it_ops;
122 static const struct dt_index_operations osd_index_iam_ops;
123 static const struct dt_index_operations osd_index_ea_ops;
125 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
126 const struct lu_fid *fid);
127 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
128 struct osd_device *osd);
130 int osd_trans_declare_op2rb[] = {
131 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
132 [OSD_OT_PUNCH] = OSD_OT_MAX,
133 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
134 [OSD_OT_CREATE] = OSD_OT_DESTROY,
135 [OSD_OT_DESTROY] = OSD_OT_CREATE,
136 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
137 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
138 [OSD_OT_WRITE] = OSD_OT_WRITE,
139 [OSD_OT_INSERT] = OSD_OT_DELETE,
140 [OSD_OT_DELETE] = OSD_OT_INSERT,
141 [OSD_OT_QUOTA] = OSD_OT_MAX,
144 static int osd_has_index(const struct osd_object *obj)
146 return obj->oo_dt.do_index_ops != NULL;
149 static int osd_object_invariant(const struct lu_object *l)
151 return osd_invariant(osd_obj(l));
155 * Concurrency: doesn't matter
159 * Concurrency: doesn't matter
161 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
163 struct osd_thread_info *oti = osd_oti_get(env);
164 return oti->oti_w_locks > 0 && o->oo_owner == env;
168 * Concurrency: doesn't access mutable data
170 static int osd_root_get(const struct lu_env *env,
171 struct dt_device *dev, struct lu_fid *f)
173 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
178 * the following set of functions are used to maintain per-thread
179 * cache of FID->ino mapping. this mechanism is needed to resolve
180 * FID to inode at dt_insert() which in turn stores ino in the
181 * directory entries to keep ldiskfs compatible with ext[34].
182 * due to locking-originated restrictions we can't lookup ino
183 * using LU cache (deadlock is possible). lookup using OI is quite
184 * expensive. so instead we maintain this cache and methods like
185 * dt_create() fill it. so in the majority of cases dt_insert() is
186 * able to find needed mapping in lockless manner.
188 static struct osd_idmap_cache *
189 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
190 const struct lu_fid *fid)
192 struct osd_thread_info *oti = osd_oti_get(env);
193 struct osd_idmap_cache *idc = oti->oti_ins_cache;
195 for (i = 0; i < oti->oti_ins_cache_used; i++) {
196 if (!lu_fid_eq(&idc[i].oic_fid, fid))
198 if (idc[i].oic_dev != osd)
207 static struct osd_idmap_cache *
208 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
209 const struct lu_fid *fid)
211 struct osd_thread_info *oti = osd_oti_get(env);
212 struct osd_idmap_cache *idc;
215 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
216 i = oti->oti_ins_cache_size * 2;
218 i = OSD_INS_CACHE_SIZE;
219 OBD_ALLOC(idc, sizeof(*idc) * i);
221 return ERR_PTR(-ENOMEM);
222 if (oti->oti_ins_cache != NULL) {
223 memcpy(idc, oti->oti_ins_cache,
224 oti->oti_ins_cache_used * sizeof(*idc));
225 OBD_FREE(oti->oti_ins_cache,
226 oti->oti_ins_cache_used * sizeof(*idc));
228 oti->oti_ins_cache = idc;
229 oti->oti_ins_cache_size = i;
232 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
235 idc->oic_lid.oii_ino = 0;
236 idc->oic_lid.oii_gen = 0;
243 * lookup mapping for the given fid in the cache, initialize a
244 * new one if not found. the initialization checks whether the
245 * object is local or remote. for local objects, OI is used to
246 * learn ino/generation. the function is used when the caller
247 * has no information about the object, e.g. at dt_insert().
249 static struct osd_idmap_cache *
250 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
251 const struct lu_fid *fid)
253 struct osd_idmap_cache *idc;
256 idc = osd_idc_find(env, osd, fid);
257 LASSERT(!IS_ERR(idc));
261 /* new mapping is needed */
262 idc = osd_idc_add(env, osd, fid);
267 rc = osd_remote_fid(env, osd, fid);
268 if (unlikely(rc < 0))
272 /* the object is local, lookup in OI */
273 /* XXX: probably cheaper to lookup in LU first? */
274 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
276 if (unlikely(rc < 0)) {
277 CERROR("can't lookup: rc = %d\n", rc);
281 /* the object is remote */
289 * lookup mapping for given FID and fill it from the given object.
290 * the object is lolcal by definition.
292 static int osd_idc_find_and_init(const struct lu_env *env,
293 struct osd_device *osd,
294 struct osd_object *obj)
296 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
297 struct osd_idmap_cache *idc;
299 idc = osd_idc_find(env, osd, fid);
300 LASSERT(!IS_ERR(idc));
302 if (obj->oo_inode == NULL)
304 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
305 LASSERT(idc->oic_lid.oii_ino == 0);
306 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
307 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
312 /* new mapping is needed */
313 idc = osd_idc_add(env, osd, fid);
317 if (obj->oo_inode != NULL) {
318 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
319 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
325 * OSD object methods.
329 * Concurrency: no concurrent access is possible that early in object
332 static struct lu_object *osd_object_alloc(const struct lu_env *env,
333 const struct lu_object_header *hdr,
336 struct osd_object *mo;
342 l = &mo->oo_dt.do_lu;
343 dt_object_init(&mo->oo_dt, NULL, d);
344 mo->oo_dt.do_ops = &osd_obj_ea_ops;
345 l->lo_ops = &osd_lu_obj_ops;
346 init_rwsem(&mo->oo_sem);
347 init_rwsem(&mo->oo_ext_idx_sem);
348 spin_lock_init(&mo->oo_guard);
355 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
356 struct dentry *dentry, struct lustre_mdt_attrs *lma)
360 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
361 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
362 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
364 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
365 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
367 lustre_lma_swab(lma);
368 /* Check LMA compatibility */
369 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
370 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
371 "for fid = "DFID", ino = %lu\n",
372 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
373 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
374 PFID(&lma->lma_self_fid), inode->i_ino);
377 } else if (rc == 0) {
385 * retrieve object from backend ext fs.
387 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
388 struct osd_inode_id *id)
390 struct inode *inode = NULL;
392 /* if we look for an inode withing a running
393 * transaction, then we risk to deadlock */
394 /* osd_dirent_check_repair() breaks this */
395 /*LASSERT(current->journal_info == NULL);*/
397 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
399 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
400 id->oii_ino, PTR_ERR(inode));
401 } else if (id->oii_gen != OSD_OII_NOGEN &&
402 inode->i_generation != id->oii_gen) {
403 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
404 "i_generation = %u\n",
405 id->oii_ino, id->oii_gen, inode->i_generation);
407 inode = ERR_PTR(-ESTALE);
408 } else if (inode->i_nlink == 0) {
409 /* due to parallel readdir and unlink,
410 * we can have dead inode here. */
411 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
413 inode = ERR_PTR(-ESTALE);
414 } else if (is_bad_inode(inode)) {
415 CWARN("%.16s: bad inode: ino = %u\n",
416 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
418 inode = ERR_PTR(-ENOENT);
420 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
421 if (id->oii_gen == OSD_OII_NOGEN)
422 osd_id_gen(id, inode->i_ino, inode->i_generation);
424 /* Do not update file c/mtime in ldiskfs.
425 * NB: we don't have any lock to protect this because we don't
426 * have reference on osd_object now, but contention with
427 * another lookup + attr_set can't happen in the tiny window
428 * between if (...) and set S_NOCMTIME. */
429 if (!(inode->i_flags & S_NOCMTIME))
430 inode->i_flags |= S_NOCMTIME;
435 int osd_ldiskfs_add_entry(struct osd_thread_info *info,
436 handle_t *handle, struct dentry *child,
437 struct inode *inode, struct htree_lock *hlock)
441 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
442 if (rc == -ENOBUFS || rc == -ENOSPC) {
443 char fidbuf[FID_LEN + 1];
444 struct lustre_mdt_attrs lma;
445 struct lu_fid fid = { };
447 struct dentry *p_dentry = child->d_parent;
449 rc2 = osd_get_lma(info, p_dentry->d_inode, p_dentry,
452 fid = lma.lma_self_fid;
453 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
454 } else if (rc2 == -ENODATA) {
455 if (unlikely(p_dentry->d_inode ==
456 inode->i_sb->s_root->d_inode))
457 lu_local_obj_fid(&fid, OSD_FS_ROOT_OID);
458 else if (info->oti_dev && !info->oti_dev->od_is_ost &&
459 fid_seq_is_mdt0(fid_seq(&fid)))
460 lu_igif_build(&fid, p_dentry->d_inode->i_ino,
461 p_dentry->d_inode->i_generation);
462 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
464 snprintf(fidbuf, FID_LEN, "%s", "unknown");
468 errstr = "has reached";
470 errstr = "is approaching";
471 CWARN("%.16s: directory (inode: %lu FID: %s) %s maximum entry limit\n",
472 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
473 p_dentry->d_inode->i_ino, fidbuf, errstr);
474 /* ignore such error now */
482 static struct inode *
483 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
484 struct osd_inode_id *id, struct lu_fid *fid)
486 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
490 inode = osd_iget(info, dev, id);
494 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
496 *fid = lma->lma_self_fid;
497 } else if (rc == -ENODATA) {
498 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
499 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
501 lu_igif_build(fid, inode->i_ino, inode->i_generation);
509 static struct inode *osd_iget_check(struct osd_thread_info *info,
510 struct osd_device *dev,
511 const struct lu_fid *fid,
512 struct osd_inode_id *id,
519 /* The cached OI mapping is trustable. If we cannot locate the inode
520 * via the cached OI mapping, then return the failure to the caller
521 * directly without further OI checking. */
523 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
526 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
527 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
536 if (is_bad_inode(inode)) {
539 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
547 if (id->oii_gen != OSD_OII_NOGEN &&
548 inode->i_generation != id->oii_gen) {
551 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
552 "oii_gen = %u, i_generation = %u\n",
553 id->oii_ino, id->oii_gen, inode->i_generation);
561 if (inode->i_nlink == 0) {
564 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
572 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
576 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
578 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
579 /* XXX: There are four possible cases:
581 * Backup/restore caused the OI invalid.
583 * Someone unlinked the object but NOT removed
584 * the OI mapping, such as mount target device
585 * as ldiskfs, and modify something directly.
587 * Someone just removed the object between the
588 * former oi_lookup and the iget. It is normal.
589 * 4. Other failure cases.
591 * Generally, when the device is mounted, it will
592 * auto check whether the system is restored from
593 * file-level backup or not. We trust such detect
594 * to distinguish the 1st case from the 2nd case. */
596 if (!IS_ERR(inode) && inode->i_generation != 0 &&
597 inode->i_generation == id->oii_gen)
598 /* "id->oii_gen != OSD_OII_NOGEN" is for
599 * "@cached == false" case. */
604 /* If the OI mapping was in OI file before the
605 * osd_iget_check(), but now, it is disappear,
606 * then it must be removed by race. That is a
607 * normal race case. */
610 if (id->oii_gen == OSD_OII_NOGEN)
611 osd_id_gen(id, inode->i_ino, inode->i_generation);
613 /* Do not update file c/mtime in ldiskfs.
614 * NB: we don't have any lock to protect this because we don't
615 * have reference on osd_object now, but contention with
616 * another lookup + attr_set can't happen in the tiny window
617 * between if (...) and set S_NOCMTIME. */
618 if (!(inode->i_flags & S_NOCMTIME))
619 inode->i_flags |= S_NOCMTIME;
636 * \retval +v: new filter_fid, does not contain self-fid
637 * \retval 0: filter_fid_old, contains self-fid
638 * \retval -v: other failure cases
640 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
641 struct dentry *dentry, struct lu_fid *fid)
643 struct filter_fid_old *ff = &info->oti_ff;
644 struct ost_id *ostid = &info->oti_ostid;
647 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
648 if (rc == sizeof(*ff)) {
650 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
651 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
652 /* XXX: use 0 as the index for compatibility, the caller will
653 * handle index related issues when necessarry. */
654 ostid_to_fid(fid, ostid, 0);
655 } else if (rc == sizeof(struct filter_fid)) {
657 } else if (rc >= 0) {
664 static int osd_lma_self_repair(struct osd_thread_info *info,
665 struct osd_device *osd, struct inode *inode,
666 const struct lu_fid *fid, __u32 compat)
671 LASSERT(current->journal_info == NULL);
673 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
674 osd_dto_credits_noquota[DTO_XATTR_SET]);
677 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
682 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
684 CWARN("%s: cannot self repair the LMA: rc = %d\n",
686 ldiskfs_journal_stop(jh);
690 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
692 struct osd_thread_info *info = osd_oti_get(env);
693 struct osd_device *osd = osd_obj2dev(obj);
694 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
695 struct inode *inode = obj->oo_inode;
696 struct dentry *dentry = &info->oti_obj_dentry;
697 struct lu_fid *fid = NULL;
698 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
702 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
703 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
704 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
705 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
706 fid = &lma->lma_self_fid;
707 rc = osd_get_idif(info, inode, dentry, fid);
708 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
709 /* For the given OST-object, if it has neither LMA nor
710 * FID in XATTR_NAME_FID, then the given FID (which is
711 * contained in the @obj, from client RPC for locating
712 * the OST-object) is trusted. We use it to generate
714 osd_lma_self_repair(info, osd, inode, rfid,
725 lustre_lma_swab(lma);
726 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
727 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
728 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
729 "fid = "DFID", ino = %lu\n", osd_name(osd),
730 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
731 PFID(rfid), inode->i_ino);
734 fid = &lma->lma_self_fid;
738 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
739 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
740 struct ost_id *oi = &info->oti_ostid;
741 struct lu_fid *fid1 = &info->oti_fid3;
742 __u32 idx = fid_idif_ost_idx(rfid);
744 /* For old IDIF, the OST index is not part of the IDIF,
745 * Means that different OSTs may have the same IDIFs.
746 * Under such case, we need to make some compatible
747 * check to make sure to trigger OI scrub properly. */
748 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
749 /* Given @rfid is new, LMA is old. */
750 fid_to_ostid(fid, oi);
751 ostid_to_fid(fid1, oi, idx);
752 if (lu_fid_eq(fid1, rfid)) {
753 if (osd->od_index_in_idif)
754 osd_lma_self_repair(info, osd,
768 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
769 const struct lu_fid *fid,
770 const struct lu_object_conf *conf)
772 struct osd_thread_info *info;
773 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
774 struct osd_device *dev;
775 struct osd_idmap_cache *oic;
776 struct osd_inode_id *id;
778 struct osd_scrub *scrub;
779 struct scrub_file *sf;
783 bool triggered = false;
786 LINVRNT(osd_invariant(obj));
787 LASSERT(obj->oo_inode == NULL);
788 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
791 scrub = &dev->od_scrub;
792 sf = &scrub->os_file;
793 info = osd_oti_get(env);
795 oic = &info->oti_cache;
797 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
800 /* For the object is created as locking anchor, or for the object to
801 * be created on disk. No need to osd_oi_lookup() at here because FID
802 * shouldn't never be re-used, if it's really a duplicate FID from
803 * unexpected reason, we should be able to detect it later by calling
804 * do_create->osd_oi_insert(). */
805 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
806 GOTO(out, result = 0);
808 /* Search order: 1. per-thread cache. */
809 if (lu_fid_eq(fid, &oic->oic_fid) &&
810 likely(oic->oic_dev == dev)) {
816 if (!list_empty(&scrub->os_inconsistent_items)) {
817 /* Search order: 2. OI scrub pending list. */
818 result = osd_oii_lookup(dev, fid, id);
824 /* Search order: 3. OI files. */
825 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
826 if (result == -ENOENT) {
827 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
828 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
829 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
831 GOTO(out, result = 0);
840 inode = osd_iget_check(info, dev, fid, id, cached);
842 result = PTR_ERR(inode);
843 if (result == -ENOENT || result == -ESTALE)
844 GOTO(out, result = -ENOENT);
846 if (result == -EREMCHG) {
849 if (unlikely(triggered))
850 GOTO(out, result = saved);
853 if (thread_is_running(&scrub->os_thread)) {
854 result = -EINPROGRESS;
855 } else if (!dev->od_noscrub) {
856 result = osd_scrub_start(dev, SS_AUTO_FULL |
857 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
858 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
859 "for "DFID", rc = %d [1]\n",
860 osd_name(dev), PFID(fid), result);
861 if (result == 0 || result == -EALREADY)
862 result = -EINPROGRESS;
869 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD))
872 /* We still have chance to get the valid inode: for the
873 * object which is referenced by remote name entry, the
874 * object on the local MDT will be linked under the dir
875 * of "/REMOTE_PARENT_DIR" with its FID string as name.
877 * We do not know whether the object for the given FID
878 * is referenced by some remote name entry or not, and
879 * especially for DNE II, a multiple-linked object may
880 * have many name entries reside on many MDTs.
882 * To simplify the operation, OSD will not distinguish
883 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
884 * only happened for the RPC from other MDT during the
885 * OI scrub, or for the client side RPC with FID only,
886 * such as FID to path, or from old connected client. */
888 result = osd_lookup_in_remote_parent(info, dev,
901 obj->oo_inode = inode;
902 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
904 result = osd_check_lma(env, obj);
906 if (result == -ENODATA) {
908 result = osd_oi_lookup(info, dev, fid, id,
911 /* result == -ENOENT means that the OI
912 * mapping has been removed by race,
913 * the target inode belongs to other
916 * Others error also can be returned
919 obj->oo_inode = NULL;
922 /* result == 0 means the cached OI
923 * mapping is still in the OI file,
924 * the target the inode is valid. */
927 /* The current OI mapping is from the OI file,
928 * since the inode has been found via
929 * osd_iget_check(), no need recheck OI. */
936 obj->oo_inode = NULL;
937 if (result != -EREMCHG)
941 result = osd_oi_lookup(info, dev, fid, id,
943 /* result == -ENOENT means the cached OI mapping
944 * has been removed from the OI file by race,
945 * above target inode belongs to other object.
947 * Others error also can be returned directly. */
951 /* result == 0, goto trigger */
953 /* The current OI mapping is from the OI file,
954 * since the inode has been found via
955 * osd_iget_check(), no need recheck OI. */
962 obj->oo_compat_dot_created = 1;
963 obj->oo_compat_dotdot_created = 1;
965 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
966 GOTO(out, result = 0);
968 LASSERT(obj->oo_hl_head == NULL);
969 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
970 if (obj->oo_hl_head == NULL) {
971 obj->oo_inode = NULL;
973 GOTO(out, result = -ENOMEM);
975 GOTO(out, result = 0);
978 if (result != 0 && cached)
979 fid_zero(&oic->oic_fid);
981 LINVRNT(osd_invariant(obj));
986 * Concurrency: shouldn't matter.
988 static void osd_object_init0(struct osd_object *obj)
990 LASSERT(obj->oo_inode != NULL);
991 obj->oo_dt.do_body_ops = &osd_body_ops;
992 obj->oo_dt.do_lu.lo_header->loh_attr |=
993 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
997 * Concurrency: no concurrent access is possible that early in object
1000 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1001 const struct lu_object_conf *conf)
1003 struct osd_object *obj = osd_obj(l);
1006 LINVRNT(osd_invariant(obj));
1008 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1009 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1010 l->lo_header->loh_attr |= LOHA_EXISTS;
1014 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1015 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1016 if (result == 0 && obj->oo_inode != NULL) {
1017 struct osd_thread_info *oti = osd_oti_get(env);
1018 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1020 osd_object_init0(obj);
1021 result = osd_get_lma(oti, obj->oo_inode,
1022 &oti->oti_obj_dentry, lma);
1024 /* Convert LMAI flags to lustre LMA flags
1025 * and cache it to oo_lma_flags */
1027 lma_to_lustre_flags(lma->lma_incompat);
1028 } else if (result == -ENODATA) {
1033 LINVRNT(osd_invariant(obj));
1038 * Concurrency: no concurrent access is possible that late in object
1041 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1043 struct osd_object *obj = osd_obj(l);
1045 LINVRNT(osd_invariant(obj));
1047 dt_object_fini(&obj->oo_dt);
1048 if (obj->oo_hl_head != NULL)
1049 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1054 * Concurrency: no concurrent access is possible that late in object
1057 static void osd_index_fini(struct osd_object *o)
1059 struct iam_container *bag;
1061 if (o->oo_dir != NULL) {
1062 bag = &o->oo_dir->od_container;
1063 if (o->oo_inode != NULL) {
1064 if (bag->ic_object == o->oo_inode)
1065 iam_container_fini(bag);
1067 OBD_FREE_PTR(o->oo_dir);
1073 * Concurrency: no concurrent access is possible that late in object
1074 * life-cycle (for all existing callers, that is. New callers have to provide
1075 * their own locking.)
1077 static int osd_inode_unlinked(const struct inode *inode)
1079 return inode->i_nlink == 0;
1083 OSD_TXN_OI_DELETE_CREDITS = 20,
1084 OSD_TXN_INODE_DELETE_CREDITS = 20
1091 #if OSD_THANDLE_STATS
1093 * Set time when the handle is allocated
1095 static void osd_th_alloced(struct osd_thandle *oth)
1097 oth->oth_alloced = cfs_time_current();
1101 * Set time when the handle started
1103 static void osd_th_started(struct osd_thandle *oth)
1105 oth->oth_started = cfs_time_current();
1109 * Helper function to convert time interval to microseconds packed in
1112 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1116 cfs_duration_usec(cfs_time_sub(end, start), &val);
1117 return val.tv_sec * 1000000 + val.tv_usec;
1121 * Check whether the we deal with this handle for too long.
1123 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1124 cfs_time_t alloced, cfs_time_t started,
1127 cfs_time_t now = cfs_time_current();
1129 LASSERT(dev != NULL);
1131 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1132 interval_to_usec(alloced, started));
1133 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1134 interval_to_usec(started, closed));
1135 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1136 interval_to_usec(closed, now));
1138 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1139 CWARN("transaction handle %p was open for too long: "
1140 "now "CFS_TIME_T" ,"
1141 "alloced "CFS_TIME_T" ,"
1142 "started "CFS_TIME_T" ,"
1143 "closed "CFS_TIME_T"\n",
1144 oth, now, alloced, started, closed);
1145 libcfs_debug_dumpstack(NULL);
1149 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1151 cfs_time_t __closed = cfs_time_current(); \
1152 cfs_time_t __alloced = oth->oth_alloced; \
1153 cfs_time_t __started = oth->oth_started; \
1156 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1159 #else /* OSD_THANDLE_STATS */
1161 #define osd_th_alloced(h) do {} while(0)
1162 #define osd_th_started(h) do {} while(0)
1163 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1165 #endif /* OSD_THANDLE_STATS */
1168 * Concurrency: doesn't access mutable data.
1170 static int osd_param_is_not_sane(const struct osd_device *dev,
1171 const struct thandle *th)
1173 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1175 return oh->ot_credits > osd_transaction_size(dev);
1179 * Concurrency: shouldn't matter.
1181 static void osd_trans_commit_cb(struct super_block *sb,
1182 struct ldiskfs_journal_cb_entry *jcb, int error)
1184 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1185 struct thandle *th = &oh->ot_super;
1186 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1187 struct dt_txn_commit_cb *dcb, *tmp;
1189 LASSERT(oh->ot_handle == NULL);
1192 CERROR("transaction @0x%p commit error: %d\n", th, error);
1194 dt_txn_hook_commit(th);
1196 /* call per-transaction callbacks if any */
1197 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1199 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1200 "commit callback entry: magic=%x name='%s'\n",
1201 dcb->dcb_magic, dcb->dcb_name);
1202 list_del_init(&dcb->dcb_linkage);
1203 dcb->dcb_func(NULL, th, dcb, error);
1206 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1210 lu_context_exit(&th->th_ctx);
1211 lu_context_fini(&th->th_ctx);
1215 static struct thandle *osd_trans_create(const struct lu_env *env,
1216 struct dt_device *d)
1218 struct osd_thread_info *oti = osd_oti_get(env);
1219 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1220 struct osd_thandle *oh;
1224 /* on pending IO in this thread should left from prev. request */
1225 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1227 th = ERR_PTR(-ENOMEM);
1228 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1230 oh->ot_quota_trans = &oti->oti_quota_trans;
1231 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1235 th->th_tags = LCT_TX_HANDLE;
1237 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1238 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1241 memset(oti->oti_declare_ops, 0,
1242 sizeof(oti->oti_declare_ops));
1243 memset(oti->oti_declare_ops_cred, 0,
1244 sizeof(oti->oti_declare_ops_cred));
1245 memset(oti->oti_declare_ops_used, 0,
1246 sizeof(oti->oti_declare_ops_used));
1251 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1253 struct osd_thread_info *oti = osd_oti_get(env);
1254 struct osd_thandle *oh;
1256 oh = container_of0(th, struct osd_thandle, ot_super);
1257 LASSERT(oh != NULL);
1259 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1260 oti->oti_declare_ops[OSD_OT_CREATE],
1261 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1262 oti->oti_declare_ops_used[OSD_OT_CREATE],
1263 oti->oti_declare_ops[OSD_OT_DESTROY],
1264 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1265 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1266 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1267 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1268 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1269 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1270 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1271 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1272 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1273 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1274 oti->oti_declare_ops[OSD_OT_WRITE],
1275 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1276 oti->oti_declare_ops_used[OSD_OT_WRITE],
1277 oti->oti_declare_ops[OSD_OT_PUNCH],
1278 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1279 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1280 oti->oti_declare_ops[OSD_OT_QUOTA],
1281 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1282 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1283 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1284 oti->oti_declare_ops[OSD_OT_INSERT],
1285 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1286 oti->oti_declare_ops_used[OSD_OT_INSERT],
1287 oti->oti_declare_ops[OSD_OT_DELETE],
1288 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1289 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1290 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1291 oti->oti_declare_ops[OSD_OT_REF_ADD],
1292 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1293 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1294 oti->oti_declare_ops[OSD_OT_REF_DEL],
1295 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1296 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1300 * Concurrency: shouldn't matter.
1302 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1305 struct osd_thread_info *oti = osd_oti_get(env);
1306 struct osd_device *dev = osd_dt_dev(d);
1308 struct osd_thandle *oh;
1313 LASSERT(current->journal_info == NULL);
1315 oh = container_of0(th, struct osd_thandle, ot_super);
1316 LASSERT(oh != NULL);
1317 LASSERT(oh->ot_handle == NULL);
1319 rc = dt_txn_hook_start(env, d, th);
1323 if (unlikely(osd_param_is_not_sane(dev, th))) {
1324 static unsigned long last_printed;
1325 static int last_credits;
1327 CWARN("%.16s: too many transaction credits (%d > %d)\n",
1328 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
1330 osd_journal(dev)->j_max_transaction_buffers);
1332 osd_trans_dump_creds(env, th);
1334 if (last_credits != oh->ot_credits &&
1335 time_after(jiffies, last_printed +
1336 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
1337 libcfs_debug_dumpstack(NULL);
1338 last_credits = oh->ot_credits;
1339 last_printed = jiffies;
1341 /* XXX Limit the credits to 'max_transaction_buffers', and
1342 * let the underlying filesystem to catch the error if
1343 * we really need so many credits.
1345 * This should be removed when we can calculate the
1346 * credits precisely. */
1347 oh->ot_credits = osd_transaction_size(dev);
1351 * XXX temporary stuff. Some abstraction layer should
1354 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1358 LASSERT(oti->oti_txns == 0);
1359 lu_context_init(&th->th_ctx, th->th_tags);
1360 lu_context_enter(&th->th_ctx);
1362 lu_device_get(&d->dd_lu_dev);
1363 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1374 static int osd_seq_exists(const struct lu_env *env,
1375 struct osd_device *osd, u64 seq)
1377 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1378 struct seq_server_site *ss = osd_seq_site(osd);
1382 LASSERT(ss != NULL);
1383 LASSERT(ss->ss_server_fld != NULL);
1385 rc = osd_fld_lookup(env, osd, seq, range);
1388 CERROR("%s: can't lookup FLD sequence "LPX64
1389 ": rc = %d\n", osd_name(osd), seq, rc);
1393 RETURN(ss->ss_node_id == range->lsr_index);
1396 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1398 struct dt_txn_commit_cb *dcb;
1399 struct dt_txn_commit_cb *tmp;
1401 /* call per-transaction stop callbacks if any */
1402 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1404 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1405 "commit callback entry: magic=%x name='%s'\n",
1406 dcb->dcb_magic, dcb->dcb_name);
1407 list_del_init(&dcb->dcb_linkage);
1408 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1413 * Concurrency: shouldn't matter.
1415 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1418 int rc = 0, remove_agents = 0;
1419 struct osd_thandle *oh;
1420 struct osd_thread_info *oti = osd_oti_get(env);
1421 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1422 struct osd_device *osd = osd_dt_dev(th->th_dev);
1423 struct qsd_instance *qsd = osd->od_quota_slave;
1424 struct lquota_trans *qtrans;
1427 oh = container_of0(th, struct osd_thandle, ot_super);
1429 /* reset OI cache for safety */
1430 oti->oti_ins_cache_used = 0;
1432 remove_agents = oh->ot_remove_agents;
1434 qtrans = oh->ot_quota_trans;
1435 oh->ot_quota_trans = NULL;
1437 if (oh->ot_handle != NULL) {
1438 handle_t *hdl = oh->ot_handle;
1441 * add commit callback
1442 * notice we don't do this in osd_trans_start()
1443 * as underlying transaction can change during truncate
1445 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1448 LASSERT(oti->oti_txns == 1);
1451 rc = dt_txn_hook_stop(env, th);
1453 CERROR("%s: failed in transaction hook: rc = %d\n",
1456 osd_trans_stop_cb(oh, rc);
1457 /* hook functions might modify th_sync */
1458 hdl->h_sync = th->th_sync;
1460 oh->ot_handle = NULL;
1461 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1463 CERROR("%s: failed to stop transaction: rc = %d\n",
1466 osd_trans_stop_cb(oh, th->th_result);
1470 /* inform the quota slave device that the transaction is stopping */
1471 qsd_op_end(env, qsd, qtrans);
1473 /* as we want IO to journal and data IO be concurrent, we don't block
1474 * awaiting data IO completion in osd_do_bio(), instead we wait here
1475 * once transaction is submitted to the journal. all reqular requests
1476 * don't do direct IO (except read/write), thus this wait_event becomes
1479 * IMPORTANT: we have to wait till any IO submited by the thread is
1480 * completed otherwise iobuf may be corrupted by different request
1482 wait_event(iobuf->dr_wait,
1483 atomic_read(&iobuf->dr_numreqs) == 0);
1484 osd_fini_iobuf(osd, iobuf);
1486 rc = iobuf->dr_error;
1488 if (unlikely(remove_agents != 0))
1489 osd_process_scheduled_agent_removals(env, osd);
1494 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1496 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1499 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1500 LASSERT(&dcb->dcb_func != NULL);
1501 if (dcb->dcb_flags & DCB_TRANS_STOP)
1502 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1504 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1510 * Called just before object is freed. Releases all resources except for
1511 * object itself (that is released by osd_object_free()).
1513 * Concurrency: no concurrent access is possible that late in object
1516 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1518 struct osd_object *obj = osd_obj(l);
1519 struct inode *inode = obj->oo_inode;
1521 LINVRNT(osd_invariant(obj));
1524 * If object is unlinked remove fid->ino mapping from object index.
1527 osd_index_fini(obj);
1528 if (inode != NULL) {
1529 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1530 qid_t uid = i_uid_read(inode);
1531 qid_t gid = i_gid_read(inode);
1534 obj->oo_inode = NULL;
1537 struct osd_thread_info *info = osd_oti_get(env);
1538 struct lquota_id_info *qi = &info->oti_qi;
1540 /* Release granted quota to master if necessary */
1541 qi->lqi_id.qid_uid = uid;
1542 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1544 qi->lqi_id.qid_uid = gid;
1545 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1551 * Concurrency: ->loo_object_release() is called under site spin-lock.
1553 static void osd_object_release(const struct lu_env *env,
1554 struct lu_object *l)
1559 * Concurrency: shouldn't matter.
1561 static int osd_object_print(const struct lu_env *env, void *cookie,
1562 lu_printer_t p, const struct lu_object *l)
1564 struct osd_object *o = osd_obj(l);
1565 struct iam_descr *d;
1567 if (o->oo_dir != NULL)
1568 d = o->oo_dir->od_container.ic_descr;
1571 return (*p)(env, cookie,
1572 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1574 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1575 o->oo_inode ? o->oo_inode->i_generation : 0,
1576 d ? d->id_ops->id_name : "plain");
1579 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1582 * Concurrency: shouldn't matter.
1584 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1585 struct obd_statfs *sfs)
1587 struct osd_device *osd = osd_dt_dev(d);
1588 struct super_block *sb = osd_sb(osd);
1589 struct kstatfs *ksfs;
1592 if (unlikely(osd->od_mnt == NULL))
1593 return -EINPROGRESS;
1595 /* osd_lproc.c call this without env, allocate ksfs for that case */
1596 if (unlikely(env == NULL)) {
1597 OBD_ALLOC_PTR(ksfs);
1601 ksfs = &osd_oti_get(env)->oti_ksfs;
1604 spin_lock(&osd->od_osfs_lock);
1605 result = sb->s_op->statfs(sb->s_root, ksfs);
1606 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1607 statfs_pack(sfs, ksfs);
1608 if (unlikely(sb->s_flags & MS_RDONLY))
1609 sfs->os_state = OS_STATE_READONLY;
1610 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1611 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1612 sfs->os_maxbytes = sb->s_maxbytes;
1614 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1616 spin_unlock(&osd->od_osfs_lock);
1618 if (unlikely(env == NULL))
1621 /* Reserve a small amount of space for local objects like last_rcvd,
1622 * llog, quota files, ... */
1623 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1626 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1627 /** Take out metadata overhead for indirect blocks */
1628 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1635 * Estimate space needed for file creations. We assume the largest filename
1636 * which is 2^64 - 1, hence a filename of 20 chars.
1637 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1639 #ifdef __LDISKFS_DIR_REC_LEN
1640 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1642 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1646 * Concurrency: doesn't access mutable data.
1648 static void osd_conf_get(const struct lu_env *env,
1649 const struct dt_device *dev,
1650 struct dt_device_param *param)
1652 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1656 * XXX should be taken from not-yet-existing fs abstraction layer.
1658 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1659 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1660 param->ddp_block_shift = sb->s_blocksize_bits;
1661 param->ddp_mount_type = LDD_MT_LDISKFS;
1662 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1663 param->ddp_maxbytes = sb->s_maxbytes;
1665 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1666 /* Overhead estimate should be fairly accurate, so we really take a tiny
1667 * error margin which also avoids fragmenting the filesystem too much */
1668 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1669 /* inode are statically allocated, so per-inode space consumption
1670 * is the space consumed by the directory entry */
1671 param->ddp_inodespace = PER_OBJ_USAGE;
1672 /* per-fragment overhead to be used by the client code */
1673 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1674 param->ddp_mntopts = 0;
1675 if (test_opt(sb, XATTR_USER))
1676 param->ddp_mntopts |= MNTOPT_USERXATTR;
1677 if (test_opt(sb, POSIX_ACL))
1678 param->ddp_mntopts |= MNTOPT_ACL;
1680 /* LOD might calculate the max stripe count based on max_ea_size,
1681 * so we need take account in the overhead as well,
1682 * xattr_header + magic + xattr_entry_head */
1683 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1684 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1686 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1687 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1688 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1692 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1696 * Concurrency: shouldn't matter.
1698 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1702 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1704 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1706 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1707 LUSTRE_OSD_LDISKFS_NAME, rc);
1713 * Start commit for OSD device.
1715 * An implementation of dt_commit_async method for OSD device.
1716 * Asychronously starts underlayng fs sync and thereby a transaction
1719 * \param env environment
1720 * \param d dt device
1722 * \see dt_device_operations
1724 static int osd_commit_async(const struct lu_env *env,
1725 struct dt_device *d)
1727 struct super_block *s = osd_sb(osd_dt_dev(d));
1730 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1731 RETURN(s->s_op->sync_fs(s, 0));
1735 * Concurrency: shouldn't matter.
1738 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1740 struct super_block *sb = osd_sb(osd_dt_dev(d));
1741 struct block_device *dev = sb->s_bdev;
1742 #ifdef HAVE_DEV_SET_RDONLY
1743 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1746 int rc = -EOPNOTSUPP;
1750 #ifdef HAVE_DEV_SET_RDONLY
1751 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1753 if (jdev && (jdev != dev)) {
1754 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1756 dev_set_rdonly(jdev);
1758 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1759 dev_set_rdonly(dev);
1761 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1762 osd_dt_dev(d)->od_svname, (long)dev, rc);
1768 * Note: we do not count into QUOTA here.
1769 * If we mount with --data_journal we may need more.
1771 const int osd_dto_credits_noquota[DTO_NR] = {
1774 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1775 * SINGLEDATA_TRANS_BLOCKS(8)
1776 * XXX Note: maybe iam need more, since iam have more level than
1779 [DTO_INDEX_INSERT] = 16,
1782 * just modify a single entry, probably merge few within a block
1784 [DTO_INDEX_DELETE] = 1,
1788 [DTO_INDEX_UPDATE] = 16,
1790 * 4(inode, inode bits, groups, GDT)
1791 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1793 [DTO_OBJECT_CREATE] = 4,
1795 * 4(inode, inode bits, groups, GDT)
1796 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1798 [DTO_OBJECT_DELETE] = 4,
1800 * Attr set credits (inode)
1802 [DTO_ATTR_SET_BASE] = 1,
1804 * Xattr set. The same as xattr of EXT3.
1805 * DATA_TRANS_BLOCKS(14)
1806 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1807 * are also counted in. Do not know why?
1809 [DTO_XATTR_SET] = 14,
1811 * credits for inode change during write.
1813 [DTO_WRITE_BASE] = 3,
1815 * credits for single block write.
1817 [DTO_WRITE_BLOCK] = 14,
1819 * Attr set credits for chown.
1820 * This is extra credits for setattr, and it is null without quota
1822 [DTO_ATTR_SET_CHOWN] = 0
1825 static const struct dt_device_operations osd_dt_ops = {
1826 .dt_root_get = osd_root_get,
1827 .dt_statfs = osd_statfs,
1828 .dt_trans_create = osd_trans_create,
1829 .dt_trans_start = osd_trans_start,
1830 .dt_trans_stop = osd_trans_stop,
1831 .dt_trans_cb_add = osd_trans_cb_add,
1832 .dt_conf_get = osd_conf_get,
1833 .dt_sync = osd_sync,
1835 .dt_commit_async = osd_commit_async,
1838 static void osd_object_read_lock(const struct lu_env *env,
1839 struct dt_object *dt, unsigned role)
1841 struct osd_object *obj = osd_dt_obj(dt);
1842 struct osd_thread_info *oti = osd_oti_get(env);
1844 LINVRNT(osd_invariant(obj));
1846 LASSERT(obj->oo_owner != env);
1847 down_read_nested(&obj->oo_sem, role);
1849 LASSERT(obj->oo_owner == NULL);
1853 static void osd_object_write_lock(const struct lu_env *env,
1854 struct dt_object *dt, unsigned role)
1856 struct osd_object *obj = osd_dt_obj(dt);
1857 struct osd_thread_info *oti = osd_oti_get(env);
1859 LINVRNT(osd_invariant(obj));
1861 LASSERT(obj->oo_owner != env);
1862 down_write_nested(&obj->oo_sem, role);
1864 LASSERT(obj->oo_owner == NULL);
1865 obj->oo_owner = env;
1869 static void osd_object_read_unlock(const struct lu_env *env,
1870 struct dt_object *dt)
1872 struct osd_object *obj = osd_dt_obj(dt);
1873 struct osd_thread_info *oti = osd_oti_get(env);
1875 LINVRNT(osd_invariant(obj));
1877 LASSERT(oti->oti_r_locks > 0);
1879 up_read(&obj->oo_sem);
1882 static void osd_object_write_unlock(const struct lu_env *env,
1883 struct dt_object *dt)
1885 struct osd_object *obj = osd_dt_obj(dt);
1886 struct osd_thread_info *oti = osd_oti_get(env);
1888 LINVRNT(osd_invariant(obj));
1890 LASSERT(obj->oo_owner == env);
1891 LASSERT(oti->oti_w_locks > 0);
1893 obj->oo_owner = NULL;
1894 up_write(&obj->oo_sem);
1897 static int osd_object_write_locked(const struct lu_env *env,
1898 struct dt_object *dt)
1900 struct osd_object *obj = osd_dt_obj(dt);
1902 LINVRNT(osd_invariant(obj));
1904 return obj->oo_owner == env;
1907 static struct timespec *osd_inode_time(const struct lu_env *env,
1908 struct inode *inode, __u64 seconds)
1910 struct osd_thread_info *oti = osd_oti_get(env);
1911 struct timespec *t = &oti->oti_time;
1913 t->tv_sec = seconds;
1915 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1919 static void osd_inode_getattr(const struct lu_env *env,
1920 struct inode *inode, struct lu_attr *attr)
1922 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1923 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1924 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1927 attr->la_atime = LTIME_S(inode->i_atime);
1928 attr->la_mtime = LTIME_S(inode->i_mtime);
1929 attr->la_ctime = LTIME_S(inode->i_ctime);
1930 attr->la_mode = inode->i_mode;
1931 attr->la_size = i_size_read(inode);
1932 attr->la_blocks = inode->i_blocks;
1933 attr->la_uid = i_uid_read(inode);
1934 attr->la_gid = i_gid_read(inode);
1935 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
1936 attr->la_nlink = inode->i_nlink;
1937 attr->la_rdev = inode->i_rdev;
1938 attr->la_blksize = 1 << inode->i_blkbits;
1939 attr->la_blkbits = inode->i_blkbits;
1942 static int osd_attr_get(const struct lu_env *env,
1943 struct dt_object *dt,
1944 struct lu_attr *attr)
1946 struct osd_object *obj = osd_dt_obj(dt);
1948 if (unlikely(!dt_object_exists(dt)))
1950 if (unlikely(obj->oo_destroyed))
1953 LASSERT(!dt_object_remote(dt));
1954 LINVRNT(osd_invariant(obj));
1956 spin_lock(&obj->oo_guard);
1957 osd_inode_getattr(env, obj->oo_inode, attr);
1958 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
1959 attr->la_flags |= LUSTRE_ORPHAN_FL;
1960 spin_unlock(&obj->oo_guard);
1965 static int osd_declare_attr_set(const struct lu_env *env,
1966 struct dt_object *dt,
1967 const struct lu_attr *attr,
1968 struct thandle *handle)
1970 struct osd_thandle *oh;
1971 struct osd_object *obj;
1972 struct osd_thread_info *info = osd_oti_get(env);
1973 struct lquota_id_info *qi = &info->oti_qi;
1981 LASSERT(dt != NULL);
1982 LASSERT(handle != NULL);
1984 obj = osd_dt_obj(dt);
1985 LASSERT(osd_invariant(obj));
1987 oh = container_of0(handle, struct osd_thandle, ot_super);
1988 LASSERT(oh->ot_handle == NULL);
1990 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1991 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1993 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
1994 osd_dto_credits_noquota[DTO_XATTR_SET]);
1996 if (attr == NULL || obj->oo_inode == NULL)
1999 bspace = obj->oo_inode->i_blocks;
2000 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2001 bspace = toqb(bspace);
2003 /* Changing ownership is always preformed by super user, it should not
2006 * We still need to call the osd_declare_qid() to calculate the journal
2007 * credits for updating quota accounting files and to trigger quota
2008 * space adjustment once the operation is completed.*/
2009 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2011 uid = i_uid_read(obj->oo_inode);
2012 qi->lqi_type = USRQUOTA;
2013 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2014 /* inode accounting */
2015 qi->lqi_is_blk = false;
2017 /* one more inode for the new uid ... */
2018 qi->lqi_id.qid_uid = attr->la_uid;
2020 /* Reserve credits for the new uid */
2021 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2022 if (rc == -EDQUOT || rc == -EINPROGRESS)
2027 /* and one less inode for the current uid */
2028 qi->lqi_id.qid_uid = uid;
2030 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2031 if (rc == -EDQUOT || rc == -EINPROGRESS)
2036 /* block accounting */
2037 qi->lqi_is_blk = true;
2039 /* more blocks for the new uid ... */
2040 qi->lqi_id.qid_uid = attr->la_uid;
2041 qi->lqi_space = bspace;
2043 * Credits for the new uid has been reserved, re-use "obj"
2044 * to save credit reservation.
2046 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2047 if (rc == -EDQUOT || rc == -EINPROGRESS)
2052 /* and finally less blocks for the current uid */
2053 qi->lqi_id.qid_uid = uid;
2054 qi->lqi_space = -bspace;
2055 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2056 if (rc == -EDQUOT || rc == -EINPROGRESS)
2062 gid = i_gid_read(obj->oo_inode);
2063 qi->lqi_type = GRPQUOTA;
2064 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2066 /* inode accounting */
2067 qi->lqi_is_blk = false;
2069 /* one more inode for the new gid ... */
2070 qi->lqi_id.qid_gid = attr->la_gid;
2072 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2073 if (rc == -EDQUOT || rc == -EINPROGRESS)
2078 /* and one less inode for the current gid */
2079 qi->lqi_id.qid_gid = gid;
2081 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2082 if (rc == -EDQUOT || rc == -EINPROGRESS)
2087 /* block accounting */
2088 qi->lqi_is_blk = true;
2090 /* more blocks for the new gid ... */
2091 qi->lqi_id.qid_gid = attr->la_gid;
2092 qi->lqi_space = bspace;
2093 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2094 if (rc == -EDQUOT || rc == -EINPROGRESS)
2099 /* and finally less blocks for the current gid */
2100 qi->lqi_id.qid_gid = gid;
2101 qi->lqi_space = -bspace;
2102 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2103 if (rc == -EDQUOT || rc == -EINPROGRESS)
2112 static int osd_inode_setattr(const struct lu_env *env,
2113 struct inode *inode, const struct lu_attr *attr)
2115 __u64 bits = attr->la_valid;
2117 /* Only allow set size for regular file */
2118 if (!S_ISREG(inode->i_mode))
2119 bits &= ~(LA_SIZE | LA_BLOCKS);
2124 if (bits & LA_ATIME)
2125 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2126 if (bits & LA_CTIME)
2127 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2128 if (bits & LA_MTIME)
2129 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2130 if (bits & LA_SIZE) {
2131 LDISKFS_I(inode)->i_disksize = attr->la_size;
2132 i_size_write(inode, attr->la_size);
2136 /* OSD should not change "i_blocks" which is used by quota.
2137 * "i_blocks" should be changed by ldiskfs only. */
2138 if (bits & LA_BLOCKS)
2139 inode->i_blocks = attr->la_blocks;
2142 inode->i_mode = (inode->i_mode & S_IFMT) |
2143 (attr->la_mode & ~S_IFMT);
2145 i_uid_write(inode, attr->la_uid);
2147 i_gid_write(inode, attr->la_gid);
2148 if (bits & LA_NLINK)
2149 set_nlink(inode, attr->la_nlink);
2151 inode->i_rdev = attr->la_rdev;
2153 if (bits & LA_FLAGS) {
2154 /* always keep S_NOCMTIME */
2155 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2161 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2163 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2164 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2168 ll_vfs_dq_init(inode);
2170 if (attr->la_valid & LA_UID)
2171 iattr.ia_valid |= ATTR_UID;
2172 if (attr->la_valid & LA_GID)
2173 iattr.ia_valid |= ATTR_GID;
2174 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2175 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2177 rc = ll_vfs_dq_transfer(inode, &iattr);
2179 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2180 "enforcement enabled on the ldiskfs "
2181 "filesystem?\n", inode->i_sb->s_id, rc);
2188 static int osd_attr_set(const struct lu_env *env,
2189 struct dt_object *dt,
2190 const struct lu_attr *attr,
2191 struct thandle *handle)
2193 struct osd_object *obj = osd_dt_obj(dt);
2194 struct inode *inode;
2197 if (!dt_object_exists(dt))
2200 LASSERT(handle != NULL);
2201 LASSERT(!dt_object_remote(dt));
2202 LASSERT(osd_invariant(obj));
2204 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2206 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2207 struct osd_thread_info *oti = osd_oti_get(env);
2208 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2209 struct lu_fid *fid1 = &oti->oti_fid;
2210 struct osd_inode_id *id = &oti->oti_id;
2211 struct iam_path_descr *ipd;
2212 struct iam_container *bag;
2213 struct osd_thandle *oh;
2216 fid_cpu_to_be(fid1, fid0);
2217 memset(id, 1, sizeof(*id));
2218 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2219 fid0)->oi_dir.od_container;
2220 ipd = osd_idx_ipd_get(env, bag);
2221 if (unlikely(ipd == NULL))
2224 oh = container_of0(handle, struct osd_thandle, ot_super);
2225 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2226 (const struct iam_rec *)id, ipd);
2227 osd_ipd_put(env, bag, ipd);
2228 return(rc > 0 ? 0 : rc);
2231 inode = obj->oo_inode;
2233 rc = osd_quota_transfer(inode, attr);
2237 spin_lock(&obj->oo_guard);
2238 rc = osd_inode_setattr(env, inode, attr);
2239 spin_unlock(&obj->oo_guard);
2243 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2245 if (!(attr->la_valid & LA_FLAGS))
2248 /* Let's check if there are extra flags need to be set into LMA */
2249 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2250 struct osd_thread_info *info = osd_oti_get(env);
2251 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2253 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2257 lma->lma_incompat |=
2258 lustre_to_lma_flags(attr->la_flags);
2259 lustre_lma_swab(lma);
2260 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2261 lma, sizeof(*lma), XATTR_REPLACE);
2263 struct osd_device *osd = osd_obj2dev(obj);
2265 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2266 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2267 lma->lma_incompat, rc);
2270 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2272 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2275 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2280 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2281 struct osd_object *obj,
2282 const char *name, const int namelen)
2284 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2287 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2288 umode_t mode, struct dt_allocation_hint *hint,
2292 struct osd_device *osd = osd_obj2dev(obj);
2293 struct osd_thandle *oth;
2294 struct dt_object *parent = NULL;
2295 struct inode *inode;
2297 LINVRNT(osd_invariant(obj));
2298 LASSERT(obj->oo_inode == NULL);
2299 LASSERT(obj->oo_hl_head == NULL);
2301 if (S_ISDIR(mode) && ldiskfs_pdo) {
2302 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2303 if (obj->oo_hl_head == NULL)
2307 oth = container_of(th, struct osd_thandle, ot_super);
2308 LASSERT(oth->ot_handle->h_transaction != NULL);
2310 if (hint != NULL && hint->dah_parent != NULL &&
2311 !dt_object_remote(hint->dah_parent))
2312 parent = hint->dah_parent;
2314 inode = ldiskfs_create_inode(oth->ot_handle,
2315 parent ? osd_dt_obj(parent)->oo_inode :
2316 osd_sb(osd)->s_root->d_inode,
2318 if (!IS_ERR(inode)) {
2319 /* Do not update file c/mtime in ldiskfs. */
2320 inode->i_flags |= S_NOCMTIME;
2322 /* For new created object, it must be consistent,
2323 * and it is unnecessary to scrub against it. */
2324 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2326 obj->oo_inode = inode;
2329 if (obj->oo_hl_head != NULL) {
2330 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2331 obj->oo_hl_head = NULL;
2333 result = PTR_ERR(inode);
2335 LINVRNT(osd_invariant(obj));
2343 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2344 struct lu_attr *attr,
2345 struct dt_allocation_hint *hint,
2346 struct dt_object_format *dof,
2350 struct osd_thandle *oth;
2351 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2353 LASSERT(S_ISDIR(attr->la_mode));
2355 oth = container_of(th, struct osd_thandle, ot_super);
2356 LASSERT(oth->ot_handle->h_transaction != NULL);
2357 result = osd_mkfile(info, obj, mode, hint, th);
2362 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2363 struct lu_attr *attr,
2364 struct dt_allocation_hint *hint,
2365 struct dt_object_format *dof,
2369 struct osd_thandle *oth;
2370 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2372 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2374 LASSERT(S_ISREG(attr->la_mode));
2376 oth = container_of(th, struct osd_thandle, ot_super);
2377 LASSERT(oth->ot_handle->h_transaction != NULL);
2379 result = osd_mkfile(info, obj, mode, hint, th);
2381 LASSERT(obj->oo_inode != NULL);
2382 if (feat->dif_flags & DT_IND_VARKEY)
2383 result = iam_lvar_create(obj->oo_inode,
2384 feat->dif_keysize_max,
2386 feat->dif_recsize_max,
2389 result = iam_lfix_create(obj->oo_inode,
2390 feat->dif_keysize_max,
2392 feat->dif_recsize_max,
2399 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2400 struct lu_attr *attr,
2401 struct dt_allocation_hint *hint,
2402 struct dt_object_format *dof,
2405 LASSERT(S_ISREG(attr->la_mode));
2406 return osd_mkfile(info, obj, (attr->la_mode &
2407 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2410 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2411 struct lu_attr *attr,
2412 struct dt_allocation_hint *hint,
2413 struct dt_object_format *dof,
2416 LASSERT(S_ISLNK(attr->la_mode));
2417 return osd_mkfile(info, obj, (attr->la_mode &
2418 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2421 static int osd_mknod(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 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2430 LINVRNT(osd_invariant(obj));
2431 LASSERT(obj->oo_inode == NULL);
2432 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2433 S_ISFIFO(mode) || S_ISSOCK(mode));
2435 result = osd_mkfile(info, obj, mode, hint, th);
2437 LASSERT(obj->oo_inode != NULL);
2439 * This inode should be marked dirty for i_rdev. Currently
2440 * that is done in the osd_attr_init().
2442 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2445 LINVRNT(osd_invariant(obj));
2449 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2451 struct dt_allocation_hint *hint,
2452 struct dt_object_format *dof,
2455 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2457 osd_obj_type_f result;
2473 result = osd_mk_index;
2484 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2485 struct dt_object *parent, struct dt_object *child,
2490 ah->dah_parent = parent;
2491 ah->dah_mode = child_mode;
2493 if (parent != NULL && !dt_object_remote(parent)) {
2494 /* will help to find FID->ino at dt_insert("..") */
2495 struct osd_object *pobj = osd_dt_obj(parent);
2496 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2500 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2501 struct lu_attr *attr, struct dt_object_format *dof)
2503 struct inode *inode = obj->oo_inode;
2504 __u64 valid = attr->la_valid;
2507 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2509 if (dof->dof_type != DFT_NODE)
2510 attr->la_valid &= ~LA_RDEV;
2511 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2512 attr->la_valid &= ~LA_ATIME;
2513 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2514 attr->la_valid &= ~LA_CTIME;
2515 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2516 attr->la_valid &= ~LA_MTIME;
2518 result = osd_quota_transfer(inode, attr);
2522 if (attr->la_valid != 0) {
2523 result = osd_inode_setattr(info->oti_env, inode, attr);
2525 * The osd_inode_setattr() should always succeed here. The
2526 * only error that could be returned is EDQUOT when we are
2527 * trying to change the UID or GID of the inode. However, this
2528 * should not happen since quota enforcement is no longer
2529 * enabled on ldiskfs (lquota takes care of it).
2531 LASSERTF(result == 0, "%d\n", result);
2532 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2535 attr->la_valid = valid;
2539 * Helper function for osd_object_create()
2541 * \retval 0, on success
2543 static int __osd_object_create(struct osd_thread_info *info,
2544 struct osd_object *obj, struct lu_attr *attr,
2545 struct dt_allocation_hint *hint,
2546 struct dt_object_format *dof,
2552 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2554 /* we drop umask so that permissions we pass are not affected */
2555 umask = current->fs->umask;
2556 current->fs->umask = 0;
2558 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2560 if (likely(obj->oo_inode != NULL)) {
2561 LASSERT(obj->oo_inode->i_state & I_NEW);
2563 /* Unlock the inode before attr initialization to avoid
2564 * unnecessary dqget operations. LU-6378 */
2565 unlock_new_inode(obj->oo_inode);
2568 if (likely(result == 0)) {
2569 osd_attr_init(info, obj, attr, dof);
2570 osd_object_init0(obj);
2573 /* restore previous umask value */
2574 current->fs->umask = umask;
2576 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2582 * Helper function for osd_object_create()
2584 * \retval 0, on success
2586 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2587 const struct lu_fid *fid, struct thandle *th)
2589 struct osd_thread_info *info = osd_oti_get(env);
2590 struct osd_inode_id *id = &info->oti_id;
2591 struct osd_device *osd = osd_obj2dev(obj);
2592 struct osd_thandle *oh;
2595 LASSERT(obj->oo_inode != NULL);
2597 oh = container_of0(th, struct osd_thandle, ot_super);
2598 LASSERT(oh->ot_handle);
2599 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2601 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2602 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2603 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2608 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2609 u64 seq, struct lu_seq_range *range)
2611 struct seq_server_site *ss = osd_seq_site(osd);
2613 if (fid_seq_is_idif(seq)) {
2614 fld_range_set_ost(range);
2615 range->lsr_index = idif_ost_idx(seq);
2619 if (!fid_seq_in_fldb(seq)) {
2620 fld_range_set_mdt(range);
2622 /* FIXME: If ss is NULL, it suppose not get lsr_index
2624 range->lsr_index = ss->ss_node_id;
2628 LASSERT(ss != NULL);
2629 fld_range_set_any(range);
2630 /* OSD will only do local fld lookup */
2631 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2635 * Concurrency: no external locking is necessary.
2637 static int osd_declare_object_create(const struct lu_env *env,
2638 struct dt_object *dt,
2639 struct lu_attr *attr,
2640 struct dt_allocation_hint *hint,
2641 struct dt_object_format *dof,
2642 struct thandle *handle)
2644 struct osd_thandle *oh;
2648 LASSERT(handle != NULL);
2650 oh = container_of0(handle, struct osd_thandle, ot_super);
2651 LASSERT(oh->ot_handle == NULL);
2653 /* EA object consumes more credits than regular object: osd_mk_index
2654 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2655 * leaf_node, could involves the block, block bitmap, groups, GDT
2656 * change for each block, so add 4 * 2 credits in that case. */
2657 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2658 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2659 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2660 /* Reuse idle OI block may cause additional one OI block
2662 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2663 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2668 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2669 osd_dt_obj(dt), false, NULL, false);
2673 /* will help to find FID->ino mapping at dt_insert() */
2674 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
2680 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2681 struct lu_attr *attr,
2682 struct dt_allocation_hint *hint,
2683 struct dt_object_format *dof, struct thandle *th)
2685 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2686 struct osd_object *obj = osd_dt_obj(dt);
2687 struct osd_thread_info *info = osd_oti_get(env);
2691 if (dt_object_exists(dt))
2694 LINVRNT(osd_invariant(obj));
2695 LASSERT(!dt_object_remote(dt));
2696 LASSERT(osd_write_locked(env, obj));
2697 LASSERT(th != NULL);
2699 if (unlikely(fid_is_acct(fid)))
2700 /* Quota files can't be created from the kernel any more,
2701 * 'tune2fs -O quota' will take care of creating them */
2704 result = __osd_object_create(info, obj, attr, hint, dof, th);
2706 result = __osd_oi_insert(env, obj, fid, th);
2707 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2708 obj->oo_dt.do_body_ops = &osd_body_ops;
2710 LASSERT(ergo(result == 0,
2711 dt_object_exists(dt) && !dt_object_remote(dt)));
2713 LASSERT(osd_invariant(obj));
2718 * Called to destroy on-disk representation of the object
2720 * Concurrency: must be locked
2722 static int osd_declare_object_destroy(const struct lu_env *env,
2723 struct dt_object *dt,
2726 struct osd_object *obj = osd_dt_obj(dt);
2727 struct inode *inode = obj->oo_inode;
2728 struct osd_thandle *oh;
2735 oh = container_of0(th, struct osd_thandle, ot_super);
2736 LASSERT(oh->ot_handle == NULL);
2738 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2739 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2740 /* Recycle idle OI leaf may cause additional three OI blocks
2742 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2743 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2744 /* one less inode */
2745 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2746 -1, oh, obj, false, NULL, false);
2749 /* data to be truncated */
2750 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2751 0, oh, obj, true, NULL, false);
2755 /* will help to find FID->ino when this object is being
2756 * added to PENDING/ */
2757 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
2762 static int osd_object_destroy(const struct lu_env *env,
2763 struct dt_object *dt,
2766 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2767 struct osd_object *obj = osd_dt_obj(dt);
2768 struct inode *inode = obj->oo_inode;
2769 struct osd_device *osd = osd_obj2dev(obj);
2770 struct osd_thandle *oh;
2774 oh = container_of0(th, struct osd_thandle, ot_super);
2775 LASSERT(oh->ot_handle);
2777 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2779 if (unlikely(fid_is_acct(fid)))
2782 if (S_ISDIR(inode->i_mode)) {
2783 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2784 inode->i_nlink == 2);
2785 /* it will check/delete the inode from remote parent,
2786 * how to optimize it? unlink performance impaction XXX */
2787 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2788 if (result != 0 && result != -ENOENT) {
2789 CERROR("%s: delete inode "DFID": rc = %d\n",
2790 osd_name(osd), PFID(fid), result);
2792 spin_lock(&obj->oo_guard);
2794 spin_unlock(&obj->oo_guard);
2795 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2798 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2800 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2801 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2804 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2805 /* XXX: add to ext3 orphan list */
2806 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2808 /* not needed in the cache anymore */
2809 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2810 obj->oo_destroyed = 1;
2816 * Put the fid into lustre_mdt_attrs, and then place the structure
2817 * inode's ea. This fid should not be altered during the life time
2820 * \retval +ve, on success
2821 * \retval -ve, on error
2823 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2825 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2826 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2828 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2832 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2835 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2838 lustre_lma_init(lma, fid, compat, incompat);
2839 lustre_lma_swab(lma);
2841 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2843 /* LMA may already exist, but we need to check that all the
2844 * desired compat/incompat flags have been added. */
2845 if (unlikely(rc == -EEXIST)) {
2846 if (compat == 0 && incompat == 0)
2849 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2850 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2855 lustre_lma_swab(lma);
2856 if (!(~lma->lma_compat & compat) &&
2857 !(~lma->lma_incompat & incompat))
2860 lma->lma_compat |= compat;
2861 lma->lma_incompat |= incompat;
2862 lustre_lma_swab(lma);
2863 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2864 sizeof(*lma), XATTR_REPLACE);
2871 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2872 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2873 * To have compatilibility with 1.8 ldiskfs driver we need to have
2874 * magic number at start of fid data.
2875 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2878 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2879 const struct lu_fid *fid)
2881 if (!fid_is_namespace_visible(fid) ||
2882 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2883 param->edp_magic = 0;
2887 param->edp_magic = LDISKFS_LUFID_MAGIC;
2888 param->edp_len = sizeof(struct lu_fid) + 1;
2889 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2893 * Try to read the fid from inode ea into dt_rec.
2895 * \param fid object fid.
2897 * \retval 0 on success
2899 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2900 __u32 ino, struct lu_fid *fid,
2901 struct osd_inode_id *id)
2903 struct osd_thread_info *info = osd_oti_get(env);
2904 struct inode *inode;
2907 osd_id_gen(id, ino, OSD_OII_NOGEN);
2908 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2910 RETURN(PTR_ERR(inode));
2916 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2918 struct inode *parent_dir,
2919 const struct lu_fid *dot_fid,
2920 const struct lu_fid *dot_dot_fid,
2921 struct osd_thandle *oth)
2923 struct ldiskfs_dentry_param *dot_ldp;
2924 struct ldiskfs_dentry_param *dot_dot_ldp;
2925 __u32 saved_nlink = dir->i_nlink;
2928 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2929 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2931 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2932 dot_ldp->edp_magic = 0;
2934 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2935 dir, dot_ldp, dot_dot_ldp);
2936 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2937 * the subseqent ref_add() will increase the dir->i_nlink
2938 * as 3. That is incorrect for new created directory.
2940 * It looks like hack, because we want to make the OSD API
2941 * to be order-independent for new created directory object
2942 * between dt_insert(..) and ref_add() operations.
2944 * Here, we only restore the in-RAM dir-inode's nlink attr,
2945 * becuase if the nlink attr is not 2, then there will be
2946 * ref_add() called following the dt_insert(..), such call
2947 * will make both the in-RAM and on-disk dir-inode's nlink
2948 * attr to be set as 2. LU-7447 */
2949 set_nlink(dir, saved_nlink);
2954 * Create an local agent inode for remote entry
2956 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2957 struct osd_device *osd,
2958 struct osd_object *pobj,
2959 const struct lu_fid *fid,
2963 struct osd_thread_info *info = osd_oti_get(env);
2964 struct inode *local;
2965 struct osd_thandle *oh;
2970 oh = container_of(th, struct osd_thandle, ot_super);
2971 LASSERT(oh->ot_handle->h_transaction != NULL);
2973 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2974 if (IS_ERR(local)) {
2975 CERROR("%s: create local error %d\n", osd_name(osd),
2976 (int)PTR_ERR(local));
2980 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2981 unlock_new_inode(local);
2983 /* Set special LMA flag for local agent inode */
2984 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2986 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2987 osd_name(osd), PFID(fid), rc);
2988 RETURN(ERR_PTR(rc));
2994 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2995 lu_object_fid(&pobj->oo_dt.do_lu),
2998 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2999 osd_name(osd), PFID(fid), rc);
3000 RETURN(ERR_PTR(rc));
3007 * when direntry is deleted, we have to take care of possible agent inode
3008 * referenced by that. unfortunately we can't do this at that point:
3009 * iget() within a running transaction leads to deadlock and we better do
3010 * not call that every delete declaration to save performance. so we put
3011 * a potention agent inode on a list and process that once the transaction
3012 * is over. Notice it's not any worse in terms of real orphans as regular
3013 * object destroy doesn't put inodes on the on-disk orphan list. this should
3014 * be addressed separately
3016 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3017 struct osd_thandle *oh,
3020 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3021 struct osd_obj_orphan *oor;
3028 oor->oor_env = (struct lu_env *)env;
3029 spin_lock(&osd->od_osfs_lock);
3030 list_add(&oor->oor_list, &osd->od_orphan_list);
3031 spin_unlock(&osd->od_osfs_lock);
3033 oh->ot_remove_agents = 1;
3039 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3040 struct osd_device *osd)
3042 struct osd_thread_info *info = osd_oti_get(env);
3043 struct osd_obj_orphan *oor, *tmp;
3044 struct osd_inode_id id;
3045 struct list_head list;
3046 struct inode *inode;
3051 INIT_LIST_HEAD(&list);
3053 spin_lock(&osd->od_osfs_lock);
3054 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3055 if (oor->oor_env == env) {
3056 list_del(&oor->oor_list);
3057 list_add(&oor->oor_list, &list);
3060 spin_unlock(&osd->od_osfs_lock);
3062 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3066 list_del(&oor->oor_list);
3069 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3070 inode = osd_iget_fid(info, osd, &id, &fid);
3074 if (!osd_remote_fid(env, osd, &fid)) {
3079 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3081 mark_inode_dirty(inode);
3082 ldiskfs_journal_stop(jh);
3090 * OSD layer object create function for interoperability mode (b11826).
3091 * This is mostly similar to osd_object_create(). Only difference being, fid is
3092 * inserted into inode ea here.
3094 * \retval 0, on success
3095 * \retval -ve, on error
3097 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3098 struct lu_attr *attr,
3099 struct dt_allocation_hint *hint,
3100 struct dt_object_format *dof,
3103 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3104 struct osd_object *obj = osd_dt_obj(dt);
3105 struct osd_thread_info *info = osd_oti_get(env);
3106 int result, on_ost = 0;
3110 if (dt_object_exists(dt))
3113 LASSERT(osd_invariant(obj));
3114 LASSERT(!dt_object_remote(dt));
3115 LASSERT(osd_write_locked(env, obj));
3116 LASSERT(th != NULL);
3118 if (unlikely(fid_is_acct(fid)))
3119 /* Quota files can't be created from the kernel any more,
3120 * 'tune2fs -O quota' will take care of creating them */
3123 result = __osd_object_create(info, obj, attr, hint, dof, th);
3125 if (fid_is_idif(fid) &&
3126 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3127 struct lu_fid *tfid = &info->oti_fid;
3128 struct ost_id *oi = &info->oti_ostid;
3130 fid_to_ostid(fid, oi);
3131 ostid_to_fid(tfid, oi, 0);
3133 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3134 LMAC_FID_ON_OST, 0);
3136 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3138 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3139 on_ost ? LMAC_FID_ON_OST : 0,
3142 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3143 obj->oo_dt.do_body_ops = &osd_body_ops;
3147 result = __osd_oi_insert(env, obj, fid, th);
3149 /* a small optimization - dt_insert() isn't usually applied
3150 * to OST objects, so we don't need to cache OI mapping for
3152 if (result == 0 && on_ost == 0) {
3153 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3154 result = osd_idc_find_and_init(env, osd, obj);
3155 LASSERT(result == 0);
3158 LASSERT(ergo(result == 0,
3159 dt_object_exists(dt) && !dt_object_remote(dt)));
3160 LINVRNT(osd_invariant(obj));
3164 static int osd_declare_object_ref_add(const struct lu_env *env,
3165 struct dt_object *dt,
3166 struct thandle *handle)
3168 struct osd_thandle *oh;
3170 /* it's possible that object doesn't exist yet */
3171 LASSERT(handle != NULL);
3173 oh = container_of0(handle, struct osd_thandle, ot_super);
3174 LASSERT(oh->ot_handle == NULL);
3176 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3177 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3183 * Concurrency: @dt is write locked.
3185 static int osd_object_ref_add(const struct lu_env *env,
3186 struct dt_object *dt, struct thandle *th)
3188 struct osd_object *obj = osd_dt_obj(dt);
3189 struct inode *inode = obj->oo_inode;
3190 struct osd_thandle *oh;
3193 if (!dt_object_exists(dt))
3196 LINVRNT(osd_invariant(obj));
3197 LASSERT(!dt_object_remote(dt));
3198 LASSERT(osd_write_locked(env, obj));
3199 LASSERT(th != NULL);
3201 oh = container_of0(th, struct osd_thandle, ot_super);
3202 LASSERT(oh->ot_handle != NULL);
3204 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3206 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3207 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3209 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3210 * (65000) subdirectories by storing "1" in i_nlink if the link count
3211 * would otherwise overflow. Directory tranversal tools understand
3212 * that (st_nlink == 1) indicates that the filesystem dose not track
3213 * hard links count on the directory, and will not abort subdirectory
3214 * scanning early once (st_nlink - 2) subdirs have been found.
3216 * This also has to properly handle the case of inodes with nlink == 0
3217 * in case they are being linked into the PENDING directory
3219 spin_lock(&obj->oo_guard);
3220 if (unlikely(inode->i_nlink == 0))
3221 /* inc_nlink from 0 may cause WARN_ON */
3222 set_nlink(inode, 1);
3224 ldiskfs_inc_count(oh->ot_handle, inode);
3225 if (!S_ISDIR(inode->i_mode))
3226 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3228 spin_unlock(&obj->oo_guard);
3230 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3231 LINVRNT(osd_invariant(obj));
3233 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3238 static int osd_declare_object_ref_del(const struct lu_env *env,
3239 struct dt_object *dt,
3240 struct thandle *handle)
3242 struct osd_thandle *oh;
3244 if (!dt_object_exists(dt))
3247 LASSERT(!dt_object_remote(dt));
3248 LASSERT(handle != NULL);
3250 oh = container_of0(handle, struct osd_thandle, ot_super);
3251 LASSERT(oh->ot_handle == NULL);
3253 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3254 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3260 * Concurrency: @dt is write locked.
3262 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3265 struct osd_object *obj = osd_dt_obj(dt);
3266 struct inode *inode = obj->oo_inode;
3267 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3268 struct osd_thandle *oh;
3270 if (!dt_object_exists(dt))
3273 LINVRNT(osd_invariant(obj));
3274 LASSERT(!dt_object_remote(dt));
3275 LASSERT(osd_write_locked(env, obj));
3276 LASSERT(th != NULL);
3278 oh = container_of0(th, struct osd_thandle, ot_super);
3279 LASSERT(oh->ot_handle != NULL);
3281 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3283 spin_lock(&obj->oo_guard);
3284 /* That can be result of upgrade from old Lustre version and
3285 * applied only to local files. Just skip this ref_del call.
3286 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3287 if (inode->i_nlink == 0) {
3288 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3289 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3290 ", maybe an upgraded file? (LU-3915)\n",
3291 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3292 spin_unlock(&obj->oo_guard);
3296 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3297 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3299 ldiskfs_dec_count(oh->ot_handle, inode);
3300 spin_unlock(&obj->oo_guard);
3302 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3303 LINVRNT(osd_invariant(obj));
3305 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3311 * Get the 64-bit version for an inode.
3313 static int osd_object_version_get(const struct lu_env *env,
3314 struct dt_object *dt, dt_obj_version_t *ver)
3316 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3318 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3319 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3320 *ver = LDISKFS_I(inode)->i_fs_version;
3325 * Concurrency: @dt is read locked.
3327 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3328 struct lu_buf *buf, const char *name)
3330 struct osd_object *obj = osd_dt_obj(dt);
3331 struct inode *inode = obj->oo_inode;
3332 struct osd_thread_info *info = osd_oti_get(env);
3333 struct dentry *dentry = &info->oti_obj_dentry;
3335 /* version get is not real XATTR but uses xattr API */
3336 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3337 /* for version we are just using xattr API but change inode
3339 if (buf->lb_len == 0)
3340 return sizeof(dt_obj_version_t);
3342 if (buf->lb_len < sizeof(dt_obj_version_t))
3345 osd_object_version_get(env, dt, buf->lb_buf);
3347 return sizeof(dt_obj_version_t);
3350 if (!dt_object_exists(dt))
3353 LASSERT(!dt_object_remote(dt));
3354 LASSERT(inode->i_op != NULL);
3355 LASSERT(inode->i_op->getxattr != NULL);
3357 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3361 static int osd_declare_xattr_set(const struct lu_env *env,
3362 struct dt_object *dt,
3363 const struct lu_buf *buf, const char *name,
3364 int fl, struct thandle *handle)
3366 struct osd_thandle *oh;
3368 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3370 LASSERT(handle != NULL);
3372 oh = container_of0(handle, struct osd_thandle, ot_super);
3373 LASSERT(oh->ot_handle == NULL);
3375 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3376 /* For non-upgrading case, the LMA is set first and
3377 * usually fit inode. But for upgrade case, the LMA
3378 * may be in another separated EA block. */
3379 if (!dt_object_exists(dt))
3381 else if (fl == LU_XATTR_REPLACE)
3385 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3389 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3394 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3395 /* learn xattr size from osd_xattr_get if
3396 attribute has not been read yet */
3397 buflen = __osd_xattr_get(
3398 osd_dt_obj(dt)->oo_inode,
3399 &osd_oti_get(env)->oti_obj_dentry,
3404 buflen = buf->lb_len;
3407 if (buflen > sb->s_blocksize) {
3408 credits += osd_calc_bkmap_credits(
3410 (buflen + sb->s_blocksize - 1) >>
3411 sb->s_blocksize_bits);
3415 * xattr set may involve inode quota change, reserve credits for
3416 * dquot_initialize()
3418 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3421 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3427 * Set the 64-bit version for object
3429 static void osd_object_version_set(const struct lu_env *env,
3430 struct dt_object *dt,
3431 dt_obj_version_t *new_version)
3433 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3435 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3436 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3438 LDISKFS_I(inode)->i_fs_version = *new_version;
3439 /** Version is set after all inode operations are finished,
3440 * so we should mark it dirty here */
3441 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3445 * Concurrency: @dt is write locked.
3447 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3448 const struct lu_buf *buf, const char *name, int fl,
3449 struct thandle *handle)
3451 struct osd_object *obj = osd_dt_obj(dt);
3452 struct inode *inode = obj->oo_inode;
3453 struct osd_thread_info *info = osd_oti_get(env);
3458 LASSERT(handle != NULL);
3460 /* version set is not real XATTR */
3461 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3462 /* for version we are just using xattr API but change inode
3464 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3465 osd_object_version_set(env, dt, buf->lb_buf);
3466 return sizeof(dt_obj_version_t);
3469 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3470 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3472 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3473 if (fl & LU_XATTR_REPLACE)
3474 fs_flags |= XATTR_REPLACE;
3476 if (fl & LU_XATTR_CREATE)
3477 fs_flags |= XATTR_CREATE;
3479 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3480 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3483 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3487 lma->lma_incompat |= LMAI_STRIPED;
3488 lustre_lma_swab(lma);
3489 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3490 sizeof(*lma), XATTR_REPLACE);
3495 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3496 strcmp(name, XATTR_NAME_LINK) == 0)
3499 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3501 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3507 * Concurrency: @dt is read locked.
3509 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3510 const struct lu_buf *buf)
3512 struct osd_object *obj = osd_dt_obj(dt);
3513 struct inode *inode = obj->oo_inode;
3514 struct osd_thread_info *info = osd_oti_get(env);
3515 struct dentry *dentry = &info->oti_obj_dentry;
3517 if (!dt_object_exists(dt))
3520 LASSERT(!dt_object_remote(dt));
3521 LASSERT(inode->i_op != NULL);
3522 LASSERT(inode->i_op->listxattr != NULL);
3524 dentry->d_inode = inode;
3525 dentry->d_sb = inode->i_sb;
3526 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3529 static int osd_declare_xattr_del(const struct lu_env *env,
3530 struct dt_object *dt, const char *name,
3531 struct thandle *handle)
3533 struct osd_thandle *oh;
3534 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3536 LASSERT(!dt_object_remote(dt));
3537 LASSERT(handle != NULL);
3539 oh = container_of0(handle, struct osd_thandle, ot_super);
3540 LASSERT(oh->ot_handle == NULL);
3542 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3543 osd_dto_credits_noquota[DTO_XATTR_SET]);
3545 * xattr del may involve inode quota change, reserve credits for
3546 * dquot_initialize()
3548 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3554 * Concurrency: @dt is write locked.
3556 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3557 const char *name, struct thandle *handle)
3559 struct osd_object *obj = osd_dt_obj(dt);
3560 struct inode *inode = obj->oo_inode;
3561 struct osd_thread_info *info = osd_oti_get(env);
3562 struct dentry *dentry = &info->oti_obj_dentry;
3565 if (!dt_object_exists(dt))
3568 LASSERT(!dt_object_remote(dt));
3569 LASSERT(inode->i_op != NULL);
3570 LASSERT(inode->i_op->removexattr != NULL);
3571 LASSERT(handle != NULL);
3573 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3575 ll_vfs_dq_init(inode);
3576 dentry->d_inode = inode;
3577 dentry->d_sb = inode->i_sb;
3578 rc = inode->i_op->removexattr(dentry, name);
3579 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3583 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3584 __u64 start, __u64 end)
3586 struct osd_object *obj = osd_dt_obj(dt);
3587 struct inode *inode = obj->oo_inode;
3588 struct osd_thread_info *info = osd_oti_get(env);
3589 struct dentry *dentry = &info->oti_obj_dentry;
3590 struct file *file = &info->oti_file;
3595 dentry->d_inode = inode;
3596 dentry->d_sb = inode->i_sb;
3597 file->f_path.dentry = dentry;
3598 file->f_mapping = inode->i_mapping;
3599 file->f_op = inode->i_fop;
3600 set_file_inode(file, inode);
3602 rc = ll_vfs_fsync_range(file, start, end, 0);
3611 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3612 const struct dt_index_features *feat)
3614 struct iam_descr *descr;
3616 if (osd_object_is_root(o))
3617 return feat == &dt_directory_features;
3619 LASSERT(o->oo_dir != NULL);
3621 descr = o->oo_dir->od_container.ic_descr;
3622 if (feat == &dt_directory_features) {
3623 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3629 feat->dif_keysize_min <= descr->id_key_size &&
3630 descr->id_key_size <= feat->dif_keysize_max &&
3631 feat->dif_recsize_min <= descr->id_rec_size &&
3632 descr->id_rec_size <= feat->dif_recsize_max &&
3633 !(feat->dif_flags & (DT_IND_VARKEY |
3634 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3635 ergo(feat->dif_flags & DT_IND_UPDATE,
3636 1 /* XXX check that object (and file system) is
3641 static int osd_iam_container_init(const struct lu_env *env,
3642 struct osd_object *obj,
3643 struct osd_directory *dir)
3645 struct iam_container *bag = &dir->od_container;
3648 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3652 result = iam_container_setup(bag);
3654 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3656 iam_container_fini(bag);
3663 * Concurrency: no external locking is necessary.
3665 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3666 const struct dt_index_features *feat)
3670 struct osd_object *obj = osd_dt_obj(dt);
3672 LINVRNT(osd_invariant(obj));
3674 if (osd_object_is_root(obj)) {
3675 dt->do_index_ops = &osd_index_ea_ops;
3677 } else if (feat == &dt_directory_features) {
3678 dt->do_index_ops = &osd_index_ea_ops;
3679 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
3684 } else if (unlikely(feat == &dt_otable_features)) {
3685 dt->do_index_ops = &osd_otable_ops;
3687 } else if (unlikely(feat == &dt_acct_features)) {
3688 dt->do_index_ops = &osd_acct_index_ops;
3691 } else if (!osd_has_index(obj)) {
3692 struct osd_directory *dir;
3697 spin_lock(&obj->oo_guard);
3698 if (obj->oo_dir == NULL)
3702 * Concurrent thread allocated container data.
3705 spin_unlock(&obj->oo_guard);
3707 * Now, that we have container data, serialize its
3710 down_write(&obj->oo_ext_idx_sem);
3712 * recheck under lock.
3714 if (!osd_has_index(obj))
3715 result = osd_iam_container_init(env, obj,
3719 up_write(&obj->oo_ext_idx_sem);
3727 if (result == 0 && skip_iam == 0) {
3728 if (!osd_iam_index_probe(env, obj, feat))
3731 LINVRNT(osd_invariant(obj));
3733 if (result == 0 && feat == &dt_quota_glb_features &&
3734 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3735 result = osd_quota_migration(env, dt);
3740 static int osd_otable_it_attr_get(const struct lu_env *env,
3741 struct dt_object *dt,
3742 struct lu_attr *attr)
3748 static const struct dt_object_operations osd_obj_ops = {
3749 .do_read_lock = osd_object_read_lock,
3750 .do_write_lock = osd_object_write_lock,
3751 .do_read_unlock = osd_object_read_unlock,
3752 .do_write_unlock = osd_object_write_unlock,
3753 .do_write_locked = osd_object_write_locked,
3754 .do_attr_get = osd_attr_get,
3755 .do_declare_attr_set = osd_declare_attr_set,
3756 .do_attr_set = osd_attr_set,
3757 .do_ah_init = osd_ah_init,
3758 .do_declare_create = osd_declare_object_create,
3759 .do_create = osd_object_create,
3760 .do_declare_destroy = osd_declare_object_destroy,
3761 .do_destroy = osd_object_destroy,
3762 .do_index_try = osd_index_try,
3763 .do_declare_ref_add = osd_declare_object_ref_add,
3764 .do_ref_add = osd_object_ref_add,
3765 .do_declare_ref_del = osd_declare_object_ref_del,
3766 .do_ref_del = osd_object_ref_del,
3767 .do_xattr_get = osd_xattr_get,
3768 .do_declare_xattr_set = osd_declare_xattr_set,
3769 .do_xattr_set = osd_xattr_set,
3770 .do_declare_xattr_del = osd_declare_xattr_del,
3771 .do_xattr_del = osd_xattr_del,
3772 .do_xattr_list = osd_xattr_list,
3773 .do_object_sync = osd_object_sync,
3777 * dt_object_operations for interoperability mode
3778 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3780 static const struct dt_object_operations osd_obj_ea_ops = {
3781 .do_read_lock = osd_object_read_lock,
3782 .do_write_lock = osd_object_write_lock,
3783 .do_read_unlock = osd_object_read_unlock,
3784 .do_write_unlock = osd_object_write_unlock,
3785 .do_write_locked = osd_object_write_locked,
3786 .do_attr_get = osd_attr_get,
3787 .do_declare_attr_set = osd_declare_attr_set,
3788 .do_attr_set = osd_attr_set,
3789 .do_ah_init = osd_ah_init,
3790 .do_declare_create = osd_declare_object_create,
3791 .do_create = osd_object_ea_create,
3792 .do_declare_destroy = osd_declare_object_destroy,
3793 .do_destroy = osd_object_destroy,
3794 .do_index_try = osd_index_try,
3795 .do_declare_ref_add = osd_declare_object_ref_add,
3796 .do_ref_add = osd_object_ref_add,
3797 .do_declare_ref_del = osd_declare_object_ref_del,
3798 .do_ref_del = osd_object_ref_del,
3799 .do_xattr_get = osd_xattr_get,
3800 .do_declare_xattr_set = osd_declare_xattr_set,
3801 .do_xattr_set = osd_xattr_set,
3802 .do_declare_xattr_del = osd_declare_xattr_del,
3803 .do_xattr_del = osd_xattr_del,
3804 .do_xattr_list = osd_xattr_list,
3805 .do_object_sync = osd_object_sync,
3808 static const struct dt_object_operations osd_obj_otable_it_ops = {
3809 .do_attr_get = osd_otable_it_attr_get,
3810 .do_index_try = osd_index_try,
3813 static int osd_index_declare_iam_delete(const struct lu_env *env,
3814 struct dt_object *dt,
3815 const struct dt_key *key,
3816 struct thandle *handle)
3818 struct osd_thandle *oh;
3820 oh = container_of0(handle, struct osd_thandle, ot_super);
3821 LASSERT(oh->ot_handle == NULL);
3823 /* Recycle may cause additional three blocks to be changed. */
3824 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3825 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3831 * delete a (key, value) pair from index \a dt specified by \a key
3833 * \param dt osd index object
3834 * \param key key for index
3835 * \param rec record reference
3836 * \param handle transaction handler
3839 * \retval -ve failure
3841 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3842 const struct dt_key *key,
3843 struct thandle *handle)
3845 struct osd_thread_info *oti = osd_oti_get(env);
3846 struct osd_object *obj = osd_dt_obj(dt);
3847 struct osd_thandle *oh;
3848 struct iam_path_descr *ipd;
3849 struct iam_container *bag = &obj->oo_dir->od_container;
3853 if (!dt_object_exists(dt))
3856 LINVRNT(osd_invariant(obj));
3857 LASSERT(!dt_object_remote(dt));
3858 LASSERT(bag->ic_object == obj->oo_inode);
3859 LASSERT(handle != NULL);
3861 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3863 ipd = osd_idx_ipd_get(env, bag);
3864 if (unlikely(ipd == NULL))
3867 oh = container_of0(handle, struct osd_thandle, ot_super);
3868 LASSERT(oh->ot_handle != NULL);
3869 LASSERT(oh->ot_handle->h_transaction != NULL);
3871 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3872 /* swab quota uid/gid provided by caller */
3873 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3874 key = (const struct dt_key *)&oti->oti_quota_id;
3877 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3878 osd_ipd_put(env, bag, ipd);
3879 LINVRNT(osd_invariant(obj));
3880 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3884 static int osd_index_declare_ea_delete(const struct lu_env *env,
3885 struct dt_object *dt,
3886 const struct dt_key *key,
3887 struct thandle *handle)
3889 struct osd_thandle *oh;
3890 struct inode *inode;
3894 LASSERT(!dt_object_remote(dt));
3895 LASSERT(handle != NULL);
3897 oh = container_of0(handle, struct osd_thandle, ot_super);
3898 LASSERT(oh->ot_handle == NULL);
3900 /* due to DNE we may need to remove an agent inode */
3901 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3902 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3903 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3905 inode = osd_dt_obj(dt)->oo_inode;
3909 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3910 0, oh, osd_dt_obj(dt), true, NULL, false);
3914 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3917 struct osd_fid_pack *rec;
3920 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3921 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3922 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3923 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3929 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3930 const struct lu_fid *fid)
3932 struct seq_server_site *ss = osd_seq_site(osd);
3935 /* FID seqs not in FLDB, must be local seq */
3936 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3939 /* If FLD is not being initialized yet, it only happens during the
3940 * initialization, likely during mgs initialization, and we assume
3941 * this is local FID. */
3942 if (ss == NULL || ss->ss_server_fld == NULL)
3945 /* Only check the local FLDB here */
3946 if (osd_seq_exists(env, osd, fid_seq(fid)))
3953 * Index delete function for interoperability mode (b11826).
3954 * It will remove the directory entry added by osd_index_ea_insert().
3955 * This entry is needed to maintain name->fid mapping.
3957 * \param key, key i.e. file entry to be deleted
3959 * \retval 0, on success
3960 * \retval -ve, on error
3962 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3963 const struct dt_key *key, struct thandle *handle)
3965 struct osd_object *obj = osd_dt_obj(dt);
3966 struct inode *dir = obj->oo_inode;
3967 struct dentry *dentry;
3968 struct osd_thandle *oh;
3969 struct ldiskfs_dir_entry_2 *de = NULL;
3970 struct buffer_head *bh;
3971 struct htree_lock *hlock = NULL;
3972 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3973 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3977 if (!dt_object_exists(dt))
3980 LINVRNT(osd_invariant(obj));
3981 LASSERT(!dt_object_remote(dt));
3982 LASSERT(handle != NULL);
3984 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3986 oh = container_of(handle, struct osd_thandle, ot_super);
3987 LASSERT(oh->ot_handle != NULL);
3988 LASSERT(oh->ot_handle->h_transaction != NULL);
3990 ll_vfs_dq_init(dir);
3991 dentry = osd_child_dentry_get(env, obj,
3992 (char *)key, strlen((char *)key));
3994 if (obj->oo_hl_head != NULL) {
3995 hlock = osd_oti_get(env)->oti_hlock;
3996 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3997 dir, LDISKFS_HLOCK_DEL);
3999 down_write(&obj->oo_ext_idx_sem);
4002 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4004 /* If this is not the ".." entry, it might be a remote DNE
4005 * entry and we need to check if the FID is for a remote
4006 * MDT. If the FID is not in the directory entry (e.g.
4007 * upgraded 1.8 filesystem without dirdata enabled) then
4008 * we need to get the FID from the LMA. For a remote directory
4009 * there HAS to be an LMA, it cannot be an IGIF inode in this
4012 * Delete the entry before the agent inode in order to
4013 * simplify error handling. At worst an error after deleting
4014 * the entry first might leak the agent inode afterward. The
4015 * reverse would need filesystem abort in case of error deleting
4016 * the entry after the agent had been removed, or leave a
4017 * dangling entry pointing at a random inode. */
4018 if (strcmp((char *)key, dotdot) != 0) {
4019 LASSERT(de != NULL);
4020 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4021 if (rc == -ENODATA) {
4022 /* can't get FID, postpone to the end of the
4023 * transaction when iget() is safe */
4024 osd_schedule_agent_inode_removal(env, oh,
4025 le32_to_cpu(de->inode));
4026 } else if (rc == 0 &&
4027 unlikely(osd_remote_fid(env, osd, fid))) {
4028 osd_schedule_agent_inode_removal(env, oh,
4029 le32_to_cpu(de->inode));
4032 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4038 ldiskfs_htree_unlock(hlock);
4040 up_write(&obj->oo_ext_idx_sem);
4045 /* For inode on the remote MDT, .. will point to
4046 * /Agent directory, Check whether it needs to delete
4047 * from agent directory */
4048 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4049 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
4051 if (rc != 0 && rc != -ENOENT) {
4052 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4053 osd_name(osd), PFID(fid), rc);
4063 LASSERT(osd_invariant(obj));
4064 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4069 * Lookup index for \a key and copy record to \a rec.
4071 * \param dt osd index object
4072 * \param key key for index
4073 * \param rec record reference
4075 * \retval +ve success : exact mach
4076 * \retval 0 return record with key not greater than \a key
4077 * \retval -ve failure
4079 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4080 struct dt_rec *rec, const struct dt_key *key)
4082 struct osd_object *obj = osd_dt_obj(dt);
4083 struct iam_path_descr *ipd;
4084 struct iam_container *bag = &obj->oo_dir->od_container;
4085 struct osd_thread_info *oti = osd_oti_get(env);
4086 struct iam_iterator *it = &oti->oti_idx_it;
4087 struct iam_rec *iam_rec;
4091 if (!dt_object_exists(dt))
4094 LASSERT(osd_invariant(obj));
4095 LASSERT(!dt_object_remote(dt));
4096 LASSERT(bag->ic_object == obj->oo_inode);
4098 ipd = osd_idx_ipd_get(env, bag);
4102 /* got ipd now we can start iterator. */
4103 iam_it_init(it, bag, 0, ipd);
4105 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4106 /* swab quota uid/gid provided by caller */
4107 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4108 key = (const struct dt_key *)&oti->oti_quota_id;
4111 rc = iam_it_get(it, (struct iam_key *)key);
4113 if (S_ISDIR(obj->oo_inode->i_mode))
4114 iam_rec = (struct iam_rec *)oti->oti_ldp;
4116 iam_rec = (struct iam_rec *) rec;
4118 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4120 if (S_ISDIR(obj->oo_inode->i_mode))
4121 osd_fid_unpack((struct lu_fid *) rec,
4122 (struct osd_fid_pack *)iam_rec);
4123 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4124 osd_quota_unpack(obj, rec);
4129 osd_ipd_put(env, bag, ipd);
4131 LINVRNT(osd_invariant(obj));
4136 static int osd_index_declare_iam_insert(const struct lu_env *env,
4137 struct dt_object *dt,
4138 const struct dt_rec *rec,
4139 const struct dt_key *key,
4140 struct thandle *handle)
4142 struct osd_thandle *oh;
4144 LASSERT(handle != NULL);
4146 oh = container_of0(handle, struct osd_thandle, ot_super);
4147 LASSERT(oh->ot_handle == NULL);
4149 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4150 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4156 * Inserts (key, value) pair in \a dt index object.
4158 * \param dt osd index object
4159 * \param key key for index
4160 * \param rec record reference
4161 * \param th transaction handler
4164 * \retval -ve failure
4166 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4167 const struct dt_rec *rec,
4168 const struct dt_key *key, struct thandle *th,
4171 struct osd_object *obj = osd_dt_obj(dt);
4172 struct iam_path_descr *ipd;
4173 struct osd_thandle *oh;
4174 struct iam_container *bag;
4175 struct osd_thread_info *oti = osd_oti_get(env);
4176 struct iam_rec *iam_rec;
4180 if (!dt_object_exists(dt))
4183 LINVRNT(osd_invariant(obj));
4184 LASSERT(!dt_object_remote(dt));
4186 bag = &obj->oo_dir->od_container;
4187 LASSERT(bag->ic_object == obj->oo_inode);
4188 LASSERT(th != NULL);
4190 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4192 ipd = osd_idx_ipd_get(env, bag);
4193 if (unlikely(ipd == NULL))
4196 oh = container_of0(th, struct osd_thandle, ot_super);
4197 LASSERT(oh->ot_handle != NULL);
4198 LASSERT(oh->ot_handle->h_transaction != NULL);
4199 if (S_ISDIR(obj->oo_inode->i_mode)) {
4200 iam_rec = (struct iam_rec *)oti->oti_ldp;
4201 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4202 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4203 /* pack quota uid/gid */
4204 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4205 key = (const struct dt_key *)&oti->oti_quota_id;
4206 /* pack quota record */
4207 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4208 iam_rec = (struct iam_rec *)rec;
4210 iam_rec = (struct iam_rec *)rec;
4213 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4215 osd_ipd_put(env, bag, ipd);
4216 LINVRNT(osd_invariant(obj));
4217 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4222 * Calls ldiskfs_add_entry() to add directory entry
4223 * into the directory. This is required for
4224 * interoperability mode (b11826)
4226 * \retval 0, on success
4227 * \retval -ve, on error
4229 static int __osd_ea_add_rec(struct osd_thread_info *info,
4230 struct osd_object *pobj, struct inode *cinode,
4231 const char *name, const struct lu_fid *fid,
4232 struct htree_lock *hlock, struct thandle *th)
4234 struct ldiskfs_dentry_param *ldp;
4235 struct dentry *child;
4236 struct osd_thandle *oth;
4239 oth = container_of(th, struct osd_thandle, ot_super);
4240 LASSERT(oth->ot_handle != NULL);
4241 LASSERT(oth->ot_handle->h_transaction != NULL);
4242 LASSERT(pobj->oo_inode);
4244 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4245 if (unlikely(pobj->oo_inode ==
4246 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4249 osd_get_ldiskfs_dirent_param(ldp, fid);
4250 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4251 child->d_fsdata = (void *)ldp;
4252 ll_vfs_dq_init(pobj->oo_inode);
4253 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4255 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4256 struct ldiskfs_dir_entry_2 *de;
4257 struct buffer_head *bh;
4260 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4263 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4266 if (S_ISDIR(cinode->i_mode))
4267 de->file_type = LDISKFS_DIRENT_LUFID |
4268 LDISKFS_FT_REG_FILE;
4270 de->file_type = LDISKFS_DIRENT_LUFID |
4272 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4283 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4284 * into the directory.Also sets flags into osd object to
4285 * indicate dot and dotdot are created. This is required for
4286 * interoperability mode (b11826)
4288 * \param dir directory for dot and dotdot fixup.
4289 * \param obj child object for linking
4291 * \retval 0, on success
4292 * \retval -ve, on error
4294 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4295 struct osd_object *dir,
4296 struct inode *parent_dir, const char *name,
4297 const struct lu_fid *dot_fid,
4298 const struct lu_fid *dot_dot_fid,
4301 struct inode *inode = dir->oo_inode;
4302 struct osd_thandle *oth;
4305 oth = container_of(th, struct osd_thandle, ot_super);
4306 LASSERT(oth->ot_handle->h_transaction != NULL);
4307 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4309 if (strcmp(name, dot) == 0) {
4310 if (dir->oo_compat_dot_created) {
4313 LASSERT(inode->i_ino == parent_dir->i_ino);
4314 dir->oo_compat_dot_created = 1;
4317 } else if (strcmp(name, dotdot) == 0) {
4318 if (!dir->oo_compat_dot_created)
4320 /* in case of rename, dotdot is already created */
4321 if (dir->oo_compat_dotdot_created) {
4322 return __osd_ea_add_rec(info, dir, parent_dir, name,
4323 dot_dot_fid, NULL, th);
4326 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4327 struct lu_fid tfid = *dot_dot_fid;
4330 result = osd_add_dot_dotdot_internal(info,
4331 dir->oo_inode, parent_dir, dot_fid,
4334 result = osd_add_dot_dotdot_internal(info,
4335 dir->oo_inode, parent_dir, dot_fid,
4340 dir->oo_compat_dotdot_created = 1;
4348 * It will call the appropriate osd_add* function and return the
4349 * value, return by respective functions.
4351 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4352 struct inode *cinode, const char *name,
4353 const struct lu_fid *fid, struct thandle *th)
4355 struct osd_thread_info *info = osd_oti_get(env);
4356 struct htree_lock *hlock;
4359 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4361 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4363 if (hlock != NULL) {
4364 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4367 down_write(&pobj->oo_ext_idx_sem);
4370 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4371 lu_object_fid(&pobj->oo_dt.do_lu),
4374 if (hlock != NULL) {
4375 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4376 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4378 down_write(&pobj->oo_ext_idx_sem);
4381 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4382 struct lu_fid *tfid = &info->oti_fid;
4386 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4389 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4394 ldiskfs_htree_unlock(hlock);
4396 up_write(&pobj->oo_ext_idx_sem);
4402 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4403 struct osd_idmap_cache *oic)
4405 struct osd_scrub *scrub = &dev->od_scrub;
4406 struct lu_fid *fid = &oic->oic_fid;
4407 struct osd_inode_id *id = &oti->oti_id;
4412 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4415 if (scrub->os_pos_current > id->oii_ino)
4419 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4420 if (rc == -ENOENT) {
4421 struct inode *inode;
4424 inode = osd_iget(oti, dev, &oic->oic_lid);
4426 /* The inode has been removed (by race maybe). */
4427 if (IS_ERR(inode)) {
4428 rc = PTR_ERR(inode);
4430 RETURN(rc == -ESTALE ? -ENOENT : rc);
4434 /* The OI mapping is lost. */
4435 if (id->oii_gen != OSD_OII_NOGEN)
4438 /* The inode may has been reused by others, we do not know,
4439 * leave it to be handled by subsequent osd_fid_lookup(). */
4441 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4446 if (thread_is_running(&scrub->os_thread)) {
4447 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4448 /* There is race condition between osd_oi_lookup and OI scrub.
4449 * The OI scrub finished just after osd_oi_lookup() failure.
4450 * Under such case, it is unnecessary to trigger OI scrub again,
4451 * but try to call osd_oi_lookup() again. */
4452 if (unlikely(rc == -EAGAIN))
4458 if (!dev->od_noscrub && ++once == 1) {
4459 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4461 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4462 "for "DFID", rc = %d [2]\n",
4463 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4465 if (rc == 0 || rc == -EALREADY)
4472 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4473 struct osd_device *dev,
4474 struct osd_idmap_cache *oic,
4475 struct lu_fid *fid, __u32 ino)
4477 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4478 struct inode *inode;
4481 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4482 inode = osd_iget(oti, dev, &oic->oic_lid);
4483 if (IS_ERR(inode)) {
4484 fid_zero(&oic->oic_fid);
4485 return PTR_ERR(inode);
4488 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4491 fid_zero(&oic->oic_fid);
4493 *fid = oic->oic_fid = lma->lma_self_fid;
4497 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4498 struct osd_inode_id *id, const struct lu_fid *fid)
4500 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4501 id->oii_ino, id->oii_gen, info);
4502 info->oti_cache.oic_lid = *id;
4503 info->oti_cache.oic_fid = *fid;
4504 info->oti_cache.oic_dev = osd;
4508 * Get parent FID from the linkEA.
4510 * For a directory which parent resides on remote MDT, to satisfy the
4511 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4512 * the other hand, to make the lookup(..) on the directory can return
4513 * the real parent FID, we append the real parent FID after its ".."
4514 * name entry in the /REMOTE_PARENT_DIR.
4516 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4517 * backup. So after the restore, we cannot get the right parent FID from
4518 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4519 * we have stored the real parent FID in the directory object's linkEA,
4520 * we can parse the linkEA for the real parent FID.
4522 * \param[in] env pointer to the thread context
4523 * \param[in] obj pointer to the object to be handled
4524 * \param[out]fid pointer to the buffer to hold the parent FID
4526 * \retval 0 for getting the real parent FID successfully
4527 * \retval negative error number on failure
4529 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4530 struct osd_object *obj,
4533 struct osd_thread_info *oti = osd_oti_get(env);
4534 struct lu_buf *buf = &oti->oti_big_buf;
4535 struct dentry *dentry = &oti->oti_obj_dentry;
4536 struct inode *inode = obj->oo_inode;
4537 struct linkea_data ldata = { NULL };
4542 if (!S_ISDIR(inode->i_mode))
4546 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4547 buf->lb_buf, buf->lb_len);
4548 if (rc == -ERANGE) {
4549 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4552 lu_buf_realloc(buf, rc);
4553 if (buf->lb_buf == NULL)
4560 if (unlikely(rc == 0))
4566 if (unlikely(buf->lb_buf == NULL)) {
4567 lu_buf_realloc(buf, rc);
4568 if (buf->lb_buf == NULL)
4575 rc = linkea_init(&ldata);
4577 linkea_first_entry(&ldata);
4578 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4585 * Calls ->lookup() to find dentry. From dentry get inode and
4586 * read inode's ea to get fid. This is required for interoperability
4589 * \retval 0, on success
4590 * \retval -ve, on error
4592 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4593 struct dt_rec *rec, const struct dt_key *key)
4595 struct inode *dir = obj->oo_inode;
4596 struct dentry *dentry;
4597 struct ldiskfs_dir_entry_2 *de;
4598 struct buffer_head *bh;
4599 struct lu_fid *fid = (struct lu_fid *) rec;
4600 struct htree_lock *hlock = NULL;
4605 LASSERT(dir->i_op != NULL);
4606 LASSERT(dir->i_op->lookup != NULL);
4608 dentry = osd_child_dentry_get(env, obj,
4609 (char *)key, strlen((char *)key));
4611 if (obj->oo_hl_head != NULL) {
4612 hlock = osd_oti_get(env)->oti_hlock;
4613 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4614 dir, LDISKFS_HLOCK_LOOKUP);
4616 down_read(&obj->oo_ext_idx_sem);
4619 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4621 struct osd_thread_info *oti = osd_oti_get(env);
4622 struct osd_inode_id *id = &oti->oti_id;
4623 struct osd_idmap_cache *oic = &oti->oti_cache;
4624 struct osd_device *dev = osd_obj2dev(obj);
4626 ino = le32_to_cpu(de->inode);
4627 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4629 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4633 rc = osd_get_fid_from_dentry(de, rec);
4635 /* done with de, release bh */
4638 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4639 const char *name = (const char *)key;
4641 /* If the parent is on remote MDT, and there
4642 * is no FID-in-dirent, then we have to get
4643 * the parent FID from the linkEA. */
4644 if (likely(strlen(name) == 2 &&
4645 name[0] == '.' && name[1] == '.'))
4646 rc = osd_get_pfid_from_linkea(env, obj,
4649 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4652 osd_id_gen(id, ino, OSD_OII_NOGEN);
4655 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4656 fid_zero(&oic->oic_fid);
4661 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4662 rc = osd_consistency_check(oti, dev, oic);
4664 fid_zero(&oic->oic_fid);
4673 ldiskfs_htree_unlock(hlock);
4675 up_read(&obj->oo_ext_idx_sem);
4680 * Put the osd object once done with it.
4682 * \param obj osd object that needs to be put
4684 static inline void osd_object_put(const struct lu_env *env,
4685 struct osd_object *obj)
4687 lu_object_put(env, &obj->oo_dt.do_lu);
4690 static int osd_index_declare_ea_insert(const struct lu_env *env,
4691 struct dt_object *dt,
4692 const struct dt_rec *rec,
4693 const struct dt_key *key,
4694 struct thandle *handle)
4696 struct osd_thandle *oh;
4697 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4698 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4699 const struct lu_fid *fid = rec1->rec_fid;
4700 int credits, rc = 0;
4701 struct osd_idmap_cache *idc;
4704 LASSERT(!dt_object_remote(dt));
4705 LASSERT(handle != NULL);
4706 LASSERT(fid != NULL);
4707 LASSERT(rec1->rec_type != 0);
4709 oh = container_of0(handle, struct osd_thandle, ot_super);
4710 LASSERT(oh->ot_handle == NULL);
4712 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4714 /* we can't call iget() while a transactions is running
4715 * (this can lead to a deadlock), but we need to know
4716 * inum and object type. so we find this information at
4717 * declaration and cache in per-thread info */
4718 idc = osd_idc_find_or_init(env, osd, fid);
4720 RETURN(PTR_ERR(idc));
4721 if (idc->oic_remote) {
4722 /* a reference to remote inode is represented by an
4723 * agent inode which we have to create */
4724 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4725 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4728 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4730 if (osd_dt_obj(dt)->oo_inode != NULL) {
4731 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4733 /* We ignore block quota on meta pool (MDTs), so needn't
4734 * calculate how many blocks will be consumed by this index
4736 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4737 i_gid_read(inode), 0, oh,
4738 osd_dt_obj(dt), true, NULL, false);
4745 * Index add function for interoperability mode (b11826).
4746 * It will add the directory entry.This entry is needed to
4747 * maintain name->fid mapping.
4749 * \param key it is key i.e. file entry to be inserted
4750 * \param rec it is value of given key i.e. fid
4752 * \retval 0, on success
4753 * \retval -ve, on error
4755 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4756 const struct dt_rec *rec,
4757 const struct dt_key *key, struct thandle *th,
4760 struct osd_object *obj = osd_dt_obj(dt);
4761 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4762 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4763 const struct lu_fid *fid = rec1->rec_fid;
4764 const char *name = (const char *)key;
4765 struct osd_thread_info *oti = osd_oti_get(env);
4766 struct inode *child_inode = NULL;
4767 struct osd_idmap_cache *idc;
4771 if (!dt_object_exists(dt))
4774 LASSERT(osd_invariant(obj));
4775 LASSERT(!dt_object_remote(dt));
4776 LASSERT(th != NULL);
4778 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4780 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4782 idc = osd_idc_find(env, osd, fid);
4783 if (unlikely(idc == NULL)) {
4784 /* this dt_insert() wasn't declared properly, so
4785 * FID is missing in OI cache. we better do not
4786 * lookup FID in FLDB/OI and don't risk to deadlock,
4787 * but in some special cases (lfsck testing, etc)
4788 * it's much simpler than fixing a caller */
4789 CERROR("%s: "DFID" wasn't declared for insert\n",
4790 osd_name(osd), PFID(fid));
4792 idc = osd_idc_find_or_init(env, osd, fid);
4794 RETURN(PTR_ERR(idc));
4797 if (idc->oic_remote) {
4798 /* Insert remote entry */
4799 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4800 struct osd_mdobj_map *omm = osd->od_mdt_map;
4801 struct osd_thandle *oh;
4803 /* If parent on remote MDT, we need put this object
4805 oh = container_of(th, typeof(*oh), ot_super);
4806 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4808 CERROR("%s: add "DFID" error: rc = %d\n",
4810 PFID(lu_object_fid(&dt->do_lu)), rc);
4814 child_inode = igrab(omm->omm_remote_parent->d_inode);
4816 child_inode = osd_create_local_agent_inode(env, osd,
4817 obj, fid, rec1->rec_type & S_IFMT, th);
4818 if (IS_ERR(child_inode))
4819 RETURN(PTR_ERR(child_inode));
4822 /* Insert local entry */
4823 if (unlikely(idc->oic_lid.oii_ino == 0)) {
4824 /* for a reason OI cache wasn't filled properly */
4825 CERROR("%s: OIC for "DFID" isn't filled\n",
4826 osd_name(osd), PFID(fid));
4829 child_inode = oti->oti_inode;
4830 if (unlikely(child_inode == NULL)) {
4831 struct ldiskfs_inode_info *lii;
4835 child_inode = oti->oti_inode = &lii->vfs_inode;
4837 child_inode->i_sb = osd_sb(osd);
4838 child_inode->i_ino = idc->oic_lid.oii_ino;
4839 child_inode->i_mode = rec1->rec_type & S_IFMT;
4842 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4844 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4845 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4847 if (child_inode && child_inode != oti->oti_inode)
4849 LASSERT(osd_invariant(obj));
4850 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4855 * Initialize osd Iterator for given osd index object.
4857 * \param dt osd index object
4860 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4861 struct dt_object *dt,
4864 struct osd_it_iam *it;
4865 struct osd_object *obj = osd_dt_obj(dt);
4866 struct lu_object *lo = &dt->do_lu;
4867 struct iam_path_descr *ipd;
4868 struct iam_container *bag = &obj->oo_dir->od_container;
4870 if (!dt_object_exists(dt))
4871 return ERR_PTR(-ENOENT);
4875 return ERR_PTR(-ENOMEM);
4877 ipd = osd_it_ipd_get(env, bag);
4878 if (likely(ipd != NULL)) {
4882 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4883 return (struct dt_it *)it;
4886 return ERR_PTR(-ENOMEM);
4891 * free given Iterator.
4894 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4896 struct osd_it_iam *it = (struct osd_it_iam *)di;
4897 struct osd_object *obj = it->oi_obj;
4899 iam_it_fini(&it->oi_it);
4900 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4901 lu_object_put(env, &obj->oo_dt.do_lu);
4906 * Move Iterator to record specified by \a key
4908 * \param di osd iterator
4909 * \param key key for index
4911 * \retval +ve di points to record with least key not larger than key
4912 * \retval 0 di points to exact matched key
4913 * \retval -ve failure
4916 static int osd_it_iam_get(const struct lu_env *env,
4917 struct dt_it *di, const struct dt_key *key)
4919 struct osd_thread_info *oti = osd_oti_get(env);
4920 struct osd_it_iam *it = (struct osd_it_iam *)di;
4922 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4923 /* swab quota uid/gid */
4924 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4925 key = (struct dt_key *)&oti->oti_quota_id;
4928 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4934 * \param di osd iterator
4936 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4938 struct osd_it_iam *it = (struct osd_it_iam *)di;
4940 iam_it_put(&it->oi_it);
4944 * Move iterator by one record
4946 * \param di osd iterator
4948 * \retval +1 end of container reached
4950 * \retval -ve failure
4953 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4955 struct osd_it_iam *it = (struct osd_it_iam *)di;
4957 return iam_it_next(&it->oi_it);
4961 * Return pointer to the key under iterator.
4964 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4965 const struct dt_it *di)
4967 struct osd_thread_info *oti = osd_oti_get(env);
4968 struct osd_it_iam *it = (struct osd_it_iam *)di;
4969 struct osd_object *obj = it->oi_obj;
4972 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4974 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4975 /* swab quota uid/gid */
4976 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4977 key = (struct dt_key *)&oti->oti_quota_id;
4984 * Return size of key under iterator (in bytes)
4987 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4989 struct osd_it_iam *it = (struct osd_it_iam *)di;
4991 return iam_it_key_size(&it->oi_it);
4995 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4997 /* check if file type is required */
4998 if (ent->lde_attrs & LUDA_TYPE) {
4999 struct luda_type *lt;
5000 int align = sizeof(*lt) - 1;
5002 len = (len + align) & ~align;
5003 lt = (struct luda_type *)(ent->lde_name + len);
5004 lt->lt_type = cpu_to_le16(DTTOIF(type));
5007 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5011 * build lu direct from backend fs dirent.
5015 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5016 char *name, __u16 namelen, __u16 type, __u32 attr)
5018 ent->lde_attrs = attr | LUDA_FID;
5019 fid_cpu_to_le(&ent->lde_fid, fid);
5021 ent->lde_hash = cpu_to_le64(offset);
5022 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5024 strncpy(ent->lde_name, name, namelen);
5025 ent->lde_name[namelen] = '\0';
5026 ent->lde_namelen = cpu_to_le16(namelen);
5028 /* append lustre attributes */
5029 osd_it_append_attrs(ent, namelen, type);
5033 * Return pointer to the record under iterator.
5035 static int osd_it_iam_rec(const struct lu_env *env,
5036 const struct dt_it *di,
5037 struct dt_rec *dtrec, __u32 attr)
5039 struct osd_it_iam *it = (struct osd_it_iam *)di;
5040 struct osd_thread_info *info = osd_oti_get(env);
5043 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5044 const struct osd_fid_pack *rec;
5045 struct lu_fid *fid = &info->oti_fid;
5046 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5052 name = (char *)iam_it_key_get(&it->oi_it);
5054 RETURN(PTR_ERR(name));
5056 namelen = iam_it_key_size(&it->oi_it);
5058 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5060 RETURN(PTR_ERR(rec));
5062 rc = osd_fid_unpack(fid, rec);
5066 hash = iam_it_store(&it->oi_it);
5068 /* IAM does not store object type in IAM index (dir) */
5069 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5071 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5072 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5073 (struct iam_rec *)dtrec);
5074 osd_quota_unpack(it->oi_obj, dtrec);
5076 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5077 (struct iam_rec *)dtrec);
5084 * Returns cookie for current Iterator position.
5086 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5088 struct osd_it_iam *it = (struct osd_it_iam *)di;
5090 return iam_it_store(&it->oi_it);
5094 * Restore iterator from cookie.
5096 * \param di osd iterator
5097 * \param hash Iterator location cookie
5099 * \retval +ve di points to record with least key not larger than key.
5100 * \retval 0 di points to exact matched key
5101 * \retval -ve failure
5104 static int osd_it_iam_load(const struct lu_env *env,
5105 const struct dt_it *di, __u64 hash)
5107 struct osd_it_iam *it = (struct osd_it_iam *)di;
5109 return iam_it_load(&it->oi_it, hash);
5112 static const struct dt_index_operations osd_index_iam_ops = {
5113 .dio_lookup = osd_index_iam_lookup,
5114 .dio_declare_insert = osd_index_declare_iam_insert,
5115 .dio_insert = osd_index_iam_insert,
5116 .dio_declare_delete = osd_index_declare_iam_delete,
5117 .dio_delete = osd_index_iam_delete,
5119 .init = osd_it_iam_init,
5120 .fini = osd_it_iam_fini,
5121 .get = osd_it_iam_get,
5122 .put = osd_it_iam_put,
5123 .next = osd_it_iam_next,
5124 .key = osd_it_iam_key,
5125 .key_size = osd_it_iam_key_size,
5126 .rec = osd_it_iam_rec,
5127 .store = osd_it_iam_store,
5128 .load = osd_it_iam_load
5134 * Creates or initializes iterator context.
5136 * \retval struct osd_it_ea, iterator structure on success
5139 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5140 struct dt_object *dt,
5143 struct osd_object *obj = osd_dt_obj(dt);
5144 struct osd_thread_info *info = osd_oti_get(env);
5145 struct osd_it_ea *oie;
5147 struct lu_object *lo = &dt->do_lu;
5148 struct dentry *obj_dentry;
5151 if (!dt_object_exists(dt) || obj->oo_destroyed)
5152 RETURN(ERR_PTR(-ENOENT));
5154 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5156 RETURN(ERR_PTR(-ENOMEM));
5157 obj_dentry = &oie->oie_dentry;
5159 obj_dentry->d_inode = obj->oo_inode;
5160 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5161 obj_dentry->d_name.hash = 0;
5163 oie->oie_rd_dirent = 0;
5164 oie->oie_it_dirent = 0;
5165 oie->oie_dirent = NULL;
5166 if (unlikely(!info->oti_it_ea_buf_used)) {
5167 oie->oie_buf = info->oti_it_ea_buf;
5168 info->oti_it_ea_buf_used = 1;
5170 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5171 if (oie->oie_buf == NULL)
5172 RETURN(ERR_PTR(-ENOMEM));
5176 file = &oie->oie_file;
5178 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5179 if (attr & LUDA_64BITHASH)
5180 file->f_mode = FMODE_64BITHASH;
5182 file->f_mode = FMODE_32BITHASH;
5183 file->f_path.dentry = obj_dentry;
5184 file->f_mapping = obj->oo_inode->i_mapping;
5185 file->f_op = obj->oo_inode->i_fop;
5186 set_file_inode(file, obj->oo_inode);
5189 RETURN((struct dt_it *) oie);
5193 * Destroy or finishes iterator context.
5195 * \param di iterator structure to be destroyed
5197 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5199 struct osd_thread_info *info = osd_oti_get(env);
5200 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5201 struct osd_object *obj = oie->oie_obj;
5202 struct inode *inode = obj->oo_inode;
5205 oie->oie_file.f_op->release(inode, &oie->oie_file);
5206 lu_object_put(env, &obj->oo_dt.do_lu);
5207 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5208 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5210 info->oti_it_ea_buf_used = 0;
5211 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5216 * It position the iterator at given key, so that next lookup continues from
5217 * that key Or it is similar to dio_it->load() but based on a key,
5218 * rather than file position.
5220 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5223 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5225 static int osd_it_ea_get(const struct lu_env *env,
5226 struct dt_it *di, const struct dt_key *key)
5228 struct osd_it_ea *it = (struct osd_it_ea *)di;
5231 LASSERT(((const char *)key)[0] == '\0');
5232 it->oie_file.f_pos = 0;
5233 it->oie_rd_dirent = 0;
5234 it->oie_it_dirent = 0;
5235 it->oie_dirent = NULL;
5243 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5247 struct osd_filldir_cbs {
5248 #ifdef HAVE_DIR_CONTEXT
5249 struct dir_context ctx;
5251 struct osd_it_ea *it;
5254 * It is called internally by ->readdir(). It fills the
5255 * iterator's in-memory data structure with required
5256 * information i.e. name, namelen, rec_size etc.
5258 * \param buf in which information to be filled in.
5259 * \param name name of the file in given dir
5261 * \retval 0 on success
5262 * \retval 1 on buffer full
5264 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5265 loff_t offset, __u64 ino,
5268 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5269 struct osd_object *obj = it->oie_obj;
5270 struct osd_it_ea_dirent *ent = it->oie_dirent;
5271 struct lu_fid *fid = &ent->oied_fid;
5272 struct osd_fid_pack *rec;
5275 /* this should never happen */
5276 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5277 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5281 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5285 /* "." is just the object itself. */
5286 if (namelen == 1 && name[0] == '.') {
5287 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5288 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5289 rec = (struct osd_fid_pack*) (name + namelen + 1);
5290 if (osd_fid_unpack(fid, rec) != 0)
5295 d_type &= ~LDISKFS_DIRENT_LUFID;
5297 /* NOT export local root. */
5298 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5299 ino = obj->oo_inode->i_ino;
5300 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5303 ent->oied_ino = ino;
5304 ent->oied_off = offset;
5305 ent->oied_namelen = namelen;
5306 ent->oied_type = d_type;
5308 memcpy(ent->oied_name, name, namelen);
5310 it->oie_rd_dirent++;
5311 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5316 * Calls ->readdir() to load a directory entry at a time
5317 * and stored it in iterator's in-memory data structure.
5319 * \param di iterator's in memory structure
5321 * \retval 0 on success
5322 * \retval -ve on error
5323 * \retval +1 reach the end of entry
5325 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5326 const struct dt_it *di)
5328 struct osd_it_ea *it = (struct osd_it_ea *)di;
5329 struct osd_object *obj = it->oie_obj;
5330 struct inode *inode = obj->oo_inode;
5331 struct htree_lock *hlock = NULL;
5332 struct file *filp = &it->oie_file;
5334 struct osd_filldir_cbs buf = {
5335 #ifdef HAVE_DIR_CONTEXT
5336 .ctx.actor = osd_ldiskfs_filldir,
5342 it->oie_dirent = it->oie_buf;
5343 it->oie_rd_dirent = 0;
5345 if (obj->oo_hl_head != NULL) {
5346 hlock = osd_oti_get(env)->oti_hlock;
5347 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5348 inode, LDISKFS_HLOCK_READDIR);
5350 down_read(&obj->oo_ext_idx_sem);
5353 #ifdef HAVE_DIR_CONTEXT
5354 buf.ctx.pos = filp->f_pos;
5355 rc = inode->i_fop->iterate(filp, &buf.ctx);
5356 filp->f_pos = buf.ctx.pos;
5358 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5362 ldiskfs_htree_unlock(hlock);
5364 up_read(&obj->oo_ext_idx_sem);
5366 if (it->oie_rd_dirent == 0) {
5367 /*If it does not get any dirent, it means it has been reached
5368 *to the end of the dir */
5369 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5373 it->oie_dirent = it->oie_buf;
5374 it->oie_it_dirent = 1;
5381 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5382 * to load a directory entry at a time and stored it in
5383 * iterator's in-memory data structure.
5385 * \param di iterator's in memory structure
5387 * \retval +ve iterator reached to end
5388 * \retval 0 iterator not reached to end
5389 * \retval -ve on error
5391 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5393 struct osd_it_ea *it = (struct osd_it_ea *)di;
5398 if (it->oie_it_dirent < it->oie_rd_dirent) {
5400 (void *) it->oie_dirent +
5401 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5402 it->oie_dirent->oied_namelen);
5403 it->oie_it_dirent++;
5406 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5409 rc = osd_ldiskfs_it_fill(env, di);
5416 * Returns the key at current position from iterator's in memory structure.
5418 * \param di iterator's in memory structure
5420 * \retval key i.e. struct dt_key on success
5422 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5423 const struct dt_it *di)
5425 struct osd_it_ea *it = (struct osd_it_ea *)di;
5427 return (struct dt_key *)it->oie_dirent->oied_name;
5431 * Returns the key's size at current position from iterator's in memory structure.
5433 * \param di iterator's in memory structure
5435 * \retval key_size i.e. struct dt_key on success
5437 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5439 struct osd_it_ea *it = (struct osd_it_ea *)di;
5441 return it->oie_dirent->oied_namelen;
5445 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5447 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5448 "dot_dotdot = %d\n", dot_dotdot);
5450 if (LDISKFS_DIR_REC_LEN(de) >=
5451 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5458 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5459 unsigned blocksize, int dot_dotdot)
5462 return osd_dot_dotdot_has_space(de, dot_dotdot);
5464 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5465 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5472 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5473 struct dentry *dentry, const struct lu_fid *fid,
5474 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5475 struct htree_lock *hlock, int dot_dotdot)
5477 struct inode *dir = dentry->d_parent->d_inode;
5478 struct inode *inode = dentry->d_inode;
5479 struct osd_fid_pack *rec;
5480 struct ldiskfs_dentry_param *ldp;
5481 int namelen = dentry->d_name.len;
5483 struct osd_thread_info *info = osd_oti_get(env);
5486 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5487 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5490 /* There is enough space to hold the FID-in-dirent. */
5491 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5493 rc = ldiskfs_journal_get_write_access(jh, bh);
5497 de->name[namelen] = 0;
5498 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5499 rec->fp_len = sizeof(struct lu_fid) + 1;
5500 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5501 de->file_type |= LDISKFS_DIRENT_LUFID;
5502 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5507 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5509 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5513 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5514 osd_get_ldiskfs_dirent_param(ldp, fid);
5515 dentry->d_fsdata = (void *)ldp;
5516 ll_vfs_dq_init(dir);
5517 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5518 /* It is too bad, we cannot reinsert the name entry back.
5519 * That means we lose it! */
5521 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5522 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5523 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5524 dir->i_ino, dir->i_generation, namelen,
5525 dentry->d_name.name, PFID(fid), rc);
5531 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5532 struct osd_it_ea *it, struct lu_fid *fid,
5533 struct osd_inode_id *id, __u32 *attr)
5535 struct osd_thread_info *info = osd_oti_get(env);
5536 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5537 struct osd_device *dev = osd_obj2dev(obj);
5538 struct super_block *sb = osd_sb(dev);
5539 const char *devname =
5540 LDISKFS_SB(sb)->s_es->s_volume_name;
5541 struct osd_it_ea_dirent *ent = it->oie_dirent;
5542 struct inode *dir = obj->oo_inode;
5543 struct htree_lock *hlock = NULL;
5544 struct buffer_head *bh = NULL;
5545 handle_t *jh = NULL;
5546 struct ldiskfs_dir_entry_2 *de;
5547 struct dentry *dentry;
5548 struct inode *inode;
5555 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5556 inode = osd_iget(info, dev, id);
5557 if (IS_ERR(inode)) {
5558 rc = PTR_ERR(inode);
5559 if (rc == -ENOENT || rc == -ESTALE) {
5560 *attr |= LUDA_UNKNOWN;
5563 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5564 "check_repair, dir = %lu/%u, name = %.*s: "
5566 devname, dir->i_ino, dir->i_generation,
5567 ent->oied_namelen, ent->oied_name, rc);
5573 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5575 rc = osd_get_lma(info, inode, dentry, lma);
5581 if (ent->oied_name[0] == '.') {
5582 if (ent->oied_namelen == 1)
5584 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5588 /* We need to ensure that the name entry is still valid.
5589 * Because it may be removed or renamed by other already.
5591 * The unlink or rename operation will start journal before PDO lock,
5592 * so to avoid deadlock, here we need to start journal handle before
5593 * related PDO lock also. But because we do not know whether there
5594 * will be something to be repaired before PDO lock, we just start
5595 * journal without conditions.
5597 * We may need to remove the name entry firstly, then insert back.
5598 * One credit is for user quota file update.
5599 * One credit is for group quota file update.
5600 * Two credits are for dirty inode. */
5601 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5602 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5604 if (dev->od_dirent_journal != 0) {
5607 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5610 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5611 "check_repair, dir = %lu/%u, credits = %d, "
5612 "name = %.*s: rc = %d\n",
5613 devname, dir->i_ino, dir->i_generation, credits,
5614 ent->oied_namelen, ent->oied_name, rc);
5616 GOTO(out_inode, rc);
5619 if (obj->oo_hl_head != NULL) {
5620 hlock = osd_oti_get(env)->oti_hlock;
5621 /* "0" means exclusive lock for the whole directory.
5622 * We need to prevent others access such name entry
5623 * during the delete + insert. Neither HLOCK_ADD nor
5624 * HLOCK_DEL cannot guarantee the atomicity. */
5625 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5627 down_write(&obj->oo_ext_idx_sem);
5630 if (obj->oo_hl_head != NULL) {
5631 hlock = osd_oti_get(env)->oti_hlock;
5632 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5633 LDISKFS_HLOCK_LOOKUP);
5635 down_read(&obj->oo_ext_idx_sem);
5639 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5640 /* For dot/dotdot entry, if there is not enough space to hold the
5641 * FID-in-dirent, just keep them there. It only happens when the
5642 * device upgraded from 1.8 or restored from MDT file-level backup.
5643 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5644 * and needs to get FID from LMA when readdir, it will not affect the
5645 * performance much. */
5646 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5647 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5648 *attr |= LUDA_IGNORE;
5654 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5655 struct lu_fid *tfid = &lma->lma_self_fid;
5657 *attr |= LUDA_IGNORE;
5658 /* It must be REMOTE_PARENT_DIR and as the
5659 * dotdot entry of remote directory */
5660 if (unlikely(dot_dotdot != 2 ||
5661 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5662 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5663 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5664 "parent directory, but got %.*s under "
5665 "dir = %lu/%u with the FID "DFID"\n",
5666 devname, ent->oied_namelen,
5667 ent->oied_name, dir->i_ino,
5668 dir->i_generation, PFID(tfid));
5670 GOTO(out, rc = -EIO);
5676 if (fid_is_sane(fid)) {
5677 /* FID-in-dirent is valid. */
5678 if (lu_fid_eq(fid, &lma->lma_self_fid))
5681 /* Do not repair under dryrun mode. */
5682 if (*attr & LUDA_VERIFY_DRYRUN) {
5683 *attr |= LUDA_REPAIR;
5690 dev->od_dirent_journal = 1;
5691 if (hlock != NULL) {
5692 ldiskfs_htree_unlock(hlock);
5695 up_read(&obj->oo_ext_idx_sem);
5701 *fid = lma->lma_self_fid;
5703 /* Update the FID-in-dirent. */
5704 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5707 *attr |= LUDA_REPAIR;
5709 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5710 "in the dirent, dir = %lu/%u, "
5711 "name = %.*s, "DFID": rc = %d\n",
5712 devname, dir->i_ino, dir->i_generation,
5713 ent->oied_namelen, ent->oied_name,
5716 /* Do not repair under dryrun mode. */
5717 if (*attr & LUDA_VERIFY_DRYRUN) {
5718 *fid = lma->lma_self_fid;
5719 *attr |= LUDA_REPAIR;
5726 dev->od_dirent_journal = 1;
5727 if (hlock != NULL) {
5728 ldiskfs_htree_unlock(hlock);
5731 up_read(&obj->oo_ext_idx_sem);
5737 *fid = lma->lma_self_fid;
5739 /* Append the FID-in-dirent. */
5740 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5743 *attr |= LUDA_REPAIR;
5745 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5746 "after the dirent, dir = %lu/%u, "
5747 "name = %.*s, "DFID": rc = %d\n",
5748 devname, dir->i_ino, dir->i_generation,
5749 ent->oied_namelen, ent->oied_name,
5753 /* Do not repair under dryrun mode. */
5754 if (*attr & LUDA_VERIFY_DRYRUN) {
5755 if (fid_is_sane(fid)) {
5756 *attr |= LUDA_REPAIR;
5758 lu_igif_build(fid, inode->i_ino,
5759 inode->i_generation);
5760 *attr |= LUDA_UPGRADE;
5768 dev->od_dirent_journal = 1;
5769 if (hlock != NULL) {
5770 ldiskfs_htree_unlock(hlock);
5773 up_read(&obj->oo_ext_idx_sem);
5780 if (unlikely(fid_is_sane(fid))) {
5781 /* FID-in-dirent exists, but FID-in-LMA is lost.
5782 * Trust the FID-in-dirent, and add FID-in-LMA. */
5783 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5785 *attr |= LUDA_REPAIR;
5787 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5788 "update dirent, dir = %lu/%u, "
5789 "name = %.*s, "DFID": rc = %d\n",
5790 devname, dir->i_ino, dir->i_generation,
5791 ent->oied_namelen, ent->oied_name,
5794 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5795 /* It is probably IGIF object. Only aappend the
5796 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5797 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5800 *attr |= LUDA_UPGRADE;
5802 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5803 "after the dirent, dir = %lu/%u, "
5804 "name = %.*s, "DFID": rc = %d\n",
5805 devname, dir->i_ino, dir->i_generation,
5806 ent->oied_namelen, ent->oied_name,
5815 if (hlock != NULL) {
5816 ldiskfs_htree_unlock(hlock);
5818 if (dev->od_dirent_journal != 0)
5819 up_write(&obj->oo_ext_idx_sem);
5821 up_read(&obj->oo_ext_idx_sem);
5825 ldiskfs_journal_stop(jh);
5829 if (rc >= 0 && !dirty)
5830 dev->od_dirent_journal = 0;
5836 * Returns the value at current position from iterator's in memory structure.
5838 * \param di struct osd_it_ea, iterator's in memory structure
5839 * \param attr attr requested for dirent.
5840 * \param lde lustre dirent
5842 * \retval 0 no error and \param lde has correct lustre dirent.
5843 * \retval -ve on error
5845 static inline int osd_it_ea_rec(const struct lu_env *env,
5846 const struct dt_it *di,
5847 struct dt_rec *dtrec, __u32 attr)
5849 struct osd_it_ea *it = (struct osd_it_ea *)di;
5850 struct osd_object *obj = it->oie_obj;
5851 struct osd_device *dev = osd_obj2dev(obj);
5852 struct osd_thread_info *oti = osd_oti_get(env);
5853 struct osd_inode_id *id = &oti->oti_id;
5854 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5855 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5856 __u32 ino = it->oie_dirent->oied_ino;
5860 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5862 if (attr & LUDA_VERIFY) {
5863 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5864 attr |= LUDA_IGNORE;
5865 /* If the parent is on remote MDT, and there
5866 * is no FID-in-dirent, then we have to get
5867 * the parent FID from the linkEA. */
5868 if (!fid_is_sane(fid) &&
5869 it->oie_dirent->oied_namelen == 2 &&
5870 it->oie_dirent->oied_name[0] == '.' &&
5871 it->oie_dirent->oied_name[1] == '.')
5872 osd_get_pfid_from_linkea(env, obj, fid);
5874 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5878 if (!fid_is_sane(fid)) {
5879 attr &= ~LUDA_IGNORE;
5880 attr |= LUDA_UNKNOWN;
5883 attr &= ~LU_DIRENT_ATTRS_MASK;
5884 if (!fid_is_sane(fid)) {
5885 bool is_dotdot = false;
5886 if (it->oie_dirent->oied_namelen == 2 &&
5887 it->oie_dirent->oied_name[0] == '.' &&
5888 it->oie_dirent->oied_name[1] == '.')
5890 /* If the parent is on remote MDT, and there
5891 * is no FID-in-dirent, then we have to get
5892 * the parent FID from the linkEA. */
5893 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5894 rc = osd_get_pfid_from_linkea(env, obj, fid);
5896 if (is_dotdot == false &&
5897 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5900 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5903 osd_id_gen(id, ino, OSD_OII_NOGEN);
5907 /* Pack the entry anyway, at least the offset is right. */
5908 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5909 it->oie_dirent->oied_name,
5910 it->oie_dirent->oied_namelen,
5911 it->oie_dirent->oied_type, attr);
5916 if (osd_remote_fid(env, dev, fid))
5919 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5920 osd_add_oi_cache(oti, dev, id, fid);
5922 RETURN(rc > 0 ? 0 : rc);
5926 * Returns the record size size at current position.
5928 * This function will return record(lu_dirent) size in bytes.
5930 * \param[in] env execution environment
5931 * \param[in] di iterator's in memory structure
5932 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5933 * calculate the lu_dirent size.
5935 * \retval record size(in bytes & in memory) of the current lu_dirent
5938 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5941 struct osd_it_ea *it = (struct osd_it_ea *)di;
5943 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5947 * Returns a cookie for current position of the iterator head, so that
5948 * user can use this cookie to load/start the iterator next time.
5950 * \param di iterator's in memory structure
5952 * \retval cookie for current position, on success
5954 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5956 struct osd_it_ea *it = (struct osd_it_ea *)di;
5958 return it->oie_dirent->oied_off;
5962 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5963 * to load a directory entry at a time and stored it i inn,
5964 * in iterator's in-memory data structure.
5966 * \param di struct osd_it_ea, iterator's in memory structure
5968 * \retval +ve on success
5969 * \retval -ve on error
5971 static int osd_it_ea_load(const struct lu_env *env,
5972 const struct dt_it *di, __u64 hash)
5974 struct osd_it_ea *it = (struct osd_it_ea *)di;
5978 it->oie_file.f_pos = hash;
5980 rc = osd_ldiskfs_it_fill(env, di);
5991 * Index lookup function for interoperability mode (b11826).
5993 * \param key, key i.e. file name to be searched
5995 * \retval +ve, on success
5996 * \retval -ve, on error
5998 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5999 struct dt_rec *rec, const struct dt_key *key)
6001 struct osd_object *obj = osd_dt_obj(dt);
6006 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6007 LINVRNT(osd_invariant(obj));
6009 rc = osd_ea_lookup_rec(env, obj, rec, key);
6016 * Index and Iterator operations for interoperability
6017 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6019 static const struct dt_index_operations osd_index_ea_ops = {
6020 .dio_lookup = osd_index_ea_lookup,
6021 .dio_declare_insert = osd_index_declare_ea_insert,
6022 .dio_insert = osd_index_ea_insert,
6023 .dio_declare_delete = osd_index_declare_ea_delete,
6024 .dio_delete = osd_index_ea_delete,
6026 .init = osd_it_ea_init,
6027 .fini = osd_it_ea_fini,
6028 .get = osd_it_ea_get,
6029 .put = osd_it_ea_put,
6030 .next = osd_it_ea_next,
6031 .key = osd_it_ea_key,
6032 .key_size = osd_it_ea_key_size,
6033 .rec = osd_it_ea_rec,
6034 .rec_size = osd_it_ea_rec_size,
6035 .store = osd_it_ea_store,
6036 .load = osd_it_ea_load
6040 static void *osd_key_init(const struct lu_context *ctx,
6041 struct lu_context_key *key)
6043 struct osd_thread_info *info;
6045 OBD_ALLOC_PTR(info);
6047 return ERR_PTR(-ENOMEM);
6049 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6050 if (info->oti_it_ea_buf == NULL)
6053 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6055 info->oti_hlock = ldiskfs_htree_lock_alloc();
6056 if (info->oti_hlock == NULL)
6062 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6065 return ERR_PTR(-ENOMEM);
6068 static void osd_key_fini(const struct lu_context *ctx,
6069 struct lu_context_key *key, void* data)
6071 struct osd_thread_info *info = data;
6072 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6073 struct osd_idmap_cache *idc = info->oti_ins_cache;
6075 if (info->oti_inode != NULL)
6077 if (info->oti_hlock != NULL)
6078 ldiskfs_htree_lock_free(info->oti_hlock);
6079 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6080 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6081 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6082 lu_buf_free(&info->oti_big_buf);
6084 LASSERT(info->oti_ins_cache_size > 0);
6085 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6086 info->oti_ins_cache = NULL;
6087 info->oti_ins_cache_size = 0;
6092 static void osd_key_exit(const struct lu_context *ctx,
6093 struct lu_context_key *key, void *data)
6095 struct osd_thread_info *info = data;
6097 LASSERT(info->oti_r_locks == 0);
6098 LASSERT(info->oti_w_locks == 0);
6099 LASSERT(info->oti_txns == 0);
6102 /* type constructor/destructor: osd_type_init, osd_type_fini */
6103 LU_TYPE_INIT_FINI(osd, &osd_key);
6105 struct lu_context_key osd_key = {
6106 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6107 .lct_init = osd_key_init,
6108 .lct_fini = osd_key_fini,
6109 .lct_exit = osd_key_exit
6113 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6114 const char *name, struct lu_device *next)
6116 struct osd_device *osd = osd_dev(d);
6118 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6119 >= sizeof(osd->od_svname))
6121 return osd_procfs_init(osd, name);
6124 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6126 struct seq_server_site *ss = osd_seq_site(osd);
6130 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6133 if (unlikely(ss == NULL))
6136 OBD_ALLOC_PTR(osd->od_cl_seq);
6137 if (osd->od_cl_seq == NULL)
6140 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6141 osd->od_svname, ss->ss_server_seq);
6143 OBD_FREE_PTR(osd->od_cl_seq);
6144 osd->od_cl_seq = NULL;
6148 if (ss->ss_node_id == 0) {
6149 /* If the OSD on the sequence controller(MDT0), then allocate
6150 * sequence here, otherwise allocate sequence after connected
6151 * to MDT0 (see mdt_register_lwp_callback()). */
6152 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6153 &osd->od_cl_seq->lcs_space, env);
6159 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6161 if (osd->od_cl_seq == NULL)
6164 seq_client_fini(osd->od_cl_seq);
6165 OBD_FREE_PTR(osd->od_cl_seq);
6166 osd->od_cl_seq = NULL;
6169 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6173 /* shutdown quota slave instance associated with the device */
6174 if (o->od_quota_slave != NULL) {
6175 qsd_fini(env, o->od_quota_slave);
6176 o->od_quota_slave = NULL;
6179 osd_fid_fini(env, o);
6184 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6188 if (o->od_mnt != NULL) {
6189 shrink_dcache_sb(osd_sb(o));
6190 osd_sync(env, &o->od_dt_dev);
6199 static int osd_mount(const struct lu_env *env,
6200 struct osd_device *o, struct lustre_cfg *cfg)
6202 const char *name = lustre_cfg_string(cfg, 0);
6203 const char *dev = lustre_cfg_string(cfg, 1);
6205 unsigned long page, s_flags, lmd_flags = 0;
6206 struct page *__page;
6207 struct file_system_type *type;
6208 char *options = NULL;
6210 struct osd_thread_info *info = osd_oti_get(env);
6211 struct lu_fid *fid = &info->oti_fid;
6212 struct inode *inode;
6213 int rc = 0, force_over_128tb = 0;
6216 if (o->od_mnt != NULL)
6219 if (strlen(dev) >= sizeof(o->od_mntdev))
6221 strcpy(o->od_mntdev, dev);
6223 str = lustre_cfg_string(cfg, 2);
6224 s_flags = simple_strtoul(str, NULL, 0);
6225 str = strstr(str, ":");
6227 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6228 opts = lustre_cfg_string(cfg, 3);
6230 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6231 CERROR("%s: device %s extents feature is not guaranteed to "
6232 "work on big-endian systems. Use \"bigendian_extents\" "
6233 "mount option to override.\n", name, dev);
6237 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
6238 force_over_128tb = 1;
6240 __page = alloc_page(GFP_IOFS);
6242 GOTO(out, rc = -ENOMEM);
6243 page = (unsigned long)page_address(__page);
6244 options = (char *)page;
6247 /* strip out the options for back compatiblity */
6248 static char *sout[] = {
6255 /* strip out option we processed in osd */
6256 "bigendian_extents",
6260 strcat(options, opts);
6261 for (rc = 0, str = options; sout[rc]; ) {
6262 char *op = strstr(str, sout[rc]);
6268 if (op == options || *(op - 1) == ',') {
6269 str = op + strlen(sout[rc]);
6270 if (*str == ',' || *str == '\0') {
6271 *str == ',' ? str++ : str;
6272 memmove(op, str, strlen(str) + 1);
6275 for (str = op; *str != ',' && *str != '\0'; str++)
6279 strncat(options, "user_xattr,acl", 14);
6282 /* Glom up mount options */
6283 if (*options != '\0')
6284 strcat(options, ",");
6285 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6287 type = get_fs_type("ldiskfs");
6289 CERROR("%s: cannot find ldiskfs module\n", name);
6290 GOTO(out, rc = -ENODEV);
6293 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6294 module_put(type->owner);
6296 if (IS_ERR(o->od_mnt)) {
6297 rc = PTR_ERR(o->od_mnt);
6299 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6303 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
6304 force_over_128tb == 0) {
6305 CERROR("%s: device %s LDISKFS does not support filesystems "
6306 "greater than 128TB and can cause data corruption. "
6307 "Use \"force_over_128tb\" mount option to override.\n",
6309 GOTO(out, rc = -EINVAL);
6312 #ifdef HAVE_DEV_SET_RDONLY
6313 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6314 CERROR("%s: underlying device %s is marked as read-only. "
6315 "Setup failed\n", name, dev);
6316 GOTO(out_mnt, rc = -EROFS);
6320 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6321 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6322 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6323 GOTO(out_mnt, rc = -EINVAL);
6326 #ifdef LDISKFS_MOUNT_DIRDATA
6327 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6328 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6329 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6330 else if (!o->od_is_ost)
6331 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6332 "enabling the dirdata feature. If you do not want to "
6333 "downgrade to Lustre-1.x again, you can enable it via "
6334 "'tune2fs -O dirdata device'\n", name, dev);
6336 inode = osd_sb(o)->s_root->d_inode;
6337 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6338 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6340 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6344 if (lmd_flags & LMD_FLG_NOSCRUB)
6355 __free_page(__page);
6360 static struct lu_device *osd_device_fini(const struct lu_env *env,
6361 struct lu_device *d)
6363 struct osd_device *o = osd_dev(d);
6366 osd_shutdown(env, o);
6368 osd_scrub_cleanup(env, o);
6369 osd_obj_map_fini(o);
6375 static int osd_device_init0(const struct lu_env *env,
6376 struct osd_device *o,
6377 struct lustre_cfg *cfg)
6379 struct lu_device *l = osd2lu_dev(o);
6380 struct osd_thread_info *info;
6384 /* if the module was re-loaded, env can loose its keys */
6385 rc = lu_env_refill((struct lu_env *) env);
6388 info = osd_oti_get(env);
6391 l->ld_ops = &osd_lu_ops;
6392 o->od_dt_dev.dd_ops = &osd_dt_ops;
6394 spin_lock_init(&o->od_osfs_lock);
6395 mutex_init(&o->od_otable_mutex);
6396 INIT_LIST_HEAD(&o->od_orphan_list);
6398 o->od_read_cache = 1;
6399 o->od_writethrough_cache = 1;
6400 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6402 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6403 sizeof(o->od_svname));
6404 if (cplen >= sizeof(o->od_svname)) {
6409 o->od_index = -1; /* -1 means index is invalid */
6410 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6411 if (rc == LDD_F_SV_TYPE_OST)
6414 o->od_full_scrub_ratio = OFSR_DEFAULT;
6415 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6416 rc = osd_mount(env, o, cfg);
6420 rc = osd_obj_map_init(env, o);
6424 rc = lu_site_init(&o->od_site, l);
6426 GOTO(out_compat, rc);
6427 o->od_site.ls_bottom_dev = l;
6429 rc = lu_site_init_finish(&o->od_site);
6433 INIT_LIST_HEAD(&o->od_ios_list);
6434 /* setup scrub, including OI files initialization */
6435 rc = osd_scrub_setup(env, o);
6439 rc = osd_procfs_init(o, o->od_svname);
6441 CERROR("%s: can't initialize procfs: rc = %d\n",
6443 GOTO(out_scrub, rc);
6446 LASSERT(l->ld_site->ls_linkage.next != NULL);
6447 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6449 /* initialize quota slave instance */
6450 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6452 if (IS_ERR(o->od_quota_slave)) {
6453 rc = PTR_ERR(o->od_quota_slave);
6454 o->od_quota_slave = NULL;
6455 GOTO(out_procfs, rc);
6463 osd_scrub_cleanup(env, o);
6465 lu_site_fini(&o->od_site);
6467 osd_obj_map_fini(o);
6474 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6475 struct lu_device_type *t,
6476 struct lustre_cfg *cfg)
6478 struct osd_device *o;
6483 return ERR_PTR(-ENOMEM);
6485 rc = dt_device_init(&o->od_dt_dev, t);
6487 /* Because the ctx might be revived in dt_device_init,
6488 * refill the env here */
6489 lu_env_refill((struct lu_env *)env);
6490 rc = osd_device_init0(env, o, cfg);
6492 dt_device_fini(&o->od_dt_dev);
6495 if (unlikely(rc != 0))
6498 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6501 static struct lu_device *osd_device_free(const struct lu_env *env,
6502 struct lu_device *d)
6504 struct osd_device *o = osd_dev(d);
6507 /* XXX: make osd top device in order to release reference */
6508 d->ld_site->ls_top_dev = d;
6509 lu_site_purge(env, d->ld_site, -1);
6510 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6511 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6512 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6514 lu_site_fini(&o->od_site);
6515 dt_device_fini(&o->od_dt_dev);
6520 static int osd_process_config(const struct lu_env *env,
6521 struct lu_device *d, struct lustre_cfg *cfg)
6523 struct osd_device *o = osd_dev(d);
6527 switch (cfg->lcfg_command) {
6529 rc = osd_mount(env, o, cfg);
6532 lu_dev_del_linkage(d->ld_site, d);
6533 rc = osd_shutdown(env, o);
6536 LASSERT(&o->od_dt_dev);
6537 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6538 cfg, &o->od_dt_dev);
6539 if (rc > 0 || rc == -ENOSYS)
6540 rc = class_process_proc_param(PARAM_OST,
6541 lprocfs_osd_obd_vars,
6542 cfg, &o->od_dt_dev);
6551 static int osd_recovery_complete(const struct lu_env *env,
6552 struct lu_device *d)
6554 struct osd_device *osd = osd_dev(d);
6558 if (osd->od_quota_slave == NULL)
6561 /* start qsd instance on recovery completion, this notifies the quota
6562 * slave code that we are about to process new requests now */
6563 rc = qsd_start(env, osd->od_quota_slave);
6568 * we use exports to track all osd users
6570 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6571 struct obd_device *obd, struct obd_uuid *cluuid,
6572 struct obd_connect_data *data, void *localdata)
6574 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6575 struct lustre_handle conn;
6579 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6581 rc = class_connect(&conn, obd, cluuid);
6585 *exp = class_conn2export(&conn);
6587 spin_lock(&osd->od_osfs_lock);
6589 spin_unlock(&osd->od_osfs_lock);
6595 * once last export (we don't count self-export) disappeared
6596 * osd can be released
6598 static int osd_obd_disconnect(struct obd_export *exp)
6600 struct obd_device *obd = exp->exp_obd;
6601 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6602 int rc, release = 0;
6605 /* Only disconnect the underlying layers on the final disconnect. */
6606 spin_lock(&osd->od_osfs_lock);
6608 if (osd->od_connects == 0)
6610 spin_unlock(&osd->od_osfs_lock);
6612 rc = class_disconnect(exp); /* bz 9811 */
6614 if (rc == 0 && release)
6615 class_manual_cleanup(obd);
6619 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6620 struct lu_device *dev)
6622 struct osd_device *osd = osd_dev(dev);
6623 struct lr_server_data *lsd =
6624 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6628 if (osd->od_quota_slave != NULL) {
6629 /* set up quota slave objects */
6630 result = qsd_prepare(env, osd->od_quota_slave);
6635 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6636 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6637 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6638 osd->od_index_in_idif = 1;
6640 osd->od_index_in_idif = 0;
6641 result = osd_register_proc_index_in_idif(osd);
6646 osd->od_index_in_idif = 1;
6650 result = osd_fid_init(env, osd);
6655 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6656 struct lu_fid *fid, struct md_op_data *op_data)
6658 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6660 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6663 static const struct lu_object_operations osd_lu_obj_ops = {
6664 .loo_object_init = osd_object_init,
6665 .loo_object_delete = osd_object_delete,
6666 .loo_object_release = osd_object_release,
6667 .loo_object_free = osd_object_free,
6668 .loo_object_print = osd_object_print,
6669 .loo_object_invariant = osd_object_invariant
6672 const struct lu_device_operations osd_lu_ops = {
6673 .ldo_object_alloc = osd_object_alloc,
6674 .ldo_process_config = osd_process_config,
6675 .ldo_recovery_complete = osd_recovery_complete,
6676 .ldo_prepare = osd_prepare,
6679 static const struct lu_device_type_operations osd_device_type_ops = {
6680 .ldto_init = osd_type_init,
6681 .ldto_fini = osd_type_fini,
6683 .ldto_start = osd_type_start,
6684 .ldto_stop = osd_type_stop,
6686 .ldto_device_alloc = osd_device_alloc,
6687 .ldto_device_free = osd_device_free,
6689 .ldto_device_init = osd_device_init,
6690 .ldto_device_fini = osd_device_fini
6693 static struct lu_device_type osd_device_type = {
6694 .ldt_tags = LU_DEVICE_DT,
6695 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6696 .ldt_ops = &osd_device_type_ops,
6697 .ldt_ctx_tags = LCT_LOCAL,
6700 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6702 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6703 struct super_block *sb = osd_sb(osd);
6705 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6709 * lprocfs legacy support.
6711 static struct obd_ops osd_obd_device_ops = {
6712 .o_owner = THIS_MODULE,
6713 .o_connect = osd_obd_connect,
6714 .o_disconnect = osd_obd_disconnect,
6715 .o_fid_alloc = osd_fid_alloc,
6716 .o_health_check = osd_health_check,
6719 static int __init osd_init(void)
6723 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6724 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6725 /* please, try to keep osd_thread_info smaller than a page */
6726 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6731 rc = lu_kmem_init(ldiskfs_caches);
6735 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6736 lprocfs_osd_module_vars,
6737 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6739 lu_kmem_fini(ldiskfs_caches);
6743 static void __exit osd_exit(void)
6745 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6746 lu_kmem_fini(ldiskfs_caches);
6749 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6750 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6751 MODULE_VERSION(LUSTRE_VERSION_STRING);
6752 MODULE_LICENSE("GPL");
6754 module_init(osd_init);
6755 module_exit(osd_exit);