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