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