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, 2013, 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_MDS
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 "osd_internal.h"
68 /* llo_* api support */
69 #include <md_object.h>
70 #include <lustre_quota.h>
73 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
74 "ldiskfs with parallel directory operations");
76 int ldiskfs_track_declares_assert;
77 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
78 "LBUG during tracking of declares");
80 static const char dot[] = ".";
81 static const char dotdot[] = "..";
82 static const char remote_obj_dir[] = "REM_OBJ_DIR";
84 static const struct lu_object_operations osd_lu_obj_ops;
85 static const struct dt_object_operations osd_obj_ops;
86 static const struct dt_object_operations osd_obj_ea_ops;
87 static const struct dt_object_operations osd_obj_otable_it_ops;
88 static const struct dt_index_operations osd_index_iam_ops;
89 static const struct dt_index_operations osd_index_ea_ops;
91 int osd_trans_declare_op2rb[] = {
92 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
93 [OSD_OT_PUNCH] = OSD_OT_MAX,
94 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
95 [OSD_OT_CREATE] = OSD_OT_DESTROY,
96 [OSD_OT_DESTROY] = OSD_OT_CREATE,
97 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
98 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
99 [OSD_OT_WRITE] = OSD_OT_WRITE,
100 [OSD_OT_INSERT] = OSD_OT_DELETE,
101 [OSD_OT_DELETE] = OSD_OT_INSERT,
102 [OSD_OT_QUOTA] = OSD_OT_MAX,
105 static int osd_has_index(const struct osd_object *obj)
107 return obj->oo_dt.do_index_ops != NULL;
110 static int osd_object_invariant(const struct lu_object *l)
112 return osd_invariant(osd_obj(l));
116 * Concurrency: doesn't matter
118 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
120 return osd_oti_get(env)->oti_r_locks > 0;
124 * Concurrency: doesn't matter
126 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
128 struct osd_thread_info *oti = osd_oti_get(env);
129 return oti->oti_w_locks > 0 && o->oo_owner == env;
133 * Concurrency: doesn't access mutable data
135 static int osd_root_get(const struct lu_env *env,
136 struct dt_device *dev, struct lu_fid *f)
138 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
143 * OSD object methods.
147 * Concurrency: no concurrent access is possible that early in object
150 static struct lu_object *osd_object_alloc(const struct lu_env *env,
151 const struct lu_object_header *hdr,
154 struct osd_object *mo;
160 l = &mo->oo_dt.do_lu;
161 dt_object_init(&mo->oo_dt, NULL, d);
162 mo->oo_dt.do_ops = &osd_obj_ea_ops;
163 l->lo_ops = &osd_lu_obj_ops;
164 init_rwsem(&mo->oo_sem);
165 init_rwsem(&mo->oo_ext_idx_sem);
166 spin_lock_init(&mo->oo_guard);
173 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
174 const char *name, void *buf, int len)
176 dentry->d_inode = inode;
177 dentry->d_sb = inode->i_sb;
178 return inode->i_op->getxattr(dentry, name, buf, len);
181 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
182 struct dentry *dentry, struct lustre_mdt_attrs *lma)
186 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
187 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
188 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
190 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
191 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
193 lustre_lma_swab(lma);
194 /* Check LMA compatibility */
195 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
196 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
197 "for fid = "DFID", ino = %lu\n",
198 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
199 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
200 PFID(&lma->lma_self_fid), inode->i_ino);
203 } else if (rc == 0) {
211 * retrieve object from backend ext fs.
213 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
214 struct osd_inode_id *id)
216 struct inode *inode = NULL;
218 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
220 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
221 id->oii_ino, PTR_ERR(inode));
222 } else if (id->oii_gen != OSD_OII_NOGEN &&
223 inode->i_generation != id->oii_gen) {
224 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
226 id->oii_ino, id->oii_gen, inode->i_generation);
228 inode = ERR_PTR(-ESTALE);
229 } else if (inode->i_nlink == 0) {
230 /* due to parallel readdir and unlink,
231 * we can have dead inode here. */
232 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
233 make_bad_inode(inode);
235 inode = ERR_PTR(-ESTALE);
236 } else if (is_bad_inode(inode)) {
237 CWARN("%.16s: bad inode: ino = %u\n",
238 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
240 inode = ERR_PTR(-ENOENT);
242 if (id->oii_gen == OSD_OII_NOGEN)
243 osd_id_gen(id, inode->i_ino, inode->i_generation);
245 /* Do not update file c/mtime in ldiskfs.
246 * NB: we don't have any lock to protect this because we don't
247 * have reference on osd_object now, but contention with
248 * another lookup + attr_set can't happen in the tiny window
249 * between if (...) and set S_NOCMTIME. */
250 if (!(inode->i_flags & S_NOCMTIME))
251 inode->i_flags |= S_NOCMTIME;
256 static struct inode *
257 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
258 struct osd_inode_id *id, struct lu_fid *fid)
260 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
264 inode = osd_iget(info, dev, id);
268 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
270 *fid = lma->lma_self_fid;
271 } else if (rc == -ENODATA) {
272 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
273 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
275 lu_igif_build(fid, inode->i_ino, inode->i_generation);
283 static struct inode *
284 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
285 struct osd_inode_id *id, const struct lu_fid *fid)
287 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
291 inode = osd_iget(info, dev, id);
295 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
304 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
305 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
306 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
308 return ERR_PTR(-EREMCHG);
314 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
315 const struct lu_fid *fid,
316 const struct lu_object_conf *conf)
318 struct osd_thread_info *info;
319 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
320 struct osd_device *dev;
321 struct osd_idmap_cache *oic;
322 struct osd_inode_id *id;
324 struct osd_scrub *scrub;
325 struct scrub_file *sf;
330 LINVRNT(osd_invariant(obj));
331 LASSERT(obj->oo_inode == NULL);
332 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
335 scrub = &dev->od_scrub;
336 sf = &scrub->os_file;
337 info = osd_oti_get(env);
339 oic = &info->oti_cache;
341 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
344 /* Search order: 1. per-thread cache. */
345 if (lu_fid_eq(fid, &oic->oic_fid)) {
351 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
352 /* Search order: 2. OI scrub pending list. */
353 result = osd_oii_lookup(dev, fid, id);
358 if (sf->sf_flags & SF_INCONSISTENT)
362 * Objects are created as locking anchors or place holders for objects
363 * yet to be created. No need to osd_oi_lookup() at here because FID
364 * shouldn't never be re-used, if it's really a duplicate FID from
365 * unexpected reason, we should be able to detect it later by calling
366 * do_create->osd_oi_insert()
368 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
369 GOTO(out, result = 0);
371 /* Search order: 3. OI files. */
372 result = osd_oi_lookup(info, dev, fid, id, true);
373 if (result == -ENOENT) {
374 if (!fid_is_norm(fid) || fid_is_on_ost(info, dev, fid) ||
375 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
377 GOTO(out, result = 0);
387 inode = osd_iget(info, dev, id);
389 inode = osd_iget_verify(info, dev, id, fid);
391 result = PTR_ERR(inode);
392 if (result == -ENOENT || result == -ESTALE) {
393 fid_zero(&oic->oic_fid);
395 } else if (result == -EREMCHG) {
398 if (thread_is_running(&scrub->os_thread)) {
399 result = -EINPROGRESS;
400 } else if (!dev->od_noscrub) {
401 result = osd_scrub_start(dev);
402 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
403 "for "DFID", rc = %d [1]\n",
404 LDISKFS_SB(osd_sb(dev))->s_es->\
405 s_volume_name,PFID(fid), result);
406 if (result == 0 || result == -EALREADY)
407 result = -EINPROGRESS;
416 obj->oo_inode = inode;
417 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
419 obj->oo_compat_dot_created = 1;
420 obj->oo_compat_dotdot_created = 1;
422 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
423 GOTO(out, result = 0);
425 LASSERT(obj->oo_hl_head == NULL);
426 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
427 if (obj->oo_hl_head == NULL) {
428 obj->oo_inode = NULL;
430 GOTO(out, result = -ENOMEM);
432 GOTO(out, result = 0);
435 LINVRNT(osd_invariant(obj));
440 * Concurrency: shouldn't matter.
442 static void osd_object_init0(struct osd_object *obj)
444 LASSERT(obj->oo_inode != NULL);
445 obj->oo_dt.do_body_ops = &osd_body_ops;
446 obj->oo_dt.do_lu.lo_header->loh_attr |=
447 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
450 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
452 struct osd_thread_info *info = osd_oti_get(env);
453 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
457 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
458 rc = __osd_xattr_get(obj->oo_inode, &info->oti_obj_dentry,
459 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
463 lustre_lma_swab(lma);
464 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
465 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
467 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
468 "fid = "DFID", ino = %lu: rc = %d\n",
469 osd_obj2dev(obj)->od_svname,
470 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
471 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
472 obj->oo_inode->i_ino, rc);
474 if (unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu),
475 &lma->lma_self_fid))) {
476 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
477 osd_obj2dev(obj)->od_svname,
478 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
479 PFID(&lma->lma_self_fid));
480 if (obj->oo_inode != NULL) {
482 obj->oo_inode = NULL;
486 } else if (rc == -ENODATA) {
487 /* haven't initialize LMA xattr */
495 * Concurrency: no concurrent access is possible that early in object
498 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
499 const struct lu_object_conf *conf)
501 struct osd_object *obj = osd_obj(l);
504 LINVRNT(osd_invariant(obj));
506 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
507 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
508 l->lo_header->loh_attr |= LOHA_EXISTS;
512 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
513 obj->oo_dt.do_body_ops = &osd_body_ops_new;
514 if (result == 0 && obj->oo_inode != NULL) {
515 result = osd_check_lma(env, obj);
519 osd_object_init0(obj);
522 LINVRNT(osd_invariant(obj));
527 * Concurrency: no concurrent access is possible that late in object
530 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
532 struct osd_object *obj = osd_obj(l);
534 LINVRNT(osd_invariant(obj));
536 dt_object_fini(&obj->oo_dt);
537 if (obj->oo_hl_head != NULL)
538 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
543 * Concurrency: no concurrent access is possible that late in object
546 static void osd_index_fini(struct osd_object *o)
548 struct iam_container *bag;
550 if (o->oo_dir != NULL) {
551 bag = &o->oo_dir->od_container;
552 if (o->oo_inode != NULL) {
553 if (bag->ic_object == o->oo_inode)
554 iam_container_fini(bag);
556 OBD_FREE_PTR(o->oo_dir);
562 * Concurrency: no concurrent access is possible that late in object
563 * life-cycle (for all existing callers, that is. New callers have to provide
564 * their own locking.)
566 static int osd_inode_unlinked(const struct inode *inode)
568 return inode->i_nlink == 0;
572 OSD_TXN_OI_DELETE_CREDITS = 20,
573 OSD_TXN_INODE_DELETE_CREDITS = 20
580 #if OSD_THANDLE_STATS
582 * Set time when the handle is allocated
584 static void osd_th_alloced(struct osd_thandle *oth)
586 oth->oth_alloced = cfs_time_current();
590 * Set time when the handle started
592 static void osd_th_started(struct osd_thandle *oth)
594 oth->oth_started = cfs_time_current();
598 * Helper function to convert time interval to microseconds packed in
601 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
605 cfs_duration_usec(cfs_time_sub(end, start), &val);
606 return val.tv_sec * 1000000 + val.tv_usec;
610 * Check whether the we deal with this handle for too long.
612 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
613 cfs_time_t alloced, cfs_time_t started,
616 cfs_time_t now = cfs_time_current();
618 LASSERT(dev != NULL);
620 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
621 interval_to_usec(alloced, started));
622 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
623 interval_to_usec(started, closed));
624 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
625 interval_to_usec(closed, now));
627 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
628 CWARN("transaction handle %p was open for too long: "
630 "alloced "CFS_TIME_T" ,"
631 "started "CFS_TIME_T" ,"
632 "closed "CFS_TIME_T"\n",
633 oth, now, alloced, started, closed);
634 libcfs_debug_dumpstack(NULL);
638 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
640 cfs_time_t __closed = cfs_time_current(); \
641 cfs_time_t __alloced = oth->oth_alloced; \
642 cfs_time_t __started = oth->oth_started; \
645 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
648 #else /* OSD_THANDLE_STATS */
650 #define osd_th_alloced(h) do {} while(0)
651 #define osd_th_started(h) do {} while(0)
652 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
654 #endif /* OSD_THANDLE_STATS */
657 * Concurrency: doesn't access mutable data.
659 static int osd_param_is_not_sane(const struct osd_device *dev,
660 const struct thandle *th)
662 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
664 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
668 * Concurrency: shouldn't matter.
670 static void osd_trans_commit_cb(struct super_block *sb,
671 struct ldiskfs_journal_cb_entry *jcb, int error)
673 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
674 struct thandle *th = &oh->ot_super;
675 struct lu_device *lud = &th->th_dev->dd_lu_dev;
676 struct dt_txn_commit_cb *dcb, *tmp;
678 LASSERT(oh->ot_handle == NULL);
681 CERROR("transaction @0x%p commit error: %d\n", th, error);
683 dt_txn_hook_commit(th);
685 /* call per-transaction callbacks if any */
686 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
687 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
688 "commit callback entry: magic=%x name='%s'\n",
689 dcb->dcb_magic, dcb->dcb_name);
690 cfs_list_del_init(&dcb->dcb_linkage);
691 dcb->dcb_func(NULL, th, dcb, error);
694 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
698 lu_context_exit(&th->th_ctx);
699 lu_context_fini(&th->th_ctx);
703 static struct thandle *osd_trans_create(const struct lu_env *env,
706 struct osd_thread_info *oti = osd_oti_get(env);
707 struct osd_iobuf *iobuf = &oti->oti_iobuf;
708 struct osd_thandle *oh;
712 /* on pending IO in this thread should left from prev. request */
713 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
715 th = ERR_PTR(-ENOMEM);
716 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
718 oh->ot_quota_trans = &oti->oti_quota_trans;
719 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
723 th->th_tags = LCT_TX_HANDLE;
725 oti->oti_dev = osd_dt_dev(d);
726 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
729 memset(oti->oti_declare_ops, 0,
730 sizeof(oti->oti_declare_ops));
731 memset(oti->oti_declare_ops_rb, 0,
732 sizeof(oti->oti_declare_ops_rb));
733 memset(oti->oti_declare_ops_cred, 0,
734 sizeof(oti->oti_declare_ops_cred));
735 oti->oti_rollback = false;
741 * Concurrency: shouldn't matter.
743 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
746 struct osd_thread_info *oti = osd_oti_get(env);
747 struct osd_device *dev = osd_dt_dev(d);
749 struct osd_thandle *oh;
754 LASSERT(current->journal_info == NULL);
756 oh = container_of0(th, struct osd_thandle, ot_super);
758 LASSERT(oh->ot_handle == NULL);
760 rc = dt_txn_hook_start(env, d, th);
764 if (unlikely(osd_param_is_not_sane(dev, th))) {
765 static unsigned long last_printed;
766 static int last_credits;
768 CWARN("%.16s: too many transaction credits (%d > %d)\n",
769 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
771 osd_journal(dev)->j_max_transaction_buffers);
772 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
773 oti->oti_declare_ops[OSD_OT_CREATE],
774 oti->oti_declare_ops_cred[OSD_OT_CREATE],
775 oti->oti_declare_ops[OSD_OT_DELETE],
776 oti->oti_declare_ops_cred[OSD_OT_DELETE],
777 oti->oti_declare_ops[OSD_OT_DESTROY],
778 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
779 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
780 oti->oti_declare_ops[OSD_OT_ATTR_SET],
781 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
782 oti->oti_declare_ops[OSD_OT_XATTR_SET],
783 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
784 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
785 oti->oti_declare_ops[OSD_OT_WRITE],
786 oti->oti_declare_ops_cred[OSD_OT_WRITE],
787 oti->oti_declare_ops[OSD_OT_PUNCH],
788 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
789 oti->oti_declare_ops[OSD_OT_QUOTA],
790 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
791 CWARN(" insert: %u/%u, delete: %u/%u\n",
792 oti->oti_declare_ops[OSD_OT_INSERT],
793 oti->oti_declare_ops_cred[OSD_OT_INSERT],
794 oti->oti_declare_ops[OSD_OT_DESTROY],
795 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
796 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
797 oti->oti_declare_ops[OSD_OT_REF_ADD],
798 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
799 oti->oti_declare_ops[OSD_OT_REF_DEL],
800 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
802 if (last_credits != oh->ot_credits &&
803 time_after(jiffies, last_printed + 60 * HZ)) {
804 libcfs_debug_dumpstack(NULL);
805 last_credits = oh->ot_credits;
806 last_printed = jiffies;
808 /* XXX Limit the credits to 'max_transaction_buffers', and
809 * let the underlying filesystem to catch the error if
810 * we really need so many credits.
812 * This should be removed when we can calculate the
813 * credits precisely. */
814 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
818 * XXX temporary stuff. Some abstraction layer should
821 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
825 LASSERT(oti->oti_txns == 0);
826 lu_context_init(&th->th_ctx, th->th_tags);
827 lu_context_enter(&th->th_ctx);
829 lu_device_get(&d->dd_lu_dev);
830 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
842 * Concurrency: shouldn't matter.
844 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
847 struct osd_thandle *oh;
848 struct osd_thread_info *oti = osd_oti_get(env);
849 struct osd_iobuf *iobuf = &oti->oti_iobuf;
850 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
853 oh = container_of0(th, struct osd_thandle, ot_super);
856 /* inform the quota slave device that the transaction is
858 qsd_op_end(env, qsd, oh->ot_quota_trans);
859 oh->ot_quota_trans = NULL;
861 if (oh->ot_handle != NULL) {
862 handle_t *hdl = oh->ot_handle;
865 * add commit callback
866 * notice we don't do this in osd_trans_start()
867 * as underlying transaction can change during truncate
869 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
872 LASSERT(oti->oti_txns == 1);
874 rc = dt_txn_hook_stop(env, th);
876 CERROR("Failure in transaction hook: %d\n", rc);
878 /* hook functions might modify th_sync */
879 hdl->h_sync = th->th_sync;
881 oh->ot_handle = NULL;
882 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
883 rc = ldiskfs_journal_stop(hdl));
885 CERROR("Failure to stop transaction: %d\n", rc);
890 /* as we want IO to journal and data IO be concurrent, we don't block
891 * awaiting data IO completion in osd_do_bio(), instead we wait here
892 * once transaction is submitted to the journal. all reqular requests
893 * don't do direct IO (except read/write), thus this wait_event becomes
896 * IMPORTANT: we have to wait till any IO submited by the thread is
897 * completed otherwise iobuf may be corrupted by different request
899 cfs_wait_event(iobuf->dr_wait,
900 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
902 rc = iobuf->dr_error;
907 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
909 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
912 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
913 LASSERT(&dcb->dcb_func != NULL);
914 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
920 * Called just before object is freed. Releases all resources except for
921 * object itself (that is released by osd_object_free()).
923 * Concurrency: no concurrent access is possible that late in object
926 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
928 struct osd_object *obj = osd_obj(l);
929 struct inode *inode = obj->oo_inode;
931 LINVRNT(osd_invariant(obj));
934 * If object is unlinked remove fid->ino mapping from object index.
939 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
940 qid_t uid = inode->i_uid;
941 qid_t gid = inode->i_gid;
944 obj->oo_inode = NULL;
947 struct osd_thread_info *info = osd_oti_get(env);
948 struct lquota_id_info *qi = &info->oti_qi;
950 /* Release granted quota to master if necessary */
951 qi->lqi_id.qid_uid = uid;
952 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
954 qi->lqi_id.qid_uid = gid;
955 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
961 * Concurrency: ->loo_object_release() is called under site spin-lock.
963 static void osd_object_release(const struct lu_env *env,
969 * Concurrency: shouldn't matter.
971 static int osd_object_print(const struct lu_env *env, void *cookie,
972 lu_printer_t p, const struct lu_object *l)
974 struct osd_object *o = osd_obj(l);
977 if (o->oo_dir != NULL)
978 d = o->oo_dir->od_container.ic_descr;
981 return (*p)(env, cookie,
982 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
984 o->oo_inode ? o->oo_inode->i_ino : 0UL,
985 o->oo_inode ? o->oo_inode->i_generation : 0,
986 d ? d->id_ops->id_name : "plain");
990 * Concurrency: shouldn't matter.
992 int osd_statfs(const struct lu_env *env, struct dt_device *d,
993 struct obd_statfs *sfs)
995 struct osd_device *osd = osd_dt_dev(d);
996 struct super_block *sb = osd_sb(osd);
997 struct kstatfs *ksfs;
1000 if (unlikely(osd->od_mnt == NULL))
1001 return -EINPROGRESS;
1003 /* osd_lproc.c call this without env, allocate ksfs for that case */
1004 if (unlikely(env == NULL)) {
1005 OBD_ALLOC_PTR(ksfs);
1009 ksfs = &osd_oti_get(env)->oti_ksfs;
1012 spin_lock(&osd->od_osfs_lock);
1013 /* cache 1 second */
1014 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1015 result = sb->s_op->statfs(sb->s_root, ksfs);
1016 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1017 osd->od_osfs_age = cfs_time_current_64();
1018 statfs_pack(&osd->od_statfs, ksfs);
1019 if (sb->s_flags & MS_RDONLY)
1020 sfs->os_state = OS_STATE_READONLY;
1024 if (likely(result == 0))
1025 *sfs = osd->od_statfs;
1026 spin_unlock(&osd->od_osfs_lock);
1028 if (unlikely(env == NULL))
1035 * Estimate space needed for file creations. We assume the largest filename
1036 * which is 2^64 - 1, hence a filename of 20 chars.
1037 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1039 #ifdef __LDISKFS_DIR_REC_LEN
1040 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1042 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1046 * Concurrency: doesn't access mutable data.
1048 static void osd_conf_get(const struct lu_env *env,
1049 const struct dt_device *dev,
1050 struct dt_device_param *param)
1052 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1055 * XXX should be taken from not-yet-existing fs abstraction layer.
1057 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1058 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1059 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1060 param->ddp_block_shift = sb->s_blocksize_bits;
1061 param->ddp_mount_type = LDD_MT_LDISKFS;
1062 param->ddp_maxbytes = sb->s_maxbytes;
1063 /* Overhead estimate should be fairly accurate, so we really take a tiny
1064 * error margin which also avoids fragmenting the filesystem too much */
1065 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1066 /* inode are statically allocated, so per-inode space consumption
1067 * is the space consumed by the directory entry */
1068 param->ddp_inodespace = PER_OBJ_USAGE;
1069 /* per-fragment overhead to be used by the client code */
1070 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1071 param->ddp_mntopts = 0;
1072 if (test_opt(sb, XATTR_USER))
1073 param->ddp_mntopts |= MNTOPT_USERXATTR;
1074 if (test_opt(sb, POSIX_ACL))
1075 param->ddp_mntopts |= MNTOPT_ACL;
1077 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1078 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1079 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1082 param->ddp_max_ea_size = sb->s_blocksize;
1087 * Concurrency: shouldn't matter.
1089 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1091 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1092 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1096 * Start commit for OSD device.
1098 * An implementation of dt_commit_async method for OSD device.
1099 * Asychronously starts underlayng fs sync and thereby a transaction
1102 * \param env environment
1103 * \param d dt device
1105 * \see dt_device_operations
1107 static int osd_commit_async(const struct lu_env *env,
1108 struct dt_device *d)
1110 struct super_block *s = osd_sb(osd_dt_dev(d));
1113 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1114 RETURN(s->s_op->sync_fs(s, 0));
1118 * Concurrency: shouldn't matter.
1121 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1123 struct super_block *sb = osd_sb(osd_dt_dev(d));
1124 struct block_device *dev = sb->s_bdev;
1125 #ifdef HAVE_DEV_SET_RDONLY
1126 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1129 int rc = -EOPNOTSUPP;
1133 #ifdef HAVE_DEV_SET_RDONLY
1134 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1136 if (jdev && (jdev != dev)) {
1137 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1139 dev_set_rdonly(jdev);
1141 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1142 dev_set_rdonly(dev);
1144 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1145 osd_dt_dev(d)->od_svname, (long)dev, rc);
1151 * Concurrency: serialization provided by callers.
1153 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1154 int mode, unsigned long timeout, __u32 alg,
1155 struct lustre_capa_key *keys)
1157 struct osd_device *dev = osd_dt_dev(d);
1160 dev->od_fl_capa = mode;
1161 dev->od_capa_timeout = timeout;
1162 dev->od_capa_alg = alg;
1163 dev->od_capa_keys = keys;
1168 * Note: we do not count into QUOTA here.
1169 * If we mount with --data_journal we may need more.
1171 const int osd_dto_credits_noquota[DTO_NR] = {
1174 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1175 * SINGLEDATA_TRANS_BLOCKS(8)
1176 * XXX Note: maybe iam need more, since iam have more level than
1179 [DTO_INDEX_INSERT] = 16,
1180 [DTO_INDEX_DELETE] = 16,
1184 [DTO_INDEX_UPDATE] = 16,
1186 * Create a object. The same as create object in EXT3.
1187 * DATA_TRANS_BLOCKS(14) +
1188 * INDEX_EXTRA_BLOCKS(8) +
1189 * 3(inode bits, groups, GDT)
1191 [DTO_OBJECT_CREATE] = 25,
1193 * XXX: real credits to be fixed
1195 [DTO_OBJECT_DELETE] = 25,
1197 * Attr set credits (inode)
1199 [DTO_ATTR_SET_BASE] = 1,
1201 * Xattr set. The same as xattr of EXT3.
1202 * DATA_TRANS_BLOCKS(14)
1203 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1204 * are also counted in. Do not know why?
1206 [DTO_XATTR_SET] = 14,
1209 * credits for inode change during write.
1211 [DTO_WRITE_BASE] = 3,
1213 * credits for single block write.
1215 [DTO_WRITE_BLOCK] = 14,
1217 * Attr set credits for chown.
1218 * This is extra credits for setattr, and it is null without quota
1220 [DTO_ATTR_SET_CHOWN]= 0
1223 static const struct dt_device_operations osd_dt_ops = {
1224 .dt_root_get = osd_root_get,
1225 .dt_statfs = osd_statfs,
1226 .dt_trans_create = osd_trans_create,
1227 .dt_trans_start = osd_trans_start,
1228 .dt_trans_stop = osd_trans_stop,
1229 .dt_trans_cb_add = osd_trans_cb_add,
1230 .dt_conf_get = osd_conf_get,
1231 .dt_sync = osd_sync,
1233 .dt_commit_async = osd_commit_async,
1234 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1237 static void osd_object_read_lock(const struct lu_env *env,
1238 struct dt_object *dt, unsigned role)
1240 struct osd_object *obj = osd_dt_obj(dt);
1241 struct osd_thread_info *oti = osd_oti_get(env);
1243 LINVRNT(osd_invariant(obj));
1245 LASSERT(obj->oo_owner != env);
1246 down_read_nested(&obj->oo_sem, role);
1248 LASSERT(obj->oo_owner == NULL);
1252 static void osd_object_write_lock(const struct lu_env *env,
1253 struct dt_object *dt, unsigned role)
1255 struct osd_object *obj = osd_dt_obj(dt);
1256 struct osd_thread_info *oti = osd_oti_get(env);
1258 LINVRNT(osd_invariant(obj));
1260 LASSERT(obj->oo_owner != env);
1261 down_write_nested(&obj->oo_sem, role);
1263 LASSERT(obj->oo_owner == NULL);
1264 obj->oo_owner = env;
1268 static void osd_object_read_unlock(const struct lu_env *env,
1269 struct dt_object *dt)
1271 struct osd_object *obj = osd_dt_obj(dt);
1272 struct osd_thread_info *oti = osd_oti_get(env);
1274 LINVRNT(osd_invariant(obj));
1276 LASSERT(oti->oti_r_locks > 0);
1278 up_read(&obj->oo_sem);
1281 static void osd_object_write_unlock(const struct lu_env *env,
1282 struct dt_object *dt)
1284 struct osd_object *obj = osd_dt_obj(dt);
1285 struct osd_thread_info *oti = osd_oti_get(env);
1287 LINVRNT(osd_invariant(obj));
1289 LASSERT(obj->oo_owner == env);
1290 LASSERT(oti->oti_w_locks > 0);
1292 obj->oo_owner = NULL;
1293 up_write(&obj->oo_sem);
1296 static int osd_object_write_locked(const struct lu_env *env,
1297 struct dt_object *dt)
1299 struct osd_object *obj = osd_dt_obj(dt);
1301 LINVRNT(osd_invariant(obj));
1303 return obj->oo_owner == env;
1306 static int capa_is_sane(const struct lu_env *env,
1307 struct osd_device *dev,
1308 struct lustre_capa *capa,
1309 struct lustre_capa_key *keys)
1311 struct osd_thread_info *oti = osd_oti_get(env);
1312 struct lustre_capa *tcapa = &oti->oti_capa;
1313 struct obd_capa *oc;
1317 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1319 if (capa_is_expired(oc)) {
1320 DEBUG_CAPA(D_ERROR, capa, "expired");
1327 if (capa_is_expired_sec(capa)) {
1328 DEBUG_CAPA(D_ERROR, capa, "expired");
1332 spin_lock(&capa_lock);
1333 for (i = 0; i < 2; i++) {
1334 if (keys[i].lk_keyid == capa->lc_keyid) {
1335 oti->oti_capa_key = keys[i];
1339 spin_unlock(&capa_lock);
1342 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1346 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1350 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1351 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1355 oc = capa_add(dev->od_capa_hash, capa);
1361 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1362 struct lustre_capa *capa, __u64 opc)
1364 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1365 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1366 struct md_capainfo *ci;
1369 if (!dev->od_fl_capa)
1372 if (capa == BYPASS_CAPA)
1375 ci = md_capainfo(env);
1379 if (ci->mc_auth == LC_ID_NONE)
1383 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1387 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1388 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1393 if (!capa_opc_supported(capa, opc)) {
1394 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1398 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1399 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1406 static struct timespec *osd_inode_time(const struct lu_env *env,
1407 struct inode *inode, __u64 seconds)
1409 struct osd_thread_info *oti = osd_oti_get(env);
1410 struct timespec *t = &oti->oti_time;
1412 t->tv_sec = seconds;
1414 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1419 static void osd_inode_getattr(const struct lu_env *env,
1420 struct inode *inode, struct lu_attr *attr)
1422 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1423 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1424 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1427 attr->la_atime = LTIME_S(inode->i_atime);
1428 attr->la_mtime = LTIME_S(inode->i_mtime);
1429 attr->la_ctime = LTIME_S(inode->i_ctime);
1430 attr->la_mode = inode->i_mode;
1431 attr->la_size = i_size_read(inode);
1432 attr->la_blocks = inode->i_blocks;
1433 attr->la_uid = inode->i_uid;
1434 attr->la_gid = inode->i_gid;
1435 attr->la_flags = LDISKFS_I(inode)->i_flags;
1436 attr->la_nlink = inode->i_nlink;
1437 attr->la_rdev = inode->i_rdev;
1438 attr->la_blksize = 1 << inode->i_blkbits;
1439 attr->la_blkbits = inode->i_blkbits;
1442 static int osd_attr_get(const struct lu_env *env,
1443 struct dt_object *dt,
1444 struct lu_attr *attr,
1445 struct lustre_capa *capa)
1447 struct osd_object *obj = osd_dt_obj(dt);
1449 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1450 LINVRNT(osd_invariant(obj));
1452 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1455 spin_lock(&obj->oo_guard);
1456 osd_inode_getattr(env, obj->oo_inode, attr);
1457 spin_unlock(&obj->oo_guard);
1461 static int osd_declare_attr_set(const struct lu_env *env,
1462 struct dt_object *dt,
1463 const struct lu_attr *attr,
1464 struct thandle *handle)
1466 struct osd_thandle *oh;
1467 struct osd_object *obj;
1468 struct osd_thread_info *info = osd_oti_get(env);
1469 struct lquota_id_info *qi = &info->oti_qi;
1475 LASSERT(dt != NULL);
1476 LASSERT(handle != NULL);
1478 obj = osd_dt_obj(dt);
1479 LASSERT(osd_invariant(obj));
1481 oh = container_of0(handle, struct osd_thandle, ot_super);
1482 LASSERT(oh->ot_handle == NULL);
1484 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1485 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1487 if (attr == NULL || obj->oo_inode == NULL)
1490 bspace = obj->oo_inode->i_blocks;
1491 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1492 bspace = toqb(bspace);
1494 /* Changing ownership is always preformed by super user, it should not
1497 * We still need to call the osd_declare_qid() to calculate the journal
1498 * credits for updating quota accounting files and to trigger quota
1499 * space adjustment once the operation is completed.*/
1500 if ((attr->la_valid & LA_UID) != 0 &&
1501 attr->la_uid != obj->oo_inode->i_uid) {
1502 qi->lqi_type = USRQUOTA;
1504 /* inode accounting */
1505 qi->lqi_is_blk = false;
1507 /* one more inode for the new owner ... */
1508 qi->lqi_id.qid_uid = attr->la_uid;
1510 allocated = (attr->la_uid == 0) ? true : false;
1511 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1512 if (rc == -EDQUOT || rc == -EINPROGRESS)
1517 /* and one less inode for the current uid */
1518 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1520 rc = osd_declare_qid(env, oh, qi, true, NULL);
1521 if (rc == -EDQUOT || rc == -EINPROGRESS)
1526 /* block accounting */
1527 qi->lqi_is_blk = true;
1529 /* more blocks for the new owner ... */
1530 qi->lqi_id.qid_uid = attr->la_uid;
1531 qi->lqi_space = bspace;
1532 allocated = (attr->la_uid == 0) ? true : false;
1533 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1534 if (rc == -EDQUOT || rc == -EINPROGRESS)
1539 /* and finally less blocks for the current owner */
1540 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1541 qi->lqi_space = -bspace;
1542 rc = osd_declare_qid(env, oh, qi, true, NULL);
1543 if (rc == -EDQUOT || rc == -EINPROGRESS)
1549 if (attr->la_valid & LA_GID &&
1550 attr->la_gid != obj->oo_inode->i_gid) {
1551 qi->lqi_type = GRPQUOTA;
1553 /* inode accounting */
1554 qi->lqi_is_blk = false;
1556 /* one more inode for the new group owner ... */
1557 qi->lqi_id.qid_gid = attr->la_gid;
1559 allocated = (attr->la_gid == 0) ? true : false;
1560 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1561 if (rc == -EDQUOT || rc == -EINPROGRESS)
1566 /* and one less inode for the current gid */
1567 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1569 rc = osd_declare_qid(env, oh, qi, true, NULL);
1570 if (rc == -EDQUOT || rc == -EINPROGRESS)
1575 /* block accounting */
1576 qi->lqi_is_blk = true;
1578 /* more blocks for the new owner ... */
1579 qi->lqi_id.qid_gid = attr->la_gid;
1580 qi->lqi_space = bspace;
1581 allocated = (attr->la_gid == 0) ? true : false;
1582 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1583 if (rc == -EDQUOT || rc == -EINPROGRESS)
1588 /* and finally less blocks for the current owner */
1589 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1590 qi->lqi_space = -bspace;
1591 rc = osd_declare_qid(env, oh, qi, true, NULL);
1592 if (rc == -EDQUOT || rc == -EINPROGRESS)
1601 static int osd_inode_setattr(const struct lu_env *env,
1602 struct inode *inode, const struct lu_attr *attr)
1606 bits = attr->la_valid;
1608 if (bits & LA_ATIME)
1609 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1610 if (bits & LA_CTIME)
1611 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1612 if (bits & LA_MTIME)
1613 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1614 if (bits & LA_SIZE) {
1615 LDISKFS_I(inode)->i_disksize = attr->la_size;
1616 i_size_write(inode, attr->la_size);
1620 /* OSD should not change "i_blocks" which is used by quota.
1621 * "i_blocks" should be changed by ldiskfs only. */
1622 if (bits & LA_BLOCKS)
1623 inode->i_blocks = attr->la_blocks;
1626 inode->i_mode = (inode->i_mode & S_IFMT) |
1627 (attr->la_mode & ~S_IFMT);
1629 inode->i_uid = attr->la_uid;
1631 inode->i_gid = attr->la_gid;
1632 if (bits & LA_NLINK)
1633 set_nlink(inode, attr->la_nlink);
1635 inode->i_rdev = attr->la_rdev;
1637 if (bits & LA_FLAGS) {
1638 /* always keep S_NOCMTIME */
1639 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1645 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1647 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1648 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1653 if (attr->la_valid & LA_UID)
1654 iattr.ia_valid |= ATTR_UID;
1655 if (attr->la_valid & LA_GID)
1656 iattr.ia_valid |= ATTR_GID;
1657 iattr.ia_uid = attr->la_uid;
1658 iattr.ia_gid = attr->la_gid;
1660 rc = ll_vfs_dq_transfer(inode, &iattr);
1662 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1663 "enforcement enabled on the ldiskfs filesystem?",
1664 inode->i_sb->s_id, rc);
1671 static int osd_attr_set(const struct lu_env *env,
1672 struct dt_object *dt,
1673 const struct lu_attr *attr,
1674 struct thandle *handle,
1675 struct lustre_capa *capa)
1677 struct osd_object *obj = osd_dt_obj(dt);
1678 struct inode *inode;
1681 LASSERT(handle != NULL);
1682 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1683 LASSERT(osd_invariant(obj));
1685 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1688 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1690 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1691 struct osd_thread_info *oti = osd_oti_get(env);
1692 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1693 struct lu_fid *fid1 = &oti->oti_fid;
1694 struct osd_inode_id *id = &oti->oti_id;
1695 struct iam_path_descr *ipd;
1696 struct iam_container *bag;
1697 struct osd_thandle *oh;
1700 fid_cpu_to_be(fid1, fid0);
1701 memset(id, 1, sizeof(*id));
1702 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1703 fid0)->oi_dir.od_container;
1704 ipd = osd_idx_ipd_get(env, bag);
1705 if (unlikely(ipd == NULL))
1708 oh = container_of0(handle, struct osd_thandle, ot_super);
1709 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1710 (const struct iam_rec *)id, ipd);
1711 osd_ipd_put(env, bag, ipd);
1712 return(rc > 0 ? 0 : rc);
1715 inode = obj->oo_inode;
1716 ll_vfs_dq_init(inode);
1718 rc = osd_quota_transfer(inode, attr);
1722 spin_lock(&obj->oo_guard);
1723 rc = osd_inode_setattr(env, inode, attr);
1724 spin_unlock(&obj->oo_guard);
1727 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1731 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1732 struct osd_object *obj,
1733 const char *name, const int namelen)
1735 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1738 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1740 struct dt_allocation_hint *hint,
1744 struct osd_device *osd = osd_obj2dev(obj);
1745 struct osd_thandle *oth;
1746 struct dt_object *parent = NULL;
1747 struct inode *inode;
1749 LINVRNT(osd_invariant(obj));
1750 LASSERT(obj->oo_inode == NULL);
1751 LASSERT(obj->oo_hl_head == NULL);
1753 if (S_ISDIR(mode) && ldiskfs_pdo) {
1754 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1755 if (obj->oo_hl_head == NULL)
1759 oth = container_of(th, struct osd_thandle, ot_super);
1760 LASSERT(oth->ot_handle->h_transaction != NULL);
1762 if (hint && hint->dah_parent)
1763 parent = hint->dah_parent;
1765 inode = ldiskfs_create_inode(oth->ot_handle,
1766 parent ? osd_dt_obj(parent)->oo_inode :
1767 osd_sb(osd)->s_root->d_inode,
1769 if (!IS_ERR(inode)) {
1770 /* Do not update file c/mtime in ldiskfs.
1771 * NB: don't need any lock because no contention at this
1773 inode->i_flags |= S_NOCMTIME;
1775 /* For new created object, it must be consistent,
1776 * and it is unnecessary to scrub against it. */
1777 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1778 obj->oo_inode = inode;
1781 if (obj->oo_hl_head != NULL) {
1782 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1783 obj->oo_hl_head = NULL;
1785 result = PTR_ERR(inode);
1787 LINVRNT(osd_invariant(obj));
1795 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1796 struct lu_attr *attr,
1797 struct dt_allocation_hint *hint,
1798 struct dt_object_format *dof,
1802 struct osd_thandle *oth;
1803 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1805 LASSERT(S_ISDIR(attr->la_mode));
1807 oth = container_of(th, struct osd_thandle, ot_super);
1808 LASSERT(oth->ot_handle->h_transaction != NULL);
1809 result = osd_mkfile(info, obj, mode, hint, th);
1814 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1815 struct lu_attr *attr,
1816 struct dt_allocation_hint *hint,
1817 struct dt_object_format *dof,
1821 struct osd_thandle *oth;
1822 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1824 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1826 LASSERT(S_ISREG(attr->la_mode));
1828 oth = container_of(th, struct osd_thandle, ot_super);
1829 LASSERT(oth->ot_handle->h_transaction != NULL);
1831 result = osd_mkfile(info, obj, mode, hint, th);
1833 LASSERT(obj->oo_inode != NULL);
1834 if (feat->dif_flags & DT_IND_VARKEY)
1835 result = iam_lvar_create(obj->oo_inode,
1836 feat->dif_keysize_max,
1838 feat->dif_recsize_max,
1841 result = iam_lfix_create(obj->oo_inode,
1842 feat->dif_keysize_max,
1844 feat->dif_recsize_max,
1851 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1852 struct lu_attr *attr,
1853 struct dt_allocation_hint *hint,
1854 struct dt_object_format *dof,
1857 LASSERT(S_ISREG(attr->la_mode));
1858 return osd_mkfile(info, obj, (attr->la_mode &
1859 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1862 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1863 struct lu_attr *attr,
1864 struct dt_allocation_hint *hint,
1865 struct dt_object_format *dof,
1868 LASSERT(S_ISLNK(attr->la_mode));
1869 return osd_mkfile(info, obj, (attr->la_mode &
1870 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1873 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1874 struct lu_attr *attr,
1875 struct dt_allocation_hint *hint,
1876 struct dt_object_format *dof,
1879 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1882 LINVRNT(osd_invariant(obj));
1883 LASSERT(obj->oo_inode == NULL);
1884 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1885 S_ISFIFO(mode) || S_ISSOCK(mode));
1887 result = osd_mkfile(info, obj, mode, hint, th);
1889 LASSERT(obj->oo_inode != NULL);
1891 * This inode should be marked dirty for i_rdev. Currently
1892 * that is done in the osd_attr_init().
1894 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1897 LINVRNT(osd_invariant(obj));
1901 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1903 struct dt_allocation_hint *hint,
1904 struct dt_object_format *dof,
1907 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1909 osd_obj_type_f result;
1925 result = osd_mk_index;
1936 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1937 struct dt_object *parent, struct dt_object *child,
1938 cfs_umode_t child_mode)
1942 memset(ah, 0, sizeof(*ah));
1943 ah->dah_parent = parent;
1944 ah->dah_mode = child_mode;
1947 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1948 struct lu_attr *attr, struct dt_object_format *dof)
1950 struct inode *inode = obj->oo_inode;
1951 __u64 valid = attr->la_valid;
1954 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1956 if (dof->dof_type != DFT_NODE)
1957 attr->la_valid &= ~LA_RDEV;
1958 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1959 attr->la_valid &= ~LA_ATIME;
1960 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1961 attr->la_valid &= ~LA_CTIME;
1962 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1963 attr->la_valid &= ~LA_MTIME;
1965 result = osd_quota_transfer(inode, attr);
1969 if (attr->la_valid != 0) {
1970 result = osd_inode_setattr(info->oti_env, inode, attr);
1972 * The osd_inode_setattr() should always succeed here. The
1973 * only error that could be returned is EDQUOT when we are
1974 * trying to change the UID or GID of the inode. However, this
1975 * should not happen since quota enforcement is no longer
1976 * enabled on ldiskfs (lquota takes care of it).
1978 LASSERTF(result == 0, "%d", result);
1979 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1982 attr->la_valid = valid;
1986 * Helper function for osd_object_create()
1988 * \retval 0, on success
1990 static int __osd_object_create(struct osd_thread_info *info,
1991 struct osd_object *obj, struct lu_attr *attr,
1992 struct dt_allocation_hint *hint,
1993 struct dt_object_format *dof,
1999 /* we drop umask so that permissions we pass are not affected */
2000 umask = current->fs->umask;
2001 current->fs->umask = 0;
2003 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2006 osd_attr_init(info, obj, attr, dof);
2007 osd_object_init0(obj);
2009 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2010 unlock_new_inode(obj->oo_inode);
2013 /* restore previous umask value */
2014 current->fs->umask = umask;
2020 * Helper function for osd_object_create()
2022 * \retval 0, on success
2024 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2025 const struct lu_fid *fid, struct thandle *th)
2027 struct osd_thread_info *info = osd_oti_get(env);
2028 struct osd_inode_id *id = &info->oti_id;
2029 struct osd_device *osd = osd_obj2dev(obj);
2031 LASSERT(obj->oo_inode != NULL);
2033 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2034 return osd_oi_insert(info, osd, fid, id, th);
2037 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2038 const struct lu_fid *fid, struct lu_seq_range *range)
2040 struct seq_server_site *ss = osd_seq_site(osd);
2043 if (fid_is_idif(fid)) {
2044 fld_range_set_ost(range);
2045 range->lsr_index = fid_idif_ost_idx(fid);
2049 if (!fid_seq_in_fldb(fid_seq(fid))) {
2050 fld_range_set_mdt(range);
2052 /* FIXME: If ss is NULL, it suppose not get lsr_index
2054 range->lsr_index = ss->ss_node_id;
2058 LASSERT(ss != NULL);
2059 fld_range_set_any(range);
2060 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2062 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
2063 osd_name(osd), PFID(fid), rc);
2069 * Concurrency: no external locking is necessary.
2071 static int osd_declare_object_create(const struct lu_env *env,
2072 struct dt_object *dt,
2073 struct lu_attr *attr,
2074 struct dt_allocation_hint *hint,
2075 struct dt_object_format *dof,
2076 struct thandle *handle)
2078 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2079 struct osd_thandle *oh;
2083 LASSERT(handle != NULL);
2085 oh = container_of0(handle, struct osd_thandle, ot_super);
2086 LASSERT(oh->ot_handle == NULL);
2088 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2089 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2090 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2091 lu_object_fid(&dt->do_lu)))
2092 /* Reuse idle OI block may cause additional one OI block
2094 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2095 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2097 /* If this is directory, then we expect . and .. to be inserted as
2098 * well. The one directory block always needs to be created for the
2099 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2100 * block), there is no danger of needing a tree for the first block.
2102 if (attr && S_ISDIR(attr->la_mode)) {
2103 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2104 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2105 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2111 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2112 false, false, NULL, false);
2116 /* It does fld look up inside declare, and the result will be
2117 * added to fld cache, so the following fld lookup inside insert
2118 * does not need send RPC anymore, so avoid send rpc with holding
2120 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2121 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2122 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2123 lu_object_fid(&dt->do_lu), range);
2129 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2130 struct lu_attr *attr,
2131 struct dt_allocation_hint *hint,
2132 struct dt_object_format *dof,
2135 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2136 struct osd_object *obj = osd_dt_obj(dt);
2137 struct osd_thread_info *info = osd_oti_get(env);
2142 LINVRNT(osd_invariant(obj));
2143 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2144 LASSERT(osd_write_locked(env, obj));
2145 LASSERT(th != NULL);
2147 if (unlikely(fid_is_acct(fid)))
2148 /* Quota files can't be created from the kernel any more,
2149 * 'tune2fs -O quota' will take care of creating them */
2152 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2153 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2155 result = __osd_object_create(info, obj, attr, hint, dof, th);
2157 result = __osd_oi_insert(env, obj, fid, th);
2159 LASSERT(ergo(result == 0,
2160 dt_object_exists(dt) && !dt_object_remote(dt)));
2162 LASSERT(osd_invariant(obj));
2167 * Called to destroy on-disk representation of the object
2169 * Concurrency: must be locked
2171 static int osd_declare_object_destroy(const struct lu_env *env,
2172 struct dt_object *dt,
2175 struct osd_object *obj = osd_dt_obj(dt);
2176 struct inode *inode = obj->oo_inode;
2177 struct osd_thandle *oh;
2181 oh = container_of0(th, struct osd_thandle, ot_super);
2182 LASSERT(oh->ot_handle == NULL);
2185 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2186 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2187 /* Recycle idle OI leaf may cause additional three OI blocks
2189 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2190 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2191 /* one less inode */
2192 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2193 false, true, NULL, false);
2196 /* data to be truncated */
2197 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2198 true, true, NULL, false);
2202 static int osd_object_destroy(const struct lu_env *env,
2203 struct dt_object *dt,
2206 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2207 struct osd_object *obj = osd_dt_obj(dt);
2208 struct inode *inode = obj->oo_inode;
2209 struct osd_device *osd = osd_obj2dev(obj);
2210 struct osd_thandle *oh;
2214 oh = container_of0(th, struct osd_thandle, ot_super);
2215 LASSERT(oh->ot_handle);
2217 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2219 if (unlikely(fid_is_acct(fid)))
2222 if (S_ISDIR(inode->i_mode)) {
2223 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2224 /* it will check/delete the inode from remote parent,
2225 * how to optimize it? unlink performance impaction XXX */
2226 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2227 if (result != 0 && result != -ENOENT) {
2228 CERROR("%s: delete inode "DFID": rc = %d\n",
2229 osd_name(osd), PFID(fid), result);
2231 spin_lock(&obj->oo_guard);
2233 spin_unlock(&obj->oo_guard);
2234 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2237 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2239 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2241 /* XXX: add to ext3 orphan list */
2242 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2244 /* not needed in the cache anymore */
2245 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2251 * Put the fid into lustre_mdt_attrs, and then place the structure
2252 * inode's ea. This fid should not be altered during the life time
2255 * \retval +ve, on success
2256 * \retval -ve, on error
2258 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2260 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2261 const struct lu_fid *fid, __u64 flags)
2263 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2266 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2269 lustre_lma_init(lma, fid, flags);
2270 lustre_lma_swab(lma);
2272 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2274 /* Someone may created the EA by race. */
2275 if (unlikely(rc == -EEXIST))
2281 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2282 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2283 * To have compatilibility with 1.8 ldiskfs driver we need to have
2284 * magic number at start of fid data.
2285 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2288 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2289 const struct dt_rec *fid)
2291 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2292 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2293 param->edp_magic = 0;
2297 param->edp_magic = LDISKFS_LUFID_MAGIC;
2298 param->edp_len = sizeof(struct lu_fid) + 1;
2299 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2303 * Try to read the fid from inode ea into dt_rec.
2305 * \param fid object fid.
2307 * \retval 0 on success
2309 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2310 __u32 ino, struct lu_fid *fid,
2311 struct osd_inode_id *id)
2313 struct osd_thread_info *info = osd_oti_get(env);
2314 struct inode *inode;
2317 osd_id_gen(id, ino, OSD_OII_NOGEN);
2318 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2320 RETURN(PTR_ERR(inode));
2326 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2328 struct inode *parent_dir,
2329 const struct dt_rec *dot_fid,
2330 const struct dt_rec *dot_dot_fid,
2331 struct osd_thandle *oth)
2333 struct ldiskfs_dentry_param *dot_ldp;
2334 struct ldiskfs_dentry_param *dot_dot_ldp;
2336 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2337 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2339 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2340 dot_ldp->edp_magic = 0;
2341 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2342 dir, dot_ldp, dot_dot_ldp);
2346 * Create an local agent inode for remote entry
2348 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2349 struct osd_device *osd,
2350 struct osd_object *pobj,
2351 const struct lu_fid *fid,
2354 struct osd_thread_info *info = osd_oti_get(env);
2355 struct inode *local;
2356 struct osd_thandle *oh;
2361 oh = container_of(th, struct osd_thandle, ot_super);
2362 LASSERT(oh->ot_handle->h_transaction != NULL);
2364 /* FIXME: Insert index api needs to know the mode of
2365 * the remote object. Just use S_IFDIR for now */
2366 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2367 if (IS_ERR(local)) {
2368 CERROR("%s: create local error %d\n", osd_name(osd),
2369 (int)PTR_ERR(local));
2373 /* Set special LMA flag for local agent inode */
2374 rc = osd_ea_fid_set(info, local, fid, LMAI_AGENT);
2376 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2377 osd_name(osd), PFID(fid), rc);
2378 RETURN(ERR_PTR(rc));
2381 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2382 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2383 (const struct dt_rec *)fid, oh);
2385 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2386 osd_name(osd), PFID(fid), rc);
2387 RETURN(ERR_PTR(rc));
2394 * Delete local agent inode for remote entry
2396 static int osd_delete_local_agent_inode(const struct lu_env *env,
2397 struct osd_device *osd,
2398 const struct lu_fid *fid,
2399 __u32 ino, struct osd_thandle *oh)
2401 struct osd_thread_info *oti = osd_oti_get(env);
2402 struct osd_inode_id *id = &oti->oti_id;
2403 struct inode *inode;
2406 id->oii_ino = le32_to_cpu(ino);
2407 id->oii_gen = OSD_OII_NOGEN;
2408 inode = osd_iget(oti, osd, id);
2409 if (IS_ERR(inode)) {
2410 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2411 PFID(fid), id->oii_ino, id->oii_gen);
2412 RETURN(PTR_ERR(inode));
2416 mark_inode_dirty(inode);
2417 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2418 osd_name(osd), PFID(fid), inode->i_ino);
2424 * OSD layer object create function for interoperability mode (b11826).
2425 * This is mostly similar to osd_object_create(). Only difference being, fid is
2426 * inserted into inode ea here.
2428 * \retval 0, on success
2429 * \retval -ve, on error
2431 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2432 struct lu_attr *attr,
2433 struct dt_allocation_hint *hint,
2434 struct dt_object_format *dof,
2437 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2438 struct osd_object *obj = osd_dt_obj(dt);
2439 struct osd_thread_info *info = osd_oti_get(env);
2444 LASSERT(osd_invariant(obj));
2445 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2446 LASSERT(osd_write_locked(env, obj));
2447 LASSERT(th != NULL);
2449 if (unlikely(fid_is_acct(fid)))
2450 /* Quota files can't be created from the kernel any more,
2451 * 'tune2fs -O quota' will take care of creating them */
2454 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2455 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2457 result = __osd_object_create(info, obj, attr, hint, dof, th);
2459 result = osd_ea_fid_set(info, obj->oo_inode, fid, 0);
2462 result = __osd_oi_insert(env, obj, fid, th);
2464 LASSERT(ergo(result == 0,
2465 dt_object_exists(dt) && !dt_object_remote(dt)));
2466 LINVRNT(osd_invariant(obj));
2470 static int osd_declare_object_ref_add(const struct lu_env *env,
2471 struct dt_object *dt,
2472 struct thandle *handle)
2474 struct osd_thandle *oh;
2476 /* it's possible that object doesn't exist yet */
2477 LASSERT(handle != NULL);
2479 oh = container_of0(handle, struct osd_thandle, ot_super);
2480 LASSERT(oh->ot_handle == NULL);
2482 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2483 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2489 * Concurrency: @dt is write locked.
2491 static int osd_object_ref_add(const struct lu_env *env,
2492 struct dt_object *dt, struct thandle *th)
2494 struct osd_object *obj = osd_dt_obj(dt);
2495 struct inode *inode = obj->oo_inode;
2496 bool need_dirty = false;
2499 LINVRNT(osd_invariant(obj));
2500 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2501 LASSERT(osd_write_locked(env, obj));
2502 LASSERT(th != NULL);
2504 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2506 /* This based on ldiskfs_inc_count(), which is not exported.
2508 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2509 * (65000) subdirectories by storing "1" in i_nlink if the link count
2510 * would otherwise overflow. Directory tranversal tools understand
2511 * that (st_nlink == 1) indicates that the filesystem dose not track
2512 * hard links count on the directory, and will not abort subdirectory
2513 * scanning early once (st_nlink - 2) subdirs have been found.
2515 * This also has to properly handle the case of inodes with nlink == 0
2516 * in case they are being linked into the PENDING directory
2518 spin_lock(&obj->oo_guard);
2519 if (unlikely(!S_ISDIR(inode->i_mode) &&
2520 inode->i_nlink >= LDISKFS_LINK_MAX)) {
2521 /* MDD should have checked this, but good to be safe */
2523 } else if (unlikely(inode->i_nlink == 0 ||
2524 (S_ISDIR(inode->i_mode) &&
2525 inode->i_nlink >= LDISKFS_LINK_MAX))) {
2526 /* inc_nlink from 0 may cause WARN_ON */
2527 set_nlink(inode, 1);
2529 } else if (!S_ISDIR(inode->i_mode) ||
2530 (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2533 } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2535 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2536 spin_unlock(&obj->oo_guard);
2539 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2541 LINVRNT(osd_invariant(obj));
2546 static int osd_declare_object_ref_del(const struct lu_env *env,
2547 struct dt_object *dt,
2548 struct thandle *handle)
2550 struct osd_thandle *oh;
2552 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2553 LASSERT(handle != NULL);
2555 oh = container_of0(handle, struct osd_thandle, ot_super);
2556 LASSERT(oh->ot_handle == NULL);
2558 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2559 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2565 * Concurrency: @dt is write locked.
2567 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2570 struct osd_object *obj = osd_dt_obj(dt);
2571 struct inode *inode = obj->oo_inode;
2573 LINVRNT(osd_invariant(obj));
2574 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2575 LASSERT(osd_write_locked(env, obj));
2576 LASSERT(th != NULL);
2578 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2580 spin_lock(&obj->oo_guard);
2581 LASSERT(inode->i_nlink > 0);
2583 /* This based on ldiskfs_dec_count(), which is not exported.
2585 * If a directory already has nlink == 1, then do not drop the nlink
2586 * count to 0, even temporarily, to avoid race conditions with other
2587 * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2589 * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2591 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2594 spin_unlock(&obj->oo_guard);
2595 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2596 LINVRNT(osd_invariant(obj));
2598 spin_unlock(&obj->oo_guard);
2605 * Get the 64-bit version for an inode.
2607 static int osd_object_version_get(const struct lu_env *env,
2608 struct dt_object *dt, dt_obj_version_t *ver)
2610 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2612 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2613 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2614 *ver = LDISKFS_I(inode)->i_fs_version;
2619 * Concurrency: @dt is read locked.
2621 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2622 struct lu_buf *buf, const char *name,
2623 struct lustre_capa *capa)
2625 struct osd_object *obj = osd_dt_obj(dt);
2626 struct inode *inode = obj->oo_inode;
2627 struct osd_thread_info *info = osd_oti_get(env);
2628 struct dentry *dentry = &info->oti_obj_dentry;
2630 /* version get is not real XATTR but uses xattr API */
2631 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2632 /* for version we are just using xattr API but change inode
2634 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2635 osd_object_version_get(env, dt, buf->lb_buf);
2636 return sizeof(dt_obj_version_t);
2639 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2640 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2642 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2645 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2649 static int osd_declare_xattr_set(const struct lu_env *env,
2650 struct dt_object *dt,
2651 const struct lu_buf *buf, const char *name,
2652 int fl, struct thandle *handle)
2654 struct osd_thandle *oh;
2656 LASSERT(handle != NULL);
2658 oh = container_of0(handle, struct osd_thandle, ot_super);
2659 LASSERT(oh->ot_handle == NULL);
2661 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2662 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2663 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2664 osd_dto_credits_noquota[DTO_XATTR_SET]);
2670 * Set the 64-bit version for object
2672 static void osd_object_version_set(const struct lu_env *env,
2673 struct dt_object *dt,
2674 dt_obj_version_t *new_version)
2676 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2678 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2679 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2681 LDISKFS_I(inode)->i_fs_version = *new_version;
2682 /** Version is set after all inode operations are finished,
2683 * so we should mark it dirty here */
2684 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2688 * Concurrency: @dt is write locked.
2690 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2691 const struct lu_buf *buf, const char *name, int fl,
2692 struct thandle *handle, struct lustre_capa *capa)
2694 struct osd_object *obj = osd_dt_obj(dt);
2695 struct inode *inode = obj->oo_inode;
2696 struct osd_thread_info *info = osd_oti_get(env);
2700 LASSERT(handle != NULL);
2702 /* version set is not real XATTR */
2703 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2704 /* for version we are just using xattr API but change inode
2706 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2707 osd_object_version_set(env, dt, buf->lb_buf);
2708 return sizeof(dt_obj_version_t);
2711 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2714 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2715 if (fl & LU_XATTR_REPLACE)
2716 fs_flags |= XATTR_REPLACE;
2718 if (fl & LU_XATTR_CREATE)
2719 fs_flags |= XATTR_CREATE;
2721 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2726 * Concurrency: @dt is read locked.
2728 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2729 struct lu_buf *buf, struct lustre_capa *capa)
2731 struct osd_object *obj = osd_dt_obj(dt);
2732 struct inode *inode = obj->oo_inode;
2733 struct osd_thread_info *info = osd_oti_get(env);
2734 struct dentry *dentry = &info->oti_obj_dentry;
2736 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2737 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2738 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2740 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2743 dentry->d_inode = inode;
2744 dentry->d_sb = inode->i_sb;
2745 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2748 static int osd_declare_xattr_del(const struct lu_env *env,
2749 struct dt_object *dt, const char *name,
2750 struct thandle *handle)
2752 struct osd_thandle *oh;
2754 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2755 LASSERT(handle != NULL);
2757 oh = container_of0(handle, struct osd_thandle, ot_super);
2758 LASSERT(oh->ot_handle == NULL);
2760 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2761 osd_dto_credits_noquota[DTO_XATTR_SET]);
2767 * Concurrency: @dt is write locked.
2769 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2770 const char *name, struct thandle *handle,
2771 struct lustre_capa *capa)
2773 struct osd_object *obj = osd_dt_obj(dt);
2774 struct inode *inode = obj->oo_inode;
2775 struct osd_thread_info *info = osd_oti_get(env);
2776 struct dentry *dentry = &info->oti_obj_dentry;
2779 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2780 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2781 LASSERT(handle != NULL);
2783 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2786 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2788 ll_vfs_dq_init(inode);
2789 dentry->d_inode = inode;
2790 dentry->d_sb = inode->i_sb;
2791 rc = inode->i_op->removexattr(dentry, name);
2795 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2796 struct dt_object *dt,
2797 struct lustre_capa *old,
2800 struct osd_thread_info *info = osd_oti_get(env);
2801 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2802 struct osd_object *obj = osd_dt_obj(dt);
2803 struct osd_device *dev = osd_obj2dev(obj);
2804 struct lustre_capa_key *key = &info->oti_capa_key;
2805 struct lustre_capa *capa = &info->oti_capa;
2806 struct obd_capa *oc;
2807 struct md_capainfo *ci;
2811 if (!dev->od_fl_capa)
2812 RETURN(ERR_PTR(-ENOENT));
2814 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2815 LINVRNT(osd_invariant(obj));
2817 /* renewal sanity check */
2818 if (old && osd_object_auth(env, dt, old, opc))
2819 RETURN(ERR_PTR(-EACCES));
2821 ci = md_capainfo(env);
2823 RETURN(ERR_PTR(-ENOENT));
2825 switch (ci->mc_auth) {
2829 capa->lc_uid = obj->oo_inode->i_uid;
2830 capa->lc_gid = obj->oo_inode->i_gid;
2831 capa->lc_flags = LC_ID_PLAIN;
2833 case LC_ID_CONVERT: {
2836 s[0] = obj->oo_inode->i_uid;
2837 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2838 s[2] = obj->oo_inode->i_gid;
2839 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2840 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2842 RETURN(ERR_PTR(rc));
2844 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2845 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2846 capa->lc_flags = LC_ID_CONVERT;
2850 RETURN(ERR_PTR(-EINVAL));
2853 capa->lc_fid = *fid;
2855 capa->lc_flags |= dev->od_capa_alg << 24;
2856 capa->lc_timeout = dev->od_capa_timeout;
2857 capa->lc_expiry = 0;
2859 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2861 LASSERT(!capa_is_expired(oc));
2865 spin_lock(&capa_lock);
2866 *key = dev->od_capa_keys[1];
2867 spin_unlock(&capa_lock);
2869 capa->lc_keyid = key->lk_keyid;
2870 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2872 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2874 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2875 RETURN(ERR_PTR(rc));
2878 oc = capa_add(dev->od_capa_hash, capa);
2882 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2884 struct osd_object *obj = osd_dt_obj(dt);
2885 struct inode *inode = obj->oo_inode;
2886 struct osd_thread_info *info = osd_oti_get(env);
2887 struct dentry *dentry = &info->oti_obj_dentry;
2888 struct file *file = &info->oti_file;
2893 dentry->d_inode = inode;
2894 dentry->d_sb = inode->i_sb;
2895 file->f_dentry = dentry;
2896 file->f_mapping = inode->i_mapping;
2897 file->f_op = inode->i_fop;
2898 #ifndef HAVE_FILE_FSYNC_4ARGS
2899 mutex_lock(&inode->i_mutex);
2901 rc = do_fsync(file, 0);
2902 #ifndef HAVE_FILE_FSYNC_4ARGS
2903 mutex_unlock(&inode->i_mutex);
2908 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2911 struct osd_object *obj = osd_dt_obj(dt);
2914 *data = (void *)obj->oo_inode;
2922 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2923 const struct dt_index_features *feat)
2925 struct iam_descr *descr;
2927 if (osd_object_is_root(o))
2928 return feat == &dt_directory_features;
2930 LASSERT(o->oo_dir != NULL);
2932 descr = o->oo_dir->od_container.ic_descr;
2933 if (feat == &dt_directory_features) {
2934 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2940 feat->dif_keysize_min <= descr->id_key_size &&
2941 descr->id_key_size <= feat->dif_keysize_max &&
2942 feat->dif_recsize_min <= descr->id_rec_size &&
2943 descr->id_rec_size <= feat->dif_recsize_max &&
2944 !(feat->dif_flags & (DT_IND_VARKEY |
2945 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2946 ergo(feat->dif_flags & DT_IND_UPDATE,
2947 1 /* XXX check that object (and file system) is
2952 static int osd_iam_container_init(const struct lu_env *env,
2953 struct osd_object *obj,
2954 struct osd_directory *dir)
2956 struct iam_container *bag = &dir->od_container;
2959 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2963 result = iam_container_setup(bag);
2965 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2967 iam_container_fini(bag);
2974 * Concurrency: no external locking is necessary.
2976 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2977 const struct dt_index_features *feat)
2981 struct osd_object *obj = osd_dt_obj(dt);
2983 LINVRNT(osd_invariant(obj));
2985 if (osd_object_is_root(obj)) {
2986 dt->do_index_ops = &osd_index_ea_ops;
2988 } else if (feat == &dt_directory_features) {
2989 dt->do_index_ops = &osd_index_ea_ops;
2990 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2995 } else if (unlikely(feat == &dt_otable_features)) {
2996 dt->do_index_ops = &osd_otable_ops;
2998 } else if (unlikely(feat == &dt_acct_features)) {
2999 dt->do_index_ops = &osd_acct_index_ops;
3002 } else if (!osd_has_index(obj)) {
3003 struct osd_directory *dir;
3008 spin_lock(&obj->oo_guard);
3009 if (obj->oo_dir == NULL)
3013 * Concurrent thread allocated container data.
3016 spin_unlock(&obj->oo_guard);
3018 * Now, that we have container data, serialize its
3021 down_write(&obj->oo_ext_idx_sem);
3023 * recheck under lock.
3025 if (!osd_has_index(obj))
3026 result = osd_iam_container_init(env, obj, dir);
3029 up_write(&obj->oo_ext_idx_sem);
3037 if (result == 0 && skip_iam == 0) {
3038 if (!osd_iam_index_probe(env, obj, feat))
3041 LINVRNT(osd_invariant(obj));
3043 if (result == 0 && is_quota_glb_feat(feat) &&
3044 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3045 result = osd_quota_migration(env, dt, feat);
3050 static int osd_otable_it_attr_get(const struct lu_env *env,
3051 struct dt_object *dt,
3052 struct lu_attr *attr,
3053 struct lustre_capa *capa)
3059 static const struct dt_object_operations osd_obj_ops = {
3060 .do_read_lock = osd_object_read_lock,
3061 .do_write_lock = osd_object_write_lock,
3062 .do_read_unlock = osd_object_read_unlock,
3063 .do_write_unlock = osd_object_write_unlock,
3064 .do_write_locked = osd_object_write_locked,
3065 .do_attr_get = osd_attr_get,
3066 .do_declare_attr_set = osd_declare_attr_set,
3067 .do_attr_set = osd_attr_set,
3068 .do_ah_init = osd_ah_init,
3069 .do_declare_create = osd_declare_object_create,
3070 .do_create = osd_object_create,
3071 .do_declare_destroy = osd_declare_object_destroy,
3072 .do_destroy = osd_object_destroy,
3073 .do_index_try = osd_index_try,
3074 .do_declare_ref_add = osd_declare_object_ref_add,
3075 .do_ref_add = osd_object_ref_add,
3076 .do_declare_ref_del = osd_declare_object_ref_del,
3077 .do_ref_del = osd_object_ref_del,
3078 .do_xattr_get = osd_xattr_get,
3079 .do_declare_xattr_set = osd_declare_xattr_set,
3080 .do_xattr_set = osd_xattr_set,
3081 .do_declare_xattr_del = osd_declare_xattr_del,
3082 .do_xattr_del = osd_xattr_del,
3083 .do_xattr_list = osd_xattr_list,
3084 .do_capa_get = osd_capa_get,
3085 .do_object_sync = osd_object_sync,
3086 .do_data_get = osd_data_get,
3090 * dt_object_operations for interoperability mode
3091 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3093 static const struct dt_object_operations osd_obj_ea_ops = {
3094 .do_read_lock = osd_object_read_lock,
3095 .do_write_lock = osd_object_write_lock,
3096 .do_read_unlock = osd_object_read_unlock,
3097 .do_write_unlock = osd_object_write_unlock,
3098 .do_write_locked = osd_object_write_locked,
3099 .do_attr_get = osd_attr_get,
3100 .do_declare_attr_set = osd_declare_attr_set,
3101 .do_attr_set = osd_attr_set,
3102 .do_ah_init = osd_ah_init,
3103 .do_declare_create = osd_declare_object_create,
3104 .do_create = osd_object_ea_create,
3105 .do_declare_destroy = osd_declare_object_destroy,
3106 .do_destroy = osd_object_destroy,
3107 .do_index_try = osd_index_try,
3108 .do_declare_ref_add = osd_declare_object_ref_add,
3109 .do_ref_add = osd_object_ref_add,
3110 .do_declare_ref_del = osd_declare_object_ref_del,
3111 .do_ref_del = osd_object_ref_del,
3112 .do_xattr_get = osd_xattr_get,
3113 .do_declare_xattr_set = osd_declare_xattr_set,
3114 .do_xattr_set = osd_xattr_set,
3115 .do_declare_xattr_del = osd_declare_xattr_del,
3116 .do_xattr_del = osd_xattr_del,
3117 .do_xattr_list = osd_xattr_list,
3118 .do_capa_get = osd_capa_get,
3119 .do_object_sync = osd_object_sync,
3120 .do_data_get = osd_data_get,
3123 static const struct dt_object_operations osd_obj_otable_it_ops = {
3124 .do_attr_get = osd_otable_it_attr_get,
3125 .do_index_try = osd_index_try,
3128 static int osd_index_declare_iam_delete(const struct lu_env *env,
3129 struct dt_object *dt,
3130 const struct dt_key *key,
3131 struct thandle *handle)
3133 struct osd_thandle *oh;
3135 oh = container_of0(handle, struct osd_thandle, ot_super);
3136 LASSERT(oh->ot_handle == NULL);
3138 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3139 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3145 * delete a (key, value) pair from index \a dt specified by \a key
3147 * \param dt osd index object
3148 * \param key key for index
3149 * \param rec record reference
3150 * \param handle transaction handler
3153 * \retval -ve failure
3156 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3157 const struct dt_key *key,
3158 struct thandle *handle,
3159 struct lustre_capa *capa)
3161 struct osd_thread_info *oti = osd_oti_get(env);
3162 struct osd_object *obj = osd_dt_obj(dt);
3163 struct osd_thandle *oh;
3164 struct iam_path_descr *ipd;
3165 struct iam_container *bag = &obj->oo_dir->od_container;
3170 LINVRNT(osd_invariant(obj));
3171 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3172 LASSERT(bag->ic_object == obj->oo_inode);
3173 LASSERT(handle != NULL);
3175 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3178 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3180 ipd = osd_idx_ipd_get(env, bag);
3181 if (unlikely(ipd == NULL))
3184 oh = container_of0(handle, struct osd_thandle, ot_super);
3185 LASSERT(oh->ot_handle != NULL);
3186 LASSERT(oh->ot_handle->h_transaction != NULL);
3188 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3189 /* swab quota uid/gid provided by caller */
3190 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3191 key = (const struct dt_key *)&oti->oti_quota_id;
3194 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3195 osd_ipd_put(env, bag, ipd);
3196 LINVRNT(osd_invariant(obj));
3200 static int osd_index_declare_ea_delete(const struct lu_env *env,
3201 struct dt_object *dt,
3202 const struct dt_key *key,
3203 struct thandle *handle)
3205 struct osd_thandle *oh;
3206 struct inode *inode;
3210 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3211 LASSERT(handle != NULL);
3213 oh = container_of0(handle, struct osd_thandle, ot_super);
3214 LASSERT(oh->ot_handle == NULL);
3216 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3217 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3219 inode = osd_dt_obj(dt)->oo_inode;
3222 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3223 true, true, NULL, false);
3227 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3230 struct osd_fid_pack *rec;
3233 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3234 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3235 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3240 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3243 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3244 struct seq_server_site *ss = osd_seq_site(osd);
3248 /* Those FID seqs, which are not in FLDB, must be local seq */
3249 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3252 rc = osd_fld_lookup(env, osd, fid, range);
3254 CERROR("%s: Can not lookup fld for "DFID"\n",
3255 osd_name(osd), PFID(fid));
3259 RETURN(ss->ss_node_id != range->lsr_index);
3263 * Index delete function for interoperability mode (b11826).
3264 * It will remove the directory entry added by osd_index_ea_insert().
3265 * This entry is needed to maintain name->fid mapping.
3267 * \param key, key i.e. file entry to be deleted
3269 * \retval 0, on success
3270 * \retval -ve, on error
3272 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3273 const struct dt_key *key,
3274 struct thandle *handle,
3275 struct lustre_capa *capa)
3277 struct osd_object *obj = osd_dt_obj(dt);
3278 struct inode *dir = obj->oo_inode;
3279 struct dentry *dentry;
3280 struct osd_thandle *oh;
3281 struct ldiskfs_dir_entry_2 *de = NULL;
3282 struct buffer_head *bh;
3283 struct htree_lock *hlock = NULL;
3284 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3285 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3289 LINVRNT(osd_invariant(obj));
3290 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3291 LASSERT(handle != NULL);
3293 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3295 oh = container_of(handle, struct osd_thandle, ot_super);
3296 LASSERT(oh->ot_handle != NULL);
3297 LASSERT(oh->ot_handle->h_transaction != NULL);
3299 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3302 ll_vfs_dq_init(dir);
3303 dentry = osd_child_dentry_get(env, obj,
3304 (char *)key, strlen((char *)key));
3306 if (obj->oo_hl_head != NULL) {
3307 hlock = osd_oti_get(env)->oti_hlock;
3308 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3309 dir, LDISKFS_HLOCK_DEL);
3311 down_write(&obj->oo_ext_idx_sem);
3314 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3318 /* If this is not the ".." entry, it might be a remote DNE
3319 * entry and we need to check if the FID is for a remote
3320 * MDT. If the FID is not in the directory entry (e.g.
3321 * upgraded 1.8 filesystem without dirdata enabled) then
3322 * we need to get the FID from the LMA. For a remote directory
3323 * there HAS to be an LMA, it cannot be an IGIF inode in this
3326 * Delete the entry before the agent inode in order to
3327 * simplify error handling. At worst an error after deleting
3328 * the entry first might leak the agent inode afterward. The
3329 * reverse would need filesystem abort in case of error deleting
3330 * the entry after the agent had been removed, or leave a
3331 * dangling entry pointing at a random inode. */
3332 if (strcmp((char *)key, dotdot) != 0) {
3333 LASSERT(de != NULL);
3334 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3335 /* If Fid is not in dentry, try to get it from LMA */
3336 if (rc == -ENODATA) {
3337 struct osd_inode_id *id;
3338 struct inode *inode;
3340 /* Before trying to get fid from the inode,
3341 * check whether the inode is valid.
3343 * If the inode has been deleted, do not go
3344 * ahead to do osd_ea_fid_get, which will set
3345 * the inode to bad inode, which might cause
3346 * the inode to be deleted uncorrectly */
3347 inode = ldiskfs_iget(osd_sb(osd),
3348 le32_to_cpu(de->inode));
3349 if (IS_ERR(inode)) {
3350 CDEBUG(D_INODE, "%s: "DFID"get inode"
3351 "error.\n", osd_name(osd),
3353 rc = PTR_ERR(inode);
3355 if (likely(inode->i_nlink != 0)) {
3356 id = &osd_oti_get(env)->oti_id;
3357 rc = osd_ea_fid_get(env, obj,
3358 le32_to_cpu(de->inode),
3361 CDEBUG(D_INFO, "%s: %u "DFID
3364 le32_to_cpu(de->inode),
3372 unlikely(osd_remote_fid(env, osd, fid)))
3373 /* Need to delete agent inode */
3374 ino = le32_to_cpu(de->inode);
3376 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3378 if (rc == 0 && unlikely(ino != 0)) {
3379 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3382 CERROR("%s: del local inode "DFID": rc = %d\n",
3383 osd_name(osd), PFID(fid), rc);
3389 ldiskfs_htree_unlock(hlock);
3391 up_write(&obj->oo_ext_idx_sem);
3396 /* For inode on the remote MDT, .. will point to
3397 * /Agent directory, Check whether it needs to delete
3398 * from agent directory */
3399 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3400 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3402 if (rc != 0 && rc != -ENOENT) {
3403 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3404 osd_name(osd), PFID(fid), rc);
3414 LASSERT(osd_invariant(obj));
3419 * Lookup index for \a key and copy record to \a rec.
3421 * \param dt osd index object
3422 * \param key key for index
3423 * \param rec record reference
3425 * \retval +ve success : exact mach
3426 * \retval 0 return record with key not greater than \a key
3427 * \retval -ve failure
3429 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3430 struct dt_rec *rec, const struct dt_key *key,
3431 struct lustre_capa *capa)
3433 struct osd_object *obj = osd_dt_obj(dt);
3434 struct iam_path_descr *ipd;
3435 struct iam_container *bag = &obj->oo_dir->od_container;
3436 struct osd_thread_info *oti = osd_oti_get(env);
3437 struct iam_iterator *it = &oti->oti_idx_it;
3438 struct iam_rec *iam_rec;
3443 LASSERT(osd_invariant(obj));
3444 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3445 LASSERT(bag->ic_object == obj->oo_inode);
3447 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3450 ipd = osd_idx_ipd_get(env, bag);
3454 /* got ipd now we can start iterator. */
3455 iam_it_init(it, bag, 0, ipd);
3457 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3458 /* swab quota uid/gid provided by caller */
3459 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3460 key = (const struct dt_key *)&oti->oti_quota_id;
3463 rc = iam_it_get(it, (struct iam_key *)key);
3465 if (S_ISDIR(obj->oo_inode->i_mode))
3466 iam_rec = (struct iam_rec *)oti->oti_ldp;
3468 iam_rec = (struct iam_rec *) rec;
3470 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3472 if (S_ISDIR(obj->oo_inode->i_mode))
3473 osd_fid_unpack((struct lu_fid *) rec,
3474 (struct osd_fid_pack *)iam_rec);
3475 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3476 osd_quota_unpack(obj, rec);
3481 osd_ipd_put(env, bag, ipd);
3483 LINVRNT(osd_invariant(obj));
3488 static int osd_index_declare_iam_insert(const struct lu_env *env,
3489 struct dt_object *dt,
3490 const struct dt_rec *rec,
3491 const struct dt_key *key,
3492 struct thandle *handle)
3494 struct osd_thandle *oh;
3496 LASSERT(handle != NULL);
3498 oh = container_of0(handle, struct osd_thandle, ot_super);
3499 LASSERT(oh->ot_handle == NULL);
3501 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3502 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3508 * Inserts (key, value) pair in \a dt index object.
3510 * \param dt osd index object
3511 * \param key key for index
3512 * \param rec record reference
3513 * \param th transaction handler
3516 * \retval -ve failure
3518 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3519 const struct dt_rec *rec,
3520 const struct dt_key *key, struct thandle *th,
3521 struct lustre_capa *capa, int ignore_quota)
3523 struct osd_object *obj = osd_dt_obj(dt);
3524 struct iam_path_descr *ipd;
3525 struct osd_thandle *oh;
3526 struct iam_container *bag = &obj->oo_dir->od_container;
3527 struct osd_thread_info *oti = osd_oti_get(env);
3528 struct iam_rec *iam_rec;
3533 LINVRNT(osd_invariant(obj));
3534 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3535 LASSERT(bag->ic_object == obj->oo_inode);
3536 LASSERT(th != NULL);
3538 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3541 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3543 ipd = osd_idx_ipd_get(env, bag);
3544 if (unlikely(ipd == NULL))
3547 oh = container_of0(th, struct osd_thandle, ot_super);
3548 LASSERT(oh->ot_handle != NULL);
3549 LASSERT(oh->ot_handle->h_transaction != NULL);
3550 if (S_ISDIR(obj->oo_inode->i_mode)) {
3551 iam_rec = (struct iam_rec *)oti->oti_ldp;
3552 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3553 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3554 /* pack quota uid/gid */
3555 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3556 key = (const struct dt_key *)&oti->oti_quota_id;
3557 /* pack quota record */
3558 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3559 iam_rec = (struct iam_rec *)rec;
3561 iam_rec = (struct iam_rec *)rec;
3564 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3566 osd_ipd_put(env, bag, ipd);
3567 LINVRNT(osd_invariant(obj));
3572 * Calls ldiskfs_add_entry() to add directory entry
3573 * into the directory. This is required for
3574 * interoperability mode (b11826)
3576 * \retval 0, on success
3577 * \retval -ve, on error
3579 static int __osd_ea_add_rec(struct osd_thread_info *info,
3580 struct osd_object *pobj, struct inode *cinode,
3581 const char *name, const struct dt_rec *fid,
3582 struct htree_lock *hlock, struct thandle *th)
3584 struct ldiskfs_dentry_param *ldp;
3585 struct dentry *child;
3586 struct osd_thandle *oth;
3589 oth = container_of(th, struct osd_thandle, ot_super);
3590 LASSERT(oth->ot_handle != NULL);
3591 LASSERT(oth->ot_handle->h_transaction != NULL);
3592 LASSERT(pobj->oo_inode);
3594 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3595 if (unlikely(pobj->oo_inode ==
3596 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3599 osd_get_ldiskfs_dirent_param(ldp, fid);
3600 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3601 child->d_fsdata = (void *)ldp;
3602 ll_vfs_dq_init(pobj->oo_inode);
3603 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3609 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3610 * into the directory.Also sets flags into osd object to
3611 * indicate dot and dotdot are created. This is required for
3612 * interoperability mode (b11826)
3614 * \param dir directory for dot and dotdot fixup.
3615 * \param obj child object for linking
3617 * \retval 0, on success
3618 * \retval -ve, on error
3620 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3621 struct osd_object *dir,
3622 struct inode *parent_dir, const char *name,
3623 const struct dt_rec *dot_fid,
3624 const struct dt_rec *dot_dot_fid,
3627 struct inode *inode = dir->oo_inode;
3628 struct osd_thandle *oth;
3631 oth = container_of(th, struct osd_thandle, ot_super);
3632 LASSERT(oth->ot_handle->h_transaction != NULL);
3633 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3635 if (strcmp(name, dot) == 0) {
3636 if (dir->oo_compat_dot_created) {
3639 LASSERT(inode == parent_dir);
3640 dir->oo_compat_dot_created = 1;
3643 } else if (strcmp(name, dotdot) == 0) {
3644 if (!dir->oo_compat_dot_created)
3646 /* in case of rename, dotdot is already created */
3647 if (dir->oo_compat_dotdot_created) {
3648 return __osd_ea_add_rec(info, dir, parent_dir, name,
3649 dot_dot_fid, NULL, th);
3652 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3653 parent_dir, dot_fid,
3656 dir->oo_compat_dotdot_created = 1;
3664 * It will call the appropriate osd_add* function and return the
3665 * value, return by respective functions.
3667 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3668 struct inode *cinode, const char *name,
3669 const struct dt_rec *fid, struct thandle *th)
3671 struct osd_thread_info *info = osd_oti_get(env);
3672 struct htree_lock *hlock;
3675 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3677 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3679 if (hlock != NULL) {
3680 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3683 down_write(&pobj->oo_ext_idx_sem);
3685 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3686 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3689 if (hlock != NULL) {
3690 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3691 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3693 down_write(&pobj->oo_ext_idx_sem);
3696 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3697 struct lu_fid *tfid = &info->oti_fid;
3699 *tfid = *(const struct lu_fid *)fid;
3701 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3702 (const struct dt_rec *)tfid,
3705 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3710 ldiskfs_htree_unlock(hlock);
3712 up_write(&pobj->oo_ext_idx_sem);
3718 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3719 struct osd_idmap_cache *oic)
3721 struct osd_scrub *scrub = &dev->od_scrub;
3722 struct lu_fid *fid = &oic->oic_fid;
3723 struct osd_inode_id *id = &oti->oti_id;
3728 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3732 rc = osd_oi_lookup(oti, dev, fid, id, true);
3733 if (rc != 0 && rc != -ENOENT)
3736 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3739 if (thread_is_running(&scrub->os_thread)) {
3740 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3741 /* There is race condition between osd_oi_lookup and OI scrub.
3742 * The OI scrub finished just after osd_oi_lookup() failure.
3743 * Under such case, it is unnecessary to trigger OI scrub again,
3744 * but try to call osd_oi_lookup() again. */
3745 if (unlikely(rc == -EAGAIN))
3751 if (!dev->od_noscrub && ++once == 1) {
3752 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3754 rc = osd_scrub_start(dev);
3755 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3757 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3766 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3767 struct osd_device *dev,
3768 struct osd_idmap_cache *oic,
3769 struct lu_fid *fid, __u32 ino)
3771 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3772 struct inode *inode;
3775 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3776 inode = osd_iget(oti, dev, &oic->oic_lid);
3777 if (IS_ERR(inode)) {
3778 fid_zero(&oic->oic_fid);
3779 return PTR_ERR(inode);
3782 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3785 fid_zero(&oic->oic_fid);
3787 *fid = oic->oic_fid = lma->lma_self_fid;
3791 static int osd_add_oi_cache(struct osd_thread_info *info,
3792 struct osd_device *osd,
3793 struct osd_inode_id *id,
3796 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3797 id->oii_ino, id->oii_gen, info);
3798 info->oti_cache.oic_lid = *id;
3799 info->oti_cache.oic_fid = *fid;
3805 * Calls ->lookup() to find dentry. From dentry get inode and
3806 * read inode's ea to get fid. This is required for interoperability
3809 * \retval 0, on success
3810 * \retval -ve, on error
3812 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3813 struct dt_rec *rec, const struct dt_key *key)
3815 struct inode *dir = obj->oo_inode;
3816 struct dentry *dentry;
3817 struct ldiskfs_dir_entry_2 *de;
3818 struct buffer_head *bh;
3819 struct lu_fid *fid = (struct lu_fid *) rec;
3820 struct htree_lock *hlock = NULL;
3825 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3827 dentry = osd_child_dentry_get(env, obj,
3828 (char *)key, strlen((char *)key));
3830 if (obj->oo_hl_head != NULL) {
3831 hlock = osd_oti_get(env)->oti_hlock;
3832 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3833 dir, LDISKFS_HLOCK_LOOKUP);
3835 down_read(&obj->oo_ext_idx_sem);
3838 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3840 struct osd_thread_info *oti = osd_oti_get(env);
3841 struct osd_inode_id *id = &oti->oti_id;
3842 struct osd_idmap_cache *oic = &oti->oti_cache;
3843 struct osd_device *dev = osd_obj2dev(obj);
3844 struct osd_scrub *scrub = &dev->od_scrub;
3845 struct scrub_file *sf = &scrub->os_file;
3847 ino = le32_to_cpu(de->inode);
3848 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3850 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3854 rc = osd_get_fid_from_dentry(de, rec);
3856 /* done with de, release bh */
3859 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3861 osd_id_gen(id, ino, OSD_OII_NOGEN);
3862 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3863 fid_zero(&oic->oic_fid);
3867 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3871 if ((scrub->os_pos_current <= ino) &&
3872 ((sf->sf_flags & SF_INCONSISTENT) ||
3873 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3874 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3876 osd_consistency_check(oti, dev, oic);
3885 ldiskfs_htree_unlock(hlock);
3887 up_read(&obj->oo_ext_idx_sem);
3892 * Find the osd object for given fid.
3894 * \param fid need to find the osd object having this fid
3896 * \retval osd_object on success
3897 * \retval -ve on error
3899 struct osd_object *osd_object_find(const struct lu_env *env,
3900 struct dt_object *dt,
3901 const struct lu_fid *fid)
3903 struct lu_device *ludev = dt->do_lu.lo_dev;
3904 struct osd_object *child = NULL;
3905 struct lu_object *luch;
3906 struct lu_object *lo;
3909 * at this point topdev might not exist yet
3910 * (i.e. MGS is preparing profiles). so we can
3911 * not rely on topdev and instead lookup with
3912 * our device passed as topdev. this can't work
3913 * if the object isn't cached yet (as osd doesn't
3914 * allocate lu_header). IOW, the object must be
3915 * in the cache, otherwise lu_object_alloc() crashes
3918 luch = lu_object_find_at(env, ludev, fid, NULL);
3919 if (!IS_ERR(luch)) {
3920 if (lu_object_exists(luch)) {
3921 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3923 child = osd_obj(lo);
3925 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3926 "lu_object can't be located"
3927 DFID"\n", PFID(fid));
3929 if (child == NULL) {
3930 lu_object_put(env, luch);
3931 CERROR("Unable to get osd_object\n");
3932 child = ERR_PTR(-ENOENT);
3935 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3936 "lu_object does not exists "DFID"\n",
3938 lu_object_put(env, luch);
3939 child = ERR_PTR(-ENOENT);
3942 child = (void *)luch;
3948 * Put the osd object once done with it.
3950 * \param obj osd object that needs to be put
3952 static inline void osd_object_put(const struct lu_env *env,
3953 struct osd_object *obj)
3955 lu_object_put(env, &obj->oo_dt.do_lu);
3958 static int osd_index_declare_ea_insert(const struct lu_env *env,
3959 struct dt_object *dt,
3960 const struct dt_rec *rec,
3961 const struct dt_key *key,
3962 struct thandle *handle)
3964 struct osd_thandle *oh;
3965 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3966 struct lu_fid *fid = (struct lu_fid *)rec;
3970 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3971 LASSERT(handle != NULL);
3973 oh = container_of0(handle, struct osd_thandle, ot_super);
3974 LASSERT(oh->ot_handle == NULL);
3976 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3977 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3979 if (osd_dt_obj(dt)->oo_inode == NULL) {
3980 const char *name = (const char *)key;
3981 /* Object is not being created yet. Only happens when
3982 * 1. declare directory create
3983 * 2. declare insert .
3984 * 3. declare insert ..
3986 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3988 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3990 /* We ignore block quota on meta pool (MDTs), so needn't
3991 * calculate how many blocks will be consumed by this index
3993 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3994 oh, true, true, NULL, false);
4000 rc = osd_remote_fid(env, osd, fid);
4006 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4007 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4008 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4009 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4010 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4011 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4017 * Index add function for interoperability mode (b11826).
4018 * It will add the directory entry.This entry is needed to
4019 * maintain name->fid mapping.
4021 * \param key it is key i.e. file entry to be inserted
4022 * \param rec it is value of given key i.e. fid
4024 * \retval 0, on success
4025 * \retval -ve, on error
4027 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4028 const struct dt_rec *rec,
4029 const struct dt_key *key, struct thandle *th,
4030 struct lustre_capa *capa, int ignore_quota)
4032 struct osd_object *obj = osd_dt_obj(dt);
4033 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4034 struct lu_fid *fid = (struct lu_fid *) rec;
4035 const char *name = (const char *)key;
4036 struct osd_thread_info *oti = osd_oti_get(env);
4037 struct osd_inode_id *id = &oti->oti_id;
4038 struct inode *child_inode = NULL;
4039 struct osd_object *child = NULL;
4043 LASSERT(osd_invariant(obj));
4044 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4045 LASSERT(th != NULL);
4047 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4049 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4052 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4054 rc = osd_remote_fid(env, osd, fid);
4056 CERROR("%s: Can not find object "DFID" rc %d\n",
4057 osd_name(osd), PFID(fid), rc);
4062 /* Insert remote entry */
4063 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4064 struct osd_mdobj_map *omm = osd->od_mdt_map;
4065 struct osd_thandle *oh;
4067 /* If parent on remote MDT, we need put this object
4069 oh = container_of(th, typeof(*oh), ot_super);
4070 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4072 CERROR("%s: add "DFID" error: rc = %d\n",
4074 PFID(lu_object_fid(&dt->do_lu)), rc);
4078 child_inode = igrab(omm->omm_remote_parent->d_inode);
4080 child_inode = osd_create_local_agent_inode(env, osd,
4083 if (IS_ERR(child_inode))
4084 RETURN(PTR_ERR(child_inode));
4087 /* Insert local entry */
4088 child = osd_object_find(env, dt, fid);
4089 if (IS_ERR(child)) {
4090 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4091 osd_name(osd), PFID(fid),
4092 id->oii_ino, id->oii_gen,
4093 (int)PTR_ERR(child_inode));
4094 RETURN(PTR_ERR(child_inode));
4096 child_inode = igrab(child->oo_inode);
4099 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4103 osd_object_put(env, child);
4104 LASSERT(osd_invariant(obj));
4109 * Initialize osd Iterator for given osd index object.
4111 * \param dt osd index object
4114 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4115 struct dt_object *dt,
4117 struct lustre_capa *capa)
4119 struct osd_it_iam *it;
4120 struct osd_thread_info *oti = osd_oti_get(env);
4121 struct osd_object *obj = osd_dt_obj(dt);
4122 struct lu_object *lo = &dt->do_lu;
4123 struct iam_path_descr *ipd;
4124 struct iam_container *bag = &obj->oo_dir->od_container;
4126 LASSERT(lu_object_exists(lo));
4128 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4129 return ERR_PTR(-EACCES);
4132 ipd = osd_it_ipd_get(env, bag);
4133 if (likely(ipd != NULL)) {
4137 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4138 return (struct dt_it *)it;
4140 return ERR_PTR(-ENOMEM);
4144 * free given Iterator.
4147 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4149 struct osd_it_iam *it = (struct osd_it_iam *)di;
4150 struct osd_object *obj = it->oi_obj;
4152 iam_it_fini(&it->oi_it);
4153 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4154 lu_object_put(env, &obj->oo_dt.do_lu);
4158 * Move Iterator to record specified by \a key
4160 * \param di osd iterator
4161 * \param key key for index
4163 * \retval +ve di points to record with least key not larger than key
4164 * \retval 0 di points to exact matched key
4165 * \retval -ve failure
4168 static int osd_it_iam_get(const struct lu_env *env,
4169 struct dt_it *di, const struct dt_key *key)
4171 struct osd_thread_info *oti = osd_oti_get(env);
4172 struct osd_it_iam *it = (struct osd_it_iam *)di;
4174 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4175 /* swab quota uid/gid */
4176 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4177 key = (struct dt_key *)&oti->oti_quota_id;
4180 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4186 * \param di osd iterator
4188 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4190 struct osd_it_iam *it = (struct osd_it_iam *)di;
4192 iam_it_put(&it->oi_it);
4196 * Move iterator by one record
4198 * \param di osd iterator
4200 * \retval +1 end of container reached
4202 * \retval -ve failure
4205 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4207 struct osd_it_iam *it = (struct osd_it_iam *)di;
4209 return iam_it_next(&it->oi_it);
4213 * Return pointer to the key under iterator.
4216 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4217 const struct dt_it *di)
4219 struct osd_thread_info *oti = osd_oti_get(env);
4220 struct osd_it_iam *it = (struct osd_it_iam *)di;
4221 struct osd_object *obj = it->oi_obj;
4224 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4226 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4227 /* swab quota uid/gid */
4228 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4229 key = (struct dt_key *)&oti->oti_quota_id;
4236 * Return size of key under iterator (in bytes)
4239 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4241 struct osd_it_iam *it = (struct osd_it_iam *)di;
4243 return iam_it_key_size(&it->oi_it);
4247 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4249 /* check if file type is required */
4250 if (ent->lde_attrs & LUDA_TYPE) {
4251 struct luda_type *lt;
4252 int align = sizeof(*lt) - 1;
4254 len = (len + align) & ~align;
4255 lt = (struct luda_type *)(ent->lde_name + len);
4256 lt->lt_type = cpu_to_le16(DTTOIF(type));
4259 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4263 * build lu direct from backend fs dirent.
4267 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4268 char *name, __u16 namelen, __u16 type, __u32 attr)
4270 ent->lde_attrs = attr | LUDA_FID;
4271 fid_cpu_to_le(&ent->lde_fid, fid);
4273 ent->lde_hash = cpu_to_le64(offset);
4274 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4276 strncpy(ent->lde_name, name, namelen);
4277 ent->lde_name[namelen] = '\0';
4278 ent->lde_namelen = cpu_to_le16(namelen);
4280 /* append lustre attributes */
4281 osd_it_append_attrs(ent, namelen, type);
4285 * Return pointer to the record under iterator.
4287 static int osd_it_iam_rec(const struct lu_env *env,
4288 const struct dt_it *di,
4289 struct dt_rec *dtrec, __u32 attr)
4291 struct osd_it_iam *it = (struct osd_it_iam *)di;
4292 struct osd_thread_info *info = osd_oti_get(env);
4295 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4296 const struct osd_fid_pack *rec;
4297 struct lu_fid *fid = &info->oti_fid;
4298 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4304 name = (char *)iam_it_key_get(&it->oi_it);
4306 RETURN(PTR_ERR(name));
4308 namelen = iam_it_key_size(&it->oi_it);
4310 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4312 RETURN(PTR_ERR(rec));
4314 rc = osd_fid_unpack(fid, rec);
4318 hash = iam_it_store(&it->oi_it);
4320 /* IAM does not store object type in IAM index (dir) */
4321 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4323 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4324 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4325 (struct iam_rec *)dtrec);
4326 osd_quota_unpack(it->oi_obj, dtrec);
4328 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4329 (struct iam_rec *)dtrec);
4336 * Returns cookie for current Iterator position.
4338 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4340 struct osd_it_iam *it = (struct osd_it_iam *)di;
4342 return iam_it_store(&it->oi_it);
4346 * Restore iterator from cookie.
4348 * \param di osd iterator
4349 * \param hash Iterator location cookie
4351 * \retval +ve di points to record with least key not larger than key.
4352 * \retval 0 di points to exact matched key
4353 * \retval -ve failure
4356 static int osd_it_iam_load(const struct lu_env *env,
4357 const struct dt_it *di, __u64 hash)
4359 struct osd_it_iam *it = (struct osd_it_iam *)di;
4361 return iam_it_load(&it->oi_it, hash);
4364 static const struct dt_index_operations osd_index_iam_ops = {
4365 .dio_lookup = osd_index_iam_lookup,
4366 .dio_declare_insert = osd_index_declare_iam_insert,
4367 .dio_insert = osd_index_iam_insert,
4368 .dio_declare_delete = osd_index_declare_iam_delete,
4369 .dio_delete = osd_index_iam_delete,
4371 .init = osd_it_iam_init,
4372 .fini = osd_it_iam_fini,
4373 .get = osd_it_iam_get,
4374 .put = osd_it_iam_put,
4375 .next = osd_it_iam_next,
4376 .key = osd_it_iam_key,
4377 .key_size = osd_it_iam_key_size,
4378 .rec = osd_it_iam_rec,
4379 .store = osd_it_iam_store,
4380 .load = osd_it_iam_load
4386 * Creates or initializes iterator context.
4388 * \retval struct osd_it_ea, iterator structure on success
4391 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4392 struct dt_object *dt,
4394 struct lustre_capa *capa)
4396 struct osd_object *obj = osd_dt_obj(dt);
4397 struct osd_thread_info *info = osd_oti_get(env);
4398 struct osd_it_ea *it = &info->oti_it_ea;
4399 struct file *file = &it->oie_file;
4400 struct lu_object *lo = &dt->do_lu;
4401 struct dentry *obj_dentry = &info->oti_it_dentry;
4403 LASSERT(lu_object_exists(lo));
4405 obj_dentry->d_inode = obj->oo_inode;
4406 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4407 obj_dentry->d_name.hash = 0;
4409 it->oie_rd_dirent = 0;
4410 it->oie_it_dirent = 0;
4411 it->oie_dirent = NULL;
4412 it->oie_buf = info->oti_it_ea_buf;
4415 /* Reset the "file" totally to avoid to reuse any old value from
4416 * former readdir handling, the "file->f_pos" should be zero. */
4417 memset(file, 0, sizeof(*file));
4418 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4419 if (attr & LUDA_64BITHASH)
4420 file->f_mode = FMODE_64BITHASH;
4422 file->f_mode = FMODE_32BITHASH;
4423 file->f_dentry = obj_dentry;
4424 file->f_mapping = obj->oo_inode->i_mapping;
4425 file->f_op = obj->oo_inode->i_fop;
4427 RETURN((struct dt_it *) it);
4431 * Destroy or finishes iterator context.
4433 * \param di iterator structure to be destroyed
4435 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4437 struct osd_it_ea *it = (struct osd_it_ea *)di;
4438 struct osd_object *obj = it->oie_obj;
4439 struct inode *inode = obj->oo_inode;
4442 it->oie_file.f_op->release(inode, &it->oie_file);
4443 lu_object_put(env, &obj->oo_dt.do_lu);
4448 * It position the iterator at given key, so that next lookup continues from
4449 * that key Or it is similar to dio_it->load() but based on a key,
4450 * rather than file position.
4452 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4455 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4457 static int osd_it_ea_get(const struct lu_env *env,
4458 struct dt_it *di, const struct dt_key *key)
4460 struct osd_it_ea *it = (struct osd_it_ea *)di;
4463 LASSERT(((const char *)key)[0] == '\0');
4464 it->oie_file.f_pos = 0;
4465 it->oie_rd_dirent = 0;
4466 it->oie_it_dirent = 0;
4467 it->oie_dirent = NULL;
4475 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4480 * It is called internally by ->readdir(). It fills the
4481 * iterator's in-memory data structure with required
4482 * information i.e. name, namelen, rec_size etc.
4484 * \param buf in which information to be filled in.
4485 * \param name name of the file in given dir
4487 * \retval 0 on success
4488 * \retval 1 on buffer full
4490 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4491 loff_t offset, __u64 ino,
4494 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4495 struct osd_object *obj = it->oie_obj;
4496 struct osd_it_ea_dirent *ent = it->oie_dirent;
4497 struct lu_fid *fid = &ent->oied_fid;
4498 struct osd_fid_pack *rec;
4501 /* this should never happen */
4502 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4503 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4507 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4511 /* "." is just the object itself. */
4512 if (namelen == 1 && name[0] == '.') {
4513 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4514 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4515 rec = (struct osd_fid_pack*) (name + namelen + 1);
4516 if (osd_fid_unpack(fid, rec) != 0)
4521 d_type &= ~LDISKFS_DIRENT_LUFID;
4523 /* NOT export local root. */
4524 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4525 ino = obj->oo_inode->i_ino;
4526 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4529 ent->oied_ino = ino;
4530 ent->oied_off = offset;
4531 ent->oied_namelen = namelen;
4532 ent->oied_type = d_type;
4534 memcpy(ent->oied_name, name, namelen);
4536 it->oie_rd_dirent++;
4537 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4542 * Calls ->readdir() to load a directory entry at a time
4543 * and stored it in iterator's in-memory data structure.
4545 * \param di iterator's in memory structure
4547 * \retval 0 on success
4548 * \retval -ve on error
4550 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4551 const struct dt_it *di)
4553 struct osd_it_ea *it = (struct osd_it_ea *)di;
4554 struct osd_object *obj = it->oie_obj;
4555 struct inode *inode = obj->oo_inode;
4556 struct htree_lock *hlock = NULL;
4560 it->oie_dirent = it->oie_buf;
4561 it->oie_rd_dirent = 0;
4563 if (obj->oo_hl_head != NULL) {
4564 hlock = osd_oti_get(env)->oti_hlock;
4565 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4566 inode, LDISKFS_HLOCK_READDIR);
4568 down_read(&obj->oo_ext_idx_sem);
4571 result = inode->i_fop->readdir(&it->oie_file, it,
4572 (filldir_t) osd_ldiskfs_filldir);
4575 ldiskfs_htree_unlock(hlock);
4577 up_read(&obj->oo_ext_idx_sem);
4579 if (it->oie_rd_dirent == 0) {
4582 it->oie_dirent = it->oie_buf;
4583 it->oie_it_dirent = 1;
4590 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4591 * to load a directory entry at a time and stored it in
4592 * iterator's in-memory data structure.
4594 * \param di iterator's in memory structure
4596 * \retval +ve iterator reached to end
4597 * \retval 0 iterator not reached to end
4598 * \retval -ve on error
4600 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4602 struct osd_it_ea *it = (struct osd_it_ea *)di;
4607 if (it->oie_it_dirent < it->oie_rd_dirent) {
4609 (void *) it->oie_dirent +
4610 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4611 it->oie_dirent->oied_namelen);
4612 it->oie_it_dirent++;
4615 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4618 rc = osd_ldiskfs_it_fill(env, di);
4625 * Returns the key at current position from iterator's in memory structure.
4627 * \param di iterator's in memory structure
4629 * \retval key i.e. struct dt_key on success
4631 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4632 const struct dt_it *di)
4634 struct osd_it_ea *it = (struct osd_it_ea *)di;
4636 return (struct dt_key *)it->oie_dirent->oied_name;
4640 * Returns the key's size at current position from iterator's in memory structure.
4642 * \param di iterator's in memory structure
4644 * \retval key_size i.e. struct dt_key on success
4646 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4648 struct osd_it_ea *it = (struct osd_it_ea *)di;
4650 return it->oie_dirent->oied_namelen;
4654 osd_dirent_update(handle_t *jh, struct super_block *sb,
4655 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4656 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4658 struct osd_fid_pack *rec;
4662 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4663 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4665 rc = ldiskfs_journal_get_write_access(jh, bh);
4667 CERROR("%.16s: fail to write access for update dirent: "
4668 "name = %.*s, rc = %d\n",
4669 LDISKFS_SB(sb)->s_es->s_volume_name,
4670 ent->oied_namelen, ent->oied_name, rc);
4674 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4675 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4676 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4678 CERROR("%.16s: fail to dirty metadata for update dirent: "
4679 "name = %.*s, rc = %d\n",
4680 LDISKFS_SB(sb)->s_es->s_volume_name,
4681 ent->oied_namelen, ent->oied_name, rc);
4687 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4689 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4690 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4697 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4699 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4700 "dot_dotdot = %d\n", dot_dotdot);
4702 if (LDISKFS_DIR_REC_LEN(de) >=
4703 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4710 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4711 struct inode *dir, struct inode *inode,
4712 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4713 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4714 struct htree_lock *hlock)
4716 struct dentry *dentry;
4717 struct osd_fid_pack *rec;
4718 struct ldiskfs_dentry_param *ldp;
4722 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4723 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4726 /* There is enough space to hold the FID-in-dirent. */
4727 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4728 dir->i_sb->s_blocksize)) {
4729 rc = ldiskfs_journal_get_write_access(jh, bh);
4731 CERROR("%.16s: fail to write access for reinsert "
4732 "dirent: name = %.*s, rc = %d\n",
4733 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4734 ent->oied_namelen, ent->oied_name, rc);
4738 de->name[de->name_len] = 0;
4739 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4740 rec->fp_len = sizeof(struct lu_fid) + 1;
4741 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4742 de->file_type |= LDISKFS_DIRENT_LUFID;
4744 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4746 CERROR("%.16s: fail to dirty metadata for reinsert "
4747 "dirent: name = %.*s, rc = %d\n",
4748 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4749 ent->oied_namelen, ent->oied_name, rc);
4754 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4756 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4757 "name = %.*s, rc = %d\n",
4758 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4759 ent->oied_namelen, ent->oied_name, rc);
4763 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4765 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4766 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4767 dentry->d_fsdata = (void *)ldp;
4768 ll_vfs_dq_init(dir);
4769 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4770 /* It is too bad, we cannot reinsert the name entry back.
4771 * That means we lose it! */
4773 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4774 "name = %.*s, rc = %d\n",
4775 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4776 ent->oied_namelen, ent->oied_name, rc);
4782 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4783 struct osd_it_ea *it, struct lu_fid *fid,
4784 struct osd_inode_id *id, __u32 *attr)
4786 struct osd_thread_info *info = osd_oti_get(env);
4787 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4788 struct osd_device *dev = osd_obj2dev(obj);
4789 struct super_block *sb = osd_sb(dev);
4790 const char *devname =
4791 LDISKFS_SB(sb)->s_es->s_volume_name;
4792 struct osd_it_ea_dirent *ent = it->oie_dirent;
4793 struct inode *dir = obj->oo_inode;
4794 struct htree_lock *hlock = NULL;
4795 struct buffer_head *bh = NULL;
4796 handle_t *jh = NULL;
4797 struct ldiskfs_dir_entry_2 *de;
4798 struct dentry *dentry;
4799 struct inode *inode;
4806 if (ent->oied_name[0] == '.') {
4807 if (ent->oied_namelen == 1)
4809 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4813 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4816 /* We need to ensure that the name entry is still valid.
4817 * Because it may be removed or renamed by other already.
4819 * The unlink or rename operation will start journal before PDO lock,
4820 * so to avoid deadlock, here we need to start journal handle before
4821 * related PDO lock also. But because we do not know whether there
4822 * will be something to be repaired before PDO lock, we just start
4823 * journal without conditions.
4825 * We may need to remove the name entry firstly, then insert back.
4826 * One credit is for user quota file update.
4827 * One credit is for group quota file update.
4828 * Two credits are for dirty inode. */
4829 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4830 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4833 if (dev->od_dirent_journal) {
4834 jh = ldiskfs_journal_start_sb(sb, credits);
4837 CERROR("%.16s: fail to start trans for dirent "
4838 "check_repair: credits %d, name %.*s, rc %d\n",
4839 devname, credits, ent->oied_namelen,
4840 ent->oied_name, rc);
4844 if (obj->oo_hl_head != NULL) {
4845 hlock = osd_oti_get(env)->oti_hlock;
4846 /* "0" means exclusive lock for the whole directory.
4847 * We need to prevent others access such name entry
4848 * during the delete + insert. Neither HLOCK_ADD nor
4849 * HLOCK_DEL cannot guarantee the atomicity. */
4850 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4852 down_write(&obj->oo_ext_idx_sem);
4855 if (obj->oo_hl_head != NULL) {
4856 hlock = osd_oti_get(env)->oti_hlock;
4857 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4858 LDISKFS_HLOCK_LOOKUP);
4860 down_read(&obj->oo_ext_idx_sem);
4864 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4865 /* For dot/dotdot entry, if there is not enough space to hold the
4866 * FID-in-dirent, just keep them there. It only happens when the
4867 * device upgraded from 1.8 or restored from MDT file-level backup.
4868 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4869 * and needs to get FID from LMA when readdir, it will not affect the
4870 * performance much. */
4871 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4872 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4873 *attr |= LUDA_IGNORE;
4874 GOTO(out_journal, rc = 0);
4877 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4878 inode = osd_iget(info, dev, id);
4879 if (IS_ERR(inode)) {
4880 rc = PTR_ERR(inode);
4881 if (rc == -ENOENT || rc == -ESTALE) {
4882 *attr |= LUDA_IGNORE;
4886 GOTO(out_journal, rc);
4889 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4891 if (fid_is_sane(fid)) {
4892 /* FID-in-dirent is valid. */
4893 if (lu_fid_eq(fid, &lma->lma_self_fid))
4894 GOTO(out_inode, rc = 0);
4896 /* Do not repair under dryrun mode. */
4897 if (*attr & LUDA_VERIFY_DRYRUN) {
4898 *attr |= LUDA_REPAIR;
4899 GOTO(out_inode, rc = 0);
4902 if (!dev->od_dirent_journal) {
4906 ldiskfs_htree_unlock(hlock);
4908 up_read(&obj->oo_ext_idx_sem);
4909 dev->od_dirent_journal = 1;
4913 *fid = lma->lma_self_fid;
4915 /* Update the FID-in-dirent. */
4916 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4918 *attr |= LUDA_REPAIR;
4920 /* Do not repair under dryrun mode. */
4921 if (*attr & LUDA_VERIFY_DRYRUN) {
4922 *fid = lma->lma_self_fid;
4923 *attr |= LUDA_REPAIR;
4924 GOTO(out_inode, rc = 0);
4927 if (!dev->od_dirent_journal) {
4931 ldiskfs_htree_unlock(hlock);
4933 up_read(&obj->oo_ext_idx_sem);
4934 dev->od_dirent_journal = 1;
4938 *fid = lma->lma_self_fid;
4940 /* Append the FID-in-dirent. */
4941 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4942 fid, bh, de, hlock);
4944 *attr |= LUDA_REPAIR;
4946 } else if (rc == -ENODATA) {
4947 /* Do not repair under dryrun mode. */
4948 if (*attr & LUDA_VERIFY_DRYRUN) {
4949 if (fid_is_sane(fid)) {
4950 *attr |= LUDA_REPAIR;
4952 lu_igif_build(fid, inode->i_ino,
4953 inode->i_generation);
4954 *attr |= LUDA_UPGRADE;
4956 GOTO(out_inode, rc = 0);
4959 if (!dev->od_dirent_journal) {
4963 ldiskfs_htree_unlock(hlock);
4965 up_read(&obj->oo_ext_idx_sem);
4966 dev->od_dirent_journal = 1;
4971 if (unlikely(fid_is_sane(fid))) {
4972 /* FID-in-dirent exists, but FID-in-LMA is lost.
4973 * Trust the FID-in-dirent, and add FID-in-LMA. */
4974 rc = osd_ea_fid_set(info, inode, fid, 0);
4976 *attr |= LUDA_REPAIR;
4978 lu_igif_build(fid, inode->i_ino, inode->i_generation);
4979 /* It is probably IGIF object. Only aappend the
4980 * FID-in-dirent. OI scrub will process FID-in-LMA. */
4981 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4982 fid, bh, de, hlock);
4984 *attr |= LUDA_UPGRADE;
4988 GOTO(out_inode, rc);
4995 if (hlock != NULL) {
4996 ldiskfs_htree_unlock(hlock);
4998 if (dev->od_dirent_journal)
4999 up_write(&obj->oo_ext_idx_sem);
5001 up_read(&obj->oo_ext_idx_sem);
5004 ldiskfs_journal_stop(jh);
5005 if (rc >= 0 && !dirty)
5006 dev->od_dirent_journal = 0;
5011 * Returns the value at current position from iterator's in memory structure.
5013 * \param di struct osd_it_ea, iterator's in memory structure
5014 * \param attr attr requested for dirent.
5015 * \param lde lustre dirent
5017 * \retval 0 no error and \param lde has correct lustre dirent.
5018 * \retval -ve on error
5020 static inline int osd_it_ea_rec(const struct lu_env *env,
5021 const struct dt_it *di,
5022 struct dt_rec *dtrec, __u32 attr)
5024 struct osd_it_ea *it = (struct osd_it_ea *)di;
5025 struct osd_object *obj = it->oie_obj;
5026 struct osd_device *dev = osd_obj2dev(obj);
5027 struct osd_scrub *scrub = &dev->od_scrub;
5028 struct scrub_file *sf = &scrub->os_file;
5029 struct osd_thread_info *oti = osd_oti_get(env);
5030 struct osd_inode_id *id = &oti->oti_id;
5031 struct osd_idmap_cache *oic = &oti->oti_cache;
5032 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5033 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5034 __u32 ino = it->oie_dirent->oied_ino;
5038 if (attr & LUDA_VERIFY) {
5040 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5041 attr |= LUDA_IGNORE;
5046 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
5048 attr &= ~LU_DIRENT_ATTRS_MASK;
5049 if (!fid_is_sane(fid)) {
5050 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5053 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5055 osd_id_gen(id, ino, OSD_OII_NOGEN);
5063 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5064 it->oie_dirent->oied_name,
5065 it->oie_dirent->oied_namelen,
5066 it->oie_dirent->oied_type, attr);
5068 if (osd_remote_fid(env, dev, fid))
5071 if (likely(!(attr & LUDA_IGNORE)))
5072 rc = osd_add_oi_cache(oti, dev, id, fid);
5074 if (!(attr & LUDA_VERIFY) &&
5075 (scrub->os_pos_current <= ino) &&
5076 ((sf->sf_flags & SF_INCONSISTENT) ||
5077 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5078 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5079 osd_consistency_check(oti, dev, oic);
5085 * Returns a cookie for current position of the iterator head, so that
5086 * user can use this cookie to load/start the iterator next time.
5088 * \param di iterator's in memory structure
5090 * \retval cookie for current position, on success
5092 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5094 struct osd_it_ea *it = (struct osd_it_ea *)di;
5096 return it->oie_dirent->oied_off;
5100 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5101 * to load a directory entry at a time and stored it i inn,
5102 * in iterator's in-memory data structure.
5104 * \param di struct osd_it_ea, iterator's in memory structure
5106 * \retval +ve on success
5107 * \retval -ve on error
5109 static int osd_it_ea_load(const struct lu_env *env,
5110 const struct dt_it *di, __u64 hash)
5112 struct osd_it_ea *it = (struct osd_it_ea *)di;
5116 it->oie_file.f_pos = hash;
5118 rc = osd_ldiskfs_it_fill(env, di);
5126 * Index lookup function for interoperability mode (b11826).
5128 * \param key, key i.e. file name to be searched
5130 * \retval +ve, on success
5131 * \retval -ve, on error
5133 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5134 struct dt_rec *rec, const struct dt_key *key,
5135 struct lustre_capa *capa)
5137 struct osd_object *obj = osd_dt_obj(dt);
5142 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5143 LINVRNT(osd_invariant(obj));
5145 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5148 rc = osd_ea_lookup_rec(env, obj, rec, key);
5155 * Index and Iterator operations for interoperability
5156 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5158 static const struct dt_index_operations osd_index_ea_ops = {
5159 .dio_lookup = osd_index_ea_lookup,
5160 .dio_declare_insert = osd_index_declare_ea_insert,
5161 .dio_insert = osd_index_ea_insert,
5162 .dio_declare_delete = osd_index_declare_ea_delete,
5163 .dio_delete = osd_index_ea_delete,
5165 .init = osd_it_ea_init,
5166 .fini = osd_it_ea_fini,
5167 .get = osd_it_ea_get,
5168 .put = osd_it_ea_put,
5169 .next = osd_it_ea_next,
5170 .key = osd_it_ea_key,
5171 .key_size = osd_it_ea_key_size,
5172 .rec = osd_it_ea_rec,
5173 .store = osd_it_ea_store,
5174 .load = osd_it_ea_load
5178 static void *osd_key_init(const struct lu_context *ctx,
5179 struct lu_context_key *key)
5181 struct osd_thread_info *info;
5183 OBD_ALLOC_PTR(info);
5185 return ERR_PTR(-ENOMEM);
5187 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5188 if (info->oti_it_ea_buf == NULL)
5191 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5193 info->oti_hlock = ldiskfs_htree_lock_alloc();
5194 if (info->oti_hlock == NULL)
5200 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5203 return ERR_PTR(-ENOMEM);
5206 static void osd_key_fini(const struct lu_context *ctx,
5207 struct lu_context_key *key, void* data)
5209 struct osd_thread_info *info = data;
5211 if (info->oti_hlock != NULL)
5212 ldiskfs_htree_lock_free(info->oti_hlock);
5213 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5214 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5215 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5219 static void osd_key_exit(const struct lu_context *ctx,
5220 struct lu_context_key *key, void *data)
5222 struct osd_thread_info *info = data;
5224 LASSERT(info->oti_r_locks == 0);
5225 LASSERT(info->oti_w_locks == 0);
5226 LASSERT(info->oti_txns == 0);
5229 /* type constructor/destructor: osd_type_init, osd_type_fini */
5230 LU_TYPE_INIT_FINI(osd, &osd_key);
5232 struct lu_context_key osd_key = {
5233 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5234 .lct_init = osd_key_init,
5235 .lct_fini = osd_key_fini,
5236 .lct_exit = osd_key_exit
5240 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5241 const char *name, struct lu_device *next)
5243 struct osd_device *osd = osd_dev(d);
5245 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5246 >= sizeof(osd->od_svname))
5248 return osd_procfs_init(osd, name);
5251 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5255 /* shutdown quota slave instance associated with the device */
5256 if (o->od_quota_slave != NULL) {
5257 qsd_fini(env, o->od_quota_slave);
5258 o->od_quota_slave = NULL;
5264 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5269 fsfilt_put_ops(o->od_fsops);
5273 if (o->od_mnt != NULL) {
5274 shrink_dcache_sb(osd_sb(o));
5275 osd_sync(env, &o->od_dt_dev);
5284 static int osd_mount(const struct lu_env *env,
5285 struct osd_device *o, struct lustre_cfg *cfg)
5287 const char *name = lustre_cfg_string(cfg, 0);
5288 const char *dev = lustre_cfg_string(cfg, 1);
5290 unsigned long page, s_flags, lmd_flags = 0;
5291 struct page *__page;
5292 struct file_system_type *type;
5293 char *options = NULL;
5298 if (o->od_mnt != NULL)
5301 if (strlen(dev) >= sizeof(o->od_mntdev))
5303 strcpy(o->od_mntdev, dev);
5305 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5306 if (o->od_fsops == NULL) {
5307 CERROR("Can't find fsfilt_ldiskfs\n");
5311 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5313 GOTO(out, rc = -ENOMEM);
5315 str = lustre_cfg_string(cfg, 2);
5316 s_flags = simple_strtoul(str, NULL, 0);
5317 str = strstr(str, ":");
5319 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5320 opts = lustre_cfg_string(cfg, 3);
5321 page = (unsigned long)cfs_page_address(__page);
5322 options = (char *)page;
5325 strcat(options, "user_xattr,acl");
5327 strcat(options, opts);
5329 /* Glom up mount options */
5330 if (*options != '\0')
5331 strcat(options, ",");
5332 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5334 type = get_fs_type("ldiskfs");
5336 CERROR("%s: cannot find ldiskfs module\n", name);
5337 GOTO(out, rc = -ENODEV);
5340 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5341 cfs_module_put(type->owner);
5343 if (IS_ERR(o->od_mnt)) {
5344 rc = PTR_ERR(o->od_mnt);
5345 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5350 #ifdef HAVE_DEV_SET_RDONLY
5351 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5352 CERROR("%s: underlying device %s is marked as read-only. "
5353 "Setup failed\n", name, dev);
5356 GOTO(out, rc = -EROFS);
5360 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5361 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5362 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5365 GOTO(out, rc = -EINVAL);
5368 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5369 LDISKFS_STATE_LUSTRE_NO_OI);
5370 if (lmd_flags & LMD_FLG_NOSCRUB)
5375 OBD_PAGE_FREE(__page);
5377 fsfilt_put_ops(o->od_fsops);
5382 static struct lu_device *osd_device_fini(const struct lu_env *env,
5383 struct lu_device *d)
5385 struct osd_device *o = osd_dev(d);
5389 osd_shutdown(env, o);
5390 osd_scrub_cleanup(env, o);
5391 osd_obj_map_fini(o);
5397 static int osd_device_init0(const struct lu_env *env,
5398 struct osd_device *o,
5399 struct lustre_cfg *cfg)
5401 struct lu_device *l = osd2lu_dev(o);
5402 struct osd_thread_info *info;
5406 /* if the module was re-loaded, env can loose its keys */
5407 rc = lu_env_refill((struct lu_env *) env);
5410 info = osd_oti_get(env);
5413 l->ld_ops = &osd_lu_ops;
5414 o->od_dt_dev.dd_ops = &osd_dt_ops;
5416 spin_lock_init(&o->od_osfs_lock);
5417 mutex_init(&o->od_otable_mutex);
5418 o->od_osfs_age = cfs_time_shift_64(-1000);
5420 o->od_capa_hash = init_capa_hash();
5421 if (o->od_capa_hash == NULL)
5422 GOTO(out, rc = -ENOMEM);
5424 o->od_read_cache = 1;
5425 o->od_writethrough_cache = 1;
5426 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5428 rc = osd_mount(env, o, cfg);
5432 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5433 sizeof(o->od_svname));
5434 if (cplen >= sizeof(o->od_svname)) {
5439 rc = osd_obj_map_init(env, o);
5443 rc = lu_site_init(&o->od_site, l);
5445 GOTO(out_compat, rc);
5446 o->od_site.ls_bottom_dev = l;
5448 rc = lu_site_init_finish(&o->od_site);
5452 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5453 /* setup scrub, including OI files initialization */
5454 rc = osd_scrub_setup(env, o);
5458 rc = osd_procfs_init(o, o->od_svname);
5460 CERROR("%s: can't initialize procfs: rc = %d\n",
5462 GOTO(out_scrub, rc);
5465 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5467 /* initialize quota slave instance */
5468 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5470 if (IS_ERR(o->od_quota_slave)) {
5471 rc = PTR_ERR(o->od_quota_slave);
5472 o->od_quota_slave = NULL;
5473 GOTO(out_procfs, rc);
5481 osd_scrub_cleanup(env, o);
5483 lu_site_fini(&o->od_site);
5485 osd_obj_map_fini(o);
5489 cleanup_capa_hash(o->od_capa_hash);
5494 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5495 struct lu_device_type *t,
5496 struct lustre_cfg *cfg)
5498 struct osd_device *o;
5503 return ERR_PTR(-ENOMEM);
5505 rc = dt_device_init(&o->od_dt_dev, t);
5507 /* Because the ctx might be revived in dt_device_init,
5508 * refill the env here */
5509 lu_env_refill((struct lu_env *)env);
5510 rc = osd_device_init0(env, o, cfg);
5512 dt_device_fini(&o->od_dt_dev);
5515 if (unlikely(rc != 0))
5518 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5521 static struct lu_device *osd_device_free(const struct lu_env *env,
5522 struct lu_device *d)
5524 struct osd_device *o = osd_dev(d);
5527 cleanup_capa_hash(o->od_capa_hash);
5528 /* XXX: make osd top device in order to release reference */
5529 d->ld_site->ls_top_dev = d;
5530 lu_site_purge(env, d->ld_site, -1);
5531 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5532 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5533 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5535 lu_site_fini(&o->od_site);
5536 dt_device_fini(&o->od_dt_dev);
5541 static int osd_process_config(const struct lu_env *env,
5542 struct lu_device *d, struct lustre_cfg *cfg)
5544 struct osd_device *o = osd_dev(d);
5548 switch(cfg->lcfg_command) {
5550 err = osd_mount(env, o, cfg);
5553 lu_dev_del_linkage(d->ld_site, d);
5554 err = osd_shutdown(env, o);
5563 static int osd_recovery_complete(const struct lu_env *env,
5564 struct lu_device *d)
5566 struct osd_device *osd = osd_dev(d);
5570 if (osd->od_quota_slave == NULL)
5573 /* start qsd instance on recovery completion, this notifies the quota
5574 * slave code that we are about to process new requests now */
5575 rc = qsd_start(env, osd->od_quota_slave);
5580 * we use exports to track all osd users
5582 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5583 struct obd_device *obd, struct obd_uuid *cluuid,
5584 struct obd_connect_data *data, void *localdata)
5586 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5587 struct lustre_handle conn;
5591 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5593 rc = class_connect(&conn, obd, cluuid);
5597 *exp = class_conn2export(&conn);
5599 spin_lock(&osd->od_osfs_lock);
5601 spin_unlock(&osd->od_osfs_lock);
5607 * once last export (we don't count self-export) disappeared
5608 * osd can be released
5610 static int osd_obd_disconnect(struct obd_export *exp)
5612 struct obd_device *obd = exp->exp_obd;
5613 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5614 int rc, release = 0;
5617 /* Only disconnect the underlying layers on the final disconnect. */
5618 spin_lock(&osd->od_osfs_lock);
5620 if (osd->od_connects == 0)
5622 spin_unlock(&osd->od_osfs_lock);
5624 rc = class_disconnect(exp); /* bz 9811 */
5626 if (rc == 0 && release)
5627 class_manual_cleanup(obd);
5631 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5632 struct lu_device *dev)
5634 struct osd_device *osd = osd_dev(dev);
5638 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5639 /* MDT/MDD still use old infrastructure to create
5641 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5647 if (osd->od_quota_slave != NULL)
5648 /* set up quota slave objects */
5649 result = qsd_prepare(env, osd->od_quota_slave);
5654 static const struct lu_object_operations osd_lu_obj_ops = {
5655 .loo_object_init = osd_object_init,
5656 .loo_object_delete = osd_object_delete,
5657 .loo_object_release = osd_object_release,
5658 .loo_object_free = osd_object_free,
5659 .loo_object_print = osd_object_print,
5660 .loo_object_invariant = osd_object_invariant
5663 const struct lu_device_operations osd_lu_ops = {
5664 .ldo_object_alloc = osd_object_alloc,
5665 .ldo_process_config = osd_process_config,
5666 .ldo_recovery_complete = osd_recovery_complete,
5667 .ldo_prepare = osd_prepare,
5670 static const struct lu_device_type_operations osd_device_type_ops = {
5671 .ldto_init = osd_type_init,
5672 .ldto_fini = osd_type_fini,
5674 .ldto_start = osd_type_start,
5675 .ldto_stop = osd_type_stop,
5677 .ldto_device_alloc = osd_device_alloc,
5678 .ldto_device_free = osd_device_free,
5680 .ldto_device_init = osd_device_init,
5681 .ldto_device_fini = osd_device_fini
5684 struct lu_device_type osd_device_type = {
5685 .ldt_tags = LU_DEVICE_DT,
5686 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5687 .ldt_ops = &osd_device_type_ops,
5688 .ldt_ctx_tags = LCT_LOCAL,
5692 * lprocfs legacy support.
5694 static struct obd_ops osd_obd_device_ops = {
5695 .o_owner = THIS_MODULE,
5696 .o_connect = osd_obd_connect,
5697 .o_disconnect = osd_obd_disconnect
5700 static int __init osd_mod_init(void)
5702 struct lprocfs_static_vars lvars;
5705 lprocfs_osd_init_vars(&lvars);
5706 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5707 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5710 static void __exit osd_mod_exit(void)
5712 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5715 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5716 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5717 MODULE_LICENSE("GPL");
5719 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);