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