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)
78 mm_segment_t oldfs = get_fs();
79 /* dst->addr is a user address, but in a different task! */
81 rc = generic_file_read(file, (char *)(long)dst->addr,
90 /* XXX replace with networking code */
91 int mds_reply(struct mds_request *req)
93 struct mds_request *clnt_req = req->rq_reply_handle;
97 /* free the request buffer */
98 kfree(req->rq_reqbuf);
99 req->rq_reqbuf = NULL;
101 /* move the reply to the client */
102 clnt_req->rq_replen = req->rq_replen;
103 clnt_req->rq_repbuf = req->rq_repbuf;
104 req->rq_repbuf = NULL;
107 /* wake up the client */
108 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
113 int mds_error(struct mds_request *req)
115 struct mds_rep_hdr *hdr;
118 hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
124 memset(hdr, 0, sizeof(*hdr));
126 hdr->seqno = req->rq_reqhdr->seqno;
127 hdr->status = req->rq_status;
128 hdr->type = MDS_TYPE_ERR;
130 req->rq_repbuf = (char *)hdr;
131 req->rq_replen = sizeof(*hdr);
134 return mds_reply(req);
137 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid, struct vfsmount **mnt)
139 /* stolen from NFS */
140 struct super_block *sb = mds->mds_sb;
141 unsigned long ino = fid->id;
142 //__u32 generation = fid->generation;
143 __u32 generation = 0;
145 struct list_head *lp;
146 struct dentry *result;
149 *mnt = mntget(mds->mds_vfsmnt);
153 return ERR_PTR(-ESTALE);
155 inode = iget(sb, ino);
157 return ERR_PTR(-ENOMEM);
159 printk("--> mds_fid2dentry: sb %p\n", inode->i_sb);
161 if (is_bad_inode(inode)
162 || (generation && inode->i_generation != generation)
164 /* we didn't find the right inode.. */
166 "bad inode %lu, link: %d ct: %d or version %u/%u\n",
168 inode->i_nlink, atomic_read(&inode->i_count),
172 return ERR_PTR(-ESTALE);
175 /* now to find a dentry.
176 * If possible, get a well-connected one
178 spin_lock(&dcache_lock);
179 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
180 result = list_entry(lp,struct dentry, d_alias);
181 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
183 result->d_vfs_flags |= DCACHE_REFERENCED;
184 spin_unlock(&dcache_lock);
189 spin_unlock(&dcache_lock);
190 result = d_alloc_root(inode);
191 if (result == NULL) {
193 return ERR_PTR(-ENOMEM);
195 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
199 static inline void mds_get_objid(struct inode *inode, __u64 *id)
201 memcpy(id, &inode->u.ext2_i.i_data, sizeof(*id));
204 int mds_getattr(struct mds_request *req)
206 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1,
212 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
213 &req->rq_replen, &req->rq_repbuf);
216 printk("mds: out of memory\n");
217 req->rq_status = -ENOMEM;
221 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
226 req->rq_rephdr->status = -ENOENT;
231 rep->ino = inode->i_ino;
232 rep->atime = inode->i_atime;
233 rep->ctime = inode->i_ctime;
234 rep->mtime = inode->i_mtime;
235 rep->uid = inode->i_uid;
236 rep->gid = inode->i_gid;
237 rep->size = inode->i_size;
238 rep->mode = inode->i_mode;
239 rep->nlink = inode->i_nlink;
241 mds_get_objid(inode, &rep->objid);
246 int mds_readpage(struct mds_request *req)
248 struct vfsmount *mnt;
249 struct dentry *de = mds_fid2dentry(req->rq_obd, &req->rq_req->fid1,
252 struct niobuf *niobuf;
256 printk("mds_readpage: ino %ld\n", de->d_inode->i_ino);
257 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
258 &req->rq_replen, &req->rq_repbuf);
261 printk("mds: out of memory\n");
262 req->rq_status = -ENOMEM;
266 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
271 req->rq_rephdr->status = PTR_ERR(de);
275 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
276 /* note: in case of an error, dentry_open puts dentry */
279 req->rq_rephdr->status = PTR_ERR(file);
283 niobuf = mds_req_tgt(req->rq_req);
285 /* to make this asynchronous make sure that the handling function
286 doesn't send a reply when this function completes. Instead a
287 callback function would send the reply */
288 rc = mds_sendpage(req, file, req->rq_req->size, niobuf);
291 req->rq_rephdr->status = rc;
296 int mds_reint(struct mds_request *req)
299 char *buf = mds_req_tgt(req->rq_req);
300 int len = req->rq_req->tgtlen;
301 struct mds_update_record rec;
303 rc = mds_update_unpack(buf, len, &rec);
305 printk(__FUNCTION__ ": invalid record\n");
309 rc = mds_reint_rec(&rec, req);
313 //int mds_handle(struct mds_conn *conn, int len, char *buf)
314 int mds_handle(struct mds_request *req)
317 struct mds_req_hdr *hdr;
321 hdr = (struct mds_req_hdr *)req->rq_reqbuf;
323 if (NTOH__u32(hdr->type) != MDS_TYPE_REQ) {
324 printk("lustre_mds: wrong packet type sent %d\n",
325 NTOH__u32(hdr->type));
330 rc = mds_unpack_req(req->rq_reqbuf, req->rq_reqlen,
331 &req->rq_reqhdr, &req->rq_req);
333 printk("lustre_mds: Invalid request\n");
338 switch (req->rq_reqhdr->opc) {
341 CDEBUG(D_INODE, "getattr\n");
342 rc = mds_getattr(req);
346 CDEBUG(D_INODE, "readpage\n");
347 rc = mds_readpage(req);
351 CDEBUG(D_INODE, "reint\n");
356 return mds_error(req);
361 printk("mds: processing error %d\n", rc);
364 CDEBUG(D_INODE, "sending reply\n");
372 static void mds_timer_run(unsigned long __data)
374 struct task_struct * p = (struct task_struct *) __data;
379 int mds_main(void *arg)
381 struct mds_obd *mds = (struct mds_obd *) arg;
382 struct timer_list timer;
386 spin_lock_irq(¤t->sigmask_lock);
387 sigfillset(¤t->blocked);
388 recalc_sigpending(current);
389 spin_unlock_irq(¤t->sigmask_lock);
391 sprintf(current->comm, "lustre_mds");
393 /* Set up an interval timer which can be used to trigger a
394 wakeup after the interval expires */
396 timer.data = (unsigned long) current;
397 timer.function = mds_timer_run;
398 mds->mds_timer = &timer;
400 /* Record that the thread is running */
401 mds->mds_thread = current;
402 wake_up(&mds->mds_done_waitq);
404 printk(KERN_INFO "lustre_mds starting. Commit interval %d seconds\n",
405 mds->mds_interval / HZ);
407 /* XXX maintain a list of all managed devices: insert here */
409 /* And now, wait forever for commit wakeup events. */
411 struct mds_request *request;
414 if (mds->mds_flags & MDS_UNMOUNT)
418 wake_up(&mds->mds_done_waitq);
419 interruptible_sleep_on(&mds->mds_waitq);
421 CDEBUG(D_INODE, "lustre_mds wakes\n");
422 CDEBUG(D_INODE, "pick up req here and continue\n");
424 if (list_empty(&mds->mds_reqs)) {
425 CDEBUG(D_INODE, "woke because of timer\n");
427 request = list_entry(mds->mds_reqs.next,
428 struct mds_request, rq_list);
429 list_del(&request->rq_list);
430 rc = mds_handle(request);
434 del_timer_sync(mds->mds_timer);
436 /* XXX maintain a list of all managed devices: cleanup here */
438 mds->mds_thread = NULL;
439 wake_up(&mds->mds_done_waitq);
440 printk("lustre_mds: exiting\n");
444 static void mds_stop_srv_thread(struct mds_obd *mds)
446 mds->mds_flags |= MDS_UNMOUNT;
448 while (mds->mds_thread) {
449 wake_up(&mds->mds_waitq);
450 sleep_on(&mds->mds_done_waitq);
454 static void mds_start_srv_thread(struct mds_obd *mds)
456 init_waitqueue_head(&mds->mds_waitq);
457 init_waitqueue_head(&mds->mds_done_waitq);
458 kernel_thread(mds_main, (void *)mds,
459 CLONE_VM | CLONE_FS | CLONE_FILES);
460 while (!mds->mds_thread)
461 sleep_on(&mds->mds_done_waitq);
464 /* mount the file system (secretly) */
465 static int mds_setup(struct obd_device *obddev, obd_count len,
469 struct obd_ioctl_data* data = buf;
470 struct mds_obd *mds = &obddev->u.mds;
471 struct vfsmount *mnt;
475 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
476 data->ioc_inlbuf1, NULL);
483 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
484 if (!obddev->u.mds.mds_sb) {
489 INIT_LIST_HEAD(&mds->mds_reqs);
490 mds->mds_thread = NULL;
492 mds->mds_interval = 3 * HZ;
493 mds->mds_vfsmnt = mnt;
494 obddev->u.mds.mds_fstype = strdup(data->ioc_inlbuf2);
496 mds->mds_ctxt.pwdmnt = mnt;
497 mds->mds_ctxt.pwd = mnt->mnt_root;
498 mds->mds_ctxt.fs = KERNEL_DS;
501 spin_lock_init(&obddev->u.mds.mds_lock);
503 mds_start_srv_thread(mds);
510 static int mds_cleanup(struct obd_device * obddev)
512 struct super_block *sb;
513 struct mds_obd *mds = &obddev->u.mds;
517 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
522 if ( !list_empty(&obddev->obd_gen_clients) ) {
523 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
529 mds_stop_srv_thread(mds);
536 if (!list_empty(&mds->mds_reqs)) {
537 // XXX reply with errors and clean up
538 CDEBUG(D_INODE, "Request list not empty!\n");
542 mntput(mds->mds_vfsmnt);
544 kfree(mds->mds_fstype);
553 /* use obd ops to offer management infrastructure */
554 static struct obd_ops mds_obd_ops = {
556 o_cleanup: mds_cleanup,
559 static int __init mds_init(void)
561 obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
565 static void __exit mds_exit(void)
567 obd_unregister_type(LUSTRE_MDS_NAME);
570 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
571 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
572 MODULE_LICENSE("GPL");
575 // for testing (maybe this stays)
576 EXPORT_SYMBOL(mds_queue_req);
578 module_init(mds_init);
579 module_exit(mds_exit);