Whamcloud - gitweb
LU-911 osd: zerocopy methods in ldiskfs osd
[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, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/osd/osd_handler.c
39  *
40  * Top-level entry points into osd module
41  *
42  * Author: Nikita Danilov <nikita@clusterfs.com>
43  *         Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44  */
45
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <linux/module.h>
52
53 /* LUSTRE_VERSION_CODE */
54 #include <lustre_ver.h>
55 /* prerequisite for linux/xattr.h */
56 #include <linux/types.h>
57 /* prerequisite for linux/xattr.h */
58 #include <linux/fs.h>
59 /* XATTR_{REPLACE,CREATE} */
60 #include <linux/xattr.h>
61 /* simple_mkdir() */
62 #include <lvfs.h>
63
64 /*
65  * struct OBD_{ALLOC,FREE}*()
66  * OBD_FAIL_CHECK
67  */
68 #include <obd_support.h>
69 /* struct ptlrpc_thread */
70 #include <lustre_net.h>
71
72 /* fid_is_local() */
73 #include <lustre_fid.h>
74
75 #include "osd_internal.h"
76 #include "osd_igif.h"
77
78 /* llo_* api support */
79 #include <md_object.h>
80
81 #ifdef HAVE_LDISKFS_PDO
82 int ldiskfs_pdo = 1;
83 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
84                 "ldiskfs with parallel directory operations");
85 #else
86 int ldiskfs_pdo = 0;
87 #endif
88
89 static const char dot[] = ".";
90 static const char dotdot[] = "..";
91 static const char remote_obj_dir[] = "REM_OBJ_DIR";
92
93 static const struct lu_object_operations      osd_lu_obj_ops;
94 static const struct dt_object_operations      osd_obj_ops;
95 static const struct dt_object_operations      osd_obj_ea_ops;
96 static const struct dt_index_operations       osd_index_iam_ops;
97 static const struct dt_index_operations       osd_index_ea_ops;
98
99 static int osd_has_index(const struct osd_object *obj)
100 {
101         return obj->oo_dt.do_index_ops != NULL;
102 }
103
104 static int osd_object_invariant(const struct lu_object *l)
105 {
106         return osd_invariant(osd_obj(l));
107 }
108
109 #ifdef HAVE_QUOTA_SUPPORT
110 static inline void
111 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
112 {
113         struct md_ucred    *uc = md_ucred(env);
114         struct cred        *tc;
115
116         LASSERT(uc != NULL);
117
118         save->oc_uid = current_fsuid();
119         save->oc_gid = current_fsgid();
120         save->oc_cap = current_cap();
121         if ((tc = prepare_creds())) {
122                 tc->fsuid         = uc->mu_fsuid;
123                 tc->fsgid         = uc->mu_fsgid;
124                 commit_creds(tc);
125         }
126         /* XXX not suboptimal */
127         cfs_curproc_cap_unpack(uc->mu_cap);
128 }
129
130 static inline void
131 osd_pop_ctxt(struct osd_ctxt *save)
132 {
133         struct cred *tc;
134
135         if ((tc = prepare_creds())) {
136                 tc->fsuid         = save->oc_uid;
137                 tc->fsgid         = save->oc_gid;
138                 tc->cap_effective = save->oc_cap;
139                 commit_creds(tc);
140         }
141 }
142 #endif
143
144 /*
145  * Concurrency: doesn't matter
146  */
147 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
148 {
149         return osd_oti_get(env)->oti_r_locks > 0;
150 }
151
152 /*
153  * Concurrency: doesn't matter
154  */
155 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
156 {
157         struct osd_thread_info *oti = osd_oti_get(env);
158         return oti->oti_w_locks > 0 && o->oo_owner == env;
159 }
160
161 /*
162  * Concurrency: doesn't access mutable data
163  */
164 static int osd_root_get(const struct lu_env *env,
165                         struct dt_device *dev, struct lu_fid *f)
166 {
167         struct inode *inode;
168
169         inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
170         LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
171         return 0;
172 }
173
174 static inline int osd_qid_type(struct osd_thandle *oh, int i)
175 {
176         return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
177 }
178
179 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
180 {
181         oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
182 }
183
184 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
185                      int type, uid_t id, struct inode *inode)
186 {
187 #ifdef CONFIG_QUOTA
188         int i, allocated = 0;
189         struct osd_object *obj;
190
191         LASSERT(dt != NULL);
192         LASSERT(oh != NULL);
193         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
194                  oh->ot_id_cnt);
195
196         /* id entry is allocated in the quota file */
197         if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
198                 allocated = 1;
199
200         for (i = 0; i < oh->ot_id_cnt; i++) {
201                 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
202                         return;
203         }
204
205         if (unlikely(i >= OSD_MAX_UGID_CNT)) {
206                 CERROR("more than %d uid/gids for a transaction?\n", i);
207                 return;
208         }
209
210         oh->ot_id_array[i] = id;
211         osd_qid_set_type(oh, i, type);
212         oh->ot_id_cnt++;
213         obj = osd_dt_obj(dt);
214         oh->ot_credits += (allocated || id == 0) ?
215                 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
216 #endif
217 }
218
219 /*
220  * OSD object methods.
221  */
222
223 /*
224  * Concurrency: no concurrent access is possible that early in object
225  * life-cycle.
226  */
227 static struct lu_object *osd_object_alloc(const struct lu_env *env,
228                                           const struct lu_object_header *hdr,
229                                           struct lu_device *d)
230 {
231         struct osd_object *mo;
232
233         OBD_ALLOC_PTR(mo);
234         if (mo != NULL) {
235                 struct lu_object *l;
236
237                 l = &mo->oo_dt.do_lu;
238                 dt_object_init(&mo->oo_dt, NULL, d);
239                 if (osd_dev(d)->od_iop_mode)
240                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
241                 else
242                         mo->oo_dt.do_ops = &osd_obj_ops;
243
244                 l->lo_ops = &osd_lu_obj_ops;
245                 cfs_init_rwsem(&mo->oo_sem);
246                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
247                 cfs_spin_lock_init(&mo->oo_guard);
248                 return l;
249         } else {
250                 return NULL;
251         }
252 }
253
254 /*
255  * retrieve object from backend ext fs.
256  **/
257 static struct inode *osd_iget(struct osd_thread_info *info,
258                               struct osd_device *dev,
259                               const struct osd_inode_id *id)
260 {
261         struct inode *inode = NULL;
262
263         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
264         if (IS_ERR(inode)) {
265                 CERROR("Cannot get inode, rc = %li\n", PTR_ERR(inode));
266         } else if (id->oii_gen != OSD_OII_NOGEN &&
267                    inode->i_generation != id->oii_gen) {
268                 iput(inode);
269                 inode = ERR_PTR(-ESTALE);
270         } else if (inode->i_nlink == 0) {
271                 /* due to parallel readdir and unlink,
272                 * we can have dead inode here. */
273                 CWARN("stale inode\n");
274                 make_bad_inode(inode);
275                 iput(inode);
276                 inode = ERR_PTR(-ESTALE);
277         } else if (is_bad_inode(inode)) {
278                 CERROR("bad inode %lx\n",inode->i_ino);
279                 iput(inode);
280                 inode = ERR_PTR(-ENOENT);
281         } else {
282                 /* Do not update file c/mtime in ldiskfs.
283                  * NB: we don't have any lock to protect this because we don't
284                  * have reference on osd_object now, but contention with
285                  * another lookup + attr_set can't happen in the tiny window
286                  * between if (...) and set S_NOCMTIME. */
287                 if (!(inode->i_flags & S_NOCMTIME))
288                         inode->i_flags |= S_NOCMTIME;
289         }
290         return inode;
291 }
292
293 static int osd_fid_lookup(const struct lu_env *env,
294                           struct osd_object *obj, const struct lu_fid *fid)
295 {
296         struct osd_thread_info *info;
297         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
298         struct osd_device      *dev;
299         struct osd_inode_id    *id;
300         struct inode           *inode;
301         int                     result;
302
303         LINVRNT(osd_invariant(obj));
304         LASSERT(obj->oo_inode == NULL);
305         LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
306         /*
307          * This assertion checks that osd layer sees only local
308          * fids. Unfortunately it is somewhat expensive (does a
309          * cache-lookup). Disabling it for production/acceptance-testing.
310          */
311         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
312
313         ENTRY;
314
315         info = osd_oti_get(env);
316         dev  = osd_dev(ldev);
317         id   = &info->oti_id;
318
319         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
320                 RETURN(-ENOENT);
321
322         result = osd_oi_lookup(info, osd_fid2oi(dev, fid), fid, id);
323         if (result != 0) {
324                 if (result == -ENOENT)
325                         result = 0;
326                 goto out;
327         }
328
329         inode = osd_iget(info, dev, id);
330         if (IS_ERR(inode)) {
331                 /*
332                  * If fid wasn't found in oi, inode-less object is
333                  * created, for which lu_object_exists() returns
334                  * false. This is used in a (frequent) case when
335                  * objects are created as locking anchors or
336                  * place holders for objects yet to be created.
337                  */
338                 result = PTR_ERR(inode);
339                 goto out;
340         }
341
342         obj->oo_inode = inode;
343         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
344         if (dev->od_iop_mode) {
345                 obj->oo_compat_dot_created = 1;
346                 obj->oo_compat_dotdot_created = 1;
347         }
348
349         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
350                 goto out;
351
352         LASSERT(obj->oo_hl_head == NULL);
353         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
354         if (obj->oo_hl_head == NULL) {
355                 obj->oo_inode = NULL;
356                 iput(inode);
357                 result = -ENOMEM;
358         }
359 out:
360         LINVRNT(osd_invariant(obj));
361
362         RETURN(result);
363 }
364
365 /*
366  * Concurrency: shouldn't matter.
367  */
368 static void osd_object_init0(struct osd_object *obj)
369 {
370         LASSERT(obj->oo_inode != NULL);
371         obj->oo_dt.do_body_ops = &osd_body_ops;
372         obj->oo_dt.do_lu.lo_header->loh_attr |=
373                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
374 }
375
376 /*
377  * Concurrency: no concurrent access is possible that early in object
378  * life-cycle.
379  */
380 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
381                            const struct lu_object_conf *unused)
382 {
383         struct osd_object *obj = osd_obj(l);
384         int result;
385
386         LINVRNT(osd_invariant(obj));
387
388         result = osd_fid_lookup(env, obj, lu_object_fid(l));
389         obj->oo_dt.do_body_ops = &osd_body_ops_new;
390         if (result == 0) {
391                 if (obj->oo_inode != NULL)
392                         osd_object_init0(obj);
393         }
394         LINVRNT(osd_invariant(obj));
395         return result;
396 }
397
398 /*
399  * Concurrency: no concurrent access is possible that late in object
400  * life-cycle.
401  */
402 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
403 {
404         struct osd_object *obj = osd_obj(l);
405
406         LINVRNT(osd_invariant(obj));
407
408         dt_object_fini(&obj->oo_dt);
409         if (obj->oo_hl_head != NULL)
410                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
411         OBD_FREE_PTR(obj);
412 }
413
414 /**
415  * IAM Iterator
416  */
417 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
418                                              const struct iam_container *bag)
419 {
420         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
421                                            osd_oti_get(env)->oti_it_ipd);
422 }
423
424 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
425                                               const struct iam_container *bag)
426 {
427         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
428                                            osd_oti_get(env)->oti_idx_ipd);
429 }
430
431 static void osd_ipd_put(const struct lu_env *env,
432                         const struct iam_container *bag,
433                         struct iam_path_descr *ipd)
434 {
435         bag->ic_descr->id_ops->id_ipd_free(ipd);
436 }
437
438 /*
439  * Concurrency: no concurrent access is possible that late in object
440  * life-cycle.
441  */
442 static void osd_index_fini(struct osd_object *o)
443 {
444         struct iam_container *bag;
445
446         if (o->oo_dir != NULL) {
447                 bag = &o->oo_dir->od_container;
448                 if (o->oo_inode != NULL) {
449                         if (bag->ic_object == o->oo_inode)
450                                 iam_container_fini(bag);
451                 }
452                 OBD_FREE_PTR(o->oo_dir);
453                 o->oo_dir = NULL;
454         }
455 }
456
457 /*
458  * Concurrency: no concurrent access is possible that late in object
459  * life-cycle (for all existing callers, that is. New callers have to provide
460  * their own locking.)
461  */
462 static int osd_inode_unlinked(const struct inode *inode)
463 {
464         return inode->i_nlink == 0;
465 }
466
467 enum {
468         OSD_TXN_OI_DELETE_CREDITS    = 20,
469         OSD_TXN_INODE_DELETE_CREDITS = 20
470 };
471
472 /*
473  * Journal
474  */
475
476 #if OSD_THANDLE_STATS
477 /**
478  * Set time when the handle is allocated
479  */
480 static void osd_th_alloced(struct osd_thandle *oth)
481 {
482         oth->oth_alloced = cfs_time_current();
483 }
484
485 /**
486  * Set time when the handle started
487  */
488 static void osd_th_started(struct osd_thandle *oth)
489 {
490         oth->oth_started = cfs_time_current();
491 }
492
493 /**
494  * Helper function to convert time interval to microseconds packed in
495  * long int (default time units for the counter in "stats" initialized
496  * by lu_time_init() )
497  */
498 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
499 {
500         struct timeval val;
501
502         cfs_duration_usec(cfs_time_sub(end, start), &val);
503         return val.tv_sec * 1000000 + val.tv_usec;
504 }
505
506 /**
507  * Check whether the we deal with this handle for too long.
508  */
509 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
510                                 cfs_time_t alloced, cfs_time_t started,
511                                 cfs_time_t closed)
512 {
513         cfs_time_t now = cfs_time_current();
514
515         LASSERT(dev != NULL);
516
517         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
518                             interval_to_usec(alloced, started));
519         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
520                             interval_to_usec(started, closed));
521         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
522                             interval_to_usec(closed, now));
523
524         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
525                 CWARN("transaction handle %p was open for too long: "
526                       "now "CFS_TIME_T" ,"
527                       "alloced "CFS_TIME_T" ,"
528                       "started "CFS_TIME_T" ,"
529                       "closed "CFS_TIME_T"\n",
530                       oth, now, alloced, started, closed);
531                 libcfs_debug_dumpstack(NULL);
532         }
533 }
534
535 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
536 {                                                                       \
537         cfs_time_t __closed = cfs_time_current();                       \
538         cfs_time_t __alloced = oth->oth_alloced;                        \
539         cfs_time_t __started = oth->oth_started;                        \
540                                                                         \
541         expr;                                                           \
542         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
543 }
544
545 #else /* OSD_THANDLE_STATS */
546
547 #define osd_th_alloced(h)                  do {} while(0)
548 #define osd_th_started(h)                  do {} while(0)
549 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
550
551 #endif /* OSD_THANDLE_STATS */
552
553 /*
554  * Concurrency: doesn't access mutable data.
555  */
556 static int osd_param_is_sane(const struct osd_device *dev,
557                              const struct thandle *th)
558 {
559         struct osd_thandle *oh;
560         oh = container_of0(th, struct osd_thandle, ot_super);
561         return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
562 }
563
564 /*
565  * Concurrency: shouldn't matter.
566  */
567 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
568 static void osd_trans_commit_cb(struct super_block *sb,
569                                 struct journal_callback *jcb, int error)
570 #else
571 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
572 #endif
573 {
574         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
575         struct thandle     *th  = &oh->ot_super;
576         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
577         struct dt_txn_commit_cb *dcb, *tmp;
578
579         LASSERT(oh->ot_handle == NULL);
580
581         if (error)
582                 CERROR("transaction @0x%p commit error: %d\n", th, error);
583
584         dt_txn_hook_commit(th);
585
586         /* call per-transaction callbacks if any */
587         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
588                 dcb->dcb_func(NULL, th, dcb, error);
589
590         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
591         lu_device_put(lud);
592         th->th_dev = NULL;
593
594         lu_context_exit(&th->th_ctx);
595         lu_context_fini(&th->th_ctx);
596         OBD_FREE_PTR(oh);
597 }
598
599 static struct thandle *osd_trans_create(const struct lu_env *env,
600                                         struct dt_device *d)
601 {
602         struct osd_thread_info *oti = osd_oti_get(env);
603         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
604         struct osd_thandle     *oh;
605         struct thandle         *th;
606         ENTRY;
607
608         /* on pending IO in this thread should left from prev. request */
609         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
610
611         th = ERR_PTR(-ENOMEM);
612         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
613         if (oh != NULL) {
614                 th = &oh->ot_super;
615                 th->th_dev = d;
616                 th->th_result = 0;
617                 th->th_tags = LCT_TX_HANDLE;
618                 oh->ot_credits = 0;
619                 oti->oti_dev = osd_dt_dev(d);
620                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
621                 osd_th_alloced(oh);
622         }
623         RETURN(th);
624 }
625
626 /*
627  * Concurrency: shouldn't matter.
628  */
629 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
630                     struct thandle *th)
631 {
632         struct osd_thread_info *oti = osd_oti_get(env);
633         struct osd_device  *dev = osd_dt_dev(d);
634         handle_t           *jh;
635         struct osd_thandle *oh;
636         int rc;
637
638         ENTRY;
639
640         LASSERT(current->journal_info == NULL);
641
642         oh = container_of0(th, struct osd_thandle, ot_super);
643         LASSERT(oh != NULL);
644         LASSERT(oh->ot_handle == NULL);
645
646         rc = dt_txn_hook_start(env, d, th);
647         if (rc != 0)
648                 GOTO(out, rc);
649
650         if (!osd_param_is_sane(dev, th)) {
651                 CWARN("%s: too many transaction credits (%d > %d)\n",
652                       d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
653                       osd_journal(dev)->j_max_transaction_buffers);
654                 /* XXX Limit the credits to 'max_transaction_buffers', and
655                  *     let the underlying filesystem to catch the error if
656                  *     we really need so many credits.
657                  *
658                  *     This should be removed when we can calculate the
659                  *     credits precisely. */
660                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
661 #ifdef OSD_TRACK_DECLARES
662                 CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
663                        oh->ot_declare_attr_set, oh->ot_declare_punch,
664                        oh->ot_declare_xattr_set);
665                 CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
666                        oh->ot_declare_create, oh->ot_declare_ref_add,
667                        oh->ot_declare_ref_del, oh->ot_declare_write);
668                 CERROR("  insert: %d, delete: %d, destroy: %d\n",
669                        oh->ot_declare_insert, oh->ot_declare_delete,
670                        oh->ot_declare_destroy);
671 #endif
672         }
673
674         /*
675          * XXX temporary stuff. Some abstraction layer should
676          * be used.
677          */
678         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
679         osd_th_started(oh);
680         if (!IS_ERR(jh)) {
681                 oh->ot_handle = jh;
682                 LASSERT(oti->oti_txns == 0);
683                 lu_context_init(&th->th_ctx, th->th_tags);
684                 lu_context_enter(&th->th_ctx);
685
686                 lu_device_get(&d->dd_lu_dev);
687                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
688                                              "osd-tx", th);
689
690                 /*
691                  * XXX: current rule is that we first start tx,
692                  *      then lock object(s), but we can't use
693                  *      this rule for data (due to locking specifics
694                  *      in ldiskfs). also in long-term we'd like to
695                  *      use usually-used (locks;tx) ordering. so,
696                  *      UGLY thing is that we'll use one ordering for
697                  *      data (ofd) and reverse ordering for metadata
698                  *      (mdd). then at some point we'll fix the latter
699                  */
700                 if (lu_device_is_md(&d->dd_lu_dev)) {
701                         LASSERT(oti->oti_r_locks == 0);
702                         LASSERT(oti->oti_w_locks == 0);
703                 }
704
705                 oti->oti_txns++;
706                 rc = 0;
707         } else {
708                 rc = PTR_ERR(jh);
709         }
710 out:
711         RETURN(rc);
712 }
713
714 /*
715  * Concurrency: shouldn't matter.
716  */
717 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
718 {
719         int                     rc = 0;
720         struct osd_thandle     *oh;
721         struct osd_thread_info *oti = osd_oti_get(env);
722         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
723
724         ENTRY;
725
726         oh = container_of0(th, struct osd_thandle, ot_super);
727
728         if (oh->ot_handle != NULL) {
729                 handle_t *hdl = oh->ot_handle;
730
731                 hdl->h_sync = th->th_sync;
732
733                 /*
734                  * add commit callback
735                  * notice we don't do this in osd_trans_start()
736                  * as underlying transaction can change during truncate
737                  */
738                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
739                                          &oh->ot_jcb);
740
741                 LASSERT(oti->oti_txns == 1);
742                 oti->oti_txns--;
743                 /*
744                  * XXX: current rule is that we first start tx,
745                  *      then lock object(s), but we can't use
746                  *      this rule for data (due to locking specifics
747                  *      in ldiskfs). also in long-term we'd like to
748                  *      use usually-used (locks;tx) ordering. so,
749                  *      UGLY thing is that we'll use one ordering for
750                  *      data (ofd) and reverse ordering for metadata
751                  *      (mdd). then at some point we'll fix the latter
752                  */
753                 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
754                         LASSERT(oti->oti_r_locks == 0);
755                         LASSERT(oti->oti_w_locks == 0);
756                 }
757                 rc = dt_txn_hook_stop(env, th);
758                 if (rc != 0)
759                         CERROR("Failure in transaction hook: %d\n", rc);
760                 oh->ot_handle = NULL;
761                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
762                                   rc = ldiskfs_journal_stop(hdl));
763                 if (rc != 0)
764                         CERROR("Failure to stop transaction: %d\n", rc);
765         } else {
766                 OBD_FREE_PTR(oh);
767         }
768
769         /* as we want IO to journal and data IO be concurrent, we don't block
770          * awaiting data IO completion in osd_do_bio(), instead we wait here
771          * once transaction is submitted to the journal. all reqular requests
772          * don't do direct IO (except read/write), thus this wait_event becomes
773          * no-op for them.
774          *
775          * IMPORTANT: we have to wait till any IO submited by the thread is
776          * completed otherwise iobuf may be corrupted by different request
777          */
778         cfs_wait_event(iobuf->dr_wait, cfs_atomic_read(&iobuf->dr_numreqs)==0);
779         if (!rc)
780                 rc = iobuf->dr_error;
781
782         RETURN(rc);
783 }
784
785 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
786 {
787         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
788                                                ot_super);
789
790         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
791
792         return 0;
793 }
794
795 /*
796  * Called just before object is freed. Releases all resources except for
797  * object itself (that is released by osd_object_free()).
798  *
799  * Concurrency: no concurrent access is possible that late in object
800  * life-cycle.
801  */
802 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
803 {
804         struct osd_object *obj   = osd_obj(l);
805         struct inode      *inode = obj->oo_inode;
806
807         LINVRNT(osd_invariant(obj));
808
809         /*
810          * If object is unlinked remove fid->ino mapping from object index.
811          */
812
813         osd_index_fini(obj);
814         if (inode != NULL) {
815                 iput(inode);
816                 obj->oo_inode = NULL;
817         }
818 }
819
820 /*
821  * Concurrency: ->loo_object_release() is called under site spin-lock.
822  */
823 static void osd_object_release(const struct lu_env *env,
824                                struct lu_object *l)
825 {
826 }
827
828 /*
829  * Concurrency: shouldn't matter.
830  */
831 static int osd_object_print(const struct lu_env *env, void *cookie,
832                             lu_printer_t p, const struct lu_object *l)
833 {
834         struct osd_object *o = osd_obj(l);
835         struct iam_descr  *d;
836
837         if (o->oo_dir != NULL)
838                 d = o->oo_dir->od_container.ic_descr;
839         else
840                 d = NULL;
841         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
842                     o, o->oo_inode,
843                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
844                     o->oo_inode ? o->oo_inode->i_generation : 0,
845                     d ? d->id_ops->id_name : "plain");
846 }
847
848 /*
849  * Concurrency: shouldn't matter.
850  */
851 int osd_statfs(const struct lu_env *env, struct dt_device *d,
852                cfs_kstatfs_t *sfs)
853 {
854         struct osd_device *osd = osd_dt_dev(d);
855         struct super_block *sb = osd_sb(osd);
856         int result = 0;
857
858         cfs_spin_lock(&osd->od_osfs_lock);
859         /* cache 1 second */
860         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
861                 result = ll_do_statfs(sb, &osd->od_kstatfs);
862                 if (likely(result == 0)) /* N.B. statfs can't really fail */
863                         osd->od_osfs_age = cfs_time_current_64();
864         }
865
866         if (likely(result == 0))
867                 *sfs = osd->od_kstatfs;
868         cfs_spin_unlock(&osd->od_osfs_lock);
869
870         return result;
871 }
872
873 /*
874  * Concurrency: doesn't access mutable data.
875  */
876 static void osd_conf_get(const struct lu_env *env,
877                          const struct dt_device *dev,
878                          struct dt_device_param *param)
879 {
880         struct super_block *sb = osd_sb(osd_dt_dev(dev));
881
882         /*
883          * XXX should be taken from not-yet-existing fs abstraction layer.
884          */
885         param->ddp_max_name_len = LDISKFS_NAME_LEN;
886         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
887         param->ddp_block_shift  = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
888         param->ddp_mntopts      = 0;
889         if (test_opt(sb, XATTR_USER))
890                 param->ddp_mntopts |= MNTOPT_USERXATTR;
891         if (test_opt(sb, POSIX_ACL))
892                 param->ddp_mntopts |= MNTOPT_ACL;
893
894 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
895         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
896                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
897         else
898 #endif
899                 param->ddp_max_ea_size = sb->s_blocksize;
900
901 }
902
903 /**
904  * Helper function to get and fill the buffer with input values.
905  */
906 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
907 {
908         struct lu_buf *buf;
909
910         buf = &osd_oti_get(env)->oti_buf;
911         buf->lb_buf = area;
912         buf->lb_len = len;
913         return buf;
914 }
915
916 /*
917  * Concurrency: shouldn't matter.
918  */
919 static int osd_sync(const struct lu_env *env, struct dt_device *d)
920 {
921         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
922         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
923 }
924
925 /**
926  * Start commit for OSD device.
927  *
928  * An implementation of dt_commit_async method for OSD device.
929  * Asychronously starts underlayng fs sync and thereby a transaction
930  * commit.
931  *
932  * \param env environment
933  * \param d dt device
934  *
935  * \see dt_device_operations
936  */
937 static int osd_commit_async(const struct lu_env *env,
938                             struct dt_device *d)
939 {
940         struct super_block *s = osd_sb(osd_dt_dev(d));
941         ENTRY;
942
943         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
944         RETURN(s->s_op->sync_fs(s, 0));
945 }
946
947 /*
948  * Concurrency: shouldn't matter.
949  */
950
951 static void osd_ro(const struct lu_env *env, struct dt_device *d)
952 {
953         struct super_block *sb = osd_sb(osd_dt_dev(d));
954         ENTRY;
955
956         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
957
958         __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
959         EXIT;
960 }
961
962 /*
963  * Concurrency: serialization provided by callers.
964  */
965 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
966                               int mode, unsigned long timeout, __u32 alg,
967                               struct lustre_capa_key *keys)
968 {
969         struct osd_device *dev = osd_dt_dev(d);
970         ENTRY;
971
972         dev->od_fl_capa = mode;
973         dev->od_capa_timeout = timeout;
974         dev->od_capa_alg = alg;
975         dev->od_capa_keys = keys;
976         RETURN(0);
977 }
978
979 /**
980  * Concurrency: serialization provided by callers.
981  */
982 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
983                                struct dt_quota_ctxt *ctxt, void *data)
984 {
985         struct obd_device *obd = (void *)ctxt;
986         struct vfsmount *mnt = (struct vfsmount *)data;
987         ENTRY;
988
989         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
990         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
991         obd->obd_lvfs_ctxt.pwdmnt = mnt;
992         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
993         obd->obd_lvfs_ctxt.fs = get_ds();
994
995         EXIT;
996 }
997
998 /**
999  * Note: we do not count into QUOTA here.
1000  * If we mount with --data_journal we may need more.
1001  */
1002 const int osd_dto_credits_noquota[DTO_NR] = {
1003         /**
1004          * Insert/Delete.
1005          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1006          * SINGLEDATA_TRANS_BLOCKS(8)
1007          * XXX Note: maybe iam need more, since iam have more level than
1008          *           EXT3 htree.
1009          */
1010         [DTO_INDEX_INSERT]  = 16,
1011         [DTO_INDEX_DELETE]  = 16,
1012         /**
1013          * Unused now
1014          */
1015         [DTO_INDEX_UPDATE]  = 16,
1016         /**
1017          * Create a object. The same as create object in EXT3.
1018          * DATA_TRANS_BLOCKS(14) +
1019          * INDEX_EXTRA_BLOCKS(8) +
1020          * 3(inode bits, groups, GDT)
1021          */
1022         [DTO_OBJECT_CREATE] = 25,
1023         /**
1024          * XXX: real credits to be fixed
1025          */
1026         [DTO_OBJECT_DELETE] = 25,
1027         /**
1028          * Attr set credits (inode)
1029          */
1030         [DTO_ATTR_SET_BASE] = 1,
1031         /**
1032          * Xattr set. The same as xattr of EXT3.
1033          * DATA_TRANS_BLOCKS(14)
1034          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1035          * are also counted in. Do not know why?
1036          */
1037         [DTO_XATTR_SET]     = 14,
1038         [DTO_LOG_REC]       = 14,
1039         /**
1040          * credits for inode change during write.
1041          */
1042         [DTO_WRITE_BASE]    = 3,
1043         /**
1044          * credits for single block write.
1045          */
1046         [DTO_WRITE_BLOCK]   = 14,
1047         /**
1048          * Attr set credits for chown.
1049          * This is extra credits for setattr, and it is null without quota
1050          */
1051         [DTO_ATTR_SET_CHOWN]= 0
1052 };
1053
1054 static const struct dt_device_operations osd_dt_ops = {
1055         .dt_root_get       = osd_root_get,
1056         .dt_statfs         = osd_statfs,
1057         .dt_trans_create   = osd_trans_create,
1058         .dt_trans_start    = osd_trans_start,
1059         .dt_trans_stop     = osd_trans_stop,
1060         .dt_trans_cb_add   = osd_trans_cb_add,
1061         .dt_conf_get       = osd_conf_get,
1062         .dt_sync           = osd_sync,
1063         .dt_ro             = osd_ro,
1064         .dt_commit_async   = osd_commit_async,
1065         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1066         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1067 };
1068
1069 static void osd_object_read_lock(const struct lu_env *env,
1070                                  struct dt_object *dt, unsigned role)
1071 {
1072         struct osd_object *obj = osd_dt_obj(dt);
1073         struct osd_thread_info *oti = osd_oti_get(env);
1074
1075         LINVRNT(osd_invariant(obj));
1076
1077         LASSERT(obj->oo_owner != env);
1078         cfs_down_read_nested(&obj->oo_sem, role);
1079
1080         LASSERT(obj->oo_owner == NULL);
1081         oti->oti_r_locks++;
1082 }
1083
1084 static void osd_object_write_lock(const struct lu_env *env,
1085                                   struct dt_object *dt, unsigned role)
1086 {
1087         struct osd_object *obj = osd_dt_obj(dt);
1088         struct osd_thread_info *oti = osd_oti_get(env);
1089
1090         LINVRNT(osd_invariant(obj));
1091
1092         LASSERT(obj->oo_owner != env);
1093         cfs_down_write_nested(&obj->oo_sem, role);
1094
1095         LASSERT(obj->oo_owner == NULL);
1096         obj->oo_owner = env;
1097         oti->oti_w_locks++;
1098 }
1099
1100 static void osd_object_read_unlock(const struct lu_env *env,
1101                                    struct dt_object *dt)
1102 {
1103         struct osd_object *obj = osd_dt_obj(dt);
1104         struct osd_thread_info *oti = osd_oti_get(env);
1105
1106         LINVRNT(osd_invariant(obj));
1107
1108         LASSERT(oti->oti_r_locks > 0);
1109         oti->oti_r_locks--;
1110         cfs_up_read(&obj->oo_sem);
1111 }
1112
1113 static void osd_object_write_unlock(const struct lu_env *env,
1114                                     struct dt_object *dt)
1115 {
1116         struct osd_object *obj = osd_dt_obj(dt);
1117         struct osd_thread_info *oti = osd_oti_get(env);
1118
1119         LINVRNT(osd_invariant(obj));
1120
1121         LASSERT(obj->oo_owner == env);
1122         LASSERT(oti->oti_w_locks > 0);
1123         oti->oti_w_locks--;
1124         obj->oo_owner = NULL;
1125         cfs_up_write(&obj->oo_sem);
1126 }
1127
1128 static int osd_object_write_locked(const struct lu_env *env,
1129                                    struct dt_object *dt)
1130 {
1131         struct osd_object *obj = osd_dt_obj(dt);
1132
1133         LINVRNT(osd_invariant(obj));
1134
1135         return obj->oo_owner == env;
1136 }
1137
1138 static int capa_is_sane(const struct lu_env *env,
1139                         struct osd_device *dev,
1140                         struct lustre_capa *capa,
1141                         struct lustre_capa_key *keys)
1142 {
1143         struct osd_thread_info *oti = osd_oti_get(env);
1144         struct lustre_capa *tcapa = &oti->oti_capa;
1145         struct obd_capa *oc;
1146         int i, rc = 0;
1147         ENTRY;
1148
1149         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1150         if (oc) {
1151                 if (capa_is_expired(oc)) {
1152                         DEBUG_CAPA(D_ERROR, capa, "expired");
1153                         rc = -ESTALE;
1154                 }
1155                 capa_put(oc);
1156                 RETURN(rc);
1157         }
1158
1159         if (capa_is_expired_sec(capa)) {
1160                 DEBUG_CAPA(D_ERROR, capa, "expired");
1161                 RETURN(-ESTALE);
1162         }
1163
1164         cfs_spin_lock(&capa_lock);
1165         for (i = 0; i < 2; i++) {
1166                 if (keys[i].lk_keyid == capa->lc_keyid) {
1167                         oti->oti_capa_key = keys[i];
1168                         break;
1169                 }
1170         }
1171         cfs_spin_unlock(&capa_lock);
1172
1173         if (i == 2) {
1174                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1175                 RETURN(-ESTALE);
1176         }
1177
1178         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1179         if (rc)
1180                 RETURN(rc);
1181
1182         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1183                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1184                 RETURN(-EACCES);
1185         }
1186
1187         oc = capa_add(dev->od_capa_hash, capa);
1188         capa_put(oc);
1189
1190         RETURN(0);
1191 }
1192
1193 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1194                     struct lustre_capa *capa, __u64 opc)
1195 {
1196         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1197         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1198         struct md_capainfo *ci;
1199         int rc;
1200
1201         if (!dev->od_fl_capa)
1202                 return 0;
1203
1204         if (capa == BYPASS_CAPA)
1205                 return 0;
1206
1207         ci = md_capainfo(env);
1208         if (unlikely(!ci))
1209                 return 0;
1210
1211         if (ci->mc_auth == LC_ID_NONE)
1212                 return 0;
1213
1214         if (!capa) {
1215                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1216                 return -EACCES;
1217         }
1218
1219         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1220                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1221                            PFID(fid));
1222                 return -EACCES;
1223         }
1224
1225         if (!capa_opc_supported(capa, opc)) {
1226                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1227                 return -EACCES;
1228         }
1229
1230         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1231                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1232                 return -EACCES;
1233         }
1234
1235         return 0;
1236 }
1237
1238 static struct timespec *osd_inode_time(const struct lu_env *env,
1239                                        struct inode *inode, __u64 seconds)
1240 {
1241         struct osd_thread_info *oti = osd_oti_get(env);
1242         struct timespec        *t   = &oti->oti_time;
1243
1244         t->tv_sec  = seconds;
1245         t->tv_nsec = 0;
1246         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1247         return t;
1248 }
1249
1250
1251 static void osd_inode_getattr(const struct lu_env *env,
1252                               struct inode *inode, struct lu_attr *attr)
1253 {
1254         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1255                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1256                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1257
1258         attr->la_atime      = LTIME_S(inode->i_atime);
1259         attr->la_mtime      = LTIME_S(inode->i_mtime);
1260         attr->la_ctime      = LTIME_S(inode->i_ctime);
1261         attr->la_mode       = inode->i_mode;
1262         attr->la_size       = i_size_read(inode);
1263         attr->la_blocks     = inode->i_blocks;
1264         attr->la_uid        = inode->i_uid;
1265         attr->la_gid        = inode->i_gid;
1266         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1267         attr->la_nlink      = inode->i_nlink;
1268         attr->la_rdev       = inode->i_rdev;
1269         attr->la_blksize    = ll_inode_blksize(inode);
1270         attr->la_blkbits    = inode->i_blkbits;
1271 }
1272
1273 static int osd_attr_get(const struct lu_env *env,
1274                         struct dt_object *dt,
1275                         struct lu_attr *attr,
1276                         struct lustre_capa *capa)
1277 {
1278         struct osd_object *obj = osd_dt_obj(dt);
1279
1280         LASSERT(dt_object_exists(dt));
1281         LINVRNT(osd_invariant(obj));
1282
1283         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1284                 return -EACCES;
1285
1286         cfs_spin_lock(&obj->oo_guard);
1287         osd_inode_getattr(env, obj->oo_inode, attr);
1288         cfs_spin_unlock(&obj->oo_guard);
1289         return 0;
1290 }
1291
1292 static int osd_declare_attr_set(const struct lu_env *env,
1293                                 struct dt_object *dt,
1294                                 const struct lu_attr *attr,
1295                                 struct thandle *handle)
1296 {
1297         struct osd_thandle *oh;
1298         struct osd_object *obj;
1299
1300         LASSERT(dt != NULL);
1301         LASSERT(handle != NULL);
1302
1303         obj = osd_dt_obj(dt);
1304         LASSERT(osd_invariant(obj));
1305
1306         oh = container_of0(handle, struct osd_thandle, ot_super);
1307         LASSERT(oh->ot_handle == NULL);
1308
1309         OSD_DECLARE_OP(oh, attr_set);
1310         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1311
1312         if (attr && attr->la_valid & LA_UID) {
1313                 if (obj->oo_inode)
1314                         osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1315                                         obj->oo_inode);
1316                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1317         }
1318         if (attr && attr->la_valid & LA_GID) {
1319                 if (obj->oo_inode)
1320                         osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1321                                         obj->oo_inode);
1322                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1323         }
1324
1325         return 0;
1326 }
1327
1328 static int osd_inode_setattr(const struct lu_env *env,
1329                              struct inode *inode, const struct lu_attr *attr)
1330 {
1331         __u64 bits;
1332
1333         bits = attr->la_valid;
1334
1335         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1336
1337 #ifdef HAVE_QUOTA_SUPPORT
1338         if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1339             (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1340                 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1341                 struct iattr iattr;
1342                 int rc;
1343
1344                 iattr.ia_valid = 0;
1345                 if (bits & LA_UID)
1346                         iattr.ia_valid |= ATTR_UID;
1347                 if (bits & LA_GID)
1348                         iattr.ia_valid |= ATTR_GID;
1349                 iattr.ia_uid = attr->la_uid;
1350                 iattr.ia_gid = attr->la_gid;
1351                 osd_push_ctxt(env, save);
1352                 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1353                 osd_pop_ctxt(save);
1354                 if (rc != 0)
1355                         return rc;
1356         }
1357 #endif
1358
1359         if (bits & LA_ATIME)
1360                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1361         if (bits & LA_CTIME)
1362                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1363         if (bits & LA_MTIME)
1364                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1365         if (bits & LA_SIZE) {
1366                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1367                 i_size_write(inode, attr->la_size);
1368         }
1369
1370 #if 0
1371         /* OSD should not change "i_blocks" which is used by quota.
1372          * "i_blocks" should be changed by ldiskfs only. */
1373         if (bits & LA_BLOCKS)
1374                 inode->i_blocks = attr->la_blocks;
1375 #endif
1376         if (bits & LA_MODE)
1377                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1378                         (attr->la_mode & ~S_IFMT);
1379         if (bits & LA_UID)
1380                 inode->i_uid    = attr->la_uid;
1381         if (bits & LA_GID)
1382                 inode->i_gid    = attr->la_gid;
1383         if (bits & LA_NLINK)
1384                 inode->i_nlink  = attr->la_nlink;
1385         if (bits & LA_RDEV)
1386                 inode->i_rdev   = attr->la_rdev;
1387
1388         if (bits & LA_FLAGS) {
1389                 /* always keep S_NOCMTIME */
1390                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1391                                  S_NOCMTIME;
1392         }
1393         return 0;
1394 }
1395
1396 static int osd_attr_set(const struct lu_env *env,
1397                         struct dt_object *dt,
1398                         const struct lu_attr *attr,
1399                         struct thandle *handle,
1400                         struct lustre_capa *capa)
1401 {
1402         struct osd_object *obj = osd_dt_obj(dt);
1403         int rc;
1404
1405         LASSERT(handle != NULL);
1406         LASSERT(dt_object_exists(dt));
1407         LASSERT(osd_invariant(obj));
1408
1409         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1410                 return -EACCES;
1411
1412         OSD_EXEC_OP(handle, attr_set);
1413
1414         cfs_spin_lock(&obj->oo_guard);
1415         rc = osd_inode_setattr(env, obj->oo_inode, attr);
1416         cfs_spin_unlock(&obj->oo_guard);
1417
1418         if (!rc)
1419                 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1420         return rc;
1421 }
1422
1423 /*
1424  * Object creation.
1425  *
1426  * XXX temporary solution.
1427  */
1428 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1429                           struct lu_attr *attr, struct thandle *th)
1430 {
1431         return 0;
1432 }
1433
1434 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1435                            struct lu_attr *attr, struct thandle *th)
1436 {
1437         osd_object_init0(obj);
1438         if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1439                 unlock_new_inode(obj->oo_inode);
1440         return 0;
1441 }
1442
1443 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1444                                             struct osd_object *obj,
1445                                             const char *name,
1446                                             const int namelen)
1447 {
1448         struct osd_thread_info *info   = osd_oti_get(env);
1449         struct dentry *child_dentry = &info->oti_child_dentry;
1450         struct dentry *obj_dentry = &info->oti_obj_dentry;
1451
1452         obj_dentry->d_inode = obj->oo_inode;
1453         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1454         obj_dentry->d_name.hash = 0;
1455
1456         child_dentry->d_name.hash = 0;
1457         child_dentry->d_parent = obj_dentry;
1458         child_dentry->d_name.name = name;
1459         child_dentry->d_name.len = namelen;
1460         return child_dentry;
1461 }
1462
1463
1464 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1465                       cfs_umode_t mode,
1466                       struct dt_allocation_hint *hint,
1467                       struct thandle *th)
1468 {
1469         int result;
1470         struct osd_device  *osd = osd_obj2dev(obj);
1471         struct osd_thandle *oth;
1472         struct dt_object   *parent;
1473         struct inode       *inode;
1474 #ifdef HAVE_QUOTA_SUPPORT
1475         struct osd_ctxt    *save = &info->oti_ctxt;
1476 #endif
1477
1478         LINVRNT(osd_invariant(obj));
1479         LASSERT(obj->oo_inode == NULL);
1480         LASSERT(obj->oo_hl_head == NULL);
1481
1482         if (S_ISDIR(mode) && ldiskfs_pdo) {
1483                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1484                 if (obj->oo_hl_head == NULL)
1485                         return -ENOMEM;
1486         }
1487
1488         oth = container_of(th, struct osd_thandle, ot_super);
1489         LASSERT(oth->ot_handle->h_transaction != NULL);
1490
1491         if (hint && hint->dah_parent)
1492                 parent = hint->dah_parent;
1493         else
1494                 parent = osd->od_obj_area;
1495
1496 #ifdef HAVE_QUOTA_SUPPORT
1497         osd_push_ctxt(info->oti_env, save);
1498 #endif
1499         inode = ldiskfs_create_inode(oth->ot_handle,
1500                                      parent ?  osd_dt_obj(parent)->oo_inode :
1501                                                osd_sb(osd)->s_root->d_inode,
1502                                      mode);
1503 #ifdef HAVE_QUOTA_SUPPORT
1504         osd_pop_ctxt(save);
1505 #endif
1506         if (!IS_ERR(inode)) {
1507                 /* Do not update file c/mtime in ldiskfs.
1508                  * NB: don't need any lock because no contention at this
1509                  * early stage */
1510                 inode->i_flags |= S_NOCMTIME;
1511                 obj->oo_inode = inode;
1512                 result = 0;
1513         } else {
1514                 if (obj->oo_hl_head != NULL) {
1515                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1516                         obj->oo_hl_head = NULL;
1517                 }
1518                 result = PTR_ERR(inode);
1519         }
1520         LINVRNT(osd_invariant(obj));
1521         return result;
1522 }
1523
1524 enum {
1525         OSD_NAME_LEN = 255
1526 };
1527
1528 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1529                      struct lu_attr *attr,
1530                      struct dt_allocation_hint *hint,
1531                      struct dt_object_format *dof,
1532                      struct thandle *th)
1533 {
1534         int result;
1535         struct osd_thandle *oth;
1536         struct osd_device *osd = osd_obj2dev(obj);
1537         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1538
1539         LASSERT(S_ISDIR(attr->la_mode));
1540
1541         oth = container_of(th, struct osd_thandle, ot_super);
1542         LASSERT(oth->ot_handle->h_transaction != NULL);
1543         result = osd_mkfile(info, obj, mode, hint, th);
1544         if (result == 0 && osd->od_iop_mode == 0) {
1545                 LASSERT(obj->oo_inode != NULL);
1546                 /*
1547                  * XXX uh-oh... call low-level iam function directly.
1548                  */
1549
1550                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1551                                          sizeof (struct osd_fid_pack),
1552                                          oth->ot_handle);
1553         }
1554         return result;
1555 }
1556
1557 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1558                         struct lu_attr *attr,
1559                         struct dt_allocation_hint *hint,
1560                         struct dt_object_format *dof,
1561                         struct thandle *th)
1562 {
1563         int result;
1564         struct osd_thandle *oth;
1565         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1566
1567         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1568
1569         LASSERT(S_ISREG(attr->la_mode));
1570
1571         oth = container_of(th, struct osd_thandle, ot_super);
1572         LASSERT(oth->ot_handle->h_transaction != NULL);
1573
1574         result = osd_mkfile(info, obj, mode, hint, th);
1575         if (result == 0) {
1576                 LASSERT(obj->oo_inode != NULL);
1577                 if (feat->dif_flags & DT_IND_VARKEY)
1578                         result = iam_lvar_create(obj->oo_inode,
1579                                                  feat->dif_keysize_max,
1580                                                  feat->dif_ptrsize,
1581                                                  feat->dif_recsize_max,
1582                                                  oth->ot_handle);
1583                 else
1584                         result = iam_lfix_create(obj->oo_inode,
1585                                                  feat->dif_keysize_max,
1586                                                  feat->dif_ptrsize,
1587                                                  feat->dif_recsize_max,
1588                                                  oth->ot_handle);
1589
1590         }
1591         return result;
1592 }
1593
1594 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1595                      struct lu_attr *attr,
1596                      struct dt_allocation_hint *hint,
1597                      struct dt_object_format *dof,
1598                      struct thandle *th)
1599 {
1600         LASSERT(S_ISREG(attr->la_mode));
1601         return osd_mkfile(info, obj, (attr->la_mode &
1602                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1603 }
1604
1605 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1606                      struct lu_attr *attr,
1607                      struct dt_allocation_hint *hint,
1608                      struct dt_object_format *dof,
1609                      struct thandle *th)
1610 {
1611         LASSERT(S_ISLNK(attr->la_mode));
1612         return osd_mkfile(info, obj, (attr->la_mode &
1613                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1614 }
1615
1616 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1617                      struct lu_attr *attr,
1618                      struct dt_allocation_hint *hint,
1619                      struct dt_object_format *dof,
1620                      struct thandle *th)
1621 {
1622         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1623         int result;
1624
1625         LINVRNT(osd_invariant(obj));
1626         LASSERT(obj->oo_inode == NULL);
1627         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1628                 S_ISFIFO(mode) || S_ISSOCK(mode));
1629
1630         result = osd_mkfile(info, obj, mode, hint, th);
1631         if (result == 0) {
1632                 LASSERT(obj->oo_inode != NULL);
1633                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1634         }
1635         LINVRNT(osd_invariant(obj));
1636         return result;
1637 }
1638
1639 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1640                               struct lu_attr *,
1641                               struct dt_allocation_hint *hint,
1642                               struct dt_object_format *dof,
1643                               struct thandle *);
1644
1645 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1646 {
1647         osd_obj_type_f result;
1648
1649         switch (type) {
1650         case DFT_DIR:
1651                 result = osd_mkdir;
1652                 break;
1653         case DFT_REGULAR:
1654                 result = osd_mkreg;
1655                 break;
1656         case DFT_SYM:
1657                 result = osd_mksym;
1658                 break;
1659         case DFT_NODE:
1660                 result = osd_mknod;
1661                 break;
1662         case DFT_INDEX:
1663                 result = osd_mk_index;
1664                 break;
1665
1666         default:
1667                 LBUG();
1668                 break;
1669         }
1670         return result;
1671 }
1672
1673
1674 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1675                         struct dt_object *parent, cfs_umode_t child_mode)
1676 {
1677         LASSERT(ah);
1678
1679         memset(ah, 0, sizeof(*ah));
1680         ah->dah_parent = parent;
1681         ah->dah_mode = child_mode;
1682 }
1683
1684 /**
1685  * Helper function for osd_object_create()
1686  *
1687  * \retval 0, on success
1688  */
1689 static int __osd_object_create(struct osd_thread_info *info,
1690                                struct osd_object *obj, struct lu_attr *attr,
1691                                struct dt_allocation_hint *hint,
1692                                struct dt_object_format *dof,
1693                                struct thandle *th)
1694 {
1695
1696         int result;
1697
1698         result = osd_create_pre(info, obj, attr, th);
1699         if (result == 0) {
1700                 result = osd_create_type_f(dof->dof_type)(info, obj,
1701                                            attr, hint, dof, th);
1702                 if (result == 0)
1703                         result = osd_create_post(info, obj, attr, th);
1704         }
1705         return result;
1706 }
1707
1708 /**
1709  * Helper function for osd_object_create()
1710  *
1711  * \retval 0, on success
1712  */
1713 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1714                            const struct lu_fid *fid, struct thandle *th)
1715 {
1716         struct osd_thread_info *info = osd_oti_get(env);
1717         struct osd_inode_id    *id   = &info->oti_id;
1718         struct osd_device      *osd  = osd_obj2dev(obj);
1719         struct md_ucred        *uc   = md_ucred(env);
1720
1721         LASSERT(obj->oo_inode != NULL);
1722         LASSERT(uc != NULL);
1723
1724         id->oii_ino = obj->oo_inode->i_ino;
1725         id->oii_gen = obj->oo_inode->i_generation;
1726
1727         return osd_oi_insert(info, osd_fid2oi(osd, fid), fid, id, th,
1728                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1729 }
1730
1731 static int osd_declare_object_create(const struct lu_env *env,
1732                                      struct dt_object *dt,
1733                                      struct lu_attr *attr,
1734                                      struct dt_allocation_hint *hint,
1735                                      struct dt_object_format *dof,
1736                                      struct thandle *handle)
1737 {
1738         struct osd_thandle *oh;
1739
1740         LASSERT(handle != NULL);
1741
1742         oh = container_of0(handle, struct osd_thandle, ot_super);
1743         LASSERT(oh->ot_handle == NULL);
1744
1745         OSD_DECLARE_OP(oh, create);
1746         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1747         /* XXX: So far, only normal fid needs be inserted into the oi,
1748          *      things could be changed later. Revise following code then. */
1749         if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1750                 OSD_DECLARE_OP(oh, insert);
1751                 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1752         }
1753         /* If this is directory, then we expect . and .. to be inserted as
1754          * well. The one directory block always needs to be created for the
1755          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1756          * block), there is no danger of needing a tree for the first block.
1757          */
1758         if (attr && S_ISDIR(attr->la_mode)) {
1759                 OSD_DECLARE_OP(oh, insert);
1760                 OSD_DECLARE_OP(oh, insert);
1761                 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1762         }
1763
1764         if (attr) {
1765                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1766                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1767         }
1768         return 0;
1769 }
1770
1771 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1772                              struct lu_attr *attr,
1773                              struct dt_allocation_hint *hint,
1774                              struct dt_object_format *dof,
1775                              struct thandle *th)
1776 {
1777         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1778         struct osd_object      *obj    = osd_dt_obj(dt);
1779         struct osd_thread_info *info   = osd_oti_get(env);
1780         int result;
1781
1782         ENTRY;
1783
1784         LINVRNT(osd_invariant(obj));
1785         LASSERT(!dt_object_exists(dt));
1786         LASSERT(osd_write_locked(env, obj));
1787         LASSERT(th != NULL);
1788
1789         OSD_EXEC_OP(th, create);
1790
1791         result = __osd_object_create(info, obj, attr, hint, dof, th);
1792         if (result == 0)
1793                 result = __osd_oi_insert(env, obj, fid, th);
1794
1795         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1796         LASSERT(osd_invariant(obj));
1797         RETURN(result);
1798 }
1799
1800 /**
1801  * Called to destroy on-disk representation of the object
1802  *
1803  * Concurrency: must be locked
1804  */
1805 static int osd_declare_object_destroy(const struct lu_env *env,
1806                                       struct dt_object *dt,
1807                                       struct thandle *th)
1808 {
1809         struct osd_object  *obj = osd_dt_obj(dt);
1810         struct inode       *inode = obj->oo_inode;
1811         struct osd_thandle *oh;
1812
1813         ENTRY;
1814
1815         oh = container_of0(th, struct osd_thandle, ot_super);
1816         LASSERT(oh->ot_handle == NULL);
1817         LASSERT(inode);
1818
1819         OSD_DECLARE_OP(oh, destroy);
1820         OSD_DECLARE_OP(oh, delete);
1821         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1822         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1823
1824         osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1825         osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1826
1827         RETURN(0);
1828 }
1829
1830 static int osd_object_destroy(const struct lu_env *env,
1831                               struct dt_object *dt,
1832                               struct thandle *th)
1833 {
1834         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
1835         struct osd_object      *obj = osd_dt_obj(dt);
1836         struct inode           *inode = obj->oo_inode;
1837         struct osd_device      *osd = osd_obj2dev(obj);
1838         struct osd_thandle     *oh;
1839         int                     result;
1840         ENTRY;
1841
1842         oh = container_of0(th, struct osd_thandle, ot_super);
1843         LASSERT(oh->ot_handle);
1844         LASSERT(inode);
1845         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1846
1847         if (S_ISDIR(inode->i_mode)) {
1848                 LASSERT(osd_inode_unlinked(inode) ||
1849                         inode->i_nlink == 1);
1850                 cfs_spin_lock(&obj->oo_guard);
1851                 inode->i_nlink = 0;
1852                 cfs_spin_unlock(&obj->oo_guard);
1853                 inode->i_sb->s_op->dirty_inode(inode);
1854         } else {
1855                 LASSERT(osd_inode_unlinked(inode));
1856         }
1857
1858         OSD_EXEC_OP(th, destroy);
1859
1860         result = osd_oi_delete(osd_oti_get(env),
1861                                osd_fid2oi(osd, fid), fid, th);
1862
1863         /* XXX: add to ext3 orphan list */
1864         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1865
1866         /* not needed in the cache anymore */
1867         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1868
1869         RETURN(0);
1870 }
1871
1872 /**
1873  * Helper function for osd_xattr_set()
1874  */
1875 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1876                            const struct lu_buf *buf, const char *name, int fl)
1877 {
1878         struct osd_object      *obj      = osd_dt_obj(dt);
1879         struct inode           *inode    = obj->oo_inode;
1880         struct osd_thread_info *info     = osd_oti_get(env);
1881         struct dentry          *dentry   = &info->oti_child_dentry;
1882         int                     fs_flags = 0;
1883         int                     rc;
1884
1885         LASSERT(dt_object_exists(dt));
1886         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1887         LASSERT(osd_write_locked(env, obj));
1888
1889         if (fl & LU_XATTR_REPLACE)
1890                 fs_flags |= XATTR_REPLACE;
1891
1892         if (fl & LU_XATTR_CREATE)
1893                 fs_flags |= XATTR_CREATE;
1894
1895         dentry->d_inode = inode;
1896         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1897                                    buf->lb_len, fs_flags);
1898         return rc;
1899 }
1900
1901 /**
1902  * Put the fid into lustre_mdt_attrs, and then place the structure
1903  * inode's ea. This fid should not be altered during the life time
1904  * of the inode.
1905  *
1906  * \retval +ve, on success
1907  * \retval -ve, on error
1908  *
1909  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1910  */
1911 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1912                           const struct lu_fid *fid)
1913 {
1914         struct osd_thread_info  *info      = osd_oti_get(env);
1915         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1916
1917         lustre_lma_init(mdt_attrs, fid);
1918         lustre_lma_swab(mdt_attrs);
1919         return __osd_xattr_set(env, dt,
1920                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1921                                XATTR_NAME_LMA, LU_XATTR_CREATE);
1922
1923 }
1924
1925 /**
1926  * Helper function to form igif
1927  */
1928 static inline void osd_igif_get(const struct lu_env *env, struct inode  *inode,
1929                                 struct lu_fid *fid)
1930 {
1931         LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1932 }
1933
1934 /**
1935  * Helper function to pack the fid, ldiskfs stores fid in packed format.
1936  */
1937 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1938                   struct lu_fid *befider)
1939 {
1940         fid_cpu_to_be(befider, (struct lu_fid *)fid);
1941         memcpy(pack->fp_area, befider, sizeof(*befider));
1942         pack->fp_len =  sizeof(*befider) + 1;
1943 }
1944
1945 /**
1946  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1947  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1948  * To have compatilibility with 1.8 ldiskfs driver we need to have
1949  * magic number at start of fid data.
1950  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1951  * its inmemory API.
1952  */
1953 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1954                                   const struct dt_rec *fid)
1955 {
1956         param->edp_magic = LDISKFS_LUFID_MAGIC;
1957         param->edp_len =  sizeof(struct lu_fid) + 1;
1958
1959         fid_cpu_to_be((struct lu_fid *)param->edp_data,
1960                       (struct lu_fid *)fid);
1961 }
1962
1963 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1964 {
1965         int result;
1966
1967         result = 0;
1968         switch (pack->fp_len) {
1969         case sizeof *fid + 1:
1970                 memcpy(fid, pack->fp_area, sizeof *fid);
1971                 fid_be_to_cpu(fid, fid);
1972                 break;
1973         default:
1974                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1975                 result = -EIO;
1976         }
1977         return result;
1978 }
1979
1980 /**
1981  * Try to read the fid from inode ea into dt_rec, if return value
1982  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1983  *
1984  * \param fid object fid.
1985  *
1986  * \retval 0 on success
1987  */
1988 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1989                           __u32 ino, struct lu_fid *fid)
1990 {
1991         struct osd_thread_info  *info      = osd_oti_get(env);
1992         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1993         struct lu_device        *ldev   = obj->oo_dt.do_lu.lo_dev;
1994         struct dentry           *dentry = &info->oti_child_dentry;
1995         struct osd_inode_id     *id     = &info->oti_id;
1996         struct osd_device       *dev;
1997         struct inode            *inode;
1998         int                      rc;
1999
2000         ENTRY;
2001         dev  = osd_dev(ldev);
2002
2003         id->oii_ino = ino;
2004         id->oii_gen = OSD_OII_NOGEN;
2005
2006         inode = osd_iget(info, dev, id);
2007         if (IS_ERR(inode)) {
2008                 rc = PTR_ERR(inode);
2009                 GOTO(out,rc);
2010         }
2011         dentry->d_inode = inode;
2012
2013         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2014         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2015                                    sizeof *mdt_attrs);
2016
2017         /* Check LMA compatibility */
2018         if (rc > 0 &&
2019             (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2020                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2021                       inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2022                       ~LMA_INCOMPAT_SUPP);
2023                 return -ENOSYS;
2024         }
2025
2026         if (rc > 0) {
2027                 lustre_lma_swab(mdt_attrs);
2028                 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2029                 rc = 0;
2030         } else if (rc == -ENODATA) {
2031                 osd_igif_get(env, inode, fid);
2032                 rc = 0;
2033         }
2034         iput(inode);
2035 out:
2036         RETURN(rc);
2037 }
2038
2039 /**
2040  * OSD layer object create function for interoperability mode (b11826).
2041  * This is mostly similar to osd_object_create(). Only difference being, fid is
2042  * inserted into inode ea here.
2043  *
2044  * \retval   0, on success
2045  * \retval -ve, on error
2046  */
2047 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2048                                 struct lu_attr *attr,
2049                                 struct dt_allocation_hint *hint,
2050                                 struct dt_object_format *dof,
2051                                 struct thandle *th)
2052 {
2053         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2054         struct osd_object      *obj    = osd_dt_obj(dt);
2055         struct osd_thread_info *info   = osd_oti_get(env);
2056         int                     result;
2057
2058         ENTRY;
2059
2060         LASSERT(osd_invariant(obj));
2061         LASSERT(!dt_object_exists(dt));
2062         LASSERT(osd_write_locked(env, obj));
2063         LASSERT(th != NULL);
2064
2065         OSD_EXEC_OP(th, create);
2066
2067         result = __osd_object_create(info, obj, attr, hint, dof, th);
2068
2069         /* objects under osd root shld have igif fid, so dont add fid EA */
2070         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2071                 result = osd_ea_fid_set(env, dt, fid);
2072
2073         if (result == 0)
2074                 result = __osd_oi_insert(env, obj, fid, th);
2075
2076         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2077         LINVRNT(osd_invariant(obj));
2078         RETURN(result);
2079 }
2080
2081 static int osd_declare_object_ref_add(const struct lu_env *env,
2082                                       struct dt_object *dt,
2083                                       struct thandle *handle)
2084 {
2085         struct osd_thandle *oh;
2086
2087         /* it's possible that object doesn't exist yet */
2088         LASSERT(handle != NULL);
2089
2090         oh = container_of0(handle, struct osd_thandle, ot_super);
2091         LASSERT(oh->ot_handle == NULL);
2092
2093         OSD_DECLARE_OP(oh, ref_add);
2094         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2095
2096         return 0;
2097 }
2098
2099 /*
2100  * Concurrency: @dt is write locked.
2101  */
2102 static int osd_object_ref_add(const struct lu_env *env,
2103                               struct dt_object *dt, struct thandle *th)
2104 {
2105         struct osd_object *obj = osd_dt_obj(dt);
2106         struct inode      *inode = obj->oo_inode;
2107
2108         LINVRNT(osd_invariant(obj));
2109         LASSERT(dt_object_exists(dt));
2110         LASSERT(osd_write_locked(env, obj));
2111         LASSERT(th != NULL);
2112
2113         OSD_EXEC_OP(th, ref_add);
2114
2115         /*
2116          * DIR_NLINK feature is set for compatibility reasons if:
2117          * 1) nlinks > LDISKFS_LINK_MAX, or
2118          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2119          *
2120          * It is easier to always set this flag (rather than check and set),
2121          * since it has less overhead, and the superblock will be dirtied
2122          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2123          * do not actually care whether this flag is set or not.
2124          */
2125         cfs_spin_lock(&obj->oo_guard);
2126         inode->i_nlink++;
2127         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2128                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2129                     inode->i_nlink == 2)
2130                         inode->i_nlink = 1;
2131         }
2132         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2133         cfs_spin_unlock(&obj->oo_guard);
2134         inode->i_sb->s_op->dirty_inode(inode);
2135         LINVRNT(osd_invariant(obj));
2136
2137         return 0;
2138 }
2139
2140 static int osd_declare_object_ref_del(const struct lu_env *env,
2141                                       struct dt_object *dt,
2142                                       struct thandle *handle)
2143 {
2144         struct osd_thandle *oh;
2145
2146         LASSERT(dt_object_exists(dt));
2147         LASSERT(handle != NULL);
2148
2149         oh = container_of0(handle, struct osd_thandle, ot_super);
2150         LASSERT(oh->ot_handle == NULL);
2151
2152         OSD_DECLARE_OP(oh, ref_del);
2153         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2154
2155         return 0;
2156 }
2157
2158 /*
2159  * Concurrency: @dt is write locked.
2160  */
2161 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2162                               struct thandle *th)
2163 {
2164         struct osd_object *obj = osd_dt_obj(dt);
2165         struct inode      *inode = obj->oo_inode;
2166
2167         LINVRNT(osd_invariant(obj));
2168         LASSERT(dt_object_exists(dt));
2169         LASSERT(osd_write_locked(env, obj));
2170         LASSERT(th != NULL);
2171
2172         OSD_EXEC_OP(th, ref_del);
2173
2174         cfs_spin_lock(&obj->oo_guard);
2175         LASSERT(inode->i_nlink > 0);
2176         inode->i_nlink--;
2177         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2178          * then the nlink count is 1. Don't let it be set to 0 or the directory
2179          * inode will be deleted incorrectly. */
2180         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2181                 inode->i_nlink++;
2182         cfs_spin_unlock(&obj->oo_guard);
2183         inode->i_sb->s_op->dirty_inode(inode);
2184         LINVRNT(osd_invariant(obj));
2185
2186         return 0;
2187 }
2188
2189 /*
2190  * Get the 64-bit version for an inode.
2191  */
2192 static int osd_object_version_get(const struct lu_env *env,
2193                                   struct dt_object *dt, dt_obj_version_t *ver)
2194 {
2195         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2196
2197         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2198                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2199         *ver = LDISKFS_I(inode)->i_fs_version;
2200         return 0;
2201 }
2202
2203 /*
2204  * Concurrency: @dt is read locked.
2205  */
2206 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2207                          struct lu_buf *buf, const char *name,
2208                          struct lustre_capa *capa)
2209 {
2210         struct osd_object      *obj    = osd_dt_obj(dt);
2211         struct inode           *inode  = obj->oo_inode;
2212         struct osd_thread_info *info   = osd_oti_get(env);
2213         struct dentry          *dentry = &info->oti_obj_dentry;
2214
2215         /* version get is not real XATTR but uses xattr API */
2216         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2217                 /* for version we are just using xattr API but change inode
2218                  * field instead */
2219                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2220                 osd_object_version_get(env, dt, buf->lb_buf);
2221                 return sizeof(dt_obj_version_t);
2222         }
2223
2224         LASSERT(dt_object_exists(dt));
2225         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2226         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2227
2228         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2229                 return -EACCES;
2230
2231         dentry->d_inode = inode;
2232         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2233 }
2234
2235
2236 static int osd_declare_xattr_set(const struct lu_env *env,
2237                                  struct dt_object *dt,
2238                                  const struct lu_buf *buf, const char *name,
2239                                  int fl, struct thandle *handle)
2240 {
2241         struct osd_thandle *oh;
2242
2243         LASSERT(handle != NULL);
2244
2245         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2246                 /* no credits for version */
2247                 return 0;
2248         }
2249
2250         oh = container_of0(handle, struct osd_thandle, ot_super);
2251         LASSERT(oh->ot_handle == NULL);
2252
2253         OSD_DECLARE_OP(oh, xattr_set);
2254         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2255
2256         return 0;
2257 }
2258
2259 /*
2260  * Set the 64-bit version for object
2261  */
2262 static void osd_object_version_set(const struct lu_env *env,
2263                                    struct dt_object *dt,
2264                                    dt_obj_version_t *new_version)
2265 {
2266         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2267
2268         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2269                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2270
2271         LDISKFS_I(inode)->i_fs_version = *new_version;
2272         /** Version is set after all inode operations are finished,
2273          *  so we should mark it dirty here */
2274         inode->i_sb->s_op->dirty_inode(inode);
2275 }
2276
2277 /*
2278  * Concurrency: @dt is write locked.
2279  */
2280 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2281                          const struct lu_buf *buf, const char *name, int fl,
2282                          struct thandle *handle, struct lustre_capa *capa)
2283 {
2284         LASSERT(handle != NULL);
2285
2286         /* version set is not real XATTR */
2287         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2288                 /* for version we are just using xattr API but change inode
2289                  * field instead */
2290                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2291                 osd_object_version_set(env, dt, buf->lb_buf);
2292                 return sizeof(dt_obj_version_t);
2293         }
2294
2295         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2296                 return -EACCES;
2297
2298         OSD_EXEC_OP(handle, xattr_set);
2299         return __osd_xattr_set(env, dt, buf, name, fl);
2300 }
2301
2302 /*
2303  * Concurrency: @dt is read locked.
2304  */
2305 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2306                           struct lu_buf *buf, struct lustre_capa *capa)
2307 {
2308         struct osd_object      *obj    = osd_dt_obj(dt);
2309         struct inode           *inode  = obj->oo_inode;
2310         struct osd_thread_info *info   = osd_oti_get(env);
2311         struct dentry          *dentry = &info->oti_obj_dentry;
2312
2313         LASSERT(dt_object_exists(dt));
2314         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2315         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2316
2317         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2318                 return -EACCES;
2319
2320         dentry->d_inode = inode;
2321         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2322 }
2323
2324 static int osd_declare_xattr_del(const struct lu_env *env,
2325                                  struct dt_object *dt, const char *name,
2326                                  struct thandle *handle)
2327 {
2328         struct osd_thandle *oh;
2329
2330         LASSERT(dt_object_exists(dt));
2331         LASSERT(handle != NULL);
2332
2333         oh = container_of0(handle, struct osd_thandle, ot_super);
2334         LASSERT(oh->ot_handle == NULL);
2335
2336         OSD_DECLARE_OP(oh, xattr_set);
2337         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2338
2339         return 0;
2340 }
2341
2342 /*
2343  * Concurrency: @dt is write locked.
2344  */
2345 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2346                          const char *name, struct thandle *handle,
2347                          struct lustre_capa *capa)
2348 {
2349         struct osd_object      *obj    = osd_dt_obj(dt);
2350         struct inode           *inode  = obj->oo_inode;
2351         struct osd_thread_info *info   = osd_oti_get(env);
2352         struct dentry          *dentry = &info->oti_obj_dentry;
2353         int                     rc;
2354
2355         LASSERT(dt_object_exists(dt));
2356         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2357         LASSERT(osd_write_locked(env, obj));
2358         LASSERT(handle != NULL);
2359
2360         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2361                 return -EACCES;
2362
2363         OSD_EXEC_OP(handle, xattr_set);
2364
2365         dentry->d_inode = inode;
2366         rc = inode->i_op->removexattr(dentry, name);
2367         return rc;
2368 }
2369
2370 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2371                                      struct dt_object *dt,
2372                                      struct lustre_capa *old,
2373                                      __u64 opc)
2374 {
2375         struct osd_thread_info *info = osd_oti_get(env);
2376         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2377         struct osd_object *obj = osd_dt_obj(dt);
2378         struct osd_device *dev = osd_obj2dev(obj);
2379         struct lustre_capa_key *key = &info->oti_capa_key;
2380         struct lustre_capa *capa = &info->oti_capa;
2381         struct obd_capa *oc;
2382         struct md_capainfo *ci;
2383         int rc;
2384         ENTRY;
2385
2386         if (!dev->od_fl_capa)
2387                 RETURN(ERR_PTR(-ENOENT));
2388
2389         LASSERT(dt_object_exists(dt));
2390         LINVRNT(osd_invariant(obj));
2391
2392         /* renewal sanity check */
2393         if (old && osd_object_auth(env, dt, old, opc))
2394                 RETURN(ERR_PTR(-EACCES));
2395
2396         ci = md_capainfo(env);
2397         if (unlikely(!ci))
2398                 RETURN(ERR_PTR(-ENOENT));
2399
2400         switch (ci->mc_auth) {
2401         case LC_ID_NONE:
2402                 RETURN(NULL);
2403         case LC_ID_PLAIN:
2404                 capa->lc_uid = obj->oo_inode->i_uid;
2405                 capa->lc_gid = obj->oo_inode->i_gid;
2406                 capa->lc_flags = LC_ID_PLAIN;
2407                 break;
2408         case LC_ID_CONVERT: {
2409                 __u32 d[4], s[4];
2410
2411                 s[0] = obj->oo_inode->i_uid;
2412                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2413                 s[2] = obj->oo_inode->i_gid;
2414                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2415                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2416                 if (unlikely(rc))
2417                         RETURN(ERR_PTR(rc));
2418
2419                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2420                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2421                 capa->lc_flags = LC_ID_CONVERT;
2422                 break;
2423         }
2424         default:
2425                 RETURN(ERR_PTR(-EINVAL));
2426         }
2427
2428         capa->lc_fid = *fid;
2429         capa->lc_opc = opc;
2430         capa->lc_flags |= dev->od_capa_alg << 24;
2431         capa->lc_timeout = dev->od_capa_timeout;
2432         capa->lc_expiry = 0;
2433
2434         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2435         if (oc) {
2436                 LASSERT(!capa_is_expired(oc));
2437                 RETURN(oc);
2438         }
2439
2440         cfs_spin_lock(&capa_lock);
2441         *key = dev->od_capa_keys[1];
2442         cfs_spin_unlock(&capa_lock);
2443
2444         capa->lc_keyid = key->lk_keyid;
2445         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2446
2447         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2448         if (rc) {
2449                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2450                 RETURN(ERR_PTR(rc));
2451         }
2452
2453         oc = capa_add(dev->od_capa_hash, capa);
2454         RETURN(oc);
2455 }
2456
2457 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2458 {
2459         struct osd_object      *obj    = osd_dt_obj(dt);
2460         struct inode           *inode  = obj->oo_inode;
2461         struct osd_thread_info *info   = osd_oti_get(env);
2462         struct dentry          *dentry = &info->oti_obj_dentry;
2463         struct file            *file   = &info->oti_file;
2464         int                     rc;
2465
2466         ENTRY;
2467
2468         dentry->d_inode = inode;
2469         file->f_dentry = dentry;
2470         file->f_mapping = inode->i_mapping;
2471         file->f_op = inode->i_fop;
2472         LOCK_INODE_MUTEX(inode);
2473         rc = file->f_op->fsync(file, dentry, 0);
2474         UNLOCK_INODE_MUTEX(inode);
2475         RETURN(rc);
2476 }
2477
2478 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2479                         void **data)
2480 {
2481         struct osd_object *obj = osd_dt_obj(dt);
2482         ENTRY;
2483
2484         *data = (void *)obj->oo_inode;
2485         RETURN(0);
2486 }
2487
2488 /*
2489  * Index operations.
2490  */
2491
2492 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2493                            const struct dt_index_features *feat)
2494 {
2495         struct iam_descr *descr;
2496
2497         if (osd_object_is_root(o))
2498                 return feat == &dt_directory_features;
2499
2500         LASSERT(o->oo_dir != NULL);
2501
2502         descr = o->oo_dir->od_container.ic_descr;
2503         if (feat == &dt_directory_features) {
2504                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2505                         return 1;
2506                 else
2507                         return 0;
2508         } else {
2509                 return
2510                         feat->dif_keysize_min <= descr->id_key_size &&
2511                         descr->id_key_size <= feat->dif_keysize_max &&
2512                         feat->dif_recsize_min <= descr->id_rec_size &&
2513                         descr->id_rec_size <= feat->dif_recsize_max &&
2514                         !(feat->dif_flags & (DT_IND_VARKEY |
2515                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2516                         ergo(feat->dif_flags & DT_IND_UPDATE,
2517                              1 /* XXX check that object (and file system) is
2518                                 * writable */);
2519         }
2520 }
2521
2522 static int osd_iam_container_init(const struct lu_env *env,
2523                                   struct osd_object *obj,
2524                                   struct osd_directory *dir)
2525 {
2526         struct iam_container *bag = &dir->od_container;
2527         int result;
2528
2529         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2530         if (result != 0)
2531                 return result;
2532
2533         result = iam_container_setup(bag);
2534         if (result != 0)
2535                 goto out;
2536
2537         if (osd_obj2dev(obj)->od_iop_mode) {
2538                 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2539
2540                 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2541                                                 ptr, 0, &result);
2542         }
2543
2544  out:
2545         if (result == 0)
2546                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2547         else
2548                 iam_container_fini(bag);
2549
2550         return result;
2551 }
2552
2553
2554 /*
2555  * Concurrency: no external locking is necessary.
2556  */
2557 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2558                          const struct dt_index_features *feat)
2559 {
2560         int result;
2561         int ea_dir = 0;
2562         struct osd_object *obj = osd_dt_obj(dt);
2563         struct osd_device *osd = osd_obj2dev(obj);
2564
2565         LINVRNT(osd_invariant(obj));
2566         LASSERT(dt_object_exists(dt));
2567
2568         if (osd_object_is_root(obj)) {
2569                 dt->do_index_ops = &osd_index_ea_ops;
2570                 result = 0;
2571         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2572                 dt->do_index_ops = &osd_index_ea_ops;
2573                 if (S_ISDIR(obj->oo_inode->i_mode))
2574                         result = 0;
2575                 else
2576                         result = -ENOTDIR;
2577                 ea_dir = 1;
2578         } else if (!osd_has_index(obj)) {
2579                 struct osd_directory *dir;
2580
2581                 OBD_ALLOC_PTR(dir);
2582                 if (dir != NULL) {
2583
2584                         cfs_spin_lock(&obj->oo_guard);
2585                         if (obj->oo_dir == NULL)
2586                                 obj->oo_dir = dir;
2587                         else
2588                                 /*
2589                                  * Concurrent thread allocated container data.
2590                                  */
2591                                 OBD_FREE_PTR(dir);
2592                         cfs_spin_unlock(&obj->oo_guard);
2593                         /*
2594                          * Now, that we have container data, serialize its
2595                          * initialization.
2596                          */
2597                         cfs_down_write(&obj->oo_ext_idx_sem);
2598                         /*
2599                          * recheck under lock.
2600                          */
2601                         if (!osd_has_index(obj))
2602                                 result = osd_iam_container_init(env, obj, dir);
2603                         else
2604                                 result = 0;
2605                         cfs_up_write(&obj->oo_ext_idx_sem);
2606                 } else {
2607                         result = -ENOMEM;
2608                 }
2609         } else {
2610                 result = 0;
2611         }
2612
2613         if (result == 0 && ea_dir == 0) {
2614                 if (!osd_iam_index_probe(env, obj, feat))
2615                         result = -ENOTDIR;
2616         }
2617         LINVRNT(osd_invariant(obj));
2618
2619         return result;
2620 }
2621
2622 static const struct dt_object_operations osd_obj_ops = {
2623         .do_read_lock         = osd_object_read_lock,
2624         .do_write_lock        = osd_object_write_lock,
2625         .do_read_unlock       = osd_object_read_unlock,
2626         .do_write_unlock      = osd_object_write_unlock,
2627         .do_write_locked      = osd_object_write_locked,
2628         .do_attr_get          = osd_attr_get,
2629         .do_declare_attr_set  = osd_declare_attr_set,
2630         .do_attr_set          = osd_attr_set,
2631         .do_ah_init           = osd_ah_init,
2632         .do_declare_create    = osd_declare_object_create,
2633         .do_create            = osd_object_create,
2634         .do_declare_destroy   = osd_declare_object_destroy,
2635         .do_destroy           = osd_object_destroy,
2636         .do_index_try         = osd_index_try,
2637         .do_declare_ref_add   = osd_declare_object_ref_add,
2638         .do_ref_add           = osd_object_ref_add,
2639         .do_declare_ref_del   = osd_declare_object_ref_del,
2640         .do_ref_del           = osd_object_ref_del,
2641         .do_xattr_get         = osd_xattr_get,
2642         .do_declare_xattr_set = osd_declare_xattr_set,
2643         .do_xattr_set         = osd_xattr_set,
2644         .do_declare_xattr_del = osd_declare_xattr_del,
2645         .do_xattr_del         = osd_xattr_del,
2646         .do_xattr_list        = osd_xattr_list,
2647         .do_capa_get          = osd_capa_get,
2648         .do_object_sync       = osd_object_sync,
2649         .do_data_get          = osd_data_get,
2650 };
2651
2652 /**
2653  * dt_object_operations for interoperability mode
2654  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2655  */
2656 static const struct dt_object_operations osd_obj_ea_ops = {
2657         .do_read_lock         = osd_object_read_lock,
2658         .do_write_lock        = osd_object_write_lock,
2659         .do_read_unlock       = osd_object_read_unlock,
2660         .do_write_unlock      = osd_object_write_unlock,
2661         .do_write_locked      = osd_object_write_locked,
2662         .do_attr_get          = osd_attr_get,
2663         .do_declare_attr_set  = osd_declare_attr_set,
2664         .do_attr_set          = osd_attr_set,
2665         .do_ah_init           = osd_ah_init,
2666         .do_declare_create    = osd_declare_object_create,
2667         .do_create            = osd_object_ea_create,
2668         .do_declare_destroy   = osd_declare_object_destroy,
2669         .do_destroy           = osd_object_destroy,
2670         .do_index_try         = osd_index_try,
2671         .do_declare_ref_add   = osd_declare_object_ref_add,
2672         .do_ref_add           = osd_object_ref_add,
2673         .do_declare_ref_del   = osd_declare_object_ref_del,
2674         .do_ref_del           = osd_object_ref_del,
2675         .do_xattr_get         = osd_xattr_get,
2676         .do_declare_xattr_set = osd_declare_xattr_set,
2677         .do_xattr_set         = osd_xattr_set,
2678         .do_declare_xattr_del = osd_declare_xattr_del,
2679         .do_xattr_del         = osd_xattr_del,
2680         .do_xattr_list        = osd_xattr_list,
2681         .do_capa_get          = osd_capa_get,
2682         .do_object_sync       = osd_object_sync,
2683         .do_data_get          = osd_data_get,
2684 };
2685
2686 static int osd_index_declare_iam_delete(const struct lu_env *env,
2687                                         struct dt_object *dt,
2688                                         const struct dt_key *key,
2689                                         struct thandle *handle)
2690 {
2691         struct osd_thandle    *oh;
2692
2693         oh = container_of0(handle, struct osd_thandle, ot_super);
2694         LASSERT(oh->ot_handle == NULL);
2695
2696         OSD_DECLARE_OP(oh, delete);
2697         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2698
2699         return 0;
2700 }
2701
2702 /**
2703  *      delete a (key, value) pair from index \a dt specified by \a key
2704  *
2705  *      \param  dt      osd index object
2706  *      \param  key     key for index
2707  *      \param  rec     record reference
2708  *      \param  handle  transaction handler
2709  *
2710  *      \retval  0  success
2711  *      \retval -ve   failure
2712  */
2713
2714 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2715                                 const struct dt_key *key,
2716                                 struct thandle *handle,
2717                                 struct lustre_capa *capa)
2718 {
2719         struct osd_object     *obj = osd_dt_obj(dt);
2720         struct osd_thandle    *oh;
2721         struct iam_path_descr *ipd;
2722         struct iam_container  *bag = &obj->oo_dir->od_container;
2723         int                    rc;
2724
2725         ENTRY;
2726
2727         LINVRNT(osd_invariant(obj));
2728         LASSERT(dt_object_exists(dt));
2729         LASSERT(bag->ic_object == obj->oo_inode);
2730         LASSERT(handle != NULL);
2731
2732         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2733                 RETURN(-EACCES);
2734
2735         OSD_EXEC_OP(handle, delete);
2736
2737         ipd = osd_idx_ipd_get(env, bag);
2738         if (unlikely(ipd == NULL))
2739                 RETURN(-ENOMEM);
2740
2741         oh = container_of0(handle, struct osd_thandle, ot_super);
2742         LASSERT(oh->ot_handle != NULL);
2743         LASSERT(oh->ot_handle->h_transaction != NULL);
2744
2745         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2746         osd_ipd_put(env, bag, ipd);
2747         LINVRNT(osd_invariant(obj));
2748         RETURN(rc);
2749 }
2750
2751 static int osd_index_declare_ea_delete(const struct lu_env *env,
2752                                        struct dt_object *dt,
2753                                        const struct dt_key *key,
2754                                        struct thandle *handle)
2755 {
2756         struct osd_thandle *oh;
2757
2758         LASSERT(dt_object_exists(dt));
2759         LASSERT(handle != NULL);
2760
2761         oh = container_of0(handle, struct osd_thandle, ot_super);
2762         LASSERT(oh->ot_handle == NULL);
2763
2764         OSD_DECLARE_OP(oh, delete);
2765         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2766
2767         LASSERT(osd_dt_obj(dt)->oo_inode);
2768         osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2769                         osd_dt_obj(dt)->oo_inode);
2770         osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2771                         osd_dt_obj(dt)->oo_inode);
2772
2773         return 0;
2774 }
2775
2776 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2777                                           struct dt_rec *fid)
2778 {
2779         struct osd_fid_pack *rec;
2780         int                  rc = -ENODATA;
2781
2782         if (de->file_type & LDISKFS_DIRENT_LUFID) {
2783                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2784                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2785         }
2786         RETURN(rc);
2787 }
2788
2789 /**
2790  * Index delete function for interoperability mode (b11826).
2791  * It will remove the directory entry added by osd_index_ea_insert().
2792  * This entry is needed to maintain name->fid mapping.
2793  *
2794  * \param key,  key i.e. file entry to be deleted
2795  *
2796  * \retval   0, on success
2797  * \retval -ve, on error
2798  */
2799 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2800                                const struct dt_key *key,
2801                                struct thandle *handle,
2802                                struct lustre_capa *capa)
2803 {
2804         struct osd_object          *obj    = osd_dt_obj(dt);
2805         struct inode               *dir    = obj->oo_inode;
2806         struct dentry              *dentry;
2807         struct osd_thandle         *oh;
2808         struct ldiskfs_dir_entry_2 *de;
2809         struct buffer_head         *bh;
2810         struct htree_lock          *hlock = NULL;
2811         int                         rc;
2812
2813         ENTRY;
2814
2815         LINVRNT(osd_invariant(obj));
2816         LASSERT(dt_object_exists(dt));
2817         LASSERT(handle != NULL);
2818
2819         OSD_EXEC_OP(handle, delete);
2820
2821         oh = container_of(handle, struct osd_thandle, ot_super);
2822         LASSERT(oh->ot_handle != NULL);
2823         LASSERT(oh->ot_handle->h_transaction != NULL);
2824
2825         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2826                 RETURN(-EACCES);
2827
2828         dentry = osd_child_dentry_get(env, obj,
2829                                       (char *)key, strlen((char *)key));
2830
2831         if (obj->oo_hl_head != NULL) {
2832                 hlock = osd_oti_get(env)->oti_hlock;
2833                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2834                                    dir, LDISKFS_HLOCK_DEL);
2835         } else {
2836                 cfs_down_write(&obj->oo_ext_idx_sem);
2837         }
2838
2839         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2840         if (bh) {
2841                 rc = ldiskfs_delete_entry(oh->ot_handle,
2842                                           dir, de, bh);
2843                 brelse(bh);
2844         } else {
2845                 rc = -ENOENT;
2846         }
2847         if (hlock != NULL)
2848                 ldiskfs_htree_unlock(hlock);
2849         else
2850                 cfs_up_write(&obj->oo_ext_idx_sem);
2851
2852         LASSERT(osd_invariant(obj));
2853         RETURN(rc);
2854 }
2855
2856 /**
2857  *      Lookup index for \a key and copy record to \a rec.
2858  *
2859  *      \param  dt      osd index object
2860  *      \param  key     key for index
2861  *      \param  rec     record reference
2862  *
2863  *      \retval  +ve  success : exact mach
2864  *      \retval  0    return record with key not greater than \a key
2865  *      \retval -ve   failure
2866  */
2867 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2868                                 struct dt_rec *rec, const struct dt_key *key,
2869                                 struct lustre_capa *capa)
2870 {
2871         struct osd_object      *obj = osd_dt_obj(dt);
2872         struct iam_path_descr  *ipd;
2873         struct iam_container   *bag = &obj->oo_dir->od_container;
2874         struct osd_thread_info *oti = osd_oti_get(env);
2875         struct iam_iterator    *it = &oti->oti_idx_it;
2876         struct iam_rec         *iam_rec;
2877         int                     rc;
2878
2879         ENTRY;
2880
2881         LASSERT(osd_invariant(obj));
2882         LASSERT(dt_object_exists(dt));
2883         LASSERT(bag->ic_object == obj->oo_inode);
2884
2885         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2886                 RETURN(-EACCES);
2887
2888         ipd = osd_idx_ipd_get(env, bag);
2889         if (IS_ERR(ipd))
2890                 RETURN(-ENOMEM);
2891
2892         /* got ipd now we can start iterator. */
2893         iam_it_init(it, bag, 0, ipd);
2894
2895         rc = iam_it_get(it, (struct iam_key *)key);
2896         if (rc >= 0) {
2897                 if (S_ISDIR(obj->oo_inode->i_mode))
2898                         iam_rec = (struct iam_rec *)oti->oti_ldp;
2899                 else
2900                         iam_rec = (struct iam_rec *) rec;
2901
2902                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2903                 if (S_ISDIR(obj->oo_inode->i_mode))
2904                         osd_fid_unpack((struct lu_fid *) rec,
2905                                        (struct osd_fid_pack *)iam_rec);
2906         }
2907         iam_it_put(it);
2908         iam_it_fini(it);
2909         osd_ipd_put(env, bag, ipd);
2910
2911         LINVRNT(osd_invariant(obj));
2912
2913         RETURN(rc);
2914 }
2915
2916 static int osd_index_declare_iam_insert(const struct lu_env *env,
2917                                         struct dt_object *dt,
2918                                         const struct dt_rec *rec,
2919                                         const struct dt_key *key,
2920                                         struct thandle *handle)
2921 {
2922         struct osd_thandle *oh;
2923
2924         LASSERT(dt_object_exists(dt));
2925         LASSERT(handle != NULL);
2926
2927         oh = container_of0(handle, struct osd_thandle, ot_super);
2928         LASSERT(oh->ot_handle == NULL);
2929
2930         OSD_DECLARE_OP(oh, insert);
2931         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2932
2933         return 0;
2934 }
2935
2936 /**
2937  *      Inserts (key, value) pair in \a dt index object.
2938  *
2939  *      \param  dt      osd index object
2940  *      \param  key     key for index
2941  *      \param  rec     record reference
2942  *      \param  th      transaction handler
2943  *
2944  *      \retval  0  success
2945  *      \retval -ve failure
2946  */
2947 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2948                                 const struct dt_rec *rec,
2949                                 const struct dt_key *key, struct thandle *th,
2950                                 struct lustre_capa *capa, int ignore_quota)
2951 {
2952         struct osd_object     *obj = osd_dt_obj(dt);
2953         struct iam_path_descr *ipd;
2954         struct osd_thandle    *oh;
2955         struct iam_container  *bag = &obj->oo_dir->od_container;
2956 #ifdef HAVE_QUOTA_SUPPORT
2957         cfs_cap_t              save = cfs_curproc_cap_pack();
2958 #endif
2959         struct osd_thread_info *oti = osd_oti_get(env);
2960         struct iam_rec         *iam_rec = (struct iam_rec *)oti->oti_ldp;
2961         int                     rc;
2962
2963         ENTRY;
2964
2965         LINVRNT(osd_invariant(obj));
2966         LASSERT(dt_object_exists(dt));
2967         LASSERT(bag->ic_object == obj->oo_inode);
2968         LASSERT(th != NULL);
2969
2970         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2971                 return -EACCES;
2972
2973         OSD_EXEC_OP(th, insert);
2974
2975         ipd = osd_idx_ipd_get(env, bag);
2976         if (unlikely(ipd == NULL))
2977                 RETURN(-ENOMEM);
2978
2979         oh = container_of0(th, struct osd_thandle, ot_super);
2980         LASSERT(oh->ot_handle != NULL);
2981         LASSERT(oh->ot_handle->h_transaction != NULL);
2982 #ifdef HAVE_QUOTA_SUPPORT
2983         if (ignore_quota)
2984                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2985         else
2986                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2987 #endif
2988         if (S_ISDIR(obj->oo_inode->i_mode))
2989                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2990         else
2991                 iam_rec = (struct iam_rec *) rec;
2992         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2993                         iam_rec, ipd);
2994 #ifdef HAVE_QUOTA_SUPPORT
2995         cfs_curproc_cap_unpack(save);
2996 #endif
2997         osd_ipd_put(env, bag, ipd);
2998         LINVRNT(osd_invariant(obj));
2999         RETURN(rc);
3000 }
3001
3002 /**
3003  * Calls ldiskfs_add_entry() to add directory entry
3004  * into the directory. This is required for
3005  * interoperability mode (b11826)
3006  *
3007  * \retval   0, on success
3008  * \retval -ve, on error
3009  */
3010 static int __osd_ea_add_rec(struct osd_thread_info *info,
3011                             struct osd_object *pobj, struct inode  *cinode,
3012                             const char *name, const struct dt_rec *fid,
3013                             struct htree_lock *hlock, struct thandle *th)
3014 {
3015         struct ldiskfs_dentry_param *ldp;
3016         struct dentry               *child;
3017         struct osd_thandle          *oth;
3018         int                          rc;
3019
3020         oth = container_of(th, struct osd_thandle, ot_super);
3021         LASSERT(oth->ot_handle != NULL);
3022         LASSERT(oth->ot_handle->h_transaction != NULL);
3023
3024         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3025
3026         if (fid_is_igif((struct lu_fid *)fid) ||
3027             fid_is_norm((struct lu_fid *)fid)) {
3028                 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3029                 osd_get_ldiskfs_dirent_param(ldp, fid);
3030                 child->d_fsdata = (void*) ldp;
3031         } else
3032                 child->d_fsdata = NULL;
3033         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3034
3035         RETURN(rc);
3036 }
3037
3038 /**
3039  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3040  * into the directory.Also sets flags into osd object to
3041  * indicate dot and dotdot are created. This is required for
3042  * interoperability mode (b11826)
3043  *
3044  * \param dir   directory for dot and dotdot fixup.
3045  * \param obj   child object for linking
3046  *
3047  * \retval   0, on success
3048  * \retval -ve, on error
3049  */
3050 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3051                               struct osd_object *dir,
3052                               struct inode  *parent_dir, const char *name,
3053                               const struct dt_rec *dot_fid,
3054                               const struct dt_rec *dot_dot_fid,
3055                               struct thandle *th)
3056 {
3057         struct inode            *inode  = dir->oo_inode;
3058         struct ldiskfs_dentry_param *dot_ldp;
3059         struct ldiskfs_dentry_param *dot_dot_ldp;
3060         struct osd_thandle      *oth;
3061         int result = 0;
3062
3063         oth = container_of(th, struct osd_thandle, ot_super);
3064         LASSERT(oth->ot_handle->h_transaction != NULL);
3065         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3066
3067         if (strcmp(name, dot) == 0) {
3068                 if (dir->oo_compat_dot_created) {
3069                         result = -EEXIST;
3070                 } else {
3071                         LASSERT(inode == parent_dir);
3072                         dir->oo_compat_dot_created = 1;
3073                         result = 0;
3074                 }
3075         } else if(strcmp(name, dotdot) == 0) {
3076                 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3077                 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3078
3079                 if (!dir->oo_compat_dot_created)
3080                         return -EINVAL;
3081                 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3082                         osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3083                         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3084                 } else {
3085                         dot_ldp = NULL;
3086                         dot_dot_ldp = NULL;
3087                 }
3088                 /* in case of rename, dotdot is already created */
3089                 if (dir->oo_compat_dotdot_created) {
3090                         return __osd_ea_add_rec(info, dir, parent_dir, name,
3091                                                 dot_dot_fid, NULL, th);
3092                 }
3093
3094                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3095                                                 inode, dot_ldp, dot_dot_ldp);
3096                 if (result == 0)
3097                        dir->oo_compat_dotdot_created = 1;
3098         }
3099
3100         return result;
3101 }
3102
3103
3104 /**
3105  * It will call the appropriate osd_add* function and return the
3106  * value, return by respective functions.
3107  */
3108 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3109                           struct inode *cinode, const char *name,
3110                           const struct dt_rec *fid, struct thandle *th)
3111 {
3112         struct osd_thread_info *info   = osd_oti_get(env);
3113         struct htree_lock      *hlock;
3114         int                     rc;
3115
3116         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3117
3118         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3119                                                    name[2] =='\0'))) {
3120                 if (hlock != NULL) {
3121                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3122                                            pobj->oo_inode, 0);
3123                 } else {
3124                         cfs_down_write(&pobj->oo_ext_idx_sem);
3125                 }
3126                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3127                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3128                                         fid, th);
3129         } else {
3130                 if (hlock != NULL) {
3131                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3132                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
3133                 } else {
3134                         cfs_down_write(&pobj->oo_ext_idx_sem);
3135                 }
3136
3137                 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3138                                       hlock, th);
3139         }
3140         if (hlock != NULL)
3141                 ldiskfs_htree_unlock(hlock);
3142         else
3143                 cfs_up_write(&pobj->oo_ext_idx_sem);
3144
3145         return rc;
3146 }
3147
3148 /**
3149  * Calls ->lookup() to find dentry. From dentry get inode and
3150  * read inode's ea to get fid. This is required for  interoperability
3151  * mode (b11826)
3152  *
3153  * \retval   0, on success
3154  * \retval -ve, on error
3155  */
3156 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3157                              struct dt_rec *rec, const struct dt_key *key)
3158 {
3159         struct inode               *dir    = obj->oo_inode;
3160         struct dentry              *dentry;
3161         struct ldiskfs_dir_entry_2 *de;
3162         struct buffer_head         *bh;
3163         struct lu_fid              *fid = (struct lu_fid *) rec;
3164         struct htree_lock          *hlock = NULL;
3165         int                         ino;
3166         int                         rc;
3167
3168         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3169
3170         dentry = osd_child_dentry_get(env, obj,
3171                                       (char *)key, strlen((char *)key));
3172
3173         if (obj->oo_hl_head != NULL) {
3174                 hlock = osd_oti_get(env)->oti_hlock;
3175                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3176                                    dir, LDISKFS_HLOCK_LOOKUP);
3177         } else {
3178                 cfs_down_read(&obj->oo_ext_idx_sem);
3179         }
3180
3181         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3182         if (bh) {
3183                 ino = le32_to_cpu(de->inode);
3184                 rc = osd_get_fid_from_dentry(de, rec);
3185
3186                 /* done with de, release bh */
3187                 brelse(bh);
3188                 if (rc != 0)
3189                         rc = osd_ea_fid_get(env, obj, ino, fid);
3190         } else {
3191                 rc = -ENOENT;
3192         }
3193
3194         if (hlock != NULL)
3195                 ldiskfs_htree_unlock(hlock);
3196         else
3197                 cfs_up_read(&obj->oo_ext_idx_sem);
3198         RETURN (rc);
3199 }
3200
3201 /**
3202  * Find the osd object for given fid.
3203  *
3204  * \param fid need to find the osd object having this fid
3205  *
3206  * \retval osd_object on success
3207  * \retval        -ve on error
3208  */
3209 struct osd_object *osd_object_find(const struct lu_env *env,
3210                                    struct dt_object *dt,
3211                                    const struct lu_fid *fid)
3212 {
3213         struct lu_device  *ludev = dt->do_lu.lo_dev;
3214         struct osd_object *child = NULL;
3215         struct lu_object  *luch;
3216         struct lu_object  *lo;
3217
3218         luch = lu_object_find(env, ludev, fid, NULL);
3219         if (!IS_ERR(luch)) {
3220                 if (lu_object_exists(luch)) {
3221                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3222                         if (lo != NULL)
3223                                 child = osd_obj(lo);
3224                         else
3225                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3226                                                 "lu_object can't be located"
3227                                                 ""DFID"\n", PFID(fid));
3228
3229                         if (child == NULL) {
3230                                 lu_object_put(env, luch);
3231                                 CERROR("Unable to get osd_object\n");
3232                                 child = ERR_PTR(-ENOENT);
3233                         }
3234                 } else {
3235                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
3236                                         "lu_object does not exists "DFID"\n",
3237                                         PFID(fid));
3238                         child = ERR_PTR(-ENOENT);
3239                 }
3240         } else
3241                 child = (void *)luch;
3242
3243         return child;
3244 }
3245
3246 /**
3247  * Put the osd object once done with it.
3248  *
3249  * \param obj osd object that needs to be put
3250  */
3251 static inline void osd_object_put(const struct lu_env *env,
3252                                   struct osd_object *obj)
3253 {
3254         lu_object_put(env, &obj->oo_dt.do_lu);
3255 }
3256
3257 static int osd_index_declare_ea_insert(const struct lu_env *env,
3258                                        struct dt_object *dt,
3259                                        const struct dt_rec *rec,
3260                                        const struct dt_key *key,
3261                                        struct thandle *handle)
3262 {
3263         struct osd_thandle *oh;
3264
3265         LASSERT(dt_object_exists(dt));
3266         LASSERT(handle != NULL);
3267
3268         oh = container_of0(handle, struct osd_thandle, ot_super);
3269      &n