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