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