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