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