Whamcloud - gitweb
6200acdfcaa03260031377e3b257e054310d323c
[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  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * These are the only exported functions, they provide some generic
24  * infrastructure for managing object devices
25  */
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28 #define EXPORT_SYMTAB
29 #ifdef __KERNEL__
30 #include <linux/config.h> /* for CONFIG_PROC_FS */
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/major.h>
35 #include <linux/sched.h>
36 #include <linux/lp.h>
37 #include <linux/slab.h>
38 #include <linux/ioport.h>
39 #include <linux/fcntl.h>
40 #include <linux/delay.h>
41 #include <linux/skbuff.h>
42 #include <linux/proc_fs.h>
43 #include <linux/fs.h>
44 #include <linux/poll.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/highmem.h>
48 #include <asm/io.h>
49 #include <asm/ioctls.h>
50 #include <asm/system.h>
51 #include <asm/poll.h>
52 #include <asm/uaccess.h>
53 #include <linux/miscdevice.h>
54 #else
55
56 # include <liblustre.h>
57
58 #endif
59
60 #include <linux/obd_support.h>
61 #include <linux/obd_class.h>
62 #include <linux/lustre_debug.h>
63 #include <linux/smp_lock.h>
64 #include <linux/lprocfs_status.h>
65 #include <portals/lib-types.h> /* for PTL_MD_MAX_IOV */
66 #include <linux/lustre_build_version.h>
67
68 struct semaphore obd_conf_sem;   /* serialize configuration commands */
69 struct obd_device obd_dev[MAX_OBD_DEVICES];
70 struct list_head obd_types;
71 atomic_t obd_memory;
72 int obd_memmax;
73
74 /* Root for /proc/lustre */
75 struct proc_dir_entry *proc_lustre_root = NULL;
76
77 /* The following are visible and mutable through /proc/sys/lustre/. */
78 unsigned long obd_fail_loc;
79 unsigned long obd_timeout = 100;
80 char obd_recovery_upcall[128] = "/usr/lib/lustre/ha_assist";
81 unsigned long obd_sync_filter; /* = 0, don't sync by default */
82
83 /*  opening /dev/obd */
84 static int obd_class_open(struct inode * inode, struct file * file)
85 {
86         struct obd_class_user_state *ocus;
87         ENTRY;
88
89         OBD_ALLOC (ocus, sizeof (*ocus));
90         if (ocus == NULL)
91                 return (-ENOMEM);
92
93         INIT_LIST_HEAD (&ocus->ocus_conns);
94         ocus->ocus_current_obd = NULL;
95         file->private_data = ocus;
96
97         MOD_INC_USE_COUNT;
98         RETURN(0);
99 }
100
101 static int
102 obd_class_add_user_conn (struct obd_class_user_state *ocus,
103                          struct lustre_handle *conn)
104 {
105         struct obd_class_user_conn *c;
106
107         /* NB holding obd_conf_sem */
108
109         OBD_ALLOC (c, sizeof (*c));
110         if (ocus == NULL)
111                 return (-ENOMEM);
112
113         c->ocuc_conn = *conn;
114         list_add (&c->ocuc_chain, &ocus->ocus_conns);
115         return (0);
116 }
117
118 static void
119 obd_class_remove_user_conn (struct obd_class_user_state *ocus,
120                             struct lustre_handle *conn)
121 {
122         struct list_head *e;
123         struct obd_class_user_conn *c;
124
125         /* NB holding obd_conf_sem or last reference */
126
127         list_for_each (e, &ocus->ocus_conns) {
128                 c = list_entry (e, struct obd_class_user_conn, ocuc_chain);
129                 if (!memcmp (conn, &c->ocuc_conn, sizeof (*conn))) {
130                         list_del (&c->ocuc_chain);
131                         OBD_FREE (c, sizeof (*c));
132                         return;
133                 }
134         }
135 }
136
137 /*  closing /dev/obd */
138 static int obd_class_release(struct inode * inode, struct file * file)
139 {
140         struct obd_class_user_state *ocus = file->private_data;
141         struct obd_class_user_conn  *c;
142         ENTRY;
143
144         while (!list_empty (&ocus->ocus_conns)) {
145                 c = list_entry (ocus->ocus_conns.next,
146                                 struct obd_class_user_conn, ocuc_chain);
147                 list_del (&c->ocuc_chain);
148
149                 CDEBUG (D_IOCTL, "Auto-disconnect %p\n", &c->ocuc_conn);
150
151                 down (&obd_conf_sem);
152                 obd_disconnect (&c->ocuc_conn);
153                 up (&obd_conf_sem);
154
155                 OBD_FREE (c, sizeof (*c));
156         }
157
158         OBD_FREE (ocus, sizeof (*ocus));
159
160         MOD_DEC_USE_COUNT;
161         RETURN(0);
162 }
163
164 static inline void obd_data2conn(struct lustre_handle *conn,
165                                  struct obd_ioctl_data *data)
166 {
167         conn->addr = data->ioc_addr;
168         conn->cookie = data->ioc_cookie;
169 }
170
171 static inline void obd_conn2data(struct obd_ioctl_data *data,
172                                  struct lustre_handle *conn)
173 {
174         data->ioc_addr = conn->addr;
175         data->ioc_cookie = conn->cookie;
176 }
177
178 static void forcibly_detach_exports(struct obd_device *obd)
179 {
180         int rc;
181         struct list_head *tmp, *n;
182         struct lustre_handle fake_conn;
183
184         CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
185                "disconnecting them", obd->obd_minor, obd);
186         list_for_each_safe(tmp, n, &obd->obd_exports) {
187                 struct obd_export *exp = list_entry(tmp, struct obd_export,
188                                                     exp_obd_chain);
189                 fake_conn.addr = (__u64)(unsigned long)exp;
190                 fake_conn.cookie = exp->exp_cookie;
191                 rc = obd_disconnect(&fake_conn);
192                 if (rc) {
193                         CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
194                                exp, rc);
195                 } else {
196                         CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
197                 }
198         }
199 }
200
201
202 int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd,
203                        unsigned long arg)
204 {
205         char *buf = NULL;
206         struct obd_ioctl_data *data;
207         struct obd_device *obd = ocus->ocus_current_obd;
208         struct lustre_handle conn;
209         int err = 0, len = 0, serialised = 0;
210         ENTRY;
211
212         switch (cmd) {
213         case OBD_IOC_BRW_WRITE:
214         case OBD_IOC_BRW_READ:
215         case OBD_IOC_GETATTR:
216         case ECHO_IOC_ENQUEUE:
217         case ECHO_IOC_CANCEL:
218                 break;
219         default:
220                 down(&obd_conf_sem);
221                 serialised = 1;
222                 break;
223         }
224
225         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
226             cmd != OBD_IOC_LIST && cmd != OBD_GET_VERSION &&
227             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV &&
228             cmd != OBD_IOC_ADD_UUID && cmd != OBD_IOC_DEL_UUID  &&
229             cmd != OBD_IOC_CLOSE_UUID) {
230                 CERROR("OBD ioctl: No device\n");
231                 GOTO(out, err = -EINVAL);
232         }
233         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
234                 CERROR("OBD ioctl: data error\n");
235                 GOTO(out, err = -EINVAL);
236         }
237         data = (struct obd_ioctl_data *)buf;
238
239         switch (cmd) {
240         case TCGETS:
241                 GOTO(out, err=-EINVAL);
242         case OBD_IOC_DEVICE: {
243                 CDEBUG(D_IOCTL, "\n");
244                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
245                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
246                         GOTO(out, err=-EINVAL);
247                 }
248                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
249
250                 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
251                 GOTO(out, err=0);
252         }
253
254         case OBD_IOC_LIST: {
255                 int i;
256                 char *buf2 = data->ioc_bulk;
257                 int remains = data->ioc_inllen1;
258
259                 if (!data->ioc_inlbuf1) {
260                         CERROR("No buffer passed!\n");
261                         GOTO(out, err=-EINVAL);
262                 }
263
264
265                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
266                         int l;
267                         char *status;
268                         struct obd_device *obd = &obd_dev[i];
269                         if (!obd->obd_type)
270                                 continue;
271                         if (obd->obd_flags & OBD_SET_UP)
272                                 status = "UP";
273                         else if (obd->obd_flags & OBD_ATTACHED)
274                                 status = "AT";
275                         else
276                                 status = "-";
277                         l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
278                                      i, status, obd->obd_type->typ_name,
279                                      obd->obd_name, obd->obd_uuid.uuid,
280                                      obd->obd_type->typ_refcnt);
281                         buf2 +=l;
282                         remains -=l;
283                         if (remains <= 0) {
284                                 CERROR("not enough space for device listing\n");
285                                 break;
286                         }
287                 }
288
289                 err = copy_to_user((void *)arg, data, len);
290                 if (err)
291                         err = -EFAULT;
292                 GOTO(out, err);
293         }
294
295         case OBD_GET_VERSION:
296                 if (!data->ioc_inlbuf1) {
297                         CERROR("No buffer passed in ioctl\n");
298                         GOTO(out, err = -EINVAL);
299                 }
300
301                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
302                         CERROR("ioctl buffer too small to hold version\n");
303                         GOTO(out, err = -EINVAL);
304                 }
305
306                 memcpy(data->ioc_bulk, BUILD_VERSION,
307                        strlen(BUILD_VERSION) + 1);
308
309                 err = copy_to_user((void *)arg, data, len);
310                 if (err)
311                         err = -EFAULT;
312                 GOTO(out, err);
313
314         case OBD_IOC_NAME2DEV: {
315                 /* Resolve a device name.  This does not change the
316                  * currently selected device.
317                  */
318                 int dev;
319
320                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
321                         CERROR("No name passed,!\n");
322                         GOTO(out, err=-EINVAL);
323                 }
324                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
325                         CERROR("Name not nul terminated!\n");
326                         GOTO(out, err=-EINVAL);
327                 }
328
329                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
330                 dev = class_name2dev(data->ioc_inlbuf1);
331                 data->ioc_dev = dev;
332                 if (dev == -1) {
333                         CDEBUG(D_IOCTL, "No device for name %s!\n",
334                                data->ioc_inlbuf1);
335                         GOTO(out, err=-EINVAL);
336                 }
337
338                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
339                        dev);
340                 err = copy_to_user((void *)arg, data, sizeof(*data));
341                 if (err)
342                         err = -EFAULT;
343                 GOTO(out, err);
344         }
345
346         case OBD_IOC_UUID2DEV: {
347                 /* Resolve a device uuid.  This does not change the
348                  * currently selected device.
349                  */
350                 int dev;
351                 struct obd_uuid uuid;
352
353                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
354                         CERROR("No UUID passed!\n");
355                         GOTO(out, err=-EINVAL);
356                 }
357                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
358                         CERROR("Name not nul terminated!\n");
359                         GOTO(out, err=-EINVAL);
360                 }
361
362                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
363                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
364                 dev = class_uuid2dev(&uuid);
365                 data->ioc_dev = dev;
366                 if (dev == -1) {
367                         CDEBUG(D_IOCTL, "No device for name %s!\n",
368                                data->ioc_inlbuf1);
369                         GOTO(out, err=-EINVAL);
370                 }
371
372                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
373                        dev);
374                 err = copy_to_user((void *)arg, data, sizeof(*data));
375                 if (err)
376                         err = -EFAULT;
377                 GOTO(out, err);
378         }
379
380         case OBD_IOC_NEWDEV: {
381                 int dev = -1;
382                 int i;
383
384                 ocus->ocus_current_obd = NULL;
385                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
386                         struct obd_device *obd = &obd_dev[i];
387                         if (!obd->obd_type) {
388                                 ocus->ocus_current_obd = obd;
389                                 dev = i;
390                                 break;
391                         }
392                 }
393
394
395                 data->ioc_dev = dev;
396                 if (dev == -1)
397                         GOTO(out, err=-EINVAL);
398
399                 err = copy_to_user((void *)arg, data, sizeof(*data));
400                 if (err)
401                         err = -EFAULT;
402                 GOTO(out, err);
403         }
404
405         case OBD_IOC_ATTACH: {
406                 struct obd_type *type;
407                 int minor, len;
408
409                 /* have we attached a type to this device */
410                 if (obd->obd_flags & OBD_ATTACHED || obd->obd_type) {
411                         CERROR("OBD: Device %d already typed as %s.\n",
412                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
413                         GOTO(out, err = -EBUSY);
414                 }
415
416                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
417                         CERROR("No type passed!\n");
418                         GOTO(out, err = -EINVAL);
419                 }
420                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
421                         CERROR("Type not nul terminated!\n");
422                         GOTO(out, err = -EINVAL);
423                 }
424                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
425                         CERROR("No name passed!\n");
426                         GOTO(out, err = -EINVAL);
427                 }
428                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
429                        MKSTR(data->ioc_inlbuf1),
430                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
431
432                 /* find the type */
433                 type = class_get_type(data->ioc_inlbuf1);
434                 if (!type) {
435                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
436                         GOTO(out, err = -EINVAL);
437                 }
438
439                 minor = obd->obd_minor;
440                 memset(obd, 0, sizeof(*obd));
441                 obd->obd_minor = minor;
442                 obd->obd_type = type;
443                 INIT_LIST_HEAD(&obd->obd_exports);
444                 INIT_LIST_HEAD(&obd->obd_imports);
445                 spin_lock_init(&obd->obd_dev_lock);
446
447                 /* XXX belong ins setup not attach  */
448                 /* recovery data */
449                 spin_lock_init(&obd->obd_processing_task_lock);
450                 init_waitqueue_head(&obd->obd_next_transno_waitq);
451                 INIT_LIST_HEAD(&obd->obd_recovery_queue);
452                 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
453
454                 len = strlen(data->ioc_inlbuf2) + 1;
455                 OBD_ALLOC(obd->obd_name, len);
456                 if (!obd->obd_name) {
457                         class_put_type(obd->obd_type);
458                         obd->obd_type = NULL;
459                         GOTO(out, err = -ENOMEM);
460                 }
461                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
462
463                 if (data->ioc_inlbuf3) {
464                         int len = strlen(data->ioc_inlbuf3);
465                         if (len >= sizeof(obd->obd_uuid)) {
466                                 CERROR("uuid must be < "LPSZ" bytes long\n",
467                                        sizeof(obd->obd_uuid));
468                                 if (obd->obd_name)
469                                         OBD_FREE(obd->obd_name,
470                                                  strlen(obd->obd_name) + 1);
471                                 class_put_type(obd->obd_type);
472                                 obd->obd_type = NULL;
473                                 GOTO(out, err=-EINVAL);
474                         }
475                         memcpy(obd->obd_uuid.uuid, data->ioc_inlbuf3, len);
476                 }
477                 /* do the attach */
478                 if (OBP(obd, attach))
479                         err = OBP(obd,attach)(obd, sizeof(*data), data);
480                 if (err) {
481                         if(data->ioc_inlbuf2)
482                                 OBD_FREE(obd->obd_name,
483                                          strlen(obd->obd_name) + 1);
484                         class_put_type(obd->obd_type);
485                         obd->obd_type = NULL;
486                 } else {
487                         obd->obd_flags |= OBD_ATTACHED;
488
489                         type->typ_refcnt++;
490                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
491                                obd->obd_minor, data->ioc_inlbuf1);
492                 }
493
494                 GOTO(out, err);
495         }
496
497         case OBD_IOC_DETACH: {
498                 ENTRY;
499                 if (obd->obd_flags & OBD_SET_UP) {
500                         CERROR("OBD device %d still set up\n", obd->obd_minor);
501                         GOTO(out, err=-EBUSY);
502                 }
503                 if (!(obd->obd_flags & OBD_ATTACHED) ) {
504                         CERROR("OBD device %d not attached\n", obd->obd_minor);
505                         GOTO(out, err=-ENODEV);
506                 }
507                 if (OBP(obd, detach))
508                         err = OBP(obd,detach)(obd);
509
510                 if (obd->obd_name) {
511                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
512                         obd->obd_name = NULL;
513                 }
514
515                 obd->obd_flags &= ~OBD_ATTACHED;
516                 obd->obd_type->typ_refcnt--;
517                 class_put_type(obd->obd_type);
518                 obd->obd_type = NULL;
519                 GOTO(out, err = 0);
520         }
521
522         case OBD_IOC_SETUP: {
523                 /* have we attached a type to this device? */
524                 if (!(obd->obd_flags & OBD_ATTACHED)) {
525                         CERROR("Device %d not attached\n", obd->obd_minor);
526                         GOTO(out, err=-ENODEV);
527                 }
528
529                 /* has this been done already? */
530                 if ( obd->obd_flags & OBD_SET_UP ) {
531                         CERROR("Device %d already setup (type %s)\n",
532                                obd->obd_minor, obd->obd_type->typ_name);
533                         GOTO(out, err=-EBUSY);
534                 }
535
536                 if ( OBT(obd) && OBP(obd, setup) )
537                         err = obd_setup(obd, sizeof(*data), data);
538
539                 if (!err) {
540                         obd->obd_type->typ_refcnt++;
541                         obd->obd_flags |= OBD_SET_UP;
542                 }
543
544                 GOTO(out, err);
545         }
546         case OBD_IOC_CLEANUP: {
547                 /* have we attached a type to this device? */
548                 if (!(obd->obd_flags & OBD_ATTACHED)) {
549                         CERROR("Device %d not attached\n", obd->obd_minor);
550                         GOTO(out, err=-ENODEV);
551                 }
552                 if (!list_empty(&obd->obd_exports)) {
553                         if (!data->ioc_inlbuf1 || data->ioc_inlbuf1[0] != 'F') {
554                                 CERROR("OBD device %d (%p) has exports\n",
555                                        obd->obd_minor, obd);
556                                 GOTO(out, err = -EBUSY);
557                         }
558                         forcibly_detach_exports(obd);
559                 }
560                 if (OBT(obd) && OBP(obd, cleanup))
561                         err = obd_cleanup(obd);
562
563                 if (!err) {
564                         obd->obd_flags &= ~OBD_SET_UP;
565                         obd->obd_type->typ_refcnt--;
566                 }
567                 GOTO(out, err);
568         }
569
570         case OBD_IOC_CONNECT: {
571                 struct obd_uuid cluuid = { "OBD_CLASS_UUID" };
572                 obd_data2conn(&conn, data);
573
574                 err = obd_connect(&conn, obd, &cluuid, NULL, NULL);
575
576                 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.addr);
577                 obd_conn2data(data, &conn);
578                 if (err)
579                         GOTO(out, err);
580
581                 err = obd_class_add_user_conn (ocus, &conn);
582                 if (err != 0) {
583                         obd_disconnect (&conn);
584                         GOTO (out, err);
585                 }
586
587                 err = copy_to_user((void *)arg, data, sizeof(*data));
588                 if (err != 0) {
589                         obd_class_remove_user_conn (ocus, &conn);
590                         obd_disconnect (&conn);
591                         GOTO (out, err=-EFAULT);
592                 }
593                 GOTO(out, err);
594         }
595
596         case OBD_IOC_DISCONNECT: {
597                 obd_data2conn(&conn, data);
598                 obd_class_remove_user_conn (ocus, &conn);
599                 err = obd_disconnect(&conn);
600                 GOTO(out, err);
601         }
602
603         case OBD_IOC_NO_TRANSNO: {
604                 if (!(obd->obd_flags & OBD_ATTACHED)) {
605                         CERROR("Device %d not attached\n", obd->obd_minor);
606                         GOTO(out, err=-ENODEV);
607                 }
608                 CDEBUG(D_IOCTL,
609                        "disabling committed-transno notifications on %d\n",
610                        obd->obd_minor);
611                 obd->obd_flags |= OBD_NO_TRANSNO;
612                 GOTO(out, err = 0);
613         }
614
615         case OBD_IOC_CLOSE_UUID: {
616                 struct lustre_peer peer;
617                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
618                        data->ioc_inlbuf1);
619                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
620                 GOTO(out, err = 0);
621         }
622         case OBD_IOC_ADD_UUID: {
623                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
624                        ", nal %d\n", data->ioc_inlbuf1, data->ioc_nid,
625                        data->ioc_nal);
626
627                 err = class_add_uuid(data->ioc_inlbuf1, data->ioc_nid,
628                                      data->ioc_nal);
629                 GOTO(out, err);
630         }
631         case OBD_IOC_DEL_UUID: {
632                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
633                        data->ioc_inlbuf1 == NULL ? "<all uuids>" :
634                        data->ioc_inlbuf1);
635
636                 err = class_del_uuid(data->ioc_inlbuf1);
637                 GOTO(out, err);
638         }
639         default: { 
640                 // obd_data2conn(&conn, data);
641                 struct obd_class_user_conn *oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
642                 err = obd_iocontrol(cmd, &oconn->ocuc_conn, len, data, NULL);
643                 if (err)
644                         GOTO(out, err);
645
646                 err = copy_to_user((void *)arg, data, len);
647                 if (err)
648                         err = -EFAULT;
649                 GOTO(out, err);
650         }
651         }
652
653  out:
654         if (buf)
655                 OBD_FREE(buf, len);
656         if (serialised)
657                 up(&obd_conf_sem);
658         RETURN(err);
659 } /* obd_class_ioctl */
660
661
662
663 #define OBD_MINOR 241
664 #ifdef __KERNEL__
665 /* to control /dev/obd */
666 static int obd_class_ioctl (struct inode * inode, struct file * filp,
667                      unsigned int cmd, unsigned long arg)
668 {
669         return class_handle_ioctl(filp->private_data, cmd, arg);
670 }
671
672 /* declare character device */
673 static struct file_operations obd_psdev_fops = {
674         ioctl: obd_class_ioctl,      /* ioctl */
675         open: obd_class_open,        /* open */
676         release: obd_class_release,  /* release */
677 };
678
679 /* modules setup */
680 static struct miscdevice obd_psdev = {
681         OBD_MINOR,
682         "obd_psdev",
683         &obd_psdev_fops
684 };
685 #else
686 void *obd_psdev = NULL;
687 #endif
688
689 void (*class_signal_connection_failure)(struct ptlrpc_connection *);
690
691 #ifdef CONFIG_HIGHMEM
692 /* Allow at most 3/4 of the kmap mappings to be consumed by vector I/O
693  * requests.  This avoids deadlocks on servers which have a lot of clients
694  * doing vector I/O.  We don't need to do this for non-vector I/O requests
695  * because singleton requests will just block on the kmap itself and never
696  * deadlock waiting for additional kmaps to complete.
697  *
698  * If we are a "server" task, we can have at most a single reservation
699  * in excess of the maximum.  This avoids a deadlock when multiple client
700  * threads are on the same machine as the server threads, and the clients
701  * have consumed all of the available mappings.  As long as a single server
702  * thread is can make progress, we are guaranteed to avoid deadlock.
703  */
704 #define OBD_KMAP_MAX (LAST_PKMAP * 3 / 4)
705 static atomic_t obd_kmap_count = ATOMIC_INIT(OBD_KMAP_MAX);
706 static DECLARE_WAIT_QUEUE_HEAD(obd_kmap_waitq);
707
708 void obd_kmap_get(int count, int server)
709 {
710         //CERROR("getting %d kmap counts (%d/%d)\n", count,
711         //       atomic_read(&obd_kmap_count), OBD_KMAP_MAX);
712         if (count == 1)
713                 atomic_dec(&obd_kmap_count);
714         else while (atomic_add_negative(-count, &obd_kmap_count)) {
715                 static long next_show = 0;
716                 static int skipped = 0;
717
718                 if (server && atomic_read(&obd_kmap_count) >= -PTL_MD_MAX_IOV)
719                         break;
720
721                 CDEBUG(D_OTHER, "negative kmap reserved count: %d\n",
722                        atomic_read(&obd_kmap_count));
723                 atomic_add(count, &obd_kmap_count);
724
725                 if (time_after(jiffies, next_show)) {
726                         CERROR("blocking %s (and %d others) for kmaps\n",
727                                current->comm, skipped);
728                         next_show = jiffies + 5*HZ;
729                         skipped = 0;
730                 } else
731                         skipped++;
732                 wait_event(obd_kmap_waitq,
733                            atomic_read(&obd_kmap_count) >= count);
734         }
735 }
736
737 void obd_kmap_put(int count)
738 {
739         atomic_add(count, &obd_kmap_count);
740         /* Wake up sleepers.  Sadly, this wakes up all of the tasks at once.
741          * We could have something smarter here like:
742         while (atomic_read(&obd_kmap_count) > 0)
743                 wake_up_nr(obd_kmap_waitq, 1);
744         although we would need to set somewhere (probably obd_class_init):
745         obd_kmap_waitq.flags |= WQ_FLAG_EXCLUSIVE;
746         For now the wait_event() condition will handle this OK I believe.
747          */
748         if (atomic_read(&obd_kmap_count) > 0)
749                 wake_up(&obd_kmap_waitq);
750 }
751
752 EXPORT_SYMBOL(obd_kmap_get);
753 EXPORT_SYMBOL(obd_kmap_put);
754 #endif
755
756 EXPORT_SYMBOL(obd_dev);
757 EXPORT_SYMBOL(obdo_cachep);
758 EXPORT_SYMBOL(obd_memory);
759 EXPORT_SYMBOL(obd_memmax);
760 EXPORT_SYMBOL(obd_fail_loc);
761 EXPORT_SYMBOL(obd_timeout);
762 EXPORT_SYMBOL(obd_recovery_upcall);
763 EXPORT_SYMBOL(obd_sync_filter);
764 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
765 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
766 EXPORT_SYMBOL(proc_lustre_root);
767
768 EXPORT_SYMBOL(class_register_type);
769 EXPORT_SYMBOL(class_unregister_type);
770 EXPORT_SYMBOL(class_get_type);
771 EXPORT_SYMBOL(class_put_type);
772 EXPORT_SYMBOL(class_name2dev);
773 EXPORT_SYMBOL(class_uuid2dev);
774 EXPORT_SYMBOL(class_uuid2obd);
775 EXPORT_SYMBOL(class_new_export);
776 EXPORT_SYMBOL(class_destroy_export);
777 EXPORT_SYMBOL(class_connect);
778 EXPORT_SYMBOL(class_conn2export);
779 EXPORT_SYMBOL(class_conn2obd);
780 EXPORT_SYMBOL(class_conn2cliimp);
781 EXPORT_SYMBOL(class_conn2ldlmimp);
782 EXPORT_SYMBOL(class_disconnect);
783 EXPORT_SYMBOL(class_disconnect_all);
784 EXPORT_SYMBOL(class_uuid_unparse);
785 EXPORT_SYMBOL(lustre_uuid_to_peer);
786
787 EXPORT_SYMBOL(class_signal_connection_failure);
788
789 EXPORT_SYMBOL(class_handle_hash);
790 EXPORT_SYMBOL(class_handle_unhash);
791 EXPORT_SYMBOL(class_handle2object);
792
793 #ifdef __KERNEL__
794 static int __init init_obdclass(void)
795 #else
796 int init_obdclass(void)
797 #endif
798 {
799         struct obd_device *obd;
800         int err;
801         int i;
802
803         printk(KERN_INFO "OBD class driver Build Version: " BUILD_VERSION
804                       ", info@clusterfs.com\n");
805
806         class_init_uuidlist();
807         class_handle_init();
808
809         sema_init(&obd_conf_sem, 1);
810         INIT_LIST_HEAD(&obd_types);
811
812         if ((err = misc_register(&obd_psdev))) {
813                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
814                 return err;
815         }
816
817         /* This struct is already zerod for us (static global) */
818         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
819                 obd->obd_minor = i;
820
821         err = obd_init_caches();
822         if (err)
823                 return err;
824
825 #ifdef __KERNEL__
826         obd_sysctl_init();
827 #endif
828
829 #ifdef LPROCFS
830         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
831         if (!proc_lustre_root)
832                 printk(KERN_ERR "error registering /proc/fs/lustre\n");
833 #else
834         proc_lustre_root = NULL;
835 #endif
836         return 0;
837 }
838
839 #ifdef __KERNEL__
840 static void __exit cleanup_obdclass(void)
841 #else
842 static void cleanup_obdclass(void)
843 #endif
844 {
845         int i;
846         ENTRY;
847
848         misc_deregister(&obd_psdev);
849         for (i = 0; i < MAX_OBD_DEVICES; i++) {
850                 struct obd_device *obd = &obd_dev[i];
851                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
852                     OBT(obd) && OBP(obd, detach)) {
853                         /* XXX should this call generic detach otherwise? */
854                         OBP(obd, detach)(obd);
855                 }
856         }
857
858         obd_cleanup_caches();
859 #ifdef __KERNEL__
860         obd_sysctl_clean();
861 #endif
862         if (proc_lustre_root) {
863                 lprocfs_remove(proc_lustre_root);
864                 proc_lustre_root = NULL;
865         }
866
867         class_handle_cleanup();
868         class_exit_uuidlist();
869
870         CERROR("obd mem max: %d leaked: %d\n", obd_memmax,
871                atomic_read(&obd_memory));
872         EXIT;
873 }
874
875 /* Check that we're building against the appropriate version of the Lustre
876  * kernel patch */
877 #ifdef __KERNEL__
878 #include <linux/lustre_version.h>
879 #define LUSTRE_SOURCE_VERSION 13
880 #if (LUSTRE_KERNEL_VERSION < LUSTRE_SOURCE_VERSION)
881 # error Cannot continue: Your Lustre kernel patch is older than the sources
882 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_SOURCE_VERSION)
883 # error Cannot continue: Your Lustre sources are older than the kernel patch
884 #endif
885 #else
886 #warning "Lib Lustre - no versioning information"
887 #endif
888
889 #ifdef __KERNEL__
890 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
891 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
892 MODULE_LICENSE("GPL");
893
894 module_init(init_obdclass);
895 module_exit(cleanup_obdclass);
896 #endif