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