Whamcloud - gitweb
117547f0a010e6b0eec268d1ae4a7e2fded8fd72
[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 lu_env          *env_del_obj = &oti->oti_obj_delete_tx_env;
527         struct thandle         *th;
528         int result;
529
530         lu_env_init(env_del_obj, LCT_DT_THREAD);
531         txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
532                             OSD_TXN_INODE_DELETE_CREDITS);
533         th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
534         if (!IS_ERR(th)) {
535                 result = osd_oi_delete(osd_oti_get(env_del_obj),
536                                        &osd->od_oi, fid, th);
537                 osd_trans_stop(env_del_obj, th);
538         } else
539                 result = PTR_ERR(th);
540
541         lu_env_fini(env_del_obj);
542         return result;
543 }
544
545 /*
546  * Called just before object is freed. Releases all resources except for
547  * object itself (that is released by osd_object_free()).
548  *
549  * Concurrency: no concurrent access is possible that late in object
550  * life-cycle.
551  */
552 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
553 {
554         struct osd_object *obj   = osd_obj(l);
555         struct inode      *inode = obj->oo_inode;
556
557         LINVRNT(osd_invariant(obj));
558
559         /*
560          * If object is unlinked remove fid->ino mapping from object index.
561          */
562
563         osd_index_fini(obj);
564         if (inode != NULL) {
565                 int result;
566
567                 if (osd_inode_unlinked(inode)) {
568                         result = osd_inode_remove(env, obj);
569                         if (result != 0)
570                                 LU_OBJECT_DEBUG(D_ERROR, env, l,
571                                                 "Failed to cleanup: %d\n",
572                                                 result);
573                 }
574
575                 iput(inode);
576                 obj->oo_inode = NULL;
577         }
578 }
579
580 /*
581  * Concurrency: ->loo_object_release() is called under site spin-lock.
582  */
583 static void osd_object_release(const struct lu_env *env,
584                                struct lu_object *l)
585 {
586         struct osd_object *o = osd_obj(l);
587
588         LASSERT(!lu_object_is_dying(l->lo_header));
589         if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
590                 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
591 }
592
593 /*
594  * Concurrency: shouldn't matter.
595  */
596 static int osd_object_print(const struct lu_env *env, void *cookie,
597                             lu_printer_t p, const struct lu_object *l)
598 {
599         struct osd_object *o = osd_obj(l);
600         struct iam_descr  *d;
601
602         if (o->oo_dir != NULL)
603                 d = o->oo_dir->od_container.ic_descr;
604         else
605                 d = NULL;
606         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
607                     o, o->oo_inode,
608                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
609                     o->oo_inode ? o->oo_inode->i_generation : 0,
610                     d ? d->id_ops->id_name : "plain");
611 }
612
613 /*
614  * Concurrency: shouldn't matter.
615  */
616 int osd_statfs(const struct lu_env *env, struct dt_device *d,
617                struct kstatfs *sfs)
618 {
619         struct osd_device *osd = osd_dt_dev(d);
620         struct super_block *sb = osd_sb(osd);
621         int result = 0;
622
623         spin_lock(&osd->od_osfs_lock);
624         /* cache 1 second */
625         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
626                 result = ll_do_statfs(sb, &osd->od_kstatfs);
627                 if (likely(result == 0)) /* N.B. statfs can't really fail */
628                         osd->od_osfs_age = cfs_time_current_64();
629         }
630
631         if (likely(result == 0))
632                 *sfs = osd->od_kstatfs;
633         spin_unlock(&osd->od_osfs_lock);
634
635         return result;
636 }
637
638 /*
639  * Concurrency: doesn't access mutable data.
640  */
641 static void osd_conf_get(const struct lu_env *env,
642                          const struct dt_device *dev,
643                          struct dt_device_param *param)
644 {
645         /*
646          * XXX should be taken from not-yet-existing fs abstraction layer.
647          */
648         param->ddp_max_name_len  = LDISKFS_NAME_LEN;
649         param->ddp_max_nlink     = LDISKFS_LINK_MAX;
650         param->ddp_block_shift   = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
651 }
652
653 /**
654  * Helper function to get and fill the buffer with input values.
655  */
656 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
657 {
658         struct lu_buf *buf;
659
660         buf = &osd_oti_get(env)->oti_buf;
661         buf->lb_buf = area;
662         buf->lb_len = len;
663         return buf;
664 }
665
666 /*
667  * Journal
668  */
669
670 /*
671  * Concurrency: doesn't access mutable data.
672  */
673 static int osd_param_is_sane(const struct osd_device *dev,
674                              const struct txn_param *param)
675 {
676         return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
677 }
678
679 /*
680  * Concurrency: shouldn't matter.
681  */
682 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
683 {
684         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
685         struct thandle     *th  = &oh->ot_super;
686         struct dt_device   *dev = th->th_dev;
687         struct lu_device   *lud = &dev->dd_lu_dev;
688
689         LASSERT(dev != NULL);
690         LASSERT(oh->ot_handle == NULL);
691
692         if (error) {
693                 CERROR("transaction @0x%p commit error: %d\n", th, error);
694         } else {
695                 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
696                 /*
697                  * This od_env_for_commit is only for commit usage.  see
698                  * "struct dt_device"
699                  */
700                 lu_context_enter(&env->le_ctx);
701                 dt_txn_hook_commit(env, th);
702                 lu_context_exit(&env->le_ctx);
703         }
704
705         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
706         lu_device_put(lud);
707         th->th_dev = NULL;
708
709         lu_context_exit(&th->th_ctx);
710         lu_context_fini(&th->th_ctx);
711         OBD_FREE_PTR(oh);
712 }
713
714 /*
715  * Concurrency: shouldn't matter.
716  */
717 static struct thandle *osd_trans_start(const struct lu_env *env,
718                                        struct dt_device *d,
719                                        struct txn_param *p)
720 {
721         struct osd_device  *dev = osd_dt_dev(d);
722         handle_t           *jh;
723         struct osd_thandle *oh;
724         struct thandle     *th;
725         int hook_res;
726
727         ENTRY;
728
729         hook_res = dt_txn_hook_start(env, d, p);
730         if (hook_res != 0)
731                 RETURN(ERR_PTR(hook_res));
732
733         if (osd_param_is_sane(dev, p)) {
734                 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
735                 if (oh != NULL) {
736                         struct osd_thread_info *oti = osd_oti_get(env);
737
738                         /*
739                          * XXX temporary stuff. Some abstraction layer should
740                          * be used.
741                          */
742
743                         jh = journal_start(osd_journal(dev), p->tp_credits);
744                         if (!IS_ERR(jh)) {
745                                 oh->ot_handle = jh;
746                                 th = &oh->ot_super;
747                                 th->th_dev = d;
748                                 th->th_result = 0;
749                                 jh->h_sync = p->tp_sync;
750                                 lu_device_get(&d->dd_lu_dev);
751                                 oh->ot_dev_link = lu_ref_add
752                                         (&d->dd_lu_dev.ld_reference,
753                                          "osd-tx", th);
754                                 /* add commit callback */
755                                 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
756                                 lu_context_enter(&th->th_ctx);
757                                 journal_callback_set(jh, osd_trans_commit_cb,
758                                                      (struct journal_callback *)&oh->ot_jcb);
759                                         LASSERT(oti->oti_txns == 0);
760                                         LASSERT(oti->oti_r_locks == 0);
761                                         LASSERT(oti->oti_w_locks == 0);
762                                         oti->oti_txns++;
763                         } else {
764                                 OBD_FREE_PTR(oh);
765                                 th = (void *)jh;
766                         }
767                 } else
768                         th = ERR_PTR(-ENOMEM);
769         } else {
770                 CERROR("Invalid transaction parameters\n");
771                 th = ERR_PTR(-EINVAL);
772         }
773
774         RETURN(th);
775 }
776
777 /*
778  * Concurrency: shouldn't matter.
779  */
780 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
781 {
782         int result;
783         struct osd_thandle *oh;
784         struct osd_thread_info *oti = osd_oti_get(env);
785
786         ENTRY;
787
788         oh = container_of0(th, struct osd_thandle, ot_super);
789         if (oh->ot_handle != NULL) {
790                 handle_t *hdl = oh->ot_handle;
791
792                 LASSERT(oti->oti_txns == 1);
793                 oti->oti_txns--;
794                 LASSERT(oti->oti_r_locks == 0);
795                 LASSERT(oti->oti_w_locks == 0);
796                 result = dt_txn_hook_stop(env, th);
797                 if (result != 0)
798                         CERROR("Failure in transaction hook: %d\n", result);
799                 oh->ot_handle = NULL;
800                 result = journal_stop(hdl);
801                 if (result != 0)
802                         CERROR("Failure to stop transaction: %d\n", result);
803         }
804         EXIT;
805 }
806
807 /*
808  * Concurrency: shouldn't matter.
809  */
810 static int osd_sync(const struct lu_env *env, struct dt_device *d)
811 {
812         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
813         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
814 }
815
816 /**
817  * Start commit for OSD device.
818  *
819  * An implementation of dt_commit_async method for OSD device.
820  * Asychronously starts underlayng fs sync and thereby a transaction
821  * commit.
822  *
823  * \param env environment
824  * \param d dt device
825  *
826  * \see dt_device_operations
827  */
828 static int osd_commit_async(const struct lu_env *env,
829                             struct dt_device *d)
830 {
831         struct super_block *s = osd_sb(osd_dt_dev(d));
832         ENTRY;
833
834         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
835         RETURN(s->s_op->sync_fs(s, 0));
836 }
837
838 /*
839  * Concurrency: shouldn't matter.
840  */
841 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
842
843 static void osd_ro(const struct lu_env *env, struct dt_device *d)
844 {
845         ENTRY;
846
847         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
848
849         __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
850                           fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
851         EXIT;
852 }
853
854
855 /*
856  * Concurrency: serialization provided by callers.
857  */
858 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
859                               int mode, unsigned long timeout, __u32 alg,
860                               struct lustre_capa_key *keys)
861 {
862         struct osd_device *dev = osd_dt_dev(d);
863         ENTRY;
864
865         dev->od_fl_capa = mode;
866         dev->od_capa_timeout = timeout;
867         dev->od_capa_alg = alg;
868         dev->od_capa_keys = keys;
869         RETURN(0);
870 }
871
872 /**
873  * Concurrency: serialization provided by callers.
874  */
875 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
876                                struct dt_quota_ctxt *ctxt, void *data)
877 {
878         struct obd_device *obd = (void *)ctxt;
879         struct vfsmount *mnt = (struct vfsmount *)data;
880         ENTRY;
881
882         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
883         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
884         obd->obd_lvfs_ctxt.pwdmnt = mnt;
885         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
886         obd->obd_lvfs_ctxt.fs = get_ds();
887
888         EXIT;
889 }
890
891 /**
892  * Note: we do not count into QUOTA here.
893  * If we mount with --data_journal we may need more.
894  */
895 static const int osd_dto_credits_noquota[DTO_NR] = {
896         /**
897          * Insert/Delete.
898          * INDEX_EXTRA_TRANS_BLOCKS(8) +
899          * SINGLEDATA_TRANS_BLOCKS(8)
900          * XXX Note: maybe iam need more, since iam have more level than
901          *           EXT3 htree.
902          */
903         [DTO_INDEX_INSERT]  = 16,
904         [DTO_INDEX_DELETE]  = 16,
905         /**
906          * Unused now
907          */
908         [DTO_IDNEX_UPDATE]  = 16,
909         /**
910          * Create a object. The same as create object in EXT3.
911          * DATA_TRANS_BLOCKS(14) +
912          * INDEX_EXTRA_BLOCKS(8) +
913          * 3(inode bits, groups, GDT)
914          */
915         [DTO_OBJECT_CREATE] = 25,
916         /**
917          * Unused now
918          */
919         [DTO_OBJECT_DELETE] = 25,
920         /**
921          * Attr set credits.
922          * 3(inode bits, group, GDT)
923          */
924         [DTO_ATTR_SET_BASE] = 3,
925         /**
926          * Xattr set. The same as xattr of EXT3.
927          * DATA_TRANS_BLOCKS(14)
928          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
929          *           also counted in. Do not know why?
930          */
931         [DTO_XATTR_SET]     = 14,
932         [DTO_LOG_REC]       = 14,
933         /**
934          * creadits for inode change during write.
935          */
936         [DTO_WRITE_BASE]    = 3,
937         /**
938          * credits for single block write.
939          */
940         [DTO_WRITE_BLOCK]   = 14,
941         /**
942          * Attr set credits for chown.
943          * 3 (inode bit, group, GDT)
944          */
945         [DTO_ATTR_SET_CHOWN]= 3
946 };
947
948 /**
949  * Note: we count into QUOTA here.
950  * If we mount with --data_journal we may need more.
951  */
952 static const int osd_dto_credits_quota[DTO_NR] = {
953         /**
954          * INDEX_EXTRA_TRANS_BLOCKS(8) +
955          * SINGLEDATA_TRANS_BLOCKS(8) +
956          * 2 * QUOTA_TRANS_BLOCKS(2)
957          */
958         [DTO_INDEX_INSERT]  = 20,
959         /**
960          * INDEX_EXTRA_TRANS_BLOCKS(8) +
961          * SINGLEDATA_TRANS_BLOCKS(8) +
962          * 2 * QUOTA_TRANS_BLOCKS(2)
963          */
964         [DTO_INDEX_DELETE]  = 20,
965         /**
966          * Unused now.
967          */ 
968         [DTO_IDNEX_UPDATE]  = 16,
969         /*
970          * Create a object. Same as create object in EXT3 filesystem.
971          * DATA_TRANS_BLOCKS(16) +
972          * INDEX_EXTRA_BLOCKS(8) +
973          * 3(inode bits, groups, GDT) +
974          * 2 * QUOTA_INIT_BLOCKS(25)
975          */
976         [DTO_OBJECT_CREATE] = 77,
977         /*
978          * Unused now.
979          * DATA_TRANS_BLOCKS(16) +
980          * INDEX_EXTRA_BLOCKS(8) +
981          * 3(inode bits, groups, GDT) +
982          * QUOTA(?)
983          */ 
984         [DTO_OBJECT_DELETE] = 27,
985         /**
986          * Attr set credits.
987          * 3 (inode bit, group, GDT) +
988          */
989         [DTO_ATTR_SET_BASE] = 3,
990         /**
991          * Xattr set. The same as xattr of EXT3.
992          * DATA_TRANS_BLOCKS(16)
993          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
994          *           also counted in. Do not know why?
995          */
996         [DTO_XATTR_SET]     = 16,
997         [DTO_LOG_REC]       = 16,
998         /**
999          * creadits for inode change during write.
1000          */
1001         [DTO_WRITE_BASE]    = 3,
1002         /**
1003          * credits for single block write.
1004          */
1005         [DTO_WRITE_BLOCK]   = 16,
1006         /**
1007          * Attr set credits for chown.
1008          * 3 (inode bit, group, GDT) +
1009          * 2 * QUOTA_INIT_BLOCKS(25) +
1010          * 2 * QUOTA_DEL_BLOCKS(9)
1011          */
1012         [DTO_ATTR_SET_CHOWN]= 71
1013 };
1014
1015 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1016                           enum dt_txn_op op)
1017 {
1018         LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1019                 ARRAY_SIZE(osd_dto_credits_quota));
1020         LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1021 #ifdef HAVE_QUOTA_SUPPORT
1022         if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1023                 return osd_dto_credits_quota[op];
1024         else
1025 #endif
1026                 return osd_dto_credits_noquota[op];
1027 }
1028
1029 static const struct dt_device_operations osd_dt_ops = {
1030         .dt_root_get       = osd_root_get,
1031         .dt_statfs         = osd_statfs,
1032         .dt_trans_start    = osd_trans_start,
1033         .dt_trans_stop     = osd_trans_stop,
1034         .dt_conf_get       = osd_conf_get,
1035         .dt_sync           = osd_sync,
1036         .dt_ro             = osd_ro,
1037         .dt_commit_async   = osd_commit_async,
1038         .dt_credit_get     = osd_credit_get,
1039         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1040         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1041 };
1042
1043 static void osd_object_read_lock(const struct lu_env *env,
1044                                  struct dt_object *dt, unsigned role)
1045 {
1046         struct osd_object *obj = osd_dt_obj(dt);
1047         struct osd_thread_info *oti = osd_oti_get(env);
1048
1049         LINVRNT(osd_invariant(obj));
1050
1051         LASSERT(obj->oo_owner != env);
1052         down_read_nested(&obj->oo_sem, role);
1053
1054         LASSERT(obj->oo_owner == NULL);
1055         oti->oti_r_locks++;
1056 }
1057
1058 static void osd_object_write_lock(const struct lu_env *env,
1059                                   struct dt_object *dt, unsigned role)
1060 {
1061         struct osd_object *obj = osd_dt_obj(dt);
1062         struct osd_thread_info *oti = osd_oti_get(env);
1063
1064         LINVRNT(osd_invariant(obj));
1065
1066         LASSERT(obj->oo_owner != env);
1067         down_write_nested(&obj->oo_sem, role);
1068
1069         LASSERT(obj->oo_owner == NULL);
1070         obj->oo_owner = env;
1071         oti->oti_w_locks++;
1072 }
1073
1074 static void osd_object_read_unlock(const struct lu_env *env,
1075                                    struct dt_object *dt)
1076 {
1077         struct osd_object *obj = osd_dt_obj(dt);
1078         struct osd_thread_info *oti = osd_oti_get(env);
1079
1080         LINVRNT(osd_invariant(obj));
1081
1082         LASSERT(oti->oti_r_locks > 0);
1083         oti->oti_r_locks--;
1084         up_read(&obj->oo_sem);
1085 }
1086
1087 static void osd_object_write_unlock(const struct lu_env *env,
1088                                     struct dt_object *dt)
1089 {
1090         struct osd_object *obj = osd_dt_obj(dt);
1091         struct osd_thread_info *oti = osd_oti_get(env);
1092
1093         LINVRNT(osd_invariant(obj));
1094
1095         LASSERT(obj->oo_owner == env);
1096         LASSERT(oti->oti_w_locks > 0);
1097         oti->oti_w_locks--;
1098         obj->oo_owner = NULL;
1099         up_write(&obj->oo_sem);
1100 }
1101
1102 static int capa_is_sane(const struct lu_env *env,
1103                         struct osd_device *dev,
1104                         struct lustre_capa *capa,
1105                         struct lustre_capa_key *keys)
1106 {
1107         struct osd_thread_info *oti = osd_oti_get(env);
1108         struct lustre_capa *tcapa = &oti->oti_capa;
1109         struct obd_capa *oc;
1110         int i, rc = 0;
1111         ENTRY;
1112
1113         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1114         if (oc) {
1115                 if (capa_is_expired(oc)) {
1116                         DEBUG_CAPA(D_ERROR, capa, "expired");
1117                         rc = -ESTALE;
1118                 }
1119                 capa_put(oc);
1120                 RETURN(rc);
1121         }
1122
1123         if (capa_is_expired_sec(capa)) {
1124                 DEBUG_CAPA(D_ERROR, capa, "expired");
1125                 RETURN(-ESTALE);
1126         }
1127
1128         spin_lock(&capa_lock);
1129         for (i = 0; i < 2; i++) {
1130                 if (keys[i].lk_keyid == capa->lc_keyid) {
1131                         oti->oti_capa_key = keys[i];
1132                         break;
1133                 }
1134         }
1135         spin_unlock(&capa_lock);
1136
1137         if (i == 2) {
1138                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1139                 RETURN(-ESTALE);
1140         }
1141
1142         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1143         if (rc)
1144                 RETURN(rc);
1145
1146         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1147                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1148                 RETURN(-EACCES);
1149         }
1150
1151         oc = capa_add(dev->od_capa_hash, capa);
1152         capa_put(oc);
1153
1154         RETURN(0);
1155 }
1156
1157 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1158                            struct lustre_capa *capa, __u64 opc)
1159 {
1160         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1161         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1162         struct md_capainfo *ci;
1163         int rc;
1164
1165         if (!dev->od_fl_capa)
1166                 return 0;
1167
1168         if (capa == BYPASS_CAPA)
1169                 return 0;
1170
1171         ci = md_capainfo(env);
1172         if (unlikely(!ci))
1173                 return 0;
1174
1175         if (ci->mc_auth == LC_ID_NONE)
1176                 return 0;
1177
1178         if (!capa) {
1179                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1180                 return -EACCES;
1181         }
1182
1183         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1184                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1185                            PFID(fid));
1186                 return -EACCES;
1187         }
1188
1189         if (!capa_opc_supported(capa, opc)) {
1190                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1191                 return -EACCES;
1192         }
1193
1194         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1195                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1196                 return -EACCES;
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int osd_attr_get(const struct lu_env *env,
1203                         struct dt_object *dt,
1204                         struct lu_attr *attr,
1205                         struct lustre_capa *capa)
1206 {
1207         struct osd_object *obj = osd_dt_obj(dt);
1208
1209         LASSERT(dt_object_exists(dt));
1210         LINVRNT(osd_invariant(obj));
1211
1212         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1213                 return -EACCES;
1214
1215         spin_lock(&obj->oo_guard);
1216         osd_inode_getattr(env, obj->oo_inode, attr);
1217         spin_unlock(&obj->oo_guard);
1218         return 0;
1219 }
1220
1221 static int osd_attr_set(const struct lu_env *env,
1222                         struct dt_object *dt,
1223                         const struct lu_attr *attr,
1224                         struct thandle *handle,
1225                         struct lustre_capa *capa)
1226 {
1227         struct osd_object *obj = osd_dt_obj(dt);
1228         int rc;
1229
1230         LASSERT(handle != NULL);
1231         LASSERT(dt_object_exists(dt));
1232         LASSERT(osd_invariant(obj));
1233
1234         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1235                 return -EACCES;
1236
1237         spin_lock(&obj->oo_guard);
1238         rc = osd_inode_setattr(env, obj->oo_inode, attr);
1239         spin_unlock(&obj->oo_guard);
1240
1241         if (!rc)
1242                 mark_inode_dirty(obj->oo_inode);
1243         return rc;
1244 }
1245
1246 static struct timespec *osd_inode_time(const struct lu_env *env,
1247                                        struct inode *inode, __u64 seconds)
1248 {
1249         struct osd_thread_info *oti = osd_oti_get(env);
1250         struct timespec        *t   = &oti->oti_time;
1251
1252         t->tv_sec  = seconds;
1253         t->tv_nsec = 0;
1254         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1255         return t;
1256 }
1257
1258 static int osd_inode_setattr(const struct lu_env *env,
1259                              struct inode *inode, const struct lu_attr *attr)
1260 {
1261         __u64 bits;
1262
1263         bits = attr->la_valid;
1264
1265         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1266
1267 #ifdef HAVE_QUOTA_SUPPORT
1268         if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1269             (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1270                 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1271                 struct iattr iattr;
1272                 int rc;
1273
1274                 iattr.ia_valid = 0;
1275                 if (bits & LA_UID)
1276                         iattr.ia_valid |= ATTR_UID;
1277                 if (bits & LA_GID)
1278                         iattr.ia_valid |= ATTR_GID;
1279                 iattr.ia_uid = attr->la_uid;
1280                 iattr.ia_gid = attr->la_gid;
1281                 osd_push_ctxt(env, save);
1282                 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1283                 osd_pop_ctxt(save);
1284                 if (rc != 0)
1285                         return rc;
1286         }
1287 #endif
1288
1289         if (bits & LA_ATIME)
1290                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1291         if (bits & LA_CTIME)
1292                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1293         if (bits & LA_MTIME)
1294                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1295         if (bits & LA_SIZE) {
1296                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1297                 i_size_write(inode, attr->la_size);
1298         }
1299
1300         /* OSD should not change "i_blocks" which is used by quota.
1301          * "i_blocks" should be changed by ldiskfs only.
1302          * Enable this assignment for SOM purpose now, until it is
1303          * stored in SOM EA. */
1304         if (bits & LA_BLOCKS)
1305                 inode->i_blocks = attr->la_blocks;
1306
1307         if (bits & LA_MODE)
1308                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1309                         (attr->la_mode & ~S_IFMT);
1310         if (bits & LA_UID)
1311                 inode->i_uid    = attr->la_uid;
1312         if (bits & LA_GID)
1313                 inode->i_gid    = attr->la_gid;
1314         if (bits & LA_NLINK)
1315                 inode->i_nlink  = attr->la_nlink;
1316         if (bits & LA_RDEV)
1317                 inode->i_rdev   = attr->la_rdev;
1318
1319         if (bits & LA_FLAGS) {
1320                 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1321
1322                 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1323                         (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1324         }
1325         return 0;
1326 }
1327
1328 /*
1329  * Object creation.
1330  *
1331  * XXX temporary solution.
1332  */
1333
1334 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1335                           struct lu_attr *attr, struct thandle *th)
1336 {
1337         return 0;
1338 }
1339
1340 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1341                            struct lu_attr *attr, struct thandle *th)
1342 {
1343         osd_object_init0(obj);
1344         return 0;
1345 }
1346
1347 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1348                                           struct inode * dir, int mode);
1349 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1350                              struct inode *inode);
1351 extern int ldiskfs_delete_entry(handle_t *handle,
1352                                 struct inode * dir,
1353                                 struct ldiskfs_dir_entry_2 * de_del,
1354                                 struct buffer_head * bh);
1355 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1356                                                struct ldiskfs_dir_entry_2
1357                                                ** res_dir);
1358 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1359                                   struct inode *inode);
1360
1361 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1362                                     int name_index, const char *name,
1363                                     const void *value, size_t value_len,
1364                                     int flags);
1365
1366 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1367                                             struct osd_object *obj,
1368                                             const char *name,
1369                                             const int namelen)
1370 {
1371         struct osd_thread_info *info   = osd_oti_get(env);
1372         struct dentry *child_dentry = &info->oti_child_dentry;
1373         struct dentry *obj_dentry = &info->oti_obj_dentry;
1374
1375         obj_dentry->d_inode = obj->oo_inode;
1376         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1377         obj_dentry->d_name.hash = 0;
1378
1379         child_dentry->d_name.hash = 0;
1380         child_dentry->d_parent = obj_dentry;
1381         child_dentry->d_name.name = name;
1382         child_dentry->d_name.len = namelen;
1383         return child_dentry;
1384 }
1385
1386
1387 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1388                       umode_t mode,
1389                       struct dt_allocation_hint *hint,
1390                       struct thandle *th)
1391 {
1392         int result;
1393         struct osd_device  *osd = osd_obj2dev(obj);
1394         struct osd_thandle *oth;
1395         struct dt_object   *parent;
1396         struct inode       *inode;
1397 #ifdef HAVE_QUOTA_SUPPORT
1398         struct osd_ctxt    *save = &info->oti_ctxt;
1399 #endif
1400
1401         LINVRNT(osd_invariant(obj));
1402         LASSERT(obj->oo_inode == NULL);
1403
1404         oth = container_of(th, struct osd_thandle, ot_super);
1405         LASSERT(oth->ot_handle->h_transaction != NULL);
1406
1407         if (hint && hint->dah_parent)
1408                 parent = hint->dah_parent;
1409         else
1410                 parent = osd->od_obj_area;
1411
1412         LASSERT(parent != NULL);
1413         LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1414
1415 #ifdef HAVE_QUOTA_SUPPORT
1416         osd_push_ctxt(info->oti_env, save);
1417 #endif
1418         inode = ldiskfs_create_inode(oth->ot_handle,
1419                                      osd_dt_obj(parent)->oo_inode, mode);
1420 #ifdef HAVE_QUOTA_SUPPORT
1421         osd_pop_ctxt(save);
1422 #endif
1423         if (!IS_ERR(inode)) {
1424                 obj->oo_inode = inode;
1425                 result = 0;
1426         } else
1427                 result = PTR_ERR(inode);
1428         LINVRNT(osd_invariant(obj));
1429         return result;
1430 }
1431
1432
1433 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1434                            int recsize, handle_t *handle);
1435
1436 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1437                            int recsize, handle_t *handle);
1438
1439
1440 enum {
1441         OSD_NAME_LEN = 255
1442 };
1443
1444 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1445                      struct lu_attr *attr,
1446                      struct dt_allocation_hint *hint,
1447                      struct dt_object_format *dof,
1448                      struct thandle *th)
1449 {
1450         int result;
1451         struct osd_thandle *oth;
1452         struct osd_device *osd = osd_obj2dev(obj);
1453         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1454
1455         LASSERT(S_ISDIR(attr->la_mode));
1456
1457         oth = container_of(th, struct osd_thandle, ot_super);
1458         LASSERT(oth->ot_handle->h_transaction != NULL);
1459         result = osd_mkfile(info, obj, mode, hint, th);
1460         if (result == 0 && osd->od_iop_mode == 0) {
1461                 LASSERT(obj->oo_inode != NULL);
1462                 /*
1463                  * XXX uh-oh... call low-level iam function directly.
1464                  */
1465
1466                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1467                                          sizeof (struct lu_fid_pack),
1468                                          oth->ot_handle);
1469         }
1470         return result;
1471 }
1472
1473 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1474                         struct lu_attr *attr,
1475                         struct dt_allocation_hint *hint,
1476                         struct dt_object_format *dof,
1477                         struct thandle *th)
1478 {
1479         int result;
1480         struct osd_thandle *oth;
1481         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1482
1483         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1484
1485         LASSERT(S_ISREG(attr->la_mode));
1486
1487         oth = container_of(th, struct osd_thandle, ot_super);
1488         LASSERT(oth->ot_handle->h_transaction != NULL);
1489
1490         result = osd_mkfile(info, obj, mode, hint, th);
1491         if (result == 0) {
1492                 LASSERT(obj->oo_inode != NULL);
1493                 if (feat->dif_flags & DT_IND_VARKEY)
1494                         result = iam_lvar_create(obj->oo_inode,
1495                                                  feat->dif_keysize_max,
1496                                                  feat->dif_ptrsize,
1497                                                  feat->dif_recsize_max,
1498                                                  oth->ot_handle);
1499                 else
1500                         result = iam_lfix_create(obj->oo_inode,
1501                                                  feat->dif_keysize_max,
1502                                                  feat->dif_ptrsize,
1503                                                  feat->dif_recsize_max,
1504                                                  oth->ot_handle);
1505
1506         }
1507         return result;
1508 }
1509
1510 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1511                      struct lu_attr *attr,
1512                      struct dt_allocation_hint *hint,
1513                      struct dt_object_format *dof,
1514                      struct thandle *th)
1515 {
1516         LASSERT(S_ISREG(attr->la_mode));
1517         return osd_mkfile(info, obj, (attr->la_mode &
1518                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1519 }
1520
1521 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1522                      struct lu_attr *attr,
1523                      struct dt_allocation_hint *hint,
1524                      struct dt_object_format *dof,
1525                      struct thandle *th)
1526 {
1527         LASSERT(S_ISLNK(attr->la_mode));
1528         return osd_mkfile(info, obj, (attr->la_mode &
1529                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1530 }
1531
1532 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1533                      struct lu_attr *attr,
1534                      struct dt_allocation_hint *hint,
1535                      struct dt_object_format *dof,
1536                      struct thandle *th)
1537 {
1538         umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1539         int result;
1540
1541         LINVRNT(osd_invariant(obj));
1542         LASSERT(obj->oo_inode == NULL);
1543         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1544                 S_ISFIFO(mode) || S_ISSOCK(mode));
1545
1546         result = osd_mkfile(info, obj, mode, hint, th);
1547         if (result == 0) {
1548                 LASSERT(obj->oo_inode != NULL);
1549                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1550         }
1551         LINVRNT(osd_invariant(obj));
1552         return result;
1553 }
1554
1555 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1556                               struct lu_attr *,
1557                               struct dt_allocation_hint *hint,
1558                               struct dt_object_format *dof,
1559                               struct thandle *);
1560
1561 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1562 {
1563         osd_obj_type_f result;
1564
1565         switch (type) {
1566         case DFT_DIR:
1567                 result = osd_mkdir;
1568                 break;
1569         case DFT_REGULAR:
1570                 result = osd_mkreg;
1571                 break;
1572         case DFT_SYM:
1573                 result = osd_mksym;
1574                 break;
1575         case DFT_NODE:
1576                 result = osd_mknod;
1577                 break;
1578         case DFT_INDEX:
1579                 result = osd_mk_index;
1580                 break;
1581
1582         default:
1583                 LBUG();
1584                 break;
1585         }
1586         return result;
1587 }
1588
1589
1590 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1591                         struct dt_object *parent, umode_t child_mode)
1592 {
1593         LASSERT(ah);
1594
1595         memset(ah, 0, sizeof(*ah));
1596         ah->dah_parent = parent;
1597         ah->dah_mode = child_mode;
1598 }
1599
1600 /**
1601  * Helper function for osd_object_create()
1602  *
1603  * \retval 0, on success
1604  */
1605 static int __osd_object_create(struct osd_thread_info *info,
1606                                struct osd_object *obj, struct lu_attr *attr,
1607                                struct dt_allocation_hint *hint,
1608                                struct dt_object_format *dof,
1609                                struct thandle *th)
1610 {
1611
1612         int result;
1613
1614         result = osd_create_pre(info, obj, attr, th);
1615         if (result == 0) {
1616                 result = osd_create_type_f(dof->dof_type)(info, obj,
1617                                            attr, hint, dof, th);
1618                 if (result == 0)
1619                         result = osd_create_post(info, obj, attr, th);
1620         }
1621         return result;
1622 }
1623
1624 /**
1625  * Helper function for osd_object_create()
1626  *
1627  * \retval 0, on success
1628  */
1629 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1630                            const struct lu_fid *fid, struct thandle *th)
1631 {
1632         struct osd_thread_info *info = osd_oti_get(env);
1633         struct osd_inode_id    *id   = &info->oti_id;
1634         struct osd_device      *osd  = osd_obj2dev(obj);
1635         struct md_ucred        *uc   = md_ucred(env);
1636
1637         LASSERT(obj->oo_inode != NULL);
1638         LASSERT(uc != NULL);
1639
1640         id->oii_ino = obj->oo_inode->i_ino;
1641         id->oii_gen = obj->oo_inode->i_generation;
1642
1643         return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1644                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1645 }
1646
1647 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1648                              struct lu_attr *attr,
1649                              struct dt_allocation_hint *hint,
1650                              struct dt_object_format *dof,
1651                              struct thandle *th)
1652 {
1653         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1654         struct osd_object      *obj    = osd_dt_obj(dt);
1655         struct osd_thread_info *info   = osd_oti_get(env);
1656         int result;
1657
1658         ENTRY;
1659
1660         LINVRNT(osd_invariant(obj));
1661         LASSERT(!dt_object_exists(dt));
1662         LASSERT(osd_write_locked(env, obj));
1663         LASSERT(th != NULL);
1664
1665         result = __osd_object_create(info, obj, attr, hint, dof, th);
1666         if (result == 0)
1667                 result = __osd_oi_insert(env, obj, fid, th);
1668
1669         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1670         LASSERT(osd_invariant(obj));
1671         RETURN(result);
1672 }
1673
1674 /**
1675  * Helper function for osd_xattr_set()
1676  */
1677 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1678                            const struct lu_buf *buf, const char *name, int fl)
1679 {
1680         struct osd_object      *obj      = osd_dt_obj(dt);
1681         struct inode           *inode    = obj->oo_inode;
1682         struct osd_thread_info *info     = osd_oti_get(env);
1683         struct dentry          *dentry   = &info->oti_child_dentry;
1684         struct timespec        *t        = &info->oti_time;
1685         int                     fs_flags = 0;
1686         int  rc;
1687
1688         LASSERT(dt_object_exists(dt));
1689         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1690         LASSERT(osd_write_locked(env, obj));
1691
1692         if (fl & LU_XATTR_REPLACE)
1693                 fs_flags |= XATTR_REPLACE;
1694
1695         if (fl & LU_XATTR_CREATE)
1696                 fs_flags |= XATTR_CREATE;
1697
1698         dentry->d_inode = inode;
1699         *t = inode->i_ctime;
1700         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1701                                    buf->lb_len, fs_flags);
1702         /* ctime should not be updated with server-side time. */
1703         spin_lock(&obj->oo_guard);
1704         inode->i_ctime = *t;
1705         spin_unlock(&obj->oo_guard);
1706         mark_inode_dirty(inode);
1707         return rc;
1708 }
1709
1710 /**
1711  * Put the fid into lustre_mdt_attrs, and then place the structure
1712  * inode's ea. This fid should not be altered during the life time
1713  * of the inode.
1714  *
1715  * \retval +ve, on success
1716  * \retval -ve, on error
1717  *
1718  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1719  */
1720 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1721                           const struct lu_fid *fid)
1722 {
1723         struct osd_thread_info  *info      = osd_oti_get(env);
1724         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1725
1726         fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1727
1728         return __osd_xattr_set(env, dt,
1729                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1730                                XATTR_NAME_LMA, LU_XATTR_CREATE);
1731
1732 }
1733
1734 /**
1735  * Helper function to form igif
1736  */
1737 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1738                                 struct lu_fid *fid)
1739 {
1740         struct inode  *inode = dentry->d_inode;
1741         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1742 }
1743
1744 /**
1745  * Helper function to pack the fid
1746  */
1747 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1748                                 struct lu_fid_pack *pack)
1749 {
1750         fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1751 }
1752
1753 /**
1754  * Try to read the fid from inode ea into dt_rec, if return value
1755  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1756  *
1757  * \param rec, the data-structure into which fid/igif is read
1758  *
1759  * \retval 0, on success
1760  */
1761 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1762                           struct dt_rec *rec)
1763 {
1764         struct inode            *inode     = dentry->d_inode;
1765         struct osd_thread_info  *info      = osd_oti_get(env);
1766         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1767         struct lu_fid           *fid       = &info->oti_fid;
1768         int rc;
1769
1770         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1771
1772         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1773                                    sizeof *mdt_attrs);
1774
1775         if (rc > 0) {
1776                 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1777                 rc = 0;
1778         } else if (rc == -ENODATA) {
1779                 osd_igif_get(env, dentry, fid);
1780                 rc = 0;
1781         }
1782
1783         if (rc == 0)
1784                 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1785
1786         return rc;
1787 }
1788
1789 /**
1790  * OSD layer object create function for interoperability mode (b11826).
1791  * This is mostly similar to osd_object_create(). Only difference being, fid is
1792  * inserted into inode ea here.
1793  *
1794  * \retval   0, on success
1795  * \retval -ve, on error
1796  */
1797 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1798                              struct lu_attr *attr,
1799                              struct dt_allocation_hint *hint,
1800                              struct dt_object_format *dof,
1801                              struct thandle *th)
1802 {
1803         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1804         struct osd_object      *obj    = osd_dt_obj(dt);
1805         struct osd_thread_info *info   = osd_oti_get(env);
1806         int result;
1807         int is_root = 0;
1808
1809         ENTRY;
1810
1811         LASSERT(osd_invariant(obj));
1812         LASSERT(!dt_object_exists(dt));
1813         LASSERT(osd_write_locked(env, obj));
1814         LASSERT(th != NULL);
1815
1816         result = __osd_object_create(info, obj, attr, hint, dof, th);
1817
1818         if (hint && hint->dah_parent)
1819                 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1820
1821         /* objects under osd root shld have igif fid, so dont add fid EA */
1822         if (result == 0 && is_root == 0)
1823                 result = osd_ea_fid_set(env, dt, fid);
1824
1825         if (result == 0)
1826                 result = __osd_oi_insert(env, obj, fid, th);
1827
1828         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1829         LINVRNT(osd_invariant(obj));
1830         RETURN(result);
1831 }
1832
1833 /*
1834  * Concurrency: @dt is write locked.
1835  */
1836 static void osd_object_ref_add(const struct lu_env *env,
1837                                struct dt_object *dt,
1838                                struct thandle *th)
1839 {
1840         struct osd_object *obj = osd_dt_obj(dt);
1841         struct inode *inode = obj->oo_inode;
1842
1843         LINVRNT(osd_invariant(obj));
1844         LASSERT(dt_object_exists(dt));
1845         LASSERT(osd_write_locked(env, obj));
1846         LASSERT(th != NULL);
1847
1848         spin_lock(&obj->oo_guard);
1849         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1850         inode->i_nlink++;
1851         spin_unlock(&obj->oo_guard);
1852         mark_inode_dirty(inode);
1853         LINVRNT(osd_invariant(obj));
1854 }
1855
1856 /*
1857  * Concurrency: @dt is write locked.
1858  */
1859 static void osd_object_ref_del(const struct lu_env *env,
1860                                struct dt_object *dt,
1861                                struct thandle *th)
1862 {
1863         struct osd_object *obj = osd_dt_obj(dt);
1864         struct inode *inode = obj->oo_inode;
1865
1866         LINVRNT(osd_invariant(obj));
1867         LASSERT(dt_object_exists(dt));
1868         LASSERT(osd_write_locked(env, obj));
1869         LASSERT(th != NULL);
1870
1871         spin_lock(&obj->oo_guard);
1872         LASSERT(inode->i_nlink > 0);
1873         inode->i_nlink--;
1874         spin_unlock(&obj->oo_guard);
1875         mark_inode_dirty(inode);
1876         LINVRNT(osd_invariant(obj));
1877 }
1878
1879 /*
1880  * Concurrency: @dt is read locked.
1881  */
1882 static int osd_xattr_get(const struct lu_env *env,
1883                          struct dt_object *dt,
1884                          struct lu_buf *buf,
1885                          const char *name,
1886                          struct lustre_capa *capa)
1887 {
1888         struct osd_object      *obj    = osd_dt_obj(dt);
1889         struct inode           *inode  = obj->oo_inode;
1890         struct osd_thread_info *info   = osd_oti_get(env);
1891         struct dentry          *dentry = &info->oti_obj_dentry;
1892
1893         LASSERT(dt_object_exists(dt));
1894         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1895         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1896
1897         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1898                 return -EACCES;
1899
1900         dentry->d_inode = inode;
1901         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1902 }
1903
1904
1905 /*
1906  * Concurrency: @dt is write locked.
1907  */
1908 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1909                          const struct lu_buf *buf, const char *name, int fl,
1910                          struct thandle *handle, struct lustre_capa *capa)
1911 {
1912         LASSERT(handle != NULL);
1913
1914         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1915                 return -EACCES;
1916
1917         return __osd_xattr_set(env, dt, buf, name, fl);
1918 }
1919
1920 /*
1921  * Concurrency: @dt is read locked.
1922  */
1923 static int osd_xattr_list(const struct lu_env *env,
1924                           struct dt_object *dt,
1925                           struct lu_buf *buf,
1926                           struct lustre_capa *capa)
1927 {
1928         struct osd_object      *obj    = osd_dt_obj(dt);
1929         struct inode           *inode  = obj->oo_inode;
1930         struct osd_thread_info *info   = osd_oti_get(env);
1931         struct dentry          *dentry = &info->oti_obj_dentry;
1932
1933         LASSERT(dt_object_exists(dt));
1934         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1935         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1936
1937         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1938                 return -EACCES;
1939
1940         dentry->d_inode = inode;
1941         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1942 }
1943
1944 /*
1945  * Concurrency: @dt is write locked.
1946  */
1947 static int osd_xattr_del(const struct lu_env *env,
1948                          struct dt_object *dt,
1949                          const char *name,
1950                          struct thandle *handle,
1951                          struct lustre_capa *capa)
1952 {
1953         struct osd_object      *obj    = osd_dt_obj(dt);
1954         struct inode           *inode  = obj->oo_inode;
1955         struct osd_thread_info *info   = osd_oti_get(env);
1956         struct dentry          *dentry = &info->oti_obj_dentry;
1957         struct timespec        *t      = &info->oti_time;
1958         int                     rc;
1959
1960         LASSERT(dt_object_exists(dt));
1961         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1962         LASSERT(osd_write_locked(env, obj));
1963         LASSERT(handle != NULL);
1964
1965         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1966                 return -EACCES;
1967
1968         dentry->d_inode = inode;
1969         *t = inode->i_ctime;
1970         rc = inode->i_op->removexattr(dentry, name);
1971         /* ctime should not be updated with server-side time. */
1972         spin_lock(&obj->oo_guard);
1973         inode->i_ctime = *t;
1974         spin_unlock(&obj->oo_guard);
1975         mark_inode_dirty(inode);
1976         return rc;
1977 }
1978
1979 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1980                                      struct dt_object *dt,
1981                                      struct lustre_capa *old,
1982                                      __u64 opc)
1983 {
1984         struct osd_thread_info *info = osd_oti_get(env);
1985         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1986         struct osd_object *obj = osd_dt_obj(dt);
1987         struct osd_device *dev = osd_obj2dev(obj);
1988         struct lustre_capa_key *key = &info->oti_capa_key;
1989         struct lustre_capa *capa = &info->oti_capa;
1990         struct obd_capa *oc;
1991         struct md_capainfo *ci;
1992         int rc;
1993         ENTRY;
1994
1995         if (!dev->od_fl_capa)
1996                 RETURN(ERR_PTR(-ENOENT));
1997
1998         LASSERT(dt_object_exists(dt));
1999         LINVRNT(osd_invariant(obj));
2000
2001         /* renewal sanity check */
2002         if (old && osd_object_auth(env, dt, old, opc))
2003                 RETURN(ERR_PTR(-EACCES));
2004
2005         ci = md_capainfo(env);
2006         if (unlikely(!ci))
2007                 RETURN(ERR_PTR(-ENOENT));
2008
2009         switch (ci->mc_auth) {
2010         case LC_ID_NONE:
2011                 RETURN(NULL);
2012         case LC_ID_PLAIN:
2013                 capa->lc_uid = obj->oo_inode->i_uid;
2014                 capa->lc_gid = obj->oo_inode->i_gid;
2015                 capa->lc_flags = LC_ID_PLAIN;
2016                 break;
2017         case LC_ID_CONVERT: {
2018                 __u32 d[4], s[4];
2019
2020                 s[0] = obj->oo_inode->i_uid;
2021                 get_random_bytes(&(s[1]), sizeof(__u32));
2022                 s[2] = obj->oo_inode->i_gid;
2023                 get_random_bytes(&(s[3]), sizeof(__u32));
2024                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2025                 if (unlikely(rc))
2026                         RETURN(ERR_PTR(rc));
2027
2028                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2029                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2030                 capa->lc_flags = LC_ID_CONVERT;
2031                 break;
2032         }
2033         default:
2034                 RETURN(ERR_PTR(-EINVAL));
2035         }
2036
2037         capa->lc_fid = *fid;
2038         capa->lc_opc = opc;
2039         capa->lc_flags |= dev->od_capa_alg << 24;
2040         capa->lc_timeout = dev->od_capa_timeout;
2041         capa->lc_expiry = 0;
2042
2043         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2044         if (oc) {
2045                 LASSERT(!capa_is_expired(oc));
2046                 RETURN(oc);
2047         }
2048
2049         spin_lock(&capa_lock);
2050         *key = dev->od_capa_keys[1];
2051         spin_unlock(&capa_lock);
2052
2053         capa->lc_keyid = key->lk_keyid;
2054         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2055
2056         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2057         if (rc) {
2058                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2059                 RETURN(ERR_PTR(rc));
2060         }
2061
2062         oc = capa_add(dev->od_capa_hash, capa);
2063         RETURN(oc);
2064 }
2065
2066 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2067 {
2068         int rc;
2069         struct osd_object      *obj    = osd_dt_obj(dt);
2070         struct inode           *inode  = obj->oo_inode;
2071         struct osd_thread_info *info   = osd_oti_get(env);
2072         struct dentry          *dentry = &info->oti_obj_dentry;
2073         struct file            *file   = &info->oti_file;
2074         ENTRY;
2075
2076         dentry->d_inode = inode;
2077         file->f_dentry = dentry;
2078         file->f_mapping = inode->i_mapping;
2079         file->f_op = inode->i_fop;
2080         LOCK_INODE_MUTEX(inode);
2081         rc = file->f_op->fsync(file, dentry, 0);
2082         UNLOCK_INODE_MUTEX(inode);
2083         RETURN(rc);
2084 }
2085
2086 /*
2087  * Get the 64-bit version for an inode.
2088  */
2089 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2090                                                struct dt_object *dt)
2091 {
2092         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2093
2094         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2095                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2096         return LDISKFS_I(inode)->i_fs_version;
2097 }
2098
2099 /*
2100  * Set the 64-bit version and return the old version.
2101  */
2102 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2103                                    dt_obj_version_t new_version)
2104 {
2105         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2106
2107         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2108                new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2109         LDISKFS_I(inode)->i_fs_version = new_version;
2110         /** Version is set after all inode operations are finished,
2111          *  so we should mark it dirty here */
2112         inode->i_sb->s_op->dirty_inode(inode);
2113 }
2114
2115 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2116                         void **data)
2117 {
2118         struct osd_object *obj = osd_dt_obj(dt);
2119         ENTRY;
2120
2121         *data = (void *)obj->oo_inode;
2122         RETURN(0);
2123 }
2124
2125 static const struct dt_object_operations osd_obj_ops = {
2126         .do_read_lock    = osd_object_read_lock,
2127         .do_write_lock   = osd_object_write_lock,
2128         .do_read_unlock  = osd_object_read_unlock,
2129         .do_write_unlock = osd_object_write_unlock,
2130         .do_attr_get     = osd_attr_get,
2131         .do_attr_set     = osd_attr_set,
2132         .do_ah_init      = osd_ah_init,
2133         .do_create       = osd_object_create,
2134         .do_index_try    = osd_index_try,
2135         .do_ref_add      = osd_object_ref_add,
2136         .do_ref_del      = osd_object_ref_del,
2137         .do_xattr_get    = osd_xattr_get,
2138         .do_xattr_set    = osd_xattr_set,
2139         .do_xattr_del    = osd_xattr_del,
2140         .do_xattr_list   = osd_xattr_list,
2141         .do_capa_get     = osd_capa_get,
2142         .do_object_sync  = osd_object_sync,
2143         .do_version_get  = osd_object_version_get,
2144         .do_version_set  = osd_object_version_set,
2145         .do_data_get     = osd_data_get,
2146 };
2147
2148 /**
2149  * dt_object_operations for interoperability mode
2150  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2151  */
2152 static const struct dt_object_operations osd_obj_ea_ops = {
2153         .do_read_lock    = osd_object_read_lock,
2154         .do_write_lock   = osd_object_write_lock,
2155         .do_read_unlock  = osd_object_read_unlock,
2156         .do_write_unlock = osd_object_write_unlock,
2157         .do_attr_get     = osd_attr_get,
2158         .do_attr_set     = osd_attr_set,
2159         .do_ah_init      = osd_ah_init,
2160         .do_create       = osd_object_ea_create,
2161         .do_index_try    = osd_index_try,
2162         .do_ref_add      = osd_object_ref_add,
2163         .do_ref_del      = osd_object_ref_del,
2164         .do_xattr_get    = osd_xattr_get,
2165         .do_xattr_set    = osd_xattr_set,
2166         .do_xattr_del    = osd_xattr_del,
2167         .do_xattr_list   = osd_xattr_list,
2168         .do_capa_get     = osd_capa_get,
2169         .do_object_sync  = osd_object_sync,
2170         .do_version_get  = osd_object_version_get,
2171         .do_version_set  = osd_object_version_set,
2172         .do_data_get     = osd_data_get,
2173 };
2174
2175 /*
2176  * Body operations.
2177  */
2178
2179 /*
2180  * XXX: Another layering violation for now.
2181  *
2182  * We don't want to use ->f_op->read methods, because generic file write
2183  *
2184  *         - serializes on ->i_sem, and
2185  *
2186  *         - does a lot of extra work like balance_dirty_pages(),
2187  *
2188  * which doesn't work for globally shared files like /last-received.
2189  */
2190 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2191 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2192                                 loff_t *offs, handle_t *handle);
2193
2194 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2195                         struct lu_buf *buf, loff_t *pos,
2196                         struct lustre_capa *capa)
2197 {
2198         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2199
2200         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2201                 RETURN(-EACCES);
2202
2203         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2204 }
2205
2206 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2207                          const struct lu_buf *buf, loff_t *pos,
2208                          struct thandle *handle, struct lustre_capa *capa,
2209                          int ignore_quota)
2210 {
2211         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
2212         struct osd_thandle *oh;
2213         ssize_t             result;
2214 #ifdef HAVE_QUOTA_SUPPORT
2215         cfs_cap_t           save = current->cap_effective;
2216 #endif
2217
2218         LASSERT(handle != NULL);
2219
2220         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2221                 RETURN(-EACCES);
2222
2223         oh = container_of(handle, struct osd_thandle, ot_super);
2224         LASSERT(oh->ot_handle->h_transaction != NULL);
2225 #ifdef HAVE_QUOTA_SUPPORT
2226         if (ignore_quota)
2227                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2228         else
2229                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2230 #endif
2231         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2232                                              pos, oh->ot_handle);
2233 #ifdef HAVE_QUOTA_SUPPORT
2234         current->cap_effective = save;
2235 #endif
2236         if (result == 0)
2237                 result = buf->lb_len;
2238         return result;
2239 }
2240
2241 static const struct dt_body_operations osd_body_ops = {
2242         .dbo_read  = osd_read,
2243         .dbo_write = osd_write
2244 };
2245
2246 /*
2247  * Index operations.
2248  */
2249
2250 static int osd_object_is_root(const struct osd_object *obj)
2251 {
2252         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2253 }
2254
2255 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2256                            const struct dt_index_features *feat)
2257 {
2258         struct iam_descr *descr;
2259         struct dt_object *dt = &o->oo_dt;
2260
2261         if (osd_object_is_root(o))
2262                 return feat == &dt_directory_features;
2263
2264         LASSERT(o->oo_dir != NULL);
2265
2266         descr = o->oo_dir->od_container.ic_descr;
2267         if (feat == &dt_directory_features) {
2268                 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2269                         return 1;
2270
2271                 if (descr == &iam_htree_compat_param) {
2272                         /* if it is a HTREE dir then there is good chance that,
2273                          * we dealing with ext3 directory here with no FIDs. */
2274
2275                         if (descr->id_rec_size ==
2276                             sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2277
2278                                 dt->do_index_ops = &osd_index_ea_ops;
2279                                 return 1;
2280                         }
2281                 }
2282                 return 0;
2283         } else {
2284                 return
2285                         feat->dif_keysize_min <= descr->id_key_size &&
2286                         descr->id_key_size <= feat->dif_keysize_max &&
2287                         feat->dif_recsize_min <= descr->id_rec_size &&
2288                         descr->id_rec_size <= feat->dif_recsize_max &&
2289                         !(feat->dif_flags & (DT_IND_VARKEY |
2290                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2291                         ergo(feat->dif_flags & DT_IND_UPDATE,
2292                              1 /* XXX check that object (and file system) is
2293                                 * writable */);
2294         }
2295 }
2296
2297 static int osd_iam_container_init(const struct lu_env *env,
2298                                   struct osd_object *obj,
2299                                   struct osd_directory *dir)
2300 {
2301         int result;
2302         struct iam_container *bag;
2303
2304         bag    = &dir->od_container;
2305         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2306         if (result == 0) {
2307                 result = iam_container_setup(bag);
2308                 if (result == 0)
2309                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2310                 else
2311                         iam_container_fini(bag);
2312         }
2313         return result;
2314 }
2315
2316 /*
2317  * Concurrency: no external locking is necessary.
2318  */
2319 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2320                          const struct dt_index_features *feat)
2321 {
2322         int result;
2323         int ea_dir = 0;
2324         struct osd_object *obj = osd_dt_obj(dt);
2325         struct osd_device *osd = osd_obj2dev(obj);
2326
2327         LINVRNT(osd_invariant(obj));
2328         LASSERT(dt_object_exists(dt));
2329
2330         if (osd_object_is_root(obj)) {
2331                 dt->do_index_ops = &osd_index_ea_ops;
2332                 result = 0;
2333         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2334                 dt->do_index_ops = &osd_index_ea_ops;
2335                 if (S_ISDIR(obj->oo_inode->i_mode))
2336                         result = 0;
2337                 else
2338                         result = -ENOTDIR;
2339                 ea_dir = 1;
2340         } else if (!osd_has_index(obj)) {
2341                 struct osd_directory *dir;
2342
2343                 OBD_ALLOC_PTR(dir);
2344                 if (dir != NULL) {
2345
2346                         spin_lock(&obj->oo_guard);
2347                         if (obj->oo_dir == NULL)
2348                                 obj->oo_dir = dir;
2349                         else
2350                                 /*
2351                                  * Concurrent thread allocated container data.
2352                                  */
2353                                 OBD_FREE_PTR(dir);
2354                         spin_unlock(&obj->oo_guard);
2355                         /*
2356                          * Now, that we have container data, serialize its
2357                          * initialization.
2358                          */
2359                         down_write(&obj->oo_ext_idx_sem);
2360                         /*
2361                          * recheck under lock.
2362                          */
2363                         if (!osd_has_index(obj))
2364                                 result = osd_iam_container_init(env, obj, dir);
2365                         else
2366                                 result = 0;
2367                         up_write(&obj->oo_ext_idx_sem);
2368                 } else
2369                         result = -ENOMEM;
2370         } else
2371                 result = 0;
2372
2373         if (result == 0 && ea_dir == 0) {
2374                 if (!osd_iam_index_probe(env, obj, feat))
2375                         result = -ENOTDIR;
2376         }
2377         LINVRNT(osd_invariant(obj));
2378
2379         return result;
2380 }
2381
2382 /**
2383  *      delete a (key, value) pair from index \a dt specified by \a key
2384  *
2385  *      \param  dt_object      osd index object
2386  *      \param  key     key for index
2387  *      \param  rec     record reference
2388  *      \param  handle  transaction handler
2389  *
2390  *      \retval  0  success
2391  *      \retval -ve   failure
2392  */
2393
2394 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2395                                 const struct dt_key *key, struct thandle *handle,
2396                                 struct lustre_capa *capa)
2397 {
2398         struct osd_object     *obj = osd_dt_obj(dt);
2399         struct osd_thandle    *oh;
2400         struct iam_path_descr *ipd;
2401         struct iam_container  *bag = &obj->oo_dir->od_container;
2402         int rc;
2403
2404         ENTRY;
2405
2406         LINVRNT(osd_invariant(obj));
2407         LASSERT(dt_object_exists(dt));
2408         LASSERT(bag->ic_object == obj->oo_inode);
2409         LASSERT(handle != NULL);
2410
2411         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2412                 RETURN(-EACCES);
2413
2414         ipd = osd_idx_ipd_get(env, bag);
2415         if (unlikely(ipd == NULL))
2416                 RETURN(-ENOMEM);
2417
2418         oh = container_of0(handle, struct osd_thandle, ot_super);
2419         LASSERT(oh->ot_handle != NULL);
2420         LASSERT(oh->ot_handle->h_transaction != NULL);
2421
2422         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2423         osd_ipd_put(env, bag, ipd);
2424         LINVRNT(osd_invariant(obj));
2425         RETURN(rc);
2426 }
2427
2428 /**
2429  * Index delete function for interoperability mode (b11826).
2430  * It will remove the directory entry added by osd_index_ea_insert().
2431  * This entry is needed to maintain name->fid mapping.
2432  *
2433  * \param key,  key i.e. file entry to be deleted
2434  *
2435  * \retval   0, on success
2436  * \retval -ve, on error
2437  */
2438 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2439                                const struct dt_key *key, struct thandle *handle,
2440                                struct lustre_capa *capa)
2441 {
2442         struct osd_object          *obj    = osd_dt_obj(dt);
2443         struct inode               *dir    = obj->oo_inode;
2444         struct dentry              *dentry;
2445         struct osd_thandle         *oh;
2446         struct ldiskfs_dir_entry_2 *de;
2447         struct buffer_head         *bh;
2448
2449         int rc;
2450
2451         ENTRY;
2452
2453         LINVRNT(osd_invariant(obj));
2454         LASSERT(dt_object_exists(dt));
2455         LASSERT(handle != NULL);
2456
2457         oh = container_of(handle, struct osd_thandle, ot_super);
2458         LASSERT(oh->ot_handle != NULL);
2459         LASSERT(oh->ot_handle->h_transaction != NULL);
2460
2461         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2462                 RETURN(-EACCES);
2463
2464         dentry = osd_child_dentry_get(env, obj,
2465                                       (char *)key, strlen((char *)key));
2466
2467         down_write(&obj->oo_ext_idx_sem);
2468         bh = ldiskfs_find_entry(dentry, &de);
2469         if (bh) {
2470                 struct osd_thread_info *oti = osd_oti_get(env);
2471                 struct timespec *ctime = &oti->oti_time;
2472                 struct timespec *mtime = &oti->oti_time2;
2473
2474                 *ctime = dir->i_ctime;
2475                 *mtime = dir->i_mtime;
2476                 rc = ldiskfs_delete_entry(oh->ot_handle,
2477                                 dir, de, bh);
2478                 /* xtime should not be updated with server-side time. */
2479                 spin_lock(&obj->oo_guard);
2480                 dir->i_ctime = *ctime;
2481                 dir->i_mtime = *mtime;
2482                 spin_unlock(&obj->oo_guard);
2483                 mark_inode_dirty(dir);
2484                 brelse(bh);
2485         } else
2486                 rc = -ENOENT;
2487
2488         up_write(&obj->oo_ext_idx_sem);
2489         LASSERT(osd_invariant(obj));
2490         RETURN(rc);
2491 }
2492
2493 /**
2494  *      Lookup index for \a key and copy record to \a rec.
2495  *
2496  *      \param  dt_object      osd index object
2497  *      \param  key     key for index
2498  *      \param  rec     record reference
2499  *
2500  *      \retval  +ve  success : exact mach
2501  *      \retval  0    return record with key not greater than \a key
2502  *      \retval -ve   failure
2503  */
2504 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2505                                 struct dt_rec *rec, const struct dt_key *key,
2506                                 struct lustre_capa *capa)
2507 {
2508         struct osd_object     *obj = osd_dt_obj(dt);
2509         struct iam_path_descr *ipd;
2510         struct iam_container  *bag = &obj->oo_dir->od_container;
2511         struct osd_thread_info *oti = osd_oti_get(env);
2512         struct iam_iterator    *it = &oti->oti_idx_it;
2513         int rc;
2514         ENTRY;
2515
2516         LASSERT(osd_invariant(obj));
2517         LASSERT(dt_object_exists(dt));
2518         LASSERT(bag->ic_object == obj->oo_inode);
2519
2520         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2521                 RETURN(-EACCES);
2522
2523         ipd = osd_idx_ipd_get(env, bag);
2524         if (IS_ERR(ipd))
2525                 RETURN(-ENOMEM);
2526
2527         /* got ipd now we can start iterator. */
2528         iam_it_init(it, bag, 0, ipd);
2529
2530         rc = iam_it_get(it, (struct iam_key *)key);
2531         if (rc >= 0)
2532                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2533
2534         iam_it_put(it);
2535         iam_it_fini(it);
2536         osd_ipd_put(env, bag, ipd);
2537
2538         LINVRNT(osd_invariant(obj));
2539
2540         RETURN(rc);
2541 }
2542
2543 /**
2544  *      Inserts (key, value) pair in \a dt index object.
2545  *
2546  *      \param  dt      osd index object
2547  *      \param  key     key for index
2548  *      \param  rec     record reference
2549  *      \param  th      transaction handler
2550  *
2551  *      \retval  0  success
2552  *      \retval -ve failure
2553  */
2554 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2555                                 const struct dt_rec *rec, const struct dt_key *key,
2556                                 struct thandle *th, struct lustre_capa *capa,
2557                                 int ignore_quota)
2558 {
2559         struct osd_object     *obj = osd_dt_obj(dt);
2560         struct iam_path_descr *ipd;
2561         struct osd_thandle    *oh;
2562         struct iam_container  *bag = &obj->oo_dir->od_container;
2563 #ifdef HAVE_QUOTA_SUPPORT
2564         cfs_cap_t              save = current->cap_effective;
2565 #endif
2566         int rc;
2567
2568         ENTRY;
2569
2570         LINVRNT(osd_invariant(obj));
2571         LASSERT(dt_object_exists(dt));
2572         LASSERT(bag->ic_object == obj->oo_inode);
2573         LASSERT(th != NULL);
2574
2575         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2576                 return -EACCES;
2577
2578         ipd = osd_idx_ipd_get(env, bag);
2579         if (unlikely(ipd == NULL))
2580                 RETURN(-ENOMEM);
2581
2582         oh = container_of0(th, struct osd_thandle, ot_super);
2583         LASSERT(oh->ot_handle != NULL);
2584         LASSERT(oh->ot_handle->h_transaction != NULL);
2585 #ifdef HAVE_QUOTA_SUPPORT
2586         if (ignore_quota)
2587                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2588         else
2589                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2590 #endif
2591         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2592                         (struct iam_rec *)rec, ipd);
2593 #ifdef HAVE_QUOTA_SUPPORT
2594         current->cap_effective = save;
2595 #endif
2596         osd_ipd_put(env, bag, ipd);
2597         LINVRNT(osd_invariant(obj));
2598         RETURN(rc);
2599 }
2600
2601 /**
2602  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2603  * into the directory.Also sets flags into osd object to
2604  * indicate dot and dotdot are created. This is required for
2605  * interoperability mode (b11826)
2606  *
2607  * \param dir   directory for dot and dotdot fixup.
2608  * \param obj   child object for linking
2609  *
2610  * \retval   0, on success
2611  * \retval -ve, on error
2612  */
2613 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2614                               struct osd_object *dir,
2615                               struct osd_object *obj, const char *name,
2616                               struct thandle *th)
2617 {
2618         struct inode            *parent_dir   = obj->oo_inode;
2619         struct inode            *inode  = dir->oo_inode;
2620         struct osd_thandle      *oth;
2621         int result = 0;
2622
2623         oth = container_of(th, struct osd_thandle, ot_super);
2624         LASSERT(oth->ot_handle->h_transaction != NULL);
2625         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2626
2627         if (strcmp(name, dot) == 0) {
2628                 if (dir->oo_compat_dot_created) {
2629                         result = -EEXIST;
2630                 } else {
2631                         LASSERT(obj == dir);
2632                         dir->oo_compat_dot_created = 1;
2633                         result = 0;
2634                 }
2635         } else if(strcmp(name, dotdot) == 0) {
2636                 if (!dir->oo_compat_dot_created)
2637                         return -EINVAL;
2638                 if (dir->oo_compat_dotdot_created)
2639                         return __osd_ea_add_rec(info, dir, obj, name, th);
2640
2641                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2642                 if (result == 0)
2643                        dir->oo_compat_dotdot_created = 1;
2644         }
2645
2646         return result;
2647 }
2648
2649 /**
2650  * Calls ldiskfs_add_entry() to add directory entry
2651  * into the directory. This is required for
2652  * interoperability mode (b11826)
2653  *
2654  * \retval   0, on success
2655  * \retval -ve, on error
2656  */
2657 static int __osd_ea_add_rec(struct osd_thread_info *info,
2658                             struct osd_object *pobj,
2659                             struct osd_object *cobj,
2660                             const char *name,
2661                             struct thandle *th)
2662 {
2663         struct dentry      *child;
2664         struct osd_thandle *oth;
2665         struct inode       *cinode  = cobj->oo_inode;
2666         int rc;
2667
2668         oth = container_of(th, struct osd_thandle, ot_super);
2669         LASSERT(oth->ot_handle != NULL);
2670         LASSERT(oth->ot_handle->h_transaction != NULL);
2671
2672         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2673         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2674
2675         RETURN(rc);
2676 }
2677
2678 /**
2679  * It will call the appropriate osd_add* function and return the
2680  * value, return by respective functions.
2681  */
2682 static int osd_ea_add_rec(const struct lu_env *env,
2683                           struct osd_object *pobj,
2684                           struct osd_object *cobj,
2685                           const char *name,
2686                           struct thandle *th)
2687 {
2688         struct osd_thread_info    *info   = osd_oti_get(env);
2689         int rc;
2690
2691         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2692                                                    name[2] =='\0')))
2693                 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2694         else
2695                 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2696
2697         return rc;
2698 }
2699
2700 /**
2701  * Calls ->lookup() to find dentry. From dentry get inode and
2702  * read inode's ea to get fid. This is required for  interoperability
2703  * mode (b11826)
2704  *
2705  * \retval   0, on success
2706  * \retval -ve, on error
2707  */
2708 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2709                              struct dt_rec *rec, const struct dt_key *key)
2710 {
2711         struct inode            *dir    = obj->oo_inode;
2712         struct osd_thread_info  *info   = osd_oti_get(env);
2713         struct dentry           *dentry;
2714         struct osd_device      *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2715         struct osd_inode_id    *id     = &info->oti_id;
2716         struct ldiskfs_dir_entry_2 *de;
2717         struct buffer_head         *bh;
2718         struct inode *inode;
2719         int ino;
2720         int rc;
2721
2722         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2723
2724         dentry = osd_child_dentry_get(env, obj,
2725                                       (char *)key, strlen((char *)key));
2726
2727         down_read(&obj->oo_ext_idx_sem);
2728         bh = ldiskfs_find_entry(dentry, &de);
2729         if (bh) {
2730                 ino = le32_to_cpu(de->inode);
2731                 brelse(bh);
2732                 id->oii_ino = ino;
2733                 id->oii_gen = OSD_OII_NOGEN;
2734
2735                 inode = osd_iget(info, dev, id);
2736                 if (!IS_ERR(inode)) {
2737                         dentry->d_inode = inode;
2738
2739                         rc = osd_ea_fid_get(env, dentry, rec);
2740                         iput(inode);
2741                 } else
2742                         rc = PTR_ERR(inode);
2743         } else
2744                 rc = -ENOENT;
2745
2746         up_read(&obj->oo_ext_idx_sem);
2747         RETURN (rc);
2748 }
2749
2750 /**
2751  * Find the osd object for given fid.
2752  *
2753  * \param fid, need to find the osd object having this fid
2754  *
2755  * \retval osd_object, on success
2756  * \retval        -ve, on error
2757  */
2758 struct osd_object *osd_object_find(const struct lu_env *env,
2759                                    struct dt_object *dt,
2760                                    const struct lu_fid *fid)
2761 {
2762         struct lu_device         *ludev = dt->do_lu.lo_dev;
2763         struct osd_object        *child = NULL;
2764         struct lu_object         *luch;
2765         struct lu_object         *lo;
2766
2767         luch = lu_object_find(env, ludev, fid, NULL);
2768         if (!IS_ERR(luch)) {
2769                 if (lu_object_exists(luch)) {
2770                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2771                         if (lo != NULL)
2772                                 child = osd_obj(lo);
2773                         else
2774                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2775                                                 "lu_object can't be located"
2776                                                 ""DFID"\n", PFID(fid));
2777
2778                         if (child == NULL) {
2779                                 lu_object_put(env, luch);
2780                                 CERROR("Unable to get osd_object\n");
2781                                 child = ERR_PTR(-ENOENT);
2782                         }
2783                 } else {
2784                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
2785                                         "lu_object does not exists "DFID"\n",
2786                                         PFID(fid));
2787                         child = ERR_PTR(-ENOENT);
2788                 }
2789         } else
2790                 child = (void *)luch;
2791
2792         return child;
2793 }
2794
2795 /**
2796  * Put the osd object once done with it.
2797  *
2798  * \param obj, osd object that needs to be put
2799  */
2800 static inline void osd_object_put(const struct lu_env *env,
2801                                   struct osd_object *obj)
2802 {
2803         lu_object_put(env, &obj->oo_dt.do_lu);
2804 }
2805
2806 /**
2807  * Index add function for interoperability mode (b11826).
2808  * It will add the directory entry.This entry is needed to
2809  * maintain name->fid mapping.
2810  *
2811  * \param key, it is key i.e. file entry to be inserted
2812  * \param rec, it is value of given key i.e. fid
2813  *
2814  * \retval   0, on success
2815  * \retval -ve, on error
2816  */
2817 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2818                                const struct dt_rec *rec,
2819                                const struct dt_key *key, struct thandle *th,
2820                                struct lustre_capa *capa, int ignore_quota)
2821 {
2822         struct osd_object        *obj   = osd_dt_obj(dt);
2823         struct lu_fid            *fid   = &osd_oti_get(env)->oti_fid;
2824         const struct lu_fid_pack *pack  = (const struct lu_fid_pack *)rec;
2825         const char               *name  = (const char *)key;
2826         struct osd_object        *child;
2827 #ifdef HAVE_QUOTA_SUPPORT
2828         cfs_cap_t                 save  = current->cap_effective;
2829 #endif
2830         int rc;
2831
2832         ENTRY;
2833
2834         LASSERT(osd_invariant(obj));
2835         LASSERT(dt_object_exists(dt));
2836         LASSERT(th != NULL);
2837
2838         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2839                 RETURN(-EACCES);
2840
2841         rc = fid_unpack(pack, fid);
2842         if (rc != 0)
2843                 RETURN(rc);
2844         child = osd_object_find(env, dt, fid);
2845         if (!IS_ERR(child)) {
2846                 struct inode *inode = obj->oo_inode;
2847                 struct osd_thread_info *oti = osd_oti_get(env);
2848                 struct timespec *ctime = &oti->oti_time;
2849                 struct timespec *mtime = &oti->oti_time2;
2850
2851                 *ctime = inode->i_ctime;
2852                 *mtime = inode->i_mtime;
2853 #ifdef HAVE_QUOTA_SUPPORT
2854                 if (ignore_quota)
2855                         current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2856                 else
2857                         current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2858 #endif
2859                 down_write(&obj->oo_ext_idx_sem);
2860                 rc = osd_ea_add_rec(env, obj, child, name, th);
2861                 up_write(&obj->oo_ext_idx_sem);
2862 #ifdef HAVE_QUOTA_SUPPORT
2863                 current->cap_effective = save;
2864 #endif
2865                 osd_object_put(env, child);
2866                 /* xtime should not be updated with server-side time. */
2867                 spin_lock(&obj->oo_guard);
2868                 inode->i_ctime = *ctime;
2869                 inode->i_mtime = *mtime;
2870                 spin_unlock(&obj->oo_guard);
2871                 mark_inode_dirty(inode);
2872         } else {
2873                 rc = PTR_ERR(child);
2874         }
2875
2876         LASSERT(osd_invariant(obj));
2877         RETURN(rc);
2878 }
2879
2880 /**
2881  *  Initialize osd Iterator for given osd index object.
2882  *
2883  *  \param  dt      osd index object
2884  */
2885
2886 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2887                                  struct dt_object *dt,
2888                                  struct lustre_capa *capa)
2889 {
2890         struct osd_it_iam         *it;
2891         struct osd_thread_info *oti = osd_oti_get(env);
2892         struct osd_object     *obj = osd_dt_obj(dt);
2893         struct lu_object      *lo  = &dt->do_lu;
2894         struct iam_path_descr *ipd;
2895         struct iam_container  *bag = &obj->oo_dir->od_container;
2896
2897         LASSERT(lu_object_exists(lo));
2898
2899         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2900                 return ERR_PTR(-EACCES);
2901
2902         it = &oti->oti_it;
2903         ipd = osd_it_ipd_get(env, bag);
2904         if (likely(ipd != NULL)) {
2905                 it->oi_obj = obj;
2906                 it->oi_ipd = ipd;
2907                 lu_object_get(lo);
2908                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2909                 return (struct dt_it *)it;
2910         }
2911         return ERR_PTR(-ENOMEM);
2912 }
2913
2914 /**
2915  * free given Iterator.
2916  */
2917
2918 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2919 {
2920         struct osd_it_iam     *it = (struct osd_it_iam *)di;
2921         struct osd_object *obj = it->oi_obj;
2922
2923         iam_it_fini(&it->oi_it);
2924         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2925         lu_object_put(env, &obj->oo_dt.do_lu);
2926 }
2927
2928 /**
2929  *  Move Iterator to record specified by \a key
2930  *
2931  *  \param  di      osd iterator
2932  *  \param  key     key for index
2933  *
2934  *  \retval +ve  di points to record with least key not larger than key
2935  *  \retval  0   di points to exact matched key
2936  *  \retval -ve  failure
2937  */
2938
2939 static int osd_it_iam_get(const struct lu_env *env,
2940                       struct dt_it *di, const struct dt_key *key)
2941 {
2942         struct osd_it_iam *it = (struct osd_it_iam *)di;
2943
2944         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2945 }
2946
2947 /**
2948  *  Release Iterator
2949  *
2950  *  \param  di      osd iterator
2951  */
2952
2953 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2954 {
2955         struct osd_it_iam *it = (struct osd_it_iam *)di;
2956
2957         iam_it_put(&it->oi_it);
2958 }
2959
2960 /**
2961  *  Move iterator by one record
2962  *
2963  *  \param  di      osd iterator
2964  *
2965  *  \retval +1   end of container reached
2966  *  \retval  0   success
2967  *  \retval -ve  failure
2968  */
2969
2970 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2971 {
2972         struct osd_it_iam *it = (struct osd_it_iam *)di;
2973
2974         return iam_it_next(&it->oi_it);
2975 }
2976
2977 /**
2978  * Return pointer to the key under iterator.
2979  */
2980
2981 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2982                                  const struct dt_it *di)
2983 {
2984         struct osd_it_iam *it = (struct osd_it_iam *)di;
2985
2986         return (struct dt_key *)iam_it_key_get(&it->oi_it);
2987 }
2988
2989 /**
2990  * Return size of key under iterator (in bytes)
2991  */
2992
2993 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2994 {
2995         struct osd_it_iam *it = (struct osd_it_iam *)di;
2996
2997         return iam_it_key_size(&it->oi_it);
2998 }
2999
3000 /**
3001  * Return pointer to the record under iterator.
3002  */
3003 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
3004                                  const struct dt_it *di)
3005 {
3006         struct osd_it_iam *it = (struct osd_it_iam *)di;
3007
3008         return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3009 }
3010
3011 /**
3012  * Returns cookie for current Iterator position.
3013  */
3014 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3015 {
3016         struct osd_it_iam *it = (struct osd_it_iam *)di;
3017
3018         return iam_it_store(&it->oi_it);
3019 }
3020
3021 /**
3022  * Restore iterator from cookie.
3023  *
3024  * \param  di      osd iterator
3025  * \param  hash    Iterator location cookie
3026  *
3027  * \retval +ve  di points to record with least key not larger than key.
3028  * \retval  0   di points to exact matched key
3029  * \retval -ve  failure
3030  */
3031
3032 static int osd_it_iam_load(const struct lu_env *env,
3033                        const struct dt_it *di, __u64 hash)
3034 {
3035         struct osd_it_iam *it = (struct osd_it_iam *)di;
3036
3037         return iam_it_load(&it->oi_it, hash);
3038 }
3039
3040 static const struct dt_index_operations osd_index_iam_ops = {
3041         .dio_lookup = osd_index_iam_lookup,
3042         .dio_insert = osd_index_iam_insert,
3043         .dio_delete = osd_index_iam_delete,
3044         .dio_it     = {
3045                 .init     = osd_it_iam_init,
3046                 .fini     = osd_it_iam_fini,
3047                 .get      = osd_it_iam_get,
3048                 .put      = osd_it_iam_put,
3049                 .next     = osd_it_iam_next,
3050                 .key      = osd_it_iam_key,
3051                 .key_size = osd_it_iam_key_size,
3052                 .rec      = osd_it_iam_rec,
3053                 .store    = osd_it_iam_store,
3054                 .load     = osd_it_iam_load
3055         }
3056 };
3057
3058 /**
3059  * Creates or initializes iterator context.
3060  *
3061  * \retval struct osd_it_ea, iterator structure on success
3062  *
3063  */
3064 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3065                                     struct dt_object *dt,
3066                                     struct lustre_capa *capa)
3067 {
3068         struct osd_object       *obj  = osd_dt_obj(dt);
3069         struct osd_thread_info  *info = osd_oti_get(env);
3070         struct osd_it_ea        *it   = &info->oti_it_ea;
3071         struct lu_object        *lo   = &dt->do_lu;
3072         struct dentry           *obj_dentry = &info->oti_it_dentry;
3073         ENTRY;
3074         LASSERT(lu_object_exists(lo));
3075
3076         obj_dentry->d_inode = obj->oo_inode;
3077         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3078         obj_dentry->d_name.hash = 0;
3079
3080         it->oie_rd_dirent       = 0;
3081         it->oie_it_dirent       = 0;
3082         it->oie_curr_pos        = 0;
3083         it->oie_next_pos        = 0;
3084         it->oie_dirent          = NULL;
3085         it->oie_buf             = info->oti_it_ea_buf;
3086         it->oie_obj             = obj;
3087         it->oie_file.f_dentry   = obj_dentry;
3088         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3089         it->oie_file.f_op         = obj->oo_inode->i_fop;
3090         it->oie_file.private_data = NULL;
3091         lu_object_get(lo);
3092         RETURN((struct dt_it*) it);
3093 }
3094
3095 /**
3096  * Destroy or finishes iterator context.
3097  *
3098  * \param di, struct osd_it_ea, iterator structure to be destroyed
3099  */
3100 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3101 {
3102         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3103         struct osd_object    *obj  = it->oie_obj;
3104         struct inode       *inode  = obj->oo_inode;
3105
3106         ENTRY;
3107         it->oie_file.f_op->release(inode, &it->oie_file);
3108         lu_object_put(env, &obj->oo_dt.do_lu);
3109         EXIT;
3110 }
3111
3112 /**
3113  * It position the iterator at given key, so that next lookup continues from
3114  * that key Or it is similar to dio_it->load() but based on a key,
3115  * rather than file position.
3116  *
3117  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3118  * to the beginning.
3119  *
3120  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3121  */
3122 static int osd_it_ea_get(const struct lu_env *env,
3123                          struct dt_it *di, const struct dt_key *key)
3124 {
3125         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3126
3127         ENTRY;
3128         LASSERT(((const char *)key)[0] == '\0');
3129         it->oie_curr_pos        = 0;
3130         it->oie_next_pos        = 0;
3131         it->oie_rd_dirent       = 0;
3132         it->oie_it_dirent       = 0;
3133         it->oie_dirent          = NULL;
3134
3135         RETURN(+1);
3136 }
3137
3138 /**
3139  * Does nothing
3140  */
3141 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3142 {
3143 }
3144
3145 /**
3146  * It is called internally by ->readdir(). It fills the
3147  * iterator's in-memory data structure with required
3148  * information i.e. name, namelen, rec_size etc.
3149  *
3150  * \param buf, in which information to be filled in.
3151  * \param name, name of the file in given dir
3152  *
3153  * \retval 0, on success
3154  * \retval 1, on buffer full
3155  */
3156 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3157                                loff_t offset, ino_t ino,
3158                                unsigned int d_type)
3159 {
3160         struct osd_it_ea        *it = (struct osd_it_ea *)buf;
3161         struct osd_it_ea_dirent *ent = it->oie_dirent;
3162         ENTRY;
3163
3164         /* this should never happen */
3165         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3166                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3167                 RETURN(-EIO);
3168         }
3169
3170         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3171             OSD_IT_EA_BUFSIZE)
3172                 RETURN(1);
3173
3174         ent->oied_ino     = ino;
3175         ent->oied_off     = offset;
3176         ent->oied_namelen = namelen;
3177         memcpy(ent->oied_name, name, namelen);
3178
3179         it->oie_rd_dirent++;
3180         it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3181         RETURN(0);
3182 }
3183
3184 /**
3185  * Calls ->readdir() to load a directory entry at a time
3186  * and stored it in iterator's in-memory data structure.
3187  *
3188  * \param di, struct osd_it_ea, iterator's in memory structure
3189  *
3190  * \retval   0, on success
3191  * \retval -ve, on error
3192  */
3193 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3194 {
3195         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3196         struct osd_object  *obj   = it->oie_obj;
3197         struct inode       *inode = obj->oo_inode;
3198         int                result = 0;
3199
3200         ENTRY;
3201         it->oie_dirent = it->oie_buf;
3202         it->oie_rd_dirent = 0;
3203         it->oie_file.f_pos = it->oie_curr_pos;
3204
3205         down_read(&obj->oo_ext_idx_sem);
3206         result = inode->i_fop->readdir(&it->oie_file, it,
3207                                        (filldir_t) osd_ldiskfs_filldir);
3208
3209         up_read(&obj->oo_ext_idx_sem);
3210         it->oie_next_pos = it->oie_file.f_pos;
3211
3212         if (it->oie_rd_dirent == 0) {
3213                 result = -EIO;
3214         } else {
3215                 it->oie_dirent = it->oie_buf;
3216                 it->oie_it_dirent = 1;
3217         }
3218
3219         RETURN(result);
3220 }
3221
3222 /**
3223  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3224  * to load a directory entry at a time and stored it in
3225  * iterator's in-memory data structure.
3226  *
3227  * \param di, struct osd_it_ea, iterator's in memory structure
3228  *
3229  * \retval +ve, iterator reached to end
3230  * \retval   0, iterator not reached to end
3231  * \retval -ve, on error
3232  */
3233 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3234 {
3235         struct osd_it_ea *it = (struct osd_it_ea *)di;
3236         int rc;
3237
3238         ENTRY;
3239
3240         if (it->oie_it_dirent < it->oie_rd_dirent) {
3241                 it->oie_dirent = (void *) it->oie_dirent +
3242                                  size_round(sizeof(struct osd_it_ea_dirent) +
3243                                             it->oie_dirent->oied_namelen);
3244                 it->oie_it_dirent++;
3245                 RETURN(0);
3246         } else {
3247                 it->oie_curr_pos = it->oie_next_pos;
3248
3249                 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3250                         rc = +1;
3251                 else
3252                         rc = osd_ldiskfs_it_fill(di);
3253         }
3254
3255         RETURN(rc);
3256 }
3257
3258 /**
3259  * Returns the key at current position from iterator's in memory structure.
3260  *
3261  * \param di, struct osd_it_ea, iterator's in memory structure
3262  *
3263  * \retval key i.e. struct dt_key on success
3264  */
3265 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3266                                     const struct dt_it *di)
3267 {
3268         struct osd_it_ea *it = (struct osd_it_ea *)di;
3269         ENTRY;
3270         RETURN((struct dt_key *)it->oie_dirent->oied_name);
3271 }
3272
3273 /**
3274  * Returns the key's size at current position from iterator's in memory structure.
3275  *
3276  * \param di, struct osd_it_ea, iterator's in memory structure
3277  *
3278  * \retval key_size i.e. struct dt_key on success
3279  */
3280 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3281 {
3282         struct osd_it_ea *it = (struct osd_it_ea *)di;
3283         ENTRY;
3284         RETURN(it->oie_dirent->oied_namelen);
3285 }
3286
3287 /**
3288  * Returns the value (i.e. fid/igif) at current position from iterator's
3289  * in memory structure.
3290  *
3291  * \param di, struct osd_it_ea, iterator's in memory structure
3292  *
3293  * \retval value i.e. struct dt_rec on success
3294  */
3295 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3296                                     const struct dt_it *di)
3297 {
3298         struct osd_it_ea       *it     = (struct osd_it_ea *)di;
3299         struct osd_object      *obj    = it->oie_obj;
3300         struct osd_thread_info *info   = osd_oti_get(env);
3301         struct osd_inode_id    *id     = &info->oti_id;
3302         struct lu_fid_pack     *rec    = &info->oti_pack;
3303         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
3304         struct dentry          *dentry = &info->oti_child_dentry;
3305         struct osd_device      *dev;
3306         struct inode           *inode;
3307         int                    rc;
3308
3309         ENTRY;
3310         dev  = osd_dev(ldev);
3311         id->oii_ino = it->oie_dirent->oied_ino;
3312         id->oii_gen = OSD_OII_NOGEN;
3313         inode = osd_iget(info, dev, id);
3314         if (!IS_ERR(inode)) {
3315                 dentry->d_inode = inode;
3316                 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3317         } else {
3318                 RETURN((struct dt_rec *) PTR_ERR(inode));
3319         }
3320
3321         rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3322         if (rc != 0)
3323                 rec = ERR_PTR(rc);
3324
3325         iput(inode);
3326         RETURN((struct dt_rec *)rec);
3327
3328 }
3329
3330 /**
3331  * Returns a cookie for current position of the iterator head, so that
3332  * user can use this cookie to load/start the iterator next time.
3333  *
3334  * \param di, struct osd_it_ea, iterator's in memory structure
3335  *
3336  * \retval cookie for current position, on success
3337  */
3338 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3339 {
3340         struct osd_it_ea *it = (struct osd_it_ea *)di;
3341         ENTRY;
3342         RETURN(it->oie_dirent->oied_off);
3343 }
3344
3345 /**
3346  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3347  * to load a directory entry at a time and stored it i inn,
3348  * in iterator's in-memory data structure.
3349  *
3350  * \param di, struct osd_it_ea, iterator's in memory structure
3351  *
3352  * \retval +ve, on success
3353  * \retval -ve, on error
3354  */
3355 static int osd_it_ea_load(const struct lu_env *env,
3356                           const struct dt_it *di, __u64 hash)
3357 {
3358         struct osd_it_ea *it = (struct osd_it_ea *)di;
3359         int rc;
3360
3361         ENTRY;
3362         it->oie_curr_pos = hash;
3363
3364         rc =  osd_ldiskfs_it_fill(di);
3365         if (rc == 0)
3366                 rc = +1;
3367
3368         RETURN(rc);
3369 }
3370 /**
3371  * Index and Iterator operations for interoperability
3372  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3373  */
3374 static const struct dt_index_operations osd_index_ea_ops = {
3375         .dio_lookup = osd_index_ea_lookup,
3376         .dio_insert = osd_index_ea_insert,
3377         .dio_delete = osd_index_ea_delete,
3378         .dio_it     = {
3379                 .init     = osd_it_ea_init,
3380                 .fini     = osd_it_ea_fini,
3381                 .get      = osd_it_ea_get,
3382                 .put      = osd_it_ea_put,
3383                 .next     = osd_it_ea_next,
3384                 .key      = osd_it_ea_key,
3385                 .key_size = osd_it_ea_key_size,
3386                 .rec      = osd_it_ea_rec,
3387                 .store    = osd_it_ea_store,
3388                 .load     = osd_it_ea_load
3389         }
3390 };
3391
3392 /**
3393  * Index lookup function for interoperability mode (b11826).
3394  *
3395  * \param key,  key i.e. file name to be searched
3396  *
3397  * \retval +ve, on success
3398  * \retval -ve, on error
3399  */
3400 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3401                                struct dt_rec *rec, const struct dt_key *key,
3402                                struct lustre_capa *capa)
3403 {
3404         struct osd_object *obj = osd_dt_obj(dt);
3405         int rc = 0;
3406
3407         ENTRY;
3408
3409         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3410         LINVRNT(osd_invariant(obj));
3411
3412         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3413                 return -EACCES;
3414
3415         rc = osd_ea_lookup_rec(env, obj, rec, key);
3416
3417         if (rc == 0)
3418                 rc = +1;
3419         RETURN(rc);
3420 }
3421
3422 /* type constructor/destructor: osd_type_init, osd_type_fini */
3423 LU_TYPE_INIT_FINI(osd, &osd_key);
3424
3425 static struct lu_context_key osd_key = {
3426         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3427         .lct_init = osd_key_init,
3428         .lct_fini = osd_key_fini,
3429         .lct_exit = osd_key_exit
3430 };
3431
3432 static void *osd_key_init(const struct lu_context *ctx,
3433                           struct lu_context_key *key)
3434 {
3435         struct osd_thread_info *info;
3436
3437         OBD_ALLOC_PTR(info);
3438         if (info != NULL) {
3439                 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3440                 if (info->oti_it_ea_buf != NULL) {
3441                         info->oti_env = container_of(ctx, struct lu_env,
3442                                                      le_ctx);
3443                 } else {
3444                         OBD_FREE_PTR(info);
3445                         info = ERR_PTR(-ENOMEM);
3446                 }
3447         } else {
3448                 info = ERR_PTR(-ENOMEM);
3449         }
3450         return info;
3451 }
3452
3453 static void osd_key_fini(const struct lu_context *ctx,
3454                          struct lu_context_key *key, void* data)
3455 {
3456         struct osd_thread_info *info = data;
3457
3458         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3459         OBD_FREE_PTR(info);
3460 }
3461
3462 static void osd_key_exit(const struct lu_context *ctx,
3463                          struct lu_context_key *key, void *data)
3464 {
3465         struct osd_thread_info *info = data;
3466
3467         LASSERT(info->oti_r_locks == 0);
3468         LASSERT(info->oti_w_locks == 0);
3469         LASSERT(info->oti_txns    == 0);
3470 }
3471
3472 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3473                            const char *name, struct lu_device *next)
3474 {
3475         int rc;
3476         struct lu_context *ctx;
3477
3478         /* context for commit hooks */
3479         ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3480         rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3481         if (rc == 0) {
3482                 rc = osd_procfs_init(osd_dev(d), name);
3483                 ctx->lc_cookie = 0x3;
3484         }
3485         return rc;
3486 }
3487
3488 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3489 {
3490         struct osd_thread_info *info = osd_oti_get(env);
3491         ENTRY;
3492         if (o->od_obj_area != NULL) {
3493                 lu_object_put(env, &o->od_obj_area->do_lu);
3494                 o->od_obj_area = NULL;
3495         }
3496         osd_oi_fini(info, &o->od_oi);
3497
3498         RETURN(0);
3499 }
3500
3501 static int osd_mount(const struct lu_env *env,
3502                      struct osd_device *o, struct lustre_cfg *cfg)
3503 {
3504         struct lustre_mount_info *lmi;
3505         const char               *dev  = lustre_cfg_string(cfg, 0);
3506         struct lustre_disk_data  *ldd;
3507         struct lustre_sb_info    *lsi;
3508
3509         ENTRY;
3510
3511         if (o->od_mount != NULL) {
3512                 CERROR("Already mounted (%s)\n", dev);
3513                 RETURN(-EEXIST);
3514         }
3515
3516         /* get mount */
3517         lmi = server_get_mount(dev);
3518         if (lmi == NULL) {
3519                 CERROR("Cannot get mount info for %s!\n", dev);
3520                 RETURN(-EFAULT);
3521         }
3522
3523         LASSERT(lmi != NULL);
3524         /* save lustre_mount_info in dt_device */
3525         o->od_mount = lmi;
3526
3527         lsi = s2lsi(lmi->lmi_sb);
3528         ldd = lsi->lsi_ldd;
3529
3530         if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3531                 o->od_iop_mode = 0;
3532                 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3533         } else
3534                 o->od_iop_mode = 1;
3535
3536         o->od_obj_area = NULL;
3537         RETURN(0);
3538 }
3539
3540 static struct lu_device *osd_device_fini(const struct lu_env *env,
3541                                          struct lu_device *d)
3542 {
3543         int rc;
3544         ENTRY;
3545
3546         shrink_dcache_sb(osd_sb(osd_dev(d)));
3547         osd_sync(env, lu2dt_dev(d));
3548
3549         rc = osd_procfs_fini(osd_dev(d));
3550         if (rc) {
3551                 CERROR("proc fini error %d \n", rc);
3552                 RETURN (ERR_PTR(rc));
3553         }
3554
3555         if (osd_dev(d)->od_mount)
3556                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3557                                  osd_dev(d)->od_mount->lmi_mnt);
3558         osd_dev(d)->od_mount = NULL;
3559
3560         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3561         RETURN(NULL);
3562 }
3563
3564 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3565                                           struct lu_device_type *t,
3566                                           struct lustre_cfg *cfg)
3567 {
3568         struct lu_device  *l;
3569         struct osd_device *o;
3570
3571         OBD_ALLOC_PTR(o);
3572         if (o != NULL) {
3573                 int result;
3574
3575                 result = dt_device_init(&o->od_dt_dev, t);
3576                 if (result == 0) {
3577                         l = osd2lu_dev(o);
3578                         l->ld_ops = &osd_lu_ops;
3579                         o->od_dt_dev.dd_ops = &osd_dt_ops;
3580                         spin_lock_init(&o->od_osfs_lock);
3581                         o->od_osfs_age = cfs_time_shift_64(-1000);
3582                         o->od_capa_hash = init_capa_hash();
3583                         if (o->od_capa_hash == NULL) {
3584                                 dt_device_fini(&o->od_dt_dev);
3585                                 l = ERR_PTR(-ENOMEM);
3586                         }
3587                 } else
3588                         l = ERR_PTR(result);
3589
3590                 if (IS_ERR(l))
3591                         OBD_FREE_PTR(o);
3592         } else
3593                 l = ERR_PTR(-ENOMEM);
3594         return l;
3595 }
3596
3597 static struct lu_device *osd_device_free(const struct lu_env *env,
3598                                          struct lu_device *d)
3599 {
3600         struct osd_device *o = osd_dev(d);
3601         ENTRY;
3602
3603         cleanup_capa_hash(o->od_capa_hash);
3604         dt_device_fini(&o->od_dt_dev);
3605         OBD_FREE_PTR(o);
3606         RETURN(NULL);
3607 }
3608
3609 static int osd_process_config(const struct lu_env *env,
3610                               struct lu_device *d, struct lustre_cfg *cfg)
3611 {
3612         struct osd_device *o = osd_dev(d);
3613         int err;
3614         ENTRY;
3615
3616         switch(cfg->lcfg_command) {
3617         case LCFG_SETUP:
3618                 err = osd_mount(env, o, cfg);
3619                 break;
3620         case LCFG_CLEANUP:
3621                 err = osd_shutdown(env, o);
3622                 break;
3623         default:
3624                 err = -ENOSYS;
3625         }
3626
3627         RETURN(err);
3628 }
3629
3630 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3631                                     struct ldiskfs_super_block * es);
3632
3633 static int osd_recovery_complete(const struct lu_env *env,
3634                                  struct lu_device *d)
3635 {
3636         struct osd_device *o = osd_dev(d);
3637         ENTRY;
3638         /* TODO: orphans handling */
3639         ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3640         RETURN(0);
3641 }
3642
3643 static int osd_prepare(const struct lu_env *env,
3644                        struct lu_device *pdev,
3645                        struct lu_device *dev)
3646 {
3647         struct osd_device *osd = osd_dev(dev);
3648         struct lustre_sb_info *lsi;
3649         struct lustre_disk_data *ldd;
3650         struct lustre_mount_info  *lmi;
3651         struct osd_thread_info *oti = osd_oti_get(env);
3652         struct dt_object *d;
3653         int result;
3654
3655         ENTRY;
3656         /* 1. initialize oi before any file create or file open */
3657         result = osd_oi_init(oti, &osd->od_oi,
3658                              &osd->od_dt_dev, lu2md_dev(pdev));
3659         if (result != 0)
3660                 RETURN(result);
3661
3662         lmi = osd->od_mount;
3663         lsi = s2lsi(lmi->lmi_sb);
3664         ldd = lsi->lsi_ldd;
3665
3666         /* 2. setup local objects */
3667         result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3668         if (result)
3669                 goto out;
3670
3671         /* 3. open remote object dir */
3672         d = dt_store_open(env, lu2dt_dev(dev), "",
3673                           remote_obj_dir, &oti->oti_fid);
3674         if (!IS_ERR(d)) {
3675                 osd->od_obj_area = d;
3676                 result = 0;
3677         } else {
3678                 result = PTR_ERR(d);
3679                 osd->od_obj_area = NULL;
3680         }
3681
3682 out:
3683         RETURN(result);
3684 }
3685
3686 static struct inode *osd_iget(struct osd_thread_info *info,
3687                               struct osd_device *dev,
3688                               const struct osd_inode_id *id)
3689 {
3690         struct inode *inode;
3691
3692         inode = iget(osd_sb(dev), id->oii_ino);
3693         if (inode == NULL) {
3694                 CERROR("no inode\n");
3695                 inode = ERR_PTR(-EACCES);
3696         } else if (id->oii_gen != OSD_OII_NOGEN &&
3697                    inode->i_generation != id->oii_gen) {
3698                 iput(inode);
3699                 inode = ERR_PTR(-ESTALE);
3700         } else if (inode->i_nlink == 0) {
3701                 /* due to parallel readdir and unlink,
3702                 * we can have dead inode here. */
3703                 CWARN("stale inode\n");
3704                 make_bad_inode(inode);
3705                 iput(inode);
3706                 inode = ERR_PTR(-ESTALE);
3707         } else if (is_bad_inode(inode)) {
3708                 CERROR("bad inode %lx\n",inode->i_ino);
3709                 iput(inode);
3710                 inode = ERR_PTR(-ENOENT);
3711         }
3712         return inode;
3713
3714 }
3715
3716 static int osd_fid_lookup(const struct lu_env *env,
3717                           struct osd_object *obj, const struct lu_fid *fid)
3718 {
3719         struct osd_thread_info *info;
3720         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
3721         struct osd_device      *dev;
3722         struct osd_inode_id    *id;
3723         struct osd_oi          *oi;
3724         struct inode           *inode;
3725         int                     result;
3726
3727         LINVRNT(osd_invariant(obj));
3728         LASSERT(obj->oo_inode == NULL);
3729         LASSERT(fid_is_sane(fid));
3730         /*
3731          * This assertion checks that osd layer sees only local
3732          * fids. Unfortunately it is somewhat expensive (does a
3733          * cache-lookup). Disabling it for production/acceptance-testing.
3734          */
3735         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3736
3737         ENTRY;
3738
3739         info = osd_oti_get(env);
3740         dev  = osd_dev(ldev);
3741         id   = &info->oti_id;
3742         oi   = &dev->od_oi;
3743
3744         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3745                 RETURN(-ENOENT);
3746
3747         result = osd_oi_lookup(info, oi, fid, id);
3748         if (result == 0) {
3749                 inode = osd_iget(info, dev, id);
3750                 if (!IS_ERR(inode)) {
3751                         obj->oo_inode = inode;
3752                         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3753                         if (dev->od_iop_mode) {
3754                                 obj->oo_compat_dot_created = 1;
3755                                 obj->oo_compat_dotdot_created = 1;
3756                         }
3757                         result = 0;
3758                 } else
3759                         /*
3760                          * If fid wasn't found in oi, inode-less object is
3761                          * created, for which lu_object_exists() returns
3762                          * false. This is used in a (frequent) case when
3763                          * objects are created as locking anchors or
3764                          * place holders for objects yet to be created.
3765                          */
3766                         result = PTR_ERR(inode);
3767         } else if (result == -ENOENT)
3768                 result = 0;
3769         LINVRNT(osd_invariant(obj));
3770
3771         RETURN(result);
3772 }
3773
3774 static void osd_inode_getattr(const struct lu_env *env,
3775                               struct inode *inode, struct lu_attr *attr)
3776 {
3777         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3778                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3779                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3780
3781         attr->la_atime      = LTIME_S(inode->i_atime);
3782         attr->la_mtime      = LTIME_S(inode->i_mtime);
3783         attr->la_ctime      = LTIME_S(inode->i_ctime);
3784         attr->la_mode       = inode->i_mode;
3785         attr->la_size       = i_size_read(inode);
3786         attr->la_blocks     = inode->i_blocks;
3787         attr->la_uid        = inode->i_uid;
3788         attr->la_gid        = inode->i_gid;
3789         attr->la_flags      = LDISKFS_I(inode)->i_flags;
3790         attr->la_nlink      = inode->i_nlink;
3791         attr->la_rdev       = inode->i_rdev;
3792         attr->la_blksize    = ll_inode_blksize(inode);
3793         attr->la_blkbits    = inode->i_blkbits;
3794 }
3795
3796 /*
3797  * Helpers.
3798  */
3799
3800 static int lu_device_is_osd(const struct lu_device *d)
3801 {
3802         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3803 }
3804
3805 static struct osd_object *osd_obj(const struct lu_object *o)
3806 {
3807         LASSERT(lu_device_is_osd(o->lo_dev));
3808         return container_of0(o, struct osd_object, oo_dt.do_lu);
3809 }
3810
3811 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3812 {
3813         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3814         return container_of0(d, struct osd_device, od_dt_dev);
3815 }
3816
3817 static struct osd_device *osd_dev(const struct lu_device *d)
3818 {
3819         LASSERT(lu_device_is_osd(d));
3820         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3821 }
3822
3823 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3824 {
3825         return osd_obj(&d->do_lu);
3826 }
3827
3828 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3829 {
3830         return osd_dev(o->oo_dt.do_lu.lo_dev);
3831 }
3832
3833 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3834 {
3835         return &osd->od_dt_dev.dd_lu_dev;
3836 }
3837
3838 static struct super_block *osd_sb(const struct osd_device *dev)
3839 {
3840         return dev->od_mount->lmi_mnt->mnt_sb;
3841 }
3842
3843 static journal_t *osd_journal(const struct osd_device *dev)
3844 {
3845         return LDISKFS_SB(osd_sb(dev))->s_journal;
3846 }
3847
3848 static int osd_has_index(const struct osd_object *obj)
3849 {
3850         return obj->oo_dt.do_index_ops != NULL;
3851 }
3852
3853 static int osd_object_invariant(const struct lu_object *l)
3854 {
3855         return osd_invariant(osd_obj(l));
3856 }
3857
3858 static const struct lu_object_operations osd_lu_obj_ops = {
3859         .loo_object_init      = osd_object_init,
3860         .loo_object_delete    = osd_object_delete,
3861         .loo_object_release   = osd_object_release,
3862         .loo_object_free      = osd_object_free,
3863         .loo_object_print     = osd_object_print,
3864         .loo_object_invariant = osd_object_invariant
3865 };
3866
3867 static const struct lu_device_operations osd_lu_ops = {
3868         .ldo_object_alloc      = osd_object_alloc,
3869         .ldo_process_config    = osd_process_config,
3870         .ldo_recovery_complete = osd_recovery_complete,
3871         .ldo_prepare           = osd_prepare,
3872 };
3873
3874 static const struct lu_device_type_operations osd_device_type_ops = {
3875         .ldto_init = osd_type_init,
3876         .ldto_fini = osd_type_fini,
3877
3878         .ldto_start = osd_type_start,
3879         .ldto_stop  = osd_type_stop,
3880
3881         .ldto_device_alloc = osd_device_alloc,
3882         .ldto_device_free  = osd_device_free,
3883
3884         .ldto_device_init    = osd_device_init,
3885         .ldto_device_fini    = osd_device_fini
3886 };
3887
3888 static struct lu_device_type osd_device_type = {
3889         .ldt_tags     = LU_DEVICE_DT,
3890         .ldt_name     = LUSTRE_OSD_NAME,
3891         .ldt_ops      = &osd_device_type_ops,
3892         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3893 };
3894
3895 /*
3896  * lprocfs legacy support.
3897  */
3898 static struct obd_ops osd_obd_device_ops = {
3899         .o_owner = THIS_MODULE
3900 };
3901
3902 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3903         .llod_name      = remote_obj_dir,
3904         .llod_oid       = OSD_REM_OBJ_DIR_OID,
3905         .llod_is_index  = 1,
3906         .llod_feat      = &dt_directory_features,
3907 };
3908
3909 static int __init osd_mod_init(void)
3910 {
3911         struct lprocfs_static_vars lvars;
3912
3913         osd_oi_mod_init();
3914         llo_local_obj_register(&llod_osd_rem_obj_dir);
3915         lprocfs_osd_init_vars(&lvars);
3916         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3917                                    LUSTRE_OSD_NAME, &osd_device_type);
3918 }
3919
3920 static void __exit osd_mod_exit(void)
3921 {
3922         llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3923         class_unregister_type(LUSTRE_OSD_NAME);
3924 }
3925
3926 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3927 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3928 MODULE_LICENSE("GPL");
3929
3930 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);