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