Whamcloud - gitweb
5d9a1555812a3449e5011a9b93dc02a172382891
[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 struct osd_directory {
86         struct iam_container od_container;
87         struct iam_descr     od_descr;
88         struct semaphore     od_sem;
89 };
90
91 struct osd_object {
92         struct dt_object       oo_dt;
93         /**
94          * Inode for file system object represented by this osd_object. This
95          * inode is pinned for the whole duration of lu_object life.
96          *
97          * Not modified concurrently (either setup early during object
98          * creation, or assigned by osd_object_create() under write lock).
99          */
100         struct inode          *oo_inode;
101         struct rw_semaphore    oo_sem;
102         struct osd_directory  *oo_dir;
103         /** protects inode attributes. */
104         spinlock_t             oo_guard;
105         const struct lu_env   *oo_owner;
106 #ifdef CONFIG_LOCKDEP
107         struct lockdep_map     oo_dep_map;
108 #endif
109 };
110
111 static int   osd_root_get      (const struct lu_env *env,
112                                 struct dt_device *dev, struct lu_fid *f);
113
114 static int   lu_device_is_osd  (const struct lu_device *d);
115 static void  osd_mod_exit      (void) __exit;
116 static int   osd_mod_init      (void) __init;
117 static int   osd_type_init     (struct lu_device_type *t);
118 static void  osd_type_fini     (struct lu_device_type *t);
119 static int   osd_object_init   (const struct lu_env *env,
120                                 struct lu_object *l,
121                                 const struct lu_object_conf *_);
122 static void  osd_object_release(const struct lu_env *env,
123                                 struct lu_object *l);
124 static int   osd_object_print  (const struct lu_env *env, void *cookie,
125                                 lu_printer_t p, const struct lu_object *o);
126 static struct lu_device *osd_device_free   (const struct lu_env *env,
127                                 struct lu_device *m);
128 static void *osd_key_init      (const struct lu_context *ctx,
129                                 struct lu_context_key *key);
130 static void  osd_key_fini      (const struct lu_context *ctx,
131                                 struct lu_context_key *key, void *data);
132 static void  osd_key_exit      (const struct lu_context *ctx,
133                                 struct lu_context_key *key, void *data);
134 static int   osd_has_index     (const struct osd_object *obj);
135 static void  osd_object_init0  (struct osd_object *obj);
136 static int   osd_device_init   (const struct lu_env *env,
137                                 struct lu_device *d, const char *,
138                                 struct lu_device *);
139 static int   osd_fid_lookup    (const struct lu_env *env,
140                                 struct osd_object *obj,
141                                 const struct lu_fid *fid);
142 static void  osd_inode_getattr (const struct lu_env *env,
143                                 struct inode *inode, struct lu_attr *attr);
144 static void  osd_inode_setattr (const struct lu_env *env,
145                                 struct inode *inode, const struct lu_attr *attr);
146 static int   osd_param_is_sane (const struct osd_device *dev,
147                                 const struct txn_param *param);
148 static int   osd_index_lookup  (const struct lu_env *env,
149                                 struct dt_object *dt,
150                                 struct dt_rec *rec, const struct dt_key *key,
151                                 struct lustre_capa *capa);
152 static int   osd_index_insert  (const struct lu_env *env,
153                                 struct dt_object *dt,
154                                 const struct dt_rec *rec,
155                                 const struct dt_key *key,
156                                 struct thandle *handle,
157                                 struct lustre_capa *capa);
158 static int   osd_index_delete  (const struct lu_env *env,
159                                 struct dt_object *dt, const struct dt_key *key,
160                                 struct thandle *handle,
161                                 struct lustre_capa *capa);
162 static int   osd_index_probe   (const struct lu_env *env,
163                                 struct osd_object *o,
164                                 const struct dt_index_features *feat);
165 static int   osd_index_try     (const struct lu_env *env,
166                                 struct dt_object *dt,
167                                 const struct dt_index_features *feat);
168 static void  osd_index_fini    (struct osd_object *o);
169
170 static void  osd_it_fini       (const struct lu_env *env, struct dt_it *di);
171 static int   osd_it_get        (const struct lu_env *env,
172                                 struct dt_it *di, const struct dt_key *key);
173 static void  osd_it_put        (const struct lu_env *env, struct dt_it *di);
174 static int   osd_it_next       (const struct lu_env *env, struct dt_it *di);
175 static int   osd_it_del        (const struct lu_env *env, struct dt_it *di,
176                                 struct thandle *th);
177 static int   osd_it_key_size   (const struct lu_env *env,
178                                 const struct dt_it *di);
179 static void  osd_conf_get      (const struct lu_env *env,
180                                 const struct dt_device *dev,
181                                 struct dt_device_param *param);
182 static void  osd_trans_stop    (const struct lu_env *env,
183                                 struct thandle *th);
184 static int   osd_object_is_root(const struct osd_object *obj);
185
186 static struct osd_object  *osd_obj          (const struct lu_object *o);
187 static struct osd_device  *osd_dev          (const struct lu_device *d);
188 static struct osd_device  *osd_dt_dev       (const struct dt_device *d);
189 static struct osd_object  *osd_dt_obj       (const struct dt_object *d);
190 static struct osd_device  *osd_obj2dev      (const struct osd_object *o);
191 static struct lu_device   *osd2lu_dev       (struct osd_device *osd);
192 static struct lu_device   *osd_device_fini  (const struct lu_env *env,
193                                              struct lu_device *d);
194 static struct lu_device   *osd_device_alloc (const struct lu_env *env,
195                                              struct lu_device_type *t,
196                                              struct lustre_cfg *cfg);
197 static struct lu_object   *osd_object_alloc (const struct lu_env *env,
198                                              const struct lu_object_header *hdr,
199                                              struct lu_device *d);
200 static struct inode       *osd_iget         (struct osd_thread_info *info,
201                                              struct osd_device *dev,
202                                              const struct osd_inode_id *id);
203 static struct super_block *osd_sb           (const struct osd_device *dev);
204 static struct dt_it       *osd_it_init      (const struct lu_env *env,
205                                              struct dt_object *dt, int wable,
206                                              struct lustre_capa *capa);
207 static struct dt_key      *osd_it_key       (const struct lu_env *env,
208                                              const struct dt_it *di);
209 static struct dt_rec      *osd_it_rec       (const struct lu_env *env,
210                                              const struct dt_it *di);
211 static struct timespec    *osd_inode_time   (const struct lu_env *env,
212                                              struct inode *inode,
213                                              __u64 seconds);
214 static struct thandle     *osd_trans_start  (const struct lu_env *env,
215                                              struct dt_device *d,
216                                              struct txn_param *p);
217 static journal_t          *osd_journal      (const struct osd_device *dev);
218
219 static const struct lu_device_type_operations osd_device_type_ops;
220 static struct lu_device_type            osd_device_type;
221 static const struct lu_object_operations      osd_lu_obj_ops;
222 static struct obd_ops                   osd_obd_device_ops;
223 static const struct lu_device_operations      osd_lu_ops;
224 static struct lu_context_key            osd_key;
225 static const struct dt_object_operations      osd_obj_ops;
226 static const struct dt_body_operations        osd_body_ops;
227 static const struct dt_index_operations       osd_index_ops;
228 static const struct dt_index_operations       osd_index_compat_ops;
229
230 struct osd_thandle {
231         struct thandle          ot_super;
232         handle_t               *ot_handle;
233         struct journal_callback ot_jcb;
234         /* Link to the device, for debugging. */
235         struct lu_ref_link     *ot_dev_link;
236
237 };
238
239 /*
240  * Invariants, assertions.
241  */
242
243 /*
244  * XXX: do not enable this, until invariant checking code is made thread safe
245  * in the face of pdirops locking.
246  */
247 #define OSD_INVARIANT_CHECKS (0)
248
249 #if OSD_INVARIANT_CHECKS
250 static int osd_invariant(const struct osd_object *obj)
251 {
252         return
253                 obj != NULL &&
254                 ergo(obj->oo_inode != NULL,
255                      obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
256                      atomic_read(&obj->oo_inode->i_count) > 0) &&
257                 ergo(obj->oo_dir != NULL &&
258                      obj->oo_dir->od_conationer.ic_object != NULL,
259                      obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
260 }
261 #else
262 #define osd_invariant(obj) (1)
263 #endif
264
265 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
266 {
267         return lu_context_key_get(&env->le_ctx, &osd_key);
268 }
269
270 /*
271  * Concurrency: doesn't matter
272  */
273 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
274 {
275         return osd_oti_get(env)->oti_r_locks > 0;
276 }
277
278 /*
279  * Concurrency: doesn't matter
280  */
281 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
282 {
283         struct osd_thread_info *oti = osd_oti_get(env);
284         return oti->oti_w_locks > 0 && o->oo_owner == env;
285 }
286
287 /*
288  * Concurrency: doesn't access mutable data
289  */
290 static int osd_root_get(const struct lu_env *env,
291                         struct dt_device *dev, struct lu_fid *f)
292 {
293         struct inode *inode;
294
295         inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
296         lu_igif_build(f, inode->i_ino, inode->i_generation);
297         return 0;
298 }
299
300 /*
301  * OSD object methods.
302  */
303
304 /*
305  * Concurrency: no concurrent access is possible that early in object
306  * life-cycle.
307  */
308 static struct lu_object *osd_object_alloc(const struct lu_env *env,
309                                           const struct lu_object_header *hdr,
310                                           struct lu_device *d)
311 {
312         struct osd_object *mo;
313
314         OBD_ALLOC_PTR(mo);
315         if (mo != NULL) {
316                 struct lu_object *l;
317
318                 l = &mo->oo_dt.do_lu;
319                 dt_object_init(&mo->oo_dt, NULL, d);
320                 mo->oo_dt.do_ops = &osd_obj_ops;
321                 l->lo_ops = &osd_lu_obj_ops;
322                 init_rwsem(&mo->oo_sem);
323                 spin_lock_init(&mo->oo_guard);
324                 return l;
325         } else
326                 return NULL;
327 }
328
329 /*
330  * Concurrency: shouldn't matter.
331  */
332 static void osd_object_init0(struct osd_object *obj)
333 {
334         LASSERT(obj->oo_inode != NULL);
335         obj->oo_dt.do_body_ops = &osd_body_ops;
336         obj->oo_dt.do_lu.lo_header->loh_attr |=
337                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
338 }
339
340 /*
341  * Concurrency: no concurrent access is possible that early in object
342  * life-cycle.
343  */
344 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
345                            const struct lu_object_conf *_)
346 {
347         struct osd_object *obj = osd_obj(l);
348         int result;
349
350         LINVRNT(osd_invariant(obj));
351
352         result = osd_fid_lookup(env, obj, lu_object_fid(l));
353         if (result == 0) {
354                 if (obj->oo_inode != NULL)
355                         osd_object_init0(obj);
356         }
357         LINVRNT(osd_invariant(obj));
358         return result;
359 }
360
361 /*
362  * Concurrency: no concurrent access is possible that late in object
363  * life-cycle.
364  */
365 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
366 {
367         struct osd_object *obj = osd_obj(l);
368
369         LINVRNT(osd_invariant(obj));
370
371         dt_object_fini(&obj->oo_dt);
372         OBD_FREE_PTR(obj);
373 }
374
375 static struct iam_path_descr *osd_ipd_get(const struct lu_env *env,
376                                           const struct iam_container *bag)
377 {
378         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
379                                                    osd_oti_get(env)->oti_ipd);
380 }
381
382 static void osd_ipd_put(const struct lu_env *env,
383                         const struct iam_container *bag,
384                         struct iam_path_descr *ipd)
385 {
386         bag->ic_descr->id_ops->id_ipd_free(ipd);
387 }
388
389 /*
390  * Concurrency: no concurrent access is possible that late in object
391  * life-cycle.
392  */
393 static void osd_index_fini(struct osd_object *o)
394 {
395         struct iam_container *bag;
396
397         if (o->oo_dir != NULL) {
398                 bag = &o->oo_dir->od_container;
399                 if (o->oo_inode != NULL) {
400                         if (bag->ic_object == o->oo_inode)
401                                 iam_container_fini(bag);
402                 }
403                 OBD_FREE_PTR(o->oo_dir);
404                 o->oo_dir = NULL;
405         }
406 }
407
408 /*
409  * Concurrency: no concurrent access is possible that late in object
410  * life-cycle (for all existing callers, that is. New callers have to provide
411  * their own locking.)
412  */
413 static int osd_inode_unlinked(const struct inode *inode)
414 {
415         return inode->i_nlink == 0;
416 }
417
418 enum {
419         OSD_TXN_OI_DELETE_CREDITS    = 20,
420         OSD_TXN_INODE_DELETE_CREDITS = 20
421 };
422
423 /*
424  * Concurrency: no concurrent access is possible that late in object
425  * life-cycle.
426  */
427 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
428 {
429         const struct lu_fid    *fid = lu_object_fid(&obj->oo_dt.do_lu);
430         struct osd_device      *osd = osd_obj2dev(obj);
431         struct osd_thread_info *oti = osd_oti_get(env);
432         struct txn_param       *prm = &oti->oti_txn;
433         struct thandle         *th;
434         int result;
435
436         txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS + 
437                             OSD_TXN_INODE_DELETE_CREDITS);
438         th = osd_trans_start(env, &osd->od_dt_dev, prm);
439         if (!IS_ERR(th)) {
440                 result = osd_oi_delete(oti, &osd->od_oi, fid, th);
441                 osd_trans_stop(env, th);
442         } else
443                 result = PTR_ERR(th);
444         return result;
445 }
446
447 /*
448  * Called just before object is freed. Releases all resources except for
449  * object itself (that is released by osd_object_free()).
450  *
451  * Concurrency: no concurrent access is possible that late in object
452  * life-cycle.
453  */
454 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
455 {
456         struct osd_object *obj   = osd_obj(l);
457         struct inode      *inode = obj->oo_inode;
458
459         LINVRNT(osd_invariant(obj));
460
461         /*
462          * If object is unlinked remove fid->ino mapping from object index.
463          *
464          * File body will be deleted by iput().
465          */
466
467         osd_index_fini(obj);
468         if (inode != NULL) {
469                 int result;
470
471                 if (osd_inode_unlinked(inode)) {
472                         result = osd_inode_remove(env, obj);
473                         if (result != 0)
474                                 LU_OBJECT_DEBUG(D_ERROR, env, l,
475                                                 "Failed to cleanup: %d\n",
476                                                 result);
477                 }
478                 iput(inode);
479                 obj->oo_inode = NULL;
480         }
481 }
482
483 /*
484  * Concurrency: ->loo_object_release() is called under site spin-lock.
485  */
486 static void osd_object_release(const struct lu_env *env,
487                                struct lu_object *l)
488 {
489         struct osd_object *o = osd_obj(l);
490
491         LASSERT(!lu_object_is_dying(l->lo_header));
492         if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
493                 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
494 }
495
496 /*
497  * Concurrency: shouldn't matter.
498  */
499 static int osd_object_print(const struct lu_env *env, void *cookie,
500                             lu_printer_t p, const struct lu_object *l)
501 {
502         struct osd_object *o = osd_obj(l);
503         struct iam_descr  *d;
504
505         if (o->oo_dir != NULL)
506                 d = o->oo_dir->od_container.ic_descr;
507         else
508                 d = NULL;
509         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
510                     o, o->oo_inode,
511                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
512                     o->oo_inode ? o->oo_inode->i_generation : 0,
513                     d ? d->id_ops->id_name : "plain");
514 }
515
516 /*
517  * Concurrency: shouldn't matter.
518  */
519 int osd_statfs(const struct lu_env *env, struct dt_device *d,
520                struct kstatfs *sfs)
521 {
522         struct osd_device *osd = osd_dt_dev(d);
523         struct super_block *sb = osd_sb(osd);
524         int result = 0;
525
526         spin_lock(&osd->od_osfs_lock);
527         /* cache 1 second */
528         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
529                 result = ll_do_statfs(sb, &osd->od_kstatfs);
530                 if (likely(result == 0)) /* N.B. statfs can't really fail */
531                         osd->od_osfs_age = cfs_time_current_64();
532         }
533
534         if (likely(result == 0))
535                 *sfs = osd->od_kstatfs; 
536         spin_unlock(&osd->od_osfs_lock);
537
538         return result;
539 }
540
541 /*
542  * Concurrency: doesn't access mutable data.
543  */
544 static void osd_conf_get(const struct lu_env *env,
545                          const struct dt_device *dev,
546                          struct dt_device_param *param)
547 {
548         /*
549          * XXX should be taken from not-yet-existing fs abstraction layer.
550          */
551         param->ddp_max_name_len  = LDISKFS_NAME_LEN;
552         param->ddp_max_nlink     = LDISKFS_LINK_MAX;
553         param->ddp_block_shift   = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
554 }
555
556 /*
557  * Journal
558  */
559
560 /*
561  * Concurrency: doesn't access mutable data.
562  */
563 static int osd_param_is_sane(const struct osd_device *dev,
564                              const struct txn_param *param)
565 {
566         return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
567 }
568
569 /*
570  * Concurrency: shouldn't matter.
571  */
572 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
573 {
574         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
575         struct thandle     *th = &oh->ot_super;
576         struct dt_device   *dev = th->th_dev;
577         struct lu_device   *lud = &dev->dd_lu_dev;
578
579         LASSERT(dev != NULL);
580         LASSERT(oh->ot_handle == NULL);
581
582         if (error) {
583                 CERROR("transaction @0x%p commit error: %d\n", th, error);
584         } else {
585                 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
586                 /*
587                  * This od_env_for_commit is only for commit usage.  see
588                  * "struct dt_device"
589                  */
590                 lu_context_enter(&env->le_ctx);
591                 dt_txn_hook_commit(env, th);
592                 lu_context_exit(&env->le_ctx);
593         }
594
595         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
596         lu_device_put(lud);
597         th->th_dev = NULL;
598
599         lu_context_exit(&th->th_ctx);
600         lu_context_fini(&th->th_ctx);
601         OBD_FREE_PTR(oh);
602 }
603
604 /*
605  * Concurrency: shouldn't matter.
606  */
607 static struct thandle *osd_trans_start(const struct lu_env *env,
608                                        struct dt_device *d,
609                                        struct txn_param *p)
610 {
611         struct osd_device  *dev = osd_dt_dev(d);
612         handle_t           *jh;
613         struct osd_thandle *oh;
614         struct thandle     *th;
615         int hook_res;
616
617         ENTRY;
618
619         hook_res = dt_txn_hook_start(env, d, p);
620         if (hook_res != 0)
621                 RETURN(ERR_PTR(hook_res));
622
623         if (osd_param_is_sane(dev, p)) {
624                 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
625                 if (oh != NULL) {
626                         struct osd_thread_info *oti = osd_oti_get(env);
627
628                         /*
629                          * XXX temporary stuff. Some abstraction layer should
630                          * be used.
631                          */
632
633                         jh = journal_start(osd_journal(dev), p->tp_credits);
634                         if (!IS_ERR(jh)) {
635                                 oh->ot_handle = jh;
636                                 th = &oh->ot_super;
637                                 th->th_dev = d;
638                                 th->th_result = 0;
639                                 jh->h_sync = p->tp_sync;
640                                 lu_device_get(&d->dd_lu_dev);
641                                 oh->ot_dev_link = lu_ref_add
642                                         (&d->dd_lu_dev.ld_reference,
643                                          "osd-tx", th);
644                                 /* add commit callback */
645                                 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
646                                 lu_context_enter(&th->th_ctx);
647                                 journal_callback_set(jh, osd_trans_commit_cb,
648                                                      (struct journal_callback *)&oh->ot_jcb);
649                                         LASSERT(oti->oti_txns == 0);
650                                         LASSERT(oti->oti_r_locks == 0);
651                                         LASSERT(oti->oti_w_locks == 0);
652                                         oti->oti_txns++;
653                         } else {
654                                 OBD_FREE_PTR(oh);
655                                 th = (void *)jh;
656                         }
657                 } else
658                         th = ERR_PTR(-ENOMEM);
659         } else {
660                 CERROR("Invalid transaction parameters\n");
661                 th = ERR_PTR(-EINVAL);
662         }
663
664         RETURN(th);
665 }
666
667 /*
668  * Concurrency: shouldn't matter.
669  */
670 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
671 {
672         int result;
673         struct osd_thandle *oh;
674         struct osd_thread_info *oti = osd_oti_get(env);
675
676         ENTRY;
677
678         oh = container_of0(th, struct osd_thandle, ot_super);
679         if (oh->ot_handle != NULL) {
680                 handle_t *hdl = oh->ot_handle;
681
682                 LASSERT(oti->oti_txns == 1);
683                 oti->oti_txns--;
684                 LASSERT(oti->oti_r_locks == 0);
685                 LASSERT(oti->oti_w_locks == 0);
686                 result = dt_txn_hook_stop(env, th);
687                 if (result != 0)
688                         CERROR("Failure in transaction hook: %d\n", result);
689                 oh->ot_handle = NULL;
690                 result = journal_stop(hdl);
691                 if (result != 0)
692                         CERROR("Failure to stop transaction: %d\n", result);
693         }
694         EXIT;
695 }
696
697 /*
698  * Concurrency: shouldn't matter.
699  */
700 static int osd_sync(const struct lu_env *env, struct dt_device *d)
701 {
702         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
703         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
704 }
705
706 /*
707  * Concurrency: shouldn't matter.
708  */
709 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
710
711 static void osd_ro(const struct lu_env *env, struct dt_device *d)
712 {
713         ENTRY;
714
715         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
716
717         __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
718                           fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
719         EXIT;
720 }
721
722 /*
723  * Concurrency: serialization provided by callers.
724  */
725 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
726                               int mode, unsigned long timeout, __u32 alg,
727                               struct lustre_capa_key *keys)
728 {
729         struct osd_device *dev = osd_dt_dev(d);
730         ENTRY;
731
732         dev->od_fl_capa = mode;
733         dev->od_capa_timeout = timeout;
734         dev->od_capa_alg = alg;
735         dev->od_capa_keys = keys;
736         RETURN(0);
737 }
738
739 /* Note: we did not count into QUOTA here, If we mount with --data_journal
740  * we may need more*/
741 static const int osd_dto_credits[DTO_NR] = {
742         /*
743          * Insert/Delete. IAM EXT3_INDEX_EXTRA_TRANS_BLOCKS(8) +
744          * EXT3_SINGLEDATA_TRANS_BLOCKS 8 XXX Note: maybe iam need more,since
745          * iam have more level than Ext3 htree
746          */
747         [DTO_INDEX_INSERT]  = 16,
748         [DTO_INDEX_DELETE]  = 16,
749         [DTO_IDNEX_UPDATE]  = 16,
750         /*
751          * Create a object. Same as create object in Ext3 filesystem, but did
752          * not count QUOTA i EXT3_DATA_TRANS_BLOCKS(12) +
753          * INDEX_EXTRA_BLOCKS(8) + 3(inode bits,groups, GDT)
754          */
755         [DTO_OBJECT_CREATE] = 23,
756         [DTO_OBJECT_DELETE] = 23,
757         /*
758          * Attr set credits 3 inode, group, GDT
759          */
760         [DTO_ATTR_SET]      = 3,
761         /*
762          * XATTR_SET. SAME AS XATTR of EXT3 EXT3_DATA_TRANS_BLOCKS XXX Note:
763          * in original MDS implmentation EXT3_INDEX_EXTRA_TRANS_BLOCKS are
764          * also counted in. Do not know why?
765          */
766         [DTO_XATTR_SET]     = 16,
767         [DTO_LOG_REC]       = 16,
768         /* creadits for inode change during write */
769         [DTO_WRITE_BASE]    = 3,
770         /* credits for single block write */
771         [DTO_WRITE_BLOCK]   = 12 
772 };
773
774 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
775                           enum dt_txn_op op)
776 {
777         LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits));
778         return osd_dto_credits[op];
779 }
780
781 static const struct dt_device_operations osd_dt_ops = {
782         .dt_root_get       = osd_root_get,
783         .dt_statfs         = osd_statfs,
784         .dt_trans_start    = osd_trans_start,
785         .dt_trans_stop     = osd_trans_stop,
786         .dt_conf_get       = osd_conf_get,
787         .dt_sync           = osd_sync,
788         .dt_ro             = osd_ro,
789         .dt_credit_get     = osd_credit_get,
790         .dt_init_capa_ctxt = osd_init_capa_ctxt,
791 };
792
793 static void osd_object_read_lock(const struct lu_env *env,
794                                  struct dt_object *dt, unsigned role)
795 {
796         struct osd_object *obj = osd_dt_obj(dt);
797         struct osd_thread_info *oti = osd_oti_get(env);
798
799         LINVRNT(osd_invariant(obj));
800
801         LASSERT(obj->oo_owner != env);
802         down_read_nested(&obj->oo_sem, role);
803
804                 LASSERT(obj->oo_owner == NULL);
805                 oti->oti_r_locks++;
806 }
807
808 static void osd_object_write_lock(const struct lu_env *env,
809                                   struct dt_object *dt, unsigned role)
810 {
811         struct osd_object *obj = osd_dt_obj(dt);
812         struct osd_thread_info *oti = osd_oti_get(env);
813
814         LINVRNT(osd_invariant(obj));
815
816         LASSERT(obj->oo_owner != env);
817         down_write_nested(&obj->oo_sem, role);
818
819                 LASSERT(obj->oo_owner == NULL);
820                 obj->oo_owner = env;
821                 oti->oti_w_locks++;
822 }
823
824 static void osd_object_read_unlock(const struct lu_env *env,
825                                    struct dt_object *dt)
826 {
827         struct osd_object *obj = osd_dt_obj(dt);
828                 struct osd_thread_info *oti = osd_oti_get(env);
829
830         LINVRNT(osd_invariant(obj));
831
832                 LASSERT(oti->oti_r_locks > 0);
833                 oti->oti_r_locks--;
834         up_read(&obj->oo_sem);
835 }
836
837 static void osd_object_write_unlock(const struct lu_env *env,
838                                     struct dt_object *dt)
839 {
840         struct osd_object *obj = osd_dt_obj(dt);
841                 struct osd_thread_info *oti = osd_oti_get(env);
842
843         LINVRNT(osd_invariant(obj));
844
845                 LASSERT(obj->oo_owner == env);
846                 LASSERT(oti->oti_w_locks > 0);
847                 oti->oti_w_locks--;
848                 obj->oo_owner = NULL;
849         up_write(&obj->oo_sem);
850 }
851
852 static int capa_is_sane(const struct lu_env *env,
853                         struct osd_device *dev,
854                         struct lustre_capa *capa,
855                         struct lustre_capa_key *keys)
856 {
857         struct osd_thread_info *oti = osd_oti_get(env);
858         struct obd_capa *oc;
859         int i, rc = 0;
860         ENTRY;
861
862         oc = capa_lookup(dev->od_capa_hash, capa, 0);
863         if (oc) {
864                 if (capa_is_expired(oc)) {
865                         DEBUG_CAPA(D_ERROR, capa, "expired");
866                         rc = -ESTALE;
867                 }
868                 capa_put(oc);
869                 RETURN(rc);
870         }
871
872         spin_lock(&capa_lock);
873         for (i = 0; i < 2; i++) {
874                 if (keys[i].lk_keyid == capa->lc_keyid) {
875                         oti->oti_capa_key = keys[i];
876                         break;
877                 }
878         }
879         spin_unlock(&capa_lock);
880
881         if (i == 2) {
882                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
883                 RETURN(-ESTALE);
884         }
885
886         rc = capa_hmac(oti->oti_capa.lc_hmac, capa, oti->oti_capa_key.lk_key);
887         if (rc)
888                 RETURN(rc);
889         if (memcmp(oti->oti_capa.lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac)))
890         {
891                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
892                 RETURN(-EACCES);
893         }
894
895         oc = capa_add(dev->od_capa_hash, capa);
896         capa_put(oc);
897
898         RETURN(0);
899 }
900
901 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
902                            struct lustre_capa *capa, __u64 opc)
903 {
904         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
905         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
906         int rc;
907
908         if (!dev->od_fl_capa)
909                 return 0;
910
911         if (capa == BYPASS_CAPA)
912                 return 0;
913
914         if (!capa) {
915                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
916                 return -EACCES;
917         }
918
919         if (!lu_fid_eq(fid, &capa->lc_fid)) {
920                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
921                            PFID(fid));
922                 return -EACCES;
923         }
924
925         if (!capa_opc_supported(capa, opc)) {
926                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
927                 return -EACCES;
928         }
929
930         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
931                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
932                 return -EACCES;
933         }
934
935         return 0;
936 }
937
938 static int osd_attr_get(const struct lu_env *env,
939                         struct dt_object *dt,
940                         struct lu_attr *attr,
941                         struct lustre_capa *capa)
942 {
943         struct osd_object *obj = osd_dt_obj(dt);
944
945         LASSERT(dt_object_exists(dt));
946         LINVRNT(osd_invariant(obj));
947
948         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
949                 return -EACCES;
950
951         spin_lock(&obj->oo_guard);
952         osd_inode_getattr(env, obj->oo_inode, attr);
953         spin_unlock(&obj->oo_guard);
954         return 0;
955 }
956
957 static int osd_attr_set(const struct lu_env *env,
958                         struct dt_object *dt,
959                         const struct lu_attr *attr,
960                         struct thandle *handle,
961                         struct lustre_capa *capa)
962 {
963         struct osd_object *obj = osd_dt_obj(dt);
964
965         LASSERT(handle != NULL);
966         LASSERT(dt_object_exists(dt));
967         LASSERT(osd_invariant(obj));
968
969         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
970                 return -EACCES;
971
972         spin_lock(&obj->oo_guard);
973         osd_inode_setattr(env, obj->oo_inode, attr);
974         spin_unlock(&obj->oo_guard);
975
976         mark_inode_dirty(obj->oo_inode);
977         return 0;
978 }
979
980 static struct timespec *osd_inode_time(const struct lu_env *env,
981                                        struct inode *inode, __u64 seconds)
982 {
983         struct osd_thread_info *oti = osd_oti_get(env);
984         struct timespec        *t   = &oti->oti_time;
985
986         t->tv_sec  = seconds;
987         t->tv_nsec = 0;
988         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
989         return t;
990 }
991
992 static void osd_inode_setattr(const struct lu_env *env,
993                               struct inode *inode, const struct lu_attr *attr)
994 {
995         __u64 bits;
996
997         bits = attr->la_valid;
998
999         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1000
1001         if (bits & LA_ATIME)
1002                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1003         if (bits & LA_CTIME)
1004                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1005         if (bits & LA_MTIME)
1006                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1007         if (bits & LA_SIZE) {
1008                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1009                 i_size_write(inode, attr->la_size);
1010         }
1011         if (bits & LA_BLOCKS)
1012                 inode->i_blocks = attr->la_blocks;
1013         if (bits & LA_MODE)
1014                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1015                         (attr->la_mode & ~S_IFMT);
1016         if (bits & LA_UID)
1017                 inode->i_uid    = attr->la_uid;
1018         if (bits & LA_GID)
1019                 inode->i_gid    = attr->la_gid;
1020         if (bits & LA_NLINK)
1021                 inode->i_nlink  = attr->la_nlink;
1022         if (bits & LA_RDEV)
1023                 inode->i_rdev   = attr->la_rdev;
1024
1025         if (bits & LA_FLAGS) {
1026                 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1027
1028                 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1029                         (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1030         }
1031 }
1032
1033 /*
1034  * Object creation.
1035  *
1036  * XXX temporary solution.
1037  */
1038
1039 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1040                           struct lu_attr *attr, struct thandle *th)
1041 {
1042         return 0;
1043 }
1044
1045 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1046                            struct lu_attr *attr, struct thandle *th)
1047 {
1048         LASSERT(obj->oo_inode != NULL);
1049
1050         osd_object_init0(obj);
1051         return 0;
1052 }
1053
1054 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1055                                           struct inode * dir, int mode);
1056
1057 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1058                       umode_t mode,
1059                       struct dt_allocation_hint *hint,
1060                       struct thandle *th)
1061 {
1062         int result;
1063         struct osd_device  *osd = osd_obj2dev(obj);
1064         struct osd_thandle *oth;
1065         struct inode       *parent;
1066         struct inode       *inode;
1067
1068         LINVRNT(osd_invariant(obj));
1069         LASSERT(obj->oo_inode == NULL);
1070         LASSERT(osd->od_obj_area != NULL);
1071
1072         oth = container_of(th, struct osd_thandle, ot_super);
1073         LASSERT(oth->ot_handle->h_transaction != NULL);
1074
1075         if (hint && hint->dah_parent)
1076                 parent = osd_dt_obj(hint->dah_parent)->oo_inode;
1077         else
1078                 parent = osd->od_obj_area->d_inode;
1079         LASSERT(parent->i_op != NULL);
1080
1081         inode = ldiskfs_create_inode(oth->ot_handle, parent, mode);
1082         if (!IS_ERR(inode)) {
1083                 obj->oo_inode = inode;
1084                 result = 0;
1085         } else
1086                 result = PTR_ERR(inode);
1087         LINVRNT(osd_invariant(obj));
1088         return result;
1089 }
1090
1091
1092 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1093                            int recsize, handle_t *handle);
1094
1095 enum {
1096         OSD_NAME_LEN = 255
1097 };
1098
1099 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1100                      struct lu_attr *attr,
1101                      struct dt_allocation_hint *hint,
1102                      struct thandle *th)
1103 {
1104         int result;
1105         struct osd_thandle *oth;
1106
1107         LASSERT(S_ISDIR(attr->la_mode));
1108
1109         oth = container_of(th, struct osd_thandle, ot_super);
1110         LASSERT(oth->ot_handle->h_transaction != NULL);
1111         result = osd_mkfile(info, obj, (attr->la_mode &
1112                             (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1113         if (result == 0) {
1114                 LASSERT(obj->oo_inode != NULL);
1115                 /*
1116                  * XXX uh-oh... call low-level iam function directly.
1117                  */
1118                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1119                                          sizeof (struct lu_fid_pack),
1120                                          oth->ot_handle);
1121         }
1122         return result;
1123 }
1124
1125 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1126                      struct lu_attr *attr,
1127                      struct dt_allocation_hint *hint,
1128                      struct thandle *th)
1129 {
1130         LASSERT(S_ISREG(attr->la_mode));
1131         return osd_mkfile(info, obj, (attr->la_mode &
1132                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1133 }
1134
1135 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1136                      struct lu_attr *attr,
1137                      struct dt_allocation_hint *hint,
1138                      struct thandle *th)
1139 {
1140         LASSERT(S_ISLNK(attr->la_mode));
1141         return osd_mkfile(info, obj, (attr->la_mode &
1142                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1143 }
1144
1145 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1146                      struct lu_attr *attr,
1147                      struct dt_allocation_hint *hint,
1148                      struct thandle *th)
1149 {
1150         int result;
1151         struct osd_device *osd = osd_obj2dev(obj);
1152         struct inode      *dir;
1153         umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1154
1155         LINVRNT(osd_invariant(obj));
1156         LASSERT(obj->oo_inode == NULL);
1157         LASSERT(osd->od_obj_area != NULL);
1158         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1159                 S_ISFIFO(mode) || S_ISSOCK(mode));
1160
1161         dir = osd->od_obj_area->d_inode;
1162         LASSERT(dir->i_op != NULL);
1163
1164         result = osd_mkfile(info, obj, mode, hint, th);
1165         if (result == 0) {
1166                 LASSERT(obj->oo_inode != NULL);
1167                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1168         }
1169         LINVRNT(osd_invariant(obj));
1170         return result;
1171 }
1172
1173 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1174                               struct lu_attr *,
1175                               struct dt_allocation_hint *hint,
1176                               struct thandle *);
1177
1178 static osd_obj_type_f osd_create_type_f(__u32 mode)
1179 {
1180         osd_obj_type_f result;
1181
1182         switch (mode) {
1183         case S_IFDIR:
1184                 result = osd_mkdir;
1185                 break;
1186         case S_IFREG:
1187                 result = osd_mkreg;
1188                 break;
1189         case S_IFLNK:
1190                 result = osd_mksym;
1191                 break;
1192         case S_IFCHR:
1193         case S_IFBLK:
1194         case S_IFIFO:
1195         case S_IFSOCK:
1196                 result = osd_mknod;
1197                 break;
1198         default:
1199                 LBUG();
1200                 break;
1201         }
1202         return result;
1203 }
1204
1205
1206 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1207                         struct dt_object *parent, umode_t child_mode)
1208 {
1209         LASSERT(ah);
1210
1211         memset(ah, 0, sizeof(*ah));
1212         ah->dah_parent = parent;
1213         ah->dah_mode = child_mode;
1214 }
1215
1216
1217 /*
1218  * Concurrency: @dt is write locked.
1219  */
1220 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1221                              struct lu_attr *attr, 
1222                              struct dt_allocation_hint *hint,
1223                              struct thandle *th)
1224 {
1225         const struct lu_fid    *fid  = lu_object_fid(&dt->do_lu);
1226         struct osd_object      *obj  = osd_dt_obj(dt);
1227         struct osd_device      *osd  = osd_obj2dev(obj);
1228         struct osd_thread_info *info = osd_oti_get(env);
1229         int result;
1230
1231         ENTRY;
1232
1233         LINVRNT(osd_invariant(obj));
1234         LASSERT(!dt_object_exists(dt));
1235         LASSERT(osd_write_locked(env, obj));
1236         LASSERT(th != NULL);
1237
1238         /*
1239          * XXX missing: Quote handling.
1240          */
1241
1242         result = osd_create_pre(info, obj, attr, th);
1243         if (result == 0) {
1244                 result = osd_create_type_f(attr->la_mode & S_IFMT)(info, obj,
1245                                                                 attr, hint, th);
1246                 if (result == 0)
1247                         result = osd_create_post(info, obj, attr, th);
1248         }
1249         if (result == 0) {
1250                 struct osd_inode_id *id = &info->oti_id;
1251
1252                 LASSERT(obj->oo_inode != NULL);
1253
1254                 id->oii_ino = obj->oo_inode->i_ino;
1255                 id->oii_gen = obj->oo_inode->i_generation;
1256
1257                 result = osd_oi_insert(info, &osd->od_oi, fid, id, th);
1258         }
1259
1260         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1261         LINVRNT(osd_invariant(obj));
1262         RETURN(result);
1263 }
1264
1265 /*
1266  * Concurrency: @dt is write locked.
1267  */
1268 static void osd_object_ref_add(const struct lu_env *env,
1269                                struct dt_object *dt,
1270                                struct thandle *th)
1271 {
1272         struct osd_object *obj = osd_dt_obj(dt);
1273         struct inode *inode = obj->oo_inode;
1274
1275         LINVRNT(osd_invariant(obj));
1276         LASSERT(dt_object_exists(dt));
1277         LASSERT(osd_write_locked(env, obj));
1278         LASSERT(th != NULL);
1279
1280         spin_lock(&obj->oo_guard);
1281         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1282         inode->i_nlink++;
1283         spin_unlock(&obj->oo_guard);
1284         mark_inode_dirty(inode);
1285         LINVRNT(osd_invariant(obj));
1286 }
1287
1288 /*
1289  * Concurrency: @dt is write locked.
1290  */
1291 static void osd_object_ref_del(const struct lu_env *env,
1292                                struct dt_object *dt,
1293                                struct thandle *th)
1294 {
1295         struct osd_object *obj = osd_dt_obj(dt);
1296         struct inode *inode = obj->oo_inode;
1297
1298         LINVRNT(osd_invariant(obj));
1299         LASSERT(dt_object_exists(dt));
1300         LASSERT(osd_write_locked(env, obj));
1301         LASSERT(th != NULL);
1302
1303         spin_lock(&obj->oo_guard);
1304         LASSERT(inode->i_nlink > 0);
1305         inode->i_nlink--;
1306         spin_unlock(&obj->oo_guard);
1307         mark_inode_dirty(inode);
1308         LINVRNT(osd_invariant(obj));
1309 }
1310
1311 /*
1312  * Concurrency: @dt is read locked.
1313  */
1314 static int osd_xattr_get(const struct lu_env *env,
1315                          struct dt_object *dt,
1316                          struct lu_buf *buf,
1317                          const char *name,
1318                          struct lustre_capa *capa)
1319 {
1320         struct osd_object      *obj    = osd_dt_obj(dt);
1321         struct inode           *inode  = obj->oo_inode;
1322         struct osd_thread_info *info   = osd_oti_get(env);
1323         struct dentry          *dentry = &info->oti_dentry;
1324
1325         LASSERT(dt_object_exists(dt));
1326         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1327         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1328
1329         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1330                 return -EACCES;
1331
1332         dentry->d_inode = inode;
1333         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1334 }
1335
1336 /*
1337  * Concurrency: @dt is write locked.
1338  */
1339 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1340                          const struct lu_buf *buf, const char *name, int fl,
1341                          struct thandle *handle, struct lustre_capa *capa)
1342 {
1343         struct osd_object      *obj    = osd_dt_obj(dt);
1344         struct inode           *inode  = obj->oo_inode;
1345         struct osd_thread_info *info   = osd_oti_get(env);
1346         struct dentry          *dentry = &info->oti_dentry;
1347         struct timespec        *t      = &info->oti_time;
1348         int                     fs_flags = 0, rc;
1349
1350         LASSERT(dt_object_exists(dt));
1351         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1352         LASSERT(osd_write_locked(env, obj));
1353         LASSERT(handle != NULL);
1354
1355         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1356                 return -EACCES;
1357
1358         if (fl & LU_XATTR_REPLACE)
1359                 fs_flags |= XATTR_REPLACE;
1360
1361         if (fl & LU_XATTR_CREATE)
1362                 fs_flags |= XATTR_CREATE;
1363
1364         dentry->d_inode = inode;
1365         *t = inode->i_ctime;
1366         rc = inode->i_op->setxattr(dentry, name,
1367                                    buf->lb_buf, buf->lb_len, fs_flags);
1368         if (likely(rc == 0)) {
1369                 /* ctime should not be updated with server-side time. */
1370                 spin_lock(&obj->oo_guard);
1371                 inode->i_ctime = *t;
1372                 spin_unlock(&obj->oo_guard);
1373                 mark_inode_dirty(inode);
1374         }
1375         return rc;
1376 }
1377
1378 /*
1379  * Concurrency: @dt is read locked.
1380  */
1381 static int osd_xattr_list(const struct lu_env *env,
1382                           struct dt_object *dt,
1383                           struct lu_buf *buf,
1384                           struct lustre_capa *capa)
1385 {
1386         struct osd_object      *obj    = osd_dt_obj(dt);
1387         struct inode           *inode  = obj->oo_inode;
1388         struct osd_thread_info *info   = osd_oti_get(env);
1389         struct dentry          *dentry = &info->oti_dentry;
1390
1391         LASSERT(dt_object_exists(dt));
1392         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1393         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1394
1395         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1396                 return -EACCES;
1397
1398         dentry->d_inode = inode;
1399         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1400 }
1401
1402 /*
1403  * Concurrency: @dt is write locked.
1404  */
1405 static int osd_xattr_del(const struct lu_env *env,
1406                          struct dt_object *dt,
1407                          const char *name,
1408                          struct thandle *handle,
1409                          struct lustre_capa *capa)
1410 {
1411         struct osd_object      *obj    = osd_dt_obj(dt);
1412         struct inode           *inode  = obj->oo_inode;
1413         struct osd_thread_info *info   = osd_oti_get(env);
1414         struct dentry          *dentry = &info->oti_dentry;
1415         struct timespec        *t      = &info->oti_time;
1416         int                     rc;
1417
1418         LASSERT(dt_object_exists(dt));
1419         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1420         LASSERT(osd_write_locked(env, obj));
1421         LASSERT(handle != NULL);
1422
1423         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1424                 return -EACCES;
1425
1426         dentry->d_inode = inode;
1427         *t = inode->i_ctime;
1428         rc = inode->i_op->removexattr(dentry, name);
1429         if (likely(rc == 0)) {
1430                 /* ctime should not be updated with server-side time. */
1431                 spin_lock(&obj->oo_guard);
1432                 inode->i_ctime = *t;
1433                 spin_unlock(&obj->oo_guard);
1434                 mark_inode_dirty(inode);
1435         }
1436         return rc;
1437 }
1438
1439 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1440                                      struct dt_object *dt,
1441                                      struct lustre_capa *old,
1442                                      __u64 opc)
1443 {
1444         struct osd_thread_info *info = osd_oti_get(env);
1445         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1446         struct osd_object *obj = osd_dt_obj(dt);
1447         struct osd_device *dev = osd_obj2dev(obj);
1448         struct lustre_capa_key *key = &info->oti_capa_key;
1449         struct lustre_capa *capa = &info->oti_capa;
1450         struct obd_capa *oc;
1451         int rc;
1452         ENTRY;
1453
1454         if (!dev->od_fl_capa)
1455                 RETURN(ERR_PTR(-ENOENT));
1456
1457         LASSERT(dt_object_exists(dt));
1458         LINVRNT(osd_invariant(obj));
1459
1460         /* renewal sanity check */
1461         if (old && osd_object_auth(env, dt, old, opc))
1462                 RETURN(ERR_PTR(-EACCES));
1463
1464         capa->lc_fid = *fid;
1465         capa->lc_opc = opc;
1466         capa->lc_uid = 0;
1467         capa->lc_flags = dev->od_capa_alg << 24;
1468         capa->lc_timeout = dev->od_capa_timeout;
1469         capa->lc_expiry = 0;
1470
1471         oc = capa_lookup(dev->od_capa_hash, capa, 1);
1472         if (oc) {
1473                 LASSERT(!capa_is_expired(oc));
1474                 RETURN(oc);
1475         }
1476
1477         spin_lock(&capa_lock);
1478         *key = dev->od_capa_keys[1];
1479         spin_unlock(&capa_lock);
1480
1481         capa->lc_keyid = key->lk_keyid;
1482         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
1483
1484         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
1485         if (rc) {
1486                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
1487                 RETURN(ERR_PTR(rc));
1488         }
1489
1490         oc = capa_add(dev->od_capa_hash, capa);
1491         RETURN(oc);
1492 }
1493
1494 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
1495 {
1496         int rc;
1497         struct osd_object      *obj    = osd_dt_obj(dt);
1498         struct inode           *inode  = obj->oo_inode;
1499         struct osd_thread_info *info   = osd_oti_get(env);
1500         struct dentry          *dentry = &info->oti_dentry;
1501         struct file            *file   = &info->oti_file;
1502         ENTRY;
1503
1504         dentry->d_inode = inode;
1505         file->f_dentry = dentry;
1506         file->f_mapping = inode->i_mapping;
1507         file->f_op = inode->i_fop;
1508         LOCK_INODE_MUTEX(inode);
1509         rc = file->f_op->fsync(file, dentry, 0);
1510         UNLOCK_INODE_MUTEX(inode);
1511         RETURN(rc);
1512 }
1513
1514 static const struct dt_object_operations osd_obj_ops = {
1515         .do_read_lock    = osd_object_read_lock,
1516         .do_write_lock   = osd_object_write_lock,
1517         .do_read_unlock  = osd_object_read_unlock,
1518         .do_write_unlock = osd_object_write_unlock,
1519         .do_attr_get     = osd_attr_get,
1520         .do_attr_set     = osd_attr_set,
1521         .do_ah_init      = osd_ah_init,
1522         .do_create       = osd_object_create,
1523         .do_index_try    = osd_index_try,
1524         .do_ref_add      = osd_object_ref_add,
1525         .do_ref_del      = osd_object_ref_del,
1526         .do_xattr_get    = osd_xattr_get,
1527         .do_xattr_set    = osd_xattr_set,
1528         .do_xattr_del    = osd_xattr_del,
1529         .do_xattr_list   = osd_xattr_list,
1530         .do_capa_get     = osd_capa_get,
1531         .do_object_sync  = osd_object_sync,
1532 };
1533
1534 /*
1535  * Body operations.
1536  */
1537
1538 /*
1539  * XXX: Another layering violation for now.
1540  *
1541  * We don't want to use ->f_op->read methods, because generic file write
1542  *
1543  *         - serializes on ->i_sem, and
1544  *
1545  *         - does a lot of extra work like balance_dirty_pages(),
1546  *
1547  * which doesn't work for globally shared files like /last-received.
1548  */
1549 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
1550 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
1551                                 loff_t *offs, handle_t *handle);
1552
1553 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1554                         struct lu_buf *buf, loff_t *pos,
1555                         struct lustre_capa *capa)
1556 {
1557         struct inode *inode = osd_dt_obj(dt)->oo_inode;
1558
1559         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
1560                 RETURN(-EACCES);
1561
1562         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1563 }
1564
1565 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1566                          const struct lu_buf *buf, loff_t *pos,
1567                          struct thandle *handle, struct lustre_capa *capa)
1568 {
1569         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
1570         struct osd_thandle *oh;
1571         ssize_t             result;
1572
1573         LASSERT(handle != NULL);
1574
1575         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1576                 RETURN(-EACCES);
1577
1578         oh = container_of(handle, struct osd_thandle, ot_super);
1579         LASSERT(oh->ot_handle->h_transaction != NULL);
1580         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
1581                                              pos, oh->ot_handle);
1582         if (result == 0)
1583                 result = buf->lb_len;
1584         return result;
1585 }
1586
1587 static const struct dt_body_operations osd_body_ops = {
1588         .dbo_read  = osd_read,
1589         .dbo_write = osd_write
1590 };
1591
1592 /*
1593  * Index operations.
1594  */
1595
1596 static int osd_object_is_root(const struct osd_object *obj)
1597 {
1598         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
1599 }
1600
1601 static int osd_index_probe(const struct lu_env *env, struct osd_object *o,
1602                            const struct dt_index_features *feat)
1603 {
1604         struct iam_descr *descr;
1605
1606         if (osd_object_is_root(o))
1607                 return feat == &dt_directory_features;
1608
1609         LASSERT(o->oo_dir != NULL);
1610
1611         descr = o->oo_dir->od_container.ic_descr;
1612         if (feat == &dt_directory_features)
1613                 return descr == &iam_htree_compat_param ||
1614                         (descr->id_rec_size == sizeof(struct lu_fid_pack) &&
1615                          1 /*
1616                             * XXX check that index looks like directory.
1617                             */
1618                                 );
1619         else
1620                 return
1621                         feat->dif_keysize_min <= descr->id_key_size &&
1622                         descr->id_key_size <= feat->dif_keysize_max &&
1623                         feat->dif_recsize_min <= descr->id_rec_size &&
1624                         descr->id_rec_size <= feat->dif_recsize_max &&
1625                         !(feat->dif_flags & (DT_IND_VARKEY |
1626                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
1627                         ergo(feat->dif_flags & DT_IND_UPDATE,
1628                              1 /* XXX check that object (and file system) is
1629                                 * writable */);
1630 }
1631
1632 static int osd_container_init(const struct lu_env *env,
1633                               struct osd_object *obj,
1634                               struct osd_directory *dir)
1635 {
1636         int result;
1637         struct iam_container *bag;
1638
1639         bag    = &dir->od_container;
1640         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
1641         if (result == 0) {
1642                 result = iam_container_setup(bag);
1643                 if (result == 0)
1644                         obj->oo_dt.do_index_ops = &osd_index_ops;
1645                 else
1646                         iam_container_fini(bag);
1647         }
1648         return result;
1649 }
1650
1651 /*
1652  * Concurrency: no external locking is necessary.
1653  */
1654 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1655                          const struct dt_index_features *feat)
1656 {
1657         int result;
1658         struct osd_object *obj = osd_dt_obj(dt);
1659
1660         LINVRNT(osd_invariant(obj));
1661         LASSERT(dt_object_exists(dt));
1662
1663         if (osd_object_is_root(obj)) {
1664                 dt->do_index_ops = &osd_index_compat_ops;
1665                 result = 0;
1666         } else if (!osd_has_index(obj)) {
1667                 struct osd_directory *dir;
1668
1669                 OBD_ALLOC_PTR(dir);
1670                 if (dir != NULL) {
1671                         sema_init(&dir->od_sem, 1);
1672
1673                         spin_lock(&obj->oo_guard);
1674                         if (obj->oo_dir == NULL)
1675                                 obj->oo_dir = dir;
1676                         else
1677                                 /*
1678                                  * Concurrent thread allocated container data.
1679                                  */
1680                                 OBD_FREE_PTR(dir);
1681                         spin_unlock(&obj->oo_guard);
1682                         /*
1683                          * Now, that we have container data, serialize its
1684                          * initialization.
1685                          */
1686                         down(&obj->oo_dir->od_sem);
1687                         /*
1688                          * recheck under lock.
1689                          */
1690                         if (!osd_has_index(obj))
1691                                 result = osd_container_init(env, obj, dir);
1692                         else
1693                                 result = 0;
1694                         up(&obj->oo_dir->od_sem);
1695                 } else
1696                         result = -ENOMEM;
1697         } else
1698                 result = 0;
1699
1700         if (result == 0) {
1701                 if (!osd_index_probe(env, obj, feat))
1702                         result = -ENOTDIR;
1703         }
1704         LINVRNT(osd_invariant(obj));
1705
1706         return result;
1707 }
1708
1709 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1710                             const struct dt_key *key, struct thandle *handle,
1711                             struct lustre_capa *capa)
1712 {
1713         struct osd_object     *obj = osd_dt_obj(dt);
1714         struct osd_thandle    *oh;
1715         struct iam_path_descr *ipd;
1716         struct iam_container  *bag = &obj->oo_dir->od_container;
1717         int rc;
1718
1719         ENTRY;
1720
1721         LINVRNT(osd_invariant(obj));
1722         LASSERT(dt_object_exists(dt));
1723         LASSERT(bag->ic_object == obj->oo_inode);
1724         LASSERT(handle != NULL);
1725
1726         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
1727                 RETURN(-EACCES);
1728
1729         ipd = osd_ipd_get(env, bag);
1730         if (unlikely(ipd == NULL))
1731                 RETURN(-ENOMEM);
1732
1733         oh = container_of0(handle, struct osd_thandle, ot_super);
1734         LASSERT(oh->ot_handle != NULL);
1735         LASSERT(oh->ot_handle->h_transaction != NULL);
1736
1737         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
1738         osd_ipd_put(env, bag, ipd);
1739         LINVRNT(osd_invariant(obj));
1740         RETURN(rc);
1741 }
1742
1743 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1744                             struct dt_rec *rec, const struct dt_key *key,
1745                             struct lustre_capa *capa)
1746 {
1747         struct osd_object     *obj = osd_dt_obj(dt);
1748         struct iam_path_descr *ipd;
1749         struct iam_container  *bag = &obj->oo_dir->od_container;
1750         int rc;
1751
1752         ENTRY;
1753
1754         LINVRNT(osd_invariant(obj));
1755         LASSERT(dt_object_exists(dt));
1756         LASSERT(bag->ic_object == obj->oo_inode);
1757
1758         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
1759                 return -EACCES;
1760
1761         ipd = osd_ipd_get(env, bag);
1762         if (unlikely(ipd == NULL))
1763                 RETURN(-ENOMEM);
1764
1765         rc = iam_lookup(bag, (const struct iam_key *)key,
1766                         (struct iam_rec *)rec, ipd);
1767         osd_ipd_put(env, bag, ipd);
1768         LINVRNT(osd_invariant(obj));
1769
1770         RETURN(rc);
1771 }
1772
1773 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1774                             const struct dt_rec *rec, const struct dt_key *key,
1775                             struct thandle *th, struct lustre_capa *capa)
1776 {
1777         struct osd_object     *obj = osd_dt_obj(dt);
1778         struct iam_path_descr *ipd;
1779         struct osd_thandle    *oh;
1780         struct iam_container  *bag = &obj->oo_dir->od_container;
1781         int rc;
1782
1783         ENTRY;
1784
1785         LINVRNT(osd_invariant(obj));
1786         LASSERT(dt_object_exists(dt));
1787         LASSERT(bag->ic_object == obj->oo_inode);
1788         LASSERT(th != NULL);
1789
1790         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
1791                 return -EACCES;
1792
1793         ipd = osd_ipd_get(env, bag);
1794         if (unlikely(ipd == NULL))
1795                 RETURN(-ENOMEM);
1796
1797         oh = container_of0(th, struct osd_thandle, ot_super);
1798         LASSERT(oh->ot_handle != NULL);
1799         LASSERT(oh->ot_handle->h_transaction != NULL);
1800         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
1801                         (struct iam_rec *)rec, ipd);
1802         osd_ipd_put(env, bag, ipd);
1803         LINVRNT(osd_invariant(obj));
1804         RETURN(rc);
1805 }
1806
1807 /*
1808  * Iterator operations.
1809  */
1810 struct osd_it {
1811         struct osd_object     *oi_obj;
1812         struct iam_path_descr *oi_ipd;
1813         struct iam_iterator    oi_it;
1814 };
1815
1816 static struct dt_it *osd_it_init(const struct lu_env *env,
1817                                  struct dt_object *dt, int writable,
1818                                  struct lustre_capa *capa)
1819 {
1820         struct osd_it         *it;
1821         struct osd_object     *obj = osd_dt_obj(dt);
1822         struct lu_object      *lo  = &dt->do_lu;
1823         struct iam_path_descr *ipd;
1824         struct iam_container  *bag = &obj->oo_dir->od_container;
1825         __u32                  flags;
1826
1827         LASSERT(lu_object_exists(lo));
1828
1829         if (osd_object_auth(env, dt, capa, writable ? CAPA_OPC_BODY_WRITE :
1830                             CAPA_OPC_BODY_READ))
1831                 return ERR_PTR(-EACCES);
1832
1833         flags = writable ? IAM_IT_MOVE|IAM_IT_WRITE : IAM_IT_MOVE;
1834         OBD_ALLOC_PTR(it);
1835         if (it != NULL) {
1836                 /*
1837                  * XXX: as ipd is allocated within osd_thread_info, assignment
1838                  * below implies that iterator usage is confined within single
1839                  * environment.
1840                  */
1841                 ipd = osd_ipd_get(env, bag);
1842                 if (likely(ipd != NULL)) {
1843                         it->oi_obj = obj;
1844                         it->oi_ipd = ipd;
1845                         lu_object_get(lo);
1846                         iam_it_init(&it->oi_it, bag, flags, ipd);
1847                         return (struct dt_it *)it;
1848                 } else
1849                         OBD_FREE_PTR(it);
1850         }
1851         return ERR_PTR(-ENOMEM);
1852 }
1853
1854 static void osd_it_fini(const struct lu_env *env, struct dt_it *di)
1855 {
1856         struct osd_it     *it = (struct osd_it *)di;
1857         struct osd_object *obj = it->oi_obj;
1858
1859         iam_it_fini(&it->oi_it);
1860         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
1861         lu_object_put(env, &obj->oo_dt.do_lu);
1862         OBD_FREE_PTR(it);
1863 }
1864
1865 static int osd_it_get(const struct lu_env *env,
1866                       struct dt_it *di, const struct dt_key *key)
1867 {
1868         struct osd_it *it = (struct osd_it *)di;
1869
1870         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
1871 }
1872
1873 static void osd_it_put(const struct lu_env *env, struct dt_it *di)
1874 {
1875         struct osd_it *it = (struct osd_it *)di;
1876
1877         iam_it_put(&it->oi_it);
1878 }
1879
1880 static int osd_it_next(const struct lu_env *env, struct dt_it *di)
1881 {
1882         struct osd_it *it = (struct osd_it *)di;
1883
1884         return iam_it_next(&it->oi_it);
1885 }
1886
1887 static int osd_it_del(const struct lu_env *env, struct dt_it *di,
1888                       struct thandle *th)
1889 {
1890         struct osd_it      *it = (struct osd_it *)di;
1891         struct osd_thandle *oh;
1892
1893         LASSERT(th != NULL);
1894
1895         oh = container_of0(th, struct osd_thandle, ot_super);
1896         LASSERT(oh->ot_handle != NULL);
1897         LASSERT(oh->ot_handle->h_transaction != NULL);
1898
1899         return iam_it_rec_delete(oh->ot_handle, &it->oi_it);
1900 }
1901
1902 static struct dt_key *osd_it_key(const struct lu_env *env,
1903                                  const struct dt_it *di)
1904 {
1905         struct osd_it *it = (struct osd_it *)di;
1906
1907         return (struct dt_key *)iam_it_key_get(&it->oi_it);
1908 }
1909
1910 static int osd_it_key_size(const struct lu_env *env, const struct dt_it *di)
1911 {
1912         struct osd_it *it = (struct osd_it *)di;
1913
1914         return iam_it_key_size(&it->oi_it);
1915 }
1916
1917 static struct dt_rec *osd_it_rec(const struct lu_env *env,
1918                                  const struct dt_it *di)
1919 {
1920         struct osd_it *it = (struct osd_it *)di;
1921
1922         return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
1923 }
1924
1925 static __u64 osd_it_store(const struct lu_env *env, const struct dt_it *di)
1926 {
1927         struct osd_it *it = (struct osd_it *)di;
1928
1929         return iam_it_store(&it->oi_it);
1930 }
1931
1932 static int osd_it_load(const struct lu_env *env,
1933                        const struct dt_it *di, __u64 hash)
1934 {
1935         struct osd_it *it = (struct osd_it *)di;
1936
1937         return iam_it_load(&it->oi_it, hash);
1938 }
1939
1940 static const struct dt_index_operations osd_index_ops = {
1941         .dio_lookup = osd_index_lookup,
1942         .dio_insert = osd_index_insert,
1943         .dio_delete = osd_index_delete,
1944         .dio_it     = {
1945                 .init     = osd_it_init,
1946                 .fini     = osd_it_fini,
1947                 .get      = osd_it_get,
1948                 .put      = osd_it_put,
1949                 .del      = osd_it_del,
1950                 .next     = osd_it_next,
1951                 .key      = osd_it_key,
1952                 .key_size = osd_it_key_size,
1953                 .rec      = osd_it_rec,
1954                 .store    = osd_it_store,
1955                 .load     = osd_it_load
1956         }
1957 };
1958
1959 static int osd_index_compat_delete(const struct lu_env *env,
1960                                    struct dt_object *dt,
1961                                    const struct dt_key *key,
1962                                    struct thandle *handle,
1963                                    struct lustre_capa *capa)
1964 {
1965         struct osd_object *obj = osd_dt_obj(dt);
1966
1967         LASSERT(handle != NULL);
1968         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
1969         ENTRY;
1970
1971 #if 0
1972         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
1973                 RETURN(-EACCES);
1974 #endif
1975
1976         RETURN(-EOPNOTSUPP);
1977 }
1978
1979 /*
1980  * Compatibility index operations.
1981  */
1982
1983
1984 static void osd_build_pack(const struct lu_env *env, struct osd_device *osd,
1985                            struct dentry *dentry, struct lu_fid_pack *pack)
1986 {
1987         struct inode  *inode = dentry->d_inode;
1988         struct lu_fid *fid   = &osd_oti_get(env)->oti_fid;
1989
1990         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1991         fid_cpu_to_be(fid, fid);
1992         pack->fp_len = sizeof *fid + 1;
1993         memcpy(pack->fp_area, fid, sizeof *fid);
1994 }
1995
1996 static int osd_index_compat_lookup(const struct lu_env *env,
1997                                    struct dt_object *dt,
1998                                    struct dt_rec *rec, const struct dt_key *key,
1999                                    struct lustre_capa *capa)
2000 {
2001         struct osd_object *obj = osd_dt_obj(dt);
2002
2003         struct osd_device      *osd  = osd_obj2dev(obj);
2004         struct osd_thread_info *info = osd_oti_get(env);
2005         struct inode           *dir;
2006
2007         int result;
2008
2009         /*
2010          * XXX temporary solution.
2011          */
2012         struct dentry *dentry;
2013         struct dentry *parent;
2014
2015         LINVRNT(osd_invariant(obj));
2016         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2017         LASSERT(osd_has_index(obj));
2018
2019         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2020                 return -EACCES;
2021
2022         info->oti_str.name = (const char *)key;
2023         info->oti_str.len  = strlen((const char *)key);
2024
2025         dir = obj->oo_inode;
2026         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2027
2028         parent = d_alloc_root(dir);
2029         if (parent == NULL)
2030                 return -ENOMEM;
2031         igrab(dir);
2032         dentry = d_alloc(parent, &info->oti_str);
2033         if (dentry != NULL) {
2034                 struct dentry *d;
2035
2036                 /*
2037                  * XXX passing NULL for nameidata should work for
2038                  * ext3/ldiskfs.
2039                  */
2040                 d = dir->i_op->lookup(dir, dentry, NULL);
2041                 if (d == NULL) {
2042                         /*
2043                          * normal case, result is in @dentry.
2044                          */
2045                         if (dentry->d_inode != NULL) {
2046                                 osd_build_pack(env, osd, dentry,
2047                                                (struct lu_fid_pack *)rec);
2048                                 result = 0;
2049                         } else
2050                                 result = -ENOENT;
2051                  } else {
2052                         /* What? Disconnected alias? Ppheeeww... */
2053                         CERROR("Aliasing where not expected\n");
2054                         result = -EIO;
2055                         dput(d);
2056                 }
2057                 dput(dentry);
2058         } else
2059                 result = -ENOMEM;
2060         dput(parent);
2061         LINVRNT(osd_invariant(obj));
2062         return result;
2063 }
2064
2065 static int osd_add_rec(struct osd_thread_info *info, struct osd_device *dev,
2066                        struct inode *dir, struct inode *inode, const char *name)
2067 {
2068         struct dentry *old;
2069         struct dentry *new;
2070         struct dentry *parent;
2071
2072         int result;
2073
2074         info->oti_str.name = name;
2075         info->oti_str.len  = strlen(name);
2076
2077         LASSERT(atomic_read(&dir->i_count) > 0);
2078         result = -ENOMEM;
2079         old = d_alloc(dev->od_obj_area, &info->oti_str);
2080         if (old != NULL) {
2081                 d_instantiate(old, inode);
2082                 igrab(inode);
2083                 LASSERT(atomic_read(&dir->i_count) > 0);
2084                 parent = d_alloc_root(dir);
2085                 if (parent != NULL) {
2086                         igrab(dir);
2087                         LASSERT(atomic_read(&dir->i_count) > 1);
2088                         new = d_alloc(parent, &info->oti_str);
2089                         LASSERT(atomic_read(&dir->i_count) > 1);
2090                         if (new != NULL) {
2091                                 LASSERT(atomic_read(&dir->i_count) > 1);
2092                                 result = dir->i_op->link(old, dir, new);
2093                                 LASSERT(atomic_read(&dir->i_count) > 1);
2094                                 dput(new);
2095                                 LASSERT(atomic_read(&dir->i_count) > 1);
2096                         }
2097                         LASSERT(atomic_read(&dir->i_count) > 1);
2098                         dput(parent);
2099                         LASSERT(atomic_read(&dir->i_count) > 0);
2100                 }
2101                 dput(old);
2102         }
2103         LASSERT(atomic_read(&dir->i_count) > 0);
2104         return result;
2105 }
2106
2107
2108 /*
2109  * XXX Temporary stuff.
2110  */
2111 static int osd_index_compat_insert(const struct lu_env *env,
2112                                    struct dt_object *dt,
2113                                    const struct dt_rec *rec,
2114                                    const struct dt_key *key, struct thandle *th,
2115                                    struct lustre_capa *capa)
2116 {
2117         struct osd_object     *obj = osd_dt_obj(dt);
2118
2119         const char          *name = (const char *)key;
2120
2121         struct lu_device    *ludev = dt->do_lu.lo_dev;
2122         struct lu_object    *luch;
2123
2124         struct osd_thread_info   *info = osd_oti_get(env);
2125         const struct lu_fid_pack *pack  = (const struct lu_fid_pack *)rec;
2126         struct lu_fid            *fid   = &osd_oti_get(env)->oti_fid;
2127
2128         int result;
2129
2130         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2131         LINVRNT(osd_invariant(obj));
2132         LASSERT(th != NULL);
2133
2134         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2135                 return -EACCES;
2136
2137         result = fid_unpack(pack, fid);
2138         if (result != 0)
2139                 return result;
2140
2141         luch = lu_object_find(env, ludev, fid, NULL);
2142         if (!IS_ERR(luch)) {
2143                 if (lu_object_exists(luch)) {
2144                         struct osd_object *child;
2145
2146                         child = osd_obj(lu_object_locate(luch->lo_header,
2147                                                          ludev->ld_type));
2148                         if (child != NULL)
2149                                 result = osd_add_rec(info, osd_obj2dev(obj),
2150                                                      obj->oo_inode,
2151                                                      child->oo_inode, name);
2152                         else {
2153                                 CERROR("No osd slice.\n");
2154                                 result = -ENOENT;
2155                         }
2156                         LINVRNT(osd_invariant(obj));
2157                         LINVRNT(osd_invariant(child));
2158                 } else {
2159                         CERROR("Sorry.\n");
2160                         result = -ENOENT;
2161                 }
2162                 lu_object_put(env, luch);
2163         } else
2164                 result = PTR_ERR(luch);
2165         LINVRNT(osd_invariant(obj));
2166         return result;
2167 }
2168
2169 static const struct dt_index_operations osd_index_compat_ops = {
2170         .dio_lookup = osd_index_compat_lookup,
2171         .dio_insert = osd_index_compat_insert,
2172         .dio_delete = osd_index_compat_delete
2173 };
2174
2175 /* type constructor/destructor: osd_type_init, osd_type_fini */
2176 LU_TYPE_INIT_FINI(osd, &osd_key);
2177
2178 static struct lu_context_key osd_key = {
2179         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
2180         .lct_init = osd_key_init,
2181         .lct_fini = osd_key_fini,
2182         .lct_exit = osd_key_exit
2183 };
2184
2185 static void *osd_key_init(const struct lu_context *ctx,
2186                           struct lu_context_key *key)
2187 {
2188         struct osd_thread_info *info;
2189
2190         OBD_ALLOC_PTR(info);
2191         if (info != NULL)
2192                 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
2193         else
2194                 info = ERR_PTR(-ENOMEM);
2195         return info;
2196 }
2197
2198 /* context key destructor: osd_key_fini */
2199 LU_KEY_FINI(osd, struct osd_thread_info);
2200
2201 static void osd_key_exit(const struct lu_context *ctx,
2202                          struct lu_context_key *key, void *data)
2203 {
2204         struct osd_thread_info *info = data;
2205
2206         LASSERT(info->oti_r_locks == 0);
2207         LASSERT(info->oti_w_locks == 0);
2208         LASSERT(info->oti_txns    == 0);
2209 }
2210
2211 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
2212                            const char *name, struct lu_device *next)
2213 {
2214         int rc;
2215         /* context for commit hooks */
2216         rc = lu_context_init(&osd_dev(d)->od_env_for_commit.le_ctx,
2217                              LCT_MD_THREAD);
2218         if (rc == 0)
2219                 rc = osd_procfs_init(osd_dev(d), name);
2220         return rc;
2221 }
2222
2223 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
2224 {
2225         struct osd_thread_info *info = osd_oti_get(env);
2226         ENTRY;
2227         if (o->od_obj_area != NULL) {
2228                 dput(o->od_obj_area);
2229                 o->od_obj_area = NULL;
2230         }
2231         osd_oi_fini(info, &o->od_oi);
2232
2233         RETURN(0);
2234 }
2235
2236 static int osd_mount(const struct lu_env *env,
2237                      struct osd_device *o, struct lustre_cfg *cfg)
2238 {
2239         struct lustre_mount_info *lmi;
2240         const char               *dev  = lustre_cfg_string(cfg, 0);
2241         struct osd_thread_info   *info = osd_oti_get(env);
2242         int result;
2243
2244         ENTRY;
2245
2246         if (o->od_mount != NULL) {
2247                 CERROR("Already mounted (%s)\n", dev);
2248                 RETURN(-EEXIST);
2249         }
2250
2251         /* get mount */
2252         lmi = server_get_mount(dev);
2253         if (lmi == NULL) {
2254                 CERROR("Cannot get mount info for %s!\n", dev);
2255                 RETURN(-EFAULT);
2256         }
2257
2258         LASSERT(lmi != NULL);
2259         /* save lustre_mount_info in dt_device */
2260         o->od_mount = lmi;
2261
2262         result = osd_oi_init(info, &o->od_oi, &o->od_dt_dev);
2263         if (result == 0) {
2264                 struct dentry *d;
2265
2266                 d = simple_mkdir(osd_sb(o)->s_root, lmi->lmi_mnt, "*OBJ-TEMP*",
2267                                  0777, 1);
2268                 if (!IS_ERR(d)) {
2269                         o->od_obj_area = d;
2270                 } else
2271                         result = PTR_ERR(d);
2272         }
2273         if (result != 0)
2274                 osd_shutdown(env, o);
2275         RETURN(result);
2276 }
2277
2278 static struct lu_device *osd_device_fini(const struct lu_env *env,
2279                                          struct lu_device *d)
2280 {
2281         int rc;
2282         ENTRY;
2283
2284         shrink_dcache_sb(osd_sb(osd_dev(d)));
2285         osd_sync(env, lu2dt_dev(d));
2286
2287         rc = osd_procfs_fini(osd_dev(d));
2288         if (rc) {
2289                 CERROR("proc fini error %d \n", rc);
2290                 RETURN (ERR_PTR(rc));
2291         }
2292
2293         if (osd_dev(d)->od_mount)
2294                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
2295                                  osd_dev(d)->od_mount->lmi_mnt);
2296         osd_dev(d)->od_mount = NULL;
2297
2298         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
2299         RETURN(NULL);
2300 }
2301
2302 static struct lu_device *osd_device_alloc(const struct lu_env *env,
2303                                           struct lu_device_type *t,
2304                                           struct lustre_cfg *cfg)
2305 {
2306         struct lu_device  *l;
2307         struct osd_device *o;
2308
2309         OBD_ALLOC_PTR(o);
2310         if (o != NULL) {
2311                 int result;
2312
2313                 result = dt_device_init(&o->od_dt_dev, t);
2314                 if (result == 0) {
2315                         l = osd2lu_dev(o);
2316                         l->ld_ops = &osd_lu_ops;
2317                         o->od_dt_dev.dd_ops = &osd_dt_ops;
2318                         spin_lock_init(&o->od_osfs_lock);
2319                         o->od_osfs_age = cfs_time_shift_64(-1000);
2320                         o->od_capa_hash = init_capa_hash();
2321                         if (o->od_capa_hash == NULL) {
2322                                 dt_device_fini(&o->od_dt_dev);
2323                                 l = ERR_PTR(-ENOMEM);
2324                         }
2325                 } else
2326                         l = ERR_PTR(result);
2327
2328                 if (IS_ERR(l))
2329                         OBD_FREE_PTR(o);
2330         } else
2331                 l = ERR_PTR(-ENOMEM);
2332         return l;
2333 }
2334
2335 static struct lu_device *osd_device_free(const struct lu_env *env,
2336                                          struct lu_device *d)
2337 {
2338         struct osd_device *o = osd_dev(d);
2339         ENTRY;
2340
2341         cleanup_capa_hash(o->od_capa_hash);
2342         dt_device_fini(&o->od_dt_dev);
2343         OBD_FREE_PTR(o);
2344         RETURN(NULL);
2345 }
2346
2347 static int osd_process_config(const struct lu_env *env,
2348                               struct lu_device *d, struct lustre_cfg *cfg)
2349 {
2350         struct osd_device *o = osd_dev(d);
2351         int err;
2352         ENTRY;
2353
2354         switch(cfg->lcfg_command) {
2355         case LCFG_SETUP:
2356                 err = osd_mount(env, o, cfg);
2357                 break;
2358         case LCFG_CLEANUP:
2359                 err = osd_shutdown(env, o);
2360                 break;
2361         default:
2362                 err = -ENOTTY;
2363         }
2364
2365         RETURN(err);
2366 }
2367 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
2368                                     struct ldiskfs_super_block * es);
2369
2370 static int osd_recovery_complete(const struct lu_env *env,
2371                                  struct lu_device *d)
2372 {
2373         struct osd_device *o = osd_dev(d);
2374         ENTRY;
2375         /* TODO: orphans handling */
2376         ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
2377         RETURN(0);
2378 }
2379
2380 static struct inode *osd_iget(struct osd_thread_info *info,
2381                               struct osd_device *dev,
2382                               const struct osd_inode_id *id)
2383 {
2384         struct inode *inode;
2385
2386         inode = iget(osd_sb(dev), id->oii_ino);
2387         if (inode == NULL) {
2388                 CERROR("no inode\n");
2389                 inode = ERR_PTR(-EACCES);
2390         } else if (is_bad_inode(inode)) {
2391                 CERROR("bad inode\n");
2392                 iput(inode);
2393                 inode = ERR_PTR(-ENOENT);
2394         } else if (inode->i_generation != id->oii_gen) {
2395                 CERROR("stale inode\n");
2396                 iput(inode);
2397                 inode = ERR_PTR(-ESTALE);
2398         }
2399
2400         return inode;
2401
2402 }
2403
2404 static int osd_fid_lookup(const struct lu_env *env,
2405                           struct osd_object *obj, const struct lu_fid *fid)
2406 {
2407         struct osd_thread_info *info;
2408         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
2409         struct osd_device      *dev;
2410         struct osd_inode_id    *id;
2411         struct osd_oi          *oi;
2412         struct inode           *inode;
2413         int                     result;
2414
2415         LINVRNT(osd_invariant(obj));
2416         LASSERT(obj->oo_inode == NULL);
2417         LASSERT(fid_is_sane(fid));
2418         /*
2419          * This assertion checks that osd layer sees only local
2420          * fids. Unfortunately it is somewhat expensive (does a
2421          * cache-lookup). Disabling it for production/acceptance-testing.
2422          */
2423         LASSERT(1 || fid_is_local(ldev->ld_site, fid));
2424
2425         ENTRY;
2426
2427         info = osd_oti_get(env);
2428         dev  = osd_dev(ldev);
2429         id   = &info->oti_id;
2430         oi   = &dev->od_oi;
2431
2432         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
2433                 RETURN(-ENOENT);
2434
2435         result = osd_oi_lookup(info, oi, fid, id);
2436         if (result == 0) {
2437                 inode = osd_iget(info, dev, id);
2438                 if (!IS_ERR(inode)) {
2439                         obj->oo_inode = inode;
2440                         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
2441                         result = 0;
2442                 } else
2443                         /*
2444                          * If fid wasn't found in oi, inode-less object is
2445                          * created, for which lu_object_exists() returns
2446                          * false. This is used in a (frequent) case when
2447                          * objects are created as locking anchors or
2448                          * place holders for objects yet to be created.
2449                          */
2450                         result = PTR_ERR(inode);
2451         } else if (result == -ENOENT)
2452                 result = 0;
2453         LINVRNT(osd_invariant(obj));
2454         RETURN(result);
2455 }
2456
2457 static void osd_inode_getattr(const struct lu_env *env,
2458                               struct inode *inode, struct lu_attr *attr)
2459 {
2460         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2461                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2462                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
2463
2464         attr->la_atime      = LTIME_S(inode->i_atime);
2465         attr->la_mtime      = LTIME_S(inode->i_mtime);
2466         attr->la_ctime      = LTIME_S(inode->i_ctime);
2467         attr->la_mode       = inode->i_mode;
2468         attr->la_size       = i_size_read(inode);
2469         attr->la_blocks     = inode->i_blocks;
2470         attr->la_uid        = inode->i_uid;
2471         attr->la_gid        = inode->i_gid;
2472         attr->la_flags      = LDISKFS_I(inode)->i_flags;
2473         attr->la_nlink      = inode->i_nlink;
2474         attr->la_rdev       = inode->i_rdev;
2475         attr->la_blksize    = ll_inode_blksize(inode);
2476         attr->la_blkbits    = inode->i_blkbits;
2477 }
2478
2479 /*
2480  * Helpers.
2481  */
2482
2483 static int lu_device_is_osd(const struct lu_device *d)
2484 {
2485         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
2486 }
2487
2488 static struct osd_object *osd_obj(const struct lu_object *o)
2489 {
2490         LASSERT(lu_device_is_osd(o->lo_dev));
2491         return container_of0(o, struct osd_object, oo_dt.do_lu);
2492 }
2493
2494 static struct osd_device *osd_dt_dev(const struct dt_device *d)
2495 {
2496         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
2497         return container_of0(d, struct osd_device, od_dt_dev);
2498 }
2499
2500 static struct osd_device *osd_dev(const struct lu_device *d)
2501 {
2502         LASSERT(lu_device_is_osd(d));
2503         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
2504 }
2505
2506 static struct osd_object *osd_dt_obj(const struct dt_object *d)
2507 {
2508         return osd_obj(&d->do_lu);
2509 }
2510
2511 static struct osd_device *osd_obj2dev(const struct osd_object *o)
2512 {
2513         return osd_dev(o->oo_dt.do_lu.lo_dev);
2514 }
2515
2516 static struct lu_device *osd2lu_dev(struct osd_device *osd)
2517 {
2518         return &osd->od_dt_dev.dd_lu_dev;
2519 }
2520
2521 static struct super_block *osd_sb(const struct osd_device *dev)
2522 {
2523         return dev->od_mount->lmi_mnt->mnt_sb;
2524 }
2525
2526 static journal_t *osd_journal(const struct osd_device *dev)
2527 {
2528         return LDISKFS_SB(osd_sb(dev))->s_journal;
2529 }
2530
2531 static int osd_has_index(const struct osd_object *obj)
2532 {
2533         return obj->oo_dt.do_index_ops != NULL;
2534 }
2535
2536 static int osd_object_invariant(const struct lu_object *l)
2537 {
2538         return osd_invariant(osd_obj(l));
2539 }
2540
2541 static const struct lu_object_operations osd_lu_obj_ops = {
2542         .loo_object_init      = osd_object_init,
2543         .loo_object_delete    = osd_object_delete,
2544         .loo_object_release   = osd_object_release,
2545         .loo_object_free      = osd_object_free,
2546         .loo_object_print     = osd_object_print,
2547         .loo_object_invariant = osd_object_invariant
2548 };
2549
2550 static const struct lu_device_operations osd_lu_ops = {
2551         .ldo_object_alloc      = osd_object_alloc,
2552         .ldo_process_config    = osd_process_config,
2553         .ldo_recovery_complete = osd_recovery_complete
2554 };
2555
2556 static const struct lu_device_type_operations osd_device_type_ops = {
2557         .ldto_init = osd_type_init,
2558         .ldto_fini = osd_type_fini,
2559
2560         .ldto_start = osd_type_start,
2561         .ldto_stop  = osd_type_stop,
2562
2563         .ldto_device_alloc = osd_device_alloc,
2564         .ldto_device_free  = osd_device_free,
2565
2566         .ldto_device_init    = osd_device_init,
2567         .ldto_device_fini    = osd_device_fini
2568 };
2569
2570 static struct lu_device_type osd_device_type = {
2571         .ldt_tags     = LU_DEVICE_DT,
2572         .ldt_name     = LUSTRE_OSD_NAME,
2573         .ldt_ops      = &osd_device_type_ops,
2574         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
2575 };
2576
2577 /*
2578  * lprocfs legacy support.
2579  */
2580 static struct obd_ops osd_obd_device_ops = {
2581         .o_owner = THIS_MODULE
2582 };
2583
2584 static int __init osd_mod_init(void)
2585 {
2586         struct lprocfs_static_vars lvars;
2587
2588         lprocfs_osd_init_vars(&lvars);
2589         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
2590                                    LUSTRE_OSD_NAME, &osd_device_type);
2591 }
2592
2593 static void __exit osd_mod_exit(void)
2594 {
2595         class_unregister_type(LUSTRE_OSD_NAME);
2596 }
2597
2598 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2599 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
2600 MODULE_LICENSE("GPL");
2601
2602 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);