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