root/drivers/block/loop.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. transfer_none
  2. transfer_xor
  3. transfer_des
  4. figure_loop_size
  5. do_lo_request
  6. loop_set_fd
  7. loop_clr_fd
  8. loop_set_status
  9. loop_get_status
  10. lo_ioctl
  11. lo_open
  12. lo_release
  13. loop_init
  14. cleanup_module

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

/* [previous][next][first][last][top][bottom][index][help] */