2 * Helper functions for multiple mount protection (MMP).
4 * Copyright (C) 2011 Whamcloud, Inc.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
15 #ifndef _DEFAULT_SOURCE
16 #define _DEFAULT_SOURCE /* since glibc 2.20 _SVID_SOURCE is deprecated */
26 #include <sys/types.h>
30 #include "ext2fs/ext2_fs.h"
31 #include "ext2fs/ext2fs.h"
37 #if __GNUC_PREREQ (4, 6)
38 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
44 errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
47 struct mmp_struct *mmp_cmp;
50 if ((mmp_blk <= fs->super->s_first_data_block) ||
51 (mmp_blk >= ext2fs_blocks_count(fs->super)))
52 return EXT2_ET_MMP_BAD_BLOCK;
54 /* ext2fs_open() reserves fd0,1,2 to avoid stdio collision, so checking
55 * mmp_fd <= 0 is OK to validate that the fd is valid. This opens its
56 * own fd to read the MMP block to ensure that it is using O_DIRECT,
57 * regardless of how the io_manager is doing reads, to avoid caching of
58 * the MMP block by the io_manager or the VM. It needs to be fresh. */
59 if (fs->mmp_fd <= 0) {
60 fs->mmp_fd = open(fs->device_name, O_RDWR | O_DIRECT);
62 retval = EXT2_ET_MMP_OPEN_DIRECT;
67 if (fs->mmp_cmp == NULL) {
68 int align = ext2fs_get_dio_alignment(fs->mmp_fd);
70 retval = ext2fs_get_memalign(fs->blocksize, align,
76 if ((blk64_t) ext2fs_llseek(fs->mmp_fd, mmp_blk * fs->blocksize,
78 mmp_blk * fs->blocksize) {
79 retval = EXT2_ET_LLSEEK_FAILED;
83 if (read(fs->mmp_fd, fs->mmp_cmp, fs->blocksize) != fs->blocksize) {
84 retval = EXT2_ET_SHORT_READ;
88 mmp_cmp = fs->mmp_cmp;
90 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
91 !ext2fs_mmp_csum_verify(fs, mmp_cmp))
92 retval = EXT2_ET_MMP_CSUM_INVALID;
94 #ifdef WORDS_BIGENDIAN
95 ext2fs_swap_mmp(mmp_cmp);
98 if (buf != NULL && buf != fs->mmp_cmp)
99 memcpy(buf, fs->mmp_cmp, fs->blocksize);
101 if (mmp_cmp->mmp_magic != EXT4_MMP_MAGIC) {
102 retval = EXT2_ET_MMP_MAGIC_INVALID;
109 return EXT2_ET_OP_NOT_SUPPORTED;
113 errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf)
116 struct mmp_struct *mmp_s = buf;
118 errcode_t retval = 0;
120 gettimeofday(&tv, 0);
121 mmp_s->mmp_time = tv.tv_sec;
122 fs->mmp_last_written = tv.tv_sec;
124 if (fs->super->s_mmp_block < fs->super->s_first_data_block ||
125 fs->super->s_mmp_block > ext2fs_blocks_count(fs->super))
126 return EXT2_ET_MMP_BAD_BLOCK;
128 #ifdef WORDS_BIGENDIAN
129 ext2fs_swap_mmp(mmp_s);
132 retval = ext2fs_mmp_csum_set(fs, mmp_s);
136 /* I was tempted to make this use O_DIRECT and the mmp_fd, but
137 * this caused no end of grief, while leaving it as-is works. */
138 retval = io_channel_write_blk64(fs->io, mmp_blk, -(int)sizeof(struct mmp_struct), buf);
140 #ifdef WORDS_BIGENDIAN
141 ext2fs_swap_mmp(mmp_s);
144 /* Make sure the block gets to disk quickly */
145 io_channel_flush(fs->io);
148 return EXT2_ET_OP_NOT_SUPPORTED;
153 #define srand(x) srandom(x)
154 #define rand() random()
157 unsigned ext2fs_mmp_new_seq(void)
163 gettimeofday(&tv, 0);
164 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
166 gettimeofday(&tv, 0);
167 /* Crank the random number generator a few times */
168 for (new_seq = (tv.tv_sec ^ tv.tv_usec) & 0x1F; new_seq > 0; new_seq--)
173 } while (new_seq > EXT4_MMP_SEQ_MAX);
177 return EXT2_ET_OP_NOT_SUPPORTED;
182 static errcode_t ext2fs_mmp_reset(ext2_filsys fs)
184 struct mmp_struct *mmp_s = NULL;
185 errcode_t retval = 0;
187 if (fs->mmp_buf == NULL) {
188 retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
193 memset(fs->mmp_buf, 0, fs->blocksize);
196 mmp_s->mmp_magic = EXT4_MMP_MAGIC;
197 mmp_s->mmp_seq = EXT4_MMP_SEQ_CLEAN;
199 #ifdef HAVE_GETHOSTNAME
200 gethostname(mmp_s->mmp_nodename, sizeof(mmp_s->mmp_nodename));
202 mmp_s->mmp_nodename[0] = '\0';
204 strncpy(mmp_s->mmp_bdevname, fs->device_name,
205 sizeof(mmp_s->mmp_bdevname));
207 mmp_s->mmp_check_interval = fs->super->s_mmp_update_interval;
208 if (mmp_s->mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
209 mmp_s->mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
211 retval = ext2fs_mmp_write(fs, fs->super->s_mmp_block, fs->mmp_buf);
217 errcode_t ext2fs_mmp_update(ext2_filsys fs)
219 return ext2fs_mmp_update2(fs, 0);
222 errcode_t ext2fs_mmp_clear(ext2_filsys fs)
225 errcode_t retval = 0;
227 if (!(fs->flags & EXT2_FLAG_RW))
228 return EXT2_ET_RO_FILSYS;
230 retval = ext2fs_mmp_reset(fs);
234 return EXT2_ET_OP_NOT_SUPPORTED;
238 errcode_t ext2fs_mmp_init(ext2_filsys fs)
241 struct ext2_super_block *sb = fs->super;
245 if (sb->s_mmp_update_interval == 0)
246 sb->s_mmp_update_interval = EXT4_MMP_UPDATE_INTERVAL;
247 /* This is probably excessively large, but who knows? */
248 else if (sb->s_mmp_update_interval > EXT4_MMP_MAX_UPDATE_INTERVAL)
249 return EXT2_ET_INVALID_ARGUMENT;
251 if (fs->mmp_buf == NULL) {
252 retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
257 retval = ext2fs_alloc_block2(fs, 0, fs->mmp_buf, &mmp_block);
261 sb->s_mmp_block = mmp_block;
263 retval = ext2fs_mmp_reset(fs);
270 return EXT2_ET_OP_NOT_SUPPORTED;
275 #define min(x, y) ((x) < (y) ? (x) : (y))
279 * Make sure that the fs is not mounted or being fsck'ed while opening the fs.
281 errcode_t ext2fs_mmp_start(ext2_filsys fs)
284 struct mmp_struct *mmp_s;
286 unsigned int mmp_check_interval;
287 errcode_t retval = 0;
289 if (fs->mmp_buf == NULL) {
290 retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
295 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
301 mmp_check_interval = fs->super->s_mmp_update_interval;
302 if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
303 mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
305 seq = mmp_s->mmp_seq;
306 if (seq == EXT4_MMP_SEQ_CLEAN)
308 if (seq == EXT4_MMP_SEQ_FSCK) {
309 retval = EXT2_ET_MMP_FSCK_ON;
313 if (seq > EXT4_MMP_SEQ_FSCK) {
314 retval = EXT2_ET_MMP_UNKNOWN_SEQ;
319 * If check_interval in MMP block is larger, use that instead of
320 * check_interval from the superblock.
322 if (mmp_s->mmp_check_interval > mmp_check_interval)
323 mmp_check_interval = mmp_s->mmp_check_interval;
325 sleep(min(mmp_check_interval * 2 + 1, mmp_check_interval + 60));
327 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
331 if (seq != mmp_s->mmp_seq) {
332 retval = EXT2_ET_MMP_FAILED;
337 if (!(fs->flags & EXT2_FLAG_RW))
340 mmp_s->mmp_seq = seq = ext2fs_mmp_new_seq();
341 #ifdef HAVE_GETHOSTNAME
342 gethostname(mmp_s->mmp_nodename, sizeof(mmp_s->mmp_nodename));
344 strcpy(mmp_s->mmp_nodename, "unknown host");
346 strncpy(mmp_s->mmp_bdevname, fs->device_name,
347 sizeof(mmp_s->mmp_bdevname));
349 retval = ext2fs_mmp_write(fs, fs->super->s_mmp_block, fs->mmp_buf);
353 sleep(min(2 * mmp_check_interval + 1, mmp_check_interval + 60));
355 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
359 if (seq != mmp_s->mmp_seq) {
360 retval = EXT2_ET_MMP_FAILED;
364 mmp_s->mmp_seq = EXT4_MMP_SEQ_FSCK;
365 retval = ext2fs_mmp_write(fs, fs->super->s_mmp_block, fs->mmp_buf);
374 return EXT2_ET_OP_NOT_SUPPORTED;
379 * Clear the MMP usage in the filesystem. If this function returns an
380 * error EXT2_ET_MMP_CHANGE_ABORT it means the filesystem was modified
381 * by some other process while in use, and changes should be dropped, or
382 * risk filesystem corruption.
384 errcode_t ext2fs_mmp_stop(ext2_filsys fs)
387 struct mmp_struct *mmp, *mmp_cmp;
388 errcode_t retval = 0;
390 if (!ext2fs_has_feature_mmp(fs->super) ||
391 !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
394 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
398 /* Check if the MMP block is not changed. */
400 mmp_cmp = fs->mmp_cmp;
401 if (memcmp(mmp, mmp_cmp, sizeof(*mmp_cmp))) {
402 retval = EXT2_ET_MMP_CHANGE_ABORT;
406 mmp_cmp->mmp_seq = EXT4_MMP_SEQ_CLEAN;
407 retval = ext2fs_mmp_write(fs, fs->super->s_mmp_block, fs->mmp_cmp);
410 if (fs->mmp_fd > 0) {
417 if (!ext2fs_has_feature_mmp(fs->super) ||
418 !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
421 return EXT2_ET_OP_NOT_SUPPORTED;
425 #define EXT2_MIN_MMP_UPDATE_INTERVAL 60
428 * Update the on-disk mmp buffer, after checking that it hasn't been changed.
430 errcode_t ext2fs_mmp_update2(ext2_filsys fs, int immediately)
433 struct mmp_struct *mmp, *mmp_cmp;
435 errcode_t retval = 0;
437 if (!ext2fs_has_feature_mmp(fs->super) ||
438 !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
441 gettimeofday(&tv, 0);
443 tv.tv_sec - fs->mmp_last_written < EXT2_MIN_MMP_UPDATE_INTERVAL)
446 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, NULL);
451 mmp_cmp = fs->mmp_cmp;
453 if (memcmp(mmp, mmp_cmp, sizeof(*mmp_cmp)))
454 return EXT2_ET_MMP_CHANGE_ABORT;
456 mmp->mmp_time = tv.tv_sec;
457 mmp->mmp_seq = EXT4_MMP_SEQ_FSCK;
458 retval = ext2fs_mmp_write(fs, fs->super->s_mmp_block, fs->mmp_buf);
463 if (!ext2fs_has_feature_mmp(fs->super) ||
464 !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
467 return EXT2_ET_OP_NOT_SUPPORTED;
470 #if __GNUC_PREREQ (4, 6)
471 #pragma GCC diagnostic pop