Whamcloud - gitweb
LU-9031 osd: handle jinode change for ldiskfs
[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 = cfs_time_current();
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 = cfs_time_current();
1478 }
1479
1480 /**
1481  * Helper function to convert time interval to microseconds packed in
1482  * long int.
1483  */
1484 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1485 {
1486         struct timeval val;
1487
1488         cfs_duration_usec(cfs_time_sub(end, start), &val);
1489         return val.tv_sec * 1000000 + val.tv_usec;
1490 }
1491
1492 /**
1493  * Check whether the we deal with this handle for too long.
1494  */
1495 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1496                                 cfs_time_t alloced, cfs_time_t started,
1497                                 cfs_time_t closed)
1498 {
1499         cfs_time_t now = cfs_time_current();
1500
1501         LASSERT(dev != NULL);
1502
1503         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1504                             interval_to_usec(alloced, started));
1505         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1506                             interval_to_usec(started, closed));
1507         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1508                             interval_to_usec(closed, now));
1509
1510         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1511                 CWARN("transaction handle %p was open for too long: "
1512                       "now "CFS_TIME_T" ,"
1513                       "alloced "CFS_TIME_T" ,"
1514                       "started "CFS_TIME_T" ,"
1515                       "closed "CFS_TIME_T"\n",
1516                       oth, now, alloced, started, closed);
1517                 libcfs_debug_dumpstack(NULL);
1518         }
1519 }
1520
1521 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
1522 {                                                                       \
1523         cfs_time_t __closed = cfs_time_current();                       \
1524         cfs_time_t __alloced = oth->oth_alloced;                        \
1525         cfs_time_t __started = oth->oth_started;                        \
1526                                                                         \
1527         expr;                                                           \
1528         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
1529 }
1530
1531 #else /* OSD_THANDLE_STATS */
1532
1533 #define osd_th_alloced(h)                  do {} while(0)
1534 #define osd_th_started(h)                  do {} while(0)
1535 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
1536
1537 #endif /* OSD_THANDLE_STATS */
1538
1539 /*
1540  * Concurrency: doesn't access mutable data.
1541  */
1542 static int osd_param_is_not_sane(const struct osd_device *dev,
1543                                  const struct thandle *th)
1544 {
1545         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1546
1547         return oh->ot_credits > osd_transaction_size(dev);
1548 }
1549
1550 /*
1551  * Concurrency: shouldn't matter.
1552  */
1553 static void osd_trans_commit_cb(struct super_block *sb,
1554                                 struct ldiskfs_journal_cb_entry *jcb, int error)
1555 {
1556         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1557         struct thandle     *th  = &oh->ot_super;
1558         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
1559         struct dt_txn_commit_cb *dcb, *tmp;
1560
1561         LASSERT(oh->ot_handle == NULL);
1562
1563         if (error)
1564                 CERROR("transaction @0x%p commit error: %d\n", th, error);
1565
1566         dt_txn_hook_commit(th);
1567
1568         /* call per-transaction callbacks if any */
1569         list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1570                                  dcb_linkage) {
1571                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1572                          "commit callback entry: magic=%x name='%s'\n",
1573                          dcb->dcb_magic, dcb->dcb_name);
1574                 list_del_init(&dcb->dcb_linkage);
1575                 dcb->dcb_func(NULL, th, dcb, error);
1576         }
1577
1578         lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1579         lu_device_put(lud);
1580         th->th_dev = NULL;
1581
1582         lu_context_exit(&th->th_ctx);
1583         lu_context_fini(&th->th_ctx);
1584         OBD_FREE_PTR(oh);
1585 }
1586
1587 #ifndef HAVE_SB_START_WRITE
1588 # define sb_start_write(sb) do {} while (0)
1589 # define sb_end_write(sb) do {} while (0)
1590 #endif
1591
1592 static struct thandle *osd_trans_create(const struct lu_env *env,
1593                                         struct dt_device *d)
1594 {
1595         struct osd_thread_info  *oti = osd_oti_get(env);
1596         struct osd_iobuf        *iobuf = &oti->oti_iobuf;
1597         struct osd_thandle      *oh;
1598         struct thandle          *th;
1599         ENTRY;
1600
1601         /* on pending IO in this thread should left from prev. request */
1602         LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1603
1604         sb_start_write(osd_sb(osd_dt_dev(d)));
1605
1606         OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1607         if (oh != NULL) {
1608                 oh->ot_quota_trans = &oti->oti_quota_trans;
1609                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1610                 th = &oh->ot_super;
1611                 th->th_dev = d;
1612                 th->th_result = 0;
1613                 th->th_tags = LCT_TX_HANDLE;
1614                 oh->ot_credits = 0;
1615                 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1616                 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1617                 osd_th_alloced(oh);
1618
1619                 memset(oti->oti_declare_ops, 0,
1620                        sizeof(oti->oti_declare_ops));
1621                 memset(oti->oti_declare_ops_cred, 0,
1622                        sizeof(oti->oti_declare_ops_cred));
1623                 memset(oti->oti_declare_ops_used, 0,
1624                        sizeof(oti->oti_declare_ops_used));
1625         } else {
1626                 sb_end_write(osd_sb(osd_dt_dev(d)));
1627                 th = ERR_PTR(-ENOMEM);
1628         }
1629         RETURN(th);
1630 }
1631
1632 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1633 {
1634         struct osd_thread_info  *oti = osd_oti_get(env);
1635         struct osd_thandle      *oh;
1636
1637         oh = container_of0(th, struct osd_thandle, ot_super);
1638         LASSERT(oh != NULL);
1639
1640         CWARN("  create: %u/%u/%u, destroy: %u/%u/%u\n",
1641               oti->oti_declare_ops[OSD_OT_CREATE],
1642               oti->oti_declare_ops_cred[OSD_OT_CREATE],
1643               oti->oti_declare_ops_used[OSD_OT_CREATE],
1644               oti->oti_declare_ops[OSD_OT_DESTROY],
1645               oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1646               oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1647         CWARN("  attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1648               oti->oti_declare_ops[OSD_OT_ATTR_SET],
1649               oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1650               oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1651               oti->oti_declare_ops[OSD_OT_XATTR_SET],
1652               oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1653               oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1654         CWARN("  write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1655               oti->oti_declare_ops[OSD_OT_WRITE],
1656               oti->oti_declare_ops_cred[OSD_OT_WRITE],
1657               oti->oti_declare_ops_used[OSD_OT_WRITE],
1658               oti->oti_declare_ops[OSD_OT_PUNCH],
1659               oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1660               oti->oti_declare_ops_used[OSD_OT_PUNCH],
1661               oti->oti_declare_ops[OSD_OT_QUOTA],
1662               oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1663               oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1664         CWARN("  insert: %u/%u/%u, delete: %u/%u/%u\n",
1665               oti->oti_declare_ops[OSD_OT_INSERT],
1666               oti->oti_declare_ops_cred[OSD_OT_INSERT],
1667               oti->oti_declare_ops_used[OSD_OT_INSERT],
1668               oti->oti_declare_ops[OSD_OT_DELETE],
1669               oti->oti_declare_ops_cred[OSD_OT_DELETE],
1670               oti->oti_declare_ops_used[OSD_OT_DELETE]);
1671         CWARN("  ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1672               oti->oti_declare_ops[OSD_OT_REF_ADD],
1673               oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1674               oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1675               oti->oti_declare_ops[OSD_OT_REF_DEL],
1676               oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1677               oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1678 }
1679
1680 /*
1681  * Concurrency: shouldn't matter.
1682  */
1683 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1684                            struct thandle *th)
1685 {
1686         struct osd_thread_info *oti = osd_oti_get(env);
1687         struct osd_device  *dev = osd_dt_dev(d);
1688         handle_t           *jh;
1689         struct osd_thandle *oh;
1690         int rc;
1691
1692         ENTRY;
1693
1694         LASSERT(current->journal_info == NULL);
1695
1696         oh = container_of0(th, struct osd_thandle, ot_super);
1697         LASSERT(oh != NULL);
1698         LASSERT(oh->ot_handle == NULL);
1699
1700         rc = dt_txn_hook_start(env, d, th);
1701         if (rc != 0)
1702                 GOTO(out, rc);
1703
1704         if (unlikely(osd_param_is_not_sane(dev, th))) {
1705                 static unsigned long last_printed;
1706                 static int last_credits;
1707
1708                 /* don't make noise on a tiny testing systems
1709                  * actual credits misuse will be caught anyway */
1710                 if (last_credits != oh->ot_credits &&
1711                     time_after(jiffies, last_printed +
1712                                msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1713                     osd_transaction_size(dev) > 512) {
1714                         CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1715                               oh->ot_credits, osd_transaction_size(dev));
1716                         osd_trans_dump_creds(env, th);
1717                         libcfs_debug_dumpstack(NULL);
1718                         last_credits = oh->ot_credits;
1719                         last_printed = jiffies;
1720                 }
1721                 /* XXX Limit the credits to 'max_transaction_buffers', and
1722                  *     let the underlying filesystem to catch the error if
1723                  *     we really need so many credits.
1724                  *
1725                  *     This should be removed when we can calculate the
1726                  *     credits precisely. */
1727                 oh->ot_credits = osd_transaction_size(dev);
1728         }
1729
1730         /*
1731          * XXX temporary stuff. Some abstraction layer should
1732          * be used.
1733          */
1734         jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1735         osd_th_started(oh);
1736         if (!IS_ERR(jh)) {
1737                 oh->ot_handle = jh;
1738                 LASSERT(oti->oti_txns == 0);
1739                 lu_context_init(&th->th_ctx, th->th_tags);
1740                 lu_context_enter(&th->th_ctx);
1741
1742                 lu_device_get(&d->dd_lu_dev);
1743                 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1744                               "osd-tx", th);
1745                 oti->oti_txns++;
1746                 rc = 0;
1747         } else {
1748                 rc = PTR_ERR(jh);
1749         }
1750 out:
1751         RETURN(rc);
1752 }
1753
1754 static int osd_seq_exists(const struct lu_env *env,
1755                           struct osd_device *osd, u64 seq)
1756 {
1757         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
1758         struct seq_server_site  *ss = osd_seq_site(osd);
1759         int                     rc;
1760         ENTRY;
1761
1762         LASSERT(ss != NULL);
1763         LASSERT(ss->ss_server_fld != NULL);
1764
1765         rc = osd_fld_lookup(env, osd, seq, range);
1766         if (rc != 0) {
1767                 if (rc != -ENOENT)
1768                         CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1769                                osd_name(osd), seq, rc);
1770                 RETURN(0);
1771         }
1772
1773         RETURN(ss->ss_node_id == range->lsr_index);
1774 }
1775
1776 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1777 {
1778         struct dt_txn_commit_cb *dcb;
1779         struct dt_txn_commit_cb *tmp;
1780
1781         /* call per-transaction stop callbacks if any */
1782         list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1783                                  dcb_linkage) {
1784                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1785                          "commit callback entry: magic=%x name='%s'\n",
1786                          dcb->dcb_magic, dcb->dcb_name);
1787                 list_del_init(&dcb->dcb_linkage);
1788                 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1789         }
1790 }
1791
1792 /*
1793  * Concurrency: shouldn't matter.
1794  */
1795 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1796                           struct thandle *th)
1797 {
1798         int                     rc = 0, remove_agents = 0;
1799         struct osd_thandle     *oh;
1800         struct osd_thread_info *oti = osd_oti_get(env);
1801         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
1802         struct osd_device      *osd = osd_dt_dev(th->th_dev);
1803         struct qsd_instance    *qsd = osd->od_quota_slave;
1804         struct lquota_trans    *qtrans;
1805         ENTRY;
1806
1807         oh = container_of0(th, struct osd_thandle, ot_super);
1808
1809         /* reset OI cache for safety */
1810         oti->oti_ins_cache_used = 0;
1811
1812         remove_agents = oh->ot_remove_agents;
1813
1814         qtrans = oh->ot_quota_trans;
1815         oh->ot_quota_trans = NULL;
1816
1817         if (oh->ot_handle != NULL) {
1818                 int rc2;
1819                 handle_t *hdl = oh->ot_handle;
1820
1821                 /*
1822                  * add commit callback
1823                  * notice we don't do this in osd_trans_start()
1824                  * as underlying transaction can change during truncate
1825                  */
1826                 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1827                                          &oh->ot_jcb);
1828
1829                 LASSERT(oti->oti_txns == 1);
1830                 oti->oti_txns--;
1831
1832                 rc = dt_txn_hook_stop(env, th);
1833                 if (rc != 0)
1834                         CERROR("%s: failed in transaction hook: rc = %d\n",
1835                                osd_name(osd), rc);
1836
1837                 osd_trans_stop_cb(oh, rc);
1838                 /* hook functions might modify th_sync */
1839                 hdl->h_sync = th->th_sync;
1840
1841                 oh->ot_handle = NULL;
1842                 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1843                 if (rc2 != 0)
1844                         CERROR("%s: failed to stop transaction: rc = %d\n",
1845                                osd_name(osd), rc2);
1846                 if (!rc)
1847                         rc = rc2;
1848         } else {
1849                 osd_trans_stop_cb(oh, th->th_result);
1850                 OBD_FREE_PTR(oh);
1851         }
1852
1853         /* inform the quota slave device that the transaction is stopping */
1854         qsd_op_end(env, qsd, qtrans);
1855
1856         /* as we want IO to journal and data IO be concurrent, we don't block
1857          * awaiting data IO completion in osd_do_bio(), instead we wait here
1858          * once transaction is submitted to the journal. all reqular requests
1859          * don't do direct IO (except read/write), thus this wait_event becomes
1860          * no-op for them.
1861          *
1862          * IMPORTANT: we have to wait till any IO submited by the thread is
1863          * completed otherwise iobuf may be corrupted by different request
1864          */
1865         wait_event(iobuf->dr_wait,
1866                        atomic_read(&iobuf->dr_numreqs) == 0);
1867         osd_fini_iobuf(osd, iobuf);
1868         if (!rc)
1869                 rc = iobuf->dr_error;
1870
1871         if (unlikely(remove_agents != 0))
1872                 osd_process_scheduled_agent_removals(env, osd);
1873
1874         sb_end_write(osd_sb(osd));
1875
1876         RETURN(rc);
1877 }
1878
1879 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1880 {
1881         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1882                                                ot_super);
1883
1884         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1885         LASSERT(&dcb->dcb_func != NULL);
1886         if (dcb->dcb_flags & DCB_TRANS_STOP)
1887                 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1888         else
1889                 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1890
1891         return 0;
1892 }
1893
1894 /*
1895  * Called just before object is freed. Releases all resources except for
1896  * object itself (that is released by osd_object_free()).
1897  *
1898  * Concurrency: no concurrent access is possible that late in object
1899  * life-cycle.
1900  */
1901 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1902 {
1903         struct osd_object *obj   = osd_obj(l);
1904         struct inode      *inode = obj->oo_inode;
1905
1906         LINVRNT(osd_invariant(obj));
1907
1908         /*
1909          * If object is unlinked remove fid->ino mapping from object index.
1910          */
1911
1912         osd_index_fini(obj);
1913         if (inode != NULL) {
1914                 struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
1915                 qid_t                    uid = i_uid_read(inode);
1916                 qid_t                    gid = i_gid_read(inode);
1917
1918                 iput(inode);
1919                 obj->oo_inode = NULL;
1920
1921                 if (qsd != NULL) {
1922                         struct osd_thread_info  *info = osd_oti_get(env);
1923                         struct lquota_id_info   *qi = &info->oti_qi;
1924
1925                         /* Release granted quota to master if necessary */
1926                         qi->lqi_id.qid_uid = uid;
1927                         qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1928
1929                         qi->lqi_id.qid_uid = gid;
1930                         qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1931                 }
1932         }
1933 }
1934
1935 /*
1936  * Concurrency: ->loo_object_release() is called under site spin-lock.
1937  */
1938 static void osd_object_release(const struct lu_env *env,
1939                                struct lu_object *l)
1940 {
1941         struct osd_object *o = osd_obj(l);
1942         /* nobody should be releasing a non-destroyed object with nlink=0
1943          * the API allows this, but ldiskfs doesn't like and then report
1944          * this inode as deleted */
1945         if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1946                 LBUG();
1947 }
1948
1949 /*
1950  * Concurrency: shouldn't matter.
1951  */
1952 static int osd_object_print(const struct lu_env *env, void *cookie,
1953                             lu_printer_t p, const struct lu_object *l)
1954 {
1955         struct osd_object *o = osd_obj(l);
1956         struct iam_descr  *d;
1957
1958         if (o->oo_dir != NULL)
1959                 d = o->oo_dir->od_container.ic_descr;
1960         else
1961                 d = NULL;
1962         return (*p)(env, cookie,
1963                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1964                     o, o->oo_inode,
1965                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
1966                     o->oo_inode ? o->oo_inode->i_generation : 0,
1967                     d ? d->id_ops->id_name : "plain");
1968 }
1969
1970 /*
1971  * Concurrency: shouldn't matter.
1972  */
1973 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1974                struct obd_statfs *sfs)
1975 {
1976         struct osd_device       *osd = osd_dt_dev(d);
1977         struct super_block      *sb = osd_sb(osd);
1978         struct kstatfs          *ksfs;
1979         __u64                    reserved;
1980         int                      result = 0;
1981
1982         if (unlikely(osd->od_mnt == NULL))
1983                 return -EINPROGRESS;
1984
1985         /* osd_lproc.c call this without env, allocate ksfs for that case */
1986         if (unlikely(env == NULL)) {
1987                 OBD_ALLOC_PTR(ksfs);
1988                 if (ksfs == NULL)
1989                         return -ENOMEM;
1990         } else {
1991                 ksfs = &osd_oti_get(env)->oti_ksfs;
1992         }
1993
1994         result = sb->s_op->statfs(sb->s_root, ksfs);
1995         if (result)
1996                 goto out;
1997
1998         statfs_pack(sfs, ksfs);
1999         if (unlikely(sb->s_flags & MS_RDONLY))
2000                 sfs->os_state |= OS_STATE_READONLY;
2001         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2002                                          LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2003                 sfs->os_maxbytes = sb->s_maxbytes;
2004         else
2005                 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2006
2007         /*
2008          * Reserve some space so to avoid fragmenting the filesystem too much.
2009          * Fragmentation not only impacts performance, but can also increase
2010          * metadata overhead significantly, causing grant calculation to be
2011          * wrong.
2012          *
2013          * Reserve 0.78% of total space, at least 8MB for small filesystems.
2014          */
2015         CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2016         reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2017         if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2018                 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2019
2020         sfs->os_blocks -= reserved;
2021         sfs->os_bfree  -= min(reserved, sfs->os_bfree);
2022         sfs->os_bavail -= min(reserved, sfs->os_bavail);
2023
2024 out:
2025         if (unlikely(env == NULL))
2026                 OBD_FREE_PTR(ksfs);
2027         return result;
2028 }
2029
2030 /**
2031  * Estimate space needed for file creations. We assume the largest filename
2032  * which is 2^64 - 1, hence a filename of 20 chars.
2033  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2034  */
2035 #ifdef __LDISKFS_DIR_REC_LEN
2036 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2037 #else
2038 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2039 #endif
2040
2041 /*
2042  * Concurrency: doesn't access mutable data.
2043  */
2044 static void osd_conf_get(const struct lu_env *env,
2045                          const struct dt_device *dev,
2046                          struct dt_device_param *param)
2047 {
2048         struct super_block *sb = osd_sb(osd_dt_dev(dev));
2049         int                ea_overhead;
2050
2051         /*
2052          * XXX should be taken from not-yet-existing fs abstraction layer.
2053          */
2054         param->ddp_max_name_len = LDISKFS_NAME_LEN;
2055         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
2056         param->ddp_symlink_max  = sb->s_blocksize;
2057         param->ddp_mount_type     = LDD_MT_LDISKFS;
2058         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2059                 param->ddp_maxbytes = sb->s_maxbytes;
2060         else
2061                 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2062         /* inode are statically allocated, so per-inode space consumption
2063          * is the space consumed by the directory entry */
2064         param->ddp_inodespace     = PER_OBJ_USAGE;
2065         /* EXT_INIT_MAX_LEN is the theoretical maximum extent size  (32k blocks
2066          * = 128MB) which is unlikely to be hit in real life. Report a smaller
2067          * maximum length to not under count the actual number of extents
2068          * needed for writing a file. */
2069         param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2070         /* worst-case extent insertion metadata overhead */
2071         param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2072         param->ddp_mntopts      = 0;
2073         if (test_opt(sb, XATTR_USER))
2074                 param->ddp_mntopts |= MNTOPT_USERXATTR;
2075         if (test_opt(sb, POSIX_ACL))
2076                 param->ddp_mntopts |= MNTOPT_ACL;
2077
2078         /* LOD might calculate the max stripe count based on max_ea_size,
2079          * so we need take account in the overhead as well,
2080          * xattr_header + magic + xattr_entry_head */
2081         ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2082                       LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2083
2084 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2085         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2086                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2087                                                                 ea_overhead;
2088         else
2089 #endif
2090                 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2091 }
2092
2093 /*
2094  * Concurrency: shouldn't matter.
2095  */
2096 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2097 {
2098         int rc;
2099
2100         CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2101
2102         rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2103
2104         CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2105                LUSTRE_OSD_LDISKFS_NAME, rc);
2106
2107         return rc;
2108 }
2109
2110 /**
2111  * Start commit for OSD device.
2112  *
2113  * An implementation of dt_commit_async method for OSD device.
2114  * Asychronously starts underlayng fs sync and thereby a transaction
2115  * commit.
2116  *
2117  * \param env environment
2118  * \param d dt device
2119  *
2120  * \see dt_device_operations
2121  */
2122 static int osd_commit_async(const struct lu_env *env,
2123                             struct dt_device *d)
2124 {
2125         struct super_block *s = osd_sb(osd_dt_dev(d));
2126         ENTRY;
2127
2128         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2129         RETURN(s->s_op->sync_fs(s, 0));
2130 }
2131
2132 /*
2133  * Concurrency: shouldn't matter.
2134  */
2135
2136 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2137 {
2138         struct super_block *sb = osd_sb(osd_dt_dev(d));
2139         struct block_device *dev = sb->s_bdev;
2140 #ifdef HAVE_DEV_SET_RDONLY
2141         struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2142         int rc = 0;
2143 #else
2144         int rc = -EOPNOTSUPP;
2145 #endif
2146         ENTRY;
2147
2148 #ifdef HAVE_DEV_SET_RDONLY
2149         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2150
2151         if (sb->s_op->freeze_fs) {
2152                 rc = sb->s_op->freeze_fs(sb);
2153                 if (rc)
2154                         goto out;
2155         }
2156
2157         if (jdev && (jdev != dev)) {
2158                 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2159                        (long)jdev);
2160                 dev_set_rdonly(jdev);
2161         }
2162         CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2163         dev_set_rdonly(dev);
2164
2165         if (sb->s_op->unfreeze_fs)
2166                 sb->s_op->unfreeze_fs(sb);
2167
2168 out:
2169 #endif
2170         if (rc)
2171                 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2172                        osd_dt_dev(d)->od_svname, (long)dev, rc);
2173
2174         RETURN(rc);
2175 }
2176
2177 /**
2178  * Note: we do not count into QUOTA here.
2179  * If we mount with --data_journal we may need more.
2180  */
2181 const int osd_dto_credits_noquota[DTO_NR] = {
2182         /**
2183          * Insert.
2184          * INDEX_EXTRA_TRANS_BLOCKS(8) +
2185          * SINGLEDATA_TRANS_BLOCKS(8)
2186          * XXX Note: maybe iam need more, since iam have more level than
2187          *           EXT3 htree.
2188          */
2189         [DTO_INDEX_INSERT]  = 16,
2190         /**
2191          * Delete
2192          * just modify a single entry, probably merge few within a block
2193          */
2194         [DTO_INDEX_DELETE]  = 1,
2195         /**
2196          * Used for OI scrub
2197          */
2198         [DTO_INDEX_UPDATE]  = 16,
2199         /**
2200          * 4(inode, inode bits, groups, GDT)
2201          *   notice: OI updates are counted separately with DTO_INDEX_INSERT
2202          */
2203         [DTO_OBJECT_CREATE] = 4,
2204         /**
2205          * 4(inode, inode bits, groups, GDT)
2206          *   notice: OI updates are counted separately with DTO_INDEX_DELETE
2207          */
2208         [DTO_OBJECT_DELETE] = 4,
2209         /**
2210          * Attr set credits (inode)
2211          */
2212         [DTO_ATTR_SET_BASE] = 1,
2213         /**
2214          * Xattr set. The same as xattr of EXT3.
2215          * DATA_TRANS_BLOCKS(14)
2216          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2217          * are also counted in. Do not know why?
2218          */
2219         [DTO_XATTR_SET]     = 14,
2220         /**
2221          * credits for inode change during write.
2222          */
2223         [DTO_WRITE_BASE]    = 3,
2224         /**
2225          * credits for single block write.
2226          */
2227         [DTO_WRITE_BLOCK]   = 14,
2228         /**
2229          * Attr set credits for chown.
2230          * This is extra credits for setattr, and it is null without quota
2231          */
2232         [DTO_ATTR_SET_CHOWN] = 0
2233 };
2234
2235 static const struct dt_device_operations osd_dt_ops = {
2236         .dt_root_get       = osd_root_get,
2237         .dt_statfs         = osd_statfs,
2238         .dt_trans_create   = osd_trans_create,
2239         .dt_trans_start    = osd_trans_start,
2240         .dt_trans_stop     = osd_trans_stop,
2241         .dt_trans_cb_add   = osd_trans_cb_add,
2242         .dt_conf_get       = osd_conf_get,
2243         .dt_sync           = osd_sync,
2244         .dt_ro             = osd_ro,
2245         .dt_commit_async   = osd_commit_async,
2246 };
2247
2248 static void osd_object_read_lock(const struct lu_env *env,
2249                                  struct dt_object *dt, unsigned role)
2250 {
2251         struct osd_object *obj = osd_dt_obj(dt);
2252         struct osd_thread_info *oti = osd_oti_get(env);
2253
2254         LINVRNT(osd_invariant(obj));
2255
2256         LASSERT(obj->oo_owner != env);
2257         down_read_nested(&obj->oo_sem, role);
2258
2259         LASSERT(obj->oo_owner == NULL);
2260         oti->oti_r_locks++;
2261 }
2262
2263 static void osd_object_write_lock(const struct lu_env *env,
2264                                   struct dt_object *dt, unsigned role)
2265 {
2266         struct osd_object *obj = osd_dt_obj(dt);
2267         struct osd_thread_info *oti = osd_oti_get(env);
2268
2269         LINVRNT(osd_invariant(obj));
2270
2271         LASSERT(obj->oo_owner != env);
2272         down_write_nested(&obj->oo_sem, role);
2273
2274         LASSERT(obj->oo_owner == NULL);
2275         obj->oo_owner = env;
2276         oti->oti_w_locks++;
2277 }
2278
2279 static void osd_object_read_unlock(const struct lu_env *env,
2280                                    struct dt_object *dt)
2281 {
2282         struct osd_object *obj = osd_dt_obj(dt);
2283         struct osd_thread_info *oti = osd_oti_get(env);
2284
2285         LINVRNT(osd_invariant(obj));
2286
2287         LASSERT(oti->oti_r_locks > 0);
2288         oti->oti_r_locks--;
2289         up_read(&obj->oo_sem);
2290 }
2291
2292 static void osd_object_write_unlock(const struct lu_env *env,
2293                                     struct dt_object *dt)
2294 {
2295         struct osd_object *obj = osd_dt_obj(dt);
2296         struct osd_thread_info *oti = osd_oti_get(env);
2297
2298         LINVRNT(osd_invariant(obj));
2299
2300         LASSERT(obj->oo_owner == env);
2301         LASSERT(oti->oti_w_locks > 0);
2302         oti->oti_w_locks--;
2303         obj->oo_owner = NULL;
2304         up_write(&obj->oo_sem);
2305 }
2306
2307 static int osd_object_write_locked(const struct lu_env *env,
2308                                    struct dt_object *dt)
2309 {
2310         struct osd_object *obj = osd_dt_obj(dt);
2311
2312         LINVRNT(osd_invariant(obj));
2313
2314         return obj->oo_owner == env;
2315 }
2316
2317 static struct timespec *osd_inode_time(const struct lu_env *env,
2318                                        struct inode *inode, __u64 seconds)
2319 {
2320         struct osd_thread_info  *oti = osd_oti_get(env);
2321         struct timespec         *t   = &oti->oti_time;
2322
2323         t->tv_sec = seconds;
2324         t->tv_nsec = 0;
2325         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2326         return t;
2327 }
2328
2329 static void osd_inode_getattr(const struct lu_env *env,
2330                               struct inode *inode, struct lu_attr *attr)
2331 {
2332         attr->la_valid  |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2333                            LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2334                            LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2335                            LA_TYPE;
2336
2337         attr->la_atime   = LTIME_S(inode->i_atime);
2338         attr->la_mtime   = LTIME_S(inode->i_mtime);
2339         attr->la_ctime   = LTIME_S(inode->i_ctime);
2340         attr->la_mode    = inode->i_mode;
2341         attr->la_size    = i_size_read(inode);
2342         attr->la_blocks  = inode->i_blocks;
2343         attr->la_uid     = i_uid_read(inode);
2344         attr->la_gid     = i_gid_read(inode);
2345         attr->la_flags   = ll_inode_to_ext_flags(inode->i_flags);
2346         attr->la_nlink   = inode->i_nlink;
2347         attr->la_rdev    = inode->i_rdev;
2348         attr->la_blksize = 1 << inode->i_blkbits;
2349         attr->la_blkbits = inode->i_blkbits;
2350 }
2351
2352 static int osd_attr_get(const struct lu_env *env,
2353                         struct dt_object *dt,
2354                         struct lu_attr *attr)
2355 {
2356         struct osd_object *obj = osd_dt_obj(dt);
2357
2358         if (unlikely(!dt_object_exists(dt)))
2359                 return -ENOENT;
2360         if (unlikely(obj->oo_destroyed))
2361                 return -ENOENT;
2362
2363         LASSERT(!dt_object_remote(dt));
2364         LINVRNT(osd_invariant(obj));
2365
2366         spin_lock(&obj->oo_guard);
2367         osd_inode_getattr(env, obj->oo_inode, attr);
2368         if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2369                 attr->la_flags |= LUSTRE_ORPHAN_FL;
2370         spin_unlock(&obj->oo_guard);
2371
2372         return 0;
2373 }
2374
2375 static int osd_declare_attr_set(const struct lu_env *env,
2376                                 struct dt_object *dt,
2377                                 const struct lu_attr *attr,
2378                                 struct thandle *handle)
2379 {
2380         struct osd_thandle     *oh;
2381         struct osd_object      *obj;
2382         struct osd_thread_info *info = osd_oti_get(env);
2383         struct lquota_id_info  *qi = &info->oti_qi;
2384         qid_t                   uid;
2385         qid_t                   gid;
2386         long long               bspace;
2387         int                     rc = 0;
2388         bool                    enforce;
2389         ENTRY;
2390
2391         LASSERT(dt != NULL);
2392         LASSERT(handle != NULL);
2393
2394         obj = osd_dt_obj(dt);
2395         LASSERT(osd_invariant(obj));
2396
2397         oh = container_of0(handle, struct osd_thandle, ot_super);
2398         LASSERT(oh->ot_handle == NULL);
2399
2400         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2401                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2402
2403         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2404                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2405
2406         if (attr == NULL || obj->oo_inode == NULL)
2407                 RETURN(rc);
2408
2409         bspace   = obj->oo_inode->i_blocks;
2410         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2411         bspace   = toqb(bspace);
2412
2413         /* Changing ownership is always preformed by super user, it should not
2414          * fail with EDQUOT.
2415          *
2416          * We still need to call the osd_declare_qid() to calculate the journal
2417          * credits for updating quota accounting files and to trigger quota
2418          * space adjustment once the operation is completed.*/
2419         if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2420                 /* USERQUOTA */
2421                 uid = i_uid_read(obj->oo_inode);
2422                 qi->lqi_type = USRQUOTA;
2423                 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2424                 /* inode accounting */
2425                 qi->lqi_is_blk = false;
2426
2427                 /* one more inode for the new uid ... */
2428                 qi->lqi_id.qid_uid = attr->la_uid;
2429                 qi->lqi_space      = 1;
2430                 /* Reserve credits for the new uid */
2431                 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2432                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2433                         rc = 0;
2434                 if (rc)
2435                         RETURN(rc);
2436
2437                 /* and one less inode for the current uid */
2438                 qi->lqi_id.qid_uid = uid;
2439                 qi->lqi_space      = -1;
2440                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2441                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2442                         rc = 0;
2443                 if (rc)
2444                         RETURN(rc);
2445
2446                 /* block accounting */
2447                 qi->lqi_is_blk = true;
2448
2449                 /* more blocks for the new uid ... */
2450                 qi->lqi_id.qid_uid = attr->la_uid;
2451                 qi->lqi_space      = bspace;
2452                 /*
2453                  * Credits for the new uid has been reserved, re-use "obj"
2454                  * to save credit reservation.
2455                  */
2456                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2457                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2458                         rc = 0;
2459                 if (rc)
2460                         RETURN(rc);
2461
2462                 /* and finally less blocks for the current uid */
2463                 qi->lqi_id.qid_uid = uid;
2464                 qi->lqi_space      = -bspace;
2465                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2466                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2467                         rc = 0;
2468                 if (rc)
2469                         RETURN(rc);
2470
2471                 /* GROUP QUOTA */
2472                 gid = i_gid_read(obj->oo_inode);
2473                 qi->lqi_type = GRPQUOTA;
2474                 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2475
2476                 /* inode accounting */
2477                 qi->lqi_is_blk = false;
2478
2479                 /* one more inode for the new gid ... */
2480                 qi->lqi_id.qid_gid = attr->la_gid;
2481                 qi->lqi_space      = 1;
2482                 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2483                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2484                         rc = 0;
2485                 if (rc)
2486                         RETURN(rc);
2487
2488                 /* and one less inode for the current gid */
2489                 qi->lqi_id.qid_gid = gid;
2490                 qi->lqi_space      = -1;
2491                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2492                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2493                         rc = 0;
2494                 if (rc)
2495                         RETURN(rc);
2496
2497                 /* block accounting */
2498                 qi->lqi_is_blk = true;
2499
2500                 /* more blocks for the new gid ... */
2501                 qi->lqi_id.qid_gid = attr->la_gid;
2502                 qi->lqi_space      = bspace;
2503                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2504                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2505                         rc = 0;
2506                 if (rc)
2507                         RETURN(rc);
2508
2509                 /* and finally less blocks for the current gid */
2510                 qi->lqi_id.qid_gid = gid;
2511                 qi->lqi_space      = -bspace;
2512                 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2513                 if (rc == -EDQUOT || rc == -EINPROGRESS)
2514                         rc = 0;
2515                 if (rc)
2516                         RETURN(rc);
2517         }
2518
2519         RETURN(rc);
2520 }
2521
2522 static int osd_inode_setattr(const struct lu_env *env,
2523                              struct inode *inode, const struct lu_attr *attr)
2524 {
2525         __u64 bits = attr->la_valid;
2526
2527         /* Only allow set size for regular file */
2528         if (!S_ISREG(inode->i_mode))
2529                 bits &= ~(LA_SIZE | LA_BLOCKS);
2530
2531         if (bits == 0)
2532                 return 0;
2533
2534         if (bits & LA_ATIME)
2535                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
2536         if (bits & LA_CTIME)
2537                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
2538         if (bits & LA_MTIME)
2539                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
2540         if (bits & LA_SIZE) {
2541                 spin_lock(&inode->i_lock);
2542                 LDISKFS_I(inode)->i_disksize = attr->la_size;
2543                 i_size_write(inode, attr->la_size);
2544                 spin_unlock(&inode->i_lock);
2545         }
2546
2547         /* OSD should not change "i_blocks" which is used by quota.
2548          * "i_blocks" should be changed by ldiskfs only. */
2549         if (bits & LA_MODE)
2550                 inode->i_mode = (inode->i_mode & S_IFMT) |
2551                                 (attr->la_mode & ~S_IFMT);
2552         if (bits & LA_UID)
2553                 i_uid_write(inode, attr->la_uid);
2554         if (bits & LA_GID)
2555                 i_gid_write(inode, attr->la_gid);
2556         if (bits & LA_NLINK)
2557                 set_nlink(inode, attr->la_nlink);
2558         if (bits & LA_RDEV)
2559                 inode->i_rdev = attr->la_rdev;
2560
2561         if (bits & LA_FLAGS) {
2562                 /* always keep S_NOCMTIME */
2563                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2564                                  S_NOCMTIME;
2565         }
2566         return 0;
2567 }
2568
2569 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2570 {
2571         if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2572             (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2573                 struct iattr    iattr;
2574                 int             rc;
2575
2576                 ll_vfs_dq_init(inode);
2577                 iattr.ia_valid = 0;
2578                 if (attr->la_valid & LA_UID)
2579                         iattr.ia_valid |= ATTR_UID;
2580                 if (attr->la_valid & LA_GID)
2581                         iattr.ia_valid |= ATTR_GID;
2582                 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2583                 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2584
2585                 rc = ll_vfs_dq_transfer(inode, &iattr);
2586                 if (rc) {
2587                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
2588                                "enforcement enabled on the ldiskfs "
2589                                "filesystem?\n", inode->i_sb->s_id, rc);
2590                         return rc;
2591                 }
2592         }
2593         return 0;
2594 }
2595
2596 static int osd_attr_set(const struct lu_env *env,
2597                         struct dt_object *dt,
2598                         const struct lu_attr *attr,
2599                         struct thandle *handle)
2600 {
2601         struct osd_object *obj = osd_dt_obj(dt);
2602         struct inode      *inode;
2603         int rc;
2604
2605         if (!dt_object_exists(dt))
2606                 return -ENOENT;
2607
2608         LASSERT(handle != NULL);
2609         LASSERT(!dt_object_remote(dt));
2610         LASSERT(osd_invariant(obj));
2611
2612         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2613
2614         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2615                 struct osd_thread_info  *oti  = osd_oti_get(env);
2616                 const struct lu_fid     *fid0 = lu_object_fid(&dt->do_lu);
2617                 struct lu_fid           *fid1 = &oti->oti_fid;
2618                 struct osd_inode_id     *id   = &oti->oti_id;
2619                 struct iam_path_descr   *ipd;
2620                 struct iam_container    *bag;
2621                 struct osd_thandle      *oh;
2622                 int                      rc;
2623
2624                 fid_cpu_to_be(fid1, fid0);
2625                 memset(id, 1, sizeof(*id));
2626                 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2627                                   fid0)->oi_dir.od_container;
2628                 ipd = osd_idx_ipd_get(env, bag);
2629                 if (unlikely(ipd == NULL))
2630                         RETURN(-ENOMEM);
2631
2632                 oh = container_of0(handle, struct osd_thandle, ot_super);
2633                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2634                                 (const struct iam_rec *)id, ipd);
2635                 osd_ipd_put(env, bag, ipd);
2636                 return(rc > 0 ? 0 : rc);
2637         }
2638
2639         inode = obj->oo_inode;
2640
2641         rc = osd_quota_transfer(inode, attr);
2642         if (rc)
2643                 return rc;
2644
2645         spin_lock(&obj->oo_guard);
2646         rc = osd_inode_setattr(env, inode, attr);
2647         spin_unlock(&obj->oo_guard);
2648         if (rc != 0)
2649                 GOTO(out, rc);
2650
2651         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2652
2653         if (!(attr->la_valid & LA_FLAGS))
2654                 GOTO(out, rc);
2655
2656         /* Let's check if there are extra flags need to be set into LMA */
2657         if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2658                 struct osd_thread_info *info = osd_oti_get(env);
2659                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2660
2661                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2662                 if (rc != 0)
2663                         GOTO(out, rc);
2664
2665                 lma->lma_incompat |=
2666                         lustre_to_lma_flags(attr->la_flags);
2667                 lustre_lma_swab(lma);
2668                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2669                                      lma, sizeof(*lma), XATTR_REPLACE);
2670                 if (rc != 0) {
2671                         struct osd_device *osd = osd_obj2dev(obj);
2672
2673                         CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2674                               osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2675                               lma->lma_incompat, rc);
2676                 } else {
2677                         obj->oo_lma_flags =
2678                                 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2679                 }
2680                 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2681         }
2682 out:
2683         osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2684
2685         return rc;
2686 }
2687
2688 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2689                                            struct osd_object *obj,
2690                                            const char *name, const int namelen)
2691 {
2692         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2693 }
2694
2695 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2696                       umode_t mode, struct dt_allocation_hint *hint,
2697                       struct thandle *th)
2698 {
2699         int result;
2700         struct osd_device  *osd = osd_obj2dev(obj);
2701         struct osd_thandle *oth;
2702         struct dt_object   *parent = NULL;
2703         struct inode       *inode;
2704
2705         LINVRNT(osd_invariant(obj));
2706         LASSERT(obj->oo_inode == NULL);
2707         LASSERT(obj->oo_hl_head == NULL);
2708
2709         if (S_ISDIR(mode) && ldiskfs_pdo) {
2710                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2711                 if (obj->oo_hl_head == NULL)
2712                         return -ENOMEM;
2713         }
2714
2715         oth = container_of(th, struct osd_thandle, ot_super);
2716         LASSERT(oth->ot_handle->h_transaction != NULL);
2717
2718         if (hint != NULL && hint->dah_parent != NULL &&
2719             !dt_object_remote(hint->dah_parent))
2720                 parent = hint->dah_parent;
2721
2722         inode = ldiskfs_create_inode(oth->ot_handle,
2723                                      parent ? osd_dt_obj(parent)->oo_inode :
2724                                               osd_sb(osd)->s_root->d_inode,
2725                                      mode);
2726         if (!IS_ERR(inode)) {
2727                 /* Do not update file c/mtime in ldiskfs. */
2728                 inode->i_flags |= S_NOCMTIME;
2729
2730                 /* For new created object, it must be consistent,
2731                  * and it is unnecessary to scrub against it. */
2732                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2733
2734                 obj->oo_inode = inode;
2735                 result = 0;
2736         } else {
2737                 if (obj->oo_hl_head != NULL) {
2738                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2739                         obj->oo_hl_head = NULL;
2740                 }
2741                 result = PTR_ERR(inode);
2742         }
2743         LINVRNT(osd_invariant(obj));
2744         return result;
2745 }
2746
2747 enum {
2748         OSD_NAME_LEN = 255
2749 };
2750
2751 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2752                      struct lu_attr *attr,
2753                      struct dt_allocation_hint *hint,
2754                      struct dt_object_format *dof,
2755                      struct thandle *th)
2756 {
2757         int result;
2758         struct osd_thandle *oth;
2759         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2760
2761         LASSERT(S_ISDIR(attr->la_mode));
2762
2763         oth = container_of(th, struct osd_thandle, ot_super);
2764         LASSERT(oth->ot_handle->h_transaction != NULL);
2765         result = osd_mkfile(info, obj, mode, hint, th);
2766
2767         return result;
2768 }
2769
2770 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2771                         struct lu_attr *attr,
2772                         struct dt_allocation_hint *hint,
2773                         struct dt_object_format *dof,
2774                         struct thandle *th)
2775 {
2776         int result;
2777         struct osd_thandle *oth;
2778         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2779
2780         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2781
2782         LASSERT(S_ISREG(attr->la_mode));
2783
2784         oth = container_of(th, struct osd_thandle, ot_super);
2785         LASSERT(oth->ot_handle->h_transaction != NULL);
2786
2787         result = osd_mkfile(info, obj, mode, hint, th);
2788         if (result == 0) {
2789                 LASSERT(obj->oo_inode != NULL);
2790                 if (feat->dif_flags & DT_IND_VARKEY)
2791                         result = iam_lvar_create(obj->oo_inode,
2792                                                  feat->dif_keysize_max,
2793                                                  feat->dif_ptrsize,
2794                                                  feat->dif_recsize_max,
2795                                                  oth->ot_handle);
2796                 else
2797                         result = iam_lfix_create(obj->oo_inode,
2798                                                  feat->dif_keysize_max,
2799                                                  feat->dif_ptrsize,
2800                                                  feat->dif_recsize_max,
2801                                                  oth->ot_handle);
2802
2803         }
2804         return result;
2805 }
2806
2807 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2808                      struct lu_attr *attr,
2809                      struct dt_allocation_hint *hint,
2810                      struct dt_object_format *dof,
2811                      struct thandle *th)
2812 {
2813         LASSERT(S_ISREG(attr->la_mode));
2814         return osd_mkfile(info, obj, (attr->la_mode &
2815                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2816 }
2817
2818 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2819                      struct lu_attr *attr,
2820                      struct dt_allocation_hint *hint,
2821                      struct dt_object_format *dof,
2822                      struct thandle *th)
2823 {
2824         LASSERT(S_ISLNK(attr->la_mode));
2825         return osd_mkfile(info, obj, (attr->la_mode &
2826                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2827 }
2828
2829 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2830                      struct lu_attr *attr,
2831                      struct dt_allocation_hint *hint,
2832                      struct dt_object_format *dof,
2833                      struct thandle *th)
2834 {
2835         umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2836         int result;
2837
2838         LINVRNT(osd_invariant(obj));
2839         LASSERT(obj->oo_inode == NULL);
2840         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2841                 S_ISFIFO(mode) || S_ISSOCK(mode));
2842
2843         result = osd_mkfile(info, obj, mode, hint, th);
2844         if (result == 0) {
2845                 LASSERT(obj->oo_inode != NULL);
2846                 /*
2847                  * This inode should be marked dirty for i_rdev.  Currently
2848                  * that is done in the osd_attr_init().
2849                  */
2850                 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2851                                    attr->la_rdev);
2852         }
2853         LINVRNT(osd_invariant(obj));
2854         return result;
2855 }
2856
2857 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2858                               struct lu_attr *,
2859                               struct dt_allocation_hint *hint,
2860                               struct dt_object_format *dof,
2861                               struct thandle *);
2862
2863 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2864 {
2865         osd_obj_type_f result;
2866
2867         switch (type) {
2868         case DFT_DIR:
2869                 result = osd_mkdir;
2870                 break;
2871         case DFT_REGULAR:
2872                 result = osd_mkreg;
2873                 break;
2874         case DFT_SYM:
2875                 result = osd_mksym;
2876                 break;
2877         case DFT_NODE:
2878                 result = osd_mknod;
2879                 break;
2880         case DFT_INDEX:
2881                 result = osd_mk_index;
2882                 break;
2883
2884         default:
2885                 LBUG();
2886                 break;
2887         }
2888         return result;
2889 }
2890
2891
2892 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2893                         struct dt_object *parent, struct dt_object *child,
2894                         umode_t child_mode)
2895 {
2896         LASSERT(ah);
2897
2898         ah->dah_parent = parent;
2899         ah->dah_mode = child_mode;
2900
2901         if (parent != NULL && !dt_object_remote(parent)) {
2902                 /* will help to find FID->ino at dt_insert("..") */
2903                 struct osd_object *pobj = osd_dt_obj(parent);
2904                 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2905         }
2906 }
2907
2908 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2909                           struct lu_attr *attr, struct dt_object_format *dof)
2910 {
2911         struct inode   *inode = obj->oo_inode;
2912         __u64           valid = attr->la_valid;
2913         int             result;
2914
2915         attr->la_valid &= ~(LA_TYPE | LA_MODE);
2916
2917         if (dof->dof_type != DFT_NODE)
2918                 attr->la_valid &= ~LA_RDEV;
2919         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2920                 attr->la_valid &= ~LA_ATIME;
2921         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2922                 attr->la_valid &= ~LA_CTIME;
2923         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2924                 attr->la_valid &= ~LA_MTIME;
2925
2926         result = osd_quota_transfer(inode, attr);
2927         if (result)
2928                 return;
2929
2930         if (attr->la_valid != 0) {
2931                 result = osd_inode_setattr(info->oti_env, inode, attr);
2932                 /*
2933                  * The osd_inode_setattr() should always succeed here.  The
2934                  * only error that could be returned is EDQUOT when we are
2935                  * trying to change the UID or GID of the inode. However, this
2936                  * should not happen since quota enforcement is no longer
2937                  * enabled on ldiskfs (lquota takes care of it).
2938                  */
2939                 LASSERTF(result == 0, "%d\n", result);
2940                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2941         }
2942
2943         attr->la_valid = valid;
2944 }
2945
2946 /**
2947  * Helper function for osd_object_create()
2948  *
2949  * \retval 0, on success
2950  */
2951 static int __osd_object_create(struct osd_thread_info *info,
2952                                struct osd_object *obj, struct lu_attr *attr,
2953                                struct dt_allocation_hint *hint,
2954                                struct dt_object_format *dof,
2955                                struct thandle *th)
2956 {
2957         int     result;
2958         __u32   umask;
2959
2960         osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2961
2962         /* we drop umask so that permissions we pass are not affected */
2963         umask = current->fs->umask;
2964         current->fs->umask = 0;
2965
2966         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2967                                                   th);
2968         if (likely(obj->oo_inode != NULL)) {
2969                 LASSERT(obj->oo_inode->i_state & I_NEW);
2970
2971                 /* Unlock the inode before attr initialization to avoid
2972                  * unnecessary dqget operations. LU-6378 */
2973                 unlock_new_inode(obj->oo_inode);
2974         }
2975
2976         if (likely(result == 0)) {
2977                 osd_attr_init(info, obj, attr, dof);
2978                 osd_object_init0(obj);
2979         }
2980
2981         /* restore previous umask value */
2982         current->fs->umask = umask;
2983
2984         osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2985
2986         return result;
2987 }
2988
2989 /**
2990  * Helper function for osd_object_create()
2991  *
2992  * \retval 0, on success
2993  */
2994 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2995                            const struct lu_fid *fid, struct thandle *th)
2996 {
2997         struct osd_thread_info *info = osd_oti_get(env);
2998         struct osd_inode_id    *id   = &info->oti_id;
2999         struct osd_device      *osd  = osd_obj2dev(obj);
3000         struct osd_thandle     *oh;
3001         int                     rc;
3002
3003         LASSERT(obj->oo_inode != NULL);
3004
3005         oh = container_of0(th, struct osd_thandle, ot_super);
3006         LASSERT(oh->ot_handle);
3007         osd_trans_exec_op(env, th, OSD_OT_INSERT);
3008
3009         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3010         rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3011                            OI_CHECK_FLD, NULL);
3012         osd_trans_exec_check(env, th, OSD_OT_INSERT);
3013
3014         return rc;
3015 }
3016
3017 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3018                    u64 seq, struct lu_seq_range *range)
3019 {
3020         struct seq_server_site  *ss = osd_seq_site(osd);
3021
3022         if (fid_seq_is_idif(seq)) {
3023                 fld_range_set_ost(range);
3024                 range->lsr_index = idif_ost_idx(seq);
3025                 return 0;
3026         }
3027
3028         if (!fid_seq_in_fldb(seq)) {
3029                 fld_range_set_mdt(range);
3030                 if (ss != NULL)
3031                         /* FIXME: If ss is NULL, it suppose not get lsr_index
3032                          * at all */
3033                         range->lsr_index = ss->ss_node_id;
3034                 return 0;
3035         }
3036
3037         LASSERT(ss != NULL);
3038         fld_range_set_any(range);
3039         /* OSD will only do local fld lookup */
3040         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3041 }
3042
3043 /*
3044  * Concurrency: no external locking is necessary.
3045  */
3046 static int osd_declare_object_create(const struct lu_env *env,
3047                                      struct dt_object *dt,
3048                                      struct lu_attr *attr,
3049                                      struct dt_allocation_hint *hint,
3050                                      struct dt_object_format *dof,
3051                                      struct thandle *handle)
3052 {
3053         struct osd_thandle      *oh;
3054         int                      rc;
3055         ENTRY;
3056
3057         LASSERT(handle != NULL);
3058
3059         oh = container_of0(handle, struct osd_thandle, ot_super);
3060         LASSERT(oh->ot_handle == NULL);
3061
3062         /* EA object consumes more credits than regular object: osd_mk_index
3063          * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3064          * leaf_node, could involves the block, block bitmap, groups, GDT
3065          * change for each block, so add 4 * 2 credits in that case. */
3066         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3067                              osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3068                              (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3069         /* Reuse idle OI block may cause additional one OI block
3070          * to be changed. */
3071         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3072                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3073
3074         if (!attr)
3075                 RETURN(0);
3076
3077         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3078                                    osd_dt_obj(dt), false, NULL, false);
3079         if (rc != 0)
3080                 RETURN(rc);
3081
3082         /* will help to find FID->ino mapping at dt_insert() */
3083         rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3084                                    osd_dt_obj(dt));
3085
3086         RETURN(rc);
3087 }
3088
3089 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3090                              struct lu_attr *attr,
3091                              struct dt_allocation_hint *hint,
3092                              struct dt_object_format *dof, struct thandle *th)
3093 {
3094         const struct lu_fid     *fid    = lu_object_fid(&dt->do_lu);
3095         struct osd_object       *obj    = osd_dt_obj(dt);
3096         struct osd_thread_info  *info   = osd_oti_get(env);
3097         int result;
3098         ENTRY;
3099
3100         if (dt_object_exists(dt))
3101                 return -EEXIST;
3102
3103         LINVRNT(osd_invariant(obj));
3104         LASSERT(!dt_object_remote(dt));
3105         LASSERT(osd_write_locked(env, obj));
3106         LASSERT(th != NULL);
3107
3108         if (unlikely(fid_is_acct(fid)))
3109                 /* Quota files can't be created from the kernel any more,
3110                  * 'tune2fs -O quota' will take care of creating them */
3111                 RETURN(-EPERM);
3112
3113         result = __osd_object_create(info, obj, attr, hint, dof, th);
3114         if (result == 0) {
3115                 result = __osd_oi_insert(env, obj, fid, th);
3116                 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3117                         obj->oo_dt.do_body_ops = &osd_body_ops;
3118         }
3119         LASSERT(ergo(result == 0,
3120                 dt_object_exists(dt) && !dt_object_remote(dt)));
3121
3122         LASSERT(osd_invariant(obj));
3123         RETURN(result);
3124 }
3125
3126 /**
3127  * Called to destroy on-disk representation of the object
3128  *
3129  * Concurrency: must be locked
3130  */
3131 static int osd_declare_object_destroy(const struct lu_env *env,
3132                                       struct dt_object *dt,
3133                                       struct thandle *th)
3134 {
3135         struct osd_object  *obj = osd_dt_obj(dt);
3136         struct inode       *inode = obj->oo_inode;
3137         struct osd_thandle *oh;
3138         int                 rc;
3139         ENTRY;
3140
3141         if (inode == NULL)
3142                 RETURN(-ENOENT);
3143
3144         oh = container_of0(th, struct osd_thandle, ot_super);
3145         LASSERT(oh->ot_handle == NULL);
3146
3147         osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3148                              osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3149         /* Recycle idle OI leaf may cause additional three OI blocks
3150          * to be changed. */
3151         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3152                 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3153                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3154         /* one less inode */
3155         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3156                                    -1, oh, obj, false, NULL, false);
3157         if (rc)
3158                 RETURN(rc);
3159         /* data to be truncated */
3160         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3161                                    0, oh, obj, true, NULL, false);
3162         if (rc)
3163                 RETURN(rc);
3164
3165         /* will help to find FID->ino when this object is being
3166          * added to PENDING/ */
3167         rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3168
3169         RETURN(rc);
3170 }
3171
3172 static int osd_object_destroy(const struct lu_env *env,
3173                               struct dt_object *dt,
3174                               struct thandle *th)
3175 {
3176         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
3177         struct osd_object      *obj = osd_dt_obj(dt);
3178         struct inode           *inode = obj->oo_inode;
3179         struct osd_device      *osd = osd_obj2dev(obj);
3180         struct osd_thandle     *oh;
3181         int                     result;
3182         ENTRY;
3183
3184         oh = container_of0(th, struct osd_thandle, ot_super);
3185         LASSERT(oh->ot_handle);
3186         LASSERT(inode);
3187         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3188
3189         if (unlikely(fid_is_acct(fid)))
3190                 RETURN(-EPERM);
3191
3192         if (S_ISDIR(inode->i_mode)) {
3193                 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3194                         inode->i_nlink == 2);
3195                 /* it will check/delete the inode from remote parent,
3196                  * how to optimize it? unlink performance impaction XXX */
3197                 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3198                 if (result != 0)
3199                         CERROR("%s: delete inode "DFID": rc = %d\n",
3200                                osd_name(osd), PFID(fid), result);
3201
3202                 spin_lock(&obj->oo_guard);
3203                 clear_nlink(inode);
3204                 spin_unlock(&obj->oo_guard);
3205                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3206         }
3207
3208         osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3209
3210         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3211
3212         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3213                 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3214                                        oh->ot_handle, OI_CHECK_FLD);
3215
3216         osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3217         /* XXX: add to ext3 orphan list */
3218         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3219
3220         /* not needed in the cache anymore */
3221         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3222         obj->oo_destroyed = 1;
3223
3224         RETURN(0);
3225 }
3226
3227 /**
3228  * Put the fid into lustre_mdt_attrs, and then place the structure
3229  * inode's ea. This fid should not be altered during the life time
3230  * of the inode.
3231  *
3232  * \retval +ve, on success
3233  * \retval -ve, on error
3234  *
3235  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3236  */
3237 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3238                    const struct lu_fid *fid, __u32 compat, __u32 incompat)
3239 {
3240         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3241         int                      rc;
3242         ENTRY;
3243
3244         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3245                 RETURN(0);
3246
3247         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3248                 rc = -ENOMEM;
3249
3250         lustre_lma_init(lma, fid, compat, incompat);
3251         lustre_lma_swab(lma);
3252
3253         rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3254                              XATTR_CREATE);
3255         /* LMA may already exist, but we need to check that all the
3256          * desired compat/incompat flags have been added. */
3257         if (unlikely(rc == -EEXIST)) {
3258                 if (compat == 0 && incompat == 0)
3259                         RETURN(0);
3260
3261                 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3262                                      XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3263                                      LMA_OLD_SIZE);
3264                 if (rc <= 0)
3265                         RETURN(-EINVAL);
3266
3267                 lustre_lma_swab(lma);
3268                 if (!(~lma->lma_compat & compat) &&
3269                     !(~lma->lma_incompat & incompat))
3270                         RETURN(0);
3271
3272                 lma->lma_compat |= compat;
3273                 lma->lma_incompat |= incompat;
3274                 lustre_lma_swab(lma);
3275                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3276                                      sizeof(*lma), XATTR_REPLACE);
3277         }
3278
3279         RETURN(rc);
3280 }
3281
3282 /**
3283  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3284  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3285  * To have compatilibility with 1.8 ldiskfs driver we need to have
3286  * magic number at start of fid data.
3287  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3288  * its inmemory API.
3289  */
3290 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3291                                          const struct lu_fid *fid)
3292 {
3293         if (!fid_is_namespace_visible(fid) ||
3294             OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3295                 param->edp_magic = 0;
3296                 return;
3297         }
3298
3299         param->edp_magic = LDISKFS_LUFID_MAGIC;
3300         param->edp_len =  sizeof(struct lu_fid) + 1;
3301         fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3302 }
3303
3304 /**
3305  * Try to read the fid from inode ea into dt_rec.
3306  *
3307  * \param fid object fid.
3308  *
3309  * \retval 0 on success
3310  */
3311 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3312                           __u32 ino, struct lu_fid *fid,
3313                           struct osd_inode_id *id)
3314 {
3315         struct osd_thread_info *info  = osd_oti_get(env);
3316         struct inode           *inode;
3317         ENTRY;
3318
3319         osd_id_gen(id, ino, OSD_OII_NOGEN);
3320         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3321         if (IS_ERR(inode))
3322                 RETURN(PTR_ERR(inode));
3323
3324         iput(inode);
3325         RETURN(0);
3326 }
3327
3328 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3329                                         struct inode *dir,
3330                                         struct inode *parent_dir,
3331                                         const struct lu_fid *dot_fid,
3332                                         const struct lu_fid *dot_dot_fid,
3333                                         struct osd_thandle *oth)
3334 {
3335         struct ldiskfs_dentry_param *dot_ldp;
3336         struct ldiskfs_dentry_param *dot_dot_ldp;
3337         __u32 saved_nlink = dir->i_nlink;
3338         int rc;
3339
3340         dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3341         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3342
3343         dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3344         dot_ldp->edp_magic = 0;
3345
3346         rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3347                                     dir, dot_ldp, dot_dot_ldp);
3348         /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3349          * the subseqent ref_add() will increase the dir->i_nlink
3350          * as 3. That is incorrect for new created directory.
3351          *
3352          * It looks like hack, because we want to make the OSD API
3353          * to be order-independent for new created directory object
3354          * between dt_insert(..) and ref_add() operations.
3355          *
3356          * Here, we only restore the in-RAM dir-inode's nlink attr,
3357          * becuase if the nlink attr is not 2, then there will be
3358          * ref_add() called following the dt_insert(..), such call
3359          * will make both the in-RAM and on-disk dir-inode's nlink
3360          * attr to be set as 2. LU-7447 */
3361         set_nlink(dir, saved_nlink);
3362         return rc;
3363 }
3364
3365 /**
3366  * Create an local agent inode for remote entry
3367  */
3368 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3369                                                   struct osd_device *osd,
3370                                                   struct osd_object *pobj,
3371                                                   const struct lu_fid *fid,
3372                                                   __u32 type,
3373                                                   struct thandle *th)
3374 {
3375         struct osd_thread_info  *info = osd_oti_get(env);
3376         struct inode            *local;
3377         struct osd_thandle      *oh;
3378         int                     rc;
3379         ENTRY;
3380
3381         LASSERT(th);
3382         oh = container_of(th, struct osd_thandle, ot_super);
3383         LASSERT(oh->ot_handle->h_transaction != NULL);
3384
3385         local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3386         if (IS_ERR(local)) {
3387                 CERROR("%s: create local error %d\n", osd_name(osd),
3388                        (int)PTR_ERR(local));
3389                 RETURN(local);
3390         }
3391
3392         /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3393          * correct gid on remote file, not agent here */
3394         local->i_gid = current_fsgid();
3395         ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3396         unlock_new_inode(local);
3397
3398         /* Set special LMA flag for local agent inode */
3399         rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3400         if (rc != 0) {
3401                 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3402                        osd_name(osd), PFID(fid), rc);
3403                 RETURN(ERR_PTR(rc));
3404         }
3405
3406         if (!S_ISDIR(type))
3407                 RETURN(local);
3408
3409         rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3410                                          lu_object_fid(&pobj->oo_dt.do_lu),
3411                                          fid, oh);
3412         if (rc != 0) {
3413                 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3414                         osd_name(osd), PFID(fid), rc);
3415                 RETURN(ERR_PTR(rc));
3416         }
3417
3418         RETURN(local);
3419 }
3420
3421 /**
3422  * when direntry is deleted, we have to take care of possible agent inode
3423  * referenced by that. unfortunately we can't do this at that point:
3424  * iget() within a running transaction leads to deadlock and we better do
3425  * not call that every delete declaration to save performance. so we put
3426  * a potention agent inode on a list and process that once the transaction
3427  * is over. Notice it's not any worse in terms of real orphans as regular
3428  * object destroy doesn't put inodes on the on-disk orphan list. this should
3429  * be addressed separately
3430  */
3431 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3432                                             struct osd_thandle *oh,
3433                                             __u32 ino)
3434 {
3435         struct osd_device      *osd = osd_dt_dev(oh->ot_super.th_dev);
3436         struct osd_obj_orphan *oor;
3437
3438         OBD_ALLOC_PTR(oor);
3439         if (oor == NULL)
3440                 return -ENOMEM;
3441
3442         oor->oor_ino = ino;
3443         oor->oor_env = (struct lu_env *)env;
3444         spin_lock(&osd->od_osfs_lock);
3445         list_add(&oor->oor_list, &osd->od_orphan_list);
3446         spin_unlock(&osd->od_osfs_lock);
3447
3448         oh->ot_remove_agents = 1;
3449
3450         return 0;
3451
3452 }
3453
3454 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3455                                                 struct osd_device *osd)
3456 {
3457         struct osd_thread_info *info = osd_oti_get(env);
3458         struct osd_obj_orphan *oor, *tmp;
3459         struct osd_inode_id id;
3460         struct list_head list;
3461         struct inode *inode;
3462         struct lu_fid fid;
3463         handle_t *jh;
3464         __u32 ino;
3465
3466         INIT_LIST_HEAD(&list);
3467
3468         spin_lock(&osd->od_osfs_lock);
3469         list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3470                 if (oor->oor_env == env) {
3471                         list_del(&oor->oor_list);
3472                         list_add(&oor->oor_list, &list);
3473                 }
3474         }
3475         spin_unlock(&osd->od_osfs_lock);
3476
3477         list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3478
3479                 ino = oor->oor_ino;
3480
3481                 list_del(&oor->oor_list);
3482                 OBD_FREE_PTR(oor);
3483
3484                 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3485                 inode = osd_iget_fid(info, osd, &id, &fid);
3486                 if (IS_ERR(inode))
3487                         continue;
3488
3489                 if (!osd_remote_fid(env, osd, &fid)) {
3490                         iput(inode);
3491                         continue;
3492                 }
3493
3494                 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3495                 clear_nlink(inode);
3496                 mark_inode_dirty(inode);
3497                 ldiskfs_journal_stop(jh);
3498                 iput(inode);
3499         }
3500
3501         return 0;
3502 }
3503
3504 /**
3505  * OSD layer object create function for interoperability mode (b11826).
3506  * This is mostly similar to osd_object_create(). Only difference being, fid is
3507  * inserted into inode ea here.
3508  *
3509  * \retval   0, on success
3510  * \retval -ve, on error
3511  */
3512 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3513                                 struct lu_attr *attr,
3514                                 struct dt_allocation_hint *hint,
3515                                 struct dt_object_format *dof,
3516                                 struct thandle *th)
3517 {
3518         const struct lu_fid     *fid    = lu_object_fid(&dt->do_lu);
3519         struct osd_object       *obj    = osd_dt_obj(dt);
3520         struct osd_thread_info  *info   = osd_oti_get(env);
3521         int                      result, on_ost = 0;
3522
3523         ENTRY;
3524
3525         if (dt_object_exists(dt))
3526                 RETURN(-EEXIST);
3527
3528         LASSERT(osd_invariant(obj));
3529         LASSERT(!dt_object_remote(dt));
3530         LASSERT(osd_write_locked(env, obj));
3531         LASSERT(th != NULL);
3532
3533         if (unlikely(fid_is_acct(fid)))
3534                 /* Quota files can't be created from the kernel any more,
3535                  * 'tune2fs -O quota' will take care of creating them */
3536                 RETURN(-EPERM);
3537
3538         result = __osd_object_create(info, obj, attr, hint, dof, th);
3539         if (result == 0) {
3540                 if (fid_is_idif(fid) &&
3541                     !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3542                         struct lu_fid *tfid = &info->oti_fid;
3543                         struct ost_id *oi   = &info->oti_ostid;
3544
3545                         fid_to_ostid(fid, oi);
3546                         ostid_to_fid(tfid, oi, 0);
3547                         on_ost = 1;
3548                         result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3549                                                 LMAC_FID_ON_OST, 0);
3550                 } else {
3551                         on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3552                                                fid, OI_CHECK_FLD);
3553                         result = osd_ea_fid_set(info, obj->oo_inode, fid,
3554                                                 on_ost ? LMAC_FID_ON_OST : 0,
3555                                                 0);
3556                 }
3557                 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3558                         obj->oo_dt.do_body_ops = &osd_body_ops;
3559         }
3560
3561         if (result == 0)
3562                 result = __osd_oi_insert(env, obj, fid, th);
3563
3564         /* a small optimization - dt_insert() isn't usually applied
3565          * to OST objects, so we don't need to cache OI mapping for
3566          * OST objects */
3567         if (result == 0 && on_ost == 0) {
3568                 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3569                 result = osd_idc_find_and_init(env, osd, obj);
3570                 LASSERT(result == 0);
3571         }
3572
3573         LASSERT(ergo(result == 0,
3574                      dt_object_exists(dt) && !dt_object_remote(dt)));
3575         LINVRNT(osd_invariant(obj));
3576         RETURN(result);
3577 }
3578
3579 static int osd_declare_object_ref_add(const struct lu_env *env,
3580                                       struct dt_object *dt,
3581                                       struct thandle *handle)
3582 {
3583         struct osd_thandle       *oh;
3584
3585         /* it's possible that object doesn't exist yet */
3586         LASSERT(handle != NULL);
3587
3588         oh = container_of0(handle, struct osd_thandle, ot_super);
3589         LASSERT(oh->ot_handle == NULL);
3590
3591         osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3592                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3593
3594         return 0;
3595 }
3596
3597 /*
3598  * Concurrency: @dt is write locked.
3599  */
3600 static int osd_object_ref_add(const struct lu_env *env,
3601                               struct dt_object *dt, struct thandle *th)
3602 {
3603         struct osd_object  *obj = osd_dt_obj(dt);
3604         struct inode       *inode = obj->oo_inode;
3605         struct osd_thandle *oh;
3606         int                 rc = 0;
3607
3608         if (!dt_object_exists(dt) || obj->oo_destroyed)
3609                 return -ENOENT;
3610
3611         LINVRNT(osd_invariant(obj));
3612         LASSERT(!dt_object_remote(dt));
3613         LASSERT(osd_write_locked(env, obj));
3614         LASSERT(th != NULL);
3615
3616         oh = container_of0(th, struct osd_thandle, ot_super);
3617         LASSERT(oh->ot_handle != NULL);
3618
3619         osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3620
3621         CDEBUG(D_INODE, DFID" increase nlink %d\n",
3622                PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3623         /*
3624          * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3625          * (65000) subdirectories by storing "1" in i_nlink if the link count
3626          * would otherwise overflow. Directory tranversal tools understand
3627          * that (st_nlink == 1) indicates that the filesystem dose not track
3628          * hard links count on the directory, and will not abort subdirectory
3629          * scanning early once (st_nlink - 2) subdirs have been found.
3630          *
3631          * This also has to properly handle the case of inodes with nlink == 0
3632          * in case they are being linked into the PENDING directory
3633          */
3634         spin_lock(&obj->oo_guard);
3635         if (unlikely(inode->i_nlink == 0))
3636                 /* inc_nlink from 0 may cause WARN_ON */
3637                 set_nlink(inode, 1);
3638         else {
3639                 ldiskfs_inc_count(oh->ot_handle, inode);
3640                 if (!S_ISDIR(inode->i_mode))
3641                         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3642         }
3643         spin_unlock(&obj->oo_guard);
3644
3645         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3646         LINVRNT(osd_invariant(obj));
3647
3648         osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3649
3650         return rc;
3651 }
3652
3653 static int osd_declare_object_ref_del(const struct lu_env *env,
3654                                       struct dt_object *dt,
3655                                       struct thandle *handle)
3656 {
3657         struct osd_thandle *oh;
3658
3659         if (!dt_object_exists(dt))
3660                 return -ENOENT;
3661
3662         LASSERT(!dt_object_remote(dt));
3663         LASSERT(handle != NULL);
3664
3665         oh = container_of0(handle, struct osd_thandle, ot_super);
3666         LASSERT(oh->ot_handle == NULL);
3667
3668         osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3669                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3670
3671         return 0;
3672 }
3673
3674 /*
3675  * Concurrency: @dt is write locked.
3676  */
3677 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3678                               struct thandle *th)
3679 {
3680         struct osd_object       *obj = osd_dt_obj(dt);
3681         struct inode            *inode = obj->oo_inode;
3682         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
3683         struct osd_thandle      *oh;
3684
3685         if (!dt_object_exists(dt))
3686                 return -ENOENT;
3687
3688         LINVRNT(osd_invariant(obj));
3689         LASSERT(!dt_object_remote(dt));
3690         LASSERT(osd_write_locked(env, obj));
3691         LASSERT(th != NULL);
3692
3693         oh = container_of0(th, struct osd_thandle, ot_super);
3694         LASSERT(oh->ot_handle != NULL);
3695
3696         osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3697
3698         spin_lock(&obj->oo_guard);
3699         /* That can be result of upgrade from old Lustre version and
3700          * applied only to local files.  Just skip this ref_del call.
3701          * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3702         if (inode->i_nlink == 0) {
3703                 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3704                              D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3705                              ", maybe an upgraded file? (LU-3915)\n",
3706                              osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3707                 spin_unlock(&obj->oo_guard);
3708                 return 0;
3709         }
3710
3711         CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3712                PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3713
3714         ldiskfs_dec_count(oh->ot_handle, inode);
3715         spin_unlock(&obj->oo_guard);
3716
3717         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3718         LINVRNT(osd_invariant(obj));
3719
3720         osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3721
3722         return 0;
3723 }
3724
3725 /*
3726  * Get the 64-bit version for an inode.
3727  */
3728 static int osd_object_version_get(const struct lu_env *env,
3729                                   struct dt_object *dt, dt_obj_version_t *ver)
3730 {
3731         struct inode *inode = osd_dt_obj(dt)->oo_inode;
3732
3733         CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3734                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3735         *ver = LDISKFS_I(inode)->i_fs_version;
3736         return 0;
3737 }
3738
3739 /*
3740  * Concurrency: @dt is read locked.
3741  */
3742 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3743                          struct lu_buf *buf, const char *name)
3744 {
3745         struct osd_object      *obj    = osd_dt_obj(dt);
3746         struct inode           *inode  = obj->oo_inode;
3747         struct osd_thread_info *info   = osd_oti_get(env);
3748         struct dentry          *dentry = &info->oti_obj_dentry;
3749         bool                    cache_xattr = false;
3750         int                     rc;
3751
3752         /* version get is not real XATTR but uses xattr API */
3753         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3754                 /* for version we are just using xattr API but change inode
3755                  * field instead */
3756                 if (buf->lb_len == 0)
3757                         return sizeof(dt_obj_version_t);
3758
3759                 if (buf->lb_len < sizeof(dt_obj_version_t))
3760                         return -ERANGE;
3761
3762                 osd_object_version_get(env, dt, buf->lb_buf);
3763
3764                 return sizeof(dt_obj_version_t);
3765         }
3766
3767         if (!dt_object_exists(dt))
3768                 return -ENOENT;
3769
3770         LASSERT(!dt_object_remote(dt));
3771         LASSERT(inode->i_op != NULL);
3772         LASSERT(inode->i_op->getxattr != NULL);
3773
3774         if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3775             strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3776                 cache_xattr = true;
3777
3778         if (cache_xattr) {
3779                 rc = osd_oxc_get(obj, name, buf);
3780                 if (rc != -ENOENT)
3781                         return rc;
3782         }
3783
3784         rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3785         if (cache_xattr) {
3786                 if (rc == -ENOENT || rc == -ENODATA)
3787                         osd_oxc_add(obj, name, NULL, 0);
3788                 else if (rc > 0 && buf->lb_buf != NULL)
3789                         osd_oxc_add(obj, name, buf->lb_buf, rc);
3790         }
3791
3792         return rc;
3793 }
3794
3795 static int osd_declare_xattr_set(const struct lu_env *env,
3796                                  struct dt_object *dt,
3797                                  const struct lu_buf *buf, const char *name,
3798                                  int fl, struct thandle *handle)
3799 {
3800         struct osd_thandle *oh;
3801         int credits;
3802         struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3803
3804         LASSERT(handle != NULL);
3805
3806         oh = container_of0(handle, struct osd_thandle, ot_super);
3807         LASSERT(oh->ot_handle == NULL);
3808
3809         if (strcmp(name, XATTR_NAME_LMA) == 0) {
3810                 /* For non-upgrading case, the LMA is set first and
3811                  * usually fit inode. But for upgrade case, the LMA
3812                  * may be in another separated EA block. */
3813                 if (!dt_object_exists(dt))
3814                         credits = 0;
3815                 else if (fl == LU_XATTR_REPLACE)
3816                         credits = 1;
3817                 else
3818                         goto upgrade;
3819         } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3820                 credits = 1;
3821         } else {
3822 upgrade:
3823                 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3824
3825                 if (buf != NULL) {
3826                         ssize_t buflen;
3827
3828                         if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3829                                 /* learn xattr size from osd_xattr_get if
3830                                    attribute has not been read yet */
3831                                 buflen = __osd_xattr_get(
3832                                     osd_dt_obj(dt)->oo_inode,
3833                                     &osd_oti_get(env)->oti_obj_dentry,
3834                                     name, NULL, 0);
3835                                 if (buflen < 0)
3836                                         buflen = 0;
3837                         } else {
3838                                 buflen = buf->lb_len;
3839                         }
3840
3841                         if (buflen > sb->s_blocksize) {
3842                                 credits += osd_calc_bkmap_credits(
3843                                     sb, NULL, 0, -1,
3844                                     (buflen + sb->s_blocksize - 1) >>
3845                                     sb->s_blocksize_bits);
3846                         }
3847                 }
3848                 /*
3849                  * xattr set may involve inode quota change, reserve credits for
3850                  * dquot_initialize()
3851                  */
3852                 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3853         }
3854
3855         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3856
3857         return 0;
3858 }
3859
3860 /*
3861  * Set the 64-bit version for object
3862  */
3863 static void osd_object_version_set(const struct lu_env *env,
3864                                    struct dt_object *dt,
3865                                    dt_obj_version_t *new_version)
3866 {
3867         struct inode *inode = osd_dt_obj(dt)->oo_inode;
3868
3869         CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3870                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3871
3872         LDISKFS_I(inode)->i_fs_version = *new_version;
3873         /** Version is set after all inode operations are finished,
3874          *  so we should mark it dirty here */
3875         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3876 }
3877
3878 /*
3879  * Concurrency: @dt is write locked.
3880  */
3881 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3882                          const struct lu_buf *buf, const char *name, int fl,
3883                          struct thandle *handle)
3884 {
3885         struct osd_object      *obj      = osd_dt_obj(dt);
3886         struct inode           *inode    = obj->oo_inode;
3887         struct osd_thread_info *info     = osd_oti_get(env);
3888         int                     fs_flags = 0;
3889         int                     rc;
3890         ENTRY;
3891
3892         LASSERT(handle != NULL);
3893
3894         /* version set is not real XATTR */
3895         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3896                 /* for version we are just using xattr API but change inode
3897                  * field instead */
3898                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3899                 osd_object_version_set(env, dt, buf->lb_buf);
3900                 return sizeof(dt_obj_version_t);
3901         }
3902
3903         CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3904                PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3905
3906         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3907         if (fl & LU_XATTR_REPLACE)
3908                 fs_flags |= XATTR_REPLACE;
3909
3910         if (fl & LU_XATTR_CREATE)
3911                 fs_flags |= XATTR_CREATE;
3912
3913         if (strcmp(name, XATTR_NAME_LMV) == 0) {
3914                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3915
3916                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3917                 if (rc != 0)
3918                         RETURN(rc);
3919
3920                 lma->lma_incompat |= LMAI_STRIPED;
3921                 lustre_lma_swab(lma);
3922                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3923                                      sizeof(*lma), XATTR_REPLACE);
3924                 if (rc != 0)
3925                         RETURN(rc);
3926         }
3927
3928         rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3929                                fs_flags);
3930         osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3931
3932         if (rc == 0 &&
3933             (strcmp(name, XATTR_NAME_LOV) == 0 ||
3934              strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3935                 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3936
3937         return rc;
3938 }
3939
3940 /*
3941  * Concurrency: @dt is read locked.
3942  */
3943 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3944                           const struct lu_buf *buf)
3945 {
3946         struct osd_object      *obj    = osd_dt_obj(dt);
3947         struct inode           *inode  = obj->oo_inode;
3948         struct osd_thread_info *info   = osd_oti_get(env);
3949         struct dentry          *dentry = &info->oti_obj_dentry;
3950
3951         if (!dt_object_exists(dt))
3952                 return -ENOENT;
3953
3954         LASSERT(!dt_object_remote(dt));
3955         LASSERT(inode->i_op != NULL);
3956         LASSERT(inode->i_op->listxattr != NULL);
3957
3958         dentry->d_inode = inode;
3959         dentry->d_sb = inode->i_sb;
3960         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3961 }
3962
3963 static int osd_declare_xattr_del(const struct lu_env *env,
3964                                  struct dt_object *dt, const char *name,
3965                                  struct thandle *handle)
3966 {
3967         struct osd_thandle *oh;
3968         struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3969
3970         LASSERT(!dt_object_remote(dt));
3971         LASSERT(handle != NULL);
3972
3973         oh = container_of0(handle, struct osd_thandle, ot_super);
3974         LASSERT(oh->ot_handle == NULL);
3975
3976         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3977                              osd_dto_credits_noquota[DTO_XATTR_SET]);
3978         /*
3979          * xattr del may involve inode quota change, reserve credits for
3980          * dquot_initialize()
3981          */
3982         oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3983
3984         return 0;
3985 }
3986
3987 /*
3988  * Concurrency: @dt is write locked.
3989  */
3990 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3991                          const char *name, struct thandle *handle)
3992 {
3993         struct osd_object      *obj    = osd_dt_obj(dt);
3994         struct inode           *inode  = obj->oo_inode;
3995         struct osd_thread_info *info   = osd_oti_get(env);
3996         struct dentry          *dentry = &info->oti_obj_dentry;
3997         int                     rc;
3998
3999         if (!dt_object_exists(dt))
4000                 return -ENOENT;
4001
4002         LASSERT(!dt_object_remote(dt));
4003         LASSERT(inode->i_op != NULL);
4004         LASSERT(inode->i_op->removexattr != NULL);
4005         LASSERT(handle != NULL);
4006
4007         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4008
4009         ll_vfs_dq_init(inode);
4010         dentry->d_inode = inode;
4011         dentry->d_sb = inode->i_sb;
4012         rc = inode->i_op->removexattr(dentry, name);
4013         osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4014
4015         if (rc == 0 &&
4016             (strcmp(name, XATTR_NAME_LOV) == 0 ||
4017              strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4018                 osd_oxc_del(obj, name);
4019
4020         return rc;
4021 }
4022
4023 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4024                            __u64 start, __u64 end)
4025 {
4026         struct osd_object       *obj    = osd_dt_obj(dt);
4027         struct inode            *inode  = obj->oo_inode;
4028         struct osd_thread_info  *info   = osd_oti_get(env);
4029         struct dentry           *dentry = &info->oti_obj_dentry;
4030         struct file             *file   = &info->oti_file;
4031         int                     rc;
4032
4033         ENTRY;
4034
4035         dentry->d_inode = inode;
4036         dentry->d_sb = inode->i_sb;
4037         file->f_path.dentry = dentry;
4038         file->f_mapping = inode->i_mapping;
4039         file->f_op = inode->i_fop;
4040         set_file_inode(file, inode);
4041
4042         rc = ll_vfs_fsync_range(file, start, end, 0);
4043
4044         RETURN(rc);
4045 }
4046
4047 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4048 {
4049         return 0;
4050 }
4051
4052 /*
4053  * Index operations.
4054  */
4055
4056 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4057                            const struct dt_index_features *feat)
4058 {
4059         struct iam_descr *descr;
4060
4061         if (osd_object_is_root(o))
4062                 return feat == &dt_directory_features;
4063
4064         LASSERT(o->oo_dir != NULL);
4065
4066         descr = o->oo_dir->od_container.ic_descr;
4067         if (feat == &dt_directory_features) {
4068                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4069                         return 1;
4070                 else
4071                         return 0;
4072         } else {
4073                 return
4074                         feat->dif_keysize_min <= descr->id_key_size &&
4075                         descr->id_key_size <= feat->dif_keysize_max &&
4076                         feat->dif_recsize_min <= descr->id_rec_size &&
4077                         descr->id_rec_size <= feat->dif_recsize_max &&
4078                         !(feat->dif_flags & (DT_IND_VARKEY |
4079                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
4080                         ergo(feat->dif_flags & DT_IND_UPDATE,
4081                              1 /* XXX check that object (and file system) is
4082                                 * writable */);
4083         }
4084 }
4085
4086 static int osd_iam_container_init(const struct lu_env *env,
4087                                   struct osd_object *obj,
4088                                   struct osd_directory *dir)
4089 {
4090         struct iam_container *bag = &dir->od_container;
4091         int result;
4092
4093         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4094         if (result != 0)
4095                 return result;
4096
4097         result = iam_container_setup(bag);
4098         if (result == 0)
4099                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4100         else
4101                 iam_container_fini(bag);
4102
4103         return result;
4104 }
4105
4106
4107 /*
4108  * Concurrency: no external locking is necessary.
4109  */
4110 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4111                          const struct dt_index_features *feat)
4112 {
4113         int                      result;
4114         int                      skip_iam = 0;
4115         struct osd_object       *obj = osd_dt_obj(dt);
4116
4117         LINVRNT(osd_invariant(obj));
4118
4119         if (osd_object_is_root(obj)) {
4120                 dt->do_index_ops = &osd_index_ea_ops;
4121                 result = 0;
4122         } else if (feat == &dt_directory_features) {
4123                 dt->do_index_ops = &osd_index_ea_ops;
4124                 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4125                         result = 0;
4126                 else
4127                         result = -ENOTDIR;
4128                 skip_iam = 1;
4129         } else if (unlikely(feat == &dt_otable_features)) {
4130                 dt->do_index_ops = &osd_otable_ops;
4131                 return 0;
4132         } else if (unlikely(feat == &dt_acct_features)) {
4133                 dt->do_index_ops = &osd_acct_index_ops;
4134                 result = 0;
4135                 skip_iam = 1;
4136         } else if (!osd_has_index(obj)) {
4137                 struct osd_directory *dir;
4138
4139                 OBD_ALLOC_PTR(dir);
4140                 if (dir != NULL) {
4141
4142                         spin_lock(&obj->oo_guard);
4143                         if (obj->oo_dir == NULL)
4144                                 obj->oo_dir = dir;
4145                         else
4146                                 /*
4147                                  * Concurrent thread allocated container data.
4148                                  */
4149                                 OBD_FREE_PTR(dir);
4150                         spin_unlock(&obj->oo_guard);
4151                         /*
4152                          * Now, that we have container data, serialize its
4153                          * initialization.
4154                          */
4155                         down_write(&obj->oo_ext_idx_sem);
4156                         /*
4157                          * recheck under lock.
4158                          */
4159                         if (!osd_has_index(obj))
4160                                 result = osd_iam_container_init(env, obj,
4161                                                                 obj->oo_dir);
4162                         else
4163                                 result = 0;
4164                         up_write(&obj->oo_ext_idx_sem);
4165                 } else {
4166                         result = -ENOMEM;
4167                 }
4168         } else {
4169                 result = 0;
4170         }
4171
4172         if (result == 0 && skip_iam == 0) {
4173                 if (!osd_iam_index_probe(env, obj, feat))
4174                         result = -ENOTDIR;
4175         }
4176         LINVRNT(osd_invariant(obj));
4177
4178         if (result == 0 && feat == &dt_quota_glb_features &&
4179             fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4180                 result = osd_quota_migration(env, dt);
4181
4182         return result;
4183 }
4184
4185 static int osd_otable_it_attr_get(const struct lu_env *env,
4186                                  struct dt_object *dt,
4187                                  struct lu_attr *attr)
4188 {
4189         attr->la_valid = 0;
4190         return 0;
4191 }
4192
4193 static const struct dt_object_operations osd_obj_ops = {
4194         .do_read_lock         = osd_object_read_lock,
4195         .do_write_lock        = osd_object_write_lock,
4196         .do_read_unlock       = osd_object_read_unlock,
4197         .do_write_unlock      = osd_object_write_unlock,
4198         .do_write_locked      = osd_object_write_locked,
4199         .do_attr_get          = osd_attr_get,
4200         .do_declare_attr_set  = osd_declare_attr_set,
4201         .do_attr_set          = osd_attr_set,
4202         .do_ah_init           = osd_ah_init,
4203         .do_declare_create    = osd_declare_object_create,
4204         .do_create            = osd_object_create,
4205         .do_declare_destroy   = osd_declare_object_destroy,
4206         .do_destroy           = osd_object_destroy,
4207         .do_index_try         = osd_index_try,
4208         .do_declare_ref_add   = osd_declare_object_ref_add,
4209         .do_ref_add           = osd_object_ref_add,
4210         .do_declare_ref_del   = osd_declare_object_ref_del,
4211         .do_ref_del           = osd_object_ref_del,
4212         .do_xattr_get         = osd_xattr_get,
4213         .do_declare_xattr_set = osd_declare_xattr_set,
4214         .do_xattr_set         = osd_xattr_set,
4215         .do_declare_xattr_del = osd_declare_xattr_del,
4216         .do_xattr_del         = osd_xattr_del,
4217         .do_xattr_list        = osd_xattr_list,
4218         .do_object_sync       = osd_object_sync,
4219         .do_invalidate        = osd_invalidate,
4220 };
4221
4222 /**
4223  * dt_object_operations for interoperability mode
4224  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4225  */
4226 static const struct dt_object_operations osd_obj_ea_ops = {
4227         .do_read_lock         = osd_object_read_lock,
4228         .do_write_lock        = osd_object_write_lock,
4229         .do_read_unlock       = osd_object_read_unlock,
4230         .do_write_unlock      = osd_object_write_unlock,
4231         .do_write_locked      = osd_object_write_locked,
4232         .do_attr_get          = osd_attr_get,
4233         .do_declare_attr_set  = osd_declare_attr_set,
4234         .do_attr_set          = osd_attr_set,
4235         .do_ah_init           = osd_ah_init,
4236         .do_declare_create    = osd_declare_object_create,
4237         .do_create            = osd_object_ea_create,
4238         .do_declare_destroy   = osd_declare_object_destroy,
4239         .do_destroy           = osd_object_destroy,
4240         .do_index_try         = osd_index_try,
4241         .do_declare_ref_add   = osd_declare_object_ref_add,
4242         .do_ref_add           = osd_object_ref_add,
4243         .do_declare_ref_del   = osd_declare_object_ref_del,
4244         .do_ref_del           = osd_object_ref_del,
4245         .do_xattr_get         = osd_xattr_get,
4246         .do_declare_xattr_set = osd_declare_xattr_set,
4247         .do_xattr_set         = osd_xattr_set,
4248         .do_declare_xattr_del = osd_declare_xattr_del,
4249         .do_xattr_del         = osd_xattr_del,
4250         .do_xattr_list        = osd_xattr_list,
4251         .do_object_sync       = osd_object_sync,
4252         .do_invalidate        = osd_invalidate,
4253 };
4254
4255 static const struct dt_object_operations osd_obj_otable_it_ops = {
4256         .do_attr_get    = osd_otable_it_attr_get,
4257         .do_index_try   = osd_index_try,
4258 };
4259
4260 static int osd_index_declare_iam_delete(const struct lu_env *env,
4261                                         struct dt_object *dt,
4262                                         const struct dt_key *key,
4263                                         struct thandle *handle)
4264 {
4265         struct osd_thandle    *oh;
4266
4267         oh = container_of0(handle, struct osd_thandle, ot_super);
4268         LASSERT(oh->ot_handle == NULL);
4269
4270         /* Recycle  may cause additional three blocks to be changed. */
4271         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4272                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4273
4274         return 0;
4275 }
4276
4277 /**
4278  *      delete a (key, value) pair from index \a dt specified by \a key
4279  *
4280  *      \param  dt      osd index object
4281  *      \param  key     key for index
4282  *      \param  rec     record reference
4283  *      \param  handle  transaction handler
4284  *
4285  *      \retval  0  success
4286  *      \retval -ve   failure
4287  */
4288 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4289                                 const struct dt_key *key,
4290                                 struct thandle *handle)
4291 {
4292         struct osd_thread_info *oti = osd_oti_get(env);
4293         struct osd_object      *obj = osd_dt_obj(dt);
4294         struct osd_thandle     *oh;
4295         struct iam_path_descr  *ipd;
4296         struct iam_container   *bag = &obj->oo_dir->od_container;
4297         int                     rc;
4298         ENTRY;
4299
4300         if (!dt_object_exists(dt))
4301                 RETURN(-ENOENT);
4302
4303         LINVRNT(osd_invariant(obj));
4304         LASSERT(!dt_object_remote(dt));
4305         LASSERT(bag->ic_object == obj->oo_inode);
4306         LASSERT(handle != NULL);
4307
4308         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4309
4310         ipd = osd_idx_ipd_get(env, bag);
4311         if (unlikely(ipd == NULL))
4312                 RETURN(-ENOMEM);
4313
4314         oh = container_of0(handle, struct osd_thandle, ot_super);
4315         LASSERT(oh->ot_handle != NULL);
4316         LASSERT(oh->ot_handle->h_transaction != NULL);
4317
4318         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4319                 /* swab quota uid/gid provided by caller */
4320                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4321                 key = (const struct dt_key *)&oti->oti_quota_id;
4322         }
4323
4324         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4325         osd_ipd_put(env, bag, ipd);
4326         LINVRNT(osd_invariant(obj));
4327         osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4328         RETURN(rc);
4329 }
4330
4331 static int osd_index_declare_ea_delete(const struct lu_env *env,
4332                                        struct dt_object *dt,
4333                                        const struct dt_key *key,
4334                                        struct thandle *handle)
4335 {
4336         struct osd_thandle *oh;
4337         struct inode       *inode;
4338         int                 rc, credits;
4339         ENTRY;
4340
4341         LASSERT(!dt_object_remote(dt));
4342         LASSERT(handle != NULL);
4343
4344         oh = container_of0(handle, struct osd_thandle, ot_super);
4345         LASSERT(oh->ot_handle == NULL);
4346
4347         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4348         if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4349                 /* '..' to a remote object has a local representative */
4350                 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4351                 /* to reset LMAI_REMOTE_PARENT */
4352                 credits += 1;
4353         }
4354         osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4355
4356         inode = osd_dt_obj(dt)->oo_inode;
4357         if (inode == NULL)
4358                 RETURN(-ENOENT);
4359
4360         rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4361                                    0, oh, osd_dt_obj(dt), true, NULL, false);
4362         RETURN(rc);
4363 }
4364
4365 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4366                                           struct dt_rec *fid)
4367 {
4368         struct osd_fid_pack *rec;
4369         int                  rc = -ENODATA;
4370
4371         if (de->file_type & LDISKFS_DIRENT_LUFID) {
4372                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4373                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4374                 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4375                         rc = -EINVAL;
4376         }
4377         return rc;
4378 }
4379
4380 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4381                           const struct lu_fid *fid)
4382 {
4383         struct seq_server_site  *ss = osd_seq_site(osd);
4384         ENTRY;
4385
4386         /* FID seqs not in FLDB, must be local seq */
4387         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4388                 RETURN(0);
4389
4390         /* If FLD is not being initialized yet, it only happens during the
4391          * initialization, likely during mgs initialization, and we assume
4392          * this is local FID. */
4393         if (ss == NULL || ss->ss_server_fld == NULL)
4394                 RETURN(0);
4395
4396         /* Only check the local FLDB here */
4397         if (osd_seq_exists(env, osd, fid_seq(fid)))
4398                 RETURN(0);
4399
4400         RETURN(1);
4401 }
4402
4403 /**
4404  * Index delete function for interoperability mode (b11826).
4405  * It will remove the directory entry added by osd_index_ea_insert().
4406  * This entry is needed to maintain name->fid mapping.
4407  *
4408  * \param key,  key i.e. file entry to be deleted
4409  *
4410  * \retval   0, on success
4411  * \retval -ve, on error
4412  */
4413 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4414                                const struct dt_key *key, struct thandle *handle)
4415 {
4416         struct osd_object          *obj = osd_dt_obj(dt);
4417         struct inode               *dir = obj->oo_inode;
4418         struct dentry              *dentry;
4419         struct osd_thandle         *oh;
4420         struct ldiskfs_dir_entry_2 *de = NULL;
4421         struct buffer_head         *bh;
4422         struct htree_lock          *hlock = NULL;
4423         struct lu_fid              *fid = &osd_oti_get(env)->oti_fid;
4424         struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
4425         int                        rc;
4426         ENTRY;
4427
4428         if (!dt_object_exists(dt))
4429                 RETURN(-ENOENT);
4430
4431         LINVRNT(osd_invariant(obj));
4432         LASSERT(!dt_object_remote(dt));
4433         LASSERT(handle != NULL);
4434
4435         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4436
4437         oh = container_of(handle, struct osd_thandle, ot_super);
4438         LASSERT(oh->ot_handle != NULL);
4439         LASSERT(oh->ot_handle->h_transaction != NULL);
4440
4441         ll_vfs_dq_init(dir);
4442         dentry = osd_child_dentry_get(env, obj,
4443                                       (char *)key, strlen((char *)key));
4444
4445         if (obj->oo_hl_head != NULL) {
4446                 hlock = osd_oti_get(env)->oti_hlock;
4447                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4448                                    dir, LDISKFS_HLOCK_DEL);
4449         } else {
4450                 down_write(&obj->oo_ext_idx_sem);
4451         }
4452
4453         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4454         if (!IS_ERR(bh)) {
4455                 /* If this is not the ".." entry, it might be a remote DNE
4456                  * entry and  we need to check if the FID is for a remote
4457                  * MDT.  If the FID is  not in the directory entry (e.g.
4458                  * upgraded 1.8 filesystem without dirdata enabled) then
4459                  * we need to get the FID from the LMA. For a remote directory
4460                  * there HAS to be an LMA, it cannot be an IGIF inode in this
4461                  * case.
4462                  *
4463                  * Delete the entry before the agent inode in order to
4464                  * simplify error handling.  At worst an error after deleting
4465                  * the entry first might leak the agent inode afterward. The
4466                  * reverse would need filesystem abort in case of error deleting
4467                  * the entry after the agent had been removed, or leave a
4468                  * dangling entry pointing at a random inode. */
4469                 if (strcmp((char *)key, dotdot) != 0) {
4470                         LASSERT(de != NULL);
4471                         rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4472                         if (rc == -ENODATA) {
4473                                 /* can't get FID, postpone to the end of the
4474                                  * transaction when iget() is safe */
4475                                 osd_schedule_agent_inode_removal(env, oh,
4476                                                 le32_to_cpu(de->inode));
4477                         } else if (rc == 0 &&
4478                                    unlikely(osd_remote_fid(env, osd, fid))) {
4479                                 osd_schedule_agent_inode_removal(env, oh,
4480                                                 le32_to_cpu(de->inode));
4481                         }
4482                 }
4483                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4484                 brelse(bh);
4485         } else {
4486                 rc = PTR_ERR(bh);
4487         }
4488         if (hlock != NULL)
4489                 ldiskfs_htree_unlock(hlock);
4490         else
4491                 up_write(&obj->oo_ext_idx_sem);
4492
4493         if (rc != 0)
4494                 GOTO(out, rc);
4495
4496         /* For inode on the remote MDT, .. will point to
4497          * /Agent directory, Check whether it needs to delete
4498          * from agent directory */
4499         if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4500                 int ret;
4501
4502                 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4503                                                     obj, oh);
4504                 if (ret != 0)
4505                         /* Sigh, the entry has been deleted, and
4506                          * it is not easy to revert it back, so
4507                          * let's keep this error private, and let
4508                          * LFSCK fix it. XXX */
4509                         CERROR("%s: delete remote parent "DFID": rc = %d\n",
4510                                osd_name(osd), PFID(fid), ret);
4511         }
4512 out:
4513         LASSERT(osd_invariant(obj));
4514         osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4515         RETURN(rc);
4516 }
4517
4518 /**
4519  *      Lookup index for \a key and copy record to \a rec.
4520  *
4521  *      \param  dt      osd index object
4522  *      \param  key     key for index
4523  *      \param  rec     record reference
4524  *
4525  *      \retval  +ve  success : exact mach
4526  *      \retval  0    return record with key not greater than \a key
4527  *      \retval -ve   failure
4528  */
4529 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4530                                 struct dt_rec *rec, const struct dt_key *key)
4531 {
4532         struct osd_object      *obj = osd_dt_obj(dt);
4533         struct iam_path_descr  *ipd;
4534         struct iam_container   *bag = &obj->oo_dir->od_container;
4535         struct osd_thread_info *oti = osd_oti_get(env);
4536         struct iam_iterator    *it = &oti->oti_idx_it;
4537         struct iam_rec         *iam_rec;
4538         int                     rc;
4539         ENTRY;
4540
4541         if (!dt_object_exists(dt))
4542                 RETURN(-ENOENT);
4543
4544         LASSERT(osd_invariant(obj));
4545         LASSERT(!dt_object_remote(dt));
4546         LASSERT(bag->ic_object == obj->oo_inode);
4547
4548         ipd = osd_idx_ipd_get(env, bag);
4549         if (IS_ERR(ipd))
4550                 RETURN(-ENOMEM);
4551
4552         /* got ipd now we can start iterator. */
4553         iam_it_init(it, bag, 0, ipd);
4554
4555         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4556                 /* swab quota uid/gid provided by caller */
4557                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4558                 key = (const struct dt_key *)&oti->oti_quota_id;
4559         }
4560
4561         rc = iam_it_get(it, (struct iam_key *)key);
4562         if (rc >= 0) {
4563                 if (S_ISDIR(obj->oo_inode->i_mode))
4564                         iam_rec = (struct iam_rec *)oti->oti_ldp;
4565                 else
4566                         iam_rec = (struct iam_rec *) rec;
4567
4568                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4569
4570                 if (S_ISDIR(obj->oo_inode->i_mode))
4571                         osd_fid_unpack((struct lu_fid *) rec,
4572                                        (struct osd_fid_pack *)iam_rec);
4573                 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4574                         osd_quota_unpack(obj, rec);
4575         }
4576
4577         iam_it_put(it);
4578         iam_it_fini(it);
4579         osd_ipd_put(env, bag, ipd);
4580
4581         LINVRNT(osd_invariant(obj));
4582
4583         RETURN(rc);
4584 }
4585
4586 static int osd_index_declare_iam_insert(const struct lu_env *env,
4587                                         struct dt_object *dt,
4588                                         const struct dt_rec *rec,
4589                                         const struct dt_key *key,
4590                                         struct thandle *handle)
4591 {
4592         struct osd_thandle *oh;
4593
4594         LASSERT(handle != NULL);
4595
4596         oh = container_of0(handle, struct osd_thandle, ot_super);
4597         LASSERT(oh->ot_handle == NULL);
4598
4599         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4600                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4601
4602         return 0;
4603 }
4604
4605 /**
4606  *      Inserts (key, value) pair in \a dt index object.
4607  *
4608  *      \param  dt      osd index object
4609  *      \param  key     key for index
4610  *      \param  rec     record reference
4611  *      \param  th      transaction handler
4612  *
4613  *      \retval  0  success
4614  *      \retval -ve failure
4615  */
4616 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4617                                 const struct dt_rec *rec,
4618                                 const struct dt_key *key, struct thandle *th,
4619                                 int ignore_quota)
4620 {
4621         struct osd_object     *obj = osd_dt_obj(dt);
4622         struct iam_path_descr *ipd;
4623         struct osd_thandle    *oh;
4624         struct iam_container  *bag;
4625         struct osd_thread_info *oti = osd_oti_get(env);
4626         struct iam_rec         *iam_rec;
4627         int                     rc;
4628         ENTRY;
4629
4630         if (!dt_object_exists(dt))
4631                 RETURN(-ENOENT);
4632
4633         LINVRNT(osd_invariant(obj));
4634         LASSERT(!dt_object_remote(dt));
4635
4636         bag = &obj->oo_dir->od_container;
4637         LASSERT(bag->ic_object == obj->oo_inode);
4638         LASSERT(th != NULL);
4639
4640         osd_trans_exec_op(env, th, OSD_OT_INSERT);
4641
4642         ipd = osd_idx_ipd_get(env, bag);
4643         if (unlikely(ipd == NULL))
4644                 RETURN(-ENOMEM);
4645
4646         oh = container_of0(th, struct osd_thandle, ot_super);
4647         LASSERT(oh->ot_handle != NULL);
4648         LASSERT(oh->ot_handle->h_transaction != NULL);
4649         if (S_ISDIR(obj->oo_inode->i_mode)) {
4650                 iam_rec = (struct iam_rec *)oti->oti_ldp;
4651                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4652         } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4653                 /* pack quota uid/gid */
4654                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4655                 key = (const struct dt_key *)&oti->oti_quota_id;
4656                 /* pack quota record */
4657                 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4658                 iam_rec = (struct iam_rec *)rec;
4659         } else {
4660                 iam_rec = (struct iam_rec *)rec;
4661         }
4662
4663         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4664                         iam_rec, ipd);
4665         osd_ipd_put(env, bag, ipd);
4666         LINVRNT(osd_invariant(obj));
4667         osd_trans_exec_check(env, th, OSD_OT_INSERT);
4668         RETURN(rc);
4669 }
4670
4671 /**
4672  * Calls ldiskfs_add_entry() to add directory entry
4673  * into the directory. This is required for
4674  * interoperability mode (b11826)
4675  *
4676  * \retval   0, on success
4677  * \retval -ve, on error
4678  */
4679 static int __osd_ea_add_rec(struct osd_thread_info *info,
4680                             struct osd_object *pobj, struct inode  *cinode,
4681                             const char *name, const struct lu_fid *fid,
4682                             struct htree_lock *hlock, struct thandle *th)
4683 {
4684         struct ldiskfs_dentry_param *ldp;
4685         struct dentry               *child;
4686         struct osd_thandle          *oth;
4687         int                          rc;
4688
4689         oth = container_of(th, struct osd_thandle, ot_super);
4690         LASSERT(oth->ot_handle != NULL);
4691         LASSERT(oth->ot_handle->h_transaction != NULL);
4692         LASSERT(pobj->oo_inode);
4693
4694         ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4695         if (unlikely(pobj->oo_inode ==
4696                      osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4697                 ldp->edp_magic = 0;
4698         else
4699                 osd_get_ldiskfs_dirent_param(ldp, fid);
4700         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4701         child->d_fsdata = (void *)ldp;
4702         ll_vfs_dq_init(pobj->oo_inode);
4703         rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4704                                    child, cinode, hlock);
4705         if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4706                 struct ldiskfs_dir_entry_2      *de;
4707                 struct buffer_head              *bh;
4708                 int                              rc1;
4709
4710                 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4711                                             NULL, hlock);
4712                 if (!IS_ERR(bh)) {
4713                         rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4714                                                                bh);
4715                         if (rc1 == 0) {
4716                                 if (S_ISDIR(cinode->i_mode))
4717                                         de->file_type = LDISKFS_DIRENT_LUFID |
4718                                                         LDISKFS_FT_REG_FILE;
4719                                 else
4720                                         de->file_type = LDISKFS_DIRENT_LUFID |
4721                                                         LDISKFS_FT_DIR;
4722                                 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4723                                                               NULL, bh);
4724                         }
4725                         brelse(bh);
4726                 }
4727         }
4728
4729         RETURN(rc);
4730 }
4731
4732 /**
4733  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4734  * into the directory.Also sets flags into osd object to
4735  * indicate dot and dotdot are created. This is required for
4736  * interoperability mode (b11826)
4737  *
4738  * \param dir   directory for dot and dotdot fixup.
4739  * \param obj   child object for linking
4740  *
4741  * \retval   0, on success
4742  * \retval -ve, on error
4743  */
4744 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4745                               struct osd_object *dir,
4746                               struct inode *parent_dir, const char *name,
4747                               const struct lu_fid *dot_fid,
4748                               const struct lu_fid *dot_dot_fid,
4749                               struct thandle *th)
4750 {
4751         struct inode                *inode = dir->oo_inode;
4752         struct osd_thandle          *oth;
4753         int result = 0;
4754
4755         oth = container_of(th, struct osd_thandle, ot_super);
4756         LASSERT(oth->ot_handle->h_transaction != NULL);
4757         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4758
4759         if (strcmp(name, dot) == 0) {
4760                 if (dir->oo_compat_dot_created) {
4761                         result = -EEXIST;
4762                 } else {
4763                         LASSERT(inode->i_ino == parent_dir->i_ino);
4764                         dir->oo_compat_dot_created = 1;
4765                         result = 0;
4766                 }
4767         } else if (strcmp(name, dotdot) == 0) {
4768                 if (!dir->oo_compat_dot_created)
4769                         return -EINVAL;
4770                 /* in case of rename, dotdot is already created */
4771                 if (dir->oo_compat_dotdot_created) {
4772                         return __osd_ea_add_rec(info, dir, parent_dir, name,
4773                                                 dot_dot_fid, NULL, th);
4774                 }
4775
4776                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4777                         struct lu_fid tfid = *dot_dot_fid;
4778
4779                         tfid.f_oid--;
4780                         result = osd_add_dot_dotdot_internal(info,
4781                                         dir->oo_inode, parent_dir, dot_fid,
4782                                         &tfid, oth);
4783                 } else {
4784                         result = osd_add_dot_dotdot_internal(info,
4785                                         dir->oo_inode, parent_dir, dot_fid,
4786                                         dot_dot_fid, oth);
4787                 }
4788
4789                 if (result == 0)
4790                         dir->oo_compat_dotdot_created = 1;
4791         }
4792
4793         return result;
4794 }
4795
4796
4797 /**
4798  * It will call the appropriate osd_add* function and return the
4799  * value, return by respective functions.
4800  */
4801 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4802                           struct inode *cinode, const char *name,
4803                           const struct lu_fid *fid, struct thandle *th)
4804 {
4805         struct osd_thread_info *info   = osd_oti_get(env);
4806         struct htree_lock      *hlock;
4807         int                     rc;
4808
4809         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4810
4811         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4812                                                    name[2] =='\0'))) {
4813                 if (hlock != NULL) {
4814                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4815                                            pobj->oo_inode, 0);
4816                 } else {
4817                         down_write(&pobj->oo_ext_idx_sem);
4818                 }
4819
4820                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4821                                         lu_object_fid(&pobj->oo_dt.do_lu),
4822                                         fid, th);
4823         } else {
4824                 if (hlock != NULL) {
4825                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4826                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
4827                 } else {
4828                         down_write(&pobj->oo_ext_idx_sem);
4829                 }
4830
4831                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4832                         struct lu_fid *tfid = &info->oti_fid;
4833
4834                         *tfid = *fid;
4835                         tfid->f_ver = ~0;
4836                         rc = __osd_ea_add_rec(info, pobj, cinode, name,
4837                                               tfid, hlock, th);
4838                 } else {
4839                         rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4840                                               hlock, th);
4841                 }
4842         }
4843         if (hlock != NULL)
4844                 ldiskfs_htree_unlock(hlock);
4845         else
4846                 up_write(&pobj->oo_ext_idx_sem);
4847
4848         return rc;
4849 }
4850
4851 static int
4852 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4853                       struct osd_idmap_cache *oic)
4854 {
4855         struct osd_scrub *scrub = &dev->od_scrub;
4856         struct lu_fid *fid = &oic->oic_fid;
4857         struct osd_inode_id *id = &oic->oic_lid;
4858         struct inode *inode = NULL;
4859         int once  = 0;
4860         bool insert;
4861         int rc;
4862         ENTRY;
4863
4864         if (!fid_is_norm(fid) && !fid_is_igif(fid))
4865                 RETURN(0);
4866
4867         if (scrub->os_pos_current > id->oii_ino)
4868                 RETURN(0);
4869
4870 again:
4871         rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4872         if (rc == -ENOENT) {
4873                 __u32 gen = id->oii_gen;
4874
4875                 insert = true;
4876                 if (inode != NULL)
4877                         goto trigger;
4878
4879                 inode = osd_iget(oti, dev, id);
4880                 /* The inode has been removed (by race maybe). */
4881                 if (IS_ERR(inode)) {
4882                         rc = PTR_ERR(inode);
4883
4884                         RETURN(rc == -ESTALE ? -ENOENT : rc);
4885                 }
4886
4887                 /* The OI mapping is lost. */
4888                 if (gen != OSD_OII_NOGEN)
4889                         goto trigger;
4890
4891                 iput(inode);
4892                 /* The inode may has been reused by others, we do not know,
4893                  * leave it to be handled by subsequent osd_fid_lookup(). */
4894                 RETURN(0);
4895         } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4896                 RETURN(rc);
4897         } else {
4898                 insert = false;
4899         }
4900
4901 trigger:
4902         if (thread_is_running(&scrub->os_thread)) {
4903                 if (inode == NULL) {
4904                         inode = osd_iget(oti, dev, id);
4905                         /* The inode has been removed (by race maybe). */
4906                         if (IS_ERR(inode)) {
4907                                 rc = PTR_ERR(inode);
4908
4909                                 RETURN(rc == -ESTALE ? -ENOENT : rc);
4910                         }
4911                 }
4912
4913                 rc = osd_oii_insert(dev, oic, insert);
4914                 /* There is race condition between osd_oi_lookup and OI scrub.
4915                  * The OI scrub finished just after osd_oi_lookup() failure.
4916                  * Under such case, it is unnecessary to trigger OI scrub again,
4917                  * but try to call osd_oi_lookup() again. */
4918                 if (unlikely(rc == -EAGAIN))
4919                         goto again;
4920
4921                 if (!S_ISDIR(inode->i_mode))
4922                         rc = 0;
4923                 else
4924                         rc = osd_check_lmv(oti, dev, inode, oic);
4925
4926                 iput(inode);
4927                 RETURN(rc);
4928         }
4929
4930         if (!dev->od_noscrub && ++once == 1) {
4931                 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4932                                      SS_CLEAR_FAILOUT);
4933                 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4934                        "%.16s: trigger partial OI scrub for RPC inconsistency "
4935                        "checking FID "DFID": rc = %d\n",
4936                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4937                        PFID(fid), rc);
4938                 if (rc == 0 || rc == -EALREADY)
4939                         goto again;
4940         }
4941
4942         if (inode != NULL)
4943                 iput(inode);
4944
4945         RETURN(rc);
4946 }
4947
4948 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4949                                struct osd_device *dev,
4950                                struct osd_idmap_cache *oic,
4951                                struct lu_fid *fid, __u32 ino)
4952 {
4953         struct lustre_mdt_attrs *lma   = &oti->oti_mdt_attrs;
4954         struct inode            *inode;
4955         int                      rc;
4956
4957         osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4958         inode = osd_iget(oti, dev, &oic->oic_lid);
4959         if (IS_ERR(inode)) {
4960                 fid_zero(&oic->oic_fid);
4961                 return PTR_ERR(inode);
4962         }
4963
4964         rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4965         iput(inode);
4966         if (rc != 0)
4967                 fid_zero(&oic->oic_fid);
4968         else
4969                 *fid = oic->oic_fid = lma->lma_self_fid;
4970         return rc;
4971 }
4972
4973 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4974                       struct osd_inode_id *id, const struct lu_fid *fid)
4975 {
4976         CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4977                id->oii_ino, id->oii_gen, info);
4978         info->oti_cache.oic_lid = *id;
4979         info->oti_cache.oic_fid = *fid;
4980         info->oti_cache.oic_dev = osd;
4981 }
4982
4983 /**
4984  * Get parent FID from the linkEA.
4985  *
4986  * For a directory which parent resides on remote MDT, to satisfy the
4987  * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4988  * the other hand, to make the lookup(..) on the directory can return
4989  * the real parent FID, we append the real parent FID after its ".."
4990  * name entry in the /REMOTE_PARENT_DIR.
4991  *
4992  * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4993  * backup. So after the restore, we cannot get the right parent FID from
4994  * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4995  * we have stored the real parent FID in the directory object's linkEA,
4996  * we can parse the linkEA for the real parent FID.
4997  *
4998  * \param[in] env       pointer to the thread context
4999  * \param[in] obj       pointer to the object to be handled
5000  * \param[out]fid       pointer to the buffer to hold the parent FID
5001  *
5002  * \retval              0 for getting the real parent FID successfully
5003  * \retval              negative error number on failure
5004  */
5005 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5006                                     struct osd_object *obj,
5007                                     struct lu_fid *fid)
5008 {
5009         struct osd_thread_info  *oti    = osd_oti_get(env);
5010         struct lu_buf           *buf    = &oti->oti_big_buf;
5011         struct dentry           *dentry = &oti->oti_obj_dentry;
5012         struct inode            *inode  = obj->oo_inode;
5013         struct linkea_data       ldata  = { NULL };
5014         int                      rc;
5015         ENTRY;
5016
5017         fid_zero(fid);
5018         if (!S_ISDIR(inode->i_mode))
5019                 RETURN(-EIO);
5020
5021 again:
5022         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5023                              buf->lb_buf, buf->lb_len);
5024         if (rc == -ERANGE) {
5025                 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5026                                      NULL, 0);
5027                 if (rc > 0) {
5028                         lu_buf_realloc(buf, rc);
5029                         if (buf->lb_buf == NULL)
5030                                 RETURN(-ENOMEM);
5031
5032                         goto again;
5033                 }
5034         }
5035
5036         if (unlikely(rc == 0))
5037                 RETURN(-ENODATA);
5038
5039         if (rc < 0)
5040                 RETURN(rc);
5041
5042         if (unlikely(buf->lb_buf == NULL)) {
5043                 lu_buf_realloc(buf, rc);
5044                 if (buf->lb_buf == NULL)
5045                         RETURN(-ENOMEM);
5046
5047                 goto again;
5048         }
5049
5050         ldata.ld_buf = buf;
5051         rc = linkea_init_with_rec(&ldata);
5052         if (!rc) {
5053                 linkea_first_entry(&ldata);
5054                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5055         }
5056
5057         RETURN(rc);
5058 }
5059
5060 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5061                                     struct inode *inode,
5062                                     const struct lu_fid *pfid,
5063                                     const char *name, const int namelen)
5064 {
5065         struct osd_thread_info *oti = osd_oti_get(env);
5066         struct lu_buf *buf = &oti->oti_big_buf;
5067         struct dentry *dentry = &oti->oti_obj_dentry;
5068         struct linkea_data ldata = { NULL };
5069         struct lu_name cname = { .ln_name = name,
5070                                  .ln_namelen = namelen };
5071         int rc;
5072         ENTRY;
5073
5074 again:
5075         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5076                              buf->lb_buf, buf->lb_len);
5077         if (rc == -ERANGE)
5078                 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5079
5080         if (rc < 0)
5081                 RETURN(rc);
5082
5083         if (unlikely(rc == 0))
5084                 RETURN(-ENODATA);
5085
5086         if (buf->lb_len < rc) {
5087                 lu_buf_realloc(buf, rc);
5088                 if (buf->lb_buf == NULL)
5089                         RETURN(-ENOMEM);
5090
5091                 goto again;
5092         }
5093
5094         ldata.ld_buf = buf;
5095         rc = linkea_init_with_rec(&ldata);
5096         if (!rc)
5097                 rc = linkea_links_find(&ldata, &cname, pfid);
5098
5099         RETURN(rc);
5100 }
5101
5102 /**
5103  * Calls ->lookup() to find dentry. From dentry get inode and
5104  * read inode's ea to get fid. This is required for  interoperability
5105  * mode (b11826)
5106  *
5107  * \retval   0, on success
5108  * \retval -ve, on error
5109  */
5110 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5111                              struct dt_rec *rec, const struct dt_key *key)
5112 {
5113         struct inode                    *dir    = obj->oo_inode;
5114         struct dentry                   *dentry;
5115         struct ldiskfs_dir_entry_2      *de;
5116         struct buffer_head              *bh;
5117         struct lu_fid                   *fid = (struct lu_fid *) rec;
5118         struct htree_lock               *hlock = NULL;
5119         int                             ino;
5120         int                             rc;
5121         ENTRY;
5122
5123         LASSERT(dir->i_op != NULL);
5124         LASSERT(dir->i_op->lookup != NULL);
5125
5126         dentry = osd_child_dentry_get(env, obj,
5127                                       (char *)key, strlen((char *)key));
5128
5129         if (obj->oo_hl_head != NULL) {
5130                 hlock = osd_oti_get(env)->oti_hlock;
5131                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5132                                    dir, LDISKFS_HLOCK_LOOKUP);
5133         } else {
5134                 down_read(&obj->oo_ext_idx_sem);
5135         }
5136
5137         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5138         if (!IS_ERR(bh)) {
5139                 struct osd_thread_info *oti = osd_oti_get(env);
5140                 struct osd_inode_id *id = &oti->oti_id;
5141                 struct osd_idmap_cache *oic = &oti->oti_cache;
5142                 struct osd_device *dev = osd_obj2dev(obj);
5143
5144                 ino = le32_to_cpu(de->inode);
5145                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5146                         brelse(bh);
5147                         rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5148                         GOTO(out, rc);
5149                 }
5150
5151                 rc = osd_get_fid_from_dentry(de, rec);
5152
5153                 /* done with de, release bh */
5154                 brelse(bh);
5155                 if (rc != 0) {
5156                         if (unlikely(ino == osd_remote_parent_ino(dev))) {
5157                                 const char *name = (const char *)key;
5158
5159                                 /* If the parent is on remote MDT, and there
5160                                  * is no FID-in-dirent, then we have to get
5161                                  * the parent FID from the linkEA.  */
5162                                 if (likely(strlen(name) == 2 &&
5163                                            name[0] == '.' && name[1] == '.'))
5164                                         rc = osd_get_pfid_from_linkea(env, obj,
5165                                                                       fid);
5166                         } else {
5167                                 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5168                         }
5169                 } else {
5170                         osd_id_gen(id, ino, OSD_OII_NOGEN);
5171                 }
5172
5173                 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5174                         fid_zero(&oic->oic_fid);
5175
5176                         GOTO(out, rc);
5177                 }
5178
5179                 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5180                 rc = osd_consistency_check(oti, dev, oic);
5181                 if (rc != 0)
5182                         fid_zero(&oic->oic_fid);
5183         } else {
5184                 rc = PTR_ERR(bh);
5185         }
5186
5187         GOTO(out, rc);
5188
5189 out:
5190         if (hlock != NULL)
5191                 ldiskfs_htree_unlock(hlock);
5192         else
5193                 up_read(&obj->oo_ext_idx_sem);
5194         return rc;
5195 }
5196
5197 /**
5198  * Put the osd object once done with it.
5199  *
5200  * \param obj osd object that needs to be put
5201  */
5202 static inline void osd_object_put(const struct lu_env *env,
5203                                   struct osd_object *obj)
5204 {
5205         lu_object_put(env, &obj->oo_dt.do_lu);
5206 }
5207
5208 static int osd_index_declare_ea_insert(const struct lu_env *env,
5209                                        struct dt_object *dt,
5210                                        const struct dt_rec *rec,
5211                                        const struct dt_key *key,
5212                                        struct thandle *handle)
5213 {
5214         struct osd_thandle      *oh;
5215         struct osd_device       *osd   = osd_dev(dt->do_lu.lo_dev);
5216         struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
5217         const struct lu_fid     *fid    = rec1->rec_fid;
5218         int                      credits, rc = 0;
5219         struct osd_idmap_cache  *idc;
5220         ENTRY;
5221
5222         LASSERT(!dt_object_remote(dt));
5223         LASSERT(handle != NULL);
5224         LASSERT(fid != NULL);
5225         LASSERT(rec1->rec_type != 0);
5226
5227         oh = container_of0(handle, struct osd_thandle, ot_super);
5228         LASSERT(oh->ot_handle == NULL);
5229
5230         credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5231
5232         /* we can't call iget() while a transactions is running
5233          * (this can lead to a deadlock), but we need to know
5234          * inum and object type. so we find this information at
5235          * declaration and cache in per-thread info */
5236         idc = osd_idc_find_or_init(env, osd, fid);
5237         if (IS_ERR(idc))
5238                 RETURN(PTR_ERR(idc));
5239         if (idc->oic_remote) {
5240                 /* a reference to remote inode is represented by an
5241                  * agent inode which we have to create */
5242                 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5243                 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5244         }
5245
5246         osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5247
5248         if (osd_dt_obj(dt)->oo_inode != NULL) {
5249                 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5250
5251                 /* We ignore block quota on meta pool (MDTs), so needn't
5252                  * calculate how many blocks will be consumed by this index
5253                  * insert */
5254                 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5255                                            i_gid_read(inode), 0, oh,
5256                                            osd_dt_obj(dt), true, NULL, false);
5257         }
5258
5259         RETURN(rc);
5260 }
5261
5262 /**
5263  * Index add function for interoperability mode (b11826).
5264  * It will add the directory entry.This entry is needed to
5265  * maintain name->fid mapping.
5266  *
5267  * \param key it is key i.e. file entry to be inserted
5268  * \param rec it is value of given key i.e. fid
5269  *
5270  * \retval   0, on success
5271  * \retval -ve, on error
5272  */
5273 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5274                                const struct dt_rec *rec,
5275                                const struct dt_key *key, struct thandle *th,
5276                                int ignore_quota)
5277 {
5278         struct osd_object       *obj = osd_dt_obj(dt);
5279         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
5280         struct dt_insert_rec    *rec1   = (struct dt_insert_rec *)rec;
5281         const struct lu_fid     *fid    = rec1->rec_fid;
5282         const char              *name = (const char *)key;
5283         struct osd_thread_info  *oti   = osd_oti_get(env);
5284         struct inode            *child_inode = NULL;
5285         struct osd_idmap_cache  *idc;
5286         int                     rc;
5287         ENTRY;
5288
5289         if (!dt_object_exists(dt))
5290                 RETURN(-ENOENT);
5291
5292         LASSERT(osd_invariant(obj));
5293         LASSERT(!dt_object_remote(dt));
5294         LASSERT(th != NULL);
5295
5296         osd_trans_exec_op(env, th, OSD_OT_INSERT);
5297
5298         LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5299
5300         idc = osd_idc_find(env, osd, fid);
5301         if (unlikely(idc == NULL)) {
5302                 /* this dt_insert() wasn't declared properly, so
5303                  * FID is missing in OI cache. we better do not
5304                  * lookup FID in FLDB/OI and don't risk to deadlock,
5305                  * but in some special cases (lfsck testing, etc)
5306                  * it's much simpler than fixing a caller */
5307                 CERROR("%s: "DFID" wasn't declared for insert\n",
5308                        osd_name(osd), PFID(fid));
5309                 dump_stack();
5310                 idc = osd_idc_find_or_init(env, osd, fid);
5311                 if (IS_ERR(idc))
5312                         RETURN(PTR_ERR(idc));
5313         }
5314
5315         if (idc->oic_remote) {
5316                 /* Insert remote entry */
5317                 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5318                         struct osd_mdobj_map    *omm = osd->od_mdt_map;
5319                         struct osd_thandle      *oh;
5320
5321                         /* If parent on remote MDT, we need put this object
5322                          * under AGENT */
5323                         oh = container_of(th, typeof(*oh), ot_super);
5324                         rc = osd_add_to_remote_parent(env, osd, obj, oh);
5325                         if (rc != 0) {
5326                                 CERROR("%s: add "DFID" error: rc = %d\n",
5327                                        osd_name(osd),
5328                                        PFID(lu_object_fid(&dt->do_lu)), rc);
5329                                 RETURN(rc);
5330                         }
5331
5332                         child_inode = igrab(omm->omm_remote_parent->d_inode);
5333                 } else {
5334                         child_inode = osd_create_local_agent_inode(env, osd,
5335                                         obj, fid, rec1->rec_type & S_IFMT, th);
5336                         if (IS_ERR(child_inode))
5337                                 RETURN(PTR_ERR(child_inode));
5338                 }
5339         } else {
5340                 /* Insert local entry */
5341                 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5342                         /* for a reason OI cache wasn't filled properly */
5343                         CERROR("%s: OIC for "DFID" isn't filled\n",
5344                                osd_name(osd), PFID(fid));
5345                         RETURN(-EINVAL);
5346                 }
5347                 child_inode = oti->oti_inode;
5348                 if (unlikely(child_inode == NULL)) {
5349                         struct ldiskfs_inode_info *lii;
5350                         OBD_ALLOC_PTR(lii);
5351                         if (lii == NULL)
5352                                 RETURN(-ENOMEM);
5353                         child_inode = oti->oti_inode = &lii->vfs_inode;
5354                 }
5355                 child_inode->i_sb = osd_sb(osd);
5356                 child_inode->i_ino = idc->oic_lid.oii_ino;
5357                 child_inode->i_mode = rec1->rec_type & S_IFMT;
5358         }
5359
5360         rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5361
5362         CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5363                obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5364
5365         if (child_inode && child_inode != oti->oti_inode)
5366                 iput(child_inode);
5367         LASSERT(osd_invariant(obj));
5368         osd_trans_exec_check(env, th, OSD_OT_INSERT);
5369         RETURN(rc);
5370 }
5371
5372 /**
5373  *  Initialize osd Iterator for given osd index object.
5374  *
5375  *  \param  dt      osd index object
5376  */
5377
5378 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5379                                      struct dt_object *dt,
5380                                      __u32 unused)
5381 {
5382         struct osd_it_iam      *it;
5383         struct osd_object      *obj = osd_dt_obj(dt);
5384         struct lu_object       *lo  = &dt->do_lu;
5385         struct iam_path_descr  *ipd;
5386         struct iam_container   *bag = &obj->oo_dir->od_container;
5387
5388         if (!dt_object_exists(dt))
5389                 return ERR_PTR(-ENOENT);
5390
5391         OBD_ALLOC_PTR(it);
5392         if (it == NULL)
5393                 return ERR_PTR(-ENOMEM);
5394
5395         ipd = osd_it_ipd_get(env, bag);
5396         if (likely(ipd != NULL)) {
5397                 it->oi_obj = obj;
5398                 it->oi_ipd = ipd;
5399                 lu_object_get(lo);
5400                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5401                 return (struct dt_it *)it;
5402         } else {
5403                 OBD_FREE_PTR(it);
5404                 return ERR_PTR(-ENOMEM);
5405         }
5406 }
5407
5408 /**
5409  * free given Iterator.
5410  */
5411
5412 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5413 {
5414         struct osd_it_iam       *it  = (struct osd_it_iam *)di;
5415         struct osd_object       *obj = it->oi_obj;
5416
5417         iam_it_fini(&it->oi_it);
5418         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5419         lu_object_put(env, &obj->oo_dt.do_lu);
5420         OBD_FREE_PTR(it);
5421 }
5422
5423 /**
5424  *  Move Iterator to record specified by \a key
5425  *
5426  *  \param  di      osd iterator
5427  *  \param  key     key for index
5428  *
5429  *  \retval +ve  di points to record with least key not larger than key
5430  *  \retval  0   di points to exact matched key
5431  *  \retval -ve  failure
5432  */
5433
5434 static int osd_it_iam_get(const struct lu_env *env,
5435                           struct dt_it *di, const struct dt_key *key)
5436 {
5437         struct osd_thread_info  *oti = osd_oti_get(env);
5438         struct osd_it_iam       *it = (struct osd_it_iam *)di;
5439
5440         if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5441                 /* swab quota uid/gid */
5442                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5443                 key = (struct dt_key *)&oti->oti_quota_id;
5444         }
5445
5446         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5447 }
5448
5449 /**
5450  *  Release Iterator
5451  *
5452  *  \param  di      osd iterator
5453  */
5454 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5455 {
5456         struct osd_it_iam *it = (struct osd_it_iam *)di;
5457
5458         iam_it_put(&it->oi_it);
5459 }
5460
5461 /**
5462  *  Move iterator by one record
5463  *
5464  *  \param  di      osd iterator
5465  *
5466  *  \retval +1   end of container reached
5467  *  \retval  0   success
5468  *  \retval -ve  failure
5469  */
5470
5471 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5472 {
5473         struct osd_it_iam *it = (struct osd_it_iam *)di;
5474
5475         return iam_it_next(&it->oi_it);
5476 }
5477
5478 /**
5479  * Return pointer to the key under iterator.
5480  */
5481
5482 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5483                                  const struct dt_it *di)
5484 {
5485         struct osd_thread_info *oti = osd_oti_get(env);
5486         struct osd_it_iam      *it = (struct osd_it_iam *)di;
5487         struct osd_object      *obj = it->oi_obj;
5488         struct dt_key          *key;
5489
5490         key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5491
5492         if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5493                 /* swab quota uid/gid */
5494                 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5495                 key = (struct dt_key *)&oti->oti_quota_id;
5496         }
5497
5498         return key;
5499 }
5500
5501 /**
5502  * Return size of key under iterator (in bytes)
5503  */
5504
5505 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5506 {
5507         struct osd_it_iam *it = (struct osd_it_iam *)di;
5508
5509         return iam_it_key_size(&it->oi_it);
5510 }
5511
5512 static inline void
5513 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5514 {
5515         /* check if file type is required */
5516         if (ent->lde_attrs & LUDA_TYPE) {
5517                 struct luda_type *lt;
5518                 int align = sizeof(*lt) - 1;
5519
5520                 len = (len + align) & ~align;
5521                 lt = (struct luda_type *)(ent->lde_name + len);
5522                 lt->lt_type = cpu_to_le16(DTTOIF(type));
5523         }
5524
5525         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5526 }
5527
5528 /**
5529  * build lu direct from backend fs dirent.
5530  */
5531
5532 static inline void
5533 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5534                    char *name, __u16 namelen, __u16 type, __u32 attr)
5535 {
5536         ent->lde_attrs = attr | LUDA_FID;
5537         fid_cpu_to_le(&ent->lde_fid, fid);
5538
5539         ent->lde_hash = cpu_to_le64(offset);
5540         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5541
5542         strncpy(ent->lde_name, name, namelen);
5543         ent->lde_name[namelen] = '\0';
5544         ent->lde_namelen = cpu_to_le16(namelen);
5545
5546         /* append lustre attributes */
5547         osd_it_append_attrs(ent, namelen, type);
5548 }
5549
5550 /**
5551  * Return pointer to the record under iterator.
5552  */
5553 static int osd_it_iam_rec(const struct lu_env *env,
5554                           const struct dt_it *di,
5555                           struct dt_rec *dtrec, __u32 attr)
5556 {
5557         struct osd_it_iam      *it   = (struct osd_it_iam *)di;
5558         struct osd_thread_info *info = osd_oti_get(env);
5559         ENTRY;
5560
5561         if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5562                 const struct osd_fid_pack *rec;
5563                 struct lu_fid             *fid = &info->oti_fid;
5564                 struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
5565                 char                      *name;
5566                 int                        namelen;
5567                 __u64                      hash;
5568                 int                        rc;
5569
5570                 name = (char *)iam_it_key_get(&it->oi_it);
5571                 if (IS_ERR(name))
5572                         RETURN(PTR_ERR(name));
5573
5574                 namelen = iam_it_key_size(&it->oi_it);
5575
5576                 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5577                 if (IS_ERR(rec))
5578                         RETURN(PTR_ERR(rec));
5579
5580                 rc = osd_fid_unpack(fid, rec);
5581                 if (rc)
5582                         RETURN(rc);
5583
5584                 hash = iam_it_store(&it->oi_it);
5585
5586                 /* IAM does not store object type in IAM index (dir) */
5587                 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5588                                    0, LUDA_FID);
5589         } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5590                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5591                            (struct iam_rec *)dtrec);
5592                 osd_quota_unpack(it->oi_obj, dtrec);
5593         } else {
5594                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5595                            (struct iam_rec *)dtrec);
5596         }
5597
5598         RETURN(0);
5599 }
5600
5601 /**
5602  * Returns cookie for current Iterator position.
5603  */
5604 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5605 {
5606         struct osd_it_iam *it = (struct osd_it_iam *)di;
5607
5608         return iam_it_store(&it->oi_it);
5609 }
5610
5611 /**
5612  * Restore iterator from cookie.
5613  *
5614  * \param  di      osd iterator
5615  * \param  hash    Iterator location cookie
5616  *
5617  * \retval +ve  di points to record with least key not larger than key.
5618  * \retval  0   di points to exact matched key
5619  * \retval -ve  failure
5620  */
5621
5622 static int osd_it_iam_load(const struct lu_env *env,
5623                            const struct dt_it *di, __u64 hash)
5624 {
5625         struct osd_it_iam *it = (struct osd_it_iam *)di;
5626
5627         return iam_it_load(&it->oi_it, hash);
5628 }
5629
5630 static const struct dt_index_operations osd_index_iam_ops = {
5631         .dio_lookup         = osd_index_iam_lookup,
5632         .dio_declare_insert = osd_index_declare_iam_insert,
5633         .dio_insert         = osd_index_iam_insert,
5634         .dio_declare_delete = osd_index_declare_iam_delete,
5635         .dio_delete         = osd_index_iam_delete,
5636         .dio_it     = {
5637                 .init     = osd_it_iam_init,
5638                 .fini     = osd_it_iam_fini,
5639                 .get      = osd_it_iam_get,
5640                 .put      = osd_it_iam_put,
5641                 .next     = osd_it_iam_next,
5642                 .key      = osd_it_iam_key,
5643                 .key_size = osd_it_iam_key_size,
5644                 .rec      = osd_it_iam_rec,
5645                 .store    = osd_it_iam_store,
5646                 .load     = osd_it_iam_load
5647         }
5648 };
5649
5650
5651 /**
5652  * Creates or initializes iterator context.
5653  *
5654  * \retval struct osd_it_ea, iterator structure on success
5655  *
5656  */
5657 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5658                                     struct dt_object *dt,
5659                                     __u32 attr)
5660 {
5661         struct osd_object       *obj  = osd_dt_obj(dt);
5662         struct osd_thread_info  *info = osd_oti_get(env);
5663         struct osd_it_ea        *oie;
5664         struct file             *file;
5665         struct lu_object        *lo   = &dt->do_lu;
5666         struct dentry           *obj_dentry;
5667         ENTRY;
5668
5669         if (!dt_object_exists(dt) || obj->oo_destroyed)
5670                 RETURN(ERR_PTR(-ENOENT));
5671
5672         OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5673         if (oie == NULL)
5674                 RETURN(ERR_PTR(-ENOMEM));
5675         obj_dentry = &oie->oie_dentry;
5676
5677         obj_dentry->d_inode = obj->oo_inode;
5678         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5679         obj_dentry->d_name.hash = 0;
5680
5681         oie->oie_rd_dirent       = 0;
5682         oie->oie_it_dirent       = 0;
5683         oie->oie_dirent          = NULL;
5684         if (unlikely(!info->oti_it_ea_buf_used)) {
5685                 oie->oie_buf = info->oti_it_ea_buf;
5686                 info->oti_it_ea_buf_used = 1;
5687         } else {
5688                 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5689                 if (oie->oie_buf == NULL)
5690                         RETURN(ERR_PTR(-ENOMEM));
5691         }
5692         oie->oie_obj             = obj;
5693
5694         file = &oie->oie_file;
5695
5696         /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5697         if (attr & LUDA_64BITHASH)
5698                 file->f_mode    = FMODE_64BITHASH;
5699         else
5700                 file->f_mode    = FMODE_32BITHASH;
5701         file->f_path.dentry     = obj_dentry;
5702         file->f_mapping         = obj->oo_inode->i_mapping;
5703         file->f_op              = obj->oo_inode->i_fop;
5704         set_file_inode(file, obj->oo_inode);
5705
5706         lu_object_get(lo);
5707         RETURN((struct dt_it *) oie);
5708 }
5709
5710 /**
5711  * Destroy or finishes iterator context.
5712  *
5713  * \param di iterator structure to be destroyed
5714  */
5715 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5716 {
5717         struct osd_thread_info  *info = osd_oti_get(env);
5718         struct osd_it_ea        *oie    = (struct osd_it_ea *)di;
5719         struct osd_object       *obj    = oie->oie_obj;
5720         struct inode            *inode  = obj->oo_inode;
5721
5722         ENTRY;
5723         oie->oie_file.f_op->release(inode, &oie->oie_file);
5724         lu_object_put(env, &obj->oo_dt.do_lu);
5725         if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5726                 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5727         else
5728                 info->oti_it_ea_buf_used = 0;
5729         OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5730         EXIT;
5731 }
5732
5733 /**
5734  * It position the iterator at given key, so that next lookup continues from
5735  * that key Or it is similar to dio_it->load() but based on a key,
5736  * rather than file position.
5737  *
5738  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5739  * to the beginning.
5740  *
5741  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5742  */
5743 static int osd_it_ea_get(const struct lu_env *env,
5744                          struct dt_it *di, const struct dt_key *key)
5745 {
5746         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
5747
5748         ENTRY;
5749         LASSERT(((const char *)key)[0] == '\0');
5750         it->oie_file.f_pos      = 0;
5751         it->oie_rd_dirent       = 0;
5752         it->oie_it_dirent       = 0;
5753         it->oie_dirent          = NULL;
5754
5755         RETURN(+1);
5756 }
5757
5758 /**
5759  * Does nothing
5760  */
5761 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5762 {
5763 }
5764
5765 struct osd_filldir_cbs {
5766 #ifdef HAVE_DIR_CONTEXT
5767         struct dir_context ctx;
5768 #endif
5769         struct osd_it_ea  *it;
5770 };
5771 /**
5772  * It is called internally by ->readdir(). It fills the
5773  * iterator's in-memory data structure with required
5774  * information i.e. name, namelen, rec_size etc.
5775  *
5776  * \param buf in which information to be filled in.
5777  * \param name name of the file in given dir
5778  *
5779  * \retval 0 on success
5780  * \retval 1 on buffer full
5781  */
5782 #ifdef HAVE_FILLDIR_USE_CTX
5783 static int osd_ldiskfs_filldir(struct dir_context *buf,
5784 #else
5785 static int osd_ldiskfs_filldir(void *buf,
5786 #endif
5787                                const char *name, int namelen,
5788                                loff_t offset, __u64 ino, unsigned d_type)
5789 {
5790         struct osd_it_ea        *it   =
5791                 ((struct osd_filldir_cbs *)buf)->it;
5792         struct osd_object       *obj  = it->oie_obj;
5793         struct osd_it_ea_dirent *ent  = it->oie_dirent;
5794         struct lu_fid           *fid  = &ent->oied_fid;
5795         struct osd_fid_pack     *rec;
5796         ENTRY;
5797
5798         /* this should never happen */
5799         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5800                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5801                 RETURN(-EIO);
5802         }
5803
5804         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5805             OSD_IT_EA_BUFSIZE)
5806                 RETURN(1);
5807
5808         /* "." is just the object itself. */
5809         if (namelen == 1 && name[0] == '.') {
5810                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5811         } else if (d_type & LDISKFS_DIRENT_LUFID) {
5812                 rec = (struct osd_fid_pack*) (name + namelen + 1);
5813                 if (osd_fid_unpack(fid, rec) != 0)
5814                         fid_zero(fid);
5815         } else {
5816                 fid_zero(fid);
5817         }
5818         d_type &= ~LDISKFS_DIRENT_LUFID;
5819
5820         /* NOT export local root. */
5821         if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5822                 ino = obj->oo_inode->i_ino;
5823                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5824         }
5825
5826         ent->oied_ino     = ino;
5827         ent->oied_off     = offset;
5828         ent->oied_namelen = namelen;
5829         ent->oied_type    = d_type;
5830
5831         memcpy(ent->oied_name, name, namelen);
5832
5833         it->oie_rd_dirent++;
5834         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5835         RETURN(0);
5836 }
5837
5838 /**
5839  * Calls ->readdir() to load a directory entry at a time
5840  * and stored it in iterator's in-memory data structure.
5841  *
5842  * \param di iterator's in memory structure
5843  *
5844  * \retval   0 on success
5845  * \retval -ve on error
5846  * \retval +1 reach the end of entry
5847  */
5848 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5849                                const struct dt_it *di)
5850 {
5851         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
5852         struct osd_object  *obj   = it->oie_obj;
5853         struct inode       *inode = obj->oo_inode;
5854         struct htree_lock  *hlock = NULL;
5855         struct file        *filp  = &it->oie_file;
5856         int                 rc = 0;
5857         struct osd_filldir_cbs buf = {
5858 #ifdef HAVE_DIR_CONTEXT
5859                 .ctx.actor = osd_ldiskfs_filldir,
5860 #endif
5861                 .it = it
5862         };
5863
5864         ENTRY;
5865         it->oie_dirent = it->oie_buf;
5866         it->oie_rd_dirent = 0;
5867
5868         if (obj->oo_hl_head != NULL) {
5869                 hlock = osd_oti_get(env)->oti_hlock;
5870                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5871                                    inode, LDISKFS_HLOCK_READDIR);
5872         } else {
5873                 down_read(&obj->oo_ext_idx_sem);
5874         }
5875
5876 #ifdef HAVE_DIR_CONTEXT
5877         buf.ctx.pos = filp->f_pos;
5878         rc = inode->i_fop->iterate(filp, &buf.ctx);
5879         filp->f_pos = buf.ctx.pos;
5880 #else
5881         rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5882 #endif
5883
5884         if (hlock != NULL)
5885                 ldiskfs_htree_unlock(hlock);
5886         else
5887                 up_read(&obj->oo_ext_idx_sem);
5888
5889         if (it->oie_rd_dirent == 0) {
5890                 /*If it does not get any dirent, it means it has been reached
5891                  *to the end of the dir */
5892                 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5893                 if (rc == 0)
5894                         rc = 1;
5895         } else {
5896                 it->oie_dirent = it->oie_buf;
5897                 it->oie_it_dirent = 1;
5898         }
5899
5900         RETURN(rc);
5901 }
5902
5903 /**
5904  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5905  * to load a directory entry at a time and stored it in
5906  * iterator's in-memory data structure.
5907  *
5908  * \param di iterator's in memory structure
5909  *
5910  * \retval +ve iterator reached to end
5911  * \retval   0 iterator not reached to end
5912  * \retval -ve on error
5913  */
5914 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5915 {
5916         struct osd_it_ea *it = (struct osd_it_ea *)di;
5917         int rc;
5918
5919         ENTRY;
5920
5921         if (it->oie_it_dirent < it->oie_rd_dirent) {
5922                 it->oie_dirent =
5923                         (void *) it->oie_dirent +
5924                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5925                                        it->oie_dirent->oied_namelen);
5926                 it->oie_it_dirent++;
5927                 RETURN(0);
5928         } else {
5929                 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5930                         rc = +1;
5931                 else
5932                         rc = osd_ldiskfs_it_fill(env, di);
5933         }
5934
5935         RETURN(rc);
5936 }
5937
5938 /**
5939  * Returns the key at current position from iterator's in memory structure.
5940  *
5941  * \param di iterator's in memory structure
5942  *
5943  * \retval key i.e. struct dt_key on success
5944  */
5945 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5946                                     const struct dt_it *di)
5947 {
5948         struct osd_it_ea *it = (struct osd_it_ea *)di;
5949
5950         return (struct dt_key *)it->oie_dirent->oied_name;
5951 }
5952
5953 /**
5954  * Returns the key's size at current position from iterator's in memory structure.
5955  *
5956  * \param di iterator's in memory structure
5957  *
5958  * \retval key_size i.e. struct dt_key on success
5959  */
5960 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5961 {
5962         struct osd_it_ea *it = (struct osd_it_ea *)di;
5963
5964         return it->oie_dirent->oied_namelen;
5965 }
5966
5967 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
5968 {
5969         if (LDISKFS_DIR_REC_LEN(de) >=
5970             __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
5971                 return true;
5972
5973         return false;
5974 }
5975
5976 static inline bool
5977 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5978                      unsigned blocksize, bool dotdot)
5979 {
5980         if (dotdot)
5981                 return osd_dotdot_has_space(de);
5982
5983         if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5984             __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5985                 return true;
5986
5987         return false;
5988 }
5989
5990 static int
5991 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
5992                     handle_t *jh, struct dentry *dentry,
5993                     const struct lu_fid *fid, struct buffer_head *bh,
5994                     struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
5995                     bool dotdot)
5996 {
5997         struct inode                *dir        = dentry->d_parent->d_inode;
5998         struct inode                *inode      = dentry->d_inode;
5999         struct osd_fid_pack         *rec;
6000         struct ldiskfs_dentry_param *ldp;
6001         int                          namelen    = dentry->d_name.len;
6002         int                          rc;
6003         struct osd_thread_info     *info        = osd_oti_get(env);
6004         ENTRY;
6005
6006         if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6007                                           LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6008                 RETURN(0);
6009
6010         /* There is enough space to hold the FID-in-dirent. */
6011         if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6012                 rc = ldiskfs_journal_get_write_access(jh, bh);
6013                 if (rc != 0)
6014                         RETURN(rc);
6015
6016                 de->name[namelen] = 0;
6017                 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6018                 rec->fp_len = sizeof(struct lu_fid) + 1;
6019                 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6020                 de->file_type |= LDISKFS_DIRENT_LUFID;
6021                 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6022
6023                 RETURN(rc);
6024         }
6025
6026         LASSERT(!dotdot);
6027
6028         rc = ldiskfs_delete_entry(jh, dir, de, bh);
6029         if (rc != 0)
6030                 RETURN(rc);
6031
6032         ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6033         osd_get_ldiskfs_dirent_param(ldp, fid);
6034         dentry->d_fsdata = (void *)ldp;
6035         ll_vfs_dq_init(dir);
6036         rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6037         /* It is too bad, we cannot reinsert the name entry back.
6038          * That means we lose it! */
6039         if (rc != 0)
6040                 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
6041                        "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6042                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
6043                        dir->i_ino, dir->i_generation, namelen,
6044                        dentry->d_name.name, PFID(fid), rc);
6045
6046         RETURN(rc);
6047 }
6048
6049 static int
6050 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6051                         struct osd_it_ea *it, struct lu_fid *fid,
6052                         struct osd_inode_id *id, __u32 *attr)
6053 {
6054         struct osd_thread_info     *info        = osd_oti_get(env);
6055         struct lustre_mdt_attrs    *lma         = &info->oti_mdt_attrs;
6056         struct osd_device          *dev         = osd_obj2dev(obj);
6057         struct super_block         *sb          = osd_sb(dev);
6058         const char                 *devname     = osd_name(dev);
6059         struct osd_it_ea_dirent    *ent         = it->oie_dirent;
6060         struct inode               *dir         = obj->oo_inode;
6061         struct htree_lock          *hlock       = NULL;
6062         struct buffer_head         *bh          = NULL;
6063         handle_t                   *jh          = NULL;
6064         struct ldiskfs_dir_entry_2 *de;
6065         struct dentry              *dentry;
6066         struct inode               *inode;
6067         const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6068         int                         credits;
6069         int                         rc;
6070         bool                        dotdot      = false;
6071         bool                        dirty       = false;
6072         ENTRY;
6073
6074         if (ent->oied_name[0] == '.') {
6075                 if (ent->oied_namelen == 1)
6076                         RETURN(0);
6077
6078                 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6079                         dotdot = true;
6080         }
6081
6082         osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6083         inode = osd_iget(info, dev, id);
6084         if (IS_ERR(inode)) {
6085                 rc = PTR_ERR(inode);
6086                 if (rc == -ENOENT || rc == -ESTALE) {
6087                         /* Maybe dangling name entry, or
6088                          * corrupted directory entry. */
6089                         *attr |= LUDA_UNKNOWN;
6090                         rc = 0;
6091                 } else {
6092                         CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6093                                "check_repair, dir = %lu/%u, name = %.*s, "
6094                                "ino = %llu, rc = %d\n",
6095                                devname, dir->i_ino, dir->i_generation,
6096                                ent->oied_namelen, ent->oied_name,
6097                                ent->oied_ino, rc);
6098                 }
6099
6100                 RETURN(rc);
6101         }
6102
6103         dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6104                                            ent->oied_namelen);
6105         rc = osd_get_lma(info, inode, dentry, lma);
6106         if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6107                 lma = NULL;
6108         else if (rc != 0)
6109                 GOTO(out, rc);
6110
6111         /* We need to ensure that the name entry is still valid.
6112          * Because it may be removed or renamed by other already.
6113          *
6114          * The unlink or rename operation will start journal before PDO lock,
6115          * so to avoid deadlock, here we need to start journal handle before
6116          * related PDO lock also. But because we do not know whether there
6117          * will be something to be repaired before PDO lock, we just start
6118          * journal without conditions.
6119          *
6120          * We may need to remove the name entry firstly, then insert back.
6121          * One credit is for user quota file update.
6122          * One credit is for group quota file update.
6123          * Two credits are for dirty inode. */
6124         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6125                   osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6126
6127         if (dev->od_dirent_journal != 0) {
6128
6129 again:
6130                 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6131                 if (IS_ERR(jh)) {
6132                         rc = PTR_ERR(jh);
6133                         CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6134                                "check_repair, dir = %lu/%u, credits = %d, "
6135                                "name = %.*s, ino = %llu: rc = %d\n",
6136                                devname, dir->i_ino, dir->i_generation, credits,
6137                                ent->oied_namelen, ent->oied_name,
6138                                ent->oied_ino, rc);
6139
6140                         GOTO(out_inode, rc);
6141                 }
6142
6143                 if (obj->oo_hl_head != NULL) {
6144                         hlock = osd_oti_get(env)->oti_hlock;
6145                         /* "0" means exclusive lock for the whole directory.
6146                          * We need to prevent others access such name entry
6147                          * during the delete + insert. Neither HLOCK_ADD nor
6148                          * HLOCK_DEL cannot guarantee the atomicity. */
6149                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6150                 } else {
6151                         down_write(&obj->oo_ext_idx_sem);
6152                 }
6153         } else {
6154                 if (obj->oo_hl_head != NULL) {
6155                         hlock = osd_oti_get(env)->oti_hlock;
6156                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6157                                            LDISKFS_HLOCK_LOOKUP);
6158                 } else {
6159                         down_read(&obj->oo_ext_idx_sem);
6160                 }
6161         }
6162
6163         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6164         if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6165                 *attr |= LUDA_IGNORE;
6166
6167                 GOTO(out, rc = 0);
6168         }
6169
6170         /* For dotdot entry, if there is not enough space to hold the
6171          * FID-in-dirent, just keep them there. It only happens when the
6172          * device upgraded from 1.8 or restored from MDT file-level backup.
6173          * For the whole directory, only dotdot entry have no FID-in-dirent
6174          * and needs to get FID from LMA when readdir, it will not affect the
6175          * performance much. */
6176         if (dotdot && !osd_dotdot_has_space(de)) {
6177                 *attr |= LUDA_UNKNOWN;
6178
6179                 GOTO(out, rc = 0);
6180         }
6181
6182         if (lma != NULL) {
6183                 if (lu_fid_eq(fid, &lma->lma_self_fid))
6184                         GOTO(out, rc = 0);
6185
6186                 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6187                         struct lu_fid *tfid = &lma->lma_self_fid;
6188
6189                         if (likely(dotdot &&
6190                                    fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6191                                    fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6192                                 /* It must be REMOTE_PARENT_DIR and as the
6193                                  * 'dotdot' entry of remote directory */
6194                                 *attr |= LUDA_IGNORE;
6195                         } else {
6196                                 CDEBUG(D_LFSCK, "%s: expect remote agent "
6197                                        "parent directory, but got %.*s under "
6198                                        "dir = %lu/%u with the FID "DFID"\n",
6199                                        devname, ent->oied_namelen,
6200                                        ent->oied_name, dir->i_ino,
6201                                        dir->i_generation, PFID(tfid));
6202
6203                                 *attr |= LUDA_UNKNOWN;
6204                         }
6205
6206                         GOTO(out, rc = 0);
6207                 }
6208         }
6209
6210         if (!fid_is_zero(fid)) {
6211                 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6212                                               ent->oied_namelen);
6213                 if (rc == -ENOENT ||
6214                     (rc == -ENODATA &&
6215                      !(dev->od_scrub.os_file.sf_flags & SF_UPGRADE))) {
6216                         /* linkEA does not recognize the dirent entry,
6217                          * it may because the dirent entry corruption
6218                          * and points to other's inode. */
6219                         CDEBUG(D_LFSCK, "%s: the target inode does not "
6220                                "recognize the dirent, dir = %lu/%u, "
6221                                " name = %.*s, ino = %llu, "
6222                                DFID": rc = %d\n", devname, dir->i_ino,
6223                                dir->i_generation, ent->oied_namelen,
6224                                ent->oied_name, ent->oied_ino, PFID(fid), rc);
6225                         *attr |= LUDA_UNKNOWN;
6226
6227                         GOTO(out, rc = 0);
6228                 }
6229
6230                 if (rc && rc != -ENODATA) {
6231                         CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6232                                "dir = %lu/%u, name = %.*s, ino = %llu, "
6233                                DFID": rc = %d\n", devname, dir->i_ino,
6234                                dir->i_generation, ent->oied_namelen,
6235                                ent->oied_name, ent->oied_ino, PFID(fid), rc);
6236                         *attr |= LUDA_UNKNOWN;
6237
6238                         GOTO(out, rc = 0);
6239                 }
6240         }
6241
6242         if (lma != NULL) {
6243                 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6244                  * valid, trusted, in spite of fid_is_sane(fid) or not. */
6245                 if (*attr & LUDA_VERIFY_DRYRUN) {
6246                         *fid = lma->lma_self_fid;
6247                         *attr |= LUDA_REPAIR;
6248
6249                         GOTO(out, rc = 0);
6250                 }
6251
6252                 if (jh == NULL) {
6253                         brelse(bh);
6254                         dev->od_dirent_journal = 1;
6255                         if (hlock != NULL) {
6256                                 ldiskfs_htree_unlock(hlock);
6257                                 hlock = NULL;
6258                         } else {
6259                                 up_read(&obj->oo_ext_idx_sem);
6260                         }
6261
6262                         goto again;
6263                 }
6264
6265                 *fid = lma->lma_self_fid;
6266                 dirty = true;
6267                 /* Update or append the FID-in-dirent. */
6268                 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6269                                          bh, de, hlock, dotdot);
6270                 if (rc == 0)
6271                         *attr |= LUDA_REPAIR;
6272                 else
6273                         CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6274                                "the dirent, dir = %lu/%u, name = %.*s, "
6275                                "ino = %llu, "DFID": rc = %d\n",
6276                                devname, dir->i_ino, dir->i_generation,
6277                                ent->oied_namelen, ent->oied_name,
6278                                ent->oied_ino, PFID(fid), rc);
6279         } else {
6280                 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6281                 if (*attr & LUDA_VERIFY_DRYRUN) {
6282                         if (fid_is_sane(fid)) {
6283                                 *attr |= LUDA_REPAIR;
6284                         } else if (dev->od_index == 0) {
6285                                 lu_igif_build(fid, inode->i_ino,
6286                                               inode->i_generation);
6287                                 *attr |= LUDA_UPGRADE;
6288                         }
6289
6290                         GOTO(out, rc = 0);
6291                 }
6292
6293                 if (jh == NULL) {
6294                         brelse(bh);
6295                         dev->od_dirent_journal = 1;
6296                         if (hlock != NULL) {
6297                                 ldiskfs_htree_unlock(hlock);
6298                                 hlock = NULL;
6299                         } else {
6300                                 up_read(&obj->oo_ext_idx_sem);
6301                         }
6302
6303                         goto again;
6304                 }
6305
6306                 dirty = true;
6307                 if (unlikely(fid_is_sane(fid))) {
6308                         /* FID-in-dirent exists, but FID-in-LMA is lost.
6309                          * Trust the FID-in-dirent, and add FID-in-LMA. */
6310                         rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6311                         if (rc == 0)
6312                                 *attr |= LUDA_REPAIR;
6313                         else
6314                                 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6315                                        "update dirent, dir = %lu/%u, "
6316                                        "name = %.*s, ino = %llu, "
6317                                        DFID": rc = %d\n",
6318                                        devname, dir->i_ino, dir->i_generation,
6319                                        ent->oied_namelen, ent->oied_name,
6320                                        ent->oied_ino, PFID(fid), rc);
6321                 } else if (dev->od_index == 0) {
6322                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
6323                         /* It is probably IGIF object. Only aappend the
6324                          * FID-in-dirent. OI scrub will process FID-in-LMA. */
6325                         rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6326                                                  bh, de, hlock, dotdot);
6327                         if (rc == 0)
6328                                 *attr |= LUDA_UPGRADE;
6329                         else
6330                                 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6331                                        "after the dirent, dir = %lu/%u, "
6332                                        "name = %.*s, ino = %llu, "
6333                                        DFID": rc = %d\n",
6334                                        devname, dir->i_ino, dir->i_generation,
6335                                        ent->oied_namelen, ent->oied_name,
6336                                        ent->oied_ino, PFID(fid), rc);
6337                 }
6338         }
6339
6340         GOTO(out, rc);
6341
6342 out:
6343         if (!IS_ERR(bh))
6344                 brelse(bh);
6345         if (hlock != NULL) {
6346                 ldiskfs_htree_unlock(hlock);
6347         } else {
6348                 if (dev->od_dirent_journal != 0)
6349                         up_write(&obj->oo_ext_idx_sem);
6350                 else
6351                         up_read(&obj->oo_ext_idx_sem);
6352         }
6353
6354         if (jh != NULL)
6355                 ldiskfs_journal_stop(jh);
6356
6357 out_inode:
6358         iput(inode);
6359         if (rc >= 0 && !dirty)
6360                 dev->od_dirent_journal = 0;
6361
6362         return rc;
6363 }
6364
6365 /**
6366  * Returns the value at current position from iterator's in memory structure.
6367  *
6368  * \param di struct osd_it_ea, iterator's in memory structure
6369  * \param attr attr requested for dirent.
6370  * \param lde lustre dirent
6371  *
6372  * \retval   0 no error and \param lde has correct lustre dirent.
6373  * \retval -ve on error
6374  */
6375 static inline int osd_it_ea_rec(const struct lu_env *env,
6376                                 const struct dt_it *di,
6377                                 struct dt_rec *dtrec, __u32 attr)
6378 {
6379         struct osd_it_ea       *it    = (struct osd_it_ea *)di;
6380         struct osd_object      *obj   = it->oie_obj;
6381         struct osd_device      *dev   = osd_obj2dev(obj);
6382         struct osd_thread_info *oti   = osd_oti_get(env);
6383         struct osd_inode_id    *id    = &oti->oti_id;
6384         struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
6385         struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
6386         __u32                   ino   = it->oie_dirent->oied_ino;
6387         int                     rc    = 0;
6388         ENTRY;
6389
6390         LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6391
6392         if (attr & LUDA_VERIFY) {
6393                 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6394                         attr |= LUDA_IGNORE;
6395                         /* If the parent is on remote MDT, and there
6396                          * is no FID-in-dirent, then we have to get
6397                          * the parent FID from the linkEA.  */
6398                         if (!fid_is_sane(fid) &&
6399                             it->oie_dirent->oied_namelen == 2 &&
6400                             it->oie_dirent->oied_name[0] == '.' &&
6401                             it->oie_dirent->oied_name[1] == '.')
6402                                 osd_get_pfid_from_linkea(env, obj, fid);
6403                 } else {
6404                         rc = osd_dirent_check_repair(env, obj, it, fid, id,
6405                                                      &attr);
6406                 }
6407
6408                 if (!fid_is_sane(fid))
6409                         attr |= LUDA_UNKNOWN;
6410         } else {
6411                 attr &= ~LU_DIRENT_ATTRS_MASK;
6412                 if (!fid_is_sane(fid)) {
6413                         bool is_dotdot = false;
6414                         if (it->oie_dirent->oied_namelen == 2 &&
6415                             it->oie_dirent->oied_name[0] == '.' &&
6416                             it->oie_dirent->oied_name[1] == '.')
6417                                 is_dotdot = true;
6418                         /* If the parent is on remote MDT, and there
6419                          * is no FID-in-dirent, then we have to get
6420                          * the parent FID from the linkEA.  */
6421                         if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6422                                 rc = osd_get_pfid_from_linkea(env, obj, fid);
6423                         } else {
6424                                 if (is_dotdot == false &&
6425                                     OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6426                                         RETURN(-ENOENT);
6427
6428                                 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6429                         }
6430                 } else {
6431                         osd_id_gen(id, ino, OSD_OII_NOGEN);
6432                 }
6433         }
6434
6435         /* Pack the entry anyway, at least the offset is right. */
6436         osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6437                            it->oie_dirent->oied_name,
6438                            it->oie_dirent->oied_namelen,
6439                            it->oie_dirent->oied_type, attr);
6440
6441         if (rc < 0)
6442                 RETURN(rc);
6443
6444         if (osd_remote_fid(env, dev, fid))
6445                 RETURN(0);
6446
6447         if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6448                 osd_add_oi_cache(oti, dev, id, fid);
6449
6450         RETURN(rc > 0 ? 0 : rc);
6451 }
6452
6453 /**
6454  * Returns the record size size at current position.
6455  *
6456  * This function will return record(lu_dirent) size in bytes.
6457  *
6458  * \param[in] env       execution environment
6459  * \param[in] di        iterator's in memory structure
6460  * \param[in] attr      attribute of the entry, only requires LUDA_TYPE to
6461  *                      calculate the lu_dirent size.
6462  *
6463  * \retval      record size(in bytes & in memory) of the current lu_dirent
6464  *              entry.
6465  */
6466 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6467                               __u32 attr)
6468 {
6469         struct osd_it_ea *it = (struct osd_it_ea *)di;
6470
6471         return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6472 }
6473
6474 /**
6475  * Returns a cookie for current position of the iterator head, so that
6476  * user can use this cookie to load/start the iterator next time.
6477  *
6478  * \param di iterator's in memory structure
6479  *
6480  * \retval cookie for current position, on success
6481  */
6482 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6483 {
6484         struct osd_it_ea *it = (struct osd_it_ea *)di;
6485
6486         return it->oie_dirent->oied_off;
6487 }
6488
6489 /**
6490  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6491  * to load a directory entry at a time and stored it i inn,
6492  * in iterator's in-memory data structure.
6493  *
6494  * \param di struct osd_it_ea, iterator's in memory structure
6495  *
6496  * \retval +ve on success
6497  * \retval -ve on error
6498  */
6499 static int osd_it_ea_load(const struct lu_env *env,
6500                           const struct dt_it *di, __u64 hash)
6501 {
6502         struct osd_it_ea *it = (struct osd_it_ea *)di;
6503         int rc;
6504
6505         ENTRY;
6506         it->oie_file.f_pos = hash;
6507
6508         rc =  osd_ldiskfs_it_fill(env, di);
6509         if (rc > 0)
6510                 rc = -ENODATA;
6511
6512         if (rc == 0)
6513                 rc = +1;
6514
6515         RETURN(rc);
6516 }
6517
6518 /**
6519  * Index lookup function for interoperability mode (b11826).
6520  *
6521  * \param key,  key i.e. file name to be searched
6522  *
6523  * \retval +ve, on success
6524  * \retval -ve, on error
6525  */
6526 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6527                                struct dt_rec *rec, const struct dt_key *key)
6528 {
6529         struct osd_object *obj = osd_dt_obj(dt);
6530         int rc = 0;
6531
6532         ENTRY;
6533
6534         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6535         LINVRNT(osd_invariant(obj));
6536
6537         rc = osd_ea_lookup_rec(env, obj, rec, key);
6538         if (rc == 0)
6539                 rc = +1;
6540         RETURN(rc);
6541 }
6542
6543 /**
6544  * Index and Iterator operations for interoperability
6545  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6546  */
6547 static const struct dt_index_operations osd_index_ea_ops = {
6548         .dio_lookup         = osd_index_ea_lookup,
6549         .dio_declare_insert = osd_index_declare_ea_insert,
6550         .dio_insert         = osd_index_ea_insert,
6551         .dio_declare_delete = osd_index_declare_ea_delete,
6552         .dio_delete         = osd_index_ea_delete,
6553         .dio_it     = {
6554                 .init     = osd_it_ea_init,
6555                 .fini     = osd_it_ea_fini,
6556                 .get      = osd_it_ea_get,
6557                 .put      = osd_it_ea_put,
6558                 .next     = osd_it_ea_next,
6559                 .key      = osd_it_ea_key,
6560                 .key_size = osd_it_ea_key_size,
6561                 .rec      = osd_it_ea_rec,
6562                 .rec_size = osd_it_ea_rec_size,
6563                 .store    = osd_it_ea_store,
6564                 .load     = osd_it_ea_load
6565         }
6566 };
6567
6568 static void *osd_key_init(const struct lu_context *ctx,
6569                           struct lu_context_key *key)
6570 {
6571         struct osd_thread_info *info;
6572
6573         OBD_ALLOC_PTR(info);
6574         if (info == NULL)
6575                 return ERR_PTR(-ENOMEM);
6576
6577         OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6578         if (info->oti_it_ea_buf == NULL)
6579                 goto out_free_info;
6580
6581         info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6582
6583         info->oti_hlock = ldiskfs_htree_lock_alloc();
6584         if (info->oti_hlock == NULL)
6585                 goto out_free_ea;
6586
6587         return info;
6588
6589  out_free_ea:
6590         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6591  out_free_info:
6592         OBD_FREE_PTR(info);
6593         return ERR_PTR(-ENOMEM);
6594 }
6595
6596 static void osd_key_fini(const struct lu_context *ctx,
6597                          struct lu_context_key *key, void* data)
6598 {
6599         struct osd_thread_info *info = data;
6600         struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6601         struct osd_idmap_cache  *idc = info->oti_ins_cache;
6602
6603         if (info->oti_inode != NULL)
6604                 OBD_FREE_PTR(lli);
6605         if (info->oti_hlock != NULL)
6606                 ldiskfs_htree_lock_free(info->oti_hlock);
6607         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6608         lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6609         lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6610         lu_buf_free(&info->oti_big_buf);
6611         if (idc != NULL) {
6612                 LASSERT(info->oti_ins_cache_size > 0);
6613                 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6614                 info->oti_ins_cache = NULL;
6615                 info->oti_ins_cache_size = 0;
6616         }
6617         OBD_FREE_PTR(info);
6618 }
6619
6620 static void osd_key_exit(const struct lu_context *ctx,
6621                          struct lu_context_key *key, void *data)
6622 {
6623         struct osd_thread_info *info = data;
6624
6625         LASSERT(info->oti_r_locks == 0);
6626         LASSERT(info->oti_w_locks == 0);
6627         LASSERT(info->oti_txns    == 0);
6628 }
6629
6630 /* type constructor/destructor: osd_type_init, osd_type_fini */
6631 LU_TYPE_INIT_FINI(osd, &osd_key);
6632
6633 struct lu_context_key osd_key = {
6634         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6635         .lct_init = osd_key_init,
6636         .lct_fini = osd_key_fini,
6637         .lct_exit = osd_key_exit
6638 };
6639
6640
6641 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6642                            const char *name, struct lu_device *next)
6643 {
6644         struct osd_device *osd = osd_dev(d);
6645
6646         if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6647             >= sizeof(osd->od_svname))
6648                 return -E2BIG;
6649         return osd_procfs_init(osd, name);
6650 }
6651
6652 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6653 {
6654         struct seq_server_site  *ss = osd_seq_site(osd);
6655         int                     rc;
6656         ENTRY;
6657
6658         if (osd->od_is_ost || osd->od_cl_seq != NULL)
6659                 RETURN(0);
6660
6661         if (unlikely(ss == NULL))
6662                 RETURN(-ENODEV);
6663
6664         OBD_ALLOC_PTR(osd->od_cl_seq);
6665         if (osd->od_cl_seq == NULL)
6666                 RETURN(-ENOMEM);
6667
6668         rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6669                              osd->od_svname, ss->ss_server_seq);
6670         if (rc != 0) {
6671                 OBD_FREE_PTR(osd->od_cl_seq);
6672                 osd->od_cl_seq = NULL;
6673                 RETURN(rc);
6674         }
6675
6676         if (ss->ss_node_id == 0) {
6677                 /* If the OSD on the sequence controller(MDT0), then allocate
6678                  * sequence here, otherwise allocate sequence after connected
6679                  * to MDT0 (see mdt_register_lwp_callback()). */
6680                 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6681                                    &osd->od_cl_seq->lcs_space, env);
6682         }
6683
6684         RETURN(rc);
6685 }
6686
6687 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6688 {
6689         if (osd->od_cl_seq == NULL)
6690                 return;
6691
6692         seq_client_fini(osd->od_cl_seq);
6693         OBD_FREE_PTR(osd->od_cl_seq);
6694         osd->od_cl_seq = NULL;
6695 }
6696
6697 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6698 {
6699         ENTRY;
6700
6701         /* shutdown quota slave instance associated with the device */
6702         if (o->od_quota_slave != NULL) {
6703                 qsd_fini(env, o->od_quota_slave);
6704                 o->od_quota_slave = NULL;
6705         }
6706
6707         osd_fid_fini(env, o);
6708
6709         RETURN(0);
6710 }
6711
6712 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6713 {
6714         ENTRY;
6715
6716         if (o->od_mnt != NULL) {
6717                 shrink_dcache_sb(osd_sb(o));
6718                 osd_sync(env, &o->od_dt_dev);
6719
6720                 mntput(o->od_mnt);
6721                 o->od_mnt = NULL;
6722         }
6723
6724         EXIT;
6725 }
6726
6727 static int osd_mount(const struct lu_env *env,
6728                      struct osd_device *o, struct lustre_cfg *cfg)
6729 {
6730         const char              *name  = lustre_cfg_string(cfg, 0);
6731         const char              *dev  = lustre_cfg_string(cfg, 1);
6732         const char              *opts;
6733         unsigned long            page, s_flags, lmd_flags = 0;
6734         struct page             *__page;
6735         struct file_system_type *type;
6736         char                    *options = NULL;
6737         char                    *str;
6738         struct osd_thread_info  *info = osd_oti_get(env);
6739         struct lu_fid           *fid = &info->oti_fid;
6740         struct inode            *inode;
6741         int                      rc = 0, force_over_256tb = 0;
6742         ENTRY;
6743
6744         if (o->od_mnt != NULL)
6745                 RETURN(0);
6746
6747         if (strlen(dev) >= sizeof(o->od_mntdev))
6748                 RETURN(-E2BIG);
6749         strcpy(o->od_mntdev, dev);
6750
6751         str = lustre_cfg_string(cfg, 2);
6752         s_flags = simple_strtoul(str, NULL, 0);
6753         str = strstr(str, ":");
6754         if (str)
6755                 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6756         opts = lustre_cfg_string(cfg, 3);
6757 #ifdef __BIG_ENDIAN
6758         if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6759                 CERROR("%s: device %s extents feature is not guaranteed to "
6760                        "work on big-endian systems. Use \"bigendian_extents\" "
6761                        "mount option to override.\n", name, dev);
6762                 RETURN(-EINVAL);
6763         }
6764 #endif
6765         if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6766                 CWARN("force_over_128tb option is depricated."
6767                       "Filesystems less then 256TB can be created without any"
6768                       "force options. Use force_over_256tb option for"
6769                       "filesystems greather then 256TB.\n");
6770         }
6771
6772         if (opts != NULL && strstr(opts, "force_over_256tb") != NULL)
6773                 force_over_256tb = 1;
6774
6775         __page = alloc_page(GFP_KERNEL);
6776         if (__page == NULL)
6777                 GOTO(out, rc = -ENOMEM);
6778         page = (unsigned long)page_address(__page);
6779         options = (char *)page;
6780         *options = '\0';
6781         if (opts != NULL) {
6782                 /* strip out the options for back compatiblity */
6783                 static char *sout[] = {
6784                         "mballoc",
6785                         "iopen",
6786                         "noiopen",
6787                         "iopen_nopriv",
6788                         "extents",
6789                         "noextents",
6790                         /* strip out option we processed in osd */
6791                         "bigendian_extents",
6792 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(3,0,53,0)
6793 #warning "remove force_over_128 option"
6794 #else
6795                         "force_over_128tb (deprecated)",
6796 #endif
6797                         "force_over_256tb",
6798                         NULL
6799                 };
6800                 strcat(options, opts);
6801                 for (rc = 0, str = options; sout[rc]; ) {
6802                         char *op = strstr(str, sout[rc]);
6803                         if (op == NULL) {
6804                                 rc++;
6805                                 str = options;
6806                                 continue;
6807                         }
6808                         if (op == options || *(op - 1) == ',') {
6809                                 str = op + strlen(sout[rc]);
6810                                 if (*str == ',' || *str == '\0') {
6811                                         *str == ',' ? str++ : str;
6812                                         memmove(op, str, strlen(str) + 1);
6813                                 }
6814                         }
6815                         for (str = op; *str != ',' && *str != '\0'; str++)
6816                                 ;
6817                 }
6818         } else {
6819                 strncat(options, "user_xattr,acl", 14);
6820         }
6821
6822         /* Glom up mount options */
6823         if (*options != '\0')
6824                 strcat(options, ",");
6825         strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6826
6827         type = get_fs_type("ldiskfs");
6828         if (!type) {
6829                 CERROR("%s: cannot find ldiskfs module\n", name);
6830                 GOTO(out, rc = -ENODEV);
6831         }
6832
6833         o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6834         module_put(type->owner);
6835
6836         if (IS_ERR(o->od_mnt)) {
6837                 rc = PTR_ERR(o->od_mnt);
6838                 o->od_mnt = NULL;
6839                 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6840                 GOTO(out, rc);
6841         }
6842
6843         if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (64ULL << 30) &&
6844             force_over_256tb == 0) {
6845                 CERROR("%s: device %s LDISKFS does not support filesystems "
6846                        "greater than 256TB and can cause data corruption. "
6847                        "Use \"force_over_256tb\" 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);