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