blob: 10217931b7090d5c768b55b50a53cfe52d93fc92 [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
54extern int del_loop(const char *device)
55{
Rob Landley6a6798b2005-08-10 20:35:54 +000056 int fd,rc=0;
Eric Andersenaad1a882001-03-16 22:47:14 +000057
Rob Landley6a6798b2005-08-10 20:35:54 +000058 if ((fd = open(device, O_RDONLY)) < 0) rc=1;
59 else {
60 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) rc=1;
Eric Andersen7b3edeb2003-05-02 16:25:01 +000061 close(fd);
Eric Andersenaad1a882001-03-16 22:47:14 +000062 }
Rob Landley6a6798b2005-08-10 20:35:54 +000063 return rc;
Eric Andersenaad1a882001-03-16 22:47:14 +000064}
65
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000066/* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
67 *device is loop device to use, or if *device==NULL finds a loop device to
68 mount it on and sets *device to a strdup of that loop device name. This
69 search will re-use an existing loop device already bound to that
70 file/offset if it finds one.
71 */
Rob Landley6a6798b2005-08-10 20:35:54 +000072extern int set_loop(char **device, const char *file, int offset)
Eric Andersenaad1a882001-03-16 22:47:14 +000073{
74 char dev[20];
Rob Landley6a6798b2005-08-10 20:35:54 +000075 bb_loop_info loopinfo;
Eric Andersenaad1a882001-03-16 22:47:14 +000076 struct stat statbuf;
Rob Landley6a6798b2005-08-10 20:35:54 +000077 int i, dfd, ffd, mode, rc=1;
Eric Andersenaad1a882001-03-16 22:47:14 +000078
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000079 /* Open the file. Barf if this doesn't work. */
Rob Landleyff567f72005-10-11 07:26:15 +000080 if((ffd = open(file, mode=O_RDWR))<0 && (ffd = open(file,mode=O_RDONLY))<0)
81 return errno;
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000082
83 /* Find a loop device. */
Rob Landley6a6798b2005-08-10 20:35:54 +000084 for(i=0;rc;i++) {
85 sprintf(dev, LOOP_FORMAT, i++);
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000086 /* Ran out of block devices, return failure. */
87 if(stat(*device ? *device : dev, &statbuf) ||
88 !S_ISBLK(statbuf.st_mode)) {
Rob Landley6a6798b2005-08-10 20:35:54 +000089 rc=ENOENT;
90 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000091 }
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000092 /* Open the sucker and check its loopiness. */
Rob Landley6a6798b2005-08-10 20:35:54 +000093 if((dfd=open(dev, mode))<0 && errno==EROFS)
94 dfd=open(dev,mode=O_RDONLY);
95 if(dfd<0) continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000096
Rob Landley6a6798b2005-08-10 20:35:54 +000097 rc=ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000098 /* If device free, claim it. */
Rob Landley6a6798b2005-08-10 20:35:54 +000099 if(rc && errno==ENXIO) {
100 memset(&loopinfo, 0, sizeof(loopinfo));
101 safe_strncpy(loopinfo.lo_file_name, file, LO_NAME_SIZE);
102 loopinfo.lo_offset = offset;
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000103 /* Associate free loop device with file. */
Rob Landley6a6798b2005-08-10 20:35:54 +0000104 if(!ioctl(dfd, LOOP_SET_FD, ffd) &&
105 !ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0;
106 else ioctl(dfd, LOOP_CLR_FD, 0);
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000107 /* If this block device already set up right, re-use it.
108 (Yes this is racy, but associating two loop devices with the same
109 file isn't pretty either. In general, mounting the same file twice
110 without using losetup manually is problematic.)
111 */
Rob Landley6a6798b2005-08-10 20:35:54 +0000112 } else if(strcmp(file,loopinfo.lo_file_name)
113 || offset!=loopinfo.lo_offset) rc=1;
114 close(dfd);
115 if(*device) break;
116 }
117 close(ffd);
118 if(!rc) {
119 if(!*device) *device=strdup(dev);
120 return mode==O_RDONLY ? 1 : 0;
121 } else return rc;
122}
Eric Andersenaad1a882001-03-16 22:47:14 +0000123
124/* END CODE */
125/*
126Local Variables:
127c-file-style: "linux"
128c-basic-offset: 4
129tab-width: 4
130End:
131*/