Whamcloud - gitweb
LU-365 Update copyright for files modified by Whamcloud
[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         int result;
2304         struct iam_container *bag;
2305
2306         bag    = &dir->od_container;
2307         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2308         if (result == 0) {
2309                 result = iam_container_setup(bag);
2310                 if (result == 0)
2311                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2312                 else
2313                         iam_container_fini(bag);
2314         }
2315         return result;
2316 }
2317
2318
2319 /*
2320  * Concurrency: no external locking is necessary.
2321  */
2322 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2323                          const struct dt_index_features *feat)
2324 {
2325         int result;
2326         int ea_dir = 0;
2327         struct osd_object *obj = osd_dt_obj(dt);
2328         struct osd_device *osd = osd_obj2dev(obj);
2329
2330         LINVRNT(osd_invariant(obj));
2331         LASSERT(dt_object_exists(dt));
2332
2333         if (osd_object_is_root(obj)) {
2334                 dt->do_index_ops = &osd_index_ea_ops;
2335                 result = 0;
2336         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2337                 dt->do_index_ops = &osd_index_ea_ops;
2338                 if (S_ISDIR(obj->oo_inode->i_mode))
2339                         result = 0;
2340                 else
2341                         result = -ENOTDIR;
2342                 ea_dir = 1;
2343         } else if (!osd_has_index(obj)) {
2344                 struct osd_directory *dir;
2345
2346                 OBD_ALLOC_PTR(dir);
2347                 if (dir != NULL) {
2348
2349                         cfs_spin_lock(&obj->oo_guard);
2350                         if (obj->oo_dir == NULL)
2351                                 obj->oo_dir = dir;
2352                         else
2353                                 /*
2354                                  * Concurrent thread allocated container data.
2355                                  */
2356                                 OBD_FREE_PTR(dir);
2357                         cfs_spin_unlock(&obj->oo_guard);
2358                         /*
2359                          * Now, that we have container data, serialize its
2360                          * initialization.
2361                          */
2362                         cfs_down_write(&obj->oo_ext_idx_sem);
2363                         /*
2364                          * recheck under lock.
2365                          */
2366                         if (!osd_has_index(obj))
2367                                 result = osd_iam_container_init(env, obj, dir);
2368                         else
2369                                 result = 0;
2370                         cfs_up_write(&obj->oo_ext_idx_sem);
2371                 } else
2372                         result = -ENOMEM;
2373         } else
2374                 result = 0;
2375
2376         if (result == 0 && ea_dir == 0) {
2377                 if (!osd_iam_index_probe(env, obj, feat))
2378                         result = -ENOTDIR;
2379         }
2380         LINVRNT(osd_invariant(obj));
2381
2382         return result;
2383 }
2384
2385 static const struct dt_object_operations osd_obj_ops = {
2386         .do_read_lock    = osd_object_read_lock,
2387         .do_write_lock   = osd_object_write_lock,
2388         .do_read_unlock  = osd_object_read_unlock,
2389         .do_write_unlock = osd_object_write_unlock,
2390         .do_write_locked = osd_object_write_locked,
2391         .do_attr_get     = osd_attr_get,
2392         .do_attr_set     = osd_attr_set,
2393         .do_ah_init      = osd_ah_init,
2394         .do_create       = osd_object_create,
2395         .do_index_try    = osd_index_try,
2396         .do_ref_add      = osd_object_ref_add,
2397         .do_ref_del      = osd_object_ref_del,
2398         .do_xattr_get    = osd_xattr_get,
2399         .do_xattr_set    = osd_xattr_set,
2400         .do_xattr_del    = osd_xattr_del,
2401         .do_xattr_list   = osd_xattr_list,
2402         .do_capa_get     = osd_capa_get,
2403         .do_object_sync  = osd_object_sync,
2404         .do_version_get  = osd_object_version_get,
2405         .do_version_set  = osd_object_version_set,
2406         .do_data_get     = osd_data_get,
2407 };
2408
2409 /**
2410  * dt_object_operations for interoperability mode
2411  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2412  */
2413 static const struct dt_object_operations osd_obj_ea_ops = {
2414         .do_read_lock    = osd_object_read_lock,
2415         .do_write_lock   = osd_object_write_lock,
2416         .do_read_unlock  = osd_object_read_unlock,
2417         .do_write_unlock = osd_object_write_unlock,
2418         .do_write_locked = osd_object_write_locked,
2419         .do_attr_get     = osd_attr_get,
2420         .do_attr_set     = osd_attr_set,
2421         .do_ah_init      = osd_ah_init,
2422         .do_create       = osd_object_ea_create,
2423         .do_index_try    = osd_index_try,
2424         .do_ref_add      = osd_object_ref_add,
2425         .do_ref_del      = osd_object_ref_del,
2426         .do_xattr_get    = osd_xattr_get,
2427         .do_xattr_set    = osd_xattr_set,
2428         .do_xattr_del    = osd_xattr_del,
2429         .do_xattr_list   = osd_xattr_list,
2430         .do_capa_get     = osd_capa_get,
2431         .do_object_sync  = osd_object_sync,
2432         .do_version_get  = osd_object_version_get,
2433         .do_version_set  = osd_object_version_set,
2434         .do_data_get     = osd_data_get,
2435 };
2436
2437 /*
2438  * Body operations.
2439  */
2440
2441 /*
2442  * XXX: Another layering violation for now.
2443  *
2444  * We don't want to use ->f_op->read methods, because generic file write
2445  *
2446  *         - serializes on ->i_sem, and
2447  *
2448  *         - does a lot of extra work like balance_dirty_pages(),
2449  *
2450  * which doesn't work for globally shared files like /last-received.
2451  */
2452 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2453 {
2454         struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2455
2456         memcpy(buffer, (char*)ei->i_data, buflen);
2457
2458         return  buflen;
2459 }
2460
2461 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2462                             loff_t *offs)
2463 {
2464         struct buffer_head *bh;
2465         unsigned long block;
2466         int osize = size;
2467         int blocksize;
2468         int csize;
2469         int boffs;
2470         int err;
2471
2472         /* prevent reading after eof */
2473         spin_lock(&inode->i_lock);
2474         if (i_size_read(inode) < *offs + size) {
2475                 size = i_size_read(inode) - *offs;
2476                 spin_unlock(&inode->i_lock);
2477                 if (size < 0) {
2478                         CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2479                                i_size_read(inode), *offs);
2480                         return -EBADR;
2481                 } else if (size == 0) {
2482                         return 0;
2483                 }
2484         } else {
2485                 spin_unlock(&inode->i_lock);
2486         }
2487
2488         blocksize = 1 << inode->i_blkbits;
2489
2490         while (size > 0) {
2491                 block = *offs >> inode->i_blkbits;
2492                 boffs = *offs & (blocksize - 1);
2493                 csize = min(blocksize - boffs, size);
2494                 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2495                 if (!bh) {
2496                         CERROR("can't read block: %d\n", err);
2497                         return err;
2498                 }
2499
2500                 memcpy(buf, bh->b_data + boffs, csize);
2501                 brelse(bh);
2502
2503                 *offs += csize;
2504                 buf += csize;
2505                 size -= csize;
2506         }
2507         return osize;
2508 }
2509
2510 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2511                         struct lu_buf *buf, loff_t *pos,
2512                         struct lustre_capa *capa)
2513 {
2514         struct osd_object      *obj    = osd_dt_obj(dt);
2515         struct inode           *inode  = obj->oo_inode;
2516         int rc;
2517
2518         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2519                 RETURN(-EACCES);
2520
2521         /* Read small symlink from inode body as we need to maintain correct
2522          * on-disk symlinks for ldiskfs.
2523          */
2524         if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2525             (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2526                 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2527         else
2528                 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2529
2530         return rc;
2531 }
2532
2533 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2534 {
2535
2536         memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2537                buflen);
2538         LDISKFS_I(inode)->i_disksize = buflen;
2539         i_size_write(inode, buflen);
2540         inode->i_sb->s_op->dirty_inode(inode);
2541
2542         return 0;
2543 }
2544
2545 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2546                                     loff_t *offs, handle_t *handle)
2547 {
2548         struct buffer_head *bh = NULL;
2549         loff_t offset = *offs;
2550         loff_t new_size = i_size_read(inode);
2551         unsigned long block;
2552         int blocksize = 1 << inode->i_blkbits;
2553         int err = 0;
2554         int size;
2555         int boffs;
2556         int dirty_inode = 0;
2557
2558         while (bufsize > 0) {
2559                 if (bh != NULL)
2560                         brelse(bh);
2561
2562                 block = offset >> inode->i_blkbits;
2563                 boffs = offset & (blocksize - 1);
2564                 size = min(blocksize - boffs, bufsize);
2565                 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2566                 if (!bh) {
2567                         CERROR("can't read/create block: %d\n", err);
2568                         break;
2569                 }
2570
2571                 err = ldiskfs_journal_get_write_access(handle, bh);
2572                 if (err) {
2573                         CERROR("journal_get_write_access() returned error %d\n",
2574                                err);
2575                         break;
2576                 }
2577                 LASSERTF(boffs + size <= bh->b_size,
2578                          "boffs %d size %d bh->b_size %lu",
2579                          boffs, size, (unsigned long)bh->b_size);
2580                 memcpy(bh->b_data + boffs, buf, size);
2581                 err = ldiskfs_journal_dirty_metadata(handle, bh);
2582                 if (err)
2583                         break;
2584
2585                 if (offset + size > new_size)
2586                         new_size = offset + size;
2587                 offset += size;
2588                 bufsize -= size;
2589                 buf += size;
2590         }
2591         if (bh)
2592                 brelse(bh);
2593
2594         /* correct in-core and on-disk sizes */
2595         if (new_size > i_size_read(inode)) {
2596                 spin_lock(&inode->i_lock);
2597                 if (new_size > i_size_read(inode))
2598                         i_size_write(inode, new_size);
2599                 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2600                         LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2601                         dirty_inode = 1;
2602                 }
2603                 spin_unlock(&inode->i_lock);
2604                 if (dirty_inode)
2605                         inode->i_sb->s_op->dirty_inode(inode);
2606         }
2607
2608         if (err == 0)
2609                 *offs = offset;
2610         return err;
2611 }
2612
2613 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2614                          const struct lu_buf *buf, loff_t *pos,
2615                          struct thandle *handle, struct lustre_capa *capa,
2616                          int ignore_quota)
2617 {
2618         struct osd_object  *obj   = osd_dt_obj(dt);
2619         struct inode       *inode = obj->oo_inode;
2620         struct osd_thandle *oh;
2621         ssize_t            result = 0;
2622 #ifdef HAVE_QUOTA_SUPPORT
2623         cfs_cap_t           save = cfs_curproc_cap_pack();
2624 #endif
2625
2626         LASSERT(handle != NULL);
2627
2628         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2629                 RETURN(-EACCES);
2630
2631         oh = container_of(handle, struct osd_thandle, ot_super);
2632         LASSERT(oh->ot_handle->h_transaction != NULL);
2633 #ifdef HAVE_QUOTA_SUPPORT
2634         if (ignore_quota)
2635                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2636         else
2637                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2638 #endif
2639         /* Write small symlink to inode body as we need to maintain correct
2640          * on-disk symlinks for ldiskfs.
2641          */
2642         if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2643            (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2644                 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2645         else
2646                 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2647                                                   buf->lb_len, pos,
2648                                                   oh->ot_handle);
2649 #ifdef HAVE_QUOTA_SUPPORT
2650         cfs_curproc_cap_unpack(save);
2651 #endif
2652         if (result == 0)
2653                 result = buf->lb_len;
2654         return result;
2655 }
2656
2657 static const struct dt_body_operations osd_body_ops = {
2658         .dbo_read  = osd_read,
2659         .dbo_write = osd_write
2660 };
2661
2662
2663 /**
2664  *      delete a (key, value) pair from index \a dt specified by \a key
2665  *
2666  *      \param  dt      osd index object
2667  *      \param  key     key for index
2668  *      \param  rec     record reference
2669  *      \param  handle  transaction handler
2670  *
2671  *      \retval  0  success
2672  *      \retval -ve   failure
2673  */
2674
2675 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2676                                 const struct dt_key *key, struct thandle *handle,
2677                                 struct lustre_capa *capa)
2678 {
2679         struct osd_object     *obj = osd_dt_obj(dt);
2680         struct osd_thandle    *oh;
2681         struct iam_path_descr *ipd;
2682         struct iam_container  *bag = &obj->oo_dir->od_container;
2683         int rc;
2684
2685         ENTRY;
2686
2687         LINVRNT(osd_invariant(obj));
2688         LASSERT(dt_object_exists(dt));
2689         LASSERT(bag->ic_object == obj->oo_inode);
2690         LASSERT(handle != NULL);
2691
2692         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2693                 RETURN(-EACCES);
2694
2695         ipd = osd_idx_ipd_get(env, bag);
2696         if (unlikely(ipd == NULL))
2697                 RETURN(-ENOMEM);
2698
2699         oh = container_of0(handle, struct osd_thandle, ot_super);
2700         LASSERT(oh->ot_handle != NULL);
2701         LASSERT(oh->ot_handle->h_transaction != NULL);
2702
2703         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2704         osd_ipd_put(env, bag, ipd);
2705         LINVRNT(osd_invariant(obj));
2706         RETURN(rc);
2707 }
2708
2709 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2710                                           struct dt_rec *fid)
2711 {
2712         struct osd_fid_pack *rec;
2713         int rc = -ENODATA;
2714
2715         if (de->file_type & LDISKFS_DIRENT_LUFID) {
2716                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2717                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2718         }
2719         RETURN(rc);
2720 }
2721
2722 /**
2723  * Index delete function for interoperability mode (b11826).
2724  * It will remove the directory entry added by osd_index_ea_insert().
2725  * This entry is needed to maintain name->fid mapping.
2726  *
2727  * \param key,  key i.e. file entry to be deleted
2728  *
2729  * \retval   0, on success
2730  * \retval -ve, on error
2731  */
2732 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2733                                const struct dt_key *key, struct thandle *handle,
2734                                struct lustre_capa *capa)
2735 {
2736         struct osd_object          *obj    = osd_dt_obj(dt);
2737         struct inode               *dir    = obj->oo_inode;
2738         struct dentry              *dentry;
2739         struct osd_thandle         *oh;
2740         struct ldiskfs_dir_entry_2 *de;
2741         struct buffer_head         *bh;
2742
2743         int rc;
2744
2745         ENTRY;
2746
2747         LINVRNT(osd_invariant(obj));
2748         LASSERT(dt_object_exists(dt));
2749         LASSERT(handle != NULL);
2750
2751         oh = container_of(handle, struct osd_thandle, ot_super);
2752         LASSERT(oh->ot_handle != NULL);
2753         LASSERT(oh->ot_handle->h_transaction != NULL);
2754
2755         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2756                 RETURN(-EACCES);
2757
2758         dentry = osd_child_dentry_get(env, obj,
2759                                       (char *)key, strlen((char *)key));
2760
2761         cfs_down_write(&obj->oo_ext_idx_sem);
2762         bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2763         if (bh) {
2764                 rc = ldiskfs_delete_entry(oh->ot_handle,
2765                                 dir, de, bh);
2766                 brelse(bh);
2767         } else
2768                 rc = -ENOENT;
2769
2770         cfs_up_write(&obj->oo_ext_idx_sem);
2771         LASSERT(osd_invariant(obj));
2772         RETURN(rc);
2773 }
2774
2775 /**
2776  *      Lookup index for \a key and copy record to \a rec.
2777  *
2778  *      \param  dt      osd index object
2779  *      \param  key     key for index
2780  *      \param  rec     record reference
2781  *
2782  *      \retval  +ve  success : exact mach
2783  *      \retval  0    return record with key not greater than \a key
2784  *      \retval -ve   failure
2785  */
2786 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2787                                 struct dt_rec *rec, const struct dt_key *key,
2788                                 struct lustre_capa *capa)
2789 {
2790         struct osd_object     *obj = osd_dt_obj(dt);
2791         struct iam_path_descr *ipd;
2792         struct iam_container  *bag = &obj->oo_dir->od_container;
2793         struct osd_thread_info *oti = osd_oti_get(env);
2794         struct iam_iterator    *it = &oti->oti_idx_it;
2795         struct iam_rec *iam_rec;
2796         int rc;
2797         ENTRY;
2798
2799         LASSERT(osd_invariant(obj));
2800         LASSERT(dt_object_exists(dt));
2801         LASSERT(bag->ic_object == obj->oo_inode);
2802
2803         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2804                 RETURN(-EACCES);
2805
2806         ipd = osd_idx_ipd_get(env, bag);
2807         if (IS_ERR(ipd))
2808                 RETURN(-ENOMEM);
2809
2810         /* got ipd now we can start iterator. */
2811         iam_it_init(it, bag, 0, ipd);
2812
2813         rc = iam_it_get(it, (struct iam_key *)key);
2814         if (rc >= 0) {
2815                 if (S_ISDIR(obj->oo_inode->i_mode))
2816                         iam_rec = (struct iam_rec *)oti->oti_ldp;
2817                 else
2818                         iam_rec = (struct iam_rec *) rec;
2819
2820                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2821                 if (S_ISDIR(obj->oo_inode->i_mode))
2822                         osd_fid_unpack((struct lu_fid *) rec,
2823                                        (struct osd_fid_pack *)iam_rec);
2824         }
2825         iam_it_put(it);
2826         iam_it_fini(it);
2827         osd_ipd_put(env, bag, ipd);
2828
2829         LINVRNT(osd_invariant(obj));
2830
2831         RETURN(rc);
2832 }
2833
2834 /**
2835  *      Inserts (key, value) pair in \a dt index object.
2836  *
2837  *      \param  dt      osd index object
2838  *      \param  key     key for index
2839  *      \param  rec     record reference
2840  *      \param  th      transaction handler
2841  *
2842  *      \retval  0  success
2843  *      \retval -ve failure
2844  */
2845 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2846                                 const struct dt_rec *rec, const struct dt_key *key,
2847                                 struct thandle *th, struct lustre_capa *capa,
2848                                 int ignore_quota)
2849 {
2850         struct osd_object     *obj = osd_dt_obj(dt);
2851         struct iam_path_descr *ipd;
2852         struct osd_thandle    *oh;
2853         struct iam_container  *bag = &obj->oo_dir->od_container;
2854 #ifdef HAVE_QUOTA_SUPPORT
2855         cfs_cap_t              save = cfs_curproc_cap_pack();
2856 #endif
2857         struct osd_thread_info *oti = osd_oti_get(env);
2858         struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2859         int rc;
2860
2861         ENTRY;
2862
2863         LINVRNT(osd_invariant(obj));
2864         LASSERT(dt_object_exists(dt));
2865         LASSERT(bag->ic_object == obj->oo_inode);
2866         LASSERT(th != NULL);
2867
2868         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2869                 return -EACCES;
2870
2871         ipd = osd_idx_ipd_get(env, bag);
2872         if (unlikely(ipd == NULL))
2873                 RETURN(-ENOMEM);
2874
2875         oh = container_of0(th, struct osd_thandle, ot_super);
2876         LASSERT(oh->ot_handle != NULL);
2877         LASSERT(oh->ot_handle->h_transaction != NULL);
2878 #ifdef HAVE_QUOTA_SUPPORT
2879         if (ignore_quota)
2880                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2881         else
2882                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2883 #endif
2884         if (S_ISDIR(obj->oo_inode->i_mode))
2885                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2886         else
2887                 iam_rec = (struct iam_rec *) rec;
2888         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2889                         iam_rec, ipd);
2890 #ifdef HAVE_QUOTA_SUPPORT
2891         cfs_curproc_cap_unpack(save);
2892 #endif
2893         osd_ipd_put(env, bag, ipd);
2894         LINVRNT(osd_invariant(obj));
2895         RETURN(rc);
2896 }
2897
2898 /**
2899  * Calls ldiskfs_add_entry() to add directory entry
2900  * into the directory. This is required for
2901  * interoperability mode (b11826)
2902  *
2903  * \retval   0, on success
2904  * \retval -ve, on error
2905  */
2906 static int __osd_ea_add_rec(struct osd_thread_info *info,
2907                             struct osd_object *pobj,
2908                             struct inode  *cinode,
2909                             const char *name,
2910                             const struct dt_rec *fid,
2911                             struct thandle *th)
2912 {
2913         struct ldiskfs_dentry_param *ldp;
2914         struct dentry      *child;
2915         struct osd_thandle *oth;
2916         int rc;
2917
2918         oth = container_of(th, struct osd_thandle, ot_super);
2919         LASSERT(oth->ot_handle != NULL);
2920         LASSERT(oth->ot_handle->h_transaction != NULL);
2921
2922         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2923
2924         if (fid_is_igif((struct lu_fid *)fid) ||
2925             fid_is_norm((struct lu_fid *)fid)) {
2926                 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2927                 osd_get_ldiskfs_dirent_param(ldp, fid);
2928                 child->d_fsdata = (void*) ldp;
2929         } else
2930                 child->d_fsdata = NULL;
2931         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2932
2933         RETURN(rc);
2934 }
2935
2936 /**
2937  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2938  * into the directory.Also sets flags into osd object to
2939  * indicate dot and dotdot are created. This is required for
2940  * interoperability mode (b11826)
2941  *
2942  * \param dir   directory for dot and dotdot fixup.
2943  * \param obj   child object for linking
2944  *
2945  * \retval   0, on success
2946  * \retval -ve, on error
2947  */
2948 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2949                               struct osd_object *dir,
2950                               struct inode  *parent_dir, const char *name,
2951                               const struct dt_rec *dot_fid,
2952                               const struct dt_rec *dot_dot_fid,
2953                               struct thandle *th)
2954 {
2955         struct inode            *inode  = dir->oo_inode;
2956         struct ldiskfs_dentry_param *dot_ldp;
2957         struct ldiskfs_dentry_param *dot_dot_ldp;
2958         struct osd_thandle      *oth;
2959         int result = 0;
2960
2961         oth = container_of(th, struct osd_thandle, ot_super);
2962         LASSERT(oth->ot_handle->h_transaction != NULL);
2963         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2964
2965         if (strcmp(name, dot) == 0) {
2966                 if (dir->oo_compat_dot_created) {
2967                         result = -EEXIST;
2968                 } else {
2969                         LASSERT(inode == parent_dir);
2970                         dir->oo_compat_dot_created = 1;
2971                         result = 0;
2972                 }
2973         } else if(strcmp(name, dotdot) == 0) {
2974                 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2975                 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2976
2977                 if (!dir->oo_compat_dot_created)
2978                         return -EINVAL;
2979                 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
2980                         osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2981                         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2982                 } else {
2983                         dot_ldp = NULL;
2984                         dot_dot_ldp = NULL;
2985                 }
2986                 /* in case of rename, dotdot is already created */
2987                 if (dir->oo_compat_dotdot_created) {
2988                         return __osd_ea_add_rec(info, dir, parent_dir, name,
2989                                                 dot_dot_fid, th);
2990                 }
2991
2992                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
2993                                                 dot_ldp, dot_dot_ldp);
2994                 if (result == 0)
2995                        dir->oo_compat_dotdot_created = 1;
2996         }
2997
2998         return result;
2999 }
3000
3001
3002 /**
3003  * It will call the appropriate osd_add* function and return the
3004  * value, return by respective functions.
3005  */
3006 static int osd_ea_add_rec(const struct lu_env *env,
3007                           struct osd_object *pobj,
3008                           struct inode *cinode,
3009                           const char *name,
3010                           const struct dt_rec *fid,
3011                           struct thandle *th)
3012 {
3013         struct osd_thread_info    *info   = osd_oti_get(env);
3014         int rc;
3015
3016         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3017                                                    name[2] =='\0')))
3018                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3019                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3020                                         fid, th);
3021         else
3022                 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3023
3024         return rc;
3025 }
3026
3027 /**
3028  * Calls ->lookup() to find dentry. From dentry get inode and
3029  * read inode's ea to get fid. This is required for  interoperability
3030  * mode (b11826)
3031  *
3032  * \retval   0, on success
3033  * \retval -ve, on error
3034  */
3035 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3036                              struct dt_rec *rec, const struct dt_key *key)
3037 {
3038         struct inode               *dir    = obj->oo_inode;
3039         struct dentry              *dentry;
3040         struct ldiskfs_dir_entry_2 *de;
3041         struct buffer_head         *bh;
3042         struct lu_fid              *fid = (struct lu_fid *) rec;
3043         int ino;
3044         int rc;
3045
3046         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3047
3048         dentry = osd_child_dentry_get(env, obj,
3049                                       (char *)key, strlen((char *)key));
3050
3051         cfs_down_read(&obj->oo_ext_idx_sem);
3052         bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3053         if (bh) {
3054                 ino = le32_to_cpu(de->inode);
3055                 rc = osd_get_fid_from_dentry(de, rec);
3056
3057                 /* done with de, release bh */
3058                 brelse(bh);
3059                 if (rc != 0)
3060                         rc = osd_ea_fid_get(env, obj, ino, fid);
3061         } else
3062                 rc = -ENOENT;
3063
3064         cfs_up_read(&obj->oo_ext_idx_sem);
3065         RETURN (rc);
3066 }
3067
3068 /**
3069  * Find the osd object for given fid.
3070  *
3071  * \param fid need to find the osd object having this fid
3072  *
3073  * \retval osd_object on success
3074  * \retval        -ve on error
3075  */
3076 struct osd_object *osd_object_find(const struct lu_env *env,
3077                                    struct dt_object *dt,
3078                                    const struct lu_fid *fid)
3079 {
3080         struct lu_device         *ludev = dt->do_lu.lo_dev;
3081         struct osd_object        *child = NULL;
3082         struct lu_object         *luch;
3083         struct lu_object         *lo;
3084
3085         luch = lu_object_find(env, ludev, fid, NULL);
3086         if (!IS_ERR(luch)) {
3087                 if (lu_object_exists(luch)) {
3088                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3089                         if (lo != NULL)
3090                                 child = osd_obj(lo);
3091                         else
3092                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3093                                                 "lu_object can't be located"
3094                                                 ""DFID"\n", PFID(fid));
3095
3096                         if (child == NULL) {
3097                                 lu_object_put(env, luch);
3098                                 CERROR("Unable to get osd_object\n");
3099                                 child = ERR_PTR(-ENOENT);
3100                         }
3101                 } else {
3102                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
3103                                         "lu_object does not exists "DFID"\n",
3104                                         PFID(fid));
3105                         child = ERR_PTR(-ENOENT);
3106                 }
3107         } else
3108                 child = (void *)luch;
3109
3110         return child;
3111 }
3112
3113 /**
3114  * Put the osd object once done with it.
3115  *
3116  * \param obj osd object that needs to be put
3117  */
3118 static inline void osd_object_put(const struct lu_env *env,
3119                                   struct osd_object *obj)
3120 {
3121         lu_object_put(env, &obj->oo_dt.do_lu);
3122 }
3123
3124 /**
3125  * Index add function for interoperability mode (b11826).
3126  * It will add the directory entry.This entry is needed to
3127  * maintain name->fid mapping.
3128  *
3129  * \param key it is key i.e. file entry to be inserted
3130  * \param rec it is value of given key i.e. fid
3131  *
3132  * \retval   0, on success
3133  * \retval -ve, on error
3134  */
3135 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3136                                const struct dt_rec *rec,
3137                                const struct dt_key *key, struct thandle *th,
3138                                struct lustre_capa *capa, int ignore_quota)
3139 {
3140         struct osd_object        *obj   = osd_dt_obj(dt);
3141         struct lu_fid            *fid   = (struct lu_fid *) rec;
3142         const char               *name  = (const char *)key;
3143         struct osd_object        *child;
3144 #ifdef HAVE_QUOTA_SUPPORT
3145         cfs_cap_t                 save  = cfs_curproc_cap_pack();
3146 #endif
3147         int rc;
3148
3149         ENTRY;
3150
3151         LASSERT(osd_invariant(obj));
3152         LASSERT(dt_object_exists(dt));
3153         LASSERT(th != NULL);
3154
3155         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3156                 RETURN(-EACCES);
3157
3158         child = osd_object_find(env, dt, fid);
3159         if (!IS_ERR(child)) {
3160 #ifdef HAVE_QUOTA_SUPPORT
3161                 if (ignore_quota)
3162                         cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3163                 else
3164                         cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3165 #endif
3166                 cfs_down_write(&obj->oo_ext_idx_sem);
3167                 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3168                 cfs_up_write(&obj->oo_ext_idx_sem);
3169 #ifdef HAVE_QUOTA_SUPPORT
3170                 cfs_curproc_cap_unpack(save);
3171 #endif
3172                 osd_object_put(env, child);
3173         } else {
3174                 rc = PTR_ERR(child);
3175         }
3176
3177         LASSERT(osd_invariant(obj));
3178         RETURN(rc);
3179 }
3180
3181 /**
3182  *  Initialize osd Iterator for given osd index object.
3183  *
3184  *  \param  dt      osd index object
3185  */
3186
3187 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3188                                      struct dt_object *dt,
3189                                      __u32 unused,
3190                                      struct lustre_capa *capa)
3191 {
3192         struct osd_it_iam         *it;
3193         struct osd_thread_info *oti = osd_oti_get(env);
3194         struct osd_object     *obj = osd_dt_obj(dt);
3195         struct lu_object      *lo  = &dt->do_lu;
3196         struct iam_path_descr *ipd;
3197         struct iam_container  *bag = &obj->oo_dir->od_container;
3198
3199         LASSERT(lu_object_exists(lo));
3200
3201         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3202                 return ERR_PTR(-EACCES);
3203
3204         it = &oti->oti_it;
3205         ipd = osd_it_ipd_get(env, bag);
3206         if (likely(ipd != NULL)) {
3207                 it->oi_obj = obj;
3208                 it->oi_ipd = ipd;
3209                 lu_object_get(lo);
3210                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3211                 return (struct dt_it *)it;
3212         }
3213         return ERR_PTR(-ENOMEM);
3214 }
3215
3216 /**
3217  * free given Iterator.
3218  */
3219
3220 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3221 {
3222         struct osd_it_iam     *it = (struct osd_it_iam *)di;
3223         struct osd_object *obj = it->oi_obj;
3224
3225         iam_it_fini(&it->oi_it);
3226         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3227         lu_object_put(env, &obj->oo_dt.do_lu);
3228 }
3229
3230 /**
3231  *  Move Iterator to record specified by \a key
3232  *
3233  *  \param  di      osd iterator
3234  *  \param  key     key for index
3235  *
3236  *  \retval +ve  di points to record with least key not larger than key
3237  *  \retval  0   di points to exact matched key
3238  *  \retval -ve  failure
3239  */
3240
3241 static int osd_it_iam_get(const struct lu_env *env,
3242                       struct dt_it *di, const struct dt_key *key)
3243 {
3244         struct osd_it_iam *it = (struct osd_it_iam *)di;
3245
3246         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3247 }
3248
3249 /**
3250  *  Release Iterator
3251  *
3252  *  \param  di      osd iterator
3253  */
3254
3255 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3256 {
3257         struct osd_it_iam *it = (struct osd_it_iam *)di;
3258
3259         iam_it_put(&it->oi_it);
3260 }
3261
3262 /**
3263  *  Move iterator by one record
3264  *
3265  *  \param  di      osd iterator
3266  *
3267  *  \retval +1   end of container reached
3268  *  \retval  0   success
3269  *  \retval -ve  failure
3270  */
3271
3272 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3273 {
3274         struct osd_it_iam *it = (struct osd_it_iam *)di;
3275
3276         return iam_it_next(&it->oi_it);
3277 }
3278
3279 /**
3280  * Return pointer to the key under iterator.
3281  */
3282
3283 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3284                                  const struct dt_it *di)
3285 {
3286         struct osd_it_iam *it = (struct osd_it_iam *)di;
3287
3288         return (struct dt_key *)iam_it_key_get(&it->oi_it);
3289 }
3290
3291 /**
3292  * Return size of key under iterator (in bytes)
3293  */
3294
3295 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3296 {
3297         struct osd_it_iam *it = (struct osd_it_iam *)di;
3298
3299         return iam_it_key_size(&it->oi_it);
3300 }
3301
3302 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3303                                        __u32 attr,
3304                                        int len,
3305                                        __u16 type)
3306 {
3307         struct luda_type        *lt;
3308         const unsigned           align = sizeof(struct luda_type) - 1;
3309
3310         /* check if file type is required */
3311         if (attr & LUDA_TYPE) {
3312                         len = (len + align) & ~align;
3313
3314                         lt = (void *) ent->lde_name + len;
3315                         lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3316                         ent->lde_attrs |= LUDA_TYPE;
3317         }
3318
3319         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3320 }
3321
3322 /**
3323  * build lu direct from backend fs dirent.
3324  */
3325
3326 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3327                                       struct lu_fid *fid,
3328                                       __u64 offset,
3329                                       char *name,
3330                                       __u16 namelen,
3331                                       __u16 type,
3332                                       __u32 attr)
3333 {
3334         fid_cpu_to_le(&ent->lde_fid, fid);
3335         ent->lde_attrs = LUDA_FID;
3336
3337         ent->lde_hash = cpu_to_le64(offset);
3338         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3339
3340         strncpy(ent->lde_name, name, namelen);
3341         ent->lde_namelen = cpu_to_le16(namelen);
3342
3343         /* append lustre attributes */
3344         osd_it_append_attrs(ent, attr, namelen, type);
3345 }
3346
3347 /**
3348  * Return pointer to the record under iterator.
3349  */
3350 static int osd_it_iam_rec(const struct lu_env *env,
3351                           const struct dt_it *di,
3352                           struct lu_dirent *lde,
3353                           __u32 attr)
3354 {
3355         struct osd_it_iam *it        = (struct osd_it_iam *)di;
3356         struct osd_thread_info *info = osd_oti_get(env);
3357         struct lu_fid     *fid       = &info->oti_fid;
3358         const struct osd_fid_pack *rec;
3359         char *name;
3360         int namelen;
3361         __u64 hash;
3362         int rc;
3363
3364         name = (char *)iam_it_key_get(&it->oi_it);
3365         if (IS_ERR(name))
3366                 RETURN(PTR_ERR(name));
3367
3368         namelen = iam_it_key_size(&it->oi_it);
3369
3370         rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3371         if (IS_ERR(rec))
3372                 RETURN(PTR_ERR(rec));
3373
3374         rc = osd_fid_unpack(fid, rec);
3375         if (rc)
3376                 RETURN(rc);
3377
3378         hash = iam_it_store(&it->oi_it);
3379
3380         /* IAM does not store object type in IAM index (dir) */
3381         osd_it_pack_dirent(lde, fid, hash, name, namelen,
3382                            0, LUDA_FID);
3383
3384         return 0;
3385 }
3386
3387 /**
3388  * Returns cookie for current Iterator position.
3389  */
3390 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3391 {
3392         struct osd_it_iam *it = (struct osd_it_iam *)di;
3393
3394         return iam_it_store(&it->oi_it);
3395 }
3396
3397 /**
3398  * Restore iterator from cookie.
3399  *
3400  * \param  di      osd iterator
3401  * \param  hash    Iterator location cookie
3402  *
3403  * \retval +ve  di points to record with least key not larger than key.
3404  * \retval  0   di points to exact matched key
3405  * \retval -ve  failure
3406  */
3407
3408 static int osd_it_iam_load(const struct lu_env *env,
3409                        const struct dt_it *di, __u64 hash)
3410 {
3411         struct osd_it_iam *it = (struct osd_it_iam *)di;
3412
3413         return iam_it_load(&it->oi_it, hash);
3414 }
3415
3416 static const struct dt_index_operations osd_index_iam_ops = {
3417         .dio_lookup = osd_index_iam_lookup,
3418         .dio_insert = osd_index_iam_insert,
3419         .dio_delete = osd_index_iam_delete,
3420         .dio_it     = {
3421                 .init     = osd_it_iam_init,
3422                 .fini     = osd_it_iam_fini,
3423                 .get      = osd_it_iam_get,
3424                 .put      = osd_it_iam_put,
3425                 .next     = osd_it_iam_next,
3426                 .key      = osd_it_iam_key,
3427                 .key_size = osd_it_iam_key_size,
3428                 .rec      = osd_it_iam_rec,
3429                 .store    = osd_it_iam_store,
3430                 .load     = osd_it_iam_load
3431         }
3432 };
3433
3434 /**
3435  * Creates or initializes iterator context.
3436  *
3437  * \retval struct osd_it_ea, iterator structure on success
3438  *
3439  */
3440 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3441                                     struct dt_object *dt,
3442                                     __u32 attr,
3443                                     struct lustre_capa *capa)
3444 {
3445         struct osd_object       *obj  = osd_dt_obj(dt);
3446         struct osd_thread_info  *info = osd_oti_get(env);
3447         struct osd_it_ea        *it   = &info->oti_it_ea;
3448         struct lu_object        *lo   = &dt->do_lu;
3449         struct dentry           *obj_dentry = &info->oti_it_dentry;
3450         ENTRY;
3451         LASSERT(lu_object_exists(lo));
3452
3453         obj_dentry->d_inode = obj->oo_inode;
3454         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3455         obj_dentry->d_name.hash = 0;
3456
3457         it->oie_rd_dirent       = 0;
3458         it->oie_it_dirent       = 0;
3459         it->oie_dirent          = NULL;
3460         it->oie_buf             = info->oti_it_ea_buf;
3461         it->oie_obj             = obj;
3462         it->oie_file.f_pos      = 0;
3463         it->oie_file.f_dentry   = obj_dentry;
3464         if (attr & LUDA_64BITHASH)
3465                 it->oie_file.f_flags = O_64BITHASH;
3466         else
3467                 it->oie_file.f_flags = O_32BITHASH;
3468         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3469         it->oie_file.f_op         = obj->oo_inode->i_fop;
3470         it->oie_file.private_data = NULL;
3471         lu_object_get(lo);
3472         RETURN((struct dt_it *) it);
3473 }
3474
3475 /**
3476  * Destroy or finishes iterator context.
3477  *
3478  * \param di iterator structure to be destroyed
3479  */
3480 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3481 {
3482         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3483         struct osd_object    *obj  = it->oie_obj;
3484         struct inode       *inode  = obj->oo_inode;
3485
3486         ENTRY;
3487         it->oie_file.f_op->release(inode, &it->oie_file);
3488         lu_object_put(env, &obj->oo_dt.do_lu);
3489         EXIT;
3490 }
3491
3492 /**
3493  * It position the iterator at given key, so that next lookup continues from
3494  * that key Or it is similar to dio_it->load() but based on a key,
3495  * rather than file position.
3496  *
3497  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3498  * to the beginning.
3499  *
3500  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3501  */
3502 static int osd_it_ea_get(const struct lu_env *env,
3503                          struct dt_it *di, const struct dt_key *key)
3504 {
3505         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3506
3507         ENTRY;
3508         LASSERT(((const char *)key)[0] == '\0');
3509         it->oie_file.f_pos      = 0;
3510         it->oie_rd_dirent       = 0;
3511         it->oie_it_dirent       = 0;
3512         it->oie_dirent          = NULL;
3513
3514         RETURN(+1);
3515 }
3516
3517 /**
3518  * Does nothing
3519  */
3520 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3521 {
3522 }
3523
3524 /**
3525  * It is called internally by ->readdir(). It fills the
3526  * iterator's in-memory data structure with required
3527  * information i.e. name, namelen, rec_size etc.
3528  *
3529  * \param buf in which information to be filled in.
3530  * \param name name of the file in given dir
3531  *
3532  * \retval 0 on success
3533  * \retval 1 on buffer full
3534  */
3535 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3536                                loff_t offset, __u64 ino,
3537                                unsigned d_type)
3538 {
3539         struct osd_it_ea        *it   = (struct osd_it_ea *)buf;
3540         struct osd_it_ea_dirent *ent  = it->oie_dirent;
3541         struct lu_fid           *fid  = &ent->oied_fid;
3542         struct osd_fid_pack     *rec;
3543         ENTRY;
3544
3545         /* this should never happen */
3546         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3547                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3548                 RETURN(-EIO);
3549         }
3550
3551         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3552             OSD_IT_EA_BUFSIZE)
3553                 RETURN(1);
3554
3555         if (d_type & LDISKFS_DIRENT_LUFID) {
3556                 rec = (struct osd_fid_pack*) (name + namelen + 1);
3557
3558                 if (osd_fid_unpack(fid, rec) != 0)
3559                         fid_zero(fid);
3560
3561                 d_type &= ~LDISKFS_DIRENT_LUFID;
3562         } else {
3563                 fid_zero(fid);
3564         }
3565
3566         ent->oied_ino     = ino;
3567         ent->oied_off     = offset;
3568         ent->oied_namelen = namelen;
3569         ent->oied_type    = d_type;
3570
3571         memcpy(ent->oied_name, name, namelen);
3572
3573         it->oie_rd_dirent++;
3574         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3575         RETURN(0);
3576 }
3577
3578 /**
3579  * Calls ->readdir() to load a directory entry at a time
3580  * and stored it in iterator's in-memory data structure.
3581  *
3582  * \param di iterator's in memory structure
3583  *
3584  * \retval   0 on success
3585  * \retval -ve on error
3586  */
3587 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3588 {
3589         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3590         struct osd_object  *obj   = it->oie_obj;
3591         struct inode       *inode = obj->oo_inode;
3592         int                result = 0;
3593
3594         ENTRY;
3595         it->oie_dirent = it->oie_buf;
3596         it->oie_rd_dirent = 0;
3597
3598         cfs_down_read(&obj->oo_ext_idx_sem);
3599         result = inode->i_fop->readdir(&it->oie_file, it,
3600                                        (filldir_t) osd_ldiskfs_filldir);
3601
3602         cfs_up_read(&obj->oo_ext_idx_sem);
3603
3604         if (it->oie_rd_dirent == 0) {
3605                 result = -EIO;
3606         } else {
3607                 it->oie_dirent = it->oie_buf;
3608                 it->oie_it_dirent = 1;
3609         }
3610
3611         RETURN(result);
3612 }
3613
3614 /**
3615  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3616  * to load a directory entry at a time and stored it in
3617  * iterator's in-memory data structure.
3618  *
3619  * \param di iterator's in memory structure
3620  *
3621  * \retval +ve iterator reached to end
3622  * \retval   0 iterator not reached to end
3623  * \retval -ve on error
3624  */
3625 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3626 {
3627         struct osd_it_ea *it = (struct osd_it_ea *)di;
3628         int rc;
3629
3630         ENTRY;
3631
3632         if (it->oie_it_dirent < it->oie_rd_dirent) {
3633                 it->oie_dirent =
3634                         (void *) it->oie_dirent +
3635                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3636                                        it->oie_dirent->oied_namelen);
3637                 it->oie_it_dirent++;
3638                 RETURN(0);
3639         } else {
3640                 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3641                         rc = +1;
3642                 else
3643                         rc = osd_ldiskfs_it_fill(di);
3644         }
3645
3646         RETURN(rc);
3647 }
3648
3649 /**
3650  * Returns the key at current position from iterator's in memory structure.
3651  *
3652  * \param di iterator's in memory structure
3653  *
3654  * \retval key i.e. struct dt_key on success
3655  */
3656 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3657                                     const struct dt_it *di)
3658 {
3659         struct osd_it_ea *it = (struct osd_it_ea *)di;
3660         ENTRY;
3661         RETURN((struct dt_key *)it->oie_dirent->oied_name);
3662 }
3663
3664 /**
3665  * Returns the key's size at current position from iterator's in memory structure.
3666  *
3667  * \param di iterator's in memory structure
3668  *
3669  * \retval key_size i.e. struct dt_key on success
3670  */
3671 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3672 {
3673         struct osd_it_ea *it = (struct osd_it_ea *)di;
3674         ENTRY;
3675         RETURN(it->oie_dirent->oied_namelen);
3676 }
3677
3678
3679 /**
3680  * Returns the value (i.e. fid/igif) at current position from iterator's
3681  * in memory structure.
3682  *
3683  * \param di struct osd_it_ea, iterator's in memory structure
3684  * \param attr attr requested for dirent.
3685  * \param lde lustre dirent
3686  *
3687  * \retval   0 no error and \param lde has correct lustre dirent.
3688  * \retval -ve on error
3689  */
3690 static inline int osd_it_ea_rec(const struct lu_env *env,
3691                                 const struct dt_it *di,
3692                                 struct lu_dirent *lde,
3693                                 __u32 attr)
3694 {
3695         struct osd_it_ea        *it     = (struct osd_it_ea *)di;
3696         struct osd_object       *obj    = it->oie_obj;
3697         struct lu_fid           *fid    = &it->oie_dirent->oied_fid;
3698         int    rc = 0;
3699
3700         ENTRY;
3701
3702         if (!fid_is_sane(fid))
3703                 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3704
3705         if (rc == 0)
3706                 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3707                                    it->oie_dirent->oied_name,
3708                                    it->oie_dirent->oied_namelen,
3709                                    it->oie_dirent->oied_type,
3710                                    attr);
3711         RETURN(rc);
3712 }
3713
3714 /**
3715  * Returns a cookie for current position of the iterator head, so that
3716  * user can use this cookie to load/start the iterator next time.
3717  *
3718  * \param di iterator's in memory structure
3719  *
3720  * \retval cookie for current position, on success
3721  */
3722 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3723 {
3724         struct osd_it_ea *it = (struct osd_it_ea *)di;
3725         ENTRY;
3726         RETURN(it->oie_dirent->oied_off);
3727 }
3728
3729 /**
3730  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3731  * to load a directory entry at a time and stored it i inn,
3732  * in iterator's in-memory data structure.
3733  *
3734  * \param di struct osd_it_ea, iterator's in memory structure
3735  *
3736  * \retval +ve on success
3737  * \retval -ve on error
3738  */
3739 static int osd_it_ea_load(const struct lu_env *env,
3740                           const struct dt_it *di, __u64 hash)
3741 {
3742         struct osd_it_ea *it = (struct osd_it_ea *)di;
3743         int rc;
3744
3745         ENTRY;
3746         it->oie_file.f_pos = hash;
3747
3748         rc =  osd_ldiskfs_it_fill(di);
3749         if (rc == 0)
3750                 rc = +1;
3751
3752         RETURN(rc);
3753 }
3754
3755 /**
3756  * Index lookup function for interoperability mode (b11826).
3757  *
3758  * \param key,  key i.e. file name to be searched
3759  *
3760  * \retval +ve, on success
3761  * \retval -ve, on error
3762  */
3763 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3764                                struct dt_rec *rec, const struct dt_key *key,
3765                                struct lustre_capa *capa)
3766 {
3767         struct osd_object *obj = osd_dt_obj(dt);
3768         int rc = 0;
3769
3770         ENTRY;
3771
3772         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3773         LINVRNT(osd_invariant(obj));
3774
3775         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3776                 return -EACCES;
3777
3778         rc = osd_ea_lookup_rec(env, obj, rec, key);
3779
3780         if (rc == 0)
3781                 rc = +1;
3782         RETURN(rc);
3783 }
3784
3785 /**
3786  * Index and Iterator operations for interoperability
3787  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3788  */
3789 static const struct dt_index_operations osd_index_ea_ops = {
3790         .dio_lookup = osd_index_ea_lookup,
3791         .dio_insert = osd_index_ea_insert,
3792         .dio_delete = osd_index_ea_delete,
3793         .dio_it     = {
3794                 .init     = osd_it_ea_init,
3795                 .fini     = osd_it_ea_fini,
3796                 .get      = osd_it_ea_get,
3797                 .put      = osd_it_ea_put,
3798                 .next     = osd_it_ea_next,
3799                 .key      = osd_it_ea_key,
3800                 .key_size = osd_it_ea_key_size,
3801                 .rec      = osd_it_ea_rec,
3802                 .store    = osd_it_ea_store,
3803                 .load     = osd_it_ea_load
3804         }
3805 };
3806
3807 static void *osd_key_init(const struct lu_context *ctx,
3808                           struct lu_context_key *key)
3809 {
3810         struct osd_thread_info *info;
3811
3812         OBD_ALLOC_PTR(info);
3813         if (info != NULL) {
3814                 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3815                 if (info->oti_it_ea_buf != NULL) {
3816                         info->oti_env = container_of(ctx, struct lu_env,
3817                                                      le_ctx);
3818                 } else {
3819                         OBD_FREE_PTR(info);
3820                         info = ERR_PTR(-ENOMEM);
3821                 }
3822         } else {
3823                 info = ERR_PTR(-ENOMEM);
3824         }
3825         return info;
3826 }
3827
3828 static void osd_key_fini(const struct lu_context *ctx,
3829                          struct lu_context_key *key, void* data)
3830 {
3831         struct osd_thread_info *info = data;
3832
3833         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3834         OBD_FREE_PTR(info);
3835 }
3836
3837 static void osd_key_exit(const struct lu_context *ctx,
3838                          struct lu_context_key *key, void *data)
3839 {
3840         struct osd_thread_info *info = data;
3841
3842         LASSERT(info->oti_r_locks == 0);
3843         LASSERT(info->oti_w_locks == 0);
3844         LASSERT(info->oti_txns    == 0);
3845 }
3846
3847 /* type constructor/destructor: osd_type_init, osd_type_fini */
3848 LU_TYPE_INIT_FINI(osd, &osd_key);
3849
3850 static struct lu_context_key osd_key = {
3851         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3852         .lct_init = osd_key_init,
3853         .lct_fini = osd_key_fini,
3854         .lct_exit = osd_key_exit
3855 };
3856
3857
3858 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3859                            const char *name, struct lu_device *next)
3860 {
3861         int rc;
3862         struct lu_context *ctx;
3863
3864         /* context for commit hooks */
3865         ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3866         rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3867         if (rc == 0) {
3868                 rc = osd_procfs_init(osd_dev(d), name);
3869                 ctx->lc_cookie = 0x3;
3870         }
3871         return rc;
3872 }
3873
3874 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3875 {
3876         struct osd_thread_info *info = osd_oti_get(env);
3877         ENTRY;
3878         if (o->od_obj_area != NULL) {
3879                 lu_object_put(env, &o->od_obj_area->do_lu);
3880                 o->od_obj_area = NULL;
3881         }
3882         osd_oi_fini(info, &o->od_oi);
3883
3884         RETURN(0);
3885 }
3886
3887 static int osd_mount(const struct lu_env *env,
3888                      struct osd_device *o, struct lustre_cfg *cfg)
3889 {
3890         struct lustre_mount_info *lmi;
3891         const char               *dev  = lustre_cfg_string(cfg, 0);
3892         struct lustre_disk_data  *ldd;
3893         struct lustre_sb_info    *lsi;
3894
3895         ENTRY;
3896         if (o->od_mount != NULL) {
3897                 CERROR("Already mounted (%s)\n", dev);
3898                 RETURN(-EEXIST);
3899         }
3900
3901         /* get mount */
3902         lmi = server_get_mount(dev);
3903         if (lmi == NULL) {
3904                 CERROR("Cannot get mount info for %s!\n", dev);
3905                 RETURN(-EFAULT);
3906         }
3907
3908         LASSERT(lmi != NULL);
3909         /* save lustre_mount_info in dt_device */
3910         o->od_mount = lmi;
3911
3912         lsi = s2lsi(lmi->lmi_sb);
3913         ldd = lsi->lsi_ldd;
3914
3915         if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3916                 o->od_iop_mode = 0;
3917                 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3918         } else
3919                 o->od_iop_mode = 1;
3920
3921         o->od_obj_area = NULL;
3922         RETURN(0);
3923 }
3924
3925 static struct lu_device *osd_device_fini(const struct lu_env *env,
3926                                          struct lu_device *d)
3927 {
3928         int rc;
3929         ENTRY;
3930
3931         shrink_dcache_sb(osd_sb(osd_dev(d)));
3932         osd_sync(env, lu2dt_dev(d));
3933
3934         rc = osd_procfs_fini(osd_dev(d));
3935         if (rc) {
3936                 CERROR("proc fini error %d \n", rc);
3937                 RETURN (ERR_PTR(rc));
3938         }
3939
3940         if (osd_dev(d)->od_mount)
3941                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3942                                  osd_dev(d)->od_mount->lmi_mnt);
3943         osd_dev(d)->od_mount = NULL;
3944
3945         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3946         RETURN(NULL);
3947 }
3948
3949 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3950                                           struct lu_device_type *t,
3951                                           struct lustre_cfg *cfg)
3952 {
3953         struct lu_device  *l;
3954         struct osd_device *o;
3955
3956         OBD_ALLOC_PTR(o);
3957         if (o != NULL) {
3958                 int result;
3959
3960                 result = dt_device_init(&o->od_dt_dev, t);
3961                 if (result == 0) {
3962                         l = osd2lu_dev(o);
3963                         l->ld_ops = &osd_lu_ops;
3964                         o->od_dt_dev.dd_ops = &osd_dt_ops;
3965                         cfs_spin_lock_init(&o->od_osfs_lock);
3966                         o->od_osfs_age = cfs_time_shift_64(-1000);
3967                         o->od_capa_hash = init_capa_hash();
3968                         if (o->od_capa_hash == NULL) {
3969                                 dt_device_fini(&o->od_dt_dev);
3970                                 l = ERR_PTR(-ENOMEM);
3971                         }
3972                 } else
3973                         l = ERR_PTR(result);
3974
3975                 if (IS_ERR(l))
3976                         OBD_FREE_PTR(o);
3977         } else
3978                 l = ERR_PTR(-ENOMEM);
3979         return l;
3980 }
3981
3982 static struct lu_device *osd_device_free(const struct lu_env *env,
3983                                          struct lu_device *d)
3984 {
3985         struct osd_device *o = osd_dev(d);
3986         ENTRY;
3987
3988         cleanup_capa_hash(o->od_capa_hash);
3989         dt_device_fini(&o->od_dt_dev);
3990         OBD_FREE_PTR(o);
3991         RETURN(NULL);
3992 }
3993
3994 static int osd_process_config(const struct lu_env *env,
3995                               struct lu_device *d, struct lustre_cfg *cfg)
3996 {
3997         struct osd_device *o = osd_dev(d);
3998         int err;
3999         ENTRY;
4000
4001         switch(cfg->lcfg_command) {
4002         case LCFG_SETUP:
4003                 err = osd_mount(env, o, cfg);
4004                 break;
4005         case LCFG_CLEANUP:
4006                 err = osd_shutdown(env, o);
4007                 break;
4008         default:
4009                 err = -ENOSYS;
4010         }
4011
4012         RETURN(err);
4013 }
4014
4015 static int osd_recovery_complete(const struct lu_env *env,
4016                                  struct lu_device *d)
4017 {
4018         RETURN(0);
4019 }
4020
4021 static int osd_prepare(const struct lu_env *env,
4022                        struct lu_device *pdev,
4023                        struct lu_device *dev)
4024 {
4025         struct osd_device *osd = osd_dev(dev);
4026         struct lustre_sb_info *lsi;
4027         struct lustre_disk_data *ldd;
4028         struct lustre_mount_info  *lmi;
4029         struct osd_thread_info *oti = osd_oti_get(env);
4030         struct dt_object *d;
4031         int result;
4032
4033         ENTRY;
4034         /* 1. initialize oi before any file create or file open */
4035         result = osd_oi_init(oti, &osd->od_oi,
4036                              &osd->od_dt_dev, lu2md_dev(pdev));
4037         if (result != 0)
4038                 RETURN(result);
4039
4040         lmi = osd->od_mount;
4041         lsi = s2lsi(lmi->lmi_sb);
4042         ldd = lsi->lsi_ldd;
4043
4044         /* 2. setup local objects */
4045         result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4046         if (result)
4047                 goto out;
4048
4049         /* 3. open remote object dir */
4050         d = dt_store_open(env, lu2dt_dev(dev), "",
4051                           remote_obj_dir, &oti->oti_fid);
4052         if (!IS_ERR(d)) {
4053                 osd->od_obj_area = d;
4054                 result = 0;
4055         } else {
4056                 result = PTR_ERR(d);
4057                 osd->od_obj_area = NULL;
4058         }
4059
4060 out:
4061         RETURN(result);
4062 }
4063
4064 static const struct lu_object_operations osd_lu_obj_ops = {
4065         .loo_object_init      = osd_object_init,
4066         .loo_object_delete    = osd_object_delete,
4067         .loo_object_release   = osd_object_release,
4068         .loo_object_free      = osd_object_free,
4069         .loo_object_print     = osd_object_print,
4070         .loo_object_invariant = osd_object_invariant
4071 };
4072
4073 static const struct lu_device_operations osd_lu_ops = {
4074         .ldo_object_alloc      = osd_object_alloc,
4075         .ldo_process_config    = osd_process_config,
4076         .ldo_recovery_complete = osd_recovery_complete,
4077         .ldo_prepare           = osd_prepare,
4078 };
4079
4080 static const struct lu_device_type_operations osd_device_type_ops = {
4081         .ldto_init = osd_type_init,
4082         .ldto_fini = osd_type_fini,
4083
4084         .ldto_start = osd_type_start,
4085         .ldto_stop  = osd_type_stop,
4086
4087         .ldto_device_alloc = osd_device_alloc,
4088         .ldto_device_free  = osd_device_free,
4089
4090         .ldto_device_init    = osd_device_init,
4091         .ldto_device_fini    = osd_device_fini
4092 };
4093
4094 static struct lu_device_type osd_device_type = {
4095         .ldt_tags     = LU_DEVICE_DT,
4096         .ldt_name     = LUSTRE_OSD_NAME,
4097         .ldt_ops      = &osd_device_type_ops,
4098         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4099 };
4100
4101 /*
4102  * lprocfs legacy support.
4103  */
4104 static struct obd_ops osd_obd_device_ops = {
4105         .o_owner = THIS_MODULE
4106 };
4107
4108 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4109         .llod_name      = remote_obj_dir,
4110         .llod_oid       = OSD_REM_OBJ_DIR_OID,
4111         .llod_is_index  = 1,
4112         .llod_feat      = &dt_directory_features,
4113 };
4114
4115 static int __init osd_mod_init(void)
4116 {
4117         struct lprocfs_static_vars lvars;
4118
4119         osd_oi_mod_init();
4120         llo_local_obj_register(&llod_osd_rem_obj_dir);
4121         lprocfs_osd_init_vars(&lvars);
4122         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4123                                    LUSTRE_OSD_NAME, &osd_device_type);
4124 }
4125
4126 static void __exit osd_mod_exit(void)
4127 {
4128         llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4129         class_unregister_type(LUSTRE_OSD_NAME);
4130 }
4131
4132 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4133 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4134 MODULE_LICENSE("GPL");
4135
4136 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);