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