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