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);
287 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
288 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
289 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
291 return ERR_PTR(EREMCHG);
297 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
298 const struct lu_fid *fid,
299 const struct lu_object_conf *conf)
301 struct osd_thread_info *info;
302 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
303 struct osd_device *dev;
304 struct osd_idmap_cache *oic;
305 struct osd_inode_id *id;
307 struct osd_scrub *scrub;
308 struct scrub_file *sf;
313 LINVRNT(osd_invariant(obj));
314 LASSERT(obj->oo_inode == NULL);
315 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
318 scrub = &dev->od_scrub;
319 sf = &scrub->os_file;
320 info = osd_oti_get(env);
322 oic = &info->oti_cache;
325 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
328 /* Search order: 1. per-thread cache. */
329 if (lu_fid_eq(fid, &oic->oic_fid)) {
331 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
332 /* Search order: 2. OI scrub pending list. */
333 result = osd_oii_lookup(dev, fid, id);
338 if (sf->sf_flags & SF_INCONSISTENT)
342 * Objects are created as locking anchors or place holders for objects
343 * yet to be created. No need to osd_oi_lookup() at here because FID
344 * shouldn't never be re-used, if it's really a duplicate FID from
345 * unexpected reason, we should be able to detect it later by calling
346 * do_create->osd_oi_insert()
348 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
349 GOTO(out, result = 0);
351 /* Search order: 3. OI files. */
352 result = osd_oi_lookup(info, dev, fid, id);
353 if (result == -ENOENT) {
354 if (!fid_is_norm(fid) ||
355 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
357 GOTO(out, result = 0);
367 inode = osd_iget(info, dev, id);
369 inode = osd_iget_verify(info, dev, id, fid);
371 result = PTR_ERR(inode);
372 if (result == -ENOENT || result == -ESTALE) {
373 fid_zero(&oic->oic_fid);
375 } else if (result == -EREMCHG) {
378 if (thread_is_running(&scrub->os_thread)) {
379 result = -EINPROGRESS;
380 } else if (!dev->od_noscrub) {
381 result = osd_scrub_start(dev);
382 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
383 "for "DFID", rc = %d [1]\n",
384 LDISKFS_SB(osd_sb(dev))->s_es->\
385 s_volume_name,PFID(fid), result);
386 if (result == 0 || result == -EALREADY)
387 result = -EINPROGRESS;
396 obj->oo_inode = inode;
397 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
398 if (dev->od_iop_mode) {
399 obj->oo_compat_dot_created = 1;
400 obj->oo_compat_dotdot_created = 1;
403 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
404 GOTO(out, result = 0);
406 LASSERT(obj->oo_hl_head == NULL);
407 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
408 if (obj->oo_hl_head == NULL) {
409 obj->oo_inode = NULL;
411 GOTO(out, result = -ENOMEM);
413 GOTO(out, result = 0);
416 LINVRNT(osd_invariant(obj));
421 * Concurrency: shouldn't matter.
423 static void osd_object_init0(struct osd_object *obj)
425 LASSERT(obj->oo_inode != NULL);
426 obj->oo_dt.do_body_ops = &osd_body_ops;
427 obj->oo_dt.do_lu.lo_header->loh_attr |=
428 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
432 * Concurrency: no concurrent access is possible that early in object
435 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
436 const struct lu_object_conf *conf)
438 struct osd_object *obj = osd_obj(l);
441 LINVRNT(osd_invariant(obj));
443 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
444 obj->oo_dt.do_body_ops = &osd_body_ops_new;
446 if (obj->oo_inode != NULL) {
447 osd_object_init0(obj);
448 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
449 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
450 /* LFSCK iterator object is special without inode */
451 l->lo_header->loh_attr |= LOHA_EXISTS;
454 LINVRNT(osd_invariant(obj));
459 * Concurrency: no concurrent access is possible that late in object
462 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
464 struct osd_object *obj = osd_obj(l);
466 LINVRNT(osd_invariant(obj));
468 dt_object_fini(&obj->oo_dt);
469 if (obj->oo_hl_head != NULL)
470 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
475 * Concurrency: no concurrent access is possible that late in object
478 static void osd_index_fini(struct osd_object *o)
480 struct iam_container *bag;
482 if (o->oo_dir != NULL) {
483 bag = &o->oo_dir->od_container;
484 if (o->oo_inode != NULL) {
485 if (bag->ic_object == o->oo_inode)
486 iam_container_fini(bag);
488 OBD_FREE_PTR(o->oo_dir);
494 * Concurrency: no concurrent access is possible that late in object
495 * life-cycle (for all existing callers, that is. New callers have to provide
496 * their own locking.)
498 static int osd_inode_unlinked(const struct inode *inode)
500 return inode->i_nlink == 0;
504 OSD_TXN_OI_DELETE_CREDITS = 20,
505 OSD_TXN_INODE_DELETE_CREDITS = 20
512 #if OSD_THANDLE_STATS
514 * Set time when the handle is allocated
516 static void osd_th_alloced(struct osd_thandle *oth)
518 oth->oth_alloced = cfs_time_current();
522 * Set time when the handle started
524 static void osd_th_started(struct osd_thandle *oth)
526 oth->oth_started = cfs_time_current();
530 * Helper function to convert time interval to microseconds packed in
531 * long int (default time units for the counter in "stats" initialized
532 * by lu_time_init() )
534 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
538 cfs_duration_usec(cfs_time_sub(end, start), &val);
539 return val.tv_sec * 1000000 + val.tv_usec;
543 * Check whether the we deal with this handle for too long.
545 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
546 cfs_time_t alloced, cfs_time_t started,
549 cfs_time_t now = cfs_time_current();
551 LASSERT(dev != NULL);
553 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
554 interval_to_usec(alloced, started));
555 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
556 interval_to_usec(started, closed));
557 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
558 interval_to_usec(closed, now));
560 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
561 CWARN("transaction handle %p was open for too long: "
563 "alloced "CFS_TIME_T" ,"
564 "started "CFS_TIME_T" ,"
565 "closed "CFS_TIME_T"\n",
566 oth, now, alloced, started, closed);
567 libcfs_debug_dumpstack(NULL);
571 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
573 cfs_time_t __closed = cfs_time_current(); \
574 cfs_time_t __alloced = oth->oth_alloced; \
575 cfs_time_t __started = oth->oth_started; \
578 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
581 #else /* OSD_THANDLE_STATS */
583 #define osd_th_alloced(h) do {} while(0)
584 #define osd_th_started(h) do {} while(0)
585 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
587 #endif /* OSD_THANDLE_STATS */
590 * Concurrency: doesn't access mutable data.
592 static int osd_param_is_sane(const struct osd_device *dev,
593 const struct thandle *th)
595 struct osd_thandle *oh;
596 oh = container_of0(th, struct osd_thandle, ot_super);
597 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
601 * Concurrency: shouldn't matter.
603 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
604 static void osd_trans_commit_cb(struct super_block *sb,
605 struct journal_callback *jcb, int error)
607 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
610 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
611 struct thandle *th = &oh->ot_super;
612 struct lu_device *lud = &th->th_dev->dd_lu_dev;
613 struct dt_txn_commit_cb *dcb, *tmp;
615 LASSERT(oh->ot_handle == NULL);
618 CERROR("transaction @0x%p commit error: %d\n", th, error);
620 dt_txn_hook_commit(th);
622 /* call per-transaction callbacks if any */
623 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
624 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
625 "commit callback entry: magic=%x name='%s'\n",
626 dcb->dcb_magic, dcb->dcb_name);
627 cfs_list_del_init(&dcb->dcb_linkage);
628 dcb->dcb_func(NULL, th, dcb, error);
631 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
635 lu_context_exit(&th->th_ctx);
636 lu_context_fini(&th->th_ctx);
640 static struct thandle *osd_trans_create(const struct lu_env *env,
643 struct osd_thread_info *oti = osd_oti_get(env);
644 struct osd_iobuf *iobuf = &oti->oti_iobuf;
645 struct osd_thandle *oh;
649 /* on pending IO in this thread should left from prev. request */
650 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
652 th = ERR_PTR(-ENOMEM);
653 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
655 oh->ot_quota_trans = &oti->oti_quota_trans;
656 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
660 th->th_tags = LCT_TX_HANDLE;
662 oti->oti_dev = osd_dt_dev(d);
663 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
670 * Concurrency: shouldn't matter.
672 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
675 struct osd_thread_info *oti = osd_oti_get(env);
676 struct osd_device *dev = osd_dt_dev(d);
678 struct osd_thandle *oh;
683 LASSERT(current->journal_info == NULL);
685 oh = container_of0(th, struct osd_thandle, ot_super);
687 LASSERT(oh->ot_handle == NULL);
689 rc = dt_txn_hook_start(env, d, th);
693 if (!osd_param_is_sane(dev, th)) {
694 CWARN("%.16s: too many transaction credits (%d > %d)\n",
695 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
697 osd_journal(dev)->j_max_transaction_buffers);
698 /* XXX Limit the credits to 'max_transaction_buffers', and
699 * let the underlying filesystem to catch the error if
700 * we really need so many credits.
702 * This should be removed when we can calculate the
703 * credits precisely. */
704 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
705 #ifdef OSD_TRACK_DECLARES
706 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
707 oh->ot_declare_attr_set, oh->ot_declare_punch,
708 oh->ot_declare_xattr_set);
709 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
710 oh->ot_declare_create, oh->ot_declare_ref_add,
711 oh->ot_declare_ref_del, oh->ot_declare_write);
712 CERROR(" insert: %d, delete: %d, destroy: %d\n",
713 oh->ot_declare_insert, oh->ot_declare_delete,
714 oh->ot_declare_destroy);
719 * XXX temporary stuff. Some abstraction layer should
722 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
726 LASSERT(oti->oti_txns == 0);
727 lu_context_init(&th->th_ctx, th->th_tags);
728 lu_context_enter(&th->th_ctx);
730 lu_device_get(&d->dd_lu_dev);
731 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
743 * Concurrency: shouldn't matter.
745 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
748 struct osd_thandle *oh;
749 struct osd_thread_info *oti = osd_oti_get(env);
750 struct osd_iobuf *iobuf = &oti->oti_iobuf;
751 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
754 oh = container_of0(th, struct osd_thandle, ot_super);
757 /* inform the quota slave device that the transaction is
759 qsd_op_end(env, qsd, oh->ot_quota_trans);
760 oh->ot_quota_trans = NULL;
762 if (oh->ot_handle != NULL) {
763 handle_t *hdl = oh->ot_handle;
766 * add commit callback
767 * notice we don't do this in osd_trans_start()
768 * as underlying transaction can change during truncate
770 osd_journal_callback_set(hdl, osd_trans_commit_cb,
773 LASSERT(oti->oti_txns == 1);
775 rc = dt_txn_hook_stop(env, th);
777 CERROR("Failure in transaction hook: %d\n", rc);
779 /* hook functions might modify th_sync */
780 hdl->h_sync = th->th_sync;
782 oh->ot_handle = NULL;
783 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
784 rc = ldiskfs_journal_stop(hdl));
786 CERROR("Failure to stop transaction: %d\n", rc);
791 /* as we want IO to journal and data IO be concurrent, we don't block
792 * awaiting data IO completion in osd_do_bio(), instead we wait here
793 * once transaction is submitted to the journal. all reqular requests
794 * don't do direct IO (except read/write), thus this wait_event becomes
797 * IMPORTANT: we have to wait till any IO submited by the thread is
798 * completed otherwise iobuf may be corrupted by different request
800 cfs_wait_event(iobuf->dr_wait,
801 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
803 rc = iobuf->dr_error;
808 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
810 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
813 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
814 LASSERT(&dcb->dcb_func != NULL);
815 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
821 * Called just before object is freed. Releases all resources except for
822 * object itself (that is released by osd_object_free()).
824 * Concurrency: no concurrent access is possible that late in object
827 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
829 struct osd_object *obj = osd_obj(l);
830 struct inode *inode = obj->oo_inode;
832 LINVRNT(osd_invariant(obj));
835 * If object is unlinked remove fid->ino mapping from object index.
841 obj->oo_inode = NULL;
846 * Concurrency: ->loo_object_release() is called under site spin-lock.
848 static void osd_object_release(const struct lu_env *env,
854 * Concurrency: shouldn't matter.
856 static int osd_object_print(const struct lu_env *env, void *cookie,
857 lu_printer_t p, const struct lu_object *l)
859 struct osd_object *o = osd_obj(l);
862 if (o->oo_dir != NULL)
863 d = o->oo_dir->od_container.ic_descr;
866 return (*p)(env, cookie,
867 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
869 o->oo_inode ? o->oo_inode->i_ino : 0UL,
870 o->oo_inode ? o->oo_inode->i_generation : 0,
871 d ? d->id_ops->id_name : "plain");
875 * Concurrency: shouldn't matter.
877 int osd_statfs(const struct lu_env *env, struct dt_device *d,
878 struct obd_statfs *sfs)
880 struct osd_device *osd = osd_dt_dev(d);
881 struct super_block *sb = osd_sb(osd);
882 struct kstatfs *ksfs;
885 if (unlikely(osd->od_mnt == NULL))
888 /* osd_lproc.c call this without env, allocate ksfs for that case */
889 if (unlikely(env == NULL)) {
894 ksfs = &osd_oti_get(env)->oti_ksfs;
897 cfs_spin_lock(&osd->od_osfs_lock);
899 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
900 result = sb->s_op->statfs(sb->s_root, ksfs);
901 if (likely(result == 0)) { /* N.B. statfs can't really fail */
902 osd->od_osfs_age = cfs_time_current_64();
903 statfs_pack(&osd->od_statfs, ksfs);
904 if (sb->s_flags & MS_RDONLY)
905 sfs->os_state = OS_STATE_READONLY;
909 if (likely(result == 0))
910 *sfs = osd->od_statfs;
911 cfs_spin_unlock(&osd->od_osfs_lock);
913 if (unlikely(env == NULL))
920 * Estimate space needed for file creations. We assume the largest filename
921 * which is 2^64 - 1, hence a filename of 20 chars.
922 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
924 #ifdef __LDISKFS_DIR_REC_LEN
925 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
927 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
931 * Concurrency: doesn't access mutable data.
933 static void osd_conf_get(const struct lu_env *env,
934 const struct dt_device *dev,
935 struct dt_device_param *param)
937 struct super_block *sb = osd_sb(osd_dt_dev(dev));
940 * XXX should be taken from not-yet-existing fs abstraction layer.
942 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
943 param->ddp_max_name_len = LDISKFS_NAME_LEN;
944 param->ddp_max_nlink = LDISKFS_LINK_MAX;
945 param->ddp_block_shift = sb->s_blocksize_bits;
946 param->ddp_mount_type = LDD_MT_LDISKFS;
947 param->ddp_maxbytes = sb->s_maxbytes;
948 /* Overhead estimate should be fairly accurate, so we really take a tiny
949 * error margin which also avoids fragmenting the filesystem too much */
950 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
951 /* inode are statically allocated, so per-inode space consumption
952 * is the space consumed by the directory entry */
953 param->ddp_inodespace = PER_OBJ_USAGE;
954 /* per-fragment overhead to be used by the client code */
955 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
956 param->ddp_mntopts = 0;
957 if (test_opt(sb, XATTR_USER))
958 param->ddp_mntopts |= MNTOPT_USERXATTR;
959 if (test_opt(sb, POSIX_ACL))
960 param->ddp_mntopts |= MNTOPT_ACL;
962 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
963 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
964 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
967 param->ddp_max_ea_size = sb->s_blocksize;
972 * Helper function to get and fill the buffer with input values.
974 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
978 buf = &osd_oti_get(env)->oti_buf;
985 * Concurrency: shouldn't matter.
987 static int osd_sync(const struct lu_env *env, struct dt_device *d)
989 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
990 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
994 * Start commit for OSD device.
996 * An implementation of dt_commit_async method for OSD device.
997 * Asychronously starts underlayng fs sync and thereby a transaction
1000 * \param env environment
1001 * \param d dt device
1003 * \see dt_device_operations
1005 static int osd_commit_async(const struct lu_env *env,
1006 struct dt_device *d)
1008 struct super_block *s = osd_sb(osd_dt_dev(d));
1011 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1012 RETURN(s->s_op->sync_fs(s, 0));
1016 * Concurrency: shouldn't matter.
1019 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1021 struct super_block *sb = osd_sb(osd_dt_dev(d));
1025 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1027 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1032 * Concurrency: serialization provided by callers.
1034 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1035 int mode, unsigned long timeout, __u32 alg,
1036 struct lustre_capa_key *keys)
1038 struct osd_device *dev = osd_dt_dev(d);
1041 dev->od_fl_capa = mode;
1042 dev->od_capa_timeout = timeout;
1043 dev->od_capa_alg = alg;
1044 dev->od_capa_keys = keys;
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,
1118 static void osd_object_read_lock(const struct lu_env *env,
1119 struct dt_object *dt, unsigned role)
1121 struct osd_object *obj = osd_dt_obj(dt);
1122 struct osd_thread_info *oti = osd_oti_get(env);
1124 LINVRNT(osd_invariant(obj));
1126 LASSERT(obj->oo_owner != env);
1127 cfs_down_read_nested(&obj->oo_sem, role);
1129 LASSERT(obj->oo_owner == NULL);
1133 static void osd_object_write_lock(const struct lu_env *env,
1134 struct dt_object *dt, unsigned role)
1136 struct osd_object *obj = osd_dt_obj(dt);
1137 struct osd_thread_info *oti = osd_oti_get(env);
1139 LINVRNT(osd_invariant(obj));
1141 LASSERT(obj->oo_owner != env);
1142 cfs_down_write_nested(&obj->oo_sem, role);
1144 LASSERT(obj->oo_owner == NULL);
1145 obj->oo_owner = env;
1149 static void osd_object_read_unlock(const struct lu_env *env,
1150 struct dt_object *dt)
1152 struct osd_object *obj = osd_dt_obj(dt);
1153 struct osd_thread_info *oti = osd_oti_get(env);
1155 LINVRNT(osd_invariant(obj));
1157 LASSERT(oti->oti_r_locks > 0);
1159 cfs_up_read(&obj->oo_sem);
1162 static void osd_object_write_unlock(const struct lu_env *env,
1163 struct dt_object *dt)
1165 struct osd_object *obj = osd_dt_obj(dt);
1166 struct osd_thread_info *oti = osd_oti_get(env);
1168 LINVRNT(osd_invariant(obj));
1170 LASSERT(obj->oo_owner == env);
1171 LASSERT(oti->oti_w_locks > 0);
1173 obj->oo_owner = NULL;
1174 cfs_up_write(&obj->oo_sem);
1177 static int osd_object_write_locked(const struct lu_env *env,
1178 struct dt_object *dt)
1180 struct osd_object *obj = osd_dt_obj(dt);
1182 LINVRNT(osd_invariant(obj));
1184 return obj->oo_owner == env;
1187 static int capa_is_sane(const struct lu_env *env,
1188 struct osd_device *dev,
1189 struct lustre_capa *capa,
1190 struct lustre_capa_key *keys)
1192 struct osd_thread_info *oti = osd_oti_get(env);
1193 struct lustre_capa *tcapa = &oti->oti_capa;
1194 struct obd_capa *oc;
1198 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1200 if (capa_is_expired(oc)) {
1201 DEBUG_CAPA(D_ERROR, capa, "expired");
1208 if (capa_is_expired_sec(capa)) {
1209 DEBUG_CAPA(D_ERROR, capa, "expired");
1213 cfs_spin_lock(&capa_lock);
1214 for (i = 0; i < 2; i++) {
1215 if (keys[i].lk_keyid == capa->lc_keyid) {
1216 oti->oti_capa_key = keys[i];
1220 cfs_spin_unlock(&capa_lock);
1223 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1227 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1231 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1232 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1236 oc = capa_add(dev->od_capa_hash, capa);
1242 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1243 struct lustre_capa *capa, __u64 opc)
1245 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1246 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1247 struct md_capainfo *ci;
1250 if (!dev->od_fl_capa)
1253 if (capa == BYPASS_CAPA)
1256 ci = md_capainfo(env);
1260 if (ci->mc_auth == LC_ID_NONE)
1264 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1268 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1269 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1274 if (!capa_opc_supported(capa, opc)) {
1275 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1279 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1280 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1287 static struct timespec *osd_inode_time(const struct lu_env *env,
1288 struct inode *inode, __u64 seconds)
1290 struct osd_thread_info *oti = osd_oti_get(env);
1291 struct timespec *t = &oti->oti_time;
1293 t->tv_sec = seconds;
1295 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1300 static void osd_inode_getattr(const struct lu_env *env,
1301 struct inode *inode, struct lu_attr *attr)
1303 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1304 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1305 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1307 attr->la_atime = LTIME_S(inode->i_atime);
1308 attr->la_mtime = LTIME_S(inode->i_mtime);
1309 attr->la_ctime = LTIME_S(inode->i_ctime);
1310 attr->la_mode = inode->i_mode;
1311 attr->la_size = i_size_read(inode);
1312 attr->la_blocks = inode->i_blocks;
1313 attr->la_uid = inode->i_uid;
1314 attr->la_gid = inode->i_gid;
1315 attr->la_flags = LDISKFS_I(inode)->i_flags;
1316 attr->la_nlink = inode->i_nlink;
1317 attr->la_rdev = inode->i_rdev;
1318 attr->la_blksize = 1 << inode->i_blkbits;
1319 attr->la_blkbits = inode->i_blkbits;
1322 static int osd_attr_get(const struct lu_env *env,
1323 struct dt_object *dt,
1324 struct lu_attr *attr,
1325 struct lustre_capa *capa)
1327 struct osd_object *obj = osd_dt_obj(dt);
1329 LASSERT(dt_object_exists(dt));
1330 LINVRNT(osd_invariant(obj));
1332 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1335 cfs_spin_lock(&obj->oo_guard);
1336 osd_inode_getattr(env, obj->oo_inode, attr);
1337 cfs_spin_unlock(&obj->oo_guard);
1341 static int osd_declare_attr_set(const struct lu_env *env,
1342 struct dt_object *dt,
1343 const struct lu_attr *attr,
1344 struct thandle *handle)
1346 struct osd_thandle *oh;
1347 struct osd_object *obj;
1348 struct osd_thread_info *info = osd_oti_get(env);
1349 struct lquota_id_info *qi = &info->oti_qi;
1355 LASSERT(dt != NULL);
1356 LASSERT(handle != NULL);
1358 obj = osd_dt_obj(dt);
1359 LASSERT(osd_invariant(obj));
1361 oh = container_of0(handle, struct osd_thandle, ot_super);
1362 LASSERT(oh->ot_handle == NULL);
1364 OSD_DECLARE_OP(oh, attr_set);
1365 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1367 if (attr == NULL || obj->oo_inode == NULL)
1370 bspace = obj->oo_inode->i_blocks;
1371 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1372 bspace = toqb(bspace);
1374 /* Changing ownership is always preformed by super user, it should not
1377 * We still need to call the osd_declare_qid() to calculate the journal
1378 * credits for updating quota accounting files and to trigger quota
1379 * space adjustment once the operation is completed.*/
1380 if ((attr->la_valid & LA_UID) != 0 &&
1381 attr->la_uid != obj->oo_inode->i_uid) {
1382 qi->lqi_type = USRQUOTA;
1384 /* inode accounting */
1385 qi->lqi_is_blk = false;
1387 /* one more inode for the new owner ... */
1388 qi->lqi_id.qid_uid = attr->la_uid;
1390 allocated = (attr->la_uid == 0) ? true : false;
1391 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1392 if (rc == -EDQUOT || rc == -EINPROGRESS)
1397 /* and one less inode for the current uid */
1398 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1400 rc = osd_declare_qid(env, oh, qi, true, NULL);
1401 if (rc == -EDQUOT || rc == -EINPROGRESS)
1406 /* block accounting */
1407 qi->lqi_is_blk = true;
1409 /* more blocks for the new owner ... */
1410 qi->lqi_id.qid_uid = attr->la_uid;
1411 qi->lqi_space = bspace;
1412 allocated = (attr->la_uid == 0) ? true : false;
1413 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1414 if (rc == -EDQUOT || rc == -EINPROGRESS)
1419 /* and finally less blocks for the current owner */
1420 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1421 qi->lqi_space = -bspace;
1422 rc = osd_declare_qid(env, oh, qi, true, NULL);
1423 if (rc == -EDQUOT || rc == -EINPROGRESS)
1429 if (attr->la_valid & LA_GID &&
1430 attr->la_gid != obj->oo_inode->i_gid) {
1431 qi->lqi_type = GRPQUOTA;
1433 /* inode accounting */
1434 qi->lqi_is_blk = false;
1436 /* one more inode for the new group owner ... */
1437 qi->lqi_id.qid_gid = attr->la_gid;
1439 allocated = (attr->la_gid == 0) ? true : false;
1440 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1441 if (rc == -EDQUOT || rc == -EINPROGRESS)
1446 /* and one less inode for the current gid */
1447 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1449 rc = osd_declare_qid(env, oh, qi, true, NULL);
1450 if (rc == -EDQUOT || rc == -EINPROGRESS)
1455 /* block accounting */
1456 qi->lqi_is_blk = true;
1458 /* more blocks for the new owner ... */
1459 qi->lqi_id.qid_gid = attr->la_gid;
1460 qi->lqi_space = bspace;
1461 allocated = (attr->la_gid == 0) ? true : false;
1462 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1463 if (rc == -EDQUOT || rc == -EINPROGRESS)
1468 /* and finally less blocks for the current owner */
1469 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1470 qi->lqi_space = -bspace;
1471 rc = osd_declare_qid(env, oh, qi, true, NULL);
1472 if (rc == -EDQUOT || rc == -EINPROGRESS)
1481 static int osd_inode_setattr(const struct lu_env *env,
1482 struct inode *inode, const struct lu_attr *attr)
1486 bits = attr->la_valid;
1488 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1490 if (bits & LA_ATIME)
1491 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1492 if (bits & LA_CTIME)
1493 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1494 if (bits & LA_MTIME)
1495 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1496 if (bits & LA_SIZE) {
1497 LDISKFS_I(inode)->i_disksize = attr->la_size;
1498 i_size_write(inode, attr->la_size);
1502 /* OSD should not change "i_blocks" which is used by quota.
1503 * "i_blocks" should be changed by ldiskfs only. */
1504 if (bits & LA_BLOCKS)
1505 inode->i_blocks = attr->la_blocks;
1508 inode->i_mode = (inode->i_mode & S_IFMT) |
1509 (attr->la_mode & ~S_IFMT);
1511 inode->i_uid = attr->la_uid;
1513 inode->i_gid = attr->la_gid;
1514 if (bits & LA_NLINK)
1515 inode->i_nlink = attr->la_nlink;
1517 inode->i_rdev = attr->la_rdev;
1519 if (bits & LA_FLAGS) {
1520 /* always keep S_NOCMTIME */
1521 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1527 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1529 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1530 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1535 if (attr->la_valid & LA_UID)
1536 iattr.ia_valid |= ATTR_UID;
1537 if (attr->la_valid & LA_GID)
1538 iattr.ia_valid |= ATTR_GID;
1539 iattr.ia_uid = attr->la_uid;
1540 iattr.ia_gid = attr->la_gid;
1542 rc = ll_vfs_dq_transfer(inode, &iattr);
1544 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1545 "enforcement enabled on the ldiskfs filesystem?",
1546 inode->i_sb->s_id, rc);
1553 static int osd_attr_set(const struct lu_env *env,
1554 struct dt_object *dt,
1555 const struct lu_attr *attr,
1556 struct thandle *handle,
1557 struct lustre_capa *capa)
1559 struct osd_object *obj = osd_dt_obj(dt);
1560 struct inode *inode;
1563 LASSERT(handle != NULL);
1564 LASSERT(dt_object_exists(dt));
1565 LASSERT(osd_invariant(obj));
1567 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1570 OSD_EXEC_OP(handle, attr_set);
1572 inode = obj->oo_inode;
1574 rc = osd_quota_transfer(inode, attr);
1578 cfs_spin_lock(&obj->oo_guard);
1579 rc = osd_inode_setattr(env, inode, attr);
1580 cfs_spin_unlock(&obj->oo_guard);
1583 inode->i_sb->s_op->dirty_inode(inode);
1587 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1588 struct osd_object *obj,
1589 const char *name, const int namelen)
1591 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1594 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1596 struct dt_allocation_hint *hint,
1600 struct osd_device *osd = osd_obj2dev(obj);
1601 struct osd_thandle *oth;
1602 struct dt_object *parent = NULL;
1603 struct inode *inode;
1605 LINVRNT(osd_invariant(obj));
1606 LASSERT(obj->oo_inode == NULL);
1607 LASSERT(obj->oo_hl_head == NULL);
1609 if (S_ISDIR(mode) && ldiskfs_pdo) {
1610 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1611 if (obj->oo_hl_head == NULL)
1615 oth = container_of(th, struct osd_thandle, ot_super);
1616 LASSERT(oth->ot_handle->h_transaction != NULL);
1618 if (hint && hint->dah_parent)
1619 parent = hint->dah_parent;
1621 inode = ldiskfs_create_inode(oth->ot_handle,
1622 parent ? osd_dt_obj(parent)->oo_inode :
1623 osd_sb(osd)->s_root->d_inode,
1625 if (!IS_ERR(inode)) {
1626 /* Do not update file c/mtime in ldiskfs.
1627 * NB: don't need any lock because no contention at this
1629 inode->i_flags |= S_NOCMTIME;
1630 inode->i_state |= I_LUSTRE_NOSCRUB;
1631 obj->oo_inode = inode;
1634 if (obj->oo_hl_head != NULL) {
1635 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1636 obj->oo_hl_head = NULL;
1638 result = PTR_ERR(inode);
1640 LINVRNT(osd_invariant(obj));
1648 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1649 struct lu_attr *attr,
1650 struct dt_allocation_hint *hint,
1651 struct dt_object_format *dof,
1655 struct osd_thandle *oth;
1656 struct osd_device *osd = osd_obj2dev(obj);
1657 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1659 LASSERT(S_ISDIR(attr->la_mode));
1661 oth = container_of(th, struct osd_thandle, ot_super);
1662 LASSERT(oth->ot_handle->h_transaction != NULL);
1663 result = osd_mkfile(info, obj, mode, hint, th);
1664 if (result == 0 && osd->od_iop_mode == 0) {
1665 LASSERT(obj->oo_inode != NULL);
1667 * XXX uh-oh... call low-level iam function directly.
1670 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1671 sizeof (struct osd_fid_pack),
1677 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1678 struct lu_attr *attr,
1679 struct dt_allocation_hint *hint,
1680 struct dt_object_format *dof,
1684 struct osd_thandle *oth;
1685 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1687 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1689 LASSERT(S_ISREG(attr->la_mode));
1691 oth = container_of(th, struct osd_thandle, ot_super);
1692 LASSERT(oth->ot_handle->h_transaction != NULL);
1694 result = osd_mkfile(info, obj, mode, hint, th);
1696 LASSERT(obj->oo_inode != NULL);
1697 if (feat->dif_flags & DT_IND_VARKEY)
1698 result = iam_lvar_create(obj->oo_inode,
1699 feat->dif_keysize_max,
1701 feat->dif_recsize_max,
1704 result = iam_lfix_create(obj->oo_inode,
1705 feat->dif_keysize_max,
1707 feat->dif_recsize_max,
1714 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1715 struct lu_attr *attr,
1716 struct dt_allocation_hint *hint,
1717 struct dt_object_format *dof,
1720 LASSERT(S_ISREG(attr->la_mode));
1721 return osd_mkfile(info, obj, (attr->la_mode &
1722 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1725 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1726 struct lu_attr *attr,
1727 struct dt_allocation_hint *hint,
1728 struct dt_object_format *dof,
1731 LASSERT(S_ISLNK(attr->la_mode));
1732 return osd_mkfile(info, obj, (attr->la_mode &
1733 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1736 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1737 struct lu_attr *attr,
1738 struct dt_allocation_hint *hint,
1739 struct dt_object_format *dof,
1742 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1745 LINVRNT(osd_invariant(obj));
1746 LASSERT(obj->oo_inode == NULL);
1747 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1748 S_ISFIFO(mode) || S_ISSOCK(mode));
1750 result = osd_mkfile(info, obj, mode, hint, th);
1752 LASSERT(obj->oo_inode != NULL);
1754 * This inode should be marked dirty for i_rdev. Currently
1755 * that is done in the osd_attr_init().
1757 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1759 LINVRNT(osd_invariant(obj));
1763 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1765 struct dt_allocation_hint *hint,
1766 struct dt_object_format *dof,
1769 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1771 osd_obj_type_f result;
1787 result = osd_mk_index;
1798 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1799 struct dt_object *parent, struct dt_object *child,
1800 cfs_umode_t child_mode)
1804 memset(ah, 0, sizeof(*ah));
1805 ah->dah_parent = parent;
1806 ah->dah_mode = child_mode;
1809 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1810 struct lu_attr *attr, struct dt_object_format *dof)
1812 struct inode *inode = obj->oo_inode;
1813 __u64 valid = attr->la_valid;
1816 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1818 if (dof->dof_type != DFT_NODE)
1819 attr->la_valid &= ~LA_RDEV;
1820 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1821 attr->la_valid &= ~LA_ATIME;
1822 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1823 attr->la_valid &= ~LA_CTIME;
1824 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1825 attr->la_valid &= ~LA_MTIME;
1827 result = osd_quota_transfer(inode, attr);
1831 if (attr->la_valid != 0) {
1832 result = osd_inode_setattr(info->oti_env, inode, attr);
1834 * The osd_inode_setattr() should always succeed here. The
1835 * only error that could be returned is EDQUOT when we are
1836 * trying to change the UID or GID of the inode. However, this
1837 * should not happen since quota enforcement is no longer
1838 * enabled on ldiskfs (lquota takes care of it).
1840 LASSERTF(result == 0, "%d", result);
1841 inode->i_sb->s_op->dirty_inode(inode);
1844 attr->la_valid = valid;
1848 * Helper function for osd_object_create()
1850 * \retval 0, on success
1852 static int __osd_object_create(struct osd_thread_info *info,
1853 struct osd_object *obj, struct lu_attr *attr,
1854 struct dt_allocation_hint *hint,
1855 struct dt_object_format *dof,
1861 /* we drop umask so that permissions we pass are not affected */
1862 umask = current->fs->umask;
1863 current->fs->umask = 0;
1865 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1868 osd_attr_init(info, obj, attr, dof);
1869 osd_object_init0(obj);
1871 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1872 unlock_new_inode(obj->oo_inode);
1875 /* restore previous umask value */
1876 current->fs->umask = umask;
1882 * Helper function for osd_object_create()
1884 * \retval 0, on success
1886 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1887 const struct lu_fid *fid, struct thandle *th)
1889 struct osd_thread_info *info = osd_oti_get(env);
1890 struct osd_inode_id *id = &info->oti_id;
1891 struct osd_device *osd = osd_obj2dev(obj);
1893 LASSERT(obj->oo_inode != NULL);
1895 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1896 return osd_oi_insert(info, osd, fid, id, th);
1899 static int osd_declare_object_create(const struct lu_env *env,
1900 struct dt_object *dt,
1901 struct lu_attr *attr,
1902 struct dt_allocation_hint *hint,
1903 struct dt_object_format *dof,
1904 struct thandle *handle)
1906 struct osd_thandle *oh;
1910 LASSERT(handle != NULL);
1912 oh = container_of0(handle, struct osd_thandle, ot_super);
1913 LASSERT(oh->ot_handle == NULL);
1915 OSD_DECLARE_OP(oh, create);
1916 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1917 /* XXX: So far, only normal fid needs be inserted into the oi,
1918 * things could be changed later. Revise following code then. */
1919 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1920 OSD_DECLARE_OP(oh, insert);
1921 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1922 /* Reuse idle OI block may cause additional one OI block
1924 oh->ot_credits += 1;
1926 /* If this is directory, then we expect . and .. to be inserted as
1927 * well. The one directory block always needs to be created for the
1928 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1929 * block), there is no danger of needing a tree for the first block.
1931 if (attr && S_ISDIR(attr->la_mode)) {
1932 OSD_DECLARE_OP(oh, insert);
1933 OSD_DECLARE_OP(oh, insert);
1934 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1940 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
1941 false, false, NULL, false);
1945 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1946 struct lu_attr *attr,
1947 struct dt_allocation_hint *hint,
1948 struct dt_object_format *dof,
1951 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1952 struct osd_object *obj = osd_dt_obj(dt);
1953 struct osd_thread_info *info = osd_oti_get(env);
1958 LINVRNT(osd_invariant(obj));
1959 LASSERT(!dt_object_exists(dt));
1960 LASSERT(osd_write_locked(env, obj));
1961 LASSERT(th != NULL);
1963 if (unlikely(fid_is_acct(fid)))
1964 /* Quota files can't be created from the kernel any more,
1965 * 'tune2fs -O quota' will take care of creating them */
1968 OSD_EXEC_OP(th, create);
1970 result = __osd_object_create(info, obj, attr, hint, dof, th);
1972 result = __osd_oi_insert(env, obj, fid, th);
1974 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1975 LASSERT(osd_invariant(obj));
1980 * Called to destroy on-disk representation of the object
1982 * Concurrency: must be locked
1984 static int osd_declare_object_destroy(const struct lu_env *env,
1985 struct dt_object *dt,
1988 struct osd_object *obj = osd_dt_obj(dt);
1989 struct inode *inode = obj->oo_inode;
1990 struct osd_thandle *oh;
1994 oh = container_of0(th, struct osd_thandle, ot_super);
1995 LASSERT(oh->ot_handle == NULL);
1998 OSD_DECLARE_OP(oh, destroy);
1999 OSD_DECLARE_OP(oh, delete);
2000 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2001 /* XXX: So far, only normal fid needs to be inserted into the OI,
2002 * so only normal fid needs to be removed from the OI also. */
2003 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
2004 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2005 /* Recycle idle OI leaf may cause additional three OI blocks
2007 oh->ot_credits += 3;
2010 /* one less inode */
2011 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2012 false, true, NULL, false);
2015 /* data to be truncated */
2016 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh, true,
2021 static int osd_object_destroy(const struct lu_env *env,
2022 struct dt_object *dt,
2025 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2026 struct osd_object *obj = osd_dt_obj(dt);
2027 struct inode *inode = obj->oo_inode;
2028 struct osd_device *osd = osd_obj2dev(obj);
2029 struct osd_thandle *oh;
2033 oh = container_of0(th, struct osd_thandle, ot_super);
2034 LASSERT(oh->ot_handle);
2036 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2038 if (unlikely(fid_is_acct(fid)))
2041 /* Parallel control for OI scrub. For most of cases, there is no
2042 * lock contention. So it will not affect unlink performance. */
2043 cfs_mutex_lock(&inode->i_mutex);
2044 if (S_ISDIR(inode->i_mode)) {
2045 LASSERT(osd_inode_unlinked(inode) ||
2046 inode->i_nlink == 1);
2047 cfs_spin_lock(&obj->oo_guard);
2049 cfs_spin_unlock(&obj->oo_guard);
2050 inode->i_sb->s_op->dirty_inode(inode);
2052 LASSERT(osd_inode_unlinked(inode));
2055 OSD_EXEC_OP(th, destroy);
2057 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2058 cfs_mutex_unlock(&inode->i_mutex);
2060 /* XXX: add to ext3 orphan list */
2061 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2063 /* not needed in the cache anymore */
2064 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2070 * Helper function for osd_xattr_set()
2072 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2073 const struct lu_buf *buf, const char *name, int fl)
2075 struct osd_object *obj = osd_dt_obj(dt);
2076 struct inode *inode = obj->oo_inode;
2077 struct osd_thread_info *info = osd_oti_get(env);
2078 struct dentry *dentry = &info->oti_child_dentry;
2082 LASSERT(dt_object_exists(dt));
2083 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2085 if (fl & LU_XATTR_REPLACE)
2086 fs_flags |= XATTR_REPLACE;
2088 if (fl & LU_XATTR_CREATE)
2089 fs_flags |= XATTR_CREATE;
2091 dentry->d_inode = inode;
2092 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2093 buf->lb_len, fs_flags);
2098 * Put the fid into lustre_mdt_attrs, and then place the structure
2099 * inode's ea. This fid should not be altered during the life time
2102 * \retval +ve, on success
2103 * \retval -ve, on error
2105 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2107 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2108 const struct lu_fid *fid)
2110 struct osd_thread_info *info = osd_oti_get(env);
2111 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2113 lustre_lma_init(mdt_attrs, fid);
2114 lustre_lma_swab(mdt_attrs);
2115 return __osd_xattr_set(env, dt,
2116 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2117 XATTR_NAME_LMA, LU_XATTR_CREATE);
2122 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2123 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2124 * To have compatilibility with 1.8 ldiskfs driver we need to have
2125 * magic number at start of fid data.
2126 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2129 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2130 const struct dt_rec *fid)
2132 param->edp_magic = LDISKFS_LUFID_MAGIC;
2133 param->edp_len = sizeof(struct lu_fid) + 1;
2135 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2136 (struct lu_fid *)fid);
2140 * Try to read the fid from inode ea into dt_rec, if return value
2141 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2143 * \param fid object fid.
2145 * \retval 0 on success
2147 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2148 __u32 ino, struct lu_fid *fid,
2149 struct osd_inode_id *id)
2151 struct osd_thread_info *info = osd_oti_get(env);
2152 struct inode *inode;
2155 osd_id_gen(id, ino, OSD_OII_NOGEN);
2156 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2158 RETURN(PTR_ERR(inode));
2165 * OSD layer object create function for interoperability mode (b11826).
2166 * This is mostly similar to osd_object_create(). Only difference being, fid is
2167 * inserted into inode ea here.
2169 * \retval 0, on success
2170 * \retval -ve, on error
2172 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2173 struct lu_attr *attr,
2174 struct dt_allocation_hint *hint,
2175 struct dt_object_format *dof,
2178 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2179 struct osd_object *obj = osd_dt_obj(dt);
2180 struct osd_thread_info *info = osd_oti_get(env);
2185 LASSERT(osd_invariant(obj));
2186 LASSERT(!dt_object_exists(dt));
2187 LASSERT(osd_write_locked(env, obj));
2188 LASSERT(th != NULL);
2190 if (unlikely(fid_is_acct(fid)))
2191 /* Quota files can't be created from the kernel any more,
2192 * 'tune2fs -O quota' will take care of creating them */
2195 OSD_EXEC_OP(th, create);
2197 result = __osd_object_create(info, obj, attr, hint, dof, th);
2198 /* objects under osd root shld have igif fid, so dont add fid EA */
2199 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2200 result = osd_ea_fid_set(env, dt, fid);
2203 result = __osd_oi_insert(env, obj, fid, th);
2205 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2206 LINVRNT(osd_invariant(obj));
2210 static int osd_declare_object_ref_add(const struct lu_env *env,
2211 struct dt_object *dt,
2212 struct thandle *handle)
2214 struct osd_thandle *oh;
2216 /* it's possible that object doesn't exist yet */
2217 LASSERT(handle != NULL);
2219 oh = container_of0(handle, struct osd_thandle, ot_super);
2220 LASSERT(oh->ot_handle == NULL);
2222 OSD_DECLARE_OP(oh, ref_add);
2223 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2229 * Concurrency: @dt is write locked.
2231 static int osd_object_ref_add(const struct lu_env *env,
2232 struct dt_object *dt, struct thandle *th)
2234 struct osd_object *obj = osd_dt_obj(dt);
2235 struct inode *inode = obj->oo_inode;
2237 LINVRNT(osd_invariant(obj));
2238 LASSERT(dt_object_exists(dt));
2239 LASSERT(osd_write_locked(env, obj));
2240 LASSERT(th != NULL);
2242 OSD_EXEC_OP(th, ref_add);
2245 * DIR_NLINK feature is set for compatibility reasons if:
2246 * 1) nlinks > LDISKFS_LINK_MAX, or
2247 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2249 * It is easier to always set this flag (rather than check and set),
2250 * since it has less overhead, and the superblock will be dirtied
2251 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2252 * do not actually care whether this flag is set or not.
2254 cfs_spin_lock(&obj->oo_guard);
2256 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2257 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2258 inode->i_nlink == 2)
2261 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2262 cfs_spin_unlock(&obj->oo_guard);
2263 inode->i_sb->s_op->dirty_inode(inode);
2264 LINVRNT(osd_invariant(obj));
2269 static int osd_declare_object_ref_del(const struct lu_env *env,
2270 struct dt_object *dt,
2271 struct thandle *handle)
2273 struct osd_thandle *oh;
2275 LASSERT(dt_object_exists(dt));
2276 LASSERT(handle != NULL);
2278 oh = container_of0(handle, struct osd_thandle, ot_super);
2279 LASSERT(oh->ot_handle == NULL);
2281 OSD_DECLARE_OP(oh, ref_del);
2282 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2288 * Concurrency: @dt is write locked.
2290 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2293 struct osd_object *obj = osd_dt_obj(dt);
2294 struct inode *inode = obj->oo_inode;
2296 LINVRNT(osd_invariant(obj));
2297 LASSERT(dt_object_exists(dt));
2298 LASSERT(osd_write_locked(env, obj));
2299 LASSERT(th != NULL);
2301 OSD_EXEC_OP(th, ref_del);
2303 cfs_spin_lock(&obj->oo_guard);
2304 LASSERT(inode->i_nlink > 0);
2306 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2307 * then the nlink count is 1. Don't let it be set to 0 or the directory
2308 * inode will be deleted incorrectly. */
2309 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2311 cfs_spin_unlock(&obj->oo_guard);
2312 inode->i_sb->s_op->dirty_inode(inode);
2313 LINVRNT(osd_invariant(obj));
2319 * Get the 64-bit version for an inode.
2321 static int osd_object_version_get(const struct lu_env *env,
2322 struct dt_object *dt, dt_obj_version_t *ver)
2324 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2326 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2327 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2328 *ver = LDISKFS_I(inode)->i_fs_version;
2333 * Concurrency: @dt is read locked.
2335 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2336 struct lu_buf *buf, const char *name,
2337 struct lustre_capa *capa)
2339 struct osd_object *obj = osd_dt_obj(dt);
2340 struct inode *inode = obj->oo_inode;
2341 struct osd_thread_info *info = osd_oti_get(env);
2342 struct dentry *dentry = &info->oti_obj_dentry;
2344 /* version get is not real XATTR but uses xattr API */
2345 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2346 /* for version we are just using xattr API but change inode
2348 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2349 osd_object_version_get(env, dt, buf->lb_buf);
2350 return sizeof(dt_obj_version_t);
2353 LASSERT(dt_object_exists(dt));
2354 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2356 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2359 dentry->d_inode = inode;
2360 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2364 static int osd_declare_xattr_set(const struct lu_env *env,
2365 struct dt_object *dt,
2366 const struct lu_buf *buf, const char *name,
2367 int fl, struct thandle *handle)
2369 struct osd_thandle *oh;
2371 LASSERT(handle != NULL);
2373 oh = container_of0(handle, struct osd_thandle, ot_super);
2374 LASSERT(oh->ot_handle == NULL);
2376 OSD_DECLARE_OP(oh, xattr_set);
2377 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2378 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2380 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2386 * Set the 64-bit version for object
2388 static void osd_object_version_set(const struct lu_env *env,
2389 struct dt_object *dt,
2390 dt_obj_version_t *new_version)
2392 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2394 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2395 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2397 LDISKFS_I(inode)->i_fs_version = *new_version;
2398 /** Version is set after all inode operations are finished,
2399 * so we should mark it dirty here */
2400 inode->i_sb->s_op->dirty_inode(inode);
2404 * Concurrency: @dt is write locked.
2406 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2407 const struct lu_buf *buf, const char *name, int fl,
2408 struct thandle *handle, struct lustre_capa *capa)
2410 LASSERT(handle != NULL);
2412 /* version set is not real XATTR */
2413 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2414 /* for version we are just using xattr API but change inode
2416 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2417 osd_object_version_set(env, dt, buf->lb_buf);
2418 return sizeof(dt_obj_version_t);
2421 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2424 OSD_EXEC_OP(handle, xattr_set);
2425 return __osd_xattr_set(env, dt, buf, name, fl);
2429 * Concurrency: @dt is read locked.
2431 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2432 struct lu_buf *buf, struct lustre_capa *capa)
2434 struct osd_object *obj = osd_dt_obj(dt);
2435 struct inode *inode = obj->oo_inode;
2436 struct osd_thread_info *info = osd_oti_get(env);
2437 struct dentry *dentry = &info->oti_obj_dentry;
2439 LASSERT(dt_object_exists(dt));
2440 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2441 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2443 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2446 dentry->d_inode = inode;
2447 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2450 static int osd_declare_xattr_del(const struct lu_env *env,
2451 struct dt_object *dt, const char *name,
2452 struct thandle *handle)
2454 struct osd_thandle *oh;
2456 LASSERT(dt_object_exists(dt));
2457 LASSERT(handle != NULL);
2459 oh = container_of0(handle, struct osd_thandle, ot_super);
2460 LASSERT(oh->ot_handle == NULL);
2462 OSD_DECLARE_OP(oh, xattr_set);
2463 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2469 * Concurrency: @dt is write locked.
2471 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2472 const char *name, struct thandle *handle,
2473 struct lustre_capa *capa)
2475 struct osd_object *obj = osd_dt_obj(dt);
2476 struct inode *inode = obj->oo_inode;
2477 struct osd_thread_info *info = osd_oti_get(env);
2478 struct dentry *dentry = &info->oti_obj_dentry;
2481 LASSERT(dt_object_exists(dt));
2482 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2483 LASSERT(osd_write_locked(env, obj));
2484 LASSERT(handle != NULL);
2486 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2489 OSD_EXEC_OP(handle, xattr_set);
2491 dentry->d_inode = inode;
2492 rc = inode->i_op->removexattr(dentry, name);
2496 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2497 struct dt_object *dt,
2498 struct lustre_capa *old,
2501 struct osd_thread_info *info = osd_oti_get(env);
2502 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2503 struct osd_object *obj = osd_dt_obj(dt);
2504 struct osd_device *dev = osd_obj2dev(obj);
2505 struct lustre_capa_key *key = &info->oti_capa_key;
2506 struct lustre_capa *capa = &info->oti_capa;
2507 struct obd_capa *oc;
2508 struct md_capainfo *ci;
2512 if (!dev->od_fl_capa)
2513 RETURN(ERR_PTR(-ENOENT));
2515 LASSERT(dt_object_exists(dt));
2516 LINVRNT(osd_invariant(obj));
2518 /* renewal sanity check */
2519 if (old && osd_object_auth(env, dt, old, opc))
2520 RETURN(ERR_PTR(-EACCES));
2522 ci = md_capainfo(env);
2524 RETURN(ERR_PTR(-ENOENT));
2526 switch (ci->mc_auth) {
2530 capa->lc_uid = obj->oo_inode->i_uid;
2531 capa->lc_gid = obj->oo_inode->i_gid;
2532 capa->lc_flags = LC_ID_PLAIN;
2534 case LC_ID_CONVERT: {
2537 s[0] = obj->oo_inode->i_uid;
2538 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2539 s[2] = obj->oo_inode->i_gid;
2540 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2541 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2543 RETURN(ERR_PTR(rc));
2545 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2546 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2547 capa->lc_flags = LC_ID_CONVERT;
2551 RETURN(ERR_PTR(-EINVAL));
2554 capa->lc_fid = *fid;
2556 capa->lc_flags |= dev->od_capa_alg << 24;
2557 capa->lc_timeout = dev->od_capa_timeout;
2558 capa->lc_expiry = 0;
2560 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2562 LASSERT(!capa_is_expired(oc));
2566 cfs_spin_lock(&capa_lock);
2567 *key = dev->od_capa_keys[1];
2568 cfs_spin_unlock(&capa_lock);
2570 capa->lc_keyid = key->lk_keyid;
2571 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2573 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2575 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2576 RETURN(ERR_PTR(rc));
2579 oc = capa_add(dev->od_capa_hash, capa);
2583 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2585 struct osd_object *obj = osd_dt_obj(dt);
2586 struct inode *inode = obj->oo_inode;
2587 struct osd_thread_info *info = osd_oti_get(env);
2588 struct dentry *dentry = &info->oti_obj_dentry;
2589 struct file *file = &info->oti_file;
2594 dentry->d_inode = inode;
2595 file->f_dentry = dentry;
2596 file->f_mapping = inode->i_mapping;
2597 file->f_op = inode->i_fop;
2598 mutex_lock(&inode->i_mutex);
2599 rc = file->f_op->fsync(file, dentry, 0);
2600 mutex_unlock(&inode->i_mutex);
2604 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2607 struct osd_object *obj = osd_dt_obj(dt);
2610 *data = (void *)obj->oo_inode;
2618 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2619 const struct dt_index_features *feat)
2621 struct iam_descr *descr;
2623 if (osd_object_is_root(o))
2624 return feat == &dt_directory_features;
2626 LASSERT(o->oo_dir != NULL);
2628 descr = o->oo_dir->od_container.ic_descr;
2629 if (feat == &dt_directory_features) {
2630 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2636 feat->dif_keysize_min <= descr->id_key_size &&
2637 descr->id_key_size <= feat->dif_keysize_max &&
2638 feat->dif_recsize_min <= descr->id_rec_size &&
2639 descr->id_rec_size <= feat->dif_recsize_max &&
2640 !(feat->dif_flags & (DT_IND_VARKEY |
2641 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2642 ergo(feat->dif_flags & DT_IND_UPDATE,
2643 1 /* XXX check that object (and file system) is
2648 static int osd_iam_container_init(const struct lu_env *env,
2649 struct osd_object *obj,
2650 struct osd_directory *dir)
2652 struct iam_container *bag = &dir->od_container;
2655 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2659 result = iam_container_setup(bag);
2661 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2663 iam_container_fini(bag);
2670 * Concurrency: no external locking is necessary.
2672 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2673 const struct dt_index_features *feat)
2677 struct osd_object *obj = osd_dt_obj(dt);
2678 struct osd_device *osd = osd_obj2dev(obj);
2680 LINVRNT(osd_invariant(obj));
2681 LASSERT(dt_object_exists(dt));
2683 if (osd_object_is_root(obj)) {
2684 dt->do_index_ops = &osd_index_ea_ops;
2686 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2687 dt->do_index_ops = &osd_index_ea_ops;
2688 if (S_ISDIR(obj->oo_inode->i_mode))
2693 } else if (unlikely(feat == &dt_otable_features)) {
2694 dt->do_index_ops = &osd_otable_ops;
2696 } else if (feat == &dt_acct_features) {
2697 dt->do_index_ops = &osd_acct_index_ops;
2700 } else if (!osd_has_index(obj)) {
2701 struct osd_directory *dir;
2706 cfs_spin_lock(&obj->oo_guard);
2707 if (obj->oo_dir == NULL)
2711 * Concurrent thread allocated container data.
2714 cfs_spin_unlock(&obj->oo_guard);
2716 * Now, that we have container data, serialize its
2719 cfs_down_write(&obj->oo_ext_idx_sem);
2721 * recheck under lock.
2723 if (!osd_has_index(obj))
2724 result = osd_iam_container_init(env, obj, dir);
2727 cfs_up_write(&obj->oo_ext_idx_sem);
2735 if (result == 0 && skip_iam == 0) {
2736 if (!osd_iam_index_probe(env, obj, feat))
2739 LINVRNT(osd_invariant(obj));
2741 if (is_quota_glb_feat(feat))
2742 result = osd_quota_migration(env, dt, feat);
2747 static int osd_otable_it_attr_get(const struct lu_env *env,
2748 struct dt_object *dt,
2749 struct lu_attr *attr,
2750 struct lustre_capa *capa)
2756 static const struct dt_object_operations osd_obj_ops = {
2757 .do_read_lock = osd_object_read_lock,
2758 .do_write_lock = osd_object_write_lock,
2759 .do_read_unlock = osd_object_read_unlock,
2760 .do_write_unlock = osd_object_write_unlock,
2761 .do_write_locked = osd_object_write_locked,
2762 .do_attr_get = osd_attr_get,
2763 .do_declare_attr_set = osd_declare_attr_set,
2764 .do_attr_set = osd_attr_set,
2765 .do_ah_init = osd_ah_init,
2766 .do_declare_create = osd_declare_object_create,
2767 .do_create = osd_object_create,
2768 .do_declare_destroy = osd_declare_object_destroy,
2769 .do_destroy = osd_object_destroy,
2770 .do_index_try = osd_index_try,
2771 .do_declare_ref_add = osd_declare_object_ref_add,
2772 .do_ref_add = osd_object_ref_add,
2773 .do_declare_ref_del = osd_declare_object_ref_del,
2774 .do_ref_del = osd_object_ref_del,
2775 .do_xattr_get = osd_xattr_get,
2776 .do_declare_xattr_set = osd_declare_xattr_set,
2777 .do_xattr_set = osd_xattr_set,
2778 .do_declare_xattr_del = osd_declare_xattr_del,
2779 .do_xattr_del = osd_xattr_del,
2780 .do_xattr_list = osd_xattr_list,
2781 .do_capa_get = osd_capa_get,
2782 .do_object_sync = osd_object_sync,
2783 .do_data_get = osd_data_get,
2787 * dt_object_operations for interoperability mode
2788 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2790 static const struct dt_object_operations osd_obj_ea_ops = {
2791 .do_read_lock = osd_object_read_lock,
2792 .do_write_lock = osd_object_write_lock,
2793 .do_read_unlock = osd_object_read_unlock,
2794 .do_write_unlock = osd_object_write_unlock,
2795 .do_write_locked = osd_object_write_locked,
2796 .do_attr_get = osd_attr_get,
2797 .do_declare_attr_set = osd_declare_attr_set,
2798 .do_attr_set = osd_attr_set,
2799 .do_ah_init = osd_ah_init,
2800 .do_declare_create = osd_declare_object_create,
2801 .do_create = osd_object_ea_create,
2802 .do_declare_destroy = osd_declare_object_destroy,
2803 .do_destroy = osd_object_destroy,
2804 .do_index_try = osd_index_try,
2805 .do_declare_ref_add = osd_declare_object_ref_add,
2806 .do_ref_add = osd_object_ref_add,
2807 .do_declare_ref_del = osd_declare_object_ref_del,
2808 .do_ref_del = osd_object_ref_del,
2809 .do_xattr_get = osd_xattr_get,
2810 .do_declare_xattr_set = osd_declare_xattr_set,
2811 .do_xattr_set = osd_xattr_set,
2812 .do_declare_xattr_del = osd_declare_xattr_del,
2813 .do_xattr_del = osd_xattr_del,
2814 .do_xattr_list = osd_xattr_list,
2815 .do_capa_get = osd_capa_get,
2816 .do_object_sync = osd_object_sync,
2817 .do_data_get = osd_data_get,
2820 static const struct dt_object_operations osd_obj_otable_it_ops = {
2821 .do_attr_get = osd_otable_it_attr_get,
2822 .do_index_try = osd_index_try,
2825 static int osd_index_declare_iam_delete(const struct lu_env *env,
2826 struct dt_object *dt,
2827 const struct dt_key *key,
2828 struct thandle *handle)
2830 struct osd_thandle *oh;
2832 oh = container_of0(handle, struct osd_thandle, ot_super);
2833 LASSERT(oh->ot_handle == NULL);
2835 OSD_DECLARE_OP(oh, delete);
2836 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2842 * delete a (key, value) pair from index \a dt specified by \a key
2844 * \param dt osd index object
2845 * \param key key for index
2846 * \param rec record reference
2847 * \param handle transaction handler
2850 * \retval -ve failure
2853 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2854 const struct dt_key *key,
2855 struct thandle *handle,
2856 struct lustre_capa *capa)
2858 struct osd_thread_info *oti = osd_oti_get(env);
2859 struct osd_object *obj = osd_dt_obj(dt);
2860 struct osd_thandle *oh;
2861 struct iam_path_descr *ipd;
2862 struct iam_container *bag = &obj->oo_dir->od_container;
2867 LINVRNT(osd_invariant(obj));
2868 LASSERT(dt_object_exists(dt));
2869 LASSERT(bag->ic_object == obj->oo_inode);
2870 LASSERT(handle != NULL);
2872 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2875 OSD_EXEC_OP(handle, delete);
2877 ipd = osd_idx_ipd_get(env, bag);
2878 if (unlikely(ipd == NULL))
2881 oh = container_of0(handle, struct osd_thandle, ot_super);
2882 LASSERT(oh->ot_handle != NULL);
2883 LASSERT(oh->ot_handle->h_transaction != NULL);
2885 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2886 /* swab quota uid/gid provided by caller */
2887 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2888 key = (const struct dt_key *)&oti->oti_quota_id;
2891 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2892 osd_ipd_put(env, bag, ipd);
2893 LINVRNT(osd_invariant(obj));
2897 static int osd_index_declare_ea_delete(const struct lu_env *env,
2898 struct dt_object *dt,
2899 const struct dt_key *key,
2900 struct thandle *handle)
2902 struct osd_thandle *oh;
2903 struct inode *inode;
2907 LASSERT(dt_object_exists(dt));
2908 LASSERT(handle != NULL);
2910 oh = container_of0(handle, struct osd_thandle, ot_super);
2911 LASSERT(oh->ot_handle == NULL);
2913 OSD_DECLARE_OP(oh, delete);
2914 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2916 inode = osd_dt_obj(dt)->oo_inode;
2919 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2920 true, true, NULL, false);
2924 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2927 struct osd_fid_pack *rec;
2930 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2931 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2932 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2938 * Index delete function for interoperability mode (b11826).
2939 * It will remove the directory entry added by osd_index_ea_insert().
2940 * This entry is needed to maintain name->fid mapping.
2942 * \param key, key i.e. file entry to be deleted
2944 * \retval 0, on success
2945 * \retval -ve, on error
2947 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2948 const struct dt_key *key,
2949 struct thandle *handle,
2950 struct lustre_capa *capa)
2952 struct osd_object *obj = osd_dt_obj(dt);
2953 struct inode *dir = obj->oo_inode;
2954 struct dentry *dentry;
2955 struct osd_thandle *oh;
2956 struct ldiskfs_dir_entry_2 *de;
2957 struct buffer_head *bh;
2958 struct htree_lock *hlock = NULL;
2963 LINVRNT(osd_invariant(obj));
2964 LASSERT(dt_object_exists(dt));
2965 LASSERT(handle != NULL);
2967 OSD_EXEC_OP(handle, delete);
2969 oh = container_of(handle, struct osd_thandle, ot_super);
2970 LASSERT(oh->ot_handle != NULL);
2971 LASSERT(oh->ot_handle->h_transaction != NULL);
2973 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2976 dentry = osd_child_dentry_get(env, obj,
2977 (char *)key, strlen((char *)key));
2979 if (obj->oo_hl_head != NULL) {
2980 hlock = osd_oti_get(env)->oti_hlock;
2981 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2982 dir, LDISKFS_HLOCK_DEL);
2984 cfs_down_write(&obj->oo_ext_idx_sem);
2987 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2989 rc = ldiskfs_delete_entry(oh->ot_handle,
2996 ldiskfs_htree_unlock(hlock);
2998 cfs_up_write(&obj->oo_ext_idx_sem);
3000 LASSERT(osd_invariant(obj));
3005 * Lookup index for \a key and copy record to \a rec.
3007 * \param dt osd index object
3008 * \param key key for index
3009 * \param rec record reference
3011 * \retval +ve success : exact mach
3012 * \retval 0 return record with key not greater than \a key
3013 * \retval -ve failure
3015 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3016 struct dt_rec *rec, const struct dt_key *key,
3017 struct lustre_capa *capa)
3019 struct osd_object *obj = osd_dt_obj(dt);
3020 struct iam_path_descr *ipd;
3021 struct iam_container *bag = &obj->oo_dir->od_container;
3022 struct osd_thread_info *oti = osd_oti_get(env);
3023 struct iam_iterator *it = &oti->oti_idx_it;
3024 struct iam_rec *iam_rec;
3029 LASSERT(osd_invariant(obj));
3030 LASSERT(dt_object_exists(dt));
3031 LASSERT(bag->ic_object == obj->oo_inode);
3033 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3036 ipd = osd_idx_ipd_get(env, bag);
3040 /* got ipd now we can start iterator. */
3041 iam_it_init(it, bag, 0, ipd);
3043 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3044 /* swab quota uid/gid provided by caller */
3045 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3046 key = (const struct dt_key *)&oti->oti_quota_id;
3049 rc = iam_it_get(it, (struct iam_key *)key);
3051 if (S_ISDIR(obj->oo_inode->i_mode))
3052 iam_rec = (struct iam_rec *)oti->oti_ldp;
3054 iam_rec = (struct iam_rec *) rec;
3056 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3058 if (S_ISDIR(obj->oo_inode->i_mode))
3059 osd_fid_unpack((struct lu_fid *) rec,
3060 (struct osd_fid_pack *)iam_rec);
3061 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3062 osd_quota_unpack(obj, rec);
3067 osd_ipd_put(env, bag, ipd);
3069 LINVRNT(osd_invariant(obj));
3074 static int osd_index_declare_iam_insert(const struct lu_env *env,
3075 struct dt_object *dt,
3076 const struct dt_rec *rec,
3077 const struct dt_key *key,
3078 struct thandle *handle)
3080 struct osd_thandle *oh;
3082 LASSERT(dt_object_exists(dt));
3083 LASSERT(handle != NULL);
3085 oh = container_of0(handle, struct osd_thandle, ot_super);
3086 LASSERT(oh->ot_handle == NULL);
3088 OSD_DECLARE_OP(oh, insert);
3089 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3095 * Inserts (key, value) pair in \a dt index object.
3097 * \param dt osd index object
3098 * \param key key for index
3099 * \param rec record reference
3100 * \param th transaction handler
3103 * \retval -ve failure
3105 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3106 const struct dt_rec *rec,
3107 const struct dt_key *key, struct thandle *th,
3108 struct lustre_capa *capa, int ignore_quota)
3110 struct osd_object *obj = osd_dt_obj(dt);
3111 struct iam_path_descr *ipd;
3112 struct osd_thandle *oh;
3113 struct iam_container *bag = &obj->oo_dir->od_container;
3114 struct osd_thread_info *oti = osd_oti_get(env);
3115 struct iam_rec *iam_rec;
3120 LINVRNT(osd_invariant(obj));
3121 LASSERT(dt_object_exists(dt));
3122 LASSERT(bag->ic_object == obj->oo_inode);
3123 LASSERT(th != NULL);
3125 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3128 OSD_EXEC_OP(th, insert);
3130 ipd = osd_idx_ipd_get(env, bag);
3131 if (unlikely(ipd == NULL))
3134 oh = container_of0(th, struct osd_thandle, ot_super);
3135 LASSERT(oh->ot_handle != NULL);
3136 LASSERT(oh->ot_handle->h_transaction != NULL);
3137 if (S_ISDIR(obj->oo_inode->i_mode)) {
3138 iam_rec = (struct iam_rec *)oti->oti_ldp;
3139 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3140 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3141 /* pack quota uid/gid */
3142 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3143 key = (const struct dt_key *)&oti->oti_quota_id;
3144 /* pack quota record */
3145 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3146 iam_rec = (struct iam_rec *)rec;
3148 iam_rec = (struct iam_rec *)rec;
3151 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3153 osd_ipd_put(env, bag, ipd);
3154 LINVRNT(osd_invariant(obj));
3159 * Calls ldiskfs_add_entry() to add directory entry
3160 * into the directory. This is required for
3161 * interoperability mode (b11826)
3163 * \retval 0, on success
3164 * \retval -ve, on error
3166 static int __osd_ea_add_rec(struct osd_thread_info *info,
3167 struct osd_object *pobj, struct inode *cinode,
3168 const char *name, const struct dt_rec *fid,
3169 struct htree_lock *hlock, struct thandle *th)
3171 struct ldiskfs_dentry_param *ldp;
3172 struct dentry *child;
3173 struct osd_thandle *oth;
3176 oth = container_of(th, struct osd_thandle, ot_super);
3177 LASSERT(oth->ot_handle != NULL);
3178 LASSERT(oth->ot_handle->h_transaction != NULL);
3180 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3182 /* XXX: remove fid_is_igif() check here.
3183 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3184 * it is IGIF now but needs FID in dir entry as well for readdir
3186 * LU-838 should fix that and remove fid_is_igif() check */
3187 if (fid_is_igif((struct lu_fid *)fid) ||
3188 fid_is_norm((struct lu_fid *)fid)) {
3189 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3190 osd_get_ldiskfs_dirent_param(ldp, fid);
3191 child->d_fsdata = (void *)ldp;
3193 child->d_fsdata = NULL;
3195 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3201 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3202 * into the directory.Also sets flags into osd object to
3203 * indicate dot and dotdot are created. This is required for
3204 * interoperability mode (b11826)
3206 * \param dir directory for dot and dotdot fixup.
3207 * \param obj child object for linking
3209 * \retval 0, on success
3210 * \retval -ve, on error
3212 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3213 struct osd_object *dir,
3214 struct inode *parent_dir, const char *name,
3215 const struct dt_rec *dot_fid,
3216 const struct dt_rec *dot_dot_fid,
3219 struct inode *inode = dir->oo_inode;
3220 struct ldiskfs_dentry_param *dot_ldp;
3221 struct ldiskfs_dentry_param *dot_dot_ldp;
3222 struct osd_thandle *oth;
3225 oth = container_of(th, struct osd_thandle, ot_super);
3226 LASSERT(oth->ot_handle->h_transaction != NULL);
3227 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3229 if (strcmp(name, dot) == 0) {
3230 if (dir->oo_compat_dot_created) {
3233 LASSERT(inode == parent_dir);
3234 dir->oo_compat_dot_created = 1;
3237 } else if(strcmp(name, dotdot) == 0) {
3238 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3239 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3241 if (!dir->oo_compat_dot_created)
3243 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3244 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3245 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3250 /* in case of rename, dotdot is already created */
3251 if (dir->oo_compat_dotdot_created) {
3252 return __osd_ea_add_rec(info, dir, parent_dir, name,
3253 dot_dot_fid, NULL, th);
3256 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3257 inode, dot_ldp, dot_dot_ldp);
3259 dir->oo_compat_dotdot_created = 1;
3267 * It will call the appropriate osd_add* function and return the
3268 * value, return by respective functions.
3270 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3271 struct inode *cinode, const char *name,
3272 const struct dt_rec *fid, struct thandle *th)
3274 struct osd_thread_info *info = osd_oti_get(env);
3275 struct htree_lock *hlock;
3278 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3280 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3282 if (hlock != NULL) {
3283 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3286 cfs_down_write(&pobj->oo_ext_idx_sem);
3288 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3289 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3292 if (hlock != NULL) {
3293 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3294 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3296 cfs_down_write(&pobj->oo_ext_idx_sem);
3299 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3303 ldiskfs_htree_unlock(hlock);
3305 cfs_up_write(&pobj->oo_ext_idx_sem);
3311 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3312 struct osd_idmap_cache *oic)
3314 struct osd_scrub *scrub = &dev->od_scrub;
3315 struct lu_fid *fid = &oic->oic_fid;
3316 struct osd_inode_id *id = &oti->oti_id;
3321 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3325 rc = osd_oi_lookup(oti, dev, fid, id);
3326 if (rc != 0 && rc != -ENOENT)
3329 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3332 if (thread_is_running(&scrub->os_thread)) {
3333 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3334 /* There is race condition between osd_oi_lookup and OI scrub.
3335 * The OI scrub finished just after osd_oi_lookup() failure.
3336 * Under such case, it is unnecessary to trigger OI scrub again,
3337 * but try to call osd_oi_lookup() again. */
3338 if (unlikely(rc == -EAGAIN))
3344 if (!dev->od_noscrub && ++once == 1) {
3345 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3347 rc = osd_scrub_start(dev);
3348 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3350 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3360 * Calls ->lookup() to find dentry. From dentry get inode and
3361 * read inode's ea to get fid. This is required for interoperability
3364 * \retval 0, on success
3365 * \retval -ve, on error
3367 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3368 struct dt_rec *rec, const struct dt_key *key)
3370 struct inode *dir = obj->oo_inode;
3371 struct dentry *dentry;
3372 struct ldiskfs_dir_entry_2 *de;
3373 struct buffer_head *bh;
3374 struct lu_fid *fid = (struct lu_fid *) rec;
3375 struct htree_lock *hlock = NULL;
3379 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3381 dentry = osd_child_dentry_get(env, obj,
3382 (char *)key, strlen((char *)key));
3384 if (obj->oo_hl_head != NULL) {
3385 hlock = osd_oti_get(env)->oti_hlock;
3386 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3387 dir, LDISKFS_HLOCK_LOOKUP);
3389 cfs_down_read(&obj->oo_ext_idx_sem);
3392 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3394 struct osd_thread_info *oti = osd_oti_get(env);
3395 struct osd_idmap_cache *oic = &oti->oti_cache;
3396 struct osd_device *dev = osd_obj2dev(obj);
3397 struct osd_scrub *scrub = &dev->od_scrub;
3398 struct scrub_file *sf = &scrub->os_file;
3400 ino = le32_to_cpu(de->inode);
3401 rc = osd_get_fid_from_dentry(de, rec);
3403 /* done with de, release bh */
3406 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3408 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3410 fid_zero(&oic->oic_fid);
3414 oic->oic_fid = *fid;
3415 if ((scrub->os_pos_current <= ino) &&
3416 (sf->sf_flags & SF_INCONSISTENT ||
3417 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3419 osd_consistency_check(oti, dev, oic);
3428 ldiskfs_htree_unlock(hlock);
3430 cfs_up_read(&obj->oo_ext_idx_sem);
3435 * Find the osd object for given fid.
3437 * \param fid need to find the osd object having this fid
3439 * \retval osd_object on success
3440 * \retval -ve on error
3442 struct osd_object *osd_object_find(const struct lu_env *env,
3443 struct dt_object *dt,
3444 const struct lu_fid *fid)
3446 struct lu_device *ludev = dt->do_lu.lo_dev;
3447 struct osd_object *child = NULL;
3448 struct lu_object *luch;
3449 struct lu_object *lo;
3452 * at this point topdev might not exist yet
3453 * (i.e. MGS is preparing profiles). so we can
3454 * not rely on topdev and instead lookup with
3455 * our device passed as topdev. this can't work
3456 * if the object isn't cached yet (as osd doesn't
3457 * allocate lu_header). IOW, the object must be
3458 * in the cache, otherwise lu_object_alloc() crashes
3461 luch = lu_object_find_at(env, ludev, fid, NULL);
3462 if (!IS_ERR(luch)) {
3463 if (lu_object_exists(luch)) {
3464 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3466 child = osd_obj(lo);
3468 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3469 "lu_object can't be located"
3470 DFID"\n", PFID(fid));
3472 if (child == NULL) {
3473 lu_object_put(env, luch);
3474 CERROR("Unable to get osd_object\n");
3475 child = ERR_PTR(-ENOENT);
3478 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3479 "lu_object does not exists "DFID"\n",
3481 lu_object_put(env, luch);
3482 child = ERR_PTR(-ENOENT);
3485 child = (void *)luch;
3491 * Put the osd object once done with it.
3493 * \param obj osd object that needs to be put
3495 static inline void osd_object_put(const struct lu_env *env,
3496 struct osd_object *obj)
3498 lu_object_put(env, &obj->oo_dt.do_lu);
3501 static int osd_index_declare_ea_insert(const struct lu_env *env,
3502 struct dt_object *dt,
3503 const struct dt_rec *rec,
3504 const struct dt_key *key,
3505 struct thandle *handle)
3507 struct osd_thandle *oh;
3508 struct inode *inode;
3512 LASSERT(dt_object_exists(dt));
3513 LASSERT(handle != NULL);
3515 oh = container_of0(handle, struct osd_thandle, ot_super);
3516 LASSERT(oh->ot_handle == NULL);
3518 OSD_DECLARE_OP(oh, insert);
3519 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3521 inode = osd_dt_obj(dt)->oo_inode;
3524 /* We ignore block quota on meta pool (MDTs), so needn't
3525 * calculate how many blocks will be consumed by this index
3527 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3528 true, true, NULL, false);
3533 * Index add function for interoperability mode (b11826).
3534 * It will add the directory entry.This entry is needed to
3535 * maintain name->fid mapping.
3537 * \param key it is key i.e. file entry to be inserted
3538 * \param rec it is value of given key i.e. fid
3540 * \retval 0, on success
3541 * \retval -ve, on error
3543 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3544 const struct dt_rec *rec,
3545 const struct dt_key *key, struct thandle *th,
3546 struct lustre_capa *capa, int ignore_quota)
3548 struct osd_object *obj = osd_dt_obj(dt);
3549 struct lu_fid *fid = (struct lu_fid *) rec;
3550 const char *name = (const char *)key;
3551 struct osd_object *child;
3556 LASSERT(osd_invariant(obj));
3557 LASSERT(dt_object_exists(dt));
3558 LASSERT(th != NULL);
3560 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3563 child = osd_object_find(env, dt, fid);
3564 if (!IS_ERR(child)) {
3565 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3566 osd_object_put(env, child);
3568 rc = PTR_ERR(child);
3571 LASSERT(osd_invariant(obj));
3576 * Initialize osd Iterator for given osd index object.
3578 * \param dt osd index object
3581 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3582 struct dt_object *dt,
3584 struct lustre_capa *capa)
3586 struct osd_it_iam *it;
3587 struct osd_thread_info *oti = osd_oti_get(env);
3588 struct osd_object *obj = osd_dt_obj(dt);
3589 struct lu_object *lo = &dt->do_lu;
3590 struct iam_path_descr *ipd;
3591 struct iam_container *bag = &obj->oo_dir->od_container;
3593 LASSERT(lu_object_exists(lo));
3595 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3596 return ERR_PTR(-EACCES);
3599 ipd = osd_it_ipd_get(env, bag);
3600 if (likely(ipd != NULL)) {
3604 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3605 return (struct dt_it *)it;
3607 return ERR_PTR(-ENOMEM);
3611 * free given Iterator.
3614 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3616 struct osd_it_iam *it = (struct osd_it_iam *)di;
3617 struct osd_object *obj = it->oi_obj;
3619 iam_it_fini(&it->oi_it);
3620 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3621 lu_object_put(env, &obj->oo_dt.do_lu);
3625 * Move Iterator to record specified by \a key
3627 * \param di osd iterator
3628 * \param key key for index
3630 * \retval +ve di points to record with least key not larger than key
3631 * \retval 0 di points to exact matched key
3632 * \retval -ve failure
3635 static int osd_it_iam_get(const struct lu_env *env,
3636 struct dt_it *di, const struct dt_key *key)
3638 struct osd_thread_info *oti = osd_oti_get(env);
3639 struct osd_it_iam *it = (struct osd_it_iam *)di;
3641 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3642 /* swab quota uid/gid */
3643 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3644 key = (struct dt_key *)&oti->oti_quota_id;
3647 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3653 * \param di osd iterator
3656 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3658 struct osd_it_iam *it = (struct osd_it_iam *)di;
3660 iam_it_put(&it->oi_it);
3664 * Move iterator by one record
3666 * \param di osd iterator
3668 * \retval +1 end of container reached
3670 * \retval -ve failure
3673 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3675 struct osd_it_iam *it = (struct osd_it_iam *)di;
3677 return iam_it_next(&it->oi_it);
3681 * Return pointer to the key under iterator.
3684 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3685 const struct dt_it *di)
3687 struct osd_thread_info *oti = osd_oti_get(env);
3688 struct osd_it_iam *it = (struct osd_it_iam *)di;
3689 struct osd_object *obj = it->oi_obj;
3692 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3694 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3695 /* swab quota uid/gid */
3696 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3697 key = (struct dt_key *)&oti->oti_quota_id;
3704 * Return size of key under iterator (in bytes)
3707 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3709 struct osd_it_iam *it = (struct osd_it_iam *)di;
3711 return iam_it_key_size(&it->oi_it);
3714 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3715 int len, __u16 type)
3717 struct luda_type *lt;
3718 const unsigned align = sizeof(struct luda_type) - 1;
3720 /* check if file type is required */
3721 if (attr & LUDA_TYPE) {
3722 len = (len + align) & ~align;
3724 lt = (void *) ent->lde_name + len;
3725 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3726 ent->lde_attrs |= LUDA_TYPE;
3729 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3733 * build lu direct from backend fs dirent.
3736 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3737 struct lu_fid *fid, __u64 offset,
3738 char *name, __u16 namelen,
3739 __u16 type, __u32 attr)
3741 fid_cpu_to_le(&ent->lde_fid, fid);
3742 ent->lde_attrs = LUDA_FID;
3744 ent->lde_hash = cpu_to_le64(offset);
3745 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3747 strncpy(ent->lde_name, name, namelen);
3748 ent->lde_namelen = cpu_to_le16(namelen);
3750 /* append lustre attributes */
3751 osd_it_append_attrs(ent, attr, namelen, type);
3755 * Return pointer to the record under iterator.
3757 static int osd_it_iam_rec(const struct lu_env *env,
3758 const struct dt_it *di,
3759 struct dt_rec *dtrec, __u32 attr)
3761 struct osd_it_iam *it = (struct osd_it_iam *)di;
3762 struct osd_thread_info *info = osd_oti_get(env);
3765 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3766 const struct osd_fid_pack *rec;
3767 struct lu_fid *fid = &info->oti_fid;
3768 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3774 name = (char *)iam_it_key_get(&it->oi_it);
3776 RETURN(PTR_ERR(name));
3778 namelen = iam_it_key_size(&it->oi_it);
3780 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3782 RETURN(PTR_ERR(rec));
3784 rc = osd_fid_unpack(fid, rec);
3788 hash = iam_it_store(&it->oi_it);
3790 /* IAM does not store object type in IAM index (dir) */
3791 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3793 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3794 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3795 (struct iam_rec *)dtrec);
3796 osd_quota_unpack(it->oi_obj, dtrec);
3798 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3799 (struct iam_rec *)dtrec);
3806 * Returns cookie for current Iterator position.
3808 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3810 struct osd_it_iam *it = (struct osd_it_iam *)di;
3812 return iam_it_store(&it->oi_it);
3816 * Restore iterator from cookie.
3818 * \param di osd iterator
3819 * \param hash Iterator location cookie
3821 * \retval +ve di points to record with least key not larger than key.
3822 * \retval 0 di points to exact matched key
3823 * \retval -ve failure
3826 static int osd_it_iam_load(const struct lu_env *env,
3827 const struct dt_it *di, __u64 hash)
3829 struct osd_it_iam *it = (struct osd_it_iam *)di;
3831 return iam_it_load(&it->oi_it, hash);
3834 static const struct dt_index_operations osd_index_iam_ops = {
3835 .dio_lookup = osd_index_iam_lookup,
3836 .dio_declare_insert = osd_index_declare_iam_insert,
3837 .dio_insert = osd_index_iam_insert,
3838 .dio_declare_delete = osd_index_declare_iam_delete,
3839 .dio_delete = osd_index_iam_delete,
3841 .init = osd_it_iam_init,
3842 .fini = osd_it_iam_fini,
3843 .get = osd_it_iam_get,
3844 .put = osd_it_iam_put,
3845 .next = osd_it_iam_next,
3846 .key = osd_it_iam_key,
3847 .key_size = osd_it_iam_key_size,
3848 .rec = osd_it_iam_rec,
3849 .store = osd_it_iam_store,
3850 .load = osd_it_iam_load
3855 * Creates or initializes iterator context.
3857 * \retval struct osd_it_ea, iterator structure on success
3860 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3861 struct dt_object *dt,
3863 struct lustre_capa *capa)
3865 struct osd_object *obj = osd_dt_obj(dt);
3866 struct osd_thread_info *info = osd_oti_get(env);
3867 struct osd_it_ea *it = &info->oti_it_ea;
3868 struct lu_object *lo = &dt->do_lu;
3869 struct dentry *obj_dentry = &info->oti_it_dentry;
3871 LASSERT(lu_object_exists(lo));
3873 obj_dentry->d_inode = obj->oo_inode;
3874 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3875 obj_dentry->d_name.hash = 0;
3877 it->oie_rd_dirent = 0;
3878 it->oie_it_dirent = 0;
3879 it->oie_dirent = NULL;
3880 it->oie_buf = info->oti_it_ea_buf;
3882 it->oie_file.f_pos = 0;
3883 it->oie_file.f_dentry = obj_dentry;
3884 if (attr & LUDA_64BITHASH)
3885 it->oie_file.f_mode |= FMODE_64BITHASH;
3887 it->oie_file.f_mode |= FMODE_32BITHASH;
3888 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3889 it->oie_file.f_op = obj->oo_inode->i_fop;
3890 it->oie_file.private_data = NULL;
3892 RETURN((struct dt_it *) it);
3896 * Destroy or finishes iterator context.
3898 * \param di iterator structure to be destroyed
3900 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3902 struct osd_it_ea *it = (struct osd_it_ea *)di;
3903 struct osd_object *obj = it->oie_obj;
3904 struct inode *inode = obj->oo_inode;
3907 it->oie_file.f_op->release(inode, &it->oie_file);
3908 lu_object_put(env, &obj->oo_dt.do_lu);
3913 * It position the iterator at given key, so that next lookup continues from
3914 * that key Or it is similar to dio_it->load() but based on a key,
3915 * rather than file position.
3917 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3920 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3922 static int osd_it_ea_get(const struct lu_env *env,
3923 struct dt_it *di, const struct dt_key *key)
3925 struct osd_it_ea *it = (struct osd_it_ea *)di;
3928 LASSERT(((const char *)key)[0] == '\0');
3929 it->oie_file.f_pos = 0;
3930 it->oie_rd_dirent = 0;
3931 it->oie_it_dirent = 0;
3932 it->oie_dirent = NULL;
3940 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3945 * It is called internally by ->readdir(). It fills the
3946 * iterator's in-memory data structure with required
3947 * information i.e. name, namelen, rec_size etc.
3949 * \param buf in which information to be filled in.
3950 * \param name name of the file in given dir
3952 * \retval 0 on success
3953 * \retval 1 on buffer full
3955 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3956 loff_t offset, __u64 ino,
3959 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3960 struct osd_it_ea_dirent *ent = it->oie_dirent;
3961 struct lu_fid *fid = &ent->oied_fid;
3962 struct osd_fid_pack *rec;
3965 /* this should never happen */
3966 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3967 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3971 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3975 if (d_type & LDISKFS_DIRENT_LUFID) {
3976 rec = (struct osd_fid_pack*) (name + namelen + 1);
3978 if (osd_fid_unpack(fid, rec) != 0)
3981 d_type &= ~LDISKFS_DIRENT_LUFID;
3986 ent->oied_ino = ino;
3987 ent->oied_off = offset;
3988 ent->oied_namelen = namelen;
3989 ent->oied_type = d_type;
3991 memcpy(ent->oied_name, name, namelen);
3993 it->oie_rd_dirent++;
3994 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3999 * Calls ->readdir() to load a directory entry at a time
4000 * and stored it in iterator's in-memory data structure.
4002 * \param di iterator's in memory structure
4004 * \retval 0 on success
4005 * \retval -ve on error
4007 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4008 const struct dt_it *di)
4010 struct osd_it_ea *it = (struct osd_it_ea *)di;
4011 struct osd_object *obj = it->oie_obj;
4012 struct inode *inode = obj->oo_inode;
4013 struct htree_lock *hlock = NULL;
4017 it->oie_dirent = it->oie_buf;
4018 it->oie_rd_dirent = 0;
4020 if (obj->oo_hl_head != NULL) {
4021 hlock = osd_oti_get(env)->oti_hlock;
4022 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4023 inode, LDISKFS_HLOCK_READDIR);
4025 cfs_down_read(&obj->oo_ext_idx_sem);
4028 result = inode->i_fop->readdir(&it->oie_file, it,
4029 (filldir_t) osd_ldiskfs_filldir);
4032 ldiskfs_htree_unlock(hlock);
4034 cfs_up_read(&obj->oo_ext_idx_sem);
4036 if (it->oie_rd_dirent == 0) {
4039 it->oie_dirent = it->oie_buf;
4040 it->oie_it_dirent = 1;
4047 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4048 * to load a directory entry at a time and stored it in
4049 * iterator's in-memory data structure.
4051 * \param di iterator's in memory structure
4053 * \retval +ve iterator reached to end
4054 * \retval 0 iterator not reached to end
4055 * \retval -ve on error
4057 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4059 struct osd_it_ea *it = (struct osd_it_ea *)di;
4064 if (it->oie_it_dirent < it->oie_rd_dirent) {
4066 (void *) it->oie_dirent +
4067 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4068 it->oie_dirent->oied_namelen);
4069 it->oie_it_dirent++;
4072 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4075 rc = osd_ldiskfs_it_fill(env, di);
4082 * Returns the key at current position from iterator's in memory structure.
4084 * \param di iterator's in memory structure
4086 * \retval key i.e. struct dt_key on success
4088 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4089 const struct dt_it *di)
4091 struct osd_it_ea *it = (struct osd_it_ea *)di;
4093 return (struct dt_key *)it->oie_dirent->oied_name;
4097 * Returns the key's size at current position from iterator's in memory structure.
4099 * \param di iterator's in memory structure
4101 * \retval key_size i.e. struct dt_key on success
4103 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4105 struct osd_it_ea *it = (struct osd_it_ea *)di;
4107 return it->oie_dirent->oied_namelen;
4112 * Returns the value (i.e. fid/igif) at current position from iterator's
4113 * in memory structure.
4115 * \param di struct osd_it_ea, iterator's in memory structure
4116 * \param attr attr requested for dirent.
4117 * \param lde lustre dirent
4119 * \retval 0 no error and \param lde has correct lustre dirent.
4120 * \retval -ve on error
4122 static inline int osd_it_ea_rec(const struct lu_env *env,
4123 const struct dt_it *di,
4124 struct dt_rec *dtrec, __u32 attr)
4126 struct osd_it_ea *it = (struct osd_it_ea *)di;
4127 struct osd_object *obj = it->oie_obj;
4128 struct osd_device *dev = osd_obj2dev(obj);
4129 struct osd_scrub *scrub = &dev->od_scrub;
4130 struct scrub_file *sf = &scrub->os_file;
4131 struct osd_thread_info *oti = osd_oti_get(env);
4132 struct osd_idmap_cache *oic = &oti->oti_cache;
4133 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4134 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4135 __u32 ino = it->oie_dirent->oied_ino;
4139 if (!fid_is_sane(fid)) {
4140 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4144 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4147 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4148 it->oie_dirent->oied_name,
4149 it->oie_dirent->oied_namelen,
4150 it->oie_dirent->oied_type, attr);
4151 oic->oic_fid = *fid;
4152 if ((scrub->os_pos_current <= ino) &&
4153 (sf->sf_flags & SF_INCONSISTENT ||
4154 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4155 osd_consistency_check(oti, dev, oic);
4161 * Returns a cookie for current position of the iterator head, so that
4162 * user can use this cookie to load/start the iterator next time.
4164 * \param di iterator's in memory structure
4166 * \retval cookie for current position, on success
4168 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4170 struct osd_it_ea *it = (struct osd_it_ea *)di;
4172 return it->oie_dirent->oied_off;
4176 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4177 * to load a directory entry at a time and stored it i inn,
4178 * in iterator's in-memory data structure.
4180 * \param di struct osd_it_ea, iterator's in memory structure
4182 * \retval +ve on success
4183 * \retval -ve on error
4185 static int osd_it_ea_load(const struct lu_env *env,
4186 const struct dt_it *di, __u64 hash)
4188 struct osd_it_ea *it = (struct osd_it_ea *)di;
4192 it->oie_file.f_pos = hash;
4194 rc = osd_ldiskfs_it_fill(env, di);
4202 * Index lookup function for interoperability mode (b11826).
4204 * \param key, key i.e. file name to be searched
4206 * \retval +ve, on success
4207 * \retval -ve, on error
4209 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4210 struct dt_rec *rec, const struct dt_key *key,
4211 struct lustre_capa *capa)
4213 struct osd_object *obj = osd_dt_obj(dt);
4218 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4219 LINVRNT(osd_invariant(obj));
4221 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4224 rc = osd_ea_lookup_rec(env, obj, rec, key);
4231 * Index and Iterator operations for interoperability
4232 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4234 static const struct dt_index_operations osd_index_ea_ops = {
4235 .dio_lookup = osd_index_ea_lookup,
4236 .dio_declare_insert = osd_index_declare_ea_insert,
4237 .dio_insert = osd_index_ea_insert,
4238 .dio_declare_delete = osd_index_declare_ea_delete,
4239 .dio_delete = osd_index_ea_delete,
4241 .init = osd_it_ea_init,
4242 .fini = osd_it_ea_fini,
4243 .get = osd_it_ea_get,
4244 .put = osd_it_ea_put,
4245 .next = osd_it_ea_next,
4246 .key = osd_it_ea_key,
4247 .key_size = osd_it_ea_key_size,
4248 .rec = osd_it_ea_rec,
4249 .store = osd_it_ea_store,
4250 .load = osd_it_ea_load
4254 static void *osd_key_init(const struct lu_context *ctx,
4255 struct lu_context_key *key)
4257 struct osd_thread_info *info;
4259 OBD_ALLOC_PTR(info);
4261 return ERR_PTR(-ENOMEM);
4263 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4264 if (info->oti_it_ea_buf == NULL)
4267 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4269 info->oti_hlock = ldiskfs_htree_lock_alloc();
4270 if (info->oti_hlock == NULL)
4276 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4279 return ERR_PTR(-ENOMEM);
4282 static void osd_key_fini(const struct lu_context *ctx,
4283 struct lu_context_key *key, void* data)
4285 struct osd_thread_info *info = data;
4287 if (info->oti_hlock != NULL)
4288 ldiskfs_htree_lock_free(info->oti_hlock);
4289 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4293 static void osd_key_exit(const struct lu_context *ctx,
4294 struct lu_context_key *key, void *data)
4296 struct osd_thread_info *info = data;
4298 LASSERT(info->oti_r_locks == 0);
4299 LASSERT(info->oti_w_locks == 0);
4300 LASSERT(info->oti_txns == 0);
4303 /* type constructor/destructor: osd_type_init, osd_type_fini */
4304 LU_TYPE_INIT_FINI(osd, &osd_key);
4306 struct lu_context_key osd_key = {
4307 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4308 .lct_init = osd_key_init,
4309 .lct_fini = osd_key_fini,
4310 .lct_exit = osd_key_exit
4314 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4315 const char *name, struct lu_device *next)
4317 struct osd_device *osd = osd_dev(d);
4319 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4320 return osd_procfs_init(osd, name);
4323 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4327 osd_scrub_cleanup(env, o);
4330 fsfilt_put_ops(o->od_fsops);
4334 /* shutdown quota slave instance associated with the device */
4335 if (o->od_quota_slave != NULL) {
4336 qsd_fini(env, o->od_quota_slave);
4337 o->od_quota_slave = NULL;
4343 static int osd_mount(const struct lu_env *env,
4344 struct osd_device *o, struct lustre_cfg *cfg)
4346 const char *name = lustre_cfg_string(cfg, 0);
4347 const char *dev = lustre_cfg_string(cfg, 1);
4349 unsigned long page, s_flags, lmd_flags = 0;
4350 struct page *__page;
4351 struct file_system_type *type;
4352 char *options = NULL;
4357 if (o->od_mnt != NULL)
4360 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4361 if (o->od_fsops == NULL) {
4362 CERROR("Can't find fsfilt_ldiskfs\n");
4366 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4368 GOTO(out, rc = -ENOMEM);
4370 str = lustre_cfg_string(cfg, 2);
4371 s_flags = simple_strtoul(str, NULL, 0);
4372 str = strstr(str, ":");
4374 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4375 opts = lustre_cfg_string(cfg, 3);
4376 page = (unsigned long)cfs_page_address(__page);
4377 options = (char *)page;
4380 strcat(options, "user_xattr,acl");
4382 strcat(options, opts);
4384 /* Glom up mount options */
4385 if (*options != '\0')
4386 strcat(options, ",");
4387 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4389 type = get_fs_type("ldiskfs");
4391 CERROR("%s: cannot find ldiskfs module\n", name);
4392 GOTO(out, rc = -ENODEV);
4395 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4396 cfs_module_put(type->owner);
4398 if (IS_ERR(o->od_mnt)) {
4399 rc = PTR_ERR(o->od_mnt);
4400 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4405 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4406 CERROR("%s: underlying device %s is marked as read-only. "
4407 "Setup failed\n", name, dev);
4410 GOTO(out, rc = -EROFS);
4413 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4414 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4415 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4418 GOTO(out, rc = -EINVAL);
4421 if (lmd_flags & LMD_FLG_IAM) {
4423 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4426 if (lmd_flags & LMD_FLG_NOSCRUB)
4431 OBD_PAGE_FREE(__page);
4433 fsfilt_put_ops(o->od_fsops);
4438 static struct lu_device *osd_device_fini(const struct lu_env *env,
4439 struct lu_device *d)
4444 rc = osd_shutdown(env, osd_dev(d));
4446 osd_compat_fini(osd_dev(d));
4448 shrink_dcache_sb(osd_sb(osd_dev(d)));
4449 osd_sync(env, lu2dt_dev(d));
4451 rc = osd_procfs_fini(osd_dev(d));
4453 CERROR("proc fini error %d \n", rc);
4454 RETURN (ERR_PTR(rc));
4457 if (osd_dev(d)->od_mnt) {
4458 mntput(osd_dev(d)->od_mnt);
4459 osd_dev(d)->od_mnt = NULL;
4465 static int osd_device_init0(const struct lu_env *env,
4466 struct osd_device *o,
4467 struct lustre_cfg *cfg)
4469 struct lu_device *l = osd2lu_dev(o);
4470 struct osd_thread_info *info;
4473 /* if the module was re-loaded, env can loose its keys */
4474 rc = lu_env_refill((struct lu_env *) env);
4477 info = osd_oti_get(env);
4480 l->ld_ops = &osd_lu_ops;
4481 o->od_dt_dev.dd_ops = &osd_dt_ops;
4483 cfs_spin_lock_init(&o->od_osfs_lock);
4484 cfs_mutex_init(&o->od_otable_mutex);
4485 o->od_osfs_age = cfs_time_shift_64(-1000);
4487 o->od_capa_hash = init_capa_hash();
4488 if (o->od_capa_hash == NULL)
4489 GOTO(out, rc = -ENOMEM);
4491 o->od_read_cache = 1;
4492 o->od_writethrough_cache = 1;
4493 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
4495 rc = osd_mount(env, o, cfg);
4499 /* setup scrub, including OI files initialization */
4500 rc = osd_scrub_setup(env, o);
4504 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4505 sizeof(o->od_svname) - 1);
4507 rc = osd_compat_init(o);
4509 GOTO(out_scrub, rc);
4511 rc = lu_site_init(&o->od_site, l);
4513 GOTO(out_compat, rc);
4514 o->od_site.ls_bottom_dev = l;
4516 rc = lu_site_init_finish(&o->od_site);
4520 rc = osd_procfs_init(o, o->od_svname);
4522 CERROR("%s: can't initialize procfs: rc = %d\n",
4527 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4529 /* initialize quota slave instance */
4530 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
4532 if (IS_ERR(o->od_quota_slave)) {
4533 rc = PTR_ERR(o->od_quota_slave);
4534 o->od_quota_slave = NULL;
4535 GOTO(out_procfs, rc);
4542 lu_site_fini(&o->od_site);
4546 osd_scrub_cleanup(env, o);
4548 osd_oi_fini(info, o);
4549 osd_shutdown(env, o);
4553 cleanup_capa_hash(o->od_capa_hash);
4558 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4559 struct lu_device_type *t,
4560 struct lustre_cfg *cfg)
4562 struct osd_device *o;
4567 return ERR_PTR(-ENOMEM);
4569 rc = dt_device_init(&o->od_dt_dev, t);
4571 rc = osd_device_init0(env, o, cfg);
4573 dt_device_fini(&o->od_dt_dev);
4576 if (unlikely(rc != 0))
4579 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4582 static struct lu_device *osd_device_free(const struct lu_env *env,
4583 struct lu_device *d)
4585 struct osd_device *o = osd_dev(d);
4588 cleanup_capa_hash(o->od_capa_hash);
4589 /* XXX: make osd top device in order to release reference */
4590 d->ld_site->ls_top_dev = d;
4591 lu_site_purge(env, d->ld_site, -1);
4592 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4593 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4594 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4596 lu_site_fini(&o->od_site);
4597 dt_device_fini(&o->od_dt_dev);
4602 static int osd_process_config(const struct lu_env *env,
4603 struct lu_device *d, struct lustre_cfg *cfg)
4605 struct osd_device *o = osd_dev(d);
4609 switch(cfg->lcfg_command) {
4611 err = osd_mount(env, o, cfg);
4614 lu_dev_del_linkage(d->ld_site, d);
4615 err = osd_shutdown(env, o);
4624 static int osd_recovery_complete(const struct lu_env *env,
4625 struct lu_device *d)
4631 * we use exports to track all osd users
4633 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4634 struct obd_device *obd, struct obd_uuid *cluuid,
4635 struct obd_connect_data *data, void *localdata)
4637 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4638 struct lustre_handle conn;
4642 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4644 rc = class_connect(&conn, obd, cluuid);
4648 *exp = class_conn2export(&conn);
4650 cfs_spin_lock(&osd->od_osfs_lock);
4652 cfs_spin_unlock(&osd->od_osfs_lock);
4658 * once last export (we don't count self-export) disappeared
4659 * osd can be released
4661 static int osd_obd_disconnect(struct obd_export *exp)
4663 struct obd_device *obd = exp->exp_obd;
4664 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4665 int rc, release = 0;
4668 /* Only disconnect the underlying layers on the final disconnect. */
4669 cfs_spin_lock(&osd->od_osfs_lock);
4671 if (osd->od_connects == 0)
4673 cfs_spin_unlock(&osd->od_osfs_lock);
4675 rc = class_disconnect(exp); /* bz 9811 */
4677 if (rc == 0 && release)
4678 class_manual_cleanup(obd);
4682 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4683 struct lu_device *dev)
4685 struct osd_device *osd = osd_dev(dev);
4689 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
4690 /* MDT/MDD still use old infrastructure to create
4692 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4698 if (osd->od_quota_slave != NULL)
4699 /* set up quota slave objects */
4700 result = qsd_prepare(env, osd->od_quota_slave);
4705 static const struct lu_object_operations osd_lu_obj_ops = {
4706 .loo_object_init = osd_object_init,
4707 .loo_object_delete = osd_object_delete,
4708 .loo_object_release = osd_object_release,
4709 .loo_object_free = osd_object_free,
4710 .loo_object_print = osd_object_print,
4711 .loo_object_invariant = osd_object_invariant
4714 const struct lu_device_operations osd_lu_ops = {
4715 .ldo_object_alloc = osd_object_alloc,
4716 .ldo_process_config = osd_process_config,
4717 .ldo_recovery_complete = osd_recovery_complete,
4718 .ldo_prepare = osd_prepare,
4721 static const struct lu_device_type_operations osd_device_type_ops = {
4722 .ldto_init = osd_type_init,
4723 .ldto_fini = osd_type_fini,
4725 .ldto_start = osd_type_start,
4726 .ldto_stop = osd_type_stop,
4728 .ldto_device_alloc = osd_device_alloc,
4729 .ldto_device_free = osd_device_free,
4731 .ldto_device_init = osd_device_init,
4732 .ldto_device_fini = osd_device_fini
4735 struct lu_device_type osd_device_type = {
4736 .ldt_tags = LU_DEVICE_DT,
4737 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4738 .ldt_ops = &osd_device_type_ops,
4739 .ldt_ctx_tags = LCT_LOCAL,
4743 * lprocfs legacy support.
4745 static struct obd_ops osd_obd_device_ops = {
4746 .o_owner = THIS_MODULE,
4747 .o_connect = osd_obd_connect,
4748 .o_disconnect = osd_obd_disconnect
4751 static int __init osd_mod_init(void)
4753 struct lprocfs_static_vars lvars;
4756 lprocfs_osd_init_vars(&lvars);
4757 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4758 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4761 static void __exit osd_mod_exit(void)
4763 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4766 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4767 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4768 MODULE_LICENSE("GPL");
4770 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);