4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
66 #include "osd_internal.h"
67 #include "osd_dynlocks.h"
69 /* llo_* api support */
70 #include <md_object.h>
71 #include <lustre_quota.h>
74 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
75 "ldiskfs with parallel directory operations");
77 int ldiskfs_track_declares_assert;
78 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
79 "LBUG during tracking of declares");
81 /* Slab to allocate dynlocks */
82 struct kmem_cache *dynlock_cachep;
84 static struct lu_kmem_descr ldiskfs_caches[] = {
86 .ckd_cache = &dynlock_cachep,
87 .ckd_name = "dynlock_cache",
88 .ckd_size = sizeof(struct dynlock_handle)
95 static const char dot[] = ".";
96 static const char dotdot[] = "..";
97 static const char remote_obj_dir[] = "REM_OBJ_DIR";
99 static const struct lu_object_operations osd_lu_obj_ops;
100 static const struct dt_object_operations osd_obj_ops;
101 static const struct dt_object_operations osd_obj_ea_ops;
102 static const struct dt_object_operations osd_obj_otable_it_ops;
103 static const struct dt_index_operations osd_index_iam_ops;
104 static const struct dt_index_operations osd_index_ea_ops;
106 int osd_trans_declare_op2rb[] = {
107 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
108 [OSD_OT_PUNCH] = OSD_OT_MAX,
109 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
110 [OSD_OT_CREATE] = OSD_OT_DESTROY,
111 [OSD_OT_DESTROY] = OSD_OT_CREATE,
112 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
113 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
114 [OSD_OT_WRITE] = OSD_OT_WRITE,
115 [OSD_OT_INSERT] = OSD_OT_DELETE,
116 [OSD_OT_DELETE] = OSD_OT_INSERT,
117 [OSD_OT_UPDATE] = OSD_OT_MAX,
118 [OSD_OT_QUOTA] = OSD_OT_MAX,
121 static int osd_has_index(const struct osd_object *obj)
123 return obj->oo_dt.do_index_ops != NULL;
126 static int osd_object_invariant(const struct lu_object *l)
128 return osd_invariant(osd_obj(l));
132 * Concurrency: doesn't matter
134 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
136 return osd_oti_get(env)->oti_r_locks > 0;
140 * Concurrency: doesn't matter
142 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
144 struct osd_thread_info *oti = osd_oti_get(env);
145 return oti->oti_w_locks > 0 && o->oo_owner == env;
149 * Concurrency: doesn't access mutable data
151 static int osd_root_get(const struct lu_env *env,
152 struct dt_device *dev, struct lu_fid *f)
154 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
159 * OSD object methods.
163 * Concurrency: no concurrent access is possible that early in object
166 static struct lu_object *osd_object_alloc(const struct lu_env *env,
167 const struct lu_object_header *hdr,
170 struct osd_object *mo;
176 l = &mo->oo_dt.do_lu;
177 dt_object_init(&mo->oo_dt, NULL, d);
178 mo->oo_dt.do_ops = &osd_obj_ea_ops;
179 l->lo_ops = &osd_lu_obj_ops;
180 init_rwsem(&mo->oo_sem);
181 init_rwsem(&mo->oo_ext_idx_sem);
182 spin_lock_init(&mo->oo_guard);
189 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
190 struct dentry *dentry, struct lustre_mdt_attrs *lma)
194 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
195 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
196 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
198 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
199 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
201 lustre_lma_swab(lma);
202 /* Check LMA compatibility */
203 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
204 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
205 "for fid = "DFID", ino = %lu\n",
206 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
207 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
208 PFID(&lma->lma_self_fid), inode->i_ino);
211 } else if (rc == 0) {
219 * retrieve object from backend ext fs.
221 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
222 struct osd_inode_id *id)
224 struct inode *inode = NULL;
226 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
228 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
229 id->oii_ino, PTR_ERR(inode));
230 } else if (id->oii_gen != OSD_OII_NOGEN &&
231 inode->i_generation != id->oii_gen) {
232 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
234 id->oii_ino, id->oii_gen, inode->i_generation);
236 inode = ERR_PTR(-ESTALE);
237 } else if (inode->i_nlink == 0) {
238 /* due to parallel readdir and unlink,
239 * we can have dead inode here. */
240 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
241 make_bad_inode(inode);
243 inode = ERR_PTR(-ESTALE);
244 } else if (is_bad_inode(inode)) {
245 CWARN("%.16s: bad inode: ino = %u\n",
246 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
248 inode = ERR_PTR(-ENOENT);
250 if (id->oii_gen == OSD_OII_NOGEN)
251 osd_id_gen(id, inode->i_ino, inode->i_generation);
253 /* Do not update file c/mtime in ldiskfs.
254 * NB: we don't have any lock to protect this because we don't
255 * have reference on osd_object now, but contention with
256 * another lookup + attr_set can't happen in the tiny window
257 * between if (...) and set S_NOCMTIME. */
258 if (!(inode->i_flags & S_NOCMTIME))
259 inode->i_flags |= S_NOCMTIME;
264 static struct inode *
265 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
266 struct osd_inode_id *id, struct lu_fid *fid)
268 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
272 inode = osd_iget(info, dev, id);
276 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
278 *fid = lma->lma_self_fid;
279 } else if (rc == -ENODATA) {
280 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
281 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
283 lu_igif_build(fid, inode->i_ino, inode->i_generation);
292 * \retval +v: new filter_fid, does not contain self-fid
293 * \retval 0: filter_fid_old, contains self-fid
294 * \retval -v: other failure cases
296 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
297 struct dentry *dentry, struct lu_fid *fid)
299 struct filter_fid_old *ff = &info->oti_ff;
300 struct ost_id *ostid = &info->oti_ostid;
303 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
304 if (rc == sizeof(*ff)) {
306 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
307 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
308 /* XXX: should use real OST index in the future. LU-3569 */
309 ostid_to_fid(fid, ostid, 0);
310 } else if (rc == sizeof(struct filter_fid)) {
312 } else if (rc >= 0) {
319 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
321 struct osd_thread_info *info = osd_oti_get(env);
322 struct osd_device *osd = osd_obj2dev(obj);
323 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
324 struct inode *inode = obj->oo_inode;
325 struct dentry *dentry = &info->oti_obj_dentry;
326 struct lu_fid *fid = NULL;
330 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
333 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
334 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
335 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
336 if (rc == -ENODATA && !fid_is_igif(lu_object_fid(&obj->oo_dt.do_lu)) &&
338 fid = &lma->lma_self_fid;
339 rc = osd_get_idif(info, inode, dentry, fid);
340 if ((rc > 0) || (rc == -ENODATA && osd->od_lma_self_repair)) {
343 /* For the given OST-object, if it has neither LMA nor
344 * FID in XATTR_NAME_FID, then the given FID (which is
345 * contained in the @obj, from client RPC for locating
346 * the OST-object) is trusted. We use it to generate
349 LASSERT(current->journal_info == NULL);
351 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
352 osd_dto_credits_noquota[DTO_XATTR_SET]);
354 CWARN("%s: cannot start journal for "
355 "lma_self_repair: rc = %ld\n",
356 osd_name(osd), PTR_ERR(jh));
360 rc = osd_ea_fid_set(info, inode,
361 lu_object_fid(&obj->oo_dt.do_lu),
362 fid_is_on_ost(info, osd,
363 lu_object_fid(&obj->oo_dt.do_lu),
365 LMAC_FID_ON_OST : 0, 0);
367 CWARN("%s: cannot self repair the LMA: "
368 "rc = %d\n", osd_name(osd), rc);
369 ldiskfs_journal_stop(jh);
374 if (unlikely(rc == -ENODATA))
382 lustre_lma_swab(lma);
383 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
384 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
385 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
386 "fid = "DFID", ino = %lu\n", osd_name(osd),
387 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
388 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
391 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
392 fid = &lma->lma_self_fid;
397 unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
398 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
399 osd_name(osd), PFID(lu_object_fid(&obj->oo_dt.do_lu)),
400 PFID(&lma->lma_self_fid));
407 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
408 const struct lu_fid *fid,
409 const struct lu_object_conf *conf)
411 struct osd_thread_info *info;
412 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
413 struct osd_device *dev;
414 struct osd_idmap_cache *oic;
415 struct osd_inode_id *id;
417 struct osd_scrub *scrub;
418 struct scrub_file *sf;
422 bool triggered = false;
425 LINVRNT(osd_invariant(obj));
426 LASSERT(obj->oo_inode == NULL);
427 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
430 scrub = &dev->od_scrub;
431 sf = &scrub->os_file;
432 info = osd_oti_get(env);
434 oic = &info->oti_cache;
436 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
439 /* For the object is created as locking anchor, or for the object to
440 * be created on disk. No need to osd_oi_lookup() at here because FID
441 * shouldn't never be re-used, if it's really a duplicate FID from
442 * unexpected reason, we should be able to detect it later by calling
443 * do_create->osd_oi_insert(). */
444 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
445 GOTO(out, result = 0);
447 /* Search order: 1. per-thread cache. */
448 if (lu_fid_eq(fid, &oic->oic_fid) &&
449 likely(oic->oic_dev == dev)) {
455 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
456 /* Search order: 2. OI scrub pending list. */
457 result = osd_oii_lookup(dev, fid, id);
462 /* Search order: 3. OI files. */
463 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
464 if (result == -ENOENT) {
465 if (!fid_is_norm(fid) ||
466 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
467 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
469 GOTO(out, result = 0);
480 inode = osd_iget(info, dev, id);
482 result = PTR_ERR(inode);
483 if (result == -ENOENT || result == -ESTALE) {
485 fid_zero(&oic->oic_fid);
486 GOTO(out, result = -ENOENT);
489 /* XXX: There are three possible cases:
490 * 1. Backup/restore caused the OI invalid.
491 * 2. Someone unlinked the object but NOT removed
492 * the OI mapping, such as mount target device
493 * as ldiskfs, and modify something directly.
494 * 3. Someone just removed the object between the
495 * former oi_lookup and the iget. It is normal.
497 * It is diffcult to distinguish the 2nd from the
498 * 1st case. Relatively speaking, the 1st case is
499 * common than the 2nd case, trigger OI scrub. */
500 result = osd_oi_lookup(info, dev, fid, id, true);
502 /* It is the case 1 or 2. */
504 } else if (result == -EREMCHG) {
507 if (unlikely(triggered))
508 GOTO(out, result = saved);
511 if (thread_is_running(&scrub->os_thread)) {
512 result = -EINPROGRESS;
513 } else if (!dev->od_noscrub) {
514 result = osd_scrub_start(dev);
515 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
516 "for "DFID", rc = %d [1]\n",
517 LDISKFS_SB(osd_sb(dev))->s_es->\
518 s_volume_name,PFID(fid), result);
519 if (result == 0 || result == -EALREADY)
520 result = -EINPROGRESS;
525 /* We still have chance to get the valid inode: for the
526 * object which is referenced by remote name entry, the
527 * object on the local MDT will be linked under the dir
528 * of "/REMOTE_PARENT_DIR" with its FID string as name.
530 * We do not know whether the object for the given FID
531 * is referenced by some remote name entry or not, and
532 * especially for DNE II, a multiple-linked object may
533 * have many name entries reside on many MDTs.
535 * To simplify the operation, OSD will not distinguish
536 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
537 * only happened for the RPC from other MDT during the
538 * OI scrub, or for the client side RPC with FID only,
539 * such as FID to path, or from old connected client. */
541 result = osd_lookup_in_remote_parent(info, dev,
554 obj->oo_inode = inode;
555 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
557 result = osd_check_lma(env, obj);
560 obj->oo_inode = NULL;
561 if (result == -EREMCHG)
567 obj->oo_compat_dot_created = 1;
568 obj->oo_compat_dotdot_created = 1;
570 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
571 GOTO(out, result = 0);
573 LASSERT(obj->oo_hl_head == NULL);
574 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
575 if (obj->oo_hl_head == NULL) {
576 obj->oo_inode = NULL;
578 GOTO(out, result = -ENOMEM);
580 GOTO(out, result = 0);
583 LINVRNT(osd_invariant(obj));
588 * Concurrency: shouldn't matter.
590 static void osd_object_init0(struct osd_object *obj)
592 LASSERT(obj->oo_inode != NULL);
593 obj->oo_dt.do_body_ops = &osd_body_ops;
594 obj->oo_dt.do_lu.lo_header->loh_attr |=
595 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
599 * Concurrency: no concurrent access is possible that early in object
602 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
603 const struct lu_object_conf *conf)
605 struct osd_object *obj = osd_obj(l);
608 LINVRNT(osd_invariant(obj));
610 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
611 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
612 l->lo_header->loh_attr |= LOHA_EXISTS;
616 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
617 obj->oo_dt.do_body_ops = &osd_body_ops_new;
618 if (result == 0 && obj->oo_inode != NULL)
619 osd_object_init0(obj);
621 LINVRNT(osd_invariant(obj));
626 * Concurrency: no concurrent access is possible that late in object
629 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
631 struct osd_object *obj = osd_obj(l);
633 LINVRNT(osd_invariant(obj));
635 dt_object_fini(&obj->oo_dt);
636 if (obj->oo_hl_head != NULL)
637 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
642 * Concurrency: no concurrent access is possible that late in object
645 static void osd_index_fini(struct osd_object *o)
647 struct iam_container *bag;
649 if (o->oo_dir != NULL) {
650 bag = &o->oo_dir->od_container;
651 if (o->oo_inode != NULL) {
652 if (bag->ic_object == o->oo_inode)
653 iam_container_fini(bag);
655 OBD_FREE_PTR(o->oo_dir);
661 * Concurrency: no concurrent access is possible that late in object
662 * life-cycle (for all existing callers, that is. New callers have to provide
663 * their own locking.)
665 static int osd_inode_unlinked(const struct inode *inode)
667 return inode->i_nlink == 0;
671 OSD_TXN_OI_DELETE_CREDITS = 20,
672 OSD_TXN_INODE_DELETE_CREDITS = 20
679 #if OSD_THANDLE_STATS
681 * Set time when the handle is allocated
683 static void osd_th_alloced(struct osd_thandle *oth)
685 oth->oth_alloced = cfs_time_current();
689 * Set time when the handle started
691 static void osd_th_started(struct osd_thandle *oth)
693 oth->oth_started = cfs_time_current();
697 * Helper function to convert time interval to microseconds packed in
700 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
704 cfs_duration_usec(cfs_time_sub(end, start), &val);
705 return val.tv_sec * 1000000 + val.tv_usec;
709 * Check whether the we deal with this handle for too long.
711 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
712 cfs_time_t alloced, cfs_time_t started,
715 cfs_time_t now = cfs_time_current();
717 LASSERT(dev != NULL);
719 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
720 interval_to_usec(alloced, started));
721 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
722 interval_to_usec(started, closed));
723 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
724 interval_to_usec(closed, now));
726 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
727 CWARN("transaction handle %p was open for too long: "
729 "alloced "CFS_TIME_T" ,"
730 "started "CFS_TIME_T" ,"
731 "closed "CFS_TIME_T"\n",
732 oth, now, alloced, started, closed);
733 libcfs_debug_dumpstack(NULL);
737 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
739 cfs_time_t __closed = cfs_time_current(); \
740 cfs_time_t __alloced = oth->oth_alloced; \
741 cfs_time_t __started = oth->oth_started; \
744 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
747 #else /* OSD_THANDLE_STATS */
749 #define osd_th_alloced(h) do {} while(0)
750 #define osd_th_started(h) do {} while(0)
751 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
753 #endif /* OSD_THANDLE_STATS */
756 * Concurrency: doesn't access mutable data.
758 static int osd_param_is_not_sane(const struct osd_device *dev,
759 const struct thandle *th)
761 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
763 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
767 * Concurrency: shouldn't matter.
769 static void osd_trans_commit_cb(struct super_block *sb,
770 struct ldiskfs_journal_cb_entry *jcb, int error)
772 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
773 struct thandle *th = &oh->ot_super;
774 struct lu_device *lud = &th->th_dev->dd_lu_dev;
775 struct dt_txn_commit_cb *dcb, *tmp;
777 LASSERT(oh->ot_handle == NULL);
780 CERROR("transaction @0x%p commit error: %d\n", th, error);
782 dt_txn_hook_commit(th);
784 /* call per-transaction callbacks if any */
785 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
786 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
787 "commit callback entry: magic=%x name='%s'\n",
788 dcb->dcb_magic, dcb->dcb_name);
789 cfs_list_del_init(&dcb->dcb_linkage);
790 dcb->dcb_func(NULL, th, dcb, error);
793 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
797 lu_context_exit(&th->th_ctx);
798 lu_context_fini(&th->th_ctx);
802 static struct thandle *osd_trans_create(const struct lu_env *env,
805 struct osd_thread_info *oti = osd_oti_get(env);
806 struct osd_iobuf *iobuf = &oti->oti_iobuf;
807 struct osd_thandle *oh;
811 /* on pending IO in this thread should left from prev. request */
812 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
814 th = ERR_PTR(-ENOMEM);
815 OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
817 oh->ot_quota_trans = &oti->oti_quota_trans;
818 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
822 th->th_tags = LCT_TX_HANDLE;
824 oti->oti_dev = osd_dt_dev(d);
825 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
828 memset(oti->oti_declare_ops, 0,
829 sizeof(oti->oti_declare_ops));
830 memset(oti->oti_declare_ops_rb, 0,
831 sizeof(oti->oti_declare_ops_rb));
832 memset(oti->oti_declare_ops_cred, 0,
833 sizeof(oti->oti_declare_ops_cred));
834 oti->oti_rollback = false;
840 * Concurrency: shouldn't matter.
842 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
845 struct osd_thread_info *oti = osd_oti_get(env);
846 struct osd_device *dev = osd_dt_dev(d);
848 struct osd_thandle *oh;
853 LASSERT(current->journal_info == NULL);
855 oh = container_of0(th, struct osd_thandle, ot_super);
857 LASSERT(oh->ot_handle == NULL);
859 rc = dt_txn_hook_start(env, d, th);
863 if (unlikely(osd_param_is_not_sane(dev, th))) {
864 static unsigned long last_printed;
865 static int last_credits;
867 CWARN("%.16s: too many transaction credits (%d > %d)\n",
868 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
870 osd_journal(dev)->j_max_transaction_buffers);
871 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
872 oti->oti_declare_ops[OSD_OT_CREATE],
873 oti->oti_declare_ops_cred[OSD_OT_CREATE],
874 oti->oti_declare_ops[OSD_OT_DELETE],
875 oti->oti_declare_ops_cred[OSD_OT_DELETE],
876 oti->oti_declare_ops[OSD_OT_DESTROY],
877 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
878 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
879 oti->oti_declare_ops[OSD_OT_ATTR_SET],
880 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
881 oti->oti_declare_ops[OSD_OT_XATTR_SET],
882 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
883 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
884 oti->oti_declare_ops[OSD_OT_WRITE],
885 oti->oti_declare_ops_cred[OSD_OT_WRITE],
886 oti->oti_declare_ops[OSD_OT_PUNCH],
887 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
888 oti->oti_declare_ops[OSD_OT_QUOTA],
889 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
890 CWARN(" insert: %u/%u, delete: %u/%u\n",
891 oti->oti_declare_ops[OSD_OT_INSERT],
892 oti->oti_declare_ops_cred[OSD_OT_INSERT],
893 oti->oti_declare_ops[OSD_OT_DESTROY],
894 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
895 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
896 oti->oti_declare_ops[OSD_OT_REF_ADD],
897 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
898 oti->oti_declare_ops[OSD_OT_REF_DEL],
899 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
901 if (last_credits != oh->ot_credits &&
902 time_after(jiffies, last_printed + 60 * HZ)) {
903 libcfs_debug_dumpstack(NULL);
904 last_credits = oh->ot_credits;
905 last_printed = jiffies;
907 /* XXX Limit the credits to 'max_transaction_buffers', and
908 * let the underlying filesystem to catch the error if
909 * we really need so many credits.
911 * This should be removed when we can calculate the
912 * credits precisely. */
913 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
917 * XXX temporary stuff. Some abstraction layer should
920 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
924 LASSERT(oti->oti_txns == 0);
925 lu_context_init(&th->th_ctx, th->th_tags);
926 lu_context_enter(&th->th_ctx);
928 lu_device_get(&d->dd_lu_dev);
929 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
940 static int osd_seq_exists(const struct lu_env *env,
941 struct osd_device *osd, obd_seq seq)
943 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
944 struct seq_server_site *ss = osd_seq_site(osd);
951 rc = osd_fld_lookup(env, osd, seq, range);
954 CERROR("%s: can't lookup FLD sequence "LPX64
955 ": rc = %d\n", osd_name(osd), seq, rc);
959 RETURN(ss->ss_node_id == range->lsr_index);
963 * Concurrency: shouldn't matter.
965 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
968 struct osd_thandle *oh;
969 struct osd_thread_info *oti = osd_oti_get(env);
970 struct osd_iobuf *iobuf = &oti->oti_iobuf;
971 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
974 oh = container_of0(th, struct osd_thandle, ot_super);
977 /* inform the quota slave device that the transaction is
979 qsd_op_end(env, qsd, oh->ot_quota_trans);
980 oh->ot_quota_trans = NULL;
982 if (oh->ot_handle != NULL) {
983 handle_t *hdl = oh->ot_handle;
986 * add commit callback
987 * notice we don't do this in osd_trans_start()
988 * as underlying transaction can change during truncate
990 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
993 LASSERT(oti->oti_txns == 1);
995 rc = dt_txn_hook_stop(env, th);
997 CERROR("Failure in transaction hook: %d\n", rc);
999 /* hook functions might modify th_sync */
1000 hdl->h_sync = th->th_sync;
1002 oh->ot_handle = NULL;
1003 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
1004 rc = ldiskfs_journal_stop(hdl));
1006 CERROR("Failure to stop transaction: %d\n", rc);
1011 /* as we want IO to journal and data IO be concurrent, we don't block
1012 * awaiting data IO completion in osd_do_bio(), instead we wait here
1013 * once transaction is submitted to the journal. all reqular requests
1014 * don't do direct IO (except read/write), thus this wait_event becomes
1017 * IMPORTANT: we have to wait till any IO submited by the thread is
1018 * completed otherwise iobuf may be corrupted by different request
1020 wait_event(iobuf->dr_wait,
1021 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
1023 rc = iobuf->dr_error;
1028 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1030 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1033 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1034 LASSERT(&dcb->dcb_func != NULL);
1035 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
1041 * Called just before object is freed. Releases all resources except for
1042 * object itself (that is released by osd_object_free()).
1044 * Concurrency: no concurrent access is possible that late in object
1047 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1049 struct osd_object *obj = osd_obj(l);
1050 struct inode *inode = obj->oo_inode;
1052 LINVRNT(osd_invariant(obj));
1055 * If object is unlinked remove fid->ino mapping from object index.
1058 osd_index_fini(obj);
1059 if (inode != NULL) {
1060 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1061 qid_t uid = inode->i_uid;
1062 qid_t gid = inode->i_gid;
1065 obj->oo_inode = NULL;
1068 struct osd_thread_info *info = osd_oti_get(env);
1069 struct lquota_id_info *qi = &info->oti_qi;
1071 /* Release granted quota to master if necessary */
1072 qi->lqi_id.qid_uid = uid;
1073 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1075 qi->lqi_id.qid_uid = gid;
1076 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1082 * Concurrency: ->loo_object_release() is called under site spin-lock.
1084 static void osd_object_release(const struct lu_env *env,
1085 struct lu_object *l)
1090 * Concurrency: shouldn't matter.
1092 static int osd_object_print(const struct lu_env *env, void *cookie,
1093 lu_printer_t p, const struct lu_object *l)
1095 struct osd_object *o = osd_obj(l);
1096 struct iam_descr *d;
1098 if (o->oo_dir != NULL)
1099 d = o->oo_dir->od_container.ic_descr;
1102 return (*p)(env, cookie,
1103 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1105 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1106 o->oo_inode ? o->oo_inode->i_generation : 0,
1107 d ? d->id_ops->id_name : "plain");
1111 * Concurrency: shouldn't matter.
1113 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1114 struct obd_statfs *sfs)
1116 struct osd_device *osd = osd_dt_dev(d);
1117 struct super_block *sb = osd_sb(osd);
1118 struct kstatfs *ksfs;
1121 if (unlikely(osd->od_mnt == NULL))
1122 return -EINPROGRESS;
1124 /* osd_lproc.c call this without env, allocate ksfs for that case */
1125 if (unlikely(env == NULL)) {
1126 OBD_ALLOC_PTR(ksfs);
1130 ksfs = &osd_oti_get(env)->oti_ksfs;
1133 spin_lock(&osd->od_osfs_lock);
1134 /* cache 1 second */
1135 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1136 result = sb->s_op->statfs(sb->s_root, ksfs);
1137 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1138 osd->od_osfs_age = cfs_time_current_64();
1139 statfs_pack(&osd->od_statfs, ksfs);
1140 if (sb->s_flags & MS_RDONLY)
1141 sfs->os_state = OS_STATE_READONLY;
1145 if (likely(result == 0))
1146 *sfs = osd->od_statfs;
1147 spin_unlock(&osd->od_osfs_lock);
1149 if (unlikely(env == NULL))
1156 * Estimate space needed for file creations. We assume the largest filename
1157 * which is 2^64 - 1, hence a filename of 20 chars.
1158 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1160 #ifdef __LDISKFS_DIR_REC_LEN
1161 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1163 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1167 * Concurrency: doesn't access mutable data.
1169 static void osd_conf_get(const struct lu_env *env,
1170 const struct dt_device *dev,
1171 struct dt_device_param *param)
1173 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1176 * XXX should be taken from not-yet-existing fs abstraction layer.
1178 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1179 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1180 param->ddp_block_shift = sb->s_blocksize_bits;
1181 param->ddp_mount_type = LDD_MT_LDISKFS;
1182 param->ddp_maxbytes = sb->s_maxbytes;
1183 /* Overhead estimate should be fairly accurate, so we really take a tiny
1184 * error margin which also avoids fragmenting the filesystem too much */
1185 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1186 /* inode are statically allocated, so per-inode space consumption
1187 * is the space consumed by the directory entry */
1188 param->ddp_inodespace = PER_OBJ_USAGE;
1189 /* per-fragment overhead to be used by the client code */
1190 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1191 param->ddp_mntopts = 0;
1192 if (test_opt(sb, XATTR_USER))
1193 param->ddp_mntopts |= MNTOPT_USERXATTR;
1194 if (test_opt(sb, POSIX_ACL))
1195 param->ddp_mntopts |= MNTOPT_ACL;
1197 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1198 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1199 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1202 param->ddp_max_ea_size = sb->s_blocksize;
1207 * Concurrency: shouldn't matter.
1209 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1211 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1212 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1216 * Start commit for OSD device.
1218 * An implementation of dt_commit_async method for OSD device.
1219 * Asychronously starts underlayng fs sync and thereby a transaction
1222 * \param env environment
1223 * \param d dt device
1225 * \see dt_device_operations
1227 static int osd_commit_async(const struct lu_env *env,
1228 struct dt_device *d)
1230 struct super_block *s = osd_sb(osd_dt_dev(d));
1233 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1234 RETURN(s->s_op->sync_fs(s, 0));
1238 * Concurrency: shouldn't matter.
1241 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1243 struct super_block *sb = osd_sb(osd_dt_dev(d));
1244 struct block_device *dev = sb->s_bdev;
1245 #ifdef HAVE_DEV_SET_RDONLY
1246 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1249 int rc = -EOPNOTSUPP;
1253 #ifdef HAVE_DEV_SET_RDONLY
1254 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1256 if (jdev && (jdev != dev)) {
1257 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1259 dev_set_rdonly(jdev);
1261 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1262 dev_set_rdonly(dev);
1264 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1265 osd_dt_dev(d)->od_svname, (long)dev, rc);
1271 * Concurrency: serialization provided by callers.
1273 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1274 int mode, unsigned long timeout, __u32 alg,
1275 struct lustre_capa_key *keys)
1277 struct osd_device *dev = osd_dt_dev(d);
1280 dev->od_fl_capa = mode;
1281 dev->od_capa_timeout = timeout;
1282 dev->od_capa_alg = alg;
1283 dev->od_capa_keys = keys;
1288 * Note: we do not count into QUOTA here.
1289 * If we mount with --data_journal we may need more.
1291 const int osd_dto_credits_noquota[DTO_NR] = {
1294 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1295 * SINGLEDATA_TRANS_BLOCKS(8)
1296 * XXX Note: maybe iam need more, since iam have more level than
1299 [DTO_INDEX_INSERT] = 16,
1300 [DTO_INDEX_DELETE] = 16,
1304 [DTO_INDEX_UPDATE] = 16,
1306 * Create a object. The same as create object in EXT3.
1307 * DATA_TRANS_BLOCKS(14) +
1308 * INDEX_EXTRA_BLOCKS(8) +
1309 * 3(inode bits, groups, GDT)
1311 [DTO_OBJECT_CREATE] = 25,
1313 * XXX: real credits to be fixed
1315 [DTO_OBJECT_DELETE] = 25,
1317 * Attr set credits (inode)
1319 [DTO_ATTR_SET_BASE] = 1,
1321 * Xattr set. The same as xattr of EXT3.
1322 * DATA_TRANS_BLOCKS(14)
1323 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1324 * are also counted in. Do not know why?
1326 [DTO_XATTR_SET] = 14,
1329 * credits for inode change during write.
1331 [DTO_WRITE_BASE] = 3,
1333 * credits for single block write.
1335 [DTO_WRITE_BLOCK] = 14,
1337 * Attr set credits for chown.
1338 * This is extra credits for setattr, and it is null without quota
1340 [DTO_ATTR_SET_CHOWN]= 0
1343 static const struct dt_device_operations osd_dt_ops = {
1344 .dt_root_get = osd_root_get,
1345 .dt_statfs = osd_statfs,
1346 .dt_trans_create = osd_trans_create,
1347 .dt_trans_start = osd_trans_start,
1348 .dt_trans_stop = osd_trans_stop,
1349 .dt_trans_cb_add = osd_trans_cb_add,
1350 .dt_conf_get = osd_conf_get,
1351 .dt_sync = osd_sync,
1353 .dt_commit_async = osd_commit_async,
1354 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1357 static void osd_object_read_lock(const struct lu_env *env,
1358 struct dt_object *dt, unsigned role)
1360 struct osd_object *obj = osd_dt_obj(dt);
1361 struct osd_thread_info *oti = osd_oti_get(env);
1363 LINVRNT(osd_invariant(obj));
1365 LASSERT(obj->oo_owner != env);
1366 down_read_nested(&obj->oo_sem, role);
1368 LASSERT(obj->oo_owner == NULL);
1372 static void osd_object_write_lock(const struct lu_env *env,
1373 struct dt_object *dt, unsigned role)
1375 struct osd_object *obj = osd_dt_obj(dt);
1376 struct osd_thread_info *oti = osd_oti_get(env);
1378 LINVRNT(osd_invariant(obj));
1380 LASSERT(obj->oo_owner != env);
1381 down_write_nested(&obj->oo_sem, role);
1383 LASSERT(obj->oo_owner == NULL);
1384 obj->oo_owner = env;
1388 static void osd_object_read_unlock(const struct lu_env *env,
1389 struct dt_object *dt)
1391 struct osd_object *obj = osd_dt_obj(dt);
1392 struct osd_thread_info *oti = osd_oti_get(env);
1394 LINVRNT(osd_invariant(obj));
1396 LASSERT(oti->oti_r_locks > 0);
1398 up_read(&obj->oo_sem);
1401 static void osd_object_write_unlock(const struct lu_env *env,
1402 struct dt_object *dt)
1404 struct osd_object *obj = osd_dt_obj(dt);
1405 struct osd_thread_info *oti = osd_oti_get(env);
1407 LINVRNT(osd_invariant(obj));
1409 LASSERT(obj->oo_owner == env);
1410 LASSERT(oti->oti_w_locks > 0);
1412 obj->oo_owner = NULL;
1413 up_write(&obj->oo_sem);
1416 static int osd_object_write_locked(const struct lu_env *env,
1417 struct dt_object *dt)
1419 struct osd_object *obj = osd_dt_obj(dt);
1421 LINVRNT(osd_invariant(obj));
1423 return obj->oo_owner == env;
1426 static int capa_is_sane(const struct lu_env *env,
1427 struct osd_device *dev,
1428 struct lustre_capa *capa,
1429 struct lustre_capa_key *keys)
1431 struct osd_thread_info *oti = osd_oti_get(env);
1432 struct lustre_capa *tcapa = &oti->oti_capa;
1433 struct obd_capa *oc;
1437 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1439 if (capa_is_expired(oc)) {
1440 DEBUG_CAPA(D_ERROR, capa, "expired");
1447 if (capa_is_expired_sec(capa)) {
1448 DEBUG_CAPA(D_ERROR, capa, "expired");
1452 spin_lock(&capa_lock);
1453 for (i = 0; i < 2; i++) {
1454 if (keys[i].lk_keyid == capa->lc_keyid) {
1455 oti->oti_capa_key = keys[i];
1459 spin_unlock(&capa_lock);
1462 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1466 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1470 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1471 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1475 oc = capa_add(dev->od_capa_hash, capa);
1481 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1482 struct lustre_capa *capa, __u64 opc)
1484 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1485 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1486 struct lu_capainfo *lci;
1489 if (!osd->od_fl_capa)
1492 if (capa == BYPASS_CAPA)
1495 lci = lu_capainfo_get(env);
1496 if (unlikely(lci == NULL))
1499 if (lci->lci_auth == LC_ID_NONE)
1503 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1504 osd_name(osd), PFID(fid), -EACCES);
1508 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1509 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1514 if (!capa_opc_supported(capa, opc)) {
1515 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1519 rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1521 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1528 static struct timespec *osd_inode_time(const struct lu_env *env,
1529 struct inode *inode, __u64 seconds)
1531 struct osd_thread_info *oti = osd_oti_get(env);
1532 struct timespec *t = &oti->oti_time;
1534 t->tv_sec = seconds;
1536 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1541 static void osd_inode_getattr(const struct lu_env *env,
1542 struct inode *inode, struct lu_attr *attr)
1544 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1545 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1546 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1549 attr->la_atime = LTIME_S(inode->i_atime);
1550 attr->la_mtime = LTIME_S(inode->i_mtime);
1551 attr->la_ctime = LTIME_S(inode->i_ctime);
1552 attr->la_mode = inode->i_mode;
1553 attr->la_size = i_size_read(inode);
1554 attr->la_blocks = inode->i_blocks;
1555 attr->la_uid = inode->i_uid;
1556 attr->la_gid = inode->i_gid;
1557 attr->la_flags = LDISKFS_I(inode)->i_flags;
1558 attr->la_nlink = inode->i_nlink;
1559 attr->la_rdev = inode->i_rdev;
1560 attr->la_blksize = 1 << inode->i_blkbits;
1561 attr->la_blkbits = inode->i_blkbits;
1564 static int osd_attr_get(const struct lu_env *env,
1565 struct dt_object *dt,
1566 struct lu_attr *attr,
1567 struct lustre_capa *capa)
1569 struct osd_object *obj = osd_dt_obj(dt);
1571 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1572 LINVRNT(osd_invariant(obj));
1574 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1577 spin_lock(&obj->oo_guard);
1578 osd_inode_getattr(env, obj->oo_inode, attr);
1579 spin_unlock(&obj->oo_guard);
1583 static int osd_declare_attr_set(const struct lu_env *env,
1584 struct dt_object *dt,
1585 const struct lu_attr *attr,
1586 struct thandle *handle)
1588 struct osd_thandle *oh;
1589 struct osd_object *obj;
1590 struct osd_thread_info *info = osd_oti_get(env);
1591 struct lquota_id_info *qi = &info->oti_qi;
1597 LASSERT(dt != NULL);
1598 LASSERT(handle != NULL);
1600 obj = osd_dt_obj(dt);
1601 LASSERT(osd_invariant(obj));
1603 oh = container_of0(handle, struct osd_thandle, ot_super);
1604 LASSERT(oh->ot_handle == NULL);
1606 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1607 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1609 if (attr == NULL || obj->oo_inode == NULL)
1612 bspace = obj->oo_inode->i_blocks;
1613 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1614 bspace = toqb(bspace);
1616 /* Changing ownership is always preformed by super user, it should not
1619 * We still need to call the osd_declare_qid() to calculate the journal
1620 * credits for updating quota accounting files and to trigger quota
1621 * space adjustment once the operation is completed.*/
1622 if ((attr->la_valid & LA_UID) != 0 &&
1623 attr->la_uid != obj->oo_inode->i_uid) {
1624 qi->lqi_type = USRQUOTA;
1626 /* inode accounting */
1627 qi->lqi_is_blk = false;
1629 /* one more inode for the new owner ... */
1630 qi->lqi_id.qid_uid = attr->la_uid;
1632 allocated = (attr->la_uid == 0) ? true : false;
1633 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1634 if (rc == -EDQUOT || rc == -EINPROGRESS)
1639 /* and one less inode for the current uid */
1640 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1642 rc = osd_declare_qid(env, oh, qi, true, NULL);
1643 if (rc == -EDQUOT || rc == -EINPROGRESS)
1648 /* block accounting */
1649 qi->lqi_is_blk = true;
1651 /* more blocks for the new owner ... */
1652 qi->lqi_id.qid_uid = attr->la_uid;
1653 qi->lqi_space = bspace;
1654 allocated = (attr->la_uid == 0) ? true : false;
1655 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1656 if (rc == -EDQUOT || rc == -EINPROGRESS)
1661 /* and finally less blocks for the current owner */
1662 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1663 qi->lqi_space = -bspace;
1664 rc = osd_declare_qid(env, oh, qi, true, NULL);
1665 if (rc == -EDQUOT || rc == -EINPROGRESS)
1671 if (attr->la_valid & LA_GID &&
1672 attr->la_gid != obj->oo_inode->i_gid) {
1673 qi->lqi_type = GRPQUOTA;
1675 /* inode accounting */
1676 qi->lqi_is_blk = false;
1678 /* one more inode for the new group owner ... */
1679 qi->lqi_id.qid_gid = attr->la_gid;
1681 allocated = (attr->la_gid == 0) ? true : false;
1682 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1683 if (rc == -EDQUOT || rc == -EINPROGRESS)
1688 /* and one less inode for the current gid */
1689 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1691 rc = osd_declare_qid(env, oh, qi, true, NULL);
1692 if (rc == -EDQUOT || rc == -EINPROGRESS)
1697 /* block accounting */
1698 qi->lqi_is_blk = true;
1700 /* more blocks for the new owner ... */
1701 qi->lqi_id.qid_gid = attr->la_gid;
1702 qi->lqi_space = bspace;
1703 allocated = (attr->la_gid == 0) ? true : false;
1704 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1705 if (rc == -EDQUOT || rc == -EINPROGRESS)
1710 /* and finally less blocks for the current owner */
1711 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1712 qi->lqi_space = -bspace;
1713 rc = osd_declare_qid(env, oh, qi, true, NULL);
1714 if (rc == -EDQUOT || rc == -EINPROGRESS)
1723 static int osd_inode_setattr(const struct lu_env *env,
1724 struct inode *inode, const struct lu_attr *attr)
1728 bits = attr->la_valid;
1730 if (bits & LA_ATIME)
1731 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1732 if (bits & LA_CTIME)
1733 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1734 if (bits & LA_MTIME)
1735 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1736 if (bits & LA_SIZE) {
1737 LDISKFS_I(inode)->i_disksize = attr->la_size;
1738 i_size_write(inode, attr->la_size);
1742 /* OSD should not change "i_blocks" which is used by quota.
1743 * "i_blocks" should be changed by ldiskfs only. */
1744 if (bits & LA_BLOCKS)
1745 inode->i_blocks = attr->la_blocks;
1748 inode->i_mode = (inode->i_mode & S_IFMT) |
1749 (attr->la_mode & ~S_IFMT);
1751 inode->i_uid = attr->la_uid;
1753 inode->i_gid = attr->la_gid;
1754 if (bits & LA_NLINK)
1755 set_nlink(inode, attr->la_nlink);
1757 inode->i_rdev = attr->la_rdev;
1759 if (bits & LA_FLAGS) {
1760 /* always keep S_NOCMTIME */
1761 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1767 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1769 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1770 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1775 if (attr->la_valid & LA_UID)
1776 iattr.ia_valid |= ATTR_UID;
1777 if (attr->la_valid & LA_GID)
1778 iattr.ia_valid |= ATTR_GID;
1779 iattr.ia_uid = attr->la_uid;
1780 iattr.ia_gid = attr->la_gid;
1782 rc = ll_vfs_dq_transfer(inode, &iattr);
1784 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1785 "enforcement enabled on the ldiskfs filesystem?",
1786 inode->i_sb->s_id, rc);
1793 static int osd_attr_set(const struct lu_env *env,
1794 struct dt_object *dt,
1795 const struct lu_attr *attr,
1796 struct thandle *handle,
1797 struct lustre_capa *capa)
1799 struct osd_object *obj = osd_dt_obj(dt);
1800 struct inode *inode;
1803 LASSERT(handle != NULL);
1804 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1805 LASSERT(osd_invariant(obj));
1807 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1810 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1812 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1813 struct osd_thread_info *oti = osd_oti_get(env);
1814 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1815 struct lu_fid *fid1 = &oti->oti_fid;
1816 struct osd_inode_id *id = &oti->oti_id;
1817 struct iam_path_descr *ipd;
1818 struct iam_container *bag;
1819 struct osd_thandle *oh;
1822 fid_cpu_to_be(fid1, fid0);
1823 memset(id, 1, sizeof(*id));
1824 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1825 fid0)->oi_dir.od_container;
1826 ipd = osd_idx_ipd_get(env, bag);
1827 if (unlikely(ipd == NULL))
1830 oh = container_of0(handle, struct osd_thandle, ot_super);
1831 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1832 (const struct iam_rec *)id, ipd);
1833 osd_ipd_put(env, bag, ipd);
1834 return(rc > 0 ? 0 : rc);
1837 inode = obj->oo_inode;
1838 ll_vfs_dq_init(inode);
1840 rc = osd_quota_transfer(inode, attr);
1844 spin_lock(&obj->oo_guard);
1845 rc = osd_inode_setattr(env, inode, attr);
1846 spin_unlock(&obj->oo_guard);
1849 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1853 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1854 struct osd_object *obj,
1855 const char *name, const int namelen)
1857 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1860 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1861 umode_t mode, struct dt_allocation_hint *hint,
1865 struct osd_device *osd = osd_obj2dev(obj);
1866 struct osd_thandle *oth;
1867 struct dt_object *parent = NULL;
1868 struct inode *inode;
1870 LINVRNT(osd_invariant(obj));
1871 LASSERT(obj->oo_inode == NULL);
1872 LASSERT(obj->oo_hl_head == NULL);
1874 if (S_ISDIR(mode) && ldiskfs_pdo) {
1875 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1876 if (obj->oo_hl_head == NULL)
1880 oth = container_of(th, struct osd_thandle, ot_super);
1881 LASSERT(oth->ot_handle->h_transaction != NULL);
1883 if (hint && hint->dah_parent)
1884 parent = hint->dah_parent;
1886 inode = ldiskfs_create_inode(oth->ot_handle,
1887 parent ? osd_dt_obj(parent)->oo_inode :
1888 osd_sb(osd)->s_root->d_inode,
1890 if (!IS_ERR(inode)) {
1891 /* Do not update file c/mtime in ldiskfs.
1892 * NB: don't need any lock because no contention at this
1894 inode->i_flags |= S_NOCMTIME;
1896 /* For new created object, it must be consistent,
1897 * and it is unnecessary to scrub against it. */
1898 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1899 obj->oo_inode = inode;
1902 if (obj->oo_hl_head != NULL) {
1903 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1904 obj->oo_hl_head = NULL;
1906 result = PTR_ERR(inode);
1908 LINVRNT(osd_invariant(obj));
1916 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1917 struct lu_attr *attr,
1918 struct dt_allocation_hint *hint,
1919 struct dt_object_format *dof,
1923 struct osd_thandle *oth;
1924 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1926 LASSERT(S_ISDIR(attr->la_mode));
1928 oth = container_of(th, struct osd_thandle, ot_super);
1929 LASSERT(oth->ot_handle->h_transaction != NULL);
1930 result = osd_mkfile(info, obj, mode, hint, th);
1935 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1936 struct lu_attr *attr,
1937 struct dt_allocation_hint *hint,
1938 struct dt_object_format *dof,
1942 struct osd_thandle *oth;
1943 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1945 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1947 LASSERT(S_ISREG(attr->la_mode));
1949 oth = container_of(th, struct osd_thandle, ot_super);
1950 LASSERT(oth->ot_handle->h_transaction != NULL);
1952 result = osd_mkfile(info, obj, mode, hint, th);
1954 LASSERT(obj->oo_inode != NULL);
1955 if (feat->dif_flags & DT_IND_VARKEY)
1956 result = iam_lvar_create(obj->oo_inode,
1957 feat->dif_keysize_max,
1959 feat->dif_recsize_max,
1962 result = iam_lfix_create(obj->oo_inode,
1963 feat->dif_keysize_max,
1965 feat->dif_recsize_max,
1972 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1973 struct lu_attr *attr,
1974 struct dt_allocation_hint *hint,
1975 struct dt_object_format *dof,
1978 LASSERT(S_ISREG(attr->la_mode));
1979 return osd_mkfile(info, obj, (attr->la_mode &
1980 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1983 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1984 struct lu_attr *attr,
1985 struct dt_allocation_hint *hint,
1986 struct dt_object_format *dof,
1989 LASSERT(S_ISLNK(attr->la_mode));
1990 return osd_mkfile(info, obj, (attr->la_mode &
1991 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1994 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1995 struct lu_attr *attr,
1996 struct dt_allocation_hint *hint,
1997 struct dt_object_format *dof,
2000 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2003 LINVRNT(osd_invariant(obj));
2004 LASSERT(obj->oo_inode == NULL);
2005 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2006 S_ISFIFO(mode) || S_ISSOCK(mode));
2008 result = osd_mkfile(info, obj, mode, hint, th);
2010 LASSERT(obj->oo_inode != NULL);
2012 * This inode should be marked dirty for i_rdev. Currently
2013 * that is done in the osd_attr_init().
2015 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2018 LINVRNT(osd_invariant(obj));
2022 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2024 struct dt_allocation_hint *hint,
2025 struct dt_object_format *dof,
2028 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2030 osd_obj_type_f result;
2046 result = osd_mk_index;
2057 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2058 struct dt_object *parent, struct dt_object *child,
2063 memset(ah, 0, sizeof(*ah));
2064 ah->dah_parent = parent;
2065 ah->dah_mode = child_mode;
2068 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2069 struct lu_attr *attr, struct dt_object_format *dof)
2071 struct inode *inode = obj->oo_inode;
2072 __u64 valid = attr->la_valid;
2075 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2077 if (dof->dof_type != DFT_NODE)
2078 attr->la_valid &= ~LA_RDEV;
2079 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2080 attr->la_valid &= ~LA_ATIME;
2081 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2082 attr->la_valid &= ~LA_CTIME;
2083 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2084 attr->la_valid &= ~LA_MTIME;
2086 result = osd_quota_transfer(inode, attr);
2090 if (attr->la_valid != 0) {
2091 result = osd_inode_setattr(info->oti_env, inode, attr);
2093 * The osd_inode_setattr() should always succeed here. The
2094 * only error that could be returned is EDQUOT when we are
2095 * trying to change the UID or GID of the inode. However, this
2096 * should not happen since quota enforcement is no longer
2097 * enabled on ldiskfs (lquota takes care of it).
2099 LASSERTF(result == 0, "%d", result);
2100 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2103 attr->la_valid = valid;
2107 * Helper function for osd_object_create()
2109 * \retval 0, on success
2111 static int __osd_object_create(struct osd_thread_info *info,
2112 struct osd_object *obj, struct lu_attr *attr,
2113 struct dt_allocation_hint *hint,
2114 struct dt_object_format *dof,
2120 /* we drop umask so that permissions we pass are not affected */
2121 umask = current->fs->umask;
2122 current->fs->umask = 0;
2124 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2127 osd_attr_init(info, obj, attr, dof);
2128 osd_object_init0(obj);
2130 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2131 unlock_new_inode(obj->oo_inode);
2134 /* restore previous umask value */
2135 current->fs->umask = umask;
2141 * Helper function for osd_object_create()
2143 * \retval 0, on success
2145 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2146 const struct lu_fid *fid, struct thandle *th)
2148 struct osd_thread_info *info = osd_oti_get(env);
2149 struct osd_inode_id *id = &info->oti_id;
2150 struct osd_device *osd = osd_obj2dev(obj);
2152 LASSERT(obj->oo_inode != NULL);
2154 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2155 return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2158 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2159 obd_seq seq, struct lu_seq_range *range)
2161 struct seq_server_site *ss = osd_seq_site(osd);
2163 if (fid_seq_is_idif(seq)) {
2164 fld_range_set_ost(range);
2165 range->lsr_index = idif_ost_idx(seq);
2169 if (!fid_seq_in_fldb(seq)) {
2170 fld_range_set_mdt(range);
2172 /* FIXME: If ss is NULL, it suppose not get lsr_index
2174 range->lsr_index = ss->ss_node_id;
2178 LASSERT(ss != NULL);
2179 fld_range_set_any(range);
2180 /* OSD will only do local fld lookup */
2181 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2185 * Concurrency: no external locking is necessary.
2187 static int osd_declare_object_create(const struct lu_env *env,
2188 struct dt_object *dt,
2189 struct lu_attr *attr,
2190 struct dt_allocation_hint *hint,
2191 struct dt_object_format *dof,
2192 struct thandle *handle)
2194 struct osd_thandle *oh;
2198 LASSERT(handle != NULL);
2200 oh = container_of0(handle, struct osd_thandle, ot_super);
2201 LASSERT(oh->ot_handle == NULL);
2203 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2204 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2205 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2206 lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2207 /* Reuse idle OI block may cause additional one OI block
2209 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2210 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2212 /* If this is directory, then we expect . and .. to be inserted as
2213 * well. The one directory block always needs to be created for the
2214 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2215 * block), there is no danger of needing a tree for the first block.
2217 if (attr && S_ISDIR(attr->la_mode)) {
2218 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2219 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2220 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2226 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2227 false, false, NULL, false);
2234 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2235 struct lu_attr *attr,
2236 struct dt_allocation_hint *hint,
2237 struct dt_object_format *dof,
2240 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2241 struct osd_object *obj = osd_dt_obj(dt);
2242 struct osd_thread_info *info = osd_oti_get(env);
2247 LINVRNT(osd_invariant(obj));
2248 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2249 LASSERT(osd_write_locked(env, obj));
2250 LASSERT(th != NULL);
2252 if (unlikely(fid_is_acct(fid)))
2253 /* Quota files can't be created from the kernel any more,
2254 * 'tune2fs -O quota' will take care of creating them */
2257 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2258 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2260 result = __osd_object_create(info, obj, attr, hint, dof, th);
2262 result = __osd_oi_insert(env, obj, fid, th);
2264 LASSERT(ergo(result == 0,
2265 dt_object_exists(dt) && !dt_object_remote(dt)));
2267 LASSERT(osd_invariant(obj));
2272 * Called to destroy on-disk representation of the object
2274 * Concurrency: must be locked
2276 static int osd_declare_object_destroy(const struct lu_env *env,
2277 struct dt_object *dt,
2280 struct osd_object *obj = osd_dt_obj(dt);
2281 struct inode *inode = obj->oo_inode;
2282 struct osd_thandle *oh;
2286 oh = container_of0(th, struct osd_thandle, ot_super);
2287 LASSERT(oh->ot_handle == NULL);
2290 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2291 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2292 /* Recycle idle OI leaf may cause additional three OI blocks
2294 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2295 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2296 /* one less inode */
2297 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2298 false, true, NULL, false);
2301 /* data to be truncated */
2302 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2303 true, true, NULL, false);
2307 static int osd_object_destroy(const struct lu_env *env,
2308 struct dt_object *dt,
2311 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2312 struct osd_object *obj = osd_dt_obj(dt);
2313 struct inode *inode = obj->oo_inode;
2314 struct osd_device *osd = osd_obj2dev(obj);
2315 struct osd_thandle *oh;
2319 oh = container_of0(th, struct osd_thandle, ot_super);
2320 LASSERT(oh->ot_handle);
2322 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2324 if (unlikely(fid_is_acct(fid)))
2327 if (S_ISDIR(inode->i_mode)) {
2328 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2329 inode->i_nlink == 2);
2330 /* it will check/delete the inode from remote parent,
2331 * how to optimize it? unlink performance impaction XXX */
2332 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2333 if (result != 0 && result != -ENOENT) {
2334 CERROR("%s: delete inode "DFID": rc = %d\n",
2335 osd_name(osd), PFID(fid), result);
2337 spin_lock(&obj->oo_guard);
2339 spin_unlock(&obj->oo_guard);
2340 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2343 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2345 result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2347 /* XXX: add to ext3 orphan list */
2348 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2350 /* not needed in the cache anymore */
2351 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2357 * Put the fid into lustre_mdt_attrs, and then place the structure
2358 * inode's ea. This fid should not be altered during the life time
2361 * \retval +ve, on success
2362 * \retval -ve, on error
2364 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2366 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2367 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2369 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2373 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2376 lustre_lma_init(lma, fid, compat, incompat);
2377 lustre_lma_swab(lma);
2379 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2381 /* LMA may already exist, but we need to check that all the
2382 * desired compat/incompat flags have been added. */
2383 if (unlikely(rc == -EEXIST)) {
2384 if (compat == 0 && incompat == 0)
2387 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2388 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2393 lustre_lma_swab(lma);
2394 if (!(~lma->lma_compat & compat) &&
2395 !(~lma->lma_incompat & incompat))
2398 lma->lma_compat |= compat;
2399 lma->lma_incompat |= incompat;
2400 lustre_lma_swab(lma);
2401 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2402 sizeof(*lma), XATTR_REPLACE);
2409 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2410 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2411 * To have compatilibility with 1.8 ldiskfs driver we need to have
2412 * magic number at start of fid data.
2413 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2416 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2417 const struct dt_rec *fid)
2419 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2420 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2421 param->edp_magic = 0;
2425 param->edp_magic = LDISKFS_LUFID_MAGIC;
2426 param->edp_len = sizeof(struct lu_fid) + 1;
2427 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2431 * Try to read the fid from inode ea into dt_rec.
2433 * \param fid object fid.
2435 * \retval 0 on success
2437 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2438 __u32 ino, struct lu_fid *fid,
2439 struct osd_inode_id *id)
2441 struct osd_thread_info *info = osd_oti_get(env);
2442 struct inode *inode;
2445 osd_id_gen(id, ino, OSD_OII_NOGEN);
2446 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2448 RETURN(PTR_ERR(inode));
2454 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2456 struct inode *parent_dir,
2457 const struct dt_rec *dot_fid,
2458 const struct dt_rec *dot_dot_fid,
2459 struct osd_thandle *oth)
2461 struct ldiskfs_dentry_param *dot_ldp;
2462 struct ldiskfs_dentry_param *dot_dot_ldp;
2464 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2465 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2467 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2468 dot_ldp->edp_magic = 0;
2469 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2470 dir, dot_ldp, dot_dot_ldp);
2474 * Create an local agent inode for remote entry
2476 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2477 struct osd_device *osd,
2478 struct osd_object *pobj,
2479 const struct lu_fid *fid,
2482 struct osd_thread_info *info = osd_oti_get(env);
2483 struct inode *local;
2484 struct osd_thandle *oh;
2489 oh = container_of(th, struct osd_thandle, ot_super);
2490 LASSERT(oh->ot_handle->h_transaction != NULL);
2492 /* FIXME: Insert index api needs to know the mode of
2493 * the remote object. Just use S_IFDIR for now */
2494 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2495 if (IS_ERR(local)) {
2496 CERROR("%s: create local error %d\n", osd_name(osd),
2497 (int)PTR_ERR(local));
2501 /* Set special LMA flag for local agent inode */
2502 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2504 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2505 osd_name(osd), PFID(fid), rc);
2506 RETURN(ERR_PTR(rc));
2509 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2510 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2511 (const struct dt_rec *)fid, oh);
2513 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2514 osd_name(osd), PFID(fid), rc);
2515 RETURN(ERR_PTR(rc));
2522 * Delete local agent inode for remote entry
2524 static int osd_delete_local_agent_inode(const struct lu_env *env,
2525 struct osd_device *osd,
2526 const struct lu_fid *fid,
2527 __u32 ino, struct osd_thandle *oh)
2529 struct osd_thread_info *oti = osd_oti_get(env);
2530 struct osd_inode_id *id = &oti->oti_id;
2531 struct inode *inode;
2534 id->oii_ino = le32_to_cpu(ino);
2535 id->oii_gen = OSD_OII_NOGEN;
2536 inode = osd_iget(oti, osd, id);
2537 if (IS_ERR(inode)) {
2538 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2539 PFID(fid), id->oii_ino, id->oii_gen);
2540 RETURN(PTR_ERR(inode));
2544 mark_inode_dirty(inode);
2545 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2546 osd_name(osd), PFID(fid), inode->i_ino);
2552 * OSD layer object create function for interoperability mode (b11826).
2553 * This is mostly similar to osd_object_create(). Only difference being, fid is
2554 * inserted into inode ea here.
2556 * \retval 0, on success
2557 * \retval -ve, on error
2559 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2560 struct lu_attr *attr,
2561 struct dt_allocation_hint *hint,
2562 struct dt_object_format *dof,
2565 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2566 struct osd_object *obj = osd_dt_obj(dt);
2567 struct osd_thread_info *info = osd_oti_get(env);
2572 LASSERT(osd_invariant(obj));
2573 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2574 LASSERT(osd_write_locked(env, obj));
2575 LASSERT(th != NULL);
2577 if (unlikely(fid_is_acct(fid)))
2578 /* Quota files can't be created from the kernel any more,
2579 * 'tune2fs -O quota' will take care of creating them */
2582 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2583 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2585 result = __osd_object_create(info, obj, attr, hint, dof, th);
2587 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2588 fid_is_on_ost(info, osd_obj2dev(obj),
2589 fid, OI_CHECK_FLD) ?
2590 LMAC_FID_ON_OST : 0, 0);
2593 result = __osd_oi_insert(env, obj, fid, th);
2595 LASSERT(ergo(result == 0,
2596 dt_object_exists(dt) && !dt_object_remote(dt)));
2597 LINVRNT(osd_invariant(obj));
2601 static int osd_declare_object_ref_add(const struct lu_env *env,
2602 struct dt_object *dt,
2603 struct thandle *handle)
2605 struct osd_thandle *oh;
2607 /* it's possible that object doesn't exist yet */
2608 LASSERT(handle != NULL);
2610 oh = container_of0(handle, struct osd_thandle, ot_super);
2611 LASSERT(oh->ot_handle == NULL);
2613 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2614 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2620 * Concurrency: @dt is write locked.
2622 static int osd_object_ref_add(const struct lu_env *env,
2623 struct dt_object *dt, struct thandle *th)
2625 struct osd_object *obj = osd_dt_obj(dt);
2626 struct inode *inode = obj->oo_inode;
2627 struct osd_thandle *oh;
2630 LINVRNT(osd_invariant(obj));
2631 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2632 LASSERT(osd_write_locked(env, obj));
2633 LASSERT(th != NULL);
2635 oh = container_of0(th, struct osd_thandle, ot_super);
2636 LASSERT(oh->ot_handle != NULL);
2638 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2641 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2642 * (65000) subdirectories by storing "1" in i_nlink if the link count
2643 * would otherwise overflow. Directory tranversal tools understand
2644 * that (st_nlink == 1) indicates that the filesystem dose not track
2645 * hard links count on the directory, and will not abort subdirectory
2646 * scanning early once (st_nlink - 2) subdirs have been found.
2648 * This also has to properly handle the case of inodes with nlink == 0
2649 * in case they are being linked into the PENDING directory
2651 spin_lock(&obj->oo_guard);
2652 ldiskfs_inc_count(oh->ot_handle, inode);
2653 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2654 spin_unlock(&obj->oo_guard);
2656 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2657 LINVRNT(osd_invariant(obj));
2662 static int osd_declare_object_ref_del(const struct lu_env *env,
2663 struct dt_object *dt,
2664 struct thandle *handle)
2666 struct osd_thandle *oh;
2668 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2669 LASSERT(handle != NULL);
2671 oh = container_of0(handle, struct osd_thandle, ot_super);
2672 LASSERT(oh->ot_handle == NULL);
2674 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2675 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2681 * Concurrency: @dt is write locked.
2683 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2686 struct osd_object *obj = osd_dt_obj(dt);
2687 struct inode *inode = obj->oo_inode;
2688 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2689 struct osd_thandle *oh;
2691 LINVRNT(osd_invariant(obj));
2692 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2693 LASSERT(osd_write_locked(env, obj));
2694 LASSERT(th != NULL);
2696 oh = container_of0(th, struct osd_thandle, ot_super);
2697 LASSERT(oh->ot_handle != NULL);
2699 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2701 spin_lock(&obj->oo_guard);
2702 /* That can be result of upgrade from old Lustre version and
2703 * applied only to local files. Just skip this ref_del call.
2704 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2705 if (inode->i_nlink == 0) {
2706 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2707 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2708 ", maybe an upgraded file? (LU-3915)\n",
2709 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2710 spin_unlock(&obj->oo_guard);
2714 ldiskfs_dec_count(oh->ot_handle, inode);
2715 spin_unlock(&obj->oo_guard);
2717 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2718 LINVRNT(osd_invariant(obj));
2724 * Get the 64-bit version for an inode.
2726 static int osd_object_version_get(const struct lu_env *env,
2727 struct dt_object *dt, dt_obj_version_t *ver)
2729 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2731 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2732 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2733 *ver = LDISKFS_I(inode)->i_fs_version;
2738 * Concurrency: @dt is read locked.
2740 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2741 struct lu_buf *buf, const char *name,
2742 struct lustre_capa *capa)
2744 struct osd_object *obj = osd_dt_obj(dt);
2745 struct inode *inode = obj->oo_inode;
2746 struct osd_thread_info *info = osd_oti_get(env);
2747 struct dentry *dentry = &info->oti_obj_dentry;
2749 /* version get is not real XATTR but uses xattr API */
2750 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2751 /* for version we are just using xattr API but change inode
2753 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2754 osd_object_version_get(env, dt, buf->lb_buf);
2755 return sizeof(dt_obj_version_t);
2758 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2759 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2761 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2764 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2768 static int osd_declare_xattr_set(const struct lu_env *env,
2769 struct dt_object *dt,
2770 const struct lu_buf *buf, const char *name,
2771 int fl, struct thandle *handle)
2773 struct osd_thandle *oh;
2775 LASSERT(handle != NULL);
2777 oh = container_of0(handle, struct osd_thandle, ot_super);
2778 LASSERT(oh->ot_handle == NULL);
2780 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2781 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2782 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2783 osd_dto_credits_noquota[DTO_XATTR_SET]);
2789 * Set the 64-bit version for object
2791 static void osd_object_version_set(const struct lu_env *env,
2792 struct dt_object *dt,
2793 dt_obj_version_t *new_version)
2795 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2797 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2798 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2800 LDISKFS_I(inode)->i_fs_version = *new_version;
2801 /** Version is set after all inode operations are finished,
2802 * so we should mark it dirty here */
2803 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2807 * Concurrency: @dt is write locked.
2809 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2810 const struct lu_buf *buf, const char *name, int fl,
2811 struct thandle *handle, struct lustre_capa *capa)
2813 struct osd_object *obj = osd_dt_obj(dt);
2814 struct inode *inode = obj->oo_inode;
2815 struct osd_thread_info *info = osd_oti_get(env);
2819 LASSERT(handle != NULL);
2821 /* version set is not real XATTR */
2822 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2823 /* for version we are just using xattr API but change inode
2825 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2826 osd_object_version_set(env, dt, buf->lb_buf);
2827 return sizeof(dt_obj_version_t);
2830 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2833 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2834 if (fl & LU_XATTR_REPLACE)
2835 fs_flags |= XATTR_REPLACE;
2837 if (fl & LU_XATTR_CREATE)
2838 fs_flags |= XATTR_CREATE;
2840 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2845 * Concurrency: @dt is read locked.
2847 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2848 struct lu_buf *buf, struct lustre_capa *capa)
2850 struct osd_object *obj = osd_dt_obj(dt);
2851 struct inode *inode = obj->oo_inode;
2852 struct osd_thread_info *info = osd_oti_get(env);
2853 struct dentry *dentry = &info->oti_obj_dentry;
2855 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2856 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2857 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2859 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2862 dentry->d_inode = inode;
2863 dentry->d_sb = inode->i_sb;
2864 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2867 static int osd_declare_xattr_del(const struct lu_env *env,
2868 struct dt_object *dt, const char *name,
2869 struct thandle *handle)
2871 struct osd_thandle *oh;
2873 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2874 LASSERT(handle != NULL);
2876 oh = container_of0(handle, struct osd_thandle, ot_super);
2877 LASSERT(oh->ot_handle == NULL);
2879 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2880 osd_dto_credits_noquota[DTO_XATTR_SET]);
2886 * Concurrency: @dt is write locked.
2888 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2889 const char *name, struct thandle *handle,
2890 struct lustre_capa *capa)
2892 struct osd_object *obj = osd_dt_obj(dt);
2893 struct inode *inode = obj->oo_inode;
2894 struct osd_thread_info *info = osd_oti_get(env);
2895 struct dentry *dentry = &info->oti_obj_dentry;
2898 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2899 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2900 LASSERT(handle != NULL);
2902 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2905 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2907 ll_vfs_dq_init(inode);
2908 dentry->d_inode = inode;
2909 dentry->d_sb = inode->i_sb;
2910 rc = inode->i_op->removexattr(dentry, name);
2914 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2915 struct dt_object *dt,
2916 struct lustre_capa *old, __u64 opc)
2918 struct osd_thread_info *info = osd_oti_get(env);
2919 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2920 struct osd_object *obj = osd_dt_obj(dt);
2921 struct osd_device *osd = osd_obj2dev(obj);
2922 struct lustre_capa_key *key = &info->oti_capa_key;
2923 struct lustre_capa *capa = &info->oti_capa;
2924 struct obd_capa *oc;
2925 struct lu_capainfo *lci;
2929 if (!osd->od_fl_capa)
2930 RETURN(ERR_PTR(-ENOENT));
2932 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2933 LINVRNT(osd_invariant(obj));
2935 /* renewal sanity check */
2936 if (old && osd_object_auth(env, dt, old, opc))
2937 RETURN(ERR_PTR(-EACCES));
2939 lci = lu_capainfo_get(env);
2940 if (unlikely(lci == NULL))
2941 RETURN(ERR_PTR(-ENOENT));
2943 switch (lci->lci_auth) {
2947 capa->lc_uid = obj->oo_inode->i_uid;
2948 capa->lc_gid = obj->oo_inode->i_gid;
2949 capa->lc_flags = LC_ID_PLAIN;
2951 case LC_ID_CONVERT: {
2954 s[0] = obj->oo_inode->i_uid;
2955 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2956 s[2] = obj->oo_inode->i_gid;
2957 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2958 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2960 RETURN(ERR_PTR(rc));
2962 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2963 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2964 capa->lc_flags = LC_ID_CONVERT;
2968 RETURN(ERR_PTR(-EINVAL));
2971 capa->lc_fid = *fid;
2973 capa->lc_flags |= osd->od_capa_alg << 24;
2974 capa->lc_timeout = osd->od_capa_timeout;
2975 capa->lc_expiry = 0;
2977 oc = capa_lookup(osd->od_capa_hash, capa, 1);
2979 LASSERT(!capa_is_expired(oc));
2983 spin_lock(&capa_lock);
2984 *key = osd->od_capa_keys[1];
2985 spin_unlock(&capa_lock);
2987 capa->lc_keyid = key->lk_keyid;
2988 capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
2990 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2992 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2993 RETURN(ERR_PTR(rc));
2996 oc = capa_add(osd->od_capa_hash, capa);
3000 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
3002 struct osd_object *obj = osd_dt_obj(dt);
3003 struct inode *inode = obj->oo_inode;
3004 struct osd_thread_info *info = osd_oti_get(env);
3005 struct dentry *dentry = &info->oti_obj_dentry;
3006 struct file *file = &info->oti_file;
3011 dentry->d_inode = inode;
3012 dentry->d_sb = inode->i_sb;
3013 file->f_dentry = dentry;
3014 file->f_mapping = inode->i_mapping;
3015 file->f_op = inode->i_fop;
3016 set_file_inode(file, inode);
3017 #ifndef HAVE_FILE_FSYNC_4ARGS
3018 mutex_lock(&inode->i_mutex);
3020 rc = do_fsync(file, 0);
3021 #ifndef HAVE_FILE_FSYNC_4ARGS
3022 mutex_unlock(&inode->i_mutex);
3027 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
3030 struct osd_object *obj = osd_dt_obj(dt);
3033 *data = (void *)obj->oo_inode;
3041 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3042 const struct dt_index_features *feat)
3044 struct iam_descr *descr;
3046 if (osd_object_is_root(o))
3047 return feat == &dt_directory_features;
3049 LASSERT(o->oo_dir != NULL);
3051 descr = o->oo_dir->od_container.ic_descr;
3052 if (feat == &dt_directory_features) {
3053 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3059 feat->dif_keysize_min <= descr->id_key_size &&
3060 descr->id_key_size <= feat->dif_keysize_max &&
3061 feat->dif_recsize_min <= descr->id_rec_size &&
3062 descr->id_rec_size <= feat->dif_recsize_max &&
3063 !(feat->dif_flags & (DT_IND_VARKEY |
3064 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3065 ergo(feat->dif_flags & DT_IND_UPDATE,
3066 1 /* XXX check that object (and file system) is
3071 static int osd_iam_container_init(const struct lu_env *env,
3072 struct osd_object *obj,
3073 struct osd_directory *dir)
3075 struct iam_container *bag = &dir->od_container;
3078 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3082 result = iam_container_setup(bag);
3084 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3086 iam_container_fini(bag);
3093 * Concurrency: no external locking is necessary.
3095 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3096 const struct dt_index_features *feat)
3100 struct osd_object *obj = osd_dt_obj(dt);
3102 LINVRNT(osd_invariant(obj));
3104 if (osd_object_is_root(obj)) {
3105 dt->do_index_ops = &osd_index_ea_ops;
3107 } else if (feat == &dt_directory_features) {
3108 dt->do_index_ops = &osd_index_ea_ops;
3109 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3114 } else if (unlikely(feat == &dt_otable_features)) {
3115 dt->do_index_ops = &osd_otable_ops;
3117 } else if (unlikely(feat == &dt_acct_features)) {
3118 dt->do_index_ops = &osd_acct_index_ops;
3121 } else if (!osd_has_index(obj)) {
3122 struct osd_directory *dir;
3127 spin_lock(&obj->oo_guard);
3128 if (obj->oo_dir == NULL)
3132 * Concurrent thread allocated container data.
3135 spin_unlock(&obj->oo_guard);
3137 * Now, that we have container data, serialize its
3140 down_write(&obj->oo_ext_idx_sem);
3142 * recheck under lock.
3144 if (!osd_has_index(obj))
3145 result = osd_iam_container_init(env, obj, dir);
3148 up_write(&obj->oo_ext_idx_sem);
3156 if (result == 0 && skip_iam == 0) {
3157 if (!osd_iam_index_probe(env, obj, feat))
3160 LINVRNT(osd_invariant(obj));
3162 if (result == 0 && is_quota_glb_feat(feat) &&
3163 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3164 result = osd_quota_migration(env, dt, feat);
3169 static int osd_otable_it_attr_get(const struct lu_env *env,
3170 struct dt_object *dt,
3171 struct lu_attr *attr,
3172 struct lustre_capa *capa)
3178 static const struct dt_object_operations osd_obj_ops = {
3179 .do_read_lock = osd_object_read_lock,
3180 .do_write_lock = osd_object_write_lock,
3181 .do_read_unlock = osd_object_read_unlock,
3182 .do_write_unlock = osd_object_write_unlock,
3183 .do_write_locked = osd_object_write_locked,
3184 .do_attr_get = osd_attr_get,
3185 .do_declare_attr_set = osd_declare_attr_set,
3186 .do_attr_set = osd_attr_set,
3187 .do_ah_init = osd_ah_init,
3188 .do_declare_create = osd_declare_object_create,
3189 .do_create = osd_object_create,
3190 .do_declare_destroy = osd_declare_object_destroy,
3191 .do_destroy = osd_object_destroy,
3192 .do_index_try = osd_index_try,
3193 .do_declare_ref_add = osd_declare_object_ref_add,
3194 .do_ref_add = osd_object_ref_add,
3195 .do_declare_ref_del = osd_declare_object_ref_del,
3196 .do_ref_del = osd_object_ref_del,
3197 .do_xattr_get = osd_xattr_get,
3198 .do_declare_xattr_set = osd_declare_xattr_set,
3199 .do_xattr_set = osd_xattr_set,
3200 .do_declare_xattr_del = osd_declare_xattr_del,
3201 .do_xattr_del = osd_xattr_del,
3202 .do_xattr_list = osd_xattr_list,
3203 .do_capa_get = osd_capa_get,
3204 .do_object_sync = osd_object_sync,
3205 .do_data_get = osd_data_get,
3209 * dt_object_operations for interoperability mode
3210 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3212 static const struct dt_object_operations osd_obj_ea_ops = {
3213 .do_read_lock = osd_object_read_lock,
3214 .do_write_lock = osd_object_write_lock,
3215 .do_read_unlock = osd_object_read_unlock,
3216 .do_write_unlock = osd_object_write_unlock,
3217 .do_write_locked = osd_object_write_locked,
3218 .do_attr_get = osd_attr_get,
3219 .do_declare_attr_set = osd_declare_attr_set,
3220 .do_attr_set = osd_attr_set,
3221 .do_ah_init = osd_ah_init,
3222 .do_declare_create = osd_declare_object_create,
3223 .do_create = osd_object_ea_create,
3224 .do_declare_destroy = osd_declare_object_destroy,
3225 .do_destroy = osd_object_destroy,
3226 .do_index_try = osd_index_try,
3227 .do_declare_ref_add = osd_declare_object_ref_add,
3228 .do_ref_add = osd_object_ref_add,
3229 .do_declare_ref_del = osd_declare_object_ref_del,
3230 .do_ref_del = osd_object_ref_del,
3231 .do_xattr_get = osd_xattr_get,
3232 .do_declare_xattr_set = osd_declare_xattr_set,
3233 .do_xattr_set = osd_xattr_set,
3234 .do_declare_xattr_del = osd_declare_xattr_del,
3235 .do_xattr_del = osd_xattr_del,
3236 .do_xattr_list = osd_xattr_list,
3237 .do_capa_get = osd_capa_get,
3238 .do_object_sync = osd_object_sync,
3239 .do_data_get = osd_data_get,
3242 static const struct dt_object_operations osd_obj_otable_it_ops = {
3243 .do_attr_get = osd_otable_it_attr_get,
3244 .do_index_try = osd_index_try,
3247 static int osd_index_declare_iam_delete(const struct lu_env *env,
3248 struct dt_object *dt,
3249 const struct dt_key *key,
3250 struct thandle *handle)
3252 struct osd_thandle *oh;
3254 oh = container_of0(handle, struct osd_thandle, ot_super);
3255 LASSERT(oh->ot_handle == NULL);
3257 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3258 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3264 * delete a (key, value) pair from index \a dt specified by \a key
3266 * \param dt osd index object
3267 * \param key key for index
3268 * \param rec record reference
3269 * \param handle transaction handler
3272 * \retval -ve failure
3275 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3276 const struct dt_key *key,
3277 struct thandle *handle,
3278 struct lustre_capa *capa)
3280 struct osd_thread_info *oti = osd_oti_get(env);
3281 struct osd_object *obj = osd_dt_obj(dt);
3282 struct osd_thandle *oh;
3283 struct iam_path_descr *ipd;
3284 struct iam_container *bag = &obj->oo_dir->od_container;
3289 LINVRNT(osd_invariant(obj));
3290 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3291 LASSERT(bag->ic_object == obj->oo_inode);
3292 LASSERT(handle != NULL);
3294 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3297 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3299 ipd = osd_idx_ipd_get(env, bag);
3300 if (unlikely(ipd == NULL))
3303 oh = container_of0(handle, struct osd_thandle, ot_super);
3304 LASSERT(oh->ot_handle != NULL);
3305 LASSERT(oh->ot_handle->h_transaction != NULL);
3307 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3308 /* swab quota uid/gid provided by caller */
3309 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3310 key = (const struct dt_key *)&oti->oti_quota_id;
3313 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3314 osd_ipd_put(env, bag, ipd);
3315 LINVRNT(osd_invariant(obj));
3319 static int osd_index_declare_ea_delete(const struct lu_env *env,
3320 struct dt_object *dt,
3321 const struct dt_key *key,
3322 struct thandle *handle)
3324 struct osd_thandle *oh;
3325 struct inode *inode;
3329 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3330 LASSERT(handle != NULL);
3332 oh = container_of0(handle, struct osd_thandle, ot_super);
3333 LASSERT(oh->ot_handle == NULL);
3335 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3336 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3338 inode = osd_dt_obj(dt)->oo_inode;
3341 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3342 true, true, NULL, false);
3346 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3349 struct osd_fid_pack *rec;
3352 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3353 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3354 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3359 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3364 /* FID seqs not in FLDB, must be local seq */
3365 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3368 if (osd_seq_exists(env, osd, fid_seq(fid)))
3375 * Index delete function for interoperability mode (b11826).
3376 * It will remove the directory entry added by osd_index_ea_insert().
3377 * This entry is needed to maintain name->fid mapping.
3379 * \param key, key i.e. file entry to be deleted
3381 * \retval 0, on success
3382 * \retval -ve, on error
3384 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3385 const struct dt_key *key,
3386 struct thandle *handle,
3387 struct lustre_capa *capa)
3389 struct osd_object *obj = osd_dt_obj(dt);
3390 struct inode *dir = obj->oo_inode;
3391 struct dentry *dentry;
3392 struct osd_thandle *oh;
3393 struct ldiskfs_dir_entry_2 *de = NULL;
3394 struct buffer_head *bh;
3395 struct htree_lock *hlock = NULL;
3396 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3397 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3401 LINVRNT(osd_invariant(obj));
3402 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3403 LASSERT(handle != NULL);
3405 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3407 oh = container_of(handle, struct osd_thandle, ot_super);
3408 LASSERT(oh->ot_handle != NULL);
3409 LASSERT(oh->ot_handle->h_transaction != NULL);
3411 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3414 ll_vfs_dq_init(dir);
3415 dentry = osd_child_dentry_get(env, obj,
3416 (char *)key, strlen((char *)key));
3418 if (obj->oo_hl_head != NULL) {
3419 hlock = osd_oti_get(env)->oti_hlock;
3420 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3421 dir, LDISKFS_HLOCK_DEL);
3423 down_write(&obj->oo_ext_idx_sem);
3426 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3430 /* If this is not the ".." entry, it might be a remote DNE
3431 * entry and we need to check if the FID is for a remote
3432 * MDT. If the FID is not in the directory entry (e.g.
3433 * upgraded 1.8 filesystem without dirdata enabled) then
3434 * we need to get the FID from the LMA. For a remote directory
3435 * there HAS to be an LMA, it cannot be an IGIF inode in this
3438 * Delete the entry before the agent inode in order to
3439 * simplify error handling. At worst an error after deleting
3440 * the entry first might leak the agent inode afterward. The
3441 * reverse would need filesystem abort in case of error deleting
3442 * the entry after the agent had been removed, or leave a
3443 * dangling entry pointing at a random inode. */
3444 if (strcmp((char *)key, dotdot) != 0) {
3445 LASSERT(de != NULL);
3446 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3447 /* If Fid is not in dentry, try to get it from LMA */
3448 if (rc == -ENODATA) {
3449 struct osd_inode_id *id;
3450 struct inode *inode;
3452 /* Before trying to get fid from the inode,
3453 * check whether the inode is valid.
3455 * If the inode has been deleted, do not go
3456 * ahead to do osd_ea_fid_get, which will set
3457 * the inode to bad inode, which might cause
3458 * the inode to be deleted uncorrectly */
3459 inode = ldiskfs_iget(osd_sb(osd),
3460 le32_to_cpu(de->inode));
3461 if (IS_ERR(inode)) {
3462 CDEBUG(D_INODE, "%s: "DFID"get inode"
3463 "error.\n", osd_name(osd),
3465 rc = PTR_ERR(inode);
3467 if (likely(inode->i_nlink != 0)) {
3468 id = &osd_oti_get(env)->oti_id;
3469 rc = osd_ea_fid_get(env, obj,
3470 le32_to_cpu(de->inode),
3473 CDEBUG(D_INFO, "%s: %u "DFID
3476 le32_to_cpu(de->inode),
3484 unlikely(osd_remote_fid(env, osd, fid)))
3485 /* Need to delete agent inode */
3486 ino = le32_to_cpu(de->inode);
3488 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3490 if (rc == 0 && unlikely(ino != 0)) {
3491 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3494 CERROR("%s: del local inode "DFID": rc = %d\n",
3495 osd_name(osd), PFID(fid), rc);
3501 ldiskfs_htree_unlock(hlock);
3503 up_write(&obj->oo_ext_idx_sem);
3508 /* For inode on the remote MDT, .. will point to
3509 * /Agent directory, Check whether it needs to delete
3510 * from agent directory */
3511 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3512 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3514 if (rc != 0 && rc != -ENOENT) {
3515 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3516 osd_name(osd), PFID(fid), rc);
3526 LASSERT(osd_invariant(obj));
3531 * Lookup index for \a key and copy record to \a rec.
3533 * \param dt osd index object
3534 * \param key key for index
3535 * \param rec record reference
3537 * \retval +ve success : exact mach
3538 * \retval 0 return record with key not greater than \a key
3539 * \retval -ve failure
3541 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3542 struct dt_rec *rec, const struct dt_key *key,
3543 struct lustre_capa *capa)
3545 struct osd_object *obj = osd_dt_obj(dt);
3546 struct iam_path_descr *ipd;
3547 struct iam_container *bag = &obj->oo_dir->od_container;
3548 struct osd_thread_info *oti = osd_oti_get(env);
3549 struct iam_iterator *it = &oti->oti_idx_it;
3550 struct iam_rec *iam_rec;
3555 LASSERT(osd_invariant(obj));
3556 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3557 LASSERT(bag->ic_object == obj->oo_inode);
3559 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3562 ipd = osd_idx_ipd_get(env, bag);
3566 /* got ipd now we can start iterator. */
3567 iam_it_init(it, bag, 0, ipd);
3569 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3570 /* swab quota uid/gid provided by caller */
3571 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3572 key = (const struct dt_key *)&oti->oti_quota_id;
3575 rc = iam_it_get(it, (struct iam_key *)key);
3577 if (S_ISDIR(obj->oo_inode->i_mode))
3578 iam_rec = (struct iam_rec *)oti->oti_ldp;
3580 iam_rec = (struct iam_rec *) rec;
3582 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3584 if (S_ISDIR(obj->oo_inode->i_mode))
3585 osd_fid_unpack((struct lu_fid *) rec,
3586 (struct osd_fid_pack *)iam_rec);
3587 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3588 osd_quota_unpack(obj, rec);
3593 osd_ipd_put(env, bag, ipd);
3595 LINVRNT(osd_invariant(obj));
3600 static int osd_index_declare_iam_insert(const struct lu_env *env,
3601 struct dt_object *dt,
3602 const struct dt_rec *rec,
3603 const struct dt_key *key,
3604 struct thandle *handle)
3606 struct osd_thandle *oh;
3608 LASSERT(handle != NULL);
3610 oh = container_of0(handle, struct osd_thandle, ot_super);
3611 LASSERT(oh->ot_handle == NULL);
3613 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3614 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3620 * Inserts (key, value) pair in \a dt index object.
3622 * \param dt osd index object
3623 * \param key key for index
3624 * \param rec record reference
3625 * \param th transaction handler
3628 * \retval -ve failure
3630 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3631 const struct dt_rec *rec,
3632 const struct dt_key *key, struct thandle *th,
3633 struct lustre_capa *capa, int ignore_quota)
3635 struct osd_object *obj = osd_dt_obj(dt);
3636 struct iam_path_descr *ipd;
3637 struct osd_thandle *oh;
3638 struct iam_container *bag = &obj->oo_dir->od_container;
3639 struct osd_thread_info *oti = osd_oti_get(env);
3640 struct iam_rec *iam_rec;
3645 LINVRNT(osd_invariant(obj));
3646 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3647 LASSERT(bag->ic_object == obj->oo_inode);
3648 LASSERT(th != NULL);
3650 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3653 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3655 ipd = osd_idx_ipd_get(env, bag);
3656 if (unlikely(ipd == NULL))
3659 oh = container_of0(th, struct osd_thandle, ot_super);
3660 LASSERT(oh->ot_handle != NULL);
3661 LASSERT(oh->ot_handle->h_transaction != NULL);
3662 if (S_ISDIR(obj->oo_inode->i_mode)) {
3663 iam_rec = (struct iam_rec *)oti->oti_ldp;
3664 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3665 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3666 /* pack quota uid/gid */
3667 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3668 key = (const struct dt_key *)&oti->oti_quota_id;
3669 /* pack quota record */
3670 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3671 iam_rec = (struct iam_rec *)rec;
3673 iam_rec = (struct iam_rec *)rec;
3676 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3678 osd_ipd_put(env, bag, ipd);
3679 LINVRNT(osd_invariant(obj));
3684 * Calls ldiskfs_add_entry() to add directory entry
3685 * into the directory. This is required for
3686 * interoperability mode (b11826)
3688 * \retval 0, on success
3689 * \retval -ve, on error
3691 static int __osd_ea_add_rec(struct osd_thread_info *info,
3692 struct osd_object *pobj, struct inode *cinode,
3693 const char *name, const struct dt_rec *fid,
3694 struct htree_lock *hlock, struct thandle *th)
3696 struct ldiskfs_dentry_param *ldp;
3697 struct dentry *child;
3698 struct osd_thandle *oth;
3701 oth = container_of(th, struct osd_thandle, ot_super);
3702 LASSERT(oth->ot_handle != NULL);
3703 LASSERT(oth->ot_handle->h_transaction != NULL);
3704 LASSERT(pobj->oo_inode);
3706 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3707 if (unlikely(pobj->oo_inode ==
3708 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3711 osd_get_ldiskfs_dirent_param(ldp, fid);
3712 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3713 child->d_fsdata = (void *)ldp;
3714 ll_vfs_dq_init(pobj->oo_inode);
3715 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3721 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3722 * into the directory.Also sets flags into osd object to
3723 * indicate dot and dotdot are created. This is required for
3724 * interoperability mode (b11826)
3726 * \param dir directory for dot and dotdot fixup.
3727 * \param obj child object for linking
3729 * \retval 0, on success
3730 * \retval -ve, on error
3732 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3733 struct osd_object *dir,
3734 struct inode *parent_dir, const char *name,
3735 const struct dt_rec *dot_fid,
3736 const struct dt_rec *dot_dot_fid,
3739 struct inode *inode = dir->oo_inode;
3740 struct osd_thandle *oth;
3743 oth = container_of(th, struct osd_thandle, ot_super);
3744 LASSERT(oth->ot_handle->h_transaction != NULL);
3745 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3747 if (strcmp(name, dot) == 0) {
3748 if (dir->oo_compat_dot_created) {
3751 LASSERT(inode == parent_dir);
3752 dir->oo_compat_dot_created = 1;
3755 } else if (strcmp(name, dotdot) == 0) {
3756 if (!dir->oo_compat_dot_created)
3758 /* in case of rename, dotdot is already created */
3759 if (dir->oo_compat_dotdot_created) {
3760 return __osd_ea_add_rec(info, dir, parent_dir, name,
3761 dot_dot_fid, NULL, th);
3764 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3765 parent_dir, dot_fid,
3768 dir->oo_compat_dotdot_created = 1;
3776 * It will call the appropriate osd_add* function and return the
3777 * value, return by respective functions.
3779 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3780 struct inode *cinode, const char *name,
3781 const struct dt_rec *fid, struct thandle *th)
3783 struct osd_thread_info *info = osd_oti_get(env);
3784 struct htree_lock *hlock;
3787 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3789 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3791 if (hlock != NULL) {
3792 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3795 down_write(&pobj->oo_ext_idx_sem);
3797 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3798 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3801 if (hlock != NULL) {
3802 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3803 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3805 down_write(&pobj->oo_ext_idx_sem);
3808 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3809 struct lu_fid *tfid = &info->oti_fid;
3811 *tfid = *(const struct lu_fid *)fid;
3813 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3814 (const struct dt_rec *)tfid,
3817 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3822 ldiskfs_htree_unlock(hlock);
3824 up_write(&pobj->oo_ext_idx_sem);
3830 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3831 struct osd_idmap_cache *oic)
3833 struct osd_scrub *scrub = &dev->od_scrub;
3834 struct lu_fid *fid = &oic->oic_fid;
3835 struct osd_inode_id *id = &oti->oti_id;
3840 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3844 rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3845 if (rc != 0 && rc != -ENOENT)
3848 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3851 if (thread_is_running(&scrub->os_thread)) {
3852 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3853 /* There is race condition between osd_oi_lookup and OI scrub.
3854 * The OI scrub finished just after osd_oi_lookup() failure.
3855 * Under such case, it is unnecessary to trigger OI scrub again,
3856 * but try to call osd_oi_lookup() again. */
3857 if (unlikely(rc == -EAGAIN))
3863 if (!dev->od_noscrub && ++once == 1) {
3864 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3866 rc = osd_scrub_start(dev);
3867 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3869 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3878 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3879 struct osd_device *dev,
3880 struct osd_idmap_cache *oic,
3881 struct lu_fid *fid, __u32 ino)
3883 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3884 struct inode *inode;
3887 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3888 inode = osd_iget(oti, dev, &oic->oic_lid);
3889 if (IS_ERR(inode)) {
3890 fid_zero(&oic->oic_fid);
3891 return PTR_ERR(inode);
3894 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3897 fid_zero(&oic->oic_fid);
3899 *fid = oic->oic_fid = lma->lma_self_fid;
3903 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3904 struct osd_inode_id *id, const struct lu_fid *fid)
3906 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3907 id->oii_ino, id->oii_gen, info);
3908 info->oti_cache.oic_lid = *id;
3909 info->oti_cache.oic_fid = *fid;
3910 info->oti_cache.oic_dev = osd;
3916 * Calls ->lookup() to find dentry. From dentry get inode and
3917 * read inode's ea to get fid. This is required for interoperability
3920 * \retval 0, on success
3921 * \retval -ve, on error
3923 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3924 struct dt_rec *rec, const struct dt_key *key)
3926 struct inode *dir = obj->oo_inode;
3927 struct dentry *dentry;
3928 struct ldiskfs_dir_entry_2 *de;
3929 struct buffer_head *bh;
3930 struct lu_fid *fid = (struct lu_fid *) rec;
3931 struct htree_lock *hlock = NULL;
3936 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3938 dentry = osd_child_dentry_get(env, obj,
3939 (char *)key, strlen((char *)key));
3941 if (obj->oo_hl_head != NULL) {
3942 hlock = osd_oti_get(env)->oti_hlock;
3943 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3944 dir, LDISKFS_HLOCK_LOOKUP);
3946 down_read(&obj->oo_ext_idx_sem);
3949 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3951 struct osd_thread_info *oti = osd_oti_get(env);
3952 struct osd_inode_id *id = &oti->oti_id;
3953 struct osd_idmap_cache *oic = &oti->oti_cache;
3954 struct osd_device *dev = osd_obj2dev(obj);
3955 struct osd_scrub *scrub = &dev->od_scrub;
3956 struct scrub_file *sf = &scrub->os_file;
3958 ino = le32_to_cpu(de->inode);
3959 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3961 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3965 rc = osd_get_fid_from_dentry(de, rec);
3967 /* done with de, release bh */
3970 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3972 osd_id_gen(id, ino, OSD_OII_NOGEN);
3974 fid_zero(&oic->oic_fid);
3978 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3982 if ((scrub->os_pos_current <= ino) &&
3983 ((sf->sf_flags & SF_INCONSISTENT) ||
3984 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3985 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3987 osd_consistency_check(oti, dev, oic);
3996 ldiskfs_htree_unlock(hlock);
3998 up_read(&obj->oo_ext_idx_sem);
4003 * Find the osd object for given fid.
4005 * \param fid need to find the osd object having this fid
4007 * \retval osd_object on success
4008 * \retval -ve on error
4010 struct osd_object *osd_object_find(const struct lu_env *env,
4011 struct dt_object *dt,
4012 const struct lu_fid *fid)
4014 struct lu_device *ludev = dt->do_lu.lo_dev;
4015 struct osd_object *child = NULL;
4016 struct lu_object *luch;
4017 struct lu_object *lo;
4020 * at this point topdev might not exist yet
4021 * (i.e. MGS is preparing profiles). so we can
4022 * not rely on topdev and instead lookup with
4023 * our device passed as topdev. this can't work
4024 * if the object isn't cached yet (as osd doesn't
4025 * allocate lu_header). IOW, the object must be
4026 * in the cache, otherwise lu_object_alloc() crashes
4029 luch = lu_object_find_at(env, ludev, fid, NULL);
4030 if (!IS_ERR(luch)) {
4031 if (lu_object_exists(luch)) {
4032 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4034 child = osd_obj(lo);
4036 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4037 "lu_object can't be located"
4038 DFID"\n", PFID(fid));
4040 if (child == NULL) {
4041 lu_object_put(env, luch);
4042 CERROR("Unable to get osd_object\n");
4043 child = ERR_PTR(-ENOENT);
4046 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4047 "lu_object does not exists "DFID"\n",
4049 lu_object_put(env, luch);
4050 child = ERR_PTR(-ENOENT);
4053 child = ERR_CAST(luch);
4060 * Put the osd object once done with it.
4062 * \param obj osd object that needs to be put
4064 static inline void osd_object_put(const struct lu_env *env,
4065 struct osd_object *obj)
4067 lu_object_put(env, &obj->oo_dt.do_lu);
4070 static int osd_index_declare_ea_insert(const struct lu_env *env,
4071 struct dt_object *dt,
4072 const struct dt_rec *rec,
4073 const struct dt_key *key,
4074 struct thandle *handle)
4076 struct osd_thandle *oh;
4077 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4078 struct lu_fid *fid = (struct lu_fid *)rec;
4082 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4083 LASSERT(handle != NULL);
4085 oh = container_of0(handle, struct osd_thandle, ot_super);
4086 LASSERT(oh->ot_handle == NULL);
4088 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4089 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4091 if (osd_dt_obj(dt)->oo_inode == NULL) {
4092 const char *name = (const char *)key;
4093 /* Object is not being created yet. Only happens when
4094 * 1. declare directory create
4095 * 2. declare insert .
4096 * 3. declare insert ..
4098 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4100 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4102 /* We ignore block quota on meta pool (MDTs), so needn't
4103 * calculate how many blocks will be consumed by this index
4105 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4106 oh, true, true, NULL, false);
4112 rc = osd_remote_fid(env, osd, fid);
4118 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4119 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4120 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4121 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4122 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4123 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4129 * Index add function for interoperability mode (b11826).
4130 * It will add the directory entry.This entry is needed to
4131 * maintain name->fid mapping.
4133 * \param key it is key i.e. file entry to be inserted
4134 * \param rec it is value of given key i.e. fid
4136 * \retval 0, on success
4137 * \retval -ve, on error
4139 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4140 const struct dt_rec *rec,
4141 const struct dt_key *key, struct thandle *th,
4142 struct lustre_capa *capa, int ignore_quota)
4144 struct osd_object *obj = osd_dt_obj(dt);
4145 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4146 struct lu_fid *fid = (struct lu_fid *) rec;
4147 const char *name = (const char *)key;
4148 struct osd_thread_info *oti = osd_oti_get(env);
4149 struct osd_inode_id *id = &oti->oti_id;
4150 struct inode *child_inode = NULL;
4151 struct osd_object *child = NULL;
4155 LASSERT(osd_invariant(obj));
4156 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4157 LASSERT(th != NULL);
4159 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4161 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4164 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4166 rc = osd_remote_fid(env, osd, fid);
4168 CERROR("%s: Can not find object "DFID" rc %d\n",
4169 osd_name(osd), PFID(fid), rc);
4174 /* Insert remote entry */
4175 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4176 struct osd_mdobj_map *omm = osd->od_mdt_map;
4177 struct osd_thandle *oh;
4179 /* If parent on remote MDT, we need put this object
4181 oh = container_of(th, typeof(*oh), ot_super);
4182 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4184 CERROR("%s: add "DFID" error: rc = %d\n",
4186 PFID(lu_object_fid(&dt->do_lu)), rc);
4190 child_inode = igrab(omm->omm_remote_parent->d_inode);
4192 child_inode = osd_create_local_agent_inode(env, osd,
4195 if (IS_ERR(child_inode))
4196 RETURN(PTR_ERR(child_inode));
4199 /* Insert local entry */
4200 child = osd_object_find(env, dt, fid);
4201 if (IS_ERR(child)) {
4202 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4203 osd_name(osd), PFID(fid),
4204 id->oii_ino, id->oii_gen,
4205 (int)PTR_ERR(child));
4206 RETURN(PTR_ERR(child));
4208 child_inode = igrab(child->oo_inode);
4211 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4215 osd_object_put(env, child);
4216 LASSERT(osd_invariant(obj));
4221 * Initialize osd Iterator for given osd index object.
4223 * \param dt osd index object
4226 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4227 struct dt_object *dt,
4229 struct lustre_capa *capa)
4231 struct osd_it_iam *it;
4232 struct osd_thread_info *oti = osd_oti_get(env);
4233 struct osd_object *obj = osd_dt_obj(dt);
4234 struct lu_object *lo = &dt->do_lu;
4235 struct iam_path_descr *ipd;
4236 struct iam_container *bag = &obj->oo_dir->od_container;
4238 LASSERT(lu_object_exists(lo));
4240 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4241 return ERR_PTR(-EACCES);
4244 ipd = osd_it_ipd_get(env, bag);
4245 if (likely(ipd != NULL)) {
4249 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4250 return (struct dt_it *)it;
4252 return ERR_PTR(-ENOMEM);
4256 * free given Iterator.
4259 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4261 struct osd_it_iam *it = (struct osd_it_iam *)di;
4262 struct osd_object *obj = it->oi_obj;
4264 iam_it_fini(&it->oi_it);
4265 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4266 lu_object_put(env, &obj->oo_dt.do_lu);
4270 * Move Iterator to record specified by \a key
4272 * \param di osd iterator
4273 * \param key key for index
4275 * \retval +ve di points to record with least key not larger than key
4276 * \retval 0 di points to exact matched key
4277 * \retval -ve failure
4280 static int osd_it_iam_get(const struct lu_env *env,
4281 struct dt_it *di, const struct dt_key *key)
4283 struct osd_thread_info *oti = osd_oti_get(env);
4284 struct osd_it_iam *it = (struct osd_it_iam *)di;
4286 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4287 /* swab quota uid/gid */
4288 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4289 key = (struct dt_key *)&oti->oti_quota_id;
4292 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4298 * \param di osd iterator
4300 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4302 struct osd_it_iam *it = (struct osd_it_iam *)di;
4304 iam_it_put(&it->oi_it);
4308 * Move iterator by one record
4310 * \param di osd iterator
4312 * \retval +1 end of container reached
4314 * \retval -ve failure
4317 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4319 struct osd_it_iam *it = (struct osd_it_iam *)di;
4321 return iam_it_next(&it->oi_it);
4325 * Return pointer to the key under iterator.
4328 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4329 const struct dt_it *di)
4331 struct osd_thread_info *oti = osd_oti_get(env);
4332 struct osd_it_iam *it = (struct osd_it_iam *)di;
4333 struct osd_object *obj = it->oi_obj;
4336 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4338 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4339 /* swab quota uid/gid */
4340 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4341 key = (struct dt_key *)&oti->oti_quota_id;
4348 * Return size of key under iterator (in bytes)
4351 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4353 struct osd_it_iam *it = (struct osd_it_iam *)di;
4355 return iam_it_key_size(&it->oi_it);
4359 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4361 /* check if file type is required */
4362 if (ent->lde_attrs & LUDA_TYPE) {
4363 struct luda_type *lt;
4364 int align = sizeof(*lt) - 1;
4366 len = (len + align) & ~align;
4367 lt = (struct luda_type *)(ent->lde_name + len);
4368 lt->lt_type = cpu_to_le16(DTTOIF(type));
4371 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4375 * build lu direct from backend fs dirent.
4379 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4380 char *name, __u16 namelen, __u16 type, __u32 attr)
4382 ent->lde_attrs = attr | LUDA_FID;
4383 fid_cpu_to_le(&ent->lde_fid, fid);
4385 ent->lde_hash = cpu_to_le64(offset);
4386 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4388 strncpy(ent->lde_name, name, namelen);
4389 ent->lde_name[namelen] = '\0';
4390 ent->lde_namelen = cpu_to_le16(namelen);
4392 /* append lustre attributes */
4393 osd_it_append_attrs(ent, namelen, type);
4397 * Return pointer to the record under iterator.
4399 static int osd_it_iam_rec(const struct lu_env *env,
4400 const struct dt_it *di,
4401 struct dt_rec *dtrec, __u32 attr)
4403 struct osd_it_iam *it = (struct osd_it_iam *)di;
4404 struct osd_thread_info *info = osd_oti_get(env);
4407 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4408 const struct osd_fid_pack *rec;
4409 struct lu_fid *fid = &info->oti_fid;
4410 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4416 name = (char *)iam_it_key_get(&it->oi_it);
4418 RETURN(PTR_ERR(name));
4420 namelen = iam_it_key_size(&it->oi_it);
4422 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4424 RETURN(PTR_ERR(rec));
4426 rc = osd_fid_unpack(fid, rec);
4430 hash = iam_it_store(&it->oi_it);
4432 /* IAM does not store object type in IAM index (dir) */
4433 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4435 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4436 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4437 (struct iam_rec *)dtrec);
4438 osd_quota_unpack(it->oi_obj, dtrec);
4440 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4441 (struct iam_rec *)dtrec);
4448 * Returns cookie for current Iterator position.
4450 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4452 struct osd_it_iam *it = (struct osd_it_iam *)di;
4454 return iam_it_store(&it->oi_it);
4458 * Restore iterator from cookie.
4460 * \param di osd iterator
4461 * \param hash Iterator location cookie
4463 * \retval +ve di points to record with least key not larger than key.
4464 * \retval 0 di points to exact matched key
4465 * \retval -ve failure
4468 static int osd_it_iam_load(const struct lu_env *env,
4469 const struct dt_it *di, __u64 hash)
4471 struct osd_it_iam *it = (struct osd_it_iam *)di;
4473 return iam_it_load(&it->oi_it, hash);
4476 static const struct dt_index_operations osd_index_iam_ops = {
4477 .dio_lookup = osd_index_iam_lookup,
4478 .dio_declare_insert = osd_index_declare_iam_insert,
4479 .dio_insert = osd_index_iam_insert,
4480 .dio_declare_delete = osd_index_declare_iam_delete,
4481 .dio_delete = osd_index_iam_delete,
4483 .init = osd_it_iam_init,
4484 .fini = osd_it_iam_fini,
4485 .get = osd_it_iam_get,
4486 .put = osd_it_iam_put,
4487 .next = osd_it_iam_next,
4488 .key = osd_it_iam_key,
4489 .key_size = osd_it_iam_key_size,
4490 .rec = osd_it_iam_rec,
4491 .store = osd_it_iam_store,
4492 .load = osd_it_iam_load
4498 * Creates or initializes iterator context.
4500 * \retval struct osd_it_ea, iterator structure on success
4503 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4504 struct dt_object *dt,
4506 struct lustre_capa *capa)
4508 struct osd_object *obj = osd_dt_obj(dt);
4509 struct osd_thread_info *info = osd_oti_get(env);
4510 struct osd_it_ea *it = &info->oti_it_ea;
4511 struct file *file = &it->oie_file;
4512 struct lu_object *lo = &dt->do_lu;
4513 struct dentry *obj_dentry = &info->oti_it_dentry;
4515 LASSERT(lu_object_exists(lo));
4517 obj_dentry->d_inode = obj->oo_inode;
4518 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4519 obj_dentry->d_name.hash = 0;
4521 it->oie_rd_dirent = 0;
4522 it->oie_it_dirent = 0;
4523 it->oie_dirent = NULL;
4524 it->oie_buf = info->oti_it_ea_buf;
4527 /* Reset the "file" totally to avoid to reuse any old value from
4528 * former readdir handling, the "file->f_pos" should be zero. */
4529 memset(file, 0, sizeof(*file));
4530 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4531 if (attr & LUDA_64BITHASH)
4532 file->f_mode = FMODE_64BITHASH;
4534 file->f_mode = FMODE_32BITHASH;
4535 file->f_dentry = obj_dentry;
4536 file->f_mapping = obj->oo_inode->i_mapping;
4537 file->f_op = obj->oo_inode->i_fop;
4538 set_file_inode(file, obj->oo_inode);
4541 RETURN((struct dt_it *) it);
4545 * Destroy or finishes iterator context.
4547 * \param di iterator structure to be destroyed
4549 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4551 struct osd_it_ea *it = (struct osd_it_ea *)di;
4552 struct osd_object *obj = it->oie_obj;
4553 struct inode *inode = obj->oo_inode;
4556 it->oie_file.f_op->release(inode, &it->oie_file);
4557 lu_object_put(env, &obj->oo_dt.do_lu);
4562 * It position the iterator at given key, so that next lookup continues from
4563 * that key Or it is similar to dio_it->load() but based on a key,
4564 * rather than file position.
4566 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4569 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4571 static int osd_it_ea_get(const struct lu_env *env,
4572 struct dt_it *di, const struct dt_key *key)
4574 struct osd_it_ea *it = (struct osd_it_ea *)di;
4577 LASSERT(((const char *)key)[0] == '\0');
4578 it->oie_file.f_pos = 0;
4579 it->oie_rd_dirent = 0;
4580 it->oie_it_dirent = 0;
4581 it->oie_dirent = NULL;
4589 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4594 * It is called internally by ->readdir(). It fills the
4595 * iterator's in-memory data structure with required
4596 * information i.e. name, namelen, rec_size etc.
4598 * \param buf in which information to be filled in.
4599 * \param name name of the file in given dir
4601 * \retval 0 on success
4602 * \retval 1 on buffer full
4604 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4605 loff_t offset, __u64 ino,
4608 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4609 struct osd_object *obj = it->oie_obj;
4610 struct osd_it_ea_dirent *ent = it->oie_dirent;
4611 struct lu_fid *fid = &ent->oied_fid;
4612 struct osd_fid_pack *rec;
4615 /* this should never happen */
4616 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4617 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4621 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4625 /* "." is just the object itself. */
4626 if (namelen == 1 && name[0] == '.') {
4627 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4628 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4629 rec = (struct osd_fid_pack*) (name + namelen + 1);
4630 if (osd_fid_unpack(fid, rec) != 0)
4635 d_type &= ~LDISKFS_DIRENT_LUFID;
4637 /* NOT export local root. */
4638 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4639 ino = obj->oo_inode->i_ino;
4640 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4643 ent->oied_ino = ino;
4644 ent->oied_off = offset;
4645 ent->oied_namelen = namelen;
4646 ent->oied_type = d_type;
4648 memcpy(ent->oied_name, name, namelen);
4650 it->oie_rd_dirent++;
4651 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4656 * Calls ->readdir() to load a directory entry at a time
4657 * and stored it in iterator's in-memory data structure.
4659 * \param di iterator's in memory structure
4661 * \retval 0 on success
4662 * \retval -ve on error
4664 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4665 const struct dt_it *di)
4667 struct osd_it_ea *it = (struct osd_it_ea *)di;
4668 struct osd_object *obj = it->oie_obj;
4669 struct inode *inode = obj->oo_inode;
4670 struct htree_lock *hlock = NULL;
4674 it->oie_dirent = it->oie_buf;
4675 it->oie_rd_dirent = 0;
4677 if (obj->oo_hl_head != NULL) {
4678 hlock = osd_oti_get(env)->oti_hlock;
4679 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4680 inode, LDISKFS_HLOCK_READDIR);
4682 down_read(&obj->oo_ext_idx_sem);
4685 result = inode->i_fop->readdir(&it->oie_file, it,
4686 (filldir_t) osd_ldiskfs_filldir);
4689 ldiskfs_htree_unlock(hlock);
4691 up_read(&obj->oo_ext_idx_sem);
4693 if (it->oie_rd_dirent == 0) {
4696 it->oie_dirent = it->oie_buf;
4697 it->oie_it_dirent = 1;
4704 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4705 * to load a directory entry at a time and stored it in
4706 * iterator's in-memory data structure.
4708 * \param di iterator's in memory structure
4710 * \retval +ve iterator reached to end
4711 * \retval 0 iterator not reached to end
4712 * \retval -ve on error
4714 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4716 struct osd_it_ea *it = (struct osd_it_ea *)di;
4721 if (it->oie_it_dirent < it->oie_rd_dirent) {
4723 (void *) it->oie_dirent +
4724 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4725 it->oie_dirent->oied_namelen);
4726 it->oie_it_dirent++;
4729 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4732 rc = osd_ldiskfs_it_fill(env, di);
4739 * Returns the key at current position from iterator's in memory structure.
4741 * \param di iterator's in memory structure
4743 * \retval key i.e. struct dt_key on success
4745 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4746 const struct dt_it *di)
4748 struct osd_it_ea *it = (struct osd_it_ea *)di;
4750 return (struct dt_key *)it->oie_dirent->oied_name;
4754 * Returns the key's size at current position from iterator's in memory structure.
4756 * \param di iterator's in memory structure
4758 * \retval key_size i.e. struct dt_key on success
4760 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4762 struct osd_it_ea *it = (struct osd_it_ea *)di;
4764 return it->oie_dirent->oied_namelen;
4768 osd_dirent_update(handle_t *jh, struct super_block *sb,
4769 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4770 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4772 struct osd_fid_pack *rec;
4776 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4777 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4779 rc = ldiskfs_journal_get_write_access(jh, bh);
4781 CERROR("%.16s: fail to write access for update dirent: "
4782 "name = %.*s, rc = %d\n",
4783 LDISKFS_SB(sb)->s_es->s_volume_name,
4784 ent->oied_namelen, ent->oied_name, rc);
4788 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4789 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4790 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4792 CERROR("%.16s: fail to dirty metadata for update dirent: "
4793 "name = %.*s, rc = %d\n",
4794 LDISKFS_SB(sb)->s_es->s_volume_name,
4795 ent->oied_namelen, ent->oied_name, rc);
4801 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4803 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4804 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4811 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4813 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4814 "dot_dotdot = %d\n", dot_dotdot);
4816 if (LDISKFS_DIR_REC_LEN(de) >=
4817 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4824 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4825 struct inode *dir, struct inode *inode,
4826 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4827 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4828 struct htree_lock *hlock)
4830 struct dentry *dentry;
4831 struct osd_fid_pack *rec;
4832 struct ldiskfs_dentry_param *ldp;
4836 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4837 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4840 /* There is enough space to hold the FID-in-dirent. */
4841 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4842 dir->i_sb->s_blocksize)) {
4843 rc = ldiskfs_journal_get_write_access(jh, bh);
4845 CERROR("%.16s: fail to write access for reinsert "
4846 "dirent: name = %.*s, rc = %d\n",
4847 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4848 ent->oied_namelen, ent->oied_name, rc);
4852 de->name[de->name_len] = 0;
4853 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4854 rec->fp_len = sizeof(struct lu_fid) + 1;
4855 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4856 de->file_type |= LDISKFS_DIRENT_LUFID;
4858 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4860 CERROR("%.16s: fail to dirty metadata for reinsert "
4861 "dirent: name = %.*s, rc = %d\n",
4862 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4863 ent->oied_namelen, ent->oied_name, rc);
4868 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4870 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4871 "name = %.*s, rc = %d\n",
4872 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4873 ent->oied_namelen, ent->oied_name, rc);
4877 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4879 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4880 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4881 dentry->d_fsdata = (void *)ldp;
4882 ll_vfs_dq_init(dir);
4883 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4884 /* It is too bad, we cannot reinsert the name entry back.
4885 * That means we lose it! */
4887 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4888 "name = %.*s, rc = %d\n",
4889 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4890 ent->oied_namelen, ent->oied_name, rc);
4896 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4897 struct osd_it_ea *it, struct lu_fid *fid,
4898 struct osd_inode_id *id, __u32 *attr)
4900 struct osd_thread_info *info = osd_oti_get(env);
4901 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4902 struct osd_device *dev = osd_obj2dev(obj);
4903 struct super_block *sb = osd_sb(dev);
4904 const char *devname =
4905 LDISKFS_SB(sb)->s_es->s_volume_name;
4906 struct osd_it_ea_dirent *ent = it->oie_dirent;
4907 struct inode *dir = obj->oo_inode;
4908 struct htree_lock *hlock = NULL;
4909 struct buffer_head *bh = NULL;
4910 handle_t *jh = NULL;
4911 struct ldiskfs_dir_entry_2 *de;
4912 struct dentry *dentry;
4913 struct inode *inode;
4920 if (ent->oied_name[0] == '.') {
4921 if (ent->oied_namelen == 1)
4923 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4927 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4930 /* We need to ensure that the name entry is still valid.
4931 * Because it may be removed or renamed by other already.
4933 * The unlink or rename operation will start journal before PDO lock,
4934 * so to avoid deadlock, here we need to start journal handle before
4935 * related PDO lock also. But because we do not know whether there
4936 * will be something to be repaired before PDO lock, we just start
4937 * journal without conditions.
4939 * We may need to remove the name entry firstly, then insert back.
4940 * One credit is for user quota file update.
4941 * One credit is for group quota file update.
4942 * Two credits are for dirty inode. */
4943 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4944 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4947 if (dev->od_dirent_journal) {
4948 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
4951 CERROR("%.16s: fail to start trans for dirent "
4952 "check_repair: credits %d, name %.*s, rc %d\n",
4953 devname, credits, ent->oied_namelen,
4954 ent->oied_name, rc);
4958 if (obj->oo_hl_head != NULL) {
4959 hlock = osd_oti_get(env)->oti_hlock;
4960 /* "0" means exclusive lock for the whole directory.
4961 * We need to prevent others access such name entry
4962 * during the delete + insert. Neither HLOCK_ADD nor
4963 * HLOCK_DEL cannot guarantee the atomicity. */
4964 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4966 down_write(&obj->oo_ext_idx_sem);
4969 if (obj->oo_hl_head != NULL) {
4970 hlock = osd_oti_get(env)->oti_hlock;
4971 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4972 LDISKFS_HLOCK_LOOKUP);
4974 down_read(&obj->oo_ext_idx_sem);
4978 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4979 /* For dot/dotdot entry, if there is not enough space to hold the
4980 * FID-in-dirent, just keep them there. It only happens when the
4981 * device upgraded from 1.8 or restored from MDT file-level backup.
4982 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4983 * and needs to get FID from LMA when readdir, it will not affect the
4984 * performance much. */
4985 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4986 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4987 *attr |= LUDA_IGNORE;
4988 GOTO(out_journal, rc = 0);
4991 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4992 inode = osd_iget(info, dev, id);
4993 if (IS_ERR(inode)) {
4994 rc = PTR_ERR(inode);
4995 if (rc == -ENOENT || rc == -ESTALE) {
4996 *attr |= LUDA_IGNORE;
5000 GOTO(out_journal, rc);
5003 /* skip the REMOTE_PARENT_DIR. */
5004 if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
5005 GOTO(out_inode, rc = 0);
5007 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5009 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5011 if (fid_is_sane(fid)) {
5012 /* FID-in-dirent is valid. */
5013 if (lu_fid_eq(fid, &lma->lma_self_fid))
5014 GOTO(out_inode, rc = 0);
5016 /* Do not repair under dryrun mode. */
5017 if (*attr & LUDA_VERIFY_DRYRUN) {
5018 *attr |= LUDA_REPAIR;
5019 GOTO(out_inode, rc = 0);
5022 if (!dev->od_dirent_journal) {
5026 ldiskfs_htree_unlock(hlock);
5028 up_read(&obj->oo_ext_idx_sem);
5029 dev->od_dirent_journal = 1;
5033 *fid = lma->lma_self_fid;
5035 /* Update the FID-in-dirent. */
5036 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5038 *attr |= LUDA_REPAIR;
5040 /* Do not repair under dryrun mode. */
5041 if (*attr & LUDA_VERIFY_DRYRUN) {
5042 *fid = lma->lma_self_fid;
5043 *attr |= LUDA_REPAIR;
5044 GOTO(out_inode, rc = 0);
5047 if (!dev->od_dirent_journal) {
5051 ldiskfs_htree_unlock(hlock);
5053 up_read(&obj->oo_ext_idx_sem);
5054 dev->od_dirent_journal = 1;
5058 *fid = lma->lma_self_fid;
5060 /* Append the FID-in-dirent. */
5061 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5062 fid, bh, de, hlock);
5064 *attr |= LUDA_REPAIR;
5066 } else if (rc == -ENODATA) {
5067 /* Do not repair under dryrun mode. */
5068 if (*attr & LUDA_VERIFY_DRYRUN) {
5069 if (fid_is_sane(fid)) {
5070 *attr |= LUDA_REPAIR;
5072 lu_igif_build(fid, inode->i_ino,
5073 inode->i_generation);
5074 *attr |= LUDA_UPGRADE;
5076 GOTO(out_inode, rc = 0);
5079 if (!dev->od_dirent_journal) {
5083 ldiskfs_htree_unlock(hlock);
5085 up_read(&obj->oo_ext_idx_sem);
5086 dev->od_dirent_journal = 1;
5091 if (unlikely(fid_is_sane(fid))) {
5092 /* FID-in-dirent exists, but FID-in-LMA is lost.
5093 * Trust the FID-in-dirent, and add FID-in-LMA. */
5094 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5096 *attr |= LUDA_REPAIR;
5098 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5099 /* It is probably IGIF object. Only aappend the
5100 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5101 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5102 fid, bh, de, hlock);
5104 *attr |= LUDA_UPGRADE;
5108 GOTO(out_inode, rc);
5115 if (hlock != NULL) {
5116 ldiskfs_htree_unlock(hlock);
5118 if (dev->od_dirent_journal)
5119 up_write(&obj->oo_ext_idx_sem);
5121 up_read(&obj->oo_ext_idx_sem);
5124 ldiskfs_journal_stop(jh);
5125 if (rc >= 0 && !dirty)
5126 dev->od_dirent_journal = 0;
5131 * Returns the value at current position from iterator's in memory structure.
5133 * \param di struct osd_it_ea, iterator's in memory structure
5134 * \param attr attr requested for dirent.
5135 * \param lde lustre dirent
5137 * \retval 0 no error and \param lde has correct lustre dirent.
5138 * \retval -ve on error
5140 static inline int osd_it_ea_rec(const struct lu_env *env,
5141 const struct dt_it *di,
5142 struct dt_rec *dtrec, __u32 attr)
5144 struct osd_it_ea *it = (struct osd_it_ea *)di;
5145 struct osd_object *obj = it->oie_obj;
5146 struct osd_device *dev = osd_obj2dev(obj);
5147 struct osd_scrub *scrub = &dev->od_scrub;
5148 struct scrub_file *sf = &scrub->os_file;
5149 struct osd_thread_info *oti = osd_oti_get(env);
5150 struct osd_inode_id *id = &oti->oti_id;
5151 struct osd_idmap_cache *oic = &oti->oti_cache;
5152 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5153 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5154 __u32 ino = it->oie_dirent->oied_ino;
5158 if (attr & LUDA_VERIFY) {
5160 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5161 attr |= LUDA_IGNORE;
5164 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5168 attr &= ~LU_DIRENT_ATTRS_MASK;
5169 if (!fid_is_sane(fid)) {
5170 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5173 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5175 osd_id_gen(id, ino, OSD_OII_NOGEN);
5179 /* Pack the entry anyway, at least the offset is right. */
5180 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5181 it->oie_dirent->oied_name,
5182 it->oie_dirent->oied_namelen,
5183 it->oie_dirent->oied_type, attr);
5188 if (osd_remote_fid(env, dev, fid))
5191 if (likely(!(attr & LUDA_IGNORE)))
5192 rc = osd_add_oi_cache(oti, dev, id, fid);
5194 if (!(attr & LUDA_VERIFY) &&
5195 (scrub->os_pos_current <= ino) &&
5196 ((sf->sf_flags & SF_INCONSISTENT) ||
5197 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5198 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5199 osd_consistency_check(oti, dev, oic);
5205 * Returns a cookie for current position of the iterator head, so that
5206 * user can use this cookie to load/start the iterator next time.
5208 * \param di iterator's in memory structure
5210 * \retval cookie for current position, on success
5212 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5214 struct osd_it_ea *it = (struct osd_it_ea *)di;
5216 return it->oie_dirent->oied_off;
5220 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5221 * to load a directory entry at a time and stored it i inn,
5222 * in iterator's in-memory data structure.
5224 * \param di struct osd_it_ea, iterator's in memory structure
5226 * \retval +ve on success
5227 * \retval -ve on error
5229 static int osd_it_ea_load(const struct lu_env *env,
5230 const struct dt_it *di, __u64 hash)
5232 struct osd_it_ea *it = (struct osd_it_ea *)di;
5236 it->oie_file.f_pos = hash;
5238 rc = osd_ldiskfs_it_fill(env, di);
5246 * Index lookup function for interoperability mode (b11826).
5248 * \param key, key i.e. file name to be searched
5250 * \retval +ve, on success
5251 * \retval -ve, on error
5253 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5254 struct dt_rec *rec, const struct dt_key *key,
5255 struct lustre_capa *capa)
5257 struct osd_object *obj = osd_dt_obj(dt);
5262 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5263 LINVRNT(osd_invariant(obj));
5265 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5268 rc = osd_ea_lookup_rec(env, obj, rec, key);
5275 * Index and Iterator operations for interoperability
5276 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5278 static const struct dt_index_operations osd_index_ea_ops = {
5279 .dio_lookup = osd_index_ea_lookup,
5280 .dio_declare_insert = osd_index_declare_ea_insert,
5281 .dio_insert = osd_index_ea_insert,
5282 .dio_declare_delete = osd_index_declare_ea_delete,
5283 .dio_delete = osd_index_ea_delete,
5285 .init = osd_it_ea_init,
5286 .fini = osd_it_ea_fini,
5287 .get = osd_it_ea_get,
5288 .put = osd_it_ea_put,
5289 .next = osd_it_ea_next,
5290 .key = osd_it_ea_key,
5291 .key_size = osd_it_ea_key_size,
5292 .rec = osd_it_ea_rec,
5293 .store = osd_it_ea_store,
5294 .load = osd_it_ea_load
5298 static void *osd_key_init(const struct lu_context *ctx,
5299 struct lu_context_key *key)
5301 struct osd_thread_info *info;
5303 OBD_ALLOC_PTR(info);
5305 return ERR_PTR(-ENOMEM);
5307 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5308 if (info->oti_it_ea_buf == NULL)
5311 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5313 info->oti_hlock = ldiskfs_htree_lock_alloc();
5314 if (info->oti_hlock == NULL)
5320 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5323 return ERR_PTR(-ENOMEM);
5326 static void osd_key_fini(const struct lu_context *ctx,
5327 struct lu_context_key *key, void* data)
5329 struct osd_thread_info *info = data;
5331 if (info->oti_hlock != NULL)
5332 ldiskfs_htree_lock_free(info->oti_hlock);
5333 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5334 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5335 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5339 static void osd_key_exit(const struct lu_context *ctx,
5340 struct lu_context_key *key, void *data)
5342 struct osd_thread_info *info = data;
5344 LASSERT(info->oti_r_locks == 0);
5345 LASSERT(info->oti_w_locks == 0);
5346 LASSERT(info->oti_txns == 0);
5349 /* type constructor/destructor: osd_type_init, osd_type_fini */
5350 LU_TYPE_INIT_FINI(osd, &osd_key);
5352 struct lu_context_key osd_key = {
5353 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5354 .lct_init = osd_key_init,
5355 .lct_fini = osd_key_fini,
5356 .lct_exit = osd_key_exit
5360 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5361 const char *name, struct lu_device *next)
5363 struct osd_device *osd = osd_dev(d);
5365 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5366 >= sizeof(osd->od_svname))
5368 return osd_procfs_init(osd, name);
5371 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5375 /* shutdown quota slave instance associated with the device */
5376 if (o->od_quota_slave != NULL) {
5377 qsd_fini(env, o->od_quota_slave);
5378 o->od_quota_slave = NULL;
5384 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5388 if (o->od_mnt != NULL) {
5389 shrink_dcache_sb(osd_sb(o));
5390 osd_sync(env, &o->od_dt_dev);
5399 static int osd_mount(const struct lu_env *env,
5400 struct osd_device *o, struct lustre_cfg *cfg)
5402 const char *name = lustre_cfg_string(cfg, 0);
5403 const char *dev = lustre_cfg_string(cfg, 1);
5405 unsigned long page, s_flags, lmd_flags = 0;
5406 struct page *__page;
5407 struct file_system_type *type;
5408 char *options = NULL;
5410 struct osd_thread_info *info = osd_oti_get(env);
5411 struct lu_fid *fid = &info->oti_fid;
5412 struct inode *inode;
5416 if (o->od_mnt != NULL)
5419 if (strlen(dev) >= sizeof(o->od_mntdev))
5421 strcpy(o->od_mntdev, dev);
5423 OBD_PAGE_ALLOC(__page, GFP_IOFS);
5425 GOTO(out, rc = -ENOMEM);
5427 str = lustre_cfg_string(cfg, 2);
5428 s_flags = simple_strtoul(str, NULL, 0);
5429 str = strstr(str, ":");
5431 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5432 opts = lustre_cfg_string(cfg, 3);
5433 page = (unsigned long)page_address(__page);
5434 options = (char *)page;
5437 strcat(options, "user_xattr,acl");
5439 strcat(options, opts);
5441 /* Glom up mount options */
5442 if (*options != '\0')
5443 strcat(options, ",");
5444 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5446 type = get_fs_type("ldiskfs");
5448 CERROR("%s: cannot find ldiskfs module\n", name);
5449 GOTO(out, rc = -ENODEV);
5452 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5453 module_put(type->owner);
5455 if (IS_ERR(o->od_mnt)) {
5456 rc = PTR_ERR(o->od_mnt);
5458 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5462 #ifdef HAVE_DEV_SET_RDONLY
5463 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5464 CERROR("%s: underlying device %s is marked as read-only. "
5465 "Setup failed\n", name, dev);
5466 GOTO(out_mnt, rc = -EROFS);
5470 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5471 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5472 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5473 GOTO(out_mnt, rc = -EINVAL);
5476 inode = osd_sb(o)->s_root->d_inode;
5477 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5478 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5479 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5481 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5485 if (lmd_flags & LMD_FLG_NOSCRUB)
5496 OBD_PAGE_FREE(__page);
5501 static struct lu_device *osd_device_fini(const struct lu_env *env,
5502 struct lu_device *d)
5504 struct osd_device *o = osd_dev(d);
5508 osd_shutdown(env, o);
5509 osd_scrub_cleanup(env, o);
5510 osd_obj_map_fini(o);
5516 static int osd_device_init0(const struct lu_env *env,
5517 struct osd_device *o,
5518 struct lustre_cfg *cfg)
5520 struct lu_device *l = osd2lu_dev(o);
5521 struct osd_thread_info *info;
5525 /* if the module was re-loaded, env can loose its keys */
5526 rc = lu_env_refill((struct lu_env *) env);
5529 info = osd_oti_get(env);
5532 l->ld_ops = &osd_lu_ops;
5533 o->od_dt_dev.dd_ops = &osd_dt_ops;
5535 spin_lock_init(&o->od_osfs_lock);
5536 mutex_init(&o->od_otable_mutex);
5537 o->od_osfs_age = cfs_time_shift_64(-1000);
5539 o->od_capa_hash = init_capa_hash();
5540 if (o->od_capa_hash == NULL)
5541 GOTO(out, rc = -ENOMEM);
5543 o->od_read_cache = 1;
5544 o->od_writethrough_cache = 1;
5545 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5547 rc = osd_mount(env, o, cfg);
5551 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5552 sizeof(o->od_svname));
5553 if (cplen >= sizeof(o->od_svname)) {
5558 if (server_name_is_ost(o->od_svname))
5561 rc = osd_obj_map_init(env, o);
5565 rc = lu_site_init(&o->od_site, l);
5567 GOTO(out_compat, rc);
5568 o->od_site.ls_bottom_dev = l;
5570 rc = lu_site_init_finish(&o->od_site);
5574 /* self-repair LMA by default */
5575 o->od_lma_self_repair = 1;
5577 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5578 /* setup scrub, including OI files initialization */
5579 rc = osd_scrub_setup(env, o);
5583 rc = osd_procfs_init(o, o->od_svname);
5585 CERROR("%s: can't initialize procfs: rc = %d\n",
5587 GOTO(out_scrub, rc);
5590 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5592 /* initialize quota slave instance */
5593 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5595 if (IS_ERR(o->od_quota_slave)) {
5596 rc = PTR_ERR(o->od_quota_slave);
5597 o->od_quota_slave = NULL;
5598 GOTO(out_procfs, rc);
5606 osd_scrub_cleanup(env, o);
5608 lu_site_fini(&o->od_site);
5610 osd_obj_map_fini(o);
5614 cleanup_capa_hash(o->od_capa_hash);
5619 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5620 struct lu_device_type *t,
5621 struct lustre_cfg *cfg)
5623 struct osd_device *o;
5628 return ERR_PTR(-ENOMEM);
5630 rc = dt_device_init(&o->od_dt_dev, t);
5632 /* Because the ctx might be revived in dt_device_init,
5633 * refill the env here */
5634 lu_env_refill((struct lu_env *)env);
5635 rc = osd_device_init0(env, o, cfg);
5637 dt_device_fini(&o->od_dt_dev);
5640 if (unlikely(rc != 0))
5643 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5646 static struct lu_device *osd_device_free(const struct lu_env *env,
5647 struct lu_device *d)
5649 struct osd_device *o = osd_dev(d);
5652 cleanup_capa_hash(o->od_capa_hash);
5653 /* XXX: make osd top device in order to release reference */
5654 d->ld_site->ls_top_dev = d;
5655 lu_site_purge(env, d->ld_site, -1);
5656 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5657 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5658 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5660 lu_site_fini(&o->od_site);
5661 dt_device_fini(&o->od_dt_dev);
5666 static int osd_process_config(const struct lu_env *env,
5667 struct lu_device *d, struct lustre_cfg *cfg)
5669 struct osd_device *o = osd_dev(d);
5673 switch(cfg->lcfg_command) {
5675 err = osd_mount(env, o, cfg);
5678 lu_dev_del_linkage(d->ld_site, d);
5679 err = osd_shutdown(env, o);
5688 static int osd_recovery_complete(const struct lu_env *env,
5689 struct lu_device *d)
5691 struct osd_device *osd = osd_dev(d);
5695 if (osd->od_quota_slave == NULL)
5698 /* start qsd instance on recovery completion, this notifies the quota
5699 * slave code that we are about to process new requests now */
5700 rc = qsd_start(env, osd->od_quota_slave);
5705 * we use exports to track all osd users
5707 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5708 struct obd_device *obd, struct obd_uuid *cluuid,
5709 struct obd_connect_data *data, void *localdata)
5711 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5712 struct lustre_handle conn;
5716 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5718 rc = class_connect(&conn, obd, cluuid);
5722 *exp = class_conn2export(&conn);
5724 spin_lock(&osd->od_osfs_lock);
5726 spin_unlock(&osd->od_osfs_lock);
5732 * once last export (we don't count self-export) disappeared
5733 * osd can be released
5735 static int osd_obd_disconnect(struct obd_export *exp)
5737 struct obd_device *obd = exp->exp_obd;
5738 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5739 int rc, release = 0;
5742 /* Only disconnect the underlying layers on the final disconnect. */
5743 spin_lock(&osd->od_osfs_lock);
5745 if (osd->od_connects == 0)
5747 spin_unlock(&osd->od_osfs_lock);
5749 rc = class_disconnect(exp); /* bz 9811 */
5751 if (rc == 0 && release)
5752 class_manual_cleanup(obd);
5756 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5757 struct lu_device *dev)
5759 struct osd_device *osd = osd_dev(dev);
5763 if (osd->od_quota_slave != NULL)
5764 /* set up quota slave objects */
5765 result = qsd_prepare(env, osd->od_quota_slave);
5770 static const struct lu_object_operations osd_lu_obj_ops = {
5771 .loo_object_init = osd_object_init,
5772 .loo_object_delete = osd_object_delete,
5773 .loo_object_release = osd_object_release,
5774 .loo_object_free = osd_object_free,
5775 .loo_object_print = osd_object_print,
5776 .loo_object_invariant = osd_object_invariant
5779 const struct lu_device_operations osd_lu_ops = {
5780 .ldo_object_alloc = osd_object_alloc,
5781 .ldo_process_config = osd_process_config,
5782 .ldo_recovery_complete = osd_recovery_complete,
5783 .ldo_prepare = osd_prepare,
5786 static const struct lu_device_type_operations osd_device_type_ops = {
5787 .ldto_init = osd_type_init,
5788 .ldto_fini = osd_type_fini,
5790 .ldto_start = osd_type_start,
5791 .ldto_stop = osd_type_stop,
5793 .ldto_device_alloc = osd_device_alloc,
5794 .ldto_device_free = osd_device_free,
5796 .ldto_device_init = osd_device_init,
5797 .ldto_device_fini = osd_device_fini
5800 struct lu_device_type osd_device_type = {
5801 .ldt_tags = LU_DEVICE_DT,
5802 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5803 .ldt_ops = &osd_device_type_ops,
5804 .ldt_ctx_tags = LCT_LOCAL,
5808 * lprocfs legacy support.
5810 static struct obd_ops osd_obd_device_ops = {
5811 .o_owner = THIS_MODULE,
5812 .o_connect = osd_obd_connect,
5813 .o_disconnect = osd_obd_disconnect
5816 static int __init osd_mod_init(void)
5818 struct lprocfs_static_vars lvars;
5822 lprocfs_osd_init_vars(&lvars);
5824 rc = lu_kmem_init(ldiskfs_caches);
5828 rc = class_register_type(&osd_obd_device_ops, NULL, NULL,
5829 #ifndef HAVE_ONLY_PROCFS_SEQ
5832 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5834 lu_kmem_fini(ldiskfs_caches);
5838 static void __exit osd_mod_exit(void)
5840 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5841 lu_kmem_fini(ldiskfs_caches);
5844 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5845 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5846 MODULE_LICENSE("GPL");
5848 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);