1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/mgs/mgs_handler.c
5 * Lustre Management Server (mgs) request handler
7 * Copyright (C) 2001-2005 Cluster File Systems, Inc.
8 * Author Nathan <nathan@clusterfs.com>
9 * Author LinSongTao <lincent@clusterfs.com>
11 * This file is part of Lustre, http://www.lustre.org.
13 * Lustre is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Lustre is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Lustre; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_MGS
33 # include <linux/module.h>
34 # include <linux/pagemap.h>
35 # include <linux/miscdevice.h>
36 # include <linux/init.h>
38 # include <liblustre.h>
41 #include <linux/obd_class.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lprocfs_status.h>
44 #include "mgs_internal.h"
46 /* Establish a connection to the MGS.*/
47 static int mgs_connect(struct lustre_handle *conn, struct obd_device *obd,
48 struct obd_uuid *cluuid, struct obd_connect_data *data)
50 struct obd_export *exp;
51 struct mgs_export_data *med;
52 struct mgs_client_data *mcd;
53 int rc, abort_recovery;
56 if (!conn || !obd || !cluuid)
59 /* Check for aborted recovery. */
60 spin_lock_bh(&obd->obd_processing_task_lock);
61 abort_recovery = obd->obd_abort_recovery;
62 spin_unlock_bh(&obd->obd_processing_task_lock);
64 target_abort_recovery(obd);
66 rc = class_connect(conn, obd, cluuid);
69 exp = class_conn2export(conn);
71 med = &exp->exp_mgs_data;
74 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
75 exp->exp_connect_flags = data->ocd_connect_flags;
78 OBD_ALLOC(mcd, sizeof(*mcd));
80 CERROR("mgs: out of memory for client data\n");
81 GOTO(out, rc = -ENOMEM);
84 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
90 OBD_FREE(mcd, sizeof(*mcd));
93 class_disconnect(exp);
95 class_export_put(exp);
101 static int mgs_init_export(struct obd_export *exp)
103 struct mgs_export_data *med = &exp->exp_mgs_data;
105 INIT_LIST_HEAD(&med->med_open_head);
106 spin_lock_init(&med->med_open_lock);
110 static int mgs_disconnect(struct obd_export *exp)
112 unsigned long irqflags;
117 class_export_get(exp);
119 /* Disconnect early so that clients can't keep using export */
120 rc = class_disconnect(exp);
122 /* complete all outstanding replies */
123 spin_lock_irqsave(&exp->exp_lock, irqflags);
124 while (!list_empty(&exp->exp_outstanding_replies)) {
125 struct ptlrpc_reply_state *rs =
126 list_entry(exp->exp_outstanding_replies.next,
127 struct ptlrpc_reply_state, rs_exp_list);
128 struct ptlrpc_service *svc = rs->rs_service;
130 spin_lock(&svc->srv_lock);
131 list_del_init(&rs->rs_exp_list);
132 ptlrpc_schedule_difficult_reply(rs);
133 spin_unlock(&svc->srv_lock);
135 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
137 class_export_put(exp);
141 /* mount the file system (secretly) */
142 static int mgs_setup(struct obd_device *obd, obd_count len, void *buf)
144 struct lprocfs_static_vars lvars;
145 struct lustre_cfg* lcfg = buf;
146 char *options = NULL;
147 struct mgs_obd *mgs = &obd->u.mgs;
148 struct vfsmount *mnt;
153 /* setup 1:/dev/loop/0 2:ext3 3:mgs 4:errors=remount-ro,iopen_nopriv*/
155 if (lcfg->lcfg_bufcount < 3)
158 if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
159 RETURN(rc = -EINVAL);
161 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
162 if (IS_ERR(obd->obd_fsops))
163 RETURN(rc = PTR_ERR(obd->obd_fsops));
165 page = __get_free_page(GFP_KERNEL);
169 options = (char *)page;
170 memset(options, 0, PAGE_SIZE);
172 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
173 sprintf(options , ",%s", lustre_cfg_string(lcfg, 4));
175 //FIXME mount was already done in lustre_fill_super,
176 //we just need to access it
177 mnt = do_kern_mount(lustre_cfg_string(lcfg, 2), 0,
178 lustre_cfg_string(lcfg, 1), (void *)options);
182 CERROR("do_kern_mount failed: rc = %d\n", rc);
186 CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
188 LASSERT(!lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb)));
190 rc = mgs_fs_setup(obd, mnt);
192 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
197 rc = llog_start_commit_thread();
201 //FIXME: no LDLM support for llog now
202 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
203 "mgs_ldlm_client", &obd->obd_ldlm_client);
205 obd->obd_replayable = 1;
207 rc = mgs_postsetup(obd);
211 lprocfs_init_vars(mgs, &lvars);
212 lprocfs_obd_setup(obd, lvars.obd_vars);
214 if (obd->obd_recovering) {
215 LCONSOLE_WARN("MGT %s now serving %s, but will be in recovery "
216 "until %d %s reconnect, or if no clients "
217 "reconnect for %d:%.02d; during that time new "
218 "clients will not be allowed to connect. "
219 "Recovery progress can be monitored by watching "
220 "/proc/fs/lustre/mgs/%s/recovery_status.\n",
222 lustre_cfg_string(lcfg, 1),
223 obd->obd_recoverable_clients,
224 (obd->obd_recoverable_clients == 1)
225 ? "client" : "clients",
226 (int)(OBD_RECOVERY_TIMEOUT / HZ) / 60,
227 (int)(OBD_RECOVERY_TIMEOUT / HZ) % 60,
230 LCONSOLE_INFO("MGT %s now serving %s with recovery %s.\n",
232 lustre_cfg_string(lcfg, 1),
233 obd->obd_replayable ? "enabled" : "disabled");
235 //FIXME: no ldlm support now
237 ping_evictor_start();
242 /* No extra cleanup needed for llog_init_commit_thread() */
246 mntput(mgs->mgs_vfsmnt);
250 fsfilt_put_ops(obd->obd_fsops);
255 static int mgs_postsetup(struct obd_device *obd)
257 struct mgs_obd *mgs = &obd->u.mgs;
261 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
266 static int mgs_precleanup(struct obd_device *obd, int stage)
271 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
272 rc = obd_llog_finish(obd, 0);
276 static int mgs_cleanup(struct obd_device *obd)
278 struct mgs_obd *mgs = &obd->u.mgs;
279 lvfs_sbdev_type save_dev;
285 if (mgs->mgs_sb == NULL)
287 save_dev = lvfs_sbdev(mgs->mgs_sb);
289 lprocfs_obd_cleanup(obd);
291 mgs_update_server_data(obd, 1);
295 if (atomic_read(&obd->u.mgs.mgs_vfsmnt->mnt_count) > 2)
296 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
297 atomic_read(&obd->u.mgs.mgs_vfsmnt->mnt_count));
299 /* We can only unlock kernel if we are in the context of sys_ioctl,
300 otherwise we never called lock_kernel */
301 if (kernel_locked()) {
306 mntput(mgs->mgs_vfsmnt);
309 spin_lock_bh(&obd->obd_processing_task_lock);
310 if (obd->obd_recovering) {
311 target_cancel_recovery_timer(obd);
312 obd->obd_recovering = 0;
314 spin_unlock_bh(&obd->obd_processing_task_lock);
316 lvfs_clear_rdonly(save_dev);
321 fsfilt_put_ops(obd->obd_fsops);
323 LCONSOLE_INFO("MDT %s has stopped.\n", obd->obd_name);
328 /* Look up an entry by inode number. */
329 /* this function ONLY returns valid dget'd dentries with an initialized inode
331 struct dentry *mgs_fid2dentry(struct mgs_obd *mgs, struct ll_fid *fid,
332 struct vfsmount **mnt)
335 unsigned long ino = fid->id;
336 __u32 generation = fid->generation;
338 struct dentry *result;
341 RETURN(ERR_PTR(-ESTALE));
343 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
345 CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
346 ino, generation, mgs->mgs_sb);
348 /* under ext3 this is neither supposed to return bad inodes
350 result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
354 inode = result->d_inode;
356 RETURN(ERR_PTR(-ENOENT));
358 if (generation && inode->i_generation != generation) {
359 /* we didn't find the right inode.. */
360 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
361 inode->i_ino, (unsigned long)inode->i_nlink,
362 atomic_read(&inode->i_count), inode->i_generation,
365 RETURN(ERR_PTR(-ENOENT));
369 *mnt = mgs->mgs_vfsmnt;
376 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
379 struct obd_device *obd = data;
382 fid.generation = gen;
383 return mgs_fid2dentry(&obd->u.mgs, &fid, NULL);
386 struct lvfs_callback_ops mgs_lvfs_ops = {
387 l_fid2dentry: mgs_lvfs_fid2dentry,
391 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
392 void *karg, void *uarg)
394 static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
395 struct obd_device *obd = exp->exp_obd;
396 struct mgs_obd *mgs = &obd->u.mgs;
397 struct obd_ioctl_data *data = karg;
398 struct lvfs_run_ctxt saved;
402 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
405 case OBD_IOC_RECORD: {
406 char *name = data->ioc_inlbuf1;
407 if (mgs->mgs_cfg_llh)
410 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
411 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
412 &mds->mds_cfg_llh, NULL, name);
414 llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
417 mds->mds_cfg_llh = NULL;
418 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
423 case OBD_IOC_ENDRECORD: {
424 if (!mds->mds_cfg_llh)
427 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
428 rc = llog_close(mds->mds_cfg_llh);
429 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
431 mds->mds_cfg_llh = NULL;
435 case OBD_IOC_CLEAR_LOG: {
436 char *name = data->ioc_inlbuf1;
437 if (mds->mds_cfg_llh)
440 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
441 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
442 &mds->mds_cfg_llh, NULL, name);
444 llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
447 rc = llog_destroy(mds->mds_cfg_llh);
448 llog_free_handle(mds->mds_cfg_llh);
450 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
452 mds->mds_cfg_llh = NULL;
456 case OBD_IOC_DORECORD: {
458 struct llog_rec_hdr rec;
459 if (!mds->mds_cfg_llh)
462 rec.lrh_len = llog_data_len(data->ioc_plen1);
464 if (data->ioc_type == LUSTRE_CFG_TYPE) {
465 rec.lrh_type = OBD_CFG_REC;
467 CERROR("unknown cfg record type:%d \n", data->ioc_type);
471 OBD_ALLOC(cfg_buf, data->ioc_plen1);
474 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
476 OBD_FREE(cfg_buf, data->ioc_plen1);
480 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
481 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
483 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
485 OBD_FREE(cfg_buf, data->ioc_plen1);
489 case OBD_IOC_PARSE: {
490 struct llog_ctxt *ctxt =
491 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
492 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
493 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
494 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
501 case OBD_IOC_DUMP_LOG: {
502 struct llog_ctxt *ctxt =
503 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
504 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
505 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
506 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
514 CDEBUG(D_HA, "syncing mds %s\n", obd->obd_name);
515 rc = fsfilt_sync(obd, obd->u.mds.mds_sb);
519 case OBD_IOC_SET_READONLY: {
521 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
522 BDEVNAME_DECLARE_STORAGE(tmp);
523 CERROR("*** setting device %s read-only ***\n",
524 ll_bdevname(obd->u.mds.mds_sb, tmp));
526 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
528 rc = fsfilt_commit(obd, inode, handle, 1);
530 CDEBUG(D_HA, "syncing mds %s\n", obd->obd_name);
531 rc = fsfilt_sync(obd, obd->u.mds.mds_sb);
533 lvfs_set_rdonly(lvfs_sbdev(obd->u.mds.mds_sb));
537 case OBD_IOC_CATLOGLIST: {
538 int count = mds->mds_lov_desc.ld_tgt_count;
539 rc = llog_catalog_list(obd, count, data);
543 case OBD_IOC_LLOG_CHECK:
544 case OBD_IOC_LLOG_CANCEL:
545 case OBD_IOC_LLOG_REMOVE: {
546 struct llog_ctxt *ctxt =
547 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
550 obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
551 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
552 rc = llog_ioctl(ctxt, cmd, data);
553 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
554 llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
555 rc2 = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"),
556 "mds_conn", 0, NULL);
561 case OBD_IOC_LLOG_INFO:
562 case OBD_IOC_LLOG_PRINT: {
563 struct llog_ctxt *ctxt =
564 llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
566 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
567 rc = llog_ioctl(ctxt, cmd, data);
568 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
573 case OBD_IOC_ABORT_RECOVERY:
574 CERROR("aborting recovery for device %s\n", obd->obd_name);
575 target_abort_recovery(obd);
579 CDEBUG(D_INFO, "unknown command %x\n", cmd);
585 /* use obd ops to offer management infrastructure */
586 static struct obd_ops mgs_obd_ops = {
587 .o_owner = THIS_MODULE,
588 .o_connect = mgs_connect,
589 .o_init_export = mgs_init_export,
590 .o_destroy_export = mgs_destroy_export,
591 .o_disconnect = mgs_disconnect,
592 .o_setup = mgs_setup,
593 .o_precleanup = mgs_precleanup,
594 .o_cleanup = mgs_cleanup,
595 .o_iocontrol = mgs_iocontrol,
596 .o_create = mgs_obd_create,
597 .o_destroy = mgs_obd_destroy,
598 .o_llog_init = mgs_llog_init,
599 .o_llog_finish = mgs_llog_finish,
600 .o_notify = mgs_notify,
603 static struct obd_ops mdt_obd_ops = {
604 .o_owner = THIS_MODULE,
605 .o_setup = mgt_setup,
606 .o_cleanup = mgt_cleanup,
609 static int __init mgs_init(void)
612 struct lprocfs_static_vars lvars;
614 lprocfs_init_vars(mgs, &lvars);
615 class_register_type(&mgs_obd_ops, lvars.module_vars, LUSTRE_MGS_NAME);
616 lprocfs_init_vars(mgt, &lvars);
617 class_register_type(&mgt_obd_ops, lvars.module_vars, LUSTRE_MGT_NAME);
622 static void /*__exit*/ mgs_exit(void)
624 class_unregister_type(LUSTRE_MGS_NAME);
625 class_unregister_type(LUSTRE_MGT_NAME);
628 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
629 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
630 MODULE_LICENSE("GPL");
632 module_init(mgs_init);
633 module_exit(mgs_exit);