2 * utils - miscellaneous device utilities for cryptsetup
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/resource.h>
32 unsigned crypt_getpagesize(void)
34 long r = sysconf(_SC_PAGESIZE);
35 return r < 0 ? DEFAULT_MEM_ALIGNMENT : r;
38 static int get_alignment(int fd)
40 int alignment = DEFAULT_MEM_ALIGNMENT;
42 #ifdef _PC_REC_XFER_ALIGN
43 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
45 alignment = DEFAULT_MEM_ALIGNMENT;
50 static void *aligned_malloc(void **base, int size, int alignment)
52 #ifdef HAVE_POSIX_MEMALIGN
53 return posix_memalign(base, alignment, size) ? NULL : *base;
55 /* Credits go to Michal's padlock patches for this alignment code */
58 ptr = malloc(size + alignment);
59 if(ptr == NULL) return NULL;
62 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
63 ptr += alignment - ((long)(ptr) & (alignment - 1));
69 ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
71 void *hangover_buf, *hangover_buf_base = NULL;
72 void *buf, *buf_base = NULL;
73 int r, hangover, solid, alignment;
76 if (fd == -1 || !orig_buf || bsize <= 0)
79 hangover = count % bsize;
80 solid = count - hangover;
81 alignment = get_alignment(fd);
83 if ((long)orig_buf & (alignment - 1)) {
84 buf = aligned_malloc(&buf_base, count, alignment);
87 memcpy(buf, orig_buf, count);
91 r = write(fd, buf, solid);
92 if (r < 0 || r != solid)
96 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
100 r = read(fd, hangover_buf, bsize);
101 if (r < 0 || r < hangover)
107 r = lseek(fd, -bsize, SEEK_CUR);
110 memcpy(hangover_buf, (char*)buf + solid, hangover);
112 r = write(fd, hangover_buf, bsize);
113 if (r < 0 || r < hangover)
118 free(hangover_buf_base);
124 ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
125 void *hangover_buf, *hangover_buf_base = NULL;
126 void *buf, *buf_base = NULL;
127 int r, hangover, solid, alignment;
130 if (fd == -1 || !orig_buf || bsize <= 0)
133 hangover = count % bsize;
134 solid = count - hangover;
135 alignment = get_alignment(fd);
137 if ((long)orig_buf & (alignment - 1)) {
138 buf = aligned_malloc(&buf_base, count, alignment);
144 r = read(fd, buf, solid);
145 if(r < 0 || r != solid)
149 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
152 r = read(fd, hangover_buf, bsize);
153 if (r < 0 || r < hangover)
156 memcpy((char *)buf + solid, hangover_buf, hangover);
160 free(hangover_buf_base);
161 if (buf != orig_buf) {
162 memcpy(orig_buf, buf, count);
169 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
170 * but we also need a function to deal with short writes at the start. But this information
171 * is implicitly included in the read/write offset, which can not be set to non-aligned
172 * boundaries. Hence, we combine llseek with write.
174 ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
176 void *frontPadBuf_base = NULL;
178 size_t innerCount = 0;
181 if (fd == -1 || !buf || bsize <= 0)
184 frontHang = offset % bsize;
186 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
190 frontPadBuf = aligned_malloc(&frontPadBuf_base,
191 bsize, get_alignment(fd));
195 r = read(fd, frontPadBuf, bsize);
196 if (r < 0 || r != bsize)
199 innerCount = bsize - frontHang;
200 if (innerCount > count)
203 memcpy(frontPadBuf + frontHang, buf, innerCount);
205 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
208 r = write(fd, frontPadBuf, bsize);
209 if (r < 0 || r != bsize)
216 ret = count ? write_blockwise(fd, bsize, buf, count) : 0;
220 free(frontPadBuf_base);
226 #define DEFAULT_PROCESS_PRIORITY -18
228 static int _priority;
229 static int _memlock_count = 0;
231 // return 1 if memory is locked
232 int crypt_memlock_inc(struct crypt_device *ctx)
234 if (!_memlock_count++) {
235 log_dbg("Locking memory.");
236 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
237 log_dbg("Cannot lock memory with mlockall.");
242 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
243 log_err(ctx, _("Cannot get process priority.\n"));
245 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
246 log_dbg("setpriority %d failed: %s",
247 DEFAULT_PROCESS_PRIORITY, strerror(errno));
249 return _memlock_count ? 1 : 0;
252 int crypt_memlock_dec(struct crypt_device *ctx)
254 if (_memlock_count && (!--_memlock_count)) {
255 log_dbg("Unlocking memory.");
256 if (munlockall() == -1)
257 log_err(ctx, _("Cannot unlock memory.\n"));
258 if (setpriority(PRIO_PROCESS, 0, _priority))
259 log_dbg("setpriority %d failed: %s", _priority, strerror(errno));
261 return _memlock_count ? 1 : 0;