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