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.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
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
15 * GNU 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/resource.h>
31 unsigned crypt_getpagesize(void)
33 long r = sysconf(_SC_PAGESIZE);
34 return r < 0 ? DEFAULT_MEM_ALIGNMENT : r;
37 static int get_alignment(int fd)
39 int alignment = DEFAULT_MEM_ALIGNMENT;
41 #ifdef _PC_REC_XFER_ALIGN
42 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
44 alignment = DEFAULT_MEM_ALIGNMENT;
49 static void *aligned_malloc(void **base, int size, int alignment)
51 #ifdef HAVE_POSIX_MEMALIGN
52 return posix_memalign(base, alignment, size) ? NULL : *base;
54 /* Credits go to Michal's padlock patches for this alignment code */
57 ptr = malloc(size + alignment);
58 if(ptr == NULL) return NULL;
61 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
62 ptr += alignment - ((long)(ptr) & (alignment - 1));
68 ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
70 void *hangover_buf, *hangover_buf_base = NULL;
71 void *buf, *buf_base = NULL;
72 int r, hangover, solid, alignment;
75 if (fd == -1 || !orig_buf || bsize <= 0)
78 hangover = count % bsize;
79 solid = count - hangover;
80 alignment = get_alignment(fd);
82 if ((long)orig_buf & (alignment - 1)) {
83 buf = aligned_malloc(&buf_base, count, alignment);
86 memcpy(buf, orig_buf, count);
90 r = write(fd, buf, solid);
91 if (r < 0 || r != solid)
95 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
99 r = read(fd, hangover_buf, bsize);
100 if (r < 0 || r < hangover)
106 r = lseek(fd, -bsize, SEEK_CUR);
109 memcpy(hangover_buf, (char*)buf + solid, hangover);
111 r = write(fd, hangover_buf, bsize);
112 if (r < 0 || r < hangover)
117 free(hangover_buf_base);
123 ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
124 void *hangover_buf, *hangover_buf_base = NULL;
125 void *buf, *buf_base = NULL;
126 int r, hangover, solid, alignment;
129 if (fd == -1 || !orig_buf || bsize <= 0)
132 hangover = count % bsize;
133 solid = count - hangover;
134 alignment = get_alignment(fd);
136 if ((long)orig_buf & (alignment - 1)) {
137 buf = aligned_malloc(&buf_base, count, alignment);
143 r = read(fd, buf, solid);
144 if(r < 0 || r != solid)
148 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
151 r = read(fd, hangover_buf, bsize);
152 if (r < 0 || r < hangover)
155 memcpy((char *)buf + solid, hangover_buf, hangover);
159 free(hangover_buf_base);
160 if (buf != orig_buf) {
161 memcpy(orig_buf, buf, count);
168 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
169 * but we also need a function to deal with short writes at the start. But this information
170 * is implicitly included in the read/write offset, which can not be set to non-aligned
171 * boundaries. Hence, we combine llseek with write.
173 ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
175 void *frontPadBuf_base = NULL;
177 size_t innerCount = 0;
180 if (fd == -1 || !buf || bsize <= 0)
183 frontHang = offset % bsize;
185 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
189 frontPadBuf = aligned_malloc(&frontPadBuf_base,
190 bsize, get_alignment(fd));
194 r = read(fd, frontPadBuf, bsize);
195 if (r < 0 || r != bsize)
198 innerCount = bsize - frontHang;
199 if (innerCount > count)
202 memcpy(frontPadBuf + frontHang, buf, innerCount);
204 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
207 r = write(fd, frontPadBuf, bsize);
208 if (r < 0 || r != bsize)
215 ret = count ? write_blockwise(fd, bsize, buf, count) : 0;
219 free(frontPadBuf_base);
225 #define DEFAULT_PROCESS_PRIORITY -18
227 static int _priority;
228 static int _memlock_count = 0;
230 // return 1 if memory is locked
231 int crypt_memlock_inc(struct crypt_device *ctx)
233 if (!_memlock_count++) {
234 log_dbg("Locking memory.");
235 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
236 log_dbg("Cannot lock memory with mlockall.");
241 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
242 log_err(ctx, _("Cannot get process priority.\n"));
244 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
245 log_dbg("setpriority %d failed: %s",
246 DEFAULT_PROCESS_PRIORITY, strerror(errno));
248 return _memlock_count ? 1 : 0;
251 int crypt_memlock_dec(struct crypt_device *ctx)
253 if (_memlock_count && (!--_memlock_count)) {
254 log_dbg("Unlocking memory.");
255 if (munlockall() == -1)
256 log_err(ctx, _("Cannot unlock memory.\n"));
257 if (setpriority(PRIO_PROCESS, 0, _priority))
258 log_dbg("setpriority %d failed: %s", _priority, strerror(errno));
260 return _memlock_count ? 1 : 0;