Whamcloud - gitweb
55b97d74bf838a00b19ca87f2545a309a3df1e4c
[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 = bits & (LA_UID | LA_GID);
1266                 iattr.ia_uid = attr->la_uid;
1267                 iattr.ia_gid = attr->la_gid;
1268                 osd_push_ctxt(env, save);
1269                 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1270                 osd_pop_ctxt(save);
1271                 if (rc != 0)
1272                         return rc;
1273         }
1274 #endif
1275
1276         if (bits & LA_ATIME)
1277                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1278         if (bits & LA_CTIME)
1279                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1280         if (bits & LA_MTIME)
1281                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1282         if (bits & LA_SIZE) {
1283                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1284                 i_size_write(inode, attr->la_size);
1285         }
1286 # if 0
1287         /*
1288          * OSD should not change "i_blocks" which is used by quota.
1289          * "i_blocks" should be changed by ldiskfs only.
1290          * Disable this assignment until SOM to fix some EA field. */
1291         if (bits & LA_BLOCKS)
1292                 inode->i_blocks = attr->la_blocks;
1293 #endif
1294         if (bits & LA_MODE)
1295                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1296                         (attr->la_mode & ~S_IFMT);
1297         if (bits & LA_UID)
1298                 inode->i_uid    = attr->la_uid;
1299         if (bits & LA_GID)
1300                 inode->i_gid    = attr->la_gid;
1301         if (bits & LA_NLINK)
1302                 inode->i_nlink  = attr->la_nlink;
1303         if (bits & LA_RDEV)
1304                 inode->i_rdev   = attr->la_rdev;
1305
1306         if (bits & LA_FLAGS) {
1307                 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1308
1309                 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1310                         (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1311         }
1312         return 0;
1313 }
1314
1315 /*
1316  * Object creation.
1317  *
1318  * XXX temporary solution.
1319  */
1320
1321 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1322                           struct lu_attr *attr, struct thandle *th)
1323 {
1324         return 0;
1325 }
1326
1327 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1328                            struct lu_attr *attr, struct thandle *th)
1329 {
1330         LASSERT(obj->oo_inode != NULL);
1331
1332         osd_object_init0(obj);
1333         return 0;
1334 }
1335
1336 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1337                                           struct inode * dir, int mode);
1338 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1339                              struct inode *inode);
1340 extern int ldiskfs_delete_entry(handle_t *handle,
1341                                 struct inode * dir,
1342                                 struct ldiskfs_dir_entry_2 * de_del,
1343                                 struct buffer_head * bh);
1344 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1345                                                struct ldiskfs_dir_entry_2
1346                                                ** res_dir);
1347 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1348                                   struct inode *inode);
1349
1350 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1351                                     int name_index, const char *name,
1352                                     const void *value, size_t value_len,
1353                                     int flags);
1354
1355 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1356                                             struct osd_object *obj,
1357                                             const char *name,
1358                                             const int namelen)
1359 {
1360         struct osd_thread_info *info   = osd_oti_get(env);
1361         struct dentry *child_dentry = &info->oti_child_dentry;
1362         struct dentry *obj_dentry = &info->oti_obj_dentry;
1363
1364         obj_dentry->d_inode = obj->oo_inode;
1365         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1366         obj_dentry->d_name.hash = 0;
1367
1368         child_dentry->d_name.hash = 0;
1369         child_dentry->d_parent = obj_dentry;
1370         child_dentry->d_name.name = name;
1371         child_dentry->d_name.len = namelen;
1372         return child_dentry;
1373 }
1374
1375
1376 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1377                       umode_t mode,
1378                       struct dt_allocation_hint *hint,
1379                       struct thandle *th)
1380 {
1381         int result;
1382         struct osd_device  *osd = osd_obj2dev(obj);
1383         struct osd_thandle *oth;
1384         struct dt_object   *parent;
1385         struct inode       *inode;
1386 #ifdef HAVE_QUOTA_SUPPORT
1387         struct osd_ctxt    *save = &info->oti_ctxt;
1388 #endif
1389
1390         LINVRNT(osd_invariant(obj));
1391         LASSERT(obj->oo_inode == NULL);
1392
1393         oth = container_of(th, struct osd_thandle, ot_super);
1394         LASSERT(oth->ot_handle->h_transaction != NULL);
1395
1396         if (hint && hint->dah_parent)
1397                 parent = hint->dah_parent;
1398         else
1399                 parent = osd->od_obj_area;
1400
1401         LASSERT(parent != NULL);
1402         LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1403
1404 #ifdef HAVE_QUOTA_SUPPORT
1405         osd_push_ctxt(info->oti_env, save);
1406 #endif
1407         inode = ldiskfs_create_inode(oth->ot_handle,
1408                                      osd_dt_obj(parent)->oo_inode, mode);
1409 #ifdef HAVE_QUOTA_SUPPORT
1410         osd_pop_ctxt(save);
1411 #endif
1412         if (!IS_ERR(inode)) {
1413                 obj->oo_inode = inode;
1414                 result = 0;
1415         } else
1416                 result = PTR_ERR(inode);
1417         LINVRNT(osd_invariant(obj));
1418         return result;
1419 }
1420
1421
1422 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1423                            int recsize, handle_t *handle);
1424
1425 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1426                            int recsize, handle_t *handle);
1427
1428
1429 enum {
1430         OSD_NAME_LEN = 255
1431 };
1432
1433 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1434                      struct lu_attr *attr,
1435                      struct dt_allocation_hint *hint,
1436                      struct dt_object_format *dof,
1437                      struct thandle *th)
1438 {
1439         int result;
1440         struct osd_thandle *oth;
1441         struct osd_device *osd = osd_obj2dev(obj);
1442         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1443
1444         LASSERT(S_ISDIR(attr->la_mode));
1445
1446         oth = container_of(th, struct osd_thandle, ot_super);
1447         LASSERT(oth->ot_handle->h_transaction != NULL);
1448         result = osd_mkfile(info, obj, mode, hint, th);
1449         if (result == 0 && osd->od_iop_mode == 0) {
1450                 LASSERT(obj->oo_inode != NULL);
1451                 /*
1452                  * XXX uh-oh... call low-level iam function directly.
1453                  */
1454
1455                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1456                                          sizeof (struct lu_fid_pack),
1457                                          oth->ot_handle);
1458         }
1459         return result;
1460 }
1461
1462 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1463                         struct lu_attr *attr,
1464                         struct dt_allocation_hint *hint,
1465                         struct dt_object_format *dof,
1466                         struct thandle *th)
1467 {
1468         int result;
1469         struct osd_thandle *oth;
1470         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1471
1472         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1473
1474         LASSERT(S_ISREG(attr->la_mode));
1475
1476         oth = container_of(th, struct osd_thandle, ot_super);
1477         LASSERT(oth->ot_handle->h_transaction != NULL);
1478
1479         result = osd_mkfile(info, obj, mode, hint, th);
1480         if (result == 0) {
1481                 LASSERT(obj->oo_inode != NULL);
1482                 if (feat->dif_flags & DT_IND_VARKEY)
1483                         result = iam_lvar_create(obj->oo_inode,
1484                                                  feat->dif_keysize_max,
1485                                                  feat->dif_ptrsize,
1486                                                  feat->dif_recsize_max,
1487                                                  oth->ot_handle);
1488                 else
1489                         result = iam_lfix_create(obj->oo_inode,
1490                                                  feat->dif_keysize_max,
1491                                                  feat->dif_ptrsize,
1492                                                  feat->dif_recsize_max,
1493                                                  oth->ot_handle);
1494
1495         }
1496         return result;
1497 }
1498
1499 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1500                      struct lu_attr *attr,
1501                      struct dt_allocation_hint *hint,
1502                      struct dt_object_format *dof,
1503                      struct thandle *th)
1504 {
1505         LASSERT(S_ISREG(attr->la_mode));
1506         return osd_mkfile(info, obj, (attr->la_mode &
1507                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1508 }
1509
1510 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1511                      struct lu_attr *attr,
1512                      struct dt_allocation_hint *hint,
1513                      struct dt_object_format *dof,
1514                      struct thandle *th)
1515 {
1516         LASSERT(S_ISLNK(attr->la_mode));
1517         return osd_mkfile(info, obj, (attr->la_mode &
1518                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1519 }
1520
1521 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1522                      struct lu_attr *attr,
1523                      struct dt_allocation_hint *hint,
1524                      struct dt_object_format *dof,
1525                      struct thandle *th)
1526 {
1527         umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1528         int result;
1529
1530         LINVRNT(osd_invariant(obj));
1531         LASSERT(obj->oo_inode == NULL);
1532         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1533                 S_ISFIFO(mode) || S_ISSOCK(mode));
1534
1535         result = osd_mkfile(info, obj, mode, hint, th);
1536         if (result == 0) {
1537                 LASSERT(obj->oo_inode != NULL);
1538                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1539         }
1540         LINVRNT(osd_invariant(obj));
1541         return result;
1542 }
1543
1544 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1545                               struct lu_attr *,
1546                               struct dt_allocation_hint *hint,
1547                               struct dt_object_format *dof,
1548                               struct thandle *);
1549
1550 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1551 {
1552         osd_obj_type_f result;
1553
1554         switch (type) {
1555         case DFT_DIR:
1556                 result = osd_mkdir;
1557                 break;
1558         case DFT_REGULAR:
1559                 result = osd_mkreg;
1560                 break;
1561         case DFT_SYM:
1562                 result = osd_mksym;
1563                 break;
1564         case DFT_NODE:
1565                 result = osd_mknod;
1566                 break;
1567         case DFT_INDEX:
1568                 result = osd_mk_index;
1569                 break;
1570
1571         default:
1572                 LBUG();
1573                 break;
1574         }
1575         return result;
1576 }
1577
1578
1579 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1580                         struct dt_object *parent, umode_t child_mode)
1581 {
1582         LASSERT(ah);
1583
1584         memset(ah, 0, sizeof(*ah));
1585         ah->dah_parent = parent;
1586         ah->dah_mode = child_mode;
1587 }
1588
1589 /**
1590  * Helper function for osd_object_create()
1591  *
1592  * \retval 0, on success
1593  */
1594 static int __osd_object_create(struct osd_thread_info *info,
1595                                struct osd_object *obj, struct lu_attr *attr,
1596                                struct dt_allocation_hint *hint,
1597                                struct dt_object_format *dof,
1598                                struct thandle *th)
1599 {
1600
1601         int result;
1602
1603         result = osd_create_pre(info, obj, attr, th);
1604         if (result == 0) {
1605                 result = osd_create_type_f(dof->dof_type)(info, obj,
1606                                            attr, hint, dof, th);
1607                 if (result == 0)
1608                         result = osd_create_post(info, obj, attr, th);
1609         }
1610         return result;
1611 }
1612
1613 /**
1614  * Helper function for osd_object_create()
1615  *
1616  * \retval 0, on success
1617  */
1618 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1619                            const struct lu_fid *fid, struct thandle *th)
1620 {
1621         struct osd_thread_info *info = osd_oti_get(env);
1622         struct osd_inode_id    *id   = &info->oti_id;
1623         struct osd_device      *osd  = osd_obj2dev(obj);
1624         struct md_ucred        *uc   = md_ucred(env);
1625
1626         LASSERT(obj->oo_inode != NULL);
1627         LASSERT(uc != NULL);
1628
1629         id->oii_ino = obj->oo_inode->i_ino;
1630         id->oii_gen = obj->oo_inode->i_generation;
1631
1632         return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1633                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1634 }
1635
1636 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1637                              struct lu_attr *attr,
1638                              struct dt_allocation_hint *hint,
1639                              struct dt_object_format *dof,
1640                              struct thandle *th)
1641 {
1642         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1643         struct osd_object      *obj    = osd_dt_obj(dt);
1644         struct osd_thread_info *info   = osd_oti_get(env);
1645         int result;
1646
1647         ENTRY;
1648
1649         LINVRNT(osd_invariant(obj));
1650         LASSERT(!dt_object_exists(dt));
1651         LASSERT(osd_write_locked(env, obj));
1652         LASSERT(th != NULL);
1653
1654         result = __osd_object_create(info, obj, attr, hint, dof, th);
1655         if (result == 0)
1656                 result = __osd_oi_insert(env, obj, fid, th);
1657
1658         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1659         LASSERT(osd_invariant(obj));
1660         RETURN(result);
1661 }
1662
1663 /**
1664  * Helper function for osd_xattr_set()
1665  */
1666 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1667                            const struct lu_buf *buf, const char *name, int fl)
1668 {
1669         struct osd_object      *obj      = osd_dt_obj(dt);
1670         struct inode           *inode    = obj->oo_inode;
1671         struct osd_thread_info *info     = osd_oti_get(env);
1672         struct dentry          *dentry   = &info->oti_child_dentry;
1673         struct timespec        *t        = &info->oti_time;
1674         int                     fs_flags = 0;
1675         int  rc;
1676
1677         LASSERT(dt_object_exists(dt));
1678         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1679         LASSERT(osd_write_locked(env, obj));
1680
1681         if (fl & LU_XATTR_REPLACE)
1682                 fs_flags |= XATTR_REPLACE;
1683
1684         if (fl & LU_XATTR_CREATE)
1685                 fs_flags |= XATTR_CREATE;
1686
1687         dentry->d_inode = inode;
1688         *t = inode->i_ctime;
1689         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1690                                    buf->lb_len, fs_flags);
1691         if (likely(rc == 0)) {
1692                 spin_lock(&obj->oo_guard);
1693                 inode->i_ctime = *t;
1694                 spin_unlock(&obj->oo_guard);
1695                 mark_inode_dirty(inode);
1696         }
1697         return rc;
1698 }
1699
1700 /**
1701  * Put the fid into lustre_mdt_attrs, and then place the structure
1702  * inode's ea. This fid should not be altered during the life time
1703  * of the inode.
1704  *
1705  * \retval +ve, on success
1706  * \retval -ve, on error
1707  *
1708  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1709  */
1710 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1711                           const struct lu_fid *fid)
1712 {
1713         struct osd_thread_info  *info      = osd_oti_get(env);
1714         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1715
1716         fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1717
1718         return __osd_xattr_set(env, dt,
1719                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1720                                XATTR_NAME_LMA, LU_XATTR_CREATE);
1721
1722 }
1723
1724 /**
1725  * Helper function to form igif
1726  */
1727 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1728                                 struct lu_fid *fid)
1729 {
1730         struct inode  *inode = dentry->d_inode;
1731         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1732 }
1733
1734 /**
1735  * Helper function to pack the fid
1736  */
1737 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1738                                 struct lu_fid_pack *pack)
1739 {
1740         fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1741 }
1742
1743 /**
1744  * Try to read the fid from inode ea into dt_rec, if return value
1745  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1746  *
1747  * \param rec, the data-structure into which fid/igif is read
1748  *
1749  * \retval 0, on success
1750  */
1751 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1752                           struct dt_rec *rec)
1753 {
1754         struct inode            *inode     = dentry->d_inode;
1755         struct osd_thread_info  *info      = osd_oti_get(env);
1756         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1757         struct lu_fid           *fid       = &info->oti_fid;
1758         int rc;
1759
1760         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1761
1762         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1763                                    sizeof *mdt_attrs);
1764
1765         if (rc > 0) {
1766                 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1767                 rc = 0;
1768         } else if (rc == -ENODATA) {
1769                 osd_igif_get(env, dentry, fid);
1770                 rc = 0;
1771         }
1772
1773         if (rc == 0)
1774                 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1775
1776         return rc;
1777 }
1778
1779 /**
1780  * OSD layer object create function for interoperability mode (b11826).
1781  * This is mostly similar to osd_object_create(). Only difference being, fid is
1782  * inserted into inode ea here.
1783  *
1784  * \retval   0, on success
1785  * \retval -ve, on error
1786  */
1787 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1788                              struct lu_attr *attr,
1789                              struct dt_allocation_hint *hint,
1790                              struct dt_object_format *dof,
1791                              struct thandle *th)
1792 {
1793         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1794         struct osd_object      *obj    = osd_dt_obj(dt);
1795         struct osd_thread_info *info   = osd_oti_get(env);
1796         int result;
1797         int is_root = 0;
1798
1799         ENTRY;
1800
1801         LASSERT(osd_invariant(obj));
1802         LASSERT(!dt_object_exists(dt));
1803         LASSERT(osd_write_locked(env, obj));
1804         LASSERT(th != NULL);
1805
1806         result = __osd_object_create(info, obj, attr, hint, dof, th);
1807
1808         if (hint && hint->dah_parent)
1809                 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1810
1811         /* objects under osd root shld have igif fid, so dont add fid EA */
1812         if (result == 0 && is_root == 0)
1813                 result = osd_ea_fid_set(env, dt, fid);
1814
1815         if (result == 0)
1816                 result = __osd_oi_insert(env, obj, fid, th);
1817
1818         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1819         LINVRNT(osd_invariant(obj));
1820         RETURN(result);
1821 }
1822
1823 /*
1824  * Concurrency: @dt is write locked.
1825  */
1826 static void osd_object_ref_add(const struct lu_env *env,
1827                                struct dt_object *dt,
1828                                struct thandle *th)
1829 {
1830         struct osd_object *obj = osd_dt_obj(dt);
1831         struct inode *inode = obj->oo_inode;
1832
1833         LINVRNT(osd_invariant(obj));
1834         LASSERT(dt_object_exists(dt));
1835         LASSERT(osd_write_locked(env, obj));
1836         LASSERT(th != NULL);
1837
1838         spin_lock(&obj->oo_guard);
1839         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1840         inode->i_nlink++;
1841         spin_unlock(&obj->oo_guard);
1842         mark_inode_dirty(inode);
1843         LINVRNT(osd_invariant(obj));
1844 }
1845
1846 /*
1847  * Concurrency: @dt is write locked.
1848  */
1849 static void osd_object_ref_del(const struct lu_env *env,
1850                                struct dt_object *dt,
1851                                struct thandle *th)
1852 {
1853         struct osd_object *obj = osd_dt_obj(dt);
1854         struct inode *inode = obj->oo_inode;
1855
1856         LINVRNT(osd_invariant(obj));
1857         LASSERT(dt_object_exists(dt));
1858         LASSERT(osd_write_locked(env, obj));
1859         LASSERT(th != NULL);
1860
1861         spin_lock(&obj->oo_guard);
1862         LASSERT(inode->i_nlink > 0);
1863         inode->i_nlink--;
1864         spin_unlock(&obj->oo_guard);
1865         mark_inode_dirty(inode);
1866         LINVRNT(osd_invariant(obj));
1867 }
1868
1869 /*
1870  * Concurrency: @dt is read locked.
1871  */
1872 static int osd_xattr_get(const struct lu_env *env,
1873                          struct dt_object *dt,
1874                          struct lu_buf *buf,
1875                          const char *name,
1876                          struct lustre_capa *capa)
1877 {
1878         struct osd_object      *obj    = osd_dt_obj(dt);
1879         struct inode           *inode  = obj->oo_inode;
1880         struct osd_thread_info *info   = osd_oti_get(env);
1881         struct dentry          *dentry = &info->oti_obj_dentry;
1882
1883         LASSERT(dt_object_exists(dt));
1884         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1885         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1886
1887         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1888                 return -EACCES;
1889
1890         dentry->d_inode = inode;
1891         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1892 }
1893
1894
1895 /*
1896  * Concurrency: @dt is write locked.
1897  */
1898 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1899                          const struct lu_buf *buf, const char *name, int fl,
1900                          struct thandle *handle, struct lustre_capa *capa)
1901 {
1902         LASSERT(handle != NULL);
1903
1904         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1905                 return -EACCES;
1906
1907         return __osd_xattr_set(env, dt, buf, name, fl);
1908 }
1909
1910 /*
1911  * Concurrency: @dt is read locked.
1912  */
1913 static int osd_xattr_list(const struct lu_env *env,
1914                           struct dt_object *dt,
1915                           struct lu_buf *buf,
1916                           struct lustre_capa *capa)
1917 {
1918         struct osd_object      *obj    = osd_dt_obj(dt);
1919         struct inode           *inode  = obj->oo_inode;
1920         struct osd_thread_info *info   = osd_oti_get(env);
1921         struct dentry          *dentry = &info->oti_obj_dentry;
1922
1923         LASSERT(dt_object_exists(dt));
1924         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1925         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1926
1927         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1928                 return -EACCES;
1929
1930         dentry->d_inode = inode;
1931         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1932 }
1933
1934 /*
1935  * Concurrency: @dt is write locked.
1936  */
1937 static int osd_xattr_del(const struct lu_env *env,
1938                          struct dt_object *dt,
1939                          const char *name,
1940                          struct thandle *handle,
1941                          struct lustre_capa *capa)
1942 {
1943         struct osd_object      *obj    = osd_dt_obj(dt);
1944         struct inode           *inode  = obj->oo_inode;
1945         struct osd_thread_info *info   = osd_oti_get(env);
1946         struct dentry          *dentry = &info->oti_obj_dentry;
1947         struct timespec        *t      = &info->oti_time;
1948         int                     rc;
1949
1950         LASSERT(dt_object_exists(dt));
1951         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1952         LASSERT(osd_write_locked(env, obj));
1953         LASSERT(handle != NULL);
1954
1955         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1956                 return -EACCES;
1957
1958         dentry->d_inode = inode;
1959         *t = inode->i_ctime;
1960         rc = inode->i_op->removexattr(dentry, name);
1961         if (likely(rc == 0)) {
1962                 /* ctime should not be updated with server-side time. */
1963                 spin_lock(&obj->oo_guard);
1964                 inode->i_ctime = *t;
1965                 spin_unlock(&obj->oo_guard);
1966                 mark_inode_dirty(inode);
1967         }
1968         return rc;
1969 }
1970
1971 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1972                                      struct dt_object *dt,
1973                                      struct lustre_capa *old,
1974                                      __u64 opc)
1975 {
1976         struct osd_thread_info *info = osd_oti_get(env);
1977         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1978         struct osd_object *obj = osd_dt_obj(dt);
1979         struct osd_device *dev = osd_obj2dev(obj);
1980         struct lustre_capa_key *key = &info->oti_capa_key;
1981         struct lustre_capa *capa = &info->oti_capa;
1982         struct obd_capa *oc;
1983         struct md_capainfo *ci;
1984         int rc;
1985         ENTRY;
1986
1987         if (!dev->od_fl_capa)
1988                 RETURN(ERR_PTR(-ENOENT));
1989
1990         LASSERT(dt_object_exists(dt));
1991         LINVRNT(osd_invariant(obj));
1992
1993         /* renewal sanity check */
1994         if (old && osd_object_auth(env, dt, old, opc))
1995                 RETURN(ERR_PTR(-EACCES));
1996
1997         ci = md_capainfo(env);
1998         if (unlikely(!ci))
1999                 RETURN(ERR_PTR(-ENOENT));
2000
2001         switch (ci->mc_auth) {
2002         case LC_ID_NONE:
2003                 RETURN(NULL);
2004         case LC_ID_PLAIN:
2005                 capa->lc_uid = obj->oo_inode->i_uid;
2006                 capa->lc_gid = obj->oo_inode->i_gid;
2007                 capa->lc_flags = LC_ID_PLAIN;
2008                 break;
2009         case LC_ID_CONVERT: {
2010                 __u32 d[4], s[4];
2011
2012                 s[0] = obj->oo_inode->i_uid;
2013                 get_random_bytes(&(s[1]), sizeof(__u32));
2014                 s[2] = obj->oo_inode->i_gid;
2015                 get_random_bytes(&(s[3]), sizeof(__u32));
2016                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2017                 if (unlikely(rc))
2018                         RETURN(ERR_PTR(rc));
2019
2020                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2021                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2022                 capa->lc_flags = LC_ID_CONVERT;
2023                 break;
2024         }
2025         default:
2026                 RETURN(ERR_PTR(-EINVAL));
2027         }
2028
2029         capa->lc_fid = *fid;
2030         capa->lc_opc = opc;
2031         capa->lc_flags |= dev->od_capa_alg << 24;
2032         capa->lc_timeout = dev->od_capa_timeout;
2033         capa->lc_expiry = 0;
2034
2035         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2036         if (oc) {
2037                 LASSERT(!capa_is_expired(oc));
2038                 RETURN(oc);
2039         }
2040
2041         spin_lock(&capa_lock);
2042         *key = dev->od_capa_keys[1];
2043         spin_unlock(&capa_lock);
2044
2045         capa->lc_keyid = key->lk_keyid;
2046         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2047
2048         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2049         if (rc) {
2050                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2051                 RETURN(ERR_PTR(rc));
2052         }
2053
2054         oc = capa_add(dev->od_capa_hash, capa);
2055         RETURN(oc);
2056 }
2057
2058 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2059 {
2060         int rc;
2061         struct osd_object      *obj    = osd_dt_obj(dt);
2062         struct inode           *inode  = obj->oo_inode;
2063         struct osd_thread_info *info   = osd_oti_get(env);
2064         struct dentry          *dentry = &info->oti_obj_dentry;
2065         struct file            *file   = &info->oti_file;
2066         ENTRY;
2067
2068         dentry->d_inode = inode;
2069         file->f_dentry = dentry;
2070         file->f_mapping = inode->i_mapping;
2071         file->f_op = inode->i_fop;
2072         LOCK_INODE_MUTEX(inode);
2073         rc = file->f_op->fsync(file, dentry, 0);
2074         UNLOCK_INODE_MUTEX(inode);
2075         RETURN(rc);
2076 }
2077
2078 static const struct dt_object_operations osd_obj_ops = {
2079         .do_read_lock    = osd_object_read_lock,
2080         .do_write_lock   = osd_object_write_lock,
2081         .do_read_unlock  = osd_object_read_unlock,
2082         .do_write_unlock = osd_object_write_unlock,
2083         .do_attr_get     = osd_attr_get,
2084         .do_attr_set     = osd_attr_set,
2085         .do_ah_init      = osd_ah_init,
2086         .do_create       = osd_object_create,
2087         .do_index_try    = osd_index_try,
2088         .do_ref_add      = osd_object_ref_add,
2089         .do_ref_del      = osd_object_ref_del,
2090         .do_xattr_get    = osd_xattr_get,
2091         .do_xattr_set    = osd_xattr_set,
2092         .do_xattr_del    = osd_xattr_del,
2093         .do_xattr_list   = osd_xattr_list,
2094         .do_capa_get     = osd_capa_get,
2095         .do_object_sync  = osd_object_sync,
2096 };
2097
2098 /**
2099  * dt_object_operations for interoperability mode
2100  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2101  */
2102 static const struct dt_object_operations osd_obj_ea_ops = {
2103         .do_read_lock    = osd_object_read_lock,
2104         .do_write_lock   = osd_object_write_lock,
2105         .do_read_unlock  = osd_object_read_unlock,
2106         .do_write_unlock = osd_object_write_unlock,
2107         .do_attr_get     = osd_attr_get,
2108         .do_attr_set     = osd_attr_set,
2109         .do_ah_init      = osd_ah_init,
2110         .do_create       = osd_object_ea_create,
2111         .do_index_try    = osd_index_try,
2112         .do_ref_add      = osd_object_ref_add,
2113         .do_ref_del      = osd_object_ref_del,
2114         .do_xattr_get    = osd_xattr_get,
2115         .do_xattr_set    = osd_xattr_set,
2116         .do_xattr_del    = osd_xattr_del,
2117         .do_xattr_list   = osd_xattr_list,
2118         .do_capa_get     = osd_capa_get,
2119         .do_object_sync  = osd_object_sync,
2120 };
2121
2122 /*
2123  * Body operations.
2124  */
2125
2126 /*
2127  * XXX: Another layering violation for now.
2128  *
2129  * We don't want to use ->f_op->read methods, because generic file write
2130  *
2131  *         - serializes on ->i_sem, and
2132  *
2133  *         - does a lot of extra work like balance_dirty_pages(),
2134  *
2135  * which doesn't work for globally shared files like /last-received.
2136  */
2137 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2138 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2139                                 loff_t *offs, handle_t *handle);
2140
2141 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2142                         struct lu_buf *buf, loff_t *pos,
2143                         struct lustre_capa *capa)
2144 {
2145         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2146
2147         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2148                 RETURN(-EACCES);
2149
2150         return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2151 }
2152
2153 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2154                          const struct lu_buf *buf, loff_t *pos,
2155                          struct thandle *handle, struct lustre_capa *capa,
2156                          int ignore_quota)
2157 {
2158         struct inode       *inode = osd_dt_obj(dt)->oo_inode;
2159         struct osd_thandle *oh;
2160         ssize_t             result;
2161 #ifdef HAVE_QUOTA_SUPPORT
2162         cfs_cap_t           save = current->cap_effective;
2163 #endif
2164
2165         LASSERT(handle != NULL);
2166
2167         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2168                 RETURN(-EACCES);
2169
2170         oh = container_of(handle, struct osd_thandle, ot_super);
2171         LASSERT(oh->ot_handle->h_transaction != NULL);
2172 #ifdef HAVE_QUOTA_SUPPORT
2173         if (ignore_quota)
2174                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2175         else
2176                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2177 #endif
2178         result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2179                                              pos, oh->ot_handle);
2180 #ifdef HAVE_QUOTA_SUPPORT
2181         current->cap_effective = save;
2182 #endif
2183         if (result == 0)
2184                 result = buf->lb_len;
2185         return result;
2186 }
2187
2188 static const struct dt_body_operations osd_body_ops = {
2189         .dbo_read  = osd_read,
2190         .dbo_write = osd_write
2191 };
2192
2193 /*
2194  * Index operations.
2195  */
2196
2197 static int osd_object_is_root(const struct osd_object *obj)
2198 {
2199         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2200 }
2201
2202 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2203                            const struct dt_index_features *feat)
2204 {
2205         struct iam_descr *descr;
2206         struct dt_object *dt = &o->oo_dt;
2207
2208         if (osd_object_is_root(o))
2209                 return feat == &dt_directory_features;
2210
2211         LASSERT(o->oo_dir != NULL);
2212
2213         descr = o->oo_dir->od_container.ic_descr;
2214         if (feat == &dt_directory_features) {
2215                 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2216                         return 1;
2217
2218                 if (descr == &iam_htree_compat_param) {
2219                         /* if it is a HTREE dir then there is good chance that,
2220                          * we dealing with ext3 directory here with no FIDs. */
2221
2222                         if (descr->id_rec_size ==
2223                             sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2224
2225                                 dt->do_index_ops = &osd_index_ea_ops;
2226                                 return 1;
2227                         }
2228                 }
2229                 return 0;
2230         } else {
2231                 return
2232                         feat->dif_keysize_min <= descr->id_key_size &&
2233                         descr->id_key_size <= feat->dif_keysize_max &&
2234                         feat->dif_recsize_min <= descr->id_rec_size &&
2235                         descr->id_rec_size <= feat->dif_recsize_max &&
2236                         !(feat->dif_flags & (DT_IND_VARKEY |
2237                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2238                         ergo(feat->dif_flags & DT_IND_UPDATE,
2239                              1 /* XXX check that object (and file system) is
2240                                 * writable */);
2241         }
2242 }
2243
2244 static int osd_iam_container_init(const struct lu_env *env,
2245                                   struct osd_object *obj,
2246                                   struct osd_directory *dir)
2247 {
2248         int result;
2249         struct iam_container *bag;
2250
2251         bag    = &dir->od_container;
2252         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2253         if (result == 0) {
2254                 result = iam_container_setup(bag);
2255                 if (result == 0)
2256                         obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2257                 else
2258                         iam_container_fini(bag);
2259         }
2260         return result;
2261 }
2262
2263 /*
2264  * Concurrency: no external locking is necessary.
2265  */
2266 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2267                          const struct dt_index_features *feat)
2268 {
2269         int result;
2270         int ea_dir = 0;
2271         struct osd_object *obj = osd_dt_obj(dt);
2272         struct osd_device *osd = osd_obj2dev(obj);
2273
2274         LINVRNT(osd_invariant(obj));
2275         LASSERT(dt_object_exists(dt));
2276
2277         if (osd_object_is_root(obj)) {
2278                 dt->do_index_ops = &osd_index_ea_ops;
2279                 result = 0;
2280         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2281                 dt->do_index_ops = &osd_index_ea_ops;
2282                 if (S_ISDIR(obj->oo_inode->i_mode))
2283                         result = 0;
2284                 else
2285                         result = -ENOTDIR;
2286                 ea_dir = 1;
2287         } else if (!osd_has_index(obj)) {
2288                 struct osd_directory *dir;
2289
2290                 OBD_ALLOC_PTR(dir);
2291                 if (dir != NULL) {
2292                         sema_init(&dir->od_sem, 1);
2293
2294                         spin_lock(&obj->oo_guard);
2295                         if (obj->oo_dir == NULL)
2296                                 obj->oo_dir = dir;
2297                         else
2298                                 /*
2299                                  * Concurrent thread allocated container data.
2300                                  */
2301                                 OBD_FREE_PTR(dir);
2302                         spin_unlock(&obj->oo_guard);
2303                         /*
2304                          * Now, that we have container data, serialize its
2305                          * initialization.
2306                          */
2307                         down(&obj->oo_dir->od_sem);
2308                         /*
2309                          * recheck under lock.
2310                          */
2311                         if (!osd_has_index(obj))
2312                                 result = osd_iam_container_init(env, obj, dir);
2313                         else
2314                                 result = 0;
2315                         up(&obj->oo_dir->od_sem);
2316                 } else
2317                         result = -ENOMEM;
2318         } else
2319                 result = 0;
2320
2321         if (result == 0 && ea_dir == 0) {
2322                 if (!osd_iam_index_probe(env, obj, feat))
2323                         result = -ENOTDIR;
2324         }
2325         LINVRNT(osd_invariant(obj));
2326
2327         return result;
2328 }
2329
2330 /**
2331  *      delete a (key, value) pair from index \a dt specified by \a key
2332  *
2333  *      \param  dt_object      osd index object
2334  *      \param  key     key for index
2335  *      \param  rec     record reference
2336  *      \param  handle  transaction handler
2337  *
2338  *      \retval  0  success
2339  *      \retval -ve   failure
2340  */
2341
2342 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2343                                 const struct dt_key *key, struct thandle *handle,
2344                                 struct lustre_capa *capa)
2345 {
2346         struct osd_object     *obj = osd_dt_obj(dt);
2347         struct osd_thandle    *oh;
2348         struct iam_path_descr *ipd;
2349         struct iam_container  *bag = &obj->oo_dir->od_container;
2350         int rc;
2351
2352         ENTRY;
2353
2354         LINVRNT(osd_invariant(obj));
2355         LASSERT(dt_object_exists(dt));
2356         LASSERT(bag->ic_object == obj->oo_inode);
2357         LASSERT(handle != NULL);
2358
2359         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2360                 RETURN(-EACCES);
2361
2362         ipd = osd_idx_ipd_get(env, bag);
2363         if (unlikely(ipd == NULL))
2364                 RETURN(-ENOMEM);
2365
2366         oh = container_of0(handle, struct osd_thandle, ot_super);
2367         LASSERT(oh->ot_handle != NULL);
2368         LASSERT(oh->ot_handle->h_transaction != NULL);
2369
2370         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2371         osd_ipd_put(env, bag, ipd);
2372         LINVRNT(osd_invariant(obj));
2373         RETURN(rc);
2374 }
2375
2376 /**
2377  * Index delete function for interoperability mode (b11826).
2378  * It will remove the directory entry added by osd_index_ea_insert().
2379  * This entry is needed to maintain name->fid mapping.
2380  *
2381  * \param key,  key i.e. file entry to be deleted
2382  *
2383  * \retval   0, on success
2384  * \retval -ve, on error
2385  */
2386 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2387                                const struct dt_key *key, struct thandle *handle,
2388                                struct lustre_capa *capa)
2389 {
2390         struct osd_object          *obj    = osd_dt_obj(dt);
2391         struct inode               *dir    = obj->oo_inode;
2392         struct dentry              *dentry;
2393         struct osd_thandle         *oh;
2394         struct ldiskfs_dir_entry_2 *de;
2395         struct buffer_head         *bh;
2396
2397         int rc;
2398
2399         ENTRY;
2400
2401         LINVRNT(osd_invariant(obj));
2402         LASSERT(dt_object_exists(dt));
2403         LASSERT(handle != NULL);
2404
2405         oh = container_of(handle, struct osd_thandle, ot_super);
2406         LASSERT(oh->ot_handle != NULL);
2407         LASSERT(oh->ot_handle->h_transaction != NULL);
2408
2409         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2410                 RETURN(-EACCES);
2411
2412         dentry = osd_child_dentry_get(env, obj,
2413                                       (char *)key, strlen((char *)key));
2414         bh = ldiskfs_find_entry(dentry, &de);
2415         if (bh) {
2416                 rc = ldiskfs_delete_entry(oh->ot_handle,
2417                                 dir, de, bh);
2418                 if (!rc)
2419                         mark_inode_dirty(dir);
2420                 brelse(bh);
2421         } else
2422                 rc = -ENOENT;
2423
2424         LASSERT(osd_invariant(obj));
2425         RETURN(rc);
2426 }
2427
2428 /**
2429  *      Lookup index for \a key and copy record to \a rec.
2430  *
2431  *      \param  dt_object      osd index object
2432  *      \param  key     key for index
2433  *      \param  rec     record reference
2434  *
2435  *      \retval  +ve  success : exact mach
2436  *      \retval  0    return record with key not greater than \a key
2437  *      \retval -ve   failure
2438  */
2439 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2440                                 struct dt_rec *rec, const struct dt_key *key,
2441                                 struct lustre_capa *capa)
2442 {
2443         struct osd_object     *obj = osd_dt_obj(dt);
2444         struct iam_path_descr *ipd;
2445         struct iam_container  *bag = &obj->oo_dir->od_container;
2446         struct osd_thread_info *oti = osd_oti_get(env);
2447         struct iam_iterator    *it = &oti->oti_idx_it;
2448         int rc;
2449         ENTRY;
2450
2451         LASSERT(osd_invariant(obj));
2452         LASSERT(dt_object_exists(dt));
2453         LASSERT(bag->ic_object == obj->oo_inode);
2454
2455         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2456                 RETURN(-EACCES);
2457
2458         ipd = osd_idx_ipd_get(env, bag);
2459         if (IS_ERR(ipd))
2460                 RETURN(-ENOMEM);
2461
2462         /* got ipd now we can start iterator. */
2463         iam_it_init(it, bag, 0, ipd);
2464
2465         rc = iam_it_get(it, (struct iam_key *)key);
2466         if (rc >= 0)
2467                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2468
2469         iam_it_put(it);
2470         iam_it_fini(it);
2471         osd_ipd_put(env, bag, ipd);
2472
2473         LINVRNT(osd_invariant(obj));
2474
2475         RETURN(rc);
2476 }
2477
2478 /**
2479  *      Inserts (key, value) pair in \a dt index object.
2480  *
2481  *      \param  dt      osd index object
2482  *      \param  key     key for index
2483  *      \param  rec     record reference
2484  *      \param  th      transaction handler
2485  *
2486  *      \retval  0  success
2487  *      \retval -ve failure
2488  */
2489 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2490                                 const struct dt_rec *rec, const struct dt_key *key,
2491                                 struct thandle *th, struct lustre_capa *capa,
2492                                 int ignore_quota)
2493 {
2494         struct osd_object     *obj = osd_dt_obj(dt);
2495         struct iam_path_descr *ipd;
2496         struct osd_thandle    *oh;
2497         struct iam_container  *bag = &obj->oo_dir->od_container;
2498 #ifdef HAVE_QUOTA_SUPPORT
2499         cfs_cap_t              save = current->cap_effective;
2500 #endif
2501         int rc;
2502
2503         ENTRY;
2504
2505         LINVRNT(osd_invariant(obj));
2506         LASSERT(dt_object_exists(dt));
2507         LASSERT(bag->ic_object == obj->oo_inode);
2508         LASSERT(th != NULL);
2509
2510         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2511                 return -EACCES;
2512
2513         ipd = osd_idx_ipd_get(env, bag);
2514         if (unlikely(ipd == NULL))
2515                 RETURN(-ENOMEM);
2516
2517         oh = container_of0(th, struct osd_thandle, ot_super);
2518         LASSERT(oh->ot_handle != NULL);
2519         LASSERT(oh->ot_handle->h_transaction != NULL);
2520 #ifdef HAVE_QUOTA_SUPPORT
2521         if (ignore_quota)
2522                 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2523         else
2524                 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2525 #endif
2526         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2527                         (struct iam_rec *)rec, ipd);
2528 #ifdef HAVE_QUOTA_SUPPORT
2529         current->cap_effective = save;
2530 #endif
2531         osd_ipd_put(env, bag, ipd);
2532         LINVRNT(osd_invariant(obj));
2533         RETURN(rc);
2534 }
2535
2536 /**
2537  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2538  * into the directory.Also sets flags into osd object to
2539  * indicate dot and dotdot are created. This is required for
2540  * interoperability mode (b11826)
2541  *
2542  * \param dir   directory for dot and dotdot fixup.
2543  * \param obj   child object for linking
2544  *
2545  * \retval   0, on success
2546  * \retval -ve, on error
2547  */
2548 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2549                               struct osd_object *dir,
2550                               struct osd_object *obj, const char *name,
2551                               struct thandle *th)
2552 {
2553         struct inode            *parent_dir   = obj->oo_inode;
2554         struct inode            *inode  = dir->oo_inode;
2555         struct osd_thandle      *oth;
2556         int result = 0;
2557
2558         oth = container_of(th, struct osd_thandle, ot_super);
2559         LASSERT(oth->ot_handle->h_transaction != NULL);
2560         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2561
2562         if (strcmp(name, dot) == 0) {
2563                 if (dir->oo_compat_dot_created) {
2564                         result = -EEXIST;
2565                 } else {
2566                         LASSERT(obj == dir);
2567                         dir->oo_compat_dot_created = 1;
2568                         result = 0;
2569                 }
2570         } else if(strcmp(name, dotdot) == 0) {
2571                 if (!dir->oo_compat_dot_created)
2572                         return -EINVAL;
2573                 if (dir->oo_compat_dotdot_created)
2574                         return __osd_ea_add_rec(info, dir, obj, name, th);
2575
2576                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2577                 if (result == 0)
2578                        dir->oo_compat_dotdot_created = 1;
2579         }
2580
2581         return result;
2582 }
2583
2584 /**
2585  * Calls ldiskfs_add_entry() to add directory entry
2586  * into the directory. This is required for
2587  * interoperability mode (b11826)
2588  *
2589  * \retval   0, on success
2590  * \retval -ve, on error
2591  */
2592 static int __osd_ea_add_rec(struct osd_thread_info *info,
2593                             struct osd_object *pobj,
2594                             struct osd_object *cobj,
2595                             const char *name,
2596                             struct thandle *th)
2597 {
2598         struct dentry      *child;
2599         struct osd_thandle *oth;
2600         struct inode       *cinode  = cobj->oo_inode;
2601         int rc;
2602
2603         oth = container_of(th, struct osd_thandle, ot_super);
2604         LASSERT(oth->ot_handle != NULL);
2605         LASSERT(oth->ot_handle->h_transaction != NULL);
2606
2607         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2608         rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2609
2610         RETURN(rc);
2611 }
2612
2613 /**
2614  * It will call the appropriate osd_add* function and return the
2615  * value, return by respective functions.
2616  */
2617 static int osd_ea_add_rec(const struct lu_env *env,
2618                           struct osd_object *pobj,
2619                           struct osd_object *cobj,
2620                           const char *name,
2621                           struct thandle *th)
2622 {
2623         struct osd_thread_info    *info   = osd_oti_get(env);
2624         int rc;
2625
2626         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2627                                                    name[2] =='\0')))
2628                 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2629         else
2630                 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2631
2632         return rc;
2633 }
2634
2635 /**
2636  * Calls ->lookup() to find dentry. From dentry get inode and
2637  * read inode's ea to get fid. This is required for  interoperability
2638  * mode (b11826)
2639  *
2640  * \retval   0, on success
2641  * \retval -ve, on error
2642  */
2643 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2644                              struct dt_rec *rec, const struct dt_key *key)
2645 {
2646         struct inode            *dir    = obj->oo_inode;
2647         struct osd_thread_info  *info   = osd_oti_get(env);
2648         struct dentry           *dentry;
2649         struct osd_device      *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2650         struct osd_inode_id    *id     = &info->oti_id;
2651         struct ldiskfs_dir_entry_2 *de;
2652         struct buffer_head         *bh;
2653         struct inode *inode;
2654         int ino;
2655         int rc;
2656
2657         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2658
2659         dentry = osd_child_dentry_get(env, obj,
2660                                       (char *)key, strlen((char *)key));
2661         bh = ldiskfs_find_entry(dentry, &de);
2662         if (bh) {
2663                 ino = le32_to_cpu(de->inode);
2664                 brelse(bh);
2665                 id->oii_ino = ino;
2666                 id->oii_gen = OSD_OII_NOGEN;
2667
2668                 inode = osd_iget(info, dev, id);
2669                 if (!IS_ERR(inode)) {
2670                         dentry->d_inode = inode;
2671
2672                         rc = osd_ea_fid_get(env, dentry, rec);
2673                         iput(inode);
2674                 } else
2675                         rc = -ENOENT;
2676         } else
2677                 rc = -ENOENT;
2678
2679         RETURN (rc);
2680 }
2681
2682 /**
2683  * Find the osd object for given fid.
2684  *
2685  * \param fid, need to find the osd object having this fid
2686  *
2687  * \retval osd_object, on success
2688  * \retval        -ve, on error
2689  */
2690 struct osd_object *osd_object_find(const struct lu_env *env,
2691                                    struct dt_object *dt,
2692                                    const struct lu_fid *fid)
2693 {
2694         struct lu_device         *ludev = dt->do_lu.lo_dev;
2695         struct osd_object        *child = NULL;
2696         struct lu_object         *luch;
2697         struct lu_object         *lo;
2698
2699         luch = lu_object_find(env, ludev, fid, NULL);
2700         if (!IS_ERR(luch)) {
2701                 if (lu_object_exists(luch)) {
2702                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2703                         if (lo != NULL)
2704                                 child = osd_obj(lo);
2705                         else
2706                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2707                                                 "lu_object can't be located"
2708                                                 ""DFID"\n", PFID(fid));
2709
2710                         if (child == NULL) {
2711                                 lu_object_put(env, luch);
2712                                 CERROR("Unable to get osd_object\n");
2713                                 child = ERR_PTR(-ENOENT);
2714                         }
2715                 } else {
2716                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
2717                                         "lu_object does not exists "DFID"\n",
2718                                         PFID(fid));
2719                         child = ERR_PTR(-ENOENT);
2720                 }
2721         } else
2722                 child = (void *)luch;
2723
2724         return child;
2725 }
2726
2727 /**
2728  * Put the osd object once done with it.
2729  *
2730  * \param obj, osd object that needs to be put
2731  */
2732 static inline void osd_object_put(const struct lu_env *env,
2733                                   struct osd_object *obj)
2734 {
2735         lu_object_put(env, &obj->oo_dt.do_lu);
2736 }
2737
2738 /**
2739  * Index add function for interoperability mode (b11826).
2740  * It will add the directory entry.This entry is needed to
2741  * maintain name->fid mapping.
2742  *
2743  * \param key, it is key i.e. file entry to be inserted
2744  * \param rec, it is value of given key i.e. fid
2745  *
2746  * \retval   0, on success
2747  * \retval -ve, on error
2748  */
2749 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2750                                const struct dt_rec *rec,
2751                                const struct dt_key *key, struct thandle *th,
2752                                struct lustre_capa *capa, int ignore_quota)
2753 {
2754         struct osd_object        *obj   = osd_dt_obj(dt);
2755         struct lu_fid            *fid   = &osd_oti_get(env)->oti_fid;
2756         const struct lu_fid_pack *pack  = (const struct lu_fid_pack *)rec;
2757         const char               *name  = (const char *)key;
2758         struct osd_object        *child;
2759 #ifdef HAVE_QUOTA_SUPPORT
2760         cfs_cap_t              save = current->cap_effective;
2761 #endif
2762         int rc;
2763
2764         ENTRY;
2765
2766         LASSERT(osd_invariant(obj));
2767         LASSERT(dt_object_exists(dt));
2768         LASSERT(th != NULL);
2769
2770         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2771                 RETURN(-EACCES);
2772
2773         rc = fid_unpack(pack, fid);
2774         if (rc != 0)
2775                 RETURN(rc);
2776         child = osd_object_find(env, dt, fid);
2777         if (!IS_ERR(child)) {
2778 #ifdef HAVE_QUOTA_SUPPORT
2779                 if (ignore_quota)
2780                         current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2781                 else
2782                         current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2783 #endif
2784                 rc = osd_ea_add_rec(env, obj, child, name, th);
2785
2786 #ifdef HAVE_QUOTA_SUPPORT
2787                 current->cap_effective = save;
2788 #endif
2789                 osd_object_put(env, child);
2790         } else {
2791                 rc = PTR_ERR(child);
2792         }
2793
2794         LASSERT(osd_invariant(obj));
2795         RETURN(rc);
2796 }
2797
2798 /**
2799  *  Initialize osd Iterator for given osd index object.
2800  *
2801  *  \param  dt      osd index object
2802  */
2803
2804 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2805                                  struct dt_object *dt,
2806                                  struct lustre_capa *capa)
2807 {
2808         struct osd_it_iam         *it;
2809         struct osd_thread_info *oti = osd_oti_get(env);
2810         struct osd_object     *obj = osd_dt_obj(dt);
2811         struct lu_object      *lo  = &dt->do_lu;
2812         struct iam_path_descr *ipd;
2813         struct iam_container  *bag = &obj->oo_dir->od_container;
2814
2815         LASSERT(lu_object_exists(lo));
2816
2817         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2818                 return ERR_PTR(-EACCES);
2819
2820         it = &oti->oti_it;
2821         ipd = osd_it_ipd_get(env, bag);
2822         if (likely(ipd != NULL)) {
2823                 it->oi_obj = obj;
2824                 it->oi_ipd = ipd;
2825                 lu_object_get(lo);
2826                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2827                 return (struct dt_it *)it;
2828         }
2829         return ERR_PTR(-ENOMEM);
2830 }
2831
2832 /**
2833  * free given Iterator.
2834  */
2835
2836 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2837 {
2838         struct osd_it_iam     *it = (struct osd_it_iam *)di;
2839         struct osd_object *obj = it->oi_obj;
2840
2841         iam_it_fini(&it->oi_it);
2842         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2843         lu_object_put(env, &obj->oo_dt.do_lu);
2844 }
2845
2846 /**
2847  *  Move Iterator to record specified by \a key
2848  *
2849  *  \param  di      osd iterator
2850  *  \param  key     key for index
2851  *
2852  *  \retval +ve  di points to record with least key not larger than key
2853  *  \retval  0   di points to exact matched key
2854  *  \retval -ve  failure
2855  */
2856
2857 static int osd_it_iam_get(const struct lu_env *env,
2858                       struct dt_it *di, const struct dt_key *key)
2859 {
2860         struct osd_it_iam *it = (struct osd_it_iam *)di;
2861
2862         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2863 }
2864
2865 /**
2866  *  Release Iterator
2867  *
2868  *  \param  di      osd iterator
2869  */
2870
2871 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2872 {
2873         struct osd_it_iam *it = (struct osd_it_iam *)di;
2874
2875         iam_it_put(&it->oi_it);
2876 }
2877
2878 /**
2879  *  Move iterator by one record
2880  *
2881  *  \param  di      osd iterator
2882  *
2883  *  \retval +1   end of container reached
2884  *  \retval  0   success
2885  *  \retval -ve  failure
2886  */
2887
2888 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2889 {
2890         struct osd_it_iam *it = (struct osd_it_iam *)di;
2891
2892         return iam_it_next(&it->oi_it);
2893 }
2894
2895 /**
2896  * Return pointer to the key under iterator.
2897  */
2898
2899 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2900                                  const struct dt_it *di)
2901 {
2902         struct osd_it_iam *it = (struct osd_it_iam *)di;
2903
2904         return (struct dt_key *)iam_it_key_get(&it->oi_it);
2905 }
2906
2907 /**
2908  * Return size of key under iterator (in bytes)
2909  */
2910
2911 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2912 {
2913         struct osd_it_iam *it = (struct osd_it_iam *)di;
2914
2915         return iam_it_key_size(&it->oi_it);
2916 }
2917
2918 /**
2919  * Return pointer to the record under iterator.
2920  */
2921 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
2922                                  const struct dt_it *di)
2923 {
2924         struct osd_it_iam *it = (struct osd_it_iam *)di;
2925
2926         return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
2927 }
2928
2929 /**
2930  * Returns cookie for current Iterator position.
2931  */
2932 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
2933 {
2934         struct osd_it_iam *it = (struct osd_it_iam *)di;
2935
2936         return iam_it_store(&it->oi_it);
2937 }
2938
2939 /**
2940  * Restore iterator from cookie.
2941  *
2942  * \param  di      osd iterator
2943  * \param  hash    Iterator location cookie
2944  *
2945  * \retval +ve  di points to record with least key not larger than key.
2946  * \retval  0   di points to exact matched key
2947  * \retval -ve  failure
2948  */
2949
2950 static int osd_it_iam_load(const struct lu_env *env,
2951                        const struct dt_it *di, __u64 hash)
2952 {
2953         struct osd_it_iam *it = (struct osd_it_iam *)di;
2954
2955         return iam_it_load(&it->oi_it, hash);
2956 }
2957
2958 static const struct dt_index_operations osd_index_iam_ops = {
2959         .dio_lookup = osd_index_iam_lookup,
2960         .dio_insert = osd_index_iam_insert,
2961         .dio_delete = osd_index_iam_delete,
2962         .dio_it     = {
2963                 .init     = osd_it_iam_init,
2964                 .fini     = osd_it_iam_fini,
2965                 .get      = osd_it_iam_get,
2966                 .put      = osd_it_iam_put,
2967                 .next     = osd_it_iam_next,
2968                 .key      = osd_it_iam_key,
2969                 .key_size = osd_it_iam_key_size,
2970                 .rec      = osd_it_iam_rec,
2971                 .store    = osd_it_iam_store,
2972                 .load     = osd_it_iam_load
2973         }
2974 };
2975
2976 /**
2977  * Creates or initializes iterator context.
2978  *
2979  * \retval struct osd_it_ea, iterator structure on success
2980  *
2981  */
2982 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
2983                                     struct dt_object *dt,
2984                                     struct lustre_capa *capa)
2985 {
2986         struct osd_object       *obj  = osd_dt_obj(dt);
2987         struct osd_thread_info  *info = osd_oti_get(env);
2988         struct osd_it_ea        *it   = &info->oti_it_ea;
2989         struct lu_object        *lo   = &dt->do_lu;
2990         struct dentry           *obj_dentry = &info->oti_it_dentry;
2991         ENTRY;
2992         LASSERT(lu_object_exists(lo));
2993
2994         obj_dentry->d_inode = obj->oo_inode;
2995         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
2996         obj_dentry->d_name.hash = 0;
2997
2998         it->oie_namelen         = 0;
2999         it->oie_curr_pos        = 0;
3000         it->oie_next_pos        = 0;
3001         it->oie_obj             = obj;
3002         it->oie_file.f_dentry   = obj_dentry;
3003         it->oie_file.f_mapping    = obj->oo_inode->i_mapping;
3004         it->oie_file.f_op         = obj->oo_inode->i_fop;
3005         it->oie_file.private_data = NULL;
3006         lu_object_get(lo);
3007
3008         RETURN((struct dt_it*) it);
3009 }
3010
3011 /**
3012  * Destroy or finishes iterator context.
3013  *
3014  * \param di, struct osd_it_ea, iterator structure to be destroyed
3015  */
3016 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3017 {
3018         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3019         struct osd_object    *obj  = it->oie_obj;
3020
3021
3022         ENTRY;
3023         lu_object_put(env, &obj->oo_dt.do_lu);
3024         EXIT;
3025 }
3026
3027 /**
3028  * It position the iterator at given key, so that next lookup continues from
3029  * that key Or it is similar to dio_it->load() but based on a key,
3030  * rather than file position.
3031  *
3032  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3033  * to the beginning.
3034  *
3035  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3036  */
3037 static int osd_it_ea_get(const struct lu_env *env,
3038                          struct dt_it *di, const struct dt_key *key)
3039 {
3040         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
3041
3042         ENTRY;
3043         LASSERT(((const char *)key)[0] == '\0');
3044         it->oie_namelen         = 0;
3045         it->oie_curr_pos        = 0;
3046         it->oie_next_pos        = 0;
3047
3048         RETURN(+1);
3049 }
3050
3051 /**
3052  * Does nothing
3053  */
3054 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3055 {
3056 }
3057
3058 /**
3059  * It is called internally by ->readdir(). It fills the
3060  * iterator's in-memory data structure with required
3061  * information i.e. name, namelen, rec_size etc.
3062  *
3063  * \param buf, in which information to be filled in.
3064  * \param name, name of the file in given dir
3065  *
3066  * \retval 0, on success
3067  * \retval 1, on buffer full
3068  */
3069 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3070                                loff_t offset, ino_t ino,
3071                                unsigned int d_type)
3072 {
3073         struct osd_it_ea   *it     = (struct osd_it_ea *)buf;
3074         struct dirent64    *dirent = &it->oie_dirent64;
3075         int                 reclen = LDISKFS_DIR_REC_LEN(namelen);
3076
3077
3078         ENTRY;
3079         if (it->oie_namelen)
3080                 RETURN(-ENOENT);
3081
3082         if (namelen == 0 || namelen > LDISKFS_NAME_LEN)
3083                 RETURN(-EIO);
3084
3085         strncpy(dirent->d_name, name, LDISKFS_NAME_LEN);
3086         dirent->d_name[namelen] = 0;
3087         dirent->d_ino           = ino;
3088         dirent->d_off           = offset;
3089         dirent->d_reclen        = reclen;
3090         it->oie_namelen         = namelen;
3091         it->oie_curr_pos        = offset;
3092
3093         RETURN(0);
3094 }
3095
3096 /**
3097  * Calls ->readdir() to load a directory entry at a time
3098  * and stored it in iterator's in-memory data structure.
3099  *
3100  * \param di, struct osd_it_ea, iterator's in memory structure
3101  *
3102  * \retval   0, on success
3103  * \retval -ve, on error
3104  */
3105 int osd_ldiskfs_it_fill(const struct dt_it *di)
3106 {
3107         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
3108         struct osd_object  *obj   = it->oie_obj;
3109         struct inode       *inode = obj->oo_inode;
3110         int                result = 0;
3111
3112         ENTRY;
3113         it->oie_namelen    = 0;
3114         it->oie_file.f_pos = it->oie_curr_pos;
3115
3116         result = inode->i_fop->readdir(&it->oie_file, it,
3117                                        (filldir_t) osd_ldiskfs_filldir);
3118
3119         it->oie_next_pos = it->oie_file.f_pos;
3120
3121         if(!result && it->oie_namelen == 0)
3122                 result = -EIO;
3123
3124         RETURN(result);
3125 }
3126
3127 /**
3128  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3129  * to load a directory entry at a time and stored it in
3130  * iterator's in-memory data structure.
3131  *
3132  * \param di, struct osd_it_ea, iterator's in memory structure
3133  *
3134  * \retval +ve, iterator reached to end
3135  * \retval   0, iterator not reached to end
3136  * \retval -ve, on error
3137  */
3138 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3139 {
3140         struct osd_it_ea *it = (struct osd_it_ea *)di;
3141         int rc;
3142
3143         ENTRY;
3144         it->oie_curr_pos = it->oie_next_pos;
3145
3146         if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3147                 rc = +1;
3148         else
3149                 rc = osd_ldiskfs_it_fill(di);
3150
3151         RETURN(rc);
3152 }
3153
3154 /**
3155  * Returns the key at current position from iterator's in memory structure.
3156  *
3157  * \param di, struct osd_it_ea, iterator's in memory structure
3158  *
3159  * \retval key i.e. struct dt_key on success
3160  */
3161 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3162                                     const struct dt_it *di)
3163 {
3164         struct osd_it_ea *it = (struct osd_it_ea *)di;
3165         ENTRY;
3166         RETURN((struct dt_key *)it->oie_dirent64.d_name);
3167 }
3168
3169 /**
3170  * Returns the key's size at current position from iterator's in memory structure.
3171  *
3172  * \param di, struct osd_it_ea, iterator's in memory structure
3173  *
3174  * \retval key_size i.e. struct dt_key on success
3175  */
3176 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3177 {
3178         struct osd_it_ea *it = (struct osd_it_ea *)di;
3179         ENTRY;
3180         RETURN(it->oie_namelen);
3181 }
3182
3183 /**
3184  * Returns the value (i.e. fid/igif) at current position from iterator's
3185  * in memory structure.
3186  *
3187  * \param di, struct osd_it_ea, iterator's in memory structure
3188  *
3189  * \retval value i.e. struct dt_rec on success
3190  */
3191 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3192                                     const struct dt_it *di)
3193 {
3194         struct osd_it_ea       *it     = (struct osd_it_ea *)di;
3195         struct osd_object      *obj    = it->oie_obj;
3196         struct osd_thread_info *info   = osd_oti_get(env);
3197         struct osd_inode_id    *id     = &info->oti_id;
3198         struct lu_fid_pack     *rec    = &info->oti_pack;
3199         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
3200         struct dentry          *dentry = &info->oti_child_dentry;
3201         struct osd_device      *dev;
3202         struct inode           *inode;
3203         int                    rc;
3204
3205         ENTRY;
3206         dev  = osd_dev(ldev);
3207         id->oii_ino = it->oie_dirent64.d_ino;
3208         id->oii_gen = OSD_OII_NOGEN;
3209         inode = osd_iget(info, dev, id);
3210         if (!IS_ERR(inode)) {
3211                 dentry->d_inode = inode;
3212                 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3213         } else {
3214                 CERROR("Error getting inode for ino =%d", id->oii_ino);
3215                 RETURN((struct dt_rec *) PTR_ERR(inode));
3216         }
3217
3218         rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3219
3220         iput(inode);
3221         RETURN((struct dt_rec *)rec);
3222
3223 }
3224
3225 /**
3226  * Returns a cookie for current position of the iterator head, so that
3227  * user can use this cookie to load/start the iterator next time.
3228  *
3229  * \param di, struct osd_it_ea, iterator's in memory structure
3230  *
3231  * \retval cookie for current position, on success
3232  */
3233 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3234 {
3235         struct osd_it_ea *it = (struct osd_it_ea *)di;
3236         ENTRY;
3237         RETURN(it->oie_curr_pos);
3238 }
3239
3240 /**
3241  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3242  * to load a directory entry at a time and stored it i inn,
3243  * in iterator's in-memory data structure.
3244  *
3245  * \param di, struct osd_it_ea, iterator's in memory structure
3246  *
3247  * \retval +ve, on success
3248  * \retval -ve, on error
3249  */
3250 static int osd_it_ea_load(const struct lu_env *env,
3251                           const struct dt_it *di, __u64 hash)
3252 {
3253         struct osd_it_ea *it = (struct osd_it_ea *)di;
3254         int rc;
3255
3256         ENTRY;
3257         it->oie_curr_pos = it->oie_next_pos = hash;
3258
3259         rc =  osd_ldiskfs_it_fill(di);
3260         if (rc == 0)
3261                 rc = +1;
3262
3263         RETURN(rc);
3264 }
3265 /**
3266  * Index and Iterator operations for interoperability
3267  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3268  */
3269 static const struct dt_index_operations osd_index_ea_ops = {
3270         .dio_lookup = osd_index_ea_lookup,
3271         .dio_insert = osd_index_ea_insert,
3272         .dio_delete = osd_index_ea_delete,
3273         .dio_it     = {
3274                 .init     = osd_it_ea_init,
3275                 .fini     = osd_it_ea_fini,
3276                 .get      = osd_it_ea_get,
3277                 .put      = osd_it_ea_put,
3278                 .next     = osd_it_ea_next,
3279                 .key      = osd_it_ea_key,
3280                 .key_size = osd_it_ea_key_size,
3281                 .rec      = osd_it_ea_rec,
3282                 .store    = osd_it_ea_store,
3283                 .load     = osd_it_ea_load
3284         }
3285 };
3286
3287 /**
3288  * Index lookup function for interoperability mode (b11826).
3289  *
3290  * \param key,  key i.e. file name to be searched
3291  *
3292  * \retval +ve, on success
3293  * \retval -ve, on error
3294  */
3295 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3296                                struct dt_rec *rec, const struct dt_key *key,
3297                                struct lustre_capa *capa)
3298 {
3299         struct osd_object *obj = osd_dt_obj(dt);
3300         int rc = 0;
3301
3302         ENTRY;
3303
3304         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3305         LINVRNT(osd_invariant(obj));
3306
3307         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3308                 return -EACCES;
3309
3310         rc = osd_ea_lookup_rec(env, obj, rec, key);
3311
3312         if (rc == 0)
3313                 rc = +1;
3314         RETURN(rc);
3315 }
3316
3317 /* type constructor/destructor: osd_type_init, osd_type_fini */
3318 LU_TYPE_INIT_FINI(osd, &osd_key);
3319
3320 static struct lu_context_key osd_key = {
3321         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3322         .lct_init = osd_key_init,
3323         .lct_fini = osd_key_fini,
3324         .lct_exit = osd_key_exit
3325 };
3326
3327 static void *osd_key_init(const struct lu_context *ctx,
3328                           struct lu_context_key *key)
3329 {
3330         struct osd_thread_info *info;
3331
3332         OBD_ALLOC_PTR(info);
3333         if (info != NULL)
3334                 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3335         else
3336                 info = ERR_PTR(-ENOMEM);
3337         return info;
3338 }
3339
3340 /* context key destructor: osd_key_fini */
3341 LU_KEY_FINI(osd, struct osd_thread_info);
3342
3343 static void osd_key_exit(const struct lu_context *ctx,
3344                          struct lu_context_key *key, void *data)
3345 {
3346         struct osd_thread_info *info = data;
3347
3348         LASSERT(info->oti_r_locks == 0);
3349         LASSERT(info->oti_w_locks == 0);
3350         LASSERT(info->oti_txns    == 0);
3351 }
3352
3353 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3354                            const char *name, struct lu_device *next)
3355 {
3356         int rc;
3357         struct lu_context *ctx;
3358
3359         /* context for commit hooks */
3360         ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3361         rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3362         if (rc == 0) {
3363                 rc = osd_procfs_init(osd_dev(d), name);
3364                 ctx->lc_cookie = 0x3;
3365         }
3366         return rc;
3367 }
3368
3369 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3370 {
3371         struct osd_thread_info *info = osd_oti_get(env);
3372         ENTRY;
3373         if (o->od_obj_area != NULL) {
3374                 lu_object_put(env, &o->od_obj_area->do_lu);
3375                 o->od_obj_area = NULL;
3376         }
3377         osd_oi_fini(info, &o->od_oi);
3378
3379         RETURN(0);
3380 }
3381
3382 static int osd_mount(const struct lu_env *env,
3383                      struct osd_device *o, struct lustre_cfg *cfg)
3384 {
3385         struct lustre_mount_info *lmi;
3386         const char               *dev  = lustre_cfg_string(cfg, 0);
3387         struct lustre_disk_data  *ldd;
3388         struct lustre_sb_info    *lsi;
3389
3390         ENTRY;
3391
3392         if (o->od_mount != NULL) {
3393                 CERROR("Already mounted (%s)\n", dev);
3394                 RETURN(-EEXIST);
3395         }
3396
3397         /* get mount */
3398         lmi = server_get_mount(dev);
3399         if (lmi == NULL) {
3400                 CERROR("Cannot get mount info for %s!\n", dev);
3401                 RETURN(-EFAULT);
3402         }
3403
3404         LASSERT(lmi != NULL);
3405         /* save lustre_mount_info in dt_device */
3406         o->od_mount = lmi;
3407
3408         lsi = s2lsi(lmi->lmi_sb);
3409         ldd = lsi->lsi_ldd;
3410
3411         if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3412                 o->od_iop_mode = 0;
3413                 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3414         } else
3415                 o->od_iop_mode = 1;
3416
3417         o->od_obj_area = NULL;
3418         RETURN(0);
3419 }
3420
3421 static struct lu_device *osd_device_fini(const struct lu_env *env,
3422                                          struct lu_device *d)
3423 {
3424         int rc;
3425         ENTRY;
3426
3427         shrink_dcache_sb(osd_sb(osd_dev(d)));
3428         osd_sync(env, lu2dt_dev(d));
3429
3430         rc = osd_procfs_fini(osd_dev(d));
3431         if (rc) {
3432                 CERROR("proc fini error %d \n", rc);
3433                 RETURN (ERR_PTR(rc));
3434         }
3435
3436         if (osd_dev(d)->od_mount)
3437                 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3438                                  osd_dev(d)->od_mount->lmi_mnt);
3439         osd_dev(d)->od_mount = NULL;
3440
3441         lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3442         RETURN(NULL);
3443 }
3444
3445 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3446                                           struct lu_device_type *t,
3447                                           struct lustre_cfg *cfg)
3448 {
3449         struct lu_device  *l;
3450         struct osd_device *o;
3451
3452         OBD_ALLOC_PTR(o);
3453         if (o != NULL) {
3454                 int result;
3455
3456                 result = dt_device_init(&o->od_dt_dev, t);
3457                 if (result == 0) {
3458                         l = osd2lu_dev(o);
3459                         l->ld_ops = &osd_lu_ops;
3460                         o->od_dt_dev.dd_ops = &osd_dt_ops;
3461                         spin_lock_init(&o->od_osfs_lock);
3462                         o->od_osfs_age = cfs_time_shift_64(-1000);
3463                         o->od_capa_hash = init_capa_hash();
3464                         if (o->od_capa_hash == NULL) {
3465                                 dt_device_fini(&o->od_dt_dev);
3466                                 l = ERR_PTR(-ENOMEM);
3467                         }
3468                 } else
3469                         l = ERR_PTR(result);
3470
3471                 if (IS_ERR(l))
3472                         OBD_FREE_PTR(o);
3473         } else
3474                 l = ERR_PTR(-ENOMEM);
3475         return l;
3476 }
3477
3478 static struct lu_device *osd_device_free(const struct lu_env *env,
3479                                          struct lu_device *d)
3480 {
3481         struct osd_device *o = osd_dev(d);
3482         ENTRY;
3483
3484         cleanup_capa_hash(o->od_capa_hash);
3485         dt_device_fini(&o->od_dt_dev);
3486         OBD_FREE_PTR(o);
3487         RETURN(NULL);
3488 }
3489
3490 static int osd_process_config(const struct lu_env *env,
3491                               struct lu_device *d, struct lustre_cfg *cfg)
3492 {
3493         struct osd_device *o = osd_dev(d);
3494         int err;
3495         ENTRY;
3496
3497         switch(cfg->lcfg_command) {
3498         case LCFG_SETUP:
3499                 err = osd_mount(env, o, cfg);
3500                 break;
3501         case LCFG_CLEANUP:
3502                 err = osd_shutdown(env, o);
3503                 break;
3504         default:
3505                 err = -ENOSYS;
3506         }
3507
3508         RETURN(err);
3509 }
3510
3511 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3512                                     struct ldiskfs_super_block * es);
3513
3514 static int osd_recovery_complete(const struct lu_env *env,
3515                                  struct lu_device *d)
3516 {
3517         struct osd_device *o = osd_dev(d);
3518         ENTRY;
3519         /* TODO: orphans handling */
3520         ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3521         RETURN(0);
3522 }
3523
3524 static int osd_prepare(const struct lu_env *env,
3525                        struct lu_device *pdev,
3526                        struct lu_device *dev)
3527 {
3528         struct osd_device *osd = osd_dev(dev);
3529         struct lustre_sb_info *lsi;
3530         struct lustre_disk_data *ldd;
3531         struct lustre_mount_info  *lmi;
3532         struct osd_thread_info *oti = osd_oti_get(env);
3533         struct dt_object *d;
3534         int result;
3535
3536         ENTRY;
3537         /* 1. initialize oi before any file create or file open */
3538         result = osd_oi_init(oti, &osd->od_oi,
3539                              &osd->od_dt_dev, lu2md_dev(pdev));
3540         if (result != 0)
3541                 RETURN(result);
3542
3543         lmi = osd->od_mount;
3544         lsi = s2lsi(lmi->lmi_sb);
3545         ldd = lsi->lsi_ldd;
3546
3547         /* 2. setup local objects */
3548         result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3549         if (result)
3550                 goto out;
3551
3552         /* 3. open remote object dir */
3553         d = dt_store_open(env, lu2dt_dev(dev), "",
3554                           remote_obj_dir, &oti->oti_fid);
3555         if (!IS_ERR(d)) {
3556                 osd->od_obj_area = d;
3557                 result = 0;
3558         } else {
3559                 result = PTR_ERR(d);
3560                 osd->od_obj_area = NULL;
3561         }
3562
3563 out:
3564         RETURN(result);
3565 }
3566
3567 static struct inode *osd_iget(struct osd_thread_info *info,
3568                               struct osd_device *dev,
3569                               const struct osd_inode_id *id)
3570 {
3571         struct inode *inode;
3572
3573         inode = iget(osd_sb(dev), id->oii_ino);
3574         if (inode == NULL) {
3575                 CERROR("no inode\n");
3576                 inode = ERR_PTR(-EACCES);
3577         } else if (is_bad_inode(inode)) {
3578                 CERROR("bad inode\n");
3579                 iput(inode);
3580                 inode = ERR_PTR(-ENOENT);
3581         } else if (id->oii_gen != OSD_OII_NOGEN &&
3582                    inode->i_generation != id->oii_gen) {
3583                 CERROR("stale inode\n");
3584                 iput(inode);
3585                 inode = ERR_PTR(-ESTALE);
3586         }
3587
3588         return inode;
3589
3590 }
3591
3592 static int osd_fid_lookup(const struct lu_env *env,
3593                           struct osd_object *obj, const struct lu_fid *fid)
3594 {
3595         struct osd_thread_info *info;
3596         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
3597         struct osd_device      *dev;
3598         struct osd_inode_id    *id;
3599         struct osd_oi          *oi;
3600         struct inode           *inode;
3601         int                     result;
3602
3603         LINVRNT(osd_invariant(obj));
3604         LASSERT(obj->oo_inode == NULL);
3605         LASSERT(fid_is_sane(fid));
3606         /*
3607          * This assertion checks that osd layer sees only local
3608          * fids. Unfortunately it is somewhat expensive (does a
3609          * cache-lookup). Disabling it for production/acceptance-testing.
3610          */
3611         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3612
3613         ENTRY;
3614
3615         info = osd_oti_get(env);
3616         dev  = osd_dev(ldev);
3617         id   = &info->oti_id;
3618         oi   = &dev->od_oi;
3619
3620         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3621                 RETURN(-ENOENT);
3622
3623         result = osd_oi_lookup(info, oi, fid, id);
3624         if (result == 0) {
3625                 inode = osd_iget(info, dev, id);
3626                 if (!IS_ERR(inode)) {
3627                         obj->oo_inode = inode;
3628                         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3629                         if (dev->od_iop_mode) {
3630                                 obj->oo_compat_dot_created = 1;
3631                                 obj->oo_compat_dotdot_created = 1;
3632                         }
3633                         result = 0;
3634                 } else
3635                         /*
3636                          * If fid wasn't found in oi, inode-less object is
3637                          * created, for which lu_object_exists() returns
3638                          * false. This is used in a (frequent) case when
3639                          * objects are created as locking anchors or
3640                          * place holders for objects yet to be created.
3641                          */
3642                         result = PTR_ERR(inode);
3643         } else if (result == -ENOENT)
3644                 result = 0;
3645         LINVRNT(osd_invariant(obj));
3646
3647         RETURN(result);
3648 }
3649
3650 static void osd_inode_getattr(const struct lu_env *env,
3651                               struct inode *inode, struct lu_attr *attr)
3652 {
3653         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3654                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3655                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3656
3657         attr->la_atime      = LTIME_S(inode->i_atime);
3658         attr->la_mtime      = LTIME_S(inode->i_mtime);
3659         attr->la_ctime      = LTIME_S(inode->i_ctime);
3660         attr->la_mode       = inode->i_mode;
3661         attr->la_size       = i_size_read(inode);
3662         attr->la_blocks     = inode->i_blocks;
3663         attr->la_uid        = inode->i_uid;
3664         attr->la_gid        = inode->i_gid;
3665         attr->la_flags      = LDISKFS_I(inode)->i_flags;
3666         attr->la_nlink      = inode->i_nlink;
3667         attr->la_rdev       = inode->i_rdev;
3668         attr->la_blksize    = ll_inode_blksize(inode);
3669         attr->la_blkbits    = inode->i_blkbits;
3670 }
3671
3672 /*
3673  * Helpers.
3674  */
3675
3676 static int lu_device_is_osd(const struct lu_device *d)
3677 {
3678         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3679 }
3680
3681 static struct osd_object *osd_obj(const struct lu_object *o)
3682 {
3683         LASSERT(lu_device_is_osd(o->lo_dev));
3684         return container_of0(o, struct osd_object, oo_dt.do_lu);
3685 }
3686
3687 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3688 {
3689         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3690         return container_of0(d, struct osd_device, od_dt_dev);
3691 }
3692
3693 static struct osd_device *osd_dev(const struct lu_device *d)
3694 {
3695         LASSERT(lu_device_is_osd(d));
3696         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3697 }
3698
3699 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3700 {
3701         return osd_obj(&d->do_lu);
3702 }
3703
3704 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3705 {
3706         return osd_dev(o->oo_dt.do_lu.lo_dev);
3707 }
3708
3709 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3710 {
3711         return &osd->od_dt_dev.dd_lu_dev;
3712 }
3713
3714 static struct super_block *osd_sb(const struct osd_device *dev)
3715 {
3716         return dev->od_mount->lmi_mnt->mnt_sb;
3717 }
3718
3719 static journal_t *osd_journal(const struct osd_device *dev)
3720 {
3721         return LDISKFS_SB(osd_sb(dev))->s_journal;
3722 }
3723
3724 static int osd_has_index(const struct osd_object *obj)
3725 {
3726         return obj->oo_dt.do_index_ops != NULL;
3727 }
3728
3729 static int osd_object_invariant(const struct lu_object *l)
3730 {
3731         return osd_invariant(osd_obj(l));
3732 }
3733
3734 static const struct lu_object_operations osd_lu_obj_ops = {
3735         .loo_object_init      = osd_object_init,
3736         .loo_object_delete    = osd_object_delete,
3737         .loo_object_release   = osd_object_release,
3738         .loo_object_free      = osd_object_free,
3739         .loo_object_print     = osd_object_print,
3740         .loo_object_invariant = osd_object_invariant
3741 };
3742
3743 static const struct lu_device_operations osd_lu_ops = {
3744         .ldo_object_alloc      = osd_object_alloc,
3745         .ldo_process_config    = osd_process_config,
3746         .ldo_recovery_complete = osd_recovery_complete,
3747         .ldo_prepare           = osd_prepare,
3748 };
3749
3750 static const struct lu_device_type_operations osd_device_type_ops = {
3751         .ldto_init = osd_type_init,
3752         .ldto_fini = osd_type_fini,
3753
3754         .ldto_start = osd_type_start,
3755         .ldto_stop  = osd_type_stop,
3756
3757         .ldto_device_alloc = osd_device_alloc,
3758         .ldto_device_free  = osd_device_free,
3759
3760         .ldto_device_init    = osd_device_init,
3761         .ldto_device_fini    = osd_device_fini
3762 };
3763
3764 static struct lu_device_type osd_device_type = {
3765         .ldt_tags     = LU_DEVICE_DT,
3766         .ldt_name     = LUSTRE_OSD_NAME,
3767         .ldt_ops      = &osd_device_type_ops,
3768         .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3769 };
3770
3771 /*
3772  * lprocfs legacy support.
3773  */
3774 static struct obd_ops osd_obd_device_ops = {
3775         .o_owner = THIS_MODULE
3776 };
3777
3778 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3779         .llod_name      = remote_obj_dir,
3780         .llod_oid       = OSD_REM_OBJ_DIR_OID,
3781         .llod_is_index  = 1,
3782         .llod_feat      = &dt_directory_features,
3783 };
3784
3785 static int __init osd_mod_init(void)
3786 {
3787         struct lprocfs_static_vars lvars;
3788
3789         osd_oi_mod_init();
3790         llo_local_obj_register(&llod_osd_rem_obj_dir);
3791         lprocfs_osd_init_vars(&lvars);
3792         return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3793                                    LUSTRE_OSD_NAME, &osd_device_type);
3794 }
3795
3796 static void __exit osd_mod_exit(void)
3797 {
3798         llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3799         class_unregister_type(LUSTRE_OSD_NAME);
3800 }
3801
3802 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3803 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3804 MODULE_LICENSE("GPL");
3805
3806 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);