Whamcloud - gitweb
4ef92657ebc651ab5066744006c80cdca26a4c66
[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_obd_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_obd_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         struct mdd_device *mdd = mdo2mdd(obj);
740
741         /* "fid" is a virtual object and hence does not have any "real"
742          * attributes. So we reuse attributes of .lustre for "fid" dir */
743         return mdd_attr_get(env, &mdd->mdd_dot_lustre->mod_obj, ma);
744 }
745
746 static int obf_attr_set(const struct lu_env *env, struct md_object *obj,
747                         const struct md_attr *ma)
748 {
749         return -EPERM;
750 }
751
752 static int obf_xattr_list(const struct lu_env *env,
753                           struct md_object *obj, struct lu_buf *buf)
754 {
755         return 0;
756 }
757
758 static int obf_xattr_get(const struct lu_env *env,
759                          struct md_object *obj, struct lu_buf *buf,
760                          const char *name)
761 {
762         int rc = 0;
763
764         /* XXX: a temp. solution till LOD/OSP is landed */
765         if (strcmp(name, XATTR_NAME_LOV) == 0) {
766                 if (buf->lb_buf == NULL) {
767                         rc = sizeof(struct lov_user_md);
768                 } else if (buf->lb_len >= sizeof(struct lov_user_md)) {
769                         rc = mdd_get_default_md(md2mdd_obj(obj), buf->lb_buf);
770                 } else {
771                         rc = -ERANGE;
772                 }
773         }
774
775         return rc;
776 }
777
778 static int obf_xattr_set(const struct lu_env *env,
779                          struct md_object *obj,
780                          const struct lu_buf *buf, const char *name,
781                          int fl)
782 {
783         return -EPERM;
784 }
785
786 static int obf_xattr_del(const struct lu_env *env,
787                          struct md_object *obj,
788                          const char *name)
789 {
790         return -EPERM;
791 }
792
793 static int obf_mdd_open(const struct lu_env *env, struct md_object *obj,
794                         int flags)
795 {
796         struct mdd_object *mdd_obj = md2mdd_obj(obj);
797
798         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
799         mdd_obj->mod_count++;
800         mdd_write_unlock(env, mdd_obj);
801
802         return 0;
803 }
804
805 static int obf_mdd_close(const struct lu_env *env, struct md_object *obj,
806                          struct md_attr *ma, int mode)
807 {
808         struct mdd_object *mdd_obj = md2mdd_obj(obj);
809
810         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
811         mdd_obj->mod_count--;
812         mdd_write_unlock(env, mdd_obj);
813
814         return 0;
815 }
816
817 /** Nothing to list in "fid" directory */
818 static int obf_mdd_readpage(const struct lu_env *env, struct md_object *obj,
819                             const struct lu_rdpg *rdpg)
820 {
821         return -EPERM;
822 }
823
824 static int obf_path(const struct lu_env *env, struct md_object *obj,
825                     char *path, int pathlen, __u64 *recno, int *linkno)
826 {
827         return -ENOSYS;
828 }
829
830 static struct md_object_operations mdd_obf_obj_ops = {
831         .moo_attr_get    = obf_attr_get,
832         .moo_attr_set    = obf_attr_set,
833         .moo_xattr_list  = obf_xattr_list,
834         .moo_xattr_get   = obf_xattr_get,
835         .moo_xattr_set   = obf_xattr_set,
836         .moo_xattr_del   = obf_xattr_del,
837         .moo_open        = obf_mdd_open,
838         .moo_close       = obf_mdd_close,
839         .moo_readpage    = obf_mdd_readpage,
840         .moo_path        = obf_path
841 };
842
843 /**
844  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
845  * are valid. We also check if object with passed fid exists or not.
846  */
847 static int obf_lookup(const struct lu_env *env, struct md_object *p,
848                       const struct lu_name *lname, struct lu_fid *f,
849                       struct md_op_spec *spec)
850 {
851         char *name = (char *)lname->ln_name;
852         struct mdd_device *mdd = mdo2mdd(p);
853         struct mdd_object *child;
854         int rc = 0;
855
856         while (*name == '[')
857                 name++;
858
859         sscanf(name, SFID, RFID(f));
860         if (!fid_is_sane(f)) {
861                 CWARN("%s: bad FID format [%s], should be "DFID"\n",
862                       mdd->mdd_obd_dev->obd_name, lname->ln_name,
863                       (__u64)FID_SEQ_NORMAL, 1, 0);
864                 GOTO(out, rc = -EINVAL);
865         }
866
867         if (!fid_is_norm(f)) {
868                 CWARN("%s: "DFID" is invalid, sequence should be "
869                       ">= "LPX64"\n", mdd->mdd_obd_dev->obd_name, PFID(f),
870                       (__u64)FID_SEQ_NORMAL);
871                 GOTO(out, rc = -EINVAL);
872         }
873
874         /* Check if object with this fid exists */
875         child = mdd_object_find(env, mdd, f);
876         if (child == NULL)
877                 GOTO(out, rc = 0);
878         if (IS_ERR(child))
879                 GOTO(out, rc = PTR_ERR(child));
880
881         if (mdd_object_exists(child) == 0)
882                 rc = -ENOENT;
883
884         mdd_object_put(env, child);
885
886 out:
887         return rc;
888 }
889
890 static int obf_create(const struct lu_env *env, struct md_object *pobj,
891                       const struct lu_name *lname, struct md_object *child,
892                       struct md_op_spec *spec, struct md_attr* ma)
893 {
894         return -EPERM;
895 }
896
897 static int obf_rename(const struct lu_env *env,
898                       struct md_object *src_pobj, struct md_object *tgt_pobj,
899                       const struct lu_fid *lf, const struct lu_name *lsname,
900                       struct md_object *tobj, const struct lu_name *ltname,
901                       struct md_attr *ma)
902 {
903         return -EPERM;
904 }
905
906 static int obf_link(const struct lu_env *env, struct md_object *tgt_obj,
907                     struct md_object *src_obj, const struct lu_name *lname,
908                     struct md_attr *ma)
909 {
910         return -EPERM;
911 }
912
913 static int obf_unlink(const struct lu_env *env, struct md_object *pobj,
914                       struct md_object *cobj, const struct lu_name *lname,
915                       struct md_attr *ma)
916 {
917         return -EPERM;
918 }
919
920 static struct md_dir_operations mdd_obf_dir_ops = {
921         .mdo_lookup = obf_lookup,
922         .mdo_create = obf_create,
923         .mdo_rename = obf_rename,
924         .mdo_link   = obf_link,
925         .mdo_unlink = obf_unlink
926 };
927
928 /**
929  * Create special in-memory "fid" object for open-by-fid.
930  */
931 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
932 {
933         struct mdd_object *mdd_obf;
934         struct lu_object *obf_lu_obj;
935         int rc = 0;
936
937         m->mdd_dot_lustre_objs.mdd_obf = mdd_object_find(env, m,
938                                                          &LU_OBF_FID);
939         if (m->mdd_dot_lustre_objs.mdd_obf == NULL ||
940             IS_ERR(m->mdd_dot_lustre_objs.mdd_obf))
941                 GOTO(out, rc = -ENOENT);
942
943         mdd_obf = m->mdd_dot_lustre_objs.mdd_obf;
944         mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
945         mdd_obf->mod_obj.mo_ops = &mdd_obf_obj_ops;
946         /* Don't allow objects to be created in "fid" dir */
947         mdd_obf->mod_flags |= IMMUTE_OBJ;
948
949         obf_lu_obj = mdd2lu_obj(mdd_obf);
950         obf_lu_obj->lo_header->loh_attr |= (LOHA_EXISTS | S_IFDIR);
951
952 out:
953         return rc;
954 }
955
956 /** Setup ".lustre" directory object */
957 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
958 {
959         struct dt_object *dt_dot_lustre;
960         struct lu_fid *fid = &mdd_env_info(env)->mti_fid;
961         int rc;
962
963         rc = create_dot_lustre_dir(env, m);
964         if (rc)
965                 return rc;
966
967         dt_dot_lustre = dt_store_open(env, m->mdd_child, mdd_root_dir_name,
968                                       dot_lustre_name, fid);
969         if (IS_ERR(dt_dot_lustre)) {
970                 rc = PTR_ERR(dt_dot_lustre);
971                 GOTO(out, rc);
972         }
973
974         /* references are released in mdd_device_shutdown() */
975         m->mdd_dot_lustre = lu2mdd_obj(lu_object_locate(dt_dot_lustre->do_lu.lo_header,
976                                                         &mdd_device_type));
977
978         m->mdd_dot_lustre->mod_obj.mo_dir_ops = &mdd_dot_lustre_dir_ops;
979         m->mdd_dot_lustre->mod_obj.mo_ops = &mdd_dot_lustre_obj_ops;
980
981         rc = mdd_obf_setup(env, m);
982         if (rc)
983                 CERROR("Error initializing \"fid\" object - %d.\n", rc);
984
985 out:
986         RETURN(rc);
987 }
988
989 static int mdd_process_config(const struct lu_env *env,
990                               struct lu_device *d, struct lustre_cfg *cfg)
991 {
992         struct mdd_device *m    = lu2mdd_dev(d);
993         struct dt_device  *dt   = m->mdd_child;
994         struct lu_device  *next = &dt->dd_lu_dev;
995         int rc;
996         ENTRY;
997
998         switch (cfg->lcfg_command) {
999         case LCFG_PARAM: {
1000                 struct lprocfs_static_vars lvars;
1001
1002                 lprocfs_mdd_init_vars(&lvars);
1003                 rc = class_process_proc_param(PARAM_MDD, lvars.obd_vars, cfg,m);
1004                 if (rc > 0 || rc == -ENOSYS)
1005                         /* we don't understand; pass it on */
1006                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
1007                 break;
1008         }
1009         case LCFG_SETUP:
1010                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1011                 if (rc)
1012                         GOTO(out, rc);
1013                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
1014
1015                 rc = mdd_init_obd(env, m, cfg);
1016                 if (rc) {
1017                         CERROR("lov init error %d\n", rc);
1018                         GOTO(out, rc);
1019                 }
1020
1021                 mdd_changelog_init(env, m);
1022                 break;
1023         case LCFG_CLEANUP:
1024                 lu_dev_del_linkage(d->ld_site, d);
1025                 mdd_device_shutdown(env, m, cfg);
1026         default:
1027                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1028                 break;
1029         }
1030 out:
1031         RETURN(rc);
1032 }
1033
1034 #if 0
1035 static int mdd_lov_set_nextid(const struct lu_env *env,
1036                               struct mdd_device *mdd)
1037 {
1038         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
1039         int rc;
1040         ENTRY;
1041
1042         LASSERT(mds->mds_lov_objids != NULL);
1043         rc = obd_set_info_async(mds->mds_lov_exp, strlen(KEY_NEXT_ID),
1044                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
1045                                 mds->mds_lov_objids, NULL);
1046
1047         RETURN(rc);
1048 }
1049
1050 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
1051                                    struct mdd_device *mdd)
1052 {
1053         /* XXX: to be implemented! */
1054         return 0;
1055 }
1056 #endif
1057
1058 static int mdd_recovery_complete(const struct lu_env *env,
1059                                  struct lu_device *d)
1060 {
1061         struct mdd_device *mdd = lu2mdd_dev(d);
1062         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1063         struct obd_device *obd = mdd2obd_dev(mdd);
1064         int rc;
1065         ENTRY;
1066
1067         LASSERT(mdd != NULL);
1068         LASSERT(obd != NULL);
1069 #if 0
1070         /* XXX: Do we need this in new stack? */
1071         rc = mdd_lov_set_nextid(env, mdd);
1072         if (rc) {
1073                 CERROR("mdd_lov_set_nextid() failed %d\n",
1074                        rc);
1075                 RETURN(rc);
1076         }
1077
1078         /* XXX: cleanup unlink. */
1079         rc = mdd_cleanup_unlink_llog(env, mdd);
1080         if (rc) {
1081                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
1082                        rc);
1083                 RETURN(rc);
1084         }
1085 #endif
1086         /* Call that with obd_recovering = 1 just to update objids */
1087         obd_notify(obd->u.mds.mds_lov_obd, NULL, (obd->obd_async_recov ?
1088                     OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
1089
1090         /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
1091         cfs_spin_lock(&obd->obd_dev_lock);
1092         obd->obd_recovering = 0;
1093         cfs_spin_unlock(&obd->obd_dev_lock);
1094         obd->obd_type->typ_dt_ops->o_postrecov(obd);
1095
1096         /* XXX: orphans handling. */
1097         __mdd_orphan_cleanup(env, mdd);
1098         rc = next->ld_ops->ldo_recovery_complete(env, next);
1099
1100         RETURN(rc);
1101 }
1102
1103 static int mdd_prepare(const struct lu_env *env,
1104                        struct lu_device *pdev,
1105                        struct lu_device *cdev)
1106 {
1107         struct mdd_device *mdd = lu2mdd_dev(cdev);
1108         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1109         struct dt_object *root;
1110         struct lu_fid     fid;
1111         int rc;
1112
1113         ENTRY;
1114         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1115         if (rc)
1116                 GOTO(out, rc);
1117
1118         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
1119         root = dt_store_open(env, mdd->mdd_child, "", mdd_root_dir_name,
1120                              &mdd->mdd_root_fid);
1121         if (!IS_ERR(root)) {
1122                 LASSERT(root != NULL);
1123                 lu_object_put(env, &root->do_lu);
1124                 rc = orph_index_init(env, mdd);
1125         } else {
1126                 rc = PTR_ERR(root);
1127         }
1128         if (rc)
1129                 GOTO(out, rc);
1130
1131         rc = mdd_dot_lustre_setup(env, mdd);
1132         if (rc) {
1133                 CERROR("Error(%d) initializing .lustre objects\n", rc);
1134                 GOTO(out, rc);
1135         }
1136
1137         /* we use capa file to declare llog changes,
1138          * will be fixed with new llog in 2.3 */
1139         root = dt_store_open(env, mdd->mdd_child, "", CAPA_KEYS, &fid);
1140         if (IS_ERR(root))
1141                 GOTO(out, rc = PTR_ERR(root));
1142
1143         mdd->mdd_capa = root;
1144         rc = mdd_lfsck_setup(env, mdd);
1145
1146         GOTO(out, rc);
1147
1148 out:
1149         return rc;
1150 }
1151
1152 const struct lu_device_operations mdd_lu_ops = {
1153         .ldo_object_alloc      = mdd_object_alloc,
1154         .ldo_process_config    = mdd_process_config,
1155         .ldo_recovery_complete = mdd_recovery_complete,
1156         .ldo_prepare           = mdd_prepare,
1157 };
1158
1159 /*
1160  * No permission check is needed.
1161  */
1162 static int mdd_root_get(const struct lu_env *env,
1163                         struct md_device *m, struct lu_fid *f)
1164 {
1165         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1166
1167         ENTRY;
1168         *f = mdd->mdd_root_fid;
1169         RETURN(0);
1170 }
1171
1172 /*
1173  * No permission check is needed.
1174  */
1175 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1176                       struct obd_statfs *sfs)
1177 {
1178         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1179         int rc;
1180
1181         ENTRY;
1182
1183         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1184
1185         RETURN(rc);
1186 }
1187
1188 /*
1189  * No permission check is needed.
1190  */
1191 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
1192                            int *md_size, int *cookie_size)
1193 {
1194         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1195         ENTRY;
1196
1197         *md_size = mdd_lov_mdsize(env, mdd);
1198         *cookie_size = mdd_lov_cookiesize(env, mdd);
1199
1200         RETURN(0);
1201 }
1202
1203 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
1204                               int mode, unsigned long timeout, __u32 alg,
1205                               struct lustre_capa_key *keys)
1206 {
1207         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1208         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
1209         int rc;
1210         ENTRY;
1211
1212         /* need barrier for mds_capa_keys access. */
1213         cfs_down_write(&mds->mds_notify_lock);
1214         mds->mds_capa_keys = keys;
1215         cfs_up_write(&mds->mds_notify_lock);
1216
1217         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
1218                                                    timeout, alg, keys);
1219         RETURN(rc);
1220 }
1221
1222 static int mdd_update_capa_key(const struct lu_env *env,
1223                                struct md_device *m,
1224                                struct lustre_capa_key *key)
1225 {
1226         struct mds_capa_info info = { .uuid = NULL, .capa = key };
1227         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1228         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_lov_exp;
1229         int rc;
1230         ENTRY;
1231
1232         rc = obd_set_info_async(env, lov_exp, sizeof(KEY_CAPA_KEY),
1233                                 KEY_CAPA_KEY, sizeof(info), &info, NULL);
1234         RETURN(rc);
1235 }
1236
1237 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1238                              int idx, void **h)
1239 {
1240         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1241
1242         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1243         return (*h == NULL ? -ENOENT : 0);
1244 }
1245
1246 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1247                                           struct lu_device_type *t,
1248                                           struct lustre_cfg *lcfg)
1249 {
1250         struct lu_device  *l;
1251         struct mdd_device *m;
1252
1253         OBD_ALLOC_PTR(m);
1254         if (m == NULL) {
1255                 l = ERR_PTR(-ENOMEM);
1256         } else {
1257                 md_device_init(&m->mdd_md_dev, t);
1258                 l = mdd2lu_dev(m);
1259                 l->ld_ops = &mdd_lu_ops;
1260                 m->mdd_md_dev.md_ops = &mdd_ops;
1261                 md_upcall_init(&m->mdd_md_dev, NULL);
1262         }
1263
1264         return l;
1265 }
1266
1267 static struct lu_device *mdd_device_free(const struct lu_env *env,
1268                                          struct lu_device *lu)
1269 {
1270         struct mdd_device *m = lu2mdd_dev(lu);
1271         ENTRY;
1272
1273         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
1274         md_device_fini(&m->mdd_md_dev);
1275         OBD_FREE_PTR(m);
1276         RETURN(NULL);
1277 }
1278
1279 static struct obd_ops mdd_obd_device_ops = {
1280         .o_owner = THIS_MODULE
1281 };
1282
1283 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
1284 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
1285
1286 static struct lu_context_key mdd_ucred_key = {
1287         .lct_tags = LCT_SESSION,
1288         .lct_init = mdd_ucred_key_init,
1289         .lct_fini = mdd_ucred_key_fini
1290 };
1291
1292 struct md_ucred *md_ucred(const struct lu_env *env)
1293 {
1294         LASSERT(env->le_ses != NULL);
1295         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
1296 }
1297 EXPORT_SYMBOL(md_ucred);
1298
1299 /*
1300  * context key constructor/destructor:
1301  * mdd_capainfo_key_init, mdd_capainfo_key_fini
1302  */
1303 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
1304
1305 struct lu_context_key mdd_capainfo_key = {
1306         .lct_tags = LCT_SESSION,
1307         .lct_init = mdd_capainfo_key_init,
1308         .lct_fini = mdd_capainfo_key_fini
1309 };
1310
1311 struct md_capainfo *md_capainfo(const struct lu_env *env)
1312 {
1313         /* NB, in mdt_init0 */
1314         if (env->le_ses == NULL)
1315                 return NULL;
1316         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
1317 }
1318 EXPORT_SYMBOL(md_capainfo);
1319
1320 static int mdd_changelog_user_register(struct mdd_device *mdd, int *id)
1321 {
1322         struct llog_ctxt *ctxt;
1323         struct llog_changelog_user_rec *rec;
1324         int rc;
1325         ENTRY;
1326
1327         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
1328         if (ctxt == NULL)
1329                 RETURN(-ENXIO);
1330
1331         OBD_ALLOC_PTR(rec);
1332         if (rec == NULL) {
1333                 llog_ctxt_put(ctxt);
1334                 RETURN(-ENOMEM);
1335         }
1336
1337         /* Assume we want it on since somebody registered */
1338         rc = mdd_changelog_on(mdd, 1);
1339         if (rc)
1340                 GOTO(out, rc);
1341
1342         rec->cur_hdr.lrh_len = sizeof(*rec);
1343         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1344         cfs_spin_lock(&mdd->mdd_cl.mc_user_lock);
1345         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1346                 cfs_spin_unlock(&mdd->mdd_cl.mc_user_lock);
1347                 CERROR("Maximum number of changelog users exceeded!\n");
1348                 GOTO(out, rc = -EOVERFLOW);
1349         }
1350         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1351         rec->cur_endrec = mdd->mdd_cl.mc_index;
1352         cfs_spin_unlock(&mdd->mdd_cl.mc_user_lock);
1353
1354         rc = llog_obd_add(NULL, ctxt, &rec->cur_hdr, NULL, NULL, 0);
1355
1356         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1357 out:
1358         OBD_FREE_PTR(rec);
1359         llog_ctxt_put(ctxt);
1360         RETURN(rc);
1361 }
1362
1363 int mdd_declare_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
1364                             struct thandle *handle)
1365 {
1366         int rc;
1367
1368
1369         /* XXX: this is a temporary solution to declare llog changes
1370          *      will be fixed in 2.3 with new llog implementation */
1371
1372         LASSERT(mdd->mdd_capa);
1373
1374         /* the llog record could be canceled either by modifying
1375          * the plain llog's header or by destroying the llog itself
1376          * when this record is the last one in it, it can't be known
1377          * here, but the catlog's header will also be modified for
1378          * the second case, then the first case can be covered and
1379          * is no need to declare it */
1380
1381         /* destroy empty plain log */
1382         rc = dt_declare_destroy(env, mdd->mdd_capa, handle);
1383         if (rc)
1384                 return rc;
1385
1386         /* record the catlog's header if an empty plain log was destroyed */
1387         rc = dt_declare_record_write(env, mdd->mdd_capa,
1388                                      sizeof(struct llog_logid_rec), 0, handle);
1389         return rc;
1390 }
1391
1392 struct mdd_changelog_user_data {
1393         __u64 mcud_endrec; /**< purge record for this user */
1394         __u64 mcud_minrec; /**< lowest changelog recno still referenced */
1395         __u32 mcud_id;
1396         __u32 mcud_minid;  /**< user id with lowest rec reference */
1397         __u32 mcud_usercount;
1398         int   mcud_found:1;
1399         struct mdd_device   *mcud_mdd;
1400 };
1401 #define MCUD_UNREGISTER -1LL
1402
1403 /** Two things:
1404  * 1. Find the smallest record everyone is willing to purge
1405  * 2. Update the last purgeable record for this user
1406  */
1407 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1408                                        struct llog_handle *llh,
1409                                        struct llog_rec_hdr *hdr, void *data)
1410 {
1411         struct llog_changelog_user_rec  *rec;
1412         struct mdd_changelog_user_data  *mcud = data;
1413         int                              rc;
1414
1415         ENTRY;
1416
1417         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
1418
1419         rec = (struct llog_changelog_user_rec *)hdr;
1420
1421         mcud->mcud_usercount++;
1422
1423         /* If we have a new endrec for this id, use it for the following
1424            min check instead of its old value */
1425         if (rec->cur_id == mcud->mcud_id)
1426                 rec->cur_endrec = max(rec->cur_endrec, mcud->mcud_endrec);
1427
1428         /* Track the minimum referenced record */
1429         if (mcud->mcud_minid == 0 || mcud->mcud_minrec > rec->cur_endrec) {
1430                 mcud->mcud_minid = rec->cur_id;
1431                 mcud->mcud_minrec = rec->cur_endrec;
1432         }
1433
1434         if (rec->cur_id != mcud->mcud_id)
1435                 RETURN(0);
1436
1437         /* Update this user's record */
1438         mcud->mcud_found = 1;
1439
1440         /* Special case: unregister this user */
1441         if (mcud->mcud_endrec == MCUD_UNREGISTER) {
1442                 struct llog_cookie cookie;
1443                 void *th;
1444                 struct mdd_device *mdd = mcud->mcud_mdd;
1445
1446                 cookie.lgc_lgl = llh->lgh_id;
1447                 cookie.lgc_index = hdr->lrh_index;
1448
1449                 /* XXX This is a workaround for the deadlock of changelog
1450                  * adding vs. changelog cancelling. LU-81. */
1451                 th = mdd_trans_create(env, mdd);
1452                 if (IS_ERR(th)) {
1453                         CERROR("Cannot get thandle\n");
1454                         RETURN(-ENOMEM);
1455                 }
1456
1457                 rc = mdd_declare_llog_cancel(env, mdd, th);
1458                 if (rc)
1459                         GOTO(stop, rc);
1460
1461                 rc = mdd_trans_start(env, mdd, th);
1462                 if (rc)
1463                         GOTO(stop, rc);
1464
1465                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1466                                              1, &cookie);
1467                 if (rc == 0)
1468                         mcud->mcud_usercount--;
1469
1470 stop:
1471                 mdd_trans_stop(env, mdd, 0, th);
1472                 RETURN(rc);
1473         }
1474
1475         /* Update the endrec */
1476         CDEBUG(D_IOCTL, "Rewriting changelog user %d endrec to "LPU64"\n",
1477                mcud->mcud_id, rec->cur_endrec);
1478
1479         /* hdr+1 is loc of data */
1480         hdr->lrh_len -= sizeof(*hdr) + sizeof(struct llog_rec_tail);
1481         rc = llog_write_rec(env, llh, hdr, NULL, 0, (void *)(hdr + 1),
1482                             hdr->lrh_index, NULL);
1483
1484         RETURN(rc);
1485 }
1486
1487 static int mdd_changelog_user_purge(const struct lu_env *env,
1488                                     struct mdd_device *mdd, int id,
1489                                     long long endrec)
1490 {
1491         struct mdd_changelog_user_data data;
1492         struct llog_ctxt *ctxt;
1493         int rc;
1494         ENTRY;
1495
1496         CDEBUG(D_IOCTL, "Purge request: id=%d, endrec=%lld\n", id, endrec);
1497
1498         data.mcud_id = id;
1499         data.mcud_minid = 0;
1500         data.mcud_minrec = 0;
1501         data.mcud_usercount = 0;
1502         data.mcud_endrec = endrec;
1503         data.mcud_mdd = mdd;
1504         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
1505         endrec = mdd->mdd_cl.mc_index;
1506         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
1507         if ((data.mcud_endrec == 0) ||
1508             ((data.mcud_endrec > endrec) &&
1509              (data.mcud_endrec != MCUD_UNREGISTER)))
1510                 data.mcud_endrec = endrec;
1511
1512         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
1513         if (ctxt == NULL)
1514                 return -ENXIO;
1515         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
1516
1517         rc = llog_cat_process(env, ctxt->loc_handle,
1518                               mdd_changelog_user_purge_cb, (void *)&data,
1519                               0, 0);
1520         if ((rc >= 0) && (data.mcud_minrec > 0)) {
1521                 CDEBUG(D_IOCTL, "Purging changelog entries up to "LPD64
1522                        ", referenced by "CHANGELOG_USER_PREFIX"%d\n",
1523                        data.mcud_minrec, data.mcud_minid);
1524                 rc = mdd_changelog_llog_cancel(env, mdd, data.mcud_minrec);
1525         } else {
1526                 CWARN("Could not determine changelog records to purge; rc=%d\n",
1527                       rc);
1528         }
1529
1530         llog_ctxt_put(ctxt);
1531
1532         if (!data.mcud_found) {
1533                 CWARN("No entry for user %d.  Last changelog reference is "
1534                       LPD64" by changelog user %d\n", data.mcud_id,
1535                       data.mcud_minrec, data.mcud_minid);
1536                rc = -ENOENT;
1537         }
1538
1539         if (!rc && data.mcud_usercount == 0)
1540                 /* No more users; turn changelogs off */
1541                 rc = mdd_changelog_on(mdd, 0);
1542
1543         RETURN (rc);
1544 }
1545
1546 /** mdd_iocontrol
1547  * May be called remotely from mdt_iocontrol_handle or locally from
1548  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1549  * an obd_ioctl_data format (but local ioctl handler does).
1550  * \param cmd - ioc
1551  * \param len - data len
1552  * \param karg - ioctl data, in kernel space
1553  */
1554 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1555                          unsigned int cmd, int len, void *karg)
1556 {
1557         struct mdd_device *mdd;
1558         struct obd_ioctl_data *data = karg;
1559         int rc;
1560         ENTRY;
1561
1562         mdd = lu2mdd_dev(&m->md_lu_dev);
1563
1564         /* Doesn't use obd_ioctl_data */
1565         switch (cmd) {
1566         case OBD_IOC_CHANGELOG_CLEAR: {
1567                 struct changelog_setinfo *cs = karg;
1568                 rc = mdd_changelog_user_purge(env, mdd, cs->cs_id,
1569                                               cs->cs_recno);
1570                 RETURN(rc);
1571         }
1572         case OBD_IOC_GET_MNTOPT: {
1573                 mntopt_t *mntopts = (mntopt_t *)karg;
1574                 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1575                 RETURN(0);
1576         }
1577         case OBD_IOC_START_LFSCK: {
1578                 struct lfsck_start *start = karg;
1579                 struct md_lfsck *lfsck = &mdd->mdd_lfsck;
1580
1581                 /* Return the kernel service version. */
1582                 /* XXX: version can be used for compatibility in the future. */
1583                 start->ls_version = lfsck->ml_version;
1584                 rc = mdd_lfsck_start(env, lfsck, start);
1585                 RETURN(rc);
1586         }
1587         case OBD_IOC_STOP_LFSCK: {
1588                 rc = mdd_lfsck_stop(env, &mdd->mdd_lfsck);
1589                 RETURN(rc);
1590         }
1591         }
1592
1593         /* Below ioctls use obd_ioctl_data */
1594         if (len != sizeof(*data)) {
1595                 CERROR("Bad ioctl size %d\n", len);
1596                 RETURN(-EINVAL);
1597         }
1598         if (data->ioc_version != OBD_IOCTL_VERSION) {
1599                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1600                        OBD_IOCTL_VERSION);
1601                 RETURN(-EINVAL);
1602         }
1603
1604         switch (cmd) {
1605         case OBD_IOC_CHANGELOG_REG:
1606                 rc = mdd_changelog_user_register(mdd, &data->ioc_u32_1);
1607                 break;
1608         case OBD_IOC_CHANGELOG_DEREG:
1609                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1,
1610                                               MCUD_UNREGISTER);
1611                 break;
1612         default:
1613                 rc = -ENOTTY;
1614         }
1615
1616         RETURN (rc);
1617 }
1618
1619 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1620 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key);
1621
1622 const struct md_device_operations mdd_ops = {
1623         .mdo_statfs         = mdd_statfs,
1624         .mdo_root_get       = mdd_root_get,
1625         .mdo_maxsize_get    = mdd_maxsize_get,
1626         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
1627         .mdo_update_capa_key= mdd_update_capa_key,
1628         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1629         .mdo_iocontrol      = mdd_iocontrol,
1630 };
1631
1632 static struct lu_device_type_operations mdd_device_type_ops = {
1633         .ldto_init = mdd_type_init,
1634         .ldto_fini = mdd_type_fini,
1635
1636         .ldto_start = mdd_type_start,
1637         .ldto_stop  = mdd_type_stop,
1638
1639         .ldto_device_alloc = mdd_device_alloc,
1640         .ldto_device_free  = mdd_device_free,
1641
1642         .ldto_device_init    = mdd_device_init,
1643         .ldto_device_fini    = mdd_device_fini
1644 };
1645
1646 static struct lu_device_type mdd_device_type = {
1647         .ldt_tags     = LU_DEVICE_MD,
1648         .ldt_name     = LUSTRE_MDD_NAME,
1649         .ldt_ops      = &mdd_device_type_ops,
1650         .ldt_ctx_tags = LCT_MD_THREAD
1651 };
1652
1653 /* context key constructor: mdd_key_init */
1654 LU_KEY_INIT(mdd, struct mdd_thread_info);
1655
1656 static void mdd_key_fini(const struct lu_context *ctx,
1657                          struct lu_context_key *key, void *data)
1658 {
1659         struct mdd_thread_info *info = data;
1660         if (info->mti_max_lmm != NULL)
1661                 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
1662         if (info->mti_max_cookie != NULL)
1663                 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
1664         mdd_buf_put(&info->mti_big_buf);
1665
1666         OBD_FREE_PTR(info);
1667 }
1668
1669 /* context key: mdd_thread_key */
1670 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1671
1672 static struct lu_local_obj_desc llod_capa_key = {
1673         .llod_name      = CAPA_KEYS,
1674         .llod_oid       = MDD_CAPA_KEYS_OID,
1675         .llod_is_index  = 0,
1676 };
1677
1678 static struct lu_local_obj_desc llod_mdd_orphan = {
1679         .llod_name      = orph_index_name,
1680         .llod_oid       = MDD_ORPHAN_OID,
1681         .llod_is_index  = 1,
1682         .llod_feat      = &dt_directory_features,
1683 };
1684
1685 static struct lu_local_obj_desc llod_mdd_root = {
1686         .llod_name      = mdd_root_dir_name,
1687         .llod_oid       = MDD_ROOT_INDEX_OID,
1688         .llod_is_index  = 1,
1689         .llod_feat      = &dt_directory_features,
1690 };
1691
1692 static struct lu_local_obj_desc llod_lfsck_bookmark = {
1693         .llod_name      = lfsck_bookmark_name,
1694         .llod_oid       = LFSCK_BOOKMARK_OID,
1695         .llod_is_index  = 0,
1696 };
1697
1698 static int __init mdd_mod_init(void)
1699 {
1700         struct lprocfs_static_vars lvars;
1701         int rc;
1702
1703         lprocfs_mdd_init_vars(&lvars);
1704
1705         rc = lu_kmem_init(mdd_caches);
1706         if (rc)
1707                 return rc;
1708
1709         llo_local_obj_register(&llod_capa_key);
1710         llo_local_obj_register(&llod_mdd_orphan);
1711         llo_local_obj_register(&llod_mdd_root);
1712         llo_local_obj_register(&llod_lfsck_bookmark);
1713
1714         rc = class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
1715                                  LUSTRE_MDD_NAME, &mdd_device_type);
1716         if (rc)
1717                 lu_kmem_fini(mdd_caches);
1718         return rc;
1719 }
1720
1721 static void __exit mdd_mod_exit(void)
1722 {
1723         llo_local_obj_unregister(&llod_capa_key);
1724         llo_local_obj_unregister(&llod_mdd_orphan);
1725         llo_local_obj_unregister(&llod_mdd_root);
1726         llo_local_obj_unregister(&llod_lfsck_bookmark);
1727
1728         class_unregister_type(LUSTRE_MDD_NAME);
1729         lu_kmem_fini(mdd_caches);
1730 }
1731
1732 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1733 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
1734 MODULE_LICENSE("GPL");
1735
1736 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);