1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/ioctl.h>
30 /* Grumble... The 2.6.x kernel breaks asm/posix_types.h
31 * so we get to try and cope as best we can... */
32 #include <linux/version.h>
33 #include <asm/posix_types.h>
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
36 #define __bb_kernel_dev_t __kernel_old_dev_t
37 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
38 #define __bb_kernel_dev_t __kernel_dev_t
40 #define __bb_kernel_dev_t unsigned short
43 /* Stuff stolen from linux/loop.h */
44 #define LO_NAME_SIZE 64
45 #define LO_KEY_SIZE 32
46 #define LOOP_SET_FD 0x4C00
47 #define LOOP_CLR_FD 0x4C01
48 #define LOOP_SET_STATUS 0x4C02
49 #define LOOP_GET_STATUS 0x4C03
52 __bb_kernel_dev_t lo_device;
53 unsigned long lo_inode;
54 __bb_kernel_dev_t lo_rdevice;
57 int lo_encrypt_key_size;
59 char lo_name[LO_NAME_SIZE];
60 unsigned char lo_encrypt_key[LO_KEY_SIZE];
61 unsigned long lo_init[2];
65 extern int del_loop(const char *device)
69 if ((fd = open(device, O_RDONLY)) < 0) {
70 bb_perror_msg("%s", device);
73 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
75 bb_perror_msg("ioctl: LOOP_CLR_FD");
82 extern int set_loop(const char *device, const char *file, int offset,
85 struct loop_info loopinfo;
88 mode = *loopro ? O_RDONLY : O_RDWR;
89 if ((ffd = open(file, mode)) < 0 && !*loopro
90 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
91 bb_perror_msg("%s", file);
94 if ((fd = open(device, mode)) < 0) {
96 bb_perror_msg("%s", device);
99 *loopro = (mode == O_RDONLY);
101 memset(&loopinfo, 0, sizeof(loopinfo));
102 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
104 loopinfo.lo_offset = offset;
106 loopinfo.lo_encrypt_key_size = 0;
107 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
108 bb_perror_msg("ioctl: LOOP_SET_FD");
113 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
114 (void) ioctl(fd, LOOP_CLR_FD, 0);
115 bb_perror_msg("ioctl: LOOP_SET_STATUS");
125 extern char *find_unused_loop_device(void)
130 struct loop_info loopinfo;
132 for (i = 0; i <= 7; i++) {
133 sprintf(dev, LOOP_FORMAT, i);
134 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
135 if ((fd = open(dev, O_RDONLY)) >= 0) {
136 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
137 if (errno == ENXIO) { /* probably free */
153 c-file-style: "linux"