Whamcloud - gitweb
LU-1302 mgs: mgs uses llog over OSD
[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, 2012, Whamcloud, Inc.
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 /* simple_mkdir() */
57 #include <lvfs.h>
58
59 /*
60  * struct OBD_{ALLOC,FREE}*()
61  * OBD_FAIL_CHECK
62  */
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66
67 /* fid_is_local() */
68 #include <lustre_fid.h>
69
70 #include "osd_internal.h"
71 #include "osd_igif.h"
72
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
76 #include <lquota.h>
77
78 #ifdef HAVE_LDISKFS_PDO
79 int ldiskfs_pdo = 1;
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81                 "ldiskfs with parallel directory operations");
82 #else
83 int ldiskfs_pdo = 0;
84 #endif
85
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
89
90 static const struct lu_object_operations      osd_lu_obj_ops;
91 static const struct dt_object_operations      osd_obj_ops;
92 static const struct dt_object_operations      osd_obj_ea_ops;
93 static const struct dt_object_operations      osd_obj_otable_it_ops;
94 static const struct dt_index_operations       osd_index_iam_ops;
95 static const struct dt_index_operations       osd_index_ea_ops;
96
97 static int osd_has_index(const struct osd_object *obj)
98 {
99         return obj->oo_dt.do_index_ops != NULL;
100 }
101
102 static int osd_object_invariant(const struct lu_object *l)
103 {
104         return osd_invariant(osd_obj(l));
105 }
106
107 /*
108  * Concurrency: doesn't matter
109  */
110 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
111 {
112         return osd_oti_get(env)->oti_r_locks > 0;
113 }
114
115 /*
116  * Concurrency: doesn't matter
117  */
118 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
119 {
120         struct osd_thread_info *oti = osd_oti_get(env);
121         return oti->oti_w_locks > 0 && o->oo_owner == env;
122 }
123
124 /*
125  * Concurrency: doesn't access mutable data
126  */
127 static int osd_root_get(const struct lu_env *env,
128                         struct dt_device *dev, struct lu_fid *f)
129 {
130         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
131         return 0;
132 }
133
134 /*
135  * OSD object methods.
136  */
137
138 /*
139  * Concurrency: no concurrent access is possible that early in object
140  * life-cycle.
141  */
142 static struct lu_object *osd_object_alloc(const struct lu_env *env,
143                                           const struct lu_object_header *hdr,
144                                           struct lu_device *d)
145 {
146         struct osd_object *mo;
147
148         OBD_ALLOC_PTR(mo);
149         if (mo != NULL) {
150                 struct lu_object *l;
151
152                 l = &mo->oo_dt.do_lu;
153                 dt_object_init(&mo->oo_dt, NULL, d);
154                 if (osd_dev(d)->od_iop_mode)
155                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
156                 else
157                         mo->oo_dt.do_ops = &osd_obj_ops;
158
159                 l->lo_ops = &osd_lu_obj_ops;
160                 cfs_init_rwsem(&mo->oo_sem);
161                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
162                 cfs_spin_lock_init(&mo->oo_guard);
163                 return l;
164         } else {
165                 return NULL;
166         }
167 }
168
169 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
170                        struct lustre_mdt_attrs *lma)
171 {
172         int rc;
173
174         dentry->d_inode = inode;
175         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
176                                    sizeof(*lma));
177         if (rc > 0) {
178                 /* Check LMA compatibility */
179                 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
180                         CWARN("%.16s: unsupported incompat LMA feature(s) "
181                               "%lx/%#x\n",
182                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
183                               inode->i_ino, le32_to_cpu(lma->lma_incompat) &
184                                                         ~LMA_INCOMPAT_SUPP);
185                         rc = -ENOSYS;
186                 } else {
187                         lustre_lma_swab(lma);
188                         rc = 0;
189                 }
190         } else if (rc == 0) {
191                 rc = -ENODATA;
192         }
193
194         return rc;
195 }
196
197 /*
198  * retrieve object from backend ext fs.
199  **/
200 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
201                        struct osd_inode_id *id)
202 {
203         struct inode *inode = NULL;
204
205         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
206         if (IS_ERR(inode)) {
207                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
208                        id->oii_ino, PTR_ERR(inode));
209         } else if (id->oii_gen != OSD_OII_NOGEN &&
210                    inode->i_generation != id->oii_gen) {
211                 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
212                        "gen1 = %u\n",
213                        id->oii_ino, id->oii_gen, inode->i_generation);
214                 iput(inode);
215                 inode = ERR_PTR(-ESTALE);
216         } else if (inode->i_nlink == 0) {
217                 /* due to parallel readdir and unlink,
218                 * we can have dead inode here. */
219                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
220                 make_bad_inode(inode);
221                 iput(inode);
222                 inode = ERR_PTR(-ESTALE);
223         } else if (is_bad_inode(inode)) {
224                 CWARN("%.16s: bad inode: ino = %u\n",
225                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
226                 iput(inode);
227                 inode = ERR_PTR(-ENOENT);
228         } else {
229                 if (id->oii_gen == OSD_OII_NOGEN)
230                         osd_id_gen(id, inode->i_ino, inode->i_generation);
231
232                 /* Do not update file c/mtime in ldiskfs.
233                  * NB: we don't have any lock to protect this because we don't
234                  * have reference on osd_object now, but contention with
235                  * another lookup + attr_set can't happen in the tiny window
236                  * between if (...) and set S_NOCMTIME. */
237                 if (!(inode->i_flags & S_NOCMTIME))
238                         inode->i_flags |= S_NOCMTIME;
239         }
240         return inode;
241 }
242
243 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
244                            struct osd_inode_id *id, struct lu_fid *fid)
245 {
246         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
247         struct inode            *inode;
248         int                      rc;
249
250         inode = osd_iget(info, dev, id);
251         if (IS_ERR(inode))
252                 return inode;
253
254         rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
255         if (rc == 0) {
256                 *fid = lma->lma_self_fid;
257         } else if (rc == -ENODATA) {
258                 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
259         } else {
260                 iput(inode);
261                 inode = ERR_PTR(rc);
262         }
263         return inode;
264 }
265
266 static struct inode *
267 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
268                 struct osd_inode_id *id, const struct lu_fid *fid)
269 {
270         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
271         struct inode            *inode;
272         int                      rc;
273
274         inode = osd_iget(info, dev, id);
275         if (IS_ERR(inode))
276                 return inode;
277
278         rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
279         if (rc != 0) {
280                 if (rc == -ENODATA) {
281                         CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
282                                inode->i_ino, PFID(fid));
283                         rc = -EREMCHG;
284                 }
285                 iput(inode);
286                 return ERR_PTR(rc);
287         }
288
289         if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
290                 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
291                        PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
292                 iput(inode);
293                 return ERR_PTR(EREMCHG);
294         }
295         return inode;
296 }
297
298 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
299                           const struct lu_fid *fid,
300                           const struct lu_object_conf *conf)
301 {
302         struct osd_thread_info *info;
303         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
304         struct osd_device      *dev;
305         struct osd_idmap_cache *oic;
306         struct osd_inode_id    *id;
307         struct inode           *inode;
308         struct osd_scrub       *scrub;
309         struct scrub_file      *sf;
310         int                     result;
311         int                     verify = 0;
312         ENTRY;
313
314         LINVRNT(osd_invariant(obj));
315         LASSERT(obj->oo_inode == NULL);
316         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
317
318         dev = osd_dev(ldev);
319         scrub = &dev->od_scrub;
320         sf = &scrub->os_file;
321         info = osd_oti_get(env);
322         LASSERT(info);
323         oic = &info->oti_cache;
324         id  = &oic->oic_lid;
325
326         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
327                 RETURN(-ENOENT);
328
329         if (fid_is_norm(fid)) {
330                 /* Search order: 1. per-thread cache. */
331                 if (lu_fid_eq(fid, &oic->oic_fid)) {
332                         goto iget;
333                 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
334                         /* Search order: 2. OI scrub pending list. */
335                         result = osd_oii_lookup(dev, fid, id);
336                         if (result == 0)
337                                 goto iget;
338                 }
339
340                 if (sf->sf_flags & SF_INCONSISTENT)
341                         verify = 1;
342         }
343
344         /*
345          * Objects are created as locking anchors or place holders for objects
346          * yet to be created. No need to osd_oi_lookup() at here because FID
347          * shouldn't never be re-used, if it's really a duplicate FID from
348          * unexpected reason, we should be able to detect it later by calling
349          * do_create->osd_oi_insert()
350          */
351         if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
352                 GOTO(out, result = 0);
353
354         /* Search order: 3. OI files. */
355         result = osd_oi_lookup(info, dev, fid, id);
356         if (result == -ENOENT) {
357                 if (!fid_is_norm(fid) ||
358                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
359                                       sf->sf_oi_bitmap))
360                         GOTO(out, result = 0);
361
362                 goto trigger;
363         }
364
365         if (result != 0)
366                 GOTO(out, result);
367
368 iget:
369         if (verify == 0)
370                 inode = osd_iget(info, dev, id);
371         else
372                 inode = osd_iget_verify(info, dev, id, fid);
373         if (IS_ERR(inode)) {
374                 result = PTR_ERR(inode);
375                 if (result == -ENOENT || result == -ESTALE) {
376                         fid_zero(&oic->oic_fid);
377                         result = 0;
378                 } else if (result == -EREMCHG) {
379
380 trigger:
381                         if (thread_is_running(&scrub->os_thread)) {
382                                 result = -EINPROGRESS;
383                         } else if (!scrub->os_no_scrub) {
384                                 result = osd_scrub_start(dev);
385                                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
386                                                "for "DFID", rc = %d [1]\n",
387                                                LDISKFS_SB(osd_sb(dev))->s_es->\
388                                                s_volume_name,PFID(fid), result);
389                                 if (result == 0 || result == -EALREADY)
390                                         result = -EINPROGRESS;
391                                 else
392                                         result = -EREMCHG;
393                         }
394                 }
395
396                 GOTO(out, result);
397         }
398
399         obj->oo_inode = inode;
400         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
401         if (dev->od_iop_mode) {
402                 obj->oo_compat_dot_created = 1;
403                 obj->oo_compat_dotdot_created = 1;
404         }
405
406         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
407                 GOTO(out, result = 0);
408
409         LASSERT(obj->oo_hl_head == NULL);
410         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
411         if (obj->oo_hl_head == NULL) {
412                 obj->oo_inode = NULL;
413                 iput(inode);
414                 GOTO(out, result = -ENOMEM);
415         }
416         GOTO(out, result = 0);
417
418 out:
419         LINVRNT(osd_invariant(obj));
420         return result;
421 }
422
423 /*
424  * Concurrency: shouldn't matter.
425  */
426 static void osd_object_init0(struct osd_object *obj)
427 {
428         LASSERT(obj->oo_inode != NULL);
429         obj->oo_dt.do_body_ops = &osd_body_ops;
430         obj->oo_dt.do_lu.lo_header->loh_attr |=
431                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
432 }
433
434 /*
435  * Concurrency: no concurrent access is possible that early in object
436  * life-cycle.
437  */
438 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
439                            const struct lu_object_conf *conf)
440 {
441         struct osd_object *obj = osd_obj(l);
442         int result;
443
444         LINVRNT(osd_invariant(obj));
445
446         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
447         obj->oo_dt.do_body_ops = &osd_body_ops_new;
448         if (result == 0) {
449                 if (obj->oo_inode != NULL) {
450                         osd_object_init0(obj);
451                 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
452                         obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
453                         /* LFSCK iterator object is special without inode */
454                         l->lo_header->loh_attr |= LOHA_EXISTS;
455                 }
456         }
457         LINVRNT(osd_invariant(obj));
458         return result;
459 }
460
461 /*
462  * Concurrency: no concurrent access is possible that late in object
463  * life-cycle.
464  */
465 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
466 {
467         struct osd_object *obj = osd_obj(l);
468
469         LINVRNT(osd_invariant(obj));
470
471         dt_object_fini(&obj->oo_dt);
472         if (obj->oo_hl_head != NULL)
473                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
474         OBD_FREE_PTR(obj);
475 }
476
477 /*
478  * Concurrency: no concurrent access is possible that late in object
479  * life-cycle.
480  */
481 static void osd_index_fini(struct osd_object *o)
482 {
483         struct iam_container *bag;
484
485         if (o->oo_dir != NULL) {
486                 bag = &o->oo_dir->od_container;
487                 if (o->oo_inode != NULL) {
488                         if (bag->ic_object == o->oo_inode)
489                                 iam_container_fini(bag);
490                 }
491                 OBD_FREE_PTR(o->oo_dir);
492                 o->oo_dir = NULL;
493         }
494 }
495
496 /*
497  * Concurrency: no concurrent access is possible that late in object
498  * life-cycle (for all existing callers, that is. New callers have to provide
499  * their own locking.)
500  */
501 static int osd_inode_unlinked(const struct inode *inode)
502 {
503         return inode->i_nlink == 0;
504 }
505
506 enum {
507         OSD_TXN_OI_DELETE_CREDITS    = 20,
508         OSD_TXN_INODE_DELETE_CREDITS = 20
509 };
510
511 /*
512  * Journal
513  */
514
515 #if OSD_THANDLE_STATS
516 /**
517  * Set time when the handle is allocated
518  */
519 static void osd_th_alloced(struct osd_thandle *oth)
520 {
521         oth->oth_alloced = cfs_time_current();
522 }
523
524 /**
525  * Set time when the handle started
526  */
527 static void osd_th_started(struct osd_thandle *oth)
528 {
529         oth->oth_started = cfs_time_current();
530 }
531
532 /**
533  * Helper function to convert time interval to microseconds packed in
534  * long int (default time units for the counter in "stats" initialized
535  * by lu_time_init() )
536  */
537 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
538 {
539         struct timeval val;
540
541         cfs_duration_usec(cfs_time_sub(end, start), &val);
542         return val.tv_sec * 1000000 + val.tv_usec;
543 }
544
545 /**
546  * Check whether the we deal with this handle for too long.
547  */
548 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
549                                 cfs_time_t alloced, cfs_time_t started,
550                                 cfs_time_t closed)
551 {
552         cfs_time_t now = cfs_time_current();
553
554         LASSERT(dev != NULL);
555
556         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
557                             interval_to_usec(alloced, started));
558         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
559                             interval_to_usec(started, closed));
560         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
561                             interval_to_usec(closed, now));
562
563         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
564                 CWARN("transaction handle %p was open for too long: "
565                       "now "CFS_TIME_T" ,"
566                       "alloced "CFS_TIME_T" ,"
567                       "started "CFS_TIME_T" ,"
568                       "closed "CFS_TIME_T"\n",
569                       oth, now, alloced, started, closed);
570                 libcfs_debug_dumpstack(NULL);
571         }
572 }
573
574 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
575 {                                                                       \
576         cfs_time_t __closed = cfs_time_current();                       \
577         cfs_time_t __alloced = oth->oth_alloced;                        \
578         cfs_time_t __started = oth->oth_started;                        \
579                                                                         \
580         expr;                                                           \
581         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
582 }
583
584 #else /* OSD_THANDLE_STATS */
585
586 #define osd_th_alloced(h)                  do {} while(0)
587 #define osd_th_started(h)                  do {} while(0)
588 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
589
590 #endif /* OSD_THANDLE_STATS */
591
592 /*
593  * Concurrency: doesn't access mutable data.
594  */
595 static int osd_param_is_sane(const struct osd_device *dev,
596                              const struct thandle *th)
597 {
598         struct osd_thandle *oh;
599         oh = container_of0(th, struct osd_thandle, ot_super);
600         return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
601 }
602
603 /*
604  * Concurrency: shouldn't matter.
605  */
606 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
607 static void osd_trans_commit_cb(struct super_block *sb,
608                                 struct journal_callback *jcb, int error)
609 #else
610 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
611 #endif
612 {
613         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
614         struct thandle     *th  = &oh->ot_super;
615         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
616         struct dt_txn_commit_cb *dcb, *tmp;
617
618         LASSERT(oh->ot_handle == NULL);
619
620         if (error)
621                 CERROR("transaction @0x%p commit error: %d\n", th, error);
622
623         dt_txn_hook_commit(th);
624
625         /* call per-transaction callbacks if any */
626         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
627                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
628                          "commit callback entry: magic=%x name='%s'\n",
629                          dcb->dcb_magic, dcb->dcb_name);
630                 cfs_list_del_init(&dcb->dcb_linkage);
631                 dcb->dcb_func(NULL, th, dcb, error);
632         }
633
634         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
635         lu_device_put(lud);
636         th->th_dev = NULL;
637
638         lu_context_exit(&th->th_ctx);
639         lu_context_fini(&th->th_ctx);
640         OBD_FREE_PTR(oh);
641 }
642
643 static struct thandle *osd_trans_create(const struct lu_env *env,
644                                         struct dt_device *d)
645 {
646         struct osd_thread_info *oti = osd_oti_get(env);
647         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
648         struct osd_thandle     *oh;
649         struct thandle         *th;
650         ENTRY;
651
652         /* on pending IO in this thread should left from prev. request */
653         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
654
655         th = ERR_PTR(-ENOMEM);
656         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
657         if (oh != NULL) {
658                 oh->ot_quota_trans = &oti->oti_quota_trans;
659                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
660                 th = &oh->ot_super;
661                 th->th_dev = d;
662                 th->th_result = 0;
663                 th->th_tags = LCT_TX_HANDLE;
664                 oh->ot_credits = 0;
665                 oti->oti_dev = osd_dt_dev(d);
666                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
667                 osd_th_alloced(oh);
668         }
669         RETURN(th);
670 }
671
672 /*
673  * Concurrency: shouldn't matter.
674  */
675 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
676                     struct thandle *th)
677 {
678         struct osd_thread_info *oti = osd_oti_get(env);
679         struct osd_device  *dev = osd_dt_dev(d);
680         handle_t           *jh;
681         struct osd_thandle *oh;
682         int rc;
683
684         ENTRY;
685
686         LASSERT(current->journal_info == NULL);
687
688         oh = container_of0(th, struct osd_thandle, ot_super);
689         LASSERT(oh != NULL);
690         LASSERT(oh->ot_handle == NULL);
691
692         rc = dt_txn_hook_start(env, d, th);
693         if (rc != 0)
694                 GOTO(out, rc);
695
696         if (!osd_param_is_sane(dev, th)) {
697                 CWARN("%.16s: too many transaction credits (%d > %d)\n",
698                       LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
699                       oh->ot_credits,
700                       osd_journal(dev)->j_max_transaction_buffers);
701                 /* XXX Limit the credits to 'max_transaction_buffers', and
702                  *     let the underlying filesystem to catch the error if
703                  *     we really need so many credits.
704                  *
705                  *     This should be removed when we can calculate the
706                  *     credits precisely. */
707                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
708 #ifdef OSD_TRACK_DECLARES
709                 CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
710                        oh->ot_declare_attr_set, oh->ot_declare_punch,
711                        oh->ot_declare_xattr_set);
712                 CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
713                        oh->ot_declare_create, oh->ot_declare_ref_add,
714                        oh->ot_declare_ref_del, oh->ot_declare_write);
715                 CERROR("  insert: %d, delete: %d, destroy: %d\n",
716                        oh->ot_declare_insert, oh->ot_declare_delete,
717                        oh->ot_declare_destroy);
718 #endif
719         }
720
721         /*
722          * XXX temporary stuff. Some abstraction layer should
723          * be used.
724          */
725         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
726         osd_th_started(oh);
727         if (!IS_ERR(jh)) {
728                 oh->ot_handle = jh;
729                 LASSERT(oti->oti_txns == 0);
730                 lu_context_init(&th->th_ctx, th->th_tags);
731                 lu_context_enter(&th->th_ctx);
732
733                 lu_device_get(&d->dd_lu_dev);
734                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
735                                              "osd-tx", th);
736                 oti->oti_txns++;
737                 rc = 0;
738         } else {
739                 rc = PTR_ERR(jh);
740         }
741 out:
742         RETURN(rc);
743 }
744
745 /*
746  * Concurrency: shouldn't matter.
747  */
748 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
749 {
750         int                     rc = 0;
751         struct osd_thandle     *oh;
752         struct osd_thread_info *oti = osd_oti_get(env);
753         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
754         struct qsd_instance    *qsd = oti->oti_dev->od_quota_slave;
755         ENTRY;
756
757         oh = container_of0(th, struct osd_thandle, ot_super);
758
759         if (qsd != NULL)
760                 /* inform the quota slave device that the transaction is
761                  * stopping */
762                 qsd_op_end(env, qsd, oh->ot_quota_trans);
763         oh->ot_quota_trans = NULL;
764
765         if (oh->ot_handle != NULL) {
766                 handle_t *hdl = oh->ot_handle;
767
768                 /*
769                  * add commit callback
770                  * notice we don't do this in osd_trans_start()
771                  * as underlying transaction can change during truncate
772                  */
773                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
774                                          &oh->ot_jcb);
775
776                 LASSERT(oti->oti_txns == 1);
777                 oti->oti_txns--;
778                 rc = dt_txn_hook_stop(env, th);
779                 if (rc != 0)
780                         CERROR("Failure in transaction hook: %d\n", rc);
781
782                 /* hook functions might modify th_sync */
783                 hdl->h_sync = th->th_sync;
784
785                 oh->ot_handle = NULL;
786                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
787                                   rc = ldiskfs_journal_stop(hdl));
788                 if (rc != 0)
789                         CERROR("Failure to stop transaction: %d\n", rc);
790         } else {
791                 OBD_FREE_PTR(oh);
792         }
793
794         /* as we want IO to journal and data IO be concurrent, we don't block
795          * awaiting data IO completion in osd_do_bio(), instead we wait here
796          * once transaction is submitted to the journal. all reqular requests
797          * don't do direct IO (except read/write), thus this wait_event becomes
798          * no-op for them.
799          *
800          * IMPORTANT: we have to wait till any IO submited by the thread is
801          * completed otherwise iobuf may be corrupted by different request
802          */
803         cfs_wait_event(iobuf->dr_wait,
804                        cfs_atomic_read(&iobuf->dr_numreqs) == 0);
805         if (!rc)
806                 rc = iobuf->dr_error;
807
808         RETURN(rc);
809 }
810
811 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
812 {
813         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
814                                                ot_super);
815
816         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
817         LASSERT(&dcb->dcb_func != NULL);
818         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
819
820         return 0;
821 }
822
823 /*
824  * Called just before object is freed. Releases all resources except for
825  * object itself (that is released by osd_object_free()).
826  *
827  * Concurrency: no concurrent access is possible that late in object
828  * life-cycle.
829  */
830 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
831 {
832         struct osd_object *obj   = osd_obj(l);
833         struct inode      *inode = obj->oo_inode;
834
835         LINVRNT(osd_invariant(obj));
836
837         /*
838          * If object is unlinked remove fid->ino mapping from object index.
839          */
840
841         osd_index_fini(obj);
842         if (inode != NULL) {
843                 iput(inode);
844                 obj->oo_inode = NULL;
845         }
846 }
847
848 /*
849  * Concurrency: ->loo_object_release() is called under site spin-lock.
850  */
851 static void osd_object_release(const struct lu_env *env,
852                                struct lu_object *l)
853 {
854 }
855
856 /*
857  * Concurrency: shouldn't matter.
858  */
859 static int osd_object_print(const struct lu_env *env, void *cookie,
860                             lu_printer_t p, const struct lu_object *l)
861 {
862         struct osd_object *o = osd_obj(l);
863         struct iam_descr  *d;
864
865         if (o->oo_dir != NULL)
866                 d = o->oo_dir->od_container.ic_descr;
867         else
868                 d = NULL;
869         return (*p)(env, cookie,
870                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
871                     o, o->oo_inode,
872                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
873                     o->oo_inode ? o->oo_inode->i_generation : 0,
874                     d ? d->id_ops->id_name : "plain");
875 }
876
877 /*
878  * Concurrency: shouldn't matter.
879  */
880 int osd_statfs(const struct lu_env *env, struct dt_device *d,
881                struct obd_statfs *sfs)
882 {
883         struct osd_device  *osd = osd_dt_dev(d);
884         struct super_block *sb = osd_sb(osd);
885         struct kstatfs     *ksfs;
886         int result = 0;
887
888         if (unlikely(osd->od_mnt == NULL))
889                 return -EINPROGRESS;
890
891         /* osd_lproc.c call this without env, allocate ksfs for that case */
892         if (unlikely(env == NULL)) {
893                 OBD_ALLOC_PTR(ksfs);
894                 if (ksfs == NULL)
895                         return -ENOMEM;
896         } else {
897                 ksfs = &osd_oti_get(env)->oti_ksfs;
898         }
899
900         cfs_spin_lock(&osd->od_osfs_lock);
901         /* cache 1 second */
902         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
903                 result = sb->s_op->statfs(sb->s_root, ksfs);
904                 if (likely(result == 0)) { /* N.B. statfs can't really fail */
905                         osd->od_osfs_age = cfs_time_current_64();
906                         statfs_pack(&osd->od_statfs, ksfs);
907                 }
908         }
909
910         if (likely(result == 0))
911                 *sfs = osd->od_statfs;
912         cfs_spin_unlock(&osd->od_osfs_lock);
913
914         if (unlikely(env == NULL))
915                 OBD_FREE_PTR(ksfs);
916
917         return result;
918 }
919
920 /*
921  * Concurrency: doesn't access mutable data.
922  */
923 static void osd_conf_get(const struct lu_env *env,
924                          const struct dt_device *dev,
925                          struct dt_device_param *param)
926 {
927         struct super_block *sb = osd_sb(osd_dt_dev(dev));
928
929         /*
930          * XXX should be taken from not-yet-existing fs abstraction layer.
931          */
932         param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
933         param->ddp_max_name_len = LDISKFS_NAME_LEN;
934         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
935         param->ddp_block_shift  = sb->s_blocksize_bits;
936         param->ddp_mount_type     = LDD_MT_LDISKFS;
937         param->ddp_mntopts      = 0;
938         if (test_opt(sb, XATTR_USER))
939                 param->ddp_mntopts |= MNTOPT_USERXATTR;
940         if (test_opt(sb, POSIX_ACL))
941                 param->ddp_mntopts |= MNTOPT_ACL;
942
943 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
944         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
945                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
946         else
947 #endif
948                 param->ddp_max_ea_size = sb->s_blocksize;
949
950 }
951
952 /**
953  * Helper function to get and fill the buffer with input values.
954  */
955 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
956 {
957         struct lu_buf *buf;
958
959         buf = &osd_oti_get(env)->oti_buf;
960         buf->lb_buf = area;
961         buf->lb_len = len;
962         return buf;
963 }
964
965 /*
966  * Concurrency: shouldn't matter.
967  */
968 static int osd_sync(const struct lu_env *env, struct dt_device *d)
969 {
970         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
971         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
972 }
973
974 /**
975  * Start commit for OSD device.
976  *
977  * An implementation of dt_commit_async method for OSD device.
978  * Asychronously starts underlayng fs sync and thereby a transaction
979  * commit.
980  *
981  * \param env environment
982  * \param d dt device
983  *
984  * \see dt_device_operations
985  */
986 static int osd_commit_async(const struct lu_env *env,
987                             struct dt_device *d)
988 {
989         struct super_block *s = osd_sb(osd_dt_dev(d));
990         ENTRY;
991
992         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
993         RETURN(s->s_op->sync_fs(s, 0));
994 }
995
996 /*
997  * Concurrency: shouldn't matter.
998  */
999
1000 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1001 {
1002         struct super_block *sb = osd_sb(osd_dt_dev(d));
1003         int rc;
1004         ENTRY;
1005
1006         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1007
1008         rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1009         RETURN(rc);
1010 }
1011
1012 /*
1013  * Concurrency: serialization provided by callers.
1014  */
1015 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1016                               int mode, unsigned long timeout, __u32 alg,
1017                               struct lustre_capa_key *keys)
1018 {
1019         struct osd_device *dev = osd_dt_dev(d);
1020         ENTRY;
1021
1022         dev->od_fl_capa = mode;
1023         dev->od_capa_timeout = timeout;
1024         dev->od_capa_alg = alg;
1025         dev->od_capa_keys = keys;
1026         RETURN(0);
1027 }
1028
1029 /**
1030  * Concurrency: serialization provided by callers.
1031  */
1032 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1033                                struct dt_quota_ctxt *ctxt, void *data)
1034 {
1035         struct obd_device *obd = (void *)ctxt;
1036         struct vfsmount *mnt = (struct vfsmount *)data;
1037         ENTRY;
1038
1039         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1040         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1041         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1042         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1043         obd->obd_lvfs_ctxt.fs = get_ds();
1044
1045         EXIT;
1046 }
1047
1048 /**
1049  * Note: we do not count into QUOTA here.
1050  * If we mount with --data_journal we may need more.
1051  */
1052 const int osd_dto_credits_noquota[DTO_NR] = {
1053         /**
1054          * Insert/Delete.
1055          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1056          * SINGLEDATA_TRANS_BLOCKS(8)
1057          * XXX Note: maybe iam need more, since iam have more level than
1058          *           EXT3 htree.
1059          */
1060         [DTO_INDEX_INSERT]  = 16,
1061         [DTO_INDEX_DELETE]  = 16,
1062         /**
1063          * Used for OI scrub
1064          */
1065         [DTO_INDEX_UPDATE]  = 16,
1066         /**
1067          * Create a object. The same as create object in EXT3.
1068          * DATA_TRANS_BLOCKS(14) +
1069          * INDEX_EXTRA_BLOCKS(8) +
1070          * 3(inode bits, groups, GDT)
1071          */
1072         [DTO_OBJECT_CREATE] = 25,
1073         /**
1074          * XXX: real credits to be fixed
1075          */
1076         [DTO_OBJECT_DELETE] = 25,
1077         /**
1078          * Attr set credits (inode)
1079          */
1080         [DTO_ATTR_SET_BASE] = 1,
1081         /**
1082          * Xattr set. The same as xattr of EXT3.
1083          * DATA_TRANS_BLOCKS(14)
1084          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1085          * are also counted in. Do not know why?
1086          */
1087         [DTO_XATTR_SET]     = 14,
1088         [DTO_LOG_REC]       = 14,
1089         /**
1090          * credits for inode change during write.
1091          */
1092         [DTO_WRITE_BASE]    = 3,
1093         /**
1094          * credits for single block write.
1095          */
1096         [DTO_WRITE_BLOCK]   = 14,
1097         /**
1098          * Attr set credits for chown.
1099          * This is extra credits for setattr, and it is null without quota
1100          */
1101         [DTO_ATTR_SET_CHOWN]= 0
1102 };
1103
1104 static const struct dt_device_operations osd_dt_ops = {
1105         .dt_root_get       = osd_root_get,
1106         .dt_statfs         = osd_statfs,
1107         .dt_trans_create   = osd_trans_create,
1108         .dt_trans_start    = osd_trans_start,
1109         .dt_trans_stop     = osd_trans_stop,
1110         .dt_trans_cb_add   = osd_trans_cb_add,
1111         .dt_conf_get       = osd_conf_get,
1112         .dt_sync           = osd_sync,
1113         .dt_ro             = osd_ro,
1114         .dt_commit_async   = osd_commit_async,
1115         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1116         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1117 };
1118
1119 static void osd_object_read_lock(const struct lu_env *env,
1120                                  struct dt_object *dt, unsigned role)
1121 {
1122         struct osd_object *obj = osd_dt_obj(dt);
1123         struct osd_thread_info *oti = osd_oti_get(env);
1124
1125         LINVRNT(osd_invariant(obj));
1126
1127         LASSERT(obj->oo_owner != env);
1128         cfs_down_read_nested(&obj->oo_sem, role);
1129
1130         LASSERT(obj->oo_owner == NULL);
1131         oti->oti_r_locks++;
1132 }
1133
1134 static void osd_object_write_lock(const struct lu_env *env,
1135                                   struct dt_object *dt, unsigned role)
1136 {
1137         struct osd_object *obj = osd_dt_obj(dt);
1138         struct osd_thread_info *oti = osd_oti_get(env);
1139
1140         LINVRNT(osd_invariant(obj));
1141
1142         LASSERT(obj->oo_owner != env);
1143         cfs_down_write_nested(&obj->oo_sem, role);
1144
1145         LASSERT(obj->oo_owner == NULL);
1146         obj->oo_owner = env;
1147         oti->oti_w_locks++;
1148 }
1149
1150 static void osd_object_read_unlock(const struct lu_env *env,
1151                                    struct dt_object *dt)
1152 {
1153         struct osd_object *obj = osd_dt_obj(dt);
1154         struct osd_thread_info *oti = osd_oti_get(env);
1155
1156         LINVRNT(osd_invariant(obj));
1157
1158         LASSERT(oti->oti_r_locks > 0);
1159         oti->oti_r_locks--;
1160         cfs_up_read(&obj->oo_sem);
1161 }
1162
1163 static void osd_object_write_unlock(const struct lu_env *env,
1164                                     struct dt_object *dt)
1165 {
1166         struct osd_object *obj = osd_dt_obj(dt);
1167         struct osd_thread_info *oti = osd_oti_get(env);
1168
1169         LINVRNT(osd_invariant(obj));
1170
1171         LASSERT(obj->oo_owner == env);
1172         LASSERT(oti->oti_w_locks > 0);
1173         oti->oti_w_locks--;
1174         obj->oo_owner = NULL;
1175         cfs_up_write(&obj->oo_sem);
1176 }
1177
1178 static int osd_object_write_locked(const struct lu_env *env,
1179                                    struct dt_object *dt)
1180 {
1181         struct osd_object *obj = osd_dt_obj(dt);
1182
1183         LINVRNT(osd_invariant(obj));
1184
1185         return obj->oo_owner == env;
1186 }
1187
1188 static int capa_is_sane(const struct lu_env *env,
1189                         struct osd_device *dev,
1190                         struct lustre_capa *capa,
1191                         struct lustre_capa_key *keys)
1192 {
1193         struct osd_thread_info *oti = osd_oti_get(env);
1194         struct lustre_capa *tcapa = &oti->oti_capa;
1195         struct obd_capa *oc;
1196         int i, rc = 0;
1197         ENTRY;
1198
1199         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1200         if (oc) {
1201                 if (capa_is_expired(oc)) {
1202                         DEBUG_CAPA(D_ERROR, capa, "expired");
1203                         rc = -ESTALE;
1204                 }
1205                 capa_put(oc);
1206                 RETURN(rc);
1207         }
1208
1209         if (capa_is_expired_sec(capa)) {
1210                 DEBUG_CAPA(D_ERROR, capa, "expired");
1211                 RETURN(-ESTALE);
1212         }
1213
1214         cfs_spin_lock(&capa_lock);
1215         for (i = 0; i < 2; i++) {
1216                 if (keys[i].lk_keyid == capa->lc_keyid) {
1217                         oti->oti_capa_key = keys[i];
1218                         break;
1219                 }
1220         }
1221         cfs_spin_unlock(&capa_lock);
1222
1223         if (i == 2) {
1224                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1225                 RETURN(-ESTALE);
1226         }
1227
1228         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1229         if (rc)
1230                 RETURN(rc);
1231
1232         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1233                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1234                 RETURN(-EACCES);
1235         }
1236
1237         oc = capa_add(dev->od_capa_hash, capa);
1238         capa_put(oc);
1239
1240         RETURN(0);
1241 }
1242
1243 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1244                     struct lustre_capa *capa, __u64 opc)
1245 {
1246         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1247         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1248         struct md_capainfo *ci;
1249         int rc;
1250
1251         if (!dev->od_fl_capa)
1252                 return 0;
1253
1254         if (capa == BYPASS_CAPA)
1255                 return 0;
1256
1257         ci = md_capainfo(env);
1258         if (unlikely(!ci))
1259                 return 0;
1260
1261         if (ci->mc_auth == LC_ID_NONE)
1262                 return 0;
1263
1264         if (!capa) {
1265                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1266                 return -EACCES;
1267         }
1268
1269         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1270                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1271                            PFID(fid));
1272                 return -EACCES;
1273         }
1274
1275         if (!capa_opc_supported(capa, opc)) {
1276                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1277                 return -EACCES;
1278         }
1279
1280         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1281                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1282                 return -EACCES;
1283         }
1284
1285         return 0;
1286 }
1287
1288 static struct timespec *osd_inode_time(const struct lu_env *env,
1289                                        struct inode *inode, __u64 seconds)
1290 {
1291         struct osd_thread_info  *oti = osd_oti_get(env);
1292         struct timespec         *t   = &oti->oti_time;
1293
1294         t->tv_sec = seconds;
1295         t->tv_nsec = 0;
1296         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1297         return t;
1298 }
1299
1300
1301 static void osd_inode_getattr(const struct lu_env *env,
1302                               struct inode *inode, struct lu_attr *attr)
1303 {
1304         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1305                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1306                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1307
1308         attr->la_atime      = LTIME_S(inode->i_atime);
1309         attr->la_mtime      = LTIME_S(inode->i_mtime);
1310         attr->la_ctime      = LTIME_S(inode->i_ctime);
1311         attr->la_mode       = inode->i_mode;
1312         attr->la_size       = i_size_read(inode);
1313         attr->la_blocks     = inode->i_blocks;
1314         attr->la_uid        = inode->i_uid;
1315         attr->la_gid        = inode->i_gid;
1316         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1317         attr->la_nlink      = inode->i_nlink;
1318         attr->la_rdev       = inode->i_rdev;
1319         attr->la_blksize    = 1 << inode->i_blkbits;
1320         attr->la_blkbits    = inode->i_blkbits;
1321 }
1322
1323 static int osd_attr_get(const struct lu_env *env,
1324                         struct dt_object *dt,
1325                         struct lu_attr *attr,
1326                         struct lustre_capa *capa)
1327 {
1328         struct osd_object *obj = osd_dt_obj(dt);
1329
1330         LASSERT(dt_object_exists(dt));
1331         LINVRNT(osd_invariant(obj));
1332
1333         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1334                 return -EACCES;
1335
1336         cfs_spin_lock(&obj->oo_guard);
1337         osd_inode_getattr(env, obj->oo_inode, attr);
1338         cfs_spin_unlock(&obj->oo_guard);
1339         return 0;
1340 }
1341
1342 static int osd_declare_attr_set(const struct lu_env *env,
1343                                 struct dt_object *dt,
1344                                 const struct lu_attr *attr,
1345                                 struct thandle *handle)
1346 {
1347         struct osd_thandle     *oh;
1348         struct osd_object      *obj;
1349         struct osd_thread_info *info = osd_oti_get(env);
1350         struct lquota_id_info  *qi = &info->oti_qi;
1351         long long               bspace;
1352         int                     rc = 0;
1353         bool                    allocated;
1354         ENTRY;
1355
1356         LASSERT(dt != NULL);
1357         LASSERT(handle != NULL);
1358
1359         obj = osd_dt_obj(dt);
1360         LASSERT(osd_invariant(obj));
1361
1362         oh = container_of0(handle, struct osd_thandle, ot_super);
1363         LASSERT(oh->ot_handle == NULL);
1364
1365         OSD_DECLARE_OP(oh, attr_set);
1366         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1367
1368         if (attr == NULL || obj->oo_inode == NULL)
1369                 RETURN(rc);
1370
1371         bspace   = obj->oo_inode->i_blocks;
1372         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1373         bspace   = toqb(bspace);
1374
1375         /* Changing ownership is always preformed by super user, it should not
1376          * fail with EDQUOT.
1377          *
1378          * We still need to call the osd_declare_qid() to calculate the journal
1379          * credits for updating quota accounting files and to trigger quota
1380          * space adjustment once the operation is completed.*/
1381         if ((attr->la_valid & LA_UID) != 0 &&
1382              attr->la_uid != obj->oo_inode->i_uid) {
1383                 qi->lqi_type = USRQUOTA;
1384
1385                 /* inode accounting */
1386                 qi->lqi_is_blk = false;
1387
1388                 /* one more inode for the new owner ... */
1389                 qi->lqi_id.qid_uid = attr->la_uid;
1390                 qi->lqi_space      = 1;
1391                 allocated = (attr->la_uid == 0) ? true : false;
1392                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1393                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1394                         rc = 0;
1395                 if (rc)
1396                         RETURN(rc);
1397
1398                 /* and one less inode for the current uid */
1399                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1400                 qi->lqi_space      = -1;
1401                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1402                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1403                         rc = 0;
1404                 if (rc)
1405                         RETURN(rc);
1406
1407                 /* block accounting */
1408                 qi->lqi_is_blk = true;
1409
1410                 /* more blocks for the new owner ... */
1411                 qi->lqi_id.qid_uid = attr->la_uid;
1412                 qi->lqi_space      = bspace;
1413                 allocated = (attr->la_uid == 0) ? true : false;
1414                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1415                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1416                         rc = 0;
1417                 if (rc)
1418                         RETURN(rc);
1419
1420                 /* and finally less blocks for the current owner */
1421                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1422                 qi->lqi_space      = -bspace;
1423                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1424                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1425                         rc = 0;
1426                 if (rc)
1427                         RETURN(rc);
1428         }
1429
1430         if (attr->la_valid & LA_GID &&
1431             attr->la_gid != obj->oo_inode->i_gid) {
1432                 qi->lqi_type = GRPQUOTA;
1433
1434                 /* inode accounting */
1435                 qi->lqi_is_blk = false;
1436
1437                 /* one more inode for the new group owner ... */
1438                 qi->lqi_id.qid_gid = attr->la_gid;
1439                 qi->lqi_space      = 1;
1440                 allocated = (attr->la_gid == 0) ? true : false;
1441                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1442                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1443                         rc = 0;
1444                 if (rc)
1445                         RETURN(rc);
1446
1447                 /* and one less inode for the current gid */
1448                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1449                 qi->lqi_space      = -1;
1450                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1451                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1452                         rc = 0;
1453                 if (rc)
1454                         RETURN(rc);
1455
1456                 /* block accounting */
1457                 qi->lqi_is_blk = true;
1458
1459                 /* more blocks for the new owner ... */
1460                 qi->lqi_id.qid_gid = attr->la_gid;
1461                 qi->lqi_space      = bspace;
1462                 allocated = (attr->la_gid == 0) ? true : false;
1463                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1464                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1465                         rc = 0;
1466                 if (rc)
1467                         RETURN(rc);
1468
1469                 /* and finally less blocks for the current owner */
1470                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1471                 qi->lqi_space      = -bspace;
1472                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1473                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1474                         rc = 0;
1475                 if (rc)
1476                         RETURN(rc);
1477         }
1478
1479         RETURN(rc);
1480 }
1481
1482 static int osd_inode_setattr(const struct lu_env *env,
1483                              struct inode *inode, const struct lu_attr *attr)
1484 {
1485         __u64 bits;
1486
1487         bits = attr->la_valid;
1488
1489         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1490
1491         if (bits & LA_ATIME)
1492                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1493         if (bits & LA_CTIME)
1494                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1495         if (bits & LA_MTIME)
1496                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1497         if (bits & LA_SIZE) {
1498                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1499                 i_size_write(inode, attr->la_size);
1500         }
1501
1502 #if 0
1503         /* OSD should not change "i_blocks" which is used by quota.
1504          * "i_blocks" should be changed by ldiskfs only. */
1505         if (bits & LA_BLOCKS)
1506                 inode->i_blocks = attr->la_blocks;
1507 #endif
1508         if (bits & LA_MODE)
1509                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1510                         (attr->la_mode & ~S_IFMT);
1511         if (bits & LA_UID)
1512                 inode->i_uid    = attr->la_uid;
1513         if (bits & LA_GID)
1514                 inode->i_gid    = attr->la_gid;
1515         if (bits & LA_NLINK)
1516                 inode->i_nlink  = attr->la_nlink;
1517         if (bits & LA_RDEV)
1518                 inode->i_rdev   = attr->la_rdev;
1519
1520         if (bits & LA_FLAGS) {
1521                 /* always keep S_NOCMTIME */
1522                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1523                                  S_NOCMTIME;
1524         }
1525         return 0;
1526 }
1527
1528 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1529 {
1530         if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1531             (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1532                 struct iattr    iattr;
1533                 int             rc;
1534
1535                 iattr.ia_valid = 0;
1536                 if (attr->la_valid & LA_UID)
1537                         iattr.ia_valid |= ATTR_UID;
1538                 if (attr->la_valid & LA_GID)
1539                         iattr.ia_valid |= ATTR_GID;
1540                 iattr.ia_uid = attr->la_uid;
1541                 iattr.ia_gid = attr->la_gid;
1542
1543                 rc = ll_vfs_dq_transfer(inode, &iattr);
1544                 if (rc) {
1545                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
1546                                "enforcement enabled on the ldiskfs filesystem?",
1547                                inode->i_sb->s_id, rc);
1548                         return rc;
1549                 }
1550         }
1551         return 0;
1552 }
1553
1554 static int osd_attr_set(const struct lu_env *env,
1555                         struct dt_object *dt,
1556                         const struct lu_attr *attr,
1557                         struct thandle *handle,
1558                         struct lustre_capa *capa)
1559 {
1560         struct osd_object *obj = osd_dt_obj(dt);
1561         struct inode      *inode;
1562         int rc;
1563
1564         LASSERT(handle != NULL);
1565         LASSERT(dt_object_exists(dt));
1566         LASSERT(osd_invariant(obj));
1567
1568         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1569                 return -EACCES;
1570
1571         OSD_EXEC_OP(handle, attr_set);
1572
1573         inode = obj->oo_inode;
1574
1575         rc = osd_quota_transfer(inode, attr);
1576         if (rc)
1577                 return rc;
1578
1579         cfs_spin_lock(&obj->oo_guard);
1580         rc = osd_inode_setattr(env, inode, attr);
1581         cfs_spin_unlock(&obj->oo_guard);
1582
1583         if (!rc)
1584                 inode->i_sb->s_op->dirty_inode(inode);
1585         return rc;
1586 }
1587
1588 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1589                                     struct osd_object *obj,
1590                                     const char *name, const int namelen)
1591 {
1592         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1593 }
1594
1595 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1596                       cfs_umode_t mode,
1597                       struct dt_allocation_hint *hint,
1598                       struct thandle *th)
1599 {
1600         int result;
1601         struct osd_device  *osd = osd_obj2dev(obj);
1602         struct osd_thandle *oth;
1603         struct dt_object   *parent = NULL;
1604         struct inode       *inode;
1605
1606         LINVRNT(osd_invariant(obj));
1607         LASSERT(obj->oo_inode == NULL);
1608         LASSERT(obj->oo_hl_head == NULL);
1609
1610         if (S_ISDIR(mode) && ldiskfs_pdo) {
1611                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1612                 if (obj->oo_hl_head == NULL)
1613                         return -ENOMEM;
1614         }
1615
1616         oth = container_of(th, struct osd_thandle, ot_super);
1617         LASSERT(oth->ot_handle->h_transaction != NULL);
1618
1619         if (hint && hint->dah_parent)
1620                 parent = hint->dah_parent;
1621
1622         inode = ldiskfs_create_inode(oth->ot_handle,
1623                                      parent ? osd_dt_obj(parent)->oo_inode :
1624                                               osd_sb(osd)->s_root->d_inode,
1625                                      mode);
1626         if (!IS_ERR(inode)) {
1627                 /* Do not update file c/mtime in ldiskfs.
1628                  * NB: don't need any lock because no contention at this
1629                  * early stage */
1630                 inode->i_flags |= S_NOCMTIME;
1631                 inode->i_state |= I_LUSTRE_NOSCRUB;
1632                 obj->oo_inode = inode;
1633                 result = 0;
1634         } else {
1635                 if (obj->oo_hl_head != NULL) {
1636                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1637                         obj->oo_hl_head = NULL;
1638                 }
1639                 result = PTR_ERR(inode);
1640         }
1641         LINVRNT(osd_invariant(obj));
1642         return result;
1643 }
1644
1645 enum {
1646         OSD_NAME_LEN = 255
1647 };
1648
1649 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1650                      struct lu_attr *attr,
1651                      struct dt_allocation_hint *hint,
1652                      struct dt_object_format *dof,
1653                      struct thandle *th)
1654 {
1655         int result;
1656         struct osd_thandle *oth;
1657         struct osd_device *osd = osd_obj2dev(obj);
1658         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1659
1660         LASSERT(S_ISDIR(attr->la_mode));
1661
1662         oth = container_of(th, struct osd_thandle, ot_super);
1663         LASSERT(oth->ot_handle->h_transaction != NULL);
1664         result = osd_mkfile(info, obj, mode, hint, th);
1665         if (result == 0 && osd->od_iop_mode == 0) {
1666                 LASSERT(obj->oo_inode != NULL);
1667                 /*
1668                  * XXX uh-oh... call low-level iam function directly.
1669                  */
1670
1671                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1672                                          sizeof (struct osd_fid_pack),
1673                                          oth->ot_handle);
1674         }
1675         return result;
1676 }
1677
1678 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1679                         struct lu_attr *attr,
1680                         struct dt_allocation_hint *hint,
1681                         struct dt_object_format *dof,
1682                         struct thandle *th)
1683 {
1684         int result;
1685         struct osd_thandle *oth;
1686         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1687
1688         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1689
1690         LASSERT(S_ISREG(attr->la_mode));
1691
1692         oth = container_of(th, struct osd_thandle, ot_super);
1693         LASSERT(oth->ot_handle->h_transaction != NULL);
1694
1695         result = osd_mkfile(info, obj, mode, hint, th);
1696         if (result == 0) {
1697                 LASSERT(obj->oo_inode != NULL);
1698                 if (feat->dif_flags & DT_IND_VARKEY)
1699                         result = iam_lvar_create(obj->oo_inode,
1700                                                  feat->dif_keysize_max,
1701                                                  feat->dif_ptrsize,
1702                                                  feat->dif_recsize_max,
1703                                                  oth->ot_handle);
1704                 else
1705                         result = iam_lfix_create(obj->oo_inode,
1706                                                  feat->dif_keysize_max,
1707                                                  feat->dif_ptrsize,
1708                                                  feat->dif_recsize_max,
1709                                                  oth->ot_handle);
1710
1711         }
1712         return result;
1713 }
1714
1715 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1716                      struct lu_attr *attr,
1717                      struct dt_allocation_hint *hint,
1718                      struct dt_object_format *dof,
1719                      struct thandle *th)
1720 {
1721         LASSERT(S_ISREG(attr->la_mode));
1722         return osd_mkfile(info, obj, (attr->la_mode &
1723                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1724 }
1725
1726 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1727                      struct lu_attr *attr,
1728                      struct dt_allocation_hint *hint,
1729                      struct dt_object_format *dof,
1730                      struct thandle *th)
1731 {
1732         LASSERT(S_ISLNK(attr->la_mode));
1733         return osd_mkfile(info, obj, (attr->la_mode &
1734                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1735 }
1736
1737 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1738                      struct lu_attr *attr,
1739                      struct dt_allocation_hint *hint,
1740                      struct dt_object_format *dof,
1741                      struct thandle *th)
1742 {
1743         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1744         int result;
1745
1746         LINVRNT(osd_invariant(obj));
1747         LASSERT(obj->oo_inode == NULL);
1748         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1749                 S_ISFIFO(mode) || S_ISSOCK(mode));
1750
1751         result = osd_mkfile(info, obj, mode, hint, th);
1752         if (result == 0) {
1753                 LASSERT(obj->oo_inode != NULL);
1754                 /*
1755                  * This inode should be marked dirty for i_rdev.  Currently
1756                  * that is done in the osd_attr_init().
1757                  */
1758                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1759         }
1760         LINVRNT(osd_invariant(obj));
1761         return result;
1762 }
1763
1764 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1765                               struct lu_attr *,
1766                               struct dt_allocation_hint *hint,
1767                               struct dt_object_format *dof,
1768                               struct thandle *);
1769
1770 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1771 {
1772         osd_obj_type_f result;
1773
1774         switch (type) {
1775         case DFT_DIR:
1776                 result = osd_mkdir;
1777                 break;
1778         case DFT_REGULAR:
1779                 result = osd_mkreg;
1780                 break;
1781         case DFT_SYM:
1782                 result = osd_mksym;
1783                 break;
1784         case DFT_NODE:
1785                 result = osd_mknod;
1786                 break;
1787         case DFT_INDEX:
1788                 result = osd_mk_index;
1789                 break;
1790
1791         default:
1792                 LBUG();
1793                 break;
1794         }
1795         return result;
1796 }
1797
1798
1799 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1800                         struct dt_object *parent, struct dt_object *child,
1801                         cfs_umode_t child_mode)
1802 {
1803         LASSERT(ah);
1804
1805         memset(ah, 0, sizeof(*ah));
1806         ah->dah_parent = parent;
1807         ah->dah_mode = child_mode;
1808 }
1809
1810 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1811                           struct lu_attr *attr, struct dt_object_format *dof)
1812 {
1813         struct inode   *inode = obj->oo_inode;
1814         __u64           valid = attr->la_valid;
1815         int             result;
1816
1817         attr->la_valid &= ~(LA_TYPE | LA_MODE);
1818
1819         if (dof->dof_type != DFT_NODE)
1820                 attr->la_valid &= ~LA_RDEV;
1821         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1822                 attr->la_valid &= ~LA_ATIME;
1823         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1824                 attr->la_valid &= ~LA_CTIME;
1825         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1826                 attr->la_valid &= ~LA_MTIME;
1827
1828         result = osd_quota_transfer(inode, attr);
1829         if (result)
1830                 return;
1831
1832         if (attr->la_valid != 0) {
1833                 result = osd_inode_setattr(info->oti_env, inode, attr);
1834                 /*
1835                  * The osd_inode_setattr() should always succeed here.  The
1836                  * only error that could be returned is EDQUOT when we are
1837                  * trying to change the UID or GID of the inode. However, this
1838                  * should not happen since quota enforcement is no longer
1839                  * enabled on ldiskfs (lquota takes care of it).
1840                  */
1841                 LASSERTF(result == 0, "%d", result);
1842                 inode->i_sb->s_op->dirty_inode(inode);
1843         }
1844
1845         attr->la_valid = valid;
1846 }
1847
1848 /**
1849  * Helper function for osd_object_create()
1850  *
1851  * \retval 0, on success
1852  */
1853 static int __osd_object_create(struct osd_thread_info *info,
1854                                struct osd_object *obj, struct lu_attr *attr,
1855                                struct dt_allocation_hint *hint,
1856                                struct dt_object_format *dof,
1857                                struct thandle *th)
1858 {
1859         int     result;
1860         __u32   umask;
1861
1862         /* we drop umask so that permissions we pass are not affected */
1863         umask = current->fs->umask;
1864         current->fs->umask = 0;
1865
1866         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1867                                                   th);
1868         if (result == 0) {
1869                 osd_attr_init(info, obj, attr, dof);
1870                 osd_object_init0(obj);
1871                 /* bz 24037 */
1872                 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1873                         unlock_new_inode(obj->oo_inode);
1874         }
1875
1876         /* restore previous umask value */
1877         current->fs->umask = umask;
1878
1879         return result;
1880 }
1881
1882 /**
1883  * Helper function for osd_object_create()
1884  *
1885  * \retval 0, on success
1886  */
1887 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1888                            const struct lu_fid *fid, struct thandle *th)
1889 {
1890         struct osd_thread_info *info = osd_oti_get(env);
1891         struct osd_inode_id    *id   = &info->oti_id;
1892         struct osd_device      *osd  = osd_obj2dev(obj);
1893
1894         LASSERT(obj->oo_inode != NULL);
1895
1896         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1897         return osd_oi_insert(info, osd, fid, id, th);
1898 }
1899
1900 static int osd_declare_object_create(const struct lu_env *env,
1901                                      struct dt_object *dt,
1902                                      struct lu_attr *attr,
1903                                      struct dt_allocation_hint *hint,
1904                                      struct dt_object_format *dof,
1905                                      struct thandle *handle)
1906 {
1907         struct osd_thandle      *oh;
1908         int                      rc;
1909         ENTRY;
1910
1911         LASSERT(handle != NULL);
1912
1913         oh = container_of0(handle, struct osd_thandle, ot_super);
1914         LASSERT(oh->ot_handle == NULL);
1915
1916         OSD_DECLARE_OP(oh, create);
1917         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1918         /* XXX: So far, only normal fid needs be inserted into the oi,
1919          *      things could be changed later. Revise following code then. */
1920         if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1921                 OSD_DECLARE_OP(oh, insert);
1922                 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1923                 /* Reuse idle OI block may cause additional one OI block
1924                  * to be changed. */
1925                 oh->ot_credits += 1;
1926         }
1927         /* If this is directory, then we expect . and .. to be inserted as
1928          * well. The one directory block always needs to be created for the
1929          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1930          * block), there is no danger of needing a tree for the first block.
1931          */
1932         if (attr && S_ISDIR(attr->la_mode)) {
1933                 OSD_DECLARE_OP(oh, insert);
1934                 OSD_DECLARE_OP(oh, insert);
1935                 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1936         }
1937
1938         if (!attr)
1939                 RETURN(0);
1940
1941         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
1942                                    false, false, NULL, false);
1943         RETURN(rc);
1944 }
1945
1946 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1947                              struct lu_attr *attr,
1948                              struct dt_allocation_hint *hint,
1949                              struct dt_object_format *dof,
1950                              struct thandle *th)
1951 {
1952         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1953         struct osd_object      *obj    = osd_dt_obj(dt);
1954         struct osd_thread_info *info   = osd_oti_get(env);
1955         int result;
1956
1957         ENTRY;
1958
1959         LINVRNT(osd_invariant(obj));
1960         LASSERT(!dt_object_exists(dt));
1961         LASSERT(osd_write_locked(env, obj));
1962         LASSERT(th != NULL);
1963
1964         if (unlikely(fid_is_acct(fid)))
1965                 /* Quota files can't be created from the kernel any more,
1966                  * 'tune2fs -O quota' will take care of creating them */
1967                 RETURN(-EPERM);
1968
1969         OSD_EXEC_OP(th, create);
1970
1971         result = __osd_object_create(info, obj, attr, hint, dof, th);
1972         if (result == 0)
1973                 result = __osd_oi_insert(env, obj, fid, th);
1974
1975         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1976         LASSERT(osd_invariant(obj));
1977         RETURN(result);
1978 }
1979
1980 /**
1981  * Called to destroy on-disk representation of the object
1982  *
1983  * Concurrency: must be locked
1984  */
1985 static int osd_declare_object_destroy(const struct lu_env *env,
1986                                       struct dt_object *dt,
1987                                       struct thandle *th)
1988 {
1989         struct osd_object  *obj = osd_dt_obj(dt);
1990         struct inode       *inode = obj->oo_inode;
1991         struct osd_thandle *oh;
1992         int                 rc;
1993         ENTRY;
1994
1995         oh = container_of0(th, struct osd_thandle, ot_super);
1996         LASSERT(oh->ot_handle == NULL);
1997         LASSERT(inode);
1998
1999         OSD_DECLARE_OP(oh, destroy);
2000         OSD_DECLARE_OP(oh, delete);
2001         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2002         /* XXX: So far, only normal fid needs to be inserted into the OI,
2003          *      so only normal fid needs to be removed from the OI also. */
2004         if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
2005                 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2006                 /* Recycle idle OI leaf may cause additional three OI blocks
2007                  * to be changed. */
2008                 oh->ot_credits += 3;
2009         }
2010
2011         /* one less inode */
2012         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2013                                    false, true, NULL, false);
2014         if (rc)
2015                 RETURN(rc);
2016         /* data to be truncated */
2017         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh, true,
2018                                    true, NULL, false);
2019         RETURN(rc);
2020 }
2021
2022 static int osd_object_destroy(const struct lu_env *env,
2023                               struct dt_object *dt,
2024                               struct thandle *th)
2025 {
2026         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2027         struct osd_object      *obj = osd_dt_obj(dt);
2028         struct inode           *inode = obj->oo_inode;
2029         struct osd_device      *osd = osd_obj2dev(obj);
2030         struct osd_thandle     *oh;
2031         int                     result;
2032         ENTRY;
2033
2034         oh = container_of0(th, struct osd_thandle, ot_super);
2035         LASSERT(oh->ot_handle);
2036         LASSERT(inode);
2037         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2038
2039         if (unlikely(fid_is_acct(fid)))
2040                 RETURN(-EPERM);
2041
2042         /* Parallel control for OI scrub. For most of cases, there is no
2043          * lock contention. So it will not affect unlink performance. */
2044         cfs_mutex_lock(&inode->i_mutex);
2045         if (S_ISDIR(inode->i_mode)) {
2046                 LASSERT(osd_inode_unlinked(inode) ||
2047                         inode->i_nlink == 1);
2048                 cfs_spin_lock(&obj->oo_guard);
2049                 inode->i_nlink = 0;
2050                 cfs_spin_unlock(&obj->oo_guard);
2051                 inode->i_sb->s_op->dirty_inode(inode);
2052         } else {
2053                 LASSERT(osd_inode_unlinked(inode));
2054         }
2055
2056         OSD_EXEC_OP(th, destroy);
2057
2058         result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2059         cfs_mutex_unlock(&inode->i_mutex);
2060
2061         /* XXX: add to ext3 orphan list */
2062         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2063
2064         /* not needed in the cache anymore */
2065         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2066
2067         RETURN(0);
2068 }
2069
2070 /**
2071  * Helper function for osd_xattr_set()
2072  */
2073 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2074                            const struct lu_buf *buf, const char *name, int fl)
2075 {
2076         struct osd_object      *obj      = osd_dt_obj(dt);
2077         struct inode           *inode    = obj->oo_inode;
2078         struct osd_thread_info *info     = osd_oti_get(env);
2079         struct dentry          *dentry   = &info->oti_child_dentry;
2080         int                     fs_flags = 0;
2081         int                     rc;
2082
2083         LASSERT(dt_object_exists(dt));
2084         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2085
2086         if (fl & LU_XATTR_REPLACE)
2087                 fs_flags |= XATTR_REPLACE;
2088
2089         if (fl & LU_XATTR_CREATE)
2090                 fs_flags |= XATTR_CREATE;
2091
2092         dentry->d_inode = inode;
2093         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2094                                    buf->lb_len, fs_flags);
2095         return rc;
2096 }
2097
2098 /**
2099  * Put the fid into lustre_mdt_attrs, and then place the structure
2100  * inode's ea. This fid should not be altered during the life time
2101  * of the inode.
2102  *
2103  * \retval +ve, on success
2104  * \retval -ve, on error
2105  *
2106  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2107  */
2108 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2109                           const struct lu_fid *fid)
2110 {
2111         struct osd_thread_info  *info      = osd_oti_get(env);
2112         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2113
2114         lustre_lma_init(mdt_attrs, fid);
2115         lustre_lma_swab(mdt_attrs);
2116         return __osd_xattr_set(env, dt,
2117                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2118                                XATTR_NAME_LMA, LU_XATTR_CREATE);
2119
2120 }
2121
2122 /**
2123  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2124  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2125  * To have compatilibility with 1.8 ldiskfs driver we need to have
2126  * magic number at start of fid data.
2127  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2128  * its inmemory API.
2129  */
2130 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2131                                   const struct dt_rec *fid)
2132 {
2133         param->edp_magic = LDISKFS_LUFID_MAGIC;
2134         param->edp_len =  sizeof(struct lu_fid) + 1;
2135
2136         fid_cpu_to_be((struct lu_fid *)param->edp_data,
2137                       (struct lu_fid *)fid);
2138 }
2139
2140 /**
2141  * Try to read the fid from inode ea into dt_rec, if return value
2142  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2143  *
2144  * \param fid object fid.
2145  *
2146  * \retval 0 on success
2147  */
2148 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2149                           __u32 ino, struct lu_fid *fid,
2150                           struct osd_inode_id *id)
2151 {
2152         struct osd_thread_info *info  = osd_oti_get(env);
2153         struct inode           *inode;
2154         ENTRY;
2155
2156         osd_id_gen(id, ino, OSD_OII_NOGEN);
2157         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2158         if (IS_ERR(inode))
2159                 RETURN(PTR_ERR(inode));
2160
2161         iput(inode);
2162         RETURN(0);
2163 }
2164
2165 /**
2166  * OSD layer object create function for interoperability mode (b11826).
2167  * This is mostly similar to osd_object_create(). Only difference being, fid is
2168  * inserted into inode ea here.
2169  *
2170  * \retval   0, on success
2171  * \retval -ve, on error
2172  */
2173 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2174                                 struct lu_attr *attr,
2175                                 struct dt_allocation_hint *hint,
2176                                 struct dt_object_format *dof,
2177                                 struct thandle *th)
2178 {
2179         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2180         struct osd_object      *obj    = osd_dt_obj(dt);
2181         struct osd_thread_info *info   = osd_oti_get(env);
2182         int                     result;
2183
2184         ENTRY;
2185
2186         LASSERT(osd_invariant(obj));
2187         LASSERT(!dt_object_exists(dt));
2188         LASSERT(osd_write_locked(env, obj));
2189         LASSERT(th != NULL);
2190
2191         if (unlikely(fid_is_acct(fid)))
2192                 /* Quota files can't be created from the kernel any more,
2193                  * 'tune2fs -O quota' will take care of creating them */
2194                 RETURN(-EPERM);
2195
2196         OSD_EXEC_OP(th, create);
2197
2198         result = __osd_object_create(info, obj, attr, hint, dof, th);
2199         /* objects under osd root shld have igif fid, so dont add fid EA */
2200         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2201                 result = osd_ea_fid_set(env, dt, fid);
2202
2203         if (result == 0)
2204                 result = __osd_oi_insert(env, obj, fid, th);
2205
2206         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2207         LINVRNT(osd_invariant(obj));
2208         RETURN(result);
2209 }
2210
2211 static int osd_declare_object_ref_add(const struct lu_env *env,
2212                                       struct dt_object *dt,
2213                                       struct thandle *handle)
2214 {
2215         struct osd_thandle *oh;
2216
2217         /* it's possible that object doesn't exist yet */
2218         LASSERT(handle != NULL);
2219
2220         oh = container_of0(handle, struct osd_thandle, ot_super);
2221         LASSERT(oh->ot_handle == NULL);
2222
2223         OSD_DECLARE_OP(oh, ref_add);
2224         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2225
2226         return 0;
2227 }
2228
2229 /*
2230  * Concurrency: @dt is write locked.
2231  */
2232 static int osd_object_ref_add(const struct lu_env *env,
2233                               struct dt_object *dt, struct thandle *th)
2234 {
2235         struct osd_object *obj = osd_dt_obj(dt);
2236         struct inode      *inode = obj->oo_inode;
2237
2238         LINVRNT(osd_invariant(obj));
2239         LASSERT(dt_object_exists(dt));
2240         LASSERT(osd_write_locked(env, obj));
2241         LASSERT(th != NULL);
2242
2243         OSD_EXEC_OP(th, ref_add);
2244
2245         /*
2246          * DIR_NLINK feature is set for compatibility reasons if:
2247          * 1) nlinks > LDISKFS_LINK_MAX, or
2248          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2249          *
2250          * It is easier to always set this flag (rather than check and set),
2251          * since it has less overhead, and the superblock will be dirtied
2252          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2253          * do not actually care whether this flag is set or not.
2254          */
2255         cfs_spin_lock(&obj->oo_guard);
2256         inode->i_nlink++;
2257         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2258                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2259                     inode->i_nlink == 2)
2260                         inode->i_nlink = 1;
2261         }
2262         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2263         cfs_spin_unlock(&obj->oo_guard);
2264         inode->i_sb->s_op->dirty_inode(inode);
2265         LINVRNT(osd_invariant(obj));
2266
2267         return 0;
2268 }
2269
2270 static int osd_declare_object_ref_del(const struct lu_env *env,
2271                                       struct dt_object *dt,
2272                                       struct thandle *handle)
2273 {
2274         struct osd_thandle *oh;
2275
2276         LASSERT(dt_object_exists(dt));
2277         LASSERT(handle != NULL);
2278
2279         oh = container_of0(handle, struct osd_thandle, ot_super);
2280         LASSERT(oh->ot_handle == NULL);
2281
2282         OSD_DECLARE_OP(oh, ref_del);
2283         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2284
2285         return 0;
2286 }
2287
2288 /*
2289  * Concurrency: @dt is write locked.
2290  */
2291 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2292                               struct thandle *th)
2293 {
2294         struct osd_object *obj = osd_dt_obj(dt);
2295         struct inode      *inode = obj->oo_inode;
2296
2297         LINVRNT(osd_invariant(obj));
2298         LASSERT(dt_object_exists(dt));
2299         LASSERT(osd_write_locked(env, obj));
2300         LASSERT(th != NULL);
2301
2302         OSD_EXEC_OP(th, ref_del);
2303
2304         cfs_spin_lock(&obj->oo_guard);
2305         LASSERT(inode->i_nlink > 0);
2306         inode->i_nlink--;
2307         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2308          * then the nlink count is 1. Don't let it be set to 0 or the directory
2309          * inode will be deleted incorrectly. */
2310         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2311                 inode->i_nlink++;
2312         cfs_spin_unlock(&obj->oo_guard);
2313         inode->i_sb->s_op->dirty_inode(inode);
2314         LINVRNT(osd_invariant(obj));
2315
2316         return 0;
2317 }
2318
2319 /*
2320  * Get the 64-bit version for an inode.
2321  */
2322 static int osd_object_version_get(const struct lu_env *env,
2323                                   struct dt_object *dt, dt_obj_version_t *ver)
2324 {
2325         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2326
2327         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2328                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2329         *ver = LDISKFS_I(inode)->i_fs_version;
2330         return 0;
2331 }
2332
2333 /*
2334  * Concurrency: @dt is read locked.
2335  */
2336 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2337                          struct lu_buf *buf, const char *name,
2338                          struct lustre_capa *capa)
2339 {
2340         struct osd_object      *obj    = osd_dt_obj(dt);
2341         struct inode           *inode  = obj->oo_inode;
2342         struct osd_thread_info *info   = osd_oti_get(env);
2343         struct dentry          *dentry = &info->oti_obj_dentry;
2344
2345         /* version get is not real XATTR but uses xattr API */
2346         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2347                 /* for version we are just using xattr API but change inode
2348                  * field instead */
2349                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2350                 osd_object_version_get(env, dt, buf->lb_buf);
2351                 return sizeof(dt_obj_version_t);
2352         }
2353
2354         LASSERT(dt_object_exists(dt));
2355         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2356
2357         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2358                 return -EACCES;
2359
2360         dentry->d_inode = inode;
2361         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2362 }
2363
2364
2365 static int osd_declare_xattr_set(const struct lu_env *env,
2366                                  struct dt_object *dt,
2367                                  const struct lu_buf *buf, const char *name,
2368                                  int fl, struct thandle *handle)
2369 {
2370         struct osd_thandle *oh;
2371
2372         LASSERT(handle != NULL);
2373
2374         oh = container_of0(handle, struct osd_thandle, ot_super);
2375         LASSERT(oh->ot_handle == NULL);
2376
2377         OSD_DECLARE_OP(oh, xattr_set);
2378         if (strcmp(name, XATTR_NAME_VERSION) == 0)
2379                 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2380         else
2381                 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2382
2383         return 0;
2384 }
2385
2386 /*
2387  * Set the 64-bit version for object
2388  */
2389 static void osd_object_version_set(const struct lu_env *env,
2390                                    struct dt_object *dt,
2391                                    dt_obj_version_t *new_version)
2392 {
2393         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2394
2395         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2396                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2397
2398         LDISKFS_I(inode)->i_fs_version = *new_version;
2399         /** Version is set after all inode operations are finished,
2400          *  so we should mark it dirty here */
2401         inode->i_sb->s_op->dirty_inode(inode);
2402 }
2403
2404 /*
2405  * Concurrency: @dt is write locked.
2406  */
2407 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2408                          const struct lu_buf *buf, const char *name, int fl,
2409                          struct thandle *handle, struct lustre_capa *capa)
2410 {
2411         LASSERT(handle != NULL);
2412
2413         /* version set is not real XATTR */
2414         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2415                 /* for version we are just using xattr API but change inode
2416                  * field instead */
2417                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2418                 osd_object_version_set(env, dt, buf->lb_buf);
2419                 return sizeof(dt_obj_version_t);
2420         }
2421
2422         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2423                 return -EACCES;
2424
2425         OSD_EXEC_OP(handle, xattr_set);
2426         return __osd_xattr_set(env, dt, buf, name, fl);
2427 }
2428
2429 /*
2430  * Concurrency: @dt is read locked.
2431  */
2432 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2433                           struct lu_buf *buf, struct lustre_capa *capa)
2434 {
2435         struct osd_object      *obj    = osd_dt_obj(dt);
2436         struct inode           *inode  = obj->oo_inode;
2437         struct osd_thread_info *info   = osd_oti_get(env);
2438         struct dentry          *dentry = &info->oti_obj_dentry;
2439
2440         LASSERT(dt_object_exists(dt));
2441         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2442         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2443
2444         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2445                 return -EACCES;
2446
2447         dentry->d_inode = inode;
2448         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2449 }
2450
2451 static int osd_declare_xattr_del(const struct lu_env *env,
2452                                  struct dt_object *dt, const char *name,
2453                                  struct thandle *handle)
2454 {
2455         struct osd_thandle *oh;
2456
2457         LASSERT(dt_object_exists(dt));
2458         LASSERT(handle != NULL);
2459
2460         oh = container_of0(handle, struct osd_thandle, ot_super);
2461         LASSERT(oh->ot_handle == NULL);
2462
2463         OSD_DECLARE_OP(oh, xattr_set);
2464         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2465
2466         return 0;
2467 }
2468
2469 /*
2470  * Concurrency: @dt is write locked.
2471  */
2472 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2473                          const char *name, struct thandle *handle,
2474                          struct lustre_capa *capa)
2475 {
2476         struct osd_object      *obj    = osd_dt_obj(dt);
2477         struct inode           *inode  = obj->oo_inode;
2478         struct osd_thread_info *info   = osd_oti_get(env);
2479         struct dentry          *dentry = &info->oti_obj_dentry;
2480         int                     rc;
2481
2482         LASSERT(dt_object_exists(dt));
2483         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2484         LASSERT(osd_write_locked(env, obj));
2485         LASSERT(handle != NULL);
2486
2487         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2488                 return -EACCES;
2489
2490         OSD_EXEC_OP(handle, xattr_set);
2491
2492         dentry->d_inode = inode;
2493         rc = inode->i_op->removexattr(dentry, name);
2494         return rc;
2495 }
2496
2497 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2498                                      struct dt_object *dt,
2499                                      struct lustre_capa *old,
2500                                      __u64 opc)
2501 {
2502         struct osd_thread_info *info = osd_oti_get(env);
2503         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2504         struct osd_object *obj = osd_dt_obj(dt);
2505         struct osd_device *dev = osd_obj2dev(obj);
2506         struct lustre_capa_key *key = &info->oti_capa_key;
2507         struct lustre_capa *capa = &info->oti_capa;
2508         struct obd_capa *oc;
2509         struct md_capainfo *ci;
2510         int rc;
2511         ENTRY;
2512
2513         if (!dev->od_fl_capa)
2514                 RETURN(ERR_PTR(-ENOENT));
2515
2516         LASSERT(dt_object_exists(dt));
2517         LINVRNT(osd_invariant(obj));
2518
2519         /* renewal sanity check */
2520         if (old && osd_object_auth(env, dt, old, opc))
2521                 RETURN(ERR_PTR(-EACCES));
2522
2523         ci = md_capainfo(env);
2524         if (unlikely(!ci))
2525                 RETURN(ERR_PTR(-ENOENT));
2526
2527         switch (ci->mc_auth) {
2528         case LC_ID_NONE:
2529                 RETURN(NULL);
2530         case LC_ID_PLAIN:
2531                 capa->lc_uid = obj->oo_inode->i_uid;
2532                 capa->lc_gid = obj->oo_inode->i_gid;
2533                 capa->lc_flags = LC_ID_PLAIN;
2534                 break;
2535         case LC_ID_CONVERT: {
2536                 __u32 d[4], s[4];
2537
2538                 s[0] = obj->oo_inode->i_uid;
2539                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2540                 s[2] = obj->oo_inode->i_gid;
2541                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2542                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2543                 if (unlikely(rc))
2544                         RETURN(ERR_PTR(rc));
2545
2546                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2547                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2548                 capa->lc_flags = LC_ID_CONVERT;
2549                 break;
2550         }
2551         default:
2552                 RETURN(ERR_PTR(-EINVAL));
2553         }
2554
2555         capa->lc_fid = *fid;
2556         capa->lc_opc = opc;
2557         capa->lc_flags |= dev->od_capa_alg << 24;
2558         capa->lc_timeout = dev->od_capa_timeout;
2559         capa->lc_expiry = 0;
2560
2561         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2562         if (oc) {
2563                 LASSERT(!capa_is_expired(oc));
2564                 RETURN(oc);
2565         }
2566
2567         cfs_spin_lock(&capa_lock);
2568         *key = dev->od_capa_keys[1];
2569         cfs_spin_unlock(&capa_lock);
2570
2571         capa->lc_keyid = key->lk_keyid;
2572         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2573
2574         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2575         if (rc) {
2576                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2577                 RETURN(ERR_PTR(rc));
2578         }
2579
2580         oc = capa_add(dev->od_capa_hash, capa);
2581         RETURN(oc);
2582 }
2583
2584 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2585 {
2586         struct osd_object       *obj    = osd_dt_obj(dt);
2587         struct inode            *inode  = obj->oo_inode;
2588         struct osd_thread_info  *info   = osd_oti_get(env);
2589         struct dentry           *dentry = &info->oti_obj_dentry;
2590         struct file             *file   = &info->oti_file;
2591         int                     rc;
2592
2593         ENTRY;
2594
2595         dentry->d_inode = inode;
2596         file->f_dentry = dentry;
2597         file->f_mapping = inode->i_mapping;
2598         file->f_op = inode->i_fop;
2599         mutex_lock(&inode->i_mutex);
2600         rc = file->f_op->fsync(file, dentry, 0);
2601         mutex_unlock(&inode->i_mutex);
2602         RETURN(rc);
2603 }
2604
2605 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2606                         void **data)
2607 {
2608         struct osd_object *obj = osd_dt_obj(dt);
2609         ENTRY;
2610
2611         *data = (void *)obj->oo_inode;
2612         RETURN(0);
2613 }
2614
2615 /*
2616  * Index operations.
2617  */
2618
2619 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2620                            const struct dt_index_features *feat)
2621 {
2622         struct iam_descr *descr;
2623
2624         if (osd_object_is_root(o))
2625                 return feat == &dt_directory_features;
2626
2627         LASSERT(o->oo_dir != NULL);
2628
2629         descr = o->oo_dir->od_container.ic_descr;
2630         if (feat == &dt_directory_features) {
2631                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2632                         return 1;
2633                 else
2634                         return 0;
2635         } else {
2636                 return
2637                         feat->dif_keysize_min <= descr->id_key_size &&
2638                         descr->id_key_size <= feat->dif_keysize_max &&
2639                         feat->dif_recsize_min <= descr->id_rec_size &&
2640                         descr->id_rec_size <= feat->dif_recsize_max &&
2641                         !(feat->dif_flags & (DT_IND_VARKEY |
2642                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2643                         ergo(feat->dif_flags & DT_IND_UPDATE,
2644                              1 /* XXX check that object (and file system) is
2645                                 * writable */);
2646         }
2647 }
2648
2649 static int osd_iam_container_init(const struct lu_env *env,
2650                                   struct osd_object *obj,
2651                                   struct osd_directory *dir)
2652 {
2653         struct iam_container *bag = &dir->od_container;
2654         int result;
2655
2656         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2657         if (result != 0)
2658                 return result;
2659
2660         result = iam_container_setup(bag);
2661         if (result == 0)
2662                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2663         else
2664                 iam_container_fini(bag);
2665
2666         return result;
2667 }
2668
2669
2670 /*
2671  * Concurrency: no external locking is necessary.
2672  */
2673 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2674                          const struct dt_index_features *feat)
2675 {
2676         int                      result;
2677         int                      skip_iam = 0;
2678         struct osd_object       *obj = osd_dt_obj(dt);
2679         struct osd_device       *osd = osd_obj2dev(obj);
2680
2681         LINVRNT(osd_invariant(obj));
2682         LASSERT(dt_object_exists(dt));
2683
2684         if (osd_object_is_root(obj)) {
2685                 dt->do_index_ops = &osd_index_ea_ops;
2686                 result = 0;
2687         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2688                 dt->do_index_ops = &osd_index_ea_ops;
2689                 if (S_ISDIR(obj->oo_inode->i_mode))
2690                         result = 0;
2691                 else
2692                         result = -ENOTDIR;
2693                 skip_iam = 1;
2694         } else if (unlikely(feat == &dt_otable_features)) {
2695                 dt->do_index_ops = &osd_otable_ops;
2696                 return 0;
2697         } else if (feat == &dt_acct_features) {
2698                 dt->do_index_ops = &osd_acct_index_ops;
2699                 result = 0;
2700                 skip_iam = 1;
2701         } else if (!osd_has_index(obj)) {
2702                 struct osd_directory *dir;
2703
2704                 OBD_ALLOC_PTR(dir);
2705                 if (dir != NULL) {
2706
2707                         cfs_spin_lock(&obj->oo_guard);
2708                         if (obj->oo_dir == NULL)
2709                                 obj->oo_dir = dir;
2710                         else
2711                                 /*
2712                                  * Concurrent thread allocated container data.
2713                                  */
2714                                 OBD_FREE_PTR(dir);
2715                         cfs_spin_unlock(&obj->oo_guard);
2716                         /*
2717                          * Now, that we have container data, serialize its
2718                          * initialization.
2719                          */
2720                         cfs_down_write(&obj->oo_ext_idx_sem);
2721                         /*
2722                          * recheck under lock.
2723                          */
2724                         if (!osd_has_index(obj))
2725                                 result = osd_iam_container_init(env, obj, dir);
2726                         else
2727                                 result = 0;
2728                         cfs_up_write(&obj->oo_ext_idx_sem);
2729                 } else {
2730                         result = -ENOMEM;
2731                 }
2732         } else {
2733                 result = 0;
2734         }
2735
2736         if (result == 0 && skip_iam == 0) {
2737                 if (!osd_iam_index_probe(env, obj, feat))
2738                         result = -ENOTDIR;
2739         }
2740         LINVRNT(osd_invariant(obj));
2741
2742         if (is_quota_glb_feat(feat))
2743                 result = osd_quota_migration(env, dt, feat);
2744
2745         return result;
2746 }
2747
2748 static int osd_otable_it_attr_get(const struct lu_env *env,
2749                                  struct dt_object *dt,
2750                                  struct lu_attr *attr,
2751                                  struct lustre_capa *capa)
2752 {
2753         attr->la_valid = 0;
2754         return 0;
2755 }
2756
2757 static const struct dt_object_operations osd_obj_ops = {
2758         .do_read_lock         = osd_object_read_lock,
2759         .do_write_lock        = osd_object_write_lock,
2760         .do_read_unlock       = osd_object_read_unlock,
2761         .do_write_unlock      = osd_object_write_unlock,
2762         .do_write_locked      = osd_object_write_locked,
2763         .do_attr_get          = osd_attr_get,
2764         .do_declare_attr_set  = osd_declare_attr_set,
2765         .do_attr_set          = osd_attr_set,
2766         .do_ah_init           = osd_ah_init,
2767         .do_declare_create    = osd_declare_object_create,
2768         .do_create            = osd_object_create,
2769         .do_declare_destroy   = osd_declare_object_destroy,
2770         .do_destroy           = osd_object_destroy,
2771         .do_index_try         = osd_index_try,
2772         .do_declare_ref_add   = osd_declare_object_ref_add,
2773         .do_ref_add           = osd_object_ref_add,
2774         .do_declare_ref_del   = osd_declare_object_ref_del,
2775         .do_ref_del           = osd_object_ref_del,
2776         .do_xattr_get         = osd_xattr_get,
2777         .do_declare_xattr_set = osd_declare_xattr_set,
2778         .do_xattr_set         = osd_xattr_set,
2779         .do_declare_xattr_del = osd_declare_xattr_del,
2780         .do_xattr_del         = osd_xattr_del,
2781         .do_xattr_list        = osd_xattr_list,
2782         .do_capa_get          = osd_capa_get,
2783         .do_object_sync       = osd_object_sync,
2784         .do_data_get          = osd_data_get,
2785 };
2786
2787 /**
2788  * dt_object_operations for interoperability mode
2789  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2790  */
2791 static const struct dt_object_operations osd_obj_ea_ops = {
2792         .do_read_lock         = osd_object_read_lock,
2793         .do_write_lock        = osd_object_write_lock,
2794         .do_read_unlock       = osd_object_read_unlock,
2795         .do_write_unlock      = osd_object_write_unlock,
2796         .do_write_locked      = osd_object_write_locked,
2797         .do_attr_get          = osd_attr_get,
2798         .do_declare_attr_set  = osd_declare_attr_set,
2799         .do_attr_set          = osd_attr_set,
2800         .do_ah_init           = osd_ah_init,
2801         .do_declare_create    = osd_declare_object_create,
2802         .do_create            = osd_object_ea_create,
2803         .do_declare_destroy   = osd_declare_object_destroy,
2804         .do_destroy           = osd_object_destroy,
2805         .do_index_try         = osd_index_try,
2806         .do_declare_ref_add   = osd_declare_object_ref_add,
2807         .do_ref_add           = osd_object_ref_add,
2808         .do_declare_ref_del   = osd_declare_object_ref_del,
2809         .do_ref_del           = osd_object_ref_del,
2810         .do_xattr_get         = osd_xattr_get,
2811         .do_declare_xattr_set = osd_declare_xattr_set,
2812         .do_xattr_set         = osd_xattr_set,
2813         .do_declare_xattr_del = osd_declare_xattr_del,
2814         .do_xattr_del         = osd_xattr_del,
2815         .do_xattr_list        = osd_xattr_list,
2816         .do_capa_get          = osd_capa_get,
2817         .do_object_sync       = osd_object_sync,
2818         .do_data_get          = osd_data_get,
2819 };
2820
2821 static const struct dt_object_operations osd_obj_otable_it_ops = {
2822         .do_attr_get    = osd_otable_it_attr_get,
2823         .do_index_try   = osd_index_try,
2824 };
2825
2826 static int osd_index_declare_iam_delete(const struct lu_env *env,
2827                                         struct dt_object *dt,
2828                                         const struct dt_key *key,
2829                                         struct thandle *handle)
2830 {
2831         struct osd_thandle    *oh;
2832
2833         oh = container_of0(handle, struct osd_thandle, ot_super);
2834         LASSERT(oh->ot_handle == NULL);
2835
2836         OSD_DECLARE_OP(oh, delete);
2837         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2838
2839         return 0;
2840 }
2841
2842 /**
2843  *      delete a (key, value) pair from index \a dt specified by \a key
2844  *
2845  *      \param  dt      osd index object
2846  *      \param  key     key for index
2847  *      \param  rec     record reference
2848  *      \param  handle  transaction handler
2849  *
2850  *      \retval  0  success
2851  *      \retval -ve   failure
2852  */
2853
2854 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2855                                 const struct dt_key *key,
2856                                 struct thandle *handle,
2857                                 struct lustre_capa *capa)
2858 {
2859         struct osd_thread_info *oti = osd_oti_get(env);
2860         struct osd_object      *obj = osd_dt_obj(dt);
2861         struct osd_thandle     *oh;
2862         struct iam_path_descr  *ipd;
2863         struct iam_container   *bag = &obj->oo_dir->od_container;
2864         int                     rc;
2865
2866         ENTRY;
2867
2868         LINVRNT(osd_invariant(obj));
2869         LASSERT(dt_object_exists(dt));
2870         LASSERT(bag->ic_object == obj->oo_inode);
2871         LASSERT(handle != NULL);
2872
2873         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2874                 RETURN(-EACCES);
2875
2876         OSD_EXEC_OP(handle, delete);
2877
2878         ipd = osd_idx_ipd_get(env, bag);
2879         if (unlikely(ipd == NULL))
2880                 RETURN(-ENOMEM);
2881
2882         oh = container_of0(handle, struct osd_thandle, ot_super);
2883         LASSERT(oh->ot_handle != NULL);
2884         LASSERT(oh->ot_handle->h_transaction != NULL);
2885
2886         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2887                 /* swab quota uid/gid provided by caller */
2888                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2889                 key = (const struct dt_key *)&oti->oti_quota_id;
2890         }
2891
2892         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2893         osd_ipd_put(env, bag, ipd);
2894         LINVRNT(osd_invariant(obj));
2895         RETURN(rc);
2896 }
2897
2898 static int osd_index_declare_ea_delete(const struct lu_env *env,
2899                                        struct dt_object *dt,
2900                                        const struct dt_key *key,
2901                                        struct thandle *handle)
2902 {
2903         struct osd_thandle *oh;
2904         struct inode       *inode;
2905         int                 rc;
2906         ENTRY;
2907
2908         LASSERT(dt_object_exists(dt));
2909         LASSERT(handle != NULL);
2910
2911         oh = container_of0(handle, struct osd_thandle, ot_super);
2912         LASSERT(oh->ot_handle == NULL);
2913
2914         OSD_DECLARE_OP(oh, delete);
2915         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2916
2917         inode = osd_dt_obj(dt)->oo_inode;
2918         LASSERT(inode);
2919
2920         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2921                                    true, true, NULL, false);
2922         RETURN(rc);
2923 }
2924
2925 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2926                                           struct dt_rec *fid)
2927 {
2928         struct osd_fid_pack *rec;
2929         int                  rc = -ENODATA;
2930
2931         if (de->file_type & LDISKFS_DIRENT_LUFID) {
2932                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2933                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2934         }
2935         RETURN(rc);
2936 }
2937
2938 /**
2939  * Index delete function for interoperability mode (b11826).
2940  * It will remove the directory entry added by osd_index_ea_insert().
2941  * This entry is needed to maintain name->fid mapping.
2942  *
2943  * \param key,  key i.e. file entry to be deleted
2944  *
2945  * \retval   0, on success
2946  * \retval -ve, on error
2947  */
2948 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2949                                const struct dt_key *key,
2950                                struct thandle *handle,
2951                                struct lustre_capa *capa)
2952 {
2953         struct osd_object          *obj    = osd_dt_obj(dt);
2954         struct inode               *dir    = obj->oo_inode;
2955         struct dentry              *dentry;
2956         struct osd_thandle         *oh;
2957         struct ldiskfs_dir_entry_2 *de;
2958         struct buffer_head         *bh;
2959         struct htree_lock          *hlock = NULL;
2960         int                         rc;
2961
2962         ENTRY;
2963
2964         LINVRNT(osd_invariant(obj));
2965         LASSERT(dt_object_exists(dt));
2966         LASSERT(handle != NULL);
2967
2968         OSD_EXEC_OP(handle, delete);
2969
2970         oh = container_of(handle, struct osd_thandle, ot_super);
2971         LASSERT(oh->ot_handle != NULL);
2972         LASSERT(oh->ot_handle->h_transaction != NULL);
2973
2974         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2975                 RETURN(-EACCES);
2976
2977         dentry = osd_child_dentry_get(env, obj,
2978                                       (char *)key, strlen((char *)key));
2979
2980         if (obj->oo_hl_head != NULL) {
2981                 hlock = osd_oti_get(env)->oti_hlock;
2982                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2983                                    dir, LDISKFS_HLOCK_DEL);
2984         } else {
2985                 cfs_down_write(&obj->oo_ext_idx_sem);
2986         }
2987
2988         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2989         if (bh) {
2990                 rc = ldiskfs_delete_entry(oh->ot_handle,
2991                                           dir, de, bh);
2992                 brelse(bh);
2993         } else {
2994                 rc = -ENOENT;
2995         }
2996         if (hlock != NULL)
2997                 ldiskfs_htree_unlock(hlock);
2998         else
2999                 cfs_up_write(&obj->oo_ext_idx_sem);
3000
3001         LASSERT(osd_invariant(obj));
3002         RETURN(rc);
3003 }
3004
3005 /**
3006  *      Lookup index for \a key and copy record to \a rec.
3007  *
3008  *      \param  dt      osd index object
3009  *      \param  key     key for index
3010  *      \param  rec     record reference
3011  *
3012  *      \retval  +ve  success : exact mach
3013  *      \retval  0    return record with key not greater than \a key
3014  *      \retval -ve   failure
3015  */
3016 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3017                                 struct dt_rec *rec, const struct dt_key *key,
3018                                 struct lustre_capa *capa)
3019 {
3020         struct osd_object      *obj = osd_dt_obj(dt);
3021         struct iam_path_descr  *ipd;
3022         struct iam_container   *bag = &obj->oo_dir->od_container;
3023         struct osd_thread_info *oti = osd_oti_get(env);
3024         struct iam_iterator    *it = &oti->oti_idx_it;
3025         struct iam_rec         *iam_rec;
3026         int                     rc;
3027
3028         ENTRY;
3029
3030         LASSERT(osd_invariant(obj));
3031         LASSERT(dt_object_exists(dt));
3032         LASSERT(bag->ic_object == obj->oo_inode);
3033
3034         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3035                 RETURN(-EACCES);
3036
3037         ipd = osd_idx_ipd_get(env, bag);
3038         if (IS_ERR(ipd))
3039                 RETURN(-ENOMEM);
3040
3041         /* got ipd now we can start iterator. */
3042         iam_it_init(it, bag, 0, ipd);
3043
3044         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3045                 /* swab quota uid/gid provided by caller */
3046                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3047                 key = (const struct dt_key *)&oti->oti_quota_id;
3048         }
3049
3050         rc = iam_it_get(it, (struct iam_key *)key);
3051         if (rc >= 0) {
3052                 if (S_ISDIR(obj->oo_inode->i_mode))
3053                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3054                 else
3055                         iam_rec = (struct iam_rec *) rec;
3056
3057                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3058
3059                 if (S_ISDIR(obj->oo_inode->i_mode))
3060                         osd_fid_unpack((struct lu_fid *) rec,
3061                                        (struct osd_fid_pack *)iam_rec);
3062                 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3063                         osd_quota_unpack(obj, rec);
3064         }
3065
3066         iam_it_put(it);
3067         iam_it_fini(it);
3068         osd_ipd_put(env, bag, ipd);
3069
3070         LINVRNT(osd_invariant(obj));
3071
3072         RETURN(rc);
3073 }
3074
3075 static int osd_index_declare_iam_insert(const struct lu_env *env,
3076                                         struct dt_object *dt,
3077                                         const struct dt_rec *rec,
3078                                         const struct dt_key *key,
3079                                         struct thandle *handle)
3080 {
3081         struct osd_thandle *oh;
3082
3083         LASSERT(dt_object_exists(dt));
3084         LASSERT(handle != NULL);
3085
3086         oh = container_of0(handle, struct osd_thandle, ot_super);
3087         LASSERT(oh->ot_handle == NULL);
3088
3089         OSD_DECLARE_OP(oh, insert);
3090         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3091
3092         return 0;
3093 }
3094
3095 /**
3096  *      Inserts (key, value) pair in \a dt index object.
3097  *
3098  *      \param  dt      osd index object
3099  *      \param  key     key for index
3100  *      \param  rec     record reference
3101  *      \param  th      transaction handler
3102  *
3103  *      \retval  0  success
3104  *      \retval -ve failure
3105  */
3106 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3107                                 const struct dt_rec *rec,
3108                                 const struct dt_key *key, struct thandle *th,
3109                                 struct lustre_capa *capa, int ignore_quota)
3110 {
3111         struct osd_object     *obj = osd_dt_obj(dt);
3112         struct iam_path_descr *ipd;
3113         struct osd_thandle    *oh;
3114         struct iam_container  *bag = &obj->oo_dir->od_container;
3115         struct osd_thread_info *oti = osd_oti_get(env);
3116         struct iam_rec         *iam_rec;
3117         int                     rc;
3118
3119         ENTRY;
3120
3121         LINVRNT(osd_invariant(obj));
3122         LASSERT(dt_object_exists(dt));
3123         LASSERT(bag->ic_object == obj->oo_inode);
3124         LASSERT(th != NULL);
3125
3126         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3127                 RETURN(-EACCES);
3128
3129         OSD_EXEC_OP(th, insert);
3130
3131         ipd = osd_idx_ipd_get(env, bag);
3132         if (unlikely(ipd == NULL))
3133                 RETURN(-ENOMEM);
3134
3135         oh = container_of0(th, struct osd_thandle, ot_super);
3136         LASSERT(oh->ot_handle != NULL);
3137         LASSERT(oh->ot_handle->h_transaction != NULL);
3138         if (S_ISDIR(obj->oo_inode->i_mode)) {
3139                 iam_rec = (struct iam_rec *)oti->oti_ldp;
3140                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3141         } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3142                 /* pack quota uid/gid */
3143                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3144                 key = (const struct dt_key *)&oti->oti_quota_id;
3145                 /* pack quota record */
3146                 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3147                 iam_rec = (struct iam_rec *)rec;
3148         } else {
3149                 iam_rec = (struct iam_rec *)rec;
3150         }
3151
3152         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3153                         iam_rec, ipd);
3154         osd_ipd_put(env, bag, ipd);
3155         LINVRNT(osd_invariant(obj));
3156         RETURN(rc);
3157 }
3158
3159 /**
3160  * Calls ldiskfs_add_entry() to add directory entry
3161  * into the directory. This is required for
3162  * interoperability mode (b11826)
3163  *
3164  * \retval   0, on success
3165  * \retval -ve, on error
3166  */
3167 static int __osd_ea_add_rec(struct osd_thread_info *info,
3168                             struct osd_object *pobj, struct inode  *cinode,
3169                             const char *name, const struct dt_rec *fid,
3170                             struct htree_lock *hlock, struct thandle *th)
3171 {
3172         struct ldiskfs_dentry_param *ldp;
3173         struct dentry               *child;
3174         struct osd_thandle          *oth;
3175         int                          rc;
3176
3177         oth = container_of(th, struct osd_thandle, ot_super);
3178         LASSERT(oth->ot_handle != NULL);
3179         LASSERT(oth->ot_handle->h_transaction != NULL);
3180
3181         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3182
3183         /* XXX: remove fid_is_igif() check here.
3184          * IGIF check is just to handle insertion of .. when it is 'ROOT',
3185          * it is IGIF now but needs FID in dir entry as well for readdir
3186          * to work.
3187          * LU-838 should fix that and remove fid_is_igif() check */
3188         if (fid_is_igif((struct lu_fid *)fid) ||
3189             fid_is_norm((struct lu_fid *)fid)) {
3190                 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3191                 osd_get_ldiskfs_dirent_param(ldp, fid);
3192                 child->d_fsdata = (void *)ldp;
3193         } else {
3194                 child->d_fsdata = NULL;
3195         }
3196         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3197
3198         RETURN(rc);
3199 }
3200
3201 /**
3202  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3203  * into the directory.Also sets flags into osd object to
3204  * indicate dot and dotdot are created. This is required for
3205  * interoperability mode (b11826)
3206  *
3207  * \param dir   directory for dot and dotdot fixup.
3208  * \param obj   child object for linking
3209  *
3210  * \retval   0, on success
3211  * \retval -ve, on error
3212  */
3213 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3214                               struct osd_object *dir,
3215                               struct inode  *parent_dir, const char *name,
3216                               const struct dt_rec *dot_fid,
3217                               const struct dt_rec *dot_dot_fid,
3218                               struct thandle *th)
3219 {
3220         struct inode                *inode = dir->oo_inode;
3221         struct ldiskfs_dentry_param *dot_ldp;
3222         struct ldiskfs_dentry_param *dot_dot_ldp;
3223         struct osd_thandle          *oth;
3224         int result = 0;
3225
3226         oth = container_of(th, struct osd_thandle, ot_super);
3227         LASSERT(oth->ot_handle->h_transaction != NULL);
3228         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3229
3230         if (strcmp(name, dot) == 0) {
3231                 if (dir->oo_compat_dot_created) {
3232                         result = -EEXIST;
3233                 } else {
3234                         LASSERT(inode == parent_dir);
3235                         dir->oo_compat_dot_created = 1;
3236                         result = 0;
3237                 }
3238         } else if(strcmp(name, dotdot) == 0) {
3239                 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3240                 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3241
3242                 if (!dir->oo_compat_dot_created)
3243                         return -EINVAL;
3244                 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3245                         osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3246                         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3247                 } else {
3248                         dot_ldp = NULL;
3249                         dot_dot_ldp = NULL;
3250                 }
3251                 /* in case of rename, dotdot is already created */
3252                 if (dir->oo_compat_dotdot_created) {
3253                         return __osd_ea_add_rec(info, dir, parent_dir, name,
3254                                                 dot_dot_fid, NULL, th);
3255                 }
3256
3257                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3258                                                 inode, dot_ldp, dot_dot_ldp);
3259                 if (result == 0)
3260                        dir->oo_compat_dotdot_created = 1;
3261         }
3262
3263         return result;
3264 }
3265
3266
3267 /**
3268  * It will call the appropriate osd_add* function and return the
3269  * value, return by respective functions.
3270  */
3271 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3272                           struct inode *cinode, const char *name,
3273                           const struct dt_rec *fid, struct thandle *th)
3274 {
3275         struct osd_thread_info *info   = osd_oti_get(env);
3276         struct htree_lock      *hlock;
3277         int                     rc;
3278
3279         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3280
3281         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3282                                                    name[2] =='\0'))) {
3283                 if (hlock != NULL) {
3284                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3285                                            pobj->oo_inode, 0);
3286                 } else {
3287                         cfs_down_write(&pobj->oo_ext_idx_sem);
3288                 }
3289                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3290                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3291                                         fid, th);
3292         } else {
3293                 if (hlock != NULL) {
3294                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3295                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
3296                 } else {
3297                         cfs_down_write(&pobj->oo_ext_idx_sem);
3298                 }
3299
3300                 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3301                                       hlock, th);
3302         }
3303         if (hlock != NULL)
3304                 ldiskfs_htree_unlock(hlock);
3305         else
3306                 cfs_up_write(&pobj->oo_ext_idx_sem);
3307
3308         return rc;
3309 }
3310
3311 static int
3312 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3313                       struct osd_idmap_cache *oic)
3314 {
3315         struct osd_scrub    *scrub = &dev->od_scrub;
3316         struct lu_fid       *fid   = &oic->oic_fid;
3317         struct osd_inode_id *id    = &oti->oti_id;
3318         int                  once  = 0;
3319         int                  rc;
3320         ENTRY;
3321
3322 again:
3323         rc = osd_oi_lookup(oti, dev, fid, id);
3324         if (rc != 0 && rc != -ENOENT)
3325                 RETURN(rc);
3326
3327         if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3328                 RETURN(0);
3329
3330         if (thread_is_running(&scrub->os_thread)) {
3331                 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3332                 /* There is race condition between osd_oi_lookup and OI scrub.
3333                  * The OI scrub finished just after osd_oi_lookup() failure.
3334                  * Under such case, it is unnecessary to trigger OI scrub again,
3335                  * but try to call osd_oi_lookup() again. */
3336                 if (unlikely(rc == -EAGAIN))
3337                         goto again;
3338
3339                 RETURN(rc);
3340         }
3341
3342         if (!scrub->os_no_scrub && ++once == 1) {
3343                 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3344                        PFID(fid));
3345                 rc = osd_scrub_start(dev);
3346                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3347                                ", rc = %d [2]\n",
3348                                LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3349                                PFID(fid), rc);
3350                 if (rc == 0)
3351                         goto again;
3352         }
3353
3354         RETURN(rc = -EREMCHG);
3355 }
3356
3357 /**
3358  * Calls ->lookup() to find dentry. From dentry get inode and
3359  * read inode's ea to get fid. This is required for  interoperability
3360  * mode (b11826)
3361  *
3362  * \retval   0, on success
3363  * \retval -ve, on error
3364  */
3365 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3366                              struct dt_rec *rec, const struct dt_key *key)
3367 {
3368         struct inode               *dir    = obj->oo_inode;
3369         struct dentry              *dentry;
3370         struct ldiskfs_dir_entry_2 *de;
3371         struct buffer_head         *bh;
3372         struct lu_fid              *fid = (struct lu_fid *) rec;
3373         struct htree_lock          *hlock = NULL;
3374         int                         ino;
3375         int                         rc;
3376
3377         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3378
3379         dentry = osd_child_dentry_get(env, obj,
3380                                       (char *)key, strlen((char *)key));
3381
3382         if (obj->oo_hl_head != NULL) {
3383                 hlock = osd_oti_get(env)->oti_hlock;
3384                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3385                                    dir, LDISKFS_HLOCK_LOOKUP);
3386         } else {
3387                 cfs_down_read(&obj->oo_ext_idx_sem);
3388         }
3389
3390         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3391         if (bh) {
3392                 struct osd_thread_info *oti = osd_oti_get(env);
3393                 struct osd_idmap_cache *oic = &oti->oti_cache;
3394                 struct osd_device *dev = osd_obj2dev(obj);
3395                 struct osd_scrub *scrub = &dev->od_scrub;
3396                 struct scrub_file *sf = &scrub->os_file;
3397
3398                 ino = le32_to_cpu(de->inode);
3399                 rc = osd_get_fid_from_dentry(de, rec);
3400
3401                 /* done with de, release bh */
3402                 brelse(bh);
3403                 if (rc != 0)
3404                         rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3405                 else
3406                         osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3407                 if (rc != 0 || !fid_is_norm(fid)) {
3408                         fid_zero(&oic->oic_fid);
3409                         GOTO(out, rc);
3410                 }
3411
3412                 oic->oic_fid = *fid;
3413                 if ((scrub->os_pos_current <= ino) &&
3414                     (sf->sf_flags & SF_INCONSISTENT ||
3415                      ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3416                                       sf->sf_oi_bitmap)))
3417                         rc = osd_consistency_check(oti, dev, oic);
3418         } else {
3419                 rc = -ENOENT;
3420         }
3421
3422         GOTO(out, rc);
3423
3424 out:
3425         if (hlock != NULL)
3426                 ldiskfs_htree_unlock(hlock);
3427         else
3428                 cfs_up_read(&obj->oo_ext_idx_sem);
3429         return rc;
3430 }
3431
3432 /**
3433  * Find the osd object for given fid.
3434  *
3435  * \param fid need to find the osd object having this fid
3436  *
3437  * \retval osd_object on success
3438  * \retval        -ve on error
3439  */
3440 struct osd_object *osd_object_find(const struct lu_env *env,
3441                                    struct dt_object *dt,
3442                                    const struct lu_fid *fid)
3443 {
3444         struct lu_device  *ludev = dt->do_lu.lo_dev;
3445         struct osd_object *child = NULL;
3446         struct lu_object  *luch;
3447         struct lu_object  *lo;
3448
3449         /*
3450          * at this point topdev might not exist yet
3451          * (i.e. MGS is preparing profiles). so we can
3452          * not rely on topdev and instead lookup with
3453          * our device passed as topdev. this can't work
3454          * if the object isn't cached yet (as osd doesn't
3455          * allocate lu_header). IOW, the object must be
3456          * in the cache, otherwise lu_object_alloc() crashes
3457          * -bzzz
3458          */
3459         luch = lu_object_find_at(env, ludev, fid, NULL);
3460         if (!IS_ERR(luch)) {
3461                 if (lu_object_exists(luch)) {
3462                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3463                         if (lo != NULL)
3464                                 child = osd_obj(lo);
3465                         else
3466                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3467                                                 "lu_object can't be located"
3468                                                 DFID"\n", PFID(fid));
3469
3470                         if (child == NULL) {
3471                                 lu_object_put(env, luch);
3472                                 CERROR("Unable to get osd_object\n");
3473                                 child = ERR_PTR(-ENOENT);
3474                         }
3475                 } else {
3476                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
3477                                         "lu_object does not exists "DFID"\n",
3478                                         PFID(fid));
3479                         lu_object_put(env, luch);
3480                         child = ERR_PTR(-ENOENT);
3481                 }
3482         } else
3483                 child = (void *)luch;
3484
3485         return child;
3486 }
3487
3488 /**
3489  * Put the osd object once done with it.
3490  *
3491  * \param obj osd object that needs to be put
3492  */
3493 static inline void osd_object_put(const struct lu_env *env,
3494                                   struct osd_object *obj)
3495 {
3496         lu_object_put(env, &obj->oo_dt.do_lu);
3497 }
3498
3499 static int osd_index_declare_ea_insert(const struct lu_env *env,
3500                                        struct dt_object *dt,
3501                                        const struct dt_rec *rec,
3502                                        const struct dt_key *key,
3503                                        struct thandle *handle)
3504 {
3505         struct osd_thandle *oh;
3506         struct inode       *inode;
3507         int                 rc;
3508         ENTRY;
3509
3510         LASSERT(dt_object_exists(dt));
3511         LASSERT(handle != NULL);
3512
3513         oh = container_of0(handle, struct osd_thandle, ot_super);
3514         LASSERT(oh->ot_handle == NULL);
3515
3516         OSD_DECLARE_OP(oh, insert);
3517         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3518
3519         inode = osd_dt_obj(dt)->oo_inode;
3520         LASSERT(inode);
3521
3522         /* We ignore block quota on meta pool (MDTs), so needn't
3523          * calculate how many blocks will be consumed by this index
3524          * insert */
3525         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3526                                    true, true, NULL, false);
3527         RETURN(rc);
3528 }
3529
3530 /**
3531  * Index add function for interoperability mode (b11826).
3532  * It will add the directory entry.This entry is needed to
3533  * maintain name->fid mapping.
3534  *
3535  * \param key it is key i.e. file entry to be inserted
3536  * \param rec it is value of given key i.e. fid
3537  *
3538  * \retval   0, on success
3539  * \retval -ve, on error
3540  */
3541 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3542                                const struct dt_rec *rec,
3543                                const struct dt_key *key, struct thandle *th,
3544                                struct lustre_capa *capa, int ignore_quota)
3545 {
3546         struct osd_object *obj   = osd_dt_obj(dt);
3547         struct lu_fid     *fid   = (struct lu_fid *) rec;
3548         const char        *name  = (const char *)key;
3549         struct osd_object *child;
3550         int                rc;
3551
3552         ENTRY;
3553
3554         LASSERT(osd_invariant(obj));
3555         LASSERT(dt_object_exists(dt));
3556         LASSERT(th != NULL);
3557
3558         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3559                 RETURN(-EACCES);
3560
3561         child = osd_object_find(env, dt, fid);
3562         if (!IS_ERR(child)) {
3563                 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3564                 osd_object_put(env, child);
3565         } else {
3566                 rc = PTR_ERR(child);
3567         }
3568
3569         LASSERT(osd_invariant(obj));
3570         RETURN(rc);
3571 }
3572
3573 /**
3574  *  Initialize osd Iterator for given osd index object.
3575  *
3576  *  \param  dt      osd index object
3577  */
3578
3579 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3580                                      struct dt_object *dt,
3581                                      __u32 unused,
3582                                      struct lustre_capa *capa)
3583 {
3584         struct osd_it_iam      *it;
3585         struct osd_thread_info *oti = osd_oti_get(env);
3586         struct osd_object      *obj = osd_dt_obj(dt);
3587         struct lu_object       *lo  = &dt->do_lu;
3588         struct iam_path_descr  *ipd;
3589         struct iam_container   *bag = &obj->oo_dir->od_container;
3590
3591         LASSERT(lu_object_exists(lo));
3592
3593         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3594                 return ERR_PTR(-EACCES);
3595
3596         it = &oti->oti_it;
3597         ipd = osd_it_ipd_get(env, bag);
3598         if (likely(ipd != NULL)) {
3599                 it->oi_obj = obj;
3600                 it->oi_ipd = ipd;
3601                 lu_object_get(lo);
3602                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3603                 return (struct dt_it *)it;
3604         }
3605         return ERR_PTR(-ENOMEM);
3606 }
3607
3608 /**
3609  * free given Iterator.
3610  */
3611
3612 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3613 {
3614         struct osd_it_iam *it = (struct osd_it_iam *)di;
3615         struct osd_object *obj = it->oi_obj;
3616
3617         iam_it_fini(&it->oi_it);
3618         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3619         lu_object_put(env, &obj->oo_dt.do_lu);
3620 }
3621
3622 /**
3623  *  Move Iterator to record specified by \a key
3624  *
3625  *  \param  di      osd iterator
3626  *  \param  key     key for index
3627  *
3628  *  \retval +ve  di points to record with least key not larger than key
3629  *  \retval  0   di points to exact matched key
3630  *  \retval -ve  failure
3631  */
3632
3633 static int osd_it_iam_get(const struct lu_env *env,
3634                           struct dt_it *di, const struct dt_key *key)
3635 {
3636         struct osd_thread_info  *oti = osd_oti_get(env);
3637         struct osd_it_iam       *it = (struct osd_it_iam *)di;
3638
3639         if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3640                 /* swab quota uid/gid */
3641                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3642                 key = (struct dt_key *)&oti->oti_quota_id;
3643         }
3644
3645         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3646 }
3647
3648 /**
3649  *  Release Iterator
3650  *
3651  *  \param  di      osd iterator
3652  */
3653
3654 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3655 {
3656         struct osd_it_iam *it = (struct osd_it_iam *)di;
3657
3658         iam_it_put(&it->oi_it);
3659 }
3660
3661 /**
3662  *  Move iterator by one record
3663  *
3664  *  \param  di      osd iterator
3665  *
3666  *  \retval +1   end of container reached
3667  *  \retval  0   success
3668  *  \retval -ve  failure
3669  */
3670
3671 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3672 {
3673         struct osd_it_iam *it = (struct osd_it_iam *)di;
3674
3675         return iam_it_next(&it->oi_it);
3676 }
3677
3678 /**
3679  * Return pointer to the key under iterator.
3680  */
3681
3682 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3683                                  const struct dt_it *di)
3684 {
3685         struct osd_thread_info *oti = osd_oti_get(env);
3686         struct osd_it_iam      *it = (struct osd_it_iam *)di;
3687         struct osd_object      *obj = it->oi_obj;
3688         struct dt_key          *key;
3689
3690         key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3691
3692         if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3693                 /* swab quota uid/gid */
3694                 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3695                 key = (struct dt_key *)&oti->oti_quota_id;
3696         }
3697
3698         return key;
3699 }
3700
3701 /**
3702  * Return size of key under iterator (in bytes)
3703  */
3704
3705 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3706 {
3707         struct osd_it_iam *it = (struct osd_it_iam *)di;
3708
3709         return iam_it_key_size(&it->oi_it);
3710 }
3711
3712 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3713                                        int len, __u16 type)
3714 {
3715         struct luda_type *lt;
3716         const unsigned    align = sizeof(struct luda_type) - 1;
3717
3718         /* check if file type is required */
3719         if (attr & LUDA_TYPE) {
3720                         len = (len + align) & ~align;
3721
3722                         lt = (void *) ent->lde_name + len;
3723                         lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3724                         ent->lde_attrs |= LUDA_TYPE;
3725         }
3726
3727         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3728 }
3729
3730 /**
3731  * build lu direct from backend fs dirent.
3732  */
3733
3734 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3735                                       struct lu_fid *fid, __u64 offset,
3736                                       char *name, __u16 namelen,
3737                                       __u16 type, __u32 attr)
3738 {
3739         fid_cpu_to_le(&ent->lde_fid, fid);
3740         ent->lde_attrs = LUDA_FID;
3741
3742         ent->lde_hash = cpu_to_le64(offset);
3743         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3744
3745         strncpy(ent->lde_name, name, namelen);
3746         ent->lde_namelen = cpu_to_le16(namelen);
3747
3748         /* append lustre attributes */
3749         osd_it_append_attrs(ent, attr, namelen, type);
3750 }
3751
3752 /**
3753  * Return pointer to the record under iterator.
3754  */
3755 static int osd_it_iam_rec(const struct lu_env *env,
3756                           const struct dt_it *di,
3757                           struct dt_rec *dtrec, __u32 attr)
3758 {
3759         struct osd_it_iam      *it   = (struct osd_it_iam *)di;
3760         struct osd_thread_info *info = osd_oti_get(env);
3761         ENTRY;
3762
3763         if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3764                 const struct osd_fid_pack *rec;
3765                 struct lu_fid             *fid = &info->oti_fid;
3766                 struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
3767                 char                      *name;
3768                 int                        namelen;
3769                 __u64                      hash;
3770                 int                        rc;
3771
3772                 name = (char *)iam_it_key_get(&it->oi_it);
3773                 if (IS_ERR(name))
3774                         RETURN(PTR_ERR(name));
3775
3776                 namelen = iam_it_key_size(&it->oi_it);
3777
3778                 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3779                 if (IS_ERR(rec))
3780                         RETURN(PTR_ERR(rec));
3781
3782                 rc = osd_fid_unpack(fid, rec);
3783                 if (rc)
3784                         RETURN(rc);
3785
3786                 hash = iam_it_store(&it->oi_it);
3787
3788                 /* IAM does not store object type in IAM index (dir) */
3789                 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3790                                    0, LUDA_FID);
3791         } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3792                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3793                            (struct iam_rec *)dtrec);
3794                 osd_quota_unpack(it->oi_obj, dtrec);
3795         } else {
3796                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3797                            (struct iam_rec *)dtrec);
3798         }
3799
3800         RETURN(0);
3801 }
3802
3803 /**
3804  * Returns cookie for current Iterator position.
3805  */
3806 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3807 {
3808         struct osd_it_iam *it = (struct osd_it_iam *)di;
3809
3810         return iam_it_store(&it->oi_it);
3811 }
3812
3813 /**
3814  * Restore iterator from cookie.
3815  *
3816  * \param  di      osd iterator
3817  * \param  hash    Iterator location cookie
3818  *
3819  * \retval +ve  di points to record with least key not larger than key.
3820  * \retval  0   di points to exact matched key
3821  * \retval -ve  failure
3822  */
3823
3824 static int osd_it_iam_load(const struct lu_env *env,
3825                            const struct dt_it *di, __u64 hash)
3826 {
3827         struct osd_it_iam *it = (struct osd_it_iam *)di;
3828
3829         return iam_it_load(&it->oi_it, hash);
3830 }
3831
3832 static const struct dt_index_operations osd_index_iam_ops = {
3833         .dio_lookup         = osd_index_iam_lookup,
3834         .dio_declare_insert = osd_index_declare_iam_insert,
3835         .dio_insert         = osd_index_iam_insert,
3836         .dio_declare_delete = osd_index_declare_iam_delete,
3837         .dio_delete         = osd_index_iam_delete,
3838         .dio_it     = {
3839                 .init     = osd_it_iam_init,
3840                 .fini     = osd_it_iam_fini,
3841                 .get      = osd_it_iam_get,
3842                 .put      = osd_it_iam_put,
3843                 .next     = osd_it_iam_next,
3844                 .key      = osd_it_iam_key,
3845                 .key_size = osd_it_iam_key_size,
3846                 .rec      = osd_it_iam_rec,
3847                 .store    = osd_it_iam_store,
3848                 .load     = osd_it_iam_load
3849         }
3850 };
3851
3852 /**
3853  * Creates or initializes iterator context.
3854  *
3855  * \retval struct osd_it_ea, iterator structure on success
3856  *
3857  */
3858 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3859                                     struct dt_object *dt,
3860                                     __u32 attr,
3861                                     struct lustre_capa *capa)
3862 {
3863         struct osd_object       *obj  = osd_dt_obj(dt);
3864         struct osd_thread_info  *info = osd_oti_get(env);
3865         struct osd_it_ea        *it   = &info->oti_it_ea;
3866         struct lu_object        *lo   = &dt->do_lu;
3867         struct dentry           *obj_dentry = &info->oti_it_dentry;
3868         ENTRY;
3869         LASSERT(lu_object_exists(lo));
3870
3871         obj_dentry->d_inode = obj->oo_inode;
3872         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3873         obj_dentry->d_name.hash = 0;
3874
3875         it->oie_rd_dirent       = 0;
3876         it->oie_it_dirent       = 0;
3877         it->oie_dirent          = NULL;
3878         it->oie_buf             = info->oti_it_ea_buf;
3879         it->oie_obj             = obj;
3880         it->oie_file.f_pos      = 0;
3881         it->oie_file.f_dentry   = obj_dentry;
3882         if (attr & LUDA_64BITHASH)
3883                 it->oie_file.f_mode |= FMODE_64BITHASH;
3884         else
3885                 it->oie_file.f_mode |= FMODE_32BITHASH;
3886         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3887         it->oie_file.f_op         = obj->oo_inode->i_fop;
3888         it->oie_file.private_data = NULL;
3889         lu_object_get(lo);
3890         RETURN((struct dt_it *) it);
3891 }
3892
3893 /**
3894  * Destroy or finishes iterator context.
3895  *
3896  * \param di iterator structure to be destroyed
3897  */
3898 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3899 {
3900         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3901         struct osd_object    *obj  = it->oie_obj;
3902         struct inode       *inode  = obj->oo_inode;
3903
3904         ENTRY;
3905         it->oie_file.f_op->release(inode, &it->oie_file);
3906         lu_object_put(env, &obj->oo_dt.do_lu);
3907         EXIT;
3908 }
3909
3910 /**
3911  * It position the iterator at given key, so that next lookup continues from
3912  * that key Or it is similar to dio_it->load() but based on a key,
3913  * rather than file position.
3914  *
3915  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3916  * to the beginning.
3917  *
3918  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3919  */
3920 static int osd_it_ea_get(const struct lu_env *env,
3921                          struct dt_it *di, const struct dt_key *key)
3922 {
3923         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3924
3925         ENTRY;
3926         LASSERT(((const char *)key)[0] == '\0');
3927         it->oie_file.f_pos      = 0;
3928         it->oie_rd_dirent       = 0;
3929         it->oie_it_dirent       = 0;
3930         it->oie_dirent          = NULL;
3931
3932         RETURN(+1);
3933 }
3934
3935 /**
3936  * Does nothing
3937  */
3938 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3939 {
3940 }
3941
3942 /**
3943  * It is called internally by ->readdir(). It fills the
3944  * iterator's in-memory data structure with required
3945  * information i.e. name, namelen, rec_size etc.
3946  *
3947  * \param buf in which information to be filled in.
3948  * \param name name of the file in given dir
3949  *
3950  * \retval 0 on success
3951  * \retval 1 on buffer full
3952  */
3953 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3954                                loff_t offset, __u64 ino,
3955                                unsigned d_type)
3956 {
3957         struct osd_it_ea        *it   = (struct osd_it_ea *)buf;
3958         struct osd_it_ea_dirent *ent  = it->oie_dirent;
3959         struct lu_fid           *fid  = &ent->oied_fid;
3960         struct osd_fid_pack     *rec;
3961         ENTRY;
3962
3963         /* this should never happen */
3964         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3965                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3966                 RETURN(-EIO);
3967         }
3968
3969         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3970             OSD_IT_EA_BUFSIZE)
3971                 RETURN(1);
3972
3973         if (d_type & LDISKFS_DIRENT_LUFID) {
3974                 rec = (struct osd_fid_pack*) (name + namelen + 1);
3975
3976                 if (osd_fid_unpack(fid, rec) != 0)
3977                         fid_zero(fid);
3978
3979                 d_type &= ~LDISKFS_DIRENT_LUFID;
3980         } else {
3981                 fid_zero(fid);
3982         }
3983
3984         ent->oied_ino     = ino;
3985         ent->oied_off     = offset;
3986         ent->oied_namelen = namelen;
3987         ent->oied_type    = d_type;
3988
3989         memcpy(ent->oied_name, name, namelen);
3990
3991         it->oie_rd_dirent++;
3992         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3993         RETURN(0);
3994 }
3995
3996 /**
3997  * Calls ->readdir() to load a directory entry at a time
3998  * and stored it in iterator's in-memory data structure.
3999  *
4000  * \param di iterator's in memory structure
4001  *
4002  * \retval   0 on success
4003  * \retval -ve on error
4004  */
4005 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4006                                const struct dt_it *di)
4007 {
4008         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
4009         struct osd_object  *obj   = it->oie_obj;
4010         struct inode       *inode = obj->oo_inode;
4011         struct htree_lock  *hlock = NULL;
4012         int                 result = 0;
4013
4014         ENTRY;
4015         it->oie_dirent = it->oie_buf;
4016         it->oie_rd_dirent = 0;
4017
4018         if (obj->oo_hl_head != NULL) {
4019                 hlock = osd_oti_get(env)->oti_hlock;
4020                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4021                                    inode, LDISKFS_HLOCK_READDIR);
4022         } else {
4023                 cfs_down_read(&obj->oo_ext_idx_sem);
4024         }
4025
4026         result = inode->i_fop->readdir(&it->oie_file, it,
4027                                        (filldir_t) osd_ldiskfs_filldir);
4028
4029         if (hlock != NULL)
4030                 ldiskfs_htree_unlock(hlock);
4031         else
4032                 cfs_up_read(&obj->oo_ext_idx_sem);
4033
4034         if (it->oie_rd_dirent == 0) {
4035                 result = -EIO;
4036         } else {
4037                 it->oie_dirent = it->oie_buf;
4038                 it->oie_it_dirent = 1;
4039         }
4040
4041         RETURN(result);
4042 }
4043
4044 /**
4045  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4046  * to load a directory entry at a time and stored it in
4047  * iterator's in-memory data structure.
4048  *
4049  * \param di iterator's in memory structure
4050  *
4051  * \retval +ve iterator reached to end
4052  * \retval   0 iterator not reached to end
4053  * \retval -ve on error
4054  */
4055 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4056 {
4057         struct osd_it_ea *it = (struct osd_it_ea *)di;
4058         int rc;
4059
4060         ENTRY;
4061
4062         if (it->oie_it_dirent < it->oie_rd_dirent) {
4063                 it->oie_dirent =
4064                         (void *) it->oie_dirent +
4065                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4066                                        it->oie_dirent->oied_namelen);
4067                 it->oie_it_dirent++;
4068                 RETURN(0);
4069         } else {
4070                 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4071                         rc = +1;
4072                 else
4073                         rc = osd_ldiskfs_it_fill(env, di);
4074         }
4075
4076         RETURN(rc);
4077 }
4078
4079 /**
4080  * Returns the key at current position from iterator's in memory structure.
4081  *
4082  * \param di iterator's in memory structure
4083  *
4084  * \retval key i.e. struct dt_key on success
4085  */
4086 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4087                                     const struct dt_it *di)
4088 {
4089         struct osd_it_ea *it = (struct osd_it_ea *)di;
4090
4091         return (struct dt_key *)it->oie_dirent->oied_name;
4092 }
4093
4094 /**
4095  * Returns the key's size at current position from iterator's in memory structure.
4096  *
4097  * \param di iterator's in memory structure
4098  *
4099  * \retval key_size i.e. struct dt_key on success
4100  */
4101 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4102 {
4103         struct osd_it_ea *it = (struct osd_it_ea *)di;
4104
4105         return it->oie_dirent->oied_namelen;
4106 }
4107
4108
4109 /**
4110  * Returns the value (i.e. fid/igif) at current position from iterator's
4111  * in memory structure.
4112  *
4113  * \param di struct osd_it_ea, iterator's in memory structure
4114  * \param attr attr requested for dirent.
4115  * \param lde lustre dirent
4116  *
4117  * \retval   0 no error and \param lde has correct lustre dirent.
4118  * \retval -ve on error
4119  */
4120 static inline int osd_it_ea_rec(const struct lu_env *env,
4121                                 const struct dt_it *di,
4122                                 struct dt_rec *dtrec, __u32 attr)
4123 {
4124         struct osd_it_ea       *it    = (struct osd_it_ea *)di;
4125         struct osd_object      *obj   = it->oie_obj;
4126         struct osd_device      *dev   = osd_obj2dev(obj);
4127         struct osd_scrub       *scrub = &dev->od_scrub;
4128         struct scrub_file      *sf    = &scrub->os_file;
4129         struct osd_thread_info *oti   = osd_oti_get(env);
4130         struct osd_idmap_cache *oic   = &oti->oti_cache;
4131         struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
4132         struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
4133         __u32                   ino   = it->oie_dirent->oied_ino;
4134         int                     rc    = 0;
4135         ENTRY;
4136
4137         if (!fid_is_sane(fid)) {
4138                 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4139                 if (rc != 0)
4140                         RETURN(rc);
4141         } else {
4142                 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4143         }
4144
4145         osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4146                            it->oie_dirent->oied_name,
4147                            it->oie_dirent->oied_namelen,
4148                            it->oie_dirent->oied_type, attr);
4149         if (!fid_is_norm(fid)) {
4150                 fid_zero(&oic->oic_fid);
4151                 RETURN(0);
4152         }
4153
4154         oic->oic_fid = *fid;
4155         if ((scrub->os_pos_current <= ino) &&
4156             (sf->sf_flags & SF_INCONSISTENT ||
4157              ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4158                 rc = osd_consistency_check(oti, dev, oic);
4159
4160         RETURN(rc);
4161 }
4162
4163 /**
4164  * Returns a cookie for current position of the iterator head, so that
4165  * user can use this cookie to load/start the iterator next time.
4166  *
4167  * \param di iterator's in memory structure
4168  *
4169  * \retval cookie for current position, on success
4170  */
4171 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4172 {
4173         struct osd_it_ea *it = (struct osd_it_ea *)di;
4174
4175         return it->oie_dirent->oied_off;
4176 }
4177
4178 /**
4179  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4180  * to load a directory entry at a time and stored it i inn,
4181  * in iterator's in-memory data structure.
4182  *
4183  * \param di struct osd_it_ea, iterator's in memory structure
4184  *
4185  * \retval +ve on success
4186  * \retval -ve on error
4187  */
4188 static int osd_it_ea_load(const struct lu_env *env,
4189                           const struct dt_it *di, __u64 hash)
4190 {
4191         struct osd_it_ea *it = (struct osd_it_ea *)di;
4192         int rc;
4193
4194         ENTRY;
4195         it->oie_file.f_pos = hash;
4196
4197         rc =  osd_ldiskfs_it_fill(env, di);
4198         if (rc == 0)
4199                 rc = +1;
4200
4201         RETURN(rc);
4202 }
4203
4204 /**
4205  * Index lookup function for interoperability mode (b11826).
4206  *
4207  * \param key,  key i.e. file name to be searched
4208  *
4209  * \retval +ve, on success
4210  * \retval -ve, on error
4211  */
4212 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4213                                struct dt_rec *rec, const struct dt_key *key,
4214                                struct lustre_capa *capa)
4215 {
4216         struct osd_object *obj = osd_dt_obj(dt);
4217         int rc = 0;
4218
4219         ENTRY;
4220
4221         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4222         LINVRNT(osd_invariant(obj));
4223
4224         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4225                 return -EACCES;
4226
4227         rc = osd_ea_lookup_rec(env, obj, rec, key);
4228
4229         if (rc == 0)
4230                 rc = +1;
4231         RETURN(rc);
4232 }
4233
4234 /**
4235  * Index and Iterator operations for interoperability
4236  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4237  */
4238 static const struct dt_index_operations osd_index_ea_ops = {
4239         .dio_lookup         = osd_index_ea_lookup,
4240         .dio_declare_insert = osd_index_declare_ea_insert,
4241         .dio_insert         = osd_index_ea_insert,
4242         .dio_declare_delete = osd_index_declare_ea_delete,
4243         .dio_delete         = osd_index_ea_delete,
4244         .dio_it     = {
4245                 .init     = osd_it_ea_init,
4246                 .fini     = osd_it_ea_fini,
4247                 .get      = osd_it_ea_get,
4248                 .put      = osd_it_ea_put,
4249                 .next     = osd_it_ea_next,
4250                 .key      = osd_it_ea_key,
4251                 .key_size = osd_it_ea_key_size,
4252                 .rec      = osd_it_ea_rec,
4253                 .store    = osd_it_ea_store,
4254                 .load     = osd_it_ea_load
4255         }
4256 };
4257
4258 static void *osd_key_init(const struct lu_context *ctx,
4259                           struct lu_context_key *key)
4260 {
4261         struct osd_thread_info *info;
4262
4263         OBD_ALLOC_PTR(info);
4264         if (info == NULL)
4265                 return ERR_PTR(-ENOMEM);
4266
4267         OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4268         if (info->oti_it_ea_buf == NULL)
4269                 goto out_free_info;
4270
4271         info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4272
4273         info->oti_hlock = ldiskfs_htree_lock_alloc();
4274         if (info->oti_hlock == NULL)
4275                 goto out_free_ea;
4276
4277         return info;
4278
4279  out_free_ea:
4280         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4281  out_free_info:
4282         OBD_FREE_PTR(info);
4283         return ERR_PTR(-ENOMEM);
4284 }
4285
4286 static void osd_key_fini(const struct lu_context *ctx,
4287                          struct lu_context_key *key, void* data)
4288 {
4289         struct osd_thread_info *info = data;
4290
4291         if (info->oti_hlock != NULL)
4292                 ldiskfs_htree_lock_free(info->oti_hlock);
4293         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4294         OBD_FREE_PTR(info);
4295 }
4296
4297 static void osd_key_exit(const struct lu_context *ctx,
4298                          struct lu_context_key *key, void *data)
4299 {
4300         struct osd_thread_info *info = data;
4301
4302         LASSERT(info->oti_r_locks == 0);
4303         LASSERT(info->oti_w_locks == 0);
4304         LASSERT(info->oti_txns    == 0);
4305 }
4306
4307 /* type constructor/destructor: osd_type_init, osd_type_fini */
4308 LU_TYPE_INIT_FINI(osd, &osd_key);
4309
4310 struct lu_context_key osd_key = {
4311         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4312         .lct_init = osd_key_init,
4313         .lct_fini = osd_key_fini,
4314         .lct_exit = osd_key_exit
4315 };
4316
4317
4318 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4319                            const char *name, struct lu_device *next)
4320 {
4321         struct osd_device *osd = osd_dev(d);
4322
4323         strncpy(osd->od_svname, name, MAX_OBD_NAME);
4324         return osd_procfs_init(osd, name);
4325 }
4326
4327 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4328 {
4329         ENTRY;
4330
4331         osd_scrub_cleanup(env, o);
4332
4333         if (o->od_fsops) {
4334                 fsfilt_put_ops(o->od_fsops);
4335                 o->od_fsops = NULL;
4336         }
4337
4338         /* shutdown quota slave instance associated with the device */
4339         if (o->od_quota_slave != NULL) {
4340                 qsd_fini(env, o->od_quota_slave);
4341                 o->od_quota_slave = NULL;
4342         }
4343
4344         RETURN(0);
4345 }
4346
4347 static int osd_mount(const struct lu_env *env,
4348                      struct osd_device *o, struct lustre_cfg *cfg)
4349 {
4350         const char              *name  = lustre_cfg_string(cfg, 0);
4351         const char              *dev  = lustre_cfg_string(cfg, 1);
4352         const char              *opts;
4353         unsigned long            page, s_flags, lmd_flags = 0;
4354         struct page             *__page;
4355         struct file_system_type *type;
4356         char                    *options = NULL;
4357         char                    *str;
4358         int                       rc = 0;
4359         ENTRY;
4360
4361         if (o->od_mnt != NULL)
4362                 RETURN(0);
4363
4364         o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4365         if (o->od_fsops == NULL) {
4366                 CERROR("Can't find fsfilt_ldiskfs\n");
4367                 RETURN(-ENOTSUPP);
4368         }
4369
4370         OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4371         if (__page == NULL)
4372                 GOTO(out, rc = -ENOMEM);
4373
4374         str = lustre_cfg_string(cfg, 2);
4375         s_flags = simple_strtoul(str, NULL, 0);
4376         str = strstr(str, ":");
4377         if (str)
4378                 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4379         opts = lustre_cfg_string(cfg, 3);
4380         page = (unsigned long)cfs_page_address(__page);
4381         options = (char *)page;
4382         *options = '\0';
4383         if (opts == NULL)
4384                 strcat(options, "user_xattr,acl");
4385         else
4386                 strcat(options, opts);
4387
4388         /* Glom up mount options */
4389         if (*options != '\0')
4390                 strcat(options, ",");
4391         strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4392
4393         type = get_fs_type("ldiskfs");
4394         if (!type) {
4395                 CERROR("%s: cannot find ldiskfs module\n", name);
4396                 GOTO(out, rc = -ENODEV);
4397         }
4398
4399         o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4400         cfs_module_put(type->owner);
4401
4402         if (IS_ERR(o->od_mnt)) {
4403                 rc = PTR_ERR(o->od_mnt);
4404                 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4405                 o->od_mnt = NULL;
4406                 GOTO(out, rc);
4407         }
4408
4409         if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4410                 CERROR("%s: underlying device %s is marked as read-only. "
4411                        "Setup failed\n", name, dev);
4412                 mntput(o->od_mnt);
4413                 o->od_mnt = NULL;
4414                 GOTO(out, rc = -EROFS);
4415         }
4416
4417         if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4418             LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4419                 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4420                 mntput(o->od_mnt);
4421                 o->od_mnt = NULL;
4422                 GOTO(out, rc = -EINVAL);
4423         }
4424
4425         if (lmd_flags & LMD_FLG_IAM) {
4426                 o->od_iop_mode = 0;
4427                 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4428         } else
4429                 o->od_iop_mode = 1;
4430         if (lmd_flags & LMD_FLG_NOSCRUB)
4431                 o->od_scrub.os_no_scrub = 1;
4432
4433 out:
4434         if (__page)
4435                 OBD_PAGE_FREE(__page);
4436         if (rc)
4437                 fsfilt_put_ops(o->od_fsops);
4438
4439         RETURN(rc);
4440 }
4441
4442 static struct lu_device *osd_device_fini(const struct lu_env *env,
4443                                          struct lu_device *d)
4444 {
4445         int rc;
4446         ENTRY;
4447
4448         rc = osd_shutdown(env, osd_dev(d));
4449
4450         osd_compat_fini(osd_dev(d));
4451
4452         shrink_dcache_sb(osd_sb(osd_dev(d)));
4453         osd_sync(env, lu2dt_dev(d));
4454
4455         rc = osd_procfs_fini(osd_dev(d));
4456         if (rc) {
4457                 CERROR("proc fini error %d \n", rc);
4458                 RETURN (ERR_PTR(rc));
4459         }
4460
4461         if (osd_dev(d)->od_mnt) {
4462                 mntput(osd_dev(d)->od_mnt);
4463                 osd_dev(d)->od_mnt = NULL;
4464         }
4465
4466         RETURN(NULL);
4467 }
4468
4469 static int osd_device_init0(const struct lu_env *env,
4470                             struct osd_device *o,
4471                             struct lustre_cfg *cfg)
4472 {
4473         struct lu_device        *l = osd2lu_dev(o);
4474         struct osd_thread_info *info;
4475         int                     rc;
4476
4477         /* if the module was re-loaded, env can loose its keys */
4478         rc = lu_env_refill((struct lu_env *) env);
4479         if (rc)
4480                 GOTO(out, rc);
4481         info = osd_oti_get(env);
4482         LASSERT(info);
4483
4484         l->ld_ops = &osd_lu_ops;
4485         o->od_dt_dev.dd_ops = &osd_dt_ops;
4486
4487         cfs_spin_lock_init(&o->od_osfs_lock);
4488         cfs_mutex_init(&o->od_otable_mutex);
4489         o->od_osfs_age = cfs_time_shift_64(-1000);
4490
4491         o->od_capa_hash = init_capa_hash();
4492         if (o->od_capa_hash == NULL)
4493                 GOTO(out, rc = -ENOMEM);
4494
4495         o->od_read_cache = 1;
4496         o->od_writethrough_cache = 1;
4497
4498         rc = osd_mount(env, o, cfg);
4499         if (rc)
4500                 GOTO(out_capa, rc);
4501
4502         /* setup scrub, including OI files initialization */
4503         rc = osd_scrub_setup(env, o);
4504         if (rc < 0)
4505                 GOTO(out_mnt, rc);
4506
4507         strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4508                         sizeof(o->od_svname) - 1);
4509
4510         rc = osd_compat_init(o);
4511         if (rc != 0)
4512                 GOTO(out_scrub, rc);
4513
4514         rc = lu_site_init(&o->od_site, l);
4515         if (rc)
4516                 GOTO(out_compat, rc);
4517
4518         rc = lu_site_init_finish(&o->od_site);
4519         if (rc)
4520                 GOTO(out_site, rc);
4521
4522         rc = osd_procfs_init(o, o->od_svname);
4523         if (rc != 0) {
4524                 CERROR("%s: can't initialize procfs: rc = %d\n",
4525                        o->od_svname, rc);
4526                 GOTO(out_site, rc);
4527         }
4528
4529         LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4530
4531         RETURN(0);
4532 out_site:
4533         lu_site_fini(&o->od_site);
4534 out_compat:
4535         osd_compat_fini(o);
4536 out_scrub:
4537         osd_scrub_cleanup(env, o);
4538 out_mnt:
4539         osd_oi_fini(info, o);
4540         osd_shutdown(env, o);
4541         mntput(o->od_mnt);
4542         o->od_mnt = NULL;
4543 out_capa:
4544         cleanup_capa_hash(o->od_capa_hash);
4545 out:
4546         RETURN(rc);
4547 }
4548
4549 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4550                                           struct lu_device_type *t,
4551                                           struct lustre_cfg *cfg)
4552 {
4553         struct osd_device *o;
4554         int                rc;
4555
4556         OBD_ALLOC_PTR(o);
4557         if (o == NULL)
4558                 return ERR_PTR(-ENOMEM);
4559
4560         rc = dt_device_init(&o->od_dt_dev, t);
4561         if (rc == 0) {
4562                 rc = osd_device_init0(env, o, cfg);
4563                 if (rc)
4564                         dt_device_fini(&o->od_dt_dev);
4565         }
4566
4567         if (unlikely(rc != 0))
4568                 OBD_FREE_PTR(o);
4569
4570         return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4571 }
4572
4573 static struct lu_device *osd_device_free(const struct lu_env *env,
4574                                          struct lu_device *d)
4575 {
4576         struct osd_device *o = osd_dev(d);
4577         ENTRY;
4578
4579         cleanup_capa_hash(o->od_capa_hash);
4580         /* XXX: make osd top device in order to release reference */
4581         d->ld_site->ls_top_dev = d;
4582         lu_site_purge(env, d->ld_site, -1);
4583         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4584                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4585                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4586         }
4587         lu_site_fini(&o->od_site);
4588         dt_device_fini(&o->od_dt_dev);
4589         OBD_FREE_PTR(o);
4590         RETURN(NULL);
4591 }
4592
4593 static int osd_process_config(const struct lu_env *env,
4594                               struct lu_device *d, struct lustre_cfg *cfg)
4595 {
4596         struct osd_device *o = osd_dev(d);
4597         int err;
4598         ENTRY;
4599
4600         switch(cfg->lcfg_command) {
4601         case LCFG_SETUP:
4602                 err = osd_mount(env, o, cfg);
4603                 break;
4604         case LCFG_CLEANUP:
4605                 lu_dev_del_linkage(d->ld_site, d);
4606                 err = osd_shutdown(env, o);
4607                 break;
4608         default:
4609                 err = -ENOSYS;
4610         }
4611
4612         RETURN(err);
4613 }
4614
4615 static int osd_recovery_complete(const struct lu_env *env,
4616                                  struct lu_device *d)
4617 {
4618         RETURN(0);
4619 }
4620
4621 /*
4622  * we use exports to track all osd users
4623  */
4624 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4625                            struct obd_device *obd, struct obd_uuid *cluuid,
4626                            struct obd_connect_data *data, void *localdata)
4627 {
4628         struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
4629         struct lustre_handle  conn;
4630         int                   rc;
4631         ENTRY;
4632
4633         CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4634
4635         rc = class_connect(&conn, obd, cluuid);
4636         if (rc)
4637                 RETURN(rc);
4638
4639         *exp = class_conn2export(&conn);
4640
4641         cfs_spin_lock(&osd->od_osfs_lock);
4642         osd->od_connects++;
4643         cfs_spin_unlock(&osd->od_osfs_lock);
4644
4645         RETURN(0);
4646 }
4647
4648 /*
4649  * once last export (we don't count self-export) disappeared
4650  * osd can be released
4651  */
4652 static int osd_obd_disconnect(struct obd_export *exp)
4653 {
4654         struct obd_device *obd = exp->exp_obd;
4655         struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4656         int                rc, release = 0;
4657         ENTRY;
4658
4659         /* Only disconnect the underlying layers on the final disconnect. */
4660         cfs_spin_lock(&osd->od_osfs_lock);
4661         osd->od_connects--;
4662         if (osd->od_connects == 0)
4663                 release = 1;
4664         cfs_spin_unlock(&osd->od_osfs_lock);
4665
4666         rc = class_disconnect(exp); /* bz 9811 */
4667
4668         if (rc == 0 && release)
4669                 class_manual_cleanup(obd);
4670         RETURN(rc);
4671 }
4672
4673 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4674                        struct lu_device *dev)
4675 {
4676         struct osd_device *osd = osd_dev(dev);
4677         int                result = 0;
4678         ENTRY;
4679
4680 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
4681         /* Unfortunately, the current MDD implementation relies on some specific
4682          * code to be executed in the OSD layer. Since OFD now also uses the OSD
4683          * module, we need a way to skip the metadata-specific code when running
4684          * with OFD.
4685          * The hack here is to check the type of the parent device which is
4686          * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4687          * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4688          * layer and still relies on lvfs. This hack won't work any more when
4689          * LOD is landed since LOD is of DT type.
4690          * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4691          * have been landed */
4692         osd->od_is_md = lu_device_is_md(pdev);
4693 #else
4694 #warning "all is_md checks must be removed from osd-ldiskfs"
4695 #endif
4696
4697         if (osd->od_is_md) {
4698                 /* 1. setup local objects */
4699                 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4700                                                  lu2dt_dev(dev));
4701                 if (result)
4702                         RETURN(result);
4703         }
4704
4705         /* 2. setup quota slave instance */
4706         osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4707                                        osd->od_proc_entry);
4708         if (IS_ERR(osd->od_quota_slave)) {
4709                 result = PTR_ERR(osd->od_quota_slave);
4710                 osd->od_quota_slave = NULL;
4711         }
4712
4713         RETURN(result);
4714 }
4715
4716 static const struct lu_object_operations osd_lu_obj_ops = {
4717         .loo_object_init      = osd_object_init,
4718         .loo_object_delete    = osd_object_delete,
4719         .loo_object_release   = osd_object_release,
4720         .loo_object_free      = osd_object_free,
4721         .loo_object_print     = osd_object_print,
4722         .loo_object_invariant = osd_object_invariant
4723 };
4724
4725 const struct lu_device_operations osd_lu_ops = {
4726         .ldo_object_alloc      = osd_object_alloc,
4727         .ldo_process_config    = osd_process_config,
4728         .ldo_recovery_complete = osd_recovery_complete,
4729         .ldo_prepare           = osd_prepare,
4730 };
4731
4732 static const struct lu_device_type_operations osd_device_type_ops = {
4733         .ldto_init = osd_type_init,
4734         .ldto_fini = osd_type_fini,
4735
4736         .ldto_start = osd_type_start,
4737         .ldto_stop  = osd_type_stop,
4738
4739         .ldto_device_alloc = osd_device_alloc,
4740         .ldto_device_free  = osd_device_free,
4741
4742         .ldto_device_init    = osd_device_init,
4743         .ldto_device_fini    = osd_device_fini
4744 };
4745
4746 struct lu_device_type osd_device_type = {
4747         .ldt_tags     = LU_DEVICE_DT,
4748         .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
4749         .ldt_ops      = &osd_device_type_ops,
4750         .ldt_ctx_tags = LCT_LOCAL,
4751 };
4752
4753 /*
4754  * lprocfs legacy support.
4755  */
4756 static struct obd_ops osd_obd_device_ops = {
4757         .o_owner = THIS_MODULE,
4758         .o_connect      = osd_obd_connect,
4759         .o_disconnect   = osd_obd_disconnect
4760 };
4761
4762 static int __init osd_mod_init(void)
4763 {
4764         struct lprocfs_static_vars lvars;
4765
4766         osd_oi_mod_init();
4767         lprocfs_osd_init_vars(&lvars);
4768         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4769                                    LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4770 }
4771
4772 static void __exit osd_mod_exit(void)
4773 {
4774         class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4775 }
4776
4777 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4778 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4779 MODULE_LICENSE("GPL");
4780
4781 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);