Whamcloud - gitweb
LU-1581 build: remove false ldiskfs dependencies
[fs/lustre-release.git] / lustre / mdd / mdd_device.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/mdd/mdd_device.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <obd_class.h>
46 #include <lprocfs_status.h>
47 #include <lustre_fid.h>
48 #include <lustre_mds.h>
49 #include <lustre_disk.h>      /* for changelogs */
50 #include <lustre_param.h>
51 #include <lustre_fid.h>
52
53 #include "mdd_internal.h"
54
55 const struct md_device_operations mdd_ops;
56 static struct lu_device_type mdd_device_type;
57
58 static const char mdd_root_dir_name[] = "ROOT";
59 static const char mdd_obf_dir_name[] = "fid";
60
61 /* Slab for MDD object allocation */
62 cfs_mem_cache_t *mdd_object_kmem;
63
64 static struct lu_kmem_descr mdd_caches[] = {
65         {
66                 .ckd_cache = &mdd_object_kmem,
67                 .ckd_name  = "mdd_obj",
68                 .ckd_size  = sizeof(struct mdd_object)
69         },
70         {
71                 .ckd_cache = NULL
72         }
73 };
74
75 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
76                            const char *name, struct lu_device *next)
77 {
78         struct mdd_device *mdd = lu2mdd_dev(d);
79         int rc;
80         ENTRY;
81
82         mdd->mdd_child = lu2dt_dev(next);
83
84         /* Prepare transactions callbacks. */
85         mdd->mdd_txn_cb.dtc_txn_start = NULL;
86         mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
87         mdd->mdd_txn_cb.dtc_txn_commit = NULL;
88         mdd->mdd_txn_cb.dtc_cookie = mdd;
89         mdd->mdd_txn_cb.dtc_tag = LCT_MD_THREAD;
90         CFS_INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
91         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
92         /* sync permission changes */
93         mdd->mdd_sync_permission = 1;
94
95         rc = mdd_procfs_init(mdd, name);
96         RETURN(rc);
97 }
98
99 static struct lu_device *mdd_device_fini(const struct lu_env *env,
100                                          struct lu_device *d)
101 {
102         struct mdd_device *mdd = lu2mdd_dev(d);
103         int rc;
104
105         rc = mdd_procfs_fini(mdd);
106         if (rc) {
107                 CERROR("proc fini error %d \n", rc);
108                 return ERR_PTR(rc);
109         }
110         return NULL;
111 }
112
113 static void mdd_changelog_fini(const struct lu_env *env,
114                                struct mdd_device *mdd);
115
116 static void mdd_device_shutdown(const struct lu_env *env,
117                                 struct mdd_device *m, struct lustre_cfg *cfg)
118 {
119         ENTRY;
120         mdd_lfsck_cleanup(env, m);
121         mdd_changelog_fini(env, m);
122         dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
123         if (m->mdd_dot_lustre_objs.mdd_obf)
124                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
125         if (m->mdd_dot_lustre)
126                 mdd_object_put(env, m->mdd_dot_lustre);
127         if (m->mdd_obd_dev)
128                 mdd_fini_obd(env, m, cfg);
129         orph_index_fini(env, m);
130         if (m->mdd_capa != NULL) {
131                 lu_object_put(env, &m->mdd_capa->do_lu);
132                 m->mdd_capa = NULL;
133         }
134         /* remove upcall device*/
135         md_upcall_fini(&m->mdd_md_dev);
136         EXIT;
137 }
138
139 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
140                              struct llog_rec_hdr *hdr, void *data)
141 {
142         struct mdd_device *mdd = (struct mdd_device *)data;
143         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
144         ENTRY;
145
146         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
147         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
148
149         CDEBUG(D_INFO,
150                "seeing record at index %d/%d/"LPU64" t=%x %.*s in log "LPX64"\n",
151                hdr->lrh_index, rec->cr_hdr.lrh_index, rec->cr.cr_index,
152                rec->cr.cr_type, rec->cr.cr_namelen, rec->cr.cr_name,
153                llh->lgh_id.lgl_oid);
154
155         mdd->mdd_cl.mc_index = rec->cr.cr_index;
156         RETURN(LLOG_PROC_BREAK);
157 }
158
159 static int changelog_user_init_cb(const struct lu_env *env,
160                                   struct llog_handle *llh,
161                                   struct llog_rec_hdr *hdr, void *data)
162 {
163         struct mdd_device *mdd = (struct mdd_device *)data;
164         struct llog_changelog_user_rec *rec =
165                 (struct llog_changelog_user_rec *)hdr;
166         ENTRY;
167
168         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
169         LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
170
171         CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec="LPU64
172                " in log "LPX64"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
173                rec->cur_id, rec->cur_endrec, llh->lgh_id.lgl_oid);
174
175         cfs_spin_lock(&mdd->mdd_cl.mc_user_lock);
176         mdd->mdd_cl.mc_lastuser = rec->cur_id;
177         cfs_spin_unlock(&mdd->mdd_cl.mc_user_lock);
178
179         RETURN(LLOG_PROC_BREAK);
180 }
181
182
183 static int mdd_changelog_llog_init(struct mdd_device *mdd)
184 {
185         struct obd_device *obd = mdd2obd_dev(mdd);
186         struct llog_ctxt *ctxt;
187         int rc;
188
189         /* Find last changelog entry number */
190         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
191         if (ctxt == NULL) {
192                 CERROR("no changelog context\n");
193                 return -EINVAL;
194         }
195         if (!ctxt->loc_handle) {
196                 llog_ctxt_put(ctxt);
197                 return -EINVAL;
198         }
199
200         rc = llog_cat_reverse_process(NULL, ctxt->loc_handle,
201                                       changelog_init_cb, mdd);
202         llog_ctxt_put(ctxt);
203
204         if (rc < 0) {
205                 CERROR("changelog init failed: %d\n", rc);
206                 return rc;
207         }
208         CDEBUG(D_IOCTL, "changelog starting index="LPU64"\n",
209                mdd->mdd_cl.mc_index);
210
211         /* Find last changelog user id */
212         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
213         if (ctxt == NULL) {
214                 CERROR("no changelog user context\n");
215                 return -EINVAL;
216         }
217         if (!ctxt->loc_handle) {
218                 llog_ctxt_put(ctxt);
219                 return -EINVAL;
220         }
221
222         rc = llog_cat_reverse_process(NULL, ctxt->loc_handle,
223                                       changelog_user_init_cb, mdd);
224         llog_ctxt_put(ctxt);
225
226         if (rc < 0) {
227                 CERROR("changelog user init failed: %d\n", rc);
228                 return rc;
229         }
230
231         /* If we have registered users, assume we want changelogs on */
232         if (mdd->mdd_cl.mc_lastuser > 0)
233                 rc = mdd_changelog_on(mdd, 1);
234
235         return rc;
236 }
237
238 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
239 {
240         int rc;
241
242         mdd->mdd_cl.mc_index = 0;
243         cfs_spin_lock_init(&mdd->mdd_cl.mc_lock);
244         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
245         mdd->mdd_cl.mc_flags = 0; /* off by default */
246         mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
247         cfs_spin_lock_init(&mdd->mdd_cl.mc_user_lock);
248         mdd->mdd_cl.mc_lastuser = 0;
249
250         rc = mdd_changelog_llog_init(mdd);
251         if (rc) {
252                 CERROR("Changelog setup during init failed %d\n", rc);
253                 mdd->mdd_cl.mc_flags |= CLM_ERR;
254         }
255
256         return rc;
257 }
258
259 static void mdd_changelog_fini(const struct lu_env *env, struct mdd_device *mdd)
260 {
261         mdd->mdd_cl.mc_flags = 0;
262 }
263
264 /* Start / stop recording */
265 int mdd_changelog_on(struct mdd_device *mdd, int on)
266 {
267         int rc = 0;
268
269         if ((on == 1) && ((mdd->mdd_cl.mc_flags & CLM_ON) == 0)) {
270                 LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
271                 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
272                         CERROR("Changelogs cannot be enabled due to error "
273                                "condition (see %s log).\n",
274                                mdd2obd_dev(mdd)->obd_name);
275                         rc = -ESRCH;
276                 } else {
277                         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
278                         mdd->mdd_cl.mc_flags |= CLM_ON;
279                         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
280                         rc = mdd_changelog_write_header(mdd, CLM_START);
281                 }
282         } else if ((on == 0) && ((mdd->mdd_cl.mc_flags & CLM_ON) == CLM_ON)) {
283                 LCONSOLE_INFO("%s: changelog off\n",mdd2obd_dev(mdd)->obd_name);
284                 rc = mdd_changelog_write_header(mdd, CLM_FINI);
285                 cfs_spin_lock(&mdd->mdd_cl.mc_lock);
286                 mdd->mdd_cl.mc_flags &= ~CLM_ON;
287                 cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
288         }
289         return rc;
290 }
291
292 static __u64 cl_time(void) {
293         cfs_fs_time_t time;
294
295         cfs_fs_time_current(&time);
296         return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
297 }
298
299 /** Add a changelog entry \a rec to the changelog llog
300  * \param mdd
301  * \param rec
302  * \param handle - currently ignored since llogs start their own transaction;
303  *                 this will hopefully be fixed in llog rewrite
304  * \retval 0 ok
305  */
306 int mdd_changelog_llog_write(struct mdd_device         *mdd,
307                              struct llog_changelog_rec *rec,
308                              struct thandle            *handle)
309 {
310         struct obd_device *obd = mdd2obd_dev(mdd);
311         struct llog_ctxt *ctxt;
312         int rc;
313
314         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
315         /* llog_lvfs_write_rec sets the llog tail len */
316         rec->cr_hdr.lrh_type = CHANGELOG_REC;
317         rec->cr.cr_time = cl_time();
318         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
319         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
320            but as long as the MDD transactions are ordered correctly for e.g.
321            rename conflicts, I don't think this should matter. */
322         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
323         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
324         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
325         if (ctxt == NULL)
326                 return -ENXIO;
327
328         /* nested journal transaction */
329         rc = llog_add(NULL, ctxt, &rec->cr_hdr, NULL, NULL, 0);
330         llog_ctxt_put(ctxt);
331
332         return rc;
333 }
334
335 /** Add a changelog_ext entry \a rec to the changelog llog
336  * \param mdd
337  * \param rec
338  * \param handle - currently ignored since llogs start their own transaction;
339  *              this will hopefully be fixed in llog rewrite
340  * \retval 0 ok
341  */
342 int mdd_changelog_ext_llog_write(struct mdd_device *mdd,
343                                  struct llog_changelog_ext_rec *rec,
344                                  struct thandle *handle)
345 {
346         struct obd_device *obd = mdd2obd_dev(mdd);
347         struct llog_ctxt *ctxt;
348         int rc;
349
350         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
351         /* llog_lvfs_write_rec sets the llog tail len */
352         rec->cr_hdr.lrh_type = CHANGELOG_REC;
353         rec->cr.cr_time = cl_time();
354         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
355         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
356          * but as long as the MDD transactions are ordered correctly for e.g.
357          * rename conflicts, I don't think this should matter. */
358         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
359         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
360         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
361         if (ctxt == NULL)
362                 return -ENXIO;
363
364         /* nested journal transaction */
365         rc = llog_add(NULL, ctxt, &rec->cr_hdr, NULL, NULL, 0);
366         llog_ctxt_put(ctxt);
367
368         return rc;
369 }
370
371 /** Remove entries with indicies up to and including \a endrec from the
372  *  changelog
373  * \param mdd
374  * \param endrec
375  * \retval 0 ok
376  */
377 int mdd_changelog_llog_cancel(const struct lu_env *env,
378                               struct mdd_device *mdd, long long endrec)
379 {
380         struct obd_device *obd = mdd2obd_dev(mdd);
381         struct llog_ctxt *ctxt;
382         long long unsigned cur;
383         int rc;
384
385         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
386         if (ctxt == NULL)
387                 return -ENXIO;
388
389         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
390         cur = (long long)mdd->mdd_cl.mc_index;
391         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
392         if (endrec > cur)
393                 endrec = cur;
394
395         /* purge to "0" is shorthand for everything */
396         if (endrec == 0)
397                 endrec = cur;
398
399         /* If purging all records, write a header entry so we don't have an
400            empty catalog and we're sure to have a valid starting index next
401            time.  In case of crash, we just restart with old log so we're
402            allright. */
403         if (endrec == cur) {
404                 /* XXX: transaction is started by llog itself */
405                 rc = mdd_changelog_write_header(mdd, CLM_PURGE);
406                 if (rc)
407                       goto out;
408         }
409
410         /* Some records were purged, so reset repeat-access time (so we
411            record new mtime update records, so users can see a file has been
412            changed since the last purge) */
413         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
414
415         /* XXX: transaction is started by llog itself */
416         rc = llog_cancel(env, ctxt, NULL, 1, (struct llog_cookie *)&endrec, 0);
417 out:
418         llog_ctxt_put(ctxt);
419         return rc;
420 }
421
422 /** Add a CL_MARK record to the changelog
423  * \param mdd
424  * \param markerflags - CLM_*
425  * \retval 0 ok
426  */
427 int mdd_changelog_write_header(struct mdd_device *mdd, int markerflags)
428 {
429         struct obd_device *obd = mdd2obd_dev(mdd);
430         struct llog_changelog_rec *rec;
431         int reclen;
432         int len = strlen(obd->obd_name);
433         int rc;
434         ENTRY;
435
436         reclen = llog_data_len(sizeof(*rec) + len);
437         OBD_ALLOC(rec, reclen);
438         if (rec == NULL)
439                 RETURN(-ENOMEM);
440
441         rec->cr.cr_flags = CLF_VERSION;
442         rec->cr.cr_type = CL_MARK;
443         rec->cr.cr_namelen = len;
444         memcpy(rec->cr.cr_name, obd->obd_name, rec->cr.cr_namelen);
445         /* Status and action flags */
446         rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
447
448         /* XXX: transaction is started by llog itself */
449         rc = (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) ?
450                 mdd_changelog_llog_write(mdd, rec, NULL) : 0;
451
452         /* assume on or off event; reset repeat-access time */
453         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
454
455         OBD_FREE(rec, reclen);
456         RETURN(rc);
457 }
458
459 /**
460  * Create ".lustre" directory.
461  */
462 static int create_dot_lustre_dir(const struct lu_env *env, struct mdd_device *m)
463 {
464         struct lu_fid *fid = &mdd_env_info(env)->mti_fid;
465         struct md_object *mdo;
466         int rc;
467
468         memcpy(fid, &LU_DOT_LUSTRE_FID, sizeof(struct lu_fid));
469         mdo = llo_store_create_index(env, &m->mdd_md_dev, m->mdd_child,
470                                      mdd_root_dir_name, dot_lustre_name,
471                                      fid, &dt_directory_features);
472         /* .lustre dir may be already present */
473         if (IS_ERR(mdo) && PTR_ERR(mdo) != -EEXIST) {
474                 rc = PTR_ERR(mdo);
475                 CERROR("creating obj [%s] fid = "DFID" rc = %d\n",
476                         dot_lustre_name, PFID(fid), rc);
477                 return rc;
478         }
479
480         if (!IS_ERR(mdo))
481                 lu_object_put(env, &mdo->mo_lu);
482
483         return 0;
484 }
485
486
487 static int dot_lustre_mdd_permission(const struct lu_env *env,
488                                      struct md_object *pobj,
489                                      struct md_object *cobj,
490                                      struct md_attr *attr, int mask)
491 {
492         if (mask & ~(MAY_READ | MAY_EXEC))
493                 return -EPERM;
494         else
495                 return 0;
496 }
497
498 static int dot_lustre_mdd_xattr_get(const struct lu_env *env,
499                                     struct md_object *obj, struct lu_buf *buf,
500                                     const char *name)
501 {
502         return 0;
503 }
504
505 static int dot_lustre_mdd_xattr_list(const struct lu_env *env,
506                                      struct md_object *obj, struct lu_buf *buf)
507 {
508         return 0;
509 }
510
511 static int dot_lustre_mdd_xattr_set(const struct lu_env *env,
512                                     struct md_object *obj,
513                                     const struct lu_buf *buf, const char *name,
514                                     int fl)
515 {
516         return -EPERM;
517 }
518
519 static int dot_lustre_mdd_xattr_del(const struct lu_env *env,
520                                     struct md_object *obj,
521                                     const char *name)
522 {
523         return -EPERM;
524 }
525
526 static int dot_lustre_mdd_readlink(const struct lu_env *env,
527                                    struct md_object *obj, struct lu_buf *buf)
528 {
529         return 0;
530 }
531
532 static int dot_lustre_mdd_object_create(const struct lu_env *env,
533                                         struct md_object *obj,
534                                         const struct md_op_spec *spec,
535                                         struct md_attr *ma)
536 {
537         return -EPERM;
538 }
539
540 static int dot_lustre_mdd_ref_add(const struct lu_env *env,
541                                   struct md_object *obj,
542                                   const struct md_attr *ma)
543 {
544         return -EPERM;
545 }
546
547 static int dot_lustre_mdd_ref_del(const struct lu_env *env,
548                                   struct md_object *obj,
549                                   struct md_attr *ma)
550 {
551         return -EPERM;
552 }
553
554 static int dot_lustre_mdd_open(const struct lu_env *env, struct md_object *obj,
555                                int flags)
556 {
557         struct mdd_object *mdd_obj = md2mdd_obj(obj);
558
559         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
560         mdd_obj->mod_count++;
561         mdd_write_unlock(env, mdd_obj);
562
563         return 0;
564 }
565
566 static int dot_lustre_mdd_close(const struct lu_env *env, struct md_object *obj,
567                                 struct md_attr *ma, int mode)
568 {
569         struct mdd_object *mdd_obj = md2mdd_obj(obj);
570
571         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
572         mdd_obj->mod_count--;
573         mdd_write_unlock(env, mdd_obj);
574
575         return 0;
576 }
577
578 static int dot_lustre_mdd_object_sync(const struct lu_env *env,
579                                       struct md_object *obj)
580 {
581         return -ENOSYS;
582 }
583
584 static int dot_lustre_mdd_path(const struct lu_env *env, struct md_object *obj,
585                            char *path, int pathlen, __u64 *recno, int *linkno)
586 {
587         return -ENOSYS;
588 }
589
590 static int dot_file_lock(const struct lu_env *env, struct md_object *obj,
591                          struct lov_mds_md *lmm, struct ldlm_extent *extent,
592                          struct lustre_handle *lockh)
593 {
594         return -ENOSYS;
595 }
596
597 static int dot_file_unlock(const struct lu_env *env, struct md_object *obj,
598                            struct lov_mds_md *lmm, struct lustre_handle *lockh)
599 {
600         return -ENOSYS;
601 }
602
603 static struct md_object_operations mdd_dot_lustre_obj_ops = {
604         .moo_permission    = dot_lustre_mdd_permission,
605         .moo_attr_get      = mdd_attr_get,
606         .moo_attr_set      = mdd_attr_set,
607         .moo_xattr_get     = dot_lustre_mdd_xattr_get,
608         .moo_xattr_list    = dot_lustre_mdd_xattr_list,
609         .moo_xattr_set     = dot_lustre_mdd_xattr_set,
610         .moo_xattr_del     = dot_lustre_mdd_xattr_del,
611         .moo_readpage      = mdd_readpage,
612         .moo_readlink      = dot_lustre_mdd_readlink,
613         .moo_object_create = dot_lustre_mdd_object_create,
614         .moo_ref_add       = dot_lustre_mdd_ref_add,
615         .moo_ref_del       = dot_lustre_mdd_ref_del,
616         .moo_open          = dot_lustre_mdd_open,
617         .moo_close         = dot_lustre_mdd_close,
618         .moo_capa_get      = mdd_capa_get,
619         .moo_object_sync   = dot_lustre_mdd_object_sync,
620         .moo_path          = dot_lustre_mdd_path,
621         .moo_file_lock     = dot_file_lock,
622         .moo_file_unlock   = dot_file_unlock,
623 };
624
625
626 static int dot_lustre_mdd_lookup(const struct lu_env *env, struct md_object *p,
627                                  const struct lu_name *lname, struct lu_fid *f,
628                                  struct md_op_spec *spec)
629 {
630         if (strcmp(lname->ln_name, mdd_obf_dir_name) == 0)
631                 *f = LU_OBF_FID;
632         else
633                 return -ENOENT;
634
635         return 0;
636 }
637
638 static mdl_mode_t dot_lustre_mdd_lock_mode(const struct lu_env *env,
639                                            struct md_object *obj,
640                                            mdl_mode_t mode)
641 {
642         return MDL_MINMODE;
643 }
644
645 static int dot_lustre_mdd_create(const struct lu_env *env,
646                                  struct md_object *pobj,
647                                  const struct lu_name *lname,
648                                  struct md_object *child,
649                                  struct md_op_spec *spec,
650                                  struct md_attr* ma)
651 {
652         return -EPERM;
653 }
654
655 static int dot_lustre_mdd_create_data(const struct lu_env *env,
656                                       struct md_object *p,
657                                       struct md_object *o,
658                                       const struct md_op_spec *spec,
659                                       struct md_attr *ma)
660 {
661         return -EPERM;
662 }
663
664 static int dot_lustre_mdd_rename(const struct lu_env *env,
665                                  struct md_object *src_pobj,
666                                  struct md_object *tgt_pobj,
667                                  const struct lu_fid *lf,
668                                  const struct lu_name *lsname,
669                                  struct md_object *tobj,
670                                  const struct lu_name *ltname,
671                                  struct md_attr *ma)
672 {
673         return -EPERM;
674 }
675
676 static int dot_lustre_mdd_link(const struct lu_env *env,
677                                struct md_object *tgt_obj,
678                                struct md_object *src_obj,
679                                const struct lu_name *lname,
680                                struct md_attr *ma)
681 {
682         return -EPERM;
683 }
684
685 static int dot_lustre_mdd_unlink(const struct lu_env *env,
686                                  struct md_object *pobj,
687                                  struct md_object *cobj,
688                                  const struct lu_name *lname,
689                                  struct md_attr *ma)
690 {
691         return -EPERM;
692 }
693
694 static int dot_lustre_mdd_name_insert(const struct lu_env *env,
695                                       struct md_object *obj,
696                                       const struct lu_name *lname,
697                                       const struct lu_fid *fid,
698                                       const struct md_attr *ma)
699 {
700         return -EPERM;
701 }
702
703 static int dot_lustre_mdd_name_remove(const struct lu_env *env,
704                                       struct md_object *obj,
705                                       const struct lu_name *lname,
706                                       const struct md_attr *ma)
707 {
708         return -EPERM;
709 }
710
711 static int dot_lustre_mdd_rename_tgt(const struct lu_env *env,
712                                      struct md_object *pobj,
713                                      struct md_object *tobj,
714                                      const struct lu_fid *fid,
715                                      const struct lu_name *lname,
716                                      struct md_attr *ma)
717 {
718         return -EPERM;
719 }
720
721
722 static struct md_dir_operations mdd_dot_lustre_dir_ops = {
723         .mdo_is_subdir   = mdd_is_subdir,
724         .mdo_lookup      = dot_lustre_mdd_lookup,
725         .mdo_lock_mode   = dot_lustre_mdd_lock_mode,
726         .mdo_create      = dot_lustre_mdd_create,
727         .mdo_create_data = dot_lustre_mdd_create_data,
728         .mdo_rename      = dot_lustre_mdd_rename,
729         .mdo_link        = dot_lustre_mdd_link,
730         .mdo_unlink      = dot_lustre_mdd_unlink,
731         .mdo_name_insert = dot_lustre_mdd_name_insert,
732         .mdo_name_remove = dot_lustre_mdd_name_remove,
733         .mdo_rename_tgt  = dot_lustre_mdd_rename_tgt,
734 };
735
736 static int obf_attr_get(const struct lu_env *env, struct md_object *obj,
737                         struct md_attr *ma)
738 {
739         int rc = 0;
740
741         if (ma->ma_need & MA_INODE) {
742                 struct mdd_device *mdd = mdo2mdd(obj);
743
744                 /* "fid" is a virtual object and hence does not have any "real"
745                  * attributes. So we reuse attributes of .lustre for "fid" dir */
746                 ma->ma_need |= MA_INODE;
747                 rc = mdd_attr_get(env, &mdd->mdd_dot_lustre->mod_obj, ma);
748                 if (rc)
749                         return rc;
750                 ma->ma_valid |= MA_INODE;
751         }
752
753         /* "fid" directory does not have any striping information. */
754         if (ma->ma_need & MA_LOV) {
755                 struct mdd_object *mdd_obj = md2mdd_obj(obj);
756
757                 if (ma->ma_valid & MA_LOV)
758                         return 0;
759
760                 if (!(S_ISREG(mdd_object_type(mdd_obj)) ||
761                       S_ISDIR(mdd_object_type(mdd_obj))))
762                         return 0;
763
764                 if (ma->ma_need & MA_LOV_DEF) {
765                         rc = mdd_get_default_md(mdd_obj, ma->ma_lmm);
766                         if (rc > 0) {
767                                 ma->ma_lmm_size = rc;
768                                 ma->ma_valid |= MA_LOV;
769                                 rc = 0;
770                         }
771                 }
772         }
773
774         return rc;
775 }
776
777 static int obf_attr_set(const struct lu_env *env, struct md_object *obj,
778                         const struct md_attr *ma)
779 {
780         return -EPERM;
781 }
782
783 static int obf_xattr_list(const struct lu_env *env,
784                           struct md_object *obj, struct lu_buf *buf)
785 {
786         return 0;
787 }
788
789 static int obf_xattr_get(const struct lu_env *env,
790                          struct md_object *obj, struct lu_buf *buf,
791                          const char *name)
792 {
793         return 0;
794 }
795
796 static int obf_xattr_set(const struct lu_env *env,
797                          struct md_object *obj,
798                          const struct lu_buf *buf, const char *name,
799                          int fl)
800 {
801         return -EPERM;
802 }
803
804 static int obf_xattr_del(const struct lu_env *env,
805                          struct md_object *obj,
806                          const char *name)
807 {
808         return -EPERM;
809 }
810
811 static int obf_mdd_open(const struct lu_env *env, struct md_object *obj,
812                         int flags)
813 {
814         struct mdd_object *mdd_obj = md2mdd_obj(obj);
815
816         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
817         mdd_obj->mod_count++;
818         mdd_write_unlock(env, mdd_obj);
819
820         return 0;
821 }
822
823 static int obf_mdd_close(const struct lu_env *env, struct md_object *obj,
824                          struct md_attr *ma, int mode)
825 {
826         struct mdd_object *mdd_obj = md2mdd_obj(obj);
827
828         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
829         mdd_obj->mod_count--;
830         mdd_write_unlock(env, mdd_obj);
831
832         return 0;
833 }
834
835 /** Nothing to list in "fid" directory */
836 static int obf_mdd_readpage(const struct lu_env *env, struct md_object *obj,
837                             const struct lu_rdpg *rdpg)
838 {
839         return -EPERM;
840 }
841
842 static int obf_path(const struct lu_env *env, struct md_object *obj,
843                     char *path, int pathlen, __u64 *recno, int *linkno)
844 {
845         return -ENOSYS;
846 }
847
848 static struct md_object_operations mdd_obf_obj_ops = {
849         .moo_attr_get    = obf_attr_get,
850         .moo_attr_set    = obf_attr_set,
851         .moo_xattr_list  = obf_xattr_list,
852         .moo_xattr_get   = obf_xattr_get,
853         .moo_xattr_set   = obf_xattr_set,
854         .moo_xattr_del   = obf_xattr_del,
855         .moo_open        = obf_mdd_open,
856         .moo_close       = obf_mdd_close,
857         .moo_readpage    = obf_mdd_readpage,
858         .moo_path        = obf_path
859 };
860
861 /**
862  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
863  * are valid. We also check if object with passed fid exists or not.
864  */
865 static int obf_lookup(const struct lu_env *env, struct md_object *p,
866                       const struct lu_name *lname, struct lu_fid *f,
867                       struct md_op_spec *spec)
868 {
869         char *name = (char *)lname->ln_name;
870         struct mdd_device *mdd = mdo2mdd(p);
871         struct mdd_object *child;
872         int rc = 0;
873
874         while (*name == '[')
875                 name++;
876
877         sscanf(name, SFID, RFID(f));
878         if (!fid_is_sane(f)) {
879                 CWARN("%s: bad FID format [%s], should be "DFID"\n",
880                       mdd->mdd_obd_dev->obd_name, lname->ln_name,
881                       (__u64)FID_SEQ_NORMAL, 1, 0);
882                 GOTO(out, rc = -EINVAL);
883         }
884
885         if (!fid_is_norm(f)) {
886                 CWARN("%s: "DFID" is invalid, sequence should be "
887                       ">= "LPX64"\n", mdd->mdd_obd_dev->obd_name, PFID(f),
888                       (__u64)FID_SEQ_NORMAL);
889                 GOTO(out, rc = -EINVAL);
890         }
891
892         /* Check if object with this fid exists */
893         child = mdd_object_find(env, mdd, f);
894         if (child == NULL)
895                 GOTO(out, rc = 0);
896         if (IS_ERR(child))
897                 GOTO(out, rc = PTR_ERR(child));
898
899         if (mdd_object_exists(child) == 0)
900                 rc = -ENOENT;
901
902         mdd_object_put(env, child);
903
904 out:
905         return rc;
906 }
907
908 static int obf_create(const struct lu_env *env, struct md_object *pobj,
909                       const struct lu_name *lname, struct md_object *child,
910                       struct md_op_spec *spec, struct md_attr* ma)
911 {
912         return -EPERM;
913 }
914
915 static int obf_rename(const struct lu_env *env,
916                       struct md_object *src_pobj, struct md_object *tgt_pobj,
917                       const struct lu_fid *lf, const struct lu_name *lsname,
918                       struct md_object *tobj, const struct lu_name *ltname,
919                       struct md_attr *ma)
920 {
921         return -EPERM;
922 }
923
924 static int obf_link(const struct lu_env *env, struct md_object *tgt_obj,
925                     struct md_object *src_obj, const struct lu_name *lname,
926                     struct md_attr *ma)
927 {
928         return -EPERM;
929 }
930
931 static int obf_unlink(const struct lu_env *env, struct md_object *pobj,
932                       struct md_object *cobj, const struct lu_name *lname,
933                       struct md_attr *ma)
934 {
935         return -EPERM;
936 }
937
938 static struct md_dir_operations mdd_obf_dir_ops = {
939         .mdo_lookup = obf_lookup,
940         .mdo_create = obf_create,
941         .mdo_rename = obf_rename,
942         .mdo_link   = obf_link,
943         .mdo_unlink = obf_unlink
944 };
945
946 /**
947  * Create special in-memory "fid" object for open-by-fid.
948  */
949 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
950 {
951         struct mdd_object *mdd_obf;
952         struct lu_object *obf_lu_obj;
953         int rc = 0;
954
955         m->mdd_dot_lustre_objs.mdd_obf = mdd_object_find(env, m,
956                                                          &LU_OBF_FID);
957         if (m->mdd_dot_lustre_objs.mdd_obf == NULL ||
958             IS_ERR(m->mdd_dot_lustre_objs.mdd_obf))
959                 GOTO(out, rc = -ENOENT);
960
961         mdd_obf = m->mdd_dot_lustre_objs.mdd_obf;
962         mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
963         mdd_obf->mod_obj.mo_ops = &mdd_obf_obj_ops;
964         /* Don't allow objects to be created in "fid" dir */
965         mdd_obf->mod_flags |= IMMUTE_OBJ;
966
967         obf_lu_obj = mdd2lu_obj(mdd_obf);
968         obf_lu_obj->lo_header->loh_attr |= (LOHA_EXISTS | S_IFDIR);
969
970 out:
971         return rc;
972 }
973
974 /** Setup ".lustre" directory object */
975 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
976 {
977         struct dt_object *dt_dot_lustre;
978         struct lu_fid *fid = &mdd_env_info(env)->mti_fid;
979         int rc;
980
981         rc = create_dot_lustre_dir(env, m);
982         if (rc)
983                 return rc;
984
985         dt_dot_lustre = dt_store_open(env, m->mdd_child, mdd_root_dir_name,
986                                       dot_lustre_name, fid);
987         if (IS_ERR(dt_dot_lustre)) {
988                 rc = PTR_ERR(dt_dot_lustre);
989                 GOTO(out, rc);
990         }
991
992         /* references are released in mdd_device_shutdown() */
993         m->mdd_dot_lustre = lu2mdd_obj(lu_object_locate(dt_dot_lustre->do_lu.lo_header,
994                                                         &mdd_device_type));
995
996         m->mdd_dot_lustre->mod_obj.mo_dir_ops = &mdd_dot_lustre_dir_ops;
997         m->mdd_dot_lustre->mod_obj.mo_ops = &mdd_dot_lustre_obj_ops;
998
999         rc = mdd_obf_setup(env, m);
1000         if (rc)
1001                 CERROR("Error initializing \"fid\" object - %d.\n", rc);
1002
1003 out:
1004         RETURN(rc);
1005 }
1006
1007 static int mdd_process_config(const struct lu_env *env,
1008                               struct lu_device *d, struct lustre_cfg *cfg)
1009 {
1010         struct mdd_device *m    = lu2mdd_dev(d);
1011         struct dt_device  *dt   = m->mdd_child;
1012         struct lu_device  *next = &dt->dd_lu_dev;
1013         int rc;
1014         ENTRY;
1015
1016         switch (cfg->lcfg_command) {
1017         case LCFG_PARAM: {
1018                 struct lprocfs_static_vars lvars;
1019
1020                 lprocfs_mdd_init_vars(&lvars);
1021                 rc = class_process_proc_param(PARAM_MDD, lvars.obd_vars, cfg,m);
1022                 if (rc > 0 || rc == -ENOSYS)
1023                         /* we don't understand; pass it on */
1024                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
1025                 break;
1026         }
1027         case LCFG_SETUP:
1028                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1029                 if (rc)
1030                         GOTO(out, rc);
1031                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
1032
1033                 rc = mdd_init_obd(env, m, cfg);
1034                 if (rc) {
1035                         CERROR("lov init error %d\n", rc);
1036                         GOTO(out, rc);
1037                 }
1038
1039                 mdd_changelog_init(env, m);
1040                 break;
1041         case LCFG_CLEANUP:
1042                 lu_dev_del_linkage(d->ld_site, d);
1043                 mdd_device_shutdown(env, m, cfg);
1044         default:
1045                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1046                 break;
1047         }
1048 out:
1049         RETURN(rc);
1050 }
1051
1052 #if 0
1053 static int mdd_lov_set_nextid(const struct lu_env *env,
1054                               struct mdd_device *mdd)
1055 {
1056         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
1057         int rc;
1058         ENTRY;
1059
1060         LASSERT(mds->mds_lov_objids != NULL);
1061         rc = obd_set_info_async(mds->mds_lov_exp, strlen(KEY_NEXT_ID),
1062                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
1063                                 mds->mds_lov_objids, NULL);
1064
1065         RETURN(rc);
1066 }
1067
1068 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
1069                                    struct mdd_device *mdd)
1070 {
1071         /* XXX: to be implemented! */
1072         return 0;
1073 }
1074 #endif
1075
1076 static int mdd_recovery_complete(const struct lu_env *env,
1077                                  struct lu_device *d)
1078 {
1079         struct mdd_device *mdd = lu2mdd_dev(d);
1080         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1081         struct obd_device *obd = mdd2obd_dev(mdd);
1082         int rc;
1083         ENTRY;
1084
1085         LASSERT(mdd != NULL);
1086         LASSERT(obd != NULL);
1087 #if 0
1088         /* XXX: Do we need this in new stack? */
1089         rc = mdd_lov_set_nextid(env, mdd);
1090         if (rc) {
1091                 CERROR("mdd_lov_set_nextid() failed %d\n",
1092                        rc);
1093                 RETURN(rc);
1094         }
1095
1096         /* XXX: cleanup unlink. */
1097         rc = mdd_cleanup_unlink_llog(env, mdd);
1098         if (rc) {
1099                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
1100                        rc);
1101                 RETURN(rc);
1102         }
1103 #endif
1104         /* Call that with obd_recovering = 1 just to update objids */
1105         obd_notify(obd->u.mds.mds_lov_obd, NULL, (obd->obd_async_recov ?
1106                     OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
1107
1108         /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
1109         cfs_spin_lock(&obd->obd_dev_lock);
1110         obd->obd_recovering = 0;
1111         cfs_spin_unlock(&obd->obd_dev_lock);
1112         obd->obd_type->typ_dt_ops->o_postrecov(obd);
1113
1114         /* XXX: orphans handling. */
1115         __mdd_orphan_cleanup(env, mdd);
1116         rc = next->ld_ops->ldo_recovery_complete(env, next);
1117
1118         RETURN(rc);
1119 }
1120
1121 static int mdd_prepare(const struct lu_env *env,
1122                        struct lu_device *pdev,
1123                        struct lu_device *cdev)
1124 {
1125         struct mdd_device *mdd = lu2mdd_dev(cdev);
1126         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1127         struct dt_object *root;
1128         struct lu_fid     fid;
1129         int rc;
1130
1131         ENTRY;
1132         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1133         if (rc)
1134                 GOTO(out, rc);
1135
1136         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
1137         root = dt_store_open(env, mdd->mdd_child, "", mdd_root_dir_name,
1138                              &mdd->mdd_root_fid);
1139         if (!IS_ERR(root)) {
1140                 LASSERT(root != NULL);
1141                 lu_object_put(env, &root->do_lu);
1142                 rc = orph_index_init(env, mdd);
1143         } else {
1144                 rc = PTR_ERR(root);
1145         }
1146         if (rc)
1147                 GOTO(out, rc);
1148
1149         rc = mdd_dot_lustre_setup(env, mdd);
1150         if (rc) {
1151                 CERROR("Error(%d) initializing .lustre objects\n", rc);
1152                 GOTO(out, rc);
1153         }
1154
1155         /* we use capa file to declare llog changes,
1156          * will be fixed with new llog in 2.3 */
1157         root = dt_store_open(env, mdd->mdd_child, "", CAPA_KEYS, &fid);
1158         if (IS_ERR(root))
1159                 GOTO(out, rc = PTR_ERR(root));
1160
1161         mdd->mdd_capa = root;
1162         rc = mdd_lfsck_setup(env, mdd);
1163
1164         GOTO(out, rc);
1165
1166 out:
1167         return rc;
1168 }
1169
1170 const struct lu_device_operations mdd_lu_ops = {
1171         .ldo_object_alloc      = mdd_object_alloc,
1172         .ldo_process_config    = mdd_process_config,
1173         .ldo_recovery_complete = mdd_recovery_complete,
1174         .ldo_prepare           = mdd_prepare,
1175 };
1176
1177 /*
1178  * No permission check is needed.
1179  */
1180 static int mdd_root_get(const struct lu_env *env,
1181                         struct md_device *m, struct lu_fid *f)
1182 {
1183         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1184
1185         ENTRY;
1186         *f = mdd->mdd_root_fid;
1187         RETURN(0);
1188 }
1189
1190 /*
1191  * No permission check is needed.
1192  */
1193 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1194                       struct obd_statfs *sfs)
1195 {
1196         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1197         int rc;
1198
1199         ENTRY;
1200
1201         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1202
1203         RETURN(rc);
1204 }
1205
1206 /*
1207  * No permission check is needed.
1208  */
1209 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
1210                            int *md_size, int *cookie_size)
1211 {
1212         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1213         ENTRY;
1214
1215         *md_size = mdd_lov_mdsize(env, mdd);
1216         *cookie_size = mdd_lov_cookiesize(env, mdd);
1217
1218         RETURN(0);
1219 }
1220
1221 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
1222                               int mode, unsigned long timeout, __u32 alg,
1223                               struct lustre_capa_key *keys)
1224 {
1225         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1226         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
1227         int rc;
1228         ENTRY;
1229
1230         /* need barrier for mds_capa_keys access. */
1231         cfs_down_write(&mds->mds_notify_lock);
1232         mds->mds_capa_keys = keys;
1233         cfs_up_write(&mds->mds_notify_lock);
1234
1235         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
1236                                                    timeout, alg, keys);
1237         RETURN(rc);
1238 }
1239
1240 static int mdd_update_capa_key(const struct lu_env *env,
1241                                struct md_device *m,
1242                                struct lustre_capa_key *key)
1243 {
1244         struct mds_capa_info info = { .uuid = NULL, .capa = key };
1245         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1246         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_lov_exp;
1247         int rc;
1248         ENTRY;
1249
1250         rc = obd_set_info_async(env, lov_exp, sizeof(KEY_CAPA_KEY),
1251                                 KEY_CAPA_KEY, sizeof(info), &info, NULL);
1252         RETURN(rc);
1253 }
1254
1255 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1256                              int idx, void **h)
1257 {
1258         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1259
1260         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1261         return (*h == NULL ? -ENOENT : 0);
1262 }
1263
1264 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1265                                           struct lu_device_type *t,
1266                                           struct lustre_cfg *lcfg)
1267 {
1268         struct lu_device  *l;
1269         struct mdd_device *m;
1270
1271         OBD_ALLOC_PTR(m);
1272         if (m == NULL) {
1273                 l = ERR_PTR(-ENOMEM);
1274         } else {
1275                 md_device_init(&m->mdd_md_dev, t);
1276                 l = mdd2lu_dev(m);
1277                 l->ld_ops = &mdd_lu_ops;
1278                 m->mdd_md_dev.md_ops = &mdd_ops;
1279                 md_upcall_init(&m->mdd_md_dev, NULL);
1280         }
1281
1282         return l;
1283 }
1284
1285 static struct lu_device *mdd_device_free(const struct lu_env *env,
1286                                          struct lu_device *lu)
1287 {
1288         struct mdd_device *m = lu2mdd_dev(lu);
1289         ENTRY;
1290
1291         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
1292         md_device_fini(&m->mdd_md_dev);
1293         OBD_FREE_PTR(m);
1294         RETURN(NULL);
1295 }
1296
1297 static struct obd_ops mdd_obd_device_ops = {
1298         .o_owner = THIS_MODULE
1299 };
1300
1301 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
1302 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
1303
1304 static struct lu_context_key mdd_ucred_key = {
1305         .lct_tags = LCT_SESSION,
1306         .lct_init = mdd_ucred_key_init,
1307         .lct_fini = mdd_ucred_key_fini
1308 };
1309
1310 struct md_ucred *md_ucred(const struct lu_env *env)
1311 {
1312         LASSERT(env->le_ses != NULL);
1313         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
1314 }
1315 EXPORT_SYMBOL(md_ucred);
1316
1317 /*
1318  * context key constructor/destructor:
1319  * mdd_capainfo_key_init, mdd_capainfo_key_fini
1320  */
1321 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
1322
1323 struct lu_context_key mdd_capainfo_key = {
1324         .lct_tags = LCT_SESSION,
1325         .lct_init = mdd_capainfo_key_init,
1326         .lct_fini = mdd_capainfo_key_fini
1327 };
1328
1329 struct md_capainfo *md_capainfo(const struct lu_env *env)
1330 {
1331         /* NB, in mdt_init0 */
1332         if (env->le_ses == NULL)
1333                 return NULL;
1334         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
1335 }
1336 EXPORT_SYMBOL(md_capainfo);
1337
1338 /*
1339  * context key constructor/destructor:
1340  * mdd_quota_key_init, mdd_quota_key_fini
1341  */
1342 LU_KEY_INIT_FINI(mdd_quota, struct md_quota);
1343
1344 struct lu_context_key mdd_quota_key = {
1345         .lct_tags = LCT_SESSION,
1346         .lct_init = mdd_quota_key_init,
1347         .lct_fini = mdd_quota_key_fini
1348 };
1349
1350 struct md_quota *md_quota(const struct lu_env *env)
1351 {
1352         LASSERT(env->le_ses != NULL);
1353         return lu_context_key_get(env->le_ses, &mdd_quota_key);
1354 }
1355 EXPORT_SYMBOL(md_quota);
1356
1357 static int mdd_changelog_user_register(struct mdd_device *mdd, int *id)
1358 {
1359         struct llog_ctxt *ctxt;
1360         struct llog_changelog_user_rec *rec;
1361         int rc;
1362         ENTRY;
1363
1364         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
1365         if (ctxt == NULL)
1366                 RETURN(-ENXIO);
1367
1368         OBD_ALLOC_PTR(rec);
1369         if (rec == NULL) {
1370                 llog_ctxt_put(ctxt);
1371                 RETURN(-ENOMEM);
1372         }
1373
1374         /* Assume we want it on since somebody registered */
1375         rc = mdd_changelog_on(mdd, 1);
1376         if (rc)
1377                 GOTO(out, rc);
1378
1379         rec->cur_hdr.lrh_len = sizeof(*rec);
1380         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1381         cfs_spin_lock(&mdd->mdd_cl.mc_user_lock);
1382         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1383                 cfs_spin_unlock(&mdd->mdd_cl.mc_user_lock);
1384                 CERROR("Maximum number of changelog users exceeded!\n");
1385                 GOTO(out, rc = -EOVERFLOW);
1386         }
1387         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1388         rec->cur_endrec = mdd->mdd_cl.mc_index;
1389         cfs_spin_unlock(&mdd->mdd_cl.mc_user_lock);
1390
1391         rc = llog_add(NULL, ctxt, &rec->cur_hdr, NULL, NULL, 0);
1392
1393         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1394 out:
1395         OBD_FREE_PTR(rec);
1396         llog_ctxt_put(ctxt);
1397         RETURN(rc);
1398 }
1399
1400 int mdd_declare_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
1401                             struct thandle *handle)
1402 {
1403         int rc;
1404
1405
1406         /* XXX: this is a temporary solution to declare llog changes
1407          *      will be fixed in 2.3 with new llog implementation */
1408
1409         LASSERT(mdd->mdd_capa);
1410
1411         /* the llog record could be canceled either by modifying
1412          * the plain llog's header or by destroying the llog itself
1413          * when this record is the last one in it, it can't be known
1414          * here, but the catlog's header will also be modified for
1415          * the second case, then the first case can be covered and
1416          * is no need to declare it */
1417
1418         /* destroy empty plain log */
1419         rc = dt_declare_destroy(env, mdd->mdd_capa, handle);
1420         if (rc)
1421                 return rc;
1422
1423         /* record the catlog's header if an empty plain log was destroyed */
1424         rc = dt_declare_record_write(env, mdd->mdd_capa,
1425                                      sizeof(struct llog_logid_rec), 0, handle);
1426         return rc;
1427 }
1428
1429 struct mdd_changelog_user_data {
1430         __u64 mcud_endrec; /**< purge record for this user */
1431         __u64 mcud_minrec; /**< lowest changelog recno still referenced */
1432         __u32 mcud_id;
1433         __u32 mcud_minid;  /**< user id with lowest rec reference */
1434         __u32 mcud_usercount;
1435         int   mcud_found:1;
1436         struct mdd_device   *mcud_mdd;
1437 };
1438 #define MCUD_UNREGISTER -1LL
1439
1440 /** Two things:
1441  * 1. Find the smallest record everyone is willing to purge
1442  * 2. Update the last purgeable record for this user
1443  */
1444 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1445                                        struct llog_handle *llh,
1446                                        struct llog_rec_hdr *hdr, void *data)
1447 {
1448         struct llog_changelog_user_rec  *rec;
1449         struct mdd_changelog_user_data  *mcud = data;
1450         int                              rc;
1451
1452         ENTRY;
1453
1454         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
1455
1456         rec = (struct llog_changelog_user_rec *)hdr;
1457
1458         mcud->mcud_usercount++;
1459
1460         /* If we have a new endrec for this id, use it for the following
1461            min check instead of its old value */
1462         if (rec->cur_id == mcud->mcud_id)
1463                 rec->cur_endrec = max(rec->cur_endrec, mcud->mcud_endrec);
1464
1465         /* Track the minimum referenced record */
1466         if (mcud->mcud_minid == 0 || mcud->mcud_minrec > rec->cur_endrec) {
1467                 mcud->mcud_minid = rec->cur_id;
1468                 mcud->mcud_minrec = rec->cur_endrec;
1469         }
1470
1471         if (rec->cur_id != mcud->mcud_id)
1472                 RETURN(0);
1473
1474         /* Update this user's record */
1475         mcud->mcud_found = 1;
1476
1477         /* Special case: unregister this user */
1478         if (mcud->mcud_endrec == MCUD_UNREGISTER) {
1479                 struct llog_cookie cookie;
1480                 void *th;
1481                 struct mdd_device *mdd = mcud->mcud_mdd;
1482
1483                 cookie.lgc_lgl = llh->lgh_id;
1484                 cookie.lgc_index = hdr->lrh_index;
1485
1486                 /* XXX This is a workaround for the deadlock of changelog
1487                  * adding vs. changelog cancelling. LU-81. */
1488                 th = mdd_trans_create(env, mdd);
1489                 if (IS_ERR(th)) {
1490                         CERROR("Cannot get thandle\n");
1491                         RETURN(-ENOMEM);
1492                 }
1493
1494                 rc = mdd_declare_llog_cancel(env, mdd, th);
1495                 if (rc)
1496                         GOTO(stop, rc);
1497
1498                 rc = mdd_trans_start(env, mdd, th);
1499                 if (rc)
1500                         GOTO(stop, rc);
1501
1502                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1503                                              1, &cookie);
1504                 if (rc == 0)
1505                         mcud->mcud_usercount--;
1506
1507 stop:
1508                 mdd_trans_stop(env, mdd, 0, th);
1509                 RETURN(rc);
1510         }
1511
1512         /* Update the endrec */
1513         CDEBUG(D_IOCTL, "Rewriting changelog user %d endrec to "LPU64"\n",
1514                mcud->mcud_id, rec->cur_endrec);
1515
1516         /* hdr+1 is loc of data */
1517         hdr->lrh_len -= sizeof(*hdr) + sizeof(struct llog_rec_tail);
1518         rc = llog_write_rec(env, llh, hdr, NULL, 0, (void *)(hdr + 1),
1519                             hdr->lrh_index);
1520
1521         RETURN(rc);
1522 }
1523
1524 static int mdd_changelog_user_purge(const struct lu_env *env,
1525                                     struct mdd_device *mdd, int id,
1526                                     long long endrec)
1527 {
1528         struct mdd_changelog_user_data data;
1529         struct llog_ctxt *ctxt;
1530         int rc;
1531         ENTRY;
1532
1533         CDEBUG(D_IOCTL, "Purge request: id=%d, endrec=%lld\n", id, endrec);
1534
1535         data.mcud_id = id;
1536         data.mcud_minid = 0;
1537         data.mcud_minrec = 0;
1538         data.mcud_usercount = 0;
1539         data.mcud_endrec = endrec;
1540         data.mcud_mdd = mdd;
1541         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
1542         endrec = mdd->mdd_cl.mc_index;
1543         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
1544         if ((data.mcud_endrec == 0) ||
1545             ((data.mcud_endrec > endrec) &&
1546              (data.mcud_endrec != MCUD_UNREGISTER)))
1547                 data.mcud_endrec = endrec;
1548
1549         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
1550         if (ctxt == NULL)
1551                 return -ENXIO;
1552         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
1553
1554         rc = llog_cat_process(env, ctxt->loc_handle,
1555                               mdd_changelog_user_purge_cb, (void *)&data,
1556                               0, 0);
1557         if ((rc >= 0) && (data.mcud_minrec > 0)) {
1558                 CDEBUG(D_IOCTL, "Purging changelog entries up to "LPD64
1559                        ", referenced by "CHANGELOG_USER_PREFIX"%d\n",
1560                        data.mcud_minrec, data.mcud_minid);
1561                 rc = mdd_changelog_llog_cancel(env, mdd, data.mcud_minrec);
1562         } else {
1563                 CWARN("Could not determine changelog records to purge; rc=%d\n",
1564                       rc);
1565         }
1566
1567         llog_ctxt_put(ctxt);
1568
1569         if (!data.mcud_found) {
1570                 CWARN("No entry for user %d.  Last changelog reference is "
1571                       LPD64" by changelog user %d\n", data.mcud_id,
1572                       data.mcud_minrec, data.mcud_minid);
1573                rc = -ENOENT;
1574         }
1575
1576         if (!rc && data.mcud_usercount == 0)
1577                 /* No more users; turn changelogs off */
1578                 rc = mdd_changelog_on(mdd, 0);
1579
1580         RETURN (rc);
1581 }
1582
1583 /** mdd_iocontrol
1584  * May be called remotely from mdt_iocontrol_handle or locally from
1585  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1586  * an obd_ioctl_data format (but local ioctl handler does).
1587  * \param cmd - ioc
1588  * \param len - data len
1589  * \param karg - ioctl data, in kernel space
1590  */
1591 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1592                          unsigned int cmd, int len, void *karg)
1593 {
1594         struct mdd_device *mdd;
1595         struct obd_ioctl_data *data = karg;
1596         int rc;
1597         ENTRY;
1598
1599         mdd = lu2mdd_dev(&m->md_lu_dev);
1600
1601         /* Doesn't use obd_ioctl_data */
1602         switch (cmd) {
1603         case OBD_IOC_CHANGELOG_CLEAR: {
1604                 struct changelog_setinfo *cs = karg;
1605                 rc = mdd_changelog_user_purge(env, mdd, cs->cs_id,
1606                                               cs->cs_recno);
1607                 RETURN(rc);
1608         }
1609         case OBD_IOC_GET_MNTOPT: {
1610                 mntopt_t *mntopts = (mntopt_t *)karg;
1611                 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1612                 RETURN(0);
1613         }
1614         case OBD_IOC_START_LFSCK: {
1615                 struct lfsck_start *start = karg;
1616                 struct md_lfsck *lfsck = &mdd->mdd_lfsck;
1617
1618                 /* Return the kernel service version. */
1619                 /* XXX: version can be used for compatibility in the future. */
1620                 start->ls_version = lfsck->ml_version;
1621                 rc = mdd_lfsck_start(env, lfsck, start);
1622                 RETURN(rc);
1623         }
1624         case OBD_IOC_STOP_LFSCK: {
1625                 rc = mdd_lfsck_stop(env, &mdd->mdd_lfsck);
1626                 RETURN(rc);
1627         }
1628         }
1629
1630         /* Below ioctls use obd_ioctl_data */
1631         if (len != sizeof(*data)) {
1632                 CERROR("Bad ioctl size %d\n", len);
1633                 RETURN(-EINVAL);
1634         }
1635         if (data->ioc_version != OBD_IOCTL_VERSION) {
1636                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1637                        OBD_IOCTL_VERSION);
1638                 RETURN(-EINVAL);
1639         }
1640
1641         switch (cmd) {
1642         case OBD_IOC_CHANGELOG_REG:
1643                 rc = mdd_changelog_user_register(mdd, &data->ioc_u32_1);
1644                 break;
1645         case OBD_IOC_CHANGELOG_DEREG:
1646                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1,
1647                                               MCUD_UNREGISTER);
1648                 break;
1649         default:
1650                 rc = -ENOTTY;
1651         }
1652
1653         RETURN (rc);
1654 }
1655
1656 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1657 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key,
1658                   &mdd_quota_key);
1659
1660 const struct md_device_operations mdd_ops = {
1661         .mdo_statfs         = mdd_statfs,
1662         .mdo_root_get       = mdd_root_get,
1663         .mdo_maxsize_get    = mdd_maxsize_get,
1664         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
1665         .mdo_update_capa_key= mdd_update_capa_key,
1666         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1667         .mdo_iocontrol      = mdd_iocontrol,
1668 #ifdef HAVE_QUOTA_SUPPORT
1669         .mdo_quota          = {
1670                 .mqo_notify      = mdd_quota_notify,
1671                 .mqo_setup       = mdd_quota_setup,
1672                 .mqo_cleanup     = mdd_quota_cleanup,
1673                 .mqo_recovery    = mdd_quota_recovery,
1674                 .mqo_check       = mdd_quota_check,
1675                 .mqo_on          = mdd_quota_on,
1676                 .mqo_off         = mdd_quota_off,
1677                 .mqo_setinfo     = mdd_quota_setinfo,
1678                 .mqo_getinfo     = mdd_quota_getinfo,
1679                 .mqo_setquota    = mdd_quota_setquota,
1680                 .mqo_getquota    = mdd_quota_getquota,
1681                 .mqo_getoinfo    = mdd_quota_getoinfo,
1682                 .mqo_getoquota   = mdd_quota_getoquota,
1683                 .mqo_invalidate  = mdd_quota_invalidate,
1684                 .mqo_finvalidate = mdd_quota_finvalidate
1685         }
1686 #endif
1687 };
1688
1689 static struct lu_device_type_operations mdd_device_type_ops = {
1690         .ldto_init = mdd_type_init,
1691         .ldto_fini = mdd_type_fini,
1692
1693         .ldto_start = mdd_type_start,
1694         .ldto_stop  = mdd_type_stop,
1695
1696         .ldto_device_alloc = mdd_device_alloc,
1697         .ldto_device_free  = mdd_device_free,
1698
1699         .ldto_device_init    = mdd_device_init,
1700         .ldto_device_fini    = mdd_device_fini
1701 };
1702
1703 static struct lu_device_type mdd_device_type = {
1704         .ldt_tags     = LU_DEVICE_MD,
1705         .ldt_name     = LUSTRE_MDD_NAME,
1706         .ldt_ops      = &mdd_device_type_ops,
1707         .ldt_ctx_tags = LCT_MD_THREAD
1708 };
1709
1710 /* context key constructor: mdd_key_init */
1711 LU_KEY_INIT(mdd, struct mdd_thread_info);
1712
1713 static void mdd_key_fini(const struct lu_context *ctx,
1714                          struct lu_context_key *key, void *data)
1715 {
1716         struct mdd_thread_info *info = data;
1717         if (info->mti_max_lmm != NULL)
1718                 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
1719         if (info->mti_max_cookie != NULL)
1720                 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
1721         mdd_buf_put(&info->mti_big_buf);
1722
1723         OBD_FREE_PTR(info);
1724 }
1725
1726 /* context key: mdd_thread_key */
1727 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1728
1729 static struct lu_local_obj_desc llod_capa_key = {
1730         .llod_name      = CAPA_KEYS,
1731         .llod_oid       = MDD_CAPA_KEYS_OID,
1732         .llod_is_index  = 0,
1733 };
1734
1735 static struct lu_local_obj_desc llod_mdd_orphan = {
1736         .llod_name      = orph_index_name,
1737         .llod_oid       = MDD_ORPHAN_OID,
1738         .llod_is_index  = 1,
1739         .llod_feat      = &dt_directory_features,
1740 };
1741
1742 static struct lu_local_obj_desc llod_mdd_root = {
1743         .llod_name      = mdd_root_dir_name,
1744         .llod_oid       = MDD_ROOT_INDEX_OID,
1745         .llod_is_index  = 1,
1746         .llod_feat      = &dt_directory_features,
1747 };
1748
1749 static struct lu_local_obj_desc llod_lfsck_bookmark = {
1750         .llod_name      = lfsck_bookmark_name,
1751         .llod_oid       = LFSCK_BOOKMARK_OID,
1752         .llod_is_index  = 0,
1753 };
1754
1755 static int __init mdd_mod_init(void)
1756 {
1757         struct lprocfs_static_vars lvars;
1758         int rc;
1759
1760         lprocfs_mdd_init_vars(&lvars);
1761
1762         rc = lu_kmem_init(mdd_caches);
1763         if (rc)
1764                 return rc;
1765
1766         llo_local_obj_register(&llod_capa_key);
1767         llo_local_obj_register(&llod_mdd_orphan);
1768         llo_local_obj_register(&llod_mdd_root);
1769         llo_local_obj_register(&llod_lfsck_bookmark);
1770
1771         rc = class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
1772                                  LUSTRE_MDD_NAME, &mdd_device_type);
1773         if (rc)
1774                 lu_kmem_fini(mdd_caches);
1775         return rc;
1776 }
1777
1778 static void __exit mdd_mod_exit(void)
1779 {
1780         llo_local_obj_unregister(&llod_capa_key);
1781         llo_local_obj_unregister(&llod_mdd_orphan);
1782         llo_local_obj_unregister(&llod_mdd_root);
1783         llo_local_obj_unregister(&llod_lfsck_bookmark);
1784
1785         class_unregister_type(LUSTRE_MDD_NAME);
1786         lu_kmem_fini(mdd_caches);
1787 }
1788
1789 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1790 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
1791 MODULE_LICENSE("GPL");
1792
1793 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);