Whamcloud - gitweb
b=19902
[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 osd_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 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1736                   struct lu_fid *befider)
1737 {
1738         fid_cpu_to_be(befider, (struct lu_fid *)fid);
1739         memcpy(pack->fp_area, befider, sizeof(*befider));
1740         pack->fp_len =  sizeof(*befider) + 1;
1741 }
1742
1743 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1744 {
1745         int result;
1746
1747         result = 0;
1748         switch (pack->fp_len) {
1749         case sizeof *fid + 1:
1750                 memcpy(fid, pack->fp_area, sizeof *fid);
1751                 fid_be_to_cpu(fid, fid);
1752                 break;
1753         default:
1754                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1755                 result = -EIO;
1756         }
1757         return result;
1758 }
1759
1760 /**
1761  * Try to read the fid from inode ea into dt_rec, if return value
1762  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1763  *
1764  * \param fid, object fid.
1765  *
1766  * \retval 0, on success
1767  */
1768 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1769                           __u32 ino, struct lu_fid *fid)
1770 {
1771         struct osd_thread_info  *info      = osd_oti_get(env);
1772         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1773         struct lu_device        *ldev   = obj->oo_dt.do_lu.lo_dev;
1774         struct dentry           *dentry = &info->oti_child_dentry;
1775         struct osd_inode_id     *id     = &info->oti_id;
1776         struct osd_device       *dev;
1777         struct inode            *inode;
1778         int                      rc;
1779
1780         ENTRY;
1781         dev  = osd_dev(ldev);
1782
1783         id->oii_ino = ino;
1784         id->oii_gen = OSD_OII_NOGEN;
1785
1786         inode = osd_iget(info, dev, id);
1787         if (IS_ERR(inode)) {
1788                 rc = PTR_ERR(inode);
1789                 GOTO(out,rc);
1790         }
1791         dentry->d_inode = inode;
1792
1793         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1794         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1795                                    sizeof *mdt_attrs);
1796
1797         /* Check LMA compatibility */
1798         if (rc > 0 &&
1799             (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1800                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1801                       inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1802                       ~LMA_INCOMPAT_SUPP);
1803                 return -ENOSYS;
1804         }
1805
1806         if (rc > 0) {
1807                 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1808                 rc = 0;
1809         } else if (rc == -ENODATA) {
1810                 osd_igif_get(env, inode, fid);
1811                 rc = 0;
1812         }
1813         iput(inode);
1814
1815 out:
1816         RETURN(rc);
1817 }
1818
1819 /**
1820  * OSD layer object create function for interoperability mode (b11826).
1821  * This is mostly similar to osd_object_create(). Only difference being, fid is
1822  * inserted into inode ea here.
1823  *
1824  * \retval   0, on success
1825  * \retval -ve, on error
1826  */
1827 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1828                              struct lu_attr *attr,
1829                              struct dt_allocation_hint *hint,
1830                              struct dt_object_format *dof,
1831                              struct thandle *th)
1832 {
1833         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1834         struct osd_object      *obj    = osd_dt_obj(dt);
1835         struct osd_thread_info *info   = osd_oti_get(env);
1836         int result;
1837         int is_root = 0;
1838
1839         ENTRY;
1840
1841         LASSERT(osd_invariant(obj));
1842         LASSERT(!dt_object_exists(dt));
1843         LASSERT(osd_write_locked(env, obj));
1844         LASSERT(th != NULL);
1845
1846         result = __osd_object_create(info, obj, attr, hint, dof, th);
1847
1848         if (hint && hint->dah_parent)
1849                 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1850
1851         /* objects under osd root shld have igif fid, so dont add fid EA */
1852         if (result == 0 && is_root == 0)
1853                 result = osd_ea_fid_set(env, dt, fid);
1854
1855         if (result == 0)
1856                 result = __osd_oi_insert(env, obj, fid, th);
1857
1858         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1859         LINVRNT(osd_invariant(obj));
1860         RETURN(result);
1861 }
1862
1863 /*
1864  * Concurrency: @dt is write locked.
1865  */
1866 static void osd_object_ref_add(const struct lu_env *env,
1867                                struct dt_object *dt,
1868                                struct thandle *th)
1869 {
1870         struct osd_object *obj = osd_dt_obj(dt);
1871         struct inode *inode = obj->oo_inode;
1872
1873         LINVRNT(osd_invariant(obj));
1874         LASSERT(dt_object_exists(dt));
1875         LASSERT(osd_write_locked(env, obj));
1876         LASSERT(th != NULL);
1877
1878         spin_lock(&obj->oo_guard);
1879         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1880         inode->i_nlink++;
1881         spin_unlock(&obj->oo_guard);
1882         mark_inode_dirty(inode);
1883         LINVRNT(osd_invariant(obj));
1884 }
1885
1886 /*
1887  * Concurrency: @dt is write locked.
1888  */
1889 static void osd_object_ref_del(const struct lu_env *env,
1890                                struct dt_object *dt,
1891                                struct thandle *th)
1892 {
1893         struct osd_object *obj = osd_dt_obj(dt);
1894         struct inode *inode = obj->oo_inode;
1895
1896         LINVRNT(osd_invariant(obj));
1897         LASSERT(dt_object_exists(dt));
1898         LASSERT(osd_write_locked(env, obj));
1899         LASSERT(th != NULL);
1900
1901         spin_lock(&obj->oo_guard);
1902         LASSERT(inode->i_nlink > 0);
1903         inode->i_nlink--;
1904         spin_unlock(&obj->oo_guard);
1905         mark_inode_dirty(inode);
1906         LINVRNT(osd_invariant(obj));
1907 }
1908
1909 /*
1910  * Concurrency: @dt is read locked.
1911  */
1912 static int osd_xattr_get(const struct lu_env *env,
1913                          struct dt_object *dt,
1914                          struct lu_buf *buf,
1915                          const char *name,
1916                          struct lustre_capa *capa)
1917 {
1918         struct osd_object      *obj    = osd_dt_obj(dt);
1919         struct inode           *inode  = obj->oo_inode;
1920         struct osd_thread_info *info   = osd_oti_get(env);
1921         struct dentry          *dentry = &info->oti_obj_dentry;
1922
1923         LASSERT(dt_object_exists(dt));
1924         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1925         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1926
1927         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1928                 return -EACCES;
1929
1930         dentry->d_inode = inode;
1931         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1932 }
1933
1934 /*
1935  * Concurrency: @dt is write locked.
1936  */
1937 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1938                          const struct lu_buf *buf, const char *name, int fl,
1939                          struct thandle *handle, struct lustre_capa *capa)
1940 {
1941         LASSERT(handle != NULL);
1942
1943         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1944                 return -EACCES;
1945
1946         return __osd_xattr_set(env, dt, buf, name, fl);
1947 }
1948
1949 /*
1950  * Concurrency: @dt is read locked.
1951  */
1952 static int osd_xattr_list(const struct lu_env *env,
1953                           struct dt_object *dt,
1954                           struct lu_buf *buf,
1955                           struct lustre_capa *capa)
1956 {
1957         struct osd_object      *obj    = osd_dt_obj(dt);
1958         struct inode           *inode  = obj->oo_inode;
1959         struct osd_thread_info *info   = osd_oti_get(env);
1960         struct dentry          *dentry = &info->oti_obj_dentry;
1961
1962         LASSERT(dt_object_exists(dt));
1963         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1964         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1965
1966         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1967                 return -EACCES;
1968
1969         dentry->d_inode = inode;
1970         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1971 }
1972
1973 /*
1974  * Concurrency: @dt is write locked.
1975  */
1976 static int osd_xattr_del(const struct lu_env *env,
1977                          struct dt_object *dt,
1978                          const char *name,
1979                          struct thandle *handle,
1980                          struct lustre_capa *capa)
1981 {
1982         struct osd_object      *obj    = osd_dt_obj(dt);
1983         struct inode           *inode  = obj->oo_inode;
1984         struct osd_thread_info *info   = osd_oti_get(env);
1985         struct dentry          *dentry = &info->oti_obj_dentry;
1986         struct timespec        *t      = &info->oti_time;
1987         int                     rc;
1988
1989         LASSERT(dt_object_exists(dt));
1990         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1991         LASSERT(osd_write_locked(env, obj));
1992         LASSERT(handle != NULL);
1993
1994         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1995                 return -EACCES;
1996
1997         dentry->d_inode = inode;
1998         *t = inode->i_ctime;
1999         rc = inode->i_op->removexattr(dentry, name);
2000         /* ctime should not be updated with server-side time. */
2001         spin_lock(&obj->oo_guard);
2002         inode->i_ctime = *t;
2003         spin_unlock(&obj->oo_guard);
2004         mark_inode_dirty(inode);
2005         return rc;
2006 }
2007
2008 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2009                                      struct dt_object *dt,
2010                                      struct lustre_capa *old,
2011                                      __u64 opc)
2012 {
2013         struct osd_thread_info *info = osd_oti_get(env);
2014         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2015         struct osd_object *obj = osd_dt_obj(dt);
2016         struct osd_device *dev = osd_obj2dev(obj);
2017         struct lustre_capa_key *key = &info->oti_capa_key;
2018         struct lustre_capa *capa = &info->oti_capa;
2019         struct obd_capa *oc;
2020         struct md_capainfo *ci;
2021         int rc;
2022         ENTRY;
2023
2024         if (!dev->od_fl_capa)
2025                 RETURN(ERR_PTR(-ENOENT));
2026
2027         LASSERT(dt_object_exists(dt));
2028         LINVRNT(osd_invariant(obj));
2029
2030         /* renewal sanity check */
2031         if (old && osd_object_auth(env, dt, old, opc))
2032                 RETURN(ERR_PTR(-EACCES));
2033
2034         ci = md_capainfo(env);
2035         if (unlikely(!ci))
2036                 RETURN(ERR_PTR(-ENOENT));
2037
2038         switch (ci->mc_auth) {
2039         case LC_ID_NONE:
2040                 RETURN(NULL);
2041         case LC_ID_PLAIN:
2042                 capa->lc_uid = obj->oo_inode->i_uid;
2043                 capa->lc_gid = obj->oo_inode->i_gid;
2044                 capa->lc_flags = LC_ID_PLAIN;
2045                 break;
2046         case LC_ID_CONVERT: {
2047                 __u32 d[4], s[4];
2048
2049                 s[0] = obj->oo_inode->i_uid;
2050                 get_random_bytes(&(s[1]), sizeof(__u32));
2051                 s[2] = obj->oo_inode->i_gid;
2052                 get_random_bytes(&(s[3]), sizeof(__u32));
2053                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2054                 if (unlikely(rc))
2055                         RETURN(ERR_PTR(rc));
2056
2057                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2058                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2059                 capa->lc_flags = LC_ID_CONVERT;
2060                 break;
2061         }
2062         default:
2063                 RETURN(ERR_PTR(-EINVAL));
2064         }
2065
2066         capa->lc_fid = *fid;
2067         capa->lc_opc = opc;
2068         capa->lc_flags |= dev->od_capa_alg << 24;
2069         capa->lc_timeout = dev->od_capa_timeout;
2070         capa->lc_expiry = 0;
2071
2072         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2073         if (oc) {
2074                 LASSERT(!capa_is_expired(oc));
2075                 RETURN(oc);
2076         }
2077
2078         spin_lock(&capa_lock);
2079         *key = dev->od_capa_keys[1];
2080         spin_unlock(&capa_lock);
2081
2082         capa->lc_keyid = key->lk_keyid;
2083         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2084
2085         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2086         if (rc) {
2087                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2088                 RETURN(ERR_PTR(rc));
2089         }
2090
2091         oc = capa_add(dev->od_capa_hash, capa);
2092         RETURN(oc);
2093 }
2094
2095 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2096 {
2097         int rc;
2098         struct osd_object      *obj    = osd_dt_obj(dt);
2099         struct inode           *inode  = obj->oo_inode;
2100         struct osd_thread_info *info   = osd_oti_get(env);
2101         struct dentry          *dentry = &info->oti_obj_dentry;
2102         struct file            *file   = &info->oti_file;
2103         ENTRY;
2104
2105         dentry->d_inode = inode;
2106         file->f_dentry = dentry;
2107         file->f_mapping = inode->i_mapping;
2108         file->f_op = inode->i_fop;
2109         LOCK_INODE_MUTEX(inode);
2110         rc = file->f_op->fsync(file, dentry, 0);
2111         UNLOCK_INODE_MUTEX(inode);
2112         RETURN(rc);
2113 }
2114
2115 /*
2116  * Get the 64-bit version for an inode.
2117  */
2118 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2119                                                struct dt_object *dt)
2120 {
2121         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2122
2123         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2124                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2125         return LDISKFS_I(inode)->i_fs_version;
2126 }
2127
2128 /*
2129  * Set the 64-bit version and return the old version.
2130  */
2131 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2132                                    dt_obj_version_t new_version)
2133 {
2134         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2135
2136         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2137                new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2138         LDISKFS_I(inode)->i_fs_version = new_version;
2139         /** Version is set after all inode operations are finished,
2140          *  so we should mark it dirty here */
2141         inode->i_sb->s_op->dirty_inode(inode);
2142 }
2143
2144 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2145                         void **data)
2146 {
2147         struct osd_object *obj = osd_dt_obj(dt);
2148         ENTRY;
2149
2150         *data = (void *)obj->oo_inode;
2151         RETURN(0);
2152 }
2153
2154 /*
2155  * Index operations.
2156  */
2157
2158 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2159                            const struct dt_index_features *feat)
2160 {
2161         struct iam_descr *descr;
2162
2163         if (osd_object_is_root(o))
2164                 return feat == &dt_directory_features;
2165
2166         LASSERT(o->oo_dir != NULL);
2167
2168         descr = o->oo_dir->od_container.ic_descr;
2169         if (feat == &dt_directory_features) {
2170                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2171                         return 1;
2172                 else
2173                         return 0;
2174         } else {
2175                 return
2176                         feat->dif_keysize_min <= descr->id_key_size &&
2177                         descr->id_key_size <= feat->dif_keysize_max &&
2178                         feat->dif_recsize_min <= descr->id_rec_size &&
2179                         descr->id_rec_size <= feat->dif_recsize_max &&
2180                         !(feat->dif_flags & (DT_IND_VARKEY |
2181                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2182                         ergo(feat->dif_flags & DT_IND_UPDATE,
2183                              1 /* XXX check that object (and file system) is
2184                                 * writable */);
2185         }
2186 }
2187
2188 static int osd_iam_container_init(const struct lu_env *env,
2189                                   struct osd_object *obj,
2190                                   struct osd_directory *dir)
2191 {
2192         int result;
2193         struct iam_container *bag;
2194
2195         bag    = &dir->od_container;
2196         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2197         if (result == 0) {
2198                 result = iam_container_setup(bag);
2199                 if (result == 0)
2200                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2201                 else
2202                         iam_container_fini(bag);
2203         }
2204         return result;
2205 }
2206
2207
2208 /*
2209  * Concurrency: no external locking is necessary.
2210  */
2211 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2212                          const struct dt_index_features *feat)
2213 {
2214         int result;
2215         int ea_dir = 0;
2216         struct osd_object *obj = osd_dt_obj(dt);
2217         struct osd_device *osd = osd_obj2dev(obj);
2218
2219         LINVRNT(osd_invariant(obj));
2220         LASSERT(dt_object_exists(dt));
2221
2222         if (osd_object_is_root(obj)) {
2223                 dt->do_index_ops = &osd_index_ea_ops;
2224                 result = 0;
2225         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2226                 dt->do_index_ops = &osd_index_ea_ops;
2227                 if (S_ISDIR(obj->oo_inode->i_mode))
2228                         result = 0;
2229                 else
2230                         result = -ENOTDIR;
2231                 ea_dir = 1;
2232         } else if (!osd_has_index(obj)) {
2233                 struct osd_directory *dir;
2234
2235                 OBD_ALLOC_PTR(dir);
2236                 if (dir != NULL) {
2237
2238                         spin_lock(&obj->oo_guard);
2239                         if (obj->oo_dir == NULL)
2240                                 obj->oo_dir = dir;
2241                         else
2242                                 /*
2243                                  * Concurrent thread allocated container data.
2244                                  */
2245                                 OBD_FREE_PTR(dir);
2246                         spin_unlock(&obj->oo_guard);
2247                         /*
2248                          * Now, that we have container data, serialize its
2249                          * initialization.
2250                          */
2251                         down_write(&obj->oo_ext_idx_sem);
2252                         /*
2253                          * recheck under lock.
2254                          */
2255                         if (!osd_has_index(obj))
2256                                 result = osd_iam_container_init(env, obj, dir);
2257                         else
2258                                 result = 0;
2259                         up_write(&obj->oo_ext_idx_sem);
2260                 } else
2261                         result = -ENOMEM;
2262         } else
2263                 result = 0;
2264
2265         if (result == 0 && ea_dir == 0) {
2266                 if (!osd_iam_index_probe(env, obj, feat))
2267                         result = -ENOTDIR;
2268         }
2269         LINVRNT(osd_invariant(obj));
2270
2271         return result;
2272 }
2273
2274 static const struct dt_object_operations osd_obj_ops = {
2275         .do_read_lock    = osd_object_read_lock,
2276         .do_write_lock   = osd_object_write_lock,
2277         .do_read_unlock  = osd_object_read_unlock,
2278         .do_write_unlock = osd_object_write_unlock,
2279         .do_write_locked = osd_object_write_locked,
2280         .do_attr_get     = osd_attr_get,
2281         .do_attr_set     = osd_attr_set,
2282         .do_ah_init      = osd_ah_init,
2283         .do_create       = osd_object_create,
2284         .do_index_try    = osd_index_try,
2285         .do_ref_add      = osd_object_ref_add,
2286         .do_ref_del      = osd_object_ref_del,
2287         .do_xattr_get    = osd_xattr_get,
2288         .do_xattr_set    = osd_xattr_set,
2289         .do_xattr_del    = osd_xattr_del,
2290         .do_xattr_list   = osd_xattr_list,
2291         .do_capa_get     = osd_capa_get,
2292         .do_object_sync  = osd_object_sync,
2293         .do_version_get  = osd_object_version_get,
2294         .do_version_set  = osd_object_version_set,
2295         .do_data_get     = osd_data_get,
2296 };
2297
2298 /**
2299  * dt_object_operations for interoperability mode
2300  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2301  */
2302 static const struct dt_object_operations osd_obj_ea_ops = {
2303         .do_read_lock    = osd_object_read_lock,
2304         .do_write_lock   = osd_object_write_lock,
2305         .do_read_unlock  = osd_object_read_unlock,
2306         .do_write_unlock = osd_object_write_unlock,
2307         .do_write_locked = osd_object_write_locked,
2308         .do_attr_get     = osd_attr_get,
2309         .do_attr_set     = osd_attr_set,
2310         .do_ah_init      = osd_ah_init,
2311         .do_create       = osd_object_ea_create,
2312         .do_index_try    = osd_index_try,
2313         .do_ref_add      = osd_object_ref_add,
2314         .do_ref_del      = osd_object_ref_del,
2315         .do_xattr_get    = osd_xattr_get,
2316         .do_xattr_set    = osd_xattr_set,
2317         .do_xattr_del    = osd_xattr_del,
2318         .do_xattr_list   = osd_xattr_list,
2319         .do_capa_get     = osd_capa_get,
2320         .do_object_sync  = osd_object_sync,
2321         .do_version_get  = osd_object_version_get,
2322         .do_version_set  = osd_object_version_set,
2323         .do_data_get     = osd_data_get,
2324 };
2325
2326 /*
2327  * Body operations.
2328  */
2329
2330 /*
2331  * XXX: Another layering violation for now.
2332  *
2333  * We don't want to use ->f_op->read methods, because generic file write
2334  *
2335  *         - serializes on ->i_sem, and
2336  *
2337  *         - does a lot of extra work like balance_dirty_pages(),
2338  *
2339  * which doesn't work for globally shared files like /last-received.
2340  */
2341 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2342 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2343                                 loff_t *offs, handle_t *handle);
2344
2345 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2346                         struct lu_buf *buf, loff_t *pos,
2347                         struct lustre_capa *capa)
2348 {
2349         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2350
2351         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2352                 RETURN(-EACCES);
2353
2354         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2355 }
2356
2357 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2358                          const struct lu_buf *buf, loff_t *pos,
2359                          struct thandle *handle, struct lustre_capa *capa,
2360                          int ignore_quota)
2361 {
2362         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
2363         struct osd_thandle *oh;
2364         ssize_t             result;
2365 #ifdef HAVE_QUOTA_SUPPORT
2366         cfs_cap_t           save = current->cap_effective;
2367 #endif
2368
2369         LASSERT(handle != NULL);
2370
2371         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2372                 RETURN(-EACCES);
2373
2374         oh = container_of(handle, struct osd_thandle, ot_super);
2375         LASSERT(oh->ot_handle->h_transaction != NULL);
2376 #ifdef HAVE_QUOTA_SUPPORT
2377         if (ignore_quota)
2378                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2379         else
2380                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2381 #endif
2382         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2383                                              pos, oh->ot_handle);
2384 #ifdef HAVE_QUOTA_SUPPORT
2385         current->cap_effective = save;
2386 #endif
2387         if (result == 0)
2388                 result = buf->lb_len;
2389         return result;
2390 }
2391
2392 static const struct dt_body_operations osd_body_ops = {
2393         .dbo_read  = osd_read,
2394         .dbo_write = osd_write
2395 };
2396
2397
2398 /**
2399  *      delete a (key, value) pair from index \a dt specified by \a key
2400  *
2401  *      \param  dt_object      osd index object
2402  *      \param  key     key for index
2403  *      \param  rec     record reference
2404  *      \param  handle  transaction handler
2405  *
2406  *      \retval  0  success
2407  *      \retval -ve   failure
2408  */
2409
2410 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2411                                 const struct dt_key *key, struct thandle *handle,
2412                                 struct lustre_capa *capa)
2413 {
2414         struct osd_object     *obj = osd_dt_obj(dt);
2415         struct osd_thandle    *oh;
2416         struct iam_path_descr *ipd;
2417         struct iam_container  *bag = &obj->oo_dir->od_container;
2418         int rc;
2419
2420         ENTRY;
2421
2422         LINVRNT(osd_invariant(obj));
2423         LASSERT(dt_object_exists(dt));
2424         LASSERT(bag->ic_object == obj->oo_inode);
2425         LASSERT(handle != NULL);
2426
2427         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2428                 RETURN(-EACCES);
2429
2430         ipd = osd_idx_ipd_get(env, bag);
2431         if (unlikely(ipd == NULL))
2432                 RETURN(-ENOMEM);
2433
2434         oh = container_of0(handle, struct osd_thandle, ot_super);
2435         LASSERT(oh->ot_handle != NULL);
2436         LASSERT(oh->ot_handle->h_transaction != NULL);
2437
2438         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2439         osd_ipd_put(env, bag, ipd);
2440         LINVRNT(osd_invariant(obj));
2441         RETURN(rc);
2442 }
2443
2444 /**
2445  * Index delete function for interoperability mode (b11826).
2446  * It will remove the directory entry added by osd_index_ea_insert().
2447  * This entry is needed to maintain name->fid mapping.
2448  *
2449  * \param key,  key i.e. file entry to be deleted
2450  *
2451  * \retval   0, on success
2452  * \retval -ve, on error
2453  */
2454 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2455                                const struct dt_key *key, struct thandle *handle,
2456                                struct lustre_capa *capa)
2457 {
2458         struct osd_object          *obj    = osd_dt_obj(dt);
2459         struct inode               *dir    = obj->oo_inode;
2460         struct dentry              *dentry;
2461         struct osd_thandle         *oh;
2462         struct ldiskfs_dir_entry_2 *de;
2463         struct buffer_head         *bh;
2464
2465         int rc;
2466
2467         ENTRY;
2468
2469         LINVRNT(osd_invariant(obj));
2470         LASSERT(dt_object_exists(dt));
2471         LASSERT(handle != NULL);
2472
2473         oh = container_of(handle, struct osd_thandle, ot_super);
2474         LASSERT(oh->ot_handle != NULL);
2475         LASSERT(oh->ot_handle->h_transaction != NULL);
2476
2477         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2478                 RETURN(-EACCES);
2479
2480         dentry = osd_child_dentry_get(env, obj,
2481                                       (char *)key, strlen((char *)key));
2482
2483         down_write(&obj->oo_ext_idx_sem);
2484         bh = ldiskfs_find_entry(dentry, &de);
2485         if (bh) {
2486                 struct osd_thread_info *oti = osd_oti_get(env);
2487                 struct timespec *ctime = &oti->oti_time;
2488                 struct timespec *mtime = &oti->oti_time2;
2489
2490                 *ctime = dir->i_ctime;
2491                 *mtime = dir->i_mtime;
2492                 rc = ldiskfs_delete_entry(oh->ot_handle,
2493                                 dir, de, bh);
2494                 /* xtime should not be updated with server-side time. */
2495                 spin_lock(&obj->oo_guard);
2496                 dir->i_ctime = *ctime;
2497                 dir->i_mtime = *mtime;
2498                 spin_unlock(&obj->oo_guard);
2499                 mark_inode_dirty(dir);
2500                 brelse(bh);
2501         } else
2502                 rc = -ENOENT;
2503
2504         up_write(&obj->oo_ext_idx_sem);
2505         LASSERT(osd_invariant(obj));
2506         RETURN(rc);
2507 }
2508
2509 /**
2510  *      Lookup index for \a key and copy record to \a rec.
2511  *
2512  *      \param  dt_object      osd index object
2513  *      \param  key     key for index
2514  *      \param  rec     record reference
2515  *
2516  *      \retval  +ve  success : exact mach
2517  *      \retval  0    return record with key not greater than \a key
2518  *      \retval -ve   failure
2519  */
2520 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2521                                 struct dt_rec *rec, const struct dt_key *key,
2522                                 struct lustre_capa *capa)
2523 {
2524         struct osd_object     *obj = osd_dt_obj(dt);
2525         struct iam_path_descr *ipd;
2526         struct iam_container  *bag = &obj->oo_dir->od_container;
2527         struct osd_thread_info *oti = osd_oti_get(env);
2528         struct iam_iterator    *it = &oti->oti_idx_it;
2529         struct iam_rec *iam_rec;
2530         int rc;
2531         ENTRY;
2532
2533         LASSERT(osd_invariant(obj));
2534         LASSERT(dt_object_exists(dt));
2535         LASSERT(bag->ic_object == obj->oo_inode);
2536
2537         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2538                 RETURN(-EACCES);
2539
2540         ipd = osd_idx_ipd_get(env, bag);
2541         if (IS_ERR(ipd))
2542                 RETURN(-ENOMEM);
2543
2544         /* got ipd now we can start iterator. */
2545         iam_it_init(it, bag, 0, ipd);
2546
2547         rc = iam_it_get(it, (struct iam_key *)key);
2548         if (rc >= 0) {
2549                 if (S_ISDIR(obj->oo_inode->i_mode))
2550                         iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2551                 else
2552                         iam_rec = (struct iam_rec *) rec;
2553
2554                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2555                 if (S_ISDIR(obj->oo_inode->i_mode))
2556                         osd_fid_unpack((struct lu_fid *) rec,
2557                                        (struct osd_fid_pack *)iam_rec);
2558         }
2559         iam_it_put(it);
2560         iam_it_fini(it);
2561         osd_ipd_put(env, bag, ipd);
2562
2563         LINVRNT(osd_invariant(obj));
2564
2565         RETURN(rc);
2566 }
2567
2568 /**
2569  *      Inserts (key, value) pair in \a dt index object.
2570  *
2571  *      \param  dt      osd index object
2572  *      \param  key     key for index
2573  *      \param  rec     record reference
2574  *      \param  th      transaction handler
2575  *
2576  *      \retval  0  success
2577  *      \retval -ve failure
2578  */
2579 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2580                                 const struct dt_rec *rec, const struct dt_key *key,
2581                                 struct thandle *th, struct lustre_capa *capa,
2582                                 int ignore_quota)
2583 {
2584         struct osd_object     *obj = osd_dt_obj(dt);
2585         struct iam_path_descr *ipd;
2586         struct osd_thandle    *oh;
2587         struct iam_container  *bag = &obj->oo_dir->od_container;
2588 #ifdef HAVE_QUOTA_SUPPORT
2589         cfs_cap_t              save = current->cap_effective;
2590 #endif
2591         struct osd_thread_info *oti = osd_oti_get(env);
2592         struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2593         int rc;
2594
2595         ENTRY;
2596
2597         LINVRNT(osd_invariant(obj));
2598         LASSERT(dt_object_exists(dt));
2599         LASSERT(bag->ic_object == obj->oo_inode);
2600         LASSERT(th != NULL);
2601
2602         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2603                 return -EACCES;
2604
2605         ipd = osd_idx_ipd_get(env, bag);
2606         if (unlikely(ipd == NULL))
2607                 RETURN(-ENOMEM);
2608
2609         oh = container_of0(th, struct osd_thandle, ot_super);
2610         LASSERT(oh->ot_handle != NULL);
2611         LASSERT(oh->ot_handle->h_transaction != NULL);
2612 #ifdef HAVE_QUOTA_SUPPORT
2613         if (ignore_quota)
2614                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2615         else
2616                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2617 #endif
2618         if (S_ISDIR(obj->oo_inode->i_mode))
2619                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2620         else
2621                 iam_rec = (struct iam_rec *) rec;
2622         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2623                         iam_rec, ipd);
2624 #ifdef HAVE_QUOTA_SUPPORT
2625         current->cap_effective = save;
2626 #endif
2627         osd_ipd_put(env, bag, ipd);
2628         LINVRNT(osd_invariant(obj));
2629         RETURN(rc);
2630 }
2631
2632 /**
2633  * Calls ldiskfs_add_entry() to add directory entry
2634  * into the directory. This is required for
2635  * interoperability mode (b11826)
2636  *
2637  * \retval   0, on success
2638  * \retval -ve, on error
2639  */
2640 static int __osd_ea_add_rec(struct osd_thread_info *info,
2641                             struct osd_object *pobj,
2642                             struct osd_object *cobj,
2643                             const char *name,
2644                             struct thandle *th)
2645 {
2646         struct dentry      *child;
2647         struct osd_thandle *oth;
2648         struct inode       *cinode  = cobj->oo_inode;
2649         int rc;
2650
2651         oth = container_of(th, struct osd_thandle, ot_super);
2652         LASSERT(oth->ot_handle != NULL);
2653         LASSERT(oth->ot_handle->h_transaction != NULL);
2654
2655         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2656         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2657
2658         RETURN(rc);
2659 }
2660
2661 /**
2662  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2663  * into the directory.Also sets flags into osd object to
2664  * indicate dot and dotdot are created. This is required for
2665  * interoperability mode (b11826)
2666  *
2667  * \param dir   directory for dot and dotdot fixup.
2668  * \param obj   child object for linking
2669  *
2670  * \retval   0, on success
2671  * \retval -ve, on error
2672  */
2673 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2674                               struct osd_object *dir,
2675                               struct osd_object *obj, const char *name,
2676                               struct thandle *th)
2677 {
2678         struct inode            *parent_dir   = obj->oo_inode;
2679         struct inode            *inode  = dir->oo_inode;
2680         struct osd_thandle      *oth;
2681         int result = 0;
2682
2683         oth = container_of(th, struct osd_thandle, ot_super);
2684         LASSERT(oth->ot_handle->h_transaction != NULL);
2685         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2686
2687         if (strcmp(name, dot) == 0) {
2688                 if (dir->oo_compat_dot_created) {
2689                         result = -EEXIST;
2690                 } else {
2691                         LASSERT(obj == dir);
2692                         dir->oo_compat_dot_created = 1;
2693                         result = 0;
2694                 }
2695         } else if(strcmp(name, dotdot) == 0) {
2696                 if (!dir->oo_compat_dot_created)
2697                         return -EINVAL;
2698                 if (dir->oo_compat_dotdot_created)
2699                         return __osd_ea_add_rec(info, dir, obj, name, th);
2700
2701                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2702                 if (result == 0)
2703                        dir->oo_compat_dotdot_created = 1;
2704         }
2705
2706         return result;
2707 }
2708
2709
2710 /**
2711  * It will call the appropriate osd_add* function and return the
2712  * value, return by respective functions.
2713  */
2714 static int osd_ea_add_rec(const struct lu_env *env,
2715                           struct osd_object *pobj,
2716                           struct osd_object *cobj,
2717                           const char *name,
2718                           struct thandle *th)
2719 {
2720         struct osd_thread_info    *info   = osd_oti_get(env);
2721         int rc;
2722
2723         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2724                                                    name[2] =='\0')))
2725                 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2726         else
2727                 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2728
2729         return rc;
2730 }
2731
2732 /**
2733  * Calls ->lookup() to find dentry. From dentry get inode and
2734  * read inode's ea to get fid. This is required for  interoperability
2735  * mode (b11826)
2736  *
2737  * \retval   0, on success
2738  * \retval -ve, on error
2739  */
2740 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2741                              struct dt_rec *rec, const struct dt_key *key)
2742 {
2743         struct inode               *dir    = obj->oo_inode;
2744         struct dentry              *dentry;
2745         struct ldiskfs_dir_entry_2 *de;
2746         struct buffer_head         *bh;
2747         struct lu_fid              *fid = (struct lu_fid *) rec;
2748         int ino;
2749         int rc;
2750
2751         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2752
2753         dentry = osd_child_dentry_get(env, obj,
2754                                       (char *)key, strlen((char *)key));
2755
2756         down_read(&obj->oo_ext_idx_sem);
2757         bh = ldiskfs_find_entry(dentry, &de);
2758         if (bh) {
2759                 ino = le32_to_cpu(de->inode);
2760                 brelse(bh);
2761                 rc = osd_ea_fid_get(env, obj, ino, fid);
2762         } else
2763                 rc = -ENOENT;
2764
2765         up_read(&obj->oo_ext_idx_sem);
2766         RETURN (rc);
2767 }
2768
2769 /**
2770  * Find the osd object for given fid.
2771  *
2772  * \param fid, need to find the osd object having this fid
2773  *
2774  * \retval osd_object, on success
2775  * \retval        -ve, on error
2776  */
2777 struct osd_object *osd_object_find(const struct lu_env *env,
2778                                    struct dt_object *dt,
2779                                    const struct lu_fid *fid)
2780 {
2781         struct lu_device         *ludev = dt->do_lu.lo_dev;
2782         struct osd_object        *child = NULL;
2783         struct lu_object         *luch;
2784         struct lu_object         *lo;
2785
2786         luch = lu_object_find(env, ludev, fid, NULL);
2787         if (!IS_ERR(luch)) {
2788                 if (lu_object_exists(luch)) {
2789                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2790                         if (lo != NULL)
2791                                 child = osd_obj(lo);
2792                         else
2793                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2794                                                 "lu_object can't be located"
2795                                                 ""DFID"\n", PFID(fid));
2796
2797                         if (child == NULL) {
2798                                 lu_object_put(env, luch);
2799                                 CERROR("Unable to get osd_object\n");
2800                                 child = ERR_PTR(-ENOENT);
2801                         }
2802                 } else {
2803                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
2804                                         "lu_object does not exists "DFID"\n",
2805                                         PFID(fid));
2806                         child = ERR_PTR(-ENOENT);
2807                 }
2808         } else
2809                 child = (void *)luch;
2810
2811         return child;
2812 }
2813
2814 /**
2815  * Put the osd object once done with it.
2816  *
2817  * \param obj, osd object that needs to be put
2818  */
2819 static inline void osd_object_put(const struct lu_env *env,
2820                                   struct osd_object *obj)
2821 {
2822         lu_object_put(env, &obj->oo_dt.do_lu);
2823 }
2824
2825 /**
2826  * Index add function for interoperability mode (b11826).
2827  * It will add the directory entry.This entry is needed to
2828  * maintain name->fid mapping.
2829  *
2830  * \param key, it is key i.e. file entry to be inserted
2831  * \param rec, it is value of given key i.e. fid
2832  *
2833  * \retval   0, on success
2834  * \retval -ve, on error
2835  */
2836 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2837                                const struct dt_rec *rec,
2838                                const struct dt_key *key, struct thandle *th,
2839                                struct lustre_capa *capa, int ignore_quota)
2840 {
2841         struct osd_object        *obj   = osd_dt_obj(dt);
2842         struct lu_fid            *fid   = (struct lu_fid *) rec;
2843         const char               *name  = (const char *)key;
2844         struct osd_object        *child;
2845 #ifdef HAVE_QUOTA_SUPPORT
2846         cfs_cap_t                 save  = current->cap_effective;
2847 #endif
2848         int rc;
2849
2850         ENTRY;
2851
2852         LASSERT(osd_invariant(obj));
2853         LASSERT(dt_object_exists(dt));
2854         LASSERT(th != NULL);
2855
2856         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2857                 RETURN(-EACCES);
2858
2859         child = osd_object_find(env, dt, fid);
2860         if (!IS_ERR(child)) {
2861                 struct inode *inode = obj->oo_inode;
2862                 struct osd_thread_info *oti = osd_oti_get(env);
2863                 struct timespec *ctime = &oti->oti_time;
2864                 struct timespec *mtime = &oti->oti_time2;
2865
2866                 *ctime = inode->i_ctime;
2867                 *mtime = inode->i_mtime;
2868 #ifdef HAVE_QUOTA_SUPPORT
2869                 if (ignore_quota)
2870                         current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2871                 else
2872                         current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2873 #endif
2874                 down_write(&obj->oo_ext_idx_sem);
2875                 rc = osd_ea_add_rec(env, obj, child, name, th);
2876                 up_write(&obj->oo_ext_idx_sem);
2877 #ifdef HAVE_QUOTA_SUPPORT
2878                 current->cap_effective = save;
2879 #endif
2880                 osd_object_put(env, child);
2881                 /* xtime should not be updated with server-side time. */
2882                 spin_lock(&obj->oo_guard);
2883                 inode->i_ctime = *ctime;
2884                 inode->i_mtime = *mtime;
2885                 spin_unlock(&obj->oo_guard);
2886                 mark_inode_dirty(inode);
2887         } else {
2888                 rc = PTR_ERR(child);
2889         }
2890
2891         LASSERT(osd_invariant(obj));
2892         RETURN(rc);
2893 }
2894
2895 /**
2896  *  Initialize osd Iterator for given osd index object.
2897  *
2898  *  \param  dt      osd index object
2899  */
2900
2901 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2902                                  struct dt_object *dt,
2903                                  struct lustre_capa *capa)
2904 {
2905         struct osd_it_iam         *it;
2906         struct osd_thread_info *oti = osd_oti_get(env);
2907         struct osd_object     *obj = osd_dt_obj(dt);
2908         struct lu_object      *lo  = &dt->do_lu;
2909         struct iam_path_descr *ipd;
2910         struct iam_container  *bag = &obj->oo_dir->od_container;
2911
2912         LASSERT(lu_object_exists(lo));
2913
2914         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2915                 return ERR_PTR(-EACCES);
2916
2917         it = &oti->oti_it;
2918         ipd = osd_it_ipd_get(env, bag);
2919         if (likely(ipd != NULL)) {
2920                 it->oi_obj = obj;
2921                 it->oi_ipd = ipd;
2922                 lu_object_get(lo);
2923                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2924                 return (struct dt_it *)it;
2925         }
2926         return ERR_PTR(-ENOMEM);
2927 }
2928
2929 /**
2930  * free given Iterator.
2931  */
2932
2933 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2934 {
2935         struct osd_it_iam     *it = (struct osd_it_iam *)di;
2936         struct osd_object *obj = it->oi_obj;
2937
2938         iam_it_fini(&it->oi_it);
2939         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2940         lu_object_put(env, &obj->oo_dt.do_lu);
2941 }
2942
2943 /**
2944  *  Move Iterator to record specified by \a key
2945  *
2946  *  \param  di      osd iterator
2947  *  \param  key     key for index
2948  *
2949  *  \retval +ve  di points to record with least key not larger than key
2950  *  \retval  0   di points to exact matched key
2951  *  \retval -ve  failure
2952  */
2953
2954 static int osd_it_iam_get(const struct lu_env *env,
2955                       struct dt_it *di, const struct dt_key *key)
2956 {
2957         struct osd_it_iam *it = (struct osd_it_iam *)di;
2958
2959         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2960 }
2961
2962 /**
2963  *  Release Iterator
2964  *
2965  *  \param  di      osd iterator
2966  */
2967
2968 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2969 {
2970         struct osd_it_iam *it = (struct osd_it_iam *)di;
2971
2972         iam_it_put(&it->oi_it);
2973 }
2974
2975 /**
2976  *  Move iterator by one record
2977  *
2978  *  \param  di      osd iterator
2979  *
2980  *  \retval +1   end of container reached
2981  *  \retval  0   success
2982  *  \retval -ve  failure
2983  */
2984
2985 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2986 {
2987         struct osd_it_iam *it = (struct osd_it_iam *)di;
2988
2989         return iam_it_next(&it->oi_it);
2990 }
2991
2992 /**
2993  * Return pointer to the key under iterator.
2994  */
2995
2996 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2997                                  const struct dt_it *di)
2998 {
2999         struct osd_it_iam *it = (struct osd_it_iam *)di;
3000
3001         return (struct dt_key *)iam_it_key_get(&it->oi_it);
3002 }
3003
3004 /**
3005  * Return size of key under iterator (in bytes)
3006  */
3007
3008 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3009 {
3010         struct osd_it_iam *it = (struct osd_it_iam *)di;
3011
3012         return iam_it_key_size(&it->oi_it);
3013 }
3014
3015 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3016                                        __u32 attr,
3017                                        int len,
3018                                        __u16 type)
3019 {
3020         struct luda_type        *lt;
3021         const unsigned           align = sizeof(struct luda_type) - 1;
3022
3023         /* check if file type is required */
3024         if (attr & LUDA_TYPE) {
3025                         len = (len + align) & ~align;
3026
3027                         lt = (void *) ent->lde_name + len;
3028                         lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3029                         ent->lde_attrs |= LUDA_TYPE;
3030         }
3031
3032         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3033 }
3034
3035 /**
3036  * build lu direct from backend fs dirent.
3037  */
3038
3039 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3040                                       struct lu_fid *fid,
3041                                       __u64 offset,
3042                                       char *name,
3043                                       __u16 namelen,
3044                                       __u16 type,
3045                                       __u32 attr)
3046 {
3047         fid_cpu_to_le(&ent->lde_fid, fid);
3048         ent->lde_attrs = LUDA_FID;
3049
3050         ent->lde_hash = cpu_to_le64(offset);
3051         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3052
3053         strncpy(ent->lde_name, name, namelen);
3054         ent->lde_namelen = cpu_to_le16(namelen);
3055
3056         /* append lustre attributes */
3057         osd_it_append_attrs(ent, attr, namelen, type);
3058 }
3059
3060 /**
3061  * Return pointer to the record under iterator.
3062  */
3063 static int osd_it_iam_rec(const struct lu_env *env,
3064                           const struct dt_it *di,
3065                           struct lu_dirent *lde,
3066                           __u32 attr)
3067 {
3068         struct osd_it_iam *it        = (struct osd_it_iam *)di;
3069         struct osd_thread_info *info = osd_oti_get(env);
3070         struct lu_fid     *fid       = &info->oti_fid;
3071         const struct osd_fid_pack *rec;
3072         char *name;
3073         int namelen;
3074         __u64 hash;
3075         int rc;
3076
3077         name = (char *)iam_it_key_get(&it->oi_it);
3078         if (IS_ERR(name))
3079                 RETURN(PTR_ERR(name));
3080
3081         namelen = iam_it_key_size(&it->oi_it);
3082
3083         rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3084         if (IS_ERR(rec))
3085                 RETURN(PTR_ERR(rec));
3086
3087         rc = osd_fid_unpack(fid, rec);
3088         if (rc)
3089                 RETURN(rc);
3090
3091         hash = iam_it_store(&it->oi_it);
3092
3093         /* IAM does not store object type in IAM index (dir) */
3094         osd_it_pack_dirent(lde, fid, hash, name, namelen,
3095                            0, LUDA_FID);
3096
3097         return 0;
3098 }
3099
3100 /**
3101  * Returns cookie for current Iterator position.
3102  */
3103 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3104 {
3105         struct osd_it_iam *it = (struct osd_it_iam *)di;
3106
3107         return iam_it_store(&it->oi_it);
3108 }
3109
3110 /**
3111  * Restore iterator from cookie.
3112  *
3113  * \param  di      osd iterator
3114  * \param  hash    Iterator location cookie
3115  *
3116  * \retval +ve  di points to record with least key not larger than key.
3117  * \retval  0   di points to exact matched key
3118  * \retval -ve  failure
3119  */
3120
3121 static int osd_it_iam_load(const struct lu_env *env,
3122                        const struct dt_it *di, __u64 hash)
3123 {
3124         struct osd_it_iam *it = (struct osd_it_iam *)di;
3125
3126         return iam_it_load(&it->oi_it, hash);
3127 }
3128
3129 static const struct dt_index_operations osd_index_iam_ops = {
3130         .dio_lookup = osd_index_iam_lookup,
3131         .dio_insert = osd_index_iam_insert,
3132         .dio_delete = osd_index_iam_delete,
3133         .dio_it     = {
3134                 .init     = osd_it_iam_init,
3135                 .fini     = osd_it_iam_fini,
3136                 .get      = osd_it_iam_get,
3137                 .put      = osd_it_iam_put,
3138                 .next     = osd_it_iam_next,
3139                 .key      = osd_it_iam_key,
3140                 .key_size = osd_it_iam_key_size,
3141                 .rec      = osd_it_iam_rec,
3142                 .store    = osd_it_iam_store,
3143                 .load     = osd_it_iam_load
3144         }
3145 };
3146
3147 /**
3148  * Creates or initializes iterator context.
3149  *
3150  * \retval struct osd_it_ea, iterator structure on success
3151  *
3152  */
3153 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3154                                     struct dt_object *dt,
3155                                     struct lustre_capa *capa)
3156 {
3157         struct osd_object       *obj  = osd_dt_obj(dt);
3158         struct osd_thread_info  *info = osd_oti_get(env);
3159         struct osd_it_ea        *it   = &info->oti_it_ea;
3160         struct lu_object        *lo   = &dt->do_lu;
3161         struct dentry           *obj_dentry = &info->oti_it_dentry;
3162         ENTRY;
3163         LASSERT(lu_object_exists(lo));
3164
3165         obj_dentry->d_inode = obj->oo_inode;
3166         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3167         obj_dentry->d_name.hash = 0;
3168
3169         it->oie_rd_dirent       = 0;
3170         it->oie_it_dirent       = 0;
3171         it->oie_dirent          = NULL;
3172         it->oie_buf             = info->oti_it_ea_buf;
3173         it->oie_obj             = obj;
3174         it->oie_file.f_pos      = 0;
3175         it->oie_file.f_dentry   = obj_dentry;
3176         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3177         it->oie_file.f_op         = obj->oo_inode->i_fop;
3178         it->oie_file.private_data = NULL;
3179         lu_object_get(lo);
3180         RETURN((struct dt_it *) it);
3181 }
3182
3183 /**
3184  * Destroy or finishes iterator context.
3185  *
3186  * \param di, struct osd_it_ea, iterator structure to be destroyed
3187  */
3188 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3189 {
3190         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3191         struct osd_object    *obj  = it->oie_obj;
3192         struct inode       *inode  = obj->oo_inode;
3193
3194         ENTRY;
3195         it->oie_file.f_op->release(inode, &it->oie_file);
3196         lu_object_put(env, &obj->oo_dt.do_lu);
3197         EXIT;
3198 }
3199
3200 /**
3201  * It position the iterator at given key, so that next lookup continues from
3202  * that key Or it is similar to dio_it->load() but based on a key,
3203  * rather than file position.
3204  *
3205  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3206  * to the beginning.
3207  *
3208  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3209  */
3210 static int osd_it_ea_get(const struct lu_env *env,
3211                          struct dt_it *di, const struct dt_key *key)
3212 {
3213         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3214
3215         ENTRY;
3216         LASSERT(((const char *)key)[0] == '\0');
3217         it->oie_file.f_pos      = 0;
3218         it->oie_rd_dirent       = 0;
3219         it->oie_it_dirent       = 0;
3220         it->oie_dirent          = NULL;
3221
3222         RETURN(+1);
3223 }
3224
3225 /**
3226  * Does nothing
3227  */
3228 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3229 {
3230 }
3231
3232 /**
3233  * It is called internally by ->readdir(). It fills the
3234  * iterator's in-memory data structure with required
3235  * information i.e. name, namelen, rec_size etc.
3236  *
3237  * \param buf, in which information to be filled in.
3238  * \param name, name of the file in given dir
3239  *
3240  * \retval 0, on success
3241  * \retval 1, on buffer full
3242  */
3243 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3244                                loff_t offset, __u64 ino,
3245                                unsigned d_type)
3246 {
3247         struct osd_it_ea        *it = (struct osd_it_ea *)buf;
3248         struct osd_it_ea_dirent *ent = it->oie_dirent;
3249         ENTRY;
3250
3251         /* this should never happen */
3252         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3253                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3254                 RETURN(-EIO);
3255         }
3256
3257         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3258             OSD_IT_EA_BUFSIZE)
3259                 RETURN(1);
3260
3261         ent->oied_ino     = ino;
3262         ent->oied_off     = offset;
3263         ent->oied_namelen = namelen;
3264         ent->oied_type    = d_type;
3265
3266         memcpy(ent->oied_name, name, namelen);
3267
3268         it->oie_rd_dirent++;
3269         it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3270         RETURN(0);
3271 }
3272
3273 /**
3274  * Calls ->readdir() to load a directory entry at a time
3275  * and stored it in iterator's in-memory data structure.
3276  *
3277  * \param di, struct osd_it_ea, iterator's in memory structure
3278  *
3279  * \retval   0, on success
3280  * \retval -ve, on error
3281  */
3282 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3283 {
3284         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3285         struct osd_object  *obj   = it->oie_obj;
3286         struct inode       *inode = obj->oo_inode;
3287         int                result = 0;
3288
3289         ENTRY;
3290         it->oie_dirent = it->oie_buf;
3291         it->oie_rd_dirent = 0;
3292
3293         down_read(&obj->oo_ext_idx_sem);
3294         result = inode->i_fop->readdir(&it->oie_file, it,
3295                                        (filldir_t) osd_ldiskfs_filldir);
3296
3297         up_read(&obj->oo_ext_idx_sem);
3298
3299         if (it->oie_rd_dirent == 0) {
3300                 result = -EIO;
3301         } else {
3302                 it->oie_dirent = it->oie_buf;
3303                 it->oie_it_dirent = 1;
3304         }
3305
3306         RETURN(result);
3307 }
3308
3309 /**
3310  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3311  * to load a directory entry at a time and stored it in
3312  * iterator's in-memory data structure.
3313  *
3314  * \param di, struct osd_it_ea, iterator's in memory structure
3315  *
3316  * \retval +ve, iterator reached to end
3317  * \retval   0, iterator not reached to end
3318  * \retval -ve, on error
3319  */
3320 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3321 {
3322         struct osd_it_ea *it = (struct osd_it_ea *)di;
3323         int rc;
3324
3325         ENTRY;
3326
3327         if (it->oie_it_dirent < it->oie_rd_dirent) {
3328                 it->oie_dirent = (void *) it->oie_dirent +
3329                                  size_round(sizeof(struct osd_it_ea_dirent) +
3330                                             it->oie_dirent->oied_namelen);
3331                 it->oie_it_dirent++;
3332                 RETURN(0);
3333         } else {
3334                 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3335                         rc = +1;
3336                 else
3337                         rc = osd_ldiskfs_it_fill(di);
3338         }
3339
3340         RETURN(rc);
3341 }
3342
3343 /**
3344  * Returns the key at current position from iterator's in memory structure.
3345  *
3346  * \param di, struct osd_it_ea, iterator's in memory structure
3347  *
3348  * \retval key i.e. struct dt_key on success
3349  */
3350 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3351                                     const struct dt_it *di)
3352 {
3353         struct osd_it_ea *it = (struct osd_it_ea *)di;
3354         ENTRY;
3355         RETURN((struct dt_key *)it->oie_dirent->oied_name);
3356 }
3357
3358 /**
3359  * Returns the key's size at current position from iterator's in memory structure.
3360  *
3361  * \param di, struct osd_it_ea, iterator's in memory structure
3362  *
3363  * \retval key_size i.e. struct dt_key on success
3364  */
3365 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3366 {
3367         struct osd_it_ea *it = (struct osd_it_ea *)di;
3368         ENTRY;
3369         RETURN(it->oie_dirent->oied_namelen);
3370 }
3371
3372
3373 /**
3374  * Returns the value (i.e. fid/igif) at current position from iterator's
3375  * in memory structure.
3376  *
3377  * \param di, struct osd_it_ea, iterator's in memory structure
3378  * \param attr, attr requested for dirent.
3379  * \param lde, lustre dirent
3380  *
3381  * \retval   0, no error and \param lde has correct lustre dirent.
3382  * \retval -ve, on error
3383  */
3384 static inline int osd_it_ea_rec(const struct lu_env *env,
3385                                 const struct dt_it *di,
3386                                 struct lu_dirent *lde,
3387                                 __u32 attr)
3388 {
3389         struct osd_it_ea        *it     = (struct osd_it_ea *)di;
3390         struct osd_object       *obj    = it->oie_obj;
3391         struct osd_thread_info  *info   = osd_oti_get(env);
3392         struct lu_fid           *fid       = &info->oti_fid;
3393         int                      rc;
3394
3395         ENTRY;
3396
3397         rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3398
3399         if (rc == 0)
3400                 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3401                                    it->oie_dirent->oied_name,
3402                                    it->oie_dirent->oied_namelen,
3403                                    it->oie_dirent->oied_type,
3404                                    attr);
3405         RETURN(rc);
3406 }
3407
3408 /**
3409  * Returns a cookie for current position of the iterator head, so that
3410  * user can use this cookie to load/start the iterator next time.
3411  *
3412  * \param di, struct osd_it_ea, iterator's in memory structure
3413  *
3414  * \retval cookie for current position, on success
3415  */
3416 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3417 {
3418         struct osd_it_ea *it = (struct osd_it_ea *)di;
3419         ENTRY;
3420         RETURN(it->oie_dirent->oied_off);
3421 }
3422
3423 /**
3424  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3425  * to load a directory entry at a time and stored it i inn,
3426  * in iterator's in-memory data structure.
3427  *
3428  * \param di, struct osd_it_ea, iterator's in memory structure
3429  *
3430  * \retval +ve, on success
3431  * \retval -ve, on error
3432  */
3433 static int osd_it_ea_load(const struct lu_env *env,
3434                           const struct dt_it *di, __u64 hash)
3435 {
3436         struct osd_it_ea *it = (struct osd_it_ea *)di;
3437         int rc;
3438
3439         ENTRY;
3440         it->oie_file.f_pos = hash;
3441
3442         rc =  osd_ldiskfs_it_fill(di);
3443         if (rc == 0)
3444                 rc = +1;
3445
3446         RETURN(rc);
3447 }
3448
3449 /**
3450  * Index lookup function for interoperability mode (b11826).
3451  *
3452  * \param key,  key i.e. file name to be searched
3453  *
3454  * \retval +ve, on success
3455  * \retval -ve, on error
3456  */
3457 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3458                                struct dt_rec *rec, const struct dt_key *key,
3459                                struct lustre_capa *capa)
3460 {
3461         struct osd_object *obj = osd_dt_obj(dt);
3462         int rc = 0;
3463
3464         ENTRY;
3465
3466         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3467         LINVRNT(osd_invariant(obj));
3468
3469         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3470                 return -EACCES;
3471
3472         rc = osd_ea_lookup_rec(env, obj, rec, key);
3473
3474         if (rc == 0)
3475                 rc = +1;
3476         RETURN(rc);
3477 }
3478
3479 /**
3480  * Index and Iterator operations for interoperability
3481  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3482  */
3483 static const struct dt_index_operations osd_index_ea_ops = {
3484         .dio_lookup = osd_index_ea_lookup,
3485         .dio_insert = osd_index_ea_insert,
3486         .dio_delete = osd_index_ea_delete,
3487         .dio_it     = {
3488                 .init     = osd_it_ea_init,
3489                 .fini     = osd_it_ea_fini,
3490                 .get      = osd_it_ea_get,
3491                 .put      = osd_it_ea_put,
3492                 .next     = osd_it_ea_next,
3493                 .key      = osd_it_ea_key,
3494                 .key_size = osd_it_ea_key_size,
3495                 .rec      = osd_it_ea_rec,
3496                 .store    = osd_it_ea_store,
3497                 .load     = osd_it_ea_load
3498         }
3499 };
3500
3501 static void *osd_key_init(const struct lu_context *ctx,
3502                           struct lu_context_key *key)
3503 {
3504         struct osd_thread_info *info;
3505
3506         OBD_ALLOC_PTR(info);
3507         if (info != NULL) {
3508                 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3509                 if (info->oti_it_ea_buf != NULL) {
3510                         info->oti_env = container_of(ctx, struct lu_env,
3511                                                      le_ctx);
3512                 } else {
3513                         OBD_FREE_PTR(info);
3514                         info = ERR_PTR(-ENOMEM);
3515                 }
3516         } else {
3517                 info = ERR_PTR(-ENOMEM);
3518         }
3519         return info;
3520 }
3521
3522 static void osd_key_fini(const struct lu_context *ctx,
3523                          struct lu_context_key *key, void* data)
3524 {
3525         struct osd_thread_info *info = data;
3526
3527         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3528         OBD_FREE_PTR(info);
3529 }
3530
3531 static void osd_key_exit(const struct lu_context *ctx,
3532                          struct lu_context_key *key, void *data)
3533 {
3534         struct osd_thread_info *info = data;
3535
3536         LASSERT(info->oti_r_locks == 0);
3537         LASSERT(info->oti_w_locks == 0);
3538         LASSERT(info->oti_txns    == 0);
3539 }
3540
3541 /* type constructor/destructor: osd_type_init, osd_type_fini */
3542 LU_TYPE_INIT_FINI(osd, &osd_key);
3543
3544 static struct lu_context_key osd_key = {
3545         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3546         .lct_init = osd_key_init,
3547         .lct_fini = osd_key_fini,
3548         .lct_exit = osd_key_exit
3549 };
3550
3551
3552 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3553                            const char *name, struct lu_device *next)
3554 {
3555         int rc;
3556         struct lu_context *ctx;
3557
3558         /* context for commit hooks */
3559         ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3560         rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3561         if (rc == 0) {
3562                 rc = osd_procfs_init(osd_dev(d), name);
3563                 ctx->lc_cookie = 0x3;
3564         }
3565         return rc;
3566 }
3567
3568 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3569 {
3570         struct osd_thread_info *info = osd_oti_get(env);
3571         ENTRY;
3572         if (o->od_obj_area != NULL) {
3573                 lu_object_put(env, &o->od_obj_area->do_lu);
3574                 o->od_obj_area = NULL;
3575         }
3576         osd_oi_fini(info, &o->od_oi);
3577
3578         RETURN(0);
3579 }
3580
3581 static int osd_mount(const struct lu_env *env,
3582                      struct osd_device *o, struct lustre_cfg *cfg)
3583 {
3584         struct lustre_mount_info *lmi;
3585         const char               *dev  = lustre_cfg_string(cfg, 0);
3586         struct lustre_disk_data  *ldd;
3587         struct lustre_sb_info    *lsi;
3588
3589         ENTRY;
3590
3591         if (o->od_mount != NULL) {
3592                 CERROR("Already mounted (%s)\n", dev);
3593                 RETURN(-EEXIST);
3594         }
3595
3596         /* get mount */
3597         lmi = server_get_mount(dev);
3598         if (lmi == NULL) {
3599                 CERROR("Cannot get mount info for %s!\n", dev);
3600                 RETURN(-EFAULT);
3601         }
3602
3603         LASSERT(lmi != NULL);
3604         /* save lustre_mount_info in dt_device */
3605         o->od_mount = lmi;
3606
3607         lsi = s2lsi(lmi->lmi_sb);
3608         ldd = lsi->lsi_ldd;
3609
3610         if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3611                 o->od_iop_mode = 0;
3612                 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3613         } else
3614                 o->od_iop_mode = 1;
3615
3616         o->od_obj_area = NULL;
3617         RETURN(0);
3618 }
3619
3620 static struct lu_device *osd_device_fini(const struct lu_env *env,
3621                                          struct lu_device *d)
3622 {
3623         int rc;
3624         ENTRY;
3625
3626         shrink_dcache_sb(osd_sb(osd_dev(d)));
3627         osd_sync(env, lu2dt_dev(d));
3628
3629         rc = osd_procfs_fini(osd_dev(d));
3630         if (rc) {
3631                 CERROR("proc fini error %d \n", rc);
3632                 RETURN (ERR_PTR(rc));
3633         }
3634
3635         if (osd_dev(d)->od_mount)
3636                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3637                                  osd_dev(d)->od_mount->lmi_mnt);
3638         osd_dev(d)->od_mount = NULL;
3639
3640         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3641         RETURN(NULL);
3642 }
3643
3644 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3645                                           struct lu_device_type *t,
3646                                           struct lustre_cfg *cfg)
3647 {
3648         struct lu_device  *l;
3649         struct osd_device *o;
3650
3651         OBD_ALLOC_PTR(o);
3652         if (o != NULL) {
3653                 int result;
3654
3655                 result = dt_device_init(&o->od_dt_dev, t);
3656                 if (result == 0) {
3657                         l = osd2lu_dev(o);
3658                         l->ld_ops = &osd_lu_ops;
3659                         o->od_dt_dev.dd_ops = &osd_dt_ops;
3660                         spin_lock_init(&o->od_osfs_lock);
3661                         o->od_osfs_age = cfs_time_shift_64(-1000);
3662                         o->od_capa_hash = init_capa_hash();
3663                         if (o->od_capa_hash == NULL) {
3664                                 dt_device_fini(&o->od_dt_dev);
3665                                 l = ERR_PTR(-ENOMEM);
3666                         }
3667                 } else
3668                         l = ERR_PTR(result);
3669
3670                 if (IS_ERR(l))
3671                         OBD_FREE_PTR(o);
3672         } else
3673                 l = ERR_PTR(-ENOMEM);
3674         return l;
3675 }
3676
3677 static struct lu_device *osd_device_free(const struct lu_env *env,
3678                                          struct lu_device *d)
3679 {
3680         struct osd_device *o = osd_dev(d);
3681         ENTRY;
3682
3683         cleanup_capa_hash(o->od_capa_hash);
3684         dt_device_fini(&o->od_dt_dev);
3685         OBD_FREE_PTR(o);
3686         RETURN(NULL);
3687 }
3688
3689 static int osd_process_config(const struct lu_env *env,
3690                               struct lu_device *d, struct lustre_cfg *cfg)
3691 {
3692         struct osd_device *o = osd_dev(d);
3693         int err;
3694         ENTRY;
3695
3696         switch(cfg->lcfg_command) {
3697         case LCFG_SETUP:
3698                 err = osd_mount(env, o, cfg);
3699                 break;
3700         case LCFG_CLEANUP:
3701                 err = osd_shutdown(env, o);
3702                 break;
3703         default:
3704                 err = -ENOSYS;
3705         }
3706
3707         RETURN(err);
3708 }
3709
3710 static int osd_recovery_complete(const struct lu_env *env,
3711                                  struct lu_device *d)
3712 {
3713         struct osd_device *o = osd_dev(d);
3714         ENTRY;
3715         /* TODO: orphans handling */
3716         ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3717         RETURN(0);
3718 }
3719
3720 static int osd_prepare(const struct lu_env *env,
3721                        struct lu_device *pdev,
3722                        struct lu_device *dev)
3723 {
3724         struct osd_device *osd = osd_dev(dev);
3725         struct lustre_sb_info *lsi;
3726         struct lustre_disk_data *ldd;
3727         struct lustre_mount_info  *lmi;
3728         struct osd_thread_info *oti = osd_oti_get(env);
3729         struct dt_object *d;
3730         int result;
3731
3732         ENTRY;
3733         /* 1. initialize oi before any file create or file open */
3734         result = osd_oi_init(oti, &osd->od_oi,
3735                              &osd->od_dt_dev, lu2md_dev(pdev));
3736         if (result != 0)
3737                 RETURN(result);
3738
3739         lmi = osd->od_mount;
3740         lsi = s2lsi(lmi->lmi_sb);
3741         ldd = lsi->lsi_ldd;
3742
3743         /* 2. setup local objects */
3744         result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3745         if (result)
3746                 goto out;
3747
3748         /* 3. open remote object dir */
3749         d = dt_store_open(env, lu2dt_dev(dev), "",
3750                           remote_obj_dir, &oti->oti_fid);
3751         if (!IS_ERR(d)) {
3752                 osd->od_obj_area = d;
3753                 result = 0;
3754         } else {
3755                 result = PTR_ERR(d);
3756                 osd->od_obj_area = NULL;
3757         }
3758
3759 out:
3760         RETURN(result);
3761 }
3762
3763 static const struct lu_object_operations osd_lu_obj_ops = {
3764         .loo_object_init      = osd_object_init,
3765         .loo_object_delete    = osd_object_delete,
3766         .loo_object_release   = osd_object_release,
3767         .loo_object_free      = osd_object_free,
3768         .loo_object_print     = osd_object_print,
3769         .loo_object_invariant = osd_object_invariant
3770 };
3771
3772 static const struct lu_device_operations osd_lu_ops = {
3773         .ldo_object_alloc      = osd_object_alloc,
3774         .ldo_process_config    = osd_process_config,
3775         .ldo_recovery_complete = osd_recovery_complete,
3776         .ldo_prepare           = osd_prepare,
3777 };
3778
3779 static const struct lu_device_type_operations osd_device_type_ops = {
3780         .ldto_init = osd_type_init,
3781         .ldto_fini = osd_type_fini,
3782
3783         .ldto_start = osd_type_start,
3784         .ldto_stop  = osd_type_stop,
3785
3786         .ldto_device_alloc = osd_device_alloc,
3787         .ldto_device_free  = osd_device_free,
3788
3789         .ldto_device_init    = osd_device_init,
3790         .ldto_device_fini    = osd_device_fini
3791 };
3792
3793 static struct lu_device_type osd_device_type = {
3794         .ldt_tags     = LU_DEVICE_DT,
3795         .ldt_name     = LUSTRE_OSD_NAME,
3796         .ldt_ops      = &osd_device_type_ops,
3797         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3798 };
3799
3800 /*
3801  * lprocfs legacy support.
3802  */
3803 static struct obd_ops osd_obd_device_ops = {
3804         .o_owner = THIS_MODULE
3805 };
3806
3807 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3808         .llod_name      = remote_obj_dir,
3809         .llod_oid       = OSD_REM_OBJ_DIR_OID,
3810         .llod_is_index  = 1,
3811         .llod_feat      = &dt_directory_features,
3812 };
3813
3814 static int __init osd_mod_init(void)
3815 {
3816         struct lprocfs_static_vars lvars;
3817
3818         osd_oi_mod_init();
3819         llo_local_obj_register(&llod_osd_rem_obj_dir);
3820         lprocfs_osd_init_vars(&lvars);
3821         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3822                                    LUSTRE_OSD_NAME, &osd_device_type);
3823 }
3824
3825 static void __exit osd_mod_exit(void)
3826 {
3827         llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3828         class_unregister_type(LUSTRE_OSD_NAME);
3829 }
3830
3831 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3832 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3833 MODULE_LICENSE("GPL");
3834
3835 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);