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 #include <lustre_quota.h>
77 #ifdef HAVE_LDISKFS_PDO
79 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
80 "ldiskfs with parallel directory operations");
85 static const char dot[] = ".";
86 static const char dotdot[] = "..";
87 static const char remote_obj_dir[] = "REM_OBJ_DIR";
89 static const struct lu_object_operations osd_lu_obj_ops;
90 static const struct dt_object_operations osd_obj_ops;
91 static const struct dt_object_operations osd_obj_ea_ops;
92 static const struct dt_object_operations osd_obj_otable_it_ops;
93 static const struct dt_index_operations osd_index_iam_ops;
94 static const struct dt_index_operations osd_index_ea_ops;
96 static int osd_has_index(const struct osd_object *obj)
98 return obj->oo_dt.do_index_ops != NULL;
101 static int osd_object_invariant(const struct lu_object *l)
103 return osd_invariant(osd_obj(l));
107 * Concurrency: doesn't matter
109 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
111 return osd_oti_get(env)->oti_r_locks > 0;
115 * Concurrency: doesn't matter
117 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
119 struct osd_thread_info *oti = osd_oti_get(env);
120 return oti->oti_w_locks > 0 && o->oo_owner == env;
124 * Concurrency: doesn't access mutable data
126 static int osd_root_get(const struct lu_env *env,
127 struct dt_device *dev, struct lu_fid *f)
129 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
134 * OSD object methods.
138 * Concurrency: no concurrent access is possible that early in object
141 static struct lu_object *osd_object_alloc(const struct lu_env *env,
142 const struct lu_object_header *hdr,
145 struct osd_object *mo;
151 l = &mo->oo_dt.do_lu;
152 dt_object_init(&mo->oo_dt, NULL, d);
153 if (osd_dev(d)->od_iop_mode)
154 mo->oo_dt.do_ops = &osd_obj_ea_ops;
156 mo->oo_dt.do_ops = &osd_obj_ops;
158 l->lo_ops = &osd_lu_obj_ops;
159 cfs_init_rwsem(&mo->oo_sem);
160 cfs_init_rwsem(&mo->oo_ext_idx_sem);
161 cfs_spin_lock_init(&mo->oo_guard);
168 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
169 struct lustre_mdt_attrs *lma)
173 dentry->d_inode = inode;
174 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
177 /* Check LMA compatibility */
178 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
179 CWARN("%.16s: unsupported incompat LMA feature(s) "
181 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
182 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
186 lustre_lma_swab(lma);
189 } else if (rc == 0) {
197 * retrieve object from backend ext fs.
199 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
200 struct osd_inode_id *id)
202 struct inode *inode = NULL;
204 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
206 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
207 id->oii_ino, PTR_ERR(inode));
208 } else if (id->oii_gen != OSD_OII_NOGEN &&
209 inode->i_generation != id->oii_gen) {
210 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
212 id->oii_ino, id->oii_gen, inode->i_generation);
214 inode = ERR_PTR(-ESTALE);
215 } else if (inode->i_nlink == 0) {
216 /* due to parallel readdir and unlink,
217 * we can have dead inode here. */
218 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
219 make_bad_inode(inode);
221 inode = ERR_PTR(-ESTALE);
222 } else if (is_bad_inode(inode)) {
223 CWARN("%.16s: bad inode: ino = %u\n",
224 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
226 inode = ERR_PTR(-ENOENT);
228 if (id->oii_gen == OSD_OII_NOGEN)
229 osd_id_gen(id, inode->i_ino, inode->i_generation);
231 /* Do not update file c/mtime in ldiskfs.
232 * NB: we don't have any lock to protect this because we don't
233 * have reference on osd_object now, but contention with
234 * another lookup + attr_set can't happen in the tiny window
235 * between if (...) and set S_NOCMTIME. */
236 if (!(inode->i_flags & S_NOCMTIME))
237 inode->i_flags |= S_NOCMTIME;
242 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
243 struct osd_inode_id *id, struct lu_fid *fid)
245 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
249 inode = osd_iget(info, dev, id);
253 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
255 *fid = lma->lma_self_fid;
256 } else if (rc == -ENODATA) {
257 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
265 static struct inode *
266 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
267 struct osd_inode_id *id, const struct lu_fid *fid)
269 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
273 inode = osd_iget(info, dev, id);
277 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
286 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
287 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
288 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
290 return ERR_PTR(EREMCHG);
296 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
297 const struct lu_fid *fid,
298 const struct lu_object_conf *conf)
300 struct osd_thread_info *info;
301 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
302 struct osd_device *dev;
303 struct osd_idmap_cache *oic;
304 struct osd_inode_id *id;
306 struct osd_scrub *scrub;
307 struct scrub_file *sf;
312 LINVRNT(osd_invariant(obj));
313 LASSERT(obj->oo_inode == NULL);
314 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
317 scrub = &dev->od_scrub;
318 sf = &scrub->os_file;
319 info = osd_oti_get(env);
321 oic = &info->oti_cache;
324 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
327 /* Search order: 1. per-thread cache. */
328 if (lu_fid_eq(fid, &oic->oic_fid)) {
330 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
331 /* Search order: 2. OI scrub pending list. */
332 result = osd_oii_lookup(dev, fid, id);
337 if (sf->sf_flags & SF_INCONSISTENT)
341 * Objects are created as locking anchors or place holders for objects
342 * yet to be created. No need to osd_oi_lookup() at here because FID
343 * shouldn't never be re-used, if it's really a duplicate FID from
344 * unexpected reason, we should be able to detect it later by calling
345 * do_create->osd_oi_insert()
347 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
348 GOTO(out, result = 0);
350 /* Search order: 3. OI files. */
351 result = osd_oi_lookup(info, dev, fid, id);
352 if (result == -ENOENT) {
353 if (!fid_is_norm(fid) ||
354 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
356 GOTO(out, result = 0);
366 inode = osd_iget(info, dev, id);
368 inode = osd_iget_verify(info, dev, id, fid);
370 result = PTR_ERR(inode);
371 if (result == -ENOENT || result == -ESTALE) {
372 fid_zero(&oic->oic_fid);
374 } else if (result == -EREMCHG) {
377 if (thread_is_running(&scrub->os_thread)) {
378 result = -EINPROGRESS;
379 } else if (!dev->od_noscrub) {
380 result = osd_scrub_start(dev);
381 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
382 "for "DFID", rc = %d [1]\n",
383 LDISKFS_SB(osd_sb(dev))->s_es->\
384 s_volume_name,PFID(fid), result);
385 if (result == 0 || result == -EALREADY)
386 result = -EINPROGRESS;
395 obj->oo_inode = inode;
396 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
397 if (dev->od_iop_mode) {
398 obj->oo_compat_dot_created = 1;
399 obj->oo_compat_dotdot_created = 1;
402 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
403 GOTO(out, result = 0);
405 LASSERT(obj->oo_hl_head == NULL);
406 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
407 if (obj->oo_hl_head == NULL) {
408 obj->oo_inode = NULL;
410 GOTO(out, result = -ENOMEM);
412 GOTO(out, result = 0);
415 LINVRNT(osd_invariant(obj));
420 * Concurrency: shouldn't matter.
422 static void osd_object_init0(struct osd_object *obj)
424 LASSERT(obj->oo_inode != NULL);
425 obj->oo_dt.do_body_ops = &osd_body_ops;
426 obj->oo_dt.do_lu.lo_header->loh_attr |=
427 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
431 * Concurrency: no concurrent access is possible that early in object
434 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
435 const struct lu_object_conf *conf)
437 struct osd_object *obj = osd_obj(l);
440 LINVRNT(osd_invariant(obj));
442 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
443 obj->oo_dt.do_body_ops = &osd_body_ops_new;
445 if (obj->oo_inode != NULL) {
446 osd_object_init0(obj);
447 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
448 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
449 /* LFSCK iterator object is special without inode */
450 l->lo_header->loh_attr |= LOHA_EXISTS;
453 LINVRNT(osd_invariant(obj));
458 * Concurrency: no concurrent access is possible that late in object
461 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
463 struct osd_object *obj = osd_obj(l);
465 LINVRNT(osd_invariant(obj));
467 dt_object_fini(&obj->oo_dt);
468 if (obj->oo_hl_head != NULL)
469 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
474 * Concurrency: no concurrent access is possible that late in object
477 static void osd_index_fini(struct osd_object *o)
479 struct iam_container *bag;
481 if (o->oo_dir != NULL) {
482 bag = &o->oo_dir->od_container;
483 if (o->oo_inode != NULL) {
484 if (bag->ic_object == o->oo_inode)
485 iam_container_fini(bag);
487 OBD_FREE_PTR(o->oo_dir);
493 * Concurrency: no concurrent access is possible that late in object
494 * life-cycle (for all existing callers, that is. New callers have to provide
495 * their own locking.)
497 static int osd_inode_unlinked(const struct inode *inode)
499 return inode->i_nlink == 0;
503 OSD_TXN_OI_DELETE_CREDITS = 20,
504 OSD_TXN_INODE_DELETE_CREDITS = 20
511 #if OSD_THANDLE_STATS
513 * Set time when the handle is allocated
515 static void osd_th_alloced(struct osd_thandle *oth)
517 oth->oth_alloced = cfs_time_current();
521 * Set time when the handle started
523 static void osd_th_started(struct osd_thandle *oth)
525 oth->oth_started = cfs_time_current();
529 * Helper function to convert time interval to microseconds packed in
530 * long int (default time units for the counter in "stats" initialized
531 * by lu_time_init() )
533 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
537 cfs_duration_usec(cfs_time_sub(end, start), &val);
538 return val.tv_sec * 1000000 + val.tv_usec;
542 * Check whether the we deal with this handle for too long.
544 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
545 cfs_time_t alloced, cfs_time_t started,
548 cfs_time_t now = cfs_time_current();
550 LASSERT(dev != NULL);
552 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
553 interval_to_usec(alloced, started));
554 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
555 interval_to_usec(started, closed));
556 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
557 interval_to_usec(closed, now));
559 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
560 CWARN("transaction handle %p was open for too long: "
562 "alloced "CFS_TIME_T" ,"
563 "started "CFS_TIME_T" ,"
564 "closed "CFS_TIME_T"\n",
565 oth, now, alloced, started, closed);
566 libcfs_debug_dumpstack(NULL);
570 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
572 cfs_time_t __closed = cfs_time_current(); \
573 cfs_time_t __alloced = oth->oth_alloced; \
574 cfs_time_t __started = oth->oth_started; \
577 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
580 #else /* OSD_THANDLE_STATS */
582 #define osd_th_alloced(h) do {} while(0)
583 #define osd_th_started(h) do {} while(0)
584 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
586 #endif /* OSD_THANDLE_STATS */
589 * Concurrency: doesn't access mutable data.
591 static int osd_param_is_sane(const struct osd_device *dev,
592 const struct thandle *th)
594 struct osd_thandle *oh;
595 oh = container_of0(th, struct osd_thandle, ot_super);
596 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
600 * Concurrency: shouldn't matter.
602 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
603 static void osd_trans_commit_cb(struct super_block *sb,
604 struct journal_callback *jcb, int error)
606 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
609 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
610 struct thandle *th = &oh->ot_super;
611 struct lu_device *lud = &th->th_dev->dd_lu_dev;
612 struct dt_txn_commit_cb *dcb, *tmp;
614 LASSERT(oh->ot_handle == NULL);
617 CERROR("transaction @0x%p commit error: %d\n", th, error);
619 dt_txn_hook_commit(th);
621 /* call per-transaction callbacks if any */
622 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
623 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
624 "commit callback entry: magic=%x name='%s'\n",
625 dcb->dcb_magic, dcb->dcb_name);
626 cfs_list_del_init(&dcb->dcb_linkage);
627 dcb->dcb_func(NULL, th, dcb, error);
630 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
634 lu_context_exit(&th->th_ctx);
635 lu_context_fini(&th->th_ctx);
639 static struct thandle *osd_trans_create(const struct lu_env *env,
642 struct osd_thread_info *oti = osd_oti_get(env);
643 struct osd_iobuf *iobuf = &oti->oti_iobuf;
644 struct osd_thandle *oh;
648 /* on pending IO in this thread should left from prev. request */
649 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
651 th = ERR_PTR(-ENOMEM);
652 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
654 oh->ot_quota_trans = &oti->oti_quota_trans;
655 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
659 th->th_tags = LCT_TX_HANDLE;
661 oti->oti_dev = osd_dt_dev(d);
662 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
669 * Concurrency: shouldn't matter.
671 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
674 struct osd_thread_info *oti = osd_oti_get(env);
675 struct osd_device *dev = osd_dt_dev(d);
677 struct osd_thandle *oh;
682 LASSERT(current->journal_info == NULL);
684 oh = container_of0(th, struct osd_thandle, ot_super);
686 LASSERT(oh->ot_handle == NULL);
688 rc = dt_txn_hook_start(env, d, th);
692 if (!osd_param_is_sane(dev, th)) {
693 CWARN("%.16s: too many transaction credits (%d > %d)\n",
694 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
696 osd_journal(dev)->j_max_transaction_buffers);
697 /* XXX Limit the credits to 'max_transaction_buffers', and
698 * let the underlying filesystem to catch the error if
699 * we really need so many credits.
701 * This should be removed when we can calculate the
702 * credits precisely. */
703 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
704 #ifdef OSD_TRACK_DECLARES
705 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
706 oh->ot_declare_attr_set, oh->ot_declare_punch,
707 oh->ot_declare_xattr_set);
708 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
709 oh->ot_declare_create, oh->ot_declare_ref_add,
710 oh->ot_declare_ref_del, oh->ot_declare_write);
711 CERROR(" insert: %d, delete: %d, destroy: %d\n",
712 oh->ot_declare_insert, oh->ot_declare_delete,
713 oh->ot_declare_destroy);
718 * XXX temporary stuff. Some abstraction layer should
721 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
725 LASSERT(oti->oti_txns == 0);
726 lu_context_init(&th->th_ctx, th->th_tags);
727 lu_context_enter(&th->th_ctx);
729 lu_device_get(&d->dd_lu_dev);
730 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
742 * Concurrency: shouldn't matter.
744 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
747 struct osd_thandle *oh;
748 struct osd_thread_info *oti = osd_oti_get(env);
749 struct osd_iobuf *iobuf = &oti->oti_iobuf;
750 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
753 oh = container_of0(th, struct osd_thandle, ot_super);
756 /* inform the quota slave device that the transaction is
758 qsd_op_end(env, qsd, oh->ot_quota_trans);
759 oh->ot_quota_trans = NULL;
761 if (oh->ot_handle != NULL) {
762 handle_t *hdl = oh->ot_handle;
765 * add commit callback
766 * notice we don't do this in osd_trans_start()
767 * as underlying transaction can change during truncate
769 osd_journal_callback_set(hdl, osd_trans_commit_cb,
772 LASSERT(oti->oti_txns == 1);
774 rc = dt_txn_hook_stop(env, th);
776 CERROR("Failure in transaction hook: %d\n", rc);
778 /* hook functions might modify th_sync */
779 hdl->h_sync = th->th_sync;
781 oh->ot_handle = NULL;
782 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
783 rc = ldiskfs_journal_stop(hdl));
785 CERROR("Failure to stop transaction: %d\n", rc);
790 /* as we want IO to journal and data IO be concurrent, we don't block
791 * awaiting data IO completion in osd_do_bio(), instead we wait here
792 * once transaction is submitted to the journal. all reqular requests
793 * don't do direct IO (except read/write), thus this wait_event becomes
796 * IMPORTANT: we have to wait till any IO submited by the thread is
797 * completed otherwise iobuf may be corrupted by different request
799 cfs_wait_event(iobuf->dr_wait,
800 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
802 rc = iobuf->dr_error;
807 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
809 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
812 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
813 LASSERT(&dcb->dcb_func != NULL);
814 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
820 * Called just before object is freed. Releases all resources except for
821 * object itself (that is released by osd_object_free()).
823 * Concurrency: no concurrent access is possible that late in object
826 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
828 struct osd_object *obj = osd_obj(l);
829 struct inode *inode = obj->oo_inode;
831 LINVRNT(osd_invariant(obj));
834 * If object is unlinked remove fid->ino mapping from object index.
839 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
840 qid_t uid = inode->i_uid;
841 qid_t gid = inode->i_gid;
844 obj->oo_inode = NULL;
847 struct osd_thread_info *info = osd_oti_get(env);
848 struct lquota_id_info *qi = &info->oti_qi;
850 /* Release granted quota to master if necessary */
851 qi->lqi_id.qid_uid = uid;
852 qsd_adjust_quota(env, qsd, &qi->lqi_id, USRQUOTA);
854 qi->lqi_id.qid_uid = gid;
855 qsd_adjust_quota(env, qsd, &qi->lqi_id, GRPQUOTA);
861 * Concurrency: ->loo_object_release() is called under site spin-lock.
863 static void osd_object_release(const struct lu_env *env,
869 * Concurrency: shouldn't matter.
871 static int osd_object_print(const struct lu_env *env, void *cookie,
872 lu_printer_t p, const struct lu_object *l)
874 struct osd_object *o = osd_obj(l);
877 if (o->oo_dir != NULL)
878 d = o->oo_dir->od_container.ic_descr;
881 return (*p)(env, cookie,
882 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
884 o->oo_inode ? o->oo_inode->i_ino : 0UL,
885 o->oo_inode ? o->oo_inode->i_generation : 0,
886 d ? d->id_ops->id_name : "plain");
890 * Concurrency: shouldn't matter.
892 int osd_statfs(const struct lu_env *env, struct dt_device *d,
893 struct obd_statfs *sfs)
895 struct osd_device *osd = osd_dt_dev(d);
896 struct super_block *sb = osd_sb(osd);
897 struct kstatfs *ksfs;
900 if (unlikely(osd->od_mnt == NULL))
903 /* osd_lproc.c call this without env, allocate ksfs for that case */
904 if (unlikely(env == NULL)) {
909 ksfs = &osd_oti_get(env)->oti_ksfs;
912 cfs_spin_lock(&osd->od_osfs_lock);
914 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
915 result = sb->s_op->statfs(sb->s_root, ksfs);
916 if (likely(result == 0)) { /* N.B. statfs can't really fail */
917 osd->od_osfs_age = cfs_time_current_64();
918 statfs_pack(&osd->od_statfs, ksfs);
919 if (sb->s_flags & MS_RDONLY)
920 sfs->os_state = OS_STATE_READONLY;
924 if (likely(result == 0))
925 *sfs = osd->od_statfs;
926 cfs_spin_unlock(&osd->od_osfs_lock);
928 if (unlikely(env == NULL))
935 * Estimate space needed for file creations. We assume the largest filename
936 * which is 2^64 - 1, hence a filename of 20 chars.
937 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
939 #ifdef __LDISKFS_DIR_REC_LEN
940 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
942 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
946 * Concurrency: doesn't access mutable data.
948 static void osd_conf_get(const struct lu_env *env,
949 const struct dt_device *dev,
950 struct dt_device_param *param)
952 struct super_block *sb = osd_sb(osd_dt_dev(dev));
955 * XXX should be taken from not-yet-existing fs abstraction layer.
957 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
958 param->ddp_max_name_len = LDISKFS_NAME_LEN;
959 param->ddp_max_nlink = LDISKFS_LINK_MAX;
960 param->ddp_block_shift = sb->s_blocksize_bits;
961 param->ddp_mount_type = LDD_MT_LDISKFS;
962 param->ddp_maxbytes = sb->s_maxbytes;
963 /* Overhead estimate should be fairly accurate, so we really take a tiny
964 * error margin which also avoids fragmenting the filesystem too much */
965 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
966 /* inode are statically allocated, so per-inode space consumption
967 * is the space consumed by the directory entry */
968 param->ddp_inodespace = PER_OBJ_USAGE;
969 /* per-fragment overhead to be used by the client code */
970 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
971 param->ddp_mntopts = 0;
972 if (test_opt(sb, XATTR_USER))
973 param->ddp_mntopts |= MNTOPT_USERXATTR;
974 if (test_opt(sb, POSIX_ACL))
975 param->ddp_mntopts |= MNTOPT_ACL;
977 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
978 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
979 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
982 param->ddp_max_ea_size = sb->s_blocksize;
987 * Helper function to get and fill the buffer with input values.
989 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
993 buf = &osd_oti_get(env)->oti_buf;
1000 * Concurrency: shouldn't matter.
1002 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1004 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1005 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1009 * Start commit for OSD device.
1011 * An implementation of dt_commit_async method for OSD device.
1012 * Asychronously starts underlayng fs sync and thereby a transaction
1015 * \param env environment
1016 * \param d dt device
1018 * \see dt_device_operations
1020 static int osd_commit_async(const struct lu_env *env,
1021 struct dt_device *d)
1023 struct super_block *s = osd_sb(osd_dt_dev(d));
1026 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1027 RETURN(s->s_op->sync_fs(s, 0));
1031 * Concurrency: shouldn't matter.
1034 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1036 struct super_block *sb = osd_sb(osd_dt_dev(d));
1040 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1042 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1047 * Concurrency: serialization provided by callers.
1049 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1050 int mode, unsigned long timeout, __u32 alg,
1051 struct lustre_capa_key *keys)
1053 struct osd_device *dev = osd_dt_dev(d);
1056 dev->od_fl_capa = mode;
1057 dev->od_capa_timeout = timeout;
1058 dev->od_capa_alg = alg;
1059 dev->od_capa_keys = keys;
1064 * Note: we do not count into QUOTA here.
1065 * If we mount with --data_journal we may need more.
1067 const int osd_dto_credits_noquota[DTO_NR] = {
1070 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1071 * SINGLEDATA_TRANS_BLOCKS(8)
1072 * XXX Note: maybe iam need more, since iam have more level than
1075 [DTO_INDEX_INSERT] = 16,
1076 [DTO_INDEX_DELETE] = 16,
1080 [DTO_INDEX_UPDATE] = 16,
1082 * Create a object. The same as create object in EXT3.
1083 * DATA_TRANS_BLOCKS(14) +
1084 * INDEX_EXTRA_BLOCKS(8) +
1085 * 3(inode bits, groups, GDT)
1087 [DTO_OBJECT_CREATE] = 25,
1089 * XXX: real credits to be fixed
1091 [DTO_OBJECT_DELETE] = 25,
1093 * Attr set credits (inode)
1095 [DTO_ATTR_SET_BASE] = 1,
1097 * Xattr set. The same as xattr of EXT3.
1098 * DATA_TRANS_BLOCKS(14)
1099 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1100 * are also counted in. Do not know why?
1102 [DTO_XATTR_SET] = 14,
1105 * credits for inode change during write.
1107 [DTO_WRITE_BASE] = 3,
1109 * credits for single block write.
1111 [DTO_WRITE_BLOCK] = 14,
1113 * Attr set credits for chown.
1114 * This is extra credits for setattr, and it is null without quota
1116 [DTO_ATTR_SET_CHOWN]= 0
1119 static const struct dt_device_operations osd_dt_ops = {
1120 .dt_root_get = osd_root_get,
1121 .dt_statfs = osd_statfs,
1122 .dt_trans_create = osd_trans_create,
1123 .dt_trans_start = osd_trans_start,
1124 .dt_trans_stop = osd_trans_stop,
1125 .dt_trans_cb_add = osd_trans_cb_add,
1126 .dt_conf_get = osd_conf_get,
1127 .dt_sync = osd_sync,
1129 .dt_commit_async = osd_commit_async,
1130 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1133 static void osd_object_read_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_read_nested(&obj->oo_sem, role);
1144 LASSERT(obj->oo_owner == NULL);
1148 static void osd_object_write_lock(const struct lu_env *env,
1149 struct dt_object *dt, unsigned role)
1151 struct osd_object *obj = osd_dt_obj(dt);
1152 struct osd_thread_info *oti = osd_oti_get(env);
1154 LINVRNT(osd_invariant(obj));
1156 LASSERT(obj->oo_owner != env);
1157 cfs_down_write_nested(&obj->oo_sem, role);
1159 LASSERT(obj->oo_owner == NULL);
1160 obj->oo_owner = env;
1164 static void osd_object_read_unlock(const struct lu_env *env,
1165 struct dt_object *dt)
1167 struct osd_object *obj = osd_dt_obj(dt);
1168 struct osd_thread_info *oti = osd_oti_get(env);
1170 LINVRNT(osd_invariant(obj));
1172 LASSERT(oti->oti_r_locks > 0);
1174 cfs_up_read(&obj->oo_sem);
1177 static void osd_object_write_unlock(const struct lu_env *env,
1178 struct dt_object *dt)
1180 struct osd_object *obj = osd_dt_obj(dt);
1181 struct osd_thread_info *oti = osd_oti_get(env);
1183 LINVRNT(osd_invariant(obj));
1185 LASSERT(obj->oo_owner == env);
1186 LASSERT(oti->oti_w_locks > 0);
1188 obj->oo_owner = NULL;
1189 cfs_up_write(&obj->oo_sem);
1192 static int osd_object_write_locked(const struct lu_env *env,
1193 struct dt_object *dt)
1195 struct osd_object *obj = osd_dt_obj(dt);
1197 LINVRNT(osd_invariant(obj));
1199 return obj->oo_owner == env;
1202 static int capa_is_sane(const struct lu_env *env,
1203 struct osd_device *dev,
1204 struct lustre_capa *capa,
1205 struct lustre_capa_key *keys)
1207 struct osd_thread_info *oti = osd_oti_get(env);
1208 struct lustre_capa *tcapa = &oti->oti_capa;
1209 struct obd_capa *oc;
1213 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1215 if (capa_is_expired(oc)) {
1216 DEBUG_CAPA(D_ERROR, capa, "expired");
1223 if (capa_is_expired_sec(capa)) {
1224 DEBUG_CAPA(D_ERROR, capa, "expired");
1228 cfs_spin_lock(&capa_lock);
1229 for (i = 0; i < 2; i++) {
1230 if (keys[i].lk_keyid == capa->lc_keyid) {
1231 oti->oti_capa_key = keys[i];
1235 cfs_spin_unlock(&capa_lock);
1238 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1242 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1246 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1247 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1251 oc = capa_add(dev->od_capa_hash, capa);
1257 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1258 struct lustre_capa *capa, __u64 opc)
1260 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1261 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1262 struct md_capainfo *ci;
1265 if (!dev->od_fl_capa)
1268 if (capa == BYPASS_CAPA)
1271 ci = md_capainfo(env);
1275 if (ci->mc_auth == LC_ID_NONE)
1279 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1283 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1284 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1289 if (!capa_opc_supported(capa, opc)) {
1290 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1294 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1295 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1302 static struct timespec *osd_inode_time(const struct lu_env *env,
1303 struct inode *inode, __u64 seconds)
1305 struct osd_thread_info *oti = osd_oti_get(env);
1306 struct timespec *t = &oti->oti_time;
1308 t->tv_sec = seconds;
1310 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1315 static void osd_inode_getattr(const struct lu_env *env,
1316 struct inode *inode, struct lu_attr *attr)
1318 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1319 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1320 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1322 attr->la_atime = LTIME_S(inode->i_atime);
1323 attr->la_mtime = LTIME_S(inode->i_mtime);
1324 attr->la_ctime = LTIME_S(inode->i_ctime);
1325 attr->la_mode = inode->i_mode;
1326 attr->la_size = i_size_read(inode);
1327 attr->la_blocks = inode->i_blocks;
1328 attr->la_uid = inode->i_uid;
1329 attr->la_gid = inode->i_gid;
1330 attr->la_flags = LDISKFS_I(inode)->i_flags;
1331 attr->la_nlink = inode->i_nlink;
1332 attr->la_rdev = inode->i_rdev;
1333 attr->la_blksize = 1 << inode->i_blkbits;
1334 attr->la_blkbits = inode->i_blkbits;
1337 static int osd_attr_get(const struct lu_env *env,
1338 struct dt_object *dt,
1339 struct lu_attr *attr,
1340 struct lustre_capa *capa)
1342 struct osd_object *obj = osd_dt_obj(dt);
1344 LASSERT(dt_object_exists(dt));
1345 LINVRNT(osd_invariant(obj));
1347 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1350 cfs_spin_lock(&obj->oo_guard);
1351 osd_inode_getattr(env, obj->oo_inode, attr);
1352 cfs_spin_unlock(&obj->oo_guard);
1356 static int osd_declare_attr_set(const struct lu_env *env,
1357 struct dt_object *dt,
1358 const struct lu_attr *attr,
1359 struct thandle *handle)
1361 struct osd_thandle *oh;
1362 struct osd_object *obj;
1363 struct osd_thread_info *info = osd_oti_get(env);
1364 struct lquota_id_info *qi = &info->oti_qi;
1370 LASSERT(dt != NULL);
1371 LASSERT(handle != NULL);
1373 obj = osd_dt_obj(dt);
1374 LASSERT(osd_invariant(obj));
1376 oh = container_of0(handle, struct osd_thandle, ot_super);
1377 LASSERT(oh->ot_handle == NULL);
1379 OSD_DECLARE_OP(oh, attr_set);
1380 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1382 if (attr == NULL || obj->oo_inode == NULL)
1385 bspace = obj->oo_inode->i_blocks;
1386 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1387 bspace = toqb(bspace);
1389 /* Changing ownership is always preformed by super user, it should not
1392 * We still need to call the osd_declare_qid() to calculate the journal
1393 * credits for updating quota accounting files and to trigger quota
1394 * space adjustment once the operation is completed.*/
1395 if ((attr->la_valid & LA_UID) != 0 &&
1396 attr->la_uid != obj->oo_inode->i_uid) {
1397 qi->lqi_type = USRQUOTA;
1399 /* inode accounting */
1400 qi->lqi_is_blk = false;
1402 /* one more inode for the new owner ... */
1403 qi->lqi_id.qid_uid = attr->la_uid;
1405 allocated = (attr->la_uid == 0) ? true : false;
1406 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1407 if (rc == -EDQUOT || rc == -EINPROGRESS)
1412 /* and one less inode for the current uid */
1413 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1415 rc = osd_declare_qid(env, oh, qi, true, NULL);
1416 if (rc == -EDQUOT || rc == -EINPROGRESS)
1421 /* block accounting */
1422 qi->lqi_is_blk = true;
1424 /* more blocks for the new owner ... */
1425 qi->lqi_id.qid_uid = attr->la_uid;
1426 qi->lqi_space = bspace;
1427 allocated = (attr->la_uid == 0) ? true : false;
1428 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1429 if (rc == -EDQUOT || rc == -EINPROGRESS)
1434 /* and finally less blocks for the current owner */
1435 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1436 qi->lqi_space = -bspace;
1437 rc = osd_declare_qid(env, oh, qi, true, NULL);
1438 if (rc == -EDQUOT || rc == -EINPROGRESS)
1444 if (attr->la_valid & LA_GID &&
1445 attr->la_gid != obj->oo_inode->i_gid) {
1446 qi->lqi_type = GRPQUOTA;
1448 /* inode accounting */
1449 qi->lqi_is_blk = false;
1451 /* one more inode for the new group owner ... */
1452 qi->lqi_id.qid_gid = attr->la_gid;
1454 allocated = (attr->la_gid == 0) ? true : false;
1455 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1456 if (rc == -EDQUOT || rc == -EINPROGRESS)
1461 /* and one less inode for the current gid */
1462 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1464 rc = osd_declare_qid(env, oh, qi, true, NULL);
1465 if (rc == -EDQUOT || rc == -EINPROGRESS)
1470 /* block accounting */
1471 qi->lqi_is_blk = true;
1473 /* more blocks for the new owner ... */
1474 qi->lqi_id.qid_gid = attr->la_gid;
1475 qi->lqi_space = bspace;
1476 allocated = (attr->la_gid == 0) ? true : false;
1477 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1478 if (rc == -EDQUOT || rc == -EINPROGRESS)
1483 /* and finally less blocks for the current owner */
1484 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1485 qi->lqi_space = -bspace;
1486 rc = osd_declare_qid(env, oh, qi, true, NULL);
1487 if (rc == -EDQUOT || rc == -EINPROGRESS)
1496 static int osd_inode_setattr(const struct lu_env *env,
1497 struct inode *inode, const struct lu_attr *attr)
1501 bits = attr->la_valid;
1503 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1505 if (bits & LA_ATIME)
1506 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1507 if (bits & LA_CTIME)
1508 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1509 if (bits & LA_MTIME)
1510 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1511 if (bits & LA_SIZE) {
1512 LDISKFS_I(inode)->i_disksize = attr->la_size;
1513 i_size_write(inode, attr->la_size);
1517 /* OSD should not change "i_blocks" which is used by quota.
1518 * "i_blocks" should be changed by ldiskfs only. */
1519 if (bits & LA_BLOCKS)
1520 inode->i_blocks = attr->la_blocks;
1523 inode->i_mode = (inode->i_mode & S_IFMT) |
1524 (attr->la_mode & ~S_IFMT);
1526 inode->i_uid = attr->la_uid;
1528 inode->i_gid = attr->la_gid;
1529 if (bits & LA_NLINK)
1530 inode->i_nlink = attr->la_nlink;
1532 inode->i_rdev = attr->la_rdev;
1534 if (bits & LA_FLAGS) {
1535 /* always keep S_NOCMTIME */
1536 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1542 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1544 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1545 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1550 if (attr->la_valid & LA_UID)
1551 iattr.ia_valid |= ATTR_UID;
1552 if (attr->la_valid & LA_GID)
1553 iattr.ia_valid |= ATTR_GID;
1554 iattr.ia_uid = attr->la_uid;
1555 iattr.ia_gid = attr->la_gid;
1557 rc = ll_vfs_dq_transfer(inode, &iattr);
1559 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1560 "enforcement enabled on the ldiskfs filesystem?",
1561 inode->i_sb->s_id, rc);
1568 static int osd_attr_set(const struct lu_env *env,
1569 struct dt_object *dt,
1570 const struct lu_attr *attr,
1571 struct thandle *handle,
1572 struct lustre_capa *capa)
1574 struct osd_object *obj = osd_dt_obj(dt);
1575 struct inode *inode;
1578 LASSERT(handle != NULL);
1579 LASSERT(dt_object_exists(dt));
1580 LASSERT(osd_invariant(obj));
1582 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1585 OSD_EXEC_OP(handle, attr_set);
1587 inode = obj->oo_inode;
1589 rc = osd_quota_transfer(inode, attr);
1593 cfs_spin_lock(&obj->oo_guard);
1594 rc = osd_inode_setattr(env, inode, attr);
1595 cfs_spin_unlock(&obj->oo_guard);
1598 inode->i_sb->s_op->dirty_inode(inode);
1602 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1603 struct osd_object *obj,
1604 const char *name, const int namelen)
1606 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1609 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1611 struct dt_allocation_hint *hint,
1615 struct osd_device *osd = osd_obj2dev(obj);
1616 struct osd_thandle *oth;
1617 struct dt_object *parent = NULL;
1618 struct inode *inode;
1620 LINVRNT(osd_invariant(obj));
1621 LASSERT(obj->oo_inode == NULL);
1622 LASSERT(obj->oo_hl_head == NULL);
1624 if (S_ISDIR(mode) && ldiskfs_pdo) {
1625 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1626 if (obj->oo_hl_head == NULL)
1630 oth = container_of(th, struct osd_thandle, ot_super);
1631 LASSERT(oth->ot_handle->h_transaction != NULL);
1633 if (hint && hint->dah_parent)
1634 parent = hint->dah_parent;
1636 inode = ldiskfs_create_inode(oth->ot_handle,
1637 parent ? osd_dt_obj(parent)->oo_inode :
1638 osd_sb(osd)->s_root->d_inode,
1640 if (!IS_ERR(inode)) {
1641 /* Do not update file c/mtime in ldiskfs.
1642 * NB: don't need any lock because no contention at this
1644 inode->i_flags |= S_NOCMTIME;
1645 inode->i_state |= I_LUSTRE_NOSCRUB;
1646 obj->oo_inode = inode;
1649 if (obj->oo_hl_head != NULL) {
1650 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1651 obj->oo_hl_head = NULL;
1653 result = PTR_ERR(inode);
1655 LINVRNT(osd_invariant(obj));
1663 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1664 struct lu_attr *attr,
1665 struct dt_allocation_hint *hint,
1666 struct dt_object_format *dof,
1670 struct osd_thandle *oth;
1671 struct osd_device *osd = osd_obj2dev(obj);
1672 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1674 LASSERT(S_ISDIR(attr->la_mode));
1676 oth = container_of(th, struct osd_thandle, ot_super);
1677 LASSERT(oth->ot_handle->h_transaction != NULL);
1678 result = osd_mkfile(info, obj, mode, hint, th);
1679 if (result == 0 && osd->od_iop_mode == 0) {
1680 LASSERT(obj->oo_inode != NULL);
1682 * XXX uh-oh... call low-level iam function directly.
1685 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1686 sizeof (struct osd_fid_pack),
1692 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1693 struct lu_attr *attr,
1694 struct dt_allocation_hint *hint,
1695 struct dt_object_format *dof,
1699 struct osd_thandle *oth;
1700 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1702 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1704 LASSERT(S_ISREG(attr->la_mode));
1706 oth = container_of(th, struct osd_thandle, ot_super);
1707 LASSERT(oth->ot_handle->h_transaction != NULL);
1709 result = osd_mkfile(info, obj, mode, hint, th);
1711 LASSERT(obj->oo_inode != NULL);
1712 if (feat->dif_flags & DT_IND_VARKEY)
1713 result = iam_lvar_create(obj->oo_inode,
1714 feat->dif_keysize_max,
1716 feat->dif_recsize_max,
1719 result = iam_lfix_create(obj->oo_inode,
1720 feat->dif_keysize_max,
1722 feat->dif_recsize_max,
1729 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1730 struct lu_attr *attr,
1731 struct dt_allocation_hint *hint,
1732 struct dt_object_format *dof,
1735 LASSERT(S_ISREG(attr->la_mode));
1736 return osd_mkfile(info, obj, (attr->la_mode &
1737 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1740 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1741 struct lu_attr *attr,
1742 struct dt_allocation_hint *hint,
1743 struct dt_object_format *dof,
1746 LASSERT(S_ISLNK(attr->la_mode));
1747 return osd_mkfile(info, obj, (attr->la_mode &
1748 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1751 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1752 struct lu_attr *attr,
1753 struct dt_allocation_hint *hint,
1754 struct dt_object_format *dof,
1757 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1760 LINVRNT(osd_invariant(obj));
1761 LASSERT(obj->oo_inode == NULL);
1762 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1763 S_ISFIFO(mode) || S_ISSOCK(mode));
1765 result = osd_mkfile(info, obj, mode, hint, th);
1767 LASSERT(obj->oo_inode != NULL);
1769 * This inode should be marked dirty for i_rdev. Currently
1770 * that is done in the osd_attr_init().
1772 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1774 LINVRNT(osd_invariant(obj));
1778 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1780 struct dt_allocation_hint *hint,
1781 struct dt_object_format *dof,
1784 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1786 osd_obj_type_f result;
1802 result = osd_mk_index;
1813 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1814 struct dt_object *parent, struct dt_object *child,
1815 cfs_umode_t child_mode)
1819 memset(ah, 0, sizeof(*ah));
1820 ah->dah_parent = parent;
1821 ah->dah_mode = child_mode;
1824 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1825 struct lu_attr *attr, struct dt_object_format *dof)
1827 struct inode *inode = obj->oo_inode;
1828 __u64 valid = attr->la_valid;
1831 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1833 if (dof->dof_type != DFT_NODE)
1834 attr->la_valid &= ~LA_RDEV;
1835 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1836 attr->la_valid &= ~LA_ATIME;
1837 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1838 attr->la_valid &= ~LA_CTIME;
1839 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1840 attr->la_valid &= ~LA_MTIME;
1842 result = osd_quota_transfer(inode, attr);
1846 if (attr->la_valid != 0) {
1847 result = osd_inode_setattr(info->oti_env, inode, attr);
1849 * The osd_inode_setattr() should always succeed here. The
1850 * only error that could be returned is EDQUOT when we are
1851 * trying to change the UID or GID of the inode. However, this
1852 * should not happen since quota enforcement is no longer
1853 * enabled on ldiskfs (lquota takes care of it).
1855 LASSERTF(result == 0, "%d", result);
1856 inode->i_sb->s_op->dirty_inode(inode);
1859 attr->la_valid = valid;
1863 * Helper function for osd_object_create()
1865 * \retval 0, on success
1867 static int __osd_object_create(struct osd_thread_info *info,
1868 struct osd_object *obj, struct lu_attr *attr,
1869 struct dt_allocation_hint *hint,
1870 struct dt_object_format *dof,
1876 /* we drop umask so that permissions we pass are not affected */
1877 umask = current->fs->umask;
1878 current->fs->umask = 0;
1880 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1883 osd_attr_init(info, obj, attr, dof);
1884 osd_object_init0(obj);
1886 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1887 unlock_new_inode(obj->oo_inode);
1890 /* restore previous umask value */
1891 current->fs->umask = umask;
1897 * Helper function for osd_object_create()
1899 * \retval 0, on success
1901 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1902 const struct lu_fid *fid, struct thandle *th)
1904 struct osd_thread_info *info = osd_oti_get(env);
1905 struct osd_inode_id *id = &info->oti_id;
1906 struct osd_device *osd = osd_obj2dev(obj);
1908 LASSERT(obj->oo_inode != NULL);
1910 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1911 return osd_oi_insert(info, osd, fid, id, th);
1914 static int osd_declare_object_create(const struct lu_env *env,
1915 struct dt_object *dt,
1916 struct lu_attr *attr,
1917 struct dt_allocation_hint *hint,
1918 struct dt_object_format *dof,
1919 struct thandle *handle)
1921 struct osd_thandle *oh;
1925 LASSERT(handle != NULL);
1927 oh = container_of0(handle, struct osd_thandle, ot_super);
1928 LASSERT(oh->ot_handle == NULL);
1930 OSD_DECLARE_OP(oh, create);
1931 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1932 /* XXX: So far, only normal fid needs be inserted into the oi,
1933 * things could be changed later. Revise following code then. */
1934 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1935 OSD_DECLARE_OP(oh, insert);
1936 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1937 /* Reuse idle OI block may cause additional one OI block
1939 oh->ot_credits += 1;
1941 /* If this is directory, then we expect . and .. to be inserted as
1942 * well. The one directory block always needs to be created for the
1943 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1944 * block), there is no danger of needing a tree for the first block.
1946 if (attr && S_ISDIR(attr->la_mode)) {
1947 OSD_DECLARE_OP(oh, insert);
1948 OSD_DECLARE_OP(oh, insert);
1949 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1955 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
1956 false, false, NULL, false);
1960 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1961 struct lu_attr *attr,
1962 struct dt_allocation_hint *hint,
1963 struct dt_object_format *dof,
1966 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1967 struct osd_object *obj = osd_dt_obj(dt);
1968 struct osd_thread_info *info = osd_oti_get(env);
1973 LINVRNT(osd_invariant(obj));
1974 LASSERT(!dt_object_exists(dt));
1975 LASSERT(osd_write_locked(env, obj));
1976 LASSERT(th != NULL);
1978 if (unlikely(fid_is_acct(fid)))
1979 /* Quota files can't be created from the kernel any more,
1980 * 'tune2fs -O quota' will take care of creating them */
1983 OSD_EXEC_OP(th, create);
1985 result = __osd_object_create(info, obj, attr, hint, dof, th);
1987 result = __osd_oi_insert(env, obj, fid, th);
1989 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1990 LASSERT(osd_invariant(obj));
1995 * Called to destroy on-disk representation of the object
1997 * Concurrency: must be locked
1999 static int osd_declare_object_destroy(const struct lu_env *env,
2000 struct dt_object *dt,
2003 struct osd_object *obj = osd_dt_obj(dt);
2004 struct inode *inode = obj->oo_inode;
2005 struct osd_thandle *oh;
2009 oh = container_of0(th, struct osd_thandle, ot_super);
2010 LASSERT(oh->ot_handle == NULL);
2013 OSD_DECLARE_OP(oh, destroy);
2014 OSD_DECLARE_OP(oh, delete);
2015 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2016 /* XXX: So far, only normal fid needs to be inserted into the OI,
2017 * so only normal fid needs to be removed from the OI also. */
2018 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
2019 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2020 /* Recycle idle OI leaf may cause additional three OI blocks
2022 oh->ot_credits += 3;
2025 /* one less inode */
2026 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2027 false, true, NULL, false);
2030 /* data to be truncated */
2031 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh, true,
2036 static int osd_object_destroy(const struct lu_env *env,
2037 struct dt_object *dt,
2040 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2041 struct osd_object *obj = osd_dt_obj(dt);
2042 struct inode *inode = obj->oo_inode;
2043 struct osd_device *osd = osd_obj2dev(obj);
2044 struct osd_thandle *oh;
2048 oh = container_of0(th, struct osd_thandle, ot_super);
2049 LASSERT(oh->ot_handle);
2051 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2053 if (unlikely(fid_is_acct(fid)))
2056 /* Parallel control for OI scrub. For most of cases, there is no
2057 * lock contention. So it will not affect unlink performance. */
2058 cfs_mutex_lock(&inode->i_mutex);
2059 if (S_ISDIR(inode->i_mode)) {
2060 LASSERT(osd_inode_unlinked(inode) ||
2061 inode->i_nlink == 1);
2062 cfs_spin_lock(&obj->oo_guard);
2064 cfs_spin_unlock(&obj->oo_guard);
2065 inode->i_sb->s_op->dirty_inode(inode);
2067 LASSERT(osd_inode_unlinked(inode));
2070 OSD_EXEC_OP(th, destroy);
2072 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2073 cfs_mutex_unlock(&inode->i_mutex);
2075 /* XXX: add to ext3 orphan list */
2076 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2078 /* not needed in the cache anymore */
2079 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2085 * Helper function for osd_xattr_set()
2087 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2088 const struct lu_buf *buf, const char *name, int fl)
2090 struct osd_object *obj = osd_dt_obj(dt);
2091 struct inode *inode = obj->oo_inode;
2092 struct osd_thread_info *info = osd_oti_get(env);
2093 struct dentry *dentry = &info->oti_child_dentry;
2097 LASSERT(dt_object_exists(dt));
2098 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2100 if (fl & LU_XATTR_REPLACE)
2101 fs_flags |= XATTR_REPLACE;
2103 if (fl & LU_XATTR_CREATE)
2104 fs_flags |= XATTR_CREATE;
2106 dentry->d_inode = inode;
2107 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2108 buf->lb_len, fs_flags);
2113 * Put the fid into lustre_mdt_attrs, and then place the structure
2114 * inode's ea. This fid should not be altered during the life time
2117 * \retval +ve, on success
2118 * \retval -ve, on error
2120 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2122 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2123 const struct lu_fid *fid)
2125 struct osd_thread_info *info = osd_oti_get(env);
2126 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2128 lustre_lma_init(mdt_attrs, fid);
2129 lustre_lma_swab(mdt_attrs);
2130 return __osd_xattr_set(env, dt,
2131 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2132 XATTR_NAME_LMA, LU_XATTR_CREATE);
2137 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2138 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2139 * To have compatilibility with 1.8 ldiskfs driver we need to have
2140 * magic number at start of fid data.
2141 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2144 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2145 const struct dt_rec *fid)
2147 param->edp_magic = LDISKFS_LUFID_MAGIC;
2148 param->edp_len = sizeof(struct lu_fid) + 1;
2150 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2151 (struct lu_fid *)fid);
2155 * Try to read the fid from inode ea into dt_rec, if return value
2156 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2158 * \param fid object fid.
2160 * \retval 0 on success
2162 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2163 __u32 ino, struct lu_fid *fid,
2164 struct osd_inode_id *id)
2166 struct osd_thread_info *info = osd_oti_get(env);
2167 struct inode *inode;
2170 osd_id_gen(id, ino, OSD_OII_NOGEN);
2171 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2173 RETURN(PTR_ERR(inode));
2180 * OSD layer object create function for interoperability mode (b11826).
2181 * This is mostly similar to osd_object_create(). Only difference being, fid is
2182 * inserted into inode ea here.
2184 * \retval 0, on success
2185 * \retval -ve, on error
2187 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2188 struct lu_attr *attr,
2189 struct dt_allocation_hint *hint,
2190 struct dt_object_format *dof,
2193 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2194 struct osd_object *obj = osd_dt_obj(dt);
2195 struct osd_thread_info *info = osd_oti_get(env);
2200 LASSERT(osd_invariant(obj));
2201 LASSERT(!dt_object_exists(dt));
2202 LASSERT(osd_write_locked(env, obj));
2203 LASSERT(th != NULL);
2205 if (unlikely(fid_is_acct(fid)))
2206 /* Quota files can't be created from the kernel any more,
2207 * 'tune2fs -O quota' will take care of creating them */
2210 OSD_EXEC_OP(th, create);
2212 result = __osd_object_create(info, obj, attr, hint, dof, th);
2213 /* objects under osd root shld have igif fid, so dont add fid EA */
2214 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2215 result = osd_ea_fid_set(env, dt, fid);
2218 result = __osd_oi_insert(env, obj, fid, th);
2220 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2221 LINVRNT(osd_invariant(obj));
2225 static int osd_declare_object_ref_add(const struct lu_env *env,
2226 struct dt_object *dt,
2227 struct thandle *handle)
2229 struct osd_thandle *oh;
2231 /* it's possible that object doesn't exist yet */
2232 LASSERT(handle != NULL);
2234 oh = container_of0(handle, struct osd_thandle, ot_super);
2235 LASSERT(oh->ot_handle == NULL);
2237 OSD_DECLARE_OP(oh, ref_add);
2238 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2244 * Concurrency: @dt is write locked.
2246 static int osd_object_ref_add(const struct lu_env *env,
2247 struct dt_object *dt, struct thandle *th)
2249 struct osd_object *obj = osd_dt_obj(dt);
2250 struct inode *inode = obj->oo_inode;
2252 LINVRNT(osd_invariant(obj));
2253 LASSERT(dt_object_exists(dt));
2254 LASSERT(osd_write_locked(env, obj));
2255 LASSERT(th != NULL);
2257 OSD_EXEC_OP(th, ref_add);
2260 * DIR_NLINK feature is set for compatibility reasons if:
2261 * 1) nlinks > LDISKFS_LINK_MAX, or
2262 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2264 * It is easier to always set this flag (rather than check and set),
2265 * since it has less overhead, and the superblock will be dirtied
2266 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2267 * do not actually care whether this flag is set or not.
2269 cfs_spin_lock(&obj->oo_guard);
2271 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2272 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2273 inode->i_nlink == 2)
2276 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2277 cfs_spin_unlock(&obj->oo_guard);
2278 inode->i_sb->s_op->dirty_inode(inode);
2279 LINVRNT(osd_invariant(obj));
2284 static int osd_declare_object_ref_del(const struct lu_env *env,
2285 struct dt_object *dt,
2286 struct thandle *handle)
2288 struct osd_thandle *oh;
2290 LASSERT(dt_object_exists(dt));
2291 LASSERT(handle != NULL);
2293 oh = container_of0(handle, struct osd_thandle, ot_super);
2294 LASSERT(oh->ot_handle == NULL);
2296 OSD_DECLARE_OP(oh, ref_del);
2297 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2303 * Concurrency: @dt is write locked.
2305 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2308 struct osd_object *obj = osd_dt_obj(dt);
2309 struct inode *inode = obj->oo_inode;
2311 LINVRNT(osd_invariant(obj));
2312 LASSERT(dt_object_exists(dt));
2313 LASSERT(osd_write_locked(env, obj));
2314 LASSERT(th != NULL);
2316 OSD_EXEC_OP(th, ref_del);
2318 cfs_spin_lock(&obj->oo_guard);
2319 LASSERT(inode->i_nlink > 0);
2321 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2322 * then the nlink count is 1. Don't let it be set to 0 or the directory
2323 * inode will be deleted incorrectly. */
2324 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2326 cfs_spin_unlock(&obj->oo_guard);
2327 inode->i_sb->s_op->dirty_inode(inode);
2328 LINVRNT(osd_invariant(obj));
2334 * Get the 64-bit version for an inode.
2336 static int osd_object_version_get(const struct lu_env *env,
2337 struct dt_object *dt, dt_obj_version_t *ver)
2339 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2341 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2342 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2343 *ver = LDISKFS_I(inode)->i_fs_version;
2348 * Concurrency: @dt is read locked.
2350 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2351 struct lu_buf *buf, const char *name,
2352 struct lustre_capa *capa)
2354 struct osd_object *obj = osd_dt_obj(dt);
2355 struct inode *inode = obj->oo_inode;
2356 struct osd_thread_info *info = osd_oti_get(env);
2357 struct dentry *dentry = &info->oti_obj_dentry;
2359 /* version get is not real XATTR but uses xattr API */
2360 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2361 /* for version we are just using xattr API but change inode
2363 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2364 osd_object_version_get(env, dt, buf->lb_buf);
2365 return sizeof(dt_obj_version_t);
2368 LASSERT(dt_object_exists(dt));
2369 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2371 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2374 dentry->d_inode = inode;
2375 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2379 static int osd_declare_xattr_set(const struct lu_env *env,
2380 struct dt_object *dt,
2381 const struct lu_buf *buf, const char *name,
2382 int fl, struct thandle *handle)
2384 struct osd_thandle *oh;
2386 LASSERT(handle != NULL);
2388 oh = container_of0(handle, struct osd_thandle, ot_super);
2389 LASSERT(oh->ot_handle == NULL);
2391 OSD_DECLARE_OP(oh, xattr_set);
2392 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2393 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2395 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2401 * Set the 64-bit version for object
2403 static void osd_object_version_set(const struct lu_env *env,
2404 struct dt_object *dt,
2405 dt_obj_version_t *new_version)
2407 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2409 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2410 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2412 LDISKFS_I(inode)->i_fs_version = *new_version;
2413 /** Version is set after all inode operations are finished,
2414 * so we should mark it dirty here */
2415 inode->i_sb->s_op->dirty_inode(inode);
2419 * Concurrency: @dt is write locked.
2421 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2422 const struct lu_buf *buf, const char *name, int fl,
2423 struct thandle *handle, struct lustre_capa *capa)
2425 LASSERT(handle != NULL);
2427 /* version set is not real XATTR */
2428 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2429 /* for version we are just using xattr API but change inode
2431 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2432 osd_object_version_set(env, dt, buf->lb_buf);
2433 return sizeof(dt_obj_version_t);
2436 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2439 OSD_EXEC_OP(handle, xattr_set);
2440 return __osd_xattr_set(env, dt, buf, name, fl);
2444 * Concurrency: @dt is read locked.
2446 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2447 struct lu_buf *buf, struct lustre_capa *capa)
2449 struct osd_object *obj = osd_dt_obj(dt);
2450 struct inode *inode = obj->oo_inode;
2451 struct osd_thread_info *info = osd_oti_get(env);
2452 struct dentry *dentry = &info->oti_obj_dentry;
2454 LASSERT(dt_object_exists(dt));
2455 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2456 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2458 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2461 dentry->d_inode = inode;
2462 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2465 static int osd_declare_xattr_del(const struct lu_env *env,
2466 struct dt_object *dt, const char *name,
2467 struct thandle *handle)
2469 struct osd_thandle *oh;
2471 LASSERT(dt_object_exists(dt));
2472 LASSERT(handle != NULL);
2474 oh = container_of0(handle, struct osd_thandle, ot_super);
2475 LASSERT(oh->ot_handle == NULL);
2477 OSD_DECLARE_OP(oh, xattr_set);
2478 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2484 * Concurrency: @dt is write locked.
2486 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2487 const char *name, struct thandle *handle,
2488 struct lustre_capa *capa)
2490 struct osd_object *obj = osd_dt_obj(dt);
2491 struct inode *inode = obj->oo_inode;
2492 struct osd_thread_info *info = osd_oti_get(env);
2493 struct dentry *dentry = &info->oti_obj_dentry;
2496 LASSERT(dt_object_exists(dt));
2497 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2498 LASSERT(osd_write_locked(env, obj));
2499 LASSERT(handle != NULL);
2501 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2504 OSD_EXEC_OP(handle, xattr_set);
2506 dentry->d_inode = inode;
2507 rc = inode->i_op->removexattr(dentry, name);
2511 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2512 struct dt_object *dt,
2513 struct lustre_capa *old,
2516 struct osd_thread_info *info = osd_oti_get(env);
2517 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2518 struct osd_object *obj = osd_dt_obj(dt);
2519 struct osd_device *dev = osd_obj2dev(obj);
2520 struct lustre_capa_key *key = &info->oti_capa_key;
2521 struct lustre_capa *capa = &info->oti_capa;
2522 struct obd_capa *oc;
2523 struct md_capainfo *ci;
2527 if (!dev->od_fl_capa)
2528 RETURN(ERR_PTR(-ENOENT));
2530 LASSERT(dt_object_exists(dt));
2531 LINVRNT(osd_invariant(obj));
2533 /* renewal sanity check */
2534 if (old && osd_object_auth(env, dt, old, opc))
2535 RETURN(ERR_PTR(-EACCES));
2537 ci = md_capainfo(env);
2539 RETURN(ERR_PTR(-ENOENT));
2541 switch (ci->mc_auth) {
2545 capa->lc_uid = obj->oo_inode->i_uid;
2546 capa->lc_gid = obj->oo_inode->i_gid;
2547 capa->lc_flags = LC_ID_PLAIN;
2549 case LC_ID_CONVERT: {
2552 s[0] = obj->oo_inode->i_uid;
2553 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2554 s[2] = obj->oo_inode->i_gid;
2555 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2556 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2558 RETURN(ERR_PTR(rc));
2560 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2561 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2562 capa->lc_flags = LC_ID_CONVERT;
2566 RETURN(ERR_PTR(-EINVAL));
2569 capa->lc_fid = *fid;
2571 capa->lc_flags |= dev->od_capa_alg << 24;
2572 capa->lc_timeout = dev->od_capa_timeout;
2573 capa->lc_expiry = 0;
2575 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2577 LASSERT(!capa_is_expired(oc));
2581 cfs_spin_lock(&capa_lock);
2582 *key = dev->od_capa_keys[1];
2583 cfs_spin_unlock(&capa_lock);
2585 capa->lc_keyid = key->lk_keyid;
2586 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2588 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2590 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2591 RETURN(ERR_PTR(rc));
2594 oc = capa_add(dev->od_capa_hash, capa);
2598 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2600 struct osd_object *obj = osd_dt_obj(dt);
2601 struct inode *inode = obj->oo_inode;
2602 struct osd_thread_info *info = osd_oti_get(env);
2603 struct dentry *dentry = &info->oti_obj_dentry;
2604 struct file *file = &info->oti_file;
2609 dentry->d_inode = inode;
2610 file->f_dentry = dentry;
2611 file->f_mapping = inode->i_mapping;
2612 file->f_op = inode->i_fop;
2613 mutex_lock(&inode->i_mutex);
2614 rc = file->f_op->fsync(file, dentry, 0);
2615 mutex_unlock(&inode->i_mutex);
2619 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2622 struct osd_object *obj = osd_dt_obj(dt);
2625 *data = (void *)obj->oo_inode;
2633 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2634 const struct dt_index_features *feat)
2636 struct iam_descr *descr;
2638 if (osd_object_is_root(o))
2639 return feat == &dt_directory_features;
2641 LASSERT(o->oo_dir != NULL);
2643 descr = o->oo_dir->od_container.ic_descr;
2644 if (feat == &dt_directory_features) {
2645 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2651 feat->dif_keysize_min <= descr->id_key_size &&
2652 descr->id_key_size <= feat->dif_keysize_max &&
2653 feat->dif_recsize_min <= descr->id_rec_size &&
2654 descr->id_rec_size <= feat->dif_recsize_max &&
2655 !(feat->dif_flags & (DT_IND_VARKEY |
2656 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2657 ergo(feat->dif_flags & DT_IND_UPDATE,
2658 1 /* XXX check that object (and file system) is
2663 static int osd_iam_container_init(const struct lu_env *env,
2664 struct osd_object *obj,
2665 struct osd_directory *dir)
2667 struct iam_container *bag = &dir->od_container;
2670 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2674 result = iam_container_setup(bag);
2676 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2678 iam_container_fini(bag);
2685 * Concurrency: no external locking is necessary.
2687 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2688 const struct dt_index_features *feat)
2692 struct osd_object *obj = osd_dt_obj(dt);
2693 struct osd_device *osd = osd_obj2dev(obj);
2695 LINVRNT(osd_invariant(obj));
2696 LASSERT(dt_object_exists(dt));
2698 if (osd_object_is_root(obj)) {
2699 dt->do_index_ops = &osd_index_ea_ops;
2701 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2702 dt->do_index_ops = &osd_index_ea_ops;
2703 if (S_ISDIR(obj->oo_inode->i_mode))
2708 } else if (unlikely(feat == &dt_otable_features)) {
2709 dt->do_index_ops = &osd_otable_ops;
2711 } else if (feat == &dt_acct_features) {
2712 dt->do_index_ops = &osd_acct_index_ops;
2715 } else if (!osd_has_index(obj)) {
2716 struct osd_directory *dir;
2721 cfs_spin_lock(&obj->oo_guard);
2722 if (obj->oo_dir == NULL)
2726 * Concurrent thread allocated container data.
2729 cfs_spin_unlock(&obj->oo_guard);
2731 * Now, that we have container data, serialize its
2734 cfs_down_write(&obj->oo_ext_idx_sem);
2736 * recheck under lock.
2738 if (!osd_has_index(obj))
2739 result = osd_iam_container_init(env, obj, dir);
2742 cfs_up_write(&obj->oo_ext_idx_sem);
2750 if (result == 0 && skip_iam == 0) {
2751 if (!osd_iam_index_probe(env, obj, feat))
2754 LINVRNT(osd_invariant(obj));
2756 if (is_quota_glb_feat(feat))
2757 result = osd_quota_migration(env, dt, feat);
2762 static int osd_otable_it_attr_get(const struct lu_env *env,
2763 struct dt_object *dt,
2764 struct lu_attr *attr,
2765 struct lustre_capa *capa)
2771 static const struct dt_object_operations osd_obj_ops = {
2772 .do_read_lock = osd_object_read_lock,
2773 .do_write_lock = osd_object_write_lock,
2774 .do_read_unlock = osd_object_read_unlock,
2775 .do_write_unlock = osd_object_write_unlock,
2776 .do_write_locked = osd_object_write_locked,
2777 .do_attr_get = osd_attr_get,
2778 .do_declare_attr_set = osd_declare_attr_set,
2779 .do_attr_set = osd_attr_set,
2780 .do_ah_init = osd_ah_init,
2781 .do_declare_create = osd_declare_object_create,
2782 .do_create = osd_object_create,
2783 .do_declare_destroy = osd_declare_object_destroy,
2784 .do_destroy = osd_object_destroy,
2785 .do_index_try = osd_index_try,
2786 .do_declare_ref_add = osd_declare_object_ref_add,
2787 .do_ref_add = osd_object_ref_add,
2788 .do_declare_ref_del = osd_declare_object_ref_del,
2789 .do_ref_del = osd_object_ref_del,
2790 .do_xattr_get = osd_xattr_get,
2791 .do_declare_xattr_set = osd_declare_xattr_set,
2792 .do_xattr_set = osd_xattr_set,
2793 .do_declare_xattr_del = osd_declare_xattr_del,
2794 .do_xattr_del = osd_xattr_del,
2795 .do_xattr_list = osd_xattr_list,
2796 .do_capa_get = osd_capa_get,
2797 .do_object_sync = osd_object_sync,
2798 .do_data_get = osd_data_get,
2802 * dt_object_operations for interoperability mode
2803 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2805 static const struct dt_object_operations osd_obj_ea_ops = {
2806 .do_read_lock = osd_object_read_lock,
2807 .do_write_lock = osd_object_write_lock,
2808 .do_read_unlock = osd_object_read_unlock,
2809 .do_write_unlock = osd_object_write_unlock,
2810 .do_write_locked = osd_object_write_locked,
2811 .do_attr_get = osd_attr_get,
2812 .do_declare_attr_set = osd_declare_attr_set,
2813 .do_attr_set = osd_attr_set,
2814 .do_ah_init = osd_ah_init,
2815 .do_declare_create = osd_declare_object_create,
2816 .do_create = osd_object_ea_create,
2817 .do_declare_destroy = osd_declare_object_destroy,
2818 .do_destroy = osd_object_destroy,
2819 .do_index_try = osd_index_try,
2820 .do_declare_ref_add = osd_declare_object_ref_add,
2821 .do_ref_add = osd_object_ref_add,
2822 .do_declare_ref_del = osd_declare_object_ref_del,
2823 .do_ref_del = osd_object_ref_del,
2824 .do_xattr_get = osd_xattr_get,
2825 .do_declare_xattr_set = osd_declare_xattr_set,
2826 .do_xattr_set = osd_xattr_set,
2827 .do_declare_xattr_del = osd_declare_xattr_del,
2828 .do_xattr_del = osd_xattr_del,
2829 .do_xattr_list = osd_xattr_list,
2830 .do_capa_get = osd_capa_get,
2831 .do_object_sync = osd_object_sync,
2832 .do_data_get = osd_data_get,
2835 static const struct dt_object_operations osd_obj_otable_it_ops = {
2836 .do_attr_get = osd_otable_it_attr_get,
2837 .do_index_try = osd_index_try,
2840 static int osd_index_declare_iam_delete(const struct lu_env *env,
2841 struct dt_object *dt,
2842 const struct dt_key *key,
2843 struct thandle *handle)
2845 struct osd_thandle *oh;
2847 oh = container_of0(handle, struct osd_thandle, ot_super);
2848 LASSERT(oh->ot_handle == NULL);
2850 OSD_DECLARE_OP(oh, delete);
2851 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2857 * delete a (key, value) pair from index \a dt specified by \a key
2859 * \param dt osd index object
2860 * \param key key for index
2861 * \param rec record reference
2862 * \param handle transaction handler
2865 * \retval -ve failure
2868 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2869 const struct dt_key *key,
2870 struct thandle *handle,
2871 struct lustre_capa *capa)
2873 struct osd_thread_info *oti = osd_oti_get(env);
2874 struct osd_object *obj = osd_dt_obj(dt);
2875 struct osd_thandle *oh;
2876 struct iam_path_descr *ipd;
2877 struct iam_container *bag = &obj->oo_dir->od_container;
2882 LINVRNT(osd_invariant(obj));
2883 LASSERT(dt_object_exists(dt));
2884 LASSERT(bag->ic_object == obj->oo_inode);
2885 LASSERT(handle != NULL);
2887 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2890 OSD_EXEC_OP(handle, delete);
2892 ipd = osd_idx_ipd_get(env, bag);
2893 if (unlikely(ipd == NULL))
2896 oh = container_of0(handle, struct osd_thandle, ot_super);
2897 LASSERT(oh->ot_handle != NULL);
2898 LASSERT(oh->ot_handle->h_transaction != NULL);
2900 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2901 /* swab quota uid/gid provided by caller */
2902 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2903 key = (const struct dt_key *)&oti->oti_quota_id;
2906 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2907 osd_ipd_put(env, bag, ipd);
2908 LINVRNT(osd_invariant(obj));
2912 static int osd_index_declare_ea_delete(const struct lu_env *env,
2913 struct dt_object *dt,
2914 const struct dt_key *key,
2915 struct thandle *handle)
2917 struct osd_thandle *oh;
2918 struct inode *inode;
2922 LASSERT(dt_object_exists(dt));
2923 LASSERT(handle != NULL);
2925 oh = container_of0(handle, struct osd_thandle, ot_super);
2926 LASSERT(oh->ot_handle == NULL);
2928 OSD_DECLARE_OP(oh, delete);
2929 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2931 inode = osd_dt_obj(dt)->oo_inode;
2934 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2935 true, true, NULL, false);
2939 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2942 struct osd_fid_pack *rec;
2945 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2946 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2947 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2953 * Index delete function for interoperability mode (b11826).
2954 * It will remove the directory entry added by osd_index_ea_insert().
2955 * This entry is needed to maintain name->fid mapping.
2957 * \param key, key i.e. file entry to be deleted
2959 * \retval 0, on success
2960 * \retval -ve, on error
2962 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2963 const struct dt_key *key,
2964 struct thandle *handle,
2965 struct lustre_capa *capa)
2967 struct osd_object *obj = osd_dt_obj(dt);
2968 struct inode *dir = obj->oo_inode;
2969 struct dentry *dentry;
2970 struct osd_thandle *oh;
2971 struct ldiskfs_dir_entry_2 *de;
2972 struct buffer_head *bh;
2973 struct htree_lock *hlock = NULL;
2978 LINVRNT(osd_invariant(obj));
2979 LASSERT(dt_object_exists(dt));
2980 LASSERT(handle != NULL);
2982 OSD_EXEC_OP(handle, delete);
2984 oh = container_of(handle, struct osd_thandle, ot_super);
2985 LASSERT(oh->ot_handle != NULL);
2986 LASSERT(oh->ot_handle->h_transaction != NULL);
2988 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2991 dentry = osd_child_dentry_get(env, obj,
2992 (char *)key, strlen((char *)key));
2994 if (obj->oo_hl_head != NULL) {
2995 hlock = osd_oti_get(env)->oti_hlock;
2996 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2997 dir, LDISKFS_HLOCK_DEL);
2999 cfs_down_write(&obj->oo_ext_idx_sem);
3002 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3004 rc = ldiskfs_delete_entry(oh->ot_handle,
3011 ldiskfs_htree_unlock(hlock);
3013 cfs_up_write(&obj->oo_ext_idx_sem);
3015 LASSERT(osd_invariant(obj));
3020 * Lookup index for \a key and copy record to \a rec.
3022 * \param dt osd index object
3023 * \param key key for index
3024 * \param rec record reference
3026 * \retval +ve success : exact mach
3027 * \retval 0 return record with key not greater than \a key
3028 * \retval -ve failure
3030 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3031 struct dt_rec *rec, const struct dt_key *key,
3032 struct lustre_capa *capa)
3034 struct osd_object *obj = osd_dt_obj(dt);
3035 struct iam_path_descr *ipd;
3036 struct iam_container *bag = &obj->oo_dir->od_container;
3037 struct osd_thread_info *oti = osd_oti_get(env);
3038 struct iam_iterator *it = &oti->oti_idx_it;
3039 struct iam_rec *iam_rec;
3044 LASSERT(osd_invariant(obj));
3045 LASSERT(dt_object_exists(dt));
3046 LASSERT(bag->ic_object == obj->oo_inode);
3048 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3051 ipd = osd_idx_ipd_get(env, bag);
3055 /* got ipd now we can start iterator. */
3056 iam_it_init(it, bag, 0, ipd);
3058 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3059 /* swab quota uid/gid provided by caller */
3060 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3061 key = (const struct dt_key *)&oti->oti_quota_id;
3064 rc = iam_it_get(it, (struct iam_key *)key);
3066 if (S_ISDIR(obj->oo_inode->i_mode))
3067 iam_rec = (struct iam_rec *)oti->oti_ldp;
3069 iam_rec = (struct iam_rec *) rec;
3071 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3073 if (S_ISDIR(obj->oo_inode->i_mode))
3074 osd_fid_unpack((struct lu_fid *) rec,
3075 (struct osd_fid_pack *)iam_rec);
3076 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3077 osd_quota_unpack(obj, rec);
3082 osd_ipd_put(env, bag, ipd);
3084 LINVRNT(osd_invariant(obj));
3089 static int osd_index_declare_iam_insert(const struct lu_env *env,
3090 struct dt_object *dt,
3091 const struct dt_rec *rec,
3092 const struct dt_key *key,
3093 struct thandle *handle)
3095 struct osd_thandle *oh;
3097 LASSERT(dt_object_exists(dt));
3098 LASSERT(handle != NULL);
3100 oh = container_of0(handle, struct osd_thandle, ot_super);
3101 LASSERT(oh->ot_handle == NULL);
3103 OSD_DECLARE_OP(oh, insert);
3104 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3110 * Inserts (key, value) pair in \a dt index object.
3112 * \param dt osd index object
3113 * \param key key for index
3114 * \param rec record reference
3115 * \param th transaction handler
3118 * \retval -ve failure
3120 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3121 const struct dt_rec *rec,
3122 const struct dt_key *key, struct thandle *th,
3123 struct lustre_capa *capa, int ignore_quota)
3125 struct osd_object *obj = osd_dt_obj(dt);
3126 struct iam_path_descr *ipd;
3127 struct osd_thandle *oh;
3128 struct iam_container *bag = &obj->oo_dir->od_container;
3129 struct osd_thread_info *oti = osd_oti_get(env);
3130 struct iam_rec *iam_rec;
3135 LINVRNT(osd_invariant(obj));
3136 LASSERT(dt_object_exists(dt));
3137 LASSERT(bag->ic_object == obj->oo_inode);
3138 LASSERT(th != NULL);
3140 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3143 OSD_EXEC_OP(th, insert);
3145 ipd = osd_idx_ipd_get(env, bag);
3146 if (unlikely(ipd == NULL))
3149 oh = container_of0(th, struct osd_thandle, ot_super);
3150 LASSERT(oh->ot_handle != NULL);
3151 LASSERT(oh->ot_handle->h_transaction != NULL);
3152 if (S_ISDIR(obj->oo_inode->i_mode)) {
3153 iam_rec = (struct iam_rec *)oti->oti_ldp;
3154 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3155 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3156 /* pack quota uid/gid */
3157 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3158 key = (const struct dt_key *)&oti->oti_quota_id;
3159 /* pack quota record */
3160 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3161 iam_rec = (struct iam_rec *)rec;
3163 iam_rec = (struct iam_rec *)rec;
3166 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3168 osd_ipd_put(env, bag, ipd);
3169 LINVRNT(osd_invariant(obj));
3174 * Calls ldiskfs_add_entry() to add directory entry
3175 * into the directory. This is required for
3176 * interoperability mode (b11826)
3178 * \retval 0, on success
3179 * \retval -ve, on error
3181 static int __osd_ea_add_rec(struct osd_thread_info *info,
3182 struct osd_object *pobj, struct inode *cinode,
3183 const char *name, const struct dt_rec *fid,
3184 struct htree_lock *hlock, struct thandle *th)
3186 struct ldiskfs_dentry_param *ldp;
3187 struct dentry *child;
3188 struct osd_thandle *oth;
3191 oth = container_of(th, struct osd_thandle, ot_super);
3192 LASSERT(oth->ot_handle != NULL);
3193 LASSERT(oth->ot_handle->h_transaction != NULL);
3195 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3197 /* XXX: remove fid_is_igif() check here.
3198 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3199 * it is IGIF now but needs FID in dir entry as well for readdir
3201 * LU-838 should fix that and remove fid_is_igif() check */
3202 if (fid_is_igif((struct lu_fid *)fid) ||
3203 fid_is_norm((struct lu_fid *)fid)) {
3204 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3205 osd_get_ldiskfs_dirent_param(ldp, fid);
3206 child->d_fsdata = (void *)ldp;
3208 child->d_fsdata = NULL;
3210 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3216 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3217 * into the directory.Also sets flags into osd object to
3218 * indicate dot and dotdot are created. This is required for
3219 * interoperability mode (b11826)
3221 * \param dir directory for dot and dotdot fixup.
3222 * \param obj child object for linking
3224 * \retval 0, on success
3225 * \retval -ve, on error
3227 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3228 struct osd_object *dir,
3229 struct inode *parent_dir, const char *name,
3230 const struct dt_rec *dot_fid,
3231 const struct dt_rec *dot_dot_fid,
3234 struct inode *inode = dir->oo_inode;
3235 struct ldiskfs_dentry_param *dot_ldp;
3236 struct ldiskfs_dentry_param *dot_dot_ldp;
3237 struct osd_thandle *oth;
3240 oth = container_of(th, struct osd_thandle, ot_super);
3241 LASSERT(oth->ot_handle->h_transaction != NULL);
3242 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3244 if (strcmp(name, dot) == 0) {
3245 if (dir->oo_compat_dot_created) {
3248 LASSERT(inode == parent_dir);
3249 dir->oo_compat_dot_created = 1;
3252 } else if(strcmp(name, dotdot) == 0) {
3253 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3254 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3256 if (!dir->oo_compat_dot_created)
3258 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3259 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3260 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3265 /* in case of rename, dotdot is already created */
3266 if (dir->oo_compat_dotdot_created) {
3267 return __osd_ea_add_rec(info, dir, parent_dir, name,
3268 dot_dot_fid, NULL, th);
3271 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3272 inode, dot_ldp, dot_dot_ldp);
3274 dir->oo_compat_dotdot_created = 1;
3282 * It will call the appropriate osd_add* function and return the
3283 * value, return by respective functions.
3285 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3286 struct inode *cinode, const char *name,
3287 const struct dt_rec *fid, struct thandle *th)
3289 struct osd_thread_info *info = osd_oti_get(env);
3290 struct htree_lock *hlock;
3293 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3295 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3297 if (hlock != NULL) {
3298 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3301 cfs_down_write(&pobj->oo_ext_idx_sem);
3303 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3304 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3307 if (hlock != NULL) {
3308 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3309 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3311 cfs_down_write(&pobj->oo_ext_idx_sem);
3314 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3318 ldiskfs_htree_unlock(hlock);
3320 cfs_up_write(&pobj->oo_ext_idx_sem);
3326 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3327 struct osd_idmap_cache *oic)
3329 struct osd_scrub *scrub = &dev->od_scrub;
3330 struct lu_fid *fid = &oic->oic_fid;
3331 struct osd_inode_id *id = &oti->oti_id;
3336 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3340 rc = osd_oi_lookup(oti, dev, fid, id);
3341 if (rc != 0 && rc != -ENOENT)
3344 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3347 if (thread_is_running(&scrub->os_thread)) {
3348 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3349 /* There is race condition between osd_oi_lookup and OI scrub.
3350 * The OI scrub finished just after osd_oi_lookup() failure.
3351 * Under such case, it is unnecessary to trigger OI scrub again,
3352 * but try to call osd_oi_lookup() again. */
3353 if (unlikely(rc == -EAGAIN))
3359 if (!dev->od_noscrub && ++once == 1) {
3360 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3362 rc = osd_scrub_start(dev);
3363 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3365 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3375 * Calls ->lookup() to find dentry. From dentry get inode and
3376 * read inode's ea to get fid. This is required for interoperability
3379 * \retval 0, on success
3380 * \retval -ve, on error
3382 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3383 struct dt_rec *rec, const struct dt_key *key)
3385 struct inode *dir = obj->oo_inode;
3386 struct dentry *dentry;
3387 struct ldiskfs_dir_entry_2 *de;
3388 struct buffer_head *bh;
3389 struct lu_fid *fid = (struct lu_fid *) rec;
3390 struct htree_lock *hlock = NULL;
3394 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3396 dentry = osd_child_dentry_get(env, obj,
3397 (char *)key, strlen((char *)key));
3399 if (obj->oo_hl_head != NULL) {
3400 hlock = osd_oti_get(env)->oti_hlock;
3401 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3402 dir, LDISKFS_HLOCK_LOOKUP);
3404 cfs_down_read(&obj->oo_ext_idx_sem);
3407 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3409 struct osd_thread_info *oti = osd_oti_get(env);
3410 struct osd_idmap_cache *oic = &oti->oti_cache;
3411 struct osd_device *dev = osd_obj2dev(obj);
3412 struct osd_scrub *scrub = &dev->od_scrub;
3413 struct scrub_file *sf = &scrub->os_file;
3415 ino = le32_to_cpu(de->inode);
3416 rc = osd_get_fid_from_dentry(de, rec);
3418 /* done with de, release bh */
3421 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3423 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3425 fid_zero(&oic->oic_fid);
3429 oic->oic_fid = *fid;
3430 if ((scrub->os_pos_current <= ino) &&
3431 (sf->sf_flags & SF_INCONSISTENT ||
3432 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3434 osd_consistency_check(oti, dev, oic);
3443 ldiskfs_htree_unlock(hlock);
3445 cfs_up_read(&obj->oo_ext_idx_sem);
3450 * Find the osd object for given fid.
3452 * \param fid need to find the osd object having this fid
3454 * \retval osd_object on success
3455 * \retval -ve on error
3457 struct osd_object *osd_object_find(const struct lu_env *env,
3458 struct dt_object *dt,
3459 const struct lu_fid *fid)
3461 struct lu_device *ludev = dt->do_lu.lo_dev;
3462 struct osd_object *child = NULL;
3463 struct lu_object *luch;
3464 struct lu_object *lo;
3467 * at this point topdev might not exist yet
3468 * (i.e. MGS is preparing profiles). so we can
3469 * not rely on topdev and instead lookup with
3470 * our device passed as topdev. this can't work
3471 * if the object isn't cached yet (as osd doesn't
3472 * allocate lu_header). IOW, the object must be
3473 * in the cache, otherwise lu_object_alloc() crashes
3476 luch = lu_object_find_at(env, ludev, fid, NULL);
3477 if (!IS_ERR(luch)) {
3478 if (lu_object_exists(luch)) {
3479 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3481 child = osd_obj(lo);
3483 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3484 "lu_object can't be located"
3485 DFID"\n", PFID(fid));
3487 if (child == NULL) {
3488 lu_object_put(env, luch);
3489 CERROR("Unable to get osd_object\n");
3490 child = ERR_PTR(-ENOENT);
3493 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3494 "lu_object does not exists "DFID"\n",
3496 lu_object_put(env, luch);
3497 child = ERR_PTR(-ENOENT);
3500 child = (void *)luch;
3506 * Put the osd object once done with it.
3508 * \param obj osd object that needs to be put
3510 static inline void osd_object_put(const struct lu_env *env,
3511 struct osd_object *obj)
3513 lu_object_put(env, &obj->oo_dt.do_lu);
3516 static int osd_index_declare_ea_insert(const struct lu_env *env,
3517 struct dt_object *dt,
3518 const struct dt_rec *rec,
3519 const struct dt_key *key,
3520 struct thandle *handle)
3522 struct osd_thandle *oh;
3523 struct inode *inode;
3527 LASSERT(dt_object_exists(dt));
3528 LASSERT(handle != NULL);
3530 oh = container_of0(handle, struct osd_thandle, ot_super);
3531 LASSERT(oh->ot_handle == NULL);
3533 OSD_DECLARE_OP(oh, insert);
3534 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3536 inode = osd_dt_obj(dt)->oo_inode;
3539 /* We ignore block quota on meta pool (MDTs), so needn't
3540 * calculate how many blocks will be consumed by this index
3542 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3543 true, true, NULL, false);
3548 * Index add function for interoperability mode (b11826).
3549 * It will add the directory entry.This entry is needed to
3550 * maintain name->fid mapping.
3552 * \param key it is key i.e. file entry to be inserted
3553 * \param rec it is value of given key i.e. fid
3555 * \retval 0, on success
3556 * \retval -ve, on error
3558 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3559 const struct dt_rec *rec,
3560 const struct dt_key *key, struct thandle *th,
3561 struct lustre_capa *capa, int ignore_quota)
3563 struct osd_object *obj = osd_dt_obj(dt);
3564 struct lu_fid *fid = (struct lu_fid *) rec;
3565 const char *name = (const char *)key;
3566 struct osd_object *child;
3571 LASSERT(osd_invariant(obj));
3572 LASSERT(dt_object_exists(dt));
3573 LASSERT(th != NULL);
3575 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3578 child = osd_object_find(env, dt, fid);
3579 if (!IS_ERR(child)) {
3580 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3581 osd_object_put(env, child);
3583 rc = PTR_ERR(child);
3586 LASSERT(osd_invariant(obj));
3591 * Initialize osd Iterator for given osd index object.
3593 * \param dt osd index object
3596 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3597 struct dt_object *dt,
3599 struct lustre_capa *capa)
3601 struct osd_it_iam *it;
3602 struct osd_thread_info *oti = osd_oti_get(env);
3603 struct osd_object *obj = osd_dt_obj(dt);
3604 struct lu_object *lo = &dt->do_lu;
3605 struct iam_path_descr *ipd;
3606 struct iam_container *bag = &obj->oo_dir->od_container;
3608 LASSERT(lu_object_exists(lo));
3610 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3611 return ERR_PTR(-EACCES);
3614 ipd = osd_it_ipd_get(env, bag);
3615 if (likely(ipd != NULL)) {
3619 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3620 return (struct dt_it *)it;
3622 return ERR_PTR(-ENOMEM);
3626 * free given Iterator.
3629 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3631 struct osd_it_iam *it = (struct osd_it_iam *)di;
3632 struct osd_object *obj = it->oi_obj;
3634 iam_it_fini(&it->oi_it);
3635 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3636 lu_object_put(env, &obj->oo_dt.do_lu);
3640 * Move Iterator to record specified by \a key
3642 * \param di osd iterator
3643 * \param key key for index
3645 * \retval +ve di points to record with least key not larger than key
3646 * \retval 0 di points to exact matched key
3647 * \retval -ve failure
3650 static int osd_it_iam_get(const struct lu_env *env,
3651 struct dt_it *di, const struct dt_key *key)
3653 struct osd_thread_info *oti = osd_oti_get(env);
3654 struct osd_it_iam *it = (struct osd_it_iam *)di;
3656 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3657 /* swab quota uid/gid */
3658 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3659 key = (struct dt_key *)&oti->oti_quota_id;
3662 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3668 * \param di osd iterator
3671 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3673 struct osd_it_iam *it = (struct osd_it_iam *)di;
3675 iam_it_put(&it->oi_it);
3679 * Move iterator by one record
3681 * \param di osd iterator
3683 * \retval +1 end of container reached
3685 * \retval -ve failure
3688 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3690 struct osd_it_iam *it = (struct osd_it_iam *)di;
3692 return iam_it_next(&it->oi_it);
3696 * Return pointer to the key under iterator.
3699 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3700 const struct dt_it *di)
3702 struct osd_thread_info *oti = osd_oti_get(env);
3703 struct osd_it_iam *it = (struct osd_it_iam *)di;
3704 struct osd_object *obj = it->oi_obj;
3707 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3709 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3710 /* swab quota uid/gid */
3711 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3712 key = (struct dt_key *)&oti->oti_quota_id;
3719 * Return size of key under iterator (in bytes)
3722 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3724 struct osd_it_iam *it = (struct osd_it_iam *)di;
3726 return iam_it_key_size(&it->oi_it);
3729 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3730 int len, __u16 type)
3732 struct luda_type *lt;
3733 const unsigned align = sizeof(struct luda_type) - 1;
3735 /* check if file type is required */
3736 if (attr & LUDA_TYPE) {
3737 len = (len + align) & ~align;
3739 lt = (void *) ent->lde_name + len;
3740 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3741 ent->lde_attrs |= LUDA_TYPE;
3744 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3748 * build lu direct from backend fs dirent.
3751 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3752 struct lu_fid *fid, __u64 offset,
3753 char *name, __u16 namelen,
3754 __u16 type, __u32 attr)
3756 fid_cpu_to_le(&ent->lde_fid, fid);
3757 ent->lde_attrs = LUDA_FID;
3759 ent->lde_hash = cpu_to_le64(offset);
3760 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3762 strncpy(ent->lde_name, name, namelen);
3763 ent->lde_namelen = cpu_to_le16(namelen);
3765 /* append lustre attributes */
3766 osd_it_append_attrs(ent, attr, namelen, type);
3770 * Return pointer to the record under iterator.
3772 static int osd_it_iam_rec(const struct lu_env *env,
3773 const struct dt_it *di,
3774 struct dt_rec *dtrec, __u32 attr)
3776 struct osd_it_iam *it = (struct osd_it_iam *)di;
3777 struct osd_thread_info *info = osd_oti_get(env);
3780 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3781 const struct osd_fid_pack *rec;
3782 struct lu_fid *fid = &info->oti_fid;
3783 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3789 name = (char *)iam_it_key_get(&it->oi_it);
3791 RETURN(PTR_ERR(name));
3793 namelen = iam_it_key_size(&it->oi_it);
3795 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3797 RETURN(PTR_ERR(rec));
3799 rc = osd_fid_unpack(fid, rec);
3803 hash = iam_it_store(&it->oi_it);
3805 /* IAM does not store object type in IAM index (dir) */
3806 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3808 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3809 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3810 (struct iam_rec *)dtrec);
3811 osd_quota_unpack(it->oi_obj, dtrec);
3813 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3814 (struct iam_rec *)dtrec);
3821 * Returns cookie for current Iterator position.
3823 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3825 struct osd_it_iam *it = (struct osd_it_iam *)di;
3827 return iam_it_store(&it->oi_it);
3831 * Restore iterator from cookie.
3833 * \param di osd iterator
3834 * \param hash Iterator location cookie
3836 * \retval +ve di points to record with least key not larger than key.
3837 * \retval 0 di points to exact matched key
3838 * \retval -ve failure
3841 static int osd_it_iam_load(const struct lu_env *env,
3842 const struct dt_it *di, __u64 hash)
3844 struct osd_it_iam *it = (struct osd_it_iam *)di;
3846 return iam_it_load(&it->oi_it, hash);
3849 static const struct dt_index_operations osd_index_iam_ops = {
3850 .dio_lookup = osd_index_iam_lookup,
3851 .dio_declare_insert = osd_index_declare_iam_insert,
3852 .dio_insert = osd_index_iam_insert,
3853 .dio_declare_delete = osd_index_declare_iam_delete,
3854 .dio_delete = osd_index_iam_delete,
3856 .init = osd_it_iam_init,
3857 .fini = osd_it_iam_fini,
3858 .get = osd_it_iam_get,
3859 .put = osd_it_iam_put,
3860 .next = osd_it_iam_next,
3861 .key = osd_it_iam_key,
3862 .key_size = osd_it_iam_key_size,
3863 .rec = osd_it_iam_rec,
3864 .store = osd_it_iam_store,
3865 .load = osd_it_iam_load
3870 * Creates or initializes iterator context.
3872 * \retval struct osd_it_ea, iterator structure on success
3875 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3876 struct dt_object *dt,
3878 struct lustre_capa *capa)
3880 struct osd_object *obj = osd_dt_obj(dt);
3881 struct osd_thread_info *info = osd_oti_get(env);
3882 struct osd_it_ea *it = &info->oti_it_ea;
3883 struct lu_object *lo = &dt->do_lu;
3884 struct dentry *obj_dentry = &info->oti_it_dentry;
3886 LASSERT(lu_object_exists(lo));
3888 obj_dentry->d_inode = obj->oo_inode;
3889 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3890 obj_dentry->d_name.hash = 0;
3892 it->oie_rd_dirent = 0;
3893 it->oie_it_dirent = 0;
3894 it->oie_dirent = NULL;
3895 it->oie_buf = info->oti_it_ea_buf;
3897 it->oie_file.f_pos = 0;
3898 it->oie_file.f_dentry = obj_dentry;
3899 if (attr & LUDA_64BITHASH)
3900 it->oie_file.f_mode |= FMODE_64BITHASH;
3902 it->oie_file.f_mode |= FMODE_32BITHASH;
3903 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3904 it->oie_file.f_op = obj->oo_inode->i_fop;
3905 it->oie_file.private_data = NULL;
3907 RETURN((struct dt_it *) it);
3911 * Destroy or finishes iterator context.
3913 * \param di iterator structure to be destroyed
3915 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3917 struct osd_it_ea *it = (struct osd_it_ea *)di;
3918 struct osd_object *obj = it->oie_obj;
3919 struct inode *inode = obj->oo_inode;
3922 it->oie_file.f_op->release(inode, &it->oie_file);
3923 lu_object_put(env, &obj->oo_dt.do_lu);
3928 * It position the iterator at given key, so that next lookup continues from
3929 * that key Or it is similar to dio_it->load() but based on a key,
3930 * rather than file position.
3932 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3935 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3937 static int osd_it_ea_get(const struct lu_env *env,
3938 struct dt_it *di, const struct dt_key *key)
3940 struct osd_it_ea *it = (struct osd_it_ea *)di;
3943 LASSERT(((const char *)key)[0] == '\0');
3944 it->oie_file.f_pos = 0;
3945 it->oie_rd_dirent = 0;
3946 it->oie_it_dirent = 0;
3947 it->oie_dirent = NULL;
3955 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3960 * It is called internally by ->readdir(). It fills the
3961 * iterator's in-memory data structure with required
3962 * information i.e. name, namelen, rec_size etc.
3964 * \param buf in which information to be filled in.
3965 * \param name name of the file in given dir
3967 * \retval 0 on success
3968 * \retval 1 on buffer full
3970 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3971 loff_t offset, __u64 ino,
3974 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3975 struct osd_it_ea_dirent *ent = it->oie_dirent;
3976 struct lu_fid *fid = &ent->oied_fid;
3977 struct osd_fid_pack *rec;
3980 /* this should never happen */
3981 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3982 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3986 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3990 if (d_type & LDISKFS_DIRENT_LUFID) {
3991 rec = (struct osd_fid_pack*) (name + namelen + 1);
3993 if (osd_fid_unpack(fid, rec) != 0)
3996 d_type &= ~LDISKFS_DIRENT_LUFID;
4001 ent->oied_ino = ino;
4002 ent->oied_off = offset;
4003 ent->oied_namelen = namelen;
4004 ent->oied_type = d_type;
4006 memcpy(ent->oied_name, name, namelen);
4008 it->oie_rd_dirent++;
4009 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4014 * Calls ->readdir() to load a directory entry at a time
4015 * and stored it in iterator's in-memory data structure.
4017 * \param di iterator's in memory structure
4019 * \retval 0 on success
4020 * \retval -ve on error
4022 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4023 const struct dt_it *di)
4025 struct osd_it_ea *it = (struct osd_it_ea *)di;
4026 struct osd_object *obj = it->oie_obj;
4027 struct inode *inode = obj->oo_inode;
4028 struct htree_lock *hlock = NULL;
4032 it->oie_dirent = it->oie_buf;
4033 it->oie_rd_dirent = 0;
4035 if (obj->oo_hl_head != NULL) {
4036 hlock = osd_oti_get(env)->oti_hlock;
4037 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4038 inode, LDISKFS_HLOCK_READDIR);
4040 cfs_down_read(&obj->oo_ext_idx_sem);
4043 result = inode->i_fop->readdir(&it->oie_file, it,
4044 (filldir_t) osd_ldiskfs_filldir);
4047 ldiskfs_htree_unlock(hlock);
4049 cfs_up_read(&obj->oo_ext_idx_sem);
4051 if (it->oie_rd_dirent == 0) {
4054 it->oie_dirent = it->oie_buf;
4055 it->oie_it_dirent = 1;
4062 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4063 * to load a directory entry at a time and stored it in
4064 * iterator's in-memory data structure.
4066 * \param di iterator's in memory structure
4068 * \retval +ve iterator reached to end
4069 * \retval 0 iterator not reached to end
4070 * \retval -ve on error
4072 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4074 struct osd_it_ea *it = (struct osd_it_ea *)di;
4079 if (it->oie_it_dirent < it->oie_rd_dirent) {
4081 (void *) it->oie_dirent +
4082 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4083 it->oie_dirent->oied_namelen);
4084 it->oie_it_dirent++;
4087 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4090 rc = osd_ldiskfs_it_fill(env, di);
4097 * Returns the key at current position from iterator's in memory structure.
4099 * \param di iterator's in memory structure
4101 * \retval key i.e. struct dt_key on success
4103 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4104 const struct dt_it *di)
4106 struct osd_it_ea *it = (struct osd_it_ea *)di;
4108 return (struct dt_key *)it->oie_dirent->oied_name;
4112 * Returns the key's size at current position from iterator's in memory structure.
4114 * \param di iterator's in memory structure
4116 * \retval key_size i.e. struct dt_key on success
4118 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4120 struct osd_it_ea *it = (struct osd_it_ea *)di;
4122 return it->oie_dirent->oied_namelen;
4127 * Returns the value (i.e. fid/igif) at current position from iterator's
4128 * in memory structure.
4130 * \param di struct osd_it_ea, iterator's in memory structure
4131 * \param attr attr requested for dirent.
4132 * \param lde lustre dirent
4134 * \retval 0 no error and \param lde has correct lustre dirent.
4135 * \retval -ve on error
4137 static inline int osd_it_ea_rec(const struct lu_env *env,
4138 const struct dt_it *di,
4139 struct dt_rec *dtrec, __u32 attr)
4141 struct osd_it_ea *it = (struct osd_it_ea *)di;
4142 struct osd_object *obj = it->oie_obj;
4143 struct osd_device *dev = osd_obj2dev(obj);
4144 struct osd_scrub *scrub = &dev->od_scrub;
4145 struct scrub_file *sf = &scrub->os_file;
4146 struct osd_thread_info *oti = osd_oti_get(env);
4147 struct osd_idmap_cache *oic = &oti->oti_cache;
4148 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4149 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4150 __u32 ino = it->oie_dirent->oied_ino;
4154 if (!fid_is_sane(fid)) {
4155 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4159 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4162 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4163 it->oie_dirent->oied_name,
4164 it->oie_dirent->oied_namelen,
4165 it->oie_dirent->oied_type, attr);
4166 oic->oic_fid = *fid;
4167 if ((scrub->os_pos_current <= ino) &&
4168 (sf->sf_flags & SF_INCONSISTENT ||
4169 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4170 osd_consistency_check(oti, dev, oic);
4176 * Returns a cookie for current position of the iterator head, so that
4177 * user can use this cookie to load/start the iterator next time.
4179 * \param di iterator's in memory structure
4181 * \retval cookie for current position, on success
4183 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4185 struct osd_it_ea *it = (struct osd_it_ea *)di;
4187 return it->oie_dirent->oied_off;
4191 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4192 * to load a directory entry at a time and stored it i inn,
4193 * in iterator's in-memory data structure.
4195 * \param di struct osd_it_ea, iterator's in memory structure
4197 * \retval +ve on success
4198 * \retval -ve on error
4200 static int osd_it_ea_load(const struct lu_env *env,
4201 const struct dt_it *di, __u64 hash)
4203 struct osd_it_ea *it = (struct osd_it_ea *)di;
4207 it->oie_file.f_pos = hash;
4209 rc = osd_ldiskfs_it_fill(env, di);
4217 * Index lookup function for interoperability mode (b11826).
4219 * \param key, key i.e. file name to be searched
4221 * \retval +ve, on success
4222 * \retval -ve, on error
4224 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4225 struct dt_rec *rec, const struct dt_key *key,
4226 struct lustre_capa *capa)
4228 struct osd_object *obj = osd_dt_obj(dt);
4233 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4234 LINVRNT(osd_invariant(obj));
4236 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4239 rc = osd_ea_lookup_rec(env, obj, rec, key);
4246 * Index and Iterator operations for interoperability
4247 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4249 static const struct dt_index_operations osd_index_ea_ops = {
4250 .dio_lookup = osd_index_ea_lookup,
4251 .dio_declare_insert = osd_index_declare_ea_insert,
4252 .dio_insert = osd_index_ea_insert,
4253 .dio_declare_delete = osd_index_declare_ea_delete,
4254 .dio_delete = osd_index_ea_delete,
4256 .init = osd_it_ea_init,
4257 .fini = osd_it_ea_fini,
4258 .get = osd_it_ea_get,
4259 .put = osd_it_ea_put,
4260 .next = osd_it_ea_next,
4261 .key = osd_it_ea_key,
4262 .key_size = osd_it_ea_key_size,
4263 .rec = osd_it_ea_rec,
4264 .store = osd_it_ea_store,
4265 .load = osd_it_ea_load
4269 static void *osd_key_init(const struct lu_context *ctx,
4270 struct lu_context_key *key)
4272 struct osd_thread_info *info;
4274 OBD_ALLOC_PTR(info);
4276 return ERR_PTR(-ENOMEM);
4278 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4279 if (info->oti_it_ea_buf == NULL)
4282 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4284 info->oti_hlock = ldiskfs_htree_lock_alloc();
4285 if (info->oti_hlock == NULL)
4291 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4294 return ERR_PTR(-ENOMEM);
4297 static void osd_key_fini(const struct lu_context *ctx,
4298 struct lu_context_key *key, void* data)
4300 struct osd_thread_info *info = data;
4302 if (info->oti_hlock != NULL)
4303 ldiskfs_htree_lock_free(info->oti_hlock);
4304 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4308 static void osd_key_exit(const struct lu_context *ctx,
4309 struct lu_context_key *key, void *data)
4311 struct osd_thread_info *info = data;
4313 LASSERT(info->oti_r_locks == 0);
4314 LASSERT(info->oti_w_locks == 0);
4315 LASSERT(info->oti_txns == 0);
4318 /* type constructor/destructor: osd_type_init, osd_type_fini */
4319 LU_TYPE_INIT_FINI(osd, &osd_key);
4321 struct lu_context_key osd_key = {
4322 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4323 .lct_init = osd_key_init,
4324 .lct_fini = osd_key_fini,
4325 .lct_exit = osd_key_exit
4329 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4330 const char *name, struct lu_device *next)
4332 struct osd_device *osd = osd_dev(d);
4334 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4335 return osd_procfs_init(osd, name);
4338 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4342 osd_scrub_cleanup(env, o);
4345 fsfilt_put_ops(o->od_fsops);
4349 /* shutdown quota slave instance associated with the device */
4350 if (o->od_quota_slave != NULL) {
4351 qsd_fini(env, o->od_quota_slave);
4352 o->od_quota_slave = NULL;
4358 static int osd_mount(const struct lu_env *env,
4359 struct osd_device *o, struct lustre_cfg *cfg)
4361 const char *name = lustre_cfg_string(cfg, 0);
4362 const char *dev = lustre_cfg_string(cfg, 1);
4364 unsigned long page, s_flags, lmd_flags = 0;
4365 struct page *__page;
4366 struct file_system_type *type;
4367 char *options = NULL;
4372 if (o->od_mnt != NULL)
4375 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4376 if (o->od_fsops == NULL) {
4377 CERROR("Can't find fsfilt_ldiskfs\n");
4381 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4383 GOTO(out, rc = -ENOMEM);
4385 str = lustre_cfg_string(cfg, 2);
4386 s_flags = simple_strtoul(str, NULL, 0);
4387 str = strstr(str, ":");
4389 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4390 opts = lustre_cfg_string(cfg, 3);
4391 page = (unsigned long)cfs_page_address(__page);
4392 options = (char *)page;
4395 strcat(options, "user_xattr,acl");
4397 strcat(options, opts);
4399 /* Glom up mount options */
4400 if (*options != '\0')
4401 strcat(options, ",");
4402 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4404 type = get_fs_type("ldiskfs");
4406 CERROR("%s: cannot find ldiskfs module\n", name);
4407 GOTO(out, rc = -ENODEV);
4410 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4411 cfs_module_put(type->owner);
4413 if (IS_ERR(o->od_mnt)) {
4414 rc = PTR_ERR(o->od_mnt);
4415 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4420 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4421 CERROR("%s: underlying device %s is marked as read-only. "
4422 "Setup failed\n", name, dev);
4425 GOTO(out, rc = -EROFS);
4428 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4429 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4430 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4433 GOTO(out, rc = -EINVAL);
4436 if (lmd_flags & LMD_FLG_IAM) {
4438 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4441 if (lmd_flags & LMD_FLG_NOSCRUB)
4446 OBD_PAGE_FREE(__page);
4448 fsfilt_put_ops(o->od_fsops);
4453 static struct lu_device *osd_device_fini(const struct lu_env *env,
4454 struct lu_device *d)
4459 rc = osd_shutdown(env, osd_dev(d));
4461 osd_compat_fini(osd_dev(d));
4463 shrink_dcache_sb(osd_sb(osd_dev(d)));
4464 osd_sync(env, lu2dt_dev(d));
4466 rc = osd_procfs_fini(osd_dev(d));
4468 CERROR("proc fini error %d \n", rc);
4469 RETURN (ERR_PTR(rc));
4472 if (osd_dev(d)->od_mnt) {
4473 mntput(osd_dev(d)->od_mnt);
4474 osd_dev(d)->od_mnt = NULL;
4480 static int osd_device_init0(const struct lu_env *env,
4481 struct osd_device *o,
4482 struct lustre_cfg *cfg)
4484 struct lu_device *l = osd2lu_dev(o);
4485 struct osd_thread_info *info;
4488 /* if the module was re-loaded, env can loose its keys */
4489 rc = lu_env_refill((struct lu_env *) env);
4492 info = osd_oti_get(env);
4495 l->ld_ops = &osd_lu_ops;
4496 o->od_dt_dev.dd_ops = &osd_dt_ops;
4498 cfs_spin_lock_init(&o->od_osfs_lock);
4499 cfs_mutex_init(&o->od_otable_mutex);
4500 o->od_osfs_age = cfs_time_shift_64(-1000);
4502 o->od_capa_hash = init_capa_hash();
4503 if (o->od_capa_hash == NULL)
4504 GOTO(out, rc = -ENOMEM);
4506 o->od_read_cache = 1;
4507 o->od_writethrough_cache = 1;
4508 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
4510 rc = osd_mount(env, o, cfg);
4514 /* setup scrub, including OI files initialization */
4515 rc = osd_scrub_setup(env, o);
4519 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4520 sizeof(o->od_svname) - 1);
4522 rc = osd_compat_init(o);
4524 GOTO(out_scrub, rc);
4526 rc = lu_site_init(&o->od_site, l);
4528 GOTO(out_compat, rc);
4529 o->od_site.ls_bottom_dev = l;
4531 rc = lu_site_init_finish(&o->od_site);
4535 rc = osd_procfs_init(o, o->od_svname);
4537 CERROR("%s: can't initialize procfs: rc = %d\n",
4542 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4544 /* initialize quota slave instance */
4545 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
4547 if (IS_ERR(o->od_quota_slave)) {
4548 rc = PTR_ERR(o->od_quota_slave);
4549 o->od_quota_slave = NULL;
4550 GOTO(out_procfs, rc);
4557 lu_site_fini(&o->od_site);
4561 osd_scrub_cleanup(env, o);
4563 osd_oi_fini(info, o);
4564 osd_shutdown(env, o);
4568 cleanup_capa_hash(o->od_capa_hash);
4573 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4574 struct lu_device_type *t,
4575 struct lustre_cfg *cfg)
4577 struct osd_device *o;
4582 return ERR_PTR(-ENOMEM);
4584 rc = dt_device_init(&o->od_dt_dev, t);
4586 rc = osd_device_init0(env, o, cfg);
4588 dt_device_fini(&o->od_dt_dev);
4591 if (unlikely(rc != 0))
4594 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4597 static struct lu_device *osd_device_free(const struct lu_env *env,
4598 struct lu_device *d)
4600 struct osd_device *o = osd_dev(d);
4603 cleanup_capa_hash(o->od_capa_hash);
4604 /* XXX: make osd top device in order to release reference */
4605 d->ld_site->ls_top_dev = d;
4606 lu_site_purge(env, d->ld_site, -1);
4607 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4608 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4609 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4611 lu_site_fini(&o->od_site);
4612 dt_device_fini(&o->od_dt_dev);
4617 static int osd_process_config(const struct lu_env *env,
4618 struct lu_device *d, struct lustre_cfg *cfg)
4620 struct osd_device *o = osd_dev(d);
4624 switch(cfg->lcfg_command) {
4626 err = osd_mount(env, o, cfg);
4629 lu_dev_del_linkage(d->ld_site, d);
4630 err = osd_shutdown(env, o);
4639 static int osd_recovery_complete(const struct lu_env *env,
4640 struct lu_device *d)
4642 struct osd_device *osd = osd_dev(d);
4646 if (osd->od_quota_slave == NULL)
4649 /* start qsd instance on recovery completion, this notifies the quota
4650 * slave code that we are about to process new requests now */
4651 rc = qsd_start(env, osd->od_quota_slave);
4656 * we use exports to track all osd users
4658 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4659 struct obd_device *obd, struct obd_uuid *cluuid,
4660 struct obd_connect_data *data, void *localdata)
4662 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4663 struct lustre_handle conn;
4667 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4669 rc = class_connect(&conn, obd, cluuid);
4673 *exp = class_conn2export(&conn);
4675 cfs_spin_lock(&osd->od_osfs_lock);
4677 cfs_spin_unlock(&osd->od_osfs_lock);
4683 * once last export (we don't count self-export) disappeared
4684 * osd can be released
4686 static int osd_obd_disconnect(struct obd_export *exp)
4688 struct obd_device *obd = exp->exp_obd;
4689 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4690 int rc, release = 0;
4693 /* Only disconnect the underlying layers on the final disconnect. */
4694 cfs_spin_lock(&osd->od_osfs_lock);
4696 if (osd->od_connects == 0)
4698 cfs_spin_unlock(&osd->od_osfs_lock);
4700 rc = class_disconnect(exp); /* bz 9811 */
4702 if (rc == 0 && release)
4703 class_manual_cleanup(obd);
4707 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4708 struct lu_device *dev)
4710 struct osd_device *osd = osd_dev(dev);
4714 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
4715 /* MDT/MDD still use old infrastructure to create
4717 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4723 if (osd->od_quota_slave != NULL)
4724 /* set up quota slave objects */
4725 result = qsd_prepare(env, osd->od_quota_slave);
4730 static const struct lu_object_operations osd_lu_obj_ops = {
4731 .loo_object_init = osd_object_init,
4732 .loo_object_delete = osd_object_delete,
4733 .loo_object_release = osd_object_release,
4734 .loo_object_free = osd_object_free,
4735 .loo_object_print = osd_object_print,
4736 .loo_object_invariant = osd_object_invariant
4739 const struct lu_device_operations osd_lu_ops = {
4740 .ldo_object_alloc = osd_object_alloc,
4741 .ldo_process_config = osd_process_config,
4742 .ldo_recovery_complete = osd_recovery_complete,
4743 .ldo_prepare = osd_prepare,
4746 static const struct lu_device_type_operations osd_device_type_ops = {
4747 .ldto_init = osd_type_init,
4748 .ldto_fini = osd_type_fini,
4750 .ldto_start = osd_type_start,
4751 .ldto_stop = osd_type_stop,
4753 .ldto_device_alloc = osd_device_alloc,
4754 .ldto_device_free = osd_device_free,
4756 .ldto_device_init = osd_device_init,
4757 .ldto_device_fini = osd_device_fini
4760 struct lu_device_type osd_device_type = {
4761 .ldt_tags = LU_DEVICE_DT,
4762 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4763 .ldt_ops = &osd_device_type_ops,
4764 .ldt_ctx_tags = LCT_LOCAL,
4768 * lprocfs legacy support.
4770 static struct obd_ops osd_obd_device_ops = {
4771 .o_owner = THIS_MODULE,
4772 .o_connect = osd_obd_connect,
4773 .o_disconnect = osd_obd_disconnect
4776 static int __init osd_mod_init(void)
4778 struct lprocfs_static_vars lvars;
4781 lprocfs_osd_init_vars(&lvars);
4782 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4783 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4786 static void __exit osd_mod_exit(void)
4788 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4791 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4792 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4793 MODULE_LICENSE("GPL");
4795 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);