Whamcloud - gitweb
optional highmem buffers in obdecho/test_brw
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * These are the only exported functions, they provide some generic
10  * infrastructure for managing object devices
11  *
12  * Object Devices Class Driver
13  */
14
15 #define EXPORT_SYMTAB
16 #include <linux/config.h> /* for CONFIG_PROC_FS */
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/sched.h>
22 #include <linux/lp.h>
23 #include <linux/slab.h>
24 #include <linux/ioport.h>
25 #include <linux/fcntl.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
28 #include <linux/proc_fs.h>
29 #include <linux/fs.h>
30 #include <linux/poll.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <asm/io.h>
34 #include <asm/system.h>
35 #include <asm/poll.h>
36 #include <asm/uaccess.h>
37 #include <linux/miscdevice.h>
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/obd_support.h>
42 #include <linux/obd_class.h>
43 #include <linux/lustre_debug.h>
44 #include <linux/smp_lock.h>
45
46 struct semaphore obd_conf_sem;   /* serialize configuration commands */
47 struct obd_device obd_dev[MAX_OBD_DEVICES];
48 struct list_head obd_types;
49 unsigned long obd_memory = 0;
50
51 /* The following are visible and mutable through /proc/sys/lustre/. */
52 unsigned long obd_fail_loc = 0;
53 unsigned long obd_timeout = 100;
54 char obd_recovery_upcall[128] = "/usr/lib/lustre/ha_assist";
55
56 int  obdclass_highmem = 0;
57
58 extern struct obd_type *class_nm_to_type(char *nm);
59
60 /*  opening /dev/obd */
61 static int obd_class_open(struct inode * inode, struct file * file)
62 {
63         ENTRY;
64
65         file->private_data = NULL;
66         MOD_INC_USE_COUNT;
67         RETURN(0);
68 }
69
70 /*  closing /dev/obd */
71 static int obd_class_release(struct inode * inode, struct file * file)
72 {
73         ENTRY;
74
75         if (file->private_data)
76                 file->private_data = NULL;
77
78         MOD_DEC_USE_COUNT;
79         RETURN(0);
80 }
81
82
83 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
84 {
85         conn->addr = data->ioc_addr;
86         conn->cookie = data->ioc_cookie;
87 }
88
89
90 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
91 {
92         data->ioc_addr = conn->addr;
93         data->ioc_cookie = conn->cookie;
94 }
95
96
97 /* to control /dev/obd */
98 static int obd_class_ioctl (struct inode * inode, struct file * filp,
99                             unsigned int cmd, unsigned long arg)
100 {
101         char *buf = NULL;
102         int len = 0;
103         struct obd_ioctl_data *data;
104         struct obd_device *obd = filp->private_data;
105         
106         struct lustre_handle conn;
107         int rw = OBD_BRW_READ;
108         int err = 0;
109         int serialised = 0;
110         ENTRY;
111
112         switch (cmd)
113         {
114         case OBD_IOC_BRW_WRITE:
115         case OBD_IOC_BRW_READ:
116         case OBD_IOC_GETATTR:
117                 break;
118         default:
119                 down(&obd_conf_sem);
120                 serialised = 1;
121                 break;
122         }
123         
124         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
125             cmd != OBD_IOC_LIST &&
126             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
127                 CERROR("OBD ioctl: No device\n");
128                 GOTO(out, err=-EINVAL);
129         }
130         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
131                 CERROR("OBD ioctl: data error\n");
132                 GOTO(out, err=-EINVAL);
133         }
134         data = (struct obd_ioctl_data *)buf;
135
136         switch (cmd) {
137         case TCGETS:
138                 GOTO(out, err=-EINVAL);
139         case OBD_IOC_DEVICE: {
140                 CDEBUG(D_IOCTL, "\n");
141                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
142                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
143                         GOTO(out, err=-EINVAL);
144                 }
145                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
146
147                 filp->private_data = &obd_dev[data->ioc_dev];
148                 GOTO(out, err=0);
149         }
150
151         case OBD_IOC_LIST: {
152                 int i;
153                 char *buf2 = data->ioc_bulk;
154                 int remains = data->ioc_inllen1;
155
156                 if (!data->ioc_inlbuf1) {
157                         CERROR("No buffer passed!\n");
158                         GOTO(out, err=-EINVAL);
159                 }
160
161
162                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
163                         int l;
164                         char *status;
165                         struct obd_device *obd = &obd_dev[i];
166                         if (!obd->obd_type)
167                                 continue;
168                         if (obd->obd_flags & OBD_SET_UP)
169                                 status = "*";
170                         else
171                                 status = " ";
172                         l = snprintf(buf2, remains, "%2d %s %s %s %s\n",
173                                      i, status, obd->obd_type->typ_name,
174                                      obd->obd_name, obd->obd_uuid);
175                         buf2 +=l;
176                         remains -=l;
177                         if (remains <= 0) {
178                                 CERROR("not enough space for device listing\n");
179                                 break;
180                         }
181                 }
182
183                 err = copy_to_user((int *)arg, data, len);
184                 GOTO(out, err);
185         }
186
187
188         case OBD_IOC_NAME2DEV: {
189                 /* Resolve a device name.  This does not change the
190                  * currently selected device.
191                  */
192                 int dev;
193
194                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
195                         CERROR("No name passed,!\n");
196                         GOTO(out, err=-EINVAL);
197                 }
198                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
199                         CERROR("Name not nul terminated!\n");
200                         GOTO(out, err=-EINVAL);
201                 }
202
203                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
204                 dev = class_name2dev(data->ioc_inlbuf1);
205                 data->ioc_dev = dev;
206                 if (dev == -1) {
207                         CDEBUG(D_IOCTL, "No device for name %s!\n",
208                                data->ioc_inlbuf1);
209                         GOTO(out, err=-EINVAL);
210                 }
211
212                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
213                        dev);
214                 err = copy_to_user((int *)arg, data, sizeof(*data));
215                 GOTO(out, err);
216         }
217
218         case OBD_IOC_UUID2DEV: {
219                 /* Resolve a device uuid.  This does not change the
220                  * currently selected device.
221                  */
222                 int dev;
223
224                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
225                         CERROR("No UUID passed!\n");
226                         GOTO(out, err=-EINVAL);
227                 }
228                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
229                         CERROR("Name not nul terminated!\n");
230                         GOTO(out, err=-EINVAL);
231                 }
232
233                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
234                 dev = class_uuid2dev(data->ioc_inlbuf1);
235                 data->ioc_dev = dev;
236                 if (dev == -1) {
237                         CDEBUG(D_IOCTL, "No device for name %s!\n",
238                                data->ioc_inlbuf1);
239                         GOTO(out, err=-EINVAL);
240                 }
241
242                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
243                        dev);
244                 err = copy_to_user((int *)arg, data, sizeof(*data));
245                 GOTO(out, err);
246         }
247
248         case OBD_IOC_NEWDEV: {
249                 int dev = -1;
250                 int i;
251
252                 filp->private_data = NULL;
253                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
254                         struct obd_device *obd = &obd_dev[i];
255                         if (!obd->obd_type) {
256                                 filp->private_data = obd;
257                                 dev = i;
258                                 break;
259                         }
260                 }
261
262
263                 data->ioc_dev = dev;
264                 if (dev == -1)
265                         GOTO(out, err=-EINVAL);
266
267                 err = copy_to_user((int *)arg, data, sizeof(*data));
268                 GOTO(out, err);
269         }
270
271         case OBD_IOC_ATTACH: {
272                 struct obd_type *type;
273
274                 /* have we attached a type to this device */
275                 if (obd->obd_flags & OBD_ATTACHED) {
276                         CERROR("OBD: Device %d already typed as %s.\n",
277                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
278                         GOTO(out, err=-EBUSY);
279                 }
280
281                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
282                         CERROR("No type passed!\n");
283                         GOTO(out, err=-EINVAL);
284                 }
285                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
286                         CERROR("Type not nul terminated!\n");
287                         GOTO(out, err=-EINVAL);
288                 }
289
290                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
291                        MKSTR(data->ioc_inlbuf1),
292                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
293
294                 /* find the type */
295                 type = class_nm_to_type(data->ioc_inlbuf1);
296                 if (!type) {
297                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
298                         GOTO(out, err=-EINVAL);
299                 }
300
301                 obd->obd_type = type;
302                 INIT_LIST_HEAD(&obd->obd_exports);
303
304                 /* do the attach */
305                 if (OBP(obd, attach))
306                         err = OBP(obd,attach)(obd, sizeof(*data), data);
307                 if (err) {
308                         obd->obd_type = NULL;
309                 } else {
310                         obd->obd_flags |= OBD_ATTACHED;
311                         type->typ_refcnt++;
312                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
313                                obd->obd_minor, data->ioc_inlbuf1);
314                         if (data->ioc_inlbuf2) {
315                                 int len = strlen(data->ioc_inlbuf2) + 1;
316                                 OBD_ALLOC(obd->obd_name, len);
317                                 if (!obd->obd_name) {
318                                         CERROR("no memory\n");
319                                         LBUG();
320                                 }
321                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
322                                 obd->obd_proc_entry =
323                                         proc_lustre_register_obd_device(obd);
324                         } else {
325                                 CERROR("WARNING: unnamed obd device\n");
326                                 obd->obd_proc_entry = NULL;
327                         }
328
329                         if (data->ioc_inlbuf3) {
330                                 int len = strlen(data->ioc_inlbuf3);
331                                 if (len > 37) {
332                                         CERROR("uuid should be shorter than 37 bytes\n");
333                                         if (obd->obd_name)
334                                                 OBD_FREE(obd->obd_name,
335                                                          strlen(obd->obd_name) + 1);
336                                         GOTO(out, err=-EINVAL);
337                                 }
338                                 memcpy(obd->obd_uuid, data->ioc_inlbuf3,
339                                        sizeof(obd->obd_uuid));
340                         }
341
342                         MOD_INC_USE_COUNT;
343                 }
344
345                 GOTO(out, err);
346         }
347
348         case OBD_IOC_DETACH: {
349                 ENTRY;
350                 if (obd->obd_flags & OBD_SET_UP) {
351                         CERROR("OBD device %d still set up\n", obd->obd_minor);
352                         GOTO(out, err=-EBUSY);
353                 }
354                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
355                         CERROR("OBD device %d not attached\n", obd->obd_minor);
356                         GOTO(out, err=-ENODEV);
357                 }
358                 if ( !list_empty(&obd->obd_exports) ) {
359                         CERROR("OBD device %d has exports\n",
360                                obd->obd_minor);
361                         GOTO(out, err=-EBUSY);
362                 }
363
364                 if (obd->obd_name) {
365                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
366                         obd->obd_name = NULL;
367                 }
368
369                 if (obd->obd_proc_entry)
370                         proc_lustre_release_obd_device(obd);
371
372                 obd->obd_flags &= ~OBD_ATTACHED;
373                 obd->obd_type->typ_refcnt--;
374                 obd->obd_type = NULL;
375                 MOD_DEC_USE_COUNT;
376                 GOTO(out, err=0);
377         }
378
379         case OBD_IOC_SETUP: {
380                 /* have we attached a type to this device? */
381                 if (!(obd->obd_flags & OBD_ATTACHED)) {
382                         CERROR("Device %d not attached\n", obd->obd_minor);
383                         GOTO(out, err=-ENODEV);
384                 }
385
386                 /* has this been done already? */
387                 if ( obd->obd_flags & OBD_SET_UP ) {
388                         CERROR("Device %d already setup (type %s)\n",
389                                obd->obd_minor, obd->obd_type->typ_name);
390                         GOTO(out, err=-EBUSY);
391                 }
392
393                 if ( OBT(obd) && OBP(obd, setup) )
394                         err = obd_setup(obd, sizeof(*data), data);
395
396                 if (!err) {
397                         obd->obd_type->typ_refcnt++;
398                         obd->obd_flags |= OBD_SET_UP;
399                 }
400
401                 GOTO(out, err);
402         }
403         case OBD_IOC_CLEANUP: {
404                 /* have we attached a type to this device? */
405                 if (!(obd->obd_flags & OBD_ATTACHED)) {
406                         CERROR("Device %d not attached\n", obd->obd_minor);
407                         GOTO(out, err=-ENODEV);
408                 }
409
410                 if ( OBT(obd) && OBP(obd, cleanup) )
411                         err = obd_cleanup(obd);
412
413                 if (!err) {
414                         obd->obd_flags &= ~OBD_SET_UP;
415                         obd->obd_type->typ_refcnt--;
416                 }
417                 GOTO(out, err);
418         }
419
420         case OBD_IOC_CONNECT: {
421                 char * cluuid = "OBD_CLASS_UUID";
422                 obd_data2conn(&conn, data);
423
424                 err = obd_connect(&conn, obd, cluuid);
425
426                 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
427                 obd_conn2data(data, &conn);
428                 if (err)
429                         GOTO(out, err);
430
431                 err = copy_to_user((int *)arg, data, sizeof(*data));
432                 GOTO(out, err);
433         }
434
435         case OBD_IOC_DISCONNECT: {
436                 obd_data2conn(&conn, data);
437                 err = obd_disconnect(&conn);
438                 GOTO(out, err);
439         }
440
441         case OBD_IOC_DEC_USE_COUNT: {
442                 MOD_DEC_USE_COUNT;
443                 GOTO(out, err=0);
444         }
445
446         case OBD_IOC_CREATE: {
447                 struct lov_stripe_md *ea;
448                 obd_data2conn(&conn, data);
449
450
451                 err = obd_create(&conn, &data->ioc_obdo1, &ea);
452                 if (err)
453                         GOTO(out, err);
454
455                 err = copy_to_user((int *)arg, data, sizeof(*data));
456                 GOTO(out, err);
457         }
458
459         case OBD_IOC_GETATTR: {
460
461                 obd_data2conn(&conn, data);
462                 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
463                 if (err)
464                         GOTO(out, err);
465
466                 err = copy_to_user((int *)arg, data, sizeof(*data));
467                 GOTO(out, err);
468         }
469
470         case OBD_IOC_SETATTR: {
471                 obd_data2conn(&conn, data);
472                 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
473                 if (err)
474                         GOTO(out, err);
475
476                 err = copy_to_user((int *)arg, data, sizeof(*data));
477                 GOTO(out, err);
478         }
479
480         case OBD_IOC_DESTROY: {
481                 //void *ea;
482                 obd_data2conn(&conn, data);
483
484                 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
485                 if (err)
486                         GOTO(out, err);
487
488                 err = copy_to_user((int *)arg, data, sizeof(*data));
489                 GOTO(out, err);
490         }
491
492         case OBD_IOC_BRW_WRITE:
493                 rw = OBD_BRW_WRITE;
494         case OBD_IOC_BRW_READ: {
495                 struct lov_stripe_md smd;
496                 struct io_cb_data *cbd = ll_init_cb();
497                 obd_count       pages = 0;
498                 struct brw_page *pga, *pgp;
499                 int             j;
500                 unsigned long off;
501                 __u64 id;
502
503                 if (!cbd)
504                         GOTO(out, err = -ENOMEM);
505
506                 obd_data2conn(&conn, data);
507
508                 pages = data->ioc_count / PAGE_SIZE;
509
510                 CDEBUG(D_INODE, "BRW %s with %d pages\n",
511                        rw == OBD_BRW_READ ? "read" : "write", pages);
512                 OBD_ALLOC(pga, pages * sizeof(*pga));
513                 if (!pga) {
514                         CERROR("no memory for %d BRW per-page data\n", pages);
515                         GOTO(brw_free, err = -ENOMEM);
516                 }
517
518                 memset(&smd, 0, sizeof(smd));
519                 id = smd.lmd_object_id = data->ioc_obdo1.o_id;
520
521                 off = data->ioc_offset;
522
523                 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
524                         pgp->pg = alloc_pages(obdclass_highmem ? GFP_HIGHUSER : GFP_KERNEL, 0);
525                         if (!pgp->pg) {
526                                 CERROR("no memory for brw pages\n");
527                                 GOTO(brw_cleanup, err = -ENOMEM);
528                         }
529                         pgp->count = PAGE_SIZE;
530                         pgp->off = off;
531                         pgp->flag = 0;
532
533                         if (rw == OBD_BRW_WRITE) {
534                                 page_debug_setup(kmap(pgp->pg), pgp->count,
535                                                  pgp->off, id);
536                                 kunmap(pgp->pg);
537                         }
538                 }
539
540                 err = obd_brw(rw, &conn, &smd, j, pga, ll_sync_io_cb, cbd);
541                 EXIT;
542         brw_cleanup:
543                 for (j = 0, pgp = pga; j < pages; j++, pgp++) {
544                         if (pgp->pg != NULL) {
545                                 int err2;
546                                 void *addr = kmap(pgp->pg);
547
548                                 err2 = page_debug_check("test_brw", addr,
549                                                         PAGE_SIZE, pgp->off,id);
550                                 kunmap(pgp->pg);
551                                 __free_pages(pgp->pg, 0);
552                                 if (!err)
553                                         err = err2;
554                         }
555                 }
556         brw_free:
557                 OBD_FREE(pga, pages * sizeof(*pga));
558                 GOTO(out, err);
559         }
560         default:
561                 obd_data2conn(&conn, data);
562
563                 err = obd_iocontrol(cmd, &conn, len, data, NULL);
564                 if (err)
565                         GOTO(out, err);
566
567                 err = copy_to_user((int *)arg, data, len);
568                 GOTO(out, err);
569         }
570
571  out:
572         if (buf)
573                 OBD_FREE(buf, len);
574         if (serialised)
575                 up(&obd_conf_sem);
576         RETURN(err);
577 } /* obd_class_ioctl */
578
579
580
581 /* declare character device */
582 static struct file_operations obd_psdev_fops = {
583         ioctl: obd_class_ioctl,       /* ioctl */
584         open: obd_class_open,        /* open */
585         release: obd_class_release,     /* release */
586 };
587
588 /* modules setup */
589 #define OBD_MINOR 241
590 static struct miscdevice obd_psdev = {
591         OBD_MINOR,
592         "obd_psdev",
593         &obd_psdev_fops
594 };
595
596 void (*class_signal_client_failure)(struct ptlrpc_client *);
597
598 EXPORT_SYMBOL(obd_dev);
599 EXPORT_SYMBOL(obdo_cachep);
600 EXPORT_SYMBOL(obd_memory);
601 EXPORT_SYMBOL(obd_fail_loc);
602 EXPORT_SYMBOL(obd_timeout);
603 EXPORT_SYMBOL(obd_recovery_upcall);
604
605 EXPORT_SYMBOL(class_register_type);
606 EXPORT_SYMBOL(class_unregister_type);
607 EXPORT_SYMBOL(class_name2dev);
608 EXPORT_SYMBOL(class_uuid2dev);
609 EXPORT_SYMBOL(class_uuid2obd);
610 EXPORT_SYMBOL(class_new_export);
611 EXPORT_SYMBOL(class_connect);
612 EXPORT_SYMBOL(class_conn2export);
613 EXPORT_SYMBOL(class_rconn2export);
614 EXPORT_SYMBOL(class_conn2obd);
615 EXPORT_SYMBOL(class_disconnect);
616 EXPORT_SYMBOL(class_disconnect_all);
617 EXPORT_SYMBOL(class_uuid_parse);
618 EXPORT_SYMBOL(class_uuid_unparse);
619 //EXPORT_SYMBOL(class_multi_setup);
620 //EXPORT_SYMBOL(class_multi_cleanup);
621
622 EXPORT_SYMBOL(class_signal_client_failure);
623
624 static int __init init_obdclass(void)
625 {
626         int err;
627         int i;
628
629         printk(KERN_INFO "OBD class driver  v0.9, info@clusterfs.com\n");
630
631         sema_init(&obd_conf_sem, 1);
632         INIT_LIST_HEAD(&obd_types);
633
634         if ((err = misc_register(&obd_psdev))) {
635                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
636                 return err;
637         }
638
639         for (i = 0; i < MAX_OBD_DEVICES; i++) {
640                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
641                 obd_dev[i].obd_minor = i;
642                 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
643         }
644
645         err = obd_init_caches();
646         if (err)
647                 return err;
648         obd_sysctl_init();
649         return 0;
650 }
651
652 static void __exit cleanup_obdclass(void)
653 {
654         int i;
655         ENTRY;
656
657         misc_deregister(&obd_psdev);
658         for (i = 0; i < MAX_OBD_DEVICES; i++) {
659                 struct obd_device *obd = &obd_dev[i];
660                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
661                     OBT(obd) && OBP(obd, detach)) {
662                         /* XXX should this call generic detach otherwise? */
663                         OBP(obd, detach)(obd);
664                 }
665         }
666
667         obd_cleanup_caches();
668         obd_sysctl_clean();
669         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
670         EXIT;
671 }
672
673 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
674 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
675 MODULE_LICENSE("GPL");
676
677 module_init(init_obdclass);
678 module_exit(cleanup_obdclass);