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