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