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