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