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