blob: 81dfca4d1ec10d41702e4c34e233d2a8ba19a425 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Bernhard Reutner-Fischer7547a6e2005-10-15 20:56:31 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Rob Landley6a6798b2005-08-10 20:35:54 +000010
Eric Andersen1e4dc962005-01-04 20:37:55 +000011#include <features.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include <stdio.h>
13#include <errno.h>
14#include <fcntl.h>
15#include <string.h>
16#include <unistd.h>
17#include <sys/ioctl.h>
18#include "libbb.h"
Eric Andersenef8cd3b2004-02-06 07:16:36 +000019
Rob Landley6a6798b2005-08-10 20:35:54 +000020/* For 2.6, use the cleaned up header to get the 64 bit API. */
Eric Andersenef8cd3b2004-02-06 07:16:36 +000021#include <linux/version.h>
Eric Andersencf6ef052004-08-16 08:29:44 +000022#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
Rob Landley6a6798b2005-08-10 20:35:54 +000023#include <linux/loop.h>
24typedef struct loop_info64 bb_loop_info;
25#define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
26#define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
Eric Andersenef8cd3b2004-02-06 07:16:36 +000027
Rob Landley6a6798b2005-08-10 20:35:54 +000028/* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
29#else
30/* Stuff stolen from linux/loop.h for 2.4 and earlier kernels*/
31#include <linux/posix_types.h>
Eric Andersenef8cd3b2004-02-06 07:16:36 +000032#define LO_NAME_SIZE 64
33#define LO_KEY_SIZE 32
34#define LOOP_SET_FD 0x4C00
35#define LOOP_CLR_FD 0x4C01
Rob Landley6a6798b2005-08-10 20:35:54 +000036#define BB_LOOP_SET_STATUS 0x4C02
37#define BB_LOOP_GET_STATUS 0x4C03
38typedef struct {
Eric Andersenef8cd3b2004-02-06 07:16:36 +000039 int lo_number;
Rob Landley6a6798b2005-08-10 20:35:54 +000040 __kernel_dev_t lo_device;
Eric Andersenef8cd3b2004-02-06 07:16:36 +000041 unsigned long lo_inode;
Rob Landley6a6798b2005-08-10 20:35:54 +000042 __kernel_dev_t lo_rdevice;
Eric Andersenef8cd3b2004-02-06 07:16:36 +000043 int lo_offset;
44 int lo_encrypt_type;
45 int lo_encrypt_key_size;
46 int lo_flags;
Rob Landley6a6798b2005-08-10 20:35:54 +000047 char lo_file_name[LO_NAME_SIZE];
Eric Andersenef8cd3b2004-02-06 07:16:36 +000048 unsigned char lo_encrypt_key[LO_KEY_SIZE];
49 unsigned long lo_init[2];
50 char reserved[4];
Rob Landley6a6798b2005-08-10 20:35:54 +000051} bb_loop_info;
52#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000053
Rob Landley1d589b22005-11-29 23:47:10 +000054char *query_loop(const char *device)
Eric Andersenaad1a882001-03-16 22:47:14 +000055{
Rob Landley1d589b22005-11-29 23:47:10 +000056 int fd;
57 bb_loop_info loopinfo;
58 char *dev=0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000059
Rob Landley1d589b22005-11-29 23:47:10 +000060 if ((fd = open(device, O_RDONLY)) < 0) return 0;
61 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo))
62 dev=bb_xasprintf("%ld %s", (long) loopinfo.lo_offset,
Eric Andersen76b24272006-01-30 17:30:22 +000063 (char *)loopinfo.lo_file_name);
Rob Landley1d589b22005-11-29 23:47:10 +000064 close(fd);
Eric Andersenaad1a882001-03-16 22:47:14 +000065
Rob Landley1d589b22005-11-29 23:47:10 +000066 return dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000067}
Rob Landley1d589b22005-11-29 23:47:10 +000068
69
70int del_loop(const char *device)
71{
72 int fd, rc;
73
74 if ((fd = open(device, O_RDONLY)) < 0) return 1;
75 rc=ioctl(fd, LOOP_CLR_FD, 0);
76 close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000077
Rob Landley6a6798b2005-08-10 20:35:54 +000078 return rc;
Eric Andersenaad1a882001-03-16 22:47:14 +000079}
80
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000081/* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
82 *device is loop device to use, or if *device==NULL finds a loop device to
83 mount it on and sets *device to a strdup of that loop device name. This
84 search will re-use an existing loop device already bound to that
85 file/offset if it finds one.
86 */
Rob Landley1d589b22005-11-29 23:47:10 +000087int set_loop(char **device, const char *file, int offset)
Eric Andersenaad1a882001-03-16 22:47:14 +000088{
Rob Landley1d589b22005-11-29 23:47:10 +000089 char dev[20], *try;
Rob Landley6a6798b2005-08-10 20:35:54 +000090 bb_loop_info loopinfo;
Eric Andersenaad1a882001-03-16 22:47:14 +000091 struct stat statbuf;
Rob Landley90854672005-12-21 16:53:57 +000092 int i, dfd, ffd, mode, rc=-1;
Eric Andersenaad1a882001-03-16 22:47:14 +000093
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000094 /* Open the file. Barf if this doesn't work. */
Rob Landleyff567f72005-10-11 07:26:15 +000095 if((ffd = open(file, mode=O_RDWR))<0 && (ffd = open(file,mode=O_RDONLY))<0)
96 return errno;
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000097
98 /* Find a loop device. */
Rob Landley1d589b22005-11-29 23:47:10 +000099 try=*device ? : dev;
Rob Landley6a6798b2005-08-10 20:35:54 +0000100 for(i=0;rc;i++) {
Rob Landleyb70ccd92006-01-22 23:17:18 +0000101 sprintf(dev, LOOP_FORMAT, i);
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000102 /* Ran out of block devices, return failure. */
Rob Landley1d589b22005-11-29 23:47:10 +0000103 if(stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000104 rc=ENOENT;
105 break;
Eric Andersenaad1a882001-03-16 22:47:14 +0000106 }
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000107 /* Open the sucker and check its loopiness. */
Rob Landley1d589b22005-11-29 23:47:10 +0000108 if((dfd=open(try, mode))<0 && errno==EROFS)
109 dfd=open(try,mode=O_RDONLY);
Rob Landley90854672005-12-21 16:53:57 +0000110 if(dfd<0) goto try_again;
Eric Andersenaad1a882001-03-16 22:47:14 +0000111
Rob Landley6a6798b2005-08-10 20:35:54 +0000112 rc=ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
Rob Landley1d589b22005-11-29 23:47:10 +0000113
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000114 /* If device free, claim it. */
Rob Landley6a6798b2005-08-10 20:35:54 +0000115 if(rc && errno==ENXIO) {
116 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen76b24272006-01-30 17:30:22 +0000117 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
Rob Landley6a6798b2005-08-10 20:35:54 +0000118 loopinfo.lo_offset = offset;
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000119 /* Associate free loop device with file. */
Rob Landley6a6798b2005-08-10 20:35:54 +0000120 if(!ioctl(dfd, LOOP_SET_FD, ffd) &&
121 !ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0;
122 else ioctl(dfd, LOOP_CLR_FD, 0);
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000123 /* If this block device already set up right, re-use it.
124 (Yes this is racy, but associating two loop devices with the same
125 file isn't pretty either. In general, mounting the same file twice
126 without using losetup manually is problematic.)
127 */
Eric Andersen76b24272006-01-30 17:30:22 +0000128 } else if(strcmp(file,(char *)loopinfo.lo_file_name)
Rob Landley1d589b22005-11-29 23:47:10 +0000129 || offset!=loopinfo.lo_offset) rc=-1;
Rob Landley6a6798b2005-08-10 20:35:54 +0000130 close(dfd);
Rob Landley90854672005-12-21 16:53:57 +0000131try_again:
Rob Landley6a6798b2005-08-10 20:35:54 +0000132 if(*device) break;
133 }
134 close(ffd);
135 if(!rc) {
136 if(!*device) *device=strdup(dev);
137 return mode==O_RDONLY ? 1 : 0;
138 } else return rc;
139}
Eric Andersenaad1a882001-03-16 22:47:14 +0000140
141/* END CODE */
142/*
143Local Variables:
144c-file-style: "linux"
145c-basic-offset: 4
146tab-width: 4
147End:
148*/