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