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