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 lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
323 struct inode *inode = obj->oo_inode;
324 struct dentry *dentry = &info->oti_obj_dentry;
325 struct lu_fid *fid = NULL;
329 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
332 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
333 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
334 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
335 if (rc == -ENODATA && !fid_is_igif(lu_object_fid(&obj->oo_dt.do_lu)) &&
336 osd_obj2dev(obj)->od_check_ff) {
337 fid = &lma->lma_self_fid;
338 rc = osd_get_idif(info, inode, dentry, fid);
343 if (unlikely(rc == -ENODATA))
351 lustre_lma_swab(lma);
352 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
353 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
354 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
355 "fid = "DFID", ino = %lu\n",
356 osd_obj2dev(obj)->od_svname,
357 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
358 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
361 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
362 fid = &lma->lma_self_fid;
367 unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
368 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
369 osd_obj2dev(obj)->od_svname,
370 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
371 PFID(&lma->lma_self_fid));
378 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
379 const struct lu_fid *fid,
380 const struct lu_object_conf *conf)
382 struct osd_thread_info *info;
383 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
384 struct osd_device *dev;
385 struct osd_idmap_cache *oic;
386 struct osd_inode_id *id;
388 struct osd_scrub *scrub;
389 struct scrub_file *sf;
393 bool triggered = false;
396 LINVRNT(osd_invariant(obj));
397 LASSERT(obj->oo_inode == NULL);
398 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
401 scrub = &dev->od_scrub;
402 sf = &scrub->os_file;
403 info = osd_oti_get(env);
405 oic = &info->oti_cache;
407 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
410 /* For the object is created as locking anchor, or for the object to
411 * be created on disk. No need to osd_oi_lookup() at here because FID
412 * shouldn't never be re-used, if it's really a duplicate FID from
413 * unexpected reason, we should be able to detect it later by calling
414 * do_create->osd_oi_insert(). */
415 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
416 GOTO(out, result = 0);
418 /* Search order: 1. per-thread cache. */
419 if (lu_fid_eq(fid, &oic->oic_fid) &&
420 likely(oic->oic_dev == dev)) {
426 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
427 /* Search order: 2. OI scrub pending list. */
428 result = osd_oii_lookup(dev, fid, id);
433 /* Search order: 3. OI files. */
434 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
435 if (result == -ENOENT) {
436 if (!fid_is_norm(fid) ||
437 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
438 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
440 GOTO(out, result = 0);
451 inode = osd_iget(info, dev, id);
453 result = PTR_ERR(inode);
454 if (result == -ENOENT || result == -ESTALE) {
456 fid_zero(&oic->oic_fid);
457 GOTO(out, result = -ENOENT);
460 /* XXX: There are three possible cases:
461 * 1. Backup/restore caused the OI invalid.
462 * 2. Someone unlinked the object but NOT removed
463 * the OI mapping, such as mount target device
464 * as ldiskfs, and modify something directly.
465 * 3. Someone just removed the object between the
466 * former oi_lookup and the iget. It is normal.
468 * It is diffcult to distinguish the 2nd from the
469 * 1st case. Relatively speaking, the 1st case is
470 * common than the 2nd case, trigger OI scrub. */
471 result = osd_oi_lookup(info, dev, fid, id, true);
473 /* It is the case 1 or 2. */
475 } else if (result == -EREMCHG) {
478 if (unlikely(triggered))
479 GOTO(out, result = saved);
482 if (thread_is_running(&scrub->os_thread)) {
483 result = -EINPROGRESS;
484 } else if (!dev->od_noscrub) {
485 result = osd_scrub_start(dev);
486 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
487 "for "DFID", rc = %d [1]\n",
488 LDISKFS_SB(osd_sb(dev))->s_es->\
489 s_volume_name,PFID(fid), result);
490 if (result == 0 || result == -EALREADY)
491 result = -EINPROGRESS;
496 /* We still have chance to get the valid inode: for the
497 * object which is referenced by remote name entry, the
498 * object on the local MDT will be linked under the dir
499 * of "/REMOTE_PARENT_DIR" with its FID string as name.
501 * We do not know whether the object for the given FID
502 * is referenced by some remote name entry or not, and
503 * especially for DNE II, a multiple-linked object may
504 * have many name entries reside on many MDTs.
506 * To simplify the operation, OSD will not distinguish
507 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
508 * only happened for the RPC from other MDT during the
509 * OI scrub, or for the client side RPC with FID only,
510 * such as FID to path, or from old connected client. */
512 result = osd_lookup_in_remote_parent(info, dev,
525 obj->oo_inode = inode;
526 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
528 result = osd_check_lma(env, obj);
531 obj->oo_inode = NULL;
532 if (result == -EREMCHG)
538 obj->oo_compat_dot_created = 1;
539 obj->oo_compat_dotdot_created = 1;
541 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
542 GOTO(out, result = 0);
544 LASSERT(obj->oo_hl_head == NULL);
545 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
546 if (obj->oo_hl_head == NULL) {
547 obj->oo_inode = NULL;
549 GOTO(out, result = -ENOMEM);
551 GOTO(out, result = 0);
554 LINVRNT(osd_invariant(obj));
559 * Concurrency: shouldn't matter.
561 static void osd_object_init0(struct osd_object *obj)
563 LASSERT(obj->oo_inode != NULL);
564 obj->oo_dt.do_body_ops = &osd_body_ops;
565 obj->oo_dt.do_lu.lo_header->loh_attr |=
566 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
570 * Concurrency: no concurrent access is possible that early in object
573 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
574 const struct lu_object_conf *conf)
576 struct osd_object *obj = osd_obj(l);
579 LINVRNT(osd_invariant(obj));
581 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
582 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
583 l->lo_header->loh_attr |= LOHA_EXISTS;
587 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
588 obj->oo_dt.do_body_ops = &osd_body_ops_new;
589 if (result == 0 && obj->oo_inode != NULL)
590 osd_object_init0(obj);
592 LINVRNT(osd_invariant(obj));
597 * Concurrency: no concurrent access is possible that late in object
600 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
602 struct osd_object *obj = osd_obj(l);
604 LINVRNT(osd_invariant(obj));
606 dt_object_fini(&obj->oo_dt);
607 if (obj->oo_hl_head != NULL)
608 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
613 * Concurrency: no concurrent access is possible that late in object
616 static void osd_index_fini(struct osd_object *o)
618 struct iam_container *bag;
620 if (o->oo_dir != NULL) {
621 bag = &o->oo_dir->od_container;
622 if (o->oo_inode != NULL) {
623 if (bag->ic_object == o->oo_inode)
624 iam_container_fini(bag);
626 OBD_FREE_PTR(o->oo_dir);
632 * Concurrency: no concurrent access is possible that late in object
633 * life-cycle (for all existing callers, that is. New callers have to provide
634 * their own locking.)
636 static int osd_inode_unlinked(const struct inode *inode)
638 return inode->i_nlink == 0;
642 OSD_TXN_OI_DELETE_CREDITS = 20,
643 OSD_TXN_INODE_DELETE_CREDITS = 20
650 #if OSD_THANDLE_STATS
652 * Set time when the handle is allocated
654 static void osd_th_alloced(struct osd_thandle *oth)
656 oth->oth_alloced = cfs_time_current();
660 * Set time when the handle started
662 static void osd_th_started(struct osd_thandle *oth)
664 oth->oth_started = cfs_time_current();
668 * Helper function to convert time interval to microseconds packed in
671 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
675 cfs_duration_usec(cfs_time_sub(end, start), &val);
676 return val.tv_sec * 1000000 + val.tv_usec;
680 * Check whether the we deal with this handle for too long.
682 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
683 cfs_time_t alloced, cfs_time_t started,
686 cfs_time_t now = cfs_time_current();
688 LASSERT(dev != NULL);
690 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
691 interval_to_usec(alloced, started));
692 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
693 interval_to_usec(started, closed));
694 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
695 interval_to_usec(closed, now));
697 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
698 CWARN("transaction handle %p was open for too long: "
700 "alloced "CFS_TIME_T" ,"
701 "started "CFS_TIME_T" ,"
702 "closed "CFS_TIME_T"\n",
703 oth, now, alloced, started, closed);
704 libcfs_debug_dumpstack(NULL);
708 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
710 cfs_time_t __closed = cfs_time_current(); \
711 cfs_time_t __alloced = oth->oth_alloced; \
712 cfs_time_t __started = oth->oth_started; \
715 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
718 #else /* OSD_THANDLE_STATS */
720 #define osd_th_alloced(h) do {} while(0)
721 #define osd_th_started(h) do {} while(0)
722 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
724 #endif /* OSD_THANDLE_STATS */
727 * Concurrency: doesn't access mutable data.
729 static int osd_param_is_not_sane(const struct osd_device *dev,
730 const struct thandle *th)
732 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
734 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
738 * Concurrency: shouldn't matter.
740 static void osd_trans_commit_cb(struct super_block *sb,
741 struct ldiskfs_journal_cb_entry *jcb, int error)
743 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
744 struct thandle *th = &oh->ot_super;
745 struct lu_device *lud = &th->th_dev->dd_lu_dev;
746 struct dt_txn_commit_cb *dcb, *tmp;
748 LASSERT(oh->ot_handle == NULL);
751 CERROR("transaction @0x%p commit error: %d\n", th, error);
753 dt_txn_hook_commit(th);
755 /* call per-transaction callbacks if any */
756 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
757 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
758 "commit callback entry: magic=%x name='%s'\n",
759 dcb->dcb_magic, dcb->dcb_name);
760 cfs_list_del_init(&dcb->dcb_linkage);
761 dcb->dcb_func(NULL, th, dcb, error);
764 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
768 lu_context_exit(&th->th_ctx);
769 lu_context_fini(&th->th_ctx);
773 static struct thandle *osd_trans_create(const struct lu_env *env,
776 struct osd_thread_info *oti = osd_oti_get(env);
777 struct osd_iobuf *iobuf = &oti->oti_iobuf;
778 struct osd_thandle *oh;
782 /* on pending IO in this thread should left from prev. request */
783 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
785 th = ERR_PTR(-ENOMEM);
786 OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
788 oh->ot_quota_trans = &oti->oti_quota_trans;
789 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
793 th->th_tags = LCT_TX_HANDLE;
795 oti->oti_dev = osd_dt_dev(d);
796 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
799 memset(oti->oti_declare_ops, 0,
800 sizeof(oti->oti_declare_ops));
801 memset(oti->oti_declare_ops_rb, 0,
802 sizeof(oti->oti_declare_ops_rb));
803 memset(oti->oti_declare_ops_cred, 0,
804 sizeof(oti->oti_declare_ops_cred));
805 oti->oti_rollback = false;
811 * Concurrency: shouldn't matter.
813 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
816 struct osd_thread_info *oti = osd_oti_get(env);
817 struct osd_device *dev = osd_dt_dev(d);
819 struct osd_thandle *oh;
824 LASSERT(current->journal_info == NULL);
826 oh = container_of0(th, struct osd_thandle, ot_super);
828 LASSERT(oh->ot_handle == NULL);
830 rc = dt_txn_hook_start(env, d, th);
834 if (unlikely(osd_param_is_not_sane(dev, th))) {
835 static unsigned long last_printed;
836 static int last_credits;
838 CWARN("%.16s: too many transaction credits (%d > %d)\n",
839 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
841 osd_journal(dev)->j_max_transaction_buffers);
842 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
843 oti->oti_declare_ops[OSD_OT_CREATE],
844 oti->oti_declare_ops_cred[OSD_OT_CREATE],
845 oti->oti_declare_ops[OSD_OT_DELETE],
846 oti->oti_declare_ops_cred[OSD_OT_DELETE],
847 oti->oti_declare_ops[OSD_OT_DESTROY],
848 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
849 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
850 oti->oti_declare_ops[OSD_OT_ATTR_SET],
851 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
852 oti->oti_declare_ops[OSD_OT_XATTR_SET],
853 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
854 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
855 oti->oti_declare_ops[OSD_OT_WRITE],
856 oti->oti_declare_ops_cred[OSD_OT_WRITE],
857 oti->oti_declare_ops[OSD_OT_PUNCH],
858 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
859 oti->oti_declare_ops[OSD_OT_QUOTA],
860 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
861 CWARN(" insert: %u/%u, delete: %u/%u\n",
862 oti->oti_declare_ops[OSD_OT_INSERT],
863 oti->oti_declare_ops_cred[OSD_OT_INSERT],
864 oti->oti_declare_ops[OSD_OT_DESTROY],
865 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
866 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
867 oti->oti_declare_ops[OSD_OT_REF_ADD],
868 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
869 oti->oti_declare_ops[OSD_OT_REF_DEL],
870 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
872 if (last_credits != oh->ot_credits &&
873 time_after(jiffies, last_printed + 60 * HZ)) {
874 libcfs_debug_dumpstack(NULL);
875 last_credits = oh->ot_credits;
876 last_printed = jiffies;
878 /* XXX Limit the credits to 'max_transaction_buffers', and
879 * let the underlying filesystem to catch the error if
880 * we really need so many credits.
882 * This should be removed when we can calculate the
883 * credits precisely. */
884 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
888 * XXX temporary stuff. Some abstraction layer should
891 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
895 LASSERT(oti->oti_txns == 0);
896 lu_context_init(&th->th_ctx, th->th_tags);
897 lu_context_enter(&th->th_ctx);
899 lu_device_get(&d->dd_lu_dev);
900 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
912 * Concurrency: shouldn't matter.
914 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
917 struct osd_thandle *oh;
918 struct osd_thread_info *oti = osd_oti_get(env);
919 struct osd_iobuf *iobuf = &oti->oti_iobuf;
920 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
923 oh = container_of0(th, struct osd_thandle, ot_super);
926 /* inform the quota slave device that the transaction is
928 qsd_op_end(env, qsd, oh->ot_quota_trans);
929 oh->ot_quota_trans = NULL;
931 if (oh->ot_handle != NULL) {
932 handle_t *hdl = oh->ot_handle;
935 * add commit callback
936 * notice we don't do this in osd_trans_start()
937 * as underlying transaction can change during truncate
939 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
942 LASSERT(oti->oti_txns == 1);
944 rc = dt_txn_hook_stop(env, th);
946 CERROR("Failure in transaction hook: %d\n", rc);
948 /* hook functions might modify th_sync */
949 hdl->h_sync = th->th_sync;
951 oh->ot_handle = NULL;
952 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
953 rc = ldiskfs_journal_stop(hdl));
955 CERROR("Failure to stop transaction: %d\n", rc);
960 /* as we want IO to journal and data IO be concurrent, we don't block
961 * awaiting data IO completion in osd_do_bio(), instead we wait here
962 * once transaction is submitted to the journal. all reqular requests
963 * don't do direct IO (except read/write), thus this wait_event becomes
966 * IMPORTANT: we have to wait till any IO submited by the thread is
967 * completed otherwise iobuf may be corrupted by different request
969 wait_event(iobuf->dr_wait,
970 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
972 rc = iobuf->dr_error;
977 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
979 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
982 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
983 LASSERT(&dcb->dcb_func != NULL);
984 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
990 * Called just before object is freed. Releases all resources except for
991 * object itself (that is released by osd_object_free()).
993 * Concurrency: no concurrent access is possible that late in object
996 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
998 struct osd_object *obj = osd_obj(l);
999 struct inode *inode = obj->oo_inode;
1001 LINVRNT(osd_invariant(obj));
1004 * If object is unlinked remove fid->ino mapping from object index.
1007 osd_index_fini(obj);
1008 if (inode != NULL) {
1009 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1010 qid_t uid = inode->i_uid;
1011 qid_t gid = inode->i_gid;
1014 obj->oo_inode = NULL;
1017 struct osd_thread_info *info = osd_oti_get(env);
1018 struct lquota_id_info *qi = &info->oti_qi;
1020 /* Release granted quota to master if necessary */
1021 qi->lqi_id.qid_uid = uid;
1022 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1024 qi->lqi_id.qid_uid = gid;
1025 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1031 * Concurrency: ->loo_object_release() is called under site spin-lock.
1033 static void osd_object_release(const struct lu_env *env,
1034 struct lu_object *l)
1039 * Concurrency: shouldn't matter.
1041 static int osd_object_print(const struct lu_env *env, void *cookie,
1042 lu_printer_t p, const struct lu_object *l)
1044 struct osd_object *o = osd_obj(l);
1045 struct iam_descr *d;
1047 if (o->oo_dir != NULL)
1048 d = o->oo_dir->od_container.ic_descr;
1051 return (*p)(env, cookie,
1052 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1054 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1055 o->oo_inode ? o->oo_inode->i_generation : 0,
1056 d ? d->id_ops->id_name : "plain");
1060 * Concurrency: shouldn't matter.
1062 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1063 struct obd_statfs *sfs)
1065 struct osd_device *osd = osd_dt_dev(d);
1066 struct super_block *sb = osd_sb(osd);
1067 struct kstatfs *ksfs;
1070 if (unlikely(osd->od_mnt == NULL))
1071 return -EINPROGRESS;
1073 /* osd_lproc.c call this without env, allocate ksfs for that case */
1074 if (unlikely(env == NULL)) {
1075 OBD_ALLOC_PTR(ksfs);
1079 ksfs = &osd_oti_get(env)->oti_ksfs;
1082 spin_lock(&osd->od_osfs_lock);
1083 /* cache 1 second */
1084 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1085 result = sb->s_op->statfs(sb->s_root, ksfs);
1086 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1087 osd->od_osfs_age = cfs_time_current_64();
1088 statfs_pack(&osd->od_statfs, ksfs);
1089 if (sb->s_flags & MS_RDONLY)
1090 sfs->os_state = OS_STATE_READONLY;
1094 if (likely(result == 0))
1095 *sfs = osd->od_statfs;
1096 spin_unlock(&osd->od_osfs_lock);
1098 if (unlikely(env == NULL))
1105 * Estimate space needed for file creations. We assume the largest filename
1106 * which is 2^64 - 1, hence a filename of 20 chars.
1107 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1109 #ifdef __LDISKFS_DIR_REC_LEN
1110 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1112 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1116 * Concurrency: doesn't access mutable data.
1118 static void osd_conf_get(const struct lu_env *env,
1119 const struct dt_device *dev,
1120 struct dt_device_param *param)
1122 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1125 * XXX should be taken from not-yet-existing fs abstraction layer.
1127 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1128 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1129 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1130 param->ddp_block_shift = sb->s_blocksize_bits;
1131 param->ddp_mount_type = LDD_MT_LDISKFS;
1132 param->ddp_maxbytes = sb->s_maxbytes;
1133 /* Overhead estimate should be fairly accurate, so we really take a tiny
1134 * error margin which also avoids fragmenting the filesystem too much */
1135 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1136 /* inode are statically allocated, so per-inode space consumption
1137 * is the space consumed by the directory entry */
1138 param->ddp_inodespace = PER_OBJ_USAGE;
1139 /* per-fragment overhead to be used by the client code */
1140 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1141 param->ddp_mntopts = 0;
1142 if (test_opt(sb, XATTR_USER))
1143 param->ddp_mntopts |= MNTOPT_USERXATTR;
1144 if (test_opt(sb, POSIX_ACL))
1145 param->ddp_mntopts |= MNTOPT_ACL;
1147 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1148 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1149 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1152 param->ddp_max_ea_size = sb->s_blocksize;
1157 * Concurrency: shouldn't matter.
1159 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1161 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1162 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1166 * Start commit for OSD device.
1168 * An implementation of dt_commit_async method for OSD device.
1169 * Asychronously starts underlayng fs sync and thereby a transaction
1172 * \param env environment
1173 * \param d dt device
1175 * \see dt_device_operations
1177 static int osd_commit_async(const struct lu_env *env,
1178 struct dt_device *d)
1180 struct super_block *s = osd_sb(osd_dt_dev(d));
1183 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1184 RETURN(s->s_op->sync_fs(s, 0));
1188 * Concurrency: shouldn't matter.
1191 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1193 struct super_block *sb = osd_sb(osd_dt_dev(d));
1194 struct block_device *dev = sb->s_bdev;
1195 #ifdef HAVE_DEV_SET_RDONLY
1196 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1199 int rc = -EOPNOTSUPP;
1203 #ifdef HAVE_DEV_SET_RDONLY
1204 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1206 if (jdev && (jdev != dev)) {
1207 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1209 dev_set_rdonly(jdev);
1211 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1212 dev_set_rdonly(dev);
1214 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1215 osd_dt_dev(d)->od_svname, (long)dev, rc);
1221 * Concurrency: serialization provided by callers.
1223 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1224 int mode, unsigned long timeout, __u32 alg,
1225 struct lustre_capa_key *keys)
1227 struct osd_device *dev = osd_dt_dev(d);
1230 dev->od_fl_capa = mode;
1231 dev->od_capa_timeout = timeout;
1232 dev->od_capa_alg = alg;
1233 dev->od_capa_keys = keys;
1238 * Note: we do not count into QUOTA here.
1239 * If we mount with --data_journal we may need more.
1241 const int osd_dto_credits_noquota[DTO_NR] = {
1244 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1245 * SINGLEDATA_TRANS_BLOCKS(8)
1246 * XXX Note: maybe iam need more, since iam have more level than
1249 [DTO_INDEX_INSERT] = 16,
1250 [DTO_INDEX_DELETE] = 16,
1254 [DTO_INDEX_UPDATE] = 16,
1256 * Create a object. The same as create object in EXT3.
1257 * DATA_TRANS_BLOCKS(14) +
1258 * INDEX_EXTRA_BLOCKS(8) +
1259 * 3(inode bits, groups, GDT)
1261 [DTO_OBJECT_CREATE] = 25,
1263 * XXX: real credits to be fixed
1265 [DTO_OBJECT_DELETE] = 25,
1267 * Attr set credits (inode)
1269 [DTO_ATTR_SET_BASE] = 1,
1271 * Xattr set. The same as xattr of EXT3.
1272 * DATA_TRANS_BLOCKS(14)
1273 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1274 * are also counted in. Do not know why?
1276 [DTO_XATTR_SET] = 14,
1279 * credits for inode change during write.
1281 [DTO_WRITE_BASE] = 3,
1283 * credits for single block write.
1285 [DTO_WRITE_BLOCK] = 14,
1287 * Attr set credits for chown.
1288 * This is extra credits for setattr, and it is null without quota
1290 [DTO_ATTR_SET_CHOWN]= 0
1293 static const struct dt_device_operations osd_dt_ops = {
1294 .dt_root_get = osd_root_get,
1295 .dt_statfs = osd_statfs,
1296 .dt_trans_create = osd_trans_create,
1297 .dt_trans_start = osd_trans_start,
1298 .dt_trans_stop = osd_trans_stop,
1299 .dt_trans_cb_add = osd_trans_cb_add,
1300 .dt_conf_get = osd_conf_get,
1301 .dt_sync = osd_sync,
1303 .dt_commit_async = osd_commit_async,
1304 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1307 static void osd_object_read_lock(const struct lu_env *env,
1308 struct dt_object *dt, unsigned role)
1310 struct osd_object *obj = osd_dt_obj(dt);
1311 struct osd_thread_info *oti = osd_oti_get(env);
1313 LINVRNT(osd_invariant(obj));
1315 LASSERT(obj->oo_owner != env);
1316 down_read_nested(&obj->oo_sem, role);
1318 LASSERT(obj->oo_owner == NULL);
1322 static void osd_object_write_lock(const struct lu_env *env,
1323 struct dt_object *dt, unsigned role)
1325 struct osd_object *obj = osd_dt_obj(dt);
1326 struct osd_thread_info *oti = osd_oti_get(env);
1328 LINVRNT(osd_invariant(obj));
1330 LASSERT(obj->oo_owner != env);
1331 down_write_nested(&obj->oo_sem, role);
1333 LASSERT(obj->oo_owner == NULL);
1334 obj->oo_owner = env;
1338 static void osd_object_read_unlock(const struct lu_env *env,
1339 struct dt_object *dt)
1341 struct osd_object *obj = osd_dt_obj(dt);
1342 struct osd_thread_info *oti = osd_oti_get(env);
1344 LINVRNT(osd_invariant(obj));
1346 LASSERT(oti->oti_r_locks > 0);
1348 up_read(&obj->oo_sem);
1351 static void osd_object_write_unlock(const struct lu_env *env,
1352 struct dt_object *dt)
1354 struct osd_object *obj = osd_dt_obj(dt);
1355 struct osd_thread_info *oti = osd_oti_get(env);
1357 LINVRNT(osd_invariant(obj));
1359 LASSERT(obj->oo_owner == env);
1360 LASSERT(oti->oti_w_locks > 0);
1362 obj->oo_owner = NULL;
1363 up_write(&obj->oo_sem);
1366 static int osd_object_write_locked(const struct lu_env *env,
1367 struct dt_object *dt)
1369 struct osd_object *obj = osd_dt_obj(dt);
1371 LINVRNT(osd_invariant(obj));
1373 return obj->oo_owner == env;
1376 static int capa_is_sane(const struct lu_env *env,
1377 struct osd_device *dev,
1378 struct lustre_capa *capa,
1379 struct lustre_capa_key *keys)
1381 struct osd_thread_info *oti = osd_oti_get(env);
1382 struct lustre_capa *tcapa = &oti->oti_capa;
1383 struct obd_capa *oc;
1387 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1389 if (capa_is_expired(oc)) {
1390 DEBUG_CAPA(D_ERROR, capa, "expired");
1397 if (capa_is_expired_sec(capa)) {
1398 DEBUG_CAPA(D_ERROR, capa, "expired");
1402 spin_lock(&capa_lock);
1403 for (i = 0; i < 2; i++) {
1404 if (keys[i].lk_keyid == capa->lc_keyid) {
1405 oti->oti_capa_key = keys[i];
1409 spin_unlock(&capa_lock);
1412 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1416 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1420 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1421 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1425 oc = capa_add(dev->od_capa_hash, capa);
1431 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1432 struct lustre_capa *capa, __u64 opc)
1434 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1435 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1436 struct lu_capainfo *lci;
1439 if (!osd->od_fl_capa)
1442 if (capa == BYPASS_CAPA)
1445 lci = lu_capainfo_get(env);
1446 if (unlikely(lci == NULL))
1449 if (lci->lci_auth == LC_ID_NONE)
1453 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1454 osd_name(osd), PFID(fid), -EACCES);
1458 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1459 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1464 if (!capa_opc_supported(capa, opc)) {
1465 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1469 rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1471 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1478 static struct timespec *osd_inode_time(const struct lu_env *env,
1479 struct inode *inode, __u64 seconds)
1481 struct osd_thread_info *oti = osd_oti_get(env);
1482 struct timespec *t = &oti->oti_time;
1484 t->tv_sec = seconds;
1486 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1491 static void osd_inode_getattr(const struct lu_env *env,
1492 struct inode *inode, struct lu_attr *attr)
1494 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1495 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1496 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1499 attr->la_atime = LTIME_S(inode->i_atime);
1500 attr->la_mtime = LTIME_S(inode->i_mtime);
1501 attr->la_ctime = LTIME_S(inode->i_ctime);
1502 attr->la_mode = inode->i_mode;
1503 attr->la_size = i_size_read(inode);
1504 attr->la_blocks = inode->i_blocks;
1505 attr->la_uid = inode->i_uid;
1506 attr->la_gid = inode->i_gid;
1507 attr->la_flags = LDISKFS_I(inode)->i_flags;
1508 attr->la_nlink = inode->i_nlink;
1509 attr->la_rdev = inode->i_rdev;
1510 attr->la_blksize = 1 << inode->i_blkbits;
1511 attr->la_blkbits = inode->i_blkbits;
1514 static int osd_attr_get(const struct lu_env *env,
1515 struct dt_object *dt,
1516 struct lu_attr *attr,
1517 struct lustre_capa *capa)
1519 struct osd_object *obj = osd_dt_obj(dt);
1521 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1522 LINVRNT(osd_invariant(obj));
1524 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1527 spin_lock(&obj->oo_guard);
1528 osd_inode_getattr(env, obj->oo_inode, attr);
1529 spin_unlock(&obj->oo_guard);
1533 static int osd_declare_attr_set(const struct lu_env *env,
1534 struct dt_object *dt,
1535 const struct lu_attr *attr,
1536 struct thandle *handle)
1538 struct osd_thandle *oh;
1539 struct osd_object *obj;
1540 struct osd_thread_info *info = osd_oti_get(env);
1541 struct lquota_id_info *qi = &info->oti_qi;
1547 LASSERT(dt != NULL);
1548 LASSERT(handle != NULL);
1550 obj = osd_dt_obj(dt);
1551 LASSERT(osd_invariant(obj));
1553 oh = container_of0(handle, struct osd_thandle, ot_super);
1554 LASSERT(oh->ot_handle == NULL);
1556 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1557 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1559 if (attr == NULL || obj->oo_inode == NULL)
1562 bspace = obj->oo_inode->i_blocks;
1563 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1564 bspace = toqb(bspace);
1566 /* Changing ownership is always preformed by super user, it should not
1569 * We still need to call the osd_declare_qid() to calculate the journal
1570 * credits for updating quota accounting files and to trigger quota
1571 * space adjustment once the operation is completed.*/
1572 if ((attr->la_valid & LA_UID) != 0 &&
1573 attr->la_uid != obj->oo_inode->i_uid) {
1574 qi->lqi_type = USRQUOTA;
1576 /* inode accounting */
1577 qi->lqi_is_blk = false;
1579 /* one more inode for the new owner ... */
1580 qi->lqi_id.qid_uid = attr->la_uid;
1582 allocated = (attr->la_uid == 0) ? true : false;
1583 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1584 if (rc == -EDQUOT || rc == -EINPROGRESS)
1589 /* and one less inode for the current uid */
1590 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1592 rc = osd_declare_qid(env, oh, qi, true, NULL);
1593 if (rc == -EDQUOT || rc == -EINPROGRESS)
1598 /* block accounting */
1599 qi->lqi_is_blk = true;
1601 /* more blocks for the new owner ... */
1602 qi->lqi_id.qid_uid = attr->la_uid;
1603 qi->lqi_space = bspace;
1604 allocated = (attr->la_uid == 0) ? true : false;
1605 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1606 if (rc == -EDQUOT || rc == -EINPROGRESS)
1611 /* and finally less blocks for the current owner */
1612 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1613 qi->lqi_space = -bspace;
1614 rc = osd_declare_qid(env, oh, qi, true, NULL);
1615 if (rc == -EDQUOT || rc == -EINPROGRESS)
1621 if (attr->la_valid & LA_GID &&
1622 attr->la_gid != obj->oo_inode->i_gid) {
1623 qi->lqi_type = GRPQUOTA;
1625 /* inode accounting */
1626 qi->lqi_is_blk = false;
1628 /* one more inode for the new group owner ... */
1629 qi->lqi_id.qid_gid = attr->la_gid;
1631 allocated = (attr->la_gid == 0) ? true : false;
1632 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1633 if (rc == -EDQUOT || rc == -EINPROGRESS)
1638 /* and one less inode for the current gid */
1639 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1641 rc = osd_declare_qid(env, oh, qi, true, NULL);
1642 if (rc == -EDQUOT || rc == -EINPROGRESS)
1647 /* block accounting */
1648 qi->lqi_is_blk = true;
1650 /* more blocks for the new owner ... */
1651 qi->lqi_id.qid_gid = attr->la_gid;
1652 qi->lqi_space = bspace;
1653 allocated = (attr->la_gid == 0) ? true : false;
1654 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1655 if (rc == -EDQUOT || rc == -EINPROGRESS)
1660 /* and finally less blocks for the current owner */
1661 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1662 qi->lqi_space = -bspace;
1663 rc = osd_declare_qid(env, oh, qi, true, NULL);
1664 if (rc == -EDQUOT || rc == -EINPROGRESS)
1673 static int osd_inode_setattr(const struct lu_env *env,
1674 struct inode *inode, const struct lu_attr *attr)
1678 bits = attr->la_valid;
1680 if (bits & LA_ATIME)
1681 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1682 if (bits & LA_CTIME)
1683 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1684 if (bits & LA_MTIME)
1685 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1686 if (bits & LA_SIZE) {
1687 LDISKFS_I(inode)->i_disksize = attr->la_size;
1688 i_size_write(inode, attr->la_size);
1692 /* OSD should not change "i_blocks" which is used by quota.
1693 * "i_blocks" should be changed by ldiskfs only. */
1694 if (bits & LA_BLOCKS)
1695 inode->i_blocks = attr->la_blocks;
1698 inode->i_mode = (inode->i_mode & S_IFMT) |
1699 (attr->la_mode & ~S_IFMT);
1701 inode->i_uid = attr->la_uid;
1703 inode->i_gid = attr->la_gid;
1704 if (bits & LA_NLINK)
1705 set_nlink(inode, attr->la_nlink);
1707 inode->i_rdev = attr->la_rdev;
1709 if (bits & LA_FLAGS) {
1710 /* always keep S_NOCMTIME */
1711 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1717 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1719 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1720 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1725 if (attr->la_valid & LA_UID)
1726 iattr.ia_valid |= ATTR_UID;
1727 if (attr->la_valid & LA_GID)
1728 iattr.ia_valid |= ATTR_GID;
1729 iattr.ia_uid = attr->la_uid;
1730 iattr.ia_gid = attr->la_gid;
1732 rc = ll_vfs_dq_transfer(inode, &iattr);
1734 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1735 "enforcement enabled on the ldiskfs filesystem?",
1736 inode->i_sb->s_id, rc);
1743 static int osd_attr_set(const struct lu_env *env,
1744 struct dt_object *dt,
1745 const struct lu_attr *attr,
1746 struct thandle *handle,
1747 struct lustre_capa *capa)
1749 struct osd_object *obj = osd_dt_obj(dt);
1750 struct inode *inode;
1753 LASSERT(handle != NULL);
1754 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1755 LASSERT(osd_invariant(obj));
1757 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1760 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1762 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1763 struct osd_thread_info *oti = osd_oti_get(env);
1764 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1765 struct lu_fid *fid1 = &oti->oti_fid;
1766 struct osd_inode_id *id = &oti->oti_id;
1767 struct iam_path_descr *ipd;
1768 struct iam_container *bag;
1769 struct osd_thandle *oh;
1772 fid_cpu_to_be(fid1, fid0);
1773 memset(id, 1, sizeof(*id));
1774 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1775 fid0)->oi_dir.od_container;
1776 ipd = osd_idx_ipd_get(env, bag);
1777 if (unlikely(ipd == NULL))
1780 oh = container_of0(handle, struct osd_thandle, ot_super);
1781 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1782 (const struct iam_rec *)id, ipd);
1783 osd_ipd_put(env, bag, ipd);
1784 return(rc > 0 ? 0 : rc);
1787 inode = obj->oo_inode;
1788 ll_vfs_dq_init(inode);
1790 rc = osd_quota_transfer(inode, attr);
1794 spin_lock(&obj->oo_guard);
1795 rc = osd_inode_setattr(env, inode, attr);
1796 spin_unlock(&obj->oo_guard);
1799 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1803 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1804 struct osd_object *obj,
1805 const char *name, const int namelen)
1807 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1810 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1811 umode_t mode, struct dt_allocation_hint *hint,
1815 struct osd_device *osd = osd_obj2dev(obj);
1816 struct osd_thandle *oth;
1817 struct dt_object *parent = NULL;
1818 struct inode *inode;
1820 LINVRNT(osd_invariant(obj));
1821 LASSERT(obj->oo_inode == NULL);
1822 LASSERT(obj->oo_hl_head == NULL);
1824 if (S_ISDIR(mode) && ldiskfs_pdo) {
1825 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1826 if (obj->oo_hl_head == NULL)
1830 oth = container_of(th, struct osd_thandle, ot_super);
1831 LASSERT(oth->ot_handle->h_transaction != NULL);
1833 if (hint && hint->dah_parent)
1834 parent = hint->dah_parent;
1836 inode = ldiskfs_create_inode(oth->ot_handle,
1837 parent ? osd_dt_obj(parent)->oo_inode :
1838 osd_sb(osd)->s_root->d_inode,
1840 if (!IS_ERR(inode)) {
1841 /* Do not update file c/mtime in ldiskfs.
1842 * NB: don't need any lock because no contention at this
1844 inode->i_flags |= S_NOCMTIME;
1846 /* For new created object, it must be consistent,
1847 * and it is unnecessary to scrub against it. */
1848 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1849 obj->oo_inode = inode;
1852 if (obj->oo_hl_head != NULL) {
1853 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1854 obj->oo_hl_head = NULL;
1856 result = PTR_ERR(inode);
1858 LINVRNT(osd_invariant(obj));
1866 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1867 struct lu_attr *attr,
1868 struct dt_allocation_hint *hint,
1869 struct dt_object_format *dof,
1873 struct osd_thandle *oth;
1874 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1876 LASSERT(S_ISDIR(attr->la_mode));
1878 oth = container_of(th, struct osd_thandle, ot_super);
1879 LASSERT(oth->ot_handle->h_transaction != NULL);
1880 result = osd_mkfile(info, obj, mode, hint, th);
1885 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1886 struct lu_attr *attr,
1887 struct dt_allocation_hint *hint,
1888 struct dt_object_format *dof,
1892 struct osd_thandle *oth;
1893 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1895 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1897 LASSERT(S_ISREG(attr->la_mode));
1899 oth = container_of(th, struct osd_thandle, ot_super);
1900 LASSERT(oth->ot_handle->h_transaction != NULL);
1902 result = osd_mkfile(info, obj, mode, hint, th);
1904 LASSERT(obj->oo_inode != NULL);
1905 if (feat->dif_flags & DT_IND_VARKEY)
1906 result = iam_lvar_create(obj->oo_inode,
1907 feat->dif_keysize_max,
1909 feat->dif_recsize_max,
1912 result = iam_lfix_create(obj->oo_inode,
1913 feat->dif_keysize_max,
1915 feat->dif_recsize_max,
1922 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1923 struct lu_attr *attr,
1924 struct dt_allocation_hint *hint,
1925 struct dt_object_format *dof,
1928 LASSERT(S_ISREG(attr->la_mode));
1929 return osd_mkfile(info, obj, (attr->la_mode &
1930 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1933 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1934 struct lu_attr *attr,
1935 struct dt_allocation_hint *hint,
1936 struct dt_object_format *dof,
1939 LASSERT(S_ISLNK(attr->la_mode));
1940 return osd_mkfile(info, obj, (attr->la_mode &
1941 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1944 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1945 struct lu_attr *attr,
1946 struct dt_allocation_hint *hint,
1947 struct dt_object_format *dof,
1950 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1953 LINVRNT(osd_invariant(obj));
1954 LASSERT(obj->oo_inode == NULL);
1955 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1956 S_ISFIFO(mode) || S_ISSOCK(mode));
1958 result = osd_mkfile(info, obj, mode, hint, th);
1960 LASSERT(obj->oo_inode != NULL);
1962 * This inode should be marked dirty for i_rdev. Currently
1963 * that is done in the osd_attr_init().
1965 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1968 LINVRNT(osd_invariant(obj));
1972 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1974 struct dt_allocation_hint *hint,
1975 struct dt_object_format *dof,
1978 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1980 osd_obj_type_f result;
1996 result = osd_mk_index;
2007 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2008 struct dt_object *parent, struct dt_object *child,
2013 memset(ah, 0, sizeof(*ah));
2014 ah->dah_parent = parent;
2015 ah->dah_mode = child_mode;
2018 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2019 struct lu_attr *attr, struct dt_object_format *dof)
2021 struct inode *inode = obj->oo_inode;
2022 __u64 valid = attr->la_valid;
2025 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2027 if (dof->dof_type != DFT_NODE)
2028 attr->la_valid &= ~LA_RDEV;
2029 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2030 attr->la_valid &= ~LA_ATIME;
2031 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2032 attr->la_valid &= ~LA_CTIME;
2033 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2034 attr->la_valid &= ~LA_MTIME;
2036 result = osd_quota_transfer(inode, attr);
2040 if (attr->la_valid != 0) {
2041 result = osd_inode_setattr(info->oti_env, inode, attr);
2043 * The osd_inode_setattr() should always succeed here. The
2044 * only error that could be returned is EDQUOT when we are
2045 * trying to change the UID or GID of the inode. However, this
2046 * should not happen since quota enforcement is no longer
2047 * enabled on ldiskfs (lquota takes care of it).
2049 LASSERTF(result == 0, "%d", result);
2050 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2053 attr->la_valid = valid;
2057 * Helper function for osd_object_create()
2059 * \retval 0, on success
2061 static int __osd_object_create(struct osd_thread_info *info,
2062 struct osd_object *obj, struct lu_attr *attr,
2063 struct dt_allocation_hint *hint,
2064 struct dt_object_format *dof,
2070 /* we drop umask so that permissions we pass are not affected */
2071 umask = current->fs->umask;
2072 current->fs->umask = 0;
2074 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2077 osd_attr_init(info, obj, attr, dof);
2078 osd_object_init0(obj);
2080 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2081 unlock_new_inode(obj->oo_inode);
2084 /* restore previous umask value */
2085 current->fs->umask = umask;
2091 * Helper function for osd_object_create()
2093 * \retval 0, on success
2095 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2096 const struct lu_fid *fid, struct thandle *th)
2098 struct osd_thread_info *info = osd_oti_get(env);
2099 struct osd_inode_id *id = &info->oti_id;
2100 struct osd_device *osd = osd_obj2dev(obj);
2102 LASSERT(obj->oo_inode != NULL);
2104 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2105 return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2108 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2109 obd_seq seq, struct lu_seq_range *range)
2111 struct seq_server_site *ss = osd_seq_site(osd);
2114 if (fid_seq_is_idif(seq)) {
2115 fld_range_set_ost(range);
2116 range->lsr_index = idif_ost_idx(seq);
2120 if (!fid_seq_in_fldb(seq)) {
2121 fld_range_set_mdt(range);
2123 /* FIXME: If ss is NULL, it suppose not get lsr_index
2125 range->lsr_index = ss->ss_node_id;
2129 LASSERT(ss != NULL);
2130 fld_range_set_any(range);
2131 rc = fld_server_lookup(env, ss->ss_server_fld, seq, range);
2133 CERROR("%s: cannot find FLD range for "LPX64": rc = %d\n",
2134 osd_name(osd), seq, rc);
2140 * Concurrency: no external locking is necessary.
2142 static int osd_declare_object_create(const struct lu_env *env,
2143 struct dt_object *dt,
2144 struct lu_attr *attr,
2145 struct dt_allocation_hint *hint,
2146 struct dt_object_format *dof,
2147 struct thandle *handle)
2149 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2150 struct osd_thandle *oh;
2154 LASSERT(handle != NULL);
2156 oh = container_of0(handle, struct osd_thandle, ot_super);
2157 LASSERT(oh->ot_handle == NULL);
2159 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2160 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2161 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2162 lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2163 /* Reuse idle OI block may cause additional one OI block
2165 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2166 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2168 /* If this is directory, then we expect . and .. to be inserted as
2169 * well. The one directory block always needs to be created for the
2170 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2171 * block), there is no danger of needing a tree for the first block.
2173 if (attr && S_ISDIR(attr->la_mode)) {
2174 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2175 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2176 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2182 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2183 false, false, NULL, false);
2187 /* It does fld look up inside declare, and the result will be
2188 * added to fld cache, so the following fld lookup inside insert
2189 * does not need send RPC anymore, so avoid send rpc with holding
2191 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2192 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2193 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2194 fid_seq(lu_object_fid(&dt->do_lu)), range);
2200 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2201 struct lu_attr *attr,
2202 struct dt_allocation_hint *hint,
2203 struct dt_object_format *dof,
2206 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2207 struct osd_object *obj = osd_dt_obj(dt);
2208 struct osd_thread_info *info = osd_oti_get(env);
2213 LINVRNT(osd_invariant(obj));
2214 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2215 LASSERT(osd_write_locked(env, obj));
2216 LASSERT(th != NULL);
2218 if (unlikely(fid_is_acct(fid)))
2219 /* Quota files can't be created from the kernel any more,
2220 * 'tune2fs -O quota' will take care of creating them */
2223 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2224 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2226 result = __osd_object_create(info, obj, attr, hint, dof, th);
2228 result = __osd_oi_insert(env, obj, fid, th);
2230 LASSERT(ergo(result == 0,
2231 dt_object_exists(dt) && !dt_object_remote(dt)));
2233 LASSERT(osd_invariant(obj));
2238 * Called to destroy on-disk representation of the object
2240 * Concurrency: must be locked
2242 static int osd_declare_object_destroy(const struct lu_env *env,
2243 struct dt_object *dt,
2246 struct osd_object *obj = osd_dt_obj(dt);
2247 struct inode *inode = obj->oo_inode;
2248 struct osd_thandle *oh;
2252 oh = container_of0(th, struct osd_thandle, ot_super);
2253 LASSERT(oh->ot_handle == NULL);
2256 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2257 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2258 /* Recycle idle OI leaf may cause additional three OI blocks
2260 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2261 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2262 /* one less inode */
2263 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2264 false, true, NULL, false);
2267 /* data to be truncated */
2268 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2269 true, true, NULL, false);
2273 static int osd_object_destroy(const struct lu_env *env,
2274 struct dt_object *dt,
2277 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2278 struct osd_object *obj = osd_dt_obj(dt);
2279 struct inode *inode = obj->oo_inode;
2280 struct osd_device *osd = osd_obj2dev(obj);
2281 struct osd_thandle *oh;
2285 oh = container_of0(th, struct osd_thandle, ot_super);
2286 LASSERT(oh->ot_handle);
2288 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2290 if (unlikely(fid_is_acct(fid)))
2293 if (S_ISDIR(inode->i_mode)) {
2294 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2295 /* it will check/delete the inode from remote parent,
2296 * how to optimize it? unlink performance impaction XXX */
2297 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2298 if (result != 0 && result != -ENOENT) {
2299 CERROR("%s: delete inode "DFID": rc = %d\n",
2300 osd_name(osd), PFID(fid), result);
2302 spin_lock(&obj->oo_guard);
2304 spin_unlock(&obj->oo_guard);
2305 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2308 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2310 result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2312 /* XXX: add to ext3 orphan list */
2313 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2315 /* not needed in the cache anymore */
2316 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2322 * Put the fid into lustre_mdt_attrs, and then place the structure
2323 * inode's ea. This fid should not be altered during the life time
2326 * \retval +ve, on success
2327 * \retval -ve, on error
2329 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2331 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2332 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2334 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2338 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2341 lustre_lma_init(lma, fid, compat, incompat);
2342 lustre_lma_swab(lma);
2344 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2346 /* LMA may already exist, but we need to check that all the
2347 * desired compat/incompat flags have been added. */
2348 if (unlikely(rc == -EEXIST)) {
2349 if (compat == 0 && incompat == 0)
2352 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2353 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2358 lustre_lma_swab(lma);
2359 if (!(~lma->lma_compat & compat) &&
2360 !(~lma->lma_incompat & incompat))
2363 lma->lma_compat |= compat;
2364 lma->lma_incompat |= incompat;
2365 lustre_lma_swab(lma);
2366 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2367 sizeof(*lma), XATTR_REPLACE);
2374 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2375 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2376 * To have compatilibility with 1.8 ldiskfs driver we need to have
2377 * magic number at start of fid data.
2378 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2381 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2382 const struct dt_rec *fid)
2384 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2385 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2386 param->edp_magic = 0;
2390 param->edp_magic = LDISKFS_LUFID_MAGIC;
2391 param->edp_len = sizeof(struct lu_fid) + 1;
2392 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2396 * Try to read the fid from inode ea into dt_rec.
2398 * \param fid object fid.
2400 * \retval 0 on success
2402 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2403 __u32 ino, struct lu_fid *fid,
2404 struct osd_inode_id *id)
2406 struct osd_thread_info *info = osd_oti_get(env);
2407 struct inode *inode;
2410 osd_id_gen(id, ino, OSD_OII_NOGEN);
2411 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2413 RETURN(PTR_ERR(inode));
2419 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2421 struct inode *parent_dir,
2422 const struct dt_rec *dot_fid,
2423 const struct dt_rec *dot_dot_fid,
2424 struct osd_thandle *oth)
2426 struct ldiskfs_dentry_param *dot_ldp;
2427 struct ldiskfs_dentry_param *dot_dot_ldp;
2429 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2430 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2432 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2433 dot_ldp->edp_magic = 0;
2434 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2435 dir, dot_ldp, dot_dot_ldp);
2439 * Create an local agent inode for remote entry
2441 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2442 struct osd_device *osd,
2443 struct osd_object *pobj,
2444 const struct lu_fid *fid,
2447 struct osd_thread_info *info = osd_oti_get(env);
2448 struct inode *local;
2449 struct osd_thandle *oh;
2454 oh = container_of(th, struct osd_thandle, ot_super);
2455 LASSERT(oh->ot_handle->h_transaction != NULL);
2457 /* FIXME: Insert index api needs to know the mode of
2458 * the remote object. Just use S_IFDIR for now */
2459 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2460 if (IS_ERR(local)) {
2461 CERROR("%s: create local error %d\n", osd_name(osd),
2462 (int)PTR_ERR(local));
2466 /* Set special LMA flag for local agent inode */
2467 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2469 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2470 osd_name(osd), PFID(fid), rc);
2471 RETURN(ERR_PTR(rc));
2474 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2475 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2476 (const struct dt_rec *)fid, oh);
2478 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2479 osd_name(osd), PFID(fid), rc);
2480 RETURN(ERR_PTR(rc));
2487 * Delete local agent inode for remote entry
2489 static int osd_delete_local_agent_inode(const struct lu_env *env,
2490 struct osd_device *osd,
2491 const struct lu_fid *fid,
2492 __u32 ino, struct osd_thandle *oh)
2494 struct osd_thread_info *oti = osd_oti_get(env);
2495 struct osd_inode_id *id = &oti->oti_id;
2496 struct inode *inode;
2499 id->oii_ino = le32_to_cpu(ino);
2500 id->oii_gen = OSD_OII_NOGEN;
2501 inode = osd_iget(oti, osd, id);
2502 if (IS_ERR(inode)) {
2503 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2504 PFID(fid), id->oii_ino, id->oii_gen);
2505 RETURN(PTR_ERR(inode));
2509 mark_inode_dirty(inode);
2510 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2511 osd_name(osd), PFID(fid), inode->i_ino);
2517 * OSD layer object create function for interoperability mode (b11826).
2518 * This is mostly similar to osd_object_create(). Only difference being, fid is
2519 * inserted into inode ea here.
2521 * \retval 0, on success
2522 * \retval -ve, on error
2524 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2525 struct lu_attr *attr,
2526 struct dt_allocation_hint *hint,
2527 struct dt_object_format *dof,
2530 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2531 struct osd_object *obj = osd_dt_obj(dt);
2532 struct osd_thread_info *info = osd_oti_get(env);
2537 LASSERT(osd_invariant(obj));
2538 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2539 LASSERT(osd_write_locked(env, obj));
2540 LASSERT(th != NULL);
2542 if (unlikely(fid_is_acct(fid)))
2543 /* Quota files can't be created from the kernel any more,
2544 * 'tune2fs -O quota' will take care of creating them */
2547 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2548 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2550 result = __osd_object_create(info, obj, attr, hint, dof, th);
2552 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2553 fid_is_on_ost(info, osd_obj2dev(obj),
2554 fid, OI_CHECK_FLD) ?
2555 LMAC_FID_ON_OST : 0, 0);
2558 result = __osd_oi_insert(env, obj, fid, th);
2560 LASSERT(ergo(result == 0,
2561 dt_object_exists(dt) && !dt_object_remote(dt)));
2562 LINVRNT(osd_invariant(obj));
2566 static int osd_declare_object_ref_add(const struct lu_env *env,
2567 struct dt_object *dt,
2568 struct thandle *handle)
2570 struct osd_thandle *oh;
2572 /* it's possible that object doesn't exist yet */
2573 LASSERT(handle != NULL);
2575 oh = container_of0(handle, struct osd_thandle, ot_super);
2576 LASSERT(oh->ot_handle == NULL);
2578 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2579 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2585 * Concurrency: @dt is write locked.
2587 static int osd_object_ref_add(const struct lu_env *env,
2588 struct dt_object *dt, struct thandle *th)
2590 struct osd_object *obj = osd_dt_obj(dt);
2591 struct inode *inode = obj->oo_inode;
2592 bool need_dirty = false;
2595 LINVRNT(osd_invariant(obj));
2596 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2597 LASSERT(osd_write_locked(env, obj));
2598 LASSERT(th != NULL);
2600 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2602 /* This based on ldiskfs_inc_count(), which is not exported.
2604 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2605 * (65000) subdirectories by storing "1" in i_nlink if the link count
2606 * would otherwise overflow. Directory tranversal tools understand
2607 * that (st_nlink == 1) indicates that the filesystem dose not track
2608 * hard links count on the directory, and will not abort subdirectory
2609 * scanning early once (st_nlink - 2) subdirs have been found.
2611 * This also has to properly handle the case of inodes with nlink == 0
2612 * in case they are being linked into the PENDING directory
2614 spin_lock(&obj->oo_guard);
2615 if (unlikely(!S_ISDIR(inode->i_mode) &&
2616 inode->i_nlink >= LDISKFS_LINK_MAX)) {
2617 /* MDD should have checked this, but good to be safe */
2619 } else if (unlikely(inode->i_nlink == 0 ||
2620 (S_ISDIR(inode->i_mode) &&
2621 inode->i_nlink >= LDISKFS_LINK_MAX))) {
2622 /* inc_nlink from 0 may cause WARN_ON */
2623 set_nlink(inode, 1);
2625 } else if (!S_ISDIR(inode->i_mode) ||
2626 (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2629 } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2631 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2632 spin_unlock(&obj->oo_guard);
2635 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2637 LINVRNT(osd_invariant(obj));
2642 static int osd_declare_object_ref_del(const struct lu_env *env,
2643 struct dt_object *dt,
2644 struct thandle *handle)
2646 struct osd_thandle *oh;
2648 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2649 LASSERT(handle != NULL);
2651 oh = container_of0(handle, struct osd_thandle, ot_super);
2652 LASSERT(oh->ot_handle == NULL);
2654 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2655 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2661 * Concurrency: @dt is write locked.
2663 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2666 struct osd_object *obj = osd_dt_obj(dt);
2667 struct inode *inode = obj->oo_inode;
2668 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2670 LINVRNT(osd_invariant(obj));
2671 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2672 LASSERT(osd_write_locked(env, obj));
2673 LASSERT(th != NULL);
2675 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2677 spin_lock(&obj->oo_guard);
2678 /* That can be result of upgrade from old Lustre version and
2679 * applied only to local files. Just skip this ref_del call.
2680 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2681 if (inode->i_nlink == 0) {
2682 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2683 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2684 ", maybe an upgraded file? (LU-3915)\n",
2685 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2686 spin_unlock(&obj->oo_guard);
2690 /* This based on ldiskfs_dec_count(), which is not exported.
2692 * If a directory already has nlink == 1, then do not drop the nlink
2693 * count to 0, even temporarily, to avoid race conditions with other
2694 * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2696 * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2698 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2701 spin_unlock(&obj->oo_guard);
2702 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2703 LINVRNT(osd_invariant(obj));
2705 spin_unlock(&obj->oo_guard);
2712 * Get the 64-bit version for an inode.
2714 static int osd_object_version_get(const struct lu_env *env,
2715 struct dt_object *dt, dt_obj_version_t *ver)
2717 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2719 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2720 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2721 *ver = LDISKFS_I(inode)->i_fs_version;
2726 * Concurrency: @dt is read locked.
2728 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2729 struct lu_buf *buf, const char *name,
2730 struct lustre_capa *capa)
2732 struct osd_object *obj = osd_dt_obj(dt);
2733 struct inode *inode = obj->oo_inode;
2734 struct osd_thread_info *info = osd_oti_get(env);
2735 struct dentry *dentry = &info->oti_obj_dentry;
2737 /* version get is not real XATTR but uses xattr API */
2738 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2739 /* for version we are just using xattr API but change inode
2741 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2742 osd_object_version_get(env, dt, buf->lb_buf);
2743 return sizeof(dt_obj_version_t);
2746 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2747 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2749 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2752 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2756 static int osd_declare_xattr_set(const struct lu_env *env,
2757 struct dt_object *dt,
2758 const struct lu_buf *buf, const char *name,
2759 int fl, struct thandle *handle)
2761 struct osd_thandle *oh;
2763 LASSERT(handle != NULL);
2765 oh = container_of0(handle, struct osd_thandle, ot_super);
2766 LASSERT(oh->ot_handle == NULL);
2768 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2769 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2770 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2771 osd_dto_credits_noquota[DTO_XATTR_SET]);
2777 * Set the 64-bit version for object
2779 static void osd_object_version_set(const struct lu_env *env,
2780 struct dt_object *dt,
2781 dt_obj_version_t *new_version)
2783 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2785 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2786 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2788 LDISKFS_I(inode)->i_fs_version = *new_version;
2789 /** Version is set after all inode operations are finished,
2790 * so we should mark it dirty here */
2791 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2795 * Concurrency: @dt is write locked.
2797 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2798 const struct lu_buf *buf, const char *name, int fl,
2799 struct thandle *handle, struct lustre_capa *capa)
2801 struct osd_object *obj = osd_dt_obj(dt);
2802 struct inode *inode = obj->oo_inode;
2803 struct osd_thread_info *info = osd_oti_get(env);
2807 LASSERT(handle != NULL);
2809 /* version set is not real XATTR */
2810 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2811 /* for version we are just using xattr API but change inode
2813 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2814 osd_object_version_set(env, dt, buf->lb_buf);
2815 return sizeof(dt_obj_version_t);
2818 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2821 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2822 if (fl & LU_XATTR_REPLACE)
2823 fs_flags |= XATTR_REPLACE;
2825 if (fl & LU_XATTR_CREATE)
2826 fs_flags |= XATTR_CREATE;
2828 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2833 * Concurrency: @dt is read locked.
2835 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2836 struct lu_buf *buf, struct lustre_capa *capa)
2838 struct osd_object *obj = osd_dt_obj(dt);
2839 struct inode *inode = obj->oo_inode;
2840 struct osd_thread_info *info = osd_oti_get(env);
2841 struct dentry *dentry = &info->oti_obj_dentry;
2843 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2844 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2845 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2847 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2850 dentry->d_inode = inode;
2851 dentry->d_sb = inode->i_sb;
2852 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2855 static int osd_declare_xattr_del(const struct lu_env *env,
2856 struct dt_object *dt, const char *name,
2857 struct thandle *handle)
2859 struct osd_thandle *oh;
2861 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2862 LASSERT(handle != NULL);
2864 oh = container_of0(handle, struct osd_thandle, ot_super);
2865 LASSERT(oh->ot_handle == NULL);
2867 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2868 osd_dto_credits_noquota[DTO_XATTR_SET]);
2874 * Concurrency: @dt is write locked.
2876 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2877 const char *name, struct thandle *handle,
2878 struct lustre_capa *capa)
2880 struct osd_object *obj = osd_dt_obj(dt);
2881 struct inode *inode = obj->oo_inode;
2882 struct osd_thread_info *info = osd_oti_get(env);
2883 struct dentry *dentry = &info->oti_obj_dentry;
2886 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2887 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2888 LASSERT(handle != NULL);
2890 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2893 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2895 ll_vfs_dq_init(inode);
2896 dentry->d_inode = inode;
2897 dentry->d_sb = inode->i_sb;
2898 rc = inode->i_op->removexattr(dentry, name);
2902 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2903 struct dt_object *dt,
2904 struct lustre_capa *old, __u64 opc)
2906 struct osd_thread_info *info = osd_oti_get(env);
2907 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2908 struct osd_object *obj = osd_dt_obj(dt);
2909 struct osd_device *osd = osd_obj2dev(obj);
2910 struct lustre_capa_key *key = &info->oti_capa_key;
2911 struct lustre_capa *capa = &info->oti_capa;
2912 struct obd_capa *oc;
2913 struct lu_capainfo *lci;
2917 if (!osd->od_fl_capa)
2918 RETURN(ERR_PTR(-ENOENT));
2920 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2921 LINVRNT(osd_invariant(obj));
2923 /* renewal sanity check */
2924 if (old && osd_object_auth(env, dt, old, opc))
2925 RETURN(ERR_PTR(-EACCES));
2927 lci = lu_capainfo_get(env);
2928 if (unlikely(lci == NULL))
2929 RETURN(ERR_PTR(-ENOENT));
2931 switch (lci->lci_auth) {
2935 capa->lc_uid = obj->oo_inode->i_uid;
2936 capa->lc_gid = obj->oo_inode->i_gid;
2937 capa->lc_flags = LC_ID_PLAIN;
2939 case LC_ID_CONVERT: {
2942 s[0] = obj->oo_inode->i_uid;
2943 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2944 s[2] = obj->oo_inode->i_gid;
2945 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2946 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2948 RETURN(ERR_PTR(rc));
2950 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2951 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2952 capa->lc_flags = LC_ID_CONVERT;
2956 RETURN(ERR_PTR(-EINVAL));
2959 capa->lc_fid = *fid;
2961 capa->lc_flags |= osd->od_capa_alg << 24;
2962 capa->lc_timeout = osd->od_capa_timeout;
2963 capa->lc_expiry = 0;
2965 oc = capa_lookup(osd->od_capa_hash, capa, 1);
2967 LASSERT(!capa_is_expired(oc));
2971 spin_lock(&capa_lock);
2972 *key = osd->od_capa_keys[1];
2973 spin_unlock(&capa_lock);
2975 capa->lc_keyid = key->lk_keyid;
2976 capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
2978 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2980 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2981 RETURN(ERR_PTR(rc));
2984 oc = capa_add(osd->od_capa_hash, capa);
2988 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2990 struct osd_object *obj = osd_dt_obj(dt);
2991 struct inode *inode = obj->oo_inode;
2992 struct osd_thread_info *info = osd_oti_get(env);
2993 struct dentry *dentry = &info->oti_obj_dentry;
2994 struct file *file = &info->oti_file;
2999 dentry->d_inode = inode;
3000 dentry->d_sb = inode->i_sb;
3001 file->f_dentry = dentry;
3002 file->f_mapping = inode->i_mapping;
3003 file->f_op = inode->i_fop;
3004 #ifndef HAVE_FILE_FSYNC_4ARGS
3005 mutex_lock(&inode->i_mutex);
3007 rc = do_fsync(file, 0);
3008 #ifndef HAVE_FILE_FSYNC_4ARGS
3009 mutex_unlock(&inode->i_mutex);
3014 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
3017 struct osd_object *obj = osd_dt_obj(dt);
3020 *data = (void *)obj->oo_inode;
3028 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3029 const struct dt_index_features *feat)
3031 struct iam_descr *descr;
3033 if (osd_object_is_root(o))
3034 return feat == &dt_directory_features;
3036 LASSERT(o->oo_dir != NULL);
3038 descr = o->oo_dir->od_container.ic_descr;
3039 if (feat == &dt_directory_features) {
3040 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3046 feat->dif_keysize_min <= descr->id_key_size &&
3047 descr->id_key_size <= feat->dif_keysize_max &&
3048 feat->dif_recsize_min <= descr->id_rec_size &&
3049 descr->id_rec_size <= feat->dif_recsize_max &&
3050 !(feat->dif_flags & (DT_IND_VARKEY |
3051 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3052 ergo(feat->dif_flags & DT_IND_UPDATE,
3053 1 /* XXX check that object (and file system) is
3058 static int osd_iam_container_init(const struct lu_env *env,
3059 struct osd_object *obj,
3060 struct osd_directory *dir)
3062 struct iam_container *bag = &dir->od_container;
3065 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3069 result = iam_container_setup(bag);
3071 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3073 iam_container_fini(bag);
3080 * Concurrency: no external locking is necessary.
3082 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3083 const struct dt_index_features *feat)
3087 struct osd_object *obj = osd_dt_obj(dt);
3089 LINVRNT(osd_invariant(obj));
3091 if (osd_object_is_root(obj)) {
3092 dt->do_index_ops = &osd_index_ea_ops;
3094 } else if (feat == &dt_directory_features) {
3095 dt->do_index_ops = &osd_index_ea_ops;
3096 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3101 } else if (unlikely(feat == &dt_otable_features)) {
3102 dt->do_index_ops = &osd_otable_ops;
3104 } else if (unlikely(feat == &dt_acct_features)) {
3105 dt->do_index_ops = &osd_acct_index_ops;
3108 } else if (!osd_has_index(obj)) {
3109 struct osd_directory *dir;
3114 spin_lock(&obj->oo_guard);
3115 if (obj->oo_dir == NULL)
3119 * Concurrent thread allocated container data.
3122 spin_unlock(&obj->oo_guard);
3124 * Now, that we have container data, serialize its
3127 down_write(&obj->oo_ext_idx_sem);
3129 * recheck under lock.
3131 if (!osd_has_index(obj))
3132 result = osd_iam_container_init(env, obj, dir);
3135 up_write(&obj->oo_ext_idx_sem);
3143 if (result == 0 && skip_iam == 0) {
3144 if (!osd_iam_index_probe(env, obj, feat))
3147 LINVRNT(osd_invariant(obj));
3149 if (result == 0 && is_quota_glb_feat(feat) &&
3150 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3151 result = osd_quota_migration(env, dt, feat);
3156 static int osd_otable_it_attr_get(const struct lu_env *env,
3157 struct dt_object *dt,
3158 struct lu_attr *attr,
3159 struct lustre_capa *capa)
3165 static const struct dt_object_operations osd_obj_ops = {
3166 .do_read_lock = osd_object_read_lock,
3167 .do_write_lock = osd_object_write_lock,
3168 .do_read_unlock = osd_object_read_unlock,
3169 .do_write_unlock = osd_object_write_unlock,
3170 .do_write_locked = osd_object_write_locked,
3171 .do_attr_get = osd_attr_get,
3172 .do_declare_attr_set = osd_declare_attr_set,
3173 .do_attr_set = osd_attr_set,
3174 .do_ah_init = osd_ah_init,
3175 .do_declare_create = osd_declare_object_create,
3176 .do_create = osd_object_create,
3177 .do_declare_destroy = osd_declare_object_destroy,
3178 .do_destroy = osd_object_destroy,
3179 .do_index_try = osd_index_try,
3180 .do_declare_ref_add = osd_declare_object_ref_add,
3181 .do_ref_add = osd_object_ref_add,
3182 .do_declare_ref_del = osd_declare_object_ref_del,
3183 .do_ref_del = osd_object_ref_del,
3184 .do_xattr_get = osd_xattr_get,
3185 .do_declare_xattr_set = osd_declare_xattr_set,
3186 .do_xattr_set = osd_xattr_set,
3187 .do_declare_xattr_del = osd_declare_xattr_del,
3188 .do_xattr_del = osd_xattr_del,
3189 .do_xattr_list = osd_xattr_list,
3190 .do_capa_get = osd_capa_get,
3191 .do_object_sync = osd_object_sync,
3192 .do_data_get = osd_data_get,
3196 * dt_object_operations for interoperability mode
3197 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3199 static const struct dt_object_operations osd_obj_ea_ops = {
3200 .do_read_lock = osd_object_read_lock,
3201 .do_write_lock = osd_object_write_lock,
3202 .do_read_unlock = osd_object_read_unlock,
3203 .do_write_unlock = osd_object_write_unlock,
3204 .do_write_locked = osd_object_write_locked,
3205 .do_attr_get = osd_attr_get,
3206 .do_declare_attr_set = osd_declare_attr_set,
3207 .do_attr_set = osd_attr_set,
3208 .do_ah_init = osd_ah_init,
3209 .do_declare_create = osd_declare_object_create,
3210 .do_create = osd_object_ea_create,
3211 .do_declare_destroy = osd_declare_object_destroy,
3212 .do_destroy = osd_object_destroy,
3213 .do_index_try = osd_index_try,
3214 .do_declare_ref_add = osd_declare_object_ref_add,
3215 .do_ref_add = osd_object_ref_add,
3216 .do_declare_ref_del = osd_declare_object_ref_del,
3217 .do_ref_del = osd_object_ref_del,
3218 .do_xattr_get = osd_xattr_get,
3219 .do_declare_xattr_set = osd_declare_xattr_set,
3220 .do_xattr_set = osd_xattr_set,
3221 .do_declare_xattr_del = osd_declare_xattr_del,
3222 .do_xattr_del = osd_xattr_del,
3223 .do_xattr_list = osd_xattr_list,
3224 .do_capa_get = osd_capa_get,
3225 .do_object_sync = osd_object_sync,
3226 .do_data_get = osd_data_get,
3229 static const struct dt_object_operations osd_obj_otable_it_ops = {
3230 .do_attr_get = osd_otable_it_attr_get,
3231 .do_index_try = osd_index_try,
3234 static int osd_index_declare_iam_delete(const struct lu_env *env,
3235 struct dt_object *dt,
3236 const struct dt_key *key,
3237 struct thandle *handle)
3239 struct osd_thandle *oh;
3241 oh = container_of0(handle, struct osd_thandle, ot_super);
3242 LASSERT(oh->ot_handle == NULL);
3244 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3245 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3251 * delete a (key, value) pair from index \a dt specified by \a key
3253 * \param dt osd index object
3254 * \param key key for index
3255 * \param rec record reference
3256 * \param handle transaction handler
3259 * \retval -ve failure
3262 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3263 const struct dt_key *key,
3264 struct thandle *handle,
3265 struct lustre_capa *capa)
3267 struct osd_thread_info *oti = osd_oti_get(env);
3268 struct osd_object *obj = osd_dt_obj(dt);
3269 struct osd_thandle *oh;
3270 struct iam_path_descr *ipd;
3271 struct iam_container *bag = &obj->oo_dir->od_container;
3276 LINVRNT(osd_invariant(obj));
3277 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3278 LASSERT(bag->ic_object == obj->oo_inode);
3279 LASSERT(handle != NULL);
3281 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3284 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3286 ipd = osd_idx_ipd_get(env, bag);
3287 if (unlikely(ipd == NULL))
3290 oh = container_of0(handle, struct osd_thandle, ot_super);
3291 LASSERT(oh->ot_handle != NULL);
3292 LASSERT(oh->ot_handle->h_transaction != NULL);
3294 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3295 /* swab quota uid/gid provided by caller */
3296 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3297 key = (const struct dt_key *)&oti->oti_quota_id;
3300 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3301 osd_ipd_put(env, bag, ipd);
3302 LINVRNT(osd_invariant(obj));
3306 static int osd_index_declare_ea_delete(const struct lu_env *env,
3307 struct dt_object *dt,
3308 const struct dt_key *key,
3309 struct thandle *handle)
3311 struct osd_thandle *oh;
3312 struct inode *inode;
3316 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3317 LASSERT(handle != NULL);
3319 oh = container_of0(handle, struct osd_thandle, ot_super);
3320 LASSERT(oh->ot_handle == NULL);
3322 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3323 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3325 inode = osd_dt_obj(dt)->oo_inode;
3328 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3329 true, true, NULL, false);
3333 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3336 struct osd_fid_pack *rec;
3339 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3340 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3341 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3346 static int osd_mdt_seq_exists(const struct lu_env *env,
3347 struct osd_device *osd, obd_seq seq)
3349 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3350 struct seq_server_site *ss = osd_seq_site(osd);
3357 /* XXX: currently, each MDT only store avaible sequence on disk, and no
3358 * allocated sequences information on disk, so we have to lookup FLDB,
3359 * but it probably makes more sense also store allocated sequence
3360 * locally, so we do not need do remote FLDB lookup in OSD */
3361 rc = osd_fld_lookup(env, osd, seq, range);
3363 CERROR("%s: Can not lookup fld for "LPX64"\n",
3364 osd_name(osd), seq);
3368 RETURN(ss->ss_node_id == range->lsr_index);
3371 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3376 /* FID seqs not in FLDB, must be local seq */
3377 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3380 /* Currently only check this for FID on MDT */
3381 if (osd_mdt_seq_exists(env, osd, fid_seq(fid)))
3388 * Index delete function for interoperability mode (b11826).
3389 * It will remove the directory entry added by osd_index_ea_insert().
3390 * This entry is needed to maintain name->fid mapping.
3392 * \param key, key i.e. file entry to be deleted
3394 * \retval 0, on success
3395 * \retval -ve, on error
3397 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3398 const struct dt_key *key,
3399 struct thandle *handle,
3400 struct lustre_capa *capa)
3402 struct osd_object *obj = osd_dt_obj(dt);
3403 struct inode *dir = obj->oo_inode;
3404 struct dentry *dentry;
3405 struct osd_thandle *oh;
3406 struct ldiskfs_dir_entry_2 *de = NULL;
3407 struct buffer_head *bh;
3408 struct htree_lock *hlock = NULL;
3409 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3410 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3414 LINVRNT(osd_invariant(obj));
3415 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3416 LASSERT(handle != NULL);
3418 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3420 oh = container_of(handle, struct osd_thandle, ot_super);
3421 LASSERT(oh->ot_handle != NULL);
3422 LASSERT(oh->ot_handle->h_transaction != NULL);
3424 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3427 ll_vfs_dq_init(dir);
3428 dentry = osd_child_dentry_get(env, obj,
3429 (char *)key, strlen((char *)key));
3431 if (obj->oo_hl_head != NULL) {
3432 hlock = osd_oti_get(env)->oti_hlock;
3433 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3434 dir, LDISKFS_HLOCK_DEL);
3436 down_write(&obj->oo_ext_idx_sem);
3439 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3443 /* If this is not the ".." entry, it might be a remote DNE
3444 * entry and we need to check if the FID is for a remote
3445 * MDT. If the FID is not in the directory entry (e.g.
3446 * upgraded 1.8 filesystem without dirdata enabled) then
3447 * we need to get the FID from the LMA. For a remote directory
3448 * there HAS to be an LMA, it cannot be an IGIF inode in this
3451 * Delete the entry before the agent inode in order to
3452 * simplify error handling. At worst an error after deleting
3453 * the entry first might leak the agent inode afterward. The
3454 * reverse would need filesystem abort in case of error deleting
3455 * the entry after the agent had been removed, or leave a
3456 * dangling entry pointing at a random inode. */
3457 if (strcmp((char *)key, dotdot) != 0) {
3458 LASSERT(de != NULL);
3459 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3460 /* If Fid is not in dentry, try to get it from LMA */
3461 if (rc == -ENODATA) {
3462 struct osd_inode_id *id;
3463 struct inode *inode;
3465 /* Before trying to get fid from the inode,
3466 * check whether the inode is valid.
3468 * If the inode has been deleted, do not go
3469 * ahead to do osd_ea_fid_get, which will set
3470 * the inode to bad inode, which might cause
3471 * the inode to be deleted uncorrectly */
3472 inode = ldiskfs_iget(osd_sb(osd),
3473 le32_to_cpu(de->inode));
3474 if (IS_ERR(inode)) {
3475 CDEBUG(D_INODE, "%s: "DFID"get inode"
3476 "error.\n", osd_name(osd),
3478 rc = PTR_ERR(inode);
3480 if (likely(inode->i_nlink != 0)) {
3481 id = &osd_oti_get(env)->oti_id;
3482 rc = osd_ea_fid_get(env, obj,
3483 le32_to_cpu(de->inode),
3486 CDEBUG(D_INFO, "%s: %u "DFID
3489 le32_to_cpu(de->inode),
3497 unlikely(osd_remote_fid(env, osd, fid)))
3498 /* Need to delete agent inode */
3499 ino = le32_to_cpu(de->inode);
3501 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3503 if (rc == 0 && unlikely(ino != 0)) {
3504 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3507 CERROR("%s: del local inode "DFID": rc = %d\n",
3508 osd_name(osd), PFID(fid), rc);
3514 ldiskfs_htree_unlock(hlock);
3516 up_write(&obj->oo_ext_idx_sem);
3521 /* For inode on the remote MDT, .. will point to
3522 * /Agent directory, Check whether it needs to delete
3523 * from agent directory */
3524 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3525 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3527 if (rc != 0 && rc != -ENOENT) {
3528 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3529 osd_name(osd), PFID(fid), rc);
3539 LASSERT(osd_invariant(obj));
3544 * Lookup index for \a key and copy record to \a rec.
3546 * \param dt osd index object
3547 * \param key key for index
3548 * \param rec record reference
3550 * \retval +ve success : exact mach
3551 * \retval 0 return record with key not greater than \a key
3552 * \retval -ve failure
3554 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3555 struct dt_rec *rec, const struct dt_key *key,
3556 struct lustre_capa *capa)
3558 struct osd_object *obj = osd_dt_obj(dt);
3559 struct iam_path_descr *ipd;
3560 struct iam_container *bag = &obj->oo_dir->od_container;
3561 struct osd_thread_info *oti = osd_oti_get(env);
3562 struct iam_iterator *it = &oti->oti_idx_it;
3563 struct iam_rec *iam_rec;
3568 LASSERT(osd_invariant(obj));
3569 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3570 LASSERT(bag->ic_object == obj->oo_inode);
3572 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3575 ipd = osd_idx_ipd_get(env, bag);
3579 /* got ipd now we can start iterator. */
3580 iam_it_init(it, bag, 0, ipd);
3582 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3583 /* swab quota uid/gid provided by caller */
3584 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3585 key = (const struct dt_key *)&oti->oti_quota_id;
3588 rc = iam_it_get(it, (struct iam_key *)key);
3590 if (S_ISDIR(obj->oo_inode->i_mode))
3591 iam_rec = (struct iam_rec *)oti->oti_ldp;
3593 iam_rec = (struct iam_rec *) rec;
3595 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3597 if (S_ISDIR(obj->oo_inode->i_mode))
3598 osd_fid_unpack((struct lu_fid *) rec,
3599 (struct osd_fid_pack *)iam_rec);
3600 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3601 osd_quota_unpack(obj, rec);
3606 osd_ipd_put(env, bag, ipd);
3608 LINVRNT(osd_invariant(obj));
3613 static int osd_index_declare_iam_insert(const struct lu_env *env,
3614 struct dt_object *dt,
3615 const struct dt_rec *rec,
3616 const struct dt_key *key,
3617 struct thandle *handle)
3619 struct osd_thandle *oh;
3621 LASSERT(handle != NULL);
3623 oh = container_of0(handle, struct osd_thandle, ot_super);
3624 LASSERT(oh->ot_handle == NULL);
3626 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3627 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3633 * Inserts (key, value) pair in \a dt index object.
3635 * \param dt osd index object
3636 * \param key key for index
3637 * \param rec record reference
3638 * \param th transaction handler
3641 * \retval -ve failure
3643 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3644 const struct dt_rec *rec,
3645 const struct dt_key *key, struct thandle *th,
3646 struct lustre_capa *capa, int ignore_quota)
3648 struct osd_object *obj = osd_dt_obj(dt);
3649 struct iam_path_descr *ipd;
3650 struct osd_thandle *oh;
3651 struct iam_container *bag = &obj->oo_dir->od_container;
3652 struct osd_thread_info *oti = osd_oti_get(env);
3653 struct iam_rec *iam_rec;
3658 LINVRNT(osd_invariant(obj));
3659 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3660 LASSERT(bag->ic_object == obj->oo_inode);
3661 LASSERT(th != NULL);
3663 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3666 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3668 ipd = osd_idx_ipd_get(env, bag);
3669 if (unlikely(ipd == NULL))
3672 oh = container_of0(th, struct osd_thandle, ot_super);
3673 LASSERT(oh->ot_handle != NULL);
3674 LASSERT(oh->ot_handle->h_transaction != NULL);
3675 if (S_ISDIR(obj->oo_inode->i_mode)) {
3676 iam_rec = (struct iam_rec *)oti->oti_ldp;
3677 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3678 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3679 /* pack quota uid/gid */
3680 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3681 key = (const struct dt_key *)&oti->oti_quota_id;
3682 /* pack quota record */
3683 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3684 iam_rec = (struct iam_rec *)rec;
3686 iam_rec = (struct iam_rec *)rec;
3689 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3691 osd_ipd_put(env, bag, ipd);
3692 LINVRNT(osd_invariant(obj));
3697 * Calls ldiskfs_add_entry() to add directory entry
3698 * into the directory. This is required for
3699 * interoperability mode (b11826)
3701 * \retval 0, on success
3702 * \retval -ve, on error
3704 static int __osd_ea_add_rec(struct osd_thread_info *info,
3705 struct osd_object *pobj, struct inode *cinode,
3706 const char *name, const struct dt_rec *fid,
3707 struct htree_lock *hlock, struct thandle *th)
3709 struct ldiskfs_dentry_param *ldp;
3710 struct dentry *child;
3711 struct osd_thandle *oth;
3714 oth = container_of(th, struct osd_thandle, ot_super);
3715 LASSERT(oth->ot_handle != NULL);
3716 LASSERT(oth->ot_handle->h_transaction != NULL);
3717 LASSERT(pobj->oo_inode);
3719 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3720 if (unlikely(pobj->oo_inode ==
3721 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3724 osd_get_ldiskfs_dirent_param(ldp, fid);
3725 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3726 child->d_fsdata = (void *)ldp;
3727 ll_vfs_dq_init(pobj->oo_inode);
3728 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3734 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3735 * into the directory.Also sets flags into osd object to
3736 * indicate dot and dotdot are created. This is required for
3737 * interoperability mode (b11826)
3739 * \param dir directory for dot and dotdot fixup.
3740 * \param obj child object for linking
3742 * \retval 0, on success
3743 * \retval -ve, on error
3745 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3746 struct osd_object *dir,
3747 struct inode *parent_dir, const char *name,
3748 const struct dt_rec *dot_fid,
3749 const struct dt_rec *dot_dot_fid,
3752 struct inode *inode = dir->oo_inode;
3753 struct osd_thandle *oth;
3756 oth = container_of(th, struct osd_thandle, ot_super);
3757 LASSERT(oth->ot_handle->h_transaction != NULL);
3758 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3760 if (strcmp(name, dot) == 0) {
3761 if (dir->oo_compat_dot_created) {
3764 LASSERT(inode == parent_dir);
3765 dir->oo_compat_dot_created = 1;
3768 } else if (strcmp(name, dotdot) == 0) {
3769 if (!dir->oo_compat_dot_created)
3771 /* in case of rename, dotdot is already created */
3772 if (dir->oo_compat_dotdot_created) {
3773 return __osd_ea_add_rec(info, dir, parent_dir, name,
3774 dot_dot_fid, NULL, th);
3777 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3778 parent_dir, dot_fid,
3781 dir->oo_compat_dotdot_created = 1;
3789 * It will call the appropriate osd_add* function and return the
3790 * value, return by respective functions.
3792 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3793 struct inode *cinode, const char *name,
3794 const struct dt_rec *fid, struct thandle *th)
3796 struct osd_thread_info *info = osd_oti_get(env);
3797 struct htree_lock *hlock;
3800 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3802 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3804 if (hlock != NULL) {
3805 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3808 down_write(&pobj->oo_ext_idx_sem);
3810 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3811 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3814 if (hlock != NULL) {
3815 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3816 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3818 down_write(&pobj->oo_ext_idx_sem);
3821 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3822 struct lu_fid *tfid = &info->oti_fid;
3824 *tfid = *(const struct lu_fid *)fid;
3826 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3827 (const struct dt_rec *)tfid,
3830 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3835 ldiskfs_htree_unlock(hlock);
3837 up_write(&pobj->oo_ext_idx_sem);
3843 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3844 struct osd_idmap_cache *oic)
3846 struct osd_scrub *scrub = &dev->od_scrub;
3847 struct lu_fid *fid = &oic->oic_fid;
3848 struct osd_inode_id *id = &oti->oti_id;
3853 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3857 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3858 if (rc != 0 && rc != -ENOENT)
3861 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3864 if (thread_is_running(&scrub->os_thread)) {
3865 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3866 /* There is race condition between osd_oi_lookup and OI scrub.
3867 * The OI scrub finished just after osd_oi_lookup() failure.
3868 * Under such case, it is unnecessary to trigger OI scrub again,
3869 * but try to call osd_oi_lookup() again. */
3870 if (unlikely(rc == -EAGAIN))
3876 if (!dev->od_noscrub && ++once == 1) {
3877 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3879 rc = osd_scrub_start(dev);
3880 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3882 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3891 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3892 struct osd_device *dev,
3893 struct osd_idmap_cache *oic,
3894 struct lu_fid *fid, __u32 ino)
3896 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3897 struct inode *inode;
3900 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3901 inode = osd_iget(oti, dev, &oic->oic_lid);
3902 if (IS_ERR(inode)) {
3903 fid_zero(&oic->oic_fid);
3904 return PTR_ERR(inode);
3907 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3910 fid_zero(&oic->oic_fid);
3912 *fid = oic->oic_fid = lma->lma_self_fid;
3916 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3917 struct osd_inode_id *id, const struct lu_fid *fid)
3919 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3920 id->oii_ino, id->oii_gen, info);
3921 info->oti_cache.oic_lid = *id;
3922 info->oti_cache.oic_fid = *fid;
3923 info->oti_cache.oic_dev = osd;
3929 * Calls ->lookup() to find dentry. From dentry get inode and
3930 * read inode's ea to get fid. This is required for interoperability
3933 * \retval 0, on success
3934 * \retval -ve, on error
3936 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3937 struct dt_rec *rec, const struct dt_key *key)
3939 struct inode *dir = obj->oo_inode;
3940 struct dentry *dentry;
3941 struct ldiskfs_dir_entry_2 *de;
3942 struct buffer_head *bh;
3943 struct lu_fid *fid = (struct lu_fid *) rec;
3944 struct htree_lock *hlock = NULL;
3949 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3951 dentry = osd_child_dentry_get(env, obj,
3952 (char *)key, strlen((char *)key));
3954 if (obj->oo_hl_head != NULL) {
3955 hlock = osd_oti_get(env)->oti_hlock;
3956 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3957 dir, LDISKFS_HLOCK_LOOKUP);
3959 down_read(&obj->oo_ext_idx_sem);
3962 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3964 struct osd_thread_info *oti = osd_oti_get(env);
3965 struct osd_inode_id *id = &oti->oti_id;
3966 struct osd_idmap_cache *oic = &oti->oti_cache;
3967 struct osd_device *dev = osd_obj2dev(obj);
3968 struct osd_scrub *scrub = &dev->od_scrub;
3969 struct scrub_file *sf = &scrub->os_file;
3971 ino = le32_to_cpu(de->inode);
3972 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3974 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3978 rc = osd_get_fid_from_dentry(de, rec);
3980 /* done with de, release bh */
3983 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3985 osd_id_gen(id, ino, OSD_OII_NOGEN);
3987 fid_zero(&oic->oic_fid);
3991 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3995 if ((scrub->os_pos_current <= ino) &&
3996 ((sf->sf_flags & SF_INCONSISTENT) ||
3997 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3998 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
4000 osd_consistency_check(oti, dev, oic);
4009 ldiskfs_htree_unlock(hlock);
4011 up_read(&obj->oo_ext_idx_sem);
4016 * Find the osd object for given fid.
4018 * \param fid need to find the osd object having this fid
4020 * \retval osd_object on success
4021 * \retval -ve on error
4023 struct osd_object *osd_object_find(const struct lu_env *env,
4024 struct dt_object *dt,
4025 const struct lu_fid *fid)
4027 struct lu_device *ludev = dt->do_lu.lo_dev;
4028 struct osd_object *child = NULL;
4029 struct lu_object *luch;
4030 struct lu_object *lo;
4033 * at this point topdev might not exist yet
4034 * (i.e. MGS is preparing profiles). so we can
4035 * not rely on topdev and instead lookup with
4036 * our device passed as topdev. this can't work
4037 * if the object isn't cached yet (as osd doesn't
4038 * allocate lu_header). IOW, the object must be
4039 * in the cache, otherwise lu_object_alloc() crashes
4042 luch = lu_object_find_at(env, ludev, fid, NULL);
4043 if (!IS_ERR(luch)) {
4044 if (lu_object_exists(luch)) {
4045 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4047 child = osd_obj(lo);
4049 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4050 "lu_object can't be located"
4051 DFID"\n", PFID(fid));
4053 if (child == NULL) {
4054 lu_object_put(env, luch);
4055 CERROR("Unable to get osd_object\n");
4056 child = ERR_PTR(-ENOENT);
4059 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4060 "lu_object does not exists "DFID"\n",
4062 lu_object_put(env, luch);
4063 child = ERR_PTR(-ENOENT);
4066 child = (void *)luch;
4072 * Put the osd object once done with it.
4074 * \param obj osd object that needs to be put
4076 static inline void osd_object_put(const struct lu_env *env,
4077 struct osd_object *obj)
4079 lu_object_put(env, &obj->oo_dt.do_lu);
4082 static int osd_index_declare_ea_insert(const struct lu_env *env,
4083 struct dt_object *dt,
4084 const struct dt_rec *rec,
4085 const struct dt_key *key,
4086 struct thandle *handle)
4088 struct osd_thandle *oh;
4089 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4090 struct lu_fid *fid = (struct lu_fid *)rec;
4094 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4095 LASSERT(handle != NULL);
4097 oh = container_of0(handle, struct osd_thandle, ot_super);
4098 LASSERT(oh->ot_handle == NULL);
4100 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4101 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4103 if (osd_dt_obj(dt)->oo_inode == NULL) {
4104 const char *name = (const char *)key;
4105 /* Object is not being created yet. Only happens when
4106 * 1. declare directory create
4107 * 2. declare insert .
4108 * 3. declare insert ..
4110 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4112 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4114 /* We ignore block quota on meta pool (MDTs), so needn't
4115 * calculate how many blocks will be consumed by this index
4117 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4118 oh, true, true, NULL, false);
4124 rc = osd_remote_fid(env, osd, fid);
4130 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4131 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4132 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4133 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4134 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4135 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4141 * Index add function for interoperability mode (b11826).
4142 * It will add the directory entry.This entry is needed to
4143 * maintain name->fid mapping.
4145 * \param key it is key i.e. file entry to be inserted
4146 * \param rec it is value of given key i.e. fid
4148 * \retval 0, on success
4149 * \retval -ve, on error
4151 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4152 const struct dt_rec *rec,
4153 const struct dt_key *key, struct thandle *th,
4154 struct lustre_capa *capa, int ignore_quota)
4156 struct osd_object *obj = osd_dt_obj(dt);
4157 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4158 struct lu_fid *fid = (struct lu_fid *) rec;
4159 const char *name = (const char *)key;
4160 struct osd_thread_info *oti = osd_oti_get(env);
4161 struct osd_inode_id *id = &oti->oti_id;
4162 struct inode *child_inode = NULL;
4163 struct osd_object *child = NULL;
4167 LASSERT(osd_invariant(obj));
4168 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4169 LASSERT(th != NULL);
4171 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4173 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4176 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4178 rc = osd_remote_fid(env, osd, fid);
4180 CERROR("%s: Can not find object "DFID" rc %d\n",
4181 osd_name(osd), PFID(fid), rc);
4186 /* Insert remote entry */
4187 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4188 struct osd_mdobj_map *omm = osd->od_mdt_map;
4189 struct osd_thandle *oh;
4191 /* If parent on remote MDT, we need put this object
4193 oh = container_of(th, typeof(*oh), ot_super);
4194 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4196 CERROR("%s: add "DFID" error: rc = %d\n",
4198 PFID(lu_object_fid(&dt->do_lu)), rc);
4202 child_inode = igrab(omm->omm_remote_parent->d_inode);
4204 child_inode = osd_create_local_agent_inode(env, osd,
4207 if (IS_ERR(child_inode))
4208 RETURN(PTR_ERR(child_inode));
4211 /* Insert local entry */
4212 child = osd_object_find(env, dt, fid);
4213 if (IS_ERR(child)) {
4214 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4215 osd_name(osd), PFID(fid),
4216 id->oii_ino, id->oii_gen,
4217 (int)PTR_ERR(child_inode));
4218 RETURN(PTR_ERR(child_inode));
4220 child_inode = igrab(child->oo_inode);
4223 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4227 osd_object_put(env, child);
4228 LASSERT(osd_invariant(obj));
4233 * Initialize osd Iterator for given osd index object.
4235 * \param dt osd index object
4238 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4239 struct dt_object *dt,
4241 struct lustre_capa *capa)
4243 struct osd_it_iam *it;
4244 struct osd_thread_info *oti = osd_oti_get(env);
4245 struct osd_object *obj = osd_dt_obj(dt);
4246 struct lu_object *lo = &dt->do_lu;
4247 struct iam_path_descr *ipd;
4248 struct iam_container *bag = &obj->oo_dir->od_container;
4250 LASSERT(lu_object_exists(lo));
4252 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4253 return ERR_PTR(-EACCES);
4256 ipd = osd_it_ipd_get(env, bag);
4257 if (likely(ipd != NULL)) {
4261 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4262 return (struct dt_it *)it;
4264 return ERR_PTR(-ENOMEM);
4268 * free given Iterator.
4271 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4273 struct osd_it_iam *it = (struct osd_it_iam *)di;
4274 struct osd_object *obj = it->oi_obj;
4276 iam_it_fini(&it->oi_it);
4277 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4278 lu_object_put(env, &obj->oo_dt.do_lu);
4282 * Move Iterator to record specified by \a key
4284 * \param di osd iterator
4285 * \param key key for index
4287 * \retval +ve di points to record with least key not larger than key
4288 * \retval 0 di points to exact matched key
4289 * \retval -ve failure
4292 static int osd_it_iam_get(const struct lu_env *env,
4293 struct dt_it *di, const struct dt_key *key)
4295 struct osd_thread_info *oti = osd_oti_get(env);
4296 struct osd_it_iam *it = (struct osd_it_iam *)di;
4298 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4299 /* swab quota uid/gid */
4300 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4301 key = (struct dt_key *)&oti->oti_quota_id;
4304 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4310 * \param di osd iterator
4312 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4314 struct osd_it_iam *it = (struct osd_it_iam *)di;
4316 iam_it_put(&it->oi_it);
4320 * Move iterator by one record
4322 * \param di osd iterator
4324 * \retval +1 end of container reached
4326 * \retval -ve failure
4329 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4331 struct osd_it_iam *it = (struct osd_it_iam *)di;
4333 return iam_it_next(&it->oi_it);
4337 * Return pointer to the key under iterator.
4340 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4341 const struct dt_it *di)
4343 struct osd_thread_info *oti = osd_oti_get(env);
4344 struct osd_it_iam *it = (struct osd_it_iam *)di;
4345 struct osd_object *obj = it->oi_obj;
4348 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4350 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4351 /* swab quota uid/gid */
4352 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4353 key = (struct dt_key *)&oti->oti_quota_id;
4360 * Return size of key under iterator (in bytes)
4363 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4365 struct osd_it_iam *it = (struct osd_it_iam *)di;
4367 return iam_it_key_size(&it->oi_it);
4371 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4373 /* check if file type is required */
4374 if (ent->lde_attrs & LUDA_TYPE) {
4375 struct luda_type *lt;
4376 int align = sizeof(*lt) - 1;
4378 len = (len + align) & ~align;
4379 lt = (struct luda_type *)(ent->lde_name + len);
4380 lt->lt_type = cpu_to_le16(DTTOIF(type));
4383 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4387 * build lu direct from backend fs dirent.
4391 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4392 char *name, __u16 namelen, __u16 type, __u32 attr)
4394 ent->lde_attrs = attr | LUDA_FID;
4395 fid_cpu_to_le(&ent->lde_fid, fid);
4397 ent->lde_hash = cpu_to_le64(offset);
4398 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4400 strncpy(ent->lde_name, name, namelen);
4401 ent->lde_name[namelen] = '\0';
4402 ent->lde_namelen = cpu_to_le16(namelen);
4404 /* append lustre attributes */
4405 osd_it_append_attrs(ent, namelen, type);
4409 * Return pointer to the record under iterator.
4411 static int osd_it_iam_rec(const struct lu_env *env,
4412 const struct dt_it *di,
4413 struct dt_rec *dtrec, __u32 attr)
4415 struct osd_it_iam *it = (struct osd_it_iam *)di;
4416 struct osd_thread_info *info = osd_oti_get(env);
4419 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4420 const struct osd_fid_pack *rec;
4421 struct lu_fid *fid = &info->oti_fid;
4422 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4428 name = (char *)iam_it_key_get(&it->oi_it);
4430 RETURN(PTR_ERR(name));
4432 namelen = iam_it_key_size(&it->oi_it);
4434 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4436 RETURN(PTR_ERR(rec));
4438 rc = osd_fid_unpack(fid, rec);
4442 hash = iam_it_store(&it->oi_it);
4444 /* IAM does not store object type in IAM index (dir) */
4445 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4447 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4448 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4449 (struct iam_rec *)dtrec);
4450 osd_quota_unpack(it->oi_obj, dtrec);
4452 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4453 (struct iam_rec *)dtrec);
4460 * Returns cookie for current Iterator position.
4462 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4464 struct osd_it_iam *it = (struct osd_it_iam *)di;
4466 return iam_it_store(&it->oi_it);
4470 * Restore iterator from cookie.
4472 * \param di osd iterator
4473 * \param hash Iterator location cookie
4475 * \retval +ve di points to record with least key not larger than key.
4476 * \retval 0 di points to exact matched key
4477 * \retval -ve failure
4480 static int osd_it_iam_load(const struct lu_env *env,
4481 const struct dt_it *di, __u64 hash)
4483 struct osd_it_iam *it = (struct osd_it_iam *)di;
4485 return iam_it_load(&it->oi_it, hash);
4488 static const struct dt_index_operations osd_index_iam_ops = {
4489 .dio_lookup = osd_index_iam_lookup,
4490 .dio_declare_insert = osd_index_declare_iam_insert,
4491 .dio_insert = osd_index_iam_insert,
4492 .dio_declare_delete = osd_index_declare_iam_delete,
4493 .dio_delete = osd_index_iam_delete,
4495 .init = osd_it_iam_init,
4496 .fini = osd_it_iam_fini,
4497 .get = osd_it_iam_get,
4498 .put = osd_it_iam_put,
4499 .next = osd_it_iam_next,
4500 .key = osd_it_iam_key,
4501 .key_size = osd_it_iam_key_size,
4502 .rec = osd_it_iam_rec,
4503 .store = osd_it_iam_store,
4504 .load = osd_it_iam_load
4510 * Creates or initializes iterator context.
4512 * \retval struct osd_it_ea, iterator structure on success
4515 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4516 struct dt_object *dt,
4518 struct lustre_capa *capa)
4520 struct osd_object *obj = osd_dt_obj(dt);
4521 struct osd_thread_info *info = osd_oti_get(env);
4522 struct osd_it_ea *it = &info->oti_it_ea;
4523 struct file *file = &it->oie_file;
4524 struct lu_object *lo = &dt->do_lu;
4525 struct dentry *obj_dentry = &info->oti_it_dentry;
4527 LASSERT(lu_object_exists(lo));
4529 obj_dentry->d_inode = obj->oo_inode;
4530 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4531 obj_dentry->d_name.hash = 0;
4533 it->oie_rd_dirent = 0;
4534 it->oie_it_dirent = 0;
4535 it->oie_dirent = NULL;
4536 it->oie_buf = info->oti_it_ea_buf;
4539 /* Reset the "file" totally to avoid to reuse any old value from
4540 * former readdir handling, the "file->f_pos" should be zero. */
4541 memset(file, 0, sizeof(*file));
4542 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4543 if (attr & LUDA_64BITHASH)
4544 file->f_mode = FMODE_64BITHASH;
4546 file->f_mode = FMODE_32BITHASH;
4547 file->f_dentry = obj_dentry;
4548 file->f_mapping = obj->oo_inode->i_mapping;
4549 file->f_op = obj->oo_inode->i_fop;
4551 RETURN((struct dt_it *) it);
4555 * Destroy or finishes iterator context.
4557 * \param di iterator structure to be destroyed
4559 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4561 struct osd_it_ea *it = (struct osd_it_ea *)di;
4562 struct osd_object *obj = it->oie_obj;
4563 struct inode *inode = obj->oo_inode;
4566 it->oie_file.f_op->release(inode, &it->oie_file);
4567 lu_object_put(env, &obj->oo_dt.do_lu);
4572 * It position the iterator at given key, so that next lookup continues from
4573 * that key Or it is similar to dio_it->load() but based on a key,
4574 * rather than file position.
4576 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4579 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4581 static int osd_it_ea_get(const struct lu_env *env,
4582 struct dt_it *di, const struct dt_key *key)
4584 struct osd_it_ea *it = (struct osd_it_ea *)di;
4587 LASSERT(((const char *)key)[0] == '\0');
4588 it->oie_file.f_pos = 0;
4589 it->oie_rd_dirent = 0;
4590 it->oie_it_dirent = 0;
4591 it->oie_dirent = NULL;
4599 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4604 * It is called internally by ->readdir(). It fills the
4605 * iterator's in-memory data structure with required
4606 * information i.e. name, namelen, rec_size etc.
4608 * \param buf in which information to be filled in.
4609 * \param name name of the file in given dir
4611 * \retval 0 on success
4612 * \retval 1 on buffer full
4614 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4615 loff_t offset, __u64 ino,
4618 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4619 struct osd_object *obj = it->oie_obj;
4620 struct osd_it_ea_dirent *ent = it->oie_dirent;
4621 struct lu_fid *fid = &ent->oied_fid;
4622 struct osd_fid_pack *rec;
4625 /* this should never happen */
4626 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4627 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4631 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4635 /* "." is just the object itself. */
4636 if (namelen == 1 && name[0] == '.') {
4637 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4638 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4639 rec = (struct osd_fid_pack*) (name + namelen + 1);
4640 if (osd_fid_unpack(fid, rec) != 0)
4645 d_type &= ~LDISKFS_DIRENT_LUFID;
4647 /* NOT export local root. */
4648 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4649 ino = obj->oo_inode->i_ino;
4650 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4653 ent->oied_ino = ino;
4654 ent->oied_off = offset;
4655 ent->oied_namelen = namelen;
4656 ent->oied_type = d_type;
4658 memcpy(ent->oied_name, name, namelen);
4660 it->oie_rd_dirent++;
4661 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4666 * Calls ->readdir() to load a directory entry at a time
4667 * and stored it in iterator's in-memory data structure.
4669 * \param di iterator's in memory structure
4671 * \retval 0 on success
4672 * \retval -ve on error
4674 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4675 const struct dt_it *di)
4677 struct osd_it_ea *it = (struct osd_it_ea *)di;
4678 struct osd_object *obj = it->oie_obj;
4679 struct inode *inode = obj->oo_inode;
4680 struct htree_lock *hlock = NULL;
4684 it->oie_dirent = it->oie_buf;
4685 it->oie_rd_dirent = 0;
4687 if (obj->oo_hl_head != NULL) {
4688 hlock = osd_oti_get(env)->oti_hlock;
4689 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4690 inode, LDISKFS_HLOCK_READDIR);
4692 down_read(&obj->oo_ext_idx_sem);
4695 result = inode->i_fop->readdir(&it->oie_file, it,
4696 (filldir_t) osd_ldiskfs_filldir);
4699 ldiskfs_htree_unlock(hlock);
4701 up_read(&obj->oo_ext_idx_sem);
4703 if (it->oie_rd_dirent == 0) {
4706 it->oie_dirent = it->oie_buf;
4707 it->oie_it_dirent = 1;
4714 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4715 * to load a directory entry at a time and stored it in
4716 * iterator's in-memory data structure.
4718 * \param di iterator's in memory structure
4720 * \retval +ve iterator reached to end
4721 * \retval 0 iterator not reached to end
4722 * \retval -ve on error
4724 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4726 struct osd_it_ea *it = (struct osd_it_ea *)di;
4731 if (it->oie_it_dirent < it->oie_rd_dirent) {
4733 (void *) it->oie_dirent +
4734 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4735 it->oie_dirent->oied_namelen);
4736 it->oie_it_dirent++;
4739 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4742 rc = osd_ldiskfs_it_fill(env, di);
4749 * Returns the key at current position from iterator's in memory structure.
4751 * \param di iterator's in memory structure
4753 * \retval key i.e. struct dt_key on success
4755 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4756 const struct dt_it *di)
4758 struct osd_it_ea *it = (struct osd_it_ea *)di;
4760 return (struct dt_key *)it->oie_dirent->oied_name;
4764 * Returns the key's size at current position from iterator's in memory structure.
4766 * \param di iterator's in memory structure
4768 * \retval key_size i.e. struct dt_key on success
4770 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4772 struct osd_it_ea *it = (struct osd_it_ea *)di;
4774 return it->oie_dirent->oied_namelen;
4778 osd_dirent_update(handle_t *jh, struct super_block *sb,
4779 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4780 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4782 struct osd_fid_pack *rec;
4786 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4787 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4789 rc = ldiskfs_journal_get_write_access(jh, bh);
4791 CERROR("%.16s: fail to write access for update dirent: "
4792 "name = %.*s, rc = %d\n",
4793 LDISKFS_SB(sb)->s_es->s_volume_name,
4794 ent->oied_namelen, ent->oied_name, rc);
4798 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4799 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4800 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4802 CERROR("%.16s: fail to dirty metadata for update dirent: "
4803 "name = %.*s, rc = %d\n",
4804 LDISKFS_SB(sb)->s_es->s_volume_name,
4805 ent->oied_namelen, ent->oied_name, rc);
4811 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4813 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4814 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4821 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4823 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4824 "dot_dotdot = %d\n", dot_dotdot);
4826 if (LDISKFS_DIR_REC_LEN(de) >=
4827 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4834 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4835 struct inode *dir, struct inode *inode,
4836 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4837 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4838 struct htree_lock *hlock)
4840 struct dentry *dentry;
4841 struct osd_fid_pack *rec;
4842 struct ldiskfs_dentry_param *ldp;
4846 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4847 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4850 /* There is enough space to hold the FID-in-dirent. */
4851 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4852 dir->i_sb->s_blocksize)) {
4853 rc = ldiskfs_journal_get_write_access(jh, bh);
4855 CERROR("%.16s: fail to write access for reinsert "
4856 "dirent: name = %.*s, rc = %d\n",
4857 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4858 ent->oied_namelen, ent->oied_name, rc);
4862 de->name[de->name_len] = 0;
4863 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4864 rec->fp_len = sizeof(struct lu_fid) + 1;
4865 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4866 de->file_type |= LDISKFS_DIRENT_LUFID;
4868 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4870 CERROR("%.16s: fail to dirty metadata for reinsert "
4871 "dirent: name = %.*s, rc = %d\n",
4872 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4873 ent->oied_namelen, ent->oied_name, rc);
4878 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4880 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4881 "name = %.*s, rc = %d\n",
4882 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4883 ent->oied_namelen, ent->oied_name, rc);
4887 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4889 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4890 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4891 dentry->d_fsdata = (void *)ldp;
4892 ll_vfs_dq_init(dir);
4893 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4894 /* It is too bad, we cannot reinsert the name entry back.
4895 * That means we lose it! */
4897 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4898 "name = %.*s, rc = %d\n",
4899 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4900 ent->oied_namelen, ent->oied_name, rc);
4906 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4907 struct osd_it_ea *it, struct lu_fid *fid,
4908 struct osd_inode_id *id, __u32 *attr)
4910 struct osd_thread_info *info = osd_oti_get(env);
4911 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4912 struct osd_device *dev = osd_obj2dev(obj);
4913 struct super_block *sb = osd_sb(dev);
4914 const char *devname =
4915 LDISKFS_SB(sb)->s_es->s_volume_name;
4916 struct osd_it_ea_dirent *ent = it->oie_dirent;
4917 struct inode *dir = obj->oo_inode;
4918 struct htree_lock *hlock = NULL;
4919 struct buffer_head *bh = NULL;
4920 handle_t *jh = NULL;
4921 struct ldiskfs_dir_entry_2 *de;
4922 struct dentry *dentry;
4923 struct inode *inode;
4930 if (ent->oied_name[0] == '.') {
4931 if (ent->oied_namelen == 1)
4933 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4937 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4940 /* We need to ensure that the name entry is still valid.
4941 * Because it may be removed or renamed by other already.
4943 * The unlink or rename operation will start journal before PDO lock,
4944 * so to avoid deadlock, here we need to start journal handle before
4945 * related PDO lock also. But because we do not know whether there
4946 * will be something to be repaired before PDO lock, we just start
4947 * journal without conditions.
4949 * We may need to remove the name entry firstly, then insert back.
4950 * One credit is for user quota file update.
4951 * One credit is for group quota file update.
4952 * Two credits are for dirty inode. */
4953 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4954 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4957 if (dev->od_dirent_journal) {
4958 jh = ldiskfs_journal_start_sb(sb, credits);
4961 CERROR("%.16s: fail to start trans for dirent "
4962 "check_repair: credits %d, name %.*s, rc %d\n",
4963 devname, credits, ent->oied_namelen,
4964 ent->oied_name, rc);
4968 if (obj->oo_hl_head != NULL) {
4969 hlock = osd_oti_get(env)->oti_hlock;
4970 /* "0" means exclusive lock for the whole directory.
4971 * We need to prevent others access such name entry
4972 * during the delete + insert. Neither HLOCK_ADD nor
4973 * HLOCK_DEL cannot guarantee the atomicity. */
4974 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4976 down_write(&obj->oo_ext_idx_sem);
4979 if (obj->oo_hl_head != NULL) {
4980 hlock = osd_oti_get(env)->oti_hlock;
4981 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4982 LDISKFS_HLOCK_LOOKUP);
4984 down_read(&obj->oo_ext_idx_sem);
4988 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4989 /* For dot/dotdot entry, if there is not enough space to hold the
4990 * FID-in-dirent, just keep them there. It only happens when the
4991 * device upgraded from 1.8 or restored from MDT file-level backup.
4992 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4993 * and needs to get FID from LMA when readdir, it will not affect the
4994 * performance much. */
4995 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4996 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4997 *attr |= LUDA_IGNORE;
4998 GOTO(out_journal, rc = 0);
5001 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5002 inode = osd_iget(info, dev, id);
5003 if (IS_ERR(inode)) {
5004 rc = PTR_ERR(inode);
5005 if (rc == -ENOENT || rc == -ESTALE) {
5006 *attr |= LUDA_IGNORE;
5010 GOTO(out_journal, rc);
5013 /* skip the REMOTE_PARENT_DIR. */
5014 if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
5015 GOTO(out_inode, rc = 0);
5017 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5019 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5021 if (fid_is_sane(fid)) {
5022 /* FID-in-dirent is valid. */
5023 if (lu_fid_eq(fid, &lma->lma_self_fid))
5024 GOTO(out_inode, rc = 0);
5026 /* Do not repair under dryrun mode. */
5027 if (*attr & LUDA_VERIFY_DRYRUN) {
5028 *attr |= LUDA_REPAIR;
5029 GOTO(out_inode, rc = 0);
5032 if (!dev->od_dirent_journal) {
5036 ldiskfs_htree_unlock(hlock);
5038 up_read(&obj->oo_ext_idx_sem);
5039 dev->od_dirent_journal = 1;
5043 *fid = lma->lma_self_fid;
5045 /* Update the FID-in-dirent. */
5046 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5048 *attr |= LUDA_REPAIR;
5050 /* Do not repair under dryrun mode. */
5051 if (*attr & LUDA_VERIFY_DRYRUN) {
5052 *fid = lma->lma_self_fid;
5053 *attr |= LUDA_REPAIR;
5054 GOTO(out_inode, rc = 0);
5057 if (!dev->od_dirent_journal) {
5061 ldiskfs_htree_unlock(hlock);
5063 up_read(&obj->oo_ext_idx_sem);
5064 dev->od_dirent_journal = 1;
5068 *fid = lma->lma_self_fid;
5070 /* Append the FID-in-dirent. */
5071 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5072 fid, bh, de, hlock);
5074 *attr |= LUDA_REPAIR;
5076 } else if (rc == -ENODATA) {
5077 /* Do not repair under dryrun mode. */
5078 if (*attr & LUDA_VERIFY_DRYRUN) {
5079 if (fid_is_sane(fid)) {
5080 *attr |= LUDA_REPAIR;
5082 lu_igif_build(fid, inode->i_ino,
5083 inode->i_generation);
5084 *attr |= LUDA_UPGRADE;
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;
5101 if (unlikely(fid_is_sane(fid))) {
5102 /* FID-in-dirent exists, but FID-in-LMA is lost.
5103 * Trust the FID-in-dirent, and add FID-in-LMA. */
5104 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5106 *attr |= LUDA_REPAIR;
5108 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5109 /* It is probably IGIF object. Only aappend the
5110 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5111 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5112 fid, bh, de, hlock);
5114 *attr |= LUDA_UPGRADE;
5118 GOTO(out_inode, rc);
5125 if (hlock != NULL) {
5126 ldiskfs_htree_unlock(hlock);
5128 if (dev->od_dirent_journal)
5129 up_write(&obj->oo_ext_idx_sem);
5131 up_read(&obj->oo_ext_idx_sem);
5134 ldiskfs_journal_stop(jh);
5135 if (rc >= 0 && !dirty)
5136 dev->od_dirent_journal = 0;
5141 * Returns the value at current position from iterator's in memory structure.
5143 * \param di struct osd_it_ea, iterator's in memory structure
5144 * \param attr attr requested for dirent.
5145 * \param lde lustre dirent
5147 * \retval 0 no error and \param lde has correct lustre dirent.
5148 * \retval -ve on error
5150 static inline int osd_it_ea_rec(const struct lu_env *env,
5151 const struct dt_it *di,
5152 struct dt_rec *dtrec, __u32 attr)
5154 struct osd_it_ea *it = (struct osd_it_ea *)di;
5155 struct osd_object *obj = it->oie_obj;
5156 struct osd_device *dev = osd_obj2dev(obj);
5157 struct osd_scrub *scrub = &dev->od_scrub;
5158 struct scrub_file *sf = &scrub->os_file;
5159 struct osd_thread_info *oti = osd_oti_get(env);
5160 struct osd_inode_id *id = &oti->oti_id;
5161 struct osd_idmap_cache *oic = &oti->oti_cache;
5162 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5163 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5164 __u32 ino = it->oie_dirent->oied_ino;
5168 if (attr & LUDA_VERIFY) {
5170 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5171 attr |= LUDA_IGNORE;
5174 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5178 attr &= ~LU_DIRENT_ATTRS_MASK;
5179 if (!fid_is_sane(fid)) {
5180 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5183 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5185 osd_id_gen(id, ino, OSD_OII_NOGEN);
5189 /* Pack the entry anyway, at least the offset is right. */
5190 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5191 it->oie_dirent->oied_name,
5192 it->oie_dirent->oied_namelen,
5193 it->oie_dirent->oied_type, attr);
5198 if (osd_remote_fid(env, dev, fid))
5201 if (likely(!(attr & LUDA_IGNORE)))
5202 rc = osd_add_oi_cache(oti, dev, id, fid);
5204 if (!(attr & LUDA_VERIFY) &&
5205 (scrub->os_pos_current <= ino) &&
5206 ((sf->sf_flags & SF_INCONSISTENT) ||
5207 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5208 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5209 osd_consistency_check(oti, dev, oic);
5215 * Returns a cookie for current position of the iterator head, so that
5216 * user can use this cookie to load/start the iterator next time.
5218 * \param di iterator's in memory structure
5220 * \retval cookie for current position, on success
5222 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5224 struct osd_it_ea *it = (struct osd_it_ea *)di;
5226 return it->oie_dirent->oied_off;
5230 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5231 * to load a directory entry at a time and stored it i inn,
5232 * in iterator's in-memory data structure.
5234 * \param di struct osd_it_ea, iterator's in memory structure
5236 * \retval +ve on success
5237 * \retval -ve on error
5239 static int osd_it_ea_load(const struct lu_env *env,
5240 const struct dt_it *di, __u64 hash)
5242 struct osd_it_ea *it = (struct osd_it_ea *)di;
5246 it->oie_file.f_pos = hash;
5248 rc = osd_ldiskfs_it_fill(env, di);
5256 * Index lookup function for interoperability mode (b11826).
5258 * \param key, key i.e. file name to be searched
5260 * \retval +ve, on success
5261 * \retval -ve, on error
5263 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5264 struct dt_rec *rec, const struct dt_key *key,
5265 struct lustre_capa *capa)
5267 struct osd_object *obj = osd_dt_obj(dt);
5272 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5273 LINVRNT(osd_invariant(obj));
5275 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5278 rc = osd_ea_lookup_rec(env, obj, rec, key);
5285 * Index and Iterator operations for interoperability
5286 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5288 static const struct dt_index_operations osd_index_ea_ops = {
5289 .dio_lookup = osd_index_ea_lookup,
5290 .dio_declare_insert = osd_index_declare_ea_insert,
5291 .dio_insert = osd_index_ea_insert,
5292 .dio_declare_delete = osd_index_declare_ea_delete,
5293 .dio_delete = osd_index_ea_delete,
5295 .init = osd_it_ea_init,
5296 .fini = osd_it_ea_fini,
5297 .get = osd_it_ea_get,
5298 .put = osd_it_ea_put,
5299 .next = osd_it_ea_next,
5300 .key = osd_it_ea_key,
5301 .key_size = osd_it_ea_key_size,
5302 .rec = osd_it_ea_rec,
5303 .store = osd_it_ea_store,
5304 .load = osd_it_ea_load
5308 static void *osd_key_init(const struct lu_context *ctx,
5309 struct lu_context_key *key)
5311 struct osd_thread_info *info;
5313 OBD_ALLOC_PTR(info);
5315 return ERR_PTR(-ENOMEM);
5317 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5318 if (info->oti_it_ea_buf == NULL)
5321 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5323 info->oti_hlock = ldiskfs_htree_lock_alloc();
5324 if (info->oti_hlock == NULL)
5330 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5333 return ERR_PTR(-ENOMEM);
5336 static void osd_key_fini(const struct lu_context *ctx,
5337 struct lu_context_key *key, void* data)
5339 struct osd_thread_info *info = data;
5341 if (info->oti_hlock != NULL)
5342 ldiskfs_htree_lock_free(info->oti_hlock);
5343 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5344 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5345 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5349 static void osd_key_exit(const struct lu_context *ctx,
5350 struct lu_context_key *key, void *data)
5352 struct osd_thread_info *info = data;
5354 LASSERT(info->oti_r_locks == 0);
5355 LASSERT(info->oti_w_locks == 0);
5356 LASSERT(info->oti_txns == 0);
5359 /* type constructor/destructor: osd_type_init, osd_type_fini */
5360 LU_TYPE_INIT_FINI(osd, &osd_key);
5362 struct lu_context_key osd_key = {
5363 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5364 .lct_init = osd_key_init,
5365 .lct_fini = osd_key_fini,
5366 .lct_exit = osd_key_exit
5370 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5371 const char *name, struct lu_device *next)
5373 struct osd_device *osd = osd_dev(d);
5375 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5376 >= sizeof(osd->od_svname))
5378 return osd_procfs_init(osd, name);
5381 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5385 /* shutdown quota slave instance associated with the device */
5386 if (o->od_quota_slave != NULL) {
5387 qsd_fini(env, o->od_quota_slave);
5388 o->od_quota_slave = NULL;
5394 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5399 fsfilt_put_ops(o->od_fsops);
5403 if (o->od_mnt != NULL) {
5404 shrink_dcache_sb(osd_sb(o));
5405 osd_sync(env, &o->od_dt_dev);
5414 static int osd_mount(const struct lu_env *env,
5415 struct osd_device *o, struct lustre_cfg *cfg)
5417 const char *name = lustre_cfg_string(cfg, 0);
5418 const char *dev = lustre_cfg_string(cfg, 1);
5420 unsigned long page, s_flags, lmd_flags = 0;
5421 struct page *__page;
5422 struct file_system_type *type;
5423 char *options = NULL;
5425 struct osd_thread_info *info = osd_oti_get(env);
5426 struct lu_fid *fid = &info->oti_fid;
5427 struct inode *inode;
5431 if (o->od_mnt != NULL)
5434 if (strlen(dev) >= sizeof(o->od_mntdev))
5436 strcpy(o->od_mntdev, dev);
5438 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5439 if (IS_ERR(o->od_fsops)) {
5440 CERROR("%s: Can't find fsfilt_ldiskfs\n", name);
5445 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5447 GOTO(out, rc = -ENOMEM);
5449 str = lustre_cfg_string(cfg, 2);
5450 s_flags = simple_strtoul(str, NULL, 0);
5451 str = strstr(str, ":");
5453 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5454 opts = lustre_cfg_string(cfg, 3);
5455 page = (unsigned long)page_address(__page);
5456 options = (char *)page;
5459 strcat(options, "user_xattr,acl");
5461 strcat(options, opts);
5463 /* Glom up mount options */
5464 if (*options != '\0')
5465 strcat(options, ",");
5466 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5468 type = get_fs_type("ldiskfs");
5470 CERROR("%s: cannot find ldiskfs module\n", name);
5471 GOTO(out, rc = -ENODEV);
5474 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5475 module_put(type->owner);
5477 if (IS_ERR(o->od_mnt)) {
5478 rc = PTR_ERR(o->od_mnt);
5480 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5484 #ifdef HAVE_DEV_SET_RDONLY
5485 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5486 CERROR("%s: underlying device %s is marked as read-only. "
5487 "Setup failed\n", name, dev);
5488 GOTO(out_mnt, rc = -EROFS);
5492 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5493 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5494 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5495 GOTO(out_mnt, rc = -EINVAL);
5498 inode = osd_sb(o)->s_root->d_inode;
5499 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5500 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5501 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5503 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5507 if (lmd_flags & LMD_FLG_NOSCRUB)
5518 OBD_PAGE_FREE(__page);
5520 fsfilt_put_ops(o->od_fsops);
5525 static struct lu_device *osd_device_fini(const struct lu_env *env,
5526 struct lu_device *d)
5528 struct osd_device *o = osd_dev(d);
5532 osd_shutdown(env, o);
5533 osd_scrub_cleanup(env, o);
5534 osd_obj_map_fini(o);
5540 static int osd_device_init0(const struct lu_env *env,
5541 struct osd_device *o,
5542 struct lustre_cfg *cfg)
5544 struct lu_device *l = osd2lu_dev(o);
5545 struct osd_thread_info *info;
5549 /* if the module was re-loaded, env can loose its keys */
5550 rc = lu_env_refill((struct lu_env *) env);
5553 info = osd_oti_get(env);
5556 l->ld_ops = &osd_lu_ops;
5557 o->od_dt_dev.dd_ops = &osd_dt_ops;
5559 spin_lock_init(&o->od_osfs_lock);
5560 mutex_init(&o->od_otable_mutex);
5561 o->od_osfs_age = cfs_time_shift_64(-1000);
5563 o->od_capa_hash = init_capa_hash();
5564 if (o->od_capa_hash == NULL)
5565 GOTO(out, rc = -ENOMEM);
5567 o->od_read_cache = 1;
5568 o->od_writethrough_cache = 1;
5569 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5571 rc = osd_mount(env, o, cfg);
5575 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5576 sizeof(o->od_svname));
5577 if (cplen >= sizeof(o->od_svname)) {
5582 if (server_name_is_ost(o->od_svname))
5585 rc = osd_obj_map_init(env, o);
5589 rc = lu_site_init(&o->od_site, l);
5591 GOTO(out_compat, rc);
5592 o->od_site.ls_bottom_dev = l;
5594 rc = lu_site_init_finish(&o->od_site);
5598 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5599 /* setup scrub, including OI files initialization */
5600 rc = osd_scrub_setup(env, o);
5604 rc = osd_procfs_init(o, o->od_svname);
5606 CERROR("%s: can't initialize procfs: rc = %d\n",
5608 GOTO(out_scrub, rc);
5611 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5613 /* initialize quota slave instance */
5614 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5616 if (IS_ERR(o->od_quota_slave)) {
5617 rc = PTR_ERR(o->od_quota_slave);
5618 o->od_quota_slave = NULL;
5619 GOTO(out_procfs, rc);
5627 osd_scrub_cleanup(env, o);
5629 lu_site_fini(&o->od_site);
5631 osd_obj_map_fini(o);
5635 cleanup_capa_hash(o->od_capa_hash);
5640 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5641 struct lu_device_type *t,
5642 struct lustre_cfg *cfg)
5644 struct osd_device *o;
5649 return ERR_PTR(-ENOMEM);
5651 rc = dt_device_init(&o->od_dt_dev, t);
5653 /* Because the ctx might be revived in dt_device_init,
5654 * refill the env here */
5655 lu_env_refill((struct lu_env *)env);
5656 rc = osd_device_init0(env, o, cfg);
5658 dt_device_fini(&o->od_dt_dev);
5661 if (unlikely(rc != 0))
5664 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5667 static struct lu_device *osd_device_free(const struct lu_env *env,
5668 struct lu_device *d)
5670 struct osd_device *o = osd_dev(d);
5673 cleanup_capa_hash(o->od_capa_hash);
5674 /* XXX: make osd top device in order to release reference */
5675 d->ld_site->ls_top_dev = d;
5676 lu_site_purge(env, d->ld_site, -1);
5677 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5678 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5679 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5681 lu_site_fini(&o->od_site);
5682 dt_device_fini(&o->od_dt_dev);
5687 static int osd_process_config(const struct lu_env *env,
5688 struct lu_device *d, struct lustre_cfg *cfg)
5690 struct osd_device *o = osd_dev(d);
5694 switch(cfg->lcfg_command) {
5696 err = osd_mount(env, o, cfg);
5699 lu_dev_del_linkage(d->ld_site, d);
5700 err = osd_shutdown(env, o);
5709 static int osd_recovery_complete(const struct lu_env *env,
5710 struct lu_device *d)
5712 struct osd_device *osd = osd_dev(d);
5716 if (osd->od_quota_slave == NULL)
5719 /* start qsd instance on recovery completion, this notifies the quota
5720 * slave code that we are about to process new requests now */
5721 rc = qsd_start(env, osd->od_quota_slave);
5726 * we use exports to track all osd users
5728 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5729 struct obd_device *obd, struct obd_uuid *cluuid,
5730 struct obd_connect_data *data, void *localdata)
5732 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5733 struct lustre_handle conn;
5737 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5739 rc = class_connect(&conn, obd, cluuid);
5743 *exp = class_conn2export(&conn);
5745 spin_lock(&osd->od_osfs_lock);
5747 spin_unlock(&osd->od_osfs_lock);
5753 * once last export (we don't count self-export) disappeared
5754 * osd can be released
5756 static int osd_obd_disconnect(struct obd_export *exp)
5758 struct obd_device *obd = exp->exp_obd;
5759 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5760 int rc, release = 0;
5763 /* Only disconnect the underlying layers on the final disconnect. */
5764 spin_lock(&osd->od_osfs_lock);
5766 if (osd->od_connects == 0)
5768 spin_unlock(&osd->od_osfs_lock);
5770 rc = class_disconnect(exp); /* bz 9811 */
5772 if (rc == 0 && release)
5773 class_manual_cleanup(obd);
5777 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5778 struct lu_device *dev)
5780 struct osd_device *osd = osd_dev(dev);
5784 if (osd->od_quota_slave != NULL)
5785 /* set up quota slave objects */
5786 result = qsd_prepare(env, osd->od_quota_slave);
5791 static const struct lu_object_operations osd_lu_obj_ops = {
5792 .loo_object_init = osd_object_init,
5793 .loo_object_delete = osd_object_delete,
5794 .loo_object_release = osd_object_release,
5795 .loo_object_free = osd_object_free,
5796 .loo_object_print = osd_object_print,
5797 .loo_object_invariant = osd_object_invariant
5800 const struct lu_device_operations osd_lu_ops = {
5801 .ldo_object_alloc = osd_object_alloc,
5802 .ldo_process_config = osd_process_config,
5803 .ldo_recovery_complete = osd_recovery_complete,
5804 .ldo_prepare = osd_prepare,
5807 static const struct lu_device_type_operations osd_device_type_ops = {
5808 .ldto_init = osd_type_init,
5809 .ldto_fini = osd_type_fini,
5811 .ldto_start = osd_type_start,
5812 .ldto_stop = osd_type_stop,
5814 .ldto_device_alloc = osd_device_alloc,
5815 .ldto_device_free = osd_device_free,
5817 .ldto_device_init = osd_device_init,
5818 .ldto_device_fini = osd_device_fini
5821 struct lu_device_type osd_device_type = {
5822 .ldt_tags = LU_DEVICE_DT,
5823 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5824 .ldt_ops = &osd_device_type_ops,
5825 .ldt_ctx_tags = LCT_LOCAL,
5829 * lprocfs legacy support.
5831 static struct obd_ops osd_obd_device_ops = {
5832 .o_owner = THIS_MODULE,
5833 .o_connect = osd_obd_connect,
5834 .o_disconnect = osd_obd_disconnect
5837 static int __init osd_mod_init(void)
5839 struct lprocfs_static_vars lvars;
5843 lprocfs_osd_init_vars(&lvars);
5845 rc = lu_kmem_init(ldiskfs_caches);
5849 rc = class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5850 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5852 lu_kmem_fini(ldiskfs_caches);
5856 static void __exit osd_mod_exit(void)
5858 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5859 lu_kmem_fini(ldiskfs_caches);
5862 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5863 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5864 MODULE_LICENSE("GPL");
5866 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);