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