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