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