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