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