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