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