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