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>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
66 #include <lustre_param.h>
68 #include "osd_internal.h"
69 #include "osd_dynlocks.h"
71 /* llo_* api support */
72 #include <md_object.h>
73 #include <lustre_quota.h>
75 #include <ldiskfs/xattr.h>
76 #include <lustre_linkea.h>
79 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
80 "ldiskfs with parallel directory operations");
82 int ldiskfs_track_declares_assert;
83 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
84 "LBUG during tracking of declares");
86 /* Slab to allocate dynlocks */
87 struct kmem_cache *dynlock_cachep;
89 /* Slab to allocate osd_it_ea */
90 struct kmem_cache *osd_itea_cachep;
92 static struct lu_kmem_descr ldiskfs_caches[] = {
94 .ckd_cache = &dynlock_cachep,
95 .ckd_name = "dynlock_cache",
96 .ckd_size = sizeof(struct dynlock_handle)
99 .ckd_cache = &osd_itea_cachep,
100 .ckd_name = "osd_itea_cache",
101 .ckd_size = sizeof(struct osd_it_ea)
108 static const char dot[] = ".";
109 static const char dotdot[] = "..";
110 static const char remote_obj_dir[] = "REM_OBJ_DIR";
112 static const struct lu_object_operations osd_lu_obj_ops;
113 static const struct dt_object_operations osd_obj_ops;
114 static const struct dt_object_operations osd_obj_ea_ops;
115 static const struct dt_object_operations osd_obj_otable_it_ops;
116 static const struct dt_index_operations osd_index_iam_ops;
117 static const struct dt_index_operations osd_index_ea_ops;
119 int osd_trans_declare_op2rb[] = {
120 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
121 [OSD_OT_PUNCH] = OSD_OT_MAX,
122 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
123 [OSD_OT_CREATE] = OSD_OT_DESTROY,
124 [OSD_OT_DESTROY] = OSD_OT_CREATE,
125 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
126 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
127 [OSD_OT_WRITE] = OSD_OT_WRITE,
128 [OSD_OT_INSERT] = OSD_OT_DELETE,
129 [OSD_OT_DELETE] = OSD_OT_INSERT,
130 [OSD_OT_UPDATE] = OSD_OT_MAX,
131 [OSD_OT_QUOTA] = OSD_OT_MAX,
134 static int osd_has_index(const struct osd_object *obj)
136 return obj->oo_dt.do_index_ops != NULL;
139 static int osd_object_invariant(const struct lu_object *l)
141 return osd_invariant(osd_obj(l));
145 * Concurrency: doesn't matter
149 * Concurrency: doesn't matter
151 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
153 struct osd_thread_info *oti = osd_oti_get(env);
154 return oti->oti_w_locks > 0 && o->oo_owner == env;
158 * Concurrency: doesn't access mutable data
160 static int osd_root_get(const struct lu_env *env,
161 struct dt_device *dev, struct lu_fid *f)
163 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
168 * OSD object methods.
172 * Concurrency: no concurrent access is possible that early in object
175 static struct lu_object *osd_object_alloc(const struct lu_env *env,
176 const struct lu_object_header *hdr,
179 struct osd_object *mo;
185 l = &mo->oo_dt.do_lu;
186 dt_object_init(&mo->oo_dt, NULL, d);
187 mo->oo_dt.do_ops = &osd_obj_ea_ops;
188 l->lo_ops = &osd_lu_obj_ops;
189 init_rwsem(&mo->oo_sem);
190 init_rwsem(&mo->oo_ext_idx_sem);
191 spin_lock_init(&mo->oo_guard);
198 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
199 struct dentry *dentry, struct lustre_mdt_attrs *lma)
203 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
204 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
205 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
207 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
208 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
210 lustre_lma_swab(lma);
211 /* Check LMA compatibility */
212 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
213 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
214 "for fid = "DFID", ino = %lu\n",
215 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
216 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
217 PFID(&lma->lma_self_fid), inode->i_ino);
220 } else if (rc == 0) {
228 * retrieve object from backend ext fs.
230 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
231 struct osd_inode_id *id)
233 struct inode *inode = NULL;
235 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
237 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
238 id->oii_ino, PTR_ERR(inode));
239 } else if (id->oii_gen != OSD_OII_NOGEN &&
240 inode->i_generation != id->oii_gen) {
241 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
242 "i_generation = %u\n",
243 id->oii_ino, id->oii_gen, inode->i_generation);
245 inode = ERR_PTR(-ESTALE);
246 } else if (inode->i_nlink == 0) {
247 /* due to parallel readdir and unlink,
248 * we can have dead inode here. */
249 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
251 inode = ERR_PTR(-ESTALE);
252 } else if (is_bad_inode(inode)) {
253 CWARN("%.16s: bad inode: ino = %u\n",
254 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
256 inode = ERR_PTR(-ENOENT);
258 if (id->oii_gen == OSD_OII_NOGEN)
259 osd_id_gen(id, inode->i_ino, inode->i_generation);
261 /* Do not update file c/mtime in ldiskfs.
262 * NB: we don't have any lock to protect this because we don't
263 * have reference on osd_object now, but contention with
264 * another lookup + attr_set can't happen in the tiny window
265 * between if (...) and set S_NOCMTIME. */
266 if (!(inode->i_flags & S_NOCMTIME))
267 inode->i_flags |= S_NOCMTIME;
272 static struct inode *
273 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
274 struct osd_inode_id *id, struct lu_fid *fid)
276 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
280 inode = osd_iget(info, dev, id);
284 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
286 *fid = lma->lma_self_fid;
287 } else if (rc == -ENODATA) {
288 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
289 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
291 lu_igif_build(fid, inode->i_ino, inode->i_generation);
299 static struct inode *osd_iget_check(struct osd_thread_info *info,
300 struct osd_device *dev,
301 const struct lu_fid *fid,
302 struct osd_inode_id *id,
309 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
312 if (!in_oi || (rc != -ENOENT && rc != -ESTALE)) {
313 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
322 if (is_bad_inode(inode)) {
325 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
333 if (id->oii_gen != OSD_OII_NOGEN &&
334 inode->i_generation != id->oii_gen) {
337 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
338 "oii_gen = %u, i_generation = %u\n",
339 id->oii_ino, id->oii_gen, inode->i_generation);
347 if (inode->i_nlink == 0) {
350 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
360 struct osd_inode_id saved_id = *id;
362 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
364 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
365 /* XXX: There are some possible cases:
367 * Backup/restore caused the OI invalid.
369 * Someone unlinked the object but NOT removed
370 * the OI mapping, such as mount target device
371 * as ldiskfs, and modify something directly.
373 * Someone just removed the object between the
374 * former oi_lookup and the iget. It is normal.
375 * 4. Other failure cases.
377 * Generally, when the device is mounted, it will
378 * auto check whether the system is restored from
379 * file-level backup or not. We trust such detect
380 * to distinguish the 1st case from the 2nd case. */
382 if (!IS_ERR(inode) && inode->i_generation != 0 &&
383 inode->i_generation == id->oii_gen) {
386 __u32 level = D_LFSCK;
389 if (!thread_is_running(&dev->od_scrub.os_thread))
392 CDEBUG(level, "%s: the OI mapping for the FID "
393 DFID" become inconsistent, the given ID "
394 "%u/%u, the ID in OI mapping %u/%u\n",
395 osd_name(dev), PFID(fid),
396 saved_id.oii_ino, saved_id.oii_gen,
397 id->oii_ino, id->oii_ino);
401 if (id->oii_gen == OSD_OII_NOGEN)
402 osd_id_gen(id, inode->i_ino, inode->i_generation);
404 /* Do not update file c/mtime in ldiskfs.
405 * NB: we don't have any lock to protect this because we don't
406 * have reference on osd_object now, but contention with
407 * another lookup + attr_set can't happen in the tiny window
408 * between if (...) and set S_NOCMTIME. */
409 if (!(inode->i_flags & S_NOCMTIME))
410 inode->i_flags |= S_NOCMTIME;
427 * \retval +v: new filter_fid, does not contain self-fid
428 * \retval 0: filter_fid_old, contains self-fid
429 * \retval -v: other failure cases
431 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
432 struct dentry *dentry, struct lu_fid *fid)
434 struct filter_fid_old *ff = &info->oti_ff;
435 struct ost_id *ostid = &info->oti_ostid;
438 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
439 if (rc == sizeof(*ff)) {
441 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
442 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
443 /* XXX: should use real OST index in the future. LU-3569 */
444 ostid_to_fid(fid, ostid, 0);
445 } else if (rc == sizeof(struct filter_fid)) {
447 } else if (rc >= 0) {
454 static int osd_lma_self_repair(struct osd_thread_info *info,
455 struct osd_device *osd, struct inode *inode,
456 const struct lu_fid *fid, __u32 compat)
461 LASSERT(current->journal_info == NULL);
463 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
464 osd_dto_credits_noquota[DTO_XATTR_SET]);
467 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
472 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
474 CWARN("%s: cannot self repair the LMA: rc = %d\n",
476 ldiskfs_journal_stop(jh);
480 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
482 struct osd_thread_info *info = osd_oti_get(env);
483 struct osd_device *osd = osd_obj2dev(obj);
484 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
485 struct inode *inode = obj->oo_inode;
486 struct dentry *dentry = &info->oti_obj_dentry;
487 struct lu_fid *fid = NULL;
488 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
492 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
493 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
494 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
495 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
496 fid = &lma->lma_self_fid;
497 rc = osd_get_idif(info, inode, dentry, fid);
498 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
499 /* For the given OST-object, if it has neither LMA nor
500 * FID in XATTR_NAME_FID, then the given FID (which is
501 * contained in the @obj, from client RPC for locating
502 * the OST-object) is trusted. We use it to generate
504 osd_lma_self_repair(info, osd, inode, rfid,
510 if (unlikely(rc == -ENODATA))
518 lustre_lma_swab(lma);
519 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
520 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
521 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
522 "fid = "DFID", ino = %lu\n", osd_name(osd),
523 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
524 PFID(rfid), inode->i_ino);
526 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
527 fid = &lma->lma_self_fid;
531 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
532 __u32 level = D_LFSCK;
534 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
535 struct ost_id *oi = &info->oti_ostid;
536 struct lu_fid *fid1 = &info->oti_fid3;
537 __u32 idx = fid_idif_ost_idx(rfid);
539 /* For old IDIF, the OST index is not part of the IDIF,
540 * Means that different OSTs may have the same IDIFs.
541 * Under such case, we need to make some compatible
542 * check to make sure to trigger OI scrub properly. */
543 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
544 /* Given @rfid is new, LMA is old. */
545 fid_to_ostid(fid, oi);
546 ostid_to_fid(fid1, oi, idx);
547 if (lu_fid_eq(fid1, rfid)) {
548 if (osd->od_index_in_idif)
549 osd_lma_self_repair(info, osd,
559 if (!thread_is_running(&osd->od_scrub.os_thread))
562 CDEBUG(level, "%s: FID "DFID" != self_fid "DFID"\n",
563 osd_name(osd), PFID(rfid), PFID(fid));
569 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
570 const struct lu_fid *fid,
571 const struct lu_object_conf *conf)
573 struct osd_thread_info *info;
574 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
575 struct osd_device *dev;
576 struct osd_idmap_cache *oic;
577 struct osd_inode_id *id;
579 struct osd_scrub *scrub;
580 struct scrub_file *sf;
584 bool triggered = false;
587 LINVRNT(osd_invariant(obj));
588 LASSERT(obj->oo_inode == NULL);
589 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
592 scrub = &dev->od_scrub;
593 sf = &scrub->os_file;
594 info = osd_oti_get(env);
596 oic = &info->oti_cache;
598 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
601 /* For the object is created as locking anchor, or for the object to
602 * be created on disk. No need to osd_oi_lookup() at here because FID
603 * shouldn't never be re-used, if it's really a duplicate FID from
604 * unexpected reason, we should be able to detect it later by calling
605 * do_create->osd_oi_insert(). */
606 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
607 GOTO(out, result = 0);
609 /* Search order: 1. per-thread cache. */
610 if (lu_fid_eq(fid, &oic->oic_fid) &&
611 likely(oic->oic_dev == dev)) {
617 if (!list_empty(&scrub->os_inconsistent_items)) {
618 /* Search order: 2. OI scrub pending list. */
619 result = osd_oii_lookup(dev, fid, id);
624 /* Search order: 3. OI files. */
625 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
626 if (result == -ENOENT) {
627 if (!fid_is_norm(fid) ||
628 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
629 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
631 GOTO(out, result = 0);
642 inode = osd_iget_check(info, dev, fid, id, in_oi);
644 result = PTR_ERR(inode);
645 if (result == -ENOENT || result == -ESTALE) {
647 fid_zero(&oic->oic_fid);
649 GOTO(out, result = -ENOENT);
650 } else if (result == -EREMCHG) {
654 fid_zero(&oic->oic_fid);
656 if (unlikely(triggered))
657 GOTO(out, result = saved);
660 if (thread_is_running(&scrub->os_thread)) {
661 result = -EINPROGRESS;
662 } else if (!dev->od_noscrub) {
663 /* Since we do not know the right OI mapping,
664 * we have to trigger OI scrub to scan the
666 result = osd_scrub_start(dev, SS_AUTO_FULL |
667 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
668 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI "
669 "scrub by RPC for "DFID", rc = %d [1]\n",
670 osd_name(dev), PFID(fid),result);
671 if (result == 0 || result == -EALREADY)
672 result = -EINPROGRESS;
677 /* We still have chance to get the valid inode: for the
678 * object which is referenced by remote name entry, the
679 * object on the local MDT will be linked under the dir
680 * of "/REMOTE_PARENT_DIR" with its FID string as name.
682 * We do not know whether the object for the given FID
683 * is referenced by some remote name entry or not, and
684 * especially for DNE II, a multiple-linked object may
685 * have many name entries reside on many MDTs.
687 * To simplify the operation, OSD will not distinguish
688 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
689 * only happened for the RPC from other MDT during the
690 * OI scrub, or for the client side RPC with FID only,
691 * such as FID to path, or from old connected client. */
693 result = osd_lookup_in_remote_parent(info, dev,
706 obj->oo_inode = inode;
707 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
709 result = osd_check_lma(env, obj);
712 obj->oo_inode = NULL;
713 if (result == -EREMCHG) {
715 result = osd_oi_lookup(info, dev, fid, id,
718 fid_zero(&oic->oic_fid);
729 obj->oo_compat_dot_created = 1;
730 obj->oo_compat_dotdot_created = 1;
732 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
733 GOTO(out, result = 0);
735 LASSERT(obj->oo_hl_head == NULL);
736 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
737 if (obj->oo_hl_head == NULL) {
738 obj->oo_inode = NULL;
740 GOTO(out, result = -ENOMEM);
742 GOTO(out, result = 0);
745 LINVRNT(osd_invariant(obj));
750 * Concurrency: shouldn't matter.
752 static void osd_object_init0(struct osd_object *obj)
754 LASSERT(obj->oo_inode != NULL);
755 obj->oo_dt.do_body_ops = &osd_body_ops;
756 obj->oo_dt.do_lu.lo_header->loh_attr |=
757 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
761 * Concurrency: no concurrent access is possible that early in object
764 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
765 const struct lu_object_conf *conf)
767 struct osd_object *obj = osd_obj(l);
770 LINVRNT(osd_invariant(obj));
772 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
773 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
774 l->lo_header->loh_attr |= LOHA_EXISTS;
778 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
779 obj->oo_dt.do_body_ops = &osd_body_ops_new;
780 if (result == 0 && obj->oo_inode != NULL)
781 osd_object_init0(obj);
783 LINVRNT(osd_invariant(obj));
788 * Concurrency: no concurrent access is possible that late in object
791 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
793 struct osd_object *obj = osd_obj(l);
795 LINVRNT(osd_invariant(obj));
797 dt_object_fini(&obj->oo_dt);
798 if (obj->oo_hl_head != NULL)
799 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
804 * Concurrency: no concurrent access is possible that late in object
807 static void osd_index_fini(struct osd_object *o)
809 struct iam_container *bag;
811 if (o->oo_dir != NULL) {
812 bag = &o->oo_dir->od_container;
813 if (o->oo_inode != NULL) {
814 if (bag->ic_object == o->oo_inode)
815 iam_container_fini(bag);
817 OBD_FREE_PTR(o->oo_dir);
823 * Concurrency: no concurrent access is possible that late in object
824 * life-cycle (for all existing callers, that is. New callers have to provide
825 * their own locking.)
827 static int osd_inode_unlinked(const struct inode *inode)
829 return inode->i_nlink == 0;
833 OSD_TXN_OI_DELETE_CREDITS = 20,
834 OSD_TXN_INODE_DELETE_CREDITS = 20
841 #if OSD_THANDLE_STATS
843 * Set time when the handle is allocated
845 static void osd_th_alloced(struct osd_thandle *oth)
847 oth->oth_alloced = cfs_time_current();
851 * Set time when the handle started
853 static void osd_th_started(struct osd_thandle *oth)
855 oth->oth_started = cfs_time_current();
859 * Helper function to convert time interval to microseconds packed in
862 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
866 cfs_duration_usec(cfs_time_sub(end, start), &val);
867 return val.tv_sec * 1000000 + val.tv_usec;
871 * Check whether the we deal with this handle for too long.
873 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
874 cfs_time_t alloced, cfs_time_t started,
877 cfs_time_t now = cfs_time_current();
879 LASSERT(dev != NULL);
881 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
882 interval_to_usec(alloced, started));
883 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
884 interval_to_usec(started, closed));
885 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
886 interval_to_usec(closed, now));
888 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
889 CWARN("transaction handle %p was open for too long: "
891 "alloced "CFS_TIME_T" ,"
892 "started "CFS_TIME_T" ,"
893 "closed "CFS_TIME_T"\n",
894 oth, now, alloced, started, closed);
895 libcfs_debug_dumpstack(NULL);
899 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
901 cfs_time_t __closed = cfs_time_current(); \
902 cfs_time_t __alloced = oth->oth_alloced; \
903 cfs_time_t __started = oth->oth_started; \
906 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
909 #else /* OSD_THANDLE_STATS */
911 #define osd_th_alloced(h) do {} while(0)
912 #define osd_th_started(h) do {} while(0)
913 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
915 #endif /* OSD_THANDLE_STATS */
918 * Concurrency: doesn't access mutable data.
920 static int osd_param_is_not_sane(const struct osd_device *dev,
921 const struct thandle *th)
923 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
925 return oh->ot_credits > osd_transaction_size(dev);
929 * Concurrency: shouldn't matter.
931 static void osd_trans_commit_cb(struct super_block *sb,
932 struct ldiskfs_journal_cb_entry *jcb, int error)
934 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
935 struct thandle *th = &oh->ot_super;
936 struct lu_device *lud = &th->th_dev->dd_lu_dev;
937 struct dt_txn_commit_cb *dcb, *tmp;
939 LASSERT(oh->ot_handle == NULL);
942 CERROR("transaction @0x%p commit error: %d\n", th, error);
944 dt_txn_hook_commit(th);
946 /* call per-transaction callbacks if any */
947 list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
948 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
949 "commit callback entry: magic=%x name='%s'\n",
950 dcb->dcb_magic, dcb->dcb_name);
951 list_del_init(&dcb->dcb_linkage);
952 dcb->dcb_func(NULL, th, dcb, error);
955 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
959 lu_context_exit(&th->th_ctx);
960 lu_context_fini(&th->th_ctx);
964 static struct thandle *osd_trans_create(const struct lu_env *env,
967 struct osd_thread_info *oti = osd_oti_get(env);
968 struct osd_iobuf *iobuf = &oti->oti_iobuf;
969 struct osd_thandle *oh;
973 /* on pending IO in this thread should left from prev. request */
974 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
976 th = ERR_PTR(-ENOMEM);
977 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
979 oh->ot_quota_trans = &oti->oti_quota_trans;
980 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
984 th->th_tags = LCT_TX_HANDLE;
986 atomic_set(&th->th_refc, 1);
987 th->th_alloc_size = sizeof(*oh);
988 oti->oti_dev = osd_dt_dev(d);
989 INIT_LIST_HEAD(&oh->ot_dcb_list);
992 memset(oti->oti_declare_ops, 0,
993 sizeof(oti->oti_declare_ops));
994 memset(oti->oti_declare_ops_rb, 0,
995 sizeof(oti->oti_declare_ops_rb));
996 memset(oti->oti_declare_ops_cred, 0,
997 sizeof(oti->oti_declare_ops_cred));
998 oti->oti_rollback = false;
1004 * Concurrency: shouldn't matter.
1006 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1009 struct osd_thread_info *oti = osd_oti_get(env);
1010 struct osd_device *dev = osd_dt_dev(d);
1012 struct osd_thandle *oh;
1017 LASSERT(current->journal_info == NULL);
1019 oh = container_of0(th, struct osd_thandle, ot_super);
1020 LASSERT(oh != NULL);
1021 LASSERT(oh->ot_handle == NULL);
1023 rc = dt_txn_hook_start(env, d, th);
1027 if (unlikely(osd_param_is_not_sane(dev, th))) {
1028 static unsigned long last_printed;
1029 static int last_credits;
1031 CWARN("%.16s: too many transaction credits (%d > %d)\n",
1032 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
1034 osd_journal(dev)->j_max_transaction_buffers);
1035 CWARN(" create: %u/%u, destroy: %u/%u\n",
1036 oti->oti_declare_ops[OSD_OT_CREATE],
1037 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1038 oti->oti_declare_ops[OSD_OT_DESTROY],
1039 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
1040 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
1041 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1042 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1043 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1044 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
1045 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
1046 oti->oti_declare_ops[OSD_OT_WRITE],
1047 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1048 oti->oti_declare_ops[OSD_OT_PUNCH],
1049 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1050 oti->oti_declare_ops[OSD_OT_QUOTA],
1051 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
1052 CWARN(" insert: %u/%u, delete: %u/%u\n",
1053 oti->oti_declare_ops[OSD_OT_INSERT],
1054 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1055 oti->oti_declare_ops[OSD_OT_DELETE],
1056 oti->oti_declare_ops_cred[OSD_OT_DELETE]);
1057 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
1058 oti->oti_declare_ops[OSD_OT_REF_ADD],
1059 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1060 oti->oti_declare_ops[OSD_OT_REF_DEL],
1061 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
1063 if (last_credits != oh->ot_credits &&
1064 time_after(jiffies, last_printed +
1065 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
1066 libcfs_debug_dumpstack(NULL);
1067 last_credits = oh->ot_credits;
1068 last_printed = jiffies;
1070 /* XXX Limit the credits to 'max_transaction_buffers', and
1071 * let the underlying filesystem to catch the error if
1072 * we really need so many credits.
1074 * This should be removed when we can calculate the
1075 * credits precisely. */
1076 oh->ot_credits = osd_transaction_size(dev);
1080 * XXX temporary stuff. Some abstraction layer should
1083 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1087 LASSERT(oti->oti_txns == 0);
1088 lu_context_init(&th->th_ctx, th->th_tags);
1089 lu_context_enter(&th->th_ctx);
1091 lu_device_get(&d->dd_lu_dev);
1092 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1103 static int osd_seq_exists(const struct lu_env *env,
1104 struct osd_device *osd, u64 seq)
1106 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1107 struct seq_server_site *ss = osd_seq_site(osd);
1111 LASSERT(ss != NULL);
1112 LASSERT(ss->ss_server_fld != NULL);
1114 rc = osd_fld_lookup(env, osd, seq, range);
1117 CERROR("%s: can't lookup FLD sequence "LPX64
1118 ": rc = %d\n", osd_name(osd), seq, rc);
1122 RETURN(ss->ss_node_id == range->lsr_index);
1126 * Concurrency: shouldn't matter.
1128 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1132 struct osd_thandle *oh;
1133 struct osd_thread_info *oti = osd_oti_get(env);
1134 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1135 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
1136 struct lquota_trans *qtrans;
1139 oh = container_of0(th, struct osd_thandle, ot_super);
1141 qtrans = oh->ot_quota_trans;
1142 oh->ot_quota_trans = NULL;
1144 if (oh->ot_handle != NULL) {
1145 handle_t *hdl = oh->ot_handle;
1148 * add commit callback
1149 * notice we don't do this in osd_trans_start()
1150 * as underlying transaction can change during truncate
1152 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1155 LASSERT(oti->oti_txns == 1);
1157 rc = dt_txn_hook_stop(env, th);
1159 CERROR("Failure in transaction hook: %d\n", rc);
1161 /* hook functions might modify th_sync */
1162 hdl->h_sync = th->th_sync;
1164 oh->ot_handle = NULL;
1165 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
1166 rc = ldiskfs_journal_stop(hdl));
1168 CERROR("Failure to stop transaction: %d\n", rc);
1170 thandle_put(&oh->ot_super);
1173 /* inform the quota slave device that the transaction is stopping */
1174 qsd_op_end(env, qsd, qtrans);
1176 /* as we want IO to journal and data IO be concurrent, we don't block
1177 * awaiting data IO completion in osd_do_bio(), instead we wait here
1178 * once transaction is submitted to the journal. all reqular requests
1179 * don't do direct IO (except read/write), thus this wait_event becomes
1182 * IMPORTANT: we have to wait till any IO submited by the thread is
1183 * completed otherwise iobuf may be corrupted by different request
1185 wait_event(iobuf->dr_wait,
1186 atomic_read(&iobuf->dr_numreqs) == 0);
1187 osd_fini_iobuf(oti->oti_dev, iobuf);
1189 rc = iobuf->dr_error;
1194 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1196 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1199 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1200 LASSERT(&dcb->dcb_func != NULL);
1201 list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
1207 * Called just before object is freed. Releases all resources except for
1208 * object itself (that is released by osd_object_free()).
1210 * Concurrency: no concurrent access is possible that late in object
1213 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1215 struct osd_object *obj = osd_obj(l);
1216 struct inode *inode = obj->oo_inode;
1218 LINVRNT(osd_invariant(obj));
1221 * If object is unlinked remove fid->ino mapping from object index.
1224 osd_index_fini(obj);
1225 if (inode != NULL) {
1226 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1227 qid_t uid = i_uid_read(inode);
1228 qid_t gid = i_gid_read(inode);
1231 obj->oo_inode = NULL;
1234 struct osd_thread_info *info = osd_oti_get(env);
1235 struct lquota_id_info *qi = &info->oti_qi;
1237 /* Release granted quota to master if necessary */
1238 qi->lqi_id.qid_uid = uid;
1239 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1241 qi->lqi_id.qid_uid = gid;
1242 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1248 * Concurrency: ->loo_object_release() is called under site spin-lock.
1250 static void osd_object_release(const struct lu_env *env,
1251 struct lu_object *l)
1256 * Concurrency: shouldn't matter.
1258 static int osd_object_print(const struct lu_env *env, void *cookie,
1259 lu_printer_t p, const struct lu_object *l)
1261 struct osd_object *o = osd_obj(l);
1262 struct iam_descr *d;
1264 if (o->oo_dir != NULL)
1265 d = o->oo_dir->od_container.ic_descr;
1268 return (*p)(env, cookie,
1269 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1271 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1272 o->oo_inode ? o->oo_inode->i_generation : 0,
1273 d ? d->id_ops->id_name : "plain");
1276 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1279 * Concurrency: shouldn't matter.
1281 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1282 struct obd_statfs *sfs)
1284 struct osd_device *osd = osd_dt_dev(d);
1285 struct super_block *sb = osd_sb(osd);
1286 struct kstatfs *ksfs;
1289 if (unlikely(osd->od_mnt == NULL))
1290 return -EINPROGRESS;
1292 /* osd_lproc.c call this without env, allocate ksfs for that case */
1293 if (unlikely(env == NULL)) {
1294 OBD_ALLOC_PTR(ksfs);
1298 ksfs = &osd_oti_get(env)->oti_ksfs;
1301 spin_lock(&osd->od_osfs_lock);
1302 result = sb->s_op->statfs(sb->s_root, ksfs);
1303 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1304 statfs_pack(sfs, ksfs);
1305 if (sb->s_flags & MS_RDONLY)
1306 sfs->os_state = OS_STATE_READONLY;
1309 spin_unlock(&osd->od_osfs_lock);
1311 if (unlikely(env == NULL))
1314 /* Reserve a small amount of space for local objects like last_rcvd,
1315 * llog, quota files, ... */
1316 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1319 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1320 /** Take out metadata overhead for indirect blocks */
1321 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1328 * Estimate space needed for file creations. We assume the largest filename
1329 * which is 2^64 - 1, hence a filename of 20 chars.
1330 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1332 #ifdef __LDISKFS_DIR_REC_LEN
1333 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1335 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1339 * Concurrency: doesn't access mutable data.
1341 static void osd_conf_get(const struct lu_env *env,
1342 const struct dt_device *dev,
1343 struct dt_device_param *param)
1345 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1349 * XXX should be taken from not-yet-existing fs abstraction layer.
1351 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1352 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1353 param->ddp_block_shift = sb->s_blocksize_bits;
1354 param->ddp_mount_type = LDD_MT_LDISKFS;
1355 param->ddp_maxbytes = sb->s_maxbytes;
1356 /* Overhead estimate should be fairly accurate, so we really take a tiny
1357 * error margin which also avoids fragmenting the filesystem too much */
1358 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1359 /* inode are statically allocated, so per-inode space consumption
1360 * is the space consumed by the directory entry */
1361 param->ddp_inodespace = PER_OBJ_USAGE;
1362 /* per-fragment overhead to be used by the client code */
1363 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1364 param->ddp_mntopts = 0;
1365 if (test_opt(sb, XATTR_USER))
1366 param->ddp_mntopts |= MNTOPT_USERXATTR;
1367 if (test_opt(sb, POSIX_ACL))
1368 param->ddp_mntopts |= MNTOPT_ACL;
1370 /* LOD might calculate the max stripe count based on max_ea_size,
1371 * so we need take account in the overhead as well,
1372 * xattr_header + magic + xattr_entry_head */
1373 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1374 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1376 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1377 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1378 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1382 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1386 * Concurrency: shouldn't matter.
1388 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1392 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1394 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1396 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1397 LUSTRE_OSD_LDISKFS_NAME, rc);
1403 * Start commit for OSD device.
1405 * An implementation of dt_commit_async method for OSD device.
1406 * Asychronously starts underlayng fs sync and thereby a transaction
1409 * \param env environment
1410 * \param d dt device
1412 * \see dt_device_operations
1414 static int osd_commit_async(const struct lu_env *env,
1415 struct dt_device *d)
1417 struct super_block *s = osd_sb(osd_dt_dev(d));
1420 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1421 RETURN(s->s_op->sync_fs(s, 0));
1425 * Concurrency: shouldn't matter.
1428 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1430 struct super_block *sb = osd_sb(osd_dt_dev(d));
1431 struct block_device *dev = sb->s_bdev;
1432 #ifdef HAVE_DEV_SET_RDONLY
1433 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1436 int rc = -EOPNOTSUPP;
1440 #ifdef HAVE_DEV_SET_RDONLY
1441 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1443 if (jdev && (jdev != dev)) {
1444 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1446 dev_set_rdonly(jdev);
1448 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1449 dev_set_rdonly(dev);
1451 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1452 osd_dt_dev(d)->od_svname, (long)dev, rc);
1458 * Concurrency: serialization provided by callers.
1460 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1461 int mode, unsigned long timeout, __u32 alg,
1462 struct lustre_capa_key *keys)
1464 struct osd_device *dev = osd_dt_dev(d);
1467 dev->od_fl_capa = mode;
1468 dev->od_capa_timeout = timeout;
1469 dev->od_capa_alg = alg;
1470 dev->od_capa_keys = keys;
1475 * Note: we do not count into QUOTA here.
1476 * If we mount with --data_journal we may need more.
1478 const int osd_dto_credits_noquota[DTO_NR] = {
1481 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1482 * SINGLEDATA_TRANS_BLOCKS(8)
1483 * XXX Note: maybe iam need more, since iam have more level than
1486 [DTO_INDEX_INSERT] = 16,
1489 * just modify a single entry, probably merge few within a block
1491 [DTO_INDEX_DELETE] = 1,
1495 [DTO_INDEX_UPDATE] = 16,
1497 * 4(inode, inode bits, groups, GDT)
1498 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1500 [DTO_OBJECT_CREATE] = 4,
1502 * 4(inode, inode bits, groups, GDT)
1503 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1505 [DTO_OBJECT_DELETE] = 4,
1507 * Attr set credits (inode)
1509 [DTO_ATTR_SET_BASE] = 1,
1511 * Xattr set. The same as xattr of EXT3.
1512 * DATA_TRANS_BLOCKS(14)
1513 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1514 * are also counted in. Do not know why?
1516 [DTO_XATTR_SET] = 14,
1518 * credits for inode change during write.
1520 [DTO_WRITE_BASE] = 3,
1522 * credits for single block write.
1524 [DTO_WRITE_BLOCK] = 14,
1526 * Attr set credits for chown.
1527 * This is extra credits for setattr, and it is null without quota
1529 [DTO_ATTR_SET_CHOWN] = 0
1532 static const struct dt_device_operations osd_dt_ops = {
1533 .dt_root_get = osd_root_get,
1534 .dt_statfs = osd_statfs,
1535 .dt_trans_create = osd_trans_create,
1536 .dt_trans_start = osd_trans_start,
1537 .dt_trans_stop = osd_trans_stop,
1538 .dt_trans_cb_add = osd_trans_cb_add,
1539 .dt_conf_get = osd_conf_get,
1540 .dt_sync = osd_sync,
1542 .dt_commit_async = osd_commit_async,
1543 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1546 static void osd_object_read_lock(const struct lu_env *env,
1547 struct dt_object *dt, unsigned role)
1549 struct osd_object *obj = osd_dt_obj(dt);
1550 struct osd_thread_info *oti = osd_oti_get(env);
1552 LINVRNT(osd_invariant(obj));
1554 LASSERT(obj->oo_owner != env);
1555 down_read_nested(&obj->oo_sem, role);
1557 LASSERT(obj->oo_owner == NULL);
1561 static void osd_object_write_lock(const struct lu_env *env,
1562 struct dt_object *dt, unsigned role)
1564 struct osd_object *obj = osd_dt_obj(dt);
1565 struct osd_thread_info *oti = osd_oti_get(env);
1567 LINVRNT(osd_invariant(obj));
1569 LASSERT(obj->oo_owner != env);
1570 down_write_nested(&obj->oo_sem, role);
1572 LASSERT(obj->oo_owner == NULL);
1573 obj->oo_owner = env;
1577 static void osd_object_read_unlock(const struct lu_env *env,
1578 struct dt_object *dt)
1580 struct osd_object *obj = osd_dt_obj(dt);
1581 struct osd_thread_info *oti = osd_oti_get(env);
1583 LINVRNT(osd_invariant(obj));
1585 LASSERT(oti->oti_r_locks > 0);
1587 up_read(&obj->oo_sem);
1590 static void osd_object_write_unlock(const struct lu_env *env,
1591 struct dt_object *dt)
1593 struct osd_object *obj = osd_dt_obj(dt);
1594 struct osd_thread_info *oti = osd_oti_get(env);
1596 LINVRNT(osd_invariant(obj));
1598 LASSERT(obj->oo_owner == env);
1599 LASSERT(oti->oti_w_locks > 0);
1601 obj->oo_owner = NULL;
1602 up_write(&obj->oo_sem);
1605 static int osd_object_write_locked(const struct lu_env *env,
1606 struct dt_object *dt)
1608 struct osd_object *obj = osd_dt_obj(dt);
1610 LINVRNT(osd_invariant(obj));
1612 return obj->oo_owner == env;
1615 static int capa_is_sane(const struct lu_env *env,
1616 struct osd_device *dev,
1617 struct lustre_capa *capa,
1618 struct lustre_capa_key *keys)
1620 struct osd_thread_info *oti = osd_oti_get(env);
1621 struct lustre_capa *tcapa = &oti->oti_capa;
1622 struct obd_capa *oc;
1626 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1628 if (capa_is_expired(oc)) {
1629 DEBUG_CAPA(D_ERROR, capa, "expired");
1636 if (capa_is_expired_sec(capa)) {
1637 DEBUG_CAPA(D_ERROR, capa, "expired");
1641 spin_lock(&capa_lock);
1642 for (i = 0; i < 2; i++) {
1643 if (keys[i].lk_keyid == capa->lc_keyid) {
1644 oti->oti_capa_key = keys[i];
1648 spin_unlock(&capa_lock);
1651 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1655 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1659 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1660 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1664 oc = capa_add(dev->od_capa_hash, capa);
1670 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1671 struct lustre_capa *capa, __u64 opc)
1673 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1674 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1675 struct lu_capainfo *lci;
1678 if (!osd->od_fl_capa)
1681 if (capa == BYPASS_CAPA)
1684 lci = lu_capainfo_get(env);
1685 if (unlikely(lci == NULL))
1688 if (lci->lci_auth == LC_ID_NONE)
1692 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1693 osd_name(osd), PFID(fid), -EACCES);
1697 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1698 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1703 if (!capa_opc_supported(capa, opc)) {
1704 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1708 rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1710 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1717 static struct timespec *osd_inode_time(const struct lu_env *env,
1718 struct inode *inode, __u64 seconds)
1720 struct osd_thread_info *oti = osd_oti_get(env);
1721 struct timespec *t = &oti->oti_time;
1723 t->tv_sec = seconds;
1725 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1730 static void osd_inode_getattr(const struct lu_env *env,
1731 struct inode *inode, struct lu_attr *attr)
1733 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1734 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1735 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1738 attr->la_atime = LTIME_S(inode->i_atime);
1739 attr->la_mtime = LTIME_S(inode->i_mtime);
1740 attr->la_ctime = LTIME_S(inode->i_ctime);
1741 attr->la_mode = inode->i_mode;
1742 attr->la_size = i_size_read(inode);
1743 attr->la_blocks = inode->i_blocks;
1744 attr->la_uid = i_uid_read(inode);
1745 attr->la_gid = i_gid_read(inode);
1746 attr->la_flags = LDISKFS_I(inode)->i_flags;
1747 attr->la_nlink = inode->i_nlink;
1748 attr->la_rdev = inode->i_rdev;
1749 attr->la_blksize = 1 << inode->i_blkbits;
1750 attr->la_blkbits = inode->i_blkbits;
1753 static int osd_attr_get(const struct lu_env *env,
1754 struct dt_object *dt,
1755 struct lu_attr *attr,
1756 struct lustre_capa *capa)
1758 struct osd_object *obj = osd_dt_obj(dt);
1760 if (!dt_object_exists(dt))
1763 LASSERT(!dt_object_remote(dt));
1764 LINVRNT(osd_invariant(obj));
1766 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1769 spin_lock(&obj->oo_guard);
1770 osd_inode_getattr(env, obj->oo_inode, attr);
1771 spin_unlock(&obj->oo_guard);
1775 static int osd_declare_attr_set(const struct lu_env *env,
1776 struct dt_object *dt,
1777 const struct lu_attr *attr,
1778 struct thandle *handle)
1780 struct osd_thandle *oh;
1781 struct osd_object *obj;
1782 struct osd_thread_info *info = osd_oti_get(env);
1783 struct lquota_id_info *qi = &info->oti_qi;
1791 LASSERT(dt != NULL);
1792 LASSERT(handle != NULL);
1794 obj = osd_dt_obj(dt);
1795 LASSERT(osd_invariant(obj));
1797 oh = container_of0(handle, struct osd_thandle, ot_super);
1798 LASSERT(oh->ot_handle == NULL);
1800 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1801 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1803 if (attr == NULL || obj->oo_inode == NULL)
1806 bspace = obj->oo_inode->i_blocks;
1807 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1808 bspace = toqb(bspace);
1810 /* Changing ownership is always preformed by super user, it should not
1813 * We still need to call the osd_declare_qid() to calculate the journal
1814 * credits for updating quota accounting files and to trigger quota
1815 * space adjustment once the operation is completed.*/
1816 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
1818 uid = i_uid_read(obj->oo_inode);
1819 qi->lqi_type = USRQUOTA;
1820 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
1821 /* inode accounting */
1822 qi->lqi_is_blk = false;
1824 /* one more inode for the new uid ... */
1825 qi->lqi_id.qid_uid = attr->la_uid;
1827 /* Reserve credits for the new uid */
1828 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1829 if (rc == -EDQUOT || rc == -EINPROGRESS)
1834 /* and one less inode for the current uid */
1835 qi->lqi_id.qid_uid = uid;
1837 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1838 if (rc == -EDQUOT || rc == -EINPROGRESS)
1843 /* block accounting */
1844 qi->lqi_is_blk = true;
1846 /* more blocks for the new uid ... */
1847 qi->lqi_id.qid_uid = attr->la_uid;
1848 qi->lqi_space = bspace;
1850 * Credits for the new uid has been reserved, re-use "obj"
1851 * to save credit reservation.
1853 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1854 if (rc == -EDQUOT || rc == -EINPROGRESS)
1859 /* and finally less blocks for the current uid */
1860 qi->lqi_id.qid_uid = uid;
1861 qi->lqi_space = -bspace;
1862 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1863 if (rc == -EDQUOT || rc == -EINPROGRESS)
1869 gid = i_gid_read(obj->oo_inode);
1870 qi->lqi_type = GRPQUOTA;
1871 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
1873 /* inode accounting */
1874 qi->lqi_is_blk = false;
1876 /* one more inode for the new gid ... */
1877 qi->lqi_id.qid_gid = attr->la_gid;
1879 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1880 if (rc == -EDQUOT || rc == -EINPROGRESS)
1885 /* and one less inode for the current gid */
1886 qi->lqi_id.qid_gid = gid;
1888 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1889 if (rc == -EDQUOT || rc == -EINPROGRESS)
1894 /* block accounting */
1895 qi->lqi_is_blk = true;
1897 /* more blocks for the new gid ... */
1898 qi->lqi_id.qid_gid = attr->la_gid;
1899 qi->lqi_space = bspace;
1900 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1901 if (rc == -EDQUOT || rc == -EINPROGRESS)
1906 /* and finally less blocks for the current gid */
1907 qi->lqi_id.qid_gid = gid;
1908 qi->lqi_space = -bspace;
1909 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1910 if (rc == -EDQUOT || rc == -EINPROGRESS)
1919 static int osd_inode_setattr(const struct lu_env *env,
1920 struct inode *inode, const struct lu_attr *attr)
1922 __u64 bits = attr->la_valid;
1924 /* Only allow set size for regular file */
1925 if (!S_ISREG(inode->i_mode))
1926 bits &= ~(LA_SIZE | LA_BLOCKS);
1931 if (bits & LA_ATIME)
1932 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1933 if (bits & LA_CTIME)
1934 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1935 if (bits & LA_MTIME)
1936 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1937 if (bits & LA_SIZE) {
1938 LDISKFS_I(inode)->i_disksize = attr->la_size;
1939 i_size_write(inode, attr->la_size);
1943 /* OSD should not change "i_blocks" which is used by quota.
1944 * "i_blocks" should be changed by ldiskfs only. */
1945 if (bits & LA_BLOCKS)
1946 inode->i_blocks = attr->la_blocks;
1949 inode->i_mode = (inode->i_mode & S_IFMT) |
1950 (attr->la_mode & ~S_IFMT);
1952 i_uid_write(inode, attr->la_uid);
1954 i_gid_write(inode, attr->la_gid);
1955 if (bits & LA_NLINK)
1956 set_nlink(inode, attr->la_nlink);
1958 inode->i_rdev = attr->la_rdev;
1960 if (bits & LA_FLAGS) {
1961 /* always keep S_NOCMTIME */
1962 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1968 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1970 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
1971 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
1975 ll_vfs_dq_init(inode);
1977 if (attr->la_valid & LA_UID)
1978 iattr.ia_valid |= ATTR_UID;
1979 if (attr->la_valid & LA_GID)
1980 iattr.ia_valid |= ATTR_GID;
1981 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
1982 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
1984 rc = ll_vfs_dq_transfer(inode, &iattr);
1986 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1987 "enforcement enabled on the ldiskfs "
1988 "filesystem?\n", inode->i_sb->s_id, rc);
1995 static int osd_attr_set(const struct lu_env *env,
1996 struct dt_object *dt,
1997 const struct lu_attr *attr,
1998 struct thandle *handle,
1999 struct lustre_capa *capa)
2001 struct osd_object *obj = osd_dt_obj(dt);
2002 struct inode *inode;
2005 if (!dt_object_exists(dt))
2008 LASSERT(handle != NULL);
2009 LASSERT(!dt_object_remote(dt));
2010 LASSERT(osd_invariant(obj));
2012 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2015 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2017 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2018 struct osd_thread_info *oti = osd_oti_get(env);
2019 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2020 struct lu_fid *fid1 = &oti->oti_fid;
2021 struct osd_inode_id *id = &oti->oti_id;
2022 struct iam_path_descr *ipd;
2023 struct iam_container *bag;
2024 struct osd_thandle *oh;
2027 fid_cpu_to_be(fid1, fid0);
2028 memset(id, 1, sizeof(*id));
2029 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2030 fid0)->oi_dir.od_container;
2031 ipd = osd_idx_ipd_get(env, bag);
2032 if (unlikely(ipd == NULL))
2035 oh = container_of0(handle, struct osd_thandle, ot_super);
2036 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2037 (const struct iam_rec *)id, ipd);
2038 osd_ipd_put(env, bag, ipd);
2039 return(rc > 0 ? 0 : rc);
2042 inode = obj->oo_inode;
2044 rc = osd_quota_transfer(inode, attr);
2048 spin_lock(&obj->oo_guard);
2049 rc = osd_inode_setattr(env, inode, attr);
2050 spin_unlock(&obj->oo_guard);
2053 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2057 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2058 struct osd_object *obj,
2059 const char *name, const int namelen)
2061 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2064 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2065 umode_t mode, struct dt_allocation_hint *hint,
2069 struct osd_device *osd = osd_obj2dev(obj);
2070 struct osd_thandle *oth;
2071 struct dt_object *parent = NULL;
2072 struct inode *inode;
2074 LINVRNT(osd_invariant(obj));
2075 LASSERT(obj->oo_inode == NULL);
2076 LASSERT(obj->oo_hl_head == NULL);
2078 if (S_ISDIR(mode) && ldiskfs_pdo) {
2079 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2080 if (obj->oo_hl_head == NULL)
2084 oth = container_of(th, struct osd_thandle, ot_super);
2085 LASSERT(oth->ot_handle->h_transaction != NULL);
2087 if (hint && hint->dah_parent)
2088 parent = hint->dah_parent;
2090 inode = ldiskfs_create_inode(oth->ot_handle,
2091 parent ? osd_dt_obj(parent)->oo_inode :
2092 osd_sb(osd)->s_root->d_inode,
2094 if (!IS_ERR(inode)) {
2095 /* Do not update file c/mtime in ldiskfs. */
2096 inode->i_flags |= S_NOCMTIME;
2098 /* For new created object, it must be consistent,
2099 * and it is unnecessary to scrub against it. */
2100 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2102 obj->oo_inode = inode;
2105 if (obj->oo_hl_head != NULL) {
2106 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2107 obj->oo_hl_head = NULL;
2109 result = PTR_ERR(inode);
2111 LINVRNT(osd_invariant(obj));
2119 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2120 struct lu_attr *attr,
2121 struct dt_allocation_hint *hint,
2122 struct dt_object_format *dof,
2126 struct osd_thandle *oth;
2127 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2129 LASSERT(S_ISDIR(attr->la_mode));
2131 oth = container_of(th, struct osd_thandle, ot_super);
2132 LASSERT(oth->ot_handle->h_transaction != NULL);
2133 result = osd_mkfile(info, obj, mode, hint, th);
2138 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2139 struct lu_attr *attr,
2140 struct dt_allocation_hint *hint,
2141 struct dt_object_format *dof,
2145 struct osd_thandle *oth;
2146 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2148 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2150 LASSERT(S_ISREG(attr->la_mode));
2152 oth = container_of(th, struct osd_thandle, ot_super);
2153 LASSERT(oth->ot_handle->h_transaction != NULL);
2155 result = osd_mkfile(info, obj, mode, hint, th);
2157 LASSERT(obj->oo_inode != NULL);
2158 if (feat->dif_flags & DT_IND_VARKEY)
2159 result = iam_lvar_create(obj->oo_inode,
2160 feat->dif_keysize_max,
2162 feat->dif_recsize_max,
2165 result = iam_lfix_create(obj->oo_inode,
2166 feat->dif_keysize_max,
2168 feat->dif_recsize_max,
2175 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2176 struct lu_attr *attr,
2177 struct dt_allocation_hint *hint,
2178 struct dt_object_format *dof,
2181 LASSERT(S_ISREG(attr->la_mode));
2182 return osd_mkfile(info, obj, (attr->la_mode &
2183 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2186 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2187 struct lu_attr *attr,
2188 struct dt_allocation_hint *hint,
2189 struct dt_object_format *dof,
2192 LASSERT(S_ISLNK(attr->la_mode));
2193 return osd_mkfile(info, obj, (attr->la_mode &
2194 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2197 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2198 struct lu_attr *attr,
2199 struct dt_allocation_hint *hint,
2200 struct dt_object_format *dof,
2203 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2206 LINVRNT(osd_invariant(obj));
2207 LASSERT(obj->oo_inode == NULL);
2208 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2209 S_ISFIFO(mode) || S_ISSOCK(mode));
2211 result = osd_mkfile(info, obj, mode, hint, th);
2213 LASSERT(obj->oo_inode != NULL);
2215 * This inode should be marked dirty for i_rdev. Currently
2216 * that is done in the osd_attr_init().
2218 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2221 LINVRNT(osd_invariant(obj));
2225 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2227 struct dt_allocation_hint *hint,
2228 struct dt_object_format *dof,
2231 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2233 osd_obj_type_f result;
2249 result = osd_mk_index;
2260 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2261 struct dt_object *parent, struct dt_object *child,
2266 ah->dah_parent = parent;
2267 ah->dah_mode = child_mode;
2270 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2271 struct lu_attr *attr, struct dt_object_format *dof)
2273 struct inode *inode = obj->oo_inode;
2274 __u64 valid = attr->la_valid;
2277 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2279 if (dof->dof_type != DFT_NODE)
2280 attr->la_valid &= ~LA_RDEV;
2281 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2282 attr->la_valid &= ~LA_ATIME;
2283 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2284 attr->la_valid &= ~LA_CTIME;
2285 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2286 attr->la_valid &= ~LA_MTIME;
2288 result = osd_quota_transfer(inode, attr);
2292 if (attr->la_valid != 0) {
2293 result = osd_inode_setattr(info->oti_env, inode, attr);
2295 * The osd_inode_setattr() should always succeed here. The
2296 * only error that could be returned is EDQUOT when we are
2297 * trying to change the UID or GID of the inode. However, this
2298 * should not happen since quota enforcement is no longer
2299 * enabled on ldiskfs (lquota takes care of it).
2301 LASSERTF(result == 0, "%d\n", result);
2302 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2305 attr->la_valid = valid;
2309 * Helper function for osd_object_create()
2311 * \retval 0, on success
2313 static int __osd_object_create(struct osd_thread_info *info,
2314 struct osd_object *obj, struct lu_attr *attr,
2315 struct dt_allocation_hint *hint,
2316 struct dt_object_format *dof,
2322 /* we drop umask so that permissions we pass are not affected */
2323 umask = current->fs->umask;
2324 current->fs->umask = 0;
2326 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2329 osd_attr_init(info, obj, attr, dof);
2330 osd_object_init0(obj);
2333 if (obj->oo_inode != NULL) {
2334 LASSERT(obj->oo_inode->i_state & I_NEW);
2336 unlock_new_inode(obj->oo_inode);
2339 /* restore previous umask value */
2340 current->fs->umask = umask;
2346 * Helper function for osd_object_create()
2348 * \retval 0, on success
2350 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2351 const struct lu_fid *fid, struct thandle *th)
2353 struct osd_thread_info *info = osd_oti_get(env);
2354 struct osd_inode_id *id = &info->oti_id;
2355 struct osd_device *osd = osd_obj2dev(obj);
2356 struct osd_thandle *oh;
2358 LASSERT(obj->oo_inode != NULL);
2360 oh = container_of0(th, struct osd_thandle, ot_super);
2361 LASSERT(oh->ot_handle);
2363 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2364 return osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2367 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2368 u64 seq, struct lu_seq_range *range)
2370 struct seq_server_site *ss = osd_seq_site(osd);
2372 if (fid_seq_is_idif(seq)) {
2373 fld_range_set_ost(range);
2374 range->lsr_index = idif_ost_idx(seq);
2378 if (!fid_seq_in_fldb(seq)) {
2379 fld_range_set_mdt(range);
2381 /* FIXME: If ss is NULL, it suppose not get lsr_index
2383 range->lsr_index = ss->ss_node_id;
2387 LASSERT(ss != NULL);
2388 fld_range_set_any(range);
2389 /* OSD will only do local fld lookup */
2390 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2394 * Concurrency: no external locking is necessary.
2396 static int osd_declare_object_create(const struct lu_env *env,
2397 struct dt_object *dt,
2398 struct lu_attr *attr,
2399 struct dt_allocation_hint *hint,
2400 struct dt_object_format *dof,
2401 struct thandle *handle)
2403 struct osd_thandle *oh;
2407 LASSERT(handle != NULL);
2409 oh = container_of0(handle, struct osd_thandle, ot_super);
2410 LASSERT(oh->ot_handle == NULL);
2412 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2413 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2414 /* Reuse idle OI block may cause additional one OI block
2416 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2417 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2419 /* If this is directory, then we expect . and .. to be inserted as
2420 * well. The one directory block always needs to be created for the
2421 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2422 * block), there is no danger of needing a tree for the first block.
2424 if (attr && S_ISDIR(attr->la_mode)) {
2425 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2426 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2427 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2433 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2434 osd_dt_obj(dt), false, NULL, false);
2441 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2442 struct lu_attr *attr,
2443 struct dt_allocation_hint *hint,
2444 struct dt_object_format *dof, struct thandle *th)
2446 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2447 struct osd_object *obj = osd_dt_obj(dt);
2448 struct osd_thread_info *info = osd_oti_get(env);
2452 if (dt_object_exists(dt))
2455 LINVRNT(osd_invariant(obj));
2456 LASSERT(!dt_object_remote(dt));
2457 LASSERT(osd_write_locked(env, obj));
2458 LASSERT(th != NULL);
2460 if (unlikely(fid_is_acct(fid)))
2461 /* Quota files can't be created from the kernel any more,
2462 * 'tune2fs -O quota' will take care of creating them */
2465 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2466 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2468 result = __osd_object_create(info, obj, attr, hint, dof, th);
2470 result = __osd_oi_insert(env, obj, fid, th);
2472 LASSERT(ergo(result == 0,
2473 dt_object_exists(dt) && !dt_object_remote(dt)));
2475 LASSERT(osd_invariant(obj));
2480 * Called to destroy on-disk representation of the object
2482 * Concurrency: must be locked
2484 static int osd_declare_object_destroy(const struct lu_env *env,
2485 struct dt_object *dt,
2488 struct osd_object *obj = osd_dt_obj(dt);
2489 struct inode *inode = obj->oo_inode;
2490 struct osd_thandle *oh;
2494 oh = container_of0(th, struct osd_thandle, ot_super);
2495 LASSERT(oh->ot_handle == NULL);
2498 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2499 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2500 /* Recycle idle OI leaf may cause additional three OI blocks
2502 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2503 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2504 /* one less inode */
2505 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2506 -1, oh, obj, false, NULL, false);
2509 /* data to be truncated */
2510 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2511 0, oh, obj, true, NULL, false);
2515 static int osd_object_destroy(const struct lu_env *env,
2516 struct dt_object *dt,
2519 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2520 struct osd_object *obj = osd_dt_obj(dt);
2521 struct inode *inode = obj->oo_inode;
2522 struct osd_device *osd = osd_obj2dev(obj);
2523 struct osd_thandle *oh;
2527 oh = container_of0(th, struct osd_thandle, ot_super);
2528 LASSERT(oh->ot_handle);
2530 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2532 if (unlikely(fid_is_acct(fid)))
2535 if (S_ISDIR(inode->i_mode)) {
2536 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2537 inode->i_nlink == 2);
2538 /* it will check/delete the inode from remote parent,
2539 * how to optimize it? unlink performance impaction XXX */
2540 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2541 if (result != 0 && result != -ENOENT) {
2542 CERROR("%s: delete inode "DFID": rc = %d\n",
2543 osd_name(osd), PFID(fid), result);
2545 spin_lock(&obj->oo_guard);
2547 spin_unlock(&obj->oo_guard);
2548 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2551 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2553 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2556 /* XXX: add to ext3 orphan list */
2557 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2559 /* not needed in the cache anymore */
2560 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2566 * Put the fid into lustre_mdt_attrs, and then place the structure
2567 * inode's ea. This fid should not be altered during the life time
2570 * \retval +ve, on success
2571 * \retval -ve, on error
2573 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2575 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2576 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2578 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2582 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2585 lustre_lma_init(lma, fid, compat, incompat);
2586 lustre_lma_swab(lma);
2588 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2590 /* LMA may already exist, but we need to check that all the
2591 * desired compat/incompat flags have been added. */
2592 if (unlikely(rc == -EEXIST)) {
2593 if (compat == 0 && incompat == 0)
2596 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2597 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2602 lustre_lma_swab(lma);
2603 if (!(~lma->lma_compat & compat) &&
2604 !(~lma->lma_incompat & incompat))
2607 lma->lma_compat |= compat;
2608 lma->lma_incompat |= incompat;
2609 lustre_lma_swab(lma);
2610 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2611 sizeof(*lma), XATTR_REPLACE);
2618 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2619 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2620 * To have compatilibility with 1.8 ldiskfs driver we need to have
2621 * magic number at start of fid data.
2622 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2625 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2626 const struct lu_fid *fid)
2628 if (!fid_is_namespace_visible(fid) ||
2629 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2630 param->edp_magic = 0;
2634 param->edp_magic = LDISKFS_LUFID_MAGIC;
2635 param->edp_len = sizeof(struct lu_fid) + 1;
2636 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2640 * Try to read the fid from inode ea into dt_rec.
2642 * \param fid object fid.
2644 * \retval 0 on success
2646 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2647 __u32 ino, struct lu_fid *fid,
2648 struct osd_inode_id *id)
2650 struct osd_thread_info *info = osd_oti_get(env);
2651 struct inode *inode;
2654 osd_id_gen(id, ino, OSD_OII_NOGEN);
2655 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2657 RETURN(PTR_ERR(inode));
2663 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2665 struct inode *parent_dir,
2666 const struct lu_fid *dot_fid,
2667 const struct lu_fid *dot_dot_fid,
2668 struct osd_thandle *oth)
2670 struct ldiskfs_dentry_param *dot_ldp;
2671 struct ldiskfs_dentry_param *dot_dot_ldp;
2673 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2674 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2676 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2677 dot_ldp->edp_magic = 0;
2678 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2679 dir, dot_ldp, dot_dot_ldp);
2683 * Create an local agent inode for remote entry
2685 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2686 struct osd_device *osd,
2687 struct osd_object *pobj,
2688 const struct lu_fid *fid,
2692 struct osd_thread_info *info = osd_oti_get(env);
2693 struct inode *local;
2694 struct osd_thandle *oh;
2699 oh = container_of(th, struct osd_thandle, ot_super);
2700 LASSERT(oh->ot_handle->h_transaction != NULL);
2702 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2703 if (IS_ERR(local)) {
2704 CERROR("%s: create local error %d\n", osd_name(osd),
2705 (int)PTR_ERR(local));
2709 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2710 unlock_new_inode(local);
2712 /* Set special LMA flag for local agent inode */
2713 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2715 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2716 osd_name(osd), PFID(fid), rc);
2717 RETURN(ERR_PTR(rc));
2723 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2724 lu_object_fid(&pobj->oo_dt.do_lu),
2727 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2728 osd_name(osd), PFID(fid), rc);
2729 RETURN(ERR_PTR(rc));
2736 * Delete local agent inode for remote entry
2738 static int osd_delete_local_agent_inode(const struct lu_env *env,
2739 struct osd_device *osd,
2740 const struct lu_fid *fid,
2741 __u32 ino, struct osd_thandle *oh)
2743 struct osd_thread_info *oti = osd_oti_get(env);
2744 struct osd_inode_id *id = &oti->oti_id;
2745 struct inode *inode;
2748 id->oii_ino = le32_to_cpu(ino);
2749 id->oii_gen = OSD_OII_NOGEN;
2750 inode = osd_iget(oti, osd, id);
2751 if (IS_ERR(inode)) {
2752 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2753 PFID(fid), id->oii_ino, id->oii_gen);
2754 RETURN(PTR_ERR(inode));
2758 mark_inode_dirty(inode);
2759 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2760 osd_name(osd), PFID(fid), inode->i_ino);
2766 * OSD layer object create function for interoperability mode (b11826).
2767 * This is mostly similar to osd_object_create(). Only difference being, fid is
2768 * inserted into inode ea here.
2770 * \retval 0, on success
2771 * \retval -ve, on error
2773 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2774 struct lu_attr *attr,
2775 struct dt_allocation_hint *hint,
2776 struct dt_object_format *dof,
2779 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2780 struct osd_object *obj = osd_dt_obj(dt);
2781 struct osd_thread_info *info = osd_oti_get(env);
2786 if (dt_object_exists(dt))
2789 LASSERT(osd_invariant(obj));
2790 LASSERT(!dt_object_remote(dt));
2791 LASSERT(osd_write_locked(env, obj));
2792 LASSERT(th != NULL);
2794 if (unlikely(fid_is_acct(fid)))
2795 /* Quota files can't be created from the kernel any more,
2796 * 'tune2fs -O quota' will take care of creating them */
2799 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2800 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2802 result = __osd_object_create(info, obj, attr, hint, dof, th);
2804 if (fid_is_idif(fid) &&
2805 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
2806 struct lu_fid *tfid = &info->oti_fid;
2807 struct ost_id *oi = &info->oti_ostid;
2809 fid_to_ostid(fid, oi);
2810 ostid_to_fid(tfid, oi, 0);
2811 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
2812 LMAC_FID_ON_OST, 0);
2814 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2815 fid_is_on_ost(info, osd_obj2dev(obj),
2816 fid, OI_CHECK_FLD) ?
2817 LMAC_FID_ON_OST : 0, 0);
2822 result = __osd_oi_insert(env, obj, fid, th);
2824 LASSERT(ergo(result == 0,
2825 dt_object_exists(dt) && !dt_object_remote(dt)));
2826 LINVRNT(osd_invariant(obj));
2830 static int osd_declare_object_ref_add(const struct lu_env *env,
2831 struct dt_object *dt,
2832 struct thandle *handle)
2834 struct osd_thandle *oh;
2836 /* it's possible that object doesn't exist yet */
2837 LASSERT(handle != NULL);
2839 oh = container_of0(handle, struct osd_thandle, ot_super);
2840 LASSERT(oh->ot_handle == NULL);
2842 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2843 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2849 * Concurrency: @dt is write locked.
2851 static int osd_object_ref_add(const struct lu_env *env,
2852 struct dt_object *dt, struct thandle *th)
2854 struct osd_object *obj = osd_dt_obj(dt);
2855 struct inode *inode = obj->oo_inode;
2856 struct osd_thandle *oh;
2859 if (!dt_object_exists(dt))
2862 LINVRNT(osd_invariant(obj));
2863 LASSERT(!dt_object_remote(dt));
2864 LASSERT(osd_write_locked(env, obj));
2865 LASSERT(th != NULL);
2867 oh = container_of0(th, struct osd_thandle, ot_super);
2868 LASSERT(oh->ot_handle != NULL);
2870 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2872 CDEBUG(D_INODE, DFID" increase nlink %d\n",
2873 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
2875 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2876 * (65000) subdirectories by storing "1" in i_nlink if the link count
2877 * would otherwise overflow. Directory tranversal tools understand
2878 * that (st_nlink == 1) indicates that the filesystem dose not track
2879 * hard links count on the directory, and will not abort subdirectory
2880 * scanning early once (st_nlink - 2) subdirs have been found.
2882 * This also has to properly handle the case of inodes with nlink == 0
2883 * in case they are being linked into the PENDING directory
2885 spin_lock(&obj->oo_guard);
2886 if (unlikely(inode->i_nlink == 0))
2887 /* inc_nlink from 0 may cause WARN_ON */
2888 set_nlink(inode, 1);
2890 ldiskfs_inc_count(oh->ot_handle, inode);
2891 if (!S_ISDIR(inode->i_mode))
2892 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2894 spin_unlock(&obj->oo_guard);
2896 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2897 LINVRNT(osd_invariant(obj));
2902 static int osd_declare_object_ref_del(const struct lu_env *env,
2903 struct dt_object *dt,
2904 struct thandle *handle)
2906 struct osd_thandle *oh;
2908 LASSERT(!dt_object_remote(dt));
2909 LASSERT(handle != NULL);
2911 oh = container_of0(handle, struct osd_thandle, ot_super);
2912 LASSERT(oh->ot_handle == NULL);
2914 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2915 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2921 * Concurrency: @dt is write locked.
2923 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2926 struct osd_object *obj = osd_dt_obj(dt);
2927 struct inode *inode = obj->oo_inode;
2928 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2929 struct osd_thandle *oh;
2931 if (!dt_object_exists(dt))
2934 LINVRNT(osd_invariant(obj));
2935 LASSERT(!dt_object_remote(dt));
2936 LASSERT(osd_write_locked(env, obj));
2937 LASSERT(th != NULL);
2939 oh = container_of0(th, struct osd_thandle, ot_super);
2940 LASSERT(oh->ot_handle != NULL);
2942 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2944 spin_lock(&obj->oo_guard);
2945 /* That can be result of upgrade from old Lustre version and
2946 * applied only to local files. Just skip this ref_del call.
2947 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2948 if (inode->i_nlink == 0) {
2949 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2950 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2951 ", maybe an upgraded file? (LU-3915)\n",
2952 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2953 spin_unlock(&obj->oo_guard);
2957 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
2958 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
2960 ldiskfs_dec_count(oh->ot_handle, inode);
2961 spin_unlock(&obj->oo_guard);
2963 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2964 LINVRNT(osd_invariant(obj));
2970 * Get the 64-bit version for an inode.
2972 static int osd_object_version_get(const struct lu_env *env,
2973 struct dt_object *dt, dt_obj_version_t *ver)
2975 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2977 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2978 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2979 *ver = LDISKFS_I(inode)->i_fs_version;
2984 * Concurrency: @dt is read locked.
2986 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2987 struct lu_buf *buf, const char *name,
2988 struct lustre_capa *capa)
2990 struct osd_object *obj = osd_dt_obj(dt);
2991 struct inode *inode = obj->oo_inode;
2992 struct osd_thread_info *info = osd_oti_get(env);
2993 struct dentry *dentry = &info->oti_obj_dentry;
2995 /* version get is not real XATTR but uses xattr API */
2996 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2997 /* for version we are just using xattr API but change inode
2999 if (buf->lb_len == 0)
3000 return sizeof(dt_obj_version_t);
3002 if (buf->lb_len < sizeof(dt_obj_version_t))
3005 osd_object_version_get(env, dt, buf->lb_buf);
3007 return sizeof(dt_obj_version_t);
3010 if (!dt_object_exists(dt))
3013 LASSERT(!dt_object_remote(dt));
3014 LASSERT(inode->i_op != NULL);
3015 LASSERT(inode->i_op->getxattr != NULL);
3017 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
3020 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3024 static int osd_declare_xattr_set(const struct lu_env *env,
3025 struct dt_object *dt,
3026 const struct lu_buf *buf, const char *name,
3027 int fl, struct thandle *handle)
3029 struct osd_thandle *oh;
3031 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3033 LASSERT(handle != NULL);
3035 oh = container_of0(handle, struct osd_thandle, ot_super);
3036 LASSERT(oh->ot_handle == NULL);
3038 /* optimistic optimization: LMA is set first and usually fit inode */
3039 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3040 if (dt_object_exists(dt))
3044 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3047 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3048 if (buf && buf->lb_len > sb->s_blocksize) {
3049 credits *= (buf->lb_len + sb->s_blocksize - 1) >>
3050 sb->s_blocksize_bits;
3053 * xattr set may involve inode quota change, reserve credits for
3054 * dquot_initialize()
3056 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3059 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3065 * Set the 64-bit version for object
3067 static void osd_object_version_set(const struct lu_env *env,
3068 struct dt_object *dt,
3069 dt_obj_version_t *new_version)
3071 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3073 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3074 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3076 LDISKFS_I(inode)->i_fs_version = *new_version;
3077 /** Version is set after all inode operations are finished,
3078 * so we should mark it dirty here */
3079 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3083 * Concurrency: @dt is write locked.
3085 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3086 const struct lu_buf *buf, const char *name, int fl,
3087 struct thandle *handle, struct lustre_capa *capa)
3089 struct osd_object *obj = osd_dt_obj(dt);
3090 struct inode *inode = obj->oo_inode;
3091 struct osd_thread_info *info = osd_oti_get(env);
3095 LASSERT(handle != NULL);
3097 /* version set is not real XATTR */
3098 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3099 /* for version we are just using xattr API but change inode
3101 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3102 osd_object_version_set(env, dt, buf->lb_buf);
3103 return sizeof(dt_obj_version_t);
3106 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
3109 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3110 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3112 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3113 if (fl & LU_XATTR_REPLACE)
3114 fs_flags |= XATTR_REPLACE;
3116 if (fl & LU_XATTR_CREATE)
3117 fs_flags |= XATTR_CREATE;
3119 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3120 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3123 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3127 lma->lma_incompat |= LMAI_STRIPED;
3128 lustre_lma_swab(lma);
3129 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3130 sizeof(*lma), XATTR_REPLACE);
3135 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3136 strcmp(name, XATTR_NAME_LINK) == 0)
3139 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3144 * Concurrency: @dt is read locked.
3146 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3147 const struct lu_buf *buf, struct lustre_capa *capa)
3149 struct osd_object *obj = osd_dt_obj(dt);
3150 struct inode *inode = obj->oo_inode;
3151 struct osd_thread_info *info = osd_oti_get(env);
3152 struct dentry *dentry = &info->oti_obj_dentry;
3154 if (!dt_object_exists(dt))
3157 LASSERT(!dt_object_remote(dt));
3158 LASSERT(inode->i_op != NULL);
3159 LASSERT(inode->i_op->listxattr != NULL);
3161 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
3164 dentry->d_inode = inode;
3165 dentry->d_sb = inode->i_sb;
3166 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3169 static int osd_declare_xattr_del(const struct lu_env *env,
3170 struct dt_object *dt, const char *name,
3171 struct thandle *handle)
3173 struct osd_thandle *oh;
3174 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3176 LASSERT(!dt_object_remote(dt));
3177 LASSERT(handle != NULL);
3179 oh = container_of0(handle, struct osd_thandle, ot_super);
3180 LASSERT(oh->ot_handle == NULL);
3182 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3183 osd_dto_credits_noquota[DTO_XATTR_SET]);
3185 * xattr del may involve inode quota change, reserve credits for
3186 * dquot_initialize()
3188 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3194 * Concurrency: @dt is write locked.
3196 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3197 const char *name, struct thandle *handle,
3198 struct lustre_capa *capa)
3200 struct osd_object *obj = osd_dt_obj(dt);
3201 struct inode *inode = obj->oo_inode;
3202 struct osd_thread_info *info = osd_oti_get(env);
3203 struct dentry *dentry = &info->oti_obj_dentry;
3206 if (!dt_object_exists(dt))
3209 LASSERT(!dt_object_remote(dt));
3210 LASSERT(inode->i_op != NULL);
3211 LASSERT(inode->i_op->removexattr != NULL);
3212 LASSERT(handle != NULL);
3214 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
3217 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3219 ll_vfs_dq_init(inode);
3220 dentry->d_inode = inode;
3221 dentry->d_sb = inode->i_sb;
3222 rc = inode->i_op->removexattr(dentry, name);
3226 static struct obd_capa *osd_capa_get(const struct lu_env *env,
3227 struct dt_object *dt,
3228 struct lustre_capa *old, __u64 opc)
3230 struct osd_thread_info *info = osd_oti_get(env);
3231 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3232 struct osd_object *obj = osd_dt_obj(dt);
3233 struct osd_device *osd = osd_obj2dev(obj);
3234 struct lustre_capa_key *key = &info->oti_capa_key;
3235 struct lustre_capa *capa = &info->oti_capa;
3236 struct obd_capa *oc;
3237 struct lu_capainfo *lci;
3241 if (!osd->od_fl_capa)
3242 RETURN(ERR_PTR(-ENOENT));
3244 if (!dt_object_exists(dt))
3245 RETURN(ERR_PTR(-ENOENT));
3247 LASSERT(!dt_object_remote(dt));
3248 LINVRNT(osd_invariant(obj));
3250 /* renewal sanity check */
3251 if (old && osd_object_auth(env, dt, old, opc))
3252 RETURN(ERR_PTR(-EACCES));
3254 lci = lu_capainfo_get(env);
3255 if (unlikely(lci == NULL))
3256 RETURN(ERR_PTR(-ENOENT));
3258 switch (lci->lci_auth) {
3262 capa->lc_uid = i_uid_read(obj->oo_inode);
3263 capa->lc_gid = i_gid_read(obj->oo_inode);
3264 capa->lc_flags = LC_ID_PLAIN;
3266 case LC_ID_CONVERT: {
3269 s[0] = i_uid_read(obj->oo_inode);
3270 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
3271 s[2] = i_uid_read(obj->oo_inode);
3272 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
3273 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
3275 RETURN(ERR_PTR(rc));
3277 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
3278 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
3279 capa->lc_flags = LC_ID_CONVERT;
3283 RETURN(ERR_PTR(-EINVAL));
3286 capa->lc_fid = *fid;
3288 capa->lc_flags |= osd->od_capa_alg << 24;
3289 capa->lc_timeout = osd->od_capa_timeout;
3290 capa->lc_expiry = 0;
3292 oc = capa_lookup(osd->od_capa_hash, capa, 1);
3294 LASSERT(!capa_is_expired(oc));
3298 spin_lock(&capa_lock);
3299 *key = osd->od_capa_keys[1];
3300 spin_unlock(&capa_lock);
3302 capa->lc_keyid = key->lk_keyid;
3303 capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
3305 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
3307 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
3308 RETURN(ERR_PTR(rc));
3311 oc = capa_add(osd->od_capa_hash, capa);
3315 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3316 __u64 start, __u64 end)
3318 struct osd_object *obj = osd_dt_obj(dt);
3319 struct inode *inode = obj->oo_inode;
3320 struct osd_thread_info *info = osd_oti_get(env);
3321 struct dentry *dentry = &info->oti_obj_dentry;
3322 struct file *file = &info->oti_file;
3327 dentry->d_inode = inode;
3328 dentry->d_sb = inode->i_sb;
3329 file->f_dentry = dentry;
3330 file->f_mapping = inode->i_mapping;
3331 file->f_op = inode->i_fop;
3332 set_file_inode(file, inode);
3334 rc = ll_vfs_fsync_range(file, start, end, 0);
3339 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
3342 struct osd_object *obj = osd_dt_obj(dt);
3345 *data = (void *)obj->oo_inode;
3353 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3354 const struct dt_index_features *feat)
3356 struct iam_descr *descr;
3358 if (osd_object_is_root(o))
3359 return feat == &dt_directory_features;
3361 LASSERT(o->oo_dir != NULL);
3363 descr = o->oo_dir->od_container.ic_descr;
3364 if (feat == &dt_directory_features) {
3365 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3371 feat->dif_keysize_min <= descr->id_key_size &&
3372 descr->id_key_size <= feat->dif_keysize_max &&
3373 feat->dif_recsize_min <= descr->id_rec_size &&
3374 descr->id_rec_size <= feat->dif_recsize_max &&
3375 !(feat->dif_flags & (DT_IND_VARKEY |
3376 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3377 ergo(feat->dif_flags & DT_IND_UPDATE,
3378 1 /* XXX check that object (and file system) is
3383 static int osd_iam_container_init(const struct lu_env *env,
3384 struct osd_object *obj,
3385 struct osd_directory *dir)
3387 struct iam_container *bag = &dir->od_container;
3390 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3394 result = iam_container_setup(bag);
3396 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3398 iam_container_fini(bag);
3405 * Concurrency: no external locking is necessary.
3407 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3408 const struct dt_index_features *feat)
3412 struct osd_object *obj = osd_dt_obj(dt);
3414 LINVRNT(osd_invariant(obj));
3416 if (osd_object_is_root(obj)) {
3417 dt->do_index_ops = &osd_index_ea_ops;
3419 } else if (feat == &dt_directory_features) {
3420 dt->do_index_ops = &osd_index_ea_ops;
3421 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3426 } else if (unlikely(feat == &dt_otable_features)) {
3427 dt->do_index_ops = &osd_otable_ops;
3429 } else if (unlikely(feat == &dt_acct_features)) {
3430 dt->do_index_ops = &osd_acct_index_ops;
3433 } else if (!osd_has_index(obj)) {
3434 struct osd_directory *dir;
3439 spin_lock(&obj->oo_guard);
3440 if (obj->oo_dir == NULL)
3444 * Concurrent thread allocated container data.
3447 spin_unlock(&obj->oo_guard);
3449 * Now, that we have container data, serialize its
3452 down_write(&obj->oo_ext_idx_sem);
3454 * recheck under lock.
3456 if (!osd_has_index(obj))
3457 result = osd_iam_container_init(env, obj,
3461 up_write(&obj->oo_ext_idx_sem);
3469 if (result == 0 && skip_iam == 0) {
3470 if (!osd_iam_index_probe(env, obj, feat))
3473 LINVRNT(osd_invariant(obj));
3475 if (result == 0 && is_quota_glb_feat(feat) &&
3476 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3477 result = osd_quota_migration(env, dt, feat);
3482 static int osd_otable_it_attr_get(const struct lu_env *env,
3483 struct dt_object *dt,
3484 struct lu_attr *attr,
3485 struct lustre_capa *capa)
3491 static const struct dt_object_operations osd_obj_ops = {
3492 .do_read_lock = osd_object_read_lock,
3493 .do_write_lock = osd_object_write_lock,
3494 .do_read_unlock = osd_object_read_unlock,
3495 .do_write_unlock = osd_object_write_unlock,
3496 .do_write_locked = osd_object_write_locked,
3497 .do_attr_get = osd_attr_get,
3498 .do_declare_attr_set = osd_declare_attr_set,
3499 .do_attr_set = osd_attr_set,
3500 .do_ah_init = osd_ah_init,
3501 .do_declare_create = osd_declare_object_create,
3502 .do_create = osd_object_create,
3503 .do_declare_destroy = osd_declare_object_destroy,
3504 .do_destroy = osd_object_destroy,
3505 .do_index_try = osd_index_try,
3506 .do_declare_ref_add = osd_declare_object_ref_add,
3507 .do_ref_add = osd_object_ref_add,
3508 .do_declare_ref_del = osd_declare_object_ref_del,
3509 .do_ref_del = osd_object_ref_del,
3510 .do_xattr_get = osd_xattr_get,
3511 .do_declare_xattr_set = osd_declare_xattr_set,
3512 .do_xattr_set = osd_xattr_set,
3513 .do_declare_xattr_del = osd_declare_xattr_del,
3514 .do_xattr_del = osd_xattr_del,
3515 .do_xattr_list = osd_xattr_list,
3516 .do_capa_get = osd_capa_get,
3517 .do_object_sync = osd_object_sync,
3518 .do_data_get = osd_data_get,
3522 * dt_object_operations for interoperability mode
3523 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3525 static const struct dt_object_operations osd_obj_ea_ops = {
3526 .do_read_lock = osd_object_read_lock,
3527 .do_write_lock = osd_object_write_lock,
3528 .do_read_unlock = osd_object_read_unlock,
3529 .do_write_unlock = osd_object_write_unlock,
3530 .do_write_locked = osd_object_write_locked,
3531 .do_attr_get = osd_attr_get,
3532 .do_declare_attr_set = osd_declare_attr_set,
3533 .do_attr_set = osd_attr_set,
3534 .do_ah_init = osd_ah_init,
3535 .do_declare_create = osd_declare_object_create,
3536 .do_create = osd_object_ea_create,
3537 .do_declare_destroy = osd_declare_object_destroy,
3538 .do_destroy = osd_object_destroy,
3539 .do_index_try = osd_index_try,
3540 .do_declare_ref_add = osd_declare_object_ref_add,
3541 .do_ref_add = osd_object_ref_add,
3542 .do_declare_ref_del = osd_declare_object_ref_del,
3543 .do_ref_del = osd_object_ref_del,
3544 .do_xattr_get = osd_xattr_get,
3545 .do_declare_xattr_set = osd_declare_xattr_set,
3546 .do_xattr_set = osd_xattr_set,
3547 .do_declare_xattr_del = osd_declare_xattr_del,
3548 .do_xattr_del = osd_xattr_del,
3549 .do_xattr_list = osd_xattr_list,
3550 .do_capa_get = osd_capa_get,
3551 .do_object_sync = osd_object_sync,
3552 .do_data_get = osd_data_get,
3555 static const struct dt_object_operations osd_obj_otable_it_ops = {
3556 .do_attr_get = osd_otable_it_attr_get,
3557 .do_index_try = osd_index_try,
3560 static int osd_index_declare_iam_delete(const struct lu_env *env,
3561 struct dt_object *dt,
3562 const struct dt_key *key,
3563 struct thandle *handle)
3565 struct osd_thandle *oh;
3567 oh = container_of0(handle, struct osd_thandle, ot_super);
3568 LASSERT(oh->ot_handle == NULL);
3570 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3571 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3577 * delete a (key, value) pair from index \a dt specified by \a key
3579 * \param dt osd index object
3580 * \param key key for index
3581 * \param rec record reference
3582 * \param handle transaction handler
3585 * \retval -ve failure
3587 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3588 const struct dt_key *key,
3589 struct thandle *handle,
3590 struct lustre_capa *capa)
3592 struct osd_thread_info *oti = osd_oti_get(env);
3593 struct osd_object *obj = osd_dt_obj(dt);
3594 struct osd_thandle *oh;
3595 struct iam_path_descr *ipd;
3596 struct iam_container *bag = &obj->oo_dir->od_container;
3600 if (!dt_object_exists(dt))
3603 LINVRNT(osd_invariant(obj));
3604 LASSERT(!dt_object_remote(dt));
3605 LASSERT(bag->ic_object == obj->oo_inode);
3606 LASSERT(handle != NULL);
3608 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3611 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3613 ipd = osd_idx_ipd_get(env, bag);
3614 if (unlikely(ipd == NULL))
3617 oh = container_of0(handle, struct osd_thandle, ot_super);
3618 LASSERT(oh->ot_handle != NULL);
3619 LASSERT(oh->ot_handle->h_transaction != NULL);
3621 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3622 /* swab quota uid/gid provided by caller */
3623 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3624 key = (const struct dt_key *)&oti->oti_quota_id;
3627 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3628 osd_ipd_put(env, bag, ipd);
3629 LINVRNT(osd_invariant(obj));
3633 static int osd_index_declare_ea_delete(const struct lu_env *env,
3634 struct dt_object *dt,
3635 const struct dt_key *key,
3636 struct thandle *handle)
3638 struct osd_thandle *oh;
3639 struct inode *inode;
3643 LASSERT(!dt_object_remote(dt));
3644 LASSERT(handle != NULL);
3646 oh = container_of0(handle, struct osd_thandle, ot_super);
3647 LASSERT(oh->ot_handle == NULL);
3649 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3650 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3652 inode = osd_dt_obj(dt)->oo_inode;
3655 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3656 0, oh, osd_dt_obj(dt), true, NULL, false);
3660 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3663 struct osd_fid_pack *rec;
3666 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3667 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3668 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3673 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3674 const struct lu_fid *fid)
3676 struct seq_server_site *ss = osd_seq_site(osd);
3679 /* FID seqs not in FLDB, must be local seq */
3680 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3683 /* If FLD is not being initialized yet, it only happens during the
3684 * initialization, likely during mgs initialization, and we assume
3685 * this is local FID. */
3686 if (ss == NULL || ss->ss_server_fld == NULL)
3689 /* Only check the local FLDB here */
3690 if (osd_seq_exists(env, osd, fid_seq(fid)))
3697 * Index delete function for interoperability mode (b11826).
3698 * It will remove the directory entry added by osd_index_ea_insert().
3699 * This entry is needed to maintain name->fid mapping.
3701 * \param key, key i.e. file entry to be deleted
3703 * \retval 0, on success
3704 * \retval -ve, on error
3706 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3707 const struct dt_key *key, struct thandle *handle,
3708 struct lustre_capa *capa)
3710 struct osd_object *obj = osd_dt_obj(dt);
3711 struct inode *dir = obj->oo_inode;
3712 struct dentry *dentry;
3713 struct osd_thandle *oh;
3714 struct ldiskfs_dir_entry_2 *de = NULL;
3715 struct buffer_head *bh;
3716 struct htree_lock *hlock = NULL;
3717 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3718 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3722 if (!dt_object_exists(dt))
3725 LINVRNT(osd_invariant(obj));
3726 LASSERT(!dt_object_remote(dt));
3727 LASSERT(handle != NULL);
3729 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3731 oh = container_of(handle, struct osd_thandle, ot_super);
3732 LASSERT(oh->ot_handle != NULL);
3733 LASSERT(oh->ot_handle->h_transaction != NULL);
3735 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3738 ll_vfs_dq_init(dir);
3739 dentry = osd_child_dentry_get(env, obj,
3740 (char *)key, strlen((char *)key));
3742 if (obj->oo_hl_head != NULL) {
3743 hlock = osd_oti_get(env)->oti_hlock;
3744 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3745 dir, LDISKFS_HLOCK_DEL);
3747 down_write(&obj->oo_ext_idx_sem);
3750 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3754 /* If this is not the ".." entry, it might be a remote DNE
3755 * entry and we need to check if the FID is for a remote
3756 * MDT. If the FID is not in the directory entry (e.g.
3757 * upgraded 1.8 filesystem without dirdata enabled) then
3758 * we need to get the FID from the LMA. For a remote directory
3759 * there HAS to be an LMA, it cannot be an IGIF inode in this
3762 * Delete the entry before the agent inode in order to
3763 * simplify error handling. At worst an error after deleting
3764 * the entry first might leak the agent inode afterward. The
3765 * reverse would need filesystem abort in case of error deleting
3766 * the entry after the agent had been removed, or leave a
3767 * dangling entry pointing at a random inode. */
3768 if (strcmp((char *)key, dotdot) != 0) {
3769 LASSERT(de != NULL);
3770 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3771 /* If Fid is not in dentry, try to get it from LMA */
3772 if (rc == -ENODATA) {
3773 struct osd_inode_id *id;
3774 struct inode *inode;
3776 /* Before trying to get fid from the inode,
3777 * check whether the inode is valid.
3779 * If the inode has been deleted, do not go
3780 * ahead to do osd_ea_fid_get, which will set
3781 * the inode to bad inode, which might cause
3782 * the inode to be deleted uncorrectly */
3783 inode = ldiskfs_iget(osd_sb(osd),
3784 le32_to_cpu(de->inode));
3785 if (IS_ERR(inode)) {
3786 CDEBUG(D_INODE, "%s: "DFID"get inode"
3787 "error.\n", osd_name(osd),
3789 rc = PTR_ERR(inode);
3791 if (likely(inode->i_nlink != 0)) {
3792 id = &osd_oti_get(env)->oti_id;
3793 rc = osd_ea_fid_get(env, obj,
3794 le32_to_cpu(de->inode),
3797 CDEBUG(D_INFO, "%s: %u "DFID
3800 le32_to_cpu(de->inode),
3808 unlikely(osd_remote_fid(env, osd, fid)))
3809 /* Need to delete agent inode */
3810 ino = le32_to_cpu(de->inode);
3812 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3814 if (rc == 0 && unlikely(ino != 0)) {
3815 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3818 CERROR("%s: del local inode "DFID": rc = %d\n",
3819 osd_name(osd), PFID(fid), rc);
3825 ldiskfs_htree_unlock(hlock);
3827 up_write(&obj->oo_ext_idx_sem);
3832 /* For inode on the remote MDT, .. will point to
3833 * /Agent directory, Check whether it needs to delete
3834 * from agent directory */
3835 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3836 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3838 if (rc != 0 && rc != -ENOENT) {
3839 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3840 osd_name(osd), PFID(fid), rc);
3850 LASSERT(osd_invariant(obj));
3855 * Lookup index for \a key and copy record to \a rec.
3857 * \param dt osd index object
3858 * \param key key for index
3859 * \param rec record reference
3861 * \retval +ve success : exact mach
3862 * \retval 0 return record with key not greater than \a key
3863 * \retval -ve failure
3865 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3866 struct dt_rec *rec, const struct dt_key *key,
3867 struct lustre_capa *capa)
3869 struct osd_object *obj = osd_dt_obj(dt);
3870 struct iam_path_descr *ipd;
3871 struct iam_container *bag = &obj->oo_dir->od_container;
3872 struct osd_thread_info *oti = osd_oti_get(env);
3873 struct iam_iterator *it = &oti->oti_idx_it;
3874 struct iam_rec *iam_rec;
3878 if (!dt_object_exists(dt))
3881 LASSERT(osd_invariant(obj));
3882 LASSERT(!dt_object_remote(dt));
3883 LASSERT(bag->ic_object == obj->oo_inode);
3885 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3888 ipd = osd_idx_ipd_get(env, bag);
3892 /* got ipd now we can start iterator. */
3893 iam_it_init(it, bag, 0, ipd);
3895 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3896 /* swab quota uid/gid provided by caller */
3897 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3898 key = (const struct dt_key *)&oti->oti_quota_id;
3901 rc = iam_it_get(it, (struct iam_key *)key);
3903 if (S_ISDIR(obj->oo_inode->i_mode))
3904 iam_rec = (struct iam_rec *)oti->oti_ldp;
3906 iam_rec = (struct iam_rec *) rec;
3908 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3910 if (S_ISDIR(obj->oo_inode->i_mode))
3911 osd_fid_unpack((struct lu_fid *) rec,
3912 (struct osd_fid_pack *)iam_rec);
3913 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3914 osd_quota_unpack(obj, rec);
3919 osd_ipd_put(env, bag, ipd);
3921 LINVRNT(osd_invariant(obj));
3926 static int osd_index_declare_iam_insert(const struct lu_env *env,
3927 struct dt_object *dt,
3928 const struct dt_rec *rec,
3929 const struct dt_key *key,
3930 struct thandle *handle)
3932 struct osd_thandle *oh;
3934 LASSERT(handle != NULL);
3936 oh = container_of0(handle, struct osd_thandle, ot_super);
3937 LASSERT(oh->ot_handle == NULL);
3939 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3940 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3946 * Inserts (key, value) pair in \a dt index object.
3948 * \param dt osd index object
3949 * \param key key for index
3950 * \param rec record reference
3951 * \param th transaction handler
3954 * \retval -ve failure
3956 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3957 const struct dt_rec *rec,
3958 const struct dt_key *key, struct thandle *th,
3959 struct lustre_capa *capa, int ignore_quota)
3961 struct osd_object *obj = osd_dt_obj(dt);
3962 struct iam_path_descr *ipd;
3963 struct osd_thandle *oh;
3964 struct iam_container *bag;
3965 struct osd_thread_info *oti = osd_oti_get(env);
3966 struct iam_rec *iam_rec;
3970 if (!dt_object_exists(dt))
3973 LINVRNT(osd_invariant(obj));
3974 LASSERT(!dt_object_remote(dt));
3976 bag = &obj->oo_dir->od_container;
3977 LASSERT(bag->ic_object == obj->oo_inode);
3978 LASSERT(th != NULL);
3980 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3983 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3985 ipd = osd_idx_ipd_get(env, bag);
3986 if (unlikely(ipd == NULL))
3989 oh = container_of0(th, struct osd_thandle, ot_super);
3990 LASSERT(oh->ot_handle != NULL);
3991 LASSERT(oh->ot_handle->h_transaction != NULL);
3992 if (S_ISDIR(obj->oo_inode->i_mode)) {
3993 iam_rec = (struct iam_rec *)oti->oti_ldp;
3994 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3995 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3996 /* pack quota uid/gid */
3997 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3998 key = (const struct dt_key *)&oti->oti_quota_id;
3999 /* pack quota record */
4000 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4001 iam_rec = (struct iam_rec *)rec;
4003 iam_rec = (struct iam_rec *)rec;
4006 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4008 osd_ipd_put(env, bag, ipd);
4009 LINVRNT(osd_invariant(obj));
4014 * Calls ldiskfs_add_entry() to add directory entry
4015 * into the directory. This is required for
4016 * interoperability mode (b11826)
4018 * \retval 0, on success
4019 * \retval -ve, on error
4021 static int __osd_ea_add_rec(struct osd_thread_info *info,
4022 struct osd_object *pobj, struct inode *cinode,
4023 const char *name, const struct lu_fid *fid,
4024 struct htree_lock *hlock, struct thandle *th)
4026 struct ldiskfs_dentry_param *ldp;
4027 struct dentry *child;
4028 struct osd_thandle *oth;
4031 oth = container_of(th, struct osd_thandle, ot_super);
4032 LASSERT(oth->ot_handle != NULL);
4033 LASSERT(oth->ot_handle->h_transaction != NULL);
4034 LASSERT(pobj->oo_inode);
4036 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4037 if (unlikely(pobj->oo_inode ==
4038 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4041 osd_get_ldiskfs_dirent_param(ldp, fid);
4042 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4043 child->d_fsdata = (void *)ldp;
4044 ll_vfs_dq_init(pobj->oo_inode);
4045 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
4046 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4047 struct ldiskfs_dir_entry_2 *de;
4048 struct buffer_head *bh;
4051 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4054 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4057 if (S_ISDIR(cinode->i_mode))
4058 de->file_type = LDISKFS_DIRENT_LUFID |
4059 LDISKFS_FT_REG_FILE;
4061 de->file_type = LDISKFS_DIRENT_LUFID |
4063 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4074 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4075 * into the directory.Also sets flags into osd object to
4076 * indicate dot and dotdot are created. This is required for
4077 * interoperability mode (b11826)
4079 * \param dir directory for dot and dotdot fixup.
4080 * \param obj child object for linking
4082 * \retval 0, on success
4083 * \retval -ve, on error
4085 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4086 struct osd_object *dir,
4087 struct inode *parent_dir, const char *name,
4088 const struct lu_fid *dot_fid,
4089 const struct lu_fid *dot_dot_fid,
4092 struct inode *inode = dir->oo_inode;
4093 struct osd_thandle *oth;
4096 oth = container_of(th, struct osd_thandle, ot_super);
4097 LASSERT(oth->ot_handle->h_transaction != NULL);
4098 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4100 if (strcmp(name, dot) == 0) {
4101 if (dir->oo_compat_dot_created) {
4104 LASSERT(inode == parent_dir);
4105 dir->oo_compat_dot_created = 1;
4108 } else if (strcmp(name, dotdot) == 0) {
4109 if (!dir->oo_compat_dot_created)
4111 /* in case of rename, dotdot is already created */
4112 if (dir->oo_compat_dotdot_created) {
4113 return __osd_ea_add_rec(info, dir, parent_dir, name,
4114 dot_dot_fid, NULL, th);
4117 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4118 struct lu_fid tfid = *dot_dot_fid;
4121 result = osd_add_dot_dotdot_internal(info,
4122 dir->oo_inode, parent_dir, dot_fid,
4125 result = osd_add_dot_dotdot_internal(info,
4126 dir->oo_inode, parent_dir, dot_fid,
4131 dir->oo_compat_dotdot_created = 1;
4139 * It will call the appropriate osd_add* function and return the
4140 * value, return by respective functions.
4142 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4143 struct inode *cinode, const char *name,
4144 const struct lu_fid *fid, struct thandle *th)
4146 struct osd_thread_info *info = osd_oti_get(env);
4147 struct htree_lock *hlock;
4150 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4152 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4154 if (hlock != NULL) {
4155 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4158 down_write(&pobj->oo_ext_idx_sem);
4161 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4162 lu_object_fid(&pobj->oo_dt.do_lu),
4165 if (hlock != NULL) {
4166 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4167 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4169 down_write(&pobj->oo_ext_idx_sem);
4172 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4173 struct lu_fid *tfid = &info->oti_fid;
4177 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4180 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4185 ldiskfs_htree_unlock(hlock);
4187 up_write(&pobj->oo_ext_idx_sem);
4193 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4194 struct osd_idmap_cache *oic)
4196 struct osd_scrub *scrub = &dev->od_scrub;
4197 struct lu_fid *fid = &oic->oic_fid;
4198 struct osd_inode_id *id = &oti->oti_id;
4203 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4206 if (scrub->os_pos_current > id->oii_ino)
4210 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
4211 if (rc != 0 && rc != -ENOENT)
4214 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
4217 if (thread_is_running(&scrub->os_thread)) {
4218 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4219 /* There is race condition between osd_oi_lookup and OI scrub.
4220 * The OI scrub finished just after osd_oi_lookup() failure.
4221 * Under such case, it is unnecessary to trigger OI scrub again,
4222 * but try to call osd_oi_lookup() again. */
4223 if (unlikely(rc == -EAGAIN))
4229 if (!dev->od_noscrub && ++once == 1) {
4230 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4232 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4233 "for "DFID", rc = %d [2]\n",
4234 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4236 if (rc == 0 || rc == -EALREADY)
4243 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4244 struct osd_device *dev,
4245 struct osd_idmap_cache *oic,
4246 struct lu_fid *fid, __u32 ino)
4248 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4249 struct inode *inode;
4252 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4253 inode = osd_iget(oti, dev, &oic->oic_lid);
4254 if (IS_ERR(inode)) {
4255 fid_zero(&oic->oic_fid);
4256 return PTR_ERR(inode);
4259 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4262 fid_zero(&oic->oic_fid);
4264 *fid = oic->oic_fid = lma->lma_self_fid;
4268 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4269 struct osd_inode_id *id, const struct lu_fid *fid)
4271 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4272 id->oii_ino, id->oii_gen, info);
4273 info->oti_cache.oic_lid = *id;
4274 info->oti_cache.oic_fid = *fid;
4275 info->oti_cache.oic_dev = osd;
4279 * Get parent FID from the linkEA.
4281 * For a directory which parent resides on remote MDT, to satisfy the
4282 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4283 * the other hand, to make the lookup(..) on the directory can return
4284 * the real parent FID, we append the real parent FID after its ".."
4285 * name entry in the /REMOTE_PARENT_DIR.
4287 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4288 * backup. So after the restore, we cannot get the right parent FID from
4289 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4290 * we have stored the real parent FID in the directory object's linkEA,
4291 * we can parse the linkEA for the real parent FID.
4293 * \param[in] env pointer to the thread context
4294 * \param[in] obj pointer to the object to be handled
4295 * \param[out]fid pointer to the buffer to hold the parent FID
4297 * \retval 0 for getting the real parent FID successfully
4298 * \retval negative error number on failure
4300 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4301 struct osd_object *obj,
4304 struct osd_thread_info *oti = osd_oti_get(env);
4305 struct lu_buf *buf = &oti->oti_big_buf;
4306 struct dentry *dentry = &oti->oti_obj_dentry;
4307 struct inode *inode = obj->oo_inode;
4308 struct linkea_data ldata = { NULL };
4313 if (!S_ISDIR(inode->i_mode))
4317 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4318 buf->lb_buf, buf->lb_len);
4319 if (rc == -ERANGE) {
4320 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4323 lu_buf_realloc(buf, rc);
4324 if (buf->lb_buf == NULL)
4331 if (unlikely(rc == 0))
4337 if (unlikely(buf->lb_buf == NULL)) {
4338 lu_buf_realloc(buf, rc);
4339 if (buf->lb_buf == NULL)
4346 rc = linkea_init(&ldata);
4348 linkea_first_entry(&ldata);
4349 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4356 * Calls ->lookup() to find dentry. From dentry get inode and
4357 * read inode's ea to get fid. This is required for interoperability
4360 * \retval 0, on success
4361 * \retval -ve, on error
4363 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4364 struct dt_rec *rec, const struct dt_key *key)
4366 struct inode *dir = obj->oo_inode;
4367 struct dentry *dentry;
4368 struct ldiskfs_dir_entry_2 *de;
4369 struct buffer_head *bh;
4370 struct lu_fid *fid = (struct lu_fid *) rec;
4371 struct htree_lock *hlock = NULL;
4376 LASSERT(dir->i_op != NULL);
4377 LASSERT(dir->i_op->lookup != NULL);
4379 dentry = osd_child_dentry_get(env, obj,
4380 (char *)key, strlen((char *)key));
4382 if (obj->oo_hl_head != NULL) {
4383 hlock = osd_oti_get(env)->oti_hlock;
4384 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4385 dir, LDISKFS_HLOCK_LOOKUP);
4387 down_read(&obj->oo_ext_idx_sem);
4390 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4392 struct osd_thread_info *oti = osd_oti_get(env);
4393 struct osd_inode_id *id = &oti->oti_id;
4394 struct osd_idmap_cache *oic = &oti->oti_cache;
4395 struct osd_device *dev = osd_obj2dev(obj);
4397 ino = le32_to_cpu(de->inode);
4398 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4400 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4404 rc = osd_get_fid_from_dentry(de, rec);
4406 /* done with de, release bh */
4409 if (unlikely(ino == osd_remote_parent_ino(dev)))
4410 /* If the parent is on remote MDT, and there
4411 * is no FID-in-dirent, then we have to get
4412 * the parent FID from the linkEA. */
4413 rc = osd_get_pfid_from_linkea(env, obj, fid);
4415 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4417 osd_id_gen(id, ino, OSD_OII_NOGEN);
4421 fid_zero(&oic->oic_fid);
4425 if (osd_remote_fid(env, dev, fid))
4428 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4429 osd_consistency_check(oti, dev, oic);
4438 ldiskfs_htree_unlock(hlock);
4440 up_read(&obj->oo_ext_idx_sem);
4445 * Find the osd object for given fid.
4447 * \param fid need to find the osd object having this fid
4449 * \retval osd_object on success
4450 * \retval -ve on error
4452 static struct osd_object *osd_object_find(const struct lu_env *env,
4453 struct dt_object *dt,
4454 const struct lu_fid *fid)
4456 struct lu_device *ludev = dt->do_lu.lo_dev;
4457 struct osd_object *child = NULL;
4458 struct lu_object *luch;
4459 struct lu_object *lo;
4462 * at this point topdev might not exist yet
4463 * (i.e. MGS is preparing profiles). so we can
4464 * not rely on topdev and instead lookup with
4465 * our device passed as topdev. this can't work
4466 * if the object isn't cached yet (as osd doesn't
4467 * allocate lu_header). IOW, the object must be
4468 * in the cache, otherwise lu_object_alloc() crashes
4471 luch = lu_object_find_at(env, ludev->ld_site->ls_top_dev == NULL ?
4472 ludev : ludev->ld_site->ls_top_dev,
4474 if (!IS_ERR(luch)) {
4475 if (lu_object_exists(luch)) {
4476 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4478 child = osd_obj(lo);
4480 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4481 "lu_object can't be located"
4482 DFID"\n", PFID(fid));
4484 if (child == NULL) {
4485 lu_object_put(env, luch);
4486 CERROR("Unable to get osd_object\n");
4487 child = ERR_PTR(-ENOENT);
4490 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4491 "lu_object does not exists "DFID"\n",
4493 lu_object_put(env, luch);
4494 child = ERR_PTR(-ENOENT);
4497 child = ERR_CAST(luch);
4504 * Put the osd object once done with it.
4506 * \param obj osd object that needs to be put
4508 static inline void osd_object_put(const struct lu_env *env,
4509 struct osd_object *obj)
4511 lu_object_put(env, &obj->oo_dt.do_lu);
4514 static int osd_index_declare_ea_insert(const struct lu_env *env,
4515 struct dt_object *dt,
4516 const struct dt_rec *rec,
4517 const struct dt_key *key,
4518 struct thandle *handle)
4520 struct osd_thandle *oh;
4521 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4522 struct lu_fid *fid = (struct lu_fid *)rec;
4526 LASSERT(!dt_object_remote(dt));
4527 LASSERT(handle != NULL);
4529 oh = container_of0(handle, struct osd_thandle, ot_super);
4530 LASSERT(oh->ot_handle == NULL);
4532 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4533 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4535 if (osd_dt_obj(dt)->oo_inode != NULL) {
4536 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4538 /* We ignore block quota on meta pool (MDTs), so needn't
4539 * calculate how many blocks will be consumed by this index
4541 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4542 i_gid_read(inode), 0, oh,
4543 osd_dt_obj(dt), true, NULL, false);
4549 rc = osd_remote_fid(env, osd, fid);
4555 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4556 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4557 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4558 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4559 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4560 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4566 * Index add function for interoperability mode (b11826).
4567 * It will add the directory entry.This entry is needed to
4568 * maintain name->fid mapping.
4570 * \param key it is key i.e. file entry to be inserted
4571 * \param rec it is value of given key i.e. fid
4573 * \retval 0, on success
4574 * \retval -ve, on error
4576 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4577 const struct dt_rec *rec,
4578 const struct dt_key *key, struct thandle *th,
4579 struct lustre_capa *capa, int ignore_quota)
4581 struct osd_object *obj = osd_dt_obj(dt);
4582 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4583 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4584 const struct lu_fid *fid = rec1->rec_fid;
4585 const char *name = (const char *)key;
4586 struct osd_thread_info *oti = osd_oti_get(env);
4587 struct osd_inode_id *id = &oti->oti_id;
4588 struct inode *child_inode = NULL;
4589 struct osd_object *child = NULL;
4593 if (!dt_object_exists(dt))
4596 LASSERT(osd_invariant(obj));
4597 LASSERT(!dt_object_remote(dt));
4598 LASSERT(th != NULL);
4600 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4602 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4605 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4607 rc = osd_remote_fid(env, osd, fid);
4609 CERROR("%s: Can not find object "DFID" rc %d\n",
4610 osd_name(osd), PFID(fid), rc);
4615 /* Insert remote entry */
4616 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4617 struct osd_mdobj_map *omm = osd->od_mdt_map;
4618 struct osd_thandle *oh;
4620 /* If parent on remote MDT, we need put this object
4622 oh = container_of(th, typeof(*oh), ot_super);
4623 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4625 CERROR("%s: add "DFID" error: rc = %d\n",
4627 PFID(lu_object_fid(&dt->do_lu)), rc);
4631 child_inode = igrab(omm->omm_remote_parent->d_inode);
4633 child_inode = osd_create_local_agent_inode(env, osd,
4634 obj, fid, rec1->rec_type & S_IFMT, th);
4635 if (IS_ERR(child_inode))
4636 RETURN(PTR_ERR(child_inode));
4639 /* Insert local entry */
4640 child = osd_object_find(env, dt, fid);
4641 if (IS_ERR(child)) {
4642 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4643 osd_name(osd), PFID(fid),
4644 id->oii_ino, id->oii_gen,
4645 (int)PTR_ERR(child));
4646 RETURN(PTR_ERR(child));
4648 child_inode = igrab(child->oo_inode);
4651 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4653 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4654 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4658 osd_object_put(env, child);
4659 LASSERT(osd_invariant(obj));
4664 * Initialize osd Iterator for given osd index object.
4666 * \param dt osd index object
4669 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4670 struct dt_object *dt,
4672 struct lustre_capa *capa)
4674 struct osd_it_iam *it;
4675 struct osd_object *obj = osd_dt_obj(dt);
4676 struct lu_object *lo = &dt->do_lu;
4677 struct iam_path_descr *ipd;
4678 struct iam_container *bag = &obj->oo_dir->od_container;
4680 if (!dt_object_exists(dt))
4681 return ERR_PTR(-ENOENT);
4683 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4684 return ERR_PTR(-EACCES);
4688 return ERR_PTR(-ENOMEM);
4690 ipd = osd_it_ipd_get(env, bag);
4691 if (likely(ipd != NULL)) {
4695 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4696 return (struct dt_it *)it;
4699 return ERR_PTR(-ENOMEM);
4704 * free given Iterator.
4707 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4709 struct osd_it_iam *it = (struct osd_it_iam *)di;
4710 struct osd_object *obj = it->oi_obj;
4712 iam_it_fini(&it->oi_it);
4713 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4714 lu_object_put(env, &obj->oo_dt.do_lu);
4719 * Move Iterator to record specified by \a key
4721 * \param di osd iterator
4722 * \param key key for index
4724 * \retval +ve di points to record with least key not larger than key
4725 * \retval 0 di points to exact matched key
4726 * \retval -ve failure
4729 static int osd_it_iam_get(const struct lu_env *env,
4730 struct dt_it *di, const struct dt_key *key)
4732 struct osd_thread_info *oti = osd_oti_get(env);
4733 struct osd_it_iam *it = (struct osd_it_iam *)di;
4735 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4736 /* swab quota uid/gid */
4737 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4738 key = (struct dt_key *)&oti->oti_quota_id;
4741 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4747 * \param di osd iterator
4749 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4751 struct osd_it_iam *it = (struct osd_it_iam *)di;
4753 iam_it_put(&it->oi_it);
4757 * Move iterator by one record
4759 * \param di osd iterator
4761 * \retval +1 end of container reached
4763 * \retval -ve failure
4766 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4768 struct osd_it_iam *it = (struct osd_it_iam *)di;
4770 return iam_it_next(&it->oi_it);
4774 * Return pointer to the key under iterator.
4777 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4778 const struct dt_it *di)
4780 struct osd_thread_info *oti = osd_oti_get(env);
4781 struct osd_it_iam *it = (struct osd_it_iam *)di;
4782 struct osd_object *obj = it->oi_obj;
4785 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4787 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4788 /* swab quota uid/gid */
4789 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4790 key = (struct dt_key *)&oti->oti_quota_id;
4797 * Return size of key under iterator (in bytes)
4800 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4802 struct osd_it_iam *it = (struct osd_it_iam *)di;
4804 return iam_it_key_size(&it->oi_it);
4808 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4810 /* check if file type is required */
4811 if (ent->lde_attrs & LUDA_TYPE) {
4812 struct luda_type *lt;
4813 int align = sizeof(*lt) - 1;
4815 len = (len + align) & ~align;
4816 lt = (struct luda_type *)(ent->lde_name + len);
4817 lt->lt_type = cpu_to_le16(DTTOIF(type));
4820 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4824 * build lu direct from backend fs dirent.
4828 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4829 char *name, __u16 namelen, __u16 type, __u32 attr)
4831 ent->lde_attrs = attr | LUDA_FID;
4832 fid_cpu_to_le(&ent->lde_fid, fid);
4834 ent->lde_hash = cpu_to_le64(offset);
4835 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4837 strncpy(ent->lde_name, name, namelen);
4838 ent->lde_name[namelen] = '\0';
4839 ent->lde_namelen = cpu_to_le16(namelen);
4841 /* append lustre attributes */
4842 osd_it_append_attrs(ent, namelen, type);
4846 * Return pointer to the record under iterator.
4848 static int osd_it_iam_rec(const struct lu_env *env,
4849 const struct dt_it *di,
4850 struct dt_rec *dtrec, __u32 attr)
4852 struct osd_it_iam *it = (struct osd_it_iam *)di;
4853 struct osd_thread_info *info = osd_oti_get(env);
4856 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4857 const struct osd_fid_pack *rec;
4858 struct lu_fid *fid = &info->oti_fid;
4859 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4865 name = (char *)iam_it_key_get(&it->oi_it);
4867 RETURN(PTR_ERR(name));
4869 namelen = iam_it_key_size(&it->oi_it);
4871 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4873 RETURN(PTR_ERR(rec));
4875 rc = osd_fid_unpack(fid, rec);
4879 hash = iam_it_store(&it->oi_it);
4881 /* IAM does not store object type in IAM index (dir) */
4882 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4884 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4885 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4886 (struct iam_rec *)dtrec);
4887 osd_quota_unpack(it->oi_obj, dtrec);
4889 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4890 (struct iam_rec *)dtrec);
4897 * Returns cookie for current Iterator position.
4899 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4901 struct osd_it_iam *it = (struct osd_it_iam *)di;
4903 return iam_it_store(&it->oi_it);
4907 * Restore iterator from cookie.
4909 * \param di osd iterator
4910 * \param hash Iterator location cookie
4912 * \retval +ve di points to record with least key not larger than key.
4913 * \retval 0 di points to exact matched key
4914 * \retval -ve failure
4917 static int osd_it_iam_load(const struct lu_env *env,
4918 const struct dt_it *di, __u64 hash)
4920 struct osd_it_iam *it = (struct osd_it_iam *)di;
4922 return iam_it_load(&it->oi_it, hash);
4925 static const struct dt_index_operations osd_index_iam_ops = {
4926 .dio_lookup = osd_index_iam_lookup,
4927 .dio_declare_insert = osd_index_declare_iam_insert,
4928 .dio_insert = osd_index_iam_insert,
4929 .dio_declare_delete = osd_index_declare_iam_delete,
4930 .dio_delete = osd_index_iam_delete,
4932 .init = osd_it_iam_init,
4933 .fini = osd_it_iam_fini,
4934 .get = osd_it_iam_get,
4935 .put = osd_it_iam_put,
4936 .next = osd_it_iam_next,
4937 .key = osd_it_iam_key,
4938 .key_size = osd_it_iam_key_size,
4939 .rec = osd_it_iam_rec,
4940 .store = osd_it_iam_store,
4941 .load = osd_it_iam_load
4947 * Creates or initializes iterator context.
4949 * \retval struct osd_it_ea, iterator structure on success
4952 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4953 struct dt_object *dt,
4955 struct lustre_capa *capa)
4957 struct osd_object *obj = osd_dt_obj(dt);
4958 struct osd_thread_info *info = osd_oti_get(env);
4959 struct osd_it_ea *oie;
4961 struct lu_object *lo = &dt->do_lu;
4962 struct dentry *obj_dentry;
4965 if (!dt_object_exists(dt))
4966 RETURN(ERR_PTR(-ENOENT));
4968 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
4970 RETURN(ERR_PTR(-ENOMEM));
4971 obj_dentry = &oie->oie_dentry;
4973 obj_dentry->d_inode = obj->oo_inode;
4974 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4975 obj_dentry->d_name.hash = 0;
4977 oie->oie_rd_dirent = 0;
4978 oie->oie_it_dirent = 0;
4979 oie->oie_dirent = NULL;
4980 if (unlikely(!info->oti_it_ea_buf_used)) {
4981 oie->oie_buf = info->oti_it_ea_buf;
4982 info->oti_it_ea_buf_used = 1;
4984 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
4985 if (oie->oie_buf == NULL)
4986 RETURN(ERR_PTR(-ENOMEM));
4990 file = &oie->oie_file;
4992 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4993 if (attr & LUDA_64BITHASH)
4994 file->f_mode = FMODE_64BITHASH;
4996 file->f_mode = FMODE_32BITHASH;
4997 file->f_dentry = obj_dentry;
4998 file->f_mapping = obj->oo_inode->i_mapping;
4999 file->f_op = obj->oo_inode->i_fop;
5000 set_file_inode(file, obj->oo_inode);
5003 RETURN((struct dt_it *) oie);
5007 * Destroy or finishes iterator context.
5009 * \param di iterator structure to be destroyed
5011 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5013 struct osd_thread_info *info = osd_oti_get(env);
5014 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5015 struct osd_object *obj = oie->oie_obj;
5016 struct inode *inode = obj->oo_inode;
5019 oie->oie_file.f_op->release(inode, &oie->oie_file);
5020 lu_object_put(env, &obj->oo_dt.do_lu);
5021 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5022 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5024 info->oti_it_ea_buf_used = 0;
5025 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5030 * It position the iterator at given key, so that next lookup continues from
5031 * that key Or it is similar to dio_it->load() but based on a key,
5032 * rather than file position.
5034 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5037 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5039 static int osd_it_ea_get(const struct lu_env *env,
5040 struct dt_it *di, const struct dt_key *key)
5042 struct osd_it_ea *it = (struct osd_it_ea *)di;
5045 LASSERT(((const char *)key)[0] == '\0');
5046 it->oie_file.f_pos = 0;
5047 it->oie_rd_dirent = 0;
5048 it->oie_it_dirent = 0;
5049 it->oie_dirent = NULL;
5057 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5061 struct osd_filldir_cbs {
5062 #ifdef HAVE_DIR_CONTEXT
5063 struct dir_context ctx;
5065 struct osd_it_ea *it;
5068 * It is called internally by ->readdir(). It fills the
5069 * iterator's in-memory data structure with required
5070 * information i.e. name, namelen, rec_size etc.
5072 * \param buf in which information to be filled in.
5073 * \param name name of the file in given dir
5075 * \retval 0 on success
5076 * \retval 1 on buffer full
5078 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5079 loff_t offset, __u64 ino,
5082 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5083 struct osd_object *obj = it->oie_obj;
5084 struct osd_it_ea_dirent *ent = it->oie_dirent;
5085 struct lu_fid *fid = &ent->oied_fid;
5086 struct osd_fid_pack *rec;
5089 /* this should never happen */
5090 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5091 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5095 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5099 /* "." is just the object itself. */
5100 if (namelen == 1 && name[0] == '.') {
5101 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5102 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5103 rec = (struct osd_fid_pack*) (name + namelen + 1);
5104 if (osd_fid_unpack(fid, rec) != 0)
5109 d_type &= ~LDISKFS_DIRENT_LUFID;
5111 /* NOT export local root. */
5112 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5113 ino = obj->oo_inode->i_ino;
5114 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5117 ent->oied_ino = ino;
5118 ent->oied_off = offset;
5119 ent->oied_namelen = namelen;
5120 ent->oied_type = d_type;
5122 memcpy(ent->oied_name, name, namelen);
5124 it->oie_rd_dirent++;
5125 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5130 * Calls ->readdir() to load a directory entry at a time
5131 * and stored it in iterator's in-memory data structure.
5133 * \param di iterator's in memory structure
5135 * \retval 0 on success
5136 * \retval -ve on error
5137 * \retval +1 reach the end of entry
5139 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5140 const struct dt_it *di)
5142 struct osd_it_ea *it = (struct osd_it_ea *)di;
5143 struct osd_object *obj = it->oie_obj;
5144 struct inode *inode = obj->oo_inode;
5145 struct htree_lock *hlock = NULL;
5146 struct file *filp = &it->oie_file;
5148 struct osd_filldir_cbs buf = {
5149 #ifdef HAVE_DIR_CONTEXT
5150 .ctx.actor = osd_ldiskfs_filldir,
5156 it->oie_dirent = it->oie_buf;
5157 it->oie_rd_dirent = 0;
5159 if (obj->oo_hl_head != NULL) {
5160 hlock = osd_oti_get(env)->oti_hlock;
5161 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5162 inode, LDISKFS_HLOCK_READDIR);
5164 down_read(&obj->oo_ext_idx_sem);
5167 #ifdef HAVE_DIR_CONTEXT
5168 buf.ctx.pos = filp->f_pos;
5169 rc = inode->i_fop->iterate(filp, &buf.ctx);
5170 filp->f_pos = buf.ctx.pos;
5172 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5176 ldiskfs_htree_unlock(hlock);
5178 up_read(&obj->oo_ext_idx_sem);
5180 if (it->oie_rd_dirent == 0) {
5181 /*If it does not get any dirent, it means it has been reached
5182 *to the end of the dir */
5183 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5187 it->oie_dirent = it->oie_buf;
5188 it->oie_it_dirent = 1;
5195 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5196 * to load a directory entry at a time and stored it in
5197 * iterator's in-memory data structure.
5199 * \param di iterator's in memory structure
5201 * \retval +ve iterator reached to end
5202 * \retval 0 iterator not reached to end
5203 * \retval -ve on error
5205 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5207 struct osd_it_ea *it = (struct osd_it_ea *)di;
5212 if (it->oie_it_dirent < it->oie_rd_dirent) {
5214 (void *) it->oie_dirent +
5215 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5216 it->oie_dirent->oied_namelen);
5217 it->oie_it_dirent++;
5220 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5223 rc = osd_ldiskfs_it_fill(env, di);
5230 * Returns the key at current position from iterator's in memory structure.
5232 * \param di iterator's in memory structure
5234 * \retval key i.e. struct dt_key on success
5236 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5237 const struct dt_it *di)
5239 struct osd_it_ea *it = (struct osd_it_ea *)di;
5241 return (struct dt_key *)it->oie_dirent->oied_name;
5245 * Returns the key's size at current position from iterator's in memory structure.
5247 * \param di iterator's in memory structure
5249 * \retval key_size i.e. struct dt_key on success
5251 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5253 struct osd_it_ea *it = (struct osd_it_ea *)di;
5255 return it->oie_dirent->oied_namelen;
5259 osd_dirent_update(handle_t *jh, struct super_block *sb,
5260 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
5261 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
5263 struct osd_fid_pack *rec;
5267 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
5268 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
5270 rc = ldiskfs_journal_get_write_access(jh, bh);
5274 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5275 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5276 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5282 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
5284 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
5285 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5292 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5294 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5295 "dot_dotdot = %d\n", dot_dotdot);
5297 if (LDISKFS_DIR_REC_LEN(de) >=
5298 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5305 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5306 struct inode *dir, struct inode *inode,
5307 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
5308 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5309 struct htree_lock *hlock)
5311 struct dentry *dentry;
5312 struct osd_fid_pack *rec;
5313 struct ldiskfs_dentry_param *ldp;
5317 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5318 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5321 /* There is enough space to hold the FID-in-dirent. */
5322 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
5323 dir->i_sb->s_blocksize)) {
5324 rc = ldiskfs_journal_get_write_access(jh, bh);
5328 de->name[de->name_len] = 0;
5329 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5330 rec->fp_len = sizeof(struct lu_fid) + 1;
5331 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5332 de->file_type |= LDISKFS_DIRENT_LUFID;
5334 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5339 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5343 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5345 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5346 osd_get_ldiskfs_dirent_param(ldp, fid);
5347 dentry->d_fsdata = (void *)ldp;
5348 ll_vfs_dq_init(dir);
5349 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
5350 /* It is too bad, we cannot reinsert the name entry back.
5351 * That means we lose it! */
5353 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5354 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5355 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5356 dir->i_ino, dir->i_generation,
5357 ent->oied_namelen, ent->oied_name, PFID(fid), rc);
5363 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5364 struct osd_it_ea *it, struct lu_fid *fid,
5365 struct osd_inode_id *id, __u32 *attr)
5367 struct osd_thread_info *info = osd_oti_get(env);
5368 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5369 struct osd_device *dev = osd_obj2dev(obj);
5370 struct super_block *sb = osd_sb(dev);
5371 const char *devname =
5372 LDISKFS_SB(sb)->s_es->s_volume_name;
5373 struct osd_it_ea_dirent *ent = it->oie_dirent;
5374 struct inode *dir = obj->oo_inode;
5375 struct htree_lock *hlock = NULL;
5376 struct buffer_head *bh = NULL;
5377 handle_t *jh = NULL;
5378 struct ldiskfs_dir_entry_2 *de;
5379 struct dentry *dentry;
5380 struct inode *inode;
5387 if (ent->oied_name[0] == '.') {
5388 if (ent->oied_namelen == 1)
5390 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5394 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
5397 /* We need to ensure that the name entry is still valid.
5398 * Because it may be removed or renamed by other already.
5400 * The unlink or rename operation will start journal before PDO lock,
5401 * so to avoid deadlock, here we need to start journal handle before
5402 * related PDO lock also. But because we do not know whether there
5403 * will be something to be repaired before PDO lock, we just start
5404 * journal without conditions.
5406 * We may need to remove the name entry firstly, then insert back.
5407 * One credit is for user quota file update.
5408 * One credit is for group quota file update.
5409 * Two credits are for dirty inode. */
5410 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5411 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5414 if (dev->od_dirent_journal != 0) {
5415 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5418 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5419 "check_repair, dir = %lu/%u, credits = %d, "
5420 "name = %.*s: rc = %d\n",
5421 devname, dir->i_ino, dir->i_generation, credits,
5422 ent->oied_namelen, ent->oied_name, rc);
5426 if (obj->oo_hl_head != NULL) {
5427 hlock = osd_oti_get(env)->oti_hlock;
5428 /* "0" means exclusive lock for the whole directory.
5429 * We need to prevent others access such name entry
5430 * during the delete + insert. Neither HLOCK_ADD nor
5431 * HLOCK_DEL cannot guarantee the atomicity. */
5432 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5434 down_write(&obj->oo_ext_idx_sem);
5437 if (obj->oo_hl_head != NULL) {
5438 hlock = osd_oti_get(env)->oti_hlock;
5439 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5440 LDISKFS_HLOCK_LOOKUP);
5442 down_read(&obj->oo_ext_idx_sem);
5446 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5447 /* For dot/dotdot entry, if there is not enough space to hold the
5448 * FID-in-dirent, just keep them there. It only happens when the
5449 * device upgraded from 1.8 or restored from MDT file-level backup.
5450 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5451 * and needs to get FID from LMA when readdir, it will not affect the
5452 * performance much. */
5453 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
5454 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5455 *attr |= LUDA_IGNORE;
5456 GOTO(out_journal, rc = 0);
5459 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5460 inode = osd_iget(info, dev, id);
5461 if (IS_ERR(inode)) {
5462 rc = PTR_ERR(inode);
5463 if (rc == -ENOENT || rc == -ESTALE)
5466 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5467 "check_repair, dir = %lu/%u, name = %.*s: "
5469 devname, dir->i_ino, dir->i_generation,
5470 ent->oied_namelen, ent->oied_name, rc);
5472 GOTO(out_journal, rc);
5475 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5477 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5479 if (fid_is_sane(fid)) {
5480 /* FID-in-dirent is valid. */
5481 if (lu_fid_eq(fid, &lma->lma_self_fid))
5482 GOTO(out_inode, rc = 0);
5484 /* Do not repair under dryrun mode. */
5485 if (*attr & LUDA_VERIFY_DRYRUN) {
5486 *attr |= LUDA_REPAIR;
5487 GOTO(out_inode, rc = 0);
5490 if (dev->od_dirent_journal == 0) {
5494 ldiskfs_htree_unlock(hlock);
5496 up_read(&obj->oo_ext_idx_sem);
5497 dev->od_dirent_journal = 1;
5501 *fid = lma->lma_self_fid;
5503 /* Update the FID-in-dirent. */
5504 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5506 *attr |= LUDA_REPAIR;
5508 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5509 "in the dirent, dir = %lu/%u, "
5510 "name = %.*s, "DFID": rc = %d\n",
5511 devname, dir->i_ino, dir->i_generation,
5512 ent->oied_namelen, ent->oied_name,
5515 /* Do not repair under dryrun mode. */
5516 if (*attr & LUDA_VERIFY_DRYRUN) {
5517 *fid = lma->lma_self_fid;
5518 *attr |= LUDA_REPAIR;
5519 GOTO(out_inode, rc = 0);
5522 if (dev->od_dirent_journal == 0) {
5526 ldiskfs_htree_unlock(hlock);
5528 up_read(&obj->oo_ext_idx_sem);
5529 dev->od_dirent_journal = 1;
5533 *fid = lma->lma_self_fid;
5535 /* Append the FID-in-dirent. */
5536 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5537 fid, bh, de, hlock);
5539 *attr |= LUDA_REPAIR;
5541 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5542 "after the dirent, dir = %lu/%u, "
5543 "name = %.*s, "DFID": rc = %d\n",
5544 devname, dir->i_ino, dir->i_generation,
5545 ent->oied_namelen, ent->oied_name,
5548 } else if (rc == -ENODATA) {
5549 /* Do not repair under dryrun mode. */
5550 if (*attr & LUDA_VERIFY_DRYRUN) {
5551 if (fid_is_sane(fid)) {
5552 *attr |= LUDA_REPAIR;
5554 lu_igif_build(fid, inode->i_ino,
5555 inode->i_generation);
5556 *attr |= LUDA_UPGRADE;
5558 GOTO(out_inode, rc = 0);
5561 if (dev->od_dirent_journal == 0) {
5565 ldiskfs_htree_unlock(hlock);
5567 up_read(&obj->oo_ext_idx_sem);
5568 dev->od_dirent_journal = 1;
5573 if (unlikely(fid_is_sane(fid))) {
5574 /* FID-in-dirent exists, but FID-in-LMA is lost.
5575 * Trust the FID-in-dirent, and add FID-in-LMA. */
5576 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5578 *attr |= LUDA_REPAIR;
5580 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5581 "update dirent, dir = %lu/%u, "
5582 "name = %.*s, "DFID": rc = %d\n",
5583 devname, dir->i_ino, dir->i_generation,
5584 ent->oied_namelen, ent->oied_name,
5587 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5588 /* It is probably IGIF object. Only aappend the
5589 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5590 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5591 fid, bh, de, hlock);
5593 *attr |= LUDA_UPGRADE;
5595 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5596 "after the dirent, dir = %lu/%u, "
5597 "name = %.*s, "DFID": rc = %d\n",
5598 devname, dir->i_ino, dir->i_generation,
5599 ent->oied_namelen, ent->oied_name,
5604 GOTO(out_inode, rc);
5611 if (hlock != NULL) {
5612 ldiskfs_htree_unlock(hlock);
5614 if (dev->od_dirent_journal != 0)
5615 up_write(&obj->oo_ext_idx_sem);
5617 up_read(&obj->oo_ext_idx_sem);
5620 ldiskfs_journal_stop(jh);
5621 if (rc >= 0 && !dirty)
5622 dev->od_dirent_journal = 0;
5627 * Returns the value at current position from iterator's in memory structure.
5629 * \param di struct osd_it_ea, iterator's in memory structure
5630 * \param attr attr requested for dirent.
5631 * \param lde lustre dirent
5633 * \retval 0 no error and \param lde has correct lustre dirent.
5634 * \retval -ve on error
5636 static inline int osd_it_ea_rec(const struct lu_env *env,
5637 const struct dt_it *di,
5638 struct dt_rec *dtrec, __u32 attr)
5640 struct osd_it_ea *it = (struct osd_it_ea *)di;
5641 struct osd_object *obj = it->oie_obj;
5642 struct osd_device *dev = osd_obj2dev(obj);
5643 struct osd_thread_info *oti = osd_oti_get(env);
5644 struct osd_inode_id *id = &oti->oti_id;
5645 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5646 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5647 __u32 ino = it->oie_dirent->oied_ino;
5651 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5653 if (attr & LUDA_VERIFY) {
5654 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5655 attr |= LUDA_IGNORE;
5656 /* If the parent is on remote MDT, and there
5657 * is no FID-in-dirent, then we have to get
5658 * the parent FID from the linkEA. */
5659 osd_get_pfid_from_linkea(env, obj, fid);
5661 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5665 attr &= ~LU_DIRENT_ATTRS_MASK;
5666 if (!fid_is_sane(fid)) {
5667 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP) &&
5668 likely(it->oie_dirent->oied_namelen != 2 ||
5669 it->oie_dirent->oied_name[0] != '.' ||
5670 it->oie_dirent->oied_name[1] != '.'))
5673 if (unlikely(ino == osd_remote_parent_ino(dev)))
5674 /* If the parent is on remote MDT, and there
5675 * is no FID-in-dirent, then we have to get
5676 * the parent FID from the linkEA. */
5677 rc = osd_get_pfid_from_linkea(env, obj, fid);
5679 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5681 osd_id_gen(id, ino, OSD_OII_NOGEN);
5685 /* Pack the entry anyway, at least the offset is right. */
5686 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5687 it->oie_dirent->oied_name,
5688 it->oie_dirent->oied_namelen,
5689 it->oie_dirent->oied_type, attr);
5694 if (osd_remote_fid(env, dev, fid))
5697 if (likely(!(attr & LUDA_IGNORE) && rc == 0))
5698 osd_add_oi_cache(oti, dev, id, fid);
5700 RETURN(rc > 0 ? 0 : rc);
5704 * Returns the record size size at current position.
5706 * This function will return record(lu_dirent) size in bytes.
5708 * \param[in] env execution environment
5709 * \param[in] di iterator's in memory structure
5710 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5711 * calculate the lu_dirent size.
5713 * \retval record size(in bytes & in memory) of the current lu_dirent
5716 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5719 struct osd_it_ea *it = (struct osd_it_ea *)di;
5721 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5725 * Returns a cookie for current position of the iterator head, so that
5726 * user can use this cookie to load/start the iterator next time.
5728 * \param di iterator's in memory structure
5730 * \retval cookie for current position, on success
5732 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5734 struct osd_it_ea *it = (struct osd_it_ea *)di;
5736 return it->oie_dirent->oied_off;
5740 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5741 * to load a directory entry at a time and stored it i inn,
5742 * in iterator's in-memory data structure.
5744 * \param di struct osd_it_ea, iterator's in memory structure
5746 * \retval +ve on success
5747 * \retval -ve on error
5749 static int osd_it_ea_load(const struct lu_env *env,
5750 const struct dt_it *di, __u64 hash)
5752 struct osd_it_ea *it = (struct osd_it_ea *)di;
5756 it->oie_file.f_pos = hash;
5758 rc = osd_ldiskfs_it_fill(env, di);
5769 * Index lookup function for interoperability mode (b11826).
5771 * \param key, key i.e. file name to be searched
5773 * \retval +ve, on success
5774 * \retval -ve, on error
5776 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5777 struct dt_rec *rec, const struct dt_key *key,
5778 struct lustre_capa *capa)
5780 struct osd_object *obj = osd_dt_obj(dt);
5785 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5786 LINVRNT(osd_invariant(obj));
5788 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5791 rc = osd_ea_lookup_rec(env, obj, rec, key);
5798 * Index and Iterator operations for interoperability
5799 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5801 static const struct dt_index_operations osd_index_ea_ops = {
5802 .dio_lookup = osd_index_ea_lookup,
5803 .dio_declare_insert = osd_index_declare_ea_insert,
5804 .dio_insert = osd_index_ea_insert,
5805 .dio_declare_delete = osd_index_declare_ea_delete,
5806 .dio_delete = osd_index_ea_delete,
5808 .init = osd_it_ea_init,
5809 .fini = osd_it_ea_fini,
5810 .get = osd_it_ea_get,
5811 .put = osd_it_ea_put,
5812 .next = osd_it_ea_next,
5813 .key = osd_it_ea_key,
5814 .key_size = osd_it_ea_key_size,
5815 .rec = osd_it_ea_rec,
5816 .rec_size = osd_it_ea_rec_size,
5817 .store = osd_it_ea_store,
5818 .load = osd_it_ea_load
5822 static void *osd_key_init(const struct lu_context *ctx,
5823 struct lu_context_key *key)
5825 struct osd_thread_info *info;
5827 OBD_ALLOC_PTR(info);
5829 return ERR_PTR(-ENOMEM);
5831 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5832 if (info->oti_it_ea_buf == NULL)
5835 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5837 info->oti_hlock = ldiskfs_htree_lock_alloc();
5838 if (info->oti_hlock == NULL)
5844 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5847 return ERR_PTR(-ENOMEM);
5850 static void osd_key_fini(const struct lu_context *ctx,
5851 struct lu_context_key *key, void* data)
5853 struct osd_thread_info *info = data;
5855 if (info->oti_hlock != NULL)
5856 ldiskfs_htree_lock_free(info->oti_hlock);
5857 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5858 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5859 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5860 lu_buf_free(&info->oti_big_buf);
5864 static void osd_key_exit(const struct lu_context *ctx,
5865 struct lu_context_key *key, void *data)
5867 struct osd_thread_info *info = data;
5869 LASSERT(info->oti_r_locks == 0);
5870 LASSERT(info->oti_w_locks == 0);
5871 LASSERT(info->oti_txns == 0);
5874 /* type constructor/destructor: osd_type_init, osd_type_fini */
5875 LU_TYPE_INIT_FINI(osd, &osd_key);
5877 struct lu_context_key osd_key = {
5878 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5879 .lct_init = osd_key_init,
5880 .lct_fini = osd_key_fini,
5881 .lct_exit = osd_key_exit
5885 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5886 const char *name, struct lu_device *next)
5888 struct osd_device *osd = osd_dev(d);
5890 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5891 >= sizeof(osd->od_svname))
5893 return osd_procfs_init(osd, name);
5896 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
5898 struct seq_server_site *ss = osd_seq_site(osd);
5902 if (osd->od_is_ost || osd->od_cl_seq != NULL)
5905 if (unlikely(ss == NULL))
5908 OBD_ALLOC_PTR(osd->od_cl_seq);
5909 if (osd->od_cl_seq == NULL)
5912 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
5913 osd->od_svname, ss->ss_server_seq);
5916 OBD_FREE_PTR(osd->od_cl_seq);
5917 osd->od_cl_seq = NULL;
5923 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
5925 if (osd->od_cl_seq == NULL)
5928 seq_client_fini(osd->od_cl_seq);
5929 OBD_FREE_PTR(osd->od_cl_seq);
5930 osd->od_cl_seq = NULL;
5933 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5937 /* shutdown quota slave instance associated with the device */
5938 if (o->od_quota_slave != NULL) {
5939 qsd_fini(env, o->od_quota_slave);
5940 o->od_quota_slave = NULL;
5943 osd_fid_fini(env, o);
5948 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5952 if (o->od_mnt != NULL) {
5953 shrink_dcache_sb(osd_sb(o));
5954 osd_sync(env, &o->od_dt_dev);
5963 static int osd_mount(const struct lu_env *env,
5964 struct osd_device *o, struct lustre_cfg *cfg)
5966 const char *name = lustre_cfg_string(cfg, 0);
5967 const char *dev = lustre_cfg_string(cfg, 1);
5969 unsigned long page, s_flags, lmd_flags = 0;
5970 struct page *__page;
5971 struct file_system_type *type;
5972 char *options = NULL;
5974 struct osd_thread_info *info = osd_oti_get(env);
5975 struct lu_fid *fid = &info->oti_fid;
5976 struct inode *inode;
5980 if (o->od_mnt != NULL)
5983 if (strlen(dev) >= sizeof(o->od_mntdev))
5985 strcpy(o->od_mntdev, dev);
5987 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5989 GOTO(out, rc = -ENOMEM);
5991 str = lustre_cfg_string(cfg, 2);
5992 s_flags = simple_strtoul(str, NULL, 0);
5993 str = strstr(str, ":");
5995 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5996 opts = lustre_cfg_string(cfg, 3);
5997 page = (unsigned long)page_address(__page);
5998 options = (char *)page;
6001 strcat(options, "user_xattr,acl");
6003 strcat(options, opts);
6005 /* Glom up mount options */
6006 if (*options != '\0')
6007 strcat(options, ",");
6008 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6010 type = get_fs_type("ldiskfs");
6012 CERROR("%s: cannot find ldiskfs module\n", name);
6013 GOTO(out, rc = -ENODEV);
6016 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6017 module_put(type->owner);
6019 if (IS_ERR(o->od_mnt)) {
6020 rc = PTR_ERR(o->od_mnt);
6022 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6026 #ifdef HAVE_DEV_SET_RDONLY
6027 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6028 CERROR("%s: underlying device %s is marked as read-only. "
6029 "Setup failed\n", name, dev);
6030 GOTO(out_mnt, rc = -EROFS);
6034 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6035 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6036 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6037 GOTO(out_mnt, rc = -EINVAL);
6040 #ifdef LDISKFS_MOUNT_DIRDATA
6041 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6042 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6043 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6044 else if (!o->od_is_ost)
6045 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6046 "enabling the dirdata feature. If you do not want to "
6047 "downgrade to Lustre-1.x again, you can enable it via "
6048 "'tune2fs -O dirdata device'\n", name, dev);
6050 inode = osd_sb(o)->s_root->d_inode;
6051 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6052 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6054 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6058 if (lmd_flags & LMD_FLG_NOSCRUB)
6069 OBD_PAGE_FREE(__page);
6074 static struct lu_device *osd_device_fini(const struct lu_env *env,
6075 struct lu_device *d)
6077 struct osd_device *o = osd_dev(d);
6080 osd_shutdown(env, o);
6082 osd_scrub_cleanup(env, o);
6083 osd_obj_map_fini(o);
6089 static int osd_device_init0(const struct lu_env *env,
6090 struct osd_device *o,
6091 struct lustre_cfg *cfg)
6093 struct lu_device *l = osd2lu_dev(o);
6094 struct osd_thread_info *info;
6098 /* if the module was re-loaded, env can loose its keys */
6099 rc = lu_env_refill((struct lu_env *) env);
6102 info = osd_oti_get(env);
6105 l->ld_ops = &osd_lu_ops;
6106 o->od_dt_dev.dd_ops = &osd_dt_ops;
6108 spin_lock_init(&o->od_osfs_lock);
6109 mutex_init(&o->od_otable_mutex);
6111 o->od_capa_hash = init_capa_hash();
6112 if (o->od_capa_hash == NULL)
6113 GOTO(out, rc = -ENOMEM);
6115 o->od_read_cache = 1;
6116 o->od_writethrough_cache = 1;
6117 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6119 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6120 sizeof(o->od_svname));
6121 if (cplen >= sizeof(o->od_svname)) {
6126 if (server_name_is_ost(o->od_svname))
6129 o->od_full_scrub_ratio = OFSR_DEFAULT;
6130 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6131 rc = osd_mount(env, o, cfg);
6135 rc = osd_obj_map_init(env, o);
6139 rc = lu_site_init(&o->od_site, l);
6141 GOTO(out_compat, rc);
6142 o->od_site.ls_bottom_dev = l;
6144 rc = lu_site_init_finish(&o->od_site);
6148 INIT_LIST_HEAD(&o->od_ios_list);
6149 /* setup scrub, including OI files initialization */
6150 rc = osd_scrub_setup(env, o);
6154 rc = osd_procfs_init(o, o->od_svname);
6156 CERROR("%s: can't initialize procfs: rc = %d\n",
6158 GOTO(out_scrub, rc);
6161 LASSERT(l->ld_site->ls_linkage.next != NULL);
6162 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6164 /* initialize quota slave instance */
6165 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6167 if (IS_ERR(o->od_quota_slave)) {
6168 rc = PTR_ERR(o->od_quota_slave);
6169 o->od_quota_slave = NULL;
6170 GOTO(out_procfs, rc);
6178 osd_scrub_cleanup(env, o);
6180 lu_site_fini(&o->od_site);
6182 osd_obj_map_fini(o);
6186 cleanup_capa_hash(o->od_capa_hash);
6191 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6192 struct lu_device_type *t,
6193 struct lustre_cfg *cfg)
6195 struct osd_device *o;
6200 return ERR_PTR(-ENOMEM);
6202 rc = dt_device_init(&o->od_dt_dev, t);
6204 /* Because the ctx might be revived in dt_device_init,
6205 * refill the env here */
6206 lu_env_refill((struct lu_env *)env);
6207 rc = osd_device_init0(env, o, cfg);
6209 dt_device_fini(&o->od_dt_dev);
6212 if (unlikely(rc != 0))
6215 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6218 static struct lu_device *osd_device_free(const struct lu_env *env,
6219 struct lu_device *d)
6221 struct osd_device *o = osd_dev(d);
6224 cleanup_capa_hash(o->od_capa_hash);
6225 /* XXX: make osd top device in order to release reference */
6226 d->ld_site->ls_top_dev = d;
6227 lu_site_purge(env, d->ld_site, -1);
6228 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6229 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6230 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6232 lu_site_fini(&o->od_site);
6233 dt_device_fini(&o->od_dt_dev);
6238 static int osd_process_config(const struct lu_env *env,
6239 struct lu_device *d, struct lustre_cfg *cfg)
6241 struct osd_device *o = osd_dev(d);
6245 switch (cfg->lcfg_command) {
6247 rc = osd_mount(env, o, cfg);
6250 lu_dev_del_linkage(d->ld_site, d);
6251 rc = osd_shutdown(env, o);
6254 LASSERT(&o->od_dt_dev);
6255 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6256 cfg, &o->od_dt_dev);
6257 if (rc > 0 || rc == -ENOSYS)
6258 rc = class_process_proc_param(PARAM_OST,
6259 lprocfs_osd_obd_vars,
6260 cfg, &o->od_dt_dev);
6269 static int osd_recovery_complete(const struct lu_env *env,
6270 struct lu_device *d)
6272 struct osd_device *osd = osd_dev(d);
6276 if (osd->od_quota_slave == NULL)
6279 /* start qsd instance on recovery completion, this notifies the quota
6280 * slave code that we are about to process new requests now */
6281 rc = qsd_start(env, osd->od_quota_slave);
6286 * we use exports to track all osd users
6288 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6289 struct obd_device *obd, struct obd_uuid *cluuid,
6290 struct obd_connect_data *data, void *localdata)
6292 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6293 struct lustre_handle conn;
6297 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6299 rc = class_connect(&conn, obd, cluuid);
6303 *exp = class_conn2export(&conn);
6305 spin_lock(&osd->od_osfs_lock);
6307 spin_unlock(&osd->od_osfs_lock);
6313 * once last export (we don't count self-export) disappeared
6314 * osd can be released
6316 static int osd_obd_disconnect(struct obd_export *exp)
6318 struct obd_device *obd = exp->exp_obd;
6319 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6320 int rc, release = 0;
6323 /* Only disconnect the underlying layers on the final disconnect. */
6324 spin_lock(&osd->od_osfs_lock);
6326 if (osd->od_connects == 0)
6328 spin_unlock(&osd->od_osfs_lock);
6330 rc = class_disconnect(exp); /* bz 9811 */
6332 if (rc == 0 && release)
6333 class_manual_cleanup(obd);
6337 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6338 struct lu_device *dev)
6340 struct osd_device *osd = osd_dev(dev);
6341 struct lr_server_data *lsd =
6342 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6346 if (osd->od_quota_slave != NULL) {
6347 /* set up quota slave objects */
6348 result = qsd_prepare(env, osd->od_quota_slave);
6353 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6354 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6355 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6356 osd->od_index_in_idif = 1;
6358 osd->od_index_in_idif = 0;
6359 result = osd_register_proc_index_in_idif(osd);
6364 osd->od_index_in_idif = 1;
6368 result = osd_fid_init(env, osd);
6373 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6374 struct lu_fid *fid, struct md_op_data *op_data)
6376 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6378 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6381 static const struct lu_object_operations osd_lu_obj_ops = {
6382 .loo_object_init = osd_object_init,
6383 .loo_object_delete = osd_object_delete,
6384 .loo_object_release = osd_object_release,
6385 .loo_object_free = osd_object_free,
6386 .loo_object_print = osd_object_print,
6387 .loo_object_invariant = osd_object_invariant
6390 const struct lu_device_operations osd_lu_ops = {
6391 .ldo_object_alloc = osd_object_alloc,
6392 .ldo_process_config = osd_process_config,
6393 .ldo_recovery_complete = osd_recovery_complete,
6394 .ldo_prepare = osd_prepare,
6397 static const struct lu_device_type_operations osd_device_type_ops = {
6398 .ldto_init = osd_type_init,
6399 .ldto_fini = osd_type_fini,
6401 .ldto_start = osd_type_start,
6402 .ldto_stop = osd_type_stop,
6404 .ldto_device_alloc = osd_device_alloc,
6405 .ldto_device_free = osd_device_free,
6407 .ldto_device_init = osd_device_init,
6408 .ldto_device_fini = osd_device_fini
6411 static struct lu_device_type osd_device_type = {
6412 .ldt_tags = LU_DEVICE_DT,
6413 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6414 .ldt_ops = &osd_device_type_ops,
6415 .ldt_ctx_tags = LCT_LOCAL,
6419 * lprocfs legacy support.
6421 static struct obd_ops osd_obd_device_ops = {
6422 .o_owner = THIS_MODULE,
6423 .o_connect = osd_obd_connect,
6424 .o_disconnect = osd_obd_disconnect,
6425 .o_fid_alloc = osd_fid_alloc,
6428 static int __init osd_mod_init(void)
6434 rc = lu_kmem_init(ldiskfs_caches);
6438 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6439 lprocfs_osd_module_vars,
6440 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6442 lu_kmem_fini(ldiskfs_caches);
6446 static void __exit osd_mod_exit(void)
6448 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6449 lu_kmem_fini(ldiskfs_caches);
6452 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6453 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6454 MODULE_LICENSE("GPL");
6456 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);