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