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, Whamcloud, Inc.
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 /* dt_acct_features */
78 #ifdef HAVE_LDISKFS_PDO
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81 "ldiskfs with parallel directory operations");
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
90 static const struct lu_object_operations osd_lu_obj_ops;
91 static const struct dt_object_operations osd_obj_ops;
92 static const struct dt_object_operations osd_obj_ea_ops;
93 static const struct dt_object_operations osd_obj_otable_it_ops;
94 static const struct dt_index_operations osd_index_iam_ops;
95 static const struct dt_index_operations osd_index_ea_ops;
97 static int osd_has_index(const struct osd_object *obj)
99 return obj->oo_dt.do_index_ops != NULL;
102 static int osd_object_invariant(const struct lu_object *l)
104 return osd_invariant(osd_obj(l));
108 * Concurrency: doesn't matter
110 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
112 return osd_oti_get(env)->oti_r_locks > 0;
116 * Concurrency: doesn't matter
118 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
120 struct osd_thread_info *oti = osd_oti_get(env);
121 return oti->oti_w_locks > 0 && o->oo_owner == env;
125 * Concurrency: doesn't access mutable data
127 static int osd_root_get(const struct lu_env *env,
128 struct dt_device *dev, struct lu_fid *f)
130 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
135 * OSD object methods.
139 * Concurrency: no concurrent access is possible that early in object
142 static struct lu_object *osd_object_alloc(const struct lu_env *env,
143 const struct lu_object_header *hdr,
146 struct osd_object *mo;
152 l = &mo->oo_dt.do_lu;
153 dt_object_init(&mo->oo_dt, NULL, d);
154 if (osd_dev(d)->od_iop_mode)
155 mo->oo_dt.do_ops = &osd_obj_ea_ops;
157 mo->oo_dt.do_ops = &osd_obj_ops;
159 l->lo_ops = &osd_lu_obj_ops;
160 cfs_init_rwsem(&mo->oo_sem);
161 cfs_init_rwsem(&mo->oo_ext_idx_sem);
162 cfs_spin_lock_init(&mo->oo_guard);
169 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
170 struct lustre_mdt_attrs *lma)
174 dentry->d_inode = inode;
175 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
178 /* Check LMA compatibility */
179 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
180 CWARN("%.16s: unsupported incompat LMA feature(s) "
182 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
183 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
187 lustre_lma_swab(lma);
190 } else if (rc == 0) {
198 * retrieve object from backend ext fs.
200 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
201 struct osd_inode_id *id)
203 struct inode *inode = NULL;
205 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
207 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
208 id->oii_ino, PTR_ERR(inode));
209 } else if (id->oii_gen != OSD_OII_NOGEN &&
210 inode->i_generation != id->oii_gen) {
211 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
213 id->oii_ino, id->oii_gen, inode->i_generation);
215 inode = ERR_PTR(-ESTALE);
216 } else if (inode->i_nlink == 0) {
217 /* due to parallel readdir and unlink,
218 * we can have dead inode here. */
219 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
220 make_bad_inode(inode);
222 inode = ERR_PTR(-ESTALE);
223 } else if (is_bad_inode(inode)) {
224 CWARN("%.16s: bad inode: ino = %u\n",
225 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
227 inode = ERR_PTR(-ENOENT);
229 if (id->oii_gen == OSD_OII_NOGEN)
230 osd_id_gen(id, inode->i_ino, inode->i_generation);
232 /* Do not update file c/mtime in ldiskfs.
233 * NB: we don't have any lock to protect this because we don't
234 * have reference on osd_object now, but contention with
235 * another lookup + attr_set can't happen in the tiny window
236 * between if (...) and set S_NOCMTIME. */
237 if (!(inode->i_flags & S_NOCMTIME))
238 inode->i_flags |= S_NOCMTIME;
243 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
244 struct osd_inode_id *id, struct lu_fid *fid)
246 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
250 inode = osd_iget(info, dev, id);
254 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
256 *fid = lma->lma_self_fid;
257 } else if (rc == -ENODATA) {
258 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
266 static struct inode *
267 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
268 struct osd_inode_id *id, const struct lu_fid *fid)
270 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
274 inode = osd_iget(info, dev, id);
278 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
280 if (rc == -ENODATA) {
281 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
282 inode->i_ino, PFID(fid));
289 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
290 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
291 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
293 return ERR_PTR(EREMCHG);
298 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
299 const struct lu_fid *fid,
300 const struct lu_object_conf *conf)
302 struct osd_thread_info *info;
303 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
304 struct osd_device *dev;
305 struct osd_idmap_cache *oic;
306 struct osd_inode_id *id;
308 struct osd_scrub *scrub;
309 struct scrub_file *sf;
314 LINVRNT(osd_invariant(obj));
315 LASSERT(obj->oo_inode == NULL);
316 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
319 scrub = &dev->od_scrub;
320 sf = &scrub->os_file;
321 info = osd_oti_get(env);
323 oic = &info->oti_cache;
326 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
329 if (fid_is_norm(fid)) {
330 /* Search order: 1. per-thread cache. */
331 if (lu_fid_eq(fid, &oic->oic_fid)) {
333 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
334 /* Search order: 2. OI scrub pending list. */
335 result = osd_oii_lookup(dev, fid, id);
340 if (sf->sf_flags & SF_INCONSISTENT)
345 * Objects are created as locking anchors or place holders for objects
346 * yet to be created. No need to osd_oi_lookup() at here because FID
347 * shouldn't never be re-used, if it's really a duplicate FID from
348 * unexpected reason, we should be able to detect it later by calling
349 * do_create->osd_oi_insert()
351 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
352 GOTO(out, result = 0);
354 /* Search order: 3. OI files. */
355 result = osd_oi_lookup(info, dev, fid, id);
356 if (result == -ENOENT) {
357 if (!fid_is_norm(fid) ||
358 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
360 GOTO(out, result = 0);
370 inode = osd_iget(info, dev, id);
372 inode = osd_iget_verify(info, dev, id, fid);
374 result = PTR_ERR(inode);
375 if (result == -ENOENT || result == -ESTALE) {
376 fid_zero(&oic->oic_fid);
378 } else if (result == -EREMCHG) {
381 if (thread_is_running(&scrub->os_thread)) {
382 result = -EINPROGRESS;
383 } else if (!scrub->os_no_scrub) {
384 result = osd_scrub_start(dev);
385 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
386 "for "DFID", rc = %d [1]\n",
387 LDISKFS_SB(osd_sb(dev))->s_es->\
388 s_volume_name,PFID(fid), result);
389 if (result == 0 || result == -EALREADY)
390 result = -EINPROGRESS;
399 obj->oo_inode = inode;
400 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
401 if (dev->od_iop_mode) {
402 obj->oo_compat_dot_created = 1;
403 obj->oo_compat_dotdot_created = 1;
406 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
407 GOTO(out, result = 0);
409 LASSERT(obj->oo_hl_head == NULL);
410 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
411 if (obj->oo_hl_head == NULL) {
412 obj->oo_inode = NULL;
414 GOTO(out, result = -ENOMEM);
416 GOTO(out, result = 0);
419 LINVRNT(osd_invariant(obj));
424 * Concurrency: shouldn't matter.
426 static void osd_object_init0(struct osd_object *obj)
428 LASSERT(obj->oo_inode != NULL);
429 obj->oo_dt.do_body_ops = &osd_body_ops;
430 obj->oo_dt.do_lu.lo_header->loh_attr |=
431 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
435 * Concurrency: no concurrent access is possible that early in object
438 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
439 const struct lu_object_conf *conf)
441 struct osd_object *obj = osd_obj(l);
444 LINVRNT(osd_invariant(obj));
446 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
447 obj->oo_dt.do_body_ops = &osd_body_ops_new;
449 if (obj->oo_inode != NULL) {
450 osd_object_init0(obj);
451 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
452 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
453 /* LFSCK iterator object is special without inode */
454 l->lo_header->loh_attr |= LOHA_EXISTS;
457 LINVRNT(osd_invariant(obj));
462 * Concurrency: no concurrent access is possible that late in object
465 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
467 struct osd_object *obj = osd_obj(l);
469 LINVRNT(osd_invariant(obj));
471 dt_object_fini(&obj->oo_dt);
472 if (obj->oo_hl_head != NULL)
473 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
478 * Concurrency: no concurrent access is possible that late in object
481 static void osd_index_fini(struct osd_object *o)
483 struct iam_container *bag;
485 if (o->oo_dir != NULL) {
486 bag = &o->oo_dir->od_container;
487 if (o->oo_inode != NULL) {
488 if (bag->ic_object == o->oo_inode)
489 iam_container_fini(bag);
491 OBD_FREE_PTR(o->oo_dir);
497 * Concurrency: no concurrent access is possible that late in object
498 * life-cycle (for all existing callers, that is. New callers have to provide
499 * their own locking.)
501 static int osd_inode_unlinked(const struct inode *inode)
503 return inode->i_nlink == 0;
507 OSD_TXN_OI_DELETE_CREDITS = 20,
508 OSD_TXN_INODE_DELETE_CREDITS = 20
515 #if OSD_THANDLE_STATS
517 * Set time when the handle is allocated
519 static void osd_th_alloced(struct osd_thandle *oth)
521 oth->oth_alloced = cfs_time_current();
525 * Set time when the handle started
527 static void osd_th_started(struct osd_thandle *oth)
529 oth->oth_started = cfs_time_current();
533 * Helper function to convert time interval to microseconds packed in
534 * long int (default time units for the counter in "stats" initialized
535 * by lu_time_init() )
537 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
541 cfs_duration_usec(cfs_time_sub(end, start), &val);
542 return val.tv_sec * 1000000 + val.tv_usec;
546 * Check whether the we deal with this handle for too long.
548 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
549 cfs_time_t alloced, cfs_time_t started,
552 cfs_time_t now = cfs_time_current();
554 LASSERT(dev != NULL);
556 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
557 interval_to_usec(alloced, started));
558 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
559 interval_to_usec(started, closed));
560 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
561 interval_to_usec(closed, now));
563 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
564 CWARN("transaction handle %p was open for too long: "
566 "alloced "CFS_TIME_T" ,"
567 "started "CFS_TIME_T" ,"
568 "closed "CFS_TIME_T"\n",
569 oth, now, alloced, started, closed);
570 libcfs_debug_dumpstack(NULL);
574 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
576 cfs_time_t __closed = cfs_time_current(); \
577 cfs_time_t __alloced = oth->oth_alloced; \
578 cfs_time_t __started = oth->oth_started; \
581 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
584 #else /* OSD_THANDLE_STATS */
586 #define osd_th_alloced(h) do {} while(0)
587 #define osd_th_started(h) do {} while(0)
588 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
590 #endif /* OSD_THANDLE_STATS */
593 * Concurrency: doesn't access mutable data.
595 static int osd_param_is_sane(const struct osd_device *dev,
596 const struct thandle *th)
598 struct osd_thandle *oh;
599 oh = container_of0(th, struct osd_thandle, ot_super);
600 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
604 * Concurrency: shouldn't matter.
606 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
607 static void osd_trans_commit_cb(struct super_block *sb,
608 struct journal_callback *jcb, int error)
610 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
613 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
614 struct thandle *th = &oh->ot_super;
615 struct lu_device *lud = &th->th_dev->dd_lu_dev;
616 struct dt_txn_commit_cb *dcb, *tmp;
618 LASSERT(oh->ot_handle == NULL);
621 CERROR("transaction @0x%p commit error: %d\n", th, error);
623 dt_txn_hook_commit(th);
625 /* call per-transaction callbacks if any */
626 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
627 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
628 "commit callback entry: magic=%x name='%s'\n",
629 dcb->dcb_magic, dcb->dcb_name);
630 cfs_list_del_init(&dcb->dcb_linkage);
631 dcb->dcb_func(NULL, th, dcb, error);
634 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
638 lu_context_exit(&th->th_ctx);
639 lu_context_fini(&th->th_ctx);
643 static struct thandle *osd_trans_create(const struct lu_env *env,
646 struct osd_thread_info *oti = osd_oti_get(env);
647 struct osd_iobuf *iobuf = &oti->oti_iobuf;
648 struct osd_thandle *oh;
652 /* on pending IO in this thread should left from prev. request */
653 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
655 th = ERR_PTR(-ENOMEM);
656 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
658 oh->ot_quota_trans = &oti->oti_quota_trans;
659 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
663 th->th_tags = LCT_TX_HANDLE;
665 oti->oti_dev = osd_dt_dev(d);
666 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
673 * Concurrency: shouldn't matter.
675 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
678 struct osd_thread_info *oti = osd_oti_get(env);
679 struct osd_device *dev = osd_dt_dev(d);
681 struct osd_thandle *oh;
686 LASSERT(current->journal_info == NULL);
688 oh = container_of0(th, struct osd_thandle, ot_super);
690 LASSERT(oh->ot_handle == NULL);
692 rc = dt_txn_hook_start(env, d, th);
696 if (!osd_param_is_sane(dev, th)) {
697 CWARN("%.16s: too many transaction credits (%d > %d)\n",
698 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
700 osd_journal(dev)->j_max_transaction_buffers);
701 /* XXX Limit the credits to 'max_transaction_buffers', and
702 * let the underlying filesystem to catch the error if
703 * we really need so many credits.
705 * This should be removed when we can calculate the
706 * credits precisely. */
707 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
708 #ifdef OSD_TRACK_DECLARES
709 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
710 oh->ot_declare_attr_set, oh->ot_declare_punch,
711 oh->ot_declare_xattr_set);
712 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
713 oh->ot_declare_create, oh->ot_declare_ref_add,
714 oh->ot_declare_ref_del, oh->ot_declare_write);
715 CERROR(" insert: %d, delete: %d, destroy: %d\n",
716 oh->ot_declare_insert, oh->ot_declare_delete,
717 oh->ot_declare_destroy);
722 * XXX temporary stuff. Some abstraction layer should
725 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
729 LASSERT(oti->oti_txns == 0);
730 lu_context_init(&th->th_ctx, th->th_tags);
731 lu_context_enter(&th->th_ctx);
733 lu_device_get(&d->dd_lu_dev);
734 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
746 * Concurrency: shouldn't matter.
748 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
751 struct osd_thandle *oh;
752 struct osd_thread_info *oti = osd_oti_get(env);
753 struct osd_iobuf *iobuf = &oti->oti_iobuf;
754 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
757 oh = container_of0(th, struct osd_thandle, ot_super);
760 /* inform the quota slave device that the transaction is
762 qsd_op_end(env, qsd, oh->ot_quota_trans);
763 oh->ot_quota_trans = NULL;
765 if (oh->ot_handle != NULL) {
766 handle_t *hdl = oh->ot_handle;
769 * add commit callback
770 * notice we don't do this in osd_trans_start()
771 * as underlying transaction can change during truncate
773 osd_journal_callback_set(hdl, osd_trans_commit_cb,
776 LASSERT(oti->oti_txns == 1);
778 rc = dt_txn_hook_stop(env, th);
780 CERROR("Failure in transaction hook: %d\n", rc);
782 /* hook functions might modify th_sync */
783 hdl->h_sync = th->th_sync;
785 oh->ot_handle = NULL;
786 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
787 rc = ldiskfs_journal_stop(hdl));
789 CERROR("Failure to stop transaction: %d\n", rc);
794 /* as we want IO to journal and data IO be concurrent, we don't block
795 * awaiting data IO completion in osd_do_bio(), instead we wait here
796 * once transaction is submitted to the journal. all reqular requests
797 * don't do direct IO (except read/write), thus this wait_event becomes
800 * IMPORTANT: we have to wait till any IO submited by the thread is
801 * completed otherwise iobuf may be corrupted by different request
803 cfs_wait_event(iobuf->dr_wait,
804 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
806 rc = iobuf->dr_error;
811 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
813 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
816 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
817 LASSERT(&dcb->dcb_func != NULL);
818 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
824 * Called just before object is freed. Releases all resources except for
825 * object itself (that is released by osd_object_free()).
827 * Concurrency: no concurrent access is possible that late in object
830 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
832 struct osd_object *obj = osd_obj(l);
833 struct inode *inode = obj->oo_inode;
835 LINVRNT(osd_invariant(obj));
838 * If object is unlinked remove fid->ino mapping from object index.
844 obj->oo_inode = NULL;
849 * Concurrency: ->loo_object_release() is called under site spin-lock.
851 static void osd_object_release(const struct lu_env *env,
857 * Concurrency: shouldn't matter.
859 static int osd_object_print(const struct lu_env *env, void *cookie,
860 lu_printer_t p, const struct lu_object *l)
862 struct osd_object *o = osd_obj(l);
865 if (o->oo_dir != NULL)
866 d = o->oo_dir->od_container.ic_descr;
869 return (*p)(env, cookie,
870 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
872 o->oo_inode ? o->oo_inode->i_ino : 0UL,
873 o->oo_inode ? o->oo_inode->i_generation : 0,
874 d ? d->id_ops->id_name : "plain");
878 * Concurrency: shouldn't matter.
880 int osd_statfs(const struct lu_env *env, struct dt_device *d,
881 struct obd_statfs *sfs)
883 struct osd_device *osd = osd_dt_dev(d);
884 struct super_block *sb = osd_sb(osd);
885 struct kstatfs *ksfs;
888 if (unlikely(osd->od_mnt == NULL))
891 /* osd_lproc.c call this without env, allocate ksfs for that case */
892 if (unlikely(env == NULL)) {
897 ksfs = &osd_oti_get(env)->oti_ksfs;
900 cfs_spin_lock(&osd->od_osfs_lock);
902 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
903 result = sb->s_op->statfs(sb->s_root, ksfs);
904 if (likely(result == 0)) { /* N.B. statfs can't really fail */
905 osd->od_osfs_age = cfs_time_current_64();
906 statfs_pack(&osd->od_statfs, ksfs);
910 if (likely(result == 0))
911 *sfs = osd->od_statfs;
912 cfs_spin_unlock(&osd->od_osfs_lock);
914 if (unlikely(env == NULL))
921 * Concurrency: doesn't access mutable data.
923 static void osd_conf_get(const struct lu_env *env,
924 const struct dt_device *dev,
925 struct dt_device_param *param)
927 struct super_block *sb = osd_sb(osd_dt_dev(dev));
930 * XXX should be taken from not-yet-existing fs abstraction layer.
932 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
933 param->ddp_max_name_len = LDISKFS_NAME_LEN;
934 param->ddp_max_nlink = LDISKFS_LINK_MAX;
935 param->ddp_block_shift = sb->s_blocksize_bits;
936 param->ddp_mount_type = LDD_MT_LDISKFS;
937 param->ddp_mntopts = 0;
938 if (test_opt(sb, XATTR_USER))
939 param->ddp_mntopts |= MNTOPT_USERXATTR;
940 if (test_opt(sb, POSIX_ACL))
941 param->ddp_mntopts |= MNTOPT_ACL;
943 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
944 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
945 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
948 param->ddp_max_ea_size = sb->s_blocksize;
953 * Helper function to get and fill the buffer with input values.
955 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
959 buf = &osd_oti_get(env)->oti_buf;
966 * Concurrency: shouldn't matter.
968 static int osd_sync(const struct lu_env *env, struct dt_device *d)
970 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
971 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
975 * Start commit for OSD device.
977 * An implementation of dt_commit_async method for OSD device.
978 * Asychronously starts underlayng fs sync and thereby a transaction
981 * \param env environment
984 * \see dt_device_operations
986 static int osd_commit_async(const struct lu_env *env,
989 struct super_block *s = osd_sb(osd_dt_dev(d));
992 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
993 RETURN(s->s_op->sync_fs(s, 0));
997 * Concurrency: shouldn't matter.
1000 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1002 struct super_block *sb = osd_sb(osd_dt_dev(d));
1006 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1008 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1013 * Concurrency: serialization provided by callers.
1015 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1016 int mode, unsigned long timeout, __u32 alg,
1017 struct lustre_capa_key *keys)
1019 struct osd_device *dev = osd_dt_dev(d);
1022 dev->od_fl_capa = mode;
1023 dev->od_capa_timeout = timeout;
1024 dev->od_capa_alg = alg;
1025 dev->od_capa_keys = keys;
1030 * Concurrency: serialization provided by callers.
1032 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1033 struct dt_quota_ctxt *ctxt, void *data)
1035 struct obd_device *obd = (void *)ctxt;
1036 struct vfsmount *mnt = (struct vfsmount *)data;
1039 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1040 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1041 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1042 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1043 obd->obd_lvfs_ctxt.fs = get_ds();
1049 * Note: we do not count into QUOTA here.
1050 * If we mount with --data_journal we may need more.
1052 const int osd_dto_credits_noquota[DTO_NR] = {
1055 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1056 * SINGLEDATA_TRANS_BLOCKS(8)
1057 * XXX Note: maybe iam need more, since iam have more level than
1060 [DTO_INDEX_INSERT] = 16,
1061 [DTO_INDEX_DELETE] = 16,
1065 [DTO_INDEX_UPDATE] = 16,
1067 * Create a object. The same as create object in EXT3.
1068 * DATA_TRANS_BLOCKS(14) +
1069 * INDEX_EXTRA_BLOCKS(8) +
1070 * 3(inode bits, groups, GDT)
1072 [DTO_OBJECT_CREATE] = 25,
1074 * XXX: real credits to be fixed
1076 [DTO_OBJECT_DELETE] = 25,
1078 * Attr set credits (inode)
1080 [DTO_ATTR_SET_BASE] = 1,
1082 * Xattr set. The same as xattr of EXT3.
1083 * DATA_TRANS_BLOCKS(14)
1084 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1085 * are also counted in. Do not know why?
1087 [DTO_XATTR_SET] = 14,
1090 * credits for inode change during write.
1092 [DTO_WRITE_BASE] = 3,
1094 * credits for single block write.
1096 [DTO_WRITE_BLOCK] = 14,
1098 * Attr set credits for chown.
1099 * This is extra credits for setattr, and it is null without quota
1101 [DTO_ATTR_SET_CHOWN]= 0
1104 static const struct dt_device_operations osd_dt_ops = {
1105 .dt_root_get = osd_root_get,
1106 .dt_statfs = osd_statfs,
1107 .dt_trans_create = osd_trans_create,
1108 .dt_trans_start = osd_trans_start,
1109 .dt_trans_stop = osd_trans_stop,
1110 .dt_trans_cb_add = osd_trans_cb_add,
1111 .dt_conf_get = osd_conf_get,
1112 .dt_sync = osd_sync,
1114 .dt_commit_async = osd_commit_async,
1115 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1116 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1119 static void osd_object_read_lock(const struct lu_env *env,
1120 struct dt_object *dt, unsigned role)
1122 struct osd_object *obj = osd_dt_obj(dt);
1123 struct osd_thread_info *oti = osd_oti_get(env);
1125 LINVRNT(osd_invariant(obj));
1127 LASSERT(obj->oo_owner != env);
1128 cfs_down_read_nested(&obj->oo_sem, role);
1130 LASSERT(obj->oo_owner == NULL);
1134 static void osd_object_write_lock(const struct lu_env *env,
1135 struct dt_object *dt, unsigned role)
1137 struct osd_object *obj = osd_dt_obj(dt);
1138 struct osd_thread_info *oti = osd_oti_get(env);
1140 LINVRNT(osd_invariant(obj));
1142 LASSERT(obj->oo_owner != env);
1143 cfs_down_write_nested(&obj->oo_sem, role);
1145 LASSERT(obj->oo_owner == NULL);
1146 obj->oo_owner = env;
1150 static void osd_object_read_unlock(const struct lu_env *env,
1151 struct dt_object *dt)
1153 struct osd_object *obj = osd_dt_obj(dt);
1154 struct osd_thread_info *oti = osd_oti_get(env);
1156 LINVRNT(osd_invariant(obj));
1158 LASSERT(oti->oti_r_locks > 0);
1160 cfs_up_read(&obj->oo_sem);
1163 static void osd_object_write_unlock(const struct lu_env *env,
1164 struct dt_object *dt)
1166 struct osd_object *obj = osd_dt_obj(dt);
1167 struct osd_thread_info *oti = osd_oti_get(env);
1169 LINVRNT(osd_invariant(obj));
1171 LASSERT(obj->oo_owner == env);
1172 LASSERT(oti->oti_w_locks > 0);
1174 obj->oo_owner = NULL;
1175 cfs_up_write(&obj->oo_sem);
1178 static int osd_object_write_locked(const struct lu_env *env,
1179 struct dt_object *dt)
1181 struct osd_object *obj = osd_dt_obj(dt);
1183 LINVRNT(osd_invariant(obj));
1185 return obj->oo_owner == env;
1188 static int capa_is_sane(const struct lu_env *env,
1189 struct osd_device *dev,
1190 struct lustre_capa *capa,
1191 struct lustre_capa_key *keys)
1193 struct osd_thread_info *oti = osd_oti_get(env);
1194 struct lustre_capa *tcapa = &oti->oti_capa;
1195 struct obd_capa *oc;
1199 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1201 if (capa_is_expired(oc)) {
1202 DEBUG_CAPA(D_ERROR, capa, "expired");
1209 if (capa_is_expired_sec(capa)) {
1210 DEBUG_CAPA(D_ERROR, capa, "expired");
1214 cfs_spin_lock(&capa_lock);
1215 for (i = 0; i < 2; i++) {
1216 if (keys[i].lk_keyid == capa->lc_keyid) {
1217 oti->oti_capa_key = keys[i];
1221 cfs_spin_unlock(&capa_lock);
1224 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1228 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1232 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1233 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1237 oc = capa_add(dev->od_capa_hash, capa);
1243 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1244 struct lustre_capa *capa, __u64 opc)
1246 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1247 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1248 struct md_capainfo *ci;
1251 if (!dev->od_fl_capa)
1254 if (capa == BYPASS_CAPA)
1257 ci = md_capainfo(env);
1261 if (ci->mc_auth == LC_ID_NONE)
1265 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1269 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1270 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1275 if (!capa_opc_supported(capa, opc)) {
1276 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1280 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1281 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1288 static struct timespec *osd_inode_time(const struct lu_env *env,
1289 struct inode *inode, __u64 seconds)
1291 struct osd_thread_info *oti = osd_oti_get(env);
1292 struct timespec *t = &oti->oti_time;
1294 t->tv_sec = seconds;
1296 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1301 static void osd_inode_getattr(const struct lu_env *env,
1302 struct inode *inode, struct lu_attr *attr)
1304 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1305 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1306 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1308 attr->la_atime = LTIME_S(inode->i_atime);
1309 attr->la_mtime = LTIME_S(inode->i_mtime);
1310 attr->la_ctime = LTIME_S(inode->i_ctime);
1311 attr->la_mode = inode->i_mode;
1312 attr->la_size = i_size_read(inode);
1313 attr->la_blocks = inode->i_blocks;
1314 attr->la_uid = inode->i_uid;
1315 attr->la_gid = inode->i_gid;
1316 attr->la_flags = LDISKFS_I(inode)->i_flags;
1317 attr->la_nlink = inode->i_nlink;
1318 attr->la_rdev = inode->i_rdev;
1319 attr->la_blksize = 1 << inode->i_blkbits;
1320 attr->la_blkbits = inode->i_blkbits;
1323 static int osd_attr_get(const struct lu_env *env,
1324 struct dt_object *dt,
1325 struct lu_attr *attr,
1326 struct lustre_capa *capa)
1328 struct osd_object *obj = osd_dt_obj(dt);
1330 LASSERT(dt_object_exists(dt));
1331 LINVRNT(osd_invariant(obj));
1333 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1336 cfs_spin_lock(&obj->oo_guard);
1337 osd_inode_getattr(env, obj->oo_inode, attr);
1338 cfs_spin_unlock(&obj->oo_guard);
1342 static int osd_declare_attr_set(const struct lu_env *env,
1343 struct dt_object *dt,
1344 const struct lu_attr *attr,
1345 struct thandle *handle)
1347 struct osd_thandle *oh;
1348 struct osd_object *obj;
1349 struct osd_thread_info *info = osd_oti_get(env);
1350 struct lquota_id_info *qi = &info->oti_qi;
1356 LASSERT(dt != NULL);
1357 LASSERT(handle != NULL);
1359 obj = osd_dt_obj(dt);
1360 LASSERT(osd_invariant(obj));
1362 oh = container_of0(handle, struct osd_thandle, ot_super);
1363 LASSERT(oh->ot_handle == NULL);
1365 OSD_DECLARE_OP(oh, attr_set);
1366 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1368 if (attr == NULL || obj->oo_inode == NULL)
1371 bspace = obj->oo_inode->i_blocks;
1372 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1373 bspace = toqb(bspace);
1375 /* Changing ownership is always preformed by super user, it should not
1378 * We still need to call the osd_declare_qid() to calculate the journal
1379 * credits for updating quota accounting files and to trigger quota
1380 * space adjustment once the operation is completed.*/
1381 if ((attr->la_valid & LA_UID) != 0 &&
1382 attr->la_uid != obj->oo_inode->i_uid) {
1383 qi->lqi_type = USRQUOTA;
1385 /* inode accounting */
1386 qi->lqi_is_blk = false;
1388 /* one more inode for the new owner ... */
1389 qi->lqi_id.qid_uid = attr->la_uid;
1391 allocated = (attr->la_uid == 0) ? true : false;
1392 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1393 if (rc == -EDQUOT || rc == -EINPROGRESS)
1398 /* and one less inode for the current uid */
1399 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1401 rc = osd_declare_qid(env, oh, qi, true, NULL);
1402 if (rc == -EDQUOT || rc == -EINPROGRESS)
1407 /* block accounting */
1408 qi->lqi_is_blk = true;
1410 /* more blocks for the new owner ... */
1411 qi->lqi_id.qid_uid = attr->la_uid;
1412 qi->lqi_space = bspace;
1413 allocated = (attr->la_uid == 0) ? true : false;
1414 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1415 if (rc == -EDQUOT || rc == -EINPROGRESS)
1420 /* and finally less blocks for the current owner */
1421 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1422 qi->lqi_space = -bspace;
1423 rc = osd_declare_qid(env, oh, qi, true, NULL);
1424 if (rc == -EDQUOT || rc == -EINPROGRESS)
1430 if (attr->la_valid & LA_GID &&
1431 attr->la_gid != obj->oo_inode->i_gid) {
1432 qi->lqi_type = GRPQUOTA;
1434 /* inode accounting */
1435 qi->lqi_is_blk = false;
1437 /* one more inode for the new group owner ... */
1438 qi->lqi_id.qid_gid = attr->la_gid;
1440 allocated = (attr->la_gid == 0) ? true : false;
1441 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1442 if (rc == -EDQUOT || rc == -EINPROGRESS)
1447 /* and one less inode for the current gid */
1448 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1450 rc = osd_declare_qid(env, oh, qi, true, NULL);
1451 if (rc == -EDQUOT || rc == -EINPROGRESS)
1456 /* block accounting */
1457 qi->lqi_is_blk = true;
1459 /* more blocks for the new owner ... */
1460 qi->lqi_id.qid_gid = attr->la_gid;
1461 qi->lqi_space = bspace;
1462 allocated = (attr->la_gid == 0) ? true : false;
1463 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1464 if (rc == -EDQUOT || rc == -EINPROGRESS)
1469 /* and finally less blocks for the current owner */
1470 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1471 qi->lqi_space = -bspace;
1472 rc = osd_declare_qid(env, oh, qi, true, NULL);
1473 if (rc == -EDQUOT || rc == -EINPROGRESS)
1482 static int osd_inode_setattr(const struct lu_env *env,
1483 struct inode *inode, const struct lu_attr *attr)
1487 bits = attr->la_valid;
1489 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1491 if (bits & LA_ATIME)
1492 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1493 if (bits & LA_CTIME)
1494 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1495 if (bits & LA_MTIME)
1496 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1497 if (bits & LA_SIZE) {
1498 LDISKFS_I(inode)->i_disksize = attr->la_size;
1499 i_size_write(inode, attr->la_size);
1503 /* OSD should not change "i_blocks" which is used by quota.
1504 * "i_blocks" should be changed by ldiskfs only. */
1505 if (bits & LA_BLOCKS)
1506 inode->i_blocks = attr->la_blocks;
1509 inode->i_mode = (inode->i_mode & S_IFMT) |
1510 (attr->la_mode & ~S_IFMT);
1512 inode->i_uid = attr->la_uid;
1514 inode->i_gid = attr->la_gid;
1515 if (bits & LA_NLINK)
1516 inode->i_nlink = attr->la_nlink;
1518 inode->i_rdev = attr->la_rdev;
1520 if (bits & LA_FLAGS) {
1521 /* always keep S_NOCMTIME */
1522 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1528 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1530 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1531 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1536 if (attr->la_valid & LA_UID)
1537 iattr.ia_valid |= ATTR_UID;
1538 if (attr->la_valid & LA_GID)
1539 iattr.ia_valid |= ATTR_GID;
1540 iattr.ia_uid = attr->la_uid;
1541 iattr.ia_gid = attr->la_gid;
1543 rc = ll_vfs_dq_transfer(inode, &iattr);
1545 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1546 "enforcement enabled on the ldiskfs filesystem?",
1547 inode->i_sb->s_id, rc);
1554 static int osd_attr_set(const struct lu_env *env,
1555 struct dt_object *dt,
1556 const struct lu_attr *attr,
1557 struct thandle *handle,
1558 struct lustre_capa *capa)
1560 struct osd_object *obj = osd_dt_obj(dt);
1561 struct inode *inode;
1564 LASSERT(handle != NULL);
1565 LASSERT(dt_object_exists(dt));
1566 LASSERT(osd_invariant(obj));
1568 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1571 OSD_EXEC_OP(handle, attr_set);
1573 inode = obj->oo_inode;
1575 rc = osd_quota_transfer(inode, attr);
1579 cfs_spin_lock(&obj->oo_guard);
1580 rc = osd_inode_setattr(env, inode, attr);
1581 cfs_spin_unlock(&obj->oo_guard);
1584 inode->i_sb->s_op->dirty_inode(inode);
1588 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1589 struct osd_object *obj,
1590 const char *name, const int namelen)
1592 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1595 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1597 struct dt_allocation_hint *hint,
1601 struct osd_device *osd = osd_obj2dev(obj);
1602 struct osd_thandle *oth;
1603 struct dt_object *parent = NULL;
1604 struct inode *inode;
1606 LINVRNT(osd_invariant(obj));
1607 LASSERT(obj->oo_inode == NULL);
1608 LASSERT(obj->oo_hl_head == NULL);
1610 if (S_ISDIR(mode) && ldiskfs_pdo) {
1611 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1612 if (obj->oo_hl_head == NULL)
1616 oth = container_of(th, struct osd_thandle, ot_super);
1617 LASSERT(oth->ot_handle->h_transaction != NULL);
1619 if (hint && hint->dah_parent)
1620 parent = hint->dah_parent;
1622 inode = ldiskfs_create_inode(oth->ot_handle,
1623 parent ? osd_dt_obj(parent)->oo_inode :
1624 osd_sb(osd)->s_root->d_inode,
1626 if (!IS_ERR(inode)) {
1627 /* Do not update file c/mtime in ldiskfs.
1628 * NB: don't need any lock because no contention at this
1630 inode->i_flags |= S_NOCMTIME;
1631 inode->i_state |= I_LUSTRE_NOSCRUB;
1632 obj->oo_inode = inode;
1635 if (obj->oo_hl_head != NULL) {
1636 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1637 obj->oo_hl_head = NULL;
1639 result = PTR_ERR(inode);
1641 LINVRNT(osd_invariant(obj));
1649 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1650 struct lu_attr *attr,
1651 struct dt_allocation_hint *hint,
1652 struct dt_object_format *dof,
1656 struct osd_thandle *oth;
1657 struct osd_device *osd = osd_obj2dev(obj);
1658 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1660 LASSERT(S_ISDIR(attr->la_mode));
1662 oth = container_of(th, struct osd_thandle, ot_super);
1663 LASSERT(oth->ot_handle->h_transaction != NULL);
1664 result = osd_mkfile(info, obj, mode, hint, th);
1665 if (result == 0 && osd->od_iop_mode == 0) {
1666 LASSERT(obj->oo_inode != NULL);
1668 * XXX uh-oh... call low-level iam function directly.
1671 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1672 sizeof (struct osd_fid_pack),
1678 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1679 struct lu_attr *attr,
1680 struct dt_allocation_hint *hint,
1681 struct dt_object_format *dof,
1685 struct osd_thandle *oth;
1686 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1688 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1690 LASSERT(S_ISREG(attr->la_mode));
1692 oth = container_of(th, struct osd_thandle, ot_super);
1693 LASSERT(oth->ot_handle->h_transaction != NULL);
1695 result = osd_mkfile(info, obj, mode, hint, th);
1697 LASSERT(obj->oo_inode != NULL);
1698 if (feat->dif_flags & DT_IND_VARKEY)
1699 result = iam_lvar_create(obj->oo_inode,
1700 feat->dif_keysize_max,
1702 feat->dif_recsize_max,
1705 result = iam_lfix_create(obj->oo_inode,
1706 feat->dif_keysize_max,
1708 feat->dif_recsize_max,
1715 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1716 struct lu_attr *attr,
1717 struct dt_allocation_hint *hint,
1718 struct dt_object_format *dof,
1721 LASSERT(S_ISREG(attr->la_mode));
1722 return osd_mkfile(info, obj, (attr->la_mode &
1723 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1726 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1727 struct lu_attr *attr,
1728 struct dt_allocation_hint *hint,
1729 struct dt_object_format *dof,
1732 LASSERT(S_ISLNK(attr->la_mode));
1733 return osd_mkfile(info, obj, (attr->la_mode &
1734 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1737 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1738 struct lu_attr *attr,
1739 struct dt_allocation_hint *hint,
1740 struct dt_object_format *dof,
1743 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1746 LINVRNT(osd_invariant(obj));
1747 LASSERT(obj->oo_inode == NULL);
1748 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1749 S_ISFIFO(mode) || S_ISSOCK(mode));
1751 result = osd_mkfile(info, obj, mode, hint, th);
1753 LASSERT(obj->oo_inode != NULL);
1755 * This inode should be marked dirty for i_rdev. Currently
1756 * that is done in the osd_attr_init().
1758 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1760 LINVRNT(osd_invariant(obj));
1764 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1766 struct dt_allocation_hint *hint,
1767 struct dt_object_format *dof,
1770 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1772 osd_obj_type_f result;
1788 result = osd_mk_index;
1799 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1800 struct dt_object *parent, struct dt_object *child,
1801 cfs_umode_t child_mode)
1805 memset(ah, 0, sizeof(*ah));
1806 ah->dah_parent = parent;
1807 ah->dah_mode = child_mode;
1810 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1811 struct lu_attr *attr, struct dt_object_format *dof)
1813 struct inode *inode = obj->oo_inode;
1814 __u64 valid = attr->la_valid;
1817 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1819 if (dof->dof_type != DFT_NODE)
1820 attr->la_valid &= ~LA_RDEV;
1821 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1822 attr->la_valid &= ~LA_ATIME;
1823 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1824 attr->la_valid &= ~LA_CTIME;
1825 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1826 attr->la_valid &= ~LA_MTIME;
1828 result = osd_quota_transfer(inode, attr);
1832 if (attr->la_valid != 0) {
1833 result = osd_inode_setattr(info->oti_env, inode, attr);
1835 * The osd_inode_setattr() should always succeed here. The
1836 * only error that could be returned is EDQUOT when we are
1837 * trying to change the UID or GID of the inode. However, this
1838 * should not happen since quota enforcement is no longer
1839 * enabled on ldiskfs (lquota takes care of it).
1841 LASSERTF(result == 0, "%d", result);
1842 inode->i_sb->s_op->dirty_inode(inode);
1845 attr->la_valid = valid;
1849 * Helper function for osd_object_create()
1851 * \retval 0, on success
1853 static int __osd_object_create(struct osd_thread_info *info,
1854 struct osd_object *obj, struct lu_attr *attr,
1855 struct dt_allocation_hint *hint,
1856 struct dt_object_format *dof,
1862 /* we drop umask so that permissions we pass are not affected */
1863 umask = current->fs->umask;
1864 current->fs->umask = 0;
1866 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1869 osd_attr_init(info, obj, attr, dof);
1870 osd_object_init0(obj);
1872 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1873 unlock_new_inode(obj->oo_inode);
1876 /* restore previous umask value */
1877 current->fs->umask = umask;
1883 * Helper function for osd_object_create()
1885 * \retval 0, on success
1887 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1888 const struct lu_fid *fid, struct thandle *th)
1890 struct osd_thread_info *info = osd_oti_get(env);
1891 struct osd_inode_id *id = &info->oti_id;
1892 struct osd_device *osd = osd_obj2dev(obj);
1894 LASSERT(obj->oo_inode != NULL);
1896 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1897 return osd_oi_insert(info, osd, fid, id, th);
1900 static int osd_declare_object_create(const struct lu_env *env,
1901 struct dt_object *dt,
1902 struct lu_attr *attr,
1903 struct dt_allocation_hint *hint,
1904 struct dt_object_format *dof,
1905 struct thandle *handle)
1907 struct osd_thandle *oh;
1911 LASSERT(handle != NULL);
1913 oh = container_of0(handle, struct osd_thandle, ot_super);
1914 LASSERT(oh->ot_handle == NULL);
1916 OSD_DECLARE_OP(oh, create);
1917 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1918 /* XXX: So far, only normal fid needs be inserted into the oi,
1919 * things could be changed later. Revise following code then. */
1920 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1921 OSD_DECLARE_OP(oh, insert);
1922 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1923 /* Reuse idle OI block may cause additional one OI block
1925 oh->ot_credits += 1;
1927 /* If this is directory, then we expect . and .. to be inserted as
1928 * well. The one directory block always needs to be created for the
1929 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1930 * block), there is no danger of needing a tree for the first block.
1932 if (attr && S_ISDIR(attr->la_mode)) {
1933 OSD_DECLARE_OP(oh, insert);
1934 OSD_DECLARE_OP(oh, insert);
1935 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1941 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
1942 false, false, NULL, false);
1946 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1947 struct lu_attr *attr,
1948 struct dt_allocation_hint *hint,
1949 struct dt_object_format *dof,
1952 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1953 struct osd_object *obj = osd_dt_obj(dt);
1954 struct osd_thread_info *info = osd_oti_get(env);
1959 LINVRNT(osd_invariant(obj));
1960 LASSERT(!dt_object_exists(dt));
1961 LASSERT(osd_write_locked(env, obj));
1962 LASSERT(th != NULL);
1964 if (unlikely(fid_is_acct(fid)))
1965 /* Quota files can't be created from the kernel any more,
1966 * 'tune2fs -O quota' will take care of creating them */
1969 OSD_EXEC_OP(th, create);
1971 result = __osd_object_create(info, obj, attr, hint, dof, th);
1973 result = __osd_oi_insert(env, obj, fid, th);
1975 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1976 LASSERT(osd_invariant(obj));
1981 * Called to destroy on-disk representation of the object
1983 * Concurrency: must be locked
1985 static int osd_declare_object_destroy(const struct lu_env *env,
1986 struct dt_object *dt,
1989 struct osd_object *obj = osd_dt_obj(dt);
1990 struct inode *inode = obj->oo_inode;
1991 struct osd_thandle *oh;
1995 oh = container_of0(th, struct osd_thandle, ot_super);
1996 LASSERT(oh->ot_handle == NULL);
1999 OSD_DECLARE_OP(oh, destroy);
2000 OSD_DECLARE_OP(oh, delete);
2001 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2002 /* XXX: So far, only normal fid needs to be inserted into the OI,
2003 * so only normal fid needs to be removed from the OI also. */
2004 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
2005 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2006 /* Recycle idle OI leaf may cause additional three OI blocks
2008 oh->ot_credits += 3;
2011 /* one less inode */
2012 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2013 false, true, NULL, false);
2016 /* data to be truncated */
2017 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh, true,
2022 static int osd_object_destroy(const struct lu_env *env,
2023 struct dt_object *dt,
2026 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2027 struct osd_object *obj = osd_dt_obj(dt);
2028 struct inode *inode = obj->oo_inode;
2029 struct osd_device *osd = osd_obj2dev(obj);
2030 struct osd_thandle *oh;
2034 oh = container_of0(th, struct osd_thandle, ot_super);
2035 LASSERT(oh->ot_handle);
2037 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2039 if (unlikely(fid_is_acct(fid)))
2042 /* Parallel control for OI scrub. For most of cases, there is no
2043 * lock contention. So it will not affect unlink performance. */
2044 cfs_mutex_lock(&inode->i_mutex);
2045 if (S_ISDIR(inode->i_mode)) {
2046 LASSERT(osd_inode_unlinked(inode) ||
2047 inode->i_nlink == 1);
2048 cfs_spin_lock(&obj->oo_guard);
2050 cfs_spin_unlock(&obj->oo_guard);
2051 inode->i_sb->s_op->dirty_inode(inode);
2053 LASSERT(osd_inode_unlinked(inode));
2056 OSD_EXEC_OP(th, destroy);
2058 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2059 cfs_mutex_unlock(&inode->i_mutex);
2061 /* XXX: add to ext3 orphan list */
2062 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2064 /* not needed in the cache anymore */
2065 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2071 * Helper function for osd_xattr_set()
2073 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2074 const struct lu_buf *buf, const char *name, int fl)
2076 struct osd_object *obj = osd_dt_obj(dt);
2077 struct inode *inode = obj->oo_inode;
2078 struct osd_thread_info *info = osd_oti_get(env);
2079 struct dentry *dentry = &info->oti_child_dentry;
2083 LASSERT(dt_object_exists(dt));
2084 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2086 if (fl & LU_XATTR_REPLACE)
2087 fs_flags |= XATTR_REPLACE;
2089 if (fl & LU_XATTR_CREATE)
2090 fs_flags |= XATTR_CREATE;
2092 dentry->d_inode = inode;
2093 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2094 buf->lb_len, fs_flags);
2099 * Put the fid into lustre_mdt_attrs, and then place the structure
2100 * inode's ea. This fid should not be altered during the life time
2103 * \retval +ve, on success
2104 * \retval -ve, on error
2106 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2108 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2109 const struct lu_fid *fid)
2111 struct osd_thread_info *info = osd_oti_get(env);
2112 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2114 lustre_lma_init(mdt_attrs, fid);
2115 lustre_lma_swab(mdt_attrs);
2116 return __osd_xattr_set(env, dt,
2117 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2118 XATTR_NAME_LMA, LU_XATTR_CREATE);
2123 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2124 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2125 * To have compatilibility with 1.8 ldiskfs driver we need to have
2126 * magic number at start of fid data.
2127 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2130 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2131 const struct dt_rec *fid)
2133 param->edp_magic = LDISKFS_LUFID_MAGIC;
2134 param->edp_len = sizeof(struct lu_fid) + 1;
2136 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2137 (struct lu_fid *)fid);
2141 * Try to read the fid from inode ea into dt_rec, if return value
2142 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2144 * \param fid object fid.
2146 * \retval 0 on success
2148 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2149 __u32 ino, struct lu_fid *fid,
2150 struct osd_inode_id *id)
2152 struct osd_thread_info *info = osd_oti_get(env);
2153 struct inode *inode;
2156 osd_id_gen(id, ino, OSD_OII_NOGEN);
2157 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2159 RETURN(PTR_ERR(inode));
2166 * OSD layer object create function for interoperability mode (b11826).
2167 * This is mostly similar to osd_object_create(). Only difference being, fid is
2168 * inserted into inode ea here.
2170 * \retval 0, on success
2171 * \retval -ve, on error
2173 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2174 struct lu_attr *attr,
2175 struct dt_allocation_hint *hint,
2176 struct dt_object_format *dof,
2179 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2180 struct osd_object *obj = osd_dt_obj(dt);
2181 struct osd_thread_info *info = osd_oti_get(env);
2186 LASSERT(osd_invariant(obj));
2187 LASSERT(!dt_object_exists(dt));
2188 LASSERT(osd_write_locked(env, obj));
2189 LASSERT(th != NULL);
2191 if (unlikely(fid_is_acct(fid)))
2192 /* Quota files can't be created from the kernel any more,
2193 * 'tune2fs -O quota' will take care of creating them */
2196 OSD_EXEC_OP(th, create);
2198 result = __osd_object_create(info, obj, attr, hint, dof, th);
2199 /* objects under osd root shld have igif fid, so dont add fid EA */
2200 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2201 result = osd_ea_fid_set(env, dt, fid);
2204 result = __osd_oi_insert(env, obj, fid, th);
2206 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2207 LINVRNT(osd_invariant(obj));
2211 static int osd_declare_object_ref_add(const struct lu_env *env,
2212 struct dt_object *dt,
2213 struct thandle *handle)
2215 struct osd_thandle *oh;
2217 /* it's possible that object doesn't exist yet */
2218 LASSERT(handle != NULL);
2220 oh = container_of0(handle, struct osd_thandle, ot_super);
2221 LASSERT(oh->ot_handle == NULL);
2223 OSD_DECLARE_OP(oh, ref_add);
2224 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2230 * Concurrency: @dt is write locked.
2232 static int osd_object_ref_add(const struct lu_env *env,
2233 struct dt_object *dt, struct thandle *th)
2235 struct osd_object *obj = osd_dt_obj(dt);
2236 struct inode *inode = obj->oo_inode;
2238 LINVRNT(osd_invariant(obj));
2239 LASSERT(dt_object_exists(dt));
2240 LASSERT(osd_write_locked(env, obj));
2241 LASSERT(th != NULL);
2243 OSD_EXEC_OP(th, ref_add);
2246 * DIR_NLINK feature is set for compatibility reasons if:
2247 * 1) nlinks > LDISKFS_LINK_MAX, or
2248 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2250 * It is easier to always set this flag (rather than check and set),
2251 * since it has less overhead, and the superblock will be dirtied
2252 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2253 * do not actually care whether this flag is set or not.
2255 cfs_spin_lock(&obj->oo_guard);
2257 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2258 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2259 inode->i_nlink == 2)
2262 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2263 cfs_spin_unlock(&obj->oo_guard);
2264 inode->i_sb->s_op->dirty_inode(inode);
2265 LINVRNT(osd_invariant(obj));
2270 static int osd_declare_object_ref_del(const struct lu_env *env,
2271 struct dt_object *dt,
2272 struct thandle *handle)
2274 struct osd_thandle *oh;
2276 LASSERT(dt_object_exists(dt));
2277 LASSERT(handle != NULL);
2279 oh = container_of0(handle, struct osd_thandle, ot_super);
2280 LASSERT(oh->ot_handle == NULL);
2282 OSD_DECLARE_OP(oh, ref_del);
2283 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2289 * Concurrency: @dt is write locked.
2291 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2294 struct osd_object *obj = osd_dt_obj(dt);
2295 struct inode *inode = obj->oo_inode;
2297 LINVRNT(osd_invariant(obj));
2298 LASSERT(dt_object_exists(dt));
2299 LASSERT(osd_write_locked(env, obj));
2300 LASSERT(th != NULL);
2302 OSD_EXEC_OP(th, ref_del);
2304 cfs_spin_lock(&obj->oo_guard);
2305 LASSERT(inode->i_nlink > 0);
2307 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2308 * then the nlink count is 1. Don't let it be set to 0 or the directory
2309 * inode will be deleted incorrectly. */
2310 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2312 cfs_spin_unlock(&obj->oo_guard);
2313 inode->i_sb->s_op->dirty_inode(inode);
2314 LINVRNT(osd_invariant(obj));
2320 * Get the 64-bit version for an inode.
2322 static int osd_object_version_get(const struct lu_env *env,
2323 struct dt_object *dt, dt_obj_version_t *ver)
2325 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2327 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2328 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2329 *ver = LDISKFS_I(inode)->i_fs_version;
2334 * Concurrency: @dt is read locked.
2336 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2337 struct lu_buf *buf, const char *name,
2338 struct lustre_capa *capa)
2340 struct osd_object *obj = osd_dt_obj(dt);
2341 struct inode *inode = obj->oo_inode;
2342 struct osd_thread_info *info = osd_oti_get(env);
2343 struct dentry *dentry = &info->oti_obj_dentry;
2345 /* version get is not real XATTR but uses xattr API */
2346 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2347 /* for version we are just using xattr API but change inode
2349 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2350 osd_object_version_get(env, dt, buf->lb_buf);
2351 return sizeof(dt_obj_version_t);
2354 LASSERT(dt_object_exists(dt));
2355 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2357 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2360 dentry->d_inode = inode;
2361 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2365 static int osd_declare_xattr_set(const struct lu_env *env,
2366 struct dt_object *dt,
2367 const struct lu_buf *buf, const char *name,
2368 int fl, struct thandle *handle)
2370 struct osd_thandle *oh;
2372 LASSERT(handle != NULL);
2374 oh = container_of0(handle, struct osd_thandle, ot_super);
2375 LASSERT(oh->ot_handle == NULL);
2377 OSD_DECLARE_OP(oh, xattr_set);
2378 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2379 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2381 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2387 * Set the 64-bit version for object
2389 static void osd_object_version_set(const struct lu_env *env,
2390 struct dt_object *dt,
2391 dt_obj_version_t *new_version)
2393 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2395 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2396 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2398 LDISKFS_I(inode)->i_fs_version = *new_version;
2399 /** Version is set after all inode operations are finished,
2400 * so we should mark it dirty here */
2401 inode->i_sb->s_op->dirty_inode(inode);
2405 * Concurrency: @dt is write locked.
2407 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2408 const struct lu_buf *buf, const char *name, int fl,
2409 struct thandle *handle, struct lustre_capa *capa)
2411 LASSERT(handle != NULL);
2413 /* version set is not real XATTR */
2414 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2415 /* for version we are just using xattr API but change inode
2417 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2418 osd_object_version_set(env, dt, buf->lb_buf);
2419 return sizeof(dt_obj_version_t);
2422 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2425 OSD_EXEC_OP(handle, xattr_set);
2426 return __osd_xattr_set(env, dt, buf, name, fl);
2430 * Concurrency: @dt is read locked.
2432 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2433 struct lu_buf *buf, struct lustre_capa *capa)
2435 struct osd_object *obj = osd_dt_obj(dt);
2436 struct inode *inode = obj->oo_inode;
2437 struct osd_thread_info *info = osd_oti_get(env);
2438 struct dentry *dentry = &info->oti_obj_dentry;
2440 LASSERT(dt_object_exists(dt));
2441 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2442 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2444 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2447 dentry->d_inode = inode;
2448 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2451 static int osd_declare_xattr_del(const struct lu_env *env,
2452 struct dt_object *dt, const char *name,
2453 struct thandle *handle)
2455 struct osd_thandle *oh;
2457 LASSERT(dt_object_exists(dt));
2458 LASSERT(handle != NULL);
2460 oh = container_of0(handle, struct osd_thandle, ot_super);
2461 LASSERT(oh->ot_handle == NULL);
2463 OSD_DECLARE_OP(oh, xattr_set);
2464 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2470 * Concurrency: @dt is write locked.
2472 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2473 const char *name, struct thandle *handle,
2474 struct lustre_capa *capa)
2476 struct osd_object *obj = osd_dt_obj(dt);
2477 struct inode *inode = obj->oo_inode;
2478 struct osd_thread_info *info = osd_oti_get(env);
2479 struct dentry *dentry = &info->oti_obj_dentry;
2482 LASSERT(dt_object_exists(dt));
2483 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2484 LASSERT(osd_write_locked(env, obj));
2485 LASSERT(handle != NULL);
2487 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2490 OSD_EXEC_OP(handle, xattr_set);
2492 dentry->d_inode = inode;
2493 rc = inode->i_op->removexattr(dentry, name);
2497 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2498 struct dt_object *dt,
2499 struct lustre_capa *old,
2502 struct osd_thread_info *info = osd_oti_get(env);
2503 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2504 struct osd_object *obj = osd_dt_obj(dt);
2505 struct osd_device *dev = osd_obj2dev(obj);
2506 struct lustre_capa_key *key = &info->oti_capa_key;
2507 struct lustre_capa *capa = &info->oti_capa;
2508 struct obd_capa *oc;
2509 struct md_capainfo *ci;
2513 if (!dev->od_fl_capa)
2514 RETURN(ERR_PTR(-ENOENT));
2516 LASSERT(dt_object_exists(dt));
2517 LINVRNT(osd_invariant(obj));
2519 /* renewal sanity check */
2520 if (old && osd_object_auth(env, dt, old, opc))
2521 RETURN(ERR_PTR(-EACCES));
2523 ci = md_capainfo(env);
2525 RETURN(ERR_PTR(-ENOENT));
2527 switch (ci->mc_auth) {
2531 capa->lc_uid = obj->oo_inode->i_uid;
2532 capa->lc_gid = obj->oo_inode->i_gid;
2533 capa->lc_flags = LC_ID_PLAIN;
2535 case LC_ID_CONVERT: {
2538 s[0] = obj->oo_inode->i_uid;
2539 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2540 s[2] = obj->oo_inode->i_gid;
2541 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2542 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2544 RETURN(ERR_PTR(rc));
2546 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2547 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2548 capa->lc_flags = LC_ID_CONVERT;
2552 RETURN(ERR_PTR(-EINVAL));
2555 capa->lc_fid = *fid;
2557 capa->lc_flags |= dev->od_capa_alg << 24;
2558 capa->lc_timeout = dev->od_capa_timeout;
2559 capa->lc_expiry = 0;
2561 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2563 LASSERT(!capa_is_expired(oc));
2567 cfs_spin_lock(&capa_lock);
2568 *key = dev->od_capa_keys[1];
2569 cfs_spin_unlock(&capa_lock);
2571 capa->lc_keyid = key->lk_keyid;
2572 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2574 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2576 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2577 RETURN(ERR_PTR(rc));
2580 oc = capa_add(dev->od_capa_hash, capa);
2584 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2586 struct osd_object *obj = osd_dt_obj(dt);
2587 struct inode *inode = obj->oo_inode;
2588 struct osd_thread_info *info = osd_oti_get(env);
2589 struct dentry *dentry = &info->oti_obj_dentry;
2590 struct file *file = &info->oti_file;
2595 dentry->d_inode = inode;
2596 file->f_dentry = dentry;
2597 file->f_mapping = inode->i_mapping;
2598 file->f_op = inode->i_fop;
2599 mutex_lock(&inode->i_mutex);
2600 rc = file->f_op->fsync(file, dentry, 0);
2601 mutex_unlock(&inode->i_mutex);
2605 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2608 struct osd_object *obj = osd_dt_obj(dt);
2611 *data = (void *)obj->oo_inode;
2619 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2620 const struct dt_index_features *feat)
2622 struct iam_descr *descr;
2624 if (osd_object_is_root(o))
2625 return feat == &dt_directory_features;
2627 LASSERT(o->oo_dir != NULL);
2629 descr = o->oo_dir->od_container.ic_descr;
2630 if (feat == &dt_directory_features) {
2631 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2637 feat->dif_keysize_min <= descr->id_key_size &&
2638 descr->id_key_size <= feat->dif_keysize_max &&
2639 feat->dif_recsize_min <= descr->id_rec_size &&
2640 descr->id_rec_size <= feat->dif_recsize_max &&
2641 !(feat->dif_flags & (DT_IND_VARKEY |
2642 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2643 ergo(feat->dif_flags & DT_IND_UPDATE,
2644 1 /* XXX check that object (and file system) is
2649 static int osd_iam_container_init(const struct lu_env *env,
2650 struct osd_object *obj,
2651 struct osd_directory *dir)
2653 struct iam_container *bag = &dir->od_container;
2656 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2660 result = iam_container_setup(bag);
2662 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2664 iam_container_fini(bag);
2671 * Concurrency: no external locking is necessary.
2673 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2674 const struct dt_index_features *feat)
2678 struct osd_object *obj = osd_dt_obj(dt);
2679 struct osd_device *osd = osd_obj2dev(obj);
2681 LINVRNT(osd_invariant(obj));
2682 LASSERT(dt_object_exists(dt));
2684 if (osd_object_is_root(obj)) {
2685 dt->do_index_ops = &osd_index_ea_ops;
2687 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2688 dt->do_index_ops = &osd_index_ea_ops;
2689 if (S_ISDIR(obj->oo_inode->i_mode))
2694 } else if (unlikely(feat == &dt_otable_features)) {
2695 dt->do_index_ops = &osd_otable_ops;
2697 } else if (feat == &dt_acct_features) {
2698 dt->do_index_ops = &osd_acct_index_ops;
2701 } else if (!osd_has_index(obj)) {
2702 struct osd_directory *dir;
2707 cfs_spin_lock(&obj->oo_guard);
2708 if (obj->oo_dir == NULL)
2712 * Concurrent thread allocated container data.
2715 cfs_spin_unlock(&obj->oo_guard);
2717 * Now, that we have container data, serialize its
2720 cfs_down_write(&obj->oo_ext_idx_sem);
2722 * recheck under lock.
2724 if (!osd_has_index(obj))
2725 result = osd_iam_container_init(env, obj, dir);
2728 cfs_up_write(&obj->oo_ext_idx_sem);
2736 if (result == 0 && skip_iam == 0) {
2737 if (!osd_iam_index_probe(env, obj, feat))
2740 LINVRNT(osd_invariant(obj));
2742 if (is_quota_glb_feat(feat))
2743 result = osd_quota_migration(env, dt, feat);
2748 static int osd_otable_it_attr_get(const struct lu_env *env,
2749 struct dt_object *dt,
2750 struct lu_attr *attr,
2751 struct lustre_capa *capa)
2757 static const struct dt_object_operations osd_obj_ops = {
2758 .do_read_lock = osd_object_read_lock,
2759 .do_write_lock = osd_object_write_lock,
2760 .do_read_unlock = osd_object_read_unlock,
2761 .do_write_unlock = osd_object_write_unlock,
2762 .do_write_locked = osd_object_write_locked,
2763 .do_attr_get = osd_attr_get,
2764 .do_declare_attr_set = osd_declare_attr_set,
2765 .do_attr_set = osd_attr_set,
2766 .do_ah_init = osd_ah_init,
2767 .do_declare_create = osd_declare_object_create,
2768 .do_create = osd_object_create,
2769 .do_declare_destroy = osd_declare_object_destroy,
2770 .do_destroy = osd_object_destroy,
2771 .do_index_try = osd_index_try,
2772 .do_declare_ref_add = osd_declare_object_ref_add,
2773 .do_ref_add = osd_object_ref_add,
2774 .do_declare_ref_del = osd_declare_object_ref_del,
2775 .do_ref_del = osd_object_ref_del,
2776 .do_xattr_get = osd_xattr_get,
2777 .do_declare_xattr_set = osd_declare_xattr_set,
2778 .do_xattr_set = osd_xattr_set,
2779 .do_declare_xattr_del = osd_declare_xattr_del,
2780 .do_xattr_del = osd_xattr_del,
2781 .do_xattr_list = osd_xattr_list,
2782 .do_capa_get = osd_capa_get,
2783 .do_object_sync = osd_object_sync,
2784 .do_data_get = osd_data_get,
2788 * dt_object_operations for interoperability mode
2789 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2791 static const struct dt_object_operations osd_obj_ea_ops = {
2792 .do_read_lock = osd_object_read_lock,
2793 .do_write_lock = osd_object_write_lock,
2794 .do_read_unlock = osd_object_read_unlock,
2795 .do_write_unlock = osd_object_write_unlock,
2796 .do_write_locked = osd_object_write_locked,
2797 .do_attr_get = osd_attr_get,
2798 .do_declare_attr_set = osd_declare_attr_set,
2799 .do_attr_set = osd_attr_set,
2800 .do_ah_init = osd_ah_init,
2801 .do_declare_create = osd_declare_object_create,
2802 .do_create = osd_object_ea_create,
2803 .do_declare_destroy = osd_declare_object_destroy,
2804 .do_destroy = osd_object_destroy,
2805 .do_index_try = osd_index_try,
2806 .do_declare_ref_add = osd_declare_object_ref_add,
2807 .do_ref_add = osd_object_ref_add,
2808 .do_declare_ref_del = osd_declare_object_ref_del,
2809 .do_ref_del = osd_object_ref_del,
2810 .do_xattr_get = osd_xattr_get,
2811 .do_declare_xattr_set = osd_declare_xattr_set,
2812 .do_xattr_set = osd_xattr_set,
2813 .do_declare_xattr_del = osd_declare_xattr_del,
2814 .do_xattr_del = osd_xattr_del,
2815 .do_xattr_list = osd_xattr_list,
2816 .do_capa_get = osd_capa_get,
2817 .do_object_sync = osd_object_sync,
2818 .do_data_get = osd_data_get,
2821 static const struct dt_object_operations osd_obj_otable_it_ops = {
2822 .do_attr_get = osd_otable_it_attr_get,
2823 .do_index_try = osd_index_try,
2826 static int osd_index_declare_iam_delete(const struct lu_env *env,
2827 struct dt_object *dt,
2828 const struct dt_key *key,
2829 struct thandle *handle)
2831 struct osd_thandle *oh;
2833 oh = container_of0(handle, struct osd_thandle, ot_super);
2834 LASSERT(oh->ot_handle == NULL);
2836 OSD_DECLARE_OP(oh, delete);
2837 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2843 * delete a (key, value) pair from index \a dt specified by \a key
2845 * \param dt osd index object
2846 * \param key key for index
2847 * \param rec record reference
2848 * \param handle transaction handler
2851 * \retval -ve failure
2854 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2855 const struct dt_key *key,
2856 struct thandle *handle,
2857 struct lustre_capa *capa)
2859 struct osd_thread_info *oti = osd_oti_get(env);
2860 struct osd_object *obj = osd_dt_obj(dt);
2861 struct osd_thandle *oh;
2862 struct iam_path_descr *ipd;
2863 struct iam_container *bag = &obj->oo_dir->od_container;
2868 LINVRNT(osd_invariant(obj));
2869 LASSERT(dt_object_exists(dt));
2870 LASSERT(bag->ic_object == obj->oo_inode);
2871 LASSERT(handle != NULL);
2873 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2876 OSD_EXEC_OP(handle, delete);
2878 ipd = osd_idx_ipd_get(env, bag);
2879 if (unlikely(ipd == NULL))
2882 oh = container_of0(handle, struct osd_thandle, ot_super);
2883 LASSERT(oh->ot_handle != NULL);
2884 LASSERT(oh->ot_handle->h_transaction != NULL);
2886 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2887 /* swab quota uid/gid provided by caller */
2888 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2889 key = (const struct dt_key *)&oti->oti_quota_id;
2892 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2893 osd_ipd_put(env, bag, ipd);
2894 LINVRNT(osd_invariant(obj));
2898 static int osd_index_declare_ea_delete(const struct lu_env *env,
2899 struct dt_object *dt,
2900 const struct dt_key *key,
2901 struct thandle *handle)
2903 struct osd_thandle *oh;
2904 struct inode *inode;
2908 LASSERT(dt_object_exists(dt));
2909 LASSERT(handle != NULL);
2911 oh = container_of0(handle, struct osd_thandle, ot_super);
2912 LASSERT(oh->ot_handle == NULL);
2914 OSD_DECLARE_OP(oh, delete);
2915 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2917 inode = osd_dt_obj(dt)->oo_inode;
2920 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2921 true, true, NULL, false);
2925 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2928 struct osd_fid_pack *rec;
2931 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2932 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2933 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2939 * Index delete function for interoperability mode (b11826).
2940 * It will remove the directory entry added by osd_index_ea_insert().
2941 * This entry is needed to maintain name->fid mapping.
2943 * \param key, key i.e. file entry to be deleted
2945 * \retval 0, on success
2946 * \retval -ve, on error
2948 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2949 const struct dt_key *key,
2950 struct thandle *handle,
2951 struct lustre_capa *capa)
2953 struct osd_object *obj = osd_dt_obj(dt);
2954 struct inode *dir = obj->oo_inode;
2955 struct dentry *dentry;
2956 struct osd_thandle *oh;
2957 struct ldiskfs_dir_entry_2 *de;
2958 struct buffer_head *bh;
2959 struct htree_lock *hlock = NULL;
2964 LINVRNT(osd_invariant(obj));
2965 LASSERT(dt_object_exists(dt));
2966 LASSERT(handle != NULL);
2968 OSD_EXEC_OP(handle, delete);
2970 oh = container_of(handle, struct osd_thandle, ot_super);
2971 LASSERT(oh->ot_handle != NULL);
2972 LASSERT(oh->ot_handle->h_transaction != NULL);
2974 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2977 dentry = osd_child_dentry_get(env, obj,
2978 (char *)key, strlen((char *)key));
2980 if (obj->oo_hl_head != NULL) {
2981 hlock = osd_oti_get(env)->oti_hlock;
2982 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2983 dir, LDISKFS_HLOCK_DEL);
2985 cfs_down_write(&obj->oo_ext_idx_sem);
2988 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2990 rc = ldiskfs_delete_entry(oh->ot_handle,
2997 ldiskfs_htree_unlock(hlock);
2999 cfs_up_write(&obj->oo_ext_idx_sem);
3001 LASSERT(osd_invariant(obj));
3006 * Lookup index for \a key and copy record to \a rec.
3008 * \param dt osd index object
3009 * \param key key for index
3010 * \param rec record reference
3012 * \retval +ve success : exact mach
3013 * \retval 0 return record with key not greater than \a key
3014 * \retval -ve failure
3016 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3017 struct dt_rec *rec, const struct dt_key *key,
3018 struct lustre_capa *capa)
3020 struct osd_object *obj = osd_dt_obj(dt);
3021 struct iam_path_descr *ipd;
3022 struct iam_container *bag = &obj->oo_dir->od_container;
3023 struct osd_thread_info *oti = osd_oti_get(env);
3024 struct iam_iterator *it = &oti->oti_idx_it;
3025 struct iam_rec *iam_rec;
3030 LASSERT(osd_invariant(obj));
3031 LASSERT(dt_object_exists(dt));
3032 LASSERT(bag->ic_object == obj->oo_inode);
3034 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3037 ipd = osd_idx_ipd_get(env, bag);
3041 /* got ipd now we can start iterator. */
3042 iam_it_init(it, bag, 0, ipd);
3044 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3045 /* swab quota uid/gid provided by caller */
3046 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3047 key = (const struct dt_key *)&oti->oti_quota_id;
3050 rc = iam_it_get(it, (struct iam_key *)key);
3052 if (S_ISDIR(obj->oo_inode->i_mode))
3053 iam_rec = (struct iam_rec *)oti->oti_ldp;
3055 iam_rec = (struct iam_rec *) rec;
3057 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3059 if (S_ISDIR(obj->oo_inode->i_mode))
3060 osd_fid_unpack((struct lu_fid *) rec,
3061 (struct osd_fid_pack *)iam_rec);
3062 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3063 osd_quota_unpack(obj, rec);
3068 osd_ipd_put(env, bag, ipd);
3070 LINVRNT(osd_invariant(obj));
3075 static int osd_index_declare_iam_insert(const struct lu_env *env,
3076 struct dt_object *dt,
3077 const struct dt_rec *rec,
3078 const struct dt_key *key,
3079 struct thandle *handle)
3081 struct osd_thandle *oh;
3083 LASSERT(dt_object_exists(dt));
3084 LASSERT(handle != NULL);
3086 oh = container_of0(handle, struct osd_thandle, ot_super);
3087 LASSERT(oh->ot_handle == NULL);
3089 OSD_DECLARE_OP(oh, insert);
3090 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3096 * Inserts (key, value) pair in \a dt index object.
3098 * \param dt osd index object
3099 * \param key key for index
3100 * \param rec record reference
3101 * \param th transaction handler
3104 * \retval -ve failure
3106 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3107 const struct dt_rec *rec,
3108 const struct dt_key *key, struct thandle *th,
3109 struct lustre_capa *capa, int ignore_quota)
3111 struct osd_object *obj = osd_dt_obj(dt);
3112 struct iam_path_descr *ipd;
3113 struct osd_thandle *oh;
3114 struct iam_container *bag = &obj->oo_dir->od_container;
3115 struct osd_thread_info *oti = osd_oti_get(env);
3116 struct iam_rec *iam_rec;
3121 LINVRNT(osd_invariant(obj));
3122 LASSERT(dt_object_exists(dt));
3123 LASSERT(bag->ic_object == obj->oo_inode);
3124 LASSERT(th != NULL);
3126 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3129 OSD_EXEC_OP(th, insert);
3131 ipd = osd_idx_ipd_get(env, bag);
3132 if (unlikely(ipd == NULL))
3135 oh = container_of0(th, struct osd_thandle, ot_super);
3136 LASSERT(oh->ot_handle != NULL);
3137 LASSERT(oh->ot_handle->h_transaction != NULL);
3138 if (S_ISDIR(obj->oo_inode->i_mode)) {
3139 iam_rec = (struct iam_rec *)oti->oti_ldp;
3140 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3141 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3142 /* pack quota uid/gid */
3143 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3144 key = (const struct dt_key *)&oti->oti_quota_id;
3145 /* pack quota record */
3146 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3147 iam_rec = (struct iam_rec *)rec;
3149 iam_rec = (struct iam_rec *)rec;
3152 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3154 osd_ipd_put(env, bag, ipd);
3155 LINVRNT(osd_invariant(obj));
3160 * Calls ldiskfs_add_entry() to add directory entry
3161 * into the directory. This is required for
3162 * interoperability mode (b11826)
3164 * \retval 0, on success
3165 * \retval -ve, on error
3167 static int __osd_ea_add_rec(struct osd_thread_info *info,
3168 struct osd_object *pobj, struct inode *cinode,
3169 const char *name, const struct dt_rec *fid,
3170 struct htree_lock *hlock, struct thandle *th)
3172 struct ldiskfs_dentry_param *ldp;
3173 struct dentry *child;
3174 struct osd_thandle *oth;
3177 oth = container_of(th, struct osd_thandle, ot_super);
3178 LASSERT(oth->ot_handle != NULL);
3179 LASSERT(oth->ot_handle->h_transaction != NULL);
3181 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3183 /* XXX: remove fid_is_igif() check here.
3184 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3185 * it is IGIF now but needs FID in dir entry as well for readdir
3187 * LU-838 should fix that and remove fid_is_igif() check */
3188 if (fid_is_igif((struct lu_fid *)fid) ||
3189 fid_is_norm((struct lu_fid *)fid)) {
3190 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3191 osd_get_ldiskfs_dirent_param(ldp, fid);
3192 child->d_fsdata = (void *)ldp;
3194 child->d_fsdata = NULL;
3196 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3202 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3203 * into the directory.Also sets flags into osd object to
3204 * indicate dot and dotdot are created. This is required for
3205 * interoperability mode (b11826)
3207 * \param dir directory for dot and dotdot fixup.
3208 * \param obj child object for linking
3210 * \retval 0, on success
3211 * \retval -ve, on error
3213 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3214 struct osd_object *dir,
3215 struct inode *parent_dir, const char *name,
3216 const struct dt_rec *dot_fid,
3217 const struct dt_rec *dot_dot_fid,
3220 struct inode *inode = dir->oo_inode;
3221 struct ldiskfs_dentry_param *dot_ldp;
3222 struct ldiskfs_dentry_param *dot_dot_ldp;
3223 struct osd_thandle *oth;
3226 oth = container_of(th, struct osd_thandle, ot_super);
3227 LASSERT(oth->ot_handle->h_transaction != NULL);
3228 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3230 if (strcmp(name, dot) == 0) {
3231 if (dir->oo_compat_dot_created) {
3234 LASSERT(inode == parent_dir);
3235 dir->oo_compat_dot_created = 1;
3238 } else if(strcmp(name, dotdot) == 0) {
3239 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3240 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3242 if (!dir->oo_compat_dot_created)
3244 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3245 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3246 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3251 /* in case of rename, dotdot is already created */
3252 if (dir->oo_compat_dotdot_created) {
3253 return __osd_ea_add_rec(info, dir, parent_dir, name,
3254 dot_dot_fid, NULL, th);
3257 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3258 inode, dot_ldp, dot_dot_ldp);
3260 dir->oo_compat_dotdot_created = 1;
3268 * It will call the appropriate osd_add* function and return the
3269 * value, return by respective functions.
3271 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3272 struct inode *cinode, const char *name,
3273 const struct dt_rec *fid, struct thandle *th)
3275 struct osd_thread_info *info = osd_oti_get(env);
3276 struct htree_lock *hlock;
3279 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3281 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3283 if (hlock != NULL) {
3284 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3287 cfs_down_write(&pobj->oo_ext_idx_sem);
3289 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3290 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3293 if (hlock != NULL) {
3294 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3295 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3297 cfs_down_write(&pobj->oo_ext_idx_sem);
3300 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3304 ldiskfs_htree_unlock(hlock);
3306 cfs_up_write(&pobj->oo_ext_idx_sem);
3312 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3313 struct osd_idmap_cache *oic)
3315 struct osd_scrub *scrub = &dev->od_scrub;
3316 struct lu_fid *fid = &oic->oic_fid;
3317 struct osd_inode_id *id = &oti->oti_id;
3323 rc = osd_oi_lookup(oti, dev, fid, id);
3324 if (rc != 0 && rc != -ENOENT)
3327 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3330 if (thread_is_running(&scrub->os_thread)) {
3331 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3332 /* There is race condition between osd_oi_lookup and OI scrub.
3333 * The OI scrub finished just after osd_oi_lookup() failure.
3334 * Under such case, it is unnecessary to trigger OI scrub again,
3335 * but try to call osd_oi_lookup() again. */
3336 if (unlikely(rc == -EAGAIN))
3342 if (!scrub->os_no_scrub && ++once == 1) {
3343 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3345 rc = osd_scrub_start(dev);
3346 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3348 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3354 RETURN(rc = -EREMCHG);
3358 * Calls ->lookup() to find dentry. From dentry get inode and
3359 * read inode's ea to get fid. This is required for interoperability
3362 * \retval 0, on success
3363 * \retval -ve, on error
3365 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3366 struct dt_rec *rec, const struct dt_key *key)
3368 struct inode *dir = obj->oo_inode;
3369 struct dentry *dentry;
3370 struct ldiskfs_dir_entry_2 *de;
3371 struct buffer_head *bh;
3372 struct lu_fid *fid = (struct lu_fid *) rec;
3373 struct htree_lock *hlock = NULL;
3377 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3379 dentry = osd_child_dentry_get(env, obj,
3380 (char *)key, strlen((char *)key));
3382 if (obj->oo_hl_head != NULL) {
3383 hlock = osd_oti_get(env)->oti_hlock;
3384 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3385 dir, LDISKFS_HLOCK_LOOKUP);
3387 cfs_down_read(&obj->oo_ext_idx_sem);
3390 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3392 struct osd_thread_info *oti = osd_oti_get(env);
3393 struct osd_idmap_cache *oic = &oti->oti_cache;
3394 struct osd_device *dev = osd_obj2dev(obj);
3395 struct osd_scrub *scrub = &dev->od_scrub;
3396 struct scrub_file *sf = &scrub->os_file;
3398 ino = le32_to_cpu(de->inode);
3399 rc = osd_get_fid_from_dentry(de, rec);
3401 /* done with de, release bh */
3404 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3406 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3407 if (rc != 0 || !fid_is_norm(fid)) {
3408 fid_zero(&oic->oic_fid);
3412 oic->oic_fid = *fid;
3413 if ((scrub->os_pos_current <= ino) &&
3414 (sf->sf_flags & SF_INCONSISTENT ||
3415 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3417 rc = osd_consistency_check(oti, dev, oic);
3426 ldiskfs_htree_unlock(hlock);
3428 cfs_up_read(&obj->oo_ext_idx_sem);
3433 * Find the osd object for given fid.
3435 * \param fid need to find the osd object having this fid
3437 * \retval osd_object on success
3438 * \retval -ve on error
3440 struct osd_object *osd_object_find(const struct lu_env *env,
3441 struct dt_object *dt,
3442 const struct lu_fid *fid)
3444 struct lu_device *ludev = dt->do_lu.lo_dev;
3445 struct osd_object *child = NULL;
3446 struct lu_object *luch;
3447 struct lu_object *lo;
3450 * at this point topdev might not exist yet
3451 * (i.e. MGS is preparing profiles). so we can
3452 * not rely on topdev and instead lookup with
3453 * our device passed as topdev. this can't work
3454 * if the object isn't cached yet (as osd doesn't
3455 * allocate lu_header). IOW, the object must be
3456 * in the cache, otherwise lu_object_alloc() crashes
3459 luch = lu_object_find_at(env, ludev, fid, NULL);
3460 if (!IS_ERR(luch)) {
3461 if (lu_object_exists(luch)) {
3462 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3464 child = osd_obj(lo);
3466 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3467 "lu_object can't be located"
3468 DFID"\n", PFID(fid));
3470 if (child == NULL) {
3471 lu_object_put(env, luch);
3472 CERROR("Unable to get osd_object\n");
3473 child = ERR_PTR(-ENOENT);
3476 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3477 "lu_object does not exists "DFID"\n",
3479 lu_object_put(env, luch);
3480 child = ERR_PTR(-ENOENT);
3483 child = (void *)luch;
3489 * Put the osd object once done with it.
3491 * \param obj osd object that needs to be put
3493 static inline void osd_object_put(const struct lu_env *env,
3494 struct osd_object *obj)
3496 lu_object_put(env, &obj->oo_dt.do_lu);
3499 static int osd_index_declare_ea_insert(const struct lu_env *env,
3500 struct dt_object *dt,
3501 const struct dt_rec *rec,
3502 const struct dt_key *key,
3503 struct thandle *handle)
3505 struct osd_thandle *oh;
3506 struct inode *inode;
3510 LASSERT(dt_object_exists(dt));
3511 LASSERT(handle != NULL);
3513 oh = container_of0(handle, struct osd_thandle, ot_super);
3514 LASSERT(oh->ot_handle == NULL);
3516 OSD_DECLARE_OP(oh, insert);
3517 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3519 inode = osd_dt_obj(dt)->oo_inode;
3522 /* We ignore block quota on meta pool (MDTs), so needn't
3523 * calculate how many blocks will be consumed by this index
3525 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3526 true, true, NULL, false);
3531 * Index add function for interoperability mode (b11826).
3532 * It will add the directory entry.This entry is needed to
3533 * maintain name->fid mapping.
3535 * \param key it is key i.e. file entry to be inserted
3536 * \param rec it is value of given key i.e. fid
3538 * \retval 0, on success
3539 * \retval -ve, on error
3541 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3542 const struct dt_rec *rec,
3543 const struct dt_key *key, struct thandle *th,
3544 struct lustre_capa *capa, int ignore_quota)
3546 struct osd_object *obj = osd_dt_obj(dt);
3547 struct lu_fid *fid = (struct lu_fid *) rec;
3548 const char *name = (const char *)key;
3549 struct osd_object *child;
3554 LASSERT(osd_invariant(obj));
3555 LASSERT(dt_object_exists(dt));
3556 LASSERT(th != NULL);
3558 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3561 child = osd_object_find(env, dt, fid);
3562 if (!IS_ERR(child)) {
3563 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3564 osd_object_put(env, child);
3566 rc = PTR_ERR(child);
3569 LASSERT(osd_invariant(obj));
3574 * Initialize osd Iterator for given osd index object.
3576 * \param dt osd index object
3579 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3580 struct dt_object *dt,
3582 struct lustre_capa *capa)
3584 struct osd_it_iam *it;
3585 struct osd_thread_info *oti = osd_oti_get(env);
3586 struct osd_object *obj = osd_dt_obj(dt);
3587 struct lu_object *lo = &dt->do_lu;
3588 struct iam_path_descr *ipd;
3589 struct iam_container *bag = &obj->oo_dir->od_container;
3591 LASSERT(lu_object_exists(lo));
3593 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3594 return ERR_PTR(-EACCES);
3597 ipd = osd_it_ipd_get(env, bag);
3598 if (likely(ipd != NULL)) {
3602 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3603 return (struct dt_it *)it;
3605 return ERR_PTR(-ENOMEM);
3609 * free given Iterator.
3612 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3614 struct osd_it_iam *it = (struct osd_it_iam *)di;
3615 struct osd_object *obj = it->oi_obj;
3617 iam_it_fini(&it->oi_it);
3618 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3619 lu_object_put(env, &obj->oo_dt.do_lu);
3623 * Move Iterator to record specified by \a key
3625 * \param di osd iterator
3626 * \param key key for index
3628 * \retval +ve di points to record with least key not larger than key
3629 * \retval 0 di points to exact matched key
3630 * \retval -ve failure
3633 static int osd_it_iam_get(const struct lu_env *env,
3634 struct dt_it *di, const struct dt_key *key)
3636 struct osd_thread_info *oti = osd_oti_get(env);
3637 struct osd_it_iam *it = (struct osd_it_iam *)di;
3639 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3640 /* swab quota uid/gid */
3641 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3642 key = (struct dt_key *)&oti->oti_quota_id;
3645 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3651 * \param di osd iterator
3654 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3656 struct osd_it_iam *it = (struct osd_it_iam *)di;
3658 iam_it_put(&it->oi_it);
3662 * Move iterator by one record
3664 * \param di osd iterator
3666 * \retval +1 end of container reached
3668 * \retval -ve failure
3671 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3673 struct osd_it_iam *it = (struct osd_it_iam *)di;
3675 return iam_it_next(&it->oi_it);
3679 * Return pointer to the key under iterator.
3682 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3683 const struct dt_it *di)
3685 struct osd_thread_info *oti = osd_oti_get(env);
3686 struct osd_it_iam *it = (struct osd_it_iam *)di;
3687 struct osd_object *obj = it->oi_obj;
3690 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3692 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3693 /* swab quota uid/gid */
3694 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3695 key = (struct dt_key *)&oti->oti_quota_id;
3702 * Return size of key under iterator (in bytes)
3705 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3707 struct osd_it_iam *it = (struct osd_it_iam *)di;
3709 return iam_it_key_size(&it->oi_it);
3712 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3713 int len, __u16 type)
3715 struct luda_type *lt;
3716 const unsigned align = sizeof(struct luda_type) - 1;
3718 /* check if file type is required */
3719 if (attr & LUDA_TYPE) {
3720 len = (len + align) & ~align;
3722 lt = (void *) ent->lde_name + len;
3723 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3724 ent->lde_attrs |= LUDA_TYPE;
3727 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3731 * build lu direct from backend fs dirent.
3734 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3735 struct lu_fid *fid, __u64 offset,
3736 char *name, __u16 namelen,
3737 __u16 type, __u32 attr)
3739 fid_cpu_to_le(&ent->lde_fid, fid);
3740 ent->lde_attrs = LUDA_FID;
3742 ent->lde_hash = cpu_to_le64(offset);
3743 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3745 strncpy(ent->lde_name, name, namelen);
3746 ent->lde_namelen = cpu_to_le16(namelen);
3748 /* append lustre attributes */
3749 osd_it_append_attrs(ent, attr, namelen, type);
3753 * Return pointer to the record under iterator.
3755 static int osd_it_iam_rec(const struct lu_env *env,
3756 const struct dt_it *di,
3757 struct dt_rec *dtrec, __u32 attr)
3759 struct osd_it_iam *it = (struct osd_it_iam *)di;
3760 struct osd_thread_info *info = osd_oti_get(env);
3763 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3764 const struct osd_fid_pack *rec;
3765 struct lu_fid *fid = &info->oti_fid;
3766 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3772 name = (char *)iam_it_key_get(&it->oi_it);
3774 RETURN(PTR_ERR(name));
3776 namelen = iam_it_key_size(&it->oi_it);
3778 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3780 RETURN(PTR_ERR(rec));
3782 rc = osd_fid_unpack(fid, rec);
3786 hash = iam_it_store(&it->oi_it);
3788 /* IAM does not store object type in IAM index (dir) */
3789 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3791 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3792 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3793 (struct iam_rec *)dtrec);
3794 osd_quota_unpack(it->oi_obj, dtrec);
3796 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3797 (struct iam_rec *)dtrec);
3804 * Returns cookie for current Iterator position.
3806 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3808 struct osd_it_iam *it = (struct osd_it_iam *)di;
3810 return iam_it_store(&it->oi_it);
3814 * Restore iterator from cookie.
3816 * \param di osd iterator
3817 * \param hash Iterator location cookie
3819 * \retval +ve di points to record with least key not larger than key.
3820 * \retval 0 di points to exact matched key
3821 * \retval -ve failure
3824 static int osd_it_iam_load(const struct lu_env *env,
3825 const struct dt_it *di, __u64 hash)
3827 struct osd_it_iam *it = (struct osd_it_iam *)di;
3829 return iam_it_load(&it->oi_it, hash);
3832 static const struct dt_index_operations osd_index_iam_ops = {
3833 .dio_lookup = osd_index_iam_lookup,
3834 .dio_declare_insert = osd_index_declare_iam_insert,
3835 .dio_insert = osd_index_iam_insert,
3836 .dio_declare_delete = osd_index_declare_iam_delete,
3837 .dio_delete = osd_index_iam_delete,
3839 .init = osd_it_iam_init,
3840 .fini = osd_it_iam_fini,
3841 .get = osd_it_iam_get,
3842 .put = osd_it_iam_put,
3843 .next = osd_it_iam_next,
3844 .key = osd_it_iam_key,
3845 .key_size = osd_it_iam_key_size,
3846 .rec = osd_it_iam_rec,
3847 .store = osd_it_iam_store,
3848 .load = osd_it_iam_load
3853 * Creates or initializes iterator context.
3855 * \retval struct osd_it_ea, iterator structure on success
3858 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3859 struct dt_object *dt,
3861 struct lustre_capa *capa)
3863 struct osd_object *obj = osd_dt_obj(dt);
3864 struct osd_thread_info *info = osd_oti_get(env);
3865 struct osd_it_ea *it = &info->oti_it_ea;
3866 struct lu_object *lo = &dt->do_lu;
3867 struct dentry *obj_dentry = &info->oti_it_dentry;
3869 LASSERT(lu_object_exists(lo));
3871 obj_dentry->d_inode = obj->oo_inode;
3872 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3873 obj_dentry->d_name.hash = 0;
3875 it->oie_rd_dirent = 0;
3876 it->oie_it_dirent = 0;
3877 it->oie_dirent = NULL;
3878 it->oie_buf = info->oti_it_ea_buf;
3880 it->oie_file.f_pos = 0;
3881 it->oie_file.f_dentry = obj_dentry;
3882 if (attr & LUDA_64BITHASH)
3883 it->oie_file.f_mode |= FMODE_64BITHASH;
3885 it->oie_file.f_mode |= FMODE_32BITHASH;
3886 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3887 it->oie_file.f_op = obj->oo_inode->i_fop;
3888 it->oie_file.private_data = NULL;
3890 RETURN((struct dt_it *) it);
3894 * Destroy or finishes iterator context.
3896 * \param di iterator structure to be destroyed
3898 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3900 struct osd_it_ea *it = (struct osd_it_ea *)di;
3901 struct osd_object *obj = it->oie_obj;
3902 struct inode *inode = obj->oo_inode;
3905 it->oie_file.f_op->release(inode, &it->oie_file);
3906 lu_object_put(env, &obj->oo_dt.do_lu);
3911 * It position the iterator at given key, so that next lookup continues from
3912 * that key Or it is similar to dio_it->load() but based on a key,
3913 * rather than file position.
3915 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3918 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3920 static int osd_it_ea_get(const struct lu_env *env,
3921 struct dt_it *di, const struct dt_key *key)
3923 struct osd_it_ea *it = (struct osd_it_ea *)di;
3926 LASSERT(((const char *)key)[0] == '\0');
3927 it->oie_file.f_pos = 0;
3928 it->oie_rd_dirent = 0;
3929 it->oie_it_dirent = 0;
3930 it->oie_dirent = NULL;
3938 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3943 * It is called internally by ->readdir(). It fills the
3944 * iterator's in-memory data structure with required
3945 * information i.e. name, namelen, rec_size etc.
3947 * \param buf in which information to be filled in.
3948 * \param name name of the file in given dir
3950 * \retval 0 on success
3951 * \retval 1 on buffer full
3953 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3954 loff_t offset, __u64 ino,
3957 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3958 struct osd_it_ea_dirent *ent = it->oie_dirent;
3959 struct lu_fid *fid = &ent->oied_fid;
3960 struct osd_fid_pack *rec;
3963 /* this should never happen */
3964 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3965 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3969 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3973 if (d_type & LDISKFS_DIRENT_LUFID) {
3974 rec = (struct osd_fid_pack*) (name + namelen + 1);
3976 if (osd_fid_unpack(fid, rec) != 0)
3979 d_type &= ~LDISKFS_DIRENT_LUFID;
3984 ent->oied_ino = ino;
3985 ent->oied_off = offset;
3986 ent->oied_namelen = namelen;
3987 ent->oied_type = d_type;
3989 memcpy(ent->oied_name, name, namelen);
3991 it->oie_rd_dirent++;
3992 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3997 * Calls ->readdir() to load a directory entry at a time
3998 * and stored it in iterator's in-memory data structure.
4000 * \param di iterator's in memory structure
4002 * \retval 0 on success
4003 * \retval -ve on error
4005 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4006 const struct dt_it *di)
4008 struct osd_it_ea *it = (struct osd_it_ea *)di;
4009 struct osd_object *obj = it->oie_obj;
4010 struct inode *inode = obj->oo_inode;
4011 struct htree_lock *hlock = NULL;
4015 it->oie_dirent = it->oie_buf;
4016 it->oie_rd_dirent = 0;
4018 if (obj->oo_hl_head != NULL) {
4019 hlock = osd_oti_get(env)->oti_hlock;
4020 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4021 inode, LDISKFS_HLOCK_READDIR);
4023 cfs_down_read(&obj->oo_ext_idx_sem);
4026 result = inode->i_fop->readdir(&it->oie_file, it,
4027 (filldir_t) osd_ldiskfs_filldir);
4030 ldiskfs_htree_unlock(hlock);
4032 cfs_up_read(&obj->oo_ext_idx_sem);
4034 if (it->oie_rd_dirent == 0) {
4037 it->oie_dirent = it->oie_buf;
4038 it->oie_it_dirent = 1;
4045 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4046 * to load a directory entry at a time and stored it in
4047 * iterator's in-memory data structure.
4049 * \param di iterator's in memory structure
4051 * \retval +ve iterator reached to end
4052 * \retval 0 iterator not reached to end
4053 * \retval -ve on error
4055 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4057 struct osd_it_ea *it = (struct osd_it_ea *)di;
4062 if (it->oie_it_dirent < it->oie_rd_dirent) {
4064 (void *) it->oie_dirent +
4065 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4066 it->oie_dirent->oied_namelen);
4067 it->oie_it_dirent++;
4070 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4073 rc = osd_ldiskfs_it_fill(env, di);
4080 * Returns the key at current position from iterator's in memory structure.
4082 * \param di iterator's in memory structure
4084 * \retval key i.e. struct dt_key on success
4086 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4087 const struct dt_it *di)
4089 struct osd_it_ea *it = (struct osd_it_ea *)di;
4091 return (struct dt_key *)it->oie_dirent->oied_name;
4095 * Returns the key's size at current position from iterator's in memory structure.
4097 * \param di iterator's in memory structure
4099 * \retval key_size i.e. struct dt_key on success
4101 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4103 struct osd_it_ea *it = (struct osd_it_ea *)di;
4105 return it->oie_dirent->oied_namelen;
4110 * Returns the value (i.e. fid/igif) at current position from iterator's
4111 * in memory structure.
4113 * \param di struct osd_it_ea, iterator's in memory structure
4114 * \param attr attr requested for dirent.
4115 * \param lde lustre dirent
4117 * \retval 0 no error and \param lde has correct lustre dirent.
4118 * \retval -ve on error
4120 static inline int osd_it_ea_rec(const struct lu_env *env,
4121 const struct dt_it *di,
4122 struct dt_rec *dtrec, __u32 attr)
4124 struct osd_it_ea *it = (struct osd_it_ea *)di;
4125 struct osd_object *obj = it->oie_obj;
4126 struct osd_device *dev = osd_obj2dev(obj);
4127 struct osd_scrub *scrub = &dev->od_scrub;
4128 struct scrub_file *sf = &scrub->os_file;
4129 struct osd_thread_info *oti = osd_oti_get(env);
4130 struct osd_idmap_cache *oic = &oti->oti_cache;
4131 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4132 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4133 __u32 ino = it->oie_dirent->oied_ino;
4137 if (!fid_is_sane(fid)) {
4138 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4142 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4145 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4146 it->oie_dirent->oied_name,
4147 it->oie_dirent->oied_namelen,
4148 it->oie_dirent->oied_type, attr);
4149 if (!fid_is_norm(fid)) {
4150 fid_zero(&oic->oic_fid);
4154 oic->oic_fid = *fid;
4155 if ((scrub->os_pos_current <= ino) &&
4156 (sf->sf_flags & SF_INCONSISTENT ||
4157 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4158 rc = osd_consistency_check(oti, dev, oic);
4164 * Returns a cookie for current position of the iterator head, so that
4165 * user can use this cookie to load/start the iterator next time.
4167 * \param di iterator's in memory structure
4169 * \retval cookie for current position, on success
4171 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4173 struct osd_it_ea *it = (struct osd_it_ea *)di;
4175 return it->oie_dirent->oied_off;
4179 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4180 * to load a directory entry at a time and stored it i inn,
4181 * in iterator's in-memory data structure.
4183 * \param di struct osd_it_ea, iterator's in memory structure
4185 * \retval +ve on success
4186 * \retval -ve on error
4188 static int osd_it_ea_load(const struct lu_env *env,
4189 const struct dt_it *di, __u64 hash)
4191 struct osd_it_ea *it = (struct osd_it_ea *)di;
4195 it->oie_file.f_pos = hash;
4197 rc = osd_ldiskfs_it_fill(env, di);
4205 * Index lookup function for interoperability mode (b11826).
4207 * \param key, key i.e. file name to be searched
4209 * \retval +ve, on success
4210 * \retval -ve, on error
4212 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4213 struct dt_rec *rec, const struct dt_key *key,
4214 struct lustre_capa *capa)
4216 struct osd_object *obj = osd_dt_obj(dt);
4221 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4222 LINVRNT(osd_invariant(obj));
4224 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4227 rc = osd_ea_lookup_rec(env, obj, rec, key);
4235 * Index and Iterator operations for interoperability
4236 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4238 static const struct dt_index_operations osd_index_ea_ops = {
4239 .dio_lookup = osd_index_ea_lookup,
4240 .dio_declare_insert = osd_index_declare_ea_insert,
4241 .dio_insert = osd_index_ea_insert,
4242 .dio_declare_delete = osd_index_declare_ea_delete,
4243 .dio_delete = osd_index_ea_delete,
4245 .init = osd_it_ea_init,
4246 .fini = osd_it_ea_fini,
4247 .get = osd_it_ea_get,
4248 .put = osd_it_ea_put,
4249 .next = osd_it_ea_next,
4250 .key = osd_it_ea_key,
4251 .key_size = osd_it_ea_key_size,
4252 .rec = osd_it_ea_rec,
4253 .store = osd_it_ea_store,
4254 .load = osd_it_ea_load
4258 static void *osd_key_init(const struct lu_context *ctx,
4259 struct lu_context_key *key)
4261 struct osd_thread_info *info;
4263 OBD_ALLOC_PTR(info);
4265 return ERR_PTR(-ENOMEM);
4267 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4268 if (info->oti_it_ea_buf == NULL)
4271 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4273 info->oti_hlock = ldiskfs_htree_lock_alloc();
4274 if (info->oti_hlock == NULL)
4280 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4283 return ERR_PTR(-ENOMEM);
4286 static void osd_key_fini(const struct lu_context *ctx,
4287 struct lu_context_key *key, void* data)
4289 struct osd_thread_info *info = data;
4291 if (info->oti_hlock != NULL)
4292 ldiskfs_htree_lock_free(info->oti_hlock);
4293 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4297 static void osd_key_exit(const struct lu_context *ctx,
4298 struct lu_context_key *key, void *data)
4300 struct osd_thread_info *info = data;
4302 LASSERT(info->oti_r_locks == 0);
4303 LASSERT(info->oti_w_locks == 0);
4304 LASSERT(info->oti_txns == 0);
4307 /* type constructor/destructor: osd_type_init, osd_type_fini */
4308 LU_TYPE_INIT_FINI(osd, &osd_key);
4310 struct lu_context_key osd_key = {
4311 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4312 .lct_init = osd_key_init,
4313 .lct_fini = osd_key_fini,
4314 .lct_exit = osd_key_exit
4318 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4319 const char *name, struct lu_device *next)
4321 struct osd_device *osd = osd_dev(d);
4323 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4324 return osd_procfs_init(osd, name);
4327 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4331 osd_scrub_cleanup(env, o);
4334 fsfilt_put_ops(o->od_fsops);
4338 /* shutdown quota slave instance associated with the device */
4339 if (o->od_quota_slave != NULL) {
4340 qsd_fini(env, o->od_quota_slave);
4341 o->od_quota_slave = NULL;
4347 static int osd_mount(const struct lu_env *env,
4348 struct osd_device *o, struct lustre_cfg *cfg)
4350 const char *name = lustre_cfg_string(cfg, 0);
4351 const char *dev = lustre_cfg_string(cfg, 1);
4353 unsigned long page, s_flags, lmd_flags = 0;
4354 struct page *__page;
4355 struct file_system_type *type;
4356 char *options = NULL;
4361 if (o->od_mnt != NULL)
4364 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4365 if (o->od_fsops == NULL) {
4366 CERROR("Can't find fsfilt_ldiskfs\n");
4370 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4372 GOTO(out, rc = -ENOMEM);
4374 str = lustre_cfg_string(cfg, 2);
4375 s_flags = simple_strtoul(str, NULL, 0);
4376 str = strstr(str, ":");
4378 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4379 opts = lustre_cfg_string(cfg, 3);
4380 page = (unsigned long)cfs_page_address(__page);
4381 options = (char *)page;
4384 strcat(options, "user_xattr,acl");
4386 strcat(options, opts);
4388 /* Glom up mount options */
4389 if (*options != '\0')
4390 strcat(options, ",");
4391 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4393 type = get_fs_type("ldiskfs");
4395 CERROR("%s: cannot find ldiskfs module\n", name);
4396 GOTO(out, rc = -ENODEV);
4399 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4400 cfs_module_put(type->owner);
4402 if (IS_ERR(o->od_mnt)) {
4403 rc = PTR_ERR(o->od_mnt);
4404 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4409 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4410 CERROR("%s: underlying device %s is marked as read-only. "
4411 "Setup failed\n", name, dev);
4414 GOTO(out, rc = -EROFS);
4417 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4418 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4419 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4422 GOTO(out, rc = -EINVAL);
4425 if (lmd_flags & LMD_FLG_IAM) {
4427 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4430 if (lmd_flags & LMD_FLG_NOSCRUB)
4431 o->od_scrub.os_no_scrub = 1;
4435 OBD_PAGE_FREE(__page);
4437 fsfilt_put_ops(o->od_fsops);
4442 static struct lu_device *osd_device_fini(const struct lu_env *env,
4443 struct lu_device *d)
4448 rc = osd_shutdown(env, osd_dev(d));
4450 osd_compat_fini(osd_dev(d));
4452 shrink_dcache_sb(osd_sb(osd_dev(d)));
4453 osd_sync(env, lu2dt_dev(d));
4455 rc = osd_procfs_fini(osd_dev(d));
4457 CERROR("proc fini error %d \n", rc);
4458 RETURN (ERR_PTR(rc));
4461 if (osd_dev(d)->od_mnt) {
4462 mntput(osd_dev(d)->od_mnt);
4463 osd_dev(d)->od_mnt = NULL;
4469 static int osd_device_init0(const struct lu_env *env,
4470 struct osd_device *o,
4471 struct lustre_cfg *cfg)
4473 struct lu_device *l = osd2lu_dev(o);
4474 struct osd_thread_info *info;
4477 /* if the module was re-loaded, env can loose its keys */
4478 rc = lu_env_refill((struct lu_env *) env);
4481 info = osd_oti_get(env);
4484 l->ld_ops = &osd_lu_ops;
4485 o->od_dt_dev.dd_ops = &osd_dt_ops;
4487 cfs_spin_lock_init(&o->od_osfs_lock);
4488 cfs_mutex_init(&o->od_otable_mutex);
4489 o->od_osfs_age = cfs_time_shift_64(-1000);
4491 o->od_capa_hash = init_capa_hash();
4492 if (o->od_capa_hash == NULL)
4493 GOTO(out, rc = -ENOMEM);
4495 o->od_read_cache = 1;
4496 o->od_writethrough_cache = 1;
4498 rc = osd_mount(env, o, cfg);
4502 /* setup scrub, including OI files initialization */
4503 rc = osd_scrub_setup(env, o);
4507 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4508 sizeof(o->od_svname) - 1);
4510 rc = osd_compat_init(o);
4512 GOTO(out_scrub, rc);
4514 rc = lu_site_init(&o->od_site, l);
4516 GOTO(out_compat, rc);
4518 rc = lu_site_init_finish(&o->od_site);
4522 rc = osd_procfs_init(o, o->od_svname);
4524 CERROR("%s: can't initialize procfs: rc = %d\n",
4529 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4533 lu_site_fini(&o->od_site);
4537 osd_scrub_cleanup(env, o);
4539 osd_oi_fini(info, o);
4540 osd_shutdown(env, o);
4544 cleanup_capa_hash(o->od_capa_hash);
4549 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4550 struct lu_device_type *t,
4551 struct lustre_cfg *cfg)
4553 struct osd_device *o;
4558 return ERR_PTR(-ENOMEM);
4560 rc = dt_device_init(&o->od_dt_dev, t);
4562 rc = osd_device_init0(env, o, cfg);
4564 dt_device_fini(&o->od_dt_dev);
4567 if (unlikely(rc != 0))
4570 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4573 static struct lu_device *osd_device_free(const struct lu_env *env,
4574 struct lu_device *d)
4576 struct osd_device *o = osd_dev(d);
4579 cleanup_capa_hash(o->od_capa_hash);
4580 /* XXX: make osd top device in order to release reference */
4581 d->ld_site->ls_top_dev = d;
4582 lu_site_purge(env, d->ld_site, -1);
4583 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4584 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4585 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4587 lu_site_fini(&o->od_site);
4588 dt_device_fini(&o->od_dt_dev);
4593 static int osd_process_config(const struct lu_env *env,
4594 struct lu_device *d, struct lustre_cfg *cfg)
4596 struct osd_device *o = osd_dev(d);
4600 switch(cfg->lcfg_command) {
4602 err = osd_mount(env, o, cfg);
4605 lu_dev_del_linkage(d->ld_site, d);
4606 err = osd_shutdown(env, o);
4615 static int osd_recovery_complete(const struct lu_env *env,
4616 struct lu_device *d)
4622 * we use exports to track all osd users
4624 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4625 struct obd_device *obd, struct obd_uuid *cluuid,
4626 struct obd_connect_data *data, void *localdata)
4628 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4629 struct lustre_handle conn;
4633 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4635 rc = class_connect(&conn, obd, cluuid);
4639 *exp = class_conn2export(&conn);
4641 cfs_spin_lock(&osd->od_osfs_lock);
4643 cfs_spin_unlock(&osd->od_osfs_lock);
4649 * once last export (we don't count self-export) disappeared
4650 * osd can be released
4652 static int osd_obd_disconnect(struct obd_export *exp)
4654 struct obd_device *obd = exp->exp_obd;
4655 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4656 int rc, release = 0;
4659 /* Only disconnect the underlying layers on the final disconnect. */
4660 cfs_spin_lock(&osd->od_osfs_lock);
4662 if (osd->od_connects == 0)
4664 cfs_spin_unlock(&osd->od_osfs_lock);
4666 rc = class_disconnect(exp); /* bz 9811 */
4668 if (rc == 0 && release)
4669 class_manual_cleanup(obd);
4673 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4674 struct lu_device *dev)
4676 struct osd_device *osd = osd_dev(dev);
4680 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
4681 /* MDT/MDD still use old infrastructure to create
4683 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4689 /* 2. setup quota slave instance */
4690 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4691 osd->od_proc_entry);
4692 if (IS_ERR(osd->od_quota_slave)) {
4693 result = PTR_ERR(osd->od_quota_slave);
4694 osd->od_quota_slave = NULL;
4700 static const struct lu_object_operations osd_lu_obj_ops = {
4701 .loo_object_init = osd_object_init,
4702 .loo_object_delete = osd_object_delete,
4703 .loo_object_release = osd_object_release,
4704 .loo_object_free = osd_object_free,
4705 .loo_object_print = osd_object_print,
4706 .loo_object_invariant = osd_object_invariant
4709 const struct lu_device_operations osd_lu_ops = {
4710 .ldo_object_alloc = osd_object_alloc,
4711 .ldo_process_config = osd_process_config,
4712 .ldo_recovery_complete = osd_recovery_complete,
4713 .ldo_prepare = osd_prepare,
4716 static const struct lu_device_type_operations osd_device_type_ops = {
4717 .ldto_init = osd_type_init,
4718 .ldto_fini = osd_type_fini,
4720 .ldto_start = osd_type_start,
4721 .ldto_stop = osd_type_stop,
4723 .ldto_device_alloc = osd_device_alloc,
4724 .ldto_device_free = osd_device_free,
4726 .ldto_device_init = osd_device_init,
4727 .ldto_device_fini = osd_device_fini
4730 struct lu_device_type osd_device_type = {
4731 .ldt_tags = LU_DEVICE_DT,
4732 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4733 .ldt_ops = &osd_device_type_ops,
4734 .ldt_ctx_tags = LCT_LOCAL,
4738 * lprocfs legacy support.
4740 static struct obd_ops osd_obd_device_ops = {
4741 .o_owner = THIS_MODULE,
4742 .o_connect = osd_obd_connect,
4743 .o_disconnect = osd_obd_disconnect
4746 static int __init osd_mod_init(void)
4748 struct lprocfs_static_vars lvars;
4751 lprocfs_osd_init_vars(&lvars);
4752 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4753 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4756 static void __exit osd_mod_exit(void)
4758 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4761 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4762 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4763 MODULE_LICENSE("GPL");
4765 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);