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>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 #include <lustre_quota.h>
78 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
79 "ldiskfs with parallel directory operations");
81 static const char dot[] = ".";
82 static const char dotdot[] = "..";
83 static const char remote_obj_dir[] = "REM_OBJ_DIR";
85 static const struct lu_object_operations osd_lu_obj_ops;
86 static const struct dt_object_operations osd_obj_ops;
87 static const struct dt_object_operations osd_obj_ea_ops;
88 static const struct dt_object_operations osd_obj_otable_it_ops;
89 static const struct dt_index_operations osd_index_iam_ops;
90 static const struct dt_index_operations osd_index_ea_ops;
92 #ifdef OSD_TRACK_DECLARES
93 int osd_trans_declare_op2rb[] = {
94 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
95 [OSD_OT_PUNCH] = OSD_OT_MAX,
96 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
97 [OSD_OT_CREATE] = OSD_OT_DESTROY,
98 [OSD_OT_DESTROY] = OSD_OT_CREATE,
99 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
100 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
101 [OSD_OT_WRITE] = OSD_OT_WRITE,
102 [OSD_OT_INSERT] = OSD_OT_DELETE,
103 [OSD_OT_DELETE] = OSD_OT_INSERT,
104 [OSD_OT_QUOTA] = OSD_OT_MAX,
108 static int osd_has_index(const struct osd_object *obj)
110 return obj->oo_dt.do_index_ops != NULL;
113 static int osd_object_invariant(const struct lu_object *l)
115 return osd_invariant(osd_obj(l));
119 * Concurrency: doesn't matter
121 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
123 return osd_oti_get(env)->oti_r_locks > 0;
127 * Concurrency: doesn't matter
129 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
131 struct osd_thread_info *oti = osd_oti_get(env);
132 return oti->oti_w_locks > 0 && o->oo_owner == env;
136 * Concurrency: doesn't access mutable data
138 static int osd_root_get(const struct lu_env *env,
139 struct dt_device *dev, struct lu_fid *f)
141 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
146 * OSD object methods.
150 * Concurrency: no concurrent access is possible that early in object
153 static struct lu_object *osd_object_alloc(const struct lu_env *env,
154 const struct lu_object_header *hdr,
157 struct osd_object *mo;
163 l = &mo->oo_dt.do_lu;
164 dt_object_init(&mo->oo_dt, NULL, d);
165 mo->oo_dt.do_ops = &osd_obj_ea_ops;
166 l->lo_ops = &osd_lu_obj_ops;
167 init_rwsem(&mo->oo_sem);
168 init_rwsem(&mo->oo_ext_idx_sem);
169 spin_lock_init(&mo->oo_guard);
176 static int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
177 struct dentry *dentry, struct lustre_mdt_attrs *lma)
181 dentry->d_inode = inode;
182 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
185 /* try with old lma size */
186 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
187 info->oti_mdt_attrs_old,
190 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
193 /* Check LMA compatibility */
194 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
195 CWARN("%.16s: unsupported incompat LMA feature(s) "
197 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
198 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
202 lustre_lma_swab(lma);
205 } else if (rc == 0) {
213 * retrieve object from backend ext fs.
215 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
216 struct osd_inode_id *id)
218 struct inode *inode = NULL;
220 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
222 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
223 id->oii_ino, PTR_ERR(inode));
224 } else if (id->oii_gen != OSD_OII_NOGEN &&
225 inode->i_generation != id->oii_gen) {
226 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
228 id->oii_ino, id->oii_gen, inode->i_generation);
230 inode = ERR_PTR(-ESTALE);
231 } else if (inode->i_nlink == 0) {
232 /* due to parallel readdir and unlink,
233 * we can have dead inode here. */
234 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
235 make_bad_inode(inode);
237 inode = ERR_PTR(-ESTALE);
238 } else if (is_bad_inode(inode)) {
239 CWARN("%.16s: bad inode: ino = %u\n",
240 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
242 inode = ERR_PTR(-ENOENT);
244 if (id->oii_gen == OSD_OII_NOGEN)
245 osd_id_gen(id, inode->i_ino, inode->i_generation);
247 /* Do not update file c/mtime in ldiskfs.
248 * NB: we don't have any lock to protect this because we don't
249 * have reference on osd_object now, but contention with
250 * another lookup + attr_set can't happen in the tiny window
251 * between if (...) and set S_NOCMTIME. */
252 if (!(inode->i_flags & S_NOCMTIME))
253 inode->i_flags |= S_NOCMTIME;
258 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
259 struct osd_inode_id *id, struct lu_fid *fid)
261 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
265 inode = osd_iget(info, dev, id);
269 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
271 *fid = lma->lma_self_fid;
272 } else if (rc == -ENODATA) {
273 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
281 static struct inode *
282 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
283 struct osd_inode_id *id, const struct lu_fid *fid)
285 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
289 inode = osd_iget(info, dev, id);
293 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
302 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
303 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
304 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
306 return ERR_PTR(-EREMCHG);
312 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
313 const struct lu_fid *fid,
314 const struct lu_object_conf *conf)
316 struct osd_thread_info *info;
317 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
318 struct osd_device *dev;
319 struct osd_idmap_cache *oic;
320 struct osd_inode_id *id;
322 struct osd_scrub *scrub;
323 struct scrub_file *sf;
328 LINVRNT(osd_invariant(obj));
329 LASSERT(obj->oo_inode == NULL);
330 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
333 scrub = &dev->od_scrub;
334 sf = &scrub->os_file;
335 info = osd_oti_get(env);
337 oic = &info->oti_cache;
339 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
342 /* Search order: 1. per-thread cache. */
343 if (lu_fid_eq(fid, &oic->oic_fid)) {
349 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
350 /* Search order: 2. OI scrub pending list. */
351 result = osd_oii_lookup(dev, fid, id);
356 if (sf->sf_flags & SF_INCONSISTENT)
360 * Objects are created as locking anchors or place holders for objects
361 * yet to be created. No need to osd_oi_lookup() at here because FID
362 * shouldn't never be re-used, if it's really a duplicate FID from
363 * unexpected reason, we should be able to detect it later by calling
364 * do_create->osd_oi_insert()
366 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
367 GOTO(out, result = 0);
369 /* Search order: 3. OI files. */
370 result = osd_oi_lookup(info, dev, fid, id);
371 if (result == -ENOENT) {
372 if (!fid_is_norm(fid) ||
373 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
375 GOTO(out, result = 0);
385 inode = osd_iget(info, dev, id);
387 inode = osd_iget_verify(info, dev, id, fid);
389 result = PTR_ERR(inode);
390 if (result == -ENOENT || result == -ESTALE) {
391 fid_zero(&oic->oic_fid);
393 } else if (result == -EREMCHG) {
396 if (thread_is_running(&scrub->os_thread)) {
397 result = -EINPROGRESS;
398 } else if (!dev->od_noscrub) {
399 result = osd_scrub_start(dev);
400 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
401 "for "DFID", rc = %d [1]\n",
402 LDISKFS_SB(osd_sb(dev))->s_es->\
403 s_volume_name,PFID(fid), result);
404 if (result == 0 || result == -EALREADY)
405 result = -EINPROGRESS;
414 obj->oo_inode = inode;
415 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
417 obj->oo_compat_dot_created = 1;
418 obj->oo_compat_dotdot_created = 1;
420 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
421 GOTO(out, result = 0);
423 LASSERT(obj->oo_hl_head == NULL);
424 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
425 if (obj->oo_hl_head == NULL) {
426 obj->oo_inode = NULL;
428 GOTO(out, result = -ENOMEM);
430 GOTO(out, result = 0);
433 LINVRNT(osd_invariant(obj));
438 * Concurrency: shouldn't matter.
440 static void osd_object_init0(struct osd_object *obj)
442 LASSERT(obj->oo_inode != NULL);
443 obj->oo_dt.do_body_ops = &osd_body_ops;
444 obj->oo_dt.do_lu.lo_header->loh_attr |=
445 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
449 * Concurrency: no concurrent access is possible that early in object
452 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
453 const struct lu_object_conf *conf)
455 struct osd_object *obj = osd_obj(l);
458 LINVRNT(osd_invariant(obj));
460 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
461 obj->oo_dt.do_body_ops = &osd_body_ops_new;
463 if (obj->oo_inode != NULL) {
464 osd_object_init0(obj);
465 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
466 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
467 /* LFSCK iterator object is special without inode */
468 l->lo_header->loh_attr |= LOHA_EXISTS;
471 LINVRNT(osd_invariant(obj));
476 * Concurrency: no concurrent access is possible that late in object
479 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
481 struct osd_object *obj = osd_obj(l);
483 LINVRNT(osd_invariant(obj));
485 dt_object_fini(&obj->oo_dt);
486 if (obj->oo_hl_head != NULL)
487 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
492 * Concurrency: no concurrent access is possible that late in object
495 static void osd_index_fini(struct osd_object *o)
497 struct iam_container *bag;
499 if (o->oo_dir != NULL) {
500 bag = &o->oo_dir->od_container;
501 if (o->oo_inode != NULL) {
502 if (bag->ic_object == o->oo_inode)
503 iam_container_fini(bag);
505 OBD_FREE_PTR(o->oo_dir);
511 * Concurrency: no concurrent access is possible that late in object
512 * life-cycle (for all existing callers, that is. New callers have to provide
513 * their own locking.)
515 static int osd_inode_unlinked(const struct inode *inode)
517 return inode->i_nlink == 0;
521 OSD_TXN_OI_DELETE_CREDITS = 20,
522 OSD_TXN_INODE_DELETE_CREDITS = 20
529 #if OSD_THANDLE_STATS
531 * Set time when the handle is allocated
533 static void osd_th_alloced(struct osd_thandle *oth)
535 oth->oth_alloced = cfs_time_current();
539 * Set time when the handle started
541 static void osd_th_started(struct osd_thandle *oth)
543 oth->oth_started = cfs_time_current();
547 * Helper function to convert time interval to microseconds packed in
550 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
554 cfs_duration_usec(cfs_time_sub(end, start), &val);
555 return val.tv_sec * 1000000 + val.tv_usec;
559 * Check whether the we deal with this handle for too long.
561 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
562 cfs_time_t alloced, cfs_time_t started,
565 cfs_time_t now = cfs_time_current();
567 LASSERT(dev != NULL);
569 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
570 interval_to_usec(alloced, started));
571 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
572 interval_to_usec(started, closed));
573 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
574 interval_to_usec(closed, now));
576 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
577 CWARN("transaction handle %p was open for too long: "
579 "alloced "CFS_TIME_T" ,"
580 "started "CFS_TIME_T" ,"
581 "closed "CFS_TIME_T"\n",
582 oth, now, alloced, started, closed);
583 libcfs_debug_dumpstack(NULL);
587 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
589 cfs_time_t __closed = cfs_time_current(); \
590 cfs_time_t __alloced = oth->oth_alloced; \
591 cfs_time_t __started = oth->oth_started; \
594 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
597 #else /* OSD_THANDLE_STATS */
599 #define osd_th_alloced(h) do {} while(0)
600 #define osd_th_started(h) do {} while(0)
601 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
603 #endif /* OSD_THANDLE_STATS */
606 * Concurrency: doesn't access mutable data.
608 static int osd_param_is_not_sane(const struct osd_device *dev,
609 const struct thandle *th)
611 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
613 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
617 * Concurrency: shouldn't matter.
619 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
620 static void osd_trans_commit_cb(struct super_block *sb,
621 struct journal_callback *jcb, int error)
623 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
626 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
627 struct thandle *th = &oh->ot_super;
628 struct lu_device *lud = &th->th_dev->dd_lu_dev;
629 struct dt_txn_commit_cb *dcb, *tmp;
631 LASSERT(oh->ot_handle == NULL);
634 CERROR("transaction @0x%p commit error: %d\n", th, error);
636 dt_txn_hook_commit(th);
638 /* call per-transaction callbacks if any */
639 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
640 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
641 "commit callback entry: magic=%x name='%s'\n",
642 dcb->dcb_magic, dcb->dcb_name);
643 cfs_list_del_init(&dcb->dcb_linkage);
644 dcb->dcb_func(NULL, th, dcb, error);
647 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
651 lu_context_exit(&th->th_ctx);
652 lu_context_fini(&th->th_ctx);
656 static struct thandle *osd_trans_create(const struct lu_env *env,
659 struct osd_thread_info *oti = osd_oti_get(env);
660 struct osd_iobuf *iobuf = &oti->oti_iobuf;
661 struct osd_thandle *oh;
665 /* on pending IO in this thread should left from prev. request */
666 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
668 th = ERR_PTR(-ENOMEM);
669 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
671 oh->ot_quota_trans = &oti->oti_quota_trans;
672 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
676 th->th_tags = LCT_TX_HANDLE;
678 oti->oti_dev = osd_dt_dev(d);
679 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
682 memset(oti->oti_declare_ops, 0, OSD_OT_MAX);
683 memset(oti->oti_declare_ops_rb, 0, OSD_OT_MAX);
684 memset(oti->oti_declare_ops_cred, 0, OSD_OT_MAX);
685 oti->oti_rollback = false;
691 * Concurrency: shouldn't matter.
693 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
696 struct osd_thread_info *oti = osd_oti_get(env);
697 struct osd_device *dev = osd_dt_dev(d);
699 struct osd_thandle *oh;
704 LASSERT(current->journal_info == NULL);
706 oh = container_of0(th, struct osd_thandle, ot_super);
708 LASSERT(oh->ot_handle == NULL);
710 rc = dt_txn_hook_start(env, d, th);
714 if (unlikely(osd_param_is_not_sane(dev, th))) {
715 static unsigned long last_printed;
716 static int last_credits;
718 CWARN("%.16s: too many transaction credits (%d > %d)\n",
719 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
721 osd_journal(dev)->j_max_transaction_buffers);
722 #ifdef OSD_TRACK_DECLARES
723 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
724 oti->oti_declare_ops[OSD_OT_CREATE],
725 oti->oti_declare_ops_cred[OSD_OT_CREATE],
726 oti->oti_declare_ops[OSD_OT_DELETE],
727 oti->oti_declare_ops_cred[OSD_OT_DELETE],
728 oti->oti_declare_ops[OSD_OT_DESTROY],
729 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
730 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
731 oti->oti_declare_ops[OSD_OT_ATTR_SET],
732 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
733 oti->oti_declare_ops[OSD_OT_XATTR_SET],
734 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
735 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
736 oti->oti_declare_ops[OSD_OT_WRITE],
737 oti->oti_declare_ops_cred[OSD_OT_WRITE],
738 oti->oti_declare_ops[OSD_OT_PUNCH],
739 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
740 oti->oti_declare_ops[OSD_OT_QUOTA],
741 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
742 CWARN(" insert: %u/%u, delete: %u/%u\n",
743 oti->oti_declare_ops[OSD_OT_INSERT],
744 oti->oti_declare_ops_cred[OSD_OT_INSERT],
745 oti->oti_declare_ops[OSD_OT_DESTROY],
746 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
747 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
748 oti->oti_declare_ops[OSD_OT_REF_ADD],
749 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
750 oti->oti_declare_ops[OSD_OT_REF_DEL],
751 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
753 if (last_credits != oh->ot_credits &&
754 time_after(jiffies, last_printed + 60 * HZ)) {
755 libcfs_debug_dumpstack(NULL);
756 last_credits = oh->ot_credits;
757 last_printed = jiffies;
760 /* XXX Limit the credits to 'max_transaction_buffers', and
761 * let the underlying filesystem to catch the error if
762 * we really need so many credits.
764 * This should be removed when we can calculate the
765 * credits precisely. */
766 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
770 * XXX temporary stuff. Some abstraction layer should
773 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
777 LASSERT(oti->oti_txns == 0);
778 lu_context_init(&th->th_ctx, th->th_tags);
779 lu_context_enter(&th->th_ctx);
781 lu_device_get(&d->dd_lu_dev);
782 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
794 * Concurrency: shouldn't matter.
796 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
799 struct osd_thandle *oh;
800 struct osd_thread_info *oti = osd_oti_get(env);
801 struct osd_iobuf *iobuf = &oti->oti_iobuf;
802 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
805 oh = container_of0(th, struct osd_thandle, ot_super);
808 /* inform the quota slave device that the transaction is
810 qsd_op_end(env, qsd, oh->ot_quota_trans);
811 oh->ot_quota_trans = NULL;
813 if (oh->ot_handle != NULL) {
814 handle_t *hdl = oh->ot_handle;
817 * add commit callback
818 * notice we don't do this in osd_trans_start()
819 * as underlying transaction can change during truncate
821 osd_journal_callback_set(hdl, osd_trans_commit_cb,
824 LASSERT(oti->oti_txns == 1);
826 rc = dt_txn_hook_stop(env, th);
828 CERROR("Failure in transaction hook: %d\n", rc);
830 /* hook functions might modify th_sync */
831 hdl->h_sync = th->th_sync;
833 oh->ot_handle = NULL;
834 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
835 rc = ldiskfs_journal_stop(hdl));
837 CERROR("Failure to stop transaction: %d\n", rc);
842 /* as we want IO to journal and data IO be concurrent, we don't block
843 * awaiting data IO completion in osd_do_bio(), instead we wait here
844 * once transaction is submitted to the journal. all reqular requests
845 * don't do direct IO (except read/write), thus this wait_event becomes
848 * IMPORTANT: we have to wait till any IO submited by the thread is
849 * completed otherwise iobuf may be corrupted by different request
851 cfs_wait_event(iobuf->dr_wait,
852 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
854 rc = iobuf->dr_error;
859 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
861 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
864 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
865 LASSERT(&dcb->dcb_func != NULL);
866 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
872 * Called just before object is freed. Releases all resources except for
873 * object itself (that is released by osd_object_free()).
875 * Concurrency: no concurrent access is possible that late in object
878 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
880 struct osd_object *obj = osd_obj(l);
881 struct inode *inode = obj->oo_inode;
883 LINVRNT(osd_invariant(obj));
886 * If object is unlinked remove fid->ino mapping from object index.
891 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
892 qid_t uid = inode->i_uid;
893 qid_t gid = inode->i_gid;
896 obj->oo_inode = NULL;
899 struct osd_thread_info *info = osd_oti_get(env);
900 struct lquota_id_info *qi = &info->oti_qi;
902 /* Release granted quota to master if necessary */
903 qi->lqi_id.qid_uid = uid;
904 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
906 qi->lqi_id.qid_uid = gid;
907 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
913 * Concurrency: ->loo_object_release() is called under site spin-lock.
915 static void osd_object_release(const struct lu_env *env,
921 * Concurrency: shouldn't matter.
923 static int osd_object_print(const struct lu_env *env, void *cookie,
924 lu_printer_t p, const struct lu_object *l)
926 struct osd_object *o = osd_obj(l);
929 if (o->oo_dir != NULL)
930 d = o->oo_dir->od_container.ic_descr;
933 return (*p)(env, cookie,
934 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
936 o->oo_inode ? o->oo_inode->i_ino : 0UL,
937 o->oo_inode ? o->oo_inode->i_generation : 0,
938 d ? d->id_ops->id_name : "plain");
942 * Concurrency: shouldn't matter.
944 int osd_statfs(const struct lu_env *env, struct dt_device *d,
945 struct obd_statfs *sfs)
947 struct osd_device *osd = osd_dt_dev(d);
948 struct super_block *sb = osd_sb(osd);
949 struct kstatfs *ksfs;
952 if (unlikely(osd->od_mnt == NULL))
955 /* osd_lproc.c call this without env, allocate ksfs for that case */
956 if (unlikely(env == NULL)) {
961 ksfs = &osd_oti_get(env)->oti_ksfs;
964 spin_lock(&osd->od_osfs_lock);
966 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
967 result = sb->s_op->statfs(sb->s_root, ksfs);
968 if (likely(result == 0)) { /* N.B. statfs can't really fail */
969 osd->od_osfs_age = cfs_time_current_64();
970 statfs_pack(&osd->od_statfs, ksfs);
971 if (sb->s_flags & MS_RDONLY)
972 sfs->os_state = OS_STATE_READONLY;
976 if (likely(result == 0))
977 *sfs = osd->od_statfs;
978 spin_unlock(&osd->od_osfs_lock);
980 if (unlikely(env == NULL))
987 * Estimate space needed for file creations. We assume the largest filename
988 * which is 2^64 - 1, hence a filename of 20 chars.
989 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
991 #ifdef __LDISKFS_DIR_REC_LEN
992 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
994 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
998 * Concurrency: doesn't access mutable data.
1000 static void osd_conf_get(const struct lu_env *env,
1001 const struct dt_device *dev,
1002 struct dt_device_param *param)
1004 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1007 * XXX should be taken from not-yet-existing fs abstraction layer.
1009 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1010 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1011 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1012 param->ddp_block_shift = sb->s_blocksize_bits;
1013 param->ddp_mount_type = LDD_MT_LDISKFS;
1014 param->ddp_maxbytes = sb->s_maxbytes;
1015 /* Overhead estimate should be fairly accurate, so we really take a tiny
1016 * error margin which also avoids fragmenting the filesystem too much */
1017 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1018 /* inode are statically allocated, so per-inode space consumption
1019 * is the space consumed by the directory entry */
1020 param->ddp_inodespace = PER_OBJ_USAGE;
1021 /* per-fragment overhead to be used by the client code */
1022 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1023 param->ddp_mntopts = 0;
1024 if (test_opt(sb, XATTR_USER))
1025 param->ddp_mntopts |= MNTOPT_USERXATTR;
1026 if (test_opt(sb, POSIX_ACL))
1027 param->ddp_mntopts |= MNTOPT_ACL;
1029 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1030 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1031 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1034 param->ddp_max_ea_size = sb->s_blocksize;
1039 * Helper function to get and fill the buffer with input values.
1041 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1045 buf = &osd_oti_get(env)->oti_buf;
1052 * Concurrency: shouldn't matter.
1054 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1056 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1057 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1061 * Start commit for OSD device.
1063 * An implementation of dt_commit_async method for OSD device.
1064 * Asychronously starts underlayng fs sync and thereby a transaction
1067 * \param env environment
1068 * \param d dt device
1070 * \see dt_device_operations
1072 static int osd_commit_async(const struct lu_env *env,
1073 struct dt_device *d)
1075 struct super_block *s = osd_sb(osd_dt_dev(d));
1078 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1079 RETURN(s->s_op->sync_fs(s, 0));
1083 * Concurrency: shouldn't matter.
1086 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1088 struct super_block *sb = osd_sb(osd_dt_dev(d));
1092 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1094 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1099 * Concurrency: serialization provided by callers.
1101 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1102 int mode, unsigned long timeout, __u32 alg,
1103 struct lustre_capa_key *keys)
1105 struct osd_device *dev = osd_dt_dev(d);
1108 dev->od_fl_capa = mode;
1109 dev->od_capa_timeout = timeout;
1110 dev->od_capa_alg = alg;
1111 dev->od_capa_keys = keys;
1116 * Note: we do not count into QUOTA here.
1117 * If we mount with --data_journal we may need more.
1119 const int osd_dto_credits_noquota[DTO_NR] = {
1122 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1123 * SINGLEDATA_TRANS_BLOCKS(8)
1124 * XXX Note: maybe iam need more, since iam have more level than
1127 [DTO_INDEX_INSERT] = 16,
1128 [DTO_INDEX_DELETE] = 16,
1132 [DTO_INDEX_UPDATE] = 16,
1134 * Create a object. The same as create object in EXT3.
1135 * DATA_TRANS_BLOCKS(14) +
1136 * INDEX_EXTRA_BLOCKS(8) +
1137 * 3(inode bits, groups, GDT)
1139 [DTO_OBJECT_CREATE] = 25,
1141 * XXX: real credits to be fixed
1143 [DTO_OBJECT_DELETE] = 25,
1145 * Attr set credits (inode)
1147 [DTO_ATTR_SET_BASE] = 1,
1149 * Xattr set. The same as xattr of EXT3.
1150 * DATA_TRANS_BLOCKS(14)
1151 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1152 * are also counted in. Do not know why?
1154 [DTO_XATTR_SET] = 14,
1157 * credits for inode change during write.
1159 [DTO_WRITE_BASE] = 3,
1161 * credits for single block write.
1163 [DTO_WRITE_BLOCK] = 14,
1165 * Attr set credits for chown.
1166 * This is extra credits for setattr, and it is null without quota
1168 [DTO_ATTR_SET_CHOWN]= 0
1171 static const struct dt_device_operations osd_dt_ops = {
1172 .dt_root_get = osd_root_get,
1173 .dt_statfs = osd_statfs,
1174 .dt_trans_create = osd_trans_create,
1175 .dt_trans_start = osd_trans_start,
1176 .dt_trans_stop = osd_trans_stop,
1177 .dt_trans_cb_add = osd_trans_cb_add,
1178 .dt_conf_get = osd_conf_get,
1179 .dt_sync = osd_sync,
1181 .dt_commit_async = osd_commit_async,
1182 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1185 static void osd_object_read_lock(const struct lu_env *env,
1186 struct dt_object *dt, unsigned role)
1188 struct osd_object *obj = osd_dt_obj(dt);
1189 struct osd_thread_info *oti = osd_oti_get(env);
1191 LINVRNT(osd_invariant(obj));
1193 LASSERT(obj->oo_owner != env);
1194 down_read_nested(&obj->oo_sem, role);
1196 LASSERT(obj->oo_owner == NULL);
1200 static void osd_object_write_lock(const struct lu_env *env,
1201 struct dt_object *dt, unsigned role)
1203 struct osd_object *obj = osd_dt_obj(dt);
1204 struct osd_thread_info *oti = osd_oti_get(env);
1206 LINVRNT(osd_invariant(obj));
1208 LASSERT(obj->oo_owner != env);
1209 down_write_nested(&obj->oo_sem, role);
1211 LASSERT(obj->oo_owner == NULL);
1212 obj->oo_owner = env;
1216 static void osd_object_read_unlock(const struct lu_env *env,
1217 struct dt_object *dt)
1219 struct osd_object *obj = osd_dt_obj(dt);
1220 struct osd_thread_info *oti = osd_oti_get(env);
1222 LINVRNT(osd_invariant(obj));
1224 LASSERT(oti->oti_r_locks > 0);
1226 up_read(&obj->oo_sem);
1229 static void osd_object_write_unlock(const struct lu_env *env,
1230 struct dt_object *dt)
1232 struct osd_object *obj = osd_dt_obj(dt);
1233 struct osd_thread_info *oti = osd_oti_get(env);
1235 LINVRNT(osd_invariant(obj));
1237 LASSERT(obj->oo_owner == env);
1238 LASSERT(oti->oti_w_locks > 0);
1240 obj->oo_owner = NULL;
1241 up_write(&obj->oo_sem);
1244 static int osd_object_write_locked(const struct lu_env *env,
1245 struct dt_object *dt)
1247 struct osd_object *obj = osd_dt_obj(dt);
1249 LINVRNT(osd_invariant(obj));
1251 return obj->oo_owner == env;
1254 static int capa_is_sane(const struct lu_env *env,
1255 struct osd_device *dev,
1256 struct lustre_capa *capa,
1257 struct lustre_capa_key *keys)
1259 struct osd_thread_info *oti = osd_oti_get(env);
1260 struct lustre_capa *tcapa = &oti->oti_capa;
1261 struct obd_capa *oc;
1265 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1267 if (capa_is_expired(oc)) {
1268 DEBUG_CAPA(D_ERROR, capa, "expired");
1275 if (capa_is_expired_sec(capa)) {
1276 DEBUG_CAPA(D_ERROR, capa, "expired");
1280 spin_lock(&capa_lock);
1281 for (i = 0; i < 2; i++) {
1282 if (keys[i].lk_keyid == capa->lc_keyid) {
1283 oti->oti_capa_key = keys[i];
1287 spin_unlock(&capa_lock);
1290 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1294 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1298 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1299 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1303 oc = capa_add(dev->od_capa_hash, capa);
1309 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1310 struct lustre_capa *capa, __u64 opc)
1312 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1313 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1314 struct md_capainfo *ci;
1317 if (!dev->od_fl_capa)
1320 if (capa == BYPASS_CAPA)
1323 ci = md_capainfo(env);
1327 if (ci->mc_auth == LC_ID_NONE)
1331 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1335 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1336 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1341 if (!capa_opc_supported(capa, opc)) {
1342 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1346 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1347 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1354 static struct timespec *osd_inode_time(const struct lu_env *env,
1355 struct inode *inode, __u64 seconds)
1357 struct osd_thread_info *oti = osd_oti_get(env);
1358 struct timespec *t = &oti->oti_time;
1360 t->tv_sec = seconds;
1362 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1367 static void osd_inode_getattr(const struct lu_env *env,
1368 struct inode *inode, struct lu_attr *attr)
1370 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1371 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1372 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1374 attr->la_atime = LTIME_S(inode->i_atime);
1375 attr->la_mtime = LTIME_S(inode->i_mtime);
1376 attr->la_ctime = LTIME_S(inode->i_ctime);
1377 attr->la_mode = inode->i_mode;
1378 attr->la_size = i_size_read(inode);
1379 attr->la_blocks = inode->i_blocks;
1380 attr->la_uid = inode->i_uid;
1381 attr->la_gid = inode->i_gid;
1382 attr->la_flags = LDISKFS_I(inode)->i_flags;
1383 attr->la_nlink = inode->i_nlink;
1384 attr->la_rdev = inode->i_rdev;
1385 attr->la_blksize = 1 << inode->i_blkbits;
1386 attr->la_blkbits = inode->i_blkbits;
1389 static int osd_attr_get(const struct lu_env *env,
1390 struct dt_object *dt,
1391 struct lu_attr *attr,
1392 struct lustre_capa *capa)
1394 struct osd_object *obj = osd_dt_obj(dt);
1396 LASSERT(dt_object_exists(dt));
1397 LINVRNT(osd_invariant(obj));
1399 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1402 spin_lock(&obj->oo_guard);
1403 osd_inode_getattr(env, obj->oo_inode, attr);
1404 spin_unlock(&obj->oo_guard);
1408 static int osd_declare_attr_set(const struct lu_env *env,
1409 struct dt_object *dt,
1410 const struct lu_attr *attr,
1411 struct thandle *handle)
1413 struct osd_thandle *oh;
1414 struct osd_object *obj;
1415 struct osd_thread_info *info = osd_oti_get(env);
1416 struct lquota_id_info *qi = &info->oti_qi;
1422 LASSERT(dt != NULL);
1423 LASSERT(handle != NULL);
1425 obj = osd_dt_obj(dt);
1426 LASSERT(osd_invariant(obj));
1428 oh = container_of0(handle, struct osd_thandle, ot_super);
1429 LASSERT(oh->ot_handle == NULL);
1431 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1432 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1434 if (attr == NULL || obj->oo_inode == NULL)
1437 bspace = obj->oo_inode->i_blocks;
1438 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1439 bspace = toqb(bspace);
1441 /* Changing ownership is always preformed by super user, it should not
1444 * We still need to call the osd_declare_qid() to calculate the journal
1445 * credits for updating quota accounting files and to trigger quota
1446 * space adjustment once the operation is completed.*/
1447 if ((attr->la_valid & LA_UID) != 0 &&
1448 attr->la_uid != obj->oo_inode->i_uid) {
1449 qi->lqi_type = USRQUOTA;
1451 /* inode accounting */
1452 qi->lqi_is_blk = false;
1454 /* one more inode for the new owner ... */
1455 qi->lqi_id.qid_uid = attr->la_uid;
1457 allocated = (attr->la_uid == 0) ? true : false;
1458 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1459 if (rc == -EDQUOT || rc == -EINPROGRESS)
1464 /* and one less inode for the current uid */
1465 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1467 rc = osd_declare_qid(env, oh, qi, true, NULL);
1468 if (rc == -EDQUOT || rc == -EINPROGRESS)
1473 /* block accounting */
1474 qi->lqi_is_blk = true;
1476 /* more blocks for the new owner ... */
1477 qi->lqi_id.qid_uid = attr->la_uid;
1478 qi->lqi_space = bspace;
1479 allocated = (attr->la_uid == 0) ? true : false;
1480 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1481 if (rc == -EDQUOT || rc == -EINPROGRESS)
1486 /* and finally less blocks for the current owner */
1487 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1488 qi->lqi_space = -bspace;
1489 rc = osd_declare_qid(env, oh, qi, true, NULL);
1490 if (rc == -EDQUOT || rc == -EINPROGRESS)
1496 if (attr->la_valid & LA_GID &&
1497 attr->la_gid != obj->oo_inode->i_gid) {
1498 qi->lqi_type = GRPQUOTA;
1500 /* inode accounting */
1501 qi->lqi_is_blk = false;
1503 /* one more inode for the new group owner ... */
1504 qi->lqi_id.qid_gid = attr->la_gid;
1506 allocated = (attr->la_gid == 0) ? true : false;
1507 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1508 if (rc == -EDQUOT || rc == -EINPROGRESS)
1513 /* and one less inode for the current gid */
1514 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1516 rc = osd_declare_qid(env, oh, qi, true, NULL);
1517 if (rc == -EDQUOT || rc == -EINPROGRESS)
1522 /* block accounting */
1523 qi->lqi_is_blk = true;
1525 /* more blocks for the new owner ... */
1526 qi->lqi_id.qid_gid = attr->la_gid;
1527 qi->lqi_space = bspace;
1528 allocated = (attr->la_gid == 0) ? true : false;
1529 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1530 if (rc == -EDQUOT || rc == -EINPROGRESS)
1535 /* and finally less blocks for the current owner */
1536 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1537 qi->lqi_space = -bspace;
1538 rc = osd_declare_qid(env, oh, qi, true, NULL);
1539 if (rc == -EDQUOT || rc == -EINPROGRESS)
1548 static int osd_inode_setattr(const struct lu_env *env,
1549 struct inode *inode, const struct lu_attr *attr)
1553 bits = attr->la_valid;
1555 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1557 if (bits & LA_ATIME)
1558 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1559 if (bits & LA_CTIME)
1560 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1561 if (bits & LA_MTIME)
1562 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1563 if (bits & LA_SIZE) {
1564 LDISKFS_I(inode)->i_disksize = attr->la_size;
1565 i_size_write(inode, attr->la_size);
1569 /* OSD should not change "i_blocks" which is used by quota.
1570 * "i_blocks" should be changed by ldiskfs only. */
1571 if (bits & LA_BLOCKS)
1572 inode->i_blocks = attr->la_blocks;
1575 inode->i_mode = (inode->i_mode & S_IFMT) |
1576 (attr->la_mode & ~S_IFMT);
1578 inode->i_uid = attr->la_uid;
1580 inode->i_gid = attr->la_gid;
1581 if (bits & LA_NLINK)
1582 set_nlink(inode, attr->la_nlink);
1584 inode->i_rdev = attr->la_rdev;
1586 if (bits & LA_FLAGS) {
1587 /* always keep S_NOCMTIME */
1588 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1594 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1596 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1597 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1602 if (attr->la_valid & LA_UID)
1603 iattr.ia_valid |= ATTR_UID;
1604 if (attr->la_valid & LA_GID)
1605 iattr.ia_valid |= ATTR_GID;
1606 iattr.ia_uid = attr->la_uid;
1607 iattr.ia_gid = attr->la_gid;
1609 rc = ll_vfs_dq_transfer(inode, &iattr);
1611 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1612 "enforcement enabled on the ldiskfs filesystem?",
1613 inode->i_sb->s_id, rc);
1620 static int osd_attr_set(const struct lu_env *env,
1621 struct dt_object *dt,
1622 const struct lu_attr *attr,
1623 struct thandle *handle,
1624 struct lustre_capa *capa)
1626 struct osd_object *obj = osd_dt_obj(dt);
1627 struct inode *inode;
1630 LASSERT(handle != NULL);
1631 LASSERT(dt_object_exists(dt));
1632 LASSERT(osd_invariant(obj));
1634 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1637 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1639 inode = obj->oo_inode;
1640 ll_vfs_dq_init(inode);
1642 rc = osd_quota_transfer(inode, attr);
1646 spin_lock(&obj->oo_guard);
1647 rc = osd_inode_setattr(env, inode, attr);
1648 spin_unlock(&obj->oo_guard);
1651 inode->i_sb->s_op->dirty_inode(inode);
1655 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1656 struct osd_object *obj,
1657 const char *name, const int namelen)
1659 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1662 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1664 struct dt_allocation_hint *hint,
1668 struct osd_device *osd = osd_obj2dev(obj);
1669 struct osd_thandle *oth;
1670 struct dt_object *parent = NULL;
1671 struct inode *inode;
1673 LINVRNT(osd_invariant(obj));
1674 LASSERT(obj->oo_inode == NULL);
1675 LASSERT(obj->oo_hl_head == NULL);
1677 if (S_ISDIR(mode) && ldiskfs_pdo) {
1678 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1679 if (obj->oo_hl_head == NULL)
1683 oth = container_of(th, struct osd_thandle, ot_super);
1684 LASSERT(oth->ot_handle->h_transaction != NULL);
1686 if (hint && hint->dah_parent)
1687 parent = hint->dah_parent;
1689 inode = ldiskfs_create_inode(oth->ot_handle,
1690 parent ? osd_dt_obj(parent)->oo_inode :
1691 osd_sb(osd)->s_root->d_inode,
1693 if (!IS_ERR(inode)) {
1694 /* Do not update file c/mtime in ldiskfs.
1695 * NB: don't need any lock because no contention at this
1697 inode->i_flags |= S_NOCMTIME;
1698 inode->i_state |= I_LUSTRE_NOSCRUB;
1699 obj->oo_inode = inode;
1702 if (obj->oo_hl_head != NULL) {
1703 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1704 obj->oo_hl_head = NULL;
1706 result = PTR_ERR(inode);
1708 LINVRNT(osd_invariant(obj));
1716 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1717 struct lu_attr *attr,
1718 struct dt_allocation_hint *hint,
1719 struct dt_object_format *dof,
1723 struct osd_thandle *oth;
1724 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1726 LASSERT(S_ISDIR(attr->la_mode));
1728 oth = container_of(th, struct osd_thandle, ot_super);
1729 LASSERT(oth->ot_handle->h_transaction != NULL);
1730 result = osd_mkfile(info, obj, mode, hint, th);
1735 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1736 struct lu_attr *attr,
1737 struct dt_allocation_hint *hint,
1738 struct dt_object_format *dof,
1742 struct osd_thandle *oth;
1743 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1745 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1747 LASSERT(S_ISREG(attr->la_mode));
1749 oth = container_of(th, struct osd_thandle, ot_super);
1750 LASSERT(oth->ot_handle->h_transaction != NULL);
1752 result = osd_mkfile(info, obj, mode, hint, th);
1754 LASSERT(obj->oo_inode != NULL);
1755 if (feat->dif_flags & DT_IND_VARKEY)
1756 result = iam_lvar_create(obj->oo_inode,
1757 feat->dif_keysize_max,
1759 feat->dif_recsize_max,
1762 result = iam_lfix_create(obj->oo_inode,
1763 feat->dif_keysize_max,
1765 feat->dif_recsize_max,
1772 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1773 struct lu_attr *attr,
1774 struct dt_allocation_hint *hint,
1775 struct dt_object_format *dof,
1778 LASSERT(S_ISREG(attr->la_mode));
1779 return osd_mkfile(info, obj, (attr->la_mode &
1780 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1783 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1784 struct lu_attr *attr,
1785 struct dt_allocation_hint *hint,
1786 struct dt_object_format *dof,
1789 LASSERT(S_ISLNK(attr->la_mode));
1790 return osd_mkfile(info, obj, (attr->la_mode &
1791 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1794 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1795 struct lu_attr *attr,
1796 struct dt_allocation_hint *hint,
1797 struct dt_object_format *dof,
1800 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1803 LINVRNT(osd_invariant(obj));
1804 LASSERT(obj->oo_inode == NULL);
1805 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1806 S_ISFIFO(mode) || S_ISSOCK(mode));
1808 result = osd_mkfile(info, obj, mode, hint, th);
1810 LASSERT(obj->oo_inode != NULL);
1812 * This inode should be marked dirty for i_rdev. Currently
1813 * that is done in the osd_attr_init().
1815 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1818 LINVRNT(osd_invariant(obj));
1822 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1824 struct dt_allocation_hint *hint,
1825 struct dt_object_format *dof,
1828 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1830 osd_obj_type_f result;
1846 result = osd_mk_index;
1857 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1858 struct dt_object *parent, struct dt_object *child,
1859 cfs_umode_t child_mode)
1863 memset(ah, 0, sizeof(*ah));
1864 ah->dah_parent = parent;
1865 ah->dah_mode = child_mode;
1868 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1869 struct lu_attr *attr, struct dt_object_format *dof)
1871 struct inode *inode = obj->oo_inode;
1872 __u64 valid = attr->la_valid;
1875 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1877 if (dof->dof_type != DFT_NODE)
1878 attr->la_valid &= ~LA_RDEV;
1879 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1880 attr->la_valid &= ~LA_ATIME;
1881 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1882 attr->la_valid &= ~LA_CTIME;
1883 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1884 attr->la_valid &= ~LA_MTIME;
1886 result = osd_quota_transfer(inode, attr);
1890 if (attr->la_valid != 0) {
1891 result = osd_inode_setattr(info->oti_env, inode, attr);
1893 * The osd_inode_setattr() should always succeed here. The
1894 * only error that could be returned is EDQUOT when we are
1895 * trying to change the UID or GID of the inode. However, this
1896 * should not happen since quota enforcement is no longer
1897 * enabled on ldiskfs (lquota takes care of it).
1899 LASSERTF(result == 0, "%d", result);
1900 inode->i_sb->s_op->dirty_inode(inode);
1903 attr->la_valid = valid;
1907 * Helper function for osd_object_create()
1909 * \retval 0, on success
1911 static int __osd_object_create(struct osd_thread_info *info,
1912 struct osd_object *obj, struct lu_attr *attr,
1913 struct dt_allocation_hint *hint,
1914 struct dt_object_format *dof,
1919 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1922 osd_attr_init(info, obj, attr, dof);
1923 osd_object_init0(obj);
1925 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1926 unlock_new_inode(obj->oo_inode);
1933 * Helper function for osd_object_create()
1935 * \retval 0, on success
1937 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1938 const struct lu_fid *fid, struct thandle *th)
1940 struct osd_thread_info *info = osd_oti_get(env);
1941 struct osd_inode_id *id = &info->oti_id;
1942 struct osd_device *osd = osd_obj2dev(obj);
1944 LASSERT(obj->oo_inode != NULL);
1946 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1947 return osd_oi_insert(info, osd, fid, id, th);
1950 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1951 const struct lu_fid *fid, struct lu_seq_range *range)
1953 struct seq_server_site *ss = osd_seq_site(osd);
1956 if (fid_is_igif(fid)) {
1957 range->lsr_flags = LU_SEQ_RANGE_MDT;
1958 range->lsr_index = 0;
1962 if (fid_is_idif(fid)) {
1963 range->lsr_flags = LU_SEQ_RANGE_OST;
1964 range->lsr_index = fid_idif_ost_idx(fid);
1968 if (!fid_is_norm(fid)) {
1969 range->lsr_flags = LU_SEQ_RANGE_MDT;
1971 /* FIXME: If ss is NULL, it suppose not get lsr_index
1973 range->lsr_index = ss->ss_node_id;
1977 LASSERT(ss != NULL);
1978 range->lsr_flags = -1;
1979 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1981 CERROR("%s can not find "DFID": rc = %d\n",
1982 osd2lu_dev(osd)->ld_obd->obd_name, PFID(fid), rc);
1988 static int osd_declare_object_create(const struct lu_env *env,
1989 struct dt_object *dt,
1990 struct lu_attr *attr,
1991 struct dt_allocation_hint *hint,
1992 struct dt_object_format *dof,
1993 struct thandle *handle)
1995 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1996 struct osd_thandle *oh;
2000 LASSERT(handle != NULL);
2002 oh = container_of0(handle, struct osd_thandle, ot_super);
2003 LASSERT(oh->ot_handle == NULL);
2005 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2006 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2007 /* XXX: So far, only normal fid needs be inserted into the oi,
2008 * things could be changed later. Revise following code then. */
2009 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2010 !fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2011 lu_object_fid(&dt->do_lu))) {
2012 /* Reuse idle OI block may cause additional one OI block
2014 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2015 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2017 /* If this is directory, then we expect . and .. to be inserted as
2018 * well. The one directory block always needs to be created for the
2019 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2020 * block), there is no danger of needing a tree for the first block.
2022 if (attr && S_ISDIR(attr->la_mode)) {
2023 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2024 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2025 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2031 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2032 false, false, NULL, false);
2036 /* It does fld look up inside declare, and the result will be
2037 * added to fld cache, so the following fld lookup inside insert
2038 * does not need send RPC anymore, so avoid send rpc with holding
2040 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2041 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2042 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2043 lu_object_fid(&dt->do_lu), range);
2049 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2050 struct lu_attr *attr,
2051 struct dt_allocation_hint *hint,
2052 struct dt_object_format *dof,
2055 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2056 struct osd_object *obj = osd_dt_obj(dt);
2057 struct osd_thread_info *info = osd_oti_get(env);
2062 LINVRNT(osd_invariant(obj));
2063 LASSERT(!dt_object_exists(dt));
2064 LASSERT(osd_write_locked(env, obj));
2065 LASSERT(th != NULL);
2067 if (unlikely(fid_is_acct(fid)))
2068 /* Quota files can't be created from the kernel any more,
2069 * 'tune2fs -O quota' will take care of creating them */
2072 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2073 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2075 result = __osd_object_create(info, obj, attr, hint, dof, th);
2077 result = __osd_oi_insert(env, obj, fid, th);
2079 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2080 LASSERT(osd_invariant(obj));
2085 * Called to destroy on-disk representation of the object
2087 * Concurrency: must be locked
2089 static int osd_declare_object_destroy(const struct lu_env *env,
2090 struct dt_object *dt,
2093 struct osd_object *obj = osd_dt_obj(dt);
2094 struct inode *inode = obj->oo_inode;
2095 struct osd_thandle *oh;
2099 oh = container_of0(th, struct osd_thandle, ot_super);
2100 LASSERT(oh->ot_handle == NULL);
2103 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2104 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2105 /* XXX: So far, only normal fid needs to be inserted into the OI,
2106 * so only normal fid needs to be removed from the OI also.
2107 * Recycle idle OI leaf may cause additional three OI blocks
2109 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2110 fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2111 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3 : 0);
2113 /* one less inode */
2114 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2115 false, true, NULL, false);
2118 /* data to be truncated */
2119 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2120 true, true, NULL, false);
2124 static int osd_object_destroy(const struct lu_env *env,
2125 struct dt_object *dt,
2128 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2129 struct osd_object *obj = osd_dt_obj(dt);
2130 struct inode *inode = obj->oo_inode;
2131 struct osd_device *osd = osd_obj2dev(obj);
2132 struct osd_thandle *oh;
2136 oh = container_of0(th, struct osd_thandle, ot_super);
2137 LASSERT(oh->ot_handle);
2139 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2141 if (unlikely(fid_is_acct(fid)))
2144 /* Parallel control for OI scrub. For most of cases, there is no
2145 * lock contention. So it will not affect unlink performance. */
2146 mutex_lock(&inode->i_mutex);
2147 if (S_ISDIR(inode->i_mode)) {
2148 LASSERT(osd_inode_unlinked(inode) ||
2149 inode->i_nlink == 1);
2150 spin_lock(&obj->oo_guard);
2152 spin_unlock(&obj->oo_guard);
2153 inode->i_sb->s_op->dirty_inode(inode);
2156 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2158 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2159 mutex_unlock(&inode->i_mutex);
2161 /* XXX: add to ext3 orphan list */
2162 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2164 /* not needed in the cache anymore */
2165 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2171 * Helper function for osd_xattr_set()
2173 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2174 const struct lu_buf *buf, const char *name, int fl)
2176 struct osd_object *obj = osd_dt_obj(dt);
2177 struct inode *inode = obj->oo_inode;
2178 struct osd_thread_info *info = osd_oti_get(env);
2179 struct dentry *dentry = &info->oti_child_dentry;
2183 LASSERT(dt_object_exists(dt));
2184 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2186 if (fl & LU_XATTR_REPLACE)
2187 fs_flags |= XATTR_REPLACE;
2189 if (fl & LU_XATTR_CREATE)
2190 fs_flags |= XATTR_CREATE;
2192 ll_vfs_dq_init(inode);
2193 dentry->d_inode = inode;
2194 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2195 buf->lb_len, fs_flags);
2200 * Put the fid into lustre_mdt_attrs, and then place the structure
2201 * inode's ea. This fid should not be altered during the life time
2204 * \retval +ve, on success
2205 * \retval -ve, on error
2207 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2209 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2210 const struct lu_fid *fid)
2212 struct osd_thread_info *info = osd_oti_get(env);
2213 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2215 lustre_lma_init(mdt_attrs, fid);
2216 lustre_lma_swab(mdt_attrs);
2217 return __osd_xattr_set(env, dt,
2218 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2219 XATTR_NAME_LMA, LU_XATTR_CREATE);
2224 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2225 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2226 * To have compatilibility with 1.8 ldiskfs driver we need to have
2227 * magic number at start of fid data.
2228 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2231 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2232 const struct dt_rec *fid)
2234 param->edp_magic = LDISKFS_LUFID_MAGIC;
2235 param->edp_len = sizeof(struct lu_fid) + 1;
2237 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2238 (struct lu_fid *)fid);
2242 * Try to read the fid from inode ea into dt_rec, if return value
2243 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2245 * \param fid object fid.
2247 * \retval 0 on success
2249 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2250 __u32 ino, struct lu_fid *fid,
2251 struct osd_inode_id *id)
2253 struct osd_thread_info *info = osd_oti_get(env);
2254 struct inode *inode;
2257 osd_id_gen(id, ino, OSD_OII_NOGEN);
2258 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2260 RETURN(PTR_ERR(inode));
2267 * OSD layer object create function for interoperability mode (b11826).
2268 * This is mostly similar to osd_object_create(). Only difference being, fid is
2269 * inserted into inode ea here.
2271 * \retval 0, on success
2272 * \retval -ve, on error
2274 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2275 struct lu_attr *attr,
2276 struct dt_allocation_hint *hint,
2277 struct dt_object_format *dof,
2280 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2281 struct osd_object *obj = osd_dt_obj(dt);
2282 struct osd_thread_info *info = osd_oti_get(env);
2287 LASSERT(osd_invariant(obj));
2288 LASSERT(!dt_object_exists(dt));
2289 LASSERT(osd_write_locked(env, obj));
2290 LASSERT(th != NULL);
2292 if (unlikely(fid_is_acct(fid)))
2293 /* Quota files can't be created from the kernel any more,
2294 * 'tune2fs -O quota' will take care of creating them */
2297 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2298 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2300 result = __osd_object_create(info, obj, attr, hint, dof, th);
2301 /* objects under osd root shld have igif fid, so dont add fid EA */
2302 /* For ost object, the fid will be stored during first write */
2303 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL &&
2304 !fid_is_on_ost(info, osd_dt_dev(th->th_dev), fid))
2305 result = osd_ea_fid_set(env, dt, fid);
2308 result = __osd_oi_insert(env, obj, fid, th);
2310 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2311 LINVRNT(osd_invariant(obj));
2315 static int osd_declare_object_ref_add(const struct lu_env *env,
2316 struct dt_object *dt,
2317 struct thandle *handle)
2319 struct osd_thandle *oh;
2321 /* it's possible that object doesn't exist yet */
2322 LASSERT(handle != NULL);
2324 oh = container_of0(handle, struct osd_thandle, ot_super);
2325 LASSERT(oh->ot_handle == NULL);
2327 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2328 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2334 * Concurrency: @dt is write locked.
2336 static int osd_object_ref_add(const struct lu_env *env,
2337 struct dt_object *dt, struct thandle *th)
2339 struct osd_object *obj = osd_dt_obj(dt);
2340 struct inode *inode = obj->oo_inode;
2342 LINVRNT(osd_invariant(obj));
2343 LASSERT(dt_object_exists(dt));
2344 LASSERT(osd_write_locked(env, obj));
2345 LASSERT(th != NULL);
2347 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2350 * DIR_NLINK feature is set for compatibility reasons if:
2351 * 1) nlinks > LDISKFS_LINK_MAX, or
2352 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2354 * It is easier to always set this flag (rather than check and set),
2355 * since it has less overhead, and the superblock will be dirtied
2356 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2357 * do not actually care whether this flag is set or not.
2359 spin_lock(&obj->oo_guard);
2360 /* inc_nlink from 0 may cause WARN_ON */
2361 if(inode->i_nlink == 0)
2362 set_nlink(inode, 1);
2365 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2366 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2367 inode->i_nlink == 2)
2368 set_nlink(inode, 1);
2370 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2371 spin_unlock(&obj->oo_guard);
2372 inode->i_sb->s_op->dirty_inode(inode);
2373 LINVRNT(osd_invariant(obj));
2378 static int osd_declare_object_ref_del(const struct lu_env *env,
2379 struct dt_object *dt,
2380 struct thandle *handle)
2382 struct osd_thandle *oh;
2384 LASSERT(dt_object_exists(dt));
2385 LASSERT(handle != NULL);
2387 oh = container_of0(handle, struct osd_thandle, ot_super);
2388 LASSERT(oh->ot_handle == NULL);
2390 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2391 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2397 * Concurrency: @dt is write locked.
2399 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2402 struct osd_object *obj = osd_dt_obj(dt);
2403 struct inode *inode = obj->oo_inode;
2405 LINVRNT(osd_invariant(obj));
2406 LASSERT(dt_object_exists(dt));
2407 LASSERT(osd_write_locked(env, obj));
2408 LASSERT(th != NULL);
2410 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2412 spin_lock(&obj->oo_guard);
2413 LASSERT(inode->i_nlink > 0);
2415 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2416 * then the nlink count is 1. Don't let it be set to 0 or the directory
2417 * inode will be deleted incorrectly. */
2418 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2419 set_nlink(inode, 1);
2420 spin_unlock(&obj->oo_guard);
2421 inode->i_sb->s_op->dirty_inode(inode);
2422 LINVRNT(osd_invariant(obj));
2428 * Get the 64-bit version for an inode.
2430 static int osd_object_version_get(const struct lu_env *env,
2431 struct dt_object *dt, dt_obj_version_t *ver)
2433 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2435 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2436 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2437 *ver = LDISKFS_I(inode)->i_fs_version;
2442 * Concurrency: @dt is read locked.
2444 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2445 struct lu_buf *buf, const char *name,
2446 struct lustre_capa *capa)
2448 struct osd_object *obj = osd_dt_obj(dt);
2449 struct inode *inode = obj->oo_inode;
2450 struct osd_thread_info *info = osd_oti_get(env);
2451 struct dentry *dentry = &info->oti_obj_dentry;
2453 /* version get is not real XATTR but uses xattr API */
2454 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2455 /* for version we are just using xattr API but change inode
2457 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2458 osd_object_version_get(env, dt, buf->lb_buf);
2459 return sizeof(dt_obj_version_t);
2462 LASSERT(dt_object_exists(dt));
2463 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2465 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2468 dentry->d_inode = inode;
2469 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2473 static int osd_declare_xattr_set(const struct lu_env *env,
2474 struct dt_object *dt,
2475 const struct lu_buf *buf, const char *name,
2476 int fl, struct thandle *handle)
2478 struct osd_thandle *oh;
2480 LASSERT(handle != NULL);
2482 oh = container_of0(handle, struct osd_thandle, ot_super);
2483 LASSERT(oh->ot_handle == NULL);
2485 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2486 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2487 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2488 osd_dto_credits_noquota[DTO_XATTR_SET]);
2494 * Set the 64-bit version for object
2496 static void osd_object_version_set(const struct lu_env *env,
2497 struct dt_object *dt,
2498 dt_obj_version_t *new_version)
2500 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2502 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2503 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2505 LDISKFS_I(inode)->i_fs_version = *new_version;
2506 /** Version is set after all inode operations are finished,
2507 * so we should mark it dirty here */
2508 inode->i_sb->s_op->dirty_inode(inode);
2512 * Concurrency: @dt is write locked.
2514 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2515 const struct lu_buf *buf, const char *name, int fl,
2516 struct thandle *handle, struct lustre_capa *capa)
2518 LASSERT(handle != NULL);
2520 /* version set is not real XATTR */
2521 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2522 /* for version we are just using xattr API but change inode
2524 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2525 osd_object_version_set(env, dt, buf->lb_buf);
2526 return sizeof(dt_obj_version_t);
2529 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2532 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2533 return __osd_xattr_set(env, dt, buf, name, fl);
2537 * Concurrency: @dt is read locked.
2539 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2540 struct lu_buf *buf, struct lustre_capa *capa)
2542 struct osd_object *obj = osd_dt_obj(dt);
2543 struct inode *inode = obj->oo_inode;
2544 struct osd_thread_info *info = osd_oti_get(env);
2545 struct dentry *dentry = &info->oti_obj_dentry;
2547 LASSERT(dt_object_exists(dt));
2548 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2549 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2551 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2554 dentry->d_inode = inode;
2555 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2558 static int osd_declare_xattr_del(const struct lu_env *env,
2559 struct dt_object *dt, const char *name,
2560 struct thandle *handle)
2562 struct osd_thandle *oh;
2564 LASSERT(dt_object_exists(dt));
2565 LASSERT(handle != NULL);
2567 oh = container_of0(handle, struct osd_thandle, ot_super);
2568 LASSERT(oh->ot_handle == NULL);
2570 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2571 osd_dto_credits_noquota[DTO_XATTR_SET]);
2577 * Concurrency: @dt is write locked.
2579 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2580 const char *name, struct thandle *handle,
2581 struct lustre_capa *capa)
2583 struct osd_object *obj = osd_dt_obj(dt);
2584 struct inode *inode = obj->oo_inode;
2585 struct osd_thread_info *info = osd_oti_get(env);
2586 struct dentry *dentry = &info->oti_obj_dentry;
2589 LASSERT(dt_object_exists(dt));
2590 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2591 LASSERT(osd_write_locked(env, obj));
2592 LASSERT(handle != NULL);
2594 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2597 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2599 ll_vfs_dq_init(inode);
2600 dentry->d_inode = inode;
2601 rc = inode->i_op->removexattr(dentry, name);
2605 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2606 struct dt_object *dt,
2607 struct lustre_capa *old,
2610 struct osd_thread_info *info = osd_oti_get(env);
2611 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2612 struct osd_object *obj = osd_dt_obj(dt);
2613 struct osd_device *dev = osd_obj2dev(obj);
2614 struct lustre_capa_key *key = &info->oti_capa_key;
2615 struct lustre_capa *capa = &info->oti_capa;
2616 struct obd_capa *oc;
2617 struct md_capainfo *ci;
2621 if (!dev->od_fl_capa)
2622 RETURN(ERR_PTR(-ENOENT));
2624 LASSERT(dt_object_exists(dt));
2625 LINVRNT(osd_invariant(obj));
2627 /* renewal sanity check */
2628 if (old && osd_object_auth(env, dt, old, opc))
2629 RETURN(ERR_PTR(-EACCES));
2631 ci = md_capainfo(env);
2633 RETURN(ERR_PTR(-ENOENT));
2635 switch (ci->mc_auth) {
2639 capa->lc_uid = obj->oo_inode->i_uid;
2640 capa->lc_gid = obj->oo_inode->i_gid;
2641 capa->lc_flags = LC_ID_PLAIN;
2643 case LC_ID_CONVERT: {
2646 s[0] = obj->oo_inode->i_uid;
2647 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2648 s[2] = obj->oo_inode->i_gid;
2649 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2650 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2652 RETURN(ERR_PTR(rc));
2654 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2655 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2656 capa->lc_flags = LC_ID_CONVERT;
2660 RETURN(ERR_PTR(-EINVAL));
2663 capa->lc_fid = *fid;
2665 capa->lc_flags |= dev->od_capa_alg << 24;
2666 capa->lc_timeout = dev->od_capa_timeout;
2667 capa->lc_expiry = 0;
2669 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2671 LASSERT(!capa_is_expired(oc));
2675 spin_lock(&capa_lock);
2676 *key = dev->od_capa_keys[1];
2677 spin_unlock(&capa_lock);
2679 capa->lc_keyid = key->lk_keyid;
2680 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2682 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2684 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2685 RETURN(ERR_PTR(rc));
2688 oc = capa_add(dev->od_capa_hash, capa);
2692 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
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;
2698 struct file *file = &info->oti_file;
2703 dentry->d_inode = inode;
2704 file->f_dentry = dentry;
2705 file->f_mapping = inode->i_mapping;
2706 file->f_op = inode->i_fop;
2707 mutex_lock(&inode->i_mutex);
2708 rc = file->f_op->fsync(file, dentry, 0);
2709 mutex_unlock(&inode->i_mutex);
2713 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2716 struct osd_object *obj = osd_dt_obj(dt);
2719 *data = (void *)obj->oo_inode;
2727 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2728 const struct dt_index_features *feat)
2730 struct iam_descr *descr;
2732 if (osd_object_is_root(o))
2733 return feat == &dt_directory_features;
2735 LASSERT(o->oo_dir != NULL);
2737 descr = o->oo_dir->od_container.ic_descr;
2738 if (feat == &dt_directory_features) {
2739 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2745 feat->dif_keysize_min <= descr->id_key_size &&
2746 descr->id_key_size <= feat->dif_keysize_max &&
2747 feat->dif_recsize_min <= descr->id_rec_size &&
2748 descr->id_rec_size <= feat->dif_recsize_max &&
2749 !(feat->dif_flags & (DT_IND_VARKEY |
2750 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2751 ergo(feat->dif_flags & DT_IND_UPDATE,
2752 1 /* XXX check that object (and file system) is
2757 static int osd_iam_container_init(const struct lu_env *env,
2758 struct osd_object *obj,
2759 struct osd_directory *dir)
2761 struct iam_container *bag = &dir->od_container;
2764 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2768 result = iam_container_setup(bag);
2770 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2772 iam_container_fini(bag);
2779 * Concurrency: no external locking is necessary.
2781 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2782 const struct dt_index_features *feat)
2786 struct osd_object *obj = osd_dt_obj(dt);
2788 LINVRNT(osd_invariant(obj));
2789 LASSERT(dt_object_exists(dt));
2791 if (osd_object_is_root(obj)) {
2792 dt->do_index_ops = &osd_index_ea_ops;
2794 } else if (feat == &dt_directory_features) {
2795 dt->do_index_ops = &osd_index_ea_ops;
2796 if (S_ISDIR(obj->oo_inode->i_mode))
2801 } else if (unlikely(feat == &dt_otable_features)) {
2802 dt->do_index_ops = &osd_otable_ops;
2804 } else if (feat == &dt_acct_features) {
2805 dt->do_index_ops = &osd_acct_index_ops;
2808 } else if (!osd_has_index(obj)) {
2809 struct osd_directory *dir;
2814 spin_lock(&obj->oo_guard);
2815 if (obj->oo_dir == NULL)
2819 * Concurrent thread allocated container data.
2822 spin_unlock(&obj->oo_guard);
2824 * Now, that we have container data, serialize its
2827 down_write(&obj->oo_ext_idx_sem);
2829 * recheck under lock.
2831 if (!osd_has_index(obj))
2832 result = osd_iam_container_init(env, obj, dir);
2835 up_write(&obj->oo_ext_idx_sem);
2843 if (result == 0 && skip_iam == 0) {
2844 if (!osd_iam_index_probe(env, obj, feat))
2847 LINVRNT(osd_invariant(obj));
2849 if (is_quota_glb_feat(feat))
2850 result = osd_quota_migration(env, dt, feat);
2855 static int osd_otable_it_attr_get(const struct lu_env *env,
2856 struct dt_object *dt,
2857 struct lu_attr *attr,
2858 struct lustre_capa *capa)
2864 static const struct dt_object_operations osd_obj_ops = {
2865 .do_read_lock = osd_object_read_lock,
2866 .do_write_lock = osd_object_write_lock,
2867 .do_read_unlock = osd_object_read_unlock,
2868 .do_write_unlock = osd_object_write_unlock,
2869 .do_write_locked = osd_object_write_locked,
2870 .do_attr_get = osd_attr_get,
2871 .do_declare_attr_set = osd_declare_attr_set,
2872 .do_attr_set = osd_attr_set,
2873 .do_ah_init = osd_ah_init,
2874 .do_declare_create = osd_declare_object_create,
2875 .do_create = osd_object_create,
2876 .do_declare_destroy = osd_declare_object_destroy,
2877 .do_destroy = osd_object_destroy,
2878 .do_index_try = osd_index_try,
2879 .do_declare_ref_add = osd_declare_object_ref_add,
2880 .do_ref_add = osd_object_ref_add,
2881 .do_declare_ref_del = osd_declare_object_ref_del,
2882 .do_ref_del = osd_object_ref_del,
2883 .do_xattr_get = osd_xattr_get,
2884 .do_declare_xattr_set = osd_declare_xattr_set,
2885 .do_xattr_set = osd_xattr_set,
2886 .do_declare_xattr_del = osd_declare_xattr_del,
2887 .do_xattr_del = osd_xattr_del,
2888 .do_xattr_list = osd_xattr_list,
2889 .do_capa_get = osd_capa_get,
2890 .do_object_sync = osd_object_sync,
2891 .do_data_get = osd_data_get,
2895 * dt_object_operations for interoperability mode
2896 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2898 static const struct dt_object_operations osd_obj_ea_ops = {
2899 .do_read_lock = osd_object_read_lock,
2900 .do_write_lock = osd_object_write_lock,
2901 .do_read_unlock = osd_object_read_unlock,
2902 .do_write_unlock = osd_object_write_unlock,
2903 .do_write_locked = osd_object_write_locked,
2904 .do_attr_get = osd_attr_get,
2905 .do_declare_attr_set = osd_declare_attr_set,
2906 .do_attr_set = osd_attr_set,
2907 .do_ah_init = osd_ah_init,
2908 .do_declare_create = osd_declare_object_create,
2909 .do_create = osd_object_ea_create,
2910 .do_declare_destroy = osd_declare_object_destroy,
2911 .do_destroy = osd_object_destroy,
2912 .do_index_try = osd_index_try,
2913 .do_declare_ref_add = osd_declare_object_ref_add,
2914 .do_ref_add = osd_object_ref_add,
2915 .do_declare_ref_del = osd_declare_object_ref_del,
2916 .do_ref_del = osd_object_ref_del,
2917 .do_xattr_get = osd_xattr_get,
2918 .do_declare_xattr_set = osd_declare_xattr_set,
2919 .do_xattr_set = osd_xattr_set,
2920 .do_declare_xattr_del = osd_declare_xattr_del,
2921 .do_xattr_del = osd_xattr_del,
2922 .do_xattr_list = osd_xattr_list,
2923 .do_capa_get = osd_capa_get,
2924 .do_object_sync = osd_object_sync,
2925 .do_data_get = osd_data_get,
2928 static const struct dt_object_operations osd_obj_otable_it_ops = {
2929 .do_attr_get = osd_otable_it_attr_get,
2930 .do_index_try = osd_index_try,
2933 static int osd_index_declare_iam_delete(const struct lu_env *env,
2934 struct dt_object *dt,
2935 const struct dt_key *key,
2936 struct thandle *handle)
2938 struct osd_thandle *oh;
2940 oh = container_of0(handle, struct osd_thandle, ot_super);
2941 LASSERT(oh->ot_handle == NULL);
2943 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2944 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
2950 * delete a (key, value) pair from index \a dt specified by \a key
2952 * \param dt osd index object
2953 * \param key key for index
2954 * \param rec record reference
2955 * \param handle transaction handler
2958 * \retval -ve failure
2961 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2962 const struct dt_key *key,
2963 struct thandle *handle,
2964 struct lustre_capa *capa)
2966 struct osd_thread_info *oti = osd_oti_get(env);
2967 struct osd_object *obj = osd_dt_obj(dt);
2968 struct osd_thandle *oh;
2969 struct iam_path_descr *ipd;
2970 struct iam_container *bag = &obj->oo_dir->od_container;
2975 LINVRNT(osd_invariant(obj));
2976 LASSERT(dt_object_exists(dt));
2977 LASSERT(bag->ic_object == obj->oo_inode);
2978 LASSERT(handle != NULL);
2980 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2983 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
2985 ipd = osd_idx_ipd_get(env, bag);
2986 if (unlikely(ipd == NULL))
2989 oh = container_of0(handle, struct osd_thandle, ot_super);
2990 LASSERT(oh->ot_handle != NULL);
2991 LASSERT(oh->ot_handle->h_transaction != NULL);
2993 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2994 /* swab quota uid/gid provided by caller */
2995 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2996 key = (const struct dt_key *)&oti->oti_quota_id;
2999 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3000 osd_ipd_put(env, bag, ipd);
3001 LINVRNT(osd_invariant(obj));
3005 static int osd_index_declare_ea_delete(const struct lu_env *env,
3006 struct dt_object *dt,
3007 const struct dt_key *key,
3008 struct thandle *handle)
3010 struct osd_thandle *oh;
3011 struct inode *inode;
3015 LASSERT(dt_object_exists(dt));
3016 LASSERT(handle != NULL);
3018 oh = container_of0(handle, struct osd_thandle, ot_super);
3019 LASSERT(oh->ot_handle == NULL);
3021 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3022 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3024 inode = osd_dt_obj(dt)->oo_inode;
3027 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3028 true, true, NULL, false);
3032 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3035 struct osd_fid_pack *rec;
3038 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3039 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3040 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3046 * Index delete function for interoperability mode (b11826).
3047 * It will remove the directory entry added by osd_index_ea_insert().
3048 * This entry is needed to maintain name->fid mapping.
3050 * \param key, key i.e. file entry to be deleted
3052 * \retval 0, on success
3053 * \retval -ve, on error
3055 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3056 const struct dt_key *key,
3057 struct thandle *handle,
3058 struct lustre_capa *capa)
3060 struct osd_object *obj = osd_dt_obj(dt);
3061 struct inode *dir = obj->oo_inode;
3062 struct dentry *dentry;
3063 struct osd_thandle *oh;
3064 struct ldiskfs_dir_entry_2 *de;
3065 struct buffer_head *bh;
3066 struct htree_lock *hlock = NULL;
3071 LINVRNT(osd_invariant(obj));
3072 LASSERT(dt_object_exists(dt));
3073 LASSERT(handle != NULL);
3075 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3077 oh = container_of(handle, struct osd_thandle, ot_super);
3078 LASSERT(oh->ot_handle != NULL);
3079 LASSERT(oh->ot_handle->h_transaction != NULL);
3081 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3084 ll_vfs_dq_init(dir);
3085 dentry = osd_child_dentry_get(env, obj,
3086 (char *)key, strlen((char *)key));
3088 if (obj->oo_hl_head != NULL) {
3089 hlock = osd_oti_get(env)->oti_hlock;
3090 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3091 dir, LDISKFS_HLOCK_DEL);
3093 down_write(&obj->oo_ext_idx_sem);
3096 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3098 rc = ldiskfs_delete_entry(oh->ot_handle,
3105 ldiskfs_htree_unlock(hlock);
3107 up_write(&obj->oo_ext_idx_sem);
3109 LASSERT(osd_invariant(obj));
3114 * Lookup index for \a key and copy record to \a rec.
3116 * \param dt osd index object
3117 * \param key key for index
3118 * \param rec record reference
3120 * \retval +ve success : exact mach
3121 * \retval 0 return record with key not greater than \a key
3122 * \retval -ve failure
3124 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3125 struct dt_rec *rec, const struct dt_key *key,
3126 struct lustre_capa *capa)
3128 struct osd_object *obj = osd_dt_obj(dt);
3129 struct iam_path_descr *ipd;
3130 struct iam_container *bag = &obj->oo_dir->od_container;
3131 struct osd_thread_info *oti = osd_oti_get(env);
3132 struct iam_iterator *it = &oti->oti_idx_it;
3133 struct iam_rec *iam_rec;
3138 LASSERT(osd_invariant(obj));
3139 LASSERT(dt_object_exists(dt));
3140 LASSERT(bag->ic_object == obj->oo_inode);
3142 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3145 ipd = osd_idx_ipd_get(env, bag);
3149 /* got ipd now we can start iterator. */
3150 iam_it_init(it, bag, 0, ipd);
3152 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3153 /* swab quota uid/gid provided by caller */
3154 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3155 key = (const struct dt_key *)&oti->oti_quota_id;
3158 rc = iam_it_get(it, (struct iam_key *)key);
3160 if (S_ISDIR(obj->oo_inode->i_mode))
3161 iam_rec = (struct iam_rec *)oti->oti_ldp;
3163 iam_rec = (struct iam_rec *) rec;
3165 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3167 if (S_ISDIR(obj->oo_inode->i_mode))
3168 osd_fid_unpack((struct lu_fid *) rec,
3169 (struct osd_fid_pack *)iam_rec);
3170 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3171 osd_quota_unpack(obj, rec);
3176 osd_ipd_put(env, bag, ipd);
3178 LINVRNT(osd_invariant(obj));
3183 static int osd_index_declare_iam_insert(const struct lu_env *env,
3184 struct dt_object *dt,
3185 const struct dt_rec *rec,
3186 const struct dt_key *key,
3187 struct thandle *handle)
3189 struct osd_thandle *oh;
3191 LASSERT(dt_object_exists(dt));
3192 LASSERT(handle != NULL);
3194 oh = container_of0(handle, struct osd_thandle, ot_super);
3195 LASSERT(oh->ot_handle == NULL);
3197 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3198 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3204 * Inserts (key, value) pair in \a dt index object.
3206 * \param dt osd index object
3207 * \param key key for index
3208 * \param rec record reference
3209 * \param th transaction handler
3212 * \retval -ve failure
3214 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3215 const struct dt_rec *rec,
3216 const struct dt_key *key, struct thandle *th,
3217 struct lustre_capa *capa, int ignore_quota)
3219 struct osd_object *obj = osd_dt_obj(dt);
3220 struct iam_path_descr *ipd;
3221 struct osd_thandle *oh;
3222 struct iam_container *bag = &obj->oo_dir->od_container;
3223 struct osd_thread_info *oti = osd_oti_get(env);
3224 struct iam_rec *iam_rec;
3229 LINVRNT(osd_invariant(obj));
3230 LASSERT(dt_object_exists(dt));
3231 LASSERT(bag->ic_object == obj->oo_inode);
3232 LASSERT(th != NULL);
3234 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3237 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3239 ipd = osd_idx_ipd_get(env, bag);
3240 if (unlikely(ipd == NULL))
3243 oh = container_of0(th, struct osd_thandle, ot_super);
3244 LASSERT(oh->ot_handle != NULL);
3245 LASSERT(oh->ot_handle->h_transaction != NULL);
3246 if (S_ISDIR(obj->oo_inode->i_mode)) {
3247 iam_rec = (struct iam_rec *)oti->oti_ldp;
3248 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3249 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3250 /* pack quota uid/gid */
3251 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3252 key = (const struct dt_key *)&oti->oti_quota_id;
3253 /* pack quota record */
3254 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3255 iam_rec = (struct iam_rec *)rec;
3257 iam_rec = (struct iam_rec *)rec;
3260 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3262 osd_ipd_put(env, bag, ipd);
3263 LINVRNT(osd_invariant(obj));
3268 * Calls ldiskfs_add_entry() to add directory entry
3269 * into the directory. This is required for
3270 * interoperability mode (b11826)
3272 * \retval 0, on success
3273 * \retval -ve, on error
3275 static int __osd_ea_add_rec(struct osd_thread_info *info,
3276 struct osd_object *pobj, struct inode *cinode,
3277 const char *name, const struct dt_rec *fid,
3278 struct htree_lock *hlock, struct thandle *th)
3280 struct ldiskfs_dentry_param *ldp;
3281 struct dentry *child;
3282 struct osd_thandle *oth;
3285 oth = container_of(th, struct osd_thandle, ot_super);
3286 LASSERT(oth->ot_handle != NULL);
3287 LASSERT(oth->ot_handle->h_transaction != NULL);
3289 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3291 /* XXX: remove fid_is_igif() check here.
3292 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3293 * it is IGIF now but needs FID in dir entry as well for readdir
3295 * LU-838 should fix that and remove fid_is_igif() check */
3296 if (fid_is_igif((struct lu_fid *)fid) ||
3297 fid_is_norm((struct lu_fid *)fid)) {
3298 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3299 osd_get_ldiskfs_dirent_param(ldp, fid);
3300 child->d_fsdata = (void *)ldp;
3302 child->d_fsdata = NULL;
3304 LASSERT(pobj->oo_inode);
3305 ll_vfs_dq_init(pobj->oo_inode);
3306 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3312 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3313 * into the directory.Also sets flags into osd object to
3314 * indicate dot and dotdot are created. This is required for
3315 * interoperability mode (b11826)
3317 * \param dir directory for dot and dotdot fixup.
3318 * \param obj child object for linking
3320 * \retval 0, on success
3321 * \retval -ve, on error
3323 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3324 struct osd_object *dir,
3325 struct inode *parent_dir, const char *name,
3326 const struct dt_rec *dot_fid,
3327 const struct dt_rec *dot_dot_fid,
3330 struct inode *inode = dir->oo_inode;
3331 struct ldiskfs_dentry_param *dot_ldp;
3332 struct ldiskfs_dentry_param *dot_dot_ldp;
3333 struct osd_thandle *oth;
3336 oth = container_of(th, struct osd_thandle, ot_super);
3337 LASSERT(oth->ot_handle->h_transaction != NULL);
3338 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3340 if (strcmp(name, dot) == 0) {
3341 if (dir->oo_compat_dot_created) {
3344 LASSERT(inode == parent_dir);
3345 dir->oo_compat_dot_created = 1;
3348 } else if(strcmp(name, dotdot) == 0) {
3349 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3350 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3352 if (!dir->oo_compat_dot_created)
3354 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3355 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3356 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3361 /* in case of rename, dotdot is already created */
3362 if (dir->oo_compat_dotdot_created) {
3363 return __osd_ea_add_rec(info, dir, parent_dir, name,
3364 dot_dot_fid, NULL, th);
3367 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3368 inode, dot_ldp, dot_dot_ldp);
3370 dir->oo_compat_dotdot_created = 1;
3378 * It will call the appropriate osd_add* function and return the
3379 * value, return by respective functions.
3381 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3382 struct inode *cinode, const char *name,
3383 const struct dt_rec *fid, struct thandle *th)
3385 struct osd_thread_info *info = osd_oti_get(env);
3386 struct htree_lock *hlock;
3389 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3391 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3393 if (hlock != NULL) {
3394 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3397 down_write(&pobj->oo_ext_idx_sem);
3399 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3400 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3403 if (hlock != NULL) {
3404 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3405 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3407 down_write(&pobj->oo_ext_idx_sem);
3410 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3414 ldiskfs_htree_unlock(hlock);
3416 up_write(&pobj->oo_ext_idx_sem);
3422 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3423 struct osd_idmap_cache *oic)
3425 struct osd_scrub *scrub = &dev->od_scrub;
3426 struct lu_fid *fid = &oic->oic_fid;
3427 struct osd_inode_id *id = &oti->oti_id;
3432 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3436 rc = osd_oi_lookup(oti, dev, fid, id);
3437 if (rc != 0 && rc != -ENOENT)
3440 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3443 if (thread_is_running(&scrub->os_thread)) {
3444 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3445 /* There is race condition between osd_oi_lookup and OI scrub.
3446 * The OI scrub finished just after osd_oi_lookup() failure.
3447 * Under such case, it is unnecessary to trigger OI scrub again,
3448 * but try to call osd_oi_lookup() again. */
3449 if (unlikely(rc == -EAGAIN))
3455 if (!dev->od_noscrub && ++once == 1) {
3456 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3458 rc = osd_scrub_start(dev);
3459 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3461 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3471 * Calls ->lookup() to find dentry. From dentry get inode and
3472 * read inode's ea to get fid. This is required for interoperability
3475 * \retval 0, on success
3476 * \retval -ve, on error
3478 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3479 struct dt_rec *rec, const struct dt_key *key)
3481 struct inode *dir = obj->oo_inode;
3482 struct dentry *dentry;
3483 struct ldiskfs_dir_entry_2 *de;
3484 struct buffer_head *bh;
3485 struct lu_fid *fid = (struct lu_fid *) rec;
3486 struct htree_lock *hlock = NULL;
3490 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3492 dentry = osd_child_dentry_get(env, obj,
3493 (char *)key, strlen((char *)key));
3495 if (obj->oo_hl_head != NULL) {
3496 hlock = osd_oti_get(env)->oti_hlock;
3497 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3498 dir, LDISKFS_HLOCK_LOOKUP);
3500 down_read(&obj->oo_ext_idx_sem);
3503 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3505 struct osd_thread_info *oti = osd_oti_get(env);
3506 struct osd_idmap_cache *oic = &oti->oti_cache;
3507 struct osd_device *dev = osd_obj2dev(obj);
3508 struct osd_scrub *scrub = &dev->od_scrub;
3509 struct scrub_file *sf = &scrub->os_file;
3511 ino = le32_to_cpu(de->inode);
3512 rc = osd_get_fid_from_dentry(de, rec);
3514 /* done with de, release bh */
3517 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3519 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3521 fid_zero(&oic->oic_fid);
3525 oic->oic_fid = *fid;
3526 if ((scrub->os_pos_current <= ino) &&
3527 (sf->sf_flags & SF_INCONSISTENT ||
3528 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3530 osd_consistency_check(oti, dev, oic);
3539 ldiskfs_htree_unlock(hlock);
3541 up_read(&obj->oo_ext_idx_sem);
3546 * Find the osd object for given fid.
3548 * \param fid need to find the osd object having this fid
3550 * \retval osd_object on success
3551 * \retval -ve on error
3553 struct osd_object *osd_object_find(const struct lu_env *env,
3554 struct dt_object *dt,
3555 const struct lu_fid *fid)
3557 struct lu_device *ludev = dt->do_lu.lo_dev;
3558 struct osd_object *child = NULL;
3559 struct lu_object *luch;
3560 struct lu_object *lo;
3563 * at this point topdev might not exist yet
3564 * (i.e. MGS is preparing profiles). so we can
3565 * not rely on topdev and instead lookup with
3566 * our device passed as topdev. this can't work
3567 * if the object isn't cached yet (as osd doesn't
3568 * allocate lu_header). IOW, the object must be
3569 * in the cache, otherwise lu_object_alloc() crashes
3572 luch = lu_object_find_at(env, ludev, fid, NULL);
3573 if (!IS_ERR(luch)) {
3574 if (lu_object_exists(luch)) {
3575 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3577 child = osd_obj(lo);
3579 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3580 "lu_object can't be located"
3581 DFID"\n", PFID(fid));
3583 if (child == NULL) {
3584 lu_object_put(env, luch);
3585 CERROR("Unable to get osd_object\n");
3586 child = ERR_PTR(-ENOENT);
3589 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3590 "lu_object does not exists "DFID"\n",
3592 lu_object_put(env, luch);
3593 child = ERR_PTR(-ENOENT);
3596 child = (void *)luch;
3602 * Put the osd object once done with it.
3604 * \param obj osd object that needs to be put
3606 static inline void osd_object_put(const struct lu_env *env,
3607 struct osd_object *obj)
3609 lu_object_put(env, &obj->oo_dt.do_lu);
3612 static int osd_index_declare_ea_insert(const struct lu_env *env,
3613 struct dt_object *dt,
3614 const struct dt_rec *rec,
3615 const struct dt_key *key,
3616 struct thandle *handle)
3618 struct osd_thandle *oh;
3619 struct inode *inode;
3620 struct lu_fid *fid = (struct lu_fid *)rec;
3624 LASSERT(dt_object_exists(dt));
3625 LASSERT(handle != NULL);
3627 oh = container_of0(handle, struct osd_thandle, ot_super);
3628 LASSERT(oh->ot_handle == NULL);
3630 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3631 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3633 inode = osd_dt_obj(dt)->oo_inode;
3636 /* We ignore block quota on meta pool (MDTs), so needn't
3637 * calculate how many blocks will be consumed by this index
3639 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3640 true, true, NULL, false);
3644 /* It does fld look up inside declare, and the result will be
3645 * added to fld cache, so the following fld lookup inside insert
3646 * does not need send RPC anymore, so avoid send rpc with holding
3648 LASSERTF(fid_is_sane(fid), "fid is insane"DFID"\n", PFID(fid));
3649 osd_fld_lookup(env, osd_dt_dev(handle->th_dev), fid,
3650 &osd_oti_get(env)->oti_seq_range);
3656 * Index add function for interoperability mode (b11826).
3657 * It will add the directory entry.This entry is needed to
3658 * maintain name->fid mapping.
3660 * \param key it is key i.e. file entry to be inserted
3661 * \param rec it is value of given key i.e. fid
3663 * \retval 0, on success
3664 * \retval -ve, on error
3666 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3667 const struct dt_rec *rec,
3668 const struct dt_key *key, struct thandle *th,
3669 struct lustre_capa *capa, int ignore_quota)
3671 struct osd_object *obj = osd_dt_obj(dt);
3672 struct lu_fid *fid = (struct lu_fid *) rec;
3673 const char *name = (const char *)key;
3674 struct osd_object *child;
3679 LASSERT(osd_invariant(obj));
3680 LASSERT(dt_object_exists(dt));
3681 LASSERT(th != NULL);
3683 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3685 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3688 child = osd_object_find(env, dt, fid);
3689 if (!IS_ERR(child)) {
3690 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3691 osd_object_put(env, child);
3693 rc = PTR_ERR(child);
3696 LASSERT(osd_invariant(obj));
3701 * Initialize osd Iterator for given osd index object.
3703 * \param dt osd index object
3706 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3707 struct dt_object *dt,
3709 struct lustre_capa *capa)
3711 struct osd_it_iam *it;
3712 struct osd_thread_info *oti = osd_oti_get(env);
3713 struct osd_object *obj = osd_dt_obj(dt);
3714 struct lu_object *lo = &dt->do_lu;
3715 struct iam_path_descr *ipd;
3716 struct iam_container *bag = &obj->oo_dir->od_container;
3718 LASSERT(lu_object_exists(lo));
3720 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3721 return ERR_PTR(-EACCES);
3724 ipd = osd_it_ipd_get(env, bag);
3725 if (likely(ipd != NULL)) {
3729 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3730 return (struct dt_it *)it;
3732 return ERR_PTR(-ENOMEM);
3736 * free given Iterator.
3739 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3741 struct osd_it_iam *it = (struct osd_it_iam *)di;
3742 struct osd_object *obj = it->oi_obj;
3744 iam_it_fini(&it->oi_it);
3745 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3746 lu_object_put(env, &obj->oo_dt.do_lu);
3750 * Move Iterator to record specified by \a key
3752 * \param di osd iterator
3753 * \param key key for index
3755 * \retval +ve di points to record with least key not larger than key
3756 * \retval 0 di points to exact matched key
3757 * \retval -ve failure
3760 static int osd_it_iam_get(const struct lu_env *env,
3761 struct dt_it *di, const struct dt_key *key)
3763 struct osd_thread_info *oti = osd_oti_get(env);
3764 struct osd_it_iam *it = (struct osd_it_iam *)di;
3766 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3767 /* swab quota uid/gid */
3768 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3769 key = (struct dt_key *)&oti->oti_quota_id;
3772 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3778 * \param di osd iterator
3781 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3783 struct osd_it_iam *it = (struct osd_it_iam *)di;
3785 iam_it_put(&it->oi_it);
3789 * Move iterator by one record
3791 * \param di osd iterator
3793 * \retval +1 end of container reached
3795 * \retval -ve failure
3798 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3800 struct osd_it_iam *it = (struct osd_it_iam *)di;
3802 return iam_it_next(&it->oi_it);
3806 * Return pointer to the key under iterator.
3809 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3810 const struct dt_it *di)
3812 struct osd_thread_info *oti = osd_oti_get(env);
3813 struct osd_it_iam *it = (struct osd_it_iam *)di;
3814 struct osd_object *obj = it->oi_obj;
3817 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3819 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3820 /* swab quota uid/gid */
3821 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3822 key = (struct dt_key *)&oti->oti_quota_id;
3829 * Return size of key under iterator (in bytes)
3832 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3834 struct osd_it_iam *it = (struct osd_it_iam *)di;
3836 return iam_it_key_size(&it->oi_it);
3839 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3840 int len, __u16 type)
3842 struct luda_type *lt;
3843 const unsigned align = sizeof(struct luda_type) - 1;
3845 /* check if file type is required */
3846 if (attr & LUDA_TYPE) {
3847 len = (len + align) & ~align;
3849 lt = (void *) ent->lde_name + len;
3850 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3851 ent->lde_attrs |= LUDA_TYPE;
3854 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3858 * build lu direct from backend fs dirent.
3861 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3862 struct lu_fid *fid, __u64 offset,
3863 char *name, __u16 namelen,
3864 __u16 type, __u32 attr)
3866 fid_cpu_to_le(&ent->lde_fid, fid);
3867 ent->lde_attrs = LUDA_FID;
3869 ent->lde_hash = cpu_to_le64(offset);
3870 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3872 strncpy(ent->lde_name, name, namelen);
3873 ent->lde_namelen = cpu_to_le16(namelen);
3875 /* append lustre attributes */
3876 osd_it_append_attrs(ent, attr, namelen, type);
3880 * Return pointer to the record under iterator.
3882 static int osd_it_iam_rec(const struct lu_env *env,
3883 const struct dt_it *di,
3884 struct dt_rec *dtrec, __u32 attr)
3886 struct osd_it_iam *it = (struct osd_it_iam *)di;
3887 struct osd_thread_info *info = osd_oti_get(env);
3890 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3891 const struct osd_fid_pack *rec;
3892 struct lu_fid *fid = &info->oti_fid;
3893 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3899 name = (char *)iam_it_key_get(&it->oi_it);
3901 RETURN(PTR_ERR(name));
3903 namelen = iam_it_key_size(&it->oi_it);
3905 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3907 RETURN(PTR_ERR(rec));
3909 rc = osd_fid_unpack(fid, rec);
3913 hash = iam_it_store(&it->oi_it);
3915 /* IAM does not store object type in IAM index (dir) */
3916 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3918 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3919 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3920 (struct iam_rec *)dtrec);
3921 osd_quota_unpack(it->oi_obj, dtrec);
3923 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3924 (struct iam_rec *)dtrec);
3931 * Returns cookie for current Iterator position.
3933 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3935 struct osd_it_iam *it = (struct osd_it_iam *)di;
3937 return iam_it_store(&it->oi_it);
3941 * Restore iterator from cookie.
3943 * \param di osd iterator
3944 * \param hash Iterator location cookie
3946 * \retval +ve di points to record with least key not larger than key.
3947 * \retval 0 di points to exact matched key
3948 * \retval -ve failure
3951 static int osd_it_iam_load(const struct lu_env *env,
3952 const struct dt_it *di, __u64 hash)
3954 struct osd_it_iam *it = (struct osd_it_iam *)di;
3956 return iam_it_load(&it->oi_it, hash);
3959 static const struct dt_index_operations osd_index_iam_ops = {
3960 .dio_lookup = osd_index_iam_lookup,
3961 .dio_declare_insert = osd_index_declare_iam_insert,
3962 .dio_insert = osd_index_iam_insert,
3963 .dio_declare_delete = osd_index_declare_iam_delete,
3964 .dio_delete = osd_index_iam_delete,
3966 .init = osd_it_iam_init,
3967 .fini = osd_it_iam_fini,
3968 .get = osd_it_iam_get,
3969 .put = osd_it_iam_put,
3970 .next = osd_it_iam_next,
3971 .key = osd_it_iam_key,
3972 .key_size = osd_it_iam_key_size,
3973 .rec = osd_it_iam_rec,
3974 .store = osd_it_iam_store,
3975 .load = osd_it_iam_load
3981 * Creates or initializes iterator context.
3983 * \retval struct osd_it_ea, iterator structure on success
3986 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3987 struct dt_object *dt,
3989 struct lustre_capa *capa)
3991 struct osd_object *obj = osd_dt_obj(dt);
3992 struct osd_thread_info *info = osd_oti_get(env);
3993 struct osd_it_ea *it = &info->oti_it_ea;
3994 struct lu_object *lo = &dt->do_lu;
3995 struct dentry *obj_dentry = &info->oti_it_dentry;
3997 LASSERT(lu_object_exists(lo));
3999 obj_dentry->d_inode = obj->oo_inode;
4000 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4001 obj_dentry->d_name.hash = 0;
4003 it->oie_rd_dirent = 0;
4004 it->oie_it_dirent = 0;
4005 it->oie_dirent = NULL;
4006 it->oie_buf = info->oti_it_ea_buf;
4008 it->oie_file.f_pos = 0;
4009 it->oie_file.f_dentry = obj_dentry;
4010 if (attr & LUDA_64BITHASH)
4011 it->oie_file.f_mode |= FMODE_64BITHASH;
4013 it->oie_file.f_mode |= FMODE_32BITHASH;
4014 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
4015 it->oie_file.f_op = obj->oo_inode->i_fop;
4016 it->oie_file.private_data = NULL;
4018 RETURN((struct dt_it *) it);
4022 * Destroy or finishes iterator context.
4024 * \param di iterator structure to be destroyed
4026 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4028 struct osd_it_ea *it = (struct osd_it_ea *)di;
4029 struct osd_object *obj = it->oie_obj;
4030 struct inode *inode = obj->oo_inode;
4033 it->oie_file.f_op->release(inode, &it->oie_file);
4034 lu_object_put(env, &obj->oo_dt.do_lu);
4039 * It position the iterator at given key, so that next lookup continues from
4040 * that key Or it is similar to dio_it->load() but based on a key,
4041 * rather than file position.
4043 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4046 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4048 static int osd_it_ea_get(const struct lu_env *env,
4049 struct dt_it *di, const struct dt_key *key)
4051 struct osd_it_ea *it = (struct osd_it_ea *)di;
4054 LASSERT(((const char *)key)[0] == '\0');
4055 it->oie_file.f_pos = 0;
4056 it->oie_rd_dirent = 0;
4057 it->oie_it_dirent = 0;
4058 it->oie_dirent = NULL;
4066 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4071 * It is called internally by ->readdir(). It fills the
4072 * iterator's in-memory data structure with required
4073 * information i.e. name, namelen, rec_size etc.
4075 * \param buf in which information to be filled in.
4076 * \param name name of the file in given dir
4078 * \retval 0 on success
4079 * \retval 1 on buffer full
4081 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4082 loff_t offset, __u64 ino,
4085 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4086 struct osd_it_ea_dirent *ent = it->oie_dirent;
4087 struct lu_fid *fid = &ent->oied_fid;
4088 struct osd_fid_pack *rec;
4091 /* this should never happen */
4092 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4093 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4097 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4101 if (d_type & LDISKFS_DIRENT_LUFID) {
4102 rec = (struct osd_fid_pack*) (name + namelen + 1);
4104 if (osd_fid_unpack(fid, rec) != 0)
4107 d_type &= ~LDISKFS_DIRENT_LUFID;
4112 ent->oied_ino = ino;
4113 ent->oied_off = offset;
4114 ent->oied_namelen = namelen;
4115 ent->oied_type = d_type;
4117 memcpy(ent->oied_name, name, namelen);
4119 it->oie_rd_dirent++;
4120 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4125 * Calls ->readdir() to load a directory entry at a time
4126 * and stored it in iterator's in-memory data structure.
4128 * \param di iterator's in memory structure
4130 * \retval 0 on success
4131 * \retval -ve on error
4133 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4134 const struct dt_it *di)
4136 struct osd_it_ea *it = (struct osd_it_ea *)di;
4137 struct osd_object *obj = it->oie_obj;
4138 struct inode *inode = obj->oo_inode;
4139 struct htree_lock *hlock = NULL;
4143 it->oie_dirent = it->oie_buf;
4144 it->oie_rd_dirent = 0;
4146 if (obj->oo_hl_head != NULL) {
4147 hlock = osd_oti_get(env)->oti_hlock;
4148 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4149 inode, LDISKFS_HLOCK_READDIR);
4151 down_read(&obj->oo_ext_idx_sem);
4154 result = inode->i_fop->readdir(&it->oie_file, it,
4155 (filldir_t) osd_ldiskfs_filldir);
4158 ldiskfs_htree_unlock(hlock);
4160 up_read(&obj->oo_ext_idx_sem);
4162 if (it->oie_rd_dirent == 0) {
4165 it->oie_dirent = it->oie_buf;
4166 it->oie_it_dirent = 1;
4173 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4174 * to load a directory entry at a time and stored it in
4175 * iterator's in-memory data structure.
4177 * \param di iterator's in memory structure
4179 * \retval +ve iterator reached to end
4180 * \retval 0 iterator not reached to end
4181 * \retval -ve on error
4183 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4185 struct osd_it_ea *it = (struct osd_it_ea *)di;
4190 if (it->oie_it_dirent < it->oie_rd_dirent) {
4192 (void *) it->oie_dirent +
4193 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4194 it->oie_dirent->oied_namelen);
4195 it->oie_it_dirent++;
4198 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4201 rc = osd_ldiskfs_it_fill(env, di);
4208 * Returns the key at current position from iterator's in memory structure.
4210 * \param di iterator's in memory structure
4212 * \retval key i.e. struct dt_key on success
4214 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4215 const struct dt_it *di)
4217 struct osd_it_ea *it = (struct osd_it_ea *)di;
4219 return (struct dt_key *)it->oie_dirent->oied_name;
4223 * Returns the key's size at current position from iterator's in memory structure.
4225 * \param di iterator's in memory structure
4227 * \retval key_size i.e. struct dt_key on success
4229 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4231 struct osd_it_ea *it = (struct osd_it_ea *)di;
4233 return it->oie_dirent->oied_namelen;
4238 * Returns the value (i.e. fid/igif) at current position from iterator's
4239 * in memory structure.
4241 * \param di struct osd_it_ea, iterator's in memory structure
4242 * \param attr attr requested for dirent.
4243 * \param lde lustre dirent
4245 * \retval 0 no error and \param lde has correct lustre dirent.
4246 * \retval -ve on error
4248 static inline int osd_it_ea_rec(const struct lu_env *env,
4249 const struct dt_it *di,
4250 struct dt_rec *dtrec, __u32 attr)
4252 struct osd_it_ea *it = (struct osd_it_ea *)di;
4253 struct osd_object *obj = it->oie_obj;
4254 struct osd_device *dev = osd_obj2dev(obj);
4255 struct osd_scrub *scrub = &dev->od_scrub;
4256 struct scrub_file *sf = &scrub->os_file;
4257 struct osd_thread_info *oti = osd_oti_get(env);
4258 struct osd_idmap_cache *oic = &oti->oti_cache;
4259 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4260 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4261 __u32 ino = it->oie_dirent->oied_ino;
4265 if (!fid_is_sane(fid)) {
4266 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4268 fid_zero(&oic->oic_fid);
4272 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4275 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4276 it->oie_dirent->oied_name,
4277 it->oie_dirent->oied_namelen,
4278 it->oie_dirent->oied_type, attr);
4279 oic->oic_fid = *fid;
4280 if ((scrub->os_pos_current <= ino) &&
4281 (sf->sf_flags & SF_INCONSISTENT ||
4282 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4283 osd_consistency_check(oti, dev, oic);
4289 * Returns a cookie for current position of the iterator head, so that
4290 * user can use this cookie to load/start the iterator next time.
4292 * \param di iterator's in memory structure
4294 * \retval cookie for current position, on success
4296 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4298 struct osd_it_ea *it = (struct osd_it_ea *)di;
4300 return it->oie_dirent->oied_off;
4304 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4305 * to load a directory entry at a time and stored it i inn,
4306 * in iterator's in-memory data structure.
4308 * \param di struct osd_it_ea, iterator's in memory structure
4310 * \retval +ve on success
4311 * \retval -ve on error
4313 static int osd_it_ea_load(const struct lu_env *env,
4314 const struct dt_it *di, __u64 hash)
4316 struct osd_it_ea *it = (struct osd_it_ea *)di;
4320 it->oie_file.f_pos = hash;
4322 rc = osd_ldiskfs_it_fill(env, di);
4330 * Index lookup function for interoperability mode (b11826).
4332 * \param key, key i.e. file name to be searched
4334 * \retval +ve, on success
4335 * \retval -ve, on error
4337 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4338 struct dt_rec *rec, const struct dt_key *key,
4339 struct lustre_capa *capa)
4341 struct osd_object *obj = osd_dt_obj(dt);
4346 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4347 LINVRNT(osd_invariant(obj));
4349 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4352 rc = osd_ea_lookup_rec(env, obj, rec, key);
4359 * Index and Iterator operations for interoperability
4360 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4362 static const struct dt_index_operations osd_index_ea_ops = {
4363 .dio_lookup = osd_index_ea_lookup,
4364 .dio_declare_insert = osd_index_declare_ea_insert,
4365 .dio_insert = osd_index_ea_insert,
4366 .dio_declare_delete = osd_index_declare_ea_delete,
4367 .dio_delete = osd_index_ea_delete,
4369 .init = osd_it_ea_init,
4370 .fini = osd_it_ea_fini,
4371 .get = osd_it_ea_get,
4372 .put = osd_it_ea_put,
4373 .next = osd_it_ea_next,
4374 .key = osd_it_ea_key,
4375 .key_size = osd_it_ea_key_size,
4376 .rec = osd_it_ea_rec,
4377 .store = osd_it_ea_store,
4378 .load = osd_it_ea_load
4382 static void *osd_key_init(const struct lu_context *ctx,
4383 struct lu_context_key *key)
4385 struct osd_thread_info *info;
4387 OBD_ALLOC_PTR(info);
4389 return ERR_PTR(-ENOMEM);
4391 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4392 if (info->oti_it_ea_buf == NULL)
4395 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4397 info->oti_hlock = ldiskfs_htree_lock_alloc();
4398 if (info->oti_hlock == NULL)
4404 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4407 return ERR_PTR(-ENOMEM);
4410 static void osd_key_fini(const struct lu_context *ctx,
4411 struct lu_context_key *key, void* data)
4413 struct osd_thread_info *info = data;
4415 if (info->oti_hlock != NULL)
4416 ldiskfs_htree_lock_free(info->oti_hlock);
4417 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4421 static void osd_key_exit(const struct lu_context *ctx,
4422 struct lu_context_key *key, void *data)
4424 struct osd_thread_info *info = data;
4426 LASSERT(info->oti_r_locks == 0);
4427 LASSERT(info->oti_w_locks == 0);
4428 LASSERT(info->oti_txns == 0);
4431 /* type constructor/destructor: osd_type_init, osd_type_fini */
4432 LU_TYPE_INIT_FINI(osd, &osd_key);
4434 struct lu_context_key osd_key = {
4435 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4436 .lct_init = osd_key_init,
4437 .lct_fini = osd_key_fini,
4438 .lct_exit = osd_key_exit
4442 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4443 const char *name, struct lu_device *next)
4445 struct osd_device *osd = osd_dev(d);
4447 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4448 return osd_procfs_init(osd, name);
4451 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4455 osd_scrub_cleanup(env, o);
4458 fsfilt_put_ops(o->od_fsops);
4462 /* shutdown quota slave instance associated with the device */
4463 if (o->od_quota_slave != NULL) {
4464 qsd_fini(env, o->od_quota_slave);
4465 o->od_quota_slave = NULL;
4471 static int osd_mount(const struct lu_env *env,
4472 struct osd_device *o, struct lustre_cfg *cfg)
4474 const char *name = lustre_cfg_string(cfg, 0);
4475 const char *dev = lustre_cfg_string(cfg, 1);
4477 unsigned long page, s_flags, lmd_flags = 0;
4478 struct page *__page;
4479 struct file_system_type *type;
4480 char *options = NULL;
4485 if (o->od_mnt != NULL)
4488 if (strlen(dev) >= sizeof(o->od_mntdev))
4490 strcpy(o->od_mntdev, dev);
4492 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4493 if (o->od_fsops == NULL) {
4494 CERROR("Can't find fsfilt_ldiskfs\n");
4498 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4500 GOTO(out, rc = -ENOMEM);
4502 str = lustre_cfg_string(cfg, 2);
4503 s_flags = simple_strtoul(str, NULL, 0);
4504 str = strstr(str, ":");
4506 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4507 opts = lustre_cfg_string(cfg, 3);
4508 page = (unsigned long)cfs_page_address(__page);
4509 options = (char *)page;
4512 strcat(options, "user_xattr,acl");
4514 strcat(options, opts);
4516 /* Glom up mount options */
4517 if (*options != '\0')
4518 strcat(options, ",");
4519 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4521 type = get_fs_type("ldiskfs");
4523 CERROR("%s: cannot find ldiskfs module\n", name);
4524 GOTO(out, rc = -ENODEV);
4527 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4528 cfs_module_put(type->owner);
4530 if (IS_ERR(o->od_mnt)) {
4531 rc = PTR_ERR(o->od_mnt);
4532 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4537 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4538 CERROR("%s: underlying device %s is marked as read-only. "
4539 "Setup failed\n", name, dev);
4542 GOTO(out, rc = -EROFS);
4545 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4546 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4547 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4550 GOTO(out, rc = -EINVAL);
4553 if (lmd_flags & LMD_FLG_NOSCRUB)
4558 OBD_PAGE_FREE(__page);
4560 fsfilt_put_ops(o->od_fsops);
4565 static struct lu_device *osd_device_fini(const struct lu_env *env,
4566 struct lu_device *d)
4571 rc = osd_shutdown(env, osd_dev(d));
4573 osd_obj_map_fini(osd_dev(d));
4575 shrink_dcache_sb(osd_sb(osd_dev(d)));
4576 osd_sync(env, lu2dt_dev(d));
4578 rc = osd_procfs_fini(osd_dev(d));
4580 CERROR("proc fini error %d \n", rc);
4581 RETURN (ERR_PTR(rc));
4584 if (osd_dev(d)->od_mnt) {
4585 mntput(osd_dev(d)->od_mnt);
4586 osd_dev(d)->od_mnt = NULL;
4592 static int osd_device_init0(const struct lu_env *env,
4593 struct osd_device *o,
4594 struct lustre_cfg *cfg)
4596 struct lu_device *l = osd2lu_dev(o);
4597 struct osd_thread_info *info;
4600 /* if the module was re-loaded, env can loose its keys */
4601 rc = lu_env_refill((struct lu_env *) env);
4604 info = osd_oti_get(env);
4607 l->ld_ops = &osd_lu_ops;
4608 o->od_dt_dev.dd_ops = &osd_dt_ops;
4610 spin_lock_init(&o->od_osfs_lock);
4611 mutex_init(&o->od_otable_mutex);
4612 o->od_osfs_age = cfs_time_shift_64(-1000);
4614 o->od_capa_hash = init_capa_hash();
4615 if (o->od_capa_hash == NULL)
4616 GOTO(out, rc = -ENOMEM);
4618 o->od_read_cache = 1;
4619 o->od_writethrough_cache = 1;
4620 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
4622 rc = osd_mount(env, o, cfg);
4626 /* setup scrub, including OI files initialization */
4627 rc = osd_scrub_setup(env, o);
4631 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4632 sizeof(o->od_svname) - 1);
4634 rc = osd_obj_map_init(o);
4636 GOTO(out_scrub, rc);
4638 rc = lu_site_init(&o->od_site, l);
4640 GOTO(out_compat, rc);
4641 o->od_site.ls_bottom_dev = l;
4643 rc = lu_site_init_finish(&o->od_site);
4647 rc = osd_procfs_init(o, o->od_svname);
4649 CERROR("%s: can't initialize procfs: rc = %d\n",
4654 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4656 /* initialize quota slave instance */
4657 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
4659 if (IS_ERR(o->od_quota_slave)) {
4660 rc = PTR_ERR(o->od_quota_slave);
4661 o->od_quota_slave = NULL;
4662 GOTO(out_procfs, rc);
4669 lu_site_fini(&o->od_site);
4671 osd_obj_map_fini(o);
4673 osd_scrub_cleanup(env, o);
4675 osd_oi_fini(info, o);
4676 osd_shutdown(env, o);
4680 cleanup_capa_hash(o->od_capa_hash);
4685 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4686 struct lu_device_type *t,
4687 struct lustre_cfg *cfg)
4689 struct osd_device *o;
4694 return ERR_PTR(-ENOMEM);
4696 rc = dt_device_init(&o->od_dt_dev, t);
4698 /* Because the ctx might be revived in dt_device_init,
4699 * refill the env here */
4700 lu_env_refill((struct lu_env *)env);
4701 rc = osd_device_init0(env, o, cfg);
4703 dt_device_fini(&o->od_dt_dev);
4706 if (unlikely(rc != 0))
4709 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4712 static struct lu_device *osd_device_free(const struct lu_env *env,
4713 struct lu_device *d)
4715 struct osd_device *o = osd_dev(d);
4718 cleanup_capa_hash(o->od_capa_hash);
4719 /* XXX: make osd top device in order to release reference */
4720 d->ld_site->ls_top_dev = d;
4721 lu_site_purge(env, d->ld_site, -1);
4722 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4723 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4724 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4726 lu_site_fini(&o->od_site);
4727 dt_device_fini(&o->od_dt_dev);
4732 static int osd_process_config(const struct lu_env *env,
4733 struct lu_device *d, struct lustre_cfg *cfg)
4735 struct osd_device *o = osd_dev(d);
4739 switch(cfg->lcfg_command) {
4741 err = osd_mount(env, o, cfg);
4744 lu_dev_del_linkage(d->ld_site, d);
4745 err = osd_shutdown(env, o);
4754 static int osd_recovery_complete(const struct lu_env *env,
4755 struct lu_device *d)
4757 struct osd_device *osd = osd_dev(d);
4761 if (osd->od_quota_slave == NULL)
4764 /* start qsd instance on recovery completion, this notifies the quota
4765 * slave code that we are about to process new requests now */
4766 rc = qsd_start(env, osd->od_quota_slave);
4771 * we use exports to track all osd users
4773 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4774 struct obd_device *obd, struct obd_uuid *cluuid,
4775 struct obd_connect_data *data, void *localdata)
4777 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4778 struct lustre_handle conn;
4782 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4784 rc = class_connect(&conn, obd, cluuid);
4788 *exp = class_conn2export(&conn);
4790 spin_lock(&osd->od_osfs_lock);
4792 spin_unlock(&osd->od_osfs_lock);
4798 * once last export (we don't count self-export) disappeared
4799 * osd can be released
4801 static int osd_obd_disconnect(struct obd_export *exp)
4803 struct obd_device *obd = exp->exp_obd;
4804 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4805 int rc, release = 0;
4808 /* Only disconnect the underlying layers on the final disconnect. */
4809 spin_lock(&osd->od_osfs_lock);
4811 if (osd->od_connects == 0)
4813 spin_unlock(&osd->od_osfs_lock);
4815 rc = class_disconnect(exp); /* bz 9811 */
4817 if (rc == 0 && release)
4818 class_manual_cleanup(obd);
4822 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4823 struct lu_device *dev)
4825 struct osd_device *osd = osd_dev(dev);
4829 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
4830 /* MDT/MDD still use old infrastructure to create
4832 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4838 if (osd->od_quota_slave != NULL)
4839 /* set up quota slave objects */
4840 result = qsd_prepare(env, osd->od_quota_slave);
4845 static const struct lu_object_operations osd_lu_obj_ops = {
4846 .loo_object_init = osd_object_init,
4847 .loo_object_delete = osd_object_delete,
4848 .loo_object_release = osd_object_release,
4849 .loo_object_free = osd_object_free,
4850 .loo_object_print = osd_object_print,
4851 .loo_object_invariant = osd_object_invariant
4854 const struct lu_device_operations osd_lu_ops = {
4855 .ldo_object_alloc = osd_object_alloc,
4856 .ldo_process_config = osd_process_config,
4857 .ldo_recovery_complete = osd_recovery_complete,
4858 .ldo_prepare = osd_prepare,
4861 static const struct lu_device_type_operations osd_device_type_ops = {
4862 .ldto_init = osd_type_init,
4863 .ldto_fini = osd_type_fini,
4865 .ldto_start = osd_type_start,
4866 .ldto_stop = osd_type_stop,
4868 .ldto_device_alloc = osd_device_alloc,
4869 .ldto_device_free = osd_device_free,
4871 .ldto_device_init = osd_device_init,
4872 .ldto_device_fini = osd_device_fini
4875 struct lu_device_type osd_device_type = {
4876 .ldt_tags = LU_DEVICE_DT,
4877 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4878 .ldt_ops = &osd_device_type_ops,
4879 .ldt_ctx_tags = LCT_LOCAL,
4883 * lprocfs legacy support.
4885 static struct obd_ops osd_obd_device_ops = {
4886 .o_owner = THIS_MODULE,
4887 .o_connect = osd_obd_connect,
4888 .o_disconnect = osd_obd_disconnect
4891 static int __init osd_mod_init(void)
4893 struct lprocfs_static_vars lvars;
4896 lprocfs_osd_init_vars(&lvars);
4897 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4898 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4901 static void __exit osd_mod_exit(void)
4903 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4906 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4907 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4908 MODULE_LICENSE("GPL");
4910 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);