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