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