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