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, 2012, 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))) {
466 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
467 "fid = "DFID", ino = %lu\n",
468 osd_obj2dev(obj)->od_svname,
469 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
470 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
471 obj->oo_inode->i_ino);
474 } else if (rc == -ENODATA) {
475 /* haven't initialize LMA xattr */
483 * Concurrency: no concurrent access is possible that early in object
486 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
487 const struct lu_object_conf *conf)
489 struct osd_object *obj = osd_obj(l);
492 LINVRNT(osd_invariant(obj));
494 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
495 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
496 l->lo_header->loh_attr |= LOHA_EXISTS;
500 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
501 obj->oo_dt.do_body_ops = &osd_body_ops_new;
502 if (result == 0 && obj->oo_inode != NULL) {
503 osd_object_init0(obj);
504 result = osd_check_lma(env, obj);
507 LINVRNT(osd_invariant(obj));
512 * Concurrency: no concurrent access is possible that late in object
515 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
517 struct osd_object *obj = osd_obj(l);
519 LINVRNT(osd_invariant(obj));
521 dt_object_fini(&obj->oo_dt);
522 if (obj->oo_hl_head != NULL)
523 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
528 * Concurrency: no concurrent access is possible that late in object
531 static void osd_index_fini(struct osd_object *o)
533 struct iam_container *bag;
535 if (o->oo_dir != NULL) {
536 bag = &o->oo_dir->od_container;
537 if (o->oo_inode != NULL) {
538 if (bag->ic_object == o->oo_inode)
539 iam_container_fini(bag);
541 OBD_FREE_PTR(o->oo_dir);
547 * Concurrency: no concurrent access is possible that late in object
548 * life-cycle (for all existing callers, that is. New callers have to provide
549 * their own locking.)
551 static int osd_inode_unlinked(const struct inode *inode)
553 return inode->i_nlink == 0;
557 OSD_TXN_OI_DELETE_CREDITS = 20,
558 OSD_TXN_INODE_DELETE_CREDITS = 20
565 #if OSD_THANDLE_STATS
567 * Set time when the handle is allocated
569 static void osd_th_alloced(struct osd_thandle *oth)
571 oth->oth_alloced = cfs_time_current();
575 * Set time when the handle started
577 static void osd_th_started(struct osd_thandle *oth)
579 oth->oth_started = cfs_time_current();
583 * Helper function to convert time interval to microseconds packed in
586 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
590 cfs_duration_usec(cfs_time_sub(end, start), &val);
591 return val.tv_sec * 1000000 + val.tv_usec;
595 * Check whether the we deal with this handle for too long.
597 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
598 cfs_time_t alloced, cfs_time_t started,
601 cfs_time_t now = cfs_time_current();
603 LASSERT(dev != NULL);
605 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
606 interval_to_usec(alloced, started));
607 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
608 interval_to_usec(started, closed));
609 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
610 interval_to_usec(closed, now));
612 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
613 CWARN("transaction handle %p was open for too long: "
615 "alloced "CFS_TIME_T" ,"
616 "started "CFS_TIME_T" ,"
617 "closed "CFS_TIME_T"\n",
618 oth, now, alloced, started, closed);
619 libcfs_debug_dumpstack(NULL);
623 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
625 cfs_time_t __closed = cfs_time_current(); \
626 cfs_time_t __alloced = oth->oth_alloced; \
627 cfs_time_t __started = oth->oth_started; \
630 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
633 #else /* OSD_THANDLE_STATS */
635 #define osd_th_alloced(h) do {} while(0)
636 #define osd_th_started(h) do {} while(0)
637 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
639 #endif /* OSD_THANDLE_STATS */
642 * Concurrency: doesn't access mutable data.
644 static int osd_param_is_not_sane(const struct osd_device *dev,
645 const struct thandle *th)
647 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
649 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
653 * Concurrency: shouldn't matter.
655 static void osd_trans_commit_cb(struct super_block *sb,
656 struct ldiskfs_journal_cb_entry *jcb, int error)
658 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
659 struct thandle *th = &oh->ot_super;
660 struct lu_device *lud = &th->th_dev->dd_lu_dev;
661 struct dt_txn_commit_cb *dcb, *tmp;
663 LASSERT(oh->ot_handle == NULL);
666 CERROR("transaction @0x%p commit error: %d\n", th, error);
668 dt_txn_hook_commit(th);
670 /* call per-transaction callbacks if any */
671 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
672 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
673 "commit callback entry: magic=%x name='%s'\n",
674 dcb->dcb_magic, dcb->dcb_name);
675 cfs_list_del_init(&dcb->dcb_linkage);
676 dcb->dcb_func(NULL, th, dcb, error);
679 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
683 lu_context_exit(&th->th_ctx);
684 lu_context_fini(&th->th_ctx);
688 static struct thandle *osd_trans_create(const struct lu_env *env,
691 struct osd_thread_info *oti = osd_oti_get(env);
692 struct osd_iobuf *iobuf = &oti->oti_iobuf;
693 struct osd_thandle *oh;
697 /* on pending IO in this thread should left from prev. request */
698 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
700 th = ERR_PTR(-ENOMEM);
701 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
703 oh->ot_quota_trans = &oti->oti_quota_trans;
704 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
708 th->th_tags = LCT_TX_HANDLE;
710 oti->oti_dev = osd_dt_dev(d);
711 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
714 memset(oti->oti_declare_ops, 0,
715 sizeof(oti->oti_declare_ops));
716 memset(oti->oti_declare_ops_rb, 0,
717 sizeof(oti->oti_declare_ops_rb));
718 memset(oti->oti_declare_ops_cred, 0,
719 sizeof(oti->oti_declare_ops_cred));
720 oti->oti_rollback = false;
726 * Concurrency: shouldn't matter.
728 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
731 struct osd_thread_info *oti = osd_oti_get(env);
732 struct osd_device *dev = osd_dt_dev(d);
734 struct osd_thandle *oh;
739 LASSERT(current->journal_info == NULL);
741 oh = container_of0(th, struct osd_thandle, ot_super);
743 LASSERT(oh->ot_handle == NULL);
745 rc = dt_txn_hook_start(env, d, th);
749 if (unlikely(osd_param_is_not_sane(dev, th))) {
750 static unsigned long last_printed;
751 static int last_credits;
753 CWARN("%.16s: too many transaction credits (%d > %d)\n",
754 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
756 osd_journal(dev)->j_max_transaction_buffers);
757 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
758 oti->oti_declare_ops[OSD_OT_CREATE],
759 oti->oti_declare_ops_cred[OSD_OT_CREATE],
760 oti->oti_declare_ops[OSD_OT_DELETE],
761 oti->oti_declare_ops_cred[OSD_OT_DELETE],
762 oti->oti_declare_ops[OSD_OT_DESTROY],
763 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
764 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
765 oti->oti_declare_ops[OSD_OT_ATTR_SET],
766 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
767 oti->oti_declare_ops[OSD_OT_XATTR_SET],
768 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
769 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
770 oti->oti_declare_ops[OSD_OT_WRITE],
771 oti->oti_declare_ops_cred[OSD_OT_WRITE],
772 oti->oti_declare_ops[OSD_OT_PUNCH],
773 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
774 oti->oti_declare_ops[OSD_OT_QUOTA],
775 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
776 CWARN(" insert: %u/%u, delete: %u/%u\n",
777 oti->oti_declare_ops[OSD_OT_INSERT],
778 oti->oti_declare_ops_cred[OSD_OT_INSERT],
779 oti->oti_declare_ops[OSD_OT_DESTROY],
780 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
781 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
782 oti->oti_declare_ops[OSD_OT_REF_ADD],
783 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
784 oti->oti_declare_ops[OSD_OT_REF_DEL],
785 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
787 if (last_credits != oh->ot_credits &&
788 time_after(jiffies, last_printed + 60 * HZ)) {
789 libcfs_debug_dumpstack(NULL);
790 last_credits = oh->ot_credits;
791 last_printed = jiffies;
793 /* XXX Limit the credits to 'max_transaction_buffers', and
794 * let the underlying filesystem to catch the error if
795 * we really need so many credits.
797 * This should be removed when we can calculate the
798 * credits precisely. */
799 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
803 * XXX temporary stuff. Some abstraction layer should
806 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
810 LASSERT(oti->oti_txns == 0);
811 lu_context_init(&th->th_ctx, th->th_tags);
812 lu_context_enter(&th->th_ctx);
814 lu_device_get(&d->dd_lu_dev);
815 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
827 * Concurrency: shouldn't matter.
829 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
832 struct osd_thandle *oh;
833 struct osd_thread_info *oti = osd_oti_get(env);
834 struct osd_iobuf *iobuf = &oti->oti_iobuf;
835 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
838 oh = container_of0(th, struct osd_thandle, ot_super);
841 /* inform the quota slave device that the transaction is
843 qsd_op_end(env, qsd, oh->ot_quota_trans);
844 oh->ot_quota_trans = NULL;
846 if (oh->ot_handle != NULL) {
847 handle_t *hdl = oh->ot_handle;
850 * add commit callback
851 * notice we don't do this in osd_trans_start()
852 * as underlying transaction can change during truncate
854 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
857 LASSERT(oti->oti_txns == 1);
859 rc = dt_txn_hook_stop(env, th);
861 CERROR("Failure in transaction hook: %d\n", rc);
863 /* hook functions might modify th_sync */
864 hdl->h_sync = th->th_sync;
866 oh->ot_handle = NULL;
867 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
868 rc = ldiskfs_journal_stop(hdl));
870 CERROR("Failure to stop transaction: %d\n", rc);
875 /* as we want IO to journal and data IO be concurrent, we don't block
876 * awaiting data IO completion in osd_do_bio(), instead we wait here
877 * once transaction is submitted to the journal. all reqular requests
878 * don't do direct IO (except read/write), thus this wait_event becomes
881 * IMPORTANT: we have to wait till any IO submited by the thread is
882 * completed otherwise iobuf may be corrupted by different request
884 cfs_wait_event(iobuf->dr_wait,
885 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
887 rc = iobuf->dr_error;
892 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
894 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
897 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
898 LASSERT(&dcb->dcb_func != NULL);
899 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
905 * Called just before object is freed. Releases all resources except for
906 * object itself (that is released by osd_object_free()).
908 * Concurrency: no concurrent access is possible that late in object
911 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
913 struct osd_object *obj = osd_obj(l);
914 struct inode *inode = obj->oo_inode;
916 LINVRNT(osd_invariant(obj));
919 * If object is unlinked remove fid->ino mapping from object index.
924 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
925 qid_t uid = inode->i_uid;
926 qid_t gid = inode->i_gid;
929 obj->oo_inode = NULL;
932 struct osd_thread_info *info = osd_oti_get(env);
933 struct lquota_id_info *qi = &info->oti_qi;
935 /* Release granted quota to master if necessary */
936 qi->lqi_id.qid_uid = uid;
937 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
939 qi->lqi_id.qid_uid = gid;
940 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
946 * Concurrency: ->loo_object_release() is called under site spin-lock.
948 static void osd_object_release(const struct lu_env *env,
954 * Concurrency: shouldn't matter.
956 static int osd_object_print(const struct lu_env *env, void *cookie,
957 lu_printer_t p, const struct lu_object *l)
959 struct osd_object *o = osd_obj(l);
962 if (o->oo_dir != NULL)
963 d = o->oo_dir->od_container.ic_descr;
966 return (*p)(env, cookie,
967 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
969 o->oo_inode ? o->oo_inode->i_ino : 0UL,
970 o->oo_inode ? o->oo_inode->i_generation : 0,
971 d ? d->id_ops->id_name : "plain");
975 * Concurrency: shouldn't matter.
977 int osd_statfs(const struct lu_env *env, struct dt_device *d,
978 struct obd_statfs *sfs)
980 struct osd_device *osd = osd_dt_dev(d);
981 struct super_block *sb = osd_sb(osd);
982 struct kstatfs *ksfs;
985 if (unlikely(osd->od_mnt == NULL))
988 /* osd_lproc.c call this without env, allocate ksfs for that case */
989 if (unlikely(env == NULL)) {
994 ksfs = &osd_oti_get(env)->oti_ksfs;
997 spin_lock(&osd->od_osfs_lock);
999 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1000 result = sb->s_op->statfs(sb->s_root, ksfs);
1001 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1002 osd->od_osfs_age = cfs_time_current_64();
1003 statfs_pack(&osd->od_statfs, ksfs);
1004 if (sb->s_flags & MS_RDONLY)
1005 sfs->os_state = OS_STATE_READONLY;
1009 if (likely(result == 0))
1010 *sfs = osd->od_statfs;
1011 spin_unlock(&osd->od_osfs_lock);
1013 if (unlikely(env == NULL))
1020 * Estimate space needed for file creations. We assume the largest filename
1021 * which is 2^64 - 1, hence a filename of 20 chars.
1022 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1024 #ifdef __LDISKFS_DIR_REC_LEN
1025 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1027 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1031 * Concurrency: doesn't access mutable data.
1033 static void osd_conf_get(const struct lu_env *env,
1034 const struct dt_device *dev,
1035 struct dt_device_param *param)
1037 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1040 * XXX should be taken from not-yet-existing fs abstraction layer.
1042 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1043 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1044 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1045 param->ddp_block_shift = sb->s_blocksize_bits;
1046 param->ddp_mount_type = LDD_MT_LDISKFS;
1047 param->ddp_maxbytes = sb->s_maxbytes;
1048 /* Overhead estimate should be fairly accurate, so we really take a tiny
1049 * error margin which also avoids fragmenting the filesystem too much */
1050 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1051 /* inode are statically allocated, so per-inode space consumption
1052 * is the space consumed by the directory entry */
1053 param->ddp_inodespace = PER_OBJ_USAGE;
1054 /* per-fragment overhead to be used by the client code */
1055 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1056 param->ddp_mntopts = 0;
1057 if (test_opt(sb, XATTR_USER))
1058 param->ddp_mntopts |= MNTOPT_USERXATTR;
1059 if (test_opt(sb, POSIX_ACL))
1060 param->ddp_mntopts |= MNTOPT_ACL;
1062 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1063 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1064 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1067 param->ddp_max_ea_size = sb->s_blocksize;
1072 * Concurrency: shouldn't matter.
1074 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1076 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1077 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1081 * Start commit for OSD device.
1083 * An implementation of dt_commit_async method for OSD device.
1084 * Asychronously starts underlayng fs sync and thereby a transaction
1087 * \param env environment
1088 * \param d dt device
1090 * \see dt_device_operations
1092 static int osd_commit_async(const struct lu_env *env,
1093 struct dt_device *d)
1095 struct super_block *s = osd_sb(osd_dt_dev(d));
1098 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1099 RETURN(s->s_op->sync_fs(s, 0));
1103 * Concurrency: shouldn't matter.
1106 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1108 struct super_block *sb = osd_sb(osd_dt_dev(d));
1109 struct block_device *dev = sb->s_bdev;
1110 #ifdef HAVE_DEV_SET_RDONLY
1111 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1114 int rc = -EOPNOTSUPP;
1118 #ifdef HAVE_DEV_SET_RDONLY
1119 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1121 if (jdev && (jdev != dev)) {
1122 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1124 dev_set_rdonly(jdev);
1126 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1127 dev_set_rdonly(dev);
1129 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1130 osd_dt_dev(d)->od_svname, (long)dev, rc);
1136 * Concurrency: serialization provided by callers.
1138 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1139 int mode, unsigned long timeout, __u32 alg,
1140 struct lustre_capa_key *keys)
1142 struct osd_device *dev = osd_dt_dev(d);
1145 dev->od_fl_capa = mode;
1146 dev->od_capa_timeout = timeout;
1147 dev->od_capa_alg = alg;
1148 dev->od_capa_keys = keys;
1153 * Note: we do not count into QUOTA here.
1154 * If we mount with --data_journal we may need more.
1156 const int osd_dto_credits_noquota[DTO_NR] = {
1159 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1160 * SINGLEDATA_TRANS_BLOCKS(8)
1161 * XXX Note: maybe iam need more, since iam have more level than
1164 [DTO_INDEX_INSERT] = 16,
1165 [DTO_INDEX_DELETE] = 16,
1169 [DTO_INDEX_UPDATE] = 16,
1171 * Create a object. The same as create object in EXT3.
1172 * DATA_TRANS_BLOCKS(14) +
1173 * INDEX_EXTRA_BLOCKS(8) +
1174 * 3(inode bits, groups, GDT)
1176 [DTO_OBJECT_CREATE] = 25,
1178 * XXX: real credits to be fixed
1180 [DTO_OBJECT_DELETE] = 25,
1182 * Attr set credits (inode)
1184 [DTO_ATTR_SET_BASE] = 1,
1186 * Xattr set. The same as xattr of EXT3.
1187 * DATA_TRANS_BLOCKS(14)
1188 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1189 * are also counted in. Do not know why?
1191 [DTO_XATTR_SET] = 14,
1194 * credits for inode change during write.
1196 [DTO_WRITE_BASE] = 3,
1198 * credits for single block write.
1200 [DTO_WRITE_BLOCK] = 14,
1202 * Attr set credits for chown.
1203 * This is extra credits for setattr, and it is null without quota
1205 [DTO_ATTR_SET_CHOWN]= 0
1208 static const struct dt_device_operations osd_dt_ops = {
1209 .dt_root_get = osd_root_get,
1210 .dt_statfs = osd_statfs,
1211 .dt_trans_create = osd_trans_create,
1212 .dt_trans_start = osd_trans_start,
1213 .dt_trans_stop = osd_trans_stop,
1214 .dt_trans_cb_add = osd_trans_cb_add,
1215 .dt_conf_get = osd_conf_get,
1216 .dt_sync = osd_sync,
1218 .dt_commit_async = osd_commit_async,
1219 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1222 static void osd_object_read_lock(const struct lu_env *env,
1223 struct dt_object *dt, unsigned role)
1225 struct osd_object *obj = osd_dt_obj(dt);
1226 struct osd_thread_info *oti = osd_oti_get(env);
1228 LINVRNT(osd_invariant(obj));
1230 LASSERT(obj->oo_owner != env);
1231 down_read_nested(&obj->oo_sem, role);
1233 LASSERT(obj->oo_owner == NULL);
1237 static void osd_object_write_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_write_nested(&obj->oo_sem, role);
1248 LASSERT(obj->oo_owner == NULL);
1249 obj->oo_owner = env;
1253 static void osd_object_read_unlock(const struct lu_env *env,
1254 struct dt_object *dt)
1256 struct osd_object *obj = osd_dt_obj(dt);
1257 struct osd_thread_info *oti = osd_oti_get(env);
1259 LINVRNT(osd_invariant(obj));
1261 LASSERT(oti->oti_r_locks > 0);
1263 up_read(&obj->oo_sem);
1266 static void osd_object_write_unlock(const struct lu_env *env,
1267 struct dt_object *dt)
1269 struct osd_object *obj = osd_dt_obj(dt);
1270 struct osd_thread_info *oti = osd_oti_get(env);
1272 LINVRNT(osd_invariant(obj));
1274 LASSERT(obj->oo_owner == env);
1275 LASSERT(oti->oti_w_locks > 0);
1277 obj->oo_owner = NULL;
1278 up_write(&obj->oo_sem);
1281 static int osd_object_write_locked(const struct lu_env *env,
1282 struct dt_object *dt)
1284 struct osd_object *obj = osd_dt_obj(dt);
1286 LINVRNT(osd_invariant(obj));
1288 return obj->oo_owner == env;
1291 static int capa_is_sane(const struct lu_env *env,
1292 struct osd_device *dev,
1293 struct lustre_capa *capa,
1294 struct lustre_capa_key *keys)
1296 struct osd_thread_info *oti = osd_oti_get(env);
1297 struct lustre_capa *tcapa = &oti->oti_capa;
1298 struct obd_capa *oc;
1302 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1304 if (capa_is_expired(oc)) {
1305 DEBUG_CAPA(D_ERROR, capa, "expired");
1312 if (capa_is_expired_sec(capa)) {
1313 DEBUG_CAPA(D_ERROR, capa, "expired");
1317 spin_lock(&capa_lock);
1318 for (i = 0; i < 2; i++) {
1319 if (keys[i].lk_keyid == capa->lc_keyid) {
1320 oti->oti_capa_key = keys[i];
1324 spin_unlock(&capa_lock);
1327 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1331 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1335 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1336 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1340 oc = capa_add(dev->od_capa_hash, capa);
1346 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1347 struct lustre_capa *capa, __u64 opc)
1349 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1350 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1351 struct md_capainfo *ci;
1354 if (!dev->od_fl_capa)
1357 if (capa == BYPASS_CAPA)
1360 ci = md_capainfo(env);
1364 if (ci->mc_auth == LC_ID_NONE)
1368 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1372 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1373 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1378 if (!capa_opc_supported(capa, opc)) {
1379 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1383 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1384 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1391 static struct timespec *osd_inode_time(const struct lu_env *env,
1392 struct inode *inode, __u64 seconds)
1394 struct osd_thread_info *oti = osd_oti_get(env);
1395 struct timespec *t = &oti->oti_time;
1397 t->tv_sec = seconds;
1399 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1404 static void osd_inode_getattr(const struct lu_env *env,
1405 struct inode *inode, struct lu_attr *attr)
1407 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1408 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1409 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1412 attr->la_atime = LTIME_S(inode->i_atime);
1413 attr->la_mtime = LTIME_S(inode->i_mtime);
1414 attr->la_ctime = LTIME_S(inode->i_ctime);
1415 attr->la_mode = inode->i_mode;
1416 attr->la_size = i_size_read(inode);
1417 attr->la_blocks = inode->i_blocks;
1418 attr->la_uid = inode->i_uid;
1419 attr->la_gid = inode->i_gid;
1420 attr->la_flags = LDISKFS_I(inode)->i_flags;
1421 attr->la_nlink = inode->i_nlink;
1422 attr->la_rdev = inode->i_rdev;
1423 attr->la_blksize = 1 << inode->i_blkbits;
1424 attr->la_blkbits = inode->i_blkbits;
1427 static int osd_attr_get(const struct lu_env *env,
1428 struct dt_object *dt,
1429 struct lu_attr *attr,
1430 struct lustre_capa *capa)
1432 struct osd_object *obj = osd_dt_obj(dt);
1434 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1435 LINVRNT(osd_invariant(obj));
1437 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1440 spin_lock(&obj->oo_guard);
1441 osd_inode_getattr(env, obj->oo_inode, attr);
1442 spin_unlock(&obj->oo_guard);
1446 static int osd_declare_attr_set(const struct lu_env *env,
1447 struct dt_object *dt,
1448 const struct lu_attr *attr,
1449 struct thandle *handle)
1451 struct osd_thandle *oh;
1452 struct osd_object *obj;
1453 struct osd_thread_info *info = osd_oti_get(env);
1454 struct lquota_id_info *qi = &info->oti_qi;
1460 LASSERT(dt != NULL);
1461 LASSERT(handle != NULL);
1463 obj = osd_dt_obj(dt);
1464 LASSERT(osd_invariant(obj));
1466 oh = container_of0(handle, struct osd_thandle, ot_super);
1467 LASSERT(oh->ot_handle == NULL);
1469 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1470 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1472 if (attr == NULL || obj->oo_inode == NULL)
1475 bspace = obj->oo_inode->i_blocks;
1476 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1477 bspace = toqb(bspace);
1479 /* Changing ownership is always preformed by super user, it should not
1482 * We still need to call the osd_declare_qid() to calculate the journal
1483 * credits for updating quota accounting files and to trigger quota
1484 * space adjustment once the operation is completed.*/
1485 if ((attr->la_valid & LA_UID) != 0 &&
1486 attr->la_uid != obj->oo_inode->i_uid) {
1487 qi->lqi_type = USRQUOTA;
1489 /* inode accounting */
1490 qi->lqi_is_blk = false;
1492 /* one more inode for the new owner ... */
1493 qi->lqi_id.qid_uid = attr->la_uid;
1495 allocated = (attr->la_uid == 0) ? true : false;
1496 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1497 if (rc == -EDQUOT || rc == -EINPROGRESS)
1502 /* and one less inode for the current uid */
1503 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1505 rc = osd_declare_qid(env, oh, qi, true, NULL);
1506 if (rc == -EDQUOT || rc == -EINPROGRESS)
1511 /* block accounting */
1512 qi->lqi_is_blk = true;
1514 /* more blocks for the new owner ... */
1515 qi->lqi_id.qid_uid = attr->la_uid;
1516 qi->lqi_space = bspace;
1517 allocated = (attr->la_uid == 0) ? true : false;
1518 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1519 if (rc == -EDQUOT || rc == -EINPROGRESS)
1524 /* and finally less blocks for the current owner */
1525 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1526 qi->lqi_space = -bspace;
1527 rc = osd_declare_qid(env, oh, qi, true, NULL);
1528 if (rc == -EDQUOT || rc == -EINPROGRESS)
1534 if (attr->la_valid & LA_GID &&
1535 attr->la_gid != obj->oo_inode->i_gid) {
1536 qi->lqi_type = GRPQUOTA;
1538 /* inode accounting */
1539 qi->lqi_is_blk = false;
1541 /* one more inode for the new group owner ... */
1542 qi->lqi_id.qid_gid = attr->la_gid;
1544 allocated = (attr->la_gid == 0) ? true : false;
1545 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1546 if (rc == -EDQUOT || rc == -EINPROGRESS)
1551 /* and one less inode for the current gid */
1552 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1554 rc = osd_declare_qid(env, oh, qi, true, NULL);
1555 if (rc == -EDQUOT || rc == -EINPROGRESS)
1560 /* block accounting */
1561 qi->lqi_is_blk = true;
1563 /* more blocks for the new owner ... */
1564 qi->lqi_id.qid_gid = attr->la_gid;
1565 qi->lqi_space = bspace;
1566 allocated = (attr->la_gid == 0) ? true : false;
1567 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1568 if (rc == -EDQUOT || rc == -EINPROGRESS)
1573 /* and finally less blocks for the current owner */
1574 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1575 qi->lqi_space = -bspace;
1576 rc = osd_declare_qid(env, oh, qi, true, NULL);
1577 if (rc == -EDQUOT || rc == -EINPROGRESS)
1586 static int osd_inode_setattr(const struct lu_env *env,
1587 struct inode *inode, const struct lu_attr *attr)
1591 bits = attr->la_valid;
1593 if (bits & LA_ATIME)
1594 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1595 if (bits & LA_CTIME)
1596 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1597 if (bits & LA_MTIME)
1598 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1599 if (bits & LA_SIZE) {
1600 LDISKFS_I(inode)->i_disksize = attr->la_size;
1601 i_size_write(inode, attr->la_size);
1605 /* OSD should not change "i_blocks" which is used by quota.
1606 * "i_blocks" should be changed by ldiskfs only. */
1607 if (bits & LA_BLOCKS)
1608 inode->i_blocks = attr->la_blocks;
1611 inode->i_mode = (inode->i_mode & S_IFMT) |
1612 (attr->la_mode & ~S_IFMT);
1614 inode->i_uid = attr->la_uid;
1616 inode->i_gid = attr->la_gid;
1617 if (bits & LA_NLINK)
1618 set_nlink(inode, attr->la_nlink);
1620 inode->i_rdev = attr->la_rdev;
1622 if (bits & LA_FLAGS) {
1623 /* always keep S_NOCMTIME */
1624 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1630 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1632 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1633 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1638 if (attr->la_valid & LA_UID)
1639 iattr.ia_valid |= ATTR_UID;
1640 if (attr->la_valid & LA_GID)
1641 iattr.ia_valid |= ATTR_GID;
1642 iattr.ia_uid = attr->la_uid;
1643 iattr.ia_gid = attr->la_gid;
1645 rc = ll_vfs_dq_transfer(inode, &iattr);
1647 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1648 "enforcement enabled on the ldiskfs filesystem?",
1649 inode->i_sb->s_id, rc);
1656 static int osd_attr_set(const struct lu_env *env,
1657 struct dt_object *dt,
1658 const struct lu_attr *attr,
1659 struct thandle *handle,
1660 struct lustre_capa *capa)
1662 struct osd_object *obj = osd_dt_obj(dt);
1663 struct inode *inode;
1666 LASSERT(handle != NULL);
1667 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1668 LASSERT(osd_invariant(obj));
1670 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1673 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1675 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1676 struct osd_thread_info *oti = osd_oti_get(env);
1677 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1678 struct lu_fid *fid1 = &oti->oti_fid;
1679 struct osd_inode_id *id = &oti->oti_id;
1680 struct iam_path_descr *ipd;
1681 struct iam_container *bag;
1682 struct osd_thandle *oh;
1685 fid_cpu_to_be(fid1, fid0);
1686 memset(id, 1, sizeof(*id));
1687 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1688 fid0)->oi_dir.od_container;
1689 ipd = osd_idx_ipd_get(env, bag);
1690 if (unlikely(ipd == NULL))
1693 oh = container_of0(handle, struct osd_thandle, ot_super);
1694 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1695 (const struct iam_rec *)id, ipd);
1696 osd_ipd_put(env, bag, ipd);
1697 return(rc > 0 ? 0 : rc);
1700 inode = obj->oo_inode;
1701 ll_vfs_dq_init(inode);
1703 rc = osd_quota_transfer(inode, attr);
1707 spin_lock(&obj->oo_guard);
1708 rc = osd_inode_setattr(env, inode, attr);
1709 spin_unlock(&obj->oo_guard);
1712 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1716 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1717 struct osd_object *obj,
1718 const char *name, const int namelen)
1720 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1723 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1725 struct dt_allocation_hint *hint,
1729 struct osd_device *osd = osd_obj2dev(obj);
1730 struct osd_thandle *oth;
1731 struct dt_object *parent = NULL;
1732 struct inode *inode;
1734 LINVRNT(osd_invariant(obj));
1735 LASSERT(obj->oo_inode == NULL);
1736 LASSERT(obj->oo_hl_head == NULL);
1738 if (S_ISDIR(mode) && ldiskfs_pdo) {
1739 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1740 if (obj->oo_hl_head == NULL)
1744 oth = container_of(th, struct osd_thandle, ot_super);
1745 LASSERT(oth->ot_handle->h_transaction != NULL);
1747 if (hint && hint->dah_parent)
1748 parent = hint->dah_parent;
1750 inode = ldiskfs_create_inode(oth->ot_handle,
1751 parent ? osd_dt_obj(parent)->oo_inode :
1752 osd_sb(osd)->s_root->d_inode,
1754 if (!IS_ERR(inode)) {
1755 /* Do not update file c/mtime in ldiskfs.
1756 * NB: don't need any lock because no contention at this
1758 inode->i_flags |= S_NOCMTIME;
1760 /* For new created object, it must be consistent,
1761 * and it is unnecessary to scrub against it. */
1762 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1763 obj->oo_inode = inode;
1766 if (obj->oo_hl_head != NULL) {
1767 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1768 obj->oo_hl_head = NULL;
1770 result = PTR_ERR(inode);
1772 LINVRNT(osd_invariant(obj));
1780 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1781 struct lu_attr *attr,
1782 struct dt_allocation_hint *hint,
1783 struct dt_object_format *dof,
1787 struct osd_thandle *oth;
1788 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1790 LASSERT(S_ISDIR(attr->la_mode));
1792 oth = container_of(th, struct osd_thandle, ot_super);
1793 LASSERT(oth->ot_handle->h_transaction != NULL);
1794 result = osd_mkfile(info, obj, mode, hint, th);
1799 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1800 struct lu_attr *attr,
1801 struct dt_allocation_hint *hint,
1802 struct dt_object_format *dof,
1806 struct osd_thandle *oth;
1807 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1809 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1811 LASSERT(S_ISREG(attr->la_mode));
1813 oth = container_of(th, struct osd_thandle, ot_super);
1814 LASSERT(oth->ot_handle->h_transaction != NULL);
1816 result = osd_mkfile(info, obj, mode, hint, th);
1818 LASSERT(obj->oo_inode != NULL);
1819 if (feat->dif_flags & DT_IND_VARKEY)
1820 result = iam_lvar_create(obj->oo_inode,
1821 feat->dif_keysize_max,
1823 feat->dif_recsize_max,
1826 result = iam_lfix_create(obj->oo_inode,
1827 feat->dif_keysize_max,
1829 feat->dif_recsize_max,
1836 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1837 struct lu_attr *attr,
1838 struct dt_allocation_hint *hint,
1839 struct dt_object_format *dof,
1842 LASSERT(S_ISREG(attr->la_mode));
1843 return osd_mkfile(info, obj, (attr->la_mode &
1844 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1847 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1848 struct lu_attr *attr,
1849 struct dt_allocation_hint *hint,
1850 struct dt_object_format *dof,
1853 LASSERT(S_ISLNK(attr->la_mode));
1854 return osd_mkfile(info, obj, (attr->la_mode &
1855 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1858 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1859 struct lu_attr *attr,
1860 struct dt_allocation_hint *hint,
1861 struct dt_object_format *dof,
1864 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1867 LINVRNT(osd_invariant(obj));
1868 LASSERT(obj->oo_inode == NULL);
1869 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1870 S_ISFIFO(mode) || S_ISSOCK(mode));
1872 result = osd_mkfile(info, obj, mode, hint, th);
1874 LASSERT(obj->oo_inode != NULL);
1876 * This inode should be marked dirty for i_rdev. Currently
1877 * that is done in the osd_attr_init().
1879 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1882 LINVRNT(osd_invariant(obj));
1886 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1888 struct dt_allocation_hint *hint,
1889 struct dt_object_format *dof,
1892 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1894 osd_obj_type_f result;
1910 result = osd_mk_index;
1921 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1922 struct dt_object *parent, struct dt_object *child,
1923 cfs_umode_t child_mode)
1927 memset(ah, 0, sizeof(*ah));
1928 ah->dah_parent = parent;
1929 ah->dah_mode = child_mode;
1932 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1933 struct lu_attr *attr, struct dt_object_format *dof)
1935 struct inode *inode = obj->oo_inode;
1936 __u64 valid = attr->la_valid;
1939 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1941 if (dof->dof_type != DFT_NODE)
1942 attr->la_valid &= ~LA_RDEV;
1943 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1944 attr->la_valid &= ~LA_ATIME;
1945 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1946 attr->la_valid &= ~LA_CTIME;
1947 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1948 attr->la_valid &= ~LA_MTIME;
1950 result = osd_quota_transfer(inode, attr);
1954 if (attr->la_valid != 0) {
1955 result = osd_inode_setattr(info->oti_env, inode, attr);
1957 * The osd_inode_setattr() should always succeed here. The
1958 * only error that could be returned is EDQUOT when we are
1959 * trying to change the UID or GID of the inode. However, this
1960 * should not happen since quota enforcement is no longer
1961 * enabled on ldiskfs (lquota takes care of it).
1963 LASSERTF(result == 0, "%d", result);
1964 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1967 attr->la_valid = valid;
1971 * Helper function for osd_object_create()
1973 * \retval 0, on success
1975 static int __osd_object_create(struct osd_thread_info *info,
1976 struct osd_object *obj, struct lu_attr *attr,
1977 struct dt_allocation_hint *hint,
1978 struct dt_object_format *dof,
1984 /* we drop umask so that permissions we pass are not affected */
1985 umask = current->fs->umask;
1986 current->fs->umask = 0;
1988 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1991 osd_attr_init(info, obj, attr, dof);
1992 osd_object_init0(obj);
1994 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1995 unlock_new_inode(obj->oo_inode);
1998 /* restore previous umask value */
1999 current->fs->umask = umask;
2005 * Helper function for osd_object_create()
2007 * \retval 0, on success
2009 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2010 const struct lu_fid *fid, struct thandle *th)
2012 struct osd_thread_info *info = osd_oti_get(env);
2013 struct osd_inode_id *id = &info->oti_id;
2014 struct osd_device *osd = osd_obj2dev(obj);
2016 LASSERT(obj->oo_inode != NULL);
2018 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2019 return osd_oi_insert(info, osd, fid, id, th);
2022 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2023 const struct lu_fid *fid, struct lu_seq_range *range)
2025 struct seq_server_site *ss = osd_seq_site(osd);
2028 if (fid_is_idif(fid)) {
2029 fld_range_set_ost(range);
2030 range->lsr_index = fid_idif_ost_idx(fid);
2034 if (!fid_seq_in_fldb(fid_seq(fid))) {
2035 fld_range_set_mdt(range);
2037 /* FIXME: If ss is NULL, it suppose not get lsr_index
2039 range->lsr_index = ss->ss_node_id;
2043 LASSERT(ss != NULL);
2044 fld_range_set_any(range);
2045 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2047 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
2048 osd_name(osd), PFID(fid), rc);
2054 * Concurrency: no external locking is necessary.
2056 static int osd_declare_object_create(const struct lu_env *env,
2057 struct dt_object *dt,
2058 struct lu_attr *attr,
2059 struct dt_allocation_hint *hint,
2060 struct dt_object_format *dof,
2061 struct thandle *handle)
2063 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2064 struct osd_thandle *oh;
2068 LASSERT(handle != NULL);
2070 oh = container_of0(handle, struct osd_thandle, ot_super);
2071 LASSERT(oh->ot_handle == NULL);
2073 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2074 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2075 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2076 lu_object_fid(&dt->do_lu)))
2077 /* Reuse idle OI block may cause additional one OI block
2079 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2080 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2082 /* If this is directory, then we expect . and .. to be inserted as
2083 * well. The one directory block always needs to be created for the
2084 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2085 * block), there is no danger of needing a tree for the first block.
2087 if (attr && S_ISDIR(attr->la_mode)) {
2088 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2089 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2090 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2096 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2097 false, false, NULL, false);
2101 /* It does fld look up inside declare, and the result will be
2102 * added to fld cache, so the following fld lookup inside insert
2103 * does not need send RPC anymore, so avoid send rpc with holding
2105 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2106 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2107 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2108 lu_object_fid(&dt->do_lu), range);
2114 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2115 struct lu_attr *attr,
2116 struct dt_allocation_hint *hint,
2117 struct dt_object_format *dof,
2120 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2121 struct osd_object *obj = osd_dt_obj(dt);
2122 struct osd_thread_info *info = osd_oti_get(env);
2127 LINVRNT(osd_invariant(obj));
2128 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2129 LASSERT(osd_write_locked(env, obj));
2130 LASSERT(th != NULL);
2132 if (unlikely(fid_is_acct(fid)))
2133 /* Quota files can't be created from the kernel any more,
2134 * 'tune2fs -O quota' will take care of creating them */
2137 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2138 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2140 result = __osd_object_create(info, obj, attr, hint, dof, th);
2142 result = __osd_oi_insert(env, obj, fid, th);
2144 LASSERT(ergo(result == 0,
2145 dt_object_exists(dt) && !dt_object_remote(dt)));
2147 LASSERT(osd_invariant(obj));
2152 * Called to destroy on-disk representation of the object
2154 * Concurrency: must be locked
2156 static int osd_declare_object_destroy(const struct lu_env *env,
2157 struct dt_object *dt,
2160 struct osd_object *obj = osd_dt_obj(dt);
2161 struct inode *inode = obj->oo_inode;
2162 struct osd_thandle *oh;
2166 oh = container_of0(th, struct osd_thandle, ot_super);
2167 LASSERT(oh->ot_handle == NULL);
2170 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2171 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2172 /* Recycle idle OI leaf may cause additional three OI blocks
2174 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2175 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2176 /* one less inode */
2177 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2178 false, true, NULL, false);
2181 /* data to be truncated */
2182 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2183 true, true, NULL, false);
2187 static int osd_object_destroy(const struct lu_env *env,
2188 struct dt_object *dt,
2191 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2192 struct osd_object *obj = osd_dt_obj(dt);
2193 struct inode *inode = obj->oo_inode;
2194 struct osd_device *osd = osd_obj2dev(obj);
2195 struct osd_thandle *oh;
2199 oh = container_of0(th, struct osd_thandle, ot_super);
2200 LASSERT(oh->ot_handle);
2202 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2204 if (unlikely(fid_is_acct(fid)))
2207 if (S_ISDIR(inode->i_mode)) {
2208 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2209 /* it will check/delete the inode from remote parent,
2210 * how to optimize it? unlink performance impaction XXX */
2211 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2212 if (result != 0 && result != -ENOENT) {
2213 CERROR("%s: delete inode "DFID": rc = %d\n",
2214 osd_name(osd), PFID(fid), result);
2216 spin_lock(&obj->oo_guard);
2218 spin_unlock(&obj->oo_guard);
2219 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2222 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2224 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2226 /* XXX: add to ext3 orphan list */
2227 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2229 /* not needed in the cache anymore */
2230 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2236 * Put the fid into lustre_mdt_attrs, and then place the structure
2237 * inode's ea. This fid should not be altered during the life time
2240 * \retval +ve, on success
2241 * \retval -ve, on error
2243 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2245 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2246 const struct lu_fid *fid, __u64 flags)
2248 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2251 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2254 lustre_lma_init(lma, fid, flags);
2255 lustre_lma_swab(lma);
2257 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2259 /* Someone may created the EA by race. */
2260 if (unlikely(rc == -EEXIST))
2266 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2267 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2268 * To have compatilibility with 1.8 ldiskfs driver we need to have
2269 * magic number at start of fid data.
2270 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2273 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2274 const struct dt_rec *fid)
2276 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2277 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2278 param->edp_magic = 0;
2282 param->edp_magic = LDISKFS_LUFID_MAGIC;
2283 param->edp_len = sizeof(struct lu_fid) + 1;
2284 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2288 * Try to read the fid from inode ea into dt_rec.
2290 * \param fid object fid.
2292 * \retval 0 on success
2294 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2295 __u32 ino, struct lu_fid *fid,
2296 struct osd_inode_id *id)
2298 struct osd_thread_info *info = osd_oti_get(env);
2299 struct inode *inode;
2302 osd_id_gen(id, ino, OSD_OII_NOGEN);
2303 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2305 RETURN(PTR_ERR(inode));
2311 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2313 struct inode *parent_dir,
2314 const struct dt_rec *dot_fid,
2315 const struct dt_rec *dot_dot_fid,
2316 struct osd_thandle *oth)
2318 struct ldiskfs_dentry_param *dot_ldp;
2319 struct ldiskfs_dentry_param *dot_dot_ldp;
2321 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2322 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2324 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2325 dot_ldp->edp_magic = 0;
2326 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2327 dir, dot_ldp, dot_dot_ldp);
2331 * Create an local agent inode for remote entry
2333 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2334 struct osd_device *osd,
2335 struct osd_object *pobj,
2336 const struct lu_fid *fid,
2339 struct osd_thread_info *info = osd_oti_get(env);
2340 struct inode *local;
2341 struct osd_thandle *oh;
2346 oh = container_of(th, struct osd_thandle, ot_super);
2347 LASSERT(oh->ot_handle->h_transaction != NULL);
2349 /* FIXME: Insert index api needs to know the mode of
2350 * the remote object. Just use S_IFDIR for now */
2351 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2352 if (IS_ERR(local)) {
2353 CERROR("%s: create local error %d\n", osd_name(osd),
2354 (int)PTR_ERR(local));
2358 /* Set special LMA flag for local agent inode */
2359 rc = osd_ea_fid_set(info, local, fid, LMAI_AGENT);
2361 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2362 osd_name(osd), PFID(fid), rc);
2363 RETURN(ERR_PTR(rc));
2366 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2367 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2368 (const struct dt_rec *)fid, oh);
2370 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2371 osd_name(osd), PFID(fid), rc);
2372 RETURN(ERR_PTR(rc));
2379 * Delete local agent inode for remote entry
2381 static int osd_delete_local_agent_inode(const struct lu_env *env,
2382 struct osd_device *osd,
2383 const struct lu_fid *fid,
2384 __u32 ino, struct osd_thandle *oh)
2386 struct osd_thread_info *oti = osd_oti_get(env);
2387 struct osd_inode_id *id = &oti->oti_id;
2388 struct inode *inode;
2391 id->oii_ino = le32_to_cpu(ino);
2392 id->oii_gen = OSD_OII_NOGEN;
2393 inode = osd_iget(oti, osd, id);
2394 if (IS_ERR(inode)) {
2395 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2396 PFID(fid), id->oii_ino, id->oii_gen);
2397 RETURN(PTR_ERR(inode));
2401 mark_inode_dirty(inode);
2402 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2403 osd_name(osd), PFID(fid), inode->i_ino);
2409 * OSD layer object create function for interoperability mode (b11826).
2410 * This is mostly similar to osd_object_create(). Only difference being, fid is
2411 * inserted into inode ea here.
2413 * \retval 0, on success
2414 * \retval -ve, on error
2416 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2417 struct lu_attr *attr,
2418 struct dt_allocation_hint *hint,
2419 struct dt_object_format *dof,
2422 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2423 struct osd_object *obj = osd_dt_obj(dt);
2424 struct osd_thread_info *info = osd_oti_get(env);
2429 LASSERT(osd_invariant(obj));
2430 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2431 LASSERT(osd_write_locked(env, obj));
2432 LASSERT(th != NULL);
2434 if (unlikely(fid_is_acct(fid)))
2435 /* Quota files can't be created from the kernel any more,
2436 * 'tune2fs -O quota' will take care of creating them */
2439 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2440 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2442 result = __osd_object_create(info, obj, attr, hint, dof, th);
2444 result = osd_ea_fid_set(info, obj->oo_inode, fid, 0);
2447 result = __osd_oi_insert(env, obj, fid, th);
2449 LASSERT(ergo(result == 0,
2450 dt_object_exists(dt) && !dt_object_remote(dt)));
2451 LINVRNT(osd_invariant(obj));
2455 static int osd_declare_object_ref_add(const struct lu_env *env,
2456 struct dt_object *dt,
2457 struct thandle *handle)
2459 struct osd_thandle *oh;
2461 /* it's possible that object doesn't exist yet */
2462 LASSERT(handle != NULL);
2464 oh = container_of0(handle, struct osd_thandle, ot_super);
2465 LASSERT(oh->ot_handle == NULL);
2467 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2468 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2474 * Concurrency: @dt is write locked.
2476 static int osd_object_ref_add(const struct lu_env *env,
2477 struct dt_object *dt, struct thandle *th)
2479 struct osd_object *obj = osd_dt_obj(dt);
2480 struct inode *inode = obj->oo_inode;
2482 LINVRNT(osd_invariant(obj));
2483 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2484 LASSERT(osd_write_locked(env, obj));
2485 LASSERT(th != NULL);
2487 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2490 * DIR_NLINK feature is set for compatibility reasons if:
2491 * 1) nlinks > LDISKFS_LINK_MAX, or
2492 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2494 * It is easier to always set this flag (rather than check and set),
2495 * since it has less overhead, and the superblock will be dirtied
2496 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2497 * do not actually care whether this flag is set or not.
2499 spin_lock(&obj->oo_guard);
2500 /* inc_nlink from 0 may cause WARN_ON */
2501 if(inode->i_nlink == 0)
2502 set_nlink(inode, 1);
2505 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2506 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2507 inode->i_nlink == 2)
2508 set_nlink(inode, 1);
2510 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2511 spin_unlock(&obj->oo_guard);
2512 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2513 LINVRNT(osd_invariant(obj));
2518 static int osd_declare_object_ref_del(const struct lu_env *env,
2519 struct dt_object *dt,
2520 struct thandle *handle)
2522 struct osd_thandle *oh;
2524 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2525 LASSERT(handle != NULL);
2527 oh = container_of0(handle, struct osd_thandle, ot_super);
2528 LASSERT(oh->ot_handle == NULL);
2530 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2531 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2537 * Concurrency: @dt is write locked.
2539 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2542 struct osd_object *obj = osd_dt_obj(dt);
2543 struct inode *inode = obj->oo_inode;
2545 LINVRNT(osd_invariant(obj));
2546 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2547 LASSERT(osd_write_locked(env, obj));
2548 LASSERT(th != NULL);
2550 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2552 spin_lock(&obj->oo_guard);
2553 LASSERT(inode->i_nlink > 0);
2555 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2556 * then the nlink count is 1. Don't let it be set to 0 or the directory
2557 * inode will be deleted incorrectly. */
2558 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2559 set_nlink(inode, 1);
2560 spin_unlock(&obj->oo_guard);
2561 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2562 LINVRNT(osd_invariant(obj));
2568 * Get the 64-bit version for an inode.
2570 static int osd_object_version_get(const struct lu_env *env,
2571 struct dt_object *dt, dt_obj_version_t *ver)
2573 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2575 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2576 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2577 *ver = LDISKFS_I(inode)->i_fs_version;
2582 * Concurrency: @dt is read locked.
2584 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2585 struct lu_buf *buf, const char *name,
2586 struct lustre_capa *capa)
2588 struct osd_object *obj = osd_dt_obj(dt);
2589 struct inode *inode = obj->oo_inode;
2590 struct osd_thread_info *info = osd_oti_get(env);
2591 struct dentry *dentry = &info->oti_obj_dentry;
2593 /* version get is not real XATTR but uses xattr API */
2594 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2595 /* for version we are just using xattr API but change inode
2597 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2598 osd_object_version_get(env, dt, buf->lb_buf);
2599 return sizeof(dt_obj_version_t);
2602 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2603 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2605 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2608 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2612 static int osd_declare_xattr_set(const struct lu_env *env,
2613 struct dt_object *dt,
2614 const struct lu_buf *buf, const char *name,
2615 int fl, struct thandle *handle)
2617 struct osd_thandle *oh;
2619 LASSERT(handle != NULL);
2621 oh = container_of0(handle, struct osd_thandle, ot_super);
2622 LASSERT(oh->ot_handle == NULL);
2624 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2625 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2626 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2627 osd_dto_credits_noquota[DTO_XATTR_SET]);
2633 * Set the 64-bit version for object
2635 static void osd_object_version_set(const struct lu_env *env,
2636 struct dt_object *dt,
2637 dt_obj_version_t *new_version)
2639 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2641 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2642 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2644 LDISKFS_I(inode)->i_fs_version = *new_version;
2645 /** Version is set after all inode operations are finished,
2646 * so we should mark it dirty here */
2647 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2651 * Concurrency: @dt is write locked.
2653 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2654 const struct lu_buf *buf, const char *name, int fl,
2655 struct thandle *handle, struct lustre_capa *capa)
2657 struct osd_object *obj = osd_dt_obj(dt);
2658 struct inode *inode = obj->oo_inode;
2659 struct osd_thread_info *info = osd_oti_get(env);
2663 LASSERT(handle != NULL);
2665 /* version set is not real XATTR */
2666 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2667 /* for version we are just using xattr API but change inode
2669 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2670 osd_object_version_set(env, dt, buf->lb_buf);
2671 return sizeof(dt_obj_version_t);
2674 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2677 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2678 if (fl & LU_XATTR_REPLACE)
2679 fs_flags |= XATTR_REPLACE;
2681 if (fl & LU_XATTR_CREATE)
2682 fs_flags |= XATTR_CREATE;
2684 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2689 * Concurrency: @dt is read locked.
2691 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2692 struct lu_buf *buf, 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);
2697 struct dentry *dentry = &info->oti_obj_dentry;
2699 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2700 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2701 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2703 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2706 dentry->d_inode = inode;
2707 dentry->d_sb = inode->i_sb;
2708 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2711 static int osd_declare_xattr_del(const struct lu_env *env,
2712 struct dt_object *dt, const char *name,
2713 struct thandle *handle)
2715 struct osd_thandle *oh;
2717 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2718 LASSERT(handle != NULL);
2720 oh = container_of0(handle, struct osd_thandle, ot_super);
2721 LASSERT(oh->ot_handle == NULL);
2723 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2724 osd_dto_credits_noquota[DTO_XATTR_SET]);
2730 * Concurrency: @dt is write locked.
2732 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2733 const char *name, struct thandle *handle,
2734 struct lustre_capa *capa)
2736 struct osd_object *obj = osd_dt_obj(dt);
2737 struct inode *inode = obj->oo_inode;
2738 struct osd_thread_info *info = osd_oti_get(env);
2739 struct dentry *dentry = &info->oti_obj_dentry;
2742 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2743 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2744 LASSERT(handle != NULL);
2746 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2749 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2751 ll_vfs_dq_init(inode);
2752 dentry->d_inode = inode;
2753 dentry->d_sb = inode->i_sb;
2754 rc = inode->i_op->removexattr(dentry, name);
2758 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2759 struct dt_object *dt,
2760 struct lustre_capa *old,
2763 struct osd_thread_info *info = osd_oti_get(env);
2764 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2765 struct osd_object *obj = osd_dt_obj(dt);
2766 struct osd_device *dev = osd_obj2dev(obj);
2767 struct lustre_capa_key *key = &info->oti_capa_key;
2768 struct lustre_capa *capa = &info->oti_capa;
2769 struct obd_capa *oc;
2770 struct md_capainfo *ci;
2774 if (!dev->od_fl_capa)
2775 RETURN(ERR_PTR(-ENOENT));
2777 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2778 LINVRNT(osd_invariant(obj));
2780 /* renewal sanity check */
2781 if (old && osd_object_auth(env, dt, old, opc))
2782 RETURN(ERR_PTR(-EACCES));
2784 ci = md_capainfo(env);
2786 RETURN(ERR_PTR(-ENOENT));
2788 switch (ci->mc_auth) {
2792 capa->lc_uid = obj->oo_inode->i_uid;
2793 capa->lc_gid = obj->oo_inode->i_gid;
2794 capa->lc_flags = LC_ID_PLAIN;
2796 case LC_ID_CONVERT: {
2799 s[0] = obj->oo_inode->i_uid;
2800 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2801 s[2] = obj->oo_inode->i_gid;
2802 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2803 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2805 RETURN(ERR_PTR(rc));
2807 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2808 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2809 capa->lc_flags = LC_ID_CONVERT;
2813 RETURN(ERR_PTR(-EINVAL));
2816 capa->lc_fid = *fid;
2818 capa->lc_flags |= dev->od_capa_alg << 24;
2819 capa->lc_timeout = dev->od_capa_timeout;
2820 capa->lc_expiry = 0;
2822 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2824 LASSERT(!capa_is_expired(oc));
2828 spin_lock(&capa_lock);
2829 *key = dev->od_capa_keys[1];
2830 spin_unlock(&capa_lock);
2832 capa->lc_keyid = key->lk_keyid;
2833 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2835 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2837 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2838 RETURN(ERR_PTR(rc));
2841 oc = capa_add(dev->od_capa_hash, capa);
2845 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2847 struct osd_object *obj = osd_dt_obj(dt);
2848 struct inode *inode = obj->oo_inode;
2849 struct osd_thread_info *info = osd_oti_get(env);
2850 struct dentry *dentry = &info->oti_obj_dentry;
2851 struct file *file = &info->oti_file;
2856 dentry->d_inode = inode;
2857 dentry->d_sb = inode->i_sb;
2858 file->f_dentry = dentry;
2859 file->f_mapping = inode->i_mapping;
2860 file->f_op = inode->i_fop;
2861 #ifndef HAVE_FILE_FSYNC_4ARGS
2862 mutex_lock(&inode->i_mutex);
2864 rc = do_fsync(file, 0);
2865 #ifndef HAVE_FILE_FSYNC_4ARGS
2866 mutex_unlock(&inode->i_mutex);
2871 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2874 struct osd_object *obj = osd_dt_obj(dt);
2877 *data = (void *)obj->oo_inode;
2885 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2886 const struct dt_index_features *feat)
2888 struct iam_descr *descr;
2890 if (osd_object_is_root(o))
2891 return feat == &dt_directory_features;
2893 LASSERT(o->oo_dir != NULL);
2895 descr = o->oo_dir->od_container.ic_descr;
2896 if (feat == &dt_directory_features) {
2897 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2903 feat->dif_keysize_min <= descr->id_key_size &&
2904 descr->id_key_size <= feat->dif_keysize_max &&
2905 feat->dif_recsize_min <= descr->id_rec_size &&
2906 descr->id_rec_size <= feat->dif_recsize_max &&
2907 !(feat->dif_flags & (DT_IND_VARKEY |
2908 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2909 ergo(feat->dif_flags & DT_IND_UPDATE,
2910 1 /* XXX check that object (and file system) is
2915 static int osd_iam_container_init(const struct lu_env *env,
2916 struct osd_object *obj,
2917 struct osd_directory *dir)
2919 struct iam_container *bag = &dir->od_container;
2922 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2926 result = iam_container_setup(bag);
2928 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2930 iam_container_fini(bag);
2937 * Concurrency: no external locking is necessary.
2939 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2940 const struct dt_index_features *feat)
2944 struct osd_object *obj = osd_dt_obj(dt);
2946 LINVRNT(osd_invariant(obj));
2948 if (osd_object_is_root(obj)) {
2949 dt->do_index_ops = &osd_index_ea_ops;
2951 } else if (feat == &dt_directory_features) {
2952 dt->do_index_ops = &osd_index_ea_ops;
2953 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2958 } else if (unlikely(feat == &dt_otable_features)) {
2959 dt->do_index_ops = &osd_otable_ops;
2961 } else if (unlikely(feat == &dt_acct_features)) {
2962 dt->do_index_ops = &osd_acct_index_ops;
2965 } else if (!osd_has_index(obj)) {
2966 struct osd_directory *dir;
2971 spin_lock(&obj->oo_guard);
2972 if (obj->oo_dir == NULL)
2976 * Concurrent thread allocated container data.
2979 spin_unlock(&obj->oo_guard);
2981 * Now, that we have container data, serialize its
2984 down_write(&obj->oo_ext_idx_sem);
2986 * recheck under lock.
2988 if (!osd_has_index(obj))
2989 result = osd_iam_container_init(env, obj, dir);
2992 up_write(&obj->oo_ext_idx_sem);
3000 if (result == 0 && skip_iam == 0) {
3001 if (!osd_iam_index_probe(env, obj, feat))
3004 LINVRNT(osd_invariant(obj));
3006 if (result == 0 && is_quota_glb_feat(feat) &&
3007 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3008 result = osd_quota_migration(env, dt, feat);
3013 static int osd_otable_it_attr_get(const struct lu_env *env,
3014 struct dt_object *dt,
3015 struct lu_attr *attr,
3016 struct lustre_capa *capa)
3022 static const struct dt_object_operations osd_obj_ops = {
3023 .do_read_lock = osd_object_read_lock,
3024 .do_write_lock = osd_object_write_lock,
3025 .do_read_unlock = osd_object_read_unlock,
3026 .do_write_unlock = osd_object_write_unlock,
3027 .do_write_locked = osd_object_write_locked,
3028 .do_attr_get = osd_attr_get,
3029 .do_declare_attr_set = osd_declare_attr_set,
3030 .do_attr_set = osd_attr_set,
3031 .do_ah_init = osd_ah_init,
3032 .do_declare_create = osd_declare_object_create,
3033 .do_create = osd_object_create,
3034 .do_declare_destroy = osd_declare_object_destroy,
3035 .do_destroy = osd_object_destroy,
3036 .do_index_try = osd_index_try,
3037 .do_declare_ref_add = osd_declare_object_ref_add,
3038 .do_ref_add = osd_object_ref_add,
3039 .do_declare_ref_del = osd_declare_object_ref_del,
3040 .do_ref_del = osd_object_ref_del,
3041 .do_xattr_get = osd_xattr_get,
3042 .do_declare_xattr_set = osd_declare_xattr_set,
3043 .do_xattr_set = osd_xattr_set,
3044 .do_declare_xattr_del = osd_declare_xattr_del,
3045 .do_xattr_del = osd_xattr_del,
3046 .do_xattr_list = osd_xattr_list,
3047 .do_capa_get = osd_capa_get,
3048 .do_object_sync = osd_object_sync,
3049 .do_data_get = osd_data_get,
3053 * dt_object_operations for interoperability mode
3054 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3056 static const struct dt_object_operations osd_obj_ea_ops = {
3057 .do_read_lock = osd_object_read_lock,
3058 .do_write_lock = osd_object_write_lock,
3059 .do_read_unlock = osd_object_read_unlock,
3060 .do_write_unlock = osd_object_write_unlock,
3061 .do_write_locked = osd_object_write_locked,
3062 .do_attr_get = osd_attr_get,
3063 .do_declare_attr_set = osd_declare_attr_set,
3064 .do_attr_set = osd_attr_set,
3065 .do_ah_init = osd_ah_init,
3066 .do_declare_create = osd_declare_object_create,
3067 .do_create = osd_object_ea_create,
3068 .do_declare_destroy = osd_declare_object_destroy,
3069 .do_destroy = osd_object_destroy,
3070 .do_index_try = osd_index_try,
3071 .do_declare_ref_add = osd_declare_object_ref_add,
3072 .do_ref_add = osd_object_ref_add,
3073 .do_declare_ref_del = osd_declare_object_ref_del,
3074 .do_ref_del = osd_object_ref_del,
3075 .do_xattr_get = osd_xattr_get,
3076 .do_declare_xattr_set = osd_declare_xattr_set,
3077 .do_xattr_set = osd_xattr_set,
3078 .do_declare_xattr_del = osd_declare_xattr_del,
3079 .do_xattr_del = osd_xattr_del,
3080 .do_xattr_list = osd_xattr_list,
3081 .do_capa_get = osd_capa_get,
3082 .do_object_sync = osd_object_sync,
3083 .do_data_get = osd_data_get,
3086 static const struct dt_object_operations osd_obj_otable_it_ops = {
3087 .do_attr_get = osd_otable_it_attr_get,
3088 .do_index_try = osd_index_try,
3091 static int osd_index_declare_iam_delete(const struct lu_env *env,
3092 struct dt_object *dt,
3093 const struct dt_key *key,
3094 struct thandle *handle)
3096 struct osd_thandle *oh;
3098 oh = container_of0(handle, struct osd_thandle, ot_super);
3099 LASSERT(oh->ot_handle == NULL);
3101 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3102 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3108 * delete a (key, value) pair from index \a dt specified by \a key
3110 * \param dt osd index object
3111 * \param key key for index
3112 * \param rec record reference
3113 * \param handle transaction handler
3116 * \retval -ve failure
3119 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3120 const struct dt_key *key,
3121 struct thandle *handle,
3122 struct lustre_capa *capa)
3124 struct osd_thread_info *oti = osd_oti_get(env);
3125 struct osd_object *obj = osd_dt_obj(dt);
3126 struct osd_thandle *oh;
3127 struct iam_path_descr *ipd;
3128 struct iam_container *bag = &obj->oo_dir->od_container;
3133 LINVRNT(osd_invariant(obj));
3134 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3135 LASSERT(bag->ic_object == obj->oo_inode);
3136 LASSERT(handle != NULL);
3138 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3141 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3143 ipd = osd_idx_ipd_get(env, bag);
3144 if (unlikely(ipd == NULL))
3147 oh = container_of0(handle, struct osd_thandle, ot_super);
3148 LASSERT(oh->ot_handle != NULL);
3149 LASSERT(oh->ot_handle->h_transaction != NULL);
3151 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3152 /* swab quota uid/gid provided by caller */
3153 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3154 key = (const struct dt_key *)&oti->oti_quota_id;
3157 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3158 osd_ipd_put(env, bag, ipd);
3159 LINVRNT(osd_invariant(obj));
3163 static int osd_index_declare_ea_delete(const struct lu_env *env,
3164 struct dt_object *dt,
3165 const struct dt_key *key,
3166 struct thandle *handle)
3168 struct osd_thandle *oh;
3169 struct inode *inode;
3173 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3174 LASSERT(handle != NULL);
3176 oh = container_of0(handle, struct osd_thandle, ot_super);
3177 LASSERT(oh->ot_handle == NULL);
3179 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3180 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3182 inode = osd_dt_obj(dt)->oo_inode;
3185 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3186 true, true, NULL, false);
3190 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3193 struct osd_fid_pack *rec;
3196 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3197 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3198 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3203 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3206 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3207 struct seq_server_site *ss = osd_seq_site(osd);
3211 /* Those FID seqs, which are not in FLDB, must be local seq */
3212 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3215 rc = osd_fld_lookup(env, osd, fid, range);
3217 CERROR("%s: Can not lookup fld for "DFID"\n",
3218 osd_name(osd), PFID(fid));
3222 RETURN(ss->ss_node_id != range->lsr_index);
3226 * Index delete function for interoperability mode (b11826).
3227 * It will remove the directory entry added by osd_index_ea_insert().
3228 * This entry is needed to maintain name->fid mapping.
3230 * \param key, key i.e. file entry to be deleted
3232 * \retval 0, on success
3233 * \retval -ve, on error
3235 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3236 const struct dt_key *key,
3237 struct thandle *handle,
3238 struct lustre_capa *capa)
3240 struct osd_object *obj = osd_dt_obj(dt);
3241 struct inode *dir = obj->oo_inode;
3242 struct dentry *dentry;
3243 struct osd_thandle *oh;
3244 struct ldiskfs_dir_entry_2 *de = NULL;
3245 struct buffer_head *bh;
3246 struct htree_lock *hlock = NULL;
3247 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3248 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3252 LINVRNT(osd_invariant(obj));
3253 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3254 LASSERT(handle != NULL);
3256 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3258 oh = container_of(handle, struct osd_thandle, ot_super);
3259 LASSERT(oh->ot_handle != NULL);
3260 LASSERT(oh->ot_handle->h_transaction != NULL);
3262 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3265 ll_vfs_dq_init(dir);
3266 dentry = osd_child_dentry_get(env, obj,
3267 (char *)key, strlen((char *)key));
3269 if (obj->oo_hl_head != NULL) {
3270 hlock = osd_oti_get(env)->oti_hlock;
3271 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3272 dir, LDISKFS_HLOCK_DEL);
3274 down_write(&obj->oo_ext_idx_sem);
3277 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3281 /* If this is not the ".." entry, it might be a remote DNE
3282 * entry and we need to check if the FID is for a remote
3283 * MDT. If the FID is not in the directory entry (e.g.
3284 * upgraded 1.8 filesystem without dirdata enabled) then
3285 * we need to get the FID from the LMA. For a remote directory
3286 * there HAS to be an LMA, it cannot be an IGIF inode in this
3289 * Delete the entry before the agent inode in order to
3290 * simplify error handling. At worst an error after deleting
3291 * the entry first might leak the agent inode afterward. The
3292 * reverse would need filesystem abort in case of error deleting
3293 * the entry after the agent had been removed, or leave a
3294 * dangling entry pointing at a random inode. */
3295 if (strcmp((char *)key, dotdot) != 0) {
3296 LASSERT(de != NULL);
3297 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3298 /* If Fid is not in dentry, try to get it from LMA */
3299 if (rc == -ENODATA) {
3300 struct osd_inode_id *id;
3301 struct inode *inode;
3303 /* Before trying to get fid from the inode,
3304 * check whether the inode is valid.
3306 * If the inode has been deleted, do not go
3307 * ahead to do osd_ea_fid_get, which will set
3308 * the inode to bad inode, which might cause
3309 * the inode to be deleted uncorrectly */
3310 inode = ldiskfs_iget(osd_sb(osd),
3311 le32_to_cpu(de->inode));
3312 if (IS_ERR(inode)) {
3313 CDEBUG(D_INODE, "%s: "DFID"get inode"
3314 "error.\n", osd_name(osd),
3316 rc = PTR_ERR(inode);
3318 if (likely(inode->i_nlink != 0)) {
3319 id = &osd_oti_get(env)->oti_id;
3320 rc = osd_ea_fid_get(env, obj,
3321 le32_to_cpu(de->inode),
3324 CDEBUG(D_INFO, "%s: %u "DFID
3327 le32_to_cpu(de->inode),
3335 unlikely(osd_remote_fid(env, osd, fid)))
3336 /* Need to delete agent inode */
3337 ino = le32_to_cpu(de->inode);
3339 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3341 if (rc == 0 && unlikely(ino != 0)) {
3342 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3345 CERROR("%s: del local inode "DFID": rc = %d\n",
3346 osd_name(osd), PFID(fid), rc);
3352 ldiskfs_htree_unlock(hlock);
3354 up_write(&obj->oo_ext_idx_sem);
3359 /* For inode on the remote MDT, .. will point to
3360 * /Agent directory, Check whether it needs to delete
3361 * from agent directory */
3362 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3363 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3365 if (rc != 0 && rc != -ENOENT) {
3366 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3367 osd_name(osd), PFID(fid), rc);
3377 LASSERT(osd_invariant(obj));
3382 * Lookup index for \a key and copy record to \a rec.
3384 * \param dt osd index object
3385 * \param key key for index
3386 * \param rec record reference
3388 * \retval +ve success : exact mach
3389 * \retval 0 return record with key not greater than \a key
3390 * \retval -ve failure
3392 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3393 struct dt_rec *rec, const struct dt_key *key,
3394 struct lustre_capa *capa)
3396 struct osd_object *obj = osd_dt_obj(dt);
3397 struct iam_path_descr *ipd;
3398 struct iam_container *bag = &obj->oo_dir->od_container;
3399 struct osd_thread_info *oti = osd_oti_get(env);
3400 struct iam_iterator *it = &oti->oti_idx_it;
3401 struct iam_rec *iam_rec;
3406 LASSERT(osd_invariant(obj));
3407 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3408 LASSERT(bag->ic_object == obj->oo_inode);
3410 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3413 ipd = osd_idx_ipd_get(env, bag);
3417 /* got ipd now we can start iterator. */
3418 iam_it_init(it, bag, 0, ipd);
3420 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3421 /* swab quota uid/gid provided by caller */
3422 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3423 key = (const struct dt_key *)&oti->oti_quota_id;
3426 rc = iam_it_get(it, (struct iam_key *)key);
3428 if (S_ISDIR(obj->oo_inode->i_mode))
3429 iam_rec = (struct iam_rec *)oti->oti_ldp;
3431 iam_rec = (struct iam_rec *) rec;
3433 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3435 if (S_ISDIR(obj->oo_inode->i_mode))
3436 osd_fid_unpack((struct lu_fid *) rec,
3437 (struct osd_fid_pack *)iam_rec);
3438 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3439 osd_quota_unpack(obj, rec);
3444 osd_ipd_put(env, bag, ipd);
3446 LINVRNT(osd_invariant(obj));
3451 static int osd_index_declare_iam_insert(const struct lu_env *env,
3452 struct dt_object *dt,
3453 const struct dt_rec *rec,
3454 const struct dt_key *key,
3455 struct thandle *handle)
3457 struct osd_thandle *oh;
3459 LASSERT(handle != NULL);
3461 oh = container_of0(handle, struct osd_thandle, ot_super);
3462 LASSERT(oh->ot_handle == NULL);
3464 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3465 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3471 * Inserts (key, value) pair in \a dt index object.
3473 * \param dt osd index object
3474 * \param key key for index
3475 * \param rec record reference
3476 * \param th transaction handler
3479 * \retval -ve failure
3481 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3482 const struct dt_rec *rec,
3483 const struct dt_key *key, struct thandle *th,
3484 struct lustre_capa *capa, int ignore_quota)
3486 struct osd_object *obj = osd_dt_obj(dt);
3487 struct iam_path_descr *ipd;
3488 struct osd_thandle *oh;
3489 struct iam_container *bag = &obj->oo_dir->od_container;
3490 struct osd_thread_info *oti = osd_oti_get(env);
3491 struct iam_rec *iam_rec;
3496 LINVRNT(osd_invariant(obj));
3497 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3498 LASSERT(bag->ic_object == obj->oo_inode);
3499 LASSERT(th != NULL);
3501 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3504 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3506 ipd = osd_idx_ipd_get(env, bag);
3507 if (unlikely(ipd == NULL))
3510 oh = container_of0(th, struct osd_thandle, ot_super);
3511 LASSERT(oh->ot_handle != NULL);
3512 LASSERT(oh->ot_handle->h_transaction != NULL);
3513 if (S_ISDIR(obj->oo_inode->i_mode)) {
3514 iam_rec = (struct iam_rec *)oti->oti_ldp;
3515 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3516 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3517 /* pack quota uid/gid */
3518 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3519 key = (const struct dt_key *)&oti->oti_quota_id;
3520 /* pack quota record */
3521 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3522 iam_rec = (struct iam_rec *)rec;
3524 iam_rec = (struct iam_rec *)rec;
3527 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3529 osd_ipd_put(env, bag, ipd);
3530 LINVRNT(osd_invariant(obj));
3535 * Calls ldiskfs_add_entry() to add directory entry
3536 * into the directory. This is required for
3537 * interoperability mode (b11826)
3539 * \retval 0, on success
3540 * \retval -ve, on error
3542 static int __osd_ea_add_rec(struct osd_thread_info *info,
3543 struct osd_object *pobj, struct inode *cinode,
3544 const char *name, const struct dt_rec *fid,
3545 struct htree_lock *hlock, struct thandle *th)
3547 struct ldiskfs_dentry_param *ldp;
3548 struct dentry *child;
3549 struct osd_thandle *oth;
3552 oth = container_of(th, struct osd_thandle, ot_super);
3553 LASSERT(oth->ot_handle != NULL);
3554 LASSERT(oth->ot_handle->h_transaction != NULL);
3555 LASSERT(pobj->oo_inode);
3557 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3558 if (unlikely(pobj->oo_inode ==
3559 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3562 osd_get_ldiskfs_dirent_param(ldp, fid);
3563 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3564 child->d_fsdata = (void *)ldp;
3565 ll_vfs_dq_init(pobj->oo_inode);
3566 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3572 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3573 * into the directory.Also sets flags into osd object to
3574 * indicate dot and dotdot are created. This is required for
3575 * interoperability mode (b11826)
3577 * \param dir directory for dot and dotdot fixup.
3578 * \param obj child object for linking
3580 * \retval 0, on success
3581 * \retval -ve, on error
3583 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3584 struct osd_object *dir,
3585 struct inode *parent_dir, const char *name,
3586 const struct dt_rec *dot_fid,
3587 const struct dt_rec *dot_dot_fid,
3590 struct inode *inode = dir->oo_inode;
3591 struct osd_thandle *oth;
3594 oth = container_of(th, struct osd_thandle, ot_super);
3595 LASSERT(oth->ot_handle->h_transaction != NULL);
3596 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3598 if (strcmp(name, dot) == 0) {
3599 if (dir->oo_compat_dot_created) {
3602 LASSERT(inode == parent_dir);
3603 dir->oo_compat_dot_created = 1;
3606 } else if (strcmp(name, dotdot) == 0) {
3607 if (!dir->oo_compat_dot_created)
3609 /* in case of rename, dotdot is already created */
3610 if (dir->oo_compat_dotdot_created) {
3611 return __osd_ea_add_rec(info, dir, parent_dir, name,
3612 dot_dot_fid, NULL, th);
3615 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3616 parent_dir, dot_fid,
3619 dir->oo_compat_dotdot_created = 1;
3627 * It will call the appropriate osd_add* function and return the
3628 * value, return by respective functions.
3630 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3631 struct inode *cinode, const char *name,
3632 const struct dt_rec *fid, struct thandle *th)
3634 struct osd_thread_info *info = osd_oti_get(env);
3635 struct htree_lock *hlock;
3638 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3640 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3642 if (hlock != NULL) {
3643 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3646 down_write(&pobj->oo_ext_idx_sem);
3648 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3649 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3652 if (hlock != NULL) {
3653 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3654 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3656 down_write(&pobj->oo_ext_idx_sem);
3659 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3660 struct lu_fid *tfid = &info->oti_fid;
3662 *tfid = *(const struct lu_fid *)fid;
3664 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3665 (const struct dt_rec *)tfid,
3668 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3673 ldiskfs_htree_unlock(hlock);
3675 up_write(&pobj->oo_ext_idx_sem);
3681 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3682 struct osd_idmap_cache *oic)
3684 struct osd_scrub *scrub = &dev->od_scrub;
3685 struct lu_fid *fid = &oic->oic_fid;
3686 struct osd_inode_id *id = &oti->oti_id;
3691 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3695 rc = osd_oi_lookup(oti, dev, fid, id, true);
3696 if (rc != 0 && rc != -ENOENT)
3699 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3702 if (thread_is_running(&scrub->os_thread)) {
3703 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3704 /* There is race condition between osd_oi_lookup and OI scrub.
3705 * The OI scrub finished just after osd_oi_lookup() failure.
3706 * Under such case, it is unnecessary to trigger OI scrub again,
3707 * but try to call osd_oi_lookup() again. */
3708 if (unlikely(rc == -EAGAIN))
3714 if (!dev->od_noscrub && ++once == 1) {
3715 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3717 rc = osd_scrub_start(dev);
3718 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3720 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3729 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3730 struct osd_device *dev,
3731 struct osd_idmap_cache *oic,
3732 struct lu_fid *fid, __u32 ino)
3734 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3735 struct inode *inode;
3738 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3739 inode = osd_iget(oti, dev, &oic->oic_lid);
3740 if (IS_ERR(inode)) {
3741 fid_zero(&oic->oic_fid);
3742 return PTR_ERR(inode);
3745 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3748 fid_zero(&oic->oic_fid);
3750 *fid = oic->oic_fid = lma->lma_self_fid;
3755 * Calls ->lookup() to find dentry. From dentry get inode and
3756 * read inode's ea to get fid. This is required for interoperability
3759 * \retval 0, on success
3760 * \retval -ve, on error
3762 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3763 struct dt_rec *rec, const struct dt_key *key)
3765 struct inode *dir = obj->oo_inode;
3766 struct dentry *dentry;
3767 struct ldiskfs_dir_entry_2 *de;
3768 struct buffer_head *bh;
3769 struct lu_fid *fid = (struct lu_fid *) rec;
3770 struct htree_lock *hlock = NULL;
3775 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3777 dentry = osd_child_dentry_get(env, obj,
3778 (char *)key, strlen((char *)key));
3780 if (obj->oo_hl_head != NULL) {
3781 hlock = osd_oti_get(env)->oti_hlock;
3782 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3783 dir, LDISKFS_HLOCK_LOOKUP);
3785 down_read(&obj->oo_ext_idx_sem);
3788 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3790 struct osd_thread_info *oti = osd_oti_get(env);
3791 struct osd_inode_id *id = &oti->oti_id;
3792 struct osd_idmap_cache *oic = &oti->oti_cache;
3793 struct osd_device *dev = osd_obj2dev(obj);
3794 struct osd_scrub *scrub = &dev->od_scrub;
3795 struct scrub_file *sf = &scrub->os_file;
3797 ino = le32_to_cpu(de->inode);
3798 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3800 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3804 rc = osd_get_fid_from_dentry(de, rec);
3806 /* done with de, release bh */
3809 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3811 osd_id_gen(id, ino, OSD_OII_NOGEN);
3812 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3813 fid_zero(&oic->oic_fid);
3818 oic->oic_fid = *fid;
3819 if ((scrub->os_pos_current <= ino) &&
3820 ((sf->sf_flags & SF_INCONSISTENT) ||
3821 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3822 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3824 osd_consistency_check(oti, dev, oic);
3833 ldiskfs_htree_unlock(hlock);
3835 up_read(&obj->oo_ext_idx_sem);
3840 * Find the osd object for given fid.
3842 * \param fid need to find the osd object having this fid
3844 * \retval osd_object on success
3845 * \retval -ve on error
3847 struct osd_object *osd_object_find(const struct lu_env *env,
3848 struct dt_object *dt,
3849 const struct lu_fid *fid)
3851 struct lu_device *ludev = dt->do_lu.lo_dev;
3852 struct osd_object *child = NULL;
3853 struct lu_object *luch;
3854 struct lu_object *lo;
3857 * at this point topdev might not exist yet
3858 * (i.e. MGS is preparing profiles). so we can
3859 * not rely on topdev and instead lookup with
3860 * our device passed as topdev. this can't work
3861 * if the object isn't cached yet (as osd doesn't
3862 * allocate lu_header). IOW, the object must be
3863 * in the cache, otherwise lu_object_alloc() crashes
3866 luch = lu_object_find_at(env, ludev, fid, NULL);
3867 if (!IS_ERR(luch)) {
3868 if (lu_object_exists(luch)) {
3869 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3871 child = osd_obj(lo);
3873 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3874 "lu_object can't be located"
3875 DFID"\n", PFID(fid));
3877 if (child == NULL) {
3878 lu_object_put(env, luch);
3879 CERROR("Unable to get osd_object\n");
3880 child = ERR_PTR(-ENOENT);
3883 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3884 "lu_object does not exists "DFID"\n",
3886 lu_object_put(env, luch);
3887 child = ERR_PTR(-ENOENT);
3890 child = (void *)luch;
3896 * Put the osd object once done with it.
3898 * \param obj osd object that needs to be put
3900 static inline void osd_object_put(const struct lu_env *env,
3901 struct osd_object *obj)
3903 lu_object_put(env, &obj->oo_dt.do_lu);
3906 static int osd_index_declare_ea_insert(const struct lu_env *env,
3907 struct dt_object *dt,
3908 const struct dt_rec *rec,
3909 const struct dt_key *key,
3910 struct thandle *handle)
3912 struct osd_thandle *oh;
3913 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3914 struct lu_fid *fid = (struct lu_fid *)rec;
3918 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3919 LASSERT(handle != NULL);
3921 oh = container_of0(handle, struct osd_thandle, ot_super);
3922 LASSERT(oh->ot_handle == NULL);
3924 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3925 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3927 if (osd_dt_obj(dt)->oo_inode == NULL) {
3928 const char *name = (const char *)key;
3929 /* Object is not being created yet. Only happens when
3930 * 1. declare directory create
3931 * 2. declare insert .
3932 * 3. declare insert ..
3934 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3936 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3938 /* We ignore block quota on meta pool (MDTs), so needn't
3939 * calculate how many blocks will be consumed by this index
3941 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3942 oh, true, true, NULL, false);
3948 rc = osd_remote_fid(env, osd, fid);
3954 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3955 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
3956 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3957 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3958 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3959 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3965 * Index add function for interoperability mode (b11826).
3966 * It will add the directory entry.This entry is needed to
3967 * maintain name->fid mapping.
3969 * \param key it is key i.e. file entry to be inserted
3970 * \param rec it is value of given key i.e. fid
3972 * \retval 0, on success
3973 * \retval -ve, on error
3975 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3976 const struct dt_rec *rec,
3977 const struct dt_key *key, struct thandle *th,
3978 struct lustre_capa *capa, int ignore_quota)
3980 struct osd_object *obj = osd_dt_obj(dt);
3981 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3982 struct lu_fid *fid = (struct lu_fid *) rec;
3983 const char *name = (const char *)key;
3984 struct osd_thread_info *oti = osd_oti_get(env);
3985 struct osd_inode_id *id = &oti->oti_id;
3986 struct inode *child_inode = NULL;
3987 struct osd_object *child = NULL;
3991 LASSERT(osd_invariant(obj));
3992 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3993 LASSERT(th != NULL);
3995 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3997 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4000 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4002 rc = osd_remote_fid(env, osd, fid);
4004 CERROR("%s: Can not find object "DFID" rc %d\n",
4005 osd_name(osd), PFID(fid), rc);
4010 /* Insert remote entry */
4011 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4012 struct osd_mdobj_map *omm = osd->od_mdt_map;
4013 struct osd_thandle *oh;
4015 /* If parent on remote MDT, we need put this object
4017 oh = container_of(th, typeof(*oh), ot_super);
4018 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4020 CERROR("%s: add "DFID" error: rc = %d\n",
4022 PFID(lu_object_fid(&dt->do_lu)), rc);
4026 child_inode = igrab(omm->omm_remote_parent->d_inode);
4028 child_inode = osd_create_local_agent_inode(env, osd,
4031 if (IS_ERR(child_inode))
4032 RETURN(PTR_ERR(child_inode));
4035 /* Insert local entry */
4036 child = osd_object_find(env, dt, fid);
4037 if (IS_ERR(child)) {
4038 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4039 osd_name(osd), PFID(fid),
4040 id->oii_ino, id->oii_gen,
4041 (int)PTR_ERR(child_inode));
4042 RETURN(PTR_ERR(child_inode));
4044 child_inode = igrab(child->oo_inode);
4047 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4051 osd_object_put(env, child);
4052 LASSERT(osd_invariant(obj));
4057 * Initialize osd Iterator for given osd index object.
4059 * \param dt osd index object
4062 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4063 struct dt_object *dt,
4065 struct lustre_capa *capa)
4067 struct osd_it_iam *it;
4068 struct osd_thread_info *oti = osd_oti_get(env);
4069 struct osd_object *obj = osd_dt_obj(dt);
4070 struct lu_object *lo = &dt->do_lu;
4071 struct iam_path_descr *ipd;
4072 struct iam_container *bag = &obj->oo_dir->od_container;
4074 LASSERT(lu_object_exists(lo));
4076 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4077 return ERR_PTR(-EACCES);
4080 ipd = osd_it_ipd_get(env, bag);
4081 if (likely(ipd != NULL)) {
4085 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4086 return (struct dt_it *)it;
4088 return ERR_PTR(-ENOMEM);
4092 * free given Iterator.
4095 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4097 struct osd_it_iam *it = (struct osd_it_iam *)di;
4098 struct osd_object *obj = it->oi_obj;
4100 iam_it_fini(&it->oi_it);
4101 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4102 lu_object_put(env, &obj->oo_dt.do_lu);
4106 * Move Iterator to record specified by \a key
4108 * \param di osd iterator
4109 * \param key key for index
4111 * \retval +ve di points to record with least key not larger than key
4112 * \retval 0 di points to exact matched key
4113 * \retval -ve failure
4116 static int osd_it_iam_get(const struct lu_env *env,
4117 struct dt_it *di, const struct dt_key *key)
4119 struct osd_thread_info *oti = osd_oti_get(env);
4120 struct osd_it_iam *it = (struct osd_it_iam *)di;
4122 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4123 /* swab quota uid/gid */
4124 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4125 key = (struct dt_key *)&oti->oti_quota_id;
4128 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4134 * \param di osd iterator
4136 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4138 struct osd_it_iam *it = (struct osd_it_iam *)di;
4140 iam_it_put(&it->oi_it);
4144 * Move iterator by one record
4146 * \param di osd iterator
4148 * \retval +1 end of container reached
4150 * \retval -ve failure
4153 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4155 struct osd_it_iam *it = (struct osd_it_iam *)di;
4157 return iam_it_next(&it->oi_it);
4161 * Return pointer to the key under iterator.
4164 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4165 const struct dt_it *di)
4167 struct osd_thread_info *oti = osd_oti_get(env);
4168 struct osd_it_iam *it = (struct osd_it_iam *)di;
4169 struct osd_object *obj = it->oi_obj;
4172 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4174 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4175 /* swab quota uid/gid */
4176 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4177 key = (struct dt_key *)&oti->oti_quota_id;
4184 * Return size of key under iterator (in bytes)
4187 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4189 struct osd_it_iam *it = (struct osd_it_iam *)di;
4191 return iam_it_key_size(&it->oi_it);
4195 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4197 /* check if file type is required */
4198 if (ent->lde_attrs & LUDA_TYPE) {
4199 struct luda_type *lt;
4200 int align = sizeof(*lt) - 1;
4202 len = (len + align) & ~align;
4203 lt = (struct luda_type *)(ent->lde_name + len);
4204 lt->lt_type = cpu_to_le16(DTTOIF(type));
4207 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4211 * build lu direct from backend fs dirent.
4215 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4216 char *name, __u16 namelen, __u16 type, __u32 attr)
4218 ent->lde_attrs = attr | LUDA_FID;
4219 fid_cpu_to_le(&ent->lde_fid, fid);
4221 ent->lde_hash = cpu_to_le64(offset);
4222 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4224 strncpy(ent->lde_name, name, namelen);
4225 ent->lde_name[namelen] = '\0';
4226 ent->lde_namelen = cpu_to_le16(namelen);
4228 /* append lustre attributes */
4229 osd_it_append_attrs(ent, namelen, type);
4233 * Return pointer to the record under iterator.
4235 static int osd_it_iam_rec(const struct lu_env *env,
4236 const struct dt_it *di,
4237 struct dt_rec *dtrec, __u32 attr)
4239 struct osd_it_iam *it = (struct osd_it_iam *)di;
4240 struct osd_thread_info *info = osd_oti_get(env);
4243 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4244 const struct osd_fid_pack *rec;
4245 struct lu_fid *fid = &info->oti_fid;
4246 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4252 name = (char *)iam_it_key_get(&it->oi_it);
4254 RETURN(PTR_ERR(name));
4256 namelen = iam_it_key_size(&it->oi_it);
4258 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4260 RETURN(PTR_ERR(rec));
4262 rc = osd_fid_unpack(fid, rec);
4266 hash = iam_it_store(&it->oi_it);
4268 /* IAM does not store object type in IAM index (dir) */
4269 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4271 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4272 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4273 (struct iam_rec *)dtrec);
4274 osd_quota_unpack(it->oi_obj, dtrec);
4276 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4277 (struct iam_rec *)dtrec);
4284 * Returns cookie for current Iterator position.
4286 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4288 struct osd_it_iam *it = (struct osd_it_iam *)di;
4290 return iam_it_store(&it->oi_it);
4294 * Restore iterator from cookie.
4296 * \param di osd iterator
4297 * \param hash Iterator location cookie
4299 * \retval +ve di points to record with least key not larger than key.
4300 * \retval 0 di points to exact matched key
4301 * \retval -ve failure
4304 static int osd_it_iam_load(const struct lu_env *env,
4305 const struct dt_it *di, __u64 hash)
4307 struct osd_it_iam *it = (struct osd_it_iam *)di;
4309 return iam_it_load(&it->oi_it, hash);
4312 static const struct dt_index_operations osd_index_iam_ops = {
4313 .dio_lookup = osd_index_iam_lookup,
4314 .dio_declare_insert = osd_index_declare_iam_insert,
4315 .dio_insert = osd_index_iam_insert,
4316 .dio_declare_delete = osd_index_declare_iam_delete,
4317 .dio_delete = osd_index_iam_delete,
4319 .init = osd_it_iam_init,
4320 .fini = osd_it_iam_fini,
4321 .get = osd_it_iam_get,
4322 .put = osd_it_iam_put,
4323 .next = osd_it_iam_next,
4324 .key = osd_it_iam_key,
4325 .key_size = osd_it_iam_key_size,
4326 .rec = osd_it_iam_rec,
4327 .store = osd_it_iam_store,
4328 .load = osd_it_iam_load
4334 * Creates or initializes iterator context.
4336 * \retval struct osd_it_ea, iterator structure on success
4339 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4340 struct dt_object *dt,
4342 struct lustre_capa *capa)
4344 struct osd_object *obj = osd_dt_obj(dt);
4345 struct osd_thread_info *info = osd_oti_get(env);
4346 struct osd_it_ea *it = &info->oti_it_ea;
4347 struct file *file = &it->oie_file;
4348 struct lu_object *lo = &dt->do_lu;
4349 struct dentry *obj_dentry = &info->oti_it_dentry;
4351 LASSERT(lu_object_exists(lo));
4353 obj_dentry->d_inode = obj->oo_inode;
4354 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4355 obj_dentry->d_name.hash = 0;
4357 it->oie_rd_dirent = 0;
4358 it->oie_it_dirent = 0;
4359 it->oie_dirent = NULL;
4360 it->oie_buf = info->oti_it_ea_buf;
4363 /* Reset the "file" totally to avoid to reuse any old value from
4364 * former readdir handling, the "file->f_pos" should be zero. */
4365 memset(file, 0, sizeof(*file));
4366 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4367 if (attr & LUDA_64BITHASH)
4368 file->f_mode = FMODE_64BITHASH;
4370 file->f_mode = FMODE_32BITHASH;
4371 file->f_dentry = obj_dentry;
4372 file->f_mapping = obj->oo_inode->i_mapping;
4373 file->f_op = obj->oo_inode->i_fop;
4375 RETURN((struct dt_it *) it);
4379 * Destroy or finishes iterator context.
4381 * \param di iterator structure to be destroyed
4383 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4385 struct osd_it_ea *it = (struct osd_it_ea *)di;
4386 struct osd_object *obj = it->oie_obj;
4387 struct inode *inode = obj->oo_inode;
4390 it->oie_file.f_op->release(inode, &it->oie_file);
4391 lu_object_put(env, &obj->oo_dt.do_lu);
4396 * It position the iterator at given key, so that next lookup continues from
4397 * that key Or it is similar to dio_it->load() but based on a key,
4398 * rather than file position.
4400 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4403 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4405 static int osd_it_ea_get(const struct lu_env *env,
4406 struct dt_it *di, const struct dt_key *key)
4408 struct osd_it_ea *it = (struct osd_it_ea *)di;
4411 LASSERT(((const char *)key)[0] == '\0');
4412 it->oie_file.f_pos = 0;
4413 it->oie_rd_dirent = 0;
4414 it->oie_it_dirent = 0;
4415 it->oie_dirent = NULL;
4423 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4428 * It is called internally by ->readdir(). It fills the
4429 * iterator's in-memory data structure with required
4430 * information i.e. name, namelen, rec_size etc.
4432 * \param buf in which information to be filled in.
4433 * \param name name of the file in given dir
4435 * \retval 0 on success
4436 * \retval 1 on buffer full
4438 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4439 loff_t offset, __u64 ino,
4442 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4443 struct osd_object *obj = it->oie_obj;
4444 struct osd_it_ea_dirent *ent = it->oie_dirent;
4445 struct lu_fid *fid = &ent->oied_fid;
4446 struct osd_fid_pack *rec;
4449 /* this should never happen */
4450 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4451 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4455 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4459 /* "." is just the object itself. */
4460 if (namelen == 1 && name[0] == '.') {
4461 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4462 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4463 rec = (struct osd_fid_pack*) (name + namelen + 1);
4464 if (osd_fid_unpack(fid, rec) != 0)
4469 d_type &= ~LDISKFS_DIRENT_LUFID;
4471 /* NOT export local root. */
4472 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4473 ino = obj->oo_inode->i_ino;
4474 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4477 ent->oied_ino = ino;
4478 ent->oied_off = offset;
4479 ent->oied_namelen = namelen;
4480 ent->oied_type = d_type;
4482 memcpy(ent->oied_name, name, namelen);
4484 it->oie_rd_dirent++;
4485 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4490 * Calls ->readdir() to load a directory entry at a time
4491 * and stored it in iterator's in-memory data structure.
4493 * \param di iterator's in memory structure
4495 * \retval 0 on success
4496 * \retval -ve on error
4498 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4499 const struct dt_it *di)
4501 struct osd_it_ea *it = (struct osd_it_ea *)di;
4502 struct osd_object *obj = it->oie_obj;
4503 struct inode *inode = obj->oo_inode;
4504 struct htree_lock *hlock = NULL;
4508 it->oie_dirent = it->oie_buf;
4509 it->oie_rd_dirent = 0;
4511 if (obj->oo_hl_head != NULL) {
4512 hlock = osd_oti_get(env)->oti_hlock;
4513 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4514 inode, LDISKFS_HLOCK_READDIR);
4516 down_read(&obj->oo_ext_idx_sem);
4519 result = inode->i_fop->readdir(&it->oie_file, it,
4520 (filldir_t) osd_ldiskfs_filldir);
4523 ldiskfs_htree_unlock(hlock);
4525 up_read(&obj->oo_ext_idx_sem);
4527 if (it->oie_rd_dirent == 0) {
4530 it->oie_dirent = it->oie_buf;
4531 it->oie_it_dirent = 1;
4538 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4539 * to load a directory entry at a time and stored it in
4540 * iterator's in-memory data structure.
4542 * \param di iterator's in memory structure
4544 * \retval +ve iterator reached to end
4545 * \retval 0 iterator not reached to end
4546 * \retval -ve on error
4548 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4550 struct osd_it_ea *it = (struct osd_it_ea *)di;
4555 if (it->oie_it_dirent < it->oie_rd_dirent) {
4557 (void *) it->oie_dirent +
4558 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4559 it->oie_dirent->oied_namelen);
4560 it->oie_it_dirent++;
4563 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4566 rc = osd_ldiskfs_it_fill(env, di);
4573 * Returns the key at current position from iterator's in memory structure.
4575 * \param di iterator's in memory structure
4577 * \retval key i.e. struct dt_key on success
4579 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4580 const struct dt_it *di)
4582 struct osd_it_ea *it = (struct osd_it_ea *)di;
4584 return (struct dt_key *)it->oie_dirent->oied_name;
4588 * Returns the key's size at current position from iterator's in memory structure.
4590 * \param di iterator's in memory structure
4592 * \retval key_size i.e. struct dt_key on success
4594 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4596 struct osd_it_ea *it = (struct osd_it_ea *)di;
4598 return it->oie_dirent->oied_namelen;
4602 osd_dirent_update(handle_t *jh, struct super_block *sb,
4603 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4604 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4606 struct osd_fid_pack *rec;
4610 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4611 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4613 rc = ldiskfs_journal_get_write_access(jh, bh);
4615 CERROR("%.16s: fail to write access for update dirent: "
4616 "name = %.*s, rc = %d\n",
4617 LDISKFS_SB(sb)->s_es->s_volume_name,
4618 ent->oied_namelen, ent->oied_name, rc);
4622 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4623 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4624 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4626 CERROR("%.16s: fail to dirty metadata for update dirent: "
4627 "name = %.*s, rc = %d\n",
4628 LDISKFS_SB(sb)->s_es->s_volume_name,
4629 ent->oied_namelen, ent->oied_name, rc);
4635 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4637 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4638 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4645 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4647 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4648 "dot_dotdot = %d\n", dot_dotdot);
4650 if (LDISKFS_DIR_REC_LEN(de) >=
4651 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4658 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4659 struct inode *dir, struct inode *inode,
4660 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4661 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4662 struct htree_lock *hlock)
4664 struct dentry *dentry;
4665 struct osd_fid_pack *rec;
4666 struct ldiskfs_dentry_param *ldp;
4670 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4671 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4674 /* There is enough space to hold the FID-in-dirent. */
4675 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4676 dir->i_sb->s_blocksize)) {
4677 rc = ldiskfs_journal_get_write_access(jh, bh);
4679 CERROR("%.16s: fail to write access for reinsert "
4680 "dirent: name = %.*s, rc = %d\n",
4681 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4682 ent->oied_namelen, ent->oied_name, rc);
4686 de->name[de->name_len] = 0;
4687 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4688 rec->fp_len = sizeof(struct lu_fid) + 1;
4689 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4690 de->file_type |= LDISKFS_DIRENT_LUFID;
4692 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4694 CERROR("%.16s: fail to dirty metadata for reinsert "
4695 "dirent: name = %.*s, rc = %d\n",
4696 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4697 ent->oied_namelen, ent->oied_name, rc);
4702 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4704 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4705 "name = %.*s, rc = %d\n",
4706 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4707 ent->oied_namelen, ent->oied_name, rc);
4711 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4713 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4714 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4715 dentry->d_fsdata = (void *)ldp;
4716 ll_vfs_dq_init(dir);
4717 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4718 /* It is too bad, we cannot reinsert the name entry back.
4719 * That means we lose it! */
4721 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4722 "name = %.*s, rc = %d\n",
4723 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4724 ent->oied_namelen, ent->oied_name, rc);
4730 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4731 struct osd_it_ea *it, struct lu_fid *fid,
4732 struct osd_inode_id *id, __u32 *attr)
4734 struct osd_thread_info *info = osd_oti_get(env);
4735 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4736 struct osd_device *dev = osd_obj2dev(obj);
4737 struct super_block *sb = osd_sb(dev);
4738 const char *devname =
4739 LDISKFS_SB(sb)->s_es->s_volume_name;
4740 struct osd_it_ea_dirent *ent = it->oie_dirent;
4741 struct inode *dir = obj->oo_inode;
4742 struct htree_lock *hlock = NULL;
4743 struct buffer_head *bh = NULL;
4744 handle_t *jh = NULL;
4745 struct ldiskfs_dir_entry_2 *de;
4746 struct dentry *dentry;
4747 struct inode *inode;
4754 if (ent->oied_name[0] == '.') {
4755 if (ent->oied_namelen == 1)
4757 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4761 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4764 /* We need to ensure that the name entry is still valid.
4765 * Because it may be removed or renamed by other already.
4767 * The unlink or rename operation will start journal before PDO lock,
4768 * so to avoid deadlock, here we need to start journal handle before
4769 * related PDO lock also. But because we do not know whether there
4770 * will be something to be repaired before PDO lock, we just start
4771 * journal without conditions.
4773 * We may need to remove the name entry firstly, then insert back.
4774 * One credit is for user quota file update.
4775 * One credit is for group quota file update.
4776 * Two credits are for dirty inode. */
4777 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4778 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4781 if (dev->od_dirent_journal) {
4782 jh = ldiskfs_journal_start_sb(sb, credits);
4785 CERROR("%.16s: fail to start trans for dirent "
4786 "check_repair: credits %d, name %.*s, rc %d\n",
4787 devname, credits, ent->oied_namelen,
4788 ent->oied_name, rc);
4792 if (obj->oo_hl_head != NULL) {
4793 hlock = osd_oti_get(env)->oti_hlock;
4794 /* "0" means exclusive lock for the whole directory.
4795 * We need to prevent others access such name entry
4796 * during the delete + insert. Neither HLOCK_ADD nor
4797 * HLOCK_DEL cannot guarantee the atomicity. */
4798 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4800 down_write(&obj->oo_ext_idx_sem);
4803 if (obj->oo_hl_head != NULL) {
4804 hlock = osd_oti_get(env)->oti_hlock;
4805 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4806 LDISKFS_HLOCK_LOOKUP);
4808 down_read(&obj->oo_ext_idx_sem);
4812 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4813 /* For dot/dotdot entry, if there is not enough space to hold the
4814 * FID-in-dirent, just keep them there. It only happens when the
4815 * device upgraded from 1.8 or restored from MDT file-level backup.
4816 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4817 * and needs to get FID from LMA when readdir, it will not affect the
4818 * performance much. */
4819 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4820 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4821 *attr |= LUDA_IGNORE;
4822 GOTO(out_journal, rc = 0);
4825 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4826 inode = osd_iget(info, dev, id);
4827 if (IS_ERR(inode)) {
4828 rc = PTR_ERR(inode);
4829 if (rc == -ENOENT || rc == -ESTALE) {
4830 *attr |= LUDA_IGNORE;
4834 GOTO(out_journal, rc);
4837 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4839 if (fid_is_sane(fid)) {
4840 /* FID-in-dirent is valid. */
4841 if (lu_fid_eq(fid, &lma->lma_self_fid))
4842 GOTO(out_inode, rc = 0);
4844 /* Do not repair under dryrun mode. */
4845 if (*attr & LUDA_VERIFY_DRYRUN) {
4846 *attr |= LUDA_REPAIR;
4847 GOTO(out_inode, rc = 0);
4850 if (!dev->od_dirent_journal) {
4854 ldiskfs_htree_unlock(hlock);
4856 up_read(&obj->oo_ext_idx_sem);
4857 dev->od_dirent_journal = 1;
4861 *fid = lma->lma_self_fid;
4863 /* Update the FID-in-dirent. */
4864 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4866 *attr |= LUDA_REPAIR;
4868 /* Do not repair under dryrun mode. */
4869 if (*attr & LUDA_VERIFY_DRYRUN) {
4870 *fid = lma->lma_self_fid;
4871 *attr |= LUDA_REPAIR;
4872 GOTO(out_inode, rc = 0);
4875 if (!dev->od_dirent_journal) {
4879 ldiskfs_htree_unlock(hlock);
4881 up_read(&obj->oo_ext_idx_sem);
4882 dev->od_dirent_journal = 1;
4886 *fid = lma->lma_self_fid;
4888 /* Append the FID-in-dirent. */
4889 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4890 fid, bh, de, hlock);
4892 *attr |= LUDA_REPAIR;
4894 } else if (rc == -ENODATA) {
4895 /* Do not repair under dryrun mode. */
4896 if (*attr & LUDA_VERIFY_DRYRUN) {
4897 if (fid_is_sane(fid)) {
4898 *attr |= LUDA_REPAIR;
4900 lu_igif_build(fid, inode->i_ino,
4901 inode->i_generation);
4902 *attr |= LUDA_UPGRADE;
4904 GOTO(out_inode, rc = 0);
4907 if (!dev->od_dirent_journal) {
4911 ldiskfs_htree_unlock(hlock);
4913 up_read(&obj->oo_ext_idx_sem);
4914 dev->od_dirent_journal = 1;
4919 if (unlikely(fid_is_sane(fid))) {
4920 /* FID-in-dirent exists, but FID-in-LMA is lost.
4921 * Trust the FID-in-dirent, and add FID-in-LMA. */
4922 rc = osd_ea_fid_set(info, inode, fid, 0);
4924 *attr |= LUDA_REPAIR;
4926 lu_igif_build(fid, inode->i_ino, inode->i_generation);
4927 /* It is probably IGIF object. Only aappend the
4928 * FID-in-dirent. OI scrub will process FID-in-LMA. */
4929 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4930 fid, bh, de, hlock);
4932 *attr |= LUDA_UPGRADE;
4936 GOTO(out_inode, rc);
4943 if (hlock != NULL) {
4944 ldiskfs_htree_unlock(hlock);
4946 if (dev->od_dirent_journal)
4947 up_write(&obj->oo_ext_idx_sem);
4949 up_read(&obj->oo_ext_idx_sem);
4952 ldiskfs_journal_stop(jh);
4953 if (rc >= 0 && !dirty)
4954 dev->od_dirent_journal = 0;
4959 * Returns the value at current position from iterator's in memory structure.
4961 * \param di struct osd_it_ea, iterator's in memory structure
4962 * \param attr attr requested for dirent.
4963 * \param lde lustre dirent
4965 * \retval 0 no error and \param lde has correct lustre dirent.
4966 * \retval -ve on error
4968 static inline int osd_it_ea_rec(const struct lu_env *env,
4969 const struct dt_it *di,
4970 struct dt_rec *dtrec, __u32 attr)
4972 struct osd_it_ea *it = (struct osd_it_ea *)di;
4973 struct osd_object *obj = it->oie_obj;
4974 struct osd_device *dev = osd_obj2dev(obj);
4975 struct osd_scrub *scrub = &dev->od_scrub;
4976 struct scrub_file *sf = &scrub->os_file;
4977 struct osd_thread_info *oti = osd_oti_get(env);
4978 struct osd_inode_id *id = &oti->oti_id;
4979 struct osd_idmap_cache *oic = &oti->oti_cache;
4980 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4981 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4982 __u32 ino = it->oie_dirent->oied_ino;
4986 if (attr & LUDA_VERIFY) {
4988 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
4989 attr |= LUDA_IGNORE;
4994 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
4996 attr &= ~LU_DIRENT_ATTRS_MASK;
4997 if (!fid_is_sane(fid)) {
4998 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5001 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5003 osd_id_gen(id, ino, OSD_OII_NOGEN);
5011 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5012 it->oie_dirent->oied_name,
5013 it->oie_dirent->oied_namelen,
5014 it->oie_dirent->oied_type, attr);
5016 if (osd_remote_fid(env, dev, fid))
5019 if (likely(!(attr & LUDA_IGNORE))) {
5021 oic->oic_fid = *fid;
5024 if (!(attr & LUDA_VERIFY) &&
5025 (scrub->os_pos_current <= ino) &&
5026 ((sf->sf_flags & SF_INCONSISTENT) ||
5027 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5028 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5029 osd_consistency_check(oti, dev, oic);
5035 * Returns a cookie for current position of the iterator head, so that
5036 * user can use this cookie to load/start the iterator next time.
5038 * \param di iterator's in memory structure
5040 * \retval cookie for current position, on success
5042 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5044 struct osd_it_ea *it = (struct osd_it_ea *)di;
5046 return it->oie_dirent->oied_off;
5050 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5051 * to load a directory entry at a time and stored it i inn,
5052 * in iterator's in-memory data structure.
5054 * \param di struct osd_it_ea, iterator's in memory structure
5056 * \retval +ve on success
5057 * \retval -ve on error
5059 static int osd_it_ea_load(const struct lu_env *env,
5060 const struct dt_it *di, __u64 hash)
5062 struct osd_it_ea *it = (struct osd_it_ea *)di;
5066 it->oie_file.f_pos = hash;
5068 rc = osd_ldiskfs_it_fill(env, di);
5076 * Index lookup function for interoperability mode (b11826).
5078 * \param key, key i.e. file name to be searched
5080 * \retval +ve, on success
5081 * \retval -ve, on error
5083 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5084 struct dt_rec *rec, const struct dt_key *key,
5085 struct lustre_capa *capa)
5087 struct osd_object *obj = osd_dt_obj(dt);
5092 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5093 LINVRNT(osd_invariant(obj));
5095 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5098 rc = osd_ea_lookup_rec(env, obj, rec, key);
5105 * Index and Iterator operations for interoperability
5106 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5108 static const struct dt_index_operations osd_index_ea_ops = {
5109 .dio_lookup = osd_index_ea_lookup,
5110 .dio_declare_insert = osd_index_declare_ea_insert,
5111 .dio_insert = osd_index_ea_insert,
5112 .dio_declare_delete = osd_index_declare_ea_delete,
5113 .dio_delete = osd_index_ea_delete,
5115 .init = osd_it_ea_init,
5116 .fini = osd_it_ea_fini,
5117 .get = osd_it_ea_get,
5118 .put = osd_it_ea_put,
5119 .next = osd_it_ea_next,
5120 .key = osd_it_ea_key,
5121 .key_size = osd_it_ea_key_size,
5122 .rec = osd_it_ea_rec,
5123 .store = osd_it_ea_store,
5124 .load = osd_it_ea_load
5128 static void *osd_key_init(const struct lu_context *ctx,
5129 struct lu_context_key *key)
5131 struct osd_thread_info *info;
5133 OBD_ALLOC_PTR(info);
5135 return ERR_PTR(-ENOMEM);
5137 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5138 if (info->oti_it_ea_buf == NULL)
5141 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5143 info->oti_hlock = ldiskfs_htree_lock_alloc();
5144 if (info->oti_hlock == NULL)
5150 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5153 return ERR_PTR(-ENOMEM);
5156 static void osd_key_fini(const struct lu_context *ctx,
5157 struct lu_context_key *key, void* data)
5159 struct osd_thread_info *info = data;
5161 if (info->oti_hlock != NULL)
5162 ldiskfs_htree_lock_free(info->oti_hlock);
5163 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5164 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5165 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5169 static void osd_key_exit(const struct lu_context *ctx,
5170 struct lu_context_key *key, void *data)
5172 struct osd_thread_info *info = data;
5174 LASSERT(info->oti_r_locks == 0);
5175 LASSERT(info->oti_w_locks == 0);
5176 LASSERT(info->oti_txns == 0);
5179 /* type constructor/destructor: osd_type_init, osd_type_fini */
5180 LU_TYPE_INIT_FINI(osd, &osd_key);
5182 struct lu_context_key osd_key = {
5183 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5184 .lct_init = osd_key_init,
5185 .lct_fini = osd_key_fini,
5186 .lct_exit = osd_key_exit
5190 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5191 const char *name, struct lu_device *next)
5193 struct osd_device *osd = osd_dev(d);
5195 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5196 >= sizeof(osd->od_svname))
5198 return osd_procfs_init(osd, name);
5201 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5205 osd_scrub_cleanup(env, o);
5208 fsfilt_put_ops(o->od_fsops);
5212 /* shutdown quota slave instance associated with the device */
5213 if (o->od_quota_slave != NULL) {
5214 qsd_fini(env, o->od_quota_slave);
5215 o->od_quota_slave = NULL;
5221 static int osd_mount(const struct lu_env *env,
5222 struct osd_device *o, struct lustre_cfg *cfg)
5224 const char *name = lustre_cfg_string(cfg, 0);
5225 const char *dev = lustre_cfg_string(cfg, 1);
5227 unsigned long page, s_flags, lmd_flags = 0;
5228 struct page *__page;
5229 struct file_system_type *type;
5230 char *options = NULL;
5235 if (o->od_mnt != NULL)
5238 if (strlen(dev) >= sizeof(o->od_mntdev))
5240 strcpy(o->od_mntdev, dev);
5242 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5243 if (o->od_fsops == NULL) {
5244 CERROR("Can't find fsfilt_ldiskfs\n");
5248 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5250 GOTO(out, rc = -ENOMEM);
5252 str = lustre_cfg_string(cfg, 2);
5253 s_flags = simple_strtoul(str, NULL, 0);
5254 str = strstr(str, ":");
5256 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5257 opts = lustre_cfg_string(cfg, 3);
5258 page = (unsigned long)cfs_page_address(__page);
5259 options = (char *)page;
5262 strcat(options, "user_xattr,acl");
5264 strcat(options, opts);
5266 /* Glom up mount options */
5267 if (*options != '\0')
5268 strcat(options, ",");
5269 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5271 type = get_fs_type("ldiskfs");
5273 CERROR("%s: cannot find ldiskfs module\n", name);
5274 GOTO(out, rc = -ENODEV);
5277 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5278 cfs_module_put(type->owner);
5280 if (IS_ERR(o->od_mnt)) {
5281 rc = PTR_ERR(o->od_mnt);
5282 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5287 #ifdef HAVE_DEV_SET_RDONLY
5288 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5289 CERROR("%s: underlying device %s is marked as read-only. "
5290 "Setup failed\n", name, dev);
5293 GOTO(out, rc = -EROFS);
5297 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5298 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5299 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5302 GOTO(out, rc = -EINVAL);
5305 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5306 LDISKFS_STATE_LUSTRE_NO_OI);
5307 if (lmd_flags & LMD_FLG_NOSCRUB)
5312 OBD_PAGE_FREE(__page);
5314 fsfilt_put_ops(o->od_fsops);
5319 static struct lu_device *osd_device_fini(const struct lu_env *env,
5320 struct lu_device *d)
5325 rc = osd_shutdown(env, osd_dev(d));
5327 osd_obj_map_fini(osd_dev(d));
5329 shrink_dcache_sb(osd_sb(osd_dev(d)));
5330 osd_sync(env, lu2dt_dev(d));
5332 rc = osd_procfs_fini(osd_dev(d));
5334 CERROR("proc fini error %d \n", rc);
5335 RETURN (ERR_PTR(rc));
5338 if (osd_dev(d)->od_mnt) {
5339 mntput(osd_dev(d)->od_mnt);
5340 osd_dev(d)->od_mnt = NULL;
5346 static int osd_device_init0(const struct lu_env *env,
5347 struct osd_device *o,
5348 struct lustre_cfg *cfg)
5350 struct lu_device *l = osd2lu_dev(o);
5351 struct osd_thread_info *info;
5355 /* if the module was re-loaded, env can loose its keys */
5356 rc = lu_env_refill((struct lu_env *) env);
5359 info = osd_oti_get(env);
5362 l->ld_ops = &osd_lu_ops;
5363 o->od_dt_dev.dd_ops = &osd_dt_ops;
5365 spin_lock_init(&o->od_osfs_lock);
5366 mutex_init(&o->od_otable_mutex);
5367 o->od_osfs_age = cfs_time_shift_64(-1000);
5369 o->od_capa_hash = init_capa_hash();
5370 if (o->od_capa_hash == NULL)
5371 GOTO(out, rc = -ENOMEM);
5373 o->od_read_cache = 1;
5374 o->od_writethrough_cache = 1;
5375 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5377 rc = osd_mount(env, o, cfg);
5381 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5382 /* setup scrub, including OI files initialization */
5383 rc = osd_scrub_setup(env, o);
5387 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5388 sizeof(o->od_svname));
5389 if (cplen >= sizeof(o->od_svname)) {
5394 rc = osd_obj_map_init(env, o);
5396 GOTO(out_scrub, rc);
5398 rc = lu_site_init(&o->od_site, l);
5400 GOTO(out_compat, rc);
5401 o->od_site.ls_bottom_dev = l;
5403 rc = lu_site_init_finish(&o->od_site);
5407 rc = osd_procfs_init(o, o->od_svname);
5409 CERROR("%s: can't initialize procfs: rc = %d\n",
5414 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5416 /* initialize quota slave instance */
5417 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5419 if (IS_ERR(o->od_quota_slave)) {
5420 rc = PTR_ERR(o->od_quota_slave);
5421 o->od_quota_slave = NULL;
5422 GOTO(out_procfs, rc);
5429 lu_site_fini(&o->od_site);
5431 osd_obj_map_fini(o);
5433 osd_scrub_cleanup(env, o);
5435 osd_oi_fini(info, o);
5436 osd_shutdown(env, o);
5440 cleanup_capa_hash(o->od_capa_hash);
5445 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5446 struct lu_device_type *t,
5447 struct lustre_cfg *cfg)
5449 struct osd_device *o;
5454 return ERR_PTR(-ENOMEM);
5456 rc = dt_device_init(&o->od_dt_dev, t);
5458 /* Because the ctx might be revived in dt_device_init,
5459 * refill the env here */
5460 lu_env_refill((struct lu_env *)env);
5461 rc = osd_device_init0(env, o, cfg);
5463 dt_device_fini(&o->od_dt_dev);
5466 if (unlikely(rc != 0))
5469 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5472 static struct lu_device *osd_device_free(const struct lu_env *env,
5473 struct lu_device *d)
5475 struct osd_device *o = osd_dev(d);
5478 cleanup_capa_hash(o->od_capa_hash);
5479 /* XXX: make osd top device in order to release reference */
5480 d->ld_site->ls_top_dev = d;
5481 lu_site_purge(env, d->ld_site, -1);
5482 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5483 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5484 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5486 lu_site_fini(&o->od_site);
5487 dt_device_fini(&o->od_dt_dev);
5492 static int osd_process_config(const struct lu_env *env,
5493 struct lu_device *d, struct lustre_cfg *cfg)
5495 struct osd_device *o = osd_dev(d);
5499 switch(cfg->lcfg_command) {
5501 err = osd_mount(env, o, cfg);
5504 lu_dev_del_linkage(d->ld_site, d);
5505 err = osd_shutdown(env, o);
5514 static int osd_recovery_complete(const struct lu_env *env,
5515 struct lu_device *d)
5517 struct osd_device *osd = osd_dev(d);
5521 if (osd->od_quota_slave == NULL)
5524 /* start qsd instance on recovery completion, this notifies the quota
5525 * slave code that we are about to process new requests now */
5526 rc = qsd_start(env, osd->od_quota_slave);
5531 * we use exports to track all osd users
5533 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5534 struct obd_device *obd, struct obd_uuid *cluuid,
5535 struct obd_connect_data *data, void *localdata)
5537 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5538 struct lustre_handle conn;
5542 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5544 rc = class_connect(&conn, obd, cluuid);
5548 *exp = class_conn2export(&conn);
5550 spin_lock(&osd->od_osfs_lock);
5552 spin_unlock(&osd->od_osfs_lock);
5558 * once last export (we don't count self-export) disappeared
5559 * osd can be released
5561 static int osd_obd_disconnect(struct obd_export *exp)
5563 struct obd_device *obd = exp->exp_obd;
5564 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5565 int rc, release = 0;
5568 /* Only disconnect the underlying layers on the final disconnect. */
5569 spin_lock(&osd->od_osfs_lock);
5571 if (osd->od_connects == 0)
5573 spin_unlock(&osd->od_osfs_lock);
5575 rc = class_disconnect(exp); /* bz 9811 */
5577 if (rc == 0 && release)
5578 class_manual_cleanup(obd);
5582 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5583 struct lu_device *dev)
5585 struct osd_device *osd = osd_dev(dev);
5589 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5590 /* MDT/MDD still use old infrastructure to create
5592 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5598 if (osd->od_quota_slave != NULL)
5599 /* set up quota slave objects */
5600 result = qsd_prepare(env, osd->od_quota_slave);
5605 static const struct lu_object_operations osd_lu_obj_ops = {
5606 .loo_object_init = osd_object_init,
5607 .loo_object_delete = osd_object_delete,
5608 .loo_object_release = osd_object_release,
5609 .loo_object_free = osd_object_free,
5610 .loo_object_print = osd_object_print,
5611 .loo_object_invariant = osd_object_invariant
5614 const struct lu_device_operations osd_lu_ops = {
5615 .ldo_object_alloc = osd_object_alloc,
5616 .ldo_process_config = osd_process_config,
5617 .ldo_recovery_complete = osd_recovery_complete,
5618 .ldo_prepare = osd_prepare,
5621 static const struct lu_device_type_operations osd_device_type_ops = {
5622 .ldto_init = osd_type_init,
5623 .ldto_fini = osd_type_fini,
5625 .ldto_start = osd_type_start,
5626 .ldto_stop = osd_type_stop,
5628 .ldto_device_alloc = osd_device_alloc,
5629 .ldto_device_free = osd_device_free,
5631 .ldto_device_init = osd_device_init,
5632 .ldto_device_fini = osd_device_fini
5635 struct lu_device_type osd_device_type = {
5636 .ldt_tags = LU_DEVICE_DT,
5637 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5638 .ldt_ops = &osd_device_type_ops,
5639 .ldt_ctx_tags = LCT_LOCAL,
5643 * lprocfs legacy support.
5645 static struct obd_ops osd_obd_device_ops = {
5646 .o_owner = THIS_MODULE,
5647 .o_connect = osd_obd_connect,
5648 .o_disconnect = osd_obd_disconnect
5651 static int __init osd_mod_init(void)
5653 struct lprocfs_static_vars lvars;
5656 lprocfs_osd_init_vars(&lvars);
5657 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5658 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5661 static void __exit osd_mod_exit(void)
5663 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5666 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5667 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5668 MODULE_LICENSE("GPL");
5670 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);