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