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