~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/block/loop.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
 11  *
 12  * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
 13  * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
 14  *
 15  * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
 16  *
 17  * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
 18  *
 19  * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
 20  *
 21  * Loadable modules and other fixes by AK, 1998
 22  *
 23  * Make real block number available to downstream transfer functions, enables
 24  * CBC (and relatives) mode encryption requiring unique IVs per data block. 
 25  * Reed H. Petty, rhp@draper.net
 26  *
 27  * Maximum number of loop devices now dynamic via max_loop module parameter.
 28  * Russell Kroll <rkroll@exploits.org> 19990701
 29  * 
 30  * Maximum number of loop devices when compiled-in now selectable by passing
 31  * max_loop=<1-255> to the kernel on boot.
 32  * Erik I. Bolsų, <eriki@himolde.no>, Oct 31, 1999
 33  *
 34  * Still To Fix:
 35  * - Advisory locking is ignored here. 
 36  * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 
 37  * - Should use the underlying filesystems/devices read function if possible
 38  *   to support read ahead (and for write)
 39  *
 40  * WARNING/FIXME:
 41  * - The block number as IV passing to low level transfer functions is broken:
 42  *   it passes the underlying device's block number instead of the
 43  *   offset. This makes it change for a given block when the file is 
 44  *   moved/restored/copied and also doesn't work over NFS. 
 45  * AV, Feb 12, 2000: we pass the logical block number now. It fixes the
 46  *   problem above. Encryption modules that used to rely on the old scheme
 47  *   should just call ->i_mapping->bmap() to calculate the physical block
 48  *   number.
 49  */ 
 50 
 51 #include <linux/module.h>
 52 
 53 #include <linux/sched.h>
 54 #include <linux/fs.h>
 55 #include <linux/file.h>
 56 #include <linux/stat.h>
 57 #include <linux/errno.h>
 58 #include <linux/major.h>
 59 
 60 #include <linux/init.h>
 61 #include <linux/devfs_fs_kernel.h>
 62 
 63 #include <asm/uaccess.h>
 64 
 65 #include <linux/loop.h>         
 66 
 67 #define MAJOR_NR LOOP_MAJOR
 68 
 69 #define DEVICE_NAME "loop"
 70 #define DEVICE_REQUEST do_lo_request
 71 #define DEVICE_NR(device) (MINOR(device))
 72 #define DEVICE_ON(device)
 73 #define DEVICE_OFF(device)
 74 #define DEVICE_NO_RANDOM
 75 #define TIMEOUT_VALUE (6 * HZ)
 76 #include <linux/blk.h>
 77 
 78 #include <linux/malloc.h>
 79 static int max_loop = 8;
 80 static struct loop_device *loop_dev;
 81 static int *loop_sizes;
 82 static int *loop_blksizes;
 83 static devfs_handle_t devfs_handle;      /*  For the directory */
 84 
 85 #define FALSE 0
 86 #define TRUE (!FALSE)
 87 
 88 /*
 89  * Transfer functions
 90  */
 91 static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
 92                   char *loop_buf, int size, int real_block)
 93 {
 94         if (cmd == READ)
 95                 memcpy(loop_buf, raw_buf, size);
 96         else
 97                 memcpy(raw_buf, loop_buf, size);
 98         return 0;
 99 }
100 
101 static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf,
102                  char *loop_buf, int size, int real_block)
103 {
104         char    *in, *out, *key;
105         int     i, keysize;
106 
107         if (cmd == READ) {
108                 in = raw_buf;
109                 out = loop_buf;
110         } else {
111                 in = loop_buf;
112                 out = raw_buf;
113         }
114         key = lo->lo_encrypt_key;
115         keysize = lo->lo_encrypt_key_size;
116         for (i=0; i < size; i++)
117                 *out++ = *in++ ^ key[(i & 511) % keysize];
118         return 0;
119 }
120 
121 static int none_status(struct loop_device *lo, struct loop_info *info)
122 {
123         return 0; 
124 } 
125 
126 static int xor_status(struct loop_device *lo, struct loop_info *info)
127 {
128         if (info->lo_encrypt_key_size <= 0)
129                 return -EINVAL;
130         return 0;
131 }
132 
133 struct loop_func_table none_funcs = { 
134         number: LO_CRYPT_NONE,
135         transfer: transfer_none,
136         init: none_status
137 };      
138 
139 struct loop_func_table xor_funcs = { 
140         number: LO_CRYPT_XOR,
141         transfer: transfer_xor,
142         init: xor_status
143 };      
144 
145 /* xfer_funcs[0] is special - its release function is never called */ 
146 struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
147         &none_funcs,
148         &xor_funcs  
149 };
150 
151 #define MAX_DISK_SIZE 1024*1024*1024
152 
153 static void figure_loop_size(struct loop_device *lo)
154 {
155         int     size;
156 
157         if (S_ISREG(lo->lo_dentry->d_inode->i_mode))
158                 size = (lo->lo_dentry->d_inode->i_size - lo->lo_offset) >> BLOCK_SIZE_BITS;
159         else {
160                 kdev_t lodev = lo->lo_device;
161                 if (blk_size[MAJOR(lodev)])
162                         size = blk_size[MAJOR(lodev)][MINOR(lodev)] -
163                                 (lo->lo_offset >> BLOCK_SIZE_BITS);
164                 else
165                         size = MAX_DISK_SIZE;
166         }
167 
168         loop_sizes[lo->lo_number] = size;
169 }
170 
171 static int lo_send(struct loop_device *lo, char *data, int len, loff_t pos,
172         int blksize)
173 {
174         struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
175         struct address_space *mapping = lo->lo_dentry->d_inode->i_mapping;
176         struct address_space_operations *aops = mapping->a_ops;
177         struct page *page;
178         char *kaddr;
179         unsigned long index;
180         unsigned size, offset;
181 
182         index = pos >> PAGE_CACHE_SHIFT;
183         offset = pos & (PAGE_CACHE_SIZE - 1);
184         while (len > 0) {
185                 int IV = index * (PAGE_CACHE_SIZE/blksize) + offset/blksize;
186                 size = PAGE_CACHE_SIZE - offset;
187                 if (size > len)
188                         size = len;
189 
190                 page = grab_cache_page(mapping, index);
191                 if (!page)
192                         goto fail;
193                 if (aops->prepare_write(file, page, offset, offset+size))
194                         goto unlock;
195                 kaddr = page_address(page);
196                 if ((lo->transfer)(lo, WRITE, kaddr+offset, data, size, IV))
197                         goto write_fail;
198                 if (aops->commit_write(file, page, offset, offset+size))
199                         goto unlock;
200                 data += size;
201                 len -= size;
202                 offset = 0;
203                 index++;
204                 pos += size;
205                 UnlockPage(page);
206                 page_cache_release(page);
207         }
208         return 0;
209 
210 write_fail:
211         printk(KERN_ERR "loop: transfer error block %ld\n", index);
212         ClearPageUptodate(page);
213         kunmap(page);
214 unlock:
215         UnlockPage(page);
216         page_cache_release(page);
217 fail:
218         return -1;
219 }
220 
221 struct lo_read_data {
222         struct loop_device *lo;
223         char *data;
224         int blksize;
225 };
226 
227 static int lo_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
228 {
229         char *kaddr;
230         unsigned long count = desc->count;
231         struct lo_read_data *p = (struct lo_read_data*)desc->buf;
232         struct loop_device *lo = p->lo;
233         int IV = page->index * (PAGE_CACHE_SIZE/p->blksize) + offset/p->blksize;
234 
235         if (size > count)
236                 size = count;
237 
238         kaddr = kmap(page);
239         if ((lo->transfer)(lo,READ,kaddr+offset,p->data,size,IV)) {
240                 size = 0;
241                 printk(KERN_ERR "loop: transfer error block %ld\n",
242                        page->index);
243                 desc->error = -EINVAL;
244         }
245         kunmap(page);
246         
247         desc->count = count - size;
248         desc->written += size;
249         p->data += size;
250         return size;
251 }
252 
253 static int lo_receive(struct loop_device *lo, char *data, int len, loff_t pos,
254         int blksize)
255 {
256         struct file *file = lo->lo_backing_file;
257         struct lo_read_data cookie;
258         read_descriptor_t desc;
259 
260         cookie.lo = lo;
261         cookie.data = data;
262         cookie.blksize = blksize;
263         desc.written = 0;
264         desc.count = len;
265         desc.buf = (char*)&cookie;
266         desc.error = 0;
267         do_generic_file_read(file, &pos, &desc, lo_read_actor);
268         return desc.error;
269 }
270 
271 static void do_lo_request(request_queue_t * q)
272 {
273         int     block, offset, len, blksize, size;
274         char    *dest_addr;
275         struct loop_device *lo;
276         struct buffer_head *bh;
277         struct request *current_request;
278         loff_t pos;
279 
280 repeat:
281         INIT_REQUEST;
282         current_request=CURRENT;
283         blkdev_dequeue_request(current_request);
284         if (MINOR(current_request->rq_dev) >= max_loop)
285                 goto error_out;
286         lo = &loop_dev[MINOR(current_request->rq_dev)];
287         if (!lo->lo_dentry || !lo->transfer)
288                 goto error_out;
289         if (current_request->cmd == WRITE) {
290                 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
291                         goto error_out;
292         } else if (current_request->cmd != READ) {
293                 printk(KERN_ERR "unknown loop device command (%d)?!?",
294                        current_request->cmd);
295                 goto error_out;
296         }
297 
298         dest_addr = current_request->buffer;
299         len = current_request->current_nr_sectors << 9;
300 
301         blksize = BLOCK_SIZE;
302         if (blksize_size[MAJOR(lo->lo_device)]) {
303             blksize = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
304             if (!blksize)
305               blksize = BLOCK_SIZE;
306         }
307 
308         if (lo->lo_flags & LO_FLAGS_DO_BMAP)
309                 goto file_backed;
310 
311         if (blksize < 512) {
312                 block = current_request->sector * (512/blksize);
313                 offset = 0;
314         } else {
315                 block = current_request->sector / (blksize >> 9);
316                 offset = (current_request->sector % (blksize >> 9)) << 9;
317         }
318         block += lo->lo_offset / blksize;
319         offset += lo->lo_offset % blksize;
320         if (offset >= blksize) {
321                 block++;
322                 offset -= blksize;
323         }
324         spin_unlock_irq(&io_request_lock);
325 
326         while (len > 0) {
327 
328                 size = blksize - offset;
329                 if (size > len)
330                         size = len;
331 
332                 bh = getblk(lo->lo_device, block, blksize);
333                 if (!bh) {
334                         printk(KERN_ERR "loop: device %s: getblk(-, %d, %d) returned NULL",
335                                 kdevname(lo->lo_device),
336                                 block, blksize);
337                         goto error_out_lock;
338                 }
339                 if (!buffer_uptodate(bh) && ((current_request->cmd == READ) ||
340                                         (offset || (len < blksize)))) {
341                         ll_rw_block(READ, 1, &bh);
342                         wait_on_buffer(bh);
343                         if (!buffer_uptodate(bh)) {
344                                 brelse(bh);
345                                 goto error_out_lock;
346                         }
347                 }
348 
349                 if ((lo->transfer)(lo, current_request->cmd,
350                                    bh->b_data + offset,
351                                    dest_addr, size, block)) {
352                         printk(KERN_ERR "loop: transfer error block %d\n",
353                                block);
354                         brelse(bh);
355                         goto error_out_lock;
356                 }
357 
358                 if (current_request->cmd == WRITE) {
359                         mark_buffer_uptodate(bh, 1);
360                         mark_buffer_dirty(bh);
361                 }
362                 brelse(bh);
363                 dest_addr += size;
364                 len -= size;
365                 offset = 0;
366                 block++;
367         }
368         goto done;
369 
370 file_backed:
371         pos = ((loff_t)current_request->sector << 9) + lo->lo_offset;
372         spin_unlock_irq(&io_request_lock);
373         if (current_request->cmd == WRITE) {
374                 if (lo_send(lo, dest_addr, len, pos, blksize))
375                         goto error_out_lock;
376         } else {
377                 if (lo_receive(lo, dest_addr, len, pos, blksize))
378                         goto error_out_lock;
379         }
380 done:
381         spin_lock_irq(&io_request_lock);
382         current_request->sector += current_request->current_nr_sectors;
383         current_request->nr_sectors -= current_request->current_nr_sectors;
384         list_add(&current_request->queue, &q->queue_head);
385         end_request(1);
386         goto repeat;
387 error_out_lock:
388         spin_lock_irq(&io_request_lock);
389 error_out:
390         list_add(&current_request->queue, &q->queue_head);
391         end_request(0);
392         goto repeat;
393 }
394 
395 static int loop_set_fd(struct loop_device *lo, kdev_t dev, unsigned int arg)
396 {
397         struct file     *file;
398         struct inode    *inode;
399         int error;
400 
401         MOD_INC_USE_COUNT;
402 
403         error = -EBUSY;
404         if (lo->lo_dentry)
405                 goto out;
406 
407         error = -EBADF;
408         file = fget(arg);
409         if (!file)
410                 goto out;
411 
412         error = -EINVAL;
413         inode = file->f_dentry->d_inode;
414 
415         if (S_ISBLK(inode->i_mode)) {
416                 /* dentry will be wired, so... */
417                 error = blkdev_get(inode->i_bdev, file->f_mode,
418                                    file->f_flags, BDEV_FILE);
419 
420                 lo->lo_device = inode->i_rdev;
421                 lo->lo_flags = 0;
422 
423                 /* Backed by a block device - don't need to hold onto
424                    a file structure */
425                 lo->lo_backing_file = NULL;
426 
427                 if (error)
428                         goto out_putf;
429         } else if (S_ISREG(inode->i_mode)) {
430                 struct address_space_operations *aops;
431 
432                 aops = inode->i_mapping->a_ops;
433                 /*
434                  * If we can't read - sorry. If we only can't write - well,
435                  * it's going to be read-only.
436                  */
437                 error = -EINVAL;
438                 if (!aops->readpage)
439                         goto out_putf;
440 
441                 if (!aops->prepare_write || !aops->commit_write)
442                         lo->lo_flags |= LO_FLAGS_READ_ONLY;
443 
444                 error = get_write_access(inode);
445                 if (error)
446                         goto out_putf;
447 
448                 /* Backed by a regular file - we need to hold onto a file
449                    structure for this file.  Friggin' NFS can't live without
450                    it on write and for reading we use do_generic_file_read(),
451                    so...  We create a new file structure based on the one
452                    passed to us via 'arg'.  This is to avoid changing the file
453                    structure that the caller is using */
454 
455                 lo->lo_device = inode->i_dev;
456                 lo->lo_flags |= LO_FLAGS_DO_BMAP;
457 
458                 error = -ENFILE;
459                 lo->lo_backing_file = get_empty_filp();
460                 if (lo->lo_backing_file == NULL) {
461                         put_write_access(inode);
462                         goto out_putf;
463                 }
464 
465                 lo->lo_backing_file->f_mode = file->f_mode;
466                 lo->lo_backing_file->f_pos = file->f_pos;
467                 lo->lo_backing_file->f_flags = file->f_flags;
468                 lo->lo_backing_file->f_owner = file->f_owner;
469                 lo->lo_backing_file->f_dentry = file->f_dentry;
470                 lo->lo_backing_file->f_vfsmnt = mntget(file->f_vfsmnt);
471                 lo->lo_backing_file->f_op = fops_get(file->f_op);
472                 lo->lo_backing_file->private_data = file->private_data;
473                 file_moveto(lo->lo_backing_file, file);
474 
475                 error = 0;
476         }
477 
478         if (IS_RDONLY (inode) || is_read_only(lo->lo_device))
479                 lo->lo_flags |= LO_FLAGS_READ_ONLY;
480 
481         set_device_ro(dev, (lo->lo_flags & LO_FLAGS_READ_ONLY)!=0);
482 
483         lo->lo_dentry = dget(file->f_dentry);
484         lo->transfer = NULL;
485         lo->ioctl = NULL;
486         figure_loop_size(lo);
487 
488  out_putf:
489         fput(file);
490  out:
491         if (error)
492                 MOD_DEC_USE_COUNT;
493         return error;
494 }
495 
496 static int loop_release_xfer(struct loop_device *lo)
497 {
498         int err = 0; 
499         if (lo->lo_encrypt_type) {
500                 struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type]; 
501                 if (xfer && xfer->release)
502                         err = xfer->release(lo); 
503                 if (xfer && xfer->unlock)
504                         xfer->unlock(lo); 
505                 lo->lo_encrypt_type = 0;
506         }
507         return err;
508 }
509 
510 static int loop_init_xfer(struct loop_device *lo, int type,struct loop_info *i)
511 {
512         int err = 0; 
513         if (type) {
514                 struct loop_func_table *xfer = xfer_funcs[type]; 
515                 if (xfer->init)
516                         err = xfer->init(lo, i);
517                 if (!err) { 
518                         lo->lo_encrypt_type = type;
519                         if (xfer->lock)
520                                 xfer->lock(lo);
521                 }
522         }
523         return err;
524 }  
525 
526 static int loop_clr_fd(struct loop_device *lo, kdev_t dev)
527 {
528         struct dentry *dentry = lo->lo_dentry;
529 
530         if (!dentry)
531                 return -ENXIO;
532         if (lo->lo_refcnt > 1)  /* we needed one fd for the ioctl */
533                 return -EBUSY;
534 
535         if (S_ISBLK(dentry->d_inode->i_mode))
536                 blkdev_put(dentry->d_inode->i_bdev, BDEV_FILE);
537 
538         lo->lo_dentry = NULL;
539 
540         if (lo->lo_backing_file != NULL) {
541                 struct file *filp = lo->lo_backing_file;
542                 if ((filp->f_mode & FMODE_WRITE) == 0)
543                         put_write_access(filp->f_dentry->d_inode);
544                 fput(filp);
545                 lo->lo_backing_file = NULL;
546         } else {
547                 dput(dentry);
548         }
549 
550         loop_release_xfer(lo);
551         lo->transfer = NULL;
552         lo->ioctl = NULL;
553         lo->lo_device = 0;
554         lo->lo_encrypt_type = 0;
555         lo->lo_offset = 0;
556         lo->lo_encrypt_key_size = 0;
557         memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
558         memset(lo->lo_name, 0, LO_NAME_SIZE);
559         loop_sizes[lo->lo_number] = 0;
560         invalidate_buffers(dev);
561         MOD_DEC_USE_COUNT;
562         return 0;
563 }
564 
565 static int loop_set_status(struct loop_device *lo, struct loop_info *arg)
566 {
567         struct loop_info info; 
568         int err;
569         unsigned int type;
570 
571         if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid && 
572             !capable(CAP_SYS_ADMIN))
573                 return -EPERM;
574         if (!lo->lo_dentry)
575                 return -ENXIO;
576         if (copy_from_user(&info, arg, sizeof (struct loop_info)))
577                 return -EFAULT; 
578         if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE)
579                 return -EINVAL;
580         type = info.lo_encrypt_type; 
581         if (type >= MAX_LO_CRYPT || xfer_funcs[type] == NULL)
582                 return -EINVAL;
583         if (type == LO_CRYPT_XOR && info.lo_encrypt_key_size == 0)
584                 return -EINVAL;
585         err = loop_release_xfer(lo);
586         if (!err) 
587                 err = loop_init_xfer(lo, type, &info);
588         if (err)
589                 return err;     
590 
591         lo->lo_offset = info.lo_offset;
592         strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE);
593 
594         lo->transfer = xfer_funcs[type]->transfer;
595         lo->ioctl = xfer_funcs[type]->ioctl;
596         lo->lo_encrypt_key_size = info.lo_encrypt_key_size;
597         lo->lo_init[0] = info.lo_init[0];
598         lo->lo_init[1] = info.lo_init[1];
599         if (info.lo_encrypt_key_size) {
600                 memcpy(lo->lo_encrypt_key, info.lo_encrypt_key, 
601                        info.lo_encrypt_key_size);
602                 lo->lo_key_owner = current->uid; 
603         }       
604         figure_loop_size(lo);
605         return 0;
606 }
607 
608 static int loop_get_status(struct loop_device *lo, struct loop_info *arg)
609 {
610         struct loop_info        info;
611 
612         if (!lo->lo_dentry)
613                 return -ENXIO;
614         if (!arg)
615                 return -EINVAL;
616         memset(&info, 0, sizeof(info));
617         info.lo_number = lo->lo_number;
618         info.lo_device = kdev_t_to_nr(lo->lo_dentry->d_inode->i_dev);
619         info.lo_inode = lo->lo_dentry->d_inode->i_ino;
620         info.lo_rdevice = kdev_t_to_nr(lo->lo_device);
621         info.lo_offset = lo->lo_offset;
622         info.lo_flags = lo->lo_flags;
623         strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE);
624         info.lo_encrypt_type = lo->lo_encrypt_type;
625         if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
626                 info.lo_encrypt_key_size = lo->lo_encrypt_key_size;
627                 memcpy(info.lo_encrypt_key, lo->lo_encrypt_key,
628                        lo->lo_encrypt_key_size);
629         }
630         return copy_to_user(arg, &info, sizeof(info)) ? -EFAULT : 0;
631 }
632 
633 static int lo_ioctl(struct inode * inode, struct file * file,
634         unsigned int cmd, unsigned long arg)
635 {
636         struct loop_device *lo;
637         int dev;
638 
639         if (!inode)
640                 return -EINVAL;
641         if (MAJOR(inode->i_rdev) != MAJOR_NR) {
642                 printk(KERN_WARNING "lo_ioctl: pseudo-major != %d\n",
643                        MAJOR_NR);
644                 return -ENODEV;
645         }
646         dev = MINOR(inode->i_rdev);
647         if (dev >= max_loop)
648                 return -ENODEV;
649         lo = &loop_dev[dev];
650         switch (cmd) {
651         case LOOP_SET_FD:
652                 return loop_set_fd(lo, inode->i_rdev, arg);
653         case LOOP_CLR_FD:
654                 return loop_clr_fd(lo, inode->i_rdev);
655         case LOOP_SET_STATUS:
656                 return loop_set_status(lo, (struct loop_info *) arg);
657         case LOOP_GET_STATUS:
658                 return loop_get_status(lo, (struct loop_info *) arg);
659         case BLKGETSIZE:   /* Return device size */
660                 if (!lo->lo_dentry)
661                         return -ENXIO;
662                 if (!arg)
663                         return -EINVAL;
664                 return put_user(loop_sizes[lo->lo_number] << 1, (long *) arg);
665         default:
666                 return lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
667         }
668         return 0;
669 }
670 
671 static int lo_open(struct inode *inode, struct file *file)
672 {
673         struct loop_device *lo;
674         int     dev, type;
675 
676 
677         if (!inode)
678                 return -EINVAL;
679         if (MAJOR(inode->i_rdev) != MAJOR_NR) {
680                 printk(KERN_WARNING "lo_open: pseudo-major != %d\n", MAJOR_NR);
681                 return -ENODEV;
682         }
683         dev = MINOR(inode->i_rdev);
684         if (dev >= max_loop) {
685                 return -ENODEV;
686         }
687         lo = &loop_dev[dev];
688 
689         type = lo->lo_encrypt_type; 
690         if (type && xfer_funcs[type] && xfer_funcs[type]->lock)
691                 xfer_funcs[type]->lock(lo);
692         lo->lo_refcnt++;
693         MOD_INC_USE_COUNT;
694         return 0;
695 }
696 
697 static int lo_release(struct inode *inode, struct file *file)
698 {
699         struct loop_device *lo;
700         int     dev;
701 
702         if (!inode)
703                 return 0;
704         if (MAJOR(inode->i_rdev) != MAJOR_NR) {
705                 printk(KERN_WARNING "lo_release: pseudo-major != %d\n",
706                        MAJOR_NR);
707                 return 0;
708         }
709         dev = MINOR(inode->i_rdev);
710         if (dev >= max_loop)
711                 return 0;
712         lo = &loop_dev[dev];
713         if (lo->lo_refcnt <= 0)
714                 printk(KERN_ERR "lo_release: refcount(%d) <= 0\n",
715                        lo->lo_refcnt);
716         else  {
717                 int type  = lo->lo_encrypt_type;
718                 --lo->lo_refcnt;
719                 if (xfer_funcs[type] && xfer_funcs[type]->unlock)
720                         xfer_funcs[type]->unlock(lo);
721                 MOD_DEC_USE_COUNT;
722         }
723         return 0;
724 }
725 
726 static struct block_device_operations lo_fops = {
727         open:           lo_open,
728         release:        lo_release,
729         ioctl:          lo_ioctl,
730 };
731 
732 /*
733  * And now the modules code and kernel interface.
734  */
735 #ifdef MODULE
736 #define loop_init init_module
737 MODULE_PARM(max_loop, "i");
738 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-255)");
739 #endif
740 
741 int loop_register_transfer(struct loop_func_table *funcs)
742 {
743         if ((unsigned)funcs->number > MAX_LO_CRYPT || xfer_funcs[funcs->number])
744                 return -EINVAL;
745         xfer_funcs[funcs->number] = funcs;
746         return 0; 
747 }
748 
749 int loop_unregister_transfer(int number)
750 {
751         struct loop_device *lo; 
752 
753         if ((unsigned)number >= MAX_LO_CRYPT)
754                 return -EINVAL; 
755         for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) { 
756                 int type = lo->lo_encrypt_type;
757                 if (type == number) { 
758                         xfer_funcs[type]->release(lo);
759                         lo->transfer = NULL; 
760                         lo->lo_encrypt_type = 0; 
761                 }
762         }
763         xfer_funcs[number] = NULL; 
764         return 0; 
765 }
766 
767 EXPORT_SYMBOL(loop_register_transfer);
768 EXPORT_SYMBOL(loop_unregister_transfer);
769 
770 static void no_plug_device(request_queue_t *q, kdev_t device)
771 {
772 }
773 
774 int __init loop_init(void) 
775 {
776         int     i;
777 
778         if (devfs_register_blkdev(MAJOR_NR, "loop", &lo_fops)) {
779                 printk(KERN_WARNING "Unable to get major number %d for loop device\n",
780                        MAJOR_NR);
781                 return -EIO;
782         }
783         devfs_handle = devfs_mk_dir (NULL, "loop", NULL);
784         devfs_register_series (devfs_handle, "%u", max_loop, DEVFS_FL_DEFAULT,
785                                MAJOR_NR, 0,
786                                S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
787                                &lo_fops, NULL);
788 
789         if ((max_loop < 1) || (max_loop > 255)) {
790                 printk (KERN_WARNING "loop: invalid max_loop (must be between 1 and 255), using default (8)\n");
791                 max_loop = 8;
792         }
793 
794         printk(KERN_INFO "loop: enabling %d loop devices\n", max_loop);
795 
796         loop_dev = kmalloc (max_loop * sizeof(struct loop_device), GFP_KERNEL);
797         if (!loop_dev) {
798                 printk (KERN_ERR "loop: Unable to create loop_dev\n");
799                 return -ENOMEM;
800         }
801 
802         loop_sizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
803         if (!loop_sizes) {
804                 printk (KERN_ERR "loop: Unable to create loop_sizes\n");
805                 kfree (loop_dev);
806                 return -ENOMEM;
807         }
808 
809         loop_blksizes = kmalloc (max_loop * sizeof(int), GFP_KERNEL);
810         if (!loop_blksizes) {
811                 printk (KERN_ERR "loop: Unable to create loop_blksizes\n");
812                 kfree (loop_dev);
813                 kfree (loop_sizes);
814                 return -ENOMEM;
815         }               
816 
817         blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
818         blk_queue_pluggable(BLK_DEFAULT_QUEUE(MAJOR_NR), no_plug_device);
819         blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR), 0);
820         for (i=0; i < max_loop; i++) {
821                 memset(&loop_dev[i], 0, sizeof(struct loop_device));
822                 loop_dev[i].lo_number = i;
823         }
824         memset(loop_sizes, 0, max_loop * sizeof(int));
825         memset(loop_blksizes, 0, max_loop * sizeof(int));
826         blk_size[MAJOR_NR] = loop_sizes;
827         blksize_size[MAJOR_NR] = loop_blksizes;
828         for (i=0; i < max_loop; i++)
829                 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &lo_fops, 0);
830 
831         return 0;
832 }
833 
834 #ifdef MODULE
835 void cleanup_module(void) 
836 {
837         devfs_unregister (devfs_handle);
838         if (devfs_unregister_blkdev(MAJOR_NR, "loop") != 0)
839                 printk(KERN_WARNING "loop: cannot unregister blkdev\n");
840 
841         blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
842         kfree (loop_dev);
843         kfree (loop_sizes);
844         kfree (loop_blksizes);
845 }
846 #endif
847 
848 #ifndef MODULE
849 static int __init max_loop_setup(char *str)
850 {
851         max_loop = simple_strtol(str,NULL,0);
852         return 1;
853 }
854 
855 __setup("max_loop=", max_loop_setup);
856 #endif
857 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.