Whamcloud - gitweb
LU-8974 osd-ldiskfs: increase supported ldiskfs fs size
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd/osd_handler.c
33  *
34  * Top-level entry points into osd module
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  *         Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
38  */
39
40 #define DEBUG_SUBSYSTEM S_OSD
41
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #ifdef HAVE_UIDGID_HEADER
45 # include <linux/uidgid.h>
46 #endif
47
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/fs.h>
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
56
57 #include <ldiskfs/ldiskfs.h>
58 #include <ldiskfs/xattr.h>
59 #include <ldiskfs/ldiskfs_extents.h>
60 #undef ENTRY
61 /*
62  * struct OBD_{ALLOC,FREE}*()
63  * OBD_FAIL_CHECK
64  */
65 #include <obd_support.h>
66 /* struct ptlrpc_thread */
67 #include <lustre_net.h>
68 #include <lustre_fid.h>
69 /* process_config */
70 #include <lustre_param.h>
71
72 #include "osd_internal.h"
73 #include "osd_dynlocks.h"
74
75 /* llo_* api support */
76 #include <md_object.h>
77 #include <lustre_quota.h>
78
79 #include <lustre_linkea.h>
80
81 int ldiskfs_pdo = 1;
82 module_param(ldiskfs_pdo, int, 0644);
83 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
84
85 int ldiskfs_track_declares_assert;
86 module_param(ldiskfs_track_declares_assert, int, 0644);
87 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
88
89 /* Slab to allocate dynlocks */
90 struct kmem_cache *dynlock_cachep;
91
92 /* Slab to allocate osd_it_ea */
93 struct kmem_cache *osd_itea_cachep;
94
95 static struct lu_kmem_descr ldiskfs_caches[] = {
96         {
97                 .ckd_cache = &dynlock_cachep,
98                 .ckd_name  = "dynlock_cache",
99                 .ckd_size  = sizeof(struct dynlock_handle)
100         },
101         {
102                 .ckd_cache = &osd_itea_cachep,
103                 .ckd_name  = "osd_itea_cache",
104                 .ckd_size  = sizeof(struct osd_it_ea)
105         },
106         {
107                 .ckd_cache = NULL
108         }
109 };
110
111 static const char dot[] = ".";
112 static const char dotdot[] = "..";
113 static const char remote_obj_dir[] = "REM_OBJ_DIR";
114
115 static const struct lu_object_operations      osd_lu_obj_ops;
116 static const struct dt_object_operations      osd_obj_ops;
117 static const struct dt_object_operations      osd_obj_ea_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;
121
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);
126
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,
139 };
140
141 static int osd_has_index(const struct osd_object *obj)
142 {
143         return obj->oo_dt.do_index_ops != NULL;
144 }
145
146 static int osd_object_invariant(const struct lu_object *l)
147 {
148         return osd_invariant(osd_obj(l));
149 }
150
151 /*
152  * Concurrency: doesn't matter
153  */
154
155 /*
156  * Concurrency: doesn't matter
157  */
158 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
159 {
160         struct osd_thread_info *oti = osd_oti_get(env);
161         return oti->oti_w_locks > 0 && o->oo_owner == env;
162 }
163
164 /*
165  * Concurrency: doesn't access mutable data
166  */
167 static int osd_root_get(const struct lu_env *env,
168                         struct dt_device *dev, struct lu_fid *f)
169 {
170         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
171         return 0;
172 }
173
174 /*
175  * the following set of functions are used to maintain per-thread
176  * cache of FID->ino mapping. this mechanism is needed to resolve
177  * FID to inode at dt_insert() which in turn stores ino in the
178  * directory entries to keep ldiskfs compatible with ext[34].
179  * due to locking-originated restrictions we can't lookup ino
180  * using LU cache (deadlock is possible). lookup using OI is quite
181  * expensive. so instead we maintain this cache and methods like
182  * dt_create() fill it. so in the majority of cases dt_insert() is
183  * able to find needed mapping in lockless manner.
184  */
185 static struct osd_idmap_cache *
186 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
187              const struct lu_fid *fid)
188 {
189         struct osd_thread_info  *oti   = osd_oti_get(env);
190         struct osd_idmap_cache  *idc    = oti->oti_ins_cache;
191         int i;
192         for (i = 0; i < oti->oti_ins_cache_used; i++) {
193                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
194                         continue;
195                 if (idc[i].oic_dev != osd)
196                         continue;
197
198                 return idc + i;
199         }
200
201         return NULL;
202 }
203
204 static struct osd_idmap_cache *
205 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
206             const struct lu_fid *fid)
207 {
208         struct osd_thread_info  *oti   = osd_oti_get(env);
209         struct osd_idmap_cache  *idc;
210         int i;
211
212         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
213                 i = oti->oti_ins_cache_size * 2;
214                 if (i == 0)
215                         i = OSD_INS_CACHE_SIZE;
216                 OBD_ALLOC(idc, sizeof(*idc) * i);
217                 if (idc == NULL)
218                         return ERR_PTR(-ENOMEM);
219                 if (oti->oti_ins_cache != NULL) {
220                         memcpy(idc, oti->oti_ins_cache,
221                                oti->oti_ins_cache_used * sizeof(*idc));
222                         OBD_FREE(oti->oti_ins_cache,
223                                  oti->oti_ins_cache_used * sizeof(*idc));
224                 }
225                 oti->oti_ins_cache = idc;
226                 oti->oti_ins_cache_size = i;
227         }
228
229         idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
230         idc->oic_fid = *fid;
231         idc->oic_dev = osd;
232         idc->oic_lid.oii_ino = 0;
233         idc->oic_lid.oii_gen = 0;
234         idc->oic_remote = 0;
235
236         return idc;
237 }
238
239 /*
240  * lookup mapping for the given fid in the cache, initialize a
241  * new one if not found. the initialization checks whether the
242  * object is local or remote. for local objects, OI is used to
243  * learn ino/generation. the function is used when the caller
244  * has no information about the object, e.g. at dt_insert().
245  */
246 static struct osd_idmap_cache *
247 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
248                      const struct lu_fid *fid)
249 {
250         struct osd_idmap_cache *idc;
251         int rc;
252
253         idc = osd_idc_find(env, osd, fid);
254         LASSERT(!IS_ERR(idc));
255         if (idc != NULL)
256                 return idc;
257
258         /* new mapping is needed */
259         idc = osd_idc_add(env, osd, fid);
260         if (IS_ERR(idc))
261                 return idc;
262
263         /* initialize it */
264         rc = osd_remote_fid(env, osd, fid);
265         if (unlikely(rc < 0))
266                 return ERR_PTR(rc);
267
268         if (rc == 0) {
269                 /* the object is local, lookup in OI */
270                 /* XXX: probably cheaper to lookup in LU first? */
271                 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
272                                    &idc->oic_lid, 0);
273                 if (unlikely(rc < 0)) {
274                         CERROR("can't lookup: rc = %d\n", rc);
275                         return ERR_PTR(rc);
276                 }
277         } else {
278                 /* the object is remote */
279                 idc->oic_remote = 1;
280         }
281
282         return idc;
283 }
284
285 /*
286  * lookup mapping for given FID and fill it from the given object.
287  * the object is lolcal by definition.
288  */
289 static int osd_idc_find_and_init(const struct lu_env *env,
290                                  struct osd_device *osd,
291                                  struct osd_object *obj)
292 {
293         const struct lu_fid     *fid = lu_object_fid(&obj->oo_dt.do_lu);
294         struct osd_idmap_cache  *idc;
295
296         idc = osd_idc_find(env, osd, fid);
297         LASSERT(!IS_ERR(idc));
298         if (idc != NULL) {
299                 if (obj->oo_inode == NULL)
300                         return 0;
301                 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
302                         LASSERT(idc->oic_lid.oii_ino == 0);
303                         idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
304                         idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
305                 }
306                 return 0;
307         }
308
309         /* new mapping is needed */
310         idc = osd_idc_add(env, osd, fid);
311         if (IS_ERR(idc))
312                 return PTR_ERR(idc);
313
314         if (obj->oo_inode != NULL) {
315                 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
316                 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
317         }
318         return 0;
319 }
320
321 /*
322  * OSD object methods.
323  */
324
325 /*
326  * Concurrency: no concurrent access is possible that early in object
327  * life-cycle.
328  */
329 static struct lu_object *osd_object_alloc(const struct lu_env *env,
330                                           const struct lu_object_header *hdr,
331                                           struct lu_device *d)
332 {
333         struct osd_object *mo;
334
335         OBD_ALLOC_PTR(mo);
336         if (mo != NULL) {
337                 struct lu_object *l;
338
339                 l = &mo->oo_dt.do_lu;
340                 dt_object_init(&mo->oo_dt, NULL, d);
341                 mo->oo_dt.do_ops = &osd_obj_ea_ops;
342                 l->lo_ops = &osd_lu_obj_ops;
343                 init_rwsem(&mo->oo_sem);
344                 init_rwsem(&mo->oo_ext_idx_sem);
345                 spin_lock_init(&mo->oo_guard);
346                 INIT_LIST_HEAD(&mo->oo_xattr_list);
347                 return l;
348         } else {
349                 return NULL;
350         }
351 }
352
353 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
354                 struct dentry *dentry, struct lustre_mdt_attrs *lma)
355 {
356         int rc;
357
358         CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
359         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
360                              info->oti_mdt_attrs_old, LMA_OLD_SIZE);
361         if (rc > 0) {
362                 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
363                         memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
364                 rc = 0;
365                 lustre_lma_swab(lma);
366                 /* Check LMA compatibility */
367                 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
368                         CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
369                               "for fid = "DFID", ino = %lu\n",
370                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
371                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
372                               PFID(&lma->lma_self_fid), inode->i_ino);
373                         rc = -EOPNOTSUPP;
374                 }
375         } else if (rc == 0) {
376                 rc = -ENODATA;
377         }
378
379         return rc;
380 }
381
382 /*
383  * retrieve object from backend ext fs.
384  **/
385 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
386                        struct osd_inode_id *id)
387 {
388         int rc;
389         struct inode *inode = NULL;
390
391         /* if we look for an inode withing a running
392          * transaction, then we risk to deadlock */
393         /* osd_dirent_check_repair() breaks this */
394         /*LASSERT(current->journal_info == NULL);*/
395
396         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
397         if (IS_ERR(inode)) {
398                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
399                        id->oii_ino, PTR_ERR(inode));
400         } else if (id->oii_gen != OSD_OII_NOGEN &&
401                    inode->i_generation != id->oii_gen) {
402                 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
403                        "i_generation = %u\n",
404                        id->oii_ino, id->oii_gen, inode->i_generation);
405                 iput(inode);
406                 inode = ERR_PTR(-ESTALE);
407         } else if (inode->i_nlink == 0) {
408                 /* due to parallel readdir and unlink,
409                 * we can have dead inode here. */
410                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
411                 iput(inode);
412                 inode = ERR_PTR(-ESTALE);
413         } else if (is_bad_inode(inode)) {
414                 CWARN("%.16s: bad inode: ino = %u\n",
415                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
416                 iput(inode);
417                 inode = ERR_PTR(-ENOENT);
418         } else if ((rc = osd_attach_jinode(inode))) {
419                 iput(inode);
420                 inode = ERR_PTR(rc);
421         } else {
422                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
423                 if (id->oii_gen == OSD_OII_NOGEN)
424                         osd_id_gen(id, inode->i_ino, inode->i_generation);
425
426                 /* Do not update file c/mtime in ldiskfs.
427                  * NB: we don't have any lock to protect this because we don't
428                  * have reference on osd_object now, but contention with
429                  * another lookup + attr_set can't happen in the tiny window
430                  * between if (...) and set S_NOCMTIME. */
431                 if (!(inode->i_flags & S_NOCMTIME))
432                         inode->i_flags |= S_NOCMTIME;
433         }
434         return inode;
435 }
436
437 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
438                           handle_t *handle, struct dentry *child,
439                           struct inode *inode, struct htree_lock *hlock)
440 {
441         int rc, rc2;
442
443         rc = __ldiskfs_add_entry(handle, child, inode, hlock);
444         if (rc == -ENOBUFS || rc == -ENOSPC) {
445                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
446                 struct inode *parent = child->d_parent->d_inode;
447                 struct lu_fid *fid = NULL;
448
449                 rc2 = osd_get_lma(info, parent, child->d_parent, lma);
450                 if (rc2 == 0) {
451                         fid = &lma->lma_self_fid;
452                 } else if (rc2 == -ENODATA) {
453                         if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
454                                 fid = &info->oti_fid3;
455                                 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
456                         } else if (!osd->od_is_ost && osd->od_index == 0) {
457                                 fid = &info->oti_fid3;
458                                 lu_igif_build(fid, parent->i_ino,
459                                               parent->i_generation);
460                         }
461                 }
462
463                 if (fid != NULL)
464                         CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
465                               "maximum entry limit\n",
466                               osd_name(osd), parent->i_ino, PFID(fid),
467                               rc == -ENOSPC ? "has reached" : "is approaching");
468                 else
469                         CWARN("%s: directory (inode: %lu, FID: unknown) %s "
470                               "maximum entry limit\n",
471                               osd_name(osd), parent->i_ino,
472                               rc == -ENOSPC ? "has reached" : "is approaching");
473
474                 /* ignore such error now */
475                 if (rc == -ENOBUFS)
476                         rc = 0;
477         }
478
479         return rc;
480 }
481
482
483 static struct inode *
484 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
485              struct osd_inode_id *id, struct lu_fid *fid)
486 {
487         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
488         struct inode            *inode;
489         int                      rc;
490
491         inode = osd_iget(info, dev, id);
492         if (IS_ERR(inode))
493                 return inode;
494
495         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
496         if (rc == 0) {
497                 *fid = lma->lma_self_fid;
498         } else if (rc == -ENODATA) {
499                 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
500                         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
501                 else
502                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
503         } else {
504                 iput(inode);
505                 inode = ERR_PTR(rc);
506         }
507         return inode;
508 }
509
510 static struct inode *osd_iget_check(struct osd_thread_info *info,
511                                     struct osd_device *dev,
512                                     const struct lu_fid *fid,
513                                     struct osd_inode_id *id,
514                                     bool cached)
515 {
516         struct inode    *inode;
517         int              rc     = 0;
518         ENTRY;
519
520         /* The cached OI mapping is trustable. If we cannot locate the inode
521          * via the cached OI mapping, then return the failure to the caller
522          * directly without further OI checking. */
523
524         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
525         if (IS_ERR(inode)) {
526                 rc = PTR_ERR(inode);
527                 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
528                         CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
529                                id->oii_ino, rc);
530
531                         GOTO(put, rc);
532                 }
533
534                 goto check_oi;
535         }
536
537         if (is_bad_inode(inode)) {
538                 rc = -ENOENT;
539                 if (cached) {
540                         CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
541
542                         GOTO(put, rc);
543                 }
544
545                 goto check_oi;
546         }
547
548         if (id->oii_gen != OSD_OII_NOGEN &&
549             inode->i_generation != id->oii_gen) {
550                 rc = -ESTALE;
551                 if (cached) {
552                         CDEBUG(D_INODE, "unmatched inode: ino = %u, "
553                                "oii_gen = %u, i_generation = %u\n",
554                                id->oii_ino, id->oii_gen, inode->i_generation);
555
556                         GOTO(put, rc);
557                 }
558
559                 goto check_oi;
560         }
561
562         if (inode->i_nlink == 0) {
563                 rc = -ENOENT;
564                 if (cached) {
565                         CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
566
567                         GOTO(put, rc);
568                 }
569
570                 goto check_oi;
571         }
572
573         ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
574
575 check_oi:
576         if (rc != 0) {
577                 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
578
579                 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
580                 /* XXX: There are four possible cases:
581                  *      1. rc = 0.
582                  *         Backup/restore caused the OI invalid.
583                  *      2. rc = 0.
584                  *         Someone unlinked the object but NOT removed
585                  *         the OI mapping, such as mount target device
586                  *         as ldiskfs, and modify something directly.
587                  *      3. rc = -ENOENT.
588                  *         Someone just removed the object between the
589                  *         former oi_lookup and the iget. It is normal.
590                  *      4. Other failure cases.
591                  *
592                  *      Generally, when the device is mounted, it will
593                  *      auto check whether the system is restored from
594                  *      file-level backup or not. We trust such detect
595                  *      to distinguish the 1st case from the 2nd case:
596                  *      if the OI files are consistent but may contain
597                  *      stale OI mappings because of case 2, if iget()
598                  *      returns -ENOENT or -ESTALE, then it should be
599                  *      the case 2. */
600                 if (rc != 0)
601                         /* If the OI mapping was in OI file before the
602                          * osd_iget_check(), but now, it is disappear,
603                          * then it must be removed by race. That is a
604                          * normal race case. */
605                         GOTO(put, rc);
606
607                 if ((!IS_ERR(inode) && inode->i_generation != 0 &&
608                      inode->i_generation == id->oii_gen) ||
609                     (IS_ERR(inode) && !(dev->od_scrub.os_file.sf_flags &
610                                         SF_INCONSISTENT)))
611                         rc = -ENOENT;
612                 else
613                         rc = -EREMCHG;
614         } else {
615                 if (id->oii_gen == OSD_OII_NOGEN)
616                         osd_id_gen(id, inode->i_ino, inode->i_generation);
617
618                 /* Do not update file c/mtime in ldiskfs.
619                  * NB: we don't have any lock to protect this because we don't
620                  * have reference on osd_object now, but contention with
621                  * another lookup + attr_set can't happen in the tiny window
622                  * between if (...) and set S_NOCMTIME. */
623                 if (!(inode->i_flags & S_NOCMTIME))
624                         inode->i_flags |= S_NOCMTIME;
625         }
626
627         GOTO(put, rc);
628
629 put:
630         if (rc != 0) {
631                 if (!IS_ERR(inode))
632                         iput(inode);
633
634                 inode = ERR_PTR(rc);
635         }
636
637         return inode;
638 }
639
640 /**
641  * \retval +v: new filter_fid, does not contain self-fid
642  * \retval 0:  filter_fid_old, contains self-fid
643  * \retval -v: other failure cases
644  */
645 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
646                  struct dentry *dentry, struct lu_fid *fid)
647 {
648         struct filter_fid_old   *ff     = &info->oti_ff;
649         struct ost_id           *ostid  = &info->oti_ostid;
650         int                      rc;
651
652         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
653         if (rc == sizeof(*ff)) {
654                 rc = 0;
655                 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
656                 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
657                 /* XXX: use 0 as the index for compatibility, the caller will
658                  *      handle index related issues when necessarry. */
659                 ostid_to_fid(fid, ostid, 0);
660         } else if (rc == sizeof(struct filter_fid)) {
661                 rc = 1;
662         } else if (rc >= 0) {
663                 rc = -EINVAL;
664         }
665
666         return rc;
667 }
668
669 static int osd_lma_self_repair(struct osd_thread_info *info,
670                                struct osd_device *osd, struct inode *inode,
671                                const struct lu_fid *fid, __u32 compat)
672 {
673         handle_t *jh;
674         int       rc;
675
676         LASSERT(current->journal_info == NULL);
677
678         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
679                                   osd_dto_credits_noquota[DTO_XATTR_SET]);
680         if (IS_ERR(jh)) {
681                 rc = PTR_ERR(jh);
682                 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
683                       osd_name(osd), rc);
684                 return rc;
685         }
686
687         rc = osd_ea_fid_set(info, inode, fid, compat, 0);
688         if (rc != 0)
689                 CWARN("%s: cannot self repair the LMA: rc = %d\n",
690                       osd_name(osd), rc);
691         ldiskfs_journal_stop(jh);
692         return rc;
693 }
694
695 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
696 {
697         struct osd_thread_info  *info   = osd_oti_get(env);
698         struct osd_device       *osd    = osd_obj2dev(obj);
699         struct lustre_mdt_attrs *lma    = &info->oti_mdt_attrs;
700         struct inode            *inode  = obj->oo_inode;
701         struct dentry           *dentry = &info->oti_obj_dentry;
702         struct lu_fid           *fid    = NULL;
703         const struct lu_fid     *rfid   = lu_object_fid(&obj->oo_dt.do_lu);
704         int                      rc;
705         ENTRY;
706
707         CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
708         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
709                              info->oti_mdt_attrs_old, LMA_OLD_SIZE);
710         if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
711                 fid = &lma->lma_self_fid;
712                 rc = osd_get_idif(info, inode, dentry, fid);
713                 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
714                         /* For the given OST-object, if it has neither LMA nor
715                          * FID in XATTR_NAME_FID, then the given FID (which is
716                          * contained in the @obj, from client RPC for locating
717                          * the OST-object) is trusted. We use it to generate
718                          * the LMA. */
719                         osd_lma_self_repair(info, osd, inode, rfid,
720                                             LMAC_FID_ON_OST);
721                         RETURN(0);
722                 }
723         }
724
725         if (rc < 0)
726                 RETURN(rc);
727
728         if (rc > 0) {
729                 rc = 0;
730                 lustre_lma_swab(lma);
731                 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
732                              CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
733                         CWARN("%s: unsupported incompat LMA feature(s) %#x for "
734                               "fid = "DFID", ino = %lu\n", osd_name(osd),
735                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
736                               PFID(rfid), inode->i_ino);
737                         rc = -EOPNOTSUPP;
738                 } else {
739                         fid = &lma->lma_self_fid;
740                 }
741         }
742
743         if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
744                 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
745                         struct ost_id   *oi   = &info->oti_ostid;
746                         struct lu_fid   *fid1 = &info->oti_fid3;
747                         __u32            idx  = fid_idif_ost_idx(rfid);
748
749                         /* For old IDIF, the OST index is not part of the IDIF,
750                          * Means that different OSTs may have the same IDIFs.
751                          * Under such case, we need to make some compatible
752                          * check to make sure to trigger OI scrub properly. */
753                         if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
754                                 /* Given @rfid is new, LMA is old. */
755                                 fid_to_ostid(fid, oi);
756                                 ostid_to_fid(fid1, oi, idx);
757                                 if (lu_fid_eq(fid1, rfid)) {
758                                         if (osd->od_index_in_idif)
759                                                 osd_lma_self_repair(info, osd,
760                                                         inode, rfid,
761                                                         LMAC_FID_ON_OST);
762                                         RETURN(0);
763                                 }
764                         }
765                 }
766
767                 rc = -EREMCHG;
768         }
769
770         RETURN(rc);
771 }
772
773 struct osd_check_lmv_buf {
774 #ifdef HAVE_DIR_CONTEXT
775         /* please keep it as first member */
776         struct dir_context       ctx;
777 #endif
778         struct osd_thread_info  *oclb_info;
779         struct osd_device       *oclb_dev;
780         struct osd_idmap_cache  *oclb_oic;
781 };
782
783 /**
784  * It is called internally by ->readdir() to filter out the
785  * local slave object's FID of the striped directory.
786  *
787  * \retval      1 found the local slave's FID
788  * \retval      0 continue to check next item
789  * \retval      -ve for failure
790  */
791 #ifdef HAVE_FILLDIR_USE_CTX
792 static int osd_stripe_dir_filldir(struct dir_context *buf,
793 #else
794 static int osd_stripe_dir_filldir(void *buf,
795 #endif
796                                   const char *name, int namelen,
797                                   loff_t offset, __u64 ino, unsigned d_type)
798 {
799         struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
800         struct osd_thread_info *oti = oclb->oclb_info;
801         struct lu_fid *fid = &oti->oti_fid3;
802         struct osd_inode_id *id = &oti->oti_id3;
803         struct osd_device *dev = oclb->oclb_dev;
804         struct osd_idmap_cache *oic = oclb->oclb_oic;
805         struct inode *inode;
806         int rc;
807
808         if (name[0] == '.')
809                 return 0;
810
811         fid_zero(fid);
812         sscanf(name + 1, SFID, RFID(fid));
813         if (!fid_is_sane(fid))
814                 return 0;
815
816         if (osd_remote_fid(oti->oti_env, dev, fid))
817                 return 0;
818
819         osd_id_gen(id, ino, OSD_OII_NOGEN);
820         inode = osd_iget(oti, dev, id);
821         if (IS_ERR(inode))
822                 return PTR_ERR(inode);
823
824         iput(inode);
825         osd_add_oi_cache(oti, dev, id, fid);
826         oic->oic_fid = *fid;
827         oic->oic_lid = *id;
828         oic->oic_dev = dev;
829         rc = osd_oii_insert(dev, oic, true);
830
831         return rc == 0 ? 1 : rc;
832 }
833
834 /* When lookup item under striped directory, we need to locate the master
835  * MDT-object of the striped directory firstly, then the client will send
836  * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
837  * and the item's name. If the system is restored from MDT file level backup,
838  * then before the OI scrub completely built the OI files, the OI mappings of
839  * the master MDT-object and slave MDT-object may be invalid. Usually, it is
840  * not a problem for the master MDT-object. Because when locate the master
841  * MDT-object, we will do name based lookup (for the striped directory itself)
842  * firstly, during such process we can setup the correct OI mapping for the
843  * master MDT-object. But it will be trouble for the slave MDT-object. Because
844  * the client will not trigger name based lookup on the MDT to locate the slave
845  * MDT-object before locating item under the striped directory, then when
846  * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
847  * is invalid and does not know what the right OI mapping is, then the MDT has
848  * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
849  * the OI file, related OI mapping is unknown yet, please try again later. And
850  * then client will re-try the RPC again and again until related OI mapping has
851  * been updated. That is quite inefficient.
852  *
853  * To resolve above trouble, we will handle it as the following two cases:
854  *
855  * 1) The slave MDT-object and the master MDT-object are on different MDTs.
856  *    It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
857  *    is linked under /REMOTE_PARENT_DIR with the name of its FID string.
858  *    We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
859  *    directly. Please check osd_fid_lookup().
860  *
861  * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
862  *    Under such case, during lookup the master MDT-object, we will lookup the
863  *    slave MDT-object via readdir against the master MDT-object, because the
864  *    slave MDT-objects information are stored as sub-directories with the name
865  *    "${FID}:${index}". Then when find the local slave MDT-object, its OI
866  *    mapping will be recorded. Then subsequent osd_fid_lookup() will know
867  *    the correct OI mapping for the slave MDT-object. */
868 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
869                          struct inode *inode, struct osd_idmap_cache *oic)
870 {
871         struct lu_buf *buf = &oti->oti_big_buf;
872         struct dentry *dentry = &oti->oti_obj_dentry;
873         struct file *filp = &oti->oti_file;
874         const struct file_operations *fops;
875         struct lmv_mds_md_v1 *lmv1;
876         struct osd_check_lmv_buf oclb = {
877 #ifdef HAVE_DIR_CONTEXT
878                 .ctx.actor = osd_stripe_dir_filldir,
879 #endif
880                 .oclb_info = oti,
881                 .oclb_dev = dev,
882                 .oclb_oic = oic
883         };
884         int rc = 0;
885         ENTRY;
886
887 again:
888         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
889                              buf->lb_len);
890         if (rc == -ERANGE) {
891                 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
892                 if (rc > 0) {
893                         lu_buf_realloc(buf, rc);
894                         if (buf->lb_buf == NULL)
895                                 GOTO(out, rc = -ENOMEM);
896
897                         goto again;
898                 }
899         }
900
901         if (unlikely(rc == 0 || rc == -ENODATA))
902                 GOTO(out, rc = 0);
903
904         if (rc < 0)
905                 GOTO(out, rc);
906
907         if (unlikely(buf->lb_buf == NULL)) {
908                 lu_buf_realloc(buf, rc);
909                 if (buf->lb_buf == NULL)
910                         GOTO(out, rc = -ENOMEM);
911
912                 goto again;
913         }
914
915         lmv1 = buf->lb_buf;
916         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
917                 GOTO(out, rc = 0);
918
919         fops = inode->i_fop;
920         dentry->d_inode = inode;
921         dentry->d_sb = inode->i_sb;
922         filp->f_pos = 0;
923         filp->f_path.dentry = dentry;
924         filp->f_mode = FMODE_64BITHASH;
925         filp->f_mapping = inode->i_mapping;
926         filp->f_op = fops;
927         filp->private_data = NULL;
928         set_file_inode(filp, inode);
929
930 #ifdef HAVE_DIR_CONTEXT
931         oclb.ctx.pos = filp->f_pos;
932         rc = fops->iterate(filp, &oclb.ctx);
933         filp->f_pos = oclb.ctx.pos;
934 #else
935         rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
936 #endif
937         fops->release(inode, filp);
938
939 out:
940         if (rc < 0)
941                 CDEBUG(D_LFSCK, "%.16s: fail to check LMV EA, inode = %lu/%u,"
942                        DFID": rc = %d\n",
943                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
944                        inode->i_ino, inode->i_generation,
945                        PFID(&oic->oic_fid), rc);
946         else
947                 rc = 0;
948
949         RETURN(rc);
950 }
951
952 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
953                           const struct lu_fid *fid,
954                           const struct lu_object_conf *conf)
955 {
956         struct osd_thread_info *info;
957         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
958         struct osd_device      *dev;
959         struct osd_idmap_cache *oic;
960         struct osd_inode_id    *id;
961         struct osd_inode_id    *tid;
962         struct inode           *inode = NULL;
963         struct osd_scrub       *scrub;
964         struct scrub_file      *sf;
965         __u32                   flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT |
966                                         SS_AUTO_FULL;
967         __u32                   saved_ino;
968         __u32                   saved_gen;
969         int                     result  = 0;
970         int                     rc1     = 0;
971         bool                    cached  = true;
972         bool                    remote  = false;
973         ENTRY;
974
975         LINVRNT(osd_invariant(obj));
976         LASSERT(obj->oo_inode == NULL);
977         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
978
979         dev = osd_dev(ldev);
980         scrub = &dev->od_scrub;
981         sf = &scrub->os_file;
982         info = osd_oti_get(env);
983         LASSERT(info);
984         oic = &info->oti_cache;
985
986         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
987                 RETURN(-ENOENT);
988
989         /* For the object is created as locking anchor, or for the object to
990          * be created on disk. No need to osd_oi_lookup() at here because FID
991          * shouldn't never be re-used, if it's really a duplicate FID from
992          * unexpected reason, we should be able to detect it later by calling
993          * do_create->osd_oi_insert(). */
994         if (conf != NULL && conf->loc_flags & LOC_F_NEW)
995                 GOTO(out, result = 0);
996
997         /* Search order: 1. per-thread cache. */
998         if (lu_fid_eq(fid, &oic->oic_fid) &&
999             likely(oic->oic_dev == dev)) {
1000                 id = &oic->oic_lid;
1001                 goto iget;
1002         }
1003
1004         id = &info->oti_id;
1005         if (!list_empty(&scrub->os_inconsistent_items)) {
1006                 /* Search order: 2. OI scrub pending list. */
1007                 result = osd_oii_lookup(dev, fid, id);
1008                 if (result == 0)
1009                         goto iget;
1010         }
1011
1012         cached = false;
1013         /* Search order: 3. OI files. */
1014         result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1015         if (result == -ENOENT) {
1016                 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1017                     fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1018                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1019                                       sf->sf_oi_bitmap))
1020                         GOTO(out, result = 0);
1021
1022                 goto trigger;
1023         }
1024
1025         if (result != 0)
1026                 GOTO(out, result);
1027
1028 iget:
1029         inode = osd_iget_check(info, dev, fid, id, cached);
1030         if (IS_ERR(inode)) {
1031                 result = PTR_ERR(inode);
1032                 if (result == -ENOENT || result == -ESTALE)
1033                         GOTO(out, result = 0);
1034
1035                 if (result == -EREMCHG) {
1036
1037 trigger:
1038                         /* We still have chance to get the valid inode: for the
1039                          * object which is referenced by remote name entry, the
1040                          * object on the local MDT will be linked under the dir
1041                          * of "/REMOTE_PARENT_DIR" with its FID string as name.
1042                          *
1043                          * We do not know whether the object for the given FID
1044                          * is referenced by some remote name entry or not, and
1045                          * especially for DNE II, a multiple-linked object may
1046                          * have many name entries reside on many MDTs.
1047                          *
1048                          * To simplify the operation, OSD will not distinguish
1049                          * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1050                          * only happened for the RPC from other MDT during the
1051                          * OI scrub, or for the client side RPC with FID only,
1052                          * such as FID to path, or from old connected client. */
1053                         if (!remote &&
1054                             !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1055                                 rc1 = osd_lookup_in_remote_parent(info, dev,
1056                                                                   fid, id);
1057                                 if (rc1 == 0) {
1058                                         remote = true;
1059                                         cached = true;
1060                                         flags |= SS_AUTO_PARTIAL;
1061                                         flags &= ~SS_AUTO_FULL;
1062                                         goto iget;
1063                                 }
1064                         }
1065
1066                         if (thread_is_running(&scrub->os_thread)) {
1067                                 if (scrub->os_partial_scan &&
1068                                     !scrub->os_in_join) {
1069                                         goto join;
1070                                 } else {
1071                                         if (inode != NULL && !IS_ERR(inode)) {
1072                                                 LASSERT(remote);
1073
1074                                                 osd_add_oi_cache(info, dev, id,
1075                                                                  fid);
1076                                                 osd_oii_insert(dev, oic, true);
1077                                         } else {
1078                                                 result = -EINPROGRESS;
1079                                         }
1080                                 }
1081                         } else if (!dev->od_noscrub) {
1082
1083 join:
1084                                 rc1 = osd_scrub_start(dev, flags);
1085                                 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
1086                                               "for the "DFID" with flags 0x%x,"
1087                                               " rc = %d\n", osd_name(dev),
1088                                               PFID(fid), flags, rc1);
1089                                 if (rc1 == 0 || rc1 == -EALREADY) {
1090                                         if (inode != NULL && !IS_ERR(inode)) {
1091                                                 LASSERT(remote);
1092
1093                                                 osd_add_oi_cache(info, dev, id,
1094                                                                  fid);
1095                                                 osd_oii_insert(dev, oic, true);
1096                                         } else {
1097                                                 result = -EINPROGRESS;
1098                                         }
1099                                 } else {
1100                                         result = -EREMCHG;
1101                                 }
1102                         } else {
1103                                 result = -EREMCHG;
1104                         }
1105                 }
1106
1107                 if (inode == NULL || IS_ERR(inode))
1108                         GOTO(out, result);
1109         } else if (remote) {
1110                 goto trigger;
1111         }
1112
1113         obj->oo_inode = inode;
1114         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
1115
1116         result = osd_check_lma(env, obj);
1117         if (result == 0)
1118                 goto found;
1119
1120         tid = &info->oti_id3;
1121         LASSERT(tid != id);
1122
1123         if (result == -ENODATA) {
1124                 if (!cached)
1125                         /* The current OI mapping is from the OI file,
1126                          * since the inode has been found via
1127                          * osd_iget_check(), no need recheck OI. */
1128                         goto found;
1129
1130                 result = osd_oi_lookup(info, dev, fid, tid, OI_CHECK_FLD);
1131                 if (result == 0) {
1132                         LASSERTF(tid->oii_ino == id->oii_ino &&
1133                                  tid->oii_gen == id->oii_gen,
1134                                  "OI mapping changed(1): %u/%u => %u/%u",
1135                                  tid->oii_ino, tid->oii_gen,
1136                                  id->oii_ino, id->oii_gen);
1137
1138                         LASSERTF(tid->oii_ino == inode->i_ino &&
1139                                  tid->oii_gen == inode->i_generation,
1140                                  "locate wrong inode(1): %u/%u => %ld/%u",
1141                                  tid->oii_ino, tid->oii_gen,
1142                                  inode->i_ino, inode->i_generation);
1143
1144                         /* "result == 0" means the cached OI mapping is still in
1145                          * the OI file, so the target the inode is valid. */
1146                         goto found;
1147                 }
1148
1149                 /* "result == -ENOENT" means that the OI mappinghas been removed
1150                  * by race, the target inode belongs to other object.
1151                  *
1152                  * Others error can be returned  directly. */
1153                 if (result == -ENOENT)
1154                         result = 0;
1155         }
1156
1157         saved_ino = inode->i_ino;
1158         saved_gen = inode->i_generation;
1159         iput(inode);
1160         inode = NULL;
1161         obj->oo_inode = NULL;
1162
1163         if (result != -EREMCHG)
1164                 GOTO(out, result);
1165
1166         if (!cached)
1167                 /* The current OI mapping is from the OI file,
1168                  * since the inode has been found via
1169                  * osd_iget_check(), no need recheck OI. */
1170                 goto trigger;
1171
1172         result = osd_oi_lookup(info, dev, fid, tid, OI_CHECK_FLD);
1173         /* "result == -ENOENT" means the cached OI mapping has been removed from
1174          * the OI file by race, above target inode belongs to other object.
1175          *
1176          * Others error can be returned directly. */
1177         if (result != 0)
1178                 GOTO(out, result = (result == -ENOENT ? 0 : result));
1179
1180         LASSERTF(tid->oii_ino == id->oii_ino && tid->oii_gen == id->oii_gen,
1181                  "OI mapping changed(2): %u/%u => %u/%u",
1182                  tid->oii_ino, tid->oii_gen, id->oii_ino, id->oii_gen);
1183
1184         LASSERTF(tid->oii_ino == saved_ino && tid->oii_gen == saved_gen,
1185                  "locate wrong inode(2): %u/%u => %u/%u",
1186                  tid->oii_ino, tid->oii_gen, saved_ino, saved_gen);
1187
1188         goto trigger;
1189
1190 found:
1191         obj->oo_compat_dot_created = 1;
1192         obj->oo_compat_dotdot_created = 1;
1193
1194         if (S_ISDIR(inode->i_mode) &&
1195             (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1196                 osd_check_lmv(info, dev, inode, oic);
1197
1198         result = osd_attach_jinode(inode);
1199         if (result) {
1200                 obj->oo_inode = NULL;
1201                 iput(inode);
1202                 GOTO(out, result);
1203         }
1204
1205         if (!ldiskfs_pdo)
1206                 GOTO(out, result = 0);
1207
1208         LASSERT(obj->oo_hl_head == NULL);
1209         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1210         if (obj->oo_hl_head == NULL) {
1211                 obj->oo_inode = NULL;
1212                 iput(inode);
1213                 GOTO(out, result = -ENOMEM);
1214         }
1215         GOTO(out, result = 0);
1216
1217 out:
1218         if (result != 0 && cached)
1219                 fid_zero(&oic->oic_fid);
1220
1221         LINVRNT(osd_invariant(obj));
1222         return result;
1223 }
1224
1225 /*
1226  * Concurrency: shouldn't matter.
1227  */
1228 static void osd_object_init0(struct osd_object *obj)
1229 {
1230         LASSERT(obj->oo_inode != NULL);
1231         obj->oo_dt.do_body_ops = &osd_body_ops;
1232         obj->oo_dt.do_lu.lo_header->loh_attr |=
1233                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1234 }
1235
1236 /*
1237  * Concurrency: no concurrent access is possible that early in object
1238  * life-cycle.
1239  */
1240 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1241                            const struct lu_object_conf *conf)
1242 {
1243         struct osd_object *obj = osd_obj(l);
1244         int result;
1245
1246         LINVRNT(osd_invariant(obj));
1247
1248         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1249                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1250                 l->lo_header->loh_attr |= LOHA_EXISTS;
1251                 return 0;
1252         }
1253
1254         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1255         obj->oo_dt.do_body_ops = &osd_body_ops_new;
1256         if (result == 0 && obj->oo_inode != NULL) {
1257                 struct osd_thread_info *oti = osd_oti_get(env);
1258                 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1259
1260                 osd_object_init0(obj);
1261                 result = osd_get_lma(oti, obj->oo_inode,
1262                                      &oti->oti_obj_dentry, lma);
1263                 if (result == 0) {
1264                         /* Convert LMAI flags to lustre LMA flags
1265                          * and cache it to oo_lma_flags */
1266                         obj->oo_lma_flags =
1267                                 lma_to_lustre_flags(lma->lma_incompat);
1268                 } else if (result == -ENODATA) {
1269                         result = 0;
1270                 }
1271         }
1272
1273         LINVRNT(osd_invariant(obj));
1274         return result;
1275 }
1276
1277 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1278  * The left part is for value, binary mode. */
1279 struct osd_xattr_entry {
1280         struct list_head        oxe_list;
1281         size_t                  oxe_len;
1282         size_t                  oxe_namelen;
1283         bool                    oxe_exist;
1284         struct rcu_head         oxe_rcu;
1285         char                    oxe_buf[0];
1286 };
1287
1288 static int osd_oxc_get(struct osd_object *obj, const char *name,
1289                        struct lu_buf *buf)
1290 {
1291         struct osd_xattr_entry *tmp;
1292         struct osd_xattr_entry *oxe = NULL;
1293         size_t namelen = strlen(name);
1294         int rc;
1295         ENTRY;
1296
1297         rcu_read_lock();
1298         list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1299                 if (namelen == tmp->oxe_namelen &&
1300                     strncmp(name, tmp->oxe_buf, namelen) == 0) {
1301                         oxe = tmp;
1302                         break;
1303                 }
1304         }
1305
1306         if (oxe == NULL)
1307                 GOTO(out, rc = -ENOENT);
1308
1309         if (!oxe->oxe_exist)
1310                 GOTO(out, rc = -ENODATA);
1311
1312         /* vallen */
1313         rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1314         LASSERT(rc > 0);
1315
1316         if (buf->lb_buf == NULL)
1317                 GOTO(out, rc);
1318
1319         if (buf->lb_len < rc)
1320                 GOTO(out, rc = -ERANGE);
1321
1322         memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1323         EXIT;
1324 out:
1325         rcu_read_unlock();
1326
1327         return rc;
1328 }
1329
1330 static void osd_oxc_free(struct rcu_head *head)
1331 {
1332         struct osd_xattr_entry *oxe;
1333
1334         oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1335         OBD_FREE(oxe, oxe->oxe_len);
1336 }
1337
1338 static void osd_oxc_add(struct osd_object *obj, const char *name,
1339                         const char *buf, int buflen)
1340 {
1341         struct osd_xattr_entry *oxe;
1342         struct osd_xattr_entry *old = NULL;
1343         struct osd_xattr_entry *tmp;
1344         size_t namelen = strlen(name);
1345         size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1346
1347         OBD_ALLOC(oxe, len);
1348         if (oxe == NULL)
1349                 return;
1350
1351         INIT_LIST_HEAD(&oxe->oxe_list);
1352         oxe->oxe_len = len;
1353         oxe->oxe_namelen = namelen;
1354         memcpy(oxe->oxe_buf, name, namelen);
1355         if (buflen > 0) {
1356                 LASSERT(buf != NULL);
1357                 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1358                 oxe->oxe_exist = true;
1359         } else {
1360                 oxe->oxe_exist = false;
1361         }
1362
1363         /* this should be rarely called, just remove old and add new */
1364         spin_lock(&obj->oo_guard);
1365         list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1366                 if (namelen == tmp->oxe_namelen &&
1367                     strncmp(name, tmp->oxe_buf, namelen) == 0) {
1368                         old = tmp;
1369                         break;
1370                 }
1371         }
1372         if (old != NULL) {
1373                 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1374                 call_rcu(&old->oxe_rcu, osd_oxc_free);
1375         } else {
1376                 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1377         }
1378         spin_unlock(&obj->oo_guard);
1379 }
1380
1381 static void osd_oxc_del(struct osd_object *obj, const char *name)
1382 {
1383         struct osd_xattr_entry *oxe;
1384         size_t namelen = strlen(name);
1385
1386         spin_lock(&obj->oo_guard);
1387         list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1388                 if (namelen == oxe->oxe_namelen &&
1389                     strncmp(name, oxe->oxe_buf, namelen) == 0) {
1390                         list_del_rcu(&oxe->oxe_list);
1391                         call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1392                         break;
1393                 }
1394         }
1395         spin_unlock(&obj->oo_guard);
1396 }
1397
1398 static void osd_oxc_fini(struct osd_object *obj)
1399 {
1400         struct osd_xattr_entry *oxe, *next;
1401
1402         list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1403                 list_del(&oxe->oxe_list);
1404                 OBD_FREE(oxe, oxe->oxe_len);
1405         }
1406 }
1407
1408 /*
1409  * Concurrency: no concurrent access is possible that late in object
1410  * life-cycle.
1411  */
1412 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1413 {
1414         struct osd_object *obj = osd_obj(l);
1415
1416         LINVRNT(osd_invariant(obj));
1417
1418         osd_oxc_fini(obj);
1419         dt_object_fini(&obj->oo_dt);
1420         if (obj->oo_hl_head != NULL)
1421                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1422         OBD_FREE_PTR(obj);
1423 }
1424
1425 /*
1426  * Concurrency: no concurrent access is possible that late in object
1427  * life-cycle.
1428  */
1429 static void osd_index_fini(struct osd_object *o)
1430 {
1431         struct iam_container *bag;
1432
1433         if (o->oo_dir != NULL) {
1434                 bag = &o->oo_dir->od_container;
1435                 if (o->oo_inode != NULL) {
1436                         if (bag->ic_object == o->oo_inode)
1437                                 iam_container_fini(bag);
1438                 }
1439                 OBD_FREE_PTR(o->oo_dir);
1440                 o->oo_dir = NULL;
1441         }
1442 }
1443
1444 /*
1445  * Concurrency: no concurrent access is possible that late in object
1446  * life-cycle (for all existing callers, that is. New callers have to provide
1447  * their own locking.)
1448  */
1449 static int osd_inode_unlinked(const struct inode *inode)
1450 {
1451         return inode->i_nlink == 0;
1452 }
1453
1454 enum {
1455         OSD_TXN_OI_DELETE_CREDITS    = 20,
1456         OSD_TXN_INODE_DELETE_CREDITS = 20
1457 };
1458
1459 /*
1460  * Journal
1461  */
1462
1463 #if OSD_THANDLE_STATS
1464 /**
1465  * Set time when the handle is allocated
1466  */
1467 static void osd_th_alloced(struct osd_thandle *oth)
1468 {
1469         oth->oth_alloced = ktime_get();
1470 }
1471
1472 /**
1473  * Set time when the handle started
1474  */
1475 static void osd_th_started(struct osd_thandle *oth)
1476 {
1477         oth->oth_started = ktime_get();
1478 }
1479
1480 /**
1481  * Check whether the we deal with this handle for too long.
1482  */
1483 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1484                                 ktime_t alloced, ktime_t started,
1485                                 ktime_t closed)
1486 {
1487         ktime_t now = ktime_get();
1488
1489         LASSERT(dev != NULL);
1490
1491         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1492                             ktime_us_delta(started, alloced));
1493         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1494                             ktime_us_delta(closed, started));
1495         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1496                             ktime_us_delta(now, closed));
1497
1498         if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1499                 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1500                       oth, now, alloced, started, closed);
1501                 libcfs_debug_dumpstack(NULL);
1502         }
1503 }
1504
1505 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
1506 {                                                                       \
1507         ktime_t __closed = ktime_get();                                 \
1508         ktime_t __alloced = oth->oth_alloced;                           \
1509         ktime_t __started = oth->oth_started;                           \
1510                                                                         \
1511         expr;                                                           \
1512         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
1513 }
1514
1515 #else /* OSD_THANDLE_STATS */
1516
1517 #define osd_th_alloced(h)                  do {} while(0)
1518 #define osd_th_started(h)                  do {} while(0)
1519 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
1520
1521 #endif /* OSD_THANDLE_STATS */
1522
1523 /*
1524  * Concurrency: doesn't access mutable data.
1525  */
1526 static int osd_param_is_not_sane(const struct osd_device *dev,
1527                                  const struct thandle *th)
1528 {
1529         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1530
1531         return oh->ot_credits > osd_transaction_size(dev);
1532 }
1533
1534 /*
1535  * Concurrency: shouldn't matter.
1536  */
1537 static void osd_trans_commit_cb(struct super_block *sb,
1538                                 struct ldiskfs_journal_cb_entry *jcb, int error)
1539 {
1540         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1541         struct thandle     *th  = &oh->ot_super;
1542         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
1543         struct dt_txn_commit_cb *dcb, *tmp;
1544
1545         LASSERT(oh->ot_handle == NULL);
1546
1547         if (error)
1548                 CERROR("transaction @0x%p commit error: %d\n", th, error);
1549
1550         dt_txn_hook_commit(th);
1551
1552         /* call per-transaction callbacks if any */
1553         list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1554                                  dcb_linkage) {
1555                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1556                          "commit callback entry: magic=%x name='%s'\n",
1557                          dcb->dcb_magic, dcb->dcb_name);
1558                 list_del_init(&dcb->dcb_linkage);
1559                 dcb->dcb_func(NULL, th, dcb, error);
1560         }
1561
1562         lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1563         lu_device_put(lud);
1564         th->th_dev = NULL;
1565
1566         lu_context_exit(&th->th_ctx);
1567         lu_context_fini(&th->th_ctx);
1568         OBD_FREE_PTR(oh);
1569 }
1570
1571 #ifndef HAVE_SB_START_WRITE
1572 # define sb_start_write(sb) do {} while (0)
1573 # define sb_end_write(sb) do {} while (0)
1574 #endif
1575
1576 static struct thandle *osd_trans_create(const struct lu_env *env,
1577                                         struct dt_device *d)
1578 {
1579         struct osd_thread_info  *oti = osd_oti_get(env);
1580         struct osd_iobuf        *iobuf = &oti->oti_iobuf;
1581         struct osd_thandle      *oh;
1582         struct thandle          *th;
1583         ENTRY;
1584
1585         /* on pending IO in this thread should left from prev. request */
1586         LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1587
1588         sb_start_write(osd_sb(osd_dt_dev(d)));
1589
1590         OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1591         if (oh != NULL) {
1592                 oh->ot_quota_trans = &oti->oti_quota_trans;
1593                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1594                 th = &oh->ot_super;
1595                 th->th_dev = d;
1596                 th->th_result = 0;
1597                 th->th_tags = LCT_TX_HANDLE;
1598                 oh->ot_credits = 0;
1599                 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1600                 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1601                 osd_th_alloced(oh);
1602
1603                 memset(oti->oti_declare_ops, 0,
1604                        sizeof(oti->oti_declare_ops));
1605                 memset(oti->oti_declare_ops_cred, 0,
1606                        sizeof(oti->oti_declare_ops_cred));
1607                 memset(oti->oti_declare_ops_used, 0,
1608                        sizeof(oti->oti_declare_ops_used));
1609         } else {
1610                 sb_end_write(osd_sb(osd_dt_dev(d)));
1611                 th = ERR_PTR(-ENOMEM);
1612         }
1613         RETURN(th);
1614 }
1615
1616 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1617 {
1618         struct osd_thread_info  *oti = osd_oti_get(env);
1619         struct osd_thandle      *oh;
1620
1621         oh = container_of0(th, struct osd_thandle, ot_super);
1622         LASSERT(oh != NULL);
1623
1624         CWARN("  create: %u/%u/%u, destroy: %u/%u/%u\n",
1625               oti->oti_declare_ops[OSD_OT_CREATE],
1626               oti->oti_declare_ops_cred[OSD_OT_CREATE],
1627               oti->oti_declare_ops_used[OSD_OT_CREATE],
1628               oti->oti_declare_ops[OSD_OT_DESTROY],
1629               oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1630               oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1631         CWARN("  attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1632               oti->oti_declare_ops[OSD_OT_ATTR_SET],
1633               oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1634               oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1635               oti->oti_declare_ops[OSD_OT_XATTR_SET],
1636               oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1637               oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1638         CWARN("  write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1639               oti->oti_declare_ops[OSD_OT_WRITE],
1640               oti->oti_declare_ops_cred[OSD_OT_WRITE],
1641               oti->oti_declare_ops_used[OSD_OT_WRITE],
1642               oti->oti_declare_ops[OSD_OT_PUNCH],
1643               oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1644               oti->oti_declare_ops_used[OSD_OT_PUNCH],
1645               oti->oti_declare_ops[OSD_OT_QUOTA],
1646               oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1647               oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1648         CWARN("  insert: %u/%u/%u, delete: %u/%u/%u\n",
1649               oti->oti_declare_ops[OSD_OT_INSERT],
1650               oti->oti_declare_ops_cred[OSD_OT_INSERT],
1651               oti->oti_declare_ops_used[OSD_OT_INSERT],
1652               oti->oti_declare_ops[OSD_OT_DELETE],
1653               oti->oti_declare_ops_cred[OSD_OT_DELETE],
1654               oti->oti_declare_ops_used[OSD_OT_DELETE]);
1655         CWARN("  ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1656               oti->oti_declare_ops[OSD_OT_REF_ADD],
1657               oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1658               oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1659               oti->oti_declare_ops[OSD_OT_REF_DEL],
1660               oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1661               oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1662 }
1663
1664 /*
1665  * Concurrency: shouldn't matter.
1666  */
1667 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1668                            struct thandle *th)
1669 {
1670         struct osd_thread_info *oti = osd_oti_get(env);
1671         struct osd_device  *dev = osd_dt_dev(d);
1672         handle_t           *jh;
1673         struct osd_thandle *oh;
1674         int rc;
1675
1676         ENTRY;
1677
1678         LASSERT(current->journal_info == NULL);
1679
1680         oh = container_of0(th, struct osd_thandle, ot_super);
1681         LASSERT(oh != NULL);
1682         LASSERT(oh->ot_handle == NULL);
1683
1684         rc = dt_txn_hook_start(env, d, th);
1685         if (rc != 0)
1686                 GOTO(out, rc);
1687
1688         if (unlikely(osd_param_is_not_sane(dev, th))) {
1689                 static unsigned long last_printed;
1690                 static int last_credits;
1691
1692                 /* don't make noise on a tiny testing systems
1693                  * actual credits misuse will be caught anyway */
1694                 if (last_credits != oh->ot_credits &&
1695                     time_after(jiffies, last_printed +
1696                                msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1697                     osd_transaction_size(dev) > 512) {
1698                         CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1699                               oh->ot_credits, osd_transaction_size(dev));
1700                         osd_trans_dump_creds(env, th);
1701                         libcfs_debug_dumpstack(NULL);
1702                         last_credits = oh->ot_credits;
1703                         last_printed = jiffies;
1704                 }
1705                 /* XXX Limit the credits to 'max_transaction_buffers', and
1706                  *     let the underlying filesystem to catch the error if
1707                  *     we really need so many credits.
1708                  *
1709                  *     This should be removed when we can calculate the
1710                  *     credits precisely. */
1711                 oh->ot_credits = osd_transaction_size(dev);
1712         }
1713
1714         /*
1715          * XXX temporary stuff. Some abstraction layer should
1716          * be used.
1717          */
1718         jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1719         osd_th_started(oh);
1720         if (!IS_ERR(jh)) {
1721                 oh->ot_handle = jh;
1722                 LASSERT(oti->oti_txns == 0);
1723                 lu_context_init(&th->th_ctx, th->th_tags);
1724                 lu_context_enter(&th->th_ctx);
1725
1726                 lu_device_get(&d->dd_lu_dev);
1727                 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1728                               "osd-tx", th);
1729                 oti->oti_txns++;
1730                 rc = 0;
1731         } else {
1732                 rc = PTR_ERR(jh);
1733         }
1734 out:
1735         RETURN(rc);
1736 }
1737
1738 static int osd_seq_exists(const struct lu_env *env,
1739                           struct osd_device *osd, u64 seq)
1740 {
1741         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
1742         struct seq_server_site  *ss = osd_seq_site(osd);
1743         int                     rc;
1744         ENTRY;
1745
1746         LASSERT(ss != NULL);
1747         LASSERT(ss->ss_server_fld != NULL);
1748
1749         rc = osd_fld_lookup(env, osd, seq, range);
1750         if (rc != 0) {
1751                 if (rc != -ENOENT)
1752                         CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1753                                osd_name(osd), seq, rc);
1754                 RETURN(0);
1755         }
1756
1757         RETURN(ss->ss_node_id == range->lsr_index);
1758 }
1759
1760 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1761 {
1762         struct dt_txn_commit_cb *dcb;
1763         struct dt_txn_commit_cb *tmp;
1764
1765         /* call per-transaction stop callbacks if any */
1766         list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1767                                  dcb_linkage) {
1768                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1769                          "commit callback entry: magic=%x name='%s'\n",
1770                          dcb->dcb_magic, dcb->dcb_name);
1771                 list_del_init(&dcb->dcb_linkage);
1772                 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1773         }
1774 }
1775
1776 /*
1777  * Concurrency: shouldn't matter.
1778  */
1779 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1780                           struct thandle *th)
1781 {
1782         int                     rc = 0, remove_agents = 0;
1783         struct osd_thandle     *oh;
1784         struct osd_thread_info *oti = osd_oti_get(env);
1785         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
1786         struct osd_device      *osd = osd_dt_dev(th->th_dev);
1787         struct qsd_instance    *qsd = osd->od_quota_slave;
1788         struct lquota_trans    *qtrans;
1789         ENTRY;
1790
1791         oh = container_of0(th, struct osd_thandle, ot_super);
1792
1793         /* reset OI cache for safety */
1794         oti->oti_ins_cache_used = 0;
1795
1796         remove_agents = oh->ot_remove_agents;
1797
1798         qtrans = oh->ot_quota_trans;
1799         oh->ot_quota_trans = NULL;
1800
1801         if (oh->ot_handle != NULL) {
1802                 int rc2;
1803                 handle_t *hdl = oh->ot_handle;
1804
1805                 /*
1806                  * add commit callback
1807                  * notice we don't do this in osd_trans_start()
1808                  * as underlying transaction can change during truncate
1809                  */
1810                 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1811                                          &oh->ot_jcb);
1812
1813                 LASSERT(oti->oti_txns == 1);
1814                 oti->oti_txns--;
1815
1816                 rc = dt_txn_hook_stop(env, th);
1817                 if (rc != 0)
1818                         CERROR("%s: failed in transaction hook: rc = %d\n",
1819                                osd_name(osd), rc);
1820
1821                 osd_trans_stop_cb(oh, rc);
1822                 /* hook functions might modify th_sync */
1823                 hdl->h_sync = th->th_sync;
1824
1825                 oh->ot_handle = NULL;
1826                 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1827                 if (rc2 != 0)
1828                         CERROR("%s: failed to stop transaction: rc = %d\n",
1829                                osd_name(osd), rc2);
1830                 if (!rc)
1831                         rc = rc2;
1832         } else {
1833                 osd_trans_stop_cb(oh, th->th_result);
1834                 OBD_FREE_PTR(oh);
1835         }
1836
1837         /* inform the quota slave device that the transaction is stopping */
1838         qsd_op_end(env, qsd, qtrans);
1839
1840         /* as we want IO to journal and data IO be concurrent, we don't block
1841          * awaiting data IO completion in osd_do_bio(), instead we wait here
1842          * once transaction is submitted to the journal. all reqular requests
1843          * don't do direct IO (except read/write), thus this wait_event becomes
1844          * no-op for them.
1845          *
1846          * IMPORTANT: we have to wait till any IO submited by the thread is
1847          * completed otherwise iobuf may be corrupted by different request
1848          */
1849         wait_event(iobuf->dr_wait,
1850                        atomic_read(&iobuf->dr_numreqs) == 0);
1851         osd_fini_iobuf(osd, iobuf);
1852         if (!rc)
1853                 rc = iobuf->dr_error;
1854
1855         if (unlikely(remove_agents != 0))
1856                 osd_process_scheduled_agent_removals(env, osd);
1857
1858         sb_end_write(osd_sb(osd));
1859
1860         RETURN(rc);
1861 }
1862
1863 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1864 {
1865         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1866                                                ot_super);
1867
1868         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1869         LASSERT(&dcb->dcb_func != NULL);
1870         if (dcb->dcb_flags & DCB_TRANS_STOP)
1871                 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1872         else
1873                 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1874
1875         return 0;
1876 }
1877
1878 /*
1879  * Called just before object is freed. Releases all resources except for
1880  * object itself (that is released by osd_object_free()).
1881  *
1882  * Concurrency: no concurrent access is possible that late in object
1883  * life-cycle.
1884  */
1885 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1886 {
1887         struct osd_object *obj   = osd_obj(l);
1888         struct inode      *inode = obj->oo_inode;
1889
1890         LINVRNT(osd_invariant(obj));
1891
1892         /*
1893          * If object is unlinked remove fid->ino mapping from object index.
1894          */
1895
1896         osd_index_fini(obj);
1897         if (inode != NULL) {
1898                 struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
1899                 qid_t                    uid = i_uid_read(inode);
1900                 qid_t                    gid = i_gid_read(inode);
1901
1902                 iput(inode);
1903                 obj->oo_inode = NULL;
1904
1905                 if (qsd != NULL) {
1906                         struct osd_thread_info  *info = osd_oti_get(env);
1907                         struct lquota_id_info   *qi = &info->oti_qi;
1908
1909                         /* Release granted quota to master if necessary */
1910                         qi->lqi_id.qid_uid = uid;
1911                         qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1912
1913                         qi->lqi_id.qid_uid = gid;
1914                         qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1915                 }
1916         }
1917 }
1918
1919 /*
1920  * Concurrency: ->loo_object_release() is called under site spin-lock.
1921  */
1922 static void osd_object_release(const struct lu_env *env,
1923                                struct lu_object *l)
1924 {
1925         struct osd_object *o = osd_obj(l);
1926         /* nobody should be releasing a non-destroyed object with nlink=0
1927          * the API allows this, but ldiskfs doesn't like and then report
1928          * this inode as deleted */
1929         if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1930                 LBUG();
1931 }
1932
1933 /*
1934  * Concurrency: shouldn't matter.
1935  */
1936 static int osd_object_print(const struct lu_env *env, void *cookie,
1937                             lu_printer_t p, const struct lu_object *l)
1938 {
1939         struct osd_object *o = osd_obj(l);
1940         struct iam_descr  *d;
1941
1942         if (o->oo_dir != NULL)
1943                 d = o->oo_dir->od_container.ic_descr;
1944         else
1945                 d = NULL;
1946         return (*p)(env, cookie,
1947                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1948                     o, o->oo_inode,
1949                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
1950                     o->oo_inode ? o->oo_inode->i_generation : 0,
1951                     d ? d->id_ops->id_name : "plain");
1952 }
1953
1954 /*
1955  * Concurrency: shouldn't matter.
1956  */
1957 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1958                struct obd_statfs *sfs)
1959 {
1960         struct osd_device       *osd = osd_dt_dev(d);
1961         struct super_block      *sb = osd_sb(osd);
1962         struct kstatfs          *ksfs;
1963         __u64                    reserved;
1964         int                      result = 0;
1965
1966         if (unlikely(osd->od_mnt == NULL))
1967                 return -EINPROGRESS;
1968
1969         /* osd_lproc.c call this without env, allocate ksfs for that case */
1970         if (unlikely(env == NULL)) {
1971                 OBD_ALLOC_PTR(ksfs);
1972                 if (ksfs == NULL)
1973                         return -ENOMEM;
1974         } else {
1975                 ksfs = &osd_oti_get(env)->oti_ksfs;
1976         }
1977
1978         result = sb->s_op->statfs(sb->s_root, ksfs);
1979         if (result)
1980                 goto out;
1981
1982         statfs_pack(sfs, ksfs);
1983         if (unlikely(sb->s_flags & MS_RDONLY))
1984                 sfs->os_state |= OS_STATE_READONLY;
1985         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1986                                          LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1987                 sfs->os_maxbytes = sb->s_maxbytes;
1988         else
1989                 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1990
1991         /*
1992          * Reserve some space so to avoid fragmenting the filesystem too much.
1993          * Fragmentation not only impacts performance, but can also increase
1994          * metadata overhead significantly, causing grant calculation to be
1995          * wrong.
1996          *
1997          * Reserve 0.78% of total space, at least 8MB for small filesystems.
1998          */
1999         CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2000         reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2001         if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2002                 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2003
2004         sfs->os_blocks -= reserved;
2005         sfs->os_bfree  -= min(reserved, sfs->os_bfree);
2006         sfs->os_bavail -= min(reserved, sfs->os_bavail);
2007
2008 out:
2009         if (unlikely(env == NULL))
2010                 OBD_FREE_PTR(ksfs);
2011         return result;
2012 }
2013
2014 /**
2015  * Estimate space needed for file creations. We assume the largest filename
2016  * which is 2^64 - 1, hence a filename of 20 chars.
2017  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2018  */
2019 #ifdef __LDISKFS_DIR_REC_LEN
2020 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2021 #else
2022 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2023 #endif
2024
2025 /*
2026  * Concurrency: doesn't access mutable data.
2027  */
2028 static void osd_conf_get(const struct lu_env *env,
2029                          const struct dt_device *dev,
2030                          struct dt_device_param *param)
2031 {
2032         struct super_block *sb = osd_sb(osd_dt_dev(dev));
2033         int                ea_overhead;
2034
2035         /*
2036          * XXX should be taken from not-yet-existing fs abstraction layer.
2037          */
2038         param->ddp_max_name_len = LDISKFS_NAME_LEN;
2039         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
2040         param->ddp_symlink_max  = sb->s_blocksize;
2041         param->ddp_mount_type     = LDD_MT_LDISKFS;
2042         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2043                 param->ddp_maxbytes = sb->s_maxbytes;
2044         else
2045                 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2046         /* inode are statically allocated, so per-inode space consumption
2047          * is the space consumed by the directory entry */
2048         param->ddp_inodespace     = PER_OBJ_USAGE;
2049         /* EXT_INIT_MAX_LEN is the theoretical maximum extent size  (32k blocks
2050          * = 128MB) which is unlikely to be hit in real life. Report a smaller
2051          * maximum length to not under count the actual number of extents
2052          * needed for writing a file. */
2053         param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2054         /* worst-case extent insertion metadata overhead */
2055         param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2056         param->ddp_mntopts      = 0;
2057         if (test_opt(sb, XATTR_USER))
2058                 param->ddp_mntopts |= MNTOPT_USERXATTR;
2059         if (test_opt(sb, POSIX_ACL))
2060                 param->ddp_mntopts |= MNTOPT_ACL;
2061
2062         /* LOD might calculate the max stripe count based on max_ea_size,
2063          * so we need take account in the overhead as well,
2064          * xattr_header + magic + xattr_entry_head */
2065         ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2066                       LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2067
2068 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2069         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2070                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2071                                                                 ea_overhead;
2072         else
2073 #endif
2074                 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2075 }
2076
2077 /*
2078  * Concurrency: shouldn't matter.
2079  */
2080 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2081 {
2082         int rc;
2083
2084         CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2085
2086         rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2087
2088         CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2089                LUSTRE_OSD_LDISKFS_NAME, rc);
2090
2091         return rc;
2092 }
2093
2094 /**
2095  * Start commit for OSD device.
2096  *
2097  * An implementation of dt_commit_async method for OSD device.
2098  * Asychronously starts underlayng fs sync and thereby a transaction
2099  * commit.
2100  *
2101  * \param env environment
2102  * \param d dt device
2103  *
2104  * \see dt_device_operations
2105  */
2106 static int osd_commit_async(const struct lu_env *env,
2107                             struct dt_device *d)
2108 {
2109         struct super_block *s = osd_sb(osd_dt_dev(d));
2110         ENTRY;
2111
2112         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2113         RETURN(s->s_op->sync_fs(s, 0));
2114 }
2115
2116 /*
2117  * Concurrency: shouldn't matter.
2118  */
2119
2120 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2121 {
2122         struct super_block *sb = osd_sb(osd_dt_dev(d));
2123         struct block_device *dev = sb->s_bdev;
2124 #ifdef HAVE_DEV_SET_RDONLY
2125         struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2126         int rc = 0;
2127 #else
2128         int rc = -EOPNOTSUPP;
2129 #endif
2130         ENTRY;
2131
2132 #ifdef HAVE_DEV_SET_RDONLY
2133         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2134
2135         if (sb->s_op->freeze_fs) {
2136                 rc = sb->s_op->freeze_fs(sb);
2137                 if (rc)
2138                         goto out;
2139         }
2140
2141         if (jdev && (jdev != dev)) {
2142                 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2143                        (long)jdev);
2144                 dev_set_rdonly(jdev);
2145         }
2146         CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2147         dev_set_rdonly(dev);
2148
2149         if (sb->s_op->unfreeze_fs)
2150                 sb->s_op->unfreeze_fs(sb);
2151
2152 out:
2153 #endif
2154         if (rc)
2155                 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2156                        osd_dt_dev(d)->od_svname, (long)dev, rc);
2157
2158         RETURN(rc);
2159 }
2160
2161 /**
2162  * Note: we do not count into QUOTA here.
2163  * If we mount with --data_journal we may need more.
2164  */
2165 const int osd_dto_credits_noquota[DTO_NR] = {
2166         /**
2167          * Insert.
2168          * INDEX_EXTRA_TRANS_BLOCKS(8) +
2169          * SINGLEDATA_TRANS_BLOCKS(8)
2170          * XXX Note: maybe iam need more, since iam have more level than
2171          *           EXT3 htree.
2172          */
2173         [DTO_INDEX_INSERT]  = 16,
2174         /**
2175          * Delete
2176          * just modify a single entry, probably merge few within a block
2177          */
2178         [DTO_INDEX_DELETE]  = 1,
2179         /**
2180          * Used for OI scrub
2181          */
2182         [DTO_INDEX_UPDATE]  = 16,
2183         /**
2184          * 4(inode, inode bits, groups, GDT)
2185          *   notice: OI updates are counted separately with DTO_INDEX_INSERT
2186          */
2187         [DTO_OBJECT_CREATE] = 4,
2188         /**
2189          * 4(inode, inode bits, groups, GDT)
2190          *   notice: OI updates are counted separately with DTO_INDEX_DELETE
2191          */
2192         [DTO_OBJECT_DELETE] = 4,
2193         /**
2194          * Attr set credits (inode)
2195          */
2196         [DTO_ATTR_SET_BASE] = 1,
2197         /**
2198          * Xattr set. The same as xattr of EXT3.
2199          * DATA_TRANS_BLOCKS(14)
2200          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2201          * are also counted in. Do not know why?
2202          */
2203         [DTO_XATTR_SET]     = 14,
2204         /**
2205          * credits for inode change during write.
2206          */
2207         [DTO_WRITE_BASE]    = 3,
2208         /**
2209          * credits for single block write.
2210          */
2211         [DTO_WRITE_BLOCK]   = 14,
2212         /**
2213          * Attr set credits for chown.
2214          * This is extra credits for setattr, and it is null without quota
2215          */
2216         [DTO_ATTR_SET_CHOWN] = 0
2217 };
2218
2219 static const struct dt_device_operations osd_dt_ops = {
2220         .dt_root_get       = osd_root_get,
2221         .dt_statfs         = osd_statfs,
2222         .dt_trans_create   = osd_trans_create,
2223         .dt_trans_start    = osd_trans_start,
2224         .dt_trans_stop     = osd_trans_stop,
2225         .dt_trans_cb_add   = osd_trans_cb_add,
2226         .dt_conf_get       = osd_conf_get,
2227         .dt_sync           = osd_sync,
2228         .dt_ro             = osd_ro,
2229         .dt_commit_async   = osd_commit_async,
2230 };
2231
2232 static void osd_object_read_lock(const struct lu_env *env,
2233                                  struct dt_object *dt, unsigned role)
2234 {
2235         struct osd_object *obj = osd_dt_obj(dt);
2236         struct osd_thread_info *oti = osd_oti_get(env);
2237
2238         LINVRNT(osd_invariant(obj));
2239
2240         LASSERT(obj->oo_owner != env);
2241         down_read_nested(&obj->oo_sem, role);
2242
2243         LASSERT(obj->oo_owner == NULL);
2244         oti->oti_r_locks++;
2245 }
2246
2247 static void osd_object_write_lock(const struct lu_env *env,
2248                                   struct dt_object *dt, unsigned role)
2249 {
2250         struct osd_object *obj = osd_dt_obj(dt);
2251         struct osd_thread_info *oti = osd_oti_get(env);
2252
2253         LINVRNT(osd_invariant(obj));
2254
2255         LASSERT(obj->oo_owner != env);
2256         down_write_nested(&obj->oo_sem, role);
2257
2258         LASSERT(obj->oo_owner == NULL);
2259         obj->oo_owner = env;
2260         oti->oti_w_locks++;
2261 }
2262
2263 static void osd_object_read_unlock(const struct lu_env *env,
2264                                    struct dt_object *dt)
2265 {
2266         struct osd_object *obj = osd_dt_obj(dt);
2267         struct osd_thread_info *oti = osd_oti_get(env);
2268
2269         LINVRNT(osd_invariant(obj));
2270
2271         LASSERT(oti->oti_r_locks > 0);
2272         oti->oti_r_locks--;
2273         up_read(&obj->oo_sem);
2274 }
2275
2276 static void osd_object_write_unlock(const struct lu_env *env,
2277                                     struct dt_object *dt)
2278 {
2279         struct osd_object *obj = osd_dt_obj(dt);
2280         struct osd_thread_info *oti = osd_oti_get(env);
2281
2282         LINVRNT(osd_invariant(obj));
2283
2284         LASSERT(obj->oo_owner == env);
2285         LASSERT(oti->oti_w_locks > 0);
2286         oti->oti_w_locks--;
2287         obj->oo_owner = NULL;
2288         up_write(&obj->oo_sem);
2289 }
2290
2291 static int osd_object_write_locked(const struct lu_env *env,
2292                                    struct dt_object *dt)
2293 {
2294         struct osd_object *obj = osd_dt_obj(dt);
2295
2296         LINVRNT(osd_invariant(obj));
2297
2298         return obj->oo_owner == env;
2299 }
2300
2301 static struct timespec *osd_inode_time(const struct lu_env *env,
2302                                        struct inode *inode, __u64 seconds)
2303 {
2304         struct osd_thread_info  *oti = osd_oti_get(env);
2305         struct timespec         *t   = &oti->oti_time;
2306
2307         t->tv_sec = seconds;
2308         t->tv_nsec = 0;
2309         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2310         return t;
2311 }
2312
2313 static void osd_inode_getattr(const struct lu_env *env,
2314                               struct inode *inode, struct lu_attr *attr)
2315 {
2316         attr->la_valid  |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2317                            LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2318                            LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2319                            LA_TYPE;
2320
2321         attr->la_atime   = LTIME_S(inode->i_atime);
2322         attr->la_mtime   = LTIME_S(inode->i_mtime);
2323         attr->la_ctime   = LTIME_S(inode->i_ctime);
2324         attr->la_mode    = inode->i_mode;
2325         attr->la_size    = i_size_read(inode);
2326         attr->la_blocks  = inode->i_blocks;
2327         attr->la_uid     = i_uid_read(inode);
2328         attr->la_gid     = i_gid_read(inode);
2329         attr->la_flags   = ll_inode_to_ext_flags(inode->i_flags);
2330         attr->la_nlink   = inode->i_nlink;
2331         attr->la_rdev    = inode->i_rdev;
2332         attr->la_blksize = 1 << inode->i_blkbits;
2333         attr->la_blkbits = inode->i_blkbits;
2334 }
2335
2336 static int osd_attr_get(const struct lu_env *env,
2337                         struct dt_object *dt,
2338                         struct lu_attr *attr)
2339 {
2340         struct osd_object *obj = osd_dt_obj(dt);
2341
2342         if (unlikely(!dt_object_exists(dt)))
2343                 return -ENOENT;
2344         if (unlikely(obj->oo_destroyed))
2345                 return -ENOENT;
2346
2347         LASSERT(!dt_object_remote(dt));
2348         LINVRNT(osd_invariant(obj));
2349
2350         spin_lock(&obj->oo_guard);
2351         osd_inode_getattr(env, obj->oo_inode, attr);
2352         if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2353                 attr->la_flags |= LUSTRE_ORPHAN_FL;
2354         spin_unlock(&obj->oo_guard);
2355
2356         return 0;
2357 }
2358
2359 static int osd_declare_attr_set(const struct lu_env *env,
2360                                 struct dt_object *dt,
2361                                 const struct lu_attr *attr,
2362                                 struct thandle *handle)
2363 {
2364         struct osd_thandle     *oh;
2365         struct osd_object      *obj;
2366         struct osd_thread_info *info = osd_oti_get(env);
2367         struct lquota_id_info  *qi = &info->oti_qi;
2368         qid_t                   uid;
2369         qid_t                   gid;
2370         long long               bspace;
2371         int                     rc = 0;
2372         bool                    enforce;
2373         ENTRY;
2374
2375         LASSERT(dt != NULL);
2376         LASSERT(handle != NULL);
2377
2378         obj = osd_dt_obj(dt);
2379         LASSERT(osd_invariant(obj));
2380
2381         oh = container_of0(handle, struct osd_thandle, ot_super);
2382         LASSERT(oh->ot_handle == NULL);
2383
2384         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2385                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2386
2387         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2388                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2389
2390         if (attr == NULL || obj->oo_inode == NULL)
2391                 RETURN(rc);
2392
2393         bspace   = obj->oo_inode->i_blocks;
2394         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2395         bspace   = toqb(bspace);
2396
2397         /* Changing ownership is always preformed by super user, it should not
2398          * fail with EDQUOT.
2399          *
2400          * We still need to call the osd_declare_qid() to calculate the journal
2401          * credits for updating quota accounting files and to trigger quota
2402          * space adjustment once the operation is completed.*/
2403         if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2404                 /* USERQUOTA */
2405                 uid = i_uid_read(obj->oo_inode);
2406                 qi->lqi_type = USRQUOTA;
2407                 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2408                 /* inode accounting */
2409                 qi->lqi_is_blk = false;
2410
2411                 /* one more inode for the new uid ... */
2412                 qi->lqi_id.qid_uid = attr->la_uid;
2413                 qi->lqi_space      = 1;
2414                 /* Reserve credits for the new uid */
2415                 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2416                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2417                         rc = 0;
2418                 if (rc)
2419                         RETURN(rc);
2420
2421                 /* and one less inode for the current uid */
2422                 qi->lqi_id.qid_uid = uid;
2423                 qi->lqi_space      = -1;
2424                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2425                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2426                         rc = 0;
2427                 if (rc)
2428                         RETURN(rc);
2429
2430                 /* block accounting */
2431                 qi->lqi_is_blk = true;
2432
2433                 /* more blocks for the new uid ... */
2434                 qi->lqi_id.qid_uid = attr->la_uid;
2435                 qi->lqi_space      = bspace;
2436                 /*
2437                  * Credits for the new uid has been reserved, re-use "obj"
2438                  * to save credit reservation.
2439                  */
2440                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2441                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2442                         rc = 0;
2443                 if (rc)
2444                         RETURN(rc);
2445
2446                 /* and finally less blocks for the current uid */
2447                 qi->lqi_id.qid_uid = uid;
2448                 qi->lqi_space      = -bspace;
2449                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2450                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2451                         rc = 0;
2452                 if (rc)
2453                         RETURN(rc);
2454
2455                 /* GROUP QUOTA */
2456                 gid = i_gid_read(obj->oo_inode);
2457                 qi->lqi_type = GRPQUOTA;
2458                 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2459
2460                 /* inode accounting */
2461                 qi->lqi_is_blk = false;
2462
2463                 /* one more inode for the new gid ... */
2464                 qi->lqi_id.qid_gid = attr->la_gid;
2465                 qi->lqi_space      = 1;
2466                 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2467                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2468                         rc = 0;
2469                 if (rc)
2470                         RETURN(rc);
2471
2472                 /* and one less inode for the current gid */
2473                 qi->lqi_id.qid_gid = gid;
2474                 qi->lqi_space      = -1;
2475                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2476                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2477                         rc = 0;
2478                 if (rc)
2479                         RETURN(rc);
2480
2481                 /* block accounting */
2482                 qi->lqi_is_blk = true;
2483
2484                 /* more blocks for the new gid ... */
2485                 qi->lqi_id.qid_gid = attr->la_gid;
2486                 qi->lqi_space      = bspace;
2487                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2488                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2489                         rc = 0;
2490                 if (rc)
2491                         RETURN(rc);
2492
2493                 /* and finally less blocks for the current gid */
2494                 qi->lqi_id.qid_gid = gid;
2495                 qi->lqi_space      = -bspace;
2496                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2497                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2498                         rc = 0;
2499                 if (rc)
2500                         RETURN(rc);
2501         }
2502
2503         RETURN(rc);
2504 }
2505
2506 static int osd_inode_setattr(const struct lu_env *env,
2507                              struct inode *inode, const struct lu_attr *attr)
2508 {
2509         __u64 bits = attr->la_valid;
2510
2511         /* Only allow set size for regular file */
2512         if (!S_ISREG(inode->i_mode))
2513                 bits &= ~(LA_SIZE | LA_BLOCKS);
2514
2515         if (bits == 0)
2516                 return 0;
2517
2518         if (bits & LA_ATIME)
2519                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
2520         if (bits & LA_CTIME)
2521                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
2522         if (bits & LA_MTIME)
2523                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
2524         if (bits & LA_SIZE) {
2525                 spin_lock(&inode->i_lock);
2526                 LDISKFS_I(inode)->i_disksize = attr->la_size;
2527                 i_size_write(inode, attr->la_size);
2528                 spin_unlock(&inode->i_lock);
2529         }
2530
2531         /* OSD should not change "i_blocks" which is used by quota.
2532          * "i_blocks" should be changed by ldiskfs only. */
2533         if (bits & LA_MODE)
2534                 inode->i_mode = (inode->i_mode & S_IFMT) |
2535                                 (attr->la_mode & ~S_IFMT);
2536         if (bits & LA_UID)
2537                 i_uid_write(inode, attr->la_uid);
2538         if (bits & LA_GID)
2539                 i_gid_write(inode, attr->la_gid);
2540         if (bits & LA_NLINK)
2541                 set_nlink(inode, attr->la_nlink);
2542         if (bits & LA_RDEV)
2543                 inode->i_rdev = attr->la_rdev;
2544
2545         if (bits & LA_FLAGS) {
2546                 /* always keep S_NOCMTIME */
2547                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2548                                  S_NOCMTIME;
2549         }
2550         return 0;
2551 }
2552
2553 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2554 {
2555         if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2556             (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2557                 struct iattr    iattr;
2558                 int             rc;
2559
2560                 ll_vfs_dq_init(inode);
2561                 iattr.ia_valid = 0;
2562                 if (attr->la_valid & LA_UID)
2563                         iattr.ia_valid |= ATTR_UID;
2564                 if (attr->la_valid & LA_GID)
2565                         iattr.ia_valid |= ATTR_GID;
2566                 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2567                 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2568
2569                 rc = ll_vfs_dq_transfer(inode, &iattr);
2570                 if (rc) {
2571                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
2572                                "enforcement enabled on the ldiskfs "
2573                                "filesystem?\n", inode->i_sb->s_id, rc);
2574                         return rc;
2575                 }
2576         }
2577         return 0;
2578 }
2579
2580 static int osd_attr_set(const struct lu_env *env,
2581                         struct dt_object *dt,
2582                         const struct lu_attr *attr,
2583                         struct thandle *handle)
2584 {
2585         struct osd_object *obj = osd_dt_obj(dt);
2586         struct inode      *inode;
2587         int rc;
2588
2589         if (!dt_object_exists(dt))
2590                 return -ENOENT;
2591
2592         LASSERT(handle != NULL);
2593         LASSERT(!dt_object_remote(dt));
2594         LASSERT(osd_invariant(obj));
2595
2596         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2597
2598         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2599                 struct osd_thread_info  *oti  = osd_oti_get(env);
2600                 const struct lu_fid     *fid0 = lu_object_fid(&dt->do_lu);
2601                 struct lu_fid           *fid1 = &oti->oti_fid;
2602                 struct osd_inode_id     *id   = &oti->oti_id;
2603                 struct iam_path_descr   *ipd;
2604                 struct iam_container    *bag;
2605                 struct osd_thandle      *oh;
2606                 int                      rc;
2607
2608                 fid_cpu_to_be(fid1, fid0);
2609                 memset(id, 1, sizeof(*id));
2610                 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2611                                   fid0)->oi_dir.od_container;
2612                 ipd = osd_idx_ipd_get(env, bag);
2613                 if (unlikely(ipd == NULL))
2614                         RETURN(-ENOMEM);
2615
2616                 oh = container_of0(handle, struct osd_thandle, ot_super);
2617                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2618                                 (const struct iam_rec *)id, ipd);
2619                 osd_ipd_put(env, bag, ipd);
2620                 return(rc > 0 ? 0 : rc);
2621         }
2622
2623         inode = obj->oo_inode;
2624
2625         rc = osd_quota_transfer(inode, attr);
2626         if (rc)
2627                 return rc;
2628
2629         spin_lock(&obj->oo_guard);
2630         rc = osd_inode_setattr(env, inode, attr);
2631         spin_unlock(&obj->oo_guard);
2632         if (rc != 0)
2633                 GOTO(out, rc);
2634
2635         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2636
2637         if (!(attr->la_valid & LA_FLAGS))
2638                 GOTO(out, rc);
2639
2640         /* Let's check if there are extra flags need to be set into LMA */
2641         if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2642                 struct osd_thread_info *info = osd_oti_get(env);
2643                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2644
2645                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2646                 if (rc != 0)
2647                         GOTO(out, rc);
2648
2649                 lma->lma_incompat |=
2650                         lustre_to_lma_flags(attr->la_flags);
2651                 lustre_lma_swab(lma);
2652                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2653                                      lma, sizeof(*lma), XATTR_REPLACE);
2654                 if (rc != 0) {
2655                         struct osd_device *osd = osd_obj2dev(obj);
2656
2657                         CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2658                               osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2659                               lma->lma_incompat, rc);
2660                 } else {
2661                         obj->oo_lma_flags =
2662                                 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2663                 }
2664                 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2665         }
2666 out:
2667         osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2668
2669         return rc;
2670 }
2671
2672 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2673                                            struct osd_object *obj,
2674                                            const char *name, const int namelen)
2675 {
2676         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2677 }
2678
2679 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2680                       umode_t mode, struct dt_allocation_hint *hint,
2681                       struct thandle *th)
2682 {
2683         int result;
2684         struct osd_device  *osd = osd_obj2dev(obj);
2685         struct osd_thandle *oth;
2686         struct dt_object   *parent = NULL;
2687         struct inode       *inode;
2688
2689         LINVRNT(osd_invariant(obj));
2690         LASSERT(obj->oo_inode == NULL);
2691         LASSERT(obj->oo_hl_head == NULL);
2692
2693         if (S_ISDIR(mode) && ldiskfs_pdo) {
2694                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2695                 if (obj->oo_hl_head == NULL)
2696                         return -ENOMEM;
2697         }
2698
2699         oth = container_of(th, struct osd_thandle, ot_super);
2700         LASSERT(oth->ot_handle->h_transaction != NULL);
2701
2702         if (hint != NULL && hint->dah_parent != NULL &&
2703             !dt_object_remote(hint->dah_parent))
2704                 parent = hint->dah_parent;
2705
2706         inode = ldiskfs_create_inode(oth->ot_handle,
2707                                      parent ? osd_dt_obj(parent)->oo_inode :
2708                                               osd_sb(osd)->s_root->d_inode,
2709                                      mode);
2710         if (!IS_ERR(inode)) {
2711                 /* Do not update file c/mtime in ldiskfs. */
2712                 inode->i_flags |= S_NOCMTIME;
2713
2714                 /* For new created object, it must be consistent,
2715                  * and it is unnecessary to scrub against it. */
2716                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2717
2718                 obj->oo_inode = inode;
2719                 result = 0;
2720         } else {
2721                 if (obj->oo_hl_head != NULL) {
2722                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2723                         obj->oo_hl_head = NULL;
2724                 }
2725                 result = PTR_ERR(inode);
2726         }
2727         LINVRNT(osd_invariant(obj));
2728         return result;
2729 }
2730
2731 enum {
2732         OSD_NAME_LEN = 255
2733 };
2734
2735 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2736                      struct lu_attr *attr,
2737                      struct dt_allocation_hint *hint,
2738                      struct dt_object_format *dof,
2739                      struct thandle *th)
2740 {
2741         int result;
2742         struct osd_thandle *oth;
2743         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2744
2745         LASSERT(S_ISDIR(attr->la_mode));
2746
2747         oth = container_of(th, struct osd_thandle, ot_super);
2748         LASSERT(oth->ot_handle->h_transaction != NULL);
2749         result = osd_mkfile(info, obj, mode, hint, th);
2750
2751         return result;
2752 }
2753
2754 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2755                         struct lu_attr *attr,
2756                         struct dt_allocation_hint *hint,
2757                         struct dt_object_format *dof,
2758                         struct thandle *th)
2759 {
2760         int result;
2761         struct osd_thandle *oth;
2762         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2763
2764         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2765
2766         LASSERT(S_ISREG(attr->la_mode));
2767
2768         oth = container_of(th, struct osd_thandle, ot_super);
2769         LASSERT(oth->ot_handle->h_transaction != NULL);
2770
2771         result = osd_mkfile(info, obj, mode, hint, th);
2772         if (result == 0) {
2773                 LASSERT(obj->oo_inode != NULL);
2774                 if (feat->dif_flags & DT_IND_VARKEY)
2775                         result = iam_lvar_create(obj->oo_inode,
2776                                                  feat->dif_keysize_max,
2777                                                  feat->dif_ptrsize,
2778                                                  feat->dif_recsize_max,
2779                                                  oth->ot_handle);
2780                 else
2781                         result = iam_lfix_create(obj->oo_inode,
2782                                                  feat->dif_keysize_max,
2783                                                  feat->dif_ptrsize,
2784                                                  feat->dif_recsize_max,
2785                                                  oth->ot_handle);
2786
2787         }
2788         return result;
2789 }
2790
2791 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2792                      struct lu_attr *attr,
2793                      struct dt_allocation_hint *hint,
2794                      struct dt_object_format *dof,
2795                      struct thandle *th)
2796 {
2797         LASSERT(S_ISREG(attr->la_mode));
2798         return osd_mkfile(info, obj, (attr->la_mode &
2799                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2800 }
2801
2802 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2803                      struct lu_attr *attr,
2804                      struct dt_allocation_hint *hint,
2805                      struct dt_object_format *dof,
2806                      struct thandle *th)
2807 {
2808         LASSERT(S_ISLNK(attr->la_mode));
2809         return osd_mkfile(info, obj, (attr->la_mode &
2810                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2811 }
2812
2813 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2814                      struct lu_attr *attr,
2815                      struct dt_allocation_hint *hint,
2816                      struct dt_object_format *dof,
2817                      struct thandle *th)
2818 {
2819         umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2820         int result;
2821
2822         LINVRNT(osd_invariant(obj));
2823         LASSERT(obj->oo_inode == NULL);
2824         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2825                 S_ISFIFO(mode) || S_ISSOCK(mode));
2826
2827         result = osd_mkfile(info, obj, mode, hint, th);
2828         if (result == 0) {
2829                 LASSERT(obj->oo_inode != NULL);
2830                 /*
2831                  * This inode should be marked dirty for i_rdev.  Currently
2832                  * that is done in the osd_attr_init().
2833                  */
2834                 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2835                                    attr->la_rdev);
2836         }
2837         LINVRNT(osd_invariant(obj));
2838         return result;
2839 }
2840
2841 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2842                               struct lu_attr *,
2843                               struct dt_allocation_hint *hint,
2844                               struct dt_object_format *dof,
2845                               struct thandle *);
2846
2847 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2848 {
2849         osd_obj_type_f result;
2850
2851         switch (type) {
2852         case DFT_DIR:
2853                 result = osd_mkdir;
2854                 break;
2855         case DFT_REGULAR:
2856                 result = osd_mkreg;
2857                 break;
2858         case DFT_SYM:
2859                 result = osd_mksym;
2860                 break;
2861         case DFT_NODE:
2862                 result = osd_mknod;
2863                 break;
2864         case DFT_INDEX:
2865                 result = osd_mk_index;
2866                 break;
2867
2868         default:
2869                 LBUG();
2870                 break;
2871         }
2872         return result;
2873 }
2874
2875
2876 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2877                         struct dt_object *parent, struct dt_object *child,
2878                         umode_t child_mode)
2879 {
2880         LASSERT(ah);
2881
2882         ah->dah_parent = parent;
2883         ah->dah_mode = child_mode;
2884
2885         if (parent != NULL && !dt_object_remote(parent)) {
2886                 /* will help to find FID->ino at dt_insert("..") */
2887                 struct osd_object *pobj = osd_dt_obj(parent);
2888                 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2889         }
2890 }
2891
2892 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2893                           struct lu_attr *attr, struct dt_object_format *dof)
2894 {
2895         struct inode   *inode = obj->oo_inode;
2896         __u64           valid = attr->la_valid;
2897         int             result;
2898
2899         attr->la_valid &= ~(LA_TYPE | LA_MODE);
2900
2901         if (dof->dof_type != DFT_NODE)
2902                 attr->la_valid &= ~LA_RDEV;
2903         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2904                 attr->la_valid &= ~LA_ATIME;
2905         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2906                 attr->la_valid &= ~LA_CTIME;
2907         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2908                 attr->la_valid &= ~LA_MTIME;
2909
2910         result = osd_quota_transfer(inode, attr);
2911         if (result)
2912                 return;
2913
2914         if (attr->la_valid != 0) {
2915                 result = osd_inode_setattr(info->oti_env, inode, attr);
2916                 /*
2917                  * The osd_inode_setattr() should always succeed here.  The
2918                  * only error that could be returned is EDQUOT when we are
2919                  * trying to change the UID or GID of the inode. However, this
2920                  * should not happen since quota enforcement is no longer
2921                  * enabled on ldiskfs (lquota takes care of it).
2922                  */
2923                 LASSERTF(result == 0, "%d\n", result);
2924                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2925         }
2926
2927         attr->la_valid = valid;
2928 }
2929
2930 /**
2931  * Helper function for osd_object_create()
2932  *
2933  * \retval 0, on success
2934  */
2935 static int __osd_object_create(struct osd_thread_info *info,
2936                                struct osd_object *obj, struct lu_attr *attr,
2937                                struct dt_allocation_hint *hint,
2938                                struct dt_object_format *dof,
2939                                struct thandle *th)
2940 {
2941         int     result;
2942         __u32   umask;
2943
2944         osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2945
2946         /* we drop umask so that permissions we pass are not affected */
2947         umask = current->fs->umask;
2948         current->fs->umask = 0;
2949
2950         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2951                                                   th);
2952         if (likely(obj->oo_inode != NULL)) {
2953                 LASSERT(obj->oo_inode->i_state & I_NEW);
2954
2955                 /* Unlock the inode before attr initialization to avoid
2956                  * unnecessary dqget operations. LU-6378 */
2957                 unlock_new_inode(obj->oo_inode);
2958         }
2959
2960         if (likely(result == 0)) {
2961                 osd_attr_init(info, obj, attr, dof);
2962                 osd_object_init0(obj);
2963         }
2964
2965         /* restore previous umask value */
2966         current->fs->umask = umask;
2967
2968         osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2969
2970         return result;
2971 }
2972
2973 /**
2974  * Helper function for osd_object_create()
2975  *
2976  * \retval 0, on success
2977  */
2978 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2979                            const struct lu_fid *fid, struct thandle *th)
2980 {
2981         struct osd_thread_info *info = osd_oti_get(env);
2982         struct osd_inode_id    *id   = &info->oti_id;
2983         struct osd_device      *osd  = osd_obj2dev(obj);
2984         struct osd_thandle     *oh;
2985         int                     rc;
2986
2987         LASSERT(obj->oo_inode != NULL);
2988
2989         oh = container_of0(th, struct osd_thandle, ot_super);
2990         LASSERT(oh->ot_handle);
2991         osd_trans_exec_op(env, th, OSD_OT_INSERT);
2992
2993         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2994         rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
2995                            OI_CHECK_FLD, NULL);
2996         osd_trans_exec_check(env, th, OSD_OT_INSERT);
2997
2998         return rc;
2999 }
3000
3001 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3002                    u64 seq, struct lu_seq_range *range)
3003 {
3004         struct seq_server_site  *ss = osd_seq_site(osd);
3005
3006         if (fid_seq_is_idif(seq)) {
3007                 fld_range_set_ost(range);
3008                 range->lsr_index = idif_ost_idx(seq);
3009                 return 0;
3010         }
3011
3012         if (!fid_seq_in_fldb(seq)) {
3013                 fld_range_set_mdt(range);
3014                 if (ss != NULL)
3015                         /* FIXME: If ss is NULL, it suppose not get lsr_index
3016                          * at all */
3017                         range->lsr_index = ss->ss_node_id;
3018                 return 0;
3019         }
3020
3021         LASSERT(ss != NULL);
3022         fld_range_set_any(range);
3023         /* OSD will only do local fld lookup */
3024         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3025 }
3026
3027 /*
3028  * Concurrency: no external locking is necessary.
3029  */
3030 static int osd_declare_object_create(const struct lu_env *env,
3031                                      struct dt_object *dt,
3032                                      struct lu_attr *attr,
3033                                      struct dt_allocation_hint *hint,
3034                                      struct dt_object_format *dof,
3035                                      struct thandle *handle)
3036 {
3037         struct osd_thandle      *oh;
3038         int                      rc;
3039         ENTRY;
3040
3041         LASSERT(handle != NULL);
3042
3043         oh = container_of0(handle, struct osd_thandle, ot_super);
3044         LASSERT(oh->ot_handle == NULL);
3045
3046         /* EA object consumes more credits than regular object: osd_mk_index
3047          * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3048          * leaf_node, could involves the block, block bitmap, groups, GDT
3049          * change for each block, so add 4 * 2 credits in that case. */
3050         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3051                              osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3052                              (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3053         /* Reuse idle OI block may cause additional one OI block
3054          * to be changed. */
3055         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3056                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3057
3058         if (!attr)
3059                 RETURN(0);
3060
3061         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3062                                    osd_dt_obj(dt), false, NULL, false);
3063         if (rc != 0)
3064                 RETURN(rc);
3065
3066         /* will help to find FID->ino mapping at dt_insert() */
3067         rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3068                                    osd_dt_obj(dt));
3069
3070         RETURN(rc);
3071 }
3072
3073 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3074                              struct lu_attr *attr,
3075                              struct dt_allocation_hint *hint,
3076                              struct dt_object_format *dof, struct thandle *th)
3077 {
3078         const struct lu_fid     *fid    = lu_object_fid(&dt->do_lu);
3079         struct osd_object       *obj    = osd_dt_obj(dt);
3080         struct osd_thread_info  *info   = osd_oti_get(env);
3081         int result;
3082         ENTRY;
3083
3084         if (dt_object_exists(dt))
3085                 return -EEXIST;
3086
3087         LINVRNT(osd_invariant(obj));
3088         LASSERT(!dt_object_remote(dt));
3089         LASSERT(osd_write_locked(env, obj));
3090         LASSERT(th != NULL);
3091
3092         if (unlikely(fid_is_acct(fid)))
3093                 /* Quota files can't be created from the kernel any more,
3094                  * 'tune2fs -O quota' will take care of creating them */
3095                 RETURN(-EPERM);
3096
3097         result = __osd_object_create(info, obj, attr, hint, dof, th);
3098         if (result == 0) {
3099                 result = __osd_oi_insert(env, obj, fid, th);
3100                 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3101                         obj->oo_dt.do_body_ops = &osd_body_ops;
3102         }
3103         LASSERT(ergo(result == 0,
3104                 dt_object_exists(dt) && !dt_object_remote(dt)));
3105
3106         LASSERT(osd_invariant(obj));
3107         RETURN(result);
3108 }
3109
3110 /**
3111  * Called to destroy on-disk representation of the object
3112  *
3113  * Concurrency: must be locked
3114  */
3115 static int osd_declare_object_destroy(const struct lu_env *env,
3116                                       struct dt_object *dt,
3117                                       struct thandle *th)
3118 {
3119         struct osd_object  *obj = osd_dt_obj(dt);
3120         struct inode       *inode = obj->oo_inode;
3121         struct osd_thandle *oh;
3122         int                 rc;
3123         ENTRY;
3124
3125         if (inode == NULL)
3126                 RETURN(-ENOENT);
3127
3128         oh = container_of0(th, struct osd_thandle, ot_super);
3129         LASSERT(oh->ot_handle == NULL);
3130
3131         osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3132                              osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3133         /* Recycle idle OI leaf may cause additional three OI blocks
3134          * to be changed. */
3135         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3136                 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3137                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3138         /* one less inode */
3139         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3140                                    -1, oh, obj, false, NULL, false);
3141         if (rc)
3142                 RETURN(rc);
3143         /* data to be truncated */
3144         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3145                                    0, oh, obj, true, NULL, false);
3146         if (rc)
3147                 RETURN(rc);
3148
3149         /* will help to find FID->ino when this object is being
3150          * added to PENDING/ */
3151         rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3152
3153         RETURN(rc);
3154 }
3155
3156 static int osd_object_destroy(const struct lu_env *env,
3157                               struct dt_object *dt,
3158                               struct thandle *th)
3159 {
3160         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
3161         struct osd_object      *obj = osd_dt_obj(dt);
3162         struct inode           *inode = obj->oo_inode;
3163         struct osd_device      *osd = osd_obj2dev(obj);
3164         struct osd_thandle     *oh;
3165         int                     result;
3166         ENTRY;
3167
3168         oh = container_of0(th, struct osd_thandle, ot_super);
3169         LASSERT(oh->ot_handle);
3170         LASSERT(inode);
3171         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3172
3173         if (unlikely(fid_is_acct(fid)))
3174                 RETURN(-EPERM);
3175
3176         if (S_ISDIR(inode->i_mode)) {
3177                 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3178                         inode->i_nlink == 2);
3179                 /* it will check/delete the inode from remote parent,
3180                  * how to optimize it? unlink performance impaction XXX */
3181                 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3182                 if (result != 0)
3183                         CERROR("%s: delete inode "DFID": rc = %d\n",
3184                                osd_name(osd), PFID(fid), result);
3185
3186                 spin_lock(&obj->oo_guard);
3187                 clear_nlink(inode);
3188                 spin_unlock(&obj->oo_guard);
3189                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3190         }
3191
3192         osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3193
3194         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3195
3196         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3197                 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3198                                        oh->ot_handle, OI_CHECK_FLD);
3199
3200         osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3201         /* XXX: add to ext3 orphan list */
3202         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3203
3204         /* not needed in the cache anymore */
3205         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3206         obj->oo_destroyed = 1;
3207
3208         RETURN(0);
3209 }
3210
3211 /**
3212  * Put the fid into lustre_mdt_attrs, and then place the structure
3213  * inode's ea. This fid should not be altered during the life time
3214  * of the inode.
3215  *
3216  * \retval +ve, on success
3217  * \retval -ve, on error
3218  *
3219  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3220  */
3221 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3222                    const struct lu_fid *fid, __u32 compat, __u32 incompat)
3223 {
3224         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3225         int                      rc;
3226         ENTRY;
3227
3228         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3229                 RETURN(0);
3230
3231         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3232                 rc = -ENOMEM;
3233
3234         lustre_lma_init(lma, fid, compat, incompat);
3235         lustre_lma_swab(lma);
3236
3237         rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3238                              XATTR_CREATE);
3239         /* LMA may already exist, but we need to check that all the
3240          * desired compat/incompat flags have been added. */
3241         if (unlikely(rc == -EEXIST)) {
3242                 if (compat == 0 && incompat == 0)
3243                         RETURN(0);
3244
3245                 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3246                                      XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3247                                      LMA_OLD_SIZE);
3248                 if (rc <= 0)
3249                         RETURN(-EINVAL);
3250
3251                 lustre_lma_swab(lma);
3252                 if (!(~lma->lma_compat & compat) &&
3253                     !(~lma->lma_incompat & incompat))
3254                         RETURN(0);
3255
3256                 lma->lma_compat |= compat;
3257                 lma->lma_incompat |= incompat;
3258                 lustre_lma_swab(lma);
3259                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3260                                      sizeof(*lma), XATTR_REPLACE);
3261         }
3262
3263         RETURN(rc);
3264 }
3265
3266 /**
3267  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3268  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3269  * To have compatilibility with 1.8 ldiskfs driver we need to have
3270  * magic number at start of fid data.
3271  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3272  * its inmemory API.
3273  */
3274 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3275                                          const struct lu_fid *fid)
3276 {
3277         if (!fid_is_namespace_visible(fid) ||
3278             OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3279                 param->edp_magic = 0;
3280                 return;
3281         }
3282
3283         param->edp_magic = LDISKFS_LUFID_MAGIC;
3284         param->edp_len =  sizeof(struct lu_fid) + 1;
3285         fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3286 }
3287
3288 /**
3289  * Try to read the fid from inode ea into dt_rec.
3290  *
3291  * \param fid object fid.
3292  *
3293  * \retval 0 on success
3294  */
3295 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3296                           __u32 ino, struct lu_fid *fid,
3297                           struct osd_inode_id *id)
3298 {
3299         struct osd_thread_info *info  = osd_oti_get(env);
3300         struct inode           *inode;
3301         ENTRY;
3302
3303         osd_id_gen(id, ino, OSD_OII_NOGEN);
3304         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3305         if (IS_ERR(inode))
3306                 RETURN(PTR_ERR(inode));
3307
3308         iput(inode);
3309         RETURN(0);
3310 }
3311
3312 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3313                                         struct inode *dir,
3314                                         struct inode *parent_dir,
3315                                         const struct lu_fid *dot_fid,
3316                                         const struct lu_fid *dot_dot_fid,
3317                                         struct osd_thandle *oth)
3318 {
3319         struct ldiskfs_dentry_param *dot_ldp;
3320         struct ldiskfs_dentry_param *dot_dot_ldp;
3321         __u32 saved_nlink = dir->i_nlink;
3322         int rc;
3323
3324         dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3325         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3326
3327         dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3328         dot_ldp->edp_magic = 0;
3329
3330         rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3331                                     dir, dot_ldp, dot_dot_ldp);
3332         /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3333          * the subseqent ref_add() will increase the dir->i_nlink
3334          * as 3. That is incorrect for new created directory.
3335          *
3336          * It looks like hack, because we want to make the OSD API
3337          * to be order-independent for new created directory object
3338          * between dt_insert(..) and ref_add() operations.
3339          *
3340          * Here, we only restore the in-RAM dir-inode's nlink attr,
3341          * becuase if the nlink attr is not 2, then there will be
3342          * ref_add() called following the dt_insert(..), such call
3343          * will make both the in-RAM and on-disk dir-inode's nlink
3344          * attr to be set as 2. LU-7447 */
3345         set_nlink(dir, saved_nlink);
3346         return rc;
3347 }
3348
3349 /**
3350  * Create an local agent inode for remote entry
3351  */
3352 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3353                                                   struct osd_device *osd,
3354                                                   struct osd_object *pobj,
3355                                                   const struct lu_fid *fid,
3356                                                   __u32 type,
3357                                                   struct thandle *th)
3358 {
3359         struct osd_thread_info  *info = osd_oti_get(env);
3360         struct inode            *local;
3361         struct osd_thandle      *oh;
3362         int                     rc;
3363         ENTRY;
3364
3365         LASSERT(th);
3366         oh = container_of(th, struct osd_thandle, ot_super);
3367         LASSERT(oh->ot_handle->h_transaction != NULL);
3368
3369         local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3370         if (IS_ERR(local)) {
3371                 CERROR("%s: create local error %d\n", osd_name(osd),
3372                        (int)PTR_ERR(local));
3373                 RETURN(local);
3374         }
3375
3376         /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3377          * correct gid on remote file, not agent here */
3378         local->i_gid = current_fsgid();
3379         ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3380         unlock_new_inode(local);
3381
3382         /* Set special LMA flag for local agent inode */
3383         rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3384         if (rc != 0) {
3385                 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3386                        osd_name(osd), PFID(fid), rc);
3387                 RETURN(ERR_PTR(rc));
3388         }
3389
3390         if (!S_ISDIR(type))
3391                 RETURN(local);
3392
3393         rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3394                                          lu_object_fid(&pobj->oo_dt.do_lu),
3395                                          fid, oh);
3396         if (rc != 0) {
3397                 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3398                         osd_name(osd), PFID(fid), rc);
3399                 RETURN(ERR_PTR(rc));
3400         }
3401
3402         RETURN(local);
3403 }
3404
3405 /**
3406  * when direntry is deleted, we have to take care of possible agent inode
3407  * referenced by that. unfortunately we can't do this at that point:
3408  * iget() within a running transaction leads to deadlock and we better do
3409  * not call that every delete declaration to save performance. so we put
3410  * a potention agent inode on a list and process that once the transaction
3411  * is over. Notice it's not any worse in terms of real orphans as regular
3412  * object destroy doesn't put inodes on the on-disk orphan list. this should
3413  * be addressed separately
3414  */
3415 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3416                                             struct osd_thandle *oh,
3417                                             __u32 ino)
3418 {
3419         struct osd_device      *osd = osd_dt_dev(oh->ot_super.th_dev);
3420         struct osd_obj_orphan *oor;
3421
3422         OBD_ALLOC_PTR(oor);
3423         if (oor == NULL)
3424                 return -ENOMEM;
3425
3426         oor->oor_ino = ino;
3427         oor->oor_env = (struct lu_env *)env;
3428         spin_lock(&osd->od_osfs_lock);
3429         list_add(&oor->oor_list, &osd->od_orphan_list);
3430         spin_unlock(&osd->od_osfs_lock);
3431
3432         oh->ot_remove_agents = 1;
3433
3434         return 0;
3435
3436 }
3437
3438 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3439                                                 struct osd_device *osd)
3440 {
3441         struct osd_thread_info *info = osd_oti_get(env);
3442         struct osd_obj_orphan *oor, *tmp;
3443         struct osd_inode_id id;
3444         struct list_head list;
3445         struct inode *inode;
3446         struct lu_fid fid;
3447         handle_t *jh;
3448         __u32 ino;
3449
3450         INIT_LIST_HEAD(&list);
3451
3452         spin_lock(&osd->od_osfs_lock);
3453         list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3454                 if (oor->oor_env == env) {
3455                         list_del(&oor->oor_list);
3456                         list_add(&oor->oor_list, &list);
3457                 }
3458         }
3459         spin_unlock(&osd->od_osfs_lock);
3460
3461         list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3462
3463                 ino = oor->oor_ino;
3464
3465                 list_del(&oor->oor_list);
3466                 OBD_FREE_PTR(oor);
3467
3468                 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3469                 inode = osd_iget_fid(info, osd, &id, &fid);
3470                 if (IS_ERR(inode))
3471                         continue;
3472
3473                 if (!osd_remote_fid(env, osd, &fid)) {
3474                         iput(inode);
3475                         continue;
3476                 }
3477
3478                 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3479                 clear_nlink(inode);
3480                 mark_inode_dirty(inode);
3481                 ldiskfs_journal_stop(jh);
3482                 iput(inode);
3483         }
3484
3485         return 0;
3486 }
3487
3488 /**
3489  * OSD layer object create function for interoperability mode (b11826).
3490  * This is mostly similar to osd_object_create(). Only difference being, fid is
3491  * inserted into inode ea here.
3492  *
3493  * \retval   0, on success
3494  * \retval -ve, on error
3495  */
3496 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3497                                 struct lu_attr *attr,
3498                                 struct dt_allocation_hint *hint,
3499                                 struct dt_object_format *dof,
3500                                 struct thandle *th)
3501 {
3502         const struct lu_fid     *fid    = lu_object_fid(&dt->do_lu);
3503         struct osd_object       *obj    = osd_dt_obj(dt);
3504         struct osd_thread_info  *info   = osd_oti_get(env);
3505         int                      result, on_ost = 0;
3506
3507         ENTRY;
3508
3509         if (dt_object_exists(dt))
3510                 RETURN(-EEXIST);
3511
3512         LASSERT(osd_invariant(obj));
3513         LASSERT(!dt_object_remote(dt));
3514         LASSERT(osd_write_locked(env, obj));
3515         LASSERT(th != NULL);
3516
3517         if (unlikely(fid_is_acct(fid)))
3518                 /* Quota files can't be created from the kernel any more,
3519                  * 'tune2fs -O quota' will take care of creating them */
3520                 RETURN(-EPERM);
3521
3522         result = __osd_object_create(info, obj, attr, hint, dof, th);
3523         if (result == 0) {
3524                 if (fid_is_idif(fid) &&
3525                     !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3526                         struct lu_fid *tfid = &info->oti_fid;
3527                         struct ost_id *oi   = &info->oti_ostid;
3528
3529                         fid_to_ostid(fid, oi);
3530                         ostid_to_fid(tfid, oi, 0);
3531                         on_ost = 1;
3532                         result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3533                                                 LMAC_FID_ON_OST, 0);
3534                 } else {
3535                         on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3536                                                fid, OI_CHECK_FLD);
3537                         result = osd_ea_fid_set(info, obj->oo_inode, fid,
3538                                                 on_ost ? LMAC_FID_ON_OST : 0,
3539                                                 0);
3540                 }
3541                 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3542                         obj->oo_dt.do_body_ops = &osd_body_ops;
3543         }
3544
3545         if (result == 0)
3546                 result = __osd_oi_insert(env, obj, fid, th);
3547
3548         /* a small optimization - dt_insert() isn't usually applied
3549          * to OST objects, so we don't need to cache OI mapping for
3550          * OST objects */
3551         if (result == 0 && on_ost == 0) {
3552                 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3553                 result = osd_idc_find_and_init(env, osd, obj);
3554                 LASSERT(result == 0);
3555         }
3556
3557         LASSERT(ergo(result == 0,
3558                      dt_object_exists(dt) && !dt_object_remote(dt)));
3559         LINVRNT(osd_invariant(obj));
3560         RETURN(result);
3561 }
3562
3563 static int osd_declare_object_ref_add(const struct lu_env *env,
3564                                       struct dt_object *dt,
3565                                       struct thandle *handle)
3566 {
3567         struct osd_thandle       *oh;
3568
3569         /* it's possible that object doesn't exist yet */
3570         LASSERT(handle != NULL);
3571
3572         oh = container_of0(handle, struct osd_thandle, ot_super);
3573         LASSERT(oh->ot_handle == NULL);
3574
3575         osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3576                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3577
3578         return 0;
3579 }
3580
3581 /*
3582  * Concurrency: @dt is write locked.
3583  */
3584 static int osd_object_ref_add(const struct lu_env *env,
3585                               struct dt_object *dt, struct thandle *th)
3586 {
3587         struct osd_object  *obj = osd_dt_obj(dt);
3588         struct inode       *inode = obj->oo_inode;
3589         struct osd_thandle *oh;
3590         int                 rc = 0;
3591
3592         if (!dt_object_exists(dt) || obj->oo_destroyed)
3593                 return -ENOENT;
3594
3595         LINVRNT(osd_invariant(obj));
3596         LASSERT(!dt_object_remote(dt));
3597         LASSERT(osd_write_locked(env, obj));
3598         LASSERT(th != NULL);
3599
3600         oh = container_of0(th, struct osd_thandle, ot_super);
3601         LASSERT(oh->ot_handle != NULL);
3602
3603         osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3604
3605         CDEBUG(D_INODE, DFID" increase nlink %d\n",
3606                PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3607         /*
3608          * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3609          * (65000) subdirectories by storing "1" in i_nlink if the link count
3610          * would otherwise overflow. Directory tranversal tools understand
3611          * that (st_nlink == 1) indicates that the filesystem dose not track
3612          * hard links count on the directory, and will not abort subdirectory
3613          * scanning early once (st_nlink - 2) subdirs have been found.
3614          *
3615          * This also has to properly handle the case of inodes with nlink == 0
3616          * in case they are being linked into the PENDING directory
3617          */
3618         spin_lock(&obj->oo_guard);
3619         if (unlikely(inode->i_nlink == 0))
3620                 /* inc_nlink from 0 may cause WARN_ON */
3621                 set_nlink(inode, 1);
3622         else {
3623                 ldiskfs_inc_count(oh->ot_handle, inode);
3624                 if (!S_ISDIR(inode->i_mode))
3625                         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3626         }
3627         spin_unlock(&obj->oo_guard);
3628
3629         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3630         LINVRNT(osd_invariant(obj));
3631
3632         osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3633
3634         return rc;
3635 }
3636
3637 static int osd_declare_object_ref_del(const struct lu_env *env,
3638                                       struct dt_object *dt,
3639                                       struct thandle *handle)
3640 {
3641         struct osd_thandle *oh;
3642
3643         if (!dt_object_exists(dt))
3644                 return -ENOENT;
3645
3646         LASSERT(!dt_object_remote(dt));
3647         LASSERT(handle != NULL);
3648
3649         oh = container_of0(handle, struct osd_thandle, ot_super);
3650         LASSERT(oh->ot_handle == NULL);
3651
3652         osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3653                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3654
3655         return 0;
3656 }
3657
3658 /*
3659  * Concurrency: @dt is write locked.
3660  */
3661 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3662                               struct thandle *th)
3663 {
3664         struct osd_object       *obj = osd_dt_obj(dt);
3665         struct inode            *inode = obj->oo_inode;
3666         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
3667         struct osd_thandle      *oh;
3668
3669         if (!dt_object_exists(dt))
3670                 return -ENOENT;
3671
3672         LINVRNT(osd_invariant(obj));
3673         LASSERT(!dt_object_remote(dt));
3674         LASSERT(osd_write_locked(env, obj));
3675         LASSERT(th != NULL);
3676
3677         oh = container_of0(th, struct osd_thandle, ot_super);
3678         LASSERT(oh->ot_handle != NULL);
3679
3680         osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3681
3682         spin_lock(&obj->oo_guard);
3683         /* That can be result of upgrade from old Lustre version and
3684          * applied only to local files.  Just skip this ref_del call.
3685          * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3686         if (inode->i_nlink == 0) {
3687                 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3688                              D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3689                              ", maybe an upgraded file? (LU-3915)\n",
3690                              osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3691                 spin_unlock(&obj->oo_guard);
3692                 return 0;
3693         }
3694
3695         CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3696                PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3697
3698         ldiskfs_dec_count(oh->ot_handle, inode);
3699         spin_unlock(&obj->oo_guard);
3700
3701         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3702         LINVRNT(osd_invariant(obj));
3703
3704         osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3705
3706         return 0;
3707 }
3708
3709 /*
3710  * Get the 64-bit version for an inode.
3711  */
3712 static int osd_object_version_get(const struct lu_env *env,
3713                                   struct dt_object *dt, dt_obj_version_t *ver)
3714 {
3715         struct inode *inode = osd_dt_obj(dt)->oo_inode;
3716
3717         CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3718                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3719         *ver = LDISKFS_I(inode)->i_fs_version;
3720         return 0;
3721 }
3722
3723 /*
3724  * Concurrency: @dt is read locked.
3725  */
3726 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3727                          struct lu_buf *buf, const char *name)
3728 {
3729         struct osd_object      *obj    = osd_dt_obj(dt);
3730         struct inode           *inode  = obj->oo_inode;
3731         struct osd_thread_info *info   = osd_oti_get(env);
3732         struct dentry          *dentry = &info->oti_obj_dentry;
3733         bool                    cache_xattr = false;
3734         int                     rc;
3735
3736         /* version get is not real XATTR but uses xattr API */
3737         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3738                 /* for version we are just using xattr API but change inode
3739                  * field instead */
3740                 if (buf->lb_len == 0)
3741                         return sizeof(dt_obj_version_t);
3742
3743                 if (buf->lb_len < sizeof(dt_obj_version_t))
3744                         return -ERANGE;
3745
3746                 osd_object_version_get(env, dt, buf->lb_buf);
3747
3748                 return sizeof(dt_obj_version_t);
3749         }
3750
3751         if (!dt_object_exists(dt))
3752                 return -ENOENT;
3753
3754         LASSERT(!dt_object_remote(dt));
3755         LASSERT(inode->i_op != NULL);
3756         LASSERT(inode->i_op->getxattr != NULL);
3757
3758         if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3759             strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3760                 cache_xattr = true;
3761
3762         if (cache_xattr) {
3763                 rc = osd_oxc_get(obj, name, buf);
3764                 if (rc != -ENOENT)
3765                         return rc;
3766         }
3767
3768         rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3769         if (cache_xattr) {
3770                 if (rc == -ENOENT || rc == -ENODATA)
3771                         osd_oxc_add(obj, name, NULL, 0);
3772                 else if (rc > 0 && buf->lb_buf != NULL)
3773                         osd_oxc_add(obj, name, buf->lb_buf, rc);
3774         }
3775
3776         return rc;
3777 }
3778
3779 static int osd_declare_xattr_set(const struct lu_env *env,
3780                                  struct dt_object *dt,
3781                                  const struct lu_buf *buf, const char *name,
3782                                  int fl, struct thandle *handle)
3783 {
3784         struct osd_thandle *oh;
3785         int credits;
3786         struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3787
3788         LASSERT(handle != NULL);
3789
3790         oh = container_of0(handle, struct osd_thandle, ot_super);
3791         LASSERT(oh->ot_handle == NULL);
3792
3793         if (strcmp(name, XATTR_NAME_LMA) == 0) {
3794                 /* For non-upgrading case, the LMA is set first and
3795                  * usually fit inode. But for upgrade case, the LMA
3796                  * may be in another separated EA block. */
3797                 if (!dt_object_exists(dt))
3798                         credits = 0;
3799                 else if (fl == LU_XATTR_REPLACE)
3800                         credits = 1;
3801                 else
3802                         goto upgrade;
3803         } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3804                 credits = 1;
3805         } else {
3806 upgrade:
3807                 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3808
3809                 if (buf != NULL) {
3810                         ssize_t buflen;
3811
3812                         if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3813                                 /* learn xattr size from osd_xattr_get if
3814                                    attribute has not been read yet */
3815                                 buflen = __osd_xattr_get(
3816                                     osd_dt_obj(dt)->oo_inode,
3817                                     &osd_oti_get(env)->oti_obj_dentry,
3818                                     name, NULL, 0);
3819                                 if (buflen < 0)
3820                                         buflen = 0;
3821                         } else {
3822                                 buflen = buf->lb_len;
3823                         }
3824
3825                         if (buflen > sb->s_blocksize) {
3826                                 credits += osd_calc_bkmap_credits(
3827                                     sb, NULL, 0, -1,
3828                                     (buflen + sb->s_blocksize - 1) >>
3829                                     sb->s_blocksize_bits);
3830                         }
3831                 }
3832                 /*
3833                  * xattr set may involve inode quota change, reserve credits for
3834                  * dquot_initialize()
3835                  */
3836                 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3837         }
3838
3839         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3840
3841         return 0;
3842 }
3843
3844 /*
3845  * Set the 64-bit version for object
3846  */
3847 static void osd_object_version_set(const struct lu_env *env,
3848                                    struct dt_object *dt,
3849                                    dt_obj_version_t *new_version)
3850 {
3851         struct inode *inode = osd_dt_obj(dt)->oo_inode;
3852
3853         CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3854                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3855
3856         LDISKFS_I(inode)->i_fs_version = *new_version;
3857         /** Version is set after all inode operations are finished,
3858          *  so we should mark it dirty here */
3859         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3860 }
3861
3862 /*
3863  * Concurrency: @dt is write locked.
3864  */
3865 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3866                          const struct lu_buf *buf, const char *name, int fl,
3867                          struct thandle *handle)
3868 {
3869         struct osd_object      *obj      = osd_dt_obj(dt);
3870         struct inode           *inode    = obj->oo_inode;
3871         struct osd_thread_info *info     = osd_oti_get(env);
3872         int                     fs_flags = 0;
3873         int                     rc;
3874         ENTRY;
3875
3876         LASSERT(handle != NULL);
3877
3878         /* version set is not real XATTR */
3879         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3880                 /* for version we are just using xattr API but change inode
3881                  * field instead */
3882                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3883                 osd_object_version_set(env, dt, buf->lb_buf);
3884                 return sizeof(dt_obj_version_t);
3885         }
3886
3887         CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3888                PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3889
3890         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3891         if (fl & LU_XATTR_REPLACE)
3892                 fs_flags |= XATTR_REPLACE;
3893
3894         if (fl & LU_XATTR_CREATE)
3895                 fs_flags |= XATTR_CREATE;
3896
3897         if (strcmp(name, XATTR_NAME_LMV) == 0) {
3898                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3899
3900                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3901                 if (rc != 0)
3902                         RETURN(rc);
3903
3904                 lma->lma_incompat |= LMAI_STRIPED;
3905                 lustre_lma_swab(lma);
3906                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3907                                      sizeof(*lma), XATTR_REPLACE);
3908                 if (rc != 0)
3909                         RETURN(rc);
3910         }
3911
3912         rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3913                                fs_flags);
3914         osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3915
3916         if (rc == 0 &&
3917             (strcmp(name, XATTR_NAME_LOV) == 0 ||
3918              strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3919                 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3920
3921         return rc;
3922 }
3923
3924 /*
3925  * Concurrency: @dt is read locked.
3926  */
3927 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3928                           const struct lu_buf *buf)
3929 {
3930         struct osd_object      *obj    = osd_dt_obj(dt);
3931         struct inode           *inode  = obj->oo_inode;
3932         struct osd_thread_info *info   = osd_oti_get(env);
3933         struct dentry          *dentry = &info->oti_obj_dentry;
3934
3935         if (!dt_object_exists(dt))
3936                 return -ENOENT;
3937
3938         LASSERT(!dt_object_remote(dt));
3939         LASSERT(inode->i_op != NULL);
3940         LASSERT(inode->i_op->listxattr != NULL);
3941
3942         dentry->d_inode = inode;
3943         dentry->d_sb = inode->i_sb;
3944         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3945 }
3946
3947 static int osd_declare_xattr_del(const struct lu_env *env,
3948                                  struct dt_object *dt, const char *name,
3949                                  struct thandle *handle)
3950 {
3951         struct osd_thandle *oh;
3952         struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3953
3954         LASSERT(!dt_object_remote(dt));
3955         LASSERT(handle != NULL);
3956
3957         oh = container_of0(handle, struct osd_thandle, ot_super);
3958         LASSERT(oh->ot_handle == NULL);
3959
3960         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3961                              osd_dto_credits_noquota[DTO_XATTR_SET]);
3962         /*
3963          * xattr del may involve inode quota change, reserve credits for
3964          * dquot_initialize()
3965          */
3966         oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3967
3968         return 0;
3969 }
3970
3971 /*
3972  * Concurrency: @dt is write locked.
3973  */
3974 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3975                          const char *name, struct thandle *handle)
3976 {
3977         struct osd_object      *obj    = osd_dt_obj(dt);
3978         struct inode           *inode  = obj->oo_inode;
3979         struct osd_thread_info *info   = osd_oti_get(env);
3980         struct dentry          *dentry = &info->oti_obj_dentry;
3981         int                     rc;
3982
3983         if (!dt_object_exists(dt))
3984                 return -ENOENT;
3985
3986         LASSERT(!dt_object_remote(dt));
3987         LASSERT(inode->i_op != NULL);
3988         LASSERT(inode->i_op->removexattr != NULL);
3989         LASSERT(handle != NULL);
3990
3991         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3992
3993         ll_vfs_dq_init(inode);
3994         dentry->d_inode = inode;
3995         dentry->d_sb = inode->i_sb;
3996         rc = inode->i_op->removexattr(dentry, name);
3997         osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3998
3999         if (rc == 0 &&
4000             (strcmp(name, XATTR_NAME_LOV) == 0 ||
4001              strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4002                 osd_oxc_del(obj, name);
4003
4004         return rc;
4005 }
4006
4007 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4008                            __u64 start, __u64 end)
4009 {
4010         struct osd_object       *obj    = osd_dt_obj(dt);
4011         struct inode            *inode  = obj->oo_inode;
4012         struct osd_thread_info  *info   = osd_oti_get(env);
4013         struct dentry           *dentry = &info->oti_obj_dentry;
4014         struct file             *file   = &info->oti_file;
4015         int                     rc;
4016
4017         ENTRY;
4018
4019         dentry->d_inode = inode;
4020         dentry->d_sb = inode->i_sb;
4021         file->f_path.dentry = dentry;
4022         file->f_mapping = inode->i_mapping;
4023         file->f_op = inode->i_fop;
4024         set_file_inode(file, inode);
4025
4026         rc = ll_vfs_fsync_range(file, start, end, 0);
4027
4028         RETURN(rc);
4029 }
4030
4031 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4032 {
4033         return 0;
4034 }
4035
4036 /*
4037  * Index operations.
4038  */
4039
4040 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4041                            const struct dt_index_features *feat)
4042 {
4043         struct iam_descr *descr;
4044
4045         if (osd_object_is_root(o))
4046                 return feat == &dt_directory_features;
4047
4048         LASSERT(o->oo_dir != NULL);
4049
4050         descr = o->oo_dir->od_container.ic_descr;
4051         if (feat == &dt_directory_features) {
4052                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4053                         return 1;
4054                 else
4055                         return 0;
4056         } else {
4057                 return
4058                         feat->dif_keysize_min <= descr->id_key_size &&
4059                         descr->id_key_size <= feat->dif_keysize_max &&
4060                         feat->dif_recsize_min <= descr->id_rec_size &&
4061                         descr->id_rec_size <= feat->dif_recsize_max &&
4062                         !(feat->dif_flags & (DT_IND_VARKEY |
4063                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
4064                         ergo(feat->dif_flags & DT_IND_UPDATE,
4065                              1 /* XXX check that object (and file system) is
4066                                 * writable */);
4067         }
4068 }
4069
4070 static int osd_iam_container_init(const struct lu_env *env,
4071                                   struct osd_object *obj,
4072                                   struct osd_directory *dir)
4073 {
4074         struct iam_container *bag = &dir->od_container;
4075         int result;
4076
4077         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4078         if (result != 0)
4079                 return result;
4080
4081         result = iam_container_setup(bag);
4082         if (result == 0)
4083                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4084         else
4085                 iam_container_fini(bag);
4086
4087         return result;
4088 }
4089
4090
4091 /*
4092  * Concurrency: no external locking is necessary.
4093  */
4094 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4095                          const struct dt_index_features *feat)
4096 {
4097         int                      result;
4098         int                      skip_iam = 0;
4099         struct osd_object       *obj = osd_dt_obj(dt);
4100
4101         LINVRNT(osd_invariant(obj));
4102
4103         if (osd_object_is_root(obj)) {
4104                 dt->do_index_ops = &osd_index_ea_ops;
4105                 result = 0;
4106         } else if (feat == &dt_directory_features) {
4107                 dt->do_index_ops = &osd_index_ea_ops;
4108                 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4109                         result = 0;
4110                 else
4111                         result = -ENOTDIR;
4112                 skip_iam = 1;
4113         } else if (unlikely(feat == &dt_otable_features)) {
4114                 dt->do_index_ops = &osd_otable_ops;
4115                 return 0;
4116         } else if (unlikely(feat == &dt_acct_features)) {
4117                 dt->do_index_ops = &osd_acct_index_ops;
4118                 result = 0;
4119                 skip_iam = 1;
4120         } else if (!osd_has_index(obj)) {
4121                 struct osd_directory *dir;
4122
4123                 OBD_ALLOC_PTR(dir);
4124                 if (dir != NULL) {
4125
4126                         spin_lock(&obj->oo_guard);
4127                         if (obj->oo_dir == NULL)
4128                                 obj->oo_dir = dir;
4129                         else
4130                                 /*
4131                                  * Concurrent thread allocated container data.
4132                                  */
4133                                 OBD_FREE_PTR(dir);
4134                         spin_unlock(&obj->oo_guard);
4135                         /*
4136                          * Now, that we have container data, serialize its
4137                          * initialization.
4138                          */
4139                         down_write(&obj->oo_ext_idx_sem);
4140                         /*
4141                          * recheck under lock.
4142                          */
4143                         if (!osd_has_index(obj))
4144                                 result = osd_iam_container_init(env, obj,
4145                                                                 obj->oo_dir);
4146                         else
4147                                 result = 0;
4148                         up_write(&obj->oo_ext_idx_sem);
4149                 } else {
4150                         result = -ENOMEM;
4151                 }
4152         } else {
4153                 result = 0;
4154         }
4155
4156         if (result == 0 && skip_iam == 0) {
4157                 if (!osd_iam_index_probe(env, obj, feat))
4158                         result = -ENOTDIR;
4159         }
4160         LINVRNT(osd_invariant(obj));
4161
4162         if (result == 0 && feat == &dt_quota_glb_features &&
4163             fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4164                 result = osd_quota_migration(env, dt);
4165
4166         return result;
4167 }
4168
4169 static int osd_otable_it_attr_get(const struct lu_env *env,
4170                                  struct dt_object *dt,
4171                                  struct lu_attr *attr)
4172 {
4173         attr->la_valid = 0;
4174         return 0;
4175 }
4176
4177 static const struct dt_object_operations osd_obj_ops = {
4178         .do_read_lock         = osd_object_read_lock,
4179         .do_write_lock        = osd_object_write_lock,
4180         .do_read_unlock       = osd_object_read_unlock,
4181         .do_write_unlock      = osd_object_write_unlock,
4182         .do_write_locked      = osd_object_write_locked,
4183         .do_attr_get          = osd_attr_get,
4184         .do_declare_attr_set  = osd_declare_attr_set,
4185         .do_attr_set          = osd_attr_set,
4186         .do_ah_init           = osd_ah_init,
4187         .do_declare_create    = osd_declare_object_create,
4188         .do_create            = osd_object_create,
4189         .do_declare_destroy   = osd_declare_object_destroy,
4190         .do_destroy           = osd_object_destroy,
4191         .do_index_try         = osd_index_try,
4192         .do_declare_ref_add   = osd_declare_object_ref_add,
4193         .do_ref_add           = osd_object_ref_add,
4194         .do_declare_ref_del   = osd_declare_object_ref_del,
4195         .do_ref_del           = osd_object_ref_del,
4196         .do_xattr_get         = osd_xattr_get,
4197         .do_declare_xattr_set = osd_declare_xattr_set,
4198         .do_xattr_set         = osd_xattr_set,
4199         .do_declare_xattr_del = osd_declare_xattr_del,
4200         .do_xattr_del         = osd_xattr_del,
4201         .do_xattr_list        = osd_xattr_list,
4202         .do_object_sync       = osd_object_sync,
4203         .do_invalidate        = osd_invalidate,
4204 };
4205
4206 /**
4207  * dt_object_operations for interoperability mode
4208  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4209  */
4210 static const struct dt_object_operations osd_obj_ea_ops = {
4211         .do_read_lock         = osd_object_read_lock,
4212         .do_write_lock        = osd_object_write_lock,
4213         .do_read_unlock       = osd_object_read_unlock,
4214         .do_write_unlock      = osd_object_write_unlock,
4215         .do_write_locked      = osd_object_write_locked,
4216         .do_attr_get          = osd_attr_get,
4217         .do_declare_attr_set  = osd_declare_attr_set,
4218         .do_attr_set          = osd_attr_set,
4219         .do_ah_init           = osd_ah_init,
4220         .do_declare_create    = osd_declare_object_create,
4221         .do_create            = osd_object_ea_create,
4222         .do_declare_destroy   = osd_declare_object_destroy,
4223         .do_destroy           = osd_object_destroy,
4224         .do_index_try         = osd_index_try,
4225         .do_declare_ref_add   = osd_declare_object_ref_add,
4226         .do_ref_add           = osd_object_ref_add,
4227         .do_declare_ref_del   = osd_declare_object_ref_del,
4228         .do_ref_del           = osd_object_ref_del,
4229         .do_xattr_get         = osd_xattr_get,
4230         .do_declare_xattr_set = osd_declare_xattr_set,
4231         .do_xattr_set         = osd_xattr_set,
4232         .do_declare_xattr_del = osd_declare_xattr_del,
4233         .do_xattr_del         = osd_xattr_del,
4234         .do_xattr_list        = osd_xattr_list,
4235         .do_object_sync       = osd_object_sync,
4236         .do_invalidate        = osd_invalidate,
4237 };
4238
4239 static const struct dt_object_operations osd_obj_otable_it_ops = {
4240         .do_attr_get    = osd_otable_it_attr_get,
4241         .do_index_try   = osd_index_try,
4242 };
4243
4244 static int osd_index_declare_iam_delete(const struct lu_env *env,
4245                                         struct dt_object *dt,
4246                                         const struct dt_key *key,
4247                                         struct thandle *handle)
4248 {
4249         struct osd_thandle    *oh;
4250
4251         oh = container_of0(handle, struct osd_thandle, ot_super);
4252         LASSERT(oh->ot_handle == NULL);
4253
4254         /* Recycle  may cause additional three blocks to be changed. */
4255         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4256                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4257
4258         return 0;
4259 }
4260
4261 /**
4262  *      delete a (key, value) pair from index \a dt specified by \a key
4263  *
4264  *      \param  dt      osd index object
4265  *      \param  key     key for index
4266  *      \param  rec     record reference
4267  *      \param  handle  transaction handler
4268  *
4269  *      \retval  0  success
4270  *      \retval -ve   failure
4271  */
4272 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4273                                 const struct dt_key *key,
4274                                 struct thandle *handle)
4275 {
4276         struct osd_thread_info *oti = osd_oti_get(env);
4277         struct osd_object      *obj = osd_dt_obj(dt);
4278         struct osd_thandle     *oh;
4279         struct iam_path_descr  *ipd;
4280         struct iam_container   *bag = &obj->oo_dir->od_container;
4281         int                     rc;
4282         ENTRY;
4283
4284         if (!dt_object_exists(dt))
4285                 RETURN(-ENOENT);
4286
4287         LINVRNT(osd_invariant(obj));
4288         LASSERT(!dt_object_remote(dt));
4289         LASSERT(bag->ic_object == obj->oo_inode);
4290         LASSERT(handle != NULL);
4291
4292         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4293
4294         ipd = osd_idx_ipd_get(env, bag);
4295         if (unlikely(ipd == NULL))
4296                 RETURN(-ENOMEM);
4297
4298         oh = container_of0(handle, struct osd_thandle, ot_super);
4299         LASSERT(oh->ot_handle != NULL);
4300         LASSERT(oh->ot_handle->h_transaction != NULL);
4301
4302         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4303                 /* swab quota uid/gid provided by caller */
4304                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4305                 key = (const struct dt_key *)&oti->oti_quota_id;
4306         }
4307
4308         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4309         osd_ipd_put(env, bag, ipd);
4310         LINVRNT(osd_invariant(obj));
4311         osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4312         RETURN(rc);
4313 }
4314
4315 static int osd_index_declare_ea_delete(const struct lu_env *env,
4316                                        struct dt_object *dt,
4317                                        const struct dt_key *key,
4318                                        struct thandle *handle)
4319 {
4320         struct osd_thandle *oh;
4321         struct inode       *inode;
4322         int                 rc, credits;
4323         ENTRY;
4324
4325         LASSERT(!dt_object_remote(dt));
4326         LASSERT(handle != NULL);
4327
4328         oh = container_of0(handle, struct osd_thandle, ot_super);
4329         LASSERT(oh->ot_handle == NULL);
4330
4331         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4332         if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4333                 /* '..' to a remote object has a local representative */
4334                 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4335                 /* to reset LMAI_REMOTE_PARENT */
4336                 credits += 1;
4337         }
4338         osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4339
4340         inode = osd_dt_obj(dt)->oo_inode;
4341         if (inode == NULL)
4342                 RETURN(-ENOENT);
4343
4344         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4345                                    0, oh, osd_dt_obj(dt), true, NULL, false);
4346         RETURN(rc);
4347 }
4348
4349 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4350                                           struct dt_rec *fid)
4351 {
4352         struct osd_fid_pack *rec;
4353         int                  rc = -ENODATA;
4354
4355         if (de->file_type & LDISKFS_DIRENT_LUFID) {
4356                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4357                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4358                 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4359                         rc = -EINVAL;
4360         }
4361         return rc;
4362 }
4363
4364 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4365                           const struct lu_fid *fid)
4366 {
4367         struct seq_server_site  *ss = osd_seq_site(osd);
4368         ENTRY;
4369
4370         /* FID seqs not in FLDB, must be local seq */
4371         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4372                 RETURN(0);
4373
4374         /* If FLD is not being initialized yet, it only happens during the
4375          * initialization, likely during mgs initialization, and we assume
4376          * this is local FID. */
4377         if (ss == NULL || ss->ss_server_fld == NULL)
4378                 RETURN(0);
4379
4380         /* Only check the local FLDB here */
4381         if (osd_seq_exists(env, osd, fid_seq(fid)))
4382                 RETURN(0);
4383
4384         RETURN(1);
4385 }
4386
4387 /**
4388  * Index delete function for interoperability mode (b11826).
4389  * It will remove the directory entry added by osd_index_ea_insert().
4390  * This entry is needed to maintain name->fid mapping.
4391  *
4392  * \param key,  key i.e. file entry to be deleted
4393  *
4394  * \retval   0, on success
4395  * \retval -ve, on error
4396  */
4397 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4398                                const struct dt_key *key, struct thandle *handle)
4399 {
4400         struct osd_object          *obj = osd_dt_obj(dt);
4401         struct inode               *dir = obj->oo_inode;
4402         struct dentry              *dentry;
4403         struct osd_thandle         *oh;
4404         struct ldiskfs_dir_entry_2 *de = NULL;
4405         struct buffer_head         *bh;
4406         struct htree_lock          *hlock = NULL;
4407         struct lu_fid              *fid = &osd_oti_get(env)->oti_fid;
4408         struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
4409         int                        rc;
4410         ENTRY;
4411
4412         if (!dt_object_exists(dt))
4413                 RETURN(-ENOENT);
4414
4415         LINVRNT(osd_invariant(obj));
4416         LASSERT(!dt_object_remote(dt));
4417         LASSERT(handle != NULL);
4418
4419         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4420
4421         oh = container_of(handle, struct osd_thandle, ot_super);
4422         LASSERT(oh->ot_handle != NULL);
4423         LASSERT(oh->ot_handle->h_transaction != NULL);
4424
4425         ll_vfs_dq_init(dir);
4426         dentry = osd_child_dentry_get(env, obj,
4427                                       (char *)key, strlen((char *)key));
4428
4429         if (obj->oo_hl_head != NULL) {
4430                 hlock = osd_oti_get(env)->oti_hlock;
4431                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4432                                    dir, LDISKFS_HLOCK_DEL);
4433         } else {
4434                 down_write(&obj->oo_ext_idx_sem);
4435         }
4436
4437         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4438         if (!IS_ERR(bh)) {
4439                 /* If this is not the ".." entry, it might be a remote DNE
4440                  * entry and  we need to check if the FID is for a remote
4441                  * MDT.  If the FID is  not in the directory entry (e.g.
4442                  * upgraded 1.8 filesystem without dirdata enabled) then
4443                  * we need to get the FID from the LMA. For a remote directory
4444                  * there HAS to be an LMA, it cannot be an IGIF inode in this
4445                  * case.
4446                  *
4447                  * Delete the entry before the agent inode in order to
4448                  * simplify error handling.  At worst an error after deleting
4449                  * the entry first might leak the agent inode afterward. The
4450                  * reverse would need filesystem abort in case of error deleting
4451                  * the entry after the agent had been removed, or leave a
4452                  * dangling entry pointing at a random inode. */
4453                 if (strcmp((char *)key, dotdot) != 0) {
4454                         LASSERT(de != NULL);
4455                         rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4456                         if (rc == -ENODATA) {
4457                                 /* can't get FID, postpone to the end of the
4458                                  * transaction when iget() is safe */
4459                                 osd_schedule_agent_inode_removal(env, oh,
4460                                                 le32_to_cpu(de->inode));
4461                         } else if (rc == 0 &&
4462                                    unlikely(osd_remote_fid(env, osd, fid))) {
4463                                 osd_schedule_agent_inode_removal(env, oh,
4464                                                 le32_to_cpu(de->inode));
4465                         }
4466                 }
4467                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4468                 brelse(bh);
4469         } else {
4470                 rc = PTR_ERR(bh);
4471         }
4472         if (hlock != NULL)
4473                 ldiskfs_htree_unlock(hlock);
4474         else
4475                 up_write(&obj->oo_ext_idx_sem);
4476
4477         if (rc != 0)
4478                 GOTO(out, rc);
4479
4480         /* For inode on the remote MDT, .. will point to
4481          * /Agent directory, Check whether it needs to delete
4482          * from agent directory */
4483         if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4484                 int ret;
4485
4486                 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4487                                                     obj, oh);
4488                 if (ret != 0)
4489                         /* Sigh, the entry has been deleted, and
4490                          * it is not easy to revert it back, so
4491                          * let's keep this error private, and let
4492                          * LFSCK fix it. XXX */
4493                         CERROR("%s: delete remote parent "DFID": rc = %d\n",
4494                                osd_name(osd), PFID(fid), ret);
4495         }
4496 out:
4497         LASSERT(osd_invariant(obj));
4498         osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4499         RETURN(rc);
4500 }
4501
4502 /**
4503  *      Lookup index for \a key and copy record to \a rec.
4504  *
4505  *      \param  dt      osd index object
4506  *      \param  key     key for index
4507  *      \param  rec     record reference
4508  *
4509  *      \retval  +ve  success : exact mach
4510  *      \retval  0    return record with key not greater than \a key
4511  *      \retval -ve   failure
4512  */
4513 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4514                                 struct dt_rec *rec, const struct dt_key *key)
4515 {
4516         struct osd_object      *obj = osd_dt_obj(dt);
4517         struct iam_path_descr  *ipd;
4518         struct iam_container   *bag = &obj->oo_dir->od_container;
4519         struct osd_thread_info *oti = osd_oti_get(env);
4520         struct iam_iterator    *it = &oti->oti_idx_it;
4521         struct iam_rec         *iam_rec;
4522         int                     rc;
4523         ENTRY;
4524
4525         if (!dt_object_exists(dt))
4526                 RETURN(-ENOENT);
4527
4528         LASSERT(osd_invariant(obj));
4529         LASSERT(!dt_object_remote(dt));
4530         LASSERT(bag->ic_object == obj->oo_inode);
4531
4532         ipd = osd_idx_ipd_get(env, bag);
4533         if (IS_ERR(ipd))
4534                 RETURN(-ENOMEM);
4535
4536         /* got ipd now we can start iterator. */
4537         iam_it_init(it, bag, 0, ipd);
4538
4539         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4540                 /* swab quota uid/gid provided by caller */
4541                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4542                 key = (const struct dt_key *)&oti->oti_quota_id;
4543         }
4544
4545         rc = iam_it_get(it, (struct iam_key *)key);
4546         if (rc >= 0) {
4547                 if (S_ISDIR(obj->oo_inode->i_mode))
4548                         iam_rec = (struct iam_rec *)oti->oti_ldp;
4549                 else
4550                         iam_rec = (struct iam_rec *) rec;
4551
4552                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4553
4554                 if (S_ISDIR(obj->oo_inode->i_mode))
4555                         osd_fid_unpack((struct lu_fid *) rec,
4556                                        (struct osd_fid_pack *)iam_rec);
4557                 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4558                         osd_quota_unpack(obj, rec);
4559         }
4560
4561         iam_it_put(it);
4562         iam_it_fini(it);
4563         osd_ipd_put(env, bag, ipd);
4564
4565         LINVRNT(osd_invariant(obj));
4566
4567         RETURN(rc);
4568 }
4569
4570 static int osd_index_declare_iam_insert(const struct lu_env *env,
4571                                         struct dt_object *dt,
4572                                         const struct dt_rec *rec,
4573                                         const struct dt_key *key,
4574                                         struct thandle *handle)
4575 {
4576         struct osd_thandle *oh;
4577
4578         LASSERT(handle != NULL);
4579
4580         oh = container_of0(handle, struct osd_thandle, ot_super);
4581         LASSERT(oh->ot_handle == NULL);
4582
4583         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4584                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4585
4586         return 0;
4587 }
4588
4589 /**
4590  *      Inserts (key, value) pair in \a dt index object.
4591  *
4592  *      \param  dt      osd index object
4593  *      \param  key     key for index
4594  *      \param  rec     record reference
4595  *      \param  th      transaction handler
4596  *
4597  *      \retval  0  success
4598  *      \retval -ve failure
4599  */
4600 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4601                                 const struct dt_rec *rec,
4602                                 const struct dt_key *key, struct thandle *th,
4603                                 int ignore_quota)
4604 {
4605         struct osd_object     *obj = osd_dt_obj(dt);
4606         struct iam_path_descr *ipd;
4607         struct osd_thandle    *oh;
4608         struct iam_container  *bag;
4609         struct osd_thread_info *oti = osd_oti_get(env);
4610         struct iam_rec         *iam_rec;
4611         int                     rc;
4612         ENTRY;
4613
4614         if (!dt_object_exists(dt))
4615                 RETURN(-ENOENT);
4616
4617         LINVRNT(osd_invariant(obj));
4618         LASSERT(!dt_object_remote(dt));
4619
4620         bag = &obj->oo_dir->od_container;
4621         LASSERT(bag->ic_object == obj->oo_inode);
4622         LASSERT(th != NULL);
4623
4624         osd_trans_exec_op(env, th, OSD_OT_INSERT);
4625
4626         ipd = osd_idx_ipd_get(env, bag);
4627         if (unlikely(ipd == NULL))
4628                 RETURN(-ENOMEM);
4629
4630         oh = container_of0(th, struct osd_thandle, ot_super);
4631         LASSERT(oh->ot_handle != NULL);
4632         LASSERT(oh->ot_handle->h_transaction != NULL);
4633         if (S_ISDIR(obj->oo_inode->i_mode)) {
4634                 iam_rec = (struct iam_rec *)oti->oti_ldp;
4635                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4636         } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4637                 /* pack quota uid/gid */
4638                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4639                 key = (const struct dt_key *)&oti->oti_quota_id;
4640                 /* pack quota record */
4641                 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4642                 iam_rec = (struct iam_rec *)rec;
4643         } else {
4644                 iam_rec = (struct iam_rec *)rec;
4645         }
4646
4647         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4648                         iam_rec, ipd);
4649         osd_ipd_put(env, bag, ipd);
4650         LINVRNT(osd_invariant(obj));
4651         osd_trans_exec_check(env, th, OSD_OT_INSERT);
4652         RETURN(rc);
4653 }
4654
4655 /**
4656  * Calls ldiskfs_add_entry() to add directory entry
4657  * into the directory. This is required for
4658  * interoperability mode (b11826)
4659  *
4660  * \retval   0, on success
4661  * \retval -ve, on error
4662  */
4663 static int __osd_ea_add_rec(struct osd_thread_info *info,
4664                             struct osd_object *pobj, struct inode  *cinode,
4665                             const char *name, const struct lu_fid *fid,
4666                             struct htree_lock *hlock, struct thandle *th)
4667 {
4668         struct ldiskfs_dentry_param *ldp;
4669         struct dentry               *child;
4670         struct osd_thandle          *oth;
4671         int                          rc;
4672
4673         oth = container_of(th, struct osd_thandle, ot_super);
4674         LASSERT(oth->ot_handle != NULL);
4675         LASSERT(oth->ot_handle->h_transaction != NULL);
4676         LASSERT(pobj->oo_inode);
4677
4678         ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4679         if (unlikely(pobj->oo_inode ==
4680                      osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4681                 ldp->edp_magic = 0;
4682         else
4683                 osd_get_ldiskfs_dirent_param(ldp, fid);
4684         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4685         child->d_fsdata = (void *)ldp;
4686         ll_vfs_dq_init(pobj->oo_inode);
4687         rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4688                                    child, cinode, hlock);
4689         if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4690                 struct ldiskfs_dir_entry_2      *de;
4691                 struct buffer_head              *bh;
4692                 int                              rc1;
4693
4694                 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4695                                             NULL, hlock);
4696                 if (!IS_ERR(bh)) {
4697                         rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4698                                                                bh);
4699                         if (rc1 == 0) {
4700                                 if (S_ISDIR(cinode->i_mode))
4701                                         de->file_type = LDISKFS_DIRENT_LUFID |
4702                                                         LDISKFS_FT_REG_FILE;
4703                                 else
4704                                         de->file_type = LDISKFS_DIRENT_LUFID |
4705                                                         LDISKFS_FT_DIR;
4706                                 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4707                                                               NULL, bh);
4708                         }
4709                         brelse(bh);
4710                 }
4711         }
4712
4713         RETURN(rc);
4714 }
4715
4716 /**
4717  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4718  * into the directory.Also sets flags into osd object to
4719  * indicate dot and dotdot are created. This is required for
4720  * interoperability mode (b11826)
4721  *
4722  * \param dir   directory for dot and dotdot fixup.
4723  * \param obj   child object for linking
4724  *
4725  * \retval   0, on success
4726  * \retval -ve, on error
4727  */
4728 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4729                               struct osd_object *dir,
4730                               struct inode *parent_dir, const char *name,
4731                               const struct lu_fid *dot_fid,
4732                               const struct lu_fid *dot_dot_fid,
4733                               struct thandle *th)
4734 {
4735         struct inode                *inode = dir->oo_inode;
4736         struct osd_thandle          *oth;
4737         int result = 0;
4738
4739         oth = container_of(th, struct osd_thandle, ot_super);
4740         LASSERT(oth->ot_handle->h_transaction != NULL);
4741         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4742
4743         if (strcmp(name, dot) == 0) {
4744                 if (dir->oo_compat_dot_created) {
4745                         result = -EEXIST;
4746                 } else {
4747                         LASSERT(inode->i_ino == parent_dir->i_ino);
4748                         dir->oo_compat_dot_created = 1;
4749                         result = 0;
4750                 }
4751         } else if (strcmp(name, dotdot) == 0) {
4752                 if (!dir->oo_compat_dot_created)
4753                         return -EINVAL;
4754                 /* in case of rename, dotdot is already created */
4755                 if (dir->oo_compat_dotdot_created) {
4756                         return __osd_ea_add_rec(info, dir, parent_dir, name,
4757                                                 dot_dot_fid, NULL, th);
4758                 }
4759
4760                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4761                         struct lu_fid tfid = *dot_dot_fid;
4762
4763                         tfid.f_oid--;
4764                         result = osd_add_dot_dotdot_internal(info,
4765                                         dir->oo_inode, parent_dir, dot_fid,
4766                                         &tfid, oth);
4767                 } else {
4768                         result = osd_add_dot_dotdot_internal(info,
4769                                         dir->oo_inode, parent_dir, dot_fid,
4770                                         dot_dot_fid, oth);
4771                 }
4772
4773                 if (result == 0)
4774                         dir->oo_compat_dotdot_created = 1;
4775         }
4776
4777         return result;
4778 }
4779
4780
4781 /**
4782  * It will call the appropriate osd_add* function and return the
4783  * value, return by respective functions.
4784  */
4785 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4786                           struct inode *cinode, const char *name,
4787                           const struct lu_fid *fid, struct thandle *th)
4788 {
4789         struct osd_thread_info *info   = osd_oti_get(env);
4790         struct htree_lock      *hlock;
4791         int                     rc;
4792
4793         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4794
4795         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4796                                                    name[2] =='\0'))) {
4797                 if (hlock != NULL) {
4798                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4799                                            pobj->oo_inode, 0);
4800                 } else {
4801                         down_write(&pobj->oo_ext_idx_sem);
4802                 }
4803
4804                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4805                                         lu_object_fid(&pobj->oo_dt.do_lu),
4806                                         fid, th);
4807         } else {
4808                 if (hlock != NULL) {
4809                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4810                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
4811                 } else {
4812                         down_write(&pobj->oo_ext_idx_sem);
4813                 }
4814
4815                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4816                         struct lu_fid *tfid = &info->oti_fid;
4817
4818                         *tfid = *fid;
4819                         tfid->f_ver = ~0;
4820                         rc = __osd_ea_add_rec(info, pobj, cinode, name,
4821                                               tfid, hlock, th);
4822                 } else {
4823                         rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4824                                               hlock, th);
4825                 }
4826         }
4827         if (hlock != NULL)
4828                 ldiskfs_htree_unlock(hlock);
4829         else
4830                 up_write(&pobj->oo_ext_idx_sem);
4831
4832         return rc;
4833 }
4834
4835 static int
4836 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4837                       struct osd_idmap_cache *oic)
4838 {
4839         struct osd_scrub *scrub = &dev->od_scrub;
4840         struct lu_fid *fid = &oic->oic_fid;
4841         struct osd_inode_id *id = &oic->oic_lid;
4842         struct inode *inode = NULL;
4843         int once  = 0;
4844         bool insert;
4845         int rc;
4846         ENTRY;
4847
4848         if (!fid_is_norm(fid) && !fid_is_igif(fid))
4849                 RETURN(0);
4850
4851         if (scrub->os_pos_current > id->oii_ino)
4852                 RETURN(0);
4853
4854 again:
4855         rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4856         if (rc == -ENOENT) {
4857                 __u32 gen = id->oii_gen;
4858
4859                 insert = true;
4860                 if (inode != NULL)
4861                         goto trigger;
4862
4863                 inode = osd_iget(oti, dev, id);
4864                 /* The inode has been removed (by race maybe). */
4865                 if (IS_ERR(inode)) {
4866                         rc = PTR_ERR(inode);
4867
4868                         RETURN(rc == -ESTALE ? -ENOENT : rc);
4869                 }
4870
4871                 /* The OI mapping is lost. */
4872                 if (gen != OSD_OII_NOGEN)
4873                         goto trigger;
4874
4875                 iput(inode);
4876                 /* The inode may has been reused by others, we do not know,
4877                  * leave it to be handled by subsequent osd_fid_lookup(). */
4878                 RETURN(0);
4879         } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4880                 RETURN(rc);
4881         } else {
4882                 insert = false;
4883         }
4884
4885 trigger:
4886         if (thread_is_running(&scrub->os_thread)) {
4887                 if (inode == NULL) {
4888                         inode = osd_iget(oti, dev, id);
4889                         /* The inode has been removed (by race maybe). */
4890                         if (IS_ERR(inode)) {
4891                                 rc = PTR_ERR(inode);
4892
4893                                 RETURN(rc == -ESTALE ? -ENOENT : rc);
4894                         }
4895                 }
4896
4897                 rc = osd_oii_insert(dev, oic, insert);
4898                 /* There is race condition between osd_oi_lookup and OI scrub.
4899                  * The OI scrub finished just after osd_oi_lookup() failure.
4900                  * Under such case, it is unnecessary to trigger OI scrub again,
4901                  * but try to call osd_oi_lookup() again. */
4902                 if (unlikely(rc == -EAGAIN))
4903                         goto again;
4904
4905                 if (!S_ISDIR(inode->i_mode))
4906                         rc = 0;
4907                 else
4908                         rc = osd_check_lmv(oti, dev, inode, oic);
4909
4910                 iput(inode);
4911                 RETURN(rc);
4912         }
4913
4914         if (!dev->od_noscrub && ++once == 1) {
4915                 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4916                                      SS_CLEAR_FAILOUT);
4917                 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4918                        "%.16s: trigger partial OI scrub for RPC inconsistency "
4919                        "checking FID "DFID": rc = %d\n",
4920                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4921                        PFID(fid), rc);
4922                 if (rc == 0 || rc == -EALREADY)
4923                         goto again;
4924         }
4925
4926         if (inode != NULL)
4927                 iput(inode);
4928
4929         RETURN(rc);
4930 }
4931
4932 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4933                                struct osd_device *dev,
4934                                struct osd_idmap_cache *oic,
4935                                struct lu_fid *fid, __u32 ino)
4936 {
4937         struct lustre_mdt_attrs *lma   = &oti->oti_mdt_attrs;
4938         struct inode            *inode;
4939         int                      rc;
4940
4941         osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4942         inode = osd_iget(oti, dev, &oic->oic_lid);
4943         if (IS_ERR(inode)) {
4944                 fid_zero(&oic->oic_fid);
4945                 return PTR_ERR(inode);
4946         }
4947
4948         rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4949         iput(inode);
4950         if (rc != 0)
4951                 fid_zero(&oic->oic_fid);
4952         else
4953                 *fid = oic->oic_fid = lma->lma_self_fid;
4954         return rc;
4955 }
4956
4957 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4958                       struct osd_inode_id *id, const struct lu_fid *fid)
4959 {
4960         CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4961                id->oii_ino, id->oii_gen, info);
4962         info->oti_cache.oic_lid = *id;
4963         info->oti_cache.oic_fid = *fid;
4964         info->oti_cache.oic_dev = osd;
4965 }
4966
4967 /**
4968  * Get parent FID from the linkEA.
4969  *
4970  * For a directory which parent resides on remote MDT, to satisfy the
4971  * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4972  * the other hand, to make the lookup(..) on the directory can return
4973  * the real parent FID, we append the real parent FID after its ".."
4974  * name entry in the /REMOTE_PARENT_DIR.
4975  *
4976  * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4977  * backup. So after the restore, we cannot get the right parent FID from
4978  * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4979  * we have stored the real parent FID in the directory object's linkEA,
4980  * we can parse the linkEA for the real parent FID.
4981  *
4982  * \param[in] env       pointer to the thread context
4983  * \param[in] obj       pointer to the object to be handled
4984  * \param[out]fid       pointer to the buffer to hold the parent FID
4985  *
4986  * \retval              0 for getting the real parent FID successfully
4987  * \retval              negative error number on failure
4988  */
4989 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4990                                     struct osd_object *obj,
4991                                     struct lu_fid *fid)
4992 {
4993         struct osd_thread_info  *oti    = osd_oti_get(env);
4994         struct lu_buf           *buf    = &oti->oti_big_buf;
4995         struct dentry           *dentry = &oti->oti_obj_dentry;
4996         struct inode            *inode  = obj->oo_inode;
4997         struct linkea_data       ldata  = { NULL };
4998         int                      rc;
4999         ENTRY;
5000
5001         fid_zero(fid);
5002         if (!S_ISDIR(inode->i_mode))
5003                 RETURN(-EIO);
5004
5005 again:
5006         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5007                              buf->lb_buf, buf->lb_len);
5008         if (rc == -ERANGE) {
5009                 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5010                                      NULL, 0);
5011                 if (rc > 0) {
5012                         lu_buf_realloc(buf, rc);
5013                         if (buf->lb_buf == NULL)
5014                                 RETURN(-ENOMEM);
5015
5016                         goto again;
5017                 }
5018         }
5019
5020         if (unlikely(rc == 0))
5021                 RETURN(-ENODATA);
5022
5023         if (rc < 0)
5024                 RETURN(rc);
5025
5026         if (unlikely(buf->lb_buf == NULL)) {
5027                 lu_buf_realloc(buf, rc);
5028                 if (buf->lb_buf == NULL)
5029                         RETURN(-ENOMEM);
5030
5031                 goto again;
5032         }
5033
5034         ldata.ld_buf = buf;
5035         rc = linkea_init_with_rec(&ldata);
5036         if (!rc) {
5037                 linkea_first_entry(&ldata);
5038                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5039         }
5040
5041         RETURN(rc);
5042 }
5043
5044 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5045                                     struct inode *inode,
5046                                     const struct lu_fid *pfid,
5047                                     const char *name, const int namelen)
5048 {
5049         struct osd_thread_info *oti = osd_oti_get(env);
5050         struct lu_buf *buf = &oti->oti_big_buf;
5051         struct dentry *dentry = &oti->oti_obj_dentry;
5052         struct linkea_data ldata = { NULL };
5053         struct lu_name cname = { .ln_name = name,
5054                                  .ln_namelen = namelen };
5055         int rc;
5056         ENTRY;
5057
5058 again:
5059         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5060                              buf->lb_buf, buf->lb_len);
5061         if (rc == -ERANGE)
5062                 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5063
5064         if (rc < 0)
5065                 RETURN(rc);
5066
5067         if (unlikely(rc == 0))
5068                 RETURN(-ENODATA);
5069
5070         if (buf->lb_len < rc) {
5071                 lu_buf_realloc(buf, rc);
5072                 if (buf->lb_buf == NULL)
5073                         RETURN(-ENOMEM);
5074
5075                 goto again;
5076         }
5077
5078         ldata.ld_buf = buf;
5079         rc = linkea_init_with_rec(&ldata);
5080         if (!rc)
5081                 rc = linkea_links_find(&ldata, &cname, pfid);
5082
5083         RETURN(rc);
5084 }
5085
5086 /**
5087  * Calls ->lookup() to find dentry. From dentry get inode and
5088  * read inode's ea to get fid. This is required for  interoperability
5089  * mode (b11826)
5090  *
5091  * \retval   0, on success
5092  * \retval -ve, on error
5093  */
5094 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5095                              struct dt_rec *rec, const struct dt_key *key)
5096 {
5097         struct inode                    *dir    = obj->oo_inode;
5098         struct dentry                   *dentry;
5099         struct ldiskfs_dir_entry_2      *de;
5100         struct buffer_head              *bh;
5101         struct lu_fid                   *fid = (struct lu_fid *) rec;
5102         struct htree_lock               *hlock = NULL;
5103         int                             ino;
5104         int                             rc;
5105         ENTRY;
5106
5107         LASSERT(dir->i_op != NULL);
5108         LASSERT(dir->i_op->lookup != NULL);
5109
5110         dentry = osd_child_dentry_get(env, obj,
5111                                       (char *)key, strlen((char *)key));
5112
5113         if (obj->oo_hl_head != NULL) {
5114                 hlock = osd_oti_get(env)->oti_hlock;
5115                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5116                                    dir, LDISKFS_HLOCK_LOOKUP);
5117         } else {
5118                 down_read(&obj->oo_ext_idx_sem);
5119         }
5120
5121         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5122         if (!IS_ERR(bh)) {
5123                 struct osd_thread_info *oti = osd_oti_get(env);
5124                 struct osd_inode_id *id = &oti->oti_id;
5125                 struct osd_idmap_cache *oic = &oti->oti_cache;
5126                 struct osd_device *dev = osd_obj2dev(obj);
5127
5128                 ino = le32_to_cpu(de->inode);
5129                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5130                         brelse(bh);
5131                         rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5132                         GOTO(out, rc);
5133                 }
5134
5135                 rc = osd_get_fid_from_dentry(de, rec);
5136
5137                 /* done with de, release bh */
5138                 brelse(bh);
5139                 if (rc != 0) {
5140                         if (unlikely(ino == osd_remote_parent_ino(dev))) {
5141                                 const char *name = (const char *)key;
5142
5143                                 /* If the parent is on remote MDT, and there
5144                                  * is no FID-in-dirent, then we have to get
5145                                  * the parent FID from the linkEA.  */
5146                                 if (likely(strlen(name) == 2 &&
5147                                            name[0] == '.' && name[1] == '.'))
5148                                         rc = osd_get_pfid_from_linkea(env, obj,
5149                                                                       fid);
5150                         } else {
5151                                 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5152                         }
5153                 } else {
5154                         osd_id_gen(id, ino, OSD_OII_NOGEN);
5155                 }
5156
5157                 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5158                         fid_zero(&oic->oic_fid);
5159
5160                         GOTO(out, rc);
5161                 }
5162
5163                 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5164                 rc = osd_consistency_check(oti, dev, oic);
5165                 if (rc != 0)
5166                         fid_zero(&oic->oic_fid);
5167         } else {
5168                 rc = PTR_ERR(bh);
5169         }
5170
5171         GOTO(out, rc);
5172
5173 out:
5174         if (hlock != NULL)
5175                 ldiskfs_htree_unlock(hlock);
5176         else
5177                 up_read(&obj->oo_ext_idx_sem);
5178         return rc;
5179 }
5180
5181 /**
5182  * Put the osd object once done with it.
5183  *
5184  * \param obj osd object that needs to be put
5185  */
5186 static inline void osd_object_put(const struct lu_env *env,
5187                                   struct osd_object *obj)
5188 {
5189         lu_object_put(env, &obj->oo_dt.do_lu);
5190 }
5191
5192 static int osd_index_declare_ea_insert(const struct lu_env *env,
5193                                        struct dt_object *dt,
5194                                        const struct dt_rec *rec,
5195                                        const struct dt_key *key,
5196                                        struct thandle *handle)
5197 {
5198         struct osd_thandle      *oh;
5199         struct osd_device       *osd   = osd_dev(dt->do_lu.lo_dev);
5200         struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
5201         const struct lu_fid     *fid    = rec1->rec_fid;
5202         int                      credits, rc = 0;
5203         struct osd_idmap_cache  *idc;
5204         ENTRY;
5205
5206         LASSERT(!dt_object_remote(dt));
5207         LASSERT(handle != NULL);
5208         LASSERT(fid != NULL);
5209         LASSERT(rec1->rec_type != 0);
5210
5211         oh = container_of0(handle, struct osd_thandle, ot_super);
5212         LASSERT(oh->ot_handle == NULL);
5213
5214         credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5215
5216         /* we can't call iget() while a transactions is running
5217          * (this can lead to a deadlock), but we need to know
5218          * inum and object type. so we find this information at
5219          * declaration and cache in per-thread info */
5220         idc = osd_idc_find_or_init(env, osd, fid);
5221         if (IS_ERR(idc))
5222                 RETURN(PTR_ERR(idc));
5223         if (idc->oic_remote) {
5224                 /* a reference to remote inode is represented by an
5225                  * agent inode which we have to create */
5226                 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5227                 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5228         }
5229
5230         osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5231
5232         if (osd_dt_obj(dt)->oo_inode != NULL) {
5233                 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5234
5235                 /* We ignore block quota on meta pool (MDTs), so needn't
5236                  * calculate how many blocks will be consumed by this index
5237                  * insert */
5238                 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5239                                            i_gid_read(inode), 0, oh,
5240                                            osd_dt_obj(dt), true, NULL, false);
5241         }
5242
5243         RETURN(rc);
5244 }
5245
5246 /**
5247  * Index add function for interoperability mode (b11826).
5248  * It will add the directory entry.This entry is needed to
5249  * maintain name->fid mapping.
5250  *
5251  * \param key it is key i.e. file entry to be inserted
5252  * \param rec it is value of given key i.e. fid
5253  *
5254  * \retval   0, on success
5255  * \retval -ve, on error
5256  */
5257 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5258                                const struct dt_rec *rec,
5259                                const struct dt_key *key, struct thandle *th,
5260                                int ignore_quota)
5261 {
5262         struct osd_object       *obj = osd_dt_obj(dt);
5263         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
5264         struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
5265         const struct lu_fid     *fid    = rec1->rec_fid;
5266         const char              *name = (const char *)key;
5267         struct osd_thread_info  *oti   = osd_oti_get(env);
5268         struct inode            *child_inode = NULL;
5269         struct osd_idmap_cache  *idc;
5270         int                     rc;
5271         ENTRY;
5272
5273         if (!dt_object_exists(dt))
5274                 RETURN(-ENOENT);
5275
5276         LASSERT(osd_invariant(obj));
5277         LASSERT(!dt_object_remote(dt));
5278         LASSERT(th != NULL);
5279
5280         osd_trans_exec_op(env, th, OSD_OT_INSERT);
5281
5282         LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5283
5284         idc = osd_idc_find(env, osd, fid);
5285         if (unlikely(idc == NULL)) {
5286                 /* this dt_insert() wasn't declared properly, so
5287                  * FID is missing in OI cache. we better do not
5288                  * lookup FID in FLDB/OI and don't risk to deadlock,
5289                  * but in some special cases (lfsck testing, etc)
5290                  * it's much simpler than fixing a caller */
5291                 CERROR("%s: "DFID" wasn't declared for insert\n",
5292                        osd_name(osd), PFID(fid));
5293                 dump_stack();
5294                 idc = osd_idc_find_or_init(env, osd, fid);
5295                 if (IS_ERR(idc))
5296                         RETURN(PTR_ERR(idc));
5297         }
5298
5299         if (idc->oic_remote) {
5300                 /* Insert remote entry */
5301                 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5302                         struct osd_mdobj_map    *omm = osd->od_mdt_map;
5303                         struct osd_thandle      *oh;
5304
5305                         /* If parent on remote MDT, we need put this object
5306                          * under AGENT */
5307                         oh = container_of(th, typeof(*oh), ot_super);
5308                         rc = osd_add_to_remote_parent(env, osd, obj, oh);
5309                         if (rc != 0) {
5310                                 CERROR("%s: add "DFID" error: rc = %d\n",
5311                                        osd_name(osd),
5312                                        PFID(lu_object_fid(&dt->do_lu)), rc);
5313                                 RETURN(rc);
5314                         }
5315
5316                         child_inode = igrab(omm->omm_remote_parent->d_inode);
5317                 } else {
5318                         child_inode = osd_create_local_agent_inode(env, osd,
5319                                         obj, fid, rec1->rec_type & S_IFMT, th);
5320                         if (IS_ERR(child_inode))
5321                                 RETURN(PTR_ERR(child_inode));
5322                 }
5323         } else {
5324                 /* Insert local entry */
5325                 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5326                         /* for a reason OI cache wasn't filled properly */
5327                         CERROR("%s: OIC for "DFID" isn't filled\n",
5328                                osd_name(osd), PFID(fid));
5329                         RETURN(-EINVAL);
5330                 }
5331                 child_inode = oti->oti_inode;
5332                 if (unlikely(child_inode == NULL)) {
5333                         struct ldiskfs_inode_info *lii;
5334                         OBD_ALLOC_PTR(lii);
5335                         if (lii == NULL)
5336                                 RETURN(-ENOMEM);
5337                         child_inode = oti->oti_inode = &lii->vfs_inode;
5338                 }
5339                 child_inode->i_sb = osd_sb(osd);
5340                 child_inode->i_ino = idc->oic_lid.oii_ino;
5341                 child_inode->i_mode = rec1->rec_type & S_IFMT;
5342         }
5343
5344         rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5345
5346         CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5347                obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5348
5349         if (child_inode && child_inode != oti->oti_inode)
5350                 iput(child_inode);
5351         LASSERT(osd_invariant(obj));
5352         osd_trans_exec_check(env, th, OSD_OT_INSERT);
5353         RETURN(rc);
5354 }
5355
5356 /**
5357  *  Initialize osd Iterator for given osd index object.
5358  *
5359  *  \param  dt      osd index object
5360  */
5361
5362 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5363                                      struct dt_object *dt,
5364                                      __u32 unused)
5365 {
5366         struct osd_it_iam      *it;
5367         struct osd_object      *obj = osd_dt_obj(dt);
5368         struct lu_object       *lo  = &dt->do_lu;
5369         struct iam_path_descr  *ipd;
5370         struct iam_container   *bag = &obj->oo_dir->od_container;
5371
5372         if (!dt_object_exists(dt))
5373                 return ERR_PTR(-ENOENT);
5374
5375         OBD_ALLOC_PTR(it);
5376         if (it == NULL)
5377                 return ERR_PTR(-ENOMEM);
5378
5379         ipd = osd_it_ipd_get(env, bag);
5380         if (likely(ipd != NULL)) {
5381                 it->oi_obj = obj;
5382                 it->oi_ipd = ipd;
5383                 lu_object_get(lo);
5384                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5385                 return (struct dt_it *)it;
5386         } else {
5387                 OBD_FREE_PTR(it);
5388                 return ERR_PTR(-ENOMEM);
5389         }
5390 }
5391
5392 /**
5393  * free given Iterator.
5394  */
5395
5396 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5397 {
5398         struct osd_it_iam       *it  = (struct osd_it_iam *)di;
5399         struct osd_object       *obj = it->oi_obj;
5400
5401         iam_it_fini(&it->oi_it);
5402         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5403         lu_object_put(env, &obj->oo_dt.do_lu);
5404         OBD_FREE_PTR(it);
5405 }
5406
5407 /**
5408  *  Move Iterator to record specified by \a key
5409  *
5410  *  \param  di      osd iterator
5411  *  \param  key     key for index
5412  *
5413  *  \retval +ve  di points to record with least key not larger than key
5414  *  \retval  0   di points to exact matched key
5415  *  \retval -ve  failure
5416  */
5417
5418 static int osd_it_iam_get(const struct lu_env *env,
5419                           struct dt_it *di, const struct dt_key *key)
5420 {
5421         struct osd_thread_info  *oti = osd_oti_get(env);
5422         struct osd_it_iam       *it = (struct osd_it_iam *)di;
5423
5424         if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5425                 /* swab quota uid/gid */
5426                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5427                 key = (struct dt_key *)&oti->oti_quota_id;
5428         }
5429
5430         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5431 }
5432
5433 /**
5434  *  Release Iterator
5435  *
5436  *  \param  di      osd iterator
5437  */
5438 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5439 {
5440         struct osd_it_iam *it = (struct osd_it_iam *)di;
5441
5442         iam_it_put(&it->oi_it);
5443 }
5444
5445 /**
5446  *  Move iterator by one record
5447  *
5448  *  \param  di      osd iterator
5449  *
5450  *  \retval +1   end of container reached
5451  *  \retval  0   success
5452  *  \retval -ve  failure
5453  */
5454
5455 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5456 {
5457         struct osd_it_iam *it = (struct osd_it_iam *)di;
5458
5459         return iam_it_next(&it->oi_it);
5460 }
5461
5462 /**
5463  * Return pointer to the key under iterator.
5464  */
5465
5466 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5467                                  const struct dt_it *di)
5468 {
5469         struct osd_thread_info *oti = osd_oti_get(env);
5470         struct osd_it_iam      *it = (struct osd_it_iam *)di;
5471         struct osd_object      *obj = it->oi_obj;
5472         struct dt_key          *key;
5473
5474         key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5475
5476         if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5477                 /* swab quota uid/gid */
5478                 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5479                 key = (struct dt_key *)&oti->oti_quota_id;
5480         }
5481
5482         return key;
5483 }
5484
5485 /**
5486  * Return size of key under iterator (in bytes)
5487  */
5488
5489 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5490 {
5491         struct osd_it_iam *it = (struct osd_it_iam *)di;
5492
5493         return iam_it_key_size(&it->oi_it);
5494 }
5495
5496 static inline void
5497 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5498 {
5499         /* check if file type is required */
5500         if (ent->lde_attrs & LUDA_TYPE) {
5501                 struct luda_type *lt;
5502                 int align = sizeof(*lt) - 1;
5503
5504                 len = (len + align) & ~align;
5505                 lt = (struct luda_type *)(ent->lde_name + len);
5506                 lt->lt_type = cpu_to_le16(DTTOIF(type));
5507         }
5508
5509         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5510 }
5511
5512 /**
5513  * build lu direct from backend fs dirent.
5514  */
5515
5516 static inline void
5517 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5518                    char *name, __u16 namelen, __u16 type, __u32 attr)
5519 {
5520         ent->lde_attrs = attr | LUDA_FID;
5521         fid_cpu_to_le(&ent->lde_fid, fid);
5522
5523         ent->lde_hash = cpu_to_le64(offset);
5524         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5525
5526         strncpy(ent->lde_name, name, namelen);
5527         ent->lde_name[namelen] = '\0';
5528         ent->lde_namelen = cpu_to_le16(namelen);
5529
5530         /* append lustre attributes */
5531         osd_it_append_attrs(ent, namelen, type);
5532 }
5533
5534 /**
5535  * Return pointer to the record under iterator.
5536  */
5537 static int osd_it_iam_rec(const struct lu_env *env,
5538                           const struct dt_it *di,
5539                           struct dt_rec *dtrec, __u32 attr)
5540 {
5541         struct osd_it_iam      *it   = (struct osd_it_iam *)di;
5542         struct osd_thread_info *info = osd_oti_get(env);
5543         ENTRY;
5544
5545         if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5546                 const struct osd_fid_pack *rec;
5547                 struct lu_fid             *fid = &info->oti_fid;
5548                 struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
5549                 char                      *name;
5550                 int                        namelen;
5551                 __u64                      hash;
5552                 int                        rc;
5553
5554                 name = (char *)iam_it_key_get(&it->oi_it);
5555                 if (IS_ERR(name))
5556                         RETURN(PTR_ERR(name));
5557
5558                 namelen = iam_it_key_size(&it->oi_it);
5559
5560                 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5561                 if (IS_ERR(rec))
5562                         RETURN(PTR_ERR(rec));
5563
5564                 rc = osd_fid_unpack(fid, rec);
5565                 if (rc)
5566                         RETURN(rc);
5567
5568                 hash = iam_it_store(&it->oi_it);
5569
5570                 /* IAM does not store object type in IAM index (dir) */
5571                 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5572                                    0, LUDA_FID);
5573         } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5574                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5575                            (struct iam_rec *)dtrec);
5576                 osd_quota_unpack(it->oi_obj, dtrec);
5577         } else {
5578                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5579                            (struct iam_rec *)dtrec);
5580         }
5581
5582         RETURN(0);
5583 }
5584
5585 /**
5586  * Returns cookie for current Iterator position.
5587  */
5588 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5589 {
5590         struct osd_it_iam *it = (struct osd_it_iam *)di;
5591
5592         return iam_it_store(&it->oi_it);
5593 }
5594
5595 /**
5596  * Restore iterator from cookie.
5597  *
5598  * \param  di      osd iterator
5599  * \param  hash    Iterator location cookie
5600  *
5601  * \retval +ve  di points to record with least key not larger than key.
5602  * \retval  0   di points to exact matched key
5603  * \retval -ve  failure
5604  */
5605
5606 static int osd_it_iam_load(const struct lu_env *env,
5607                            const struct dt_it *di, __u64 hash)
5608 {
5609         struct osd_it_iam *it = (struct osd_it_iam *)di;
5610
5611         return iam_it_load(&it->oi_it, hash);
5612 }
5613
5614 static const struct dt_index_operations osd_index_iam_ops = {
5615         .dio_lookup         = osd_index_iam_lookup,
5616         .dio_declare_insert = osd_index_declare_iam_insert,
5617         .dio_insert         = osd_index_iam_insert,
5618         .dio_declare_delete = osd_index_declare_iam_delete,
5619         .dio_delete         = osd_index_iam_delete,
5620         .dio_it     = {
5621                 .init     = osd_it_iam_init,
5622                 .fini     = osd_it_iam_fini,
5623                 .get      = osd_it_iam_get,
5624                 .put      = osd_it_iam_put,
5625                 .next     = osd_it_iam_next,
5626                 .key      = osd_it_iam_key,
5627                 .key_size = osd_it_iam_key_size,
5628                 .rec      = osd_it_iam_rec,
5629                 .store    = osd_it_iam_store,
5630                 .load     = osd_it_iam_load
5631         }
5632 };
5633
5634
5635 /**
5636  * Creates or initializes iterator context.
5637  *
5638  * \retval struct osd_it_ea, iterator structure on success
5639  *
5640  */
5641 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5642                                     struct dt_object *dt,
5643                                     __u32 attr)
5644 {
5645         struct osd_object       *obj  = osd_dt_obj(dt);
5646         struct osd_thread_info  *info = osd_oti_get(env);
5647         struct osd_it_ea        *oie;
5648         struct file             *file;
5649         struct lu_object        *lo   = &dt->do_lu;
5650         struct dentry           *obj_dentry;
5651         ENTRY;
5652
5653         if (!dt_object_exists(dt) || obj->oo_destroyed)
5654                 RETURN(ERR_PTR(-ENOENT));
5655
5656         OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5657         if (oie == NULL)
5658                 RETURN(ERR_PTR(-ENOMEM));
5659         obj_dentry = &oie->oie_dentry;
5660
5661         obj_dentry->d_inode = obj->oo_inode;
5662         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5663         obj_dentry->d_name.hash = 0;
5664
5665         oie->oie_rd_dirent       = 0;
5666         oie->oie_it_dirent       = 0;
5667         oie->oie_dirent          = NULL;
5668         if (unlikely(!info->oti_it_ea_buf_used)) {
5669                 oie->oie_buf = info->oti_it_ea_buf;
5670                 info->oti_it_ea_buf_used = 1;
5671         } else {
5672                 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5673                 if (oie->oie_buf == NULL)
5674                         RETURN(ERR_PTR(-ENOMEM));
5675         }
5676         oie->oie_obj             = obj;
5677
5678         file = &oie->oie_file;
5679
5680         /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5681         if (attr & LUDA_64BITHASH)
5682                 file->f_mode    = FMODE_64BITHASH;
5683         else
5684                 file->f_mode    = FMODE_32BITHASH;
5685         file->f_path.dentry     = obj_dentry;
5686         file->f_mapping         = obj->oo_inode->i_mapping;
5687         file->f_op              = obj->oo_inode->i_fop;
5688         set_file_inode(file, obj->oo_inode);
5689
5690         lu_object_get(lo);
5691         RETURN((struct dt_it *) oie);
5692 }
5693
5694 /**
5695  * Destroy or finishes iterator context.
5696  *
5697  * \param di iterator structure to be destroyed
5698  */
5699 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5700 {
5701         struct osd_thread_info  *info = osd_oti_get(env);
5702         struct osd_it_ea        *oie    = (struct osd_it_ea *)di;
5703         struct osd_object       *obj    = oie->oie_obj;
5704         struct inode            *inode  = obj->oo_inode;
5705
5706         ENTRY;
5707         oie->oie_file.f_op->release(inode, &oie->oie_file);
5708         lu_object_put(env, &obj->oo_dt.do_lu);
5709         if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5710                 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5711         else
5712                 info->oti_it_ea_buf_used = 0;
5713         OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5714         EXIT;
5715 }
5716
5717 /**
5718  * It position the iterator at given key, so that next lookup continues from
5719  * that key Or it is similar to dio_it->load() but based on a key,
5720  * rather than file position.
5721  *
5722  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5723  * to the beginning.
5724  *
5725  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5726  */
5727 static int osd_it_ea_get(const struct lu_env *env,
5728                          struct dt_it *di, const struct dt_key *key)
5729 {
5730         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
5731
5732         ENTRY;
5733         LASSERT(((const char *)key)[0] == '\0');
5734         it->oie_file.f_pos      = 0;
5735         it->oie_rd_dirent       = 0;
5736         it->oie_it_dirent       = 0;
5737         it->oie_dirent          = NULL;
5738
5739         RETURN(+1);
5740 }
5741
5742 /**
5743  * Does nothing
5744  */
5745 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5746 {
5747 }
5748
5749 struct osd_filldir_cbs {
5750 #ifdef HAVE_DIR_CONTEXT
5751         struct dir_context ctx;
5752 #endif
5753         struct osd_it_ea  *it;
5754 };
5755 /**
5756  * It is called internally by ->readdir(). It fills the
5757  * iterator's in-memory data structure with required
5758  * information i.e. name, namelen, rec_size etc.
5759  *
5760  * \param buf in which information to be filled in.
5761  * \param name name of the file in given dir
5762  *
5763  * \retval 0 on success
5764  * \retval 1 on buffer full
5765  */
5766 #ifdef HAVE_FILLDIR_USE_CTX
5767 static int osd_ldiskfs_filldir(struct dir_context *buf,
5768 #else
5769 static int osd_ldiskfs_filldir(void *buf,
5770 #endif
5771                                const char *name, int namelen,
5772                                loff_t offset, __u64 ino, unsigned d_type)
5773 {
5774         struct osd_it_ea        *it   =
5775                 ((struct osd_filldir_cbs *)buf)->it;
5776         struct osd_object       *obj  = it->oie_obj;
5777         struct osd_it_ea_dirent *ent  = it->oie_dirent;
5778         struct lu_fid           *fid  = &ent->oied_fid;
5779         struct osd_fid_pack     *rec;
5780         ENTRY;
5781
5782         /* this should never happen */
5783         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5784                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5785                 RETURN(-EIO);
5786         }
5787
5788         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5789             OSD_IT_EA_BUFSIZE)
5790                 RETURN(1);
5791
5792         /* "." is just the object itself. */
5793         if (namelen == 1 && name[0] == '.') {
5794                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5795         } else if (d_type & LDISKFS_DIRENT_LUFID) {
5796                 rec = (struct osd_fid_pack*) (name + namelen + 1);
5797                 if (osd_fid_unpack(fid, rec) != 0)
5798                         fid_zero(fid);
5799         } else {
5800                 fid_zero(fid);
5801         }
5802         d_type &= ~LDISKFS_DIRENT_LUFID;
5803
5804         /* NOT export local root. */
5805         if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5806                 ino = obj->oo_inode->i_ino;
5807                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5808         }
5809
5810         ent->oied_ino     = ino;
5811         ent->oied_off     = offset;
5812         ent->oied_namelen = namelen;
5813         ent->oied_type    = d_type;
5814
5815         memcpy(ent->oied_name, name, namelen);
5816
5817         it->oie_rd_dirent++;
5818         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5819         RETURN(0);
5820 }
5821
5822 /**
5823  * Calls ->readdir() to load a directory entry at a time
5824  * and stored it in iterator's in-memory data structure.
5825  *
5826  * \param di iterator's in memory structure
5827  *
5828  * \retval   0 on success
5829  * \retval -ve on error
5830  * \retval +1 reach the end of entry
5831  */
5832 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5833                                const struct dt_it *di)
5834 {
5835         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
5836         struct osd_object  *obj   = it->oie_obj;
5837         struct inode       *inode = obj->oo_inode;
5838         struct htree_lock  *hlock = NULL;
5839         struct file        *filp  = &it->oie_file;
5840         int                 rc = 0;
5841         struct osd_filldir_cbs buf = {
5842 #ifdef HAVE_DIR_CONTEXT
5843                 .ctx.actor = osd_ldiskfs_filldir,
5844 #endif
5845                 .it = it
5846         };
5847
5848         ENTRY;
5849         it->oie_dirent = it->oie_buf;
5850         it->oie_rd_dirent = 0;
5851
5852         if (obj->oo_hl_head != NULL) {
5853                 hlock = osd_oti_get(env)->oti_hlock;
5854                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5855                                    inode, LDISKFS_HLOCK_READDIR);
5856         } else {
5857                 down_read(&obj->oo_ext_idx_sem);
5858         }
5859
5860 #ifdef HAVE_DIR_CONTEXT
5861         buf.ctx.pos = filp->f_pos;
5862         rc = inode->i_fop->iterate(filp, &buf.ctx);
5863         filp->f_pos = buf.ctx.pos;
5864 #else
5865         rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5866 #endif
5867
5868         if (hlock != NULL)
5869                 ldiskfs_htree_unlock(hlock);
5870         else
5871                 up_read(&obj->oo_ext_idx_sem);
5872
5873         if (it->oie_rd_dirent == 0) {
5874                 /*If it does not get any dirent, it means it has been reached
5875                  *to the end of the dir */
5876                 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5877                 if (rc == 0)
5878                         rc = 1;
5879         } else {
5880                 it->oie_dirent = it->oie_buf;
5881                 it->oie_it_dirent = 1;
5882         }
5883
5884         RETURN(rc);
5885 }
5886
5887 /**
5888  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5889  * to load a directory entry at a time and stored it in
5890  * iterator's in-memory data structure.
5891  *
5892  * \param di iterator's in memory structure
5893  *
5894  * \retval +ve iterator reached to end
5895  * \retval   0 iterator not reached to end
5896  * \retval -ve on error
5897  */
5898 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5899 {
5900         struct osd_it_ea *it = (struct osd_it_ea *)di;
5901         int rc;
5902
5903         ENTRY;
5904
5905         if (it->oie_it_dirent < it->oie_rd_dirent) {
5906                 it->oie_dirent =
5907                         (void *) it->oie_dirent +
5908                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5909                                        it->oie_dirent->oied_namelen);
5910                 it->oie_it_dirent++;
5911                 RETURN(0);
5912         } else {
5913                 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5914                         rc = +1;
5915                 else
5916                         rc = osd_ldiskfs_it_fill(env, di);
5917         }
5918
5919         RETURN(rc);
5920 }
5921
5922 /**
5923  * Returns the key at current position from iterator's in memory structure.
5924  *
5925  * \param di iterator's in memory structure
5926  *
5927  * \retval key i.e. struct dt_key on success
5928  */
5929 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5930                                     const struct dt_it *di)
5931 {
5932         struct osd_it_ea *it = (struct osd_it_ea *)di;
5933
5934         return (struct dt_key *)it->oie_dirent->oied_name;
5935 }
5936
5937 /**
5938  * Returns the key's size at current position from iterator's in memory structure.
5939  *
5940  * \param di iterator's in memory structure
5941  *
5942  * \retval key_size i.e. struct dt_key on success
5943  */
5944 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5945 {
5946         struct osd_it_ea *it = (struct osd_it_ea *)di;
5947
5948         return it->oie_dirent->oied_namelen;
5949 }
5950
5951 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
5952 {
5953         if (LDISKFS_DIR_REC_LEN(de) >=
5954             __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
5955                 return true;
5956
5957         return false;
5958 }
5959
5960 static inline bool
5961 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5962                      unsigned blocksize, bool dotdot)
5963 {
5964         if (dotdot)
5965                 return osd_dotdot_has_space(de);
5966
5967         if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5968             __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5969                 return true;
5970
5971         return false;
5972 }
5973
5974 static int
5975 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
5976                     handle_t *jh, struct dentry *dentry,
5977                     const struct lu_fid *fid, struct buffer_head *bh,
5978                     struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
5979                     bool dotdot)
5980 {
5981         struct inode                *dir        = dentry->d_parent->d_inode;
5982         struct inode                *inode      = dentry->d_inode;
5983         struct osd_fid_pack         *rec;
5984         struct ldiskfs_dentry_param *ldp;
5985         int                          namelen    = dentry->d_name.len;
5986         int                          rc;
5987         struct osd_thread_info     *info        = osd_oti_get(env);
5988         ENTRY;
5989
5990         if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5991                                           LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5992                 RETURN(0);
5993
5994         /* There is enough space to hold the FID-in-dirent. */
5995         if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
5996                 rc = ldiskfs_journal_get_write_access(jh, bh);
5997                 if (rc != 0)
5998                         RETURN(rc);
5999
6000                 de->name[namelen] = 0;
6001                 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6002                 rec->fp_len = sizeof(struct lu_fid) + 1;
6003                 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6004                 de->file_type |= LDISKFS_DIRENT_LUFID;
6005                 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6006
6007                 RETURN(rc);
6008         }
6009
6010         LASSERT(!dotdot);
6011
6012         rc = ldiskfs_delete_entry(jh, dir, de, bh);
6013         if (rc != 0)
6014                 RETURN(rc);
6015
6016         ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6017         osd_get_ldiskfs_dirent_param(ldp, fid);
6018         dentry->d_fsdata = (void *)ldp;
6019         ll_vfs_dq_init(dir);
6020         rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6021         /* It is too bad, we cannot reinsert the name entry back.
6022          * That means we lose it! */
6023         if (rc != 0)
6024                 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
6025                        "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6026                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
6027                        dir->i_ino, dir->i_generation, namelen,
6028                        dentry->d_name.name, PFID(fid), rc);
6029
6030         RETURN(rc);
6031 }
6032
6033 static int
6034 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6035                         struct osd_it_ea *it, struct lu_fid *fid,
6036                         struct osd_inode_id *id, __u32 *attr)
6037 {
6038         struct osd_thread_info     *info        = osd_oti_get(env);
6039         struct lustre_mdt_attrs    *lma         = &info->oti_mdt_attrs;
6040         struct osd_device          *dev         = osd_obj2dev(obj);
6041         struct super_block         *sb          = osd_sb(dev);
6042         const char                 *devname     = osd_name(dev);
6043         struct osd_it_ea_dirent    *ent         = it->oie_dirent;
6044         struct inode               *dir         = obj->oo_inode;
6045         struct htree_lock          *hlock       = NULL;
6046         struct buffer_head         *bh          = NULL;
6047         handle_t                   *jh          = NULL;
6048         struct ldiskfs_dir_entry_2 *de;
6049         struct dentry              *dentry;
6050         struct inode               *inode;
6051         const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6052         int                         credits;
6053         int                         rc;
6054         bool                        dotdot      = false;
6055         bool                        dirty       = false;
6056         ENTRY;
6057
6058         if (ent->oied_name[0] == '.') {
6059                 if (ent->oied_namelen == 1)
6060                         RETURN(0);
6061
6062                 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6063                         dotdot = true;
6064         }
6065
6066         osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6067         inode = osd_iget(info, dev, id);
6068         if (IS_ERR(inode)) {
6069                 rc = PTR_ERR(inode);
6070                 if (rc == -ENOENT || rc == -ESTALE) {
6071                         /* Maybe dangling name entry, or
6072                          * corrupted directory entry. */
6073                         *attr |= LUDA_UNKNOWN;
6074                         rc = 0;
6075                 } else {
6076                         CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6077                                "check_repair, dir = %lu/%u, name = %.*s, "
6078                                "ino = %llu, rc = %d\n",
6079                                devname, dir->i_ino, dir->i_generation,
6080                                ent->oied_namelen, ent->oied_name,
6081                                ent->oied_ino, rc);
6082                 }
6083
6084                 RETURN(rc);
6085         }
6086
6087         dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6088                                            ent->oied_namelen);
6089         rc = osd_get_lma(info, inode, dentry, lma);
6090         if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6091                 lma = NULL;
6092         else if (rc != 0)
6093                 GOTO(out, rc);
6094
6095         /* We need to ensure that the name entry is still valid.
6096          * Because it may be removed or renamed by other already.
6097          *
6098          * The unlink or rename operation will start journal before PDO lock,
6099          * so to avoid deadlock, here we need to start journal handle before
6100          * related PDO lock also. But because we do not know whether there
6101          * will be something to be repaired before PDO lock, we just start
6102          * journal without conditions.
6103          *
6104          * We may need to remove the name entry firstly, then insert back.
6105          * One credit is for user quota file update.
6106          * One credit is for group quota file update.
6107          * Two credits are for dirty inode. */
6108         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6109                   osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6110
6111         if (dev->od_dirent_journal != 0) {
6112
6113 again:
6114                 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6115                 if (IS_ERR(jh)) {
6116                         rc = PTR_ERR(jh);
6117                         CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6118                                "check_repair, dir = %lu/%u, credits = %d, "
6119                                "name = %.*s, ino = %llu: rc = %d\n",
6120                                devname, dir->i_ino, dir->i_generation, credits,
6121                                ent->oied_namelen, ent->oied_name,
6122                                ent->oied_ino, rc);
6123
6124                         GOTO(out_inode, rc);
6125                 }
6126
6127                 if (obj->oo_hl_head != NULL) {
6128                         hlock = osd_oti_get(env)->oti_hlock;
6129                         /* "0" means exclusive lock for the whole directory.
6130                          * We need to prevent others access such name entry
6131                          * during the delete + insert. Neither HLOCK_ADD nor
6132                          * HLOCK_DEL cannot guarantee the atomicity. */
6133                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6134                 } else {
6135                         down_write(&obj->oo_ext_idx_sem);
6136                 }
6137         } else {
6138                 if (obj->oo_hl_head != NULL) {
6139                         hlock = osd_oti_get(env)->oti_hlock;
6140                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6141                                            LDISKFS_HLOCK_LOOKUP);
6142                 } else {
6143                         down_read(&obj->oo_ext_idx_sem);
6144                 }
6145         }
6146
6147         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6148         if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6149                 *attr |= LUDA_IGNORE;
6150
6151                 GOTO(out, rc = 0);
6152         }
6153
6154         /* For dotdot entry, if there is not enough space to hold the
6155          * FID-in-dirent, just keep them there. It only happens when the
6156          * device upgraded from 1.8 or restored from MDT file-level backup.
6157          * For the whole directory, only dotdot entry have no FID-in-dirent
6158          * and needs to get FID from LMA when readdir, it will not affect the
6159          * performance much. */
6160         if (dotdot && !osd_dotdot_has_space(de)) {
6161                 *attr |= LUDA_UNKNOWN;
6162
6163                 GOTO(out, rc = 0);
6164         }
6165
6166         if (lma != NULL) {
6167                 if (lu_fid_eq(fid, &lma->lma_self_fid))
6168                         GOTO(out, rc = 0);
6169
6170                 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6171                         struct lu_fid *tfid = &lma->lma_self_fid;
6172
6173                         if (likely(dotdot &&
6174                                    fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6175                                    fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6176                                 /* It must be REMOTE_PARENT_DIR and as the
6177                                  * 'dotdot' entry of remote directory */
6178                                 *attr |= LUDA_IGNORE;
6179                         } else {
6180                                 CDEBUG(D_LFSCK, "%s: expect remote agent "
6181                                        "parent directory, but got %.*s under "
6182                                        "dir = %lu/%u with the FID "DFID"\n",
6183                                        devname, ent->oied_namelen,
6184                                        ent->oied_name, dir->i_ino,
6185                                        dir->i_generation, PFID(tfid));
6186
6187                                 *attr |= LUDA_UNKNOWN;
6188                         }
6189
6190                         GOTO(out, rc = 0);
6191                 }
6192         }
6193
6194         if (!fid_is_zero(fid)) {
6195                 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6196                                               ent->oied_namelen);
6197                 if (rc == -ENOENT ||
6198                     (rc == -ENODATA &&
6199                      !(dev->od_scrub.os_file.sf_flags & SF_UPGRADE))) {
6200                         /* linkEA does not recognize the dirent entry,
6201                          * it may because the dirent entry corruption
6202                          * and points to other's inode. */
6203                         CDEBUG(D_LFSCK, "%s: the target inode does not "
6204                                "recognize the dirent, dir = %lu/%u, "
6205                                " name = %.*s, ino = %llu, "
6206                                DFID": rc = %d\n", devname, dir->i_ino,
6207                                dir->i_generation, ent->oied_namelen,
6208                                ent->oied_name, ent->oied_ino, PFID(fid), rc);
6209                         *attr |= LUDA_UNKNOWN;
6210
6211                         GOTO(out, rc = 0);
6212                 }
6213
6214                 if (rc && rc != -ENODATA) {
6215                         CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6216                                "dir = %lu/%u, name = %.*s, ino = %llu, "
6217                                DFID": rc = %d\n", devname, dir->i_ino,
6218                                dir->i_generation, ent->oied_namelen,
6219                                ent->oied_name, ent->oied_ino, PFID(fid), rc);
6220                         *attr |= LUDA_UNKNOWN;
6221
6222                         GOTO(out, rc = 0);
6223                 }
6224         }
6225
6226         if (lma != NULL) {
6227                 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6228                  * valid, trusted, in spite of fid_is_sane(fid) or not. */
6229                 if (*attr & LUDA_VERIFY_DRYRUN) {
6230                         *fid = lma->lma_self_fid;
6231                         *attr |= LUDA_REPAIR;
6232
6233                         GOTO(out, rc = 0);
6234                 }
6235
6236                 if (jh == NULL) {
6237                         brelse(bh);
6238                         dev->od_dirent_journal = 1;
6239                         if (hlock != NULL) {
6240                                 ldiskfs_htree_unlock(hlock);
6241                                 hlock = NULL;
6242                         } else {
6243                                 up_read(&obj->oo_ext_idx_sem);
6244                         }
6245
6246                         goto again;
6247                 }
6248
6249                 *fid = lma->lma_self_fid;
6250                 dirty = true;
6251                 /* Update or append the FID-in-dirent. */
6252                 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6253                                          bh, de, hlock, dotdot);
6254                 if (rc == 0)
6255                         *attr |= LUDA_REPAIR;
6256                 else
6257                         CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6258                                "the dirent, dir = %lu/%u, name = %.*s, "
6259                                "ino = %llu, "DFID": rc = %d\n",
6260                                devname, dir->i_ino, dir->i_generation,
6261                                ent->oied_namelen, ent->oied_name,
6262                                ent->oied_ino, PFID(fid), rc);
6263         } else {
6264                 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6265                 if (*attr & LUDA_VERIFY_DRYRUN) {
6266                         if (fid_is_sane(fid)) {
6267                                 *attr |= LUDA_REPAIR;
6268                         } else if (dev->od_index == 0) {
6269                                 lu_igif_build(fid, inode->i_ino,
6270                                               inode->i_generation);
6271                                 *attr |= LUDA_UPGRADE;
6272                         }
6273
6274                         GOTO(out, rc = 0);
6275                 }
6276
6277                 if (jh == NULL) {
6278                         brelse(bh);
6279                         dev->od_dirent_journal = 1;
6280                         if (hlock != NULL) {
6281                                 ldiskfs_htree_unlock(hlock);
6282                                 hlock = NULL;
6283                         } else {
6284                                 up_read(&obj->oo_ext_idx_sem);
6285                         }
6286
6287                         goto again;
6288                 }
6289
6290                 dirty = true;
6291                 if (unlikely(fid_is_sane(fid))) {
6292                         /* FID-in-dirent exists, but FID-in-LMA is lost.
6293                          * Trust the FID-in-dirent, and add FID-in-LMA. */
6294                         rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6295                         if (rc == 0)
6296                                 *attr |= LUDA_REPAIR;
6297                         else
6298                                 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6299                                        "update dirent, dir = %lu/%u, "
6300                                        "name = %.*s, ino = %llu, "
6301                                        DFID": rc = %d\n",
6302                                        devname, dir->i_ino, dir->i_generation,
6303                                        ent->oied_namelen, ent->oied_name,
6304                                        ent->oied_ino, PFID(fid), rc);
6305                 } else if (dev->od_index == 0) {
6306                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
6307                         /* It is probably IGIF object. Only aappend the
6308                          * FID-in-dirent. OI scrub will process FID-in-LMA. */
6309                         rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6310                                                  bh, de, hlock, dotdot);
6311                         if (rc == 0)
6312                                 *attr |= LUDA_UPGRADE;
6313                         else
6314                                 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6315                                        "after the dirent, dir = %lu/%u, "
6316                                        "name = %.*s, ino = %llu, "
6317                                        DFID": rc = %d\n",
6318                                        devname, dir->i_ino, dir->i_generation,
6319                                        ent->oied_namelen, ent->oied_name,
6320                                        ent->oied_ino, PFID(fid), rc);
6321                 }
6322         }
6323
6324         GOTO(out, rc);
6325
6326 out:
6327         if (!IS_ERR(bh))
6328                 brelse(bh);
6329         if (hlock != NULL) {
6330                 ldiskfs_htree_unlock(hlock);
6331         } else {
6332                 if (dev->od_dirent_journal != 0)
6333                         up_write(&obj->oo_ext_idx_sem);
6334                 else
6335                         up_read(&obj->oo_ext_idx_sem);
6336         }
6337
6338         if (jh != NULL)
6339                 ldiskfs_journal_stop(jh);
6340
6341 out_inode:
6342         iput(inode);
6343         if (rc >= 0 && !dirty)
6344                 dev->od_dirent_journal = 0;
6345
6346         return rc;
6347 }
6348
6349 /**
6350  * Returns the value at current position from iterator's in memory structure.
6351  *
6352  * \param di struct osd_it_ea, iterator's in memory structure
6353  * \param attr attr requested for dirent.
6354  * \param lde lustre dirent
6355  *
6356  * \retval   0 no error and \param lde has correct lustre dirent.
6357  * \retval -ve on error
6358  */
6359 static inline int osd_it_ea_rec(const struct lu_env *env,
6360                                 const struct dt_it *di,
6361                                 struct dt_rec *dtrec, __u32 attr)
6362 {
6363         struct osd_it_ea       *it    = (struct osd_it_ea *)di;
6364         struct osd_object      *obj   = it->oie_obj;
6365         struct osd_device      *dev   = osd_obj2dev(obj);
6366         struct osd_thread_info *oti   = osd_oti_get(env);
6367         struct osd_inode_id    *id    = &oti->oti_id;
6368         struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
6369         struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
6370         __u32                   ino   = it->oie_dirent->oied_ino;
6371         int                     rc    = 0;
6372         ENTRY;
6373
6374         LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6375
6376         if (attr & LUDA_VERIFY) {
6377                 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6378                         attr |= LUDA_IGNORE;
6379                         /* If the parent is on remote MDT, and there
6380                          * is no FID-in-dirent, then we have to get
6381                          * the parent FID from the linkEA.  */
6382                         if (!fid_is_sane(fid) &&
6383                             it->oie_dirent->oied_namelen == 2 &&
6384                             it->oie_dirent->oied_name[0] == '.' &&
6385                             it->oie_dirent->oied_name[1] == '.')
6386                                 osd_get_pfid_from_linkea(env, obj, fid);
6387                 } else {
6388                         rc = osd_dirent_check_repair(env, obj, it, fid, id,
6389                                                      &attr);
6390                 }
6391
6392                 if (!fid_is_sane(fid))
6393                         attr |= LUDA_UNKNOWN;
6394         } else {
6395                 attr &= ~LU_DIRENT_ATTRS_MASK;
6396                 if (!fid_is_sane(fid)) {
6397                         bool is_dotdot = false;
6398                         if (it->oie_dirent->oied_namelen == 2 &&
6399                             it->oie_dirent->oied_name[0] == '.' &&
6400                             it->oie_dirent->oied_name[1] == '.')
6401                                 is_dotdot = true;
6402                         /* If the parent is on remote MDT, and there
6403                          * is no FID-in-dirent, then we have to get
6404                          * the parent FID from the linkEA.  */
6405                         if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6406                                 rc = osd_get_pfid_from_linkea(env, obj, fid);
6407                         } else {
6408                                 if (is_dotdot == false &&
6409                                     OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6410                                         RETURN(-ENOENT);
6411
6412                                 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6413                         }
6414                 } else {
6415                         osd_id_gen(id, ino, OSD_OII_NOGEN);
6416                 }
6417         }
6418
6419         /* Pack the entry anyway, at least the offset is right. */
6420         osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6421                            it->oie_dirent->oied_name,
6422                            it->oie_dirent->oied_namelen,
6423                            it->oie_dirent->oied_type, attr);
6424
6425         if (rc < 0)
6426                 RETURN(rc);
6427
6428         if (osd_remote_fid(env, dev, fid))
6429                 RETURN(0);
6430
6431         if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6432                 osd_add_oi_cache(oti, dev, id, fid);
6433
6434         RETURN(rc > 0 ? 0 : rc);
6435 }
6436
6437 /**
6438  * Returns the record size size at current position.
6439  *
6440  * This function will return record(lu_dirent) size in bytes.
6441  *
6442  * \param[in] env       execution environment
6443  * \param[in] di        iterator's in memory structure
6444  * \param[in] attr      attribute of the entry, only requires LUDA_TYPE to
6445  *                      calculate the lu_dirent size.
6446  *
6447  * \retval      record size(in bytes & in memory) of the current lu_dirent
6448  *              entry.
6449  */
6450 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6451                               __u32 attr)
6452 {
6453         struct osd_it_ea *it = (struct osd_it_ea *)di;
6454
6455         return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6456 }
6457
6458 /**
6459  * Returns a cookie for current position of the iterator head, so that
6460  * user can use this cookie to load/start the iterator next time.
6461  *
6462  * \param di iterator's in memory structure
6463  *
6464  * \retval cookie for current position, on success
6465  */
6466 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6467 {
6468         struct osd_it_ea *it = (struct osd_it_ea *)di;
6469
6470         return it->oie_dirent->oied_off;
6471 }
6472
6473 /**
6474  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6475  * to load a directory entry at a time and stored it i inn,
6476  * in iterator's in-memory data structure.
6477  *
6478  * \param di struct osd_it_ea, iterator's in memory structure
6479  *
6480  * \retval +ve on success
6481  * \retval -ve on error
6482  */
6483 static int osd_it_ea_load(const struct lu_env *env,
6484                           const struct dt_it *di, __u64 hash)
6485 {
6486         struct osd_it_ea *it = (struct osd_it_ea *)di;
6487         int rc;
6488
6489         ENTRY;
6490         it->oie_file.f_pos = hash;
6491
6492         rc =  osd_ldiskfs_it_fill(env, di);
6493         if (rc > 0)
6494                 rc = -ENODATA;
6495
6496         if (rc == 0)
6497                 rc = +1;
6498
6499         RETURN(rc);
6500 }
6501
6502 /**
6503  * Index lookup function for interoperability mode (b11826).
6504  *
6505  * \param key,  key i.e. file name to be searched
6506  *
6507  * \retval +ve, on success
6508  * \retval -ve, on error
6509  */
6510 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6511                                struct dt_rec *rec, const struct dt_key *key)
6512 {
6513         struct osd_object *obj = osd_dt_obj(dt);
6514         int rc = 0;
6515
6516         ENTRY;
6517
6518         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6519         LINVRNT(osd_invariant(obj));
6520
6521         rc = osd_ea_lookup_rec(env, obj, rec, key);
6522         if (rc == 0)
6523                 rc = +1;
6524         RETURN(rc);
6525 }
6526
6527 /**
6528  * Index and Iterator operations for interoperability
6529  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6530  */
6531 static const struct dt_index_operations osd_index_ea_ops = {
6532         .dio_lookup         = osd_index_ea_lookup,
6533         .dio_declare_insert = osd_index_declare_ea_insert,
6534         .dio_insert         = osd_index_ea_insert,
6535         .dio_declare_delete = osd_index_declare_ea_delete,
6536         .dio_delete         = osd_index_ea_delete,
6537         .dio_it     = {
6538                 .init     = osd_it_ea_init,
6539                 .fini     = osd_it_ea_fini,
6540                 .get      = osd_it_ea_get,
6541                 .put      = osd_it_ea_put,
6542                 .next     = osd_it_ea_next,
6543                 .key      = osd_it_ea_key,
6544                 .key_size = osd_it_ea_key_size,
6545                 .rec      = osd_it_ea_rec,
6546                 .rec_size = osd_it_ea_rec_size,
6547                 .store    = osd_it_ea_store,
6548                 .load     = osd_it_ea_load
6549         }
6550 };
6551
6552 static void *osd_key_init(const struct lu_context *ctx,
6553                           struct lu_context_key *key)
6554 {
6555         struct osd_thread_info *info;
6556
6557         OBD_ALLOC_PTR(info);
6558         if (info == NULL)
6559                 return ERR_PTR(-ENOMEM);
6560
6561         OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6562         if (info->oti_it_ea_buf == NULL)
6563                 goto out_free_info;
6564
6565         info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6566
6567         info->oti_hlock = ldiskfs_htree_lock_alloc();
6568         if (info->oti_hlock == NULL)
6569                 goto out_free_ea;
6570
6571         return info;
6572
6573  out_free_ea:
6574         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6575  out_free_info:
6576         OBD_FREE_PTR(info);
6577         return ERR_PTR(-ENOMEM);
6578 }
6579
6580 static void osd_key_fini(const struct lu_context *ctx,
6581                          struct lu_context_key *key, void* data)
6582 {
6583         struct osd_thread_info *info = data;
6584         struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6585         struct osd_idmap_cache  *idc = info->oti_ins_cache;
6586
6587         if (info->oti_inode != NULL)
6588                 OBD_FREE_PTR(lli);
6589         if (info->oti_hlock != NULL)
6590                 ldiskfs_htree_lock_free(info->oti_hlock);
6591         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6592         lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6593         lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6594         lu_buf_free(&info->oti_big_buf);
6595         if (idc != NULL) {
6596                 LASSERT(info->oti_ins_cache_size > 0);
6597                 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6598                 info->oti_ins_cache = NULL;
6599                 info->oti_ins_cache_size = 0;
6600         }
6601         OBD_FREE_PTR(info);
6602 }
6603
6604 static void osd_key_exit(const struct lu_context *ctx,
6605                          struct lu_context_key *key, void *data)
6606 {
6607         struct osd_thread_info *info = data;
6608
6609         LASSERT(info->oti_r_locks == 0);
6610         LASSERT(info->oti_w_locks == 0);
6611         LASSERT(info->oti_txns    == 0);
6612 }
6613
6614 /* type constructor/destructor: osd_type_init, osd_type_fini */
6615 LU_TYPE_INIT_FINI(osd, &osd_key);
6616
6617 struct lu_context_key osd_key = {
6618         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6619         .lct_init = osd_key_init,
6620         .lct_fini = osd_key_fini,
6621         .lct_exit = osd_key_exit
6622 };
6623
6624
6625 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6626                            const char *name, struct lu_device *next)
6627 {
6628         struct osd_device *osd = osd_dev(d);
6629
6630         if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6631             >= sizeof(osd->od_svname))
6632                 return -E2BIG;
6633         return osd_procfs_init(osd, name);
6634 }
6635
6636 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6637 {
6638         struct seq_server_site  *ss = osd_seq_site(osd);
6639         int                     rc;
6640         ENTRY;
6641
6642         if (osd->od_is_ost || osd->od_cl_seq != NULL)
6643                 RETURN(0);
6644
6645         if (unlikely(ss == NULL))
6646                 RETURN(-ENODEV);
6647
6648         OBD_ALLOC_PTR(osd->od_cl_seq);
6649         if (osd->od_cl_seq == NULL)
6650                 RETURN(-ENOMEM);
6651
6652         rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6653                              osd->od_svname, ss->ss_server_seq);
6654         if (rc != 0) {
6655                 OBD_FREE_PTR(osd->od_cl_seq);
6656                 osd->od_cl_seq = NULL;
6657                 RETURN(rc);
6658         }
6659
6660         if (ss->ss_node_id == 0) {
6661                 /* If the OSD on the sequence controller(MDT0), then allocate
6662                  * sequence here, otherwise allocate sequence after connected
6663                  * to MDT0 (see mdt_register_lwp_callback()). */
6664                 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6665                                    &osd->od_cl_seq->lcs_space, env);
6666         }
6667
6668         RETURN(rc);
6669 }
6670
6671 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6672 {
6673         if (osd->od_cl_seq == NULL)
6674                 return;
6675
6676         seq_client_fini(osd->od_cl_seq);
6677         OBD_FREE_PTR(osd->od_cl_seq);
6678         osd->od_cl_seq = NULL;
6679 }
6680
6681 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6682 {
6683         ENTRY;
6684
6685         /* shutdown quota slave instance associated with the device */
6686         if (o->od_quota_slave != NULL) {
6687                 qsd_fini(env, o->od_quota_slave);
6688                 o->od_quota_slave = NULL;
6689         }
6690
6691         osd_fid_fini(env, o);
6692
6693         RETURN(0);
6694 }
6695
6696 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6697 {
6698         ENTRY;
6699
6700         if (o->od_mnt != NULL) {
6701                 shrink_dcache_sb(osd_sb(o));
6702                 osd_sync(env, &o->od_dt_dev);
6703
6704                 mntput(o->od_mnt);
6705                 o->od_mnt = NULL;
6706         }
6707
6708         EXIT;
6709 }
6710
6711 static int osd_mount(const struct lu_env *env,
6712                      struct osd_device *o, struct lustre_cfg *cfg)
6713 {
6714         const char              *name  = lustre_cfg_string(cfg, 0);
6715         const char              *dev  = lustre_cfg_string(cfg, 1);
6716         const char              *opts;
6717         unsigned long            page, s_flags, lmd_flags = 0;
6718         struct page             *__page;
6719         struct file_system_type *type;
6720         char                    *options = NULL;
6721         char                    *str;
6722         struct osd_thread_info  *info = osd_oti_get(env);
6723         struct lu_fid           *fid = &info->oti_fid;
6724         struct inode            *inode;
6725         int                      rc = 0, force_over_512tb = 0;
6726         ENTRY;
6727
6728         if (o->od_mnt != NULL)
6729                 RETURN(0);
6730
6731         if (strlen(dev) >= sizeof(o->od_mntdev))
6732                 RETURN(-E2BIG);
6733         strcpy(o->od_mntdev, dev);
6734
6735         str = lustre_cfg_string(cfg, 2);
6736         s_flags = simple_strtoul(str, NULL, 0);
6737         str = strstr(str, ":");
6738         if (str)
6739                 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6740         opts = lustre_cfg_string(cfg, 3);
6741 #ifdef __BIG_ENDIAN
6742         if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6743                 CERROR("%s: device %s extents feature is not guaranteed to "
6744                        "work on big-endian systems. Use \"bigendian_extents\" "
6745                        "mount option to override.\n", name, dev);
6746                 RETURN(-EINVAL);
6747         }
6748 #endif
6749 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6750         if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6751                 CWARN("force_over_128tb option is deprecated. "
6752                       "Filesystems less than 512TB can be created without any "
6753                       "force options. Use force_over_512tb option for "
6754                       "filesystems greater than 512TB.\n");
6755         }
6756 #endif
6757 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
6758         if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
6759                 CWARN("force_over_256tb option is deprecated. "
6760                       "Filesystems less than 512TB can be created without any "
6761                       "force options. Use force_over_512tb option for "
6762                       "filesystems greater than 512TB.\n");
6763         }
6764 #endif
6765
6766         if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
6767                 force_over_512tb = 1;
6768
6769         __page = alloc_page(GFP_KERNEL);
6770         if (__page == NULL)
6771                 GOTO(out, rc = -ENOMEM);
6772         page = (unsigned long)page_address(__page);
6773         options = (char *)page;
6774         *options = '\0';
6775         if (opts != NULL) {
6776                 /* strip out the options for back compatiblity */
6777                 static char *sout[] = {
6778                         "mballoc",
6779                         "iopen",
6780                         "noiopen",
6781                         "iopen_nopriv",
6782                         "extents",
6783                         "noextents",
6784                         /* strip out option we processed in osd */
6785                         "bigendian_extents",
6786                         "force_over_128tb",
6787                         "force_over_256tb",
6788                         "force_over_512tb",
6789                         NULL
6790                 };
6791                 strcat(options, opts);
6792                 for (rc = 0, str = options; sout[rc]; ) {
6793                         char *op = strstr(str, sout[rc]);
6794                         if (op == NULL) {
6795                                 rc++;
6796                                 str = options;
6797                                 continue;
6798                         }
6799                         if (op == options || *(op - 1) == ',') {
6800                                 str = op + strlen(sout[rc]);
6801                                 if (*str == ',' || *str == '\0') {
6802                                         *str == ',' ? str++ : str;
6803                                         memmove(op, str, strlen(str) + 1);
6804                                 }
6805                         }
6806                         for (str = op; *str != ',' && *str != '\0'; str++)
6807                                 ;
6808                 }
6809         } else {
6810                 strncat(options, "user_xattr,acl", 14);
6811         }
6812
6813         /* Glom up mount options */
6814         if (*options != '\0')
6815                 strcat(options, ",");
6816         strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6817
6818         type = get_fs_type("ldiskfs");
6819         if (!type) {
6820                 CERROR("%s: cannot find ldiskfs module\n", name);
6821                 GOTO(out, rc = -ENODEV);
6822         }
6823
6824         o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6825         module_put(type->owner);
6826
6827         if (IS_ERR(o->od_mnt)) {
6828                 rc = PTR_ERR(o->od_mnt);
6829                 o->od_mnt = NULL;
6830                 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6831                 GOTO(out, rc);
6832         }
6833
6834         if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
6835                                  osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
6836                                  force_over_512tb == 0) {
6837                 CERROR("%s: device %s LDISKFS does not support filesystems "
6838                        "greater than 512TB and can cause data corruption. "
6839                        "Use \"force_over_512tb\" mount option to override.\n",
6840                        name, dev);
6841                 GOTO(out, rc = -EINVAL);
6842         }
6843
6844 #ifdef HAVE_DEV_SET_RDONLY
6845         if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6846                 CERROR("%s: underlying device %s is marked as read-only. "
6847                        "Setup failed\n", name, dev);
6848                 GOTO(out_mnt, rc = -EROFS);
6849         }
6850 #endif
6851
6852         if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6853                                         LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6854                 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6855                 GOTO(out_mnt, rc = -EINVAL);
6856         }
6857
6858 #ifdef LDISKFS_MOUNT_DIRDATA
6859         if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6860                                          LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6861                 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6862         else if (!o->od_is_ost)
6863                 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6864                       "enabling the dirdata feature. If you do not want to "
6865                       "downgrade to Lustre-1.x again, you can enable it via "
6866                       "'tune2fs -O dirdata device'\n", name, dev);
6867 #endif
6868         inode = osd_sb(o)->s_root->d_inode;
6869         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6870         rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6871         if (rc != 0) {
6872                 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6873                 GOTO(out_mnt, rc);
6874         }
6875
6876         if (lmd_flags & LMD_FLG_NOSCRUB)
6877                 o->od_noscrub = 1;
6878
6879         GOTO(out, rc = 0);
6880
6881 out_mnt:
6882         mntput(o->od_mnt);
6883         o->od_mnt = NULL;
6884
6885 out:
6886         if (__page)
6887                 __free_page(__page);
6888
6889         return rc;
6890 }
6891
6892 static struct lu_device *osd_device_fini(const struct lu_env *env,
6893                                          struct lu_device *d)
6894 {
6895         struct osd_device *o = osd_dev(d);
6896         ENTRY;
6897
6898         osd_shutdown(env, o);
6899         osd_procfs_fini(o);
6900         osd_scrub_cleanup(env, o);
6901         osd_obj_map_fini(o);
6902         osd_umount(env, o);
6903
6904         RETURN(NULL);
6905 }
6906
6907 static int osd_device_init0(const struct lu_env *env,
6908                             struct osd_device *o,
6909                             struct lustre_cfg *cfg)
6910 {
6911         struct lu_device        *l = osd2lu_dev(o);
6912         struct osd_thread_info *info;
6913         int                     rc;
6914         int                     cplen = 0;
6915
6916         /* if the module was re-loaded, env can loose its keys */
6917         rc = lu_env_refill((struct lu_env *) env);
6918         if (rc)
6919                 GOTO(out, rc);
6920         info = osd_oti_get(env);
6921         LASSERT(info);
6922
6923         l->ld_ops = &osd_lu_ops;
6924         o->od_dt_dev.dd_ops = &osd_dt_ops;
6925
6926         spin_lock_init(&o->od_osfs_lock);
6927         mutex_init(&o->od_otable_mutex);
6928         INIT_LIST_HEAD(&o->od_orphan_list);
6929
6930         o->od_read_cache = 1;
6931         o->od_writethrough_cache = 1;
6932         o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6933
6934         cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6935                         sizeof(o->od_svname));
6936         if (cplen >= sizeof(o->od_svname)) {
6937                 rc = -E2BIG;
6938                 GOTO(out, rc);
6939         }
6940
6941         o->od_index = -1; /* -1 means index is invalid */
6942         rc = server_name2index(o->od_svname, &o->od_index, NULL);
6943         if (rc == LDD_F_SV_TYPE_OST)
6944                 o->od_is_ost = 1;
6945
6946         o->od_full_scrub_ratio = OFSR_DEFAULT;
6947         o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6948         rc = osd_mount(env, o, cfg);
6949         if (rc != 0)
6950                 GOTO(out, rc);
6951
6952         rc = osd_obj_map_init(env, o);
6953         if (rc != 0)
6954                 GOTO(out_mnt, rc);
6955
6956         rc = lu_site_init(&o->od_site, l);
6957         if (rc != 0)
6958                 GOTO(out_compat, rc);
6959         o->od_site.ls_bottom_dev = l;
6960
6961         rc = lu_site_init_finish(&o->od_site);
6962         if (rc != 0)
6963                 GOTO(out_site, rc);
6964
6965         INIT_LIST_HEAD(&o->od_ios_list);
6966         /* setup scrub, including OI files initialization */
6967         rc = osd_scrub_setup(env, o);
6968         if (rc < 0)
6969                 GOTO(out_site, rc);
6970
6971         rc = osd_procfs_init(o, o->od_svname);
6972         if (rc != 0) {
6973                 CERROR("%s: can't initialize procfs: rc = %d\n",
6974                        o->od_svname, rc);
6975                 GOTO(out_scrub, rc);
6976         }
6977
6978         LASSERT(l->ld_site->ls_linkage.next != NULL);
6979         LASSERT(l->ld_site->ls_linkage.prev != NULL);
6980
6981         /* initialize quota slave instance */
6982         o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6983                                      o->od_proc_entry);
6984         if (IS_ERR(o->od_quota_slave)) {
6985                 rc = PTR_ERR(o->od_quota_slave);
6986                 o->od_quota_slave = NULL;
6987                 GOTO(out_procfs, rc);
6988         }
6989
6990         RETURN(0);
6991
6992 out_procfs:
6993         osd_procfs_fini(o);
6994 out_scrub:
6995         osd_scrub_cleanup(env, o);
6996 out_site:
6997         lu_site_fini(&o->od_site);
6998 out_compat:
6999         osd_obj_map_fini(o);
7000 out_mnt:
7001         osd_umount(env, o);
7002 out:
7003         return rc;
7004 }
7005
7006 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7007                                           struct lu_device_type *t,
7008                                           struct lustre_cfg *cfg)
7009 {
7010         struct osd_device *o;
7011         int                rc;
7012
7013         OBD_ALLOC_PTR(o);
7014         if (o == NULL)
7015                 return ERR_PTR(-ENOMEM);
7016
7017         rc = dt_device_init(&o->od_dt_dev, t);
7018         if (rc == 0) {
7019                 /* Because the ctx might be revived in dt_device_init,
7020                  * refill the env here */
7021                 lu_env_refill((struct lu_env *)env);
7022                 rc = osd_device_init0(env, o, cfg);
7023                 if (rc)
7024                         dt_device_fini(&o->od_dt_dev);
7025         }
7026
7027         if (unlikely(rc != 0))
7028                 OBD_FREE_PTR(o);
7029
7030         return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7031 }
7032
7033 static struct lu_device *osd_device_free(const struct lu_env *env,
7034                                          struct lu_device *d)
7035 {
7036         struct osd_device *o = osd_dev(d);
7037         ENTRY;
7038
7039         /* XXX: make osd top device in order to release reference */
7040         d->ld_site->ls_top_dev = d;
7041         lu_site_purge(env, d->ld_site, -1);
7042         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7043                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7044                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7045         }
7046         lu_site_fini(&o->od_site);
7047         dt_device_fini(&o->od_dt_dev);
7048         OBD_FREE_PTR(o);
7049         RETURN(NULL);
7050 }
7051
7052 static int osd_process_config(const struct lu_env *env,
7053                               struct lu_device *d, struct lustre_cfg *cfg)
7054 {
7055         struct osd_device               *o = osd_dev(d);
7056         int                             rc;
7057         ENTRY;
7058
7059         switch (cfg->lcfg_command) {
7060         case LCFG_SETUP:
7061                 rc = osd_mount(env, o, cfg);
7062                 break;
7063         case LCFG_CLEANUP:
7064                 lu_dev_del_linkage(d->ld_site, d);
7065                 rc = osd_shutdown(env, o);
7066                 break;
7067         case LCFG_PARAM:
7068                 LASSERT(&o->od_dt_dev);
7069                 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7070                                               cfg, &o->od_dt_dev);
7071                 if (rc > 0 || rc == -ENOSYS)
7072                         rc = class_process_proc_param(PARAM_OST,
7073                                                       lprocfs_osd_obd_vars,
7074                                                       cfg, &o->od_dt_dev);
7075                 break;
7076         default:
7077                 rc = -ENOSYS;
7078         }
7079
7080         RETURN(rc);
7081 }
7082
7083 static int osd_recovery_complete(const struct lu_env *env,
7084                                  struct lu_device *d)
7085 {
7086         struct osd_device       *osd = osd_dev(d);
7087         int                      rc = 0;
7088         ENTRY;
7089
7090         if (osd->od_quota_slave == NULL)
7091                 RETURN(0);
7092
7093         /* start qsd instance on recovery completion, this notifies the quota
7094          * slave code that we are about to process new requests now */
7095         rc = qsd_start(env, osd->od_quota_slave);
7096         RETURN(rc);
7097 }
7098
7099 /*
7100  * we use exports to track all osd users
7101  */
7102 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7103                            struct obd_device *obd, struct obd_uuid *cluuid,
7104                            struct obd_connect_data *data, void *localdata)
7105 {
7106         struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
7107         struct lustre_handle  conn;
7108         int                   rc;
7109         ENTRY;
7110
7111         CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7112
7113         rc = class_connect(&conn, obd, cluuid);
7114         if (rc)
7115                 RETURN(rc);
7116
7117         *exp = class_conn2export(&conn);
7118
7119         spin_lock(&osd->od_osfs_lock);
7120         osd->od_connects++;
7121         spin_unlock(&osd->od_osfs_lock);
7122
7123         RETURN(0);
7124 }
7125
7126 /*
7127  * once last export (we don't count self-export) disappeared
7128  * osd can be released
7129  */
7130 static int osd_obd_disconnect(struct obd_export *exp)
7131 {
7132         struct obd_device *obd = exp->exp_obd;
7133         struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7134         int                rc, release = 0;
7135         ENTRY;
7136
7137         /* Only disconnect the underlying layers on the final disconnect. */
7138         spin_lock(&osd->od_osfs_lock);
7139         osd->od_connects--;
7140         if (osd->od_connects == 0)
7141                 release = 1;
7142         spin_unlock(&osd->od_osfs_lock);
7143
7144         rc = class_disconnect(exp); /* bz 9811 */
7145
7146         if (rc == 0 && release)
7147                 class_manual_cleanup(obd);
7148         RETURN(rc);
7149 }
7150
7151 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7152                        struct lu_device *dev)
7153 {
7154         struct osd_device       *osd    = osd_dev(dev);
7155         struct lr_server_data   *lsd    =
7156                         &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7157         int                      result = 0;
7158         ENTRY;
7159
7160         if (osd->od_quota_slave != NULL) {
7161                 /* set up quota slave objects */
7162                 result = qsd_prepare(env, osd->od_quota_slave);
7163                 if (result != 0)
7164                         RETURN(result);
7165         }
7166
7167         if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7168 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7169                 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7170                         osd->od_index_in_idif = 1;
7171                 } else {
7172                         osd->od_index_in_idif = 0;
7173                         result = osd_register_proc_index_in_idif(osd);
7174                         if (result != 0)
7175                                 RETURN(result);
7176                 }
7177 #else
7178                 osd->od_index_in_idif = 1;
7179 #endif
7180         }
7181
7182         result = osd_fid_init(env, osd);
7183
7184         RETURN(result);
7185 }
7186
7187 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7188                          struct lu_fid *fid, struct md_op_data *op_data)
7189 {
7190         struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7191
7192         return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7193 }
7194
7195 static const struct lu_object_operations osd_lu_obj_ops = {
7196         .loo_object_init      = osd_object_init,
7197         .loo_object_delete    = osd_object_delete,
7198         .loo_object_release   = osd_object_release,
7199         .loo_object_free      = osd_object_free,
7200         .loo_object_print     = osd_object_print,
7201         .loo_object_invariant = osd_object_invariant
7202 };
7203
7204 const struct lu_device_operations osd_lu_ops = {
7205         .ldo_object_alloc      = osd_object_alloc,
7206         .ldo_process_config    = osd_process_config,
7207         .ldo_recovery_complete = osd_recovery_complete,
7208         .ldo_prepare           = osd_prepare,
7209 };
7210
7211 static const struct lu_device_type_operations osd_device_type_ops = {
7212         .ldto_init = osd_type_init,
7213         .ldto_fini = osd_type_fini,
7214
7215         .ldto_start = osd_type_start,
7216         .ldto_stop  = osd_type_stop,
7217
7218         .ldto_device_alloc = osd_device_alloc,
7219         .ldto_device_free  = osd_device_free,
7220
7221         .ldto_device_init    = osd_device_init,
7222         .ldto_device_fini    = osd_device_fini
7223 };
7224
7225 static struct lu_device_type osd_device_type = {
7226         .ldt_tags     = LU_DEVICE_DT,
7227         .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
7228         .ldt_ops      = &osd_device_type_ops,
7229         .ldt_ctx_tags = LCT_LOCAL,
7230 };
7231
7232 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7233 {
7234         struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7235         struct super_block *sb = osd_sb(osd);
7236
7237         return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7238 }
7239
7240 /*
7241  * lprocfs legacy support.
7242  */
7243 static struct obd_ops osd_obd_device_ops = {
7244         .o_owner = THIS_MODULE,
7245         .o_connect      = osd_obd_connect,
7246         .o_disconnect   = osd_obd_disconnect,
7247         .o_fid_alloc    = osd_fid_alloc,
7248         .o_health_check = osd_health_check,
7249 };
7250
7251 static int __init osd_init(void)
7252 {
7253         int rc;
7254
7255         LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7256 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7257         /* please, try to keep osd_thread_info smaller than a page */
7258         CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7259 #endif
7260
7261         osd_oi_mod_init();
7262
7263         rc = lu_kmem_init(ldiskfs_caches);
7264         if (rc)
7265                 return rc;
7266
7267         rc = class_register_type(&osd_obd_device_ops, NULL, true,
7268                                  lprocfs_osd_module_vars,
7269                                  LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7270         if (rc)
7271                 lu_kmem_fini(ldiskfs_caches);
7272         return rc;
7273 }
7274
7275 static void __exit osd_exit(void)
7276 {
7277         class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7278         lu_kmem_fini(ldiskfs_caches);
7279 }
7280
7281 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7282 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7283 MODULE_VERSION(LUSTRE_VERSION_STRING);
7284 MODULE_LICENSE("GPL");
7285
7286 module_init(osd_init);
7287 module_exit(osd_exit);