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 osd_object_init0(obj);
1019 LINVRNT(osd_invariant(obj));
1024 * Concurrency: no concurrent access is possible that late in object
1027 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1029 struct osd_object *obj = osd_obj(l);
1031 LINVRNT(osd_invariant(obj));
1033 dt_object_fini(&obj->oo_dt);
1034 if (obj->oo_hl_head != NULL)
1035 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1040 * Concurrency: no concurrent access is possible that late in object
1043 static void osd_index_fini(struct osd_object *o)
1045 struct iam_container *bag;
1047 if (o->oo_dir != NULL) {
1048 bag = &o->oo_dir->od_container;
1049 if (o->oo_inode != NULL) {
1050 if (bag->ic_object == o->oo_inode)
1051 iam_container_fini(bag);
1053 OBD_FREE_PTR(o->oo_dir);
1059 * Concurrency: no concurrent access is possible that late in object
1060 * life-cycle (for all existing callers, that is. New callers have to provide
1061 * their own locking.)
1063 static int osd_inode_unlinked(const struct inode *inode)
1065 return inode->i_nlink == 0;
1069 OSD_TXN_OI_DELETE_CREDITS = 20,
1070 OSD_TXN_INODE_DELETE_CREDITS = 20
1077 #if OSD_THANDLE_STATS
1079 * Set time when the handle is allocated
1081 static void osd_th_alloced(struct osd_thandle *oth)
1083 oth->oth_alloced = cfs_time_current();
1087 * Set time when the handle started
1089 static void osd_th_started(struct osd_thandle *oth)
1091 oth->oth_started = cfs_time_current();
1095 * Helper function to convert time interval to microseconds packed in
1098 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1102 cfs_duration_usec(cfs_time_sub(end, start), &val);
1103 return val.tv_sec * 1000000 + val.tv_usec;
1107 * Check whether the we deal with this handle for too long.
1109 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1110 cfs_time_t alloced, cfs_time_t started,
1113 cfs_time_t now = cfs_time_current();
1115 LASSERT(dev != NULL);
1117 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1118 interval_to_usec(alloced, started));
1119 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1120 interval_to_usec(started, closed));
1121 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1122 interval_to_usec(closed, now));
1124 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1125 CWARN("transaction handle %p was open for too long: "
1126 "now "CFS_TIME_T" ,"
1127 "alloced "CFS_TIME_T" ,"
1128 "started "CFS_TIME_T" ,"
1129 "closed "CFS_TIME_T"\n",
1130 oth, now, alloced, started, closed);
1131 libcfs_debug_dumpstack(NULL);
1135 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1137 cfs_time_t __closed = cfs_time_current(); \
1138 cfs_time_t __alloced = oth->oth_alloced; \
1139 cfs_time_t __started = oth->oth_started; \
1142 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1145 #else /* OSD_THANDLE_STATS */
1147 #define osd_th_alloced(h) do {} while(0)
1148 #define osd_th_started(h) do {} while(0)
1149 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1151 #endif /* OSD_THANDLE_STATS */
1154 * Concurrency: doesn't access mutable data.
1156 static int osd_param_is_not_sane(const struct osd_device *dev,
1157 const struct thandle *th)
1159 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1161 return oh->ot_credits > osd_transaction_size(dev);
1165 * Concurrency: shouldn't matter.
1167 static void osd_trans_commit_cb(struct super_block *sb,
1168 struct ldiskfs_journal_cb_entry *jcb, int error)
1170 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1171 struct thandle *th = &oh->ot_super;
1172 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1173 struct dt_txn_commit_cb *dcb, *tmp;
1175 LASSERT(oh->ot_handle == NULL);
1178 CERROR("transaction @0x%p commit error: %d\n", th, error);
1180 dt_txn_hook_commit(th);
1182 /* call per-transaction callbacks if any */
1183 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1185 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1186 "commit callback entry: magic=%x name='%s'\n",
1187 dcb->dcb_magic, dcb->dcb_name);
1188 list_del_init(&dcb->dcb_linkage);
1189 dcb->dcb_func(NULL, th, dcb, error);
1192 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1196 lu_context_exit(&th->th_ctx);
1197 lu_context_fini(&th->th_ctx);
1201 static struct thandle *osd_trans_create(const struct lu_env *env,
1202 struct dt_device *d)
1204 struct osd_thread_info *oti = osd_oti_get(env);
1205 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1206 struct osd_thandle *oh;
1210 /* on pending IO in this thread should left from prev. request */
1211 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1213 th = ERR_PTR(-ENOMEM);
1214 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1216 oh->ot_quota_trans = &oti->oti_quota_trans;
1217 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1221 th->th_tags = LCT_TX_HANDLE;
1223 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1224 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1227 memset(oti->oti_declare_ops, 0,
1228 sizeof(oti->oti_declare_ops));
1229 memset(oti->oti_declare_ops_cred, 0,
1230 sizeof(oti->oti_declare_ops_cred));
1231 memset(oti->oti_declare_ops_used, 0,
1232 sizeof(oti->oti_declare_ops_used));
1237 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1239 struct osd_thread_info *oti = osd_oti_get(env);
1240 struct osd_thandle *oh;
1242 oh = container_of0(th, struct osd_thandle, ot_super);
1243 LASSERT(oh != NULL);
1245 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1246 oti->oti_declare_ops[OSD_OT_CREATE],
1247 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1248 oti->oti_declare_ops_used[OSD_OT_CREATE],
1249 oti->oti_declare_ops[OSD_OT_DESTROY],
1250 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1251 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1252 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1253 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1254 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1255 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1256 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1257 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1258 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1259 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1260 oti->oti_declare_ops[OSD_OT_WRITE],
1261 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1262 oti->oti_declare_ops_used[OSD_OT_WRITE],
1263 oti->oti_declare_ops[OSD_OT_PUNCH],
1264 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1265 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1266 oti->oti_declare_ops[OSD_OT_QUOTA],
1267 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1268 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1269 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1270 oti->oti_declare_ops[OSD_OT_INSERT],
1271 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1272 oti->oti_declare_ops_used[OSD_OT_INSERT],
1273 oti->oti_declare_ops[OSD_OT_DELETE],
1274 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1275 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1276 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1277 oti->oti_declare_ops[OSD_OT_REF_ADD],
1278 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1279 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1280 oti->oti_declare_ops[OSD_OT_REF_DEL],
1281 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1282 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1286 * Concurrency: shouldn't matter.
1288 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1291 struct osd_thread_info *oti = osd_oti_get(env);
1292 struct osd_device *dev = osd_dt_dev(d);
1294 struct osd_thandle *oh;
1299 LASSERT(current->journal_info == NULL);
1301 oh = container_of0(th, struct osd_thandle, ot_super);
1302 LASSERT(oh != NULL);
1303 LASSERT(oh->ot_handle == NULL);
1305 rc = dt_txn_hook_start(env, d, th);
1309 if (unlikely(osd_param_is_not_sane(dev, th))) {
1310 static unsigned long last_printed;
1311 static int last_credits;
1313 CWARN("%.16s: too many transaction credits (%d > %d)\n",
1314 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
1316 osd_journal(dev)->j_max_transaction_buffers);
1318 osd_trans_dump_creds(env, th);
1320 if (last_credits != oh->ot_credits &&
1321 time_after(jiffies, last_printed +
1322 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
1323 libcfs_debug_dumpstack(NULL);
1324 last_credits = oh->ot_credits;
1325 last_printed = jiffies;
1327 /* XXX Limit the credits to 'max_transaction_buffers', and
1328 * let the underlying filesystem to catch the error if
1329 * we really need so many credits.
1331 * This should be removed when we can calculate the
1332 * credits precisely. */
1333 oh->ot_credits = osd_transaction_size(dev);
1337 * XXX temporary stuff. Some abstraction layer should
1340 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1344 LASSERT(oti->oti_txns == 0);
1345 lu_context_init(&th->th_ctx, th->th_tags);
1346 lu_context_enter(&th->th_ctx);
1348 lu_device_get(&d->dd_lu_dev);
1349 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1360 static int osd_seq_exists(const struct lu_env *env,
1361 struct osd_device *osd, u64 seq)
1363 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1364 struct seq_server_site *ss = osd_seq_site(osd);
1368 LASSERT(ss != NULL);
1369 LASSERT(ss->ss_server_fld != NULL);
1371 rc = osd_fld_lookup(env, osd, seq, range);
1374 CERROR("%s: can't lookup FLD sequence "LPX64
1375 ": rc = %d\n", osd_name(osd), seq, rc);
1379 RETURN(ss->ss_node_id == range->lsr_index);
1382 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1384 struct dt_txn_commit_cb *dcb;
1385 struct dt_txn_commit_cb *tmp;
1387 /* call per-transaction stop callbacks if any */
1388 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1390 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1391 "commit callback entry: magic=%x name='%s'\n",
1392 dcb->dcb_magic, dcb->dcb_name);
1393 list_del_init(&dcb->dcb_linkage);
1394 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1399 * Concurrency: shouldn't matter.
1401 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1404 int rc = 0, remove_agents = 0;
1405 struct osd_thandle *oh;
1406 struct osd_thread_info *oti = osd_oti_get(env);
1407 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1408 struct osd_device *osd = osd_dt_dev(th->th_dev);
1409 struct qsd_instance *qsd = osd->od_quota_slave;
1410 struct lquota_trans *qtrans;
1413 oh = container_of0(th, struct osd_thandle, ot_super);
1415 /* reset OI cache for safety */
1416 oti->oti_ins_cache_used = 0;
1418 remove_agents = oh->ot_remove_agents;
1420 qtrans = oh->ot_quota_trans;
1421 oh->ot_quota_trans = NULL;
1423 if (oh->ot_handle != NULL) {
1424 handle_t *hdl = oh->ot_handle;
1427 * add commit callback
1428 * notice we don't do this in osd_trans_start()
1429 * as underlying transaction can change during truncate
1431 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1434 LASSERT(oti->oti_txns == 1);
1437 rc = dt_txn_hook_stop(env, th);
1439 CERROR("%s: failed in transaction hook: rc = %d\n",
1442 osd_trans_stop_cb(oh, rc);
1443 /* hook functions might modify th_sync */
1444 hdl->h_sync = th->th_sync;
1446 oh->ot_handle = NULL;
1447 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1449 CERROR("%s: failed to stop transaction: rc = %d\n",
1452 osd_trans_stop_cb(oh, th->th_result);
1456 /* inform the quota slave device that the transaction is stopping */
1457 qsd_op_end(env, qsd, qtrans);
1459 /* as we want IO to journal and data IO be concurrent, we don't block
1460 * awaiting data IO completion in osd_do_bio(), instead we wait here
1461 * once transaction is submitted to the journal. all reqular requests
1462 * don't do direct IO (except read/write), thus this wait_event becomes
1465 * IMPORTANT: we have to wait till any IO submited by the thread is
1466 * completed otherwise iobuf may be corrupted by different request
1468 wait_event(iobuf->dr_wait,
1469 atomic_read(&iobuf->dr_numreqs) == 0);
1470 osd_fini_iobuf(osd, iobuf);
1472 rc = iobuf->dr_error;
1474 if (unlikely(remove_agents != 0))
1475 osd_process_scheduled_agent_removals(env, osd);
1480 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1482 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1485 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1486 LASSERT(&dcb->dcb_func != NULL);
1487 if (dcb->dcb_flags & DCB_TRANS_STOP)
1488 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1490 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1496 * Called just before object is freed. Releases all resources except for
1497 * object itself (that is released by osd_object_free()).
1499 * Concurrency: no concurrent access is possible that late in object
1502 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1504 struct osd_object *obj = osd_obj(l);
1505 struct inode *inode = obj->oo_inode;
1507 LINVRNT(osd_invariant(obj));
1510 * If object is unlinked remove fid->ino mapping from object index.
1513 osd_index_fini(obj);
1514 if (inode != NULL) {
1515 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1516 qid_t uid = i_uid_read(inode);
1517 qid_t gid = i_gid_read(inode);
1520 obj->oo_inode = NULL;
1523 struct osd_thread_info *info = osd_oti_get(env);
1524 struct lquota_id_info *qi = &info->oti_qi;
1526 /* Release granted quota to master if necessary */
1527 qi->lqi_id.qid_uid = uid;
1528 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1530 qi->lqi_id.qid_uid = gid;
1531 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1537 * Concurrency: ->loo_object_release() is called under site spin-lock.
1539 static void osd_object_release(const struct lu_env *env,
1540 struct lu_object *l)
1545 * Concurrency: shouldn't matter.
1547 static int osd_object_print(const struct lu_env *env, void *cookie,
1548 lu_printer_t p, const struct lu_object *l)
1550 struct osd_object *o = osd_obj(l);
1551 struct iam_descr *d;
1553 if (o->oo_dir != NULL)
1554 d = o->oo_dir->od_container.ic_descr;
1557 return (*p)(env, cookie,
1558 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1560 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1561 o->oo_inode ? o->oo_inode->i_generation : 0,
1562 d ? d->id_ops->id_name : "plain");
1565 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1568 * Concurrency: shouldn't matter.
1570 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1571 struct obd_statfs *sfs)
1573 struct osd_device *osd = osd_dt_dev(d);
1574 struct super_block *sb = osd_sb(osd);
1575 struct kstatfs *ksfs;
1578 if (unlikely(osd->od_mnt == NULL))
1579 return -EINPROGRESS;
1581 /* osd_lproc.c call this without env, allocate ksfs for that case */
1582 if (unlikely(env == NULL)) {
1583 OBD_ALLOC_PTR(ksfs);
1587 ksfs = &osd_oti_get(env)->oti_ksfs;
1590 spin_lock(&osd->od_osfs_lock);
1591 result = sb->s_op->statfs(sb->s_root, ksfs);
1592 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1593 statfs_pack(sfs, ksfs);
1594 if (unlikely(sb->s_flags & MS_RDONLY))
1595 sfs->os_state = OS_STATE_READONLY;
1596 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1597 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1598 sfs->os_maxbytes = sb->s_maxbytes;
1600 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1602 spin_unlock(&osd->od_osfs_lock);
1604 if (unlikely(env == NULL))
1607 /* Reserve a small amount of space for local objects like last_rcvd,
1608 * llog, quota files, ... */
1609 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1612 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1613 /** Take out metadata overhead for indirect blocks */
1614 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1621 * Estimate space needed for file creations. We assume the largest filename
1622 * which is 2^64 - 1, hence a filename of 20 chars.
1623 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1625 #ifdef __LDISKFS_DIR_REC_LEN
1626 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1628 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1632 * Concurrency: doesn't access mutable data.
1634 static void osd_conf_get(const struct lu_env *env,
1635 const struct dt_device *dev,
1636 struct dt_device_param *param)
1638 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1642 * XXX should be taken from not-yet-existing fs abstraction layer.
1644 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1645 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1646 param->ddp_block_shift = sb->s_blocksize_bits;
1647 param->ddp_mount_type = LDD_MT_LDISKFS;
1648 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1649 param->ddp_maxbytes = sb->s_maxbytes;
1651 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1652 /* Overhead estimate should be fairly accurate, so we really take a tiny
1653 * error margin which also avoids fragmenting the filesystem too much */
1654 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1655 /* inode are statically allocated, so per-inode space consumption
1656 * is the space consumed by the directory entry */
1657 param->ddp_inodespace = PER_OBJ_USAGE;
1658 /* per-fragment overhead to be used by the client code */
1659 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1660 param->ddp_mntopts = 0;
1661 if (test_opt(sb, XATTR_USER))
1662 param->ddp_mntopts |= MNTOPT_USERXATTR;
1663 if (test_opt(sb, POSIX_ACL))
1664 param->ddp_mntopts |= MNTOPT_ACL;
1666 /* LOD might calculate the max stripe count based on max_ea_size,
1667 * so we need take account in the overhead as well,
1668 * xattr_header + magic + xattr_entry_head */
1669 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1670 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1672 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1673 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1674 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1678 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1682 * Concurrency: shouldn't matter.
1684 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1688 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1690 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1692 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1693 LUSTRE_OSD_LDISKFS_NAME, rc);
1699 * Start commit for OSD device.
1701 * An implementation of dt_commit_async method for OSD device.
1702 * Asychronously starts underlayng fs sync and thereby a transaction
1705 * \param env environment
1706 * \param d dt device
1708 * \see dt_device_operations
1710 static int osd_commit_async(const struct lu_env *env,
1711 struct dt_device *d)
1713 struct super_block *s = osd_sb(osd_dt_dev(d));
1716 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1717 RETURN(s->s_op->sync_fs(s, 0));
1721 * Concurrency: shouldn't matter.
1724 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1726 struct super_block *sb = osd_sb(osd_dt_dev(d));
1727 struct block_device *dev = sb->s_bdev;
1728 #ifdef HAVE_DEV_SET_RDONLY
1729 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1732 int rc = -EOPNOTSUPP;
1736 #ifdef HAVE_DEV_SET_RDONLY
1737 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1739 if (jdev && (jdev != dev)) {
1740 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1742 dev_set_rdonly(jdev);
1744 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1745 dev_set_rdonly(dev);
1747 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1748 osd_dt_dev(d)->od_svname, (long)dev, rc);
1754 * Note: we do not count into QUOTA here.
1755 * If we mount with --data_journal we may need more.
1757 const int osd_dto_credits_noquota[DTO_NR] = {
1760 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1761 * SINGLEDATA_TRANS_BLOCKS(8)
1762 * XXX Note: maybe iam need more, since iam have more level than
1765 [DTO_INDEX_INSERT] = 16,
1768 * just modify a single entry, probably merge few within a block
1770 [DTO_INDEX_DELETE] = 1,
1774 [DTO_INDEX_UPDATE] = 16,
1776 * 4(inode, inode bits, groups, GDT)
1777 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1779 [DTO_OBJECT_CREATE] = 4,
1781 * 4(inode, inode bits, groups, GDT)
1782 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1784 [DTO_OBJECT_DELETE] = 4,
1786 * Attr set credits (inode)
1788 [DTO_ATTR_SET_BASE] = 1,
1790 * Xattr set. The same as xattr of EXT3.
1791 * DATA_TRANS_BLOCKS(14)
1792 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1793 * are also counted in. Do not know why?
1795 [DTO_XATTR_SET] = 14,
1797 * credits for inode change during write.
1799 [DTO_WRITE_BASE] = 3,
1801 * credits for single block write.
1803 [DTO_WRITE_BLOCK] = 14,
1805 * Attr set credits for chown.
1806 * This is extra credits for setattr, and it is null without quota
1808 [DTO_ATTR_SET_CHOWN] = 0
1811 static const struct dt_device_operations osd_dt_ops = {
1812 .dt_root_get = osd_root_get,
1813 .dt_statfs = osd_statfs,
1814 .dt_trans_create = osd_trans_create,
1815 .dt_trans_start = osd_trans_start,
1816 .dt_trans_stop = osd_trans_stop,
1817 .dt_trans_cb_add = osd_trans_cb_add,
1818 .dt_conf_get = osd_conf_get,
1819 .dt_sync = osd_sync,
1821 .dt_commit_async = osd_commit_async,
1824 static void osd_object_read_lock(const struct lu_env *env,
1825 struct dt_object *dt, unsigned role)
1827 struct osd_object *obj = osd_dt_obj(dt);
1828 struct osd_thread_info *oti = osd_oti_get(env);
1830 LINVRNT(osd_invariant(obj));
1832 LASSERT(obj->oo_owner != env);
1833 down_read_nested(&obj->oo_sem, role);
1835 LASSERT(obj->oo_owner == NULL);
1839 static void osd_object_write_lock(const struct lu_env *env,
1840 struct dt_object *dt, unsigned role)
1842 struct osd_object *obj = osd_dt_obj(dt);
1843 struct osd_thread_info *oti = osd_oti_get(env);
1845 LINVRNT(osd_invariant(obj));
1847 LASSERT(obj->oo_owner != env);
1848 down_write_nested(&obj->oo_sem, role);
1850 LASSERT(obj->oo_owner == NULL);
1851 obj->oo_owner = env;
1855 static void osd_object_read_unlock(const struct lu_env *env,
1856 struct dt_object *dt)
1858 struct osd_object *obj = osd_dt_obj(dt);
1859 struct osd_thread_info *oti = osd_oti_get(env);
1861 LINVRNT(osd_invariant(obj));
1863 LASSERT(oti->oti_r_locks > 0);
1865 up_read(&obj->oo_sem);
1868 static void osd_object_write_unlock(const struct lu_env *env,
1869 struct dt_object *dt)
1871 struct osd_object *obj = osd_dt_obj(dt);
1872 struct osd_thread_info *oti = osd_oti_get(env);
1874 LINVRNT(osd_invariant(obj));
1876 LASSERT(obj->oo_owner == env);
1877 LASSERT(oti->oti_w_locks > 0);
1879 obj->oo_owner = NULL;
1880 up_write(&obj->oo_sem);
1883 static int osd_object_write_locked(const struct lu_env *env,
1884 struct dt_object *dt)
1886 struct osd_object *obj = osd_dt_obj(dt);
1888 LINVRNT(osd_invariant(obj));
1890 return obj->oo_owner == env;
1893 static struct timespec *osd_inode_time(const struct lu_env *env,
1894 struct inode *inode, __u64 seconds)
1896 struct osd_thread_info *oti = osd_oti_get(env);
1897 struct timespec *t = &oti->oti_time;
1899 t->tv_sec = seconds;
1901 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1906 static void osd_inode_getattr(const struct lu_env *env,
1907 struct inode *inode, struct lu_attr *attr)
1909 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1910 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1911 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1914 attr->la_atime = LTIME_S(inode->i_atime);
1915 attr->la_mtime = LTIME_S(inode->i_mtime);
1916 attr->la_ctime = LTIME_S(inode->i_ctime);
1917 attr->la_mode = inode->i_mode;
1918 attr->la_size = i_size_read(inode);
1919 attr->la_blocks = inode->i_blocks;
1920 attr->la_uid = i_uid_read(inode);
1921 attr->la_gid = i_gid_read(inode);
1922 attr->la_flags = LDISKFS_I(inode)->i_flags;
1923 attr->la_nlink = inode->i_nlink;
1924 attr->la_rdev = inode->i_rdev;
1925 attr->la_blksize = 1 << inode->i_blkbits;
1926 attr->la_blkbits = inode->i_blkbits;
1929 static int osd_attr_get(const struct lu_env *env,
1930 struct dt_object *dt,
1931 struct lu_attr *attr)
1933 struct osd_object *obj = osd_dt_obj(dt);
1935 if (!dt_object_exists(dt))
1938 LASSERT(!dt_object_remote(dt));
1939 LINVRNT(osd_invariant(obj));
1941 spin_lock(&obj->oo_guard);
1942 osd_inode_getattr(env, obj->oo_inode, attr);
1943 spin_unlock(&obj->oo_guard);
1947 static int osd_declare_attr_set(const struct lu_env *env,
1948 struct dt_object *dt,
1949 const struct lu_attr *attr,
1950 struct thandle *handle)
1952 struct osd_thandle *oh;
1953 struct osd_object *obj;
1954 struct osd_thread_info *info = osd_oti_get(env);
1955 struct lquota_id_info *qi = &info->oti_qi;
1963 LASSERT(dt != NULL);
1964 LASSERT(handle != NULL);
1966 obj = osd_dt_obj(dt);
1967 LASSERT(osd_invariant(obj));
1969 oh = container_of0(handle, struct osd_thandle, ot_super);
1970 LASSERT(oh->ot_handle == NULL);
1972 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1973 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1975 if (attr == NULL || obj->oo_inode == NULL)
1978 bspace = obj->oo_inode->i_blocks;
1979 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1980 bspace = toqb(bspace);
1982 /* Changing ownership is always preformed by super user, it should not
1985 * We still need to call the osd_declare_qid() to calculate the journal
1986 * credits for updating quota accounting files and to trigger quota
1987 * space adjustment once the operation is completed.*/
1988 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
1990 uid = i_uid_read(obj->oo_inode);
1991 qi->lqi_type = USRQUOTA;
1992 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
1993 /* inode accounting */
1994 qi->lqi_is_blk = false;
1996 /* one more inode for the new uid ... */
1997 qi->lqi_id.qid_uid = attr->la_uid;
1999 /* Reserve credits for the new uid */
2000 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2001 if (rc == -EDQUOT || rc == -EINPROGRESS)
2006 /* and one less inode for the current uid */
2007 qi->lqi_id.qid_uid = uid;
2009 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2010 if (rc == -EDQUOT || rc == -EINPROGRESS)
2015 /* block accounting */
2016 qi->lqi_is_blk = true;
2018 /* more blocks for the new uid ... */
2019 qi->lqi_id.qid_uid = attr->la_uid;
2020 qi->lqi_space = bspace;
2022 * Credits for the new uid has been reserved, re-use "obj"
2023 * to save credit reservation.
2025 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2026 if (rc == -EDQUOT || rc == -EINPROGRESS)
2031 /* and finally less blocks for the current uid */
2032 qi->lqi_id.qid_uid = uid;
2033 qi->lqi_space = -bspace;
2034 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2035 if (rc == -EDQUOT || rc == -EINPROGRESS)
2041 gid = i_gid_read(obj->oo_inode);
2042 qi->lqi_type = GRPQUOTA;
2043 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2045 /* inode accounting */
2046 qi->lqi_is_blk = false;
2048 /* one more inode for the new gid ... */
2049 qi->lqi_id.qid_gid = attr->la_gid;
2051 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2052 if (rc == -EDQUOT || rc == -EINPROGRESS)
2057 /* and one less inode for the current gid */
2058 qi->lqi_id.qid_gid = gid;
2060 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2061 if (rc == -EDQUOT || rc == -EINPROGRESS)
2066 /* block accounting */
2067 qi->lqi_is_blk = true;
2069 /* more blocks for the new gid ... */
2070 qi->lqi_id.qid_gid = attr->la_gid;
2071 qi->lqi_space = bspace;
2072 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2073 if (rc == -EDQUOT || rc == -EINPROGRESS)
2078 /* and finally less blocks for the current gid */
2079 qi->lqi_id.qid_gid = gid;
2080 qi->lqi_space = -bspace;
2081 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2082 if (rc == -EDQUOT || rc == -EINPROGRESS)
2091 static int osd_inode_setattr(const struct lu_env *env,
2092 struct inode *inode, const struct lu_attr *attr)
2094 __u64 bits = attr->la_valid;
2096 /* Only allow set size for regular file */
2097 if (!S_ISREG(inode->i_mode))
2098 bits &= ~(LA_SIZE | LA_BLOCKS);
2103 if (bits & LA_ATIME)
2104 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2105 if (bits & LA_CTIME)
2106 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2107 if (bits & LA_MTIME)
2108 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2109 if (bits & LA_SIZE) {
2110 LDISKFS_I(inode)->i_disksize = attr->la_size;
2111 i_size_write(inode, attr->la_size);
2115 /* OSD should not change "i_blocks" which is used by quota.
2116 * "i_blocks" should be changed by ldiskfs only. */
2117 if (bits & LA_BLOCKS)
2118 inode->i_blocks = attr->la_blocks;
2121 inode->i_mode = (inode->i_mode & S_IFMT) |
2122 (attr->la_mode & ~S_IFMT);
2124 i_uid_write(inode, attr->la_uid);
2126 i_gid_write(inode, attr->la_gid);
2127 if (bits & LA_NLINK)
2128 set_nlink(inode, attr->la_nlink);
2130 inode->i_rdev = attr->la_rdev;
2132 if (bits & LA_FLAGS) {
2133 /* always keep S_NOCMTIME */
2134 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2140 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2142 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2143 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2147 ll_vfs_dq_init(inode);
2149 if (attr->la_valid & LA_UID)
2150 iattr.ia_valid |= ATTR_UID;
2151 if (attr->la_valid & LA_GID)
2152 iattr.ia_valid |= ATTR_GID;
2153 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2154 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2156 rc = ll_vfs_dq_transfer(inode, &iattr);
2158 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2159 "enforcement enabled on the ldiskfs "
2160 "filesystem?\n", inode->i_sb->s_id, rc);
2167 static int osd_attr_set(const struct lu_env *env,
2168 struct dt_object *dt,
2169 const struct lu_attr *attr,
2170 struct thandle *handle)
2172 struct osd_object *obj = osd_dt_obj(dt);
2173 struct inode *inode;
2176 if (!dt_object_exists(dt))
2179 LASSERT(handle != NULL);
2180 LASSERT(!dt_object_remote(dt));
2181 LASSERT(osd_invariant(obj));
2183 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2185 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2186 struct osd_thread_info *oti = osd_oti_get(env);
2187 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2188 struct lu_fid *fid1 = &oti->oti_fid;
2189 struct osd_inode_id *id = &oti->oti_id;
2190 struct iam_path_descr *ipd;
2191 struct iam_container *bag;
2192 struct osd_thandle *oh;
2195 fid_cpu_to_be(fid1, fid0);
2196 memset(id, 1, sizeof(*id));
2197 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2198 fid0)->oi_dir.od_container;
2199 ipd = osd_idx_ipd_get(env, bag);
2200 if (unlikely(ipd == NULL))
2203 oh = container_of0(handle, struct osd_thandle, ot_super);
2204 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2205 (const struct iam_rec *)id, ipd);
2206 osd_ipd_put(env, bag, ipd);
2207 return(rc > 0 ? 0 : rc);
2210 inode = obj->oo_inode;
2212 rc = osd_quota_transfer(inode, attr);
2216 spin_lock(&obj->oo_guard);
2217 rc = osd_inode_setattr(env, inode, attr);
2218 spin_unlock(&obj->oo_guard);
2221 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2223 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2228 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2229 struct osd_object *obj,
2230 const char *name, const int namelen)
2232 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2235 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2236 umode_t mode, struct dt_allocation_hint *hint,
2240 struct osd_device *osd = osd_obj2dev(obj);
2241 struct osd_thandle *oth;
2242 struct dt_object *parent = NULL;
2243 struct inode *inode;
2245 LINVRNT(osd_invariant(obj));
2246 LASSERT(obj->oo_inode == NULL);
2247 LASSERT(obj->oo_hl_head == NULL);
2249 if (S_ISDIR(mode) && ldiskfs_pdo) {
2250 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2251 if (obj->oo_hl_head == NULL)
2255 oth = container_of(th, struct osd_thandle, ot_super);
2256 LASSERT(oth->ot_handle->h_transaction != NULL);
2258 if (hint != NULL && hint->dah_parent != NULL &&
2259 !dt_object_remote(hint->dah_parent))
2260 parent = hint->dah_parent;
2262 inode = ldiskfs_create_inode(oth->ot_handle,
2263 parent ? osd_dt_obj(parent)->oo_inode :
2264 osd_sb(osd)->s_root->d_inode,
2266 if (!IS_ERR(inode)) {
2267 /* Do not update file c/mtime in ldiskfs. */
2268 inode->i_flags |= S_NOCMTIME;
2270 /* For new created object, it must be consistent,
2271 * and it is unnecessary to scrub against it. */
2272 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2274 obj->oo_inode = inode;
2277 if (obj->oo_hl_head != NULL) {
2278 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2279 obj->oo_hl_head = NULL;
2281 result = PTR_ERR(inode);
2283 LINVRNT(osd_invariant(obj));
2291 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2292 struct lu_attr *attr,
2293 struct dt_allocation_hint *hint,
2294 struct dt_object_format *dof,
2298 struct osd_thandle *oth;
2299 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2301 LASSERT(S_ISDIR(attr->la_mode));
2303 oth = container_of(th, struct osd_thandle, ot_super);
2304 LASSERT(oth->ot_handle->h_transaction != NULL);
2305 result = osd_mkfile(info, obj, mode, hint, th);
2310 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2311 struct lu_attr *attr,
2312 struct dt_allocation_hint *hint,
2313 struct dt_object_format *dof,
2317 struct osd_thandle *oth;
2318 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2320 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2322 LASSERT(S_ISREG(attr->la_mode));
2324 oth = container_of(th, struct osd_thandle, ot_super);
2325 LASSERT(oth->ot_handle->h_transaction != NULL);
2327 result = osd_mkfile(info, obj, mode, hint, th);
2329 LASSERT(obj->oo_inode != NULL);
2330 if (feat->dif_flags & DT_IND_VARKEY)
2331 result = iam_lvar_create(obj->oo_inode,
2332 feat->dif_keysize_max,
2334 feat->dif_recsize_max,
2337 result = iam_lfix_create(obj->oo_inode,
2338 feat->dif_keysize_max,
2340 feat->dif_recsize_max,
2347 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2348 struct lu_attr *attr,
2349 struct dt_allocation_hint *hint,
2350 struct dt_object_format *dof,
2353 LASSERT(S_ISREG(attr->la_mode));
2354 return osd_mkfile(info, obj, (attr->la_mode &
2355 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2358 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2359 struct lu_attr *attr,
2360 struct dt_allocation_hint *hint,
2361 struct dt_object_format *dof,
2364 LASSERT(S_ISLNK(attr->la_mode));
2365 return osd_mkfile(info, obj, (attr->la_mode &
2366 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2369 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2370 struct lu_attr *attr,
2371 struct dt_allocation_hint *hint,
2372 struct dt_object_format *dof,
2375 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2378 LINVRNT(osd_invariant(obj));
2379 LASSERT(obj->oo_inode == NULL);
2380 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2381 S_ISFIFO(mode) || S_ISSOCK(mode));
2383 result = osd_mkfile(info, obj, mode, hint, th);
2385 LASSERT(obj->oo_inode != NULL);
2387 * This inode should be marked dirty for i_rdev. Currently
2388 * that is done in the osd_attr_init().
2390 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2393 LINVRNT(osd_invariant(obj));
2397 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2399 struct dt_allocation_hint *hint,
2400 struct dt_object_format *dof,
2403 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2405 osd_obj_type_f result;
2421 result = osd_mk_index;
2432 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2433 struct dt_object *parent, struct dt_object *child,
2438 ah->dah_parent = parent;
2439 ah->dah_mode = child_mode;
2441 if (parent != NULL && !dt_object_remote(parent)) {
2442 /* will help to find FID->ino at dt_insert("..") */
2443 struct osd_object *pobj = osd_dt_obj(parent);
2444 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2448 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2449 struct lu_attr *attr, struct dt_object_format *dof)
2451 struct inode *inode = obj->oo_inode;
2452 __u64 valid = attr->la_valid;
2455 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2457 if (dof->dof_type != DFT_NODE)
2458 attr->la_valid &= ~LA_RDEV;
2459 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2460 attr->la_valid &= ~LA_ATIME;
2461 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2462 attr->la_valid &= ~LA_CTIME;
2463 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2464 attr->la_valid &= ~LA_MTIME;
2466 result = osd_quota_transfer(inode, attr);
2470 if (attr->la_valid != 0) {
2471 result = osd_inode_setattr(info->oti_env, inode, attr);
2473 * The osd_inode_setattr() should always succeed here. The
2474 * only error that could be returned is EDQUOT when we are
2475 * trying to change the UID or GID of the inode. However, this
2476 * should not happen since quota enforcement is no longer
2477 * enabled on ldiskfs (lquota takes care of it).
2479 LASSERTF(result == 0, "%d\n", result);
2480 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2483 attr->la_valid = valid;
2487 * Helper function for osd_object_create()
2489 * \retval 0, on success
2491 static int __osd_object_create(struct osd_thread_info *info,
2492 struct osd_object *obj, struct lu_attr *attr,
2493 struct dt_allocation_hint *hint,
2494 struct dt_object_format *dof,
2500 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2502 /* we drop umask so that permissions we pass are not affected */
2503 umask = current->fs->umask;
2504 current->fs->umask = 0;
2506 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2508 if (likely(obj->oo_inode != NULL)) {
2509 LASSERT(obj->oo_inode->i_state & I_NEW);
2511 /* Unlock the inode before attr initialization to avoid
2512 * unnecessary dqget operations. LU-6378 */
2513 unlock_new_inode(obj->oo_inode);
2516 if (likely(result == 0)) {
2517 osd_attr_init(info, obj, attr, dof);
2518 osd_object_init0(obj);
2521 /* restore previous umask value */
2522 current->fs->umask = umask;
2524 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2530 * Helper function for osd_object_create()
2532 * \retval 0, on success
2534 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2535 const struct lu_fid *fid, struct thandle *th)
2537 struct osd_thread_info *info = osd_oti_get(env);
2538 struct osd_inode_id *id = &info->oti_id;
2539 struct osd_device *osd = osd_obj2dev(obj);
2540 struct osd_thandle *oh;
2543 LASSERT(obj->oo_inode != NULL);
2545 oh = container_of0(th, struct osd_thandle, ot_super);
2546 LASSERT(oh->ot_handle);
2547 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2549 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2550 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2551 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2556 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2557 u64 seq, struct lu_seq_range *range)
2559 struct seq_server_site *ss = osd_seq_site(osd);
2561 if (fid_seq_is_idif(seq)) {
2562 fld_range_set_ost(range);
2563 range->lsr_index = idif_ost_idx(seq);
2567 if (!fid_seq_in_fldb(seq)) {
2568 fld_range_set_mdt(range);
2570 /* FIXME: If ss is NULL, it suppose not get lsr_index
2572 range->lsr_index = ss->ss_node_id;
2576 LASSERT(ss != NULL);
2577 fld_range_set_any(range);
2578 /* OSD will only do local fld lookup */
2579 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2583 * Concurrency: no external locking is necessary.
2585 static int osd_declare_object_create(const struct lu_env *env,
2586 struct dt_object *dt,
2587 struct lu_attr *attr,
2588 struct dt_allocation_hint *hint,
2589 struct dt_object_format *dof,
2590 struct thandle *handle)
2592 struct osd_thandle *oh;
2596 LASSERT(handle != NULL);
2598 oh = container_of0(handle, struct osd_thandle, ot_super);
2599 LASSERT(oh->ot_handle == NULL);
2601 /* EA object consumes more credits than regular object: osd_mk_index
2602 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2603 * leaf_node, could involves the block, block bitmap, groups, GDT
2604 * change for each block, so add 4 * 2 credits in that case. */
2605 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2606 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2607 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2608 /* Reuse idle OI block may cause additional one OI block
2610 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2611 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2616 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2617 osd_dt_obj(dt), false, NULL, false);
2621 /* will help to find FID->ino mapping at dt_insert() */
2622 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
2628 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2629 struct lu_attr *attr,
2630 struct dt_allocation_hint *hint,
2631 struct dt_object_format *dof, struct thandle *th)
2633 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2634 struct osd_object *obj = osd_dt_obj(dt);
2635 struct osd_thread_info *info = osd_oti_get(env);
2639 if (dt_object_exists(dt))
2642 LINVRNT(osd_invariant(obj));
2643 LASSERT(!dt_object_remote(dt));
2644 LASSERT(osd_write_locked(env, obj));
2645 LASSERT(th != NULL);
2647 if (unlikely(fid_is_acct(fid)))
2648 /* Quota files can't be created from the kernel any more,
2649 * 'tune2fs -O quota' will take care of creating them */
2652 result = __osd_object_create(info, obj, attr, hint, dof, th);
2654 result = __osd_oi_insert(env, obj, fid, th);
2655 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2656 obj->oo_dt.do_body_ops = &osd_body_ops;
2658 LASSERT(ergo(result == 0,
2659 dt_object_exists(dt) && !dt_object_remote(dt)));
2661 LASSERT(osd_invariant(obj));
2666 * Called to destroy on-disk representation of the object
2668 * Concurrency: must be locked
2670 static int osd_declare_object_destroy(const struct lu_env *env,
2671 struct dt_object *dt,
2674 struct osd_object *obj = osd_dt_obj(dt);
2675 struct inode *inode = obj->oo_inode;
2676 struct osd_thandle *oh;
2683 oh = container_of0(th, struct osd_thandle, ot_super);
2684 LASSERT(oh->ot_handle == NULL);
2686 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2687 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2688 /* Recycle idle OI leaf may cause additional three OI blocks
2690 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2691 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2692 /* one less inode */
2693 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2694 -1, oh, obj, false, NULL, false);
2697 /* data to be truncated */
2698 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2699 0, oh, obj, true, NULL, false);
2703 /* will help to find FID->ino when this object is being
2704 * added to PENDING/ */
2705 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
2710 static int osd_object_destroy(const struct lu_env *env,
2711 struct dt_object *dt,
2714 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2715 struct osd_object *obj = osd_dt_obj(dt);
2716 struct inode *inode = obj->oo_inode;
2717 struct osd_device *osd = osd_obj2dev(obj);
2718 struct osd_thandle *oh;
2722 oh = container_of0(th, struct osd_thandle, ot_super);
2723 LASSERT(oh->ot_handle);
2725 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2727 if (unlikely(fid_is_acct(fid)))
2730 if (S_ISDIR(inode->i_mode)) {
2731 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2732 inode->i_nlink == 2);
2733 /* it will check/delete the inode from remote parent,
2734 * how to optimize it? unlink performance impaction XXX */
2735 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2736 if (result != 0 && result != -ENOENT) {
2737 CERROR("%s: delete inode "DFID": rc = %d\n",
2738 osd_name(osd), PFID(fid), result);
2740 spin_lock(&obj->oo_guard);
2742 spin_unlock(&obj->oo_guard);
2743 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2746 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2748 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2749 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2752 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2753 /* XXX: add to ext3 orphan list */
2754 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2756 /* not needed in the cache anymore */
2757 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2758 obj->oo_destroyed = 1;
2764 * Put the fid into lustre_mdt_attrs, and then place the structure
2765 * inode's ea. This fid should not be altered during the life time
2768 * \retval +ve, on success
2769 * \retval -ve, on error
2771 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2773 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2774 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2776 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2780 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2783 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2786 lustre_lma_init(lma, fid, compat, incompat);
2787 lustre_lma_swab(lma);
2789 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2791 /* LMA may already exist, but we need to check that all the
2792 * desired compat/incompat flags have been added. */
2793 if (unlikely(rc == -EEXIST)) {
2794 if (compat == 0 && incompat == 0)
2797 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2798 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2803 lustre_lma_swab(lma);
2804 if (!(~lma->lma_compat & compat) &&
2805 !(~lma->lma_incompat & incompat))
2808 lma->lma_compat |= compat;
2809 lma->lma_incompat |= incompat;
2810 lustre_lma_swab(lma);
2811 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2812 sizeof(*lma), XATTR_REPLACE);
2819 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2820 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2821 * To have compatilibility with 1.8 ldiskfs driver we need to have
2822 * magic number at start of fid data.
2823 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2826 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2827 const struct lu_fid *fid)
2829 if (!fid_is_namespace_visible(fid) ||
2830 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2831 param->edp_magic = 0;
2835 param->edp_magic = LDISKFS_LUFID_MAGIC;
2836 param->edp_len = sizeof(struct lu_fid) + 1;
2837 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2841 * Try to read the fid from inode ea into dt_rec.
2843 * \param fid object fid.
2845 * \retval 0 on success
2847 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2848 __u32 ino, struct lu_fid *fid,
2849 struct osd_inode_id *id)
2851 struct osd_thread_info *info = osd_oti_get(env);
2852 struct inode *inode;
2855 osd_id_gen(id, ino, OSD_OII_NOGEN);
2856 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2858 RETURN(PTR_ERR(inode));
2864 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2866 struct inode *parent_dir,
2867 const struct lu_fid *dot_fid,
2868 const struct lu_fid *dot_dot_fid,
2869 struct osd_thandle *oth)
2871 struct ldiskfs_dentry_param *dot_ldp;
2872 struct ldiskfs_dentry_param *dot_dot_ldp;
2873 __u32 saved_nlink = dir->i_nlink;
2876 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2877 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2879 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2880 dot_ldp->edp_magic = 0;
2882 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2883 dir, dot_ldp, dot_dot_ldp);
2884 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2885 * the subseqent ref_add() will increase the dir->i_nlink
2886 * as 3. That is incorrect for new created directory.
2888 * It looks like hack, because we want to make the OSD API
2889 * to be order-independent for new created directory object
2890 * between dt_insert(..) and ref_add() operations.
2892 * Here, we only restore the in-RAM dir-inode's nlink attr,
2893 * becuase if the nlink attr is not 2, then there will be
2894 * ref_add() called following the dt_insert(..), such call
2895 * will make both the in-RAM and on-disk dir-inode's nlink
2896 * attr to be set as 2. LU-7447 */
2897 set_nlink(dir, saved_nlink);
2902 * Create an local agent inode for remote entry
2904 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2905 struct osd_device *osd,
2906 struct osd_object *pobj,
2907 const struct lu_fid *fid,
2911 struct osd_thread_info *info = osd_oti_get(env);
2912 struct inode *local;
2913 struct osd_thandle *oh;
2918 oh = container_of(th, struct osd_thandle, ot_super);
2919 LASSERT(oh->ot_handle->h_transaction != NULL);
2921 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2922 if (IS_ERR(local)) {
2923 CERROR("%s: create local error %d\n", osd_name(osd),
2924 (int)PTR_ERR(local));
2928 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2929 unlock_new_inode(local);
2931 /* Set special LMA flag for local agent inode */
2932 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2934 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2935 osd_name(osd), PFID(fid), rc);
2936 RETURN(ERR_PTR(rc));
2942 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2943 lu_object_fid(&pobj->oo_dt.do_lu),
2946 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2947 osd_name(osd), PFID(fid), rc);
2948 RETURN(ERR_PTR(rc));
2955 * when direntry is deleted, we have to take care of possible agent inode
2956 * referenced by that. unfortunately we can't do this at that point:
2957 * iget() within a running transaction leads to deadlock and we better do
2958 * not call that every delete declaration to save performance. so we put
2959 * a potention agent inode on a list and process that once the transaction
2960 * is over. Notice it's not any worse in terms of real orphans as regular
2961 * object destroy doesn't put inodes on the on-disk orphan list. this should
2962 * be addressed separately
2964 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
2965 struct osd_thandle *oh,
2968 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
2969 struct osd_obj_orphan *oor;
2976 oor->oor_env = (struct lu_env *)env;
2977 spin_lock(&osd->od_osfs_lock);
2978 list_add(&oor->oor_list, &osd->od_orphan_list);
2979 spin_unlock(&osd->od_osfs_lock);
2981 oh->ot_remove_agents = 1;
2987 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
2988 struct osd_device *osd)
2990 struct osd_thread_info *info = osd_oti_get(env);
2991 struct osd_obj_orphan *oor, *tmp;
2992 struct osd_inode_id id;
2993 struct list_head list;
2994 struct inode *inode;
2999 INIT_LIST_HEAD(&list);
3001 spin_lock(&osd->od_osfs_lock);
3002 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3003 if (oor->oor_env == env) {
3004 list_del(&oor->oor_list);
3005 list_add(&oor->oor_list, &list);
3008 spin_unlock(&osd->od_osfs_lock);
3010 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3014 list_del(&oor->oor_list);
3017 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3018 inode = osd_iget_fid(info, osd, &id, &fid);
3022 if (!osd_remote_fid(env, osd, &fid)) {
3027 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3029 mark_inode_dirty(inode);
3030 ldiskfs_journal_stop(jh);
3038 * OSD layer object create function for interoperability mode (b11826).
3039 * This is mostly similar to osd_object_create(). Only difference being, fid is
3040 * inserted into inode ea here.
3042 * \retval 0, on success
3043 * \retval -ve, on error
3045 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3046 struct lu_attr *attr,
3047 struct dt_allocation_hint *hint,
3048 struct dt_object_format *dof,
3051 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3052 struct osd_object *obj = osd_dt_obj(dt);
3053 struct osd_thread_info *info = osd_oti_get(env);
3054 int result, on_ost = 0;
3058 if (dt_object_exists(dt))
3061 LASSERT(osd_invariant(obj));
3062 LASSERT(!dt_object_remote(dt));
3063 LASSERT(osd_write_locked(env, obj));
3064 LASSERT(th != NULL);
3066 if (unlikely(fid_is_acct(fid)))
3067 /* Quota files can't be created from the kernel any more,
3068 * 'tune2fs -O quota' will take care of creating them */
3071 result = __osd_object_create(info, obj, attr, hint, dof, th);
3073 if (fid_is_idif(fid) &&
3074 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3075 struct lu_fid *tfid = &info->oti_fid;
3076 struct ost_id *oi = &info->oti_ostid;
3078 fid_to_ostid(fid, oi);
3079 ostid_to_fid(tfid, oi, 0);
3081 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3082 LMAC_FID_ON_OST, 0);
3084 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3086 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3087 on_ost ? LMAC_FID_ON_OST : 0,
3090 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3091 obj->oo_dt.do_body_ops = &osd_body_ops;
3095 result = __osd_oi_insert(env, obj, fid, th);
3097 /* a small optimization - dt_insert() isn't usually applied
3098 * to OST objects, so we don't need to cache OI mapping for
3100 if (result == 0 && on_ost == 0) {
3101 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3102 result = osd_idc_find_and_init(env, osd, obj);
3103 LASSERT(result == 0);
3106 LASSERT(ergo(result == 0,
3107 dt_object_exists(dt) && !dt_object_remote(dt)));
3108 LINVRNT(osd_invariant(obj));
3112 static int osd_declare_object_ref_add(const struct lu_env *env,
3113 struct dt_object *dt,
3114 struct thandle *handle)
3116 struct osd_thandle *oh;
3118 /* it's possible that object doesn't exist yet */
3119 LASSERT(handle != NULL);
3121 oh = container_of0(handle, struct osd_thandle, ot_super);
3122 LASSERT(oh->ot_handle == NULL);
3124 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3125 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3131 * Concurrency: @dt is write locked.
3133 static int osd_object_ref_add(const struct lu_env *env,
3134 struct dt_object *dt, struct thandle *th)
3136 struct osd_object *obj = osd_dt_obj(dt);
3137 struct inode *inode = obj->oo_inode;
3138 struct osd_thandle *oh;
3141 if (!dt_object_exists(dt))
3144 LINVRNT(osd_invariant(obj));
3145 LASSERT(!dt_object_remote(dt));
3146 LASSERT(osd_write_locked(env, obj));
3147 LASSERT(th != NULL);
3149 oh = container_of0(th, struct osd_thandle, ot_super);
3150 LASSERT(oh->ot_handle != NULL);
3152 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3154 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3155 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3157 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3158 * (65000) subdirectories by storing "1" in i_nlink if the link count
3159 * would otherwise overflow. Directory tranversal tools understand
3160 * that (st_nlink == 1) indicates that the filesystem dose not track
3161 * hard links count on the directory, and will not abort subdirectory
3162 * scanning early once (st_nlink - 2) subdirs have been found.
3164 * This also has to properly handle the case of inodes with nlink == 0
3165 * in case they are being linked into the PENDING directory
3167 spin_lock(&obj->oo_guard);
3168 if (unlikely(inode->i_nlink == 0))
3169 /* inc_nlink from 0 may cause WARN_ON */
3170 set_nlink(inode, 1);
3172 ldiskfs_inc_count(oh->ot_handle, inode);
3173 if (!S_ISDIR(inode->i_mode))
3174 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3176 spin_unlock(&obj->oo_guard);
3178 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3179 LINVRNT(osd_invariant(obj));
3181 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3186 static int osd_declare_object_ref_del(const struct lu_env *env,
3187 struct dt_object *dt,
3188 struct thandle *handle)
3190 struct osd_thandle *oh;
3192 if (!dt_object_exists(dt))
3195 LASSERT(!dt_object_remote(dt));
3196 LASSERT(handle != NULL);
3198 oh = container_of0(handle, struct osd_thandle, ot_super);
3199 LASSERT(oh->ot_handle == NULL);
3201 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3202 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3208 * Concurrency: @dt is write locked.
3210 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3213 struct osd_object *obj = osd_dt_obj(dt);
3214 struct inode *inode = obj->oo_inode;
3215 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3216 struct osd_thandle *oh;
3218 if (!dt_object_exists(dt))
3221 LINVRNT(osd_invariant(obj));
3222 LASSERT(!dt_object_remote(dt));
3223 LASSERT(osd_write_locked(env, obj));
3224 LASSERT(th != NULL);
3226 oh = container_of0(th, struct osd_thandle, ot_super);
3227 LASSERT(oh->ot_handle != NULL);
3229 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3231 spin_lock(&obj->oo_guard);
3232 /* That can be result of upgrade from old Lustre version and
3233 * applied only to local files. Just skip this ref_del call.
3234 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3235 if (inode->i_nlink == 0) {
3236 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3237 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3238 ", maybe an upgraded file? (LU-3915)\n",
3239 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3240 spin_unlock(&obj->oo_guard);
3244 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3245 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3247 ldiskfs_dec_count(oh->ot_handle, inode);
3248 spin_unlock(&obj->oo_guard);
3250 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3251 LINVRNT(osd_invariant(obj));
3253 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3259 * Get the 64-bit version for an inode.
3261 static int osd_object_version_get(const struct lu_env *env,
3262 struct dt_object *dt, dt_obj_version_t *ver)
3264 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3266 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3267 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3268 *ver = LDISKFS_I(inode)->i_fs_version;
3273 * Concurrency: @dt is read locked.
3275 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3276 struct lu_buf *buf, const char *name)
3278 struct osd_object *obj = osd_dt_obj(dt);
3279 struct inode *inode = obj->oo_inode;
3280 struct osd_thread_info *info = osd_oti_get(env);
3281 struct dentry *dentry = &info->oti_obj_dentry;
3283 /* version get is not real XATTR but uses xattr API */
3284 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3285 /* for version we are just using xattr API but change inode
3287 if (buf->lb_len == 0)
3288 return sizeof(dt_obj_version_t);
3290 if (buf->lb_len < sizeof(dt_obj_version_t))
3293 osd_object_version_get(env, dt, buf->lb_buf);
3295 return sizeof(dt_obj_version_t);
3298 if (!dt_object_exists(dt))
3301 LASSERT(!dt_object_remote(dt));
3302 LASSERT(inode->i_op != NULL);
3303 LASSERT(inode->i_op->getxattr != NULL);
3305 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3309 static int osd_declare_xattr_set(const struct lu_env *env,
3310 struct dt_object *dt,
3311 const struct lu_buf *buf, const char *name,
3312 int fl, struct thandle *handle)
3314 struct osd_thandle *oh;
3316 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3318 LASSERT(handle != NULL);
3320 oh = container_of0(handle, struct osd_thandle, ot_super);
3321 LASSERT(oh->ot_handle == NULL);
3323 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3324 /* For non-upgrading case, the LMA is set first and
3325 * usually fit inode. But for upgrade case, the LMA
3326 * may be in another separated EA block. */
3327 if (!dt_object_exists(dt))
3329 else if (fl == LU_XATTR_REPLACE)
3333 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3337 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3342 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3343 /* learn xattr size from osd_xattr_get if
3344 attribute has not been read yet */
3345 buflen = __osd_xattr_get(
3346 osd_dt_obj(dt)->oo_inode,
3347 &osd_oti_get(env)->oti_obj_dentry,
3352 buflen = buf->lb_len;
3355 if (buflen > sb->s_blocksize) {
3356 credits += osd_calc_bkmap_credits(
3358 (buflen + sb->s_blocksize - 1) >>
3359 sb->s_blocksize_bits);
3363 * xattr set may involve inode quota change, reserve credits for
3364 * dquot_initialize()
3366 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3369 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3375 * Set the 64-bit version for object
3377 static void osd_object_version_set(const struct lu_env *env,
3378 struct dt_object *dt,
3379 dt_obj_version_t *new_version)
3381 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3383 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3384 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3386 LDISKFS_I(inode)->i_fs_version = *new_version;
3387 /** Version is set after all inode operations are finished,
3388 * so we should mark it dirty here */
3389 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3393 * Concurrency: @dt is write locked.
3395 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3396 const struct lu_buf *buf, const char *name, int fl,
3397 struct thandle *handle)
3399 struct osd_object *obj = osd_dt_obj(dt);
3400 struct inode *inode = obj->oo_inode;
3401 struct osd_thread_info *info = osd_oti_get(env);
3406 LASSERT(handle != NULL);
3408 /* version set is not real XATTR */
3409 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3410 /* for version we are just using xattr API but change inode
3412 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3413 osd_object_version_set(env, dt, buf->lb_buf);
3414 return sizeof(dt_obj_version_t);
3417 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3418 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3420 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3421 if (fl & LU_XATTR_REPLACE)
3422 fs_flags |= XATTR_REPLACE;
3424 if (fl & LU_XATTR_CREATE)
3425 fs_flags |= XATTR_CREATE;
3427 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3428 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3431 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3435 lma->lma_incompat |= LMAI_STRIPED;
3436 lustre_lma_swab(lma);
3437 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3438 sizeof(*lma), XATTR_REPLACE);
3443 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3444 strcmp(name, XATTR_NAME_LINK) == 0)
3447 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3449 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3455 * Concurrency: @dt is read locked.
3457 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3458 const struct lu_buf *buf)
3460 struct osd_object *obj = osd_dt_obj(dt);
3461 struct inode *inode = obj->oo_inode;
3462 struct osd_thread_info *info = osd_oti_get(env);
3463 struct dentry *dentry = &info->oti_obj_dentry;
3465 if (!dt_object_exists(dt))
3468 LASSERT(!dt_object_remote(dt));
3469 LASSERT(inode->i_op != NULL);
3470 LASSERT(inode->i_op->listxattr != NULL);
3472 dentry->d_inode = inode;
3473 dentry->d_sb = inode->i_sb;
3474 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3477 static int osd_declare_xattr_del(const struct lu_env *env,
3478 struct dt_object *dt, const char *name,
3479 struct thandle *handle)
3481 struct osd_thandle *oh;
3482 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3484 LASSERT(!dt_object_remote(dt));
3485 LASSERT(handle != NULL);
3487 oh = container_of0(handle, struct osd_thandle, ot_super);
3488 LASSERT(oh->ot_handle == NULL);
3490 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3491 osd_dto_credits_noquota[DTO_XATTR_SET]);
3493 * xattr del may involve inode quota change, reserve credits for
3494 * dquot_initialize()
3496 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3502 * Concurrency: @dt is write locked.
3504 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3505 const char *name, struct thandle *handle)
3507 struct osd_object *obj = osd_dt_obj(dt);
3508 struct inode *inode = obj->oo_inode;
3509 struct osd_thread_info *info = osd_oti_get(env);
3510 struct dentry *dentry = &info->oti_obj_dentry;
3513 if (!dt_object_exists(dt))
3516 LASSERT(!dt_object_remote(dt));
3517 LASSERT(inode->i_op != NULL);
3518 LASSERT(inode->i_op->removexattr != NULL);
3519 LASSERT(handle != NULL);
3521 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3523 ll_vfs_dq_init(inode);
3524 dentry->d_inode = inode;
3525 dentry->d_sb = inode->i_sb;
3526 rc = inode->i_op->removexattr(dentry, name);
3527 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3531 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3532 __u64 start, __u64 end)
3534 struct osd_object *obj = osd_dt_obj(dt);
3535 struct inode *inode = obj->oo_inode;
3536 struct osd_thread_info *info = osd_oti_get(env);
3537 struct dentry *dentry = &info->oti_obj_dentry;
3538 struct file *file = &info->oti_file;
3543 dentry->d_inode = inode;
3544 dentry->d_sb = inode->i_sb;
3545 file->f_path.dentry = dentry;
3546 file->f_mapping = inode->i_mapping;
3547 file->f_op = inode->i_fop;
3548 set_file_inode(file, inode);
3550 rc = ll_vfs_fsync_range(file, start, end, 0);
3559 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3560 const struct dt_index_features *feat)
3562 struct iam_descr *descr;
3564 if (osd_object_is_root(o))
3565 return feat == &dt_directory_features;
3567 LASSERT(o->oo_dir != NULL);
3569 descr = o->oo_dir->od_container.ic_descr;
3570 if (feat == &dt_directory_features) {
3571 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3577 feat->dif_keysize_min <= descr->id_key_size &&
3578 descr->id_key_size <= feat->dif_keysize_max &&
3579 feat->dif_recsize_min <= descr->id_rec_size &&
3580 descr->id_rec_size <= feat->dif_recsize_max &&
3581 !(feat->dif_flags & (DT_IND_VARKEY |
3582 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3583 ergo(feat->dif_flags & DT_IND_UPDATE,
3584 1 /* XXX check that object (and file system) is
3589 static int osd_iam_container_init(const struct lu_env *env,
3590 struct osd_object *obj,
3591 struct osd_directory *dir)
3593 struct iam_container *bag = &dir->od_container;
3596 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3600 result = iam_container_setup(bag);
3602 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3604 iam_container_fini(bag);
3611 * Concurrency: no external locking is necessary.
3613 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3614 const struct dt_index_features *feat)
3618 struct osd_object *obj = osd_dt_obj(dt);
3620 LINVRNT(osd_invariant(obj));
3622 if (osd_object_is_root(obj)) {
3623 dt->do_index_ops = &osd_index_ea_ops;
3625 } else if (feat == &dt_directory_features) {
3626 dt->do_index_ops = &osd_index_ea_ops;
3627 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
3632 } else if (unlikely(feat == &dt_otable_features)) {
3633 dt->do_index_ops = &osd_otable_ops;
3635 } else if (unlikely(feat == &dt_acct_features)) {
3636 dt->do_index_ops = &osd_acct_index_ops;
3639 } else if (!osd_has_index(obj)) {
3640 struct osd_directory *dir;
3645 spin_lock(&obj->oo_guard);
3646 if (obj->oo_dir == NULL)
3650 * Concurrent thread allocated container data.
3653 spin_unlock(&obj->oo_guard);
3655 * Now, that we have container data, serialize its
3658 down_write(&obj->oo_ext_idx_sem);
3660 * recheck under lock.
3662 if (!osd_has_index(obj))
3663 result = osd_iam_container_init(env, obj,
3667 up_write(&obj->oo_ext_idx_sem);
3675 if (result == 0 && skip_iam == 0) {
3676 if (!osd_iam_index_probe(env, obj, feat))
3679 LINVRNT(osd_invariant(obj));
3681 if (result == 0 && feat == &dt_quota_glb_features &&
3682 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3683 result = osd_quota_migration(env, dt);
3688 static int osd_otable_it_attr_get(const struct lu_env *env,
3689 struct dt_object *dt,
3690 struct lu_attr *attr)
3696 static const struct dt_object_operations osd_obj_ops = {
3697 .do_read_lock = osd_object_read_lock,
3698 .do_write_lock = osd_object_write_lock,
3699 .do_read_unlock = osd_object_read_unlock,
3700 .do_write_unlock = osd_object_write_unlock,
3701 .do_write_locked = osd_object_write_locked,
3702 .do_attr_get = osd_attr_get,
3703 .do_declare_attr_set = osd_declare_attr_set,
3704 .do_attr_set = osd_attr_set,
3705 .do_ah_init = osd_ah_init,
3706 .do_declare_create = osd_declare_object_create,
3707 .do_create = osd_object_create,
3708 .do_declare_destroy = osd_declare_object_destroy,
3709 .do_destroy = osd_object_destroy,
3710 .do_index_try = osd_index_try,
3711 .do_declare_ref_add = osd_declare_object_ref_add,
3712 .do_ref_add = osd_object_ref_add,
3713 .do_declare_ref_del = osd_declare_object_ref_del,
3714 .do_ref_del = osd_object_ref_del,
3715 .do_xattr_get = osd_xattr_get,
3716 .do_declare_xattr_set = osd_declare_xattr_set,
3717 .do_xattr_set = osd_xattr_set,
3718 .do_declare_xattr_del = osd_declare_xattr_del,
3719 .do_xattr_del = osd_xattr_del,
3720 .do_xattr_list = osd_xattr_list,
3721 .do_object_sync = osd_object_sync,
3725 * dt_object_operations for interoperability mode
3726 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3728 static const struct dt_object_operations osd_obj_ea_ops = {
3729 .do_read_lock = osd_object_read_lock,
3730 .do_write_lock = osd_object_write_lock,
3731 .do_read_unlock = osd_object_read_unlock,
3732 .do_write_unlock = osd_object_write_unlock,
3733 .do_write_locked = osd_object_write_locked,
3734 .do_attr_get = osd_attr_get,
3735 .do_declare_attr_set = osd_declare_attr_set,
3736 .do_attr_set = osd_attr_set,
3737 .do_ah_init = osd_ah_init,
3738 .do_declare_create = osd_declare_object_create,
3739 .do_create = osd_object_ea_create,
3740 .do_declare_destroy = osd_declare_object_destroy,
3741 .do_destroy = osd_object_destroy,
3742 .do_index_try = osd_index_try,
3743 .do_declare_ref_add = osd_declare_object_ref_add,
3744 .do_ref_add = osd_object_ref_add,
3745 .do_declare_ref_del = osd_declare_object_ref_del,
3746 .do_ref_del = osd_object_ref_del,
3747 .do_xattr_get = osd_xattr_get,
3748 .do_declare_xattr_set = osd_declare_xattr_set,
3749 .do_xattr_set = osd_xattr_set,
3750 .do_declare_xattr_del = osd_declare_xattr_del,
3751 .do_xattr_del = osd_xattr_del,
3752 .do_xattr_list = osd_xattr_list,
3753 .do_object_sync = osd_object_sync,
3756 static const struct dt_object_operations osd_obj_otable_it_ops = {
3757 .do_attr_get = osd_otable_it_attr_get,
3758 .do_index_try = osd_index_try,
3761 static int osd_index_declare_iam_delete(const struct lu_env *env,
3762 struct dt_object *dt,
3763 const struct dt_key *key,
3764 struct thandle *handle)
3766 struct osd_thandle *oh;
3768 oh = container_of0(handle, struct osd_thandle, ot_super);
3769 LASSERT(oh->ot_handle == NULL);
3771 /* Recycle may cause additional three blocks to be changed. */
3772 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3773 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3779 * delete a (key, value) pair from index \a dt specified by \a key
3781 * \param dt osd index object
3782 * \param key key for index
3783 * \param rec record reference
3784 * \param handle transaction handler
3787 * \retval -ve failure
3789 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3790 const struct dt_key *key,
3791 struct thandle *handle)
3793 struct osd_thread_info *oti = osd_oti_get(env);
3794 struct osd_object *obj = osd_dt_obj(dt);
3795 struct osd_thandle *oh;
3796 struct iam_path_descr *ipd;
3797 struct iam_container *bag = &obj->oo_dir->od_container;
3801 if (!dt_object_exists(dt))
3804 LINVRNT(osd_invariant(obj));
3805 LASSERT(!dt_object_remote(dt));
3806 LASSERT(bag->ic_object == obj->oo_inode);
3807 LASSERT(handle != NULL);
3809 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3811 ipd = osd_idx_ipd_get(env, bag);
3812 if (unlikely(ipd == NULL))
3815 oh = container_of0(handle, struct osd_thandle, ot_super);
3816 LASSERT(oh->ot_handle != NULL);
3817 LASSERT(oh->ot_handle->h_transaction != NULL);
3819 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3820 /* swab quota uid/gid provided by caller */
3821 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3822 key = (const struct dt_key *)&oti->oti_quota_id;
3825 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3826 osd_ipd_put(env, bag, ipd);
3827 LINVRNT(osd_invariant(obj));
3828 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3832 static int osd_index_declare_ea_delete(const struct lu_env *env,
3833 struct dt_object *dt,
3834 const struct dt_key *key,
3835 struct thandle *handle)
3837 struct osd_thandle *oh;
3838 struct inode *inode;
3842 LASSERT(!dt_object_remote(dt));
3843 LASSERT(handle != NULL);
3845 oh = container_of0(handle, struct osd_thandle, ot_super);
3846 LASSERT(oh->ot_handle == NULL);
3848 /* due to DNE we may need to remove an agent inode */
3849 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3850 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3851 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3853 inode = osd_dt_obj(dt)->oo_inode;
3857 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3858 0, oh, osd_dt_obj(dt), true, NULL, false);
3862 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3865 struct osd_fid_pack *rec;
3868 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3869 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3870 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3871 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3877 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3878 const struct lu_fid *fid)
3880 struct seq_server_site *ss = osd_seq_site(osd);
3883 /* FID seqs not in FLDB, must be local seq */
3884 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3887 /* If FLD is not being initialized yet, it only happens during the
3888 * initialization, likely during mgs initialization, and we assume
3889 * this is local FID. */
3890 if (ss == NULL || ss->ss_server_fld == NULL)
3893 /* Only check the local FLDB here */
3894 if (osd_seq_exists(env, osd, fid_seq(fid)))
3901 * Index delete function for interoperability mode (b11826).
3902 * It will remove the directory entry added by osd_index_ea_insert().
3903 * This entry is needed to maintain name->fid mapping.
3905 * \param key, key i.e. file entry to be deleted
3907 * \retval 0, on success
3908 * \retval -ve, on error
3910 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3911 const struct dt_key *key, struct thandle *handle)
3913 struct osd_object *obj = osd_dt_obj(dt);
3914 struct inode *dir = obj->oo_inode;
3915 struct dentry *dentry;
3916 struct osd_thandle *oh;
3917 struct ldiskfs_dir_entry_2 *de = NULL;
3918 struct buffer_head *bh;
3919 struct htree_lock *hlock = NULL;
3920 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3921 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3925 if (!dt_object_exists(dt))
3928 LINVRNT(osd_invariant(obj));
3929 LASSERT(!dt_object_remote(dt));
3930 LASSERT(handle != NULL);
3932 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3934 oh = container_of(handle, struct osd_thandle, ot_super);
3935 LASSERT(oh->ot_handle != NULL);
3936 LASSERT(oh->ot_handle->h_transaction != NULL);
3938 ll_vfs_dq_init(dir);
3939 dentry = osd_child_dentry_get(env, obj,
3940 (char *)key, strlen((char *)key));
3942 if (obj->oo_hl_head != NULL) {
3943 hlock = osd_oti_get(env)->oti_hlock;
3944 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3945 dir, LDISKFS_HLOCK_DEL);
3947 down_write(&obj->oo_ext_idx_sem);
3950 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3952 /* If this is not the ".." entry, it might be a remote DNE
3953 * entry and we need to check if the FID is for a remote
3954 * MDT. If the FID is not in the directory entry (e.g.
3955 * upgraded 1.8 filesystem without dirdata enabled) then
3956 * we need to get the FID from the LMA. For a remote directory
3957 * there HAS to be an LMA, it cannot be an IGIF inode in this
3960 * Delete the entry before the agent inode in order to
3961 * simplify error handling. At worst an error after deleting
3962 * the entry first might leak the agent inode afterward. The
3963 * reverse would need filesystem abort in case of error deleting
3964 * the entry after the agent had been removed, or leave a
3965 * dangling entry pointing at a random inode. */
3966 if (strcmp((char *)key, dotdot) != 0) {
3967 LASSERT(de != NULL);
3968 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3969 if (rc == -ENODATA) {
3970 /* can't get FID, postpone to the end of the
3971 * transaction when iget() is safe */
3972 osd_schedule_agent_inode_removal(env, oh,
3973 le32_to_cpu(de->inode));
3974 } else if (rc == 0 &&
3975 unlikely(osd_remote_fid(env, osd, fid))) {
3976 osd_schedule_agent_inode_removal(env, oh,
3977 le32_to_cpu(de->inode));
3980 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3986 ldiskfs_htree_unlock(hlock);
3988 up_write(&obj->oo_ext_idx_sem);
3993 /* For inode on the remote MDT, .. will point to
3994 * /Agent directory, Check whether it needs to delete
3995 * from agent directory */
3996 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3997 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3999 if (rc != 0 && rc != -ENOENT) {
4000 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4001 osd_name(osd), PFID(fid), rc);
4011 LASSERT(osd_invariant(obj));
4012 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4017 * Lookup index for \a key and copy record to \a rec.
4019 * \param dt osd index object
4020 * \param key key for index
4021 * \param rec record reference
4023 * \retval +ve success : exact mach
4024 * \retval 0 return record with key not greater than \a key
4025 * \retval -ve failure
4027 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4028 struct dt_rec *rec, const struct dt_key *key)
4030 struct osd_object *obj = osd_dt_obj(dt);
4031 struct iam_path_descr *ipd;
4032 struct iam_container *bag = &obj->oo_dir->od_container;
4033 struct osd_thread_info *oti = osd_oti_get(env);
4034 struct iam_iterator *it = &oti->oti_idx_it;
4035 struct iam_rec *iam_rec;
4039 if (!dt_object_exists(dt))
4042 LASSERT(osd_invariant(obj));
4043 LASSERT(!dt_object_remote(dt));
4044 LASSERT(bag->ic_object == obj->oo_inode);
4046 ipd = osd_idx_ipd_get(env, bag);
4050 /* got ipd now we can start iterator. */
4051 iam_it_init(it, bag, 0, ipd);
4053 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4054 /* swab quota uid/gid provided by caller */
4055 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4056 key = (const struct dt_key *)&oti->oti_quota_id;
4059 rc = iam_it_get(it, (struct iam_key *)key);
4061 if (S_ISDIR(obj->oo_inode->i_mode))
4062 iam_rec = (struct iam_rec *)oti->oti_ldp;
4064 iam_rec = (struct iam_rec *) rec;
4066 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4068 if (S_ISDIR(obj->oo_inode->i_mode))
4069 osd_fid_unpack((struct lu_fid *) rec,
4070 (struct osd_fid_pack *)iam_rec);
4071 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4072 osd_quota_unpack(obj, rec);
4077 osd_ipd_put(env, bag, ipd);
4079 LINVRNT(osd_invariant(obj));
4084 static int osd_index_declare_iam_insert(const struct lu_env *env,
4085 struct dt_object *dt,
4086 const struct dt_rec *rec,
4087 const struct dt_key *key,
4088 struct thandle *handle)
4090 struct osd_thandle *oh;
4092 LASSERT(handle != NULL);
4094 oh = container_of0(handle, struct osd_thandle, ot_super);
4095 LASSERT(oh->ot_handle == NULL);
4097 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4098 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4104 * Inserts (key, value) pair in \a dt index object.
4106 * \param dt osd index object
4107 * \param key key for index
4108 * \param rec record reference
4109 * \param th transaction handler
4112 * \retval -ve failure
4114 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4115 const struct dt_rec *rec,
4116 const struct dt_key *key, struct thandle *th,
4119 struct osd_object *obj = osd_dt_obj(dt);
4120 struct iam_path_descr *ipd;
4121 struct osd_thandle *oh;
4122 struct iam_container *bag;
4123 struct osd_thread_info *oti = osd_oti_get(env);
4124 struct iam_rec *iam_rec;
4128 if (!dt_object_exists(dt))
4131 LINVRNT(osd_invariant(obj));
4132 LASSERT(!dt_object_remote(dt));
4134 bag = &obj->oo_dir->od_container;
4135 LASSERT(bag->ic_object == obj->oo_inode);
4136 LASSERT(th != NULL);
4138 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4140 ipd = osd_idx_ipd_get(env, bag);
4141 if (unlikely(ipd == NULL))
4144 oh = container_of0(th, struct osd_thandle, ot_super);
4145 LASSERT(oh->ot_handle != NULL);
4146 LASSERT(oh->ot_handle->h_transaction != NULL);
4147 if (S_ISDIR(obj->oo_inode->i_mode)) {
4148 iam_rec = (struct iam_rec *)oti->oti_ldp;
4149 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4150 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4151 /* pack quota uid/gid */
4152 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4153 key = (const struct dt_key *)&oti->oti_quota_id;
4154 /* pack quota record */
4155 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4156 iam_rec = (struct iam_rec *)rec;
4158 iam_rec = (struct iam_rec *)rec;
4161 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4163 osd_ipd_put(env, bag, ipd);
4164 LINVRNT(osd_invariant(obj));
4165 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4170 * Calls ldiskfs_add_entry() to add directory entry
4171 * into the directory. This is required for
4172 * interoperability mode (b11826)
4174 * \retval 0, on success
4175 * \retval -ve, on error
4177 static int __osd_ea_add_rec(struct osd_thread_info *info,
4178 struct osd_object *pobj, struct inode *cinode,
4179 const char *name, const struct lu_fid *fid,
4180 struct htree_lock *hlock, struct thandle *th)
4182 struct ldiskfs_dentry_param *ldp;
4183 struct dentry *child;
4184 struct osd_thandle *oth;
4187 oth = container_of(th, struct osd_thandle, ot_super);
4188 LASSERT(oth->ot_handle != NULL);
4189 LASSERT(oth->ot_handle->h_transaction != NULL);
4190 LASSERT(pobj->oo_inode);
4192 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4193 if (unlikely(pobj->oo_inode ==
4194 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4197 osd_get_ldiskfs_dirent_param(ldp, fid);
4198 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4199 child->d_fsdata = (void *)ldp;
4200 ll_vfs_dq_init(pobj->oo_inode);
4201 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4203 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4204 struct ldiskfs_dir_entry_2 *de;
4205 struct buffer_head *bh;
4208 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4211 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4214 if (S_ISDIR(cinode->i_mode))
4215 de->file_type = LDISKFS_DIRENT_LUFID |
4216 LDISKFS_FT_REG_FILE;
4218 de->file_type = LDISKFS_DIRENT_LUFID |
4220 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4231 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4232 * into the directory.Also sets flags into osd object to
4233 * indicate dot and dotdot are created. This is required for
4234 * interoperability mode (b11826)
4236 * \param dir directory for dot and dotdot fixup.
4237 * \param obj child object for linking
4239 * \retval 0, on success
4240 * \retval -ve, on error
4242 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4243 struct osd_object *dir,
4244 struct inode *parent_dir, const char *name,
4245 const struct lu_fid *dot_fid,
4246 const struct lu_fid *dot_dot_fid,
4249 struct inode *inode = dir->oo_inode;
4250 struct osd_thandle *oth;
4253 oth = container_of(th, struct osd_thandle, ot_super);
4254 LASSERT(oth->ot_handle->h_transaction != NULL);
4255 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4257 if (strcmp(name, dot) == 0) {
4258 if (dir->oo_compat_dot_created) {
4261 LASSERT(inode->i_ino == parent_dir->i_ino);
4262 dir->oo_compat_dot_created = 1;
4265 } else if (strcmp(name, dotdot) == 0) {
4266 if (!dir->oo_compat_dot_created)
4268 /* in case of rename, dotdot is already created */
4269 if (dir->oo_compat_dotdot_created) {
4270 return __osd_ea_add_rec(info, dir, parent_dir, name,
4271 dot_dot_fid, NULL, th);
4274 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4275 struct lu_fid tfid = *dot_dot_fid;
4278 result = osd_add_dot_dotdot_internal(info,
4279 dir->oo_inode, parent_dir, dot_fid,
4282 result = osd_add_dot_dotdot_internal(info,
4283 dir->oo_inode, parent_dir, dot_fid,
4288 dir->oo_compat_dotdot_created = 1;
4296 * It will call the appropriate osd_add* function and return the
4297 * value, return by respective functions.
4299 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4300 struct inode *cinode, const char *name,
4301 const struct lu_fid *fid, struct thandle *th)
4303 struct osd_thread_info *info = osd_oti_get(env);
4304 struct htree_lock *hlock;
4307 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4309 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4311 if (hlock != NULL) {
4312 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4315 down_write(&pobj->oo_ext_idx_sem);
4318 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4319 lu_object_fid(&pobj->oo_dt.do_lu),
4322 if (hlock != NULL) {
4323 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4324 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4326 down_write(&pobj->oo_ext_idx_sem);
4329 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4330 struct lu_fid *tfid = &info->oti_fid;
4334 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4337 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4342 ldiskfs_htree_unlock(hlock);
4344 up_write(&pobj->oo_ext_idx_sem);
4350 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4351 struct osd_idmap_cache *oic)
4353 struct osd_scrub *scrub = &dev->od_scrub;
4354 struct lu_fid *fid = &oic->oic_fid;
4355 struct osd_inode_id *id = &oti->oti_id;
4360 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4363 if (scrub->os_pos_current > id->oii_ino)
4367 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4368 if (rc == -ENOENT) {
4369 struct inode *inode;
4372 inode = osd_iget(oti, dev, &oic->oic_lid);
4374 /* The inode has been removed (by race maybe). */
4375 if (IS_ERR(inode)) {
4376 rc = PTR_ERR(inode);
4378 RETURN(rc == -ESTALE ? -ENOENT : rc);
4382 /* The OI mapping is lost. */
4383 if (id->oii_gen != OSD_OII_NOGEN)
4386 /* The inode may has been reused by others, we do not know,
4387 * leave it to be handled by subsequent osd_fid_lookup(). */
4389 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4394 if (thread_is_running(&scrub->os_thread)) {
4395 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4396 /* There is race condition between osd_oi_lookup and OI scrub.
4397 * The OI scrub finished just after osd_oi_lookup() failure.
4398 * Under such case, it is unnecessary to trigger OI scrub again,
4399 * but try to call osd_oi_lookup() again. */
4400 if (unlikely(rc == -EAGAIN))
4406 if (!dev->od_noscrub && ++once == 1) {
4407 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4409 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4410 "for "DFID", rc = %d [2]\n",
4411 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4413 if (rc == 0 || rc == -EALREADY)
4420 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4421 struct osd_device *dev,
4422 struct osd_idmap_cache *oic,
4423 struct lu_fid *fid, __u32 ino)
4425 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4426 struct inode *inode;
4429 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4430 inode = osd_iget(oti, dev, &oic->oic_lid);
4431 if (IS_ERR(inode)) {
4432 fid_zero(&oic->oic_fid);
4433 return PTR_ERR(inode);
4436 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4439 fid_zero(&oic->oic_fid);
4441 *fid = oic->oic_fid = lma->lma_self_fid;
4445 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4446 struct osd_inode_id *id, const struct lu_fid *fid)
4448 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4449 id->oii_ino, id->oii_gen, info);
4450 info->oti_cache.oic_lid = *id;
4451 info->oti_cache.oic_fid = *fid;
4452 info->oti_cache.oic_dev = osd;
4456 * Get parent FID from the linkEA.
4458 * For a directory which parent resides on remote MDT, to satisfy the
4459 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4460 * the other hand, to make the lookup(..) on the directory can return
4461 * the real parent FID, we append the real parent FID after its ".."
4462 * name entry in the /REMOTE_PARENT_DIR.
4464 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4465 * backup. So after the restore, we cannot get the right parent FID from
4466 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4467 * we have stored the real parent FID in the directory object's linkEA,
4468 * we can parse the linkEA for the real parent FID.
4470 * \param[in] env pointer to the thread context
4471 * \param[in] obj pointer to the object to be handled
4472 * \param[out]fid pointer to the buffer to hold the parent FID
4474 * \retval 0 for getting the real parent FID successfully
4475 * \retval negative error number on failure
4477 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4478 struct osd_object *obj,
4481 struct osd_thread_info *oti = osd_oti_get(env);
4482 struct lu_buf *buf = &oti->oti_big_buf;
4483 struct dentry *dentry = &oti->oti_obj_dentry;
4484 struct inode *inode = obj->oo_inode;
4485 struct linkea_data ldata = { NULL };
4490 if (!S_ISDIR(inode->i_mode))
4494 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4495 buf->lb_buf, buf->lb_len);
4496 if (rc == -ERANGE) {
4497 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4500 lu_buf_realloc(buf, rc);
4501 if (buf->lb_buf == NULL)
4508 if (unlikely(rc == 0))
4514 if (unlikely(buf->lb_buf == NULL)) {
4515 lu_buf_realloc(buf, rc);
4516 if (buf->lb_buf == NULL)
4523 rc = linkea_init(&ldata);
4525 linkea_first_entry(&ldata);
4526 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4533 * Calls ->lookup() to find dentry. From dentry get inode and
4534 * read inode's ea to get fid. This is required for interoperability
4537 * \retval 0, on success
4538 * \retval -ve, on error
4540 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4541 struct dt_rec *rec, const struct dt_key *key)
4543 struct inode *dir = obj->oo_inode;
4544 struct dentry *dentry;
4545 struct ldiskfs_dir_entry_2 *de;
4546 struct buffer_head *bh;
4547 struct lu_fid *fid = (struct lu_fid *) rec;
4548 struct htree_lock *hlock = NULL;
4553 LASSERT(dir->i_op != NULL);
4554 LASSERT(dir->i_op->lookup != NULL);
4556 dentry = osd_child_dentry_get(env, obj,
4557 (char *)key, strlen((char *)key));
4559 if (obj->oo_hl_head != NULL) {
4560 hlock = osd_oti_get(env)->oti_hlock;
4561 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4562 dir, LDISKFS_HLOCK_LOOKUP);
4564 down_read(&obj->oo_ext_idx_sem);
4567 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4569 struct osd_thread_info *oti = osd_oti_get(env);
4570 struct osd_inode_id *id = &oti->oti_id;
4571 struct osd_idmap_cache *oic = &oti->oti_cache;
4572 struct osd_device *dev = osd_obj2dev(obj);
4574 ino = le32_to_cpu(de->inode);
4575 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4577 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4581 rc = osd_get_fid_from_dentry(de, rec);
4583 /* done with de, release bh */
4586 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4587 const char *name = (const char *)key;
4589 /* If the parent is on remote MDT, and there
4590 * is no FID-in-dirent, then we have to get
4591 * the parent FID from the linkEA. */
4592 if (likely(strlen(name) == 2 &&
4593 name[0] == '.' && name[1] == '.'))
4594 rc = osd_get_pfid_from_linkea(env, obj,
4597 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4600 osd_id_gen(id, ino, OSD_OII_NOGEN);
4603 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4604 fid_zero(&oic->oic_fid);
4609 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4610 rc = osd_consistency_check(oti, dev, oic);
4612 fid_zero(&oic->oic_fid);
4621 ldiskfs_htree_unlock(hlock);
4623 up_read(&obj->oo_ext_idx_sem);
4628 * Put the osd object once done with it.
4630 * \param obj osd object that needs to be put
4632 static inline void osd_object_put(const struct lu_env *env,
4633 struct osd_object *obj)
4635 lu_object_put(env, &obj->oo_dt.do_lu);
4638 static int osd_index_declare_ea_insert(const struct lu_env *env,
4639 struct dt_object *dt,
4640 const struct dt_rec *rec,
4641 const struct dt_key *key,
4642 struct thandle *handle)
4644 struct osd_thandle *oh;
4645 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4646 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4647 const struct lu_fid *fid = rec1->rec_fid;
4648 int credits, rc = 0;
4649 struct osd_idmap_cache *idc;
4652 LASSERT(!dt_object_remote(dt));
4653 LASSERT(handle != NULL);
4654 LASSERT(fid != NULL);
4655 LASSERT(rec1->rec_type != 0);
4657 oh = container_of0(handle, struct osd_thandle, ot_super);
4658 LASSERT(oh->ot_handle == NULL);
4660 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4662 /* we can't call iget() while a transactions is running
4663 * (this can lead to a deadlock), but we need to know
4664 * inum and object type. so we find this information at
4665 * declaration and cache in per-thread info */
4666 idc = osd_idc_find_or_init(env, osd, fid);
4668 RETURN(PTR_ERR(idc));
4669 if (idc->oic_remote) {
4670 /* a reference to remote inode is represented by an
4671 * agent inode which we have to create */
4672 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4673 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4676 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4678 if (osd_dt_obj(dt)->oo_inode != NULL) {
4679 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4681 /* We ignore block quota on meta pool (MDTs), so needn't
4682 * calculate how many blocks will be consumed by this index
4684 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4685 i_gid_read(inode), 0, oh,
4686 osd_dt_obj(dt), true, NULL, false);
4693 * Index add function for interoperability mode (b11826).
4694 * It will add the directory entry.This entry is needed to
4695 * maintain name->fid mapping.
4697 * \param key it is key i.e. file entry to be inserted
4698 * \param rec it is value of given key i.e. fid
4700 * \retval 0, on success
4701 * \retval -ve, on error
4703 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4704 const struct dt_rec *rec,
4705 const struct dt_key *key, struct thandle *th,
4708 struct osd_object *obj = osd_dt_obj(dt);
4709 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4710 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4711 const struct lu_fid *fid = rec1->rec_fid;
4712 const char *name = (const char *)key;
4713 struct osd_thread_info *oti = osd_oti_get(env);
4714 struct inode *child_inode = NULL;
4715 struct osd_idmap_cache *idc;
4719 if (!dt_object_exists(dt))
4722 LASSERT(osd_invariant(obj));
4723 LASSERT(!dt_object_remote(dt));
4724 LASSERT(th != NULL);
4726 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4728 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4730 idc = osd_idc_find(env, osd, fid);
4731 if (unlikely(idc == NULL)) {
4732 /* this dt_insert() wasn't declared properly, so
4733 * FID is missing in OI cache. we better do not
4734 * lookup FID in FLDB/OI and don't risk to deadlock,
4735 * but in some special cases (lfsck testing, etc)
4736 * it's much simpler than fixing a caller */
4737 CERROR("%s: "DFID" wasn't declared for insert\n",
4738 osd_name(osd), PFID(fid));
4740 idc = osd_idc_find_or_init(env, osd, fid);
4742 RETURN(PTR_ERR(idc));
4745 if (idc->oic_remote) {
4746 /* Insert remote entry */
4747 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4748 struct osd_mdobj_map *omm = osd->od_mdt_map;
4749 struct osd_thandle *oh;
4751 /* If parent on remote MDT, we need put this object
4753 oh = container_of(th, typeof(*oh), ot_super);
4754 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4756 CERROR("%s: add "DFID" error: rc = %d\n",
4758 PFID(lu_object_fid(&dt->do_lu)), rc);
4762 child_inode = igrab(omm->omm_remote_parent->d_inode);
4764 child_inode = osd_create_local_agent_inode(env, osd,
4765 obj, fid, rec1->rec_type & S_IFMT, th);
4766 if (IS_ERR(child_inode))
4767 RETURN(PTR_ERR(child_inode));
4770 /* Insert local entry */
4771 if (unlikely(idc->oic_lid.oii_ino == 0)) {
4772 /* for a reason OI cache wasn't filled properly */
4773 CERROR("%s: OIC for "DFID" isn't filled\n",
4774 osd_name(osd), PFID(fid));
4777 child_inode = oti->oti_inode;
4778 if (unlikely(child_inode == NULL)) {
4779 struct ldiskfs_inode_info *lii;
4783 child_inode = oti->oti_inode = &lii->vfs_inode;
4785 child_inode->i_sb = osd_sb(osd);
4786 child_inode->i_ino = idc->oic_lid.oii_ino;
4787 child_inode->i_mode = rec1->rec_type & S_IFMT;
4790 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4792 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4793 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4795 if (child_inode && child_inode != oti->oti_inode)
4797 LASSERT(osd_invariant(obj));
4798 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4803 * Initialize osd Iterator for given osd index object.
4805 * \param dt osd index object
4808 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4809 struct dt_object *dt,
4812 struct osd_it_iam *it;
4813 struct osd_object *obj = osd_dt_obj(dt);
4814 struct lu_object *lo = &dt->do_lu;
4815 struct iam_path_descr *ipd;
4816 struct iam_container *bag = &obj->oo_dir->od_container;
4818 if (!dt_object_exists(dt))
4819 return ERR_PTR(-ENOENT);
4823 return ERR_PTR(-ENOMEM);
4825 ipd = osd_it_ipd_get(env, bag);
4826 if (likely(ipd != NULL)) {
4830 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4831 return (struct dt_it *)it;
4834 return ERR_PTR(-ENOMEM);
4839 * free given Iterator.
4842 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4844 struct osd_it_iam *it = (struct osd_it_iam *)di;
4845 struct osd_object *obj = it->oi_obj;
4847 iam_it_fini(&it->oi_it);
4848 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4849 lu_object_put(env, &obj->oo_dt.do_lu);
4854 * Move Iterator to record specified by \a key
4856 * \param di osd iterator
4857 * \param key key for index
4859 * \retval +ve di points to record with least key not larger than key
4860 * \retval 0 di points to exact matched key
4861 * \retval -ve failure
4864 static int osd_it_iam_get(const struct lu_env *env,
4865 struct dt_it *di, const struct dt_key *key)
4867 struct osd_thread_info *oti = osd_oti_get(env);
4868 struct osd_it_iam *it = (struct osd_it_iam *)di;
4870 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4871 /* swab quota uid/gid */
4872 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4873 key = (struct dt_key *)&oti->oti_quota_id;
4876 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4882 * \param di osd iterator
4884 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4886 struct osd_it_iam *it = (struct osd_it_iam *)di;
4888 iam_it_put(&it->oi_it);
4892 * Move iterator by one record
4894 * \param di osd iterator
4896 * \retval +1 end of container reached
4898 * \retval -ve failure
4901 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4903 struct osd_it_iam *it = (struct osd_it_iam *)di;
4905 return iam_it_next(&it->oi_it);
4909 * Return pointer to the key under iterator.
4912 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4913 const struct dt_it *di)
4915 struct osd_thread_info *oti = osd_oti_get(env);
4916 struct osd_it_iam *it = (struct osd_it_iam *)di;
4917 struct osd_object *obj = it->oi_obj;
4920 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4922 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4923 /* swab quota uid/gid */
4924 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4925 key = (struct dt_key *)&oti->oti_quota_id;
4932 * Return size of key under iterator (in bytes)
4935 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4937 struct osd_it_iam *it = (struct osd_it_iam *)di;
4939 return iam_it_key_size(&it->oi_it);
4943 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4945 /* check if file type is required */
4946 if (ent->lde_attrs & LUDA_TYPE) {
4947 struct luda_type *lt;
4948 int align = sizeof(*lt) - 1;
4950 len = (len + align) & ~align;
4951 lt = (struct luda_type *)(ent->lde_name + len);
4952 lt->lt_type = cpu_to_le16(DTTOIF(type));
4955 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4959 * build lu direct from backend fs dirent.
4963 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4964 char *name, __u16 namelen, __u16 type, __u32 attr)
4966 ent->lde_attrs = attr | LUDA_FID;
4967 fid_cpu_to_le(&ent->lde_fid, fid);
4969 ent->lde_hash = cpu_to_le64(offset);
4970 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4972 strncpy(ent->lde_name, name, namelen);
4973 ent->lde_name[namelen] = '\0';
4974 ent->lde_namelen = cpu_to_le16(namelen);
4976 /* append lustre attributes */
4977 osd_it_append_attrs(ent, namelen, type);
4981 * Return pointer to the record under iterator.
4983 static int osd_it_iam_rec(const struct lu_env *env,
4984 const struct dt_it *di,
4985 struct dt_rec *dtrec, __u32 attr)
4987 struct osd_it_iam *it = (struct osd_it_iam *)di;
4988 struct osd_thread_info *info = osd_oti_get(env);
4991 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4992 const struct osd_fid_pack *rec;
4993 struct lu_fid *fid = &info->oti_fid;
4994 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5000 name = (char *)iam_it_key_get(&it->oi_it);
5002 RETURN(PTR_ERR(name));
5004 namelen = iam_it_key_size(&it->oi_it);
5006 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5008 RETURN(PTR_ERR(rec));
5010 rc = osd_fid_unpack(fid, rec);
5014 hash = iam_it_store(&it->oi_it);
5016 /* IAM does not store object type in IAM index (dir) */
5017 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5019 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5020 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5021 (struct iam_rec *)dtrec);
5022 osd_quota_unpack(it->oi_obj, dtrec);
5024 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5025 (struct iam_rec *)dtrec);
5032 * Returns cookie for current Iterator position.
5034 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5036 struct osd_it_iam *it = (struct osd_it_iam *)di;
5038 return iam_it_store(&it->oi_it);
5042 * Restore iterator from cookie.
5044 * \param di osd iterator
5045 * \param hash Iterator location cookie
5047 * \retval +ve di points to record with least key not larger than key.
5048 * \retval 0 di points to exact matched key
5049 * \retval -ve failure
5052 static int osd_it_iam_load(const struct lu_env *env,
5053 const struct dt_it *di, __u64 hash)
5055 struct osd_it_iam *it = (struct osd_it_iam *)di;
5057 return iam_it_load(&it->oi_it, hash);
5060 static const struct dt_index_operations osd_index_iam_ops = {
5061 .dio_lookup = osd_index_iam_lookup,
5062 .dio_declare_insert = osd_index_declare_iam_insert,
5063 .dio_insert = osd_index_iam_insert,
5064 .dio_declare_delete = osd_index_declare_iam_delete,
5065 .dio_delete = osd_index_iam_delete,
5067 .init = osd_it_iam_init,
5068 .fini = osd_it_iam_fini,
5069 .get = osd_it_iam_get,
5070 .put = osd_it_iam_put,
5071 .next = osd_it_iam_next,
5072 .key = osd_it_iam_key,
5073 .key_size = osd_it_iam_key_size,
5074 .rec = osd_it_iam_rec,
5075 .store = osd_it_iam_store,
5076 .load = osd_it_iam_load
5082 * Creates or initializes iterator context.
5084 * \retval struct osd_it_ea, iterator structure on success
5087 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5088 struct dt_object *dt,
5091 struct osd_object *obj = osd_dt_obj(dt);
5092 struct osd_thread_info *info = osd_oti_get(env);
5093 struct osd_it_ea *oie;
5095 struct lu_object *lo = &dt->do_lu;
5096 struct dentry *obj_dentry;
5099 if (!dt_object_exists(dt) || obj->oo_destroyed)
5100 RETURN(ERR_PTR(-ENOENT));
5102 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5104 RETURN(ERR_PTR(-ENOMEM));
5105 obj_dentry = &oie->oie_dentry;
5107 obj_dentry->d_inode = obj->oo_inode;
5108 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5109 obj_dentry->d_name.hash = 0;
5111 oie->oie_rd_dirent = 0;
5112 oie->oie_it_dirent = 0;
5113 oie->oie_dirent = NULL;
5114 if (unlikely(!info->oti_it_ea_buf_used)) {
5115 oie->oie_buf = info->oti_it_ea_buf;
5116 info->oti_it_ea_buf_used = 1;
5118 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5119 if (oie->oie_buf == NULL)
5120 RETURN(ERR_PTR(-ENOMEM));
5124 file = &oie->oie_file;
5126 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5127 if (attr & LUDA_64BITHASH)
5128 file->f_mode = FMODE_64BITHASH;
5130 file->f_mode = FMODE_32BITHASH;
5131 file->f_path.dentry = obj_dentry;
5132 file->f_mapping = obj->oo_inode->i_mapping;
5133 file->f_op = obj->oo_inode->i_fop;
5134 set_file_inode(file, obj->oo_inode);
5137 RETURN((struct dt_it *) oie);
5141 * Destroy or finishes iterator context.
5143 * \param di iterator structure to be destroyed
5145 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5147 struct osd_thread_info *info = osd_oti_get(env);
5148 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5149 struct osd_object *obj = oie->oie_obj;
5150 struct inode *inode = obj->oo_inode;
5153 oie->oie_file.f_op->release(inode, &oie->oie_file);
5154 lu_object_put(env, &obj->oo_dt.do_lu);
5155 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5156 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5158 info->oti_it_ea_buf_used = 0;
5159 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5164 * It position the iterator at given key, so that next lookup continues from
5165 * that key Or it is similar to dio_it->load() but based on a key,
5166 * rather than file position.
5168 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5171 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5173 static int osd_it_ea_get(const struct lu_env *env,
5174 struct dt_it *di, const struct dt_key *key)
5176 struct osd_it_ea *it = (struct osd_it_ea *)di;
5179 LASSERT(((const char *)key)[0] == '\0');
5180 it->oie_file.f_pos = 0;
5181 it->oie_rd_dirent = 0;
5182 it->oie_it_dirent = 0;
5183 it->oie_dirent = NULL;
5191 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5195 struct osd_filldir_cbs {
5196 #ifdef HAVE_DIR_CONTEXT
5197 struct dir_context ctx;
5199 struct osd_it_ea *it;
5202 * It is called internally by ->readdir(). It fills the
5203 * iterator's in-memory data structure with required
5204 * information i.e. name, namelen, rec_size etc.
5206 * \param buf in which information to be filled in.
5207 * \param name name of the file in given dir
5209 * \retval 0 on success
5210 * \retval 1 on buffer full
5212 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5213 loff_t offset, __u64 ino,
5216 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5217 struct osd_object *obj = it->oie_obj;
5218 struct osd_it_ea_dirent *ent = it->oie_dirent;
5219 struct lu_fid *fid = &ent->oied_fid;
5220 struct osd_fid_pack *rec;
5223 /* this should never happen */
5224 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5225 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5229 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5233 /* "." is just the object itself. */
5234 if (namelen == 1 && name[0] == '.') {
5235 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5236 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5237 rec = (struct osd_fid_pack*) (name + namelen + 1);
5238 if (osd_fid_unpack(fid, rec) != 0)
5243 d_type &= ~LDISKFS_DIRENT_LUFID;
5245 /* NOT export local root. */
5246 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5247 ino = obj->oo_inode->i_ino;
5248 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5251 ent->oied_ino = ino;
5252 ent->oied_off = offset;
5253 ent->oied_namelen = namelen;
5254 ent->oied_type = d_type;
5256 memcpy(ent->oied_name, name, namelen);
5258 it->oie_rd_dirent++;
5259 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5264 * Calls ->readdir() to load a directory entry at a time
5265 * and stored it in iterator's in-memory data structure.
5267 * \param di iterator's in memory structure
5269 * \retval 0 on success
5270 * \retval -ve on error
5271 * \retval +1 reach the end of entry
5273 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5274 const struct dt_it *di)
5276 struct osd_it_ea *it = (struct osd_it_ea *)di;
5277 struct osd_object *obj = it->oie_obj;
5278 struct inode *inode = obj->oo_inode;
5279 struct htree_lock *hlock = NULL;
5280 struct file *filp = &it->oie_file;
5282 struct osd_filldir_cbs buf = {
5283 #ifdef HAVE_DIR_CONTEXT
5284 .ctx.actor = osd_ldiskfs_filldir,
5290 it->oie_dirent = it->oie_buf;
5291 it->oie_rd_dirent = 0;
5293 if (obj->oo_hl_head != NULL) {
5294 hlock = osd_oti_get(env)->oti_hlock;
5295 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5296 inode, LDISKFS_HLOCK_READDIR);
5298 down_read(&obj->oo_ext_idx_sem);
5301 #ifdef HAVE_DIR_CONTEXT
5302 buf.ctx.pos = filp->f_pos;
5303 rc = inode->i_fop->iterate(filp, &buf.ctx);
5304 filp->f_pos = buf.ctx.pos;
5306 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5310 ldiskfs_htree_unlock(hlock);
5312 up_read(&obj->oo_ext_idx_sem);
5314 if (it->oie_rd_dirent == 0) {
5315 /*If it does not get any dirent, it means it has been reached
5316 *to the end of the dir */
5317 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5321 it->oie_dirent = it->oie_buf;
5322 it->oie_it_dirent = 1;
5329 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5330 * to load a directory entry at a time and stored it in
5331 * iterator's in-memory data structure.
5333 * \param di iterator's in memory structure
5335 * \retval +ve iterator reached to end
5336 * \retval 0 iterator not reached to end
5337 * \retval -ve on error
5339 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5341 struct osd_it_ea *it = (struct osd_it_ea *)di;
5346 if (it->oie_it_dirent < it->oie_rd_dirent) {
5348 (void *) it->oie_dirent +
5349 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5350 it->oie_dirent->oied_namelen);
5351 it->oie_it_dirent++;
5354 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5357 rc = osd_ldiskfs_it_fill(env, di);
5364 * Returns the key at current position from iterator's in memory structure.
5366 * \param di iterator's in memory structure
5368 * \retval key i.e. struct dt_key on success
5370 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5371 const struct dt_it *di)
5373 struct osd_it_ea *it = (struct osd_it_ea *)di;
5375 return (struct dt_key *)it->oie_dirent->oied_name;
5379 * Returns the key's size at current position from iterator's in memory structure.
5381 * \param di iterator's in memory structure
5383 * \retval key_size i.e. struct dt_key on success
5385 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5387 struct osd_it_ea *it = (struct osd_it_ea *)di;
5389 return it->oie_dirent->oied_namelen;
5393 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5395 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5396 "dot_dotdot = %d\n", dot_dotdot);
5398 if (LDISKFS_DIR_REC_LEN(de) >=
5399 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5406 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5407 unsigned blocksize, int dot_dotdot)
5410 return osd_dot_dotdot_has_space(de, dot_dotdot);
5412 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5413 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5420 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5421 struct dentry *dentry, const struct lu_fid *fid,
5422 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5423 struct htree_lock *hlock, int dot_dotdot)
5425 struct inode *dir = dentry->d_parent->d_inode;
5426 struct inode *inode = dentry->d_inode;
5427 struct osd_fid_pack *rec;
5428 struct ldiskfs_dentry_param *ldp;
5429 int namelen = dentry->d_name.len;
5431 struct osd_thread_info *info = osd_oti_get(env);
5434 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5435 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5438 /* There is enough space to hold the FID-in-dirent. */
5439 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5441 rc = ldiskfs_journal_get_write_access(jh, bh);
5445 de->name[namelen] = 0;
5446 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5447 rec->fp_len = sizeof(struct lu_fid) + 1;
5448 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5449 de->file_type |= LDISKFS_DIRENT_LUFID;
5450 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5455 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5457 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5461 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5462 osd_get_ldiskfs_dirent_param(ldp, fid);
5463 dentry->d_fsdata = (void *)ldp;
5464 ll_vfs_dq_init(dir);
5465 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5466 /* It is too bad, we cannot reinsert the name entry back.
5467 * That means we lose it! */
5469 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5470 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5471 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5472 dir->i_ino, dir->i_generation, namelen,
5473 dentry->d_name.name, PFID(fid), rc);
5479 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5480 struct osd_it_ea *it, struct lu_fid *fid,
5481 struct osd_inode_id *id, __u32 *attr)
5483 struct osd_thread_info *info = osd_oti_get(env);
5484 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5485 struct osd_device *dev = osd_obj2dev(obj);
5486 struct super_block *sb = osd_sb(dev);
5487 const char *devname =
5488 LDISKFS_SB(sb)->s_es->s_volume_name;
5489 struct osd_it_ea_dirent *ent = it->oie_dirent;
5490 struct inode *dir = obj->oo_inode;
5491 struct htree_lock *hlock = NULL;
5492 struct buffer_head *bh = NULL;
5493 handle_t *jh = NULL;
5494 struct ldiskfs_dir_entry_2 *de;
5495 struct dentry *dentry;
5496 struct inode *inode;
5503 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5504 inode = osd_iget(info, dev, id);
5505 if (IS_ERR(inode)) {
5506 rc = PTR_ERR(inode);
5507 if (rc == -ENOENT || rc == -ESTALE) {
5508 *attr |= LUDA_UNKNOWN;
5511 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5512 "check_repair, dir = %lu/%u, name = %.*s: "
5514 devname, dir->i_ino, dir->i_generation,
5515 ent->oied_namelen, ent->oied_name, rc);
5521 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5523 rc = osd_get_lma(info, inode, dentry, lma);
5529 if (ent->oied_name[0] == '.') {
5530 if (ent->oied_namelen == 1)
5532 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5536 /* We need to ensure that the name entry is still valid.
5537 * Because it may be removed or renamed by other already.
5539 * The unlink or rename operation will start journal before PDO lock,
5540 * so to avoid deadlock, here we need to start journal handle before
5541 * related PDO lock also. But because we do not know whether there
5542 * will be something to be repaired before PDO lock, we just start
5543 * journal without conditions.
5545 * We may need to remove the name entry firstly, then insert back.
5546 * One credit is for user quota file update.
5547 * One credit is for group quota file update.
5548 * Two credits are for dirty inode. */
5549 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5550 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5552 if (dev->od_dirent_journal != 0) {
5555 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5558 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5559 "check_repair, dir = %lu/%u, credits = %d, "
5560 "name = %.*s: rc = %d\n",
5561 devname, dir->i_ino, dir->i_generation, credits,
5562 ent->oied_namelen, ent->oied_name, rc);
5564 GOTO(out_inode, rc);
5567 if (obj->oo_hl_head != NULL) {
5568 hlock = osd_oti_get(env)->oti_hlock;
5569 /* "0" means exclusive lock for the whole directory.
5570 * We need to prevent others access such name entry
5571 * during the delete + insert. Neither HLOCK_ADD nor
5572 * HLOCK_DEL cannot guarantee the atomicity. */
5573 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5575 down_write(&obj->oo_ext_idx_sem);
5578 if (obj->oo_hl_head != NULL) {
5579 hlock = osd_oti_get(env)->oti_hlock;
5580 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5581 LDISKFS_HLOCK_LOOKUP);
5583 down_read(&obj->oo_ext_idx_sem);
5587 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5588 /* For dot/dotdot entry, if there is not enough space to hold the
5589 * FID-in-dirent, just keep them there. It only happens when the
5590 * device upgraded from 1.8 or restored from MDT file-level backup.
5591 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5592 * and needs to get FID from LMA when readdir, it will not affect the
5593 * performance much. */
5594 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5595 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5596 *attr |= LUDA_IGNORE;
5602 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5603 struct lu_fid *tfid = &lma->lma_self_fid;
5605 *attr |= LUDA_IGNORE;
5606 /* It must be REMOTE_PARENT_DIR and as the
5607 * dotdot entry of remote directory */
5608 if (unlikely(dot_dotdot != 2 ||
5609 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5610 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5611 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5612 "parent directory, but got %.*s under "
5613 "dir = %lu/%u with the FID "DFID"\n",
5614 devname, ent->oied_namelen,
5615 ent->oied_name, dir->i_ino,
5616 dir->i_generation, PFID(tfid));
5618 GOTO(out, rc = -EIO);
5624 if (fid_is_sane(fid)) {
5625 /* FID-in-dirent is valid. */
5626 if (lu_fid_eq(fid, &lma->lma_self_fid))
5629 /* Do not repair under dryrun mode. */
5630 if (*attr & LUDA_VERIFY_DRYRUN) {
5631 *attr |= LUDA_REPAIR;
5638 dev->od_dirent_journal = 1;
5639 if (hlock != NULL) {
5640 ldiskfs_htree_unlock(hlock);
5643 up_read(&obj->oo_ext_idx_sem);
5649 *fid = lma->lma_self_fid;
5651 /* Update the FID-in-dirent. */
5652 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5655 *attr |= LUDA_REPAIR;
5657 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5658 "in the dirent, dir = %lu/%u, "
5659 "name = %.*s, "DFID": rc = %d\n",
5660 devname, dir->i_ino, dir->i_generation,
5661 ent->oied_namelen, ent->oied_name,
5664 /* Do not repair under dryrun mode. */
5665 if (*attr & LUDA_VERIFY_DRYRUN) {
5666 *fid = lma->lma_self_fid;
5667 *attr |= LUDA_REPAIR;
5674 dev->od_dirent_journal = 1;
5675 if (hlock != NULL) {
5676 ldiskfs_htree_unlock(hlock);
5679 up_read(&obj->oo_ext_idx_sem);
5685 *fid = lma->lma_self_fid;
5687 /* Append the FID-in-dirent. */
5688 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5691 *attr |= LUDA_REPAIR;
5693 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5694 "after the dirent, dir = %lu/%u, "
5695 "name = %.*s, "DFID": rc = %d\n",
5696 devname, dir->i_ino, dir->i_generation,
5697 ent->oied_namelen, ent->oied_name,
5701 /* Do not repair under dryrun mode. */
5702 if (*attr & LUDA_VERIFY_DRYRUN) {
5703 if (fid_is_sane(fid)) {
5704 *attr |= LUDA_REPAIR;
5706 lu_igif_build(fid, inode->i_ino,
5707 inode->i_generation);
5708 *attr |= LUDA_UPGRADE;
5716 dev->od_dirent_journal = 1;
5717 if (hlock != NULL) {
5718 ldiskfs_htree_unlock(hlock);
5721 up_read(&obj->oo_ext_idx_sem);
5728 if (unlikely(fid_is_sane(fid))) {
5729 /* FID-in-dirent exists, but FID-in-LMA is lost.
5730 * Trust the FID-in-dirent, and add FID-in-LMA. */
5731 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5733 *attr |= LUDA_REPAIR;
5735 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5736 "update dirent, dir = %lu/%u, "
5737 "name = %.*s, "DFID": rc = %d\n",
5738 devname, dir->i_ino, dir->i_generation,
5739 ent->oied_namelen, ent->oied_name,
5742 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5743 /* It is probably IGIF object. Only aappend the
5744 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5745 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5748 *attr |= LUDA_UPGRADE;
5750 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5751 "after the dirent, dir = %lu/%u, "
5752 "name = %.*s, "DFID": rc = %d\n",
5753 devname, dir->i_ino, dir->i_generation,
5754 ent->oied_namelen, ent->oied_name,
5763 if (hlock != NULL) {
5764 ldiskfs_htree_unlock(hlock);
5766 if (dev->od_dirent_journal != 0)
5767 up_write(&obj->oo_ext_idx_sem);
5769 up_read(&obj->oo_ext_idx_sem);
5773 ldiskfs_journal_stop(jh);
5777 if (rc >= 0 && !dirty)
5778 dev->od_dirent_journal = 0;
5784 * Returns the value at current position from iterator's in memory structure.
5786 * \param di struct osd_it_ea, iterator's in memory structure
5787 * \param attr attr requested for dirent.
5788 * \param lde lustre dirent
5790 * \retval 0 no error and \param lde has correct lustre dirent.
5791 * \retval -ve on error
5793 static inline int osd_it_ea_rec(const struct lu_env *env,
5794 const struct dt_it *di,
5795 struct dt_rec *dtrec, __u32 attr)
5797 struct osd_it_ea *it = (struct osd_it_ea *)di;
5798 struct osd_object *obj = it->oie_obj;
5799 struct osd_device *dev = osd_obj2dev(obj);
5800 struct osd_thread_info *oti = osd_oti_get(env);
5801 struct osd_inode_id *id = &oti->oti_id;
5802 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5803 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5804 __u32 ino = it->oie_dirent->oied_ino;
5808 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5810 if (attr & LUDA_VERIFY) {
5811 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5812 attr |= LUDA_IGNORE;
5813 /* If the parent is on remote MDT, and there
5814 * is no FID-in-dirent, then we have to get
5815 * the parent FID from the linkEA. */
5816 if (!fid_is_sane(fid) &&
5817 it->oie_dirent->oied_namelen == 2 &&
5818 it->oie_dirent->oied_name[0] == '.' &&
5819 it->oie_dirent->oied_name[1] == '.')
5820 osd_get_pfid_from_linkea(env, obj, fid);
5822 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5826 if (!fid_is_sane(fid)) {
5827 attr &= ~LUDA_IGNORE;
5828 attr |= LUDA_UNKNOWN;
5831 attr &= ~LU_DIRENT_ATTRS_MASK;
5832 if (!fid_is_sane(fid)) {
5833 bool is_dotdot = false;
5834 if (it->oie_dirent->oied_namelen == 2 &&
5835 it->oie_dirent->oied_name[0] == '.' &&
5836 it->oie_dirent->oied_name[1] == '.')
5838 /* If the parent is on remote MDT, and there
5839 * is no FID-in-dirent, then we have to get
5840 * the parent FID from the linkEA. */
5841 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5842 rc = osd_get_pfid_from_linkea(env, obj, fid);
5844 if (is_dotdot == false &&
5845 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5848 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5851 osd_id_gen(id, ino, OSD_OII_NOGEN);
5855 /* Pack the entry anyway, at least the offset is right. */
5856 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5857 it->oie_dirent->oied_name,
5858 it->oie_dirent->oied_namelen,
5859 it->oie_dirent->oied_type, attr);
5864 if (osd_remote_fid(env, dev, fid))
5867 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5868 osd_add_oi_cache(oti, dev, id, fid);
5870 RETURN(rc > 0 ? 0 : rc);
5874 * Returns the record size size at current position.
5876 * This function will return record(lu_dirent) size in bytes.
5878 * \param[in] env execution environment
5879 * \param[in] di iterator's in memory structure
5880 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5881 * calculate the lu_dirent size.
5883 * \retval record size(in bytes & in memory) of the current lu_dirent
5886 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5889 struct osd_it_ea *it = (struct osd_it_ea *)di;
5891 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5895 * Returns a cookie for current position of the iterator head, so that
5896 * user can use this cookie to load/start the iterator next time.
5898 * \param di iterator's in memory structure
5900 * \retval cookie for current position, on success
5902 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5904 struct osd_it_ea *it = (struct osd_it_ea *)di;
5906 return it->oie_dirent->oied_off;
5910 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5911 * to load a directory entry at a time and stored it i inn,
5912 * in iterator's in-memory data structure.
5914 * \param di struct osd_it_ea, iterator's in memory structure
5916 * \retval +ve on success
5917 * \retval -ve on error
5919 static int osd_it_ea_load(const struct lu_env *env,
5920 const struct dt_it *di, __u64 hash)
5922 struct osd_it_ea *it = (struct osd_it_ea *)di;
5926 it->oie_file.f_pos = hash;
5928 rc = osd_ldiskfs_it_fill(env, di);
5939 * Index lookup function for interoperability mode (b11826).
5941 * \param key, key i.e. file name to be searched
5943 * \retval +ve, on success
5944 * \retval -ve, on error
5946 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5947 struct dt_rec *rec, const struct dt_key *key)
5949 struct osd_object *obj = osd_dt_obj(dt);
5954 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5955 LINVRNT(osd_invariant(obj));
5957 rc = osd_ea_lookup_rec(env, obj, rec, key);
5964 * Index and Iterator operations for interoperability
5965 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5967 static const struct dt_index_operations osd_index_ea_ops = {
5968 .dio_lookup = osd_index_ea_lookup,
5969 .dio_declare_insert = osd_index_declare_ea_insert,
5970 .dio_insert = osd_index_ea_insert,
5971 .dio_declare_delete = osd_index_declare_ea_delete,
5972 .dio_delete = osd_index_ea_delete,
5974 .init = osd_it_ea_init,
5975 .fini = osd_it_ea_fini,
5976 .get = osd_it_ea_get,
5977 .put = osd_it_ea_put,
5978 .next = osd_it_ea_next,
5979 .key = osd_it_ea_key,
5980 .key_size = osd_it_ea_key_size,
5981 .rec = osd_it_ea_rec,
5982 .rec_size = osd_it_ea_rec_size,
5983 .store = osd_it_ea_store,
5984 .load = osd_it_ea_load
5988 static void *osd_key_init(const struct lu_context *ctx,
5989 struct lu_context_key *key)
5991 struct osd_thread_info *info;
5993 OBD_ALLOC_PTR(info);
5995 return ERR_PTR(-ENOMEM);
5997 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5998 if (info->oti_it_ea_buf == NULL)
6001 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6003 info->oti_hlock = ldiskfs_htree_lock_alloc();
6004 if (info->oti_hlock == NULL)
6010 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6013 return ERR_PTR(-ENOMEM);
6016 static void osd_key_fini(const struct lu_context *ctx,
6017 struct lu_context_key *key, void* data)
6019 struct osd_thread_info *info = data;
6020 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6021 struct osd_idmap_cache *idc = info->oti_ins_cache;
6023 if (info->oti_inode != NULL)
6025 if (info->oti_hlock != NULL)
6026 ldiskfs_htree_lock_free(info->oti_hlock);
6027 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6028 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6029 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6030 lu_buf_free(&info->oti_big_buf);
6032 LASSERT(info->oti_ins_cache_size > 0);
6033 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6034 info->oti_ins_cache = NULL;
6035 info->oti_ins_cache_size = 0;
6040 static void osd_key_exit(const struct lu_context *ctx,
6041 struct lu_context_key *key, void *data)
6043 struct osd_thread_info *info = data;
6045 LASSERT(info->oti_r_locks == 0);
6046 LASSERT(info->oti_w_locks == 0);
6047 LASSERT(info->oti_txns == 0);
6050 /* type constructor/destructor: osd_type_init, osd_type_fini */
6051 LU_TYPE_INIT_FINI(osd, &osd_key);
6053 struct lu_context_key osd_key = {
6054 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6055 .lct_init = osd_key_init,
6056 .lct_fini = osd_key_fini,
6057 .lct_exit = osd_key_exit
6061 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6062 const char *name, struct lu_device *next)
6064 struct osd_device *osd = osd_dev(d);
6066 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6067 >= sizeof(osd->od_svname))
6069 return osd_procfs_init(osd, name);
6072 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6074 struct seq_server_site *ss = osd_seq_site(osd);
6078 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6081 if (unlikely(ss == NULL))
6084 OBD_ALLOC_PTR(osd->od_cl_seq);
6085 if (osd->od_cl_seq == NULL)
6088 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6089 osd->od_svname, ss->ss_server_seq);
6091 OBD_FREE_PTR(osd->od_cl_seq);
6092 osd->od_cl_seq = NULL;
6096 if (ss->ss_node_id == 0) {
6097 /* If the OSD on the sequence controller(MDT0), then allocate
6098 * sequence here, otherwise allocate sequence after connected
6099 * to MDT0 (see mdt_register_lwp_callback()). */
6100 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6101 &osd->od_cl_seq->lcs_space, env);
6107 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6109 if (osd->od_cl_seq == NULL)
6112 seq_client_fini(osd->od_cl_seq);
6113 OBD_FREE_PTR(osd->od_cl_seq);
6114 osd->od_cl_seq = NULL;
6117 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6121 /* shutdown quota slave instance associated with the device */
6122 if (o->od_quota_slave != NULL) {
6123 qsd_fini(env, o->od_quota_slave);
6124 o->od_quota_slave = NULL;
6127 osd_fid_fini(env, o);
6132 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6136 if (o->od_mnt != NULL) {
6137 shrink_dcache_sb(osd_sb(o));
6138 osd_sync(env, &o->od_dt_dev);
6147 static int osd_mount(const struct lu_env *env,
6148 struct osd_device *o, struct lustre_cfg *cfg)
6150 const char *name = lustre_cfg_string(cfg, 0);
6151 const char *dev = lustre_cfg_string(cfg, 1);
6153 unsigned long page, s_flags, lmd_flags = 0;
6154 struct page *__page;
6155 struct file_system_type *type;
6156 char *options = NULL;
6158 struct osd_thread_info *info = osd_oti_get(env);
6159 struct lu_fid *fid = &info->oti_fid;
6160 struct inode *inode;
6161 int rc = 0, force_over_128tb = 0;
6164 if (o->od_mnt != NULL)
6167 if (strlen(dev) >= sizeof(o->od_mntdev))
6169 strcpy(o->od_mntdev, dev);
6171 str = lustre_cfg_string(cfg, 2);
6172 s_flags = simple_strtoul(str, NULL, 0);
6173 str = strstr(str, ":");
6175 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6176 opts = lustre_cfg_string(cfg, 3);
6178 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6179 CERROR("%s: device %s extents feature is not guaranteed to "
6180 "work on big-endian systems. Use \"bigendian_extents\" "
6181 "mount option to override.\n", name, dev);
6185 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
6186 force_over_128tb = 1;
6188 __page = alloc_page(GFP_IOFS);
6190 GOTO(out, rc = -ENOMEM);
6191 page = (unsigned long)page_address(__page);
6192 options = (char *)page;
6195 /* strip out the options for back compatiblity */
6196 static char *sout[] = {
6203 /* strip out option we processed in osd */
6204 "bigendian_extents",
6208 strcat(options, opts);
6209 for (rc = 0, str = options; sout[rc]; ) {
6210 char *op = strstr(str, sout[rc]);
6216 if (op == options || *(op - 1) == ',') {
6217 str = op + strlen(sout[rc]);
6218 if (*str == ',' || *str == '\0') {
6219 *str == ',' ? str++ : str;
6220 memmove(op, str, strlen(str) + 1);
6223 for (str = op; *str != ',' && *str != '\0'; str++)
6227 strncat(options, "user_xattr,acl", 14);
6230 /* Glom up mount options */
6231 if (*options != '\0')
6232 strcat(options, ",");
6233 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6235 type = get_fs_type("ldiskfs");
6237 CERROR("%s: cannot find ldiskfs module\n", name);
6238 GOTO(out, rc = -ENODEV);
6241 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6242 module_put(type->owner);
6244 if (IS_ERR(o->od_mnt)) {
6245 rc = PTR_ERR(o->od_mnt);
6247 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6251 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
6252 force_over_128tb == 0) {
6253 CERROR("%s: device %s LDISKFS does not support filesystems "
6254 "greater than 128TB and can cause data corruption. "
6255 "Use \"force_over_128tb\" mount option to override.\n",
6257 GOTO(out, rc = -EINVAL);
6260 #ifdef HAVE_DEV_SET_RDONLY
6261 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6262 CERROR("%s: underlying device %s is marked as read-only. "
6263 "Setup failed\n", name, dev);
6264 GOTO(out_mnt, rc = -EROFS);
6268 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6269 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6270 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6271 GOTO(out_mnt, rc = -EINVAL);
6274 #ifdef LDISKFS_MOUNT_DIRDATA
6275 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6276 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6277 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6278 else if (!o->od_is_ost)
6279 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6280 "enabling the dirdata feature. If you do not want to "
6281 "downgrade to Lustre-1.x again, you can enable it via "
6282 "'tune2fs -O dirdata device'\n", name, dev);
6284 inode = osd_sb(o)->s_root->d_inode;
6285 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6286 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6288 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6292 if (lmd_flags & LMD_FLG_NOSCRUB)
6303 __free_page(__page);
6308 static struct lu_device *osd_device_fini(const struct lu_env *env,
6309 struct lu_device *d)
6311 struct osd_device *o = osd_dev(d);
6314 osd_shutdown(env, o);
6316 osd_scrub_cleanup(env, o);
6317 osd_obj_map_fini(o);
6323 static int osd_device_init0(const struct lu_env *env,
6324 struct osd_device *o,
6325 struct lustre_cfg *cfg)
6327 struct lu_device *l = osd2lu_dev(o);
6328 struct osd_thread_info *info;
6332 /* if the module was re-loaded, env can loose its keys */
6333 rc = lu_env_refill((struct lu_env *) env);
6336 info = osd_oti_get(env);
6339 l->ld_ops = &osd_lu_ops;
6340 o->od_dt_dev.dd_ops = &osd_dt_ops;
6342 spin_lock_init(&o->od_osfs_lock);
6343 mutex_init(&o->od_otable_mutex);
6344 INIT_LIST_HEAD(&o->od_orphan_list);
6346 o->od_read_cache = 1;
6347 o->od_writethrough_cache = 1;
6348 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6350 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6351 sizeof(o->od_svname));
6352 if (cplen >= sizeof(o->od_svname)) {
6357 o->od_index = -1; /* -1 means index is invalid */
6358 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6359 if (rc == LDD_F_SV_TYPE_OST)
6362 o->od_full_scrub_ratio = OFSR_DEFAULT;
6363 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6364 rc = osd_mount(env, o, cfg);
6368 rc = osd_obj_map_init(env, o);
6372 rc = lu_site_init(&o->od_site, l);
6374 GOTO(out_compat, rc);
6375 o->od_site.ls_bottom_dev = l;
6377 rc = lu_site_init_finish(&o->od_site);
6381 INIT_LIST_HEAD(&o->od_ios_list);
6382 /* setup scrub, including OI files initialization */
6383 rc = osd_scrub_setup(env, o);
6387 rc = osd_procfs_init(o, o->od_svname);
6389 CERROR("%s: can't initialize procfs: rc = %d\n",
6391 GOTO(out_scrub, rc);
6394 LASSERT(l->ld_site->ls_linkage.next != NULL);
6395 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6397 /* initialize quota slave instance */
6398 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6400 if (IS_ERR(o->od_quota_slave)) {
6401 rc = PTR_ERR(o->od_quota_slave);
6402 o->od_quota_slave = NULL;
6403 GOTO(out_procfs, rc);
6411 osd_scrub_cleanup(env, o);
6413 lu_site_fini(&o->od_site);
6415 osd_obj_map_fini(o);
6422 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6423 struct lu_device_type *t,
6424 struct lustre_cfg *cfg)
6426 struct osd_device *o;
6431 return ERR_PTR(-ENOMEM);
6433 rc = dt_device_init(&o->od_dt_dev, t);
6435 /* Because the ctx might be revived in dt_device_init,
6436 * refill the env here */
6437 lu_env_refill((struct lu_env *)env);
6438 rc = osd_device_init0(env, o, cfg);
6440 dt_device_fini(&o->od_dt_dev);
6443 if (unlikely(rc != 0))
6446 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6449 static struct lu_device *osd_device_free(const struct lu_env *env,
6450 struct lu_device *d)
6452 struct osd_device *o = osd_dev(d);
6455 /* XXX: make osd top device in order to release reference */
6456 d->ld_site->ls_top_dev = d;
6457 lu_site_purge(env, d->ld_site, -1);
6458 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6459 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6460 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6462 lu_site_fini(&o->od_site);
6463 dt_device_fini(&o->od_dt_dev);
6468 static int osd_process_config(const struct lu_env *env,
6469 struct lu_device *d, struct lustre_cfg *cfg)
6471 struct osd_device *o = osd_dev(d);
6475 switch (cfg->lcfg_command) {
6477 rc = osd_mount(env, o, cfg);
6480 lu_dev_del_linkage(d->ld_site, d);
6481 rc = osd_shutdown(env, o);
6484 LASSERT(&o->od_dt_dev);
6485 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6486 cfg, &o->od_dt_dev);
6487 if (rc > 0 || rc == -ENOSYS)
6488 rc = class_process_proc_param(PARAM_OST,
6489 lprocfs_osd_obd_vars,
6490 cfg, &o->od_dt_dev);
6499 static int osd_recovery_complete(const struct lu_env *env,
6500 struct lu_device *d)
6502 struct osd_device *osd = osd_dev(d);
6506 if (osd->od_quota_slave == NULL)
6509 /* start qsd instance on recovery completion, this notifies the quota
6510 * slave code that we are about to process new requests now */
6511 rc = qsd_start(env, osd->od_quota_slave);
6516 * we use exports to track all osd users
6518 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6519 struct obd_device *obd, struct obd_uuid *cluuid,
6520 struct obd_connect_data *data, void *localdata)
6522 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6523 struct lustre_handle conn;
6527 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6529 rc = class_connect(&conn, obd, cluuid);
6533 *exp = class_conn2export(&conn);
6535 spin_lock(&osd->od_osfs_lock);
6537 spin_unlock(&osd->od_osfs_lock);
6543 * once last export (we don't count self-export) disappeared
6544 * osd can be released
6546 static int osd_obd_disconnect(struct obd_export *exp)
6548 struct obd_device *obd = exp->exp_obd;
6549 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6550 int rc, release = 0;
6553 /* Only disconnect the underlying layers on the final disconnect. */
6554 spin_lock(&osd->od_osfs_lock);
6556 if (osd->od_connects == 0)
6558 spin_unlock(&osd->od_osfs_lock);
6560 rc = class_disconnect(exp); /* bz 9811 */
6562 if (rc == 0 && release)
6563 class_manual_cleanup(obd);
6567 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6568 struct lu_device *dev)
6570 struct osd_device *osd = osd_dev(dev);
6571 struct lr_server_data *lsd =
6572 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6576 if (osd->od_quota_slave != NULL) {
6577 /* set up quota slave objects */
6578 result = qsd_prepare(env, osd->od_quota_slave);
6583 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6584 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6585 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6586 osd->od_index_in_idif = 1;
6588 osd->od_index_in_idif = 0;
6589 result = osd_register_proc_index_in_idif(osd);
6594 osd->od_index_in_idif = 1;
6598 result = osd_fid_init(env, osd);
6603 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6604 struct lu_fid *fid, struct md_op_data *op_data)
6606 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6608 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6611 static const struct lu_object_operations osd_lu_obj_ops = {
6612 .loo_object_init = osd_object_init,
6613 .loo_object_delete = osd_object_delete,
6614 .loo_object_release = osd_object_release,
6615 .loo_object_free = osd_object_free,
6616 .loo_object_print = osd_object_print,
6617 .loo_object_invariant = osd_object_invariant
6620 const struct lu_device_operations osd_lu_ops = {
6621 .ldo_object_alloc = osd_object_alloc,
6622 .ldo_process_config = osd_process_config,
6623 .ldo_recovery_complete = osd_recovery_complete,
6624 .ldo_prepare = osd_prepare,
6627 static const struct lu_device_type_operations osd_device_type_ops = {
6628 .ldto_init = osd_type_init,
6629 .ldto_fini = osd_type_fini,
6631 .ldto_start = osd_type_start,
6632 .ldto_stop = osd_type_stop,
6634 .ldto_device_alloc = osd_device_alloc,
6635 .ldto_device_free = osd_device_free,
6637 .ldto_device_init = osd_device_init,
6638 .ldto_device_fini = osd_device_fini
6641 static struct lu_device_type osd_device_type = {
6642 .ldt_tags = LU_DEVICE_DT,
6643 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6644 .ldt_ops = &osd_device_type_ops,
6645 .ldt_ctx_tags = LCT_LOCAL,
6648 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6650 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6651 struct super_block *sb = osd_sb(osd);
6653 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6657 * lprocfs legacy support.
6659 static struct obd_ops osd_obd_device_ops = {
6660 .o_owner = THIS_MODULE,
6661 .o_connect = osd_obd_connect,
6662 .o_disconnect = osd_obd_disconnect,
6663 .o_fid_alloc = osd_fid_alloc,
6664 .o_health_check = osd_health_check,
6667 static int __init osd_init(void)
6671 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6672 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6673 /* please, try to keep osd_thread_info smaller than a page */
6674 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6679 rc = lu_kmem_init(ldiskfs_caches);
6683 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6684 lprocfs_osd_module_vars,
6685 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6687 lu_kmem_fini(ldiskfs_caches);
6691 static void __exit osd_exit(void)
6693 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6694 lu_kmem_fini(ldiskfs_caches);
6697 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6698 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6699 MODULE_VERSION(LUSTRE_VERSION_STRING);
6700 MODULE_LICENSE("GPL");
6702 module_init(osd_init);
6703 module_exit(osd_exit);