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