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