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