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)
1556 struct osd_object *o = osd_obj(l);
1557 /* nobody should be releasing a non-destroyed object with nlink=0
1558 * the API allows this, but ldiskfs doesn't like and then report
1559 * this inode as deleted */
1560 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1565 * Concurrency: shouldn't matter.
1567 static int osd_object_print(const struct lu_env *env, void *cookie,
1568 lu_printer_t p, const struct lu_object *l)
1570 struct osd_object *o = osd_obj(l);
1571 struct iam_descr *d;
1573 if (o->oo_dir != NULL)
1574 d = o->oo_dir->od_container.ic_descr;
1577 return (*p)(env, cookie,
1578 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1580 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1581 o->oo_inode ? o->oo_inode->i_generation : 0,
1582 d ? d->id_ops->id_name : "plain");
1585 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1588 * Concurrency: shouldn't matter.
1590 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1591 struct obd_statfs *sfs)
1593 struct osd_device *osd = osd_dt_dev(d);
1594 struct super_block *sb = osd_sb(osd);
1595 struct kstatfs *ksfs;
1598 if (unlikely(osd->od_mnt == NULL))
1599 return -EINPROGRESS;
1601 /* osd_lproc.c call this without env, allocate ksfs for that case */
1602 if (unlikely(env == NULL)) {
1603 OBD_ALLOC_PTR(ksfs);
1607 ksfs = &osd_oti_get(env)->oti_ksfs;
1610 spin_lock(&osd->od_osfs_lock);
1611 result = sb->s_op->statfs(sb->s_root, ksfs);
1612 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1613 statfs_pack(sfs, ksfs);
1614 if (unlikely(sb->s_flags & MS_RDONLY))
1615 sfs->os_state = OS_STATE_READONLY;
1616 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1617 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1618 sfs->os_maxbytes = sb->s_maxbytes;
1620 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1622 spin_unlock(&osd->od_osfs_lock);
1624 if (unlikely(env == NULL))
1627 /* Reserve a small amount of space for local objects like last_rcvd,
1628 * llog, quota files, ... */
1629 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1632 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1633 /** Take out metadata overhead for indirect blocks */
1634 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1641 * Estimate space needed for file creations. We assume the largest filename
1642 * which is 2^64 - 1, hence a filename of 20 chars.
1643 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1645 #ifdef __LDISKFS_DIR_REC_LEN
1646 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1648 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1652 * Concurrency: doesn't access mutable data.
1654 static void osd_conf_get(const struct lu_env *env,
1655 const struct dt_device *dev,
1656 struct dt_device_param *param)
1658 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1662 * XXX should be taken from not-yet-existing fs abstraction layer.
1664 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1665 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1666 param->ddp_block_shift = sb->s_blocksize_bits;
1667 param->ddp_mount_type = LDD_MT_LDISKFS;
1668 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1669 param->ddp_maxbytes = sb->s_maxbytes;
1671 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1672 /* Overhead estimate should be fairly accurate, so we really take a tiny
1673 * error margin which also avoids fragmenting the filesystem too much */
1674 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1675 /* inode are statically allocated, so per-inode space consumption
1676 * is the space consumed by the directory entry */
1677 param->ddp_inodespace = PER_OBJ_USAGE;
1678 /* per-fragment overhead to be used by the client code */
1679 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1680 param->ddp_mntopts = 0;
1681 if (test_opt(sb, XATTR_USER))
1682 param->ddp_mntopts |= MNTOPT_USERXATTR;
1683 if (test_opt(sb, POSIX_ACL))
1684 param->ddp_mntopts |= MNTOPT_ACL;
1686 /* LOD might calculate the max stripe count based on max_ea_size,
1687 * so we need take account in the overhead as well,
1688 * xattr_header + magic + xattr_entry_head */
1689 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1690 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1692 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1693 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1694 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1698 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1702 * Concurrency: shouldn't matter.
1704 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1708 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1710 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1712 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1713 LUSTRE_OSD_LDISKFS_NAME, rc);
1719 * Start commit for OSD device.
1721 * An implementation of dt_commit_async method for OSD device.
1722 * Asychronously starts underlayng fs sync and thereby a transaction
1725 * \param env environment
1726 * \param d dt device
1728 * \see dt_device_operations
1730 static int osd_commit_async(const struct lu_env *env,
1731 struct dt_device *d)
1733 struct super_block *s = osd_sb(osd_dt_dev(d));
1736 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1737 RETURN(s->s_op->sync_fs(s, 0));
1741 * Concurrency: shouldn't matter.
1744 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1746 struct super_block *sb = osd_sb(osd_dt_dev(d));
1747 struct block_device *dev = sb->s_bdev;
1748 #ifdef HAVE_DEV_SET_RDONLY
1749 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1752 int rc = -EOPNOTSUPP;
1756 #ifdef HAVE_DEV_SET_RDONLY
1757 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1759 if (jdev && (jdev != dev)) {
1760 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1762 dev_set_rdonly(jdev);
1764 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1765 dev_set_rdonly(dev);
1767 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1768 osd_dt_dev(d)->od_svname, (long)dev, rc);
1774 * Note: we do not count into QUOTA here.
1775 * If we mount with --data_journal we may need more.
1777 const int osd_dto_credits_noquota[DTO_NR] = {
1780 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1781 * SINGLEDATA_TRANS_BLOCKS(8)
1782 * XXX Note: maybe iam need more, since iam have more level than
1785 [DTO_INDEX_INSERT] = 16,
1788 * just modify a single entry, probably merge few within a block
1790 [DTO_INDEX_DELETE] = 1,
1794 [DTO_INDEX_UPDATE] = 16,
1796 * 4(inode, inode bits, groups, GDT)
1797 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1799 [DTO_OBJECT_CREATE] = 4,
1801 * 4(inode, inode bits, groups, GDT)
1802 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1804 [DTO_OBJECT_DELETE] = 4,
1806 * Attr set credits (inode)
1808 [DTO_ATTR_SET_BASE] = 1,
1810 * Xattr set. The same as xattr of EXT3.
1811 * DATA_TRANS_BLOCKS(14)
1812 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1813 * are also counted in. Do not know why?
1815 [DTO_XATTR_SET] = 14,
1817 * credits for inode change during write.
1819 [DTO_WRITE_BASE] = 3,
1821 * credits for single block write.
1823 [DTO_WRITE_BLOCK] = 14,
1825 * Attr set credits for chown.
1826 * This is extra credits for setattr, and it is null without quota
1828 [DTO_ATTR_SET_CHOWN] = 0
1831 static const struct dt_device_operations osd_dt_ops = {
1832 .dt_root_get = osd_root_get,
1833 .dt_statfs = osd_statfs,
1834 .dt_trans_create = osd_trans_create,
1835 .dt_trans_start = osd_trans_start,
1836 .dt_trans_stop = osd_trans_stop,
1837 .dt_trans_cb_add = osd_trans_cb_add,
1838 .dt_conf_get = osd_conf_get,
1839 .dt_sync = osd_sync,
1841 .dt_commit_async = osd_commit_async,
1844 static void osd_object_read_lock(const struct lu_env *env,
1845 struct dt_object *dt, unsigned role)
1847 struct osd_object *obj = osd_dt_obj(dt);
1848 struct osd_thread_info *oti = osd_oti_get(env);
1850 LINVRNT(osd_invariant(obj));
1852 LASSERT(obj->oo_owner != env);
1853 down_read_nested(&obj->oo_sem, role);
1855 LASSERT(obj->oo_owner == NULL);
1859 static void osd_object_write_lock(const struct lu_env *env,
1860 struct dt_object *dt, unsigned role)
1862 struct osd_object *obj = osd_dt_obj(dt);
1863 struct osd_thread_info *oti = osd_oti_get(env);
1865 LINVRNT(osd_invariant(obj));
1867 LASSERT(obj->oo_owner != env);
1868 down_write_nested(&obj->oo_sem, role);
1870 LASSERT(obj->oo_owner == NULL);
1871 obj->oo_owner = env;
1875 static void osd_object_read_unlock(const struct lu_env *env,
1876 struct dt_object *dt)
1878 struct osd_object *obj = osd_dt_obj(dt);
1879 struct osd_thread_info *oti = osd_oti_get(env);
1881 LINVRNT(osd_invariant(obj));
1883 LASSERT(oti->oti_r_locks > 0);
1885 up_read(&obj->oo_sem);
1888 static void osd_object_write_unlock(const struct lu_env *env,
1889 struct dt_object *dt)
1891 struct osd_object *obj = osd_dt_obj(dt);
1892 struct osd_thread_info *oti = osd_oti_get(env);
1894 LINVRNT(osd_invariant(obj));
1896 LASSERT(obj->oo_owner == env);
1897 LASSERT(oti->oti_w_locks > 0);
1899 obj->oo_owner = NULL;
1900 up_write(&obj->oo_sem);
1903 static int osd_object_write_locked(const struct lu_env *env,
1904 struct dt_object *dt)
1906 struct osd_object *obj = osd_dt_obj(dt);
1908 LINVRNT(osd_invariant(obj));
1910 return obj->oo_owner == env;
1913 static struct timespec *osd_inode_time(const struct lu_env *env,
1914 struct inode *inode, __u64 seconds)
1916 struct osd_thread_info *oti = osd_oti_get(env);
1917 struct timespec *t = &oti->oti_time;
1919 t->tv_sec = seconds;
1921 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1925 static void osd_inode_getattr(const struct lu_env *env,
1926 struct inode *inode, struct lu_attr *attr)
1928 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1929 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1930 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1933 attr->la_atime = LTIME_S(inode->i_atime);
1934 attr->la_mtime = LTIME_S(inode->i_mtime);
1935 attr->la_ctime = LTIME_S(inode->i_ctime);
1936 attr->la_mode = inode->i_mode;
1937 attr->la_size = i_size_read(inode);
1938 attr->la_blocks = inode->i_blocks;
1939 attr->la_uid = i_uid_read(inode);
1940 attr->la_gid = i_gid_read(inode);
1941 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
1942 attr->la_nlink = inode->i_nlink;
1943 attr->la_rdev = inode->i_rdev;
1944 attr->la_blksize = 1 << inode->i_blkbits;
1945 attr->la_blkbits = inode->i_blkbits;
1948 static int osd_attr_get(const struct lu_env *env,
1949 struct dt_object *dt,
1950 struct lu_attr *attr)
1952 struct osd_object *obj = osd_dt_obj(dt);
1954 if (unlikely(!dt_object_exists(dt)))
1956 if (unlikely(obj->oo_destroyed))
1959 LASSERT(!dt_object_remote(dt));
1960 LINVRNT(osd_invariant(obj));
1962 spin_lock(&obj->oo_guard);
1963 osd_inode_getattr(env, obj->oo_inode, attr);
1964 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
1965 attr->la_flags |= LUSTRE_ORPHAN_FL;
1966 spin_unlock(&obj->oo_guard);
1971 static int osd_declare_attr_set(const struct lu_env *env,
1972 struct dt_object *dt,
1973 const struct lu_attr *attr,
1974 struct thandle *handle)
1976 struct osd_thandle *oh;
1977 struct osd_object *obj;
1978 struct osd_thread_info *info = osd_oti_get(env);
1979 struct lquota_id_info *qi = &info->oti_qi;
1987 LASSERT(dt != NULL);
1988 LASSERT(handle != NULL);
1990 obj = osd_dt_obj(dt);
1991 LASSERT(osd_invariant(obj));
1993 oh = container_of0(handle, struct osd_thandle, ot_super);
1994 LASSERT(oh->ot_handle == NULL);
1996 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1997 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1999 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2000 osd_dto_credits_noquota[DTO_XATTR_SET]);
2002 if (attr == NULL || obj->oo_inode == NULL)
2005 bspace = obj->oo_inode->i_blocks;
2006 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2007 bspace = toqb(bspace);
2009 /* Changing ownership is always preformed by super user, it should not
2012 * We still need to call the osd_declare_qid() to calculate the journal
2013 * credits for updating quota accounting files and to trigger quota
2014 * space adjustment once the operation is completed.*/
2015 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2017 uid = i_uid_read(obj->oo_inode);
2018 qi->lqi_type = USRQUOTA;
2019 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2020 /* inode accounting */
2021 qi->lqi_is_blk = false;
2023 /* one more inode for the new uid ... */
2024 qi->lqi_id.qid_uid = attr->la_uid;
2026 /* Reserve credits for the new uid */
2027 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2028 if (rc == -EDQUOT || rc == -EINPROGRESS)
2033 /* and one less inode for the current uid */
2034 qi->lqi_id.qid_uid = uid;
2036 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2037 if (rc == -EDQUOT || rc == -EINPROGRESS)
2042 /* block accounting */
2043 qi->lqi_is_blk = true;
2045 /* more blocks for the new uid ... */
2046 qi->lqi_id.qid_uid = attr->la_uid;
2047 qi->lqi_space = bspace;
2049 * Credits for the new uid has been reserved, re-use "obj"
2050 * to save credit reservation.
2052 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2053 if (rc == -EDQUOT || rc == -EINPROGRESS)
2058 /* and finally less blocks for the current uid */
2059 qi->lqi_id.qid_uid = uid;
2060 qi->lqi_space = -bspace;
2061 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2062 if (rc == -EDQUOT || rc == -EINPROGRESS)
2068 gid = i_gid_read(obj->oo_inode);
2069 qi->lqi_type = GRPQUOTA;
2070 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2072 /* inode accounting */
2073 qi->lqi_is_blk = false;
2075 /* one more inode for the new gid ... */
2076 qi->lqi_id.qid_gid = attr->la_gid;
2078 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2079 if (rc == -EDQUOT || rc == -EINPROGRESS)
2084 /* and one less inode for the current gid */
2085 qi->lqi_id.qid_gid = gid;
2087 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2088 if (rc == -EDQUOT || rc == -EINPROGRESS)
2093 /* block accounting */
2094 qi->lqi_is_blk = true;
2096 /* more blocks for the new gid ... */
2097 qi->lqi_id.qid_gid = attr->la_gid;
2098 qi->lqi_space = bspace;
2099 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2100 if (rc == -EDQUOT || rc == -EINPROGRESS)
2105 /* and finally less blocks for the current gid */
2106 qi->lqi_id.qid_gid = gid;
2107 qi->lqi_space = -bspace;
2108 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2109 if (rc == -EDQUOT || rc == -EINPROGRESS)
2118 static int osd_inode_setattr(const struct lu_env *env,
2119 struct inode *inode, const struct lu_attr *attr)
2121 __u64 bits = attr->la_valid;
2123 /* Only allow set size for regular file */
2124 if (!S_ISREG(inode->i_mode))
2125 bits &= ~(LA_SIZE | LA_BLOCKS);
2130 if (bits & LA_ATIME)
2131 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2132 if (bits & LA_CTIME)
2133 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2134 if (bits & LA_MTIME)
2135 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2136 if (bits & LA_SIZE) {
2137 LDISKFS_I(inode)->i_disksize = attr->la_size;
2138 i_size_write(inode, attr->la_size);
2142 /* OSD should not change "i_blocks" which is used by quota.
2143 * "i_blocks" should be changed by ldiskfs only. */
2144 if (bits & LA_BLOCKS)
2145 inode->i_blocks = attr->la_blocks;
2148 inode->i_mode = (inode->i_mode & S_IFMT) |
2149 (attr->la_mode & ~S_IFMT);
2151 i_uid_write(inode, attr->la_uid);
2153 i_gid_write(inode, attr->la_gid);
2154 if (bits & LA_NLINK)
2155 set_nlink(inode, attr->la_nlink);
2157 inode->i_rdev = attr->la_rdev;
2159 if (bits & LA_FLAGS) {
2160 /* always keep S_NOCMTIME */
2161 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2167 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2169 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2170 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2174 ll_vfs_dq_init(inode);
2176 if (attr->la_valid & LA_UID)
2177 iattr.ia_valid |= ATTR_UID;
2178 if (attr->la_valid & LA_GID)
2179 iattr.ia_valid |= ATTR_GID;
2180 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2181 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2183 rc = ll_vfs_dq_transfer(inode, &iattr);
2185 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2186 "enforcement enabled on the ldiskfs "
2187 "filesystem?\n", inode->i_sb->s_id, rc);
2194 static int osd_attr_set(const struct lu_env *env,
2195 struct dt_object *dt,
2196 const struct lu_attr *attr,
2197 struct thandle *handle)
2199 struct osd_object *obj = osd_dt_obj(dt);
2200 struct inode *inode;
2203 if (!dt_object_exists(dt))
2206 LASSERT(handle != NULL);
2207 LASSERT(!dt_object_remote(dt));
2208 LASSERT(osd_invariant(obj));
2210 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2212 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2213 struct osd_thread_info *oti = osd_oti_get(env);
2214 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2215 struct lu_fid *fid1 = &oti->oti_fid;
2216 struct osd_inode_id *id = &oti->oti_id;
2217 struct iam_path_descr *ipd;
2218 struct iam_container *bag;
2219 struct osd_thandle *oh;
2222 fid_cpu_to_be(fid1, fid0);
2223 memset(id, 1, sizeof(*id));
2224 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2225 fid0)->oi_dir.od_container;
2226 ipd = osd_idx_ipd_get(env, bag);
2227 if (unlikely(ipd == NULL))
2230 oh = container_of0(handle, struct osd_thandle, ot_super);
2231 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2232 (const struct iam_rec *)id, ipd);
2233 osd_ipd_put(env, bag, ipd);
2234 return(rc > 0 ? 0 : rc);
2237 inode = obj->oo_inode;
2239 rc = osd_quota_transfer(inode, attr);
2243 spin_lock(&obj->oo_guard);
2244 rc = osd_inode_setattr(env, inode, attr);
2245 spin_unlock(&obj->oo_guard);
2249 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2251 if (!(attr->la_valid & LA_FLAGS))
2254 /* Let's check if there are extra flags need to be set into LMA */
2255 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2256 struct osd_thread_info *info = osd_oti_get(env);
2257 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2259 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2263 lma->lma_incompat |=
2264 lustre_to_lma_flags(attr->la_flags);
2265 lustre_lma_swab(lma);
2266 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2267 lma, sizeof(*lma), XATTR_REPLACE);
2269 struct osd_device *osd = osd_obj2dev(obj);
2271 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2272 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2273 lma->lma_incompat, rc);
2276 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2278 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2281 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2286 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2287 struct osd_object *obj,
2288 const char *name, const int namelen)
2290 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2293 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2294 umode_t mode, struct dt_allocation_hint *hint,
2298 struct osd_device *osd = osd_obj2dev(obj);
2299 struct osd_thandle *oth;
2300 struct dt_object *parent = NULL;
2301 struct inode *inode;
2303 LINVRNT(osd_invariant(obj));
2304 LASSERT(obj->oo_inode == NULL);
2305 LASSERT(obj->oo_hl_head == NULL);
2307 if (S_ISDIR(mode) && ldiskfs_pdo) {
2308 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2309 if (obj->oo_hl_head == NULL)
2313 oth = container_of(th, struct osd_thandle, ot_super);
2314 LASSERT(oth->ot_handle->h_transaction != NULL);
2316 if (hint != NULL && hint->dah_parent != NULL &&
2317 !dt_object_remote(hint->dah_parent))
2318 parent = hint->dah_parent;
2320 inode = ldiskfs_create_inode(oth->ot_handle,
2321 parent ? osd_dt_obj(parent)->oo_inode :
2322 osd_sb(osd)->s_root->d_inode,
2324 if (!IS_ERR(inode)) {
2325 /* Do not update file c/mtime in ldiskfs. */
2326 inode->i_flags |= S_NOCMTIME;
2328 /* For new created object, it must be consistent,
2329 * and it is unnecessary to scrub against it. */
2330 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2332 obj->oo_inode = inode;
2335 if (obj->oo_hl_head != NULL) {
2336 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2337 obj->oo_hl_head = NULL;
2339 result = PTR_ERR(inode);
2341 LINVRNT(osd_invariant(obj));
2349 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2350 struct lu_attr *attr,
2351 struct dt_allocation_hint *hint,
2352 struct dt_object_format *dof,
2356 struct osd_thandle *oth;
2357 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2359 LASSERT(S_ISDIR(attr->la_mode));
2361 oth = container_of(th, struct osd_thandle, ot_super);
2362 LASSERT(oth->ot_handle->h_transaction != NULL);
2363 result = osd_mkfile(info, obj, mode, hint, th);
2368 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2369 struct lu_attr *attr,
2370 struct dt_allocation_hint *hint,
2371 struct dt_object_format *dof,
2375 struct osd_thandle *oth;
2376 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2378 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2380 LASSERT(S_ISREG(attr->la_mode));
2382 oth = container_of(th, struct osd_thandle, ot_super);
2383 LASSERT(oth->ot_handle->h_transaction != NULL);
2385 result = osd_mkfile(info, obj, mode, hint, th);
2387 LASSERT(obj->oo_inode != NULL);
2388 if (feat->dif_flags & DT_IND_VARKEY)
2389 result = iam_lvar_create(obj->oo_inode,
2390 feat->dif_keysize_max,
2392 feat->dif_recsize_max,
2395 result = iam_lfix_create(obj->oo_inode,
2396 feat->dif_keysize_max,
2398 feat->dif_recsize_max,
2405 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2406 struct lu_attr *attr,
2407 struct dt_allocation_hint *hint,
2408 struct dt_object_format *dof,
2411 LASSERT(S_ISREG(attr->la_mode));
2412 return osd_mkfile(info, obj, (attr->la_mode &
2413 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2416 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2417 struct lu_attr *attr,
2418 struct dt_allocation_hint *hint,
2419 struct dt_object_format *dof,
2422 LASSERT(S_ISLNK(attr->la_mode));
2423 return osd_mkfile(info, obj, (attr->la_mode &
2424 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2427 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2428 struct lu_attr *attr,
2429 struct dt_allocation_hint *hint,
2430 struct dt_object_format *dof,
2433 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2436 LINVRNT(osd_invariant(obj));
2437 LASSERT(obj->oo_inode == NULL);
2438 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2439 S_ISFIFO(mode) || S_ISSOCK(mode));
2441 result = osd_mkfile(info, obj, mode, hint, th);
2443 LASSERT(obj->oo_inode != NULL);
2445 * This inode should be marked dirty for i_rdev. Currently
2446 * that is done in the osd_attr_init().
2448 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2451 LINVRNT(osd_invariant(obj));
2455 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2457 struct dt_allocation_hint *hint,
2458 struct dt_object_format *dof,
2461 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2463 osd_obj_type_f result;
2479 result = osd_mk_index;
2490 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2491 struct dt_object *parent, struct dt_object *child,
2496 ah->dah_parent = parent;
2497 ah->dah_mode = child_mode;
2499 if (parent != NULL && !dt_object_remote(parent)) {
2500 /* will help to find FID->ino at dt_insert("..") */
2501 struct osd_object *pobj = osd_dt_obj(parent);
2502 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2506 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2507 struct lu_attr *attr, struct dt_object_format *dof)
2509 struct inode *inode = obj->oo_inode;
2510 __u64 valid = attr->la_valid;
2513 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2515 if (dof->dof_type != DFT_NODE)
2516 attr->la_valid &= ~LA_RDEV;
2517 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2518 attr->la_valid &= ~LA_ATIME;
2519 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2520 attr->la_valid &= ~LA_CTIME;
2521 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2522 attr->la_valid &= ~LA_MTIME;
2524 result = osd_quota_transfer(inode, attr);
2528 if (attr->la_valid != 0) {
2529 result = osd_inode_setattr(info->oti_env, inode, attr);
2531 * The osd_inode_setattr() should always succeed here. The
2532 * only error that could be returned is EDQUOT when we are
2533 * trying to change the UID or GID of the inode. However, this
2534 * should not happen since quota enforcement is no longer
2535 * enabled on ldiskfs (lquota takes care of it).
2537 LASSERTF(result == 0, "%d\n", result);
2538 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2541 attr->la_valid = valid;
2545 * Helper function for osd_object_create()
2547 * \retval 0, on success
2549 static int __osd_object_create(struct osd_thread_info *info,
2550 struct osd_object *obj, struct lu_attr *attr,
2551 struct dt_allocation_hint *hint,
2552 struct dt_object_format *dof,
2558 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2560 /* we drop umask so that permissions we pass are not affected */
2561 umask = current->fs->umask;
2562 current->fs->umask = 0;
2564 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2566 if (likely(obj->oo_inode != NULL)) {
2567 LASSERT(obj->oo_inode->i_state & I_NEW);
2569 /* Unlock the inode before attr initialization to avoid
2570 * unnecessary dqget operations. LU-6378 */
2571 unlock_new_inode(obj->oo_inode);
2574 if (likely(result == 0)) {
2575 osd_attr_init(info, obj, attr, dof);
2576 osd_object_init0(obj);
2579 /* restore previous umask value */
2580 current->fs->umask = umask;
2582 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2588 * Helper function for osd_object_create()
2590 * \retval 0, on success
2592 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2593 const struct lu_fid *fid, struct thandle *th)
2595 struct osd_thread_info *info = osd_oti_get(env);
2596 struct osd_inode_id *id = &info->oti_id;
2597 struct osd_device *osd = osd_obj2dev(obj);
2598 struct osd_thandle *oh;
2601 LASSERT(obj->oo_inode != NULL);
2603 oh = container_of0(th, struct osd_thandle, ot_super);
2604 LASSERT(oh->ot_handle);
2605 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2607 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2608 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2609 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2614 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2615 u64 seq, struct lu_seq_range *range)
2617 struct seq_server_site *ss = osd_seq_site(osd);
2619 if (fid_seq_is_idif(seq)) {
2620 fld_range_set_ost(range);
2621 range->lsr_index = idif_ost_idx(seq);
2625 if (!fid_seq_in_fldb(seq)) {
2626 fld_range_set_mdt(range);
2628 /* FIXME: If ss is NULL, it suppose not get lsr_index
2630 range->lsr_index = ss->ss_node_id;
2634 LASSERT(ss != NULL);
2635 fld_range_set_any(range);
2636 /* OSD will only do local fld lookup */
2637 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2641 * Concurrency: no external locking is necessary.
2643 static int osd_declare_object_create(const struct lu_env *env,
2644 struct dt_object *dt,
2645 struct lu_attr *attr,
2646 struct dt_allocation_hint *hint,
2647 struct dt_object_format *dof,
2648 struct thandle *handle)
2650 struct osd_thandle *oh;
2654 LASSERT(handle != NULL);
2656 oh = container_of0(handle, struct osd_thandle, ot_super);
2657 LASSERT(oh->ot_handle == NULL);
2659 /* EA object consumes more credits than regular object: osd_mk_index
2660 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2661 * leaf_node, could involves the block, block bitmap, groups, GDT
2662 * change for each block, so add 4 * 2 credits in that case. */
2663 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2664 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2665 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2666 /* Reuse idle OI block may cause additional one OI block
2668 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2669 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2674 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2675 osd_dt_obj(dt), false, NULL, false);
2679 /* will help to find FID->ino mapping at dt_insert() */
2680 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
2686 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2687 struct lu_attr *attr,
2688 struct dt_allocation_hint *hint,
2689 struct dt_object_format *dof, struct thandle *th)
2691 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2692 struct osd_object *obj = osd_dt_obj(dt);
2693 struct osd_thread_info *info = osd_oti_get(env);
2697 if (dt_object_exists(dt))
2700 LINVRNT(osd_invariant(obj));
2701 LASSERT(!dt_object_remote(dt));
2702 LASSERT(osd_write_locked(env, obj));
2703 LASSERT(th != NULL);
2705 if (unlikely(fid_is_acct(fid)))
2706 /* Quota files can't be created from the kernel any more,
2707 * 'tune2fs -O quota' will take care of creating them */
2710 result = __osd_object_create(info, obj, attr, hint, dof, th);
2712 result = __osd_oi_insert(env, obj, fid, th);
2713 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2714 obj->oo_dt.do_body_ops = &osd_body_ops;
2716 LASSERT(ergo(result == 0,
2717 dt_object_exists(dt) && !dt_object_remote(dt)));
2719 LASSERT(osd_invariant(obj));
2724 * Called to destroy on-disk representation of the object
2726 * Concurrency: must be locked
2728 static int osd_declare_object_destroy(const struct lu_env *env,
2729 struct dt_object *dt,
2732 struct osd_object *obj = osd_dt_obj(dt);
2733 struct inode *inode = obj->oo_inode;
2734 struct osd_thandle *oh;
2741 oh = container_of0(th, struct osd_thandle, ot_super);
2742 LASSERT(oh->ot_handle == NULL);
2744 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2745 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2746 /* Recycle idle OI leaf may cause additional three OI blocks
2748 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2749 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2750 /* one less inode */
2751 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2752 -1, oh, obj, false, NULL, false);
2755 /* data to be truncated */
2756 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2757 0, oh, obj, true, NULL, false);
2761 /* will help to find FID->ino when this object is being
2762 * added to PENDING/ */
2763 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
2768 static int osd_object_destroy(const struct lu_env *env,
2769 struct dt_object *dt,
2772 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2773 struct osd_object *obj = osd_dt_obj(dt);
2774 struct inode *inode = obj->oo_inode;
2775 struct osd_device *osd = osd_obj2dev(obj);
2776 struct osd_thandle *oh;
2780 oh = container_of0(th, struct osd_thandle, ot_super);
2781 LASSERT(oh->ot_handle);
2783 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2785 if (unlikely(fid_is_acct(fid)))
2788 if (S_ISDIR(inode->i_mode)) {
2789 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2790 inode->i_nlink == 2);
2791 /* it will check/delete the inode from remote parent,
2792 * how to optimize it? unlink performance impaction XXX */
2793 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2794 if (result != 0 && result != -ENOENT) {
2795 CERROR("%s: delete inode "DFID": rc = %d\n",
2796 osd_name(osd), PFID(fid), result);
2798 spin_lock(&obj->oo_guard);
2800 spin_unlock(&obj->oo_guard);
2801 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2804 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2806 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2807 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2810 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2811 /* XXX: add to ext3 orphan list */
2812 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2814 /* not needed in the cache anymore */
2815 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2816 obj->oo_destroyed = 1;
2822 * Put the fid into lustre_mdt_attrs, and then place the structure
2823 * inode's ea. This fid should not be altered during the life time
2826 * \retval +ve, on success
2827 * \retval -ve, on error
2829 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2831 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2832 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2834 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2838 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2841 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2844 lustre_lma_init(lma, fid, compat, incompat);
2845 lustre_lma_swab(lma);
2847 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2849 /* LMA may already exist, but we need to check that all the
2850 * desired compat/incompat flags have been added. */
2851 if (unlikely(rc == -EEXIST)) {
2852 if (compat == 0 && incompat == 0)
2855 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2856 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2861 lustre_lma_swab(lma);
2862 if (!(~lma->lma_compat & compat) &&
2863 !(~lma->lma_incompat & incompat))
2866 lma->lma_compat |= compat;
2867 lma->lma_incompat |= incompat;
2868 lustre_lma_swab(lma);
2869 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2870 sizeof(*lma), XATTR_REPLACE);
2877 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2878 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2879 * To have compatilibility with 1.8 ldiskfs driver we need to have
2880 * magic number at start of fid data.
2881 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2884 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2885 const struct lu_fid *fid)
2887 if (!fid_is_namespace_visible(fid) ||
2888 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2889 param->edp_magic = 0;
2893 param->edp_magic = LDISKFS_LUFID_MAGIC;
2894 param->edp_len = sizeof(struct lu_fid) + 1;
2895 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2899 * Try to read the fid from inode ea into dt_rec.
2901 * \param fid object fid.
2903 * \retval 0 on success
2905 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2906 __u32 ino, struct lu_fid *fid,
2907 struct osd_inode_id *id)
2909 struct osd_thread_info *info = osd_oti_get(env);
2910 struct inode *inode;
2913 osd_id_gen(id, ino, OSD_OII_NOGEN);
2914 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2916 RETURN(PTR_ERR(inode));
2922 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2924 struct inode *parent_dir,
2925 const struct lu_fid *dot_fid,
2926 const struct lu_fid *dot_dot_fid,
2927 struct osd_thandle *oth)
2929 struct ldiskfs_dentry_param *dot_ldp;
2930 struct ldiskfs_dentry_param *dot_dot_ldp;
2931 __u32 saved_nlink = dir->i_nlink;
2934 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2935 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2937 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2938 dot_ldp->edp_magic = 0;
2940 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2941 dir, dot_ldp, dot_dot_ldp);
2942 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2943 * the subseqent ref_add() will increase the dir->i_nlink
2944 * as 3. That is incorrect for new created directory.
2946 * It looks like hack, because we want to make the OSD API
2947 * to be order-independent for new created directory object
2948 * between dt_insert(..) and ref_add() operations.
2950 * Here, we only restore the in-RAM dir-inode's nlink attr,
2951 * becuase if the nlink attr is not 2, then there will be
2952 * ref_add() called following the dt_insert(..), such call
2953 * will make both the in-RAM and on-disk dir-inode's nlink
2954 * attr to be set as 2. LU-7447 */
2955 set_nlink(dir, saved_nlink);
2960 * Create an local agent inode for remote entry
2962 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2963 struct osd_device *osd,
2964 struct osd_object *pobj,
2965 const struct lu_fid *fid,
2969 struct osd_thread_info *info = osd_oti_get(env);
2970 struct inode *local;
2971 struct osd_thandle *oh;
2976 oh = container_of(th, struct osd_thandle, ot_super);
2977 LASSERT(oh->ot_handle->h_transaction != NULL);
2979 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2980 if (IS_ERR(local)) {
2981 CERROR("%s: create local error %d\n", osd_name(osd),
2982 (int)PTR_ERR(local));
2986 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2987 unlock_new_inode(local);
2989 /* Set special LMA flag for local agent inode */
2990 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2992 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2993 osd_name(osd), PFID(fid), rc);
2994 RETURN(ERR_PTR(rc));
3000 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3001 lu_object_fid(&pobj->oo_dt.do_lu),
3004 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3005 osd_name(osd), PFID(fid), rc);
3006 RETURN(ERR_PTR(rc));
3013 * when direntry is deleted, we have to take care of possible agent inode
3014 * referenced by that. unfortunately we can't do this at that point:
3015 * iget() within a running transaction leads to deadlock and we better do
3016 * not call that every delete declaration to save performance. so we put
3017 * a potention agent inode on a list and process that once the transaction
3018 * is over. Notice it's not any worse in terms of real orphans as regular
3019 * object destroy doesn't put inodes on the on-disk orphan list. this should
3020 * be addressed separately
3022 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3023 struct osd_thandle *oh,
3026 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3027 struct osd_obj_orphan *oor;
3034 oor->oor_env = (struct lu_env *)env;
3035 spin_lock(&osd->od_osfs_lock);
3036 list_add(&oor->oor_list, &osd->od_orphan_list);
3037 spin_unlock(&osd->od_osfs_lock);
3039 oh->ot_remove_agents = 1;
3045 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3046 struct osd_device *osd)
3048 struct osd_thread_info *info = osd_oti_get(env);
3049 struct osd_obj_orphan *oor, *tmp;
3050 struct osd_inode_id id;
3051 struct list_head list;
3052 struct inode *inode;
3057 INIT_LIST_HEAD(&list);
3059 spin_lock(&osd->od_osfs_lock);
3060 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3061 if (oor->oor_env == env) {
3062 list_del(&oor->oor_list);
3063 list_add(&oor->oor_list, &list);
3066 spin_unlock(&osd->od_osfs_lock);
3068 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3072 list_del(&oor->oor_list);
3075 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3076 inode = osd_iget_fid(info, osd, &id, &fid);
3080 if (!osd_remote_fid(env, osd, &fid)) {
3085 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3087 mark_inode_dirty(inode);
3088 ldiskfs_journal_stop(jh);
3096 * OSD layer object create function for interoperability mode (b11826).
3097 * This is mostly similar to osd_object_create(). Only difference being, fid is
3098 * inserted into inode ea here.
3100 * \retval 0, on success
3101 * \retval -ve, on error
3103 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3104 struct lu_attr *attr,
3105 struct dt_allocation_hint *hint,
3106 struct dt_object_format *dof,
3109 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3110 struct osd_object *obj = osd_dt_obj(dt);
3111 struct osd_thread_info *info = osd_oti_get(env);
3112 int result, on_ost = 0;
3116 if (dt_object_exists(dt))
3119 LASSERT(osd_invariant(obj));
3120 LASSERT(!dt_object_remote(dt));
3121 LASSERT(osd_write_locked(env, obj));
3122 LASSERT(th != NULL);
3124 if (unlikely(fid_is_acct(fid)))
3125 /* Quota files can't be created from the kernel any more,
3126 * 'tune2fs -O quota' will take care of creating them */
3129 result = __osd_object_create(info, obj, attr, hint, dof, th);
3131 if (fid_is_idif(fid) &&
3132 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3133 struct lu_fid *tfid = &info->oti_fid;
3134 struct ost_id *oi = &info->oti_ostid;
3136 fid_to_ostid(fid, oi);
3137 ostid_to_fid(tfid, oi, 0);
3139 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3140 LMAC_FID_ON_OST, 0);
3142 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3144 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3145 on_ost ? LMAC_FID_ON_OST : 0,
3148 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3149 obj->oo_dt.do_body_ops = &osd_body_ops;
3153 result = __osd_oi_insert(env, obj, fid, th);
3155 /* a small optimization - dt_insert() isn't usually applied
3156 * to OST objects, so we don't need to cache OI mapping for
3158 if (result == 0 && on_ost == 0) {
3159 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3160 result = osd_idc_find_and_init(env, osd, obj);
3161 LASSERT(result == 0);
3164 LASSERT(ergo(result == 0,
3165 dt_object_exists(dt) && !dt_object_remote(dt)));
3166 LINVRNT(osd_invariant(obj));
3170 static int osd_declare_object_ref_add(const struct lu_env *env,
3171 struct dt_object *dt,
3172 struct thandle *handle)
3174 struct osd_thandle *oh;
3176 /* it's possible that object doesn't exist yet */
3177 LASSERT(handle != NULL);
3179 oh = container_of0(handle, struct osd_thandle, ot_super);
3180 LASSERT(oh->ot_handle == NULL);
3182 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3183 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3189 * Concurrency: @dt is write locked.
3191 static int osd_object_ref_add(const struct lu_env *env,
3192 struct dt_object *dt, struct thandle *th)
3194 struct osd_object *obj = osd_dt_obj(dt);
3195 struct inode *inode = obj->oo_inode;
3196 struct osd_thandle *oh;
3199 if (!dt_object_exists(dt) || obj->oo_destroyed)
3202 LINVRNT(osd_invariant(obj));
3203 LASSERT(!dt_object_remote(dt));
3204 LASSERT(osd_write_locked(env, obj));
3205 LASSERT(th != NULL);
3207 oh = container_of0(th, struct osd_thandle, ot_super);
3208 LASSERT(oh->ot_handle != NULL);
3210 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3212 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3213 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3215 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3216 * (65000) subdirectories by storing "1" in i_nlink if the link count
3217 * would otherwise overflow. Directory tranversal tools understand
3218 * that (st_nlink == 1) indicates that the filesystem dose not track
3219 * hard links count on the directory, and will not abort subdirectory
3220 * scanning early once (st_nlink - 2) subdirs have been found.
3222 * This also has to properly handle the case of inodes with nlink == 0
3223 * in case they are being linked into the PENDING directory
3225 spin_lock(&obj->oo_guard);
3226 if (unlikely(inode->i_nlink == 0))
3227 /* inc_nlink from 0 may cause WARN_ON */
3228 set_nlink(inode, 1);
3230 ldiskfs_inc_count(oh->ot_handle, inode);
3231 if (!S_ISDIR(inode->i_mode))
3232 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3234 spin_unlock(&obj->oo_guard);
3236 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3237 LINVRNT(osd_invariant(obj));
3239 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3244 static int osd_declare_object_ref_del(const struct lu_env *env,
3245 struct dt_object *dt,
3246 struct thandle *handle)
3248 struct osd_thandle *oh;
3250 if (!dt_object_exists(dt))
3253 LASSERT(!dt_object_remote(dt));
3254 LASSERT(handle != NULL);
3256 oh = container_of0(handle, struct osd_thandle, ot_super);
3257 LASSERT(oh->ot_handle == NULL);
3259 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3260 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3266 * Concurrency: @dt is write locked.
3268 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3271 struct osd_object *obj = osd_dt_obj(dt);
3272 struct inode *inode = obj->oo_inode;
3273 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3274 struct osd_thandle *oh;
3276 if (!dt_object_exists(dt))
3279 LINVRNT(osd_invariant(obj));
3280 LASSERT(!dt_object_remote(dt));
3281 LASSERT(osd_write_locked(env, obj));
3282 LASSERT(th != NULL);
3284 oh = container_of0(th, struct osd_thandle, ot_super);
3285 LASSERT(oh->ot_handle != NULL);
3287 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3289 spin_lock(&obj->oo_guard);
3290 /* That can be result of upgrade from old Lustre version and
3291 * applied only to local files. Just skip this ref_del call.
3292 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3293 if (inode->i_nlink == 0) {
3294 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3295 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3296 ", maybe an upgraded file? (LU-3915)\n",
3297 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3298 spin_unlock(&obj->oo_guard);
3302 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3303 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3305 ldiskfs_dec_count(oh->ot_handle, inode);
3306 spin_unlock(&obj->oo_guard);
3308 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3309 LINVRNT(osd_invariant(obj));
3311 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3317 * Get the 64-bit version for an inode.
3319 static int osd_object_version_get(const struct lu_env *env,
3320 struct dt_object *dt, dt_obj_version_t *ver)
3322 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3324 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3325 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3326 *ver = LDISKFS_I(inode)->i_fs_version;
3331 * Concurrency: @dt is read locked.
3333 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3334 struct lu_buf *buf, const char *name)
3336 struct osd_object *obj = osd_dt_obj(dt);
3337 struct inode *inode = obj->oo_inode;
3338 struct osd_thread_info *info = osd_oti_get(env);
3339 struct dentry *dentry = &info->oti_obj_dentry;
3341 /* version get is not real XATTR but uses xattr API */
3342 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3343 /* for version we are just using xattr API but change inode
3345 if (buf->lb_len == 0)
3346 return sizeof(dt_obj_version_t);
3348 if (buf->lb_len < sizeof(dt_obj_version_t))
3351 osd_object_version_get(env, dt, buf->lb_buf);
3353 return sizeof(dt_obj_version_t);
3356 if (!dt_object_exists(dt))
3359 LASSERT(!dt_object_remote(dt));
3360 LASSERT(inode->i_op != NULL);
3361 LASSERT(inode->i_op->getxattr != NULL);
3363 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3367 static int osd_declare_xattr_set(const struct lu_env *env,
3368 struct dt_object *dt,
3369 const struct lu_buf *buf, const char *name,
3370 int fl, struct thandle *handle)
3372 struct osd_thandle *oh;
3374 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3376 LASSERT(handle != NULL);
3378 oh = container_of0(handle, struct osd_thandle, ot_super);
3379 LASSERT(oh->ot_handle == NULL);
3381 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3382 /* For non-upgrading case, the LMA is set first and
3383 * usually fit inode. But for upgrade case, the LMA
3384 * may be in another separated EA block. */
3385 if (!dt_object_exists(dt))
3387 else if (fl == LU_XATTR_REPLACE)
3391 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3395 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3400 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3401 /* learn xattr size from osd_xattr_get if
3402 attribute has not been read yet */
3403 buflen = __osd_xattr_get(
3404 osd_dt_obj(dt)->oo_inode,
3405 &osd_oti_get(env)->oti_obj_dentry,
3410 buflen = buf->lb_len;
3413 if (buflen > sb->s_blocksize) {
3414 credits += osd_calc_bkmap_credits(
3416 (buflen + sb->s_blocksize - 1) >>
3417 sb->s_blocksize_bits);
3421 * xattr set may involve inode quota change, reserve credits for
3422 * dquot_initialize()
3424 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3427 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3433 * Set the 64-bit version for object
3435 static void osd_object_version_set(const struct lu_env *env,
3436 struct dt_object *dt,
3437 dt_obj_version_t *new_version)
3439 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3441 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3442 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3444 LDISKFS_I(inode)->i_fs_version = *new_version;
3445 /** Version is set after all inode operations are finished,
3446 * so we should mark it dirty here */
3447 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3451 * Concurrency: @dt is write locked.
3453 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3454 const struct lu_buf *buf, const char *name, int fl,
3455 struct thandle *handle)
3457 struct osd_object *obj = osd_dt_obj(dt);
3458 struct inode *inode = obj->oo_inode;
3459 struct osd_thread_info *info = osd_oti_get(env);
3464 LASSERT(handle != NULL);
3466 /* version set is not real XATTR */
3467 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3468 /* for version we are just using xattr API but change inode
3470 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3471 osd_object_version_set(env, dt, buf->lb_buf);
3472 return sizeof(dt_obj_version_t);
3475 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3476 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3478 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3479 if (fl & LU_XATTR_REPLACE)
3480 fs_flags |= XATTR_REPLACE;
3482 if (fl & LU_XATTR_CREATE)
3483 fs_flags |= XATTR_CREATE;
3485 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3486 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3489 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3493 lma->lma_incompat |= LMAI_STRIPED;
3494 lustre_lma_swab(lma);
3495 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3496 sizeof(*lma), XATTR_REPLACE);
3501 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3502 strcmp(name, XATTR_NAME_LINK) == 0)
3505 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3507 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3513 * Concurrency: @dt is read locked.
3515 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3516 const struct lu_buf *buf)
3518 struct osd_object *obj = osd_dt_obj(dt);
3519 struct inode *inode = obj->oo_inode;
3520 struct osd_thread_info *info = osd_oti_get(env);
3521 struct dentry *dentry = &info->oti_obj_dentry;
3523 if (!dt_object_exists(dt))
3526 LASSERT(!dt_object_remote(dt));
3527 LASSERT(inode->i_op != NULL);
3528 LASSERT(inode->i_op->listxattr != NULL);
3530 dentry->d_inode = inode;
3531 dentry->d_sb = inode->i_sb;
3532 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3535 static int osd_declare_xattr_del(const struct lu_env *env,
3536 struct dt_object *dt, const char *name,
3537 struct thandle *handle)
3539 struct osd_thandle *oh;
3540 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3542 LASSERT(!dt_object_remote(dt));
3543 LASSERT(handle != NULL);
3545 oh = container_of0(handle, struct osd_thandle, ot_super);
3546 LASSERT(oh->ot_handle == NULL);
3548 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3549 osd_dto_credits_noquota[DTO_XATTR_SET]);
3551 * xattr del may involve inode quota change, reserve credits for
3552 * dquot_initialize()
3554 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3560 * Concurrency: @dt is write locked.
3562 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3563 const char *name, struct thandle *handle)
3565 struct osd_object *obj = osd_dt_obj(dt);
3566 struct inode *inode = obj->oo_inode;
3567 struct osd_thread_info *info = osd_oti_get(env);
3568 struct dentry *dentry = &info->oti_obj_dentry;
3571 if (!dt_object_exists(dt))
3574 LASSERT(!dt_object_remote(dt));
3575 LASSERT(inode->i_op != NULL);
3576 LASSERT(inode->i_op->removexattr != NULL);
3577 LASSERT(handle != NULL);
3579 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3581 ll_vfs_dq_init(inode);
3582 dentry->d_inode = inode;
3583 dentry->d_sb = inode->i_sb;
3584 rc = inode->i_op->removexattr(dentry, name);
3585 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3589 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3590 __u64 start, __u64 end)
3592 struct osd_object *obj = osd_dt_obj(dt);
3593 struct inode *inode = obj->oo_inode;
3594 struct osd_thread_info *info = osd_oti_get(env);
3595 struct dentry *dentry = &info->oti_obj_dentry;
3596 struct file *file = &info->oti_file;
3601 dentry->d_inode = inode;
3602 dentry->d_sb = inode->i_sb;
3603 file->f_path.dentry = dentry;
3604 file->f_mapping = inode->i_mapping;
3605 file->f_op = inode->i_fop;
3606 set_file_inode(file, inode);
3608 rc = ll_vfs_fsync_range(file, start, end, 0);
3617 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3618 const struct dt_index_features *feat)
3620 struct iam_descr *descr;
3622 if (osd_object_is_root(o))
3623 return feat == &dt_directory_features;
3625 LASSERT(o->oo_dir != NULL);
3627 descr = o->oo_dir->od_container.ic_descr;
3628 if (feat == &dt_directory_features) {
3629 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3635 feat->dif_keysize_min <= descr->id_key_size &&
3636 descr->id_key_size <= feat->dif_keysize_max &&
3637 feat->dif_recsize_min <= descr->id_rec_size &&
3638 descr->id_rec_size <= feat->dif_recsize_max &&
3639 !(feat->dif_flags & (DT_IND_VARKEY |
3640 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3641 ergo(feat->dif_flags & DT_IND_UPDATE,
3642 1 /* XXX check that object (and file system) is
3647 static int osd_iam_container_init(const struct lu_env *env,
3648 struct osd_object *obj,
3649 struct osd_directory *dir)
3651 struct iam_container *bag = &dir->od_container;
3654 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3658 result = iam_container_setup(bag);
3660 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3662 iam_container_fini(bag);
3669 * Concurrency: no external locking is necessary.
3671 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3672 const struct dt_index_features *feat)
3676 struct osd_object *obj = osd_dt_obj(dt);
3678 LINVRNT(osd_invariant(obj));
3680 if (osd_object_is_root(obj)) {
3681 dt->do_index_ops = &osd_index_ea_ops;
3683 } else if (feat == &dt_directory_features) {
3684 dt->do_index_ops = &osd_index_ea_ops;
3685 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
3690 } else if (unlikely(feat == &dt_otable_features)) {
3691 dt->do_index_ops = &osd_otable_ops;
3693 } else if (unlikely(feat == &dt_acct_features)) {
3694 dt->do_index_ops = &osd_acct_index_ops;
3697 } else if (!osd_has_index(obj)) {
3698 struct osd_directory *dir;
3703 spin_lock(&obj->oo_guard);
3704 if (obj->oo_dir == NULL)
3708 * Concurrent thread allocated container data.
3711 spin_unlock(&obj->oo_guard);
3713 * Now, that we have container data, serialize its
3716 down_write(&obj->oo_ext_idx_sem);
3718 * recheck under lock.
3720 if (!osd_has_index(obj))
3721 result = osd_iam_container_init(env, obj,
3725 up_write(&obj->oo_ext_idx_sem);
3733 if (result == 0 && skip_iam == 0) {
3734 if (!osd_iam_index_probe(env, obj, feat))
3737 LINVRNT(osd_invariant(obj));
3739 if (result == 0 && feat == &dt_quota_glb_features &&
3740 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3741 result = osd_quota_migration(env, dt);
3746 static int osd_otable_it_attr_get(const struct lu_env *env,
3747 struct dt_object *dt,
3748 struct lu_attr *attr)
3754 static const struct dt_object_operations osd_obj_ops = {
3755 .do_read_lock = osd_object_read_lock,
3756 .do_write_lock = osd_object_write_lock,
3757 .do_read_unlock = osd_object_read_unlock,
3758 .do_write_unlock = osd_object_write_unlock,
3759 .do_write_locked = osd_object_write_locked,
3760 .do_attr_get = osd_attr_get,
3761 .do_declare_attr_set = osd_declare_attr_set,
3762 .do_attr_set = osd_attr_set,
3763 .do_ah_init = osd_ah_init,
3764 .do_declare_create = osd_declare_object_create,
3765 .do_create = osd_object_create,
3766 .do_declare_destroy = osd_declare_object_destroy,
3767 .do_destroy = osd_object_destroy,
3768 .do_index_try = osd_index_try,
3769 .do_declare_ref_add = osd_declare_object_ref_add,
3770 .do_ref_add = osd_object_ref_add,
3771 .do_declare_ref_del = osd_declare_object_ref_del,
3772 .do_ref_del = osd_object_ref_del,
3773 .do_xattr_get = osd_xattr_get,
3774 .do_declare_xattr_set = osd_declare_xattr_set,
3775 .do_xattr_set = osd_xattr_set,
3776 .do_declare_xattr_del = osd_declare_xattr_del,
3777 .do_xattr_del = osd_xattr_del,
3778 .do_xattr_list = osd_xattr_list,
3779 .do_object_sync = osd_object_sync,
3783 * dt_object_operations for interoperability mode
3784 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3786 static const struct dt_object_operations osd_obj_ea_ops = {
3787 .do_read_lock = osd_object_read_lock,
3788 .do_write_lock = osd_object_write_lock,
3789 .do_read_unlock = osd_object_read_unlock,
3790 .do_write_unlock = osd_object_write_unlock,
3791 .do_write_locked = osd_object_write_locked,
3792 .do_attr_get = osd_attr_get,
3793 .do_declare_attr_set = osd_declare_attr_set,
3794 .do_attr_set = osd_attr_set,
3795 .do_ah_init = osd_ah_init,
3796 .do_declare_create = osd_declare_object_create,
3797 .do_create = osd_object_ea_create,
3798 .do_declare_destroy = osd_declare_object_destroy,
3799 .do_destroy = osd_object_destroy,
3800 .do_index_try = osd_index_try,
3801 .do_declare_ref_add = osd_declare_object_ref_add,
3802 .do_ref_add = osd_object_ref_add,
3803 .do_declare_ref_del = osd_declare_object_ref_del,
3804 .do_ref_del = osd_object_ref_del,
3805 .do_xattr_get = osd_xattr_get,
3806 .do_declare_xattr_set = osd_declare_xattr_set,
3807 .do_xattr_set = osd_xattr_set,
3808 .do_declare_xattr_del = osd_declare_xattr_del,
3809 .do_xattr_del = osd_xattr_del,
3810 .do_xattr_list = osd_xattr_list,
3811 .do_object_sync = osd_object_sync,
3814 static const struct dt_object_operations osd_obj_otable_it_ops = {
3815 .do_attr_get = osd_otable_it_attr_get,
3816 .do_index_try = osd_index_try,
3819 static int osd_index_declare_iam_delete(const struct lu_env *env,
3820 struct dt_object *dt,
3821 const struct dt_key *key,
3822 struct thandle *handle)
3824 struct osd_thandle *oh;
3826 oh = container_of0(handle, struct osd_thandle, ot_super);
3827 LASSERT(oh->ot_handle == NULL);
3829 /* Recycle may cause additional three blocks to be changed. */
3830 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3831 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3837 * delete a (key, value) pair from index \a dt specified by \a key
3839 * \param dt osd index object
3840 * \param key key for index
3841 * \param rec record reference
3842 * \param handle transaction handler
3845 * \retval -ve failure
3847 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3848 const struct dt_key *key,
3849 struct thandle *handle)
3851 struct osd_thread_info *oti = osd_oti_get(env);
3852 struct osd_object *obj = osd_dt_obj(dt);
3853 struct osd_thandle *oh;
3854 struct iam_path_descr *ipd;
3855 struct iam_container *bag = &obj->oo_dir->od_container;
3859 if (!dt_object_exists(dt))
3862 LINVRNT(osd_invariant(obj));
3863 LASSERT(!dt_object_remote(dt));
3864 LASSERT(bag->ic_object == obj->oo_inode);
3865 LASSERT(handle != NULL);
3867 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3869 ipd = osd_idx_ipd_get(env, bag);
3870 if (unlikely(ipd == NULL))
3873 oh = container_of0(handle, struct osd_thandle, ot_super);
3874 LASSERT(oh->ot_handle != NULL);
3875 LASSERT(oh->ot_handle->h_transaction != NULL);
3877 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3878 /* swab quota uid/gid provided by caller */
3879 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3880 key = (const struct dt_key *)&oti->oti_quota_id;
3883 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3884 osd_ipd_put(env, bag, ipd);
3885 LINVRNT(osd_invariant(obj));
3886 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3890 static int osd_index_declare_ea_delete(const struct lu_env *env,
3891 struct dt_object *dt,
3892 const struct dt_key *key,
3893 struct thandle *handle)
3895 struct osd_thandle *oh;
3896 struct inode *inode;
3900 LASSERT(!dt_object_remote(dt));
3901 LASSERT(handle != NULL);
3903 oh = container_of0(handle, struct osd_thandle, ot_super);
3904 LASSERT(oh->ot_handle == NULL);
3906 /* due to DNE we may need to remove an agent inode */
3907 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3908 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3909 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3911 inode = osd_dt_obj(dt)->oo_inode;
3915 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3916 0, oh, osd_dt_obj(dt), true, NULL, false);
3920 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3923 struct osd_fid_pack *rec;
3926 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3927 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3928 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3929 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3935 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3936 const struct lu_fid *fid)
3938 struct seq_server_site *ss = osd_seq_site(osd);
3941 /* FID seqs not in FLDB, must be local seq */
3942 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3945 /* If FLD is not being initialized yet, it only happens during the
3946 * initialization, likely during mgs initialization, and we assume
3947 * this is local FID. */
3948 if (ss == NULL || ss->ss_server_fld == NULL)
3951 /* Only check the local FLDB here */
3952 if (osd_seq_exists(env, osd, fid_seq(fid)))
3959 * Index delete function for interoperability mode (b11826).
3960 * It will remove the directory entry added by osd_index_ea_insert().
3961 * This entry is needed to maintain name->fid mapping.
3963 * \param key, key i.e. file entry to be deleted
3965 * \retval 0, on success
3966 * \retval -ve, on error
3968 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3969 const struct dt_key *key, struct thandle *handle)
3971 struct osd_object *obj = osd_dt_obj(dt);
3972 struct inode *dir = obj->oo_inode;
3973 struct dentry *dentry;
3974 struct osd_thandle *oh;
3975 struct ldiskfs_dir_entry_2 *de = NULL;
3976 struct buffer_head *bh;
3977 struct htree_lock *hlock = NULL;
3978 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3979 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3983 if (!dt_object_exists(dt))
3986 LINVRNT(osd_invariant(obj));
3987 LASSERT(!dt_object_remote(dt));
3988 LASSERT(handle != NULL);
3990 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3992 oh = container_of(handle, struct osd_thandle, ot_super);
3993 LASSERT(oh->ot_handle != NULL);
3994 LASSERT(oh->ot_handle->h_transaction != NULL);
3996 ll_vfs_dq_init(dir);
3997 dentry = osd_child_dentry_get(env, obj,
3998 (char *)key, strlen((char *)key));
4000 if (obj->oo_hl_head != NULL) {
4001 hlock = osd_oti_get(env)->oti_hlock;
4002 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4003 dir, LDISKFS_HLOCK_DEL);
4005 down_write(&obj->oo_ext_idx_sem);
4008 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4010 /* If this is not the ".." entry, it might be a remote DNE
4011 * entry and we need to check if the FID is for a remote
4012 * MDT. If the FID is not in the directory entry (e.g.
4013 * upgraded 1.8 filesystem without dirdata enabled) then
4014 * we need to get the FID from the LMA. For a remote directory
4015 * there HAS to be an LMA, it cannot be an IGIF inode in this
4018 * Delete the entry before the agent inode in order to
4019 * simplify error handling. At worst an error after deleting
4020 * the entry first might leak the agent inode afterward. The
4021 * reverse would need filesystem abort in case of error deleting
4022 * the entry after the agent had been removed, or leave a
4023 * dangling entry pointing at a random inode. */
4024 if (strcmp((char *)key, dotdot) != 0) {
4025 LASSERT(de != NULL);
4026 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4027 if (rc == -ENODATA) {
4028 /* can't get FID, postpone to the end of the
4029 * transaction when iget() is safe */
4030 osd_schedule_agent_inode_removal(env, oh,
4031 le32_to_cpu(de->inode));
4032 } else if (rc == 0 &&
4033 unlikely(osd_remote_fid(env, osd, fid))) {
4034 osd_schedule_agent_inode_removal(env, oh,
4035 le32_to_cpu(de->inode));
4038 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4044 ldiskfs_htree_unlock(hlock);
4046 up_write(&obj->oo_ext_idx_sem);
4051 /* For inode on the remote MDT, .. will point to
4052 * /Agent directory, Check whether it needs to delete
4053 * from agent directory */
4054 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4055 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
4057 if (rc != 0 && rc != -ENOENT) {
4058 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4059 osd_name(osd), PFID(fid), rc);
4069 LASSERT(osd_invariant(obj));
4070 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4075 * Lookup index for \a key and copy record to \a rec.
4077 * \param dt osd index object
4078 * \param key key for index
4079 * \param rec record reference
4081 * \retval +ve success : exact mach
4082 * \retval 0 return record with key not greater than \a key
4083 * \retval -ve failure
4085 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4086 struct dt_rec *rec, const struct dt_key *key)
4088 struct osd_object *obj = osd_dt_obj(dt);
4089 struct iam_path_descr *ipd;
4090 struct iam_container *bag = &obj->oo_dir->od_container;
4091 struct osd_thread_info *oti = osd_oti_get(env);
4092 struct iam_iterator *it = &oti->oti_idx_it;
4093 struct iam_rec *iam_rec;
4097 if (!dt_object_exists(dt))
4100 LASSERT(osd_invariant(obj));
4101 LASSERT(!dt_object_remote(dt));
4102 LASSERT(bag->ic_object == obj->oo_inode);
4104 ipd = osd_idx_ipd_get(env, bag);
4108 /* got ipd now we can start iterator. */
4109 iam_it_init(it, bag, 0, ipd);
4111 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4112 /* swab quota uid/gid provided by caller */
4113 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4114 key = (const struct dt_key *)&oti->oti_quota_id;
4117 rc = iam_it_get(it, (struct iam_key *)key);
4119 if (S_ISDIR(obj->oo_inode->i_mode))
4120 iam_rec = (struct iam_rec *)oti->oti_ldp;
4122 iam_rec = (struct iam_rec *) rec;
4124 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4126 if (S_ISDIR(obj->oo_inode->i_mode))
4127 osd_fid_unpack((struct lu_fid *) rec,
4128 (struct osd_fid_pack *)iam_rec);
4129 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4130 osd_quota_unpack(obj, rec);
4135 osd_ipd_put(env, bag, ipd);
4137 LINVRNT(osd_invariant(obj));
4142 static int osd_index_declare_iam_insert(const struct lu_env *env,
4143 struct dt_object *dt,
4144 const struct dt_rec *rec,
4145 const struct dt_key *key,
4146 struct thandle *handle)
4148 struct osd_thandle *oh;
4150 LASSERT(handle != NULL);
4152 oh = container_of0(handle, struct osd_thandle, ot_super);
4153 LASSERT(oh->ot_handle == NULL);
4155 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4156 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4162 * Inserts (key, value) pair in \a dt index object.
4164 * \param dt osd index object
4165 * \param key key for index
4166 * \param rec record reference
4167 * \param th transaction handler
4170 * \retval -ve failure
4172 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4173 const struct dt_rec *rec,
4174 const struct dt_key *key, struct thandle *th,
4177 struct osd_object *obj = osd_dt_obj(dt);
4178 struct iam_path_descr *ipd;
4179 struct osd_thandle *oh;
4180 struct iam_container *bag;
4181 struct osd_thread_info *oti = osd_oti_get(env);
4182 struct iam_rec *iam_rec;
4186 if (!dt_object_exists(dt))
4189 LINVRNT(osd_invariant(obj));
4190 LASSERT(!dt_object_remote(dt));
4192 bag = &obj->oo_dir->od_container;
4193 LASSERT(bag->ic_object == obj->oo_inode);
4194 LASSERT(th != NULL);
4196 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4198 ipd = osd_idx_ipd_get(env, bag);
4199 if (unlikely(ipd == NULL))
4202 oh = container_of0(th, struct osd_thandle, ot_super);
4203 LASSERT(oh->ot_handle != NULL);
4204 LASSERT(oh->ot_handle->h_transaction != NULL);
4205 if (S_ISDIR(obj->oo_inode->i_mode)) {
4206 iam_rec = (struct iam_rec *)oti->oti_ldp;
4207 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4208 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4209 /* pack quota uid/gid */
4210 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4211 key = (const struct dt_key *)&oti->oti_quota_id;
4212 /* pack quota record */
4213 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4214 iam_rec = (struct iam_rec *)rec;
4216 iam_rec = (struct iam_rec *)rec;
4219 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4221 osd_ipd_put(env, bag, ipd);
4222 LINVRNT(osd_invariant(obj));
4223 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4228 * Calls ldiskfs_add_entry() to add directory entry
4229 * into the directory. This is required for
4230 * interoperability mode (b11826)
4232 * \retval 0, on success
4233 * \retval -ve, on error
4235 static int __osd_ea_add_rec(struct osd_thread_info *info,
4236 struct osd_object *pobj, struct inode *cinode,
4237 const char *name, const struct lu_fid *fid,
4238 struct htree_lock *hlock, struct thandle *th)
4240 struct ldiskfs_dentry_param *ldp;
4241 struct dentry *child;
4242 struct osd_thandle *oth;
4245 oth = container_of(th, struct osd_thandle, ot_super);
4246 LASSERT(oth->ot_handle != NULL);
4247 LASSERT(oth->ot_handle->h_transaction != NULL);
4248 LASSERT(pobj->oo_inode);
4250 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4251 if (unlikely(pobj->oo_inode ==
4252 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4255 osd_get_ldiskfs_dirent_param(ldp, fid);
4256 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4257 child->d_fsdata = (void *)ldp;
4258 ll_vfs_dq_init(pobj->oo_inode);
4259 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4261 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4262 struct ldiskfs_dir_entry_2 *de;
4263 struct buffer_head *bh;
4266 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4269 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4272 if (S_ISDIR(cinode->i_mode))
4273 de->file_type = LDISKFS_DIRENT_LUFID |
4274 LDISKFS_FT_REG_FILE;
4276 de->file_type = LDISKFS_DIRENT_LUFID |
4278 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4289 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4290 * into the directory.Also sets flags into osd object to
4291 * indicate dot and dotdot are created. This is required for
4292 * interoperability mode (b11826)
4294 * \param dir directory for dot and dotdot fixup.
4295 * \param obj child object for linking
4297 * \retval 0, on success
4298 * \retval -ve, on error
4300 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4301 struct osd_object *dir,
4302 struct inode *parent_dir, const char *name,
4303 const struct lu_fid *dot_fid,
4304 const struct lu_fid *dot_dot_fid,
4307 struct inode *inode = dir->oo_inode;
4308 struct osd_thandle *oth;
4311 oth = container_of(th, struct osd_thandle, ot_super);
4312 LASSERT(oth->ot_handle->h_transaction != NULL);
4313 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4315 if (strcmp(name, dot) == 0) {
4316 if (dir->oo_compat_dot_created) {
4319 LASSERT(inode->i_ino == parent_dir->i_ino);
4320 dir->oo_compat_dot_created = 1;
4323 } else if (strcmp(name, dotdot) == 0) {
4324 if (!dir->oo_compat_dot_created)
4326 /* in case of rename, dotdot is already created */
4327 if (dir->oo_compat_dotdot_created) {
4328 return __osd_ea_add_rec(info, dir, parent_dir, name,
4329 dot_dot_fid, NULL, th);
4332 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4333 struct lu_fid tfid = *dot_dot_fid;
4336 result = osd_add_dot_dotdot_internal(info,
4337 dir->oo_inode, parent_dir, dot_fid,
4340 result = osd_add_dot_dotdot_internal(info,
4341 dir->oo_inode, parent_dir, dot_fid,
4346 dir->oo_compat_dotdot_created = 1;
4354 * It will call the appropriate osd_add* function and return the
4355 * value, return by respective functions.
4357 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4358 struct inode *cinode, const char *name,
4359 const struct lu_fid *fid, struct thandle *th)
4361 struct osd_thread_info *info = osd_oti_get(env);
4362 struct htree_lock *hlock;
4365 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4367 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4369 if (hlock != NULL) {
4370 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4373 down_write(&pobj->oo_ext_idx_sem);
4376 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4377 lu_object_fid(&pobj->oo_dt.do_lu),
4380 if (hlock != NULL) {
4381 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4382 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4384 down_write(&pobj->oo_ext_idx_sem);
4387 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4388 struct lu_fid *tfid = &info->oti_fid;
4392 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4395 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4400 ldiskfs_htree_unlock(hlock);
4402 up_write(&pobj->oo_ext_idx_sem);
4408 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4409 struct osd_idmap_cache *oic)
4411 struct osd_scrub *scrub = &dev->od_scrub;
4412 struct lu_fid *fid = &oic->oic_fid;
4413 struct osd_inode_id *id = &oti->oti_id;
4418 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4421 if (scrub->os_pos_current > id->oii_ino)
4425 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4426 if (rc == -ENOENT) {
4427 struct inode *inode;
4430 inode = osd_iget(oti, dev, &oic->oic_lid);
4432 /* The inode has been removed (by race maybe). */
4433 if (IS_ERR(inode)) {
4434 rc = PTR_ERR(inode);
4436 RETURN(rc == -ESTALE ? -ENOENT : rc);
4440 /* The OI mapping is lost. */
4441 if (id->oii_gen != OSD_OII_NOGEN)
4444 /* The inode may has been reused by others, we do not know,
4445 * leave it to be handled by subsequent osd_fid_lookup(). */
4447 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4452 if (thread_is_running(&scrub->os_thread)) {
4453 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4454 /* There is race condition between osd_oi_lookup and OI scrub.
4455 * The OI scrub finished just after osd_oi_lookup() failure.
4456 * Under such case, it is unnecessary to trigger OI scrub again,
4457 * but try to call osd_oi_lookup() again. */
4458 if (unlikely(rc == -EAGAIN))
4464 if (!dev->od_noscrub && ++once == 1) {
4465 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4467 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4468 "for "DFID", rc = %d [2]\n",
4469 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4471 if (rc == 0 || rc == -EALREADY)
4478 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4479 struct osd_device *dev,
4480 struct osd_idmap_cache *oic,
4481 struct lu_fid *fid, __u32 ino)
4483 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4484 struct inode *inode;
4487 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4488 inode = osd_iget(oti, dev, &oic->oic_lid);
4489 if (IS_ERR(inode)) {
4490 fid_zero(&oic->oic_fid);
4491 return PTR_ERR(inode);
4494 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4497 fid_zero(&oic->oic_fid);
4499 *fid = oic->oic_fid = lma->lma_self_fid;
4503 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4504 struct osd_inode_id *id, const struct lu_fid *fid)
4506 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4507 id->oii_ino, id->oii_gen, info);
4508 info->oti_cache.oic_lid = *id;
4509 info->oti_cache.oic_fid = *fid;
4510 info->oti_cache.oic_dev = osd;
4514 * Get parent FID from the linkEA.
4516 * For a directory which parent resides on remote MDT, to satisfy the
4517 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4518 * the other hand, to make the lookup(..) on the directory can return
4519 * the real parent FID, we append the real parent FID after its ".."
4520 * name entry in the /REMOTE_PARENT_DIR.
4522 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4523 * backup. So after the restore, we cannot get the right parent FID from
4524 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4525 * we have stored the real parent FID in the directory object's linkEA,
4526 * we can parse the linkEA for the real parent FID.
4528 * \param[in] env pointer to the thread context
4529 * \param[in] obj pointer to the object to be handled
4530 * \param[out]fid pointer to the buffer to hold the parent FID
4532 * \retval 0 for getting the real parent FID successfully
4533 * \retval negative error number on failure
4535 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4536 struct osd_object *obj,
4539 struct osd_thread_info *oti = osd_oti_get(env);
4540 struct lu_buf *buf = &oti->oti_big_buf;
4541 struct dentry *dentry = &oti->oti_obj_dentry;
4542 struct inode *inode = obj->oo_inode;
4543 struct linkea_data ldata = { NULL };
4548 if (!S_ISDIR(inode->i_mode))
4552 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4553 buf->lb_buf, buf->lb_len);
4554 if (rc == -ERANGE) {
4555 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4558 lu_buf_realloc(buf, rc);
4559 if (buf->lb_buf == NULL)
4566 if (unlikely(rc == 0))
4572 if (unlikely(buf->lb_buf == NULL)) {
4573 lu_buf_realloc(buf, rc);
4574 if (buf->lb_buf == NULL)
4581 rc = linkea_init(&ldata);
4583 linkea_first_entry(&ldata);
4584 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4591 * Calls ->lookup() to find dentry. From dentry get inode and
4592 * read inode's ea to get fid. This is required for interoperability
4595 * \retval 0, on success
4596 * \retval -ve, on error
4598 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4599 struct dt_rec *rec, const struct dt_key *key)
4601 struct inode *dir = obj->oo_inode;
4602 struct dentry *dentry;
4603 struct ldiskfs_dir_entry_2 *de;
4604 struct buffer_head *bh;
4605 struct lu_fid *fid = (struct lu_fid *) rec;
4606 struct htree_lock *hlock = NULL;
4611 LASSERT(dir->i_op != NULL);
4612 LASSERT(dir->i_op->lookup != NULL);
4614 dentry = osd_child_dentry_get(env, obj,
4615 (char *)key, strlen((char *)key));
4617 if (obj->oo_hl_head != NULL) {
4618 hlock = osd_oti_get(env)->oti_hlock;
4619 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4620 dir, LDISKFS_HLOCK_LOOKUP);
4622 down_read(&obj->oo_ext_idx_sem);
4625 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4627 struct osd_thread_info *oti = osd_oti_get(env);
4628 struct osd_inode_id *id = &oti->oti_id;
4629 struct osd_idmap_cache *oic = &oti->oti_cache;
4630 struct osd_device *dev = osd_obj2dev(obj);
4632 ino = le32_to_cpu(de->inode);
4633 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4635 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4639 rc = osd_get_fid_from_dentry(de, rec);
4641 /* done with de, release bh */
4644 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4645 const char *name = (const char *)key;
4647 /* If the parent is on remote MDT, and there
4648 * is no FID-in-dirent, then we have to get
4649 * the parent FID from the linkEA. */
4650 if (likely(strlen(name) == 2 &&
4651 name[0] == '.' && name[1] == '.'))
4652 rc = osd_get_pfid_from_linkea(env, obj,
4655 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4658 osd_id_gen(id, ino, OSD_OII_NOGEN);
4661 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4662 fid_zero(&oic->oic_fid);
4667 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4668 rc = osd_consistency_check(oti, dev, oic);
4670 fid_zero(&oic->oic_fid);
4679 ldiskfs_htree_unlock(hlock);
4681 up_read(&obj->oo_ext_idx_sem);
4686 * Put the osd object once done with it.
4688 * \param obj osd object that needs to be put
4690 static inline void osd_object_put(const struct lu_env *env,
4691 struct osd_object *obj)
4693 lu_object_put(env, &obj->oo_dt.do_lu);
4696 static int osd_index_declare_ea_insert(const struct lu_env *env,
4697 struct dt_object *dt,
4698 const struct dt_rec *rec,
4699 const struct dt_key *key,
4700 struct thandle *handle)
4702 struct osd_thandle *oh;
4703 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4704 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4705 const struct lu_fid *fid = rec1->rec_fid;
4706 int credits, rc = 0;
4707 struct osd_idmap_cache *idc;
4710 LASSERT(!dt_object_remote(dt));
4711 LASSERT(handle != NULL);
4712 LASSERT(fid != NULL);
4713 LASSERT(rec1->rec_type != 0);
4715 oh = container_of0(handle, struct osd_thandle, ot_super);
4716 LASSERT(oh->ot_handle == NULL);
4718 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4720 /* we can't call iget() while a transactions is running
4721 * (this can lead to a deadlock), but we need to know
4722 * inum and object type. so we find this information at
4723 * declaration and cache in per-thread info */
4724 idc = osd_idc_find_or_init(env, osd, fid);
4726 RETURN(PTR_ERR(idc));
4727 if (idc->oic_remote) {
4728 /* a reference to remote inode is represented by an
4729 * agent inode which we have to create */
4730 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4731 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4734 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4736 if (osd_dt_obj(dt)->oo_inode != NULL) {
4737 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4739 /* We ignore block quota on meta pool (MDTs), so needn't
4740 * calculate how many blocks will be consumed by this index
4742 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4743 i_gid_read(inode), 0, oh,
4744 osd_dt_obj(dt), true, NULL, false);
4751 * Index add function for interoperability mode (b11826).
4752 * It will add the directory entry.This entry is needed to
4753 * maintain name->fid mapping.
4755 * \param key it is key i.e. file entry to be inserted
4756 * \param rec it is value of given key i.e. fid
4758 * \retval 0, on success
4759 * \retval -ve, on error
4761 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4762 const struct dt_rec *rec,
4763 const struct dt_key *key, struct thandle *th,
4766 struct osd_object *obj = osd_dt_obj(dt);
4767 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4768 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4769 const struct lu_fid *fid = rec1->rec_fid;
4770 const char *name = (const char *)key;
4771 struct osd_thread_info *oti = osd_oti_get(env);
4772 struct inode *child_inode = NULL;
4773 struct osd_idmap_cache *idc;
4777 if (!dt_object_exists(dt))
4780 LASSERT(osd_invariant(obj));
4781 LASSERT(!dt_object_remote(dt));
4782 LASSERT(th != NULL);
4784 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4786 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4788 idc = osd_idc_find(env, osd, fid);
4789 if (unlikely(idc == NULL)) {
4790 /* this dt_insert() wasn't declared properly, so
4791 * FID is missing in OI cache. we better do not
4792 * lookup FID in FLDB/OI and don't risk to deadlock,
4793 * but in some special cases (lfsck testing, etc)
4794 * it's much simpler than fixing a caller */
4795 CERROR("%s: "DFID" wasn't declared for insert\n",
4796 osd_name(osd), PFID(fid));
4798 idc = osd_idc_find_or_init(env, osd, fid);
4800 RETURN(PTR_ERR(idc));
4803 if (idc->oic_remote) {
4804 /* Insert remote entry */
4805 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4806 struct osd_mdobj_map *omm = osd->od_mdt_map;
4807 struct osd_thandle *oh;
4809 /* If parent on remote MDT, we need put this object
4811 oh = container_of(th, typeof(*oh), ot_super);
4812 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4814 CERROR("%s: add "DFID" error: rc = %d\n",
4816 PFID(lu_object_fid(&dt->do_lu)), rc);
4820 child_inode = igrab(omm->omm_remote_parent->d_inode);
4822 child_inode = osd_create_local_agent_inode(env, osd,
4823 obj, fid, rec1->rec_type & S_IFMT, th);
4824 if (IS_ERR(child_inode))
4825 RETURN(PTR_ERR(child_inode));
4828 /* Insert local entry */
4829 if (unlikely(idc->oic_lid.oii_ino == 0)) {
4830 /* for a reason OI cache wasn't filled properly */
4831 CERROR("%s: OIC for "DFID" isn't filled\n",
4832 osd_name(osd), PFID(fid));
4835 child_inode = oti->oti_inode;
4836 if (unlikely(child_inode == NULL)) {
4837 struct ldiskfs_inode_info *lii;
4841 child_inode = oti->oti_inode = &lii->vfs_inode;
4843 child_inode->i_sb = osd_sb(osd);
4844 child_inode->i_ino = idc->oic_lid.oii_ino;
4845 child_inode->i_mode = rec1->rec_type & S_IFMT;
4848 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4850 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4851 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4853 if (child_inode && child_inode != oti->oti_inode)
4855 LASSERT(osd_invariant(obj));
4856 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4861 * Initialize osd Iterator for given osd index object.
4863 * \param dt osd index object
4866 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4867 struct dt_object *dt,
4870 struct osd_it_iam *it;
4871 struct osd_object *obj = osd_dt_obj(dt);
4872 struct lu_object *lo = &dt->do_lu;
4873 struct iam_path_descr *ipd;
4874 struct iam_container *bag = &obj->oo_dir->od_container;
4876 if (!dt_object_exists(dt))
4877 return ERR_PTR(-ENOENT);
4881 return ERR_PTR(-ENOMEM);
4883 ipd = osd_it_ipd_get(env, bag);
4884 if (likely(ipd != NULL)) {
4888 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4889 return (struct dt_it *)it;
4892 return ERR_PTR(-ENOMEM);
4897 * free given Iterator.
4900 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4902 struct osd_it_iam *it = (struct osd_it_iam *)di;
4903 struct osd_object *obj = it->oi_obj;
4905 iam_it_fini(&it->oi_it);
4906 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4907 lu_object_put(env, &obj->oo_dt.do_lu);
4912 * Move Iterator to record specified by \a key
4914 * \param di osd iterator
4915 * \param key key for index
4917 * \retval +ve di points to record with least key not larger than key
4918 * \retval 0 di points to exact matched key
4919 * \retval -ve failure
4922 static int osd_it_iam_get(const struct lu_env *env,
4923 struct dt_it *di, const struct dt_key *key)
4925 struct osd_thread_info *oti = osd_oti_get(env);
4926 struct osd_it_iam *it = (struct osd_it_iam *)di;
4928 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4929 /* swab quota uid/gid */
4930 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4931 key = (struct dt_key *)&oti->oti_quota_id;
4934 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4940 * \param di osd iterator
4942 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4944 struct osd_it_iam *it = (struct osd_it_iam *)di;
4946 iam_it_put(&it->oi_it);
4950 * Move iterator by one record
4952 * \param di osd iterator
4954 * \retval +1 end of container reached
4956 * \retval -ve failure
4959 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4961 struct osd_it_iam *it = (struct osd_it_iam *)di;
4963 return iam_it_next(&it->oi_it);
4967 * Return pointer to the key under iterator.
4970 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4971 const struct dt_it *di)
4973 struct osd_thread_info *oti = osd_oti_get(env);
4974 struct osd_it_iam *it = (struct osd_it_iam *)di;
4975 struct osd_object *obj = it->oi_obj;
4978 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4980 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4981 /* swab quota uid/gid */
4982 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4983 key = (struct dt_key *)&oti->oti_quota_id;
4990 * Return size of key under iterator (in bytes)
4993 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4995 struct osd_it_iam *it = (struct osd_it_iam *)di;
4997 return iam_it_key_size(&it->oi_it);
5001 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5003 /* check if file type is required */
5004 if (ent->lde_attrs & LUDA_TYPE) {
5005 struct luda_type *lt;
5006 int align = sizeof(*lt) - 1;
5008 len = (len + align) & ~align;
5009 lt = (struct luda_type *)(ent->lde_name + len);
5010 lt->lt_type = cpu_to_le16(DTTOIF(type));
5013 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5017 * build lu direct from backend fs dirent.
5021 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5022 char *name, __u16 namelen, __u16 type, __u32 attr)
5024 ent->lde_attrs = attr | LUDA_FID;
5025 fid_cpu_to_le(&ent->lde_fid, fid);
5027 ent->lde_hash = cpu_to_le64(offset);
5028 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5030 strncpy(ent->lde_name, name, namelen);
5031 ent->lde_name[namelen] = '\0';
5032 ent->lde_namelen = cpu_to_le16(namelen);
5034 /* append lustre attributes */
5035 osd_it_append_attrs(ent, namelen, type);
5039 * Return pointer to the record under iterator.
5041 static int osd_it_iam_rec(const struct lu_env *env,
5042 const struct dt_it *di,
5043 struct dt_rec *dtrec, __u32 attr)
5045 struct osd_it_iam *it = (struct osd_it_iam *)di;
5046 struct osd_thread_info *info = osd_oti_get(env);
5049 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5050 const struct osd_fid_pack *rec;
5051 struct lu_fid *fid = &info->oti_fid;
5052 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5058 name = (char *)iam_it_key_get(&it->oi_it);
5060 RETURN(PTR_ERR(name));
5062 namelen = iam_it_key_size(&it->oi_it);
5064 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5066 RETURN(PTR_ERR(rec));
5068 rc = osd_fid_unpack(fid, rec);
5072 hash = iam_it_store(&it->oi_it);
5074 /* IAM does not store object type in IAM index (dir) */
5075 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5077 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5078 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5079 (struct iam_rec *)dtrec);
5080 osd_quota_unpack(it->oi_obj, dtrec);
5082 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5083 (struct iam_rec *)dtrec);
5090 * Returns cookie for current Iterator position.
5092 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5094 struct osd_it_iam *it = (struct osd_it_iam *)di;
5096 return iam_it_store(&it->oi_it);
5100 * Restore iterator from cookie.
5102 * \param di osd iterator
5103 * \param hash Iterator location cookie
5105 * \retval +ve di points to record with least key not larger than key.
5106 * \retval 0 di points to exact matched key
5107 * \retval -ve failure
5110 static int osd_it_iam_load(const struct lu_env *env,
5111 const struct dt_it *di, __u64 hash)
5113 struct osd_it_iam *it = (struct osd_it_iam *)di;
5115 return iam_it_load(&it->oi_it, hash);
5118 static const struct dt_index_operations osd_index_iam_ops = {
5119 .dio_lookup = osd_index_iam_lookup,
5120 .dio_declare_insert = osd_index_declare_iam_insert,
5121 .dio_insert = osd_index_iam_insert,
5122 .dio_declare_delete = osd_index_declare_iam_delete,
5123 .dio_delete = osd_index_iam_delete,
5125 .init = osd_it_iam_init,
5126 .fini = osd_it_iam_fini,
5127 .get = osd_it_iam_get,
5128 .put = osd_it_iam_put,
5129 .next = osd_it_iam_next,
5130 .key = osd_it_iam_key,
5131 .key_size = osd_it_iam_key_size,
5132 .rec = osd_it_iam_rec,
5133 .store = osd_it_iam_store,
5134 .load = osd_it_iam_load
5140 * Creates or initializes iterator context.
5142 * \retval struct osd_it_ea, iterator structure on success
5145 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5146 struct dt_object *dt,
5149 struct osd_object *obj = osd_dt_obj(dt);
5150 struct osd_thread_info *info = osd_oti_get(env);
5151 struct osd_it_ea *oie;
5153 struct lu_object *lo = &dt->do_lu;
5154 struct dentry *obj_dentry;
5157 if (!dt_object_exists(dt) || obj->oo_destroyed)
5158 RETURN(ERR_PTR(-ENOENT));
5160 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5162 RETURN(ERR_PTR(-ENOMEM));
5163 obj_dentry = &oie->oie_dentry;
5165 obj_dentry->d_inode = obj->oo_inode;
5166 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5167 obj_dentry->d_name.hash = 0;
5169 oie->oie_rd_dirent = 0;
5170 oie->oie_it_dirent = 0;
5171 oie->oie_dirent = NULL;
5172 if (unlikely(!info->oti_it_ea_buf_used)) {
5173 oie->oie_buf = info->oti_it_ea_buf;
5174 info->oti_it_ea_buf_used = 1;
5176 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5177 if (oie->oie_buf == NULL)
5178 RETURN(ERR_PTR(-ENOMEM));
5182 file = &oie->oie_file;
5184 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5185 if (attr & LUDA_64BITHASH)
5186 file->f_mode = FMODE_64BITHASH;
5188 file->f_mode = FMODE_32BITHASH;
5189 file->f_path.dentry = obj_dentry;
5190 file->f_mapping = obj->oo_inode->i_mapping;
5191 file->f_op = obj->oo_inode->i_fop;
5192 set_file_inode(file, obj->oo_inode);
5195 RETURN((struct dt_it *) oie);
5199 * Destroy or finishes iterator context.
5201 * \param di iterator structure to be destroyed
5203 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5205 struct osd_thread_info *info = osd_oti_get(env);
5206 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5207 struct osd_object *obj = oie->oie_obj;
5208 struct inode *inode = obj->oo_inode;
5211 oie->oie_file.f_op->release(inode, &oie->oie_file);
5212 lu_object_put(env, &obj->oo_dt.do_lu);
5213 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5214 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5216 info->oti_it_ea_buf_used = 0;
5217 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5222 * It position the iterator at given key, so that next lookup continues from
5223 * that key Or it is similar to dio_it->load() but based on a key,
5224 * rather than file position.
5226 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5229 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5231 static int osd_it_ea_get(const struct lu_env *env,
5232 struct dt_it *di, const struct dt_key *key)
5234 struct osd_it_ea *it = (struct osd_it_ea *)di;
5237 LASSERT(((const char *)key)[0] == '\0');
5238 it->oie_file.f_pos = 0;
5239 it->oie_rd_dirent = 0;
5240 it->oie_it_dirent = 0;
5241 it->oie_dirent = NULL;
5249 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5253 struct osd_filldir_cbs {
5254 #ifdef HAVE_DIR_CONTEXT
5255 struct dir_context ctx;
5257 struct osd_it_ea *it;
5260 * It is called internally by ->readdir(). It fills the
5261 * iterator's in-memory data structure with required
5262 * information i.e. name, namelen, rec_size etc.
5264 * \param buf in which information to be filled in.
5265 * \param name name of the file in given dir
5267 * \retval 0 on success
5268 * \retval 1 on buffer full
5270 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5271 loff_t offset, __u64 ino,
5274 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5275 struct osd_object *obj = it->oie_obj;
5276 struct osd_it_ea_dirent *ent = it->oie_dirent;
5277 struct lu_fid *fid = &ent->oied_fid;
5278 struct osd_fid_pack *rec;
5281 /* this should never happen */
5282 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5283 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5287 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5291 /* "." is just the object itself. */
5292 if (namelen == 1 && name[0] == '.') {
5293 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5294 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5295 rec = (struct osd_fid_pack*) (name + namelen + 1);
5296 if (osd_fid_unpack(fid, rec) != 0)
5301 d_type &= ~LDISKFS_DIRENT_LUFID;
5303 /* NOT export local root. */
5304 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5305 ino = obj->oo_inode->i_ino;
5306 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5309 ent->oied_ino = ino;
5310 ent->oied_off = offset;
5311 ent->oied_namelen = namelen;
5312 ent->oied_type = d_type;
5314 memcpy(ent->oied_name, name, namelen);
5316 it->oie_rd_dirent++;
5317 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5322 * Calls ->readdir() to load a directory entry at a time
5323 * and stored it in iterator's in-memory data structure.
5325 * \param di iterator's in memory structure
5327 * \retval 0 on success
5328 * \retval -ve on error
5329 * \retval +1 reach the end of entry
5331 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5332 const struct dt_it *di)
5334 struct osd_it_ea *it = (struct osd_it_ea *)di;
5335 struct osd_object *obj = it->oie_obj;
5336 struct inode *inode = obj->oo_inode;
5337 struct htree_lock *hlock = NULL;
5338 struct file *filp = &it->oie_file;
5340 struct osd_filldir_cbs buf = {
5341 #ifdef HAVE_DIR_CONTEXT
5342 .ctx.actor = osd_ldiskfs_filldir,
5348 it->oie_dirent = it->oie_buf;
5349 it->oie_rd_dirent = 0;
5351 if (obj->oo_hl_head != NULL) {
5352 hlock = osd_oti_get(env)->oti_hlock;
5353 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5354 inode, LDISKFS_HLOCK_READDIR);
5356 down_read(&obj->oo_ext_idx_sem);
5359 #ifdef HAVE_DIR_CONTEXT
5360 buf.ctx.pos = filp->f_pos;
5361 rc = inode->i_fop->iterate(filp, &buf.ctx);
5362 filp->f_pos = buf.ctx.pos;
5364 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5368 ldiskfs_htree_unlock(hlock);
5370 up_read(&obj->oo_ext_idx_sem);
5372 if (it->oie_rd_dirent == 0) {
5373 /*If it does not get any dirent, it means it has been reached
5374 *to the end of the dir */
5375 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5379 it->oie_dirent = it->oie_buf;
5380 it->oie_it_dirent = 1;
5387 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5388 * to load a directory entry at a time and stored it in
5389 * iterator's in-memory data structure.
5391 * \param di iterator's in memory structure
5393 * \retval +ve iterator reached to end
5394 * \retval 0 iterator not reached to end
5395 * \retval -ve on error
5397 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5399 struct osd_it_ea *it = (struct osd_it_ea *)di;
5404 if (it->oie_it_dirent < it->oie_rd_dirent) {
5406 (void *) it->oie_dirent +
5407 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5408 it->oie_dirent->oied_namelen);
5409 it->oie_it_dirent++;
5412 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5415 rc = osd_ldiskfs_it_fill(env, di);
5422 * Returns the key at current position from iterator's in memory structure.
5424 * \param di iterator's in memory structure
5426 * \retval key i.e. struct dt_key on success
5428 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5429 const struct dt_it *di)
5431 struct osd_it_ea *it = (struct osd_it_ea *)di;
5433 return (struct dt_key *)it->oie_dirent->oied_name;
5437 * Returns the key's size at current position from iterator's in memory structure.
5439 * \param di iterator's in memory structure
5441 * \retval key_size i.e. struct dt_key on success
5443 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5445 struct osd_it_ea *it = (struct osd_it_ea *)di;
5447 return it->oie_dirent->oied_namelen;
5451 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5453 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5454 "dot_dotdot = %d\n", dot_dotdot);
5456 if (LDISKFS_DIR_REC_LEN(de) >=
5457 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5464 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5465 unsigned blocksize, int dot_dotdot)
5468 return osd_dot_dotdot_has_space(de, dot_dotdot);
5470 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5471 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5478 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5479 struct dentry *dentry, const struct lu_fid *fid,
5480 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5481 struct htree_lock *hlock, int dot_dotdot)
5483 struct inode *dir = dentry->d_parent->d_inode;
5484 struct inode *inode = dentry->d_inode;
5485 struct osd_fid_pack *rec;
5486 struct ldiskfs_dentry_param *ldp;
5487 int namelen = dentry->d_name.len;
5489 struct osd_thread_info *info = osd_oti_get(env);
5492 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5493 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5496 /* There is enough space to hold the FID-in-dirent. */
5497 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5499 rc = ldiskfs_journal_get_write_access(jh, bh);
5503 de->name[namelen] = 0;
5504 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5505 rec->fp_len = sizeof(struct lu_fid) + 1;
5506 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5507 de->file_type |= LDISKFS_DIRENT_LUFID;
5508 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5513 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5515 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5519 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5520 osd_get_ldiskfs_dirent_param(ldp, fid);
5521 dentry->d_fsdata = (void *)ldp;
5522 ll_vfs_dq_init(dir);
5523 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5524 /* It is too bad, we cannot reinsert the name entry back.
5525 * That means we lose it! */
5527 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5528 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5529 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5530 dir->i_ino, dir->i_generation, namelen,
5531 dentry->d_name.name, PFID(fid), rc);
5537 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5538 struct osd_it_ea *it, struct lu_fid *fid,
5539 struct osd_inode_id *id, __u32 *attr)
5541 struct osd_thread_info *info = osd_oti_get(env);
5542 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5543 struct osd_device *dev = osd_obj2dev(obj);
5544 struct super_block *sb = osd_sb(dev);
5545 const char *devname =
5546 LDISKFS_SB(sb)->s_es->s_volume_name;
5547 struct osd_it_ea_dirent *ent = it->oie_dirent;
5548 struct inode *dir = obj->oo_inode;
5549 struct htree_lock *hlock = NULL;
5550 struct buffer_head *bh = NULL;
5551 handle_t *jh = NULL;
5552 struct ldiskfs_dir_entry_2 *de;
5553 struct dentry *dentry;
5554 struct inode *inode;
5561 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5562 inode = osd_iget(info, dev, id);
5563 if (IS_ERR(inode)) {
5564 rc = PTR_ERR(inode);
5565 if (rc == -ENOENT || rc == -ESTALE) {
5566 *attr |= LUDA_UNKNOWN;
5569 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5570 "check_repair, dir = %lu/%u, name = %.*s: "
5572 devname, dir->i_ino, dir->i_generation,
5573 ent->oied_namelen, ent->oied_name, rc);
5579 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5581 rc = osd_get_lma(info, inode, dentry, lma);
5587 if (ent->oied_name[0] == '.') {
5588 if (ent->oied_namelen == 1)
5590 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5594 /* We need to ensure that the name entry is still valid.
5595 * Because it may be removed or renamed by other already.
5597 * The unlink or rename operation will start journal before PDO lock,
5598 * so to avoid deadlock, here we need to start journal handle before
5599 * related PDO lock also. But because we do not know whether there
5600 * will be something to be repaired before PDO lock, we just start
5601 * journal without conditions.
5603 * We may need to remove the name entry firstly, then insert back.
5604 * One credit is for user quota file update.
5605 * One credit is for group quota file update.
5606 * Two credits are for dirty inode. */
5607 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5608 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5610 if (dev->od_dirent_journal != 0) {
5613 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5616 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5617 "check_repair, dir = %lu/%u, credits = %d, "
5618 "name = %.*s: rc = %d\n",
5619 devname, dir->i_ino, dir->i_generation, credits,
5620 ent->oied_namelen, ent->oied_name, rc);
5622 GOTO(out_inode, rc);
5625 if (obj->oo_hl_head != NULL) {
5626 hlock = osd_oti_get(env)->oti_hlock;
5627 /* "0" means exclusive lock for the whole directory.
5628 * We need to prevent others access such name entry
5629 * during the delete + insert. Neither HLOCK_ADD nor
5630 * HLOCK_DEL cannot guarantee the atomicity. */
5631 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5633 down_write(&obj->oo_ext_idx_sem);
5636 if (obj->oo_hl_head != NULL) {
5637 hlock = osd_oti_get(env)->oti_hlock;
5638 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5639 LDISKFS_HLOCK_LOOKUP);
5641 down_read(&obj->oo_ext_idx_sem);
5645 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5646 /* For dot/dotdot entry, if there is not enough space to hold the
5647 * FID-in-dirent, just keep them there. It only happens when the
5648 * device upgraded from 1.8 or restored from MDT file-level backup.
5649 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5650 * and needs to get FID from LMA when readdir, it will not affect the
5651 * performance much. */
5652 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5653 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5654 *attr |= LUDA_IGNORE;
5660 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5661 struct lu_fid *tfid = &lma->lma_self_fid;
5663 *attr |= LUDA_IGNORE;
5664 /* It must be REMOTE_PARENT_DIR and as the
5665 * dotdot entry of remote directory */
5666 if (unlikely(dot_dotdot != 2 ||
5667 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5668 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5669 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5670 "parent directory, but got %.*s under "
5671 "dir = %lu/%u with the FID "DFID"\n",
5672 devname, ent->oied_namelen,
5673 ent->oied_name, dir->i_ino,
5674 dir->i_generation, PFID(tfid));
5676 GOTO(out, rc = -EIO);
5682 if (fid_is_sane(fid)) {
5683 /* FID-in-dirent is valid. */
5684 if (lu_fid_eq(fid, &lma->lma_self_fid))
5687 /* Do not repair under dryrun mode. */
5688 if (*attr & LUDA_VERIFY_DRYRUN) {
5689 *attr |= LUDA_REPAIR;
5696 dev->od_dirent_journal = 1;
5697 if (hlock != NULL) {
5698 ldiskfs_htree_unlock(hlock);
5701 up_read(&obj->oo_ext_idx_sem);
5707 *fid = lma->lma_self_fid;
5709 /* Update the FID-in-dirent. */
5710 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5713 *attr |= LUDA_REPAIR;
5715 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5716 "in the dirent, dir = %lu/%u, "
5717 "name = %.*s, "DFID": rc = %d\n",
5718 devname, dir->i_ino, dir->i_generation,
5719 ent->oied_namelen, ent->oied_name,
5722 /* Do not repair under dryrun mode. */
5723 if (*attr & LUDA_VERIFY_DRYRUN) {
5724 *fid = lma->lma_self_fid;
5725 *attr |= LUDA_REPAIR;
5732 dev->od_dirent_journal = 1;
5733 if (hlock != NULL) {
5734 ldiskfs_htree_unlock(hlock);
5737 up_read(&obj->oo_ext_idx_sem);
5743 *fid = lma->lma_self_fid;
5745 /* Append the FID-in-dirent. */
5746 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5749 *attr |= LUDA_REPAIR;
5751 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5752 "after the dirent, dir = %lu/%u, "
5753 "name = %.*s, "DFID": rc = %d\n",
5754 devname, dir->i_ino, dir->i_generation,
5755 ent->oied_namelen, ent->oied_name,
5759 /* Do not repair under dryrun mode. */
5760 if (*attr & LUDA_VERIFY_DRYRUN) {
5761 if (fid_is_sane(fid)) {
5762 *attr |= LUDA_REPAIR;
5764 lu_igif_build(fid, inode->i_ino,
5765 inode->i_generation);
5766 *attr |= LUDA_UPGRADE;
5774 dev->od_dirent_journal = 1;
5775 if (hlock != NULL) {
5776 ldiskfs_htree_unlock(hlock);
5779 up_read(&obj->oo_ext_idx_sem);
5786 if (unlikely(fid_is_sane(fid))) {
5787 /* FID-in-dirent exists, but FID-in-LMA is lost.
5788 * Trust the FID-in-dirent, and add FID-in-LMA. */
5789 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5791 *attr |= LUDA_REPAIR;
5793 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5794 "update dirent, dir = %lu/%u, "
5795 "name = %.*s, "DFID": rc = %d\n",
5796 devname, dir->i_ino, dir->i_generation,
5797 ent->oied_namelen, ent->oied_name,
5800 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5801 /* It is probably IGIF object. Only aappend the
5802 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5803 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5806 *attr |= LUDA_UPGRADE;
5808 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5809 "after the dirent, dir = %lu/%u, "
5810 "name = %.*s, "DFID": rc = %d\n",
5811 devname, dir->i_ino, dir->i_generation,
5812 ent->oied_namelen, ent->oied_name,
5821 if (hlock != NULL) {
5822 ldiskfs_htree_unlock(hlock);
5824 if (dev->od_dirent_journal != 0)
5825 up_write(&obj->oo_ext_idx_sem);
5827 up_read(&obj->oo_ext_idx_sem);
5831 ldiskfs_journal_stop(jh);
5835 if (rc >= 0 && !dirty)
5836 dev->od_dirent_journal = 0;
5842 * Returns the value at current position from iterator's in memory structure.
5844 * \param di struct osd_it_ea, iterator's in memory structure
5845 * \param attr attr requested for dirent.
5846 * \param lde lustre dirent
5848 * \retval 0 no error and \param lde has correct lustre dirent.
5849 * \retval -ve on error
5851 static inline int osd_it_ea_rec(const struct lu_env *env,
5852 const struct dt_it *di,
5853 struct dt_rec *dtrec, __u32 attr)
5855 struct osd_it_ea *it = (struct osd_it_ea *)di;
5856 struct osd_object *obj = it->oie_obj;
5857 struct osd_device *dev = osd_obj2dev(obj);
5858 struct osd_thread_info *oti = osd_oti_get(env);
5859 struct osd_inode_id *id = &oti->oti_id;
5860 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5861 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5862 __u32 ino = it->oie_dirent->oied_ino;
5866 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5868 if (attr & LUDA_VERIFY) {
5869 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5870 attr |= LUDA_IGNORE;
5871 /* If the parent is on remote MDT, and there
5872 * is no FID-in-dirent, then we have to get
5873 * the parent FID from the linkEA. */
5874 if (!fid_is_sane(fid) &&
5875 it->oie_dirent->oied_namelen == 2 &&
5876 it->oie_dirent->oied_name[0] == '.' &&
5877 it->oie_dirent->oied_name[1] == '.')
5878 osd_get_pfid_from_linkea(env, obj, fid);
5880 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5884 if (!fid_is_sane(fid)) {
5885 attr &= ~LUDA_IGNORE;
5886 attr |= LUDA_UNKNOWN;
5889 attr &= ~LU_DIRENT_ATTRS_MASK;
5890 if (!fid_is_sane(fid)) {
5891 bool is_dotdot = false;
5892 if (it->oie_dirent->oied_namelen == 2 &&
5893 it->oie_dirent->oied_name[0] == '.' &&
5894 it->oie_dirent->oied_name[1] == '.')
5896 /* If the parent is on remote MDT, and there
5897 * is no FID-in-dirent, then we have to get
5898 * the parent FID from the linkEA. */
5899 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5900 rc = osd_get_pfid_from_linkea(env, obj, fid);
5902 if (is_dotdot == false &&
5903 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5906 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5909 osd_id_gen(id, ino, OSD_OII_NOGEN);
5913 /* Pack the entry anyway, at least the offset is right. */
5914 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5915 it->oie_dirent->oied_name,
5916 it->oie_dirent->oied_namelen,
5917 it->oie_dirent->oied_type, attr);
5922 if (osd_remote_fid(env, dev, fid))
5925 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5926 osd_add_oi_cache(oti, dev, id, fid);
5928 RETURN(rc > 0 ? 0 : rc);
5932 * Returns the record size size at current position.
5934 * This function will return record(lu_dirent) size in bytes.
5936 * \param[in] env execution environment
5937 * \param[in] di iterator's in memory structure
5938 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5939 * calculate the lu_dirent size.
5941 * \retval record size(in bytes & in memory) of the current lu_dirent
5944 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5947 struct osd_it_ea *it = (struct osd_it_ea *)di;
5949 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5953 * Returns a cookie for current position of the iterator head, so that
5954 * user can use this cookie to load/start the iterator next time.
5956 * \param di iterator's in memory structure
5958 * \retval cookie for current position, on success
5960 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5962 struct osd_it_ea *it = (struct osd_it_ea *)di;
5964 return it->oie_dirent->oied_off;
5968 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5969 * to load a directory entry at a time and stored it i inn,
5970 * in iterator's in-memory data structure.
5972 * \param di struct osd_it_ea, iterator's in memory structure
5974 * \retval +ve on success
5975 * \retval -ve on error
5977 static int osd_it_ea_load(const struct lu_env *env,
5978 const struct dt_it *di, __u64 hash)
5980 struct osd_it_ea *it = (struct osd_it_ea *)di;
5984 it->oie_file.f_pos = hash;
5986 rc = osd_ldiskfs_it_fill(env, di);
5997 * Index lookup function for interoperability mode (b11826).
5999 * \param key, key i.e. file name to be searched
6001 * \retval +ve, on success
6002 * \retval -ve, on error
6004 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6005 struct dt_rec *rec, const struct dt_key *key)
6007 struct osd_object *obj = osd_dt_obj(dt);
6012 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6013 LINVRNT(osd_invariant(obj));
6015 rc = osd_ea_lookup_rec(env, obj, rec, key);
6022 * Index and Iterator operations for interoperability
6023 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6025 static const struct dt_index_operations osd_index_ea_ops = {
6026 .dio_lookup = osd_index_ea_lookup,
6027 .dio_declare_insert = osd_index_declare_ea_insert,
6028 .dio_insert = osd_index_ea_insert,
6029 .dio_declare_delete = osd_index_declare_ea_delete,
6030 .dio_delete = osd_index_ea_delete,
6032 .init = osd_it_ea_init,
6033 .fini = osd_it_ea_fini,
6034 .get = osd_it_ea_get,
6035 .put = osd_it_ea_put,
6036 .next = osd_it_ea_next,
6037 .key = osd_it_ea_key,
6038 .key_size = osd_it_ea_key_size,
6039 .rec = osd_it_ea_rec,
6040 .rec_size = osd_it_ea_rec_size,
6041 .store = osd_it_ea_store,
6042 .load = osd_it_ea_load
6046 static void *osd_key_init(const struct lu_context *ctx,
6047 struct lu_context_key *key)
6049 struct osd_thread_info *info;
6051 OBD_ALLOC_PTR(info);
6053 return ERR_PTR(-ENOMEM);
6055 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6056 if (info->oti_it_ea_buf == NULL)
6059 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6061 info->oti_hlock = ldiskfs_htree_lock_alloc();
6062 if (info->oti_hlock == NULL)
6068 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6071 return ERR_PTR(-ENOMEM);
6074 static void osd_key_fini(const struct lu_context *ctx,
6075 struct lu_context_key *key, void* data)
6077 struct osd_thread_info *info = data;
6078 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6079 struct osd_idmap_cache *idc = info->oti_ins_cache;
6081 if (info->oti_inode != NULL)
6083 if (info->oti_hlock != NULL)
6084 ldiskfs_htree_lock_free(info->oti_hlock);
6085 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6086 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6087 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6088 lu_buf_free(&info->oti_big_buf);
6090 LASSERT(info->oti_ins_cache_size > 0);
6091 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6092 info->oti_ins_cache = NULL;
6093 info->oti_ins_cache_size = 0;
6098 static void osd_key_exit(const struct lu_context *ctx,
6099 struct lu_context_key *key, void *data)
6101 struct osd_thread_info *info = data;
6103 LASSERT(info->oti_r_locks == 0);
6104 LASSERT(info->oti_w_locks == 0);
6105 LASSERT(info->oti_txns == 0);
6108 /* type constructor/destructor: osd_type_init, osd_type_fini */
6109 LU_TYPE_INIT_FINI(osd, &osd_key);
6111 struct lu_context_key osd_key = {
6112 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6113 .lct_init = osd_key_init,
6114 .lct_fini = osd_key_fini,
6115 .lct_exit = osd_key_exit
6119 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6120 const char *name, struct lu_device *next)
6122 struct osd_device *osd = osd_dev(d);
6124 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6125 >= sizeof(osd->od_svname))
6127 return osd_procfs_init(osd, name);
6130 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6132 struct seq_server_site *ss = osd_seq_site(osd);
6136 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6139 if (unlikely(ss == NULL))
6142 OBD_ALLOC_PTR(osd->od_cl_seq);
6143 if (osd->od_cl_seq == NULL)
6146 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6147 osd->od_svname, ss->ss_server_seq);
6149 OBD_FREE_PTR(osd->od_cl_seq);
6150 osd->od_cl_seq = NULL;
6154 if (ss->ss_node_id == 0) {
6155 /* If the OSD on the sequence controller(MDT0), then allocate
6156 * sequence here, otherwise allocate sequence after connected
6157 * to MDT0 (see mdt_register_lwp_callback()). */
6158 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6159 &osd->od_cl_seq->lcs_space, env);
6165 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6167 if (osd->od_cl_seq == NULL)
6170 seq_client_fini(osd->od_cl_seq);
6171 OBD_FREE_PTR(osd->od_cl_seq);
6172 osd->od_cl_seq = NULL;
6175 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6179 /* shutdown quota slave instance associated with the device */
6180 if (o->od_quota_slave != NULL) {
6181 qsd_fini(env, o->od_quota_slave);
6182 o->od_quota_slave = NULL;
6185 osd_fid_fini(env, o);
6190 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6194 if (o->od_mnt != NULL) {
6195 shrink_dcache_sb(osd_sb(o));
6196 osd_sync(env, &o->od_dt_dev);
6205 static int osd_mount(const struct lu_env *env,
6206 struct osd_device *o, struct lustre_cfg *cfg)
6208 const char *name = lustre_cfg_string(cfg, 0);
6209 const char *dev = lustre_cfg_string(cfg, 1);
6211 unsigned long page, s_flags, lmd_flags = 0;
6212 struct page *__page;
6213 struct file_system_type *type;
6214 char *options = NULL;
6216 struct osd_thread_info *info = osd_oti_get(env);
6217 struct lu_fid *fid = &info->oti_fid;
6218 struct inode *inode;
6219 int rc = 0, force_over_128tb = 0;
6222 if (o->od_mnt != NULL)
6225 if (strlen(dev) >= sizeof(o->od_mntdev))
6227 strcpy(o->od_mntdev, dev);
6229 str = lustre_cfg_string(cfg, 2);
6230 s_flags = simple_strtoul(str, NULL, 0);
6231 str = strstr(str, ":");
6233 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6234 opts = lustre_cfg_string(cfg, 3);
6236 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6237 CERROR("%s: device %s extents feature is not guaranteed to "
6238 "work on big-endian systems. Use \"bigendian_extents\" "
6239 "mount option to override.\n", name, dev);
6243 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
6244 force_over_128tb = 1;
6246 __page = alloc_page(GFP_IOFS);
6248 GOTO(out, rc = -ENOMEM);
6249 page = (unsigned long)page_address(__page);
6250 options = (char *)page;
6253 /* strip out the options for back compatiblity */
6254 static char *sout[] = {
6261 /* strip out option we processed in osd */
6262 "bigendian_extents",
6266 strcat(options, opts);
6267 for (rc = 0, str = options; sout[rc]; ) {
6268 char *op = strstr(str, sout[rc]);
6274 if (op == options || *(op - 1) == ',') {
6275 str = op + strlen(sout[rc]);
6276 if (*str == ',' || *str == '\0') {
6277 *str == ',' ? str++ : str;
6278 memmove(op, str, strlen(str) + 1);
6281 for (str = op; *str != ',' && *str != '\0'; str++)
6285 strncat(options, "user_xattr,acl", 14);
6288 /* Glom up mount options */
6289 if (*options != '\0')
6290 strcat(options, ",");
6291 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6293 type = get_fs_type("ldiskfs");
6295 CERROR("%s: cannot find ldiskfs module\n", name);
6296 GOTO(out, rc = -ENODEV);
6299 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6300 module_put(type->owner);
6302 if (IS_ERR(o->od_mnt)) {
6303 rc = PTR_ERR(o->od_mnt);
6305 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6309 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
6310 force_over_128tb == 0) {
6311 CERROR("%s: device %s LDISKFS does not support filesystems "
6312 "greater than 128TB and can cause data corruption. "
6313 "Use \"force_over_128tb\" mount option to override.\n",
6315 GOTO(out, rc = -EINVAL);
6318 #ifdef HAVE_DEV_SET_RDONLY
6319 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6320 CERROR("%s: underlying device %s is marked as read-only. "
6321 "Setup failed\n", name, dev);
6322 GOTO(out_mnt, rc = -EROFS);
6326 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6327 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6328 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6329 GOTO(out_mnt, rc = -EINVAL);
6332 #ifdef LDISKFS_MOUNT_DIRDATA
6333 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6334 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6335 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6336 else if (!o->od_is_ost)
6337 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6338 "enabling the dirdata feature. If you do not want to "
6339 "downgrade to Lustre-1.x again, you can enable it via "
6340 "'tune2fs -O dirdata device'\n", name, dev);
6342 inode = osd_sb(o)->s_root->d_inode;
6343 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6344 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6346 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6350 if (lmd_flags & LMD_FLG_NOSCRUB)
6361 __free_page(__page);
6366 static struct lu_device *osd_device_fini(const struct lu_env *env,
6367 struct lu_device *d)
6369 struct osd_device *o = osd_dev(d);
6372 osd_shutdown(env, o);
6374 osd_scrub_cleanup(env, o);
6375 osd_obj_map_fini(o);
6381 static int osd_device_init0(const struct lu_env *env,
6382 struct osd_device *o,
6383 struct lustre_cfg *cfg)
6385 struct lu_device *l = osd2lu_dev(o);
6386 struct osd_thread_info *info;
6390 /* if the module was re-loaded, env can loose its keys */
6391 rc = lu_env_refill((struct lu_env *) env);
6394 info = osd_oti_get(env);
6397 l->ld_ops = &osd_lu_ops;
6398 o->od_dt_dev.dd_ops = &osd_dt_ops;
6400 spin_lock_init(&o->od_osfs_lock);
6401 mutex_init(&o->od_otable_mutex);
6402 INIT_LIST_HEAD(&o->od_orphan_list);
6404 o->od_read_cache = 1;
6405 o->od_writethrough_cache = 1;
6406 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6408 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6409 sizeof(o->od_svname));
6410 if (cplen >= sizeof(o->od_svname)) {
6415 o->od_index = -1; /* -1 means index is invalid */
6416 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6417 if (rc == LDD_F_SV_TYPE_OST)
6420 o->od_full_scrub_ratio = OFSR_DEFAULT;
6421 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6422 rc = osd_mount(env, o, cfg);
6426 rc = osd_obj_map_init(env, o);
6430 rc = lu_site_init(&o->od_site, l);
6432 GOTO(out_compat, rc);
6433 o->od_site.ls_bottom_dev = l;
6435 rc = lu_site_init_finish(&o->od_site);
6439 INIT_LIST_HEAD(&o->od_ios_list);
6440 /* setup scrub, including OI files initialization */
6441 rc = osd_scrub_setup(env, o);
6445 rc = osd_procfs_init(o, o->od_svname);
6447 CERROR("%s: can't initialize procfs: rc = %d\n",
6449 GOTO(out_scrub, rc);
6452 LASSERT(l->ld_site->ls_linkage.next != NULL);
6453 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6455 /* initialize quota slave instance */
6456 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6458 if (IS_ERR(o->od_quota_slave)) {
6459 rc = PTR_ERR(o->od_quota_slave);
6460 o->od_quota_slave = NULL;
6461 GOTO(out_procfs, rc);
6469 osd_scrub_cleanup(env, o);
6471 lu_site_fini(&o->od_site);
6473 osd_obj_map_fini(o);
6480 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6481 struct lu_device_type *t,
6482 struct lustre_cfg *cfg)
6484 struct osd_device *o;
6489 return ERR_PTR(-ENOMEM);
6491 rc = dt_device_init(&o->od_dt_dev, t);
6493 /* Because the ctx might be revived in dt_device_init,
6494 * refill the env here */
6495 lu_env_refill((struct lu_env *)env);
6496 rc = osd_device_init0(env, o, cfg);
6498 dt_device_fini(&o->od_dt_dev);
6501 if (unlikely(rc != 0))
6504 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6507 static struct lu_device *osd_device_free(const struct lu_env *env,
6508 struct lu_device *d)
6510 struct osd_device *o = osd_dev(d);
6513 /* XXX: make osd top device in order to release reference */
6514 d->ld_site->ls_top_dev = d;
6515 lu_site_purge(env, d->ld_site, -1);
6516 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6517 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6518 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6520 lu_site_fini(&o->od_site);
6521 dt_device_fini(&o->od_dt_dev);
6526 static int osd_process_config(const struct lu_env *env,
6527 struct lu_device *d, struct lustre_cfg *cfg)
6529 struct osd_device *o = osd_dev(d);
6533 switch (cfg->lcfg_command) {
6535 rc = osd_mount(env, o, cfg);
6538 lu_dev_del_linkage(d->ld_site, d);
6539 rc = osd_shutdown(env, o);
6542 LASSERT(&o->od_dt_dev);
6543 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6544 cfg, &o->od_dt_dev);
6545 if (rc > 0 || rc == -ENOSYS)
6546 rc = class_process_proc_param(PARAM_OST,
6547 lprocfs_osd_obd_vars,
6548 cfg, &o->od_dt_dev);
6557 static int osd_recovery_complete(const struct lu_env *env,
6558 struct lu_device *d)
6560 struct osd_device *osd = osd_dev(d);
6564 if (osd->od_quota_slave == NULL)
6567 /* start qsd instance on recovery completion, this notifies the quota
6568 * slave code that we are about to process new requests now */
6569 rc = qsd_start(env, osd->od_quota_slave);
6574 * we use exports to track all osd users
6576 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6577 struct obd_device *obd, struct obd_uuid *cluuid,
6578 struct obd_connect_data *data, void *localdata)
6580 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6581 struct lustre_handle conn;
6585 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6587 rc = class_connect(&conn, obd, cluuid);
6591 *exp = class_conn2export(&conn);
6593 spin_lock(&osd->od_osfs_lock);
6595 spin_unlock(&osd->od_osfs_lock);
6601 * once last export (we don't count self-export) disappeared
6602 * osd can be released
6604 static int osd_obd_disconnect(struct obd_export *exp)
6606 struct obd_device *obd = exp->exp_obd;
6607 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6608 int rc, release = 0;
6611 /* Only disconnect the underlying layers on the final disconnect. */
6612 spin_lock(&osd->od_osfs_lock);
6614 if (osd->od_connects == 0)
6616 spin_unlock(&osd->od_osfs_lock);
6618 rc = class_disconnect(exp); /* bz 9811 */
6620 if (rc == 0 && release)
6621 class_manual_cleanup(obd);
6625 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6626 struct lu_device *dev)
6628 struct osd_device *osd = osd_dev(dev);
6629 struct lr_server_data *lsd =
6630 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6634 if (osd->od_quota_slave != NULL) {
6635 /* set up quota slave objects */
6636 result = qsd_prepare(env, osd->od_quota_slave);
6641 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6642 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6643 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6644 osd->od_index_in_idif = 1;
6646 osd->od_index_in_idif = 0;
6647 result = osd_register_proc_index_in_idif(osd);
6652 osd->od_index_in_idif = 1;
6656 result = osd_fid_init(env, osd);
6661 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6662 struct lu_fid *fid, struct md_op_data *op_data)
6664 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6666 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6669 static const struct lu_object_operations osd_lu_obj_ops = {
6670 .loo_object_init = osd_object_init,
6671 .loo_object_delete = osd_object_delete,
6672 .loo_object_release = osd_object_release,
6673 .loo_object_free = osd_object_free,
6674 .loo_object_print = osd_object_print,
6675 .loo_object_invariant = osd_object_invariant
6678 const struct lu_device_operations osd_lu_ops = {
6679 .ldo_object_alloc = osd_object_alloc,
6680 .ldo_process_config = osd_process_config,
6681 .ldo_recovery_complete = osd_recovery_complete,
6682 .ldo_prepare = osd_prepare,
6685 static const struct lu_device_type_operations osd_device_type_ops = {
6686 .ldto_init = osd_type_init,
6687 .ldto_fini = osd_type_fini,
6689 .ldto_start = osd_type_start,
6690 .ldto_stop = osd_type_stop,
6692 .ldto_device_alloc = osd_device_alloc,
6693 .ldto_device_free = osd_device_free,
6695 .ldto_device_init = osd_device_init,
6696 .ldto_device_fini = osd_device_fini
6699 static struct lu_device_type osd_device_type = {
6700 .ldt_tags = LU_DEVICE_DT,
6701 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6702 .ldt_ops = &osd_device_type_ops,
6703 .ldt_ctx_tags = LCT_LOCAL,
6706 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6708 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6709 struct super_block *sb = osd_sb(osd);
6711 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6715 * lprocfs legacy support.
6717 static struct obd_ops osd_obd_device_ops = {
6718 .o_owner = THIS_MODULE,
6719 .o_connect = osd_obd_connect,
6720 .o_disconnect = osd_obd_disconnect,
6721 .o_fid_alloc = osd_fid_alloc,
6722 .o_health_check = osd_health_check,
6725 static int __init osd_init(void)
6729 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6730 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6731 /* please, try to keep osd_thread_info smaller than a page */
6732 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6737 rc = lu_kmem_init(ldiskfs_caches);
6741 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6742 lprocfs_osd_module_vars,
6743 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6745 lu_kmem_fini(ldiskfs_caches);
6749 static void __exit osd_exit(void)
6751 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6752 lu_kmem_fini(ldiskfs_caches);
6755 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6756 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6757 MODULE_VERSION(LUSTRE_VERSION_STRING);
6758 MODULE_LICENSE("GPL");
6760 module_init(osd_init);
6761 module_exit(osd_exit);