Whamcloud - gitweb
b=19669
[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         /* Check LMA compatibility */
1776         if (rc > 0 &&
1777             (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1778                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1779                       inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1780                       ~LMA_INCOMPAT_SUPP);
1781                 return -ENOSYS;
1782         }
1783
1784         if (rc > 0) {
1785                 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1786                 rc = 0;
1787         } else if (rc == -ENODATA) {
1788                 osd_igif_get(env, dentry, fid);
1789                 rc = 0;
1790         }
1791
1792         if (rc == 0)
1793                 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1794
1795         return rc;
1796 }
1797
1798 /**
1799  * OSD layer object create function for interoperability mode (b11826).
1800  * This is mostly similar to osd_object_create(). Only difference being, fid is
1801  * inserted into inode ea here.
1802  *
1803  * \retval   0, on success
1804  * \retval -ve, on error
1805  */
1806 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1807                              struct lu_attr *attr,
1808                              struct dt_allocation_hint *hint,
1809                              struct dt_object_format *dof,
1810                              struct thandle *th)
1811 {
1812         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1813         struct osd_object      *obj    = osd_dt_obj(dt);
1814         struct osd_thread_info *info   = osd_oti_get(env);
1815         int result;
1816         int is_root = 0;
1817
1818         ENTRY;
1819
1820         LASSERT(osd_invariant(obj));
1821         LASSERT(!dt_object_exists(dt));
1822         LASSERT(osd_write_locked(env, obj));
1823         LASSERT(th != NULL);
1824
1825         result = __osd_object_create(info, obj, attr, hint, dof, th);
1826
1827         if (hint && hint->dah_parent)
1828                 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1829
1830         /* objects under osd root shld have igif fid, so dont add fid EA */
1831         if (result == 0 && is_root == 0)
1832                 result = osd_ea_fid_set(env, dt, fid);
1833
1834         if (result == 0)
1835                 result = __osd_oi_insert(env, obj, fid, th);
1836
1837         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1838         LINVRNT(osd_invariant(obj));
1839         RETURN(result);
1840 }
1841
1842 /*
1843  * Concurrency: @dt is write locked.
1844  */
1845 static void osd_object_ref_add(const struct lu_env *env,
1846                                struct dt_object *dt,
1847                                struct thandle *th)
1848 {
1849         struct osd_object *obj = osd_dt_obj(dt);
1850         struct inode *inode = obj->oo_inode;
1851
1852         LINVRNT(osd_invariant(obj));
1853         LASSERT(dt_object_exists(dt));
1854         LASSERT(osd_write_locked(env, obj));
1855         LASSERT(th != NULL);
1856
1857         spin_lock(&obj->oo_guard);
1858         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1859         inode->i_nlink++;
1860         spin_unlock(&obj->oo_guard);
1861         mark_inode_dirty(inode);
1862         LINVRNT(osd_invariant(obj));
1863 }
1864
1865 /*
1866  * Concurrency: @dt is write locked.
1867  */
1868 static void osd_object_ref_del(const struct lu_env *env,
1869                                struct dt_object *dt,
1870                                struct thandle *th)
1871 {
1872         struct osd_object *obj = osd_dt_obj(dt);
1873         struct inode *inode = obj->oo_inode;
1874
1875         LINVRNT(osd_invariant(obj));
1876         LASSERT(dt_object_exists(dt));
1877         LASSERT(osd_write_locked(env, obj));
1878         LASSERT(th != NULL);
1879
1880         spin_lock(&obj->oo_guard);
1881         LASSERT(inode->i_nlink > 0);
1882         inode->i_nlink--;
1883         spin_unlock(&obj->oo_guard);
1884         mark_inode_dirty(inode);
1885         LINVRNT(osd_invariant(obj));
1886 }
1887
1888 /*
1889  * Concurrency: @dt is read locked.
1890  */
1891 static int osd_xattr_get(const struct lu_env *env,
1892                          struct dt_object *dt,
1893                          struct lu_buf *buf,
1894                          const char *name,
1895                          struct lustre_capa *capa)
1896 {
1897         struct osd_object      *obj    = osd_dt_obj(dt);
1898         struct inode           *inode  = obj->oo_inode;
1899         struct osd_thread_info *info   = osd_oti_get(env);
1900         struct dentry          *dentry = &info->oti_obj_dentry;
1901
1902         LASSERT(dt_object_exists(dt));
1903         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1904         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1905
1906         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1907                 return -EACCES;
1908
1909         dentry->d_inode = inode;
1910         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1911 }
1912
1913
1914 /*
1915  * Concurrency: @dt is write locked.
1916  */
1917 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1918                          const struct lu_buf *buf, const char *name, int fl,
1919                          struct thandle *handle, struct lustre_capa *capa)
1920 {
1921         LASSERT(handle != NULL);
1922
1923         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1924                 return -EACCES;
1925
1926         return __osd_xattr_set(env, dt, buf, name, fl);
1927 }
1928
1929 /*
1930  * Concurrency: @dt is read locked.
1931  */
1932 static int osd_xattr_list(const struct lu_env *env,
1933                           struct dt_object *dt,
1934                           struct lu_buf *buf,
1935                           struct lustre_capa *capa)
1936 {
1937         struct osd_object      *obj    = osd_dt_obj(dt);
1938         struct inode           *inode  = obj->oo_inode;
1939         struct osd_thread_info *info   = osd_oti_get(env);
1940         struct dentry          *dentry = &info->oti_obj_dentry;
1941
1942         LASSERT(dt_object_exists(dt));
1943         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1944         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1945
1946         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1947                 return -EACCES;
1948
1949         dentry->d_inode = inode;
1950         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1951 }
1952
1953 /*
1954  * Concurrency: @dt is write locked.
1955  */
1956 static int osd_xattr_del(const struct lu_env *env,
1957                          struct dt_object *dt,
1958                          const char *name,
1959                          struct thandle *handle,
1960                          struct lustre_capa *capa)
1961 {
1962         struct osd_object      *obj    = osd_dt_obj(dt);
1963         struct inode           *inode  = obj->oo_inode;
1964         struct osd_thread_info *info   = osd_oti_get(env);
1965         struct dentry          *dentry = &info->oti_obj_dentry;
1966         struct timespec        *t      = &info->oti_time;
1967         int                     rc;
1968
1969         LASSERT(dt_object_exists(dt));
1970         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1971         LASSERT(osd_write_locked(env, obj));
1972         LASSERT(handle != NULL);
1973
1974         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1975                 return -EACCES;
1976
1977         dentry->d_inode = inode;
1978         *t = inode->i_ctime;
1979         rc = inode->i_op->removexattr(dentry, name);
1980         /* ctime should not be updated with server-side time. */
1981         spin_lock(&obj->oo_guard);
1982         inode->i_ctime = *t;
1983         spin_unlock(&obj->oo_guard);
1984         mark_inode_dirty(inode);
1985         return rc;
1986 }
1987
1988 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1989                                      struct dt_object *dt,
1990                                      struct lustre_capa *old,
1991                                      __u64 opc)
1992 {
1993         struct osd_thread_info *info = osd_oti_get(env);
1994         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1995         struct osd_object *obj = osd_dt_obj(dt);
1996         struct osd_device *dev = osd_obj2dev(obj);
1997         struct lustre_capa_key *key = &info->oti_capa_key;
1998         struct lustre_capa *capa = &info->oti_capa;
1999         struct obd_capa *oc;
2000         struct md_capainfo *ci;
2001         int rc;
2002         ENTRY;
2003
2004         if (!dev->od_fl_capa)
2005                 RETURN(ERR_PTR(-ENOENT));
2006
2007         LASSERT(dt_object_exists(dt));
2008         LINVRNT(osd_invariant(obj));
2009
2010         /* renewal sanity check */
2011         if (old && osd_object_auth(env, dt, old, opc))
2012                 RETURN(ERR_PTR(-EACCES));
2013
2014         ci = md_capainfo(env);
2015         if (unlikely(!ci))
2016                 RETURN(ERR_PTR(-ENOENT));
2017
2018         switch (ci->mc_auth) {
2019         case LC_ID_NONE:
2020                 RETURN(NULL);
2021         case LC_ID_PLAIN:
2022                 capa->lc_uid = obj->oo_inode->i_uid;
2023                 capa->lc_gid = obj->oo_inode->i_gid;
2024                 capa->lc_flags = LC_ID_PLAIN;
2025                 break;
2026         case LC_ID_CONVERT: {
2027                 __u32 d[4], s[4];
2028
2029                 s[0] = obj->oo_inode->i_uid;
2030                 get_random_bytes(&(s[1]), sizeof(__u32));
2031                 s[2] = obj->oo_inode->i_gid;
2032                 get_random_bytes(&(s[3]), sizeof(__u32));
2033                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2034                 if (unlikely(rc))
2035                         RETURN(ERR_PTR(rc));
2036
2037                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2038                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2039                 capa->lc_flags = LC_ID_CONVERT;
2040                 break;
2041         }
2042         default:
2043                 RETURN(ERR_PTR(-EINVAL));
2044         }
2045
2046         capa->lc_fid = *fid;
2047         capa->lc_opc = opc;
2048         capa->lc_flags |= dev->od_capa_alg << 24;
2049         capa->lc_timeout = dev->od_capa_timeout;
2050         capa->lc_expiry = 0;
2051
2052         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2053         if (oc) {
2054                 LASSERT(!capa_is_expired(oc));
2055                 RETURN(oc);
2056         }
2057
2058         spin_lock(&capa_lock);
2059         *key = dev->od_capa_keys[1];
2060         spin_unlock(&capa_lock);
2061
2062         capa->lc_keyid = key->lk_keyid;
2063         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2064
2065         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2066         if (rc) {
2067                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2068                 RETURN(ERR_PTR(rc));
2069         }
2070
2071         oc = capa_add(dev->od_capa_hash, capa);
2072         RETURN(oc);
2073 }
2074
2075 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2076 {
2077         int rc;
2078         struct osd_object      *obj    = osd_dt_obj(dt);
2079         struct inode           *inode  = obj->oo_inode;
2080         struct osd_thread_info *info   = osd_oti_get(env);
2081         struct dentry          *dentry = &info->oti_obj_dentry;
2082         struct file            *file   = &info->oti_file;
2083         ENTRY;
2084
2085         dentry->d_inode = inode;
2086         file->f_dentry = dentry;
2087         file->f_mapping = inode->i_mapping;
2088         file->f_op = inode->i_fop;
2089         LOCK_INODE_MUTEX(inode);
2090         rc = file->f_op->fsync(file, dentry, 0);
2091         UNLOCK_INODE_MUTEX(inode);
2092         RETURN(rc);
2093 }
2094
2095 /*
2096  * Get the 64-bit version for an inode.
2097  */
2098 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2099                                                struct dt_object *dt)
2100 {
2101         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2102
2103         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2104                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2105         return LDISKFS_I(inode)->i_fs_version;
2106 }
2107
2108 /*
2109  * Set the 64-bit version and return the old version.
2110  */
2111 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2112                                    dt_obj_version_t new_version)
2113 {
2114         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2115
2116         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2117                new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2118         LDISKFS_I(inode)->i_fs_version = new_version;
2119         /** Version is set after all inode operations are finished,
2120          *  so we should mark it dirty here */
2121         inode->i_sb->s_op->dirty_inode(inode);
2122 }
2123
2124 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2125                         void **data)
2126 {
2127         struct osd_object *obj = osd_dt_obj(dt);
2128         ENTRY;
2129
2130         *data = (void *)obj->oo_inode;
2131         RETURN(0);
2132 }
2133
2134 static const struct dt_object_operations osd_obj_ops = {
2135         .do_read_lock    = osd_object_read_lock,
2136         .do_write_lock   = osd_object_write_lock,
2137         .do_read_unlock  = osd_object_read_unlock,
2138         .do_write_unlock = osd_object_write_unlock,
2139         .do_attr_get     = osd_attr_get,
2140         .do_attr_set     = osd_attr_set,
2141         .do_ah_init      = osd_ah_init,
2142         .do_create       = osd_object_create,
2143         .do_index_try    = osd_index_try,
2144         .do_ref_add      = osd_object_ref_add,
2145         .do_ref_del      = osd_object_ref_del,
2146         .do_xattr_get    = osd_xattr_get,
2147         .do_xattr_set    = osd_xattr_set,
2148         .do_xattr_del    = osd_xattr_del,
2149         .do_xattr_list   = osd_xattr_list,
2150         .do_capa_get     = osd_capa_get,
2151         .do_object_sync  = osd_object_sync,
2152         .do_version_get  = osd_object_version_get,
2153         .do_version_set  = osd_object_version_set,
2154         .do_data_get     = osd_data_get,
2155 };
2156
2157 /**
2158  * dt_object_operations for interoperability mode
2159  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2160  */
2161 static const struct dt_object_operations osd_obj_ea_ops = {
2162         .do_read_lock    = osd_object_read_lock,
2163         .do_write_lock   = osd_object_write_lock,
2164         .do_read_unlock  = osd_object_read_unlock,
2165         .do_write_unlock = osd_object_write_unlock,
2166         .do_attr_get     = osd_attr_get,
2167         .do_attr_set     = osd_attr_set,
2168         .do_ah_init      = osd_ah_init,
2169         .do_create       = osd_object_ea_create,
2170         .do_index_try    = osd_index_try,
2171         .do_ref_add      = osd_object_ref_add,
2172         .do_ref_del      = osd_object_ref_del,
2173         .do_xattr_get    = osd_xattr_get,
2174         .do_xattr_set    = osd_xattr_set,
2175         .do_xattr_del    = osd_xattr_del,
2176         .do_xattr_list   = osd_xattr_list,
2177         .do_capa_get     = osd_capa_get,
2178         .do_object_sync  = osd_object_sync,
2179         .do_version_get  = osd_object_version_get,
2180         .do_version_set  = osd_object_version_set,
2181         .do_data_get     = osd_data_get,
2182 };
2183
2184 /*
2185  * Body operations.
2186  */
2187
2188 /*
2189  * XXX: Another layering violation for now.
2190  *
2191  * We don't want to use ->f_op->read methods, because generic file write
2192  *
2193  *         - serializes on ->i_sem, and
2194  *
2195  *         - does a lot of extra work like balance_dirty_pages(),
2196  *
2197  * which doesn't work for globally shared files like /last-received.
2198  */
2199 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2200 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2201                                 loff_t *offs, handle_t *handle);
2202
2203 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2204                         struct lu_buf *buf, loff_t *pos,
2205                         struct lustre_capa *capa)
2206 {
2207         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2208
2209         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2210                 RETURN(-EACCES);
2211
2212         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2213 }
2214
2215 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2216                          const struct lu_buf *buf, loff_t *pos,
2217                          struct thandle *handle, struct lustre_capa *capa,
2218                          int ignore_quota)
2219 {
2220         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
2221         struct osd_thandle *oh;
2222         ssize_t             result;
2223 #ifdef HAVE_QUOTA_SUPPORT
2224         cfs_cap_t           save = current->cap_effective;
2225 #endif
2226
2227         LASSERT(handle != NULL);
2228
2229         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2230                 RETURN(-EACCES);
2231
2232         oh = container_of(handle, struct osd_thandle, ot_super);
2233         LASSERT(oh->ot_handle->h_transaction != NULL);
2234 #ifdef HAVE_QUOTA_SUPPORT
2235         if (ignore_quota)
2236                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2237         else
2238                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2239 #endif
2240         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2241                                              pos, oh->ot_handle);
2242 #ifdef HAVE_QUOTA_SUPPORT
2243         current->cap_effective = save;
2244 #endif
2245         if (result == 0)
2246                 result = buf->lb_len;
2247         return result;
2248 }
2249
2250 static const struct dt_body_operations osd_body_ops = {
2251         .dbo_read  = osd_read,
2252         .dbo_write = osd_write
2253 };
2254
2255 /*
2256  * Index operations.
2257  */
2258
2259 static int osd_object_is_root(const struct osd_object *obj)
2260 {
2261         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2262 }
2263
2264 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2265                            const struct dt_index_features *feat)
2266 {
2267         struct iam_descr *descr;
2268         struct dt_object *dt = &o->oo_dt;
2269
2270         if (osd_object_is_root(o))
2271                 return feat == &dt_directory_features;
2272
2273         LASSERT(o->oo_dir != NULL);
2274
2275         descr = o->oo_dir->od_container.ic_descr;
2276         if (feat == &dt_directory_features) {
2277                 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2278                         return 1;
2279
2280                 if (descr == &iam_htree_compat_param) {
2281                         /* if it is a HTREE dir then there is good chance that,
2282                          * we dealing with ext3 directory here with no FIDs. */
2283
2284                         if (descr->id_rec_size ==
2285                             sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2286
2287                                 dt->do_index_ops = &osd_index_ea_ops;
2288                                 return 1;
2289                         }
2290                 }
2291                 return 0;
2292         } else {
2293                 return
2294                         feat->dif_keysize_min <= descr->id_key_size &&
2295                         descr->id_key_size <= feat->dif_keysize_max &&
2296                         feat->dif_recsize_min <= descr->id_rec_size &&
2297                         descr->id_rec_size <= feat->dif_recsize_max &&
2298                         !(feat->dif_flags & (DT_IND_VARKEY |
2299                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2300                         ergo(feat->dif_flags & DT_IND_UPDATE,
2301                              1 /* XXX check that object (and file system) is
2302                                 * writable */);
2303         }
2304 }
2305
2306 static int osd_iam_container_init(const struct lu_env *env,
2307                                   struct osd_object *obj,
2308                                   struct osd_directory *dir)
2309 {
2310         int result;
2311         struct iam_container *bag;
2312
2313         bag    = &dir->od_container;
2314         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2315         if (result == 0) {
2316                 result = iam_container_setup(bag);
2317                 if (result == 0)
2318                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2319                 else
2320                         iam_container_fini(bag);
2321         }
2322         return result;
2323 }
2324
2325 /*
2326  * Concurrency: no external locking is necessary.
2327  */
2328 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2329                          const struct dt_index_features *feat)
2330 {
2331         int result;
2332         int ea_dir = 0;
2333         struct osd_object *obj = osd_dt_obj(dt);
2334         struct osd_device *osd = osd_obj2dev(obj);
2335
2336         LINVRNT(osd_invariant(obj));
2337         LASSERT(dt_object_exists(dt));
2338
2339         if (osd_object_is_root(obj)) {
2340                 dt->do_index_ops = &osd_index_ea_ops;
2341                 result = 0;
2342         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2343                 dt->do_index_ops = &osd_index_ea_ops;
2344                 if (S_ISDIR(obj->oo_inode->i_mode))
2345                         result = 0;
2346                 else
2347                         result = -ENOTDIR;
2348                 ea_dir = 1;
2349         } else if (!osd_has_index(obj)) {
2350                 struct osd_directory *dir;
2351
2352                 OBD_ALLOC_PTR(dir);
2353                 if (dir != NULL) {
2354
2355                         spin_lock(&obj->oo_guard);
2356                         if (obj->oo_dir == NULL)
2357                                 obj->oo_dir = dir;
2358                         else
2359                                 /*
2360                                  * Concurrent thread allocated container data.
2361                                  */
2362                                 OBD_FREE_PTR(dir);
2363                         spin_unlock(&obj->oo_guard);
2364                         /*
2365                          * Now, that we have container data, serialize its
2366                          * initialization.
2367                          */
2368                         down_write(&obj->oo_ext_idx_sem);
2369                         /*
2370                          * recheck under lock.
2371                          */
2372                         if (!osd_has_index(obj))
2373                                 result = osd_iam_container_init(env, obj, dir);
2374                         else
2375                                 result = 0;
2376                         up_write(&obj->oo_ext_idx_sem);
2377                 } else
2378                         result = -ENOMEM;
2379         } else
2380                 result = 0;
2381
2382         if (result == 0 && ea_dir == 0) {
2383                 if (!osd_iam_index_probe(env, obj, feat))
2384                         result = -ENOTDIR;
2385         }
2386         LINVRNT(osd_invariant(obj));
2387
2388         return result;
2389 }
2390
2391 /**
2392  *      delete a (key, value) pair from index \a dt specified by \a key
2393  *
2394  *      \param  dt_object      osd index object
2395  *      \param  key     key for index
2396  *      \param  rec     record reference
2397  *      \param  handle  transaction handler
2398  *
2399  *      \retval  0  success
2400  *      \retval -ve   failure
2401  */
2402
2403 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2404                                 const struct dt_key *key, struct thandle *handle,
2405                                 struct lustre_capa *capa)
2406 {
2407         struct osd_object     *obj = osd_dt_obj(dt);
2408         struct osd_thandle    *oh;
2409         struct iam_path_descr *ipd;
2410         struct iam_container  *bag = &obj->oo_dir->od_container;
2411         int rc;
2412
2413         ENTRY;
2414
2415         LINVRNT(osd_invariant(obj));
2416         LASSERT(dt_object_exists(dt));
2417         LASSERT(bag->ic_object == obj->oo_inode);
2418         LASSERT(handle != NULL);
2419
2420         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2421                 RETURN(-EACCES);
2422
2423         ipd = osd_idx_ipd_get(env, bag);
2424         if (unlikely(ipd == NULL))
2425                 RETURN(-ENOMEM);
2426
2427         oh = container_of0(handle, struct osd_thandle, ot_super);
2428         LASSERT(oh->ot_handle != NULL);
2429         LASSERT(oh->ot_handle->h_transaction != NULL);
2430
2431         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2432         osd_ipd_put(env, bag, ipd);
2433         LINVRNT(osd_invariant(obj));
2434         RETURN(rc);
2435 }
2436
2437 /**
2438  * Index delete function for interoperability mode (b11826).
2439  * It will remove the directory entry added by osd_index_ea_insert().
2440  * This entry is needed to maintain name->fid mapping.
2441  *
2442  * \param key,  key i.e. file entry to be deleted
2443  *
2444  * \retval   0, on success
2445  * \retval -ve, on error
2446  */
2447 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2448                                const struct dt_key *key, struct thandle *handle,
2449                                struct lustre_capa *capa)
2450 {
2451         struct osd_object          *obj    = osd_dt_obj(dt);
2452         struct inode               *dir    = obj->oo_inode;
2453         struct dentry              *dentry;
2454         struct osd_thandle         *oh;
2455         struct ldiskfs_dir_entry_2 *de;
2456         struct buffer_head         *bh;
2457
2458         int rc;
2459
2460         ENTRY;
2461
2462         LINVRNT(osd_invariant(obj));
2463         LASSERT(dt_object_exists(dt));
2464         LASSERT(handle != NULL);
2465
2466         oh = container_of(handle, struct osd_thandle, ot_super);
2467         LASSERT(oh->ot_handle != NULL);
2468         LASSERT(oh->ot_handle->h_transaction != NULL);
2469
2470         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2471                 RETURN(-EACCES);
2472
2473         dentry = osd_child_dentry_get(env, obj,
2474                                       (char *)key, strlen((char *)key));
2475
2476         down_write(&obj->oo_ext_idx_sem);
2477         bh = ldiskfs_find_entry(dentry, &de);
2478         if (bh) {
2479                 struct osd_thread_info *oti = osd_oti_get(env);
2480                 struct timespec *ctime = &oti->oti_time;
2481                 struct timespec *mtime = &oti->oti_time2;
2482
2483                 *ctime = dir->i_ctime;
2484                 *mtime = dir->i_mtime;
2485                 rc = ldiskfs_delete_entry(oh->ot_handle,
2486                                 dir, de, bh);
2487                 /* xtime should not be updated with server-side time. */
2488                 spin_lock(&obj->oo_guard);
2489                 dir->i_ctime = *ctime;
2490                 dir->i_mtime = *mtime;
2491                 spin_unlock(&obj->oo_guard);
2492                 mark_inode_dirty(dir);
2493                 brelse(bh);
2494         } else
2495                 rc = -ENOENT;
2496
2497         up_write(&obj->oo_ext_idx_sem);
2498         LASSERT(osd_invariant(obj));
2499         RETURN(rc);
2500 }
2501
2502 /**
2503  *      Lookup index for \a key and copy record to \a rec.
2504  *
2505  *      \param  dt_object      osd index object
2506  *      \param  key     key for index
2507  *      \param  rec     record reference
2508  *
2509  *      \retval  +ve  success : exact mach
2510  *      \retval  0    return record with key not greater than \a key
2511  *      \retval -ve   failure
2512  */
2513 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2514                                 struct dt_rec *rec, const struct dt_key *key,
2515                                 struct lustre_capa *capa)
2516 {
2517         struct osd_object     *obj = osd_dt_obj(dt);
2518         struct iam_path_descr *ipd;
2519         struct iam_container  *bag = &obj->oo_dir->od_container;
2520         struct osd_thread_info *oti = osd_oti_get(env);
2521         struct iam_iterator    *it = &oti->oti_idx_it;
2522         int rc;
2523         ENTRY;
2524
2525         LASSERT(osd_invariant(obj));
2526         LASSERT(dt_object_exists(dt));
2527         LASSERT(bag->ic_object == obj->oo_inode);
2528
2529         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2530                 RETURN(-EACCES);
2531
2532         ipd = osd_idx_ipd_get(env, bag);
2533         if (IS_ERR(ipd))
2534                 RETURN(-ENOMEM);
2535
2536         /* got ipd now we can start iterator. */
2537         iam_it_init(it, bag, 0, ipd);
2538
2539         rc = iam_it_get(it, (struct iam_key *)key);
2540         if (rc >= 0)
2541                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2542
2543         iam_it_put(it);
2544         iam_it_fini(it);
2545         osd_ipd_put(env, bag, ipd);
2546
2547         LINVRNT(osd_invariant(obj));
2548
2549         RETURN(rc);
2550 }
2551
2552 /**
2553  *      Inserts (key, value) pair in \a dt index object.
2554  *
2555  *      \param  dt      osd index object
2556  *      \param  key     key for index
2557  *      \param  rec     record reference
2558  *      \param  th      transaction handler
2559  *
2560  *      \retval  0  success
2561  *      \retval -ve failure
2562  */
2563 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2564                                 const struct dt_rec *rec, const struct dt_key *key,
2565                                 struct thandle *th, struct lustre_capa *capa,
2566                                 int ignore_quota)
2567 {
2568         struct osd_object     *obj = osd_dt_obj(dt);
2569         struct iam_path_descr *ipd;
2570         struct osd_thandle    *oh;
2571         struct iam_container  *bag = &obj->oo_dir->od_container;
2572 #ifdef HAVE_QUOTA_SUPPORT
2573         cfs_cap_t              save = current->cap_effective;
2574 #endif
2575         int rc;
2576
2577         ENTRY;
2578
2579         LINVRNT(osd_invariant(obj));
2580         LASSERT(dt_object_exists(dt));
2581         LASSERT(bag->ic_object == obj->oo_inode);
2582         LASSERT(th != NULL);
2583
2584         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2585                 return -EACCES;
2586
2587         ipd = osd_idx_ipd_get(env, bag);
2588         if (unlikely(ipd == NULL))
2589                 RETURN(-ENOMEM);
2590
2591         oh = container_of0(th, struct osd_thandle, ot_super);
2592         LASSERT(oh->ot_handle != NULL);
2593         LASSERT(oh->ot_handle->h_transaction != NULL);
2594 #ifdef HAVE_QUOTA_SUPPORT
2595         if (ignore_quota)
2596                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2597         else
2598                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2599 #endif
2600         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2601                         (struct iam_rec *)rec, ipd);
2602 #ifdef HAVE_QUOTA_SUPPORT
2603         current->cap_effective = save;
2604 #endif
2605         osd_ipd_put(env, bag, ipd);
2606         LINVRNT(osd_invariant(obj));
2607         RETURN(rc);
2608 }
2609
2610 /**
2611  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2612  * into the directory.Also sets flags into osd object to
2613  * indicate dot and dotdot are created. This is required for
2614  * interoperability mode (b11826)
2615  *
2616  * \param dir   directory for dot and dotdot fixup.
2617  * \param obj   child object for linking
2618  *
2619  * \retval   0, on success
2620  * \retval -ve, on error
2621  */
2622 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2623                               struct osd_object *dir,
2624                               struct osd_object *obj, const char *name,
2625                               struct thandle *th)
2626 {
2627         struct inode            *parent_dir   = obj->oo_inode;
2628         struct inode            *inode  = dir->oo_inode;
2629         struct osd_thandle      *oth;
2630         int result = 0;
2631
2632         oth = container_of(th, struct osd_thandle, ot_super);
2633         LASSERT(oth->ot_handle->h_transaction != NULL);
2634         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2635
2636         if (strcmp(name, dot) == 0) {
2637                 if (dir->oo_compat_dot_created) {
2638                         result = -EEXIST;
2639                 } else {
2640                         LASSERT(obj == dir);
2641                         dir->oo_compat_dot_created = 1;
2642                         result = 0;
2643                 }
2644         } else if(strcmp(name, dotdot) == 0) {
2645                 if (!dir->oo_compat_dot_created)
2646                         return -EINVAL;
2647                 if (dir->oo_compat_dotdot_created)
2648                         return __osd_ea_add_rec(info, dir, obj, name, th);
2649
2650                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2651                 if (result == 0)
2652                        dir->oo_compat_dotdot_created = 1;
2653         }
2654
2655         return result;
2656 }
2657
2658 /**
2659  * Calls ldiskfs_add_entry() to add directory entry
2660  * into the directory. This is required for
2661  * interoperability mode (b11826)
2662  *
2663  * \retval   0, on success
2664  * \retval -ve, on error
2665  */
2666 static int __osd_ea_add_rec(struct osd_thread_info *info,
2667                             struct osd_object *pobj,
2668                             struct osd_object *cobj,
2669                             const char *name,
2670                             struct thandle *th)
2671 {
2672         struct dentry      *child;
2673         struct osd_thandle *oth;
2674         struct inode       *cinode  = cobj->oo_inode;
2675         int rc;
2676
2677         oth = container_of(th, struct osd_thandle, ot_super);
2678         LASSERT(oth->ot_handle != NULL);
2679         LASSERT(oth->ot_handle->h_transaction != NULL);
2680
2681         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2682         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2683
2684         RETURN(rc);
2685 }
2686
2687 /**
2688  * It will call the appropriate osd_add* function and return the
2689  * value, return by respective functions.
2690  */
2691 static int osd_ea_add_rec(const struct lu_env *env,
2692                           struct osd_object *pobj,
2693                           struct osd_object *cobj,
2694                           const char *name,
2695                           struct thandle *th)
2696 {
2697         struct osd_thread_info    *info   = osd_oti_get(env);
2698         int rc;
2699
2700         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2701                                                    name[2] =='\0')))
2702                 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2703         else
2704                 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2705
2706         return rc;
2707 }
2708
2709 /**
2710  * Calls ->lookup() to find dentry. From dentry get inode and
2711  * read inode's ea to get fid. This is required for  interoperability
2712  * mode (b11826)
2713  *
2714  * \retval   0, on success
2715  * \retval -ve, on error
2716  */
2717 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2718                              struct dt_rec *rec, const struct dt_key *key)
2719 {
2720         struct inode            *dir    = obj->oo_inode;
2721         struct osd_thread_info  *info   = osd_oti_get(env);
2722         struct dentry           *dentry;
2723         struct osd_device      *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2724         struct osd_inode_id    *id     = &info->oti_id;
2725         struct ldiskfs_dir_entry_2 *de;
2726         struct buffer_head         *bh;
2727         struct inode *inode;
2728         int ino;
2729         int rc;
2730
2731         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2732
2733         dentry = osd_child_dentry_get(env, obj,
2734                                       (char *)key, strlen((char *)key));
2735
2736         down_read(&obj->oo_ext_idx_sem);
2737         bh = ldiskfs_find_entry(dentry, &de);
2738         if (bh) {
2739                 ino = le32_to_cpu(de->inode);
2740                 brelse(bh);
2741                 id->oii_ino = ino;
2742                 id->oii_gen = OSD_OII_NOGEN;
2743
2744                 inode = osd_iget(info, dev, id);
2745                 if (!IS_ERR(inode)) {
2746                         dentry->d_inode = inode;
2747
2748                         rc = osd_ea_fid_get(env, dentry, rec);
2749                         iput(inode);
2750                 } else
2751                         rc = PTR_ERR(inode);
2752         } else
2753                 rc = -ENOENT;
2754
2755         up_read(&obj->oo_ext_idx_sem);
2756         RETURN (rc);
2757 }
2758
2759 /**
2760  * Find the osd object for given fid.
2761  *
2762  * \param fid, need to find the osd object having this fid
2763  *
2764  * \retval osd_object, on success
2765  * \retval        -ve, on error
2766  */
2767 struct osd_object *osd_object_find(const struct lu_env *env,
2768                                    struct dt_object *dt,
2769                                    const struct lu_fid *fid)
2770 {
2771         struct lu_device         *ludev = dt->do_lu.lo_dev;
2772         struct osd_object        *child = NULL;
2773         struct lu_object         *luch;
2774         struct lu_object         *lo;
2775
2776         luch = lu_object_find(env, ludev, fid, NULL);
2777         if (!IS_ERR(luch)) {
2778                 if (lu_object_exists(luch)) {
2779                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2780                         if (lo != NULL)
2781                                 child = osd_obj(lo);
2782                         else
2783                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2784                                                 "lu_object can't be located"
2785                                                 ""DFID"\n", PFID(fid));
2786
2787                         if (child == NULL) {
2788                                 lu_object_put(env, luch);
2789                                 CERROR("Unable to get osd_object\n");
2790                                 child = ERR_PTR(-ENOENT);
2791                         }
2792                 } else {
2793                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
2794                                         "lu_object does not exists "DFID"\n",
2795                                         PFID(fid));
2796                         child = ERR_PTR(-ENOENT);
2797                 }
2798         } else
2799                 child = (void *)luch;
2800
2801         return child;
2802 }
2803
2804 /**
2805  * Put the osd object once done with it.
2806  *
2807  * \param obj, osd object that needs to be put
2808  */
2809 static inline void osd_object_put(const struct lu_env *env,
2810                                   struct osd_object *obj)
2811 {
2812         lu_object_put(env, &obj->oo_dt.do_lu);
2813 }
2814
2815 /**
2816  * Index add function for interoperability mode (b11826).
2817  * It will add the directory entry.This entry is needed to
2818  * maintain name->fid mapping.
2819  *
2820  * \param key, it is key i.e. file entry to be inserted
2821  * \param rec, it is value of given key i.e. fid
2822  *
2823  * \retval   0, on success
2824  * \retval -ve, on error
2825  */
2826 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2827                                const struct dt_rec *rec,
2828                                const struct dt_key *key, struct thandle *th,
2829                                struct lustre_capa *capa, int ignore_quota)
2830 {
2831         struct osd_object        *obj   = osd_dt_obj(dt);
2832         struct lu_fid            *fid   = &osd_oti_get(env)->oti_fid;
2833         const struct lu_fid_pack *pack  = (const struct lu_fid_pack *)rec;
2834         const char               *name  = (const char *)key;
2835         struct osd_object        *child;
2836 #ifdef HAVE_QUOTA_SUPPORT
2837         cfs_cap_t                 save  = current->cap_effective;
2838 #endif
2839         int rc;
2840
2841         ENTRY;
2842
2843         LASSERT(osd_invariant(obj));
2844         LASSERT(dt_object_exists(dt));
2845         LASSERT(th != NULL);
2846
2847         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2848                 RETURN(-EACCES);
2849
2850         rc = fid_unpack(pack, fid);
2851         if (rc != 0)
2852                 RETURN(rc);
2853         child = osd_object_find(env, dt, fid);
2854         if (!IS_ERR(child)) {
2855                 struct inode *inode = obj->oo_inode;
2856                 struct osd_thread_info *oti = osd_oti_get(env);
2857                 struct timespec *ctime = &oti->oti_time;
2858                 struct timespec *mtime = &oti->oti_time2;
2859
2860                 *ctime = inode->i_ctime;
2861                 *mtime = inode->i_mtime;
2862 #ifdef HAVE_QUOTA_SUPPORT
2863                 if (ignore_quota)
2864                         current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2865                 else
2866                         current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2867 #endif
2868                 down_write(&obj->oo_ext_idx_sem);
2869                 rc = osd_ea_add_rec(env, obj, child, name, th);
2870                 up_write(&obj->oo_ext_idx_sem);
2871 #ifdef HAVE_QUOTA_SUPPORT
2872                 current->cap_effective = save;
2873 #endif
2874                 osd_object_put(env, child);
2875                 /* xtime should not be updated with server-side time. */
2876                 spin_lock(&obj->oo_guard);
2877                 inode->i_ctime = *ctime;
2878                 inode->i_mtime = *mtime;
2879                 spin_unlock(&obj->oo_guard);
2880                 mark_inode_dirty(inode);
2881         } else {
2882                 rc = PTR_ERR(child);
2883         }
2884
2885         LASSERT(osd_invariant(obj));
2886         RETURN(rc);
2887 }
2888
2889 /**
2890  *  Initialize osd Iterator for given osd index object.
2891  *
2892  *  \param  dt      osd index object
2893  */
2894
2895 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2896                                  struct dt_object *dt,
2897                                  struct lustre_capa *capa)
2898 {
2899         struct osd_it_iam         *it;
2900         struct osd_thread_info *oti = osd_oti_get(env);
2901         struct osd_object     *obj = osd_dt_obj(dt);
2902         struct lu_object      *lo  = &dt->do_lu;
2903         struct iam_path_descr *ipd;
2904         struct iam_container  *bag = &obj->oo_dir->od_container;
2905
2906         LASSERT(lu_object_exists(lo));
2907
2908         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2909                 return ERR_PTR(-EACCES);
2910
2911         it = &oti->oti_it;
2912         ipd = osd_it_ipd_get(env, bag);
2913         if (likely(ipd != NULL)) {
2914                 it->oi_obj = obj;
2915                 it->oi_ipd = ipd;
2916                 lu_object_get(lo);
2917                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2918                 return (struct dt_it *)it;
2919         }
2920         return ERR_PTR(-ENOMEM);
2921 }
2922
2923 /**
2924  * free given Iterator.
2925  */
2926
2927 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2928 {
2929         struct osd_it_iam     *it = (struct osd_it_iam *)di;
2930         struct osd_object *obj = it->oi_obj;
2931
2932         iam_it_fini(&it->oi_it);
2933         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2934         lu_object_put(env, &obj->oo_dt.do_lu);
2935 }
2936
2937 /**
2938  *  Move Iterator to record specified by \a key
2939  *
2940  *  \param  di      osd iterator
2941  *  \param  key     key for index
2942  *
2943  *  \retval +ve  di points to record with least key not larger than key
2944  *  \retval  0   di points to exact matched key
2945  *  \retval -ve  failure
2946  */
2947
2948 static int osd_it_iam_get(const struct lu_env *env,
2949                       struct dt_it *di, const struct dt_key *key)
2950 {
2951         struct osd_it_iam *it = (struct osd_it_iam *)di;
2952
2953         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2954 }
2955
2956 /**
2957  *  Release Iterator
2958  *
2959  *  \param  di      osd iterator
2960  */
2961
2962 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2963 {
2964         struct osd_it_iam *it = (struct osd_it_iam *)di;
2965
2966         iam_it_put(&it->oi_it);
2967 }
2968
2969 /**
2970  *  Move iterator by one record
2971  *
2972  *  \param  di      osd iterator
2973  *
2974  *  \retval +1   end of container reached
2975  *  \retval  0   success
2976  *  \retval -ve  failure
2977  */
2978
2979 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2980 {
2981         struct osd_it_iam *it = (struct osd_it_iam *)di;
2982
2983         return iam_it_next(&it->oi_it);
2984 }
2985
2986 /**
2987  * Return pointer to the key under iterator.
2988  */
2989
2990 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2991                                  const struct dt_it *di)
2992 {
2993         struct osd_it_iam *it = (struct osd_it_iam *)di;
2994
2995         return (struct dt_key *)iam_it_key_get(&it->oi_it);
2996 }
2997
2998 /**
2999  * Return size of key under iterator (in bytes)
3000  */
3001
3002 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3003 {
3004         struct osd_it_iam *it = (struct osd_it_iam *)di;
3005
3006         return iam_it_key_size(&it->oi_it);
3007 }
3008
3009 /**
3010  * Return pointer to the record under iterator.
3011  */
3012 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
3013                                  const struct dt_it *di)
3014 {
3015         struct osd_it_iam *it = (struct osd_it_iam *)di;
3016
3017         return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3018 }
3019
3020 /**
3021  * Returns cookie for current Iterator position.
3022  */
3023 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3024 {
3025         struct osd_it_iam *it = (struct osd_it_iam *)di;
3026
3027         return iam_it_store(&it->oi_it);
3028 }
3029
3030 /**
3031  * Restore iterator from cookie.
3032  *
3033  * \param  di      osd iterator
3034  * \param  hash    Iterator location cookie
3035  *
3036  * \retval +ve  di points to record with least key not larger than key.
3037  * \retval  0   di points to exact matched key
3038  * \retval -ve  failure
3039  */
3040
3041 static int osd_it_iam_load(const struct lu_env *env,
3042                        const struct dt_it *di, __u64 hash)
3043 {
3044         struct osd_it_iam *it = (struct osd_it_iam *)di;
3045
3046         return iam_it_load(&it->oi_it, hash);
3047 }
3048
3049 static const struct dt_index_operations osd_index_iam_ops = {
3050         .dio_lookup = osd_index_iam_lookup,
3051         .dio_insert = osd_index_iam_insert,
3052         .dio_delete = osd_index_iam_delete,
3053         .dio_it     = {
3054                 .init     = osd_it_iam_init,
3055                 .fini     = osd_it_iam_fini,
3056                 .get      = osd_it_iam_get,
3057                 .put      = osd_it_iam_put,
3058                 .next     = osd_it_iam_next,
3059                 .key      = osd_it_iam_key,
3060                 .key_size = osd_it_iam_key_size,
3061                 .rec      = osd_it_iam_rec,
3062                 .store    = osd_it_iam_store,
3063                 .load     = osd_it_iam_load
3064         }
3065 };
3066
3067 /**
3068  * Creates or initializes iterator context.
3069  *
3070  * \retval struct osd_it_ea, iterator structure on success
3071  *
3072  */
3073 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3074                                     struct dt_object *dt,
3075                                     struct lustre_capa *capa)
3076 {
3077         struct osd_object       *obj  = osd_dt_obj(dt);
3078         struct osd_thread_info  *info = osd_oti_get(env);
3079         struct osd_it_ea        *it   = &info->oti_it_ea;
3080         struct lu_object        *lo   = &dt->do_lu;
3081         struct dentry           *obj_dentry = &info->oti_it_dentry;
3082         ENTRY;
3083         LASSERT(lu_object_exists(lo));
3084
3085         obj_dentry->d_inode = obj->oo_inode;
3086         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3087         obj_dentry->d_name.hash = 0;
3088
3089         it->oie_rd_dirent       = 0;
3090         it->oie_it_dirent       = 0;
3091         it->oie_curr_pos        = 0;
3092         it->oie_next_pos        = 0;
3093         it->oie_dirent          = NULL;
3094         it->oie_buf             = info->oti_it_ea_buf;
3095         it->oie_obj             = obj;
3096         it->oie_file.f_dentry   = obj_dentry;
3097         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3098         it->oie_file.f_op         = obj->oo_inode->i_fop;
3099         it->oie_file.private_data = NULL;
3100         lu_object_get(lo);
3101         RETURN((struct dt_it*) it);
3102 }
3103
3104 /**
3105  * Destroy or finishes iterator context.
3106  *
3107  * \param di, struct osd_it_ea, iterator structure to be destroyed
3108  */
3109 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3110 {
3111         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3112         struct osd_object    *obj  = it->oie_obj;
3113         struct inode       *inode  = obj->oo_inode;
3114
3115         ENTRY;
3116         it->oie_file.f_op->release(inode, &it->oie_file);
3117         lu_object_put(env, &obj->oo_dt.do_lu);
3118         EXIT;
3119 }
3120
3121 /**
3122  * It position the iterator at given key, so that next lookup continues from
3123  * that key Or it is similar to dio_it->load() but based on a key,
3124  * rather than file position.
3125  *
3126  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3127  * to the beginning.
3128  *
3129  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3130  */
3131 static int osd_it_ea_get(const struct lu_env *env,
3132                          struct dt_it *di, const struct dt_key *key)
3133 {
3134         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3135
3136         ENTRY;
3137         LASSERT(((const char *)key)[0] == '\0');
3138         it->oie_curr_pos        = 0;
3139         it->oie_next_pos        = 0;
3140         it->oie_rd_dirent       = 0;
3141         it->oie_it_dirent       = 0;
3142         it->oie_dirent          = NULL;
3143
3144         RETURN(+1);
3145 }
3146
3147 /**
3148  * Does nothing
3149  */
3150 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3151 {
3152 }
3153
3154 /**
3155  * It is called internally by ->readdir(). It fills the
3156  * iterator's in-memory data structure with required
3157  * information i.e. name, namelen, rec_size etc.
3158  *
3159  * \param buf, in which information to be filled in.
3160  * \param name, name of the file in given dir
3161  *
3162  * \retval 0, on success
3163  * \retval 1, on buffer full
3164  */
3165 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3166                                loff_t offset, ino_t ino,
3167                                unsigned int d_type)
3168 {
3169         struct osd_it_ea        *it = (struct osd_it_ea *)buf;
3170         struct osd_it_ea_dirent *ent = it->oie_dirent;
3171         ENTRY;
3172
3173         /* this should never happen */
3174         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3175                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3176                 RETURN(-EIO);
3177         }
3178
3179         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3180             OSD_IT_EA_BUFSIZE)
3181                 RETURN(1);
3182
3183         ent->oied_ino     = ino;
3184         ent->oied_off     = offset;
3185         ent->oied_namelen = namelen;
3186         memcpy(ent->oied_name, name, namelen);
3187
3188         it->oie_rd_dirent++;
3189         it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3190         RETURN(0);
3191 }
3192
3193 /**
3194  * Calls ->readdir() to load a directory entry at a time
3195  * and stored it in iterator's in-memory data structure.
3196  *
3197  * \param di, struct osd_it_ea, iterator's in memory structure
3198  *
3199  * \retval   0, on success
3200  * \retval -ve, on error
3201  */
3202 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3203 {
3204         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3205         struct osd_object  *obj   = it->oie_obj;
3206         struct inode       *inode = obj->oo_inode;
3207         int                result = 0;
3208
3209         ENTRY;
3210         it->oie_dirent = it->oie_buf;
3211         it->oie_rd_dirent = 0;
3212         it->oie_file.f_pos = it->oie_curr_pos;
3213
3214         down_read(&obj->oo_ext_idx_sem);
3215         result = inode->i_fop->readdir(&it->oie_file, it,
3216                                        (filldir_t) osd_ldiskfs_filldir);
3217
3218         up_read(&obj->oo_ext_idx_sem);
3219         it->oie_next_pos = it->oie_file.f_pos;
3220
3221         if (it->oie_rd_dirent == 0) {
3222                 result = -EIO;
3223         } else {
3224                 it->oie_dirent = it->oie_buf;
3225                 it->oie_it_dirent = 1;
3226         }
3227
3228         RETURN(result);
3229 }
3230
3231 /**
3232  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3233  * to load a directory entry at a time and stored it in
3234  * iterator's in-memory data structure.
3235  *
3236  * \param di, struct osd_it_ea, iterator's in memory structure
3237  *
3238  * \retval +ve, iterator reached to end
3239  * \retval   0, iterator not reached to end
3240  * \retval -ve, on error
3241  */
3242 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3243 {
3244         struct osd_it_ea *it = (struct osd_it_ea *)di;
3245         int rc;
3246
3247         ENTRY;
3248
3249         if (it->oie_it_dirent < it->oie_rd_dirent) {
3250                 it->oie_dirent = (void *) it->oie_dirent +
3251                                  size_round(sizeof(struct osd_it_ea_dirent) +
3252                                             it->oie_dirent->oied_namelen);
3253                 it->oie_it_dirent++;
3254                 RETURN(0);
3255         } else {
3256                 it->oie_curr_pos = it->oie_next_pos;
3257
3258                 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3259                         rc = +1;
3260                 else
3261                         rc = osd_ldiskfs_it_fill(di);
3262         }
3263
3264         RETURN(rc);
3265 }
3266
3267 /**
3268  * Returns the key at current position from iterator's in memory structure.
3269  *
3270  * \param di, struct osd_it_ea, iterator's in memory structure
3271  *
3272  * \retval key i.e. struct dt_key on success
3273  */
3274 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3275                                     const struct dt_it *di)
3276 {
3277         struct osd_it_ea *it = (struct osd_it_ea *)di;
3278         ENTRY;
3279         RETURN((struct dt_key *)it->oie_dirent->oied_name);
3280 }
3281
3282 /**
3283  * Returns the key's size at current position from iterator's in memory structure.
3284  *
3285  * \param di, struct osd_it_ea, iterator's in memory structure
3286  *
3287  * \retval key_size i.e. struct dt_key on success
3288  */
3289 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3290 {
3291         struct osd_it_ea *it = (struct osd_it_ea *)di;
3292         ENTRY;
3293         RETURN(it->oie_dirent->oied_namelen);
3294 }
3295
3296 /**
3297  * Returns the value (i.e. fid/igif) at current position from iterator's
3298  * in memory structure.
3299  *
3300  * \param di, struct osd_it_ea, iterator's in memory structure
3301  *
3302  * \retval value i.e. struct dt_rec on success
3303  */
3304 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3305                                     const struct dt_it *di)
3306 {
3307         struct osd_it_ea       *it     = (struct osd_it_ea *)di;
3308         struct osd_object      *obj    = it->oie_obj;
3309         struct osd_thread_info *info   = osd_oti_get(env);
3310         struct osd_inode_id    *id     = &info->oti_id;
3311         struct lu_fid_pack     *rec    = &info->oti_pack;
3312         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
3313         struct dentry          *dentry = &info->oti_child_dentry;
3314         struct osd_device      *dev;
3315         struct inode           *inode;
3316         int                    rc;
3317
3318         ENTRY;
3319         dev  = osd_dev(ldev);
3320         id->oii_ino = it->oie_dirent->oied_ino;
3321         id->oii_gen = OSD_OII_NOGEN;
3322         inode = osd_iget(info, dev, id);
3323         if (!IS_ERR(inode)) {
3324                 dentry->d_inode = inode;
3325                 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3326         } else {
3327                 RETURN((struct dt_rec *) PTR_ERR(inode));
3328         }
3329
3330         rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3331         if (rc != 0)
3332                 rec = ERR_PTR(rc);
3333
3334         iput(inode);
3335         RETURN((struct dt_rec *)rec);
3336
3337 }
3338
3339 /**
3340  * Returns a cookie for current position of the iterator head, so that
3341  * user can use this cookie to load/start the iterator next time.
3342  *
3343  * \param di, struct osd_it_ea, iterator's in memory structure
3344  *
3345  * \retval cookie for current position, on success
3346  */
3347 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3348 {
3349         struct osd_it_ea *it = (struct osd_it_ea *)di;
3350         ENTRY;
3351         RETURN(it->oie_dirent->oied_off);
3352 }
3353
3354 /**
3355  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3356  * to load a directory entry at a time and stored it i inn,
3357  * in iterator's in-memory data structure.
3358  *
3359  * \param di, struct osd_it_ea, iterator's in memory structure
3360  *
3361  * \retval +ve, on success
3362  * \retval -ve, on error
3363  */
3364 static int osd_it_ea_load(const struct lu_env *env,
3365                           const struct dt_it *di, __u64 hash)
3366 {
3367         struct osd_it_ea *it = (struct osd_it_ea *)di;
3368         int rc;
3369
3370         ENTRY;
3371         it->oie_curr_pos = hash;
3372
3373         rc =  osd_ldiskfs_it_fill(di);
3374         if (rc == 0)
3375                 rc = +1;
3376
3377         RETURN(rc);
3378 }
3379 /**
3380  * Index and Iterator operations for interoperability
3381  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3382  */
3383 static const struct dt_index_operations osd_index_ea_ops = {
3384         .dio_lookup = osd_index_ea_lookup,
3385         .dio_insert = osd_index_ea_insert,
3386         .dio_delete = osd_index_ea_delete,
3387         .dio_it     = {
3388                 .init     = osd_it_ea_init,
3389                 .fini     = osd_it_ea_fini,
3390                 .get      = osd_it_ea_get,
3391                 .put      = osd_it_ea_put,
3392                 .next     = osd_it_ea_next,
3393                 .key      = osd_it_ea_key,
3394                 .key_size = osd_it_ea_key_size,
3395                 .rec      = osd_it_ea_rec,
3396                 .store    = osd_it_ea_store,
3397                 .load     = osd_it_ea_load
3398         }
3399 };
3400
3401 /**
3402  * Index lookup function for interoperability mode (b11826).
3403  *
3404  * \param key,  key i.e. file name to be searched
3405  *
3406  * \retval +ve, on success
3407  * \retval -ve, on error
3408  */
3409 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3410                                struct dt_rec *rec, const struct dt_key *key,
3411                                struct lustre_capa *capa)
3412 {
3413         struct osd_object *obj = osd_dt_obj(dt);
3414         int rc = 0;
3415
3416         ENTRY;
3417
3418         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3419         LINVRNT(osd_invariant(obj));
3420
3421         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3422                 return -EACCES;
3423
3424         rc = osd_ea_lookup_rec(env, obj, rec, key);
3425
3426         if (rc == 0)
3427                 rc = +1;
3428         RETURN(rc);
3429 }
3430
3431 /* type constructor/destructor: osd_type_init, osd_type_fini */
3432 LU_TYPE_INIT_FINI(osd, &osd_key);
3433
3434 static struct lu_context_key osd_key = {
3435         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3436         .lct_init = osd_key_init,
3437         .lct_fini = osd_key_fini,
3438         .lct_exit = osd_key_exit
3439 };
3440
3441 static void *osd_key_init(const struct lu_context *ctx,
3442                           struct lu_context_key *key)
3443 {
3444         struct osd_thread_info *info;
3445
3446         OBD_ALLOC_PTR(info);
3447         if (info != NULL) {
3448                 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3449                 if (info->oti_it_ea_buf != NULL) {
3450                         info->oti_env = container_of(ctx, struct lu_env,
3451                                                      le_ctx);
3452                 } else {
3453                         OBD_FREE_PTR(info);
3454                         info = ERR_PTR(-ENOMEM);
3455                 }
3456         } else {
3457                 info = ERR_PTR(-ENOMEM);
3458         }
3459         return info;
3460 }
3461
3462 static void osd_key_fini(const struct lu_context *ctx,
3463                          struct lu_context_key *key, void* data)
3464 {
3465         struct osd_thread_info *info = data;
3466
3467         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3468         OBD_FREE_PTR(info);
3469 }
3470
3471 static void osd_key_exit(const struct lu_context *ctx,
3472                          struct lu_context_key *key, void *data)
3473 {
3474         struct osd_thread_info *info = data;
3475
3476         LASSERT(info->oti_r_locks == 0);
3477         LASSERT(info->oti_w_locks == 0);
3478         LASSERT(info->oti_txns    == 0);
3479 }
3480
3481 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3482                            const char *name, struct lu_device *next)
3483 {
3484         int rc;
3485         struct lu_context *ctx;
3486
3487         /* context for commit hooks */
3488         ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3489         rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3490         if (rc == 0) {
3491                 rc = osd_procfs_init(osd_dev(d), name);
3492                 ctx->lc_cookie = 0x3;
3493         }
3494         return rc;
3495 }
3496
3497 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3498 {
3499         struct osd_thread_info *info = osd_oti_get(env);
3500         ENTRY;
3501         if (o->od_obj_area != NULL) {
3502                 lu_object_put(env, &o->od_obj_area->do_lu);
3503                 o->od_obj_area = NULL;
3504         }
3505         osd_oi_fini(info, &o->od_oi);
3506
3507         RETURN(0);
3508 }
3509
3510 static int osd_mount(const struct lu_env *env,
3511                      struct osd_device *o, struct lustre_cfg *cfg)
3512 {
3513         struct lustre_mount_info *lmi;
3514         const char               *dev  = lustre_cfg_string(cfg, 0);
3515         struct lustre_disk_data  *ldd;
3516         struct lustre_sb_info    *lsi;
3517
3518         ENTRY;
3519
3520         if (o->od_mount != NULL) {
3521                 CERROR("Already mounted (%s)\n", dev);
3522                 RETURN(-EEXIST);
3523         }
3524
3525         /* get mount */
3526         lmi = server_get_mount(dev);
3527         if (lmi == NULL) {
3528                 CERROR("Cannot get mount info for %s!\n", dev);
3529                 RETURN(-EFAULT);
3530         }
3531
3532         LASSERT(lmi != NULL);
3533         /* save lustre_mount_info in dt_device */
3534         o->od_mount = lmi;
3535
3536         lsi = s2lsi(lmi->lmi_sb);
3537         ldd = lsi->lsi_ldd;
3538
3539         if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3540                 o->od_iop_mode = 0;
3541                 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3542         } else
3543                 o->od_iop_mode = 1;
3544
3545         o->od_obj_area = NULL;
3546         RETURN(0);
3547 }
3548
3549 static struct lu_device *osd_device_fini(const struct lu_env *env,
3550                                          struct lu_device *d)
3551 {
3552         int rc;
3553         ENTRY;
3554
3555         shrink_dcache_sb(osd_sb(osd_dev(d)));
3556         osd_sync(env, lu2dt_dev(d));
3557
3558         rc = osd_procfs_fini(osd_dev(d));
3559         if (rc) {
3560                 CERROR("proc fini error %d \n", rc);
3561                 RETURN (ERR_PTR(rc));
3562         }
3563
3564         if (osd_dev(d)->od_mount)
3565                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3566                                  osd_dev(d)->od_mount->lmi_mnt);
3567         osd_dev(d)->od_mount = NULL;
3568
3569         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3570         RETURN(NULL);
3571 }
3572
3573 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3574                                           struct lu_device_type *t,
3575                                           struct lustre_cfg *cfg)
3576 {
3577         struct lu_device  *l;
3578         struct osd_device *o;
3579
3580         OBD_ALLOC_PTR(o);
3581         if (o != NULL) {
3582                 int result;
3583
3584                 result = dt_device_init(&o->od_dt_dev, t);
3585                 if (result == 0) {
3586                         l = osd2lu_dev(o);
3587                         l->ld_ops = &osd_lu_ops;
3588                         o->od_dt_dev.dd_ops = &osd_dt_ops;
3589                         spin_lock_init(&o->od_osfs_lock);
3590                         o->od_osfs_age = cfs_time_shift_64(-1000);
3591                         o->od_capa_hash = init_capa_hash();
3592                         if (o->od_capa_hash == NULL) {
3593                                 dt_device_fini(&o->od_dt_dev);
3594                                 l = ERR_PTR(-ENOMEM);
3595                         }
3596                 } else
3597                         l = ERR_PTR(result);
3598
3599                 if (IS_ERR(l))
3600                         OBD_FREE_PTR(o);
3601         } else
3602                 l = ERR_PTR(-ENOMEM);
3603         return l;
3604 }
3605
3606 static struct lu_device *osd_device_free(const struct lu_env *env,
3607                                          struct lu_device *d)
3608 {
3609         struct osd_device *o = osd_dev(d);
3610         ENTRY;
3611
3612         cleanup_capa_hash(o->od_capa_hash);
3613         dt_device_fini(&o->od_dt_dev);
3614         OBD_FREE_PTR(o);
3615         RETURN(NULL);
3616 }
3617
3618 static int osd_process_config(const struct lu_env *env,
3619                               struct lu_device *d, struct lustre_cfg *cfg)
3620 {
3621         struct osd_device *o = osd_dev(d);
3622         int err;
3623         ENTRY;
3624
3625         switch(cfg->lcfg_command) {
3626         case LCFG_SETUP:
3627                 err = osd_mount(env, o, cfg);
3628                 break;
3629         case LCFG_CLEANUP:
3630                 err = osd_shutdown(env, o);
3631                 break;
3632         default:
3633                 err = -ENOSYS;
3634         }
3635
3636         RETURN(err);
3637 }
3638
3639 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3640                                     struct ldiskfs_super_block * es);
3641
3642 static int osd_recovery_complete(const struct lu_env *env,
3643                                  struct lu_device *d)
3644 {
3645         struct osd_device *o = osd_dev(d);
3646         ENTRY;
3647         /* TODO: orphans handling */
3648         ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3649         RETURN(0);
3650 }
3651
3652 static int osd_prepare(const struct lu_env *env,
3653                        struct lu_device *pdev,
3654                        struct lu_device *dev)
3655 {
3656         struct osd_device *osd = osd_dev(dev);
3657         struct lustre_sb_info *lsi;
3658         struct lustre_disk_data *ldd;
3659         struct lustre_mount_info  *lmi;
3660         struct osd_thread_info *oti = osd_oti_get(env);
3661         struct dt_object *d;
3662         int result;
3663
3664         ENTRY;
3665         /* 1. initialize oi before any file create or file open */
3666         result = osd_oi_init(oti, &osd->od_oi,
3667                              &osd->od_dt_dev, lu2md_dev(pdev));
3668         if (result != 0)
3669                 RETURN(result);
3670
3671         lmi = osd->od_mount;
3672         lsi = s2lsi(lmi->lmi_sb);
3673         ldd = lsi->lsi_ldd;
3674
3675         /* 2. setup local objects */
3676         result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3677         if (result)
3678                 goto out;
3679
3680         /* 3. open remote object dir */
3681         d = dt_store_open(env, lu2dt_dev(dev), "",
3682                           remote_obj_dir, &oti->oti_fid);
3683         if (!IS_ERR(d)) {
3684                 osd->od_obj_area = d;
3685                 result = 0;
3686         } else {
3687                 result = PTR_ERR(d);
3688                 osd->od_obj_area = NULL;
3689         }
3690
3691 out:
3692         RETURN(result);
3693 }
3694
3695 static struct inode *osd_iget(struct osd_thread_info *info,
3696                               struct osd_device *dev,
3697                               const struct osd_inode_id *id)
3698 {
3699         struct inode *inode;
3700
3701         inode = iget(osd_sb(dev), id->oii_ino);
3702         if (inode == NULL) {
3703                 CERROR("no inode\n");
3704                 inode = ERR_PTR(-EACCES);
3705         } else if (id->oii_gen != OSD_OII_NOGEN &&
3706                    inode->i_generation != id->oii_gen) {
3707                 iput(inode);
3708                 inode = ERR_PTR(-ESTALE);
3709         } else if (inode->i_nlink == 0) {
3710                 /* due to parallel readdir and unlink,
3711                 * we can have dead inode here. */
3712                 CWARN("stale inode\n");
3713                 make_bad_inode(inode);
3714                 iput(inode);
3715                 inode = ERR_PTR(-ESTALE);
3716         } else if (is_bad_inode(inode)) {
3717                 CERROR("bad inode %lx\n",inode->i_ino);
3718                 iput(inode);
3719                 inode = ERR_PTR(-ENOENT);
3720         }
3721         return inode;
3722
3723 }
3724
3725 static int osd_fid_lookup(const struct lu_env *env,
3726                           struct osd_object *obj, const struct lu_fid *fid)
3727 {
3728         struct osd_thread_info *info;
3729         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
3730         struct osd_device      *dev;
3731         struct osd_inode_id    *id;
3732         struct osd_oi          *oi;
3733         struct inode           *inode;
3734         int                     result;
3735
3736         LINVRNT(osd_invariant(obj));
3737         LASSERT(obj->oo_inode == NULL);
3738         LASSERT(fid_is_sane(fid));
3739         /*
3740          * This assertion checks that osd layer sees only local
3741          * fids. Unfortunately it is somewhat expensive (does a
3742          * cache-lookup). Disabling it for production/acceptance-testing.
3743          */
3744         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3745
3746         ENTRY;
3747
3748         info = osd_oti_get(env);
3749         dev  = osd_dev(ldev);
3750         id   = &info->oti_id;
3751         oi   = &dev->od_oi;
3752
3753         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3754                 RETURN(-ENOENT);
3755
3756         result = osd_oi_lookup(info, oi, fid, id);
3757         if (result == 0) {
3758                 inode = osd_iget(info, dev, id);
3759                 if (!IS_ERR(inode)) {
3760                         obj->oo_inode = inode;
3761                         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3762                         if (dev->od_iop_mode) {
3763                                 obj->oo_compat_dot_created = 1;
3764                                 obj->oo_compat_dotdot_created = 1;
3765                         }
3766                         result = 0;
3767                 } else
3768                         /*
3769                          * If fid wasn't found in oi, inode-less object is
3770                          * created, for which lu_object_exists() returns
3771                          * false. This is used in a (frequent) case when
3772                          * objects are created as locking anchors or
3773                          * place holders for objects yet to be created.
3774                          */
3775                         result = PTR_ERR(inode);
3776         } else if (result == -ENOENT)
3777                 result = 0;
3778         LINVRNT(osd_invariant(obj));
3779
3780         RETURN(result);
3781 }
3782
3783 static void osd_inode_getattr(const struct lu_env *env,
3784                               struct inode *inode, struct lu_attr *attr)
3785 {
3786         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3787                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3788                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3789
3790         attr->la_atime      = LTIME_S(inode->i_atime);
3791         attr->la_mtime      = LTIME_S(inode->i_mtime);
3792         attr->la_ctime      = LTIME_S(inode->i_ctime);
3793         attr->la_mode       = inode->i_mode;
3794         attr->la_size       = i_size_read(inode);
3795         attr->la_blocks     = inode->i_blocks;
3796         attr->la_uid        = inode->i_uid;
3797         attr->la_gid        = inode->i_gid;
3798         attr->la_flags      = LDISKFS_I(inode)->i_flags;
3799         attr->la_nlink      = inode->i_nlink;
3800         attr->la_rdev       = inode->i_rdev;
3801         attr->la_blksize    = ll_inode_blksize(inode);
3802         attr->la_blkbits    = inode->i_blkbits;
3803 }
3804
3805 /*
3806  * Helpers.
3807  */
3808
3809 static int lu_device_is_osd(const struct lu_device *d)
3810 {
3811         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3812 }
3813
3814 static struct osd_object *osd_obj(const struct lu_object *o)
3815 {
3816         LASSERT(lu_device_is_osd(o->lo_dev));
3817         return container_of0(o, struct osd_object, oo_dt.do_lu);
3818 }
3819
3820 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3821 {
3822         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3823         return container_of0(d, struct osd_device, od_dt_dev);
3824 }
3825
3826 static struct osd_device *osd_dev(const struct lu_device *d)
3827 {
3828         LASSERT(lu_device_is_osd(d));
3829         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3830 }
3831
3832 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3833 {
3834         return osd_obj(&d->do_lu);
3835 }
3836
3837 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3838 {
3839         return osd_dev(o->oo_dt.do_lu.lo_dev);
3840 }
3841
3842 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3843 {
3844         return &osd->od_dt_dev.dd_lu_dev;
3845 }
3846
3847 static struct super_block *osd_sb(const struct osd_device *dev)
3848 {
3849         return dev->od_mount->lmi_mnt->mnt_sb;
3850 }
3851
3852 static journal_t *osd_journal(const struct osd_device *dev)
3853 {
3854         return LDISKFS_SB(osd_sb(dev))->s_journal;
3855 }
3856
3857 static int osd_has_index(const struct osd_object *obj)
3858 {
3859         return obj->oo_dt.do_index_ops != NULL;
3860 }
3861
3862 static int osd_object_invariant(const struct lu_object *l)
3863 {
3864         return osd_invariant(osd_obj(l));
3865 }
3866
3867 static const struct lu_object_operations osd_lu_obj_ops = {
3868         .loo_object_init      = osd_object_init,
3869         .loo_object_delete    = osd_object_delete,
3870         .loo_object_release   = osd_object_release,
3871         .loo_object_free      = osd_object_free,
3872         .loo_object_print     = osd_object_print,
3873         .loo_object_invariant = osd_object_invariant
3874 };
3875
3876 static const struct lu_device_operations osd_lu_ops = {
3877         .ldo_object_alloc      = osd_object_alloc,
3878         .ldo_process_config    = osd_process_config,
3879         .ldo_recovery_complete = osd_recovery_complete,
3880         .ldo_prepare           = osd_prepare,
3881 };
3882
3883 static const struct lu_device_type_operations osd_device_type_ops = {
3884         .ldto_init = osd_type_init,
3885         .ldto_fini = osd_type_fini,
3886
3887         .ldto_start = osd_type_start,
3888         .ldto_stop  = osd_type_stop,
3889
3890         .ldto_device_alloc = osd_device_alloc,
3891         .ldto_device_free  = osd_device_free,
3892
3893         .ldto_device_init    = osd_device_init,
3894         .ldto_device_fini    = osd_device_fini
3895 };
3896
3897 static struct lu_device_type osd_device_type = {
3898         .ldt_tags     = LU_DEVICE_DT,
3899         .ldt_name     = LUSTRE_OSD_NAME,
3900         .ldt_ops      = &osd_device_type_ops,
3901         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3902 };
3903
3904 /*
3905  * lprocfs legacy support.
3906  */
3907 static struct obd_ops osd_obd_device_ops = {
3908         .o_owner = THIS_MODULE
3909 };
3910
3911 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3912         .llod_name      = remote_obj_dir,
3913         .llod_oid       = OSD_REM_OBJ_DIR_OID,
3914         .llod_is_index  = 1,
3915         .llod_feat      = &dt_directory_features,
3916 };
3917
3918 static int __init osd_mod_init(void)
3919 {
3920         struct lprocfs_static_vars lvars;
3921
3922         osd_oi_mod_init();
3923         llo_local_obj_register(&llod_osd_rem_obj_dir);
3924         lprocfs_osd_init_vars(&lvars);
3925         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3926                                    LUSTRE_OSD_NAME, &osd_device_type);
3927 }
3928
3929 static void __exit osd_mod_exit(void)
3930 {
3931         llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3932         class_unregister_type(LUSTRE_OSD_NAME);
3933 }
3934
3935 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3936 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3937 MODULE_LICENSE("GPL");
3938
3939 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);