Whamcloud - gitweb
131bb285ecf9ed75e06640b438b5b5295e9dcca6
[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, Intel Corporation.
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
57 /*
58  * struct OBD_{ALLOC,FREE}*()
59  * OBD_FAIL_CHECK
60  */
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
65
66 #include "osd_internal.h"
67
68 /* llo_* api support */
69 #include <md_object.h>
70 #include <lustre_quota.h>
71
72 int ldiskfs_pdo = 1;
73 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
74                 "ldiskfs with parallel directory operations");
75
76 static const char dot[] = ".";
77 static const char dotdot[] = "..";
78 static const char remote_obj_dir[] = "REM_OBJ_DIR";
79
80 static const struct lu_object_operations      osd_lu_obj_ops;
81 static const struct dt_object_operations      osd_obj_ops;
82 static const struct dt_object_operations      osd_obj_ea_ops;
83 static const struct dt_object_operations      osd_obj_otable_it_ops;
84 static const struct dt_index_operations       osd_index_iam_ops;
85 static const struct dt_index_operations       osd_index_ea_ops;
86
87 #ifdef OSD_TRACK_DECLARES
88 int osd_trans_declare_op2rb[] = {
89         [OSD_OT_ATTR_SET]       = OSD_OT_ATTR_SET,
90         [OSD_OT_PUNCH]          = OSD_OT_MAX,
91         [OSD_OT_XATTR_SET]      = OSD_OT_XATTR_SET,
92         [OSD_OT_CREATE]         = OSD_OT_DESTROY,
93         [OSD_OT_DESTROY]        = OSD_OT_CREATE,
94         [OSD_OT_REF_ADD]        = OSD_OT_REF_DEL,
95         [OSD_OT_REF_DEL]        = OSD_OT_REF_ADD,
96         [OSD_OT_WRITE]          = OSD_OT_WRITE,
97         [OSD_OT_INSERT]         = OSD_OT_DELETE,
98         [OSD_OT_DELETE]         = OSD_OT_INSERT,
99         [OSD_OT_QUOTA]          = OSD_OT_MAX,
100 };
101 #endif
102
103 static int osd_has_index(const struct osd_object *obj)
104 {
105         return obj->oo_dt.do_index_ops != NULL;
106 }
107
108 static int osd_object_invariant(const struct lu_object *l)
109 {
110         return osd_invariant(osd_obj(l));
111 }
112
113 /*
114  * Concurrency: doesn't matter
115  */
116 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
117 {
118         return osd_oti_get(env)->oti_r_locks > 0;
119 }
120
121 /*
122  * Concurrency: doesn't matter
123  */
124 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
125 {
126         struct osd_thread_info *oti = osd_oti_get(env);
127         return oti->oti_w_locks > 0 && o->oo_owner == env;
128 }
129
130 /*
131  * Concurrency: doesn't access mutable data
132  */
133 static int osd_root_get(const struct lu_env *env,
134                         struct dt_device *dev, struct lu_fid *f)
135 {
136         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
137         return 0;
138 }
139
140 /*
141  * OSD object methods.
142  */
143
144 /*
145  * Concurrency: no concurrent access is possible that early in object
146  * life-cycle.
147  */
148 static struct lu_object *osd_object_alloc(const struct lu_env *env,
149                                           const struct lu_object_header *hdr,
150                                           struct lu_device *d)
151 {
152         struct osd_object *mo;
153
154         OBD_ALLOC_PTR(mo);
155         if (mo != NULL) {
156                 struct lu_object *l;
157
158                 l = &mo->oo_dt.do_lu;
159                 dt_object_init(&mo->oo_dt, NULL, d);
160                 mo->oo_dt.do_ops = &osd_obj_ea_ops;
161                 l->lo_ops = &osd_lu_obj_ops;
162                 init_rwsem(&mo->oo_sem);
163                 init_rwsem(&mo->oo_ext_idx_sem);
164                 spin_lock_init(&mo->oo_guard);
165                 return l;
166         } else {
167                 return NULL;
168         }
169 }
170
171 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
172                                   const char *name, void *buf, int len)
173 {
174         dentry->d_inode = inode;
175         return inode->i_op->getxattr(dentry, name, buf, len);
176 }
177
178 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
179                 struct dentry *dentry, struct lustre_mdt_attrs *lma)
180 {
181         int rc;
182
183         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
184                              sizeof(*lma));
185         if (rc == -ERANGE) {
186                 /* try with old lma size */
187                 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
188                                            info->oti_mdt_attrs_old,
189                                            LMA_OLD_SIZE);
190                 if (rc > 0)
191                         memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
192         }
193         if (rc > 0) {
194                 /* Check LMA compatibility */
195                 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
196                         CWARN("%.16s: unsupported incompat LMA feature(s) "
197                               "%lu/%#x\n",
198                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
199                               inode->i_ino, le32_to_cpu(lma->lma_incompat) &
200                                                         ~LMA_INCOMPAT_SUPP);
201                         rc = -ENOSYS;
202                 } else {
203                         lustre_lma_swab(lma);
204                         rc = 0;
205                 }
206         } else if (rc == 0) {
207                 rc = -ENODATA;
208         }
209
210         return rc;
211 }
212
213 /*
214  * retrieve object from backend ext fs.
215  **/
216 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
217                        struct osd_inode_id *id)
218 {
219         struct inode *inode = NULL;
220
221         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
222         if (IS_ERR(inode)) {
223                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
224                        id->oii_ino, PTR_ERR(inode));
225         } else if (id->oii_gen != OSD_OII_NOGEN &&
226                    inode->i_generation != id->oii_gen) {
227                 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
228                        "gen1 = %u\n",
229                        id->oii_ino, id->oii_gen, inode->i_generation);
230                 iput(inode);
231                 inode = ERR_PTR(-ESTALE);
232         } else if (inode->i_nlink == 0) {
233                 /* due to parallel readdir and unlink,
234                 * we can have dead inode here. */
235                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
236                 make_bad_inode(inode);
237                 iput(inode);
238                 inode = ERR_PTR(-ESTALE);
239         } else if (is_bad_inode(inode)) {
240                 CWARN("%.16s: bad inode: ino = %u\n",
241                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
242                 iput(inode);
243                 inode = ERR_PTR(-ENOENT);
244         } else {
245                 if (id->oii_gen == OSD_OII_NOGEN)
246                         osd_id_gen(id, inode->i_ino, inode->i_generation);
247
248                 /* Do not update file c/mtime in ldiskfs.
249                  * NB: we don't have any lock to protect this because we don't
250                  * have reference on osd_object now, but contention with
251                  * another lookup + attr_set can't happen in the tiny window
252                  * between if (...) and set S_NOCMTIME. */
253                 if (!(inode->i_flags & S_NOCMTIME))
254                         inode->i_flags |= S_NOCMTIME;
255         }
256         return inode;
257 }
258
259 static struct inode *
260 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
261              struct osd_inode_id *id, struct lu_fid *fid)
262 {
263         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
264         struct inode            *inode;
265         int                      rc;
266
267         inode = osd_iget(info, dev, id);
268         if (IS_ERR(inode))
269                 return inode;
270
271         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
272         if (rc == 0) {
273                 *fid = lma->lma_self_fid;
274         } else if (rc == -ENODATA) {
275                 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
276                         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
277                 else
278                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
279         } else {
280                 iput(inode);
281                 inode = ERR_PTR(rc);
282         }
283         return inode;
284 }
285
286 static struct inode *
287 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
288                 struct osd_inode_id *id, const struct lu_fid *fid)
289 {
290         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
291         struct inode            *inode;
292         int                      rc;
293
294         inode = osd_iget(info, dev, id);
295         if (IS_ERR(inode))
296                 return inode;
297
298         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
299         if (rc == -ENODATA)
300                 return inode;
301
302         if (rc != 0) {
303                 iput(inode);
304                 return ERR_PTR(rc);
305         }
306
307         if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
308                 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
309                        PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
310                 iput(inode);
311                 return ERR_PTR(-EREMCHG);
312         }
313
314         return inode;
315 }
316
317 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
318                           const struct lu_fid *fid,
319                           const struct lu_object_conf *conf)
320 {
321         struct osd_thread_info *info;
322         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
323         struct osd_device      *dev;
324         struct osd_idmap_cache *oic;
325         struct osd_inode_id    *id;
326         struct inode           *inode;
327         struct osd_scrub       *scrub;
328         struct scrub_file      *sf;
329         int                     result;
330         int                     verify = 0;
331         ENTRY;
332
333         LINVRNT(osd_invariant(obj));
334         LASSERT(obj->oo_inode == NULL);
335         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
336
337         dev = osd_dev(ldev);
338         scrub = &dev->od_scrub;
339         sf = &scrub->os_file;
340         info = osd_oti_get(env);
341         LASSERT(info);
342         oic = &info->oti_cache;
343
344         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
345                 RETURN(-ENOENT);
346
347         /* Search order: 1. per-thread cache. */
348         if (lu_fid_eq(fid, &oic->oic_fid)) {
349                 id = &oic->oic_lid;
350                 goto iget;
351         }
352
353         id = &info->oti_id;
354         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
355                 /* Search order: 2. OI scrub pending list. */
356                 result = osd_oii_lookup(dev, fid, id);
357                 if (result == 0)
358                         goto iget;
359         }
360
361         if (sf->sf_flags & SF_INCONSISTENT)
362                 verify = 1;
363
364         /*
365          * Objects are created as locking anchors or place holders for objects
366          * yet to be created. No need to osd_oi_lookup() at here because FID
367          * shouldn't never be re-used, if it's really a duplicate FID from
368          * unexpected reason, we should be able to detect it later by calling
369          * do_create->osd_oi_insert()
370          */
371         if (conf != NULL && conf->loc_flags & LOC_F_NEW)
372                 GOTO(out, result = 0);
373
374         /* Search order: 3. OI files. */
375         result = osd_oi_lookup(info, dev, fid, id, true);
376         if (result == -ENOENT) {
377                 if (!fid_is_norm(fid) || fid_is_on_ost(info, dev, fid) ||
378                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
379                                       sf->sf_oi_bitmap))
380                         GOTO(out, result = 0);
381
382                 goto trigger;
383         }
384
385         if (result != 0)
386                 GOTO(out, result);
387
388 iget:
389         if (verify == 0)
390                 inode = osd_iget(info, dev, id);
391         else
392                 inode = osd_iget_verify(info, dev, id, fid);
393         if (IS_ERR(inode)) {
394                 result = PTR_ERR(inode);
395                 if (result == -ENOENT || result == -ESTALE) {
396                         fid_zero(&oic->oic_fid);
397                         result = 0;
398                 } else if (result == -EREMCHG) {
399
400 trigger:
401                         if (thread_is_running(&scrub->os_thread)) {
402                                 result = -EINPROGRESS;
403                         } else if (!dev->od_noscrub) {
404                                 result = osd_scrub_start(dev);
405                                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
406                                                "for "DFID", rc = %d [1]\n",
407                                                LDISKFS_SB(osd_sb(dev))->s_es->\
408                                                s_volume_name,PFID(fid), result);
409                                 if (result == 0 || result == -EALREADY)
410                                         result = -EINPROGRESS;
411                                 else
412                                         result = -EREMCHG;
413                         }
414                 }
415
416                 GOTO(out, result);
417         }
418
419         obj->oo_inode = inode;
420         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
421
422         obj->oo_compat_dot_created = 1;
423         obj->oo_compat_dotdot_created = 1;
424
425         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
426                 GOTO(out, result = 0);
427
428         LASSERT(obj->oo_hl_head == NULL);
429         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
430         if (obj->oo_hl_head == NULL) {
431                 obj->oo_inode = NULL;
432                 iput(inode);
433                 GOTO(out, result = -ENOMEM);
434         }
435         GOTO(out, result = 0);
436
437 out:
438         LINVRNT(osd_invariant(obj));
439         return result;
440 }
441
442 /*
443  * Concurrency: shouldn't matter.
444  */
445 static void osd_object_init0(struct osd_object *obj)
446 {
447         LASSERT(obj->oo_inode != NULL);
448         obj->oo_dt.do_body_ops = &osd_body_ops;
449         obj->oo_dt.do_lu.lo_header->loh_attr |=
450                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
451 }
452
453 /*
454  * Concurrency: no concurrent access is possible that early in object
455  * life-cycle.
456  */
457 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
458                            const struct lu_object_conf *conf)
459 {
460         struct osd_object *obj = osd_obj(l);
461         int result;
462
463         LINVRNT(osd_invariant(obj));
464
465         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
466                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
467                 l->lo_header->loh_attr |= LOHA_EXISTS;
468                 return 0;
469         }
470
471         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
472         obj->oo_dt.do_body_ops = &osd_body_ops_new;
473         if (result == 0 && obj->oo_inode != NULL)
474                 osd_object_init0(obj);
475
476         LINVRNT(osd_invariant(obj));
477         return result;
478 }
479
480 /*
481  * Concurrency: no concurrent access is possible that late in object
482  * life-cycle.
483  */
484 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
485 {
486         struct osd_object *obj = osd_obj(l);
487
488         LINVRNT(osd_invariant(obj));
489
490         dt_object_fini(&obj->oo_dt);
491         if (obj->oo_hl_head != NULL)
492                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
493         OBD_FREE_PTR(obj);
494 }
495
496 /*
497  * Concurrency: no concurrent access is possible that late in object
498  * life-cycle.
499  */
500 static void osd_index_fini(struct osd_object *o)
501 {
502         struct iam_container *bag;
503
504         if (o->oo_dir != NULL) {
505                 bag = &o->oo_dir->od_container;
506                 if (o->oo_inode != NULL) {
507                         if (bag->ic_object == o->oo_inode)
508                                 iam_container_fini(bag);
509                 }
510                 OBD_FREE_PTR(o->oo_dir);
511                 o->oo_dir = NULL;
512         }
513 }
514
515 /*
516  * Concurrency: no concurrent access is possible that late in object
517  * life-cycle (for all existing callers, that is. New callers have to provide
518  * their own locking.)
519  */
520 static int osd_inode_unlinked(const struct inode *inode)
521 {
522         return inode->i_nlink == 0;
523 }
524
525 enum {
526         OSD_TXN_OI_DELETE_CREDITS    = 20,
527         OSD_TXN_INODE_DELETE_CREDITS = 20
528 };
529
530 /*
531  * Journal
532  */
533
534 #if OSD_THANDLE_STATS
535 /**
536  * Set time when the handle is allocated
537  */
538 static void osd_th_alloced(struct osd_thandle *oth)
539 {
540         oth->oth_alloced = cfs_time_current();
541 }
542
543 /**
544  * Set time when the handle started
545  */
546 static void osd_th_started(struct osd_thandle *oth)
547 {
548         oth->oth_started = cfs_time_current();
549 }
550
551 /**
552  * Helper function to convert time interval to microseconds packed in
553  * long int.
554  */
555 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
556 {
557         struct timeval val;
558
559         cfs_duration_usec(cfs_time_sub(end, start), &val);
560         return val.tv_sec * 1000000 + val.tv_usec;
561 }
562
563 /**
564  * Check whether the we deal with this handle for too long.
565  */
566 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
567                                 cfs_time_t alloced, cfs_time_t started,
568                                 cfs_time_t closed)
569 {
570         cfs_time_t now = cfs_time_current();
571
572         LASSERT(dev != NULL);
573
574         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
575                             interval_to_usec(alloced, started));
576         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
577                             interval_to_usec(started, closed));
578         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
579                             interval_to_usec(closed, now));
580
581         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
582                 CWARN("transaction handle %p was open for too long: "
583                       "now "CFS_TIME_T" ,"
584                       "alloced "CFS_TIME_T" ,"
585                       "started "CFS_TIME_T" ,"
586                       "closed "CFS_TIME_T"\n",
587                       oth, now, alloced, started, closed);
588                 libcfs_debug_dumpstack(NULL);
589         }
590 }
591
592 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
593 {                                                                       \
594         cfs_time_t __closed = cfs_time_current();                       \
595         cfs_time_t __alloced = oth->oth_alloced;                        \
596         cfs_time_t __started = oth->oth_started;                        \
597                                                                         \
598         expr;                                                           \
599         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
600 }
601
602 #else /* OSD_THANDLE_STATS */
603
604 #define osd_th_alloced(h)                  do {} while(0)
605 #define osd_th_started(h)                  do {} while(0)
606 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
607
608 #endif /* OSD_THANDLE_STATS */
609
610 /*
611  * Concurrency: doesn't access mutable data.
612  */
613 static int osd_param_is_not_sane(const struct osd_device *dev,
614                                  const struct thandle *th)
615 {
616         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
617
618         return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
619 }
620
621 /*
622  * Concurrency: shouldn't matter.
623  */
624 static void osd_trans_commit_cb(struct super_block *sb,
625                                 struct ldiskfs_journal_cb_entry *jcb, int error)
626 {
627         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
628         struct thandle     *th  = &oh->ot_super;
629         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
630         struct dt_txn_commit_cb *dcb, *tmp;
631
632         LASSERT(oh->ot_handle == NULL);
633
634         if (error)
635                 CERROR("transaction @0x%p commit error: %d\n", th, error);
636
637         dt_txn_hook_commit(th);
638
639         /* call per-transaction callbacks if any */
640         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
641                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
642                          "commit callback entry: magic=%x name='%s'\n",
643                          dcb->dcb_magic, dcb->dcb_name);
644                 cfs_list_del_init(&dcb->dcb_linkage);
645                 dcb->dcb_func(NULL, th, dcb, error);
646         }
647
648         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
649         lu_device_put(lud);
650         th->th_dev = NULL;
651
652         lu_context_exit(&th->th_ctx);
653         lu_context_fini(&th->th_ctx);
654         OBD_FREE_PTR(oh);
655 }
656
657 static struct thandle *osd_trans_create(const struct lu_env *env,
658                                         struct dt_device *d)
659 {
660         struct osd_thread_info *oti = osd_oti_get(env);
661         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
662         struct osd_thandle     *oh;
663         struct thandle         *th;
664         ENTRY;
665
666         /* on pending IO in this thread should left from prev. request */
667         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
668
669         th = ERR_PTR(-ENOMEM);
670         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
671         if (oh != NULL) {
672                 oh->ot_quota_trans = &oti->oti_quota_trans;
673                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
674                 th = &oh->ot_super;
675                 th->th_dev = d;
676                 th->th_result = 0;
677                 th->th_tags = LCT_TX_HANDLE;
678                 oh->ot_credits = 0;
679                 oti->oti_dev = osd_dt_dev(d);
680                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
681                 osd_th_alloced(oh);
682
683                 memset(oti->oti_declare_ops, 0, OSD_OT_MAX);
684                 memset(oti->oti_declare_ops_rb, 0, OSD_OT_MAX);
685                 memset(oti->oti_declare_ops_cred, 0, OSD_OT_MAX);
686                 oti->oti_rollback = false;
687         }
688         RETURN(th);
689 }
690
691 /*
692  * Concurrency: shouldn't matter.
693  */
694 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
695                     struct thandle *th)
696 {
697         struct osd_thread_info *oti = osd_oti_get(env);
698         struct osd_device  *dev = osd_dt_dev(d);
699         handle_t           *jh;
700         struct osd_thandle *oh;
701         int rc;
702
703         ENTRY;
704
705         LASSERT(current->journal_info == NULL);
706
707         oh = container_of0(th, struct osd_thandle, ot_super);
708         LASSERT(oh != NULL);
709         LASSERT(oh->ot_handle == NULL);
710
711         rc = dt_txn_hook_start(env, d, th);
712         if (rc != 0)
713                 GOTO(out, rc);
714
715         if (unlikely(osd_param_is_not_sane(dev, th))) {
716                 static unsigned long last_printed;
717                 static int last_credits;
718
719                 CWARN("%.16s: too many transaction credits (%d > %d)\n",
720                       LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
721                       oh->ot_credits,
722                       osd_journal(dev)->j_max_transaction_buffers);
723 #ifdef OSD_TRACK_DECLARES
724                 CWARN("  create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
725                       oti->oti_declare_ops[OSD_OT_CREATE],
726                       oti->oti_declare_ops_cred[OSD_OT_CREATE],
727                       oti->oti_declare_ops[OSD_OT_DELETE],
728                       oti->oti_declare_ops_cred[OSD_OT_DELETE],
729                       oti->oti_declare_ops[OSD_OT_DESTROY],
730                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
731                 CWARN("  attr_set: %u/%u, xattr_set: %u/%u\n",
732                       oti->oti_declare_ops[OSD_OT_ATTR_SET],
733                       oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
734                       oti->oti_declare_ops[OSD_OT_XATTR_SET],
735                       oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
736                 CWARN("  write: %u/%u, punch: %u/%u, quota %u/%u\n",
737                       oti->oti_declare_ops[OSD_OT_WRITE],
738                       oti->oti_declare_ops_cred[OSD_OT_WRITE],
739                       oti->oti_declare_ops[OSD_OT_PUNCH],
740                       oti->oti_declare_ops_cred[OSD_OT_PUNCH],
741                       oti->oti_declare_ops[OSD_OT_QUOTA],
742                       oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
743                 CWARN("  insert: %u/%u, delete: %u/%u\n",
744                       oti->oti_declare_ops[OSD_OT_INSERT],
745                       oti->oti_declare_ops_cred[OSD_OT_INSERT],
746                       oti->oti_declare_ops[OSD_OT_DESTROY],
747                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
748                 CWARN("  ref_add: %u/%u, ref_del: %u/%u\n",
749                       oti->oti_declare_ops[OSD_OT_REF_ADD],
750                       oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
751                       oti->oti_declare_ops[OSD_OT_REF_DEL],
752                       oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
753
754                 if (last_credits != oh->ot_credits &&
755                     time_after(jiffies, last_printed + 60 * HZ)) {
756                         libcfs_debug_dumpstack(NULL);
757                         last_credits = oh->ot_credits;
758                         last_printed = jiffies;
759                 }
760 #endif
761                 /* XXX Limit the credits to 'max_transaction_buffers', and
762                  *     let the underlying filesystem to catch the error if
763                  *     we really need so many credits.
764                  *
765                  *     This should be removed when we can calculate the
766                  *     credits precisely. */
767                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
768         }
769
770         /*
771          * XXX temporary stuff. Some abstraction layer should
772          * be used.
773          */
774         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
775         osd_th_started(oh);
776         if (!IS_ERR(jh)) {
777                 oh->ot_handle = jh;
778                 LASSERT(oti->oti_txns == 0);
779                 lu_context_init(&th->th_ctx, th->th_tags);
780                 lu_context_enter(&th->th_ctx);
781
782                 lu_device_get(&d->dd_lu_dev);
783                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
784                                              "osd-tx", th);
785                 oti->oti_txns++;
786                 rc = 0;
787         } else {
788                 rc = PTR_ERR(jh);
789         }
790 out:
791         RETURN(rc);
792 }
793
794 /*
795  * Concurrency: shouldn't matter.
796  */
797 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
798 {
799         int                     rc = 0;
800         struct osd_thandle     *oh;
801         struct osd_thread_info *oti = osd_oti_get(env);
802         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
803         struct qsd_instance    *qsd = oti->oti_dev->od_quota_slave;
804         ENTRY;
805
806         oh = container_of0(th, struct osd_thandle, ot_super);
807
808         if (qsd != NULL)
809                 /* inform the quota slave device that the transaction is
810                  * stopping */
811                 qsd_op_end(env, qsd, oh->ot_quota_trans);
812         oh->ot_quota_trans = NULL;
813
814         if (oh->ot_handle != NULL) {
815                 handle_t *hdl = oh->ot_handle;
816
817                 /*
818                  * add commit callback
819                  * notice we don't do this in osd_trans_start()
820                  * as underlying transaction can change during truncate
821                  */
822                 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
823                                          &oh->ot_jcb);
824
825                 LASSERT(oti->oti_txns == 1);
826                 oti->oti_txns--;
827                 rc = dt_txn_hook_stop(env, th);
828                 if (rc != 0)
829                         CERROR("Failure in transaction hook: %d\n", rc);
830
831                 /* hook functions might modify th_sync */
832                 hdl->h_sync = th->th_sync;
833
834                 oh->ot_handle = NULL;
835                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
836                                   rc = ldiskfs_journal_stop(hdl));
837                 if (rc != 0)
838                         CERROR("Failure to stop transaction: %d\n", rc);
839         } else {
840                 OBD_FREE_PTR(oh);
841         }
842
843         /* as we want IO to journal and data IO be concurrent, we don't block
844          * awaiting data IO completion in osd_do_bio(), instead we wait here
845          * once transaction is submitted to the journal. all reqular requests
846          * don't do direct IO (except read/write), thus this wait_event becomes
847          * no-op for them.
848          *
849          * IMPORTANT: we have to wait till any IO submited by the thread is
850          * completed otherwise iobuf may be corrupted by different request
851          */
852         cfs_wait_event(iobuf->dr_wait,
853                        cfs_atomic_read(&iobuf->dr_numreqs) == 0);
854         if (!rc)
855                 rc = iobuf->dr_error;
856
857         RETURN(rc);
858 }
859
860 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
861 {
862         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
863                                                ot_super);
864
865         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
866         LASSERT(&dcb->dcb_func != NULL);
867         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
868
869         return 0;
870 }
871
872 /*
873  * Called just before object is freed. Releases all resources except for
874  * object itself (that is released by osd_object_free()).
875  *
876  * Concurrency: no concurrent access is possible that late in object
877  * life-cycle.
878  */
879 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
880 {
881         struct osd_object *obj   = osd_obj(l);
882         struct inode      *inode = obj->oo_inode;
883
884         LINVRNT(osd_invariant(obj));
885
886         /*
887          * If object is unlinked remove fid->ino mapping from object index.
888          */
889
890         osd_index_fini(obj);
891         if (inode != NULL) {
892                 struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
893                 qid_t                    uid = inode->i_uid;
894                 qid_t                    gid = inode->i_gid;
895
896                 iput(inode);
897                 obj->oo_inode = NULL;
898
899                 if (qsd != NULL) {
900                         struct osd_thread_info  *info = osd_oti_get(env);
901                         struct lquota_id_info   *qi = &info->oti_qi;
902
903                         /* Release granted quota to master if necessary */
904                         qi->lqi_id.qid_uid = uid;
905                         qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
906
907                         qi->lqi_id.qid_uid = gid;
908                         qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
909                 }
910         }
911 }
912
913 /*
914  * Concurrency: ->loo_object_release() is called under site spin-lock.
915  */
916 static void osd_object_release(const struct lu_env *env,
917                                struct lu_object *l)
918 {
919 }
920
921 /*
922  * Concurrency: shouldn't matter.
923  */
924 static int osd_object_print(const struct lu_env *env, void *cookie,
925                             lu_printer_t p, const struct lu_object *l)
926 {
927         struct osd_object *o = osd_obj(l);
928         struct iam_descr  *d;
929
930         if (o->oo_dir != NULL)
931                 d = o->oo_dir->od_container.ic_descr;
932         else
933                 d = NULL;
934         return (*p)(env, cookie,
935                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
936                     o, o->oo_inode,
937                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
938                     o->oo_inode ? o->oo_inode->i_generation : 0,
939                     d ? d->id_ops->id_name : "plain");
940 }
941
942 /*
943  * Concurrency: shouldn't matter.
944  */
945 int osd_statfs(const struct lu_env *env, struct dt_device *d,
946                struct obd_statfs *sfs)
947 {
948         struct osd_device  *osd = osd_dt_dev(d);
949         struct super_block *sb = osd_sb(osd);
950         struct kstatfs     *ksfs;
951         int result = 0;
952
953         if (unlikely(osd->od_mnt == NULL))
954                 return -EINPROGRESS;
955
956         /* osd_lproc.c call this without env, allocate ksfs for that case */
957         if (unlikely(env == NULL)) {
958                 OBD_ALLOC_PTR(ksfs);
959                 if (ksfs == NULL)
960                         return -ENOMEM;
961         } else {
962                 ksfs = &osd_oti_get(env)->oti_ksfs;
963         }
964
965         spin_lock(&osd->od_osfs_lock);
966         /* cache 1 second */
967         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
968                 result = sb->s_op->statfs(sb->s_root, ksfs);
969                 if (likely(result == 0)) { /* N.B. statfs can't really fail */
970                         osd->od_osfs_age = cfs_time_current_64();
971                         statfs_pack(&osd->od_statfs, ksfs);
972                         if (sb->s_flags & MS_RDONLY)
973                                 sfs->os_state = OS_STATE_READONLY;
974                 }
975         }
976
977         if (likely(result == 0))
978                 *sfs = osd->od_statfs;
979         spin_unlock(&osd->od_osfs_lock);
980
981         if (unlikely(env == NULL))
982                 OBD_FREE_PTR(ksfs);
983
984         return result;
985 }
986
987 /**
988  * Estimate space needed for file creations. We assume the largest filename
989  * which is 2^64 - 1, hence a filename of 20 chars.
990  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
991  */
992 #ifdef __LDISKFS_DIR_REC_LEN
993 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
994 #else
995 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
996 #endif
997
998 /*
999  * Concurrency: doesn't access mutable data.
1000  */
1001 static void osd_conf_get(const struct lu_env *env,
1002                          const struct dt_device *dev,
1003                          struct dt_device_param *param)
1004 {
1005         struct super_block *sb = osd_sb(osd_dt_dev(dev));
1006
1007         /*
1008          * XXX should be taken from not-yet-existing fs abstraction layer.
1009          */
1010         param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1011         param->ddp_max_name_len = LDISKFS_NAME_LEN;
1012         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1013         param->ddp_block_shift  = sb->s_blocksize_bits;
1014         param->ddp_mount_type     = LDD_MT_LDISKFS;
1015         param->ddp_maxbytes       = sb->s_maxbytes;
1016         /* Overhead estimate should be fairly accurate, so we really take a tiny
1017          * error margin which also avoids fragmenting the filesystem too much */
1018         param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1019         /* inode are statically allocated, so per-inode space consumption
1020          * is the space consumed by the directory entry */
1021         param->ddp_inodespace     = PER_OBJ_USAGE;
1022         /* per-fragment overhead to be used by the client code */
1023         param->ddp_grant_frag     = 6 * LDISKFS_BLOCK_SIZE(sb);
1024         param->ddp_mntopts      = 0;
1025         if (test_opt(sb, XATTR_USER))
1026                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1027         if (test_opt(sb, POSIX_ACL))
1028                 param->ddp_mntopts |= MNTOPT_ACL;
1029
1030 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1031         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1032                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1033         else
1034 #endif
1035                 param->ddp_max_ea_size = sb->s_blocksize;
1036
1037 }
1038
1039 /*
1040  * Concurrency: shouldn't matter.
1041  */
1042 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1043 {
1044         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1045         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1046 }
1047
1048 /**
1049  * Start commit for OSD device.
1050  *
1051  * An implementation of dt_commit_async method for OSD device.
1052  * Asychronously starts underlayng fs sync and thereby a transaction
1053  * commit.
1054  *
1055  * \param env environment
1056  * \param d dt device
1057  *
1058  * \see dt_device_operations
1059  */
1060 static int osd_commit_async(const struct lu_env *env,
1061                             struct dt_device *d)
1062 {
1063         struct super_block *s = osd_sb(osd_dt_dev(d));
1064         ENTRY;
1065
1066         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1067         RETURN(s->s_op->sync_fs(s, 0));
1068 }
1069
1070 /*
1071  * Concurrency: shouldn't matter.
1072  */
1073
1074 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1075 {
1076         struct super_block *sb = osd_sb(osd_dt_dev(d));
1077         struct block_device *dev = sb->s_bdev;
1078 #ifdef HAVE_DEV_SET_RDONLY
1079         struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1080         int rc = 0;
1081 #else
1082         int rc = -EOPNOTSUPP;
1083 #endif
1084         ENTRY;
1085
1086 #ifdef HAVE_DEV_SET_RDONLY
1087         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1088
1089         if (jdev && (jdev != dev)) {
1090                 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1091                        (long)jdev);
1092                 dev_set_rdonly(jdev);
1093         }
1094         CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1095         dev_set_rdonly(dev);
1096 #else
1097         CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1098                osd_dt_dev(d)->od_svname, (long)dev, rc);
1099 #endif
1100         RETURN(rc);
1101 }
1102
1103 /*
1104  * Concurrency: serialization provided by callers.
1105  */
1106 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1107                               int mode, unsigned long timeout, __u32 alg,
1108                               struct lustre_capa_key *keys)
1109 {
1110         struct osd_device *dev = osd_dt_dev(d);
1111         ENTRY;
1112
1113         dev->od_fl_capa = mode;
1114         dev->od_capa_timeout = timeout;
1115         dev->od_capa_alg = alg;
1116         dev->od_capa_keys = keys;
1117         RETURN(0);
1118 }
1119
1120 /**
1121  * Note: we do not count into QUOTA here.
1122  * If we mount with --data_journal we may need more.
1123  */
1124 const int osd_dto_credits_noquota[DTO_NR] = {
1125         /**
1126          * Insert/Delete.
1127          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1128          * SINGLEDATA_TRANS_BLOCKS(8)
1129          * XXX Note: maybe iam need more, since iam have more level than
1130          *           EXT3 htree.
1131          */
1132         [DTO_INDEX_INSERT]  = 16,
1133         [DTO_INDEX_DELETE]  = 16,
1134         /**
1135          * Used for OI scrub
1136          */
1137         [DTO_INDEX_UPDATE]  = 16,
1138         /**
1139          * Create a object. The same as create object in EXT3.
1140          * DATA_TRANS_BLOCKS(14) +
1141          * INDEX_EXTRA_BLOCKS(8) +
1142          * 3(inode bits, groups, GDT)
1143          */
1144         [DTO_OBJECT_CREATE] = 25,
1145         /**
1146          * XXX: real credits to be fixed
1147          */
1148         [DTO_OBJECT_DELETE] = 25,
1149         /**
1150          * Attr set credits (inode)
1151          */
1152         [DTO_ATTR_SET_BASE] = 1,
1153         /**
1154          * Xattr set. The same as xattr of EXT3.
1155          * DATA_TRANS_BLOCKS(14)
1156          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1157          * are also counted in. Do not know why?
1158          */
1159         [DTO_XATTR_SET]     = 14,
1160         [DTO_LOG_REC]       = 14,
1161         /**
1162          * credits for inode change during write.
1163          */
1164         [DTO_WRITE_BASE]    = 3,
1165         /**
1166          * credits for single block write.
1167          */
1168         [DTO_WRITE_BLOCK]   = 14,
1169         /**
1170          * Attr set credits for chown.
1171          * This is extra credits for setattr, and it is null without quota
1172          */
1173         [DTO_ATTR_SET_CHOWN]= 0
1174 };
1175
1176 static const struct dt_device_operations osd_dt_ops = {
1177         .dt_root_get       = osd_root_get,
1178         .dt_statfs         = osd_statfs,
1179         .dt_trans_create   = osd_trans_create,
1180         .dt_trans_start    = osd_trans_start,
1181         .dt_trans_stop     = osd_trans_stop,
1182         .dt_trans_cb_add   = osd_trans_cb_add,
1183         .dt_conf_get       = osd_conf_get,
1184         .dt_sync           = osd_sync,
1185         .dt_ro             = osd_ro,
1186         .dt_commit_async   = osd_commit_async,
1187         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1188 };
1189
1190 static void osd_object_read_lock(const struct lu_env *env,
1191                                  struct dt_object *dt, unsigned role)
1192 {
1193         struct osd_object *obj = osd_dt_obj(dt);
1194         struct osd_thread_info *oti = osd_oti_get(env);
1195
1196         LINVRNT(osd_invariant(obj));
1197
1198         LASSERT(obj->oo_owner != env);
1199         down_read_nested(&obj->oo_sem, role);
1200
1201         LASSERT(obj->oo_owner == NULL);
1202         oti->oti_r_locks++;
1203 }
1204
1205 static void osd_object_write_lock(const struct lu_env *env,
1206                                   struct dt_object *dt, unsigned role)
1207 {
1208         struct osd_object *obj = osd_dt_obj(dt);
1209         struct osd_thread_info *oti = osd_oti_get(env);
1210
1211         LINVRNT(osd_invariant(obj));
1212
1213         LASSERT(obj->oo_owner != env);
1214         down_write_nested(&obj->oo_sem, role);
1215
1216         LASSERT(obj->oo_owner == NULL);
1217         obj->oo_owner = env;
1218         oti->oti_w_locks++;
1219 }
1220
1221 static void osd_object_read_unlock(const struct lu_env *env,
1222                                    struct dt_object *dt)
1223 {
1224         struct osd_object *obj = osd_dt_obj(dt);
1225         struct osd_thread_info *oti = osd_oti_get(env);
1226
1227         LINVRNT(osd_invariant(obj));
1228
1229         LASSERT(oti->oti_r_locks > 0);
1230         oti->oti_r_locks--;
1231         up_read(&obj->oo_sem);
1232 }
1233
1234 static void osd_object_write_unlock(const struct lu_env *env,
1235                                     struct dt_object *dt)
1236 {
1237         struct osd_object *obj = osd_dt_obj(dt);
1238         struct osd_thread_info *oti = osd_oti_get(env);
1239
1240         LINVRNT(osd_invariant(obj));
1241
1242         LASSERT(obj->oo_owner == env);
1243         LASSERT(oti->oti_w_locks > 0);
1244         oti->oti_w_locks--;
1245         obj->oo_owner = NULL;
1246         up_write(&obj->oo_sem);
1247 }
1248
1249 static int osd_object_write_locked(const struct lu_env *env,
1250                                    struct dt_object *dt)
1251 {
1252         struct osd_object *obj = osd_dt_obj(dt);
1253
1254         LINVRNT(osd_invariant(obj));
1255
1256         return obj->oo_owner == env;
1257 }
1258
1259 static int capa_is_sane(const struct lu_env *env,
1260                         struct osd_device *dev,
1261                         struct lustre_capa *capa,
1262                         struct lustre_capa_key *keys)
1263 {
1264         struct osd_thread_info *oti = osd_oti_get(env);
1265         struct lustre_capa *tcapa = &oti->oti_capa;
1266         struct obd_capa *oc;
1267         int i, rc = 0;
1268         ENTRY;
1269
1270         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1271         if (oc) {
1272                 if (capa_is_expired(oc)) {
1273                         DEBUG_CAPA(D_ERROR, capa, "expired");
1274                         rc = -ESTALE;
1275                 }
1276                 capa_put(oc);
1277                 RETURN(rc);
1278         }
1279
1280         if (capa_is_expired_sec(capa)) {
1281                 DEBUG_CAPA(D_ERROR, capa, "expired");
1282                 RETURN(-ESTALE);
1283         }
1284
1285         spin_lock(&capa_lock);
1286         for (i = 0; i < 2; i++) {
1287                 if (keys[i].lk_keyid == capa->lc_keyid) {
1288                         oti->oti_capa_key = keys[i];
1289                         break;
1290                 }
1291         }
1292         spin_unlock(&capa_lock);
1293
1294         if (i == 2) {
1295                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1296                 RETURN(-ESTALE);
1297         }
1298
1299         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1300         if (rc)
1301                 RETURN(rc);
1302
1303         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1304                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1305                 RETURN(-EACCES);
1306         }
1307
1308         oc = capa_add(dev->od_capa_hash, capa);
1309         capa_put(oc);
1310
1311         RETURN(0);
1312 }
1313
1314 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1315                     struct lustre_capa *capa, __u64 opc)
1316 {
1317         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1318         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1319         struct md_capainfo *ci;
1320         int rc;
1321
1322         if (!dev->od_fl_capa)
1323                 return 0;
1324
1325         if (capa == BYPASS_CAPA)
1326                 return 0;
1327
1328         ci = md_capainfo(env);
1329         if (unlikely(!ci))
1330                 return 0;
1331
1332         if (ci->mc_auth == LC_ID_NONE)
1333                 return 0;
1334
1335         if (!capa) {
1336                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1337                 return -EACCES;
1338         }
1339
1340         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1341                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1342                            PFID(fid));
1343                 return -EACCES;
1344         }
1345
1346         if (!capa_opc_supported(capa, opc)) {
1347                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1348                 return -EACCES;
1349         }
1350
1351         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1352                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1353                 return -EACCES;
1354         }
1355
1356         return 0;
1357 }
1358
1359 static struct timespec *osd_inode_time(const struct lu_env *env,
1360                                        struct inode *inode, __u64 seconds)
1361 {
1362         struct osd_thread_info  *oti = osd_oti_get(env);
1363         struct timespec         *t   = &oti->oti_time;
1364
1365         t->tv_sec = seconds;
1366         t->tv_nsec = 0;
1367         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1368         return t;
1369 }
1370
1371
1372 static void osd_inode_getattr(const struct lu_env *env,
1373                               struct inode *inode, struct lu_attr *attr)
1374 {
1375         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1376                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1377                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1378                                LA_TYPE;
1379
1380         attr->la_atime      = LTIME_S(inode->i_atime);
1381         attr->la_mtime      = LTIME_S(inode->i_mtime);
1382         attr->la_ctime      = LTIME_S(inode->i_ctime);
1383         attr->la_mode       = inode->i_mode;
1384         attr->la_size       = i_size_read(inode);
1385         attr->la_blocks     = inode->i_blocks;
1386         attr->la_uid        = inode->i_uid;
1387         attr->la_gid        = inode->i_gid;
1388         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1389         attr->la_nlink      = inode->i_nlink;
1390         attr->la_rdev       = inode->i_rdev;
1391         attr->la_blksize    = 1 << inode->i_blkbits;
1392         attr->la_blkbits    = inode->i_blkbits;
1393 }
1394
1395 static int osd_attr_get(const struct lu_env *env,
1396                         struct dt_object *dt,
1397                         struct lu_attr *attr,
1398                         struct lustre_capa *capa)
1399 {
1400         struct osd_object *obj = osd_dt_obj(dt);
1401
1402         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1403         LINVRNT(osd_invariant(obj));
1404
1405         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1406                 return -EACCES;
1407
1408         spin_lock(&obj->oo_guard);
1409         osd_inode_getattr(env, obj->oo_inode, attr);
1410         spin_unlock(&obj->oo_guard);
1411         return 0;
1412 }
1413
1414 static int osd_declare_attr_set(const struct lu_env *env,
1415                                 struct dt_object *dt,
1416                                 const struct lu_attr *attr,
1417                                 struct thandle *handle)
1418 {
1419         struct osd_thandle     *oh;
1420         struct osd_object      *obj;
1421         struct osd_thread_info *info = osd_oti_get(env);
1422         struct lquota_id_info  *qi = &info->oti_qi;
1423         long long               bspace;
1424         int                     rc = 0;
1425         bool                    allocated;
1426         ENTRY;
1427
1428         LASSERT(dt != NULL);
1429         LASSERT(handle != NULL);
1430
1431         obj = osd_dt_obj(dt);
1432         LASSERT(osd_invariant(obj));
1433
1434         oh = container_of0(handle, struct osd_thandle, ot_super);
1435         LASSERT(oh->ot_handle == NULL);
1436
1437         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1438                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1439
1440         if (attr == NULL || obj->oo_inode == NULL)
1441                 RETURN(rc);
1442
1443         bspace   = obj->oo_inode->i_blocks;
1444         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1445         bspace   = toqb(bspace);
1446
1447         /* Changing ownership is always preformed by super user, it should not
1448          * fail with EDQUOT.
1449          *
1450          * We still need to call the osd_declare_qid() to calculate the journal
1451          * credits for updating quota accounting files and to trigger quota
1452          * space adjustment once the operation is completed.*/
1453         if ((attr->la_valid & LA_UID) != 0 &&
1454              attr->la_uid != obj->oo_inode->i_uid) {
1455                 qi->lqi_type = USRQUOTA;
1456
1457                 /* inode accounting */
1458                 qi->lqi_is_blk = false;
1459
1460                 /* one more inode for the new owner ... */
1461                 qi->lqi_id.qid_uid = attr->la_uid;
1462                 qi->lqi_space      = 1;
1463                 allocated = (attr->la_uid == 0) ? true : false;
1464                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1465                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1466                         rc = 0;
1467                 if (rc)
1468                         RETURN(rc);
1469
1470                 /* and one less inode for the current uid */
1471                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1472                 qi->lqi_space      = -1;
1473                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1474                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1475                         rc = 0;
1476                 if (rc)
1477                         RETURN(rc);
1478
1479                 /* block accounting */
1480                 qi->lqi_is_blk = true;
1481
1482                 /* more blocks for the new owner ... */
1483                 qi->lqi_id.qid_uid = attr->la_uid;
1484                 qi->lqi_space      = bspace;
1485                 allocated = (attr->la_uid == 0) ? true : false;
1486                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1487                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1488                         rc = 0;
1489                 if (rc)
1490                         RETURN(rc);
1491
1492                 /* and finally less blocks for the current owner */
1493                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1494                 qi->lqi_space      = -bspace;
1495                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1496                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1497                         rc = 0;
1498                 if (rc)
1499                         RETURN(rc);
1500         }
1501
1502         if (attr->la_valid & LA_GID &&
1503             attr->la_gid != obj->oo_inode->i_gid) {
1504                 qi->lqi_type = GRPQUOTA;
1505
1506                 /* inode accounting */
1507                 qi->lqi_is_blk = false;
1508
1509                 /* one more inode for the new group owner ... */
1510                 qi->lqi_id.qid_gid = attr->la_gid;
1511                 qi->lqi_space      = 1;
1512                 allocated = (attr->la_gid == 0) ? true : false;
1513                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1514                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1515                         rc = 0;
1516                 if (rc)
1517                         RETURN(rc);
1518
1519                 /* and one less inode for the current gid */
1520                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1521                 qi->lqi_space      = -1;
1522                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1523                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1524                         rc = 0;
1525                 if (rc)
1526                         RETURN(rc);
1527
1528                 /* block accounting */
1529                 qi->lqi_is_blk = true;
1530
1531                 /* more blocks for the new owner ... */
1532                 qi->lqi_id.qid_gid = attr->la_gid;
1533                 qi->lqi_space      = bspace;
1534                 allocated = (attr->la_gid == 0) ? true : false;
1535                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1536                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1537                         rc = 0;
1538                 if (rc)
1539                         RETURN(rc);
1540
1541                 /* and finally less blocks for the current owner */
1542                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1543                 qi->lqi_space      = -bspace;
1544                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1545                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1546                         rc = 0;
1547                 if (rc)
1548                         RETURN(rc);
1549         }
1550
1551         RETURN(rc);
1552 }
1553
1554 static int osd_inode_setattr(const struct lu_env *env,
1555                              struct inode *inode, const struct lu_attr *attr)
1556 {
1557         __u64 bits;
1558
1559         bits = attr->la_valid;
1560
1561         if (bits & LA_ATIME)
1562                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1563         if (bits & LA_CTIME)
1564                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1565         if (bits & LA_MTIME)
1566                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1567         if (bits & LA_SIZE) {
1568                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1569                 i_size_write(inode, attr->la_size);
1570         }
1571
1572 #if 0
1573         /* OSD should not change "i_blocks" which is used by quota.
1574          * "i_blocks" should be changed by ldiskfs only. */
1575         if (bits & LA_BLOCKS)
1576                 inode->i_blocks = attr->la_blocks;
1577 #endif
1578         if (bits & LA_MODE)
1579                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1580                         (attr->la_mode & ~S_IFMT);
1581         if (bits & LA_UID)
1582                 inode->i_uid    = attr->la_uid;
1583         if (bits & LA_GID)
1584                 inode->i_gid    = attr->la_gid;
1585         if (bits & LA_NLINK)
1586                 set_nlink(inode, attr->la_nlink);
1587         if (bits & LA_RDEV)
1588                 inode->i_rdev   = attr->la_rdev;
1589
1590         if (bits & LA_FLAGS) {
1591                 /* always keep S_NOCMTIME */
1592                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1593                                  S_NOCMTIME;
1594         }
1595         return 0;
1596 }
1597
1598 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1599 {
1600         if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1601             (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1602                 struct iattr    iattr;
1603                 int             rc;
1604
1605                 iattr.ia_valid = 0;
1606                 if (attr->la_valid & LA_UID)
1607                         iattr.ia_valid |= ATTR_UID;
1608                 if (attr->la_valid & LA_GID)
1609                         iattr.ia_valid |= ATTR_GID;
1610                 iattr.ia_uid = attr->la_uid;
1611                 iattr.ia_gid = attr->la_gid;
1612
1613                 rc = ll_vfs_dq_transfer(inode, &iattr);
1614                 if (rc) {
1615                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
1616                                "enforcement enabled on the ldiskfs filesystem?",
1617                                inode->i_sb->s_id, rc);
1618                         return rc;
1619                 }
1620         }
1621         return 0;
1622 }
1623
1624 static int osd_attr_set(const struct lu_env *env,
1625                         struct dt_object *dt,
1626                         const struct lu_attr *attr,
1627                         struct thandle *handle,
1628                         struct lustre_capa *capa)
1629 {
1630         struct osd_object *obj = osd_dt_obj(dt);
1631         struct inode      *inode;
1632         int rc;
1633
1634         LASSERT(handle != NULL);
1635         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1636         LASSERT(osd_invariant(obj));
1637
1638         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1639                 return -EACCES;
1640
1641         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1642
1643         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1644                 struct osd_thread_info  *oti  = osd_oti_get(env);
1645                 const struct lu_fid     *fid0 = lu_object_fid(&dt->do_lu);
1646                 struct lu_fid           *fid1 = &oti->oti_fid;
1647                 struct osd_inode_id     *id   = &oti->oti_id;
1648                 struct iam_path_descr   *ipd;
1649                 struct iam_container    *bag;
1650                 struct osd_thandle      *oh;
1651                 int                      rc;
1652
1653                 fid_cpu_to_be(fid1, fid0);
1654                 memset(id, 1, sizeof(*id));
1655                 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1656                                   fid0)->oi_dir.od_container;
1657                 ipd = osd_idx_ipd_get(env, bag);
1658                 if (unlikely(ipd == NULL))
1659                         RETURN(-ENOMEM);
1660
1661                 oh = container_of0(handle, struct osd_thandle, ot_super);
1662                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1663                                 (const struct iam_rec *)id, ipd);
1664                 osd_ipd_put(env, bag, ipd);
1665                 return(rc > 0 ? 0 : rc);
1666         }
1667
1668         inode = obj->oo_inode;
1669         ll_vfs_dq_init(inode);
1670
1671         rc = osd_quota_transfer(inode, attr);
1672         if (rc)
1673                 return rc;
1674
1675         spin_lock(&obj->oo_guard);
1676         rc = osd_inode_setattr(env, inode, attr);
1677         spin_unlock(&obj->oo_guard);
1678
1679         if (!rc)
1680                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1681         return rc;
1682 }
1683
1684 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1685                                     struct osd_object *obj,
1686                                     const char *name, const int namelen)
1687 {
1688         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1689 }
1690
1691 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1692                       cfs_umode_t mode,
1693                       struct dt_allocation_hint *hint,
1694                       struct thandle *th)
1695 {
1696         int result;
1697         struct osd_device  *osd = osd_obj2dev(obj);
1698         struct osd_thandle *oth;
1699         struct dt_object   *parent = NULL;
1700         struct inode       *inode;
1701
1702         LINVRNT(osd_invariant(obj));
1703         LASSERT(obj->oo_inode == NULL);
1704         LASSERT(obj->oo_hl_head == NULL);
1705
1706         if (S_ISDIR(mode) && ldiskfs_pdo) {
1707                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1708                 if (obj->oo_hl_head == NULL)
1709                         return -ENOMEM;
1710         }
1711
1712         oth = container_of(th, struct osd_thandle, ot_super);
1713         LASSERT(oth->ot_handle->h_transaction != NULL);
1714
1715         if (hint && hint->dah_parent)
1716                 parent = hint->dah_parent;
1717
1718         inode = ldiskfs_create_inode(oth->ot_handle,
1719                                      parent ? osd_dt_obj(parent)->oo_inode :
1720                                               osd_sb(osd)->s_root->d_inode,
1721                                      mode);
1722         if (!IS_ERR(inode)) {
1723                 /* Do not update file c/mtime in ldiskfs.
1724                  * NB: don't need any lock because no contention at this
1725                  * early stage */
1726                 inode->i_flags |= S_NOCMTIME;
1727
1728                 /* For new created object, it must be consistent,
1729                  * and it is unnecessary to scrub against it. */
1730                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1731                 obj->oo_inode = inode;
1732                 result = 0;
1733         } else {
1734                 if (obj->oo_hl_head != NULL) {
1735                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1736                         obj->oo_hl_head = NULL;
1737                 }
1738                 result = PTR_ERR(inode);
1739         }
1740         LINVRNT(osd_invariant(obj));
1741         return result;
1742 }
1743
1744 enum {
1745         OSD_NAME_LEN = 255
1746 };
1747
1748 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1749                      struct lu_attr *attr,
1750                      struct dt_allocation_hint *hint,
1751                      struct dt_object_format *dof,
1752                      struct thandle *th)
1753 {
1754         int result;
1755         struct osd_thandle *oth;
1756         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1757
1758         LASSERT(S_ISDIR(attr->la_mode));
1759
1760         oth = container_of(th, struct osd_thandle, ot_super);
1761         LASSERT(oth->ot_handle->h_transaction != NULL);
1762         result = osd_mkfile(info, obj, mode, hint, th);
1763
1764         return result;
1765 }
1766
1767 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1768                         struct lu_attr *attr,
1769                         struct dt_allocation_hint *hint,
1770                         struct dt_object_format *dof,
1771                         struct thandle *th)
1772 {
1773         int result;
1774         struct osd_thandle *oth;
1775         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1776
1777         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1778
1779         LASSERT(S_ISREG(attr->la_mode));
1780
1781         oth = container_of(th, struct osd_thandle, ot_super);
1782         LASSERT(oth->ot_handle->h_transaction != NULL);
1783
1784         result = osd_mkfile(info, obj, mode, hint, th);
1785         if (result == 0) {
1786                 LASSERT(obj->oo_inode != NULL);
1787                 if (feat->dif_flags & DT_IND_VARKEY)
1788                         result = iam_lvar_create(obj->oo_inode,
1789                                                  feat->dif_keysize_max,
1790                                                  feat->dif_ptrsize,
1791                                                  feat->dif_recsize_max,
1792                                                  oth->ot_handle);
1793                 else
1794                         result = iam_lfix_create(obj->oo_inode,
1795                                                  feat->dif_keysize_max,
1796                                                  feat->dif_ptrsize,
1797                                                  feat->dif_recsize_max,
1798                                                  oth->ot_handle);
1799
1800         }
1801         return result;
1802 }
1803
1804 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1805                      struct lu_attr *attr,
1806                      struct dt_allocation_hint *hint,
1807                      struct dt_object_format *dof,
1808                      struct thandle *th)
1809 {
1810         LASSERT(S_ISREG(attr->la_mode));
1811         return osd_mkfile(info, obj, (attr->la_mode &
1812                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1813 }
1814
1815 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1816                      struct lu_attr *attr,
1817                      struct dt_allocation_hint *hint,
1818                      struct dt_object_format *dof,
1819                      struct thandle *th)
1820 {
1821         LASSERT(S_ISLNK(attr->la_mode));
1822         return osd_mkfile(info, obj, (attr->la_mode &
1823                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1824 }
1825
1826 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1827                      struct lu_attr *attr,
1828                      struct dt_allocation_hint *hint,
1829                      struct dt_object_format *dof,
1830                      struct thandle *th)
1831 {
1832         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1833         int result;
1834
1835         LINVRNT(osd_invariant(obj));
1836         LASSERT(obj->oo_inode == NULL);
1837         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1838                 S_ISFIFO(mode) || S_ISSOCK(mode));
1839
1840         result = osd_mkfile(info, obj, mode, hint, th);
1841         if (result == 0) {
1842                 LASSERT(obj->oo_inode != NULL);
1843                 /*
1844                  * This inode should be marked dirty for i_rdev.  Currently
1845                  * that is done in the osd_attr_init().
1846                  */
1847                 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1848                                    attr->la_rdev);
1849         }
1850         LINVRNT(osd_invariant(obj));
1851         return result;
1852 }
1853
1854 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1855                               struct lu_attr *,
1856                               struct dt_allocation_hint *hint,
1857                               struct dt_object_format *dof,
1858                               struct thandle *);
1859
1860 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1861 {
1862         osd_obj_type_f result;
1863
1864         switch (type) {
1865         case DFT_DIR:
1866                 result = osd_mkdir;
1867                 break;
1868         case DFT_REGULAR:
1869                 result = osd_mkreg;
1870                 break;
1871         case DFT_SYM:
1872                 result = osd_mksym;
1873                 break;
1874         case DFT_NODE:
1875                 result = osd_mknod;
1876                 break;
1877         case DFT_INDEX:
1878                 result = osd_mk_index;
1879                 break;
1880
1881         default:
1882                 LBUG();
1883                 break;
1884         }
1885         return result;
1886 }
1887
1888
1889 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1890                         struct dt_object *parent, struct dt_object *child,
1891                         cfs_umode_t child_mode)
1892 {
1893         LASSERT(ah);
1894
1895         memset(ah, 0, sizeof(*ah));
1896         ah->dah_parent = parent;
1897         ah->dah_mode = child_mode;
1898 }
1899
1900 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1901                           struct lu_attr *attr, struct dt_object_format *dof)
1902 {
1903         struct inode   *inode = obj->oo_inode;
1904         __u64           valid = attr->la_valid;
1905         int             result;
1906
1907         attr->la_valid &= ~(LA_TYPE | LA_MODE);
1908
1909         if (dof->dof_type != DFT_NODE)
1910                 attr->la_valid &= ~LA_RDEV;
1911         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1912                 attr->la_valid &= ~LA_ATIME;
1913         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1914                 attr->la_valid &= ~LA_CTIME;
1915         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1916                 attr->la_valid &= ~LA_MTIME;
1917
1918         result = osd_quota_transfer(inode, attr);
1919         if (result)
1920                 return;
1921
1922         if (attr->la_valid != 0) {
1923                 result = osd_inode_setattr(info->oti_env, inode, attr);
1924                 /*
1925                  * The osd_inode_setattr() should always succeed here.  The
1926                  * only error that could be returned is EDQUOT when we are
1927                  * trying to change the UID or GID of the inode. However, this
1928                  * should not happen since quota enforcement is no longer
1929                  * enabled on ldiskfs (lquota takes care of it).
1930                  */
1931                 LASSERTF(result == 0, "%d", result);
1932                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1933         }
1934
1935         attr->la_valid = valid;
1936 }
1937
1938 /**
1939  * Helper function for osd_object_create()
1940  *
1941  * \retval 0, on success
1942  */
1943 static int __osd_object_create(struct osd_thread_info *info,
1944                                struct osd_object *obj, struct lu_attr *attr,
1945                                struct dt_allocation_hint *hint,
1946                                struct dt_object_format *dof,
1947                                struct thandle *th)
1948 {
1949         int     result;
1950         __u32   umask;
1951
1952         /* we drop umask so that permissions we pass are not affected */
1953         umask = current->fs->umask;
1954         current->fs->umask = 0;
1955
1956         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1957                                                   th);
1958         if (result == 0) {
1959                 osd_attr_init(info, obj, attr, dof);
1960                 osd_object_init0(obj);
1961                 /* bz 24037 */
1962                 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1963                         unlock_new_inode(obj->oo_inode);
1964         }
1965
1966         /* restore previous umask value */
1967         current->fs->umask = umask;
1968
1969         return result;
1970 }
1971
1972 /**
1973  * Helper function for osd_object_create()
1974  *
1975  * \retval 0, on success
1976  */
1977 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1978                            const struct lu_fid *fid, struct thandle *th)
1979 {
1980         struct osd_thread_info *info = osd_oti_get(env);
1981         struct osd_inode_id    *id   = &info->oti_id;
1982         struct osd_device      *osd  = osd_obj2dev(obj);
1983
1984         LASSERT(obj->oo_inode != NULL);
1985
1986         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1987         return osd_oi_insert(info, osd, fid, id, th);
1988 }
1989
1990 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1991                    const struct lu_fid *fid, struct lu_seq_range *range)
1992 {
1993         struct seq_server_site  *ss = osd_seq_site(osd);
1994         int                     rc;
1995
1996         if (fid_is_idif(fid)) {
1997                 range->lsr_flags = LU_SEQ_RANGE_OST;
1998                 range->lsr_index = fid_idif_ost_idx(fid);
1999                 return 0;
2000         }
2001
2002         if (!fid_seq_in_fldb(fid_seq(fid))) {
2003                 range->lsr_flags = LU_SEQ_RANGE_MDT;
2004                 if (ss != NULL)
2005                         /* FIXME: If ss is NULL, it suppose not get lsr_index
2006                          * at all */
2007                         range->lsr_index = ss->ss_node_id;
2008                 return 0;
2009         }
2010
2011         LASSERT(ss != NULL);
2012         range->lsr_flags = -1;
2013         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2014         if (rc != 0) {
2015                 CERROR("%s can not find "DFID": rc = %d\n",
2016                        osd_name(osd), PFID(fid), rc);
2017         }
2018         return rc;
2019 }
2020
2021 /*
2022  * Concurrency: no external locking is necessary.
2023  */
2024 static int osd_declare_object_create(const struct lu_env *env,
2025                                      struct dt_object *dt,
2026                                      struct lu_attr *attr,
2027                                      struct dt_allocation_hint *hint,
2028                                      struct dt_object_format *dof,
2029                                      struct thandle *handle)
2030 {
2031         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
2032         struct osd_thandle      *oh;
2033         int                      rc;
2034         ENTRY;
2035
2036         LASSERT(handle != NULL);
2037
2038         oh = container_of0(handle, struct osd_thandle, ot_super);
2039         LASSERT(oh->ot_handle == NULL);
2040
2041         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2042                              osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2043         if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2044                            lu_object_fid(&dt->do_lu)))
2045                 /* Reuse idle OI block may cause additional one OI block
2046                  * to be changed. */
2047                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2048                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2049
2050         /* If this is directory, then we expect . and .. to be inserted as
2051          * well. The one directory block always needs to be created for the
2052          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2053          * block), there is no danger of needing a tree for the first block.
2054          */
2055         if (attr && S_ISDIR(attr->la_mode)) {
2056                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2057                                      osd_dto_credits_noquota[DTO_WRITE_BASE]);
2058                 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2059         }
2060
2061         if (!attr)
2062                 RETURN(0);
2063
2064         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2065                                    false, false, NULL, false);
2066         if (rc != 0)
2067                 RETURN(rc);
2068
2069         /* It does fld look up inside declare, and the result will be
2070          * added to fld cache, so the following fld lookup inside insert
2071          * does not need send RPC anymore, so avoid send rpc with holding
2072          * transaction */
2073         if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2074                 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2075                 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2076                                lu_object_fid(&dt->do_lu), range);
2077
2078
2079         RETURN(rc);
2080 }
2081
2082 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2083                              struct lu_attr *attr,
2084                              struct dt_allocation_hint *hint,
2085                              struct dt_object_format *dof,
2086                              struct thandle *th)
2087 {
2088         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2089         struct osd_object      *obj    = osd_dt_obj(dt);
2090         struct osd_thread_info *info   = osd_oti_get(env);
2091         int result;
2092
2093         ENTRY;
2094
2095         LINVRNT(osd_invariant(obj));
2096         LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2097         LASSERT(osd_write_locked(env, obj));
2098         LASSERT(th != NULL);
2099
2100         if (unlikely(fid_is_acct(fid)))
2101                 /* Quota files can't be created from the kernel any more,
2102                  * 'tune2fs -O quota' will take care of creating them */
2103                 RETURN(-EPERM);
2104
2105         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2106         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2107
2108         result = __osd_object_create(info, obj, attr, hint, dof, th);
2109         if (result == 0)
2110                 result = __osd_oi_insert(env, obj, fid, th);
2111
2112         LASSERT(ergo(result == 0,
2113                      dt_object_exists(dt) && !dt_object_remote(dt)));
2114
2115         LASSERT(osd_invariant(obj));
2116         RETURN(result);
2117 }
2118
2119 /**
2120  * Called to destroy on-disk representation of the object
2121  *
2122  * Concurrency: must be locked
2123  */
2124 static int osd_declare_object_destroy(const struct lu_env *env,
2125                                       struct dt_object *dt,
2126                                       struct thandle *th)
2127 {
2128         struct osd_object  *obj = osd_dt_obj(dt);
2129         struct inode       *inode = obj->oo_inode;
2130         struct osd_thandle *oh;
2131         int                 rc;
2132         ENTRY;
2133
2134         oh = container_of0(th, struct osd_thandle, ot_super);
2135         LASSERT(oh->ot_handle == NULL);
2136         LASSERT(inode);
2137
2138         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2139                              osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2140         /* Recycle idle OI leaf may cause additional three OI blocks
2141          * to be changed. */
2142         osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2143                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2144
2145         /* one less inode */
2146         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2147                                    false, true, NULL, false);
2148         if (rc)
2149                 RETURN(rc);
2150         /* data to be truncated */
2151         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2152                                    true, true, NULL, false);
2153         RETURN(rc);
2154 }
2155
2156 static int osd_object_destroy(const struct lu_env *env,
2157                               struct dt_object *dt,
2158                               struct thandle *th)
2159 {
2160         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2161         struct osd_object      *obj = osd_dt_obj(dt);
2162         struct inode           *inode = obj->oo_inode;
2163         struct osd_device      *osd = osd_obj2dev(obj);
2164         struct osd_thandle     *oh;
2165         int                     result;
2166         ENTRY;
2167
2168         oh = container_of0(th, struct osd_thandle, ot_super);
2169         LASSERT(oh->ot_handle);
2170         LASSERT(inode);
2171         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2172
2173         if (unlikely(fid_is_acct(fid)))
2174                 RETURN(-EPERM);
2175
2176         /* Parallel control for OI scrub. For most of cases, there is no
2177          * lock contention. So it will not affect unlink performance. */
2178         mutex_lock(&inode->i_mutex);
2179         if (S_ISDIR(inode->i_mode)) {
2180                 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2181                 /* it will check/delete the inode from remote parent,
2182                  * how to optimize it? unlink performance impaction XXX */
2183                 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2184                 if (result != 0 && result != -ENOENT) {
2185                         CERROR("%s: delete inode "DFID": rc = %d\n",
2186                                osd_name(osd), PFID(fid), result);
2187                 }
2188                 spin_lock(&obj->oo_guard);
2189                 clear_nlink(inode);
2190                 spin_unlock(&obj->oo_guard);
2191                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2192         }
2193
2194         osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2195
2196         result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2197         mutex_unlock(&inode->i_mutex);
2198
2199         /* XXX: add to ext3 orphan list */
2200         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2201
2202         /* not needed in the cache anymore */
2203         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2204
2205         RETURN(0);
2206 }
2207
2208 /**
2209  * Put the fid into lustre_mdt_attrs, and then place the structure
2210  * inode's ea. This fid should not be altered during the life time
2211  * of the inode.
2212  *
2213  * \retval +ve, on success
2214  * \retval -ve, on error
2215  *
2216  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2217  */
2218 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2219                    const struct lu_fid *fid, __u64 flags)
2220 {
2221         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2222         int                      rc;
2223
2224         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2225                 return 0;
2226
2227         lustre_lma_init(lma, fid, flags);
2228         lustre_lma_swab(lma);
2229
2230         rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2231                              XATTR_CREATE);
2232         /* Someone may created the EA by race. */
2233         if (unlikely(rc == -EEXIST))
2234                 rc = 0;
2235         return rc;
2236 }
2237
2238 /**
2239  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2240  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2241  * To have compatilibility with 1.8 ldiskfs driver we need to have
2242  * magic number at start of fid data.
2243  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2244  * its inmemory API.
2245  */
2246 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2247                                   const struct dt_rec *fid)
2248 {
2249         if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2250             OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2251                 param->edp_magic = 0;
2252                 return;
2253         }
2254
2255         param->edp_magic = LDISKFS_LUFID_MAGIC;
2256         param->edp_len =  sizeof(struct lu_fid) + 1;
2257         fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2258 }
2259
2260 /**
2261  * Try to read the fid from inode ea into dt_rec.
2262  *
2263  * \param fid object fid.
2264  *
2265  * \retval 0 on success
2266  */
2267 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2268                           __u32 ino, struct lu_fid *fid,
2269                           struct osd_inode_id *id)
2270 {
2271         struct osd_thread_info *info  = osd_oti_get(env);
2272         struct inode           *inode;
2273         ENTRY;
2274
2275         osd_id_gen(id, ino, OSD_OII_NOGEN);
2276         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2277         if (IS_ERR(inode))
2278                 RETURN(PTR_ERR(inode));
2279
2280         iput(inode);
2281         RETURN(0);
2282 }
2283
2284 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2285                                         struct inode *dir,
2286                                         struct inode  *parent_dir,
2287                                         const struct dt_rec *dot_fid,
2288                                         const struct dt_rec *dot_dot_fid,
2289                                         struct osd_thandle *oth)
2290 {
2291         struct ldiskfs_dentry_param *dot_ldp;
2292         struct ldiskfs_dentry_param *dot_dot_ldp;
2293
2294         dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2295         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2296
2297         dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2298         dot_ldp->edp_magic = 0;
2299         return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2300                                         dir, dot_ldp, dot_dot_ldp);
2301 }
2302
2303 /**
2304  * Create an local agent inode for remote entry
2305  */
2306 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2307                                                   struct osd_device *osd,
2308                                                   struct osd_object *pobj,
2309                                                   const struct lu_fid *fid,
2310                                                   struct thandle *th)
2311 {
2312         struct osd_thread_info  *info = osd_oti_get(env);
2313         struct inode            *local;
2314         struct osd_thandle      *oh;
2315         int                     rc;
2316         ENTRY;
2317
2318         LASSERT(th);
2319         oh = container_of(th, struct osd_thandle, ot_super);
2320         LASSERT(oh->ot_handle->h_transaction != NULL);
2321
2322         /* FIXME: Insert index api needs to know the mode of
2323          * the remote object. Just use S_IFDIR for now */
2324         local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2325         if (IS_ERR(local)) {
2326                 CERROR("%s: create local error %d\n", osd_name(osd),
2327                        (int)PTR_ERR(local));
2328                 RETURN(local);
2329         }
2330
2331         /* Set special LMA flag for local agent inode */
2332         rc = osd_ea_fid_set(info, local, fid, LMAI_AGENT);
2333         if (rc != 0) {
2334                 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2335                        osd_name(osd), PFID(fid), rc);
2336                 RETURN(ERR_PTR(rc));
2337         }
2338
2339         rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2340                 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2341                 (const struct dt_rec *)fid, oh);
2342         if (rc != 0) {
2343                 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2344                         osd_name(osd), PFID(fid), rc);
2345                 RETURN(ERR_PTR(rc));
2346         }
2347
2348         RETURN(local);
2349 }
2350
2351 /**
2352  * Delete local agent inode for remote entry
2353  */
2354 static int osd_delete_local_agent_inode(const struct lu_env *env,
2355                                         struct osd_device *osd,
2356                                         const struct lu_fid *fid,
2357                                         __u32 ino, struct osd_thandle *oh)
2358 {
2359         struct osd_thread_info  *oti = osd_oti_get(env);
2360         struct osd_inode_id     *id = &oti->oti_id;
2361         struct inode            *inode;
2362         ENTRY;
2363
2364         id->oii_ino = le32_to_cpu(ino);
2365         id->oii_gen = OSD_OII_NOGEN;
2366         inode = osd_iget(oti, osd, id);
2367         if (IS_ERR(inode)) {
2368                 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2369                        PFID(fid), id->oii_ino, id->oii_gen);
2370                 RETURN(PTR_ERR(inode));
2371         }
2372
2373         clear_nlink(inode);
2374         mark_inode_dirty(inode);
2375         CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2376                 osd_name(osd), PFID(fid), inode->i_ino);
2377         iput(inode);
2378         RETURN(0);
2379 }
2380
2381 /**
2382  * OSD layer object create function for interoperability mode (b11826).
2383  * This is mostly similar to osd_object_create(). Only difference being, fid is
2384  * inserted into inode ea here.
2385  *
2386  * \retval   0, on success
2387  * \retval -ve, on error
2388  */
2389 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2390                                 struct lu_attr *attr,
2391                                 struct dt_allocation_hint *hint,
2392                                 struct dt_object_format *dof,
2393                                 struct thandle *th)
2394 {
2395         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2396         struct osd_object      *obj    = osd_dt_obj(dt);
2397         struct osd_thread_info *info   = osd_oti_get(env);
2398         int                     result;
2399
2400         ENTRY;
2401
2402         LASSERT(osd_invariant(obj));
2403         LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2404         LASSERT(osd_write_locked(env, obj));
2405         LASSERT(th != NULL);
2406
2407         if (unlikely(fid_is_acct(fid)))
2408                 /* Quota files can't be created from the kernel any more,
2409                  * 'tune2fs -O quota' will take care of creating them */
2410                 RETURN(-EPERM);
2411
2412         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2413         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2414
2415         result = __osd_object_create(info, obj, attr, hint, dof, th);
2416
2417         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF) && !fid_is_internal(fid))
2418                 return result;
2419
2420         if ((result == 0) &&
2421             (fid_is_last_id(fid) ||
2422              !fid_is_on_ost(info, osd_dt_dev(th->th_dev), fid)))
2423                 result = osd_ea_fid_set(info, obj->oo_inode, fid, 0);
2424
2425         if (result == 0)
2426                 result = __osd_oi_insert(env, obj, fid, th);
2427
2428         LASSERT(ergo(result == 0,
2429                      dt_object_exists(dt) && !dt_object_remote(dt)));
2430         LINVRNT(osd_invariant(obj));
2431         RETURN(result);
2432 }
2433
2434 static int osd_declare_object_ref_add(const struct lu_env *env,
2435                                       struct dt_object *dt,
2436                                       struct thandle *handle)
2437 {
2438         struct osd_thandle       *oh;
2439
2440         /* it's possible that object doesn't exist yet */
2441         LASSERT(handle != NULL);
2442
2443         oh = container_of0(handle, struct osd_thandle, ot_super);
2444         LASSERT(oh->ot_handle == NULL);
2445
2446         osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2447                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2448
2449         return 0;
2450 }
2451
2452 /*
2453  * Concurrency: @dt is write locked.
2454  */
2455 static int osd_object_ref_add(const struct lu_env *env,
2456                               struct dt_object *dt, struct thandle *th)
2457 {
2458         struct osd_object *obj = osd_dt_obj(dt);
2459         struct inode      *inode = obj->oo_inode;
2460
2461         LINVRNT(osd_invariant(obj));
2462         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2463         LASSERT(osd_write_locked(env, obj));
2464         LASSERT(th != NULL);
2465
2466         osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2467
2468         /*
2469          * DIR_NLINK feature is set for compatibility reasons if:
2470          * 1) nlinks > LDISKFS_LINK_MAX, or
2471          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2472          *
2473          * It is easier to always set this flag (rather than check and set),
2474          * since it has less overhead, and the superblock will be dirtied
2475          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2476          * do not actually care whether this flag is set or not.
2477          */
2478         spin_lock(&obj->oo_guard);
2479         /* inc_nlink from 0 may cause WARN_ON */
2480         if(inode->i_nlink == 0)
2481                 set_nlink(inode, 1);
2482         else
2483                 inc_nlink(inode);
2484         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2485                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2486                     inode->i_nlink == 2)
2487                         set_nlink(inode, 1);
2488         }
2489         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2490         spin_unlock(&obj->oo_guard);
2491         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2492         LINVRNT(osd_invariant(obj));
2493
2494         return 0;
2495 }
2496
2497 static int osd_declare_object_ref_del(const struct lu_env *env,
2498                                       struct dt_object *dt,
2499                                       struct thandle *handle)
2500 {
2501         struct osd_thandle *oh;
2502
2503         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2504         LASSERT(handle != NULL);
2505
2506         oh = container_of0(handle, struct osd_thandle, ot_super);
2507         LASSERT(oh->ot_handle == NULL);
2508
2509         osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2510                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2511
2512         return 0;
2513 }
2514
2515 /*
2516  * Concurrency: @dt is write locked.
2517  */
2518 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2519                               struct thandle *th)
2520 {
2521         struct osd_object *obj = osd_dt_obj(dt);
2522         struct inode      *inode = obj->oo_inode;
2523
2524         LINVRNT(osd_invariant(obj));
2525         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2526         LASSERT(osd_write_locked(env, obj));
2527         LASSERT(th != NULL);
2528
2529         osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2530
2531         spin_lock(&obj->oo_guard);
2532         LASSERT(inode->i_nlink > 0);
2533         drop_nlink(inode);
2534         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2535          * then the nlink count is 1. Don't let it be set to 0 or the directory
2536          * inode will be deleted incorrectly. */
2537         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2538                 set_nlink(inode, 1);
2539         spin_unlock(&obj->oo_guard);
2540         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2541         LINVRNT(osd_invariant(obj));
2542
2543         return 0;
2544 }
2545
2546 /*
2547  * Get the 64-bit version for an inode.
2548  */
2549 static int osd_object_version_get(const struct lu_env *env,
2550                                   struct dt_object *dt, dt_obj_version_t *ver)
2551 {
2552         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2553
2554         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2555                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2556         *ver = LDISKFS_I(inode)->i_fs_version;
2557         return 0;
2558 }
2559
2560 /*
2561  * Concurrency: @dt is read locked.
2562  */
2563 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2564                          struct lu_buf *buf, const char *name,
2565                          struct lustre_capa *capa)
2566 {
2567         struct osd_object      *obj    = osd_dt_obj(dt);
2568         struct inode           *inode  = obj->oo_inode;
2569         struct osd_thread_info *info   = osd_oti_get(env);
2570         struct dentry          *dentry = &info->oti_obj_dentry;
2571
2572         /* version get is not real XATTR but uses xattr API */
2573         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2574                 /* for version we are just using xattr API but change inode
2575                  * field instead */
2576                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2577                 osd_object_version_get(env, dt, buf->lb_buf);
2578                 return sizeof(dt_obj_version_t);
2579         }
2580
2581         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2582         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2583
2584         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2585                 return -EACCES;
2586
2587         return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2588 }
2589
2590
2591 static int osd_declare_xattr_set(const struct lu_env *env,
2592                                  struct dt_object *dt,
2593                                  const struct lu_buf *buf, const char *name,
2594                                  int fl, struct thandle *handle)
2595 {
2596         struct osd_thandle *oh;
2597
2598         LASSERT(handle != NULL);
2599
2600         oh = container_of0(handle, struct osd_thandle, ot_super);
2601         LASSERT(oh->ot_handle == NULL);
2602
2603         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2604                              strcmp(name, XATTR_NAME_VERSION) == 0 ?
2605                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2606                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2607
2608         return 0;
2609 }
2610
2611 /*
2612  * Set the 64-bit version for object
2613  */
2614 static void osd_object_version_set(const struct lu_env *env,
2615                                    struct dt_object *dt,
2616                                    dt_obj_version_t *new_version)
2617 {
2618         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2619
2620         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2621                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2622
2623         LDISKFS_I(inode)->i_fs_version = *new_version;
2624         /** Version is set after all inode operations are finished,
2625          *  so we should mark it dirty here */
2626         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2627 }
2628
2629 /*
2630  * Concurrency: @dt is write locked.
2631  */
2632 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2633                          const struct lu_buf *buf, const char *name, int fl,
2634                          struct thandle *handle, struct lustre_capa *capa)
2635 {
2636         struct osd_object      *obj      = osd_dt_obj(dt);
2637         struct inode           *inode    = obj->oo_inode;
2638         struct osd_thread_info *info     = osd_oti_get(env);
2639         int                     fs_flags = 0;
2640         ENTRY;
2641
2642         LASSERT(handle != NULL);
2643
2644         /* version set is not real XATTR */
2645         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2646                 /* for version we are just using xattr API but change inode
2647                  * field instead */
2648                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2649                 osd_object_version_set(env, dt, buf->lb_buf);
2650                 return sizeof(dt_obj_version_t);
2651         }
2652
2653         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2654                 return -EACCES;
2655
2656         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2657         if (fl & LU_XATTR_REPLACE)
2658                 fs_flags |= XATTR_REPLACE;
2659
2660         if (fl & LU_XATTR_CREATE)
2661                 fs_flags |= XATTR_CREATE;
2662
2663         return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2664                                fs_flags);
2665 }
2666
2667 /*
2668  * Concurrency: @dt is read locked.
2669  */
2670 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2671                           struct lu_buf *buf, struct lustre_capa *capa)
2672 {
2673         struct osd_object      *obj    = osd_dt_obj(dt);
2674         struct inode           *inode  = obj->oo_inode;
2675         struct osd_thread_info *info   = osd_oti_get(env);
2676         struct dentry          *dentry = &info->oti_obj_dentry;
2677
2678         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2679         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2680         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2681
2682         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2683                 return -EACCES;
2684
2685         dentry->d_inode = inode;
2686         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2687 }
2688
2689 static int osd_declare_xattr_del(const struct lu_env *env,
2690                                  struct dt_object *dt, const char *name,
2691                                  struct thandle *handle)
2692 {
2693         struct osd_thandle *oh;
2694
2695         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2696         LASSERT(handle != NULL);
2697
2698         oh = container_of0(handle, struct osd_thandle, ot_super);
2699         LASSERT(oh->ot_handle == NULL);
2700
2701         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2702                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2703
2704         return 0;
2705 }
2706
2707 /*
2708  * Concurrency: @dt is write locked.
2709  */
2710 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2711                          const char *name, struct thandle *handle,
2712                          struct lustre_capa *capa)
2713 {
2714         struct osd_object      *obj    = osd_dt_obj(dt);
2715         struct inode           *inode  = obj->oo_inode;
2716         struct osd_thread_info *info   = osd_oti_get(env);
2717         struct dentry          *dentry = &info->oti_obj_dentry;
2718         int                     rc;
2719
2720         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2721         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2722         LASSERT(handle != NULL);
2723
2724         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2725                 return -EACCES;
2726
2727         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2728
2729         ll_vfs_dq_init(inode);
2730         dentry->d_inode = inode;
2731         rc = inode->i_op->removexattr(dentry, name);
2732         return rc;
2733 }
2734
2735 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2736                                      struct dt_object *dt,
2737                                      struct lustre_capa *old,
2738                                      __u64 opc)
2739 {
2740         struct osd_thread_info *info = osd_oti_get(env);
2741         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2742         struct osd_object *obj = osd_dt_obj(dt);
2743         struct osd_device *dev = osd_obj2dev(obj);
2744         struct lustre_capa_key *key = &info->oti_capa_key;
2745         struct lustre_capa *capa = &info->oti_capa;
2746         struct obd_capa *oc;
2747         struct md_capainfo *ci;
2748         int rc;
2749         ENTRY;
2750
2751         if (!dev->od_fl_capa)
2752                 RETURN(ERR_PTR(-ENOENT));
2753
2754         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2755         LINVRNT(osd_invariant(obj));
2756
2757         /* renewal sanity check */
2758         if (old && osd_object_auth(env, dt, old, opc))
2759                 RETURN(ERR_PTR(-EACCES));
2760
2761         ci = md_capainfo(env);
2762         if (unlikely(!ci))
2763                 RETURN(ERR_PTR(-ENOENT));
2764
2765         switch (ci->mc_auth) {
2766         case LC_ID_NONE:
2767                 RETURN(NULL);
2768         case LC_ID_PLAIN:
2769                 capa->lc_uid = obj->oo_inode->i_uid;
2770                 capa->lc_gid = obj->oo_inode->i_gid;
2771                 capa->lc_flags = LC_ID_PLAIN;
2772                 break;
2773         case LC_ID_CONVERT: {
2774                 __u32 d[4], s[4];
2775
2776                 s[0] = obj->oo_inode->i_uid;
2777                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2778                 s[2] = obj->oo_inode->i_gid;
2779                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2780                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2781                 if (unlikely(rc))
2782                         RETURN(ERR_PTR(rc));
2783
2784                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2785                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2786                 capa->lc_flags = LC_ID_CONVERT;
2787                 break;
2788         }
2789         default:
2790                 RETURN(ERR_PTR(-EINVAL));
2791         }
2792
2793         capa->lc_fid = *fid;
2794         capa->lc_opc = opc;
2795         capa->lc_flags |= dev->od_capa_alg << 24;
2796         capa->lc_timeout = dev->od_capa_timeout;
2797         capa->lc_expiry = 0;
2798
2799         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2800         if (oc) {
2801                 LASSERT(!capa_is_expired(oc));
2802                 RETURN(oc);
2803         }
2804
2805         spin_lock(&capa_lock);
2806         *key = dev->od_capa_keys[1];
2807         spin_unlock(&capa_lock);
2808
2809         capa->lc_keyid = key->lk_keyid;
2810         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2811
2812         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2813         if (rc) {
2814                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2815                 RETURN(ERR_PTR(rc));
2816         }
2817
2818         oc = capa_add(dev->od_capa_hash, capa);
2819         RETURN(oc);
2820 }
2821
2822 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2823 {
2824         struct osd_object       *obj    = osd_dt_obj(dt);
2825         struct inode            *inode  = obj->oo_inode;
2826         struct osd_thread_info  *info   = osd_oti_get(env);
2827         struct dentry           *dentry = &info->oti_obj_dentry;
2828         struct file             *file   = &info->oti_file;
2829         int                     rc;
2830
2831         ENTRY;
2832
2833         dentry->d_inode = inode;
2834         file->f_dentry = dentry;
2835         file->f_mapping = inode->i_mapping;
2836         file->f_op = inode->i_fop;
2837 #ifndef HAVE_FILE_FSYNC_4ARGS
2838         mutex_lock(&inode->i_mutex);
2839 #endif
2840         rc = do_fsync(file, 0);
2841 #ifndef HAVE_FILE_FSYNC_4ARGS
2842         mutex_unlock(&inode->i_mutex);
2843 #endif
2844         RETURN(rc);
2845 }
2846
2847 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2848                         void **data)
2849 {
2850         struct osd_object *obj = osd_dt_obj(dt);
2851         ENTRY;
2852
2853         *data = (void *)obj->oo_inode;
2854         RETURN(0);
2855 }
2856
2857 /*
2858  * Index operations.
2859  */
2860
2861 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2862                            const struct dt_index_features *feat)
2863 {
2864         struct iam_descr *descr;
2865
2866         if (osd_object_is_root(o))
2867                 return feat == &dt_directory_features;
2868
2869         LASSERT(o->oo_dir != NULL);
2870
2871         descr = o->oo_dir->od_container.ic_descr;
2872         if (feat == &dt_directory_features) {
2873                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2874                         return 1;
2875                 else
2876                         return 0;
2877         } else {
2878                 return
2879                         feat->dif_keysize_min <= descr->id_key_size &&
2880                         descr->id_key_size <= feat->dif_keysize_max &&
2881                         feat->dif_recsize_min <= descr->id_rec_size &&
2882                         descr->id_rec_size <= feat->dif_recsize_max &&
2883                         !(feat->dif_flags & (DT_IND_VARKEY |
2884                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2885                         ergo(feat->dif_flags & DT_IND_UPDATE,
2886                              1 /* XXX check that object (and file system) is
2887                                 * writable */);
2888         }
2889 }
2890
2891 static int osd_iam_container_init(const struct lu_env *env,
2892                                   struct osd_object *obj,
2893                                   struct osd_directory *dir)
2894 {
2895         struct iam_container *bag = &dir->od_container;
2896         int result;
2897
2898         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2899         if (result != 0)
2900                 return result;
2901
2902         result = iam_container_setup(bag);
2903         if (result == 0)
2904                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2905         else
2906                 iam_container_fini(bag);
2907
2908         return result;
2909 }
2910
2911
2912 /*
2913  * Concurrency: no external locking is necessary.
2914  */
2915 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2916                          const struct dt_index_features *feat)
2917 {
2918         int                      result;
2919         int                      skip_iam = 0;
2920         struct osd_object       *obj = osd_dt_obj(dt);
2921
2922         LINVRNT(osd_invariant(obj));
2923
2924         if (osd_object_is_root(obj)) {
2925                 dt->do_index_ops = &osd_index_ea_ops;
2926                 result = 0;
2927         } else if (feat == &dt_directory_features) {
2928                 dt->do_index_ops = &osd_index_ea_ops;
2929                 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2930                         result = 0;
2931                 else
2932                         result = -ENOTDIR;
2933                 skip_iam = 1;
2934         } else if (unlikely(feat == &dt_otable_features)) {
2935                 dt->do_index_ops = &osd_otable_ops;
2936                 return 0;
2937         } else if (unlikely(feat == &dt_acct_features)) {
2938                 dt->do_index_ops = &osd_acct_index_ops;
2939                 result = 0;
2940                 skip_iam = 1;
2941         } else if (!osd_has_index(obj)) {
2942                 struct osd_directory *dir;
2943
2944                 OBD_ALLOC_PTR(dir);
2945                 if (dir != NULL) {
2946
2947                         spin_lock(&obj->oo_guard);
2948                         if (obj->oo_dir == NULL)
2949                                 obj->oo_dir = dir;
2950                         else
2951                                 /*
2952                                  * Concurrent thread allocated container data.
2953                                  */
2954                                 OBD_FREE_PTR(dir);
2955                         spin_unlock(&obj->oo_guard);
2956                         /*
2957                          * Now, that we have container data, serialize its
2958                          * initialization.
2959                          */
2960                         down_write(&obj->oo_ext_idx_sem);
2961                         /*
2962                          * recheck under lock.
2963                          */
2964                         if (!osd_has_index(obj))
2965                                 result = osd_iam_container_init(env, obj, dir);
2966                         else
2967                                 result = 0;
2968                         up_write(&obj->oo_ext_idx_sem);
2969                 } else {
2970                         result = -ENOMEM;
2971                 }
2972         } else {
2973                 result = 0;
2974         }
2975
2976         if (result == 0 && skip_iam == 0) {
2977                 if (!osd_iam_index_probe(env, obj, feat))
2978                         result = -ENOTDIR;
2979         }
2980         LINVRNT(osd_invariant(obj));
2981
2982         if (result == 0 && is_quota_glb_feat(feat) &&
2983             fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
2984                 result = osd_quota_migration(env, dt, feat);
2985
2986         return result;
2987 }
2988
2989 static int osd_otable_it_attr_get(const struct lu_env *env,
2990                                  struct dt_object *dt,
2991                                  struct lu_attr *attr,
2992                                  struct lustre_capa *capa)
2993 {
2994         attr->la_valid = 0;
2995         return 0;
2996 }
2997
2998 static const struct dt_object_operations osd_obj_ops = {
2999         .do_read_lock         = osd_object_read_lock,
3000         .do_write_lock        = osd_object_write_lock,
3001         .do_read_unlock       = osd_object_read_unlock,
3002         .do_write_unlock      = osd_object_write_unlock,
3003         .do_write_locked      = osd_object_write_locked,
3004         .do_attr_get          = osd_attr_get,
3005         .do_declare_attr_set  = osd_declare_attr_set,
3006         .do_attr_set          = osd_attr_set,
3007         .do_ah_init           = osd_ah_init,
3008         .do_declare_create    = osd_declare_object_create,
3009         .do_create            = osd_object_create,
3010         .do_declare_destroy   = osd_declare_object_destroy,
3011         .do_destroy           = osd_object_destroy,
3012         .do_index_try         = osd_index_try,
3013         .do_declare_ref_add   = osd_declare_object_ref_add,
3014         .do_ref_add           = osd_object_ref_add,
3015         .do_declare_ref_del   = osd_declare_object_ref_del,
3016         .do_ref_del           = osd_object_ref_del,
3017         .do_xattr_get         = osd_xattr_get,
3018         .do_declare_xattr_set = osd_declare_xattr_set,
3019         .do_xattr_set         = osd_xattr_set,
3020         .do_declare_xattr_del = osd_declare_xattr_del,
3021         .do_xattr_del         = osd_xattr_del,
3022         .do_xattr_list        = osd_xattr_list,
3023         .do_capa_get          = osd_capa_get,
3024         .do_object_sync       = osd_object_sync,
3025         .do_data_get          = osd_data_get,
3026 };
3027
3028 /**
3029  * dt_object_operations for interoperability mode
3030  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3031  */
3032 static const struct dt_object_operations osd_obj_ea_ops = {
3033         .do_read_lock         = osd_object_read_lock,
3034         .do_write_lock        = osd_object_write_lock,
3035         .do_read_unlock       = osd_object_read_unlock,
3036         .do_write_unlock      = osd_object_write_unlock,
3037         .do_write_locked      = osd_object_write_locked,
3038         .do_attr_get          = osd_attr_get,
3039         .do_declare_attr_set  = osd_declare_attr_set,
3040         .do_attr_set          = osd_attr_set,
3041         .do_ah_init           = osd_ah_init,
3042         .do_declare_create    = osd_declare_object_create,
3043         .do_create            = osd_object_ea_create,
3044         .do_declare_destroy   = osd_declare_object_destroy,
3045         .do_destroy           = osd_object_destroy,
3046         .do_index_try         = osd_index_try,
3047         .do_declare_ref_add   = osd_declare_object_ref_add,
3048         .do_ref_add           = osd_object_ref_add,
3049         .do_declare_ref_del   = osd_declare_object_ref_del,
3050         .do_ref_del           = osd_object_ref_del,
3051         .do_xattr_get         = osd_xattr_get,
3052         .do_declare_xattr_set = osd_declare_xattr_set,
3053         .do_xattr_set         = osd_xattr_set,
3054         .do_declare_xattr_del = osd_declare_xattr_del,
3055         .do_xattr_del         = osd_xattr_del,
3056         .do_xattr_list        = osd_xattr_list,
3057         .do_capa_get          = osd_capa_get,
3058         .do_object_sync       = osd_object_sync,
3059         .do_data_get          = osd_data_get,
3060 };
3061
3062 static const struct dt_object_operations osd_obj_otable_it_ops = {
3063         .do_attr_get    = osd_otable_it_attr_get,
3064         .do_index_try   = osd_index_try,
3065 };
3066
3067 static int osd_index_declare_iam_delete(const struct lu_env *env,
3068                                         struct dt_object *dt,
3069                                         const struct dt_key *key,
3070                                         struct thandle *handle)
3071 {
3072         struct osd_thandle    *oh;
3073
3074         oh = container_of0(handle, struct osd_thandle, ot_super);
3075         LASSERT(oh->ot_handle == NULL);
3076
3077         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3078                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3079
3080         return 0;
3081 }
3082
3083 /**
3084  *      delete a (key, value) pair from index \a dt specified by \a key
3085  *
3086  *      \param  dt      osd index object
3087  *      \param  key     key for index
3088  *      \param  rec     record reference
3089  *      \param  handle  transaction handler
3090  *
3091  *      \retval  0  success
3092  *      \retval -ve   failure
3093  */
3094
3095 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3096                                 const struct dt_key *key,
3097                                 struct thandle *handle,
3098                                 struct lustre_capa *capa)
3099 {
3100         struct osd_thread_info *oti = osd_oti_get(env);
3101         struct osd_object      *obj = osd_dt_obj(dt);
3102         struct osd_thandle     *oh;
3103         struct iam_path_descr  *ipd;
3104         struct iam_container   *bag = &obj->oo_dir->od_container;
3105         int                     rc;
3106
3107         ENTRY;
3108
3109         LINVRNT(osd_invariant(obj));
3110         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3111         LASSERT(bag->ic_object == obj->oo_inode);
3112         LASSERT(handle != NULL);
3113
3114         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3115                 RETURN(-EACCES);
3116
3117         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3118
3119         ipd = osd_idx_ipd_get(env, bag);
3120         if (unlikely(ipd == NULL))
3121                 RETURN(-ENOMEM);
3122
3123         oh = container_of0(handle, struct osd_thandle, ot_super);
3124         LASSERT(oh->ot_handle != NULL);
3125         LASSERT(oh->ot_handle->h_transaction != NULL);
3126
3127         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3128                 /* swab quota uid/gid provided by caller */
3129                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3130                 key = (const struct dt_key *)&oti->oti_quota_id;
3131         }
3132
3133         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3134         osd_ipd_put(env, bag, ipd);
3135         LINVRNT(osd_invariant(obj));
3136         RETURN(rc);
3137 }
3138
3139 static int osd_index_declare_ea_delete(const struct lu_env *env,
3140                                        struct dt_object *dt,
3141                                        const struct dt_key *key,
3142                                        struct thandle *handle)
3143 {
3144         struct osd_thandle *oh;
3145         struct inode       *inode;
3146         int                 rc;
3147         ENTRY;
3148
3149         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3150         LASSERT(handle != NULL);
3151
3152         oh = container_of0(handle, struct osd_thandle, ot_super);
3153         LASSERT(oh->ot_handle == NULL);
3154
3155         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3156                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3157
3158         inode = osd_dt_obj(dt)->oo_inode;
3159         LASSERT(inode);
3160
3161         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3162                                    true, true, NULL, false);
3163         RETURN(rc);
3164 }
3165
3166 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3167                                           struct dt_rec *fid)
3168 {
3169         struct osd_fid_pack *rec;
3170         int                  rc = -ENODATA;
3171
3172         if (de->file_type & LDISKFS_DIRENT_LUFID) {
3173                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3174                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3175         }
3176         return rc;
3177 }
3178
3179 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3180                           struct lu_fid *fid)
3181 {
3182         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
3183         struct seq_server_site  *ss = osd_seq_site(osd);
3184         int                     rc;
3185         ENTRY;
3186
3187         /* Those FID seqs, which are not in FLDB, must be local seq */
3188         if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3189                 RETURN(0);
3190
3191         rc = osd_fld_lookup(env, osd, fid, range);
3192         if (rc != 0) {
3193                 CERROR("%s: Can not lookup fld for "DFID"\n",
3194                        osd_name(osd), PFID(fid));
3195                 RETURN(rc);
3196         }
3197
3198         RETURN(ss->ss_node_id != range->lsr_index);
3199 }
3200
3201 /**
3202  * Index delete function for interoperability mode (b11826).
3203  * It will remove the directory entry added by osd_index_ea_insert().
3204  * This entry is needed to maintain name->fid mapping.
3205  *
3206  * \param key,  key i.e. file entry to be deleted
3207  *
3208  * \retval   0, on success
3209  * \retval -ve, on error
3210  */
3211 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3212                                const struct dt_key *key,
3213                                struct thandle *handle,
3214                                struct lustre_capa *capa)
3215 {
3216         struct osd_object          *obj    = osd_dt_obj(dt);
3217         struct inode               *dir    = obj->oo_inode;
3218         struct dentry              *dentry;
3219         struct osd_thandle         *oh;
3220         struct ldiskfs_dir_entry_2 *de = NULL;
3221         struct buffer_head         *bh;
3222         struct htree_lock          *hlock = NULL;
3223         struct lu_fid              *fid = &osd_oti_get(env)->oti_fid;
3224         struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
3225         int                        rc;
3226         ENTRY;
3227
3228         LINVRNT(osd_invariant(obj));
3229         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3230         LASSERT(handle != NULL);
3231
3232         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3233
3234         oh = container_of(handle, struct osd_thandle, ot_super);
3235         LASSERT(oh->ot_handle != NULL);
3236         LASSERT(oh->ot_handle->h_transaction != NULL);
3237
3238         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3239                 RETURN(-EACCES);
3240
3241         ll_vfs_dq_init(dir);
3242         dentry = osd_child_dentry_get(env, obj,
3243                                       (char *)key, strlen((char *)key));
3244
3245         if (obj->oo_hl_head != NULL) {
3246                 hlock = osd_oti_get(env)->oti_hlock;
3247                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3248                                    dir, LDISKFS_HLOCK_DEL);
3249         } else {
3250                 down_write(&obj->oo_ext_idx_sem);
3251         }
3252
3253         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3254         if (bh) {
3255                 __u32 ino = 0;
3256
3257                 /* Tried to delete local agent inode for the entries,
3258                  * If it tries to delete .., which only happens during
3259                  * rename or move the dir to /ORPHAN, even .. might
3260                  * point to the remote parent, but it do not have
3261                  * local agent inode, so we do not need check .. at all,
3262                  * Note: we need delete entry first, then inode to keep
3263                  * lfsck safe. */
3264                 if (strcmp((char *)key, dotdot) != 0) {
3265                         LASSERT(de != NULL);
3266                         rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3267                         if (rc == 0 &&
3268                             unlikely(osd_remote_fid(env, osd, fid)))
3269                                 /* Need to delete agent inode */
3270                                 ino = le32_to_cpu(de->inode);
3271                 }
3272                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3273                 brelse(bh);
3274                 if (rc == 0 && unlikely(ino != 0)) {
3275                         rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3276                                                           oh);
3277                         if (rc != 0)
3278                                 CERROR("%s: del local inode "DFID": rc = %d\n",
3279                                        osd_name(osd), PFID(fid), rc);
3280                 }
3281         } else {
3282                 rc = -ENOENT;
3283         }
3284         if (hlock != NULL)
3285                 ldiskfs_htree_unlock(hlock);
3286         else
3287                 up_write(&obj->oo_ext_idx_sem);
3288
3289         if (rc != 0)
3290                 GOTO(out, rc);
3291
3292         /* For inode on the remote MDT, .. will point to
3293          * /Agent directory, Check whether it needs to delete
3294          * from agent directory */
3295         if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3296                 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3297                                                    oh);
3298                 if (rc != 0 && rc != -ENOENT) {
3299                         CERROR("%s: delete agent inode "DFID": rc = %d\n",
3300                                osd_name(osd), PFID(fid), rc);
3301                 }
3302
3303                 if (rc == -ENOENT)
3304                         rc = 0;
3305
3306                 GOTO(out, rc);
3307         }
3308 out:
3309
3310         LASSERT(osd_invariant(obj));
3311         RETURN(rc);
3312 }
3313
3314 /**
3315  *      Lookup index for \a key and copy record to \a rec.
3316  *
3317  *      \param  dt      osd index object
3318  *      \param  key     key for index
3319  *      \param  rec     record reference
3320  *
3321  *      \retval  +ve  success : exact mach
3322  *      \retval  0    return record with key not greater than \a key
3323  *      \retval -ve   failure
3324  */
3325 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3326                                 struct dt_rec *rec, const struct dt_key *key,
3327                                 struct lustre_capa *capa)
3328 {
3329         struct osd_object      *obj = osd_dt_obj(dt);
3330         struct iam_path_descr  *ipd;
3331         struct iam_container   *bag = &obj->oo_dir->od_container;
3332         struct osd_thread_info *oti = osd_oti_get(env);
3333         struct iam_iterator    *it = &oti->oti_idx_it;
3334         struct iam_rec         *iam_rec;
3335         int                     rc;
3336
3337         ENTRY;
3338
3339         LASSERT(osd_invariant(obj));
3340         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3341         LASSERT(bag->ic_object == obj->oo_inode);
3342
3343         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3344                 RETURN(-EACCES);
3345
3346         ipd = osd_idx_ipd_get(env, bag);
3347         if (IS_ERR(ipd))
3348                 RETURN(-ENOMEM);
3349
3350         /* got ipd now we can start iterator. */
3351         iam_it_init(it, bag, 0, ipd);
3352
3353         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3354                 /* swab quota uid/gid provided by caller */
3355                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3356                 key = (const struct dt_key *)&oti->oti_quota_id;
3357         }
3358
3359         rc = iam_it_get(it, (struct iam_key *)key);
3360         if (rc >= 0) {
3361                 if (S_ISDIR(obj->oo_inode->i_mode))
3362                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3363                 else
3364                         iam_rec = (struct iam_rec *) rec;
3365
3366                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3367
3368                 if (S_ISDIR(obj->oo_inode->i_mode))
3369                         osd_fid_unpack((struct lu_fid *) rec,
3370                                        (struct osd_fid_pack *)iam_rec);
3371                 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3372                         osd_quota_unpack(obj, rec);
3373         }
3374
3375         iam_it_put(it);
3376         iam_it_fini(it);
3377         osd_ipd_put(env, bag, ipd);
3378
3379         LINVRNT(osd_invariant(obj));
3380
3381         RETURN(rc);
3382 }
3383
3384 static int osd_index_declare_iam_insert(const struct lu_env *env,
3385                                         struct dt_object *dt,
3386                                         const struct dt_rec *rec,
3387                                         const struct dt_key *key,
3388                                         struct thandle *handle)
3389 {
3390         struct osd_thandle *oh;
3391
3392         LASSERT(handle != NULL);
3393
3394         oh = container_of0(handle, struct osd_thandle, ot_super);
3395         LASSERT(oh->ot_handle == NULL);
3396
3397         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3398                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3399
3400         return 0;
3401 }
3402
3403 /**
3404  *      Inserts (key, value) pair in \a dt index object.
3405  *
3406  *      \param  dt      osd index object
3407  *      \param  key     key for index
3408  *      \param  rec     record reference
3409  *      \param  th      transaction handler
3410  *
3411  *      \retval  0  success
3412  *      \retval -ve failure
3413  */
3414 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3415                                 const struct dt_rec *rec,
3416                                 const struct dt_key *key, struct thandle *th,
3417                                 struct lustre_capa *capa, int ignore_quota)
3418 {
3419         struct osd_object     *obj = osd_dt_obj(dt);
3420         struct iam_path_descr *ipd;
3421         struct osd_thandle    *oh;
3422         struct iam_container  *bag = &obj->oo_dir->od_container;
3423         struct osd_thread_info *oti = osd_oti_get(env);
3424         struct iam_rec         *iam_rec;
3425         int                     rc;
3426
3427         ENTRY;
3428
3429         LINVRNT(osd_invariant(obj));
3430         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3431         LASSERT(bag->ic_object == obj->oo_inode);
3432         LASSERT(th != NULL);
3433
3434         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3435                 RETURN(-EACCES);
3436
3437         osd_trans_exec_op(env, th, OSD_OT_INSERT);
3438
3439         ipd = osd_idx_ipd_get(env, bag);
3440         if (unlikely(ipd == NULL))
3441                 RETURN(-ENOMEM);
3442
3443         oh = container_of0(th, struct osd_thandle, ot_super);
3444         LASSERT(oh->ot_handle != NULL);
3445         LASSERT(oh->ot_handle->h_transaction != NULL);
3446         if (S_ISDIR(obj->oo_inode->i_mode)) {
3447                 iam_rec = (struct iam_rec *)oti->oti_ldp;
3448                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3449         } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3450                 /* pack quota uid/gid */
3451                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3452                 key = (const struct dt_key *)&oti->oti_quota_id;
3453                 /* pack quota record */
3454                 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3455                 iam_rec = (struct iam_rec *)rec;
3456         } else {
3457                 iam_rec = (struct iam_rec *)rec;
3458         }
3459
3460         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3461                         iam_rec, ipd);
3462         osd_ipd_put(env, bag, ipd);
3463         LINVRNT(osd_invariant(obj));
3464         RETURN(rc);
3465 }
3466
3467 /**
3468  * Calls ldiskfs_add_entry() to add directory entry
3469  * into the directory. This is required for
3470  * interoperability mode (b11826)
3471  *
3472  * \retval   0, on success
3473  * \retval -ve, on error
3474  */
3475 static int __osd_ea_add_rec(struct osd_thread_info *info,
3476                             struct osd_object *pobj, struct inode  *cinode,
3477                             const char *name, const struct dt_rec *fid,
3478                             struct htree_lock *hlock, struct thandle *th)
3479 {
3480         struct ldiskfs_dentry_param *ldp;
3481         struct dentry               *child;
3482         struct osd_thandle          *oth;
3483         int                          rc;
3484
3485         oth = container_of(th, struct osd_thandle, ot_super);
3486         LASSERT(oth->ot_handle != NULL);
3487         LASSERT(oth->ot_handle->h_transaction != NULL);
3488         LASSERT(pobj->oo_inode);
3489
3490         ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3491         if (unlikely(pobj->oo_inode ==
3492                      osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3493                 ldp->edp_magic = 0;
3494         else
3495                 osd_get_ldiskfs_dirent_param(ldp, fid);
3496         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3497         child->d_fsdata = (void *)ldp;
3498         ll_vfs_dq_init(pobj->oo_inode);
3499         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3500
3501         RETURN(rc);
3502 }
3503
3504 /**
3505  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3506  * into the directory.Also sets flags into osd object to
3507  * indicate dot and dotdot are created. This is required for
3508  * interoperability mode (b11826)
3509  *
3510  * \param dir   directory for dot and dotdot fixup.
3511  * \param obj   child object for linking
3512  *
3513  * \retval   0, on success
3514  * \retval -ve, on error
3515  */
3516 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3517                               struct osd_object *dir,
3518                               struct inode  *parent_dir, const char *name,
3519                               const struct dt_rec *dot_fid,
3520                               const struct dt_rec *dot_dot_fid,
3521                               struct thandle *th)
3522 {
3523         struct inode                *inode = dir->oo_inode;
3524         struct osd_thandle          *oth;
3525         int result = 0;
3526
3527         oth = container_of(th, struct osd_thandle, ot_super);
3528         LASSERT(oth->ot_handle->h_transaction != NULL);
3529         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3530
3531         if (strcmp(name, dot) == 0) {
3532                 if (dir->oo_compat_dot_created) {
3533                         result = -EEXIST;
3534                 } else {
3535                         LASSERT(inode == parent_dir);
3536                         dir->oo_compat_dot_created = 1;
3537                         result = 0;
3538                 }
3539         } else if (strcmp(name, dotdot) == 0) {
3540                 if (!dir->oo_compat_dot_created)
3541                         return -EINVAL;
3542                 /* in case of rename, dotdot is already created */
3543                 if (dir->oo_compat_dotdot_created) {
3544                         return __osd_ea_add_rec(info, dir, parent_dir, name,
3545                                                 dot_dot_fid, NULL, th);
3546                 }
3547
3548                 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3549                                                 parent_dir, dot_fid,
3550                                                 dot_dot_fid, oth);
3551                 if (result == 0)
3552                         dir->oo_compat_dotdot_created = 1;
3553         }
3554
3555         return result;
3556 }
3557
3558
3559 /**
3560  * It will call the appropriate osd_add* function and return the
3561  * value, return by respective functions.
3562  */
3563 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3564                           struct inode *cinode, const char *name,
3565                           const struct dt_rec *fid, struct thandle *th)
3566 {
3567         struct osd_thread_info *info   = osd_oti_get(env);
3568         struct htree_lock      *hlock;
3569         int                     rc;
3570
3571         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3572
3573         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3574                                                    name[2] =='\0'))) {
3575                 if (hlock != NULL) {
3576                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3577                                            pobj->oo_inode, 0);
3578                 } else {
3579                         down_write(&pobj->oo_ext_idx_sem);
3580                 }
3581                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3582                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3583                                         fid, th);
3584         } else {
3585                 if (hlock != NULL) {
3586                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3587                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
3588                 } else {
3589                         down_write(&pobj->oo_ext_idx_sem);
3590                 }
3591
3592                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3593                         struct lu_fid *tfid = &info->oti_fid;
3594
3595                         *tfid = *(const struct lu_fid *)fid;
3596                         tfid->f_ver = ~0;
3597                         rc = __osd_ea_add_rec(info, pobj, cinode, name,
3598                                               (const struct dt_rec *)tfid,
3599                                               hlock, th);
3600                 } else {
3601                         rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3602                                               hlock, th);
3603                 }
3604         }
3605         if (hlock != NULL)
3606                 ldiskfs_htree_unlock(hlock);
3607         else
3608                 up_write(&pobj->oo_ext_idx_sem);
3609
3610         return rc;
3611 }
3612
3613 static void
3614 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3615                       struct osd_idmap_cache *oic)
3616 {
3617         struct osd_scrub    *scrub = &dev->od_scrub;
3618         struct lu_fid       *fid   = &oic->oic_fid;
3619         struct osd_inode_id *id    = &oti->oti_id;
3620         int                  once  = 0;
3621         int                  rc;
3622         ENTRY;
3623
3624         if (!fid_is_norm(fid) && !fid_is_igif(fid))
3625                 RETURN_EXIT;
3626
3627 again:
3628         rc = osd_oi_lookup(oti, dev, fid, id, true);
3629         if (rc != 0 && rc != -ENOENT)
3630                 RETURN_EXIT;
3631
3632         if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3633                 RETURN_EXIT;
3634
3635         if (thread_is_running(&scrub->os_thread)) {
3636                 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3637                 /* There is race condition between osd_oi_lookup and OI scrub.
3638                  * The OI scrub finished just after osd_oi_lookup() failure.
3639                  * Under such case, it is unnecessary to trigger OI scrub again,
3640                  * but try to call osd_oi_lookup() again. */
3641                 if (unlikely(rc == -EAGAIN))
3642                         goto again;
3643
3644                 RETURN_EXIT;
3645         }
3646
3647         if (!dev->od_noscrub && ++once == 1) {
3648                 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3649                        PFID(fid));
3650                 rc = osd_scrub_start(dev);
3651                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3652                                ", rc = %d [2]\n",
3653                                LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3654                                PFID(fid), rc);
3655                 if (rc == 0)
3656                         goto again;
3657         }
3658
3659         EXIT;
3660 }
3661
3662 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3663                                struct osd_device *dev,
3664                                struct osd_idmap_cache *oic,
3665                                struct lu_fid *fid, __u32 ino)
3666 {
3667         struct lustre_mdt_attrs *lma   = &oti->oti_mdt_attrs;
3668         struct inode            *inode;
3669         int                      rc;
3670
3671         osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3672         inode = osd_iget(oti, dev, &oic->oic_lid);
3673         if (IS_ERR(inode)) {
3674                 fid_zero(&oic->oic_fid);
3675                 return PTR_ERR(inode);
3676         }
3677
3678         rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3679         iput(inode);
3680         if (rc != 0)
3681                 fid_zero(&oic->oic_fid);
3682         else
3683                 *fid = oic->oic_fid = lma->lma_self_fid;
3684         return rc;
3685 }
3686
3687 /**
3688  * Calls ->lookup() to find dentry. From dentry get inode and
3689  * read inode's ea to get fid. This is required for  interoperability
3690  * mode (b11826)
3691  *
3692  * \retval   0, on success
3693  * \retval -ve, on error
3694  */
3695 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3696                              struct dt_rec *rec, const struct dt_key *key)
3697 {
3698         struct inode               *dir    = obj->oo_inode;
3699         struct dentry              *dentry;
3700         struct ldiskfs_dir_entry_2 *de;
3701         struct buffer_head         *bh;
3702         struct lu_fid              *fid = (struct lu_fid *) rec;
3703         struct htree_lock          *hlock = NULL;
3704         int                         ino;
3705         int                         rc;
3706         ENTRY;
3707
3708         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3709
3710         dentry = osd_child_dentry_get(env, obj,
3711                                       (char *)key, strlen((char *)key));
3712
3713         if (obj->oo_hl_head != NULL) {
3714                 hlock = osd_oti_get(env)->oti_hlock;
3715                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3716                                    dir, LDISKFS_HLOCK_LOOKUP);
3717         } else {
3718                 down_read(&obj->oo_ext_idx_sem);
3719         }
3720
3721         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3722         if (bh) {
3723                 struct osd_thread_info *oti = osd_oti_get(env);
3724                 struct osd_inode_id *id = &oti->oti_id;
3725                 struct osd_idmap_cache *oic = &oti->oti_cache;
3726                 struct osd_device *dev = osd_obj2dev(obj);
3727                 struct osd_scrub *scrub = &dev->od_scrub;
3728                 struct scrub_file *sf = &scrub->os_file;
3729
3730                 ino = le32_to_cpu(de->inode);
3731                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3732                         brelse(bh);
3733                         rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3734                         GOTO(out, rc);
3735                 }
3736
3737                 rc = osd_get_fid_from_dentry(de, rec);
3738
3739                 /* done with de, release bh */
3740                 brelse(bh);
3741                 if (rc != 0)
3742                         rc = osd_ea_fid_get(env, obj, ino, fid, id);
3743                 else
3744                         osd_id_gen(id, ino, OSD_OII_NOGEN);
3745                 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3746                         fid_zero(&oic->oic_fid);
3747                         GOTO(out, rc);
3748                 }
3749
3750                 oic->oic_lid = *id;
3751                 oic->oic_fid = *fid;
3752                 if ((scrub->os_pos_current <= ino) &&
3753                     ((sf->sf_flags & SF_INCONSISTENT) ||
3754                      (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3755                      ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3756                                       sf->sf_oi_bitmap)))
3757                         osd_consistency_check(oti, dev, oic);
3758         } else {
3759                 rc = -ENOENT;
3760         }
3761
3762         GOTO(out, rc);
3763
3764 out:
3765         if (hlock != NULL)
3766                 ldiskfs_htree_unlock(hlock);
3767         else
3768                 up_read(&obj->oo_ext_idx_sem);
3769         return rc;
3770 }
3771
3772 /**
3773  * Find the osd object for given fid.
3774  *
3775  * \param fid need to find the osd object having this fid
3776  *
3777  * \retval osd_object on success
3778  * \retval        -ve on error
3779  */
3780 struct osd_object *osd_object_find(const struct lu_env *env,
3781                                    struct dt_object *dt,
3782                                    const struct lu_fid *fid)
3783 {
3784         struct lu_device  *ludev = dt->do_lu.lo_dev;
3785         struct osd_object *child = NULL;
3786         struct lu_object  *luch;
3787         struct lu_object  *lo;
3788
3789         /*
3790          * at this point topdev might not exist yet
3791          * (i.e. MGS is preparing profiles). so we can
3792          * not rely on topdev and instead lookup with
3793          * our device passed as topdev. this can't work
3794          * if the object isn't cached yet (as osd doesn't
3795          * allocate lu_header). IOW, the object must be
3796          * in the cache, otherwise lu_object_alloc() crashes
3797          * -bzzz
3798          */
3799         luch = lu_object_find_at(env, ludev, fid, NULL);
3800         if (!IS_ERR(luch)) {
3801                 if (lu_object_exists(luch)) {
3802                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3803                         if (lo != NULL)
3804                                 child = osd_obj(lo);
3805                         else
3806                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3807                                                 "lu_object can't be located"
3808                                                 DFID"\n", PFID(fid));
3809
3810                         if (child == NULL) {
3811                                 lu_object_put(env, luch);
3812                                 CERROR("Unable to get osd_object\n");
3813                                 child = ERR_PTR(-ENOENT);
3814                         }
3815                 } else {
3816                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
3817                                         "lu_object does not exists "DFID"\n",
3818                                         PFID(fid));
3819                         lu_object_put(env, luch);
3820                         child = ERR_PTR(-ENOENT);
3821                 }
3822         } else
3823                 child = (void *)luch;
3824
3825         return child;
3826 }
3827
3828 /**
3829  * Put the osd object once done with it.
3830  *
3831  * \param obj osd object that needs to be put
3832  */
3833 static inline void osd_object_put(const struct lu_env *env,
3834                                   struct osd_object *obj)
3835 {
3836         lu_object_put(env, &obj->oo_dt.do_lu);
3837 }
3838
3839 static int osd_index_declare_ea_insert(const struct lu_env *env,
3840                                        struct dt_object *dt,
3841                                        const struct dt_rec *rec,
3842                                        const struct dt_key *key,
3843                                        struct thandle *handle)
3844 {
3845         struct osd_thandle      *oh;
3846         struct osd_device       *osd   = osd_dev(dt->do_lu.lo_dev);
3847         struct lu_fid           *fid = (struct lu_fid *)rec;
3848         int                     rc;
3849         ENTRY;
3850
3851         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3852         LASSERT(handle != NULL);
3853
3854         oh = container_of0(handle, struct osd_thandle, ot_super);
3855         LASSERT(oh->ot_handle == NULL);
3856
3857         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3858                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3859
3860         if (osd_dt_obj(dt)->oo_inode == NULL) {
3861                 const char *name  = (const char *)key;
3862                 /* Object is not being created yet. Only happens when
3863                  *     1. declare directory create
3864                  *     2. declare insert .
3865                  *     3. declare insert ..
3866                  */
3867                 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3868         } else {
3869                 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3870
3871                 /* We ignore block quota on meta pool (MDTs), so needn't
3872                  * calculate how many blocks will be consumed by this index
3873                  * insert */
3874                 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3875                                            oh, true, true, NULL, false);
3876         }
3877
3878         if (fid == NULL)
3879                 RETURN(0);
3880
3881         rc = osd_remote_fid(env, osd, fid);
3882         if (rc <= 0)
3883                 RETURN(rc);
3884
3885         rc = 0;
3886
3887         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3888                              osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
3889         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3890                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3891         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3892                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3893
3894         RETURN(rc);
3895 }
3896
3897 /**
3898  * Index add function for interoperability mode (b11826).
3899  * It will add the directory entry.This entry is needed to
3900  * maintain name->fid mapping.
3901  *
3902  * \param key it is key i.e. file entry to be inserted
3903  * \param rec it is value of given key i.e. fid
3904  *
3905  * \retval   0, on success
3906  * \retval -ve, on error
3907  */
3908 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3909                                const struct dt_rec *rec,
3910                                const struct dt_key *key, struct thandle *th,
3911                                struct lustre_capa *capa, int ignore_quota)
3912 {
3913         struct osd_object       *obj = osd_dt_obj(dt);
3914         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
3915         struct lu_fid           *fid = (struct lu_fid *) rec;
3916         const char              *name = (const char *)key;
3917         struct osd_thread_info  *oti   = osd_oti_get(env);
3918         struct osd_inode_id     *id    = &oti->oti_id;
3919         struct inode            *child_inode = NULL;
3920         struct osd_object       *child = NULL;
3921         int                     rc;
3922         ENTRY;
3923
3924         LASSERT(osd_invariant(obj));
3925         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3926         LASSERT(th != NULL);
3927
3928         osd_trans_exec_op(env, th, OSD_OT_INSERT);
3929
3930         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3931                 RETURN(-EACCES);
3932
3933         LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
3934
3935         rc = osd_remote_fid(env, osd, fid);
3936         if (rc < 0) {
3937                 CERROR("%s: Can not find object "DFID" rc %d\n",
3938                        osd_name(osd), PFID(fid), rc);
3939                 RETURN(rc);
3940         }
3941
3942         if (rc == 1) {
3943                 /* Insert remote entry */
3944                 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
3945                         struct osd_mdobj_map    *omm = osd->od_mdt_map;
3946                         struct osd_thandle      *oh;
3947
3948                         /* If parent on remote MDT, we need put this object
3949                          * under AGENT */
3950                         oh = container_of(th, typeof(*oh), ot_super);
3951                         rc = osd_add_to_remote_parent(env, osd, obj, oh);
3952                         if (rc != 0) {
3953                                 CERROR("%s: add "DFID" error: rc = %d\n",
3954                                        osd_name(osd),
3955                                        PFID(lu_object_fid(&dt->do_lu)), rc);
3956                                 RETURN(rc);
3957                         }
3958
3959                         child_inode = igrab(omm->omm_remote_parent->d_inode);
3960                 } else {
3961                         child_inode = osd_create_local_agent_inode(env, osd,
3962                                                                    obj, fid,
3963                                                                    th);
3964                         if (IS_ERR(child_inode))
3965                                 RETURN(PTR_ERR(child_inode));
3966                 }
3967         } else {
3968                 /* Insert local entry */
3969                 child = osd_object_find(env, dt, fid);
3970                 if (IS_ERR(child)) {
3971                         CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
3972                                osd_name(osd), PFID(fid),
3973                                id->oii_ino, id->oii_gen,
3974                                (int)PTR_ERR(child_inode));
3975                         RETURN(PTR_ERR(child_inode));
3976                 }
3977                 child_inode = igrab(child->oo_inode);
3978         }
3979
3980         rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
3981
3982         iput(child_inode);
3983         if (child != NULL)
3984                 osd_object_put(env, child);
3985         LASSERT(osd_invariant(obj));
3986         RETURN(rc);
3987 }
3988
3989 /**
3990  *  Initialize osd Iterator for given osd index object.
3991  *
3992  *  \param  dt      osd index object
3993  */
3994
3995 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3996                                      struct dt_object *dt,
3997                                      __u32 unused,
3998                                      struct lustre_capa *capa)
3999 {
4000         struct osd_it_iam      *it;
4001         struct osd_thread_info *oti = osd_oti_get(env);
4002         struct osd_object      *obj = osd_dt_obj(dt);
4003         struct lu_object       *lo  = &dt->do_lu;
4004         struct iam_path_descr  *ipd;
4005         struct iam_container   *bag = &obj->oo_dir->od_container;
4006
4007         LASSERT(lu_object_exists(lo));
4008
4009         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4010                 return ERR_PTR(-EACCES);
4011
4012         it = &oti->oti_it;
4013         ipd = osd_it_ipd_get(env, bag);
4014         if (likely(ipd != NULL)) {
4015                 it->oi_obj = obj;
4016                 it->oi_ipd = ipd;
4017                 lu_object_get(lo);
4018                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4019                 return (struct dt_it *)it;
4020         }
4021         return ERR_PTR(-ENOMEM);
4022 }
4023
4024 /**
4025  * free given Iterator.
4026  */
4027
4028 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4029 {
4030         struct osd_it_iam *it = (struct osd_it_iam *)di;
4031         struct osd_object *obj = it->oi_obj;
4032
4033         iam_it_fini(&it->oi_it);
4034         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4035         lu_object_put(env, &obj->oo_dt.do_lu);
4036 }
4037
4038 /**
4039  *  Move Iterator to record specified by \a key
4040  *
4041  *  \param  di      osd iterator
4042  *  \param  key     key for index
4043  *
4044  *  \retval +ve  di points to record with least key not larger than key
4045  *  \retval  0   di points to exact matched key
4046  *  \retval -ve  failure
4047  */
4048
4049 static int osd_it_iam_get(const struct lu_env *env,
4050                           struct dt_it *di, const struct dt_key *key)
4051 {
4052         struct osd_thread_info  *oti = osd_oti_get(env);
4053         struct osd_it_iam       *it = (struct osd_it_iam *)di;
4054
4055         if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4056                 /* swab quota uid/gid */
4057                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4058                 key = (struct dt_key *)&oti->oti_quota_id;
4059         }
4060
4061         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4062 }
4063
4064 /**
4065  *  Release Iterator
4066  *
4067  *  \param  di      osd iterator
4068  */
4069 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4070 {
4071         struct osd_it_iam *it = (struct osd_it_iam *)di;
4072
4073         iam_it_put(&it->oi_it);
4074 }
4075
4076 /**
4077  *  Move iterator by one record
4078  *
4079  *  \param  di      osd iterator
4080  *
4081  *  \retval +1   end of container reached
4082  *  \retval  0   success
4083  *  \retval -ve  failure
4084  */
4085
4086 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4087 {
4088         struct osd_it_iam *it = (struct osd_it_iam *)di;
4089
4090         return iam_it_next(&it->oi_it);
4091 }
4092
4093 /**
4094  * Return pointer to the key under iterator.
4095  */
4096
4097 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4098                                  const struct dt_it *di)
4099 {
4100         struct osd_thread_info *oti = osd_oti_get(env);
4101         struct osd_it_iam      *it = (struct osd_it_iam *)di;
4102         struct osd_object      *obj = it->oi_obj;
4103         struct dt_key          *key;
4104
4105         key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4106
4107         if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4108                 /* swab quota uid/gid */
4109                 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4110                 key = (struct dt_key *)&oti->oti_quota_id;
4111         }
4112
4113         return key;
4114 }
4115
4116 /**
4117  * Return size of key under iterator (in bytes)
4118  */
4119
4120 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4121 {
4122         struct osd_it_iam *it = (struct osd_it_iam *)di;
4123
4124         return iam_it_key_size(&it->oi_it);
4125 }
4126
4127 static inline void
4128 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4129 {
4130         /* check if file type is required */
4131         if (ent->lde_attrs & LUDA_TYPE) {
4132                 struct luda_type *lt;
4133                 int align = sizeof(*lt) - 1;
4134
4135                 len = (len + align) & ~align;
4136                 lt = (struct luda_type *)(ent->lde_name + len);
4137                 lt->lt_type = cpu_to_le16(DTTOIF(type));
4138         }
4139
4140         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4141 }
4142
4143 /**
4144  * build lu direct from backend fs dirent.
4145  */
4146
4147 static inline void
4148 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4149                    char *name, __u16 namelen, __u16 type, __u32 attr)
4150 {
4151         ent->lde_attrs = attr | LUDA_FID;
4152         fid_cpu_to_le(&ent->lde_fid, fid);
4153
4154         ent->lde_hash = cpu_to_le64(offset);
4155         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4156
4157         strncpy(ent->lde_name, name, namelen);
4158         ent->lde_name[namelen] = '\0';
4159         ent->lde_namelen = cpu_to_le16(namelen);
4160
4161         /* append lustre attributes */
4162         osd_it_append_attrs(ent, namelen, type);
4163 }
4164
4165 /**
4166  * Return pointer to the record under iterator.
4167  */
4168 static int osd_it_iam_rec(const struct lu_env *env,
4169                           const struct dt_it *di,
4170                           struct dt_rec *dtrec, __u32 attr)
4171 {
4172         struct osd_it_iam      *it   = (struct osd_it_iam *)di;
4173         struct osd_thread_info *info = osd_oti_get(env);
4174         ENTRY;
4175
4176         if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4177                 const struct osd_fid_pack *rec;
4178                 struct lu_fid             *fid = &info->oti_fid;
4179                 struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
4180                 char                      *name;
4181                 int                        namelen;
4182                 __u64                      hash;
4183                 int                        rc;
4184
4185                 name = (char *)iam_it_key_get(&it->oi_it);
4186                 if (IS_ERR(name))
4187                         RETURN(PTR_ERR(name));
4188
4189                 namelen = iam_it_key_size(&it->oi_it);
4190
4191                 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4192                 if (IS_ERR(rec))
4193                         RETURN(PTR_ERR(rec));
4194
4195                 rc = osd_fid_unpack(fid, rec);
4196                 if (rc)
4197                         RETURN(rc);
4198
4199                 hash = iam_it_store(&it->oi_it);
4200
4201                 /* IAM does not store object type in IAM index (dir) */
4202                 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4203                                    0, LUDA_FID);
4204         } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4205                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4206                            (struct iam_rec *)dtrec);
4207                 osd_quota_unpack(it->oi_obj, dtrec);
4208         } else {
4209                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4210                            (struct iam_rec *)dtrec);
4211         }
4212
4213         RETURN(0);
4214 }
4215
4216 /**
4217  * Returns cookie for current Iterator position.
4218  */
4219 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4220 {
4221         struct osd_it_iam *it = (struct osd_it_iam *)di;
4222
4223         return iam_it_store(&it->oi_it);
4224 }
4225
4226 /**
4227  * Restore iterator from cookie.
4228  *
4229  * \param  di      osd iterator
4230  * \param  hash    Iterator location cookie
4231  *
4232  * \retval +ve  di points to record with least key not larger than key.
4233  * \retval  0   di points to exact matched key
4234  * \retval -ve  failure
4235  */
4236
4237 static int osd_it_iam_load(const struct lu_env *env,
4238                            const struct dt_it *di, __u64 hash)
4239 {
4240         struct osd_it_iam *it = (struct osd_it_iam *)di;
4241
4242         return iam_it_load(&it->oi_it, hash);
4243 }
4244
4245 static const struct dt_index_operations osd_index_iam_ops = {
4246         .dio_lookup         = osd_index_iam_lookup,
4247         .dio_declare_insert = osd_index_declare_iam_insert,
4248         .dio_insert         = osd_index_iam_insert,
4249         .dio_declare_delete = osd_index_declare_iam_delete,
4250         .dio_delete         = osd_index_iam_delete,
4251         .dio_it     = {
4252                 .init     = osd_it_iam_init,
4253                 .fini     = osd_it_iam_fini,
4254                 .get      = osd_it_iam_get,
4255                 .put      = osd_it_iam_put,
4256                 .next     = osd_it_iam_next,
4257                 .key      = osd_it_iam_key,
4258                 .key_size = osd_it_iam_key_size,
4259                 .rec      = osd_it_iam_rec,
4260                 .store    = osd_it_iam_store,
4261                 .load     = osd_it_iam_load
4262         }
4263 };
4264
4265
4266 /**
4267  * Creates or initializes iterator context.
4268  *
4269  * \retval struct osd_it_ea, iterator structure on success
4270  *
4271  */
4272 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4273                                     struct dt_object *dt,
4274                                     __u32 attr,
4275                                     struct lustre_capa *capa)
4276 {
4277         struct osd_object       *obj  = osd_dt_obj(dt);
4278         struct osd_thread_info  *info = osd_oti_get(env);
4279         struct osd_it_ea        *it   = &info->oti_it_ea;
4280         struct lu_object        *lo   = &dt->do_lu;
4281         struct dentry           *obj_dentry = &info->oti_it_dentry;
4282         ENTRY;
4283         LASSERT(lu_object_exists(lo));
4284
4285         obj_dentry->d_inode = obj->oo_inode;
4286         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4287         obj_dentry->d_name.hash = 0;
4288
4289         it->oie_rd_dirent       = 0;
4290         it->oie_it_dirent       = 0;
4291         it->oie_dirent          = NULL;
4292         it->oie_buf             = info->oti_it_ea_buf;
4293         it->oie_obj             = obj;
4294         it->oie_file.f_pos      = 0;
4295         it->oie_file.f_dentry   = obj_dentry;
4296         if (attr & LUDA_64BITHASH)
4297                 it->oie_file.f_mode |= FMODE_64BITHASH;
4298         else
4299                 it->oie_file.f_mode |= FMODE_32BITHASH;
4300         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
4301         it->oie_file.f_op         = obj->oo_inode->i_fop;
4302         it->oie_file.private_data = NULL;
4303         lu_object_get(lo);
4304         RETURN((struct dt_it *) it);
4305 }
4306
4307 /**
4308  * Destroy or finishes iterator context.
4309  *
4310  * \param di iterator structure to be destroyed
4311  */
4312 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4313 {
4314         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
4315         struct osd_object    *obj  = it->oie_obj;
4316         struct inode       *inode  = obj->oo_inode;
4317
4318         ENTRY;
4319         it->oie_file.f_op->release(inode, &it->oie_file);
4320         lu_object_put(env, &obj->oo_dt.do_lu);
4321         EXIT;
4322 }
4323
4324 /**
4325  * It position the iterator at given key, so that next lookup continues from
4326  * that key Or it is similar to dio_it->load() but based on a key,
4327  * rather than file position.
4328  *
4329  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4330  * to the beginning.
4331  *
4332  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4333  */
4334 static int osd_it_ea_get(const struct lu_env *env,
4335                          struct dt_it *di, const struct dt_key *key)
4336 {
4337         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
4338
4339         ENTRY;
4340         LASSERT(((const char *)key)[0] == '\0');
4341         it->oie_file.f_pos      = 0;
4342         it->oie_rd_dirent       = 0;
4343         it->oie_it_dirent       = 0;
4344         it->oie_dirent          = NULL;
4345
4346         RETURN(+1);
4347 }
4348
4349 /**
4350  * Does nothing
4351  */
4352 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4353 {
4354 }
4355
4356 /**
4357  * It is called internally by ->readdir(). It fills the
4358  * iterator's in-memory data structure with required
4359  * information i.e. name, namelen, rec_size etc.
4360  *
4361  * \param buf in which information to be filled in.
4362  * \param name name of the file in given dir
4363  *
4364  * \retval 0 on success
4365  * \retval 1 on buffer full
4366  */
4367 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4368                                loff_t offset, __u64 ino,
4369                                unsigned d_type)
4370 {
4371         struct osd_it_ea        *it   = (struct osd_it_ea *)buf;
4372         struct osd_object       *obj  = it->oie_obj;
4373         struct osd_it_ea_dirent *ent  = it->oie_dirent;
4374         struct lu_fid           *fid  = &ent->oied_fid;
4375         struct osd_fid_pack     *rec;
4376         ENTRY;
4377
4378         /* this should never happen */
4379         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4380                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4381                 RETURN(-EIO);
4382         }
4383
4384         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4385             OSD_IT_EA_BUFSIZE)
4386                 RETURN(1);
4387
4388         /* "." is just the object itself. */
4389         if (namelen == 1 && name[0] == '.') {
4390                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4391         } else if (d_type & LDISKFS_DIRENT_LUFID) {
4392                 rec = (struct osd_fid_pack*) (name + namelen + 1);
4393                 if (osd_fid_unpack(fid, rec) != 0)
4394                         fid_zero(fid);
4395         } else {
4396                 fid_zero(fid);
4397         }
4398         d_type &= ~LDISKFS_DIRENT_LUFID;
4399
4400         /* NOT export local root. */
4401         if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4402                 ino = obj->oo_inode->i_ino;
4403                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4404         }
4405
4406         ent->oied_ino     = ino;
4407         ent->oied_off     = offset;
4408         ent->oied_namelen = namelen;
4409         ent->oied_type    = d_type;
4410
4411         memcpy(ent->oied_name, name, namelen);
4412
4413         it->oie_rd_dirent++;
4414         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4415         RETURN(0);
4416 }
4417
4418 /**
4419  * Calls ->readdir() to load a directory entry at a time
4420  * and stored it in iterator's in-memory data structure.
4421  *
4422  * \param di iterator's in memory structure
4423  *
4424  * \retval   0 on success
4425  * \retval -ve on error
4426  */
4427 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4428                                const struct dt_it *di)
4429 {
4430         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
4431         struct osd_object  *obj   = it->oie_obj;
4432         struct inode       *inode = obj->oo_inode;
4433         struct htree_lock  *hlock = NULL;
4434         int                 result = 0;
4435
4436         ENTRY;
4437         it->oie_dirent = it->oie_buf;
4438         it->oie_rd_dirent = 0;
4439
4440         if (obj->oo_hl_head != NULL) {
4441                 hlock = osd_oti_get(env)->oti_hlock;
4442                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4443                                    inode, LDISKFS_HLOCK_READDIR);
4444         } else {
4445                 down_read(&obj->oo_ext_idx_sem);
4446         }
4447
4448         result = inode->i_fop->readdir(&it->oie_file, it,
4449                                        (filldir_t) osd_ldiskfs_filldir);
4450
4451         if (hlock != NULL)
4452                 ldiskfs_htree_unlock(hlock);
4453         else
4454                 up_read(&obj->oo_ext_idx_sem);
4455
4456         if (it->oie_rd_dirent == 0) {
4457                 result = -EIO;
4458         } else {
4459                 it->oie_dirent = it->oie_buf;
4460                 it->oie_it_dirent = 1;
4461         }
4462
4463         RETURN(result);
4464 }
4465
4466 /**
4467  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4468  * to load a directory entry at a time and stored it in
4469  * iterator's in-memory data structure.
4470  *
4471  * \param di iterator's in memory structure
4472  *
4473  * \retval +ve iterator reached to end
4474  * \retval   0 iterator not reached to end
4475  * \retval -ve on error
4476  */
4477 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4478 {
4479         struct osd_it_ea *it = (struct osd_it_ea *)di;
4480         int rc;
4481
4482         ENTRY;
4483
4484         if (it->oie_it_dirent < it->oie_rd_dirent) {
4485                 it->oie_dirent =
4486                         (void *) it->oie_dirent +
4487                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4488                                        it->oie_dirent->oied_namelen);
4489                 it->oie_it_dirent++;
4490                 RETURN(0);
4491         } else {
4492                 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4493                         rc = +1;
4494                 else
4495                         rc = osd_ldiskfs_it_fill(env, di);
4496         }
4497
4498         RETURN(rc);
4499 }
4500
4501 /**
4502  * Returns the key at current position from iterator's in memory structure.
4503  *
4504  * \param di iterator's in memory structure
4505  *
4506  * \retval key i.e. struct dt_key on success
4507  */
4508 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4509                                     const struct dt_it *di)
4510 {
4511         struct osd_it_ea *it = (struct osd_it_ea *)di;
4512
4513         return (struct dt_key *)it->oie_dirent->oied_name;
4514 }
4515
4516 /**
4517  * Returns the key's size at current position from iterator's in memory structure.
4518  *
4519  * \param di iterator's in memory structure
4520  *
4521  * \retval key_size i.e. struct dt_key on success
4522  */
4523 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4524 {
4525         struct osd_it_ea *it = (struct osd_it_ea *)di;
4526
4527         return it->oie_dirent->oied_namelen;
4528 }
4529
4530 static int
4531 osd_dirent_update(handle_t *jh, struct super_block *sb,
4532                   struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4533                   struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4534 {
4535         struct osd_fid_pack *rec;
4536         int                  rc;
4537         ENTRY;
4538
4539         LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4540         LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4541
4542         rc = ldiskfs_journal_get_write_access(jh, bh);
4543         if (rc != 0) {
4544                 CERROR("%.16s: fail to write access for update dirent: "
4545                        "name = %.*s, rc = %d\n",
4546                        LDISKFS_SB(sb)->s_es->s_volume_name,
4547                        ent->oied_namelen, ent->oied_name, rc);
4548                 RETURN(rc);
4549         }
4550
4551         rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4552         fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4553         rc = ldiskfs_journal_dirty_metadata(jh, bh);
4554         if (rc != 0)
4555                 CERROR("%.16s: fail to dirty metadata for update dirent: "
4556                        "name = %.*s, rc = %d\n",
4557                        LDISKFS_SB(sb)->s_es->s_volume_name,
4558                        ent->oied_namelen, ent->oied_name, rc);
4559
4560         RETURN(rc);
4561 }
4562
4563 static inline int
4564 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4565 {
4566         if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4567             __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4568                 return 1;
4569         else
4570                 return 0;
4571 }
4572
4573 static inline int
4574 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4575 {
4576         LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4577                  "dot_dotdot = %d\n", dot_dotdot);
4578
4579         if (LDISKFS_DIR_REC_LEN(de) >=
4580             __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4581                 return 1;
4582         else
4583                 return 0;
4584 }
4585
4586 static int
4587 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4588                     struct inode *dir, struct inode *inode,
4589                     struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4590                     struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4591                     struct htree_lock *hlock)
4592 {
4593         struct dentry               *dentry;
4594         struct osd_fid_pack         *rec;
4595         struct ldiskfs_dentry_param *ldp;
4596         int                          rc;
4597         ENTRY;
4598
4599         if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4600                                           LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4601                 RETURN(0);
4602
4603         /* There is enough space to hold the FID-in-dirent. */
4604         if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4605                                  dir->i_sb->s_blocksize)) {
4606                 rc = ldiskfs_journal_get_write_access(jh, bh);
4607                 if (rc != 0) {
4608                         CERROR("%.16s: fail to write access for reinsert "
4609                                "dirent: name = %.*s, rc = %d\n",
4610                                LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4611                                ent->oied_namelen, ent->oied_name, rc);
4612                         RETURN(rc);
4613                 }
4614
4615                 de->name[de->name_len] = 0;
4616                 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4617                 rec->fp_len = sizeof(struct lu_fid) + 1;
4618                 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4619                 de->file_type |= LDISKFS_DIRENT_LUFID;
4620
4621                 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4622                 if (rc != 0)
4623                         CERROR("%.16s: fail to dirty metadata for reinsert "
4624                                "dirent: name = %.*s, rc = %d\n",
4625                                LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4626                                ent->oied_namelen, ent->oied_name, rc);
4627
4628                 RETURN(rc);
4629         }
4630
4631         rc = ldiskfs_delete_entry(jh, dir, de, bh);
4632         if (rc != 0) {
4633                 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4634                        "name = %.*s, rc = %d\n",
4635                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4636                        ent->oied_namelen, ent->oied_name, rc);
4637                 RETURN(rc);
4638         }
4639
4640         dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4641                                            ent->oied_namelen);
4642         ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4643         osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4644         dentry->d_fsdata = (void *)ldp;
4645         ll_vfs_dq_init(dir);
4646         rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4647         /* It is too bad, we cannot reinsert the name entry back.
4648          * That means we lose it! */
4649         if (rc != 0)
4650                 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4651                        "name = %.*s, rc = %d\n",
4652                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4653                        ent->oied_namelen, ent->oied_name, rc);
4654
4655         RETURN(rc);
4656 }
4657
4658 static int
4659 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4660                         struct osd_it_ea *it, struct lu_fid *fid,
4661                         struct osd_inode_id *id, __u32 *attr)
4662 {
4663         struct osd_thread_info     *info        = osd_oti_get(env);
4664         struct lustre_mdt_attrs    *lma         = &info->oti_mdt_attrs;
4665         struct osd_device          *dev         = osd_obj2dev(obj);
4666         struct super_block         *sb          = osd_sb(dev);
4667         const char                 *devname     =
4668                                         LDISKFS_SB(sb)->s_es->s_volume_name;
4669         struct osd_it_ea_dirent    *ent         = it->oie_dirent;
4670         struct inode               *dir         = obj->oo_inode;
4671         struct htree_lock          *hlock       = NULL;
4672         struct buffer_head         *bh          = NULL;
4673         handle_t                   *jh          = NULL;
4674         struct ldiskfs_dir_entry_2 *de;
4675         struct dentry              *dentry;
4676         struct inode               *inode;
4677         int                         credits;
4678         int                         rc;
4679         int                         dot_dotdot  = 0;
4680         bool                        dirty       = false;
4681         ENTRY;
4682
4683         if (ent->oied_name[0] == '.') {
4684                 if (ent->oied_namelen == 1)
4685                         dot_dotdot = 1;
4686                 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4687                         dot_dotdot = 2;
4688         }
4689
4690         dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4691                                       ent->oied_namelen);
4692
4693         /* We need to ensure that the name entry is still valid.
4694          * Because it may be removed or renamed by other already.
4695          *
4696          * The unlink or rename operation will start journal before PDO lock,
4697          * so to avoid deadlock, here we need to start journal handle before
4698          * related PDO lock also. But because we do not know whether there
4699          * will be something to be repaired before PDO lock, we just start
4700          * journal without conditions.
4701          *
4702          * We may need to remove the name entry firstly, then insert back.
4703          * One credit is for user quota file update.
4704          * One credit is for group quota file update.
4705          * Two credits are for dirty inode. */
4706         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4707                   osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4708
4709 again:
4710         if (dev->od_dirent_journal) {
4711                 jh = ldiskfs_journal_start_sb(sb, credits);
4712                 if (IS_ERR(jh)) {
4713                         rc = PTR_ERR(jh);
4714                         CERROR("%.16s: fail to start trans for dirent "
4715                                "check_repair: credits %d, name %.*s, rc %d\n",
4716                                devname, credits, ent->oied_namelen,
4717                                ent->oied_name, rc);
4718                         RETURN(rc);
4719                 }
4720
4721                 if (obj->oo_hl_head != NULL) {
4722                         hlock = osd_oti_get(env)->oti_hlock;
4723                         /* "0" means exclusive lock for the whole directory.
4724                          * We need to prevent others access such name entry
4725                          * during the delete + insert. Neither HLOCK_ADD nor
4726                          * HLOCK_DEL cannot guarantee the atomicity. */
4727                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4728                 } else {
4729                         down_write(&obj->oo_ext_idx_sem);
4730                 }
4731         } else {
4732                 if (obj->oo_hl_head != NULL) {
4733                         hlock = osd_oti_get(env)->oti_hlock;
4734                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4735                                            LDISKFS_HLOCK_LOOKUP);
4736                 } else {
4737                         down_read(&obj->oo_ext_idx_sem);
4738                 }
4739         }
4740
4741         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4742         /* For dot/dotdot entry, if there is not enough space to hold the
4743          * FID-in-dirent, just keep them there. It only happens when the
4744          * device upgraded from 1.8 or restored from MDT file-level backup.
4745          * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4746          * and needs to get FID from LMA when readdir, it will not affect the
4747          * performance much. */
4748         if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4749             (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4750                 *attr |= LUDA_IGNORE;
4751                 GOTO(out_journal, rc = 0);
4752         }
4753
4754         osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4755         inode = osd_iget(info, dev, id);
4756         if (IS_ERR(inode)) {
4757                 rc = PTR_ERR(inode);
4758                 if (rc == -ENOENT || rc == -ESTALE) {
4759                         *attr |= LUDA_IGNORE;
4760                         rc = 0;
4761                 }
4762
4763                 GOTO(out_journal, rc);
4764         }
4765
4766         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4767         if (rc == 0) {
4768                 if (fid_is_sane(fid)) {
4769                         /* FID-in-dirent is valid. */
4770                         if (lu_fid_eq(fid, &lma->lma_self_fid))
4771                                 GOTO(out_inode, rc = 0);
4772
4773                         /* Do not repair under dryrun mode. */
4774                         if (*attr & LUDA_VERIFY_DRYRUN) {
4775                                 *attr |= LUDA_REPAIR;
4776                                 GOTO(out_inode, rc = 0);
4777                         }
4778
4779                         if (!dev->od_dirent_journal) {
4780                                 iput(inode);
4781                                 brelse(bh);
4782                                 if (hlock != NULL)
4783                                         ldiskfs_htree_unlock(hlock);
4784                                 else
4785                                         up_read(&obj->oo_ext_idx_sem);
4786                                 dev->od_dirent_journal = 1;
4787                                 goto again;
4788                         }
4789
4790                         *fid = lma->lma_self_fid;
4791                         dirty = true;
4792                         /* Update the FID-in-dirent. */
4793                         rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4794                         if (rc == 0)
4795                                 *attr |= LUDA_REPAIR;
4796                 } else {
4797                         /* Do not repair under dryrun mode. */
4798                         if (*attr & LUDA_VERIFY_DRYRUN) {
4799                                 *fid = lma->lma_self_fid;
4800                                 *attr |= LUDA_REPAIR;
4801                                 GOTO(out_inode, rc = 0);
4802                         }
4803
4804                         if (!dev->od_dirent_journal) {
4805                                 iput(inode);
4806                                 brelse(bh);
4807                                 if (hlock != NULL)
4808                                         ldiskfs_htree_unlock(hlock);
4809                                 else
4810                                         up_read(&obj->oo_ext_idx_sem);
4811                                 dev->od_dirent_journal = 1;
4812                                 goto again;
4813                         }
4814
4815                         *fid = lma->lma_self_fid;
4816                         dirty = true;
4817                         /* Append the FID-in-dirent. */
4818                         rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4819                                                  fid, bh, de, hlock);
4820                         if (rc == 0)
4821                                 *attr |= LUDA_REPAIR;
4822                 }
4823         } else if (rc == -ENODATA) {
4824                 /* Do not repair under dryrun mode. */
4825                 if (*attr & LUDA_VERIFY_DRYRUN) {
4826                         if (fid_is_sane(fid)) {
4827                                 *attr |= LUDA_REPAIR;
4828                         } else {
4829                                 lu_igif_build(fid, inode->i_ino,
4830                                               inode->i_generation);
4831                                 *attr |= LUDA_UPGRADE;
4832                         }
4833                         GOTO(out_inode, rc = 0);
4834                 }
4835
4836                 if (!dev->od_dirent_journal) {
4837                         iput(inode);
4838                         brelse(bh);
4839                         if (hlock != NULL)
4840                                 ldiskfs_htree_unlock(hlock);
4841                         else
4842                                 up_read(&obj->oo_ext_idx_sem);
4843                         dev->od_dirent_journal = 1;
4844                         goto again;
4845                 }
4846
4847                 dirty = true;
4848                 if (unlikely(fid_is_sane(fid))) {
4849                         /* FID-in-dirent exists, but FID-in-LMA is lost.
4850                          * Trust the FID-in-dirent, and add FID-in-LMA. */
4851                         rc = osd_ea_fid_set(info, inode, fid, 0);
4852                         if (rc == 0)
4853                                 *attr |= LUDA_REPAIR;
4854                 } else {
4855                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
4856                         /* It is probably IGIF object. Only aappend the
4857                          * FID-in-dirent. OI scrub will process FID-in-LMA. */
4858                         rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4859                                                  fid, bh, de, hlock);
4860                         if (rc == 0)
4861                                 *attr |= LUDA_UPGRADE;
4862                 }
4863         }
4864
4865         GOTO(out_inode, rc);
4866
4867 out_inode:
4868         iput(inode);
4869
4870 out_journal:
4871         brelse(bh);
4872         if (hlock != NULL) {
4873                 ldiskfs_htree_unlock(hlock);
4874         } else {
4875                 if (dev->od_dirent_journal)
4876                         up_write(&obj->oo_ext_idx_sem);
4877                 else
4878                         up_read(&obj->oo_ext_idx_sem);
4879         }
4880         if (jh != NULL)
4881                 ldiskfs_journal_stop(jh);
4882         if (rc >= 0 && !dirty)
4883                 dev->od_dirent_journal = 0;
4884         return rc;
4885 }
4886
4887 /**
4888  * Returns the value at current position from iterator's in memory structure.
4889  *
4890  * \param di struct osd_it_ea, iterator's in memory structure
4891  * \param attr attr requested for dirent.
4892  * \param lde lustre dirent
4893  *
4894  * \retval   0 no error and \param lde has correct lustre dirent.
4895  * \retval -ve on error
4896  */
4897 static inline int osd_it_ea_rec(const struct lu_env *env,
4898                                 const struct dt_it *di,
4899                                 struct dt_rec *dtrec, __u32 attr)
4900 {
4901         struct osd_it_ea       *it    = (struct osd_it_ea *)di;
4902         struct osd_object      *obj   = it->oie_obj;
4903         struct osd_device      *dev   = osd_obj2dev(obj);
4904         struct osd_scrub       *scrub = &dev->od_scrub;
4905         struct scrub_file      *sf    = &scrub->os_file;
4906         struct osd_thread_info *oti   = osd_oti_get(env);
4907         struct osd_inode_id    *id    = &oti->oti_id;
4908         struct osd_idmap_cache *oic   = &oti->oti_cache;
4909         struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
4910         struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
4911         __u32                   ino   = it->oie_dirent->oied_ino;
4912         int                     rc    = 0;
4913         ENTRY;
4914
4915         if (attr & LUDA_VERIFY) {
4916                 attr |= LUDA_TYPE;
4917                 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
4918                         attr |= LUDA_IGNORE;
4919                         rc = 0;
4920                         goto pack;
4921                 }
4922
4923                 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
4924         } else {
4925                 attr &= ~LU_DIRENT_ATTRS_MASK;
4926                 if (!fid_is_sane(fid)) {
4927                         if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
4928                                 RETURN(-ENOENT);
4929
4930                         rc = osd_ea_fid_get(env, obj, ino, fid, id);
4931                 } else {
4932                         osd_id_gen(id, ino, OSD_OII_NOGEN);
4933                 }
4934         }
4935
4936         if (rc < 0)
4937                 RETURN(rc);
4938
4939 pack:
4940         osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4941                            it->oie_dirent->oied_name,
4942                            it->oie_dirent->oied_namelen,
4943                            it->oie_dirent->oied_type, attr);
4944
4945         if (osd_remote_fid(env, dev, fid))
4946                 RETURN(0);
4947
4948         if (likely(!(attr & LUDA_IGNORE))) {
4949                 oic->oic_lid = *id;
4950                 oic->oic_fid = *fid;
4951         }
4952
4953         if (!(attr & LUDA_VERIFY) &&
4954             (scrub->os_pos_current <= ino) &&
4955             ((sf->sf_flags & SF_INCONSISTENT) ||
4956              (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
4957              ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4958                 osd_consistency_check(oti, dev, oic);
4959
4960         RETURN(rc);
4961 }
4962
4963 /**
4964  * Returns a cookie for current position of the iterator head, so that
4965  * user can use this cookie to load/start the iterator next time.
4966  *
4967  * \param di iterator's in memory structure
4968  *
4969  * \retval cookie for current position, on success
4970  */
4971 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4972 {
4973         struct osd_it_ea *it = (struct osd_it_ea *)di;
4974
4975         return it->oie_dirent->oied_off;
4976 }
4977
4978 /**
4979  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4980  * to load a directory entry at a time and stored it i inn,
4981  * in iterator's in-memory data structure.
4982  *
4983  * \param di struct osd_it_ea, iterator's in memory structure
4984  *
4985  * \retval +ve on success
4986  * \retval -ve on error
4987  */
4988 static int osd_it_ea_load(const struct lu_env *env,
4989                           const struct dt_it *di, __u64 hash)
4990 {
4991         struct osd_it_ea *it = (struct osd_it_ea *)di;
4992         int rc;
4993
4994         ENTRY;
4995         it->oie_file.f_pos = hash;
4996
4997         rc =  osd_ldiskfs_it_fill(env, di);
4998         if (rc == 0)
4999                 rc = +1;
5000
5001         RETURN(rc);
5002 }
5003
5004 /**
5005  * Index lookup function for interoperability mode (b11826).
5006  *
5007  * \param key,  key i.e. file name to be searched
5008  *
5009  * \retval +ve, on success
5010  * \retval -ve, on error
5011  */
5012 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5013                                struct dt_rec *rec, const struct dt_key *key,
5014                                struct lustre_capa *capa)
5015 {
5016         struct osd_object *obj = osd_dt_obj(dt);
5017         int rc = 0;
5018
5019         ENTRY;
5020
5021         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5022         LINVRNT(osd_invariant(obj));
5023
5024         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5025                 return -EACCES;
5026
5027         rc = osd_ea_lookup_rec(env, obj, rec, key);
5028         if (rc == 0)
5029                 rc = +1;
5030         RETURN(rc);
5031 }
5032
5033 /**
5034  * Index and Iterator operations for interoperability
5035  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5036  */
5037 static const struct dt_index_operations osd_index_ea_ops = {
5038         .dio_lookup         = osd_index_ea_lookup,
5039         .dio_declare_insert = osd_index_declare_ea_insert,
5040         .dio_insert         = osd_index_ea_insert,
5041         .dio_declare_delete = osd_index_declare_ea_delete,
5042         .dio_delete         = osd_index_ea_delete,
5043         .dio_it     = {
5044                 .init     = osd_it_ea_init,
5045                 .fini     = osd_it_ea_fini,
5046                 .get      = osd_it_ea_get,
5047                 .put      = osd_it_ea_put,
5048                 .next     = osd_it_ea_next,
5049                 .key      = osd_it_ea_key,
5050                 .key_size = osd_it_ea_key_size,
5051                 .rec      = osd_it_ea_rec,
5052                 .store    = osd_it_ea_store,
5053                 .load     = osd_it_ea_load
5054         }
5055 };
5056
5057 static void *osd_key_init(const struct lu_context *ctx,
5058                           struct lu_context_key *key)
5059 {
5060         struct osd_thread_info *info;
5061
5062         OBD_ALLOC_PTR(info);
5063         if (info == NULL)
5064                 return ERR_PTR(-ENOMEM);
5065
5066         OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5067         if (info->oti_it_ea_buf == NULL)
5068                 goto out_free_info;
5069
5070         info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5071
5072         info->oti_hlock = ldiskfs_htree_lock_alloc();
5073         if (info->oti_hlock == NULL)
5074                 goto out_free_ea;
5075
5076         return info;
5077
5078  out_free_ea:
5079         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5080  out_free_info:
5081         OBD_FREE_PTR(info);
5082         return ERR_PTR(-ENOMEM);
5083 }
5084
5085 static void osd_key_fini(const struct lu_context *ctx,
5086                          struct lu_context_key *key, void* data)
5087 {
5088         struct osd_thread_info *info = data;
5089
5090         if (info->oti_hlock != NULL)
5091                 ldiskfs_htree_lock_free(info->oti_hlock);
5092         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5093         lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5094         lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5095         OBD_FREE_PTR(info);
5096 }
5097
5098 static void osd_key_exit(const struct lu_context *ctx,
5099                          struct lu_context_key *key, void *data)
5100 {
5101         struct osd_thread_info *info = data;
5102
5103         LASSERT(info->oti_r_locks == 0);
5104         LASSERT(info->oti_w_locks == 0);
5105         LASSERT(info->oti_txns    == 0);
5106 }
5107
5108 /* type constructor/destructor: osd_type_init, osd_type_fini */
5109 LU_TYPE_INIT_FINI(osd, &osd_key);
5110
5111 struct lu_context_key osd_key = {
5112         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5113         .lct_init = osd_key_init,
5114         .lct_fini = osd_key_fini,
5115         .lct_exit = osd_key_exit
5116 };
5117
5118
5119 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5120                            const char *name, struct lu_device *next)
5121 {
5122         struct osd_device *osd = osd_dev(d);
5123
5124         if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5125             >= sizeof(osd->od_svname))
5126                 return -E2BIG;
5127         return osd_procfs_init(osd, name);
5128 }
5129
5130 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5131 {
5132         ENTRY;
5133
5134         osd_scrub_cleanup(env, o);
5135
5136         if (o->od_fsops) {
5137                 fsfilt_put_ops(o->od_fsops);
5138                 o->od_fsops = NULL;
5139         }
5140
5141         /* shutdown quota slave instance associated with the device */
5142         if (o->od_quota_slave != NULL) {
5143                 qsd_fini(env, o->od_quota_slave);
5144                 o->od_quota_slave = NULL;
5145         }
5146
5147         RETURN(0);
5148 }
5149
5150 static int osd_mount(const struct lu_env *env,
5151                      struct osd_device *o, struct lustre_cfg *cfg)
5152 {
5153         const char              *name  = lustre_cfg_string(cfg, 0);
5154         const char              *dev  = lustre_cfg_string(cfg, 1);
5155         const char              *opts;
5156         unsigned long            page, s_flags, lmd_flags = 0;
5157         struct page             *__page;
5158         struct file_system_type *type;
5159         char                    *options = NULL;
5160         char                    *str;
5161         int                       rc = 0;
5162         ENTRY;
5163
5164         if (o->od_mnt != NULL)
5165                 RETURN(0);
5166
5167         if (strlen(dev) >= sizeof(o->od_mntdev))
5168                 RETURN(-E2BIG);
5169         strcpy(o->od_mntdev, dev);
5170
5171         o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5172         if (o->od_fsops == NULL) {
5173                 CERROR("Can't find fsfilt_ldiskfs\n");
5174                 RETURN(-ENOTSUPP);
5175         }
5176
5177         OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5178         if (__page == NULL)
5179                 GOTO(out, rc = -ENOMEM);
5180
5181         str = lustre_cfg_string(cfg, 2);
5182         s_flags = simple_strtoul(str, NULL, 0);
5183         str = strstr(str, ":");
5184         if (str)
5185                 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5186         opts = lustre_cfg_string(cfg, 3);
5187         page = (unsigned long)cfs_page_address(__page);
5188         options = (char *)page;
5189         *options = '\0';
5190         if (opts == NULL)
5191                 strcat(options, "user_xattr,acl");
5192         else
5193                 strcat(options, opts);
5194
5195         /* Glom up mount options */
5196         if (*options != '\0')
5197                 strcat(options, ",");
5198         strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5199
5200         type = get_fs_type("ldiskfs");
5201         if (!type) {
5202                 CERROR("%s: cannot find ldiskfs module\n", name);
5203                 GOTO(out, rc = -ENODEV);
5204         }
5205
5206         o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5207         cfs_module_put(type->owner);
5208
5209         if (IS_ERR(o->od_mnt)) {
5210                 rc = PTR_ERR(o->od_mnt);
5211                 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5212                 o->od_mnt = NULL;
5213                 GOTO(out, rc);
5214         }
5215
5216 #ifdef HAVE_DEV_SET_RDONLY
5217         if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5218                 CERROR("%s: underlying device %s is marked as read-only. "
5219                        "Setup failed\n", name, dev);
5220                 mntput(o->od_mnt);
5221                 o->od_mnt = NULL;
5222                 GOTO(out, rc = -EROFS);
5223         }
5224 #endif
5225
5226         if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5227             LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5228                 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5229                 mntput(o->od_mnt);
5230                 o->od_mnt = NULL;
5231                 GOTO(out, rc = -EINVAL);
5232         }
5233
5234         ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5235                                 LDISKFS_STATE_LUSTRE_NO_OI);
5236         if (lmd_flags & LMD_FLG_NOSCRUB)
5237                 o->od_noscrub = 1;
5238
5239 out:
5240         if (__page)
5241                 OBD_PAGE_FREE(__page);
5242         if (rc)
5243                 fsfilt_put_ops(o->od_fsops);
5244
5245         RETURN(rc);
5246 }
5247
5248 static struct lu_device *osd_device_fini(const struct lu_env *env,
5249                                          struct lu_device *d)
5250 {
5251         int rc;
5252         ENTRY;
5253
5254         rc = osd_shutdown(env, osd_dev(d));
5255
5256         osd_obj_map_fini(osd_dev(d));
5257
5258         shrink_dcache_sb(osd_sb(osd_dev(d)));
5259         osd_sync(env, lu2dt_dev(d));
5260
5261         rc = osd_procfs_fini(osd_dev(d));
5262         if (rc) {
5263                 CERROR("proc fini error %d \n", rc);
5264                 RETURN (ERR_PTR(rc));
5265         }
5266
5267         if (osd_dev(d)->od_mnt) {
5268                 mntput(osd_dev(d)->od_mnt);
5269                 osd_dev(d)->od_mnt = NULL;
5270         }
5271
5272         RETURN(NULL);
5273 }
5274
5275 static int osd_device_init0(const struct lu_env *env,
5276                             struct osd_device *o,
5277                             struct lustre_cfg *cfg)
5278 {
5279         struct lu_device        *l = osd2lu_dev(o);
5280         struct osd_thread_info *info;
5281         int                     rc;
5282         int                     cplen = 0;
5283
5284         /* if the module was re-loaded, env can loose its keys */
5285         rc = lu_env_refill((struct lu_env *) env);
5286         if (rc)
5287                 GOTO(out, rc);
5288         info = osd_oti_get(env);
5289         LASSERT(info);
5290
5291         l->ld_ops = &osd_lu_ops;
5292         o->od_dt_dev.dd_ops = &osd_dt_ops;
5293
5294         spin_lock_init(&o->od_osfs_lock);
5295         mutex_init(&o->od_otable_mutex);
5296         o->od_osfs_age = cfs_time_shift_64(-1000);
5297
5298         o->od_capa_hash = init_capa_hash();
5299         if (o->od_capa_hash == NULL)
5300                 GOTO(out, rc = -ENOMEM);
5301
5302         o->od_read_cache = 1;
5303         o->od_writethrough_cache = 1;
5304         o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5305
5306         rc = osd_mount(env, o, cfg);
5307         if (rc)
5308                 GOTO(out_capa, rc);
5309
5310         CFS_INIT_LIST_HEAD(&o->od_ios_list);
5311         /* setup scrub, including OI files initialization */
5312         rc = osd_scrub_setup(env, o);
5313         if (rc < 0)
5314                 GOTO(out_mnt, rc);
5315
5316         cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5317                         sizeof(o->od_svname));
5318         if (cplen >= sizeof(o->od_svname)) {
5319                 rc = -E2BIG;
5320                 GOTO(out_mnt, rc);
5321         }
5322
5323         rc = osd_obj_map_init(env, o);
5324         if (rc != 0)
5325                 GOTO(out_scrub, rc);
5326
5327         rc = lu_site_init(&o->od_site, l);
5328         if (rc)
5329                 GOTO(out_compat, rc);
5330         o->od_site.ls_bottom_dev = l;
5331
5332         rc = lu_site_init_finish(&o->od_site);
5333         if (rc)
5334                 GOTO(out_site, rc);
5335
5336         rc = osd_procfs_init(o, o->od_svname);
5337         if (rc != 0) {
5338                 CERROR("%s: can't initialize procfs: rc = %d\n",
5339                        o->od_svname, rc);
5340                 GOTO(out_site, rc);
5341         }
5342
5343         LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5344
5345         /* initialize quota slave instance */
5346         o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5347                                      o->od_proc_entry);
5348         if (IS_ERR(o->od_quota_slave)) {
5349                 rc = PTR_ERR(o->od_quota_slave);
5350                 o->od_quota_slave = NULL;
5351                 GOTO(out_procfs, rc);
5352         }
5353
5354         RETURN(0);
5355 out_procfs:
5356         osd_procfs_fini(o);
5357 out_site:
5358         lu_site_fini(&o->od_site);
5359 out_compat:
5360         osd_obj_map_fini(o);
5361 out_scrub:
5362         osd_scrub_cleanup(env, o);
5363 out_mnt:
5364         osd_oi_fini(info, o);
5365         osd_shutdown(env, o);
5366         mntput(o->od_mnt);
5367         o->od_mnt = NULL;
5368 out_capa:
5369         cleanup_capa_hash(o->od_capa_hash);
5370 out:
5371         RETURN(rc);
5372 }
5373
5374 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5375                                           struct lu_device_type *t,
5376                                           struct lustre_cfg *cfg)
5377 {
5378         struct osd_device *o;
5379         int                rc;
5380
5381         OBD_ALLOC_PTR(o);
5382         if (o == NULL)
5383                 return ERR_PTR(-ENOMEM);
5384
5385         rc = dt_device_init(&o->od_dt_dev, t);
5386         if (rc == 0) {
5387                 /* Because the ctx might be revived in dt_device_init,
5388                  * refill the env here */
5389                 lu_env_refill((struct lu_env *)env);
5390                 rc = osd_device_init0(env, o, cfg);
5391                 if (rc)
5392                         dt_device_fini(&o->od_dt_dev);
5393         }
5394
5395         if (unlikely(rc != 0))
5396                 OBD_FREE_PTR(o);
5397
5398         return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5399 }
5400
5401 static struct lu_device *osd_device_free(const struct lu_env *env,
5402                                          struct lu_device *d)
5403 {
5404         struct osd_device *o = osd_dev(d);
5405         ENTRY;
5406
5407         cleanup_capa_hash(o->od_capa_hash);
5408         /* XXX: make osd top device in order to release reference */
5409         d->ld_site->ls_top_dev = d;
5410         lu_site_purge(env, d->ld_site, -1);
5411         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5412                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5413                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5414         }
5415         lu_site_fini(&o->od_site);
5416         dt_device_fini(&o->od_dt_dev);
5417         OBD_FREE_PTR(o);
5418         RETURN(NULL);
5419 }
5420
5421 static int osd_process_config(const struct lu_env *env,
5422                               struct lu_device *d, struct lustre_cfg *cfg)
5423 {
5424         struct osd_device *o = osd_dev(d);
5425         int err;
5426         ENTRY;
5427
5428         switch(cfg->lcfg_command) {
5429         case LCFG_SETUP:
5430                 err = osd_mount(env, o, cfg);
5431                 break;
5432         case LCFG_CLEANUP:
5433                 lu_dev_del_linkage(d->ld_site, d);
5434                 err = osd_shutdown(env, o);
5435                 break;
5436         default:
5437                 err = -ENOSYS;
5438         }
5439
5440         RETURN(err);
5441 }
5442
5443 static int osd_recovery_complete(const struct lu_env *env,
5444                                  struct lu_device *d)
5445 {
5446         struct osd_device       *osd = osd_dev(d);
5447         int                      rc = 0;
5448         ENTRY;
5449
5450         if (osd->od_quota_slave == NULL)
5451                 RETURN(0);
5452
5453         /* start qsd instance on recovery completion, this notifies the quota
5454          * slave code that we are about to process new requests now */
5455         rc = qsd_start(env, osd->od_quota_slave);
5456         RETURN(rc);
5457 }
5458
5459 /*
5460  * we use exports to track all osd users
5461  */
5462 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5463                            struct obd_device *obd, struct obd_uuid *cluuid,
5464                            struct obd_connect_data *data, void *localdata)
5465 {
5466         struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
5467         struct lustre_handle  conn;
5468         int                   rc;
5469         ENTRY;
5470
5471         CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5472
5473         rc = class_connect(&conn, obd, cluuid);
5474         if (rc)
5475                 RETURN(rc);
5476
5477         *exp = class_conn2export(&conn);
5478
5479         spin_lock(&osd->od_osfs_lock);
5480         osd->od_connects++;
5481         spin_unlock(&osd->od_osfs_lock);
5482
5483         RETURN(0);
5484 }
5485
5486 /*
5487  * once last export (we don't count self-export) disappeared
5488  * osd can be released
5489  */
5490 static int osd_obd_disconnect(struct obd_export *exp)
5491 {
5492         struct obd_device *obd = exp->exp_obd;
5493         struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5494         int                rc, release = 0;
5495         ENTRY;
5496
5497         /* Only disconnect the underlying layers on the final disconnect. */
5498         spin_lock(&osd->od_osfs_lock);
5499         osd->od_connects--;
5500         if (osd->od_connects == 0)
5501                 release = 1;
5502         spin_unlock(&osd->od_osfs_lock);
5503
5504         rc = class_disconnect(exp); /* bz 9811 */
5505
5506         if (rc == 0 && release)
5507                 class_manual_cleanup(obd);
5508         RETURN(rc);
5509 }
5510
5511 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5512                        struct lu_device *dev)
5513 {
5514         struct osd_device *osd = osd_dev(dev);
5515         int                result = 0;
5516         ENTRY;
5517
5518         if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5519                 /* MDT/MDD still use old infrastructure to create
5520                  * special files */
5521                 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5522                                                  lu2dt_dev(dev));
5523                 if (result)
5524                         RETURN(result);
5525         }
5526
5527         if (osd->od_quota_slave != NULL)
5528                 /* set up quota slave objects */
5529                 result = qsd_prepare(env, osd->od_quota_slave);
5530
5531         RETURN(result);
5532 }
5533
5534 static const struct lu_object_operations osd_lu_obj_ops = {
5535         .loo_object_init      = osd_object_init,
5536         .loo_object_delete    = osd_object_delete,
5537         .loo_object_release   = osd_object_release,
5538         .loo_object_free      = osd_object_free,
5539         .loo_object_print     = osd_object_print,
5540         .loo_object_invariant = osd_object_invariant
5541 };
5542
5543 const struct lu_device_operations osd_lu_ops = {
5544         .ldo_object_alloc      = osd_object_alloc,
5545         .ldo_process_config    = osd_process_config,
5546         .ldo_recovery_complete = osd_recovery_complete,
5547         .ldo_prepare           = osd_prepare,
5548 };
5549
5550 static const struct lu_device_type_operations osd_device_type_ops = {
5551         .ldto_init = osd_type_init,
5552         .ldto_fini = osd_type_fini,
5553
5554         .ldto_start = osd_type_start,
5555         .ldto_stop  = osd_type_stop,
5556
5557         .ldto_device_alloc = osd_device_alloc,
5558         .ldto_device_free  = osd_device_free,
5559
5560         .ldto_device_init    = osd_device_init,
5561         .ldto_device_fini    = osd_device_fini
5562 };
5563
5564 struct lu_device_type osd_device_type = {
5565         .ldt_tags     = LU_DEVICE_DT,
5566         .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
5567         .ldt_ops      = &osd_device_type_ops,
5568         .ldt_ctx_tags = LCT_LOCAL,
5569 };
5570
5571 /*
5572  * lprocfs legacy support.
5573  */
5574 static struct obd_ops osd_obd_device_ops = {
5575         .o_owner = THIS_MODULE,
5576         .o_connect      = osd_obd_connect,
5577         .o_disconnect   = osd_obd_disconnect
5578 };
5579
5580 static int __init osd_mod_init(void)
5581 {
5582         struct lprocfs_static_vars lvars;
5583
5584         osd_oi_mod_init();
5585         lprocfs_osd_init_vars(&lvars);
5586         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5587                                    LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5588 }
5589
5590 static void __exit osd_mod_exit(void)
5591 {
5592         class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5593 }
5594
5595 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5596 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5597 MODULE_LICENSE("GPL");
5598
5599 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);