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_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
135 oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
136 oa.o_flags = OBD_FL_DELORPHAN;
137 if (ost_uuid != NULL) {
138 memcpy(&oa.o_inline, ost_uuid, sizeof(*ost_uuid));
139 oa.o_valid |= OBD_MD_FLINLINE;
141 rc = obd_create(mds->mds_osc_exp, &oa, &empty_ea, &oti);
146 /* update the LOV-OSC knowledge of the last used object id's */
147 int mds_lov_set_nextid(struct obd_device *obd)
149 struct mds_obd *mds = &obd->u.mds;
153 LASSERT(!obd->obd_recovering);
155 LASSERT(mds->mds_lov_objids != NULL);
157 rc = obd_set_info(mds->mds_osc_exp, strlen("next_id"), "next_id",
158 mds->mds_lov_desc.ld_tgt_count, mds->mds_lov_objids);
162 rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
168 /* tell the LOV-OSC by how much to pre-create */
169 int mds_lov_set_growth(struct mds_obd *mds, int count)
174 rc = obd_set_info(mds->mds_osc_exp, strlen("growth_count"),
175 "growth_count", sizeof(count), &count);
180 int mds_lov_connect(struct obd_device *obd, char * lov_name)
182 struct mds_obd *mds = &obd->u.mds;
183 struct lustre_handle conn = {0,};
188 if (IS_ERR(mds->mds_osc_obd))
189 RETURN(PTR_ERR(mds->mds_osc_obd));
191 if (mds->mds_osc_obd)
194 mds->mds_osc_obd = class_name2obd(lov_name);
195 if (!mds->mds_osc_obd) {
196 CERROR("MDS cannot locate LOV %s\n",
198 mds->mds_osc_obd = ERR_PTR(-ENOTCONN);
202 rc = obd_connect(&conn, mds->mds_osc_obd, &obd->obd_uuid);
204 CERROR("MDS cannot connect to LOV %s (%d)\n",
206 mds->mds_osc_obd = ERR_PTR(rc);
209 mds->mds_osc_exp = class_conn2export(&conn);
211 rc = obd_register_observer(mds->mds_osc_obd, obd);
213 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
215 GOTO(err_discon, rc);
218 valsize = sizeof(mds->mds_lov_desc);
219 rc = obd_get_info(mds->mds_osc_exp, strlen("lovdesc") + 1, "lovdesc",
220 &valsize, &mds->mds_lov_desc);
224 i = lov_mds_md_size(mds->mds_lov_desc.ld_tgt_count);
225 if (i > mds->mds_max_mdsize)
226 mds->mds_max_mdsize = i;
227 mds->mds_max_cookiesize = mds->mds_lov_desc.ld_tgt_count*
228 sizeof(struct llog_cookie);
229 mds->mds_has_lov_desc = 1;
230 rc = mds_lov_read_objids(obd);
232 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
236 rc = llog_cat_initialize(obd, &obd->obd_llogs,
237 mds->mds_lov_desc.ld_tgt_count);
239 CERROR("failed to initialize catalog %d\n", rc);
243 /* FIXME before set info call is made, we must initialize logging */
244 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
245 valsize = sizeof(group);
246 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
251 /* If we're mounting this code for the first time on an existing FS,
252 * we need to populate the objids array from the real OST values */
253 if (!mds->mds_lov_objids_valid) {
254 int size = sizeof(obd_id) * mds->mds_lov_desc.ld_tgt_count;
255 rc = obd_get_info(mds->mds_osc_exp, strlen("last_id"),
256 "last_id", &size, mds->mds_lov_objids);
258 for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
259 CWARN("got last object "LPU64" from OST %d\n",
260 mds->mds_lov_objids[i], i);
261 mds->mds_lov_objids_valid = 1;
262 rc = mds_lov_write_objids(obd);
264 CERROR("got last objids from OSTs, but error "
265 "writing objids file: %d\n", rc);
269 /* I want to see a callback happen when the OBD moves to a
270 * "For General Use" state, and that's when we'll call
271 * set_nextid(). The class driver can help us here, because
272 * it can use the obd_recovering flag to determine when the
273 * the OBD is full available. */
274 if (!obd->obd_recovering) {
275 struct llog_ctxt *ctxt;
276 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
277 rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
280 CERROR("faild at llog_origin_connect: %d\n", rc);
282 rc = mds_cleanup_orphans(obd);
284 CERROR("Cleanup %d orphans while MDS isn't recovering\n", rc);
286 rc = mds_lov_set_nextid(obd);
293 /* cleanup all llogging subsystems */
294 rc = obd_llog_finish(obd, &obd->obd_llogs,
295 mds->mds_lov_desc.ld_tgt_count);
297 CERROR("failed to cleanup llogging subsystems\n");
299 obd_register_observer(mds->mds_osc_obd, NULL);
301 obd_disconnect(mds->mds_osc_exp, 0);
302 mds->mds_osc_exp = NULL;
303 mds->mds_osc_obd = ERR_PTR(rc);
307 int mds_lov_disconnect(struct obd_device *obd, int flags)
309 struct mds_obd *mds = &obd->u.mds;
313 if (!IS_ERR(mds->mds_osc_obd) && mds->mds_osc_exp != NULL) {
314 /* cleanup all llogging subsystems */
315 rc = obd_llog_finish(obd, &obd->obd_llogs,
316 mds->mds_lov_desc.ld_tgt_count);
318 CERROR("failed to cleanup llogging subsystems\n");
320 obd_register_observer(mds->mds_osc_obd, NULL);
322 rc = obd_disconnect(mds->mds_osc_exp, flags);
323 /* if obd_disconnect fails (probably because the
324 * export was disconnected by class_disconnect_exports)
325 * then we just need to drop our ref. */
327 class_export_put(mds->mds_osc_exp);
328 mds->mds_osc_exp = NULL;
329 mds->mds_osc_obd = NULL;
335 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
336 void *karg, void *uarg)
338 static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
339 struct obd_device *obd = exp->exp_obd;
340 struct mds_obd *mds = &obd->u.mds;
341 struct obd_ioctl_data *data = karg;
342 struct obd_run_ctxt saved;
343 struct llog_ctxt *ctxt;
347 case OBD_IOC_RECORD: {
348 char *name = data->ioc_inlbuf1;
349 if (mds->mds_cfg_llh)
352 push_ctxt(&saved, &obd->obd_ctxt, NULL);
353 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
354 rc = llog_create(ctxt, &mds->mds_cfg_llh, NULL, name);
356 llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
359 mds->mds_cfg_llh = NULL;
360 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
365 case OBD_IOC_ENDRECORD: {
366 if (!mds->mds_cfg_llh)
369 push_ctxt(&saved, &obd->obd_ctxt, NULL);
370 rc = llog_close(mds->mds_cfg_llh);
371 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
373 mds->mds_cfg_llh = NULL;
377 case OBD_IOC_CLEAR_LOG: {
378 char *name = data->ioc_inlbuf1;
379 if (mds->mds_cfg_llh)
382 push_ctxt(&saved, &obd->obd_ctxt, NULL);
383 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
384 rc = llog_create(ctxt, &mds->mds_cfg_llh, NULL, name);
386 llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
389 rc = llog_destroy(mds->mds_cfg_llh);
390 llog_free_handle(mds->mds_cfg_llh);
392 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
394 mds->mds_cfg_llh = NULL;
398 case OBD_IOC_DORECORD: {
400 struct llog_rec_hdr rec;
401 if (!mds->mds_cfg_llh)
404 rec.lrh_len = llog_data_len(data->ioc_plen1);
406 if (data->ioc_type == LUSTRE_CFG_TYPE) {
407 rec.lrh_type = OBD_CFG_REC;
408 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
409 rec.lrh_type = PTL_CFG_REC;
411 CERROR("unknown cfg record type:%d \n", data->ioc_type);
415 OBD_ALLOC(cfg_buf, data->ioc_plen1);
418 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
420 OBD_FREE(cfg_buf, data->ioc_plen1);
424 push_ctxt(&saved, &obd->obd_ctxt, NULL);
425 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
427 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
429 OBD_FREE(cfg_buf, data->ioc_plen1);
433 case OBD_IOC_PARSE: {
434 struct llog_ctxt *ctxt =
435 llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
436 push_ctxt(&saved, &obd->obd_ctxt, NULL);
437 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
438 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
445 case OBD_IOC_DUMP_LOG: {
446 struct llog_ctxt *ctxt =
447 llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
448 push_ctxt(&saved, &obd->obd_ctxt, NULL);
449 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
450 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
457 case OBD_IOC_SET_READONLY: {
459 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
460 BDEVNAME_DECLARE_STORAGE(tmp);
461 CERROR("setting device %s read-only\n",
462 ll_bdevname(obd->u.mds.mds_sb, tmp));
464 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
466 rc = fsfilt_commit(obd, inode, handle, 1);
468 dev_set_rdonly(ll_sbdev(obd->u.mds.mds_sb), 2);
472 case OBD_IOC_CATLOGLIST: {
473 int count = mds->mds_lov_desc.ld_tgt_count;
474 rc = llog_catlog_list(obd, count, data);
478 case OBD_IOC_LLOG_CHECK:
479 case OBD_IOC_LLOG_CANCEL:
480 case OBD_IOC_LLOG_REMOVE: {
481 struct llog_ctxt *ctxt =
482 llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
485 obd_llog_finish(obd, &obd->obd_llogs,
486 mds->mds_lov_desc.ld_tgt_count);
487 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
488 rc = llog_ioctl(ctxt, cmd, data);
489 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
490 llog_cat_initialize(obd, &obd->obd_llogs,
491 mds->mds_lov_desc.ld_tgt_count);
492 rc2 = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
498 case OBD_IOC_LLOG_INFO:
499 case OBD_IOC_LLOG_PRINT: {
500 struct llog_ctxt *ctxt =
501 llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
503 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
504 rc = llog_ioctl(ctxt, cmd, data);
505 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
510 case OBD_IOC_ABORT_RECOVERY:
511 CERROR("aborting recovery for device %s\n", obd->obd_name);
512 target_abort_recovery(obd);
521 int mds_notify(struct obd_device *obd, struct obd_device *watched, int active)
523 struct obd_uuid *uuid;
530 if (strcmp(watched->obd_type->typ_name, "osc")) {
531 CERROR("unexpected notification of %s %s!\n",
532 watched->obd_type->typ_name, watched->obd_name);
536 uuid = &watched->u.cli.cl_import->imp_target_uuid;
537 if (obd->obd_recovering) {
538 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
539 obd->obd_name, uuid->uuid);
541 struct llog_ctxt *ctxt;
543 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
544 LASSERT(ctxt != NULL);
546 rc = obd_set_info(obd->u.mds.mds_osc_exp, strlen("mds_conn"), "mds_conn",
551 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
552 rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
555 CERROR("faild at llog_origin_connect: %d\n", rc);
559 CWARN("MDS %s: %s now active, resetting orphans\n",
560 obd->obd_name, uuid->uuid);
561 rc = mds_lov_clearorphans(&obd->u.mds, uuid);
566 /* Convert the on-disk LOV EA structre.
567 * We always try to convert from an old LOV EA format to the common in-memory
568 * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
569 * then convert back to the new on-disk format and save it back to disk
570 * (obd_packmd() only ever saves to the new on-disk format) so we don't have
571 * to convert it each time this inode is accessed.
573 * This function is a bit interesting in the error handling. We can safely
574 * ship the old lmm to the client in case of failure, since it uses the same
575 * obd_unpackmd() code and can do the conversion if the MDS fails for some
576 * reason. We will not delete the old lmm data until we have written the
577 * new format lmm data in fsfilt_set_md(). */
578 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
579 struct lov_mds_md *lmm, int lmm_size)
581 struct lov_stripe_md *lsm = NULL;
586 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
589 CWARN("converting LOV EA on %lu/%u from V0 to V1\n",
590 inode->i_ino, inode->i_generation);
591 rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, lmm, lmm_size);
595 rc = obd_packmd(obd->u.mds.mds_osc_exp, &lmm, lsm);
600 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
601 if (IS_ERR(handle)) {
602 rc = PTR_ERR(handle);
606 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size);
608 err = fsfilt_commit(obd, inode, handle, 0);
610 rc = err ? err : lmm_size;
613 obd_free_memmd(obd->u.mds.mds_osc_exp, &lsm);