Whamcloud - gitweb
71e5d1339b8fcf6df09ae294d5814ce80fe41864
[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, 2014, Intel Corporation.
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 <linux/module.h>
46 #include <obd_class.h>
47 #include <lustre_ioctl.h>
48 #include <lustre_mds.h>
49 #include <obd_support.h>
50 #include <lu_object.h>
51 #include <lustre_param.h>
52 #include <lustre_fid.h>
53
54 #include "mdd_internal.h"
55
56 static const struct md_device_operations mdd_ops;
57 static struct lu_device_type mdd_device_type;
58
59 static const char mdd_root_dir_name[] = "ROOT";
60 static const char mdd_obf_dir_name[] = "fid";
61 static const char mdd_lpf_dir_name[] = "lost+found";
62
63 /* Slab for MDD object allocation */
64 struct kmem_cache *mdd_object_kmem;
65
66 static struct lu_kmem_descr mdd_caches[] = {
67         {
68                 .ckd_cache = &mdd_object_kmem,
69                 .ckd_name  = "mdd_obj",
70                 .ckd_size  = sizeof(struct mdd_object)
71         },
72         {
73                 .ckd_cache = NULL
74         }
75 };
76
77 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
78                                const char *nextdev)
79 {
80         struct obd_connect_data *data = NULL;
81         struct lu_device        *lud = mdd2lu_dev(m);
82         struct obd_device       *obd;
83         int                      rc;
84         ENTRY;
85
86         LASSERT(m->mdd_child_exp == NULL);
87
88         OBD_ALLOC(data, sizeof(*data));
89         if (data == NULL)
90                 GOTO(out, rc = -ENOMEM);
91
92         obd = class_name2obd(nextdev);
93         if (obd == NULL) {
94                 CERROR("can't locate next device: %s\n", nextdev);
95                 GOTO(out, rc = -ENOTCONN);
96         }
97
98         data->ocd_connect_flags = OBD_CONNECT_VERSION;
99         data->ocd_version = LUSTRE_VERSION_CODE;
100
101         rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
102         if (rc) {
103                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
104                 GOTO(out, rc);
105         }
106
107         lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
108         LASSERT(lud->ld_site);
109         m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
110         m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
111         lu_dev_add_linkage(lud->ld_site, lud);
112
113 out:
114         if (data)
115                 OBD_FREE(data, sizeof(*data));
116         RETURN(rc);
117 }
118
119 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
120                 struct lu_device_type *t, struct lustre_cfg *lcfg)
121 {
122         int rc = -EINVAL;
123         const char *dev;
124         ENTRY;
125
126         dev = lustre_cfg_string(lcfg, 0);
127         if (dev == NULL)
128                 RETURN(rc);
129
130         mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
131         if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
132                 RETURN(rc);
133         mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
134         mdd->mdd_md_dev.md_ops = &mdd_ops;
135
136         rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
137         if (rc != 0)
138                 RETURN(rc);
139
140         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
141         /* sync permission changes */
142         mdd->mdd_sync_permission = 1;
143
144         dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
145
146         /* we are using service name but not mdd obd name
147          * for compatibility reasons.
148          * It is passed from MDT in lustre_cfg[2] buffer */
149         rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
150         if (rc < 0)
151                 obd_disconnect(mdd->mdd_child_exp);
152
153         RETURN(rc);
154 }
155
156 static struct lu_device *mdd_device_fini(const struct lu_env *env,
157                                          struct lu_device *d)
158 {
159         struct mdd_device *mdd = lu2mdd_dev(d);
160
161         if (d->ld_site)
162                 lu_dev_del_linkage(d->ld_site, d);
163
164         mdd_procfs_fini(mdd);
165         return NULL;
166 }
167
168 static int changelog_init_cb(const struct lu_env *env, 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_rec *rec = (struct llog_changelog_rec *)hdr;
173
174         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
175         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
176
177         CDEBUG(D_INFO,
178                "seeing record at index %d/%d/"LPU64" t=%x %.*s in log"
179                DOSTID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
180                rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
181                changelog_rec_name(&rec->cr), POSTID(&llh->lgh_id.lgl_oi));
182
183         mdd->mdd_cl.mc_index = rec->cr.cr_index;
184         return LLOG_PROC_BREAK;
185 }
186
187 static int changelog_user_init_cb(const struct lu_env *env,
188                                   struct llog_handle *llh,
189                                   struct llog_rec_hdr *hdr, void *data)
190 {
191         struct mdd_device *mdd = (struct mdd_device *)data;
192         struct llog_changelog_user_rec *rec =
193                 (struct llog_changelog_user_rec *)hdr;
194
195         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
196         LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
197
198         CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec="LPU64
199                " in log "DOSTID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
200                rec->cur_id, rec->cur_endrec, POSTID(&llh->lgh_id.lgl_oi));
201
202         spin_lock(&mdd->mdd_cl.mc_user_lock);
203         mdd->mdd_cl.mc_lastuser = rec->cur_id;
204         if (rec->cur_endrec > mdd->mdd_cl.mc_index)
205                 mdd->mdd_cl.mc_index = rec->cur_endrec;
206         spin_unlock(&mdd->mdd_cl.mc_user_lock);
207
208         return LLOG_PROC_BREAK;
209 }
210
211 static int llog_changelog_cancel_cb(const struct lu_env *env,
212                                     struct llog_handle *llh,
213                                     struct llog_rec_hdr *hdr, void *data)
214 {
215         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
216         struct llog_cookie       cookie;
217         long long                endrec = *(long long *)data;
218         int                      rc;
219
220         ENTRY;
221
222         /* This is always a (sub)log, not the catalog */
223         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
224
225         if (rec->cr.cr_index > endrec)
226                 /* records are in order, so we're done */
227                 RETURN(LLOG_PROC_BREAK);
228
229         cookie.lgc_lgl = llh->lgh_id;
230         cookie.lgc_index = hdr->lrh_index;
231
232         /* cancel them one at a time.  I suppose we could store up the cookies
233          * and cancel them all at once; probably more efficient, but this is
234          * done as a user call, so who cares... */
235         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
236                                      &cookie);
237         RETURN(rc < 0 ? rc : 0);
238 }
239
240 static int llog_changelog_cancel(const struct lu_env *env,
241                                  struct llog_ctxt *ctxt,
242                                  struct llog_cookie *cookies, int flags)
243 {
244         struct llog_handle      *cathandle = ctxt->loc_handle;
245         int                      rc;
246
247         ENTRY;
248
249         /* This should only be called with the catalog handle */
250         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
251
252         rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
253                               (void *)cookies, 0, 0);
254         if (rc >= 0)
255                 /* 0 or 1 means we're done */
256                 rc = 0;
257         else
258                 CERROR("%s: cancel idx %u of catalog "DOSTID" rc=%d\n",
259                        ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
260                        POSTID(&cathandle->lgh_id.lgl_oi), rc);
261
262         RETURN(rc);
263 }
264
265 static struct llog_operations changelog_orig_logops;
266
267 static int
268 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd, int on);
269
270 static int mdd_changelog_llog_init(const struct lu_env *env,
271                                    struct mdd_device *mdd)
272 {
273         struct obd_device       *obd = mdd2obd_dev(mdd);
274         struct llog_ctxt        *ctxt = NULL, *uctxt = NULL;
275         int                      rc;
276
277         ENTRY;
278
279         /* LU-2844 mdd setup failure should not cause umount oops */
280         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
281                 RETURN(-EIO);
282
283         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
284         obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
285         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
286                         obd, &changelog_orig_logops);
287         if (rc) {
288                 CERROR("%s: changelog llog setup failed: rc = %d\n",
289                        obd->obd_name, rc);
290                 RETURN(rc);
291         }
292
293         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
294         LASSERT(ctxt);
295
296         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
297                               CHANGELOG_CATALOG);
298         if (rc)
299                 GOTO(out_cleanup, rc);
300
301         rc = llog_cat_init_and_process(env, ctxt->loc_handle);
302         if (rc)
303                 GOTO(out_close, rc);
304
305         rc = llog_cat_reverse_process(env, ctxt->loc_handle,
306                                       changelog_init_cb, mdd);
307
308         if (rc < 0) {
309                 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
310                        rc);
311                 GOTO(out_close, rc);
312         }
313
314         CDEBUG(D_IOCTL, "changelog starting index="LPU64"\n",
315                mdd->mdd_cl.mc_index);
316
317         /* setup user changelog */
318         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
319                         obd, &changelog_orig_logops);
320         if (rc) {
321                 CERROR("%s: changelog users llog setup failed: rc = %d\n",
322                        obd->obd_name, rc);
323                 GOTO(out_close, rc);
324         }
325
326         uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
327         LASSERT(ctxt);
328
329         rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
330                               CHANGELOG_USERS);
331         if (rc)
332                 GOTO(out_ucleanup, rc);
333
334         uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
335         uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
336
337         rc = llog_cat_init_and_process(env, uctxt->loc_handle);
338         if (rc)
339                 GOTO(out_uclose, rc);
340
341         rc = llog_cat_reverse_process(env, uctxt->loc_handle,
342                                       changelog_user_init_cb, mdd);
343         if (rc < 0) {
344                 CERROR("%s: changelog user init failed: rc = %d\n",
345                        obd->obd_name, rc);
346                 GOTO(out_uclose, rc);
347         }
348
349         /* If we have registered users, assume we want changelogs on */
350         if (mdd->mdd_cl.mc_lastuser > 0) {
351                 rc = mdd_changelog_on(env, mdd, 1);
352                 if (rc < 0)
353                         GOTO(out_uclose, rc);
354         }
355         llog_ctxt_put(ctxt);
356         llog_ctxt_put(uctxt);
357         RETURN(0);
358 out_uclose:
359         llog_cat_close(env, uctxt->loc_handle);
360 out_ucleanup:
361         llog_cleanup(env, uctxt);
362 out_close:
363         llog_cat_close(env, ctxt->loc_handle);
364 out_cleanup:
365         llog_cleanup(env, ctxt);
366         return rc;
367 }
368
369 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
370 {
371         struct obd_device       *obd = mdd2obd_dev(mdd);
372         int                      rc;
373
374         mdd->mdd_cl.mc_index = 0;
375         spin_lock_init(&mdd->mdd_cl.mc_lock);
376         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
377         mdd->mdd_cl.mc_flags = 0; /* off by default */
378         mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
379         spin_lock_init(&mdd->mdd_cl.mc_user_lock);
380         mdd->mdd_cl.mc_lastuser = 0;
381
382         rc = mdd_changelog_llog_init(env, mdd);
383         if (rc) {
384                 CERROR("%s: changelog setup during init failed: rc = %d\n",
385                        obd->obd_name, rc);
386                 mdd->mdd_cl.mc_flags |= CLM_ERR;
387         }
388
389         return rc;
390 }
391
392 static void mdd_changelog_fini(const struct lu_env *env,
393                                struct mdd_device *mdd)
394 {
395         struct obd_device       *obd = mdd2obd_dev(mdd);
396         struct llog_ctxt        *ctxt;
397
398         mdd->mdd_cl.mc_flags = 0;
399
400         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
401         if (ctxt) {
402                 llog_cat_close(env, ctxt->loc_handle);
403                 llog_cleanup(env, ctxt);
404         }
405         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
406         if (ctxt) {
407                 llog_cat_close(env, ctxt->loc_handle);
408                 llog_cleanup(env, ctxt);
409         }
410 }
411
412 static int
413 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
414                            int markerflags);
415
416 /* Start / stop recording */
417 static int
418 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd, int on)
419 {
420         int rc = 0;
421
422         if ((on == 1) && ((mdd->mdd_cl.mc_flags & CLM_ON) == 0)) {
423                 LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
424                 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
425                         CERROR("Changelogs cannot be enabled due to error "
426                                "condition (see %s log).\n",
427                                mdd2obd_dev(mdd)->obd_name);
428                         rc = -ESRCH;
429                 } else {
430                         spin_lock(&mdd->mdd_cl.mc_lock);
431                         mdd->mdd_cl.mc_flags |= CLM_ON;
432                         spin_unlock(&mdd->mdd_cl.mc_lock);
433                         rc = mdd_changelog_write_header(env, mdd, CLM_START);
434                 }
435         } else if ((on == 0) && ((mdd->mdd_cl.mc_flags & CLM_ON) == CLM_ON)) {
436                 LCONSOLE_INFO("%s: changelog off\n",mdd2obd_dev(mdd)->obd_name);
437                 rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
438                 spin_lock(&mdd->mdd_cl.mc_lock);
439                 mdd->mdd_cl.mc_flags &= ~CLM_ON;
440                 spin_unlock(&mdd->mdd_cl.mc_lock);
441         }
442         return rc;
443 }
444
445 /** Remove entries with indicies up to and including \a endrec from the
446  *  changelog
447  * \param mdd
448  * \param endrec
449  * \retval 0 ok
450  */
451 static int
452 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
453                           long long endrec)
454 {
455         struct obd_device *obd = mdd2obd_dev(mdd);
456         struct llog_ctxt *ctxt;
457         long long unsigned cur;
458         int rc;
459
460         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
461         if (ctxt == NULL)
462                 return -ENXIO;
463
464         spin_lock(&mdd->mdd_cl.mc_lock);
465         cur = (long long)mdd->mdd_cl.mc_index;
466         spin_unlock(&mdd->mdd_cl.mc_lock);
467         if (endrec > cur)
468                 endrec = cur;
469
470         /* purge to "0" is shorthand for everything */
471         if (endrec == 0)
472                 endrec = cur;
473
474         /* If purging all records, write a header entry so we don't have an
475            empty catalog and we're sure to have a valid starting index next
476            time.  In case of crash, we just restart with old log so we're
477            allright. */
478         if (endrec == cur) {
479                 /* XXX: transaction is started by llog itself */
480                 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
481                 if (rc)
482                       goto out;
483         }
484
485         /* Some records were purged, so reset repeat-access time (so we
486            record new mtime update records, so users can see a file has been
487            changed since the last purge) */
488         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
489
490         rc = llog_cancel(env, ctxt, (struct llog_cookie *)&endrec, 0);
491 out:
492         llog_ctxt_put(ctxt);
493         return rc;
494 }
495
496 /** Add a CL_MARK record to the changelog
497  * \param mdd
498  * \param markerflags - CLM_*
499  * \retval 0 ok
500  */
501 int mdd_changelog_write_header(const struct lu_env *env,
502                                struct mdd_device *mdd, int markerflags)
503 {
504         struct obd_device               *obd = mdd2obd_dev(mdd);
505         struct llog_changelog_rec       *rec;
506         struct lu_buf                   *buf;
507         struct llog_ctxt                *ctxt;
508         int                              reclen;
509         int                              len = strlen(obd->obd_name);
510         int                              rc;
511
512         ENTRY;
513
514         if (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) {
515                 mdd->mdd_cl.mc_starttime = cfs_time_current_64();
516                 RETURN(0);
517         }
518
519         reclen = llog_data_len(sizeof(*rec) + len);
520         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
521         if (buf->lb_buf == NULL)
522                 RETURN(-ENOMEM);
523         rec = buf->lb_buf;
524
525         rec->cr.cr_flags = CLF_VERSION;
526         rec->cr.cr_type = CL_MARK;
527         rec->cr.cr_namelen = len;
528         memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
529         /* Status and action flags */
530         rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
531         rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
532                                             rec->cr.cr_namelen);
533         rec->cr_hdr.lrh_type = CHANGELOG_REC;
534         rec->cr.cr_time = cl_time();
535         spin_lock(&mdd->mdd_cl.mc_lock);
536         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
537         spin_unlock(&mdd->mdd_cl.mc_lock);
538
539         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
540         LASSERT(ctxt);
541
542         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
543         if (rc > 0)
544                 rc = 0;
545         llog_ctxt_put(ctxt);
546
547         /* assume on or off event; reset repeat-access time */
548         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
549         RETURN(rc);
550 }
551
552 /**
553  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
554  * are valid. We also check if object with passed fid exists or not.
555  */
556 static int obf_lookup(const struct lu_env *env, struct md_object *p,
557                       const struct lu_name *lname, struct lu_fid *f,
558                       struct md_op_spec *spec)
559 {
560         char *name = (char *)lname->ln_name;
561         struct mdd_device *mdd = mdo2mdd(p);
562         struct mdd_object *child;
563         int rc = 0;
564
565         while (*name == '[')
566                 name++;
567
568         sscanf(name, SFID, RFID(f));
569         if (!fid_is_sane(f)) {
570                 CWARN("%s: Trying to lookup invalid FID [%s] in %s/%s, FID "
571                       "format should be "DFID"\n", mdd2obd_dev(mdd)->obd_name,
572                       lname->ln_name, dot_lustre_name, mdd_obf_dir_name,
573                       (__u64)FID_SEQ_NORMAL, 1, 0);
574                 GOTO(out, rc = -EINVAL);
575         }
576
577         if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
578             !fid_seq_is_dot(f->f_seq)) {
579                 CWARN("%s: Trying to lookup invalid FID "DFID" in %s/%s, "
580                       "sequence should be >= "LPX64" or within ["LPX64","
581                       ""LPX64"].\n", mdd2obd_dev(mdd)->obd_name, PFID(f),
582                       dot_lustre_name, mdd_obf_dir_name, (__u64)FID_SEQ_NORMAL,
583                       (__u64)FID_SEQ_IGIF, (__u64)FID_SEQ_IGIF_MAX);
584                 GOTO(out, rc = -EINVAL);
585         }
586
587         /* Check if object with this fid exists */
588         child = mdd_object_find(env, mdd, f);
589         if (IS_ERR(child))
590                 GOTO(out, rc = PTR_ERR(child));
591
592         if (mdd_object_exists(child) == 0)
593                 rc = -ENOENT;
594
595         mdd_object_put(env, child);
596
597 out:
598         return rc;
599 }
600
601 static int mdd_dummy_create(const struct lu_env *env,
602                             struct md_object *pobj,
603                             const struct lu_name *lname,
604                             struct md_object *child,
605                             struct md_op_spec *spec,
606                             struct md_attr* ma)
607 {
608         return -EPERM;
609 }
610
611 static int mdd_dummy_rename(const struct lu_env *env,
612                             struct md_object *src_pobj,
613                             struct md_object *tgt_pobj,
614                             const struct lu_fid *lf,
615                             const struct lu_name *lsname,
616                             struct md_object *tobj,
617                             const struct lu_name *ltname,
618                             struct md_attr *ma)
619 {
620         return -EPERM;
621 }
622
623 static int mdd_dummy_link(const struct lu_env *env,
624                           struct md_object *tgt_obj,
625                           struct md_object *src_obj,
626                           const struct lu_name *lname,
627                           struct md_attr *ma)
628 {
629         return -EPERM;
630 }
631
632 static int mdd_dummy_unlink(const struct lu_env *env,
633                             struct md_object *pobj,
634                             struct md_object *cobj,
635                             const struct lu_name *lname,
636                             struct md_attr *ma,
637                             int no_name)
638 {
639         return -EPERM;
640 }
641
642 static struct md_dir_operations mdd_obf_dir_ops = {
643         .mdo_lookup = obf_lookup,
644         .mdo_create = mdd_dummy_create,
645         .mdo_rename = mdd_dummy_rename,
646         .mdo_link   = mdd_dummy_link,
647         .mdo_unlink = mdd_dummy_unlink
648 };
649
650 static struct md_dir_operations mdd_lpf_dir_ops = {
651         .mdo_lookup = mdd_lookup,
652         .mdo_create = mdd_dummy_create,
653         .mdo_rename = mdd_dummy_rename,
654         .mdo_link   = mdd_dummy_link,
655         .mdo_unlink = mdd_dummy_unlink
656 };
657
658 static struct md_object *mdo_locate(const struct lu_env *env,
659                                     struct md_device *md,
660                                     const struct lu_fid *fid)
661 {
662         struct lu_object *obj;
663         struct md_object *mdo;
664
665         obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
666         if (!IS_ERR(obj)) {
667                 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
668                 LASSERT(obj != NULL);
669                 mdo = lu2md(obj);
670         } else {
671                 mdo = ERR_PTR(PTR_ERR(obj));
672         }
673         return mdo;
674 }
675
676 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
677 {
678         struct md_object        *mdo;
679         struct mdd_object       *mdd_lpf;
680         struct lu_fid            fid    = LU_LPF_FID;
681         int                      rc;
682         ENTRY;
683
684         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
685                                    mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
686                                    &fid);
687         if (rc != 0)
688                 RETURN(rc);
689
690         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
691         if (IS_ERR(mdo))
692                 RETURN(PTR_ERR(mdo));
693
694         LASSERT(lu_object_exists(&mdo->mo_lu));
695
696         mdd_lpf = md2mdd_obj(mdo);
697         mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
698         m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
699
700         RETURN(0);
701 }
702
703 /**
704  * Create special in-memory "fid" object for open-by-fid.
705  */
706 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
707 {
708         struct md_object        *mdo;
709         struct mdd_object       *mdd_obf;
710         struct lu_fid            fid = LU_OBF_FID;
711         int                      rc;
712
713         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
714                                    mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
715         if (rc < 0)
716                 RETURN(rc);
717
718         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
719         if (IS_ERR(mdo))
720                 RETURN(PTR_ERR(mdo));
721
722         LASSERT(lu_object_exists(&mdo->mo_lu));
723
724         mdd_obf = md2mdd_obj(mdo);
725         mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
726         m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
727
728         return 0;
729 }
730
731 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
732                                    struct mdd_device *m)
733 {
734         if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
735                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
736                 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
737         }
738         if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
739                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
740                 m->mdd_dot_lustre_objs.mdd_obf = NULL;
741         }
742         if (m->mdd_dot_lustre != NULL) {
743                 mdd_object_put(env, m->mdd_dot_lustre);
744                 m->mdd_dot_lustre = NULL;
745         }
746 }
747
748 /** Setup ".lustre" directory object */
749 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
750 {
751         struct md_object        *mdo;
752         struct lu_fid            fid;
753         int                      rc;
754
755         ENTRY;
756         /* Create ".lustre" directory in ROOT. */
757         fid = LU_DOT_LUSTRE_FID;
758         rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
759                                    dot_lustre_name,
760                                    S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
761                                    &fid);
762         if (rc < 0)
763                 RETURN(rc);
764         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
765         if (IS_ERR(mdo))
766                 RETURN(PTR_ERR(mdo));
767         LASSERT(lu_object_exists(&mdo->mo_lu));
768
769         m->mdd_dot_lustre = md2mdd_obj(mdo);
770
771         rc = mdd_obf_setup(env, m);
772         if (rc) {
773                 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
774                        mdd2obd_dev(m)->obd_name, rc);
775                 GOTO(out, rc);
776         }
777
778         rc = mdd_lpf_setup(env, m);
779         if (rc != 0) {
780                 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
781                        mdd2obd_dev(m)->obd_name, rc);
782                 GOTO(out, rc);
783         }
784
785         RETURN(0);
786
787 out:
788         mdd_dot_lustre_cleanup(env, m);
789
790         return rc;
791 }
792
793
794 static struct llog_operations hsm_actions_logops;
795
796 /**
797  * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
798  * object in obd_device
799  */
800 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
801                                      struct mdd_device *m)
802 {
803         struct obd_device       *obd = mdd2obd_dev(m);
804         struct llog_ctxt        *ctxt = NULL;
805         int                      rc;
806         ENTRY;
807
808         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
809         obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
810
811         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
812                         obd, &hsm_actions_logops);
813         if (rc) {
814                 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
815                         obd->obd_name, rc);
816                 RETURN(rc);
817         }
818
819         ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
820         LASSERT(ctxt);
821
822         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
823                               HSM_ACTIONS);
824         if (rc) {
825                 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
826                         obd->obd_name, rc);
827                 GOTO(out_cleanup, rc);
828         }
829
830         rc = llog_cat_init_and_process(env, ctxt->loc_handle);
831         if (rc)
832                 GOTO(out_close, rc);
833
834         llog_ctxt_put(ctxt);
835         RETURN(0);
836
837 out_close:
838         llog_cat_close(env, ctxt->loc_handle);
839         ctxt->loc_handle = NULL;
840 out_cleanup:
841         llog_cleanup(env, ctxt);
842
843         return rc;
844 }
845
846 /**
847  * cleanup the context created by llog_setup_named()
848  */
849 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
850                                      struct mdd_device *m)
851 {
852         struct obd_device       *obd = mdd2obd_dev(m);
853         struct llog_ctxt        *lctxt;
854         ENTRY;
855
856         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
857         if (lctxt) {
858                 llog_cat_close(env, lctxt->loc_handle);
859                 lctxt->loc_handle = NULL;
860                 llog_cleanup(env, lctxt);
861         }
862
863         RETURN(0);
864 }
865
866 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
867                                 struct lustre_cfg *cfg)
868 {
869         mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
870         lfsck_degister(env, m->mdd_bottom);
871         mdd_hsm_actions_llog_fini(env, m);
872         mdd_changelog_fini(env, m);
873         orph_index_fini(env, m);
874         mdd_dot_lustre_cleanup(env, m);
875         if (m->mdd_los != NULL) {
876                 local_oid_storage_fini(env, m->mdd_los);
877                 m->mdd_los = NULL;
878         }
879         lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
880
881         if (m->mdd_child_exp)
882                 obd_disconnect(m->mdd_child_exp);
883 }
884
885 static int mdd_process_config(const struct lu_env *env,
886                               struct lu_device *d, struct lustre_cfg *cfg)
887 {
888         struct mdd_device *m    = lu2mdd_dev(d);
889         struct dt_device  *dt   = m->mdd_child;
890         struct lu_device  *next = &dt->dd_lu_dev;
891         int rc;
892         ENTRY;
893
894         switch (cfg->lcfg_command) {
895         case LCFG_PARAM: {
896                 struct obd_device *obd = mdd2obd_dev(m);
897
898                 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
899                 if (rc > 0 || rc == -ENOSYS)
900                         /* we don't understand; pass it on */
901                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
902                 break;
903         }
904         case LCFG_SETUP:
905                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
906                 if (rc)
907                         GOTO(out, rc);
908                 dt_conf_get(env, dt, &m->mdd_dt_conf);
909                 break;
910         case LCFG_CLEANUP:
911                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
912                 lu_dev_del_linkage(d->ld_site, d);
913                 mdd_device_shutdown(env, m, cfg);
914                 break;
915         default:
916                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
917                 break;
918         }
919 out:
920         RETURN(rc);
921 }
922
923 static int mdd_recovery_complete(const struct lu_env *env,
924                                  struct lu_device *d)
925 {
926         struct mdd_device *mdd = lu2mdd_dev(d);
927         struct lu_device *next;
928         int rc;
929         ENTRY;
930
931         LASSERT(mdd != NULL);
932         next = &mdd->mdd_child->dd_lu_dev;
933
934         /* XXX: orphans handling. */
935         mdd_orphan_cleanup(env, mdd);
936         rc = next->ld_ops->ldo_recovery_complete(env, next);
937
938         RETURN(rc);
939 }
940
941 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
942                           const struct lu_fid *pfid, const char *name, __u32 mode,
943                           struct lu_fid *fid)
944 {
945         struct dt_object        *parent, *dto;
946         int                      rc;
947
948         ENTRY;
949
950         LASSERT(!fid_is_zero(pfid));
951         parent = dt_locate(env, mdd->mdd_bottom, pfid);
952         if (unlikely(IS_ERR(parent)))
953                 RETURN(PTR_ERR(parent));
954
955         /* create local file/dir, if @fid is passed then try to use it */
956         if (fid_is_zero(fid))
957                 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
958                                                 name, mode);
959         else
960                 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
961                                                          fid, parent, name,
962                                                          mode);
963         if (IS_ERR(dto))
964                 GOTO(out_put, rc = PTR_ERR(dto));
965         *fid = *lu_object_fid(&dto->do_lu);
966         /* since stack is not fully set up the local_storage uses own stack
967          * and we should drop its object from cache */
968         lu_object_put_nocache(env, &dto->do_lu);
969         EXIT;
970 out_put:
971         lu_object_put(env, &parent->do_lu);
972         return 0;
973 }
974
975 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
976                                 enum lfsck_events event)
977 {
978         return 0;
979 }
980
981 static int mdd_prepare(const struct lu_env *env,
982                        struct lu_device *pdev,
983                        struct lu_device *cdev)
984 {
985         struct mdd_device       *mdd = lu2mdd_dev(cdev);
986         struct lu_device        *next = &mdd->mdd_child->dd_lu_dev;
987         struct lu_fid            fid;
988         int                      rc;
989
990         ENTRY;
991
992         rc = next->ld_ops->ldo_prepare(env, cdev, next);
993         if (rc)
994                 RETURN(rc);
995
996         /* Setup local dirs */
997         fid.f_seq = FID_SEQ_LOCAL_NAME;
998         fid.f_oid = 1;
999         fid.f_ver = 0;
1000         rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1001                                     &mdd->mdd_los);
1002         if (rc)
1003                 RETURN(rc);
1004
1005         rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1006         if (rc < 0)
1007                 GOTO(out_los, rc);
1008
1009         lu_root_fid(&fid);
1010         if (mdd_seq_site(mdd)->ss_node_id == 0) {
1011                 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1012                                            mdd_root_dir_name, S_IFDIR |
1013                                            S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1014                 if (rc != 0) {
1015                         CERROR("%s: create root fid failed: rc = %d\n",
1016                                mdd2obd_dev(mdd)->obd_name, rc);
1017                         GOTO(out_los, rc);
1018                 }
1019
1020                 mdd->mdd_root_fid = fid;
1021                 rc = mdd_dot_lustre_setup(env, mdd);
1022                 if (rc != 0) {
1023                         CERROR("%s: initializing .lustre failed: rc = %d\n",
1024                                mdd2obd_dev(mdd)->obd_name, rc);
1025                         GOTO(out_los, rc);
1026                 }
1027
1028                 rc = mdd_compat_fixes(env, mdd);
1029                 if (rc != 0)
1030                         GOTO(out_dot, rc);
1031         } else {
1032                 /* Normal client usually send root access to MDT0 directly,
1033                  * the root FID on non-MDT0 will only be used by echo client. */
1034                 mdd->mdd_root_fid = fid;
1035         }
1036
1037         rc = orph_index_init(env, mdd);
1038         if (rc < 0)
1039                 GOTO(out_dot, rc);
1040
1041         rc = mdd_changelog_init(env, mdd);
1042         if (rc != 0) {
1043                 CERROR("%s: failed to initialize changelog: rc = %d\n",
1044                        mdd2obd_dev(mdd)->obd_name, rc);
1045                 GOTO(out_orph, rc);
1046         }
1047
1048         rc = mdd_hsm_actions_llog_init(env, mdd);
1049         if (rc != 0)
1050                 GOTO(out_changelog, rc);
1051
1052         rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1053                             mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1054                             mdd, true);
1055         if (rc != 0) {
1056                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1057                        mdd2obd_dev(mdd)->obd_name, rc);
1058                 GOTO(out_hsm, rc);
1059         }
1060         RETURN(0);
1061 out_hsm:
1062         mdd_hsm_actions_llog_fini(env, mdd);
1063 out_changelog:
1064         mdd_changelog_fini(env, mdd);
1065 out_orph:
1066         orph_index_fini(env, mdd);
1067 out_dot:
1068         if (mdd_seq_site(mdd)->ss_node_id == 0)
1069                 mdd_dot_lustre_cleanup(env, mdd);
1070 out_los:
1071         local_oid_storage_fini(env, mdd->mdd_los);
1072         mdd->mdd_los = NULL;
1073
1074         return rc;
1075 }
1076
1077 const struct lu_device_operations mdd_lu_ops = {
1078         .ldo_object_alloc      = mdd_object_alloc,
1079         .ldo_process_config    = mdd_process_config,
1080         .ldo_recovery_complete = mdd_recovery_complete,
1081         .ldo_prepare           = mdd_prepare,
1082 };
1083
1084 static int mdd_root_get(const struct lu_env *env,
1085                         struct md_device *m, struct lu_fid *f)
1086 {
1087         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1088
1089         ENTRY;
1090         *f = mdd->mdd_root_fid;
1091         RETURN(0);
1092 }
1093
1094 /*
1095  * No permission check is needed.
1096  */
1097 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1098                       struct obd_statfs *sfs)
1099 {
1100         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1101         int rc;
1102
1103         ENTRY;
1104
1105         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1106
1107         sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1108
1109         RETURN(rc);
1110 }
1111
1112 /*
1113  * No permission check is needed.
1114  */
1115 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
1116                               int mode, unsigned long timeout, __u32 alg,
1117                               struct lustre_capa_key *keys)
1118 {
1119         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1120         int rc;
1121         ENTRY;
1122
1123         /* need barrier for mds_capa_keys access. */
1124
1125         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
1126                                                    timeout, alg, keys);
1127         RETURN(rc);
1128 }
1129
1130 static int mdd_maxeasize_get(const struct lu_env *env, struct md_device *m,
1131                                 int *easize)
1132 {
1133         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1134         ENTRY;
1135
1136         *easize = mdd->mdd_dt_conf.ddp_max_ea_size;
1137
1138         RETURN(0);
1139 }
1140
1141 static int mdd_update_capa_key(const struct lu_env *env,
1142                                struct md_device *m,
1143                                struct lustre_capa_key *key)
1144 {
1145         /* we do not support capabilities ... */
1146         return -EINVAL;
1147 }
1148
1149 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1150                              int idx, void **h)
1151 {
1152         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1153
1154         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1155         return (*h == NULL ? -ENOENT : 0);
1156 }
1157
1158 static struct lu_device *mdd_device_free(const struct lu_env *env,
1159                                          struct lu_device *lu)
1160 {
1161         struct mdd_device *m = lu2mdd_dev(lu);
1162         ENTRY;
1163
1164         LASSERT(atomic_read(&lu->ld_ref) == 0);
1165         md_device_fini(&m->mdd_md_dev);
1166         OBD_FREE_PTR(m);
1167         RETURN(NULL);
1168 }
1169
1170 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1171                                           struct lu_device_type *t,
1172                                           struct lustre_cfg *lcfg)
1173 {
1174         struct lu_device  *l;
1175         struct mdd_device *m;
1176
1177         OBD_ALLOC_PTR(m);
1178         if (m == NULL) {
1179                 l = ERR_PTR(-ENOMEM);
1180         } else {
1181                 int rc;
1182
1183                 l = mdd2lu_dev(m);
1184                 md_device_init(&m->mdd_md_dev, t);
1185                 rc = mdd_init0(env, m, t, lcfg);
1186                 if (rc != 0) {
1187                         mdd_device_free(env, l);
1188                         l = ERR_PTR(rc);
1189                 }
1190         }
1191
1192         return l;
1193 }
1194
1195 /*
1196  * we use exports to track all mdd users
1197  */
1198 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1199                            struct obd_device *obd, struct obd_uuid *cluuid,
1200                            struct obd_connect_data *data, void *localdata)
1201 {
1202         struct mdd_device    *mdd = lu2mdd_dev(obd->obd_lu_dev);
1203         struct lustre_handle  conn;
1204         int                   rc;
1205         ENTRY;
1206
1207         CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1208
1209         rc = class_connect(&conn, obd, cluuid);
1210         if (rc)
1211                 RETURN(rc);
1212
1213         *exp = class_conn2export(&conn);
1214
1215         /* Why should there ever be more than 1 connect? */
1216         LASSERT(mdd->mdd_connects == 0);
1217         mdd->mdd_connects++;
1218
1219         RETURN(0);
1220 }
1221
1222 /*
1223  * once last export (we don't count self-export) disappeared
1224  * mdd can be released
1225  */
1226 static int mdd_obd_disconnect(struct obd_export *exp)
1227 {
1228         struct obd_device *obd = exp->exp_obd;
1229         struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1230         int                rc, release = 0;
1231         ENTRY;
1232
1233         mdd->mdd_connects--;
1234         if (mdd->mdd_connects == 0)
1235                 release = 1;
1236
1237         rc = class_disconnect(exp);
1238
1239         if (rc == 0 && release)
1240                 class_manual_cleanup(obd);
1241         RETURN(rc);
1242 }
1243
1244 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1245                             __u32 keylen, void *key, __u32 *vallen, void *val,
1246                             struct lov_stripe_md *lsm)
1247 {
1248         int rc = -EINVAL;
1249
1250         if (KEY_IS(KEY_OSP_CONNECTED)) {
1251                 struct obd_device       *obd = exp->exp_obd;
1252                 struct mdd_device       *mdd;
1253
1254                 if (!obd->obd_set_up || obd->obd_stopping)
1255                         RETURN(-EAGAIN);
1256
1257                 mdd = lu2mdd_dev(obd->obd_lu_dev);
1258                 LASSERT(mdd);
1259                 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1260                                   val, lsm);
1261                 RETURN(rc);
1262         }
1263
1264         RETURN(rc);
1265 }
1266
1267 static struct obd_ops mdd_obd_device_ops = {
1268         .o_owner        = THIS_MODULE,
1269         .o_connect      = mdd_obd_connect,
1270         .o_disconnect   = mdd_obd_disconnect,
1271         .o_get_info     = mdd_obd_get_info,
1272 };
1273
1274 static int mdd_changelog_user_register(const struct lu_env *env,
1275                                        struct mdd_device *mdd, int *id)
1276 {
1277         struct llog_ctxt *ctxt;
1278         struct llog_changelog_user_rec *rec;
1279         int rc;
1280         ENTRY;
1281
1282         ctxt = llog_get_context(mdd2obd_dev(mdd),
1283                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1284         if (ctxt == NULL)
1285                 RETURN(-ENXIO);
1286
1287         OBD_ALLOC_PTR(rec);
1288         if (rec == NULL) {
1289                 llog_ctxt_put(ctxt);
1290                 RETURN(-ENOMEM);
1291         }
1292
1293         /* Assume we want it on since somebody registered */
1294         rc = mdd_changelog_on(env, mdd, 1);
1295         if (rc)
1296                 GOTO(out, rc);
1297
1298         rec->cur_hdr.lrh_len = sizeof(*rec);
1299         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1300         spin_lock(&mdd->mdd_cl.mc_user_lock);
1301         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1302                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1303                 CERROR("Maximum number of changelog users exceeded!\n");
1304                 GOTO(out, rc = -EOVERFLOW);
1305         }
1306         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1307         rec->cur_endrec = mdd->mdd_cl.mc_index;
1308         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1309
1310         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1311
1312         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1313 out:
1314         OBD_FREE_PTR(rec);
1315         llog_ctxt_put(ctxt);
1316         RETURN(rc);
1317 }
1318
1319 struct mdd_changelog_user_data {
1320         __u64 mcud_endrec; /**< purge record for this user */
1321         __u64 mcud_minrec; /**< lowest changelog recno still referenced */
1322         __u32 mcud_id;
1323         __u32 mcud_minid;  /**< user id with lowest rec reference */
1324         __u32 mcud_usercount;
1325         unsigned int mcud_found:1;
1326 };
1327 #define MCUD_UNREGISTER -1LL
1328
1329 /** Two things:
1330  * 1. Find the smallest record everyone is willing to purge
1331  * 2. Update the last purgeable record for this user
1332  */
1333 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1334                                        struct llog_handle *llh,
1335                                        struct llog_rec_hdr *hdr, void *data)
1336 {
1337         struct llog_changelog_user_rec  *rec;
1338         struct mdd_changelog_user_data  *mcud = data;
1339         int                              rc;
1340
1341         ENTRY;
1342
1343         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
1344
1345         rec = (struct llog_changelog_user_rec *)hdr;
1346
1347         mcud->mcud_usercount++;
1348
1349         /* If we have a new endrec for this id, use it for the following
1350            min check instead of its old value */
1351         if (rec->cur_id == mcud->mcud_id)
1352                 rec->cur_endrec = max(rec->cur_endrec, mcud->mcud_endrec);
1353
1354         /* Track the minimum referenced record */
1355         if (mcud->mcud_minid == 0 || mcud->mcud_minrec > rec->cur_endrec) {
1356                 mcud->mcud_minid = rec->cur_id;
1357                 mcud->mcud_minrec = rec->cur_endrec;
1358         }
1359
1360         if (rec->cur_id != mcud->mcud_id)
1361                 RETURN(0);
1362
1363         /* Update this user's record */
1364         mcud->mcud_found = 1;
1365
1366         /* Special case: unregister this user */
1367         if (mcud->mcud_endrec == MCUD_UNREGISTER) {
1368                 struct llog_cookie cookie;
1369
1370                 cookie.lgc_lgl = llh->lgh_id;
1371                 cookie.lgc_index = hdr->lrh_index;
1372
1373                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1374                                              1, &cookie);
1375                 if (rc == 0)
1376                         mcud->mcud_usercount--;
1377
1378                 RETURN(rc);
1379         }
1380
1381         /* Update the endrec */
1382         CDEBUG(D_IOCTL, "Rewriting changelog user %d endrec to "LPU64"\n",
1383                mcud->mcud_id, rec->cur_endrec);
1384
1385         rc = llog_write(env, llh, hdr, hdr->lrh_index);
1386
1387         RETURN(rc);
1388 }
1389
1390 static int mdd_changelog_user_purge(const struct lu_env *env,
1391                                     struct mdd_device *mdd, int id,
1392                                     long long endrec)
1393 {
1394         struct mdd_changelog_user_data data;
1395         struct llog_ctxt *ctxt;
1396         int rc;
1397         ENTRY;
1398
1399         CDEBUG(D_IOCTL, "Purge request: id=%d, endrec=%lld\n", id, endrec);
1400
1401         data.mcud_id = id;
1402         data.mcud_minid = 0;
1403         data.mcud_minrec = 0;
1404         data.mcud_usercount = 0;
1405         data.mcud_endrec = endrec;
1406         spin_lock(&mdd->mdd_cl.mc_lock);
1407         endrec = mdd->mdd_cl.mc_index;
1408         spin_unlock(&mdd->mdd_cl.mc_lock);
1409         if ((data.mcud_endrec == 0) ||
1410             ((data.mcud_endrec > endrec) &&
1411              (data.mcud_endrec != MCUD_UNREGISTER)))
1412                 data.mcud_endrec = endrec;
1413
1414         ctxt = llog_get_context(mdd2obd_dev(mdd),
1415                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1416         if (ctxt == NULL)
1417                 return -ENXIO;
1418
1419         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
1420
1421         rc = llog_cat_process(env, ctxt->loc_handle,
1422                               mdd_changelog_user_purge_cb, (void *)&data,
1423                               0, 0);
1424         if ((rc >= 0) && (data.mcud_minrec > 0)) {
1425                 CDEBUG(D_IOCTL, "Purging changelog entries up to "LPD64
1426                        ", referenced by "CHANGELOG_USER_PREFIX"%d\n",
1427                        data.mcud_minrec, data.mcud_minid);
1428                 rc = mdd_changelog_llog_cancel(env, mdd, data.mcud_minrec);
1429         } else {
1430                 CWARN("Could not determine changelog records to purge; rc=%d\n",
1431                       rc);
1432         }
1433
1434         llog_ctxt_put(ctxt);
1435
1436         if (!data.mcud_found) {
1437                 CWARN("No entry for user %d.  Last changelog reference is "
1438                       LPD64" by changelog user %d\n", data.mcud_id,
1439                       data.mcud_minrec, data.mcud_minid);
1440                rc = -ENOENT;
1441         }
1442
1443         if (!rc && data.mcud_usercount == 0)
1444                 /* No more users; turn changelogs off */
1445                 rc = mdd_changelog_on(env, mdd, 0);
1446
1447         RETURN (rc);
1448 }
1449
1450 /** mdd_iocontrol
1451  * May be called remotely from mdt_iocontrol_handle or locally from
1452  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1453  * an obd_ioctl_data format (but local ioctl handler does).
1454  * \param cmd - ioc
1455  * \param len - data len
1456  * \param karg - ioctl data, in kernel space
1457  */
1458 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1459                          unsigned int cmd, int len, void *karg)
1460 {
1461         struct mdd_device *mdd;
1462         struct obd_ioctl_data *data = karg;
1463         int rc;
1464         ENTRY;
1465
1466         mdd = lu2mdd_dev(&m->md_lu_dev);
1467
1468         /* Doesn't use obd_ioctl_data */
1469         switch (cmd) {
1470         case OBD_IOC_CHANGELOG_CLEAR: {
1471                 struct changelog_setinfo *cs = karg;
1472                 rc = mdd_changelog_user_purge(env, mdd, cs->cs_id,
1473                                               cs->cs_recno);
1474                 RETURN(rc);
1475         }
1476         case OBD_IOC_GET_MNTOPT: {
1477                 mntopt_t *mntopts = (mntopt_t *)karg;
1478                 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1479                 RETURN(0);
1480         }
1481         case OBD_IOC_START_LFSCK: {
1482                 rc = lfsck_start(env, mdd->mdd_bottom,
1483                                  (struct lfsck_start_param *)karg);
1484                 RETURN(rc);
1485         }
1486         case OBD_IOC_STOP_LFSCK: {
1487                 rc = lfsck_stop(env, mdd->mdd_bottom,
1488                                 (struct lfsck_stop *)karg);
1489                 RETURN(rc);
1490         }
1491         }
1492
1493         /* Below ioctls use obd_ioctl_data */
1494         if (len != sizeof(*data)) {
1495                 CERROR("Bad ioctl size %d\n", len);
1496                 RETURN(-EINVAL);
1497         }
1498         if (data->ioc_version != OBD_IOCTL_VERSION) {
1499                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1500                        OBD_IOCTL_VERSION);
1501                 RETURN(-EINVAL);
1502         }
1503
1504         switch (cmd) {
1505         case OBD_IOC_CHANGELOG_REG:
1506                 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1507                 break;
1508         case OBD_IOC_CHANGELOG_DEREG:
1509                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1,
1510                                               MCUD_UNREGISTER);
1511                 break;
1512         default:
1513                 rc = -ENOTTY;
1514         }
1515
1516         RETURN (rc);
1517 }
1518
1519 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1520 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1521
1522 static const struct md_device_operations mdd_ops = {
1523         .mdo_statfs         = mdd_statfs,
1524         .mdo_root_get       = mdd_root_get,
1525         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
1526         .mdo_update_capa_key= mdd_update_capa_key,
1527         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1528         .mdo_iocontrol      = mdd_iocontrol,
1529         .mdo_maxeasize_get  = mdd_maxeasize_get,
1530 };
1531
1532 static struct lu_device_type_operations mdd_device_type_ops = {
1533         .ldto_init = mdd_type_init,
1534         .ldto_fini = mdd_type_fini,
1535
1536         .ldto_start = mdd_type_start,
1537         .ldto_stop  = mdd_type_stop,
1538
1539         .ldto_device_alloc = mdd_device_alloc,
1540         .ldto_device_free  = mdd_device_free,
1541
1542         .ldto_device_fini    = mdd_device_fini
1543 };
1544
1545 static struct lu_device_type mdd_device_type = {
1546         .ldt_tags     = LU_DEVICE_MD,
1547         .ldt_name     = LUSTRE_MDD_NAME,
1548         .ldt_ops      = &mdd_device_type_ops,
1549         .ldt_ctx_tags = LCT_MD_THREAD
1550 };
1551
1552 /* context key constructor: mdd_key_init */
1553 LU_KEY_INIT(mdd, struct mdd_thread_info);
1554
1555 static void mdd_key_fini(const struct lu_context *ctx,
1556                          struct lu_context_key *key, void *data)
1557 {
1558         struct mdd_thread_info *info = data;
1559
1560         lu_buf_free(&info->mti_big_buf);
1561         lu_buf_free(&info->mti_link_buf);
1562
1563         OBD_FREE_PTR(info);
1564 }
1565
1566 /* context key: mdd_thread_key */
1567 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1568
1569 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1570                              int (*func)(void *), void *data, char *name)
1571 {
1572         struct task_struct      *task;
1573
1574         LASSERT(thread->mgt_init == false);
1575         init_completion(&thread->mgt_started);
1576         init_completion(&thread->mgt_finished);
1577         thread->mgt_data = data;
1578         thread->mgt_abort = false;
1579         thread->mgt_init = true;
1580
1581         task = kthread_run(func, thread, name);
1582         if (IS_ERR(task)) {
1583                 complete(&thread->mgt_finished);
1584                 return PTR_ERR(task);
1585         }
1586         wait_for_completion(&thread->mgt_started);
1587         return 0;
1588 }
1589
1590 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1591 {
1592         if (thread->mgt_init == true) {
1593                 thread->mgt_abort = true;
1594                 wait_for_completion(&thread->mgt_finished);
1595         }
1596 }
1597
1598 static int __init mdd_mod_init(void)
1599 {
1600         int rc;
1601
1602         rc = lu_kmem_init(mdd_caches);
1603         if (rc)
1604                 return rc;
1605
1606         changelog_orig_logops = llog_osd_ops;
1607         changelog_orig_logops.lop_cancel = llog_changelog_cancel;
1608         changelog_orig_logops.lop_add = llog_cat_add_rec;
1609         changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1610
1611         hsm_actions_logops = llog_osd_ops;
1612         hsm_actions_logops.lop_add = llog_cat_add_rec;
1613         hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
1614
1615         rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
1616                                  LUSTRE_MDD_NAME, &mdd_device_type);
1617         if (rc)
1618                 lu_kmem_fini(mdd_caches);
1619         return rc;
1620 }
1621
1622 static void __exit mdd_mod_exit(void)
1623 {
1624         class_unregister_type(LUSTRE_MDD_NAME);
1625         lu_kmem_fini(mdd_caches);
1626 }
1627
1628 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1629 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
1630 MODULE_LICENSE("GPL");
1631
1632 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);