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