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