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) {
321 fid = &lma->lma_self_fid;
322 rc = osd_get_idif(info, inode, dentry, fid);
327 if (unlikely(rc == -ENODATA))
335 lustre_lma_swab(lma);
336 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
337 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
338 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
339 "fid = "DFID", ino = %lu\n",
340 osd_obj2dev(obj)->od_svname,
341 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
342 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
345 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
346 fid = &lma->lma_self_fid;
351 unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
352 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
353 osd_obj2dev(obj)->od_svname,
354 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
355 PFID(&lma->lma_self_fid));
362 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
363 const struct lu_fid *fid,
364 const struct lu_object_conf *conf)
366 struct osd_thread_info *info;
367 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
368 struct osd_device *dev;
369 struct osd_idmap_cache *oic;
370 struct osd_inode_id *id;
372 struct osd_scrub *scrub;
373 struct scrub_file *sf;
377 bool triggered = false;
380 LINVRNT(osd_invariant(obj));
381 LASSERT(obj->oo_inode == NULL);
382 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
385 scrub = &dev->od_scrub;
386 sf = &scrub->os_file;
387 info = osd_oti_get(env);
389 oic = &info->oti_cache;
391 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
394 /* Search order: 1. per-thread cache. */
395 if (lu_fid_eq(fid, &oic->oic_fid)) {
401 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
402 /* Search order: 2. OI scrub pending list. */
403 result = osd_oii_lookup(dev, fid, id);
409 * Objects are created as locking anchors or place holders for objects
410 * yet to be created. No need to osd_oi_lookup() at here because FID
411 * shouldn't never be re-used, if it's really a duplicate FID from
412 * unexpected reason, we should be able to detect it later by calling
413 * do_create->osd_oi_insert()
415 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
416 GOTO(out, result = 0);
418 /* Search order: 3. OI files. */
419 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
420 if (result == -ENOENT) {
421 if (!fid_is_norm(fid) ||
422 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
423 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
425 GOTO(out, result = 0);
436 inode = osd_iget(info, dev, id);
438 result = PTR_ERR(inode);
439 if (result == -ENOENT || result == -ESTALE) {
441 fid_zero(&oic->oic_fid);
442 GOTO(out, result = 0);
445 /* XXX: There are three possible cases:
446 * 1. Backup/restore caused the OI invalid.
447 * 2. Someone unlinked the object but NOT removed
448 * the OI mapping, such as mount target device
449 * as ldiskfs, and modify something directly.
450 * 3. Someone just removed the object between the
451 * former oi_lookup and the iget. It is normal.
453 * It is diffcult to distinguish the 2nd from the
454 * 1st case. Relatively speaking, the 1st case is
455 * common than the 2nd case, trigger OI scrub. */
456 result = osd_oi_lookup(info, dev, fid, id, true);
458 /* It is the case 1 or 2. */
461 if (result == -ENOENT)
462 /* It is the case 3. */
464 } else if (result == -EREMCHG) {
467 if (unlikely(triggered))
468 GOTO(out, result = saved);
471 if (thread_is_running(&scrub->os_thread)) {
472 result = -EINPROGRESS;
473 } else if (!dev->od_noscrub) {
474 result = osd_scrub_start(dev);
475 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
476 "for "DFID", rc = %d [1]\n",
477 LDISKFS_SB(osd_sb(dev))->s_es->\
478 s_volume_name,PFID(fid), result);
479 if (result == 0 || result == -EALREADY)
480 result = -EINPROGRESS;
485 /* We still have chance to get the valid inode: for the
486 * object which is referenced by remote name entry, the
487 * object on the local MDT will be linked under the dir
488 * of "/REMOTE_PARENT_DIR" with its FID string as name.
490 * We do not know whether the object for the given FID
491 * is referenced by some remote name entry or not, and
492 * especially for DNE II, a multiple-linked object may
493 * have many name entries reside on many MDTs.
495 * To simplify the operation, OSD will not distinguish
496 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
497 * only happened for the RPC from other MDT during the
498 * OI scrub, or for the client side RPC with FID only,
499 * such as FID to path, or from old connected client. */
501 result = osd_lookup_in_remote_parent(info, dev,
514 obj->oo_inode = inode;
515 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
517 result = osd_check_lma(env, obj);
520 obj->oo_inode = NULL;
521 if (result == -EREMCHG)
527 obj->oo_compat_dot_created = 1;
528 obj->oo_compat_dotdot_created = 1;
530 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
531 GOTO(out, result = 0);
533 LASSERT(obj->oo_hl_head == NULL);
534 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
535 if (obj->oo_hl_head == NULL) {
536 obj->oo_inode = NULL;
538 GOTO(out, result = -ENOMEM);
540 GOTO(out, result = 0);
543 LINVRNT(osd_invariant(obj));
548 * Concurrency: shouldn't matter.
550 static void osd_object_init0(struct osd_object *obj)
552 LASSERT(obj->oo_inode != NULL);
553 obj->oo_dt.do_body_ops = &osd_body_ops;
554 obj->oo_dt.do_lu.lo_header->loh_attr |=
555 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
559 * Concurrency: no concurrent access is possible that early in object
562 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
563 const struct lu_object_conf *conf)
565 struct osd_object *obj = osd_obj(l);
568 LINVRNT(osd_invariant(obj));
570 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
571 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
572 l->lo_header->loh_attr |= LOHA_EXISTS;
576 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
577 obj->oo_dt.do_body_ops = &osd_body_ops_new;
578 if (result == 0 && obj->oo_inode != NULL)
579 osd_object_init0(obj);
581 LINVRNT(osd_invariant(obj));
586 * Concurrency: no concurrent access is possible that late in object
589 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
591 struct osd_object *obj = osd_obj(l);
593 LINVRNT(osd_invariant(obj));
595 dt_object_fini(&obj->oo_dt);
596 if (obj->oo_hl_head != NULL)
597 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
602 * Concurrency: no concurrent access is possible that late in object
605 static void osd_index_fini(struct osd_object *o)
607 struct iam_container *bag;
609 if (o->oo_dir != NULL) {
610 bag = &o->oo_dir->od_container;
611 if (o->oo_inode != NULL) {
612 if (bag->ic_object == o->oo_inode)
613 iam_container_fini(bag);
615 OBD_FREE_PTR(o->oo_dir);
621 * Concurrency: no concurrent access is possible that late in object
622 * life-cycle (for all existing callers, that is. New callers have to provide
623 * their own locking.)
625 static int osd_inode_unlinked(const struct inode *inode)
627 return inode->i_nlink == 0;
631 OSD_TXN_OI_DELETE_CREDITS = 20,
632 OSD_TXN_INODE_DELETE_CREDITS = 20
639 #if OSD_THANDLE_STATS
641 * Set time when the handle is allocated
643 static void osd_th_alloced(struct osd_thandle *oth)
645 oth->oth_alloced = cfs_time_current();
649 * Set time when the handle started
651 static void osd_th_started(struct osd_thandle *oth)
653 oth->oth_started = cfs_time_current();
657 * Helper function to convert time interval to microseconds packed in
660 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
664 cfs_duration_usec(cfs_time_sub(end, start), &val);
665 return val.tv_sec * 1000000 + val.tv_usec;
669 * Check whether the we deal with this handle for too long.
671 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
672 cfs_time_t alloced, cfs_time_t started,
675 cfs_time_t now = cfs_time_current();
677 LASSERT(dev != NULL);
679 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
680 interval_to_usec(alloced, started));
681 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
682 interval_to_usec(started, closed));
683 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
684 interval_to_usec(closed, now));
686 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
687 CWARN("transaction handle %p was open for too long: "
689 "alloced "CFS_TIME_T" ,"
690 "started "CFS_TIME_T" ,"
691 "closed "CFS_TIME_T"\n",
692 oth, now, alloced, started, closed);
693 libcfs_debug_dumpstack(NULL);
697 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
699 cfs_time_t __closed = cfs_time_current(); \
700 cfs_time_t __alloced = oth->oth_alloced; \
701 cfs_time_t __started = oth->oth_started; \
704 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
707 #else /* OSD_THANDLE_STATS */
709 #define osd_th_alloced(h) do {} while(0)
710 #define osd_th_started(h) do {} while(0)
711 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
713 #endif /* OSD_THANDLE_STATS */
716 * Concurrency: doesn't access mutable data.
718 static int osd_param_is_not_sane(const struct osd_device *dev,
719 const struct thandle *th)
721 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
723 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
727 * Concurrency: shouldn't matter.
729 static void osd_trans_commit_cb(struct super_block *sb,
730 struct ldiskfs_journal_cb_entry *jcb, int error)
732 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
733 struct thandle *th = &oh->ot_super;
734 struct lu_device *lud = &th->th_dev->dd_lu_dev;
735 struct dt_txn_commit_cb *dcb, *tmp;
737 LASSERT(oh->ot_handle == NULL);
740 CERROR("transaction @0x%p commit error: %d\n", th, error);
742 dt_txn_hook_commit(th);
744 /* call per-transaction callbacks if any */
745 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
746 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
747 "commit callback entry: magic=%x name='%s'\n",
748 dcb->dcb_magic, dcb->dcb_name);
749 cfs_list_del_init(&dcb->dcb_linkage);
750 dcb->dcb_func(NULL, th, dcb, error);
753 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
757 lu_context_exit(&th->th_ctx);
758 lu_context_fini(&th->th_ctx);
762 static struct thandle *osd_trans_create(const struct lu_env *env,
765 struct osd_thread_info *oti = osd_oti_get(env);
766 struct osd_iobuf *iobuf = &oti->oti_iobuf;
767 struct osd_thandle *oh;
771 /* on pending IO in this thread should left from prev. request */
772 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
774 th = ERR_PTR(-ENOMEM);
775 OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
777 oh->ot_quota_trans = &oti->oti_quota_trans;
778 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
782 th->th_tags = LCT_TX_HANDLE;
784 oti->oti_dev = osd_dt_dev(d);
785 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
788 memset(oti->oti_declare_ops, 0,
789 sizeof(oti->oti_declare_ops));
790 memset(oti->oti_declare_ops_rb, 0,
791 sizeof(oti->oti_declare_ops_rb));
792 memset(oti->oti_declare_ops_cred, 0,
793 sizeof(oti->oti_declare_ops_cred));
794 oti->oti_rollback = false;
800 * Concurrency: shouldn't matter.
802 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
805 struct osd_thread_info *oti = osd_oti_get(env);
806 struct osd_device *dev = osd_dt_dev(d);
808 struct osd_thandle *oh;
813 LASSERT(current->journal_info == NULL);
815 oh = container_of0(th, struct osd_thandle, ot_super);
817 LASSERT(oh->ot_handle == NULL);
819 rc = dt_txn_hook_start(env, d, th);
823 if (unlikely(osd_param_is_not_sane(dev, th))) {
824 static unsigned long last_printed;
825 static int last_credits;
827 CWARN("%.16s: too many transaction credits (%d > %d)\n",
828 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
830 osd_journal(dev)->j_max_transaction_buffers);
831 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
832 oti->oti_declare_ops[OSD_OT_CREATE],
833 oti->oti_declare_ops_cred[OSD_OT_CREATE],
834 oti->oti_declare_ops[OSD_OT_DELETE],
835 oti->oti_declare_ops_cred[OSD_OT_DELETE],
836 oti->oti_declare_ops[OSD_OT_DESTROY],
837 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
838 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
839 oti->oti_declare_ops[OSD_OT_ATTR_SET],
840 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
841 oti->oti_declare_ops[OSD_OT_XATTR_SET],
842 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
843 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
844 oti->oti_declare_ops[OSD_OT_WRITE],
845 oti->oti_declare_ops_cred[OSD_OT_WRITE],
846 oti->oti_declare_ops[OSD_OT_PUNCH],
847 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
848 oti->oti_declare_ops[OSD_OT_QUOTA],
849 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
850 CWARN(" insert: %u/%u, delete: %u/%u\n",
851 oti->oti_declare_ops[OSD_OT_INSERT],
852 oti->oti_declare_ops_cred[OSD_OT_INSERT],
853 oti->oti_declare_ops[OSD_OT_DESTROY],
854 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
855 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
856 oti->oti_declare_ops[OSD_OT_REF_ADD],
857 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
858 oti->oti_declare_ops[OSD_OT_REF_DEL],
859 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
861 if (last_credits != oh->ot_credits &&
862 time_after(jiffies, last_printed + 60 * HZ)) {
863 libcfs_debug_dumpstack(NULL);
864 last_credits = oh->ot_credits;
865 last_printed = jiffies;
867 /* XXX Limit the credits to 'max_transaction_buffers', and
868 * let the underlying filesystem to catch the error if
869 * we really need so many credits.
871 * This should be removed when we can calculate the
872 * credits precisely. */
873 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
877 * XXX temporary stuff. Some abstraction layer should
880 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
884 LASSERT(oti->oti_txns == 0);
885 lu_context_init(&th->th_ctx, th->th_tags);
886 lu_context_enter(&th->th_ctx);
888 lu_device_get(&d->dd_lu_dev);
889 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
901 * Concurrency: shouldn't matter.
903 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
906 struct osd_thandle *oh;
907 struct osd_thread_info *oti = osd_oti_get(env);
908 struct osd_iobuf *iobuf = &oti->oti_iobuf;
909 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
912 oh = container_of0(th, struct osd_thandle, ot_super);
915 /* inform the quota slave device that the transaction is
917 qsd_op_end(env, qsd, oh->ot_quota_trans);
918 oh->ot_quota_trans = NULL;
920 if (oh->ot_handle != NULL) {
921 handle_t *hdl = oh->ot_handle;
924 * add commit callback
925 * notice we don't do this in osd_trans_start()
926 * as underlying transaction can change during truncate
928 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
931 LASSERT(oti->oti_txns == 1);
933 rc = dt_txn_hook_stop(env, th);
935 CERROR("Failure in transaction hook: %d\n", rc);
937 /* hook functions might modify th_sync */
938 hdl->h_sync = th->th_sync;
940 oh->ot_handle = NULL;
941 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
942 rc = ldiskfs_journal_stop(hdl));
944 CERROR("Failure to stop transaction: %d\n", rc);
949 /* as we want IO to journal and data IO be concurrent, we don't block
950 * awaiting data IO completion in osd_do_bio(), instead we wait here
951 * once transaction is submitted to the journal. all reqular requests
952 * don't do direct IO (except read/write), thus this wait_event becomes
955 * IMPORTANT: we have to wait till any IO submited by the thread is
956 * completed otherwise iobuf may be corrupted by different request
958 cfs_wait_event(iobuf->dr_wait,
959 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
961 rc = iobuf->dr_error;
966 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
968 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
971 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
972 LASSERT(&dcb->dcb_func != NULL);
973 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
979 * Called just before object is freed. Releases all resources except for
980 * object itself (that is released by osd_object_free()).
982 * Concurrency: no concurrent access is possible that late in object
985 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
987 struct osd_object *obj = osd_obj(l);
988 struct inode *inode = obj->oo_inode;
990 LINVRNT(osd_invariant(obj));
993 * If object is unlinked remove fid->ino mapping from object index.
998 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
999 qid_t uid = inode->i_uid;
1000 qid_t gid = inode->i_gid;
1003 obj->oo_inode = NULL;
1006 struct osd_thread_info *info = osd_oti_get(env);
1007 struct lquota_id_info *qi = &info->oti_qi;
1009 /* Release granted quota to master if necessary */
1010 qi->lqi_id.qid_uid = uid;
1011 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1013 qi->lqi_id.qid_uid = gid;
1014 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1020 * Concurrency: ->loo_object_release() is called under site spin-lock.
1022 static void osd_object_release(const struct lu_env *env,
1023 struct lu_object *l)
1028 * Concurrency: shouldn't matter.
1030 static int osd_object_print(const struct lu_env *env, void *cookie,
1031 lu_printer_t p, const struct lu_object *l)
1033 struct osd_object *o = osd_obj(l);
1034 struct iam_descr *d;
1036 if (o->oo_dir != NULL)
1037 d = o->oo_dir->od_container.ic_descr;
1040 return (*p)(env, cookie,
1041 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1043 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1044 o->oo_inode ? o->oo_inode->i_generation : 0,
1045 d ? d->id_ops->id_name : "plain");
1049 * Concurrency: shouldn't matter.
1051 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1052 struct obd_statfs *sfs)
1054 struct osd_device *osd = osd_dt_dev(d);
1055 struct super_block *sb = osd_sb(osd);
1056 struct kstatfs *ksfs;
1059 if (unlikely(osd->od_mnt == NULL))
1060 return -EINPROGRESS;
1062 /* osd_lproc.c call this without env, allocate ksfs for that case */
1063 if (unlikely(env == NULL)) {
1064 OBD_ALLOC_PTR(ksfs);
1068 ksfs = &osd_oti_get(env)->oti_ksfs;
1071 spin_lock(&osd->od_osfs_lock);
1072 /* cache 1 second */
1073 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1074 result = sb->s_op->statfs(sb->s_root, ksfs);
1075 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1076 osd->od_osfs_age = cfs_time_current_64();
1077 statfs_pack(&osd->od_statfs, ksfs);
1078 if (sb->s_flags & MS_RDONLY)
1079 sfs->os_state = OS_STATE_READONLY;
1083 if (likely(result == 0))
1084 *sfs = osd->od_statfs;
1085 spin_unlock(&osd->od_osfs_lock);
1087 if (unlikely(env == NULL))
1094 * Estimate space needed for file creations. We assume the largest filename
1095 * which is 2^64 - 1, hence a filename of 20 chars.
1096 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1098 #ifdef __LDISKFS_DIR_REC_LEN
1099 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1101 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1105 * Concurrency: doesn't access mutable data.
1107 static void osd_conf_get(const struct lu_env *env,
1108 const struct dt_device *dev,
1109 struct dt_device_param *param)
1111 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1114 * XXX should be taken from not-yet-existing fs abstraction layer.
1116 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1117 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1118 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1119 param->ddp_block_shift = sb->s_blocksize_bits;
1120 param->ddp_mount_type = LDD_MT_LDISKFS;
1121 param->ddp_maxbytes = sb->s_maxbytes;
1122 /* Overhead estimate should be fairly accurate, so we really take a tiny
1123 * error margin which also avoids fragmenting the filesystem too much */
1124 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1125 /* inode are statically allocated, so per-inode space consumption
1126 * is the space consumed by the directory entry */
1127 param->ddp_inodespace = PER_OBJ_USAGE;
1128 /* per-fragment overhead to be used by the client code */
1129 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1130 param->ddp_mntopts = 0;
1131 if (test_opt(sb, XATTR_USER))
1132 param->ddp_mntopts |= MNTOPT_USERXATTR;
1133 if (test_opt(sb, POSIX_ACL))
1134 param->ddp_mntopts |= MNTOPT_ACL;
1136 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1137 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1138 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1141 param->ddp_max_ea_size = sb->s_blocksize;
1146 * Concurrency: shouldn't matter.
1148 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1150 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1151 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1155 * Start commit for OSD device.
1157 * An implementation of dt_commit_async method for OSD device.
1158 * Asychronously starts underlayng fs sync and thereby a transaction
1161 * \param env environment
1162 * \param d dt device
1164 * \see dt_device_operations
1166 static int osd_commit_async(const struct lu_env *env,
1167 struct dt_device *d)
1169 struct super_block *s = osd_sb(osd_dt_dev(d));
1172 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1173 RETURN(s->s_op->sync_fs(s, 0));
1177 * Concurrency: shouldn't matter.
1180 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1182 struct super_block *sb = osd_sb(osd_dt_dev(d));
1183 struct block_device *dev = sb->s_bdev;
1184 #ifdef HAVE_DEV_SET_RDONLY
1185 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1188 int rc = -EOPNOTSUPP;
1192 #ifdef HAVE_DEV_SET_RDONLY
1193 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1195 if (jdev && (jdev != dev)) {
1196 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1198 dev_set_rdonly(jdev);
1200 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1201 dev_set_rdonly(dev);
1203 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1204 osd_dt_dev(d)->od_svname, (long)dev, rc);
1210 * Concurrency: serialization provided by callers.
1212 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1213 int mode, unsigned long timeout, __u32 alg,
1214 struct lustre_capa_key *keys)
1216 struct osd_device *dev = osd_dt_dev(d);
1219 dev->od_fl_capa = mode;
1220 dev->od_capa_timeout = timeout;
1221 dev->od_capa_alg = alg;
1222 dev->od_capa_keys = keys;
1227 * Note: we do not count into QUOTA here.
1228 * If we mount with --data_journal we may need more.
1230 const int osd_dto_credits_noquota[DTO_NR] = {
1233 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1234 * SINGLEDATA_TRANS_BLOCKS(8)
1235 * XXX Note: maybe iam need more, since iam have more level than
1238 [DTO_INDEX_INSERT] = 16,
1239 [DTO_INDEX_DELETE] = 16,
1243 [DTO_INDEX_UPDATE] = 16,
1245 * Create a object. The same as create object in EXT3.
1246 * DATA_TRANS_BLOCKS(14) +
1247 * INDEX_EXTRA_BLOCKS(8) +
1248 * 3(inode bits, groups, GDT)
1250 [DTO_OBJECT_CREATE] = 25,
1252 * XXX: real credits to be fixed
1254 [DTO_OBJECT_DELETE] = 25,
1256 * Attr set credits (inode)
1258 [DTO_ATTR_SET_BASE] = 1,
1260 * Xattr set. The same as xattr of EXT3.
1261 * DATA_TRANS_BLOCKS(14)
1262 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1263 * are also counted in. Do not know why?
1265 [DTO_XATTR_SET] = 14,
1268 * credits for inode change during write.
1270 [DTO_WRITE_BASE] = 3,
1272 * credits for single block write.
1274 [DTO_WRITE_BLOCK] = 14,
1276 * Attr set credits for chown.
1277 * This is extra credits for setattr, and it is null without quota
1279 [DTO_ATTR_SET_CHOWN]= 0
1282 static const struct dt_device_operations osd_dt_ops = {
1283 .dt_root_get = osd_root_get,
1284 .dt_statfs = osd_statfs,
1285 .dt_trans_create = osd_trans_create,
1286 .dt_trans_start = osd_trans_start,
1287 .dt_trans_stop = osd_trans_stop,
1288 .dt_trans_cb_add = osd_trans_cb_add,
1289 .dt_conf_get = osd_conf_get,
1290 .dt_sync = osd_sync,
1292 .dt_commit_async = osd_commit_async,
1293 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1296 static void osd_object_read_lock(const struct lu_env *env,
1297 struct dt_object *dt, unsigned role)
1299 struct osd_object *obj = osd_dt_obj(dt);
1300 struct osd_thread_info *oti = osd_oti_get(env);
1302 LINVRNT(osd_invariant(obj));
1304 LASSERT(obj->oo_owner != env);
1305 down_read_nested(&obj->oo_sem, role);
1307 LASSERT(obj->oo_owner == NULL);
1311 static void osd_object_write_lock(const struct lu_env *env,
1312 struct dt_object *dt, unsigned role)
1314 struct osd_object *obj = osd_dt_obj(dt);
1315 struct osd_thread_info *oti = osd_oti_get(env);
1317 LINVRNT(osd_invariant(obj));
1319 LASSERT(obj->oo_owner != env);
1320 down_write_nested(&obj->oo_sem, role);
1322 LASSERT(obj->oo_owner == NULL);
1323 obj->oo_owner = env;
1327 static void osd_object_read_unlock(const struct lu_env *env,
1328 struct dt_object *dt)
1330 struct osd_object *obj = osd_dt_obj(dt);
1331 struct osd_thread_info *oti = osd_oti_get(env);
1333 LINVRNT(osd_invariant(obj));
1335 LASSERT(oti->oti_r_locks > 0);
1337 up_read(&obj->oo_sem);
1340 static void osd_object_write_unlock(const struct lu_env *env,
1341 struct dt_object *dt)
1343 struct osd_object *obj = osd_dt_obj(dt);
1344 struct osd_thread_info *oti = osd_oti_get(env);
1346 LINVRNT(osd_invariant(obj));
1348 LASSERT(obj->oo_owner == env);
1349 LASSERT(oti->oti_w_locks > 0);
1351 obj->oo_owner = NULL;
1352 up_write(&obj->oo_sem);
1355 static int osd_object_write_locked(const struct lu_env *env,
1356 struct dt_object *dt)
1358 struct osd_object *obj = osd_dt_obj(dt);
1360 LINVRNT(osd_invariant(obj));
1362 return obj->oo_owner == env;
1365 static int capa_is_sane(const struct lu_env *env,
1366 struct osd_device *dev,
1367 struct lustre_capa *capa,
1368 struct lustre_capa_key *keys)
1370 struct osd_thread_info *oti = osd_oti_get(env);
1371 struct lustre_capa *tcapa = &oti->oti_capa;
1372 struct obd_capa *oc;
1376 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1378 if (capa_is_expired(oc)) {
1379 DEBUG_CAPA(D_ERROR, capa, "expired");
1386 if (capa_is_expired_sec(capa)) {
1387 DEBUG_CAPA(D_ERROR, capa, "expired");
1391 spin_lock(&capa_lock);
1392 for (i = 0; i < 2; i++) {
1393 if (keys[i].lk_keyid == capa->lc_keyid) {
1394 oti->oti_capa_key = keys[i];
1398 spin_unlock(&capa_lock);
1401 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1405 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1409 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1410 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1414 oc = capa_add(dev->od_capa_hash, capa);
1420 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1421 struct lustre_capa *capa, __u64 opc)
1423 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1424 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1425 struct md_capainfo *ci;
1428 if (!dev->od_fl_capa)
1431 if (capa == BYPASS_CAPA)
1434 ci = md_capainfo(env);
1438 if (ci->mc_auth == LC_ID_NONE)
1442 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1446 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1447 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1452 if (!capa_opc_supported(capa, opc)) {
1453 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1457 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1458 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1465 static struct timespec *osd_inode_time(const struct lu_env *env,
1466 struct inode *inode, __u64 seconds)
1468 struct osd_thread_info *oti = osd_oti_get(env);
1469 struct timespec *t = &oti->oti_time;
1471 t->tv_sec = seconds;
1473 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1478 static void osd_inode_getattr(const struct lu_env *env,
1479 struct inode *inode, struct lu_attr *attr)
1481 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1482 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1483 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1486 attr->la_atime = LTIME_S(inode->i_atime);
1487 attr->la_mtime = LTIME_S(inode->i_mtime);
1488 attr->la_ctime = LTIME_S(inode->i_ctime);
1489 attr->la_mode = inode->i_mode;
1490 attr->la_size = i_size_read(inode);
1491 attr->la_blocks = inode->i_blocks;
1492 attr->la_uid = inode->i_uid;
1493 attr->la_gid = inode->i_gid;
1494 attr->la_flags = LDISKFS_I(inode)->i_flags;
1495 attr->la_nlink = inode->i_nlink;
1496 attr->la_rdev = inode->i_rdev;
1497 attr->la_blksize = 1 << inode->i_blkbits;
1498 attr->la_blkbits = inode->i_blkbits;
1501 static int osd_attr_get(const struct lu_env *env,
1502 struct dt_object *dt,
1503 struct lu_attr *attr,
1504 struct lustre_capa *capa)
1506 struct osd_object *obj = osd_dt_obj(dt);
1508 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1509 LINVRNT(osd_invariant(obj));
1511 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1514 spin_lock(&obj->oo_guard);
1515 osd_inode_getattr(env, obj->oo_inode, attr);
1516 spin_unlock(&obj->oo_guard);
1520 static int osd_declare_attr_set(const struct lu_env *env,
1521 struct dt_object *dt,
1522 const struct lu_attr *attr,
1523 struct thandle *handle)
1525 struct osd_thandle *oh;
1526 struct osd_object *obj;
1527 struct osd_thread_info *info = osd_oti_get(env);
1528 struct lquota_id_info *qi = &info->oti_qi;
1534 LASSERT(dt != NULL);
1535 LASSERT(handle != NULL);
1537 obj = osd_dt_obj(dt);
1538 LASSERT(osd_invariant(obj));
1540 oh = container_of0(handle, struct osd_thandle, ot_super);
1541 LASSERT(oh->ot_handle == NULL);
1543 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1544 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1546 if (attr == NULL || obj->oo_inode == NULL)
1549 bspace = obj->oo_inode->i_blocks;
1550 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1551 bspace = toqb(bspace);
1553 /* Changing ownership is always preformed by super user, it should not
1556 * We still need to call the osd_declare_qid() to calculate the journal
1557 * credits for updating quota accounting files and to trigger quota
1558 * space adjustment once the operation is completed.*/
1559 if ((attr->la_valid & LA_UID) != 0 &&
1560 attr->la_uid != obj->oo_inode->i_uid) {
1561 qi->lqi_type = USRQUOTA;
1563 /* inode accounting */
1564 qi->lqi_is_blk = false;
1566 /* one more inode for the new owner ... */
1567 qi->lqi_id.qid_uid = attr->la_uid;
1569 allocated = (attr->la_uid == 0) ? true : false;
1570 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1571 if (rc == -EDQUOT || rc == -EINPROGRESS)
1576 /* and one less inode for the current uid */
1577 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1579 rc = osd_declare_qid(env, oh, qi, true, NULL);
1580 if (rc == -EDQUOT || rc == -EINPROGRESS)
1585 /* block accounting */
1586 qi->lqi_is_blk = true;
1588 /* more blocks for the new owner ... */
1589 qi->lqi_id.qid_uid = attr->la_uid;
1590 qi->lqi_space = bspace;
1591 allocated = (attr->la_uid == 0) ? true : false;
1592 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1593 if (rc == -EDQUOT || rc == -EINPROGRESS)
1598 /* and finally less blocks for the current owner */
1599 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1600 qi->lqi_space = -bspace;
1601 rc = osd_declare_qid(env, oh, qi, true, NULL);
1602 if (rc == -EDQUOT || rc == -EINPROGRESS)
1608 if (attr->la_valid & LA_GID &&
1609 attr->la_gid != obj->oo_inode->i_gid) {
1610 qi->lqi_type = GRPQUOTA;
1612 /* inode accounting */
1613 qi->lqi_is_blk = false;
1615 /* one more inode for the new group owner ... */
1616 qi->lqi_id.qid_gid = attr->la_gid;
1618 allocated = (attr->la_gid == 0) ? true : false;
1619 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1620 if (rc == -EDQUOT || rc == -EINPROGRESS)
1625 /* and one less inode for the current gid */
1626 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1628 rc = osd_declare_qid(env, oh, qi, true, NULL);
1629 if (rc == -EDQUOT || rc == -EINPROGRESS)
1634 /* block accounting */
1635 qi->lqi_is_blk = true;
1637 /* more blocks for the new owner ... */
1638 qi->lqi_id.qid_gid = attr->la_gid;
1639 qi->lqi_space = bspace;
1640 allocated = (attr->la_gid == 0) ? true : false;
1641 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1642 if (rc == -EDQUOT || rc == -EINPROGRESS)
1647 /* and finally less blocks for the current owner */
1648 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1649 qi->lqi_space = -bspace;
1650 rc = osd_declare_qid(env, oh, qi, true, NULL);
1651 if (rc == -EDQUOT || rc == -EINPROGRESS)
1660 static int osd_inode_setattr(const struct lu_env *env,
1661 struct inode *inode, const struct lu_attr *attr)
1665 bits = attr->la_valid;
1667 if (bits & LA_ATIME)
1668 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1669 if (bits & LA_CTIME)
1670 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1671 if (bits & LA_MTIME)
1672 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1673 if (bits & LA_SIZE) {
1674 LDISKFS_I(inode)->i_disksize = attr->la_size;
1675 i_size_write(inode, attr->la_size);
1679 /* OSD should not change "i_blocks" which is used by quota.
1680 * "i_blocks" should be changed by ldiskfs only. */
1681 if (bits & LA_BLOCKS)
1682 inode->i_blocks = attr->la_blocks;
1685 inode->i_mode = (inode->i_mode & S_IFMT) |
1686 (attr->la_mode & ~S_IFMT);
1688 inode->i_uid = attr->la_uid;
1690 inode->i_gid = attr->la_gid;
1691 if (bits & LA_NLINK)
1692 set_nlink(inode, attr->la_nlink);
1694 inode->i_rdev = attr->la_rdev;
1696 if (bits & LA_FLAGS) {
1697 /* always keep S_NOCMTIME */
1698 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1704 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1706 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1707 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1712 if (attr->la_valid & LA_UID)
1713 iattr.ia_valid |= ATTR_UID;
1714 if (attr->la_valid & LA_GID)
1715 iattr.ia_valid |= ATTR_GID;
1716 iattr.ia_uid = attr->la_uid;
1717 iattr.ia_gid = attr->la_gid;
1719 rc = ll_vfs_dq_transfer(inode, &iattr);
1721 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1722 "enforcement enabled on the ldiskfs filesystem?",
1723 inode->i_sb->s_id, rc);
1730 static int osd_attr_set(const struct lu_env *env,
1731 struct dt_object *dt,
1732 const struct lu_attr *attr,
1733 struct thandle *handle,
1734 struct lustre_capa *capa)
1736 struct osd_object *obj = osd_dt_obj(dt);
1737 struct inode *inode;
1740 LASSERT(handle != NULL);
1741 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1742 LASSERT(osd_invariant(obj));
1744 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1747 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1749 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1750 struct osd_thread_info *oti = osd_oti_get(env);
1751 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1752 struct lu_fid *fid1 = &oti->oti_fid;
1753 struct osd_inode_id *id = &oti->oti_id;
1754 struct iam_path_descr *ipd;
1755 struct iam_container *bag;
1756 struct osd_thandle *oh;
1759 fid_cpu_to_be(fid1, fid0);
1760 memset(id, 1, sizeof(*id));
1761 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1762 fid0)->oi_dir.od_container;
1763 ipd = osd_idx_ipd_get(env, bag);
1764 if (unlikely(ipd == NULL))
1767 oh = container_of0(handle, struct osd_thandle, ot_super);
1768 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1769 (const struct iam_rec *)id, ipd);
1770 osd_ipd_put(env, bag, ipd);
1771 return(rc > 0 ? 0 : rc);
1774 inode = obj->oo_inode;
1775 ll_vfs_dq_init(inode);
1777 rc = osd_quota_transfer(inode, attr);
1781 spin_lock(&obj->oo_guard);
1782 rc = osd_inode_setattr(env, inode, attr);
1783 spin_unlock(&obj->oo_guard);
1786 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1790 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1791 struct osd_object *obj,
1792 const char *name, const int namelen)
1794 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1797 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1799 struct dt_allocation_hint *hint,
1803 struct osd_device *osd = osd_obj2dev(obj);
1804 struct osd_thandle *oth;
1805 struct dt_object *parent = NULL;
1806 struct inode *inode;
1808 LINVRNT(osd_invariant(obj));
1809 LASSERT(obj->oo_inode == NULL);
1810 LASSERT(obj->oo_hl_head == NULL);
1812 if (S_ISDIR(mode) && ldiskfs_pdo) {
1813 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1814 if (obj->oo_hl_head == NULL)
1818 oth = container_of(th, struct osd_thandle, ot_super);
1819 LASSERT(oth->ot_handle->h_transaction != NULL);
1821 if (hint && hint->dah_parent)
1822 parent = hint->dah_parent;
1824 inode = ldiskfs_create_inode(oth->ot_handle,
1825 parent ? osd_dt_obj(parent)->oo_inode :
1826 osd_sb(osd)->s_root->d_inode,
1828 if (!IS_ERR(inode)) {
1829 /* Do not update file c/mtime in ldiskfs.
1830 * NB: don't need any lock because no contention at this
1832 inode->i_flags |= S_NOCMTIME;
1834 /* For new created object, it must be consistent,
1835 * and it is unnecessary to scrub against it. */
1836 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1837 obj->oo_inode = inode;
1840 if (obj->oo_hl_head != NULL) {
1841 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1842 obj->oo_hl_head = NULL;
1844 result = PTR_ERR(inode);
1846 LINVRNT(osd_invariant(obj));
1854 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1855 struct lu_attr *attr,
1856 struct dt_allocation_hint *hint,
1857 struct dt_object_format *dof,
1861 struct osd_thandle *oth;
1862 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1864 LASSERT(S_ISDIR(attr->la_mode));
1866 oth = container_of(th, struct osd_thandle, ot_super);
1867 LASSERT(oth->ot_handle->h_transaction != NULL);
1868 result = osd_mkfile(info, obj, mode, hint, th);
1873 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1874 struct lu_attr *attr,
1875 struct dt_allocation_hint *hint,
1876 struct dt_object_format *dof,
1880 struct osd_thandle *oth;
1881 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1883 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1885 LASSERT(S_ISREG(attr->la_mode));
1887 oth = container_of(th, struct osd_thandle, ot_super);
1888 LASSERT(oth->ot_handle->h_transaction != NULL);
1890 result = osd_mkfile(info, obj, mode, hint, th);
1892 LASSERT(obj->oo_inode != NULL);
1893 if (feat->dif_flags & DT_IND_VARKEY)
1894 result = iam_lvar_create(obj->oo_inode,
1895 feat->dif_keysize_max,
1897 feat->dif_recsize_max,
1900 result = iam_lfix_create(obj->oo_inode,
1901 feat->dif_keysize_max,
1903 feat->dif_recsize_max,
1910 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1911 struct lu_attr *attr,
1912 struct dt_allocation_hint *hint,
1913 struct dt_object_format *dof,
1916 LASSERT(S_ISREG(attr->la_mode));
1917 return osd_mkfile(info, obj, (attr->la_mode &
1918 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1921 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1922 struct lu_attr *attr,
1923 struct dt_allocation_hint *hint,
1924 struct dt_object_format *dof,
1927 LASSERT(S_ISLNK(attr->la_mode));
1928 return osd_mkfile(info, obj, (attr->la_mode &
1929 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1932 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1933 struct lu_attr *attr,
1934 struct dt_allocation_hint *hint,
1935 struct dt_object_format *dof,
1938 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1941 LINVRNT(osd_invariant(obj));
1942 LASSERT(obj->oo_inode == NULL);
1943 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1944 S_ISFIFO(mode) || S_ISSOCK(mode));
1946 result = osd_mkfile(info, obj, mode, hint, th);
1948 LASSERT(obj->oo_inode != NULL);
1950 * This inode should be marked dirty for i_rdev. Currently
1951 * that is done in the osd_attr_init().
1953 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1956 LINVRNT(osd_invariant(obj));
1960 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1962 struct dt_allocation_hint *hint,
1963 struct dt_object_format *dof,
1966 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1968 osd_obj_type_f result;
1984 result = osd_mk_index;
1995 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1996 struct dt_object *parent, struct dt_object *child,
1997 cfs_umode_t child_mode)
2001 memset(ah, 0, sizeof(*ah));
2002 ah->dah_parent = parent;
2003 ah->dah_mode = child_mode;
2006 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2007 struct lu_attr *attr, struct dt_object_format *dof)
2009 struct inode *inode = obj->oo_inode;
2010 __u64 valid = attr->la_valid;
2013 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2015 if (dof->dof_type != DFT_NODE)
2016 attr->la_valid &= ~LA_RDEV;
2017 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2018 attr->la_valid &= ~LA_ATIME;
2019 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2020 attr->la_valid &= ~LA_CTIME;
2021 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2022 attr->la_valid &= ~LA_MTIME;
2024 result = osd_quota_transfer(inode, attr);
2028 if (attr->la_valid != 0) {
2029 result = osd_inode_setattr(info->oti_env, inode, attr);
2031 * The osd_inode_setattr() should always succeed here. The
2032 * only error that could be returned is EDQUOT when we are
2033 * trying to change the UID or GID of the inode. However, this
2034 * should not happen since quota enforcement is no longer
2035 * enabled on ldiskfs (lquota takes care of it).
2037 LASSERTF(result == 0, "%d", result);
2038 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2041 attr->la_valid = valid;
2045 * Helper function for osd_object_create()
2047 * \retval 0, on success
2049 static int __osd_object_create(struct osd_thread_info *info,
2050 struct osd_object *obj, struct lu_attr *attr,
2051 struct dt_allocation_hint *hint,
2052 struct dt_object_format *dof,
2058 /* we drop umask so that permissions we pass are not affected */
2059 umask = current->fs->umask;
2060 current->fs->umask = 0;
2062 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2065 osd_attr_init(info, obj, attr, dof);
2066 osd_object_init0(obj);
2068 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2069 unlock_new_inode(obj->oo_inode);
2072 /* restore previous umask value */
2073 current->fs->umask = umask;
2079 * Helper function for osd_object_create()
2081 * \retval 0, on success
2083 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2084 const struct lu_fid *fid, struct thandle *th)
2086 struct osd_thread_info *info = osd_oti_get(env);
2087 struct osd_inode_id *id = &info->oti_id;
2088 struct osd_device *osd = osd_obj2dev(obj);
2090 LASSERT(obj->oo_inode != NULL);
2092 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2093 return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2096 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2097 const struct lu_fid *fid, struct lu_seq_range *range)
2099 struct seq_server_site *ss = osd_seq_site(osd);
2102 if (fid_is_idif(fid)) {
2103 fld_range_set_ost(range);
2104 range->lsr_index = fid_idif_ost_idx(fid);
2108 if (!fid_seq_in_fldb(fid_seq(fid))) {
2109 fld_range_set_mdt(range);
2111 /* FIXME: If ss is NULL, it suppose not get lsr_index
2113 range->lsr_index = ss->ss_node_id;
2117 LASSERT(ss != NULL);
2118 fld_range_set_any(range);
2119 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2121 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
2122 osd_name(osd), PFID(fid), rc);
2128 * Concurrency: no external locking is necessary.
2130 static int osd_declare_object_create(const struct lu_env *env,
2131 struct dt_object *dt,
2132 struct lu_attr *attr,
2133 struct dt_allocation_hint *hint,
2134 struct dt_object_format *dof,
2135 struct thandle *handle)
2137 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2138 struct osd_thandle *oh;
2142 LASSERT(handle != NULL);
2144 oh = container_of0(handle, struct osd_thandle, ot_super);
2145 LASSERT(oh->ot_handle == NULL);
2147 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2148 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2149 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2150 lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2151 /* Reuse idle OI block may cause additional one OI block
2153 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2154 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2156 /* If this is directory, then we expect . and .. to be inserted as
2157 * well. The one directory block always needs to be created for the
2158 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2159 * block), there is no danger of needing a tree for the first block.
2161 if (attr && S_ISDIR(attr->la_mode)) {
2162 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2163 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2164 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2170 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2171 false, false, NULL, false);
2175 /* It does fld look up inside declare, and the result will be
2176 * added to fld cache, so the following fld lookup inside insert
2177 * does not need send RPC anymore, so avoid send rpc with holding
2179 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2180 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2181 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2182 lu_object_fid(&dt->do_lu), range);
2188 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2189 struct lu_attr *attr,
2190 struct dt_allocation_hint *hint,
2191 struct dt_object_format *dof,
2194 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2195 struct osd_object *obj = osd_dt_obj(dt);
2196 struct osd_thread_info *info = osd_oti_get(env);
2201 LINVRNT(osd_invariant(obj));
2202 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2203 LASSERT(osd_write_locked(env, obj));
2204 LASSERT(th != NULL);
2206 if (unlikely(fid_is_acct(fid)))
2207 /* Quota files can't be created from the kernel any more,
2208 * 'tune2fs -O quota' will take care of creating them */
2211 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2212 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2214 result = __osd_object_create(info, obj, attr, hint, dof, th);
2216 result = __osd_oi_insert(env, obj, fid, th);
2218 LASSERT(ergo(result == 0,
2219 dt_object_exists(dt) && !dt_object_remote(dt)));
2221 LASSERT(osd_invariant(obj));
2226 * Called to destroy on-disk representation of the object
2228 * Concurrency: must be locked
2230 static int osd_declare_object_destroy(const struct lu_env *env,
2231 struct dt_object *dt,
2234 struct osd_object *obj = osd_dt_obj(dt);
2235 struct inode *inode = obj->oo_inode;
2236 struct osd_thandle *oh;
2240 oh = container_of0(th, struct osd_thandle, ot_super);
2241 LASSERT(oh->ot_handle == NULL);
2244 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2245 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2246 /* Recycle idle OI leaf may cause additional three OI blocks
2248 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2249 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2250 /* one less inode */
2251 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2252 false, true, NULL, false);
2255 /* data to be truncated */
2256 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2257 true, true, NULL, false);
2261 static int osd_object_destroy(const struct lu_env *env,
2262 struct dt_object *dt,
2265 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2266 struct osd_object *obj = osd_dt_obj(dt);
2267 struct inode *inode = obj->oo_inode;
2268 struct osd_device *osd = osd_obj2dev(obj);
2269 struct osd_thandle *oh;
2273 oh = container_of0(th, struct osd_thandle, ot_super);
2274 LASSERT(oh->ot_handle);
2276 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2278 if (unlikely(fid_is_acct(fid)))
2281 if (S_ISDIR(inode->i_mode)) {
2282 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2283 /* it will check/delete the inode from remote parent,
2284 * how to optimize it? unlink performance impaction XXX */
2285 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2286 if (result != 0 && result != -ENOENT) {
2287 CERROR("%s: delete inode "DFID": rc = %d\n",
2288 osd_name(osd), PFID(fid), result);
2290 spin_lock(&obj->oo_guard);
2292 spin_unlock(&obj->oo_guard);
2293 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2296 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2298 result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2300 /* XXX: add to ext3 orphan list */
2301 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2303 /* not needed in the cache anymore */
2304 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2310 * Put the fid into lustre_mdt_attrs, and then place the structure
2311 * inode's ea. This fid should not be altered during the life time
2314 * \retval +ve, on success
2315 * \retval -ve, on error
2317 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2319 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2320 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2322 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2326 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2329 lustre_lma_init(lma, fid, compat, incompat);
2330 lustre_lma_swab(lma);
2332 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2334 /* LMA may already exist, but we need to check that all the
2335 * desired compat/incompat flags have been added. */
2336 if (unlikely(rc == -EEXIST)) {
2337 if (compat == 0 && incompat == 0)
2340 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2341 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2346 lustre_lma_swab(lma);
2347 if (!(~lma->lma_compat & compat) &&
2348 !(~lma->lma_incompat & incompat))
2351 lma->lma_compat |= compat;
2352 lma->lma_incompat |= incompat;
2353 lustre_lma_swab(lma);
2354 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2355 sizeof(*lma), XATTR_REPLACE);
2362 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2363 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2364 * To have compatilibility with 1.8 ldiskfs driver we need to have
2365 * magic number at start of fid data.
2366 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2369 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2370 const struct dt_rec *fid)
2372 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2373 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2374 param->edp_magic = 0;
2378 param->edp_magic = LDISKFS_LUFID_MAGIC;
2379 param->edp_len = sizeof(struct lu_fid) + 1;
2380 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2384 * Try to read the fid from inode ea into dt_rec.
2386 * \param fid object fid.
2388 * \retval 0 on success
2390 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2391 __u32 ino, struct lu_fid *fid,
2392 struct osd_inode_id *id)
2394 struct osd_thread_info *info = osd_oti_get(env);
2395 struct inode *inode;
2398 osd_id_gen(id, ino, OSD_OII_NOGEN);
2399 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2401 RETURN(PTR_ERR(inode));
2407 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2409 struct inode *parent_dir,
2410 const struct dt_rec *dot_fid,
2411 const struct dt_rec *dot_dot_fid,
2412 struct osd_thandle *oth)
2414 struct ldiskfs_dentry_param *dot_ldp;
2415 struct ldiskfs_dentry_param *dot_dot_ldp;
2417 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2418 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2420 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2421 dot_ldp->edp_magic = 0;
2422 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2423 dir, dot_ldp, dot_dot_ldp);
2427 * Create an local agent inode for remote entry
2429 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2430 struct osd_device *osd,
2431 struct osd_object *pobj,
2432 const struct lu_fid *fid,
2435 struct osd_thread_info *info = osd_oti_get(env);
2436 struct inode *local;
2437 struct osd_thandle *oh;
2442 oh = container_of(th, struct osd_thandle, ot_super);
2443 LASSERT(oh->ot_handle->h_transaction != NULL);
2445 /* FIXME: Insert index api needs to know the mode of
2446 * the remote object. Just use S_IFDIR for now */
2447 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2448 if (IS_ERR(local)) {
2449 CERROR("%s: create local error %d\n", osd_name(osd),
2450 (int)PTR_ERR(local));
2454 /* Set special LMA flag for local agent inode */
2455 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2457 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2458 osd_name(osd), PFID(fid), rc);
2459 RETURN(ERR_PTR(rc));
2462 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2463 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2464 (const struct dt_rec *)fid, oh);
2466 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2467 osd_name(osd), PFID(fid), rc);
2468 RETURN(ERR_PTR(rc));
2475 * Delete local agent inode for remote entry
2477 static int osd_delete_local_agent_inode(const struct lu_env *env,
2478 struct osd_device *osd,
2479 const struct lu_fid *fid,
2480 __u32 ino, struct osd_thandle *oh)
2482 struct osd_thread_info *oti = osd_oti_get(env);
2483 struct osd_inode_id *id = &oti->oti_id;
2484 struct inode *inode;
2487 id->oii_ino = le32_to_cpu(ino);
2488 id->oii_gen = OSD_OII_NOGEN;
2489 inode = osd_iget(oti, osd, id);
2490 if (IS_ERR(inode)) {
2491 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2492 PFID(fid), id->oii_ino, id->oii_gen);
2493 RETURN(PTR_ERR(inode));
2497 mark_inode_dirty(inode);
2498 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2499 osd_name(osd), PFID(fid), inode->i_ino);
2505 * OSD layer object create function for interoperability mode (b11826).
2506 * This is mostly similar to osd_object_create(). Only difference being, fid is
2507 * inserted into inode ea here.
2509 * \retval 0, on success
2510 * \retval -ve, on error
2512 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2513 struct lu_attr *attr,
2514 struct dt_allocation_hint *hint,
2515 struct dt_object_format *dof,
2518 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2519 struct osd_object *obj = osd_dt_obj(dt);
2520 struct osd_thread_info *info = osd_oti_get(env);
2525 LASSERT(osd_invariant(obj));
2526 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2527 LASSERT(osd_write_locked(env, obj));
2528 LASSERT(th != NULL);
2530 if (unlikely(fid_is_acct(fid)))
2531 /* Quota files can't be created from the kernel any more,
2532 * 'tune2fs -O quota' will take care of creating them */
2535 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2536 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2538 result = __osd_object_create(info, obj, attr, hint, dof, th);
2540 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2541 fid_is_on_ost(info, osd_obj2dev(obj),
2542 fid, OI_CHECK_FLD) ?
2543 LMAC_FID_ON_OST : 0, 0);
2546 result = __osd_oi_insert(env, obj, fid, th);
2548 LASSERT(ergo(result == 0,
2549 dt_object_exists(dt) && !dt_object_remote(dt)));
2550 LINVRNT(osd_invariant(obj));
2554 static int osd_declare_object_ref_add(const struct lu_env *env,
2555 struct dt_object *dt,
2556 struct thandle *handle)
2558 struct osd_thandle *oh;
2560 /* it's possible that object doesn't exist yet */
2561 LASSERT(handle != NULL);
2563 oh = container_of0(handle, struct osd_thandle, ot_super);
2564 LASSERT(oh->ot_handle == NULL);
2566 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2567 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2573 * Concurrency: @dt is write locked.
2575 static int osd_object_ref_add(const struct lu_env *env,
2576 struct dt_object *dt, struct thandle *th)
2578 struct osd_object *obj = osd_dt_obj(dt);
2579 struct inode *inode = obj->oo_inode;
2580 bool need_dirty = false;
2583 LINVRNT(osd_invariant(obj));
2584 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2585 LASSERT(osd_write_locked(env, obj));
2586 LASSERT(th != NULL);
2588 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2590 /* This based on ldiskfs_inc_count(), which is not exported.
2592 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2593 * (65000) subdirectories by storing "1" in i_nlink if the link count
2594 * would otherwise overflow. Directory tranversal tools understand
2595 * that (st_nlink == 1) indicates that the filesystem dose not track
2596 * hard links count on the directory, and will not abort subdirectory
2597 * scanning early once (st_nlink - 2) subdirs have been found.
2599 * This also has to properly handle the case of inodes with nlink == 0
2600 * in case they are being linked into the PENDING directory
2602 spin_lock(&obj->oo_guard);
2603 if (unlikely(!S_ISDIR(inode->i_mode) &&
2604 inode->i_nlink >= LDISKFS_LINK_MAX)) {
2605 /* MDD should have checked this, but good to be safe */
2607 } else if (unlikely(inode->i_nlink == 0 ||
2608 (S_ISDIR(inode->i_mode) &&
2609 inode->i_nlink >= LDISKFS_LINK_MAX))) {
2610 /* inc_nlink from 0 may cause WARN_ON */
2611 set_nlink(inode, 1);
2613 } else if (!S_ISDIR(inode->i_mode) ||
2614 (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2617 } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2619 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2620 spin_unlock(&obj->oo_guard);
2623 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2625 LINVRNT(osd_invariant(obj));
2630 static int osd_declare_object_ref_del(const struct lu_env *env,
2631 struct dt_object *dt,
2632 struct thandle *handle)
2634 struct osd_thandle *oh;
2636 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2637 LASSERT(handle != NULL);
2639 oh = container_of0(handle, struct osd_thandle, ot_super);
2640 LASSERT(oh->ot_handle == NULL);
2642 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2643 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2649 * Concurrency: @dt is write locked.
2651 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2654 struct osd_object *obj = osd_dt_obj(dt);
2655 struct inode *inode = obj->oo_inode;
2657 LINVRNT(osd_invariant(obj));
2658 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2659 LASSERT(osd_write_locked(env, obj));
2660 LASSERT(th != NULL);
2662 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2664 spin_lock(&obj->oo_guard);
2665 LASSERT(inode->i_nlink > 0);
2667 /* This based on ldiskfs_dec_count(), which is not exported.
2669 * If a directory already has nlink == 1, then do not drop the nlink
2670 * count to 0, even temporarily, to avoid race conditions with other
2671 * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2673 * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2675 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2678 spin_unlock(&obj->oo_guard);
2679 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2680 LINVRNT(osd_invariant(obj));
2682 spin_unlock(&obj->oo_guard);
2689 * Get the 64-bit version for an inode.
2691 static int osd_object_version_get(const struct lu_env *env,
2692 struct dt_object *dt, dt_obj_version_t *ver)
2694 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2696 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2697 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2698 *ver = LDISKFS_I(inode)->i_fs_version;
2703 * Concurrency: @dt is read locked.
2705 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2706 struct lu_buf *buf, const char *name,
2707 struct lustre_capa *capa)
2709 struct osd_object *obj = osd_dt_obj(dt);
2710 struct inode *inode = obj->oo_inode;
2711 struct osd_thread_info *info = osd_oti_get(env);
2712 struct dentry *dentry = &info->oti_obj_dentry;
2714 /* version get is not real XATTR but uses xattr API */
2715 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2716 /* for version we are just using xattr API but change inode
2718 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2719 osd_object_version_get(env, dt, buf->lb_buf);
2720 return sizeof(dt_obj_version_t);
2723 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2724 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2726 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2729 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2733 static int osd_declare_xattr_set(const struct lu_env *env,
2734 struct dt_object *dt,
2735 const struct lu_buf *buf, const char *name,
2736 int fl, struct thandle *handle)
2738 struct osd_thandle *oh;
2740 LASSERT(handle != NULL);
2742 oh = container_of0(handle, struct osd_thandle, ot_super);
2743 LASSERT(oh->ot_handle == NULL);
2745 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2746 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2747 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2748 osd_dto_credits_noquota[DTO_XATTR_SET]);
2754 * Set the 64-bit version for object
2756 static void osd_object_version_set(const struct lu_env *env,
2757 struct dt_object *dt,
2758 dt_obj_version_t *new_version)
2760 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2762 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2763 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2765 LDISKFS_I(inode)->i_fs_version = *new_version;
2766 /** Version is set after all inode operations are finished,
2767 * so we should mark it dirty here */
2768 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2772 * Concurrency: @dt is write locked.
2774 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2775 const struct lu_buf *buf, const char *name, int fl,
2776 struct thandle *handle, struct lustre_capa *capa)
2778 struct osd_object *obj = osd_dt_obj(dt);
2779 struct inode *inode = obj->oo_inode;
2780 struct osd_thread_info *info = osd_oti_get(env);
2784 LASSERT(handle != NULL);
2786 /* version set is not real XATTR */
2787 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2788 /* for version we are just using xattr API but change inode
2790 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2791 osd_object_version_set(env, dt, buf->lb_buf);
2792 return sizeof(dt_obj_version_t);
2795 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2798 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2799 if (fl & LU_XATTR_REPLACE)
2800 fs_flags |= XATTR_REPLACE;
2802 if (fl & LU_XATTR_CREATE)
2803 fs_flags |= XATTR_CREATE;
2805 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2810 * Concurrency: @dt is read locked.
2812 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2813 struct lu_buf *buf, struct lustre_capa *capa)
2815 struct osd_object *obj = osd_dt_obj(dt);
2816 struct inode *inode = obj->oo_inode;
2817 struct osd_thread_info *info = osd_oti_get(env);
2818 struct dentry *dentry = &info->oti_obj_dentry;
2820 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2821 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2822 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2824 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2827 dentry->d_inode = inode;
2828 dentry->d_sb = inode->i_sb;
2829 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2832 static int osd_declare_xattr_del(const struct lu_env *env,
2833 struct dt_object *dt, const char *name,
2834 struct thandle *handle)
2836 struct osd_thandle *oh;
2838 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2839 LASSERT(handle != NULL);
2841 oh = container_of0(handle, struct osd_thandle, ot_super);
2842 LASSERT(oh->ot_handle == NULL);
2844 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2845 osd_dto_credits_noquota[DTO_XATTR_SET]);
2851 * Concurrency: @dt is write locked.
2853 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2854 const char *name, struct thandle *handle,
2855 struct lustre_capa *capa)
2857 struct osd_object *obj = osd_dt_obj(dt);
2858 struct inode *inode = obj->oo_inode;
2859 struct osd_thread_info *info = osd_oti_get(env);
2860 struct dentry *dentry = &info->oti_obj_dentry;
2863 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2864 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2865 LASSERT(handle != NULL);
2867 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2870 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2872 ll_vfs_dq_init(inode);
2873 dentry->d_inode = inode;
2874 dentry->d_sb = inode->i_sb;
2875 rc = inode->i_op->removexattr(dentry, name);
2879 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2880 struct dt_object *dt,
2881 struct lustre_capa *old,
2884 struct osd_thread_info *info = osd_oti_get(env);
2885 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2886 struct osd_object *obj = osd_dt_obj(dt);
2887 struct osd_device *dev = osd_obj2dev(obj);
2888 struct lustre_capa_key *key = &info->oti_capa_key;
2889 struct lustre_capa *capa = &info->oti_capa;
2890 struct obd_capa *oc;
2891 struct md_capainfo *ci;
2895 if (!dev->od_fl_capa)
2896 RETURN(ERR_PTR(-ENOENT));
2898 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2899 LINVRNT(osd_invariant(obj));
2901 /* renewal sanity check */
2902 if (old && osd_object_auth(env, dt, old, opc))
2903 RETURN(ERR_PTR(-EACCES));
2905 ci = md_capainfo(env);
2907 RETURN(ERR_PTR(-ENOENT));
2909 switch (ci->mc_auth) {
2913 capa->lc_uid = obj->oo_inode->i_uid;
2914 capa->lc_gid = obj->oo_inode->i_gid;
2915 capa->lc_flags = LC_ID_PLAIN;
2917 case LC_ID_CONVERT: {
2920 s[0] = obj->oo_inode->i_uid;
2921 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2922 s[2] = obj->oo_inode->i_gid;
2923 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2924 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2926 RETURN(ERR_PTR(rc));
2928 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2929 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2930 capa->lc_flags = LC_ID_CONVERT;
2934 RETURN(ERR_PTR(-EINVAL));
2937 capa->lc_fid = *fid;
2939 capa->lc_flags |= dev->od_capa_alg << 24;
2940 capa->lc_timeout = dev->od_capa_timeout;
2941 capa->lc_expiry = 0;
2943 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2945 LASSERT(!capa_is_expired(oc));
2949 spin_lock(&capa_lock);
2950 *key = dev->od_capa_keys[1];
2951 spin_unlock(&capa_lock);
2953 capa->lc_keyid = key->lk_keyid;
2954 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2956 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2958 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2959 RETURN(ERR_PTR(rc));
2962 oc = capa_add(dev->od_capa_hash, capa);
2966 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2968 struct osd_object *obj = osd_dt_obj(dt);
2969 struct inode *inode = obj->oo_inode;
2970 struct osd_thread_info *info = osd_oti_get(env);
2971 struct dentry *dentry = &info->oti_obj_dentry;
2972 struct file *file = &info->oti_file;
2977 dentry->d_inode = inode;
2978 dentry->d_sb = inode->i_sb;
2979 file->f_dentry = dentry;
2980 file->f_mapping = inode->i_mapping;
2981 file->f_op = inode->i_fop;
2982 #ifndef HAVE_FILE_FSYNC_4ARGS
2983 mutex_lock(&inode->i_mutex);
2985 rc = do_fsync(file, 0);
2986 #ifndef HAVE_FILE_FSYNC_4ARGS
2987 mutex_unlock(&inode->i_mutex);
2992 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2995 struct osd_object *obj = osd_dt_obj(dt);
2998 *data = (void *)obj->oo_inode;
3006 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3007 const struct dt_index_features *feat)
3009 struct iam_descr *descr;
3011 if (osd_object_is_root(o))
3012 return feat == &dt_directory_features;
3014 LASSERT(o->oo_dir != NULL);
3016 descr = o->oo_dir->od_container.ic_descr;
3017 if (feat == &dt_directory_features) {
3018 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3024 feat->dif_keysize_min <= descr->id_key_size &&
3025 descr->id_key_size <= feat->dif_keysize_max &&
3026 feat->dif_recsize_min <= descr->id_rec_size &&
3027 descr->id_rec_size <= feat->dif_recsize_max &&
3028 !(feat->dif_flags & (DT_IND_VARKEY |
3029 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3030 ergo(feat->dif_flags & DT_IND_UPDATE,
3031 1 /* XXX check that object (and file system) is
3036 static int osd_iam_container_init(const struct lu_env *env,
3037 struct osd_object *obj,
3038 struct osd_directory *dir)
3040 struct iam_container *bag = &dir->od_container;
3043 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3047 result = iam_container_setup(bag);
3049 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3051 iam_container_fini(bag);
3058 * Concurrency: no external locking is necessary.
3060 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3061 const struct dt_index_features *feat)
3065 struct osd_object *obj = osd_dt_obj(dt);
3067 LINVRNT(osd_invariant(obj));
3069 if (osd_object_is_root(obj)) {
3070 dt->do_index_ops = &osd_index_ea_ops;
3072 } else if (feat == &dt_directory_features) {
3073 dt->do_index_ops = &osd_index_ea_ops;
3074 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3079 } else if (unlikely(feat == &dt_otable_features)) {
3080 dt->do_index_ops = &osd_otable_ops;
3082 } else if (unlikely(feat == &dt_acct_features)) {
3083 dt->do_index_ops = &osd_acct_index_ops;
3086 } else if (!osd_has_index(obj)) {
3087 struct osd_directory *dir;
3092 spin_lock(&obj->oo_guard);
3093 if (obj->oo_dir == NULL)
3097 * Concurrent thread allocated container data.
3100 spin_unlock(&obj->oo_guard);
3102 * Now, that we have container data, serialize its
3105 down_write(&obj->oo_ext_idx_sem);
3107 * recheck under lock.
3109 if (!osd_has_index(obj))
3110 result = osd_iam_container_init(env, obj, dir);
3113 up_write(&obj->oo_ext_idx_sem);
3121 if (result == 0 && skip_iam == 0) {
3122 if (!osd_iam_index_probe(env, obj, feat))
3125 LINVRNT(osd_invariant(obj));
3127 if (result == 0 && is_quota_glb_feat(feat) &&
3128 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3129 result = osd_quota_migration(env, dt, feat);
3134 static int osd_otable_it_attr_get(const struct lu_env *env,
3135 struct dt_object *dt,
3136 struct lu_attr *attr,
3137 struct lustre_capa *capa)
3143 static const struct dt_object_operations osd_obj_ops = {
3144 .do_read_lock = osd_object_read_lock,
3145 .do_write_lock = osd_object_write_lock,
3146 .do_read_unlock = osd_object_read_unlock,
3147 .do_write_unlock = osd_object_write_unlock,
3148 .do_write_locked = osd_object_write_locked,
3149 .do_attr_get = osd_attr_get,
3150 .do_declare_attr_set = osd_declare_attr_set,
3151 .do_attr_set = osd_attr_set,
3152 .do_ah_init = osd_ah_init,
3153 .do_declare_create = osd_declare_object_create,
3154 .do_create = osd_object_create,
3155 .do_declare_destroy = osd_declare_object_destroy,
3156 .do_destroy = osd_object_destroy,
3157 .do_index_try = osd_index_try,
3158 .do_declare_ref_add = osd_declare_object_ref_add,
3159 .do_ref_add = osd_object_ref_add,
3160 .do_declare_ref_del = osd_declare_object_ref_del,
3161 .do_ref_del = osd_object_ref_del,
3162 .do_xattr_get = osd_xattr_get,
3163 .do_declare_xattr_set = osd_declare_xattr_set,
3164 .do_xattr_set = osd_xattr_set,
3165 .do_declare_xattr_del = osd_declare_xattr_del,
3166 .do_xattr_del = osd_xattr_del,
3167 .do_xattr_list = osd_xattr_list,
3168 .do_capa_get = osd_capa_get,
3169 .do_object_sync = osd_object_sync,
3170 .do_data_get = osd_data_get,
3174 * dt_object_operations for interoperability mode
3175 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3177 static const struct dt_object_operations osd_obj_ea_ops = {
3178 .do_read_lock = osd_object_read_lock,
3179 .do_write_lock = osd_object_write_lock,
3180 .do_read_unlock = osd_object_read_unlock,
3181 .do_write_unlock = osd_object_write_unlock,
3182 .do_write_locked = osd_object_write_locked,
3183 .do_attr_get = osd_attr_get,
3184 .do_declare_attr_set = osd_declare_attr_set,
3185 .do_attr_set = osd_attr_set,
3186 .do_ah_init = osd_ah_init,
3187 .do_declare_create = osd_declare_object_create,
3188 .do_create = osd_object_ea_create,
3189 .do_declare_destroy = osd_declare_object_destroy,
3190 .do_destroy = osd_object_destroy,
3191 .do_index_try = osd_index_try,
3192 .do_declare_ref_add = osd_declare_object_ref_add,
3193 .do_ref_add = osd_object_ref_add,
3194 .do_declare_ref_del = osd_declare_object_ref_del,
3195 .do_ref_del = osd_object_ref_del,
3196 .do_xattr_get = osd_xattr_get,
3197 .do_declare_xattr_set = osd_declare_xattr_set,
3198 .do_xattr_set = osd_xattr_set,
3199 .do_declare_xattr_del = osd_declare_xattr_del,
3200 .do_xattr_del = osd_xattr_del,
3201 .do_xattr_list = osd_xattr_list,
3202 .do_capa_get = osd_capa_get,
3203 .do_object_sync = osd_object_sync,
3204 .do_data_get = osd_data_get,
3207 static const struct dt_object_operations osd_obj_otable_it_ops = {
3208 .do_attr_get = osd_otable_it_attr_get,
3209 .do_index_try = osd_index_try,
3212 static int osd_index_declare_iam_delete(const struct lu_env *env,
3213 struct dt_object *dt,
3214 const struct dt_key *key,
3215 struct thandle *handle)
3217 struct osd_thandle *oh;
3219 oh = container_of0(handle, struct osd_thandle, ot_super);
3220 LASSERT(oh->ot_handle == NULL);
3222 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3223 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3229 * delete a (key, value) pair from index \a dt specified by \a key
3231 * \param dt osd index object
3232 * \param key key for index
3233 * \param rec record reference
3234 * \param handle transaction handler
3237 * \retval -ve failure
3240 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3241 const struct dt_key *key,
3242 struct thandle *handle,
3243 struct lustre_capa *capa)
3245 struct osd_thread_info *oti = osd_oti_get(env);
3246 struct osd_object *obj = osd_dt_obj(dt);
3247 struct osd_thandle *oh;
3248 struct iam_path_descr *ipd;
3249 struct iam_container *bag = &obj->oo_dir->od_container;
3254 LINVRNT(osd_invariant(obj));
3255 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3256 LASSERT(bag->ic_object == obj->oo_inode);
3257 LASSERT(handle != NULL);
3259 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3262 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3264 ipd = osd_idx_ipd_get(env, bag);
3265 if (unlikely(ipd == NULL))
3268 oh = container_of0(handle, struct osd_thandle, ot_super);
3269 LASSERT(oh->ot_handle != NULL);
3270 LASSERT(oh->ot_handle->h_transaction != NULL);
3272 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3273 /* swab quota uid/gid provided by caller */
3274 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3275 key = (const struct dt_key *)&oti->oti_quota_id;
3278 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3279 osd_ipd_put(env, bag, ipd);
3280 LINVRNT(osd_invariant(obj));
3284 static int osd_index_declare_ea_delete(const struct lu_env *env,
3285 struct dt_object *dt,
3286 const struct dt_key *key,
3287 struct thandle *handle)
3289 struct osd_thandle *oh;
3290 struct inode *inode;
3294 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3295 LASSERT(handle != NULL);
3297 oh = container_of0(handle, struct osd_thandle, ot_super);
3298 LASSERT(oh->ot_handle == NULL);
3300 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3301 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3303 inode = osd_dt_obj(dt)->oo_inode;
3306 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3307 true, true, NULL, false);
3311 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3314 struct osd_fid_pack *rec;
3317 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3318 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3319 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3324 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3327 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3328 struct seq_server_site *ss = osd_seq_site(osd);
3332 /* Those FID seqs, which are not in FLDB, must be local seq */
3333 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3336 rc = osd_fld_lookup(env, osd, fid, range);
3338 CERROR("%s: Can not lookup fld for "DFID"\n",
3339 osd_name(osd), PFID(fid));
3343 RETURN(ss->ss_node_id != range->lsr_index);
3347 * Index delete function for interoperability mode (b11826).
3348 * It will remove the directory entry added by osd_index_ea_insert().
3349 * This entry is needed to maintain name->fid mapping.
3351 * \param key, key i.e. file entry to be deleted
3353 * \retval 0, on success
3354 * \retval -ve, on error
3356 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3357 const struct dt_key *key,
3358 struct thandle *handle,
3359 struct lustre_capa *capa)
3361 struct osd_object *obj = osd_dt_obj(dt);
3362 struct inode *dir = obj->oo_inode;
3363 struct dentry *dentry;
3364 struct osd_thandle *oh;
3365 struct ldiskfs_dir_entry_2 *de = NULL;
3366 struct buffer_head *bh;
3367 struct htree_lock *hlock = NULL;
3368 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3369 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3373 LINVRNT(osd_invariant(obj));
3374 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3375 LASSERT(handle != NULL);
3377 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3379 oh = container_of(handle, struct osd_thandle, ot_super);
3380 LASSERT(oh->ot_handle != NULL);
3381 LASSERT(oh->ot_handle->h_transaction != NULL);
3383 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3386 ll_vfs_dq_init(dir);
3387 dentry = osd_child_dentry_get(env, obj,
3388 (char *)key, strlen((char *)key));
3390 if (obj->oo_hl_head != NULL) {
3391 hlock = osd_oti_get(env)->oti_hlock;
3392 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3393 dir, LDISKFS_HLOCK_DEL);
3395 down_write(&obj->oo_ext_idx_sem);
3398 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3402 /* If this is not the ".." entry, it might be a remote DNE
3403 * entry and we need to check if the FID is for a remote
3404 * MDT. If the FID is not in the directory entry (e.g.
3405 * upgraded 1.8 filesystem without dirdata enabled) then
3406 * we need to get the FID from the LMA. For a remote directory
3407 * there HAS to be an LMA, it cannot be an IGIF inode in this
3410 * Delete the entry before the agent inode in order to
3411 * simplify error handling. At worst an error after deleting
3412 * the entry first might leak the agent inode afterward. The
3413 * reverse would need filesystem abort in case of error deleting
3414 * the entry after the agent had been removed, or leave a
3415 * dangling entry pointing at a random inode. */
3416 if (strcmp((char *)key, dotdot) != 0) {
3417 LASSERT(de != NULL);
3418 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3419 /* If Fid is not in dentry, try to get it from LMA */
3420 if (rc == -ENODATA) {
3421 struct osd_inode_id *id;
3422 struct inode *inode;
3424 /* Before trying to get fid from the inode,
3425 * check whether the inode is valid.
3427 * If the inode has been deleted, do not go
3428 * ahead to do osd_ea_fid_get, which will set
3429 * the inode to bad inode, which might cause
3430 * the inode to be deleted uncorrectly */
3431 inode = ldiskfs_iget(osd_sb(osd),
3432 le32_to_cpu(de->inode));
3433 if (IS_ERR(inode)) {
3434 CDEBUG(D_INODE, "%s: "DFID"get inode"
3435 "error.\n", osd_name(osd),
3437 rc = PTR_ERR(inode);
3439 if (likely(inode->i_nlink != 0)) {
3440 id = &osd_oti_get(env)->oti_id;
3441 rc = osd_ea_fid_get(env, obj,
3442 le32_to_cpu(de->inode),
3445 CDEBUG(D_INFO, "%s: %u "DFID
3448 le32_to_cpu(de->inode),
3456 unlikely(osd_remote_fid(env, osd, fid)))
3457 /* Need to delete agent inode */
3458 ino = le32_to_cpu(de->inode);
3460 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3462 if (rc == 0 && unlikely(ino != 0)) {
3463 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3466 CERROR("%s: del local inode "DFID": rc = %d\n",
3467 osd_name(osd), PFID(fid), rc);
3473 ldiskfs_htree_unlock(hlock);
3475 up_write(&obj->oo_ext_idx_sem);
3480 /* For inode on the remote MDT, .. will point to
3481 * /Agent directory, Check whether it needs to delete
3482 * from agent directory */
3483 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3484 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3486 if (rc != 0 && rc != -ENOENT) {
3487 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3488 osd_name(osd), PFID(fid), rc);
3498 LASSERT(osd_invariant(obj));
3503 * Lookup index for \a key and copy record to \a rec.
3505 * \param dt osd index object
3506 * \param key key for index
3507 * \param rec record reference
3509 * \retval +ve success : exact mach
3510 * \retval 0 return record with key not greater than \a key
3511 * \retval -ve failure
3513 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3514 struct dt_rec *rec, const struct dt_key *key,
3515 struct lustre_capa *capa)
3517 struct osd_object *obj = osd_dt_obj(dt);
3518 struct iam_path_descr *ipd;
3519 struct iam_container *bag = &obj->oo_dir->od_container;
3520 struct osd_thread_info *oti = osd_oti_get(env);
3521 struct iam_iterator *it = &oti->oti_idx_it;
3522 struct iam_rec *iam_rec;
3527 LASSERT(osd_invariant(obj));
3528 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3529 LASSERT(bag->ic_object == obj->oo_inode);
3531 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3534 ipd = osd_idx_ipd_get(env, bag);
3538 /* got ipd now we can start iterator. */
3539 iam_it_init(it, bag, 0, ipd);
3541 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3542 /* swab quota uid/gid provided by caller */
3543 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3544 key = (const struct dt_key *)&oti->oti_quota_id;
3547 rc = iam_it_get(it, (struct iam_key *)key);
3549 if (S_ISDIR(obj->oo_inode->i_mode))
3550 iam_rec = (struct iam_rec *)oti->oti_ldp;
3552 iam_rec = (struct iam_rec *) rec;
3554 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3556 if (S_ISDIR(obj->oo_inode->i_mode))
3557 osd_fid_unpack((struct lu_fid *) rec,
3558 (struct osd_fid_pack *)iam_rec);
3559 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3560 osd_quota_unpack(obj, rec);
3565 osd_ipd_put(env, bag, ipd);
3567 LINVRNT(osd_invariant(obj));
3572 static int osd_index_declare_iam_insert(const struct lu_env *env,
3573 struct dt_object *dt,
3574 const struct dt_rec *rec,
3575 const struct dt_key *key,
3576 struct thandle *handle)
3578 struct osd_thandle *oh;
3580 LASSERT(handle != NULL);
3582 oh = container_of0(handle, struct osd_thandle, ot_super);
3583 LASSERT(oh->ot_handle == NULL);
3585 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3586 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3592 * Inserts (key, value) pair in \a dt index object.
3594 * \param dt osd index object
3595 * \param key key for index
3596 * \param rec record reference
3597 * \param th transaction handler
3600 * \retval -ve failure
3602 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3603 const struct dt_rec *rec,
3604 const struct dt_key *key, struct thandle *th,
3605 struct lustre_capa *capa, int ignore_quota)
3607 struct osd_object *obj = osd_dt_obj(dt);
3608 struct iam_path_descr *ipd;
3609 struct osd_thandle *oh;
3610 struct iam_container *bag = &obj->oo_dir->od_container;
3611 struct osd_thread_info *oti = osd_oti_get(env);
3612 struct iam_rec *iam_rec;
3617 LINVRNT(osd_invariant(obj));
3618 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3619 LASSERT(bag->ic_object == obj->oo_inode);
3620 LASSERT(th != NULL);
3622 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3625 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3627 ipd = osd_idx_ipd_get(env, bag);
3628 if (unlikely(ipd == NULL))
3631 oh = container_of0(th, struct osd_thandle, ot_super);
3632 LASSERT(oh->ot_handle != NULL);
3633 LASSERT(oh->ot_handle->h_transaction != NULL);
3634 if (S_ISDIR(obj->oo_inode->i_mode)) {
3635 iam_rec = (struct iam_rec *)oti->oti_ldp;
3636 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3637 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3638 /* pack quota uid/gid */
3639 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3640 key = (const struct dt_key *)&oti->oti_quota_id;
3641 /* pack quota record */
3642 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3643 iam_rec = (struct iam_rec *)rec;
3645 iam_rec = (struct iam_rec *)rec;
3648 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3650 osd_ipd_put(env, bag, ipd);
3651 LINVRNT(osd_invariant(obj));
3656 * Calls ldiskfs_add_entry() to add directory entry
3657 * into the directory. This is required for
3658 * interoperability mode (b11826)
3660 * \retval 0, on success
3661 * \retval -ve, on error
3663 static int __osd_ea_add_rec(struct osd_thread_info *info,
3664 struct osd_object *pobj, struct inode *cinode,
3665 const char *name, const struct dt_rec *fid,
3666 struct htree_lock *hlock, struct thandle *th)
3668 struct ldiskfs_dentry_param *ldp;
3669 struct dentry *child;
3670 struct osd_thandle *oth;
3673 oth = container_of(th, struct osd_thandle, ot_super);
3674 LASSERT(oth->ot_handle != NULL);
3675 LASSERT(oth->ot_handle->h_transaction != NULL);
3676 LASSERT(pobj->oo_inode);
3678 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3679 if (unlikely(pobj->oo_inode ==
3680 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3683 osd_get_ldiskfs_dirent_param(ldp, fid);
3684 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3685 child->d_fsdata = (void *)ldp;
3686 ll_vfs_dq_init(pobj->oo_inode);
3687 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3693 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3694 * into the directory.Also sets flags into osd object to
3695 * indicate dot and dotdot are created. This is required for
3696 * interoperability mode (b11826)
3698 * \param dir directory for dot and dotdot fixup.
3699 * \param obj child object for linking
3701 * \retval 0, on success
3702 * \retval -ve, on error
3704 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3705 struct osd_object *dir,
3706 struct inode *parent_dir, const char *name,
3707 const struct dt_rec *dot_fid,
3708 const struct dt_rec *dot_dot_fid,
3711 struct inode *inode = dir->oo_inode;
3712 struct osd_thandle *oth;
3715 oth = container_of(th, struct osd_thandle, ot_super);
3716 LASSERT(oth->ot_handle->h_transaction != NULL);
3717 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3719 if (strcmp(name, dot) == 0) {
3720 if (dir->oo_compat_dot_created) {
3723 LASSERT(inode == parent_dir);
3724 dir->oo_compat_dot_created = 1;
3727 } else if (strcmp(name, dotdot) == 0) {
3728 if (!dir->oo_compat_dot_created)
3730 /* in case of rename, dotdot is already created */
3731 if (dir->oo_compat_dotdot_created) {
3732 return __osd_ea_add_rec(info, dir, parent_dir, name,
3733 dot_dot_fid, NULL, th);
3736 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3737 parent_dir, dot_fid,
3740 dir->oo_compat_dotdot_created = 1;
3748 * It will call the appropriate osd_add* function and return the
3749 * value, return by respective functions.
3751 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3752 struct inode *cinode, const char *name,
3753 const struct dt_rec *fid, struct thandle *th)
3755 struct osd_thread_info *info = osd_oti_get(env);
3756 struct htree_lock *hlock;
3759 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3761 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3763 if (hlock != NULL) {
3764 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3767 down_write(&pobj->oo_ext_idx_sem);
3769 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3770 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3773 if (hlock != NULL) {
3774 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3775 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3777 down_write(&pobj->oo_ext_idx_sem);
3780 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3781 struct lu_fid *tfid = &info->oti_fid;
3783 *tfid = *(const struct lu_fid *)fid;
3785 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3786 (const struct dt_rec *)tfid,
3789 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3794 ldiskfs_htree_unlock(hlock);
3796 up_write(&pobj->oo_ext_idx_sem);
3802 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3803 struct osd_idmap_cache *oic)
3805 struct osd_scrub *scrub = &dev->od_scrub;
3806 struct lu_fid *fid = &oic->oic_fid;
3807 struct osd_inode_id *id = &oti->oti_id;
3812 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3816 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3817 if (rc != 0 && rc != -ENOENT)
3820 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3823 if (thread_is_running(&scrub->os_thread)) {
3824 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3825 /* There is race condition between osd_oi_lookup and OI scrub.
3826 * The OI scrub finished just after osd_oi_lookup() failure.
3827 * Under such case, it is unnecessary to trigger OI scrub again,
3828 * but try to call osd_oi_lookup() again. */
3829 if (unlikely(rc == -EAGAIN))
3835 if (!dev->od_noscrub && ++once == 1) {
3836 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3838 rc = osd_scrub_start(dev);
3839 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3841 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3850 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3851 struct osd_device *dev,
3852 struct osd_idmap_cache *oic,
3853 struct lu_fid *fid, __u32 ino)
3855 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3856 struct inode *inode;
3859 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3860 inode = osd_iget(oti, dev, &oic->oic_lid);
3861 if (IS_ERR(inode)) {
3862 fid_zero(&oic->oic_fid);
3863 return PTR_ERR(inode);
3866 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3869 fid_zero(&oic->oic_fid);
3871 *fid = oic->oic_fid = lma->lma_self_fid;
3875 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3876 struct osd_inode_id *id, const struct lu_fid *fid)
3878 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3879 id->oii_ino, id->oii_gen, info);
3880 info->oti_cache.oic_lid = *id;
3881 info->oti_cache.oic_fid = *fid;
3887 * Calls ->lookup() to find dentry. From dentry get inode and
3888 * read inode's ea to get fid. This is required for interoperability
3891 * \retval 0, on success
3892 * \retval -ve, on error
3894 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3895 struct dt_rec *rec, const struct dt_key *key)
3897 struct inode *dir = obj->oo_inode;
3898 struct dentry *dentry;
3899 struct ldiskfs_dir_entry_2 *de;
3900 struct buffer_head *bh;
3901 struct lu_fid *fid = (struct lu_fid *) rec;
3902 struct htree_lock *hlock = NULL;
3907 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3909 dentry = osd_child_dentry_get(env, obj,
3910 (char *)key, strlen((char *)key));
3912 if (obj->oo_hl_head != NULL) {
3913 hlock = osd_oti_get(env)->oti_hlock;
3914 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3915 dir, LDISKFS_HLOCK_LOOKUP);
3917 down_read(&obj->oo_ext_idx_sem);
3920 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3922 struct osd_thread_info *oti = osd_oti_get(env);
3923 struct osd_inode_id *id = &oti->oti_id;
3924 struct osd_idmap_cache *oic = &oti->oti_cache;
3925 struct osd_device *dev = osd_obj2dev(obj);
3926 struct osd_scrub *scrub = &dev->od_scrub;
3927 struct scrub_file *sf = &scrub->os_file;
3929 ino = le32_to_cpu(de->inode);
3930 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3932 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3936 rc = osd_get_fid_from_dentry(de, rec);
3938 /* done with de, release bh */
3941 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3943 osd_id_gen(id, ino, OSD_OII_NOGEN);
3944 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3945 fid_zero(&oic->oic_fid);
3949 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3953 if ((scrub->os_pos_current <= ino) &&
3954 ((sf->sf_flags & SF_INCONSISTENT) ||
3955 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3956 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3958 osd_consistency_check(oti, dev, oic);
3967 ldiskfs_htree_unlock(hlock);
3969 up_read(&obj->oo_ext_idx_sem);
3974 * Find the osd object for given fid.
3976 * \param fid need to find the osd object having this fid
3978 * \retval osd_object on success
3979 * \retval -ve on error
3981 struct osd_object *osd_object_find(const struct lu_env *env,
3982 struct dt_object *dt,
3983 const struct lu_fid *fid)
3985 struct lu_device *ludev = dt->do_lu.lo_dev;
3986 struct osd_object *child = NULL;
3987 struct lu_object *luch;
3988 struct lu_object *lo;
3991 * at this point topdev might not exist yet
3992 * (i.e. MGS is preparing profiles). so we can
3993 * not rely on topdev and instead lookup with
3994 * our device passed as topdev. this can't work
3995 * if the object isn't cached yet (as osd doesn't
3996 * allocate lu_header). IOW, the object must be
3997 * in the cache, otherwise lu_object_alloc() crashes
4000 luch = lu_object_find_at(env, ludev, fid, NULL);
4001 if (!IS_ERR(luch)) {
4002 if (lu_object_exists(luch)) {
4003 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4005 child = osd_obj(lo);
4007 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4008 "lu_object can't be located"
4009 DFID"\n", PFID(fid));
4011 if (child == NULL) {
4012 lu_object_put(env, luch);
4013 CERROR("Unable to get osd_object\n");
4014 child = ERR_PTR(-ENOENT);
4017 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4018 "lu_object does not exists "DFID"\n",
4020 lu_object_put(env, luch);
4021 child = ERR_PTR(-ENOENT);
4024 child = (void *)luch;
4030 * Put the osd object once done with it.
4032 * \param obj osd object that needs to be put
4034 static inline void osd_object_put(const struct lu_env *env,
4035 struct osd_object *obj)
4037 lu_object_put(env, &obj->oo_dt.do_lu);
4040 static int osd_index_declare_ea_insert(const struct lu_env *env,
4041 struct dt_object *dt,
4042 const struct dt_rec *rec,
4043 const struct dt_key *key,
4044 struct thandle *handle)
4046 struct osd_thandle *oh;
4047 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4048 struct lu_fid *fid = (struct lu_fid *)rec;
4052 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4053 LASSERT(handle != NULL);
4055 oh = container_of0(handle, struct osd_thandle, ot_super);
4056 LASSERT(oh->ot_handle == NULL);
4058 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4059 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4061 if (osd_dt_obj(dt)->oo_inode == NULL) {
4062 const char *name = (const char *)key;
4063 /* Object is not being created yet. Only happens when
4064 * 1. declare directory create
4065 * 2. declare insert .
4066 * 3. declare insert ..
4068 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4070 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4072 /* We ignore block quota on meta pool (MDTs), so needn't
4073 * calculate how many blocks will be consumed by this index
4075 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4076 oh, true, true, NULL, false);
4082 rc = osd_remote_fid(env, osd, fid);
4088 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4089 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4090 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4091 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4092 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4093 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4099 * Index add function for interoperability mode (b11826).
4100 * It will add the directory entry.This entry is needed to
4101 * maintain name->fid mapping.
4103 * \param key it is key i.e. file entry to be inserted
4104 * \param rec it is value of given key i.e. fid
4106 * \retval 0, on success
4107 * \retval -ve, on error
4109 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4110 const struct dt_rec *rec,
4111 const struct dt_key *key, struct thandle *th,
4112 struct lustre_capa *capa, int ignore_quota)
4114 struct osd_object *obj = osd_dt_obj(dt);
4115 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4116 struct lu_fid *fid = (struct lu_fid *) rec;
4117 const char *name = (const char *)key;
4118 struct osd_thread_info *oti = osd_oti_get(env);
4119 struct osd_inode_id *id = &oti->oti_id;
4120 struct inode *child_inode = NULL;
4121 struct osd_object *child = NULL;
4125 LASSERT(osd_invariant(obj));
4126 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4127 LASSERT(th != NULL);
4129 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4131 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4134 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4136 rc = osd_remote_fid(env, osd, fid);
4138 CERROR("%s: Can not find object "DFID" rc %d\n",
4139 osd_name(osd), PFID(fid), rc);
4144 /* Insert remote entry */
4145 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4146 struct osd_mdobj_map *omm = osd->od_mdt_map;
4147 struct osd_thandle *oh;
4149 /* If parent on remote MDT, we need put this object
4151 oh = container_of(th, typeof(*oh), ot_super);
4152 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4154 CERROR("%s: add "DFID" error: rc = %d\n",
4156 PFID(lu_object_fid(&dt->do_lu)), rc);
4160 child_inode = igrab(omm->omm_remote_parent->d_inode);
4162 child_inode = osd_create_local_agent_inode(env, osd,
4165 if (IS_ERR(child_inode))
4166 RETURN(PTR_ERR(child_inode));
4169 /* Insert local entry */
4170 child = osd_object_find(env, dt, fid);
4171 if (IS_ERR(child)) {
4172 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4173 osd_name(osd), PFID(fid),
4174 id->oii_ino, id->oii_gen,
4175 (int)PTR_ERR(child_inode));
4176 RETURN(PTR_ERR(child_inode));
4178 child_inode = igrab(child->oo_inode);
4181 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4185 osd_object_put(env, child);
4186 LASSERT(osd_invariant(obj));
4191 * Initialize osd Iterator for given osd index object.
4193 * \param dt osd index object
4196 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4197 struct dt_object *dt,
4199 struct lustre_capa *capa)
4201 struct osd_it_iam *it;
4202 struct osd_thread_info *oti = osd_oti_get(env);
4203 struct osd_object *obj = osd_dt_obj(dt);
4204 struct lu_object *lo = &dt->do_lu;
4205 struct iam_path_descr *ipd;
4206 struct iam_container *bag = &obj->oo_dir->od_container;
4208 LASSERT(lu_object_exists(lo));
4210 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4211 return ERR_PTR(-EACCES);
4214 ipd = osd_it_ipd_get(env, bag);
4215 if (likely(ipd != NULL)) {
4219 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4220 return (struct dt_it *)it;
4222 return ERR_PTR(-ENOMEM);
4226 * free given Iterator.
4229 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4231 struct osd_it_iam *it = (struct osd_it_iam *)di;
4232 struct osd_object *obj = it->oi_obj;
4234 iam_it_fini(&it->oi_it);
4235 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4236 lu_object_put(env, &obj->oo_dt.do_lu);
4240 * Move Iterator to record specified by \a key
4242 * \param di osd iterator
4243 * \param key key for index
4245 * \retval +ve di points to record with least key not larger than key
4246 * \retval 0 di points to exact matched key
4247 * \retval -ve failure
4250 static int osd_it_iam_get(const struct lu_env *env,
4251 struct dt_it *di, const struct dt_key *key)
4253 struct osd_thread_info *oti = osd_oti_get(env);
4254 struct osd_it_iam *it = (struct osd_it_iam *)di;
4256 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4257 /* swab quota uid/gid */
4258 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4259 key = (struct dt_key *)&oti->oti_quota_id;
4262 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4268 * \param di osd iterator
4270 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4272 struct osd_it_iam *it = (struct osd_it_iam *)di;
4274 iam_it_put(&it->oi_it);
4278 * Move iterator by one record
4280 * \param di osd iterator
4282 * \retval +1 end of container reached
4284 * \retval -ve failure
4287 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4289 struct osd_it_iam *it = (struct osd_it_iam *)di;
4291 return iam_it_next(&it->oi_it);
4295 * Return pointer to the key under iterator.
4298 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4299 const struct dt_it *di)
4301 struct osd_thread_info *oti = osd_oti_get(env);
4302 struct osd_it_iam *it = (struct osd_it_iam *)di;
4303 struct osd_object *obj = it->oi_obj;
4306 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4308 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4309 /* swab quota uid/gid */
4310 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4311 key = (struct dt_key *)&oti->oti_quota_id;
4318 * Return size of key under iterator (in bytes)
4321 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4323 struct osd_it_iam *it = (struct osd_it_iam *)di;
4325 return iam_it_key_size(&it->oi_it);
4329 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4331 /* check if file type is required */
4332 if (ent->lde_attrs & LUDA_TYPE) {
4333 struct luda_type *lt;
4334 int align = sizeof(*lt) - 1;
4336 len = (len + align) & ~align;
4337 lt = (struct luda_type *)(ent->lde_name + len);
4338 lt->lt_type = cpu_to_le16(DTTOIF(type));
4341 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4345 * build lu direct from backend fs dirent.
4349 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4350 char *name, __u16 namelen, __u16 type, __u32 attr)
4352 ent->lde_attrs = attr | LUDA_FID;
4353 fid_cpu_to_le(&ent->lde_fid, fid);
4355 ent->lde_hash = cpu_to_le64(offset);
4356 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4358 strncpy(ent->lde_name, name, namelen);
4359 ent->lde_name[namelen] = '\0';
4360 ent->lde_namelen = cpu_to_le16(namelen);
4362 /* append lustre attributes */
4363 osd_it_append_attrs(ent, namelen, type);
4367 * Return pointer to the record under iterator.
4369 static int osd_it_iam_rec(const struct lu_env *env,
4370 const struct dt_it *di,
4371 struct dt_rec *dtrec, __u32 attr)
4373 struct osd_it_iam *it = (struct osd_it_iam *)di;
4374 struct osd_thread_info *info = osd_oti_get(env);
4377 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4378 const struct osd_fid_pack *rec;
4379 struct lu_fid *fid = &info->oti_fid;
4380 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4386 name = (char *)iam_it_key_get(&it->oi_it);
4388 RETURN(PTR_ERR(name));
4390 namelen = iam_it_key_size(&it->oi_it);
4392 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4394 RETURN(PTR_ERR(rec));
4396 rc = osd_fid_unpack(fid, rec);
4400 hash = iam_it_store(&it->oi_it);
4402 /* IAM does not store object type in IAM index (dir) */
4403 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4405 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4406 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4407 (struct iam_rec *)dtrec);
4408 osd_quota_unpack(it->oi_obj, dtrec);
4410 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4411 (struct iam_rec *)dtrec);
4418 * Returns cookie for current Iterator position.
4420 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4422 struct osd_it_iam *it = (struct osd_it_iam *)di;
4424 return iam_it_store(&it->oi_it);
4428 * Restore iterator from cookie.
4430 * \param di osd iterator
4431 * \param hash Iterator location cookie
4433 * \retval +ve di points to record with least key not larger than key.
4434 * \retval 0 di points to exact matched key
4435 * \retval -ve failure
4438 static int osd_it_iam_load(const struct lu_env *env,
4439 const struct dt_it *di, __u64 hash)
4441 struct osd_it_iam *it = (struct osd_it_iam *)di;
4443 return iam_it_load(&it->oi_it, hash);
4446 static const struct dt_index_operations osd_index_iam_ops = {
4447 .dio_lookup = osd_index_iam_lookup,
4448 .dio_declare_insert = osd_index_declare_iam_insert,
4449 .dio_insert = osd_index_iam_insert,
4450 .dio_declare_delete = osd_index_declare_iam_delete,
4451 .dio_delete = osd_index_iam_delete,
4453 .init = osd_it_iam_init,
4454 .fini = osd_it_iam_fini,
4455 .get = osd_it_iam_get,
4456 .put = osd_it_iam_put,
4457 .next = osd_it_iam_next,
4458 .key = osd_it_iam_key,
4459 .key_size = osd_it_iam_key_size,
4460 .rec = osd_it_iam_rec,
4461 .store = osd_it_iam_store,
4462 .load = osd_it_iam_load
4468 * Creates or initializes iterator context.
4470 * \retval struct osd_it_ea, iterator structure on success
4473 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4474 struct dt_object *dt,
4476 struct lustre_capa *capa)
4478 struct osd_object *obj = osd_dt_obj(dt);
4479 struct osd_thread_info *info = osd_oti_get(env);
4480 struct osd_it_ea *it = &info->oti_it_ea;
4481 struct file *file = &it->oie_file;
4482 struct lu_object *lo = &dt->do_lu;
4483 struct dentry *obj_dentry = &info->oti_it_dentry;
4485 LASSERT(lu_object_exists(lo));
4487 obj_dentry->d_inode = obj->oo_inode;
4488 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4489 obj_dentry->d_name.hash = 0;
4491 it->oie_rd_dirent = 0;
4492 it->oie_it_dirent = 0;
4493 it->oie_dirent = NULL;
4494 it->oie_buf = info->oti_it_ea_buf;
4497 /* Reset the "file" totally to avoid to reuse any old value from
4498 * former readdir handling, the "file->f_pos" should be zero. */
4499 memset(file, 0, sizeof(*file));
4500 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4501 if (attr & LUDA_64BITHASH)
4502 file->f_mode = FMODE_64BITHASH;
4504 file->f_mode = FMODE_32BITHASH;
4505 file->f_dentry = obj_dentry;
4506 file->f_mapping = obj->oo_inode->i_mapping;
4507 file->f_op = obj->oo_inode->i_fop;
4509 RETURN((struct dt_it *) it);
4513 * Destroy or finishes iterator context.
4515 * \param di iterator structure to be destroyed
4517 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4519 struct osd_it_ea *it = (struct osd_it_ea *)di;
4520 struct osd_object *obj = it->oie_obj;
4521 struct inode *inode = obj->oo_inode;
4524 it->oie_file.f_op->release(inode, &it->oie_file);
4525 lu_object_put(env, &obj->oo_dt.do_lu);
4530 * It position the iterator at given key, so that next lookup continues from
4531 * that key Or it is similar to dio_it->load() but based on a key,
4532 * rather than file position.
4534 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4537 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4539 static int osd_it_ea_get(const struct lu_env *env,
4540 struct dt_it *di, const struct dt_key *key)
4542 struct osd_it_ea *it = (struct osd_it_ea *)di;
4545 LASSERT(((const char *)key)[0] == '\0');
4546 it->oie_file.f_pos = 0;
4547 it->oie_rd_dirent = 0;
4548 it->oie_it_dirent = 0;
4549 it->oie_dirent = NULL;
4557 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4562 * It is called internally by ->readdir(). It fills the
4563 * iterator's in-memory data structure with required
4564 * information i.e. name, namelen, rec_size etc.
4566 * \param buf in which information to be filled in.
4567 * \param name name of the file in given dir
4569 * \retval 0 on success
4570 * \retval 1 on buffer full
4572 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4573 loff_t offset, __u64 ino,
4576 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4577 struct osd_object *obj = it->oie_obj;
4578 struct osd_it_ea_dirent *ent = it->oie_dirent;
4579 struct lu_fid *fid = &ent->oied_fid;
4580 struct osd_fid_pack *rec;
4583 /* this should never happen */
4584 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4585 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4589 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4593 /* "." is just the object itself. */
4594 if (namelen == 1 && name[0] == '.') {
4595 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4596 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4597 rec = (struct osd_fid_pack*) (name + namelen + 1);
4598 if (osd_fid_unpack(fid, rec) != 0)
4603 d_type &= ~LDISKFS_DIRENT_LUFID;
4605 /* NOT export local root. */
4606 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4607 ino = obj->oo_inode->i_ino;
4608 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4611 ent->oied_ino = ino;
4612 ent->oied_off = offset;
4613 ent->oied_namelen = namelen;
4614 ent->oied_type = d_type;
4616 memcpy(ent->oied_name, name, namelen);
4618 it->oie_rd_dirent++;
4619 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4624 * Calls ->readdir() to load a directory entry at a time
4625 * and stored it in iterator's in-memory data structure.
4627 * \param di iterator's in memory structure
4629 * \retval 0 on success
4630 * \retval -ve on error
4632 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4633 const struct dt_it *di)
4635 struct osd_it_ea *it = (struct osd_it_ea *)di;
4636 struct osd_object *obj = it->oie_obj;
4637 struct inode *inode = obj->oo_inode;
4638 struct htree_lock *hlock = NULL;
4642 it->oie_dirent = it->oie_buf;
4643 it->oie_rd_dirent = 0;
4645 if (obj->oo_hl_head != NULL) {
4646 hlock = osd_oti_get(env)->oti_hlock;
4647 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4648 inode, LDISKFS_HLOCK_READDIR);
4650 down_read(&obj->oo_ext_idx_sem);
4653 result = inode->i_fop->readdir(&it->oie_file, it,
4654 (filldir_t) osd_ldiskfs_filldir);
4657 ldiskfs_htree_unlock(hlock);
4659 up_read(&obj->oo_ext_idx_sem);
4661 if (it->oie_rd_dirent == 0) {
4664 it->oie_dirent = it->oie_buf;
4665 it->oie_it_dirent = 1;
4672 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4673 * to load a directory entry at a time and stored it in
4674 * iterator's in-memory data structure.
4676 * \param di iterator's in memory structure
4678 * \retval +ve iterator reached to end
4679 * \retval 0 iterator not reached to end
4680 * \retval -ve on error
4682 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4684 struct osd_it_ea *it = (struct osd_it_ea *)di;
4689 if (it->oie_it_dirent < it->oie_rd_dirent) {
4691 (void *) it->oie_dirent +
4692 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4693 it->oie_dirent->oied_namelen);
4694 it->oie_it_dirent++;
4697 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4700 rc = osd_ldiskfs_it_fill(env, di);
4707 * Returns the key at current position from iterator's in memory structure.
4709 * \param di iterator's in memory structure
4711 * \retval key i.e. struct dt_key on success
4713 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4714 const struct dt_it *di)
4716 struct osd_it_ea *it = (struct osd_it_ea *)di;
4718 return (struct dt_key *)it->oie_dirent->oied_name;
4722 * Returns the key's size at current position from iterator's in memory structure.
4724 * \param di iterator's in memory structure
4726 * \retval key_size i.e. struct dt_key on success
4728 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4730 struct osd_it_ea *it = (struct osd_it_ea *)di;
4732 return it->oie_dirent->oied_namelen;
4736 osd_dirent_update(handle_t *jh, struct super_block *sb,
4737 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4738 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4740 struct osd_fid_pack *rec;
4744 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4745 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4747 rc = ldiskfs_journal_get_write_access(jh, bh);
4749 CERROR("%.16s: fail to write access for update dirent: "
4750 "name = %.*s, rc = %d\n",
4751 LDISKFS_SB(sb)->s_es->s_volume_name,
4752 ent->oied_namelen, ent->oied_name, rc);
4756 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4757 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4758 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4760 CERROR("%.16s: fail to dirty metadata for update dirent: "
4761 "name = %.*s, rc = %d\n",
4762 LDISKFS_SB(sb)->s_es->s_volume_name,
4763 ent->oied_namelen, ent->oied_name, rc);
4769 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4771 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4772 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4779 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4781 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4782 "dot_dotdot = %d\n", dot_dotdot);
4784 if (LDISKFS_DIR_REC_LEN(de) >=
4785 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4792 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4793 struct inode *dir, struct inode *inode,
4794 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4795 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4796 struct htree_lock *hlock)
4798 struct dentry *dentry;
4799 struct osd_fid_pack *rec;
4800 struct ldiskfs_dentry_param *ldp;
4804 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4805 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4808 /* There is enough space to hold the FID-in-dirent. */
4809 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4810 dir->i_sb->s_blocksize)) {
4811 rc = ldiskfs_journal_get_write_access(jh, bh);
4813 CERROR("%.16s: fail to write access for reinsert "
4814 "dirent: name = %.*s, rc = %d\n",
4815 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4816 ent->oied_namelen, ent->oied_name, rc);
4820 de->name[de->name_len] = 0;
4821 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4822 rec->fp_len = sizeof(struct lu_fid) + 1;
4823 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4824 de->file_type |= LDISKFS_DIRENT_LUFID;
4826 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4828 CERROR("%.16s: fail to dirty metadata for reinsert "
4829 "dirent: name = %.*s, rc = %d\n",
4830 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4831 ent->oied_namelen, ent->oied_name, rc);
4836 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4838 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4839 "name = %.*s, rc = %d\n",
4840 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4841 ent->oied_namelen, ent->oied_name, rc);
4845 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4847 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4848 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4849 dentry->d_fsdata = (void *)ldp;
4850 ll_vfs_dq_init(dir);
4851 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4852 /* It is too bad, we cannot reinsert the name entry back.
4853 * That means we lose it! */
4855 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4856 "name = %.*s, rc = %d\n",
4857 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4858 ent->oied_namelen, ent->oied_name, rc);
4864 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4865 struct osd_it_ea *it, struct lu_fid *fid,
4866 struct osd_inode_id *id, __u32 *attr)
4868 struct osd_thread_info *info = osd_oti_get(env);
4869 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4870 struct osd_device *dev = osd_obj2dev(obj);
4871 struct super_block *sb = osd_sb(dev);
4872 const char *devname =
4873 LDISKFS_SB(sb)->s_es->s_volume_name;
4874 struct osd_it_ea_dirent *ent = it->oie_dirent;
4875 struct inode *dir = obj->oo_inode;
4876 struct htree_lock *hlock = NULL;
4877 struct buffer_head *bh = NULL;
4878 handle_t *jh = NULL;
4879 struct ldiskfs_dir_entry_2 *de;
4880 struct dentry *dentry;
4881 struct inode *inode;
4888 if (ent->oied_name[0] == '.') {
4889 if (ent->oied_namelen == 1)
4891 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4895 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4898 /* We need to ensure that the name entry is still valid.
4899 * Because it may be removed or renamed by other already.
4901 * The unlink or rename operation will start journal before PDO lock,
4902 * so to avoid deadlock, here we need to start journal handle before
4903 * related PDO lock also. But because we do not know whether there
4904 * will be something to be repaired before PDO lock, we just start
4905 * journal without conditions.
4907 * We may need to remove the name entry firstly, then insert back.
4908 * One credit is for user quota file update.
4909 * One credit is for group quota file update.
4910 * Two credits are for dirty inode. */
4911 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4912 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4915 if (dev->od_dirent_journal) {
4916 jh = ldiskfs_journal_start_sb(sb, credits);
4919 CERROR("%.16s: fail to start trans for dirent "
4920 "check_repair: credits %d, name %.*s, rc %d\n",
4921 devname, credits, ent->oied_namelen,
4922 ent->oied_name, rc);
4926 if (obj->oo_hl_head != NULL) {
4927 hlock = osd_oti_get(env)->oti_hlock;
4928 /* "0" means exclusive lock for the whole directory.
4929 * We need to prevent others access such name entry
4930 * during the delete + insert. Neither HLOCK_ADD nor
4931 * HLOCK_DEL cannot guarantee the atomicity. */
4932 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4934 down_write(&obj->oo_ext_idx_sem);
4937 if (obj->oo_hl_head != NULL) {
4938 hlock = osd_oti_get(env)->oti_hlock;
4939 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4940 LDISKFS_HLOCK_LOOKUP);
4942 down_read(&obj->oo_ext_idx_sem);
4946 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4947 /* For dot/dotdot entry, if there is not enough space to hold the
4948 * FID-in-dirent, just keep them there. It only happens when the
4949 * device upgraded from 1.8 or restored from MDT file-level backup.
4950 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4951 * and needs to get FID from LMA when readdir, it will not affect the
4952 * performance much. */
4953 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4954 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4955 *attr |= LUDA_IGNORE;
4956 GOTO(out_journal, rc = 0);
4959 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4960 inode = osd_iget(info, dev, id);
4961 if (IS_ERR(inode)) {
4962 rc = PTR_ERR(inode);
4963 if (rc == -ENOENT || rc == -ESTALE) {
4964 *attr |= LUDA_IGNORE;
4968 GOTO(out_journal, rc);
4971 /* skip the REMOTE_PARENT_DIR. */
4972 if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
4973 GOTO(out_inode, rc = 0);
4975 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4977 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
4979 if (fid_is_sane(fid)) {
4980 /* FID-in-dirent is valid. */
4981 if (lu_fid_eq(fid, &lma->lma_self_fid))
4982 GOTO(out_inode, rc = 0);
4984 /* Do not repair under dryrun mode. */
4985 if (*attr & LUDA_VERIFY_DRYRUN) {
4986 *attr |= LUDA_REPAIR;
4987 GOTO(out_inode, rc = 0);
4990 if (!dev->od_dirent_journal) {
4994 ldiskfs_htree_unlock(hlock);
4996 up_read(&obj->oo_ext_idx_sem);
4997 dev->od_dirent_journal = 1;
5001 *fid = lma->lma_self_fid;
5003 /* Update the FID-in-dirent. */
5004 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5006 *attr |= LUDA_REPAIR;
5008 /* Do not repair under dryrun mode. */
5009 if (*attr & LUDA_VERIFY_DRYRUN) {
5010 *fid = lma->lma_self_fid;
5011 *attr |= LUDA_REPAIR;
5012 GOTO(out_inode, rc = 0);
5015 if (!dev->od_dirent_journal) {
5019 ldiskfs_htree_unlock(hlock);
5021 up_read(&obj->oo_ext_idx_sem);
5022 dev->od_dirent_journal = 1;
5026 *fid = lma->lma_self_fid;
5028 /* Append the FID-in-dirent. */
5029 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5030 fid, bh, de, hlock);
5032 *attr |= LUDA_REPAIR;
5034 } else if (rc == -ENODATA) {
5035 /* Do not repair under dryrun mode. */
5036 if (*attr & LUDA_VERIFY_DRYRUN) {
5037 if (fid_is_sane(fid)) {
5038 *attr |= LUDA_REPAIR;
5040 lu_igif_build(fid, inode->i_ino,
5041 inode->i_generation);
5042 *attr |= LUDA_UPGRADE;
5044 GOTO(out_inode, rc = 0);
5047 if (!dev->od_dirent_journal) {
5051 ldiskfs_htree_unlock(hlock);
5053 up_read(&obj->oo_ext_idx_sem);
5054 dev->od_dirent_journal = 1;
5059 if (unlikely(fid_is_sane(fid))) {
5060 /* FID-in-dirent exists, but FID-in-LMA is lost.
5061 * Trust the FID-in-dirent, and add FID-in-LMA. */
5062 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5064 *attr |= LUDA_REPAIR;
5066 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5067 /* It is probably IGIF object. Only aappend the
5068 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5069 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5070 fid, bh, de, hlock);
5072 *attr |= LUDA_UPGRADE;
5076 GOTO(out_inode, rc);
5083 if (hlock != NULL) {
5084 ldiskfs_htree_unlock(hlock);
5086 if (dev->od_dirent_journal)
5087 up_write(&obj->oo_ext_idx_sem);
5089 up_read(&obj->oo_ext_idx_sem);
5092 ldiskfs_journal_stop(jh);
5093 if (rc >= 0 && !dirty)
5094 dev->od_dirent_journal = 0;
5099 * Returns the value at current position from iterator's in memory structure.
5101 * \param di struct osd_it_ea, iterator's in memory structure
5102 * \param attr attr requested for dirent.
5103 * \param lde lustre dirent
5105 * \retval 0 no error and \param lde has correct lustre dirent.
5106 * \retval -ve on error
5108 static inline int osd_it_ea_rec(const struct lu_env *env,
5109 const struct dt_it *di,
5110 struct dt_rec *dtrec, __u32 attr)
5112 struct osd_it_ea *it = (struct osd_it_ea *)di;
5113 struct osd_object *obj = it->oie_obj;
5114 struct osd_device *dev = osd_obj2dev(obj);
5115 struct osd_scrub *scrub = &dev->od_scrub;
5116 struct scrub_file *sf = &scrub->os_file;
5117 struct osd_thread_info *oti = osd_oti_get(env);
5118 struct osd_inode_id *id = &oti->oti_id;
5119 struct osd_idmap_cache *oic = &oti->oti_cache;
5120 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5121 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5122 __u32 ino = it->oie_dirent->oied_ino;
5126 if (attr & LUDA_VERIFY) {
5128 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5129 attr |= LUDA_IGNORE;
5132 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5136 attr &= ~LU_DIRENT_ATTRS_MASK;
5137 if (!fid_is_sane(fid)) {
5138 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5141 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5143 osd_id_gen(id, ino, OSD_OII_NOGEN);
5147 /* Pack the entry anyway, at least the offset is right. */
5148 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5149 it->oie_dirent->oied_name,
5150 it->oie_dirent->oied_namelen,
5151 it->oie_dirent->oied_type, attr);
5156 if (osd_remote_fid(env, dev, fid))
5159 if (likely(!(attr & LUDA_IGNORE)))
5160 rc = osd_add_oi_cache(oti, dev, id, fid);
5162 if (!(attr & LUDA_VERIFY) &&
5163 (scrub->os_pos_current <= ino) &&
5164 ((sf->sf_flags & SF_INCONSISTENT) ||
5165 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5166 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5167 osd_consistency_check(oti, dev, oic);
5173 * Returns a cookie for current position of the iterator head, so that
5174 * user can use this cookie to load/start the iterator next time.
5176 * \param di iterator's in memory structure
5178 * \retval cookie for current position, on success
5180 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5182 struct osd_it_ea *it = (struct osd_it_ea *)di;
5184 return it->oie_dirent->oied_off;
5188 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5189 * to load a directory entry at a time and stored it i inn,
5190 * in iterator's in-memory data structure.
5192 * \param di struct osd_it_ea, iterator's in memory structure
5194 * \retval +ve on success
5195 * \retval -ve on error
5197 static int osd_it_ea_load(const struct lu_env *env,
5198 const struct dt_it *di, __u64 hash)
5200 struct osd_it_ea *it = (struct osd_it_ea *)di;
5204 it->oie_file.f_pos = hash;
5206 rc = osd_ldiskfs_it_fill(env, di);
5214 * Index lookup function for interoperability mode (b11826).
5216 * \param key, key i.e. file name to be searched
5218 * \retval +ve, on success
5219 * \retval -ve, on error
5221 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5222 struct dt_rec *rec, const struct dt_key *key,
5223 struct lustre_capa *capa)
5225 struct osd_object *obj = osd_dt_obj(dt);
5230 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5231 LINVRNT(osd_invariant(obj));
5233 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5236 rc = osd_ea_lookup_rec(env, obj, rec, key);
5243 * Index and Iterator operations for interoperability
5244 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5246 static const struct dt_index_operations osd_index_ea_ops = {
5247 .dio_lookup = osd_index_ea_lookup,
5248 .dio_declare_insert = osd_index_declare_ea_insert,
5249 .dio_insert = osd_index_ea_insert,
5250 .dio_declare_delete = osd_index_declare_ea_delete,
5251 .dio_delete = osd_index_ea_delete,
5253 .init = osd_it_ea_init,
5254 .fini = osd_it_ea_fini,
5255 .get = osd_it_ea_get,
5256 .put = osd_it_ea_put,
5257 .next = osd_it_ea_next,
5258 .key = osd_it_ea_key,
5259 .key_size = osd_it_ea_key_size,
5260 .rec = osd_it_ea_rec,
5261 .store = osd_it_ea_store,
5262 .load = osd_it_ea_load
5266 static void *osd_key_init(const struct lu_context *ctx,
5267 struct lu_context_key *key)
5269 struct osd_thread_info *info;
5271 OBD_ALLOC_PTR(info);
5273 return ERR_PTR(-ENOMEM);
5275 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5276 if (info->oti_it_ea_buf == NULL)
5279 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5281 info->oti_hlock = ldiskfs_htree_lock_alloc();
5282 if (info->oti_hlock == NULL)
5288 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5291 return ERR_PTR(-ENOMEM);
5294 static void osd_key_fini(const struct lu_context *ctx,
5295 struct lu_context_key *key, void* data)
5297 struct osd_thread_info *info = data;
5299 if (info->oti_hlock != NULL)
5300 ldiskfs_htree_lock_free(info->oti_hlock);
5301 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5302 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5303 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5307 static void osd_key_exit(const struct lu_context *ctx,
5308 struct lu_context_key *key, void *data)
5310 struct osd_thread_info *info = data;
5312 LASSERT(info->oti_r_locks == 0);
5313 LASSERT(info->oti_w_locks == 0);
5314 LASSERT(info->oti_txns == 0);
5317 /* type constructor/destructor: osd_type_init, osd_type_fini */
5318 LU_TYPE_INIT_FINI(osd, &osd_key);
5320 struct lu_context_key osd_key = {
5321 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5322 .lct_init = osd_key_init,
5323 .lct_fini = osd_key_fini,
5324 .lct_exit = osd_key_exit
5328 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5329 const char *name, struct lu_device *next)
5331 struct osd_device *osd = osd_dev(d);
5333 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5334 >= sizeof(osd->od_svname))
5336 return osd_procfs_init(osd, name);
5339 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5343 /* shutdown quota slave instance associated with the device */
5344 if (o->od_quota_slave != NULL) {
5345 qsd_fini(env, o->od_quota_slave);
5346 o->od_quota_slave = NULL;
5352 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5357 fsfilt_put_ops(o->od_fsops);
5361 if (o->od_mnt != NULL) {
5362 shrink_dcache_sb(osd_sb(o));
5363 osd_sync(env, &o->od_dt_dev);
5372 static int osd_mount(const struct lu_env *env,
5373 struct osd_device *o, struct lustre_cfg *cfg)
5375 const char *name = lustre_cfg_string(cfg, 0);
5376 const char *dev = lustre_cfg_string(cfg, 1);
5378 unsigned long page, s_flags, lmd_flags = 0;
5379 struct page *__page;
5380 struct file_system_type *type;
5381 char *options = NULL;
5383 struct osd_thread_info *info = osd_oti_get(env);
5384 struct lu_fid *fid = &info->oti_fid;
5385 struct inode *inode;
5389 if (o->od_mnt != NULL)
5392 if (strlen(dev) >= sizeof(o->od_mntdev))
5394 strcpy(o->od_mntdev, dev);
5396 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5397 if (IS_ERR(o->od_fsops)) {
5398 CERROR("%s: Can't find fsfilt_ldiskfs\n", name);
5403 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5405 GOTO(out, rc = -ENOMEM);
5407 str = lustre_cfg_string(cfg, 2);
5408 s_flags = simple_strtoul(str, NULL, 0);
5409 str = strstr(str, ":");
5411 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5412 opts = lustre_cfg_string(cfg, 3);
5413 page = (unsigned long)page_address(__page);
5414 options = (char *)page;
5417 strcat(options, "user_xattr,acl");
5419 strcat(options, opts);
5421 /* Glom up mount options */
5422 if (*options != '\0')
5423 strcat(options, ",");
5424 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5426 type = get_fs_type("ldiskfs");
5428 CERROR("%s: cannot find ldiskfs module\n", name);
5429 GOTO(out, rc = -ENODEV);
5432 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5433 cfs_module_put(type->owner);
5435 if (IS_ERR(o->od_mnt)) {
5436 rc = PTR_ERR(o->od_mnt);
5438 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5442 #ifdef HAVE_DEV_SET_RDONLY
5443 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5444 CERROR("%s: underlying device %s is marked as read-only. "
5445 "Setup failed\n", name, dev);
5446 GOTO(out_mnt, rc = -EROFS);
5450 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5451 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5452 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5453 GOTO(out_mnt, rc = -EINVAL);
5456 inode = osd_sb(o)->s_root->d_inode;
5457 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5458 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5459 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5461 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5465 if (lmd_flags & LMD_FLG_NOSCRUB)
5476 OBD_PAGE_FREE(__page);
5478 fsfilt_put_ops(o->od_fsops);
5483 static struct lu_device *osd_device_fini(const struct lu_env *env,
5484 struct lu_device *d)
5486 struct osd_device *o = osd_dev(d);
5490 osd_shutdown(env, o);
5491 osd_scrub_cleanup(env, o);
5492 osd_obj_map_fini(o);
5498 static int osd_device_init0(const struct lu_env *env,
5499 struct osd_device *o,
5500 struct lustre_cfg *cfg)
5502 struct lu_device *l = osd2lu_dev(o);
5503 struct osd_thread_info *info;
5507 /* if the module was re-loaded, env can loose its keys */
5508 rc = lu_env_refill((struct lu_env *) env);
5511 info = osd_oti_get(env);
5514 l->ld_ops = &osd_lu_ops;
5515 o->od_dt_dev.dd_ops = &osd_dt_ops;
5517 spin_lock_init(&o->od_osfs_lock);
5518 mutex_init(&o->od_otable_mutex);
5519 o->od_osfs_age = cfs_time_shift_64(-1000);
5521 o->od_capa_hash = init_capa_hash();
5522 if (o->od_capa_hash == NULL)
5523 GOTO(out, rc = -ENOMEM);
5525 o->od_read_cache = 1;
5526 o->od_writethrough_cache = 1;
5527 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5529 rc = osd_mount(env, o, cfg);
5533 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5534 sizeof(o->od_svname));
5535 if (cplen >= sizeof(o->od_svname)) {
5540 rc = osd_obj_map_init(env, o);
5544 rc = lu_site_init(&o->od_site, l);
5546 GOTO(out_compat, rc);
5547 o->od_site.ls_bottom_dev = l;
5549 rc = lu_site_init_finish(&o->od_site);
5553 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5554 /* setup scrub, including OI files initialization */
5555 rc = osd_scrub_setup(env, o);
5559 rc = osd_procfs_init(o, o->od_svname);
5561 CERROR("%s: can't initialize procfs: rc = %d\n",
5563 GOTO(out_scrub, rc);
5566 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5568 /* initialize quota slave instance */
5569 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5571 if (IS_ERR(o->od_quota_slave)) {
5572 rc = PTR_ERR(o->od_quota_slave);
5573 o->od_quota_slave = NULL;
5574 GOTO(out_procfs, rc);
5582 osd_scrub_cleanup(env, o);
5584 lu_site_fini(&o->od_site);
5586 osd_obj_map_fini(o);
5590 cleanup_capa_hash(o->od_capa_hash);
5595 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5596 struct lu_device_type *t,
5597 struct lustre_cfg *cfg)
5599 struct osd_device *o;
5604 return ERR_PTR(-ENOMEM);
5606 rc = dt_device_init(&o->od_dt_dev, t);
5608 /* Because the ctx might be revived in dt_device_init,
5609 * refill the env here */
5610 lu_env_refill((struct lu_env *)env);
5611 rc = osd_device_init0(env, o, cfg);
5613 dt_device_fini(&o->od_dt_dev);
5616 if (unlikely(rc != 0))
5619 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5622 static struct lu_device *osd_device_free(const struct lu_env *env,
5623 struct lu_device *d)
5625 struct osd_device *o = osd_dev(d);
5628 cleanup_capa_hash(o->od_capa_hash);
5629 /* XXX: make osd top device in order to release reference */
5630 d->ld_site->ls_top_dev = d;
5631 lu_site_purge(env, d->ld_site, -1);
5632 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5633 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5634 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5636 lu_site_fini(&o->od_site);
5637 dt_device_fini(&o->od_dt_dev);
5642 static int osd_process_config(const struct lu_env *env,
5643 struct lu_device *d, struct lustre_cfg *cfg)
5645 struct osd_device *o = osd_dev(d);
5649 switch(cfg->lcfg_command) {
5651 err = osd_mount(env, o, cfg);
5654 lu_dev_del_linkage(d->ld_site, d);
5655 err = osd_shutdown(env, o);
5664 static int osd_recovery_complete(const struct lu_env *env,
5665 struct lu_device *d)
5667 struct osd_device *osd = osd_dev(d);
5671 if (osd->od_quota_slave == NULL)
5674 /* start qsd instance on recovery completion, this notifies the quota
5675 * slave code that we are about to process new requests now */
5676 rc = qsd_start(env, osd->od_quota_slave);
5681 * we use exports to track all osd users
5683 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5684 struct obd_device *obd, struct obd_uuid *cluuid,
5685 struct obd_connect_data *data, void *localdata)
5687 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5688 struct lustre_handle conn;
5692 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5694 rc = class_connect(&conn, obd, cluuid);
5698 *exp = class_conn2export(&conn);
5700 spin_lock(&osd->od_osfs_lock);
5702 spin_unlock(&osd->od_osfs_lock);
5708 * once last export (we don't count self-export) disappeared
5709 * osd can be released
5711 static int osd_obd_disconnect(struct obd_export *exp)
5713 struct obd_device *obd = exp->exp_obd;
5714 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5715 int rc, release = 0;
5718 /* Only disconnect the underlying layers on the final disconnect. */
5719 spin_lock(&osd->od_osfs_lock);
5721 if (osd->od_connects == 0)
5723 spin_unlock(&osd->od_osfs_lock);
5725 rc = class_disconnect(exp); /* bz 9811 */
5727 if (rc == 0 && release)
5728 class_manual_cleanup(obd);
5732 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5733 struct lu_device *dev)
5735 struct osd_device *osd = osd_dev(dev);
5739 if (osd->od_quota_slave != NULL)
5740 /* set up quota slave objects */
5741 result = qsd_prepare(env, osd->od_quota_slave);
5746 static const struct lu_object_operations osd_lu_obj_ops = {
5747 .loo_object_init = osd_object_init,
5748 .loo_object_delete = osd_object_delete,
5749 .loo_object_release = osd_object_release,
5750 .loo_object_free = osd_object_free,
5751 .loo_object_print = osd_object_print,
5752 .loo_object_invariant = osd_object_invariant
5755 const struct lu_device_operations osd_lu_ops = {
5756 .ldo_object_alloc = osd_object_alloc,
5757 .ldo_process_config = osd_process_config,
5758 .ldo_recovery_complete = osd_recovery_complete,
5759 .ldo_prepare = osd_prepare,
5762 static const struct lu_device_type_operations osd_device_type_ops = {
5763 .ldto_init = osd_type_init,
5764 .ldto_fini = osd_type_fini,
5766 .ldto_start = osd_type_start,
5767 .ldto_stop = osd_type_stop,
5769 .ldto_device_alloc = osd_device_alloc,
5770 .ldto_device_free = osd_device_free,
5772 .ldto_device_init = osd_device_init,
5773 .ldto_device_fini = osd_device_fini
5776 struct lu_device_type osd_device_type = {
5777 .ldt_tags = LU_DEVICE_DT,
5778 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5779 .ldt_ops = &osd_device_type_ops,
5780 .ldt_ctx_tags = LCT_LOCAL,
5784 * lprocfs legacy support.
5786 static struct obd_ops osd_obd_device_ops = {
5787 .o_owner = THIS_MODULE,
5788 .o_connect = osd_obd_connect,
5789 .o_disconnect = osd_obd_disconnect
5792 static int __init osd_mod_init(void)
5794 struct lprocfs_static_vars lvars;
5797 lprocfs_osd_init_vars(&lvars);
5798 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5799 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5802 static void __exit osd_mod_exit(void)
5804 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5807 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5808 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5809 MODULE_LICENSE("GPL");
5811 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);