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