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