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