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