4 * Lustre Metadata Server (mds) request handler
6 * Copyright (C) 2001 Cluster File Systems, Inc.
8 * This code is issued under the GNU General Public License.
9 * See the file COPYING in this distribution
11 * by Peter Braam <braam@clusterfs.com>
13 * This server is single threaded at present (but can easily be multi threaded).
20 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/quotaops.h>
27 #include <asm/unistd.h>
28 #include <linux/obd_support.h>
29 #include <linux/obd.h>
30 #include <linux/lustre_lib.h>
31 #include <linux/lustre_idl.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/obd_class.h>
36 static struct mds_obd *MDS;
38 // XXX make this networked!
39 static int mds_queue_req(struct mds_request *req)
41 struct mds_request *srv_req;
48 srv_req = kmalloc(sizeof(*srv_req), GFP_KERNEL);
54 printk("---> MDS at %d %p, incoming req %p, srv_req %p\n",
55 __LINE__, MDS, req, srv_req);
57 memset(srv_req, 0, sizeof(*req));
59 /* move the request buffer */
60 srv_req->rq_reqbuf = req->rq_reqbuf;
61 srv_req->rq_reqlen = req->rq_reqlen;
62 srv_req->rq_obd = MDS;
64 /* remember where it came from */
65 srv_req->rq_reply_handle = req;
67 list_add(&srv_req->rq_list, &MDS->mds_reqs);
68 wake_up(&MDS->mds_waitq);
72 /* XXX replace with networking code */
73 int mds_reply(struct mds_request *req)
75 struct mds_request *clnt_req = req->rq_reply_handle;
79 /* free the request buffer */
80 kfree(req->rq_reqbuf);
81 req->rq_reqbuf = NULL;
83 /* move the reply to the client */
84 clnt_req->rq_replen = req->rq_replen;
85 clnt_req->rq_repbuf = req->rq_repbuf;
86 req->rq_repbuf = NULL;
89 /* wake up the client */
90 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
95 int mds_error(struct mds_request *req)
97 struct mds_rep_hdr *hdr;
100 hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
106 memset(hdr, 0, sizeof(*hdr));
108 hdr->seqno = req->rq_reqhdr->seqno;
109 hdr->status = req->rq_status;
110 hdr->type = MDS_TYPE_ERR;
112 req->rq_repbuf = (char *)hdr;
113 req->rq_replen = sizeof(*hdr);
116 return mds_reply(req);
121 static struct dentry *mds_fid2dentry(struct mds_obd *mds, struct lustre_fid *fid)
126 inode = iget(mds->mds_sb, fid->id);
131 de = d_alloc_root(inode);
142 int mds_getattr(struct mds_request *req)
144 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1);
149 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
150 &req->rq_replen, &req->rq_repbuf);
153 printk("mds: out of memory\n");
154 req->rq_status = -ENOMEM;
158 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
163 req->rq_rephdr->status = -ENOENT;
168 rep->atime = inode->i_atime;
169 rep->ctime = inode->i_ctime;
170 rep->mtime = inode->i_mtime;
171 rep->uid = inode->i_uid;
172 rep->gid = inode->i_gid;
173 rep->size = inode->i_size;
174 rep->mode = inode->i_mode;
181 //int mds_handle(struct mds_conn *conn, int len, char *buf)
182 int mds_handle(struct mds_request *req)
185 struct mds_req_hdr *hdr;
189 hdr = (struct mds_req_hdr *)req->rq_reqbuf;
191 if (NTOH__u32(hdr->type) != MDS_TYPE_REQ) {
192 printk("lustre_mds: wrong packet type sent %d\n",
193 NTOH__u32(hdr->type));
198 rc = mds_unpack_req(req->rq_reqbuf, req->rq_reqlen,
199 &req->rq_reqhdr, &req->rq_req);
201 printk("lustre_mds: Invalid request\n");
206 switch (req->rq_reqhdr->opc) {
209 CDEBUG(D_INODE, "getattr\n");
210 rc = mds_getattr(req);
214 return mds_getattr(req);
217 return mds_getattr(req);
220 return mds_getattr(req);
223 return mds_getattr(req);
226 return mds_getattr(req);
229 return mds_getattr(req);
232 return mds_getattr(req);
235 return mds_getattr(req);
238 return mds_getattr(req);
241 return mds_getattr(req);
244 return mds_error(req);
249 printk("mds: processing error %d\n", rc);
252 CDEBUG(D_INODE, "sending reply\n");
260 static void mds_timer_run(unsigned long __data)
262 struct task_struct * p = (struct task_struct *) __data;
267 int mds_main(void *arg)
269 struct mds_obd *mds = (struct mds_obd *) arg;
270 struct timer_list timer;
274 spin_lock_irq(¤t->sigmask_lock);
275 sigfillset(¤t->blocked);
276 recalc_sigpending(current);
277 spin_unlock_irq(¤t->sigmask_lock);
279 sprintf(current->comm, "lustre_mds");
281 /* Set up an interval timer which can be used to trigger a
282 wakeup after the interval expires */
284 timer.data = (unsigned long) current;
285 timer.function = mds_timer_run;
286 mds->mds_timer = &timer;
288 /* Record that the thread is running */
289 mds->mds_thread = current;
290 wake_up(&mds->mds_done_waitq);
292 printk(KERN_INFO "lustre_mds starting. Commit interval %d seconds\n",
293 mds->mds_interval / HZ);
295 /* XXX maintain a list of all managed devices: insert here */
297 /* And now, wait forever for commit wakeup events. */
299 struct mds_request *request;
302 if (mds->mds_flags & MDS_UNMOUNT)
306 wake_up(&mds->mds_done_waitq);
307 interruptible_sleep_on(&mds->mds_waitq);
309 CDEBUG(D_INODE, "lustre_mds wakes\n");
310 CDEBUG(D_INODE, "pick up req here and continue\n");
312 if (list_empty(&mds->mds_reqs)) {
313 CDEBUG(D_INODE, "woke because of timer\n");
315 request = list_entry(mds->mds_reqs.next,
316 struct mds_request, rq_list);
317 list_del(&request->rq_list);
318 rc = mds_handle(request);
322 del_timer_sync(mds->mds_timer);
324 /* XXX maintain a list of all managed devices: cleanup here */
326 mds->mds_thread = NULL;
327 wake_up(&mds->mds_done_waitq);
328 printk("lustre_mds: exiting\n");
332 static void mds_stop_srv_thread(struct mds_obd *mds)
334 mds->mds_flags |= MDS_UNMOUNT;
336 while (mds->mds_thread) {
337 wake_up(&mds->mds_waitq);
338 sleep_on(&mds->mds_done_waitq);
342 static void mds_start_srv_thread(struct mds_obd *mds)
344 init_waitqueue_head(&mds->mds_waitq);
345 init_waitqueue_head(&mds->mds_done_waitq);
346 kernel_thread(mds_main, (void *)mds,
347 CLONE_VM | CLONE_FS | CLONE_FILES);
348 while (!mds->mds_thread)
349 sleep_on(&mds->mds_done_waitq);
352 /* mount the file system (secretly) */
353 static int mds_setup(struct obd_device *obddev, obd_count len,
357 struct obd_ioctl_data* data = buf;
358 struct mds_obd *mds = &obddev->u.mds;
359 struct vfsmount *mnt;
363 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
364 data->ioc_inlbuf1, NULL);
371 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
372 if (!obddev->u.mds.mds_sb) {
377 INIT_LIST_HEAD(&mds->mds_reqs);
378 mds->mds_thread = NULL;
380 mds->mds_interval = 3 * HZ;
381 mds->mds_vfsmnt = mnt;
382 obddev->u.mds.mds_fstype = strdup(data->ioc_inlbuf2);
384 mds->mds_ctxt.pwdmnt = mnt;
385 mds->mds_ctxt.pwd = mnt->mnt_root;
386 mds->mds_ctxt.fs = KERNEL_DS;
389 spin_lock_init(&obddev->u.mds.fo_lock);
391 mds_start_srv_thread(mds);
398 static int mds_cleanup(struct obd_device * obddev)
400 struct super_block *sb;
401 struct mds_obd *mds = &obddev->u.mds;
405 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
410 if ( !list_empty(&obddev->obd_gen_clients) ) {
411 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
417 mds_stop_srv_thread(mds);
424 if (!list_empty(&mds->mds_reqs)) {
425 // XXX reply with errors and clean up
426 CDEBUG(D_INODE, "Request list not empty!\n");
430 mntput(mds->mds_vfsmnt);
432 kfree(mds->mds_fstype);
441 /* use obd ops to offer management infrastructure */
442 static struct obd_ops mds_obd_ops = {
444 o_cleanup: mds_cleanup,
447 static int __init mds_init(void)
449 obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
453 static void __exit mds_exit(void)
455 obd_unregister_type(LUSTRE_MDS_NAME);
458 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
459 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
460 MODULE_LICENSE("GPL");
463 // for testing (maybe this stays)
464 EXPORT_SYMBOL(mds_queue_req);
466 module_init(mds_init);
467 module_exit(mds_exit);