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, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
66 #include "osd_internal.h"
68 /* llo_* api support */
69 #include <md_object.h>
70 #include <lustre_quota.h>
73 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
74 "ldiskfs with parallel directory operations");
76 int ldiskfs_track_declares_assert;
77 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
78 "LBUG during tracking of declares");
80 static const char dot[] = ".";
81 static const char dotdot[] = "..";
82 static const char remote_obj_dir[] = "REM_OBJ_DIR";
84 static const struct lu_object_operations osd_lu_obj_ops;
85 static const struct dt_object_operations osd_obj_ops;
86 static const struct dt_object_operations osd_obj_ea_ops;
87 static const struct dt_object_operations osd_obj_otable_it_ops;
88 static const struct dt_index_operations osd_index_iam_ops;
89 static const struct dt_index_operations osd_index_ea_ops;
91 int osd_trans_declare_op2rb[] = {
92 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
93 [OSD_OT_PUNCH] = OSD_OT_MAX,
94 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
95 [OSD_OT_CREATE] = OSD_OT_DESTROY,
96 [OSD_OT_DESTROY] = OSD_OT_CREATE,
97 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
98 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
99 [OSD_OT_WRITE] = OSD_OT_WRITE,
100 [OSD_OT_INSERT] = OSD_OT_DELETE,
101 [OSD_OT_DELETE] = OSD_OT_INSERT,
102 [OSD_OT_UPDATE] = OSD_OT_MAX,
103 [OSD_OT_QUOTA] = OSD_OT_MAX,
106 static int osd_has_index(const struct osd_object *obj)
108 return obj->oo_dt.do_index_ops != NULL;
111 static int osd_object_invariant(const struct lu_object *l)
113 return osd_invariant(osd_obj(l));
117 * Concurrency: doesn't matter
119 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
121 return osd_oti_get(env)->oti_r_locks > 0;
125 * Concurrency: doesn't matter
127 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
129 struct osd_thread_info *oti = osd_oti_get(env);
130 return oti->oti_w_locks > 0 && o->oo_owner == env;
134 * Concurrency: doesn't access mutable data
136 static int osd_root_get(const struct lu_env *env,
137 struct dt_device *dev, struct lu_fid *f)
139 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
144 * OSD object methods.
148 * Concurrency: no concurrent access is possible that early in object
151 static struct lu_object *osd_object_alloc(const struct lu_env *env,
152 const struct lu_object_header *hdr,
155 struct osd_object *mo;
161 l = &mo->oo_dt.do_lu;
162 dt_object_init(&mo->oo_dt, NULL, d);
163 mo->oo_dt.do_ops = &osd_obj_ea_ops;
164 l->lo_ops = &osd_lu_obj_ops;
165 init_rwsem(&mo->oo_sem);
166 init_rwsem(&mo->oo_ext_idx_sem);
167 spin_lock_init(&mo->oo_guard);
174 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
175 struct dentry *dentry, struct lustre_mdt_attrs *lma)
179 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
180 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
181 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
183 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
184 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
186 lustre_lma_swab(lma);
187 /* Check LMA compatibility */
188 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
189 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
190 "for fid = "DFID", ino = %lu\n",
191 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
192 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
193 PFID(&lma->lma_self_fid), inode->i_ino);
196 } else if (rc == 0) {
204 * retrieve object from backend ext fs.
206 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
207 struct osd_inode_id *id)
209 struct inode *inode = NULL;
211 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
213 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
214 id->oii_ino, PTR_ERR(inode));
215 } else if (id->oii_gen != OSD_OII_NOGEN &&
216 inode->i_generation != id->oii_gen) {
217 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
219 id->oii_ino, id->oii_gen, inode->i_generation);
221 inode = ERR_PTR(-ESTALE);
222 } else if (inode->i_nlink == 0) {
223 /* due to parallel readdir and unlink,
224 * we can have dead inode here. */
225 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
226 make_bad_inode(inode);
228 inode = ERR_PTR(-ESTALE);
229 } else if (is_bad_inode(inode)) {
230 CWARN("%.16s: bad inode: ino = %u\n",
231 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
233 inode = ERR_PTR(-ENOENT);
235 if (id->oii_gen == OSD_OII_NOGEN)
236 osd_id_gen(id, inode->i_ino, inode->i_generation);
238 /* Do not update file c/mtime in ldiskfs.
239 * NB: we don't have any lock to protect this because we don't
240 * have reference on osd_object now, but contention with
241 * another lookup + attr_set can't happen in the tiny window
242 * between if (...) and set S_NOCMTIME. */
243 if (!(inode->i_flags & S_NOCMTIME))
244 inode->i_flags |= S_NOCMTIME;
249 static struct inode *
250 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
251 struct osd_inode_id *id, struct lu_fid *fid)
253 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
257 inode = osd_iget(info, dev, id);
261 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
263 *fid = lma->lma_self_fid;
264 } else if (rc == -ENODATA) {
265 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
266 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
268 lu_igif_build(fid, inode->i_ino, inode->i_generation);
277 * \retval +v: new filter_fid, does not contain self-fid
278 * \retval 0: filter_fid_old, contains self-fid
279 * \retval -v: other failure cases
281 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
282 struct dentry *dentry, struct lu_fid *fid)
284 struct filter_fid_old *ff = &info->oti_ff;
285 struct ost_id *ostid = &info->oti_ostid;
288 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
289 if (rc == sizeof(*ff)) {
291 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
292 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
293 /* XXX: should use real OST index in the future. LU-3569 */
294 ostid_to_fid(fid, ostid, 0);
295 } else if (rc == sizeof(struct filter_fid)) {
297 } else if (rc >= 0) {
304 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
306 struct osd_thread_info *info = osd_oti_get(env);
307 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
308 struct inode *inode = obj->oo_inode;
309 struct dentry *dentry = &info->oti_obj_dentry;
310 struct lu_fid *fid = NULL;
314 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
317 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
318 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
319 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
320 if (rc == -ENODATA && !fid_is_igif(lu_object_fid(&obj->oo_dt.do_lu)) &&
321 osd_obj2dev(obj)->od_check_ff) {
322 fid = &lma->lma_self_fid;
323 rc = osd_get_idif(info, inode, dentry, fid);
328 if (unlikely(rc == -ENODATA))
336 lustre_lma_swab(lma);
337 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
338 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
339 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
340 "fid = "DFID", ino = %lu\n",
341 osd_obj2dev(obj)->od_svname,
342 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
343 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
346 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
347 fid = &lma->lma_self_fid;
352 unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
353 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
354 osd_obj2dev(obj)->od_svname,
355 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
356 PFID(&lma->lma_self_fid));
363 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
364 const struct lu_fid *fid,
365 const struct lu_object_conf *conf)
367 struct osd_thread_info *info;
368 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
369 struct osd_device *dev;
370 struct osd_idmap_cache *oic;
371 struct osd_inode_id *id;
373 struct osd_scrub *scrub;
374 struct scrub_file *sf;
378 bool triggered = false;
381 LINVRNT(osd_invariant(obj));
382 LASSERT(obj->oo_inode == NULL);
383 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
386 scrub = &dev->od_scrub;
387 sf = &scrub->os_file;
388 info = osd_oti_get(env);
390 oic = &info->oti_cache;
392 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
395 /* Search order: 1. per-thread cache. */
396 if (lu_fid_eq(fid, &oic->oic_fid)) {
402 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
403 /* Search order: 2. OI scrub pending list. */
404 result = osd_oii_lookup(dev, fid, id);
410 * Objects are created as locking anchors or place holders for objects
411 * yet to be created. No need to osd_oi_lookup() at here because FID
412 * shouldn't never be re-used, if it's really a duplicate FID from
413 * unexpected reason, we should be able to detect it later by calling
414 * do_create->osd_oi_insert()
416 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
417 GOTO(out, result = 0);
419 /* Search order: 3. OI files. */
420 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
421 if (result == -ENOENT) {
422 if (!fid_is_norm(fid) ||
423 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
424 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
426 GOTO(out, result = 0);
437 inode = osd_iget(info, dev, id);
439 result = PTR_ERR(inode);
440 if (result == -ENOENT || result == -ESTALE) {
442 fid_zero(&oic->oic_fid);
443 GOTO(out, result = 0);
446 /* XXX: There are three possible cases:
447 * 1. Backup/restore caused the OI invalid.
448 * 2. Someone unlinked the object but NOT removed
449 * the OI mapping, such as mount target device
450 * as ldiskfs, and modify something directly.
451 * 3. Someone just removed the object between the
452 * former oi_lookup and the iget. It is normal.
454 * It is diffcult to distinguish the 2nd from the
455 * 1st case. Relatively speaking, the 1st case is
456 * common than the 2nd case, trigger OI scrub. */
457 result = osd_oi_lookup(info, dev, fid, id, true);
459 /* It is the case 1 or 2. */
462 if (result == -ENOENT)
463 /* It is the case 3. */
465 } else if (result == -EREMCHG) {
468 if (unlikely(triggered))
469 GOTO(out, result = saved);
472 if (thread_is_running(&scrub->os_thread)) {
473 result = -EINPROGRESS;
474 } else if (!dev->od_noscrub) {
475 result = osd_scrub_start(dev);
476 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
477 "for "DFID", rc = %d [1]\n",
478 LDISKFS_SB(osd_sb(dev))->s_es->\
479 s_volume_name,PFID(fid), result);
480 if (result == 0 || result == -EALREADY)
481 result = -EINPROGRESS;
486 /* We still have chance to get the valid inode: for the
487 * object which is referenced by remote name entry, the
488 * object on the local MDT will be linked under the dir
489 * of "/REMOTE_PARENT_DIR" with its FID string as name.
491 * We do not know whether the object for the given FID
492 * is referenced by some remote name entry or not, and
493 * especially for DNE II, a multiple-linked object may
494 * have many name entries reside on many MDTs.
496 * To simplify the operation, OSD will not distinguish
497 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
498 * only happened for the RPC from other MDT during the
499 * OI scrub, or for the client side RPC with FID only,
500 * such as FID to path, or from old connected client. */
502 result = osd_lookup_in_remote_parent(info, dev,
515 obj->oo_inode = inode;
516 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
518 result = osd_check_lma(env, obj);
521 obj->oo_inode = NULL;
522 if (result == -EREMCHG)
528 obj->oo_compat_dot_created = 1;
529 obj->oo_compat_dotdot_created = 1;
531 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
532 GOTO(out, result = 0);
534 LASSERT(obj->oo_hl_head == NULL);
535 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
536 if (obj->oo_hl_head == NULL) {
537 obj->oo_inode = NULL;
539 GOTO(out, result = -ENOMEM);
541 GOTO(out, result = 0);
544 LINVRNT(osd_invariant(obj));
549 * Concurrency: shouldn't matter.
551 static void osd_object_init0(struct osd_object *obj)
553 LASSERT(obj->oo_inode != NULL);
554 obj->oo_dt.do_body_ops = &osd_body_ops;
555 obj->oo_dt.do_lu.lo_header->loh_attr |=
556 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
560 * Concurrency: no concurrent access is possible that early in object
563 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
564 const struct lu_object_conf *conf)
566 struct osd_object *obj = osd_obj(l);
569 LINVRNT(osd_invariant(obj));
571 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
572 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
573 l->lo_header->loh_attr |= LOHA_EXISTS;
577 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
578 obj->oo_dt.do_body_ops = &osd_body_ops_new;
579 if (result == 0 && obj->oo_inode != NULL)
580 osd_object_init0(obj);
582 LINVRNT(osd_invariant(obj));
587 * Concurrency: no concurrent access is possible that late in object
590 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
592 struct osd_object *obj = osd_obj(l);
594 LINVRNT(osd_invariant(obj));
596 dt_object_fini(&obj->oo_dt);
597 if (obj->oo_hl_head != NULL)
598 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
603 * Concurrency: no concurrent access is possible that late in object
606 static void osd_index_fini(struct osd_object *o)
608 struct iam_container *bag;
610 if (o->oo_dir != NULL) {
611 bag = &o->oo_dir->od_container;
612 if (o->oo_inode != NULL) {
613 if (bag->ic_object == o->oo_inode)
614 iam_container_fini(bag);
616 OBD_FREE_PTR(o->oo_dir);
622 * Concurrency: no concurrent access is possible that late in object
623 * life-cycle (for all existing callers, that is. New callers have to provide
624 * their own locking.)
626 static int osd_inode_unlinked(const struct inode *inode)
628 return inode->i_nlink == 0;
632 OSD_TXN_OI_DELETE_CREDITS = 20,
633 OSD_TXN_INODE_DELETE_CREDITS = 20
640 #if OSD_THANDLE_STATS
642 * Set time when the handle is allocated
644 static void osd_th_alloced(struct osd_thandle *oth)
646 oth->oth_alloced = cfs_time_current();
650 * Set time when the handle started
652 static void osd_th_started(struct osd_thandle *oth)
654 oth->oth_started = cfs_time_current();
658 * Helper function to convert time interval to microseconds packed in
661 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
665 cfs_duration_usec(cfs_time_sub(end, start), &val);
666 return val.tv_sec * 1000000 + val.tv_usec;
670 * Check whether the we deal with this handle for too long.
672 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
673 cfs_time_t alloced, cfs_time_t started,
676 cfs_time_t now = cfs_time_current();
678 LASSERT(dev != NULL);
680 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
681 interval_to_usec(alloced, started));
682 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
683 interval_to_usec(started, closed));
684 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
685 interval_to_usec(closed, now));
687 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
688 CWARN("transaction handle %p was open for too long: "
690 "alloced "CFS_TIME_T" ,"
691 "started "CFS_TIME_T" ,"
692 "closed "CFS_TIME_T"\n",
693 oth, now, alloced, started, closed);
694 libcfs_debug_dumpstack(NULL);
698 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
700 cfs_time_t __closed = cfs_time_current(); \
701 cfs_time_t __alloced = oth->oth_alloced; \
702 cfs_time_t __started = oth->oth_started; \
705 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
708 #else /* OSD_THANDLE_STATS */
710 #define osd_th_alloced(h) do {} while(0)
711 #define osd_th_started(h) do {} while(0)
712 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
714 #endif /* OSD_THANDLE_STATS */
717 * Concurrency: doesn't access mutable data.
719 static int osd_param_is_not_sane(const struct osd_device *dev,
720 const struct thandle *th)
722 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
724 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
728 * Concurrency: shouldn't matter.
730 static void osd_trans_commit_cb(struct super_block *sb,
731 struct ldiskfs_journal_cb_entry *jcb, int error)
733 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
734 struct thandle *th = &oh->ot_super;
735 struct lu_device *lud = &th->th_dev->dd_lu_dev;
736 struct dt_txn_commit_cb *dcb, *tmp;
738 LASSERT(oh->ot_handle == NULL);
741 CERROR("transaction @0x%p commit error: %d\n", th, error);
743 dt_txn_hook_commit(th);
745 /* call per-transaction callbacks if any */
746 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
747 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
748 "commit callback entry: magic=%x name='%s'\n",
749 dcb->dcb_magic, dcb->dcb_name);
750 cfs_list_del_init(&dcb->dcb_linkage);
751 dcb->dcb_func(NULL, th, dcb, error);
754 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
758 lu_context_exit(&th->th_ctx);
759 lu_context_fini(&th->th_ctx);
763 static struct thandle *osd_trans_create(const struct lu_env *env,
766 struct osd_thread_info *oti = osd_oti_get(env);
767 struct osd_iobuf *iobuf = &oti->oti_iobuf;
768 struct osd_thandle *oh;
772 /* on pending IO in this thread should left from prev. request */
773 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
775 th = ERR_PTR(-ENOMEM);
776 OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
778 oh->ot_quota_trans = &oti->oti_quota_trans;
779 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
783 th->th_tags = LCT_TX_HANDLE;
785 oti->oti_dev = osd_dt_dev(d);
786 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
789 memset(oti->oti_declare_ops, 0,
790 sizeof(oti->oti_declare_ops));
791 memset(oti->oti_declare_ops_rb, 0,
792 sizeof(oti->oti_declare_ops_rb));
793 memset(oti->oti_declare_ops_cred, 0,
794 sizeof(oti->oti_declare_ops_cred));
795 oti->oti_rollback = false;
801 * Concurrency: shouldn't matter.
803 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
806 struct osd_thread_info *oti = osd_oti_get(env);
807 struct osd_device *dev = osd_dt_dev(d);
809 struct osd_thandle *oh;
814 LASSERT(current->journal_info == NULL);
816 oh = container_of0(th, struct osd_thandle, ot_super);
818 LASSERT(oh->ot_handle == NULL);
820 rc = dt_txn_hook_start(env, d, th);
824 if (unlikely(osd_param_is_not_sane(dev, th))) {
825 static unsigned long last_printed;
826 static int last_credits;
828 CWARN("%.16s: too many transaction credits (%d > %d)\n",
829 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
831 osd_journal(dev)->j_max_transaction_buffers);
832 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
833 oti->oti_declare_ops[OSD_OT_CREATE],
834 oti->oti_declare_ops_cred[OSD_OT_CREATE],
835 oti->oti_declare_ops[OSD_OT_DELETE],
836 oti->oti_declare_ops_cred[OSD_OT_DELETE],
837 oti->oti_declare_ops[OSD_OT_DESTROY],
838 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
839 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
840 oti->oti_declare_ops[OSD_OT_ATTR_SET],
841 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
842 oti->oti_declare_ops[OSD_OT_XATTR_SET],
843 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
844 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
845 oti->oti_declare_ops[OSD_OT_WRITE],
846 oti->oti_declare_ops_cred[OSD_OT_WRITE],
847 oti->oti_declare_ops[OSD_OT_PUNCH],
848 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
849 oti->oti_declare_ops[OSD_OT_QUOTA],
850 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
851 CWARN(" insert: %u/%u, delete: %u/%u\n",
852 oti->oti_declare_ops[OSD_OT_INSERT],
853 oti->oti_declare_ops_cred[OSD_OT_INSERT],
854 oti->oti_declare_ops[OSD_OT_DESTROY],
855 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
856 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
857 oti->oti_declare_ops[OSD_OT_REF_ADD],
858 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
859 oti->oti_declare_ops[OSD_OT_REF_DEL],
860 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
862 if (last_credits != oh->ot_credits &&
863 time_after(jiffies, last_printed + 60 * HZ)) {
864 libcfs_debug_dumpstack(NULL);
865 last_credits = oh->ot_credits;
866 last_printed = jiffies;
868 /* XXX Limit the credits to 'max_transaction_buffers', and
869 * let the underlying filesystem to catch the error if
870 * we really need so many credits.
872 * This should be removed when we can calculate the
873 * credits precisely. */
874 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
878 * XXX temporary stuff. Some abstraction layer should
881 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
885 LASSERT(oti->oti_txns == 0);
886 lu_context_init(&th->th_ctx, th->th_tags);
887 lu_context_enter(&th->th_ctx);
889 lu_device_get(&d->dd_lu_dev);
890 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
902 * Concurrency: shouldn't matter.
904 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
907 struct osd_thandle *oh;
908 struct osd_thread_info *oti = osd_oti_get(env);
909 struct osd_iobuf *iobuf = &oti->oti_iobuf;
910 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
913 oh = container_of0(th, struct osd_thandle, ot_super);
916 /* inform the quota slave device that the transaction is
918 qsd_op_end(env, qsd, oh->ot_quota_trans);
919 oh->ot_quota_trans = NULL;
921 if (oh->ot_handle != NULL) {
922 handle_t *hdl = oh->ot_handle;
925 * add commit callback
926 * notice we don't do this in osd_trans_start()
927 * as underlying transaction can change during truncate
929 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
932 LASSERT(oti->oti_txns == 1);
934 rc = dt_txn_hook_stop(env, th);
936 CERROR("Failure in transaction hook: %d\n", rc);
938 /* hook functions might modify th_sync */
939 hdl->h_sync = th->th_sync;
941 oh->ot_handle = NULL;
942 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
943 rc = ldiskfs_journal_stop(hdl));
945 CERROR("Failure to stop transaction: %d\n", rc);
950 /* as we want IO to journal and data IO be concurrent, we don't block
951 * awaiting data IO completion in osd_do_bio(), instead we wait here
952 * once transaction is submitted to the journal. all reqular requests
953 * don't do direct IO (except read/write), thus this wait_event becomes
956 * IMPORTANT: we have to wait till any IO submited by the thread is
957 * completed otherwise iobuf may be corrupted by different request
959 cfs_wait_event(iobuf->dr_wait,
960 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
962 rc = iobuf->dr_error;
967 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
969 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
972 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
973 LASSERT(&dcb->dcb_func != NULL);
974 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
980 * Called just before object is freed. Releases all resources except for
981 * object itself (that is released by osd_object_free()).
983 * Concurrency: no concurrent access is possible that late in object
986 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
988 struct osd_object *obj = osd_obj(l);
989 struct inode *inode = obj->oo_inode;
991 LINVRNT(osd_invariant(obj));
994 * If object is unlinked remove fid->ino mapping from object index.
999 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1000 qid_t uid = inode->i_uid;
1001 qid_t gid = inode->i_gid;
1004 obj->oo_inode = NULL;
1007 struct osd_thread_info *info = osd_oti_get(env);
1008 struct lquota_id_info *qi = &info->oti_qi;
1010 /* Release granted quota to master if necessary */
1011 qi->lqi_id.qid_uid = uid;
1012 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1014 qi->lqi_id.qid_uid = gid;
1015 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1021 * Concurrency: ->loo_object_release() is called under site spin-lock.
1023 static void osd_object_release(const struct lu_env *env,
1024 struct lu_object *l)
1029 * Concurrency: shouldn't matter.
1031 static int osd_object_print(const struct lu_env *env, void *cookie,
1032 lu_printer_t p, const struct lu_object *l)
1034 struct osd_object *o = osd_obj(l);
1035 struct iam_descr *d;
1037 if (o->oo_dir != NULL)
1038 d = o->oo_dir->od_container.ic_descr;
1041 return (*p)(env, cookie,
1042 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1044 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1045 o->oo_inode ? o->oo_inode->i_generation : 0,
1046 d ? d->id_ops->id_name : "plain");
1050 * Concurrency: shouldn't matter.
1052 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1053 struct obd_statfs *sfs)
1055 struct osd_device *osd = osd_dt_dev(d);
1056 struct super_block *sb = osd_sb(osd);
1057 struct kstatfs *ksfs;
1060 if (unlikely(osd->od_mnt == NULL))
1061 return -EINPROGRESS;
1063 /* osd_lproc.c call this without env, allocate ksfs for that case */
1064 if (unlikely(env == NULL)) {
1065 OBD_ALLOC_PTR(ksfs);
1069 ksfs = &osd_oti_get(env)->oti_ksfs;
1072 spin_lock(&osd->od_osfs_lock);
1073 /* cache 1 second */
1074 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1075 result = sb->s_op->statfs(sb->s_root, ksfs);
1076 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1077 osd->od_osfs_age = cfs_time_current_64();
1078 statfs_pack(&osd->od_statfs, ksfs);
1079 if (sb->s_flags & MS_RDONLY)
1080 sfs->os_state = OS_STATE_READONLY;
1084 if (likely(result == 0))
1085 *sfs = osd->od_statfs;
1086 spin_unlock(&osd->od_osfs_lock);
1088 if (unlikely(env == NULL))
1095 * Estimate space needed for file creations. We assume the largest filename
1096 * which is 2^64 - 1, hence a filename of 20 chars.
1097 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1099 #ifdef __LDISKFS_DIR_REC_LEN
1100 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1102 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1106 * Concurrency: doesn't access mutable data.
1108 static void osd_conf_get(const struct lu_env *env,
1109 const struct dt_device *dev,
1110 struct dt_device_param *param)
1112 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1115 * XXX should be taken from not-yet-existing fs abstraction layer.
1117 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1118 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1119 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1120 param->ddp_block_shift = sb->s_blocksize_bits;
1121 param->ddp_mount_type = LDD_MT_LDISKFS;
1122 param->ddp_maxbytes = sb->s_maxbytes;
1123 /* Overhead estimate should be fairly accurate, so we really take a tiny
1124 * error margin which also avoids fragmenting the filesystem too much */
1125 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1126 /* inode are statically allocated, so per-inode space consumption
1127 * is the space consumed by the directory entry */
1128 param->ddp_inodespace = PER_OBJ_USAGE;
1129 /* per-fragment overhead to be used by the client code */
1130 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1131 param->ddp_mntopts = 0;
1132 if (test_opt(sb, XATTR_USER))
1133 param->ddp_mntopts |= MNTOPT_USERXATTR;
1134 if (test_opt(sb, POSIX_ACL))
1135 param->ddp_mntopts |= MNTOPT_ACL;
1137 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1138 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1139 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1142 param->ddp_max_ea_size = sb->s_blocksize;
1147 * Concurrency: shouldn't matter.
1149 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1151 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1152 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1156 * Start commit for OSD device.
1158 * An implementation of dt_commit_async method for OSD device.
1159 * Asychronously starts underlayng fs sync and thereby a transaction
1162 * \param env environment
1163 * \param d dt device
1165 * \see dt_device_operations
1167 static int osd_commit_async(const struct lu_env *env,
1168 struct dt_device *d)
1170 struct super_block *s = osd_sb(osd_dt_dev(d));
1173 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1174 RETURN(s->s_op->sync_fs(s, 0));
1178 * Concurrency: shouldn't matter.
1181 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1183 struct super_block *sb = osd_sb(osd_dt_dev(d));
1184 struct block_device *dev = sb->s_bdev;
1185 #ifdef HAVE_DEV_SET_RDONLY
1186 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1189 int rc = -EOPNOTSUPP;
1193 #ifdef HAVE_DEV_SET_RDONLY
1194 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1196 if (jdev && (jdev != dev)) {
1197 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1199 dev_set_rdonly(jdev);
1201 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1202 dev_set_rdonly(dev);
1204 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1205 osd_dt_dev(d)->od_svname, (long)dev, rc);
1211 * Concurrency: serialization provided by callers.
1213 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1214 int mode, unsigned long timeout, __u32 alg,
1215 struct lustre_capa_key *keys)
1217 struct osd_device *dev = osd_dt_dev(d);
1220 dev->od_fl_capa = mode;
1221 dev->od_capa_timeout = timeout;
1222 dev->od_capa_alg = alg;
1223 dev->od_capa_keys = keys;
1228 * Note: we do not count into QUOTA here.
1229 * If we mount with --data_journal we may need more.
1231 const int osd_dto_credits_noquota[DTO_NR] = {
1234 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1235 * SINGLEDATA_TRANS_BLOCKS(8)
1236 * XXX Note: maybe iam need more, since iam have more level than
1239 [DTO_INDEX_INSERT] = 16,
1240 [DTO_INDEX_DELETE] = 16,
1244 [DTO_INDEX_UPDATE] = 16,
1246 * Create a object. The same as create object in EXT3.
1247 * DATA_TRANS_BLOCKS(14) +
1248 * INDEX_EXTRA_BLOCKS(8) +
1249 * 3(inode bits, groups, GDT)
1251 [DTO_OBJECT_CREATE] = 25,
1253 * XXX: real credits to be fixed
1255 [DTO_OBJECT_DELETE] = 25,
1257 * Attr set credits (inode)
1259 [DTO_ATTR_SET_BASE] = 1,
1261 * Xattr set. The same as xattr of EXT3.
1262 * DATA_TRANS_BLOCKS(14)
1263 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1264 * are also counted in. Do not know why?
1266 [DTO_XATTR_SET] = 14,
1269 * credits for inode change during write.
1271 [DTO_WRITE_BASE] = 3,
1273 * credits for single block write.
1275 [DTO_WRITE_BLOCK] = 14,
1277 * Attr set credits for chown.
1278 * This is extra credits for setattr, and it is null without quota
1280 [DTO_ATTR_SET_CHOWN]= 0
1283 static const struct dt_device_operations osd_dt_ops = {
1284 .dt_root_get = osd_root_get,
1285 .dt_statfs = osd_statfs,
1286 .dt_trans_create = osd_trans_create,
1287 .dt_trans_start = osd_trans_start,
1288 .dt_trans_stop = osd_trans_stop,
1289 .dt_trans_cb_add = osd_trans_cb_add,
1290 .dt_conf_get = osd_conf_get,
1291 .dt_sync = osd_sync,
1293 .dt_commit_async = osd_commit_async,
1294 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1297 static void osd_object_read_lock(const struct lu_env *env,
1298 struct dt_object *dt, unsigned role)
1300 struct osd_object *obj = osd_dt_obj(dt);
1301 struct osd_thread_info *oti = osd_oti_get(env);
1303 LINVRNT(osd_invariant(obj));
1305 LASSERT(obj->oo_owner != env);
1306 down_read_nested(&obj->oo_sem, role);
1308 LASSERT(obj->oo_owner == NULL);
1312 static void osd_object_write_lock(const struct lu_env *env,
1313 struct dt_object *dt, unsigned role)
1315 struct osd_object *obj = osd_dt_obj(dt);
1316 struct osd_thread_info *oti = osd_oti_get(env);
1318 LINVRNT(osd_invariant(obj));
1320 LASSERT(obj->oo_owner != env);
1321 down_write_nested(&obj->oo_sem, role);
1323 LASSERT(obj->oo_owner == NULL);
1324 obj->oo_owner = env;
1328 static void osd_object_read_unlock(const struct lu_env *env,
1329 struct dt_object *dt)
1331 struct osd_object *obj = osd_dt_obj(dt);
1332 struct osd_thread_info *oti = osd_oti_get(env);
1334 LINVRNT(osd_invariant(obj));
1336 LASSERT(oti->oti_r_locks > 0);
1338 up_read(&obj->oo_sem);
1341 static void osd_object_write_unlock(const struct lu_env *env,
1342 struct dt_object *dt)
1344 struct osd_object *obj = osd_dt_obj(dt);
1345 struct osd_thread_info *oti = osd_oti_get(env);
1347 LINVRNT(osd_invariant(obj));
1349 LASSERT(obj->oo_owner == env);
1350 LASSERT(oti->oti_w_locks > 0);
1352 obj->oo_owner = NULL;
1353 up_write(&obj->oo_sem);
1356 static int osd_object_write_locked(const struct lu_env *env,
1357 struct dt_object *dt)
1359 struct osd_object *obj = osd_dt_obj(dt);
1361 LINVRNT(osd_invariant(obj));
1363 return obj->oo_owner == env;
1366 static int capa_is_sane(const struct lu_env *env,
1367 struct osd_device *dev,
1368 struct lustre_capa *capa,
1369 struct lustre_capa_key *keys)
1371 struct osd_thread_info *oti = osd_oti_get(env);
1372 struct lustre_capa *tcapa = &oti->oti_capa;
1373 struct obd_capa *oc;
1377 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1379 if (capa_is_expired(oc)) {
1380 DEBUG_CAPA(D_ERROR, capa, "expired");
1387 if (capa_is_expired_sec(capa)) {
1388 DEBUG_CAPA(D_ERROR, capa, "expired");
1392 spin_lock(&capa_lock);
1393 for (i = 0; i < 2; i++) {
1394 if (keys[i].lk_keyid == capa->lc_keyid) {
1395 oti->oti_capa_key = keys[i];
1399 spin_unlock(&capa_lock);
1402 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1406 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1410 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1411 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1415 oc = capa_add(dev->od_capa_hash, capa);
1421 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1422 struct lustre_capa *capa, __u64 opc)
1424 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1425 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1426 struct lu_capainfo *lci;
1429 if (!osd->od_fl_capa)
1432 if (capa == BYPASS_CAPA)
1435 lci = lu_capainfo_get(env);
1436 if (unlikely(lci == NULL))
1439 if (lci->lci_auth == LC_ID_NONE)
1443 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1444 osd_name(osd), PFID(fid), -EACCES);
1448 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1449 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1454 if (!capa_opc_supported(capa, opc)) {
1455 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1459 rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1461 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1468 static struct timespec *osd_inode_time(const struct lu_env *env,
1469 struct inode *inode, __u64 seconds)
1471 struct osd_thread_info *oti = osd_oti_get(env);
1472 struct timespec *t = &oti->oti_time;
1474 t->tv_sec = seconds;
1476 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1481 static void osd_inode_getattr(const struct lu_env *env,
1482 struct inode *inode, struct lu_attr *attr)
1484 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1485 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1486 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1489 attr->la_atime = LTIME_S(inode->i_atime);
1490 attr->la_mtime = LTIME_S(inode->i_mtime);
1491 attr->la_ctime = LTIME_S(inode->i_ctime);
1492 attr->la_mode = inode->i_mode;
1493 attr->la_size = i_size_read(inode);
1494 attr->la_blocks = inode->i_blocks;
1495 attr->la_uid = inode->i_uid;
1496 attr->la_gid = inode->i_gid;
1497 attr->la_flags = LDISKFS_I(inode)->i_flags;
1498 attr->la_nlink = inode->i_nlink;
1499 attr->la_rdev = inode->i_rdev;
1500 attr->la_blksize = 1 << inode->i_blkbits;
1501 attr->la_blkbits = inode->i_blkbits;
1504 static int osd_attr_get(const struct lu_env *env,
1505 struct dt_object *dt,
1506 struct lu_attr *attr,
1507 struct lustre_capa *capa)
1509 struct osd_object *obj = osd_dt_obj(dt);
1511 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1512 LINVRNT(osd_invariant(obj));
1514 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1517 spin_lock(&obj->oo_guard);
1518 osd_inode_getattr(env, obj->oo_inode, attr);
1519 spin_unlock(&obj->oo_guard);
1523 static int osd_declare_attr_set(const struct lu_env *env,
1524 struct dt_object *dt,
1525 const struct lu_attr *attr,
1526 struct thandle *handle)
1528 struct osd_thandle *oh;
1529 struct osd_object *obj;
1530 struct osd_thread_info *info = osd_oti_get(env);
1531 struct lquota_id_info *qi = &info->oti_qi;
1537 LASSERT(dt != NULL);
1538 LASSERT(handle != NULL);
1540 obj = osd_dt_obj(dt);
1541 LASSERT(osd_invariant(obj));
1543 oh = container_of0(handle, struct osd_thandle, ot_super);
1544 LASSERT(oh->ot_handle == NULL);
1546 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1547 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1549 if (attr == NULL || obj->oo_inode == NULL)
1552 bspace = obj->oo_inode->i_blocks;
1553 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1554 bspace = toqb(bspace);
1556 /* Changing ownership is always preformed by super user, it should not
1559 * We still need to call the osd_declare_qid() to calculate the journal
1560 * credits for updating quota accounting files and to trigger quota
1561 * space adjustment once the operation is completed.*/
1562 if ((attr->la_valid & LA_UID) != 0 &&
1563 attr->la_uid != obj->oo_inode->i_uid) {
1564 qi->lqi_type = USRQUOTA;
1566 /* inode accounting */
1567 qi->lqi_is_blk = false;
1569 /* one more inode for the new owner ... */
1570 qi->lqi_id.qid_uid = attr->la_uid;
1572 allocated = (attr->la_uid == 0) ? true : false;
1573 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1574 if (rc == -EDQUOT || rc == -EINPROGRESS)
1579 /* and one less inode for the current uid */
1580 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1582 rc = osd_declare_qid(env, oh, qi, true, NULL);
1583 if (rc == -EDQUOT || rc == -EINPROGRESS)
1588 /* block accounting */
1589 qi->lqi_is_blk = true;
1591 /* more blocks for the new owner ... */
1592 qi->lqi_id.qid_uid = attr->la_uid;
1593 qi->lqi_space = bspace;
1594 allocated = (attr->la_uid == 0) ? true : false;
1595 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1596 if (rc == -EDQUOT || rc == -EINPROGRESS)
1601 /* and finally less blocks for the current owner */
1602 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1603 qi->lqi_space = -bspace;
1604 rc = osd_declare_qid(env, oh, qi, true, NULL);
1605 if (rc == -EDQUOT || rc == -EINPROGRESS)
1611 if (attr->la_valid & LA_GID &&
1612 attr->la_gid != obj->oo_inode->i_gid) {
1613 qi->lqi_type = GRPQUOTA;
1615 /* inode accounting */
1616 qi->lqi_is_blk = false;
1618 /* one more inode for the new group owner ... */
1619 qi->lqi_id.qid_gid = attr->la_gid;
1621 allocated = (attr->la_gid == 0) ? true : false;
1622 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1623 if (rc == -EDQUOT || rc == -EINPROGRESS)
1628 /* and one less inode for the current gid */
1629 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1631 rc = osd_declare_qid(env, oh, qi, true, NULL);
1632 if (rc == -EDQUOT || rc == -EINPROGRESS)
1637 /* block accounting */
1638 qi->lqi_is_blk = true;
1640 /* more blocks for the new owner ... */
1641 qi->lqi_id.qid_gid = attr->la_gid;
1642 qi->lqi_space = bspace;
1643 allocated = (attr->la_gid == 0) ? true : false;
1644 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1645 if (rc == -EDQUOT || rc == -EINPROGRESS)
1650 /* and finally less blocks for the current owner */
1651 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1652 qi->lqi_space = -bspace;
1653 rc = osd_declare_qid(env, oh, qi, true, NULL);
1654 if (rc == -EDQUOT || rc == -EINPROGRESS)
1663 static int osd_inode_setattr(const struct lu_env *env,
1664 struct inode *inode, const struct lu_attr *attr)
1668 bits = attr->la_valid;
1670 if (bits & LA_ATIME)
1671 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1672 if (bits & LA_CTIME)
1673 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1674 if (bits & LA_MTIME)
1675 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1676 if (bits & LA_SIZE) {
1677 LDISKFS_I(inode)->i_disksize = attr->la_size;
1678 i_size_write(inode, attr->la_size);
1682 /* OSD should not change "i_blocks" which is used by quota.
1683 * "i_blocks" should be changed by ldiskfs only. */
1684 if (bits & LA_BLOCKS)
1685 inode->i_blocks = attr->la_blocks;
1688 inode->i_mode = (inode->i_mode & S_IFMT) |
1689 (attr->la_mode & ~S_IFMT);
1691 inode->i_uid = attr->la_uid;
1693 inode->i_gid = attr->la_gid;
1694 if (bits & LA_NLINK)
1695 set_nlink(inode, attr->la_nlink);
1697 inode->i_rdev = attr->la_rdev;
1699 if (bits & LA_FLAGS) {
1700 /* always keep S_NOCMTIME */
1701 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1707 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1709 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1710 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1715 if (attr->la_valid & LA_UID)
1716 iattr.ia_valid |= ATTR_UID;
1717 if (attr->la_valid & LA_GID)
1718 iattr.ia_valid |= ATTR_GID;
1719 iattr.ia_uid = attr->la_uid;
1720 iattr.ia_gid = attr->la_gid;
1722 rc = ll_vfs_dq_transfer(inode, &iattr);
1724 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1725 "enforcement enabled on the ldiskfs filesystem?",
1726 inode->i_sb->s_id, rc);
1733 static int osd_attr_set(const struct lu_env *env,
1734 struct dt_object *dt,
1735 const struct lu_attr *attr,
1736 struct thandle *handle,
1737 struct lustre_capa *capa)
1739 struct osd_object *obj = osd_dt_obj(dt);
1740 struct inode *inode;
1743 LASSERT(handle != NULL);
1744 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1745 LASSERT(osd_invariant(obj));
1747 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1750 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1752 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1753 struct osd_thread_info *oti = osd_oti_get(env);
1754 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1755 struct lu_fid *fid1 = &oti->oti_fid;
1756 struct osd_inode_id *id = &oti->oti_id;
1757 struct iam_path_descr *ipd;
1758 struct iam_container *bag;
1759 struct osd_thandle *oh;
1762 fid_cpu_to_be(fid1, fid0);
1763 memset(id, 1, sizeof(*id));
1764 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1765 fid0)->oi_dir.od_container;
1766 ipd = osd_idx_ipd_get(env, bag);
1767 if (unlikely(ipd == NULL))
1770 oh = container_of0(handle, struct osd_thandle, ot_super);
1771 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1772 (const struct iam_rec *)id, ipd);
1773 osd_ipd_put(env, bag, ipd);
1774 return(rc > 0 ? 0 : rc);
1777 inode = obj->oo_inode;
1778 ll_vfs_dq_init(inode);
1780 rc = osd_quota_transfer(inode, attr);
1784 spin_lock(&obj->oo_guard);
1785 rc = osd_inode_setattr(env, inode, attr);
1786 spin_unlock(&obj->oo_guard);
1789 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1793 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1794 struct osd_object *obj,
1795 const char *name, const int namelen)
1797 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1800 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1801 umode_t mode, struct dt_allocation_hint *hint,
1805 struct osd_device *osd = osd_obj2dev(obj);
1806 struct osd_thandle *oth;
1807 struct dt_object *parent = NULL;
1808 struct inode *inode;
1810 LINVRNT(osd_invariant(obj));
1811 LASSERT(obj->oo_inode == NULL);
1812 LASSERT(obj->oo_hl_head == NULL);
1814 if (S_ISDIR(mode) && ldiskfs_pdo) {
1815 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1816 if (obj->oo_hl_head == NULL)
1820 oth = container_of(th, struct osd_thandle, ot_super);
1821 LASSERT(oth->ot_handle->h_transaction != NULL);
1823 if (hint && hint->dah_parent)
1824 parent = hint->dah_parent;
1826 inode = ldiskfs_create_inode(oth->ot_handle,
1827 parent ? osd_dt_obj(parent)->oo_inode :
1828 osd_sb(osd)->s_root->d_inode,
1830 if (!IS_ERR(inode)) {
1831 /* Do not update file c/mtime in ldiskfs.
1832 * NB: don't need any lock because no contention at this
1834 inode->i_flags |= S_NOCMTIME;
1836 /* For new created object, it must be consistent,
1837 * and it is unnecessary to scrub against it. */
1838 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1839 obj->oo_inode = inode;
1842 if (obj->oo_hl_head != NULL) {
1843 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1844 obj->oo_hl_head = NULL;
1846 result = PTR_ERR(inode);
1848 LINVRNT(osd_invariant(obj));
1856 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1857 struct lu_attr *attr,
1858 struct dt_allocation_hint *hint,
1859 struct dt_object_format *dof,
1863 struct osd_thandle *oth;
1864 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1866 LASSERT(S_ISDIR(attr->la_mode));
1868 oth = container_of(th, struct osd_thandle, ot_super);
1869 LASSERT(oth->ot_handle->h_transaction != NULL);
1870 result = osd_mkfile(info, obj, mode, hint, th);
1875 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1876 struct lu_attr *attr,
1877 struct dt_allocation_hint *hint,
1878 struct dt_object_format *dof,
1882 struct osd_thandle *oth;
1883 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1885 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1887 LASSERT(S_ISREG(attr->la_mode));
1889 oth = container_of(th, struct osd_thandle, ot_super);
1890 LASSERT(oth->ot_handle->h_transaction != NULL);
1892 result = osd_mkfile(info, obj, mode, hint, th);
1894 LASSERT(obj->oo_inode != NULL);
1895 if (feat->dif_flags & DT_IND_VARKEY)
1896 result = iam_lvar_create(obj->oo_inode,
1897 feat->dif_keysize_max,
1899 feat->dif_recsize_max,
1902 result = iam_lfix_create(obj->oo_inode,
1903 feat->dif_keysize_max,
1905 feat->dif_recsize_max,
1912 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1913 struct lu_attr *attr,
1914 struct dt_allocation_hint *hint,
1915 struct dt_object_format *dof,
1918 LASSERT(S_ISREG(attr->la_mode));
1919 return osd_mkfile(info, obj, (attr->la_mode &
1920 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1923 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1924 struct lu_attr *attr,
1925 struct dt_allocation_hint *hint,
1926 struct dt_object_format *dof,
1929 LASSERT(S_ISLNK(attr->la_mode));
1930 return osd_mkfile(info, obj, (attr->la_mode &
1931 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1934 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1935 struct lu_attr *attr,
1936 struct dt_allocation_hint *hint,
1937 struct dt_object_format *dof,
1940 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1943 LINVRNT(osd_invariant(obj));
1944 LASSERT(obj->oo_inode == NULL);
1945 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1946 S_ISFIFO(mode) || S_ISSOCK(mode));
1948 result = osd_mkfile(info, obj, mode, hint, th);
1950 LASSERT(obj->oo_inode != NULL);
1952 * This inode should be marked dirty for i_rdev. Currently
1953 * that is done in the osd_attr_init().
1955 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1958 LINVRNT(osd_invariant(obj));
1962 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1964 struct dt_allocation_hint *hint,
1965 struct dt_object_format *dof,
1968 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1970 osd_obj_type_f result;
1986 result = osd_mk_index;
1997 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1998 struct dt_object *parent, struct dt_object *child,
2003 memset(ah, 0, sizeof(*ah));
2004 ah->dah_parent = parent;
2005 ah->dah_mode = child_mode;
2008 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2009 struct lu_attr *attr, struct dt_object_format *dof)
2011 struct inode *inode = obj->oo_inode;
2012 __u64 valid = attr->la_valid;
2015 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2017 if (dof->dof_type != DFT_NODE)
2018 attr->la_valid &= ~LA_RDEV;
2019 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2020 attr->la_valid &= ~LA_ATIME;
2021 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2022 attr->la_valid &= ~LA_CTIME;
2023 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2024 attr->la_valid &= ~LA_MTIME;
2026 result = osd_quota_transfer(inode, attr);
2030 if (attr->la_valid != 0) {
2031 result = osd_inode_setattr(info->oti_env, inode, attr);
2033 * The osd_inode_setattr() should always succeed here. The
2034 * only error that could be returned is EDQUOT when we are
2035 * trying to change the UID or GID of the inode. However, this
2036 * should not happen since quota enforcement is no longer
2037 * enabled on ldiskfs (lquota takes care of it).
2039 LASSERTF(result == 0, "%d", result);
2040 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2043 attr->la_valid = valid;
2047 * Helper function for osd_object_create()
2049 * \retval 0, on success
2051 static int __osd_object_create(struct osd_thread_info *info,
2052 struct osd_object *obj, struct lu_attr *attr,
2053 struct dt_allocation_hint *hint,
2054 struct dt_object_format *dof,
2060 /* we drop umask so that permissions we pass are not affected */
2061 umask = current->fs->umask;
2062 current->fs->umask = 0;
2064 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2067 osd_attr_init(info, obj, attr, dof);
2068 osd_object_init0(obj);
2070 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2071 unlock_new_inode(obj->oo_inode);
2074 /* restore previous umask value */
2075 current->fs->umask = umask;
2081 * Helper function for osd_object_create()
2083 * \retval 0, on success
2085 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2086 const struct lu_fid *fid, struct thandle *th)
2088 struct osd_thread_info *info = osd_oti_get(env);
2089 struct osd_inode_id *id = &info->oti_id;
2090 struct osd_device *osd = osd_obj2dev(obj);
2092 LASSERT(obj->oo_inode != NULL);
2094 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2095 return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2098 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2099 const struct lu_fid *fid, struct lu_seq_range *range)
2101 struct seq_server_site *ss = osd_seq_site(osd);
2104 if (fid_is_idif(fid)) {
2105 fld_range_set_ost(range);
2106 range->lsr_index = fid_idif_ost_idx(fid);
2110 if (!fid_seq_in_fldb(fid_seq(fid))) {
2111 fld_range_set_mdt(range);
2113 /* FIXME: If ss is NULL, it suppose not get lsr_index
2115 range->lsr_index = ss->ss_node_id;
2119 LASSERT(ss != NULL);
2120 fld_range_set_any(range);
2121 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2123 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
2124 osd_name(osd), PFID(fid), rc);
2130 * Concurrency: no external locking is necessary.
2132 static int osd_declare_object_create(const struct lu_env *env,
2133 struct dt_object *dt,
2134 struct lu_attr *attr,
2135 struct dt_allocation_hint *hint,
2136 struct dt_object_format *dof,
2137 struct thandle *handle)
2139 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2140 struct osd_thandle *oh;
2144 LASSERT(handle != NULL);
2146 oh = container_of0(handle, struct osd_thandle, ot_super);
2147 LASSERT(oh->ot_handle == NULL);
2149 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2150 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2151 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2152 lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2153 /* Reuse idle OI block may cause additional one OI block
2155 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2156 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2158 /* If this is directory, then we expect . and .. to be inserted as
2159 * well. The one directory block always needs to be created for the
2160 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2161 * block), there is no danger of needing a tree for the first block.
2163 if (attr && S_ISDIR(attr->la_mode)) {
2164 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2165 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2166 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2172 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2173 false, false, NULL, false);
2177 /* It does fld look up inside declare, and the result will be
2178 * added to fld cache, so the following fld lookup inside insert
2179 * does not need send RPC anymore, so avoid send rpc with holding
2181 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2182 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2183 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2184 lu_object_fid(&dt->do_lu), range);
2190 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2191 struct lu_attr *attr,
2192 struct dt_allocation_hint *hint,
2193 struct dt_object_format *dof,
2196 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2197 struct osd_object *obj = osd_dt_obj(dt);
2198 struct osd_thread_info *info = osd_oti_get(env);
2203 LINVRNT(osd_invariant(obj));
2204 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2205 LASSERT(osd_write_locked(env, obj));
2206 LASSERT(th != NULL);
2208 if (unlikely(fid_is_acct(fid)))
2209 /* Quota files can't be created from the kernel any more,
2210 * 'tune2fs -O quota' will take care of creating them */
2213 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2214 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2216 result = __osd_object_create(info, obj, attr, hint, dof, th);
2218 result = __osd_oi_insert(env, obj, fid, th);
2220 LASSERT(ergo(result == 0,
2221 dt_object_exists(dt) && !dt_object_remote(dt)));
2223 LASSERT(osd_invariant(obj));
2228 * Called to destroy on-disk representation of the object
2230 * Concurrency: must be locked
2232 static int osd_declare_object_destroy(const struct lu_env *env,
2233 struct dt_object *dt,
2236 struct osd_object *obj = osd_dt_obj(dt);
2237 struct inode *inode = obj->oo_inode;
2238 struct osd_thandle *oh;
2242 oh = container_of0(th, struct osd_thandle, ot_super);
2243 LASSERT(oh->ot_handle == NULL);
2246 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2247 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2248 /* Recycle idle OI leaf may cause additional three OI blocks
2250 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2251 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2252 /* one less inode */
2253 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2254 false, true, NULL, false);
2257 /* data to be truncated */
2258 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2259 true, true, NULL, false);
2263 static int osd_object_destroy(const struct lu_env *env,
2264 struct dt_object *dt,
2267 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2268 struct osd_object *obj = osd_dt_obj(dt);
2269 struct inode *inode = obj->oo_inode;
2270 struct osd_device *osd = osd_obj2dev(obj);
2271 struct osd_thandle *oh;
2275 oh = container_of0(th, struct osd_thandle, ot_super);
2276 LASSERT(oh->ot_handle);
2278 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2280 if (unlikely(fid_is_acct(fid)))
2283 if (S_ISDIR(inode->i_mode)) {
2284 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2285 /* it will check/delete the inode from remote parent,
2286 * how to optimize it? unlink performance impaction XXX */
2287 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2288 if (result != 0 && result != -ENOENT) {
2289 CERROR("%s: delete inode "DFID": rc = %d\n",
2290 osd_name(osd), PFID(fid), result);
2292 spin_lock(&obj->oo_guard);
2294 spin_unlock(&obj->oo_guard);
2295 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2298 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2300 result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2302 /* XXX: add to ext3 orphan list */
2303 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2305 /* not needed in the cache anymore */
2306 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2312 * Put the fid into lustre_mdt_attrs, and then place the structure
2313 * inode's ea. This fid should not be altered during the life time
2316 * \retval +ve, on success
2317 * \retval -ve, on error
2319 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2321 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2322 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2324 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2328 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2331 lustre_lma_init(lma, fid, compat, incompat);
2332 lustre_lma_swab(lma);
2334 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2336 /* LMA may already exist, but we need to check that all the
2337 * desired compat/incompat flags have been added. */
2338 if (unlikely(rc == -EEXIST)) {
2339 if (compat == 0 && incompat == 0)
2342 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2343 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2348 lustre_lma_swab(lma);
2349 if (!(~lma->lma_compat & compat) &&
2350 !(~lma->lma_incompat & incompat))
2353 lma->lma_compat |= compat;
2354 lma->lma_incompat |= incompat;
2355 lustre_lma_swab(lma);
2356 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2357 sizeof(*lma), XATTR_REPLACE);
2364 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2365 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2366 * To have compatilibility with 1.8 ldiskfs driver we need to have
2367 * magic number at start of fid data.
2368 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2371 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2372 const struct dt_rec *fid)
2374 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2375 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2376 param->edp_magic = 0;
2380 param->edp_magic = LDISKFS_LUFID_MAGIC;
2381 param->edp_len = sizeof(struct lu_fid) + 1;
2382 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2386 * Try to read the fid from inode ea into dt_rec.
2388 * \param fid object fid.
2390 * \retval 0 on success
2392 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2393 __u32 ino, struct lu_fid *fid,
2394 struct osd_inode_id *id)
2396 struct osd_thread_info *info = osd_oti_get(env);
2397 struct inode *inode;
2400 osd_id_gen(id, ino, OSD_OII_NOGEN);
2401 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2403 RETURN(PTR_ERR(inode));
2409 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2411 struct inode *parent_dir,
2412 const struct dt_rec *dot_fid,
2413 const struct dt_rec *dot_dot_fid,
2414 struct osd_thandle *oth)
2416 struct ldiskfs_dentry_param *dot_ldp;
2417 struct ldiskfs_dentry_param *dot_dot_ldp;
2419 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2420 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2422 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2423 dot_ldp->edp_magic = 0;
2424 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2425 dir, dot_ldp, dot_dot_ldp);
2429 * Create an local agent inode for remote entry
2431 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2432 struct osd_device *osd,
2433 struct osd_object *pobj,
2434 const struct lu_fid *fid,
2437 struct osd_thread_info *info = osd_oti_get(env);
2438 struct inode *local;
2439 struct osd_thandle *oh;
2444 oh = container_of(th, struct osd_thandle, ot_super);
2445 LASSERT(oh->ot_handle->h_transaction != NULL);
2447 /* FIXME: Insert index api needs to know the mode of
2448 * the remote object. Just use S_IFDIR for now */
2449 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2450 if (IS_ERR(local)) {
2451 CERROR("%s: create local error %d\n", osd_name(osd),
2452 (int)PTR_ERR(local));
2456 /* Set special LMA flag for local agent inode */
2457 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2459 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2460 osd_name(osd), PFID(fid), rc);
2461 RETURN(ERR_PTR(rc));
2464 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2465 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2466 (const struct dt_rec *)fid, oh);
2468 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2469 osd_name(osd), PFID(fid), rc);
2470 RETURN(ERR_PTR(rc));
2477 * Delete local agent inode for remote entry
2479 static int osd_delete_local_agent_inode(const struct lu_env *env,
2480 struct osd_device *osd,
2481 const struct lu_fid *fid,
2482 __u32 ino, struct osd_thandle *oh)
2484 struct osd_thread_info *oti = osd_oti_get(env);
2485 struct osd_inode_id *id = &oti->oti_id;
2486 struct inode *inode;
2489 id->oii_ino = le32_to_cpu(ino);
2490 id->oii_gen = OSD_OII_NOGEN;
2491 inode = osd_iget(oti, osd, id);
2492 if (IS_ERR(inode)) {
2493 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2494 PFID(fid), id->oii_ino, id->oii_gen);
2495 RETURN(PTR_ERR(inode));
2499 mark_inode_dirty(inode);
2500 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2501 osd_name(osd), PFID(fid), inode->i_ino);
2507 * OSD layer object create function for interoperability mode (b11826).
2508 * This is mostly similar to osd_object_create(). Only difference being, fid is
2509 * inserted into inode ea here.
2511 * \retval 0, on success
2512 * \retval -ve, on error
2514 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2515 struct lu_attr *attr,
2516 struct dt_allocation_hint *hint,
2517 struct dt_object_format *dof,
2520 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2521 struct osd_object *obj = osd_dt_obj(dt);
2522 struct osd_thread_info *info = osd_oti_get(env);
2527 LASSERT(osd_invariant(obj));
2528 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2529 LASSERT(osd_write_locked(env, obj));
2530 LASSERT(th != NULL);
2532 if (unlikely(fid_is_acct(fid)))
2533 /* Quota files can't be created from the kernel any more,
2534 * 'tune2fs -O quota' will take care of creating them */
2537 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2538 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2540 result = __osd_object_create(info, obj, attr, hint, dof, th);
2542 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2543 fid_is_on_ost(info, osd_obj2dev(obj),
2544 fid, OI_CHECK_FLD) ?
2545 LMAC_FID_ON_OST : 0, 0);
2548 result = __osd_oi_insert(env, obj, fid, th);
2550 LASSERT(ergo(result == 0,
2551 dt_object_exists(dt) && !dt_object_remote(dt)));
2552 LINVRNT(osd_invariant(obj));
2556 static int osd_declare_object_ref_add(const struct lu_env *env,
2557 struct dt_object *dt,
2558 struct thandle *handle)
2560 struct osd_thandle *oh;
2562 /* it's possible that object doesn't exist yet */
2563 LASSERT(handle != NULL);
2565 oh = container_of0(handle, struct osd_thandle, ot_super);
2566 LASSERT(oh->ot_handle == NULL);
2568 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2569 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2575 * Concurrency: @dt is write locked.
2577 static int osd_object_ref_add(const struct lu_env *env,
2578 struct dt_object *dt, struct thandle *th)
2580 struct osd_object *obj = osd_dt_obj(dt);
2581 struct inode *inode = obj->oo_inode;
2582 bool need_dirty = false;
2585 LINVRNT(osd_invariant(obj));
2586 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2587 LASSERT(osd_write_locked(env, obj));
2588 LASSERT(th != NULL);
2590 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2592 /* This based on ldiskfs_inc_count(), which is not exported.
2594 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2595 * (65000) subdirectories by storing "1" in i_nlink if the link count
2596 * would otherwise overflow. Directory tranversal tools understand
2597 * that (st_nlink == 1) indicates that the filesystem dose not track
2598 * hard links count on the directory, and will not abort subdirectory
2599 * scanning early once (st_nlink - 2) subdirs have been found.
2601 * This also has to properly handle the case of inodes with nlink == 0
2602 * in case they are being linked into the PENDING directory
2604 spin_lock(&obj->oo_guard);
2605 if (unlikely(!S_ISDIR(inode->i_mode) &&
2606 inode->i_nlink >= LDISKFS_LINK_MAX)) {
2607 /* MDD should have checked this, but good to be safe */
2609 } else if (unlikely(inode->i_nlink == 0 ||
2610 (S_ISDIR(inode->i_mode) &&
2611 inode->i_nlink >= LDISKFS_LINK_MAX))) {
2612 /* inc_nlink from 0 may cause WARN_ON */
2613 set_nlink(inode, 1);
2615 } else if (!S_ISDIR(inode->i_mode) ||
2616 (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2619 } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2621 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2622 spin_unlock(&obj->oo_guard);
2625 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2627 LINVRNT(osd_invariant(obj));
2632 static int osd_declare_object_ref_del(const struct lu_env *env,
2633 struct dt_object *dt,
2634 struct thandle *handle)
2636 struct osd_thandle *oh;
2638 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2639 LASSERT(handle != NULL);
2641 oh = container_of0(handle, struct osd_thandle, ot_super);
2642 LASSERT(oh->ot_handle == NULL);
2644 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2645 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2651 * Concurrency: @dt is write locked.
2653 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2656 struct osd_object *obj = osd_dt_obj(dt);
2657 struct inode *inode = obj->oo_inode;
2659 LINVRNT(osd_invariant(obj));
2660 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2661 LASSERT(osd_write_locked(env, obj));
2662 LASSERT(th != NULL);
2664 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2666 spin_lock(&obj->oo_guard);
2667 LASSERT(inode->i_nlink > 0);
2669 /* This based on ldiskfs_dec_count(), which is not exported.
2671 * If a directory already has nlink == 1, then do not drop the nlink
2672 * count to 0, even temporarily, to avoid race conditions with other
2673 * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2675 * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2677 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2680 spin_unlock(&obj->oo_guard);
2681 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2682 LINVRNT(osd_invariant(obj));
2684 spin_unlock(&obj->oo_guard);
2691 * Get the 64-bit version for an inode.
2693 static int osd_object_version_get(const struct lu_env *env,
2694 struct dt_object *dt, dt_obj_version_t *ver)
2696 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2698 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2699 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2700 *ver = LDISKFS_I(inode)->i_fs_version;
2705 * Concurrency: @dt is read locked.
2707 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2708 struct lu_buf *buf, const char *name,
2709 struct lustre_capa *capa)
2711 struct osd_object *obj = osd_dt_obj(dt);
2712 struct inode *inode = obj->oo_inode;
2713 struct osd_thread_info *info = osd_oti_get(env);
2714 struct dentry *dentry = &info->oti_obj_dentry;
2716 /* version get is not real XATTR but uses xattr API */
2717 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2718 /* for version we are just using xattr API but change inode
2720 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2721 osd_object_version_get(env, dt, buf->lb_buf);
2722 return sizeof(dt_obj_version_t);
2725 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2726 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2728 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2731 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2735 static int osd_declare_xattr_set(const struct lu_env *env,
2736 struct dt_object *dt,
2737 const struct lu_buf *buf, const char *name,
2738 int fl, struct thandle *handle)
2740 struct osd_thandle *oh;
2742 LASSERT(handle != NULL);
2744 oh = container_of0(handle, struct osd_thandle, ot_super);
2745 LASSERT(oh->ot_handle == NULL);
2747 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2748 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2749 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2750 osd_dto_credits_noquota[DTO_XATTR_SET]);
2756 * Set the 64-bit version for object
2758 static void osd_object_version_set(const struct lu_env *env,
2759 struct dt_object *dt,
2760 dt_obj_version_t *new_version)
2762 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2764 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2765 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2767 LDISKFS_I(inode)->i_fs_version = *new_version;
2768 /** Version is set after all inode operations are finished,
2769 * so we should mark it dirty here */
2770 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2774 * Concurrency: @dt is write locked.
2776 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2777 const struct lu_buf *buf, const char *name, int fl,
2778 struct thandle *handle, struct lustre_capa *capa)
2780 struct osd_object *obj = osd_dt_obj(dt);
2781 struct inode *inode = obj->oo_inode;
2782 struct osd_thread_info *info = osd_oti_get(env);
2786 LASSERT(handle != NULL);
2788 /* version set is not real XATTR */
2789 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2790 /* for version we are just using xattr API but change inode
2792 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2793 osd_object_version_set(env, dt, buf->lb_buf);
2794 return sizeof(dt_obj_version_t);
2797 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2800 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2801 if (fl & LU_XATTR_REPLACE)
2802 fs_flags |= XATTR_REPLACE;
2804 if (fl & LU_XATTR_CREATE)
2805 fs_flags |= XATTR_CREATE;
2807 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2812 * Concurrency: @dt is read locked.
2814 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2815 struct lu_buf *buf, struct lustre_capa *capa)
2817 struct osd_object *obj = osd_dt_obj(dt);
2818 struct inode *inode = obj->oo_inode;
2819 struct osd_thread_info *info = osd_oti_get(env);
2820 struct dentry *dentry = &info->oti_obj_dentry;
2822 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2823 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2824 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2826 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2829 dentry->d_inode = inode;
2830 dentry->d_sb = inode->i_sb;
2831 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2834 static int osd_declare_xattr_del(const struct lu_env *env,
2835 struct dt_object *dt, const char *name,
2836 struct thandle *handle)
2838 struct osd_thandle *oh;
2840 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2841 LASSERT(handle != NULL);
2843 oh = container_of0(handle, struct osd_thandle, ot_super);
2844 LASSERT(oh->ot_handle == NULL);
2846 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2847 osd_dto_credits_noquota[DTO_XATTR_SET]);
2853 * Concurrency: @dt is write locked.
2855 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2856 const char *name, struct thandle *handle,
2857 struct lustre_capa *capa)
2859 struct osd_object *obj = osd_dt_obj(dt);
2860 struct inode *inode = obj->oo_inode;
2861 struct osd_thread_info *info = osd_oti_get(env);
2862 struct dentry *dentry = &info->oti_obj_dentry;
2865 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2866 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2867 LASSERT(handle != NULL);
2869 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2872 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2874 ll_vfs_dq_init(inode);
2875 dentry->d_inode = inode;
2876 dentry->d_sb = inode->i_sb;
2877 rc = inode->i_op->removexattr(dentry, name);
2881 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2882 struct dt_object *dt,
2883 struct lustre_capa *old, __u64 opc)
2885 struct osd_thread_info *info = osd_oti_get(env);
2886 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2887 struct osd_object *obj = osd_dt_obj(dt);
2888 struct osd_device *osd = osd_obj2dev(obj);
2889 struct lustre_capa_key *key = &info->oti_capa_key;
2890 struct lustre_capa *capa = &info->oti_capa;
2891 struct obd_capa *oc;
2892 struct lu_capainfo *lci;
2896 if (!osd->od_fl_capa)
2897 RETURN(ERR_PTR(-ENOENT));
2899 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2900 LINVRNT(osd_invariant(obj));
2902 /* renewal sanity check */
2903 if (old && osd_object_auth(env, dt, old, opc))
2904 RETURN(ERR_PTR(-EACCES));
2906 lci = lu_capainfo_get(env);
2907 if (unlikely(lci == NULL))
2908 RETURN(ERR_PTR(-ENOENT));
2910 switch (lci->lci_auth) {
2914 capa->lc_uid = obj->oo_inode->i_uid;
2915 capa->lc_gid = obj->oo_inode->i_gid;
2916 capa->lc_flags = LC_ID_PLAIN;
2918 case LC_ID_CONVERT: {
2921 s[0] = obj->oo_inode->i_uid;
2922 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2923 s[2] = obj->oo_inode->i_gid;
2924 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2925 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2927 RETURN(ERR_PTR(rc));
2929 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2930 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2931 capa->lc_flags = LC_ID_CONVERT;
2935 RETURN(ERR_PTR(-EINVAL));
2938 capa->lc_fid = *fid;
2940 capa->lc_flags |= osd->od_capa_alg << 24;
2941 capa->lc_timeout = osd->od_capa_timeout;
2942 capa->lc_expiry = 0;
2944 oc = capa_lookup(osd->od_capa_hash, capa, 1);
2946 LASSERT(!capa_is_expired(oc));
2950 spin_lock(&capa_lock);
2951 *key = osd->od_capa_keys[1];
2952 spin_unlock(&capa_lock);
2954 capa->lc_keyid = key->lk_keyid;
2955 capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
2957 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2959 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2960 RETURN(ERR_PTR(rc));
2963 oc = capa_add(osd->od_capa_hash, capa);
2967 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2969 struct osd_object *obj = osd_dt_obj(dt);
2970 struct inode *inode = obj->oo_inode;
2971 struct osd_thread_info *info = osd_oti_get(env);
2972 struct dentry *dentry = &info->oti_obj_dentry;
2973 struct file *file = &info->oti_file;
2978 dentry->d_inode = inode;
2979 dentry->d_sb = inode->i_sb;
2980 file->f_dentry = dentry;
2981 file->f_mapping = inode->i_mapping;
2982 file->f_op = inode->i_fop;
2983 #ifndef HAVE_FILE_FSYNC_4ARGS
2984 mutex_lock(&inode->i_mutex);
2986 rc = do_fsync(file, 0);
2987 #ifndef HAVE_FILE_FSYNC_4ARGS
2988 mutex_unlock(&inode->i_mutex);
2993 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2996 struct osd_object *obj = osd_dt_obj(dt);
2999 *data = (void *)obj->oo_inode;
3007 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3008 const struct dt_index_features *feat)
3010 struct iam_descr *descr;
3012 if (osd_object_is_root(o))
3013 return feat == &dt_directory_features;
3015 LASSERT(o->oo_dir != NULL);
3017 descr = o->oo_dir->od_container.ic_descr;
3018 if (feat == &dt_directory_features) {
3019 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3025 feat->dif_keysize_min <= descr->id_key_size &&
3026 descr->id_key_size <= feat->dif_keysize_max &&
3027 feat->dif_recsize_min <= descr->id_rec_size &&
3028 descr->id_rec_size <= feat->dif_recsize_max &&
3029 !(feat->dif_flags & (DT_IND_VARKEY |
3030 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3031 ergo(feat->dif_flags & DT_IND_UPDATE,
3032 1 /* XXX check that object (and file system) is
3037 static int osd_iam_container_init(const struct lu_env *env,
3038 struct osd_object *obj,
3039 struct osd_directory *dir)
3041 struct iam_container *bag = &dir->od_container;
3044 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3048 result = iam_container_setup(bag);
3050 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3052 iam_container_fini(bag);
3059 * Concurrency: no external locking is necessary.
3061 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3062 const struct dt_index_features *feat)
3066 struct osd_object *obj = osd_dt_obj(dt);
3068 LINVRNT(osd_invariant(obj));
3070 if (osd_object_is_root(obj)) {
3071 dt->do_index_ops = &osd_index_ea_ops;
3073 } else if (feat == &dt_directory_features) {
3074 dt->do_index_ops = &osd_index_ea_ops;
3075 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3080 } else if (unlikely(feat == &dt_otable_features)) {
3081 dt->do_index_ops = &osd_otable_ops;
3083 } else if (unlikely(feat == &dt_acct_features)) {
3084 dt->do_index_ops = &osd_acct_index_ops;
3087 } else if (!osd_has_index(obj)) {
3088 struct osd_directory *dir;
3093 spin_lock(&obj->oo_guard);
3094 if (obj->oo_dir == NULL)
3098 * Concurrent thread allocated container data.
3101 spin_unlock(&obj->oo_guard);
3103 * Now, that we have container data, serialize its
3106 down_write(&obj->oo_ext_idx_sem);
3108 * recheck under lock.
3110 if (!osd_has_index(obj))
3111 result = osd_iam_container_init(env, obj, dir);
3114 up_write(&obj->oo_ext_idx_sem);
3122 if (result == 0 && skip_iam == 0) {
3123 if (!osd_iam_index_probe(env, obj, feat))
3126 LINVRNT(osd_invariant(obj));
3128 if (result == 0 && is_quota_glb_feat(feat) &&
3129 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3130 result = osd_quota_migration(env, dt, feat);
3135 static int osd_otable_it_attr_get(const struct lu_env *env,
3136 struct dt_object *dt,
3137 struct lu_attr *attr,
3138 struct lustre_capa *capa)
3144 static const struct dt_object_operations osd_obj_ops = {
3145 .do_read_lock = osd_object_read_lock,
3146 .do_write_lock = osd_object_write_lock,
3147 .do_read_unlock = osd_object_read_unlock,
3148 .do_write_unlock = osd_object_write_unlock,
3149 .do_write_locked = osd_object_write_locked,
3150 .do_attr_get = osd_attr_get,
3151 .do_declare_attr_set = osd_declare_attr_set,
3152 .do_attr_set = osd_attr_set,
3153 .do_ah_init = osd_ah_init,
3154 .do_declare_create = osd_declare_object_create,
3155 .do_create = osd_object_create,
3156 .do_declare_destroy = osd_declare_object_destroy,
3157 .do_destroy = osd_object_destroy,
3158 .do_index_try = osd_index_try,
3159 .do_declare_ref_add = osd_declare_object_ref_add,
3160 .do_ref_add = osd_object_ref_add,
3161 .do_declare_ref_del = osd_declare_object_ref_del,
3162 .do_ref_del = osd_object_ref_del,
3163 .do_xattr_get = osd_xattr_get,
3164 .do_declare_xattr_set = osd_declare_xattr_set,
3165 .do_xattr_set = osd_xattr_set,
3166 .do_declare_xattr_del = osd_declare_xattr_del,
3167 .do_xattr_del = osd_xattr_del,
3168 .do_xattr_list = osd_xattr_list,
3169 .do_capa_get = osd_capa_get,
3170 .do_object_sync = osd_object_sync,
3171 .do_data_get = osd_data_get,
3175 * dt_object_operations for interoperability mode
3176 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3178 static const struct dt_object_operations osd_obj_ea_ops = {
3179 .do_read_lock = osd_object_read_lock,
3180 .do_write_lock = osd_object_write_lock,
3181 .do_read_unlock = osd_object_read_unlock,
3182 .do_write_unlock = osd_object_write_unlock,
3183 .do_write_locked = osd_object_write_locked,
3184 .do_attr_get = osd_attr_get,
3185 .do_declare_attr_set = osd_declare_attr_set,
3186 .do_attr_set = osd_attr_set,
3187 .do_ah_init = osd_ah_init,
3188 .do_declare_create = osd_declare_object_create,
3189 .do_create = osd_object_ea_create,
3190 .do_declare_destroy = osd_declare_object_destroy,
3191 .do_destroy = osd_object_destroy,
3192 .do_index_try = osd_index_try,
3193 .do_declare_ref_add = osd_declare_object_ref_add,
3194 .do_ref_add = osd_object_ref_add,
3195 .do_declare_ref_del = osd_declare_object_ref_del,
3196 .do_ref_del = osd_object_ref_del,
3197 .do_xattr_get = osd_xattr_get,
3198 .do_declare_xattr_set = osd_declare_xattr_set,
3199 .do_xattr_set = osd_xattr_set,
3200 .do_declare_xattr_del = osd_declare_xattr_del,
3201 .do_xattr_del = osd_xattr_del,
3202 .do_xattr_list = osd_xattr_list,
3203 .do_capa_get = osd_capa_get,
3204 .do_object_sync = osd_object_sync,
3205 .do_data_get = osd_data_get,
3208 static const struct dt_object_operations osd_obj_otable_it_ops = {
3209 .do_attr_get = osd_otable_it_attr_get,
3210 .do_index_try = osd_index_try,
3213 static int osd_index_declare_iam_delete(const struct lu_env *env,
3214 struct dt_object *dt,
3215 const struct dt_key *key,
3216 struct thandle *handle)
3218 struct osd_thandle *oh;
3220 oh = container_of0(handle, struct osd_thandle, ot_super);
3221 LASSERT(oh->ot_handle == NULL);
3223 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3224 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3230 * delete a (key, value) pair from index \a dt specified by \a key
3232 * \param dt osd index object
3233 * \param key key for index
3234 * \param rec record reference
3235 * \param handle transaction handler
3238 * \retval -ve failure
3241 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3242 const struct dt_key *key,
3243 struct thandle *handle,
3244 struct lustre_capa *capa)
3246 struct osd_thread_info *oti = osd_oti_get(env);
3247 struct osd_object *obj = osd_dt_obj(dt);
3248 struct osd_thandle *oh;
3249 struct iam_path_descr *ipd;
3250 struct iam_container *bag = &obj->oo_dir->od_container;
3255 LINVRNT(osd_invariant(obj));
3256 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3257 LASSERT(bag->ic_object == obj->oo_inode);
3258 LASSERT(handle != NULL);
3260 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3263 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3265 ipd = osd_idx_ipd_get(env, bag);
3266 if (unlikely(ipd == NULL))
3269 oh = container_of0(handle, struct osd_thandle, ot_super);
3270 LASSERT(oh->ot_handle != NULL);
3271 LASSERT(oh->ot_handle->h_transaction != NULL);
3273 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3274 /* swab quota uid/gid provided by caller */
3275 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3276 key = (const struct dt_key *)&oti->oti_quota_id;
3279 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3280 osd_ipd_put(env, bag, ipd);
3281 LINVRNT(osd_invariant(obj));
3285 static int osd_index_declare_ea_delete(const struct lu_env *env,
3286 struct dt_object *dt,
3287 const struct dt_key *key,
3288 struct thandle *handle)
3290 struct osd_thandle *oh;
3291 struct inode *inode;
3295 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3296 LASSERT(handle != NULL);
3298 oh = container_of0(handle, struct osd_thandle, ot_super);
3299 LASSERT(oh->ot_handle == NULL);
3301 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3302 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3304 inode = osd_dt_obj(dt)->oo_inode;
3307 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3308 true, true, NULL, false);
3312 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3315 struct osd_fid_pack *rec;
3318 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3319 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3320 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3325 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3328 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3329 struct seq_server_site *ss = osd_seq_site(osd);
3333 /* Those FID seqs, which are not in FLDB, must be local seq */
3334 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3337 rc = osd_fld_lookup(env, osd, fid, range);
3339 CERROR("%s: Can not lookup fld for "DFID"\n",
3340 osd_name(osd), PFID(fid));
3344 RETURN(ss->ss_node_id != range->lsr_index);
3348 * Index delete function for interoperability mode (b11826).
3349 * It will remove the directory entry added by osd_index_ea_insert().
3350 * This entry is needed to maintain name->fid mapping.
3352 * \param key, key i.e. file entry to be deleted
3354 * \retval 0, on success
3355 * \retval -ve, on error
3357 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3358 const struct dt_key *key,
3359 struct thandle *handle,
3360 struct lustre_capa *capa)
3362 struct osd_object *obj = osd_dt_obj(dt);
3363 struct inode *dir = obj->oo_inode;
3364 struct dentry *dentry;
3365 struct osd_thandle *oh;
3366 struct ldiskfs_dir_entry_2 *de = NULL;
3367 struct buffer_head *bh;
3368 struct htree_lock *hlock = NULL;
3369 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3370 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3374 LINVRNT(osd_invariant(obj));
3375 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3376 LASSERT(handle != NULL);
3378 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3380 oh = container_of(handle, struct osd_thandle, ot_super);
3381 LASSERT(oh->ot_handle != NULL);
3382 LASSERT(oh->ot_handle->h_transaction != NULL);
3384 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3387 ll_vfs_dq_init(dir);
3388 dentry = osd_child_dentry_get(env, obj,
3389 (char *)key, strlen((char *)key));
3391 if (obj->oo_hl_head != NULL) {
3392 hlock = osd_oti_get(env)->oti_hlock;
3393 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3394 dir, LDISKFS_HLOCK_DEL);
3396 down_write(&obj->oo_ext_idx_sem);
3399 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3403 /* If this is not the ".." entry, it might be a remote DNE
3404 * entry and we need to check if the FID is for a remote
3405 * MDT. If the FID is not in the directory entry (e.g.
3406 * upgraded 1.8 filesystem without dirdata enabled) then
3407 * we need to get the FID from the LMA. For a remote directory
3408 * there HAS to be an LMA, it cannot be an IGIF inode in this
3411 * Delete the entry before the agent inode in order to
3412 * simplify error handling. At worst an error after deleting
3413 * the entry first might leak the agent inode afterward. The
3414 * reverse would need filesystem abort in case of error deleting
3415 * the entry after the agent had been removed, or leave a
3416 * dangling entry pointing at a random inode. */
3417 if (strcmp((char *)key, dotdot) != 0) {
3418 LASSERT(de != NULL);
3419 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3420 /* If Fid is not in dentry, try to get it from LMA */
3421 if (rc == -ENODATA) {
3422 struct osd_inode_id *id;
3423 struct inode *inode;
3425 /* Before trying to get fid from the inode,
3426 * check whether the inode is valid.
3428 * If the inode has been deleted, do not go
3429 * ahead to do osd_ea_fid_get, which will set
3430 * the inode to bad inode, which might cause
3431 * the inode to be deleted uncorrectly */
3432 inode = ldiskfs_iget(osd_sb(osd),
3433 le32_to_cpu(de->inode));
3434 if (IS_ERR(inode)) {
3435 CDEBUG(D_INODE, "%s: "DFID"get inode"
3436 "error.\n", osd_name(osd),
3438 rc = PTR_ERR(inode);
3440 if (likely(inode->i_nlink != 0)) {
3441 id = &osd_oti_get(env)->oti_id;
3442 rc = osd_ea_fid_get(env, obj,
3443 le32_to_cpu(de->inode),
3446 CDEBUG(D_INFO, "%s: %u "DFID
3449 le32_to_cpu(de->inode),
3457 unlikely(osd_remote_fid(env, osd, fid)))
3458 /* Need to delete agent inode */
3459 ino = le32_to_cpu(de->inode);
3461 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3463 if (rc == 0 && unlikely(ino != 0)) {
3464 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3467 CERROR("%s: del local inode "DFID": rc = %d\n",
3468 osd_name(osd), PFID(fid), rc);
3474 ldiskfs_htree_unlock(hlock);
3476 up_write(&obj->oo_ext_idx_sem);
3481 /* For inode on the remote MDT, .. will point to
3482 * /Agent directory, Check whether it needs to delete
3483 * from agent directory */
3484 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3485 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3487 if (rc != 0 && rc != -ENOENT) {
3488 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3489 osd_name(osd), PFID(fid), rc);
3499 LASSERT(osd_invariant(obj));
3504 * Lookup index for \a key and copy record to \a rec.
3506 * \param dt osd index object
3507 * \param key key for index
3508 * \param rec record reference
3510 * \retval +ve success : exact mach
3511 * \retval 0 return record with key not greater than \a key
3512 * \retval -ve failure
3514 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3515 struct dt_rec *rec, const struct dt_key *key,
3516 struct lustre_capa *capa)
3518 struct osd_object *obj = osd_dt_obj(dt);
3519 struct iam_path_descr *ipd;
3520 struct iam_container *bag = &obj->oo_dir->od_container;
3521 struct osd_thread_info *oti = osd_oti_get(env);
3522 struct iam_iterator *it = &oti->oti_idx_it;
3523 struct iam_rec *iam_rec;
3528 LASSERT(osd_invariant(obj));
3529 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3530 LASSERT(bag->ic_object == obj->oo_inode);
3532 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3535 ipd = osd_idx_ipd_get(env, bag);
3539 /* got ipd now we can start iterator. */
3540 iam_it_init(it, bag, 0, ipd);
3542 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3543 /* swab quota uid/gid provided by caller */
3544 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3545 key = (const struct dt_key *)&oti->oti_quota_id;
3548 rc = iam_it_get(it, (struct iam_key *)key);
3550 if (S_ISDIR(obj->oo_inode->i_mode))
3551 iam_rec = (struct iam_rec *)oti->oti_ldp;
3553 iam_rec = (struct iam_rec *) rec;
3555 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3557 if (S_ISDIR(obj->oo_inode->i_mode))
3558 osd_fid_unpack((struct lu_fid *) rec,
3559 (struct osd_fid_pack *)iam_rec);
3560 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3561 osd_quota_unpack(obj, rec);
3566 osd_ipd_put(env, bag, ipd);
3568 LINVRNT(osd_invariant(obj));
3573 static int osd_index_declare_iam_insert(const struct lu_env *env,
3574 struct dt_object *dt,
3575 const struct dt_rec *rec,
3576 const struct dt_key *key,
3577 struct thandle *handle)
3579 struct osd_thandle *oh;
3581 LASSERT(handle != NULL);
3583 oh = container_of0(handle, struct osd_thandle, ot_super);
3584 LASSERT(oh->ot_handle == NULL);
3586 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3587 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3593 * Inserts (key, value) pair in \a dt index object.
3595 * \param dt osd index object
3596 * \param key key for index
3597 * \param rec record reference
3598 * \param th transaction handler
3601 * \retval -ve failure
3603 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3604 const struct dt_rec *rec,
3605 const struct dt_key *key, struct thandle *th,
3606 struct lustre_capa *capa, int ignore_quota)
3608 struct osd_object *obj = osd_dt_obj(dt);
3609 struct iam_path_descr *ipd;
3610 struct osd_thandle *oh;
3611 struct iam_container *bag = &obj->oo_dir->od_container;
3612 struct osd_thread_info *oti = osd_oti_get(env);
3613 struct iam_rec *iam_rec;
3618 LINVRNT(osd_invariant(obj));
3619 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3620 LASSERT(bag->ic_object == obj->oo_inode);
3621 LASSERT(th != NULL);
3623 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3626 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3628 ipd = osd_idx_ipd_get(env, bag);
3629 if (unlikely(ipd == NULL))
3632 oh = container_of0(th, struct osd_thandle, ot_super);
3633 LASSERT(oh->ot_handle != NULL);
3634 LASSERT(oh->ot_handle->h_transaction != NULL);
3635 if (S_ISDIR(obj->oo_inode->i_mode)) {
3636 iam_rec = (struct iam_rec *)oti->oti_ldp;
3637 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3638 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3639 /* pack quota uid/gid */
3640 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3641 key = (const struct dt_key *)&oti->oti_quota_id;
3642 /* pack quota record */
3643 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3644 iam_rec = (struct iam_rec *)rec;
3646 iam_rec = (struct iam_rec *)rec;
3649 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3651 osd_ipd_put(env, bag, ipd);
3652 LINVRNT(osd_invariant(obj));
3657 * Calls ldiskfs_add_entry() to add directory entry
3658 * into the directory. This is required for
3659 * interoperability mode (b11826)
3661 * \retval 0, on success
3662 * \retval -ve, on error
3664 static int __osd_ea_add_rec(struct osd_thread_info *info,
3665 struct osd_object *pobj, struct inode *cinode,
3666 const char *name, const struct dt_rec *fid,
3667 struct htree_lock *hlock, struct thandle *th)
3669 struct ldiskfs_dentry_param *ldp;
3670 struct dentry *child;
3671 struct osd_thandle *oth;
3674 oth = container_of(th, struct osd_thandle, ot_super);
3675 LASSERT(oth->ot_handle != NULL);
3676 LASSERT(oth->ot_handle->h_transaction != NULL);
3677 LASSERT(pobj->oo_inode);
3679 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3680 if (unlikely(pobj->oo_inode ==
3681 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3684 osd_get_ldiskfs_dirent_param(ldp, fid);
3685 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3686 child->d_fsdata = (void *)ldp;
3687 ll_vfs_dq_init(pobj->oo_inode);
3688 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3694 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3695 * into the directory.Also sets flags into osd object to
3696 * indicate dot and dotdot are created. This is required for
3697 * interoperability mode (b11826)
3699 * \param dir directory for dot and dotdot fixup.
3700 * \param obj child object for linking
3702 * \retval 0, on success
3703 * \retval -ve, on error
3705 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3706 struct osd_object *dir,
3707 struct inode *parent_dir, const char *name,
3708 const struct dt_rec *dot_fid,
3709 const struct dt_rec *dot_dot_fid,
3712 struct inode *inode = dir->oo_inode;
3713 struct osd_thandle *oth;
3716 oth = container_of(th, struct osd_thandle, ot_super);
3717 LASSERT(oth->ot_handle->h_transaction != NULL);
3718 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3720 if (strcmp(name, dot) == 0) {
3721 if (dir->oo_compat_dot_created) {
3724 LASSERT(inode == parent_dir);
3725 dir->oo_compat_dot_created = 1;
3728 } else if (strcmp(name, dotdot) == 0) {
3729 if (!dir->oo_compat_dot_created)
3731 /* in case of rename, dotdot is already created */
3732 if (dir->oo_compat_dotdot_created) {
3733 return __osd_ea_add_rec(info, dir, parent_dir, name,
3734 dot_dot_fid, NULL, th);
3737 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3738 parent_dir, dot_fid,
3741 dir->oo_compat_dotdot_created = 1;
3749 * It will call the appropriate osd_add* function and return the
3750 * value, return by respective functions.
3752 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3753 struct inode *cinode, const char *name,
3754 const struct dt_rec *fid, struct thandle *th)
3756 struct osd_thread_info *info = osd_oti_get(env);
3757 struct htree_lock *hlock;
3760 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3762 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3764 if (hlock != NULL) {
3765 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3768 down_write(&pobj->oo_ext_idx_sem);
3770 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3771 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3774 if (hlock != NULL) {
3775 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3776 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3778 down_write(&pobj->oo_ext_idx_sem);
3781 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3782 struct lu_fid *tfid = &info->oti_fid;
3784 *tfid = *(const struct lu_fid *)fid;
3786 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3787 (const struct dt_rec *)tfid,
3790 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3795 ldiskfs_htree_unlock(hlock);
3797 up_write(&pobj->oo_ext_idx_sem);
3803 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3804 struct osd_idmap_cache *oic)
3806 struct osd_scrub *scrub = &dev->od_scrub;
3807 struct lu_fid *fid = &oic->oic_fid;
3808 struct osd_inode_id *id = &oti->oti_id;
3813 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3817 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3818 if (rc != 0 && rc != -ENOENT)
3821 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3824 if (thread_is_running(&scrub->os_thread)) {
3825 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3826 /* There is race condition between osd_oi_lookup and OI scrub.
3827 * The OI scrub finished just after osd_oi_lookup() failure.
3828 * Under such case, it is unnecessary to trigger OI scrub again,
3829 * but try to call osd_oi_lookup() again. */
3830 if (unlikely(rc == -EAGAIN))
3836 if (!dev->od_noscrub && ++once == 1) {
3837 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3839 rc = osd_scrub_start(dev);
3840 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3842 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3851 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3852 struct osd_device *dev,
3853 struct osd_idmap_cache *oic,
3854 struct lu_fid *fid, __u32 ino)
3856 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3857 struct inode *inode;
3860 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3861 inode = osd_iget(oti, dev, &oic->oic_lid);
3862 if (IS_ERR(inode)) {
3863 fid_zero(&oic->oic_fid);
3864 return PTR_ERR(inode);
3867 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3870 fid_zero(&oic->oic_fid);
3872 *fid = oic->oic_fid = lma->lma_self_fid;
3876 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3877 struct osd_inode_id *id, const struct lu_fid *fid)
3879 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3880 id->oii_ino, id->oii_gen, info);
3881 info->oti_cache.oic_lid = *id;
3882 info->oti_cache.oic_fid = *fid;
3888 * Calls ->lookup() to find dentry. From dentry get inode and
3889 * read inode's ea to get fid. This is required for interoperability
3892 * \retval 0, on success
3893 * \retval -ve, on error
3895 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3896 struct dt_rec *rec, const struct dt_key *key)
3898 struct inode *dir = obj->oo_inode;
3899 struct dentry *dentry;
3900 struct ldiskfs_dir_entry_2 *de;
3901 struct buffer_head *bh;
3902 struct lu_fid *fid = (struct lu_fid *) rec;
3903 struct htree_lock *hlock = NULL;
3908 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3910 dentry = osd_child_dentry_get(env, obj,
3911 (char *)key, strlen((char *)key));
3913 if (obj->oo_hl_head != NULL) {
3914 hlock = osd_oti_get(env)->oti_hlock;
3915 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3916 dir, LDISKFS_HLOCK_LOOKUP);
3918 down_read(&obj->oo_ext_idx_sem);
3921 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3923 struct osd_thread_info *oti = osd_oti_get(env);
3924 struct osd_inode_id *id = &oti->oti_id;
3925 struct osd_idmap_cache *oic = &oti->oti_cache;
3926 struct osd_device *dev = osd_obj2dev(obj);
3927 struct osd_scrub *scrub = &dev->od_scrub;
3928 struct scrub_file *sf = &scrub->os_file;
3930 ino = le32_to_cpu(de->inode);
3931 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3933 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3937 rc = osd_get_fid_from_dentry(de, rec);
3939 /* done with de, release bh */
3942 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3944 osd_id_gen(id, ino, OSD_OII_NOGEN);
3945 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3946 fid_zero(&oic->oic_fid);
3950 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3954 if ((scrub->os_pos_current <= ino) &&
3955 ((sf->sf_flags & SF_INCONSISTENT) ||
3956 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3957 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3959 osd_consistency_check(oti, dev, oic);
3968 ldiskfs_htree_unlock(hlock);
3970 up_read(&obj->oo_ext_idx_sem);
3975 * Find the osd object for given fid.
3977 * \param fid need to find the osd object having this fid
3979 * \retval osd_object on success
3980 * \retval -ve on error
3982 struct osd_object *osd_object_find(const struct lu_env *env,
3983 struct dt_object *dt,
3984 const struct lu_fid *fid)
3986 struct lu_device *ludev = dt->do_lu.lo_dev;
3987 struct osd_object *child = NULL;
3988 struct lu_object *luch;
3989 struct lu_object *lo;
3992 * at this point topdev might not exist yet
3993 * (i.e. MGS is preparing profiles). so we can
3994 * not rely on topdev and instead lookup with
3995 * our device passed as topdev. this can't work
3996 * if the object isn't cached yet (as osd doesn't
3997 * allocate lu_header). IOW, the object must be
3998 * in the cache, otherwise lu_object_alloc() crashes
4001 luch = lu_object_find_at(env, ludev, fid, NULL);
4002 if (!IS_ERR(luch)) {
4003 if (lu_object_exists(luch)) {
4004 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4006 child = osd_obj(lo);
4008 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4009 "lu_object can't be located"
4010 DFID"\n", PFID(fid));
4012 if (child == NULL) {
4013 lu_object_put(env, luch);
4014 CERROR("Unable to get osd_object\n");
4015 child = ERR_PTR(-ENOENT);
4018 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4019 "lu_object does not exists "DFID"\n",
4021 lu_object_put(env, luch);
4022 child = ERR_PTR(-ENOENT);
4025 child = (void *)luch;
4031 * Put the osd object once done with it.
4033 * \param obj osd object that needs to be put
4035 static inline void osd_object_put(const struct lu_env *env,
4036 struct osd_object *obj)
4038 lu_object_put(env, &obj->oo_dt.do_lu);
4041 static int osd_index_declare_ea_insert(const struct lu_env *env,
4042 struct dt_object *dt,
4043 const struct dt_rec *rec,
4044 const struct dt_key *key,
4045 struct thandle *handle)
4047 struct osd_thandle *oh;
4048 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4049 struct lu_fid *fid = (struct lu_fid *)rec;
4053 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4054 LASSERT(handle != NULL);
4056 oh = container_of0(handle, struct osd_thandle, ot_super);
4057 LASSERT(oh->ot_handle == NULL);
4059 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4060 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4062 if (osd_dt_obj(dt)->oo_inode == NULL) {
4063 const char *name = (const char *)key;
4064 /* Object is not being created yet. Only happens when
4065 * 1. declare directory create
4066 * 2. declare insert .
4067 * 3. declare insert ..
4069 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4071 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4073 /* We ignore block quota on meta pool (MDTs), so needn't
4074 * calculate how many blocks will be consumed by this index
4076 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4077 oh, true, true, NULL, false);
4083 rc = osd_remote_fid(env, osd, fid);
4089 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4090 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4091 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4092 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4093 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4094 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4100 * Index add function for interoperability mode (b11826).
4101 * It will add the directory entry.This entry is needed to
4102 * maintain name->fid mapping.
4104 * \param key it is key i.e. file entry to be inserted
4105 * \param rec it is value of given key i.e. fid
4107 * \retval 0, on success
4108 * \retval -ve, on error
4110 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4111 const struct dt_rec *rec,
4112 const struct dt_key *key, struct thandle *th,
4113 struct lustre_capa *capa, int ignore_quota)
4115 struct osd_object *obj = osd_dt_obj(dt);
4116 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4117 struct lu_fid *fid = (struct lu_fid *) rec;
4118 const char *name = (const char *)key;
4119 struct osd_thread_info *oti = osd_oti_get(env);
4120 struct osd_inode_id *id = &oti->oti_id;
4121 struct inode *child_inode = NULL;
4122 struct osd_object *child = NULL;
4126 LASSERT(osd_invariant(obj));
4127 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4128 LASSERT(th != NULL);
4130 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4132 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4135 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4137 rc = osd_remote_fid(env, osd, fid);
4139 CERROR("%s: Can not find object "DFID" rc %d\n",
4140 osd_name(osd), PFID(fid), rc);
4145 /* Insert remote entry */
4146 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4147 struct osd_mdobj_map *omm = osd->od_mdt_map;
4148 struct osd_thandle *oh;
4150 /* If parent on remote MDT, we need put this object
4152 oh = container_of(th, typeof(*oh), ot_super);
4153 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4155 CERROR("%s: add "DFID" error: rc = %d\n",
4157 PFID(lu_object_fid(&dt->do_lu)), rc);
4161 child_inode = igrab(omm->omm_remote_parent->d_inode);
4163 child_inode = osd_create_local_agent_inode(env, osd,
4166 if (IS_ERR(child_inode))
4167 RETURN(PTR_ERR(child_inode));
4170 /* Insert local entry */
4171 child = osd_object_find(env, dt, fid);
4172 if (IS_ERR(child)) {
4173 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4174 osd_name(osd), PFID(fid),
4175 id->oii_ino, id->oii_gen,
4176 (int)PTR_ERR(child_inode));
4177 RETURN(PTR_ERR(child_inode));
4179 child_inode = igrab(child->oo_inode);
4182 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4186 osd_object_put(env, child);
4187 LASSERT(osd_invariant(obj));
4192 * Initialize osd Iterator for given osd index object.
4194 * \param dt osd index object
4197 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4198 struct dt_object *dt,
4200 struct lustre_capa *capa)
4202 struct osd_it_iam *it;
4203 struct osd_thread_info *oti = osd_oti_get(env);
4204 struct osd_object *obj = osd_dt_obj(dt);
4205 struct lu_object *lo = &dt->do_lu;
4206 struct iam_path_descr *ipd;
4207 struct iam_container *bag = &obj->oo_dir->od_container;
4209 LASSERT(lu_object_exists(lo));
4211 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4212 return ERR_PTR(-EACCES);
4215 ipd = osd_it_ipd_get(env, bag);
4216 if (likely(ipd != NULL)) {
4220 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4221 return (struct dt_it *)it;
4223 return ERR_PTR(-ENOMEM);
4227 * free given Iterator.
4230 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4232 struct osd_it_iam *it = (struct osd_it_iam *)di;
4233 struct osd_object *obj = it->oi_obj;
4235 iam_it_fini(&it->oi_it);
4236 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4237 lu_object_put(env, &obj->oo_dt.do_lu);
4241 * Move Iterator to record specified by \a key
4243 * \param di osd iterator
4244 * \param key key for index
4246 * \retval +ve di points to record with least key not larger than key
4247 * \retval 0 di points to exact matched key
4248 * \retval -ve failure
4251 static int osd_it_iam_get(const struct lu_env *env,
4252 struct dt_it *di, const struct dt_key *key)
4254 struct osd_thread_info *oti = osd_oti_get(env);
4255 struct osd_it_iam *it = (struct osd_it_iam *)di;
4257 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4258 /* swab quota uid/gid */
4259 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4260 key = (struct dt_key *)&oti->oti_quota_id;
4263 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4269 * \param di osd iterator
4271 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4273 struct osd_it_iam *it = (struct osd_it_iam *)di;
4275 iam_it_put(&it->oi_it);
4279 * Move iterator by one record
4281 * \param di osd iterator
4283 * \retval +1 end of container reached
4285 * \retval -ve failure
4288 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4290 struct osd_it_iam *it = (struct osd_it_iam *)di;
4292 return iam_it_next(&it->oi_it);
4296 * Return pointer to the key under iterator.
4299 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4300 const struct dt_it *di)
4302 struct osd_thread_info *oti = osd_oti_get(env);
4303 struct osd_it_iam *it = (struct osd_it_iam *)di;
4304 struct osd_object *obj = it->oi_obj;
4307 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4309 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4310 /* swab quota uid/gid */
4311 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4312 key = (struct dt_key *)&oti->oti_quota_id;
4319 * Return size of key under iterator (in bytes)
4322 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4324 struct osd_it_iam *it = (struct osd_it_iam *)di;
4326 return iam_it_key_size(&it->oi_it);
4330 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4332 /* check if file type is required */
4333 if (ent->lde_attrs & LUDA_TYPE) {
4334 struct luda_type *lt;
4335 int align = sizeof(*lt) - 1;
4337 len = (len + align) & ~align;
4338 lt = (struct luda_type *)(ent->lde_name + len);
4339 lt->lt_type = cpu_to_le16(DTTOIF(type));
4342 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4346 * build lu direct from backend fs dirent.
4350 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4351 char *name, __u16 namelen, __u16 type, __u32 attr)
4353 ent->lde_attrs = attr | LUDA_FID;
4354 fid_cpu_to_le(&ent->lde_fid, fid);
4356 ent->lde_hash = cpu_to_le64(offset);
4357 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4359 strncpy(ent->lde_name, name, namelen);
4360 ent->lde_name[namelen] = '\0';
4361 ent->lde_namelen = cpu_to_le16(namelen);
4363 /* append lustre attributes */
4364 osd_it_append_attrs(ent, namelen, type);
4368 * Return pointer to the record under iterator.
4370 static int osd_it_iam_rec(const struct lu_env *env,
4371 const struct dt_it *di,
4372 struct dt_rec *dtrec, __u32 attr)
4374 struct osd_it_iam *it = (struct osd_it_iam *)di;
4375 struct osd_thread_info *info = osd_oti_get(env);
4378 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4379 const struct osd_fid_pack *rec;
4380 struct lu_fid *fid = &info->oti_fid;
4381 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4387 name = (char *)iam_it_key_get(&it->oi_it);
4389 RETURN(PTR_ERR(name));
4391 namelen = iam_it_key_size(&it->oi_it);
4393 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4395 RETURN(PTR_ERR(rec));
4397 rc = osd_fid_unpack(fid, rec);
4401 hash = iam_it_store(&it->oi_it);
4403 /* IAM does not store object type in IAM index (dir) */
4404 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4406 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4407 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4408 (struct iam_rec *)dtrec);
4409 osd_quota_unpack(it->oi_obj, dtrec);
4411 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4412 (struct iam_rec *)dtrec);
4419 * Returns cookie for current Iterator position.
4421 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4423 struct osd_it_iam *it = (struct osd_it_iam *)di;
4425 return iam_it_store(&it->oi_it);
4429 * Restore iterator from cookie.
4431 * \param di osd iterator
4432 * \param hash Iterator location cookie
4434 * \retval +ve di points to record with least key not larger than key.
4435 * \retval 0 di points to exact matched key
4436 * \retval -ve failure
4439 static int osd_it_iam_load(const struct lu_env *env,
4440 const struct dt_it *di, __u64 hash)
4442 struct osd_it_iam *it = (struct osd_it_iam *)di;
4444 return iam_it_load(&it->oi_it, hash);
4447 static const struct dt_index_operations osd_index_iam_ops = {
4448 .dio_lookup = osd_index_iam_lookup,
4449 .dio_declare_insert = osd_index_declare_iam_insert,
4450 .dio_insert = osd_index_iam_insert,
4451 .dio_declare_delete = osd_index_declare_iam_delete,
4452 .dio_delete = osd_index_iam_delete,
4454 .init = osd_it_iam_init,
4455 .fini = osd_it_iam_fini,
4456 .get = osd_it_iam_get,
4457 .put = osd_it_iam_put,
4458 .next = osd_it_iam_next,
4459 .key = osd_it_iam_key,
4460 .key_size = osd_it_iam_key_size,
4461 .rec = osd_it_iam_rec,
4462 .store = osd_it_iam_store,
4463 .load = osd_it_iam_load
4469 * Creates or initializes iterator context.
4471 * \retval struct osd_it_ea, iterator structure on success
4474 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4475 struct dt_object *dt,
4477 struct lustre_capa *capa)
4479 struct osd_object *obj = osd_dt_obj(dt);
4480 struct osd_thread_info *info = osd_oti_get(env);
4481 struct osd_it_ea *it = &info->oti_it_ea;
4482 struct file *file = &it->oie_file;
4483 struct lu_object *lo = &dt->do_lu;
4484 struct dentry *obj_dentry = &info->oti_it_dentry;
4486 LASSERT(lu_object_exists(lo));
4488 obj_dentry->d_inode = obj->oo_inode;
4489 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4490 obj_dentry->d_name.hash = 0;
4492 it->oie_rd_dirent = 0;
4493 it->oie_it_dirent = 0;
4494 it->oie_dirent = NULL;
4495 it->oie_buf = info->oti_it_ea_buf;
4498 /* Reset the "file" totally to avoid to reuse any old value from
4499 * former readdir handling, the "file->f_pos" should be zero. */
4500 memset(file, 0, sizeof(*file));
4501 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4502 if (attr & LUDA_64BITHASH)
4503 file->f_mode = FMODE_64BITHASH;
4505 file->f_mode = FMODE_32BITHASH;
4506 file->f_dentry = obj_dentry;
4507 file->f_mapping = obj->oo_inode->i_mapping;
4508 file->f_op = obj->oo_inode->i_fop;
4510 RETURN((struct dt_it *) it);
4514 * Destroy or finishes iterator context.
4516 * \param di iterator structure to be destroyed
4518 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4520 struct osd_it_ea *it = (struct osd_it_ea *)di;
4521 struct osd_object *obj = it->oie_obj;
4522 struct inode *inode = obj->oo_inode;
4525 it->oie_file.f_op->release(inode, &it->oie_file);
4526 lu_object_put(env, &obj->oo_dt.do_lu);
4531 * It position the iterator at given key, so that next lookup continues from
4532 * that key Or it is similar to dio_it->load() but based on a key,
4533 * rather than file position.
4535 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4538 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4540 static int osd_it_ea_get(const struct lu_env *env,
4541 struct dt_it *di, const struct dt_key *key)
4543 struct osd_it_ea *it = (struct osd_it_ea *)di;
4546 LASSERT(((const char *)key)[0] == '\0');
4547 it->oie_file.f_pos = 0;
4548 it->oie_rd_dirent = 0;
4549 it->oie_it_dirent = 0;
4550 it->oie_dirent = NULL;
4558 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4563 * It is called internally by ->readdir(). It fills the
4564 * iterator's in-memory data structure with required
4565 * information i.e. name, namelen, rec_size etc.
4567 * \param buf in which information to be filled in.
4568 * \param name name of the file in given dir
4570 * \retval 0 on success
4571 * \retval 1 on buffer full
4573 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4574 loff_t offset, __u64 ino,
4577 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4578 struct osd_object *obj = it->oie_obj;
4579 struct osd_it_ea_dirent *ent = it->oie_dirent;
4580 struct lu_fid *fid = &ent->oied_fid;
4581 struct osd_fid_pack *rec;
4584 /* this should never happen */
4585 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4586 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4590 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4594 /* "." is just the object itself. */
4595 if (namelen == 1 && name[0] == '.') {
4596 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4597 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4598 rec = (struct osd_fid_pack*) (name + namelen + 1);
4599 if (osd_fid_unpack(fid, rec) != 0)
4604 d_type &= ~LDISKFS_DIRENT_LUFID;
4606 /* NOT export local root. */
4607 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4608 ino = obj->oo_inode->i_ino;
4609 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4612 ent->oied_ino = ino;
4613 ent->oied_off = offset;
4614 ent->oied_namelen = namelen;
4615 ent->oied_type = d_type;
4617 memcpy(ent->oied_name, name, namelen);
4619 it->oie_rd_dirent++;
4620 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4625 * Calls ->readdir() to load a directory entry at a time
4626 * and stored it in iterator's in-memory data structure.
4628 * \param di iterator's in memory structure
4630 * \retval 0 on success
4631 * \retval -ve on error
4633 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4634 const struct dt_it *di)
4636 struct osd_it_ea *it = (struct osd_it_ea *)di;
4637 struct osd_object *obj = it->oie_obj;
4638 struct inode *inode = obj->oo_inode;
4639 struct htree_lock *hlock = NULL;
4643 it->oie_dirent = it->oie_buf;
4644 it->oie_rd_dirent = 0;
4646 if (obj->oo_hl_head != NULL) {
4647 hlock = osd_oti_get(env)->oti_hlock;
4648 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4649 inode, LDISKFS_HLOCK_READDIR);
4651 down_read(&obj->oo_ext_idx_sem);
4654 result = inode->i_fop->readdir(&it->oie_file, it,
4655 (filldir_t) osd_ldiskfs_filldir);
4658 ldiskfs_htree_unlock(hlock);
4660 up_read(&obj->oo_ext_idx_sem);
4662 if (it->oie_rd_dirent == 0) {
4665 it->oie_dirent = it->oie_buf;
4666 it->oie_it_dirent = 1;
4673 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4674 * to load a directory entry at a time and stored it in
4675 * iterator's in-memory data structure.
4677 * \param di iterator's in memory structure
4679 * \retval +ve iterator reached to end
4680 * \retval 0 iterator not reached to end
4681 * \retval -ve on error
4683 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4685 struct osd_it_ea *it = (struct osd_it_ea *)di;
4690 if (it->oie_it_dirent < it->oie_rd_dirent) {
4692 (void *) it->oie_dirent +
4693 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4694 it->oie_dirent->oied_namelen);
4695 it->oie_it_dirent++;
4698 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4701 rc = osd_ldiskfs_it_fill(env, di);
4708 * Returns the key at current position from iterator's in memory structure.
4710 * \param di iterator's in memory structure
4712 * \retval key i.e. struct dt_key on success
4714 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4715 const struct dt_it *di)
4717 struct osd_it_ea *it = (struct osd_it_ea *)di;
4719 return (struct dt_key *)it->oie_dirent->oied_name;
4723 * Returns the key's size at current position from iterator's in memory structure.
4725 * \param di iterator's in memory structure
4727 * \retval key_size i.e. struct dt_key on success
4729 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4731 struct osd_it_ea *it = (struct osd_it_ea *)di;
4733 return it->oie_dirent->oied_namelen;
4737 osd_dirent_update(handle_t *jh, struct super_block *sb,
4738 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4739 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4741 struct osd_fid_pack *rec;
4745 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4746 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4748 rc = ldiskfs_journal_get_write_access(jh, bh);
4750 CERROR("%.16s: fail to write access for update dirent: "
4751 "name = %.*s, rc = %d\n",
4752 LDISKFS_SB(sb)->s_es->s_volume_name,
4753 ent->oied_namelen, ent->oied_name, rc);
4757 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4758 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4759 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4761 CERROR("%.16s: fail to dirty metadata for update dirent: "
4762 "name = %.*s, rc = %d\n",
4763 LDISKFS_SB(sb)->s_es->s_volume_name,
4764 ent->oied_namelen, ent->oied_name, rc);
4770 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4772 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4773 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4780 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4782 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4783 "dot_dotdot = %d\n", dot_dotdot);
4785 if (LDISKFS_DIR_REC_LEN(de) >=
4786 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4793 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4794 struct inode *dir, struct inode *inode,
4795 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4796 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4797 struct htree_lock *hlock)
4799 struct dentry *dentry;
4800 struct osd_fid_pack *rec;
4801 struct ldiskfs_dentry_param *ldp;
4805 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4806 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4809 /* There is enough space to hold the FID-in-dirent. */
4810 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4811 dir->i_sb->s_blocksize)) {
4812 rc = ldiskfs_journal_get_write_access(jh, bh);
4814 CERROR("%.16s: fail to write access for reinsert "
4815 "dirent: name = %.*s, rc = %d\n",
4816 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4817 ent->oied_namelen, ent->oied_name, rc);
4821 de->name[de->name_len] = 0;
4822 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4823 rec->fp_len = sizeof(struct lu_fid) + 1;
4824 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4825 de->file_type |= LDISKFS_DIRENT_LUFID;
4827 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4829 CERROR("%.16s: fail to dirty metadata for reinsert "
4830 "dirent: name = %.*s, rc = %d\n",
4831 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4832 ent->oied_namelen, ent->oied_name, rc);
4837 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4839 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4840 "name = %.*s, rc = %d\n",
4841 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4842 ent->oied_namelen, ent->oied_name, rc);
4846 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4848 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4849 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4850 dentry->d_fsdata = (void *)ldp;
4851 ll_vfs_dq_init(dir);
4852 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4853 /* It is too bad, we cannot reinsert the name entry back.
4854 * That means we lose it! */
4856 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4857 "name = %.*s, rc = %d\n",
4858 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4859 ent->oied_namelen, ent->oied_name, rc);
4865 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4866 struct osd_it_ea *it, struct lu_fid *fid,
4867 struct osd_inode_id *id, __u32 *attr)
4869 struct osd_thread_info *info = osd_oti_get(env);
4870 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4871 struct osd_device *dev = osd_obj2dev(obj);
4872 struct super_block *sb = osd_sb(dev);
4873 const char *devname =
4874 LDISKFS_SB(sb)->s_es->s_volume_name;
4875 struct osd_it_ea_dirent *ent = it->oie_dirent;
4876 struct inode *dir = obj->oo_inode;
4877 struct htree_lock *hlock = NULL;
4878 struct buffer_head *bh = NULL;
4879 handle_t *jh = NULL;
4880 struct ldiskfs_dir_entry_2 *de;
4881 struct dentry *dentry;
4882 struct inode *inode;
4889 if (ent->oied_name[0] == '.') {
4890 if (ent->oied_namelen == 1)
4892 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4896 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4899 /* We need to ensure that the name entry is still valid.
4900 * Because it may be removed or renamed by other already.
4902 * The unlink or rename operation will start journal before PDO lock,
4903 * so to avoid deadlock, here we need to start journal handle before
4904 * related PDO lock also. But because we do not know whether there
4905 * will be something to be repaired before PDO lock, we just start
4906 * journal without conditions.
4908 * We may need to remove the name entry firstly, then insert back.
4909 * One credit is for user quota file update.
4910 * One credit is for group quota file update.
4911 * Two credits are for dirty inode. */
4912 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4913 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4916 if (dev->od_dirent_journal) {
4917 jh = ldiskfs_journal_start_sb(sb, credits);
4920 CERROR("%.16s: fail to start trans for dirent "
4921 "check_repair: credits %d, name %.*s, rc %d\n",
4922 devname, credits, ent->oied_namelen,
4923 ent->oied_name, rc);
4927 if (obj->oo_hl_head != NULL) {
4928 hlock = osd_oti_get(env)->oti_hlock;
4929 /* "0" means exclusive lock for the whole directory.
4930 * We need to prevent others access such name entry
4931 * during the delete + insert. Neither HLOCK_ADD nor
4932 * HLOCK_DEL cannot guarantee the atomicity. */
4933 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4935 down_write(&obj->oo_ext_idx_sem);
4938 if (obj->oo_hl_head != NULL) {
4939 hlock = osd_oti_get(env)->oti_hlock;
4940 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4941 LDISKFS_HLOCK_LOOKUP);
4943 down_read(&obj->oo_ext_idx_sem);
4947 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4948 /* For dot/dotdot entry, if there is not enough space to hold the
4949 * FID-in-dirent, just keep them there. It only happens when the
4950 * device upgraded from 1.8 or restored from MDT file-level backup.
4951 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4952 * and needs to get FID from LMA when readdir, it will not affect the
4953 * performance much. */
4954 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4955 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4956 *attr |= LUDA_IGNORE;
4957 GOTO(out_journal, rc = 0);
4960 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4961 inode = osd_iget(info, dev, id);
4962 if (IS_ERR(inode)) {
4963 rc = PTR_ERR(inode);
4964 if (rc == -ENOENT || rc == -ESTALE) {
4965 *attr |= LUDA_IGNORE;
4969 GOTO(out_journal, rc);
4972 /* skip the REMOTE_PARENT_DIR. */
4973 if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
4974 GOTO(out_inode, rc = 0);
4976 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4978 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
4980 if (fid_is_sane(fid)) {
4981 /* FID-in-dirent is valid. */
4982 if (lu_fid_eq(fid, &lma->lma_self_fid))
4983 GOTO(out_inode, rc = 0);
4985 /* Do not repair under dryrun mode. */
4986 if (*attr & LUDA_VERIFY_DRYRUN) {
4987 *attr |= LUDA_REPAIR;
4988 GOTO(out_inode, rc = 0);
4991 if (!dev->od_dirent_journal) {
4995 ldiskfs_htree_unlock(hlock);
4997 up_read(&obj->oo_ext_idx_sem);
4998 dev->od_dirent_journal = 1;
5002 *fid = lma->lma_self_fid;
5004 /* Update the FID-in-dirent. */
5005 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5007 *attr |= LUDA_REPAIR;
5009 /* Do not repair under dryrun mode. */
5010 if (*attr & LUDA_VERIFY_DRYRUN) {
5011 *fid = lma->lma_self_fid;
5012 *attr |= LUDA_REPAIR;
5013 GOTO(out_inode, rc = 0);
5016 if (!dev->od_dirent_journal) {
5020 ldiskfs_htree_unlock(hlock);
5022 up_read(&obj->oo_ext_idx_sem);
5023 dev->od_dirent_journal = 1;
5027 *fid = lma->lma_self_fid;
5029 /* Append the FID-in-dirent. */
5030 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5031 fid, bh, de, hlock);
5033 *attr |= LUDA_REPAIR;
5035 } else if (rc == -ENODATA) {
5036 /* Do not repair under dryrun mode. */
5037 if (*attr & LUDA_VERIFY_DRYRUN) {
5038 if (fid_is_sane(fid)) {
5039 *attr |= LUDA_REPAIR;
5041 lu_igif_build(fid, inode->i_ino,
5042 inode->i_generation);
5043 *attr |= LUDA_UPGRADE;
5045 GOTO(out_inode, rc = 0);
5048 if (!dev->od_dirent_journal) {
5052 ldiskfs_htree_unlock(hlock);
5054 up_read(&obj->oo_ext_idx_sem);
5055 dev->od_dirent_journal = 1;
5060 if (unlikely(fid_is_sane(fid))) {
5061 /* FID-in-dirent exists, but FID-in-LMA is lost.
5062 * Trust the FID-in-dirent, and add FID-in-LMA. */
5063 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5065 *attr |= LUDA_REPAIR;
5067 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5068 /* It is probably IGIF object. Only aappend the
5069 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5070 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5071 fid, bh, de, hlock);
5073 *attr |= LUDA_UPGRADE;
5077 GOTO(out_inode, rc);
5084 if (hlock != NULL) {
5085 ldiskfs_htree_unlock(hlock);
5087 if (dev->od_dirent_journal)
5088 up_write(&obj->oo_ext_idx_sem);
5090 up_read(&obj->oo_ext_idx_sem);
5093 ldiskfs_journal_stop(jh);
5094 if (rc >= 0 && !dirty)
5095 dev->od_dirent_journal = 0;
5100 * Returns the value at current position from iterator's in memory structure.
5102 * \param di struct osd_it_ea, iterator's in memory structure
5103 * \param attr attr requested for dirent.
5104 * \param lde lustre dirent
5106 * \retval 0 no error and \param lde has correct lustre dirent.
5107 * \retval -ve on error
5109 static inline int osd_it_ea_rec(const struct lu_env *env,
5110 const struct dt_it *di,
5111 struct dt_rec *dtrec, __u32 attr)
5113 struct osd_it_ea *it = (struct osd_it_ea *)di;
5114 struct osd_object *obj = it->oie_obj;
5115 struct osd_device *dev = osd_obj2dev(obj);
5116 struct osd_scrub *scrub = &dev->od_scrub;
5117 struct scrub_file *sf = &scrub->os_file;
5118 struct osd_thread_info *oti = osd_oti_get(env);
5119 struct osd_inode_id *id = &oti->oti_id;
5120 struct osd_idmap_cache *oic = &oti->oti_cache;
5121 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5122 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5123 __u32 ino = it->oie_dirent->oied_ino;
5127 if (attr & LUDA_VERIFY) {
5129 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5130 attr |= LUDA_IGNORE;
5133 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5137 attr &= ~LU_DIRENT_ATTRS_MASK;
5138 if (!fid_is_sane(fid)) {
5139 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5142 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5144 osd_id_gen(id, ino, OSD_OII_NOGEN);
5148 /* Pack the entry anyway, at least the offset is right. */
5149 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5150 it->oie_dirent->oied_name,
5151 it->oie_dirent->oied_namelen,
5152 it->oie_dirent->oied_type, attr);
5157 if (osd_remote_fid(env, dev, fid))
5160 if (likely(!(attr & LUDA_IGNORE)))
5161 rc = osd_add_oi_cache(oti, dev, id, fid);
5163 if (!(attr & LUDA_VERIFY) &&
5164 (scrub->os_pos_current <= ino) &&
5165 ((sf->sf_flags & SF_INCONSISTENT) ||
5166 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5167 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5168 osd_consistency_check(oti, dev, oic);
5174 * Returns a cookie for current position of the iterator head, so that
5175 * user can use this cookie to load/start the iterator next time.
5177 * \param di iterator's in memory structure
5179 * \retval cookie for current position, on success
5181 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5183 struct osd_it_ea *it = (struct osd_it_ea *)di;
5185 return it->oie_dirent->oied_off;
5189 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5190 * to load a directory entry at a time and stored it i inn,
5191 * in iterator's in-memory data structure.
5193 * \param di struct osd_it_ea, iterator's in memory structure
5195 * \retval +ve on success
5196 * \retval -ve on error
5198 static int osd_it_ea_load(const struct lu_env *env,
5199 const struct dt_it *di, __u64 hash)
5201 struct osd_it_ea *it = (struct osd_it_ea *)di;
5205 it->oie_file.f_pos = hash;
5207 rc = osd_ldiskfs_it_fill(env, di);
5215 * Index lookup function for interoperability mode (b11826).
5217 * \param key, key i.e. file name to be searched
5219 * \retval +ve, on success
5220 * \retval -ve, on error
5222 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5223 struct dt_rec *rec, const struct dt_key *key,
5224 struct lustre_capa *capa)
5226 struct osd_object *obj = osd_dt_obj(dt);
5231 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5232 LINVRNT(osd_invariant(obj));
5234 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5237 rc = osd_ea_lookup_rec(env, obj, rec, key);
5244 * Index and Iterator operations for interoperability
5245 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5247 static const struct dt_index_operations osd_index_ea_ops = {
5248 .dio_lookup = osd_index_ea_lookup,
5249 .dio_declare_insert = osd_index_declare_ea_insert,
5250 .dio_insert = osd_index_ea_insert,
5251 .dio_declare_delete = osd_index_declare_ea_delete,
5252 .dio_delete = osd_index_ea_delete,
5254 .init = osd_it_ea_init,
5255 .fini = osd_it_ea_fini,
5256 .get = osd_it_ea_get,
5257 .put = osd_it_ea_put,
5258 .next = osd_it_ea_next,
5259 .key = osd_it_ea_key,
5260 .key_size = osd_it_ea_key_size,
5261 .rec = osd_it_ea_rec,
5262 .store = osd_it_ea_store,
5263 .load = osd_it_ea_load
5267 static void *osd_key_init(const struct lu_context *ctx,
5268 struct lu_context_key *key)
5270 struct osd_thread_info *info;
5272 OBD_ALLOC_PTR(info);
5274 return ERR_PTR(-ENOMEM);
5276 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5277 if (info->oti_it_ea_buf == NULL)
5280 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5282 info->oti_hlock = ldiskfs_htree_lock_alloc();
5283 if (info->oti_hlock == NULL)
5289 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5292 return ERR_PTR(-ENOMEM);
5295 static void osd_key_fini(const struct lu_context *ctx,
5296 struct lu_context_key *key, void* data)
5298 struct osd_thread_info *info = data;
5300 if (info->oti_hlock != NULL)
5301 ldiskfs_htree_lock_free(info->oti_hlock);
5302 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5303 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5304 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5308 static void osd_key_exit(const struct lu_context *ctx,
5309 struct lu_context_key *key, void *data)
5311 struct osd_thread_info *info = data;
5313 LASSERT(info->oti_r_locks == 0);
5314 LASSERT(info->oti_w_locks == 0);
5315 LASSERT(info->oti_txns == 0);
5318 /* type constructor/destructor: osd_type_init, osd_type_fini */
5319 LU_TYPE_INIT_FINI(osd, &osd_key);
5321 struct lu_context_key osd_key = {
5322 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5323 .lct_init = osd_key_init,
5324 .lct_fini = osd_key_fini,
5325 .lct_exit = osd_key_exit
5329 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5330 const char *name, struct lu_device *next)
5332 struct osd_device *osd = osd_dev(d);
5334 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5335 >= sizeof(osd->od_svname))
5337 return osd_procfs_init(osd, name);
5340 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5344 /* shutdown quota slave instance associated with the device */
5345 if (o->od_quota_slave != NULL) {
5346 qsd_fini(env, o->od_quota_slave);
5347 o->od_quota_slave = NULL;
5353 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5358 fsfilt_put_ops(o->od_fsops);
5362 if (o->od_mnt != NULL) {
5363 shrink_dcache_sb(osd_sb(o));
5364 osd_sync(env, &o->od_dt_dev);
5373 static int osd_mount(const struct lu_env *env,
5374 struct osd_device *o, struct lustre_cfg *cfg)
5376 const char *name = lustre_cfg_string(cfg, 0);
5377 const char *dev = lustre_cfg_string(cfg, 1);
5379 unsigned long page, s_flags, lmd_flags = 0;
5380 struct page *__page;
5381 struct file_system_type *type;
5382 char *options = NULL;
5384 struct osd_thread_info *info = osd_oti_get(env);
5385 struct lu_fid *fid = &info->oti_fid;
5386 struct inode *inode;
5390 if (o->od_mnt != NULL)
5393 if (strlen(dev) >= sizeof(o->od_mntdev))
5395 strcpy(o->od_mntdev, dev);
5397 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5398 if (IS_ERR(o->od_fsops)) {
5399 CERROR("%s: Can't find fsfilt_ldiskfs\n", name);
5404 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5406 GOTO(out, rc = -ENOMEM);
5408 str = lustre_cfg_string(cfg, 2);
5409 s_flags = simple_strtoul(str, NULL, 0);
5410 str = strstr(str, ":");
5412 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5413 opts = lustre_cfg_string(cfg, 3);
5414 page = (unsigned long)page_address(__page);
5415 options = (char *)page;
5418 strcat(options, "user_xattr,acl");
5420 strcat(options, opts);
5422 /* Glom up mount options */
5423 if (*options != '\0')
5424 strcat(options, ",");
5425 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5427 type = get_fs_type("ldiskfs");
5429 CERROR("%s: cannot find ldiskfs module\n", name);
5430 GOTO(out, rc = -ENODEV);
5433 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5434 cfs_module_put(type->owner);
5436 if (IS_ERR(o->od_mnt)) {
5437 rc = PTR_ERR(o->od_mnt);
5439 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5443 #ifdef HAVE_DEV_SET_RDONLY
5444 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5445 CERROR("%s: underlying device %s is marked as read-only. "
5446 "Setup failed\n", name, dev);
5447 GOTO(out_mnt, rc = -EROFS);
5451 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5452 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5453 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5454 GOTO(out_mnt, rc = -EINVAL);
5457 inode = osd_sb(o)->s_root->d_inode;
5458 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5459 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5460 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5462 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5466 if (lmd_flags & LMD_FLG_NOSCRUB)
5477 OBD_PAGE_FREE(__page);
5479 fsfilt_put_ops(o->od_fsops);
5484 static struct lu_device *osd_device_fini(const struct lu_env *env,
5485 struct lu_device *d)
5487 struct osd_device *o = osd_dev(d);
5491 osd_shutdown(env, o);
5492 osd_scrub_cleanup(env, o);
5493 osd_obj_map_fini(o);
5499 static int osd_device_init0(const struct lu_env *env,
5500 struct osd_device *o,
5501 struct lustre_cfg *cfg)
5503 struct lu_device *l = osd2lu_dev(o);
5504 struct osd_thread_info *info;
5508 /* if the module was re-loaded, env can loose its keys */
5509 rc = lu_env_refill((struct lu_env *) env);
5512 info = osd_oti_get(env);
5515 l->ld_ops = &osd_lu_ops;
5516 o->od_dt_dev.dd_ops = &osd_dt_ops;
5518 spin_lock_init(&o->od_osfs_lock);
5519 mutex_init(&o->od_otable_mutex);
5520 o->od_osfs_age = cfs_time_shift_64(-1000);
5522 o->od_capa_hash = init_capa_hash();
5523 if (o->od_capa_hash == NULL)
5524 GOTO(out, rc = -ENOMEM);
5526 o->od_read_cache = 1;
5527 o->od_writethrough_cache = 1;
5528 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5530 rc = osd_mount(env, o, cfg);
5534 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5535 sizeof(o->od_svname));
5536 if (cplen >= sizeof(o->od_svname)) {
5541 rc = osd_obj_map_init(env, o);
5545 rc = lu_site_init(&o->od_site, l);
5547 GOTO(out_compat, rc);
5548 o->od_site.ls_bottom_dev = l;
5550 rc = lu_site_init_finish(&o->od_site);
5554 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5555 /* setup scrub, including OI files initialization */
5556 rc = osd_scrub_setup(env, o);
5560 rc = osd_procfs_init(o, o->od_svname);
5562 CERROR("%s: can't initialize procfs: rc = %d\n",
5564 GOTO(out_scrub, rc);
5567 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5569 /* initialize quota slave instance */
5570 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5572 if (IS_ERR(o->od_quota_slave)) {
5573 rc = PTR_ERR(o->od_quota_slave);
5574 o->od_quota_slave = NULL;
5575 GOTO(out_procfs, rc);
5583 osd_scrub_cleanup(env, o);
5585 lu_site_fini(&o->od_site);
5587 osd_obj_map_fini(o);
5591 cleanup_capa_hash(o->od_capa_hash);
5596 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5597 struct lu_device_type *t,
5598 struct lustre_cfg *cfg)
5600 struct osd_device *o;
5605 return ERR_PTR(-ENOMEM);
5607 rc = dt_device_init(&o->od_dt_dev, t);
5609 /* Because the ctx might be revived in dt_device_init,
5610 * refill the env here */
5611 lu_env_refill((struct lu_env *)env);
5612 rc = osd_device_init0(env, o, cfg);
5614 dt_device_fini(&o->od_dt_dev);
5617 if (unlikely(rc != 0))
5620 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5623 static struct lu_device *osd_device_free(const struct lu_env *env,
5624 struct lu_device *d)
5626 struct osd_device *o = osd_dev(d);
5629 cleanup_capa_hash(o->od_capa_hash);
5630 /* XXX: make osd top device in order to release reference */
5631 d->ld_site->ls_top_dev = d;
5632 lu_site_purge(env, d->ld_site, -1);
5633 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5634 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5635 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5637 lu_site_fini(&o->od_site);
5638 dt_device_fini(&o->od_dt_dev);
5643 static int osd_process_config(const struct lu_env *env,
5644 struct lu_device *d, struct lustre_cfg *cfg)
5646 struct osd_device *o = osd_dev(d);
5650 switch(cfg->lcfg_command) {
5652 err = osd_mount(env, o, cfg);
5655 lu_dev_del_linkage(d->ld_site, d);
5656 err = osd_shutdown(env, o);
5665 static int osd_recovery_complete(const struct lu_env *env,
5666 struct lu_device *d)
5668 struct osd_device *osd = osd_dev(d);
5672 if (osd->od_quota_slave == NULL)
5675 /* start qsd instance on recovery completion, this notifies the quota
5676 * slave code that we are about to process new requests now */
5677 rc = qsd_start(env, osd->od_quota_slave);
5682 * we use exports to track all osd users
5684 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5685 struct obd_device *obd, struct obd_uuid *cluuid,
5686 struct obd_connect_data *data, void *localdata)
5688 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5689 struct lustre_handle conn;
5693 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5695 rc = class_connect(&conn, obd, cluuid);
5699 *exp = class_conn2export(&conn);
5701 spin_lock(&osd->od_osfs_lock);
5703 spin_unlock(&osd->od_osfs_lock);
5709 * once last export (we don't count self-export) disappeared
5710 * osd can be released
5712 static int osd_obd_disconnect(struct obd_export *exp)
5714 struct obd_device *obd = exp->exp_obd;
5715 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5716 int rc, release = 0;
5719 /* Only disconnect the underlying layers on the final disconnect. */
5720 spin_lock(&osd->od_osfs_lock);
5722 if (osd->od_connects == 0)
5724 spin_unlock(&osd->od_osfs_lock);
5726 rc = class_disconnect(exp); /* bz 9811 */
5728 if (rc == 0 && release)
5729 class_manual_cleanup(obd);
5733 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5734 struct lu_device *dev)
5736 struct osd_device *osd = osd_dev(dev);
5740 if (osd->od_quota_slave != NULL)
5741 /* set up quota slave objects */
5742 result = qsd_prepare(env, osd->od_quota_slave);
5747 static const struct lu_object_operations osd_lu_obj_ops = {
5748 .loo_object_init = osd_object_init,
5749 .loo_object_delete = osd_object_delete,
5750 .loo_object_release = osd_object_release,
5751 .loo_object_free = osd_object_free,
5752 .loo_object_print = osd_object_print,
5753 .loo_object_invariant = osd_object_invariant
5756 const struct lu_device_operations osd_lu_ops = {
5757 .ldo_object_alloc = osd_object_alloc,
5758 .ldo_process_config = osd_process_config,
5759 .ldo_recovery_complete = osd_recovery_complete,
5760 .ldo_prepare = osd_prepare,
5763 static const struct lu_device_type_operations osd_device_type_ops = {
5764 .ldto_init = osd_type_init,
5765 .ldto_fini = osd_type_fini,
5767 .ldto_start = osd_type_start,
5768 .ldto_stop = osd_type_stop,
5770 .ldto_device_alloc = osd_device_alloc,
5771 .ldto_device_free = osd_device_free,
5773 .ldto_device_init = osd_device_init,
5774 .ldto_device_fini = osd_device_fini
5777 struct lu_device_type osd_device_type = {
5778 .ldt_tags = LU_DEVICE_DT,
5779 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5780 .ldt_ops = &osd_device_type_ops,
5781 .ldt_ctx_tags = LCT_LOCAL,
5785 * lprocfs legacy support.
5787 static struct obd_ops osd_obd_device_ops = {
5788 .o_owner = THIS_MODULE,
5789 .o_connect = osd_obd_connect,
5790 .o_disconnect = osd_obd_disconnect
5793 static int __init osd_mod_init(void)
5795 struct lprocfs_static_vars lvars;
5798 lprocfs_osd_init_vars(&lvars);
5799 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5800 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5803 static void __exit osd_mod_exit(void)
5805 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5808 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5809 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5810 MODULE_LICENSE("GPL");
5812 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);