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