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 return (unsigned)sysconf(_SC_PAGESIZE);
36 static int get_alignment(int fd)
38 int alignment = DEFAULT_MEM_ALIGNMENT;
40 #ifdef _PC_REC_XFER_ALIGN
41 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
43 alignment = DEFAULT_MEM_ALIGNMENT;
48 static void *aligned_malloc(void **base, int size, int alignment)
50 #ifdef HAVE_POSIX_MEMALIGN
51 return posix_memalign(base, alignment, size) ? NULL : *base;
53 /* Credits go to Michal's padlock patches for this alignment code */
56 ptr = malloc(size + alignment);
57 if(ptr == NULL) return NULL;
60 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
61 ptr += alignment - ((long)(ptr) & (alignment - 1));
67 ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
69 void *hangover_buf, *hangover_buf_base = NULL;
70 void *buf, *buf_base = NULL;
71 int r, hangover, solid, alignment;
74 if (fd == -1 || !orig_buf || bsize <= 0)
77 hangover = count % bsize;
78 solid = count - hangover;
79 alignment = get_alignment(fd);
81 if ((long)orig_buf & (alignment - 1)) {
82 buf = aligned_malloc(&buf_base, count, alignment);
85 memcpy(buf, orig_buf, count);
89 r = write(fd, buf, solid);
90 if (r < 0 || r != solid)
94 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
98 r = read(fd, hangover_buf, bsize);
99 if (r < 0 || r != bsize)
102 r = lseek(fd, -bsize, SEEK_CUR);
105 memcpy(hangover_buf, (char*)buf + solid, hangover);
107 r = write(fd, hangover_buf, bsize);
108 if (r < 0 || r < hangover)
113 free(hangover_buf_base);
119 ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
120 void *hangover_buf, *hangover_buf_base = NULL;
121 void *buf, *buf_base = NULL;
122 int r, hangover, solid, alignment;
125 if (fd == -1 || !orig_buf || bsize <= 0)
128 hangover = count % bsize;
129 solid = count - hangover;
130 alignment = get_alignment(fd);
132 if ((long)orig_buf & (alignment - 1)) {
133 buf = aligned_malloc(&buf_base, count, alignment);
139 r = read(fd, buf, solid);
140 if(r < 0 || r != solid)
144 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
147 r = read(fd, hangover_buf, bsize);
148 if (r < 0 || r < hangover)
151 memcpy((char *)buf + solid, hangover_buf, hangover);
155 free(hangover_buf_base);
156 if (buf != orig_buf) {
157 memcpy(orig_buf, buf, count);
164 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
165 * but we also need a function to deal with short writes at the start. But this information
166 * is implicitly included in the read/write offset, which can not be set to non-aligned
167 * boundaries. Hence, we combine llseek with write.
169 ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
171 void *frontPadBuf_base = NULL;
173 size_t innerCount = 0;
176 if (fd == -1 || !buf || bsize <= 0)
179 frontHang = offset % bsize;
181 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
185 frontPadBuf = aligned_malloc(&frontPadBuf_base,
186 bsize, get_alignment(fd));
190 r = read(fd, frontPadBuf, bsize);
191 if (r < 0 || r != bsize)
194 innerCount = bsize - frontHang;
195 if (innerCount > count)
198 memcpy(frontPadBuf + frontHang, buf, innerCount);
200 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
203 r = write(fd, frontPadBuf, bsize);
204 if (r < 0 || r != bsize)
211 ret = count ? write_blockwise(fd, bsize, buf, count) : 0;
215 free(frontPadBuf_base);
221 #define DEFAULT_PROCESS_PRIORITY -18
223 static int _priority;
224 static int _memlock_count = 0;
226 // return 1 if memory is locked
227 int crypt_memlock_inc(struct crypt_device *ctx)
229 if (!_memlock_count++) {
230 log_dbg("Locking memory.");
231 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
232 log_dbg("Cannot lock memory with mlockall.");
237 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
238 log_err(ctx, _("Cannot get process priority.\n"));
240 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
241 log_dbg("setpriority %d failed: %s",
242 DEFAULT_PROCESS_PRIORITY, strerror(errno));
244 return _memlock_count ? 1 : 0;
247 int crypt_memlock_dec(struct crypt_device *ctx)
249 if (_memlock_count && (!--_memlock_count)) {
250 log_dbg("Unlocking memory.");
251 if (munlockall() == -1)
252 log_err(ctx, _("Cannot unlock memory.\n"));
253 if (setpriority(PRIO_PROCESS, 0, _priority))
254 log_dbg("setpriority %d failed: %s", _priority, strerror(errno));
256 return _memlock_count ? 1 : 0;