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 <asm/uaccess.h>
29 #include <linux/obd_support.h>
30 #include <linux/obd.h>
31 #include <linux/lustre_lib.h>
32 #include <linux/lustre_idl.h>
33 #include <linux/lustre_mds.h>
34 #include <linux/obd_class.h>
37 static struct mds_obd *MDS;
39 // XXX make this networked!
40 static int mds_queue_req(struct mds_request *req)
42 struct mds_request *srv_req;
49 srv_req = kmalloc(sizeof(*srv_req), GFP_KERNEL);
55 printk("---> MDS at %d %p, incoming req %p, srv_req %p\n",
56 __LINE__, MDS, req, srv_req);
58 memset(srv_req, 0, sizeof(*req));
60 /* move the request buffer */
61 srv_req->rq_reqbuf = req->rq_reqbuf;
62 srv_req->rq_reqlen = req->rq_reqlen;
63 srv_req->rq_obd = MDS;
65 /* remember where it came from */
66 srv_req->rq_reply_handle = req;
68 list_add(&srv_req->rq_list, &MDS->mds_reqs);
69 wake_up(&MDS->mds_waitq);
73 /* XXX replace with networking code */
74 int mds_reply(struct mds_request *req)
76 struct mds_request *clnt_req = req->rq_reply_handle;
80 /* free the request buffer */
81 kfree(req->rq_reqbuf);
82 req->rq_reqbuf = NULL;
84 /* move the reply to the client */
85 clnt_req->rq_replen = req->rq_replen;
86 clnt_req->rq_repbuf = req->rq_repbuf;
87 req->rq_repbuf = NULL;
90 /* wake up the client */
91 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
96 int mds_error(struct mds_request *req)
98 struct mds_rep_hdr *hdr;
101 hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
107 memset(hdr, 0, sizeof(*hdr));
109 hdr->seqno = req->rq_reqhdr->seqno;
110 hdr->status = req->rq_status;
111 hdr->type = MDS_TYPE_ERR;
113 req->rq_repbuf = (char *)hdr;
114 req->rq_replen = sizeof(*hdr);
117 return mds_reply(req);
122 static struct dentry *mds_fid2dentry(struct mds_obd *mds, struct lustre_fid *fid)
127 inode = iget(mds->mds_sb, fid->id);
132 de = d_alloc_root(inode);
143 int mds_getattr(struct mds_request *req)
145 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1);
150 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
151 &req->rq_replen, &req->rq_repbuf);
154 printk("mds: out of memory\n");
155 req->rq_status = -ENOMEM;
159 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
164 req->rq_rephdr->status = -ENOENT;
169 rep->atime = inode->i_atime;
170 rep->ctime = inode->i_ctime;
171 rep->mtime = inode->i_mtime;
172 rep->uid = inode->i_uid;
173 rep->gid = inode->i_gid;
174 rep->size = inode->i_size;
175 rep->mode = inode->i_mode;
183 //int mds_handle(struct mds_conn *conn, int len, char *buf)
184 int mds_handle(struct mds_request *req)
187 struct mds_req_hdr *hdr;
191 hdr = (struct mds_req_hdr *)req->rq_reqbuf;
193 if (NTOH__u32(hdr->type) != MDS_TYPE_REQ) {
194 printk("lustre_mds: wrong packet type sent %d\n",
195 NTOH__u32(hdr->type));
200 rc = mds_unpack_req(req->rq_reqbuf, req->rq_reqlen,
201 &req->rq_reqhdr, &req->rq_req);
203 printk("lustre_mds: Invalid request\n");
208 switch (req->rq_reqhdr->opc) {
211 CDEBUG(D_INODE, "getattr\n");
212 rc = mds_getattr(req);
216 return mds_getattr(req);
219 return mds_getattr(req);
222 return mds_getattr(req);
225 return mds_getattr(req);
228 return mds_getattr(req);
231 return mds_getattr(req);
234 return mds_getattr(req);
237 return mds_getattr(req);
240 return mds_getattr(req);
243 return mds_getattr(req);
246 return mds_error(req);
251 printk("mds: processing error %d\n", rc);
254 CDEBUG(D_INODE, "sending reply\n");
262 static void mds_timer_run(unsigned long __data)
264 struct task_struct * p = (struct task_struct *) __data;
269 int mds_main(void *arg)
271 struct mds_obd *mds = (struct mds_obd *) arg;
272 struct timer_list timer;
276 spin_lock_irq(¤t->sigmask_lock);
277 sigfillset(¤t->blocked);
278 recalc_sigpending(current);
279 spin_unlock_irq(¤t->sigmask_lock);
281 sprintf(current->comm, "lustre_mds");
283 /* Set up an interval timer which can be used to trigger a
284 wakeup after the interval expires */
286 timer.data = (unsigned long) current;
287 timer.function = mds_timer_run;
288 mds->mds_timer = &timer;
290 /* Record that the thread is running */
291 mds->mds_thread = current;
292 wake_up(&mds->mds_done_waitq);
294 printk(KERN_INFO "lustre_mds starting. Commit interval %d seconds\n",
295 mds->mds_interval / HZ);
297 /* XXX maintain a list of all managed devices: insert here */
299 /* And now, wait forever for commit wakeup events. */
301 struct mds_request *request;
304 if (mds->mds_flags & MDS_UNMOUNT)
308 wake_up(&mds->mds_done_waitq);
309 interruptible_sleep_on(&mds->mds_waitq);
311 CDEBUG(D_INODE, "lustre_mds wakes\n");
312 CDEBUG(D_INODE, "pick up req here and continue\n");
314 if (list_empty(&mds->mds_reqs)) {
315 CDEBUG(D_INODE, "woke because of timer\n");
317 request = list_entry(mds->mds_reqs.next,
318 struct mds_request, rq_list);
319 list_del(&request->rq_list);
320 rc = mds_handle(request);
324 del_timer_sync(mds->mds_timer);
326 /* XXX maintain a list of all managed devices: cleanup here */
328 mds->mds_thread = NULL;
329 wake_up(&mds->mds_done_waitq);
330 printk("lustre_mds: exiting\n");
334 static void mds_stop_srv_thread(struct mds_obd *mds)
336 mds->mds_flags |= MDS_UNMOUNT;
338 while (mds->mds_thread) {
339 wake_up(&mds->mds_waitq);
340 sleep_on(&mds->mds_done_waitq);
344 static void mds_start_srv_thread(struct mds_obd *mds)
346 init_waitqueue_head(&mds->mds_waitq);
347 init_waitqueue_head(&mds->mds_done_waitq);
348 kernel_thread(mds_main, (void *)mds,
349 CLONE_VM | CLONE_FS | CLONE_FILES);
350 while (!mds->mds_thread)
351 sleep_on(&mds->mds_done_waitq);
354 /* mount the file system (secretly) */
355 static int mds_setup(struct obd_device *obddev, obd_count len,
359 struct obd_ioctl_data* data = buf;
360 struct mds_obd *mds = &obddev->u.mds;
361 struct vfsmount *mnt;
365 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
366 data->ioc_inlbuf1, NULL);
373 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
374 if (!obddev->u.mds.mds_sb) {
379 INIT_LIST_HEAD(&mds->mds_reqs);
380 mds->mds_thread = NULL;
382 mds->mds_interval = 3 * HZ;
383 mds->mds_vfsmnt = mnt;
384 obddev->u.mds.mds_fstype = strdup(data->ioc_inlbuf2);
386 mds->mds_ctxt.pwdmnt = mnt;
387 mds->mds_ctxt.pwd = mnt->mnt_root;
388 mds->mds_ctxt.fs = KERNEL_DS;
391 spin_lock_init(&obddev->u.mds.mds_lock);
393 mds_start_srv_thread(mds);
400 static int mds_cleanup(struct obd_device * obddev)
402 struct super_block *sb;
403 struct mds_obd *mds = &obddev->u.mds;
407 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
412 if ( !list_empty(&obddev->obd_gen_clients) ) {
413 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
419 mds_stop_srv_thread(mds);
426 if (!list_empty(&mds->mds_reqs)) {
427 // XXX reply with errors and clean up
428 CDEBUG(D_INODE, "Request list not empty!\n");
432 mntput(mds->mds_vfsmnt);
434 kfree(mds->mds_fstype);
443 /* use obd ops to offer management infrastructure */
444 static struct obd_ops mds_obd_ops = {
446 o_cleanup: mds_cleanup,
449 static int __init mds_init(void)
451 obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
455 static void __exit mds_exit(void)
457 obd_unregister_type(LUSTRE_MDS_NAME);
460 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
461 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
462 MODULE_LICENSE("GPL");
465 // for testing (maybe this stays)
466 EXPORT_SYMBOL(mds_queue_req);
468 module_init(mds_init);
469 module_exit(mds_exit);