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 != bsize)
103 r = lseek(fd, -bsize, SEEK_CUR);
106 memcpy(hangover_buf, (char*)buf + solid, hangover);
108 r = write(fd, hangover_buf, bsize);
109 if (r < 0 || r < hangover)
114 free(hangover_buf_base);
120 ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
121 void *hangover_buf, *hangover_buf_base = NULL;
122 void *buf, *buf_base = NULL;
123 int r, hangover, solid, alignment;
126 if (fd == -1 || !orig_buf || bsize <= 0)
129 hangover = count % bsize;
130 solid = count - hangover;
131 alignment = get_alignment(fd);
133 if ((long)orig_buf & (alignment - 1)) {
134 buf = aligned_malloc(&buf_base, count, alignment);
140 r = read(fd, buf, solid);
141 if(r < 0 || r != solid)
145 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
148 r = read(fd, hangover_buf, bsize);
149 if (r < 0 || r < hangover)
152 memcpy((char *)buf + solid, hangover_buf, hangover);
156 free(hangover_buf_base);
157 if (buf != orig_buf) {
158 memcpy(orig_buf, buf, count);
165 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
166 * but we also need a function to deal with short writes at the start. But this information
167 * is implicitly included in the read/write offset, which can not be set to non-aligned
168 * boundaries. Hence, we combine llseek with write.
170 ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
172 void *frontPadBuf_base = NULL;
174 size_t innerCount = 0;
177 if (fd == -1 || !buf || bsize <= 0)
180 frontHang = offset % bsize;
182 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
186 frontPadBuf = aligned_malloc(&frontPadBuf_base,
187 bsize, get_alignment(fd));
191 r = read(fd, frontPadBuf, bsize);
192 if (r < 0 || r != bsize)
195 innerCount = bsize - frontHang;
196 if (innerCount > count)
199 memcpy(frontPadBuf + frontHang, buf, innerCount);
201 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
204 r = write(fd, frontPadBuf, bsize);
205 if (r < 0 || r != bsize)
212 ret = count ? write_blockwise(fd, bsize, buf, count) : 0;
216 free(frontPadBuf_base);
222 #define DEFAULT_PROCESS_PRIORITY -18
224 static int _priority;
225 static int _memlock_count = 0;
227 // return 1 if memory is locked
228 int crypt_memlock_inc(struct crypt_device *ctx)
230 if (!_memlock_count++) {
231 log_dbg("Locking memory.");
232 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
233 log_dbg("Cannot lock memory with mlockall.");
238 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
239 log_err(ctx, _("Cannot get process priority.\n"));
241 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
242 log_dbg("setpriority %d failed: %s",
243 DEFAULT_PROCESS_PRIORITY, strerror(errno));
245 return _memlock_count ? 1 : 0;
248 int crypt_memlock_dec(struct crypt_device *ctx)
250 if (_memlock_count && (!--_memlock_count)) {
251 log_dbg("Unlocking memory.");
252 if (munlockall() == -1)
253 log_err(ctx, _("Cannot unlock memory.\n"));
254 if (setpriority(PRIO_PROCESS, 0, _priority))
255 log_dbg("setpriority %d failed: %s", _priority, strerror(errno));
257 return _memlock_count ? 1 : 0;