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