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