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