1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: env_file.c,v 12.17 2008/01/08 20:58:22 bostic Exp $ 7 */ 8 9#include "db_config.h" 10 11#include "db_int.h" 12 13/* 14 * __db_file_extend -- 15 * Initialize a regular file by writing the last page of the file. 16 * 17 * PUBLIC: int __db_file_extend __P((ENV *, DB_FH *, size_t)); 18 */ 19int 20__db_file_extend(env, fhp, size) 21 ENV *env; 22 DB_FH *fhp; 23 size_t size; 24{ 25 db_pgno_t pages; 26 size_t nw; 27 u_int32_t relative; 28 int ret; 29 char *buf; 30 31 /* 32 * Extend the file by writing the last page. If the region is >4Gb, 33 * increment may be larger than the maximum possible seek "relative" 34 * argument, as it's an unsigned 32-bit value. Break the offset into 35 * pages of 1MB each so we don't overflow -- (2^20 + 2^32 is bigger 36 * than any memory I expect to see for awhile). 37 */ 38#undef FILE_EXTEND_IO_SIZE 39#define FILE_EXTEND_IO_SIZE (8 * 1024) 40 if ((ret = __os_calloc(env, FILE_EXTEND_IO_SIZE, 1, &buf)) != 0) 41 return (ret); 42 43 pages = (db_pgno_t)((size - FILE_EXTEND_IO_SIZE) / MEGABYTE); 44 relative = (u_int32_t)((size - FILE_EXTEND_IO_SIZE) % MEGABYTE); 45 if ((ret = __os_seek(env, fhp, pages, MEGABYTE, relative)) != 0) 46 goto err; 47 if ((ret = __os_write(env, fhp, buf, FILE_EXTEND_IO_SIZE, &nw)) != 0) 48 goto err; 49 50err: __os_free(env, buf); 51 52 return (0); 53} 54 55/* 56 * __db_file_multi_write -- 57 * Overwrite a file with multiple passes to corrupt the data. 58 * 59 * PUBLIC: int __db_file_multi_write __P((ENV *, const char *)); 60 */ 61int 62__db_file_multi_write(env, path) 63 ENV *env; 64 const char *path; 65{ 66 DB_FH *fhp; 67 u_int32_t mbytes, bytes; 68 int ret; 69 70 if ((ret = __os_open(env, path, 0, DB_OSO_REGION, 0, &fhp)) == 0 && 71 (ret = __os_ioinfo(env, path, fhp, &mbytes, &bytes, NULL)) == 0) { 72 /* 73 * !!! 74 * Overwrite a regular file with alternating 0xff, 0x00 and 0xff 75 * byte patterns. Implies a fixed-block filesystem, journaling 76 * or logging filesystems will require operating system support. 77 */ 78 if ((ret = 79 __db_file_write(env, fhp, mbytes, bytes, 255)) != 0) 80 goto err; 81 if ((ret = 82 __db_file_write(env, fhp, mbytes, bytes, 0)) != 0) 83 goto err; 84 if ((ret = 85 __db_file_write(env, fhp, mbytes, bytes, 255)) != 0) 86 goto err; 87 } else 88 __db_err(env, ret, "%s", path); 89 90err: if (fhp != NULL) 91 (void)__os_closehandle(env, fhp); 92 return (ret); 93} 94 95/* 96 * __db_file_write -- 97 * A single pass over the file, writing the specified byte pattern. 98 * 99 * PUBLIC: int __db_file_write __P((ENV *, 100 * PUBLIC: DB_FH *, u_int32_t, u_int32_t, int)); 101 */ 102int 103__db_file_write(env, fhp, mbytes, bytes, pattern) 104 ENV *env; 105 DB_FH *fhp; 106 int pattern; 107 u_int32_t mbytes, bytes; 108{ 109 size_t len, nw; 110 int i, ret; 111 char *buf; 112 113#undef FILE_WRITE_IO_SIZE 114#define FILE_WRITE_IO_SIZE (64 * 1024) 115 if ((ret = __os_malloc(env, FILE_WRITE_IO_SIZE, &buf)) != 0) 116 return (ret); 117 memset(buf, pattern, FILE_WRITE_IO_SIZE); 118 119 if ((ret = __os_seek(env, fhp, 0, 0, 0)) != 0) 120 goto err; 121 for (; mbytes > 0; --mbytes) 122 for (i = MEGABYTE / FILE_WRITE_IO_SIZE; i > 0; --i) 123 if ((ret = __os_write( 124 env, fhp, buf, FILE_WRITE_IO_SIZE, &nw)) != 0) 125 goto err; 126 for (; bytes > 0; bytes -= (u_int32_t)len) { 127 len = bytes < FILE_WRITE_IO_SIZE ? bytes : FILE_WRITE_IO_SIZE; 128 if ((ret = __os_write(env, fhp, buf, len, &nw)) != 0) 129 goto err; 130 } 131 132 ret = __os_fsync(env, fhp); 133 134err: __os_free(env, buf); 135 return (ret); 136} 137