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