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