summaryrefslogtreecommitdiffstats
path: root/modules/spl/spl-generic.c
blob: 8cd217cf11fe6dbafb4109d8dda932373b0e80eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <sys/sysmacros.h>
#include <sys/vmsystm.h>
#include <sys/vnode.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <linux/proc_fs.h>
#include <linux/kmod.h>
#include "config.h"

/*
 * Generic support
 */
static char spl_debug_buffer1[1024];
static char spl_debug_buffer2[1024];
static spinlock_t spl_debug_lock = SPIN_LOCK_UNLOCKED;

unsigned long spl_debug_mask = 0;
unsigned long spl_debug_subsys = 0xff;
unsigned long spl_hostid = 0;
char hw_serial[11] = "<none>";

EXPORT_SYMBOL(spl_debug_mask);
EXPORT_SYMBOL(spl_debug_subsys);
EXPORT_SYMBOL(spl_hostid);
EXPORT_SYMBOL(hw_serial);

static struct proc_dir_entry *spl_proc_root = NULL;
static struct proc_dir_entry *spl_proc_debug_mask = NULL;
static struct proc_dir_entry *spl_proc_debug_subsys = NULL;
static struct proc_dir_entry *spl_proc_hostid = NULL;
static struct proc_dir_entry *spl_proc_hw_serial = NULL;

int p0 = 0;
EXPORT_SYMBOL(p0);

vmem_t *zio_alloc_arena = NULL;
EXPORT_SYMBOL(zio_alloc_arena);


int
highbit(unsigned long i)
{
        register int h = 1;

        if (i == 0)
                return (0);
#if BITS_PER_LONG == 64
        if (i & 0xffffffff00000000ul) {
                h += 32; i >>= 32;
        }
#endif
        if (i & 0xffff0000) {
                h += 16; i >>= 16;
        }
        if (i & 0xff00) {
                h += 8; i >>= 8;
        }
        if (i & 0xf0) {
                h += 4; i >>= 4;
        }
        if (i & 0xc) {
                h += 2; i >>= 2;
        }
        if (i & 0x2) {
                h += 1;
        }
        return (h);
}
EXPORT_SYMBOL(highbit);

int
ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result)
{
        char *end;
        return (*result = simple_strtoul(str, &end, base));
}
EXPORT_SYMBOL(ddi_strtoul);

/* XXX: Not the most efficient debug function ever.  This should be re-done
 * as an internal per-cpu in-memory debug log accessable via /proc/.  Not as
 * a shared global buffer everything gets serialize though.  That said I'll
 * worry about performance considerations once I've dealt with correctness.
 */
void
__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
{
        char *sfp;
        struct timeval tv;
	unsigned long flags;
        va_list ap;

        sfp = strrchr(file, '/');
        do_gettimeofday(&tv);

	/* XXX: This is particularly bad for performance, but we need to
	 * disable irqs here or two __dprintf()'s may deadlock on each
	 * other if one if called from an irq handler.  This is yet another
	 * reason why we really, really, need an internal debug log.
	 */
	spin_lock_irqsave(&spl_debug_lock, flags);
	memset(spl_debug_buffer1, 0, 1024);
	memset(spl_debug_buffer2, 0, 1024);

        snprintf(spl_debug_buffer1, 1023,
                 "spl: %lu.%06lu:%d:%u:%s:%d:%s(): ",
                 tv.tv_sec, tv.tv_usec, current->pid,
                 smp_processor_id(),
                 sfp == NULL ? file : sfp + 1,
                 line, func);

        va_start(ap, fmt);
        vsnprintf(spl_debug_buffer2, 1023, fmt, ap);
        va_end(ap);

        printk("%s%s", spl_debug_buffer1, spl_debug_buffer2);
	spin_unlock_irqrestore(&spl_debug_lock, flags);
}
EXPORT_SYMBOL(__dprintf);

static int
spl_proc_rd_generic_ul(char *page, char **start, off_t off,
		       int count, int *eof, unsigned long val)
{
        *start = page;
        *eof = 1;

	if (off || count > PAGE_SIZE)
		return 0;

	return snprintf(page, PAGE_SIZE, "0x%lx\n", val & 0xffffffff);
}

static int
spl_proc_rd_debug_mask(char *page, char **start, off_t off,
                       int count, int *eof, void *data)
{
	int rc;

	spin_lock(&spl_debug_lock);
	rc = spl_proc_rd_generic_ul(page, start, off, count,
	                            eof, spl_debug_mask);
	spin_unlock(&spl_debug_lock);

	return rc;
}

static int
spl_proc_rd_debug_subsys(char *page, char **start, off_t off,
                         int count, int *eof, void *data)
{
	int rc;

	spin_lock(&spl_debug_lock);
	rc = spl_proc_rd_generic_ul(page, start, off, count,
		                    eof, spl_debug_subsys);
	spin_unlock(&spl_debug_lock);

	return rc;
}

static int
spl_proc_rd_hostid(char *page, char **start, off_t off,
                   int count, int *eof, void *data)
{
        *start = page;
        *eof = 1;

	if (off || count > PAGE_SIZE)
		return 0;

	return snprintf(page, PAGE_SIZE, "%lx\n", spl_hostid & 0xffffffff);
}

static int
spl_proc_rd_hw_serial(char *page, char **start, off_t off,
                      int count, int *eof, void *data)
{
        *start = page;
        *eof = 1;

	if (off || count > PAGE_SIZE)
		return 0;

	strncpy(page, hw_serial, 11);
	return strlen(page);
}

static int
spl_proc_wr_generic_ul(const char *ubuf, unsigned long count,
                       unsigned long *val, int base)
{
	char *end, kbuf[32];

	if (count >= sizeof(kbuf))
		return -EOVERFLOW;

	if (copy_from_user(kbuf, ubuf, count))
		return -EFAULT;

	kbuf[count] = '\0';
	*val = (int)simple_strtoul(kbuf, &end, base);
	if (kbuf == end)
		return -EINVAL;

	return 0;
}

static int
spl_proc_wr_debug_mask(struct file *file, const char *ubuf,
                       unsigned long count, void *data, int mode)
{
	unsigned long val;
	int rc;

	rc = spl_proc_wr_generic_ul(ubuf, count, &val, 16);
	if (rc)
		return rc;

	spin_lock(&spl_debug_lock);
	spl_debug_mask = val;
	spin_unlock(&spl_debug_lock);

	return count;
}

static int
spl_proc_wr_debug_subsys(struct file *file, const char *ubuf,
                         unsigned long count, void *data, int mode)
{
	unsigned long val;
	int rc;

	rc = spl_proc_wr_generic_ul(ubuf, count, &val, 16);
	if (rc)
		return rc;

	spin_lock(&spl_debug_lock);
	spl_debug_subsys = val;
	spin_unlock(&spl_debug_lock);

	return count;
}

static int
spl_proc_wr_hostid(struct file *file, const char *ubuf,
                   unsigned long count, void *data, int mode)
{
	unsigned long val;
	int rc;

	rc = spl_proc_wr_generic_ul(ubuf, count, &val, 16);
	if (rc)
		return rc;

	spl_hostid = val;
	sprintf(hw_serial, "%lu\n", ((long)val >= 0) ? val : -val);

	return count;
}

static struct proc_dir_entry *
spl_register_proc_entry(const char *name, mode_t mode,
                        struct proc_dir_entry *parent, void *data,
                        void *read_proc, void *write_proc)
{
        struct proc_dir_entry *entry;

        entry = create_proc_entry(name, mode, parent);
        if (!entry)
                return ERR_PTR(-EINVAL);

        entry->data = data;
        entry->read_proc = read_proc;
        entry->write_proc = write_proc;

        return entry;
} /* register_proc_entry() */

void spl_set_debug_mask(unsigned long mask) {
	spin_lock(&spl_debug_lock);
	spl_debug_mask = mask;
	spin_unlock(&spl_debug_lock);
}
EXPORT_SYMBOL(spl_set_debug_mask);

void spl_set_debug_subsys(unsigned long mask) {
	spin_lock(&spl_debug_lock);
	spl_debug_subsys = mask;
	spin_unlock(&spl_debug_lock);
}
EXPORT_SYMBOL(spl_set_debug_subsys);

static int __init spl_init(void)
{
	int rc = 0;
	char sh_path[] = "/bin/sh";
	char *argv[] = { sh_path,
	                 "-c",
	                 "/usr/bin/hostid >/proc/spl/hostid",
	                 NULL };
	char *envp[] = { "HOME=/",
	                 "TERM=linux",
	                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
	                 NULL };

        spl_proc_root = proc_mkdir("spl", NULL);
        if (!spl_proc_root) {
                printk("spl: Error unable to create /proc/spl/ directory\n");
		return -EINVAL;
        }

	spl_proc_debug_mask = spl_register_proc_entry("debug_mask", 0644,
	                                          spl_proc_root, NULL,
	                                          spl_proc_rd_debug_mask,
	                                          spl_proc_wr_debug_mask);
	if (IS_ERR(spl_proc_debug_mask)) {
		rc = PTR_ERR(spl_proc_debug_mask);
		goto out;
	}

	spl_proc_debug_subsys = spl_register_proc_entry("debug_subsys", 0644,
	                                            spl_proc_root, NULL,
	                                            spl_proc_rd_debug_subsys,
	                                            spl_proc_wr_debug_subsys);
	if (IS_ERR(spl_proc_debug_subsys)) {
		rc = PTR_ERR(spl_proc_debug_subsys);
		goto out2;
	}

	spl_proc_hostid = spl_register_proc_entry("hostid", 0644,
	                                          spl_proc_root, NULL,
	                                          spl_proc_rd_hostid,
	                                          spl_proc_wr_hostid);
	if (IS_ERR(spl_proc_hostid)) {
		rc = PTR_ERR(spl_proc_hostid);
		goto out3;
	}

	spl_proc_hw_serial = spl_register_proc_entry("hw_serial", 0444,
	                                          spl_proc_root, NULL,
	                                          spl_proc_rd_hw_serial,
	                                          NULL);
	if (IS_ERR(spl_proc_hw_serial)) {
		rc = PTR_ERR(spl_proc_hw_serial);
		goto out4;
	}

	if ((rc = kmem_init()))
		goto out4;

	if ((rc = vn_init()))
		goto out4;

	/* Doing address resolution in the kernel is tricky and just
	 * not a good idea in general.  So to set the proper 'hw_serial'
	 * use the usermodehelper support to ask '/bin/sh' to run
	 * '/usr/bin/hostid' and redirect the result to /proc/spl/hostid
	 * for us to use.  It's a horific solution but it will do.
	 */
	if ((rc = call_usermodehelper(sh_path, argv, envp, 1)))
		goto out4;

        printk("spl: Loaded Solaris Porting Layer v%s\n", VERSION);

	return 0;

out4:
	if (spl_proc_hw_serial)
		remove_proc_entry("hw_serial", spl_proc_root);
out3:
	if (spl_proc_hostid)
		remove_proc_entry("hostid", spl_proc_root);
out2:
	if (spl_proc_debug_mask)
		remove_proc_entry("debug_mask", spl_proc_root);

	if (spl_proc_debug_subsys)
		remove_proc_entry("debug_subsys", spl_proc_root);
out:
        remove_proc_entry("spl", NULL);

	return rc;
}

static void spl_fini(void)
{
	vn_fini();
	kmem_fini();

	remove_proc_entry("hw_serial", spl_proc_root);
	remove_proc_entry("hostid", spl_proc_root);
	remove_proc_entry("debug_subsys", spl_proc_root);
	remove_proc_entry("debug_mask", spl_proc_root);
        remove_proc_entry("spl", NULL);

	return;
}

module_init(spl_init);
module_exit(spl_fini);

MODULE_AUTHOR("Lawrence Livermore National Labs");
MODULE_DESCRIPTION("Solaris Porting Layer");
MODULE_LICENSE("GPL");