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