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