From a10cde35f5ce717d8cc7766260f42312072ac2d9 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Wed, 6 Jul 2016 18:58:47 +1000 Subject: [PATCH] DataFlash: stop logging before filling SD card on PX4 Filling the SD card causes NuttX to have conniptions, including data loss and failure to boot --- libraries/DataFlash/DataFlash_File.cpp | 15 +++++++++++++++ libraries/DataFlash/DataFlash_File.h | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/libraries/DataFlash/DataFlash_File.cpp b/libraries/DataFlash/DataFlash_File.cpp index 5784265ff5..3c196d5a5b 100644 --- a/libraries/DataFlash/DataFlash_File.cpp +++ b/libraries/DataFlash/DataFlash_File.cpp @@ -824,6 +824,12 @@ uint16_t DataFlash_File::start_new_log(void) _read_fd = -1; } + if (disk_space_avail() < _free_space_min_avail) { + hal.console->printf("Out of space for logging\n"); + _open_error = true; + return 0xffff; + } + uint16_t log_num = find_last_log(); // re-use empty logs if possible if (_get_log_size(log_num) > 0 || log_num == 0) { @@ -1068,6 +1074,15 @@ void DataFlash_File::_io_timer(void) // least once per 2 seconds if data is available return; } + if (tnow - _free_space_last_check_time > _free_space_check_interval) { + _free_space_last_check_time = tnow; + if (disk_space_avail() < _free_space_min_avail) { + hal.console->printf("Out of space for logging\n"); + stop_logging(); + _open_error = true; // prevent logging starting again + return; + } + } hal.util->perf_begin(_perf_write); diff --git a/libraries/DataFlash/DataFlash_File.h b/libraries/DataFlash/DataFlash_File.h index a6802b245e..6e228def84 100644 --- a/libraries/DataFlash/DataFlash_File.h +++ b/libraries/DataFlash/DataFlash_File.h @@ -145,6 +145,13 @@ private: return ret; }; + // free-space checks; filling up SD cards under NuttX leads to + // corrupt filesystems which cause loss of data, failure to gather + // data and failures-to-boot. + uint64_t _free_space_last_check_time; // microseconds + const uint32_t _free_space_check_interval = 1000000UL; // microseconds + const uint32_t _free_space_min_avail = 8388608; // bytes + AP_HAL::Semaphore *semaphore; // performance counters