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