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