2 * utils - miscellaneous device utilities for cryptsetup
4 * Copyright (C) 2004, Jana Saout <jana@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 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/resource.h>
33 unsigned crypt_getpagesize(void)
35 long r = sysconf(_SC_PAGESIZE);
36 return r < 0 ? DEFAULT_MEM_ALIGNMENT : r;
39 static int get_alignment(int fd)
41 int alignment = DEFAULT_MEM_ALIGNMENT;
43 #ifdef _PC_REC_XFER_ALIGN
44 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
46 alignment = DEFAULT_MEM_ALIGNMENT;
51 static void *aligned_malloc(void **base, int size, int alignment)
53 #ifdef HAVE_POSIX_MEMALIGN
54 return posix_memalign(base, alignment, size) ? NULL : *base;
56 /* Credits go to Michal's padlock patches for this alignment code */
59 ptr = malloc(size + alignment);
60 if(ptr == NULL) return NULL;
63 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
64 ptr += alignment - ((long)(ptr) & (alignment - 1));
70 ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
72 void *hangover_buf, *hangover_buf_base = NULL;
73 void *buf, *buf_base = NULL;
74 int r, hangover, solid, alignment;
77 if (fd == -1 || !orig_buf || bsize <= 0)
80 hangover = count % bsize;
81 solid = count - hangover;
82 alignment = get_alignment(fd);
84 if ((long)orig_buf & (alignment - 1)) {
85 buf = aligned_malloc(&buf_base, count, alignment);
88 memcpy(buf, orig_buf, count);
92 r = write(fd, buf, solid);
93 if (r < 0 || r != solid)
97 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
101 r = read(fd, hangover_buf, bsize);
102 if (r < 0 || r < hangover)
108 r = lseek(fd, -bsize, SEEK_CUR);
111 memcpy(hangover_buf, (char*)buf + solid, hangover);
113 r = write(fd, hangover_buf, bsize);
114 if (r < 0 || r < hangover)
119 free(hangover_buf_base);
125 ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
126 void *hangover_buf, *hangover_buf_base = NULL;
127 void *buf, *buf_base = NULL;
128 int r, hangover, solid, alignment;
131 if (fd == -1 || !orig_buf || bsize <= 0)
134 hangover = count % bsize;
135 solid = count - hangover;
136 alignment = get_alignment(fd);
138 if ((long)orig_buf & (alignment - 1)) {
139 buf = aligned_malloc(&buf_base, count, alignment);
145 r = read(fd, buf, solid);
146 if(r < 0 || r != solid)
150 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
153 r = read(fd, hangover_buf, bsize);
154 if (r < 0 || r < hangover)
157 memcpy((char *)buf + solid, hangover_buf, hangover);
161 free(hangover_buf_base);
162 if (buf != orig_buf) {
163 memcpy(orig_buf, buf, count);
170 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
171 * but we also need a function to deal with short writes at the start. But this information
172 * is implicitly included in the read/write offset, which can not be set to non-aligned
173 * boundaries. Hence, we combine llseek with write.
175 ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
177 void *frontPadBuf_base = NULL;
179 size_t innerCount = 0;
182 if (fd == -1 || !buf || bsize <= 0)
185 frontHang = offset % bsize;
187 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
191 frontPadBuf = aligned_malloc(&frontPadBuf_base,
192 bsize, get_alignment(fd));
196 r = read(fd, frontPadBuf, bsize);
197 if (r < 0 || r != bsize)
200 innerCount = bsize - frontHang;
201 if (innerCount > count)
204 memcpy(frontPadBuf + frontHang, buf, innerCount);
206 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
209 r = write(fd, frontPadBuf, bsize);
210 if (r < 0 || r != bsize)
217 ret = count ? write_blockwise(fd, bsize, buf, count) : 0;
221 free(frontPadBuf_base);
227 #define DEFAULT_PROCESS_PRIORITY -18
229 static int _priority;
230 static int _memlock_count = 0;
232 // return 1 if memory is locked
233 int crypt_memlock_inc(struct crypt_device *ctx)
235 if (!_memlock_count++) {
236 log_dbg("Locking memory.");
237 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
238 log_dbg("Cannot lock memory with mlockall.");
243 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
244 log_err(ctx, _("Cannot get process priority.\n"));
246 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
247 log_dbg("setpriority %d failed: %s",
248 DEFAULT_PROCESS_PRIORITY, strerror(errno));
250 return _memlock_count ? 1 : 0;
253 int crypt_memlock_dec(struct crypt_device *ctx)
255 if (_memlock_count && (!--_memlock_count)) {
256 log_dbg("Unlocking memory.");
257 if (munlockall() == -1)
258 log_err(ctx, _("Cannot unlock memory.\n"));
259 if (setpriority(PRIO_PROCESS, 0, _priority))
260 log_dbg("setpriority %d failed: %s", _priority, strerror(errno));
262 return _memlock_count ? 1 : 0;