1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mds) handling of striped file data
7 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 # define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_idl.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_lov.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_fsfilt.h>
39 #include "mds_internal.h"
41 void le_lov_desc_to_cpu (struct lov_desc *ld)
43 ld->ld_tgt_count = le32_to_cpu (ld->ld_tgt_count);
44 ld->ld_default_stripe_count = le32_to_cpu (ld->ld_default_stripe_count);
45 ld->ld_default_stripe_size = le32_to_cpu (ld->ld_default_stripe_size);
46 ld->ld_pattern = le32_to_cpu (ld->ld_pattern);
49 void cpu_to_le_lov_desc (struct lov_desc *ld)
51 ld->ld_tgt_count = cpu_to_le32 (ld->ld_tgt_count);
52 ld->ld_default_stripe_count = cpu_to_le32 (ld->ld_default_stripe_count);
53 ld->ld_default_stripe_size = cpu_to_le32 (ld->ld_default_stripe_size);
54 ld->ld_pattern = cpu_to_le32 (ld->ld_pattern);
57 void mds_lov_update_objids(struct obd_device *obd, obd_id *ids)
59 struct mds_obd *mds = &obd->u.mds;
64 for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
65 if (ids[i] > (mds->mds_lov_objids)[i])
66 (mds->mds_lov_objids)[i] = ids[i];
71 static int mds_lov_read_objids(struct obd_device *obd)
73 struct mds_obd *mds = &obd->u.mds;
76 int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(*ids);
79 if (mds->mds_lov_objids != NULL)
85 mds->mds_lov_objids = ids;
87 if (mds->mds_lov_objid_filp->f_dentry->d_inode->i_size == 0)
89 rc = fsfilt_read_record(obd, mds->mds_lov_objid_filp, ids, size, &off);
91 CERROR("Error reading objids %d\n", rc);
93 mds->mds_lov_objids_valid = 1;
97 for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
98 CDEBUG(D_INFO, "read last object "LPU64" for idx %d\n",
99 mds->mds_lov_objids[i], i);
104 int mds_lov_write_objids(struct obd_device *obd)
106 struct mds_obd *mds = &obd->u.mds;
108 int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id);
111 for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
112 CDEBUG(D_INFO, "writing last object "LPU64" for idx %d\n",
113 mds->mds_lov_objids[i], i);
115 rc = fsfilt_write_record(obd, mds->mds_lov_objid_filp,
116 mds->mds_lov_objids, size, &off, 0);
120 static int mds_lov_clearorphans(struct mds_obd *mds, struct obd_uuid *ost_uuid)
124 struct obd_trans_info oti = {0};
125 struct lov_stripe_md *empty_ea = NULL;
128 LASSERT(mds->mds_lov_objids != NULL);
130 /* This create will in fact either create or destroy: If the OST is
131 * missing objects below this ID, they will be created. If it finds
132 * objects above this ID, they will be removed. */
133 memset(&oa, 0, sizeof(oa));
134 oa.o_valid = OBD_MD_FLFLAGS;
135 oa.o_flags = OBD_FL_DELORPHAN;
136 if (ost_uuid != NULL) {
137 memcpy(&oa.o_inline, ost_uuid, sizeof(*ost_uuid));
138 oa.o_valid |= OBD_MD_FLINLINE;
140 rc = obd_create(mds->mds_osc_exp, &oa, &empty_ea, &oti);
145 /* update the LOV-OSC knowledge of the last used object id's */
146 int mds_lov_set_nextid(struct obd_device *obd)
148 struct mds_obd *mds = &obd->u.mds;
152 LASSERT(!obd->obd_recovering);
154 LASSERT(mds->mds_lov_objids != NULL);
156 rc = obd_set_info(mds->mds_osc_exp, strlen("next_id"), "next_id",
157 mds->mds_lov_desc.ld_tgt_count, mds->mds_lov_objids);
161 rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
166 if (rc && mds->mds_lov_objids) {
167 /* Might as well crash here, until we figure out what to do.
168 * If we OBD_FREE, we'll just LASSERT the next time through this
171 OBD_FREE(mds->mds_lov_objids,
172 mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
173 mds->mds_lov_objids = NULL;
179 /* tell the LOV-OSC by how much to pre-create */
180 int mds_lov_set_growth(struct mds_obd *mds, int count)
185 rc = obd_set_info(mds->mds_osc_exp, strlen("growth_count"),
186 "growth_count", sizeof(count), &count);
191 int mds_lov_connect(struct obd_device *obd, char * lov_name)
193 struct mds_obd *mds = &obd->u.mds;
194 struct lustre_handle conn = {0,};
199 if (IS_ERR(mds->mds_osc_obd))
200 RETURN(PTR_ERR(mds->mds_osc_obd));
202 if (mds->mds_osc_obd)
205 mds->mds_osc_obd = class_name2obd(lov_name);
206 if (!mds->mds_osc_obd) {
207 CERROR("MDS cannot locate LOV %s\n",
209 mds->mds_osc_obd = ERR_PTR(-ENOTCONN);
213 rc = obd_connect(&conn, mds->mds_osc_obd, &obd->obd_uuid);
215 CERROR("MDS cannot connect to LOV %s (%d)\n",
217 mds->mds_osc_obd = ERR_PTR(rc);
220 mds->mds_osc_exp = class_conn2export(&conn);
222 rc = obd_register_observer(mds->mds_osc_obd, obd);
224 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
226 GOTO(err_discon, rc);
229 valsize = sizeof(mds->mds_lov_desc);
230 rc = obd_get_info(mds->mds_osc_exp, strlen("lovdesc") + 1, "lovdesc",
231 &valsize, &mds->mds_lov_desc);
235 mds->mds_max_mdsize = lov_mds_md_size(mds->mds_lov_desc.ld_tgt_count);
236 mds->mds_max_cookiesize = mds->mds_lov_desc.ld_tgt_count*
237 sizeof(struct llog_cookie);
238 mds->mds_has_lov_desc = 1;
239 rc = mds_lov_read_objids(obd);
241 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
245 rc = llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
247 CERROR("failed to initialize catalog %d\n", rc);
251 /* FIXME before set info call is made, we must initialize logging */
252 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
257 /* If we're mounting this code for the first time on an existing FS,
258 * we need to populate the objids array from the real OST values */
259 if (!mds->mds_lov_objids_valid) {
260 int size = sizeof(obd_id) * mds->mds_lov_desc.ld_tgt_count;
261 rc = obd_get_info(mds->mds_osc_exp, strlen("last_id"),
262 "last_id", &size, mds->mds_lov_objids);
264 for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
265 CWARN("got last object "LPU64" from OST %d\n",
266 mds->mds_lov_objids[i], i);
267 mds->mds_lov_objids_valid = 1;
268 rc = mds_lov_write_objids(obd);
270 CERROR("got last objids from OSTs, but error "
271 "writing objids file: %d\n", rc);
275 /* I want to see a callback happen when the OBD moves to a
276 * "For General Use" state, and that's when we'll call
277 * set_nextid(). The class driver can help us here, because
278 * it can use the obd_recovering flag to determine when the
279 * the OBD is full available. */
280 if (!obd->obd_recovering) {
281 rc = llog_connect(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT),
282 obd->u.mds.mds_lov_desc.ld_tgt_count, NULL,
285 CERROR("faild at llog_origin_connect: %d\n", rc);
287 rc = mds_cleanup_orphans(obd);
289 CERROR("Cleanup %d orphans while MDS isn't recovering\n", rc);
291 rc = mds_lov_set_nextid(obd);
298 /* cleanup all llogging subsystems */
299 rc = obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
301 CERROR("failed to cleanup llogging subsystems\n");
303 obd_register_observer(mds->mds_osc_obd, NULL);
305 obd_disconnect(mds->mds_osc_exp, 0);
306 mds->mds_osc_exp = NULL;
307 mds->mds_osc_obd = ERR_PTR(rc);
311 int mds_lov_disconnect(struct obd_device *obd, int flags)
313 struct mds_obd *mds = &obd->u.mds;
317 if (!IS_ERR(mds->mds_osc_obd) && mds->mds_osc_exp != NULL) {
318 /* cleanup all llogging subsystems */
319 rc = obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
321 CERROR("failed to cleanup llogging subsystems\n");
323 obd_register_observer(mds->mds_osc_obd, NULL);
325 rc = obd_disconnect(mds->mds_osc_exp, flags);
326 /* if obd_disconnect fails (probably because the
327 * export was disconnected by class_disconnect_exports)
328 * then we just need to drop our ref. */
330 class_export_put(mds->mds_osc_exp);
331 mds->mds_osc_exp = NULL;
332 mds->mds_osc_obd = NULL;
338 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
339 void *karg, void *uarg)
341 static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
342 struct obd_device *obd = exp->exp_obd;
343 struct mds_obd *mds = &obd->u.mds;
344 struct obd_ioctl_data *data = karg;
345 struct obd_run_ctxt saved;
349 case OBD_IOC_RECORD: {
350 char *name = data->ioc_inlbuf1;
351 if (mds->mds_cfg_llh)
354 push_ctxt(&saved, &obd->obd_ctxt, NULL);
355 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
356 &mds->mds_cfg_llh, NULL, name);
358 llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
361 mds->mds_cfg_llh = NULL;
362 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
367 case OBD_IOC_ENDRECORD: {
368 if (!mds->mds_cfg_llh)
371 push_ctxt(&saved, &obd->obd_ctxt, NULL);
372 rc = llog_close(mds->mds_cfg_llh);
373 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
375 mds->mds_cfg_llh = NULL;
379 case OBD_IOC_DORECORD: {
381 struct llog_rec_hdr rec;
382 if (!mds->mds_cfg_llh)
385 rec.lrh_len = llog_data_len(data->ioc_plen1);
387 if (data->ioc_type == LUSTRE_CFG_TYPE) {
388 rec.lrh_type = OBD_CFG_REC;
389 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
390 rec.lrh_type = PTL_CFG_REC;
392 CERROR("unknown cfg record type:%d \n", data->ioc_type);
396 OBD_ALLOC(cfg_buf, data->ioc_plen1);
399 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
401 OBD_FREE(cfg_buf, data->ioc_plen1);
405 push_ctxt(&saved, &obd->obd_ctxt, NULL);
406 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
408 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
410 OBD_FREE(cfg_buf, data->ioc_plen1);
414 case OBD_IOC_PARSE: {
415 struct llog_ctxt *ctxt =
416 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
417 push_ctxt(&saved, &obd->obd_ctxt, NULL);
418 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
419 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
426 case OBD_IOC_DUMP_LOG: {
427 struct llog_ctxt *ctxt =
428 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
429 push_ctxt(&saved, &obd->obd_ctxt, NULL);
430 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
431 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
438 case OBD_IOC_SET_READONLY: {
440 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
441 BDEVNAME_DECLARE_STORAGE(tmp);
442 CERROR("setting device %s read-only\n",
443 ll_bdevname(obd->u.mds.mds_sb, tmp));
445 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
447 rc = fsfilt_commit(obd, inode, handle, 1);
449 dev_set_rdonly(ll_sbdev(obd->u.mds.mds_sb), 2);
453 case OBD_IOC_CATLOGLIST: {
454 int count = mds->mds_lov_desc.ld_tgt_count;
455 rc = llog_catlog_list(obd, count, data);
459 case OBD_IOC_LLOG_CHECK:
460 case OBD_IOC_LLOG_CANCEL:
461 case OBD_IOC_LLOG_REMOVE: {
462 struct llog_ctxt *ctxt =
463 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
465 obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
466 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
467 rc = llog_ioctl(ctxt, cmd, data);
468 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
469 llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
473 case OBD_IOC_LLOG_INFO:
474 case OBD_IOC_LLOG_PRINT: {
475 struct llog_ctxt *ctxt =
476 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
478 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
479 rc = llog_ioctl(ctxt, cmd, data);
480 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
485 case OBD_IOC_ABORT_RECOVERY:
486 CERROR("aborting recovery for device %s\n", obd->obd_name);
487 target_abort_recovery(obd);
496 int mds_notify(struct obd_device *obd, struct obd_device *watched, int active)
498 struct obd_uuid *uuid;
505 if (strcmp(watched->obd_type->typ_name, "osc")) {
506 CERROR("unexpected notification of %s %s!\n",
507 watched->obd_type->typ_name, watched->obd_name);
511 uuid = &watched->u.cli.cl_import->imp_target_uuid;
512 if (obd->obd_recovering) {
513 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
514 obd->obd_name, uuid->uuid);
516 LASSERT(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT) != NULL);
518 rc = obd_set_info(obd->u.mds.mds_osc_exp, strlen("mds_conn"), "mds_conn",
523 rc = llog_connect(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT),
524 obd->u.mds.mds_lov_desc.ld_tgt_count,
527 CERROR("faild at llog_origin_connect: %d\n", rc);
531 CWARN("MDS %s: %s now active, resetting orphans\n",
532 obd->obd_name, uuid->uuid);
533 rc = mds_lov_clearorphans(&obd->u.mds, uuid);
538 /* Convert the on-disk LOV EA structre.
539 * We always try to convert from an old LOV EA format to the common in-memory
540 * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
541 * then convert back to the new on-disk format and save it back to disk
542 * (obd_packmd() only ever saves to the new on-disk format) so we don't have
543 * to convert it each time this inode is accessed.
545 * This function is a bit interesting in the error handling. We can safely
546 * ship the old lmm to the client in case of failure, since it uses the same
547 * obd_unpackmd() code and can do the conversion if the MDS fails for some
548 * reason. We will not delete the old lmm data until we have written the
549 * new format lmm data in fsfilt_set_md(). */
550 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
551 struct lov_mds_md *lmm, int lmm_size)
553 struct lov_stripe_md *lsm = NULL;
558 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
561 CWARN("converting LOV EA on %lu/%u from V0 to V1\n",
562 inode->i_ino, inode->i_generation);
563 rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, lmm, lmm_size);
567 rc = obd_packmd(obd->u.mds.mds_osc_exp, &lmm, lsm);
572 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
573 if (IS_ERR(handle)) {
574 rc = PTR_ERR(handle);
578 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size);
580 err = fsfilt_commit(obd, inode, handle, 0);
582 rc = err ? err : lmm_size;
585 obd_free_memmd(obd->u.mds.mds_osc_exp, &lsm);