Whamcloud - gitweb
LU-909 osd: changes to osd api
[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_body_operations        osd_body_ops_new;
142 static const struct dt_index_operations       osd_index_iam_ops;
143 static const struct dt_index_operations       osd_index_ea_ops;
144
145 #define OSD_TRACK_DECLARES
146 #ifdef OSD_TRACK_DECLARES
147 #define OSD_DECLARE_OP(oh, op)   {                               \
148         LASSERT(oh->ot_handle == NULL);                          \
149         ((oh)->ot_declare_ ##op)++; }
150 #define OSD_EXEC_OP(handle, op)     {                            \
151         struct osd_thandle *oh;                                  \
152         oh = container_of0(handle, struct osd_thandle, ot_super);\
153         LASSERT((oh)->ot_declare_ ##op > 0);                     \
154         ((oh)->ot_declare_ ##op)--; }
155 #else
156 #define OSD_DECLARE_OP(oh, op)
157 #define OSD_EXEC_OP(oh, op)
158 #endif
159
160 struct osd_thandle {
161         struct thandle          ot_super;
162         handle_t               *ot_handle;
163         struct journal_callback ot_jcb;
164         cfs_list_t              ot_dcb_list;
165         /* Link to the device, for debugging. */
166         struct lu_ref_link     *ot_dev_link;
167         int                     ot_credits;
168
169 #ifdef OSD_TRACK_DECLARES
170         unsigned char           ot_declare_attr_set;
171         unsigned char           ot_declare_punch;
172         unsigned char           ot_declare_xattr_set;
173         unsigned char           ot_declare_create;
174         unsigned char           ot_declare_destroy;
175         unsigned char           ot_declare_ref_add;
176         unsigned char           ot_declare_ref_del;
177         unsigned char           ot_declare_write;
178         unsigned char           ot_declare_insert;
179         unsigned char           ot_declare_delete;
180 #endif
181
182 #if OSD_THANDLE_STATS
183         /** time when this handle was allocated */
184         cfs_time_t oth_alloced;
185
186         /** time when this thanle was started */
187         cfs_time_t oth_started;
188 #endif
189 };
190
191 /**
192  * Basic transaction credit op
193  */
194 enum dt_txn_op {
195         DTO_INDEX_INSERT,
196         DTO_INDEX_DELETE,
197         DTO_INDEX_UPDATE,
198         DTO_OBJECT_CREATE,
199         DTO_OBJECT_DELETE,
200         DTO_ATTR_SET_BASE,
201         DTO_XATTR_SET,
202         DTO_LOG_REC, /**< XXX temporary: dt layer knows nothing about llog. */
203         DTO_WRITE_BASE,
204         DTO_WRITE_BLOCK,
205         DTO_ATTR_SET_CHOWN,
206
207         DTO_NR
208 };
209
210 /*
211  * Helpers.
212  */
213 static int lu_device_is_osd(const struct lu_device *d)
214 {
215         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
216 }
217
218 static struct osd_device *osd_dt_dev(const struct dt_device *d)
219 {
220         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
221         return container_of0(d, struct osd_device, od_dt_dev);
222 }
223
224 static struct osd_device *osd_dev(const struct lu_device *d)
225 {
226         LASSERT(lu_device_is_osd(d));
227         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
228 }
229
230 static struct osd_device *osd_obj2dev(const struct osd_object *o)
231 {
232         return osd_dev(o->oo_dt.do_lu.lo_dev);
233 }
234
235 static struct super_block *osd_sb(const struct osd_device *dev)
236 {
237         return dev->od_mount->lmi_mnt->mnt_sb;
238 }
239
240 static int osd_object_is_root(const struct osd_object *obj)
241 {
242         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
243 }
244
245 static struct osd_object *osd_obj(const struct lu_object *o)
246 {
247         LASSERT(lu_device_is_osd(o->lo_dev));
248         return container_of0(o, struct osd_object, oo_dt.do_lu);
249 }
250
251 static struct osd_object *osd_dt_obj(const struct dt_object *d)
252 {
253         return osd_obj(&d->do_lu);
254 }
255
256 static struct lu_device *osd2lu_dev(struct osd_device *osd)
257 {
258         return &osd->od_dt_dev.dd_lu_dev;
259 }
260
261 static journal_t *osd_journal(const struct osd_device *dev)
262 {
263         return LDISKFS_SB(osd_sb(dev))->s_journal;
264 }
265
266 static int osd_has_index(const struct osd_object *obj)
267 {
268         return obj->oo_dt.do_index_ops != NULL;
269 }
270
271 static int osd_object_invariant(const struct lu_object *l)
272 {
273         return osd_invariant(osd_obj(l));
274 }
275
276 #ifdef HAVE_QUOTA_SUPPORT
277 static inline void
278 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
279 {
280         struct md_ucred    *uc = md_ucred(env);
281         struct cred        *tc;
282
283         LASSERT(uc != NULL);
284
285         save->oc_uid = current_fsuid();
286         save->oc_gid = current_fsgid();
287         save->oc_cap = current_cap();
288         if ((tc = prepare_creds())) {
289                 tc->fsuid         = uc->mu_fsuid;
290                 tc->fsgid         = uc->mu_fsgid;
291                 commit_creds(tc);
292         }
293         /* XXX not suboptimal */
294         cfs_curproc_cap_unpack(uc->mu_cap);
295 }
296
297 static inline void
298 osd_pop_ctxt(struct osd_ctxt *save)
299 {
300         struct cred *tc;
301
302         if ((tc = prepare_creds())) {
303                 tc->fsuid         = save->oc_uid;
304                 tc->fsgid         = save->oc_gid;
305                 tc->cap_effective = save->oc_cap;
306                 commit_creds(tc);
307         }
308 }
309 #endif
310
311 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
312 {
313         return lu_context_key_get(&env->le_ctx, &osd_key);
314 }
315
316 /*
317  * Concurrency: doesn't matter
318  */
319 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
320 {
321         return osd_oti_get(env)->oti_r_locks > 0;
322 }
323
324 /*
325  * Concurrency: doesn't matter
326  */
327 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
328 {
329         struct osd_thread_info *oti = osd_oti_get(env);
330         return oti->oti_w_locks > 0 && o->oo_owner == env;
331 }
332
333 /*
334  * Concurrency: doesn't access mutable data
335  */
336 static int osd_root_get(const struct lu_env *env,
337                         struct dt_device *dev, struct lu_fid *f)
338 {
339         struct inode *inode;
340
341         inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
342         LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
343         return 0;
344 }
345
346 /*
347  * OSD object methods.
348  */
349
350 /*
351  * Concurrency: no concurrent access is possible that early in object
352  * life-cycle.
353  */
354 static struct lu_object *osd_object_alloc(const struct lu_env *env,
355                                           const struct lu_object_header *hdr,
356                                           struct lu_device *d)
357 {
358         struct osd_object *mo;
359
360         OBD_ALLOC_PTR(mo);
361         if (mo != NULL) {
362                 struct lu_object *l;
363
364                 l = &mo->oo_dt.do_lu;
365                 dt_object_init(&mo->oo_dt, NULL, d);
366                 if (osd_dev(d)->od_iop_mode)
367                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
368                 else
369                         mo->oo_dt.do_ops = &osd_obj_ops;
370
371                 l->lo_ops = &osd_lu_obj_ops;
372                 cfs_init_rwsem(&mo->oo_sem);
373                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
374                 cfs_spin_lock_init(&mo->oo_guard);
375                 return l;
376         } else {
377                 return NULL;
378         }
379 }
380
381 /*
382  * retrieve object from backend ext fs.
383  **/
384 static struct inode *osd_iget(struct osd_thread_info *info,
385                               struct osd_device *dev,
386                               const struct osd_inode_id *id)
387 {
388         struct inode *inode = NULL;
389
390 #ifdef HAVE_EXT4_LDISKFS
391         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
392         if (IS_ERR(inode))
393         /* Newer kernels return an error instead of a NULL pointer */
394                 inode = NULL;
395 #else
396         inode = iget(osd_sb(dev), id->oii_ino);
397 #endif
398         if (inode == NULL) {
399                 CERROR("no inode\n");
400                 inode = ERR_PTR(-EACCES);
401         } else if (id->oii_gen != OSD_OII_NOGEN &&
402                    inode->i_generation != id->oii_gen) {
403                 iput(inode);
404                 inode = ERR_PTR(-ESTALE);
405         } else if (inode->i_nlink == 0) {
406                 /* due to parallel readdir and unlink,
407                 * we can have dead inode here. */
408                 CWARN("stale inode\n");
409                 make_bad_inode(inode);
410                 iput(inode);
411                 inode = ERR_PTR(-ESTALE);
412         } else if (is_bad_inode(inode)) {
413                 CERROR("bad inode %lx\n",inode->i_ino);
414                 iput(inode);
415                 inode = ERR_PTR(-ENOENT);
416         } else {
417                 /* Do not update file c/mtime in ldiskfs.
418                  * NB: we don't have any lock to protect this because we don't
419                  * have reference on osd_object now, but contention with
420                  * another lookup + attr_set can't happen in the tiny window
421                  * between if (...) and set S_NOCMTIME. */
422                 if (!(inode->i_flags & S_NOCMTIME))
423                         inode->i_flags |= S_NOCMTIME;
424         }
425         return inode;
426 }
427
428 static int osd_fid_lookup(const struct lu_env *env,
429                           struct osd_object *obj, const struct lu_fid *fid)
430 {
431         struct osd_thread_info *info;
432         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
433         struct osd_device      *dev;
434         struct osd_inode_id    *id;
435         struct osd_oi          *oi;
436         struct inode           *inode;
437         int                     result;
438
439         LINVRNT(osd_invariant(obj));
440         LASSERT(obj->oo_inode == NULL);
441         LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
442         /*
443          * This assertion checks that osd layer sees only local
444          * fids. Unfortunately it is somewhat expensive (does a
445          * cache-lookup). Disabling it for production/acceptance-testing.
446          */
447         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
448
449         ENTRY;
450
451         info = osd_oti_get(env);
452         dev  = osd_dev(ldev);
453         id   = &info->oti_id;
454         oi   = &dev->od_oi;
455
456         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
457                 RETURN(-ENOENT);
458
459         result = osd_oi_lookup(info, oi, fid, id);
460         if (result != 0) {
461                 if (result == -ENOENT)
462                         result = 0;
463                 goto out;
464         }
465
466         inode = osd_iget(info, dev, id);
467         if (IS_ERR(inode)) {
468                 /*
469                  * If fid wasn't found in oi, inode-less object is
470                  * created, for which lu_object_exists() returns
471                  * false. This is used in a (frequent) case when
472                  * objects are created as locking anchors or
473                  * place holders for objects yet to be created.
474                  */
475                 result = PTR_ERR(inode);
476                 goto out;
477         }
478
479         obj->oo_inode = inode;
480         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
481         if (dev->od_iop_mode) {
482                 obj->oo_compat_dot_created = 1;
483                 obj->oo_compat_dotdot_created = 1;
484         }
485
486         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
487                 goto out;
488
489         LASSERT(obj->oo_hl_head == NULL);
490         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
491         if (obj->oo_hl_head == NULL) {
492                 obj->oo_inode = NULL;
493                 iput(inode);
494                 result = -ENOMEM;
495         }
496 out:
497         LINVRNT(osd_invariant(obj));
498
499         RETURN(result);
500 }
501
502 /*
503  * Concurrency: shouldn't matter.
504  */
505 static void osd_object_init0(struct osd_object *obj)
506 {
507         LASSERT(obj->oo_inode != NULL);
508         obj->oo_dt.do_body_ops = &osd_body_ops;
509         obj->oo_dt.do_lu.lo_header->loh_attr |=
510                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
511 }
512
513 /*
514  * Concurrency: no concurrent access is possible that early in object
515  * life-cycle.
516  */
517 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
518                            const struct lu_object_conf *unused)
519 {
520         struct osd_object *obj = osd_obj(l);
521         int result;
522
523         LINVRNT(osd_invariant(obj));
524
525         result = osd_fid_lookup(env, obj, lu_object_fid(l));
526         obj->oo_dt.do_body_ops = &osd_body_ops_new;
527         if (result == 0) {
528                 if (obj->oo_inode != NULL)
529                         osd_object_init0(obj);
530         }
531         LINVRNT(osd_invariant(obj));
532         return result;
533 }
534
535 /*
536  * Concurrency: no concurrent access is possible that late in object
537  * life-cycle.
538  */
539 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
540 {
541         struct osd_object *obj = osd_obj(l);
542
543         LINVRNT(osd_invariant(obj));
544
545         dt_object_fini(&obj->oo_dt);
546         if (obj->oo_hl_head != NULL)
547                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
548         OBD_FREE_PTR(obj);
549 }
550
551 /**
552  * IAM Iterator
553  */
554 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
555                                              const struct iam_container *bag)
556 {
557         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
558                                            osd_oti_get(env)->oti_it_ipd);
559 }
560
561 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
562                                               const struct iam_container *bag)
563 {
564         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
565                                            osd_oti_get(env)->oti_idx_ipd);
566 }
567
568 static void osd_ipd_put(const struct lu_env *env,
569                         const struct iam_container *bag,
570                         struct iam_path_descr *ipd)
571 {
572         bag->ic_descr->id_ops->id_ipd_free(ipd);
573 }
574
575 /*
576  * Concurrency: no concurrent access is possible that late in object
577  * life-cycle.
578  */
579 static void osd_index_fini(struct osd_object *o)
580 {
581         struct iam_container *bag;
582
583         if (o->oo_dir != NULL) {
584                 bag = &o->oo_dir->od_container;
585                 if (o->oo_inode != NULL) {
586                         if (bag->ic_object == o->oo_inode)
587                                 iam_container_fini(bag);
588                 }
589                 OBD_FREE_PTR(o->oo_dir);
590                 o->oo_dir = NULL;
591         }
592 }
593
594 /*
595  * Concurrency: no concurrent access is possible that late in object
596  * life-cycle (for all existing callers, that is. New callers have to provide
597  * their own locking.)
598  */
599 static int osd_inode_unlinked(const struct inode *inode)
600 {
601         return inode->i_nlink == 0;
602 }
603
604 enum {
605         OSD_TXN_OI_DELETE_CREDITS    = 20,
606         OSD_TXN_INODE_DELETE_CREDITS = 20
607 };
608
609 /*
610  * Journal
611  */
612
613 #if OSD_THANDLE_STATS
614 /**
615  * Set time when the handle is allocated
616  */
617 static void osd_th_alloced(struct osd_thandle *oth)
618 {
619         oth->oth_alloced = cfs_time_current();
620 }
621
622 /**
623  * Set time when the handle started
624  */
625 static void osd_th_started(struct osd_thandle *oth)
626 {
627         oth->oth_started = cfs_time_current();
628 }
629
630 /**
631  * Helper function to convert time interval to microseconds packed in
632  * long int (default time units for the counter in "stats" initialized
633  * by lu_time_init() )
634  */
635 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
636 {
637         struct timeval val;
638
639         cfs_duration_usec(cfs_time_sub(end, start), &val);
640         return val.tv_sec * 1000000 + val.tv_usec;
641 }
642
643 /**
644  * Check whether the we deal with this handle for too long.
645  */
646 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
647                                 cfs_time_t alloced, cfs_time_t started,
648                                 cfs_time_t closed)
649 {
650         cfs_time_t now = cfs_time_current();
651
652         LASSERT(dev != NULL);
653
654         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
655                             interval_to_usec(alloced, started));
656         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
657                             interval_to_usec(started, closed));
658         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
659                             interval_to_usec(closed, now));
660
661         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
662                 CWARN("transaction handle %p was open for too long: "
663                       "now "CFS_TIME_T" ,"
664                       "alloced "CFS_TIME_T" ,"
665                       "started "CFS_TIME_T" ,"
666                       "closed "CFS_TIME_T"\n",
667                       oth, now, alloced, started, closed);
668                 libcfs_debug_dumpstack(NULL);
669         }
670 }
671
672 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
673 {                                                                       \
674         cfs_time_t __closed = cfs_time_current();                       \
675         cfs_time_t __alloced = oth->oth_alloced;                        \
676         cfs_time_t __started = oth->oth_started;                        \
677                                                                         \
678         expr;                                                           \
679         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
680 }
681
682 #else /* OSD_THANDLE_STATS */
683
684 #define osd_th_alloced(h)                  do {} while(0)
685 #define osd_th_started(h)                  do {} while(0)
686 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
687
688 #endif /* OSD_THANDLE_STATS */
689
690 /*
691  * Concurrency: doesn't access mutable data.
692  */
693 static int osd_param_is_sane(const struct osd_device *dev,
694                              const struct thandle *th)
695 {
696         struct osd_thandle *oh;
697         oh = container_of0(th, struct osd_thandle, ot_super);
698         return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
699 }
700
701 /*
702  * Concurrency: shouldn't matter.
703  */
704 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
705 static void osd_trans_commit_cb(struct super_block *sb,
706                                 struct journal_callback *jcb, int error)
707 #else
708 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
709 #endif
710 {
711         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
712         struct thandle     *th  = &oh->ot_super;
713         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
714         struct dt_txn_commit_cb *dcb, *tmp;
715
716         LASSERT(oh->ot_handle == NULL);
717
718         if (error)
719                 CERROR("transaction @0x%p commit error: %d\n", th, error);
720
721         dt_txn_hook_commit(th);
722
723         /* call per-transaction callbacks if any */
724         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
725                 dcb->dcb_func(NULL, th, dcb, error);
726
727         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
728         lu_device_put(lud);
729         th->th_dev = NULL;
730
731         lu_context_exit(&th->th_ctx);
732         lu_context_fini(&th->th_ctx);
733         OBD_FREE_PTR(oh);
734 }
735
736 static struct thandle *osd_trans_create(const struct lu_env *env,
737                                         struct dt_device *d)
738 {
739         struct osd_thread_info *oti = osd_oti_get(env);
740         struct osd_thandle     *oh;
741         struct thandle         *th;
742         ENTRY;
743
744         th = ERR_PTR(-ENOMEM);
745         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
746         if (oh != NULL) {
747                 th = &oh->ot_super;
748                 th->th_dev = d;
749                 th->th_result = 0;
750                 th->th_tags = LCT_TX_HANDLE;
751                 oh->ot_credits = 0;
752                 oti->oti_dev = osd_dt_dev(d);
753                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
754                 osd_th_alloced(oh);
755         }
756         RETURN(th);
757 }
758
759 /*
760  * Concurrency: shouldn't matter.
761  */
762 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
763                     struct thandle *th)
764 {
765         struct osd_thread_info *oti = osd_oti_get(env);
766         struct osd_device  *dev = osd_dt_dev(d);
767         handle_t           *jh;
768         struct osd_thandle *oh;
769         int rc;
770
771         ENTRY;
772
773         LASSERT(current->journal_info == NULL);
774
775         oh = container_of0(th, struct osd_thandle, ot_super);
776         LASSERT(oh != NULL);
777         LASSERT(oh->ot_handle == NULL);
778
779         rc = dt_txn_hook_start(env, d, th);
780         if (rc != 0)
781                 GOTO(out, rc);
782
783         oh->ot_credits += LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
784
785         if (!osd_param_is_sane(dev, th)) {
786                 CWARN("%s: too many transaction credits (%d > %d)\n",
787                       d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
788                       osd_journal(dev)->j_max_transaction_buffers);
789 #ifdef OSD_TRACK_DECLARES
790                 CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
791                        oh->ot_declare_attr_set, oh->ot_declare_punch,
792                        oh->ot_declare_xattr_set);
793                 CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
794                        oh->ot_declare_create, oh->ot_declare_ref_add,
795                        oh->ot_declare_ref_del, oh->ot_declare_write);
796                 CERROR("  insert: %d, delete: %d\n",
797                        oh->ot_declare_insert, oh->ot_declare_delete);
798 #endif
799         }
800
801         /*
802          * XXX temporary stuff. Some abstraction layer should
803          * be used.
804          */
805         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
806         osd_th_started(oh);
807         if (!IS_ERR(jh)) {
808                 oh->ot_handle = jh;
809                 LASSERT(oti->oti_txns == 0);
810                 lu_context_init(&th->th_ctx, th->th_tags);
811                 lu_context_enter(&th->th_ctx);
812
813                 lu_device_get(&d->dd_lu_dev);
814                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
815                                              "osd-tx", th);
816
817                 /*
818                  * XXX: current rule is that we first start tx,
819                  *      then lock object(s), but we can't use
820                  *      this rule for data (due to locking specifics
821                  *      in ldiskfs). also in long-term we'd like to
822                  *      use usually-used (locks;tx) ordering. so,
823                  *      UGLY thing is that we'll use one ordering for
824                  *      data (ofd) and reverse ordering for metadata
825                  *      (mdd). then at some point we'll fix the latter
826                  */
827                 if (lu_device_is_md(&d->dd_lu_dev)) {
828                         LASSERT(oti->oti_r_locks == 0);
829                         LASSERT(oti->oti_w_locks == 0);
830                 }
831
832                 oti->oti_txns++;
833                 rc = 0;
834         } else {
835                 rc = PTR_ERR(jh);
836         }
837 out:
838         RETURN(rc);
839 }
840
841 /*
842  * Concurrency: shouldn't matter.
843  */
844 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
845 {
846         int                     rc = 0;
847         struct osd_thandle     *oh;
848         struct osd_thread_info *oti = osd_oti_get(env);
849
850         ENTRY;
851
852         oh = container_of0(th, struct osd_thandle, ot_super);
853
854         if (oh->ot_handle != NULL) {
855                 handle_t *hdl = oh->ot_handle;
856
857                 hdl->h_sync = th->th_sync;
858
859                 /*
860                  * add commit callback
861                  * notice we don't do this in osd_trans_start()
862                  * as underlying transaction can change during truncate
863                  */
864                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
865                                          &oh->ot_jcb);
866
867                 LASSERT(oti->oti_txns == 1);
868                 oti->oti_txns--;
869                 /*
870                  * XXX: current rule is that we first start tx,
871                  *      then lock object(s), but we can't use
872                  *      this rule for data (due to locking specifics
873                  *      in ldiskfs). also in long-term we'd like to
874                  *      use usually-used (locks;tx) ordering. so,
875                  *      UGLY thing is that we'll use one ordering for
876                  *      data (ofd) and reverse ordering for metadata
877                  *      (mdd). then at some point we'll fix the latter
878                  */
879                 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
880                         LASSERT(oti->oti_r_locks == 0);
881                         LASSERT(oti->oti_w_locks == 0);
882                 }
883                 rc = dt_txn_hook_stop(env, th);
884                 if (rc != 0)
885                         CERROR("Failure in transaction hook: %d\n", rc);
886                 oh->ot_handle = NULL;
887                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
888                                   rc = ldiskfs_journal_stop(hdl));
889                 if (rc != 0)
890                         CERROR("Failure to stop transaction: %d\n", rc);
891         } else {
892                 OBD_FREE_PTR(oh);
893         }
894
895         RETURN(rc);
896 }
897
898 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
899 {
900         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
901                                                ot_super);
902
903         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
904
905         return 0;
906 }
907
908 /*
909  * Called just before object is freed. Releases all resources except for
910  * object itself (that is released by osd_object_free()).
911  *
912  * Concurrency: no concurrent access is possible that late in object
913  * life-cycle.
914  */
915 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
916 {
917         struct osd_object *obj   = osd_obj(l);
918         struct inode      *inode = obj->oo_inode;
919
920         LINVRNT(osd_invariant(obj));
921
922         /*
923          * If object is unlinked remove fid->ino mapping from object index.
924          */
925
926         osd_index_fini(obj);
927         if (inode != NULL) {
928                 iput(inode);
929                 obj->oo_inode = NULL;
930         }
931 }
932
933 /*
934  * Concurrency: ->loo_object_release() is called under site spin-lock.
935  */
936 static void osd_object_release(const struct lu_env *env,
937                                struct lu_object *l)
938 {
939 }
940
941 /*
942  * Concurrency: shouldn't matter.
943  */
944 static int osd_object_print(const struct lu_env *env, void *cookie,
945                             lu_printer_t p, const struct lu_object *l)
946 {
947         struct osd_object *o = osd_obj(l);
948         struct iam_descr  *d;
949
950         if (o->oo_dir != NULL)
951                 d = o->oo_dir->od_container.ic_descr;
952         else
953                 d = NULL;
954         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
955                     o, o->oo_inode,
956                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
957                     o->oo_inode ? o->oo_inode->i_generation : 0,
958                     d ? d->id_ops->id_name : "plain");
959 }
960
961 /*
962  * Concurrency: shouldn't matter.
963  */
964 int osd_statfs(const struct lu_env *env, struct dt_device *d,
965                cfs_kstatfs_t *sfs)
966 {
967         struct osd_device *osd = osd_dt_dev(d);
968         struct super_block *sb = osd_sb(osd);
969         int result = 0;
970
971         cfs_spin_lock(&osd->od_osfs_lock);
972         /* cache 1 second */
973         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
974                 result = ll_do_statfs(sb, &osd->od_kstatfs);
975                 if (likely(result == 0)) /* N.B. statfs can't really fail */
976                         osd->od_osfs_age = cfs_time_current_64();
977         }
978
979         if (likely(result == 0))
980                 *sfs = osd->od_kstatfs;
981         cfs_spin_unlock(&osd->od_osfs_lock);
982
983         return result;
984 }
985
986 /*
987  * Concurrency: doesn't access mutable data.
988  */
989 static void osd_conf_get(const struct lu_env *env,
990                          const struct dt_device *dev,
991                          struct dt_device_param *param)
992 {
993         struct super_block *sb = osd_sb(osd_dt_dev(dev));
994
995         /*
996          * XXX should be taken from not-yet-existing fs abstraction layer.
997          */
998         param->ddp_max_name_len = LDISKFS_NAME_LEN;
999         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1000         param->ddp_block_shift  = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
1001         param->ddp_mntopts      = 0;
1002         if (test_opt(sb, XATTR_USER))
1003                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1004         if (test_opt(sb, POSIX_ACL))
1005                 param->ddp_mntopts |= MNTOPT_ACL;
1006
1007 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1008         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1009                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1010         else
1011 #endif
1012                 param->ddp_max_ea_size = sb->s_blocksize;
1013
1014 }
1015
1016 /**
1017  * Helper function to get and fill the buffer with input values.
1018  */
1019 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1020 {
1021         struct lu_buf *buf;
1022
1023         buf = &osd_oti_get(env)->oti_buf;
1024         buf->lb_buf = area;
1025         buf->lb_len = len;
1026         return buf;
1027 }
1028
1029 /*
1030  * Concurrency: shouldn't matter.
1031  */
1032 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1033 {
1034         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1035         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1036 }
1037
1038 /**
1039  * Start commit for OSD device.
1040  *
1041  * An implementation of dt_commit_async method for OSD device.
1042  * Asychronously starts underlayng fs sync and thereby a transaction
1043  * commit.
1044  *
1045  * \param env environment
1046  * \param d dt device
1047  *
1048  * \see dt_device_operations
1049  */
1050 static int osd_commit_async(const struct lu_env *env,
1051                             struct dt_device *d)
1052 {
1053         struct super_block *s = osd_sb(osd_dt_dev(d));
1054         ENTRY;
1055
1056         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1057         RETURN(s->s_op->sync_fs(s, 0));
1058 }
1059
1060 /*
1061  * Concurrency: shouldn't matter.
1062  */
1063
1064 static void osd_ro(const struct lu_env *env, struct dt_device *d)
1065 {
1066         struct super_block *sb = osd_sb(osd_dt_dev(d));
1067         ENTRY;
1068
1069         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1070
1071         __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1072         EXIT;
1073 }
1074
1075 /*
1076  * Concurrency: serialization provided by callers.
1077  */
1078 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1079                               int mode, unsigned long timeout, __u32 alg,
1080                               struct lustre_capa_key *keys)
1081 {
1082         struct osd_device *dev = osd_dt_dev(d);
1083         ENTRY;
1084
1085         dev->od_fl_capa = mode;
1086         dev->od_capa_timeout = timeout;
1087         dev->od_capa_alg = alg;
1088         dev->od_capa_keys = keys;
1089         RETURN(0);
1090 }
1091
1092 /**
1093  * Concurrency: serialization provided by callers.
1094  */
1095 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1096                                struct dt_quota_ctxt *ctxt, void *data)
1097 {
1098         struct obd_device *obd = (void *)ctxt;
1099         struct vfsmount *mnt = (struct vfsmount *)data;
1100         ENTRY;
1101
1102         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1103         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1104         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1105         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1106         obd->obd_lvfs_ctxt.fs = get_ds();
1107
1108         EXIT;
1109 }
1110
1111 /**
1112  * Note: we do not count into QUOTA here.
1113  * If we mount with --data_journal we may need more.
1114  */
1115 static const int osd_dto_credits_noquota[DTO_NR] = {
1116         /**
1117          * Insert/Delete.
1118          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1119          * SINGLEDATA_TRANS_BLOCKS(8)
1120          * XXX Note: maybe iam need more, since iam have more level than
1121          *           EXT3 htree.
1122          */
1123         [DTO_INDEX_INSERT]  = 16,
1124         [DTO_INDEX_DELETE]  = 16,
1125         /**
1126          * Unused now
1127          */
1128         [DTO_INDEX_UPDATE]  = 16,
1129         /**
1130          * Create a object. The same as create object in EXT3.
1131          * DATA_TRANS_BLOCKS(14) +
1132          * INDEX_EXTRA_BLOCKS(8) +
1133          * 3(inode bits, groups, GDT)
1134          */
1135         [DTO_OBJECT_CREATE] = 25,
1136         /**
1137          * XXX: real credits to be fixed
1138          */
1139         [DTO_OBJECT_DELETE] = 25,
1140         /**
1141          * Attr set credits (inode)
1142          */
1143         [DTO_ATTR_SET_BASE] = 1,
1144         /**
1145          * Xattr set. The same as xattr of EXT3.
1146          * DATA_TRANS_BLOCKS(14)
1147          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1148          * are also counted in. Do not know why?
1149          */
1150         [DTO_XATTR_SET]     = 14,
1151         [DTO_LOG_REC]       = 14,
1152         /**
1153          * credits for inode change during write.
1154          */
1155         [DTO_WRITE_BASE]    = 3,
1156         /**
1157          * credits for single block write.
1158          */
1159         [DTO_WRITE_BLOCK]   = 14,
1160         /**
1161          * Attr set credits for chown.
1162          * This is extra credits for setattr, and it is null without quota
1163          */
1164         [DTO_ATTR_SET_CHOWN]= 0
1165 };
1166
1167 /**
1168  * Note: we count into QUOTA here.
1169  * If we mount with --data_journal we may need more.
1170  */
1171 static const int osd_dto_credits_quota[DTO_NR] = {
1172         /**
1173          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1174          * SINGLEDATA_TRANS_BLOCKS(8) +
1175          * 2 * QUOTA_TRANS_BLOCKS(2)
1176          */
1177         [DTO_INDEX_INSERT]  = 20,
1178         /**
1179          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1180          * SINGLEDATA_TRANS_BLOCKS(8) +
1181          * 2 * QUOTA_TRANS_BLOCKS(2)
1182          */
1183         [DTO_INDEX_DELETE]  = 20,
1184         /**
1185          * Unused now.
1186          */
1187         [DTO_INDEX_UPDATE]  = 16,
1188         /*
1189          * Create a object. Same as create object in EXT3 filesystem.
1190          * DATA_TRANS_BLOCKS(16) +
1191          * INDEX_EXTRA_BLOCKS(8) +
1192          * 3(inode bits, groups, GDT) +
1193          * 2 * QUOTA_INIT_BLOCKS(25)
1194          */
1195         [DTO_OBJECT_CREATE] = 77,
1196         /*
1197          * Unused now.
1198          * DATA_TRANS_BLOCKS(16) +
1199          * INDEX_EXTRA_BLOCKS(8) +
1200          * 3(inode bits, groups, GDT) +
1201          * QUOTA(?)
1202          */
1203         [DTO_OBJECT_DELETE] = 27,
1204         /**
1205          * Attr set credits.
1206          * 3 (inode bit, group, GDT) +
1207          */
1208         [DTO_ATTR_SET_BASE] = 3,
1209         /**
1210          * Xattr set. The same as xattr of EXT3.
1211          * DATA_TRANS_BLOCKS(16)
1212          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1213          *           also counted in. Do not know why?
1214          */
1215         [DTO_XATTR_SET]     = 16,
1216         [DTO_LOG_REC]       = 16,
1217         /**
1218          * creadits for inode change during write.
1219          */
1220         [DTO_WRITE_BASE]    = 3,
1221         /**
1222          * credits for single block write.
1223          */
1224         [DTO_WRITE_BLOCK]   = 16,
1225         /**
1226          * Attr set credits for chown.
1227          * It is added to already set setattr credits
1228          * 2 * QUOTA_INIT_BLOCKS(25) +
1229          * 2 * QUOTA_DEL_BLOCKS(9)
1230          */
1231         [DTO_ATTR_SET_CHOWN]= 68,
1232 };
1233
1234 static const struct dt_device_operations osd_dt_ops = {
1235         .dt_root_get       = osd_root_get,
1236         .dt_statfs         = osd_statfs,
1237         .dt_trans_create   = osd_trans_create,
1238         .dt_trans_start    = osd_trans_start,
1239         .dt_trans_stop     = osd_trans_stop,
1240         .dt_trans_cb_add   = osd_trans_cb_add,
1241         .dt_conf_get       = osd_conf_get,
1242         .dt_sync           = osd_sync,
1243         .dt_ro             = osd_ro,
1244         .dt_commit_async   = osd_commit_async,
1245         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1246         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1247 };
1248
1249 static void osd_object_read_lock(const struct lu_env *env,
1250                                  struct dt_object *dt, unsigned role)
1251 {
1252         struct osd_object *obj = osd_dt_obj(dt);
1253         struct osd_thread_info *oti = osd_oti_get(env);
1254
1255         LINVRNT(osd_invariant(obj));
1256
1257         LASSERT(obj->oo_owner != env);
1258         cfs_down_read_nested(&obj->oo_sem, role);
1259
1260         LASSERT(obj->oo_owner == NULL);
1261         oti->oti_r_locks++;
1262 }
1263
1264 static void osd_object_write_lock(const struct lu_env *env,
1265                                   struct dt_object *dt, unsigned role)
1266 {
1267         struct osd_object *obj = osd_dt_obj(dt);
1268         struct osd_thread_info *oti = osd_oti_get(env);
1269
1270         LINVRNT(osd_invariant(obj));
1271
1272         LASSERT(obj->oo_owner != env);
1273         cfs_down_write_nested(&obj->oo_sem, role);
1274
1275         LASSERT(obj->oo_owner == NULL);
1276         obj->oo_owner = env;
1277         oti->oti_w_locks++;
1278 }
1279
1280 static void osd_object_read_unlock(const struct lu_env *env,
1281                                    struct dt_object *dt)
1282 {
1283         struct osd_object *obj = osd_dt_obj(dt);
1284         struct osd_thread_info *oti = osd_oti_get(env);
1285
1286         LINVRNT(osd_invariant(obj));
1287
1288         LASSERT(oti->oti_r_locks > 0);
1289         oti->oti_r_locks--;
1290         cfs_up_read(&obj->oo_sem);
1291 }
1292
1293 static void osd_object_write_unlock(const struct lu_env *env,
1294                                     struct dt_object *dt)
1295 {
1296         struct osd_object *obj = osd_dt_obj(dt);
1297         struct osd_thread_info *oti = osd_oti_get(env);
1298
1299         LINVRNT(osd_invariant(obj));
1300
1301         LASSERT(obj->oo_owner == env);
1302         LASSERT(oti->oti_w_locks > 0);
1303         oti->oti_w_locks--;
1304         obj->oo_owner = NULL;
1305         cfs_up_write(&obj->oo_sem);
1306 }
1307
1308 static int osd_object_write_locked(const struct lu_env *env,
1309                                    struct dt_object *dt)
1310 {
1311         struct osd_object *obj = osd_dt_obj(dt);
1312
1313         LINVRNT(osd_invariant(obj));
1314
1315         return obj->oo_owner == env;
1316 }
1317
1318 static int capa_is_sane(const struct lu_env *env,
1319                         struct osd_device *dev,
1320                         struct lustre_capa *capa,
1321                         struct lustre_capa_key *keys)
1322 {
1323         struct osd_thread_info *oti = osd_oti_get(env);
1324         struct lustre_capa *tcapa = &oti->oti_capa;
1325         struct obd_capa *oc;
1326         int i, rc = 0;
1327         ENTRY;
1328
1329         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1330         if (oc) {
1331                 if (capa_is_expired(oc)) {
1332                         DEBUG_CAPA(D_ERROR, capa, "expired");
1333                         rc = -ESTALE;
1334                 }
1335                 capa_put(oc);
1336                 RETURN(rc);
1337         }
1338
1339         if (capa_is_expired_sec(capa)) {
1340                 DEBUG_CAPA(D_ERROR, capa, "expired");
1341                 RETURN(-ESTALE);
1342         }
1343
1344         cfs_spin_lock(&capa_lock);
1345         for (i = 0; i < 2; i++) {
1346                 if (keys[i].lk_keyid == capa->lc_keyid) {
1347                         oti->oti_capa_key = keys[i];
1348                         break;
1349                 }
1350         }
1351         cfs_spin_unlock(&capa_lock);
1352
1353         if (i == 2) {
1354                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1355                 RETURN(-ESTALE);
1356         }
1357
1358         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1359         if (rc)
1360                 RETURN(rc);
1361
1362         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1363                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1364                 RETURN(-EACCES);
1365         }
1366
1367         oc = capa_add(dev->od_capa_hash, capa);
1368         capa_put(oc);
1369
1370         RETURN(0);
1371 }
1372
1373 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1374                            struct lustre_capa *capa, __u64 opc)
1375 {
1376         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1377         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1378         struct md_capainfo *ci;
1379         int rc;
1380
1381         if (!dev->od_fl_capa)
1382                 return 0;
1383
1384         if (capa == BYPASS_CAPA)
1385                 return 0;
1386
1387         ci = md_capainfo(env);
1388         if (unlikely(!ci))
1389                 return 0;
1390
1391         if (ci->mc_auth == LC_ID_NONE)
1392                 return 0;
1393
1394         if (!capa) {
1395                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1396                 return -EACCES;
1397         }
1398
1399         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1400                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1401                            PFID(fid));
1402                 return -EACCES;
1403         }
1404
1405         if (!capa_opc_supported(capa, opc)) {
1406                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1407                 return -EACCES;
1408         }
1409
1410         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1411                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1412                 return -EACCES;
1413         }
1414
1415         return 0;
1416 }
1417
1418 static struct timespec *osd_inode_time(const struct lu_env *env,
1419                                        struct inode *inode, __u64 seconds)
1420 {
1421         struct osd_thread_info *oti = osd_oti_get(env);
1422         struct timespec        *t   = &oti->oti_time;
1423
1424         t->tv_sec  = seconds;
1425         t->tv_nsec = 0;
1426         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1427         return t;
1428 }
1429
1430
1431 static void osd_inode_getattr(const struct lu_env *env,
1432                               struct inode *inode, struct lu_attr *attr)
1433 {
1434         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1435                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1436                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1437
1438         attr->la_atime      = LTIME_S(inode->i_atime);
1439         attr->la_mtime      = LTIME_S(inode->i_mtime);
1440         attr->la_ctime      = LTIME_S(inode->i_ctime);
1441         attr->la_mode       = inode->i_mode;
1442         attr->la_size       = i_size_read(inode);
1443         attr->la_blocks     = inode->i_blocks;
1444         attr->la_uid        = inode->i_uid;
1445         attr->la_gid        = inode->i_gid;
1446         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1447         attr->la_nlink      = inode->i_nlink;
1448         attr->la_rdev       = inode->i_rdev;
1449         attr->la_blksize    = ll_inode_blksize(inode);
1450         attr->la_blkbits    = inode->i_blkbits;
1451 }
1452
1453 static int osd_attr_get(const struct lu_env *env,
1454                         struct dt_object *dt,
1455                         struct lu_attr *attr,
1456                         struct lustre_capa *capa)
1457 {
1458         struct osd_object *obj = osd_dt_obj(dt);
1459
1460         LASSERT(dt_object_exists(dt));
1461         LINVRNT(osd_invariant(obj));
1462
1463         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1464                 return -EACCES;
1465
1466         cfs_spin_lock(&obj->oo_guard);
1467         osd_inode_getattr(env, obj->oo_inode, attr);
1468         cfs_spin_unlock(&obj->oo_guard);
1469         return 0;
1470 }
1471
1472 static int osd_declare_attr_set(const struct lu_env *env,
1473                                 struct dt_object *dt,
1474                                 const struct lu_attr *attr,
1475                                 struct thandle *handle)
1476 {
1477         struct osd_thandle *oh;
1478
1479         LASSERT(handle != NULL);
1480         LASSERT(osd_invariant(obj));
1481
1482         oh = container_of0(handle, struct osd_thandle, ot_super);
1483         LASSERT(oh->ot_handle == NULL);
1484
1485         OSD_DECLARE_OP(oh, attr_set);
1486         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1487
1488         return 0;
1489 }
1490
1491 static int osd_inode_setattr(const struct lu_env *env,
1492                              struct inode *inode, const struct lu_attr *attr)
1493 {
1494         __u64 bits;
1495
1496         bits = attr->la_valid;
1497
1498         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1499
1500 #ifdef HAVE_QUOTA_SUPPORT
1501         if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1502             (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1503                 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1504                 struct iattr iattr;
1505                 int rc;
1506
1507                 iattr.ia_valid = 0;
1508                 if (bits & LA_UID)
1509                         iattr.ia_valid |= ATTR_UID;
1510                 if (bits & LA_GID)
1511                         iattr.ia_valid |= ATTR_GID;
1512                 iattr.ia_uid = attr->la_uid;
1513                 iattr.ia_gid = attr->la_gid;
1514                 osd_push_ctxt(env, save);
1515                 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1516                 osd_pop_ctxt(save);
1517                 if (rc != 0)
1518                         return rc;
1519         }
1520 #endif
1521
1522         if (bits & LA_ATIME)
1523                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1524         if (bits & LA_CTIME)
1525                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1526         if (bits & LA_MTIME)
1527                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1528         if (bits & LA_SIZE) {
1529                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1530                 i_size_write(inode, attr->la_size);
1531         }
1532
1533 #if 0
1534         /* OSD should not change "i_blocks" which is used by quota.
1535          * "i_blocks" should be changed by ldiskfs only. */
1536         if (bits & LA_BLOCKS)
1537                 inode->i_blocks = attr->la_blocks;
1538 #endif
1539         if (bits & LA_MODE)
1540                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1541                         (attr->la_mode & ~S_IFMT);
1542         if (bits & LA_UID)
1543                 inode->i_uid    = attr->la_uid;
1544         if (bits & LA_GID)
1545                 inode->i_gid    = attr->la_gid;
1546         if (bits & LA_NLINK)
1547                 inode->i_nlink  = attr->la_nlink;
1548         if (bits & LA_RDEV)
1549                 inode->i_rdev   = attr->la_rdev;
1550
1551         if (bits & LA_FLAGS) {
1552                 /* always keep S_NOCMTIME */
1553                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1554                                  S_NOCMTIME;
1555         }
1556         return 0;
1557 }
1558
1559 static int osd_attr_set(const struct lu_env *env,
1560                         struct dt_object *dt,
1561                         const struct lu_attr *attr,
1562                         struct thandle *handle,
1563                         struct lustre_capa *capa)
1564 {
1565         struct osd_object *obj = osd_dt_obj(dt);
1566         int rc;
1567
1568         LASSERT(handle != NULL);
1569         LASSERT(dt_object_exists(dt));
1570         LASSERT(osd_invariant(obj));
1571
1572         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1573                 return -EACCES;
1574
1575         OSD_EXEC_OP(handle, attr_set);
1576
1577         cfs_spin_lock(&obj->oo_guard);
1578         rc = osd_inode_setattr(env, obj->oo_inode, attr);
1579         cfs_spin_unlock(&obj->oo_guard);
1580
1581         if (!rc)
1582                 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1583         return rc;
1584 }
1585
1586 /*
1587  * Object creation.
1588  *
1589  * XXX temporary solution.
1590  */
1591 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1592                           struct lu_attr *attr, struct thandle *th)
1593 {
1594         return 0;
1595 }
1596
1597 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1598                            struct lu_attr *attr, struct thandle *th)
1599 {
1600         osd_object_init0(obj);
1601         if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1602                 unlock_new_inode(obj->oo_inode);
1603         return 0;
1604 }
1605
1606 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1607                                             struct osd_object *obj,
1608                                             const char *name,
1609                                             const int namelen)
1610 {
1611         struct osd_thread_info *info   = osd_oti_get(env);
1612         struct dentry *child_dentry = &info->oti_child_dentry;
1613         struct dentry *obj_dentry = &info->oti_obj_dentry;
1614
1615         obj_dentry->d_inode = obj->oo_inode;
1616         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1617         obj_dentry->d_name.hash = 0;
1618
1619         child_dentry->d_name.hash = 0;
1620         child_dentry->d_parent = obj_dentry;
1621         child_dentry->d_name.name = name;
1622         child_dentry->d_name.len = namelen;
1623         return child_dentry;
1624 }
1625
1626
1627 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1628                       cfs_umode_t mode,
1629                       struct dt_allocation_hint *hint,
1630                       struct thandle *th)
1631 {
1632         int result;
1633         struct osd_device  *osd = osd_obj2dev(obj);
1634         struct osd_thandle *oth;
1635         struct dt_object   *parent;
1636         struct inode       *inode;
1637 #ifdef HAVE_QUOTA_SUPPORT
1638         struct osd_ctxt    *save = &info->oti_ctxt;
1639 #endif
1640
1641         LINVRNT(osd_invariant(obj));
1642         LASSERT(obj->oo_inode == NULL);
1643         LASSERT(obj->oo_hl_head == NULL);
1644
1645         if (S_ISDIR(mode) && ldiskfs_pdo) {
1646                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1647                 if (obj->oo_hl_head == NULL)
1648                         return -ENOMEM;
1649         }
1650
1651         oth = container_of(th, struct osd_thandle, ot_super);
1652         LASSERT(oth->ot_handle->h_transaction != NULL);
1653
1654         if (hint && hint->dah_parent)
1655                 parent = hint->dah_parent;
1656         else
1657                 parent = osd->od_obj_area;
1658
1659         LASSERT(parent != NULL);
1660         LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1661
1662 #ifdef HAVE_QUOTA_SUPPORT
1663         osd_push_ctxt(info->oti_env, save);
1664 #endif
1665         inode = ldiskfs_create_inode(oth->ot_handle,
1666                                      osd_dt_obj(parent)->oo_inode, mode);
1667 #ifdef HAVE_QUOTA_SUPPORT
1668         osd_pop_ctxt(save);
1669 #endif
1670         if (!IS_ERR(inode)) {
1671                 /* Do not update file c/mtime in ldiskfs.
1672                  * NB: don't need any lock because no contention at this
1673                  * early stage */
1674                 inode->i_flags |= S_NOCMTIME;
1675                 obj->oo_inode = inode;
1676                 result = 0;
1677         } else {
1678                 if (obj->oo_hl_head != NULL) {
1679                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1680                         obj->oo_hl_head = NULL;
1681                 }
1682                 result = PTR_ERR(inode);
1683         }
1684         LINVRNT(osd_invariant(obj));
1685         return result;
1686 }
1687
1688 enum {
1689         OSD_NAME_LEN = 255
1690 };
1691
1692 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1693                      struct lu_attr *attr,
1694                      struct dt_allocation_hint *hint,
1695                      struct dt_object_format *dof,
1696                      struct thandle *th)
1697 {
1698         int result;
1699         struct osd_thandle *oth;
1700         struct osd_device *osd = osd_obj2dev(obj);
1701         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1702
1703         LASSERT(S_ISDIR(attr->la_mode));
1704
1705         oth = container_of(th, struct osd_thandle, ot_super);
1706         LASSERT(oth->ot_handle->h_transaction != NULL);
1707         result = osd_mkfile(info, obj, mode, hint, th);
1708         if (result == 0 && osd->od_iop_mode == 0) {
1709                 LASSERT(obj->oo_inode != NULL);
1710                 /*
1711                  * XXX uh-oh... call low-level iam function directly.
1712                  */
1713
1714                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1715                                          sizeof (struct osd_fid_pack),
1716                                          oth->ot_handle);
1717         }
1718         return result;
1719 }
1720
1721 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1722                         struct lu_attr *attr,
1723                         struct dt_allocation_hint *hint,
1724                         struct dt_object_format *dof,
1725                         struct thandle *th)
1726 {
1727         int result;
1728         struct osd_thandle *oth;
1729         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1730
1731         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1732
1733         LASSERT(S_ISREG(attr->la_mode));
1734
1735         oth = container_of(th, struct osd_thandle, ot_super);
1736         LASSERT(oth->ot_handle->h_transaction != NULL);
1737
1738         result = osd_mkfile(info, obj, mode, hint, th);
1739         if (result == 0) {
1740                 LASSERT(obj->oo_inode != NULL);
1741                 if (feat->dif_flags & DT_IND_VARKEY)
1742                         result = iam_lvar_create(obj->oo_inode,
1743                                                  feat->dif_keysize_max,
1744                                                  feat->dif_ptrsize,
1745                                                  feat->dif_recsize_max,
1746                                                  oth->ot_handle);
1747                 else
1748                         result = iam_lfix_create(obj->oo_inode,
1749                                                  feat->dif_keysize_max,
1750                                                  feat->dif_ptrsize,
1751                                                  feat->dif_recsize_max,
1752                                                  oth->ot_handle);
1753
1754         }
1755         return result;
1756 }
1757
1758 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1759                      struct lu_attr *attr,
1760                      struct dt_allocation_hint *hint,
1761                      struct dt_object_format *dof,
1762                      struct thandle *th)
1763 {
1764         LASSERT(S_ISREG(attr->la_mode));
1765         return osd_mkfile(info, obj, (attr->la_mode &
1766                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1767 }
1768
1769 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1770                      struct lu_attr *attr,
1771                      struct dt_allocation_hint *hint,
1772                      struct dt_object_format *dof,
1773                      struct thandle *th)
1774 {
1775         LASSERT(S_ISLNK(attr->la_mode));
1776         return osd_mkfile(info, obj, (attr->la_mode &
1777                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1778 }
1779
1780 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1781                      struct lu_attr *attr,
1782                      struct dt_allocation_hint *hint,
1783                      struct dt_object_format *dof,
1784                      struct thandle *th)
1785 {
1786         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1787         int result;
1788
1789         LINVRNT(osd_invariant(obj));
1790         LASSERT(obj->oo_inode == NULL);
1791         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1792                 S_ISFIFO(mode) || S_ISSOCK(mode));
1793
1794         result = osd_mkfile(info, obj, mode, hint, th);
1795         if (result == 0) {
1796                 LASSERT(obj->oo_inode != NULL);
1797                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1798         }
1799         LINVRNT(osd_invariant(obj));
1800         return result;
1801 }
1802
1803 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1804                               struct lu_attr *,
1805                               struct dt_allocation_hint *hint,
1806                               struct dt_object_format *dof,
1807                               struct thandle *);
1808
1809 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1810 {
1811         osd_obj_type_f result;
1812
1813         switch (type) {
1814         case DFT_DIR:
1815                 result = osd_mkdir;
1816                 break;
1817         case DFT_REGULAR:
1818                 result = osd_mkreg;
1819                 break;
1820         case DFT_SYM:
1821                 result = osd_mksym;
1822                 break;
1823         case DFT_NODE:
1824                 result = osd_mknod;
1825                 break;
1826         case DFT_INDEX:
1827                 result = osd_mk_index;
1828                 break;
1829
1830         default:
1831                 LBUG();
1832                 break;
1833         }
1834         return result;
1835 }
1836
1837
1838 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1839                         struct dt_object *parent, cfs_umode_t child_mode)
1840 {
1841         LASSERT(ah);
1842
1843         memset(ah, 0, sizeof(*ah));
1844         ah->dah_parent = parent;
1845         ah->dah_mode = child_mode;
1846 }
1847
1848 /**
1849  * Helper function for osd_object_create()
1850  *
1851  * \retval 0, on success
1852  */
1853 static int __osd_object_create(struct osd_thread_info *info,
1854                                struct osd_object *obj, struct lu_attr *attr,
1855                                struct dt_allocation_hint *hint,
1856                                struct dt_object_format *dof,
1857                                struct thandle *th)
1858 {
1859
1860         int result;
1861
1862         result = osd_create_pre(info, obj, attr, th);
1863         if (result == 0) {
1864                 result = osd_create_type_f(dof->dof_type)(info, obj,
1865                                            attr, hint, dof, th);
1866                 if (result == 0)
1867                         result = osd_create_post(info, obj, attr, th);
1868         }
1869         return result;
1870 }
1871
1872 /**
1873  * Helper function for osd_object_create()
1874  *
1875  * \retval 0, on success
1876  */
1877 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1878                            const struct lu_fid *fid, struct thandle *th)
1879 {
1880         struct osd_thread_info *info = osd_oti_get(env);
1881         struct osd_inode_id    *id   = &info->oti_id;
1882         struct osd_device      *osd  = osd_obj2dev(obj);
1883         struct md_ucred        *uc   = md_ucred(env);
1884
1885         LASSERT(obj->oo_inode != NULL);
1886         LASSERT(uc != NULL);
1887
1888         id->oii_ino = obj->oo_inode->i_ino;
1889         id->oii_gen = obj->oo_inode->i_generation;
1890
1891         return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1892                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1893 }
1894
1895 static int osd_declare_object_create(const struct lu_env *env,
1896                                      struct dt_object *dt,
1897                                      struct lu_attr *attr,
1898                                      struct dt_allocation_hint *hint,
1899                                      struct dt_object_format *dof,
1900                                      struct thandle *handle)
1901 {
1902         struct osd_thandle *oh;
1903
1904         LASSERT(handle != NULL);
1905
1906         oh = container_of0(handle, struct osd_thandle, ot_super);
1907         LASSERT(oh->ot_handle == NULL);
1908
1909         OSD_DECLARE_OP(oh, insert);
1910         OSD_DECLARE_OP(oh, create);
1911         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1912         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1913
1914         /* if this is directory, then we expect . and ..
1915          * to be inserted as well */
1916         OSD_DECLARE_OP(oh, insert);
1917         OSD_DECLARE_OP(oh, insert);
1918         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1919         return 0;
1920 }
1921
1922 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1923                              struct lu_attr *attr,
1924                              struct dt_allocation_hint *hint,
1925                              struct dt_object_format *dof,
1926                              struct thandle *th)
1927 {
1928         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1929         struct osd_object      *obj    = osd_dt_obj(dt);
1930         struct osd_thread_info *info   = osd_oti_get(env);
1931         int result;
1932
1933         ENTRY;
1934
1935         LINVRNT(osd_invariant(obj));
1936         LASSERT(!dt_object_exists(dt));
1937         LASSERT(osd_write_locked(env, obj));
1938         LASSERT(th != NULL);
1939
1940         OSD_EXEC_OP(th, create);
1941
1942         result = __osd_object_create(info, obj, attr, hint, dof, th);
1943         if (result == 0)
1944                 result = __osd_oi_insert(env, obj, fid, th);
1945
1946         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1947         LASSERT(osd_invariant(obj));
1948         RETURN(result);
1949 }
1950
1951 /**
1952  * Called to destroy on-disk representation of the object
1953  *
1954  * Concurrency: must be locked
1955  */
1956 static int osd_declare_object_destroy(const struct lu_env *env,
1957                                       struct dt_object *dt,
1958                                       struct thandle *th)
1959 {
1960         struct osd_object      *obj = osd_dt_obj(dt);
1961         struct inode           *inode = obj->oo_inode;
1962         struct osd_thandle     *oh;
1963         ENTRY;
1964
1965         oh = container_of0(th, struct osd_thandle, ot_super);
1966         LASSERT(oh->ot_handle == NULL);
1967         LASSERT(inode);
1968         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1969
1970         OSD_DECLARE_OP(oh, destroy);
1971         OSD_DECLARE_OP(oh, delete);
1972         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1973         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1974
1975         RETURN(0);
1976 }
1977
1978 static int osd_object_destroy(const struct lu_env *env,
1979                               struct dt_object *dt,
1980                               struct thandle *th)
1981 {
1982         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
1983         struct osd_object      *obj = osd_dt_obj(dt);
1984         struct inode           *inode = obj->oo_inode;
1985         struct osd_device      *osd = osd_obj2dev(obj);
1986         struct osd_thandle     *oh;
1987         int                     result;
1988         ENTRY;
1989
1990         oh = container_of0(th, struct osd_thandle, ot_super);
1991         LASSERT(oh->ot_handle);
1992         LASSERT(inode);
1993         LASSERT(osd_inode_unlinked(inode));
1994
1995         OSD_EXEC_OP(th, destroy);
1996
1997         result = osd_oi_delete(osd_oti_get(env), &osd->od_oi, fid, th);
1998
1999         /* XXX: add to ext3 orphan list */
2000         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2001
2002         /* not needed in the cache anymore */
2003         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2004
2005         RETURN(0);
2006 }
2007
2008 /**
2009  * Helper function for osd_xattr_set()
2010  */
2011 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2012                            const struct lu_buf *buf, const char *name, int fl)
2013 {
2014         struct osd_object      *obj      = osd_dt_obj(dt);
2015         struct inode           *inode    = obj->oo_inode;
2016         struct osd_thread_info *info     = osd_oti_get(env);
2017         struct dentry          *dentry   = &info->oti_child_dentry;
2018         int                     fs_flags = 0;
2019         int  rc;
2020
2021         LASSERT(dt_object_exists(dt));
2022         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2023         LASSERT(osd_write_locked(env, obj));
2024
2025         if (fl & LU_XATTR_REPLACE)
2026                 fs_flags |= XATTR_REPLACE;
2027
2028         if (fl & LU_XATTR_CREATE)
2029                 fs_flags |= XATTR_CREATE;
2030
2031         dentry->d_inode = inode;
2032         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2033                                    buf->lb_len, fs_flags);
2034         return rc;
2035 }
2036
2037 /**
2038  * Put the fid into lustre_mdt_attrs, and then place the structure
2039  * inode's ea. This fid should not be altered during the life time
2040  * of the inode.
2041  *
2042  * \retval +ve, on success
2043  * \retval -ve, on error
2044  *
2045  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2046  */
2047 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2048                           const struct lu_fid *fid)
2049 {
2050         struct osd_thread_info  *info      = osd_oti_get(env);
2051         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2052
2053         lustre_lma_init(mdt_attrs, fid);
2054         lustre_lma_swab(mdt_attrs);
2055         return __osd_xattr_set(env, dt,
2056                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2057                                XATTR_NAME_LMA, LU_XATTR_CREATE);
2058
2059 }
2060
2061 /**
2062  * Helper function to form igif
2063  */
2064 static inline void osd_igif_get(const struct lu_env *env, struct inode  *inode,
2065                                 struct lu_fid *fid)
2066 {
2067         LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
2068 }
2069
2070 /**
2071  * Helper function to pack the fid, ldiskfs stores fid in packed format.
2072  */
2073 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
2074                   struct lu_fid *befider)
2075 {
2076         fid_cpu_to_be(befider, (struct lu_fid *)fid);
2077         memcpy(pack->fp_area, befider, sizeof(*befider));
2078         pack->fp_len =  sizeof(*befider) + 1;
2079 }
2080
2081 /**
2082  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2083  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2084  * To have compatilibility with 1.8 ldiskfs driver we need to have
2085  * magic number at start of fid data.
2086  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2087  * its inmemory API.
2088  */
2089 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2090                                   const struct dt_rec *fid)
2091 {
2092         param->edp_magic = LDISKFS_LUFID_MAGIC;
2093         param->edp_len =  sizeof(struct lu_fid) + 1;
2094
2095         fid_cpu_to_be((struct lu_fid *)param->edp_data,
2096                       (struct lu_fid *)fid);
2097 }
2098
2099 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
2100 {
2101         int result;
2102
2103         result = 0;
2104         switch (pack->fp_len) {
2105         case sizeof *fid + 1:
2106                 memcpy(fid, pack->fp_area, sizeof *fid);
2107                 fid_be_to_cpu(fid, fid);
2108                 break;
2109         default:
2110                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
2111                 result = -EIO;
2112         }
2113         return result;
2114 }
2115
2116 /**
2117  * Try to read the fid from inode ea into dt_rec, if return value
2118  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2119  *
2120  * \param fid object fid.
2121  *
2122  * \retval 0 on success
2123  */
2124 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2125                           __u32 ino, struct lu_fid *fid)
2126 {
2127         struct osd_thread_info  *info      = osd_oti_get(env);
2128         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2129         struct lu_device        *ldev   = obj->oo_dt.do_lu.lo_dev;
2130         struct dentry           *dentry = &info->oti_child_dentry;
2131         struct osd_inode_id     *id     = &info->oti_id;
2132         struct osd_device       *dev;
2133         struct inode            *inode;
2134         int                      rc;
2135
2136         ENTRY;
2137         dev  = osd_dev(ldev);
2138
2139         id->oii_ino = ino;
2140         id->oii_gen = OSD_OII_NOGEN;
2141
2142         inode = osd_iget(info, dev, id);
2143         if (IS_ERR(inode)) {
2144                 rc = PTR_ERR(inode);
2145                 GOTO(out,rc);
2146         }
2147         dentry->d_inode = inode;
2148
2149         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2150         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2151                                    sizeof *mdt_attrs);
2152
2153         /* Check LMA compatibility */
2154         if (rc > 0 &&
2155             (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2156                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2157                       inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2158                       ~LMA_INCOMPAT_SUPP);
2159                 return -ENOSYS;
2160         }
2161
2162         if (rc > 0) {
2163                 lustre_lma_swab(mdt_attrs);
2164                 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2165                 rc = 0;
2166         } else if (rc == -ENODATA) {
2167                 osd_igif_get(env, inode, fid);
2168                 rc = 0;
2169         }
2170         iput(inode);
2171 out:
2172         RETURN(rc);
2173 }
2174
2175 /**
2176  * OSD layer object create function for interoperability mode (b11826).
2177  * This is mostly similar to osd_object_create(). Only difference being, fid is
2178  * inserted into inode ea here.
2179  *
2180  * \retval   0, on success
2181  * \retval -ve, on error
2182  */
2183 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2184                              struct lu_attr *attr,
2185                              struct dt_allocation_hint *hint,
2186                              struct dt_object_format *dof,
2187                              struct thandle *th)
2188 {
2189         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2190         struct osd_object      *obj    = osd_dt_obj(dt);
2191         struct osd_thread_info *info   = osd_oti_get(env);
2192         int result;
2193
2194         ENTRY;
2195
2196         LASSERT(osd_invariant(obj));
2197         LASSERT(!dt_object_exists(dt));
2198         LASSERT(osd_write_locked(env, obj));
2199         LASSERT(th != NULL);
2200
2201         OSD_EXEC_OP(th, create);
2202
2203         result = __osd_object_create(info, obj, attr, hint, dof, th);
2204
2205         /* objects under osd root shld have igif fid, so dont add fid EA */
2206         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2207                 result = osd_ea_fid_set(env, dt, fid);
2208
2209         if (result == 0)
2210                 result = __osd_oi_insert(env, obj, fid, th);
2211
2212         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2213         LINVRNT(osd_invariant(obj));
2214         RETURN(result);
2215 }
2216
2217 static int osd_declare_object_ref_add(const struct lu_env *env,
2218                                struct dt_object *dt,
2219                                struct thandle *handle)
2220 {
2221         struct osd_thandle *oh;
2222
2223         /* it's possible that object doesn't exist yet */
2224         LASSERT(handle != NULL);
2225
2226         oh = container_of0(handle, struct osd_thandle, ot_super);
2227         LASSERT(oh->ot_handle == NULL);
2228
2229         OSD_DECLARE_OP(oh, ref_add);
2230         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2231
2232         return 0;
2233 }
2234
2235 /*
2236  * Concurrency: @dt is write locked.
2237  */
2238 static int osd_object_ref_add(const struct lu_env *env,
2239                               struct dt_object *dt,
2240                               struct thandle *th)
2241 {
2242         struct osd_object *obj = osd_dt_obj(dt);
2243         struct inode *inode = obj->oo_inode;
2244
2245         LINVRNT(osd_invariant(obj));
2246         LASSERT(dt_object_exists(dt));
2247         LASSERT(osd_write_locked(env, obj));
2248         LASSERT(th != NULL);
2249
2250         OSD_EXEC_OP(th, ref_add);
2251
2252         cfs_spin_lock(&obj->oo_guard);
2253         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2254         inode->i_nlink++;
2255         cfs_spin_unlock(&obj->oo_guard);
2256         inode->i_sb->s_op->dirty_inode(inode);
2257         LINVRNT(osd_invariant(obj));
2258
2259         return 0;
2260 }
2261
2262 static int osd_declare_object_ref_del(const struct lu_env *env,
2263                                struct dt_object *dt,
2264                                struct thandle *handle)
2265 {
2266         struct osd_thandle *oh;
2267
2268         LASSERT(dt_object_exists(dt));
2269         LASSERT(handle != NULL);
2270
2271         oh = container_of0(handle, struct osd_thandle, ot_super);
2272         LASSERT(oh->ot_handle == NULL);
2273
2274         OSD_DECLARE_OP(oh, ref_del);
2275         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2276
2277         return 0;
2278 }
2279
2280 /*
2281  * Concurrency: @dt is write locked.
2282  */
2283 static int osd_object_ref_del(const struct lu_env *env,
2284                               struct dt_object *dt,
2285                               struct thandle *th)
2286 {
2287         struct osd_object *obj = osd_dt_obj(dt);
2288         struct inode *inode = obj->oo_inode;
2289
2290         LINVRNT(osd_invariant(obj));
2291         LASSERT(dt_object_exists(dt));
2292         LASSERT(osd_write_locked(env, obj));
2293         LASSERT(th != NULL);
2294
2295         OSD_EXEC_OP(th, ref_del);
2296
2297         cfs_spin_lock(&obj->oo_guard);
2298         LASSERT(inode->i_nlink > 0);
2299         inode->i_nlink--;
2300         cfs_spin_unlock(&obj->oo_guard);
2301         inode->i_sb->s_op->dirty_inode(inode);
2302         LINVRNT(osd_invariant(obj));
2303
2304         return 0;
2305 }
2306
2307 /*
2308  * Get the 64-bit version for an inode.
2309  */
2310 static int osd_object_version_get(const struct lu_env *env,
2311                                   struct dt_object *dt, dt_obj_version_t *ver)
2312 {
2313         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2314
2315         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2316                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2317         *ver = LDISKFS_I(inode)->i_fs_version;
2318         return 0;
2319 }
2320
2321 /*
2322  * Concurrency: @dt is read locked.
2323  */
2324 static int osd_xattr_get(const struct lu_env *env,
2325                          struct dt_object *dt,
2326                          struct lu_buf *buf,
2327                          const char *name,
2328                          struct lustre_capa *capa)
2329 {
2330         struct osd_object      *obj    = osd_dt_obj(dt);
2331         struct inode           *inode  = obj->oo_inode;
2332         struct osd_thread_info *info   = osd_oti_get(env);
2333         struct dentry          *dentry = &info->oti_obj_dentry;
2334
2335         /* version get is not real XATTR but uses xattr API */
2336         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2337                 /* for version we are just using xattr API but change inode
2338                  * field instead */
2339                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2340                 osd_object_version_get(env, dt, buf->lb_buf);
2341                 return sizeof(dt_obj_version_t);
2342         }
2343
2344         LASSERT(dt_object_exists(dt));
2345         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2346         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2347
2348         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2349                 return -EACCES;
2350
2351         dentry->d_inode = inode;
2352         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2353 }
2354
2355
2356 static int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
2357                                  const struct lu_buf *buf, const char *name,
2358                                  int fl, struct thandle *handle)
2359 {
2360         struct osd_thandle *oh;
2361
2362         LASSERT(handle != NULL);
2363
2364         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2365                 /* no credits for version */
2366                 return 0;
2367         }
2368
2369         oh = container_of0(handle, struct osd_thandle, ot_super);
2370         LASSERT(oh->ot_handle == NULL);
2371
2372         OSD_DECLARE_OP(oh, xattr_set);
2373         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2374
2375         return 0;
2376 }
2377
2378 /*
2379  * Set the 64-bit version for object
2380  */
2381 static void osd_object_version_set(const struct lu_env *env,
2382                                    struct dt_object *dt,
2383                                    dt_obj_version_t *new_version)
2384 {
2385         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2386
2387         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2388                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2389
2390         LDISKFS_I(inode)->i_fs_version = *new_version;
2391         /** Version is set after all inode operations are finished,
2392          *  so we should mark it dirty here */
2393         inode->i_sb->s_op->dirty_inode(inode);
2394 }
2395
2396 /*
2397  * Concurrency: @dt is write locked.
2398  */
2399 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2400                          const struct lu_buf *buf, const char *name, int fl,
2401                          struct thandle *handle, struct lustre_capa *capa)
2402 {
2403         LASSERT(handle != NULL);
2404
2405         /* version set is not real XATTR */
2406         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2407                 /* for version we are just using xattr API but change inode
2408                  * field instead */
2409                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2410                 osd_object_version_set(env, dt, buf->lb_buf);
2411                 return sizeof(dt_obj_version_t);
2412         }
2413
2414         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2415                 return -EACCES;
2416
2417         OSD_EXEC_OP(handle, xattr_set);
2418         return __osd_xattr_set(env, dt, buf, name, fl);
2419 }
2420
2421 /*
2422  * Concurrency: @dt is read locked.
2423  */
2424 static int osd_xattr_list(const struct lu_env *env,
2425                           struct dt_object *dt,
2426                           struct lu_buf *buf,
2427                           struct lustre_capa *capa)
2428 {
2429         struct osd_object      *obj    = osd_dt_obj(dt);
2430         struct inode           *inode  = obj->oo_inode;
2431         struct osd_thread_info *info   = osd_oti_get(env);
2432         struct dentry          *dentry = &info->oti_obj_dentry;
2433
2434         LASSERT(dt_object_exists(dt));
2435         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2436         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2437
2438         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2439                 return -EACCES;
2440
2441         dentry->d_inode = inode;
2442         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2443 }
2444
2445 static int osd_declare_xattr_del(const struct lu_env *env,
2446                                  struct dt_object *dt,
2447                                 const char *name,
2448                                 struct thandle *handle)
2449 {
2450         struct osd_thandle *oh;
2451
2452         LASSERT(dt_object_exists(dt));
2453         LASSERT(handle != NULL);
2454
2455         oh = container_of0(handle, struct osd_thandle, ot_super);
2456         LASSERT(oh->ot_handle == NULL);
2457
2458         OSD_DECLARE_OP(oh, xattr_set);
2459         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2460
2461         return 0;
2462 }
2463
2464 /*
2465  * Concurrency: @dt is write locked.
2466  */
2467 static int osd_xattr_del(const struct lu_env *env,
2468                          struct dt_object *dt,
2469                          const char *name,
2470                          struct thandle *handle,
2471                          struct lustre_capa *capa)
2472 {
2473         struct osd_object      *obj    = osd_dt_obj(dt);
2474         struct inode           *inode  = obj->oo_inode;
2475         struct osd_thread_info *info   = osd_oti_get(env);
2476         struct dentry          *dentry = &info->oti_obj_dentry;
2477         int                     rc;
2478
2479         LASSERT(dt_object_exists(dt));
2480         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2481         LASSERT(osd_write_locked(env, obj));
2482         LASSERT(handle != NULL);
2483
2484         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2485                 return -EACCES;
2486
2487         OSD_EXEC_OP(handle, xattr_set);
2488
2489         dentry->d_inode = inode;
2490         rc = inode->i_op->removexattr(dentry, name);
2491         return rc;
2492 }
2493
2494 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2495                                      struct dt_object *dt,
2496                                      struct lustre_capa *old,
2497                                      __u64 opc)
2498 {
2499         struct osd_thread_info *info = osd_oti_get(env);
2500         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2501         struct osd_object *obj = osd_dt_obj(dt);
2502         struct osd_device *dev = osd_obj2dev(obj);
2503         struct lustre_capa_key *key = &info->oti_capa_key;
2504         struct lustre_capa *capa = &info->oti_capa;
2505         struct obd_capa *oc;
2506         struct md_capainfo *ci;
2507         int rc;
2508         ENTRY;
2509
2510         if (!dev->od_fl_capa)
2511                 RETURN(ERR_PTR(-ENOENT));
2512
2513         LASSERT(dt_object_exists(dt));
2514         LINVRNT(osd_invariant(obj));
2515
2516         /* renewal sanity check */
2517         if (old && osd_object_auth(env, dt, old, opc))
2518                 RETURN(ERR_PTR(-EACCES));
2519
2520         ci = md_capainfo(env);
2521         if (unlikely(!ci))
2522                 RETURN(ERR_PTR(-ENOENT));
2523
2524         switch (ci->mc_auth) {
2525         case LC_ID_NONE:
2526                 RETURN(NULL);
2527         case LC_ID_PLAIN:
2528                 capa->lc_uid = obj->oo_inode->i_uid;
2529                 capa->lc_gid = obj->oo_inode->i_gid;
2530                 capa->lc_flags = LC_ID_PLAIN;
2531                 break;
2532         case LC_ID_CONVERT: {
2533                 __u32 d[4], s[4];
2534
2535                 s[0] = obj->oo_inode->i_uid;
2536                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2537                 s[2] = obj->oo_inode->i_gid;
2538                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2539                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2540                 if (unlikely(rc))
2541                         RETURN(ERR_PTR(rc));
2542
2543                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2544                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2545                 capa->lc_flags = LC_ID_CONVERT;
2546                 break;
2547         }
2548         default:
2549                 RETURN(ERR_PTR(-EINVAL));
2550         }
2551
2552         capa->lc_fid = *fid;
2553         capa->lc_opc = opc;
2554         capa->lc_flags |= dev->od_capa_alg << 24;
2555         capa->lc_timeout = dev->od_capa_timeout;
2556         capa->lc_expiry = 0;
2557
2558         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2559         if (oc) {
2560                 LASSERT(!capa_is_expired(oc));
2561                 RETURN(oc);
2562         }
2563
2564         cfs_spin_lock(&capa_lock);
2565         *key = dev->od_capa_keys[1];
2566         cfs_spin_unlock(&capa_lock);
2567
2568         capa->lc_keyid = key->lk_keyid;
2569         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2570
2571         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2572         if (rc) {
2573                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2574                 RETURN(ERR_PTR(rc));
2575         }
2576
2577         oc = capa_add(dev->od_capa_hash, capa);
2578         RETURN(oc);
2579 }
2580
2581 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2582 {
2583         int rc;
2584         struct osd_object      *obj    = osd_dt_obj(dt);
2585         struct inode           *inode  = obj->oo_inode;
2586         struct osd_thread_info *info   = osd_oti_get(env);
2587         struct dentry          *dentry = &info->oti_obj_dentry;
2588         struct file            *file   = &info->oti_file;
2589         ENTRY;
2590
2591         dentry->d_inode = inode;
2592         file->f_dentry = dentry;
2593         file->f_mapping = inode->i_mapping;
2594         file->f_op = inode->i_fop;
2595         LOCK_INODE_MUTEX(inode);
2596         rc = file->f_op->fsync(file, dentry, 0);
2597         UNLOCK_INODE_MUTEX(inode);
2598         RETURN(rc);
2599 }
2600
2601 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2602                         void **data)
2603 {
2604         struct osd_object *obj = osd_dt_obj(dt);
2605         ENTRY;
2606
2607         *data = (void *)obj->oo_inode;
2608         RETURN(0);
2609 }
2610
2611 /*
2612  * Index operations.
2613  */
2614
2615 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2616                            const struct dt_index_features *feat)
2617 {
2618         struct iam_descr *descr;
2619
2620         if (osd_object_is_root(o))
2621                 return feat == &dt_directory_features;
2622
2623         LASSERT(o->oo_dir != NULL);
2624
2625         descr = o->oo_dir->od_container.ic_descr;
2626         if (feat == &dt_directory_features) {
2627                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2628                         return 1;
2629                 else
2630                         return 0;
2631         } else {
2632                 return
2633                         feat->dif_keysize_min <= descr->id_key_size &&
2634                         descr->id_key_size <= feat->dif_keysize_max &&
2635                         feat->dif_recsize_min <= descr->id_rec_size &&
2636                         descr->id_rec_size <= feat->dif_recsize_max &&
2637                         !(feat->dif_flags & (DT_IND_VARKEY |
2638                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2639                         ergo(feat->dif_flags & DT_IND_UPDATE,
2640                              1 /* XXX check that object (and file system) is
2641                                 * writable */);
2642         }
2643 }
2644
2645 static int osd_iam_container_init(const struct lu_env *env,
2646                                   struct osd_object *obj,
2647                                   struct osd_directory *dir)
2648 {
2649         struct iam_container *bag = &dir->od_container;
2650         int result;
2651
2652         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2653         if (result != 0)
2654                 return result;
2655
2656         result = iam_container_setup(bag);
2657         if (result != 0)
2658                 goto out;
2659
2660         if (osd_obj2dev(obj)->od_iop_mode) {
2661                 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2662
2663                 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2664                                                 ptr, 0, &result);
2665         }
2666
2667  out:
2668         if (result == 0)
2669                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2670         else
2671                 iam_container_fini(bag);
2672
2673         return result;
2674 }
2675
2676
2677 /*
2678  * Concurrency: no external locking is necessary.
2679  */
2680 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2681                          const struct dt_index_features *feat)
2682 {
2683         int result;
2684         int ea_dir = 0;
2685         struct osd_object *obj = osd_dt_obj(dt);
2686         struct osd_device *osd = osd_obj2dev(obj);
2687
2688         LINVRNT(osd_invariant(obj));
2689         LASSERT(dt_object_exists(dt));
2690
2691         if (osd_object_is_root(obj)) {
2692                 dt->do_index_ops = &osd_index_ea_ops;
2693                 result = 0;
2694         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2695                 dt->do_index_ops = &osd_index_ea_ops;
2696                 if (S_ISDIR(obj->oo_inode->i_mode))
2697                         result = 0;
2698                 else
2699                         result = -ENOTDIR;
2700                 ea_dir = 1;
2701         } else if (!osd_has_index(obj)) {
2702                 struct osd_directory *dir;
2703
2704                 OBD_ALLOC_PTR(dir);
2705                 if (dir != NULL) {
2706
2707                         cfs_spin_lock(&obj->oo_guard);
2708                         if (obj->oo_dir == NULL)
2709                                 obj->oo_dir = dir;
2710                         else
2711                                 /*
2712                                  * Concurrent thread allocated container data.
2713                                  */
2714                                 OBD_FREE_PTR(dir);
2715                         cfs_spin_unlock(&obj->oo_guard);
2716                         /*
2717                          * Now, that we have container data, serialize its
2718                          * initialization.
2719                          */
2720                         cfs_down_write(&obj->oo_ext_idx_sem);
2721                         /*
2722                          * recheck under lock.
2723                          */
2724                         if (!osd_has_index(obj))
2725                                 result = osd_iam_container_init(env, obj, dir);
2726                         else
2727                                 result = 0;
2728                         cfs_up_write(&obj->oo_ext_idx_sem);
2729                 } else {
2730                         result = -ENOMEM;
2731                 }
2732         } else {
2733                 result = 0;
2734         }
2735
2736         if (result == 0 && ea_dir == 0) {
2737                 if (!osd_iam_index_probe(env, obj, feat))
2738                         result = -ENOTDIR;
2739         }
2740         LINVRNT(osd_invariant(obj));
2741
2742         return result;
2743 }
2744
2745 static const struct dt_object_operations osd_obj_ops = {
2746         .do_read_lock         = osd_object_read_lock,
2747         .do_write_lock        = osd_object_write_lock,
2748         .do_read_unlock       = osd_object_read_unlock,
2749         .do_write_unlock      = osd_object_write_unlock,
2750         .do_write_locked      = osd_object_write_locked,
2751         .do_attr_get          = osd_attr_get,
2752         .do_declare_attr_set  = osd_declare_attr_set,
2753         .do_attr_set          = osd_attr_set,
2754         .do_ah_init           = osd_ah_init,
2755         .do_declare_create    = osd_declare_object_create,
2756         .do_create            = osd_object_create,
2757         .do_declare_destroy   = osd_declare_object_destroy,
2758         .do_destroy           = osd_object_destroy,
2759         .do_index_try         = osd_index_try,
2760         .do_declare_ref_add   = osd_declare_object_ref_add,
2761         .do_ref_add           = osd_object_ref_add,
2762         .do_declare_ref_del   = osd_declare_object_ref_del,
2763         .do_ref_del           = osd_object_ref_del,
2764         .do_xattr_get         = osd_xattr_get,
2765         .do_declare_xattr_set = osd_declare_xattr_set,
2766         .do_xattr_set         = osd_xattr_set,
2767         .do_declare_xattr_del = osd_declare_xattr_del,
2768         .do_xattr_del         = osd_xattr_del,
2769         .do_xattr_list        = osd_xattr_list,
2770         .do_capa_get          = osd_capa_get,
2771         .do_object_sync       = osd_object_sync,
2772         .do_data_get          = osd_data_get,
2773 };
2774
2775 /**
2776  * dt_object_operations for interoperability mode
2777  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2778  */
2779 static const struct dt_object_operations osd_obj_ea_ops = {
2780         .do_read_lock         = osd_object_read_lock,
2781         .do_write_lock        = osd_object_write_lock,
2782         .do_read_unlock       = osd_object_read_unlock,
2783         .do_write_unlock      = osd_object_write_unlock,
2784         .do_write_locked      = osd_object_write_locked,
2785         .do_attr_get          = osd_attr_get,
2786         .do_declare_attr_set  = osd_declare_attr_set,
2787         .do_attr_set          = osd_attr_set,
2788         .do_ah_init           = osd_ah_init,
2789         .do_declare_create    = osd_declare_object_create,
2790         .do_create            = osd_object_ea_create,
2791         .do_declare_destroy   = osd_declare_object_destroy,
2792         .do_destroy           = osd_object_destroy,
2793         .do_index_try         = osd_index_try,
2794         .do_declare_ref_add   = osd_declare_object_ref_add,
2795         .do_ref_add           = osd_object_ref_add,
2796         .do_declare_ref_del   = osd_declare_object_ref_del,
2797         .do_ref_del           = osd_object_ref_del,
2798         .do_xattr_get         = osd_xattr_get,
2799         .do_declare_xattr_set = osd_declare_xattr_set,
2800         .do_xattr_set         = osd_xattr_set,
2801         .do_declare_xattr_del = osd_declare_xattr_del,
2802         .do_xattr_del         = osd_xattr_del,
2803         .do_xattr_list        = osd_xattr_list,
2804         .do_capa_get          = osd_capa_get,
2805         .do_object_sync       = osd_object_sync,
2806         .do_data_get          = osd_data_get,
2807 };
2808
2809 /*
2810  * Body operations.
2811  */
2812
2813 /*
2814  * XXX: Another layering violation for now.
2815  *
2816  * We don't want to use ->f_op->read methods, because generic file write
2817  *
2818  *         - serializes on ->i_sem, and
2819  *
2820  *         - does a lot of extra work like balance_dirty_pages(),
2821  *
2822  * which doesn't work for globally shared files like /last-received.
2823  */
2824 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2825 {
2826         struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2827
2828         memcpy(buffer, (char*)ei->i_data, buflen);
2829
2830         return  buflen;
2831 }
2832
2833 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2834                             loff_t *offs)
2835 {
2836         struct buffer_head *bh;
2837         unsigned long block;
2838         int osize = size;
2839         int blocksize;
2840         int csize;
2841         int boffs;
2842         int err;
2843
2844         /* prevent reading after eof */
2845         spin_lock(&inode->i_lock);
2846         if (i_size_read(inode) < *offs + size) {
2847                 size = i_size_read(inode) - *offs;
2848                 spin_unlock(&inode->i_lock);
2849                 if (size < 0) {
2850                         CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2851                                i_size_read(inode), *offs);
2852                         return -EBADR;
2853                 } else if (size == 0) {
2854                         return 0;
2855                 }
2856         } else {
2857                 spin_unlock(&inode->i_lock);
2858         }
2859
2860         blocksize = 1 << inode->i_blkbits;
2861
2862         while (size > 0) {
2863                 block = *offs >> inode->i_blkbits;
2864                 boffs = *offs & (blocksize - 1);
2865                 csize = min(blocksize - boffs, size);
2866                 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2867                 if (!bh) {
2868                         CERROR("can't read block: %d\n", err);
2869                         return err;
2870                 }
2871
2872                 memcpy(buf, bh->b_data + boffs, csize);
2873                 brelse(bh);
2874
2875                 *offs += csize;
2876                 buf += csize;
2877                 size -= csize;
2878         }
2879         return osize;
2880 }
2881
2882 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2883                         struct lu_buf *buf, loff_t *pos,
2884                         struct lustre_capa *capa)
2885 {
2886         struct osd_object      *obj    = osd_dt_obj(dt);
2887         struct inode           *inode  = obj->oo_inode;
2888         int rc;
2889
2890         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2891                 RETURN(-EACCES);
2892
2893         /* Read small symlink from inode body as we need to maintain correct
2894          * on-disk symlinks for ldiskfs.
2895          */
2896         if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2897             (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2898                 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2899         else
2900                 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2901
2902         return rc;
2903 }
2904
2905 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2906 {
2907
2908         memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2909                buflen);
2910         LDISKFS_I(inode)->i_disksize = buflen;
2911         i_size_write(inode, buflen);
2912         inode->i_sb->s_op->dirty_inode(inode);
2913
2914         return 0;
2915 }
2916
2917 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2918                                     loff_t *offs, handle_t *handle)
2919 {
2920         struct buffer_head *bh = NULL;
2921         loff_t offset = *offs;
2922         loff_t new_size = i_size_read(inode);
2923         unsigned long block;
2924         int blocksize = 1 << inode->i_blkbits;
2925         int err = 0;
2926         int size;
2927         int boffs;
2928         int dirty_inode = 0;
2929
2930         while (bufsize > 0) {
2931                 if (bh != NULL)
2932                         brelse(bh);
2933
2934                 block = offset >> inode->i_blkbits;
2935                 boffs = offset & (blocksize - 1);
2936                 size = min(blocksize - boffs, bufsize);
2937                 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2938                 if (!bh) {
2939                         CERROR("can't read/create block: %d\n", err);
2940                         break;
2941                 }
2942
2943                 err = ldiskfs_journal_get_write_access(handle, bh);
2944                 if (err) {
2945                         CERROR("journal_get_write_access() returned error %d\n",
2946                                err);
2947                         break;
2948                 }
2949                 LASSERTF(boffs + size <= bh->b_size,
2950                          "boffs %d size %d bh->b_size %lu",
2951                          boffs, size, (unsigned long)bh->b_size);
2952                 memcpy(bh->b_data + boffs, buf, size);
2953                 err = ldiskfs_journal_dirty_metadata(handle, bh);
2954                 if (err)
2955                         break;
2956
2957                 if (offset + size > new_size)
2958                         new_size = offset + size;
2959                 offset += size;
2960                 bufsize -= size;
2961                 buf += size;
2962         }
2963         if (bh)
2964                 brelse(bh);
2965
2966         /* correct in-core and on-disk sizes */
2967         if (new_size > i_size_read(inode)) {
2968                 spin_lock(&inode->i_lock);
2969                 if (new_size > i_size_read(inode))
2970                         i_size_write(inode, new_size);
2971                 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2972                         LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2973                         dirty_inode = 1;
2974                 }
2975                 spin_unlock(&inode->i_lock);
2976                 if (dirty_inode)
2977                         inode->i_sb->s_op->dirty_inode(inode);
2978         }
2979
2980         if (err == 0)
2981                 *offs = offset;
2982         return err;
2983 }
2984
2985 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
2986                                  const loff_t size, loff_t pos,
2987                                  struct thandle *handle)
2988 {
2989         struct osd_thandle *oh;
2990
2991         LASSERT(handle != NULL);
2992
2993         oh = container_of0(handle, struct osd_thandle, ot_super);
2994         LASSERT(oh->ot_handle == NULL);
2995
2996         OSD_DECLARE_OP(oh, write);
2997         oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BLOCK];
2998
2999         return 0;
3000 }
3001
3002 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
3003                          const struct lu_buf *buf, loff_t *pos,
3004                          struct thandle *handle, struct lustre_capa *capa,
3005                          int ignore_quota)
3006 {
3007         struct osd_object  *obj   = osd_dt_obj(dt);
3008         struct inode       *inode = obj->oo_inode;
3009         struct osd_thandle *oh;
3010         ssize_t            result = 0;
3011 #ifdef HAVE_QUOTA_SUPPORT
3012         cfs_cap_t           save = cfs_curproc_cap_pack();
3013 #endif
3014
3015         LASSERT(handle != NULL);
3016
3017         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
3018                 RETURN(-EACCES);
3019
3020         oh = container_of(handle, struct osd_thandle, ot_super);
3021         LASSERT(oh->ot_handle->h_transaction != NULL);
3022 #ifdef HAVE_QUOTA_SUPPORT
3023         if (ignore_quota)
3024                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3025         else
3026                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3027 #endif
3028         /* Write small symlink to inode body as we need to maintain correct
3029          * on-disk symlinks for ldiskfs.
3030          */
3031         if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
3032            (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
3033                 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
3034         else
3035                 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
3036                                                   buf->lb_len, pos,
3037                                                   oh->ot_handle);
3038 #ifdef HAVE_QUOTA_SUPPORT
3039         cfs_curproc_cap_unpack(save);
3040 #endif
3041         if (result == 0)
3042                 result = buf->lb_len;
3043         return result;
3044 }
3045
3046 /*
3047  * in some cases we may need declare methods for objects being created
3048  * e.g., when we create symlink
3049  */
3050 static const struct dt_body_operations osd_body_ops_new = {
3051         .dbo_declare_write = osd_declare_write,
3052 };
3053
3054 static const struct dt_body_operations osd_body_ops = {
3055         .dbo_read          = osd_read,
3056         .dbo_declare_write = osd_declare_write,
3057         .dbo_write         = osd_write
3058 };
3059
3060 static int osd_index_declare_iam_delete(const struct lu_env *env,
3061                                         struct dt_object *dt,
3062                                         const struct dt_key *key,
3063                                         struct thandle *handle)
3064 {
3065         struct osd_thandle    *oh;
3066
3067         oh = container_of0(handle, struct osd_thandle, ot_super);
3068         LASSERT(oh->ot_handle == NULL);
3069
3070         OSD_DECLARE_OP(oh, delete);
3071         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3072
3073         return 0;
3074 }
3075
3076 /**
3077  *      delete a (key, value) pair from index \a dt specified by \a key
3078  *
3079  *      \param  dt      osd index object
3080  *      \param  key     key for index
3081  *      \param  rec     record reference
3082  *      \param  handle  transaction handler
3083  *
3084  *      \retval  0  success
3085  *      \retval -ve   failure
3086  */
3087
3088 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3089                                 const struct dt_key *key, struct thandle *handle,
3090                                 struct lustre_capa *capa)
3091 {
3092         struct osd_object     *obj = osd_dt_obj(dt);
3093         struct osd_thandle    *oh;
3094         struct iam_path_descr *ipd;
3095         struct iam_container  *bag = &obj->oo_dir->od_container;
3096         int rc;
3097
3098         ENTRY;
3099
3100         LINVRNT(osd_invariant(obj));
3101         LASSERT(dt_object_exists(dt));
3102         LASSERT(bag->ic_object == obj->oo_inode);
3103         LASSERT(handle != NULL);
3104
3105         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3106                 RETURN(-EACCES);
3107
3108         OSD_EXEC_OP(handle, delete);
3109
3110         ipd = osd_idx_ipd_get(env, bag);
3111         if (unlikely(ipd == NULL))
3112                 RETURN(-ENOMEM);
3113
3114         oh = container_of0(handle, struct osd_thandle, ot_super);
3115         LASSERT(oh->ot_handle != NULL);
3116         LASSERT(oh->ot_handle->h_transaction != NULL);
3117
3118         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3119         osd_ipd_put(env, bag, ipd);
3120         LINVRNT(osd_invariant(obj));
3121         RETURN(rc);
3122 }
3123
3124 static int osd_index_declare_ea_delete(const struct lu_env *env,
3125                                        struct dt_object *dt,
3126                                        const struct dt_key *key,
3127                                        struct thandle *handle)
3128 {
3129         struct osd_thandle *oh;
3130
3131         LASSERT(dt_object_exists(dt));
3132         LASSERT(handle != NULL);
3133
3134         oh = container_of0(handle, struct osd_thandle, ot_super);
3135         LASSERT(oh->ot_handle == NULL);
3136
3137         OSD_DECLARE_OP(oh, delete);
3138         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3139
3140         return 0;
3141 }
3142
3143 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3144                                           struct dt_rec *fid)
3145 {
3146         struct osd_fid_pack *rec;
3147         int rc = -ENODATA;
3148
3149         if (de->file_type & LDISKFS_DIRENT_LUFID) {
3150                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3151                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3152         }
3153         RETURN(rc);
3154 }
3155
3156 /**
3157  * Index delete function for interoperability mode (b11826).
3158  * It will remove the directory entry added by osd_index_ea_insert().
3159  * This entry is needed to maintain name->fid mapping.
3160  *
3161  * \param key,  key i.e. file entry to be deleted
3162  *
3163  * \retval   0, on success
3164  * \retval -ve, on error
3165  */
3166 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3167                                const struct dt_key *key, struct thandle *handle,
3168                                struct lustre_capa *capa)
3169 {
3170         struct osd_object          *obj    = osd_dt_obj(dt);
3171         struct inode               *dir    = obj->oo_inode;
3172         struct dentry              *dentry;
3173         struct osd_thandle         *oh;
3174         struct ldiskfs_dir_entry_2 *de;
3175         struct buffer_head         *bh;
3176         struct htree_lock          *hlock = NULL;
3177
3178         int rc;
3179
3180         ENTRY;
3181
3182         LINVRNT(osd_invariant(obj));
3183         LASSERT(dt_object_exists(dt));
3184         LASSERT(handle != NULL);
3185
3186         OSD_EXEC_OP(handle, delete);
3187
3188         oh = container_of(handle, struct osd_thandle, ot_super);
3189         LASSERT(oh->ot_handle != NULL);
3190         LASSERT(oh->ot_handle->h_transaction != NULL);
3191
3192         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3193                 RETURN(-EACCES);
3194
3195         dentry = osd_child_dentry_get(env, obj,
3196                                       (char *)key, strlen((char *)key));
3197
3198         if (obj->oo_hl_head != NULL) {
3199                 hlock = osd_oti_get(env)->oti_hlock;
3200                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3201                                    dir, LDISKFS_HLOCK_DEL);
3202         } else {
3203                 cfs_down_write(&obj->oo_ext_idx_sem);
3204         }
3205
3206         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3207         if (bh) {
3208                 rc = ldiskfs_delete_entry(oh->ot_handle,
3209                                           dir, de, bh);
3210                 brelse(bh);
3211         } else {
3212                 rc = -ENOENT;
3213         }
3214         if (hlock != NULL)
3215                 ldiskfs_htree_unlock(hlock);
3216         else
3217                 cfs_up_write(&obj->oo_ext_idx_sem);
3218
3219         LASSERT(osd_invariant(obj));
3220         RETURN(rc);
3221 }
3222
3223 /**
3224  *      Lookup index for \a key and copy record to \a rec.
3225  *
3226  *      \param  dt      osd index object
3227  *      \param  key     key for index
3228  *      \param  rec     record reference
3229  *
3230  *      \retval  +ve  success : exact mach
3231  *      \retval  0    return record with key not greater than \a key
3232  *      \retval -ve   failure
3233  */
3234 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3235                                 struct dt_rec *rec, const struct dt_key *key,
3236                                 struct lustre_capa *capa)
3237 {
3238         struct osd_object     *obj = osd_dt_obj(dt);
3239         struct iam_path_descr *ipd;
3240         struct iam_container  *bag = &obj->oo_dir->od_container;
3241         struct osd_thread_info *oti = osd_oti_get(env);
3242         struct iam_iterator    *it = &oti->oti_idx_it;
3243         struct iam_rec *iam_rec;
3244         int rc;
3245         ENTRY;
3246
3247         LASSERT(osd_invariant(obj));
3248         LASSERT(dt_object_exists(dt));
3249         LASSERT(bag->ic_object == obj->oo_inode);
3250
3251         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3252                 RETURN(-EACCES);
3253
3254         ipd = osd_idx_ipd_get(env, bag);
3255         if (IS_ERR(ipd))
3256                 RETURN(-ENOMEM);
3257
3258         /* got ipd now we can start iterator. */
3259         iam_it_init(it, bag, 0, ipd);
3260
3261         rc = iam_it_get(it, (struct iam_key *)key);
3262         if (rc >= 0) {
3263                 if (S_ISDIR(obj->oo_inode->i_mode))
3264                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3265                 else
3266                         iam_rec = (struct iam_rec *) rec;
3267
3268                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3269                 if (S_ISDIR(obj->oo_inode->i_mode))
3270                         osd_fid_unpack((struct lu_fid *) rec,
3271                                        (struct osd_fid_pack *)iam_rec);
3272         }
3273         iam_it_put(it);
3274         iam_it_fini(it);
3275         osd_ipd_put(env, bag, ipd);
3276
3277         LINVRNT(osd_invariant(obj));
3278
3279         RETURN(rc);
3280 }
3281
3282 static int osd_index_declare_iam_insert(const struct lu_env *env,
3283                                         struct dt_object *dt,
3284                                         const struct dt_rec *rec,
3285                                         const struct dt_key *key,
3286                                         struct thandle *handle)
3287 {
3288         struct osd_thandle *oh;
3289
3290         LASSERT(dt_object_exists(dt));
3291         LASSERT(handle != NULL);
3292
3293         oh = container_of0(handle, struct osd_thandle, ot_super);
3294         LASSERT(oh->ot_handle == NULL);
3295
3296         OSD_DECLARE_OP(oh, insert);
3297         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3298
3299         return 0;
3300 }
3301
3302 /**
3303  *      Inserts (key, value) pair in \a dt index object.
3304  *
3305  *      \param  dt      osd index object
3306  *      \param  key     key for index
3307  *      \param  rec     record reference
3308  *      \param  th      transaction handler
3309  *
3310  *      \retval  0  success
3311  *      \retval -ve failure