Lines Matching refs:lo
90 static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf, in transfer_none() argument
103 static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf, in transfer_xor() argument
117 key = lo->lo_encrypt_key; in transfer_xor()
118 keysize = lo->lo_encrypt_key_size; in transfer_xor()
124 static int none_status(struct loop_device *lo, struct loop_info *info) in none_status() argument
126 lo->lo_flags |= LO_FLAGS_BH_REMAP; in none_status()
130 static int xor_status(struct loop_device *lo, struct loop_info *info) in xor_status() argument
157 static int compute_loop_size(struct loop_device *lo, struct dentry * lo_dentry, kdev_t lodev) in compute_loop_size() argument
160 return (lo_dentry->d_inode->i_size - lo->lo_offset) >> BLOCK_SIZE_BITS; in compute_loop_size()
163 (lo->lo_offset >> BLOCK_SIZE_BITS); in compute_loop_size()
167 static void figure_loop_size(struct loop_device *lo) in figure_loop_size() argument
169 loop_sizes[lo->lo_number] = compute_loop_size(lo, in figure_loop_size()
170 lo->lo_backing_file->f_dentry, in figure_loop_size()
171 lo->lo_device); in figure_loop_size()
174 static int lo_send(struct loop_device *lo, struct buffer_head *bh, int bsize, in lo_send() argument
177 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */ in lo_send()
206 transfer_result = lo_do_transfer(lo, WRITE, kaddr + offset, data, size, IV); in lo_send()
241 struct loop_device *lo; member
251 struct loop_device *lo = p->lo; in lo_read_actor() local
258 if (lo_do_transfer(lo, READ, kaddr + offset, p->data, size, IV)) { in lo_read_actor()
271 static int lo_receive(struct loop_device *lo, struct buffer_head *bh, int bsize, in lo_receive() argument
278 cookie.lo = lo; in lo_receive()
285 spin_lock_irq(&lo->lo_lock); in lo_receive()
286 file = lo->lo_backing_file; in lo_receive()
287 spin_unlock_irq(&lo->lo_lock); in lo_receive()
292 static inline int loop_get_bs(struct loop_device *lo) in loop_get_bs() argument
296 if (blksize_size[MAJOR(lo->lo_device)]) in loop_get_bs()
297 bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)]; in loop_get_bs()
304 static inline unsigned long loop_get_iv(struct loop_device *lo, in loop_get_iv() argument
307 int bs = loop_get_bs(lo); in loop_get_iv()
310 IV = sector / (bs >> 9) + lo->lo_offset / bs; in loop_get_iv()
311 offset = ((sector % (bs >> 9)) << 9) + lo->lo_offset % bs; in loop_get_iv()
318 static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw) in do_bh_filebacked() argument
323 pos = ((loff_t) bh->b_rsector << 9) + lo->lo_offset; in do_bh_filebacked()
326 ret = lo_send(lo, bh, loop_get_bs(lo), pos); in do_bh_filebacked()
328 ret = lo_receive(lo, bh, loop_get_bs(lo), pos); in do_bh_filebacked()
348 static void loop_add_bh(struct loop_device *lo, struct buffer_head *bh) in loop_add_bh() argument
352 spin_lock_irqsave(&lo->lo_lock, flags); in loop_add_bh()
353 if (lo->lo_bhtail) { in loop_add_bh()
354 lo->lo_bhtail->b_reqnext = bh; in loop_add_bh()
355 lo->lo_bhtail = bh; in loop_add_bh()
357 lo->lo_bh = lo->lo_bhtail = bh; in loop_add_bh()
358 spin_unlock_irqrestore(&lo->lo_lock, flags); in loop_add_bh()
360 up(&lo->lo_bh_mutex); in loop_add_bh()
366 static struct buffer_head *loop_get_bh(struct loop_device *lo) in loop_get_bh() argument
370 spin_lock_irq(&lo->lo_lock); in loop_get_bh()
371 if ((bh = lo->lo_bh)) { in loop_get_bh()
372 if (bh == lo->lo_bhtail) in loop_get_bh()
373 lo->lo_bhtail = NULL; in loop_get_bh()
374 lo->lo_bh = bh->b_reqnext; in loop_get_bh()
377 spin_unlock_irq(&lo->lo_lock); in loop_get_bh()
391 struct loop_device *lo = &loop_dev[MINOR(bh->b_dev)]; in loop_end_io_transfer() local
397 if (atomic_dec_and_test(&lo->lo_pending)) in loop_end_io_transfer()
398 up(&lo->lo_bh_mutex); in loop_end_io_transfer()
401 loop_add_bh(lo, bh); in loop_end_io_transfer()
404 static struct buffer_head *loop_get_buffer(struct loop_device *lo, in loop_get_buffer() argument
412 if (lo->lo_flags & LO_FLAGS_BH_REMAP) { in loop_get_buffer()
453 bh->b_rsector = rbh->b_rsector + (lo->lo_offset >> 9); in loop_get_buffer()
454 spin_lock_irq(&lo->lo_lock); in loop_get_buffer()
455 bh->b_rdev = lo->lo_device; in loop_get_buffer()
456 spin_unlock_irq(&lo->lo_lock); in loop_get_buffer()
464 struct loop_device *lo; in loop_make_request() local
473 lo = &loop_dev[MINOR(rbh->b_rdev)]; in loop_make_request()
474 spin_lock_irq(&lo->lo_lock); in loop_make_request()
475 if (lo->lo_state != Lo_bound) in loop_make_request()
477 atomic_inc(&lo->lo_pending); in loop_make_request()
478 spin_unlock_irq(&lo->lo_lock); in loop_make_request()
481 if (lo->lo_flags & LO_FLAGS_READ_ONLY) in loop_make_request()
495 if (lo->lo_flags & LO_FLAGS_DO_BMAP) { in loop_make_request()
502 loop_add_bh(lo, rbh); in loop_make_request()
509 bh = loop_get_buffer(lo, rbh); in loop_make_request()
510 IV = loop_get_iv(lo, rbh->b_rsector); in loop_make_request()
513 if (lo_do_transfer(lo, WRITE, bh->b_data, rbh->b_data, in loop_make_request()
522 if (atomic_dec_and_test(&lo->lo_pending)) in loop_make_request()
523 up(&lo->lo_bh_mutex); in loop_make_request()
529 spin_unlock_irq(&lo->lo_lock); in loop_make_request()
533 static inline void loop_handle_bh(struct loop_device *lo,struct buffer_head *bh) in loop_handle_bh() argument
540 if (lo->lo_flags & LO_FLAGS_DO_BMAP) { in loop_handle_bh()
543 ret = do_bh_filebacked(lo, bh, rw); in loop_handle_bh()
547 unsigned long IV = loop_get_iv(lo, rbh->b_rsector); in loop_handle_bh()
549 ret = lo_do_transfer(lo, READ, bh->b_data, rbh->b_data, in loop_handle_bh()
565 struct loop_device *lo = data; in loop_thread() local
572 sprintf(current->comm, "loop%d", lo->lo_number); in loop_thread()
579 spin_lock_irq(&lo->lo_lock); in loop_thread()
580 lo->lo_state = Lo_bound; in loop_thread()
581 atomic_inc(&lo->lo_pending); in loop_thread()
582 spin_unlock_irq(&lo->lo_lock); in loop_thread()
589 up(&lo->lo_sem); in loop_thread()
592 down_interruptible(&lo->lo_bh_mutex); in loop_thread()
597 if (!atomic_read(&lo->lo_pending)) in loop_thread()
600 bh = loop_get_bh(lo); in loop_thread()
605 loop_handle_bh(lo, bh); in loop_thread()
611 if (atomic_dec_and_test(&lo->lo_pending)) in loop_thread()
615 up(&lo->lo_sem); in loop_thread()
619 static int loop_set_fd(struct loop_device *lo, struct file *lo_file, kdev_t dev, in loop_set_fd() argument
632 if (lo->lo_state != Lo_unbound) in loop_set_fd()
678 lo->lo_device = lo_device; in loop_set_fd()
679 lo->lo_flags = lo_flags; in loop_set_fd()
680 lo->lo_backing_file = file; in loop_set_fd()
681 lo->transfer = NULL; in loop_set_fd()
682 lo->ioctl = NULL; in loop_set_fd()
683 figure_loop_size(lo); in loop_set_fd()
684 lo->old_gfp_mask = inode->i_mapping->gfp_mask; in loop_set_fd()
695 lo->lo_bh = lo->lo_bhtail = NULL; in loop_set_fd()
696 error = kernel_thread(loop_thread, lo, in loop_set_fd()
700 down(&lo->lo_sem); /* wait for the thread to start */ in loop_set_fd()
706 lo->lo_backing_file = NULL; in loop_set_fd()
707 lo->lo_device = 0; in loop_set_fd()
708 lo->lo_flags = 0; in loop_set_fd()
709 loop_sizes[lo->lo_number] = 0; in loop_set_fd()
710 inode->i_mapping->gfp_mask = lo->old_gfp_mask; in loop_set_fd()
711 lo->lo_state = Lo_unbound; in loop_set_fd()
720 static int loop_release_xfer(struct loop_device *lo) in loop_release_xfer() argument
723 if (lo->lo_encrypt_type) { in loop_release_xfer()
724 struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type]; in loop_release_xfer()
726 err = xfer->release(lo); in loop_release_xfer()
728 xfer->unlock(lo); in loop_release_xfer()
729 lo->lo_encrypt_type = 0; in loop_release_xfer()
734 static int loop_init_xfer(struct loop_device *lo, int type,struct loop_info *i) in loop_init_xfer() argument
740 err = xfer->init(lo, i); in loop_init_xfer()
742 lo->lo_encrypt_type = type; in loop_init_xfer()
744 xfer->lock(lo); in loop_init_xfer()
750 static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) in loop_clr_fd() argument
752 struct file *filp = lo->lo_backing_file; in loop_clr_fd()
753 int gfp = lo->old_gfp_mask; in loop_clr_fd()
755 if (lo->lo_state != Lo_bound) in loop_clr_fd()
757 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ in loop_clr_fd()
762 spin_lock_irq(&lo->lo_lock); in loop_clr_fd()
763 lo->lo_state = Lo_rundown; in loop_clr_fd()
764 if (atomic_dec_and_test(&lo->lo_pending)) in loop_clr_fd()
765 up(&lo->lo_bh_mutex); in loop_clr_fd()
766 spin_unlock_irq(&lo->lo_lock); in loop_clr_fd()
768 down(&lo->lo_sem); in loop_clr_fd()
770 lo->lo_backing_file = NULL; in loop_clr_fd()
772 loop_release_xfer(lo); in loop_clr_fd()
773 lo->transfer = NULL; in loop_clr_fd()
774 lo->ioctl = NULL; in loop_clr_fd()
775 lo->lo_device = 0; in loop_clr_fd()
776 lo->lo_encrypt_type = 0; in loop_clr_fd()
777 lo->lo_offset = 0; in loop_clr_fd()
778 lo->lo_encrypt_key_size = 0; in loop_clr_fd()
779 lo->lo_flags = 0; in loop_clr_fd()
780 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); in loop_clr_fd()
781 memset(lo->lo_name, 0, LO_NAME_SIZE); in loop_clr_fd()
782 loop_sizes[lo->lo_number] = 0; in loop_clr_fd()
785 lo->lo_state = Lo_unbound; in loop_clr_fd()
791 static int loop_set_status(struct loop_device *lo, struct loop_info *arg) in loop_set_status() argument
797 if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid && in loop_set_status()
800 if (lo->lo_state != Lo_bound) in loop_set_status()
811 err = loop_release_xfer(lo); in loop_set_status()
813 err = loop_init_xfer(lo, type, &info); in loop_set_status()
817 lo->lo_offset = info.lo_offset; in loop_set_status()
818 strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE); in loop_set_status()
820 lo->transfer = xfer_funcs[type]->transfer; in loop_set_status()
821 lo->ioctl = xfer_funcs[type]->ioctl; in loop_set_status()
822 lo->lo_encrypt_key_size = info.lo_encrypt_key_size; in loop_set_status()
823 lo->lo_init[0] = info.lo_init[0]; in loop_set_status()
824 lo->lo_init[1] = info.lo_init[1]; in loop_set_status()
826 memcpy(lo->lo_encrypt_key, info.lo_encrypt_key, in loop_set_status()
828 lo->lo_key_owner = current->uid; in loop_set_status()
830 figure_loop_size(lo); in loop_set_status()
834 static int loop_get_status(struct loop_device *lo, struct loop_info *arg) in loop_get_status() argument
837 struct file *file = lo->lo_backing_file; in loop_get_status()
839 if (lo->lo_state != Lo_bound) in loop_get_status()
844 info.lo_number = lo->lo_number; in loop_get_status()
847 info.lo_rdevice = kdev_t_to_nr(lo->lo_device); in loop_get_status()
848 info.lo_offset = lo->lo_offset; in loop_get_status()
849 info.lo_flags = lo->lo_flags; in loop_get_status()
850 strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE); in loop_get_status()
851 info.lo_encrypt_type = lo->lo_encrypt_type; in loop_get_status()
852 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { in loop_get_status()
853 info.lo_encrypt_key_size = lo->lo_encrypt_key_size; in loop_get_status()
854 memcpy(info.lo_encrypt_key, lo->lo_encrypt_key, in loop_get_status()
855 lo->lo_encrypt_key_size); in loop_get_status()
863 struct loop_device *lo; in lo_ioctl() local
876 lo = &loop_dev[dev]; in lo_ioctl()
877 down(&lo->lo_ctl_mutex); in lo_ioctl()
880 err = loop_set_fd(lo, file, inode->i_rdev, arg); in lo_ioctl()
883 err = loop_clr_fd(lo, inode->i_bdev); in lo_ioctl()
886 err = loop_set_status(lo, (struct loop_info *) arg); in lo_ioctl()
889 err = loop_get_status(lo, (struct loop_info *) arg); in lo_ioctl()
892 if (lo->lo_state != Lo_bound) { in lo_ioctl()
896 err = put_user((unsigned long)loop_sizes[lo->lo_number] << 1, (unsigned long *) arg); in lo_ioctl()
899 if (lo->lo_state != Lo_bound) { in lo_ioctl()
903 err = put_user((u64)loop_sizes[lo->lo_number] << 10, (u64*)arg); in lo_ioctl()
911 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; in lo_ioctl()
913 up(&lo->lo_ctl_mutex); in lo_ioctl()
919 struct loop_device *lo; in lo_open() local
932 lo = &loop_dev[dev]; in lo_open()
934 down(&lo->lo_ctl_mutex); in lo_open()
936 type = lo->lo_encrypt_type; in lo_open()
938 xfer_funcs[type]->lock(lo); in lo_open()
939 lo->lo_refcnt++; in lo_open()
940 up(&lo->lo_ctl_mutex); in lo_open()
946 struct loop_device *lo; in lo_release() local
960 lo = &loop_dev[dev]; in lo_release()
961 down(&lo->lo_ctl_mutex); in lo_release()
962 type = lo->lo_encrypt_type; in lo_release()
963 --lo->lo_refcnt; in lo_release()
965 xfer_funcs[type]->unlock(lo); in lo_release()
967 up(&lo->lo_ctl_mutex); in lo_release()
996 struct loop_device *lo; in loop_unregister_transfer() local
1000 for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) { in loop_unregister_transfer()
1001 int type = lo->lo_encrypt_type; in loop_unregister_transfer()
1003 xfer_funcs[type]->release(lo); in loop_unregister_transfer()
1004 lo->transfer = NULL; in loop_unregister_transfer()
1005 lo->lo_encrypt_type = 0; in loop_unregister_transfer()
1047 struct loop_device *lo = &loop_dev[i]; in loop_init() local
1048 memset(lo, 0, sizeof(struct loop_device)); in loop_init()
1049 init_MUTEX(&lo->lo_ctl_mutex); in loop_init()
1050 init_MUTEX_LOCKED(&lo->lo_sem); in loop_init()
1051 init_MUTEX_LOCKED(&lo->lo_bh_mutex); in loop_init()
1052 lo->lo_number = i; in loop_init()
1053 spin_lock_init(&lo->lo_lock); in loop_init()