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