blob: 24dab1ac0533b79a95bdddd69850bdbcc8289bfc [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>
Rob Landley53437472006-07-16 08:14:35 +00006 * Copyright (C) 2005 by Rob Landley <rob@landley.net>
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020011#include <linux/version.h>
12
13#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
Eric Andersenef8cd3b2004-02-06 07:16:36 +000014
Rob Landley6a6798b2005-08-10 20:35:54 +000015/* For 2.6, use the cleaned up header to get the 64 bit API. */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020016/* linux/loop.h relies on __u64. Make sure we have that as a proper type
17 * until userspace is widely fixed. */
18# if (defined __INTEL_COMPILER && !defined __GNUC__) \
19 || (defined __GNUC__ && defined __STRICT_ANSI__)
20__extension__ typedef long long __s64;
21__extension__ typedef unsigned long long __u64;
22# endif
23# include <linux/loop.h>
Rob Landley6a6798b2005-08-10 20:35:54 +000024typedef struct loop_info64 bb_loop_info;
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020025# define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
26# define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
27
28#else
Eric Andersenef8cd3b2004-02-06 07:16:36 +000029
Rob Landley6a6798b2005-08-10 20:35:54 +000030/* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020031/* Stuff stolen from linux/loop.h for 2.4 and earlier kernels */
32# include <linux/posix_types.h>
33# define LO_NAME_SIZE 64
34# define LO_KEY_SIZE 32
35# define LOOP_SET_FD 0x4C00
36# define LOOP_CLR_FD 0x4C01
37# define BB_LOOP_SET_STATUS 0x4C02
38# define BB_LOOP_GET_STATUS 0x4C03
Rob Landley6a6798b2005-08-10 20:35:54 +000039typedef struct {
Eric Andersenef8cd3b2004-02-06 07:16:36 +000040 int lo_number;
Rob Landley6a6798b2005-08-10 20:35:54 +000041 __kernel_dev_t lo_device;
Eric Andersenef8cd3b2004-02-06 07:16:36 +000042 unsigned long lo_inode;
Rob Landley6a6798b2005-08-10 20:35:54 +000043 __kernel_dev_t lo_rdevice;
Eric Andersenef8cd3b2004-02-06 07:16:36 +000044 int lo_offset;
45 int lo_encrypt_type;
46 int lo_encrypt_key_size;
47 int lo_flags;
Rob Landley6a6798b2005-08-10 20:35:54 +000048 char lo_file_name[LO_NAME_SIZE];
Eric Andersenef8cd3b2004-02-06 07:16:36 +000049 unsigned char lo_encrypt_key[LO_KEY_SIZE];
50 unsigned long lo_init[2];
51 char reserved[4];
Rob Landley6a6798b2005-08-10 20:35:54 +000052} bb_loop_info;
53#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000054
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000055char* FAST_FUNC query_loop(const char *device)
Eric Andersenaad1a882001-03-16 22:47:14 +000056{
Rob Landley1d589b22005-11-29 23:47:10 +000057 int fd;
58 bb_loop_info loopinfo;
Denis Vlasenko13858992006-10-08 12:49:22 +000059 char *dev = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Denis Vlasenko13858992006-10-08 12:49:22 +000061 fd = open(device, O_RDONLY);
62 if (fd < 0) return 0;
Rob Landley1d589b22005-11-29 23:47:10 +000063 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo))
Denis Vlasenko13858992006-10-08 12:49:22 +000064 dev = xasprintf("%ld %s", (long) loopinfo.lo_offset,
Eric Andersen76b24272006-01-30 17:30:22 +000065 (char *)loopinfo.lo_file_name);
Rob Landley1d589b22005-11-29 23:47:10 +000066 close(fd);
Eric Andersenaad1a882001-03-16 22:47:14 +000067
Rob Landley1d589b22005-11-29 23:47:10 +000068 return dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000069}
Rob Landley1d589b22005-11-29 23:47:10 +000070
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000071int FAST_FUNC del_loop(const char *device)
Rob Landley1d589b22005-11-29 23:47:10 +000072{
73 int fd, rc;
74
Denis Vlasenko13858992006-10-08 12:49:22 +000075 fd = open(device, O_RDONLY);
76 if (fd < 0) return 1;
77 rc = ioctl(fd, LOOP_CLR_FD, 0);
Rob Landley1d589b22005-11-29 23:47:10 +000078 close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000079
Rob Landley6a6798b2005-08-10 20:35:54 +000080 return rc;
Eric Andersenaad1a882001-03-16 22:47:14 +000081}
82
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000083/* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
84 *device is loop device to use, or if *device==NULL finds a loop device to
85 mount it on and sets *device to a strdup of that loop device name. This
86 search will re-use an existing loop device already bound to that
87 file/offset if it finds one.
88 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000089int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset)
Eric Andersenaad1a882001-03-16 22:47:14 +000090{
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +000091 char dev[LOOP_NAMESIZE];
92 char *try;
Rob Landley6a6798b2005-08-10 20:35:54 +000093 bb_loop_info loopinfo;
Eric Andersenaad1a882001-03-16 22:47:14 +000094 struct stat statbuf;
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000095 int i, dfd, ffd, mode, rc = -1;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000096
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +000097 /* Open the file. Barf if this doesn't work. */
Denis Vlasenko13858992006-10-08 12:49:22 +000098 mode = O_RDWR;
99 ffd = open(file, mode);
100 if (ffd < 0) {
101 mode = O_RDONLY;
102 ffd = open(file, mode);
103 if (ffd < 0)
104 return -errno;
105 }
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000106
107 /* Find a loop device. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000108 try = *device ? : dev;
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000109 for (i = 0; rc; i++) {
Rob Landleyb70ccd92006-01-22 23:17:18 +0000110 sprintf(dev, LOOP_FORMAT, i);
Rob Landleyaae8b342006-03-18 02:38:10 +0000111
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000112 /* Ran out of block devices, return failure. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000113 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000114 rc = -ENOENT;
Rob Landley6a6798b2005-08-10 20:35:54 +0000115 break;
Eric Andersenaad1a882001-03-16 22:47:14 +0000116 }
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000117 /* Open the sucker and check its loopiness. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000118 dfd = open(try, mode);
119 if (dfd < 0 && errno == EROFS) {
120 mode = O_RDONLY;
121 dfd = open(try, mode);
122 }
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000123 if (dfd < 0)
124 goto try_again;
Eric Andersenaad1a882001-03-16 22:47:14 +0000125
Denis Vlasenko13858992006-10-08 12:49:22 +0000126 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
Rob Landley1d589b22005-11-29 23:47:10 +0000127
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000128 /* If device is free, claim it. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000129 if (rc && errno == ENXIO) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000130 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen76b24272006-01-30 17:30:22 +0000131 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
Rob Landley6a6798b2005-08-10 20:35:54 +0000132 loopinfo.lo_offset = offset;
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000133 /* Associate free loop device with file. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000134 if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000135 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))
136 rc = 0;
137 else
138 ioctl(dfd, LOOP_CLR_FD, 0);
Rob Landley934da822006-06-25 15:29:12 +0000139 }
Rob Landleyaae8b342006-03-18 02:38:10 +0000140
Bernhard Reutner-Fischer94c33312005-10-15 14:13:09 +0000141 /* If this block device already set up right, re-use it.
142 (Yes this is racy, but associating two loop devices with the same
143 file isn't pretty either. In general, mounting the same file twice
144 without using losetup manually is problematic.)
145 */
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000146 } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
Denis Vlasenko7039a662006-10-08 17:54:47 +0000147 || offset != loopinfo.lo_offset) {
148 rc = -1;
149 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000150 close(dfd);
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +0000151 try_again:
Denis Vlasenko13858992006-10-08 12:49:22 +0000152 if (*device) break;
Rob Landley6a6798b2005-08-10 20:35:54 +0000153 }
154 close(ffd);
Denis Vlasenko13858992006-10-08 12:49:22 +0000155 if (!rc) {
Denis Vlasenkoc34d3552007-04-19 00:09:34 +0000156 if (!*device)
157 *device = xstrdup(dev);
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +0000158 return (mode == O_RDONLY); /* 1:ro, 0:rw */
Denis Vlasenko13858992006-10-08 12:49:22 +0000159 }
160 return rc;
Rob Landley6a6798b2005-08-10 20:35:54 +0000161}