OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [block/] [loop.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/drivers/block/loop.c
3
 *
4
 *  Written by Theodore Ts'o, 3/29/93
5
 *
6
 * Copyright 1993 by Theodore Ts'o.  Redistribution of this file is
7
 * permitted under the GNU Public License.
8
 *
9
 * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
10
 * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
11
 *
12
 * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
13
 *
14
 * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
15
 *
16
 * Fixed do_loop_request() re-entrancy - <Vincent.Renardias@waw.com> Mar 20, 1997
17
 */
18
 
19
#include <linux/module.h>
20
 
21
#include <linux/config.h>
22
#include <linux/fs.h>
23
#include <linux/stat.h>
24
#include <linux/errno.h>
25
#include <linux/major.h>
26
 
27
#include <asm/segment.h>
28
 
29
#ifdef CONFIG_BLK_DEV_LOOP_DES
30
#include <linux/des.h>
31
#endif
32
 
33
#ifdef CONFIG_BLK_DEV_LOOP_IDEA
34
#include <linux/idea.h>
35
#endif
36
 
37
#include <linux/loop.h>         /* must follow des.h */
38
 
39
#define MAJOR_NR LOOP_MAJOR
40
 
41
#define DEVICE_NAME "loop"
42
#define DEVICE_REQUEST do_lo_request
43
#define DEVICE_NR(device) (MINOR(device))
44
#define DEVICE_ON(device)
45
#define DEVICE_OFF(device)
46
#define DEVICE_NO_RANDOM
47
#define TIMEOUT_VALUE (6 * HZ)
48
#include <linux/blk.h>
49
 
50
#define MAX_LOOP 8
51
static struct loop_device loop_dev[MAX_LOOP];
52
static int loop_sizes[MAX_LOOP];
53
static int loop_blksizes[MAX_LOOP];
54
 
55
/*
56
 * Transfer functions
57
 */
58
static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
59
                  char *loop_buf, int size)
60
{
61
        if (cmd == READ)
62
                memcpy(loop_buf, raw_buf, size);
63
        else
64
                memcpy(raw_buf, loop_buf, size);
65
        return 0;
66
}
67
 
68
static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf,
69
                 char *loop_buf, int size)
70
{
71
        char    *in, *out, *key;
72
        int     i, keysize;
73
 
74
        if (cmd == READ) {
75
                in = raw_buf;
76
                out = loop_buf;
77
        } else {
78
                in = loop_buf;
79
                out = raw_buf;
80
        }
81
        key = lo->lo_encrypt_key;
82
        keysize = lo->lo_encrypt_key_size;
83
        for (i=0; i < size; i++)
84
                *out++ = *in++ ^ key[(i & 511) % keysize];
85
        return 0;
86
}
87
 
88
#ifdef DES_AVAILABLE
89
static int transfer_des(struct loop_device *lo, int cmd, char *raw_buf,
90
                  char *loop_buf, int size)
91
{
92
        unsigned long tmp[2];
93
        unsigned long x0,x1,p0,p1;
94
 
95
        if (size & 7)
96
                return -EINVAL;
97
        x0 = lo->lo_des_init[0];
98
        x1 = lo->lo_des_init[1];
99
        while (size) {
100
                if (cmd == READ) {
101
                        tmp[0] = (p0 = ((unsigned long *) raw_buf)[0])^x0;
102
                        tmp[1] = (p1 = ((unsigned long *) raw_buf)[1])^x1;
103
                        des_ecb_encrypt((des_cblock *) tmp,(des_cblock *)
104
                            loop_buf,lo->lo_des_key,DES_ENCRYPT);
105
                        x0 = p0^((unsigned long *) loop_buf)[0];
106
                        x1 = p1^((unsigned long *) loop_buf)[1];
107
                }
108
                else {
109
                        p0 = ((unsigned long *) loop_buf)[0];
110
                        p1 = ((unsigned long *) loop_buf)[1];
111
                        des_ecb_encrypt((des_cblock *) loop_buf,(des_cblock *)
112
                            raw_buf,lo->lo_des_key,DES_DECRYPT);
113
                        ((unsigned long *) raw_buf)[0] ^= x0;
114
                        ((unsigned long *) raw_buf)[1] ^= x1;
115
                        x0 = p0^((unsigned long *) raw_buf)[0];
116
                        x1 = p1^((unsigned long *) raw_buf)[1];
117
                }
118
                size -= 8;
119
                raw_buf += 8;
120
                loop_buf += 8;
121
        }
122
        return 0;
123
}
124
#endif
125
 
126
#ifdef IDEA_AVAILABLE
127
 
128
extern void idea_encrypt_block(idea_key,char *,char *,int);
129
 
130
static int transfer_idea(struct loop_device *lo, int cmd, char *raw_buf,
131
                  char *loop_buf, int size)
132
{
133
  if (cmd==READ) {
134
    idea_encrypt_block(lo->lo_idea_en_key,raw_buf,loop_buf,size);
135
  }
136
  else {
137
    idea_encrypt_block(lo->lo_idea_de_key,loop_buf,raw_buf,size);
138
  }
139
  return 0;
140
}
141
#endif
142
 
143
static transfer_proc_t xfer_funcs[MAX_LOOP] = {
144
        transfer_none,          /* LO_CRYPT_NONE */
145
        transfer_xor,           /* LO_CRYPT_XOR */
146
#ifdef DES_AVAILABLE
147
        transfer_des,           /* LO_CRYPT_DES */
148
#else
149
        NULL,                   /* LO_CRYPT_DES */
150
#endif
151
#ifdef IDEA_AVAILABLE           /* LO_CRYPT_IDEA */
152
        transfer_idea
153
#else
154
        NULL
155
#endif
156
};
157
 
158
 
159
#define MAX_DISK_SIZE 1024*1024*1024
160
 
161
 
162
static void figure_loop_size(struct loop_device *lo)
163
{
164
        int     size;
165
 
166
        if (S_ISREG(lo->lo_inode->i_mode))
167
                size = (lo->lo_inode->i_size - lo->lo_offset) / BLOCK_SIZE;
168
        else {
169
                kdev_t lodev = lo->lo_device;
170
                if (blk_size[MAJOR(lodev)])
171
                        size = blk_size[MAJOR(lodev)][MINOR(lodev)] -
172
                                lo->lo_offset / BLOCK_SIZE;
173
                else
174
                        size = MAX_DISK_SIZE;
175
        }
176
 
177
        loop_sizes[lo->lo_number] = size;
178
}
179
 
180
static void do_lo_request(void)
181
{
182
        int     real_block, block, offset, len, blksize, size;
183
        char    *dest_addr;
184
        struct loop_device *lo;
185
        struct buffer_head *bh;
186
        struct request *current_request;
187
 
188
repeat:
189
        INIT_REQUEST;
190
        current_request=CURRENT;
191
        CURRENT=current_request->next;
192
        if (MINOR(current_request->rq_dev) >= MAX_LOOP)
193
                goto error_out;
194
        lo = &loop_dev[MINOR(current_request->rq_dev)];
195
        if (!lo->lo_inode || !lo->transfer)
196
                goto error_out;
197
 
198
        blksize = BLOCK_SIZE;
199
        if (blksize_size[MAJOR(lo->lo_device)]) {
200
            blksize = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
201
            if (!blksize)
202
              blksize = BLOCK_SIZE;
203
        }
204
 
205
        dest_addr = current_request->buffer;
206
 
207
        if (blksize < 512) {
208
                block = current_request->sector * (512/blksize);
209
                offset = 0;
210
        } else {
211
                block = current_request->sector / (blksize >> 9);
212
                offset = (current_request->sector % (blksize >> 9)) << 9;
213
        }
214
        block += lo->lo_offset / blksize;
215
        offset += lo->lo_offset % blksize;
216
        if (offset > blksize) {
217
                block++;
218
                offset -= blksize;
219
        }
220
        len = current_request->current_nr_sectors << 9;
221
 
222
        if (current_request->cmd == WRITE) {
223
                if (lo->lo_flags & LO_FLAGS_READ_ONLY)
224
                        goto error_out;
225
        } else if (current_request->cmd != READ) {
226
                printk("unknown loop device command (%d)?!?", current_request->cmd);
227
                goto error_out;
228
        }
229
        while (len > 0) {
230
                real_block = block;
231
                if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
232
                        real_block = bmap(lo->lo_inode, block);
233
                        if (!real_block) {
234
                                printk("loop: block %d not present\n", block);
235
                                goto error_out;
236
                        }
237
                }
238
                bh = getblk(lo->lo_device, real_block, blksize);
239
                if (!bh) {
240
                        printk("loop: device %s: getblk(-, %d, %d) returned NULL",
241
                               kdevname(lo->lo_device),
242
                               block, blksize);
243
                        goto error_out;
244
                }
245
                if (!buffer_uptodate(bh) && ((current_request->cmd == READ) ||
246
                                        (offset || (len < blksize)))) {
247
                        ll_rw_block(READ, 1, &bh);
248
                        wait_on_buffer(bh);
249
                        if (!buffer_uptodate(bh)) {
250
                                brelse(bh);
251
                                goto error_out;
252
                        }
253
                }
254
                size = blksize - offset;
255
                if (size > len)
256
                        size = len;
257
 
258
                if ((lo->transfer)(lo, current_request->cmd, bh->b_data + offset,
259
                                   dest_addr, size)) {
260
                        printk("loop: transfer error block %d\n", block);
261
                        brelse(bh);
262
                        goto error_out;
263
                }
264
                if (current_request->cmd == WRITE) {
265
                        mark_buffer_uptodate(bh, 1);
266
                        mark_buffer_dirty(bh, 1);
267
                }
268
                brelse(bh);
269
                dest_addr += size;
270
                len -= size;
271
                offset = 0;
272
                block++;
273
        }
274
        current_request->next=CURRENT;
275
        CURRENT=current_request;
276
        end_request(1);
277
        goto repeat;
278
error_out:
279
    current_request->next=CURRENT;
280
        CURRENT=current_request;
281
        end_request(0);
282
        goto repeat;
283
}
284
 
285
static int loop_set_fd(struct loop_device *lo, kdev_t dev, unsigned int arg)
286
{
287
        struct file     *file;
288
        struct inode    *inode;
289
 
290
        if (arg >= NR_OPEN || !(file = current->files->fd[arg]))
291
                return -EBADF;
292
        if (lo->lo_inode)
293
                return -EBUSY;
294
        inode = file->f_inode;
295
        if (!inode) {
296
                printk("loop_set_fd: NULL inode?!?\n");
297
                return -EINVAL;
298
        }
299
        if (S_ISBLK(inode->i_mode)) {
300
                int error = blkdev_open(inode, file);
301
                if (error)
302
                        return error;
303
                lo->lo_device = inode->i_rdev;
304
                lo->lo_flags = 0;
305
        } else if (S_ISREG(inode->i_mode)) {
306
                lo->lo_device = inode->i_dev;
307
                lo->lo_flags = LO_FLAGS_DO_BMAP;
308
        } else
309
                return -EINVAL;
310
 
311
        if (IS_RDONLY (inode) || is_read_only(lo->lo_device)) {
312
                lo->lo_flags |= LO_FLAGS_READ_ONLY;
313
                set_device_ro(dev, 1);
314
        } else {
315
                invalidate_inode_pages (inode);
316
                set_device_ro(dev, 0);
317
        }
318
 
319
        lo->lo_inode = inode;
320
        lo->lo_inode->i_count++;
321
        lo->transfer = NULL;
322
        figure_loop_size(lo);
323
        MOD_INC_USE_COUNT;
324
        return 0;
325
}
326
 
327
static int loop_clr_fd(struct loop_device *lo, kdev_t dev)
328
{
329
        if (!lo->lo_inode)
330
                return -ENXIO;
331
        if (lo->lo_refcnt > 1)  /* we needed one fd for the ioctl */
332
                return -EBUSY;
333
        if (S_ISBLK(lo->lo_inode->i_mode))
334
                blkdev_release (lo->lo_inode);
335
        iput(lo->lo_inode);
336
        lo->lo_device = 0;
337
        lo->lo_inode = NULL;
338
        lo->lo_encrypt_type = 0;
339
        lo->lo_offset = 0;
340
        lo->lo_encrypt_key_size = 0;
341
        memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
342
        memset(lo->lo_name, 0, LO_NAME_SIZE);
343
        loop_sizes[lo->lo_number] = 0;
344
        invalidate_buffers(dev);
345
        MOD_DEC_USE_COUNT;
346
        return 0;
347
}
348
 
349
static int loop_set_status(struct loop_device *lo, struct loop_info *arg)
350
{
351
        struct loop_info info;
352
        int err;
353
 
354
        if (!lo->lo_inode)
355
                return -ENXIO;
356
        if (!arg)
357
                return -EINVAL;
358
        err = verify_area(VERIFY_READ, arg, sizeof(info));
359
        if (err)
360
                return err;
361
        memcpy_fromfs(&info, arg, sizeof(info));
362
        if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE)
363
                return -EINVAL;
364
        switch (info.lo_encrypt_type) {
365
        case LO_CRYPT_NONE:
366
                break;
367
        case LO_CRYPT_XOR:
368
                if (info.lo_encrypt_key_size <= 0)
369
                        return -EINVAL;
370
                break;
371
#ifdef DES_AVAILABLE
372
        case LO_CRYPT_DES:
373
                if (info.lo_encrypt_key_size != 8)
374
                        return -EINVAL;
375
                des_set_key((des_cblock *) lo->lo_encrypt_key,
376
                   lo->lo_des_key);
377
                memcpy(lo->lo_des_init,info.lo_init,8);
378
                break;
379
#endif
380
#ifdef IDEA_AVAILABLE
381
        case LO_CRYPT_IDEA:
382
          {
383
                uint16 tmpkey[8];
384
 
385
                if (info.lo_encrypt_key_size != IDEAKEYSIZE)
386
                        return -EINVAL;
387
                /* create key in lo-> from info.lo_encrypt_key */
388
                memcpy(tmpkey,info.lo_encrypt_key,sizeof(tmpkey));
389
                en_key_idea(tmpkey,lo->lo_idea_en_key);
390
                de_key_idea(lo->lo_idea_en_key,lo->lo_idea_de_key);
391
                break;
392
          }
393
#endif
394
        default:
395
                return -EINVAL;
396
        }
397
        lo->lo_offset = info.lo_offset;
398
        strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE);
399
        lo->lo_encrypt_type = info.lo_encrypt_type;
400
        lo->transfer = xfer_funcs[lo->lo_encrypt_type];
401
        lo->lo_encrypt_key_size = info.lo_encrypt_key_size;
402
        if (info.lo_encrypt_key_size)
403
                memcpy(lo->lo_encrypt_key, info.lo_encrypt_key,
404
                       info.lo_encrypt_key_size);
405
        figure_loop_size(lo);
406
        return 0;
407
}
408
 
409
static int loop_get_status(struct loop_device *lo, struct loop_info *arg)
410
{
411
        struct loop_info        info;
412
        int err;
413
 
414
        if (!lo->lo_inode)
415
                return -ENXIO;
416
        if (!arg)
417
                return -EINVAL;
418
        err = verify_area(VERIFY_WRITE, arg, sizeof(info));
419
        if (err)
420
                return err;
421
        memset(&info, 0, sizeof(info));
422
        info.lo_number = lo->lo_number;
423
        info.lo_device = kdev_t_to_nr(lo->lo_inode->i_dev);
424
        info.lo_inode = lo->lo_inode->i_ino;
425
        info.lo_rdevice = kdev_t_to_nr(lo->lo_device);
426
        info.lo_offset = lo->lo_offset;
427
        info.lo_flags = lo->lo_flags;
428
        strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE);
429
        info.lo_encrypt_type = lo->lo_encrypt_type;
430
        if (lo->lo_encrypt_key_size && suser()) {
431
                info.lo_encrypt_key_size = lo->lo_encrypt_key_size;
432
                memcpy(info.lo_encrypt_key, lo->lo_encrypt_key,
433
                       lo->lo_encrypt_key_size);
434
        }
435
        memcpy_tofs(arg, &info, sizeof(info));
436
        return 0;
437
}
438
 
439
static int lo_ioctl(struct inode * inode, struct file * file,
440
        unsigned int cmd, unsigned long arg)
441
{
442
        struct loop_device *lo;
443
        int dev, err;
444
 
445
        if (!inode)
446
                return -EINVAL;
447
        if (MAJOR(inode->i_rdev) != MAJOR_NR) {
448
                printk("lo_ioctl: pseudo-major != %d\n", MAJOR_NR);
449
                return -ENODEV;
450
        }
451
        dev = MINOR(inode->i_rdev);
452
        if (dev >= MAX_LOOP)
453
                return -ENODEV;
454
        lo = &loop_dev[dev];
455
        switch (cmd) {
456
        case LOOP_SET_FD:
457
                return loop_set_fd(lo, inode->i_rdev, arg);
458
        case LOOP_CLR_FD:
459
                return loop_clr_fd(lo, inode->i_rdev);
460
        case LOOP_SET_STATUS:
461
                return loop_set_status(lo, (struct loop_info *) arg);
462
        case LOOP_GET_STATUS:
463
                return loop_get_status(lo, (struct loop_info *) arg);
464
        case BLKGETSIZE:   /* Return device size */
465
                if (!lo->lo_inode)
466
                        return -ENXIO;
467
                if (!arg)  return -EINVAL;
468
                err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
469
                if (err)
470
                        return err;
471
                put_fs_long(loop_sizes[lo->lo_number] << 1, (long *) arg);
472
                return 0;
473
                default:
474
                        return -EINVAL;
475
        }
476
        return 0;
477
}
478
 
479
static int lo_open(struct inode *inode, struct file *file)
480
{
481
        struct loop_device *lo;
482
        int     dev;
483
 
484
        if (!inode)
485
                return -EINVAL;
486
        if (MAJOR(inode->i_rdev) != MAJOR_NR) {
487
                printk("lo_open: pseudo-major != %d\n", MAJOR_NR);
488
                return -ENODEV;
489
        }
490
        dev = MINOR(inode->i_rdev);
491
        if (dev >= MAX_LOOP)
492
                return -ENODEV;
493
        lo = &loop_dev[dev];
494
        lo->lo_refcnt++;
495
        MOD_INC_USE_COUNT;
496
        return 0;
497
}
498
 
499
static void lo_release(struct inode *inode, struct file *file)
500
{
501
        struct loop_device *lo;
502
        int     dev;
503
 
504
        if (!inode)
505
                return;
506
        if (MAJOR(inode->i_rdev) != MAJOR_NR) {
507
                printk("lo_release: pseudo-major != %d\n", MAJOR_NR);
508
                return;
509
        }
510
        dev = MINOR(inode->i_rdev);
511
        if (dev >= MAX_LOOP)
512
                return;
513
        fsync_dev(inode->i_rdev);
514
        lo = &loop_dev[dev];
515
        if (lo->lo_refcnt <= 0)
516
                printk("lo_release: refcount(%d) <= 0\n", lo->lo_refcnt);
517
        else  {
518
                lo->lo_refcnt--;
519
                MOD_DEC_USE_COUNT;
520
        }
521
}
522
 
523
static struct file_operations lo_fops = {
524
        NULL,                   /* lseek - default */
525
        block_read,             /* read - general block-dev read */
526
        block_write,            /* write - general block-dev write */
527
        NULL,                   /* readdir - bad */
528
        NULL,                   /* select */
529
        lo_ioctl,               /* ioctl */
530
        NULL,                   /* mmap */
531
        lo_open,                /* open */
532
        lo_release              /* release */
533
};
534
 
535
/*
536
 * And now the modules code and kernel interface.
537
 */
538
#ifdef MODULE
539
#define loop_init init_module
540
#endif
541
 
542
int
543
loop_init( void ) {
544
        int     i;
545
 
546
        if (register_blkdev(MAJOR_NR, "loop", &lo_fops)) {
547
                printk("Unable to get major number %d for loop device\n",
548
                       MAJOR_NR);
549
                return -EIO;
550
        }
551
#ifndef MODULE
552
        printk("loop: registered device at major %d\n", MAJOR_NR);
553
#ifdef DES_AVAILABLE
554
        printk("loop: DES encryption available\n");
555
#endif
556
#ifdef IDEA_AVAILABLE
557
        printk("loop: IDEA encryption available\n");
558
#endif
559
#endif
560
 
561
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
562
        for (i=0; i < MAX_LOOP; i++) {
563
                memset(&loop_dev[i], 0, sizeof(struct loop_device));
564
                loop_dev[i].lo_number = i;
565
        }
566
        memset(&loop_sizes, 0, sizeof(loop_sizes));
567
        memset(&loop_blksizes, 0, sizeof(loop_blksizes));
568
        blk_size[MAJOR_NR] = loop_sizes;
569
        blksize_size[MAJOR_NR] = loop_blksizes;
570
 
571
        return 0;
572
}
573
 
574
#ifdef MODULE
575
void
576
cleanup_module( void ) {
577
  if (unregister_blkdev(MAJOR_NR, "loop") != 0)
578
    printk("loop: cleanup_module failed\n");
579
}
580
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.