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