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