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"
67 #include "osd_dynlocks.h"
69 /* llo_* api support */
70 #include <md_object.h>
71 #include <lustre_quota.h>
74 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
75 "ldiskfs with parallel directory operations");
77 int ldiskfs_track_declares_assert;
78 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
79 "LBUG during tracking of declares");
81 /* Slab to allocate dynlocks */
82 struct kmem_cache *dynlock_cachep;
84 static struct lu_kmem_descr ldiskfs_caches[] = {
86 .ckd_cache = &dynlock_cachep,
87 .ckd_name = "dynlock_cache",
88 .ckd_size = sizeof(struct dynlock_handle)
95 static const char dot[] = ".";
96 static const char dotdot[] = "..";
97 static const char remote_obj_dir[] = "REM_OBJ_DIR";
99 static const struct lu_object_operations osd_lu_obj_ops;
100 static const struct dt_object_operations osd_obj_ops;
101 static const struct dt_object_operations osd_obj_ea_ops;
102 static const struct dt_object_operations osd_obj_otable_it_ops;
103 static const struct dt_index_operations osd_index_iam_ops;
104 static const struct dt_index_operations osd_index_ea_ops;
106 int osd_trans_declare_op2rb[] = {
107 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
108 [OSD_OT_PUNCH] = OSD_OT_MAX,
109 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
110 [OSD_OT_CREATE] = OSD_OT_DESTROY,
111 [OSD_OT_DESTROY] = OSD_OT_CREATE,
112 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
113 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
114 [OSD_OT_WRITE] = OSD_OT_WRITE,
115 [OSD_OT_INSERT] = OSD_OT_DELETE,
116 [OSD_OT_DELETE] = OSD_OT_INSERT,
117 [OSD_OT_UPDATE] = OSD_OT_MAX,
118 [OSD_OT_QUOTA] = OSD_OT_MAX,
121 static int osd_has_index(const struct osd_object *obj)
123 return obj->oo_dt.do_index_ops != NULL;
126 static int osd_object_invariant(const struct lu_object *l)
128 return osd_invariant(osd_obj(l));
132 * Concurrency: doesn't matter
134 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
136 return osd_oti_get(env)->oti_r_locks > 0;
140 * Concurrency: doesn't matter
142 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
144 struct osd_thread_info *oti = osd_oti_get(env);
145 return oti->oti_w_locks > 0 && o->oo_owner == env;
149 * Concurrency: doesn't access mutable data
151 static int osd_root_get(const struct lu_env *env,
152 struct dt_device *dev, struct lu_fid *f)
154 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
159 * OSD object methods.
163 * Concurrency: no concurrent access is possible that early in object
166 static struct lu_object *osd_object_alloc(const struct lu_env *env,
167 const struct lu_object_header *hdr,
170 struct osd_object *mo;
176 l = &mo->oo_dt.do_lu;
177 dt_object_init(&mo->oo_dt, NULL, d);
178 mo->oo_dt.do_ops = &osd_obj_ea_ops;
179 l->lo_ops = &osd_lu_obj_ops;
180 init_rwsem(&mo->oo_sem);
181 init_rwsem(&mo->oo_ext_idx_sem);
182 spin_lock_init(&mo->oo_guard);
189 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
190 struct dentry *dentry, struct lustre_mdt_attrs *lma)
194 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
195 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
196 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
198 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
199 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
201 lustre_lma_swab(lma);
202 /* Check LMA compatibility */
203 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
204 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
205 "for fid = "DFID", ino = %lu\n",
206 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
207 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
208 PFID(&lma->lma_self_fid), inode->i_ino);
211 } else if (rc == 0) {
219 * retrieve object from backend ext fs.
221 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
222 struct osd_inode_id *id)
224 struct inode *inode = NULL;
226 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
228 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
229 id->oii_ino, PTR_ERR(inode));
230 } else if (id->oii_gen != OSD_OII_NOGEN &&
231 inode->i_generation != id->oii_gen) {
232 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
234 id->oii_ino, id->oii_gen, inode->i_generation);
236 inode = ERR_PTR(-ESTALE);
237 } else if (inode->i_nlink == 0) {
238 /* due to parallel readdir and unlink,
239 * we can have dead inode here. */
240 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
241 make_bad_inode(inode);
243 inode = ERR_PTR(-ESTALE);
244 } else if (is_bad_inode(inode)) {
245 CWARN("%.16s: bad inode: ino = %u\n",
246 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
248 inode = ERR_PTR(-ENOENT);
250 if (id->oii_gen == OSD_OII_NOGEN)
251 osd_id_gen(id, inode->i_ino, inode->i_generation);
253 /* Do not update file c/mtime in ldiskfs.
254 * NB: we don't have any lock to protect this because we don't
255 * have reference on osd_object now, but contention with
256 * another lookup + attr_set can't happen in the tiny window
257 * between if (...) and set S_NOCMTIME. */
258 if (!(inode->i_flags & S_NOCMTIME))
259 inode->i_flags |= S_NOCMTIME;
264 static struct inode *
265 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
266 struct osd_inode_id *id, struct lu_fid *fid)
268 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
272 inode = osd_iget(info, dev, id);
276 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
278 *fid = lma->lma_self_fid;
279 } else if (rc == -ENODATA) {
280 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
281 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
283 lu_igif_build(fid, inode->i_ino, inode->i_generation);
292 * \retval +v: new filter_fid, does not contain self-fid
293 * \retval 0: filter_fid_old, contains self-fid
294 * \retval -v: other failure cases
296 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
297 struct dentry *dentry, struct lu_fid *fid)
299 struct filter_fid_old *ff = &info->oti_ff;
300 struct ost_id *ostid = &info->oti_ostid;
303 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
304 if (rc == sizeof(*ff)) {
306 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
307 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
308 /* XXX: should use real OST index in the future. LU-3569 */
309 ostid_to_fid(fid, ostid, 0);
310 } else if (rc == sizeof(struct filter_fid)) {
312 } else if (rc >= 0) {
319 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
321 struct osd_thread_info *info = osd_oti_get(env);
322 struct osd_device *osd = osd_obj2dev(obj);
323 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
324 struct inode *inode = obj->oo_inode;
325 struct dentry *dentry = &info->oti_obj_dentry;
326 struct lu_fid *fid = NULL;
330 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
333 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
334 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
335 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
336 if (rc == -ENODATA && !fid_is_igif(lu_object_fid(&obj->oo_dt.do_lu)) &&
338 fid = &lma->lma_self_fid;
339 rc = osd_get_idif(info, inode, dentry, fid);
340 if ((rc > 0) || (rc == -ENODATA && osd->od_lma_self_repair)) {
343 /* For the given OST-object, if it has neither LMA nor
344 * FID in XATTR_NAME_FID, then the given FID (which is
345 * contained in the @obj, from client RPC for locating
346 * the OST-object) is trusted. We use it to generate
349 LASSERT(current->journal_info == NULL);
351 jh = ldiskfs_journal_start_sb(osd_sb(osd),
352 osd_dto_credits_noquota[DTO_XATTR_SET]);
354 CWARN("%s: cannot start journal for "
355 "lma_self_repair: rc = %ld\n",
356 osd_name(osd), PTR_ERR(jh));
360 rc = osd_ea_fid_set(info, inode,
361 lu_object_fid(&obj->oo_dt.do_lu),
362 fid_is_on_ost(info, osd,
363 lu_object_fid(&obj->oo_dt.do_lu),
365 LMAC_FID_ON_OST : 0, 0);
367 CWARN("%s: cannot self repair the LMA: "
368 "rc = %d\n", osd_name(osd), rc);
369 ldiskfs_journal_stop(jh);
374 if (unlikely(rc == -ENODATA))
382 lustre_lma_swab(lma);
383 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
384 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
385 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
386 "fid = "DFID", ino = %lu\n", osd_name(osd),
387 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
388 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
391 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
392 fid = &lma->lma_self_fid;
397 unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
398 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
399 osd_name(osd), PFID(lu_object_fid(&obj->oo_dt.do_lu)),
400 PFID(&lma->lma_self_fid));
407 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
408 const struct lu_fid *fid,
409 const struct lu_object_conf *conf)
411 struct osd_thread_info *info;
412 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
413 struct osd_device *dev;
414 struct osd_idmap_cache *oic;
415 struct osd_inode_id *id;
417 struct osd_scrub *scrub;
418 struct scrub_file *sf;
422 bool triggered = false;
425 LINVRNT(osd_invariant(obj));
426 LASSERT(obj->oo_inode == NULL);
427 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
430 scrub = &dev->od_scrub;
431 sf = &scrub->os_file;
432 info = osd_oti_get(env);
434 oic = &info->oti_cache;
436 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
439 /* For the object is created as locking anchor, or for the object to
440 * be created on disk. No need to osd_oi_lookup() at here because FID
441 * shouldn't never be re-used, if it's really a duplicate FID from
442 * unexpected reason, we should be able to detect it later by calling
443 * do_create->osd_oi_insert(). */
444 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
445 GOTO(out, result = 0);
447 /* Search order: 1. per-thread cache. */
448 if (lu_fid_eq(fid, &oic->oic_fid) &&
449 likely(oic->oic_dev == dev)) {
455 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
456 /* Search order: 2. OI scrub pending list. */
457 result = osd_oii_lookup(dev, fid, id);
462 /* Search order: 3. OI files. */
463 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
464 if (result == -ENOENT) {
465 if (!fid_is_norm(fid) ||
466 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
467 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
469 GOTO(out, result = 0);
480 inode = osd_iget(info, dev, id);
482 result = PTR_ERR(inode);
483 if (result == -ENOENT || result == -ESTALE) {
485 fid_zero(&oic->oic_fid);
486 GOTO(out, result = -ENOENT);
489 /* XXX: There are three possible cases:
490 * 1. Backup/restore caused the OI invalid.
491 * 2. Someone unlinked the object but NOT removed
492 * the OI mapping, such as mount target device
493 * as ldiskfs, and modify something directly.
494 * 3. Someone just removed the object between the
495 * former oi_lookup and the iget. It is normal.
497 * It is diffcult to distinguish the 2nd from the
498 * 1st case. Relatively speaking, the 1st case is
499 * common than the 2nd case, trigger OI scrub. */
500 result = osd_oi_lookup(info, dev, fid, id, true);
502 /* It is the case 1 or 2. */
504 } else if (result == -EREMCHG) {
507 if (unlikely(triggered))
508 GOTO(out, result = saved);
511 if (thread_is_running(&scrub->os_thread)) {
512 result = -EINPROGRESS;
513 } else if (!dev->od_noscrub) {
514 result = osd_scrub_start(dev);
515 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
516 "for "DFID", rc = %d [1]\n",
517 LDISKFS_SB(osd_sb(dev))->s_es->\
518 s_volume_name,PFID(fid), result);
519 if (result == 0 || result == -EALREADY)
520 result = -EINPROGRESS;
525 /* We still have chance to get the valid inode: for the
526 * object which is referenced by remote name entry, the
527 * object on the local MDT will be linked under the dir
528 * of "/REMOTE_PARENT_DIR" with its FID string as name.
530 * We do not know whether the object for the given FID
531 * is referenced by some remote name entry or not, and
532 * especially for DNE II, a multiple-linked object may
533 * have many name entries reside on many MDTs.
535 * To simplify the operation, OSD will not distinguish
536 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
537 * only happened for the RPC from other MDT during the
538 * OI scrub, or for the client side RPC with FID only,
539 * such as FID to path, or from old connected client. */
541 result = osd_lookup_in_remote_parent(info, dev,
554 obj->oo_inode = inode;
555 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
557 result = osd_check_lma(env, obj);
560 obj->oo_inode = NULL;
561 if (result == -EREMCHG)
567 obj->oo_compat_dot_created = 1;
568 obj->oo_compat_dotdot_created = 1;
570 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
571 GOTO(out, result = 0);
573 LASSERT(obj->oo_hl_head == NULL);
574 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
575 if (obj->oo_hl_head == NULL) {
576 obj->oo_inode = NULL;
578 GOTO(out, result = -ENOMEM);
580 GOTO(out, result = 0);
583 LINVRNT(osd_invariant(obj));
588 * Concurrency: shouldn't matter.
590 static void osd_object_init0(struct osd_object *obj)
592 LASSERT(obj->oo_inode != NULL);
593 obj->oo_dt.do_body_ops = &osd_body_ops;
594 obj->oo_dt.do_lu.lo_header->loh_attr |=
595 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
599 * Concurrency: no concurrent access is possible that early in object
602 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
603 const struct lu_object_conf *conf)
605 struct osd_object *obj = osd_obj(l);
608 LINVRNT(osd_invariant(obj));
610 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
611 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
612 l->lo_header->loh_attr |= LOHA_EXISTS;
616 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
617 obj->oo_dt.do_body_ops = &osd_body_ops_new;
618 if (result == 0 && obj->oo_inode != NULL)
619 osd_object_init0(obj);
621 LINVRNT(osd_invariant(obj));
626 * Concurrency: no concurrent access is possible that late in object
629 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
631 struct osd_object *obj = osd_obj(l);
633 LINVRNT(osd_invariant(obj));
635 dt_object_fini(&obj->oo_dt);
636 if (obj->oo_hl_head != NULL)
637 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
642 * Concurrency: no concurrent access is possible that late in object
645 static void osd_index_fini(struct osd_object *o)
647 struct iam_container *bag;
649 if (o->oo_dir != NULL) {
650 bag = &o->oo_dir->od_container;
651 if (o->oo_inode != NULL) {
652 if (bag->ic_object == o->oo_inode)
653 iam_container_fini(bag);
655 OBD_FREE_PTR(o->oo_dir);
661 * Concurrency: no concurrent access is possible that late in object
662 * life-cycle (for all existing callers, that is. New callers have to provide
663 * their own locking.)
665 static int osd_inode_unlinked(const struct inode *inode)
667 return inode->i_nlink == 0;
671 OSD_TXN_OI_DELETE_CREDITS = 20,
672 OSD_TXN_INODE_DELETE_CREDITS = 20
679 #if OSD_THANDLE_STATS
681 * Set time when the handle is allocated
683 static void osd_th_alloced(struct osd_thandle *oth)
685 oth->oth_alloced = cfs_time_current();
689 * Set time when the handle started
691 static void osd_th_started(struct osd_thandle *oth)
693 oth->oth_started = cfs_time_current();
697 * Helper function to convert time interval to microseconds packed in
700 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
704 cfs_duration_usec(cfs_time_sub(end, start), &val);
705 return val.tv_sec * 1000000 + val.tv_usec;
709 * Check whether the we deal with this handle for too long.
711 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
712 cfs_time_t alloced, cfs_time_t started,
715 cfs_time_t now = cfs_time_current();
717 LASSERT(dev != NULL);
719 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
720 interval_to_usec(alloced, started));
721 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
722 interval_to_usec(started, closed));
723 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
724 interval_to_usec(closed, now));
726 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
727 CWARN("transaction handle %p was open for too long: "
729 "alloced "CFS_TIME_T" ,"
730 "started "CFS_TIME_T" ,"
731 "closed "CFS_TIME_T"\n",
732 oth, now, alloced, started, closed);
733 libcfs_debug_dumpstack(NULL);
737 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
739 cfs_time_t __closed = cfs_time_current(); \
740 cfs_time_t __alloced = oth->oth_alloced; \
741 cfs_time_t __started = oth->oth_started; \
744 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
747 #else /* OSD_THANDLE_STATS */
749 #define osd_th_alloced(h) do {} while(0)
750 #define osd_th_started(h) do {} while(0)
751 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
753 #endif /* OSD_THANDLE_STATS */
756 * Concurrency: doesn't access mutable data.
758 static int osd_param_is_not_sane(const struct osd_device *dev,
759 const struct thandle *th)
761 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
763 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
767 * Concurrency: shouldn't matter.
769 static void osd_trans_commit_cb(struct super_block *sb,
770 struct ldiskfs_journal_cb_entry *jcb, int error)
772 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
773 struct thandle *th = &oh->ot_super;
774 struct lu_device *lud = &th->th_dev->dd_lu_dev;
775 struct dt_txn_commit_cb *dcb, *tmp;
777 LASSERT(oh->ot_handle == NULL);
780 CERROR("transaction @0x%p commit error: %d\n", th, error);
782 dt_txn_hook_commit(th);
784 /* call per-transaction callbacks if any */
785 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
786 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
787 "commit callback entry: magic=%x name='%s'\n",
788 dcb->dcb_magic, dcb->dcb_name);
789 cfs_list_del_init(&dcb->dcb_linkage);
790 dcb->dcb_func(NULL, th, dcb, error);
793 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
797 lu_context_exit(&th->th_ctx);
798 lu_context_fini(&th->th_ctx);
802 static struct thandle *osd_trans_create(const struct lu_env *env,
805 struct osd_thread_info *oti = osd_oti_get(env);
806 struct osd_iobuf *iobuf = &oti->oti_iobuf;
807 struct osd_thandle *oh;
811 /* on pending IO in this thread should left from prev. request */
812 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
814 th = ERR_PTR(-ENOMEM);
815 OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
817 oh->ot_quota_trans = &oti->oti_quota_trans;
818 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
822 th->th_tags = LCT_TX_HANDLE;
824 oti->oti_dev = osd_dt_dev(d);
825 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
828 memset(oti->oti_declare_ops, 0,
829 sizeof(oti->oti_declare_ops));
830 memset(oti->oti_declare_ops_rb, 0,
831 sizeof(oti->oti_declare_ops_rb));
832 memset(oti->oti_declare_ops_cred, 0,
833 sizeof(oti->oti_declare_ops_cred));
834 oti->oti_rollback = false;
840 * Concurrency: shouldn't matter.
842 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
845 struct osd_thread_info *oti = osd_oti_get(env);
846 struct osd_device *dev = osd_dt_dev(d);
848 struct osd_thandle *oh;
853 LASSERT(current->journal_info == NULL);
855 oh = container_of0(th, struct osd_thandle, ot_super);
857 LASSERT(oh->ot_handle == NULL);
859 rc = dt_txn_hook_start(env, d, th);
863 if (unlikely(osd_param_is_not_sane(dev, th))) {
864 static unsigned long last_printed;
865 static int last_credits;
867 CWARN("%.16s: too many transaction credits (%d > %d)\n",
868 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
870 osd_journal(dev)->j_max_transaction_buffers);
871 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
872 oti->oti_declare_ops[OSD_OT_CREATE],
873 oti->oti_declare_ops_cred[OSD_OT_CREATE],
874 oti->oti_declare_ops[OSD_OT_DELETE],
875 oti->oti_declare_ops_cred[OSD_OT_DELETE],
876 oti->oti_declare_ops[OSD_OT_DESTROY],
877 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
878 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
879 oti->oti_declare_ops[OSD_OT_ATTR_SET],
880 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
881 oti->oti_declare_ops[OSD_OT_XATTR_SET],
882 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
883 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
884 oti->oti_declare_ops[OSD_OT_WRITE],
885 oti->oti_declare_ops_cred[OSD_OT_WRITE],
886 oti->oti_declare_ops[OSD_OT_PUNCH],
887 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
888 oti->oti_declare_ops[OSD_OT_QUOTA],
889 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
890 CWARN(" insert: %u/%u, delete: %u/%u\n",
891 oti->oti_declare_ops[OSD_OT_INSERT],
892 oti->oti_declare_ops_cred[OSD_OT_INSERT],
893 oti->oti_declare_ops[OSD_OT_DESTROY],
894 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
895 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
896 oti->oti_declare_ops[OSD_OT_REF_ADD],
897 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
898 oti->oti_declare_ops[OSD_OT_REF_DEL],
899 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
901 if (last_credits != oh->ot_credits &&
902 time_after(jiffies, last_printed + 60 * HZ)) {
903 libcfs_debug_dumpstack(NULL);
904 last_credits = oh->ot_credits;
905 last_printed = jiffies;
907 /* XXX Limit the credits to 'max_transaction_buffers', and
908 * let the underlying filesystem to catch the error if
909 * we really need so many credits.
911 * This should be removed when we can calculate the
912 * credits precisely. */
913 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
917 * XXX temporary stuff. Some abstraction layer should
920 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
924 LASSERT(oti->oti_txns == 0);
925 lu_context_init(&th->th_ctx, th->th_tags);
926 lu_context_enter(&th->th_ctx);
928 lu_device_get(&d->dd_lu_dev);
929 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
941 * Concurrency: shouldn't matter.
943 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
946 struct osd_thandle *oh;
947 struct osd_thread_info *oti = osd_oti_get(env);
948 struct osd_iobuf *iobuf = &oti->oti_iobuf;
949 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
952 oh = container_of0(th, struct osd_thandle, ot_super);
955 /* inform the quota slave device that the transaction is
957 qsd_op_end(env, qsd, oh->ot_quota_trans);
958 oh->ot_quota_trans = NULL;
960 if (oh->ot_handle != NULL) {
961 handle_t *hdl = oh->ot_handle;
964 * add commit callback
965 * notice we don't do this in osd_trans_start()
966 * as underlying transaction can change during truncate
968 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
971 LASSERT(oti->oti_txns == 1);
973 rc = dt_txn_hook_stop(env, th);
975 CERROR("Failure in transaction hook: %d\n", rc);
977 /* hook functions might modify th_sync */
978 hdl->h_sync = th->th_sync;
980 oh->ot_handle = NULL;
981 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
982 rc = ldiskfs_journal_stop(hdl));
984 CERROR("Failure to stop transaction: %d\n", rc);
989 /* as we want IO to journal and data IO be concurrent, we don't block
990 * awaiting data IO completion in osd_do_bio(), instead we wait here
991 * once transaction is submitted to the journal. all reqular requests
992 * don't do direct IO (except read/write), thus this wait_event becomes
995 * IMPORTANT: we have to wait till any IO submited by the thread is
996 * completed otherwise iobuf may be corrupted by different request
998 wait_event(iobuf->dr_wait,
999 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
1001 rc = iobuf->dr_error;
1006 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1008 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1011 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1012 LASSERT(&dcb->dcb_func != NULL);
1013 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
1019 * Called just before object is freed. Releases all resources except for
1020 * object itself (that is released by osd_object_free()).
1022 * Concurrency: no concurrent access is possible that late in object
1025 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1027 struct osd_object *obj = osd_obj(l);
1028 struct inode *inode = obj->oo_inode;
1030 LINVRNT(osd_invariant(obj));
1033 * If object is unlinked remove fid->ino mapping from object index.
1036 osd_index_fini(obj);
1037 if (inode != NULL) {
1038 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1039 qid_t uid = inode->i_uid;
1040 qid_t gid = inode->i_gid;
1043 obj->oo_inode = NULL;
1046 struct osd_thread_info *info = osd_oti_get(env);
1047 struct lquota_id_info *qi = &info->oti_qi;
1049 /* Release granted quota to master if necessary */
1050 qi->lqi_id.qid_uid = uid;
1051 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1053 qi->lqi_id.qid_uid = gid;
1054 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1060 * Concurrency: ->loo_object_release() is called under site spin-lock.
1062 static void osd_object_release(const struct lu_env *env,
1063 struct lu_object *l)
1068 * Concurrency: shouldn't matter.
1070 static int osd_object_print(const struct lu_env *env, void *cookie,
1071 lu_printer_t p, const struct lu_object *l)
1073 struct osd_object *o = osd_obj(l);
1074 struct iam_descr *d;
1076 if (o->oo_dir != NULL)
1077 d = o->oo_dir->od_container.ic_descr;
1080 return (*p)(env, cookie,
1081 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1083 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1084 o->oo_inode ? o->oo_inode->i_generation : 0,
1085 d ? d->id_ops->id_name : "plain");
1089 * Concurrency: shouldn't matter.
1091 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1092 struct obd_statfs *sfs)
1094 struct osd_device *osd = osd_dt_dev(d);
1095 struct super_block *sb = osd_sb(osd);
1096 struct kstatfs *ksfs;
1099 if (unlikely(osd->od_mnt == NULL))
1100 return -EINPROGRESS;
1102 /* osd_lproc.c call this without env, allocate ksfs for that case */
1103 if (unlikely(env == NULL)) {
1104 OBD_ALLOC_PTR(ksfs);
1108 ksfs = &osd_oti_get(env)->oti_ksfs;
1111 spin_lock(&osd->od_osfs_lock);
1112 /* cache 1 second */
1113 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1114 result = sb->s_op->statfs(sb->s_root, ksfs);
1115 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1116 osd->od_osfs_age = cfs_time_current_64();
1117 statfs_pack(&osd->od_statfs, ksfs);
1118 if (sb->s_flags & MS_RDONLY)
1119 sfs->os_state = OS_STATE_READONLY;
1123 if (likely(result == 0))
1124 *sfs = osd->od_statfs;
1125 spin_unlock(&osd->od_osfs_lock);
1127 if (unlikely(env == NULL))
1134 * Estimate space needed for file creations. We assume the largest filename
1135 * which is 2^64 - 1, hence a filename of 20 chars.
1136 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1138 #ifdef __LDISKFS_DIR_REC_LEN
1139 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1141 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1145 * Concurrency: doesn't access mutable data.
1147 static void osd_conf_get(const struct lu_env *env,
1148 const struct dt_device *dev,
1149 struct dt_device_param *param)
1151 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1154 * XXX should be taken from not-yet-existing fs abstraction layer.
1156 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1157 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1158 param->ddp_block_shift = sb->s_blocksize_bits;
1159 param->ddp_mount_type = LDD_MT_LDISKFS;
1160 param->ddp_maxbytes = sb->s_maxbytes;
1161 /* Overhead estimate should be fairly accurate, so we really take a tiny
1162 * error margin which also avoids fragmenting the filesystem too much */
1163 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1164 /* inode are statically allocated, so per-inode space consumption
1165 * is the space consumed by the directory entry */
1166 param->ddp_inodespace = PER_OBJ_USAGE;
1167 /* per-fragment overhead to be used by the client code */
1168 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1169 param->ddp_mntopts = 0;
1170 if (test_opt(sb, XATTR_USER))
1171 param->ddp_mntopts |= MNTOPT_USERXATTR;
1172 if (test_opt(sb, POSIX_ACL))
1173 param->ddp_mntopts |= MNTOPT_ACL;
1175 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1176 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1177 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1180 param->ddp_max_ea_size = sb->s_blocksize;
1185 * Concurrency: shouldn't matter.
1187 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1189 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1190 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1194 * Start commit for OSD device.
1196 * An implementation of dt_commit_async method for OSD device.
1197 * Asychronously starts underlayng fs sync and thereby a transaction
1200 * \param env environment
1201 * \param d dt device
1203 * \see dt_device_operations
1205 static int osd_commit_async(const struct lu_env *env,
1206 struct dt_device *d)
1208 struct super_block *s = osd_sb(osd_dt_dev(d));
1211 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1212 RETURN(s->s_op->sync_fs(s, 0));
1216 * Concurrency: shouldn't matter.
1219 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1221 struct super_block *sb = osd_sb(osd_dt_dev(d));
1222 struct block_device *dev = sb->s_bdev;
1223 #ifdef HAVE_DEV_SET_RDONLY
1224 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1227 int rc = -EOPNOTSUPP;
1231 #ifdef HAVE_DEV_SET_RDONLY
1232 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1234 if (jdev && (jdev != dev)) {
1235 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1237 dev_set_rdonly(jdev);
1239 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1240 dev_set_rdonly(dev);
1242 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1243 osd_dt_dev(d)->od_svname, (long)dev, rc);
1249 * Concurrency: serialization provided by callers.
1251 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1252 int mode, unsigned long timeout, __u32 alg,
1253 struct lustre_capa_key *keys)
1255 struct osd_device *dev = osd_dt_dev(d);
1258 dev->od_fl_capa = mode;
1259 dev->od_capa_timeout = timeout;
1260 dev->od_capa_alg = alg;
1261 dev->od_capa_keys = keys;
1266 * Note: we do not count into QUOTA here.
1267 * If we mount with --data_journal we may need more.
1269 const int osd_dto_credits_noquota[DTO_NR] = {
1272 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1273 * SINGLEDATA_TRANS_BLOCKS(8)
1274 * XXX Note: maybe iam need more, since iam have more level than
1277 [DTO_INDEX_INSERT] = 16,
1278 [DTO_INDEX_DELETE] = 16,
1282 [DTO_INDEX_UPDATE] = 16,
1284 * Create a object. The same as create object in EXT3.
1285 * DATA_TRANS_BLOCKS(14) +
1286 * INDEX_EXTRA_BLOCKS(8) +
1287 * 3(inode bits, groups, GDT)
1289 [DTO_OBJECT_CREATE] = 25,
1291 * XXX: real credits to be fixed
1293 [DTO_OBJECT_DELETE] = 25,
1295 * Attr set credits (inode)
1297 [DTO_ATTR_SET_BASE] = 1,
1299 * Xattr set. The same as xattr of EXT3.
1300 * DATA_TRANS_BLOCKS(14)
1301 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1302 * are also counted in. Do not know why?
1304 [DTO_XATTR_SET] = 14,
1307 * credits for inode change during write.
1309 [DTO_WRITE_BASE] = 3,
1311 * credits for single block write.
1313 [DTO_WRITE_BLOCK] = 14,
1315 * Attr set credits for chown.
1316 * This is extra credits for setattr, and it is null without quota
1318 [DTO_ATTR_SET_CHOWN]= 0
1321 static const struct dt_device_operations osd_dt_ops = {
1322 .dt_root_get = osd_root_get,
1323 .dt_statfs = osd_statfs,
1324 .dt_trans_create = osd_trans_create,
1325 .dt_trans_start = osd_trans_start,
1326 .dt_trans_stop = osd_trans_stop,
1327 .dt_trans_cb_add = osd_trans_cb_add,
1328 .dt_conf_get = osd_conf_get,
1329 .dt_sync = osd_sync,
1331 .dt_commit_async = osd_commit_async,
1332 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1335 static void osd_object_read_lock(const struct lu_env *env,
1336 struct dt_object *dt, unsigned role)
1338 struct osd_object *obj = osd_dt_obj(dt);
1339 struct osd_thread_info *oti = osd_oti_get(env);
1341 LINVRNT(osd_invariant(obj));
1343 LASSERT(obj->oo_owner != env);
1344 down_read_nested(&obj->oo_sem, role);
1346 LASSERT(obj->oo_owner == NULL);
1350 static void osd_object_write_lock(const struct lu_env *env,
1351 struct dt_object *dt, unsigned role)
1353 struct osd_object *obj = osd_dt_obj(dt);
1354 struct osd_thread_info *oti = osd_oti_get(env);
1356 LINVRNT(osd_invariant(obj));
1358 LASSERT(obj->oo_owner != env);
1359 down_write_nested(&obj->oo_sem, role);
1361 LASSERT(obj->oo_owner == NULL);
1362 obj->oo_owner = env;
1366 static void osd_object_read_unlock(const struct lu_env *env,
1367 struct dt_object *dt)
1369 struct osd_object *obj = osd_dt_obj(dt);
1370 struct osd_thread_info *oti = osd_oti_get(env);
1372 LINVRNT(osd_invariant(obj));
1374 LASSERT(oti->oti_r_locks > 0);
1376 up_read(&obj->oo_sem);
1379 static void osd_object_write_unlock(const struct lu_env *env,
1380 struct dt_object *dt)
1382 struct osd_object *obj = osd_dt_obj(dt);
1383 struct osd_thread_info *oti = osd_oti_get(env);
1385 LINVRNT(osd_invariant(obj));
1387 LASSERT(obj->oo_owner == env);
1388 LASSERT(oti->oti_w_locks > 0);
1390 obj->oo_owner = NULL;
1391 up_write(&obj->oo_sem);
1394 static int osd_object_write_locked(const struct lu_env *env,
1395 struct dt_object *dt)
1397 struct osd_object *obj = osd_dt_obj(dt);
1399 LINVRNT(osd_invariant(obj));
1401 return obj->oo_owner == env;
1404 static int capa_is_sane(const struct lu_env *env,
1405 struct osd_device *dev,
1406 struct lustre_capa *capa,
1407 struct lustre_capa_key *keys)
1409 struct osd_thread_info *oti = osd_oti_get(env);
1410 struct lustre_capa *tcapa = &oti->oti_capa;
1411 struct obd_capa *oc;
1415 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1417 if (capa_is_expired(oc)) {
1418 DEBUG_CAPA(D_ERROR, capa, "expired");
1425 if (capa_is_expired_sec(capa)) {
1426 DEBUG_CAPA(D_ERROR, capa, "expired");
1430 spin_lock(&capa_lock);
1431 for (i = 0; i < 2; i++) {
1432 if (keys[i].lk_keyid == capa->lc_keyid) {
1433 oti->oti_capa_key = keys[i];
1437 spin_unlock(&capa_lock);
1440 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1444 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1448 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1449 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1453 oc = capa_add(dev->od_capa_hash, capa);
1459 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1460 struct lustre_capa *capa, __u64 opc)
1462 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1463 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1464 struct lu_capainfo *lci;
1467 if (!osd->od_fl_capa)
1470 if (capa == BYPASS_CAPA)
1473 lci = lu_capainfo_get(env);
1474 if (unlikely(lci == NULL))
1477 if (lci->lci_auth == LC_ID_NONE)
1481 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1482 osd_name(osd), PFID(fid), -EACCES);
1486 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1487 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1492 if (!capa_opc_supported(capa, opc)) {
1493 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1497 rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1499 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1506 static struct timespec *osd_inode_time(const struct lu_env *env,
1507 struct inode *inode, __u64 seconds)
1509 struct osd_thread_info *oti = osd_oti_get(env);
1510 struct timespec *t = &oti->oti_time;
1512 t->tv_sec = seconds;
1514 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1519 static void osd_inode_getattr(const struct lu_env *env,
1520 struct inode *inode, struct lu_attr *attr)
1522 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1523 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1524 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1527 attr->la_atime = LTIME_S(inode->i_atime);
1528 attr->la_mtime = LTIME_S(inode->i_mtime);
1529 attr->la_ctime = LTIME_S(inode->i_ctime);
1530 attr->la_mode = inode->i_mode;
1531 attr->la_size = i_size_read(inode);
1532 attr->la_blocks = inode->i_blocks;
1533 attr->la_uid = inode->i_uid;
1534 attr->la_gid = inode->i_gid;
1535 attr->la_flags = LDISKFS_I(inode)->i_flags;
1536 attr->la_nlink = inode->i_nlink;
1537 attr->la_rdev = inode->i_rdev;
1538 attr->la_blksize = 1 << inode->i_blkbits;
1539 attr->la_blkbits = inode->i_blkbits;
1542 static int osd_attr_get(const struct lu_env *env,
1543 struct dt_object *dt,
1544 struct lu_attr *attr,
1545 struct lustre_capa *capa)
1547 struct osd_object *obj = osd_dt_obj(dt);
1549 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1550 LINVRNT(osd_invariant(obj));
1552 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1555 spin_lock(&obj->oo_guard);
1556 osd_inode_getattr(env, obj->oo_inode, attr);
1557 spin_unlock(&obj->oo_guard);
1561 static int osd_declare_attr_set(const struct lu_env *env,
1562 struct dt_object *dt,
1563 const struct lu_attr *attr,
1564 struct thandle *handle)
1566 struct osd_thandle *oh;
1567 struct osd_object *obj;
1568 struct osd_thread_info *info = osd_oti_get(env);
1569 struct lquota_id_info *qi = &info->oti_qi;
1575 LASSERT(dt != NULL);
1576 LASSERT(handle != NULL);
1578 obj = osd_dt_obj(dt);
1579 LASSERT(osd_invariant(obj));
1581 oh = container_of0(handle, struct osd_thandle, ot_super);
1582 LASSERT(oh->ot_handle == NULL);
1584 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1585 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1587 if (attr == NULL || obj->oo_inode == NULL)
1590 bspace = obj->oo_inode->i_blocks;
1591 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1592 bspace = toqb(bspace);
1594 /* Changing ownership is always preformed by super user, it should not
1597 * We still need to call the osd_declare_qid() to calculate the journal
1598 * credits for updating quota accounting files and to trigger quota
1599 * space adjustment once the operation is completed.*/
1600 if ((attr->la_valid & LA_UID) != 0 &&
1601 attr->la_uid != obj->oo_inode->i_uid) {
1602 qi->lqi_type = USRQUOTA;
1604 /* inode accounting */
1605 qi->lqi_is_blk = false;
1607 /* one more inode for the new owner ... */
1608 qi->lqi_id.qid_uid = attr->la_uid;
1610 allocated = (attr->la_uid == 0) ? true : false;
1611 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1612 if (rc == -EDQUOT || rc == -EINPROGRESS)
1617 /* and one less inode for the current uid */
1618 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1620 rc = osd_declare_qid(env, oh, qi, true, NULL);
1621 if (rc == -EDQUOT || rc == -EINPROGRESS)
1626 /* block accounting */
1627 qi->lqi_is_blk = true;
1629 /* more blocks for the new owner ... */
1630 qi->lqi_id.qid_uid = attr->la_uid;
1631 qi->lqi_space = bspace;
1632 allocated = (attr->la_uid == 0) ? true : false;
1633 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1634 if (rc == -EDQUOT || rc == -EINPROGRESS)
1639 /* and finally less blocks for the current owner */
1640 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1641 qi->lqi_space = -bspace;
1642 rc = osd_declare_qid(env, oh, qi, true, NULL);
1643 if (rc == -EDQUOT || rc == -EINPROGRESS)
1649 if (attr->la_valid & LA_GID &&
1650 attr->la_gid != obj->oo_inode->i_gid) {
1651 qi->lqi_type = GRPQUOTA;
1653 /* inode accounting */
1654 qi->lqi_is_blk = false;
1656 /* one more inode for the new group owner ... */
1657 qi->lqi_id.qid_gid = attr->la_gid;
1659 allocated = (attr->la_gid == 0) ? true : false;
1660 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1661 if (rc == -EDQUOT || rc == -EINPROGRESS)
1666 /* and one less inode for the current gid */
1667 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1669 rc = osd_declare_qid(env, oh, qi, true, NULL);
1670 if (rc == -EDQUOT || rc == -EINPROGRESS)
1675 /* block accounting */
1676 qi->lqi_is_blk = true;
1678 /* more blocks for the new owner ... */
1679 qi->lqi_id.qid_gid = attr->la_gid;
1680 qi->lqi_space = bspace;
1681 allocated = (attr->la_gid == 0) ? true : false;
1682 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1683 if (rc == -EDQUOT || rc == -EINPROGRESS)
1688 /* and finally less blocks for the current owner */
1689 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1690 qi->lqi_space = -bspace;
1691 rc = osd_declare_qid(env, oh, qi, true, NULL);
1692 if (rc == -EDQUOT || rc == -EINPROGRESS)
1701 static int osd_inode_setattr(const struct lu_env *env,
1702 struct inode *inode, const struct lu_attr *attr)
1706 bits = attr->la_valid;
1708 if (bits & LA_ATIME)
1709 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1710 if (bits & LA_CTIME)
1711 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1712 if (bits & LA_MTIME)
1713 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1714 if (bits & LA_SIZE) {
1715 LDISKFS_I(inode)->i_disksize = attr->la_size;
1716 i_size_write(inode, attr->la_size);
1720 /* OSD should not change "i_blocks" which is used by quota.
1721 * "i_blocks" should be changed by ldiskfs only. */
1722 if (bits & LA_BLOCKS)
1723 inode->i_blocks = attr->la_blocks;
1726 inode->i_mode = (inode->i_mode & S_IFMT) |
1727 (attr->la_mode & ~S_IFMT);
1729 inode->i_uid = attr->la_uid;
1731 inode->i_gid = attr->la_gid;
1732 if (bits & LA_NLINK)
1733 set_nlink(inode, attr->la_nlink);
1735 inode->i_rdev = attr->la_rdev;
1737 if (bits & LA_FLAGS) {
1738 /* always keep S_NOCMTIME */
1739 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1745 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1747 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1748 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1753 if (attr->la_valid & LA_UID)
1754 iattr.ia_valid |= ATTR_UID;
1755 if (attr->la_valid & LA_GID)
1756 iattr.ia_valid |= ATTR_GID;
1757 iattr.ia_uid = attr->la_uid;
1758 iattr.ia_gid = attr->la_gid;
1760 rc = ll_vfs_dq_transfer(inode, &iattr);
1762 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1763 "enforcement enabled on the ldiskfs filesystem?",
1764 inode->i_sb->s_id, rc);
1771 static int osd_attr_set(const struct lu_env *env,
1772 struct dt_object *dt,
1773 const struct lu_attr *attr,
1774 struct thandle *handle,
1775 struct lustre_capa *capa)
1777 struct osd_object *obj = osd_dt_obj(dt);
1778 struct inode *inode;
1781 LASSERT(handle != NULL);
1782 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1783 LASSERT(osd_invariant(obj));
1785 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1788 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1790 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1791 struct osd_thread_info *oti = osd_oti_get(env);
1792 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1793 struct lu_fid *fid1 = &oti->oti_fid;
1794 struct osd_inode_id *id = &oti->oti_id;
1795 struct iam_path_descr *ipd;
1796 struct iam_container *bag;
1797 struct osd_thandle *oh;
1800 fid_cpu_to_be(fid1, fid0);
1801 memset(id, 1, sizeof(*id));
1802 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1803 fid0)->oi_dir.od_container;
1804 ipd = osd_idx_ipd_get(env, bag);
1805 if (unlikely(ipd == NULL))
1808 oh = container_of0(handle, struct osd_thandle, ot_super);
1809 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1810 (const struct iam_rec *)id, ipd);
1811 osd_ipd_put(env, bag, ipd);
1812 return(rc > 0 ? 0 : rc);
1815 inode = obj->oo_inode;
1816 ll_vfs_dq_init(inode);
1818 rc = osd_quota_transfer(inode, attr);
1822 spin_lock(&obj->oo_guard);
1823 rc = osd_inode_setattr(env, inode, attr);
1824 spin_unlock(&obj->oo_guard);
1827 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1831 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1832 struct osd_object *obj,
1833 const char *name, const int namelen)
1835 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1838 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1839 umode_t mode, struct dt_allocation_hint *hint,
1843 struct osd_device *osd = osd_obj2dev(obj);
1844 struct osd_thandle *oth;
1845 struct dt_object *parent = NULL;
1846 struct inode *inode;
1848 LINVRNT(osd_invariant(obj));
1849 LASSERT(obj->oo_inode == NULL);
1850 LASSERT(obj->oo_hl_head == NULL);
1852 if (S_ISDIR(mode) && ldiskfs_pdo) {
1853 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1854 if (obj->oo_hl_head == NULL)
1858 oth = container_of(th, struct osd_thandle, ot_super);
1859 LASSERT(oth->ot_handle->h_transaction != NULL);
1861 if (hint && hint->dah_parent)
1862 parent = hint->dah_parent;
1864 inode = ldiskfs_create_inode(oth->ot_handle,
1865 parent ? osd_dt_obj(parent)->oo_inode :
1866 osd_sb(osd)->s_root->d_inode,
1868 if (!IS_ERR(inode)) {
1869 /* Do not update file c/mtime in ldiskfs.
1870 * NB: don't need any lock because no contention at this
1872 inode->i_flags |= S_NOCMTIME;
1874 /* For new created object, it must be consistent,
1875 * and it is unnecessary to scrub against it. */
1876 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1877 obj->oo_inode = inode;
1880 if (obj->oo_hl_head != NULL) {
1881 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1882 obj->oo_hl_head = NULL;
1884 result = PTR_ERR(inode);
1886 LINVRNT(osd_invariant(obj));
1894 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1895 struct lu_attr *attr,
1896 struct dt_allocation_hint *hint,
1897 struct dt_object_format *dof,
1901 struct osd_thandle *oth;
1902 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1904 LASSERT(S_ISDIR(attr->la_mode));
1906 oth = container_of(th, struct osd_thandle, ot_super);
1907 LASSERT(oth->ot_handle->h_transaction != NULL);
1908 result = osd_mkfile(info, obj, mode, hint, th);
1913 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1914 struct lu_attr *attr,
1915 struct dt_allocation_hint *hint,
1916 struct dt_object_format *dof,
1920 struct osd_thandle *oth;
1921 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1923 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1925 LASSERT(S_ISREG(attr->la_mode));
1927 oth = container_of(th, struct osd_thandle, ot_super);
1928 LASSERT(oth->ot_handle->h_transaction != NULL);
1930 result = osd_mkfile(info, obj, mode, hint, th);
1932 LASSERT(obj->oo_inode != NULL);
1933 if (feat->dif_flags & DT_IND_VARKEY)
1934 result = iam_lvar_create(obj->oo_inode,
1935 feat->dif_keysize_max,
1937 feat->dif_recsize_max,
1940 result = iam_lfix_create(obj->oo_inode,
1941 feat->dif_keysize_max,
1943 feat->dif_recsize_max,
1950 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1951 struct lu_attr *attr,
1952 struct dt_allocation_hint *hint,
1953 struct dt_object_format *dof,
1956 LASSERT(S_ISREG(attr->la_mode));
1957 return osd_mkfile(info, obj, (attr->la_mode &
1958 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1961 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1962 struct lu_attr *attr,
1963 struct dt_allocation_hint *hint,
1964 struct dt_object_format *dof,
1967 LASSERT(S_ISLNK(attr->la_mode));
1968 return osd_mkfile(info, obj, (attr->la_mode &
1969 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1972 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1973 struct lu_attr *attr,
1974 struct dt_allocation_hint *hint,
1975 struct dt_object_format *dof,
1978 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1981 LINVRNT(osd_invariant(obj));
1982 LASSERT(obj->oo_inode == NULL);
1983 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1984 S_ISFIFO(mode) || S_ISSOCK(mode));
1986 result = osd_mkfile(info, obj, mode, hint, th);
1988 LASSERT(obj->oo_inode != NULL);
1990 * This inode should be marked dirty for i_rdev. Currently
1991 * that is done in the osd_attr_init().
1993 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1996 LINVRNT(osd_invariant(obj));
2000 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2002 struct dt_allocation_hint *hint,
2003 struct dt_object_format *dof,
2006 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2008 osd_obj_type_f result;
2024 result = osd_mk_index;
2035 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2036 struct dt_object *parent, struct dt_object *child,
2041 memset(ah, 0, sizeof(*ah));
2042 ah->dah_parent = parent;
2043 ah->dah_mode = child_mode;
2046 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2047 struct lu_attr *attr, struct dt_object_format *dof)
2049 struct inode *inode = obj->oo_inode;
2050 __u64 valid = attr->la_valid;
2053 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2055 if (dof->dof_type != DFT_NODE)
2056 attr->la_valid &= ~LA_RDEV;
2057 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2058 attr->la_valid &= ~LA_ATIME;
2059 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2060 attr->la_valid &= ~LA_CTIME;
2061 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2062 attr->la_valid &= ~LA_MTIME;
2064 result = osd_quota_transfer(inode, attr);
2068 if (attr->la_valid != 0) {
2069 result = osd_inode_setattr(info->oti_env, inode, attr);
2071 * The osd_inode_setattr() should always succeed here. The
2072 * only error that could be returned is EDQUOT when we are
2073 * trying to change the UID or GID of the inode. However, this
2074 * should not happen since quota enforcement is no longer
2075 * enabled on ldiskfs (lquota takes care of it).
2077 LASSERTF(result == 0, "%d", result);
2078 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2081 attr->la_valid = valid;
2085 * Helper function for osd_object_create()
2087 * \retval 0, on success
2089 static int __osd_object_create(struct osd_thread_info *info,
2090 struct osd_object *obj, struct lu_attr *attr,
2091 struct dt_allocation_hint *hint,
2092 struct dt_object_format *dof,
2098 /* we drop umask so that permissions we pass are not affected */
2099 umask = current->fs->umask;
2100 current->fs->umask = 0;
2102 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2105 osd_attr_init(info, obj, attr, dof);
2106 osd_object_init0(obj);
2108 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2109 unlock_new_inode(obj->oo_inode);
2112 /* restore previous umask value */
2113 current->fs->umask = umask;
2119 * Helper function for osd_object_create()
2121 * \retval 0, on success
2123 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2124 const struct lu_fid *fid, struct thandle *th)
2126 struct osd_thread_info *info = osd_oti_get(env);
2127 struct osd_inode_id *id = &info->oti_id;
2128 struct osd_device *osd = osd_obj2dev(obj);
2130 LASSERT(obj->oo_inode != NULL);
2132 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2133 return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2136 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2137 obd_seq seq, struct lu_seq_range *range)
2139 struct seq_server_site *ss = osd_seq_site(osd);
2142 if (fid_seq_is_idif(seq)) {
2143 fld_range_set_ost(range);
2144 range->lsr_index = idif_ost_idx(seq);
2148 if (!fid_seq_in_fldb(seq)) {
2149 fld_range_set_mdt(range);
2151 /* FIXME: If ss is NULL, it suppose not get lsr_index
2153 range->lsr_index = ss->ss_node_id;
2157 LASSERT(ss != NULL);
2158 fld_range_set_any(range);
2159 rc = fld_server_lookup(env, ss->ss_server_fld, seq, range);
2161 CERROR("%s: cannot find FLD range for "LPX64": rc = %d\n",
2162 osd_name(osd), seq, rc);
2168 * Concurrency: no external locking is necessary.
2170 static int osd_declare_object_create(const struct lu_env *env,
2171 struct dt_object *dt,
2172 struct lu_attr *attr,
2173 struct dt_allocation_hint *hint,
2174 struct dt_object_format *dof,
2175 struct thandle *handle)
2177 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2178 struct osd_thandle *oh;
2182 LASSERT(handle != NULL);
2184 oh = container_of0(handle, struct osd_thandle, ot_super);
2185 LASSERT(oh->ot_handle == NULL);
2187 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2188 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2189 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2190 lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2191 /* Reuse idle OI block may cause additional one OI block
2193 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2194 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2196 /* If this is directory, then we expect . and .. to be inserted as
2197 * well. The one directory block always needs to be created for the
2198 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2199 * block), there is no danger of needing a tree for the first block.
2201 if (attr && S_ISDIR(attr->la_mode)) {
2202 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2203 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2204 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2210 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2211 false, false, NULL, false);
2215 /* It does fld look up inside declare, and the result will be
2216 * added to fld cache, so the following fld lookup inside insert
2217 * does not need send RPC anymore, so avoid send rpc with holding
2219 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2220 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2221 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2222 fid_seq(lu_object_fid(&dt->do_lu)), range);
2228 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2229 struct lu_attr *attr,
2230 struct dt_allocation_hint *hint,
2231 struct dt_object_format *dof,
2234 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2235 struct osd_object *obj = osd_dt_obj(dt);
2236 struct osd_thread_info *info = osd_oti_get(env);
2241 LINVRNT(osd_invariant(obj));
2242 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2243 LASSERT(osd_write_locked(env, obj));
2244 LASSERT(th != NULL);
2246 if (unlikely(fid_is_acct(fid)))
2247 /* Quota files can't be created from the kernel any more,
2248 * 'tune2fs -O quota' will take care of creating them */
2251 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2252 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2254 result = __osd_object_create(info, obj, attr, hint, dof, th);
2256 result = __osd_oi_insert(env, obj, fid, th);
2258 LASSERT(ergo(result == 0,
2259 dt_object_exists(dt) && !dt_object_remote(dt)));
2261 LASSERT(osd_invariant(obj));
2266 * Called to destroy on-disk representation of the object
2268 * Concurrency: must be locked
2270 static int osd_declare_object_destroy(const struct lu_env *env,
2271 struct dt_object *dt,
2274 struct osd_object *obj = osd_dt_obj(dt);
2275 struct inode *inode = obj->oo_inode;
2276 struct osd_thandle *oh;
2280 oh = container_of0(th, struct osd_thandle, ot_super);
2281 LASSERT(oh->ot_handle == NULL);
2284 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2285 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2286 /* Recycle idle OI leaf may cause additional three OI blocks
2288 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2289 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2290 /* one less inode */
2291 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2292 false, true, NULL, false);
2295 /* data to be truncated */
2296 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2297 true, true, NULL, false);
2301 static int osd_object_destroy(const struct lu_env *env,
2302 struct dt_object *dt,
2305 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2306 struct osd_object *obj = osd_dt_obj(dt);
2307 struct inode *inode = obj->oo_inode;
2308 struct osd_device *osd = osd_obj2dev(obj);
2309 struct osd_thandle *oh;
2313 oh = container_of0(th, struct osd_thandle, ot_super);
2314 LASSERT(oh->ot_handle);
2316 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2318 if (unlikely(fid_is_acct(fid)))
2321 if (S_ISDIR(inode->i_mode)) {
2322 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2323 /* it will check/delete the inode from remote parent,
2324 * how to optimize it? unlink performance impaction XXX */
2325 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2326 if (result != 0 && result != -ENOENT) {
2327 CERROR("%s: delete inode "DFID": rc = %d\n",
2328 osd_name(osd), PFID(fid), result);
2330 spin_lock(&obj->oo_guard);
2332 spin_unlock(&obj->oo_guard);
2333 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2336 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2338 result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2340 /* XXX: add to ext3 orphan list */
2341 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2343 /* not needed in the cache anymore */
2344 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2350 * Put the fid into lustre_mdt_attrs, and then place the structure
2351 * inode's ea. This fid should not be altered during the life time
2354 * \retval +ve, on success
2355 * \retval -ve, on error
2357 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2359 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2360 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2362 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2366 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2369 lustre_lma_init(lma, fid, compat, incompat);
2370 lustre_lma_swab(lma);
2372 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2374 /* LMA may already exist, but we need to check that all the
2375 * desired compat/incompat flags have been added. */
2376 if (unlikely(rc == -EEXIST)) {
2377 if (compat == 0 && incompat == 0)
2380 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2381 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2386 lustre_lma_swab(lma);
2387 if (!(~lma->lma_compat & compat) &&
2388 !(~lma->lma_incompat & incompat))
2391 lma->lma_compat |= compat;
2392 lma->lma_incompat |= incompat;
2393 lustre_lma_swab(lma);
2394 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2395 sizeof(*lma), XATTR_REPLACE);
2402 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2403 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2404 * To have compatilibility with 1.8 ldiskfs driver we need to have
2405 * magic number at start of fid data.
2406 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2409 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2410 const struct dt_rec *fid)
2412 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2413 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2414 param->edp_magic = 0;
2418 param->edp_magic = LDISKFS_LUFID_MAGIC;
2419 param->edp_len = sizeof(struct lu_fid) + 1;
2420 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2424 * Try to read the fid from inode ea into dt_rec.
2426 * \param fid object fid.
2428 * \retval 0 on success
2430 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2431 __u32 ino, struct lu_fid *fid,
2432 struct osd_inode_id *id)
2434 struct osd_thread_info *info = osd_oti_get(env);
2435 struct inode *inode;
2438 osd_id_gen(id, ino, OSD_OII_NOGEN);
2439 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2441 RETURN(PTR_ERR(inode));
2447 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2449 struct inode *parent_dir,
2450 const struct dt_rec *dot_fid,
2451 const struct dt_rec *dot_dot_fid,
2452 struct osd_thandle *oth)
2454 struct ldiskfs_dentry_param *dot_ldp;
2455 struct ldiskfs_dentry_param *dot_dot_ldp;
2457 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2458 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2460 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2461 dot_ldp->edp_magic = 0;
2462 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2463 dir, dot_ldp, dot_dot_ldp);
2467 * Create an local agent inode for remote entry
2469 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2470 struct osd_device *osd,
2471 struct osd_object *pobj,
2472 const struct lu_fid *fid,
2475 struct osd_thread_info *info = osd_oti_get(env);
2476 struct inode *local;
2477 struct osd_thandle *oh;
2482 oh = container_of(th, struct osd_thandle, ot_super);
2483 LASSERT(oh->ot_handle->h_transaction != NULL);
2485 /* FIXME: Insert index api needs to know the mode of
2486 * the remote object. Just use S_IFDIR for now */
2487 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2488 if (IS_ERR(local)) {
2489 CERROR("%s: create local error %d\n", osd_name(osd),
2490 (int)PTR_ERR(local));
2494 /* Set special LMA flag for local agent inode */
2495 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2497 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2498 osd_name(osd), PFID(fid), rc);
2499 RETURN(ERR_PTR(rc));
2502 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2503 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2504 (const struct dt_rec *)fid, oh);
2506 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2507 osd_name(osd), PFID(fid), rc);
2508 RETURN(ERR_PTR(rc));
2515 * Delete local agent inode for remote entry
2517 static int osd_delete_local_agent_inode(const struct lu_env *env,
2518 struct osd_device *osd,
2519 const struct lu_fid *fid,
2520 __u32 ino, struct osd_thandle *oh)
2522 struct osd_thread_info *oti = osd_oti_get(env);
2523 struct osd_inode_id *id = &oti->oti_id;
2524 struct inode *inode;
2527 id->oii_ino = le32_to_cpu(ino);
2528 id->oii_gen = OSD_OII_NOGEN;
2529 inode = osd_iget(oti, osd, id);
2530 if (IS_ERR(inode)) {
2531 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2532 PFID(fid), id->oii_ino, id->oii_gen);
2533 RETURN(PTR_ERR(inode));
2537 mark_inode_dirty(inode);
2538 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2539 osd_name(osd), PFID(fid), inode->i_ino);
2545 * OSD layer object create function for interoperability mode (b11826).
2546 * This is mostly similar to osd_object_create(). Only difference being, fid is
2547 * inserted into inode ea here.
2549 * \retval 0, on success
2550 * \retval -ve, on error
2552 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2553 struct lu_attr *attr,
2554 struct dt_allocation_hint *hint,
2555 struct dt_object_format *dof,
2558 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2559 struct osd_object *obj = osd_dt_obj(dt);
2560 struct osd_thread_info *info = osd_oti_get(env);
2565 LASSERT(osd_invariant(obj));
2566 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2567 LASSERT(osd_write_locked(env, obj));
2568 LASSERT(th != NULL);
2570 if (unlikely(fid_is_acct(fid)))
2571 /* Quota files can't be created from the kernel any more,
2572 * 'tune2fs -O quota' will take care of creating them */
2575 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2576 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2578 result = __osd_object_create(info, obj, attr, hint, dof, th);
2580 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2581 fid_is_on_ost(info, osd_obj2dev(obj),
2582 fid, OI_CHECK_FLD) ?
2583 LMAC_FID_ON_OST : 0, 0);
2586 result = __osd_oi_insert(env, obj, fid, th);
2588 LASSERT(ergo(result == 0,
2589 dt_object_exists(dt) && !dt_object_remote(dt)));
2590 LINVRNT(osd_invariant(obj));
2594 static int osd_declare_object_ref_add(const struct lu_env *env,
2595 struct dt_object *dt,
2596 struct thandle *handle)
2598 struct osd_thandle *oh;
2600 /* it's possible that object doesn't exist yet */
2601 LASSERT(handle != NULL);
2603 oh = container_of0(handle, struct osd_thandle, ot_super);
2604 LASSERT(oh->ot_handle == NULL);
2606 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2607 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2613 * Concurrency: @dt is write locked.
2615 static int osd_object_ref_add(const struct lu_env *env,
2616 struct dt_object *dt, struct thandle *th)
2618 struct osd_object *obj = osd_dt_obj(dt);
2619 struct inode *inode = obj->oo_inode;
2620 bool need_dirty = false;
2623 LINVRNT(osd_invariant(obj));
2624 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2625 LASSERT(osd_write_locked(env, obj));
2626 LASSERT(th != NULL);
2628 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2630 /* This based on ldiskfs_inc_count(), which is not exported.
2632 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2633 * (65000) subdirectories by storing "1" in i_nlink if the link count
2634 * would otherwise overflow. Directory tranversal tools understand
2635 * that (st_nlink == 1) indicates that the filesystem dose not track
2636 * hard links count on the directory, and will not abort subdirectory
2637 * scanning early once (st_nlink - 2) subdirs have been found.
2639 * This also has to properly handle the case of inodes with nlink == 0
2640 * in case they are being linked into the PENDING directory
2642 spin_lock(&obj->oo_guard);
2643 if (unlikely(!S_ISDIR(inode->i_mode) &&
2644 inode->i_nlink >= LDISKFS_LINK_MAX)) {
2645 /* MDD should have checked this, but good to be safe */
2647 } else if (unlikely(inode->i_nlink == 0 ||
2648 (S_ISDIR(inode->i_mode) &&
2649 inode->i_nlink >= LDISKFS_LINK_MAX))) {
2650 /* inc_nlink from 0 may cause WARN_ON */
2651 set_nlink(inode, 1);
2653 } else if (!S_ISDIR(inode->i_mode) ||
2654 (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2657 } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2659 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2660 spin_unlock(&obj->oo_guard);
2663 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2665 LINVRNT(osd_invariant(obj));
2670 static int osd_declare_object_ref_del(const struct lu_env *env,
2671 struct dt_object *dt,
2672 struct thandle *handle)
2674 struct osd_thandle *oh;
2676 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2677 LASSERT(handle != NULL);
2679 oh = container_of0(handle, struct osd_thandle, ot_super);
2680 LASSERT(oh->ot_handle == NULL);
2682 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2683 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2689 * Concurrency: @dt is write locked.
2691 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2694 struct osd_object *obj = osd_dt_obj(dt);
2695 struct inode *inode = obj->oo_inode;
2696 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2698 LINVRNT(osd_invariant(obj));
2699 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2700 LASSERT(osd_write_locked(env, obj));
2701 LASSERT(th != NULL);
2703 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2705 spin_lock(&obj->oo_guard);
2706 /* That can be result of upgrade from old Lustre version and
2707 * applied only to local files. Just skip this ref_del call.
2708 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2709 if (inode->i_nlink == 0) {
2710 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2711 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2712 ", maybe an upgraded file? (LU-3915)\n",
2713 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2714 spin_unlock(&obj->oo_guard);
2718 /* This based on ldiskfs_dec_count(), which is not exported.
2720 * If a directory already has nlink == 1, then do not drop the nlink
2721 * count to 0, even temporarily, to avoid race conditions with other
2722 * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2724 * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2726 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2729 spin_unlock(&obj->oo_guard);
2730 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2731 LINVRNT(osd_invariant(obj));
2733 spin_unlock(&obj->oo_guard);
2740 * Get the 64-bit version for an inode.
2742 static int osd_object_version_get(const struct lu_env *env,
2743 struct dt_object *dt, dt_obj_version_t *ver)
2745 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2747 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2748 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2749 *ver = LDISKFS_I(inode)->i_fs_version;
2754 * Concurrency: @dt is read locked.
2756 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2757 struct lu_buf *buf, const char *name,
2758 struct lustre_capa *capa)
2760 struct osd_object *obj = osd_dt_obj(dt);
2761 struct inode *inode = obj->oo_inode;
2762 struct osd_thread_info *info = osd_oti_get(env);
2763 struct dentry *dentry = &info->oti_obj_dentry;
2765 /* version get is not real XATTR but uses xattr API */
2766 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2767 /* for version we are just using xattr API but change inode
2769 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2770 osd_object_version_get(env, dt, buf->lb_buf);
2771 return sizeof(dt_obj_version_t);
2774 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2775 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2777 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2780 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2784 static int osd_declare_xattr_set(const struct lu_env *env,
2785 struct dt_object *dt,
2786 const struct lu_buf *buf, const char *name,
2787 int fl, struct thandle *handle)
2789 struct osd_thandle *oh;
2791 LASSERT(handle != NULL);
2793 oh = container_of0(handle, struct osd_thandle, ot_super);
2794 LASSERT(oh->ot_handle == NULL);
2796 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2797 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2798 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2799 osd_dto_credits_noquota[DTO_XATTR_SET]);
2805 * Set the 64-bit version for object
2807 static void osd_object_version_set(const struct lu_env *env,
2808 struct dt_object *dt,
2809 dt_obj_version_t *new_version)
2811 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2813 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2814 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2816 LDISKFS_I(inode)->i_fs_version = *new_version;
2817 /** Version is set after all inode operations are finished,
2818 * so we should mark it dirty here */
2819 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2823 * Concurrency: @dt is write locked.
2825 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2826 const struct lu_buf *buf, const char *name, int fl,
2827 struct thandle *handle, struct lustre_capa *capa)
2829 struct osd_object *obj = osd_dt_obj(dt);
2830 struct inode *inode = obj->oo_inode;
2831 struct osd_thread_info *info = osd_oti_get(env);
2835 LASSERT(handle != NULL);
2837 /* version set is not real XATTR */
2838 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2839 /* for version we are just using xattr API but change inode
2841 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2842 osd_object_version_set(env, dt, buf->lb_buf);
2843 return sizeof(dt_obj_version_t);
2846 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2849 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2850 if (fl & LU_XATTR_REPLACE)
2851 fs_flags |= XATTR_REPLACE;
2853 if (fl & LU_XATTR_CREATE)
2854 fs_flags |= XATTR_CREATE;
2856 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2861 * Concurrency: @dt is read locked.
2863 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2864 struct lu_buf *buf, struct lustre_capa *capa)
2866 struct osd_object *obj = osd_dt_obj(dt);
2867 struct inode *inode = obj->oo_inode;
2868 struct osd_thread_info *info = osd_oti_get(env);
2869 struct dentry *dentry = &info->oti_obj_dentry;
2871 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2872 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2873 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2875 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2878 dentry->d_inode = inode;
2879 dentry->d_sb = inode->i_sb;
2880 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2883 static int osd_declare_xattr_del(const struct lu_env *env,
2884 struct dt_object *dt, const char *name,
2885 struct thandle *handle)
2887 struct osd_thandle *oh;
2889 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2890 LASSERT(handle != NULL);
2892 oh = container_of0(handle, struct osd_thandle, ot_super);
2893 LASSERT(oh->ot_handle == NULL);
2895 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2896 osd_dto_credits_noquota[DTO_XATTR_SET]);
2902 * Concurrency: @dt is write locked.
2904 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2905 const char *name, struct thandle *handle,
2906 struct lustre_capa *capa)
2908 struct osd_object *obj = osd_dt_obj(dt);
2909 struct inode *inode = obj->oo_inode;
2910 struct osd_thread_info *info = osd_oti_get(env);
2911 struct dentry *dentry = &info->oti_obj_dentry;
2914 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2915 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2916 LASSERT(handle != NULL);
2918 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2921 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2923 ll_vfs_dq_init(inode);
2924 dentry->d_inode = inode;
2925 dentry->d_sb = inode->i_sb;
2926 rc = inode->i_op->removexattr(dentry, name);
2930 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2931 struct dt_object *dt,
2932 struct lustre_capa *old, __u64 opc)
2934 struct osd_thread_info *info = osd_oti_get(env);
2935 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2936 struct osd_object *obj = osd_dt_obj(dt);
2937 struct osd_device *osd = osd_obj2dev(obj);
2938 struct lustre_capa_key *key = &info->oti_capa_key;
2939 struct lustre_capa *capa = &info->oti_capa;
2940 struct obd_capa *oc;
2941 struct lu_capainfo *lci;
2945 if (!osd->od_fl_capa)
2946 RETURN(ERR_PTR(-ENOENT));
2948 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2949 LINVRNT(osd_invariant(obj));
2951 /* renewal sanity check */
2952 if (old && osd_object_auth(env, dt, old, opc))
2953 RETURN(ERR_PTR(-EACCES));
2955 lci = lu_capainfo_get(env);
2956 if (unlikely(lci == NULL))
2957 RETURN(ERR_PTR(-ENOENT));
2959 switch (lci->lci_auth) {
2963 capa->lc_uid = obj->oo_inode->i_uid;
2964 capa->lc_gid = obj->oo_inode->i_gid;
2965 capa->lc_flags = LC_ID_PLAIN;
2967 case LC_ID_CONVERT: {
2970 s[0] = obj->oo_inode->i_uid;
2971 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2972 s[2] = obj->oo_inode->i_gid;
2973 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2974 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2976 RETURN(ERR_PTR(rc));
2978 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2979 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2980 capa->lc_flags = LC_ID_CONVERT;
2984 RETURN(ERR_PTR(-EINVAL));
2987 capa->lc_fid = *fid;
2989 capa->lc_flags |= osd->od_capa_alg << 24;
2990 capa->lc_timeout = osd->od_capa_timeout;
2991 capa->lc_expiry = 0;
2993 oc = capa_lookup(osd->od_capa_hash, capa, 1);
2995 LASSERT(!capa_is_expired(oc));
2999 spin_lock(&capa_lock);
3000 *key = osd->od_capa_keys[1];
3001 spin_unlock(&capa_lock);
3003 capa->lc_keyid = key->lk_keyid;
3004 capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
3006 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
3008 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
3009 RETURN(ERR_PTR(rc));
3012 oc = capa_add(osd->od_capa_hash, capa);
3016 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
3018 struct osd_object *obj = osd_dt_obj(dt);
3019 struct inode *inode = obj->oo_inode;
3020 struct osd_thread_info *info = osd_oti_get(env);
3021 struct dentry *dentry = &info->oti_obj_dentry;
3022 struct file *file = &info->oti_file;
3027 dentry->d_inode = inode;
3028 dentry->d_sb = inode->i_sb;
3029 file->f_dentry = dentry;
3030 file->f_mapping = inode->i_mapping;
3031 file->f_op = inode->i_fop;
3032 set_file_inode(file, inode);
3033 #ifndef HAVE_FILE_FSYNC_4ARGS
3034 mutex_lock(&inode->i_mutex);
3036 rc = do_fsync(file, 0);
3037 #ifndef HAVE_FILE_FSYNC_4ARGS
3038 mutex_unlock(&inode->i_mutex);
3043 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
3046 struct osd_object *obj = osd_dt_obj(dt);
3049 *data = (void *)obj->oo_inode;
3057 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3058 const struct dt_index_features *feat)
3060 struct iam_descr *descr;
3062 if (osd_object_is_root(o))
3063 return feat == &dt_directory_features;
3065 LASSERT(o->oo_dir != NULL);
3067 descr = o->oo_dir->od_container.ic_descr;
3068 if (feat == &dt_directory_features) {
3069 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3075 feat->dif_keysize_min <= descr->id_key_size &&
3076 descr->id_key_size <= feat->dif_keysize_max &&
3077 feat->dif_recsize_min <= descr->id_rec_size &&
3078 descr->id_rec_size <= feat->dif_recsize_max &&
3079 !(feat->dif_flags & (DT_IND_VARKEY |
3080 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3081 ergo(feat->dif_flags & DT_IND_UPDATE,
3082 1 /* XXX check that object (and file system) is
3087 static int osd_iam_container_init(const struct lu_env *env,
3088 struct osd_object *obj,
3089 struct osd_directory *dir)
3091 struct iam_container *bag = &dir->od_container;
3094 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3098 result = iam_container_setup(bag);
3100 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3102 iam_container_fini(bag);
3109 * Concurrency: no external locking is necessary.
3111 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3112 const struct dt_index_features *feat)
3116 struct osd_object *obj = osd_dt_obj(dt);
3118 LINVRNT(osd_invariant(obj));
3120 if (osd_object_is_root(obj)) {
3121 dt->do_index_ops = &osd_index_ea_ops;
3123 } else if (feat == &dt_directory_features) {
3124 dt->do_index_ops = &osd_index_ea_ops;
3125 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3130 } else if (unlikely(feat == &dt_otable_features)) {
3131 dt->do_index_ops = &osd_otable_ops;
3133 } else if (unlikely(feat == &dt_acct_features)) {
3134 dt->do_index_ops = &osd_acct_index_ops;
3137 } else if (!osd_has_index(obj)) {
3138 struct osd_directory *dir;
3143 spin_lock(&obj->oo_guard);
3144 if (obj->oo_dir == NULL)
3148 * Concurrent thread allocated container data.
3151 spin_unlock(&obj->oo_guard);
3153 * Now, that we have container data, serialize its
3156 down_write(&obj->oo_ext_idx_sem);
3158 * recheck under lock.
3160 if (!osd_has_index(obj))
3161 result = osd_iam_container_init(env, obj, dir);
3164 up_write(&obj->oo_ext_idx_sem);
3172 if (result == 0 && skip_iam == 0) {
3173 if (!osd_iam_index_probe(env, obj, feat))
3176 LINVRNT(osd_invariant(obj));
3178 if (result == 0 && is_quota_glb_feat(feat) &&
3179 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3180 result = osd_quota_migration(env, dt, feat);
3185 static int osd_otable_it_attr_get(const struct lu_env *env,
3186 struct dt_object *dt,
3187 struct lu_attr *attr,
3188 struct lustre_capa *capa)
3194 static const struct dt_object_operations osd_obj_ops = {
3195 .do_read_lock = osd_object_read_lock,
3196 .do_write_lock = osd_object_write_lock,
3197 .do_read_unlock = osd_object_read_unlock,
3198 .do_write_unlock = osd_object_write_unlock,
3199 .do_write_locked = osd_object_write_locked,
3200 .do_attr_get = osd_attr_get,
3201 .do_declare_attr_set = osd_declare_attr_set,
3202 .do_attr_set = osd_attr_set,
3203 .do_ah_init = osd_ah_init,
3204 .do_declare_create = osd_declare_object_create,
3205 .do_create = osd_object_create,
3206 .do_declare_destroy = osd_declare_object_destroy,
3207 .do_destroy = osd_object_destroy,
3208 .do_index_try = osd_index_try,
3209 .do_declare_ref_add = osd_declare_object_ref_add,
3210 .do_ref_add = osd_object_ref_add,
3211 .do_declare_ref_del = osd_declare_object_ref_del,
3212 .do_ref_del = osd_object_ref_del,
3213 .do_xattr_get = osd_xattr_get,
3214 .do_declare_xattr_set = osd_declare_xattr_set,
3215 .do_xattr_set = osd_xattr_set,
3216 .do_declare_xattr_del = osd_declare_xattr_del,
3217 .do_xattr_del = osd_xattr_del,
3218 .do_xattr_list = osd_xattr_list,
3219 .do_capa_get = osd_capa_get,
3220 .do_object_sync = osd_object_sync,
3221 .do_data_get = osd_data_get,
3225 * dt_object_operations for interoperability mode
3226 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3228 static const struct dt_object_operations osd_obj_ea_ops = {
3229 .do_read_lock = osd_object_read_lock,
3230 .do_write_lock = osd_object_write_lock,
3231 .do_read_unlock = osd_object_read_unlock,
3232 .do_write_unlock = osd_object_write_unlock,
3233 .do_write_locked = osd_object_write_locked,
3234 .do_attr_get = osd_attr_get,
3235 .do_declare_attr_set = osd_declare_attr_set,
3236 .do_attr_set = osd_attr_set,
3237 .do_ah_init = osd_ah_init,
3238 .do_declare_create = osd_declare_object_create,
3239 .do_create = osd_object_ea_create,
3240 .do_declare_destroy = osd_declare_object_destroy,
3241 .do_destroy = osd_object_destroy,
3242 .do_index_try = osd_index_try,
3243 .do_declare_ref_add = osd_declare_object_ref_add,
3244 .do_ref_add = osd_object_ref_add,
3245 .do_declare_ref_del = osd_declare_object_ref_del,
3246 .do_ref_del = osd_object_ref_del,
3247 .do_xattr_get = osd_xattr_get,
3248 .do_declare_xattr_set = osd_declare_xattr_set,
3249 .do_xattr_set = osd_xattr_set,
3250 .do_declare_xattr_del = osd_declare_xattr_del,
3251 .do_xattr_del = osd_xattr_del,
3252 .do_xattr_list = osd_xattr_list,
3253 .do_capa_get = osd_capa_get,
3254 .do_object_sync = osd_object_sync,
3255 .do_data_get = osd_data_get,
3258 static const struct dt_object_operations osd_obj_otable_it_ops = {
3259 .do_attr_get = osd_otable_it_attr_get,
3260 .do_index_try = osd_index_try,
3263 static int osd_index_declare_iam_delete(const struct lu_env *env,
3264 struct dt_object *dt,
3265 const struct dt_key *key,
3266 struct thandle *handle)
3268 struct osd_thandle *oh;
3270 oh = container_of0(handle, struct osd_thandle, ot_super);
3271 LASSERT(oh->ot_handle == NULL);
3273 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3274 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3280 * delete a (key, value) pair from index \a dt specified by \a key
3282 * \param dt osd index object
3283 * \param key key for index
3284 * \param rec record reference
3285 * \param handle transaction handler
3288 * \retval -ve failure
3291 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3292 const struct dt_key *key,
3293 struct thandle *handle,
3294 struct lustre_capa *capa)
3296 struct osd_thread_info *oti = osd_oti_get(env);
3297 struct osd_object *obj = osd_dt_obj(dt);
3298 struct osd_thandle *oh;
3299 struct iam_path_descr *ipd;
3300 struct iam_container *bag = &obj->oo_dir->od_container;
3305 LINVRNT(osd_invariant(obj));
3306 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3307 LASSERT(bag->ic_object == obj->oo_inode);
3308 LASSERT(handle != NULL);
3310 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3313 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3315 ipd = osd_idx_ipd_get(env, bag);
3316 if (unlikely(ipd == NULL))
3319 oh = container_of0(handle, struct osd_thandle, ot_super);
3320 LASSERT(oh->ot_handle != NULL);
3321 LASSERT(oh->ot_handle->h_transaction != NULL);
3323 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3324 /* swab quota uid/gid provided by caller */
3325 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3326 key = (const struct dt_key *)&oti->oti_quota_id;
3329 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3330 osd_ipd_put(env, bag, ipd);
3331 LINVRNT(osd_invariant(obj));
3335 static int osd_index_declare_ea_delete(const struct lu_env *env,
3336 struct dt_object *dt,
3337 const struct dt_key *key,
3338 struct thandle *handle)
3340 struct osd_thandle *oh;
3341 struct inode *inode;
3345 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3346 LASSERT(handle != NULL);
3348 oh = container_of0(handle, struct osd_thandle, ot_super);
3349 LASSERT(oh->ot_handle == NULL);
3351 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3352 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3354 inode = osd_dt_obj(dt)->oo_inode;
3357 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3358 true, true, NULL, false);
3362 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3365 struct osd_fid_pack *rec;
3368 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3369 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3370 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3375 static int osd_mdt_seq_exists(const struct lu_env *env,
3376 struct osd_device *osd, obd_seq seq)
3378 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3379 struct seq_server_site *ss = osd_seq_site(osd);
3386 /* XXX: currently, each MDT only store avaible sequence on disk, and no
3387 * allocated sequences information on disk, so we have to lookup FLDB,
3388 * but it probably makes more sense also store allocated sequence
3389 * locally, so we do not need do remote FLDB lookup in OSD */
3390 rc = osd_fld_lookup(env, osd, seq, range);
3392 CERROR("%s: Can not lookup fld for "LPX64"\n",
3393 osd_name(osd), seq);
3397 RETURN(ss->ss_node_id == range->lsr_index);
3400 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3405 /* FID seqs not in FLDB, must be local seq */
3406 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3409 /* Currently only check this for FID on MDT */
3410 if (osd_mdt_seq_exists(env, osd, fid_seq(fid)))
3417 * Index delete function for interoperability mode (b11826).
3418 * It will remove the directory entry added by osd_index_ea_insert().
3419 * This entry is needed to maintain name->fid mapping.
3421 * \param key, key i.e. file entry to be deleted
3423 * \retval 0, on success
3424 * \retval -ve, on error
3426 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3427 const struct dt_key *key,
3428 struct thandle *handle,
3429 struct lustre_capa *capa)
3431 struct osd_object *obj = osd_dt_obj(dt);
3432 struct inode *dir = obj->oo_inode;
3433 struct dentry *dentry;
3434 struct osd_thandle *oh;
3435 struct ldiskfs_dir_entry_2 *de = NULL;
3436 struct buffer_head *bh;
3437 struct htree_lock *hlock = NULL;
3438 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3439 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3443 LINVRNT(osd_invariant(obj));
3444 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3445 LASSERT(handle != NULL);
3447 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3449 oh = container_of(handle, struct osd_thandle, ot_super);
3450 LASSERT(oh->ot_handle != NULL);
3451 LASSERT(oh->ot_handle->h_transaction != NULL);
3453 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3456 ll_vfs_dq_init(dir);
3457 dentry = osd_child_dentry_get(env, obj,
3458 (char *)key, strlen((char *)key));
3460 if (obj->oo_hl_head != NULL) {
3461 hlock = osd_oti_get(env)->oti_hlock;
3462 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3463 dir, LDISKFS_HLOCK_DEL);
3465 down_write(&obj->oo_ext_idx_sem);
3468 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3472 /* If this is not the ".." entry, it might be a remote DNE
3473 * entry and we need to check if the FID is for a remote
3474 * MDT. If the FID is not in the directory entry (e.g.
3475 * upgraded 1.8 filesystem without dirdata enabled) then
3476 * we need to get the FID from the LMA. For a remote directory
3477 * there HAS to be an LMA, it cannot be an IGIF inode in this
3480 * Delete the entry before the agent inode in order to
3481 * simplify error handling. At worst an error after deleting
3482 * the entry first might leak the agent inode afterward. The
3483 * reverse would need filesystem abort in case of error deleting
3484 * the entry after the agent had been removed, or leave a
3485 * dangling entry pointing at a random inode. */
3486 if (strcmp((char *)key, dotdot) != 0) {
3487 LASSERT(de != NULL);
3488 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3489 /* If Fid is not in dentry, try to get it from LMA */
3490 if (rc == -ENODATA) {
3491 struct osd_inode_id *id;
3492 struct inode *inode;
3494 /* Before trying to get fid from the inode,
3495 * check whether the inode is valid.
3497 * If the inode has been deleted, do not go
3498 * ahead to do osd_ea_fid_get, which will set
3499 * the inode to bad inode, which might cause
3500 * the inode to be deleted uncorrectly */
3501 inode = ldiskfs_iget(osd_sb(osd),
3502 le32_to_cpu(de->inode));
3503 if (IS_ERR(inode)) {
3504 CDEBUG(D_INODE, "%s: "DFID"get inode"
3505 "error.\n", osd_name(osd),
3507 rc = PTR_ERR(inode);
3509 if (likely(inode->i_nlink != 0)) {
3510 id = &osd_oti_get(env)->oti_id;
3511 rc = osd_ea_fid_get(env, obj,
3512 le32_to_cpu(de->inode),
3515 CDEBUG(D_INFO, "%s: %u "DFID
3518 le32_to_cpu(de->inode),
3526 unlikely(osd_remote_fid(env, osd, fid)))
3527 /* Need to delete agent inode */
3528 ino = le32_to_cpu(de->inode);
3530 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3532 if (rc == 0 && unlikely(ino != 0)) {
3533 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3536 CERROR("%s: del local inode "DFID": rc = %d\n",
3537 osd_name(osd), PFID(fid), rc);
3543 ldiskfs_htree_unlock(hlock);
3545 up_write(&obj->oo_ext_idx_sem);
3550 /* For inode on the remote MDT, .. will point to
3551 * /Agent directory, Check whether it needs to delete
3552 * from agent directory */
3553 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3554 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3556 if (rc != 0 && rc != -ENOENT) {
3557 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3558 osd_name(osd), PFID(fid), rc);
3568 LASSERT(osd_invariant(obj));
3573 * Lookup index for \a key and copy record to \a rec.
3575 * \param dt osd index object
3576 * \param key key for index
3577 * \param rec record reference
3579 * \retval +ve success : exact mach
3580 * \retval 0 return record with key not greater than \a key
3581 * \retval -ve failure
3583 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3584 struct dt_rec *rec, const struct dt_key *key,
3585 struct lustre_capa *capa)
3587 struct osd_object *obj = osd_dt_obj(dt);
3588 struct iam_path_descr *ipd;
3589 struct iam_container *bag = &obj->oo_dir->od_container;
3590 struct osd_thread_info *oti = osd_oti_get(env);
3591 struct iam_iterator *it = &oti->oti_idx_it;
3592 struct iam_rec *iam_rec;
3597 LASSERT(osd_invariant(obj));
3598 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3599 LASSERT(bag->ic_object == obj->oo_inode);
3601 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3604 ipd = osd_idx_ipd_get(env, bag);
3608 /* got ipd now we can start iterator. */
3609 iam_it_init(it, bag, 0, ipd);
3611 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3612 /* swab quota uid/gid provided by caller */
3613 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3614 key = (const struct dt_key *)&oti->oti_quota_id;
3617 rc = iam_it_get(it, (struct iam_key *)key);
3619 if (S_ISDIR(obj->oo_inode->i_mode))
3620 iam_rec = (struct iam_rec *)oti->oti_ldp;
3622 iam_rec = (struct iam_rec *) rec;
3624 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3626 if (S_ISDIR(obj->oo_inode->i_mode))
3627 osd_fid_unpack((struct lu_fid *) rec,
3628 (struct osd_fid_pack *)iam_rec);
3629 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3630 osd_quota_unpack(obj, rec);
3635 osd_ipd_put(env, bag, ipd);
3637 LINVRNT(osd_invariant(obj));
3642 static int osd_index_declare_iam_insert(const struct lu_env *env,
3643 struct dt_object *dt,
3644 const struct dt_rec *rec,
3645 const struct dt_key *key,
3646 struct thandle *handle)
3648 struct osd_thandle *oh;
3650 LASSERT(handle != NULL);
3652 oh = container_of0(handle, struct osd_thandle, ot_super);
3653 LASSERT(oh->ot_handle == NULL);
3655 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3656 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3662 * Inserts (key, value) pair in \a dt index object.
3664 * \param dt osd index object
3665 * \param key key for index
3666 * \param rec record reference
3667 * \param th transaction handler
3670 * \retval -ve failure
3672 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3673 const struct dt_rec *rec,
3674 const struct dt_key *key, struct thandle *th,
3675 struct lustre_capa *capa, int ignore_quota)
3677 struct osd_object *obj = osd_dt_obj(dt);
3678 struct iam_path_descr *ipd;
3679 struct osd_thandle *oh;
3680 struct iam_container *bag = &obj->oo_dir->od_container;
3681 struct osd_thread_info *oti = osd_oti_get(env);
3682 struct iam_rec *iam_rec;
3687 LINVRNT(osd_invariant(obj));
3688 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3689 LASSERT(bag->ic_object == obj->oo_inode);
3690 LASSERT(th != NULL);
3692 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3695 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3697 ipd = osd_idx_ipd_get(env, bag);
3698 if (unlikely(ipd == NULL))
3701 oh = container_of0(th, struct osd_thandle, ot_super);
3702 LASSERT(oh->ot_handle != NULL);
3703 LASSERT(oh->ot_handle->h_transaction != NULL);
3704 if (S_ISDIR(obj->oo_inode->i_mode)) {
3705 iam_rec = (struct iam_rec *)oti->oti_ldp;
3706 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3707 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3708 /* pack quota uid/gid */
3709 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3710 key = (const struct dt_key *)&oti->oti_quota_id;
3711 /* pack quota record */
3712 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3713 iam_rec = (struct iam_rec *)rec;
3715 iam_rec = (struct iam_rec *)rec;
3718 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3720 osd_ipd_put(env, bag, ipd);
3721 LINVRNT(osd_invariant(obj));
3726 * Calls ldiskfs_add_entry() to add directory entry
3727 * into the directory. This is required for
3728 * interoperability mode (b11826)
3730 * \retval 0, on success
3731 * \retval -ve, on error
3733 static int __osd_ea_add_rec(struct osd_thread_info *info,
3734 struct osd_object *pobj, struct inode *cinode,
3735 const char *name, const struct dt_rec *fid,
3736 struct htree_lock *hlock, struct thandle *th)
3738 struct ldiskfs_dentry_param *ldp;
3739 struct dentry *child;
3740 struct osd_thandle *oth;
3743 oth = container_of(th, struct osd_thandle, ot_super);
3744 LASSERT(oth->ot_handle != NULL);
3745 LASSERT(oth->ot_handle->h_transaction != NULL);
3746 LASSERT(pobj->oo_inode);
3748 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3749 if (unlikely(pobj->oo_inode ==
3750 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3753 osd_get_ldiskfs_dirent_param(ldp, fid);
3754 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3755 child->d_fsdata = (void *)ldp;
3756 ll_vfs_dq_init(pobj->oo_inode);
3757 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3763 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3764 * into the directory.Also sets flags into osd object to
3765 * indicate dot and dotdot are created. This is required for
3766 * interoperability mode (b11826)
3768 * \param dir directory for dot and dotdot fixup.
3769 * \param obj child object for linking
3771 * \retval 0, on success
3772 * \retval -ve, on error
3774 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3775 struct osd_object *dir,
3776 struct inode *parent_dir, const char *name,
3777 const struct dt_rec *dot_fid,
3778 const struct dt_rec *dot_dot_fid,
3781 struct inode *inode = dir->oo_inode;
3782 struct osd_thandle *oth;
3785 oth = container_of(th, struct osd_thandle, ot_super);
3786 LASSERT(oth->ot_handle->h_transaction != NULL);
3787 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3789 if (strcmp(name, dot) == 0) {
3790 if (dir->oo_compat_dot_created) {
3793 LASSERT(inode == parent_dir);
3794 dir->oo_compat_dot_created = 1;
3797 } else if (strcmp(name, dotdot) == 0) {
3798 if (!dir->oo_compat_dot_created)
3800 /* in case of rename, dotdot is already created */
3801 if (dir->oo_compat_dotdot_created) {
3802 return __osd_ea_add_rec(info, dir, parent_dir, name,
3803 dot_dot_fid, NULL, th);
3806 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3807 parent_dir, dot_fid,
3810 dir->oo_compat_dotdot_created = 1;
3818 * It will call the appropriate osd_add* function and return the
3819 * value, return by respective functions.
3821 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3822 struct inode *cinode, const char *name,
3823 const struct dt_rec *fid, struct thandle *th)
3825 struct osd_thread_info *info = osd_oti_get(env);
3826 struct htree_lock *hlock;
3829 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3831 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3833 if (hlock != NULL) {
3834 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3837 down_write(&pobj->oo_ext_idx_sem);
3839 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3840 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3843 if (hlock != NULL) {
3844 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3845 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3847 down_write(&pobj->oo_ext_idx_sem);
3850 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3851 struct lu_fid *tfid = &info->oti_fid;
3853 *tfid = *(const struct lu_fid *)fid;
3855 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3856 (const struct dt_rec *)tfid,
3859 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3864 ldiskfs_htree_unlock(hlock);
3866 up_write(&pobj->oo_ext_idx_sem);
3872 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3873 struct osd_idmap_cache *oic)
3875 struct osd_scrub *scrub = &dev->od_scrub;
3876 struct lu_fid *fid = &oic->oic_fid;
3877 struct osd_inode_id *id = &oti->oti_id;
3882 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3886 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3887 if (rc != 0 && rc != -ENOENT)
3890 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3893 if (thread_is_running(&scrub->os_thread)) {
3894 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3895 /* There is race condition between osd_oi_lookup and OI scrub.
3896 * The OI scrub finished just after osd_oi_lookup() failure.
3897 * Under such case, it is unnecessary to trigger OI scrub again,
3898 * but try to call osd_oi_lookup() again. */
3899 if (unlikely(rc == -EAGAIN))
3905 if (!dev->od_noscrub && ++once == 1) {
3906 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3908 rc = osd_scrub_start(dev);
3909 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3911 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3920 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3921 struct osd_device *dev,
3922 struct osd_idmap_cache *oic,
3923 struct lu_fid *fid, __u32 ino)
3925 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3926 struct inode *inode;
3929 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3930 inode = osd_iget(oti, dev, &oic->oic_lid);
3931 if (IS_ERR(inode)) {
3932 fid_zero(&oic->oic_fid);
3933 return PTR_ERR(inode);
3936 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3939 fid_zero(&oic->oic_fid);
3941 *fid = oic->oic_fid = lma->lma_self_fid;
3945 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3946 struct osd_inode_id *id, const struct lu_fid *fid)
3948 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3949 id->oii_ino, id->oii_gen, info);
3950 info->oti_cache.oic_lid = *id;
3951 info->oti_cache.oic_fid = *fid;
3952 info->oti_cache.oic_dev = osd;
3958 * Calls ->lookup() to find dentry. From dentry get inode and
3959 * read inode's ea to get fid. This is required for interoperability
3962 * \retval 0, on success
3963 * \retval -ve, on error
3965 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3966 struct dt_rec *rec, const struct dt_key *key)
3968 struct inode *dir = obj->oo_inode;
3969 struct dentry *dentry;
3970 struct ldiskfs_dir_entry_2 *de;
3971 struct buffer_head *bh;
3972 struct lu_fid *fid = (struct lu_fid *) rec;
3973 struct htree_lock *hlock = NULL;
3978 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3980 dentry = osd_child_dentry_get(env, obj,
3981 (char *)key, strlen((char *)key));
3983 if (obj->oo_hl_head != NULL) {
3984 hlock = osd_oti_get(env)->oti_hlock;
3985 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3986 dir, LDISKFS_HLOCK_LOOKUP);
3988 down_read(&obj->oo_ext_idx_sem);
3991 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3993 struct osd_thread_info *oti = osd_oti_get(env);
3994 struct osd_inode_id *id = &oti->oti_id;
3995 struct osd_idmap_cache *oic = &oti->oti_cache;
3996 struct osd_device *dev = osd_obj2dev(obj);
3997 struct osd_scrub *scrub = &dev->od_scrub;
3998 struct scrub_file *sf = &scrub->os_file;
4000 ino = le32_to_cpu(de->inode);
4001 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4003 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4007 rc = osd_get_fid_from_dentry(de, rec);
4009 /* done with de, release bh */
4012 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4014 osd_id_gen(id, ino, OSD_OII_NOGEN);
4016 fid_zero(&oic->oic_fid);
4020 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
4024 if ((scrub->os_pos_current <= ino) &&
4025 ((sf->sf_flags & SF_INCONSISTENT) ||
4026 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
4027 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
4029 osd_consistency_check(oti, dev, oic);
4038 ldiskfs_htree_unlock(hlock);
4040 up_read(&obj->oo_ext_idx_sem);
4045 * Find the osd object for given fid.
4047 * \param fid need to find the osd object having this fid
4049 * \retval osd_object on success
4050 * \retval -ve on error
4052 struct osd_object *osd_object_find(const struct lu_env *env,
4053 struct dt_object *dt,
4054 const struct lu_fid *fid)
4056 struct lu_device *ludev = dt->do_lu.lo_dev;
4057 struct osd_object *child = NULL;
4058 struct lu_object *luch;
4059 struct lu_object *lo;
4062 * at this point topdev might not exist yet
4063 * (i.e. MGS is preparing profiles). so we can
4064 * not rely on topdev and instead lookup with
4065 * our device passed as topdev. this can't work
4066 * if the object isn't cached yet (as osd doesn't
4067 * allocate lu_header). IOW, the object must be
4068 * in the cache, otherwise lu_object_alloc() crashes
4071 luch = lu_object_find_at(env, ludev, fid, NULL);
4072 if (!IS_ERR(luch)) {
4073 if (lu_object_exists(luch)) {
4074 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4076 child = osd_obj(lo);
4078 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4079 "lu_object can't be located"
4080 DFID"\n", PFID(fid));
4082 if (child == NULL) {
4083 lu_object_put(env, luch);
4084 CERROR("Unable to get osd_object\n");
4085 child = ERR_PTR(-ENOENT);
4088 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4089 "lu_object does not exists "DFID"\n",
4091 lu_object_put(env, luch);
4092 child = ERR_PTR(-ENOENT);
4095 child = ERR_CAST(luch);
4102 * Put the osd object once done with it.
4104 * \param obj osd object that needs to be put
4106 static inline void osd_object_put(const struct lu_env *env,
4107 struct osd_object *obj)
4109 lu_object_put(env, &obj->oo_dt.do_lu);
4112 static int osd_index_declare_ea_insert(const struct lu_env *env,
4113 struct dt_object *dt,
4114 const struct dt_rec *rec,
4115 const struct dt_key *key,
4116 struct thandle *handle)
4118 struct osd_thandle *oh;
4119 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4120 struct lu_fid *fid = (struct lu_fid *)rec;
4124 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4125 LASSERT(handle != NULL);
4127 oh = container_of0(handle, struct osd_thandle, ot_super);
4128 LASSERT(oh->ot_handle == NULL);
4130 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4131 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4133 if (osd_dt_obj(dt)->oo_inode == NULL) {
4134 const char *name = (const char *)key;
4135 /* Object is not being created yet. Only happens when
4136 * 1. declare directory create
4137 * 2. declare insert .
4138 * 3. declare insert ..
4140 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4142 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4144 /* We ignore block quota on meta pool (MDTs), so needn't
4145 * calculate how many blocks will be consumed by this index
4147 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4148 oh, true, true, NULL, false);
4154 rc = osd_remote_fid(env, osd, fid);
4160 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4161 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4162 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4163 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4164 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4165 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4171 * Index add function for interoperability mode (b11826).
4172 * It will add the directory entry.This entry is needed to
4173 * maintain name->fid mapping.
4175 * \param key it is key i.e. file entry to be inserted
4176 * \param rec it is value of given key i.e. fid
4178 * \retval 0, on success
4179 * \retval -ve, on error
4181 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4182 const struct dt_rec *rec,
4183 const struct dt_key *key, struct thandle *th,
4184 struct lustre_capa *capa, int ignore_quota)
4186 struct osd_object *obj = osd_dt_obj(dt);
4187 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4188 struct lu_fid *fid = (struct lu_fid *) rec;
4189 const char *name = (const char *)key;
4190 struct osd_thread_info *oti = osd_oti_get(env);
4191 struct osd_inode_id *id = &oti->oti_id;
4192 struct inode *child_inode = NULL;
4193 struct osd_object *child = NULL;
4197 LASSERT(osd_invariant(obj));
4198 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4199 LASSERT(th != NULL);
4201 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4203 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4206 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4208 rc = osd_remote_fid(env, osd, fid);
4210 CERROR("%s: Can not find object "DFID" rc %d\n",
4211 osd_name(osd), PFID(fid), rc);
4216 /* Insert remote entry */
4217 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4218 struct osd_mdobj_map *omm = osd->od_mdt_map;
4219 struct osd_thandle *oh;
4221 /* If parent on remote MDT, we need put this object
4223 oh = container_of(th, typeof(*oh), ot_super);
4224 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4226 CERROR("%s: add "DFID" error: rc = %d\n",
4228 PFID(lu_object_fid(&dt->do_lu)), rc);
4232 child_inode = igrab(omm->omm_remote_parent->d_inode);
4234 child_inode = osd_create_local_agent_inode(env, osd,
4237 if (IS_ERR(child_inode))
4238 RETURN(PTR_ERR(child_inode));
4241 /* Insert local entry */
4242 child = osd_object_find(env, dt, fid);
4243 if (IS_ERR(child)) {
4244 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4245 osd_name(osd), PFID(fid),
4246 id->oii_ino, id->oii_gen,
4247 (int)PTR_ERR(child));
4248 RETURN(PTR_ERR(child));
4250 child_inode = igrab(child->oo_inode);
4253 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4257 osd_object_put(env, child);
4258 LASSERT(osd_invariant(obj));
4263 * Initialize osd Iterator for given osd index object.
4265 * \param dt osd index object
4268 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4269 struct dt_object *dt,
4271 struct lustre_capa *capa)
4273 struct osd_it_iam *it;
4274 struct osd_thread_info *oti = osd_oti_get(env);
4275 struct osd_object *obj = osd_dt_obj(dt);
4276 struct lu_object *lo = &dt->do_lu;
4277 struct iam_path_descr *ipd;
4278 struct iam_container *bag = &obj->oo_dir->od_container;
4280 LASSERT(lu_object_exists(lo));
4282 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4283 return ERR_PTR(-EACCES);
4286 ipd = osd_it_ipd_get(env, bag);
4287 if (likely(ipd != NULL)) {
4291 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4292 return (struct dt_it *)it;
4294 return ERR_PTR(-ENOMEM);
4298 * free given Iterator.
4301 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4303 struct osd_it_iam *it = (struct osd_it_iam *)di;
4304 struct osd_object *obj = it->oi_obj;
4306 iam_it_fini(&it->oi_it);
4307 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4308 lu_object_put(env, &obj->oo_dt.do_lu);
4312 * Move Iterator to record specified by \a key
4314 * \param di osd iterator
4315 * \param key key for index
4317 * \retval +ve di points to record with least key not larger than key
4318 * \retval 0 di points to exact matched key
4319 * \retval -ve failure
4322 static int osd_it_iam_get(const struct lu_env *env,
4323 struct dt_it *di, const struct dt_key *key)
4325 struct osd_thread_info *oti = osd_oti_get(env);
4326 struct osd_it_iam *it = (struct osd_it_iam *)di;
4328 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4329 /* swab quota uid/gid */
4330 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4331 key = (struct dt_key *)&oti->oti_quota_id;
4334 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4340 * \param di osd iterator
4342 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4344 struct osd_it_iam *it = (struct osd_it_iam *)di;
4346 iam_it_put(&it->oi_it);
4350 * Move iterator by one record
4352 * \param di osd iterator
4354 * \retval +1 end of container reached
4356 * \retval -ve failure
4359 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4361 struct osd_it_iam *it = (struct osd_it_iam *)di;
4363 return iam_it_next(&it->oi_it);
4367 * Return pointer to the key under iterator.
4370 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4371 const struct dt_it *di)
4373 struct osd_thread_info *oti = osd_oti_get(env);
4374 struct osd_it_iam *it = (struct osd_it_iam *)di;
4375 struct osd_object *obj = it->oi_obj;
4378 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4380 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4381 /* swab quota uid/gid */
4382 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4383 key = (struct dt_key *)&oti->oti_quota_id;
4390 * Return size of key under iterator (in bytes)
4393 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4395 struct osd_it_iam *it = (struct osd_it_iam *)di;
4397 return iam_it_key_size(&it->oi_it);
4401 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4403 /* check if file type is required */
4404 if (ent->lde_attrs & LUDA_TYPE) {
4405 struct luda_type *lt;
4406 int align = sizeof(*lt) - 1;
4408 len = (len + align) & ~align;
4409 lt = (struct luda_type *)(ent->lde_name + len);
4410 lt->lt_type = cpu_to_le16(DTTOIF(type));
4413 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4417 * build lu direct from backend fs dirent.
4421 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4422 char *name, __u16 namelen, __u16 type, __u32 attr)
4424 ent->lde_attrs = attr | LUDA_FID;
4425 fid_cpu_to_le(&ent->lde_fid, fid);
4427 ent->lde_hash = cpu_to_le64(offset);
4428 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4430 strncpy(ent->lde_name, name, namelen);
4431 ent->lde_name[namelen] = '\0';
4432 ent->lde_namelen = cpu_to_le16(namelen);
4434 /* append lustre attributes */
4435 osd_it_append_attrs(ent, namelen, type);
4439 * Return pointer to the record under iterator.
4441 static int osd_it_iam_rec(const struct lu_env *env,
4442 const struct dt_it *di,
4443 struct dt_rec *dtrec, __u32 attr)
4445 struct osd_it_iam *it = (struct osd_it_iam *)di;
4446 struct osd_thread_info *info = osd_oti_get(env);
4449 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4450 const struct osd_fid_pack *rec;
4451 struct lu_fid *fid = &info->oti_fid;
4452 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4458 name = (char *)iam_it_key_get(&it->oi_it);
4460 RETURN(PTR_ERR(name));
4462 namelen = iam_it_key_size(&it->oi_it);
4464 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4466 RETURN(PTR_ERR(rec));
4468 rc = osd_fid_unpack(fid, rec);
4472 hash = iam_it_store(&it->oi_it);
4474 /* IAM does not store object type in IAM index (dir) */
4475 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4477 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4478 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4479 (struct iam_rec *)dtrec);
4480 osd_quota_unpack(it->oi_obj, dtrec);
4482 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4483 (struct iam_rec *)dtrec);
4490 * Returns cookie for current Iterator position.
4492 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4494 struct osd_it_iam *it = (struct osd_it_iam *)di;
4496 return iam_it_store(&it->oi_it);
4500 * Restore iterator from cookie.
4502 * \param di osd iterator
4503 * \param hash Iterator location cookie
4505 * \retval +ve di points to record with least key not larger than key.
4506 * \retval 0 di points to exact matched key
4507 * \retval -ve failure
4510 static int osd_it_iam_load(const struct lu_env *env,
4511 const struct dt_it *di, __u64 hash)
4513 struct osd_it_iam *it = (struct osd_it_iam *)di;
4515 return iam_it_load(&it->oi_it, hash);
4518 static const struct dt_index_operations osd_index_iam_ops = {
4519 .dio_lookup = osd_index_iam_lookup,
4520 .dio_declare_insert = osd_index_declare_iam_insert,
4521 .dio_insert = osd_index_iam_insert,
4522 .dio_declare_delete = osd_index_declare_iam_delete,
4523 .dio_delete = osd_index_iam_delete,
4525 .init = osd_it_iam_init,
4526 .fini = osd_it_iam_fini,
4527 .get = osd_it_iam_get,
4528 .put = osd_it_iam_put,
4529 .next = osd_it_iam_next,
4530 .key = osd_it_iam_key,
4531 .key_size = osd_it_iam_key_size,
4532 .rec = osd_it_iam_rec,
4533 .store = osd_it_iam_store,
4534 .load = osd_it_iam_load
4540 * Creates or initializes iterator context.
4542 * \retval struct osd_it_ea, iterator structure on success
4545 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4546 struct dt_object *dt,
4548 struct lustre_capa *capa)
4550 struct osd_object *obj = osd_dt_obj(dt);
4551 struct osd_thread_info *info = osd_oti_get(env);
4552 struct osd_it_ea *it = &info->oti_it_ea;
4553 struct file *file = &it->oie_file;
4554 struct lu_object *lo = &dt->do_lu;
4555 struct dentry *obj_dentry = &info->oti_it_dentry;
4557 LASSERT(lu_object_exists(lo));
4559 obj_dentry->d_inode = obj->oo_inode;
4560 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4561 obj_dentry->d_name.hash = 0;
4563 it->oie_rd_dirent = 0;
4564 it->oie_it_dirent = 0;
4565 it->oie_dirent = NULL;
4566 it->oie_buf = info->oti_it_ea_buf;
4569 /* Reset the "file" totally to avoid to reuse any old value from
4570 * former readdir handling, the "file->f_pos" should be zero. */
4571 memset(file, 0, sizeof(*file));
4572 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4573 if (attr & LUDA_64BITHASH)
4574 file->f_mode = FMODE_64BITHASH;
4576 file->f_mode = FMODE_32BITHASH;
4577 file->f_dentry = obj_dentry;
4578 file->f_mapping = obj->oo_inode->i_mapping;
4579 file->f_op = obj->oo_inode->i_fop;
4580 set_file_inode(file, obj->oo_inode);
4583 RETURN((struct dt_it *) it);
4587 * Destroy or finishes iterator context.
4589 * \param di iterator structure to be destroyed
4591 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4593 struct osd_it_ea *it = (struct osd_it_ea *)di;
4594 struct osd_object *obj = it->oie_obj;
4595 struct inode *inode = obj->oo_inode;
4598 it->oie_file.f_op->release(inode, &it->oie_file);
4599 lu_object_put(env, &obj->oo_dt.do_lu);
4604 * It position the iterator at given key, so that next lookup continues from
4605 * that key Or it is similar to dio_it->load() but based on a key,
4606 * rather than file position.
4608 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4611 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4613 static int osd_it_ea_get(const struct lu_env *env,
4614 struct dt_it *di, const struct dt_key *key)
4616 struct osd_it_ea *it = (struct osd_it_ea *)di;
4619 LASSERT(((const char *)key)[0] == '\0');
4620 it->oie_file.f_pos = 0;
4621 it->oie_rd_dirent = 0;
4622 it->oie_it_dirent = 0;
4623 it->oie_dirent = NULL;
4631 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4636 * It is called internally by ->readdir(). It fills the
4637 * iterator's in-memory data structure with required
4638 * information i.e. name, namelen, rec_size etc.
4640 * \param buf in which information to be filled in.
4641 * \param name name of the file in given dir
4643 * \retval 0 on success
4644 * \retval 1 on buffer full
4646 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4647 loff_t offset, __u64 ino,
4650 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4651 struct osd_object *obj = it->oie_obj;
4652 struct osd_it_ea_dirent *ent = it->oie_dirent;
4653 struct lu_fid *fid = &ent->oied_fid;
4654 struct osd_fid_pack *rec;
4657 /* this should never happen */
4658 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4659 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4663 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4667 /* "." is just the object itself. */
4668 if (namelen == 1 && name[0] == '.') {
4669 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4670 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4671 rec = (struct osd_fid_pack*) (name + namelen + 1);
4672 if (osd_fid_unpack(fid, rec) != 0)
4677 d_type &= ~LDISKFS_DIRENT_LUFID;
4679 /* NOT export local root. */
4680 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4681 ino = obj->oo_inode->i_ino;
4682 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4685 ent->oied_ino = ino;
4686 ent->oied_off = offset;
4687 ent->oied_namelen = namelen;
4688 ent->oied_type = d_type;
4690 memcpy(ent->oied_name, name, namelen);
4692 it->oie_rd_dirent++;
4693 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4698 * Calls ->readdir() to load a directory entry at a time
4699 * and stored it in iterator's in-memory data structure.
4701 * \param di iterator's in memory structure
4703 * \retval 0 on success
4704 * \retval -ve on error
4706 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4707 const struct dt_it *di)
4709 struct osd_it_ea *it = (struct osd_it_ea *)di;
4710 struct osd_object *obj = it->oie_obj;
4711 struct inode *inode = obj->oo_inode;
4712 struct htree_lock *hlock = NULL;
4716 it->oie_dirent = it->oie_buf;
4717 it->oie_rd_dirent = 0;
4719 if (obj->oo_hl_head != NULL) {
4720 hlock = osd_oti_get(env)->oti_hlock;
4721 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4722 inode, LDISKFS_HLOCK_READDIR);
4724 down_read(&obj->oo_ext_idx_sem);
4727 result = inode->i_fop->readdir(&it->oie_file, it,
4728 (filldir_t) osd_ldiskfs_filldir);
4731 ldiskfs_htree_unlock(hlock);
4733 up_read(&obj->oo_ext_idx_sem);
4735 if (it->oie_rd_dirent == 0) {
4738 it->oie_dirent = it->oie_buf;
4739 it->oie_it_dirent = 1;
4746 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4747 * to load a directory entry at a time and stored it in
4748 * iterator's in-memory data structure.
4750 * \param di iterator's in memory structure
4752 * \retval +ve iterator reached to end
4753 * \retval 0 iterator not reached to end
4754 * \retval -ve on error
4756 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4758 struct osd_it_ea *it = (struct osd_it_ea *)di;
4763 if (it->oie_it_dirent < it->oie_rd_dirent) {
4765 (void *) it->oie_dirent +
4766 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4767 it->oie_dirent->oied_namelen);
4768 it->oie_it_dirent++;
4771 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4774 rc = osd_ldiskfs_it_fill(env, di);
4781 * Returns the key at current position from iterator's in memory structure.
4783 * \param di iterator's in memory structure
4785 * \retval key i.e. struct dt_key on success
4787 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4788 const struct dt_it *di)
4790 struct osd_it_ea *it = (struct osd_it_ea *)di;
4792 return (struct dt_key *)it->oie_dirent->oied_name;
4796 * Returns the key's size at current position from iterator's in memory structure.
4798 * \param di iterator's in memory structure
4800 * \retval key_size i.e. struct dt_key on success
4802 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4804 struct osd_it_ea *it = (struct osd_it_ea *)di;
4806 return it->oie_dirent->oied_namelen;
4810 osd_dirent_update(handle_t *jh, struct super_block *sb,
4811 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4812 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4814 struct osd_fid_pack *rec;
4818 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4819 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4821 rc = ldiskfs_journal_get_write_access(jh, bh);
4823 CERROR("%.16s: fail to write access for update dirent: "
4824 "name = %.*s, rc = %d\n",
4825 LDISKFS_SB(sb)->s_es->s_volume_name,
4826 ent->oied_namelen, ent->oied_name, rc);
4830 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4831 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4832 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4834 CERROR("%.16s: fail to dirty metadata for update dirent: "
4835 "name = %.*s, rc = %d\n",
4836 LDISKFS_SB(sb)->s_es->s_volume_name,
4837 ent->oied_namelen, ent->oied_name, rc);
4843 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4845 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4846 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4853 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4855 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4856 "dot_dotdot = %d\n", dot_dotdot);
4858 if (LDISKFS_DIR_REC_LEN(de) >=
4859 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4866 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4867 struct inode *dir, struct inode *inode,
4868 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4869 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4870 struct htree_lock *hlock)
4872 struct dentry *dentry;
4873 struct osd_fid_pack *rec;
4874 struct ldiskfs_dentry_param *ldp;
4878 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4879 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4882 /* There is enough space to hold the FID-in-dirent. */
4883 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4884 dir->i_sb->s_blocksize)) {
4885 rc = ldiskfs_journal_get_write_access(jh, bh);
4887 CERROR("%.16s: fail to write access for reinsert "
4888 "dirent: name = %.*s, rc = %d\n",
4889 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4890 ent->oied_namelen, ent->oied_name, rc);
4894 de->name[de->name_len] = 0;
4895 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4896 rec->fp_len = sizeof(struct lu_fid) + 1;
4897 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4898 de->file_type |= LDISKFS_DIRENT_LUFID;
4900 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4902 CERROR("%.16s: fail to dirty metadata for reinsert "
4903 "dirent: name = %.*s, rc = %d\n",
4904 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4905 ent->oied_namelen, ent->oied_name, rc);
4910 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4912 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4913 "name = %.*s, rc = %d\n",
4914 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4915 ent->oied_namelen, ent->oied_name, rc);
4919 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4921 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4922 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4923 dentry->d_fsdata = (void *)ldp;
4924 ll_vfs_dq_init(dir);
4925 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4926 /* It is too bad, we cannot reinsert the name entry back.
4927 * That means we lose it! */
4929 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4930 "name = %.*s, rc = %d\n",
4931 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4932 ent->oied_namelen, ent->oied_name, rc);
4938 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4939 struct osd_it_ea *it, struct lu_fid *fid,
4940 struct osd_inode_id *id, __u32 *attr)
4942 struct osd_thread_info *info = osd_oti_get(env);
4943 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4944 struct osd_device *dev = osd_obj2dev(obj);
4945 struct super_block *sb = osd_sb(dev);
4946 const char *devname =
4947 LDISKFS_SB(sb)->s_es->s_volume_name;
4948 struct osd_it_ea_dirent *ent = it->oie_dirent;
4949 struct inode *dir = obj->oo_inode;
4950 struct htree_lock *hlock = NULL;
4951 struct buffer_head *bh = NULL;
4952 handle_t *jh = NULL;
4953 struct ldiskfs_dir_entry_2 *de;
4954 struct dentry *dentry;
4955 struct inode *inode;
4962 if (ent->oied_name[0] == '.') {
4963 if (ent->oied_namelen == 1)
4965 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4969 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4972 /* We need to ensure that the name entry is still valid.
4973 * Because it may be removed or renamed by other already.
4975 * The unlink or rename operation will start journal before PDO lock,
4976 * so to avoid deadlock, here we need to start journal handle before
4977 * related PDO lock also. But because we do not know whether there
4978 * will be something to be repaired before PDO lock, we just start
4979 * journal without conditions.
4981 * We may need to remove the name entry firstly, then insert back.
4982 * One credit is for user quota file update.
4983 * One credit is for group quota file update.
4984 * Two credits are for dirty inode. */
4985 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4986 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4989 if (dev->od_dirent_journal) {
4990 jh = ldiskfs_journal_start_sb(sb, credits);
4993 CERROR("%.16s: fail to start trans for dirent "
4994 "check_repair: credits %d, name %.*s, rc %d\n",
4995 devname, credits, ent->oied_namelen,
4996 ent->oied_name, rc);
5000 if (obj->oo_hl_head != NULL) {
5001 hlock = osd_oti_get(env)->oti_hlock;
5002 /* "0" means exclusive lock for the whole directory.
5003 * We need to prevent others access such name entry
5004 * during the delete + insert. Neither HLOCK_ADD nor
5005 * HLOCK_DEL cannot guarantee the atomicity. */
5006 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5008 down_write(&obj->oo_ext_idx_sem);
5011 if (obj->oo_hl_head != NULL) {
5012 hlock = osd_oti_get(env)->oti_hlock;
5013 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5014 LDISKFS_HLOCK_LOOKUP);
5016 down_read(&obj->oo_ext_idx_sem);
5020 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
5021 /* For dot/dotdot entry, if there is not enough space to hold the
5022 * FID-in-dirent, just keep them there. It only happens when the
5023 * device upgraded from 1.8 or restored from MDT file-level backup.
5024 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5025 * and needs to get FID from LMA when readdir, it will not affect the
5026 * performance much. */
5027 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
5028 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5029 *attr |= LUDA_IGNORE;
5030 GOTO(out_journal, rc = 0);
5033 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5034 inode = osd_iget(info, dev, id);
5035 if (IS_ERR(inode)) {
5036 rc = PTR_ERR(inode);
5037 if (rc == -ENOENT || rc == -ESTALE) {
5038 *attr |= LUDA_IGNORE;
5042 GOTO(out_journal, rc);
5045 /* skip the REMOTE_PARENT_DIR. */
5046 if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
5047 GOTO(out_inode, rc = 0);
5049 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5051 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5053 if (fid_is_sane(fid)) {
5054 /* FID-in-dirent is valid. */
5055 if (lu_fid_eq(fid, &lma->lma_self_fid))
5056 GOTO(out_inode, rc = 0);
5058 /* Do not repair under dryrun mode. */
5059 if (*attr & LUDA_VERIFY_DRYRUN) {
5060 *attr |= LUDA_REPAIR;
5061 GOTO(out_inode, rc = 0);
5064 if (!dev->od_dirent_journal) {
5068 ldiskfs_htree_unlock(hlock);
5070 up_read(&obj->oo_ext_idx_sem);
5071 dev->od_dirent_journal = 1;
5075 *fid = lma->lma_self_fid;
5077 /* Update the FID-in-dirent. */
5078 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5080 *attr |= LUDA_REPAIR;
5082 /* Do not repair under dryrun mode. */
5083 if (*attr & LUDA_VERIFY_DRYRUN) {
5084 *fid = lma->lma_self_fid;
5085 *attr |= LUDA_REPAIR;
5086 GOTO(out_inode, rc = 0);
5089 if (!dev->od_dirent_journal) {
5093 ldiskfs_htree_unlock(hlock);
5095 up_read(&obj->oo_ext_idx_sem);
5096 dev->od_dirent_journal = 1;
5100 *fid = lma->lma_self_fid;
5102 /* Append the FID-in-dirent. */
5103 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5104 fid, bh, de, hlock);
5106 *attr |= LUDA_REPAIR;
5108 } else if (rc == -ENODATA) {
5109 /* Do not repair under dryrun mode. */
5110 if (*attr & LUDA_VERIFY_DRYRUN) {
5111 if (fid_is_sane(fid)) {
5112 *attr |= LUDA_REPAIR;
5114 lu_igif_build(fid, inode->i_ino,
5115 inode->i_generation);
5116 *attr |= LUDA_UPGRADE;
5118 GOTO(out_inode, rc = 0);
5121 if (!dev->od_dirent_journal) {
5125 ldiskfs_htree_unlock(hlock);
5127 up_read(&obj->oo_ext_idx_sem);
5128 dev->od_dirent_journal = 1;
5133 if (unlikely(fid_is_sane(fid))) {
5134 /* FID-in-dirent exists, but FID-in-LMA is lost.
5135 * Trust the FID-in-dirent, and add FID-in-LMA. */
5136 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5138 *attr |= LUDA_REPAIR;
5140 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5141 /* It is probably IGIF object. Only aappend the
5142 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5143 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5144 fid, bh, de, hlock);
5146 *attr |= LUDA_UPGRADE;
5150 GOTO(out_inode, rc);
5157 if (hlock != NULL) {
5158 ldiskfs_htree_unlock(hlock);
5160 if (dev->od_dirent_journal)
5161 up_write(&obj->oo_ext_idx_sem);
5163 up_read(&obj->oo_ext_idx_sem);
5166 ldiskfs_journal_stop(jh);
5167 if (rc >= 0 && !dirty)
5168 dev->od_dirent_journal = 0;
5173 * Returns the value at current position from iterator's in memory structure.
5175 * \param di struct osd_it_ea, iterator's in memory structure
5176 * \param attr attr requested for dirent.
5177 * \param lde lustre dirent
5179 * \retval 0 no error and \param lde has correct lustre dirent.
5180 * \retval -ve on error
5182 static inline int osd_it_ea_rec(const struct lu_env *env,
5183 const struct dt_it *di,
5184 struct dt_rec *dtrec, __u32 attr)
5186 struct osd_it_ea *it = (struct osd_it_ea *)di;
5187 struct osd_object *obj = it->oie_obj;
5188 struct osd_device *dev = osd_obj2dev(obj);
5189 struct osd_scrub *scrub = &dev->od_scrub;
5190 struct scrub_file *sf = &scrub->os_file;
5191 struct osd_thread_info *oti = osd_oti_get(env);
5192 struct osd_inode_id *id = &oti->oti_id;
5193 struct osd_idmap_cache *oic = &oti->oti_cache;
5194 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5195 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5196 __u32 ino = it->oie_dirent->oied_ino;
5200 if (attr & LUDA_VERIFY) {
5202 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5203 attr |= LUDA_IGNORE;
5206 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5210 attr &= ~LU_DIRENT_ATTRS_MASK;
5211 if (!fid_is_sane(fid)) {
5212 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5215 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5217 osd_id_gen(id, ino, OSD_OII_NOGEN);
5221 /* Pack the entry anyway, at least the offset is right. */
5222 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5223 it->oie_dirent->oied_name,
5224 it->oie_dirent->oied_namelen,
5225 it->oie_dirent->oied_type, attr);
5230 if (osd_remote_fid(env, dev, fid))
5233 if (likely(!(attr & LUDA_IGNORE)))
5234 rc = osd_add_oi_cache(oti, dev, id, fid);
5236 if (!(attr & LUDA_VERIFY) &&
5237 (scrub->os_pos_current <= ino) &&
5238 ((sf->sf_flags & SF_INCONSISTENT) ||
5239 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5240 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5241 osd_consistency_check(oti, dev, oic);
5247 * Returns a cookie for current position of the iterator head, so that
5248 * user can use this cookie to load/start the iterator next time.
5250 * \param di iterator's in memory structure
5252 * \retval cookie for current position, on success
5254 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5256 struct osd_it_ea *it = (struct osd_it_ea *)di;
5258 return it->oie_dirent->oied_off;
5262 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5263 * to load a directory entry at a time and stored it i inn,
5264 * in iterator's in-memory data structure.
5266 * \param di struct osd_it_ea, iterator's in memory structure
5268 * \retval +ve on success
5269 * \retval -ve on error
5271 static int osd_it_ea_load(const struct lu_env *env,
5272 const struct dt_it *di, __u64 hash)
5274 struct osd_it_ea *it = (struct osd_it_ea *)di;
5278 it->oie_file.f_pos = hash;
5280 rc = osd_ldiskfs_it_fill(env, di);
5288 * Index lookup function for interoperability mode (b11826).
5290 * \param key, key i.e. file name to be searched
5292 * \retval +ve, on success
5293 * \retval -ve, on error
5295 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5296 struct dt_rec *rec, const struct dt_key *key,
5297 struct lustre_capa *capa)
5299 struct osd_object *obj = osd_dt_obj(dt);
5304 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5305 LINVRNT(osd_invariant(obj));
5307 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5310 rc = osd_ea_lookup_rec(env, obj, rec, key);
5317 * Index and Iterator operations for interoperability
5318 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5320 static const struct dt_index_operations osd_index_ea_ops = {
5321 .dio_lookup = osd_index_ea_lookup,
5322 .dio_declare_insert = osd_index_declare_ea_insert,
5323 .dio_insert = osd_index_ea_insert,
5324 .dio_declare_delete = osd_index_declare_ea_delete,
5325 .dio_delete = osd_index_ea_delete,
5327 .init = osd_it_ea_init,
5328 .fini = osd_it_ea_fini,
5329 .get = osd_it_ea_get,
5330 .put = osd_it_ea_put,
5331 .next = osd_it_ea_next,
5332 .key = osd_it_ea_key,
5333 .key_size = osd_it_ea_key_size,
5334 .rec = osd_it_ea_rec,
5335 .store = osd_it_ea_store,
5336 .load = osd_it_ea_load
5340 static void *osd_key_init(const struct lu_context *ctx,
5341 struct lu_context_key *key)
5343 struct osd_thread_info *info;
5345 OBD_ALLOC_PTR(info);
5347 return ERR_PTR(-ENOMEM);
5349 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5350 if (info->oti_it_ea_buf == NULL)
5353 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5355 info->oti_hlock = ldiskfs_htree_lock_alloc();
5356 if (info->oti_hlock == NULL)
5362 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5365 return ERR_PTR(-ENOMEM);
5368 static void osd_key_fini(const struct lu_context *ctx,
5369 struct lu_context_key *key, void* data)
5371 struct osd_thread_info *info = data;
5373 if (info->oti_hlock != NULL)
5374 ldiskfs_htree_lock_free(info->oti_hlock);
5375 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5376 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5377 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5381 static void osd_key_exit(const struct lu_context *ctx,
5382 struct lu_context_key *key, void *data)
5384 struct osd_thread_info *info = data;
5386 LASSERT(info->oti_r_locks == 0);
5387 LASSERT(info->oti_w_locks == 0);
5388 LASSERT(info->oti_txns == 0);
5391 /* type constructor/destructor: osd_type_init, osd_type_fini */
5392 LU_TYPE_INIT_FINI(osd, &osd_key);
5394 struct lu_context_key osd_key = {
5395 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5396 .lct_init = osd_key_init,
5397 .lct_fini = osd_key_fini,
5398 .lct_exit = osd_key_exit
5402 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5403 const char *name, struct lu_device *next)
5405 struct osd_device *osd = osd_dev(d);
5407 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5408 >= sizeof(osd->od_svname))
5410 return osd_procfs_init(osd, name);
5413 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5417 /* shutdown quota slave instance associated with the device */
5418 if (o->od_quota_slave != NULL) {
5419 qsd_fini(env, o->od_quota_slave);
5420 o->od_quota_slave = NULL;
5426 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5430 if (o->od_mnt != NULL) {
5431 shrink_dcache_sb(osd_sb(o));
5432 osd_sync(env, &o->od_dt_dev);
5441 static int osd_mount(const struct lu_env *env,
5442 struct osd_device *o, struct lustre_cfg *cfg)
5444 const char *name = lustre_cfg_string(cfg, 0);
5445 const char *dev = lustre_cfg_string(cfg, 1);
5447 unsigned long page, s_flags, lmd_flags = 0;
5448 struct page *__page;
5449 struct file_system_type *type;
5450 char *options = NULL;
5452 struct osd_thread_info *info = osd_oti_get(env);
5453 struct lu_fid *fid = &info->oti_fid;
5454 struct inode *inode;
5458 if (o->od_mnt != NULL)
5461 if (strlen(dev) >= sizeof(o->od_mntdev))
5463 strcpy(o->od_mntdev, dev);
5465 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5467 GOTO(out, rc = -ENOMEM);
5469 str = lustre_cfg_string(cfg, 2);
5470 s_flags = simple_strtoul(str, NULL, 0);
5471 str = strstr(str, ":");
5473 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5474 opts = lustre_cfg_string(cfg, 3);
5475 page = (unsigned long)page_address(__page);
5476 options = (char *)page;
5479 strcat(options, "user_xattr,acl");
5481 strcat(options, opts);
5483 /* Glom up mount options */
5484 if (*options != '\0')
5485 strcat(options, ",");
5486 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5488 type = get_fs_type("ldiskfs");
5490 CERROR("%s: cannot find ldiskfs module\n", name);
5491 GOTO(out, rc = -ENODEV);
5494 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5495 module_put(type->owner);
5497 if (IS_ERR(o->od_mnt)) {
5498 rc = PTR_ERR(o->od_mnt);
5500 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5504 #ifdef HAVE_DEV_SET_RDONLY
5505 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5506 CERROR("%s: underlying device %s is marked as read-only. "
5507 "Setup failed\n", name, dev);
5508 GOTO(out_mnt, rc = -EROFS);
5512 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5513 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5514 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5515 GOTO(out_mnt, rc = -EINVAL);
5518 inode = osd_sb(o)->s_root->d_inode;
5519 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5520 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5521 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5523 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5527 if (lmd_flags & LMD_FLG_NOSCRUB)
5538 OBD_PAGE_FREE(__page);
5543 static struct lu_device *osd_device_fini(const struct lu_env *env,
5544 struct lu_device *d)
5546 struct osd_device *o = osd_dev(d);
5550 osd_shutdown(env, o);
5551 osd_scrub_cleanup(env, o);
5552 osd_obj_map_fini(o);
5558 static int osd_device_init0(const struct lu_env *env,
5559 struct osd_device *o,
5560 struct lustre_cfg *cfg)
5562 struct lu_device *l = osd2lu_dev(o);
5563 struct osd_thread_info *info;
5567 /* if the module was re-loaded, env can loose its keys */
5568 rc = lu_env_refill((struct lu_env *) env);
5571 info = osd_oti_get(env);
5574 l->ld_ops = &osd_lu_ops;
5575 o->od_dt_dev.dd_ops = &osd_dt_ops;
5577 spin_lock_init(&o->od_osfs_lock);
5578 mutex_init(&o->od_otable_mutex);
5579 o->od_osfs_age = cfs_time_shift_64(-1000);
5581 o->od_capa_hash = init_capa_hash();
5582 if (o->od_capa_hash == NULL)
5583 GOTO(out, rc = -ENOMEM);
5585 o->od_read_cache = 1;
5586 o->od_writethrough_cache = 1;
5587 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5589 rc = osd_mount(env, o, cfg);
5593 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5594 sizeof(o->od_svname));
5595 if (cplen >= sizeof(o->od_svname)) {
5600 if (server_name_is_ost(o->od_svname))
5603 rc = osd_obj_map_init(env, o);
5607 rc = lu_site_init(&o->od_site, l);
5609 GOTO(out_compat, rc);
5610 o->od_site.ls_bottom_dev = l;
5612 rc = lu_site_init_finish(&o->od_site);
5616 /* self-repair LMA by default */
5617 o->od_lma_self_repair = 1;
5619 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5620 /* setup scrub, including OI files initialization */
5621 rc = osd_scrub_setup(env, o);
5625 rc = osd_procfs_init(o, o->od_svname);
5627 CERROR("%s: can't initialize procfs: rc = %d\n",
5629 GOTO(out_scrub, rc);
5632 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5634 /* initialize quota slave instance */
5635 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5637 if (IS_ERR(o->od_quota_slave)) {
5638 rc = PTR_ERR(o->od_quota_slave);
5639 o->od_quota_slave = NULL;
5640 GOTO(out_procfs, rc);
5648 osd_scrub_cleanup(env, o);
5650 lu_site_fini(&o->od_site);
5652 osd_obj_map_fini(o);
5656 cleanup_capa_hash(o->od_capa_hash);
5661 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5662 struct lu_device_type *t,
5663 struct lustre_cfg *cfg)
5665 struct osd_device *o;
5670 return ERR_PTR(-ENOMEM);
5672 rc = dt_device_init(&o->od_dt_dev, t);
5674 /* Because the ctx might be revived in dt_device_init,
5675 * refill the env here */
5676 lu_env_refill((struct lu_env *)env);
5677 rc = osd_device_init0(env, o, cfg);
5679 dt_device_fini(&o->od_dt_dev);
5682 if (unlikely(rc != 0))
5685 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5688 static struct lu_device *osd_device_free(const struct lu_env *env,
5689 struct lu_device *d)
5691 struct osd_device *o = osd_dev(d);
5694 cleanup_capa_hash(o->od_capa_hash);
5695 /* XXX: make osd top device in order to release reference */
5696 d->ld_site->ls_top_dev = d;
5697 lu_site_purge(env, d->ld_site, -1);
5698 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5699 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5700 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5702 lu_site_fini(&o->od_site);
5703 dt_device_fini(&o->od_dt_dev);
5708 static int osd_process_config(const struct lu_env *env,
5709 struct lu_device *d, struct lustre_cfg *cfg)
5711 struct osd_device *o = osd_dev(d);
5715 switch(cfg->lcfg_command) {
5717 err = osd_mount(env, o, cfg);
5720 lu_dev_del_linkage(d->ld_site, d);
5721 err = osd_shutdown(env, o);
5730 static int osd_recovery_complete(const struct lu_env *env,
5731 struct lu_device *d)
5733 struct osd_device *osd = osd_dev(d);
5737 if (osd->od_quota_slave == NULL)
5740 /* start qsd instance on recovery completion, this notifies the quota
5741 * slave code that we are about to process new requests now */
5742 rc = qsd_start(env, osd->od_quota_slave);
5747 * we use exports to track all osd users
5749 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5750 struct obd_device *obd, struct obd_uuid *cluuid,
5751 struct obd_connect_data *data, void *localdata)
5753 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5754 struct lustre_handle conn;
5758 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5760 rc = class_connect(&conn, obd, cluuid);
5764 *exp = class_conn2export(&conn);
5766 spin_lock(&osd->od_osfs_lock);
5768 spin_unlock(&osd->od_osfs_lock);
5774 * once last export (we don't count self-export) disappeared
5775 * osd can be released
5777 static int osd_obd_disconnect(struct obd_export *exp)
5779 struct obd_device *obd = exp->exp_obd;
5780 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5781 int rc, release = 0;
5784 /* Only disconnect the underlying layers on the final disconnect. */
5785 spin_lock(&osd->od_osfs_lock);
5787 if (osd->od_connects == 0)
5789 spin_unlock(&osd->od_osfs_lock);
5791 rc = class_disconnect(exp); /* bz 9811 */
5793 if (rc == 0 && release)
5794 class_manual_cleanup(obd);
5798 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5799 struct lu_device *dev)
5801 struct osd_device *osd = osd_dev(dev);
5805 if (osd->od_quota_slave != NULL)
5806 /* set up quota slave objects */
5807 result = qsd_prepare(env, osd->od_quota_slave);
5812 static const struct lu_object_operations osd_lu_obj_ops = {
5813 .loo_object_init = osd_object_init,
5814 .loo_object_delete = osd_object_delete,
5815 .loo_object_release = osd_object_release,
5816 .loo_object_free = osd_object_free,
5817 .loo_object_print = osd_object_print,
5818 .loo_object_invariant = osd_object_invariant
5821 const struct lu_device_operations osd_lu_ops = {
5822 .ldo_object_alloc = osd_object_alloc,
5823 .ldo_process_config = osd_process_config,
5824 .ldo_recovery_complete = osd_recovery_complete,
5825 .ldo_prepare = osd_prepare,
5828 static const struct lu_device_type_operations osd_device_type_ops = {
5829 .ldto_init = osd_type_init,
5830 .ldto_fini = osd_type_fini,
5832 .ldto_start = osd_type_start,
5833 .ldto_stop = osd_type_stop,
5835 .ldto_device_alloc = osd_device_alloc,
5836 .ldto_device_free = osd_device_free,
5838 .ldto_device_init = osd_device_init,
5839 .ldto_device_fini = osd_device_fini
5842 struct lu_device_type osd_device_type = {
5843 .ldt_tags = LU_DEVICE_DT,
5844 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5845 .ldt_ops = &osd_device_type_ops,
5846 .ldt_ctx_tags = LCT_LOCAL,
5850 * lprocfs legacy support.
5852 static struct obd_ops osd_obd_device_ops = {
5853 .o_owner = THIS_MODULE,
5854 .o_connect = osd_obd_connect,
5855 .o_disconnect = osd_obd_disconnect
5858 static int __init osd_mod_init(void)
5860 struct lprocfs_static_vars lvars;
5864 lprocfs_osd_init_vars(&lvars);
5866 rc = lu_kmem_init(ldiskfs_caches);
5870 rc = class_register_type(&osd_obd_device_ops, NULL, NULL,
5871 #ifndef HAVE_ONLY_PROCFS_SEQ
5874 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5876 lu_kmem_fini(ldiskfs_caches);
5880 static void __exit osd_mod_exit(void)
5882 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5883 lu_kmem_fini(ldiskfs_caches);
5886 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5887 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5888 MODULE_LICENSE("GPL");
5890 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);