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