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