Whamcloud - gitweb
LU-13783 osd-ldiskfs: use alloc_file_pseudo to create fake files
[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
68 #include "osd_internal.h"
69 #include "osd_dynlocks.h"
70
71 /* llo_* api support */
72 #include <md_object.h>
73 #include <lustre_quota.h>
74
75 #include <lustre_linkea.h>
76
77 /* encoding routines */
78 #include <lustre_crypto.h>
79
80 /* Maximum EA size is limited by LNET_MTU for remote objects */
81 #define OSD_MAX_EA_SIZE 1048364
82
83 int ldiskfs_pdo = 1;
84 module_param(ldiskfs_pdo, int, 0644);
85 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
86
87 int ldiskfs_track_declares_assert;
88 module_param(ldiskfs_track_declares_assert, int, 0644);
89 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
90
91 /* Slab to allocate dynlocks */
92 struct kmem_cache *dynlock_cachep;
93
94 /* Slab to allocate osd_it_ea */
95 struct kmem_cache *osd_itea_cachep;
96
97 static struct lu_kmem_descr ldiskfs_caches[] = {
98         {
99                 .ckd_cache = &dynlock_cachep,
100                 .ckd_name  = "dynlock_cache",
101                 .ckd_size  = sizeof(struct dynlock_handle)
102         },
103         {
104                 .ckd_cache = &osd_itea_cachep,
105                 .ckd_name  = "osd_itea_cache",
106                 .ckd_size  = sizeof(struct osd_it_ea)
107         },
108         {
109                 .ckd_cache = NULL
110         }
111 };
112
113 static const char dot[] = ".";
114 static const char dotdot[] = "..";
115
116 static const struct lu_object_operations      osd_lu_obj_ops;
117 static const struct dt_object_operations      osd_obj_ops;
118 static const struct dt_object_operations      osd_obj_otable_it_ops;
119 static const struct dt_index_operations       osd_index_iam_ops;
120 static const struct dt_index_operations       osd_index_ea_ops;
121
122 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
123                           const struct lu_fid *fid);
124 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
125                                                 struct osd_device *osd);
126
127 int osd_trans_declare_op2rb[] = {
128         [OSD_OT_ATTR_SET]       = OSD_OT_ATTR_SET,
129         [OSD_OT_PUNCH]          = OSD_OT_MAX,
130         [OSD_OT_XATTR_SET]      = OSD_OT_XATTR_SET,
131         [OSD_OT_CREATE]         = OSD_OT_DESTROY,
132         [OSD_OT_DESTROY]        = OSD_OT_CREATE,
133         [OSD_OT_REF_ADD]        = OSD_OT_REF_DEL,
134         [OSD_OT_REF_DEL]        = OSD_OT_REF_ADD,
135         [OSD_OT_WRITE]          = OSD_OT_WRITE,
136         [OSD_OT_INSERT]         = OSD_OT_DELETE,
137         [OSD_OT_DELETE]         = OSD_OT_INSERT,
138         [OSD_OT_QUOTA]          = OSD_OT_MAX,
139 };
140
141 static int osd_has_index(const struct osd_object *obj)
142 {
143         return obj->oo_dt.do_index_ops != NULL;
144 }
145
146 static int osd_object_invariant(const struct lu_object *l)
147 {
148         return osd_invariant(osd_obj(l));
149 }
150
151 /*
152  * Concurrency: doesn't matter
153  */
154 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
155 {
156         struct osd_thread_info *oti = osd_oti_get(env);
157
158         return oti->oti_w_locks > 0 && o->oo_owner == env;
159 }
160
161 /*
162  * Concurrency: doesn't access mutable data
163  */
164 static int osd_root_get(const struct lu_env *env,
165                         struct dt_device *dev, struct lu_fid *f)
166 {
167         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
168         return 0;
169 }
170
171 /*
172  * the following set of functions are used to maintain per-thread
173  * cache of FID->ino mapping. this mechanism is needed to resolve
174  * FID to inode at dt_insert() which in turn stores ino in the
175  * directory entries to keep ldiskfs compatible with ext[34].
176  * due to locking-originated restrictions we can't lookup ino
177  * using LU cache (deadlock is possible). lookup using OI is quite
178  * expensive. so instead we maintain this cache and methods like
179  * dt_create() fill it. so in the majority of cases dt_insert() is
180  * able to find needed mapping in lockless manner.
181  */
182 static struct osd_idmap_cache *
183 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
184              const struct lu_fid *fid)
185 {
186         struct osd_thread_info *oti = osd_oti_get(env);
187         struct osd_idmap_cache *idc = oti->oti_ins_cache;
188         int i;
189
190         for (i = 0; i < oti->oti_ins_cache_used; i++) {
191                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
192                         continue;
193                 if (idc[i].oic_dev != osd)
194                         continue;
195
196                 return idc + i;
197         }
198
199         return NULL;
200 }
201
202 static struct osd_idmap_cache *
203 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
204             const struct lu_fid *fid)
205 {
206         struct osd_thread_info *oti   = osd_oti_get(env);
207         struct osd_idmap_cache *idc;
208         int i;
209
210         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
211                 i = oti->oti_ins_cache_size * 2;
212                 if (i == 0)
213                         i = OSD_INS_CACHE_SIZE;
214                 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
215                 if (idc == NULL)
216                         return ERR_PTR(-ENOMEM);
217                 if (oti->oti_ins_cache != NULL) {
218                         memcpy(idc, oti->oti_ins_cache,
219                                oti->oti_ins_cache_used * sizeof(*idc));
220                         OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
221                                            oti->oti_ins_cache_used);
222                 }
223                 oti->oti_ins_cache = idc;
224                 oti->oti_ins_cache_size = i;
225         }
226
227         idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
228         idc->oic_fid = *fid;
229         idc->oic_dev = osd;
230         idc->oic_lid.oii_ino = 0;
231         idc->oic_lid.oii_gen = 0;
232         idc->oic_remote = 0;
233
234         return idc;
235 }
236
237 /*
238  * lookup mapping for the given fid in the cache, initialize a
239  * new one if not found. the initialization checks whether the
240  * object is local or remote. for local objects, OI is used to
241  * learn ino/generation. the function is used when the caller
242  * has no information about the object, e.g. at dt_insert().
243  */
244 static struct osd_idmap_cache *
245 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
246                      const struct lu_fid *fid)
247 {
248         struct osd_idmap_cache *idc;
249         int rc;
250
251         idc = osd_idc_find(env, osd, fid);
252         LASSERT(!IS_ERR(idc));
253         if (idc != NULL)
254                 return idc;
255
256         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
257                osd->od_svname, PFID(fid));
258
259         /* new mapping is needed */
260         idc = osd_idc_add(env, osd, fid);
261         if (IS_ERR(idc)) {
262                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
263                        osd->od_svname, PFID(fid), PTR_ERR(idc));
264                 return idc;
265         }
266
267         /* initialize it */
268         rc = osd_remote_fid(env, osd, fid);
269         if (unlikely(rc < 0))
270                 return ERR_PTR(rc);
271
272         if (rc == 0) {
273                 /* the object is local, lookup in OI */
274                 /* XXX: probably cheaper to lookup in LU first? */
275                 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
276                                    &idc->oic_lid, 0);
277                 if (unlikely(rc < 0)) {
278                         CERROR("can't lookup: rc = %d\n", rc);
279                         return ERR_PTR(rc);
280                 }
281         } else {
282                 /* the object is remote */
283                 idc->oic_remote = 1;
284         }
285
286         return idc;
287 }
288
289 static void osd_idc_dump_lma(const struct lu_env *env,
290                                 struct osd_device *osd,
291                                 unsigned long ino,
292                                 bool check_in_oi)
293 {
294         struct osd_thread_info *info = osd_oti_get(env);
295         struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
296         const struct lu_fid *fid;
297         struct osd_inode_id lid;
298         struct inode *inode;
299         int rc;
300
301         inode = osd_ldiskfs_iget(osd_sb(osd), ino);
302         if (IS_ERR(inode)) {
303                 CERROR("%s: can't get inode %lu: rc = %d\n",
304                        osd->od_svname, ino, (int)PTR_ERR(inode));
305                 return;
306         }
307         if (is_bad_inode(inode)) {
308                 CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
309                 goto put;
310         }
311         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
312         if (rc) {
313                 CERROR("%s: can't get LMA for %lu: rc = %d\n",
314                        osd->od_svname, ino, rc);
315                 goto put;
316         }
317         fid = &loa->loa_lma.lma_self_fid;
318         LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
319                       PFID(fid), ino, (unsigned)inode->i_generation);
320         if (!check_in_oi)
321                 goto put;
322         rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
323         if (rc) {
324                 CERROR("%s: can't lookup "DFID": rc = %d\n",
325                        osd->od_svname, PFID(fid), rc);
326                 goto put;
327         }
328         LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
329                       PFID(fid), lid.oii_ino, lid.oii_gen);
330 put:
331         iput(inode);
332 }
333
334 static void osd_idc_dump_debug(const struct lu_env *env,
335                                 struct osd_device *osd,
336                                 const struct lu_fid *fid,
337                                 unsigned long ino1,
338                                 unsigned long ino2)
339 {
340         struct osd_inode_id lid;
341
342         int rc;
343
344         rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
345         if (!rc) {
346                 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
347                         osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
348                 osd_idc_dump_lma(env, osd, lid.oii_ino, false);
349         } else {
350                 CERROR("%s: can't lookup "DFID": rc = %d\n",
351                        osd->od_svname, PFID(fid), rc);
352         }
353         if (ino1)
354                 osd_idc_dump_lma(env, osd, ino1, true);
355         if (ino2)
356                 osd_idc_dump_lma(env, osd, ino2, true);
357 }
358
359 /*
360  * lookup mapping for given FID and fill it from the given object.
361  * the object is lolcal by definition.
362  */
363 static int osd_idc_find_and_init(const struct lu_env *env,
364                                  struct osd_device *osd,
365                                  struct osd_object *obj)
366 {
367         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
368         struct osd_idmap_cache *idc;
369
370         idc = osd_idc_find(env, osd, fid);
371         LASSERT(!IS_ERR(idc));
372         if (idc != NULL) {
373                 if (obj->oo_inode == NULL)
374                         return 0;
375                 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
376                         if (idc->oic_lid.oii_ino) {
377                                 osd_idc_dump_debug(env, osd, fid,
378                                                    idc->oic_lid.oii_ino,
379                                                    obj->oo_inode->i_ino);
380                                 return -EINVAL;
381                         }
382                         idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
383                         idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
384                 }
385                 return 0;
386         }
387
388         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
389                osd->od_svname, PFID(fid));
390
391         /* new mapping is needed */
392         idc = osd_idc_add(env, osd, fid);
393         if (IS_ERR(idc)) {
394                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
395                        osd->od_svname, PFID(fid), PTR_ERR(idc));
396                 return PTR_ERR(idc);
397         }
398
399         if (obj->oo_inode != NULL) {
400                 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
401                 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
402         }
403         return 0;
404 }
405
406 /*
407  * OSD object methods.
408  */
409
410 /*
411  * Concurrency: no concurrent access is possible that early in object
412  * life-cycle.
413  */
414 static struct lu_object *osd_object_alloc(const struct lu_env *env,
415                                           const struct lu_object_header *hdr,
416                                           struct lu_device *d)
417 {
418         struct osd_object *mo;
419
420         OBD_ALLOC_PTR(mo);
421         if (mo != NULL) {
422                 struct lu_object *l;
423                 struct lu_object_header *h;
424                 struct osd_device *o = osd_dev(d);
425
426                 l = &mo->oo_dt.do_lu;
427                 if (unlikely(o->od_in_init)) {
428                         OBD_ALLOC_PTR(h);
429                         if (!h) {
430                                 OBD_FREE_PTR(mo);
431                                 return NULL;
432                         }
433
434                         lu_object_header_init(h);
435                         lu_object_init(l, h, d);
436                         lu_object_add_top(h, l);
437                         mo->oo_header = h;
438                 } else {
439                         dt_object_init(&mo->oo_dt, NULL, d);
440                         mo->oo_header = NULL;
441                 }
442
443                 mo->oo_dt.do_ops = &osd_obj_ops;
444                 l->lo_ops = &osd_lu_obj_ops;
445                 init_rwsem(&mo->oo_sem);
446                 init_rwsem(&mo->oo_ext_idx_sem);
447                 spin_lock_init(&mo->oo_guard);
448                 INIT_LIST_HEAD(&mo->oo_xattr_list);
449                 return l;
450         }
451         return NULL;
452 }
453
454 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
455                 struct dentry *dentry, struct lustre_ost_attrs *loa)
456 {
457         int rc;
458
459         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
460                              (void *)loa, sizeof(*loa));
461         if (rc > 0) {
462                 struct lustre_mdt_attrs *lma = &loa->loa_lma;
463
464                 if (rc < sizeof(*lma))
465                         return -EINVAL;
466
467                 rc = 0;
468                 lustre_loa_swab(loa, true);
469                 /* Check LMA compatibility */
470                 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
471                         rc = -EOPNOTSUPP;
472                         CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
473                               osd_ino2name(inode),
474                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
475                               PFID(&lma->lma_self_fid), inode->i_ino, rc);
476                 }
477         } else if (rc == 0) {
478                 rc = -ENODATA;
479         }
480
481         return rc;
482 }
483
484 /*
485  * retrieve object from backend ext fs.
486  **/
487 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
488                        struct osd_inode_id *id)
489 {
490         int rc;
491         struct inode *inode = NULL;
492
493         /*
494          * if we look for an inode withing a running
495          * transaction, then we risk to deadlock
496          * osd_dirent_check_repair() breaks this
497          */
498          /* LASSERT(current->journal_info == NULL); */
499
500         inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
501         if (IS_ERR(inode)) {
502                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
503                        id->oii_ino, PTR_ERR(inode));
504         } else if (id->oii_gen != OSD_OII_NOGEN &&
505                    inode->i_generation != id->oii_gen) {
506                 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
507                        "i_generation = %u\n",
508                        id->oii_ino, id->oii_gen, inode->i_generation);
509                 iput(inode);
510                 inode = ERR_PTR(-ESTALE);
511         } else if (inode->i_nlink == 0) {
512                 /*
513                  * due to parallel readdir and unlink,
514                  * we can have dead inode here.
515                  */
516                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
517                 iput(inode);
518                 inode = ERR_PTR(-ESTALE);
519         } else if (is_bad_inode(inode)) {
520                 rc = -ENOENT;
521                 CWARN("%s: bad inode: ino = %u: rc = %d\n",
522                       osd_dev2name(dev), id->oii_ino, rc);
523                 iput(inode);
524                 inode = ERR_PTR(rc);
525         } else if ((rc = osd_attach_jinode(inode))) {
526                 iput(inode);
527                 inode = ERR_PTR(rc);
528         } else {
529                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
530                 if (id->oii_gen == OSD_OII_NOGEN)
531                         osd_id_gen(id, inode->i_ino, inode->i_generation);
532
533                 /*
534                  * Do not update file c/mtime in ldiskfs.
535                  * NB: we don't have any lock to protect this because we don't
536                  * have reference on osd_object now, but contention with
537                  * another lookup + attr_set can't happen in the tiny window
538                  * between if (...) and set S_NOCMTIME.
539                  */
540                 if (!(inode->i_flags & S_NOCMTIME))
541                         inode->i_flags |= S_NOCMTIME;
542         }
543         return inode;
544 }
545
546 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
547                           handle_t *handle, struct dentry *child,
548                           struct inode *inode, struct htree_lock *hlock)
549 {
550         int rc, rc2;
551
552         rc = __ldiskfs_add_entry(handle, child, inode, hlock);
553         if (rc == -ENOBUFS || rc == -ENOSPC) {
554                 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
555                 struct inode *parent = child->d_parent->d_inode;
556                 struct lu_fid *fid = NULL;
557                 char fidstr[FID_LEN + 1] = "unknown";
558
559                 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
560                 if (!rc2) {
561                         fid = &loa->loa_lma.lma_self_fid;
562                 } else if (rc2 == -ENODATA) {
563                         if (unlikely(is_root_inode(parent))) {
564                                 fid = &info->oti_fid3;
565                                 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
566                         } else if (!osd->od_is_ost && osd->od_index == 0) {
567                                 fid = &info->oti_fid3;
568                                 lu_igif_build(fid, parent->i_ino,
569                                               parent->i_generation);
570                         }
571                 }
572
573                 if (fid != NULL)
574                         snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
575
576                 /* below message is checked in sanity.sh test_129 */
577                 if (rc == -ENOSPC) {
578                         CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
579                               osd_name(osd), parent->i_ino, fidstr);
580                 } else {
581                         rc = 0; /* ignore such error now */
582                         CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
583                               osd_name(osd), parent->i_ino, fidstr);
584                 }
585
586         }
587
588         return rc;
589 }
590
591
592 struct inode *
593 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
594              struct osd_inode_id *id, struct lu_fid *fid)
595 {
596         struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
597         struct inode *inode;
598         int rc;
599
600         inode = osd_iget(info, dev, id);
601         if (IS_ERR(inode))
602                 return inode;
603
604         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
605         if (!rc) {
606                 *fid = loa->loa_lma.lma_self_fid;
607         } else if (rc == -ENODATA) {
608                 if (unlikely(is_root_inode(inode)))
609                         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
610                 else
611                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
612         } else {
613                 iput(inode);
614                 inode = ERR_PTR(rc);
615         }
616         return inode;
617 }
618
619 static struct inode *osd_iget_check(struct osd_thread_info *info,
620                                     struct osd_device *dev,
621                                     const struct lu_fid *fid,
622                                     struct osd_inode_id *id,
623                                     bool trusted)
624 {
625         struct inode *inode;
626         int rc = 0;
627
628         ENTRY;
629
630         /*
631          * The cached OI mapping is trustable. If we cannot locate the inode
632          * via the cached OI mapping, then return the failure to the caller
633          * directly without further OI checking.
634          */
635
636 again:
637         inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
638         if (IS_ERR(inode)) {
639                 rc = PTR_ERR(inode);
640                 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
641                         goto check_oi;
642
643                 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
644                        PFID(fid), id->oii_ino, rc);
645                 GOTO(put, rc);
646         }
647
648         if (is_bad_inode(inode)) {
649                 rc = -ENOENT;
650                 if (!trusted)
651                         goto check_oi;
652
653                 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
654                        PFID(fid), id->oii_ino);
655                 GOTO(put, rc);
656         }
657
658         if (id->oii_gen != OSD_OII_NOGEN &&
659             inode->i_generation != id->oii_gen) {
660                 rc = -ESTALE;
661                 if (!trusted)
662                         goto check_oi;
663
664                 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
665                        "oii_gen = %u, i_generation = %u\n", PFID(fid),
666                        id->oii_ino, id->oii_gen, inode->i_generation);
667                 GOTO(put, rc);
668         }
669
670         if (inode->i_nlink == 0) {
671                 rc = -ENOENT;
672                 if (!trusted)
673                         goto check_oi;
674
675                 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
676                        PFID(fid), id->oii_ino);
677                 GOTO(put, rc);
678         }
679
680         ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
681
682 check_oi:
683         if (rc != 0) {
684                 __u32 saved_ino = id->oii_ino;
685                 __u32 saved_gen = id->oii_gen;
686
687                 LASSERT(!trusted);
688                 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
689
690                 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
691                 /*
692                  * XXX: There are four possible cases:
693                  *      1. rc = 0.
694                  *         Backup/restore caused the OI invalid.
695                  *      2. rc = 0.
696                  *         Someone unlinked the object but NOT removed
697                  *         the OI mapping, such as mount target device
698                  *         as ldiskfs, and modify something directly.
699                  *      3. rc = -ENOENT.
700                  *         Someone just removed the object between the
701                  *         former oi_lookup and the iget. It is normal.
702                  *      4. Other failure cases.
703                  *
704                  *      Generally, when the device is mounted, it will
705                  *      auto check whether the system is restored from
706                  *      file-level backup or not. We trust such detect
707                  *      to distinguish the 1st case from the 2nd case:
708                  *      if the OI files are consistent but may contain
709                  *      stale OI mappings because of case 2, if iget()
710                  *      returns -ENOENT or -ESTALE, then it should be
711                  *      the case 2.
712                  */
713                 if (rc != 0)
714                         /*
715                          * If the OI mapping was in OI file before the
716                          * osd_iget_check(), but now, it is disappear,
717                          * then it must be removed by race. That is a
718                          * normal race case.
719                          */
720                         GOTO(put, rc);
721
722                 /*
723                  * It is the OI scrub updated the OI mapping by race.
724                  * The new OI mapping must be valid.
725                  */
726                 if (saved_ino != id->oii_ino ||
727                     (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
728                         if (!IS_ERR(inode))
729                                 iput(inode);
730
731                         trusted = true;
732                         goto again;
733                 }
734
735                 if (IS_ERR(inode)) {
736                         if (dev->od_scrub.os_scrub.os_file.sf_flags &
737                             SF_INCONSISTENT)
738                                 /*
739                                  * It still can be the case 2, but we cannot
740                                  * distinguish it from the case 1. So return
741                                  * -EREMCHG to block current operation until
742                                  *  OI scrub rebuilt the OI mappings.
743                                  */
744                                 rc = -EREMCHG;
745                         else
746                                 rc = -ENOENT;
747
748                         GOTO(put, rc);
749                 }
750
751                 if (inode->i_generation == id->oii_gen)
752                         rc = -ENOENT;
753                 else
754                         rc = -EREMCHG;
755         } else {
756                 if (id->oii_gen == OSD_OII_NOGEN)
757                         osd_id_gen(id, inode->i_ino, inode->i_generation);
758
759                 /*
760                  * Do not update file c/mtime in ldiskfs.
761                  * NB: we don't have any lock to protect this because we don't
762                  * have reference on osd_object now, but contention with
763                  * another lookup + attr_set can't happen in the tiny window
764                  * between if (...) and set S_NOCMTIME.
765                  */
766                 if (!(inode->i_flags & S_NOCMTIME))
767                         inode->i_flags |= S_NOCMTIME;
768         }
769
770         GOTO(put, rc);
771
772 put:
773         if (rc != 0) {
774                 if (!IS_ERR(inode))
775                         iput(inode);
776
777                 inode = ERR_PTR(rc);
778         }
779
780         return inode;
781 }
782
783 /**
784  * \retval +v: new filter_fid does not contain self-fid
785  * \retval 0:  filter_fid_18_23, contains self-fid
786  * \retval -v: other failure cases
787  */
788 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
789                  struct dentry *dentry, struct lu_fid *fid)
790 {
791         struct filter_fid *ff = &info->oti_ff;
792         struct ost_id *ostid = &info->oti_ostid;
793         int rc;
794
795         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
796         if (rc == sizeof(struct filter_fid_18_23)) {
797                 struct filter_fid_18_23 *ff_old = (void *)ff;
798
799                 ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
800                 rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
801                 /*
802                  * XXX: use 0 as the index for compatibility, the caller will
803                  * handle index related issues when necessary.
804                  */
805                 if (!rc)
806                         ostid_to_fid(fid, ostid, 0);
807         } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
808                 rc = 1;
809         } else if (rc >= 0) {
810                 rc = -EINVAL;
811         }
812
813         return rc;
814 }
815
816 static int osd_lma_self_repair(struct osd_thread_info *info,
817                                struct osd_device *osd, struct inode *inode,
818                                const struct lu_fid *fid, __u32 compat)
819 {
820         handle_t *jh;
821         int rc;
822
823         LASSERT(current->journal_info == NULL);
824
825         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
826                                   osd_dto_credits_noquota[DTO_XATTR_SET]);
827         if (IS_ERR(jh)) {
828                 rc = PTR_ERR(jh);
829                 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
830                       osd_name(osd), rc);
831                 return rc;
832         }
833
834         rc = osd_ea_fid_set(info, inode, fid, compat, 0);
835         if (rc != 0)
836                 CWARN("%s: cannot self repair the LMA: rc = %d\n",
837                       osd_name(osd), rc);
838         ldiskfs_journal_stop(jh);
839         return rc;
840 }
841
842 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
843 {
844         struct osd_thread_info *info = osd_oti_get(env);
845         struct osd_device *osd = osd_obj2dev(obj);
846         struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
847         struct lustre_mdt_attrs *lma = &loa->loa_lma;
848         struct inode *inode = obj->oo_inode;
849         struct dentry *dentry = &info->oti_obj_dentry;
850         struct lu_fid *fid = NULL;
851         const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
852         int rc;
853
854         ENTRY;
855
856         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
857                              (void *)loa, sizeof(*loa));
858         if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
859                 fid = &lma->lma_self_fid;
860                 rc = osd_get_idif(info, inode, dentry, fid);
861                 if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
862                         /*
863                          * For the given OST-object, if it has neither LMA nor
864                          * FID in XATTR_NAME_FID, then the given FID (which is
865                          * contained in the @obj, from client RPC for locating
866                          * the OST-object) is trusted. We use it to generate
867                          * the LMA.
868                          */
869                         osd_lma_self_repair(info, osd, inode, rfid,
870                                             LMAC_FID_ON_OST);
871                         RETURN(0);
872                 }
873         }
874
875         if (rc < 0)
876                 RETURN(rc);
877
878         if (rc > 0) {
879                 rc = 0;
880                 lustre_lma_swab(lma);
881                 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
882                              (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
883                               S_ISREG(inode->i_mode)))) {
884                         CWARN("%s: unsupported incompat LMA feature(s) %#x for "
885                               "fid = "DFID", ino = %lu\n", osd_name(osd),
886                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
887                               PFID(rfid), inode->i_ino);
888                         rc = -EOPNOTSUPP;
889                 } else {
890                         fid = &lma->lma_self_fid;
891                         if (lma->lma_compat & LMAC_STRIPE_INFO &&
892                             osd->od_is_ost)
893                                 obj->oo_pfid_in_lma = 1;
894                         if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
895                             !osd->od_is_ost)
896                                 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
897                 }
898         }
899
900         if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
901                 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
902                         struct ost_id   *oi   = &info->oti_ostid;
903                         struct lu_fid   *fid1 = &info->oti_fid3;
904                         __u32            idx  = fid_idif_ost_idx(rfid);
905
906                         /*
907                          * For old IDIF, the OST index is not part of the IDIF,
908                          * Means that different OSTs may have the same IDIFs.
909                          * Under such case, we need to make some compatible
910                          * check to make sure to trigger OI scrub properly.
911                          */
912                         if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
913                                 /* Given @rfid is new, LMA is old. */
914                                 fid_to_ostid(fid, oi);
915                                 ostid_to_fid(fid1, oi, idx);
916                                 if (lu_fid_eq(fid1, rfid)) {
917                                         if (osd->od_index_in_idif)
918                                                 osd_lma_self_repair(info, osd,
919                                                         inode, rfid,
920                                                         LMAC_FID_ON_OST);
921                                         RETURN(0);
922                                 }
923                         }
924                 }
925
926                 rc = -EREMCHG;
927         }
928
929         RETURN(rc);
930 }
931
932 struct osd_check_lmv_buf {
933         /* please keep it as first member */
934         struct dir_context ctx;
935         struct osd_thread_info *oclb_info;
936         struct osd_device *oclb_dev;
937         int oclb_items;
938         bool oclb_found;
939 };
940
941 /**
942  * It is called internally by ->iterate*() to filter out the
943  * local slave object's FID of the striped directory.
944  *
945  * \retval      1 found the local slave's FID
946  * \retval      0 continue to check next item
947  * \retval      -ve for failure
948  */
949 #ifdef HAVE_FILLDIR_USE_CTX
950 static int osd_stripe_dir_filldir(struct dir_context *buf,
951 #else
952 static int osd_stripe_dir_filldir(void *buf,
953 #endif
954                                   const char *name, int namelen,
955                                   loff_t offset, __u64 ino, unsigned int d_type)
956 {
957         struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
958         struct osd_thread_info *oti = oclb->oclb_info;
959         struct lu_fid *fid = &oti->oti_fid3;
960         struct osd_inode_id *id = &oti->oti_id3;
961         struct osd_device *dev = oclb->oclb_dev;
962         struct inode *inode;
963
964         oclb->oclb_items++;
965
966         if (name[0] == '.')
967                 return 0;
968
969         fid_zero(fid);
970         sscanf(name + 1, SFID, RFID(fid));
971         if (!fid_is_sane(fid))
972                 return 0;
973
974         if (osd_remote_fid(oti->oti_env, dev, fid))
975                 return 0;
976
977         osd_id_gen(id, ino, OSD_OII_NOGEN);
978         inode = osd_iget(oti, dev, id);
979         if (IS_ERR(inode))
980                 return PTR_ERR(inode);
981
982         iput(inode);
983         osd_add_oi_cache(oti, dev, id, fid);
984         osd_oii_insert(dev, fid, id, true);
985         oclb->oclb_found = true;
986
987         return 1;
988 }
989
990 /*
991  * When lookup item under striped directory, we need to locate the master
992  * MDT-object of the striped directory firstly, then the client will send
993  * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
994  * and the item's name. If the system is restored from MDT file level backup,
995  * then before the OI scrub completely built the OI files, the OI mappings of
996  * the master MDT-object and slave MDT-object may be invalid. Usually, it is
997  * not a problem for the master MDT-object. Because when locate the master
998  * MDT-object, we will do name based lookup (for the striped directory itself)
999  * firstly, during such process we can setup the correct OI mapping for the
1000  * master MDT-object. But it will be trouble for the slave MDT-object. Because
1001  * the client will not trigger name based lookup on the MDT to locate the slave
1002  * MDT-object before locating item under the striped directory, then when
1003  * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
1004  * is invalid and does not know what the right OI mapping is, then the MDT has
1005  * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
1006  * the OI file, related OI mapping is unknown yet, please try again later. And
1007  * then client will re-try the RPC again and again until related OI mapping has
1008  * been updated. That is quite inefficient.
1009  *
1010  * To resolve above trouble, we will handle it as the following two cases:
1011  *
1012  * 1) The slave MDT-object and the master MDT-object are on different MDTs.
1013  *    It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
1014  *    is linked under /REMOTE_PARENT_DIR with the name of its FID string.
1015  *    We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
1016  *    directly. Please check osd_fid_lookup().
1017  *
1018  * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
1019  *    Under such case, during lookup the master MDT-object, we will lookup the
1020  *    slave MDT-object via readdir against the master MDT-object, because the
1021  *    slave MDT-objects information are stored as sub-directories with the name
1022  *    "${FID}:${index}". Then when find the local slave MDT-object, its OI
1023  *    mapping will be recorded. Then subsequent osd_fid_lookup() will know
1024  *    the correct OI mapping for the slave MDT-object.
1025  */
1026 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
1027                          struct inode *inode)
1028 {
1029         struct lu_buf *buf = &oti->oti_big_buf;
1030         struct file *filp;
1031         struct lmv_mds_md_v1 *lmv1;
1032         struct osd_check_lmv_buf oclb = {
1033                 .ctx.actor = osd_stripe_dir_filldir,
1034                 .oclb_info = oti,
1035                 .oclb_dev = dev,
1036                 .oclb_found = false,
1037         };
1038         int rc = 0;
1039
1040         ENTRY;
1041         /* We should use the VFS layer to create a real dentry. */
1042         oti->oti_obj_dentry.d_inode = inode;
1043         oti->oti_obj_dentry.d_sb = inode->i_sb;
1044
1045         filp = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
1046                                  inode->i_fop);
1047         if (IS_ERR(filp))
1048                 RETURN(-ENOMEM);
1049
1050         filp->f_mode |= FMODE_64BITHASH;
1051         filp->f_pos = 0;
1052         ihold(inode);
1053 again:
1054         rc = __osd_xattr_get(inode, filp->f_path.dentry, XATTR_NAME_LMV,
1055                              buf->lb_buf, buf->lb_len);
1056         if (rc == -ERANGE) {
1057                 rc = __osd_xattr_get(inode, filp->f_path.dentry,
1058                                      XATTR_NAME_LMV, NULL, 0);
1059                 if (rc > 0) {
1060                         lu_buf_realloc(buf, rc);
1061                         if (buf->lb_buf == NULL)
1062                                 GOTO(out, rc = -ENOMEM);
1063
1064                         goto again;
1065                 }
1066         }
1067
1068         if (unlikely(rc == 0 || rc == -ENODATA))
1069                 GOTO(out, rc = 0);
1070
1071         if (rc < 0)
1072                 GOTO(out, rc);
1073
1074         if (unlikely(buf->lb_buf == NULL)) {
1075                 lu_buf_realloc(buf, rc);
1076                 if (buf->lb_buf == NULL)
1077                         GOTO(out, rc = -ENOMEM);
1078
1079                 goto again;
1080         }
1081
1082         lmv1 = buf->lb_buf;
1083         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1084                 GOTO(out, rc = 0);
1085
1086         do {
1087                 oclb.oclb_items = 0;
1088                 rc = iterate_dir(filp, &oclb.ctx);
1089         } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1090                  filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1091 out:
1092         fput(filp);
1093         if (rc < 0)
1094                 CDEBUG(D_LFSCK,
1095                        "%s: cannot check LMV, ino = %lu/%u: rc = %d\n",
1096                        osd_ino2name(inode), inode->i_ino, inode->i_generation,
1097                        rc);
1098         else
1099                 rc = 0;
1100
1101         RETURN(rc);
1102 }
1103
1104 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1105                           const struct lu_fid *fid,
1106                           const struct lu_object_conf *conf)
1107 {
1108         struct osd_thread_info *info;
1109         struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1110         struct osd_device *dev;
1111         struct osd_idmap_cache *oic;
1112         struct osd_inode_id *id;
1113         struct inode *inode = NULL;
1114         struct lustre_scrub *scrub;
1115         struct scrub_file *sf;
1116         __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1117         __u32 saved_ino;
1118         __u32 saved_gen;
1119         int result = 0;
1120         int rc1 = 0;
1121         bool remote = false;
1122         bool trusted = true;
1123         bool updated = false;
1124         bool checked = false;
1125
1126         ENTRY;
1127
1128         LINVRNT(osd_invariant(obj));
1129         LASSERT(obj->oo_inode == NULL);
1130
1131         if (fid_is_sane(fid) == 0) {
1132                 CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
1133                        PFID(fid));
1134                 dump_stack();
1135                 RETURN(-EINVAL);
1136         }
1137
1138         dev = osd_dev(ldev);
1139         scrub = &dev->od_scrub.os_scrub;
1140         sf = &scrub->os_file;
1141         info = osd_oti_get(env);
1142         LASSERT(info);
1143         oic = &info->oti_cache;
1144
1145         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1146                 RETURN(-ENOENT);
1147
1148         /*
1149          * For the object is created as locking anchor, or for the object to
1150          * be created on disk. No need to osd_oi_lookup() at here because FID
1151          * shouldn't never be re-used, if it's really a duplicate FID from
1152          * unexpected reason, we should be able to detect it later by calling
1153          * do_create->osd_oi_insert().
1154          */
1155         if (conf && conf->loc_flags & LOC_F_NEW)
1156                 GOTO(out, result = 0);
1157
1158         /* Search order: 1. per-thread cache. */
1159         if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1160                 id = &oic->oic_lid;
1161                 goto iget;
1162         }
1163
1164         id = &info->oti_id;
1165         memset(id, 0, sizeof(struct osd_inode_id));
1166         if (!list_empty(&scrub->os_inconsistent_items)) {
1167                 /* Search order: 2. OI scrub pending list. */
1168                 result = osd_oii_lookup(dev, fid, id);
1169                 if (!result)
1170                         goto iget;
1171         }
1172
1173         /*
1174          * The OI mapping in the OI file can be updated by the OI scrub
1175          * when we locate the inode via FID. So it may be not trustable.
1176          */
1177         trusted = false;
1178
1179         /* Search order: 3. OI files. */
1180         result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1181         if (result == -ENOENT) {
1182                 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1183                     fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1184                     !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1185                                       sf->sf_oi_bitmap))
1186                         GOTO(out, result = 0);
1187
1188                 goto trigger;
1189         }
1190
1191         /* -ESTALE is returned if inode of OST object doesn't exist */
1192         if (result == -ESTALE &&
1193             fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1194                 GOTO(out, result = 0);
1195         }
1196
1197         if (result)
1198                 GOTO(out, result);
1199
1200 iget:
1201         obj->oo_inode = NULL;
1202         /* for later passes through checks, not true on first pass */
1203         if (!IS_ERR_OR_NULL(inode))
1204                 iput(inode);
1205
1206         inode = osd_iget_check(info, dev, fid, id, trusted);
1207         if (!IS_ERR(inode)) {
1208                 obj->oo_inode = inode;
1209                 result = 0;
1210                 if (remote)
1211                         goto trigger;
1212
1213                 goto check_lma;
1214         }
1215
1216         result = PTR_ERR(inode);
1217         if (result == -ENOENT || result == -ESTALE)
1218                 GOTO(out, result = 0);
1219
1220         if (result != -EREMCHG)
1221                 GOTO(out, result);
1222
1223 trigger:
1224         /*
1225          * We still have chance to get the valid inode: for the
1226          * object which is referenced by remote name entry, the
1227          * object on the local MDT will be linked under the dir
1228          * of "/REMOTE_PARENT_DIR" with its FID string as name.
1229          *
1230          * We do not know whether the object for the given FID
1231          * is referenced by some remote name entry or not, and
1232          * especially for DNE II, a multiple-linked object may
1233          * have many name entries reside on many MDTs.
1234          *
1235          * To simplify the operation, OSD will not distinguish
1236          * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1237          * only happened for the RPC from other MDT during the
1238          * OI scrub, or for the client side RPC with FID only,
1239          * such as FID to path, or from old connected client.
1240          */
1241         if (!remote) {
1242                 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1243                 if (!rc1) {
1244                         remote = true;
1245                         trusted = true;
1246                         flags |= SS_AUTO_PARTIAL;
1247                         flags &= ~SS_AUTO_FULL;
1248                         goto iget;
1249                 }
1250         }
1251
1252         if (scrub->os_running) {
1253                 if (scrub->os_partial_scan && !scrub->os_in_join)
1254                         goto join;
1255
1256                 if (IS_ERR_OR_NULL(inode) || result) {
1257                         osd_oii_insert(dev, fid, id, result == -ENOENT);
1258                         GOTO(out, result = -EINPROGRESS);
1259                 }
1260
1261                 LASSERT(remote);
1262                 LASSERT(obj->oo_inode == inode);
1263
1264                 osd_oii_insert(dev, fid, id, true);
1265                 goto found;
1266         }
1267
1268         if (dev->od_scrub.os_scrub.os_auto_scrub_interval == AS_NEVER) {
1269                 if (!remote)
1270                         GOTO(out, result = -EREMCHG);
1271
1272                 LASSERT(!result);
1273                 LASSERT(obj->oo_inode == inode);
1274
1275                 osd_add_oi_cache(info, dev, id, fid);
1276                 goto found;
1277         }
1278
1279 join:
1280         rc1 = osd_scrub_start(env, dev, flags);
1281         CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
1282                      "%s: trigger OI scrub by RPC for "DFID"/%u with flags %#x: rc = %d\n",
1283                      osd_name(dev), PFID(fid), id->oii_ino, flags, rc1);
1284         if (rc1 && rc1 != -EALREADY)
1285                 GOTO(out, result = -EREMCHG);
1286
1287         if (IS_ERR_OR_NULL(inode) || result) {
1288                 osd_oii_insert(dev, fid, id, result == -ENOENT);
1289                 GOTO(out, result = -EINPROGRESS);
1290         }
1291
1292         LASSERT(remote);
1293         LASSERT(obj->oo_inode == inode);
1294
1295         osd_oii_insert(dev, fid, id, true);
1296         goto found;
1297
1298 check_lma:
1299         checked = true;
1300         if (unlikely(obj->oo_header))
1301                 goto found;
1302
1303         result = osd_check_lma(env, obj);
1304         if (!result)
1305                 goto found;
1306
1307         LASSERTF(id->oii_ino == inode->i_ino &&
1308                  id->oii_gen == inode->i_generation,
1309                  "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1310                  PFID(fid), id->oii_ino, id->oii_gen,
1311                  inode->i_ino, inode->i_generation);
1312
1313         saved_ino = inode->i_ino;
1314         saved_gen = inode->i_generation;
1315
1316         if (unlikely(result == -ENODATA)) {
1317                 /*
1318                  * If the OI scrub updated the OI mapping by race, it
1319                  * must be valid. Trust the inode that has no LMA EA.
1320                  */
1321                 if (updated)
1322                         goto found;
1323
1324                 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1325                 if (!result) {
1326                         /*
1327                          * The OI mapping is still there, the inode is still
1328                          * valid. It is just becaues the inode has no LMA EA.
1329                          */
1330                         if (saved_ino == id->oii_ino &&
1331                             saved_gen == id->oii_gen)
1332                                 goto found;
1333
1334                         /*
1335                          * It is the OI scrub updated the OI mapping by race.
1336                          * The new OI mapping must be valid.
1337                          */
1338                         trusted = true;
1339                         updated = true;
1340                         goto iget;
1341                 }
1342
1343                 /*
1344                  * "result == -ENOENT" means that the OI mappinghas been
1345                  * removed by race, so the inode belongs to other object.
1346                  *
1347                  * Others error can be returned  directly.
1348                  */
1349                 if (result == -ENOENT) {
1350                         LASSERT(trusted);
1351
1352                         obj->oo_inode = NULL;
1353                         result = 0;
1354                 }
1355         }
1356
1357         if (result != -EREMCHG)
1358                 GOTO(out, result);
1359
1360         LASSERT(!updated);
1361
1362         /*
1363          * if two OST objects map to the same inode, and inode mode is
1364          * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1365          * reserved by precreate, and not written yet, in this case, don't
1366          * set inode for the object whose FID mismatch, so that it can create
1367          * inode and not block precreate.
1368          */
1369         if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1370             inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1371                 obj->oo_inode = NULL;
1372                 GOTO(out, result = 0);
1373         }
1374
1375         result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1376         /*
1377          * "result == -ENOENT" means the cached OI mapping has been removed
1378          * from the OI file by race, above inode belongs to other object.
1379          */
1380         if (result == -ENOENT) {
1381                 LASSERT(trusted);
1382
1383                 obj->oo_inode = NULL;
1384                 GOTO(out, result = 0);
1385         }
1386
1387         if (result)
1388                 GOTO(out, result);
1389
1390         if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1391                 result = -EREMCHG;
1392                 osd_scrub_refresh_mapping(info, dev, fid, id, DTO_INDEX_DELETE,
1393                                           true, 0, NULL);
1394                 goto trigger;
1395         }
1396
1397         /*
1398          * It is the OI scrub updated the OI mapping by race.
1399          * The new OI mapping must be valid.
1400          */
1401         trusted = true;
1402         updated = true;
1403         goto iget;
1404
1405 found:
1406         if (!checked) {
1407                 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1408                 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1409
1410                 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1411                 if (!result) {
1412                         if (lma->lma_compat & LMAC_STRIPE_INFO &&
1413                             dev->od_is_ost)
1414                                 obj->oo_pfid_in_lma = 1;
1415                         if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1416                             !dev->od_is_ost)
1417                                 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1418                 } else if (result != -ENODATA) {
1419                         GOTO(out, result);
1420                 }
1421         }
1422
1423         obj->oo_compat_dot_created = 1;
1424         obj->oo_compat_dotdot_created = 1;
1425
1426         if (S_ISDIR(inode->i_mode) &&
1427             (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1428                 osd_check_lmv(info, dev, inode);
1429
1430         result = osd_attach_jinode(inode);
1431         if (result)
1432                 GOTO(out, result);
1433
1434         if (!ldiskfs_pdo)
1435                 GOTO(out, result = 0);
1436
1437         LASSERT(!obj->oo_hl_head);
1438         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1439
1440         GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1441
1442 out:
1443         if (result || !obj->oo_inode) {
1444                 if (!IS_ERR_OR_NULL(inode))
1445                         iput(inode);
1446
1447                 obj->oo_inode = NULL;
1448                 if (trusted)
1449                         fid_zero(&oic->oic_fid);
1450         }
1451
1452         LINVRNT(osd_invariant(obj));
1453         return result;
1454 }
1455
1456 /*
1457  * Concurrency: shouldn't matter.
1458  */
1459 static void osd_object_init0(struct osd_object *obj)
1460 {
1461         LASSERT(obj->oo_inode != NULL);
1462         obj->oo_dt.do_body_ops = &osd_body_ops;
1463         obj->oo_dt.do_lu.lo_header->loh_attr |=
1464                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1465 }
1466
1467 /*
1468  * Concurrency: no concurrent access is possible that early in object
1469  * life-cycle.
1470  */
1471 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1472                            const struct lu_object_conf *conf)
1473 {
1474         struct osd_object *obj = osd_obj(l);
1475         int result;
1476
1477         LINVRNT(osd_invariant(obj));
1478
1479         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1480             cfs_fail_val == 2) {
1481                 struct osd_thread_info *info = osd_oti_get(env);
1482                 struct osd_idmap_cache *oic = &info->oti_cache;
1483                 /* invalidate thread cache */
1484                 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1485         }
1486         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1487                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1488                 l->lo_header->loh_attr |= LOHA_EXISTS;
1489                 return 0;
1490         }
1491
1492         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1493         obj->oo_dt.do_body_ops = &osd_body_ops_new;
1494         if (result == 0 && obj->oo_inode != NULL) {
1495                 struct osd_thread_info *oti = osd_oti_get(env);
1496                 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1497
1498                 osd_object_init0(obj);
1499                 if (unlikely(obj->oo_header))
1500                         return 0;
1501
1502                 result = osd_get_lma(oti, obj->oo_inode,
1503                                      &oti->oti_obj_dentry, loa);
1504                 if (!result) {
1505                         /*
1506                          * Convert LMAI flags to lustre LMA flags
1507                          * and cache it to oo_lma_flags
1508                          */
1509                         obj->oo_lma_flags =
1510                                 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1511                 } else if (result == -ENODATA) {
1512                         result = 0;
1513                 }
1514         }
1515         obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1516
1517         LINVRNT(osd_invariant(obj));
1518         return result;
1519 }
1520
1521 /*
1522  * The first part of oxe_buf is xattr name, and is '\0' terminated.
1523  * The left part is for value, binary mode.
1524  */
1525 struct osd_xattr_entry {
1526         struct list_head        oxe_list;
1527         size_t                  oxe_len;
1528         size_t                  oxe_namelen;
1529         bool                    oxe_exist;
1530         struct rcu_head         oxe_rcu;
1531         char                    oxe_buf[0];
1532 };
1533
1534 static int osd_oxc_get(struct osd_object *obj, const char *name,
1535                        struct lu_buf *buf)
1536 {
1537         struct osd_xattr_entry *tmp;
1538         struct osd_xattr_entry *oxe = NULL;
1539         size_t namelen = strlen(name);
1540         int rc;
1541
1542         rcu_read_lock();
1543         list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1544                 if (namelen == tmp->oxe_namelen &&
1545                     strncmp(name, tmp->oxe_buf, namelen) == 0) {
1546                         oxe = tmp;
1547                         break;
1548                 }
1549         }
1550
1551         if (oxe == NULL)
1552                 GOTO(out, rc = -ENOENT);
1553
1554         if (!oxe->oxe_exist)
1555                 GOTO(out, rc = -ENODATA);
1556
1557         /* vallen */
1558         rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1559         LASSERT(rc > 0);
1560
1561         if (buf->lb_buf == NULL)
1562                 GOTO(out, rc);
1563
1564         if (buf->lb_len < rc)
1565                 GOTO(out, rc = -ERANGE);
1566
1567         memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1568 out:
1569         rcu_read_unlock();
1570
1571         return rc;
1572 }
1573
1574 static void osd_oxc_free(struct rcu_head *head)
1575 {
1576         struct osd_xattr_entry *oxe;
1577
1578         oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1579         OBD_FREE(oxe, oxe->oxe_len);
1580 }
1581
1582 static void osd_oxc_add(struct osd_object *obj, const char *name,
1583                         const char *buf, int buflen)
1584 {
1585         struct osd_xattr_entry *oxe;
1586         struct osd_xattr_entry *old = NULL;
1587         struct osd_xattr_entry *tmp;
1588         size_t namelen = strlen(name);
1589         size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1590
1591         OBD_ALLOC(oxe, len);
1592         if (oxe == NULL)
1593                 return;
1594
1595         INIT_LIST_HEAD(&oxe->oxe_list);
1596         oxe->oxe_len = len;
1597         oxe->oxe_namelen = namelen;
1598         memcpy(oxe->oxe_buf, name, namelen);
1599         if (buflen > 0) {
1600                 LASSERT(buf != NULL);
1601                 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1602                 oxe->oxe_exist = true;
1603         } else {
1604                 oxe->oxe_exist = false;
1605         }
1606
1607         /* this should be rarely called, just remove old and add new */
1608         spin_lock(&obj->oo_guard);
1609         list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1610                 if (namelen == tmp->oxe_namelen &&
1611                     strncmp(name, tmp->oxe_buf, namelen) == 0) {
1612                         old = tmp;
1613                         break;
1614                 }
1615         }
1616         if (old != NULL) {
1617                 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1618                 call_rcu(&old->oxe_rcu, osd_oxc_free);
1619         } else {
1620                 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1621         }
1622         spin_unlock(&obj->oo_guard);
1623 }
1624
1625 static void osd_oxc_del(struct osd_object *obj, const char *name)
1626 {
1627         struct osd_xattr_entry *oxe;
1628         size_t namelen = strlen(name);
1629
1630         spin_lock(&obj->oo_guard);
1631         list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1632                 if (namelen == oxe->oxe_namelen &&
1633                     strncmp(name, oxe->oxe_buf, namelen) == 0) {
1634                         list_del_rcu(&oxe->oxe_list);
1635                         call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1636                         break;
1637                 }
1638         }
1639         spin_unlock(&obj->oo_guard);
1640 }
1641
1642 static void osd_oxc_fini(struct osd_object *obj)
1643 {
1644         struct osd_xattr_entry *oxe, *next;
1645
1646         list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1647                 list_del(&oxe->oxe_list);
1648                 OBD_FREE(oxe, oxe->oxe_len);
1649         }
1650 }
1651
1652 /*
1653  * Concurrency: no concurrent access is possible that late in object
1654  * life-cycle.
1655  */
1656 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1657 {
1658         struct osd_object *obj = osd_obj(l);
1659         struct lu_object_header *h = obj->oo_header;
1660
1661         LINVRNT(osd_invariant(obj));
1662
1663         osd_oxc_fini(obj);
1664         dt_object_fini(&obj->oo_dt);
1665         if (obj->oo_hl_head != NULL)
1666                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1667         /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1668         OBD_FREE_PTR(obj);
1669         if (unlikely(h))
1670                 lu_object_header_free(h);
1671 }
1672
1673 /*
1674  * Concurrency: no concurrent access is possible that late in object
1675  * life-cycle.
1676  */
1677 static void osd_index_fini(struct osd_object *o)
1678 {
1679         struct iam_container *bag;
1680
1681         if (o->oo_dir != NULL) {
1682                 bag = &o->oo_dir->od_container;
1683                 if (o->oo_inode != NULL) {
1684                         if (bag->ic_object == o->oo_inode)
1685                                 iam_container_fini(bag);
1686                 }
1687                 OBD_FREE_PTR(o->oo_dir);
1688                 o->oo_dir = NULL;
1689         }
1690 }
1691
1692 enum {
1693         OSD_TXN_OI_DELETE_CREDITS    = 20,
1694         OSD_TXN_INODE_DELETE_CREDITS = 20
1695 };
1696
1697 /*
1698  * Journal
1699  */
1700
1701 #if OSD_THANDLE_STATS
1702 /**
1703  * Set time when the handle is allocated
1704  */
1705 static void osd_th_alloced(struct osd_thandle *oth)
1706 {
1707         oth->oth_alloced = ktime_get();
1708 }
1709
1710 /**
1711  * Set time when the handle started
1712  */
1713 static void osd_th_started(struct osd_thandle *oth)
1714 {
1715         oth->oth_started = ktime_get();
1716 }
1717
1718 /**
1719  * Check whether the we deal with this handle for too long.
1720  */
1721 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1722                                 ktime_t alloced, ktime_t started,
1723                                 ktime_t closed)
1724 {
1725         ktime_t now = ktime_get();
1726
1727         LASSERT(dev != NULL);
1728
1729         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1730                             ktime_us_delta(started, alloced));
1731         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1732                             ktime_us_delta(closed, started));
1733         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1734                             ktime_us_delta(now, closed));
1735
1736         if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1737                 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1738                                 oth, now, alloced, started, closed);
1739                 libcfs_debug_dumpstack(NULL);
1740         }
1741 }
1742
1743 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
1744 {                                                                       \
1745         ktime_t __closed = ktime_get();                                 \
1746         ktime_t __alloced = oth->oth_alloced;                           \
1747         ktime_t __started = oth->oth_started;                           \
1748                                                                         \
1749         expr;                                                           \
1750         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
1751 }
1752
1753 #else /* OSD_THANDLE_STATS */
1754
1755 #define osd_th_alloced(h)                  do {} while(0)
1756 #define osd_th_started(h)                  do {} while(0)
1757 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
1758
1759 #endif /* OSD_THANDLE_STATS */
1760
1761 /*
1762  * Concurrency: doesn't access mutable data.
1763  */
1764 static int osd_param_is_not_sane(const struct osd_device *dev,
1765                                  const struct thandle *th)
1766 {
1767         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1768
1769         return oh->ot_credits > osd_transaction_size(dev);
1770 }
1771
1772 /*
1773  * Concurrency: shouldn't matter.
1774  */
1775 static void osd_trans_commit_cb(struct super_block *sb,
1776                                 struct ldiskfs_journal_cb_entry *jcb, int error)
1777 {
1778         struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1779         struct thandle *th = &oh->ot_super;
1780         struct lu_device *lud = &th->th_dev->dd_lu_dev;
1781         struct osd_device *osd = osd_dev(lud);
1782         struct dt_txn_commit_cb *dcb, *tmp;
1783
1784         LASSERT(oh->ot_handle == NULL);
1785
1786         if (error)
1787                 CERROR("transaction @0x%p commit error: %d\n", th, error);
1788
1789         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_DELAY_TRANS, 40);
1790         /* call per-transaction callbacks if any */
1791         list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1792                                  dcb_linkage) {
1793                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1794                          "commit callback entry: magic=%x name='%s'\n",
1795                          dcb->dcb_magic, dcb->dcb_name);
1796                 list_del_init(&dcb->dcb_linkage);
1797                 dcb->dcb_func(NULL, th, dcb, error);
1798         }
1799
1800         lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1801         if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1802                 wake_up(&osd->od_commit_cb_done);
1803         th->th_dev = NULL;
1804
1805         OBD_FREE_PTR(oh);
1806 }
1807
1808 static struct thandle *osd_trans_create(const struct lu_env *env,
1809                                         struct dt_device *d)
1810 {
1811         struct osd_thread_info *oti = osd_oti_get(env);
1812         struct osd_iobuf *iobuf = &oti->oti_iobuf;
1813         struct osd_thandle *oh;
1814         struct thandle *th;
1815
1816         ENTRY;
1817
1818         if (d->dd_rdonly) {
1819                 CERROR("%s: someone try to start transaction under "
1820                        "readonly mode, should be disabled.\n",
1821                        osd_name(osd_dt_dev(d)));
1822                 dump_stack();
1823                 RETURN(ERR_PTR(-EROFS));
1824         }
1825
1826         /* on pending IO in this thread should left from prev. request */
1827         LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1828
1829         sb_start_write(osd_sb(osd_dt_dev(d)));
1830
1831         OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1832         if (!oh) {
1833                 sb_end_write(osd_sb(osd_dt_dev(d)));
1834                 RETURN(ERR_PTR(-ENOMEM));
1835         }
1836
1837         oh->ot_quota_trans = &oti->oti_quota_trans;
1838         memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1839         th = &oh->ot_super;
1840         th->th_dev = d;
1841         th->th_result = 0;
1842         oh->ot_credits = 0;
1843         oh->oh_declared_ext = 0;
1844         INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1845         INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1846         INIT_LIST_HEAD(&oh->ot_trunc_locks);
1847         osd_th_alloced(oh);
1848
1849         memset(oti->oti_declare_ops, 0,
1850                sizeof(oti->oti_declare_ops));
1851         memset(oti->oti_declare_ops_cred, 0,
1852                sizeof(oti->oti_declare_ops_cred));
1853         memset(oti->oti_declare_ops_used, 0,
1854                sizeof(oti->oti_declare_ops_used));
1855
1856         oti->oti_ins_cache_depth++;
1857
1858         RETURN(th);
1859 }
1860
1861 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1862 {
1863         struct osd_thread_info *oti = osd_oti_get(env);
1864         struct osd_thandle *oh;
1865
1866         oh = container_of(th, struct osd_thandle, ot_super);
1867         LASSERT(oh != NULL);
1868
1869         CWARN("  create: %u/%u/%u, destroy: %u/%u/%u\n",
1870               oti->oti_declare_ops[OSD_OT_CREATE],
1871               oti->oti_declare_ops_cred[OSD_OT_CREATE],
1872               oti->oti_declare_ops_used[OSD_OT_CREATE],
1873               oti->oti_declare_ops[OSD_OT_DESTROY],
1874               oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1875               oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1876         CWARN("  attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1877               oti->oti_declare_ops[OSD_OT_ATTR_SET],
1878               oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1879               oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1880               oti->oti_declare_ops[OSD_OT_XATTR_SET],
1881               oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1882               oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1883         CWARN("  write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1884               oti->oti_declare_ops[OSD_OT_WRITE],
1885               oti->oti_declare_ops_cred[OSD_OT_WRITE],
1886               oti->oti_declare_ops_used[OSD_OT_WRITE],
1887               oti->oti_declare_ops[OSD_OT_PUNCH],
1888               oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1889               oti->oti_declare_ops_used[OSD_OT_PUNCH],
1890               oti->oti_declare_ops[OSD_OT_QUOTA],
1891               oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1892               oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1893         CWARN("  insert: %u/%u/%u, delete: %u/%u/%u\n",
1894               oti->oti_declare_ops[OSD_OT_INSERT],
1895               oti->oti_declare_ops_cred[OSD_OT_INSERT],
1896               oti->oti_declare_ops_used[OSD_OT_INSERT],
1897               oti->oti_declare_ops[OSD_OT_DELETE],
1898               oti->oti_declare_ops_cred[OSD_OT_DELETE],
1899               oti->oti_declare_ops_used[OSD_OT_DELETE]);
1900         CWARN("  ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1901               oti->oti_declare_ops[OSD_OT_REF_ADD],
1902               oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1903               oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1904               oti->oti_declare_ops[OSD_OT_REF_DEL],
1905               oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1906               oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1907 }
1908
1909 /*
1910  * Concurrency: shouldn't matter.
1911  */
1912 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1913                            struct thandle *th)
1914 {
1915         struct osd_thread_info *oti = osd_oti_get(env);
1916         struct osd_device *dev = osd_dt_dev(d);
1917         handle_t *jh;
1918         struct osd_thandle *oh;
1919         int rc;
1920
1921         ENTRY;
1922
1923         LASSERT(current->journal_info == NULL);
1924
1925         oh = container_of(th, struct osd_thandle, ot_super);
1926         LASSERT(oh != NULL);
1927         LASSERT(oh->ot_handle == NULL);
1928
1929         rc = dt_txn_hook_start(env, d, th);
1930         if (rc != 0)
1931                 GOTO(out, rc);
1932
1933         if (unlikely(osd_param_is_not_sane(dev, th))) {
1934                 static unsigned long last_printed;
1935                 static int last_credits;
1936
1937                 /*
1938                  * don't make noise on a tiny testing systems
1939                  * actual credits misuse will be caught anyway
1940                  */
1941                 if (last_credits != oh->ot_credits &&
1942                     time_after(jiffies, last_printed +
1943                                cfs_time_seconds(60)) &&
1944                     osd_transaction_size(dev) > 512) {
1945                         CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1946                               oh->ot_credits, osd_transaction_size(dev));
1947                         osd_trans_dump_creds(env, th);
1948                         libcfs_debug_dumpstack(NULL);
1949                         last_credits = oh->ot_credits;
1950                         last_printed = jiffies;
1951                 }
1952                 /*
1953                  * XXX Limit the credits to 'max_transaction_buffers', and
1954                  *     let the underlying filesystem to catch the error if
1955                  *     we really need so many credits.
1956                  *
1957                  *     This should be removed when we can calculate the
1958                  *     credits precisely.
1959                  */
1960                 oh->ot_credits = osd_transaction_size(dev);
1961         } else if (ldiskfs_track_declares_assert != 0) {
1962                 /*
1963                  * reserve few credits to prevent an assertion in JBD
1964                  * our debugging mechanism will be able to detected
1965                  * overuse. this can help to debug single-update
1966                  * transactions
1967                  */
1968                 oh->ot_credits += 10;
1969                 if (unlikely(osd_param_is_not_sane(dev, th)))
1970                         oh->ot_credits = osd_transaction_size(dev);
1971         }
1972
1973         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
1974                 GOTO(out, rc = -EIO);
1975
1976         /*
1977          * XXX temporary stuff. Some abstraction layer should
1978          * be used.
1979          */
1980         jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1981         osd_th_started(oh);
1982         if (!IS_ERR(jh)) {
1983                 oh->ot_handle = jh;
1984                 LASSERT(oti->oti_txns == 0);
1985
1986                 atomic_inc(&dev->od_commit_cb_in_flight);
1987                 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1988                               "osd-tx", th);
1989                 oti->oti_txns++;
1990                 rc = 0;
1991         } else {
1992                 rc = PTR_ERR(jh);
1993         }
1994 out:
1995         RETURN(rc);
1996 }
1997
1998 static int osd_seq_exists(const struct lu_env *env,
1999                           struct osd_device *osd, u64 seq)
2000 {
2001         struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2002         struct seq_server_site *ss = osd_seq_site(osd);
2003         int rc;
2004
2005         ENTRY;
2006
2007         LASSERT(ss != NULL);
2008         LASSERT(ss->ss_server_fld != NULL);
2009
2010         rc = osd_fld_lookup(env, osd, seq, range);
2011         if (rc != 0) {
2012                 if (rc != -ENOENT)
2013                         CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2014                                osd_name(osd), seq, rc);
2015                 RETURN(0);
2016         }
2017
2018         RETURN(ss->ss_node_id == range->lsr_index);
2019 }
2020
2021 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2022 {
2023         struct dt_txn_commit_cb *dcb;
2024         struct dt_txn_commit_cb *tmp;
2025
2026         /* call per-transaction stop callbacks if any */
2027         list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2028                                  dcb_linkage) {
2029                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2030                          "commit callback entry: magic=%x name='%s'\n",
2031                          dcb->dcb_magic, dcb->dcb_name);
2032                 list_del_init(&dcb->dcb_linkage);
2033                 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2034         }
2035 }
2036
2037 /*
2038  * Concurrency: shouldn't matter.
2039  */
2040 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2041                           struct thandle *th)
2042 {
2043         struct osd_thread_info *oti = osd_oti_get(env);
2044         struct osd_thandle *oh;
2045         struct osd_iobuf *iobuf = &oti->oti_iobuf;
2046         struct osd_device *osd = osd_dt_dev(th->th_dev);
2047         struct qsd_instance *qsd = osd_def_qsd(osd);
2048         struct lquota_trans *qtrans;
2049         LIST_HEAD(truncates);
2050         int rc = 0, remove_agents = 0;
2051
2052         ENTRY;
2053
2054         oh = container_of(th, struct osd_thandle, ot_super);
2055
2056         remove_agents = oh->ot_remove_agents;
2057
2058         qtrans = oh->ot_quota_trans;
2059         oh->ot_quota_trans = NULL;
2060
2061         /* move locks to local list, stop tx, execute truncates */
2062         list_splice(&oh->ot_trunc_locks, &truncates);
2063
2064         if (oh->ot_handle != NULL) {
2065                 int rc2;
2066
2067                 handle_t *hdl = oh->ot_handle;
2068
2069                 /*
2070                  * add commit callback
2071                  * notice we don't do this in osd_trans_start()
2072                  * as underlying transaction can change during truncate
2073                  */
2074                 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2075                                              &oh->ot_jcb);
2076
2077                 LASSERT(oti->oti_txns == 1);
2078                 oti->oti_txns--;
2079
2080                 rc = dt_txn_hook_stop(env, th);
2081                 if (rc != 0)
2082                         CERROR("%s: failed in transaction hook: rc = %d\n",
2083                                osd_name(osd), rc);
2084
2085                 osd_trans_stop_cb(oh, rc);
2086                 /* hook functions might modify th_sync */
2087                 hdl->h_sync = th->th_sync;
2088
2089                 oh->ot_handle = NULL;
2090                 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2091                 if (rc2 != 0)
2092                         CERROR("%s: failed to stop transaction: rc = %d\n",
2093                                osd_name(osd), rc2);
2094                 if (!rc)
2095                         rc = rc2;
2096
2097                 /* We preserve the origin behavior of ignoring any
2098                  * failures with the underlying punch / truncate
2099                  * operation. We do record for debugging if an error
2100                  * does occur in the lctl dk logs.
2101                  */
2102                 rc2 = osd_process_truncates(env, &truncates);
2103                 if (rc2 != 0)
2104                         CERROR("%s: failed truncate process: rc = %d\n",
2105                                osd_name(osd), rc2);
2106         } else {
2107                 osd_trans_stop_cb(oh, th->th_result);
2108                 OBD_FREE_PTR(oh);
2109         }
2110
2111         osd_trunc_unlock_all(env, &truncates);
2112
2113         /* inform the quota slave device that the transaction is stopping */
2114         qsd_op_end(env, qsd, qtrans);
2115
2116         /*
2117          * as we want IO to journal and data IO be concurrent, we don't block
2118          * awaiting data IO completion in osd_do_bio(), instead we wait here
2119          * once transaction is submitted to the journal. all reqular requests
2120          * don't do direct IO (except read/write), thus this wait_event becomes
2121          * no-op for them.
2122          *
2123          * IMPORTANT: we have to wait till any IO submited by the thread is
2124          * completed otherwise iobuf may be corrupted by different request
2125          */
2126         wait_event(iobuf->dr_wait,
2127                        atomic_read(&iobuf->dr_numreqs) == 0);
2128         osd_fini_iobuf(osd, iobuf);
2129         if (!rc)
2130                 rc = iobuf->dr_error;
2131
2132         if (unlikely(remove_agents != 0))
2133                 osd_process_scheduled_agent_removals(env, osd);
2134
2135         LASSERT(oti->oti_ins_cache_depth > 0);
2136         oti->oti_ins_cache_depth--;
2137         /* reset OI cache for safety */
2138         if (oti->oti_ins_cache_depth == 0)
2139                 oti->oti_ins_cache_used = 0;
2140
2141         sb_end_write(osd_sb(osd));
2142
2143         RETURN(rc);
2144 }
2145
2146 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2147 {
2148         struct osd_thandle *oh = container_of(th, struct osd_thandle,
2149                                               ot_super);
2150
2151         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2152         LASSERT(&dcb->dcb_func != NULL);
2153         if (dcb->dcb_flags & DCB_TRANS_STOP)
2154                 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2155         else
2156                 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2157
2158         return 0;
2159 }
2160
2161 /*
2162  * Called just before object is freed. Releases all resources except for
2163  * object itself (that is released by osd_object_free()).
2164  *
2165  * Concurrency: no concurrent access is possible that late in object
2166  * life-cycle.
2167  */
2168 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2169 {
2170         struct osd_object *obj = osd_obj(l);
2171         struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2172         struct inode *inode = obj->oo_inode;
2173         __u64 projid;
2174         qid_t uid;
2175         qid_t gid;
2176
2177         LINVRNT(osd_invariant(obj));
2178
2179         /*
2180          * If object is unlinked remove fid->ino mapping from object index.
2181          */
2182
2183         osd_index_fini(obj);
2184
2185         if (!inode)
2186                 return;
2187
2188         if (osd_has_index(obj) &&  obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2189                 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2190
2191         uid = i_uid_read(inode);
2192         gid = i_gid_read(inode);
2193         projid = i_projid_read(inode);
2194
2195         obj->oo_inode = NULL;
2196         iput(inode);
2197
2198         /* do not rebalance quota if the caller needs to release memory
2199          * otherwise qsd_refresh_usage() may went into a new ldiskfs
2200          * transaction and risk to deadlock - LU-12178 */
2201         if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2202                 return;
2203
2204         if (!obj->oo_header && qsd) {
2205                 struct osd_thread_info *info = osd_oti_get(env);
2206                 struct lquota_id_info *qi = &info->oti_qi;
2207
2208                 /* Release granted quota to master if necessary */
2209                 qi->lqi_id.qid_uid = uid;
2210                 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2211
2212                 qi->lqi_id.qid_uid = gid;
2213                 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2214
2215                 qi->lqi_id.qid_uid = projid;
2216                 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2217         }
2218 }
2219
2220 /*
2221  * Concurrency: ->loo_object_release() is called under site spin-lock.
2222  */
2223 static void osd_object_release(const struct lu_env *env,
2224                                struct lu_object *l)
2225 {
2226         struct osd_object *o = osd_obj(l);
2227
2228         /*
2229          * nobody should be releasing a non-destroyed object with nlink=0
2230          * the API allows this, but ldiskfs doesn't like and then report
2231          * this inode as deleted
2232          */
2233         LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2234                   o->oo_inode->i_nlink == 0));
2235 }
2236
2237 /*
2238  * Concurrency: shouldn't matter.
2239  */
2240 static int osd_object_print(const struct lu_env *env, void *cookie,
2241                             lu_printer_t p, const struct lu_object *l)
2242 {
2243         struct osd_object *o = osd_obj(l);
2244         struct iam_descr *d;
2245
2246         if (o->oo_dir != NULL)
2247                 d = o->oo_dir->od_container.ic_descr;
2248         else
2249                 d = NULL;
2250         return (*p)(env, cookie,
2251                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2252                     o, o->oo_inode,
2253                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
2254                     o->oo_inode ? o->oo_inode->i_generation : 0,
2255                     d ? d->id_ops->id_name : "plain");
2256 }
2257
2258 /*
2259  * Concurrency: shouldn't matter.
2260  */
2261 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2262                 struct obd_statfs *sfs, struct obd_statfs_info *info)
2263 {
2264         struct osd_device *osd = osd_dt_dev(d);
2265         struct super_block *sb = osd_sb(osd);
2266         struct kstatfs *ksfs;
2267         __u64 reserved;
2268         int result = 0;
2269
2270         if (unlikely(osd->od_mnt == NULL))
2271                 return -EINPROGRESS;
2272
2273         /* osd_lproc.c call this without env, allocate ksfs for that case */
2274         if (unlikely(env == NULL)) {
2275                 OBD_ALLOC_PTR(ksfs);
2276                 if (ksfs == NULL)
2277                         return -ENOMEM;
2278         } else {
2279                 ksfs = &osd_oti_get(env)->oti_ksfs;
2280         }
2281
2282         result = sb->s_op->statfs(sb->s_root, ksfs);
2283         if (result)
2284                 goto out;
2285
2286         statfs_pack(sfs, ksfs);
2287         if (unlikely(sb->s_flags & SB_RDONLY))
2288                 sfs->os_state |= OS_STATFS_READONLY;
2289
2290         sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2291
2292         if (ldiskfs_has_feature_extents(sb))
2293                 sfs->os_maxbytes = sb->s_maxbytes;
2294         else
2295                 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2296
2297         /*
2298          * Reserve some space so to avoid fragmenting the filesystem too much.
2299          * Fragmentation not only impacts performance, but can also increase
2300          * metadata overhead significantly, causing grant calculation to be
2301          * wrong.
2302          *
2303          * Reserve 0.78% of total space, at least 8MB for small filesystems.
2304          */
2305         BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2306         reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2307         if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2308                 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2309
2310         sfs->os_blocks -= reserved;
2311         sfs->os_bfree  -= min(reserved, sfs->os_bfree);
2312         sfs->os_bavail -= min(reserved, sfs->os_bavail);
2313
2314 out:
2315         if (unlikely(env == NULL))
2316                 OBD_FREE_PTR(ksfs);
2317         return result;
2318 }
2319
2320 /**
2321  * Estimate space needed for file creations. We assume the largest filename
2322  * which is 2^64 - 1, hence a filename of 20 chars.
2323  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2324  */
2325 #ifdef __LDISKFS_DIR_REC_LEN
2326 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2327 #else
2328 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2329 #endif
2330
2331 /*
2332  * Concurrency: doesn't access mutable data.
2333  */
2334 static void osd_conf_get(const struct lu_env *env,
2335                          const struct dt_device *dev,
2336                          struct dt_device_param *param)
2337 {
2338         struct osd_device *d = osd_dt_dev(dev);
2339         struct super_block *sb = osd_sb(d);
2340         struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2341         const char *name;
2342         int ea_overhead;
2343
2344         /*
2345          * XXX should be taken from not-yet-existing fs abstraction layer.
2346          */
2347         param->ddp_max_name_len = LDISKFS_NAME_LEN;
2348         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
2349         param->ddp_symlink_max  = sb->s_blocksize;
2350         param->ddp_mount_type   = LDD_MT_LDISKFS;
2351         if (ldiskfs_has_feature_extents(sb))
2352                 param->ddp_maxbytes = sb->s_maxbytes;
2353         else
2354                 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2355         /*
2356          * inode are statically allocated, so per-inode space consumption
2357          * is the space consumed by the directory entry
2358          */
2359         param->ddp_inodespace     = PER_OBJ_USAGE;
2360         /*
2361          * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2362          * is 128MB) which is unlikely to be hit in real life. Report a smaller
2363          * maximum length to not under-count the actual number of extents
2364          * needed for writing a file if there are sub-optimal block allocations.
2365          */
2366         param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2367         /* worst-case extent insertion metadata overhead */
2368         param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2369         param->ddp_mntopts = 0;
2370         if (test_opt(sb, XATTR_USER))
2371                 param->ddp_mntopts |= MNTOPT_USERXATTR;
2372         if (test_opt(sb, POSIX_ACL))
2373                 param->ddp_mntopts |= MNTOPT_ACL;
2374
2375         /*
2376          * LOD might calculate the max stripe count based on max_ea_size,
2377          * so we need take account in the overhead as well,
2378          * xattr_header + magic + xattr_entry_head
2379          */
2380         ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2381                       LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2382
2383 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2384         if (ldiskfs_has_feature_ea_inode(sb))
2385                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2386                                                                 ea_overhead;
2387         else
2388 #endif
2389                 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2390
2391         if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2392                 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2393
2394         /*
2395          * Preferred RPC size for efficient disk IO.  4MB shows good
2396          * all-around performance for ldiskfs, but use bigalloc chunk size
2397          * by default if larger.
2398          */
2399 #if defined(LDISKFS_CLUSTER_SIZE)
2400         if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2401                 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2402         else
2403 #endif
2404                 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2405
2406         param->ddp_t10_cksum_type = 0;
2407         if (bi) {
2408                 unsigned short interval = blk_integrity_interval(bi);
2409                 name = blk_integrity_name(bi);
2410                 /*
2411                  * Expected values:
2412                  * T10-DIF-TYPE1-CRC
2413                  * T10-DIF-TYPE3-CRC
2414                  * T10-DIF-TYPE1-IP
2415                  * T10-DIF-TYPE3-IP
2416                  */
2417                 if (strncmp(name, "T10-DIF-TYPE",
2418                             sizeof("T10-DIF-TYPE") - 1) == 0) {
2419                         /* also skip "1/3-" at end */
2420                         const int type_off = sizeof("T10-DIF-TYPE.");
2421                         char type_number = name[type_off - 2];
2422
2423                         if (interval != 512 && interval != 4096) {
2424                                 CERROR("%s: unsupported T10PI sector size %u\n",
2425                                        d->od_svname, interval);
2426                         } else if (type_number != '1' && type_number != '3') {
2427                                 CERROR("%s: unsupported T10PI type %s\n",
2428                                        d->od_svname, name);
2429                         } else if (strcmp(name + type_off, "CRC") == 0) {
2430                                 d->od_t10_type = type_number == '1' ?
2431                                         OSD_T10_TYPE1_CRC : OSD_T10_TYPE3_CRC;
2432                                 param->ddp_t10_cksum_type = interval == 512 ?
2433                                         OBD_CKSUM_T10CRC512 :
2434                                         OBD_CKSUM_T10CRC4K;
2435                         } else if (strcmp(name + type_off, "IP") == 0) {
2436                                 d->od_t10_type = type_number == '1' ?
2437                                         OSD_T10_TYPE1_IP : OSD_T10_TYPE3_IP;
2438                                 param->ddp_t10_cksum_type = interval == 512 ?
2439                                         OBD_CKSUM_T10IP512 :
2440                                         OBD_CKSUM_T10IP4K;
2441                         } else {
2442                                 CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
2443                                        d->od_svname, name);
2444                         }
2445
2446                 } else {
2447                         CERROR("%s: unsupported T10PI type '%s'\n",
2448                                d->od_svname, name);
2449                 }
2450         }
2451
2452         param->ddp_has_lseek_data_hole = true;
2453 }
2454
2455 static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
2456 {
2457         return osd_sb(osd_dt_dev(d));
2458 }
2459
2460 /*
2461  * Concurrency: shouldn't matter.
2462  */
2463 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2464 {
2465         int rc;
2466         struct super_block *s = osd_sb(osd_dt_dev(d));
2467         ENTRY;
2468
2469         down_read(&s->s_umount);
2470         rc = s->s_op->sync_fs(s, 1);
2471         up_read(&s->s_umount);
2472
2473         CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2474                rc);
2475
2476         return rc;
2477 }
2478
2479 /**
2480  * Start commit for OSD device.
2481  *
2482  * An implementation of dt_commit_async method for OSD device.
2483  * Asychronously starts underlayng fs sync and thereby a transaction
2484  * commit.
2485  *
2486  * \param env environment
2487  * \param d dt device
2488  *
2489  * \see dt_device_operations
2490  */
2491 static int osd_commit_async(const struct lu_env *env,
2492                             struct dt_device *d)
2493 {
2494         struct super_block *s = osd_sb(osd_dt_dev(d));
2495         int rc;
2496
2497         ENTRY;
2498
2499         CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2500         down_read(&s->s_umount);
2501         rc = s->s_op->sync_fs(s, 0);
2502         up_read(&s->s_umount);
2503
2504         RETURN(rc);
2505 }
2506
2507 /*
2508  * Concurrency: shouldn't matter.
2509  */
2510 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2511 {
2512         struct super_block *sb = osd_sb(osd_dt_dev(d));
2513         struct block_device *dev = sb->s_bdev;
2514         int rc = -EOPNOTSUPP;
2515
2516         ENTRY;
2517
2518         CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2519                osd_dt_dev(d)->od_svname, (long)dev, rc);
2520
2521         RETURN(rc);
2522 }
2523
2524 /**
2525  * Note: we do not count into QUOTA here.
2526  * If we mount with --data_journal we may need more.
2527  */
2528 const int osd_dto_credits_noquota[DTO_NR] = {
2529         /**
2530          * Insert.
2531          * INDEX_EXTRA_TRANS_BLOCKS(8) +
2532          * SINGLEDATA_TRANS_BLOCKS(8)
2533          * XXX Note: maybe iam need more, since iam have more level than
2534          *           EXT3 htree.
2535          */
2536         [DTO_INDEX_INSERT]  = 16,
2537         /**
2538          * Delete
2539          * just modify a single entry, probably merge few within a block
2540          */
2541         [DTO_INDEX_DELETE]  = 1,
2542         /**
2543          * Used for OI scrub
2544          */
2545         [DTO_INDEX_UPDATE]  = 16,
2546         /**
2547          * 4(inode, inode bits, groups, GDT)
2548          *   notice: OI updates are counted separately with DTO_INDEX_INSERT
2549          */
2550         [DTO_OBJECT_CREATE] = 4,
2551         /**
2552          * 4(inode, inode bits, groups, GDT)
2553          *   notice: OI updates are counted separately with DTO_INDEX_DELETE
2554          */
2555         [DTO_OBJECT_DELETE] = 4,
2556         /**
2557          * Attr set credits (inode)
2558          */
2559         [DTO_ATTR_SET_BASE] = 1,
2560         /**
2561          * Xattr set. The same as xattr of EXT3.
2562          * DATA_TRANS_BLOCKS(14)
2563          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2564          * are also counted in. Do not know why?
2565          */
2566         [DTO_XATTR_SET]     = 14,
2567         /**
2568          * credits for inode change during write.
2569          */
2570         [DTO_WRITE_BASE]    = 3,
2571         /**
2572          * credits for single block write.
2573          */
2574         [DTO_WRITE_BLOCK]   = 14,
2575         /**
2576          * Attr set credits for chown.
2577          * This is extra credits for setattr, and it is null without quota
2578          */
2579         [DTO_ATTR_SET_CHOWN] = 0
2580 };
2581
2582 /* reserve or free quota for some operation */
2583 static int osd_reserve_or_free_quota(const struct lu_env *env,
2584                                      struct dt_device *dev,
2585                                      enum quota_type type, __u64 uid,
2586                                      __u64 gid, __s64 count, bool is_md)
2587 {
2588         int rc;
2589         struct osd_device       *osd = osd_dt_dev(dev);
2590         struct osd_thread_info  *info = osd_oti_get(env);
2591         struct lquota_id_info   *qi = &info->oti_qi;
2592         struct qsd_instance     *qsd = NULL;
2593
2594         ENTRY;
2595
2596         if (is_md)
2597                 qsd = osd->od_quota_slave_md;
2598         else
2599                 qsd = osd->od_quota_slave_dt;
2600
2601         rc = quota_reserve_or_free(env, qsd, qi, type, uid, gid, count, is_md);
2602         RETURN(rc);
2603 }
2604
2605 static const struct dt_device_operations osd_dt_ops = {
2606         .dt_root_get              = osd_root_get,
2607         .dt_statfs                = osd_statfs,
2608         .dt_trans_create          = osd_trans_create,
2609         .dt_trans_start           = osd_trans_start,
2610         .dt_trans_stop            = osd_trans_stop,
2611         .dt_trans_cb_add          = osd_trans_cb_add,
2612         .dt_conf_get              = osd_conf_get,
2613         .dt_mnt_sb_get            = osd_mnt_sb_get,
2614         .dt_sync                  = osd_sync,
2615         .dt_ro                    = osd_ro,
2616         .dt_commit_async          = osd_commit_async,
2617         .dt_reserve_or_free_quota = osd_reserve_or_free_quota,
2618 };
2619
2620 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2621                           unsigned int role)
2622 {
2623         struct osd_object *obj = osd_dt_obj(dt);
2624         struct osd_thread_info *oti = osd_oti_get(env);
2625
2626         LINVRNT(osd_invariant(obj));
2627
2628         LASSERT(obj->oo_owner != env);
2629         down_read_nested(&obj->oo_sem, role);
2630
2631         LASSERT(obj->oo_owner == NULL);
2632         oti->oti_r_locks++;
2633 }
2634
2635 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2636                            unsigned int role)
2637 {
2638         struct osd_object *obj = osd_dt_obj(dt);
2639         struct osd_thread_info *oti = osd_oti_get(env);
2640
2641         LINVRNT(osd_invariant(obj));
2642
2643         LASSERT(obj->oo_owner != env);
2644         down_write_nested(&obj->oo_sem, role);
2645
2646         LASSERT(obj->oo_owner == NULL);
2647         obj->oo_owner = env;
2648         oti->oti_w_locks++;
2649 }
2650
2651 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2652 {
2653         struct osd_object *obj = osd_dt_obj(dt);
2654         struct osd_thread_info *oti = osd_oti_get(env);
2655
2656         LINVRNT(osd_invariant(obj));
2657
2658         LASSERT(oti->oti_r_locks > 0);
2659         oti->oti_r_locks--;
2660         up_read(&obj->oo_sem);
2661 }
2662
2663 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2664 {
2665         struct osd_object *obj = osd_dt_obj(dt);
2666         struct osd_thread_info *oti = osd_oti_get(env);
2667
2668         LINVRNT(osd_invariant(obj));
2669
2670         LASSERT(obj->oo_owner == env);
2671         LASSERT(oti->oti_w_locks > 0);
2672         oti->oti_w_locks--;
2673         obj->oo_owner = NULL;
2674         up_write(&obj->oo_sem);
2675 }
2676
2677 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2678 {
2679         struct osd_object *obj = osd_dt_obj(dt);
2680
2681         LINVRNT(osd_invariant(obj));
2682
2683         return obj->oo_owner == env;
2684 }
2685
2686 static void osd_inode_getattr(const struct lu_env *env,
2687                               struct inode *inode, struct lu_attr *attr)
2688 {
2689         attr->la_valid  |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2690                            LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2691                            LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2692                            LA_BLKSIZE | LA_TYPE | LA_BTIME;
2693
2694         attr->la_atime = inode->i_atime.tv_sec;
2695         attr->la_mtime = inode->i_mtime.tv_sec;
2696         attr->la_ctime = inode->i_ctime.tv_sec;
2697         attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2698         attr->la_mode    = inode->i_mode;
2699         attr->la_size    = i_size_read(inode);
2700         attr->la_blocks  = inode->i_blocks;
2701         attr->la_uid     = i_uid_read(inode);
2702         attr->la_gid     = i_gid_read(inode);
2703         attr->la_projid  = i_projid_read(inode);
2704         attr->la_flags   = ll_inode_to_ext_flags(inode->i_flags);
2705         attr->la_nlink   = inode->i_nlink;
2706         attr->la_rdev    = inode->i_rdev;
2707         attr->la_blksize = 1 << inode->i_blkbits;
2708         attr->la_blkbits = inode->i_blkbits;
2709         /*
2710          * Ext4 did not transfer inherit flags from raw inode
2711          * to inode flags, and ext4 internally test raw inode
2712          * @i_flags directly. Instead of patching ext4, we do it here.
2713          */
2714         if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2715                 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2716 }
2717
2718 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2719                             u64 *count)
2720 {
2721         struct osd_object *obj = osd_dt_obj(dt);
2722         const struct dt_it_ops *iops;
2723         struct dt_it *it;
2724         int rc;
2725
2726         ENTRY;
2727
2728         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2729         LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2730
2731         if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2732                 *count = obj->oo_dirent_count;
2733                 RETURN(0);
2734         }
2735
2736         /* directory not initialized yet */
2737         if (!dt->do_index_ops) {
2738                 *count = 0;
2739                 RETURN(0);
2740         }
2741
2742         iops = &dt->do_index_ops->dio_it;
2743         it = iops->init(env, dt, LUDA_64BITHASH);
2744         if (IS_ERR(it))
2745                 RETURN(PTR_ERR(it));
2746
2747         rc = iops->load(env, it, 0);
2748         if (rc < 0) {
2749                 if (rc == -ENODATA) {
2750                         rc = 0;
2751                         *count = 0;
2752                 }
2753                 GOTO(out, rc);
2754         }
2755         if (rc > 0)
2756                 rc = iops->next(env, it);
2757
2758         for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2759                 if (rc == -ESTALE)
2760                         continue;
2761
2762                 if (iops->key_size(env, it) == 0)
2763                         continue;
2764
2765                 (*count)++;
2766         }
2767         if (rc == 1) {
2768                 obj->oo_dirent_count = *count;
2769                 rc = 0;
2770         }
2771 out:
2772         iops->put(env, it);
2773         iops->fini(env, it);
2774
2775         RETURN(rc);
2776 }
2777
2778 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2779                         struct lu_attr *attr)
2780 {
2781         struct osd_object *obj = osd_dt_obj(dt);
2782         int rc = 0;
2783
2784         if (unlikely(!dt_object_exists(dt)))
2785                 return -ENOENT;
2786         if (unlikely(obj->oo_destroyed))
2787                 return -ENOENT;
2788
2789         LASSERT(!dt_object_remote(dt));
2790         LINVRNT(osd_invariant(obj));
2791
2792         spin_lock(&obj->oo_guard);
2793         osd_inode_getattr(env, obj->oo_inode, attr);
2794         if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2795                 attr->la_valid |= LA_FLAGS;
2796                 attr->la_flags |= LUSTRE_ORPHAN_FL;
2797         }
2798         if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2799                 attr->la_valid |= LA_FLAGS;
2800                 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2801         }
2802         spin_unlock(&obj->oo_guard);
2803
2804         if (S_ISDIR(obj->oo_inode->i_mode) &&
2805             fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2806                 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2807
2808         return rc;
2809 }
2810
2811 static int osd_declare_attr_qid(const struct lu_env *env,
2812                                 struct osd_object *obj,
2813                                 struct osd_thandle *oh, long long bspace,
2814                                 qid_t old_id, qid_t new_id, bool enforce,
2815                                 unsigned int type)
2816 {
2817         int rc;
2818         struct osd_thread_info *info = osd_oti_get(env);
2819         struct lquota_id_info  *qi = &info->oti_qi;
2820
2821         qi->lqi_type = type;
2822         /* inode accounting */
2823         qi->lqi_is_blk = false;
2824
2825         /* one more inode for the new id ... */
2826         qi->lqi_id.qid_uid = new_id;
2827         qi->lqi_space      = 1;
2828         /* Reserve credits for the new id */
2829         rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2830         if (rc == -EDQUOT || rc == -EINPROGRESS)
2831                 rc = 0;
2832         if (rc)
2833                 RETURN(rc);
2834
2835         /* and one less inode for the current id */
2836         qi->lqi_id.qid_uid = old_id;
2837         qi->lqi_space = -1;
2838         rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2839         if (rc == -EDQUOT || rc == -EINPROGRESS)
2840                 rc = 0;
2841         if (rc)
2842                 RETURN(rc);
2843
2844         /* block accounting */
2845         qi->lqi_is_blk = true;
2846
2847         /* more blocks for the new id ... */
2848         qi->lqi_id.qid_uid = new_id;
2849         qi->lqi_space      = bspace;
2850         /*
2851          * Credits for the new uid has been reserved, re-use "obj"
2852          * to save credit reservation.
2853          */
2854         rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2855         if (rc == -EDQUOT || rc == -EINPROGRESS)
2856                 rc = 0;
2857         if (rc)
2858                 RETURN(rc);
2859
2860         /* and finally less blocks for the current uid */
2861         qi->lqi_id.qid_uid = old_id;
2862         qi->lqi_space      = -bspace;
2863         rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2864         if (rc == -EDQUOT || rc == -EINPROGRESS)
2865                 rc = 0;
2866
2867         RETURN(rc);
2868 }
2869
2870 static int osd_declare_attr_set(const struct lu_env *env,
2871                                 struct dt_object *dt,
2872                                 const struct lu_attr *attr,
2873                                 struct thandle *handle)
2874 {
2875         struct osd_thandle *oh;
2876         struct osd_object *obj;
2877         qid_t uid;
2878         qid_t gid;
2879         long long bspace;
2880         int rc = 0;
2881         bool enforce;
2882
2883         ENTRY;
2884
2885         LASSERT(dt != NULL);
2886         LASSERT(handle != NULL);
2887
2888         obj = osd_dt_obj(dt);
2889         LASSERT(osd_invariant(obj));
2890
2891         oh = container_of(handle, struct osd_thandle, ot_super);
2892         LASSERT(oh->ot_handle == NULL);
2893
2894         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2895                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2896
2897         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2898                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2899
2900         if (attr == NULL || obj->oo_inode == NULL)
2901                 RETURN(rc);
2902
2903         bspace   = obj->oo_inode->i_blocks << 9;
2904         bspace   = toqb(bspace);
2905
2906         /*
2907          * Changing ownership is always preformed by super user, it should not
2908          * fail with EDQUOT unless required explicitly.
2909          *
2910          * We still need to call the osd_declare_qid() to calculate the journal
2911          * credits for updating quota accounting files and to trigger quota
2912          * space adjustment once the operation is completed.
2913          */
2914         if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2915                 /* USERQUOTA */
2916                 uid = i_uid_read(obj->oo_inode);
2917                 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2918                 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2919                                           attr->la_uid, enforce, USRQUOTA);
2920                 if (rc)
2921                         RETURN(rc);
2922
2923                 gid = i_gid_read(obj->oo_inode);
2924                 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
2925                        attr->la_uid, gid, attr->la_gid);
2926                 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2927                 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
2928                                           attr->la_gid, enforce, GRPQUOTA);
2929                 if (rc)
2930                         RETURN(rc);
2931
2932         }
2933 #ifdef HAVE_PROJECT_QUOTA
2934         if (attr->la_valid & LA_PROJID) {
2935                 __u32 projid = i_projid_read(obj->oo_inode);
2936
2937                 enforce = (attr->la_valid & LA_PROJID) &&
2938                                         (attr->la_projid != projid);
2939                 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2940                                           (qid_t)projid, (qid_t)attr->la_projid,
2941                                           enforce, PRJQUOTA);
2942                 if (rc)
2943                         RETURN(rc);
2944         }
2945 #endif
2946         RETURN(rc);
2947 }
2948
2949 static int osd_inode_setattr(const struct lu_env *env,
2950                              struct inode *inode, const struct lu_attr *attr)
2951 {
2952         __u64 bits = attr->la_valid;
2953
2954         /* Only allow set size for regular file */
2955         if (!S_ISREG(inode->i_mode))
2956                 bits &= ~(LA_SIZE | LA_BLOCKS);
2957
2958         if (bits == 0)
2959                 return 0;
2960
2961         if (bits & LA_ATIME)
2962                 inode->i_atime = osd_inode_time(inode, attr->la_atime);
2963         if (bits & LA_CTIME)
2964                 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
2965         if (bits & LA_MTIME)
2966                 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
2967         if (bits & LA_SIZE) {
2968                 spin_lock(&inode->i_lock);
2969                 LDISKFS_I(inode)->i_disksize = attr->la_size;
2970                 i_size_write(inode, attr->la_size);
2971                 spin_unlock(&inode->i_lock);
2972         }
2973
2974         /*
2975          * OSD should not change "i_blocks" which is used by quota.
2976          * "i_blocks" should be changed by ldiskfs only.
2977          */
2978         if (bits & LA_MODE)
2979                 inode->i_mode = (inode->i_mode & S_IFMT) |
2980                                 (attr->la_mode & ~S_IFMT);
2981         if (bits & LA_UID)
2982                 i_uid_write(inode, attr->la_uid);
2983         if (bits & LA_GID)
2984                 i_gid_write(inode, attr->la_gid);
2985         if (bits & LA_PROJID)
2986                 i_projid_write(inode, attr->la_projid);
2987         if (bits & LA_NLINK)
2988                 set_nlink(inode, attr->la_nlink);
2989         if (bits & LA_RDEV)
2990                 inode->i_rdev = attr->la_rdev;
2991
2992         if (bits & LA_FLAGS) {
2993                 /* always keep S_NOCMTIME */
2994                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2995                                  S_NOCMTIME;
2996 #if defined(S_ENCRYPTED)
2997                 /* Always remove S_ENCRYPTED, because ldiskfs must not be
2998                  * aware of encryption status. It is just stored into LMA
2999                  * so that it can be forwared to client side.
3000                  */
3001                 inode->i_flags &= ~S_ENCRYPTED;
3002 #endif
3003                 /*
3004                  * Ext4 did not transfer inherit flags from
3005                  * @inode->i_flags to raw inode i_flags when writing
3006                  * flags, we do it explictly here.
3007                  */
3008                 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
3009                         LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
3010                 else
3011                         LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
3012         }
3013         return 0;
3014 }
3015
3016 #ifdef HAVE_PROJECT_QUOTA
3017 static int osd_transfer_project(struct inode *inode, __u32 projid,
3018                                 struct thandle *handle)
3019 {
3020         struct super_block *sb = inode->i_sb;
3021         struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3022         int err;
3023         kprojid_t kprojid;
3024         struct ldiskfs_iloc iloc;
3025         struct ldiskfs_inode *raw_inode;
3026         struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3027
3028         if (!ldiskfs_has_feature_project(sb)) {
3029                 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3030                         == LDISKFS_DEF_PROJID);
3031                 if (projid != LDISKFS_DEF_PROJID)
3032                         return -EOPNOTSUPP;
3033                 else
3034                         return 0;
3035         }
3036
3037         if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3038                 return -EOPNOTSUPP;
3039
3040         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3041         if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3042                 return 0;
3043
3044         err = ldiskfs_get_inode_loc(inode, &iloc);
3045         if (err)
3046                 return err;
3047
3048         raw_inode = ldiskfs_raw_inode(&iloc);
3049         if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3050                 struct osd_thandle *oh = container_of(handle,
3051                                                       struct osd_thandle,
3052                                                       ot_super);
3053                 /**
3054                  * try to expand inode size automatically.
3055                  */
3056                 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3057                 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3058                         err = -EOVERFLOW;
3059                         brelse(iloc.bh);
3060                         return err;
3061                 }
3062         }
3063         brelse(iloc.bh);
3064
3065         dquot_initialize(inode);
3066         transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3067         if (transfer_to[PRJQUOTA]) {
3068                 lock_dquot_transfer(inode);
3069                 err = __dquot_transfer(inode, transfer_to);
3070                 unlock_dquot_transfer(inode);
3071                 dqput(transfer_to[PRJQUOTA]);
3072                 if (err)
3073                         return err;
3074         }
3075
3076         return err;
3077 }
3078 #endif
3079
3080 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3081                               struct thandle *handle)
3082 {
3083         int rc;
3084
3085         if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3086             (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3087                 struct iattr iattr;
3088
3089                 CDEBUG(D_QUOTA,
3090                        "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3091                        inode->i_ino, i_uid_read(inode), attr->la_uid,
3092                        i_gid_read(inode), attr->la_gid);
3093
3094                 dquot_initialize(inode);
3095                 iattr.ia_valid = 0;
3096                 if (attr->la_valid & LA_UID)
3097                         iattr.ia_valid |= ATTR_UID;
3098                 if (attr->la_valid & LA_GID)
3099                         iattr.ia_valid |= ATTR_GID;
3100                 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3101                 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3102
3103                 lock_dquot_transfer(inode);
3104                 rc = dquot_transfer(inode, &iattr);
3105                 unlock_dquot_transfer(inode);
3106                 if (rc) {
3107                         CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3108                                osd_ino2name(inode), rc);
3109                         return rc;
3110                 }
3111         }
3112
3113         /* Handle project id transfer here properly */
3114         if (attr->la_valid & LA_PROJID &&
3115             attr->la_projid != i_projid_read(inode)) {
3116                 if (!projid_valid(make_kprojid(&init_user_ns, attr->la_projid)))
3117                         return -EINVAL;
3118 #ifdef HAVE_PROJECT_QUOTA
3119                 rc = osd_transfer_project(inode, attr->la_projid, handle);
3120 #else
3121                 rc = -ENOTSUPP;
3122 #endif
3123                 if (rc) {
3124                         CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3125                                osd_ino2name(inode), rc);
3126                         return rc;
3127                 }
3128         }
3129         return 0;
3130 }
3131
3132 static int osd_attr_set(const struct lu_env *env,
3133                         struct dt_object *dt,
3134                         const struct lu_attr *attr,
3135                         struct thandle *handle)
3136 {
3137         struct osd_object *obj = osd_dt_obj(dt);
3138         struct inode *inode;
3139         int rc;
3140
3141         if (!dt_object_exists(dt))
3142                 return -ENOENT;
3143
3144         LASSERT(handle != NULL);
3145         LASSERT(!dt_object_remote(dt));
3146         LASSERT(osd_invariant(obj));
3147
3148         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3149
3150         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3151             !osd_obj2dev(obj)->od_is_ost) {
3152                 struct osd_thread_info *oti = osd_oti_get(env);
3153                 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3154                 struct lu_fid *fid1 = &oti->oti_fid;
3155                 struct osd_inode_id *id = &oti->oti_id;
3156                 struct iam_path_descr *ipd;
3157                 struct iam_container *bag;
3158                 struct osd_thandle *oh;
3159                 int rc;
3160
3161                 fid_cpu_to_be(fid1, fid0);
3162                 memset(id, 1, sizeof(*id));
3163                 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3164                                   fid0)->oi_dir.od_container;
3165                 ipd = osd_idx_ipd_get(env, bag);
3166                 if (unlikely(ipd == NULL))
3167                         RETURN(-ENOMEM);
3168
3169                 oh = container_of(handle, struct osd_thandle, ot_super);
3170                 rc = iam_update(oh->ot_handle, bag,
3171                                 (const struct iam_key *)fid1,
3172                                 (const struct iam_rec *)id, ipd);
3173                 osd_ipd_put(env, bag, ipd);
3174                 return(rc > 0 ? 0 : rc);
3175         }
3176
3177         inode = obj->oo_inode;
3178