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