Whamcloud - gitweb
14d670e8d1b8a3b238f3704ad98f3e9344ecd14f
[fs/lustre-release.git] / lustre / mds / handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/handler.c
5  *
6  *  Lustre Metadata Server (mds) request handler
7  *
8  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
9  *
10  *  This code is issued under the GNU General Public License.
11  *  See the file COPYING in this distribution
12  *
13  *  by Peter Braam <braam@clusterfs.com>
14  *
15  *  This server is single threaded at present (but can easily be multi threaded)
16  *
17  */
18
19 #define EXPORT_SYMTAB
20
21 #include <linux/version.h>
22 #include <linux/module.h>
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26 #include <linux/quotaops.h>
27 #include <asm/unistd.h>
28 #include <asm/uaccess.h>
29
30 #define DEBUG_SUBSYSTEM S_MDS
31
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_lib.h>
34 #include <linux/lustre_net.h>
35
36 int mds_sendpage(struct ptlrpc_request *req, struct file *file,
37                  __u64 offset, struct niobuf *dst)
38 {
39         int rc = 0;
40         mm_segment_t oldfs = get_fs();
41
42         if (req->rq_peer.peer_nid == 0) {
43                 /* dst->addr is a user address, but in a different task! */
44                 char *buf = (char *)(long)dst->addr;
45
46                 set_fs(KERNEL_DS);
47                 rc = mds_fs_readpage(&req->rq_obd->u.mds, file, buf, PAGE_SIZE,
48                                      &offset);
49                 set_fs(oldfs);
50
51                 if (rc != PAGE_SIZE) {
52                         rc = -EIO;
53                         GOTO(out, rc);
54                 }
55                 EXIT;
56         } else {
57                 struct ptlrpc_bulk_desc *bulk;
58                 char *buf;
59
60                 bulk = ptlrpc_prep_bulk(&req->rq_peer);
61                 if (bulk == NULL) {
62                         rc = -ENOMEM;
63                         GOTO(out, rc);
64                 }
65
66                 bulk->b_xid = req->rq_xid;
67
68                 OBD_ALLOC(buf, PAGE_SIZE);
69                 if (!buf) {
70                         rc = -ENOMEM;
71                         GOTO(cleanup_bulk, rc);
72                 }
73
74                 set_fs(KERNEL_DS);
75                 rc = mds_fs_readpage(&req->rq_obd->u.mds, file, buf, PAGE_SIZE,
76                                      &offset);
77                 set_fs(oldfs);
78
79                 if (rc != PAGE_SIZE) {
80                         rc = -EIO;
81                         GOTO(cleanup_buf, rc);
82                 }
83
84                 bulk->b_buf = buf;
85                 bulk->b_buflen = PAGE_SIZE;
86
87                 rc = ptlrpc_send_bulk(bulk, MDS_BULK_PORTAL);
88                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
89                         CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
90                                OBD_FAIL_MDS_SENDPAGE, rc);
91                         PtlMDUnlink(bulk->b_md_h);
92                         GOTO(cleanup_buf, rc);
93                 }
94                 wait_event_interruptible(bulk->b_waitq,
95                                          ptlrpc_check_bulk_sent(bulk));
96
97                 if (bulk->b_flags == PTL_RPC_INTR) {
98                         rc = -EINTR;
99                         GOTO(cleanup_buf, rc);
100                 }
101
102                 EXIT;
103         cleanup_buf:
104                 OBD_FREE(buf, PAGE_SIZE);
105         cleanup_bulk:
106                 OBD_FREE(bulk, sizeof(*bulk));
107         }
108 out:
109         return rc;
110 }
111
112 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
113                               struct vfsmount **mnt)
114 {
115         /* stolen from NFS */
116         struct super_block *sb = mds->mds_sb;
117         unsigned long ino = fid->id;
118         __u32 generation = fid->generation;
119         struct inode *inode;
120         struct list_head *lp;
121         struct dentry *result;
122
123         if (ino == 0)
124                 return ERR_PTR(-ESTALE);
125
126         inode = iget(sb, ino);
127         if (inode == NULL)
128                 return ERR_PTR(-ENOMEM);
129
130         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
131
132         if (is_bad_inode(inode) ||
133             (generation && inode->i_generation != generation)) {
134                 /* we didn't find the right inode.. */
135                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
136                         inode->i_ino,
137                         inode->i_nlink, atomic_read(&inode->i_count),
138                         inode->i_generation,
139                         generation);
140                 LBUG();
141                 iput(inode);
142                 return ERR_PTR(-ESTALE);
143         }
144
145         /* now to find a dentry.
146          * If possible, get a well-connected one
147          */
148         if (mnt)
149                 *mnt = mds->mds_vfsmnt;
150         spin_lock(&dcache_lock);
151         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
152                 result = list_entry(lp,struct dentry, d_alias);
153                 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
154                         dget_locked(result);
155                         result->d_vfs_flags |= DCACHE_REFERENCED;
156                         spin_unlock(&dcache_lock);
157                         iput(inode);
158                         if (mnt)
159                                 mntget(*mnt);
160                         return result;
161                 }
162         }
163         spin_unlock(&dcache_lock);
164         result = d_alloc_root(inode);
165         if (result == NULL) {
166                 iput(inode);
167                 return ERR_PTR(-ENOMEM);
168         }
169         if (mnt)
170                 mntget(*mnt);
171         result->d_flags |= DCACHE_NFSD_DISCONNECTED;
172         return result;
173 }
174
175 int mds_getattr(struct ptlrpc_request *req)
176 {
177         struct dentry *de;
178         struct inode *inode;
179         struct mds_body *body;
180         struct mds_obd *mds = &req->rq_obd->u.mds;
181         int rc, size = sizeof(*body);
182         ENTRY;
183
184         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repbuf);
185         req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
186         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
187                 CERROR("mds: out of memory\n");
188                 req->rq_status = -ENOMEM;
189                 RETURN(0);
190         }
191
192         body = lustre_msg_buf(req->rq_reqmsg, 0);
193         de = mds_fid2dentry(mds, &body->fid1, NULL);
194         if (IS_ERR(de)) {
195                 req->rq_status = -ENOENT;
196                 RETURN(0);
197         }
198
199         body = lustre_msg_buf(req->rq_repmsg, 0);
200         inode = de->d_inode;
201         body->ino = inode->i_ino;
202         body->generation = inode->i_generation;
203         body->atime = inode->i_atime;
204         body->ctime = inode->i_ctime;
205         body->mtime = inode->i_mtime;
206         body->uid = inode->i_uid;
207         body->gid = inode->i_gid;
208         body->size = inode->i_size;
209         body->mode = inode->i_mode;
210         body->nlink = inode->i_nlink;
211         body->valid = ~0;
212         mds_fs_get_objid(mds, inode, &body->objid);
213         l_dput(de);
214         RETURN(0);
215 }
216
217 int mds_open(struct ptlrpc_request *req)
218 {
219         struct dentry *de;
220         struct mds_body *body;
221         struct file *file;
222         struct vfsmount *mnt;
223         __u32 flags;
224         int rc, size = sizeof(*body);
225         ENTRY;
226
227         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repbuf);
228         req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
229         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
230                 CERROR("mds: out of memory\n");
231                 req->rq_status = -ENOMEM;
232                 RETURN(0);
233         }
234
235         body = lustre_msg_buf(req->rq_reqmsg, 0);
236         de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
237         if (IS_ERR(de)) {
238                 req->rq_status = -ENOENT;
239                 RETURN(0);
240         }
241         flags = body->flags;
242         file = dentry_open(de, mnt, flags);
243         if (!file || IS_ERR(file)) {
244                 req->rq_status = -EINVAL;
245                 RETURN(0);
246         }
247
248         body = lustre_msg_buf(req->rq_repmsg, 0);
249         body->objid = (__u64) (unsigned long)file;
250         RETURN(0);
251 }
252
253 int mds_close(struct ptlrpc_request *req)
254 {
255         struct dentry *de;
256         struct mds_body *body;
257         struct file *file;
258         struct vfsmount *mnt;
259         int rc;
260         ENTRY;
261
262         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repbuf);
263         req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
264         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
265                 CERROR("mds: out of memory\n");
266                 req->rq_status = -ENOMEM;
267                 RETURN(0);
268         }
269
270         body = lustre_msg_buf(req->rq_reqmsg, 0);
271         de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
272         if (IS_ERR(de)) {
273                 req->rq_status = -ENOENT;
274                 RETURN(0);
275         }
276
277         file = (struct file *)(unsigned long)body->objid;
278         req->rq_status = filp_close(file, 0);
279         l_dput(de);
280         mntput(mnt);
281
282         RETURN(0);
283 }
284
285 int mds_readpage(struct ptlrpc_request *req)
286 {
287         struct vfsmount *mnt;
288         struct dentry *de;
289         struct file *file;
290         struct niobuf *niobuf;
291         struct mds_body *body;
292         int rc, size = sizeof(*body);
293         ENTRY;
294
295         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repbuf);
296         req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
297         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
298                 CERROR("mds: out of memory\n");
299                 req->rq_status = -ENOMEM;
300                 RETURN(0);
301         }
302
303         body = lustre_msg_buf(req->rq_reqmsg, 0);
304         de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
305         if (IS_ERR(de)) {
306                 req->rq_status = PTR_ERR(de);
307                 RETURN(0);
308         }
309
310         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
311
312         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
313         /* note: in case of an error, dentry_open puts dentry */
314         if (IS_ERR(file)) {
315                 req->rq_status = PTR_ERR(file);
316                 RETURN(0);
317         }
318
319         niobuf = lustre_msg_buf(req->rq_reqmsg, 1);
320         if (!niobuf) {
321                 req->rq_status = -EINVAL;
322                 LBUG();
323                 RETURN(0);
324         }
325
326         /* to make this asynchronous make sure that the handling function
327            doesn't send a reply when this function completes. Instead a
328            callback function would send the reply */
329         rc = mds_sendpage(req, file, body->size, niobuf);
330
331         filp_close(file, 0);
332         req->rq_status = rc;
333         RETURN(0);
334 }
335
336 int mds_reint(struct ptlrpc_request *req)
337 {
338         int rc;
339         struct mds_update_record rec;
340
341         rc = mds_update_unpack(req, &rec);
342         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
343                 CERROR("invalid record\n");
344                 req->rq_status = -EINVAL;
345                 RETURN(0);
346         }
347         /* rc will be used to interrupt a for loop over multiple records */
348         rc = mds_reint_rec(&rec, req);
349         return 0;
350 }
351
352 int mds_handle(struct obd_device *dev, struct ptlrpc_service *svc,
353                struct ptlrpc_request *req)
354 {
355         int rc;
356         ENTRY;
357
358         rc = lustre_unpack_msg(req->rq_reqbuf, req->rq_reqlen);
359         req->rq_reqmsg = (struct lustre_msg *)req->rq_reqbuf;
360         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
361                 CERROR("lustre_mds: Invalid request\n");
362                 GOTO(out, rc);
363         }
364
365         if (req->rq_reqmsg->type != PTL_RPC_REQUEST) {
366                 CERROR("lustre_mds: wrong packet type sent %d\n",
367                        req->rq_reqmsg->type);
368                 GOTO(out, rc = -EINVAL);
369         }
370
371         switch (req->rq_reqmsg->opc) {
372         case MDS_GETATTR:
373                 CDEBUG(D_INODE, "getattr\n");
374                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
375                 rc = mds_getattr(req);
376                 break;
377
378         case MDS_READPAGE:
379                 CDEBUG(D_INODE, "readpage\n");
380                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
381                 rc = mds_readpage(req);
382
383                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
384                         return 0;
385                 break;
386
387         case MDS_REINT:
388                 CDEBUG(D_INODE, "reint\n");
389                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
390                 rc = mds_reint(req);
391                 break;
392
393         case MDS_OPEN:
394                 CDEBUG(D_INODE, "open\n");
395                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
396                 rc = mds_open(req);
397                 break;
398
399         case MDS_CLOSE:
400                 CDEBUG(D_INODE, "close\n");
401                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
402                 rc = mds_close(req);
403                 break;
404
405         default:
406                 rc = ptlrpc_error(svc, req);
407                 RETURN(rc);
408         }
409
410         EXIT;
411 out:
412         if (rc) {
413                 ptlrpc_error(svc, req);
414         } else {
415                 CDEBUG(D_NET, "sending reply\n");
416                 ptlrpc_reply(svc, req);
417         }
418
419         return 0;
420 }
421
422 static int mds_prep(struct obd_device *obddev)
423 {
424         struct obd_run_ctxt saved;
425         struct mds_obd *mds = &obddev->u.mds;
426         struct super_operations *s_ops;
427         int err;
428
429         mds->mds_service = ptlrpc_init_svc(128 * 1024,
430                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
431                                            "self", mds_handle);
432
433         if (!mds->mds_service) {
434                 CERROR("failed to start service\n");
435                 RETURN(-EINVAL);
436         }
437
438         err = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
439         if (err) {
440                 CERROR("cannot start thread\n");
441                 GOTO(err_svc, err);
442         }
443
444         push_ctxt(&saved, &mds->mds_ctxt);
445         err = simple_mkdir(current->fs->pwd, "ROOT", 0700);
446         err = simple_mkdir(current->fs->pwd, "FH", 0700);
447         pop_ctxt(&saved);
448
449         /*
450          * Replace the client filesystem delete_inode method with our own,
451          * so that we can clear the object ID before the inode is deleted.
452          * The fs_delete_inode method will call cl_delete_inode for us.
453          *
454          * We need to do this for the MDS superblock only, hence we install
455          * a modified copy of the original superblock method table.
456          *
457          * We still assume that there is only a single MDS client filesystem
458          * type, as we don't have access to the mds struct in * delete_inode.
459          */
460         OBD_ALLOC(s_ops, sizeof(*s_ops));
461         memcpy(s_ops, mds->mds_sb->s_op, sizeof(*s_ops));
462         mds->mds_fsops->cl_delete_inode = s_ops->delete_inode;
463         s_ops->delete_inode = mds->mds_fsops->fs_delete_inode;
464         mds->mds_sb->s_op = s_ops;
465
466         RETURN(0);
467
468 err_svc:
469         rpc_unregister_service(mds->mds_service);
470         OBD_FREE(mds->mds_service, sizeof(*mds->mds_service));
471
472         return(err);
473 }
474
475 /* mount the file system (secretly) */
476 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
477 {
478         struct obd_ioctl_data* data = buf;
479         struct mds_obd *mds = &obddev->u.mds;
480         struct vfsmount *mnt;
481         int err = 0;
482         ENTRY;
483
484 #ifdef CONFIG_DEV_RDONLY
485         dev_clear_rdonly(2);
486 #endif
487         mds->mds_fstype = strdup(data->ioc_inlbuf2);
488
489         if (!strcmp(mds->mds_fstype, "ext3"))
490                 mds->mds_fsops = &mds_ext3_fs_ops;
491         else if (!strcmp(mds->mds_fstype, "ext2"))
492                 mds->mds_fsops = &mds_ext2_fs_ops;
493         else {
494                 CERROR("unsupported MDS filesystem type %s\n", mds->mds_fstype);
495                 GOTO(err_kfree, (err = -EPERM));
496         }
497
498         MOD_INC_USE_COUNT;
499         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
500         if (IS_ERR(mnt)) {
501                 CERROR("do_kern_mount failed: %d\n", err);
502                 GOTO(err_dec, err = PTR_ERR(mnt));
503         }
504
505         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
506         if (!mds->mds_sb)
507                 GOTO(err_put, (err = -ENODEV));
508
509         mds->mds_vfsmnt = mnt;
510         mds->mds_ctxt.pwdmnt = mnt;
511         mds->mds_ctxt.pwd = mnt->mnt_root;
512         mds->mds_ctxt.fs = KERNEL_DS;
513
514         err = mds_prep(obddev);
515         if (err)
516                 GOTO(err_put, err);
517
518         RETURN(0);
519
520 err_put:
521         unlock_kernel();
522         mntput(mds->mds_vfsmnt);
523         mds->mds_sb = 0;
524         lock_kernel();
525 err_dec:
526         MOD_DEC_USE_COUNT;
527 err_kfree:
528         kfree(mds->mds_fstype);
529         return err;
530 }
531
532 static int mds_cleanup(struct obd_device * obddev)
533 {
534         struct super_operations *s_ops = NULL;
535         struct super_block *sb;
536         struct mds_obd *mds = &obddev->u.mds;
537
538         ENTRY;
539
540         if ( !list_empty(&obddev->obd_gen_clients) ) {
541                 CERROR("still has clients!\n");
542                 RETURN(-EBUSY);
543         }
544
545         ptlrpc_stop_thread(mds->mds_service);
546         rpc_unregister_service(mds->mds_service);
547         if (!list_empty(&mds->mds_service->srv_reqs)) {
548                 // XXX reply with errors and clean up
549                 CERROR("Request list not empty!\n");
550         }
551         OBD_FREE(mds->mds_service, sizeof(*mds->mds_service));
552
553         sb = mds->mds_sb;
554         if (!mds->mds_sb)
555                 RETURN(0);
556
557         s_ops = sb->s_op;
558
559         unlock_kernel();
560         mntput(mds->mds_vfsmnt);
561         mds->mds_sb = 0;
562         kfree(mds->mds_fstype);
563         lock_kernel();
564 #ifdef CONFIG_DEV_RDONLY
565         dev_clear_rdonly(2);
566 #endif
567         OBD_FREE(s_ops, sizeof(*s_ops));
568
569         MOD_DEC_USE_COUNT;
570         RETURN(0);
571 }
572
573 /* use obd ops to offer management infrastructure */
574 static struct obd_ops mds_obd_ops = {
575         o_setup:       mds_setup,
576         o_cleanup:     mds_cleanup,
577 };
578
579 static int __init mds_init(void)
580 {
581         obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
582         return 0;
583 }
584
585 static void __exit mds_exit(void)
586 {
587         obd_unregister_type(LUSTRE_MDS_NAME);
588 }
589
590 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
591 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
592 MODULE_LICENSE("GPL");
593
594 module_init(mds_init);
595 module_exit(mds_exit);