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