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