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 do this over the net */
74 int mds_sendpage(struct mds_request *req, struct file *file,
75 __u64 offset, struct niobuf *dst)
79 rc = generic_file_read(file, (char *)(long)dst->addr,
87 /* XXX replace with networking code */
88 int mds_reply(struct mds_request *req)
90 struct mds_request *clnt_req = req->rq_reply_handle;
94 /* free the request buffer */
95 kfree(req->rq_reqbuf);
96 req->rq_reqbuf = NULL;
98 /* move the reply to the client */
99 clnt_req->rq_replen = req->rq_replen;
100 clnt_req->rq_repbuf = req->rq_repbuf;
101 req->rq_repbuf = NULL;
104 /* wake up the client */
105 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
110 int mds_error(struct mds_request *req)
112 struct mds_rep_hdr *hdr;
115 hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
121 memset(hdr, 0, sizeof(*hdr));
123 hdr->seqno = req->rq_reqhdr->seqno;
124 hdr->status = req->rq_status;
125 hdr->type = MDS_TYPE_ERR;
127 req->rq_repbuf = (char *)hdr;
128 req->rq_replen = sizeof(*hdr);
131 return mds_reply(req);
134 static struct dentry *mds_fid2dentry(struct mds_obd *mds, struct lustre_fid *fid, struct vfsmount **mnt)
137 /* iget isn't really right if the inode is currently unallocated!!
138 * This should really all be done inside each filesystem
140 * ext2fs' read_inode has been strengthed to return a bad_inode if the inode
143 * Currently we don't know the generation for parent directory, so a generation
144 * of 0 means "accept any"
146 struct super_block *sb = mds->mds_sb;
147 unsigned long ino = fid->id;
148 __u32 generation = fid->generation;
150 struct list_head *lp;
151 struct dentry *result;
154 *mnt = mntget(mds->mds_vfsmnt);
158 return ERR_PTR(-ESTALE);
159 inode = iget(sb, ino);
161 return ERR_PTR(-ENOMEM);
163 printk("--> mds_fid2dentry: sb %p\n", inode->i_sb);
165 if (is_bad_inode(inode)
166 || (generation && inode->i_generation != generation)
168 /* we didn't find the right inode.. */
169 printk("mds_fid2dentry: Inode %lu, Bad count: %d %d or version %u %u\n",
171 inode->i_nlink, atomic_read(&inode->i_count),
176 return ERR_PTR(-ESTALE);
178 /* now to find a dentry.
179 * If possible, get a well-connected one
181 spin_lock(&dcache_lock);
182 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
183 result = list_entry(lp,struct dentry, d_alias);
184 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
186 result->d_vfs_flags |= DCACHE_REFERENCED;
187 spin_unlock(&dcache_lock);
192 spin_unlock(&dcache_lock);
193 result = d_alloc_root(inode);
194 if (result == NULL) {
196 return ERR_PTR(-ENOMEM);
198 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
202 int mds_getattr(struct mds_request *req)
204 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1,
210 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
211 &req->rq_replen, &req->rq_repbuf);
214 printk("mds: out of memory\n");
215 req->rq_status = -ENOMEM;
219 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
224 req->rq_rephdr->status = -ENOENT;
229 rep->ino = inode->i_ino;
230 rep->atime = inode->i_atime;
231 rep->ctime = inode->i_ctime;
232 rep->mtime = inode->i_mtime;
233 rep->uid = inode->i_uid;
234 rep->gid = inode->i_gid;
235 rep->size = inode->i_size;
236 rep->mode = inode->i_mode;
243 int mds_readpage(struct mds_request *req)
245 struct vfsmount *mnt;
246 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1,
249 struct niobuf *niobuf;
253 printk("mds_readpage: ino %ld\n", de->d_inode->i_ino);
254 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
255 &req->rq_replen, &req->rq_repbuf);
258 printk("mds: out of memory\n");
259 req->rq_status = -ENOMEM;
263 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
268 req->rq_rephdr->status = PTR_ERR(de);
272 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
273 /* note: in case of an error, dentry_open puts dentry */
276 req->rq_rephdr->status = PTR_ERR(file);
280 niobuf = (struct niobuf *)req->rq_req->tgt;
282 /* to make this asynchronous make sure that the handling function
283 doesn't send a reply when this function completes. Instead a
284 callback function would send the reply */
285 rc = mds_sendpage(req, file, req->rq_req->size, niobuf);
288 req->rq_rephdr->status = rc;
295 //int mds_handle(struct mds_conn *conn, int len, char *buf)
296 int mds_handle(struct mds_request *req)
299 struct mds_req_hdr *hdr;
303 hdr = (struct mds_req_hdr *)req->rq_reqbuf;
305 if (NTOH__u32(hdr->type) != MDS_TYPE_REQ) {
306 printk("lustre_mds: wrong packet type sent %d\n",
307 NTOH__u32(hdr->type));
312 rc = mds_unpack_req(req->rq_reqbuf, req->rq_reqlen,
313 &req->rq_reqhdr, &req->rq_req);
315 printk("lustre_mds: Invalid request\n");
320 switch (req->rq_reqhdr->opc) {
323 CDEBUG(D_INODE, "getattr\n");
324 rc = mds_getattr(req);
328 CDEBUG(D_INODE, "readpage\n");
329 rc = mds_readpage(req);
333 return mds_getattr(req);
336 return mds_getattr(req);
339 return mds_getattr(req);
342 return mds_getattr(req);
345 return mds_getattr(req);
348 return mds_getattr(req);
351 return mds_getattr(req);
354 return mds_getattr(req);
357 return mds_getattr(req);
360 return mds_error(req);
365 printk("mds: processing error %d\n", rc);
368 CDEBUG(D_INODE, "sending reply\n");
376 static void mds_timer_run(unsigned long __data)
378 struct task_struct * p = (struct task_struct *) __data;
383 int mds_main(void *arg)
385 struct mds_obd *mds = (struct mds_obd *) arg;
386 struct timer_list timer;
390 spin_lock_irq(¤t->sigmask_lock);
391 sigfillset(¤t->blocked);
392 recalc_sigpending(current);
393 spin_unlock_irq(¤t->sigmask_lock);
395 sprintf(current->comm, "lustre_mds");
397 /* Set up an interval timer which can be used to trigger a
398 wakeup after the interval expires */
400 timer.data = (unsigned long) current;
401 timer.function = mds_timer_run;
402 mds->mds_timer = &timer;
404 /* Record that the thread is running */
405 mds->mds_thread = current;
406 wake_up(&mds->mds_done_waitq);
408 printk(KERN_INFO "lustre_mds starting. Commit interval %d seconds\n",
409 mds->mds_interval / HZ);
411 /* XXX maintain a list of all managed devices: insert here */
413 /* And now, wait forever for commit wakeup events. */
415 struct mds_request *request;
418 if (mds->mds_flags & MDS_UNMOUNT)
422 wake_up(&mds->mds_done_waitq);
423 interruptible_sleep_on(&mds->mds_waitq);
425 CDEBUG(D_INODE, "lustre_mds wakes\n");
426 CDEBUG(D_INODE, "pick up req here and continue\n");
428 if (list_empty(&mds->mds_reqs)) {
429 CDEBUG(D_INODE, "woke because of timer\n");
431 request = list_entry(mds->mds_reqs.next,
432 struct mds_request, rq_list);
433 list_del(&request->rq_list);
434 rc = mds_handle(request);
438 del_timer_sync(mds->mds_timer);
440 /* XXX maintain a list of all managed devices: cleanup here */
442 mds->mds_thread = NULL;
443 wake_up(&mds->mds_done_waitq);
444 printk("lustre_mds: exiting\n");
448 static void mds_stop_srv_thread(struct mds_obd *mds)
450 mds->mds_flags |= MDS_UNMOUNT;
452 while (mds->mds_thread) {
453 wake_up(&mds->mds_waitq);
454 sleep_on(&mds->mds_done_waitq);
458 static void mds_start_srv_thread(struct mds_obd *mds)
460 init_waitqueue_head(&mds->mds_waitq);
461 init_waitqueue_head(&mds->mds_done_waitq);
462 kernel_thread(mds_main, (void *)mds,
463 CLONE_VM | CLONE_FS | CLONE_FILES);
464 while (!mds->mds_thread)
465 sleep_on(&mds->mds_done_waitq);
468 /* mount the file system (secretly) */
469 static int mds_setup(struct obd_device *obddev, obd_count len,
473 struct obd_ioctl_data* data = buf;
474 struct mds_obd *mds = &obddev->u.mds;
475 struct vfsmount *mnt;
479 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
480 data->ioc_inlbuf1, NULL);
487 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
488 if (!obddev->u.mds.mds_sb) {
493 INIT_LIST_HEAD(&mds->mds_reqs);
494 mds->mds_thread = NULL;
496 mds->mds_interval = 3 * HZ;
497 mds->mds_vfsmnt = mnt;
498 obddev->u.mds.mds_fstype = strdup(data->ioc_inlbuf2);
500 mds->mds_ctxt.pwdmnt = mnt;
501 mds->mds_ctxt.pwd = mnt->mnt_root;
502 mds->mds_ctxt.fs = KERNEL_DS;
505 spin_lock_init(&obddev->u.mds.mds_lock);
507 mds_start_srv_thread(mds);
514 static int mds_cleanup(struct obd_device * obddev)
516 struct super_block *sb;
517 struct mds_obd *mds = &obddev->u.mds;
521 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
526 if ( !list_empty(&obddev->obd_gen_clients) ) {
527 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
533 mds_stop_srv_thread(mds);
540 if (!list_empty(&mds->mds_reqs)) {
541 // XXX reply with errors and clean up
542 CDEBUG(D_INODE, "Request list not empty!\n");
546 mntput(mds->mds_vfsmnt);
548 kfree(mds->mds_fstype);
557 /* use obd ops to offer management infrastructure */
558 static struct obd_ops mds_obd_ops = {
560 o_cleanup: mds_cleanup,
563 static int __init mds_init(void)
565 obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
569 static void __exit mds_exit(void)
571 obd_unregister_type(LUSTRE_MDS_NAME);
574 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
575 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
576 MODULE_LICENSE("GPL");
579 // for testing (maybe this stays)
580 EXPORT_SYMBOL(mds_queue_req);
582 module_init(mds_init);
583 module_exit(mds_exit);