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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd/osd_handler.c
33 * Top-level entry points into osd module
35 * Author: Nikita Danilov <nikita@clusterfs.com>
36 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <linux/fs_struct.h>
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
46 /* prerequisite for linux/xattr.h */
47 #include <linux/types.h>
48 /* prerequisite for linux/xattr.h */
50 /* XATTR_{REPLACE,CREATE} */
51 #include <linux/xattr.h>
53 #include <ldiskfs/ldiskfs.h>
54 #include <ldiskfs/xattr.h>
55 #include <ldiskfs/ldiskfs_extents.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 <uapi/linux/lustre/lustre_param.h>
68 #include "osd_internal.h"
69 #include "osd_dynlocks.h"
71 /* llo_* api support */
72 #include <md_object.h>
73 #include <lustre_quota.h>
75 #include <lustre_linkea.h>
77 /* encoding routines */
78 #include <lustre_crypto.h>
80 /* Maximum EA size is limited by LNET_MTU for remote objects */
81 #define OSD_MAX_EA_SIZE 1048364
84 module_param(ldiskfs_pdo, int, 0644);
85 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
87 int ldiskfs_track_declares_assert;
88 module_param(ldiskfs_track_declares_assert, int, 0644);
89 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
91 /* Slab to allocate dynlocks */
92 struct kmem_cache *dynlock_cachep;
94 /* Slab to allocate osd_it_ea */
95 struct kmem_cache *osd_itea_cachep;
97 static struct lu_kmem_descr ldiskfs_caches[] = {
99 .ckd_cache = &dynlock_cachep,
100 .ckd_name = "dynlock_cache",
101 .ckd_size = sizeof(struct dynlock_handle)
104 .ckd_cache = &osd_itea_cachep,
105 .ckd_name = "osd_itea_cache",
106 .ckd_size = sizeof(struct osd_it_ea)
113 static const char dot[] = ".";
114 static const char dotdot[] = "..";
116 static const struct lu_object_operations osd_lu_obj_ops;
117 static const struct dt_object_operations osd_obj_ops;
118 static const struct dt_object_operations osd_obj_otable_it_ops;
119 static const struct dt_index_operations osd_index_iam_ops;
120 static const struct dt_index_operations osd_index_ea_ops;
122 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
123 const struct lu_fid *fid);
124 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
125 struct osd_device *osd);
127 int osd_trans_declare_op2rb[] = {
128 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
129 [OSD_OT_PUNCH] = OSD_OT_MAX,
130 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
131 [OSD_OT_CREATE] = OSD_OT_DESTROY,
132 [OSD_OT_DESTROY] = OSD_OT_CREATE,
133 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
134 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
135 [OSD_OT_WRITE] = OSD_OT_WRITE,
136 [OSD_OT_INSERT] = OSD_OT_DELETE,
137 [OSD_OT_DELETE] = OSD_OT_INSERT,
138 [OSD_OT_QUOTA] = OSD_OT_MAX,
141 static int osd_has_index(const struct osd_object *obj)
143 return obj->oo_dt.do_index_ops != NULL;
146 static int osd_object_invariant(const struct lu_object *l)
148 return osd_invariant(osd_obj(l));
152 * Concurrency: doesn't matter
154 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
156 struct osd_thread_info *oti = osd_oti_get(env);
158 return oti->oti_w_locks > 0 && o->oo_owner == env;
162 * Concurrency: doesn't access mutable data
164 static int osd_root_get(const struct lu_env *env,
165 struct dt_device *dev, struct lu_fid *f)
167 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
172 * the following set of functions are used to maintain per-thread
173 * cache of FID->ino mapping. this mechanism is needed to resolve
174 * FID to inode at dt_insert() which in turn stores ino in the
175 * directory entries to keep ldiskfs compatible with ext[34].
176 * due to locking-originated restrictions we can't lookup ino
177 * using LU cache (deadlock is possible). lookup using OI is quite
178 * expensive. so instead we maintain this cache and methods like
179 * dt_create() fill it. so in the majority of cases dt_insert() is
180 * able to find needed mapping in lockless manner.
182 static struct osd_idmap_cache *
183 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
184 const struct lu_fid *fid)
186 struct osd_thread_info *oti = osd_oti_get(env);
187 struct osd_idmap_cache *idc = oti->oti_ins_cache;
190 for (i = 0; i < oti->oti_ins_cache_used; i++) {
191 if (!lu_fid_eq(&idc[i].oic_fid, fid))
193 if (idc[i].oic_dev != osd)
202 static struct osd_idmap_cache *
203 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
204 const struct lu_fid *fid)
206 struct osd_thread_info *oti = osd_oti_get(env);
207 struct osd_idmap_cache *idc;
210 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
211 i = oti->oti_ins_cache_size * 2;
213 i = OSD_INS_CACHE_SIZE;
214 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
216 return ERR_PTR(-ENOMEM);
217 if (oti->oti_ins_cache != NULL) {
218 memcpy(idc, oti->oti_ins_cache,
219 oti->oti_ins_cache_used * sizeof(*idc));
220 OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
221 oti->oti_ins_cache_used);
223 oti->oti_ins_cache = idc;
224 oti->oti_ins_cache_size = i;
227 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
230 idc->oic_lid.oii_ino = 0;
231 idc->oic_lid.oii_gen = 0;
238 * lookup mapping for the given fid in the cache, initialize a
239 * new one if not found. the initialization checks whether the
240 * object is local or remote. for local objects, OI is used to
241 * learn ino/generation. the function is used when the caller
242 * has no information about the object, e.g. at dt_insert().
244 static struct osd_idmap_cache *
245 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
246 const struct lu_fid *fid)
248 struct osd_idmap_cache *idc;
251 idc = osd_idc_find(env, osd, fid);
252 LASSERT(!IS_ERR(idc));
256 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
257 osd->od_svname, PFID(fid));
259 /* new mapping is needed */
260 idc = osd_idc_add(env, osd, fid);
262 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
263 osd->od_svname, PFID(fid), PTR_ERR(idc));
268 rc = osd_remote_fid(env, osd, fid);
269 if (unlikely(rc < 0))
273 /* the object is local, lookup in OI */
274 /* XXX: probably cheaper to lookup in LU first? */
275 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
277 if (unlikely(rc < 0)) {
278 CERROR("can't lookup: rc = %d\n", rc);
282 /* the object is remote */
289 static void osd_idc_dump_lma(const struct lu_env *env,
290 struct osd_device *osd,
294 struct osd_thread_info *info = osd_oti_get(env);
295 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
296 const struct lu_fid *fid;
297 struct osd_inode_id lid;
301 inode = osd_ldiskfs_iget(osd_sb(osd), ino);
303 CERROR("%s: can't get inode %lu: rc = %d\n",
304 osd->od_svname, ino, (int)PTR_ERR(inode));
307 if (is_bad_inode(inode)) {
308 CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
311 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
313 CERROR("%s: can't get LMA for %lu: rc = %d\n",
314 osd->od_svname, ino, rc);
317 fid = &loa->loa_lma.lma_self_fid;
318 LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
319 PFID(fid), ino, (unsigned)inode->i_generation);
322 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
324 CERROR("%s: can't lookup "DFID": rc = %d\n",
325 osd->od_svname, PFID(fid), rc);
328 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
329 PFID(fid), lid.oii_ino, lid.oii_gen);
334 static void osd_idc_dump_debug(const struct lu_env *env,
335 struct osd_device *osd,
336 const struct lu_fid *fid,
340 struct osd_inode_id lid;
344 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
346 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
347 osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
348 osd_idc_dump_lma(env, osd, lid.oii_ino, false);
350 CERROR("%s: can't lookup "DFID": rc = %d\n",
351 osd->od_svname, PFID(fid), rc);
354 osd_idc_dump_lma(env, osd, ino1, true);
356 osd_idc_dump_lma(env, osd, ino2, true);
360 * lookup mapping for given FID and fill it from the given object.
361 * the object is lolcal by definition.
363 static int osd_idc_find_and_init(const struct lu_env *env,
364 struct osd_device *osd,
365 struct osd_object *obj)
367 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
368 struct osd_idmap_cache *idc;
370 idc = osd_idc_find(env, osd, fid);
371 LASSERT(!IS_ERR(idc));
373 if (obj->oo_inode == NULL)
375 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
376 if (idc->oic_lid.oii_ino) {
377 osd_idc_dump_debug(env, osd, fid,
378 idc->oic_lid.oii_ino,
379 obj->oo_inode->i_ino);
382 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
383 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
388 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
389 osd->od_svname, PFID(fid));
391 /* new mapping is needed */
392 idc = osd_idc_add(env, osd, fid);
394 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
395 osd->od_svname, PFID(fid), PTR_ERR(idc));
399 if (obj->oo_inode != NULL) {
400 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
401 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
407 * OSD object methods.
411 * Concurrency: no concurrent access is possible that early in object
414 static struct lu_object *osd_object_alloc(const struct lu_env *env,
415 const struct lu_object_header *hdr,
418 struct osd_object *mo;
423 struct lu_object_header *h;
424 struct osd_device *o = osd_dev(d);
426 l = &mo->oo_dt.do_lu;
427 if (unlikely(o->od_in_init)) {
434 lu_object_header_init(h);
435 lu_object_init(l, h, d);
436 lu_object_add_top(h, l);
439 dt_object_init(&mo->oo_dt, NULL, d);
440 mo->oo_header = NULL;
443 mo->oo_dt.do_ops = &osd_obj_ops;
444 l->lo_ops = &osd_lu_obj_ops;
445 init_rwsem(&mo->oo_sem);
446 init_rwsem(&mo->oo_ext_idx_sem);
447 spin_lock_init(&mo->oo_guard);
448 INIT_LIST_HEAD(&mo->oo_xattr_list);
454 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
455 struct dentry *dentry, struct lustre_ost_attrs *loa)
459 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
460 (void *)loa, sizeof(*loa));
462 struct lustre_mdt_attrs *lma = &loa->loa_lma;
464 if (rc < sizeof(*lma))
468 lustre_loa_swab(loa, true);
469 /* Check LMA compatibility */
470 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
472 CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
474 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
475 PFID(&lma->lma_self_fid), inode->i_ino, rc);
477 } else if (rc == 0) {
485 * retrieve object from backend ext fs.
487 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
488 struct osd_inode_id *id)
491 struct inode *inode = NULL;
494 * if we look for an inode withing a running
495 * transaction, then we risk to deadlock
496 * osd_dirent_check_repair() breaks this
498 /* LASSERT(current->journal_info == NULL); */
500 inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
502 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
503 id->oii_ino, PTR_ERR(inode));
504 } else if (id->oii_gen != OSD_OII_NOGEN &&
505 inode->i_generation != id->oii_gen) {
506 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
507 "i_generation = %u\n",
508 id->oii_ino, id->oii_gen, inode->i_generation);
510 inode = ERR_PTR(-ESTALE);
511 } else if (inode->i_nlink == 0) {
513 * due to parallel readdir and unlink,
514 * we can have dead inode here.
516 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
518 inode = ERR_PTR(-ESTALE);
519 } else if (is_bad_inode(inode)) {
520 CWARN("%s: bad inode: ino = %u: rc = %d\n",
521 osd_dev2name(dev), id->oii_ino, -ENOENT);
523 inode = ERR_PTR(-ENOENT);
524 } else if (osd_is_ea_inode(inode)) {
526 * EA inode is internal ldiskfs object, should don't visible
529 CDEBUG(D_INODE, "EA inode: ino = %u\n", id->oii_ino);
531 inode = ERR_PTR(-ENOENT);
532 } else if ((rc = osd_attach_jinode(inode))) {
536 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
537 if (id->oii_gen == OSD_OII_NOGEN)
538 osd_id_gen(id, inode->i_ino, inode->i_generation);
541 * Do not update file c/mtime in ldiskfs.
542 * NB: we don't have any lock to protect this because we don't
543 * have reference on osd_object now, but contention with
544 * another lookup + attr_set can't happen in the tiny window
545 * between if (...) and set S_NOCMTIME.
547 if (!(inode->i_flags & S_NOCMTIME))
548 inode->i_flags |= S_NOCMTIME;
553 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
554 handle_t *handle, struct dentry *child,
555 struct inode *inode, struct htree_lock *hlock)
559 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
560 if (rc == -ENOBUFS || rc == -ENOSPC) {
561 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
562 struct inode *parent = child->d_parent->d_inode;
563 struct lu_fid *fid = NULL;
564 char fidstr[FID_LEN + 1] = "unknown";
566 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
568 fid = &loa->loa_lma.lma_self_fid;
569 } else if (rc2 == -ENODATA) {
570 if (unlikely(is_root_inode(parent))) {
571 fid = &info->oti_fid3;
572 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
573 } else if (!osd->od_is_ost && osd->od_index == 0) {
574 fid = &info->oti_fid3;
575 lu_igif_build(fid, parent->i_ino,
576 parent->i_generation);
581 snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
583 /* below message is checked in sanity.sh test_129 */
585 CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
586 osd_name(osd), parent->i_ino, fidstr);
588 rc = 0; /* ignore such error now */
589 CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
590 osd_name(osd), parent->i_ino, fidstr);
600 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
601 struct osd_inode_id *id, struct lu_fid *fid)
603 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
607 inode = osd_iget(info, dev, id);
611 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
613 *fid = loa->loa_lma.lma_self_fid;
614 } else if (rc == -ENODATA) {
615 if (unlikely(is_root_inode(inode)))
616 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
618 lu_igif_build(fid, inode->i_ino, inode->i_generation);
626 static struct inode *osd_iget_check(struct osd_thread_info *info,
627 struct osd_device *dev,
628 const struct lu_fid *fid,
629 struct osd_inode_id *id,
638 * The cached OI mapping is trustable. If we cannot locate the inode
639 * via the cached OI mapping, then return the failure to the caller
640 * directly without further OI checking.
644 inode = osd_iget(info, dev, id);
647 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
650 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
651 PFID(fid), id->oii_ino, rc);
657 __u32 saved_ino = id->oii_ino;
658 __u32 saved_gen = id->oii_gen;
661 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
663 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
665 * XXX: There are four possible cases:
667 * Backup/restore caused the OI invalid.
669 * Someone unlinked the object but NOT removed
670 * the OI mapping, such as mount target device
671 * as ldiskfs, and modify something directly.
673 * Someone just removed the object between the
674 * former oi_lookup and the iget. It is normal.
675 * 4. Other failure cases.
677 * Generally, when the device is mounted, it will
678 * auto check whether the system is restored from
679 * file-level backup or not. We trust such detect
680 * to distinguish the 1st case from the 2nd case:
681 * if the OI files are consistent but may contain
682 * stale OI mappings because of case 2, if iget()
683 * returns -ENOENT or -ESTALE, then it should be
688 * If the OI mapping was in OI file before the
689 * osd_iget_check(), but now, it is disappear,
690 * then it must be removed by race. That is a
696 * It is the OI scrub updated the OI mapping by race.
697 * The new OI mapping must be valid.
699 if (saved_ino != id->oii_ino ||
700 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
709 if (dev->od_scrub.os_scrub.os_file.sf_flags &
712 * It still can be the case 2, but we cannot
713 * distinguish it from the case 1. So return
714 * -EREMCHG to block current operation until
715 * OI scrub rebuilt the OI mappings.
724 if (inode->i_generation == id->oii_gen)
744 * \retval +v: new filter_fid does not contain self-fid
745 * \retval 0: filter_fid_18_23, contains self-fid
746 * \retval -v: other failure cases
748 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
749 struct dentry *dentry, struct lu_fid *fid)
751 struct filter_fid *ff = &info->oti_ff;
752 struct ost_id *ostid = &info->oti_ostid;
755 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
756 if (rc == sizeof(struct filter_fid_18_23)) {
757 struct filter_fid_18_23 *ff_old = (void *)ff;
759 ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
760 rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
762 * XXX: use 0 as the index for compatibility, the caller will
763 * handle index related issues when necessary.
766 ostid_to_fid(fid, ostid, 0);
767 } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
769 } else if (rc >= 0) {
776 static int osd_lma_self_repair(struct osd_thread_info *info,
777 struct osd_device *osd, struct inode *inode,
778 const struct lu_fid *fid, __u32 compat)
783 LASSERT(current->journal_info == NULL);
785 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
786 osd_dto_credits_noquota[DTO_XATTR_SET]);
789 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
794 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
796 CWARN("%s: cannot self repair the LMA: rc = %d\n",
798 ldiskfs_journal_stop(jh);
802 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
804 struct osd_thread_info *info = osd_oti_get(env);
805 struct osd_device *osd = osd_obj2dev(obj);
806 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
807 struct lustre_mdt_attrs *lma = &loa->loa_lma;
808 struct inode *inode = obj->oo_inode;
809 struct dentry *dentry = &info->oti_obj_dentry;
810 struct lu_fid *fid = NULL;
811 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
816 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
817 (void *)loa, sizeof(*loa));
818 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
819 fid = &lma->lma_self_fid;
820 rc = osd_get_idif(info, inode, dentry, fid);
821 if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
823 * For the given OST-object, if it has neither LMA nor
824 * FID in XATTR_NAME_FID, then the given FID (which is
825 * contained in the @obj, from client RPC for locating
826 * the OST-object) is trusted. We use it to generate
829 osd_lma_self_repair(info, osd, inode, rfid,
840 lustre_lma_swab(lma);
841 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
842 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
843 S_ISREG(inode->i_mode)))) {
844 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
845 "fid = "DFID", ino = %lu\n", osd_name(osd),
846 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
847 PFID(rfid), inode->i_ino);
850 fid = &lma->lma_self_fid;
851 if (lma->lma_compat & LMAC_STRIPE_INFO &&
853 obj->oo_pfid_in_lma = 1;
854 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
856 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
860 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
861 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
862 struct ost_id *oi = &info->oti_ostid;
863 struct lu_fid *fid1 = &info->oti_fid3;
864 __u32 idx = fid_idif_ost_idx(rfid);
867 * For old IDIF, the OST index is not part of the IDIF,
868 * Means that different OSTs may have the same IDIFs.
869 * Under such case, we need to make some compatible
870 * check to make sure to trigger OI scrub properly.
872 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
873 /* Given @rfid is new, LMA is old. */
874 fid_to_ostid(fid, oi);
875 ostid_to_fid(fid1, oi, idx);
876 if (lu_fid_eq(fid1, rfid)) {
877 if (osd->od_index_in_idif)
878 osd_lma_self_repair(info, osd,
892 struct osd_check_lmv_buf {
893 /* please keep it as first member */
894 struct dir_context ctx;
895 struct osd_thread_info *oclb_info;
896 struct osd_device *oclb_dev;
902 * It is called internally by ->iterate*() to filter out the
903 * local slave object's FID of the striped directory.
905 * \retval 1 found the local slave's FID
906 * \retval 0 continue to check next item
907 * \retval -ve for failure
909 #ifdef HAVE_FILLDIR_USE_CTX
910 static int osd_stripe_dir_filldir(struct dir_context *buf,
912 static int osd_stripe_dir_filldir(void *buf,
914 const char *name, int namelen,
915 loff_t offset, __u64 ino, unsigned int d_type)
917 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
918 struct osd_thread_info *oti = oclb->oclb_info;
919 struct lu_fid *fid = &oti->oti_fid3;
920 struct osd_inode_id *id = &oti->oti_id3;
921 struct osd_device *dev = oclb->oclb_dev;
930 sscanf(name + 1, SFID, RFID(fid));
931 if (!fid_is_sane(fid))
934 if (osd_remote_fid(oti->oti_env, dev, fid))
937 osd_id_gen(id, ino, OSD_OII_NOGEN);
938 inode = osd_iget(oti, dev, id);
940 return PTR_ERR(inode);
943 osd_add_oi_cache(oti, dev, id, fid);
944 osd_scrub_oi_insert(dev, fid, id, true);
945 oclb->oclb_found = true;
951 * When lookup item under striped directory, we need to locate the master
952 * MDT-object of the striped directory firstly, then the client will send
953 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
954 * and the item's name. If the system is restored from MDT file level backup,
955 * then before the OI scrub completely built the OI files, the OI mappings of
956 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
957 * not a problem for the master MDT-object. Because when locate the master
958 * MDT-object, we will do name based lookup (for the striped directory itself)
959 * firstly, during such process we can setup the correct OI mapping for the
960 * master MDT-object. But it will be trouble for the slave MDT-object. Because
961 * the client will not trigger name based lookup on the MDT to locate the slave
962 * MDT-object before locating item under the striped directory, then when
963 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
964 * is invalid and does not know what the right OI mapping is, then the MDT has
965 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
966 * the OI file, related OI mapping is unknown yet, please try again later. And
967 * then client will re-try the RPC again and again until related OI mapping has
968 * been updated. That is quite inefficient.
970 * To resolve above trouble, we will handle it as the following two cases:
972 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
973 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
974 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
975 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
976 * directly. Please check osd_fid_lookup().
978 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
979 * Under such case, during lookup the master MDT-object, we will lookup the
980 * slave MDT-object via readdir against the master MDT-object, because the
981 * slave MDT-objects information are stored as sub-directories with the name
982 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
983 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
984 * the correct OI mapping for the slave MDT-object.
986 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
989 struct lu_buf *buf = &oti->oti_big_buf;
991 struct lmv_mds_md_v1 *lmv1;
992 struct osd_check_lmv_buf oclb = {
993 .ctx.actor = osd_stripe_dir_filldir,
1001 /* We should use the VFS layer to create a real dentry. */
1002 oti->oti_obj_dentry.d_inode = inode;
1003 oti->oti_obj_dentry.d_sb = inode->i_sb;
1005 filp = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
1010 filp->f_mode |= FMODE_64BITHASH;
1014 rc = __osd_xattr_get(inode, filp->f_path.dentry, XATTR_NAME_LMV,
1015 buf->lb_buf, buf->lb_len);
1016 if (rc == -ERANGE) {
1017 rc = __osd_xattr_get(inode, filp->f_path.dentry,
1018 XATTR_NAME_LMV, NULL, 0);
1020 lu_buf_realloc(buf, rc);
1021 if (buf->lb_buf == NULL)
1022 GOTO(out, rc = -ENOMEM);
1028 if (unlikely(rc == 0 || rc == -ENODATA))
1034 if (unlikely(buf->lb_buf == NULL)) {
1035 lu_buf_realloc(buf, rc);
1036 if (buf->lb_buf == NULL)
1037 GOTO(out, rc = -ENOMEM);
1043 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1047 oclb.oclb_items = 0;
1048 rc = iterate_dir(filp, &oclb.ctx);
1049 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1050 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1055 "%s: cannot check LMV, ino = %lu/%u: rc = %d\n",
1056 osd_ino2name(inode), inode->i_ino, inode->i_generation,
1065 * Is object in scrub inconsistent/stale list.
1067 * \a scrub has two lists, os_inconsistent_items contains mappings to fix, while
1068 * os_stale_items contains mappings failed to fix.
1070 static bool fid_in_scrub_list(struct lustre_scrub *scrub,
1071 const struct list_head *list,
1072 const struct lu_fid *fid)
1074 struct osd_inconsistent_item *oii;
1076 if (list_empty(list))
1079 spin_lock(&scrub->os_lock);
1080 list_for_each_entry(oii, list, oii_list) {
1081 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1082 spin_unlock(&scrub->os_lock);
1086 spin_unlock(&scrub->os_lock);
1091 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1092 const struct lu_fid *fid,
1093 const struct lu_object_conf *conf)
1095 struct osd_thread_info *info;
1096 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1097 struct osd_device *dev;
1098 struct osd_idmap_cache *oic;
1099 struct osd_inode_id *id;
1100 struct inode *inode = NULL;
1101 struct lustre_scrub *scrub;
1102 struct scrub_file *sf;
1103 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1108 bool remote = false;
1109 bool trusted = true;
1110 bool updated = false;
1111 bool checked = false;
1116 LINVRNT(osd_invariant(obj));
1117 LASSERT(obj->oo_inode == NULL);
1119 if (fid_is_sane(fid) == 0) {
1120 CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
1126 dev = osd_dev(ldev);
1127 scrub = &dev->od_scrub.os_scrub;
1128 sf = &scrub->os_file;
1129 info = osd_oti_get(env);
1131 oic = &info->oti_cache;
1133 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1137 * For the object is created as locking anchor, or for the object to
1138 * be created on disk. No need to osd_oi_lookup() at here because FID
1139 * shouldn't never be re-used, if it's really a duplicate FID from
1140 * unexpected reason, we should be able to detect it later by calling
1141 * do_create->osd_oi_insert().
1143 if (conf && conf->loc_flags & LOC_F_NEW)
1146 /* Search order: 1. per-thread cache. */
1147 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1152 /* Search order: 2. OI scrub pending list. */
1154 memset(id, 0, sizeof(struct osd_inode_id));
1155 if (fid_in_scrub_list(scrub, &scrub->os_inconsistent_items, fid))
1156 RETURN(-EINPROGRESS);
1158 stale = fid_in_scrub_list(scrub, &scrub->os_stale_items, fid);
1159 if (stale && OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1163 * The OI mapping in the OI file can be updated by the OI scrub
1164 * when we locate the inode via FID. So it may be not trustable.
1168 /* Search order: 3. OI files. */
1169 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1170 if (result == -ENOENT) {
1171 if (!fid_is_norm(fid) ||
1172 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1173 !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1175 GOTO(out, result = 0);
1180 /* -ESTALE is returned if inode of OST object doesn't exist */
1181 if (result == -ESTALE &&
1182 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1183 GOTO(out, result = 0);
1190 obj->oo_inode = NULL;
1191 /* for later passes through checks, not true on first pass */
1192 if (!IS_ERR_OR_NULL(inode))
1195 inode = osd_iget_check(info, dev, fid, id, trusted);
1196 if (!IS_ERR(inode)) {
1197 obj->oo_inode = inode;
1205 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1208 result = PTR_ERR(inode);
1209 if (result == -ENOENT || result == -ESTALE)
1210 GOTO(out, result = 0);
1212 if (result != -EREMCHG)
1216 /* don't trigger repeatedly for stale mapping */
1218 GOTO(out, result = -ESTALE);
1221 * We still have chance to get the valid inode: for the
1222 * object which is referenced by remote name entry, the
1223 * object on the local MDT will be linked under the dir
1224 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1226 * We do not know whether the object for the given FID
1227 * is referenced by some remote name entry or not, and
1228 * especially for DNE II, a multiple-linked object may
1229 * have many name entries reside on many MDTs.
1231 * To simplify the operation, OSD will not distinguish
1232 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1233 * only happened for the RPC from other MDT during the
1234 * OI scrub, or for the client side RPC with FID only,
1235 * such as FID to path, or from old connected client.
1238 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1242 flags |= SS_AUTO_PARTIAL;
1243 flags &= ~SS_AUTO_FULL;
1248 if (scrub->os_running) {
1249 if (scrub->os_partial_scan && !scrub->os_in_join)
1252 if (IS_ERR_OR_NULL(inode) || result) {
1253 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1254 GOTO(out, result = -EINPROGRESS);
1258 LASSERT(obj->oo_inode == inode);
1260 osd_scrub_oi_insert(dev, fid, id, true);
1264 if (dev->od_scrub.os_scrub.os_auto_scrub_interval == AS_NEVER) {
1266 GOTO(out, result = -EREMCHG);
1269 LASSERT(obj->oo_inode == inode);
1271 osd_add_oi_cache(info, dev, id, fid);
1276 if (IS_ERR_OR_NULL(inode) || result)
1277 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1279 rc1 = osd_scrub_start(env, dev, flags);
1280 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
1281 "%s: trigger OI scrub by RPC for "DFID"/%u with flags %#x: rc = %d\n",
1282 osd_name(dev), PFID(fid), id->oii_ino, flags, rc1);
1283 if (rc1 && rc1 != -EALREADY)
1284 GOTO(out, result = -EREMCHG);
1286 if (IS_ERR_OR_NULL(inode) || result)
1287 GOTO(out, result = -EINPROGRESS);
1290 LASSERT(obj->oo_inode == inode);
1295 if (unlikely(obj->oo_header))
1298 result = osd_check_lma(env, obj);
1302 LASSERTF(id->oii_ino == inode->i_ino &&
1303 id->oii_gen == inode->i_generation,
1304 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1305 PFID(fid), id->oii_ino, id->oii_gen,
1306 inode->i_ino, inode->i_generation);
1308 saved_ino = inode->i_ino;
1309 saved_gen = inode->i_generation;
1311 if (unlikely(result == -ENODATA)) {
1313 * If the OI scrub updated the OI mapping by race, it
1314 * must be valid. Trust the inode that has no LMA EA.
1319 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1322 * The OI mapping is still there, the inode is still
1323 * valid. It is just becaues the inode has no LMA EA.
1325 if (saved_ino == id->oii_ino &&
1326 saved_gen == id->oii_gen)
1330 * It is the OI scrub updated the OI mapping by race.
1331 * The new OI mapping must be valid.
1339 * "result == -ENOENT" means that the OI mappinghas been
1340 * removed by race, so the inode belongs to other object.
1342 * Others error can be returned directly.
1344 if (result == -ENOENT) {
1347 obj->oo_inode = NULL;
1352 if (result != -EREMCHG)
1358 * if two OST objects map to the same inode, and inode mode is
1359 * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1360 * reserved by precreate, and not written yet, in this case, don't
1361 * set inode for the object whose FID mismatch, so that it can create
1362 * inode and not block precreate.
1364 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1365 inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1366 obj->oo_inode = NULL;
1367 GOTO(out, result = 0);
1370 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1372 * "result == -ENOENT" means the cached OI mapping has been removed
1373 * from the OI file by race, above inode belongs to other object.
1375 if (result == -ENOENT) {
1378 obj->oo_inode = NULL;
1379 GOTO(out, result = 0);
1385 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1387 osd_scrub_refresh_mapping(info, dev, fid, id, DTO_INDEX_DELETE,
1393 * It is the OI scrub updated the OI mapping by race.
1394 * The new OI mapping must be valid.
1402 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1403 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1405 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1407 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1409 obj->oo_pfid_in_lma = 1;
1410 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1412 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1413 } else if (result != -ENODATA) {
1418 obj->oo_compat_dot_created = 1;
1419 obj->oo_compat_dotdot_created = 1;
1421 if (S_ISDIR(inode->i_mode) &&
1422 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1423 osd_check_lmv(info, dev, inode);
1425 result = osd_attach_jinode(inode);
1430 GOTO(out, result = 0);
1432 LASSERT(!obj->oo_hl_head);
1433 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1435 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1438 if (!result && stale)
1439 osd_scrub_oi_resurrect(scrub, fid);
1441 if (result || !obj->oo_inode) {
1442 if (!IS_ERR_OR_NULL(inode))
1445 obj->oo_inode = NULL;
1447 fid_zero(&oic->oic_fid);
1450 LINVRNT(osd_invariant(obj));
1455 * Concurrency: shouldn't matter.
1457 static void osd_object_init0(struct osd_object *obj)
1459 LASSERT(obj->oo_inode != NULL);
1460 obj->oo_dt.do_body_ops = &osd_body_ops;
1461 obj->oo_dt.do_lu.lo_header->loh_attr |=
1462 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1466 * Concurrency: no concurrent access is possible that early in object
1469 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1470 const struct lu_object_conf *conf)
1472 struct osd_object *obj = osd_obj(l);
1475 LINVRNT(osd_invariant(obj));
1477 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1478 cfs_fail_val == 2) {
1479 struct osd_thread_info *info = osd_oti_get(env);
1480 struct osd_idmap_cache *oic = &info->oti_cache;
1481 /* invalidate thread cache */
1482 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1484 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1485 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1486 l->lo_header->loh_attr |= LOHA_EXISTS;
1490 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1491 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1492 if (result == 0 && obj->oo_inode != NULL) {
1493 struct osd_thread_info *oti = osd_oti_get(env);
1494 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1496 osd_object_init0(obj);
1497 if (unlikely(obj->oo_header))
1500 result = osd_get_lma(oti, obj->oo_inode,
1501 &oti->oti_obj_dentry, loa);
1504 * Convert LMAI flags to lustre LMA flags
1505 * and cache it to oo_lma_flags
1508 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1509 } else if (result == -ENODATA) {
1513 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1515 LINVRNT(osd_invariant(obj));
1520 * The first part of oxe_buf is xattr name, and is '\0' terminated.
1521 * The left part is for value, binary mode.
1523 struct osd_xattr_entry {
1524 struct list_head oxe_list;
1528 struct rcu_head oxe_rcu;
1532 static int osd_oxc_get(struct osd_object *obj, const char *name,
1535 struct osd_xattr_entry *tmp;
1536 struct osd_xattr_entry *oxe = NULL;
1537 size_t namelen = strlen(name);
1541 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1542 if (namelen == tmp->oxe_namelen &&
1543 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1550 GOTO(out, rc = -ENOENT);
1552 if (!oxe->oxe_exist)
1553 GOTO(out, rc = -ENODATA);
1556 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1559 if (buf->lb_buf == NULL)
1562 if (buf->lb_len < rc)
1563 GOTO(out, rc = -ERANGE);
1565 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1572 static void osd_oxc_free(struct rcu_head *head)
1574 struct osd_xattr_entry *oxe;
1576 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1577 OBD_FREE(oxe, oxe->oxe_len);
1580 static void osd_oxc_add(struct osd_object *obj, const char *name,
1581 const char *buf, int buflen)
1583 struct osd_xattr_entry *oxe;
1584 struct osd_xattr_entry *old = NULL;
1585 struct osd_xattr_entry *tmp;
1586 size_t namelen = strlen(name);
1587 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1589 OBD_ALLOC(oxe, len);
1593 INIT_LIST_HEAD(&oxe->oxe_list);
1595 oxe->oxe_namelen = namelen;
1596 memcpy(oxe->oxe_buf, name, namelen);
1598 LASSERT(buf != NULL);
1599 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1600 oxe->oxe_exist = true;
1602 oxe->oxe_exist = false;
1605 /* this should be rarely called, just remove old and add new */
1606 spin_lock(&obj->oo_guard);
1607 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1608 if (namelen == tmp->oxe_namelen &&
1609 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1615 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1616 call_rcu(&old->oxe_rcu, osd_oxc_free);
1618 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1620 spin_unlock(&obj->oo_guard);
1623 static void osd_oxc_del(struct osd_object *obj, const char *name)
1625 struct osd_xattr_entry *oxe;
1626 size_t namelen = strlen(name);
1628 spin_lock(&obj->oo_guard);
1629 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1630 if (namelen == oxe->oxe_namelen &&
1631 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1632 list_del_rcu(&oxe->oxe_list);
1633 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1637 spin_unlock(&obj->oo_guard);
1640 static void osd_oxc_fini(struct osd_object *obj)
1642 struct osd_xattr_entry *oxe, *next;
1644 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1645 list_del(&oxe->oxe_list);
1646 OBD_FREE(oxe, oxe->oxe_len);
1651 * Concurrency: no concurrent access is possible that late in object
1654 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1656 struct osd_object *obj = osd_obj(l);
1657 struct lu_object_header *h = obj->oo_header;
1659 LINVRNT(osd_invariant(obj));
1662 dt_object_fini(&obj->oo_dt);
1663 if (obj->oo_hl_head != NULL)
1664 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1665 /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1668 lu_object_header_free(h);
1672 * Concurrency: no concurrent access is possible that late in object
1675 static void osd_index_fini(struct osd_object *o)
1677 struct iam_container *bag;
1679 if (o->oo_dir != NULL) {
1680 bag = &o->oo_dir->od_container;
1681 if (o->oo_inode != NULL) {
1682 if (bag->ic_object == o->oo_inode)
1683 iam_container_fini(bag);
1685 OBD_FREE_PTR(o->oo_dir);
1691 OSD_TXN_OI_DELETE_CREDITS = 20,
1692 OSD_TXN_INODE_DELETE_CREDITS = 20
1699 #if OSD_THANDLE_STATS
1701 * Set time when the handle is allocated
1703 static void osd_th_alloced(struct osd_thandle *oth)
1705 oth->oth_alloced = ktime_get();
1709 * Set time when the handle started
1711 static void osd_th_started(struct osd_thandle *oth)
1713 oth->oth_started = ktime_get();
1717 * Check whether the we deal with this handle for too long.
1719 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1720 ktime_t alloced, ktime_t started,
1723 ktime_t now = ktime_get();
1725 LASSERT(dev != NULL);
1727 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1728 ktime_us_delta(started, alloced));
1729 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1730 ktime_us_delta(closed, started));
1731 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1732 ktime_us_delta(now, closed));
1734 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1735 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1736 oth, now, alloced, started, closed);
1737 libcfs_debug_dumpstack(NULL);
1741 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1743 ktime_t __closed = ktime_get(); \
1744 ktime_t __alloced = oth->oth_alloced; \
1745 ktime_t __started = oth->oth_started; \
1748 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1751 #else /* OSD_THANDLE_STATS */
1753 #define osd_th_alloced(h) do {} while(0)
1754 #define osd_th_started(h) do {} while(0)
1755 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1757 #endif /* OSD_THANDLE_STATS */
1760 * Concurrency: doesn't access mutable data.
1762 static int osd_param_is_not_sane(const struct osd_device *dev,
1763 const struct thandle *th)
1765 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1767 return oh->ot_credits > osd_transaction_size(dev);
1771 * Concurrency: shouldn't matter.
1773 static void osd_trans_commit_cb(struct super_block *sb,
1774 struct ldiskfs_journal_cb_entry *jcb, int error)
1776 struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1777 struct thandle *th = &oh->ot_super;
1778 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1779 struct osd_device *osd = osd_dev(lud);
1780 struct dt_txn_commit_cb *dcb, *tmp;
1782 LASSERT(oh->ot_handle == NULL);
1785 CERROR("transaction @0x%p commit error: %d\n", th, error);
1787 /* call per-transaction callbacks if any */
1788 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1790 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1791 "commit callback entry: magic=%x name='%s'\n",
1792 dcb->dcb_magic, dcb->dcb_name);
1793 list_del_init(&dcb->dcb_linkage);
1794 dcb->dcb_func(NULL, th, dcb, error);
1797 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1798 if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1799 wake_up(&osd->od_commit_cb_done);
1805 static struct thandle *osd_trans_create(const struct lu_env *env,
1806 struct dt_device *d)
1808 struct osd_thread_info *oti = osd_oti_get(env);
1809 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1810 struct osd_thandle *oh;
1816 CERROR("%s: someone try to start transaction under "
1817 "readonly mode, should be disabled.\n",
1818 osd_name(osd_dt_dev(d)));
1820 RETURN(ERR_PTR(-EROFS));
1823 /* on pending IO in this thread should left from prev. request */
1824 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1826 sb_start_write(osd_sb(osd_dt_dev(d)));
1828 OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1830 sb_end_write(osd_sb(osd_dt_dev(d)));
1831 RETURN(ERR_PTR(-ENOMEM));
1834 oh->ot_quota_trans = &oti->oti_quota_trans;
1835 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1840 oh->oh_declared_ext = 0;
1841 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1842 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1843 INIT_LIST_HEAD(&oh->ot_trunc_locks);
1846 memset(oti->oti_declare_ops, 0,
1847 sizeof(oti->oti_declare_ops));
1848 memset(oti->oti_declare_ops_cred, 0,
1849 sizeof(oti->oti_declare_ops_cred));
1850 memset(oti->oti_declare_ops_used, 0,
1851 sizeof(oti->oti_declare_ops_used));
1853 oti->oti_ins_cache_depth++;
1858 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1860 struct osd_thread_info *oti = osd_oti_get(env);
1861 struct osd_thandle *oh;
1863 oh = container_of(th, struct osd_thandle, ot_super);
1864 LASSERT(oh != NULL);
1866 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1867 oti->oti_declare_ops[OSD_OT_CREATE],
1868 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1869 oti->oti_declare_ops_used[OSD_OT_CREATE],
1870 oti->oti_declare_ops[OSD_OT_DESTROY],
1871 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1872 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1873 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1874 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1875 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1876 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1877 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1878 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1879 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1880 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1881 oti->oti_declare_ops[OSD_OT_WRITE],
1882 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1883 oti->oti_declare_ops_used[OSD_OT_WRITE],
1884 oti->oti_declare_ops[OSD_OT_PUNCH],
1885 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1886 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1887 oti->oti_declare_ops[OSD_OT_QUOTA],
1888 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1889 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1890 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1891 oti->oti_declare_ops[OSD_OT_INSERT],
1892 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1893 oti->oti_declare_ops_used[OSD_OT_INSERT],
1894 oti->oti_declare_ops[OSD_OT_DELETE],
1895 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1896 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1897 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1898 oti->oti_declare_ops[OSD_OT_REF_ADD],
1899 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1900 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1901 oti->oti_declare_ops[OSD_OT_REF_DEL],
1902 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1903 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1907 * Concurrency: shouldn't matter.
1909 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1912 struct osd_thread_info *oti = osd_oti_get(env);
1913 struct osd_device *dev = osd_dt_dev(d);
1915 struct osd_thandle *oh;
1920 LASSERT(current->journal_info == NULL);
1922 oh = container_of(th, struct osd_thandle, ot_super);
1923 LASSERT(oh != NULL);
1924 LASSERT(oh->ot_handle == NULL);
1926 rc = dt_txn_hook_start(env, d, th);
1930 if (unlikely(osd_param_is_not_sane(dev, th))) {
1931 static unsigned long last_printed;
1932 static int last_credits;
1935 * don't make noise on a tiny testing systems
1936 * actual credits misuse will be caught anyway
1938 if (last_credits != oh->ot_credits &&
1939 time_after(jiffies, last_printed +
1940 cfs_time_seconds(60)) &&
1941 osd_transaction_size(dev) > 512) {
1942 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1943 oh->ot_credits, osd_transaction_size(dev));
1944 osd_trans_dump_creds(env, th);
1945 libcfs_debug_dumpstack(NULL);
1946 last_credits = oh->ot_credits;
1947 last_printed = jiffies;
1950 * XXX Limit the credits to 'max_transaction_buffers', and
1951 * let the underlying filesystem to catch the error if
1952 * we really need so many credits.
1954 * This should be removed when we can calculate the
1955 * credits precisely.
1957 oh->ot_credits = osd_transaction_size(dev);
1958 } else if (ldiskfs_track_declares_assert != 0) {
1960 * reserve few credits to prevent an assertion in JBD
1961 * our debugging mechanism will be able to detected
1962 * overuse. this can help to debug single-update
1965 oh->ot_credits += 10;
1966 if (unlikely(osd_param_is_not_sane(dev, th)))
1967 oh->ot_credits = osd_transaction_size(dev);
1970 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
1971 GOTO(out, rc = -EIO);
1974 * we ignore quota checks for system-owned files, but still
1975 * need to count blocks for uid/gid/projid
1977 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, 3);
1980 * XXX temporary stuff. Some abstraction layer should
1983 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1987 LASSERT(oti->oti_txns == 0);
1989 atomic_inc(&dev->od_commit_cb_in_flight);
1990 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
2001 static int osd_seq_exists(const struct lu_env *env,
2002 struct osd_device *osd, u64 seq)
2004 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2005 struct seq_server_site *ss = osd_seq_site(osd);
2010 LASSERT(ss != NULL);
2011 LASSERT(ss->ss_server_fld != NULL);
2013 rc = osd_fld_lookup(env, osd, seq, range);
2016 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2017 osd_name(osd), seq, rc);
2021 RETURN(ss->ss_node_id == range->lsr_index);
2024 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2026 struct dt_txn_commit_cb *dcb;
2027 struct dt_txn_commit_cb *tmp;
2029 /* call per-transaction stop callbacks if any */
2030 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2032 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2033 "commit callback entry: magic=%x name='%s'\n",
2034 dcb->dcb_magic, dcb->dcb_name);
2035 list_del_init(&dcb->dcb_linkage);
2036 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2041 * Concurrency: shouldn't matter.
2043 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2046 struct osd_thread_info *oti = osd_oti_get(env);
2047 struct osd_thandle *oh;
2048 struct osd_iobuf *iobuf = &oti->oti_iobuf;
2049 struct osd_device *osd = osd_dt_dev(th->th_dev);
2050 struct qsd_instance *qsd = osd_def_qsd(osd);
2051 struct lquota_trans *qtrans;
2052 LIST_HEAD(truncates);
2053 int rc = 0, remove_agents = 0;
2057 oh = container_of(th, struct osd_thandle, ot_super);
2059 remove_agents = oh->ot_remove_agents;
2061 qtrans = oh->ot_quota_trans;
2062 oh->ot_quota_trans = NULL;
2064 /* move locks to local list, stop tx, execute truncates */
2065 list_splice(&oh->ot_trunc_locks, &truncates);
2067 if (oh->ot_handle != NULL) {
2070 handle_t *hdl = oh->ot_handle;
2073 * add commit callback
2074 * notice we don't do this in osd_trans_start()
2075 * as underlying transaction can change during truncate
2077 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2080 LASSERT(oti->oti_txns == 1);
2083 rc = dt_txn_hook_stop(env, th);
2085 CERROR("%s: failed in transaction hook: rc = %d\n",
2088 osd_trans_stop_cb(oh, rc);
2089 /* hook functions might modify th_sync */
2090 hdl->h_sync = th->th_sync;
2092 oh->ot_handle = NULL;
2093 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2095 CERROR("%s: failed to stop transaction: rc = %d\n",
2096 osd_name(osd), rc2);
2100 /* We preserve the origin behavior of ignoring any
2101 * failures with the underlying punch / truncate
2102 * operation. We do record for debugging if an error
2103 * does occur in the lctl dk logs.
2105 rc2 = osd_process_truncates(env, &truncates);
2107 CERROR("%s: failed truncate process: rc = %d\n",
2108 osd_name(osd), rc2);
2110 osd_trans_stop_cb(oh, th->th_result);
2114 osd_trunc_unlock_all(env, &truncates);
2116 /* inform the quota slave device that the transaction is stopping */
2117 qsd_op_end(env, qsd, qtrans);
2120 * as we want IO to journal and data IO be concurrent, we don't block
2121 * awaiting data IO completion in osd_do_bio(), instead we wait here
2122 * once transaction is submitted to the journal. all reqular requests
2123 * don't do direct IO (except read/write), thus this wait_event becomes
2126 * IMPORTANT: we have to wait till any IO submited by the thread is
2127 * completed otherwise iobuf may be corrupted by different request
2129 wait_event(iobuf->dr_wait,
2130 atomic_read(&iobuf->dr_numreqs) == 0);
2131 osd_fini_iobuf(osd, iobuf);
2133 rc = iobuf->dr_error;
2135 if (unlikely(remove_agents != 0))
2136 osd_process_scheduled_agent_removals(env, osd);
2138 LASSERT(oti->oti_ins_cache_depth > 0);
2139 oti->oti_ins_cache_depth--;
2140 /* reset OI cache for safety */
2141 if (oti->oti_ins_cache_depth == 0)
2142 oti->oti_ins_cache_used = 0;
2144 sb_end_write(osd_sb(osd));
2149 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2151 struct osd_thandle *oh = container_of(th, struct osd_thandle,
2154 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2155 LASSERT(&dcb->dcb_func != NULL);
2156 if (dcb->dcb_flags & DCB_TRANS_STOP)
2157 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2159 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2165 * Called just before object is freed. Releases all resources except for
2166 * object itself (that is released by osd_object_free()).
2168 * Concurrency: no concurrent access is possible that late in object
2171 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2173 struct osd_object *obj = osd_obj(l);
2174 struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2175 struct inode *inode = obj->oo_inode;
2180 LINVRNT(osd_invariant(obj));
2183 * If object is unlinked remove fid->ino mapping from object index.
2186 osd_index_fini(obj);
2191 if (osd_has_index(obj) && obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2192 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2194 uid = i_uid_read(inode);
2195 gid = i_gid_read(inode);
2196 projid = i_projid_read(inode);
2198 obj->oo_inode = NULL;
2201 /* do not rebalance quota if the caller needs to release memory
2202 * otherwise qsd_refresh_usage() may went into a new ldiskfs
2203 * transaction and risk to deadlock - LU-12178 */
2204 if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2207 if (!obj->oo_header && qsd) {
2208 struct osd_thread_info *info = osd_oti_get(env);
2209 struct lquota_id_info *qi = &info->oti_qi;
2211 /* Release granted quota to master if necessary */
2212 qi->lqi_id.qid_uid = uid;
2213 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2215 qi->lqi_id.qid_uid = gid;
2216 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2218 qi->lqi_id.qid_uid = projid;
2219 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2224 * Concurrency: ->loo_object_release() is called under site spin-lock.
2226 static void osd_object_release(const struct lu_env *env,
2227 struct lu_object *l)
2229 struct osd_object *o = osd_obj(l);
2232 * nobody should be releasing a non-destroyed object with nlink=0
2233 * the API allows this, but ldiskfs doesn't like and then report
2234 * this inode as deleted
2236 LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2237 o->oo_inode->i_nlink == 0));
2241 * Concurrency: shouldn't matter.
2243 static int osd_object_print(const struct lu_env *env, void *cookie,
2244 lu_printer_t p, const struct lu_object *l)
2246 struct osd_object *o = osd_obj(l);
2247 struct iam_descr *d;
2249 if (o->oo_dir != NULL)
2250 d = o->oo_dir->od_container.ic_descr;
2253 return (*p)(env, cookie,
2254 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2256 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2257 o->oo_inode ? o->oo_inode->i_generation : 0,
2258 d ? d->id_ops->id_name : "plain");
2262 * Concurrency: shouldn't matter.
2264 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2265 struct obd_statfs *sfs, struct obd_statfs_info *info)
2267 struct osd_device *osd = osd_dt_dev(d);
2268 struct super_block *sb = osd_sb(osd);
2269 struct kstatfs *ksfs;
2273 if (unlikely(osd->od_mnt == NULL))
2274 return -EINPROGRESS;
2276 /* osd_lproc.c call this without env, allocate ksfs for that case */
2277 if (unlikely(env == NULL)) {
2278 OBD_ALLOC_PTR(ksfs);
2282 ksfs = &osd_oti_get(env)->oti_ksfs;
2285 result = sb->s_op->statfs(sb->s_root, ksfs);
2289 statfs_pack(sfs, ksfs);
2290 if (unlikely(sb->s_flags & SB_RDONLY))
2291 sfs->os_state |= OS_STATFS_READONLY;
2293 sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2295 if (ldiskfs_has_feature_extents(sb))
2296 sfs->os_maxbytes = sb->s_maxbytes;
2298 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2301 * Reserve some space so to avoid fragmenting the filesystem too much.
2302 * Fragmentation not only impacts performance, but can also increase
2303 * metadata overhead significantly, causing grant calculation to be
2306 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2308 BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2309 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2310 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2311 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2313 sfs->os_blocks -= reserved;
2314 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2315 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2318 if (unlikely(env == NULL))
2324 * Estimate space needed for file creations. We assume the largest filename
2325 * which is 2^64 - 1, hence a filename of 20 chars.
2326 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2328 #ifdef __LDISKFS_DIR_REC_LEN
2329 # define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2330 #elif defined LDISKFS_DIR_REC_LEN_WITH_DIR
2331 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20, NULL)
2333 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2337 * Concurrency: doesn't access mutable data.
2339 static void osd_conf_get(const struct lu_env *env,
2340 const struct dt_device *dev,
2341 struct dt_device_param *param)
2343 struct osd_device *d = osd_dt_dev(dev);
2344 struct super_block *sb = osd_sb(d);
2345 struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2350 * XXX should be taken from not-yet-existing fs abstraction layer.
2352 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2353 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2354 param->ddp_symlink_max = sb->s_blocksize;
2355 param->ddp_mount_type = LDD_MT_LDISKFS;
2356 if (ldiskfs_has_feature_extents(sb))
2357 param->ddp_maxbytes = sb->s_maxbytes;
2359 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2361 * inode are statically allocated, so per-inode space consumption
2362 * is the space consumed by the directory entry
2364 param->ddp_inodespace = PER_OBJ_USAGE;
2366 * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2367 * is 128MB) which is unlikely to be hit in real life. Report a smaller
2368 * maximum length to not under-count the actual number of extents
2369 * needed for writing a file if there are sub-optimal block allocations.
2371 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2372 /* worst-case extent insertion metadata overhead */
2373 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2374 param->ddp_mntopts = 0;
2375 if (test_opt(sb, XATTR_USER))
2376 param->ddp_mntopts |= MNTOPT_USERXATTR;
2377 if (test_opt(sb, POSIX_ACL))
2378 param->ddp_mntopts |= MNTOPT_ACL;
2381 * LOD might calculate the max stripe count based on max_ea_size,
2382 * so we need take account in the overhead as well,
2383 * xattr_header + magic + xattr_entry_head
2385 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2386 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2388 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2389 if (ldiskfs_has_feature_ea_inode(sb))
2390 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2394 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2396 if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2397 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2400 * Preferred RPC size for efficient disk IO. 4MB shows good
2401 * all-around performance for ldiskfs, but use bigalloc chunk size
2402 * by default if larger.
2404 #if defined(LDISKFS_CLUSTER_SIZE)
2405 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2406 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2409 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2411 param->ddp_t10_cksum_type = 0;
2413 unsigned short interval = blk_integrity_interval(bi);
2414 name = blk_integrity_name(bi);
2424 if (strncmp(name, "T10-DIF-TYPE",
2425 sizeof("T10-DIF-TYPE") - 1) == 0) {
2426 /* also skip "1/2/3-" at end */
2427 const int type_off = sizeof("T10-DIF-TYPE.");
2428 char type_number = name[type_off - 2];
2430 if (interval != 512 && interval != 4096) {
2431 CERROR("%s: unsupported T10PI sector size %u\n",
2432 d->od_svname, interval);
2435 switch (type_number) {
2437 d->od_t10_type = OSD_T10_TYPE1;
2440 d->od_t10_type = OSD_T10_TYPE2;
2443 d->od_t10_type = OSD_T10_TYPE3;
2446 CERROR("%s: unsupported T10PI type %s\n",
2447 d->od_svname, name);
2450 if (strcmp(name + type_off, "CRC") == 0) {
2451 d->od_t10_type |= OSD_T10_TYPE_CRC;
2452 param->ddp_t10_cksum_type = interval == 512 ?
2453 OBD_CKSUM_T10CRC512 :
2455 } else if (strcmp(name + type_off, "IP") == 0) {
2456 d->od_t10_type |= OSD_T10_TYPE_IP;
2457 param->ddp_t10_cksum_type = interval == 512 ?
2458 OBD_CKSUM_T10IP512 :
2461 CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
2462 d->od_svname, name);
2467 CERROR("%s: unsupported T10PI type '%s'\n",
2468 d->od_svname, name);
2473 param->ddp_has_lseek_data_hole = true;
2476 static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
2478 return osd_sb(osd_dt_dev(d));
2482 * Concurrency: shouldn't matter.
2484 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2487 struct super_block *s = osd_sb(osd_dt_dev(d));
2490 down_read(&s->s_umount);
2491 rc = s->s_op->sync_fs(s, 1);
2492 up_read(&s->s_umount);
2494 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2501 * Start commit for OSD device.
2503 * An implementation of dt_commit_async method for OSD device.
2504 * Asychronously starts underlayng fs sync and thereby a transaction
2507 * \param env environment
2508 * \param d dt device
2510 * \see dt_device_operations
2512 static int osd_commit_async(const struct lu_env *env,
2513 struct dt_device *d)
2515 struct super_block *s = osd_sb(osd_dt_dev(d));
2520 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2521 down_read(&s->s_umount);
2522 rc = s->s_op->sync_fs(s, 0);
2523 up_read(&s->s_umount);
2529 * Concurrency: shouldn't matter.
2531 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2533 struct super_block *sb = osd_sb(osd_dt_dev(d));
2534 struct block_device *dev = sb->s_bdev;
2535 int rc = -EOPNOTSUPP;
2539 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2540 osd_dt_dev(d)->od_svname, (long)dev, rc);
2546 * Note: we do not count into QUOTA here.
2547 * If we mount with --data_journal we may need more.
2549 const int osd_dto_credits_noquota[DTO_NR] = {
2552 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2553 * SINGLEDATA_TRANS_BLOCKS(8)
2554 * XXX Note: maybe iam need more, since iam have more level than
2557 [DTO_INDEX_INSERT] = 16,
2560 * just modify a single entry, probably merge few within a block
2562 [DTO_INDEX_DELETE] = 1,
2566 [DTO_INDEX_UPDATE] = 16,
2568 * 4(inode, inode bits, groups, GDT)
2569 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2571 [DTO_OBJECT_CREATE] = 4,
2573 * 4(inode, inode bits, groups, GDT)
2574 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2576 [DTO_OBJECT_DELETE] = 4,
2578 * Attr set credits (inode)
2580 [DTO_ATTR_SET_BASE] = 1,
2582 * Xattr set. The same as xattr of EXT3.
2583 * DATA_TRANS_BLOCKS(14)
2584 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2585 * are also counted in. Do not know why?
2587 [DTO_XATTR_SET] = 14,
2589 * credits for inode change during write.
2591 [DTO_WRITE_BASE] = 3,
2593 * credits for single block write.
2595 [DTO_WRITE_BLOCK] = 14,
2597 * Attr set credits for chown.
2598 * This is extra credits for setattr, and it is null without quota
2600 [DTO_ATTR_SET_CHOWN] = 0
2603 /* reserve or free quota for some operation */
2604 static int osd_reserve_or_free_quota(const struct lu_env *env,
2605 struct dt_device *dev,
2606 struct lquota_id_info *qi)
2608 struct osd_device *osd = osd_dt_dev(dev);
2609 struct qsd_instance *qsd = NULL;
2615 qsd = osd->od_quota_slave_dt;
2617 qsd = osd->od_quota_slave_md;
2619 rc = qsd_reserve_or_free_quota(env, qsd, qi);
2623 static const struct dt_device_operations osd_dt_ops = {
2624 .dt_root_get = osd_root_get,
2625 .dt_statfs = osd_statfs,
2626 .dt_trans_create = osd_trans_create,
2627 .dt_trans_start = osd_trans_start,
2628 .dt_trans_stop = osd_trans_stop,
2629 .dt_trans_cb_add = osd_trans_cb_add,
2630 .dt_conf_get = osd_conf_get,
2631 .dt_mnt_sb_get = osd_mnt_sb_get,
2632 .dt_sync = osd_sync,
2634 .dt_commit_async = osd_commit_async,
2635 .dt_reserve_or_free_quota = osd_reserve_or_free_quota,
2638 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2641 struct osd_object *obj = osd_dt_obj(dt);
2642 struct osd_thread_info *oti = osd_oti_get(env);
2644 LINVRNT(osd_invariant(obj));
2646 LASSERT(obj->oo_owner != env);
2647 down_read_nested(&obj->oo_sem, role);
2649 LASSERT(obj->oo_owner == NULL);
2653 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2656 struct osd_object *obj = osd_dt_obj(dt);
2657 struct osd_thread_info *oti = osd_oti_get(env);
2659 LINVRNT(osd_invariant(obj));
2661 LASSERT(obj->oo_owner != env);
2662 down_write_nested(&obj->oo_sem, role);
2664 LASSERT(obj->oo_owner == NULL);
2665 obj->oo_owner = env;
2669 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2671 struct osd_object *obj = osd_dt_obj(dt);
2672 struct osd_thread_info *oti = osd_oti_get(env);
2674 LINVRNT(osd_invariant(obj));
2676 LASSERT(oti->oti_r_locks > 0);
2678 up_read(&obj->oo_sem);
2681 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2683 struct osd_object *obj = osd_dt_obj(dt);
2684 struct osd_thread_info *oti = osd_oti_get(env);
2686 LINVRNT(osd_invariant(obj));
2688 LASSERT(obj->oo_owner == env);
2689 LASSERT(oti->oti_w_locks > 0);
2691 obj->oo_owner = NULL;
2692 up_write(&obj->oo_sem);
2695 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2697 struct osd_object *obj = osd_dt_obj(dt);
2699 LINVRNT(osd_invariant(obj));
2701 return obj->oo_owner == env;
2704 static void osd_inode_getattr(const struct lu_env *env,
2705 struct inode *inode, struct lu_attr *attr)
2707 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2708 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2709 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2710 LA_BLKSIZE | LA_TYPE | LA_BTIME;
2712 attr->la_atime = inode->i_atime.tv_sec;
2713 attr->la_mtime = inode->i_mtime.tv_sec;
2714 attr->la_ctime = inode->i_ctime.tv_sec;
2715 attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2716 attr->la_mode = inode->i_mode;
2717 attr->la_size = i_size_read(inode);
2718 attr->la_blocks = inode->i_blocks;
2719 attr->la_uid = i_uid_read(inode);
2720 attr->la_gid = i_gid_read(inode);
2721 attr->la_projid = i_projid_read(inode);
2722 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2723 attr->la_nlink = inode->i_nlink;
2724 attr->la_rdev = inode->i_rdev;
2725 attr->la_blksize = 1 << inode->i_blkbits;
2726 attr->la_blkbits = inode->i_blkbits;
2728 * Ext4 did not transfer inherit flags from raw inode
2729 * to inode flags, and ext4 internally test raw inode
2730 * @i_flags directly. Instead of patching ext4, we do it here.
2732 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2733 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2736 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2739 struct osd_object *obj = osd_dt_obj(dt);
2740 const struct dt_it_ops *iops;
2746 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2747 LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2749 if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2750 *count = obj->oo_dirent_count;
2754 /* directory not initialized yet */
2755 if (!dt->do_index_ops) {
2760 iops = &dt->do_index_ops->dio_it;
2761 it = iops->init(env, dt, LUDA_64BITHASH);
2763 RETURN(PTR_ERR(it));
2765 rc = iops->load(env, it, 0);
2767 if (rc == -ENODATA) {
2774 rc = iops->next(env, it);
2776 for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2780 if (iops->key_size(env, it) == 0)
2786 obj->oo_dirent_count = *count;
2791 iops->fini(env, it);
2796 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2797 struct lu_attr *attr)
2799 struct osd_object *obj = osd_dt_obj(dt);
2802 if (unlikely(!dt_object_exists(dt)))
2804 if (unlikely(obj->oo_destroyed))
2807 LASSERT(!dt_object_remote(dt));
2808 LINVRNT(osd_invariant(obj));
2810 spin_lock(&obj->oo_guard);
2811 osd_inode_getattr(env, obj->oo_inode, attr);
2812 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2813 attr->la_valid |= LA_FLAGS;
2814 attr->la_flags |= LUSTRE_ORPHAN_FL;
2816 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2817 attr->la_valid |= LA_FLAGS;
2818 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2820 spin_unlock(&obj->oo_guard);
2822 if (S_ISDIR(obj->oo_inode->i_mode) &&
2823 fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2824 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2829 static int osd_declare_attr_qid(const struct lu_env *env,
2830 struct osd_object *obj,
2831 struct osd_thandle *oh, long long bspace,
2832 qid_t old_id, qid_t new_id, bool enforce,
2836 struct osd_thread_info *info = osd_oti_get(env);
2837 struct lquota_id_info *qi = &info->oti_qi;
2839 qi->lqi_type = type;
2840 /* inode accounting */
2841 qi->lqi_is_blk = false;
2843 /* one more inode for the new id ... */
2844 qi->lqi_id.qid_uid = new_id;
2846 /* Reserve credits for the new id */
2847 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2848 if (rc == -EDQUOT || rc == -EINPROGRESS)
2853 /* and one less inode for the current id */
2854 qi->lqi_id.qid_uid = old_id;
2856 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2857 if (rc == -EDQUOT || rc == -EINPROGRESS)
2862 /* block accounting */
2863 qi->lqi_is_blk = true;
2865 /* more blocks for the new id ... */
2866 qi->lqi_id.qid_uid = new_id;
2867 qi->lqi_space = bspace;
2869 * Credits for the new uid has been reserved, re-use "obj"
2870 * to save credit reservation.
2872 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2873 if (rc == -EDQUOT || rc == -EINPROGRESS)
2878 /* and finally less blocks for the current uid */
2879 qi->lqi_id.qid_uid = old_id;
2880 qi->lqi_space = -bspace;
2881 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2882 if (rc == -EDQUOT || rc == -EINPROGRESS)
2888 static int osd_declare_attr_set(const struct lu_env *env,
2889 struct dt_object *dt,
2890 const struct lu_attr *attr,
2891 struct thandle *handle)
2893 struct osd_thandle *oh;
2894 struct osd_object *obj;
2903 LASSERT(dt != NULL);
2904 LASSERT(handle != NULL);
2906 obj = osd_dt_obj(dt);
2907 LASSERT(osd_invariant(obj));
2909 oh = container_of(handle, struct osd_thandle, ot_super);
2910 LASSERT(oh->ot_handle == NULL);
2912 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2913 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2915 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2916 osd_dto_credits_noquota[DTO_XATTR_SET]);
2918 if (attr == NULL || obj->oo_inode == NULL)
2921 bspace = obj->oo_inode->i_blocks << 9;
2922 bspace = toqb(bspace);
2925 * Changing ownership is always preformed by super user, it should not
2926 * fail with EDQUOT unless required explicitly.
2928 * We still need to call the osd_declare_qid() to calculate the journal
2929 * credits for updating quota accounting files and to trigger quota
2930 * space adjustment once the operation is completed.
2932 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2934 uid = i_uid_read(obj->oo_inode);
2935 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2936 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2937 attr->la_uid, enforce, USRQUOTA);
2941 gid = i_gid_read(obj->oo_inode);
2942 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
2943 attr->la_uid, gid, attr->la_gid);
2944 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2945 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
2946 attr->la_gid, enforce, GRPQUOTA);
2951 #ifdef HAVE_PROJECT_QUOTA
2952 if (attr->la_valid & LA_PROJID) {
2953 __u32 projid = i_projid_read(obj->oo_inode);
2955 enforce = (attr->la_valid & LA_PROJID) &&
2956 (attr->la_projid != projid);
2957 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2958 (qid_t)projid, (qid_t)attr->la_projid,
2964 /* punch must be aware we are dealing with an encrypted file */
2965 if (attr->la_valid & LA_FLAGS && attr->la_flags & LUSTRE_ENCRYPT_FL)
2966 obj->oo_lma_flags |= LUSTRE_ENCRYPT_FL;
2971 static int osd_inode_setattr(const struct lu_env *env,
2972 struct inode *inode, const struct lu_attr *attr)
2974 __u64 bits = attr->la_valid;
2976 /* Only allow set size for regular file */
2977 if (!S_ISREG(inode->i_mode))
2978 bits &= ~(LA_SIZE | LA_BLOCKS);
2983 if (bits & LA_ATIME)
2984 inode->i_atime = osd_inode_time(inode, attr->la_atime);
2985 if (bits & LA_CTIME)
2986 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
2987 if (bits & LA_MTIME)
2988 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
2989 if (bits & LA_SIZE) {
2990 spin_lock(&inode->i_lock);
2991 LDISKFS_I(inode)->i_disksize = attr->la_size;
2992 i_size_write(inode, attr->la_size);
2993 spin_unlock(&inode->i_lock);
2997 * OSD should not change "i_blocks" which is used by quota.
2998 * "i_blocks" should be changed by ldiskfs only.
3001 inode->i_mode = (inode->i_mode & S_IFMT) |
3002 (attr->la_mode & ~S_IFMT);
3004 i_uid_write(inode, attr->la_uid);
3006 i_gid_write(inode, attr->la_gid);
3007 if (bits & LA_PROJID)
3008 i_projid_write(inode, attr->la_projid);
3009 if (bits & LA_NLINK)
3010 set_nlink(inode, attr->la_nlink);
3012 inode->i_rdev = attr->la_rdev;
3014 if (bits & LA_FLAGS) {
3015 /* always keep S_NOCMTIME */
3016 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
3018 #if defined(S_ENCRYPTED)
3019 /* Always remove S_ENCRYPTED, because ldiskfs must not be
3020 * aware of encryption status. It is just stored into LMA
3021 * so that it can be forwared to client side.
3023 inode->i_flags &= ~S_ENCRYPTED;
3026 * Ext4 did not transfer inherit flags from
3027 * @inode->i_flags to raw inode i_flags when writing
3028 * flags, we do it explictly here.
3030 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
3031 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
3033 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
3038 #ifdef HAVE_PROJECT_QUOTA
3039 static int osd_transfer_project(struct inode *inode, __u32 projid,
3040 struct thandle *handle)
3042 struct super_block *sb = inode->i_sb;
3043 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3046 struct ldiskfs_iloc iloc;
3047 struct ldiskfs_inode *raw_inode;
3048 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3050 if (!ldiskfs_has_feature_project(sb)) {
3051 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3052 == LDISKFS_DEF_PROJID);
3053 if (projid != LDISKFS_DEF_PROJID)
3059 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3062 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3063 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3066 err = ldiskfs_get_inode_loc(inode, &iloc);
3070 raw_inode = ldiskfs_raw_inode(&iloc);
3071 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3072 struct osd_thandle *oh = container_of(handle,
3076 * try to expand inode size automatically.
3078 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3079 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3087 dquot_initialize(inode);
3088 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3089 if (transfer_to[PRJQUOTA]) {
3090 lock_dquot_transfer(inode);
3091 err = __dquot_transfer(inode, transfer_to);
3092 unlock_dquot_transfer(inode);
3093 dqput(transfer_to[PRJQUOTA]);
3102 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3103 struct thandle *handle)
3107 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3108 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3112 "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3113 inode->i_ino, i_uid_read(inode), attr->la_uid,
3114 i_gid_read(inode), attr->la_gid);
3116 dquot_initialize(inode);
3118 if (attr->la_valid & LA_UID)
3119 iattr.ia_valid |= ATTR_UID;
3120 if (attr->la_valid & LA_GID)
3121 iattr.ia_valid |= ATTR_GID;
3122 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3123 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3125 lock_dquot_transfer(inode);
3126 rc = dquot_transfer(inode, &iattr);
3127 unlock_dquot_transfer(inode);
3129 CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3130 osd_ino2name(inode), rc);
3135 /* Handle project id transfer here properly */
3136 if (attr->la_valid & LA_PROJID &&
3137 attr->la_projid != i_projid_read(inode)) {
3138 if (!projid_valid(make_kprojid(&init_user_ns, attr->la_projid)))
3140 #ifdef HAVE_PROJECT_QUOTA
3141 rc = osd_transfer_project(inode, attr->la_projid, handle);
3146 CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3147 osd_ino2name(inode), rc);
3154 static int osd_attr_set(const struct lu_env *env,
3155 struct dt_object *dt,
3156 const struct lu_attr *attr,
3157 struct thandle *handle)
3159 struct osd_object *obj = osd_dt_obj(dt);
3160 struct inode *inode;
3163 if (!dt_object_exists(dt))
3166 LASSERT(handle != NULL);
3167 LASSERT(!dt_object_remote(dt));
3168 LASSERT(osd_invariant(obj));
3170 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3172 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3173 !osd_obj2dev(obj)->od_is_ost) {
3174 struct osd_thread_info *oti = osd_oti_get(env);
3175 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3176 struct lu_fid *fid1 = &oti->oti_fid;
3177 struct osd_inode_id *id = &oti->oti_id;
3178 struct iam_path_descr *ipd;
3179 struct iam_container *bag;
3180 struct osd_thandle *oh;
3183 fid_cpu_to_be(fid1, fid0);
3184 memset(id, 1, sizeof(*id));
3185 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3186 fid0)->oi_dir.od_container;
3187 ipd = osd_idx_ipd_get(env, bag);
3188 if (unlikely(ipd == NULL))
3191 oh = container_of(handle, struct osd_thandle, ot_super);
3192 rc = iam_update(oh->ot_handle, bag,
3193 (const struct iam_key *)fid1,
3194 (const struct iam_rec *)id, ipd);
3195 osd_ipd_put(env, bag, ipd);
3196 return(rc > 0 ? 0 : rc);
3199 inode = obj->oo_inode;
3201 rc = osd_quota_transfer(inode, attr, handle);
3205 spin_lock(&obj->oo_guard);
3206 rc = osd_inode_setattr(env, inode, attr);
3207 spin_unlock(&obj->oo_guard);
3211 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3213 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
3215 if (!(attr->la_valid & LA_FLAGS))
3218 /* Let's check if there are extra flags need to be set into LMA */
3219 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
3220 struct osd_thread_info *info = osd_oti_get(env);
3221 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
3223 LASSERT(!obj->oo_pfid_in_lma);
3225 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
3226 &info->oti_ost_attrs);
3230 lma->lma_incompat |=
3231 lustre_to_lma_flags(attr->la_flags);
3232 lustre_lma_swab(lma);
3234 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3236 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
3237 lma, sizeof(*lma), XATTR_REPLACE);
3239 struct osd_device *osd = osd_obj2dev(obj);
3241 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
3242 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3243 lma->lma_incompat, rc);
3246 attr->la_flags & LUSTRE_LMA_FL_MASKS;
3248 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3255 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
3256 struct osd_object *obj,
3257 const char *name, const int namelen)
3259 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
3262 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
3263 umode_t mode, struct dt_allocation_hint *hint,
3264 struct thandle *th, struct lu_attr *attr)
3267 struct osd_device *osd = osd_obj2dev(obj);
3268 struct osd_thandle *oth;
3269 struct dt_object *parent = NULL;
3270 struct inode *inode;
3271 struct iattr iattr = {
3272 .ia_valid = ATTR_UID | ATTR_GID |
3273 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
3274 .ia_ctime.tv_sec = attr->la_ctime,
3275 .ia_mtime.tv_sec = attr->la_mtime,
3276 .ia_atime.tv_sec = attr->la_atime,
3277 .ia_uid = GLOBAL_ROOT_UID,
3278 .ia_gid = GLOBAL_ROOT_GID,
3280 const struct osd_timespec omit = { .tv_nsec = UTIME_OMIT };
3282 if (attr->la_valid & LA_UID)
3283 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3284 if (attr->la_valid & LA_GID)
3285 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3287 LINVRNT(osd_invariant(obj));
3288 LASSERT(obj->oo_inode == NULL);
3289 LASSERT(obj->oo_hl_head == NULL);
3291 if (S_ISDIR(mode) && ldiskfs_pdo) {
3293 ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
3294 if (obj->oo_hl_head == NULL)
3298 oth = container_of(th, struct osd_thandle, ot_super);
3299 LASSERT(oth->ot_handle->h_transaction != NULL);
3301 if (hint != NULL && hint->dah_parent != NULL &&
3302 !dt_object_remote(hint->dah_parent))
3303 parent = hint->dah_parent;
3305 /* if a time component is not valid set it to UTIME_OMIT */
3306 if (!(attr->la_valid & LA_CTIME))
3307 iattr.ia_ctime = omit;
3308 if (!(attr->la_valid & LA_MTIME))
3309 iattr.ia_mtime = omit;
3310 if (!(attr->la_valid & LA_ATIME))
3311 iattr.ia_atime = omit;
3313 inode = ldiskfs_create_inode(oth->ot_handle,
3314 parent ? osd_dt_obj(parent)->oo_inode :
3315 osd_sb(osd)->s_root->d_inode,
3317 if (!IS_ERR(inode)) {
3318 /* Do not update file c/mtime in ldiskfs. */
3319 inode->i_flags |= S_NOCMTIME;
3322 * For new created object, it must be consistent,
3323 * and it is unnecessary to scrub against it.
3325 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
3327 obj->oo_inode = inode;
3330 if (obj->oo_hl_head != NULL) {
3331 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
3332 obj->oo_hl_head = NULL;
3334 result = PTR_ERR(inode);
3336 LINVRNT(osd_invariant(obj));
3344 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
3345 struct lu_attr *attr,
3346 struct dt_allocation_hint *hint,
3347 struct dt_object_format *dof,
3351 struct osd_thandle *oth;
3352 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
3354 LASSERT(S_ISDIR(attr->la_mode));
3356 oth = container_of(th, struct osd_thandle, ot_super);
3357 LASSERT(oth->ot_handle->h_transaction != NULL);
3358 if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
3359 obj->oo_dirent_count = 0;
3360 result = osd_mkfile(info, obj, mode, hint, th, attr);
3365 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
3366 struct lu_attr *attr,
3367 struct dt_allocation_hint *hint,
3368 struct dt_object_format *dof,
3372 struct osd_thandle *oth;
3373 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
3375 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
3377 LASSERT(S_ISREG(attr->la_mode));
3379 oth = container_of(th, struct osd_thandle, ot_super);
3380 LASSERT(oth->ot_handle->h_transaction != NULL);
3382 result = osd_mkfile(info, obj, mode, hint, th, attr);
3384 LASSERT(obj->oo_inode != NULL);
3385 if (feat->dif_flags & DT_IND_VARKEY)
3386 result = iam_lvar_create(obj->oo_inode,
3387 feat->dif_keysize_max,
3389 feat->dif_recsize_max,
3392 result = iam_lfix_create(obj->oo_inode,
3393 feat->dif_keysize_max,
3395 feat->dif_recsize_max,
3401 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3402 struct lu_attr *attr,
3403 struct dt_allocation_hint *hint,
3404 struct dt_object_format *dof,
3407 LASSERT(S_ISREG(attr->la_mode));
3408 return osd_mkfile(info, obj, (attr->la_mode &
3409 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3413 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3414 struct lu_attr *attr,
3415 struct dt_allocation_hint *hint,
3416 struct dt_object_format *dof,
3419 LASSERT(S_ISLNK(attr->la_mode));
3420 return osd_mkfile(info, obj, (attr->la_mode &
3421 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3425 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3426 struct lu_attr *attr,
3427 struct dt_allocation_hint *hint,
3428 struct dt_object_format *dof,
3431 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3434 LINVRNT(osd_invariant(obj));
3435 LASSERT(obj->oo_inode == NULL);
3436 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3437 S_ISFIFO(mode) || S_ISSOCK(mode));
3439 result = osd_mkfile(info, obj, mode, hint, th, attr);
3441 LASSERT(obj->oo_inode != NULL);
3443 * This inode should be marked dirty for i_rdev. Currently
3444 * that is done in the osd_attr_init().
3446 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3449 LINVRNT(osd_invariant(obj));
3453 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3455 struct dt_allocation_hint *hint,
3456 struct dt_object_format *dof,
3459 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3461 osd_obj_type_f result;
3477 result = osd_mk_index;
3487 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3488 struct dt_object *parent, struct dt_object *child,
3493 ah->dah_parent = parent;
3495 if (parent != NULL && !dt_object_remote(parent)) {
3496 /* will help to find FID->ino at dt_insert("..") */
3497 struct osd_object *pobj = osd_dt_obj(parent);
3499 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3503 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3504 struct lu_attr *attr, struct dt_object_format *dof,
3505 struct thandle *handle)
3507 struct inode *inode = obj->oo_inode;
3508 __u64 valid = attr->la_valid;
3511 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3513 if (dof->dof_type != DFT_NODE)
3514 attr->la_valid &= ~LA_RDEV;
3515 if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
3516 attr->la_valid &= ~LA_ATIME;
3517 if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
3518 attr->la_valid &= ~LA_CTIME;
3519 if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
3520 attr->la_valid &= ~LA_MTIME;
3522 result = osd_quota_transfer(inode, attr, handle);
3526 if (attr->la_valid != 0) {
3527 result = osd_inode_setattr(info->oti_env, inode, attr);
3529 * The osd_inode_setattr() should always succeed here. The
3530 * only error that could be returned is EDQUOT when we are
3531 * trying to change the UID or GID of the inode. However, this
3532 * should not happen since quota enforcement is no longer
3533 * enabled on ldiskfs (lquota takes care of it).
3535 LASSERTF(result == 0, "%d\n", result);
3536 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3539 attr->la_valid = valid;
3543 * Helper function for osd_create()
3545 * \retval 0, on success
3547 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3548 struct lu_attr *attr, struct dt_allocation_hint *hint,
3549 struct dt_object_format *dof, struct thandle *th)
3554 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3556 /* we drop umask so that permissions we pass are not affected */
3557 umask = current->fs->umask;
3558 current->fs->umask = 0;
3560 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3562 if (likely(obj->oo_inode != NULL)) {
3563 LASSERT(obj->oo_inode->i_state & I_NEW);
3566 * Unlock the inode before attr initialization to avoid
3567 * unnecessary dqget operations. LU-6378
3569 unlock_new_inode(obj->oo_inode);
3572 if (likely(result == 0)) {
3573 osd_attr_init(info, obj, attr, dof, th);
3574 osd_object_init0(obj);
3577 /* restore previous umask value */
3578 current->fs->umask = umask;
3580 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3586 * Helper function for osd_create()
3588 * \retval 0, on success
3590 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3591 const struct lu_fid *fid, struct thandle *th)
3593 struct osd_thread_info *info = osd_oti_get(env);
3594 struct osd_inode_id *id = &info->oti_id;
3595 struct osd_device *osd = osd_obj2dev(obj);
3596 struct osd_thandle *oh;
3599 LASSERT(obj->oo_inode != NULL);
3601 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OI_ENOSPC))
3604 oh = container_of(th, struct osd_thandle, ot_super);
3605 LASSERT(oh->ot_handle);
3606 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3608 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3609 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3610 OI_CHECK_FLD, NULL);
3611 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
3612 struct lu_fid next_fid = *fid;
3614 /* insert next object in advance, and map to the same inode */
3616 if (next_fid.f_oid != 0) {
3617 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3618 osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
3619 OI_CHECK_FLD, NULL);
3620 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3624 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3629 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3630 u64 seq, struct lu_seq_range *range)
3632 struct seq_server_site *ss = osd_seq_site(osd);
3634 if (fid_seq_is_idif(seq)) {
3635 fld_range_set_ost(range);
3636 range->lsr_index = idif_ost_idx(seq);
3640 if (!fid_seq_in_fldb(seq)) {
3641 fld_range_set_mdt(range);
3644 * FIXME: If ss is NULL, it suppose not get lsr_index
3647 range->lsr_index = ss->ss_node_id;
3651 LASSERT(ss != NULL);
3652 fld_range_set_any(range);
3653 /* OSD will only do local fld lookup */
3654 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3657 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3658 struct lu_attr *attr,
3659 struct dt_allocation_hint *hint,
3660 struct dt_object_format *dof,
3661 struct thandle *handle)
3663 struct osd_thandle *oh;
3664 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3670 LASSERT(handle != NULL);
3672 oh = container_of(handle, struct osd_thandle, ot_super);
3673 LASSERT(oh->ot_handle == NULL);
3676 * EA object consumes more credits than regular object: osd_mk_index
3677 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3678 * leaf_node, could involves the block, block bitmap, groups, GDT
3679 * change for each block, so add 4 * 2 credits in that case.
3681 * The default ACL initialization may consume an additional 16 blocks
3683 credits = osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3684 ((dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3687 * While ldiskfs_new_inode() calls ldiskfs_init_acl() we have to add
3688 * credits for possible default ACL creation in new inode
3690 if (hint && hint->dah_acl_len)
3691 credits += osd_calc_bkmap_credits(sb, NULL, 0, -1,
3692 (hint->dah_acl_len + sb->s_blocksize - 1) >>
3693 sb->s_blocksize_bits);
3695 osd_trans_declare_op(env, oh, OSD_OT_CREATE, credits);
3698 * Reuse idle OI block may cause additional one OI block
3701 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3702 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3703 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
3704 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3705 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3707 /* will help to find FID->ino mapping at dt_insert() */
3708 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3716 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3717 attr->la_projid, 1, oh, osd_dt_obj(dt),
3718 NULL, OSD_QID_INODE);
3726 * Called to destroy on-disk representation of the object
3728 * Concurrency: must be locked
3730 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3733 struct osd_object *obj = osd_dt_obj(dt);
3734 struct inode *inode = obj->oo_inode;
3735 struct osd_thandle *oh;
3743 oh = container_of(th, struct osd_thandle, ot_super);
3744 LASSERT(oh->ot_handle == NULL);
3746 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3747 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3749 /* For removing agent entry */
3750 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3751 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3754 * Recycle idle OI leaf may cause additional three OI blocks
3757 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3758 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3759 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3760 /* one less inode */
3761 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3762 i_projid_read(inode), -1, oh, obj, NULL,
3766 /* data to be truncated */
3767 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3768 i_projid_read(inode), 0, oh, obj, NULL,
3774 * will help to find FID->ino when this object is being
3777 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3782 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3785 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3786 struct osd_object *obj = osd_dt_obj(dt);
3787 struct inode *inode = obj->oo_inode;
3788 struct osd_device *osd = osd_obj2dev(obj);
3789 struct osd_thandle *oh;
3794 oh = container_of(th, struct osd_thandle, ot_super);
3795 LASSERT(oh->ot_handle);
3797 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3799 if (unlikely(fid_is_acct(fid)))
3802 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3803 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3805 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3806 osd_name(osd), PFID(fid), result);
3809 if (S_ISDIR(inode->i_mode)) {
3810 if (inode->i_nlink > 2)
3811 CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
3812 osd_name(osd), PFID(fid), inode->i_ino,
3815 spin_lock(&obj->oo_guard);
3817 spin_unlock(&obj->oo_guard);
3818 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3821 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3823 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3825 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3826 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3827 oh->ot_handle, OI_CHECK_FLD);
3829 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3830 /* XXX: add to ext3 orphan list */
3831 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3833 /* not needed in the cache anymore */
3834 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3835 obj->oo_destroyed = 1;
3841 * Put the fid into lustre_mdt_attrs, and then place the structure
3842 * inode's ea. This fid should not be altered during the life time
3845 * \retval +ve, on success
3846 * \retval -ve, on error
3848 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3850 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3851 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3853 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3854 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3859 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3862 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3865 lustre_loa_init(loa, fid, compat, incompat);
3866 lustre_loa_swab(loa, false);
3869 * For the OST device with 256 bytes inode size by default,
3870 * the PFID EA will be stored together with LMA EA to avoid
3871 * performance trouble. Otherwise the PFID EA can be stored
3872 * independently. LU-8998
3874 if ((compat & LMAC_FID_ON_OST) &&
3875 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3876 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3877 sizeof(*loa), XATTR_CREATE);
3879 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3880 sizeof(*lma), XATTR_CREATE);
3882 * LMA may already exist, but we need to check that all the
3883 * desired compat/incompat flags have been added.
3885 if (unlikely(rc == -EEXIST)) {
3886 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3887 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3891 if (rc < sizeof(*lma))
3894 lustre_loa_swab(loa, true);
3895 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3896 ((compat == 0 && incompat == 0) ||
3897 (!(~lma->lma_compat & compat) &&
3898 !(~lma->lma_incompat & incompat))))
3901 lma->lma_self_fid = *fid;
3902 lma->lma_compat |= compat;
3903 lma->lma_incompat |= incompat;
3904 if (rc == sizeof(*lma)) {
3905 lustre_lma_swab(lma);
3906 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3907 sizeof(*lma), XATTR_REPLACE);
3909 lustre_loa_swab(loa, false);
3910 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3911 sizeof(*loa), XATTR_REPLACE);
3919 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3920 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3921 * To have compatilibility with 1.8 ldiskfs driver we need to have
3922 * magic number at start of fid data.
3923 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3926 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3927 const struct lu_fid *fid)
3929 if (!fid_is_namespace_visible(fid) ||
3930 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3931 param->edp_magic = 0;
3935 param->edp_magic = LDISKFS_LUFID_MAGIC;
3936 param->edp_len = sizeof(struct lu_fid) + 1;
3937 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3941 * Try to read the fid from inode ea into dt_rec.
3943 * \param fid object fid.
3945 * \retval 0 on success
3947 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3948 __u32 ino, struct lu_fid *fid,
3949 struct osd_inode_id *id)
3951 struct osd_thread_info *info = osd_oti_get(env);
3952 struct inode *inode;
3956 osd_id_gen(id, ino, OSD_OII_NOGEN);
3957 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3959 RETURN(PTR_ERR(inode));
3965 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3967 struct inode *parent_dir,
3968 const struct lu_fid *dot_fid,
3969 const struct lu_fid *dot_dot_fid,
3970 struct osd_thandle *oth)
3972 struct ldiskfs_dentry_param *dot_ldp;
3973 struct ldiskfs_dentry_param *dot_dot_ldp;
3974 __u32 saved_nlink = dir->i_nlink;
3977 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_DOTDOT_ENOSPC))
3980 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3981 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3983 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3984 dot_ldp->edp_magic = 0;
3986 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3987 dir, dot_ldp, dot_dot_ldp);
3989 * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3990 * the subseqent ref_add() will increase the dir->i_nlink
3991 * as 3. That is incorrect for new created directory.
3993 * It looks like hack, because we want to make the OSD API
3994 * to be order-independent for new created directory object
3995 * between dt_insert(..) and ref_add() operations.
3997 * Here, we only restore the in-RAM dir-inode's nlink attr,
3998 * becuase if the nlink attr is not 2, then there will be
3999 * ref_add() called following the dt_insert(..), such call
4000 * will make both the in-RAM and on-disk dir-inode's nlink
4001 * attr to be set as 2. LU-7447
4003 set_nlink(dir, saved_nlink);
4008 * Create an local agent inode for remote entry
4010 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
4011 struct osd_device *osd,
4012 struct osd_object *pobj,
4013 const struct lu_fid *fid,
4017 struct osd_thread_info *info = osd_oti_get(env);
4018 struct inode *local;
4019 struct osd_thandle *oh;
4020 struct iattr iattr = {
4021 .ia_valid = ATTR_UID | ATTR_GID |
4022 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
4023 .ia_ctime.tv_nsec = UTIME_OMIT,
4024 .ia_mtime.tv_nsec = UTIME_OMIT,
4025 .ia_atime.tv_nsec = UTIME_OMIT,
4026 .ia_uid = GLOBAL_ROOT_UID,
4027 .ia_gid = GLOBAL_ROOT_GID,
4034 oh = container_of(th, struct osd_thandle, ot_super);
4035 LASSERT(oh->ot_handle->h_transaction != NULL);
4037 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode,
4039 if (IS_ERR(local)) {
4040 CERROR("%s: create local error %d\n", osd_name(osd),
4041 (int)PTR_ERR(local));
4046 * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
4047 * correct gid on remote file, not agent here
4049 local->i_gid = current_fsgid();
4050 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
4052 /* e2fsck doesn't like empty symlinks. Store remote FID as symlink.
4053 * That gives e2fsck something to look at and be happy, and allows
4054 * debugging if we need to determine where this symlink came from.
4056 if (S_ISLNK(type)) {
4057 BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
4058 ldiskfs_clear_inode_flag(local, LDISKFS_INODE_EXTENTS);
4059 rc = scnprintf((char *)LDISKFS_I(local)->i_data,
4060 LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
4062 i_size_write(local, rc);
4063 LDISKFS_I(local)->i_disksize = rc;
4065 unlock_new_inode(local);
4067 /* Agent inode should not have project ID */
4068 #ifdef HAVE_PROJECT_QUOTA
4069 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
4070 i_projid_read(pobj->oo_inode) != 0) {
4071 rc = osd_transfer_project(local, 0, th);
4073 CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
4074 osd_ino2name(local), rc);
4075 RETURN(ERR_PTR(rc));
4079 /* Set special LMA flag for local agent inode */
4080 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
4082 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
4083 osd_name(osd), PFID(fid), rc);
4084 RETURN(ERR_PTR(rc));
4090 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode, fid,
4091 lu_object_fid(&pobj->oo_dt.do_lu),
4094 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
4095 osd_name(osd), PFID(fid), rc);
4096 RETURN(ERR_PTR(rc));
4103 * when direntry is deleted, we have to take care of possible agent inode
4104 * referenced by that. unfortunately we can't do this at that point:
4105 * iget() within a running transaction leads to deadlock and we better do
4106 * not call that every delete declaration to save performance. so we put
4107 * a potention agent inode on a list and process that once the transaction
4108 * is over. Notice it's not any worse in terms of real orphans as regular
4109 * object destroy doesn't put inodes on the on-disk orphan list. this should
4110 * be addressed separately
4112 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
4113 struct osd_thandle *oh,
4116 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
4117 struct osd_obj_orphan *oor;
4124 oor->oor_env = (struct lu_env *)env;
4125 spin_lock(&osd->od_osfs_lock);
4126 list_add(&oor->oor_list, &osd->od_orphan_list);
4127 spin_unlock(&osd->od_osfs_lock);
4129 oh->ot_remove_agents = 1;
4135 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
4136 struct osd_device *osd)
4138 struct osd_thread_info *info = osd_oti_get(env);
4139 struct osd_obj_orphan *oor, *tmp;
4140 struct osd_inode_id id;
4142 struct inode *inode;
4147 spin_lock(&osd->od_osfs_lock);
4148 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
4149 if (oor->oor_env == env)
4150 list_move(&oor->oor_list, &list);
4152 spin_unlock(&osd->od_osfs_lock);
4154 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
4158 list_del(&oor->oor_list);
4161 osd_id_gen(&id, ino, OSD_OII_NOGEN);
4162 inode = osd_iget_fid(info, osd, &id, &fid);
4166 if (!osd_remote_fid(env, osd, &fid)) {
4171 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
4173 mark_inode_dirty(inode);
4174 ldiskfs_journal_stop(jh);
4182 * OSD layer object create function for OST objects (b=11826).
4184 * The FID is inserted into inode xattr here.
4186 * \retval 0, on success
4187 * \retval -ve, on error
4189 static int osd_create(const struct lu_env *env, struct dt_object *dt,
4190 struct lu_attr *attr, struct dt_allocation_hint *hint,
4191 struct dt_object_format *dof, struct thandle *th)
4193 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4194 struct osd_object *obj = osd_dt_obj(dt);
4195 struct osd_thread_info *info = osd_oti_get(env);
4196 int result, on_ost = 0;
4200 if (dt_object_exists(dt))
4203 LINVRNT(osd_invariant(obj));
4204 LASSERT(!dt_object_remote(dt));
4205 LASSERT(osd_is_write_locked(env, obj));
4206 LASSERT(th != NULL);
4208 if (unlikely(fid_is_acct(fid)))
4210 * Quota files can't be created from the kernel any more,
4211 * 'tune2fs -O quota' will take care of creating them
4215 result = __osd_create(info, obj, attr, hint, dof, th);
4217 if (fid_is_idif(fid) &&
4218 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
4219 struct lu_fid *tfid = &info->oti_fid;
4220 struct ost_id *oi = &info->oti_ostid;
4222 fid_to_ostid(fid, oi);
4223 ostid_to_fid(tfid, oi, 0);
4225 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
4226 LMAC_FID_ON_OST, 0);
4228 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
4230 result = osd_ea_fid_set(info, obj->oo_inode, fid,
4231 on_ost ? LMAC_FID_ON_OST : 0,
4234 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
4235 obj->oo_dt.do_body_ops = &osd_body_ops;
4238 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY)) {
4239 struct inode *inode = obj->oo_inode;
4241 result = __osd_oi_insert(env, obj, fid, th);
4242 if (result && inode) {
4243 spin_lock(&obj->oo_guard);
4245 spin_unlock(&obj->oo_guard);
4246 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4247 ldiskfs_set_inode_state(inode,
4248 LDISKFS_STATE_LUSTRE_DESTROY);
4250 obj->oo_inode = NULL;
4255 * a small optimization - dt_insert() isn't usually applied
4256 * to OST objects, so we don't need to cache OI mapping for
4259 if (result == 0 && on_ost == 0) {
4260 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4262 result = osd_idc_find_and_init(env, osd, obj);
4263 LASSERT(result == 0);
4266 LASSERT(ergo(result == 0,
4267 dt_object_exists(dt) && !dt_object_remote(dt)));
4268 LINVRNT(osd_invariant(obj));
4272 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
4273 struct thandle *handle)
4275 struct osd_thandle *oh;
4278 /* it's possible that object doesn't exist yet */
4279 LASSERT(handle != NULL);
4281 oh = container_of(handle, struct osd_thandle, ot_super);
4282 LASSERT(oh->ot_handle == NULL);
4284 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
4285 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4287 rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
4294 * Concurrency: @dt is write locked.
4296 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
4299 struct osd_object *obj = osd_dt_obj(dt);
4300 struct inode *inode = obj->oo_inode;
4301 struct osd_thandle *oh;
4304 if (!dt_object_exists(dt) || obj->oo_destroyed)
4307 LINVRNT(osd_invariant(obj));
4308 LASSERT(!dt_object_remote(dt));
4309 LASSERT(osd_is_write_locked(env, obj));
4310 LASSERT(th != NULL);
4312 oh = container_of(th, struct osd_thandle, ot_super);
4313 LASSERT(oh->ot_handle != NULL);
4315 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
4317 CDEBUG(D_INODE, DFID" increase nlink %d\n",
4318 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4320 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
4321 * (65000) subdirectories by storing "1" in i_nlink if the link count
4322 * would otherwise overflow. Directory tranversal tools understand
4323 * that (st_nlink == 1) indicates that the filesystem dose not track
4324 * hard links count on the directory, and will not abort subdirectory
4325 * scanning early once (st_nlink - 2) subdirs have been found.
4327 * This also has to properly handle the case of inodes with nlink == 0
4328 * in case they are being linked into the PENDING directory
4330 spin_lock(&obj->oo_guard);
4331 if (unlikely(inode->i_nlink == 0))
4332 /* inc_nlink from 0 may cause WARN_ON */
4333 set_nlink(inode, 1);
4335 osd_ldiskfs_inc_count(oh->ot_handle, inode);
4336 if (!S_ISDIR(inode->i_mode))
4337 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
4339 spin_unlock(&obj->oo_guard);
4341 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4342 LINVRNT(osd_invariant(obj));
4344 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
4349 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
4350 struct thandle *handle)
4352 struct osd_thandle *oh;
4354 if (!dt_object_exists(dt))
4357 LASSERT(!dt_object_remote(dt));
4358 LASSERT(handle != NULL);
4360 oh = container_of(handle, struct osd_thandle, ot_super);
4361 LASSERT(oh->ot_handle == NULL);
4363 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
4364 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4370 * Concurrency: @dt is write locked.
4372 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
4375 struct osd_object *obj = osd_dt_obj(dt);
4376 struct inode *inode = obj->oo_inode;
4377 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4378 struct osd_thandle *oh;
4380 if (!dt_object_exists(dt))
4383 LINVRNT(osd_invariant(obj));
4384 LASSERT(!dt_object_remote(dt));
4385 LASSERT(osd_is_write_locked(env, obj));
4386 LASSERT(th != NULL);
4388 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
4391 oh = container_of(th, struct osd_thandle, ot_super);
4392 LASSERT(oh->ot_handle != NULL);
4394 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
4396 spin_lock(&obj->oo_guard);
4398 * That can be result of upgrade from old Lustre version and
4399 * applied only to local files. Just skip this ref_del call.
4400 * ext4_unlink() only treats this as a warning, don't LASSERT here.
4402 if (inode->i_nlink == 0) {
4403 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
4404 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
4405 ", maybe an upgraded file? (LU-3915)\n",
4406 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
4407 spin_unlock(&obj->oo_guard);
4411 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
4412 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4414 osd_ldiskfs_dec_count(oh->ot_handle, inode);
4415 spin_unlock(&obj->oo_guard);
4417 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4418 LINVRNT(osd_invariant(obj));
4420 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
4426 * Concurrency: @dt is read locked.
4428 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
4429 struct lu_buf *buf, const char *name)
4431 struct osd_object *obj = osd_dt_obj(dt);
4432 struct inode *inode = obj->oo_inode;
4433 struct osd_thread_info *info = osd_oti_get(env);
4434 struct dentry *dentry = &info->oti_obj_dentry;
4435 bool cache_xattr = false;
4440 /* version get is not real XATTR but uses xattr API */
4441 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4442 dt_obj_version_t *ver = buf->lb_buf;
4445 * for version we are just using xattr API but change inode
4448 if (buf->lb_len == 0)
4449 return sizeof(dt_obj_version_t);
4451 if (buf->lb_len < sizeof(dt_obj_version_t))
4454 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
4455 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4457 *ver = LDISKFS_I(inode)->i_fs_version;
4459 return sizeof(dt_obj_version_t);
4462 if (!dt_object_exists(dt))
4465 LASSERT(!dt_object_remote(dt));
4466 LASSERT(inode->i_op != NULL);
4467 #ifdef HAVE_IOP_XATTR
4468 LASSERT(inode->i_op->getxattr != NULL);
4471 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
4472 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
4476 rc = osd_oxc_get(obj, name, buf);
4481 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4482 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4483 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4484 struct filter_fid *ff;
4485 struct ost_layout *ol;
4487 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4491 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4494 if (buf->lb_len == 0 || !buf->lb_buf)
4497 if (buf->lb_len < rc)
4501 ol = &ff->ff_layout;
4502 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
4503 PFID_STRIPE_IDX_BITS);
4504 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4505 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4506 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4507 if (lma->lma_compat & LMAC_COMP_INFO) {
4508 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4509 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4510 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4512 ol->ol_comp_start = 0;
4513 ol->ol_comp_end = 0;
4517 /* Get enc context xattr directly from ldiskfs instead of going
4518 * through the VFS, as there is no xattr handler for
4521 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
4522 rc = ldiskfs_xattr_get(inode,
4523 LDISKFS_XATTR_INDEX_ENCRYPTION,
4524 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
4525 buf->lb_buf, buf->lb_len);
4527 rc = __osd_xattr_get(inode, dentry, name,
4528 buf->lb_buf, buf->lb_len);
4532 if (rc == -ENOENT || rc == -ENODATA)
4533 osd_oxc_add(obj, name, NULL, 0);
4534 else if (rc > 0 && buf->lb_buf != NULL)
4535 osd_oxc_add(obj, name, buf->lb_buf, rc);
4541 static int osd_declare_xattr_set(const struct lu_env *env,
4542 struct dt_object *dt,
4543 const struct lu_buf *buf, const char *name,
4544 int fl, struct thandle *handle)
4546 struct osd_thandle *oh;
4548 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4550 LASSERT(handle != NULL);
4552 oh = container_of(handle, struct osd_thandle, ot_super);
4553 LASSERT(oh->ot_handle == NULL);
4555 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4557 * For non-upgrading case, the LMA is set first and
4558 * usually fit inode. But for upgrade case, the LMA
4559 * may be in another separated EA block.
4561 if (dt_object_exists(dt)) {
4562 if (fl == LU_XATTR_REPLACE)
4567 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4569 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4570 /* We may need to delete the old PFID EA. */
4571 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4572 if (fl == LU_XATTR_REPLACE)
4578 * If some name entry resides on remote MDT, then will create
4579 * agent entry under remote parent. On the other hand, if the
4580 * remote entry will be removed, then related agent entry may
4581 * need to be removed from the remote parent. So there may be
4582 * kinds of cases, let's declare enough credits. The credits
4583 * for create agent entry is enough for remove case.
4585 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4586 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4587 if (dt_object_exists(dt))
4588 credits += 1; /* For updating LMA */
4592 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4597 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4599 * learn xattr size from osd_xattr_get if
4600 * attribute has not been read yet
4602 buflen = __osd_xattr_get(
4603 osd_dt_obj(dt)->oo_inode,
4604 &osd_oti_get(env)->oti_obj_dentry,
4609 buflen = buf->lb_len;
4612 if (buflen > sb->s_blocksize) {
4613 credits += osd_calc_bkmap_credits(
4615 (buflen + sb->s_blocksize - 1) >>
4616 sb->s_blocksize_bits);
4620 * xattr set may involve inode quota change, reserve credits for
4621 * dquot_initialize()
4623 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4626 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4631 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4632 const struct lu_buf *buf, int fl,
4633 struct thandle *handle)
4635 struct osd_thread_info *info = osd_oti_get(env);
4636 struct dentry *dentry = &info->oti_obj_dentry;
4637 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4638 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4639 struct inode *inode = obj->oo_inode;
4640 struct filter_fid *ff = buf->lb_buf;
4641 struct ost_layout *ol = &ff->ff_layout;
4642 int flags = XATTR_REPLACE;
4647 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4650 rc = osd_get_lma(info, inode, dentry, loa);
4651 if (rc == -ENODATA) {
4652 /* Usually for upgarding from old device */
4653 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4654 LMAC_FID_ON_OST, 0);
4655 flags = XATTR_CREATE;
4660 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4661 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4664 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4665 /* Separate PFID EA from LMA */
4666 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4667 lustre_lma_swab(lma);
4668 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4669 sizeof(*lma), XATTR_REPLACE);
4671 obj->oo_pfid_in_lma = 0;
4672 rc = LU_XATTR_CREATE;
4678 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4682 * Old client does not send stripe information,
4683 * then store the PFID EA on disk separatedly.
4685 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4686 ol->ol_stripe_size == 0))
4689 /* Remove old PFID EA entry firstly. */
4690 dquot_initialize(inode);
4691 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
4692 if (rc == -ENODATA) {
4693 /* XATTR_NAME_FID is already absent */
4700 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4701 if (likely(ol->ol_stripe_size != 0)) {
4702 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4703 PFID_STRIPE_IDX_BITS;
4704 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4705 lma->lma_compat |= LMAC_STRIPE_INFO;
4706 if (ol->ol_comp_id != 0) {
4707 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4708 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4709 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4710 lma->lma_compat |= LMAC_COMP_INFO;
4714 lustre_loa_swab(loa, false);
4716 /* Store the PFID EA inside LMA. */
4717 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4720 obj->oo_pfid_in_lma = 1;
4726 * In DNE environment, the object (in spite of regular file or directory)
4727 * and its name entry may reside on different MDTs. Under such case, we will
4728 * create an agent entry on the MDT where the object resides. The agent entry
4729 * references the object locally, that makes the object to be visible to the
4730 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4731 * such as 'tar' can handle the object properly.
4733 * We handle the agent entry during set linkEA that is the common interface
4734 * for both regular file and directroy, can handle kinds of cases, such as
4735 * create/link/unlink/rename, and so on.
4737 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4739 * XXX: There are two known issues:
4740 * 1. For one object, we will create at most one agent entry even if there
4741 * may be more than one cross-MDTs hard links on the object. So the local
4742 * e2fsck may claim that the object's nlink is larger than the name entries
4743 * that reference such inode. And in further, the e2fsck will fix the nlink
4744 * attribute to match the local references. Then it will cause the object's
4745 * nlink attribute to be inconsistent with the global references. it is bad
4746 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4747 * other hand, the global namespace LFSCK can repair the object's attribute
4748 * according to the linkEA.
4749 * 2. There may be too many hard links on the object as to its linkEA overflow,
4750 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4751 * case happened, then at this point, we do not know whether there are some
4752 * cross-MDTs reference. But there are local references, it guarantees that
4753 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4755 static int osd_xattr_handle_linkea(const struct lu_env *env,
4756 struct osd_device *osd,
4757 struct osd_object *obj,
4758 const struct lu_buf *buf,
4759 struct thandle *handle)
4761 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4762 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4763 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4764 struct lu_name tmpname;
4765 struct osd_thandle *oh;
4767 bool remote = false;
4771 oh = container_of(handle, struct osd_thandle, ot_super);
4772 LASSERT(oh->ot_handle != NULL);
4774 rc = linkea_init_with_rec(&ldata);
4776 linkea_first_entry(&ldata);
4777 while (ldata.ld_lee != NULL && !remote) {
4778 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4780 if (osd_remote_fid(env, osd, tfid) > 0)
4783 linkea_next_entry(&ldata);
4785 } else if (rc == -ENODATA) {
4791 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4792 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4794 CERROR("%s: failed to remove agent entry for "DFID
4795 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4796 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4797 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4799 CERROR("%s: failed to create agent entry for "DFID
4800 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4807 * Concurrency: @dt is write locked.
4809 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4810 const struct lu_buf *buf, const char *name, int fl,
4811 struct thandle *handle)
4813 struct osd_object *obj = osd_dt_obj(dt);
4814 struct osd_device *osd = osd_obj2dev(obj);
4815 struct inode *inode = obj->oo_inode;
4816 struct osd_thread_info *info = osd_oti_get(env);
4826 /* version set is not real XATTR */
4827 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4828 dt_obj_version_t *version = buf->lb_buf;
4831 * for version we are just using xattr API but change inode
4834 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4836 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4837 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4839 LDISKFS_I(inode)->i_fs_version = *version;
4841 * Version is set after all inode operations are finished,
4842 * so we should mark it dirty here
4844 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4849 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4850 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4853 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4856 * For the OST device with 256 bytes inode size by default,
4857 * the PFID EA will be stored together with LMA EA to avoid
4858 * performance trouble. Otherwise the PFID EA can be stored
4859 * independently. LU-8998
4861 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4862 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4863 LASSERT(buf->lb_buf);
4865 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4868 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4869 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4870 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4872 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4876 lma->lma_incompat |= LMAI_STRIPED;
4877 lustre_lma_swab(lma);
4878 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4879 sizeof(*lma), XATTR_REPLACE);
4882 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
4883 LASSERT(!osd->od_is_ost);
4885 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
4890 if (fl & LU_XATTR_REPLACE)
4891 fs_flags |= XATTR_REPLACE;
4893 if (fl & LU_XATTR_CREATE)
4894 fs_flags |= XATTR_CREATE;
4896 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) {
4897 /* Set enc context xattr directly in ldiskfs instead of going
4898 * through the VFS, as there is no xattr handler for
4901 struct osd_thandle *oth = container_of(handle,
4905 if (!oth->ot_handle)
4906 /* this should be already part of a transaction */
4909 rc = ldiskfs_xattr_set_handle(oth->ot_handle, inode,
4910 LDISKFS_XATTR_INDEX_ENCRYPTION,
4911 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
4912 buf->lb_buf, len, fs_flags);
4914 rc = __osd_xattr_set(info, inode, name,
4915 buf->lb_buf, len, fs_flags);
4917 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4920 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4921 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4922 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4928 * Concurrency: @dt is read locked.
4930 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4931 const struct lu_buf *buf)
4933 struct osd_object *obj = osd_dt_obj(dt);
4934 struct osd_device *dev = osd_obj2dev(obj);
4935 struct inode *inode = obj->oo_inode;
4936 struct osd_thread_info *info = osd_oti_get(env);
4937 struct dentry *dentry = &info->oti_obj_dentry;
4940 if (!dt_object_exists(dt))
4943 LASSERT(!dt_object_remote(dt));
4944 LASSERT(inode->i_op != NULL);
4945 LASSERT(inode->i_op->listxattr != NULL);
4947 dentry->d_inode = inode;
4948 dentry->d_sb = inode->i_sb;
4949 rc = inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4951 if (rc < 0 || buf->lb_buf == NULL)
4954 /* Hide virtual project ID xattr from list if disabled */
4955 if (!dev->od_enable_projid_xattr) {
4956 char *end = (char *)buf->lb_buf + rc;
4957 char *p = buf->lb_buf;
4960 char *next = p + strlen(p) + 1;
4962 if (strcmp(p, XATTR_NAME_PROJID) == 0) {
4964 memmove(p, next, end - next);
4976 static int osd_declare_xattr_del(const struct lu_env *env,
4977 struct dt_object *dt, const char *name,
4978 struct thandle *handle)
4980 struct osd_thandle *oh;
4981 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4983 LASSERT(!dt_object_remote(dt));
4984 LASSERT(handle != NULL);
4986 oh = container_of(handle, struct osd_thandle, ot_super);
4987 LASSERT(oh->ot_handle == NULL);
4989 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4990 osd_dto_credits_noquota[DTO_XATTR_SET]);
4992 * xattr del may involve inode quota change, reserve credits for
4993 * dquot_initialize()
4995 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
5001 * Concurrency: @dt is write locked.
5003 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
5004 const char *name, struct thandle *handle)
5006 struct osd_object *obj = osd_dt_obj(dt);
5007 struct inode *inode = obj->oo_inode;
5008 struct osd_thread_info *info = osd_oti_get(env);
5009 struct dentry *dentry = &info->oti_obj_dentry;
5012 if (!dt_object_exists(dt))
5015 LASSERT(!dt_object_remote(dt));
5016 LASSERT(inode->i_op != NULL);
5017 LASSERT(handle != NULL);
5018 #ifdef HAVE_IOP_XATTR
5019 LASSERT(inode->i_op->removexattr != NULL);
5022 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
5024 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
5025 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
5027 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
5028 &info->oti_ost_attrs);
5030 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
5032 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
5033 lustre_lma_swab(lma);
5034 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
5035 sizeof(*lma), XATTR_REPLACE);
5037 obj->oo_pfid_in_lma = 0;
5040 dquot_initialize(inode);
5041 dentry->d_inode = inode;
5042 dentry->d_sb = inode->i_sb;
5043 rc = ll_vfs_removexattr(dentry, inode, name);
5046 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
5049 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5050 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
5051 osd_oxc_del(obj, name);
5056 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
5057 __u64 start, __u64 end)
5059 struct osd_object *obj = osd_dt_obj(dt);
5060 struct osd_device *dev = osd_obj2dev(obj);
5061 struct inode *inode = obj->oo_inode;
5066 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
5069 RETURN(PTR_ERR(file));
5071 file->f_mode |= FMODE_64BITHASH;
5072 rc = vfs_fsync_range(file, start, end, 0);
5079 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
5084 static bool osd_check_stale(struct dt_object *dt)
5092 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
5093 const struct dt_index_features *feat)
5095 struct iam_descr *descr;
5097 if (osd_object_is_root(o))
5098 return feat == &dt_directory_features;
5100 LASSERT(o->oo_dir != NULL);
5102 descr = o->oo_dir->od_container.ic_descr;
5103 if (feat == &dt_directory_features) {
5104 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
5109 return feat->dif_keysize_min <= descr->id_key_size &&
5110 descr->id_key_size <= feat->dif_keysize_max &&
5111 feat->dif_recsize_min <= descr->id_rec_size &&
5112 descr->id_rec_size <= feat->dif_recsize_max &&
5113 !(feat->dif_flags & (DT_IND_VARKEY |
5114 DT_IND_VARREC | DT_IND_NONUNQ)) &&
5115 ergo(feat->dif_flags & DT_IND_UPDATE,
5116 1 /* XXX check that object (and fs) is writable */);
5120 static int osd_iam_container_init(const struct lu_env *env,
5121 struct osd_object *obj,
5122 struct osd_directory *dir)
5124 struct iam_container *bag = &dir->od_container;
5127 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
5131 result = iam_container_setup(bag);
5133 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
5135 iam_container_fini(bag);
5142 * Concurrency: no external locking is necessary.
5144 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
5145 const struct dt_index_features *feat)
5149 struct osd_object *obj = osd_dt_obj(dt);
5151 LINVRNT(osd_invariant(obj));
5153 if (osd_object_is_root(obj)) {
5154 dt->do_index_ops = &osd_index_ea_ops;
5156 } else if (feat == &dt_directory_features) {
5157 dt->do_index_ops = &osd_index_ea_ops;
5158 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
5163 } else if (unlikely(feat == &dt_otable_features)) {
5164 dt->do_index_ops = &osd_otable_ops;
5166 } else if (unlikely(feat == &dt_acct_features)) {
5167 dt->do_index_ops = &osd_acct_index_ops;
5170 } else if (!osd_has_index(obj)) {
5171 struct osd_directory *dir;
5172 struct osd_device *osd = osd_obj2dev(obj);
5173 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
5178 spin_lock(&obj->oo_guard);
5179 if (obj->oo_dir == NULL)
5183 * Concurrent thread allocated container data.
5186 spin_unlock(&obj->oo_guard);
5188 * Now, that we have container data, serialize its
5191 down_write(&obj->oo_ext_idx_sem);
5193 * recheck under lock.
5196 if (osd_has_index(obj)) {
5201 result = osd_iam_container_init(env, obj, obj->oo_dir);
5202 if (result || feat == &dt_lfsck_namespace_features ||
5203 feat == &dt_lfsck_layout_orphan_features ||
5204 feat == &dt_lfsck_layout_dangling_features)
5207 result = osd_index_register(osd, fid,
5208 feat->dif_keysize_max,
5209 feat->dif_recsize_max);
5211 CWARN("%s: failed to register index "
5213 osd_name(osd), PFID(fid), result);
5214 else if (result > 0)
5217 CDEBUG(D_LFSCK, "%s: index object "DFID
5218 " (%d/%d) registered\n",
5219 osd_name(osd), PFID(fid),
5220 (int)feat->dif_keysize_max,
5221 (int)feat->dif_recsize_max);
5224 up_write(&obj->oo_ext_idx_sem);
5232 if (result == 0 && skip_iam == 0) {
5233 if (!osd_iam_index_probe(env, obj, feat))
5236 LINVRNT(osd_invariant(obj));
5241 static int osd_otable_it_attr_get(const struct lu_env *env,
5242 struct dt_object *dt,
5243 struct lu_attr *attr)
5249 static const struct dt_object_operations osd_obj_ops = {
5250 .do_read_lock = osd_read_lock,
5251 .do_write_lock = osd_write_lock,
5252 .do_read_unlock = osd_read_unlock,
5253 .do_write_unlock = osd_write_unlock,
5254 .do_write_locked = osd_write_locked,
5255 .do_attr_get = osd_attr_get,
5256 .do_declare_attr_set = osd_declare_attr_set,
5257 .do_attr_set = osd_attr_set,
5258 .do_ah_init = osd_ah_init,
5259 .do_declare_create = osd_declare_create,
5260 .do_create = osd_create,
5261 .do_declare_destroy = osd_declare_destroy,
5262 .do_destroy = osd_destroy,
5263 .do_index_try = osd_index_try,
5264 .do_declare_ref_add = osd_declare_ref_add,
5265 .do_ref_add = osd_ref_add,
5266 .do_declare_ref_del = osd_declare_ref_del,
5267 .do_ref_del = osd_ref_del,
5268 .do_xattr_get = osd_xattr_get,
5269 .do_declare_xattr_set = osd_declare_xattr_set,
5270 .do_xattr_set = osd_xattr_set,
5271 .do_declare_xattr_del = osd_declare_xattr_del,
5272 .do_xattr_del = osd_xattr_del,
5273 .do_xattr_list = osd_xattr_list,
5274 .do_object_sync = osd_object_sync,
5275 .do_invalidate = osd_invalidate,
5276 .do_check_stale = osd_check_stale,
5279 static const struct dt_object_operations osd_obj_otable_it_ops = {
5280 .do_attr_get = osd_otable_it_attr_get,
5281 .do_index_try = osd_index_try,
5284 static int osd_index_declare_iam_delete(const struct lu_env *env,
5285 struct dt_object *dt,
5286 const struct dt_key *key,
5287 struct thandle *handle)
5289 struct osd_thandle *oh;
5291 oh = container_of(handle, struct osd_thandle, ot_super);
5292 LASSERT(oh->ot_handle == NULL);
5294 /* Recycle may cause additional three blocks to be changed. */
5295 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
5296 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
5302 * delete a (key, value) pair from index \a dt specified by \a key
5304 * \param dt osd index object
5305 * \param key key for index
5306 * \param rec record reference
5307 * \param handle transaction handler
5310 * \retval -ve failure
5312 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
5313 const struct dt_key *key,
5314 struct thandle *handle)
5316 struct osd_thread_info *oti = osd_oti_get(env);
5317 struct osd_object *obj = osd_dt_obj(dt);
5318 struct osd_thandle *oh;
5319 struct iam_path_descr *ipd;
5320 struct iam_container *bag = &obj->oo_dir->od_container;
5325 if (!dt_object_exists(dt))
5328 LINVRNT(osd_invariant(obj));
5329 LASSERT(!dt_object_remote(dt));
5330 LASSERT(bag->ic_object == obj->oo_inode);
5331 LASSERT(handle != NULL);
5333 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5335 ipd = osd_idx_ipd_get(env, bag);
5336 if (unlikely(ipd == NULL))
5339 oh = container_of(handle, struct osd_thandle, ot_super);
5340 LASSERT(oh->ot_handle != NULL);
5341 LASSERT(oh->ot_handle->h_transaction != NULL);
5343 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5344 /* swab quota uid/gid provided by caller */
5345 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5346 key = (const struct dt_key *)&oti->oti_quota_id;
5349 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
5350 osd_ipd_put(env, bag, ipd);
5351 LINVRNT(osd_invariant(obj));
5352 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5356 static int osd_index_declare_ea_delete(const struct lu_env *env,
5357 struct dt_object *dt,
5358 const struct dt_key *key,
5359 struct thandle *handle)
5361 struct osd_thandle *oh;
5362 struct inode *inode;
5367 LASSERT(!dt_object_remote(dt));
5368 LASSERT(handle != NULL);
5370 oh = container_of(handle, struct osd_thandle, ot_super);
5371 LASSERT(oh->ot_handle == NULL);
5373 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
5374 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
5376 inode = osd_dt_obj(dt)->oo_inode;
5380 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
5381 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
5386 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
5389 struct osd_fid_pack *rec;
5392 if (de->file_type & LDISKFS_DIRENT_LUFID) {
5393 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5394 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
5395 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
5401 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
5402 const struct lu_fid *fid)
5404 struct seq_server_site *ss = osd_seq_site(osd);
5408 /* FID seqs not in FLDB, must be local seq */
5409 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
5413 * If FLD is not being initialized yet, it only happens during the
5414 * initialization, likely during mgs initialization, and we assume
5415 * this is local FID.
5417 if (ss == NULL || ss->ss_server_fld == NULL)
5420 /* Only check the local FLDB here */
5421 if (osd_seq_exists(env, osd, fid_seq(fid)))
5427 static void osd_take_care_of_agent(const struct lu_env *env,
5428 struct osd_device *osd,
5429 struct osd_thandle *oh,
5430 struct ldiskfs_dir_entry_2 *de)
5432 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
5433 struct osd_idmap_cache *idc;
5434 int rc, schedule = 0;
5436 LASSERT(de != NULL);
5438 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
5439 if (likely(rc == 0)) {
5440 idc = osd_idc_find_or_init(env, osd, fid);
5441 if (IS_ERR(idc) || idc->oic_remote)
5443 } else if (rc == -ENODATA) {
5445 * can't get FID, postpone to the end of the
5446 * transaction when iget() is safe
5450 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
5453 osd_schedule_agent_inode_removal(env, oh,
5454 le32_to_cpu(de->inode));
5458 * Utility function to get real name from object name
5460 * \param[in] obj pointer to the object to be handled
5461 * \param[in] name object name
5462 * \param[in] len object name len
5463 * \param[out]ln pointer to the struct lu_name to hold the real name
5465 * If file is not encrypted, real name is just the object name.
5466 * If file is encrypted, object name needs to be decoded. In
5467 * this case a new buffer is allocated, and ln->ln_name needs to be freed by
5470 * \retval 0, on success
5471 * \retval -ve, on error
5473 static int obj_name2lu_name(struct osd_object *obj, const char *name,
5474 int len, struct lu_name *ln)
5476 if (!(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
5478 ln->ln_namelen = len;
5480 char *buf = kmalloc(len, GFP_NOFS);
5485 len = critical_decode(name, len, buf);
5487 ln->ln_namelen = len;
5494 * Index delete function for interoperability mode (b11826).
5495 * It will remove the directory entry added by osd_index_ea_insert().
5496 * This entry is needed to maintain name->fid mapping.
5498 * \param key, key i.e. file entry to be deleted
5500 * \retval 0, on success
5501 * \retval -ve, on error
5503 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
5504 const struct dt_key *key, struct thandle *handle)
5506 struct osd_object *obj = osd_dt_obj(dt);
5507 struct inode *dir = obj->oo_inode;
5508 struct dentry *dentry;
5509 struct osd_thandle *oh;
5510 struct ldiskfs_dir_entry_2 *de = NULL;
5511 struct buffer_head *bh;
5512 struct htree_lock *hlock = NULL;
5513 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5519 if (!dt_object_exists(dt))
5522 LINVRNT(osd_invariant(obj));
5523 LASSERT(!dt_object_remote(dt));
5524 LASSERT(handle != NULL);
5526 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
5530 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5532 oh = container_of(handle, struct osd_thandle, ot_super);
5533 LASSERT(oh->ot_handle != NULL);
5534 LASSERT(oh->ot_handle->h_transaction != NULL);
5536 dquot_initialize(dir);
5537 dentry = osd_child_dentry_get(env, obj, ln.ln_name, ln.ln_namelen);
5539 if (obj->oo_hl_head != NULL) {
5540 hlock = osd_oti_get(env)->oti_hlock;
5541 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5542 dir, LDISKFS_HLOCK_DEL);
5544 down_write(&obj->oo_ext_idx_sem);
5547 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5550 * If this is not the ".." entry, it might be a remote DNE
5551 * entry and we need to check if the FID is for a remote
5552 * MDT. If the FID is not in the directory entry (e.g.
5553 * upgraded 1.8 filesystem without dirdata enabled) then
5554 * we need to get the FID from the LMA. For a remote directory
5555 * there HAS to be an LMA, it cannot be an IGIF inode in this
5558 * Delete the entry before the agent inode in order to
5559 * simplify error handling. At worst an error after deleting
5560 * the entry first might leak the agent inode afterward. The
5561 * reverse would need filesystem abort in case of error deleting
5562 * the entry after the agent had been removed, or leave a
5563 * dangling entry pointing at a random inode.
5565 if (strcmp((char *)key, dotdot) != 0)
5566 osd_take_care_of_agent(env, osd, oh, de);
5567 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
5573 if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
5574 obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
5575 /* NB, dirent count may not be accurate, because it's counted
5578 if (obj->oo_dirent_count)
5579 obj->oo_dirent_count--;
5581 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
5584 ldiskfs_htree_unlock(hlock);
5586 up_write(&obj->oo_ext_idx_sem);
5589 LASSERT(osd_invariant(obj));
5590 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5591 if (ln.ln_name != (char *)key)
5597 * Lookup index for \a key and copy record to \a rec.
5599 * \param dt osd index object
5600 * \param key key for index
5601 * \param rec record reference
5603 * \retval +ve success : exact mach
5604 * \retval 0 return record with key not greater than \a key
5605 * \retval -ve failure
5607 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
5608 struct dt_rec *rec, const struct dt_key *key)
5610 struct osd_object *obj = osd_dt_obj(dt);
5611 struct iam_path_descr *ipd;
5612 struct iam_container *bag = &obj->oo_dir->od_container;
5613 struct osd_thread_info *oti = osd_oti_get(env);
5614 struct iam_iterator *it = &oti->oti_idx_it;
5615 struct iam_rec *iam_rec;
5620 if (!dt_object_exists(dt))
5623 LASSERT(osd_invariant(obj));
5624 LASSERT(!dt_object_remote(dt));
5625 LASSERT(bag->ic_object == obj->oo_inode);
5627 ipd = osd_idx_ipd_get(env, bag);
5631 /* got ipd now we can start iterator. */
5632 iam_it_init(it, bag, 0, ipd);
5634 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5635 /* swab quota uid/gid provided by caller */
5636 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5637 key = (const struct dt_key *)&oti->oti_quota_id;
5640 rc = iam_it_get(it, (struct iam_key *)key);
5642 if (S_ISDIR(obj->oo_inode->i_mode))
5643 iam_rec = (struct iam_rec *)oti->oti_ldp;
5645 iam_rec = (struct iam_rec *)rec;
5647 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5649 if (S_ISDIR(obj->oo_inode->i_mode))
5650 osd_fid_unpack((struct lu_fid *)rec,
5651 (struct osd_fid_pack *)iam_rec);
5652 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5653 osd_quota_unpack(obj, rec);
5658 osd_ipd_put(env, bag, ipd);
5660 LINVRNT(osd_invariant(obj));
5665 static int osd_index_declare_iam_insert(const struct lu_env *env,
5666 struct dt_object *dt,
5667 const struct dt_rec *rec,
5668 const struct dt_key *key,
5669 struct thandle *handle)
5671 struct osd_thandle *oh;
5673 LASSERT(handle != NULL);
5675 oh = container_of(handle, struct osd_thandle, ot_super);
5676 LASSERT(oh->ot_handle == NULL);
5678 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5679 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5685 * Inserts (key, value) pair in \a dt index object.
5687 * \param dt osd index object
5688 * \param key key for index
5689 * \param rec record reference
5690 * \param th transaction handler
5693 * \retval -ve failure
5695 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5696 const struct dt_rec *rec,
5697 const struct dt_key *key, struct thandle *th)
5699 struct osd_object *obj = osd_dt_obj(dt);
5700 struct iam_path_descr *ipd;
5701 struct osd_thandle *oh;
5702 struct iam_container *bag;
5703 struct osd_thread_info *oti = osd_oti_get(env);
5704 struct iam_rec *iam_rec;
5709 if (!dt_object_exists(dt))
5712 LINVRNT(osd_invariant(obj));
5713 LASSERT(!dt_object_remote(dt));
5715 bag = &obj->oo_dir->od_container;
5716 LASSERT(bag->ic_object == obj->oo_inode);
5717 LASSERT(th != NULL);
5719 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5721 ipd = osd_idx_ipd_get(env, bag);
5722 if (unlikely(ipd == NULL))
5725 oh = container_of(th, struct osd_thandle, ot_super);
5726 LASSERT(oh->ot_handle != NULL);
5727 LASSERT(oh->ot_handle->h_transaction != NULL);
5728 if (S_ISDIR(obj->oo_inode->i_mode)) {
5729 iam_rec = (struct iam_rec *)oti->oti_ldp;
5730 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
5732 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5733 /* pack quota uid/gid */
5734 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5735 key = (const struct dt_key *)&oti->oti_quota_id;
5736 /* pack quota record */
5737 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5738 iam_rec = (struct iam_rec *)rec;
5740 iam_rec = (struct iam_rec *)rec;
5743 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5745 osd_ipd_put(env, bag, ipd);
5746 LINVRNT(osd_invariant(obj));
5747 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5752 * Calls ldiskfs_add_entry() to add directory entry
5753 * into the directory. This is required for
5754 * interoperability mode (b11826)
5756 * \retval 0, on success
5757 * \retval -ve, on error
5759 static int __osd_ea_add_rec(struct osd_thread_info *info,
5760 struct osd_object *pobj, struct inode *cinode,
5761 const char *name, const struct lu_fid *fid,
5762 struct htree_lock *hlock, struct thandle *th)
5764 struct ldiskfs_dentry_param *ldp;
5765 struct dentry *child;
5766 struct osd_thandle *oth;
5770 oth = container_of(th, struct osd_thandle, ot_super);
5771 LASSERT(oth->ot_handle != NULL);
5772 LASSERT(oth->ot_handle->h_transaction != NULL);
5773 LASSERT(pobj->oo_inode);
5775 rc = obj_name2lu_name(pobj, name, strlen(name), &ln);
5779 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5780 if (unlikely(osd_object_is_root(pobj)))
5783 osd_get_ldiskfs_dirent_param(ldp, fid);
5784 child = osd_child_dentry_get(info->oti_env, pobj,
5785 ln.ln_name, ln.ln_namelen);
5786 child->d_fsdata = (void *)ldp;
5787 dquot_initialize(pobj->oo_inode);
5788 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5789 child, cinode, hlock);
5790 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5791 struct ldiskfs_dir_entry_2 *de;
5792 struct buffer_head *bh;
5795 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5798 rc1 = osd_ldiskfs_journal_get_write_access(
5799 oth->ot_handle, pobj->oo_inode->i_sb, bh,
5802 if (S_ISDIR(cinode->i_mode))
5803 de->file_type = LDISKFS_DIRENT_LUFID |
5804 LDISKFS_FT_REG_FILE;
5806 de->file_type = LDISKFS_DIRENT_LUFID |
5808 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5815 if (ln.ln_name != name)
5821 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5822 * into the directory.Also sets flags into osd object to
5823 * indicate dot and dotdot are created. This is required for
5824 * interoperability mode (b11826)
5826 * \param dir directory for dot and dotdot fixup.
5827 * \param obj child object for linking
5829 * \retval 0, on success
5830 * \retval -ve, on error
5832 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5833 struct osd_object *dir,
5834 struct inode *parent_dir, const char *name,
5835 const struct lu_fid *dot_fid,
5836 const struct lu_fid *dot_dot_fid,
5839 struct inode *inode = dir->oo_inode;
5840 struct osd_thandle *oth;
5843 oth = container_of(th, struct osd_thandle, ot_super);
5844 LASSERT(oth->ot_handle->h_transaction != NULL);
5845 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
5847 if (strcmp(name, dot) == 0) {
5848 if (dir->oo_compat_dot_created) {
5851 LASSERT(inode->i_ino == parent_dir->i_ino);
5852 dir->oo_compat_dot_created = 1;
5855 } else if (strcmp(name, dotdot) == 0) {
5856 if (!dir->oo_compat_dot_created)
5858 /* in case of rename, dotdot is already created */
5859 if (dir->oo_compat_dotdot_created) {
5860 return __osd_ea_add_rec(info, dir, parent_dir, name,
5861 dot_dot_fid, NULL, th);
5864 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
5865 struct lu_fid tfid = *dot_dot_fid;
5868 result = osd_add_dot_dotdot_internal(info,
5869 dir->oo_inode, parent_dir, dot_fid,
5872 result = osd_add_dot_dotdot_internal(info,
5873 dir->oo_inode, parent_dir, dot_fid,
5878 dir->oo_compat_dotdot_created = 1;
5886 * It will call the appropriate osd_add* function and return the
5887 * value, return by respective functions.
5889 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5890 struct inode *cinode, const char *name,
5891 const struct lu_fid *fid, struct thandle *th)
5893 struct osd_thread_info *info = osd_oti_get(env);
5894 struct htree_lock *hlock;
5897 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5899 if (name[0] == '.' && (name[1] == '\0' ||
5900 (name[1] == '.' && name[2] == '\0'))) {
5901 if (hlock != NULL) {
5902 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5905 down_write(&pobj->oo_ext_idx_sem);
5908 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5909 lu_object_fid(&pobj->oo_dt.do_lu),
5912 if (hlock != NULL) {
5913 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5914 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5916 down_write(&pobj->oo_ext_idx_sem);
5919 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5920 struct lu_fid *tfid = &info->oti_fid;
5924 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5927 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5931 if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
5932 && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
5933 pobj->oo_dirent_count++;
5936 ldiskfs_htree_unlock(hlock);
5938 up_write(&pobj->oo_ext_idx_sem);
5944 osd_ldiskfs_consistency_check(struct osd_thread_info *oti,
5945 struct osd_device *dev,
5946 const struct lu_fid *fid,
5947 struct osd_inode_id *id)
5949 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5950 struct inode *inode = NULL;
5956 if (!scrub_needs_check(scrub, fid, id->oii_ino))
5959 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5960 if (rc == -ENOENT) {
5961 __u32 gen = id->oii_gen;
5967 inode = osd_iget(oti, dev, id);
5968 /* The inode has been removed (by race maybe). */
5969 if (IS_ERR(inode)) {
5970 rc = PTR_ERR(inode);
5972 RETURN(rc == -ESTALE ? -ENOENT : rc);
5975 /* The OI mapping is lost. */
5976 if (gen != OSD_OII_NOGEN)
5980 * The inode may has been reused by others, we do not know,
5981 * leave it to be handled by subsequent osd_fid_lookup().
5984 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5991 if (scrub->os_running) {
5992 if (inode == NULL) {
5993 inode = osd_iget(oti, dev, id);
5994 /* The inode has been removed (by race maybe). */
5995 if (IS_ERR(inode)) {
5996 rc = PTR_ERR(inode);
5998 RETURN(rc == -ESTALE ? -ENOENT : rc);
6002 rc = osd_scrub_oi_insert(dev, fid, id, insert);
6004 * There is race condition between osd_oi_lookup and OI scrub.
6005 * The OI scrub finished just after osd_oi_lookup() failure.
6006 * Under such case, it is unnecessary to trigger OI scrub again,
6007 * but try to call osd_oi_lookup() again.
6009 if (unlikely(rc == -EAGAIN))
6012 if (!S_ISDIR(inode->i_mode))
6015 rc = osd_check_lmv(oti, dev, inode);
6020 if (dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
6022 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
6023 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
6024 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
6025 "%s: trigger partial OI scrub for RPC inconsistency, checking FID "DFID"/%u: rc = %d\n",
6026 osd_dev2name(dev), PFID(fid), id->oii_ino, rc);
6027 if (rc == 0 || rc == -EALREADY)
6039 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
6040 struct osd_device *dev,
6041 struct lu_fid *fid, __u32 ino)
6043 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
6044 struct osd_idmap_cache *oic = &oti->oti_cache;
6045 struct inode *inode;
6048 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
6049 inode = osd_iget(oti, dev, &oic->oic_lid);
6050 if (IS_ERR(inode)) {
6051 fid_zero(&oic->oic_fid);
6052 return PTR_ERR(inode);
6055 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
6058 fid_zero(&oic->oic_fid);
6060 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
6064 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
6065 struct osd_inode_id *id, const struct lu_fid *fid)
6067 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
6068 id->oii_ino, id->oii_gen, info);
6069 info->oti_cache.oic_lid = *id;
6070 info->oti_cache.oic_fid = *fid;
6071 info->oti_cache.oic_dev = osd;
6075 * Get parent FID from the linkEA.
6077 * For a directory which parent resides on remote MDT, to satisfy the
6078 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
6079 * the other hand, to make the lookup(..) on the directory can return
6080 * the real parent FID, we append the real parent FID after its ".."
6081 * name entry in the /REMOTE_PARENT_DIR.
6083 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
6084 * backup. So after the restore, we cannot get the right parent FID from
6085 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
6086 * we have stored the real parent FID in the directory object's linkEA,
6087 * we can parse the linkEA for the real parent FID.
6089 * \param[in] env pointer to the thread context
6090 * \param[in] obj pointer to the object to be handled
6091 * \param[out]fid pointer to the buffer to hold the parent FID
6093 * \retval 0 for getting the real parent FID successfully
6094 * \retval negative error number on failure
6096 static int osd_get_pfid_from_linkea(const struct lu_env *env,
6097 struct osd_object *obj,
6100 struct osd_thread_info *oti = osd_oti_get(env);
6101 struct lu_buf *buf = &oti->oti_big_buf;
6102 struct dentry *dentry = &oti->oti_obj_dentry;
6103 struct inode *inode = obj->oo_inode;
6104 struct linkea_data ldata = { NULL };
6110 if (!S_ISDIR(inode->i_mode))
6114 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6115 buf->lb_buf, buf->lb_len);
6116 if (rc == -ERANGE) {
6117 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6120 lu_buf_realloc(buf, rc);
6121 if (buf->lb_buf == NULL)
6128 if (unlikely(rc == 0))
6134 if (unlikely(buf->lb_buf == NULL)) {
6135 lu_buf_realloc(buf, rc);
6136 if (buf->lb_buf == NULL)
6143 rc = linkea_init_with_rec(&ldata);
6145 linkea_first_entry(&ldata);
6146 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
6152 static int osd_verify_ent_by_linkea(const struct lu_env *env,
6153 struct inode *inode,
6154 const struct lu_fid *pfid,
6155 const char *name, const int namelen)
6157 struct osd_thread_info *oti = osd_oti_get(env);
6158 struct lu_buf *buf = &oti->oti_big_buf;
6159 struct dentry *dentry = &oti->oti_obj_dentry;
6160 struct linkea_data ldata = { NULL };
6161 struct lu_name cname = { .ln_name = name,
6162 .ln_namelen = namelen };
6168 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6169 buf->lb_buf, buf->lb_len);
6171 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
6176 if (unlikely(rc == 0))
6179 if (buf->lb_len < rc) {
6180 lu_buf_realloc(buf, rc);
6181 if (buf->lb_buf == NULL)
6188 rc = linkea_init_with_rec(&ldata);
6190 rc = linkea_links_find(&ldata, &cname, pfid);
6196 * Calls ->lookup() to find dentry. From dentry get inode and
6197 * read inode's ea to get fid. This is required for interoperability
6200 * \retval 0, on success
6201 * \retval -ve, on error
6203 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
6204 struct dt_rec *rec, const struct dt_key *key)
6206 struct inode *dir = obj->oo_inode;
6207 struct dentry *dentry;
6208 struct ldiskfs_dir_entry_2 *de;
6209 struct buffer_head *bh;
6210 struct lu_fid *fid = (struct lu_fid *)rec;
6211 struct htree_lock *hlock = NULL;
6218 LASSERT(dir->i_op != NULL);
6219 LASSERT(dir->i_op->lookup != NULL);
6221 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
6225 dentry = osd_child_dentry_get(env, obj, ln.ln_name, ln.ln_namelen);
6227 if (obj->oo_hl_head != NULL) {
6228 hlock = osd_oti_get(env)->oti_hlock;
6229 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6230 dir, LDISKFS_HLOCK_LOOKUP);
6232 down_read(&obj->oo_ext_idx_sem);
6235 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6237 struct osd_thread_info *oti = osd_oti_get(env);
6238 struct osd_inode_id *id = &oti->oti_id;
6239 struct osd_device *dev = osd_obj2dev(obj);
6241 ino = le32_to_cpu(de->inode);
6242 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
6244 rc = osd_fail_fid_lookup(oti, dev, fid, ino);
6248 rc = osd_get_fid_from_dentry(de, rec);
6250 /* done with de, release bh */
6253 if (unlikely(is_remote_parent_ino(dev, ino))) {
6254 const char *name = (const char *)key;
6257 * If the parent is on remote MDT, and there
6258 * is no FID-in-dirent, then we have to get
6259 * the parent FID from the linkEA.
6261 if (likely(strlen(name) == 2 &&
6262 name[0] == '.' && name[1] == '.'))
6263 rc = osd_get_pfid_from_linkea(env, obj,
6266 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6269 osd_id_gen(id, ino, OSD_OII_NOGEN);
6272 if (rc != 0 || osd_remote_fid(env, dev, fid))
6275 rc = osd_ldiskfs_consistency_check(oti, dev, fid, id);
6276 if (rc != -ENOENT) {
6277 /* Other error should not affect lookup result. */
6280 /* Normal file mapping should be added into OI cache
6281 * after FID in LMA check, but for local files like
6282 * hsm_actions, their FIDs are not stored in OI files,
6283 * see osd_initial_OI_scrub(), and here is the only
6284 * place to load mapping into OI cache.
6286 if (!fid_is_namespace_visible(fid))
6287 osd_add_oi_cache(osd_oti_get(env),
6288 osd_obj2dev(obj), id, fid);
6290 CDEBUG(D_INODE, DFID"/"DNAME" => "DFID"\n",
6291 PFID(lu_object_fid(&obj->oo_dt.do_lu)), PNAME(&ln),
6301 ldiskfs_htree_unlock(hlock);
6303 up_read(&obj->oo_ext_idx_sem);
6304 if (ln.ln_name != (char *)key)
6309 static int osd_index_declare_ea_insert(const struct lu_env *env,
6310 struct dt_object *dt,
6311 const struct dt_rec *rec,
6312 const struct dt_key *key,
6313 struct thandle *handle)
6315 struct osd_thandle *oh;
6316 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6317 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6318 const struct lu_fid *fid = rec1->rec_fid;
6319 int credits, rc = 0;
6320 struct osd_idmap_cache *idc;
6324 LASSERT(!dt_object_remote(dt));
6325 LASSERT(handle != NULL);
6326 LASSERT(fid != NULL);
6327 LASSERT(rec1->rec_type != 0);
6329 oh = container_of(handle, struct osd_thandle, ot_super);
6330 LASSERT(oh->ot_handle == NULL);
6332 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
6335 * we can't call iget() while a transactions is running
6336 * (this can lead to a deadlock), but we need to know
6337 * inum and object type. so we find this information at
6338 * declaration and cache in per-thread info
6340 idc = osd_idc_find_or_init(env, osd, fid);
6342 RETURN(PTR_ERR(idc));
6343 if (idc->oic_remote) {
6345 * a reference to remote inode is represented by an
6346 * agent inode which we have to create
6348 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
6349 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
6352 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
6354 if (osd_dt_obj(dt)->oo_inode != NULL) {
6355 struct inode *inode = osd_dt_obj(dt)->oo_inode;
6358 * We ignore block quota on meta pool (MDTs), so needn't
6359 * calculate how many blocks will be consumed by this index
6362 rc = osd_declare_inode_qid(env, i_uid_read(inode),
6364 i_projid_read(inode), 0,
6365 oh, osd_dt_obj(dt), NULL,
6370 #ifdef HAVE_PROJECT_QUOTA
6372 * Reserve credits for local agent inode to transfer
6373 * to 0, quota enforcement is ignored in this case.
6375 if (idc->oic_remote &&
6376 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
6377 i_projid_read(inode) != 0)
6378 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
6379 0, i_projid_read(inode),
6380 0, false, PRJQUOTA);
6388 * Index add function for interoperability mode (b11826).
6389 * It will add the directory entry.This entry is needed to
6390 * maintain name->fid mapping.
6392 * \param key it is key i.e. file entry to be inserted
6393 * \param rec it is value of given key i.e. fid
6395 * \retval 0, on success
6396 * \retval -ve, on error
6398 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
6399 const struct dt_rec *rec,
6400 const struct dt_key *key, struct thandle *th)
6402 struct osd_object *obj = osd_dt_obj(dt);
6403 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6404 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6405 const struct lu_fid *fid = rec1->rec_fid;
6406 const char *name = (const char *)key;
6407 struct osd_thread_info *oti = osd_oti_get(env);
6408 struct inode *child_inode = NULL;
6409 struct osd_idmap_cache *idc;
6414 if (!dt_object_exists(dt))
6417 LASSERT(osd_invariant(obj));
6418 LASSERT(!dt_object_remote(dt));
6419 LASSERT(th != NULL);
6421 osd_trans_exec_op(env, th, OSD_OT_INSERT);
6423 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
6425 idc = osd_idc_find(env, osd, fid);
6426 if (unlikely(idc == NULL)) {
6427 idc = osd_idc_find_or_init(env, osd, fid);
6430 * this dt_insert() wasn't declared properly, so
6431 * FID is missing in OI cache. we better do not
6432 * lookup FID in FLDB/OI and don't risk to deadlock,
6433 * but in some special cases (lfsck testing, etc)
6434 * it's much simpler than fixing a caller.
6436 * normally this error should be placed after the first
6437 * find, but migrate may attach source stripes to
6438 * target, which doesn't create stripes.
6440 CERROR("%s: "DFID" wasn't declared for insert\n",
6441 osd_name(osd), PFID(fid));
6443 RETURN(PTR_ERR(idc));
6447 if (idc->oic_remote) {
6448 /* Insert remote entry */
6449 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
6451 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
6453 child_inode = osd_create_local_agent_inode(env, osd,
6454 obj, fid, rec1->rec_type & S_IFMT, th);
6455 if (IS_ERR(child_inode))
6456 RETURN(PTR_ERR(child_inode));
6459 /* Insert local entry */
6460 if (unlikely(idc->oic_lid.oii_ino == 0)) {
6461 /* for a reason OI cache wasn't filled properly */
6462 CERROR("%s: OIC for "DFID" isn't filled\n",
6463 osd_name(osd), PFID(fid));
6466 child_inode = oti->oti_inode;
6467 if (unlikely(child_inode == NULL)) {
6468 struct ldiskfs_inode_info *lii;
6473 child_inode = oti->oti_inode = &lii->vfs_inode;
6475 child_inode->i_sb = osd_sb(osd);
6476 child_inode->i_ino = idc->oic_lid.oii_ino;
6477 child_inode->i_mode = rec1->rec_type & S_IFMT;
6480 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
6482 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
6483 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
6485 if (child_inode && child_inode != oti->oti_inode)
6487 LASSERT(osd_invariant(obj));
6488 osd_trans_exec_check(env, th, OSD_OT_INSERT);
6494 * Initialize osd Iterator for given osd index object.
6496 * \param dt osd index object
6499 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
6500 struct dt_object *dt,
6503 struct osd_it_iam *it;
6504 struct osd_object *obj = osd_dt_obj(dt);
6505 struct lu_object *lo = &dt->do_lu;
6506 struct iam_path_descr *ipd;
6507 struct iam_container *bag = &obj->oo_dir->od_container;
6509 if (!dt_object_exists(dt))
6510 return ERR_PTR(-ENOENT);
6514 return ERR_PTR(-ENOMEM);
6516 ipd = osd_it_ipd_get(env, bag);
6517 if (likely(ipd != NULL)) {
6521 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
6522 return (struct dt_it *)it;
6525 return ERR_PTR(-ENOMEM);
6530 * free given Iterator.
6532 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
6534 struct osd_it_iam *it = (struct osd_it_iam *)di;
6535 struct osd_object *obj = it->oi_obj;
6537 iam_it_fini(&it->oi_it);
6538 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
6539 osd_object_put(env, obj);
6544 * Move Iterator to record specified by \a key
6546 * \param di osd iterator
6547 * \param key key for index
6549 * \retval +ve di points to record with least key not larger than key
6550 * \retval 0 di points to exact matched key
6551 * \retval -ve failure
6554 static int osd_it_iam_get(const struct lu_env *env,
6555 struct dt_it *di, const struct dt_key *key)
6557 struct osd_thread_info *oti = osd_oti_get(env);
6558 struct osd_it_iam *it = (struct osd_it_iam *)di;
6560 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6561 /* swab quota uid/gid */
6562 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
6563 key = (struct dt_key *)&oti->oti_quota_id;
6566 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
6572 * \param di osd iterator
6574 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
6576 struct osd_it_iam *it = (struct osd_it_iam *)di;
6578 iam_it_put(&it->oi_it);
6582 * Move iterator by one record
6584 * \param di osd iterator
6586 * \retval +1 end of container reached
6588 * \retval -ve failure
6591 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
6593 struct osd_it_iam *it = (struct osd_it_iam *)di;
6595 return iam_it_next(&it->oi_it);
6599 * Return pointer to the key under iterator.
6602 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
6603 const struct dt_it *di)
6605 struct osd_thread_info *oti = osd_oti_get(env);
6606 struct osd_it_iam *it = (struct osd_it_iam *)di;
6607 struct osd_object *obj = it->oi_obj;
6610 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
6612 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
6613 /* swab quota uid/gid */
6614 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
6615 key = (struct dt_key *)&oti->oti_quota_id;
6622 * Return size of key under iterator (in bytes)
6625 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
6627 struct osd_it_iam *it = (struct osd_it_iam *)di;
6629 return iam_it_key_size(&it->oi_it);
6633 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
6635 /* check if file type is required */
6636 if (ent->lde_attrs & LUDA_TYPE) {
6637 struct luda_type *lt;
6638 int align = sizeof(*lt) - 1;
6640 len = (len + align) & ~align;
6641 lt = (struct luda_type *)(ent->lde_name + len);
6642 lt->lt_type = cpu_to_le16(DTTOIF(type));
6645 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
6649 * build lu direct from backend fs dirent.
6653 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
6654 char *name, __u16 namelen, __u16 type, __u32 attr)
6656 ent->lde_attrs = attr | LUDA_FID;
6657 fid_cpu_to_le(&ent->lde_fid, fid);
6659 ent->lde_hash = cpu_to_le64(offset);
6660 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
6662 strncpy(ent->lde_name, name, namelen);
6663 ent->lde_name[namelen] = '\0';
6664 ent->lde_namelen = cpu_to_le16(namelen);
6666 /* append lustre attributes */
6667 osd_it_append_attrs(ent, namelen, type);
6671 * Return pointer to the record under iterator.
6673 static int osd_it_iam_rec(const struct lu_env *env,
6674 const struct dt_it *di,
6675 struct dt_rec *dtrec, __u32 attr)
6677 struct osd_it_iam *it = (struct osd_it_iam *)di;
6678 struct osd_thread_info *info = osd_oti_get(env);
6682 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6683 const struct osd_fid_pack *rec;
6684 struct lu_fid *fid = &info->oti_fid;
6685 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6691 name = (char *)iam_it_key_get(&it->oi_it);
6693 RETURN(PTR_ERR(name));
6695 namelen = iam_it_key_size(&it->oi_it);
6697 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6699 RETURN(PTR_ERR(rec));
6701 rc = osd_fid_unpack(fid, rec);
6705 hash = iam_it_store(&it->oi_it);
6707 /* IAM does not store object type in IAM index (dir) */
6708 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6710 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6711 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6712 (struct iam_rec *)dtrec);
6713 osd_quota_unpack(it->oi_obj, dtrec);
6715 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6716 (struct iam_rec *)dtrec);
6723 * Returns cookie for current Iterator position.
6725 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6727 struct osd_it_iam *it = (struct osd_it_iam *)di;
6729 return iam_it_store(&it->oi_it);
6733 * Restore iterator from cookie.
6735 * \param di osd iterator
6736 * \param hash Iterator location cookie
6738 * \retval +ve di points to record with least key not larger than key.
6739 * \retval 0 di points to exact matched key
6740 * \retval -ve failure
6743 static int osd_it_iam_load(const struct lu_env *env,
6744 const struct dt_it *di, __u64 hash)
6746 struct osd_it_iam *it = (struct osd_it_iam *)di;
6748 return iam_it_load(&it->oi_it, hash);
6751 static const struct dt_index_operations osd_index_iam_ops = {
6752 .dio_lookup = osd_index_iam_lookup,
6753 .dio_declare_insert = osd_index_declare_iam_insert,
6754 .dio_insert = osd_index_iam_insert,
6755 .dio_declare_delete = osd_index_declare_iam_delete,
6756 .dio_delete = osd_index_iam_delete,
6758 .init = osd_it_iam_init,
6759 .fini = osd_it_iam_fini,
6760 .get = osd_it_iam_get,
6761 .put = osd_it_iam_put,
6762 .next = osd_it_iam_next,
6763 .key = osd_it_iam_key,
6764 .key_size = osd_it_iam_key_size,
6765 .rec = osd_it_iam_rec,
6766 .store = osd_it_iam_store,
6767 .load = osd_it_iam_load
6771 struct osd_it_ea *osd_it_dir_init(const struct lu_env *env,
6772 struct osd_device *dev,
6773 struct inode *inode, u32 attr)
6775 struct osd_thread_info *info = osd_oti_get(env);
6776 struct osd_it_ea *oie;
6780 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
6783 RETURN(ERR_CAST(file));
6785 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6786 if (attr & LUDA_64BITHASH)
6787 file->f_mode |= FMODE_64BITHASH;
6789 file->f_mode |= FMODE_32BITHASH;
6792 OBD_SLAB_ALLOC_PTR(oie, osd_itea_cachep);
6796 oie->oie_rd_dirent = 0;
6797 oie->oie_it_dirent = 0;
6798 oie->oie_dirent = NULL;
6799 if (unlikely(!info->oti_it_ea_buf_used)) {
6800 oie->oie_buf = info->oti_it_ea_buf;
6801 info->oti_it_ea_buf_used = 1;
6803 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6807 oie->oie_obj = NULL;
6808 oie->oie_file = file;
6813 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6817 return ERR_PTR(-ENOMEM);
6821 * Creates or initializes iterator context.
6823 * \retval struct osd_it_ea, iterator structure on success
6826 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6827 struct dt_object *dt,
6830 struct osd_object *obj = osd_dt_obj(dt);
6831 struct osd_device *dev = osd_obj2dev(obj);
6832 struct lu_object *lo = &dt->do_lu;
6833 struct osd_it_ea *oie;
6837 if (!dt_object_exists(dt) || obj->oo_destroyed)
6838 RETURN(ERR_PTR(-ENOENT));
6840 oie = osd_it_dir_init(env, dev, obj->oo_inode, attr);
6842 RETURN(ERR_CAST(oie));
6846 RETURN((struct dt_it *)oie);
6849 void osd_it_dir_fini(const struct lu_env *env, struct osd_it_ea *oie,
6850 struct inode *inode)
6852 struct osd_thread_info *info = osd_oti_get(env);
6855 fput(oie->oie_file);
6856 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
6857 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6859 info->oti_it_ea_buf_used = 0;
6860 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6865 * Destroy or finishes iterator context.
6867 * \param di iterator structure to be destroyed
6869 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
6871 struct osd_it_ea *oie = (struct osd_it_ea *)di;
6872 struct osd_object *obj = oie->oie_obj;
6873 struct inode *inode = obj->oo_inode;
6876 osd_it_dir_fini(env, (struct osd_it_ea *)di, inode);
6877 osd_object_put(env, obj);
6882 * It position the iterator at given key, so that next lookup continues from
6883 * that key Or it is similar to dio_it->load() but based on a key,
6884 * rather than file position.
6886 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
6889 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
6891 static int osd_it_ea_get(const struct lu_env *env,
6892 struct dt_it *di, const struct dt_key *key)
6894 struct osd_it_ea *it = (struct osd_it_ea *)di;
6897 LASSERT(((const char *)key)[0] == '\0');
6898 it->oie_file->f_pos = 0;
6899 it->oie_rd_dirent = 0;
6900 it->oie_it_dirent = 0;
6901 it->oie_dirent = NULL;
6909 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
6913 struct osd_filldir_cbs {
6914 struct dir_context ctx;
6915 struct osd_it_ea *it;
6918 * It is called internally by ->iterate*(). It fills the
6919 * iterator's in-memory data structure with required
6920 * information i.e. name, namelen, rec_size etc.
6922 * \param buf in which information to be filled in.
6923 * \param name name of the file in given dir
6925 * \retval 0 on success
6926 * \retval 1 on buffer full
6928 #ifdef HAVE_FILLDIR_USE_CTX
6929 static int osd_ldiskfs_filldir(struct dir_context *ctx,
6931 static int osd_ldiskfs_filldir(void *ctx,
6933 const char *name, int namelen,
6934 loff_t offset, __u64 ino, unsigned int d_type)
6936 struct osd_it_ea *it = ((struct osd_filldir_cbs *)ctx)->it;
6937 struct osd_object *obj = it->oie_obj;
6938 struct osd_it_ea_dirent *ent = it->oie_dirent;
6939 struct lu_fid *fid = &ent->oied_fid;
6940 char *buf = it->oie_buf;
6941 struct osd_fid_pack *rec;
6944 /* this should never happen */
6945 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6946 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6950 /* Check for enough space. Note oied_name is not NUL terminated. */
6951 if (&ent->oied_name[namelen] > buf + OSD_IT_EA_BUFSIZE)
6954 /* "." is just the object itself. */
6955 if (namelen == 1 && name[0] == '.') {
6957 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6958 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6959 rec = (struct osd_fid_pack *)(name + namelen + 1);
6960 if (osd_fid_unpack(fid, rec) != 0)
6965 d_type &= ~LDISKFS_DIRENT_LUFID;
6967 /* NOT export local root. */
6969 unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6970 ino = obj->oo_inode->i_ino;
6971 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6974 if (obj == NULL || !(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
6975 ent->oied_namelen = namelen;
6976 memcpy(ent->oied_name, name, namelen);
6978 int encoded_namelen = critical_chars(name, namelen);
6980 /* Check again for enough space. */
6981 if (&ent->oied_name[encoded_namelen] > buf + OSD_IT_EA_BUFSIZE)
6984 ent->oied_namelen = encoded_namelen;
6986 if (encoded_namelen == namelen)
6987 memcpy(ent->oied_name, name, namelen);
6989 critical_encode(name, namelen, ent->oied_name);
6992 ent->oied_ino = ino;
6993 ent->oied_off = offset;
6994 ent->oied_type = d_type;
6996 it->oie_rd_dirent++;
6997 it->oie_dirent = (void *)ent + cfs_size_round(sizeof(*ent) + ent->oied_namelen);
7002 * Calls ->iterate*() to load a directory entry at a time
7003 * and stored it in iterator's in-memory data structure.
7005 * \param di iterator's in memory structure
7007 * \retval 0 on success
7008 * \retval -ve on error
7009 * \retval +1 reach the end of entry
7011 int osd_ldiskfs_it_fill(const struct lu_env *env, const struct dt_it *di)
7013 struct osd_it_ea *it = (struct osd_it_ea *)di;
7014 struct osd_object *obj = it->oie_obj;
7015 struct htree_lock *hlock = NULL;
7016 struct file *filp = it->oie_file;
7018 struct osd_filldir_cbs buf = {
7019 .ctx.actor = osd_ldiskfs_filldir,
7024 it->oie_dirent = it->oie_buf;
7025 it->oie_rd_dirent = 0;
7028 if (obj->oo_hl_head != NULL) {
7029 hlock = osd_oti_get(env)->oti_hlock;
7030 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
7032 LDISKFS_HLOCK_READDIR);
7034 down_read(&obj->oo_ext_idx_sem);
7038 rc = iterate_dir(filp, &buf.ctx);
7042 if (it->oie_rd_dirent == 0) {
7044 * If it does not get any dirent, it means it has been reached
7045 * to the end of the dir
7047 it->oie_file->f_pos = ldiskfs_get_htree_eof(it->oie_file);
7051 it->oie_dirent = it->oie_buf;
7052 it->oie_it_dirent = 1;
7057 ldiskfs_htree_unlock(hlock);
7059 up_read(&obj->oo_ext_idx_sem);
7066 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7067 * to load a directory entry at a time and stored it in
7068 * iterator's in-memory data structure.
7070 * \param di iterator's in memory structure
7072 * \retval +ve iterator reached to end
7073 * \retval 0 iterator not reached to end
7074 * \retval -ve on error
7076 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
7078 struct osd_it_ea *it = (struct osd_it_ea *)di;
7083 if (it->oie_it_dirent < it->oie_rd_dirent) {
7085 (void *)it->oie_dirent +
7086 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
7087 it->oie_dirent->oied_namelen);
7088 it->oie_it_dirent++;
7091 if (it->oie_file->f_pos == ldiskfs_get_htree_eof(it->oie_file))
7094 rc = osd_ldiskfs_it_fill(env, di);
7101 * Returns the key at current position from iterator's in memory structure.
7103 * \param di iterator's in memory structure
7105 * \retval key i.e. struct dt_key on success
7107 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
7108 const struct dt_it *di)
7110 struct osd_it_ea *it = (struct osd_it_ea *)di;
7112 return (struct dt_key *)it->oie_dirent->oied_name;
7116 * Returns key's size at current position from iterator's in memory structure.
7118 * \param di iterator's in memory structure
7120 * \retval key_size i.e. struct dt_key on success
7122 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
7124 struct osd_it_ea *it = (struct osd_it_ea *)di;
7126 return it->oie_dirent->oied_namelen;
7129 #if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
7130 #undef LDISKFS_DIR_REC_LEN
7131 # if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7132 # define LDISKFS_DIR_REC_LEN(de, dir) LDISKFS_DIR_ENTRY_LEN_((de), (dir))
7134 # define LDISKFS_DIR_REC_LEN(de) LDISKFS_DIR_ENTRY_LEN_((de))
7138 #if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7139 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de), NULL)
7141 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de))
7144 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
7146 if (LDISKFS_DIR_REC_LEN_DIR(de) >=
7147 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
7154 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
7155 unsigned int blocksize, bool dotdot)
7158 return osd_dotdot_has_space(de);
7160 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
7161 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
7168 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
7169 handle_t *jh, struct dentry *dentry,
7170 const struct lu_fid *fid, struct buffer_head *bh,
7171 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
7174 struct inode *dir = dentry->d_parent->d_inode;
7175 struct inode *inode = dentry->d_inode;
7176 struct osd_fid_pack *rec;
7177 struct ldiskfs_dentry_param *ldp;
7178 int namelen = dentry->d_name.len;
7180 struct osd_thread_info *info = osd_oti_get(env);
7184 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
7187 /* There is enough space to hold the FID-in-dirent. */
7188 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
7189 rc = osd_ldiskfs_journal_get_write_access(jh, dir->i_sb, bh,
7194 de->name[namelen] = 0;
7195 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
7196 rec->fp_len = sizeof(struct lu_fid) + 1;
7197 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
7198 de->file_type |= LDISKFS_DIRENT_LUFID;
7199 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
7206 rc = ldiskfs_delete_entry(jh, dir, de, bh);
7210 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
7211 osd_get_ldiskfs_dirent_param(ldp, fid);
7212 dentry->d_fsdata = (void *)ldp;
7213 dquot_initialize(dir);
7214 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
7216 * It is too bad, we cannot reinsert the name entry back.
7217 * That means we lose it!
7221 "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
7222 osd_ino2name(inode), dir->i_ino, dir->i_generation,
7223 namelen, dentry->d_name.name, PFID(fid), rc);
7229 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
7230 struct osd_it_ea *it, struct lu_fid *fid,
7231 struct osd_inode_id *id, __u32 *attr)
7233 struct osd_thread_info *info = osd_oti_get(env);
7234 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
7235 struct osd_device *dev = osd_obj2dev(obj);
7236 struct super_block *sb = osd_sb(dev);
7237 const char *devname = osd_name(dev);
7238 struct osd_it_ea_dirent *ent = it->oie_dirent;
7239 struct inode *dir = obj->oo_inode;
7240 struct htree_lock *hlock = NULL;
7241 struct buffer_head *bh = NULL;
7242 handle_t *jh = NULL;
7243 struct ldiskfs_dir_entry_2 *de;
7244 struct dentry *dentry;
7245 struct inode *inode;
7246 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
7249 bool dotdot = false;
7255 if (ent->oied_name[0] == '.') {
7256 if (ent->oied_namelen == 1)
7259 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
7263 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
7264 inode = osd_iget(info, dev, id);
7265 if (IS_ERR(inode)) {
7266 rc = PTR_ERR(inode);
7267 if (rc == -ENOENT || rc == -ESTALE) {
7269 * Maybe dangling name entry, or
7270 * corrupted directory entry.
7272 *attr |= LUDA_UNKNOWN;
7275 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
7276 "check_repair, dir = %lu/%u, name = %.*s, "
7277 "ino = %llu, rc = %d\n",
7278 devname, dir->i_ino, dir->i_generation,
7279 ent->oied_namelen, ent->oied_name,
7286 rc = obj_name2lu_name(obj, ent->oied_name, ent->oied_namelen, &ln);
7290 dentry = osd_child_dentry_by_inode(env, dir, ln.ln_name, ln.ln_namelen);
7291 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
7292 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
7298 * We need to ensure that the name entry is still valid.
7299 * Because it may be removed or renamed by other already.
7301 * The unlink or rename operation will start journal before PDO lock,
7302 * so to avoid deadlock, here we need to start journal handle before
7303 * related PDO lock also. But because we do not know whether there
7304 * will be something to be repaired before PDO lock, we just start
7305 * journal without conditions.
7307 * We may need to remove the name entry firstly, then insert back.
7308 * One credit is for user quota file update.
7309 * One credit is for group quota file update.
7310 * Two credits are for dirty inode.
7312 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
7313 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
7315 if (dev->od_dirent_journal != 0) {
7318 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
7321 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
7322 "check_repair, dir = %lu/%u, credits = %d, "
7323 "name = %.*s, ino = %llu: rc = %d\n",
7324 devname, dir->i_ino, dir->i_generation, credits,
7325 ent->oied_namelen, ent->oied_name,
7328 GOTO(out_inode, rc);
7331 if (obj->oo_hl_head != NULL) {
7332 hlock = osd_oti_get(env)->oti_hlock;
7334 * "0" means exclusive lock for the whole directory.
7335 * We need to prevent others access such name entry
7336 * during the delete + insert. Neither HLOCK_ADD nor
7337 * HLOCK_DEL cannot guarantee the atomicity.
7339 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
7341 down_write(&obj->oo_ext_idx_sem);
7344 if (obj->oo_hl_head != NULL) {
7345 hlock = osd_oti_get(env)->oti_hlock;
7346 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
7347 LDISKFS_HLOCK_LOOKUP);
7349 down_read(&obj->oo_ext_idx_sem);
7353 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
7354 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
7355 *attr |= LUDA_IGNORE;
7361 * For dotdot entry, if there is not enough space to hold the
7362 * FID-in-dirent, just keep them there. It only happens when the
7363 * device upgraded from 1.8 or restored from MDT file-level backup.
7364 * For the whole directory, only dotdot entry have no FID-in-dirent
7365 * and needs to get FID from LMA when readdir, it will not affect the
7368 if (dotdot && !osd_dotdot_has_space(de)) {
7369 *attr |= LUDA_UNKNOWN;
7375 if (lu_fid_eq(fid, &lma->lma_self_fid))
7378 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
7379 struct lu_fid *tfid = &lma->lma_self_fid;
7381 if (likely(dotdot &&
7382 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
7383 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
7385 * It must be REMOTE_PARENT_DIR and as the
7386 * 'dotdot' entry of remote directory
7388 *attr |= LUDA_IGNORE;
7390 CDEBUG(D_LFSCK, "%s: expect remote agent "
7391 "parent directory, but got %.*s under "
7392 "dir = %lu/%u with the FID "DFID"\n",
7393 devname, ent->oied_namelen,
7394 ent->oied_name, dir->i_ino,
7395 dir->i_generation, PFID(tfid));
7397 *attr |= LUDA_UNKNOWN;
7404 if (!fid_is_zero(fid)) {
7405 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
7407 if (rc == -ENOENT ||
7409 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
7411 * linkEA does not recognize the dirent entry,
7412 * it may because the dirent entry corruption
7413 * and points to other's inode.
7415 CDEBUG(D_LFSCK, "%s: the target inode does not "
7416 "recognize the dirent, dir = %lu/%u, "
7417 " name = %.*s, ino = %llu, "
7418 DFID": rc = %d\n", devname, dir->i_ino,
7419 dir->i_generation, ent->oied_namelen,
7420 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7421 *attr |= LUDA_UNKNOWN;
7426 if (rc && rc != -ENODATA) {
7427 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
7428 "dir = %lu/%u, name = %.*s, ino = %llu, "
7429 DFID": rc = %d\n", devname, dir->i_ino,
7430 dir->i_generation, ent->oied_namelen,
7431 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7432 *attr |= LUDA_UNKNOWN;
7440 * linkEA recognizes the dirent entry, the FID-in-LMA is
7441 * valid, trusted, in spite of fid_is_sane(fid) or not.
7443 if (*attr & LUDA_VERIFY_DRYRUN) {
7444 *fid = lma->lma_self_fid;
7445 *attr |= LUDA_REPAIR;
7452 dev->od_dirent_journal = 1;
7453 if (hlock != NULL) {
7454 ldiskfs_htree_unlock(hlock);
7457 up_read(&obj->oo_ext_idx_sem);
7463 *fid = lma->lma_self_fid;
7465 /* Update or append the FID-in-dirent. */
7466 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7467 bh, de, hlock, dotdot);
7469 *attr |= LUDA_REPAIR;
7471 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
7472 "the dirent, dir = %lu/%u, name = %.*s, "
7473 "ino = %llu, "DFID": rc = %d\n",
7474 devname, dir->i_ino, dir->i_generation,
7475 ent->oied_namelen, ent->oied_name,
7476 ent->oied_ino, PFID(fid), rc);
7478 /* lma is NULL, trust the FID-in-dirent if it is valid. */
7479 if (*attr & LUDA_VERIFY_DRYRUN) {
7480 if (fid_is_sane(fid)) {
7481 *attr |= LUDA_REPAIR;
7482 } else if (dev->od_index == 0) {
7483 lu_igif_build(fid, inode->i_ino,
7484 inode->i_generation);
7485 *attr |= LUDA_UPGRADE;
7493 dev->od_dirent_journal = 1;
7494 if (hlock != NULL) {
7495 ldiskfs_htree_unlock(hlock);
7498 up_read(&obj->oo_ext_idx_sem);
7505 if (unlikely(fid_is_sane(fid))) {
7507 * FID-in-dirent exists, but FID-in-LMA is lost.
7508 * Trust the FID-in-dirent, and add FID-in-LMA.
7510 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
7512 *attr |= LUDA_REPAIR;
7514 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
7515 "update dirent, dir = %lu/%u, "
7516 "name = %.*s, ino = %llu, "
7518 devname, dir->i_ino, dir->i_generation,
7519 ent->oied_namelen, ent->oied_name,
7520 ent->oied_ino, PFID(fid), rc);
7521 } else if (dev->od_index == 0) {
7522 lu_igif_build(fid, inode->i_ino, inode->i_generation);
7524 * It is probably IGIF object. Only aappend the
7525 * FID-in-dirent. OI scrub will process FID-in-LMA.
7527 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7528 bh, de, hlock, dotdot);
7530 *attr |= LUDA_UPGRADE;
7532 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
7533 "after the dirent, dir = %lu/%u, "
7534 "name = %.*s, ino = %llu, "
7536 devname, dir->i_ino, dir->i_generation,
7537 ent->oied_namelen, ent->oied_name,
7538 ent->oied_ino, PFID(fid), rc);
7547 if (hlock != NULL) {
7548 ldiskfs_htree_unlock(hlock);
7550 if (dev->od_dirent_journal != 0)
7551 up_write(&obj->oo_ext_idx_sem);
7553 up_read(&obj->oo_ext_idx_sem);
7557 ldiskfs_journal_stop(jh);
7561 if (rc >= 0 && !dirty)
7562 dev->od_dirent_journal = 0;
7563 if (ln.ln_name != ent->oied_name)
7570 * Returns the value at current position from iterator's in memory structure.
7572 * \param di struct osd_it_ea, iterator's in memory structure
7573 * \param attr attr requested for dirent.
7574 * \param lde lustre dirent
7576 * \retval 0 no error and \param lde has correct lustre dirent.
7577 * \retval -ve on error
7579 static inline int osd_it_ea_rec(const struct lu_env *env,
7580 const struct dt_it *di,
7581 struct dt_rec *dtrec, __u32 attr)
7583 struct osd_it_ea *it = (struct osd_it_ea *)di;
7584 struct osd_object *obj = it->oie_obj;
7585 struct osd_device *dev = osd_obj2dev(obj);
7586 struct osd_thread_info *oti = osd_oti_get(env);
7587 struct osd_inode_id *id = &oti->oti_id;
7588 struct lu_fid *fid = &it->oie_dirent->oied_fid;
7589 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
7590 __u32 ino = it->oie_dirent->oied_ino;
7595 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
7597 if (attr & LUDA_VERIFY) {
7598 if (unlikely(is_remote_parent_ino(dev, ino))) {
7599 attr |= LUDA_IGNORE;
7601 * If the parent is on remote MDT, and there
7602 * is no FID-in-dirent, then we have to get
7603 * the parent FID from the linkEA.
7605 if (!fid_is_sane(fid) &&
7606 it->oie_dirent->oied_namelen == 2 &&
7607 it->oie_dirent->oied_name[0] == '.' &&
7608 it->oie_dirent->oied_name[1] == '.')
7609 osd_get_pfid_from_linkea(env, obj, fid);
7611 rc = osd_dirent_check_repair(env, obj, it, fid, id,
7615 if (!fid_is_sane(fid))
7616 attr |= LUDA_UNKNOWN;
7618 attr &= ~LU_DIRENT_ATTRS_MASK;
7619 if (!fid_is_sane(fid)) {
7620 bool is_dotdot = false;
7622 if (it->oie_dirent->oied_namelen == 2 &&
7623 it->oie_dirent->oied_name[0] == '.' &&
7624 it->oie_dirent->oied_name[1] == '.')
7627 * If the parent is on remote MDT, and there
7628 * is no FID-in-dirent, then we have to get
7629 * the parent FID from the linkEA.
7631 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
7632 rc = osd_get_pfid_from_linkea(env, obj, fid);
7634 if (is_dotdot == false &&
7635 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
7638 rc = osd_ea_fid_get(env, obj, ino, fid, id);
7643 /* Pack the entry anyway, at least the offset is right. */
7644 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
7645 it->oie_dirent->oied_name,
7646 it->oie_dirent->oied_namelen,
7647 it->oie_dirent->oied_type, attr);
7649 RETURN(rc > 0 ? 0 : rc);
7653 * Returns the record size size at current position.
7655 * This function will return record(lu_dirent) size in bytes.
7657 * \param[in] env execution environment
7658 * \param[in] di iterator's in memory structure
7659 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
7660 * calculate the lu_dirent size.
7662 * \retval record size(in bytes & in memory) of the current lu_dirent
7665 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
7668 struct osd_it_ea *it = (struct osd_it_ea *)di;
7670 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
7674 * Returns a cookie for current position of the iterator head, so that
7675 * user can use this cookie to load/start the iterator next time.
7677 * \param di iterator's in memory structure
7679 * \retval cookie for current position, on success
7681 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
7683 struct osd_it_ea *it = (struct osd_it_ea *)di;
7685 return it->oie_dirent->oied_off;
7689 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7690 * to load a directory entry at a time and stored it i inn,
7691 * in iterator's in-memory data structure.
7693 * \param di struct osd_it_ea, iterator's in memory structure
7695 * \retval +ve on success
7696 * \retval -ve on error
7698 static int osd_it_ea_load(const struct lu_env *env,
7699 const struct dt_it *di, __u64 hash)
7701 struct osd_it_ea *it = (struct osd_it_ea *)di;
7705 it->oie_file->f_pos = hash;
7707 rc = osd_ldiskfs_it_fill(env, di);
7718 * Index lookup function for interoperability mode (b11826).
7720 * \param key, key i.e. file name to be searched
7722 * \retval +ve, on success
7723 * \retval -ve, on error
7725 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
7726 struct dt_rec *rec, const struct dt_key *key)
7728 struct osd_object *obj = osd_dt_obj(dt);
7733 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
7734 LINVRNT(osd_invariant(obj));
7736 rc = osd_ea_lookup_rec(env, obj, rec, key);
7743 * Index and Iterator operations for interoperability
7744 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
7746 static const struct dt_index_operations osd_index_ea_ops = {
7747 .dio_lookup = osd_index_ea_lookup,
7748 .dio_declare_insert = osd_index_declare_ea_insert,
7749 .dio_insert = osd_index_ea_insert,
7750 .dio_declare_delete = osd_index_declare_ea_delete,
7751 .dio_delete = osd_index_ea_delete,
7753 .init = osd_it_ea_init,
7754 .fini = osd_it_ea_fini,
7755 .get = osd_it_ea_get,
7756 .put = osd_it_ea_put,
7757 .next = osd_it_ea_next,
7758 .key = osd_it_ea_key,
7759 .key_size = osd_it_ea_key_size,
7760 .rec = osd_it_ea_rec,
7761 .rec_size = osd_it_ea_rec_size,
7762 .store = osd_it_ea_store,
7763 .load = osd_it_ea_load
7767 static void *osd_key_init(const struct lu_context *ctx,
7768 struct lu_context_key *key)
7770 struct osd_thread_info *info;
7772 OBD_ALLOC_PTR(info);
7774 return ERR_PTR(-ENOMEM);
7776 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7777 if (info->oti_it_ea_buf == NULL)
7780 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
7782 info->oti_hlock = ldiskfs_htree_lock_alloc();
7783 if (info->oti_hlock == NULL)
7789 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7792 return ERR_PTR(-ENOMEM);
7795 static void osd_key_fini(const struct lu_context *ctx,
7796 struct lu_context_key *key, void *data)
7798 struct osd_thread_info *info = data;
7799 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
7800 struct osd_idmap_cache *idc = info->oti_ins_cache;
7802 if (info->oti_dio_pages) {
7804 for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
7805 struct page *page = info->oti_dio_pages[i];
7807 LASSERT(PagePrivate2(page));
7808 LASSERT(PageLocked(page));
7809 ClearPagePrivate2(page);
7814 OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
7815 PTLRPC_MAX_BRW_PAGES);
7818 if (info->oti_inode != NULL)
7820 if (info->oti_hlock != NULL)
7821 ldiskfs_htree_lock_free(info->oti_hlock);
7822 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7823 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
7824 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
7825 lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
7826 lu_buf_free(&info->oti_big_buf);
7828 LASSERT(info->oti_ins_cache_size > 0);
7829 OBD_FREE_PTR_ARRAY_LARGE(idc, info->oti_ins_cache_size);
7830 info->oti_ins_cache = NULL;
7831 info->oti_ins_cache_size = 0;
7836 static void osd_key_exit(const struct lu_context *ctx,
7837 struct lu_context_key *key, void *data)
7839 struct osd_thread_info *info = data;
7841 LASSERT(info->oti_r_locks == 0);
7842 LASSERT(info->oti_w_locks == 0);
7843 LASSERT(info->oti_txns == 0);
7844 LASSERTF(info->oti_dio_pages_used == 0, "%d\n",
7845 info->oti_dio_pages_used);
7848 /* type constructor/destructor: osd_type_init, osd_type_fini */
7849 LU_TYPE_INIT_FINI(osd, &osd_key);
7851 struct lu_context_key osd_key = {
7852 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
7853 .lct_init = osd_key_init,
7854 .lct_fini = osd_key_fini,
7855 .lct_exit = osd_key_exit
7859 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
7860 const char *name, struct lu_device *next)
7862 struct osd_device *osd = osd_dev(d);
7864 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
7865 sizeof(osd->od_svname))
7867 return osd_procfs_init(osd, name);
7870 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
7872 struct seq_server_site *ss = osd_seq_site(osd);
7877 if (osd->od_is_ost || osd->od_cl_seq != NULL)
7880 if (unlikely(ss == NULL))
7883 OBD_ALLOC_PTR(osd->od_cl_seq);
7884 if (osd->od_cl_seq == NULL)
7887 seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
7888 osd->od_svname, ss->ss_server_seq);
7890 if (ss->ss_node_id == 0) {
7892 * If the OSD on the sequence controller(MDT0), then allocate
7893 * sequence here, otherwise allocate sequence after connected
7894 * to MDT0 (see mdt_register_lwp_callback()).
7896 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
7897 &osd->od_cl_seq->lcs_space, env);
7903 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
7905 if (osd->od_cl_seq == NULL)
7908 seq_client_fini(osd->od_cl_seq);
7909 OBD_FREE_PTR(osd->od_cl_seq);
7910 osd->od_cl_seq = NULL;
7913 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
7917 /* shutdown quota slave instance associated with the device */
7918 if (o->od_quota_slave_md != NULL) {
7919 struct qsd_instance *qsd = o->od_quota_slave_md;
7921 o->od_quota_slave_md = NULL;
7925 if (o->od_quota_slave_dt != NULL) {
7926 struct qsd_instance *qsd = o->od_quota_slave_dt;
7928 o->od_quota_slave_dt = NULL;
7932 osd_fid_fini(env, o);
7933 osd_scrub_cleanup(env, o);
7938 static void osd_umount(const struct lu_env *env, struct osd_device *o)
7942 if (o->od_mnt != NULL) {
7943 shrink_dcache_sb(osd_sb(o));
7944 osd_sync(env, &o->od_dt_dev);
7945 wait_event(o->od_commit_cb_done,
7946 !atomic_read(&o->od_commit_cb_in_flight));
7955 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
7956 # ifndef LDISKFS_HAS_INCOMPAT_FEATURE
7957 /* Newer kernels provide the ldiskfs_set_feature_largedir() wrapper already,
7958 * which calls ldiskfs_update_dynamic_rev() to update ancient filesystems.
7959 * All ldiskfs filesystems are already v2, so it is a no-op and unnecessary.
7960 * This avoids maintaining patches to export this otherwise-useless function.
7962 void ldiskfs_update_dynamic_rev(struct super_block *sb)
7969 static int osd_mount(const struct lu_env *env,
7970 struct osd_device *o, struct lustre_cfg *cfg)
7972 const char *name = lustre_cfg_string(cfg, 0);
7973 const char *dev = lustre_cfg_string(cfg, 1);
7975 unsigned long page, s_flags = 0, lmd_flags = 0;
7976 struct page *__page;
7977 struct file_system_type *type;
7978 char *options = NULL;
7980 struct osd_thread_info *info = osd_oti_get(env);
7981 struct lu_fid *fid = &info->oti_fid;
7982 struct inode *inode;
7983 int rc = 0, force_over_1024tb = 0;
7987 if (o->od_mnt != NULL)
7990 if (strlen(dev) >= sizeof(o->od_mntdev))
7992 strcpy(o->od_mntdev, dev);
7994 str = lustre_cfg_buf(cfg, 2);
7995 sscanf(str, "%lu:%lu", &s_flags, &lmd_flags);
7997 opts = lustre_cfg_string(cfg, 3);
7999 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
8000 CERROR("%s: device %s extents feature is not guaranteed to "
8001 "work on big-endian systems. Use \"bigendian_extents\" "
8002 "mount option to override.\n", name, dev);
8006 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8007 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
8008 CWARN("force_over_128tb option is deprecated. Filesystems smaller than 1024TB can be created without any force option. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
8012 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
8013 CWARN("force_over_256tb option is deprecated. Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8016 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8017 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
8018 CWARN("force_over_512tb option is deprecated. Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8022 if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
8023 force_over_1024tb = 1;
8025 __page = alloc_page(GFP_KERNEL);
8027 GOTO(out, rc = -ENOMEM);
8028 page = (unsigned long)page_address(__page);
8029 options = (char *)page;
8032 /* strip out the options for back compatiblity */
8033 static const char * const sout[] = {
8040 /* strip out option we processed in osd */
8041 "bigendian_extents",
8045 "force_over_1024tb",
8049 strncat(options, opts, PAGE_SIZE);
8050 for (rc = 0, str = options; sout[rc]; ) {
8051 char *op = strstr(str, sout[rc]);
8058 if (op == options || *(op - 1) == ',') {
8059 str = op + strlen(sout[rc]);
8060 if (*str == ',' || *str == '\0') {
8061 *str == ',' ? str++ : str;
8062 memmove(op, str, strlen(str) + 1);
8065 for (str = op; *str != ',' && *str != '\0'; str++)
8069 strncat(options, "user_xattr,acl", PAGE_SIZE);
8072 /* Glom up mount options */
8073 if (*options != '\0')
8074 strncat(options, ",", PAGE_SIZE);
8075 strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
8077 type = get_fs_type("ldiskfs");
8079 CERROR("%s: cannot find ldiskfs module\n", name);
8080 GOTO(out, rc = -ENODEV);
8083 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
8084 module_put(type->owner);
8086 if (IS_ERR(o->od_mnt)) {
8087 rc = PTR_ERR(o->od_mnt);
8089 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
8093 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
8094 osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
8095 force_over_1024tb == 0) {
8096 CERROR("%s: device %s LDISKFS has not been tested on filesystems larger than 1024TB and may cause data corruption. Use 'force_over_1024tb' mount option to override.\n",
8098 GOTO(out_mnt, rc = -EINVAL);
8101 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
8102 LCONSOLE_WARN("%s: not support dev_rdonly on this device\n",
8105 GOTO(out_mnt, rc = -EOPNOTSUPP);
8108 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
8109 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
8110 GOTO(out_mnt, rc = -EINVAL);
8113 if (ldiskfs_has_feature_fast_commit(o->od_mnt->mnt_sb)) {
8114 CERROR("%s: device %s is mounted with fast_commit that breaks recovery\n",
8116 GOTO(out_mnt, rc = -EOPNOTSUPP);
8119 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8120 #ifdef LDISKFS_MOUNT_DIRDATA
8121 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
8122 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
8123 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
8124 CWARN("%s: device %s was upgraded from Lustre-1.x without "
8125 "enabling the dirdata feature. If you do not want to "
8126 "downgrade to Lustre-1.x again, you can enable it via "
8127 "'tune2fs -O dirdata device'\n", name, dev);
8129 /* enable large_dir on MDTs to avoid REMOTE_PARENT_DIR overflow,
8130 * and on very large OSTs to avoid object directory overflow */
8131 if (unlikely(!ldiskfs_has_feature_largedir(o->od_mnt->mnt_sb) &&
8132 !strstr(name, "MGS"))) {
8133 ldiskfs_set_feature_largedir(o->od_mnt->mnt_sb);
8134 LCONSOLE_INFO("%s: enabled 'large_dir' feature on device %s\n",
8138 inode = osd_sb(o)->s_root->d_inode;
8139 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
8140 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
8142 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
8146 if (lmd_flags & LMD_FLG_NOSCRUB)
8147 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_NEVER;
8149 if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
8150 /* do not use pagecache with flash-backed storage */
8151 o->od_writethrough_cache = 0;
8152 o->od_read_cache = 0;
8163 __free_page(__page);
8168 static struct lu_device *osd_device_fini(const struct lu_env *env,
8169 struct lu_device *d)
8171 struct osd_device *o = osd_dev(d);
8175 osd_index_backup(env, o, false);
8176 osd_shutdown(env, o);
8178 if (o->od_oi_table != NULL)
8179 osd_oi_fini(osd_oti_get(env), o);
8180 if (o->od_extent_bytes_percpu)
8181 free_percpu(o->od_extent_bytes_percpu);
8182 osd_obj_map_fini(o);
8188 static int osd_device_init0(const struct lu_env *env,
8189 struct osd_device *o,
8190 struct lustre_cfg *cfg)
8192 struct lu_device *l = osd2lu_dev(o);
8193 struct osd_thread_info *info;
8196 bool restored = false;
8199 /* if the module was re-loaded, env can loose its keys */
8200 rc = lu_env_refill((struct lu_env *)env);
8203 info = osd_oti_get(env);
8206 l->ld_ops = &osd_lu_ops;
8207 o->od_dt_dev.dd_ops = &osd_dt_ops;
8209 spin_lock_init(&o->od_osfs_lock);
8210 mutex_init(&o->od_otable_mutex);
8211 INIT_LIST_HEAD(&o->od_orphan_list);
8212 INIT_LIST_HEAD(&o->od_index_backup_list);
8213 INIT_LIST_HEAD(&o->od_index_restore_list);
8214 spin_lock_init(&o->od_lock);
8215 o->od_index_backup_policy = LIBP_NONE;
8217 init_waitqueue_head(&o->od_commit_cb_done);
8219 o->od_read_cache = 1;
8220 o->od_writethrough_cache = 1;
8221 o->od_enable_projid_xattr = 0;
8222 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
8223 o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
8224 o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
8225 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_DEFAULT;
8226 /* default fallocate to unwritten extents: LU-14326/LU-14333 */
8227 o->od_fallocate_zero_blocks = 0;
8229 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
8230 sizeof(o->od_svname));
8231 if (cplen >= sizeof(o->od_svname)) {
8236 o->od_index_backup_stop = 0;
8237 o->od_index = -1; /* -1 means index is invalid */
8238 rc = server_name2index(o->od_svname, &o->od_index, NULL);
8239 if (rc == LDD_F_SV_TYPE_OST)
8242 o->od_full_scrub_ratio = OFSR_DEFAULT;
8243 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
8244 rc = osd_mount(env, o, cfg);
8248 /* Can only check block device after mount */
8249 o->od_nonrotational =
8250 blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
8252 rc = osd_obj_map_init(env, o);
8256 rc = lu_site_init(&o->od_site, l);
8258 GOTO(out_compat, rc);
8259 o->od_site.ls_bottom_dev = l;
8261 rc = lu_site_init_finish(&o->od_site);
8265 opts = lustre_cfg_string(cfg, 3);
8266 if (opts && strstr(opts, "resetoi"))
8269 INIT_LIST_HEAD(&o->od_ios_list);
8271 rc = lprocfs_init_brw_stats(&o->od_brw_stats);
8273 GOTO(out_brw_stats, rc);
8275 /* setup scrub, including OI files initialization */
8277 rc = osd_scrub_setup(env, o, restored);
8280 GOTO(out_brw_stats, rc);
8282 rc = osd_procfs_init(o, o->od_svname);
8284 CERROR("%s: can't initialize procfs: rc = %d\n",
8286 GOTO(out_scrub, rc);
8289 LASSERT(l->ld_site->ls_linkage.next != NULL);
8290 LASSERT(l->ld_site->ls_linkage.prev != NULL);
8292 /* initialize quota slave instance */
8293 /* currently it's no need to prepare qsd_instance_md for OST */
8294 if (!o->od_is_ost) {
8295 o->od_quota_slave_md = qsd_init(env, o->od_svname,
8296 &o->od_dt_dev, o->od_proc_entry,
8298 if (IS_ERR(o->od_quota_slave_md)) {
8299 rc = PTR_ERR(o->od_quota_slave_md);
8300 o->od_quota_slave_md = NULL;
8301 GOTO(out_procfs, rc);
8305 o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
8306 o->od_proc_entry, false, true);
8308 if (IS_ERR(o->od_quota_slave_dt)) {
8309 if (o->od_quota_slave_md != NULL) {
8310 qsd_fini(env, o->od_quota_slave_md);
8311 o->od_quota_slave_md = NULL;
8314 rc = PTR_ERR(o->od_quota_slave_dt);
8315 o->od_quota_slave_dt = NULL;
8316 GOTO(out_procfs, rc);
8319 o->od_extent_bytes_percpu = alloc_percpu(unsigned int);
8320 if (!o->od_extent_bytes_percpu) {
8322 GOTO(out_procfs, rc);
8330 osd_scrub_cleanup(env, o);
8332 lprocfs_fini_brw_stats(&o->od_brw_stats);
8334 lu_site_fini(&o->od_site);
8336 osd_obj_map_fini(o);
8343 static struct lu_device *osd_device_alloc(const struct lu_env *env,
8344 struct lu_device_type *t,
8345 struct lustre_cfg *cfg)
8347 struct osd_device *o;
8352 return ERR_PTR(-ENOMEM);
8354 rc = dt_device_init(&o->od_dt_dev, t);
8357 * Because the ctx might be revived in dt_device_init,
8358 * refill the env here
8360 lu_env_refill((struct lu_env *)env);
8361 rc = osd_device_init0(env, o, cfg);
8363 dt_device_fini(&o->od_dt_dev);
8366 if (unlikely(rc != 0))
8369 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
8372 static struct lu_device *osd_device_free(const struct lu_env *env,
8373 struct lu_device *d)
8375 struct osd_device *o = osd_dev(d);
8379 /* XXX: make osd top device in order to release reference */
8380 d->ld_site->ls_top_dev = d;
8381 lu_site_purge(env, d->ld_site, -1);
8382 lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
8383 D_ERROR, lu_cdebug_printer);
8384 lu_site_fini(&o->od_site);
8385 dt_device_fini(&o->od_dt_dev);
8390 static int osd_process_config(const struct lu_env *env,
8391 struct lu_device *d, struct lustre_cfg *cfg)
8393 struct osd_device *o = osd_dev(d);
8399 switch (cfg->lcfg_command) {
8401 rc = osd_mount(env, o, cfg);
8405 * For the case LCFG_PRE_CLEANUP is not called in advance,
8406 * that may happend if hit failure during mount process.
8408 osd_index_backup(env, o, false);
8409 lu_dev_del_linkage(d->ld_site, d);
8410 rc = osd_shutdown(env, o);
8413 LASSERT(&o->od_dt_dev);
8414 count = class_modify_config(cfg, PARAM_OSD,
8415 &o->od_dt_dev.dd_kobj);
8417 count = class_modify_config(cfg, PARAM_OST,
8418 &o->od_dt_dev.dd_kobj);
8419 rc = count > 0 ? 0 : count;
8421 case LCFG_PRE_CLEANUP:
8423 osd_index_backup(env, o,
8424 o->od_index_backup_policy != LIBP_NONE);
8434 static int osd_recovery_complete(const struct lu_env *env,
8435 struct lu_device *d)
8437 struct osd_device *osd = osd_dev(d);
8442 if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
8446 * start qsd instance on recovery completion, this notifies the quota
8447 * slave code that we are about to process new requests now
8449 rc = qsd_start(env, osd->od_quota_slave_dt);
8450 if (rc == 0 && osd->od_quota_slave_md != NULL)
8451 rc = qsd_start(env, osd->od_quota_slave_md);
8457 * we use exports to track all osd users
8459 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
8460 struct obd_device *obd, struct obd_uuid *cluuid,
8461 struct obd_connect_data *data, void *localdata)
8463 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8464 struct lustre_handle conn;
8469 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
8471 rc = class_connect(&conn, obd, cluuid);
8475 *exp = class_conn2export(&conn);
8477 spin_lock(&osd->od_osfs_lock);
8479 spin_unlock(&osd->od_osfs_lock);
8485 * once last export (we don't count self-export) disappeared
8486 * osd can be released
8488 static int osd_obd_disconnect(struct obd_export *exp)
8490 struct obd_device *obd = exp->exp_obd;
8491 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8492 int rc, release = 0;
8496 /* Only disconnect the underlying layers on the final disconnect. */
8497 spin_lock(&osd->od_osfs_lock);
8499 if (osd->od_connects == 0)
8501 spin_unlock(&osd->od_osfs_lock);
8503 rc = class_disconnect(exp); /* bz 9811 */
8505 if (rc == 0 && release)
8506 class_manual_cleanup(obd);
8510 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
8511 struct lu_device *dev)
8513 struct osd_device *osd = osd_dev(dev);
8514 struct lr_server_data *lsd =
8515 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
8520 if (osd->od_quota_slave_md != NULL) {
8521 /* set up quota slave objects for inode */
8522 result = qsd_prepare(env, osd->od_quota_slave_md);
8527 if (osd->od_quota_slave_dt != NULL) {
8528 /* set up quota slave objects for block */
8529 result = qsd_prepare(env, osd->od_quota_slave_dt);
8535 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
8536 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
8537 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
8538 osd->od_index_in_idif = 1;
8540 osd->od_index_in_idif = 0;
8541 result = osd_register_proc_index_in_idif(osd);
8546 osd->od_index_in_idif = 1;
8550 result = osd_fid_init(env, osd);
8556 * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
8560 * see include/lu_object.h for the details.
8562 static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
8563 struct lu_fid *fid, struct lu_object *parent,
8564 const struct lu_name *name)
8566 struct osd_device *osd = osd_dev(d);
8568 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
8571 static const struct lu_object_operations osd_lu_obj_ops = {
8572 .loo_object_init = osd_object_init,
8573 .loo_object_delete = osd_object_delete,
8574 .loo_object_release = osd_object_release,
8575 .loo_object_free = osd_object_free,
8576 .loo_object_print = osd_object_print,
8577 .loo_object_invariant = osd_object_invariant
8580 const struct lu_device_operations osd_lu_ops = {
8581 .ldo_object_alloc = osd_object_alloc,
8582 .ldo_process_config = osd_process_config,
8583 .ldo_recovery_complete = osd_recovery_complete,
8584 .ldo_prepare = osd_prepare,
8585 .ldo_fid_alloc = osd_fid_alloc,
8588 static const struct lu_device_type_operations osd_device_type_ops = {
8589 .ldto_init = osd_type_init,
8590 .ldto_fini = osd_type_fini,
8592 .ldto_start = osd_type_start,
8593 .ldto_stop = osd_type_stop,
8595 .ldto_device_alloc = osd_device_alloc,
8596 .ldto_device_free = osd_device_free,
8598 .ldto_device_init = osd_device_init,
8599 .ldto_device_fini = osd_device_fini
8602 static struct lu_device_type osd_device_type = {
8603 .ldt_tags = LU_DEVICE_DT,
8604 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
8605 .ldt_ops = &osd_device_type_ops,
8606 .ldt_ctx_tags = LCT_LOCAL,
8609 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
8611 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8612 struct super_block *sb = osd_sb(osd);
8614 return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
8618 * lprocfs legacy support.
8620 static const struct obd_ops osd_obd_device_ops = {
8621 .o_owner = THIS_MODULE,
8622 .o_connect = osd_obd_connect,
8623 .o_disconnect = osd_obd_disconnect,
8624 .o_health_check = osd_health_check,
8627 static ssize_t track_declares_assert_show(struct kobject *kobj,
8628 struct attribute *attr,
8631 return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
8634 static ssize_t track_declares_assert_store(struct kobject *kobj,
8635 struct attribute *attr,
8636 const char *buffer, size_t count)
8638 bool track_declares_assert;
8641 rc = kstrtobool(buffer, &track_declares_assert);
8645 ldiskfs_track_declares_assert = track_declares_assert;
8649 LUSTRE_RW_ATTR(track_declares_assert);
8651 static int __init osd_init(void)
8653 struct kobject *kobj;
8656 BUILD_BUG_ON(BH_DXLock >=
8657 sizeof(((struct buffer_head *)0)->b_state) * 8);
8658 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
8659 /* please, try to keep osd_thread_info smaller than a page */
8660 BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
8665 rc = lu_kmem_init(ldiskfs_caches);
8669 rc = class_register_type(&osd_obd_device_ops, NULL, true,
8670 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
8672 lu_kmem_fini(ldiskfs_caches);
8676 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8678 rc = sysfs_create_file(kobj,
8679 &lustre_attr_track_declares_assert.attr);
8682 CWARN("osd-ldiskfs: track_declares_assert failed to register with sysfs\n");
8690 static void __exit osd_exit(void)
8692 struct kobject *kobj;
8694 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8696 sysfs_remove_file(kobj,
8697 &lustre_attr_track_declares_assert.attr);
8700 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
8701 lu_kmem_fini(ldiskfs_caches);
8704 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8705 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
8706 MODULE_VERSION(LUSTRE_VERSION_STRING);
8707 MODULE_LICENSE("GPL");
8709 module_init(osd_init);
8710 module_exit(osd_exit);