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