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, 2014, 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 int osd_trans_declare_op2rb[] = {
126 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
127 [OSD_OT_PUNCH] = OSD_OT_MAX,
128 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
129 [OSD_OT_CREATE] = OSD_OT_DESTROY,
130 [OSD_OT_DESTROY] = OSD_OT_CREATE,
131 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
132 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
133 [OSD_OT_WRITE] = OSD_OT_WRITE,
134 [OSD_OT_INSERT] = OSD_OT_DELETE,
135 [OSD_OT_DELETE] = OSD_OT_INSERT,
136 [OSD_OT_QUOTA] = OSD_OT_MAX,
139 static int osd_has_index(const struct osd_object *obj)
141 return obj->oo_dt.do_index_ops != NULL;
144 static int osd_object_invariant(const struct lu_object *l)
146 return osd_invariant(osd_obj(l));
150 * Concurrency: doesn't matter
154 * Concurrency: doesn't matter
156 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
158 struct osd_thread_info *oti = osd_oti_get(env);
159 return oti->oti_w_locks > 0 && o->oo_owner == env;
163 * Concurrency: doesn't access mutable data
165 static int osd_root_get(const struct lu_env *env,
166 struct dt_device *dev, struct lu_fid *f)
168 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
173 * OSD object methods.
177 * Concurrency: no concurrent access is possible that early in object
180 static struct lu_object *osd_object_alloc(const struct lu_env *env,
181 const struct lu_object_header *hdr,
184 struct osd_object *mo;
190 l = &mo->oo_dt.do_lu;
191 dt_object_init(&mo->oo_dt, NULL, d);
192 mo->oo_dt.do_ops = &osd_obj_ea_ops;
193 l->lo_ops = &osd_lu_obj_ops;
194 init_rwsem(&mo->oo_sem);
195 init_rwsem(&mo->oo_ext_idx_sem);
196 spin_lock_init(&mo->oo_guard);
203 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
204 struct dentry *dentry, struct lustre_mdt_attrs *lma)
208 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
209 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
210 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
212 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
213 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
215 lustre_lma_swab(lma);
216 /* Check LMA compatibility */
217 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
218 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
219 "for fid = "DFID", ino = %lu\n",
220 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
221 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
222 PFID(&lma->lma_self_fid), inode->i_ino);
225 } else if (rc == 0) {
233 * retrieve object from backend ext fs.
235 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
236 struct osd_inode_id *id)
238 struct inode *inode = NULL;
240 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
242 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
243 id->oii_ino, PTR_ERR(inode));
244 } else if (id->oii_gen != OSD_OII_NOGEN &&
245 inode->i_generation != id->oii_gen) {
246 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
247 "i_generation = %u\n",
248 id->oii_ino, id->oii_gen, inode->i_generation);
250 inode = ERR_PTR(-ESTALE);
251 } else if (inode->i_nlink == 0) {
252 /* due to parallel readdir and unlink,
253 * we can have dead inode here. */
254 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
256 inode = ERR_PTR(-ESTALE);
257 } else if (is_bad_inode(inode)) {
258 CWARN("%.16s: bad inode: ino = %u\n",
259 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
261 inode = ERR_PTR(-ENOENT);
263 if (id->oii_gen == OSD_OII_NOGEN)
264 osd_id_gen(id, inode->i_ino, inode->i_generation);
266 /* Do not update file c/mtime in ldiskfs.
267 * NB: we don't have any lock to protect this because we don't
268 * have reference on osd_object now, but contention with
269 * another lookup + attr_set can't happen in the tiny window
270 * between if (...) and set S_NOCMTIME. */
271 if (!(inode->i_flags & S_NOCMTIME))
272 inode->i_flags |= S_NOCMTIME;
277 static struct inode *
278 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
279 struct osd_inode_id *id, struct lu_fid *fid)
281 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
285 inode = osd_iget(info, dev, id);
289 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
291 *fid = lma->lma_self_fid;
292 } else if (rc == -ENODATA) {
293 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
294 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
296 lu_igif_build(fid, inode->i_ino, inode->i_generation);
305 * \retval +v: new filter_fid, does not contain self-fid
306 * \retval 0: filter_fid_old, contains self-fid
307 * \retval -v: other failure cases
309 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
310 struct dentry *dentry, struct lu_fid *fid)
312 struct filter_fid_old *ff = &info->oti_ff;
313 struct ost_id *ostid = &info->oti_ostid;
316 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
317 if (rc == sizeof(*ff)) {
319 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
320 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
321 /* XXX: should use real OST index in the future. LU-3569 */
322 ostid_to_fid(fid, ostid, 0);
323 } else if (rc == sizeof(struct filter_fid)) {
325 } else if (rc >= 0) {
332 static int osd_lma_self_repair(struct osd_thread_info *info,
333 struct osd_device *osd, struct inode *inode,
334 const struct lu_fid *fid, __u32 compat)
339 LASSERT(current->journal_info == NULL);
341 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
342 osd_dto_credits_noquota[DTO_XATTR_SET]);
345 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
350 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
352 CWARN("%s: cannot self repair the LMA: rc = %d\n",
354 ldiskfs_journal_stop(jh);
358 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
360 struct osd_thread_info *info = osd_oti_get(env);
361 struct osd_device *osd = osd_obj2dev(obj);
362 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
363 struct inode *inode = obj->oo_inode;
364 struct dentry *dentry = &info->oti_obj_dentry;
365 struct lu_fid *fid = NULL;
366 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
370 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
371 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
372 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
373 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
374 fid = &lma->lma_self_fid;
375 rc = osd_get_idif(info, inode, dentry, fid);
376 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
377 /* For the given OST-object, if it has neither LMA nor
378 * FID in XATTR_NAME_FID, then the given FID (which is
379 * contained in the @obj, from client RPC for locating
380 * the OST-object) is trusted. We use it to generate
382 osd_lma_self_repair(info, osd, inode, rfid,
388 if (unlikely(rc == -ENODATA))
396 lustre_lma_swab(lma);
397 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
398 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
399 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
400 "fid = "DFID", ino = %lu\n", osd_name(osd),
401 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
402 PFID(rfid), inode->i_ino);
405 fid = &lma->lma_self_fid;
409 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
410 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
411 struct ost_id *oi = &info->oti_ostid;
412 struct lu_fid *fid1 = &info->oti_fid3;
413 __u32 idx = fid_idif_ost_idx(rfid);
415 /* For old IDIF, the OST index is not part of the IDIF,
416 * Means that different OSTs may have the same IDIFs.
417 * Under such case, we need to make some compatible
418 * check to make sure to trigger OI scrub properly. */
419 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
420 /* Given @rfid is new, LMA is old. */
421 fid_to_ostid(fid, oi);
422 ostid_to_fid(fid1, oi, idx);
423 if (lu_fid_eq(fid1, rfid)) {
424 if (osd->od_index_in_idif)
425 osd_lma_self_repair(info, osd,
439 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
440 const struct lu_fid *fid,
441 const struct lu_object_conf *conf)
443 struct osd_thread_info *info;
444 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
445 struct osd_device *dev;
446 struct osd_idmap_cache *oic;
447 struct osd_inode_id *id;
449 struct osd_scrub *scrub;
450 struct scrub_file *sf;
454 bool in_cache = false;
455 bool triggered = false;
458 LINVRNT(osd_invariant(obj));
459 LASSERT(obj->oo_inode == NULL);
460 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
463 scrub = &dev->od_scrub;
464 sf = &scrub->os_file;
465 info = osd_oti_get(env);
467 oic = &info->oti_cache;
469 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
472 /* For the object is created as locking anchor, or for the object to
473 * be created on disk. No need to osd_oi_lookup() at here because FID
474 * shouldn't never be re-used, if it's really a duplicate FID from
475 * unexpected reason, we should be able to detect it later by calling
476 * do_create->osd_oi_insert(). */
477 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
478 GOTO(out, result = 0);
480 /* Search order: 1. per-thread cache. */
481 if (lu_fid_eq(fid, &oic->oic_fid) &&
482 likely(oic->oic_dev == dev)) {
489 if (!list_empty(&scrub->os_inconsistent_items)) {
490 /* Search order: 2. OI scrub pending list. */
491 result = osd_oii_lookup(dev, fid, id);
496 /* Search order: 3. OI files. */
497 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
498 if (result == -ENOENT) {
499 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
500 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
502 GOTO(out, result = 0);
513 inode = osd_iget(info, dev, id);
515 result = PTR_ERR(inode);
516 if (result != -ENOENT && result != -ESTALE)
520 fid_zero(&oic->oic_fid);
522 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
524 GOTO(out, result = (result == -ENOENT ? 0 : result));
526 /* The OI mapping is there, but the inode is NOT there.
527 * Two possible cases for that:
529 * 1) Backup/restore caused the OI invalid.
530 * 2) Someone unlinked the object but NOT removed
531 * the OI mapping, such as mount target device
532 * as ldiskfs, and modify something directly.
534 * Generally, when the device is mounted, it will
535 * auto check whether the system is restored from
536 * file-level backup or not. We trust such detect
537 * to distinguish the 1st case from the 2nd case. */
538 if (!(scrub->os_file.sf_flags & SF_INCONSISTENT))
539 GOTO(out, result = 0);
542 if (unlikely(triggered))
543 GOTO(out, result = saved);
546 if (thread_is_running(&scrub->os_thread)) {
547 result = -EINPROGRESS;
548 } else if (!dev->od_noscrub) {
549 /* Since we do not know the right OI mapping, we have
550 * to trigger OI scrub to scan the whole device. */
551 result = osd_scrub_start(dev, SS_AUTO_FULL |
552 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
553 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI "
554 "scrub by RPC for "DFID", rc = %d [1]\n",
555 osd_name(dev), PFID(fid), result);
556 if (result == 0 || result == -EALREADY)
557 result = -EINPROGRESS;
562 /* We still have chance to get the valid inode: for the
563 * object which is referenced by remote name entry, the
564 * object on the local MDT will be linked under the dir
565 * of "/REMOTE_PARENT_DIR" with its FID string as name.
567 * We do not know whether the object for the given FID
568 * is referenced by some remote name entry or not, and
569 * especially for DNE II, a multiple-linked object may
570 * have many name entries reside on many MDTs.
572 * To simplify the operation, OSD will not distinguish
573 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
574 * only happened for the RPC from other MDT during the
575 * OI scrub, or for the client side RPC with FID only,
576 * such as FID to path, or from old connected client. */
578 result = osd_lookup_in_remote_parent(info, dev, fid, id);
584 GOTO(out, result = saved);
587 obj->oo_inode = inode;
588 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
590 result = osd_check_lma(env, obj);
593 obj->oo_inode = NULL;
595 if (result != -EREMCHG)
599 fid_zero(&oic->oic_fid);
601 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
605 if (result != -ENOENT)
608 if (!in_oi && (fid_is_norm(fid) || fid_is_igif(fid)) &&
609 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
613 GOTO(out, result = 0);
616 obj->oo_compat_dot_created = 1;
617 obj->oo_compat_dotdot_created = 1;
619 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
620 GOTO(out, result = 0);
622 LASSERT(obj->oo_hl_head == NULL);
623 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
624 if (obj->oo_hl_head == NULL) {
625 obj->oo_inode = NULL;
627 GOTO(out, result = -ENOMEM);
629 GOTO(out, result = 0);
632 LINVRNT(osd_invariant(obj));
637 * Concurrency: shouldn't matter.
639 static void osd_object_init0(struct osd_object *obj)
641 LASSERT(obj->oo_inode != NULL);
642 obj->oo_dt.do_body_ops = &osd_body_ops;
643 obj->oo_dt.do_lu.lo_header->loh_attr |=
644 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
648 * Concurrency: no concurrent access is possible that early in object
651 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
652 const struct lu_object_conf *conf)
654 struct osd_object *obj = osd_obj(l);
657 LINVRNT(osd_invariant(obj));
659 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
660 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
661 l->lo_header->loh_attr |= LOHA_EXISTS;
665 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
666 obj->oo_dt.do_body_ops = &osd_body_ops_new;
667 if (result == 0 && obj->oo_inode != NULL)
668 osd_object_init0(obj);
670 LINVRNT(osd_invariant(obj));
675 * Concurrency: no concurrent access is possible that late in object
678 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
680 struct osd_object *obj = osd_obj(l);
682 LINVRNT(osd_invariant(obj));
684 dt_object_fini(&obj->oo_dt);
685 if (obj->oo_hl_head != NULL)
686 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
691 * Concurrency: no concurrent access is possible that late in object
694 static void osd_index_fini(struct osd_object *o)
696 struct iam_container *bag;
698 if (o->oo_dir != NULL) {
699 bag = &o->oo_dir->od_container;
700 if (o->oo_inode != NULL) {
701 if (bag->ic_object == o->oo_inode)
702 iam_container_fini(bag);
704 OBD_FREE_PTR(o->oo_dir);
710 * Concurrency: no concurrent access is possible that late in object
711 * life-cycle (for all existing callers, that is. New callers have to provide
712 * their own locking.)
714 static int osd_inode_unlinked(const struct inode *inode)
716 return inode->i_nlink == 0;
720 OSD_TXN_OI_DELETE_CREDITS = 20,
721 OSD_TXN_INODE_DELETE_CREDITS = 20
728 #if OSD_THANDLE_STATS
730 * Set time when the handle is allocated
732 static void osd_th_alloced(struct osd_thandle *oth)
734 oth->oth_alloced = cfs_time_current();
738 * Set time when the handle started
740 static void osd_th_started(struct osd_thandle *oth)
742 oth->oth_started = cfs_time_current();
746 * Helper function to convert time interval to microseconds packed in
749 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
753 cfs_duration_usec(cfs_time_sub(end, start), &val);
754 return val.tv_sec * 1000000 + val.tv_usec;
758 * Check whether the we deal with this handle for too long.
760 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
761 cfs_time_t alloced, cfs_time_t started,
764 cfs_time_t now = cfs_time_current();
766 LASSERT(dev != NULL);
768 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
769 interval_to_usec(alloced, started));
770 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
771 interval_to_usec(started, closed));
772 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
773 interval_to_usec(closed, now));
775 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
776 CWARN("transaction handle %p was open for too long: "
778 "alloced "CFS_TIME_T" ,"
779 "started "CFS_TIME_T" ,"
780 "closed "CFS_TIME_T"\n",
781 oth, now, alloced, started, closed);
782 libcfs_debug_dumpstack(NULL);
786 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
788 cfs_time_t __closed = cfs_time_current(); \
789 cfs_time_t __alloced = oth->oth_alloced; \
790 cfs_time_t __started = oth->oth_started; \
793 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
796 #else /* OSD_THANDLE_STATS */
798 #define osd_th_alloced(h) do {} while(0)
799 #define osd_th_started(h) do {} while(0)
800 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
802 #endif /* OSD_THANDLE_STATS */
805 * Concurrency: doesn't access mutable data.
807 static int osd_param_is_not_sane(const struct osd_device *dev,
808 const struct thandle *th)
810 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
812 return oh->ot_credits > osd_transaction_size(dev);
816 * Concurrency: shouldn't matter.
818 static void osd_trans_commit_cb(struct super_block *sb,
819 struct ldiskfs_journal_cb_entry *jcb, int error)
821 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
822 struct thandle *th = &oh->ot_super;
823 struct lu_device *lud = &th->th_dev->dd_lu_dev;
824 struct dt_txn_commit_cb *dcb, *tmp;
826 LASSERT(oh->ot_handle == NULL);
829 CERROR("transaction @0x%p commit error: %d\n", th, error);
831 dt_txn_hook_commit(th);
833 /* call per-transaction callbacks if any */
834 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
836 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
837 "commit callback entry: magic=%x name='%s'\n",
838 dcb->dcb_magic, dcb->dcb_name);
839 list_del_init(&dcb->dcb_linkage);
840 dcb->dcb_func(NULL, th, dcb, error);
843 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
847 lu_context_exit(&th->th_ctx);
848 lu_context_fini(&th->th_ctx);
852 static struct thandle *osd_trans_create(const struct lu_env *env,
855 struct osd_thread_info *oti = osd_oti_get(env);
856 struct osd_iobuf *iobuf = &oti->oti_iobuf;
857 struct osd_thandle *oh;
861 /* on pending IO in this thread should left from prev. request */
862 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
864 th = ERR_PTR(-ENOMEM);
865 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
867 oh->ot_quota_trans = &oti->oti_quota_trans;
868 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
872 th->th_tags = LCT_TX_HANDLE;
874 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
875 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
878 memset(oti->oti_declare_ops, 0,
879 sizeof(oti->oti_declare_ops));
880 memset(oti->oti_declare_ops_cred, 0,
881 sizeof(oti->oti_declare_ops_cred));
882 memset(oti->oti_declare_ops_used, 0,
883 sizeof(oti->oti_declare_ops_used));
888 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
890 struct osd_thread_info *oti = osd_oti_get(env);
891 struct osd_thandle *oh;
893 oh = container_of0(th, struct osd_thandle, ot_super);
896 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
897 oti->oti_declare_ops[OSD_OT_CREATE],
898 oti->oti_declare_ops_cred[OSD_OT_CREATE],
899 oti->oti_declare_ops_used[OSD_OT_CREATE],
900 oti->oti_declare_ops[OSD_OT_DESTROY],
901 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
902 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
903 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
904 oti->oti_declare_ops[OSD_OT_ATTR_SET],
905 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
906 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
907 oti->oti_declare_ops[OSD_OT_XATTR_SET],
908 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
909 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
910 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
911 oti->oti_declare_ops[OSD_OT_WRITE],
912 oti->oti_declare_ops_cred[OSD_OT_WRITE],
913 oti->oti_declare_ops_used[OSD_OT_WRITE],
914 oti->oti_declare_ops[OSD_OT_PUNCH],
915 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
916 oti->oti_declare_ops_used[OSD_OT_PUNCH],
917 oti->oti_declare_ops[OSD_OT_QUOTA],
918 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
919 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
920 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
921 oti->oti_declare_ops[OSD_OT_INSERT],
922 oti->oti_declare_ops_cred[OSD_OT_INSERT],
923 oti->oti_declare_ops_used[OSD_OT_INSERT],
924 oti->oti_declare_ops[OSD_OT_DELETE],
925 oti->oti_declare_ops_cred[OSD_OT_DELETE],
926 oti->oti_declare_ops_used[OSD_OT_DELETE]);
927 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
928 oti->oti_declare_ops[OSD_OT_REF_ADD],
929 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
930 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
931 oti->oti_declare_ops[OSD_OT_REF_DEL],
932 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
933 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
937 * Concurrency: shouldn't matter.
939 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
942 struct osd_thread_info *oti = osd_oti_get(env);
943 struct osd_device *dev = osd_dt_dev(d);
945 struct osd_thandle *oh;
950 LASSERT(current->journal_info == NULL);
952 oh = container_of0(th, struct osd_thandle, ot_super);
954 LASSERT(oh->ot_handle == NULL);
956 rc = dt_txn_hook_start(env, d, th);
960 if (unlikely(osd_param_is_not_sane(dev, th))) {
961 static unsigned long last_printed;
962 static int last_credits;
964 CWARN("%.16s: too many transaction credits (%d > %d)\n",
965 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
967 osd_journal(dev)->j_max_transaction_buffers);
969 osd_trans_dump_creds(env, th);
971 if (last_credits != oh->ot_credits &&
972 time_after(jiffies, last_printed +
973 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
974 libcfs_debug_dumpstack(NULL);
975 last_credits = oh->ot_credits;
976 last_printed = jiffies;
978 /* XXX Limit the credits to 'max_transaction_buffers', and
979 * let the underlying filesystem to catch the error if
980 * we really need so many credits.
982 * This should be removed when we can calculate the
983 * credits precisely. */
984 oh->ot_credits = osd_transaction_size(dev);
988 * XXX temporary stuff. Some abstraction layer should
991 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
995 LASSERT(oti->oti_txns == 0);
996 lu_context_init(&th->th_ctx, th->th_tags);
997 lu_context_enter(&th->th_ctx);
999 lu_device_get(&d->dd_lu_dev);
1000 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1011 static int osd_seq_exists(const struct lu_env *env,
1012 struct osd_device *osd, u64 seq)
1014 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1015 struct seq_server_site *ss = osd_seq_site(osd);
1019 LASSERT(ss != NULL);
1020 LASSERT(ss->ss_server_fld != NULL);
1022 rc = osd_fld_lookup(env, osd, seq, range);
1025 CERROR("%s: can't lookup FLD sequence "LPX64
1026 ": rc = %d\n", osd_name(osd), seq, rc);
1030 RETURN(ss->ss_node_id == range->lsr_index);
1033 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1035 struct dt_txn_commit_cb *dcb;
1036 struct dt_txn_commit_cb *tmp;
1038 /* call per-transaction stop callbacks if any */
1039 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1041 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1042 "commit callback entry: magic=%x name='%s'\n",
1043 dcb->dcb_magic, dcb->dcb_name);
1044 list_del_init(&dcb->dcb_linkage);
1045 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1050 * Concurrency: shouldn't matter.
1052 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1056 struct osd_thandle *oh;
1057 struct osd_thread_info *oti = osd_oti_get(env);
1058 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1059 struct osd_device *osd = osd_dt_dev(th->th_dev);
1060 struct qsd_instance *qsd = osd->od_quota_slave;
1061 struct lquota_trans *qtrans;
1064 oh = container_of0(th, struct osd_thandle, ot_super);
1066 qtrans = oh->ot_quota_trans;
1067 oh->ot_quota_trans = NULL;
1069 if (oh->ot_handle != NULL) {
1070 handle_t *hdl = oh->ot_handle;
1073 * add commit callback
1074 * notice we don't do this in osd_trans_start()
1075 * as underlying transaction can change during truncate
1077 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1080 LASSERT(oti->oti_txns == 1);
1083 rc = dt_txn_hook_stop(env, th);
1085 CERROR("%s: failed in transaction hook: rc = %d\n",
1088 osd_trans_stop_cb(oh, rc);
1089 /* hook functions might modify th_sync */
1090 hdl->h_sync = th->th_sync;
1092 oh->ot_handle = NULL;
1093 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1095 CERROR("%s: failed to stop transaction: rc = %d\n",
1098 osd_trans_stop_cb(oh, th->th_result);
1102 /* inform the quota slave device that the transaction is stopping */
1103 qsd_op_end(env, qsd, qtrans);
1105 /* as we want IO to journal and data IO be concurrent, we don't block
1106 * awaiting data IO completion in osd_do_bio(), instead we wait here
1107 * once transaction is submitted to the journal. all reqular requests
1108 * don't do direct IO (except read/write), thus this wait_event becomes
1111 * IMPORTANT: we have to wait till any IO submited by the thread is
1112 * completed otherwise iobuf may be corrupted by different request
1114 wait_event(iobuf->dr_wait,
1115 atomic_read(&iobuf->dr_numreqs) == 0);
1116 osd_fini_iobuf(osd, iobuf);
1118 rc = iobuf->dr_error;
1123 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1125 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1128 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1129 LASSERT(&dcb->dcb_func != NULL);
1130 if (dcb->dcb_flags & DCB_TRANS_STOP)
1131 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1133 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1139 * Called just before object is freed. Releases all resources except for
1140 * object itself (that is released by osd_object_free()).
1142 * Concurrency: no concurrent access is possible that late in object
1145 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1147 struct osd_object *obj = osd_obj(l);
1148 struct inode *inode = obj->oo_inode;
1150 LINVRNT(osd_invariant(obj));
1153 * If object is unlinked remove fid->ino mapping from object index.
1156 osd_index_fini(obj);
1157 if (inode != NULL) {
1158 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1159 qid_t uid = i_uid_read(inode);
1160 qid_t gid = i_gid_read(inode);
1163 obj->oo_inode = NULL;
1166 struct osd_thread_info *info = osd_oti_get(env);
1167 struct lquota_id_info *qi = &info->oti_qi;
1169 /* Release granted quota to master if necessary */
1170 qi->lqi_id.qid_uid = uid;
1171 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1173 qi->lqi_id.qid_uid = gid;
1174 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1180 * Concurrency: ->loo_object_release() is called under site spin-lock.
1182 static void osd_object_release(const struct lu_env *env,
1183 struct lu_object *l)
1188 * Concurrency: shouldn't matter.
1190 static int osd_object_print(const struct lu_env *env, void *cookie,
1191 lu_printer_t p, const struct lu_object *l)
1193 struct osd_object *o = osd_obj(l);
1194 struct iam_descr *d;
1196 if (o->oo_dir != NULL)
1197 d = o->oo_dir->od_container.ic_descr;
1200 return (*p)(env, cookie,
1201 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1203 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1204 o->oo_inode ? o->oo_inode->i_generation : 0,
1205 d ? d->id_ops->id_name : "plain");
1208 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1211 * Concurrency: shouldn't matter.
1213 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1214 struct obd_statfs *sfs)
1216 struct osd_device *osd = osd_dt_dev(d);
1217 struct super_block *sb = osd_sb(osd);
1218 struct kstatfs *ksfs;
1221 if (unlikely(osd->od_mnt == NULL))
1222 return -EINPROGRESS;
1224 /* osd_lproc.c call this without env, allocate ksfs for that case */
1225 if (unlikely(env == NULL)) {
1226 OBD_ALLOC_PTR(ksfs);
1230 ksfs = &osd_oti_get(env)->oti_ksfs;
1233 spin_lock(&osd->od_osfs_lock);
1234 result = sb->s_op->statfs(sb->s_root, ksfs);
1235 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1236 statfs_pack(sfs, ksfs);
1237 if (unlikely(sb->s_flags & MS_RDONLY))
1238 sfs->os_state = OS_STATE_READONLY;
1239 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1240 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1241 sfs->os_maxbytes = sb->s_maxbytes;
1243 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1245 spin_unlock(&osd->od_osfs_lock);
1247 if (unlikely(env == NULL))
1250 /* Reserve a small amount of space for local objects like last_rcvd,
1251 * llog, quota files, ... */
1252 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1255 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1256 /** Take out metadata overhead for indirect blocks */
1257 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1264 * Estimate space needed for file creations. We assume the largest filename
1265 * which is 2^64 - 1, hence a filename of 20 chars.
1266 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1268 #ifdef __LDISKFS_DIR_REC_LEN
1269 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1271 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1275 * Concurrency: doesn't access mutable data.
1277 static void osd_conf_get(const struct lu_env *env,
1278 const struct dt_device *dev,
1279 struct dt_device_param *param)
1281 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1285 * XXX should be taken from not-yet-existing fs abstraction layer.
1287 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1288 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1289 param->ddp_block_shift = sb->s_blocksize_bits;
1290 param->ddp_mount_type = LDD_MT_LDISKFS;
1291 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1292 param->ddp_maxbytes = sb->s_maxbytes;
1294 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1295 /* Overhead estimate should be fairly accurate, so we really take a tiny
1296 * error margin which also avoids fragmenting the filesystem too much */
1297 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1298 /* inode are statically allocated, so per-inode space consumption
1299 * is the space consumed by the directory entry */
1300 param->ddp_inodespace = PER_OBJ_USAGE;
1301 /* per-fragment overhead to be used by the client code */
1302 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1303 param->ddp_mntopts = 0;
1304 if (test_opt(sb, XATTR_USER))
1305 param->ddp_mntopts |= MNTOPT_USERXATTR;
1306 if (test_opt(sb, POSIX_ACL))
1307 param->ddp_mntopts |= MNTOPT_ACL;
1309 /* LOD might calculate the max stripe count based on max_ea_size,
1310 * so we need take account in the overhead as well,
1311 * xattr_header + magic + xattr_entry_head */
1312 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1313 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1315 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1316 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1317 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1321 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1325 * Concurrency: shouldn't matter.
1327 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1331 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1333 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1335 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1336 LUSTRE_OSD_LDISKFS_NAME, rc);
1342 * Start commit for OSD device.
1344 * An implementation of dt_commit_async method for OSD device.
1345 * Asychronously starts underlayng fs sync and thereby a transaction
1348 * \param env environment
1349 * \param d dt device
1351 * \see dt_device_operations
1353 static int osd_commit_async(const struct lu_env *env,
1354 struct dt_device *d)
1356 struct super_block *s = osd_sb(osd_dt_dev(d));
1359 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1360 RETURN(s->s_op->sync_fs(s, 0));
1364 * Concurrency: shouldn't matter.
1367 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1369 struct super_block *sb = osd_sb(osd_dt_dev(d));
1370 struct block_device *dev = sb->s_bdev;
1371 #ifdef HAVE_DEV_SET_RDONLY
1372 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1375 int rc = -EOPNOTSUPP;
1379 #ifdef HAVE_DEV_SET_RDONLY
1380 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1382 if (jdev && (jdev != dev)) {
1383 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1385 dev_set_rdonly(jdev);
1387 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1388 dev_set_rdonly(dev);
1390 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1391 osd_dt_dev(d)->od_svname, (long)dev, rc);
1397 * Note: we do not count into QUOTA here.
1398 * If we mount with --data_journal we may need more.
1400 const int osd_dto_credits_noquota[DTO_NR] = {
1403 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1404 * SINGLEDATA_TRANS_BLOCKS(8)
1405 * XXX Note: maybe iam need more, since iam have more level than
1408 [DTO_INDEX_INSERT] = 16,
1411 * just modify a single entry, probably merge few within a block
1413 [DTO_INDEX_DELETE] = 1,
1417 [DTO_INDEX_UPDATE] = 16,
1419 * 4(inode, inode bits, groups, GDT)
1420 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1422 [DTO_OBJECT_CREATE] = 4,
1424 * 4(inode, inode bits, groups, GDT)
1425 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1427 [DTO_OBJECT_DELETE] = 4,
1429 * Attr set credits (inode)
1431 [DTO_ATTR_SET_BASE] = 1,
1433 * Xattr set. The same as xattr of EXT3.
1434 * DATA_TRANS_BLOCKS(14)
1435 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1436 * are also counted in. Do not know why?
1438 [DTO_XATTR_SET] = 14,
1440 * credits for inode change during write.
1442 [DTO_WRITE_BASE] = 3,
1444 * credits for single block write.
1446 [DTO_WRITE_BLOCK] = 14,
1448 * Attr set credits for chown.
1449 * This is extra credits for setattr, and it is null without quota
1451 [DTO_ATTR_SET_CHOWN] = 0
1454 static const struct dt_device_operations osd_dt_ops = {
1455 .dt_root_get = osd_root_get,
1456 .dt_statfs = osd_statfs,
1457 .dt_trans_create = osd_trans_create,
1458 .dt_trans_start = osd_trans_start,
1459 .dt_trans_stop = osd_trans_stop,
1460 .dt_trans_cb_add = osd_trans_cb_add,
1461 .dt_conf_get = osd_conf_get,
1462 .dt_sync = osd_sync,
1464 .dt_commit_async = osd_commit_async,
1467 static void osd_object_read_lock(const struct lu_env *env,
1468 struct dt_object *dt, unsigned role)
1470 struct osd_object *obj = osd_dt_obj(dt);
1471 struct osd_thread_info *oti = osd_oti_get(env);
1473 LINVRNT(osd_invariant(obj));
1475 LASSERT(obj->oo_owner != env);
1476 down_read_nested(&obj->oo_sem, role);
1478 LASSERT(obj->oo_owner == NULL);
1482 static void osd_object_write_lock(const struct lu_env *env,
1483 struct dt_object *dt, unsigned role)
1485 struct osd_object *obj = osd_dt_obj(dt);
1486 struct osd_thread_info *oti = osd_oti_get(env);
1488 LINVRNT(osd_invariant(obj));
1490 LASSERT(obj->oo_owner != env);
1491 down_write_nested(&obj->oo_sem, role);
1493 LASSERT(obj->oo_owner == NULL);
1494 obj->oo_owner = env;
1498 static void osd_object_read_unlock(const struct lu_env *env,
1499 struct dt_object *dt)
1501 struct osd_object *obj = osd_dt_obj(dt);
1502 struct osd_thread_info *oti = osd_oti_get(env);
1504 LINVRNT(osd_invariant(obj));
1506 LASSERT(oti->oti_r_locks > 0);
1508 up_read(&obj->oo_sem);
1511 static void osd_object_write_unlock(const struct lu_env *env,
1512 struct dt_object *dt)
1514 struct osd_object *obj = osd_dt_obj(dt);
1515 struct osd_thread_info *oti = osd_oti_get(env);
1517 LINVRNT(osd_invariant(obj));
1519 LASSERT(obj->oo_owner == env);
1520 LASSERT(oti->oti_w_locks > 0);
1522 obj->oo_owner = NULL;
1523 up_write(&obj->oo_sem);
1526 static int osd_object_write_locked(const struct lu_env *env,
1527 struct dt_object *dt)
1529 struct osd_object *obj = osd_dt_obj(dt);
1531 LINVRNT(osd_invariant(obj));
1533 return obj->oo_owner == env;
1536 static struct timespec *osd_inode_time(const struct lu_env *env,
1537 struct inode *inode, __u64 seconds)
1539 struct osd_thread_info *oti = osd_oti_get(env);
1540 struct timespec *t = &oti->oti_time;
1542 t->tv_sec = seconds;
1544 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1549 static void osd_inode_getattr(const struct lu_env *env,
1550 struct inode *inode, struct lu_attr *attr)
1552 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1553 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1554 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1557 attr->la_atime = LTIME_S(inode->i_atime);
1558 attr->la_mtime = LTIME_S(inode->i_mtime);
1559 attr->la_ctime = LTIME_S(inode->i_ctime);
1560 attr->la_mode = inode->i_mode;
1561 attr->la_size = i_size_read(inode);
1562 attr->la_blocks = inode->i_blocks;
1563 attr->la_uid = i_uid_read(inode);
1564 attr->la_gid = i_gid_read(inode);
1565 attr->la_flags = LDISKFS_I(inode)->i_flags;
1566 attr->la_nlink = inode->i_nlink;
1567 attr->la_rdev = inode->i_rdev;
1568 attr->la_blksize = 1 << inode->i_blkbits;
1569 attr->la_blkbits = inode->i_blkbits;
1572 static int osd_attr_get(const struct lu_env *env,
1573 struct dt_object *dt,
1574 struct lu_attr *attr)
1576 struct osd_object *obj = osd_dt_obj(dt);
1578 if (!dt_object_exists(dt))
1581 LASSERT(!dt_object_remote(dt));
1582 LINVRNT(osd_invariant(obj));
1584 spin_lock(&obj->oo_guard);
1585 osd_inode_getattr(env, obj->oo_inode, attr);
1586 spin_unlock(&obj->oo_guard);
1590 static int osd_declare_attr_set(const struct lu_env *env,
1591 struct dt_object *dt,
1592 const struct lu_attr *attr,
1593 struct thandle *handle)
1595 struct osd_thandle *oh;
1596 struct osd_object *obj;
1597 struct osd_thread_info *info = osd_oti_get(env);
1598 struct lquota_id_info *qi = &info->oti_qi;
1606 LASSERT(dt != NULL);
1607 LASSERT(handle != NULL);
1609 obj = osd_dt_obj(dt);
1610 LASSERT(osd_invariant(obj));
1612 oh = container_of0(handle, struct osd_thandle, ot_super);
1613 LASSERT(oh->ot_handle == NULL);
1615 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1616 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1618 if (attr == NULL || obj->oo_inode == NULL)
1621 bspace = obj->oo_inode->i_blocks;
1622 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1623 bspace = toqb(bspace);
1625 /* Changing ownership is always preformed by super user, it should not
1628 * We still need to call the osd_declare_qid() to calculate the journal
1629 * credits for updating quota accounting files and to trigger quota
1630 * space adjustment once the operation is completed.*/
1631 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
1633 uid = i_uid_read(obj->oo_inode);
1634 qi->lqi_type = USRQUOTA;
1635 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
1636 /* inode accounting */
1637 qi->lqi_is_blk = false;
1639 /* one more inode for the new uid ... */
1640 qi->lqi_id.qid_uid = attr->la_uid;
1642 /* Reserve credits for the new uid */
1643 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1644 if (rc == -EDQUOT || rc == -EINPROGRESS)
1649 /* and one less inode for the current uid */
1650 qi->lqi_id.qid_uid = uid;
1652 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1653 if (rc == -EDQUOT || rc == -EINPROGRESS)
1658 /* block accounting */
1659 qi->lqi_is_blk = true;
1661 /* more blocks for the new uid ... */
1662 qi->lqi_id.qid_uid = attr->la_uid;
1663 qi->lqi_space = bspace;
1665 * Credits for the new uid has been reserved, re-use "obj"
1666 * to save credit reservation.
1668 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1669 if (rc == -EDQUOT || rc == -EINPROGRESS)
1674 /* and finally less blocks for the current uid */
1675 qi->lqi_id.qid_uid = uid;
1676 qi->lqi_space = -bspace;
1677 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1678 if (rc == -EDQUOT || rc == -EINPROGRESS)
1684 gid = i_gid_read(obj->oo_inode);
1685 qi->lqi_type = GRPQUOTA;
1686 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
1688 /* inode accounting */
1689 qi->lqi_is_blk = false;
1691 /* one more inode for the new gid ... */
1692 qi->lqi_id.qid_gid = attr->la_gid;
1694 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1695 if (rc == -EDQUOT || rc == -EINPROGRESS)
1700 /* and one less inode for the current gid */
1701 qi->lqi_id.qid_gid = gid;
1703 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1704 if (rc == -EDQUOT || rc == -EINPROGRESS)
1709 /* block accounting */
1710 qi->lqi_is_blk = true;
1712 /* more blocks for the new gid ... */
1713 qi->lqi_id.qid_gid = attr->la_gid;
1714 qi->lqi_space = bspace;
1715 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1716 if (rc == -EDQUOT || rc == -EINPROGRESS)
1721 /* and finally less blocks for the current gid */
1722 qi->lqi_id.qid_gid = gid;
1723 qi->lqi_space = -bspace;
1724 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1725 if (rc == -EDQUOT || rc == -EINPROGRESS)
1734 static int osd_inode_setattr(const struct lu_env *env,
1735 struct inode *inode, const struct lu_attr *attr)
1737 __u64 bits = attr->la_valid;
1739 /* Only allow set size for regular file */
1740 if (!S_ISREG(inode->i_mode))
1741 bits &= ~(LA_SIZE | LA_BLOCKS);
1746 if (bits & LA_ATIME)
1747 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1748 if (bits & LA_CTIME)
1749 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1750 if (bits & LA_MTIME)
1751 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1752 if (bits & LA_SIZE) {
1753 LDISKFS_I(inode)->i_disksize = attr->la_size;
1754 i_size_write(inode, attr->la_size);
1758 /* OSD should not change "i_blocks" which is used by quota.
1759 * "i_blocks" should be changed by ldiskfs only. */
1760 if (bits & LA_BLOCKS)
1761 inode->i_blocks = attr->la_blocks;
1764 inode->i_mode = (inode->i_mode & S_IFMT) |
1765 (attr->la_mode & ~S_IFMT);
1767 i_uid_write(inode, attr->la_uid);
1769 i_gid_write(inode, attr->la_gid);
1770 if (bits & LA_NLINK)
1771 set_nlink(inode, attr->la_nlink);
1773 inode->i_rdev = attr->la_rdev;
1775 if (bits & LA_FLAGS) {
1776 /* always keep S_NOCMTIME */
1777 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1783 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1785 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
1786 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
1790 ll_vfs_dq_init(inode);
1792 if (attr->la_valid & LA_UID)
1793 iattr.ia_valid |= ATTR_UID;
1794 if (attr->la_valid & LA_GID)
1795 iattr.ia_valid |= ATTR_GID;
1796 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
1797 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
1799 rc = ll_vfs_dq_transfer(inode, &iattr);
1801 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1802 "enforcement enabled on the ldiskfs "
1803 "filesystem?\n", inode->i_sb->s_id, rc);
1810 static int osd_attr_set(const struct lu_env *env,
1811 struct dt_object *dt,
1812 const struct lu_attr *attr,
1813 struct thandle *handle)
1815 struct osd_object *obj = osd_dt_obj(dt);
1816 struct inode *inode;
1819 if (!dt_object_exists(dt))
1822 LASSERT(handle != NULL);
1823 LASSERT(!dt_object_remote(dt));
1824 LASSERT(osd_invariant(obj));
1826 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1828 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1829 struct osd_thread_info *oti = osd_oti_get(env);
1830 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1831 struct lu_fid *fid1 = &oti->oti_fid;
1832 struct osd_inode_id *id = &oti->oti_id;
1833 struct iam_path_descr *ipd;
1834 struct iam_container *bag;
1835 struct osd_thandle *oh;
1838 fid_cpu_to_be(fid1, fid0);
1839 memset(id, 1, sizeof(*id));
1840 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1841 fid0)->oi_dir.od_container;
1842 ipd = osd_idx_ipd_get(env, bag);
1843 if (unlikely(ipd == NULL))
1846 oh = container_of0(handle, struct osd_thandle, ot_super);
1847 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1848 (const struct iam_rec *)id, ipd);
1849 osd_ipd_put(env, bag, ipd);
1850 return(rc > 0 ? 0 : rc);
1853 inode = obj->oo_inode;
1855 rc = osd_quota_transfer(inode, attr);
1859 spin_lock(&obj->oo_guard);
1860 rc = osd_inode_setattr(env, inode, attr);
1861 spin_unlock(&obj->oo_guard);
1864 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1866 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
1871 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
1872 struct osd_object *obj,
1873 const char *name, const int namelen)
1875 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1878 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1879 umode_t mode, struct dt_allocation_hint *hint,
1883 struct osd_device *osd = osd_obj2dev(obj);
1884 struct osd_thandle *oth;
1885 struct dt_object *parent = NULL;
1886 struct inode *inode;
1888 LINVRNT(osd_invariant(obj));
1889 LASSERT(obj->oo_inode == NULL);
1890 LASSERT(obj->oo_hl_head == NULL);
1892 if (S_ISDIR(mode) && ldiskfs_pdo) {
1893 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1894 if (obj->oo_hl_head == NULL)
1898 oth = container_of(th, struct osd_thandle, ot_super);
1899 LASSERT(oth->ot_handle->h_transaction != NULL);
1901 if (hint != NULL && hint->dah_parent != NULL &&
1902 !dt_object_remote(hint->dah_parent))
1903 parent = hint->dah_parent;
1905 inode = ldiskfs_create_inode(oth->ot_handle,
1906 parent ? osd_dt_obj(parent)->oo_inode :
1907 osd_sb(osd)->s_root->d_inode,
1909 if (!IS_ERR(inode)) {
1910 /* Do not update file c/mtime in ldiskfs. */
1911 inode->i_flags |= S_NOCMTIME;
1913 /* For new created object, it must be consistent,
1914 * and it is unnecessary to scrub against it. */
1915 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1917 obj->oo_inode = inode;
1920 if (obj->oo_hl_head != NULL) {
1921 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1922 obj->oo_hl_head = NULL;
1924 result = PTR_ERR(inode);
1926 LINVRNT(osd_invariant(obj));
1934 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1935 struct lu_attr *attr,
1936 struct dt_allocation_hint *hint,
1937 struct dt_object_format *dof,
1941 struct osd_thandle *oth;
1942 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1944 LASSERT(S_ISDIR(attr->la_mode));
1946 oth = container_of(th, struct osd_thandle, ot_super);
1947 LASSERT(oth->ot_handle->h_transaction != NULL);
1948 result = osd_mkfile(info, obj, mode, hint, th);
1953 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1954 struct lu_attr *attr,
1955 struct dt_allocation_hint *hint,
1956 struct dt_object_format *dof,
1960 struct osd_thandle *oth;
1961 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1963 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1965 LASSERT(S_ISREG(attr->la_mode));
1967 oth = container_of(th, struct osd_thandle, ot_super);
1968 LASSERT(oth->ot_handle->h_transaction != NULL);
1970 result = osd_mkfile(info, obj, mode, hint, th);
1972 LASSERT(obj->oo_inode != NULL);
1973 if (feat->dif_flags & DT_IND_VARKEY)
1974 result = iam_lvar_create(obj->oo_inode,
1975 feat->dif_keysize_max,
1977 feat->dif_recsize_max,
1980 result = iam_lfix_create(obj->oo_inode,
1981 feat->dif_keysize_max,
1983 feat->dif_recsize_max,
1990 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1991 struct lu_attr *attr,
1992 struct dt_allocation_hint *hint,
1993 struct dt_object_format *dof,
1996 LASSERT(S_ISREG(attr->la_mode));
1997 return osd_mkfile(info, obj, (attr->la_mode &
1998 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2001 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2002 struct lu_attr *attr,
2003 struct dt_allocation_hint *hint,
2004 struct dt_object_format *dof,
2007 LASSERT(S_ISLNK(attr->la_mode));
2008 return osd_mkfile(info, obj, (attr->la_mode &
2009 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2012 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2013 struct lu_attr *attr,
2014 struct dt_allocation_hint *hint,
2015 struct dt_object_format *dof,
2018 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2021 LINVRNT(osd_invariant(obj));
2022 LASSERT(obj->oo_inode == NULL);
2023 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2024 S_ISFIFO(mode) || S_ISSOCK(mode));
2026 result = osd_mkfile(info, obj, mode, hint, th);
2028 LASSERT(obj->oo_inode != NULL);
2030 * This inode should be marked dirty for i_rdev. Currently
2031 * that is done in the osd_attr_init().
2033 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2036 LINVRNT(osd_invariant(obj));
2040 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2042 struct dt_allocation_hint *hint,
2043 struct dt_object_format *dof,
2046 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2048 osd_obj_type_f result;
2064 result = osd_mk_index;
2075 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2076 struct dt_object *parent, struct dt_object *child,
2081 ah->dah_parent = parent;
2082 ah->dah_mode = child_mode;
2085 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2086 struct lu_attr *attr, struct dt_object_format *dof)
2088 struct inode *inode = obj->oo_inode;
2089 __u64 valid = attr->la_valid;
2092 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2094 if (dof->dof_type != DFT_NODE)
2095 attr->la_valid &= ~LA_RDEV;
2096 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2097 attr->la_valid &= ~LA_ATIME;
2098 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2099 attr->la_valid &= ~LA_CTIME;
2100 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2101 attr->la_valid &= ~LA_MTIME;
2103 result = osd_quota_transfer(inode, attr);
2107 if (attr->la_valid != 0) {
2108 result = osd_inode_setattr(info->oti_env, inode, attr);
2110 * The osd_inode_setattr() should always succeed here. The
2111 * only error that could be returned is EDQUOT when we are
2112 * trying to change the UID or GID of the inode. However, this
2113 * should not happen since quota enforcement is no longer
2114 * enabled on ldiskfs (lquota takes care of it).
2116 LASSERTF(result == 0, "%d\n", result);
2117 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2120 attr->la_valid = valid;
2124 * Helper function for osd_object_create()
2126 * \retval 0, on success
2128 static int __osd_object_create(struct osd_thread_info *info,
2129 struct osd_object *obj, struct lu_attr *attr,
2130 struct dt_allocation_hint *hint,
2131 struct dt_object_format *dof,
2137 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2139 /* we drop umask so that permissions we pass are not affected */
2140 umask = current->fs->umask;
2141 current->fs->umask = 0;
2143 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2145 if (likely(obj->oo_inode != NULL)) {
2146 LASSERT(obj->oo_inode->i_state & I_NEW);
2148 /* Unlock the inode before attr initialization to avoid
2149 * unnecessary dqget operations. LU-6378 */
2150 unlock_new_inode(obj->oo_inode);
2153 if (likely(result == 0)) {
2154 osd_attr_init(info, obj, attr, dof);
2155 osd_object_init0(obj);
2158 /* restore previous umask value */
2159 current->fs->umask = umask;
2161 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2167 * Helper function for osd_object_create()
2169 * \retval 0, on success
2171 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2172 const struct lu_fid *fid, struct thandle *th)
2174 struct osd_thread_info *info = osd_oti_get(env);
2175 struct osd_inode_id *id = &info->oti_id;
2176 struct osd_device *osd = osd_obj2dev(obj);
2177 struct osd_thandle *oh;
2180 LASSERT(obj->oo_inode != NULL);
2182 oh = container_of0(th, struct osd_thandle, ot_super);
2183 LASSERT(oh->ot_handle);
2184 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2186 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2187 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2188 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2193 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2194 u64 seq, struct lu_seq_range *range)
2196 struct seq_server_site *ss = osd_seq_site(osd);
2198 if (fid_seq_is_idif(seq)) {
2199 fld_range_set_ost(range);
2200 range->lsr_index = idif_ost_idx(seq);
2204 if (!fid_seq_in_fldb(seq)) {
2205 fld_range_set_mdt(range);
2207 /* FIXME: If ss is NULL, it suppose not get lsr_index
2209 range->lsr_index = ss->ss_node_id;
2213 LASSERT(ss != NULL);
2214 fld_range_set_any(range);
2215 /* OSD will only do local fld lookup */
2216 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2220 * Concurrency: no external locking is necessary.
2222 static int osd_declare_object_create(const struct lu_env *env,
2223 struct dt_object *dt,
2224 struct lu_attr *attr,
2225 struct dt_allocation_hint *hint,
2226 struct dt_object_format *dof,
2227 struct thandle *handle)
2229 struct osd_thandle *oh;
2233 LASSERT(handle != NULL);
2235 oh = container_of0(handle, struct osd_thandle, ot_super);
2236 LASSERT(oh->ot_handle == NULL);
2238 /* EA object consumes more credits than regular object: osd_mk_index
2239 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2240 * leaf_node, could involves the block, block bitmap, groups, GDT
2241 * change for each block, so add 4 * 2 credits in that case. */
2242 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2243 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2244 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2245 /* Reuse idle OI block may cause additional one OI block
2247 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2248 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2253 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2254 osd_dt_obj(dt), false, NULL, false);
2261 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2262 struct lu_attr *attr,
2263 struct dt_allocation_hint *hint,
2264 struct dt_object_format *dof, struct thandle *th)
2266 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2267 struct osd_object *obj = osd_dt_obj(dt);
2268 struct osd_thread_info *info = osd_oti_get(env);
2272 if (dt_object_exists(dt))
2275 LINVRNT(osd_invariant(obj));
2276 LASSERT(!dt_object_remote(dt));
2277 LASSERT(osd_write_locked(env, obj));
2278 LASSERT(th != NULL);
2280 if (unlikely(fid_is_acct(fid)))
2281 /* Quota files can't be created from the kernel any more,
2282 * 'tune2fs -O quota' will take care of creating them */
2285 result = __osd_object_create(info, obj, attr, hint, dof, th);
2287 result = __osd_oi_insert(env, obj, fid, th);
2289 LASSERT(ergo(result == 0,
2290 dt_object_exists(dt) && !dt_object_remote(dt)));
2292 LASSERT(osd_invariant(obj));
2297 * Called to destroy on-disk representation of the object
2299 * Concurrency: must be locked
2301 static int osd_declare_object_destroy(const struct lu_env *env,
2302 struct dt_object *dt,
2305 struct osd_object *obj = osd_dt_obj(dt);
2306 struct inode *inode = obj->oo_inode;
2307 struct osd_thandle *oh;
2311 oh = container_of0(th, struct osd_thandle, ot_super);
2312 LASSERT(oh->ot_handle == NULL);
2315 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2316 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2317 /* Recycle idle OI leaf may cause additional three OI blocks
2319 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2320 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2321 /* one less inode */
2322 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2323 -1, oh, obj, false, NULL, false);
2326 /* data to be truncated */
2327 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2328 0, oh, obj, true, NULL, false);
2332 static int osd_object_destroy(const struct lu_env *env,
2333 struct dt_object *dt,
2336 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2337 struct osd_object *obj = osd_dt_obj(dt);
2338 struct inode *inode = obj->oo_inode;
2339 struct osd_device *osd = osd_obj2dev(obj);
2340 struct osd_thandle *oh;
2344 oh = container_of0(th, struct osd_thandle, ot_super);
2345 LASSERT(oh->ot_handle);
2347 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2349 if (unlikely(fid_is_acct(fid)))
2352 if (S_ISDIR(inode->i_mode)) {
2353 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2354 inode->i_nlink == 2);
2355 /* it will check/delete the inode from remote parent,
2356 * how to optimize it? unlink performance impaction XXX */
2357 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2358 if (result != 0 && result != -ENOENT) {
2359 CERROR("%s: delete inode "DFID": rc = %d\n",
2360 osd_name(osd), PFID(fid), result);
2362 spin_lock(&obj->oo_guard);
2364 spin_unlock(&obj->oo_guard);
2365 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2368 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2370 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2373 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2374 /* XXX: add to ext3 orphan list */
2375 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2377 /* not needed in the cache anymore */
2378 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2379 obj->oo_destroyed = 1;
2385 * Put the fid into lustre_mdt_attrs, and then place the structure
2386 * inode's ea. This fid should not be altered during the life time
2389 * \retval +ve, on success
2390 * \retval -ve, on error
2392 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2394 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2395 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2397 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2401 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2404 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2407 lustre_lma_init(lma, fid, compat, incompat);
2408 lustre_lma_swab(lma);
2410 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2412 /* LMA may already exist, but we need to check that all the
2413 * desired compat/incompat flags have been added. */
2414 if (unlikely(rc == -EEXIST)) {
2415 if (compat == 0 && incompat == 0)
2418 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2419 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2424 lustre_lma_swab(lma);
2425 if (!(~lma->lma_compat & compat) &&
2426 !(~lma->lma_incompat & incompat))
2429 lma->lma_compat |= compat;
2430 lma->lma_incompat |= incompat;
2431 lustre_lma_swab(lma);
2432 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2433 sizeof(*lma), XATTR_REPLACE);
2440 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2441 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2442 * To have compatilibility with 1.8 ldiskfs driver we need to have
2443 * magic number at start of fid data.
2444 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2447 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2448 const struct lu_fid *fid)
2450 if (!fid_is_namespace_visible(fid) ||
2451 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2452 param->edp_magic = 0;
2456 param->edp_magic = LDISKFS_LUFID_MAGIC;
2457 param->edp_len = sizeof(struct lu_fid) + 1;
2458 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2462 * Try to read the fid from inode ea into dt_rec.
2464 * \param fid object fid.
2466 * \retval 0 on success
2468 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2469 __u32 ino, struct lu_fid *fid,
2470 struct osd_inode_id *id)
2472 struct osd_thread_info *info = osd_oti_get(env);
2473 struct inode *inode;
2476 osd_id_gen(id, ino, OSD_OII_NOGEN);
2477 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2479 RETURN(PTR_ERR(inode));
2485 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2487 struct inode *parent_dir,
2488 const struct lu_fid *dot_fid,
2489 const struct lu_fid *dot_dot_fid,
2490 struct osd_thandle *oth)
2492 struct ldiskfs_dentry_param *dot_ldp;
2493 struct ldiskfs_dentry_param *dot_dot_ldp;
2495 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2496 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2498 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2499 dot_ldp->edp_magic = 0;
2500 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2501 dir, dot_ldp, dot_dot_ldp);
2505 * Create an local agent inode for remote entry
2507 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2508 struct osd_device *osd,
2509 struct osd_object *pobj,
2510 const struct lu_fid *fid,
2514 struct osd_thread_info *info = osd_oti_get(env);
2515 struct inode *local;
2516 struct osd_thandle *oh;
2521 oh = container_of(th, struct osd_thandle, ot_super);
2522 LASSERT(oh->ot_handle->h_transaction != NULL);
2524 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2525 if (IS_ERR(local)) {
2526 CERROR("%s: create local error %d\n", osd_name(osd),
2527 (int)PTR_ERR(local));
2531 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2532 unlock_new_inode(local);
2534 /* Set special LMA flag for local agent inode */
2535 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2537 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2538 osd_name(osd), PFID(fid), rc);
2539 RETURN(ERR_PTR(rc));
2545 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2546 lu_object_fid(&pobj->oo_dt.do_lu),
2549 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2550 osd_name(osd), PFID(fid), rc);
2551 RETURN(ERR_PTR(rc));
2558 * Delete local agent inode for remote entry
2560 static int osd_delete_local_agent_inode(const struct lu_env *env,
2561 struct osd_device *osd,
2562 const struct lu_fid *fid,
2563 __u32 ino, struct osd_thandle *oh)
2565 struct osd_thread_info *oti = osd_oti_get(env);
2566 struct osd_inode_id *id = &oti->oti_id;
2567 struct inode *inode;
2570 id->oii_ino = le32_to_cpu(ino);
2571 id->oii_gen = OSD_OII_NOGEN;
2572 inode = osd_iget(oti, osd, id);
2573 if (IS_ERR(inode)) {
2574 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2575 PFID(fid), id->oii_ino, id->oii_gen);
2576 RETURN(PTR_ERR(inode));
2580 mark_inode_dirty(inode);
2581 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2582 osd_name(osd), PFID(fid), inode->i_ino);
2588 * OSD layer object create function for interoperability mode (b11826).
2589 * This is mostly similar to osd_object_create(). Only difference being, fid is
2590 * inserted into inode ea here.
2592 * \retval 0, on success
2593 * \retval -ve, on error
2595 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2596 struct lu_attr *attr,
2597 struct dt_allocation_hint *hint,
2598 struct dt_object_format *dof,
2601 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2602 struct osd_object *obj = osd_dt_obj(dt);
2603 struct osd_thread_info *info = osd_oti_get(env);
2608 if (dt_object_exists(dt))
2611 LASSERT(osd_invariant(obj));
2612 LASSERT(!dt_object_remote(dt));
2613 LASSERT(osd_write_locked(env, obj));
2614 LASSERT(th != NULL);
2616 if (unlikely(fid_is_acct(fid)))
2617 /* Quota files can't be created from the kernel any more,
2618 * 'tune2fs -O quota' will take care of creating them */
2621 result = __osd_object_create(info, obj, attr, hint, dof, th);
2623 if (fid_is_idif(fid) &&
2624 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
2625 struct lu_fid *tfid = &info->oti_fid;
2626 struct ost_id *oi = &info->oti_ostid;
2628 fid_to_ostid(fid, oi);
2629 ostid_to_fid(tfid, oi, 0);
2630 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
2631 LMAC_FID_ON_OST, 0);
2633 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2634 fid_is_on_ost(info, osd_obj2dev(obj),
2635 fid, OI_CHECK_FLD) ?
2636 LMAC_FID_ON_OST : 0, 0);
2641 result = __osd_oi_insert(env, obj, fid, th);
2643 LASSERT(ergo(result == 0,
2644 dt_object_exists(dt) && !dt_object_remote(dt)));
2645 LINVRNT(osd_invariant(obj));
2649 static int osd_declare_object_ref_add(const struct lu_env *env,
2650 struct dt_object *dt,
2651 struct thandle *handle)
2653 struct osd_thandle *oh;
2655 /* it's possible that object doesn't exist yet */
2656 LASSERT(handle != NULL);
2658 oh = container_of0(handle, struct osd_thandle, ot_super);
2659 LASSERT(oh->ot_handle == NULL);
2661 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2662 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2668 * Concurrency: @dt is write locked.
2670 static int osd_object_ref_add(const struct lu_env *env,
2671 struct dt_object *dt, struct thandle *th)
2673 struct osd_object *obj = osd_dt_obj(dt);
2674 struct inode *inode = obj->oo_inode;
2675 struct osd_thandle *oh;
2678 if (!dt_object_exists(dt))
2681 LINVRNT(osd_invariant(obj));
2682 LASSERT(!dt_object_remote(dt));
2683 LASSERT(osd_write_locked(env, obj));
2684 LASSERT(th != NULL);
2686 oh = container_of0(th, struct osd_thandle, ot_super);
2687 LASSERT(oh->ot_handle != NULL);
2689 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2691 CDEBUG(D_INODE, DFID" increase nlink %d\n",
2692 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
2694 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2695 * (65000) subdirectories by storing "1" in i_nlink if the link count
2696 * would otherwise overflow. Directory tranversal tools understand
2697 * that (st_nlink == 1) indicates that the filesystem dose not track
2698 * hard links count on the directory, and will not abort subdirectory
2699 * scanning early once (st_nlink - 2) subdirs have been found.
2701 * This also has to properly handle the case of inodes with nlink == 0
2702 * in case they are being linked into the PENDING directory
2704 spin_lock(&obj->oo_guard);
2705 if (unlikely(inode->i_nlink == 0))
2706 /* inc_nlink from 0 may cause WARN_ON */
2707 set_nlink(inode, 1);
2709 ldiskfs_inc_count(oh->ot_handle, inode);
2710 if (!S_ISDIR(inode->i_mode))
2711 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2713 spin_unlock(&obj->oo_guard);
2715 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2716 LINVRNT(osd_invariant(obj));
2718 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
2723 static int osd_declare_object_ref_del(const struct lu_env *env,
2724 struct dt_object *dt,
2725 struct thandle *handle)
2727 struct osd_thandle *oh;
2729 LASSERT(!dt_object_remote(dt));
2730 LASSERT(handle != NULL);
2732 oh = container_of0(handle, struct osd_thandle, ot_super);
2733 LASSERT(oh->ot_handle == NULL);
2735 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2736 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2742 * Concurrency: @dt is write locked.
2744 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2747 struct osd_object *obj = osd_dt_obj(dt);
2748 struct inode *inode = obj->oo_inode;
2749 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2750 struct osd_thandle *oh;
2752 if (!dt_object_exists(dt))
2755 LINVRNT(osd_invariant(obj));
2756 LASSERT(!dt_object_remote(dt));
2757 LASSERT(osd_write_locked(env, obj));
2758 LASSERT(th != NULL);
2760 oh = container_of0(th, struct osd_thandle, ot_super);
2761 LASSERT(oh->ot_handle != NULL);
2763 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2765 spin_lock(&obj->oo_guard);
2766 /* That can be result of upgrade from old Lustre version and
2767 * applied only to local files. Just skip this ref_del call.
2768 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2769 if (inode->i_nlink == 0) {
2770 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2771 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2772 ", maybe an upgraded file? (LU-3915)\n",
2773 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2774 spin_unlock(&obj->oo_guard);
2778 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
2779 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
2781 ldiskfs_dec_count(oh->ot_handle, inode);
2782 spin_unlock(&obj->oo_guard);
2784 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2785 LINVRNT(osd_invariant(obj));
2787 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
2793 * Get the 64-bit version for an inode.
2795 static int osd_object_version_get(const struct lu_env *env,
2796 struct dt_object *dt, dt_obj_version_t *ver)
2798 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2800 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2801 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2802 *ver = LDISKFS_I(inode)->i_fs_version;
2807 * Concurrency: @dt is read locked.
2809 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2810 struct lu_buf *buf, const char *name)
2812 struct osd_object *obj = osd_dt_obj(dt);
2813 struct inode *inode = obj->oo_inode;
2814 struct osd_thread_info *info = osd_oti_get(env);
2815 struct dentry *dentry = &info->oti_obj_dentry;
2817 /* version get is not real XATTR but uses xattr API */
2818 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2819 /* for version we are just using xattr API but change inode
2821 if (buf->lb_len == 0)
2822 return sizeof(dt_obj_version_t);
2824 if (buf->lb_len < sizeof(dt_obj_version_t))
2827 osd_object_version_get(env, dt, buf->lb_buf);
2829 return sizeof(dt_obj_version_t);
2832 if (!dt_object_exists(dt))
2835 LASSERT(!dt_object_remote(dt));
2836 LASSERT(inode->i_op != NULL);
2837 LASSERT(inode->i_op->getxattr != NULL);
2839 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2843 static int osd_declare_xattr_set(const struct lu_env *env,
2844 struct dt_object *dt,
2845 const struct lu_buf *buf, const char *name,
2846 int fl, struct thandle *handle)
2848 struct osd_thandle *oh;
2850 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
2852 LASSERT(handle != NULL);
2854 oh = container_of0(handle, struct osd_thandle, ot_super);
2855 LASSERT(oh->ot_handle == NULL);
2857 if (strcmp(name, XATTR_NAME_LMA) == 0) {
2858 /* For non-upgrading case, the LMA is set first and
2859 * usually fit inode. But for upgrade case, the LMA
2860 * may be in another separated EA block. */
2861 if (!dt_object_exists(dt))
2863 else if (fl == LU_XATTR_REPLACE)
2867 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2871 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
2872 if (buf && buf->lb_len > sb->s_blocksize) {
2873 credits *= (buf->lb_len + sb->s_blocksize - 1) >>
2874 sb->s_blocksize_bits;
2877 * xattr set may involve inode quota change, reserve credits for
2878 * dquot_initialize()
2880 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
2883 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
2889 * Set the 64-bit version for object
2891 static void osd_object_version_set(const struct lu_env *env,
2892 struct dt_object *dt,
2893 dt_obj_version_t *new_version)
2895 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2897 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2898 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2900 LDISKFS_I(inode)->i_fs_version = *new_version;
2901 /** Version is set after all inode operations are finished,
2902 * so we should mark it dirty here */
2903 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2907 * Concurrency: @dt is write locked.
2909 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2910 const struct lu_buf *buf, const char *name, int fl,
2911 struct thandle *handle)
2913 struct osd_object *obj = osd_dt_obj(dt);
2914 struct inode *inode = obj->oo_inode;
2915 struct osd_thread_info *info = osd_oti_get(env);
2920 LASSERT(handle != NULL);
2922 /* version set is not real XATTR */
2923 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2924 /* for version we are just using xattr API but change inode
2926 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2927 osd_object_version_set(env, dt, buf->lb_buf);
2928 return sizeof(dt_obj_version_t);
2931 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
2932 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
2934 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2935 if (fl & LU_XATTR_REPLACE)
2936 fs_flags |= XATTR_REPLACE;
2938 if (fl & LU_XATTR_CREATE)
2939 fs_flags |= XATTR_CREATE;
2941 if (strcmp(name, XATTR_NAME_LMV) == 0) {
2942 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2945 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2949 lma->lma_incompat |= LMAI_STRIPED;
2950 lustre_lma_swab(lma);
2951 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2952 sizeof(*lma), XATTR_REPLACE);
2957 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
2958 strcmp(name, XATTR_NAME_LINK) == 0)
2961 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2963 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2969 * Concurrency: @dt is read locked.
2971 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2972 const struct lu_buf *buf)
2974 struct osd_object *obj = osd_dt_obj(dt);
2975 struct inode *inode = obj->oo_inode;
2976 struct osd_thread_info *info = osd_oti_get(env);
2977 struct dentry *dentry = &info->oti_obj_dentry;
2979 if (!dt_object_exists(dt))
2982 LASSERT(!dt_object_remote(dt));
2983 LASSERT(inode->i_op != NULL);
2984 LASSERT(inode->i_op->listxattr != NULL);
2986 dentry->d_inode = inode;
2987 dentry->d_sb = inode->i_sb;
2988 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2991 static int osd_declare_xattr_del(const struct lu_env *env,
2992 struct dt_object *dt, const char *name,
2993 struct thandle *handle)
2995 struct osd_thandle *oh;
2996 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
2998 LASSERT(!dt_object_remote(dt));
2999 LASSERT(handle != NULL);
3001 oh = container_of0(handle, struct osd_thandle, ot_super);
3002 LASSERT(oh->ot_handle == NULL);
3004 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3005 osd_dto_credits_noquota[DTO_XATTR_SET]);
3007 * xattr del may involve inode quota change, reserve credits for
3008 * dquot_initialize()
3010 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3016 * Concurrency: @dt is write locked.
3018 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3019 const char *name, struct thandle *handle)
3021 struct osd_object *obj = osd_dt_obj(dt);
3022 struct inode *inode = obj->oo_inode;
3023 struct osd_thread_info *info = osd_oti_get(env);
3024 struct dentry *dentry = &info->oti_obj_dentry;
3027 if (!dt_object_exists(dt))
3030 LASSERT(!dt_object_remote(dt));
3031 LASSERT(inode->i_op != NULL);
3032 LASSERT(inode->i_op->removexattr != NULL);
3033 LASSERT(handle != NULL);
3035 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3037 ll_vfs_dq_init(inode);
3038 dentry->d_inode = inode;
3039 dentry->d_sb = inode->i_sb;
3040 rc = inode->i_op->removexattr(dentry, name);
3041 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3045 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3046 __u64 start, __u64 end)
3048 struct osd_object *obj = osd_dt_obj(dt);
3049 struct inode *inode = obj->oo_inode;
3050 struct osd_thread_info *info = osd_oti_get(env);
3051 struct dentry *dentry = &info->oti_obj_dentry;
3052 struct file *file = &info->oti_file;
3057 dentry->d_inode = inode;
3058 dentry->d_sb = inode->i_sb;
3059 file->f_path.dentry = dentry;
3060 file->f_mapping = inode->i_mapping;
3061 file->f_op = inode->i_fop;
3062 set_file_inode(file, inode);
3064 rc = ll_vfs_fsync_range(file, start, end, 0);
3073 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3074 const struct dt_index_features *feat)
3076 struct iam_descr *descr;
3078 if (osd_object_is_root(o))
3079 return feat == &dt_directory_features;
3081 LASSERT(o->oo_dir != NULL);
3083 descr = o->oo_dir->od_container.ic_descr;
3084 if (feat == &dt_directory_features) {
3085 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3091 feat->dif_keysize_min <= descr->id_key_size &&
3092 descr->id_key_size <= feat->dif_keysize_max &&
3093 feat->dif_recsize_min <= descr->id_rec_size &&
3094 descr->id_rec_size <= feat->dif_recsize_max &&
3095 !(feat->dif_flags & (DT_IND_VARKEY |
3096 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3097 ergo(feat->dif_flags & DT_IND_UPDATE,
3098 1 /* XXX check that object (and file system) is
3103 static int osd_iam_container_init(const struct lu_env *env,
3104 struct osd_object *obj,
3105 struct osd_directory *dir)
3107 struct iam_container *bag = &dir->od_container;
3110 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3114 result = iam_container_setup(bag);
3116 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3118 iam_container_fini(bag);
3125 * Concurrency: no external locking is necessary.
3127 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3128 const struct dt_index_features *feat)
3132 struct osd_object *obj = osd_dt_obj(dt);
3134 LINVRNT(osd_invariant(obj));
3136 if (osd_object_is_root(obj)) {
3137 dt->do_index_ops = &osd_index_ea_ops;
3139 } else if (feat == &dt_directory_features) {
3140 dt->do_index_ops = &osd_index_ea_ops;
3141 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3146 } else if (unlikely(feat == &dt_otable_features)) {
3147 dt->do_index_ops = &osd_otable_ops;
3149 } else if (unlikely(feat == &dt_acct_features)) {
3150 dt->do_index_ops = &osd_acct_index_ops;
3153 } else if (!osd_has_index(obj)) {
3154 struct osd_directory *dir;
3159 spin_lock(&obj->oo_guard);
3160 if (obj->oo_dir == NULL)
3164 * Concurrent thread allocated container data.
3167 spin_unlock(&obj->oo_guard);
3169 * Now, that we have container data, serialize its
3172 down_write(&obj->oo_ext_idx_sem);
3174 * recheck under lock.
3176 if (!osd_has_index(obj))
3177 result = osd_iam_container_init(env, obj,
3181 up_write(&obj->oo_ext_idx_sem);
3189 if (result == 0 && skip_iam == 0) {
3190 if (!osd_iam_index_probe(env, obj, feat))
3193 LINVRNT(osd_invariant(obj));
3195 if (result == 0 && feat == &dt_quota_glb_features &&
3196 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3197 result = osd_quota_migration(env, dt);
3202 static int osd_otable_it_attr_get(const struct lu_env *env,
3203 struct dt_object *dt,
3204 struct lu_attr *attr)
3210 static const struct dt_object_operations osd_obj_ops = {
3211 .do_read_lock = osd_object_read_lock,
3212 .do_write_lock = osd_object_write_lock,
3213 .do_read_unlock = osd_object_read_unlock,
3214 .do_write_unlock = osd_object_write_unlock,
3215 .do_write_locked = osd_object_write_locked,
3216 .do_attr_get = osd_attr_get,
3217 .do_declare_attr_set = osd_declare_attr_set,
3218 .do_attr_set = osd_attr_set,
3219 .do_ah_init = osd_ah_init,
3220 .do_declare_create = osd_declare_object_create,
3221 .do_create = osd_object_create,
3222 .do_declare_destroy = osd_declare_object_destroy,
3223 .do_destroy = osd_object_destroy,
3224 .do_index_try = osd_index_try,
3225 .do_declare_ref_add = osd_declare_object_ref_add,
3226 .do_ref_add = osd_object_ref_add,
3227 .do_declare_ref_del = osd_declare_object_ref_del,
3228 .do_ref_del = osd_object_ref_del,
3229 .do_xattr_get = osd_xattr_get,
3230 .do_declare_xattr_set = osd_declare_xattr_set,
3231 .do_xattr_set = osd_xattr_set,
3232 .do_declare_xattr_del = osd_declare_xattr_del,
3233 .do_xattr_del = osd_xattr_del,
3234 .do_xattr_list = osd_xattr_list,
3235 .do_object_sync = osd_object_sync,
3239 * dt_object_operations for interoperability mode
3240 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3242 static const struct dt_object_operations osd_obj_ea_ops = {
3243 .do_read_lock = osd_object_read_lock,
3244 .do_write_lock = osd_object_write_lock,
3245 .do_read_unlock = osd_object_read_unlock,
3246 .do_write_unlock = osd_object_write_unlock,
3247 .do_write_locked = osd_object_write_locked,
3248 .do_attr_get = osd_attr_get,
3249 .do_declare_attr_set = osd_declare_attr_set,
3250 .do_attr_set = osd_attr_set,
3251 .do_ah_init = osd_ah_init,
3252 .do_declare_create = osd_declare_object_create,
3253 .do_create = osd_object_ea_create,
3254 .do_declare_destroy = osd_declare_object_destroy,
3255 .do_destroy = osd_object_destroy,
3256 .do_index_try = osd_index_try,
3257 .do_declare_ref_add = osd_declare_object_ref_add,
3258 .do_ref_add = osd_object_ref_add,
3259 .do_declare_ref_del = osd_declare_object_ref_del,
3260 .do_ref_del = osd_object_ref_del,
3261 .do_xattr_get = osd_xattr_get,
3262 .do_declare_xattr_set = osd_declare_xattr_set,
3263 .do_xattr_set = osd_xattr_set,
3264 .do_declare_xattr_del = osd_declare_xattr_del,
3265 .do_xattr_del = osd_xattr_del,
3266 .do_xattr_list = osd_xattr_list,
3267 .do_object_sync = osd_object_sync,
3270 static const struct dt_object_operations osd_obj_otable_it_ops = {
3271 .do_attr_get = osd_otable_it_attr_get,
3272 .do_index_try = osd_index_try,
3275 static int osd_index_declare_iam_delete(const struct lu_env *env,
3276 struct dt_object *dt,
3277 const struct dt_key *key,
3278 struct thandle *handle)
3280 struct osd_thandle *oh;
3282 oh = container_of0(handle, struct osd_thandle, ot_super);
3283 LASSERT(oh->ot_handle == NULL);
3285 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3286 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3292 * delete a (key, value) pair from index \a dt specified by \a key
3294 * \param dt osd index object
3295 * \param key key for index
3296 * \param rec record reference
3297 * \param handle transaction handler
3300 * \retval -ve failure
3302 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3303 const struct dt_key *key,
3304 struct thandle *handle)
3306 struct osd_thread_info *oti = osd_oti_get(env);
3307 struct osd_object *obj = osd_dt_obj(dt);
3308 struct osd_thandle *oh;
3309 struct iam_path_descr *ipd;
3310 struct iam_container *bag = &obj->oo_dir->od_container;
3314 if (!dt_object_exists(dt))
3317 LINVRNT(osd_invariant(obj));
3318 LASSERT(!dt_object_remote(dt));
3319 LASSERT(bag->ic_object == obj->oo_inode);
3320 LASSERT(handle != NULL);
3322 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3324 ipd = osd_idx_ipd_get(env, bag);
3325 if (unlikely(ipd == NULL))
3328 oh = container_of0(handle, struct osd_thandle, ot_super);
3329 LASSERT(oh->ot_handle != NULL);
3330 LASSERT(oh->ot_handle->h_transaction != NULL);
3332 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3333 /* swab quota uid/gid provided by caller */
3334 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3335 key = (const struct dt_key *)&oti->oti_quota_id;
3338 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3339 osd_ipd_put(env, bag, ipd);
3340 LINVRNT(osd_invariant(obj));
3341 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3345 static int osd_index_declare_ea_delete(const struct lu_env *env,
3346 struct dt_object *dt,
3347 const struct dt_key *key,
3348 struct thandle *handle)
3350 struct osd_thandle *oh;
3351 struct inode *inode;
3355 LASSERT(!dt_object_remote(dt));
3356 LASSERT(handle != NULL);
3358 oh = container_of0(handle, struct osd_thandle, ot_super);
3359 LASSERT(oh->ot_handle == NULL);
3361 /* due to DNE we may need to remove an agent inode */
3362 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3363 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3364 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3366 inode = osd_dt_obj(dt)->oo_inode;
3369 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3370 0, oh, osd_dt_obj(dt), true, NULL, false);
3374 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3377 struct osd_fid_pack *rec;
3380 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3381 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3382 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3383 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3389 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3390 const struct lu_fid *fid)
3392 struct seq_server_site *ss = osd_seq_site(osd);
3395 /* FID seqs not in FLDB, must be local seq */
3396 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3399 /* If FLD is not being initialized yet, it only happens during the
3400 * initialization, likely during mgs initialization, and we assume
3401 * this is local FID. */
3402 if (ss == NULL || ss->ss_server_fld == NULL)
3405 /* Only check the local FLDB here */
3406 if (osd_seq_exists(env, osd, fid_seq(fid)))
3413 * Index delete function for interoperability mode (b11826).
3414 * It will remove the directory entry added by osd_index_ea_insert().
3415 * This entry is needed to maintain name->fid mapping.
3417 * \param key, key i.e. file entry to be deleted
3419 * \retval 0, on success
3420 * \retval -ve, on error
3422 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3423 const struct dt_key *key, struct thandle *handle)
3425 struct osd_object *obj = osd_dt_obj(dt);
3426 struct inode *dir = obj->oo_inode;
3427 struct dentry *dentry;
3428 struct osd_thandle *oh;
3429 struct ldiskfs_dir_entry_2 *de = NULL;
3430 struct buffer_head *bh;
3431 struct htree_lock *hlock = NULL;
3432 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3433 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3437 if (!dt_object_exists(dt))
3440 LINVRNT(osd_invariant(obj));
3441 LASSERT(!dt_object_remote(dt));
3442 LASSERT(handle != NULL);
3444 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3446 oh = container_of(handle, struct osd_thandle, ot_super);
3447 LASSERT(oh->ot_handle != NULL);
3448 LASSERT(oh->ot_handle->h_transaction != NULL);
3450 ll_vfs_dq_init(dir);
3451 dentry = osd_child_dentry_get(env, obj,
3452 (char *)key, strlen((char *)key));
3454 if (obj->oo_hl_head != NULL) {
3455 hlock = osd_oti_get(env)->oti_hlock;
3456 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3457 dir, LDISKFS_HLOCK_DEL);
3459 down_write(&obj->oo_ext_idx_sem);
3462 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3466 /* If this is not the ".." entry, it might be a remote DNE
3467 * entry and we need to check if the FID is for a remote
3468 * MDT. If the FID is not in the directory entry (e.g.
3469 * upgraded 1.8 filesystem without dirdata enabled) then
3470 * we need to get the FID from the LMA. For a remote directory
3471 * there HAS to be an LMA, it cannot be an IGIF inode in this
3474 * Delete the entry before the agent inode in order to
3475 * simplify error handling. At worst an error after deleting
3476 * the entry first might leak the agent inode afterward. The
3477 * reverse would need filesystem abort in case of error deleting
3478 * the entry after the agent had been removed, or leave a
3479 * dangling entry pointing at a random inode. */
3480 if (strcmp((char *)key, dotdot) != 0) {
3481 LASSERT(de != NULL);
3482 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3483 /* If Fid is not in dentry, try to get it from LMA */
3484 if (rc == -ENODATA) {
3485 struct osd_inode_id *id;
3486 struct inode *inode;
3488 /* Before trying to get fid from the inode,
3489 * check whether the inode is valid.
3491 * If the inode has been deleted, do not go
3492 * ahead to do osd_ea_fid_get, which will set
3493 * the inode to bad inode, which might cause
3494 * the inode to be deleted uncorrectly */
3495 inode = ldiskfs_iget(osd_sb(osd),
3496 le32_to_cpu(de->inode));
3497 if (IS_ERR(inode)) {
3498 CDEBUG(D_INODE, "%s: "DFID"get inode"
3499 "error.\n", osd_name(osd),
3501 rc = PTR_ERR(inode);
3503 if (likely(inode->i_nlink != 0)) {
3504 id = &osd_oti_get(env)->oti_id;
3505 rc = osd_ea_fid_get(env, obj,
3506 le32_to_cpu(de->inode),
3509 CDEBUG(D_INFO, "%s: %u "DFID
3512 le32_to_cpu(de->inode),
3520 unlikely(osd_remote_fid(env, osd, fid)))
3521 /* Need to delete agent inode */
3522 ino = le32_to_cpu(de->inode);
3524 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3526 if (rc == 0 && unlikely(ino != 0)) {
3527 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3530 CERROR("%s: del local inode "DFID": rc = %d\n",
3531 osd_name(osd), PFID(fid), rc);
3537 ldiskfs_htree_unlock(hlock);
3539 up_write(&obj->oo_ext_idx_sem);
3544 /* For inode on the remote MDT, .. will point to
3545 * /Agent directory, Check whether it needs to delete
3546 * from agent directory */
3547 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3548 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3550 if (rc != 0 && rc != -ENOENT) {
3551 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3552 osd_name(osd), PFID(fid), rc);
3562 LASSERT(osd_invariant(obj));
3563 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3568 * Lookup index for \a key and copy record to \a rec.
3570 * \param dt osd index object
3571 * \param key key for index
3572 * \param rec record reference
3574 * \retval +ve success : exact mach
3575 * \retval 0 return record with key not greater than \a key
3576 * \retval -ve failure
3578 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3579 struct dt_rec *rec, const struct dt_key *key)
3581 struct osd_object *obj = osd_dt_obj(dt);
3582 struct iam_path_descr *ipd;
3583 struct iam_container *bag = &obj->oo_dir->od_container;
3584 struct osd_thread_info *oti = osd_oti_get(env);
3585 struct iam_iterator *it = &oti->oti_idx_it;
3586 struct iam_rec *iam_rec;
3590 if (!dt_object_exists(dt))
3593 LASSERT(osd_invariant(obj));
3594 LASSERT(!dt_object_remote(dt));
3595 LASSERT(bag->ic_object == obj->oo_inode);
3597 ipd = osd_idx_ipd_get(env, bag);
3601 /* got ipd now we can start iterator. */
3602 iam_it_init(it, bag, 0, ipd);
3604 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3605 /* swab quota uid/gid provided by caller */
3606 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3607 key = (const struct dt_key *)&oti->oti_quota_id;
3610 rc = iam_it_get(it, (struct iam_key *)key);
3612 if (S_ISDIR(obj->oo_inode->i_mode))
3613 iam_rec = (struct iam_rec *)oti->oti_ldp;
3615 iam_rec = (struct iam_rec *) rec;
3617 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3619 if (S_ISDIR(obj->oo_inode->i_mode))
3620 osd_fid_unpack((struct lu_fid *) rec,
3621 (struct osd_fid_pack *)iam_rec);
3622 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3623 osd_quota_unpack(obj, rec);
3628 osd_ipd_put(env, bag, ipd);
3630 LINVRNT(osd_invariant(obj));
3635 static int osd_index_declare_iam_insert(const struct lu_env *env,
3636 struct dt_object *dt,
3637 const struct dt_rec *rec,
3638 const struct dt_key *key,
3639 struct thandle *handle)
3641 struct osd_thandle *oh;
3643 LASSERT(handle != NULL);
3645 oh = container_of0(handle, struct osd_thandle, ot_super);
3646 LASSERT(oh->ot_handle == NULL);
3648 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3649 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3655 * Inserts (key, value) pair in \a dt index object.
3657 * \param dt osd index object
3658 * \param key key for index
3659 * \param rec record reference
3660 * \param th transaction handler
3663 * \retval -ve failure
3665 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3666 const struct dt_rec *rec,
3667 const struct dt_key *key, struct thandle *th,
3670 struct osd_object *obj = osd_dt_obj(dt);
3671 struct iam_path_descr *ipd;
3672 struct osd_thandle *oh;
3673 struct iam_container *bag;
3674 struct osd_thread_info *oti = osd_oti_get(env);
3675 struct iam_rec *iam_rec;
3679 if (!dt_object_exists(dt))
3682 LINVRNT(osd_invariant(obj));
3683 LASSERT(!dt_object_remote(dt));
3685 bag = &obj->oo_dir->od_container;
3686 LASSERT(bag->ic_object == obj->oo_inode);
3687 LASSERT(th != NULL);
3689 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3691 ipd = osd_idx_ipd_get(env, bag);
3692 if (unlikely(ipd == NULL))
3695 oh = container_of0(th, struct osd_thandle, ot_super);
3696 LASSERT(oh->ot_handle != NULL);
3697 LASSERT(oh->ot_handle->h_transaction != NULL);
3698 if (S_ISDIR(obj->oo_inode->i_mode)) {
3699 iam_rec = (struct iam_rec *)oti->oti_ldp;
3700 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3701 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3702 /* pack quota uid/gid */
3703 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3704 key = (const struct dt_key *)&oti->oti_quota_id;
3705 /* pack quota record */
3706 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3707 iam_rec = (struct iam_rec *)rec;
3709 iam_rec = (struct iam_rec *)rec;
3712 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3714 osd_ipd_put(env, bag, ipd);
3715 LINVRNT(osd_invariant(obj));
3716 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3721 * Calls ldiskfs_add_entry() to add directory entry
3722 * into the directory. This is required for
3723 * interoperability mode (b11826)
3725 * \retval 0, on success
3726 * \retval -ve, on error
3728 static int __osd_ea_add_rec(struct osd_thread_info *info,
3729 struct osd_object *pobj, struct inode *cinode,
3730 const char *name, const struct lu_fid *fid,
3731 struct htree_lock *hlock, struct thandle *th)
3733 struct ldiskfs_dentry_param *ldp;
3734 struct dentry *child;
3735 struct osd_thandle *oth;
3738 oth = container_of(th, struct osd_thandle, ot_super);
3739 LASSERT(oth->ot_handle != NULL);
3740 LASSERT(oth->ot_handle->h_transaction != NULL);
3741 LASSERT(pobj->oo_inode);
3743 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3744 if (unlikely(pobj->oo_inode ==
3745 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3748 osd_get_ldiskfs_dirent_param(ldp, fid);
3749 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3750 child->d_fsdata = (void *)ldp;
3751 ll_vfs_dq_init(pobj->oo_inode);
3752 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3753 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
3754 struct ldiskfs_dir_entry_2 *de;
3755 struct buffer_head *bh;
3758 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
3761 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
3764 if (S_ISDIR(cinode->i_mode))
3765 de->file_type = LDISKFS_DIRENT_LUFID |
3766 LDISKFS_FT_REG_FILE;
3768 de->file_type = LDISKFS_DIRENT_LUFID |
3770 ldiskfs_handle_dirty_metadata(oth->ot_handle,
3781 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3782 * into the directory.Also sets flags into osd object to
3783 * indicate dot and dotdot are created. This is required for
3784 * interoperability mode (b11826)
3786 * \param dir directory for dot and dotdot fixup.
3787 * \param obj child object for linking
3789 * \retval 0, on success
3790 * \retval -ve, on error
3792 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3793 struct osd_object *dir,
3794 struct inode *parent_dir, const char *name,
3795 const struct lu_fid *dot_fid,
3796 const struct lu_fid *dot_dot_fid,
3799 struct inode *inode = dir->oo_inode;
3800 struct osd_thandle *oth;
3803 oth = container_of(th, struct osd_thandle, ot_super);
3804 LASSERT(oth->ot_handle->h_transaction != NULL);
3805 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3807 if (strcmp(name, dot) == 0) {
3808 if (dir->oo_compat_dot_created) {
3811 LASSERT(inode == parent_dir);
3812 dir->oo_compat_dot_created = 1;
3815 } else if (strcmp(name, dotdot) == 0) {
3816 if (!dir->oo_compat_dot_created)
3818 /* in case of rename, dotdot is already created */
3819 if (dir->oo_compat_dotdot_created) {
3820 return __osd_ea_add_rec(info, dir, parent_dir, name,
3821 dot_dot_fid, NULL, th);
3824 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
3825 struct lu_fid tfid = *dot_dot_fid;
3828 result = osd_add_dot_dotdot_internal(info,
3829 dir->oo_inode, parent_dir, dot_fid,
3832 result = osd_add_dot_dotdot_internal(info,
3833 dir->oo_inode, parent_dir, dot_fid,
3838 dir->oo_compat_dotdot_created = 1;
3841 /* ldiskfs_init_new_dir() doesn't call ldiskfs_mark_inode_dirty()
3842 * this seem as an optimization as usually it's called
3843 * later to refresh mtime of the parent. Lustre does not
3844 * update mtime in few cases (e.g. PENDING, .lustre)
3845 * we still need to transfer i_size/etc to the buffer cache */
3846 ldiskfs_mark_inode_dirty(oth->ot_handle, dir->oo_inode);
3853 * It will call the appropriate osd_add* function and return the
3854 * value, return by respective functions.
3856 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3857 struct inode *cinode, const char *name,
3858 const struct lu_fid *fid, struct thandle *th)
3860 struct osd_thread_info *info = osd_oti_get(env);
3861 struct htree_lock *hlock;
3864 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3866 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3868 if (hlock != NULL) {
3869 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3872 down_write(&pobj->oo_ext_idx_sem);
3875 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3876 lu_object_fid(&pobj->oo_dt.do_lu),
3879 if (hlock != NULL) {
3880 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3881 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3883 down_write(&pobj->oo_ext_idx_sem);
3886 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3887 struct lu_fid *tfid = &info->oti_fid;
3891 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3894 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3899 ldiskfs_htree_unlock(hlock);
3901 up_write(&pobj->oo_ext_idx_sem);
3907 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3908 struct osd_idmap_cache *oic)
3910 struct osd_scrub *scrub = &dev->od_scrub;
3911 struct lu_fid *fid = &oic->oic_fid;
3912 struct osd_inode_id *id = &oti->oti_id;
3917 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3920 if (scrub->os_pos_current > id->oii_ino)
3924 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3925 if (rc != 0 && rc != -ENOENT)
3928 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3931 if (thread_is_running(&scrub->os_thread)) {
3932 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3933 /* There is race condition between osd_oi_lookup and OI scrub.
3934 * The OI scrub finished just after osd_oi_lookup() failure.
3935 * Under such case, it is unnecessary to trigger OI scrub again,
3936 * but try to call osd_oi_lookup() again. */
3937 if (unlikely(rc == -EAGAIN))
3943 if (!dev->od_noscrub && ++once == 1) {
3944 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
3946 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
3947 "for "DFID", rc = %d [2]\n",
3948 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3950 if (rc == 0 || rc == -EALREADY)
3957 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3958 struct osd_device *dev,
3959 struct osd_idmap_cache *oic,
3960 struct lu_fid *fid, __u32 ino)
3962 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3963 struct inode *inode;
3966 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3967 inode = osd_iget(oti, dev, &oic->oic_lid);
3968 if (IS_ERR(inode)) {
3969 fid_zero(&oic->oic_fid);
3970 return PTR_ERR(inode);
3973 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3976 fid_zero(&oic->oic_fid);
3978 *fid = oic->oic_fid = lma->lma_self_fid;
3982 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3983 struct osd_inode_id *id, const struct lu_fid *fid)
3985 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3986 id->oii_ino, id->oii_gen, info);
3987 info->oti_cache.oic_lid = *id;
3988 info->oti_cache.oic_fid = *fid;
3989 info->oti_cache.oic_dev = osd;
3993 * Get parent FID from the linkEA.
3995 * For a directory which parent resides on remote MDT, to satisfy the
3996 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
3997 * the other hand, to make the lookup(..) on the directory can return
3998 * the real parent FID, we append the real parent FID after its ".."
3999 * name entry in the /REMOTE_PARENT_DIR.
4001 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4002 * backup. So after the restore, we cannot get the right parent FID from
4003 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4004 * we have stored the real parent FID in the directory object's linkEA,
4005 * we can parse the linkEA for the real parent FID.
4007 * \param[in] env pointer to the thread context
4008 * \param[in] obj pointer to the object to be handled
4009 * \param[out]fid pointer to the buffer to hold the parent FID
4011 * \retval 0 for getting the real parent FID successfully
4012 * \retval negative error number on failure
4014 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4015 struct osd_object *obj,
4018 struct osd_thread_info *oti = osd_oti_get(env);
4019 struct lu_buf *buf = &oti->oti_big_buf;
4020 struct dentry *dentry = &oti->oti_obj_dentry;
4021 struct inode *inode = obj->oo_inode;
4022 struct linkea_data ldata = { NULL };
4027 if (!S_ISDIR(inode->i_mode))
4031 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4032 buf->lb_buf, buf->lb_len);
4033 if (rc == -ERANGE) {
4034 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4037 lu_buf_realloc(buf, rc);
4038 if (buf->lb_buf == NULL)
4045 if (unlikely(rc == 0))
4051 if (unlikely(buf->lb_buf == NULL)) {
4052 lu_buf_realloc(buf, rc);
4053 if (buf->lb_buf == NULL)
4060 rc = linkea_init(&ldata);
4062 linkea_first_entry(&ldata);
4063 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4070 * Calls ->lookup() to find dentry. From dentry get inode and
4071 * read inode's ea to get fid. This is required for interoperability
4074 * \retval 0, on success
4075 * \retval -ve, on error
4077 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4078 struct dt_rec *rec, const struct dt_key *key)
4080 struct inode *dir = obj->oo_inode;
4081 struct dentry *dentry;
4082 struct ldiskfs_dir_entry_2 *de;
4083 struct buffer_head *bh;
4084 struct lu_fid *fid = (struct lu_fid *) rec;
4085 struct htree_lock *hlock = NULL;
4090 LASSERT(dir->i_op != NULL);
4091 LASSERT(dir->i_op->lookup != NULL);
4093 dentry = osd_child_dentry_get(env, obj,
4094 (char *)key, strlen((char *)key));
4096 if (obj->oo_hl_head != NULL) {
4097 hlock = osd_oti_get(env)->oti_hlock;
4098 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4099 dir, LDISKFS_HLOCK_LOOKUP);
4101 down_read(&obj->oo_ext_idx_sem);
4104 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4106 struct osd_thread_info *oti = osd_oti_get(env);
4107 struct osd_inode_id *id = &oti->oti_id;
4108 struct osd_idmap_cache *oic = &oti->oti_cache;
4109 struct osd_device *dev = osd_obj2dev(obj);
4111 ino = le32_to_cpu(de->inode);
4112 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4114 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4118 rc = osd_get_fid_from_dentry(de, rec);
4120 /* done with de, release bh */
4123 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4124 const char *name = (const char *)key;
4126 /* If the parent is on remote MDT, and there
4127 * is no FID-in-dirent, then we have to get
4128 * the parent FID from the linkEA. */
4129 if (likely(strlen(name) == 2 &&
4130 name[0] == '.' && name[1] == '.'))
4131 rc = osd_get_pfid_from_linkea(env, obj,
4134 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4137 osd_id_gen(id, ino, OSD_OII_NOGEN);
4141 fid_zero(&oic->oic_fid);
4145 if (osd_remote_fid(env, dev, fid))
4148 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4149 osd_consistency_check(oti, dev, oic);
4158 ldiskfs_htree_unlock(hlock);
4160 up_read(&obj->oo_ext_idx_sem);
4165 * Find the osd object for given fid.
4167 * \param fid need to find the osd object having this fid
4169 * \retval osd_object on success
4170 * \retval -ve on error
4172 static struct osd_object *osd_object_find(const struct lu_env *env,
4173 struct dt_object *dt,
4174 const struct lu_fid *fid)
4176 struct lu_device *ludev = dt->do_lu.lo_dev;
4177 struct osd_object *child = NULL;
4178 struct lu_object *luch;
4179 struct lu_object *lo;
4182 * at this point topdev might not exist yet
4183 * (i.e. MGS is preparing profiles). so we can
4184 * not rely on topdev and instead lookup with
4185 * our device passed as topdev. this can't work
4186 * if the object isn't cached yet (as osd doesn't
4187 * allocate lu_header). IOW, the object must be
4188 * in the cache, otherwise lu_object_alloc() crashes
4191 luch = lu_object_find_at(env, ludev->ld_site->ls_top_dev == NULL ?
4192 ludev : ludev->ld_site->ls_top_dev,
4194 if (!IS_ERR(luch)) {
4195 if (lu_object_exists(luch)) {
4196 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4198 child = osd_obj(lo);
4200 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4201 "lu_object can't be located"
4202 DFID"\n", PFID(fid));
4204 if (child == NULL) {
4205 lu_object_put(env, luch);
4206 CERROR("Unable to get osd_object\n");
4207 child = ERR_PTR(-ENOENT);
4210 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4211 "lu_object does not exists "DFID"\n",
4213 lu_object_put(env, luch);
4214 child = ERR_PTR(-ENOENT);
4217 child = ERR_CAST(luch);
4224 * Put the osd object once done with it.
4226 * \param obj osd object that needs to be put
4228 static inline void osd_object_put(const struct lu_env *env,
4229 struct osd_object *obj)
4231 lu_object_put(env, &obj->oo_dt.do_lu);
4234 static int osd_index_declare_ea_insert(const struct lu_env *env,
4235 struct dt_object *dt,
4236 const struct dt_rec *rec,
4237 const struct dt_key *key,
4238 struct thandle *handle)
4240 struct osd_thandle *oh;
4241 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4242 struct lu_fid *fid = (struct lu_fid *)rec;
4243 int credits, rc = 0;
4246 LASSERT(!dt_object_remote(dt));
4247 LASSERT(handle != NULL);
4249 oh = container_of0(handle, struct osd_thandle, ot_super);
4250 LASSERT(oh->ot_handle == NULL);
4252 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4254 rc = osd_remote_fid(env, osd, fid);
4255 if (unlikely(rc < 0))
4258 /* a reference to remote inode is represented by an
4259 * agent inode which we have to create */
4260 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4261 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4266 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4268 if (osd_dt_obj(dt)->oo_inode != NULL) {
4269 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4271 /* We ignore block quota on meta pool (MDTs), so needn't
4272 * calculate how many blocks will be consumed by this index
4274 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4275 i_gid_read(inode), 0, oh,
4276 osd_dt_obj(dt), true, NULL, false);
4283 * Index add function for interoperability mode (b11826).
4284 * It will add the directory entry.This entry is needed to
4285 * maintain name->fid mapping.
4287 * \param key it is key i.e. file entry to be inserted
4288 * \param rec it is value of given key i.e. fid
4290 * \retval 0, on success
4291 * \retval -ve, on error
4293 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4294 const struct dt_rec *rec,
4295 const struct dt_key *key, struct thandle *th,
4298 struct osd_object *obj = osd_dt_obj(dt);
4299 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4300 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4301 const struct lu_fid *fid = rec1->rec_fid;
4302 const char *name = (const char *)key;
4303 struct osd_thread_info *oti = osd_oti_get(env);
4304 struct osd_inode_id *id = &oti->oti_id;
4305 struct inode *child_inode = NULL;
4306 struct osd_object *child = NULL;
4310 if (!dt_object_exists(dt))
4313 LASSERT(osd_invariant(obj));
4314 LASSERT(!dt_object_remote(dt));
4315 LASSERT(th != NULL);
4317 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4319 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4321 rc = osd_remote_fid(env, osd, fid);
4323 CERROR("%s: Can not find object "DFID" rc %d\n",
4324 osd_name(osd), PFID(fid), rc);
4329 /* Insert remote entry */
4330 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4331 struct osd_mdobj_map *omm = osd->od_mdt_map;
4332 struct osd_thandle *oh;
4334 /* If parent on remote MDT, we need put this object
4336 oh = container_of(th, typeof(*oh), ot_super);
4337 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4339 CERROR("%s: add "DFID" error: rc = %d\n",
4341 PFID(lu_object_fid(&dt->do_lu)), rc);
4345 child_inode = igrab(omm->omm_remote_parent->d_inode);
4347 child_inode = osd_create_local_agent_inode(env, osd,
4348 obj, fid, rec1->rec_type & S_IFMT, th);
4349 if (IS_ERR(child_inode))
4350 RETURN(PTR_ERR(child_inode));
4353 /* Insert local entry */
4354 child = osd_object_find(env, dt, fid);
4355 if (IS_ERR(child)) {
4356 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4357 osd_name(osd), PFID(fid),
4358 id->oii_ino, id->oii_gen,
4359 (int)PTR_ERR(child));
4360 RETURN(PTR_ERR(child));
4362 child_inode = igrab(child->oo_inode);
4365 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4367 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4368 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4372 osd_object_put(env, child);
4373 LASSERT(osd_invariant(obj));
4374 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4379 * Initialize osd Iterator for given osd index object.
4381 * \param dt osd index object
4384 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4385 struct dt_object *dt,
4388 struct osd_it_iam *it;
4389 struct osd_object *obj = osd_dt_obj(dt);
4390 struct lu_object *lo = &dt->do_lu;
4391 struct iam_path_descr *ipd;
4392 struct iam_container *bag = &obj->oo_dir->od_container;
4394 if (!dt_object_exists(dt))
4395 return ERR_PTR(-ENOENT);
4399 return ERR_PTR(-ENOMEM);
4401 ipd = osd_it_ipd_get(env, bag);
4402 if (likely(ipd != NULL)) {
4406 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4407 return (struct dt_it *)it;
4410 return ERR_PTR(-ENOMEM);
4415 * free given Iterator.
4418 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4420 struct osd_it_iam *it = (struct osd_it_iam *)di;
4421 struct osd_object *obj = it->oi_obj;
4423 iam_it_fini(&it->oi_it);
4424 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4425 lu_object_put(env, &obj->oo_dt.do_lu);
4430 * Move Iterator to record specified by \a key
4432 * \param di osd iterator
4433 * \param key key for index
4435 * \retval +ve di points to record with least key not larger than key
4436 * \retval 0 di points to exact matched key
4437 * \retval -ve failure
4440 static int osd_it_iam_get(const struct lu_env *env,
4441 struct dt_it *di, const struct dt_key *key)
4443 struct osd_thread_info *oti = osd_oti_get(env);
4444 struct osd_it_iam *it = (struct osd_it_iam *)di;
4446 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4447 /* swab quota uid/gid */
4448 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4449 key = (struct dt_key *)&oti->oti_quota_id;
4452 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4458 * \param di osd iterator
4460 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4462 struct osd_it_iam *it = (struct osd_it_iam *)di;
4464 iam_it_put(&it->oi_it);
4468 * Move iterator by one record
4470 * \param di osd iterator
4472 * \retval +1 end of container reached
4474 * \retval -ve failure
4477 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4479 struct osd_it_iam *it = (struct osd_it_iam *)di;
4481 return iam_it_next(&it->oi_it);
4485 * Return pointer to the key under iterator.
4488 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4489 const struct dt_it *di)
4491 struct osd_thread_info *oti = osd_oti_get(env);
4492 struct osd_it_iam *it = (struct osd_it_iam *)di;
4493 struct osd_object *obj = it->oi_obj;
4496 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4498 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4499 /* swab quota uid/gid */
4500 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4501 key = (struct dt_key *)&oti->oti_quota_id;
4508 * Return size of key under iterator (in bytes)
4511 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4513 struct osd_it_iam *it = (struct osd_it_iam *)di;
4515 return iam_it_key_size(&it->oi_it);
4519 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4521 /* check if file type is required */
4522 if (ent->lde_attrs & LUDA_TYPE) {
4523 struct luda_type *lt;
4524 int align = sizeof(*lt) - 1;
4526 len = (len + align) & ~align;
4527 lt = (struct luda_type *)(ent->lde_name + len);
4528 lt->lt_type = cpu_to_le16(DTTOIF(type));
4531 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4535 * build lu direct from backend fs dirent.
4539 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4540 char *name, __u16 namelen, __u16 type, __u32 attr)
4542 ent->lde_attrs = attr | LUDA_FID;
4543 fid_cpu_to_le(&ent->lde_fid, fid);
4545 ent->lde_hash = cpu_to_le64(offset);
4546 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4548 strncpy(ent->lde_name, name, namelen);
4549 ent->lde_name[namelen] = '\0';
4550 ent->lde_namelen = cpu_to_le16(namelen);
4552 /* append lustre attributes */
4553 osd_it_append_attrs(ent, namelen, type);
4557 * Return pointer to the record under iterator.
4559 static int osd_it_iam_rec(const struct lu_env *env,
4560 const struct dt_it *di,
4561 struct dt_rec *dtrec, __u32 attr)
4563 struct osd_it_iam *it = (struct osd_it_iam *)di;
4564 struct osd_thread_info *info = osd_oti_get(env);
4567 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4568 const struct osd_fid_pack *rec;
4569 struct lu_fid *fid = &info->oti_fid;
4570 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4576 name = (char *)iam_it_key_get(&it->oi_it);
4578 RETURN(PTR_ERR(name));
4580 namelen = iam_it_key_size(&it->oi_it);
4582 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4584 RETURN(PTR_ERR(rec));
4586 rc = osd_fid_unpack(fid, rec);
4590 hash = iam_it_store(&it->oi_it);
4592 /* IAM does not store object type in IAM index (dir) */
4593 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4595 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4596 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4597 (struct iam_rec *)dtrec);
4598 osd_quota_unpack(it->oi_obj, dtrec);
4600 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4601 (struct iam_rec *)dtrec);
4608 * Returns cookie for current Iterator position.
4610 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4612 struct osd_it_iam *it = (struct osd_it_iam *)di;
4614 return iam_it_store(&it->oi_it);
4618 * Restore iterator from cookie.
4620 * \param di osd iterator
4621 * \param hash Iterator location cookie
4623 * \retval +ve di points to record with least key not larger than key.
4624 * \retval 0 di points to exact matched key
4625 * \retval -ve failure
4628 static int osd_it_iam_load(const struct lu_env *env,
4629 const struct dt_it *di, __u64 hash)
4631 struct osd_it_iam *it = (struct osd_it_iam *)di;
4633 return iam_it_load(&it->oi_it, hash);
4636 static const struct dt_index_operations osd_index_iam_ops = {
4637 .dio_lookup = osd_index_iam_lookup,
4638 .dio_declare_insert = osd_index_declare_iam_insert,
4639 .dio_insert = osd_index_iam_insert,
4640 .dio_declare_delete = osd_index_declare_iam_delete,
4641 .dio_delete = osd_index_iam_delete,
4643 .init = osd_it_iam_init,
4644 .fini = osd_it_iam_fini,
4645 .get = osd_it_iam_get,
4646 .put = osd_it_iam_put,
4647 .next = osd_it_iam_next,
4648 .key = osd_it_iam_key,
4649 .key_size = osd_it_iam_key_size,
4650 .rec = osd_it_iam_rec,
4651 .store = osd_it_iam_store,
4652 .load = osd_it_iam_load
4658 * Creates or initializes iterator context.
4660 * \retval struct osd_it_ea, iterator structure on success
4663 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4664 struct dt_object *dt,
4667 struct osd_object *obj = osd_dt_obj(dt);
4668 struct osd_thread_info *info = osd_oti_get(env);
4669 struct osd_it_ea *oie;
4671 struct lu_object *lo = &dt->do_lu;
4672 struct dentry *obj_dentry;
4675 if (!dt_object_exists(dt) || obj->oo_destroyed)
4676 RETURN(ERR_PTR(-ENOENT));
4678 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
4680 RETURN(ERR_PTR(-ENOMEM));
4681 obj_dentry = &oie->oie_dentry;
4683 obj_dentry->d_inode = obj->oo_inode;
4684 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4685 obj_dentry->d_name.hash = 0;
4687 oie->oie_rd_dirent = 0;
4688 oie->oie_it_dirent = 0;
4689 oie->oie_dirent = NULL;
4690 if (unlikely(!info->oti_it_ea_buf_used)) {
4691 oie->oie_buf = info->oti_it_ea_buf;
4692 info->oti_it_ea_buf_used = 1;
4694 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
4695 if (oie->oie_buf == NULL)
4696 RETURN(ERR_PTR(-ENOMEM));
4700 file = &oie->oie_file;
4702 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4703 if (attr & LUDA_64BITHASH)
4704 file->f_mode = FMODE_64BITHASH;
4706 file->f_mode = FMODE_32BITHASH;
4707 file->f_path.dentry = obj_dentry;
4708 file->f_mapping = obj->oo_inode->i_mapping;
4709 file->f_op = obj->oo_inode->i_fop;
4710 set_file_inode(file, obj->oo_inode);
4713 RETURN((struct dt_it *) oie);
4717 * Destroy or finishes iterator context.
4719 * \param di iterator structure to be destroyed
4721 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4723 struct osd_thread_info *info = osd_oti_get(env);
4724 struct osd_it_ea *oie = (struct osd_it_ea *)di;
4725 struct osd_object *obj = oie->oie_obj;
4726 struct inode *inode = obj->oo_inode;
4729 oie->oie_file.f_op->release(inode, &oie->oie_file);
4730 lu_object_put(env, &obj->oo_dt.do_lu);
4731 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
4732 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
4734 info->oti_it_ea_buf_used = 0;
4735 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
4740 * It position the iterator at given key, so that next lookup continues from
4741 * that key Or it is similar to dio_it->load() but based on a key,
4742 * rather than file position.
4744 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4747 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4749 static int osd_it_ea_get(const struct lu_env *env,
4750 struct dt_it *di, const struct dt_key *key)
4752 struct osd_it_ea *it = (struct osd_it_ea *)di;
4755 LASSERT(((const char *)key)[0] == '\0');
4756 it->oie_file.f_pos = 0;
4757 it->oie_rd_dirent = 0;
4758 it->oie_it_dirent = 0;
4759 it->oie_dirent = NULL;
4767 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4771 struct osd_filldir_cbs {
4772 #ifdef HAVE_DIR_CONTEXT
4773 struct dir_context ctx;
4775 struct osd_it_ea *it;
4778 * It is called internally by ->readdir(). It fills the
4779 * iterator's in-memory data structure with required
4780 * information i.e. name, namelen, rec_size etc.
4782 * \param buf in which information to be filled in.
4783 * \param name name of the file in given dir
4785 * \retval 0 on success
4786 * \retval 1 on buffer full
4788 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
4789 loff_t offset, __u64 ino,
4792 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
4793 struct osd_object *obj = it->oie_obj;
4794 struct osd_it_ea_dirent *ent = it->oie_dirent;
4795 struct lu_fid *fid = &ent->oied_fid;
4796 struct osd_fid_pack *rec;
4799 /* this should never happen */
4800 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4801 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4805 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4809 /* "." is just the object itself. */
4810 if (namelen == 1 && name[0] == '.') {
4811 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4812 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4813 rec = (struct osd_fid_pack*) (name + namelen + 1);
4814 if (osd_fid_unpack(fid, rec) != 0)
4819 d_type &= ~LDISKFS_DIRENT_LUFID;
4821 /* NOT export local root. */
4822 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4823 ino = obj->oo_inode->i_ino;
4824 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4827 ent->oied_ino = ino;
4828 ent->oied_off = offset;
4829 ent->oied_namelen = namelen;
4830 ent->oied_type = d_type;
4832 memcpy(ent->oied_name, name, namelen);
4834 it->oie_rd_dirent++;
4835 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4840 * Calls ->readdir() to load a directory entry at a time
4841 * and stored it in iterator's in-memory data structure.
4843 * \param di iterator's in memory structure
4845 * \retval 0 on success
4846 * \retval -ve on error
4847 * \retval +1 reach the end of entry
4849 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4850 const struct dt_it *di)
4852 struct osd_it_ea *it = (struct osd_it_ea *)di;
4853 struct osd_object *obj = it->oie_obj;
4854 struct inode *inode = obj->oo_inode;
4855 struct htree_lock *hlock = NULL;
4856 struct file *filp = &it->oie_file;
4858 struct osd_filldir_cbs buf = {
4859 #ifdef HAVE_DIR_CONTEXT
4860 .ctx.actor = osd_ldiskfs_filldir,
4866 it->oie_dirent = it->oie_buf;
4867 it->oie_rd_dirent = 0;
4869 if (obj->oo_hl_head != NULL) {
4870 hlock = osd_oti_get(env)->oti_hlock;
4871 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4872 inode, LDISKFS_HLOCK_READDIR);
4874 down_read(&obj->oo_ext_idx_sem);
4877 #ifdef HAVE_DIR_CONTEXT
4878 buf.ctx.pos = filp->f_pos;
4879 rc = inode->i_fop->iterate(filp, &buf.ctx);
4880 filp->f_pos = buf.ctx.pos;
4882 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
4886 ldiskfs_htree_unlock(hlock);
4888 up_read(&obj->oo_ext_idx_sem);
4890 if (it->oie_rd_dirent == 0) {
4891 /*If it does not get any dirent, it means it has been reached
4892 *to the end of the dir */
4893 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
4897 it->oie_dirent = it->oie_buf;
4898 it->oie_it_dirent = 1;
4905 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4906 * to load a directory entry at a time and stored it in
4907 * iterator's in-memory data structure.
4909 * \param di iterator's in memory structure
4911 * \retval +ve iterator reached to end
4912 * \retval 0 iterator not reached to end
4913 * \retval -ve on error
4915 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4917 struct osd_it_ea *it = (struct osd_it_ea *)di;
4922 if (it->oie_it_dirent < it->oie_rd_dirent) {
4924 (void *) it->oie_dirent +
4925 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4926 it->oie_dirent->oied_namelen);
4927 it->oie_it_dirent++;
4930 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4933 rc = osd_ldiskfs_it_fill(env, di);
4940 * Returns the key at current position from iterator's in memory structure.
4942 * \param di iterator's in memory structure
4944 * \retval key i.e. struct dt_key on success
4946 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4947 const struct dt_it *di)
4949 struct osd_it_ea *it = (struct osd_it_ea *)di;
4951 return (struct dt_key *)it->oie_dirent->oied_name;
4955 * Returns the key's size at current position from iterator's in memory structure.
4957 * \param di iterator's in memory structure
4959 * \retval key_size i.e. struct dt_key on success
4961 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4963 struct osd_it_ea *it = (struct osd_it_ea *)di;
4965 return it->oie_dirent->oied_namelen;
4969 osd_dirent_update(handle_t *jh, struct super_block *sb,
4970 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4971 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4973 struct osd_fid_pack *rec;
4977 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4978 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4980 rc = ldiskfs_journal_get_write_access(jh, bh);
4984 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4985 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4986 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
4992 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4994 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4995 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5002 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5004 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5005 "dot_dotdot = %d\n", dot_dotdot);
5007 if (LDISKFS_DIR_REC_LEN(de) >=
5008 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5015 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5016 struct inode *dir, struct inode *inode,
5017 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
5018 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5019 struct htree_lock *hlock)
5021 struct dentry *dentry;
5022 struct osd_fid_pack *rec;
5023 struct ldiskfs_dentry_param *ldp;
5027 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5028 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5031 /* There is enough space to hold the FID-in-dirent. */
5032 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
5033 dir->i_sb->s_blocksize)) {
5034 rc = ldiskfs_journal_get_write_access(jh, bh);
5038 de->name[de->name_len] = 0;
5039 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5040 rec->fp_len = sizeof(struct lu_fid) + 1;
5041 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5042 de->file_type |= LDISKFS_DIRENT_LUFID;
5044 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5049 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5053 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5055 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5056 osd_get_ldiskfs_dirent_param(ldp, fid);
5057 dentry->d_fsdata = (void *)ldp;
5058 ll_vfs_dq_init(dir);
5059 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
5060 /* It is too bad, we cannot reinsert the name entry back.
5061 * That means we lose it! */
5063 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5064 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5065 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5066 dir->i_ino, dir->i_generation,
5067 ent->oied_namelen, ent->oied_name, PFID(fid), rc);
5073 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5074 struct osd_it_ea *it, struct lu_fid *fid,
5075 struct osd_inode_id *id, __u32 *attr)
5077 struct osd_thread_info *info = osd_oti_get(env);
5078 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5079 struct osd_device *dev = osd_obj2dev(obj);
5080 struct super_block *sb = osd_sb(dev);
5081 const char *devname =
5082 LDISKFS_SB(sb)->s_es->s_volume_name;
5083 struct osd_it_ea_dirent *ent = it->oie_dirent;
5084 struct inode *dir = obj->oo_inode;
5085 struct htree_lock *hlock = NULL;
5086 struct buffer_head *bh = NULL;
5087 handle_t *jh = NULL;
5088 struct ldiskfs_dir_entry_2 *de;
5089 struct dentry *dentry;
5090 struct inode *inode;
5097 if (ent->oied_name[0] == '.') {
5098 if (ent->oied_namelen == 1)
5100 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5104 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
5107 /* We need to ensure that the name entry is still valid.
5108 * Because it may be removed or renamed by other already.
5110 * The unlink or rename operation will start journal before PDO lock,
5111 * so to avoid deadlock, here we need to start journal handle before
5112 * related PDO lock also. But because we do not know whether there
5113 * will be something to be repaired before PDO lock, we just start
5114 * journal without conditions.
5116 * We may need to remove the name entry firstly, then insert back.
5117 * One credit is for user quota file update.
5118 * One credit is for group quota file update.
5119 * Two credits are for dirty inode. */
5120 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5121 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5124 if (dev->od_dirent_journal != 0) {
5125 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5128 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5129 "check_repair, dir = %lu/%u, credits = %d, "
5130 "name = %.*s: rc = %d\n",
5131 devname, dir->i_ino, dir->i_generation, credits,
5132 ent->oied_namelen, ent->oied_name, rc);
5136 if (obj->oo_hl_head != NULL) {
5137 hlock = osd_oti_get(env)->oti_hlock;
5138 /* "0" means exclusive lock for the whole directory.
5139 * We need to prevent others access such name entry
5140 * during the delete + insert. Neither HLOCK_ADD nor
5141 * HLOCK_DEL cannot guarantee the atomicity. */
5142 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5144 down_write(&obj->oo_ext_idx_sem);
5147 if (obj->oo_hl_head != NULL) {
5148 hlock = osd_oti_get(env)->oti_hlock;
5149 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5150 LDISKFS_HLOCK_LOOKUP);
5152 down_read(&obj->oo_ext_idx_sem);
5156 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5157 /* For dot/dotdot entry, if there is not enough space to hold the
5158 * FID-in-dirent, just keep them there. It only happens when the
5159 * device upgraded from 1.8 or restored from MDT file-level backup.
5160 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5161 * and needs to get FID from LMA when readdir, it will not affect the
5162 * performance much. */
5163 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
5164 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5165 *attr |= LUDA_IGNORE;
5166 GOTO(out_journal, rc = 0);
5169 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5170 inode = osd_iget(info, dev, id);
5171 if (IS_ERR(inode)) {
5172 rc = PTR_ERR(inode);
5173 if (rc == -ENOENT || rc == -ESTALE)
5176 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5177 "check_repair, dir = %lu/%u, name = %.*s: "
5179 devname, dir->i_ino, dir->i_generation,
5180 ent->oied_namelen, ent->oied_name, rc);
5182 GOTO(out_journal, rc);
5185 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5187 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5189 if (fid_is_sane(fid)) {
5190 /* FID-in-dirent is valid. */
5191 if (lu_fid_eq(fid, &lma->lma_self_fid))
5192 GOTO(out_inode, rc = 0);
5194 /* Do not repair under dryrun mode. */
5195 if (*attr & LUDA_VERIFY_DRYRUN) {
5196 *attr |= LUDA_REPAIR;
5197 GOTO(out_inode, rc = 0);
5200 if (dev->od_dirent_journal == 0) {
5204 ldiskfs_htree_unlock(hlock);
5206 up_read(&obj->oo_ext_idx_sem);
5207 dev->od_dirent_journal = 1;
5211 *fid = lma->lma_self_fid;
5213 /* Update the FID-in-dirent. */
5214 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5216 *attr |= LUDA_REPAIR;
5218 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5219 "in the dirent, dir = %lu/%u, "
5220 "name = %.*s, "DFID": rc = %d\n",
5221 devname, dir->i_ino, dir->i_generation,
5222 ent->oied_namelen, ent->oied_name,
5225 /* Do not repair under dryrun mode. */
5226 if (*attr & LUDA_VERIFY_DRYRUN) {
5227 *fid = lma->lma_self_fid;
5228 *attr |= LUDA_REPAIR;
5229 GOTO(out_inode, rc = 0);
5232 if (dev->od_dirent_journal == 0) {
5236 ldiskfs_htree_unlock(hlock);
5238 up_read(&obj->oo_ext_idx_sem);
5239 dev->od_dirent_journal = 1;
5243 *fid = lma->lma_self_fid;
5245 /* Append the FID-in-dirent. */
5246 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5247 fid, bh, de, hlock);
5249 *attr |= LUDA_REPAIR;
5251 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5252 "after the dirent, dir = %lu/%u, "
5253 "name = %.*s, "DFID": rc = %d\n",
5254 devname, dir->i_ino, dir->i_generation,
5255 ent->oied_namelen, ent->oied_name,
5258 } else if (rc == -ENODATA) {
5259 /* Do not repair under dryrun mode. */
5260 if (*attr & LUDA_VERIFY_DRYRUN) {
5261 if (fid_is_sane(fid)) {
5262 *attr |= LUDA_REPAIR;
5264 lu_igif_build(fid, inode->i_ino,
5265 inode->i_generation);
5266 *attr |= LUDA_UPGRADE;
5268 GOTO(out_inode, rc = 0);
5271 if (dev->od_dirent_journal == 0) {
5275 ldiskfs_htree_unlock(hlock);
5277 up_read(&obj->oo_ext_idx_sem);
5278 dev->od_dirent_journal = 1;
5283 if (unlikely(fid_is_sane(fid))) {
5284 /* FID-in-dirent exists, but FID-in-LMA is lost.
5285 * Trust the FID-in-dirent, and add FID-in-LMA. */
5286 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5288 *attr |= LUDA_REPAIR;
5290 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5291 "update dirent, dir = %lu/%u, "
5292 "name = %.*s, "DFID": rc = %d\n",
5293 devname, dir->i_ino, dir->i_generation,
5294 ent->oied_namelen, ent->oied_name,
5297 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5298 /* It is probably IGIF object. Only aappend the
5299 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5300 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5301 fid, bh, de, hlock);
5303 *attr |= LUDA_UPGRADE;
5305 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5306 "after the dirent, dir = %lu/%u, "
5307 "name = %.*s, "DFID": rc = %d\n",
5308 devname, dir->i_ino, dir->i_generation,
5309 ent->oied_namelen, ent->oied_name,
5314 GOTO(out_inode, rc);
5321 if (hlock != NULL) {
5322 ldiskfs_htree_unlock(hlock);
5324 if (dev->od_dirent_journal != 0)
5325 up_write(&obj->oo_ext_idx_sem);
5327 up_read(&obj->oo_ext_idx_sem);
5330 ldiskfs_journal_stop(jh);
5331 if (rc >= 0 && !dirty)
5332 dev->od_dirent_journal = 0;
5337 * Returns the value at current position from iterator's in memory structure.
5339 * \param di struct osd_it_ea, iterator's in memory structure
5340 * \param attr attr requested for dirent.
5341 * \param lde lustre dirent
5343 * \retval 0 no error and \param lde has correct lustre dirent.
5344 * \retval -ve on error
5346 static inline int osd_it_ea_rec(const struct lu_env *env,
5347 const struct dt_it *di,
5348 struct dt_rec *dtrec, __u32 attr)
5350 struct osd_it_ea *it = (struct osd_it_ea *)di;
5351 struct osd_object *obj = it->oie_obj;
5352 struct osd_device *dev = osd_obj2dev(obj);
5353 struct osd_thread_info *oti = osd_oti_get(env);
5354 struct osd_inode_id *id = &oti->oti_id;
5355 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5356 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5357 __u32 ino = it->oie_dirent->oied_ino;
5361 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5363 if (attr & LUDA_VERIFY) {
5364 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5365 attr |= LUDA_IGNORE;
5366 /* If the parent is on remote MDT, and there
5367 * is no FID-in-dirent, then we have to get
5368 * the parent FID from the linkEA. */
5369 if (!fid_is_sane(fid) &&
5370 it->oie_dirent->oied_namelen == 2 &&
5371 it->oie_dirent->oied_name[0] == '.' &&
5372 it->oie_dirent->oied_name[1] == '.')
5373 osd_get_pfid_from_linkea(env, obj, fid);
5375 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5379 attr &= ~LU_DIRENT_ATTRS_MASK;
5380 if (!fid_is_sane(fid)) {
5381 bool is_dotdot = false;
5382 if (it->oie_dirent->oied_namelen == 2 &&
5383 it->oie_dirent->oied_name[0] == '.' &&
5384 it->oie_dirent->oied_name[1] == '.')
5386 /* If the parent is on remote MDT, and there
5387 * is no FID-in-dirent, then we have to get
5388 * the parent FID from the linkEA. */
5389 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5390 rc = osd_get_pfid_from_linkea(env, obj, fid);
5392 if (is_dotdot == false &&
5393 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5396 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5399 osd_id_gen(id, ino, OSD_OII_NOGEN);
5403 /* Pack the entry anyway, at least the offset is right. */
5404 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5405 it->oie_dirent->oied_name,
5406 it->oie_dirent->oied_namelen,
5407 it->oie_dirent->oied_type, attr);
5412 if (osd_remote_fid(env, dev, fid))
5415 if (likely(!(attr & LUDA_IGNORE) && rc == 0))
5416 osd_add_oi_cache(oti, dev, id, fid);
5418 RETURN(rc > 0 ? 0 : rc);
5422 * Returns the record size size at current position.
5424 * This function will return record(lu_dirent) size in bytes.
5426 * \param[in] env execution environment
5427 * \param[in] di iterator's in memory structure
5428 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5429 * calculate the lu_dirent size.
5431 * \retval record size(in bytes & in memory) of the current lu_dirent
5434 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5437 struct osd_it_ea *it = (struct osd_it_ea *)di;
5439 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5443 * Returns a cookie for current position of the iterator head, so that
5444 * user can use this cookie to load/start the iterator next time.
5446 * \param di iterator's in memory structure
5448 * \retval cookie for current position, on success
5450 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5452 struct osd_it_ea *it = (struct osd_it_ea *)di;
5454 return it->oie_dirent->oied_off;
5458 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5459 * to load a directory entry at a time and stored it i inn,
5460 * in iterator's in-memory data structure.
5462 * \param di struct osd_it_ea, iterator's in memory structure
5464 * \retval +ve on success
5465 * \retval -ve on error
5467 static int osd_it_ea_load(const struct lu_env *env,
5468 const struct dt_it *di, __u64 hash)
5470 struct osd_it_ea *it = (struct osd_it_ea *)di;
5474 it->oie_file.f_pos = hash;
5476 rc = osd_ldiskfs_it_fill(env, di);
5487 * Index lookup function for interoperability mode (b11826).
5489 * \param key, key i.e. file name to be searched
5491 * \retval +ve, on success
5492 * \retval -ve, on error
5494 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5495 struct dt_rec *rec, const struct dt_key *key)
5497 struct osd_object *obj = osd_dt_obj(dt);
5502 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5503 LINVRNT(osd_invariant(obj));
5505 rc = osd_ea_lookup_rec(env, obj, rec, key);
5512 * Index and Iterator operations for interoperability
5513 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5515 static const struct dt_index_operations osd_index_ea_ops = {
5516 .dio_lookup = osd_index_ea_lookup,
5517 .dio_declare_insert = osd_index_declare_ea_insert,
5518 .dio_insert = osd_index_ea_insert,
5519 .dio_declare_delete = osd_index_declare_ea_delete,
5520 .dio_delete = osd_index_ea_delete,
5522 .init = osd_it_ea_init,
5523 .fini = osd_it_ea_fini,
5524 .get = osd_it_ea_get,
5525 .put = osd_it_ea_put,
5526 .next = osd_it_ea_next,
5527 .key = osd_it_ea_key,
5528 .key_size = osd_it_ea_key_size,
5529 .rec = osd_it_ea_rec,
5530 .rec_size = osd_it_ea_rec_size,
5531 .store = osd_it_ea_store,
5532 .load = osd_it_ea_load
5536 static void *osd_key_init(const struct lu_context *ctx,
5537 struct lu_context_key *key)
5539 struct osd_thread_info *info;
5541 OBD_ALLOC_PTR(info);
5543 return ERR_PTR(-ENOMEM);
5545 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5546 if (info->oti_it_ea_buf == NULL)
5549 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5551 info->oti_hlock = ldiskfs_htree_lock_alloc();
5552 if (info->oti_hlock == NULL)
5558 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5561 return ERR_PTR(-ENOMEM);
5564 static void osd_key_fini(const struct lu_context *ctx,
5565 struct lu_context_key *key, void* data)
5567 struct osd_thread_info *info = data;
5569 if (info->oti_inode != NULL)
5570 OBD_FREE_PTR(info->oti_inode);
5571 if (info->oti_hlock != NULL)
5572 ldiskfs_htree_lock_free(info->oti_hlock);
5573 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5574 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5575 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5576 lu_buf_free(&info->oti_big_buf);
5580 static void osd_key_exit(const struct lu_context *ctx,
5581 struct lu_context_key *key, void *data)
5583 struct osd_thread_info *info = data;
5585 LASSERT(info->oti_r_locks == 0);
5586 LASSERT(info->oti_w_locks == 0);
5587 LASSERT(info->oti_txns == 0);
5590 /* type constructor/destructor: osd_type_init, osd_type_fini */
5591 LU_TYPE_INIT_FINI(osd, &osd_key);
5593 struct lu_context_key osd_key = {
5594 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5595 .lct_init = osd_key_init,
5596 .lct_fini = osd_key_fini,
5597 .lct_exit = osd_key_exit
5601 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5602 const char *name, struct lu_device *next)
5604 struct osd_device *osd = osd_dev(d);
5606 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5607 >= sizeof(osd->od_svname))
5609 return osd_procfs_init(osd, name);
5612 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
5614 struct seq_server_site *ss = osd_seq_site(osd);
5618 if (osd->od_is_ost || osd->od_cl_seq != NULL)
5621 if (unlikely(ss == NULL))
5624 OBD_ALLOC_PTR(osd->od_cl_seq);
5625 if (osd->od_cl_seq == NULL)
5628 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
5629 osd->od_svname, ss->ss_server_seq);
5631 OBD_FREE_PTR(osd->od_cl_seq);
5632 osd->od_cl_seq = NULL;
5636 if (ss->ss_node_id == 0) {
5637 /* If the OSD on the sequence controller(MDT0), then allocate
5638 * sequence here, otherwise allocate sequence after connected
5639 * to MDT0 (see mdt_register_lwp_callback()). */
5640 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
5641 &osd->od_cl_seq->lcs_space, env);
5647 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
5649 if (osd->od_cl_seq == NULL)
5652 seq_client_fini(osd->od_cl_seq);
5653 OBD_FREE_PTR(osd->od_cl_seq);
5654 osd->od_cl_seq = NULL;
5657 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5661 /* shutdown quota slave instance associated with the device */
5662 if (o->od_quota_slave != NULL) {
5663 qsd_fini(env, o->od_quota_slave);
5664 o->od_quota_slave = NULL;
5667 osd_fid_fini(env, o);
5672 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5676 if (o->od_mnt != NULL) {
5677 shrink_dcache_sb(osd_sb(o));
5678 osd_sync(env, &o->od_dt_dev);
5687 static int osd_mount(const struct lu_env *env,
5688 struct osd_device *o, struct lustre_cfg *cfg)
5690 const char *name = lustre_cfg_string(cfg, 0);
5691 const char *dev = lustre_cfg_string(cfg, 1);
5693 unsigned long page, s_flags, lmd_flags = 0;
5694 struct page *__page;
5695 struct file_system_type *type;
5696 char *options = NULL;
5698 struct osd_thread_info *info = osd_oti_get(env);
5699 struct lu_fid *fid = &info->oti_fid;
5700 struct inode *inode;
5701 int rc = 0, force_over_128tb = 0;
5704 if (o->od_mnt != NULL)
5707 if (strlen(dev) >= sizeof(o->od_mntdev))
5709 strcpy(o->od_mntdev, dev);
5711 str = lustre_cfg_string(cfg, 2);
5712 s_flags = simple_strtoul(str, NULL, 0);
5713 str = strstr(str, ":");
5715 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5716 opts = lustre_cfg_string(cfg, 3);
5718 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
5719 CERROR("%s: device %s extents feature is not guaranteed to "
5720 "work on big-endian systems. Use \"bigendian_extents\" "
5721 "mount option to override.\n", name, dev);
5725 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
5726 force_over_128tb = 1;
5728 __page = alloc_page(GFP_IOFS);
5730 GOTO(out, rc = -ENOMEM);
5731 page = (unsigned long)page_address(__page);
5732 options = (char *)page;
5735 /* strip out the options for back compatiblity */
5736 static char *sout[] = {
5743 /* strip out option we processed in osd */
5744 "bigendian_extents",
5748 strcat(options, opts);
5749 for (rc = 0, str = options; sout[rc]; ) {
5750 char *op = strstr(str, sout[rc]);
5756 if (op == options || *(op - 1) == ',') {
5757 str = op + strlen(sout[rc]);
5758 if (*str == ',' || *str == '\0') {
5759 *str == ',' ? str++ : str;
5760 memmove(op, str, strlen(str) + 1);
5763 for (str = op; *str != ',' && *str != '\0'; str++)
5767 strncat(options, "user_xattr,acl", 14);
5770 /* Glom up mount options */
5771 if (*options != '\0')
5772 strcat(options, ",");
5773 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5775 type = get_fs_type("ldiskfs");
5777 CERROR("%s: cannot find ldiskfs module\n", name);
5778 GOTO(out, rc = -ENODEV);
5781 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5782 module_put(type->owner);
5784 if (IS_ERR(o->od_mnt)) {
5785 rc = PTR_ERR(o->od_mnt);
5787 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5791 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
5792 force_over_128tb == 0) {
5793 CERROR("%s: device %s LDISKFS does not support filesystems "
5794 "greater than 128TB and can cause data corruption. "
5795 "Use \"force_over_128tb\" mount option to override.\n",
5797 GOTO(out, rc = -EINVAL);
5800 #ifdef HAVE_DEV_SET_RDONLY
5801 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5802 CERROR("%s: underlying device %s is marked as read-only. "
5803 "Setup failed\n", name, dev);
5804 GOTO(out_mnt, rc = -EROFS);
5808 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5809 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5810 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5811 GOTO(out_mnt, rc = -EINVAL);
5814 #ifdef LDISKFS_MOUNT_DIRDATA
5815 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
5816 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5817 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
5818 else if (!o->od_is_ost)
5819 CWARN("%s: device %s was upgraded from Lustre-1.x without "
5820 "enabling the dirdata feature. If you do not want to "
5821 "downgrade to Lustre-1.x again, you can enable it via "
5822 "'tune2fs -O dirdata device'\n", name, dev);
5824 inode = osd_sb(o)->s_root->d_inode;
5825 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5826 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5828 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5832 if (lmd_flags & LMD_FLG_NOSCRUB)
5843 __free_page(__page);
5848 static struct lu_device *osd_device_fini(const struct lu_env *env,
5849 struct lu_device *d)
5851 struct osd_device *o = osd_dev(d);
5854 osd_shutdown(env, o);
5856 osd_scrub_cleanup(env, o);
5857 osd_obj_map_fini(o);
5863 static int osd_device_init0(const struct lu_env *env,
5864 struct osd_device *o,
5865 struct lustre_cfg *cfg)
5867 struct lu_device *l = osd2lu_dev(o);
5868 struct osd_thread_info *info;
5872 /* if the module was re-loaded, env can loose its keys */
5873 rc = lu_env_refill((struct lu_env *) env);
5876 info = osd_oti_get(env);
5879 l->ld_ops = &osd_lu_ops;
5880 o->od_dt_dev.dd_ops = &osd_dt_ops;
5882 spin_lock_init(&o->od_osfs_lock);
5883 mutex_init(&o->od_otable_mutex);
5885 o->od_read_cache = 1;
5886 o->od_writethrough_cache = 1;
5887 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5889 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5890 sizeof(o->od_svname));
5891 if (cplen >= sizeof(o->od_svname)) {
5896 o->od_index = -1; /* -1 means index is invalid */
5897 rc = server_name2index(o->od_svname, &o->od_index, NULL);
5898 if (rc == LDD_F_SV_TYPE_OST)
5901 o->od_full_scrub_ratio = OFSR_DEFAULT;
5902 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
5903 rc = osd_mount(env, o, cfg);
5907 rc = osd_obj_map_init(env, o);
5911 rc = lu_site_init(&o->od_site, l);
5913 GOTO(out_compat, rc);
5914 o->od_site.ls_bottom_dev = l;
5916 rc = lu_site_init_finish(&o->od_site);
5920 INIT_LIST_HEAD(&o->od_ios_list);
5921 /* setup scrub, including OI files initialization */
5922 rc = osd_scrub_setup(env, o);
5926 rc = osd_procfs_init(o, o->od_svname);
5928 CERROR("%s: can't initialize procfs: rc = %d\n",
5930 GOTO(out_scrub, rc);
5933 LASSERT(l->ld_site->ls_linkage.next != NULL);
5934 LASSERT(l->ld_site->ls_linkage.prev != NULL);
5936 /* initialize quota slave instance */
5937 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5939 if (IS_ERR(o->od_quota_slave)) {
5940 rc = PTR_ERR(o->od_quota_slave);
5941 o->od_quota_slave = NULL;
5942 GOTO(out_procfs, rc);
5950 osd_scrub_cleanup(env, o);
5952 lu_site_fini(&o->od_site);
5954 osd_obj_map_fini(o);
5961 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5962 struct lu_device_type *t,
5963 struct lustre_cfg *cfg)
5965 struct osd_device *o;
5970 return ERR_PTR(-ENOMEM);
5972 rc = dt_device_init(&o->od_dt_dev, t);
5974 /* Because the ctx might be revived in dt_device_init,
5975 * refill the env here */
5976 lu_env_refill((struct lu_env *)env);
5977 rc = osd_device_init0(env, o, cfg);
5979 dt_device_fini(&o->od_dt_dev);
5982 if (unlikely(rc != 0))
5985 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5988 static struct lu_device *osd_device_free(const struct lu_env *env,
5989 struct lu_device *d)
5991 struct osd_device *o = osd_dev(d);
5994 /* XXX: make osd top device in order to release reference */
5995 d->ld_site->ls_top_dev = d;
5996 lu_site_purge(env, d->ld_site, -1);
5997 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5998 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5999 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6001 lu_site_fini(&o->od_site);
6002 dt_device_fini(&o->od_dt_dev);
6007 static int osd_process_config(const struct lu_env *env,
6008 struct lu_device *d, struct lustre_cfg *cfg)
6010 struct osd_device *o = osd_dev(d);
6014 switch (cfg->lcfg_command) {
6016 rc = osd_mount(env, o, cfg);
6019 lu_dev_del_linkage(d->ld_site, d);
6020 rc = osd_shutdown(env, o);
6023 LASSERT(&o->od_dt_dev);
6024 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6025 cfg, &o->od_dt_dev);
6026 if (rc > 0 || rc == -ENOSYS)
6027 rc = class_process_proc_param(PARAM_OST,
6028 lprocfs_osd_obd_vars,
6029 cfg, &o->od_dt_dev);
6038 static int osd_recovery_complete(const struct lu_env *env,
6039 struct lu_device *d)
6041 struct osd_device *osd = osd_dev(d);
6045 if (osd->od_quota_slave == NULL)
6048 /* start qsd instance on recovery completion, this notifies the quota
6049 * slave code that we are about to process new requests now */
6050 rc = qsd_start(env, osd->od_quota_slave);
6055 * we use exports to track all osd users
6057 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6058 struct obd_device *obd, struct obd_uuid *cluuid,
6059 struct obd_connect_data *data, void *localdata)
6061 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6062 struct lustre_handle conn;
6066 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6068 rc = class_connect(&conn, obd, cluuid);
6072 *exp = class_conn2export(&conn);
6074 spin_lock(&osd->od_osfs_lock);
6076 spin_unlock(&osd->od_osfs_lock);
6082 * once last export (we don't count self-export) disappeared
6083 * osd can be released
6085 static int osd_obd_disconnect(struct obd_export *exp)
6087 struct obd_device *obd = exp->exp_obd;
6088 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6089 int rc, release = 0;
6092 /* Only disconnect the underlying layers on the final disconnect. */
6093 spin_lock(&osd->od_osfs_lock);
6095 if (osd->od_connects == 0)
6097 spin_unlock(&osd->od_osfs_lock);
6099 rc = class_disconnect(exp); /* bz 9811 */
6101 if (rc == 0 && release)
6102 class_manual_cleanup(obd);
6106 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6107 struct lu_device *dev)
6109 struct osd_device *osd = osd_dev(dev);
6110 struct lr_server_data *lsd =
6111 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6115 if (osd->od_quota_slave != NULL) {
6116 /* set up quota slave objects */
6117 result = qsd_prepare(env, osd->od_quota_slave);
6122 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6123 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6124 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6125 osd->od_index_in_idif = 1;
6127 osd->od_index_in_idif = 0;
6128 result = osd_register_proc_index_in_idif(osd);
6133 osd->od_index_in_idif = 1;
6137 result = osd_fid_init(env, osd);
6142 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6143 struct lu_fid *fid, struct md_op_data *op_data)
6145 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6147 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6150 static const struct lu_object_operations osd_lu_obj_ops = {
6151 .loo_object_init = osd_object_init,
6152 .loo_object_delete = osd_object_delete,
6153 .loo_object_release = osd_object_release,
6154 .loo_object_free = osd_object_free,
6155 .loo_object_print = osd_object_print,
6156 .loo_object_invariant = osd_object_invariant
6159 const struct lu_device_operations osd_lu_ops = {
6160 .ldo_object_alloc = osd_object_alloc,
6161 .ldo_process_config = osd_process_config,
6162 .ldo_recovery_complete = osd_recovery_complete,
6163 .ldo_prepare = osd_prepare,
6166 static const struct lu_device_type_operations osd_device_type_ops = {
6167 .ldto_init = osd_type_init,
6168 .ldto_fini = osd_type_fini,
6170 .ldto_start = osd_type_start,
6171 .ldto_stop = osd_type_stop,
6173 .ldto_device_alloc = osd_device_alloc,
6174 .ldto_device_free = osd_device_free,
6176 .ldto_device_init = osd_device_init,
6177 .ldto_device_fini = osd_device_fini
6180 static struct lu_device_type osd_device_type = {
6181 .ldt_tags = LU_DEVICE_DT,
6182 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6183 .ldt_ops = &osd_device_type_ops,
6184 .ldt_ctx_tags = LCT_LOCAL,
6187 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6189 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6190 struct super_block *sb = osd_sb(osd);
6192 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6196 * lprocfs legacy support.
6198 static struct obd_ops osd_obd_device_ops = {
6199 .o_owner = THIS_MODULE,
6200 .o_connect = osd_obd_connect,
6201 .o_disconnect = osd_obd_disconnect,
6202 .o_fid_alloc = osd_fid_alloc,
6203 .o_health_check = osd_health_check,
6206 static int __init osd_mod_init(void)
6210 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6211 /* please, try to keep osd_thread_info smaller than a page */
6212 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6217 rc = lu_kmem_init(ldiskfs_caches);
6221 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6222 lprocfs_osd_module_vars,
6223 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6225 lu_kmem_fini(ldiskfs_caches);
6229 static void __exit osd_mod_exit(void)
6231 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6232 lu_kmem_fini(ldiskfs_caches);
6235 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6236 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6237 MODULE_VERSION(LUSTRE_VERSION_STRING);
6238 MODULE_LICENSE("GPL");
6240 module_init(osd_mod_init);
6241 module_exit(osd_mod_exit);