| 1 |
/* |
| 2 |
* Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved. |
| 3 |
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
* for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
* |
| 19 |
* Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 20 |
* Sunnyvale, CA 94085, or: http://www.zmanda.com |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "amanda.h" |
| 24 |
#include <string.h> /* memset() */ |
| 25 |
#include "fsusage.h" |
| 26 |
#include "amutil.h" |
| 27 |
#include <regex.h> |
| 28 |
|
| 29 |
#include "vfs-device.h" |
| 30 |
|
| 31 |
/* This regex will match all VfsDevice files in a directory. We use it |
| 32 |
for cleanup and verification. Note that this regex does NOT match |
| 33 |
the volume label. */ |
| 34 |
#define VFS_DEVICE_FILE_REGEX "^[0-9]+[\\.-]" |
| 35 |
|
| 36 |
/* The name of the volume lockfile. Should be the same as that |
| 37 |
generated by lockfile_name(0). */ |
| 38 |
#define VOLUME_LOCKFILE_NAME "00000-lock" |
| 39 |
|
| 40 |
#define VFS_DEVICE_MIN_BLOCK_SIZE (1) |
| 41 |
#define VFS_DEVICE_MAX_BLOCK_SIZE (INT_MAX) |
| 42 |
#define VFS_DEVICE_DEFAULT_BLOCK_SIZE (DISK_BLOCK_BYTES) |
| 43 |
|
| 44 |
/* Allow comfortable room for another block and a header before PEOM */ |
| 45 |
#define EOM_EARLY_WARNING_ZONE_BLOCKS 4 |
| 46 |
|
| 47 |
/* Constants for free-space monitoring */ |
| 48 |
#define MONITOR_FREE_SPACE_EVERY_SECONDS 5 |
| 49 |
#define MONITOR_FREE_SPACE_EVERY_KB 102400 |
| 50 |
#define MONITOR_FREE_SPACE_CLOSELY_WITHIN_BLOCKS 128 |
| 51 |
|
| 52 |
void vfs_device_register(void); |
| 53 |
|
| 54 |
/* here are local prototypes */ |
| 55 |
static void vfs_device_init (VfsDevice * o); |
| 56 |
static void vfs_device_class_init (VfsDeviceClass * c); |
| 57 |
static void vfs_device_base_init (VfsDeviceClass * c); |
| 58 |
static void vfs_device_finalize (GObject * o); |
| 59 |
|
| 60 |
static gboolean vfs_device_start(Device * dself, DeviceAccessMode mode, |
| 61 |
char * label, char * timestamp); |
| 62 |
static gboolean vfs_device_finish (Device * dself); |
| 63 |
static void vfs_device_open_device (Device * dself, char * device_name, |
| 64 |
char * device_type, char * device_node); |
| 65 |
static gboolean vfs_device_start_file (Device * dself, dumpfile_t * ji); |
| 66 |
static gboolean vfs_device_finish_file (Device * dself); |
| 67 |
static dumpfile_t * vfs_device_seek_file (Device * self, guint file); |
| 68 |
static gboolean vfs_device_seek_block (Device * self, guint64 block); |
| 69 |
static gboolean vfs_device_recycle_file (Device * dself, guint filenum); |
| 70 |
static gboolean vfs_device_erase (Device * dself); |
| 71 |
static Device * vfs_device_factory(char * device_name, char * device_type, char * device_node); |
| 72 |
static DeviceStatusFlags vfs_device_read_label(Device * dself); |
| 73 |
static DeviceWriteResult vfs_device_write_block(Device * self, guint size, |
| 74 |
gpointer data); |
| 75 |
static int vfs_device_read_block(Device * self, gpointer data, int * size_req, int max_block); |
| 76 |
|
| 77 |
/* Various helper functions. */ |
| 78 |
static gboolean vfs_clear_and_prepare_label(Device *dself, char *label, |
| 79 |
char *timestamp); |
| 80 |
static void vfs_release_file(Device *dself); |
| 81 |
static void vfs_update_volume_size(Device *dself); |
| 82 |
static gboolean vfs_device_start_file_open(Device *dself, dumpfile_t *ji); |
| 83 |
static gboolean vfs_validate(Device *dself); |
| 84 |
|
| 85 |
static gboolean check_is_dir(VfsDevice * self, const char * name); |
| 86 |
static char * file_number_to_file_name(VfsDevice * self, guint file); |
| 87 |
static gboolean file_number_to_file_name_functor(const char * filename, |
| 88 |
gpointer datap); |
| 89 |
static gboolean vfs_device_set_max_volume_usage_fn(Device *dself, |
| 90 |
DevicePropertyBase *base, GValue *val, |
| 91 |
PropertySurety surety, PropertySource source); |
| 92 |
static gboolean vfs_device_set_enforce_max_volume_usage_fn(Device *dself, |
| 93 |
DevicePropertyBase *base, GValue *val, |
| 94 |
PropertySurety surety, PropertySource source); |
| 95 |
static gboolean property_get_monitor_free_space_fn(Device *dself, |
| 96 |
DevicePropertyBase *base, GValue *val, |
| 97 |
PropertySurety *surety, PropertySource *source); |
| 98 |
static gboolean property_set_monitor_free_space_fn(Device *dself, |
| 99 |
DevicePropertyBase *base, GValue *val, |
| 100 |
PropertySurety surety, PropertySource source); |
| 101 |
static gboolean property_get_slow_write_fn(Device *dself, |
| 102 |
DevicePropertyBase *base, GValue *val, |
| 103 |
PropertySurety *surety, PropertySource *source); |
| 104 |
static gboolean property_set_slow_write_fn(Device *dself, |
| 105 |
DevicePropertyBase *base, GValue *val, |
| 106 |
PropertySurety surety, PropertySource source); |
| 107 |
static gboolean property_get_use_data_fn(Device *dself, |
| 108 |
DevicePropertyBase *base, GValue *val, |
| 109 |
PropertySurety *surety, PropertySource *source); |
| 110 |
static gboolean property_set_use_data_fn(Device *dself, |
| 111 |
DevicePropertyBase *base, GValue *val, |
| 112 |
PropertySurety surety, PropertySource source); |
| 113 |
static gboolean property_set_leom_fn(Device *dself, |
| 114 |
DevicePropertyBase *base, GValue *val, |
| 115 |
PropertySurety surety, PropertySource source); |
| 116 |
//static char* lockfile_name(VfsDevice * self, guint file); |
| 117 |
static gboolean open_lock(VfsDevice * self, int file, gboolean exclusive); |
| 118 |
static void promote_volume_lock(VfsDevice * self); |
| 119 |
static void demote_volume_lock(VfsDevice * self); |
| 120 |
static gboolean delete_vfs_files_functor(const char * filename, |
| 121 |
gpointer self); |
| 122 |
static gboolean check_dir_empty_functor(const char * filename, |
| 123 |
gpointer self); |
| 124 |
static int search_vfs_directory(VfsDevice *self, const char * regex, |
| 125 |
SearchDirectoryFunctor functor, gpointer user_data); |
| 126 |
static gint get_last_file_number(VfsDevice * self); |
| 127 |
static gboolean get_last_file_number_functor(const char * filename, |
| 128 |
gpointer datap); |
| 129 |
static char * make_new_file_name(VfsDevice * self, const dumpfile_t * ji); |
| 130 |
static gboolean try_unlink(const char * file); |
| 131 |
|
| 132 |
/* return TRUE if the device is going to hit ENOSPC "soon" - this is used to |
| 133 |
* detect LEOM as represented by actually running out of space on the |
| 134 |
* underlying filesystem. Size is the size of the buffer that is about to |
| 135 |
* be written. */ |
| 136 |
static gboolean check_at_leom(VfsDevice *self, guint64 size); |
| 137 |
/* Similar, but for PEOM */ |
| 138 |
static gboolean check_at_peom(VfsDevice *self, guint64 size); |
| 139 |
|
| 140 |
/* pointer to the classes of our parents */ |
| 141 |
static DeviceClass *parent_class = NULL; |
| 142 |
|
| 143 |
/* device-specific properties */ |
| 144 |
DevicePropertyBase device_property_monitor_free_space; |
| 145 |
#define PROPERTY_MONITOR_FREE_SPACE (device_property_monitor_free_space.ID) |
| 146 |
|
| 147 |
DevicePropertyBase device_property_slow_write; |
| 148 |
#define PROPERTY_SLOW_WRITE (device_property_slow_write.ID) |
| 149 |
|
| 150 |
DevicePropertyBase device_property_use_data; |
| 151 |
#define PROPERTY_USE_DATA (device_property_use_data.ID) |
| 152 |
|
| 153 |
void vfs_device_register(void) { |
| 154 |
static const char * device_prefix_list[] = { "file", NULL }; |
| 155 |
|
| 156 |
device_property_fill_and_register(&device_property_monitor_free_space, |
| 157 |
G_TYPE_BOOLEAN, "monitor_free_space", |
| 158 |
"Should VFS device monitor the filesystem's available free space?"); |
| 159 |
device_property_fill_and_register(&device_property_slow_write, |
| 160 |
G_TYPE_BOOLEAN, "slow_write", |
| 161 |
"Write 5 blocks by second, use for testing only"); |
| 162 |
device_property_fill_and_register(&device_property_use_data, |
| 163 |
G_TYPE_STRING, "use_data", |
| 164 |
"Should VFS device use the data subdir?"); |
| 165 |
|
| 166 |
register_device(vfs_device_factory, device_prefix_list); |
| 167 |
} |
| 168 |
|
| 169 |
GType |
| 170 |
vfs_device_get_type (void) |
| 171 |
{ |
| 172 |
static GType type = 0; |
| 173 |
|
| 174 |
if (G_UNLIKELY(type == 0)) { |
| 175 |
static const GTypeInfo info = { |
| 176 |
sizeof (VfsDeviceClass), |
| 177 |
(GBaseInitFunc) vfs_device_base_init, |
| 178 |
(GBaseFinalizeFunc) NULL, |
| 179 |
(GClassInitFunc) vfs_device_class_init, |
| 180 |
(GClassFinalizeFunc) NULL, |
| 181 |
NULL /* class_data */, |
| 182 |
sizeof (VfsDevice), |
| 183 |
0 /* n_preallocs */, |
| 184 |
(GInstanceInitFunc) vfs_device_init, |
| 185 |
NULL |
| 186 |
}; |
| 187 |
|
| 188 |
type = g_type_register_static (TYPE_DEVICE, "VfsDevice", |
| 189 |
&info, (GTypeFlags)0); |
| 190 |
} |
| 191 |
|
| 192 |
return type; |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
vfs_device_init ( |
| 197 |
VfsDevice *self) |
| 198 |
{ |
| 199 |
Device *dself = DEVICE(self); |
| 200 |
GValue response; |
| 201 |
|
| 202 |
self->dir_name = self->file_name = NULL; |
| 203 |
self->open_file_fd = -1; |
| 204 |
self->volume_bytes = 0; |
| 205 |
self->volume_limit = 0; |
| 206 |
self->leom = TRUE; |
| 207 |
self->enforce_volume_limit = TRUE; |
| 208 |
|
| 209 |
self->monitor_free_space = TRUE; |
| 210 |
self->slow_write = FALSE; |
| 211 |
self->slow_count = 0; |
| 212 |
self->use_data = 2; |
| 213 |
self->checked_fs_free_bytes = G_MAXUINT64; |
| 214 |
self->checked_fs_free_time = 0; |
| 215 |
self->checked_fs_free_bytes = G_MAXUINT64; |
| 216 |
self->clear_and_prepare_label = &vfs_clear_and_prepare_label; |
| 217 |
self->release_file = &vfs_release_file; |
| 218 |
self->update_volume_size = &vfs_update_volume_size; |
| 219 |
self->device_start_file_open = &vfs_device_start_file_open; |
| 220 |
self->validate = &vfs_validate; |
| 221 |
|
| 222 |
/* Register Properties */ |
| 223 |
bzero(&response, sizeof(response)); |
| 224 |
|
| 225 |
g_value_init(&response, CONCURRENCY_PARADIGM_TYPE); |
| 226 |
g_value_set_enum(&response, CONCURRENCY_PARADIGM_RANDOM_ACCESS); |
| 227 |
device_set_simple_property(dself, PROPERTY_CONCURRENCY, |
| 228 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 229 |
g_value_unset(&response); |
| 230 |
|
| 231 |
g_value_init(&response, STREAMING_REQUIREMENT_TYPE); |
| 232 |
g_value_set_enum(&response, STREAMING_REQUIREMENT_NONE); |
| 233 |
device_set_simple_property(dself, PROPERTY_STREAMING, |
| 234 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 235 |
g_value_unset(&response); |
| 236 |
|
| 237 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 238 |
g_value_set_boolean(&response, TRUE); |
| 239 |
device_set_simple_property(dself, PROPERTY_APPENDABLE, |
| 240 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 241 |
g_value_unset(&response); |
| 242 |
|
| 243 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 244 |
g_value_set_boolean(&response, TRUE); |
| 245 |
device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION, |
| 246 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 247 |
g_value_unset(&response); |
| 248 |
|
| 249 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 250 |
g_value_set_boolean(&response, TRUE); |
| 251 |
device_set_simple_property(dself, PROPERTY_FULL_DELETION, |
| 252 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 253 |
g_value_unset(&response); |
| 254 |
|
| 255 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 256 |
g_value_set_boolean(&response, TRUE); |
| 257 |
device_set_simple_property(dself, PROPERTY_LEOM, |
| 258 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 259 |
g_value_unset(&response); |
| 260 |
|
| 261 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 262 |
g_value_set_boolean(&response, TRUE); |
| 263 |
device_set_simple_property(dself, PROPERTY_ENFORCE_MAX_VOLUME_USAGE, |
| 264 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 265 |
g_value_unset(&response); |
| 266 |
|
| 267 |
g_value_init(&response, G_TYPE_BOOLEAN); |
| 268 |
g_value_set_boolean(&response, FALSE); |
| 269 |
device_set_simple_property(dself, PROPERTY_COMPRESSION, |
| 270 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 271 |
g_value_unset(&response); |
| 272 |
|
| 273 |
g_value_init(&response, MEDIA_ACCESS_MODE_TYPE); |
| 274 |
g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE); |
| 275 |
device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE, |
| 276 |
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED); |
| 277 |
g_value_unset(&response); |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
vfs_device_class_init( |
| 282 |
VfsDeviceClass *c) |
| 283 |
{ |
| 284 |
GObjectClass *g_object_class = (GObjectClass*) c; |
| 285 |
DeviceClass *device_class = DEVICE_CLASS(c); |
| 286 |
|
| 287 |
parent_class = g_type_class_ref(TYPE_DEVICE); |
| 288 |
|
| 289 |
device_class->open_device = vfs_device_open_device; |
| 290 |
device_class->start = vfs_device_start; |
| 291 |
device_class->start_file = vfs_device_start_file; |
| 292 |
device_class->read_label = vfs_device_read_label; |
| 293 |
device_class->write_block = vfs_device_write_block; |
| 294 |
device_class->read_block = vfs_device_read_block; |
| 295 |
device_class->finish_file = vfs_device_finish_file; |
| 296 |
device_class->seek_file = vfs_device_seek_file; |
| 297 |
device_class->seek_block = vfs_device_seek_block; |
| 298 |
device_class->recycle_file = vfs_device_recycle_file; |
| 299 |
device_class->erase = vfs_device_erase; |
| 300 |
device_class->finish = vfs_device_finish; |
| 301 |
|
| 302 |
g_object_class->finalize = vfs_device_finalize; |
| 303 |
} |
| 304 |
|
| 305 |
static void |
| 306 |
vfs_device_base_init( |
| 307 |
VfsDeviceClass *c) |
| 308 |
{ |
| 309 |
DeviceClass *device_class = (DeviceClass *)c; |
| 310 |
|
| 311 |
device_class_register_property(device_class, PROPERTY_MONITOR_FREE_SPACE, |
| 312 |
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK, |
| 313 |
property_get_monitor_free_space_fn, |
| 314 |
property_set_monitor_free_space_fn); |
| 315 |
|
| 316 |
device_class_register_property(device_class, PROPERTY_SLOW_WRITE, |
| 317 |
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK, |
| 318 |
property_get_slow_write_fn, |
| 319 |
property_set_slow_write_fn); |
| 320 |
|
| 321 |
device_class_register_property(device_class, PROPERTY_USE_DATA, |
| 322 |
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK, |
| 323 |
property_get_use_data_fn, |
| 324 |
property_set_use_data_fn); |
| 325 |
|
| 326 |
device_class_register_property(device_class, PROPERTY_MAX_VOLUME_USAGE, |
| 327 |
(PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK) & |
| 328 |
(~ PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE), |
| 329 |
device_simple_property_get_fn, |
| 330 |
vfs_device_set_max_volume_usage_fn); |
| 331 |
|
| 332 |
device_class_register_property(device_class, PROPERTY_ENFORCE_MAX_VOLUME_USAGE, |
| 333 |
(PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK) & |
| 334 |
(~ PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE), |
| 335 |
device_simple_property_get_fn, |
| 336 |
vfs_device_set_enforce_max_volume_usage_fn); |
| 337 |
|
| 338 |
device_class_register_property(device_class, PROPERTY_COMPRESSION, |
| 339 |
PROPERTY_ACCESS_GET_MASK, |
| 340 |
device_simple_property_get_fn, |
| 341 |
NULL); |
| 342 |
|
| 343 |
/* add the ability to set LEOM to FALSE, for testing purposes */ |
| 344 |
device_class_register_property(device_class, PROPERTY_LEOM, |
| 345 |
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START, |
| 346 |
device_simple_property_get_fn, |
| 347 |
property_set_leom_fn); |
| 348 |
} |
| 349 |
|
| 350 |
static gboolean |
| 351 |
vfs_device_set_max_volume_usage_fn( |
| 352 |
Device *dself, |
| 353 |
DevicePropertyBase *base, |
| 354 |
GValue *val, |
| 355 |
PropertySurety surety, |
| 356 |
PropertySource source) |
| 357 |
{ |
| 358 |
VfsDevice *self = VFS_DEVICE(dself); |
| 359 |
|
| 360 |
self->volume_limit = g_value_get_uint64(val); |
| 361 |
|
| 362 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 363 |
} |
| 364 |
|
| 365 |
static gboolean |
| 366 |
vfs_device_set_enforce_max_volume_usage_fn( |
| 367 |
Device *dself, |
| 368 |
DevicePropertyBase *base, |
| 369 |
GValue *val, |
| 370 |
PropertySurety surety, |
| 371 |
PropertySource source) |
| 372 |
{ |
| 373 |
VfsDevice *self = VFS_DEVICE(dself); |
| 374 |
|
| 375 |
self->enforce_volume_limit = g_value_get_boolean(val); |
| 376 |
|
| 377 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 378 |
} |
| 379 |
|
| 380 |
static gboolean |
| 381 |
property_get_monitor_free_space_fn( |
| 382 |
Device *dself, |
| 383 |
DevicePropertyBase *base G_GNUC_UNUSED, |
| 384 |
GValue *val, |
| 385 |
PropertySurety *surety, |
| 386 |
PropertySource *source) |
| 387 |
{ |
| 388 |
VfsDevice *self = VFS_DEVICE(dself); |
| 389 |
|
| 390 |
g_value_unset_init(val, G_TYPE_BOOLEAN); |
| 391 |
g_value_set_boolean(val, self->monitor_free_space); |
| 392 |
|
| 393 |
if (surety) |
| 394 |
*surety = PROPERTY_SURETY_GOOD; |
| 395 |
|
| 396 |
if (source) |
| 397 |
*source = PROPERTY_SOURCE_DEFAULT; |
| 398 |
|
| 399 |
return TRUE; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
static gboolean |
| 404 |
property_set_monitor_free_space_fn( |
| 405 |
Device *dself, |
| 406 |
DevicePropertyBase *base, |
| 407 |
GValue *val, |
| 408 |
PropertySurety surety, |
| 409 |
PropertySource source) |
| 410 |
{ |
| 411 |
VfsDevice *self = VFS_DEVICE(dself); |
| 412 |
|
| 413 |
self->monitor_free_space = g_value_get_boolean(val); |
| 414 |
|
| 415 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 416 |
} |
| 417 |
|
| 418 |
static gboolean |
| 419 |
property_get_slow_write_fn( |
| 420 |
Device *dself, |
| 421 |
DevicePropertyBase *base G_GNUC_UNUSED, |
| 422 |
GValue *val, |
| 423 |
PropertySurety *surety, |
| 424 |
PropertySource *source) |
| 425 |
{ |
| 426 |
VfsDevice *self = VFS_DEVICE(dself); |
| 427 |
|
| 428 |
g_value_unset_init(val, G_TYPE_BOOLEAN); |
| 429 |
g_value_set_boolean(val, self->slow_write); |
| 430 |
|
| 431 |
if (surety) |
| 432 |
*surety = PROPERTY_SURETY_GOOD; |
| 433 |
|
| 434 |
if (source) |
| 435 |
*source = PROPERTY_SOURCE_DEFAULT; |
| 436 |
|
| 437 |
return TRUE; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
static gboolean |
| 442 |
property_set_slow_write_fn( |
| 443 |
Device *dself, |
| 444 |
DevicePropertyBase *base, |
| 445 |
GValue *val, |
| 446 |
PropertySurety surety, |
| 447 |
PropertySource source) |
| 448 |
{ |
| 449 |
VfsDevice *self = VFS_DEVICE(dself); |
| 450 |
|
| 451 |
self->slow_write = g_value_get_boolean(val); |
| 452 |
|
| 453 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 454 |
} |
| 455 |
|
| 456 |
static gboolean |
| 457 |
property_get_use_data_fn( |
| 458 |
Device *dself, |
| 459 |
DevicePropertyBase *base G_GNUC_UNUSED, |
| 460 |
GValue *val, |
| 461 |
PropertySurety *surety, |
| 462 |
PropertySource *source) |
| 463 |
{ |
| 464 |
VfsDevice *self = VFS_DEVICE(dself); |
| 465 |
|
| 466 |
g_value_unset_init(val, G_TYPE_STRING); |
| 467 |
switch (self->use_data) { |
| 468 |
case 0: g_value_set_string(val, "NO"); break; |
| 469 |
case 1: g_value_set_string(val, "YES"); break; |
| 470 |
case 2: g_value_set_string(val, "EXIST"); break; |
| 471 |
} |
| 472 |
if (surety) |
| 473 |
*surety = PROPERTY_SURETY_GOOD; |
| 474 |
|
| 475 |
if (source) |
| 476 |
*source = PROPERTY_SOURCE_DEFAULT; |
| 477 |
|
| 478 |
return TRUE; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
static gboolean |
| 483 |
property_set_use_data_fn( |
| 484 |
Device *dself, |
| 485 |
DevicePropertyBase *base, |
| 486 |
GValue *val, |
| 487 |
PropertySurety surety, |
| 488 |
PropertySource source) |
| 489 |
{ |
| 490 |
VfsDevice *self = VFS_DEVICE(dself); |
| 491 |
|
| 492 |
const char *value = g_value_get_string(val); |
| 493 |
|
| 494 |
if (g_strcasecmp(value, "NO") == 0 || |
| 495 |
g_strcasecmp(value, "FALSE") == 0) { |
| 496 |
self->use_data = 0; |
| 497 |
} else if (g_strcasecmp(value, "YES") == 0 || |
| 498 |
g_strcasecmp(value, "TRUE") == 0) { |
| 499 |
self->use_data = 1; |
| 500 |
} else if (g_strcasecmp(value, "EXIST") == 0) { |
| 501 |
self->use_data = 2; |
| 502 |
} else { |
| 503 |
g_warning(_("Illegal USE-DATA value (%s), using 'EXIST'."), value); |
| 504 |
} |
| 505 |
|
| 506 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 507 |
} |
| 508 |
|
| 509 |
static gboolean |
| 510 |
property_set_leom_fn( |
| 511 |
Device *dself, |
| 512 |
DevicePropertyBase *base, |
| 513 |
GValue *val, |
| 514 |
PropertySurety surety, |
| 515 |
PropertySource source) |
| 516 |
{ |
| 517 |
VfsDevice *self = VFS_DEVICE(dself); |
| 518 |
|
| 519 |
self->leom = g_value_get_boolean(val); |
| 520 |
|
| 521 |
return device_simple_property_set_fn(dself, base, val, surety, source); |
| 522 |
} |
| 523 |
|
| 524 |
/* Drops everything associated with the volume file: Its name and fd. */ |
| 525 |
void |
| 526 |
vfs_release_file( |
| 527 |
Device *dself) |
| 528 |
{ |
| 529 |
VfsDevice *self = VFS_DEVICE(dself); |
| 530 |
|
| 531 |
/* Doesn't hurt. */ |
| 532 |
if (self->open_file_fd != -1) { |
| 533 |
robust_close(self->open_file_fd); |
| 534 |
self->open_file_fd = -1; |
| 535 |
} |
| 536 |
amfree(self->file_name); |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
static void vfs_device_finalize( |
| 541 |
GObject *obj_self) |
| 542 |
{ |
| 543 |
VfsDevice *self = VFS_DEVICE (obj_self); |
| 544 |
Device *dself = (Device*)self; |
| 545 |
|
| 546 |
if (dself->access_mode != ACCESS_NULL) { |
| 547 |
device_finish(dself); |
| 548 |
} |
| 549 |
|
| 550 |
if(G_OBJECT_CLASS(parent_class)->finalize) |
| 551 |
(* G_OBJECT_CLASS(parent_class)->finalize)(obj_self); |
| 552 |
|
| 553 |
amfree(self->dir_name); |
| 554 |
|
| 555 |
self->release_file(dself); |
| 556 |
} |
| 557 |
|
| 558 |
static Device *vfs_device_factory( |
| 559 |
char *device_name, |
| 560 |
char *device_type, |
| 561 |
char *device_node) |
| 562 |
{ |
| 563 |
Device * rval; |
| 564 |
g_assert(g_str_equal(device_type, "file")); |
| 565 |
rval = DEVICE(g_object_new(TYPE_VFS_DEVICE, NULL)); |
| 566 |
device_open_device(rval, device_name, device_type, device_node); |
| 567 |
return rval; |
| 568 |
} |
| 569 |
|
| 570 |
static gboolean check_is_dir( |
| 571 |
VfsDevice *self, |
| 572 |
const char *name) |
| 573 |
{ |
| 574 |
Device *dself = DEVICE(self); |
| 575 |
struct stat dir_status; |
| 576 |
|
| 577 |
if (stat(name, &dir_status) < 0) { |
| 578 |
#ifdef EINTR |
| 579 |
if (errno == EINTR) { |
| 580 |
return check_is_dir(self, name); |
| 581 |
} |
| 582 |
#endif /* EINTR */ |
| 583 |
device_set_error(dself, |
| 584 |
g_strdup_printf(_("Error checking directory %s: %s"), name, strerror(errno)), |
| 585 |
DEVICE_STATUS_DEVICE_ERROR); |
| 586 |
return FALSE; |
| 587 |
} else if (!S_ISDIR(dir_status.st_mode)) { |
| 588 |
device_set_error(dself, |
| 589 |
g_strdup_printf(_("VFS Device path %s is not a directory"), name), |
| 590 |
DEVICE_STATUS_DEVICE_ERROR); |
| 591 |
return FALSE; |
| 592 |
} else { |
| 593 |
return TRUE; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
typedef struct { |
| 598 |
VfsDevice * self; |
| 599 |
int count; |
| 600 |
char * result; |
| 601 |
} fnfn_data; |
| 602 |
|
| 603 |
/* A SearchDirectoryFunctor. */ |
| 604 |
static gboolean |
| 605 |
file_number_to_file_name_functor(const char *filename, |
| 606 |
gpointer datap) { |
| 607 |
char *result_tmp; |
| 608 |
struct stat file_status; |
| 609 |
fnfn_data *data = (fnfn_data*)datap; |
| 610 |
|
| 611 |
result_tmp = g_strjoin(NULL, data->self->dir_name, "/", filename, NULL); |
| 612 |
|
| 613 |
/* Just to be thorough, let's check that it's a real |
| 614 |
file. */ |
| 615 |
if (0 != stat(result_tmp, &file_status)) { |
| 616 |
g_warning(_("Cannot stat file %s (%s), ignoring it"), result_tmp, strerror(errno)); |
| 617 |
} else if (!S_ISREG(file_status.st_mode)) { |
| 618 |
g_warning(_("%s is not a regular file, ignoring it"), result_tmp); |
| 619 |
} else { |
| 620 |
data->count ++; |
| 621 |
if (data->result == NULL) { |
| 622 |
data->result = result_tmp; |
| 623 |
result_tmp = NULL; |
| 624 |
} |
| 625 |
} |
| 626 |
amfree(result_tmp); |
| 627 |
return TRUE; |
| 628 |
} |
| 629 |
|
| 630 |
/* This function finds the filename for a given file number. We search |
| 631 |
* for a filesystem file matching the regex /^0*$device_file\./; if |
| 632 |
* there is more than one such file we make a warning and take an |
| 633 |
* arbitrary one. */ |
| 634 |
static char * |
| 635 |
file_number_to_file_name( |
| 636 |
VfsDevice *self, |
| 637 |
guint device_file) |
| 638 |
{ |
| 639 |
char *regex; |
| 640 |
fnfn_data data; |
| 641 |
|
| 642 |
data.self = self; |
| 643 |
data.count = 0; |
| 644 |
data.result = NULL; |
| 645 |
|
| 646 |
regex = g_strdup_printf("^0*%u\\.", device_file); |
| 647 |
|
| 648 |
search_vfs_directory(self, regex, |
| 649 |
file_number_to_file_name_functor, &data); |
| 650 |
|
| 651 |
amfree(regex); |
| 652 |
|
| 653 |
if (data.count == 0) { |
| 654 |
g_assert(data.result == NULL); |
| 655 |
return NULL; |
| 656 |
} else if (data.count > 1) { |
| 657 |
g_warning("Found multiple names for file number %d, choosing file %s", |
| 658 |
device_file, data.result); |
| 659 |
return data.result; |
| 660 |
} else { |
| 661 |
g_assert(data.result != NULL); |
| 662 |
return data.result; |
| 663 |
} |
| 664 |
g_assert_not_reached(); |
| 665 |
} |
| 666 |
|
| 667 |
/* This function returns the dynamically-allocated lockfile name for a |
| 668 |
given file number. */ |
| 669 |
/* |
| 670 |
static char * lockfile_name(VfsDevice * self, guint number) { |
| 671 |
return g_strdup_printf("%s/%05d-lock", self->dir_name, number); |
| 672 |
} |
| 673 |
*/ |
| 674 |
|
| 675 |
/* Does what you expect. If the lock already exists, it is released |
| 676 |
* and regained, in case the mode is changing. |
| 677 |
* The file field has several options: |
| 678 |
* - file > 0: Open a lock on a real volume file. |
| 679 |
* - file = 0: Open the volume lock as a volume file (for setup). |
| 680 |
* - file < 0: Open the volume lock as a volume lock (persistantly). |
| 681 |
*/ |
| 682 |
static gboolean open_lock(G_GNUC_UNUSED VfsDevice * self, |
| 683 |
G_GNUC_UNUSED int file, |
| 684 |
G_GNUC_UNUSED gboolean exclusive) |
| 685 |
{ |
| 686 |
/* At the moment, file locking is horribly broken. */ |
| 687 |
return TRUE; |
| 688 |
} |
| 689 |
|
| 690 |
/* For now, does it the bad way. */ |
| 691 |
static void promote_volume_lock(VfsDevice * self G_GNUC_UNUSED) { |
| 692 |
} |
| 693 |
|
| 694 |
static void demote_volume_lock(VfsDevice * self G_GNUC_UNUSED) { |
| 695 |
} |
| 696 |
|
| 697 |
/* A SearchDirectoryFunctor */ |
| 698 |
static gboolean |
| 699 |
update_volume_size_functor(const char *filename, |
| 700 |
gpointer user_data) |
| 701 |
{ |
| 702 |
char *full_filename; |
| 703 |
struct stat stat_buf; |
| 704 |
VfsDevice *self = VFS_DEVICE(user_data); |
| 705 |
|
| 706 |
full_filename = g_strjoin(NULL, self->dir_name, "/", filename, NULL); |
| 707 |
|
| 708 |
if (stat(full_filename, &stat_buf) < 0) { |
| 709 |
/* Log it and keep going. */ |
| 710 |
g_warning(_("Couldn't stat file %s: %s"), full_filename, strerror(errno)); |
| 711 |
amfree(full_filename); |
| 712 |
return TRUE; |
| 713 |
} |
| 714 |
|
| 715 |
amfree(full_filename); |
| 716 |
self->volume_bytes += stat_buf.st_size; |
| 717 |
|
| 718 |
return TRUE; |
| 719 |
} |
| 720 |
|
| 721 |
static void |
| 722 |
vfs_update_volume_size( |
| 723 |
Device *dself) |
| 724 |
{ |
| 725 |
VfsDevice *self = VFS_DEVICE(dself); |
| 726 |
self->volume_bytes = 0; |
| 727 |
search_vfs_directory(self, "^[0-9]+\\.", |
| 728 |
update_volume_size_functor, self); |
| 729 |
|
| 730 |
} |
| 731 |
|
| 732 |
static void |
| 733 |
vfs_device_open_device( |
| 734 |
Device *dself, |
| 735 |
char *device_name, |
| 736 |
char *device_type, |
| 737 |
char *device_node) |
| 738 |
{ |
| 739 |
VfsDevice *self = VFS_DEVICE(dself); |
| 740 |
struct stat dir_status; |
| 741 |
|
| 742 |
dself->min_block_size = VFS_DEVICE_MIN_BLOCK_SIZE; |
| 743 |
dself->max_block_size = VFS_DEVICE_MAX_BLOCK_SIZE; |
| 744 |
dself->block_size = VFS_DEVICE_DEFAULT_BLOCK_SIZE; |
| 745 |
|
| 746 |
/* We don't have to free this ourselves; it will be freed by |
| 747 |
* vfs_device_finalize whether we succeed here or not. */ |
| 748 |
switch (self->use_data) { |
| 749 |
case 0: self->dir_name = g_strconcat(device_node, "/", NULL); |
| 750 |
break; |
| 751 |
case 1: self->dir_name = g_strconcat(device_node, "/data/", NULL); |
| 752 |
break; |
| 753 |
case 2: self->dir_name = g_strconcat(device_node, "/data/", NULL); |
| 754 |
if (stat(self->dir_name, &dir_status) != 0) { |
| 755 |
g_free(self->dir_name); |
| 756 |
self->dir_name = g_strconcat(device_node, "/", NULL); |
| 757 |
} |
| 758 |
break; |
| 759 |
} |
| 760 |
g_debug("dir_name: %s", self->dir_name); |
| 761 |
|
| 762 |
if (parent_class->open_device) { |
| 763 |
parent_class->open_device(dself, device_name, device_type, device_node); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
/* A SearchDirectoryFunctor */ |
| 768 |
static gboolean |
| 769 |
delete_vfs_files_functor( |
| 770 |
const char *filename, |
| 771 |
gpointer user_data) |
| 772 |
{ |
| 773 |
VfsDevice *self = VFS_DEVICE(user_data); |
| 774 |
Device *dself = DEVICE(self); |
| 775 |
char *path_name; |
| 776 |
gboolean ret = TRUE; |
| 777 |
|
| 778 |
/* Skip the volume lock. */ |
| 779 |
if (g_str_equal(filename, VOLUME_LOCKFILE_NAME)) |
| 780 |
return TRUE; |
| 781 |
|
| 782 |
path_name = g_strjoin(NULL, self->dir_name, "/", filename, NULL); |
| 783 |
if (unlink(path_name) != 0) { |
| 784 |
device_set_error(dself, |
| 785 |
g_strdup_printf("Error unlinking %s: %s", path_name, strerror(errno)), |
| 786 |
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR); |
| 787 |
ret = FALSE; |
| 788 |
} |
| 789 |
amfree(path_name); |
| 790 |
return ret; |
| 791 |
} |
| 792 |
|
| 793 |
/* delete_vfs_files deletes all VfsDevice files in the directory except the |
| 794 |
volume lockfile. */ |
| 795 |
void |
| 796 |
delete_vfs_files( |
| 797 |
VfsDevice *self) |
| 798 |
{ |
| 799 |
g_assert(self != NULL); |
| 800 |
|
| 801 |
/* This function assumes that the volume is locked! */ |
| 802 |
search_vfs_directory(self, VFS_DEVICE_FILE_REGEX, |
| 803 |
delete_vfs_files_functor, self); |
| 804 |
} |
| 805 |
|
| 806 |
/* This is a functor suitable for search_directory. It simply prints a |
| 807 |
warning. It also dodges the volume lockfile. */ |
| 808 |
static gboolean check_dir_empty_functor(const char * filename, |
| 809 |
gpointer user_data) { |
| 810 |
VfsDevice * self = VFS_DEVICE(user_data); |
| 811 |
Device *dself = DEVICE(self); |
| 812 |
char * path_name; |
| 813 |
|
| 814 |
if (g_str_equal(filename, VOLUME_LOCKFILE_NAME)) |
| 815 |
return TRUE; |
| 816 |
|
| 817 |
path_name = g_strjoin(NULL, self->dir_name, "/", filename, NULL); |
| 818 |
|
| 819 |
device_set_error(dself, |
| 820 |
g_strdup_printf("Found spurious storage file %s", path_name), |
| 821 |
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR); |
| 822 |
|
| 823 |
amfree(path_name); |
| 824 |
return FALSE; |
| 825 |
} |
| 826 |
|
| 827 |
/* This function is used to write volume and dump headers. */ |
| 828 |
gboolean |
| 829 |
vfs_write_amanda_header( |
| 830 |
VfsDevice *self, |
| 831 |
const dumpfile_t *header) |
| 832 |
{ |
| 833 |
char *label_buffer; |
| 834 |
IoResult result; |
| 835 |
Device *dself = DEVICE(self); |
| 836 |
|
| 837 |
g_assert(header != NULL); |
| 838 |
|
| 839 |
label_buffer = device_build_amanda_header(dself, header, NULL); |
| 840 |
if (!label_buffer) { |
| 841 |
amfree(label_buffer); |
| 842 |
device_set_error(dself, |
| 843 |
g_strdup(_("Amanda file header won't fit in a single block!")), |
| 844 |
DEVICE_STATUS_DEVICE_ERROR); |
| 845 |
return FALSE; |
| 846 |
} |
| 847 |
|
| 848 |
result = vfs_device_robust_write(self, label_buffer, VFS_DEVICE_LABEL_SIZE); |
| 849 |
/* vfs_device_robust_write sets error status if necessary */ |
| 850 |
amfree(label_buffer); |
| 851 |
return (result == RESULT_SUCCESS); |
| 852 |
} |
| 853 |
|
| 854 |
/* clear_and_label will erase the contents of the directory, and write |
| 855 |
* this label in its place. This function assumes we already have a volume |
| 856 |
* label write lock in place (e.g., promote_lock() has been called.) */ |
| 857 |
static gboolean |
| 858 |
vfs_clear_and_prepare_label( |
| 859 |
Device *dself, |
| 860 |
char *label, |
| 861 |
char *timestamp) |
| 862 |
{ |
| 863 |
dumpfile_t *label_header; |
| 864 |
VfsDevice *self = VFS_DEVICE(dself); |
| 865 |
|
| 866 |
self->release_file(dself); |
| 867 |
|
| 868 |
/* Delete any extant data, except our volume lock. */ |
| 869 |
delete_vfs_files(self); |
| 870 |
if (device_in_error(dself)) return FALSE; |
| 871 |
|
| 872 |
/* Print warnings about any remaining files. */ |
| 873 |
search_vfs_directory(self, VFS_DEVICE_FILE_REGEX, |
| 874 |
check_dir_empty_functor, self); |
| 875 |
if (device_in_error(dself)) return FALSE; |
| 876 |
|
| 877 |
self->file_name = g_strdup_printf("%s/00000.%s", self->dir_name, label); |
| 878 |
|
| 879 |
self->open_file_fd = robust_open(self->file_name, |
| 880 |
O_CREAT | O_EXCL | O_WRONLY, |
| 881 |
VFS_DEVICE_CREAT_MODE); |
| 882 |
if (self->open_file_fd < 0) { |
| 883 |
device_set_error(dself, |
| 884 |
g_strdup_printf(_("Can't open file %s: %s"), self->file_name, strerror(errno)), |
| 885 |
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR); |
| 886 |
return FALSE; |
| 887 |
} |
| 888 |
|
| 889 |
label_header = make_tapestart_header(DEVICE(self), label, timestamp); |
| 890 |
if (!vfs_write_amanda_header(self, label_header)) { |
| 891 |
/* vfs_write_amanda_header sets error status if necessary */ |
| 892 |
dumpfile_free(label_header); |
| 893 |
aclose(self->open_file_fd); |
| 894 |
return FALSE; |
| 895 |
} |
| 896 |
dumpfile_free(dself->volume_header); |
| 897 |
dself->header_block_size = VFS_DEVICE_LABEL_SIZE; |
| 898 |
dself->volume_header = label_header; |
| 899 |
self->volume_bytes = VFS_DEVICE_LABEL_SIZE; |
| 900 |
return TRUE; |
| 901 |
} |
| 902 |
|
| 903 |
/* Just like search_directory, but returns -1 in the event of an error */ |
| 904 |
static int |
| 905 |
search_vfs_directory( |
| 906 |
VfsDevice *self, |
| 907 |
const char * regex, |
| 908 |
SearchDirectoryFunctor functor, |
| 909 |
gpointer user_data) |
| 910 |
{ |
| 911 |
Device *dself = DEVICE(self); |
| 912 |
DIR *dir_handle; |
| 913 |
int result = -1; |
| 914 |
|
| 915 |
dir_handle = opendir(self->dir_name); |
| 916 |
if (dir_handle == NULL) { |
| 917 |
device_set_error(dself, |
| 918 |
g_strdup_printf(_("Couldn't open device %s (directory %s) for reading: %s"), |
| 919 |
dself->device_name, self->dir_name, strerror(errno)), |
| 920 |
DEVICE_STATUS_DEVICE_ERROR); |
| 921 |
goto error; |
| 922 |
} |
| 923 |
|
| 924 |
/* TODO: is this the right moment to acquire a lock?? */ |
| 925 |
|
| 926 |
result = search_directory(dir_handle, regex, functor, user_data); |
| 927 |
|
| 928 |
error: |
| 929 |
if (dir_handle) |
| 930 |
closedir(dir_handle); |
| 931 |
return result; |
| 932 |
} |
| 933 |
|
| 934 |
static gboolean |
| 935 |
vfs_validate( |
| 936 |
Device *dself) |
| 937 |
{ |
| 938 |
VfsDevice *self = VFS_DEVICE(dself); |
| 939 |
return check_is_dir(self, self->dir_name); |
| 940 |
} |
| 941 |
|
| 942 |
static DeviceStatusFlags |
| 943 |
vfs_device_read_label( |
| 944 |
Device *dself) |
| 945 |
{ |
| 946 |
VfsDevice *self = VFS_DEVICE(dself); |
| 947 |
dumpfile_t *amanda_header; |
| 948 |
|
| 949 |
g_assert(self != NULL); |
| 950 |
g_assert(!dself->in_file); |
| 951 |
|
| 952 |
if (self->dir_name && !self->validate(dself)) { |
| 953 |
/* error message set by validate */ |
| 954 |
return dself->status; |
| 955 |
} |
| 956 |
|
| 957 |
amfree(dself->volume_label); |
| 958 |
amfree(dself->volume_time); |
| 959 |
dumpfile_free(dself->volume_header); |
| 960 |
dself->volume_header = NULL; |
| 961 |
|
| 962 |
if (device_in_error(dself)) return dself->status; |
| 963 |
|
| 964 |
amanda_header = dself->volume_header = device_seek_file(dself, 0); |
| 965 |
self->release_file(dself); |
| 966 |
if (amanda_header == NULL) { |
| 967 |
/* This means an error occured getting locks or opening the header |
| 968 |
* file. */ |
| 969 |
// device_set_error(dself, |
| 970 |
// g_strdup("Error loading device header -- unlabeled volume?"), |
| 971 |
// DEVICE_STATUS_DEVICE_ERROR |
| 972 |
// | DEVICE_STATUS_VOLUME_ERROR |
| 973 |
// | DEVICE_STATUS_VOLUME_UNLABELED); |
| 974 |
return dself->status; |
| 975 |
} |
| 976 |
|
| 977 |
if (amanda_header->type != F_TAPESTART && |
| 978 |
amanda_header->type != F_EMPTY) { |
| 979 |
/* This is an error, and should not happen. */ |
| 980 |
device_set_error(dself, |
| 981 |
g_strdup(_("Got a bad volume label")), |
| 982 |
DEVICE_STATUS_VOLUME_ERROR); |
| 983 |
amfree(amanda_header); |
| 984 |
return dself->status; |
| 985 |
} |
| 986 |
|
| 987 |
/* self->volume_header is already set */ |
| 988 |
|
| 989 |
if (amanda_header->type == F_TAPESTART) { |
| 990 |
dself->volume_label = g_strdup(amanda_header->name); |
| 991 |
dself->volume_time = g_strdup(amanda_header->datestamp); |
| 992 |
device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS); |
| 993 |
} |
| 994 |
|
| 995 |
self->update_volume_size(dself); |
| 996 |
|
| 997 |
return dself->status; |
| 998 |
} |
| 999 |
|
| 1000 |
static DeviceWriteResult |
| 1001 |
vfs_device_write_block( |
| 1002 |
Device * dself, |
| 1003 |
guint size, |
| 1004 |
gpointer data) |
| 1005 |
{ |
| 1006 |
VfsDevice * self = VFS_DEVICE(dself); |
| 1007 |
IoResult result; |
| 1008 |
|
| 1009 |
if (device_in_error(self)) return WRITE_FAILED; |
| 1010 |
|
| 1011 |
g_assert(self->open_file_fd >= 0); |
| 1012 |
|
| 1013 |
if (check_at_leom(self, size)) |
| 1014 |
dself->is_eom = TRUE; |
| 1015 |
|
| 1016 |
if (check_at_peom(self, size)) { |
| 1017 |
/* check_at_peom() only checks against MAX_VOLUME_USAGE limit */ |
| 1018 |
DeviceWriteResult dwr = self->leom ? WRITE_FULL : WRITE_FAILED; |
| 1019 |
dself->is_eom = TRUE; |
| 1020 |
device_set_error(dself, |
| 1021 |
g_strdup(_("No space left on device: more than MAX_VOLUME_USAGE bytes written")), |
| 1022 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1023 |
if (fsync(self->open_file_fd) == -1) { |
| 1024 |
g_debug("fsync failed: %s", strerror(errno)); |
| 1025 |
dwr = WRITE_FAILED; |
| 1026 |
} |
| 1027 |
return dwr; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (self->slow_write) { |
| 1031 |
self->slow_count++; |
| 1032 |
if (self->slow_count >= 2) { |
| 1033 |
sleep(1); |
| 1034 |
self->slow_count = 0; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
result = vfs_device_robust_write(self, data, size); |
| 1039 |
|
| 1040 |
if (result == RESULT_NO_SPACE) { |
| 1041 |
DeviceWriteResult dwr = self->leom ? WRITE_SPACE : WRITE_FAILED; |
| 1042 |
if (ftruncate(self->open_file_fd, |
| 1043 |
dself->bytes_written + VFS_DEVICE_LABEL_SIZE) == -1) { |
| 1044 |
g_debug("ftruncate failed: %s", strerror(errno)); |
| 1045 |
dwr = WRITE_FAILED; |
| 1046 |
} |
| 1047 |
if (lseek(self->open_file_fd, |
| 1048 |
dself->bytes_written + VFS_DEVICE_LABEL_SIZE, |
| 1049 |
SEEK_SET) == -1) { |
| 1050 |
g_debug("ftruncate failed: %s", strerror(errno)); |
| 1051 |
dwr = WRITE_FAILED; |
| 1052 |
} |
| 1053 |
if (fsync(self->open_file_fd) == -1) { |
| 1054 |
g_debug("fsync failed: %s", strerror(errno)); |
| 1055 |
dwr = WRITE_FAILED; |
| 1056 |
} |
| 1057 |
return dwr; |
| 1058 |
} else if (result != RESULT_SUCCESS) { |
| 1059 |
/* vfs_device_robust_write set error status appropriately */ |
| 1060 |
return WRITE_FAILED; |
| 1061 |
} |
| 1062 |
|
| 1063 |
self->volume_bytes += size; |
| 1064 |
self->checked_bytes_used += size; |
| 1065 |
dself->block++; |
| 1066 |
g_mutex_lock(dself->device_mutex); |
| 1067 |
dself->bytes_written += size; |
| 1068 |
g_mutex_unlock(dself->device_mutex); |
| 1069 |
|
| 1070 |
return WRITE_SUCCEED; |
| 1071 |
} |
| 1072 |
|
| 1073 |
static int |
| 1074 |
vfs_device_read_block( |
| 1075 |
Device *dself, |
| 1076 |
gpointer data, |
| 1077 |
int *size_req, |
| 1078 |
int max_block G_GNUC_UNUSED) |
| 1079 |
{ |
| 1080 |
VfsDevice * self; |
| 1081 |
int size; |
| 1082 |
IoResult result; |
| 1083 |
|
| 1084 |
self = VFS_DEVICE(dself); |
| 1085 |
|
| 1086 |
if (device_in_error(self)) return -1; |
| 1087 |
|
| 1088 |
if (data == NULL || (gsize)*size_req < dself->block_size) { |
| 1089 |
/* Just a size query. */ |
| 1090 |
g_assert(dself->block_size < INT_MAX); |
| 1091 |
*size_req = (int)dself->block_size; |
| 1092 |
return 0; |
| 1093 |
} |
| 1094 |
|
| 1095 |
size = dself->block_size; |
| 1096 |
result = vfs_device_robust_read(self, data, &size); |
| 1097 |
switch (result) { |
| 1098 |
case RESULT_SUCCESS: |
| 1099 |
*size_req = size; |
| 1100 |
g_mutex_lock(dself->device_mutex); |
| 1101 |
dself->bytes_read += size; |
| 1102 |
g_mutex_unlock(dself->device_mutex); |
| 1103 |
dself->block++; |
| 1104 |
return size; |
| 1105 |
case RESULT_NO_DATA: |
| 1106 |
dself->is_eof = TRUE; |
| 1107 |
g_mutex_lock(dself->device_mutex); |
| 1108 |
dself->in_file = FALSE; |
| 1109 |
g_mutex_unlock(dself->device_mutex); |
| 1110 |
device_set_error(dself, |
| 1111 |
g_strdup(_("EOF")), |
| 1112 |
DEVICE_STATUS_SUCCESS); |
| 1113 |
return -1; |
| 1114 |
default: |
| 1115 |
device_set_error(dself, |
| 1116 |
g_strdup_printf(_("Error reading from data file: %s"), strerror(errno)), |
| 1117 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1118 |
return -1; |
| 1119 |
} |
| 1120 |
|
| 1121 |
g_assert_not_reached(); |
| 1122 |
} |
| 1123 |
|
| 1124 |
static gboolean |
| 1125 |
vfs_device_start( |
| 1126 |
Device *dself, |
| 1127 |
DeviceAccessMode mode, |
| 1128 |
char *label, |
| 1129 |
char *timestamp) |
| 1130 |
{ |
| 1131 |
VfsDevice * self = VFS_DEVICE(dself); |
| 1132 |
|
| 1133 |
if (!self->validate(dself)) { |
| 1134 |
/* error message set by validate */ |
| 1135 |
return FALSE; |
| 1136 |
} |
| 1137 |
|
| 1138 |
g_mutex_lock(dself->device_mutex); |
| 1139 |
dself->in_file = FALSE; |
| 1140 |
g_mutex_unlock(dself->device_mutex); |
| 1141 |
|
| 1142 |
if (mode == ACCESS_WRITE) { |
| 1143 |
promote_volume_lock(self); |
| 1144 |
if (!self->clear_and_prepare_label(dself, label, timestamp)) { |
| 1145 |
/* clear_and_prepare_label sets error status if necessary */ |
| 1146 |
demote_volume_lock(self); |
| 1147 |
return FALSE; |
| 1148 |
} |
| 1149 |
|
| 1150 |
g_free(dself->volume_label); |
| 1151 |
dself->volume_label = g_strdup(label); |
| 1152 |
g_free(dself->volume_time); |
| 1153 |
dself->volume_time = g_strdup(timestamp); |
| 1154 |
|
| 1155 |
/* unset the VOLUME_UNLABELED flag, if it was set */ |
| 1156 |
device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS); |
| 1157 |
|
| 1158 |
demote_volume_lock(self); |
| 1159 |
dself->access_mode = mode; |
| 1160 |
} else { |
| 1161 |
if (dself->volume_label == NULL && device_read_label(dself) != DEVICE_STATUS_SUCCESS) { |
| 1162 |
/* device_read_label already set our error message */ |
| 1163 |
return FALSE; |
| 1164 |
} else { |
| 1165 |
dself->access_mode = mode; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
self->release_file(dself); |
| 1170 |
|
| 1171 |
return TRUE; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static gboolean |
| 1175 |
vfs_device_finish( |
| 1176 |
Device *dself) |
| 1177 |
{ |
| 1178 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1179 |
|
| 1180 |
self->release_file(dself); |
| 1181 |
|
| 1182 |
dself->access_mode = ACCESS_NULL; |
| 1183 |
g_mutex_lock(dself->device_mutex); |
| 1184 |
dself->in_file = FALSE; |
| 1185 |
g_mutex_unlock(dself->device_mutex); |
| 1186 |
|
| 1187 |
if (device_in_error(self)) return FALSE; |
| 1188 |
|
| 1189 |
return TRUE; |
| 1190 |
} |
| 1191 |
|
| 1192 |
typedef struct { |
| 1193 |
VfsDevice *self; |
| 1194 |
int rval; |
| 1195 |
} glfn_data; |
| 1196 |
|
| 1197 |
/* A SearchDirectoryFunctor. */ |
| 1198 |
static gboolean |
| 1199 |
get_last_file_number_functor( |
| 1200 |
const char *filename, |
| 1201 |
gpointer datap) |
| 1202 |
{ |
| 1203 |
guint64 file; |
| 1204 |
glfn_data *data = (glfn_data *)datap; |
| 1205 |
|
| 1206 |
file = g_ascii_strtoull(filename, NULL, 10); /* Guaranteed to work. */ |
| 1207 |
if (file > G_MAXINT) { |
| 1208 |
g_warning(_("Super-large device file %s found, ignoring"), filename); |
| 1209 |
return TRUE; |
| 1210 |
} |
| 1211 |
/* This condition is needlessly complex due to sign issues. */ |
| 1212 |
if (data->rval < 0 || ((guint)data->rval) < file) { |
| 1213 |
data->rval = file; |
| 1214 |
} |
| 1215 |
return TRUE; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static gint |
| 1219 |
get_last_file_number( |
| 1220 |
VfsDevice *self) |
| 1221 |
{ |
| 1222 |
glfn_data data; |
| 1223 |
int count; |
| 1224 |
Device *dself = DEVICE(self); |
| 1225 |
data.self = self; |
| 1226 |
data.rval = -1; |
| 1227 |
|
| 1228 |
count = search_vfs_directory(self, "^[0-9]+\\.", |
| 1229 |
get_last_file_number_functor, &data); |
| 1230 |
|
| 1231 |
if (count <= 0) { |
| 1232 |
/* Somebody deleted something important while we weren't looking. */ |
| 1233 |
device_set_error(dself, |
| 1234 |
g_strdup(_("Error identifying VFS device contents!")), |
| 1235 |
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR); |
| 1236 |
return -1; |
| 1237 |
} else { |
| 1238 |
g_assert(data.rval >= 0); |
| 1239 |
} |
| 1240 |
|
| 1241 |
return data.rval; |
| 1242 |
} |
| 1243 |
|
| 1244 |
typedef struct { |
| 1245 |
VfsDevice * self; |
| 1246 |
guint request; |
| 1247 |
int best_found; |
| 1248 |
} gnfn_data; |
| 1249 |
|
| 1250 |
/* A SearchDirectoryFunctor. */ |
| 1251 |
static gboolean |
| 1252 |
get_next_file_number_functor( |
| 1253 |
const char *filename, |
| 1254 |
gpointer datap) |
| 1255 |
{ |
| 1256 |
guint file; |
| 1257 |
gnfn_data *data = (gnfn_data*)datap; |
| 1258 |
|
| 1259 |
file = g_ascii_strtoull(filename, NULL, 10); /* Guaranteed to work. */ |
| 1260 |
if (file > G_MAXINT) { |
| 1261 |
g_warning(_("Super-large device file %s found, ignoring"), filename); |
| 1262 |
return TRUE; |
| 1263 |
} |
| 1264 |
/* This condition is needlessly complex due to sign issues. */ |
| 1265 |
if (file >= data->request && |
| 1266 |
(data->best_found < 0 || file < (guint)data->best_found)) { |
| 1267 |
data->best_found = file; |
| 1268 |
} |
| 1269 |
return TRUE; |
| 1270 |
} |
| 1271 |
|
| 1272 |
/* Returns the file number equal to or greater than the given requested |
| 1273 |
* file number. */ |
| 1274 |
static gint |
| 1275 |
get_next_file_number( |
| 1276 |
VfsDevice *self, |
| 1277 |
guint request) |
| 1278 |
{ |
| 1279 |
gnfn_data data; |
| 1280 |
int count; |
| 1281 |
Device *dself = DEVICE(self); |
| 1282 |
data.self = self; |
| 1283 |
data.request = request; |
| 1284 |
data.best_found = -1; |
| 1285 |
|
| 1286 |
count = search_vfs_directory(self, "^[0-9]+\\.", |
| 1287 |
get_next_file_number_functor, &data); |
| 1288 |
|
| 1289 |
if (count <= 0) { |
| 1290 |
/* Somebody deleted something important while we weren't looking. */ |
| 1291 |
device_set_error(dself, |
| 1292 |
g_strdup(_("Error identifying VFS device contents!")), |
| 1293 |
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR); |
| 1294 |
return -1; |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* Could be -1. */ |
| 1298 |
return data.best_found; |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* Finds the file number, acquires a lock, and returns the new file name. */ |
| 1302 |
static char * |
| 1303 |
make_new_file_name( |
| 1304 |
VfsDevice *self, |
| 1305 |
const dumpfile_t *ji) |
| 1306 |
{ |
| 1307 |
char * rval; |
| 1308 |
char *base, *sanitary_base; |
| 1309 |
int fileno; |
| 1310 |
|
| 1311 |
for (;;) { |
| 1312 |
fileno = 1 + get_last_file_number(self); |
| 1313 |
if (fileno <= 0) |
| 1314 |
return NULL; |
| 1315 |
|
| 1316 |
if (open_lock(self, fileno, TRUE)) { |
| 1317 |
break; |
| 1318 |
} else { |
| 1319 |
continue; |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* record that we're at this filenum now */ |
| 1324 |
DEVICE(self)->file = fileno; |
| 1325 |
|
| 1326 |
base = g_strdup_printf("%05d.%s.%s.%d", fileno, ji->name, ji->disk, |
| 1327 |
ji->dumplevel); |
| 1328 |
sanitary_base = sanitise_filename(base); |
| 1329 |
amfree(base); |
| 1330 |
rval = g_strdup_printf("%s/%s", self->dir_name, sanitary_base); |
| 1331 |
amfree(sanitary_base); |
| 1332 |
return rval; |
| 1333 |
} |
| 1334 |
|
| 1335 |
static gboolean |
| 1336 |
vfs_device_start_file_open( |
| 1337 |
Device *dself, |
| 1338 |
dumpfile_t *ji) |
| 1339 |
{ |
| 1340 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1341 |
|
| 1342 |
self->file_name = make_new_file_name(self, ji); |
| 1343 |
if (self->file_name == NULL) { |
| 1344 |
device_set_error(dself, |
| 1345 |
g_strdup(_("Could not create header filename")), |
| 1346 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1347 |
return FALSE; |
| 1348 |
} |
| 1349 |
|
| 1350 |
self->open_file_fd = robust_open(self->file_name, |
| 1351 |
O_CREAT | O_EXCL | O_RDWR, |
| 1352 |
VFS_DEVICE_CREAT_MODE); |
| 1353 |
if (self->open_file_fd < 0) { |
| 1354 |
device_set_error(dself, |
| 1355 |
g_strdup_printf(_("Can't create file %s: %s"), self->file_name, strerror(errno)), |
| 1356 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1357 |
self->release_file(dself); |
| 1358 |
return FALSE; |
| 1359 |
} |
| 1360 |
|
| 1361 |
return TRUE; |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
static gboolean |
| 1366 |
vfs_device_start_file( |
| 1367 |
Device *dself, |
| 1368 |
dumpfile_t *ji) |
| 1369 |
{ |
| 1370 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1371 |
|
| 1372 |
dself->is_eom = FALSE; |
| 1373 |
|
| 1374 |
if (device_in_error(self)) return FALSE; |
| 1375 |
|
| 1376 |
/* set the blocksize in the header to 32k, since the VFS header is always |
| 1377 |
* 32k regardless of the block_size setting */ |
| 1378 |
ji->blocksize = 32768; |
| 1379 |
|
| 1380 |
if (check_at_leom(self, VFS_DEVICE_LABEL_SIZE)) |
| 1381 |
dself->is_eom = TRUE; |
| 1382 |
|
| 1383 |
if (check_at_peom(self, VFS_DEVICE_LABEL_SIZE)) { |
| 1384 |
/* check_at_peom() only checks against MAX_VOLUME_USAGE limit */ |
| 1385 |
dself->is_eom = TRUE; |
| 1386 |
device_set_error(dself, |
| 1387 |
g_strdup(_("No space left on device: more than MAX_VOLUME_USAGE bytes written")), |
| 1388 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1389 |
return FALSE; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* The basic idea here is thus: |
| 1393 |
1) Try to get a lock on the next filenumber. |
| 1394 |
2) If that fails, update our idea of "next filenumber" and try again. |
| 1395 |
3) Then open the file itself. |
| 1396 |
4) Write the label. |
| 1397 |
5) Chain up. */ |
| 1398 |
|
| 1399 |
if (!self->device_start_file_open(dself, ji)) { |
| 1400 |
return FALSE; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (!vfs_write_amanda_header(self, ji)) { |
| 1404 |
/* vfs_write_amanda_header sets error status if necessary */ |
| 1405 |
self->release_file(dself); |
| 1406 |
return FALSE; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* handle some accounting business */ |
| 1410 |
self->volume_bytes += VFS_DEVICE_LABEL_SIZE; |
| 1411 |
self->checked_bytes_used += VFS_DEVICE_LABEL_SIZE; |
| 1412 |
dself->block = 0; |
| 1413 |
g_mutex_lock(dself->device_mutex); |
| 1414 |
dself->in_file = TRUE; |
| 1415 |
dself->bytes_written = 0; |
| 1416 |
g_mutex_unlock(dself->device_mutex); |
| 1417 |
/* make_new_file_name set dself->file for us */ |
| 1418 |
|
| 1419 |
return TRUE; |
| 1420 |
} |
| 1421 |
|
| 1422 |
static gboolean |
| 1423 |
vfs_device_finish_file( |
| 1424 |
Device *dself) |
| 1425 |
{ |
| 1426 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1427 |
|
| 1428 |
if (!dself->in_file) |
| 1429 |
return TRUE; |
| 1430 |
|
| 1431 |
g_mutex_lock(dself->device_mutex); |
| 1432 |
dself->in_file = FALSE; |
| 1433 |
g_mutex_unlock(dself->device_mutex); |
| 1434 |
|
| 1435 |
self->release_file(dself); |
| 1436 |
|
| 1437 |
if (device_in_error(self)) return FALSE; |
| 1438 |
|
| 1439 |
return TRUE; |
| 1440 |
} |
| 1441 |
|
| 1442 |
/* This function is used for two purposes, rather than one. In |
| 1443 |
* addition to its documented behavior, we also use it to open the |
| 1444 |
* volume label for reading at startup. In that second case, we avoid |
| 1445 |
* FdDevice-related side effects. */ |
| 1446 |
static dumpfile_t * |
| 1447 |
vfs_device_seek_file( |
| 1448 |
Device *dself, |
| 1449 |
guint requested_file) |
| 1450 |
{ |
| 1451 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1452 |
int file; |
| 1453 |
dumpfile_t * rval; |
| 1454 |
char header_buffer[VFS_DEVICE_LABEL_SIZE]; |
| 1455 |
int header_buffer_size = sizeof(header_buffer); |
| 1456 |
IoResult result; |
| 1457 |
|
| 1458 |
if (device_in_error(self)) return NULL; |
| 1459 |
|
| 1460 |
dself->is_eof = FALSE; |
| 1461 |
dself->block = 0; |
| 1462 |
g_mutex_lock(dself->device_mutex); |
| 1463 |
dself->in_file = FALSE; |
| 1464 |
dself->bytes_read = 0; |
| 1465 |
g_mutex_unlock(dself->device_mutex); |
| 1466 |
|
| 1467 |
self->release_file(dself); |
| 1468 |
|
| 1469 |
if (requested_file > 0) { |
| 1470 |
file = get_next_file_number(self, requested_file); |
| 1471 |
} else { |
| 1472 |
file = requested_file; |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (file < 0) { |
| 1476 |
/* Did they request one past the end? */ |
| 1477 |
char * tmp_file_name; |
| 1478 |
tmp_file_name = file_number_to_file_name(self, requested_file - 1); |
| 1479 |
if (tmp_file_name != NULL) { |
| 1480 |
free(tmp_file_name); |
| 1481 |
dself->file = requested_file; /* other attributes are already correct */ |
| 1482 |
return make_tapeend_header(); |
| 1483 |
} else { |
| 1484 |
device_set_error(dself, |
| 1485 |
g_strdup(_("Attempt to read past tape-end file")), |
| 1486 |
DEVICE_STATUS_SUCCESS); |
| 1487 |
return NULL; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (!open_lock(self, file, FALSE)) { |
| 1492 |
device_set_error(dself, |
| 1493 |
g_strdup(_("could not acquire lock")), |
| 1494 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1495 |
return NULL; |
| 1496 |
} |
| 1497 |
|
| 1498 |
self->file_name = file_number_to_file_name(self, file); |
| 1499 |
if (self->file_name == NULL) { |
| 1500 |
device_set_error(dself, |
| 1501 |
g_strdup_printf(_("File %d not found"), file), |
| 1502 |
file == 0 ? DEVICE_STATUS_VOLUME_UNLABELED |
| 1503 |
: DEVICE_STATUS_VOLUME_ERROR); |
| 1504 |
self->release_file(dself); |
| 1505 |
rval = g_new(dumpfile_t, 1); |
| 1506 |
fh_init(rval); |
| 1507 |
return rval; |
| 1508 |
} |
| 1509 |
|
| 1510 |
self->open_file_fd = robust_open(self->file_name, O_RDONLY, 0); |
| 1511 |
if (self->open_file_fd < 0) { |
| 1512 |
device_set_error(dself, |
| 1513 |
g_strdup_printf(_("Couldn't open file %s: %s"), self->file_name, strerror(errno)), |
| 1514 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1515 |
amfree(self->file_name); |
| 1516 |
self->release_file(dself); |
| 1517 |
return NULL; |
| 1518 |
} |
| 1519 |
|
| 1520 |
result = vfs_device_robust_read(self, header_buffer, |
| 1521 |
&header_buffer_size); |
| 1522 |
if (result != RESULT_SUCCESS) { |
| 1523 |
device_set_error(dself, |
| 1524 |
g_strdup_printf(_("Problem reading Amanda header: %s"), device_error(dself)), |
| 1525 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1526 |
self->release_file(dself); |
| 1527 |
return NULL; |
| 1528 |
} |
| 1529 |
|
| 1530 |
rval = g_new(dumpfile_t, 1); |
| 1531 |
parse_file_header(header_buffer, rval, header_buffer_size); |
| 1532 |
switch (rval->type) { |
| 1533 |
case F_DUMPFILE: |
| 1534 |
case F_CONT_DUMPFILE: |
| 1535 |
case F_SPLIT_DUMPFILE: |
| 1536 |
break; |
| 1537 |
|
| 1538 |
case F_TAPESTART: |
| 1539 |
/* file 0 should have a TAPESTART header; vfs_device_read_label |
| 1540 |
* uses this */ |
| 1541 |
if (requested_file == 0) |
| 1542 |
break; |
| 1543 |
/* FALLTHROUGH */ |
| 1544 |
|
| 1545 |
default: |
| 1546 |
device_set_error(dself, |
| 1547 |
g_strdup(_("Invalid amanda header while reading file header")), |
| 1548 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1549 |
amfree(rval); |
| 1550 |
self->release_file(dself); |
| 1551 |
return NULL; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* update our state */ |
| 1555 |
if (requested_file == 0) { |
| 1556 |
dself->header_block_size = header_buffer_size; |
| 1557 |
} |
| 1558 |
g_mutex_lock(dself->device_mutex); |
| 1559 |
if (file > 0) { |
| 1560 |
dself->in_file = TRUE; |
| 1561 |
} |
| 1562 |
dself->file = file; |
| 1563 |
g_mutex_unlock(dself->device_mutex); |
| 1564 |
|
| 1565 |
return rval; |
| 1566 |
} |
| 1567 |
|
| 1568 |
static gboolean |
| 1569 |
vfs_device_seek_block( |
| 1570 |
Device *dself, |
| 1571 |
guint64 block) |
| 1572 |
{ |
| 1573 |
VfsDevice * self = VFS_DEVICE(dself); |
| 1574 |
off_t result; |
| 1575 |
|
| 1576 |
g_assert(self->open_file_fd >= 0); |
| 1577 |
g_assert(sizeof(off_t) >= sizeof(guint64)); |
| 1578 |
if (device_in_error(self)) return FALSE; |
| 1579 |
|
| 1580 |
/* Pretty simple. We figure out the blocksize and use that. */ |
| 1581 |
result = lseek(self->open_file_fd, |
| 1582 |
(block) * dself->block_size + VFS_DEVICE_LABEL_SIZE, |
| 1583 |
SEEK_SET); |
| 1584 |
|
| 1585 |
dself->block = block; |
| 1586 |
|
| 1587 |
if (result == (off_t)(-1)) { |
| 1588 |
device_set_error(dself, |
| 1589 |
g_strdup_printf(_("Error seeking within file: %s"), strerror(errno)), |
| 1590 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1591 |
return FALSE; |
| 1592 |
} |
| 1593 |
|
| 1594 |
return TRUE; |
| 1595 |
} |
| 1596 |
|
| 1597 |
static gboolean |
| 1598 |
try_unlink( |
| 1599 |
const char *file) |
| 1600 |
{ |
| 1601 |
if (unlink(file) < 0) { |
| 1602 |
return FALSE; |
| 1603 |
} else { |
| 1604 |
return TRUE; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
static gboolean |
| 1609 |
check_at_leom( |
| 1610 |
VfsDevice *self, |
| 1611 |
guint64 size) |
| 1612 |
{ |
| 1613 |
gboolean recheck = FALSE; |
| 1614 |
guint64 est_avail_now; |
| 1615 |
struct fs_usage fsusage; |
| 1616 |
guint64 block_size = DEVICE(self)->block_size; |
| 1617 |
guint64 eom_warning_buffer = EOM_EARLY_WARNING_ZONE_BLOCKS * block_size; |
| 1618 |
|
| 1619 |
if (!self->leom || !self->monitor_free_space) |
| 1620 |
return FALSE; |
| 1621 |
|
| 1622 |
/* handle VOLUME_LIMIT */ |
| 1623 |
if (self->enforce_volume_limit && self->volume_limit && |
| 1624 |
self->volume_bytes + size + eom_warning_buffer > self->volume_limit) { |
| 1625 |
return TRUE; |
| 1626 |
} |
| 1627 |
|
| 1628 |
/* handle actual filesystem available space, using some heuristics to avoid polling this |
| 1629 |
* too frequently */ |
| 1630 |
est_avail_now = 0; |
| 1631 |
if (self->checked_fs_free_bytes >= self->checked_bytes_used + size) |
| 1632 |
est_avail_now = self->checked_fs_free_bytes - self->checked_bytes_used - size; |
| 1633 |
|
| 1634 |
/* is it time to check again? */ |
| 1635 |
if (est_avail_now <= block_size * MONITOR_FREE_SPACE_CLOSELY_WITHIN_BLOCKS) { |
| 1636 |
recheck = TRUE; |
| 1637 |
} else if (self->checked_bytes_used > MONITOR_FREE_SPACE_EVERY_KB * 1024) { |
| 1638 |
recheck = TRUE; |
| 1639 |
} else if (self->checked_fs_free_time + MONITOR_FREE_SPACE_EVERY_SECONDS <= time(NULL)) { |
| 1640 |
recheck = TRUE; |
| 1641 |
} |
| 1642 |
|
| 1643 |
if (!recheck) |
| 1644 |
return FALSE; |
| 1645 |
|
| 1646 |
if (get_fs_usage(self->dir_name, NULL, &fsusage) < 0 || fsusage.fsu_bavail_top_bit_set) { |
| 1647 |
g_warning("Filesystem cannot provide free space: %s; setting MONITOR_FREE_SPACE false", |
| 1648 |
fsusage.fsu_bavail_top_bit_set? "no result" : strerror(errno)); |
| 1649 |
self->monitor_free_space = FALSE; |
| 1650 |
return FALSE; |
| 1651 |
} |
| 1652 |
|
| 1653 |
self->checked_fs_free_bytes = fsusage.fsu_bavail * fsusage.fsu_blocksize; |
| 1654 |
self->checked_bytes_used = 0; |
| 1655 |
self->checked_fs_free_time = time(NULL); |
| 1656 |
|
| 1657 |
if (self->checked_fs_free_bytes - size <= eom_warning_buffer) { |
| 1658 |
g_debug("%s: at LEOM", DEVICE(self)->device_name); |
| 1659 |
return TRUE; |
| 1660 |
} |
| 1661 |
|
| 1662 |
return FALSE; |
| 1663 |
} |
| 1664 |
|
| 1665 |
static gboolean |
| 1666 |
check_at_peom( |
| 1667 |
VfsDevice *self, |
| 1668 |
guint64 size) |
| 1669 |
{ |
| 1670 |
if (self->enforce_volume_limit && (self->volume_limit > 0)) { |
| 1671 |
guint64 newtotal = self->volume_bytes + size; |
| 1672 |
if (newtotal > self->volume_limit) { |
| 1673 |
return TRUE; |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
return FALSE; |
| 1678 |
} |
| 1679 |
|
| 1680 |
static gboolean |
| 1681 |
vfs_device_recycle_file( |
| 1682 |
Device *dself, |
| 1683 |
guint filenum) |
| 1684 |
{ |
| 1685 |
VfsDevice * self = VFS_DEVICE(dself); |
| 1686 |
struct stat file_status; |
| 1687 |
off_t file_size; |
| 1688 |
|
| 1689 |
if (device_in_error(self)) return FALSE; |
| 1690 |
|
| 1691 |
/* Game Plan: |
| 1692 |
* 1) Get a write lock on the file in question. |
| 1693 |
* 2) Unlink the file in question. |
| 1694 |
* 3) Unlink the lock. |
| 1695 |
* 4) Release the lock. |
| 1696 |
* FIXME: Is it OK to unlink the lockfile? |
| 1697 |
*/ |
| 1698 |
|
| 1699 |
self->file_name = file_number_to_file_name(self, filenum); |
| 1700 |
if (self->file_name == NULL) { |
| 1701 |
device_set_error(dself, |
| 1702 |
g_strdup_printf(_("File %d not found"), filenum), |
| 1703 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1704 |
return FALSE; |
| 1705 |
} |
| 1706 |
|
| 1707 |
if (!open_lock(self, filenum, FALSE)) { |
| 1708 |
device_set_error(dself, |
| 1709 |
g_strdup(_("could not acquire lock")), |
| 1710 |
DEVICE_STATUS_DEVICE_ERROR); |
| 1711 |
return FALSE; |
| 1712 |
} |
| 1713 |
|
| 1714 |
if (0 != stat(self->file_name, &file_status)) { |
| 1715 |
device_set_error(dself, |
| 1716 |
g_strdup_printf(_("Cannot stat file %s (%s), so not removing"), |
| 1717 |
self->file_name, strerror(errno)), |
| 1718 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1719 |
return FALSE; |
| 1720 |
} |
| 1721 |
file_size = file_status.st_size; |
| 1722 |
|
| 1723 |
if (!try_unlink(self->file_name)) { |
| 1724 |
device_set_error(dself, |
| 1725 |
g_strdup_printf(_("Unlink of %s failed: %s"), self->file_name, strerror(errno)), |
| 1726 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1727 |
self->release_file(dself); |
| 1728 |
return FALSE; |
| 1729 |
} |
| 1730 |
|
| 1731 |
self->volume_bytes -= file_size; |
| 1732 |
self->release_file(dself); |
| 1733 |
return TRUE; |
| 1734 |
} |
| 1735 |
|
| 1736 |
static gboolean |
| 1737 |
vfs_device_erase( |
| 1738 |
Device *dself) |
| 1739 |
{ |
| 1740 |
VfsDevice *self = VFS_DEVICE(dself); |
| 1741 |
|
| 1742 |
if (!open_lock(self, 0, TRUE)) |
| 1743 |
return FALSE; |
| 1744 |
|
| 1745 |
delete_vfs_files(self); |
| 1746 |
if (device_in_error(dself)) return FALSE; |
| 1747 |
|
| 1748 |
self->release_file(dself); |
| 1749 |
|
| 1750 |
dumpfile_free(dself->volume_header); |
| 1751 |
dself->volume_header = NULL; |
| 1752 |
device_set_error(dself, g_strdup("Unlabeled volume"), |
| 1753 |
DEVICE_STATUS_VOLUME_UNLABELED); |
| 1754 |
|
| 1755 |
return TRUE; |
| 1756 |
} |
| 1757 |
|
| 1758 |
IoResult |
| 1759 |
vfs_device_robust_read( |
| 1760 |
VfsDevice *self, |
| 1761 |
char *buf, |
| 1762 |
int *count) |
| 1763 |
{ |
| 1764 |
int fd = self->open_file_fd; |
| 1765 |
Device *dself = DEVICE(self); |
| 1766 |
int want = *count, got = 0; |
| 1767 |
|
| 1768 |
while (got < want) { |
| 1769 |
int result; |
| 1770 |
result = read(fd, buf + got, want - got); |
| 1771 |
if (result > 0) { |
| 1772 |
got += result; |
| 1773 |
} else if (result == 0) { |
| 1774 |
/* end of file */ |
| 1775 |
if (got == 0) { |
| 1776 |
return RESULT_NO_DATA; |
| 1777 |
} else { |
| 1778 |
*count = got; |
| 1779 |
return RESULT_SUCCESS; |
| 1780 |
} |
| 1781 |
} else if (0 |
| 1782 |
#ifdef EAGAIN |
| 1783 |
|| errno == EAGAIN |
| 1784 |
#endif |
| 1785 |
#ifdef EWOULDBLOCK |
| 1786 |
|| errno == EWOULDBLOCK |
| 1787 |
#endif |
| 1788 |
#ifdef EINTR |
| 1789 |
|| errno == EINTR |
| 1790 |
#endif |
| 1791 |
) { |
| 1792 |
/* Try again. */ |
| 1793 |
continue; |
| 1794 |
} else { |
| 1795 |
/* Error occured. */ |
| 1796 |
device_set_error(dself, |
| 1797 |
g_strdup_printf(_("Error reading fd %d: %s"), fd, strerror(errno)), |
| 1798 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1799 |
*count = got; |
| 1800 |
return RESULT_ERROR; |
| 1801 |
} |
| 1802 |
} |
| 1803 |
|
| 1804 |
*count = got; |
| 1805 |
return RESULT_SUCCESS; |
| 1806 |
} |
| 1807 |
|
| 1808 |
IoResult |
| 1809 |
vfs_device_robust_write( |
| 1810 |
VfsDevice *self, |
| 1811 |
char *buf, |
| 1812 |
int count) |
| 1813 |
{ |
| 1814 |
int fd = self->open_file_fd; |
| 1815 |
Device *dself = DEVICE(self); |
| 1816 |
int rval = 0; |
| 1817 |
|
| 1818 |
while (rval < count) { |
| 1819 |
int result; |
| 1820 |
result = write(fd, buf + rval, count - rval); |
| 1821 |
if (result > 0) { |
| 1822 |
rval += result; |
| 1823 |
continue; |
| 1824 |
} else if (0 |
| 1825 |
#ifdef EAGAIN |
| 1826 |
|| errno == EAGAIN |
| 1827 |
#endif |
| 1828 |
#ifdef EWOULDBLOCK |
| 1829 |
|| errno == EWOULDBLOCK |
| 1830 |
#endif |
| 1831 |
#ifdef EINTR |
| 1832 |
|| errno == EINTR |
| 1833 |
#endif |
| 1834 |
) { |
| 1835 |
/* Try again. */ |
| 1836 |
continue; |
| 1837 |
} else if (0 |
| 1838 |
#ifdef EFBIG |
| 1839 |
|| errno == EFBIG |
| 1840 |
#endif |
| 1841 |
#ifdef ENOSPC |
| 1842 |
|| errno == ENOSPC |
| 1843 |
#endif |
| 1844 |
) { |
| 1845 |
/* We are definitely out of space. */ |
| 1846 |
device_set_error(dself, |
| 1847 |
g_strdup_printf(_("No space left on device: %s"), strerror(errno)), |
| 1848 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1849 |
return RESULT_NO_SPACE; |
| 1850 |
} else { |
| 1851 |
/* Error occured. Note that here we handle EIO as an error. */ |
| 1852 |
device_set_error(dself, |
| 1853 |
g_strdup_printf(_("Error writing device fd %d: %s"), fd, strerror(errno)), |
| 1854 |
DEVICE_STATUS_VOLUME_ERROR); |
| 1855 |
return RESULT_ERROR; |
| 1856 |
} |
| 1857 |
} |
| 1858 |
return RESULT_SUCCESS; |
| 1859 |
} |
| 1860 |
|
| 1861 |
/* TODO: add prop */ |