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