Whamcloud - gitweb
b=17670
[fs/lustre-release.git] / lustre / osd / osd_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49
50 /* LUSTRE_VERSION_CODE */
51 #include <lustre_ver.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/types.h>
54 /* prerequisite for linux/xattr.h */
55 #include <linux/fs.h>
56 /* XATTR_{REPLACE,CREATE} */
57 #include <linux/xattr.h>
58 /*
59  * XXX temporary stuff: direct access to ldiskfs/jdb. Interface between osd
60  * and file system is not yet specified.
61  */
62 /* handle_t, journal_start(), journal_stop() */
63 #include <linux/jbd.h>
64 /* LDISKFS_SB() */
65 #include <linux/ldiskfs_fs.h>
66 #include <linux/ldiskfs_jbd.h>
67 /* simple_mkdir() */
68 #include <lvfs.h>
69
70 /*
71  * struct OBD_{ALLOC,FREE}*()
72  * OBD_FAIL_CHECK
73  */
74 #include <obd_support.h>
75 /* struct ptlrpc_thread */
76 #include <lustre_net.h>
77
78 /* fid_is_local() */
79 #include <lustre_fid.h>
80 #include <linux/lustre_iam.h>
81
82 #include "osd_internal.h"
83 #include "osd_igif.h"
84
85 /* llo_* api support */
86 #include <md_object.h>
87
88 static const char dot[] = ".";
89 static const char dotdot[] = "..";
90 static const char remote_obj_dir[] = "REM_OBJ_DIR";
91
92 struct osd_directory {
93         struct iam_container od_container;
94         struct iam_descr     od_descr;
95 };
96
97 struct osd_object {
98         struct dt_object       oo_dt;
99         /**
100          * Inode for file system object represented by this osd_object. This
101          * inode is pinned for the whole duration of lu_object life.
102          *
103          * Not modified concurrently (either setup early during object
104          * creation, or assigned by osd_object_create() under write lock).
105          */
106         struct inode          *oo_inode;
107         /**
108          * to protect index ops.
109          */
110         struct rw_semaphore    oo_ext_idx_sem;
111         struct rw_semaphore    oo_sem;
112         struct osd_directory  *oo_dir;
113         /** protects inode attributes. */
114         spinlock_t             oo_guard;
115         /**
116          * Following two members are used to indicate the presence of dot and
117          * dotdot in the given directory. This is required for interop mode
118          * (b11826).
119          */
120         int oo_compat_dot_created;
121         int oo_compat_dotdot_created;
122
123         const struct lu_env   *oo_owner;
124 #ifdef CONFIG_LOCKDEP
125         struct lockdep_map     oo_dep_map;
126 #endif
127 };
128
129 static const struct lu_object_operations      osd_lu_obj_ops;
130 static const struct lu_device_operations      osd_lu_ops;
131 static       struct lu_context_key            osd_key;
132 static const struct dt_object_operations      osd_obj_ops;
133 static const struct dt_object_operations      osd_obj_ea_ops;
134 static const struct dt_body_operations        osd_body_ops;
135 static const struct dt_index_operations       osd_index_iam_ops;
136 static const struct dt_index_operations       osd_index_ea_ops;
137
138 struct osd_thandle {
139         struct thandle          ot_super;
140         handle_t               *ot_handle;
141         struct journal_callback ot_jcb;
142         /* Link to the device, for debugging. */
143         struct lu_ref_link     *ot_dev_link;
144
145 };
146
147 /*
148  * Helpers.
149  */
150 static int lu_device_is_osd(const struct lu_device *d)
151 {
152         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
153 }
154
155 static struct osd_device *osd_dt_dev(const struct dt_device *d)
156 {
157         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
158         return container_of0(d, struct osd_device, od_dt_dev);
159 }
160
161 static struct osd_device *osd_dev(const struct lu_device *d)
162 {
163         LASSERT(lu_device_is_osd(d));
164         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
165 }
166
167 static struct osd_device *osd_obj2dev(const struct osd_object *o)
168 {
169         return osd_dev(o->oo_dt.do_lu.lo_dev);
170 }
171
172 static struct super_block *osd_sb(const struct osd_device *dev)
173 {
174         return dev->od_mount->lmi_mnt->mnt_sb;
175 }
176
177 static int osd_object_is_root(const struct osd_object *obj)
178 {
179         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
180 }
181
182 static struct osd_object *osd_obj(const struct lu_object *o)
183 {
184         LASSERT(lu_device_is_osd(o->lo_dev));
185         return container_of0(o, struct osd_object, oo_dt.do_lu);
186 }
187
188 static struct osd_object *osd_dt_obj(const struct dt_object *d)
189 {
190         return osd_obj(&d->do_lu);
191 }
192
193 static struct lu_device *osd2lu_dev(struct osd_device *osd)
194 {
195         return &osd->od_dt_dev.dd_lu_dev;
196 }
197
198 static journal_t *osd_journal(const struct osd_device *dev)
199 {
200         return LDISKFS_SB(osd_sb(dev))->s_journal;
201 }
202
203 static int osd_has_index(const struct osd_object *obj)
204 {
205         return obj->oo_dt.do_index_ops != NULL;
206 }
207
208 static int osd_object_invariant(const struct lu_object *l)
209 {
210         return osd_invariant(osd_obj(l));
211 }
212
213 #ifdef HAVE_QUOTA_SUPPORT
214 static inline void
215 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
216 {
217         struct md_ucred    *uc = md_ucred(env);
218
219         LASSERT(uc != NULL);
220
221         save->oc_uid = current->fsuid;
222         save->oc_gid = current->fsgid;
223         save->oc_cap = current->cap_effective;
224         current->fsuid         = uc->mu_fsuid;
225         current->fsgid         = uc->mu_fsgid;
226         current->cap_effective = uc->mu_cap;
227 }
228
229 static inline void
230 osd_pop_ctxt(struct osd_ctxt *save)
231 {
232         current->fsuid         = save->oc_uid;
233         current->fsgid         = save->oc_gid;
234         current->cap_effective = save->oc_cap;
235 }
236 #endif
237
238 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
239 {
240         return lu_context_key_get(&env->le_ctx, &osd_key);
241 }
242
243 /*
244  * Concurrency: doesn't matter
245  */
246 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
247 {
248         return osd_oti_get(env)->oti_r_locks > 0;
249 }
250
251 /*
252  * Concurrency: doesn't matter
253  */
254 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
255 {
256         struct osd_thread_info *oti = osd_oti_get(env);
257         return oti->oti_w_locks > 0 && o->oo_owner == env;
258 }
259
260 /*
261  * Concurrency: doesn't access mutable data
262  */
263 static int osd_root_get(const struct lu_env *env,
264                         struct dt_device *dev, struct lu_fid *f)
265 {
266         struct inode *inode;
267
268         inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
269         lu_igif_build(f, inode->i_ino, inode->i_generation);
270         return 0;
271 }
272
273 /*
274  * OSD object methods.
275  */
276
277 /*
278  * Concurrency: no concurrent access is possible that early in object
279  * life-cycle.
280  */
281 static struct lu_object *osd_object_alloc(const struct lu_env *env,
282                                           const struct lu_object_header *hdr,
283                                           struct lu_device *d)
284 {
285         struct osd_object *mo;
286
287         OBD_ALLOC_PTR(mo);
288         if (mo != NULL) {
289                 struct lu_object *l;
290
291                 l = &mo->oo_dt.do_lu;
292                 dt_object_init(&mo->oo_dt, NULL, d);
293                 if (osd_dev(d)->od_iop_mode)
294                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
295                 else
296                         mo->oo_dt.do_ops = &osd_obj_ops;
297
298                 l->lo_ops = &osd_lu_obj_ops;
299                 init_rwsem(&mo->oo_sem);
300                 init_rwsem(&mo->oo_ext_idx_sem);
301                 spin_lock_init(&mo->oo_guard);
302                 return l;
303         } else
304                 return NULL;
305 }
306
307 /*
308  * retrieve object from backend ext fs.
309  **/
310 static struct inode *osd_iget(struct osd_thread_info *info,
311                               struct osd_device *dev,
312                               const struct osd_inode_id *id)
313 {
314         struct inode *inode;
315
316         inode = iget(osd_sb(dev), id->oii_ino);
317         if (inode == NULL) {
318                 CERROR("no inode\n");
319                 inode = ERR_PTR(-EACCES);
320         } else if (id->oii_gen != OSD_OII_NOGEN &&
321                    inode->i_generation != id->oii_gen) {
322                 iput(inode);
323                 inode = ERR_PTR(-ESTALE);
324         } else if (inode->i_nlink == 0) {
325                 /* due to parallel readdir and unlink,
326                 * we can have dead inode here. */
327                 CWARN("stale inode\n");
328                 make_bad_inode(inode);
329                 iput(inode);
330                 inode = ERR_PTR(-ESTALE);
331         } else if (is_bad_inode(inode)) {
332                 CERROR("bad inode %lx\n",inode->i_ino);
333                 iput(inode);
334                 inode = ERR_PTR(-ENOENT);
335         }
336         return inode;
337 }
338
339 static int osd_fid_lookup(const struct lu_env *env,
340                           struct osd_object *obj, const struct lu_fid *fid)
341 {
342         struct osd_thread_info *info;
343         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
344         struct osd_device      *dev;
345         struct osd_inode_id    *id;
346         struct osd_oi          *oi;
347         struct inode           *inode;
348         int                     result;
349
350         LINVRNT(osd_invariant(obj));
351         LASSERT(obj->oo_inode == NULL);
352         LASSERT(fid_is_sane(fid));
353         /*
354          * This assertion checks that osd layer sees only local
355          * fids. Unfortunately it is somewhat expensive (does a
356          * cache-lookup). Disabling it for production/acceptance-testing.
357          */
358         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
359
360         ENTRY;
361
362         info = osd_oti_get(env);
363         dev  = osd_dev(ldev);
364         id   = &info->oti_id;
365         oi   = &dev->od_oi;
366
367         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
368                 RETURN(-ENOENT);
369
370         result = osd_oi_lookup(info, oi, fid, id);
371         if (result == 0) {
372                 inode = osd_iget(info, dev, id);
373                 if (!IS_ERR(inode)) {
374                         obj->oo_inode = inode;
375                         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
376                         if (dev->od_iop_mode) {
377                                 obj->oo_compat_dot_created = 1;
378                                 obj->oo_compat_dotdot_created = 1;
379                         }
380                         result = 0;
381                 } else
382                         /*
383                          * If fid wasn't found in oi, inode-less object is
384                          * created, for which lu_object_exists() returns
385                          * false. This is used in a (frequent) case when
386                          * objects are created as locking anchors or
387                          * place holders for objects yet to be created.
388                          */
389                         result = PTR_ERR(inode);
390         } else if (result == -ENOENT)
391                 result = 0;
392         LINVRNT(osd_invariant(obj));
393
394         RETURN(result);
395 }
396
397 /*
398  * Concurrency: shouldn't matter.
399  */
400 static void osd_object_init0(struct osd_object *obj)
401 {
402         LASSERT(obj->oo_inode != NULL);
403         obj->oo_dt.do_body_ops = &osd_body_ops;
404         obj->oo_dt.do_lu.lo_header->loh_attr |=
405                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
406 }
407
408 /*
409  * Concurrency: no concurrent access is possible that early in object
410  * life-cycle.
411  */
412 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
413                            const struct lu_object_conf *unused)
414 {
415         struct osd_object *obj = osd_obj(l);
416         int result;
417
418         LINVRNT(osd_invariant(obj));
419
420         result = osd_fid_lookup(env, obj, lu_object_fid(l));
421         if (result == 0) {
422                 if (obj->oo_inode != NULL)
423                         osd_object_init0(obj);
424         }
425         LINVRNT(osd_invariant(obj));
426         return result;
427 }
428
429 /*
430  * Concurrency: no concurrent access is possible that late in object
431  * life-cycle.
432  */
433 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
434 {
435         struct osd_object *obj = osd_obj(l);
436
437         LINVRNT(osd_invariant(obj));
438
439         dt_object_fini(&obj->oo_dt);
440         OBD_FREE_PTR(obj);
441 }
442
443 /**
444  * IAM Iterator
445  */
446 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
447                                              const struct iam_container *bag)
448 {
449         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
450                                            osd_oti_get(env)->oti_it_ipd);
451 }
452
453 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
454                                               const struct iam_container *bag)
455 {
456         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
457                                            osd_oti_get(env)->oti_idx_ipd);
458 }
459
460 static void osd_ipd_put(const struct lu_env *env,
461                         const struct iam_container *bag,
462                         struct iam_path_descr *ipd)
463 {
464         bag->ic_descr->id_ops->id_ipd_free(ipd);
465 }
466
467 /*
468  * Concurrency: no concurrent access is possible that late in object
469  * life-cycle.
470  */
471 static void osd_index_fini(struct osd_object *o)
472 {
473         struct iam_container *bag;
474
475         if (o->oo_dir != NULL) {
476                 bag = &o->oo_dir->od_container;
477                 if (o->oo_inode != NULL) {
478                         if (bag->ic_object == o->oo_inode)
479                                 iam_container_fini(bag);
480                 }
481                 OBD_FREE_PTR(o->oo_dir);
482                 o->oo_dir = NULL;
483         }
484 }
485
486 /*
487  * Concurrency: no concurrent access is possible that late in object
488  * life-cycle (for all existing callers, that is. New callers have to provide
489  * their own locking.)
490  */
491 static int osd_inode_unlinked(const struct inode *inode)
492 {
493         return inode->i_nlink == 0;
494 }
495
496 enum {
497         OSD_TXN_OI_DELETE_CREDITS    = 20,
498         OSD_TXN_INODE_DELETE_CREDITS = 20
499 };
500
501 /*
502  * Journal
503  */
504
505 /*
506  * Concurrency: doesn't access mutable data.
507  */
508 static int osd_param_is_sane(const struct osd_device *dev,
509                              const struct txn_param *param)
510 {
511         return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
512 }
513
514 /*
515  * Concurrency: shouldn't matter.
516  */
517 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
518 {
519         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
520         struct thandle     *th  = &oh->ot_super;
521         struct dt_device   *dev = th->th_dev;
522         struct lu_device   *lud = &dev->dd_lu_dev;
523
524         LASSERT(dev != NULL);
525         LASSERT(oh->ot_handle == NULL);
526
527         if (error) {
528                 CERROR("transaction @0x%p commit error: %d\n", th, error);
529         } else {
530                 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
531                 /*
532                  * This od_env_for_commit is only for commit usage.  see
533                  * "struct dt_device"
534                  */
535                 lu_context_enter(&env->le_ctx);
536                 dt_txn_hook_commit(env, th);
537                 lu_context_exit(&env->le_ctx);
538         }
539
540         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
541         lu_device_put(lud);
542         th->th_dev = NULL;
543
544         lu_context_exit(&th->th_ctx);
545         lu_context_fini(&th->th_ctx);
546         OBD_FREE_PTR(oh);
547 }
548
549 /*
550  * Concurrency: shouldn't matter.
551  */
552 static struct thandle *osd_trans_start(const struct lu_env *env,
553                                        struct dt_device *d,
554                                        struct txn_param *p)
555 {
556         struct osd_device  *dev = osd_dt_dev(d);
557         handle_t           *jh;
558         struct osd_thandle *oh;
559         struct thandle     *th;
560         int hook_res;
561
562         ENTRY;
563
564         hook_res = dt_txn_hook_start(env, d, p);
565         if (hook_res != 0)
566                 RETURN(ERR_PTR(hook_res));
567
568         if (osd_param_is_sane(dev, p)) {
569                 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
570                 if (oh != NULL) {
571                         struct osd_thread_info *oti = osd_oti_get(env);
572
573                         /*
574                          * XXX temporary stuff. Some abstraction layer should
575                          * be used.
576                          */
577
578                         jh = journal_start(osd_journal(dev), p->tp_credits);
579                         if (!IS_ERR(jh)) {
580                                 oh->ot_handle = jh;
581                                 th = &oh->ot_super;
582                                 th->th_dev = d;
583                                 th->th_result = 0;
584                                 jh->h_sync = p->tp_sync;
585                                 lu_device_get(&d->dd_lu_dev);
586                                 oh->ot_dev_link = lu_ref_add
587                                         (&d->dd_lu_dev.ld_reference,
588                                          "osd-tx", th);
589                                 /* add commit callback */
590                                 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
591                                 lu_context_enter(&th->th_ctx);
592                                 journal_callback_set(jh, osd_trans_commit_cb,
593                                                      (struct journal_callback *)&oh->ot_jcb);
594                                         LASSERT(oti->oti_txns == 0);
595                                         LASSERT(oti->oti_r_locks == 0);
596                                         LASSERT(oti->oti_w_locks == 0);
597                                         oti->oti_txns++;
598                         } else {
599                                 OBD_FREE_PTR(oh);
600                                 th = (void *)jh;
601                         }
602                 } else
603                         th = ERR_PTR(-ENOMEM);
604         } else {
605                 CERROR("Invalid transaction parameters\n");
606                 th = ERR_PTR(-EINVAL);
607         }
608
609         RETURN(th);
610 }
611
612 /*
613  * Concurrency: shouldn't matter.
614  */
615 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
616 {
617         int result;
618         struct osd_thandle *oh;
619         struct osd_thread_info *oti = osd_oti_get(env);
620
621         ENTRY;
622
623         oh = container_of0(th, struct osd_thandle, ot_super);
624         if (oh->ot_handle != NULL) {
625                 handle_t *hdl = oh->ot_handle;
626
627                 LASSERT(oti->oti_txns == 1);
628                 oti->oti_txns--;
629                 LASSERT(oti->oti_r_locks == 0);
630                 LASSERT(oti->oti_w_locks == 0);
631                 result = dt_txn_hook_stop(env, th);
632                 if (result != 0)
633                         CERROR("Failure in transaction hook: %d\n", result);
634                 oh->ot_handle = NULL;
635                 result = journal_stop(hdl);
636                 if (result != 0)
637                         CERROR("Failure to stop transaction: %d\n", result);
638         }
639         EXIT;
640 }
641
642 /*
643  * Concurrency: no concurrent access is possible that late in object
644  * life-cycle.
645  */
646 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
647 {
648         const struct lu_fid    *fid = lu_object_fid(&obj->oo_dt.do_lu);
649         struct osd_device      *osd = osd_obj2dev(obj);
650         struct osd_thread_info *oti = osd_oti_get(env);
651         struct txn_param       *prm = &oti->oti_txn;
652         struct lu_env          *env_del_obj = &oti->oti_obj_delete_tx_env;
653         struct thandle         *th;
654         int result;
655
656         lu_env_init(env_del_obj, LCT_DT_THREAD);
657         txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
658                             OSD_TXN_INODE_DELETE_CREDITS);
659         th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
660         if (!IS_ERR(th)) {
661                 result = osd_oi_delete(osd_oti_get(env_del_obj),
662                                        &osd->od_oi, fid, th);
663                 osd_trans_stop(env_del_obj, th);
664         } else
665                 result = PTR_ERR(th);
666
667         lu_env_fini(env_del_obj);
668         return result;
669 }
670
671 /*
672  * Called just before object is freed. Releases all resources except for
673  * object itself (that is released by osd_object_free()).
674  *
675  * Concurrency: no concurrent access is possible that late in object
676  * life-cycle.
677  */
678 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
679 {
680         struct osd_object *obj   = osd_obj(l);
681         struct inode      *inode = obj->oo_inode;
682
683         LINVRNT(osd_invariant(obj));
684
685         /*
686          * If object is unlinked remove fid->ino mapping from object index.
687          */
688
689         osd_index_fini(obj);
690         if (inode != NULL) {
691                 int result;
692
693                 if (osd_inode_unlinked(inode)) {
694                         result = osd_inode_remove(env, obj);
695                         if (result != 0)
696                                 LU_OBJECT_DEBUG(D_ERROR, env, l,
697                                                 "Failed to cleanup: %d\n",
698                                                 result);
699                 }
700
701                 iput(inode);
702                 obj->oo_inode = NULL;
703         }
704 }
705
706 /*
707  * Concurrency: ->loo_object_release() is called under site spin-lock.
708  */
709 static void osd_object_release(const struct lu_env *env,
710                                struct lu_object *l)
711 {
712         struct osd_object *o = osd_obj(l);
713
714         LASSERT(!lu_object_is_dying(l->lo_header));
715         if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
716                 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
717 }
718
719 /*
720  * Concurrency: shouldn't matter.
721  */
722 static int osd_object_print(const struct lu_env *env, void *cookie,
723                             lu_printer_t p, const struct lu_object *l)
724 {
725         struct osd_object *o = osd_obj(l);
726         struct iam_descr  *d;
727
728         if (o->oo_dir != NULL)
729                 d = o->oo_dir->od_container.ic_descr;
730         else
731                 d = NULL;
732         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
733                     o, o->oo_inode,
734                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
735                     o->oo_inode ? o->oo_inode->i_generation : 0,
736                     d ? d->id_ops->id_name : "plain");
737 }
738
739 /*
740  * Concurrency: shouldn't matter.
741  */
742 int osd_statfs(const struct lu_env *env, struct dt_device *d,
743                struct kstatfs *sfs)
744 {
745         struct osd_device *osd = osd_dt_dev(d);
746         struct super_block *sb = osd_sb(osd);
747         int result = 0;
748
749         spin_lock(&osd->od_osfs_lock);
750         /* cache 1 second */
751         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
752                 result = ll_do_statfs(sb, &osd->od_kstatfs);
753                 if (likely(result == 0)) /* N.B. statfs can't really fail */
754                         osd->od_osfs_age = cfs_time_current_64();
755         }
756
757         if (likely(result == 0))
758                 *sfs = osd->od_kstatfs;
759         spin_unlock(&osd->od_osfs_lock);
760
761         return result;
762 }
763
764 /*
765  * Concurrency: doesn't access mutable data.
766  */
767 static void osd_conf_get(const struct lu_env *env,
768                          const struct dt_device *dev,
769                          struct dt_device_param *param)
770 {
771         /*
772          * XXX should be taken from not-yet-existing fs abstraction layer.
773          */
774         param->ddp_max_name_len  = LDISKFS_NAME_LEN;
775         param->ddp_max_nlink     = LDISKFS_LINK_MAX;
776         param->ddp_block_shift   = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
777 }
778
779 /**
780  * Helper function to get and fill the buffer with input values.
781  */
782 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
783 {
784         struct lu_buf *buf;
785
786         buf = &osd_oti_get(env)->oti_buf;
787         buf->lb_buf = area;
788         buf->lb_len = len;
789         return buf;
790 }
791
792 /*
793  * Concurrency: shouldn't matter.
794  */
795 static int osd_sync(const struct lu_env *env, struct dt_device *d)
796 {
797         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
798         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
799 }
800
801 /**
802  * Start commit for OSD device.
803  *
804  * An implementation of dt_commit_async method for OSD device.
805  * Asychronously starts underlayng fs sync and thereby a transaction
806  * commit.
807  *
808  * \param env environment
809  * \param d dt device
810  *
811  * \see dt_device_operations
812  */
813 static int osd_commit_async(const struct lu_env *env,
814                             struct dt_device *d)
815 {
816         struct super_block *s = osd_sb(osd_dt_dev(d));
817         ENTRY;
818
819         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
820         RETURN(s->s_op->sync_fs(s, 0));
821 }
822
823 /*
824  * Concurrency: shouldn't matter.
825  */
826 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
827
828 static void osd_ro(const struct lu_env *env, struct dt_device *d)
829 {
830         ENTRY;
831
832         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
833
834         __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
835                           fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
836         EXIT;
837 }
838
839
840 /*
841  * Concurrency: serialization provided by callers.
842  */
843 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
844                               int mode, unsigned long timeout, __u32 alg,
845                               struct lustre_capa_key *keys)
846 {
847         struct osd_device *dev = osd_dt_dev(d);
848         ENTRY;
849
850         dev->od_fl_capa = mode;
851         dev->od_capa_timeout = timeout;
852         dev->od_capa_alg = alg;
853         dev->od_capa_keys = keys;
854         RETURN(0);
855 }
856
857 /**
858  * Concurrency: serialization provided by callers.
859  */
860 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
861                                struct dt_quota_ctxt *ctxt, void *data)
862 {
863         struct obd_device *obd = (void *)ctxt;
864         struct vfsmount *mnt = (struct vfsmount *)data;
865         ENTRY;
866
867         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
868         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
869         obd->obd_lvfs_ctxt.pwdmnt = mnt;
870         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
871         obd->obd_lvfs_ctxt.fs = get_ds();
872
873         EXIT;
874 }
875
876 /**
877  * Note: we do not count into QUOTA here.
878  * If we mount with --data_journal we may need more.
879  */
880 static const int osd_dto_credits_noquota[DTO_NR] = {
881         /**
882          * Insert/Delete.
883          * INDEX_EXTRA_TRANS_BLOCKS(8) +
884          * SINGLEDATA_TRANS_BLOCKS(8)
885          * XXX Note: maybe iam need more, since iam have more level than
886          *           EXT3 htree.
887          */
888         [DTO_INDEX_INSERT]  = 16,
889         [DTO_INDEX_DELETE]  = 16,
890         /**
891          * Unused now
892          */
893         [DTO_IDNEX_UPDATE]  = 16,
894         /**
895          * Create a object. The same as create object in EXT3.
896          * DATA_TRANS_BLOCKS(14) +
897          * INDEX_EXTRA_BLOCKS(8) +
898          * 3(inode bits, groups, GDT)
899          */
900         [DTO_OBJECT_CREATE] = 25,
901         /**
902          * Unused now
903          */
904         [DTO_OBJECT_DELETE] = 25,
905         /**
906          * Attr set credits.
907          * 3(inode bits, group, GDT)
908          */
909         [DTO_ATTR_SET_BASE] = 3,
910         /**
911          * Xattr set. The same as xattr of EXT3.
912          * DATA_TRANS_BLOCKS(14)
913          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
914          *           also counted in. Do not know why?
915          */
916         [DTO_XATTR_SET]     = 14,
917         [DTO_LOG_REC]       = 14,
918         /**
919          * creadits for inode change during write.
920          */
921         [DTO_WRITE_BASE]    = 3,
922         /**
923          * credits for single block write.
924          */
925         [DTO_WRITE_BLOCK]   = 14,
926         /**
927          * Attr set credits for chown.
928          * 3 (inode bit, group, GDT)
929          */
930         [DTO_ATTR_SET_CHOWN]= 3
931 };
932
933 /**
934  * Note: we count into QUOTA here.
935  * If we mount with --data_journal we may need more.
936  */
937 static const int osd_dto_credits_quota[DTO_NR] = {
938         /**
939          * INDEX_EXTRA_TRANS_BLOCKS(8) +
940          * SINGLEDATA_TRANS_BLOCKS(8) +
941          * 2 * QUOTA_TRANS_BLOCKS(2)
942          */
943         [DTO_INDEX_INSERT]  = 20,
944         /**
945          * INDEX_EXTRA_TRANS_BLOCKS(8) +
946          * SINGLEDATA_TRANS_BLOCKS(8) +
947          * 2 * QUOTA_TRANS_BLOCKS(2)
948          */
949         [DTO_INDEX_DELETE]  = 20,
950         /**
951          * Unused now.
952          */
953         [DTO_IDNEX_UPDATE]  = 16,
954         /*
955          * Create a object. Same as create object in EXT3 filesystem.
956          * DATA_TRANS_BLOCKS(16) +
957          * INDEX_EXTRA_BLOCKS(8) +
958          * 3(inode bits, groups, GDT) +
959          * 2 * QUOTA_INIT_BLOCKS(25)
960          */
961         [DTO_OBJECT_CREATE] = 77,
962         /*
963          * Unused now.
964          * DATA_TRANS_BLOCKS(16) +
965          * INDEX_EXTRA_BLOCKS(8) +
966          * 3(inode bits, groups, GDT) +
967          * QUOTA(?)
968          */
969         [DTO_OBJECT_DELETE] = 27,
970         /**
971          * Attr set credits.
972          * 3 (inode bit, group, GDT) +
973          */
974         [DTO_ATTR_SET_BASE] = 3,
975         /**
976          * Xattr set. The same as xattr of EXT3.
977          * DATA_TRANS_BLOCKS(16)
978          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
979          *           also counted in. Do not know why?
980          */
981         [DTO_XATTR_SET]     = 16,
982         [DTO_LOG_REC]       = 16,
983         /**
984          * creadits for inode change during write.
985          */
986         [DTO_WRITE_BASE]    = 3,
987         /**
988          * credits for single block write.
989          */
990         [DTO_WRITE_BLOCK]   = 16,
991         /**
992          * Attr set credits for chown.
993          * 3 (inode bit, group, GDT) +
994          * 2 * QUOTA_INIT_BLOCKS(25) +
995          * 2 * QUOTA_DEL_BLOCKS(9)
996          */
997         [DTO_ATTR_SET_CHOWN]= 71
998 };
999
1000 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1001                           enum dt_txn_op op)
1002 {
1003         LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1004                 ARRAY_SIZE(osd_dto_credits_quota));
1005         LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1006 #ifdef HAVE_QUOTA_SUPPORT
1007         if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1008                 return osd_dto_credits_quota[op];
1009         else
1010 #endif
1011                 return osd_dto_credits_noquota[op];
1012 }
1013
1014 static const struct dt_device_operations osd_dt_ops = {
1015         .dt_root_get       = osd_root_get,
1016         .dt_statfs         = osd_statfs,
1017         .dt_trans_start    = osd_trans_start,
1018         .dt_trans_stop     = osd_trans_stop,
1019         .dt_conf_get       = osd_conf_get,
1020         .dt_sync           = osd_sync,
1021         .dt_ro             = osd_ro,
1022         .dt_commit_async   = osd_commit_async,
1023         .dt_credit_get     = osd_credit_get,
1024         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1025         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1026 };
1027
1028 static void osd_object_read_lock(const struct lu_env *env,
1029                                  struct dt_object *dt, unsigned role)
1030 {
1031         struct osd_object *obj = osd_dt_obj(dt);
1032         struct osd_thread_info *oti = osd_oti_get(env);
1033
1034         LINVRNT(osd_invariant(obj));
1035
1036         LASSERT(obj->oo_owner != env);
1037         down_read_nested(&obj->oo_sem, role);
1038
1039         LASSERT(obj->oo_owner == NULL);
1040         oti->oti_r_locks++;
1041 }
1042
1043 static void osd_object_write_lock(const struct lu_env *env,
1044                                   struct dt_object *dt, unsigned role)
1045 {
1046         struct osd_object *obj = osd_dt_obj(dt);
1047         struct osd_thread_info *oti = osd_oti_get(env);
1048
1049         LINVRNT(osd_invariant(obj));
1050
1051         LASSERT(obj->oo_owner != env);
1052         down_write_nested(&obj->oo_sem, role);
1053
1054         LASSERT(obj->oo_owner == NULL);
1055         obj->oo_owner = env;
1056         oti->oti_w_locks++;
1057 }
1058
1059 static void osd_object_read_unlock(const struct lu_env *env,
1060                                    struct dt_object *dt)
1061 {
1062         struct osd_object *obj = osd_dt_obj(dt);
1063         struct osd_thread_info *oti = osd_oti_get(env);
1064
1065         LINVRNT(osd_invariant(obj));
1066
1067         LASSERT(oti->oti_r_locks > 0);
1068         oti->oti_r_locks--;
1069         up_read(&obj->oo_sem);
1070 }
1071
1072 static void osd_object_write_unlock(const struct lu_env *env,
1073                                     struct dt_object *dt)
1074 {
1075         struct osd_object *obj = osd_dt_obj(dt);
1076         struct osd_thread_info *oti = osd_oti_get(env);
1077
1078         LINVRNT(osd_invariant(obj));
1079
1080         LASSERT(obj->oo_owner == env);
1081         LASSERT(oti->oti_w_locks > 0);
1082         oti->oti_w_locks--;
1083         obj->oo_owner = NULL;
1084         up_write(&obj->oo_sem);
1085 }
1086
1087 static int osd_object_write_locked(const struct lu_env *env,
1088                                    struct dt_object *dt)
1089 {
1090         struct osd_object *obj = osd_dt_obj(dt);
1091
1092         LINVRNT(osd_invariant(obj));
1093
1094         return obj->oo_owner == env;
1095 }
1096
1097 static int capa_is_sane(const struct lu_env *env,
1098                         struct osd_device *dev,
1099                         struct lustre_capa *capa,
1100                         struct lustre_capa_key *keys)
1101 {
1102         struct osd_thread_info *oti = osd_oti_get(env);
1103         struct lustre_capa *tcapa = &oti->oti_capa;
1104         struct obd_capa *oc;
1105         int i, rc = 0;
1106         ENTRY;
1107
1108         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1109         if (oc) {
1110                 if (capa_is_expired(oc)) {
1111                         DEBUG_CAPA(D_ERROR, capa, "expired");
1112                         rc = -ESTALE;
1113                 }
1114                 capa_put(oc);
1115                 RETURN(rc);
1116         }
1117
1118         if (capa_is_expired_sec(capa)) {
1119                 DEBUG_CAPA(D_ERROR, capa, "expired");
1120                 RETURN(-ESTALE);
1121         }
1122
1123         spin_lock(&capa_lock);
1124         for (i = 0; i < 2; i++) {
1125                 if (keys[i].lk_keyid == capa->lc_keyid) {
1126                         oti->oti_capa_key = keys[i];
1127                         break;
1128                 }
1129         }
1130         spin_unlock(&capa_lock);
1131
1132         if (i == 2) {
1133                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1134                 RETURN(-ESTALE);
1135         }
1136
1137         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1138         if (rc)
1139                 RETURN(rc);
1140
1141         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1142                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1143                 RETURN(-EACCES);
1144         }
1145
1146         oc = capa_add(dev->od_capa_hash, capa);
1147         capa_put(oc);
1148
1149         RETURN(0);
1150 }
1151
1152 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1153                            struct lustre_capa *capa, __u64 opc)
1154 {
1155         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1156         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1157         struct md_capainfo *ci;
1158         int rc;
1159
1160         if (!dev->od_fl_capa)
1161                 return 0;
1162
1163         if (capa == BYPASS_CAPA)
1164                 return 0;
1165
1166         ci = md_capainfo(env);
1167         if (unlikely(!ci))
1168                 return 0;
1169
1170         if (ci->mc_auth == LC_ID_NONE)
1171                 return 0;
1172
1173         if (!capa) {
1174                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1175                 return -EACCES;
1176         }
1177
1178         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1179                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1180                            PFID(fid));
1181                 return -EACCES;
1182         }
1183
1184         if (!capa_opc_supported(capa, opc)) {
1185                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1186                 return -EACCES;
1187         }
1188
1189         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1190                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1191                 return -EACCES;
1192         }
1193
1194         return 0;
1195 }
1196
1197 static struct timespec *osd_inode_time(const struct lu_env *env,
1198                                        struct inode *inode, __u64 seconds)
1199 {
1200         struct osd_thread_info *oti = osd_oti_get(env);
1201         struct timespec        *t   = &oti->oti_time;
1202
1203         t->tv_sec  = seconds;
1204         t->tv_nsec = 0;
1205         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1206         return t;
1207 }
1208
1209
1210 static void osd_inode_getattr(const struct lu_env *env,
1211                               struct inode *inode, struct lu_attr *attr)
1212 {
1213         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1214                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1215                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1216
1217         attr->la_atime      = LTIME_S(inode->i_atime);
1218         attr->la_mtime      = LTIME_S(inode->i_mtime);
1219         attr->la_ctime      = LTIME_S(inode->i_ctime);
1220         attr->la_mode       = inode->i_mode;
1221         attr->la_size       = i_size_read(inode);
1222         attr->la_blocks     = inode->i_blocks;
1223         attr->la_uid        = inode->i_uid;
1224         attr->la_gid        = inode->i_gid;
1225         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1226         attr->la_nlink      = inode->i_nlink;
1227         attr->la_rdev       = inode->i_rdev;
1228         attr->la_blksize    = ll_inode_blksize(inode);
1229         attr->la_blkbits    = inode->i_blkbits;
1230 }
1231
1232 static int osd_attr_get(const struct lu_env *env,
1233                         struct dt_object *dt,
1234                         struct lu_attr *attr,
1235                         struct lustre_capa *capa)
1236 {
1237         struct osd_object *obj = osd_dt_obj(dt);
1238
1239         LASSERT(dt_object_exists(dt));
1240         LINVRNT(osd_invariant(obj));
1241
1242         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1243                 return -EACCES;
1244
1245         spin_lock(&obj->oo_guard);
1246         osd_inode_getattr(env, obj->oo_inode, attr);
1247         spin_unlock(&obj->oo_guard);
1248         return 0;
1249 }
1250
1251 static int osd_inode_setattr(const struct lu_env *env,
1252                              struct inode *inode, const struct lu_attr *attr)
1253 {
1254         __u64 bits;
1255
1256         bits = attr->la_valid;
1257
1258         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1259
1260 #ifdef HAVE_QUOTA_SUPPORT
1261         if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1262             (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1263                 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1264                 struct iattr iattr;
1265                 int rc;
1266
1267                 iattr.ia_valid = 0;
1268                 if (bits & LA_UID)
1269                         iattr.ia_valid |= ATTR_UID;
1270                 if (bits & LA_GID)
1271                         iattr.ia_valid |= ATTR_GID;
1272                 iattr.ia_uid = attr->la_uid;
1273                 iattr.ia_gid = attr->la_gid;
1274                 osd_push_ctxt(env, save);
1275                 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1276                 osd_pop_ctxt(save);
1277                 if (rc != 0)
1278                         return rc;
1279         }
1280 #endif
1281
1282         if (bits & LA_ATIME)
1283                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1284         if (bits & LA_CTIME)
1285                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1286         if (bits & LA_MTIME)
1287                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1288         if (bits & LA_SIZE) {
1289                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1290                 i_size_write(inode, attr->la_size);
1291         }
1292
1293         /* OSD should not change "i_blocks" which is used by quota.
1294          * "i_blocks" should be changed by ldiskfs only.
1295          * Enable this assignment for SOM purpose now, until it is
1296          * stored in SOM EA. */
1297         if (bits & LA_BLOCKS)
1298                 inode->i_blocks = attr->la_blocks;
1299
1300         if (bits & LA_MODE)
1301                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1302                         (attr->la_mode & ~S_IFMT);
1303         if (bits & LA_UID)
1304                 inode->i_uid    = attr->la_uid;
1305         if (bits & LA_GID)
1306                 inode->i_gid    = attr->la_gid;
1307         if (bits & LA_NLINK)
1308                 inode->i_nlink  = attr->la_nlink;
1309         if (bits & LA_RDEV)
1310                 inode->i_rdev   = attr->la_rdev;
1311
1312         if (bits & LA_FLAGS) {
1313                 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1314
1315                 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1316                         (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1317         }
1318         return 0;
1319 }
1320
1321 static int osd_attr_set(const struct lu_env *env,
1322                         struct dt_object *dt,
1323                         const struct lu_attr *attr,
1324                         struct thandle *handle,
1325                         struct lustre_capa *capa)
1326 {
1327         struct osd_object *obj = osd_dt_obj(dt);
1328         int rc;
1329
1330         LASSERT(handle != NULL);
1331         LASSERT(dt_object_exists(dt));
1332         LASSERT(osd_invariant(obj));
1333
1334         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1335                 return -EACCES;
1336
1337         spin_lock(&obj->oo_guard);
1338         rc = osd_inode_setattr(env, obj->oo_inode, attr);
1339         spin_unlock(&obj->oo_guard);
1340
1341         if (!rc)
1342                 mark_inode_dirty(obj->oo_inode);
1343         return rc;
1344 }
1345
1346 /*
1347  * Object creation.
1348  *
1349  * XXX temporary solution.
1350  */
1351 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1352                           struct lu_attr *attr, struct thandle *th)
1353 {
1354         return 0;
1355 }
1356
1357 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1358                            struct lu_attr *attr, struct thandle *th)
1359 {
1360         osd_object_init0(obj);
1361         return 0;
1362 }
1363
1364 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1365                                           struct inode * dir, int mode);
1366 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1367                              struct inode *inode);
1368 extern int ldiskfs_delete_entry(handle_t *handle,
1369                                 struct inode * dir,
1370                                 struct ldiskfs_dir_entry_2 * de_del,
1371                                 struct buffer_head * bh);
1372 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1373                                                struct ldiskfs_dir_entry_2
1374                                                ** res_dir);
1375 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1376                                   struct inode *inode);
1377
1378 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1379                                     int name_index, const char *name,
1380                                     const void *value, size_t value_len,
1381                                     int flags);
1382
1383 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1384                                             struct osd_object *obj,
1385                                             const char *name,
1386                                             const int namelen)
1387 {
1388         struct osd_thread_info *info   = osd_oti_get(env);
1389         struct dentry *child_dentry = &info->oti_child_dentry;
1390         struct dentry *obj_dentry = &info->oti_obj_dentry;
1391
1392         obj_dentry->d_inode = obj->oo_inode;
1393         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1394         obj_dentry->d_name.hash = 0;
1395
1396         child_dentry->d_name.hash = 0;
1397         child_dentry->d_parent = obj_dentry;
1398         child_dentry->d_name.name = name;
1399         child_dentry->d_name.len = namelen;
1400         return child_dentry;
1401 }
1402
1403
1404 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1405                       umode_t mode,
1406                       struct dt_allocation_hint *hint,
1407                       struct thandle *th)
1408 {
1409         int result;
1410         struct osd_device  *osd = osd_obj2dev(obj);
1411         struct osd_thandle *oth;
1412         struct dt_object   *parent;
1413         struct inode       *inode;
1414 #ifdef HAVE_QUOTA_SUPPORT
1415         struct osd_ctxt    *save = &info->oti_ctxt;
1416 #endif
1417
1418         LINVRNT(osd_invariant(obj));
1419         LASSERT(obj->oo_inode == NULL);
1420
1421         oth = container_of(th, struct osd_thandle, ot_super);
1422         LASSERT(oth->ot_handle->h_transaction != NULL);
1423
1424         if (hint && hint->dah_parent)
1425                 parent = hint->dah_parent;
1426         else
1427                 parent = osd->od_obj_area;
1428
1429         LASSERT(parent != NULL);
1430         LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1431
1432 #ifdef HAVE_QUOTA_SUPPORT
1433         osd_push_ctxt(info->oti_env, save);
1434 #endif
1435         inode = ldiskfs_create_inode(oth->ot_handle,
1436                                      osd_dt_obj(parent)->oo_inode, mode);
1437 #ifdef HAVE_QUOTA_SUPPORT
1438         osd_pop_ctxt(save);
1439 #endif
1440         if (!IS_ERR(inode)) {
1441                 obj->oo_inode = inode;
1442                 result = 0;
1443         } else
1444                 result = PTR_ERR(inode);
1445         LINVRNT(osd_invariant(obj));
1446         return result;
1447 }
1448
1449
1450 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1451                            int recsize, handle_t *handle);
1452
1453 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1454                            int recsize, handle_t *handle);
1455
1456 enum {
1457         OSD_NAME_LEN = 255
1458 };
1459
1460 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1461                      struct lu_attr *attr,
1462                      struct dt_allocation_hint *hint,
1463                      struct dt_object_format *dof,
1464                      struct thandle *th)
1465 {
1466         int result;
1467         struct osd_thandle *oth;
1468         struct osd_device *osd = osd_obj2dev(obj);
1469         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1470
1471         LASSERT(S_ISDIR(attr->la_mode));
1472
1473         oth = container_of(th, struct osd_thandle, ot_super);
1474         LASSERT(oth->ot_handle->h_transaction != NULL);
1475         result = osd_mkfile(info, obj, mode, hint, th);
1476         if (result == 0 && osd->od_iop_mode == 0) {
1477                 LASSERT(obj->oo_inode != NULL);
1478                 /*
1479                  * XXX uh-oh... call low-level iam function directly.
1480                  */
1481
1482                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1483                                          sizeof (struct lu_fid_pack),
1484                                          oth->ot_handle);
1485         }
1486         return result;
1487 }
1488
1489 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1490                         struct lu_attr *attr,
1491                         struct dt_allocation_hint *hint,
1492                         struct dt_object_format *dof,
1493                         struct thandle *th)
1494 {
1495         int result;
1496         struct osd_thandle *oth;
1497         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1498
1499         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1500
1501         LASSERT(S_ISREG(attr->la_mode));
1502
1503         oth = container_of(th, struct osd_thandle, ot_super);
1504         LASSERT(oth->ot_handle->h_transaction != NULL);
1505
1506         result = osd_mkfile(info, obj, mode, hint, th);
1507         if (result == 0) {
1508                 LASSERT(obj->oo_inode != NULL);
1509                 if (feat->dif_flags & DT_IND_VARKEY)
1510                         result = iam_lvar_create(obj->oo_inode,
1511                                                  feat->dif_keysize_max,
1512                                                  feat->dif_ptrsize,
1513                                                  feat->dif_recsize_max,
1514                                                  oth->ot_handle);
1515                 else
1516                         result = iam_lfix_create(obj->oo_inode,
1517                                                  feat->dif_keysize_max,
1518                                                  feat->dif_ptrsize,
1519                                                  feat->dif_recsize_max,
1520                                                  oth->ot_handle);
1521
1522         }
1523         return result;
1524 }
1525
1526 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1527                      struct lu_attr *attr,
1528                      struct dt_allocation_hint *hint,
1529                      struct dt_object_format *dof,
1530                      struct thandle *th)
1531 {
1532         LASSERT(S_ISREG(attr->la_mode));
1533         return osd_mkfile(info, obj, (attr->la_mode &
1534                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1535 }
1536
1537 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1538                      struct lu_attr *attr,
1539                      struct dt_allocation_hint *hint,
1540                      struct dt_object_format *dof,
1541                      struct thandle *th)
1542 {
1543         LASSERT(S_ISLNK(attr->la_mode));
1544         return osd_mkfile(info, obj, (attr->la_mode &
1545                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1546 }
1547
1548 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1549                      struct lu_attr *attr,
1550                      struct dt_allocation_hint *hint,
1551                      struct dt_object_format *dof,
1552                      struct thandle *th)
1553 {
1554         umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1555         int result;
1556
1557         LINVRNT(osd_invariant(obj));
1558         LASSERT(obj->oo_inode == NULL);
1559         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1560                 S_ISFIFO(mode) || S_ISSOCK(mode));
1561
1562         result = osd_mkfile(info, obj, mode, hint, th);
1563         if (result == 0) {
1564                 LASSERT(obj->oo_inode != NULL);
1565                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1566         }
1567         LINVRNT(osd_invariant(obj));
1568         return result;
1569 }
1570
1571 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1572                               struct lu_attr *,
1573                               struct dt_allocation_hint *hint,
1574                               struct dt_object_format *dof,
1575                               struct thandle *);
1576
1577 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1578 {
1579         osd_obj_type_f result;
1580
1581         switch (type) {
1582         case DFT_DIR:
1583                 result = osd_mkdir;
1584                 break;
1585         case DFT_REGULAR:
1586                 result = osd_mkreg;
1587                 break;
1588         case DFT_SYM:
1589                 result = osd_mksym;
1590                 break;
1591         case DFT_NODE:
1592                 result = osd_mknod;
1593                 break;
1594         case DFT_INDEX:
1595                 result = osd_mk_index;
1596                 break;
1597
1598         default:
1599                 LBUG();
1600                 break;
1601         }
1602         return result;
1603 }
1604
1605
1606 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1607                         struct dt_object *parent, umode_t child_mode)
1608 {
1609         LASSERT(ah);
1610
1611         memset(ah, 0, sizeof(*ah));
1612         ah->dah_parent = parent;
1613         ah->dah_mode = child_mode;
1614 }
1615
1616 /**
1617  * Helper function for osd_object_create()
1618  *
1619  * \retval 0, on success
1620  */
1621 static int __osd_object_create(struct osd_thread_info *info,
1622                                struct osd_object *obj, struct lu_attr *attr,
1623                                struct dt_allocation_hint *hint,
1624                                struct dt_object_format *dof,
1625                                struct thandle *th)
1626 {
1627
1628         int result;
1629
1630         result = osd_create_pre(info, obj, attr, th);
1631         if (result == 0) {
1632                 result = osd_create_type_f(dof->dof_type)(info, obj,
1633                                            attr, hint, dof, th);
1634                 if (result == 0)
1635                         result = osd_create_post(info, obj, attr, th);
1636         }
1637         return result;
1638 }
1639
1640 /**
1641  * Helper function for osd_object_create()
1642  *
1643  * \retval 0, on success
1644  */
1645 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1646                            const struct lu_fid *fid, struct thandle *th)
1647 {
1648         struct osd_thread_info *info = osd_oti_get(env);
1649         struct osd_inode_id    *id   = &info->oti_id;
1650         struct osd_device      *osd  = osd_obj2dev(obj);
1651         struct md_ucred        *uc   = md_ucred(env);
1652
1653         LASSERT(obj->oo_inode != NULL);
1654         LASSERT(uc != NULL);
1655
1656         id->oii_ino = obj->oo_inode->i_ino;
1657         id->oii_gen = obj->oo_inode->i_generation;
1658
1659         return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1660                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1661 }
1662
1663 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1664                              struct lu_attr *attr,
1665                              struct dt_allocation_hint *hint,
1666                              struct dt_object_format *dof,
1667                              struct thandle *th)
1668 {
1669         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1670         struct osd_object      *obj    = osd_dt_obj(dt);
1671         struct osd_thread_info *info   = osd_oti_get(env);
1672         int result;
1673
1674         ENTRY;
1675
1676         LINVRNT(osd_invariant(obj));
1677         LASSERT(!dt_object_exists(dt));
1678         LASSERT(osd_write_locked(env, obj));
1679         LASSERT(th != NULL);
1680
1681         result = __osd_object_create(info, obj, attr, hint, dof, th);
1682         if (result == 0)
1683                 result = __osd_oi_insert(env, obj, fid, th);
1684
1685         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1686         LASSERT(osd_invariant(obj));
1687         RETURN(result);
1688 }
1689
1690 /**
1691  * Helper function for osd_xattr_set()
1692  */
1693 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1694                            const struct lu_buf *buf, const char *name, int fl)
1695 {
1696         struct osd_object      *obj      = osd_dt_obj(dt);
1697         struct inode           *inode    = obj->oo_inode;
1698         struct osd_thread_info *info     = osd_oti_get(env);
1699         struct dentry          *dentry   = &info->oti_child_dentry;
1700         struct timespec        *t        = &info->oti_time;
1701         int                     fs_flags = 0;
1702         int  rc;
1703
1704         LASSERT(dt_object_exists(dt));
1705         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1706         LASSERT(osd_write_locked(env, obj));
1707
1708         if (fl & LU_XATTR_REPLACE)
1709                 fs_flags |= XATTR_REPLACE;
1710
1711         if (fl & LU_XATTR_CREATE)
1712                 fs_flags |= XATTR_CREATE;
1713
1714         dentry->d_inode = inode;
1715         *t = inode->i_ctime;
1716         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1717                                    buf->lb_len, fs_flags);
1718         /* ctime should not be updated with server-side time. */
1719         spin_lock(&obj->oo_guard);
1720         inode->i_ctime = *t;
1721         spin_unlock(&obj->oo_guard);
1722         mark_inode_dirty(inode);
1723         return rc;
1724 }
1725
1726 /**
1727  * Put the fid into lustre_mdt_attrs, and then place the structure
1728  * inode's ea. This fid should not be altered during the life time
1729  * of the inode.
1730  *
1731  * \retval +ve, on success
1732  * \retval -ve, on error
1733  *
1734  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1735  */
1736 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1737                           const struct lu_fid *fid)
1738 {
1739         struct osd_thread_info  *info      = osd_oti_get(env);
1740         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1741
1742         fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1743
1744         return __osd_xattr_set(env, dt,
1745                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1746                                XATTR_NAME_LMA, LU_XATTR_CREATE);
1747
1748 }
1749
1750 /**
1751  * Helper function to form igif
1752  */
1753 static inline void osd_igif_get(const struct lu_env *env, struct inode  *inode,
1754                                 struct lu_fid *fid)
1755 {
1756         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1757 }
1758
1759 /**
1760  * Helper function to pack the fid
1761  */
1762 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1763                                 struct dt_rec *pack)
1764 {
1765         fid_pack((struct lu_fid_pack *)pack, fid, &osd_oti_get(env)->oti_fid);
1766 }
1767
1768 /**
1769  * Try to read the fid from inode ea into dt_rec, if return value
1770  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1771  *
1772  * \param fid, object fid.
1773  *
1774  * \retval 0, on success
1775  */
1776 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1777                           __u32 ino, struct lu_fid *fid)
1778 {
1779         struct osd_thread_info  *info      = osd_oti_get(env);
1780         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1781         struct lu_device        *ldev   = obj->oo_dt.do_lu.lo_dev;
1782         struct dentry           *dentry = &info->oti_child_dentry;
1783         struct osd_inode_id     *id     = &info->oti_id;
1784         struct osd_device       *dev;
1785         struct inode            *inode;
1786         int                      rc;
1787
1788         ENTRY;
1789         dev  = osd_dev(ldev);
1790
1791         id->oii_ino = ino;
1792         id->oii_gen = OSD_OII_NOGEN;
1793
1794         inode = osd_iget(info, dev, id);
1795         if (IS_ERR(inode)) {
1796                 rc = PTR_ERR(inode);
1797                 GOTO(out,rc);
1798         }
1799         dentry->d_inode = inode;
1800
1801         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1802         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1803                                    sizeof *mdt_attrs);
1804
1805         /* Check LMA compatibility */
1806         if (rc > 0 &&
1807             (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1808                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1809                       inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1810                       ~LMA_INCOMPAT_SUPP);
1811                 return -ENOSYS;
1812         }
1813
1814         if (rc > 0) {
1815                 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1816                 rc = 0;
1817         } else if (rc == -ENODATA) {
1818                 osd_igif_get(env, inode, fid);
1819                 rc = 0;
1820         }
1821         iput(inode);
1822
1823 out:
1824         RETURN(rc);
1825 }
1826
1827 /**
1828  * OSD layer object create function for interoperability mode (b11826).
1829  * This is mostly similar to osd_object_create(). Only difference being, fid is
1830  * inserted into inode ea here.
1831  *
1832  * \retval   0, on success
1833  * \retval -ve, on error
1834  */
1835 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1836                              struct lu_attr *attr,
1837                              struct dt_allocation_hint *hint,
1838                              struct dt_object_format *dof,
1839                              struct thandle *th)
1840 {
1841         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1842         struct osd_object      *obj    = osd_dt_obj(dt);
1843         struct osd_thread_info *info   = osd_oti_get(env);
1844         int result;
1845         int is_root = 0;
1846
1847         ENTRY;
1848
1849         LASSERT(osd_invariant(obj));
1850         LASSERT(!dt_object_exists(dt));
1851         LASSERT(osd_write_locked(env, obj));
1852         LASSERT(th != NULL);
1853
1854         result = __osd_object_create(info, obj, attr, hint, dof, th);
1855
1856         if (hint && hint->dah_parent)
1857                 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1858
1859         /* objects under osd root shld have igif fid, so dont add fid EA */
1860         if (result == 0 && is_root == 0)
1861                 result = osd_ea_fid_set(env, dt, fid);
1862
1863         if (result == 0)
1864                 result = __osd_oi_insert(env, obj, fid, th);
1865
1866         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1867         LINVRNT(osd_invariant(obj));
1868         RETURN(result);
1869 }
1870
1871 /*
1872  * Concurrency: @dt is write locked.
1873  */
1874 static void osd_object_ref_add(const struct lu_env *env,
1875                                struct dt_object *dt,
1876                                struct thandle *th)
1877 {
1878         struct osd_object *obj = osd_dt_obj(dt);
1879         struct inode *inode = obj->oo_inode;
1880
1881         LINVRNT(osd_invariant(obj));
1882         LASSERT(dt_object_exists(dt));
1883         LASSERT(osd_write_locked(env, obj));
1884         LASSERT(th != NULL);
1885
1886         spin_lock(&obj->oo_guard);
1887         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1888         inode->i_nlink++;
1889         spin_unlock(&obj->oo_guard);
1890         mark_inode_dirty(inode);
1891         LINVRNT(osd_invariant(obj));
1892 }
1893
1894 /*
1895  * Concurrency: @dt is write locked.
1896  */
1897 static void osd_object_ref_del(const struct lu_env *env,
1898                                struct dt_object *dt,
1899                                struct thandle *th)
1900 {
1901         struct osd_object *obj = osd_dt_obj(dt);
1902         struct inode *inode = obj->oo_inode;
1903
1904         LINVRNT(osd_invariant(obj));
1905         LASSERT(dt_object_exists(dt));
1906         LASSERT(osd_write_locked(env, obj));
1907         LASSERT(th != NULL);
1908
1909         spin_lock(&obj->oo_guard);
1910         LASSERT(inode->i_nlink > 0);
1911         inode->i_nlink--;
1912         spin_unlock(&obj->oo_guard);
1913         mark_inode_dirty(inode);
1914         LINVRNT(osd_invariant(obj));
1915 }
1916
1917 /*
1918  * Concurrency: @dt is read locked.
1919  */
1920 static int osd_xattr_get(const struct lu_env *env,
1921                          struct dt_object *dt,
1922                          struct lu_buf *buf,
1923                          const char *name,
1924                          struct lustre_capa *capa)
1925 {
1926         struct osd_object      *obj    = osd_dt_obj(dt);
1927         struct inode           *inode  = obj->oo_inode;
1928         struct osd_thread_info *info   = osd_oti_get(env);
1929         struct dentry          *dentry = &info->oti_obj_dentry;
1930
1931         LASSERT(dt_object_exists(dt));
1932         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1933         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1934
1935         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1936                 return -EACCES;
1937
1938         dentry->d_inode = inode;
1939         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1940 }
1941
1942 /*
1943  * Concurrency: @dt is write locked.
1944  */
1945 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1946                          const struct lu_buf *buf, const char *name, int fl,
1947                          struct thandle *handle, struct lustre_capa *capa)
1948 {
1949         LASSERT(handle != NULL);
1950
1951         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1952                 return -EACCES;
1953
1954         return __osd_xattr_set(env, dt, buf, name, fl);
1955 }
1956
1957 /*
1958  * Concurrency: @dt is read locked.
1959  */
1960 static int osd_xattr_list(const struct lu_env *env,
1961                           struct dt_object *dt,
1962                           struct lu_buf *buf,
1963                           struct lustre_capa *capa)
1964 {
1965         struct osd_object      *obj    = osd_dt_obj(dt);
1966         struct inode           *inode  = obj->oo_inode;
1967         struct osd_thread_info *info   = osd_oti_get(env);
1968         struct dentry          *dentry = &info->oti_obj_dentry;
1969
1970         LASSERT(dt_object_exists(dt));
1971         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1972         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1973
1974         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1975                 return -EACCES;
1976
1977         dentry->d_inode = inode;
1978         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1979 }
1980
1981 /*
1982  * Concurrency: @dt is write locked.
1983  */
1984 static int osd_xattr_del(const struct lu_env *env,
1985                          struct dt_object *dt,
1986                          const char *name,
1987                          struct thandle *handle,
1988                          struct lustre_capa *capa)
1989 {
1990         struct osd_object      *obj    = osd_dt_obj(dt);
1991         struct inode           *inode  = obj->oo_inode;
1992         struct osd_thread_info *info   = osd_oti_get(env);
1993         struct dentry          *dentry = &info->oti_obj_dentry;
1994         struct timespec        *t      = &info->oti_time;
1995         int                     rc;
1996
1997         LASSERT(dt_object_exists(dt));
1998         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1999         LASSERT(osd_write_locked(env, obj));
2000         LASSERT(handle != NULL);
2001
2002         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2003                 return -EACCES;
2004
2005         dentry->d_inode = inode;
2006         *t = inode->i_ctime;
2007         rc = inode->i_op->removexattr(dentry, name);
2008         /* ctime should not be updated with server-side time. */
2009         spin_lock(&obj->oo_guard);
2010         inode->i_ctime = *t;
2011         spin_unlock(&obj->oo_guard);
2012         mark_inode_dirty(inode);
2013         return rc;
2014 }
2015
2016 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2017                                      struct dt_object *dt,
2018                                      struct lustre_capa *old,
2019                                      __u64 opc)
2020 {
2021         struct osd_thread_info *info = osd_oti_get(env);
2022         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2023         struct osd_object *obj = osd_dt_obj(dt);
2024         struct osd_device *dev = osd_obj2dev(obj);
2025         struct lustre_capa_key *key = &info->oti_capa_key;
2026         struct lustre_capa *capa = &info->oti_capa;
2027         struct obd_capa *oc;
2028         struct md_capainfo *ci;
2029         int rc;
2030         ENTRY;
2031
2032         if (!dev->od_fl_capa)
2033                 RETURN(ERR_PTR(-ENOENT));
2034
2035         LASSERT(dt_object_exists(dt));
2036         LINVRNT(osd_invariant(obj));
2037
2038         /* renewal sanity check */
2039         if (old && osd_object_auth(env, dt, old, opc))
2040                 RETURN(ERR_PTR(-EACCES));
2041
2042         ci = md_capainfo(env);
2043         if (unlikely(!ci))
2044                 RETURN(ERR_PTR(-ENOENT));
2045
2046         switch (ci->mc_auth) {
2047         case LC_ID_NONE:
2048                 RETURN(NULL);
2049         case LC_ID_PLAIN:
2050                 capa->lc_uid = obj->oo_inode->i_uid;
2051                 capa->lc_gid = obj->oo_inode->i_gid;
2052                 capa->lc_flags = LC_ID_PLAIN;
2053                 break;
2054         case LC_ID_CONVERT: {
2055                 __u32 d[4], s[4];
2056
2057                 s[0] = obj->oo_inode->i_uid;
2058                 get_random_bytes(&(s[1]), sizeof(__u32));
2059                 s[2] = obj->oo_inode->i_gid;
2060                 get_random_bytes(&(s[3]), sizeof(__u32));
2061                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2062                 if (unlikely(rc))
2063                         RETURN(ERR_PTR(rc));
2064
2065                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2066                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2067                 capa->lc_flags = LC_ID_CONVERT;
2068                 break;
2069         }
2070         default:
2071                 RETURN(ERR_PTR(-EINVAL));
2072         }
2073
2074         capa->lc_fid = *fid;
2075         capa->lc_opc = opc;
2076         capa->lc_flags |= dev->od_capa_alg << 24;
2077         capa->lc_timeout = dev->od_capa_timeout;
2078         capa->lc_expiry = 0;
2079
2080         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2081         if (oc) {
2082                 LASSERT(!capa_is_expired(oc));
2083                 RETURN(oc);
2084         }
2085
2086         spin_lock(&capa_lock);
2087         *key = dev->od_capa_keys[1];
2088         spin_unlock(&capa_lock);
2089
2090         capa->lc_keyid = key->lk_keyid;
2091         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2092
2093         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2094         if (rc) {
2095                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2096                 RETURN(ERR_PTR(rc));
2097         }
2098
2099         oc = capa_add(dev->od_capa_hash, capa);
2100         RETURN(oc);
2101 }
2102
2103 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2104 {
2105         int rc;
2106         struct osd_object      *obj    = osd_dt_obj(dt);
2107         struct inode           *inode  = obj->oo_inode;
2108         struct osd_thread_info *info   = osd_oti_get(env);
2109         struct dentry          *dentry = &info->oti_obj_dentry;
2110         struct file            *file   = &info->oti_file;
2111         ENTRY;
2112
2113         dentry->d_inode = inode;
2114         file->f_dentry = dentry;
2115         file->f_mapping = inode->i_mapping;
2116         file->f_op = inode->i_fop;
2117         LOCK_INODE_MUTEX(inode);
2118         rc = file->f_op->fsync(file, dentry, 0);
2119         UNLOCK_INODE_MUTEX(inode);
2120         RETURN(rc);
2121 }
2122
2123 /*
2124  * Get the 64-bit version for an inode.
2125  */
2126 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2127                                                struct dt_object *dt)
2128 {
2129         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2130
2131         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2132                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2133         return LDISKFS_I(inode)->i_fs_version;
2134 }
2135
2136 /*
2137  * Set the 64-bit version and return the old version.
2138  */
2139 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2140                                    dt_obj_version_t new_version)
2141 {
2142         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2143
2144         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2145                new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2146         LDISKFS_I(inode)->i_fs_version = new_version;
2147         /** Version is set after all inode operations are finished,
2148          *  so we should mark it dirty here */
2149         inode->i_sb->s_op->dirty_inode(inode);
2150 }
2151
2152 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2153                         void **data)
2154 {
2155         struct osd_object *obj = osd_dt_obj(dt);
2156         ENTRY;
2157
2158         *data = (void *)obj->oo_inode;
2159         RETURN(0);
2160 }
2161
2162 /*
2163  * Index operations.
2164  */
2165
2166 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2167                            const struct dt_index_features *feat)
2168 {
2169         struct iam_descr *descr;
2170         struct dt_object *dt = &o->oo_dt;
2171
2172         if (osd_object_is_root(o))
2173                 return feat == &dt_directory_features;
2174
2175         LASSERT(o->oo_dir != NULL);
2176
2177         descr = o->oo_dir->od_container.ic_descr;
2178         if (feat == &dt_directory_features) {
2179                 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2180                         return 1;
2181
2182                 if (descr == &iam_htree_compat_param) {
2183                         /* if it is a HTREE dir then there is good chance that,
2184                          * we dealing with ext3 directory here with no FIDs. */
2185
2186                         if (descr->id_rec_size ==
2187                             sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2188
2189                                 dt->do_index_ops = &osd_index_ea_ops;
2190                                 return 1;
2191                         }
2192                 }
2193                 return 0;
2194         } else {
2195                 return
2196                         feat->dif_keysize_min <= descr->id_key_size &&
2197                         descr->id_key_size <= feat->dif_keysize_max &&
2198                         feat->dif_recsize_min <= descr->id_rec_size &&
2199                         descr->id_rec_size <= feat->dif_recsize_max &&
2200                         !(feat->dif_flags & (DT_IND_VARKEY |
2201                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2202                         ergo(feat->dif_flags & DT_IND_UPDATE,
2203                              1 /* XXX check that object (and file system) is
2204                                 * writable */);
2205         }
2206 }
2207
2208 static int osd_iam_container_init(const struct lu_env *env,
2209                                   struct osd_object *obj,
2210                                   struct osd_directory *dir)
2211 {
2212         int result;
2213         struct iam_container *bag;
2214
2215         bag    = &dir->od_container;
2216         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2217         if (result == 0) {
2218                 result = iam_container_setup(bag);
2219                 if (result == 0)
2220                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2221                 else
2222                         iam_container_fini(bag);
2223         }
2224         return result;
2225 }
2226
2227
2228 /*
2229  * Concurrency: no external locking is necessary.
2230  */
2231 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2232                          const struct dt_index_features *feat)
2233 {
2234         int result;
2235         int ea_dir = 0;
2236         struct osd_object *obj = osd_dt_obj(dt);
2237         struct osd_device *osd = osd_obj2dev(obj);
2238
2239         LINVRNT(osd_invariant(obj));
2240         LASSERT(dt_object_exists(dt));
2241
2242         if (osd_object_is_root(obj)) {
2243                 dt->do_index_ops = &osd_index_ea_ops;
2244                 result = 0;
2245         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2246                 dt->do_index_ops = &osd_index_ea_ops;
2247                 if (S_ISDIR(obj->oo_inode->i_mode))
2248                         result = 0;
2249                 else
2250                         result = -ENOTDIR;
2251                 ea_dir = 1;
2252         } else if (!osd_has_index(obj)) {
2253                 struct osd_directory *dir;
2254
2255                 OBD_ALLOC_PTR(dir);
2256                 if (dir != NULL) {
2257
2258                         spin_lock(&obj->oo_guard);
2259                         if (obj->oo_dir == NULL)
2260                                 obj->oo_dir = dir;
2261                         else
2262                                 /*
2263                                  * Concurrent thread allocated container data.
2264                                  */
2265                                 OBD_FREE_PTR(dir);
2266                         spin_unlock(&obj->oo_guard);
2267                         /*
2268                          * Now, that we have container data, serialize its
2269                          * initialization.
2270                          */
2271                         down_write(&obj->oo_ext_idx_sem);
2272                         /*
2273                          * recheck under lock.
2274                          */
2275                         if (!osd_has_index(obj))
2276                                 result = osd_iam_container_init(env, obj, dir);
2277                         else
2278                                 result = 0;
2279                         up_write(&obj->oo_ext_idx_sem);
2280                 } else
2281                         result = -ENOMEM;
2282         } else
2283                 result = 0;
2284
2285         if (result == 0 && ea_dir == 0) {
2286                 if (!osd_iam_index_probe(env, obj, feat))
2287                         result = -ENOTDIR;
2288         }
2289         LINVRNT(osd_invariant(obj));
2290
2291         return result;
2292 }
2293
2294 static const struct dt_object_operations osd_obj_ops = {
2295         .do_read_lock    = osd_object_read_lock,
2296         .do_write_lock   = osd_object_write_lock,
2297         .do_read_unlock  = osd_object_read_unlock,
2298         .do_write_unlock = osd_object_write_unlock,
2299         .do_write_locked = osd_object_write_locked,
2300         .do_attr_get     = osd_attr_get,
2301         .do_attr_set     = osd_attr_set,
2302         .do_ah_init      = osd_ah_init,
2303         .do_create       = osd_object_create,
2304         .do_index_try    = osd_index_try,
2305         .do_ref_add      = osd_object_ref_add,
2306         .do_ref_del      = osd_object_ref_del,
2307         .do_xattr_get    = osd_xattr_get,
2308         .do_xattr_set    = osd_xattr_set,
2309         .do_xattr_del    = osd_xattr_del,
2310         .do_xattr_list   = osd_xattr_list,
2311         .do_capa_get     = osd_capa_get,
2312         .do_object_sync  = osd_object_sync,
2313         .do_version_get  = osd_object_version_get,
2314         .do_version_set  = osd_object_version_set,
2315         .do_data_get     = osd_data_get,
2316 };
2317
2318 /**
2319  * dt_object_operations for interoperability mode
2320  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2321  */
2322 static const struct dt_object_operations osd_obj_ea_ops = {
2323         .do_read_lock    = osd_object_read_lock,
2324         .do_write_lock   = osd_object_write_lock,
2325         .do_read_unlock  = osd_object_read_unlock,
2326         .do_write_unlock = osd_object_write_unlock,
2327         .do_write_locked = osd_object_write_locked,
2328         .do_attr_get     = osd_attr_get,
2329         .do_attr_set     = osd_attr_set,
2330         .do_ah_init      = osd_ah_init,
2331         .do_create       = osd_object_ea_create,
2332         .do_index_try    = osd_index_try,
2333         .do_ref_add      = osd_object_ref_add,
2334         .do_ref_del      = osd_object_ref_del,
2335         .do_xattr_get    = osd_xattr_get,
2336         .do_xattr_set    = osd_xattr_set,
2337         .do_xattr_del    = osd_xattr_del,
2338         .do_xattr_list   = osd_xattr_list,
2339         .do_capa_get     = osd_capa_get,
2340         .do_object_sync  = osd_object_sync,
2341         .do_version_get  = osd_object_version_get,
2342         .do_version_set  = osd_object_version_set,
2343         .do_data_get     = osd_data_get,
2344 };
2345
2346 /*
2347  * Body operations.
2348  */
2349
2350 /*
2351  * XXX: Another layering violation for now.
2352  *
2353  * We don't want to use ->f_op->read methods, because generic file write
2354  *
2355  *         - serializes on ->i_sem, and
2356  *
2357  *         - does a lot of extra work like balance_dirty_pages(),
2358  *
2359  * which doesn't work for globally shared files like /last-received.
2360  */
2361 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2362 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2363                                 loff_t *offs, handle_t *handle);
2364
2365 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2366                         struct lu_buf *buf, loff_t *pos,
2367                         struct lustre_capa *capa)
2368 {
2369         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2370
2371         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2372                 RETURN(-EACCES);
2373
2374         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2375 }
2376
2377 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2378                          const struct lu_buf *buf, loff_t *pos,
2379                          struct thandle *handle, struct lustre_capa *capa,
2380                          int ignore_quota)
2381 {
2382         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
2383         struct osd_thandle *oh;
2384         ssize_t             result;
2385 #ifdef HAVE_QUOTA_SUPPORT
2386         cfs_cap_t           save = current->cap_effective;
2387 #endif
2388
2389         LASSERT(handle != NULL);
2390
2391         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2392                 RETURN(-EACCES);
2393
2394         oh = container_of(handle, struct osd_thandle, ot_super);
2395         LASSERT(oh->ot_handle->h_transaction != NULL);
2396 #ifdef HAVE_QUOTA_SUPPORT
2397         if (ignore_quota)
2398                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2399         else
2400                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2401 #endif
2402         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2403                                              pos, oh->ot_handle);
2404 #ifdef HAVE_QUOTA_SUPPORT
2405         current->cap_effective = save;
2406 #endif
2407         if (result == 0)
2408                 result = buf->lb_len;
2409         return result;
2410 }
2411
2412 static const struct dt_body_operations osd_body_ops = {
2413         .dbo_read  = osd_read,
2414         .dbo_write = osd_write
2415 };
2416
2417
2418 /**
2419  *      delete a (key, value) pair from index \a dt specified by \a key
2420  *
2421  *      \param  dt_object      osd index object
2422  *      \param  key     key for index
2423  *      \param  rec     record reference
2424  *      \param  handle  transaction handler
2425  *
2426  *      \retval  0  success
2427  *      \retval -ve   failure
2428  */
2429
2430 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2431                                 const struct dt_key *key, struct thandle *handle,
2432                                 struct lustre_capa *capa)
2433 {
2434         struct osd_object     *obj = osd_dt_obj(dt);
2435         struct osd_thandle    *oh;
2436         struct iam_path_descr *ipd;
2437         struct iam_container  *bag = &obj->oo_dir->od_container;
2438         int rc;
2439
2440         ENTRY;
2441
2442         LINVRNT(osd_invariant(obj));
2443         LASSERT(dt_object_exists(dt));
2444         LASSERT(bag->ic_object == obj->oo_inode);
2445         LASSERT(handle != NULL);
2446
2447         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2448                 RETURN(-EACCES);
2449
2450         ipd = osd_idx_ipd_get(env, bag);
2451         if (unlikely(ipd == NULL))
2452                 RETURN(-ENOMEM);
2453
2454         oh = container_of0(handle, struct osd_thandle, ot_super);
2455         LASSERT(oh->ot_handle != NULL);
2456         LASSERT(oh->ot_handle->h_transaction != NULL);
2457
2458         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2459         osd_ipd_put(env, bag, ipd);
2460         LINVRNT(osd_invariant(obj));
2461         RETURN(rc);
2462 }
2463
2464 /**
2465  * Index delete function for interoperability mode (b11826).
2466  * It will remove the directory entry added by osd_index_ea_insert().
2467  * This entry is needed to maintain name->fid mapping.
2468  *
2469  * \param key,  key i.e. file entry to be deleted
2470  *
2471  * \retval   0, on success
2472  * \retval -ve, on error
2473  */
2474 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2475                                const struct dt_key *key, struct thandle *handle,
2476                                struct lustre_capa *capa)
2477 {
2478         struct osd_object          *obj    = osd_dt_obj(dt);
2479         struct inode               *dir    = obj->oo_inode;
2480         struct dentry              *dentry;
2481         struct osd_thandle         *oh;
2482         struct ldiskfs_dir_entry_2 *de;
2483         struct buffer_head         *bh;
2484
2485         int rc;
2486
2487         ENTRY;
2488
2489         LINVRNT(osd_invariant(obj));
2490         LASSERT(dt_object_exists(dt));
2491         LASSERT(handle != NULL);
2492
2493         oh = container_of(handle, struct osd_thandle, ot_super);
2494         LASSERT(oh->ot_handle != NULL);
2495         LASSERT(oh->ot_handle->h_transaction != NULL);
2496
2497         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2498                 RETURN(-EACCES);
2499
2500         dentry = osd_child_dentry_get(env, obj,
2501                                       (char *)key, strlen((char *)key));
2502
2503         down_write(&obj->oo_ext_idx_sem);
2504         bh = ldiskfs_find_entry(dentry, &de);
2505         if (bh) {
2506                 struct osd_thread_info *oti = osd_oti_get(env);
2507                 struct timespec *ctime = &oti->oti_time;
2508                 struct timespec *mtime = &oti->oti_time2;
2509
2510                 *ctime = dir->i_ctime;
2511                 *mtime = dir->i_mtime;
2512                 rc = ldiskfs_delete_entry(oh->ot_handle,
2513                                 dir, de, bh);
2514                 /* xtime should not be updated with server-side time. */
2515                 spin_lock(&obj->oo_guard);
2516                 dir->i_ctime = *ctime;
2517                 dir->i_mtime = *mtime;
2518                 spin_unlock(&obj->oo_guard);
2519                 mark_inode_dirty(dir);
2520                 brelse(bh);
2521         } else
2522                 rc = -ENOENT;
2523
2524         up_write(&obj->oo_ext_idx_sem);
2525         LASSERT(osd_invariant(obj));
2526         RETURN(rc);
2527 }
2528
2529 /**
2530  *      Lookup index for \a key and copy record to \a rec.
2531  *
2532  *      \param  dt_object      osd index object
2533  *      \param  key     key for index
2534  *      \param  rec     record reference
2535  *
2536  *      \retval  +ve  success : exact mach
2537  *      \retval  0    return record with key not greater than \a key
2538  *      \retval -ve   failure
2539  */
2540 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2541                                 struct dt_rec *rec, const struct dt_key *key,
2542                                 struct lustre_capa *capa)
2543 {
2544         struct osd_object     *obj = osd_dt_obj(dt);
2545         struct iam_path_descr *ipd;
2546         struct iam_container  *bag = &obj->oo_dir->od_container;
2547         struct osd_thread_info *oti = osd_oti_get(env);
2548         struct iam_iterator    *it = &oti->oti_idx_it;
2549         int rc;
2550         ENTRY;
2551
2552         LASSERT(osd_invariant(obj));
2553         LASSERT(dt_object_exists(dt));
2554         LASSERT(bag->ic_object == obj->oo_inode);
2555
2556         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2557                 RETURN(-EACCES);
2558
2559         ipd = osd_idx_ipd_get(env, bag);
2560         if (IS_ERR(ipd))
2561                 RETURN(-ENOMEM);
2562
2563         /* got ipd now we can start iterator. */
2564         iam_it_init(it, bag, 0, ipd);
2565
2566         rc = iam_it_get(it, (struct iam_key *)key);
2567         if (rc >= 0)
2568                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2569
2570         iam_it_put(it);
2571         iam_it_fini(it);
2572         osd_ipd_put(env, bag, ipd);
2573
2574         LINVRNT(osd_invariant(obj));
2575
2576         RETURN(rc);
2577 }
2578
2579 /**
2580  *      Inserts (key, value) pair in \a dt index object.
2581  *
2582  *      \param  dt      osd index object
2583  *      \param  key     key for index
2584  *      \param  rec     record reference
2585  *      \param  th      transaction handler
2586  *
2587  *      \retval  0  success
2588  *      \retval -ve failure
2589  */
2590 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2591                                 const struct dt_rec *rec, const struct dt_key *key,
2592                                 struct thandle *th, struct lustre_capa *capa,
2593                                 int ignore_quota)
2594 {
2595         struct osd_object     *obj = osd_dt_obj(dt);
2596         struct iam_path_descr *ipd;
2597         struct osd_thandle    *oh;
2598         struct iam_container  *bag = &obj->oo_dir->od_container;
2599 #ifdef HAVE_QUOTA_SUPPORT
2600         cfs_cap_t              save = current->cap_effective;
2601 #endif
2602         int rc;
2603
2604         ENTRY;
2605
2606         LINVRNT(osd_invariant(obj));
2607         LASSERT(dt_object_exists(dt));
2608         LASSERT(bag->ic_object == obj->oo_inode);
2609         LASSERT(th != NULL);
2610
2611         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2612                 return -EACCES;
2613
2614         ipd = osd_idx_ipd_get(env, bag);
2615         if (unlikely(ipd == NULL))
2616                 RETURN(-ENOMEM);
2617
2618         oh = container_of0(th, struct osd_thandle, ot_super);
2619         LASSERT(oh->ot_handle != NULL);
2620         LASSERT(oh->ot_handle->h_transaction != NULL);
2621 #ifdef HAVE_QUOTA_SUPPORT
2622         if (ignore_quota)
2623                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2624         else
2625                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2626 #endif
2627         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2628                         (struct iam_rec *)rec, ipd);
2629 #ifdef HAVE_QUOTA_SUPPORT
2630         current->cap_effective = save;
2631 #endif
2632         osd_ipd_put(env, bag, ipd);
2633         LINVRNT(osd_invariant(obj));
2634         RETURN(rc);
2635 }
2636
2637 /**
2638  * Calls ldiskfs_add_entry() to add directory entry
2639  * into the directory. This is required for
2640  * interoperability mode (b11826)
2641  *
2642  * \retval   0, on success
2643  * \retval -ve, on error
2644  */
2645 static int __osd_ea_add_rec(struct osd_thread_info *info,
2646                             struct osd_object *pobj,
2647                             struct osd_object *cobj,
2648                             const char *name,
2649                             struct thandle *th)
2650 {
2651         struct dentry      *child;
2652         struct osd_thandle *oth;
2653         struct inode       *cinode  = cobj->oo_inode;
2654         int rc;
2655
2656         oth = container_of(th, struct osd_thandle, ot_super);
2657         LASSERT(oth->ot_handle != NULL);
2658         LASSERT(oth->ot_handle->h_transaction != NULL);
2659
2660         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2661         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2662
2663         RETURN(rc);
2664 }
2665
2666 /**
2667  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2668  * into the directory.Also sets flags into osd object to
2669  * indicate dot and dotdot are created. This is required for
2670  * interoperability mode (b11826)
2671  *
2672  * \param dir   directory for dot and dotdot fixup.
2673  * \param obj   child object for linking
2674  *
2675  * \retval   0, on success
2676  * \retval -ve, on error
2677  */
2678 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2679                               struct osd_object *dir,
2680                               struct osd_object *obj, const char *name,
2681                               struct thandle *th)
2682 {
2683         struct inode            *parent_dir   = obj->oo_inode;
2684         struct inode            *inode  = dir->oo_inode;
2685         struct osd_thandle      *oth;
2686         int result = 0;
2687
2688         oth = container_of(th, struct osd_thandle, ot_super);
2689         LASSERT(oth->ot_handle->h_transaction != NULL);
2690         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2691
2692         if (strcmp(name, dot) == 0) {
2693                 if (dir->oo_compat_dot_created) {
2694                         result = -EEXIST;
2695                 } else {
2696                         LASSERT(obj == dir);
2697                         dir->oo_compat_dot_created = 1;
2698                         result = 0;
2699                 }
2700         } else if(strcmp(name, dotdot) == 0) {
2701                 if (!dir->oo_compat_dot_created)
2702                         return -EINVAL;
2703                 if (dir->oo_compat_dotdot_created)
2704                         return __osd_ea_add_rec(info, dir, obj, name, th);
2705
2706                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2707                 if (result == 0)
2708                        dir->oo_compat_dotdot_created = 1;
2709         }
2710
2711         return result;
2712 }
2713
2714
2715 /**
2716  * It will call the appropriate osd_add* function and return the
2717  * value, return by respective functions.
2718  */
2719 static int osd_ea_add_rec(const struct lu_env *env,
2720                           struct osd_object *pobj,
2721                           struct osd_object *cobj,
2722                           const char *name,
2723                           struct thandle *th)
2724 {
2725         struct osd_thread_info    *info   = osd_oti_get(env);
2726         int rc;
2727
2728         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2729                                                    name[2] =='\0')))
2730                 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2731         else
2732                 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2733
2734         return rc;
2735 }
2736
2737 /**
2738  * Calls ->lookup() to find dentry. From dentry get inode and
2739  * read inode's ea to get fid. This is required for  interoperability
2740  * mode (b11826)
2741  *
2742  * \retval   0, on success
2743  * \retval -ve, on error
2744  */
2745 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2746                              struct dt_rec *rec, const struct dt_key *key)
2747 {
2748         struct inode               *dir    = obj->oo_inode;
2749         struct osd_thread_info     *info   = osd_oti_get(env);
2750         struct dentry              *dentry;
2751         struct ldiskfs_dir_entry_2 *de;
2752         struct buffer_head         *bh;
2753         struct lu_fid              *fid = &info->oti_fid;
2754         int ino;
2755         int rc;
2756
2757         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2758
2759         dentry = osd_child_dentry_get(env, obj,
2760                                       (char *)key, strlen((char *)key));
2761
2762         down_read(&obj->oo_ext_idx_sem);
2763         bh = ldiskfs_find_entry(dentry, &de);
2764         if (bh) {
2765                 ino = le32_to_cpu(de->inode);
2766                 brelse(bh);
2767                 rc = osd_ea_fid_get(env, obj, ino, fid);
2768                 if (rc == 0)
2769                         osd_fid_pack(env, fid, rec);
2770         } else
2771                 rc = -ENOENT;
2772
2773         up_read(&obj->oo_ext_idx_sem);
2774         RETURN (rc);
2775 }
2776
2777 /**
2778  * Find the osd object for given fid.
2779  *
2780  * \param fid, need to find the osd object having this fid
2781  *
2782  * \retval osd_object, on success
2783  * \retval        -ve, on error
2784  */
2785 struct osd_object *osd_object_find(const struct lu_env *env,
2786                                    struct dt_object *dt,
2787                                    const struct lu_fid *fid)
2788 {
2789         struct lu_device         *ludev = dt->do_lu.lo_dev;
2790         struct osd_object        *child = NULL;
2791         struct lu_object         *luch;
2792         struct lu_object         *lo;
2793
2794         luch = lu_object_find(env, ludev, fid, NULL);
2795         if (!IS_ERR(luch)) {
2796                 if (lu_object_exists(luch)) {
2797                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2798                         if (lo != NULL)
2799                                 child = osd_obj(lo);
2800                         else
2801                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2802                                                 "lu_object can't be located"
2803                                                 ""DFID"\n", PFID(fid));
2804
2805                         if (child == NULL) {
2806                                 lu_object_put(env, luch);
2807                                 CERROR("Unable to get osd_object\n");
2808                                 child = ERR_PTR(-ENOENT);
2809                         }
2810                 } else {
2811                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
2812                                         "lu_object does not exists "DFID"\n",
2813                                         PFID(fid));
2814                         child = ERR_PTR(-ENOENT);
2815                 }
2816         } else
2817                 child = (void *)luch;
2818
2819         return child;
2820 }
2821
2822 /**
2823  * Put the osd object once done with it.
2824  *
2825  * \param obj, osd object that needs to be put
2826  */
2827 static inline void osd_object_put(const struct lu_env *env,
2828                                   struct osd_object *obj)
2829 {
2830         lu_object_put(env, &obj->oo_dt.do_lu);
2831 }
2832
2833 /**
2834  * Index add function for interoperability mode (b11826).
2835  * It will add the directory entry.This entry is needed to
2836  * maintain name->fid mapping.
2837  *
2838  * \param key, it is key i.e. file entry to be inserted
2839  * \param rec, it is value of given key i.e. fid
2840  *
2841  * \retval   0, on success
2842  * \retval -ve, on error
2843  */
2844 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2845                                const struct dt_rec *rec,
2846                                const struct dt_key *key, struct thandle *th,
2847                                struct lustre_capa *capa, int ignore_quota)
2848 {
2849         struct osd_object        *obj   = osd_dt_obj(dt);
2850         struct lu_fid            *fid   = &osd_oti_get(env)->oti_fid;
2851         const struct lu_fid_pack *pack  = (const struct lu_fid_pack *)rec;
2852         const char               *name  = (const char *)key;
2853         struct osd_object        *child;
2854 #ifdef HAVE_QUOTA_SUPPORT
2855         cfs_cap_t                 save  = current->cap_effective;
2856 #endif
2857         int rc;
2858
2859         ENTRY;
2860
2861         LASSERT(osd_invariant(obj));
2862         LASSERT(dt_object_exists(dt));
2863         LASSERT(th != NULL);
2864
2865         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2866                 RETURN(-EACCES);
2867
2868         rc = fid_unpack(pack, fid);
2869         if (rc != 0)
2870                 RETURN(rc);
2871         child = osd_object_find(env, dt, fid);
2872         if (!IS_ERR(child)) {
2873                 struct inode *inode = obj->oo_inode;
2874                 struct osd_thread_info *oti = osd_oti_get(env);
2875                 struct timespec *ctime = &oti->oti_time;
2876                 struct timespec *mtime = &oti->oti_time2;
2877
2878                 *ctime = inode->i_ctime;
2879                 *mtime = inode->i_mtime;
2880 #ifdef HAVE_QUOTA_SUPPORT
2881                 if (ignore_quota)
2882                         current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2883                 else
2884                         current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2885 #endif
2886                 down_write(&obj->oo_ext_idx_sem);
2887                 rc = osd_ea_add_rec(env, obj, child, name, th);
2888                 up_write(&obj->oo_ext_idx_sem);
2889 #ifdef HAVE_QUOTA_SUPPORT
2890                 current->cap_effective = save;
2891 #endif
2892                 osd_object_put(env, child);
2893                 /* xtime should not be updated with server-side time. */
2894                 spin_lock(&obj->oo_guard);
2895                 inode->i_ctime = *ctime;
2896                 inode->i_mtime = *mtime;
2897                 spin_unlock(&obj->oo_guard);
2898                 mark_inode_dirty(inode);
2899         } else {
2900                 rc = PTR_ERR(child);
2901         }
2902
2903         LASSERT(osd_invariant(obj));
2904         RETURN(rc);
2905 }
2906
2907 /**
2908  *  Initialize osd Iterator for given osd index object.
2909  *
2910  *  \param  dt      osd index object
2911  */
2912
2913 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2914                                  struct dt_object *dt,
2915                                  struct lustre_capa *capa)
2916 {
2917         struct osd_it_iam         *it;
2918         struct osd_thread_info *oti = osd_oti_get(env);
2919         struct osd_object     *obj = osd_dt_obj(dt);
2920         struct lu_object      *lo  = &dt->do_lu;
2921         struct iam_path_descr *ipd;
2922         struct iam_container  *bag = &obj->oo_dir->od_container;
2923
2924         LASSERT(lu_object_exists(lo));
2925
2926         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2927                 return ERR_PTR(-EACCES);
2928
2929         it = &oti->oti_it;
2930         ipd = osd_it_ipd_get(env, bag);
2931         if (likely(ipd != NULL)) {
2932                 it->oi_obj = obj;
2933                 it->oi_ipd = ipd;
2934                 lu_object_get(lo);
2935                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2936                 return (struct dt_it *)it;
2937         }
2938         return ERR_PTR(-ENOMEM);
2939 }
2940
2941 /**
2942  * free given Iterator.
2943  */
2944
2945 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2946 {
2947         struct osd_it_iam     *it = (struct osd_it_iam *)di;
2948         struct osd_object *obj = it->oi_obj;
2949
2950         iam_it_fini(&it->oi_it);
2951         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2952         lu_object_put(env, &obj->oo_dt.do_lu);
2953 }
2954
2955 /**
2956  *  Move Iterator to record specified by \a key
2957  *
2958  *  \param  di      osd iterator
2959  *  \param  key     key for index
2960  *
2961  *  \retval +ve  di points to record with least key not larger than key
2962  *  \retval  0   di points to exact matched key
2963  *  \retval -ve  failure
2964  */
2965
2966 static int osd_it_iam_get(const struct lu_env *env,
2967                       struct dt_it *di, const struct dt_key *key)
2968 {
2969         struct osd_it_iam *it = (struct osd_it_iam *)di;
2970
2971         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2972 }
2973
2974 /**
2975  *  Release Iterator
2976  *
2977  *  \param  di      osd iterator
2978  */
2979
2980 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2981 {
2982         struct osd_it_iam *it = (struct osd_it_iam *)di;
2983
2984         iam_it_put(&it->oi_it);
2985 }
2986
2987 /**
2988  *  Move iterator by one record
2989  *
2990  *  \param  di      osd iterator
2991  *
2992  *  \retval +1   end of container reached
2993  *  \retval  0   success
2994  *  \retval -ve  failure
2995  */
2996
2997 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2998 {
2999         struct osd_it_iam *it = (struct osd_it_iam *)di;
3000
3001         return iam_it_next(&it->oi_it);
3002 }
3003
3004 /**
3005  * Return pointer to the key under iterator.
3006  */
3007
3008 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3009                                  const struct dt_it *di)
3010 {
3011         struct osd_it_iam *it = (struct osd_it_iam *)di;
3012
3013         return (struct dt_key *)iam_it_key_get(&it->oi_it);
3014 }
3015
3016 /**
3017  * Return size of key under iterator (in bytes)
3018  */
3019
3020 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3021 {
3022         struct osd_it_iam *it = (struct osd_it_iam *)di;
3023
3024         return iam_it_key_size(&it->oi_it);
3025 }
3026
3027 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3028                                        __u32 attr,
3029                                        int len,
3030                                        __u16 type)
3031 {
3032         struct luda_type        *lt;
3033         const unsigned           align = sizeof(struct luda_type) - 1;
3034
3035         /* check if file type is required */
3036         if (attr & LUDA_TYPE) {
3037                         len = (len + align) & ~align;
3038
3039                         lt = (void *) ent->lde_name + len;
3040                         lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3041                         ent->lde_attrs |= LUDA_TYPE;
3042         }
3043
3044         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3045 }
3046
3047 /**
3048  * build lu direct from backend fs dirent.
3049  */
3050
3051 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3052                                       struct lu_fid *fid,
3053                                       __u64 offset,
3054                                       char *name,
3055                                       __u16 namelen,
3056                                       __u16 type,
3057                                       __u32 attr)
3058 {
3059         fid_cpu_to_le(&ent->lde_fid, fid);
3060         ent->lde_attrs = LUDA_FID;
3061
3062         ent->lde_hash = cpu_to_le64(offset);
3063         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3064
3065         strncpy(ent->lde_name, name, namelen);
3066         ent->lde_namelen = cpu_to_le16(namelen);
3067
3068         /* append lustre attributes */
3069         osd_it_append_attrs(ent, attr, namelen, type);
3070 }
3071
3072 /**
3073  * Return pointer to the record under iterator.
3074  */
3075 static int osd_it_iam_rec(const struct lu_env *env,
3076                           const struct dt_it *di,
3077                           struct lu_dirent *lde,
3078                           __u32 attr)
3079 {
3080         struct osd_it_iam *it        = (struct osd_it_iam *)di;
3081         struct osd_thread_info *info = osd_oti_get(env);
3082         struct lu_fid     *fid       = &info->oti_fid;
3083         const struct lu_fid_pack *rec;
3084         char *name;
3085         int namelen;
3086         __u64 hash;
3087         int rc;
3088
3089         name = (char *)iam_it_key_get(&it->oi_it);
3090         if (IS_ERR(name))
3091                 RETURN(PTR_ERR(name));
3092
3093         namelen = iam_it_key_size(&it->oi_it);
3094
3095         rec = (const struct lu_fid_pack *) iam_it_rec_get(&it->oi_it);
3096         if (IS_ERR(rec))
3097                 RETURN(PTR_ERR(rec));
3098
3099         rc = fid_unpack(rec, fid);
3100         if (rc)
3101                 RETURN(rc);
3102
3103         hash = iam_it_store(&it->oi_it);
3104
3105         /* IAM does not store object type in IAM index (dir) */
3106         osd_it_pack_dirent(lde, fid, hash, name, namelen,
3107                            0, LUDA_FID);
3108
3109         return 0;
3110 }
3111
3112 /**
3113  * Returns cookie for current Iterator position.
3114  */
3115 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3116 {
3117         struct osd_it_iam *it = (struct osd_it_iam *)di;
3118
3119         return iam_it_store(&it->oi_it);
3120 }
3121
3122 /**
3123  * Restore iterator from cookie.
3124  *
3125  * \param  di      osd iterator
3126  * \param  hash    Iterator location cookie
3127  *
3128  * \retval +ve  di points to record with least key not larger than key.
3129  * \retval  0   di points to exact matched key
3130  * \retval -ve  failure
3131  */
3132
3133 static int osd_it_iam_load(const struct lu_env *env,
3134                        const struct dt_it *di, __u64 hash)
3135 {
3136         struct osd_it_iam *it = (struct osd_it_iam *)di;
3137
3138         return iam_it_load(&it->oi_it, hash);
3139 }
3140
3141 static const struct dt_index_operations osd_index_iam_ops = {
3142         .dio_lookup = osd_index_iam_lookup,
3143         .dio_insert = osd_index_iam_insert,
3144         .dio_delete = osd_index_iam_delete,
3145         .dio_it     = {
3146                 .init     = osd_it_iam_init,
3147                 .fini     = osd_it_iam_fini,
3148                 .get      = osd_it_iam_get,
3149                 .put      = osd_it_iam_put,
3150                 .next     = osd_it_iam_next,
3151                 .key      = osd_it_iam_key,
3152                 .key_size = osd_it_iam_key_size,
3153                 .rec      = osd_it_iam_rec,
3154                 .store    = osd_it_iam_store,
3155                 .load     = osd_it_iam_load
3156         }
3157 };
3158
3159 /**
3160  * Creates or initializes iterator context.
3161  *
3162  * \retval struct osd_it_ea, iterator structure on success
3163  *
3164  */
3165 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3166                                     struct dt_object *dt,
3167                                     struct lustre_capa *capa)
3168 {
3169         struct osd_object       *obj  = osd_dt_obj(dt);
3170         struct osd_thread_info  *info = osd_oti_get(env);
3171         struct osd_it_ea        *it   = &info->oti_it_ea;
3172         struct lu_object        *lo   = &dt->do_lu;
3173         struct dentry           *obj_dentry = &info->oti_it_dentry;
3174         ENTRY;
3175         LASSERT(lu_object_exists(lo));
3176
3177         obj_dentry->d_inode = obj->oo_inode;
3178         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3179         obj_dentry->d_name.hash = 0;
3180
3181         it->oie_rd_dirent       = 0;
3182         it->oie_it_dirent       = 0;
3183         it->oie_dirent          = NULL;
3184         it->oie_buf             = info->oti_it_ea_buf;
3185         it->oie_obj             = obj;
3186         it->oie_file.f_pos      = 0;
3187         it->oie_file.f_dentry   = obj_dentry;
3188         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3189         it->oie_file.f_op         = obj->oo_inode->i_fop;
3190         it->oie_file.private_data = NULL;
3191         lu_object_get(lo);
3192         RETURN((struct dt_it*) it);
3193 }
3194
3195 /**
3196  * Destroy or finishes iterator context.
3197  *
3198  * \param di, struct osd_it_ea, iterator structure to be destroyed
3199  */
3200 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3201 {
3202         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3203         struct osd_object    *obj  = it->oie_obj;
3204         struct inode       *inode  = obj->oo_inode;
3205
3206         ENTRY;
3207         it->oie_file.f_op->release(inode, &it->oie_file);
3208         lu_object_put(env, &obj->oo_dt.do_lu);
3209         EXIT;
3210 }
3211
3212 /**
3213  * It position the iterator at given key, so that next lookup continues from
3214  * that key Or it is similar to dio_it->load() but based on a key,
3215  * rather than file position.
3216  *
3217  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3218  * to the beginning.
3219  *
3220  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3221  */
3222 static int osd_it_ea_get(const struct lu_env *env,
3223                          struct dt_it *di, const struct dt_key *key)
3224 {
3225         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3226
3227         ENTRY;
3228         LASSERT(((const char *)key)[0] == '\0');
3229         it->oie_file.f_pos      = 0;
3230         it->oie_rd_dirent       = 0;
3231         it->oie_it_dirent       = 0;
3232         it->oie_dirent          = NULL;
3233
3234         RETURN(+1);
3235 }
3236
3237 /**
3238  * Does nothing
3239  */
3240 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3241 {
3242 }
3243
3244 /**
3245  * It is called internally by ->readdir(). It fills the
3246  * iterator's in-memory data structure with required
3247  * information i.e. name, namelen, rec_size etc.
3248  *
3249  * \param buf, in which information to be filled in.
3250  * \param name, name of the file in given dir
3251  *
3252  * \retval 0, on success
3253  * \retval 1, on buffer full
3254  */
3255 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3256                                loff_t offset, __u64 ino,
3257                                unsigned d_type)
3258 {
3259         struct osd_it_ea        *it = (struct osd_it_ea *)buf;
3260         struct osd_it_ea_dirent *ent = it->oie_dirent;
3261         ENTRY;
3262
3263         /* this should never happen */
3264         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3265                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3266                 RETURN(-EIO);
3267         }
3268
3269         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3270             OSD_IT_EA_BUFSIZE)
3271                 RETURN(1);
3272
3273         ent->oied_ino     = ino;
3274         ent->oied_off     = offset;
3275         ent->oied_namelen = namelen;
3276         ent->oied_type    = d_type;
3277
3278         memcpy(ent->oied_name, name, namelen);
3279
3280         it->oie_rd_dirent++;
3281         it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3282         RETURN(0);
3283 }
3284
3285 /**
3286  * Calls ->readdir() to load a directory entry at a time
3287  * and stored it in iterator's in-memory data structure.
3288  *
3289  * \param di, struct osd_it_ea, iterator's in memory structure
3290  *
3291  * \retval   0, on success
3292  * \retval -ve, on error
3293  */
3294 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3295 {
3296         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3297         struct osd_object  *obj   = it->oie_obj;
3298         struct inode       *inode = obj->oo_inode;
3299         int                result = 0;
3300
3301         ENTRY;
3302         it->oie_dirent = it->oie_buf;
3303         it->oie_rd_dirent = 0;
3304
3305         down_read(&obj->oo_ext_idx_sem);
3306         result = inode->i_fop->readdir(&it->oie_file, it,
3307                                        (filldir_t) osd_ldiskfs_filldir);
3308
3309         up_read(&obj->oo_ext_idx_sem);
3310
3311         if (it->oie_rd_dirent == 0) {
3312                 result = -EIO;
3313         } else {
3314                 it->oie_dirent = it->oie_buf;
3315                 it->oie_it_dirent = 1;
3316         }
3317
3318         RETURN(result);
3319 }
3320
3321 /**
3322  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3323  * to load a directory entry at a time and stored it in
3324  * iterator's in-memory data structure.
3325  *
3326  * \param di, struct osd_it_ea, iterator's in memory structure
3327  *
3328  * \retval +ve, iterator reached to end
3329  * \retval   0, iterator not reached to end
3330  * \retval -ve, on error
3331  */
3332 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3333 {
3334         struct osd_it_ea *it = (struct osd_it_ea *)di;
3335         int rc;
3336
3337         ENTRY;
3338
3339         if (it->oie_it_dirent < it->oie_rd_dirent) {
3340                 it->oie_dirent = (void *) it->oie_dirent +
3341                                  size_round(sizeof(struct osd_it_ea_dirent) +
3342                                             it->oie_dirent->oied_namelen);
3343                 it->oie_it_dirent++;
3344                 RETURN(0);
3345         } else {
3346                 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3347                         rc = +1;
3348                 else
3349                         rc = osd_ldiskfs_it_fill(di);
3350         }
3351
3352         RETURN(rc);
3353 }
3354
3355 /**
3356  * Returns the key at current position from iterator's in memory structure.
3357  *
3358  * \param di, struct osd_it_ea, iterator's in memory structure
3359  *
3360  * \retval key i.e. struct dt_key on success
3361  */
3362 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3363                                     const struct dt_it *di)
3364 {
3365         struct osd_it_ea *it = (struct osd_it_ea *)di;
3366         ENTRY;
3367         RETURN((struct dt_key *)it->oie_dirent->oied_name);
3368 }
3369
3370 /**
3371  * Returns the key's size at current position from iterator's in memory structure.
3372  *
3373  * \param di, struct osd_it_ea, iterator's in memory structure
3374  *
3375  * \retval key_size i.e. struct dt_key on success
3376  */
3377 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3378 {
3379         struct osd_it_ea *it = (struct osd_it_ea *)di;
3380         ENTRY;
3381         RETURN(it->oie_dirent->oied_namelen);
3382 }
3383
3384
3385 /**
3386  * Returns the value (i.e. fid/igif) at current position from iterator's
3387  * in memory structure.
3388  *
3389  * \param di, struct osd_it_ea, iterator's in memory structure
3390  * \param attr, attr requested for dirent.
3391  * \param lde, lustre dirent
3392  *
3393  * \retval   0, no error and \param lde has correct lustre dirent.
3394  * \retval -ve, on error
3395  */
3396 static inline int osd_it_ea_rec(const struct lu_env *env,
3397                                 const struct dt_it *di,
3398                                 struct lu_dirent *lde,
3399                                 __u32 attr)
3400 {
3401         struct osd_it_ea        *it     = (struct osd_it_ea *)di;
3402         struct osd_object       *obj    = it->oie_obj;
3403         struct osd_thread_info  *info   = osd_oti_get(env);
3404         struct lu_fid  &n