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