2 * Copyright (c) 2005-2008 Zmanda Inc. All Rights Reserved.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2.1 as
6 * published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
22 #include <string.h> /* memset() */
29 * Type checking and casting macros
31 #define TYPE_VFS_DEVICE (vfs_device_get_type())
32 #define VFS_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), vfs_device_get_type(), VfsDevice)
33 #define VFS_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), vfs_device_get_type(), VfsDevice const)
34 #define VFS_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), vfs_device_get_type(), VfsDeviceClass)
35 #define IS_VFS_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), vfs_device_get_type ())
37 #define VFS_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), vfs_device_get_type(), VfsDeviceClass)
38 static GType vfs_device_get_type (void);
41 * Main object structure
60 DeviceClass __parent__;
64 /* This regex will match all VfsDevice files in a directory. We use it
65 for cleanup and verification. Note that this regex does NOT match
67 #define VFS_DEVICE_FILE_REGEX "^[0-9]+[\\.-]"
69 /* The name of the volume lockfile. Should be the same as that
70 generated by lockfile_name(0). */
71 #define VOLUME_LOCKFILE_NAME "00000-lock"
73 #define VFS_DEVICE_MIN_BLOCK_SIZE (1)
74 #define VFS_DEVICE_MAX_BLOCK_SIZE (INT_MAX)
75 #define VFS_DEVICE_DEFAULT_BLOCK_SIZE (DISK_BLOCK_BYTES)
76 #define VFS_DEVICE_LABEL_SIZE (32768)
78 /* This looks dangerous, but is actually modified by the umask. */
79 #define VFS_DEVICE_CREAT_MODE 0666
81 /* Possible (abstracted) results from a system I/O operation. */
84 RESULT_ERROR, /* Undefined error. */
85 RESULT_NO_DATA, /* End of File, while reading */
86 RESULT_NO_SPACE, /* Out of space. Sometimes we don't know if
87 it was this or I/O error, but this is the
88 preferred explanation. */
92 void vfs_device_register(void);
94 /* here are local prototypes */
95 static void vfs_device_init (VfsDevice * o);
96 static void vfs_device_class_init (VfsDeviceClass * c);
97 static void vfs_device_base_init (VfsDeviceClass * c);
98 static void vfs_device_finalize (GObject * o);
100 static gboolean vfs_device_start(Device * pself, DeviceAccessMode mode,
101 char * label, char * timestamp);
102 static gboolean vfs_device_finish (Device * pself);
103 static void vfs_device_open_device (Device * pself, char * device_name,
104 char * device_type, char * device_node);
105 static gboolean vfs_device_start_file (Device * pself, dumpfile_t * ji);
106 static gboolean vfs_device_finish_file (Device * pself);
107 static dumpfile_t * vfs_device_seek_file (Device * self, guint file);
108 static gboolean vfs_device_seek_block (Device * self, guint64 block);
109 static gboolean vfs_device_recycle_file (Device * pself, guint filenum);
110 static Device * vfs_device_factory(char * device_name, char * device_type, char * device_node);
111 static DeviceStatusFlags vfs_device_read_label(Device * dself);
112 static gboolean vfs_device_write_block(Device * self, guint size, gpointer data);
113 static int vfs_device_read_block(Device * self, gpointer data, int * size_req);
114 static IoResult vfs_device_robust_write(VfsDevice * self, char *buf,
116 static IoResult vfs_device_robust_read(VfsDevice * self, char *buf,
119 /* Various helper functions. */
120 static void release_file(VfsDevice * self);
121 static gboolean check_is_dir(Device * d_self, const char * name);
122 static char* file_number_to_file_name(VfsDevice * self, guint file);
123 static gboolean file_number_to_file_name_functor(const char * filename,
125 static gboolean vfs_device_set_max_volume_usage_fn(Device *p_self,
126 DevicePropertyBase *base, GValue *val,
127 PropertySurety surety, PropertySource source);
128 gboolean vfs_device_get_free_space_fn(struct Device *p_self,
129 DevicePropertyBase *base, GValue *val,
130 PropertySurety *surety, PropertySource *source);
131 //static char* lockfile_name(VfsDevice * self, guint file);
132 static gboolean open_lock(VfsDevice * self, int file, gboolean exclusive);
133 static void promote_volume_lock(VfsDevice * self);
134 static void demote_volume_lock(VfsDevice * self);
135 static void delete_vfs_files(VfsDevice * self);
136 static gboolean delete_vfs_files_functor(const char * filename,
138 static gboolean check_dir_empty_functor(const char * filename,
140 static gboolean clear_and_prepare_label(VfsDevice * self, char * label,
142 static int search_vfs_directory(VfsDevice *self, const char * regex,
143 SearchDirectoryFunctor functor, gpointer user_data);
144 static gint get_last_file_number(VfsDevice * self);
145 static gboolean get_last_file_number_functor(const char * filename,
147 static char * make_new_file_name(VfsDevice * self, const dumpfile_t * ji);
148 static gboolean try_unlink(const char * file);
150 /* pointer to the classes of our parents */
151 static DeviceClass *parent_class = NULL;
153 void vfs_device_register(void) {
154 static const char * device_prefix_list[] = { "file", NULL };
155 register_device(vfs_device_factory, device_prefix_list);
159 vfs_device_get_type (void)
161 static GType type = 0;
163 if G_UNLIKELY(type == 0) {
164 static const GTypeInfo info = {
165 sizeof (VfsDeviceClass),
166 (GBaseInitFunc) vfs_device_base_init,
167 (GBaseFinalizeFunc) NULL,
168 (GClassInitFunc) vfs_device_class_init,
169 (GClassFinalizeFunc) NULL,
170 NULL /* class_data */,
173 (GInstanceInitFunc) vfs_device_init,
177 type = g_type_register_static (TYPE_DEVICE, "VfsDevice",
178 &info, (GTypeFlags)0);
185 vfs_device_init (VfsDevice * self) {
186 Device * dself = DEVICE(self);
189 self->dir_name = self->file_name = NULL;
190 self->open_file_fd = -1;
191 self->volume_bytes = 0;
192 self->volume_limit = 0;
194 /* Register Properties */
195 bzero(&response, sizeof(response));
197 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
198 g_value_set_enum(&response, CONCURRENCY_PARADIGM_RANDOM_ACCESS);
199 device_set_simple_property(dself, PROPERTY_CONCURRENCY,
200 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
201 g_value_unset(&response);
203 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
204 g_value_set_enum(&response, STREAMING_REQUIREMENT_NONE);
205 device_set_simple_property(dself, PROPERTY_STREAMING,
206 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
207 g_value_unset(&response);
209 g_value_init(&response, G_TYPE_BOOLEAN);
210 g_value_set_boolean(&response, TRUE);
211 device_set_simple_property(dself, PROPERTY_APPENDABLE,
212 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
213 g_value_unset(&response);
215 g_value_init(&response, G_TYPE_BOOLEAN);
216 g_value_set_boolean(&response, TRUE);
217 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
218 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
219 g_value_unset(&response);
221 g_value_init(&response, G_TYPE_BOOLEAN);
222 g_value_set_boolean(&response, FALSE);
223 device_set_simple_property(dself, PROPERTY_COMPRESSION,
224 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
225 g_value_unset(&response);
227 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
228 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
229 device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE,
230 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
231 g_value_unset(&response);
235 vfs_device_class_init (VfsDeviceClass * c)
237 GObjectClass *g_object_class = (GObjectClass*) c;
238 DeviceClass *device_class = (DeviceClass *)c;
240 parent_class = g_type_class_ref(TYPE_DEVICE);
242 device_class->open_device = vfs_device_open_device;
243 device_class->start = vfs_device_start;
244 device_class->start_file = vfs_device_start_file;
245 device_class->read_label = vfs_device_read_label;
246 device_class->write_block = vfs_device_write_block;
247 device_class->read_block = vfs_device_read_block;
248 device_class->finish_file = vfs_device_finish_file;
249 device_class->seek_file = vfs_device_seek_file;
250 device_class->seek_block = vfs_device_seek_block;
251 device_class->recycle_file = vfs_device_recycle_file;
252 device_class->finish = vfs_device_finish;
253 g_object_class->finalize = vfs_device_finalize;
257 vfs_device_base_init (VfsDeviceClass * c)
259 DeviceClass *device_class = (DeviceClass *)c;
261 device_class_register_property(device_class, PROPERTY_FREE_SPACE,
262 PROPERTY_ACCESS_GET_MASK,
263 vfs_device_get_free_space_fn,
266 device_class_register_property(device_class, PROPERTY_MAX_VOLUME_USAGE,
267 (PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK) &
268 (~ PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE),
269 device_simple_property_get_fn,
270 vfs_device_set_max_volume_usage_fn);
272 device_class_register_property(device_class, PROPERTY_COMPRESSION,
273 PROPERTY_ACCESS_GET_MASK,
274 device_simple_property_get_fn,
279 vfs_device_set_max_volume_usage_fn(Device *p_self,
280 DevicePropertyBase *base, GValue *val,
281 PropertySurety surety, PropertySource source)
283 VfsDevice *self = VFS_DEVICE(p_self);
285 self->volume_limit = g_value_get_uint64(val);
287 return device_simple_property_set_fn(p_self, base, val, surety, source);
291 vfs_device_get_free_space_fn(struct Device *p_self,
292 DevicePropertyBase *base G_GNUC_UNUSED, GValue *val,
293 PropertySurety *surety, PropertySource *source)
295 VfsDevice *self = VFS_DEVICE(p_self);
297 struct fs_usage fsusage;
300 if (get_fs_usage(self->dir_name, NULL, &fsusage) == 0) {
301 if (fsusage.fsu_bavail_top_bit_set)
304 bytes_avail = fsusage.fsu_bavail * fsusage.fsu_blocksize;
305 if (self->volume_limit && (guint64)self->volume_limit < bytes_avail / 1024)
306 bytes_avail = (guint64)self->volume_limit * 1024;
308 qsize.accuracy = SIZE_ACCURACY_REAL;
309 qsize.bytes = bytes_avail;
311 *surety = PROPERTY_SURETY_GOOD;
313 g_warning(_("get_fs_usage('%s') failed: %s"), self->dir_name, strerror(errno));
314 qsize.accuracy = SIZE_ACCURACY_UNKNOWN;
317 *surety = PROPERTY_SURETY_BAD;
320 g_value_unset_init(val, QUALIFIED_SIZE_TYPE);
321 g_value_set_boxed(val, &qsize);
324 *source = PROPERTY_SOURCE_DETECTED;
329 /* Drops everything associated with the volume file: Its name and fd. */
330 static void release_file(VfsDevice * self) {
332 if (self->open_file_fd != -1)
333 robust_close(self->open_file_fd);
334 amfree(self->file_name);
336 self->open_file_fd = -1;
339 static void vfs_device_finalize(GObject * obj_self) {
340 VfsDevice *self = VFS_DEVICE (obj_self);
341 Device * d_self = (Device*)self;
343 if (d_self->access_mode != ACCESS_NULL) {
344 device_finish(d_self);
347 if(G_OBJECT_CLASS(parent_class)->finalize)
348 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
350 amfree(self->dir_name);
355 static Device * vfs_device_factory(char * device_name, char * device_type, char * device_node) {
357 g_assert(0 == strcmp(device_type, "file"));
358 rval = DEVICE(g_object_new(TYPE_VFS_DEVICE, NULL));
359 device_open_device(rval, device_name, device_type, device_node);
363 static gboolean check_is_dir(Device * d_self, const char * name) {
364 struct stat dir_status;
366 if (stat(name, &dir_status) < 0) {
368 if (errno == EINTR) {
369 return check_is_dir(d_self, name);
372 device_set_error(d_self,
373 vstrallocf(_("Error checking directory %s: %s"), name, strerror(errno)),
374 DEVICE_STATUS_DEVICE_ERROR);
376 } else if (!S_ISDIR(dir_status.st_mode)) {
377 device_set_error(d_self,
378 vstrallocf(_("VFS Device path %s is not a directory"), name),
379 DEVICE_STATUS_DEVICE_ERROR);
392 /* A SearchDirectoryFunctor. */
393 static gboolean file_number_to_file_name_functor(const char * filename,
396 struct stat file_status;
397 fnfn_data *data = (fnfn_data*)datap;
399 result_tmp = vstralloc(data->self->dir_name, "/", filename, NULL);
401 /* Just to be thorough, let's check that it's a real
403 if (0 != stat(result_tmp, &file_status)) {
404 g_warning(_("Cannot stat file %s (%s), ignoring it"), result_tmp, strerror(errno));
405 } else if (!S_ISREG(file_status.st_mode)) {
406 g_warning(_("%s is not a regular file, ignoring it"), result_tmp);
409 if (data->result == NULL) {
410 data->result = result_tmp;
418 /* This function finds the filename for a given file number. We search
419 * for a filesystem file matching the regex /^0*$device_file\./; if
420 * there is more than one such file we make a warning and take an
422 static char * file_number_to_file_name(VfsDevice * self, guint device_file) {
430 regex = g_strdup_printf("^0*%u\\.", device_file);
432 search_vfs_directory(self, regex,
433 file_number_to_file_name_functor, &data);
437 if (data.count == 0) {
438 g_assert(data.result == NULL);
440 } else if (data.count > 1) {
441 g_warning("Found multiple names for file number %d, choosing file %s",
442 device_file, data.result);
445 g_assert(data.result != NULL);
448 g_assert_not_reached();
451 /* This function returns the dynamically-allocated lockfile name for a
452 given file number. */
454 static char * lockfile_name(VfsDevice * self, guint number) {
455 return g_strdup_printf("%s/%05d-lock", self->dir_name, number);
459 /* Does what you expect. If the lock already exists, it is released
460 * and regained, in case the mode is changing.
461 * The file field has several options:
462 * - file > 0: Open a lock on a real volume file.
463 * - file = 0: Open the volume lock as a volume file (for setup).
464 * - file < 0: Open the volume lock as a volume lock (persistantly).
466 static gboolean open_lock(G_GNUC_UNUSED VfsDevice * self,
467 G_GNUC_UNUSED int file,
468 G_GNUC_UNUSED gboolean exclusive) {
470 /* At the moment, file locking is horribly broken. */
474 /* For now, does it the bad way. */
475 static void promote_volume_lock(VfsDevice * self G_GNUC_UNUSED) {
478 static void demote_volume_lock(VfsDevice * self G_GNUC_UNUSED) {
481 /* A SearchDirectoryFunctor */
482 static gboolean update_volume_size_functor(const char * filename,
483 gpointer user_data) {
484 char * full_filename;
485 struct stat stat_buf;
486 VfsDevice * self = user_data;
488 full_filename = vstralloc(self->dir_name, "/", filename, NULL);
490 if (stat(full_filename, &stat_buf) < 0) {
491 /* Log it and keep going. */
492 g_warning(_("Couldn't stat file %s: %s"), full_filename, strerror(errno));
493 amfree(full_filename);
497 amfree(full_filename);
498 self->volume_bytes += stat_buf.st_size;
503 static void update_volume_size(VfsDevice * self) {
504 self->volume_bytes = 0;
505 search_vfs_directory(self, "^[0-9]+\\.",
506 update_volume_size_functor, self);
511 vfs_device_open_device (Device * pself, char * device_name, char * device_type, char * device_node) {
513 self = VFS_DEVICE(pself);
515 pself->min_block_size = VFS_DEVICE_MIN_BLOCK_SIZE;
516 pself->max_block_size = VFS_DEVICE_MAX_BLOCK_SIZE;
517 pself->block_size = VFS_DEVICE_DEFAULT_BLOCK_SIZE;
519 /* We don't have to free this ourselves; it will be freed by
520 * vfs_device_finalize whether we succeed here or not. */
521 self->dir_name = g_strconcat(device_node, "/data/", NULL);
523 if (parent_class->open_device) {
524 parent_class->open_device(pself, device_name, device_type, device_node);
528 /* A SearchDirectoryFunctor */
529 static gboolean delete_vfs_files_functor(const char * filename,
530 gpointer user_data) {
535 self = VFS_DEVICE(user_data);
536 d_self = DEVICE(self);
538 /* Skip the volume lock. */
539 if (strcmp(filename, VOLUME_LOCKFILE_NAME) == 0)
542 path_name = vstralloc(self->dir_name, "/", filename, NULL);
543 if (unlink(path_name) != 0) {
544 g_warning(_("Error unlinking %s: %s"), path_name, strerror(errno));
550 /* delete_vfs_files deletes all VfsDevice files in the directory except the
552 static void delete_vfs_files(VfsDevice * self) {
553 g_assert(self != NULL);
555 /* This function assumes that the volume is locked! */
556 search_vfs_directory(self, VFS_DEVICE_FILE_REGEX,
557 delete_vfs_files_functor, self);
560 /* This is a functor suitable for search_directory. It simply prints a
561 warning. It also dodges the volume lockfile. */
562 static gboolean check_dir_empty_functor(const char * filename,
563 gpointer user_data) {
568 self = VFS_DEVICE(user_data);
569 d_self = DEVICE(self);
571 if (strcmp(filename, VOLUME_LOCKFILE_NAME) == 0)
574 path_name = vstralloc(self->dir_name, "/", filename, NULL);
576 g_warning(_("Found spurious storage file %s"), path_name);
582 /* This function is used to write volume and dump headers. */
583 static gboolean write_amanda_header(VfsDevice * self,
584 const dumpfile_t * header) {
587 Device *d_self = DEVICE(self);
589 g_assert(header != NULL);
591 label_buffer = build_header(header, VFS_DEVICE_LABEL_SIZE);
592 if (strlen(label_buffer)+1 > VFS_DEVICE_LABEL_SIZE) {
593 amfree(label_buffer);
594 device_set_error(d_self,
595 stralloc(_("Amanda file header won't fit in a single block!")),
596 DEVICE_STATUS_DEVICE_ERROR);
600 result = vfs_device_robust_write(self, label_buffer, VFS_DEVICE_LABEL_SIZE);
601 /* vfs_device_robust_write sets error status if necessary */
602 amfree(label_buffer);
603 return (result == RESULT_SUCCESS);
606 /* clear_and_label will erase the contents of the directory, and write
607 * this label in its place. This function assumes we already have a volume
608 * label write lock in place (e.g., promote_lock() has been called.) */
609 static gboolean clear_and_prepare_label(VfsDevice * self, char * label,
611 dumpfile_t * label_header;
612 Device *d_self = DEVICE(self);
616 /* Delete any extant data, except our volume lock. */
617 delete_vfs_files(self);
619 /* Print warnings about any remaining files. */
620 search_vfs_directory(self, VFS_DEVICE_FILE_REGEX,
621 check_dir_empty_functor, self);
623 self->file_name = g_strdup_printf("%s/00000.%s", self->dir_name, label);
625 self->open_file_fd = robust_open(self->file_name,
626 O_CREAT | O_EXCL | O_WRONLY,
627 VFS_DEVICE_CREAT_MODE);
628 if (self->open_file_fd < 0) {
629 device_set_error(d_self,
630 vstrallocf(_("Can't open file %s: %s"), self->file_name, strerror(errno)),
631 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
635 label_header = make_tapestart_header(DEVICE(self), label, timestamp);
636 if (!write_amanda_header(self, label_header)) {
637 /* write_amanda_header sets error status if necessary */
638 amfree(label_header);
641 amfree(label_header);
642 self->volume_bytes = VFS_DEVICE_LABEL_SIZE;
646 /* Just like search_directory, but returns -1 in the event of an error */
648 search_vfs_directory(
651 SearchDirectoryFunctor functor,
654 Device *dself = DEVICE(self);
658 dir_handle = opendir(self->dir_name);
659 if (dir_handle == NULL) {
660 device_set_error(dself,
661 vstrallocf(_("Couldn't open device %s (directory %s) for reading: %s"),
662 dself->device_name, self->dir_name, strerror(errno)),
663 DEVICE_STATUS_DEVICE_ERROR);
667 /* TODO: is this the right moment to acquire a lock?? */
669 result = search_directory(dir_handle, regex, functor, user_data);
673 closedir(dir_handle);
677 static DeviceStatusFlags vfs_device_read_label(Device * dself) {
678 dumpfile_t * amanda_header;
681 self = VFS_DEVICE(dself);
682 g_assert(self != NULL);
684 if (!check_is_dir(dself, self->dir_name)) {
685 /* error message set by check_is_dir */
686 return dself->status;
689 amfree(dself->volume_label);
690 amfree(dself->volume_time);
691 amfree(dself->volume_header);
693 if (device_in_error(self)) return dself->status;
695 amanda_header = dself->volume_header = vfs_device_seek_file(dself, 0);
696 if (amanda_header == NULL) {
697 /* This means an error occured getting locks or opening the header
699 device_set_error(dself,
700 stralloc("Error loading device header -- unlabeled volume?"),
701 DEVICE_STATUS_DEVICE_ERROR
702 | DEVICE_STATUS_VOLUME_ERROR
703 | DEVICE_STATUS_VOLUME_UNLABELED);
704 return dself->status;
707 if (amanda_header->type != F_TAPESTART) {
708 /* This is an error, and should not happen. */
709 device_set_error(dself,
710 stralloc(_("Got a bad volume label")),
711 DEVICE_STATUS_VOLUME_ERROR);
712 amfree(amanda_header);
713 return dself->status;
716 dself->volume_label = g_strdup(amanda_header->name);
717 dself->volume_time = g_strdup(amanda_header->datestamp);
718 /* dself->volume_header is already set */
720 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
722 update_volume_size(self);
724 return dself->status;
727 static gboolean vfs_device_write_block(Device * pself, guint size, gpointer data) {
728 VfsDevice * self = VFS_DEVICE(pself);
731 if (device_in_error(self)) return FALSE;
733 g_assert(self->open_file_fd >= 0);
735 if (self->volume_limit > 0 &&
736 self->volume_bytes + size > self->volume_limit) {
738 pself->is_eof = TRUE;
739 device_set_error(pself,
740 stralloc(_("No space left on device")),
741 DEVICE_STATUS_VOLUME_ERROR);
745 result = vfs_device_robust_write(self, data, size);
746 if (result != RESULT_SUCCESS) {
747 /* vfs_device_robust_write set error status appropriately */
751 self->volume_bytes += size;
758 vfs_device_read_block(Device * pself, gpointer data, int * size_req) {
763 self = VFS_DEVICE(pself);
765 if (device_in_error(self)) return -1;
767 if (data == NULL || (gsize)*size_req < pself->block_size) {
768 /* Just a size query. */
769 g_assert(pself->block_size < INT_MAX);
770 *size_req = (int)pself->block_size;
774 size = pself->block_size;
775 result = vfs_device_robust_read(self, data, &size);
782 pself->is_eof = TRUE;
783 pself->in_file = FALSE;
784 device_set_error(pself,
786 DEVICE_STATUS_SUCCESS);
789 device_set_error(pself,
790 vstrallocf(_("Error reading from data file: %s"), strerror(errno)),
791 DEVICE_STATUS_DEVICE_ERROR);
795 g_assert_not_reached();
798 static gboolean vfs_device_start(Device * pself,
799 DeviceAccessMode mode, char * label,
802 self = VFS_DEVICE(pself);
804 if (!check_is_dir(pself, self->dir_name)) {
805 /* error message set by check_is_dir */
809 pself->in_file = FALSE;
811 if (mode == ACCESS_WRITE) {
812 promote_volume_lock(self);
813 if (!clear_and_prepare_label(self, label, timestamp)) {
814 /* clear_and_prepare_label sets error status if necessary */
815 demote_volume_lock(self);
819 pself->volume_label = newstralloc(pself->volume_label, label);
820 pself->volume_time = newstralloc(pself->volume_time, timestamp);
822 /* unset the VOLUME_UNLABELED flag, if it was set */
823 device_set_error(pself, NULL, DEVICE_STATUS_SUCCESS);
825 demote_volume_lock(self);
826 pself->access_mode = mode;
828 if (pself->volume_label == NULL && device_read_label(pself) != DEVICE_STATUS_SUCCESS) {
829 /* device_read_label already set our error message */
832 pself->access_mode = mode;
842 vfs_device_finish (Device * pself) {
844 self = VFS_DEVICE(pself);
846 if (device_in_error(self)) return FALSE;
848 pself->access_mode = ACCESS_NULL;
857 /* A SearchDirectoryFunctor. */
858 static gboolean get_last_file_number_functor(const char * filename,
861 glfn_data * data = (glfn_data*)datap;
863 file = g_ascii_strtoull(filename, NULL, 10); /* Guaranteed to work. */
864 if (file > G_MAXINT) {
865 g_warning(_("Super-large device file %s found, ignoring"), filename);
868 /* This condition is needlessly complex due to sign issues. */
869 if (data->rval < 0 || ((guint)data->rval) < file) {
875 static gint get_last_file_number(VfsDevice * self) {
878 Device *d_self = DEVICE(self);
882 count = search_vfs_directory(self, "^[0-9]+\\.",
883 get_last_file_number_functor, &data);
886 /* Somebody deleted something important while we weren't looking. */
887 device_set_error(d_self,
888 stralloc(_("Error identifying VFS device contents!")),
889 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
892 g_assert(data.rval >= 0);
904 /* A SearchDirectoryFunctor. */
905 static gboolean get_next_file_number_functor(const char * filename,
908 gnfn_data * data = (gnfn_data*)datap;
910 file = g_ascii_strtoull(filename, NULL, 10); /* Guaranteed to work. */
911 if (file > G_MAXINT) {
912 g_warning(_("Super-large device file %s found, ignoring"), filename);
915 /* This condition is needlessly complex due to sign issues. */
916 if (file >= data->request &&
917 (data->best_found < 0 || file < (guint)data->best_found)) {
918 data->best_found = file;
923 /* Returns the file number equal to or greater than the given requested
925 static gint get_next_file_number(VfsDevice * self, guint request) {
928 Device *d_self = DEVICE(self);
930 data.request = request;
931 data.best_found = -1;
933 count = search_vfs_directory(self, "^[0-9]+\\.",
934 get_next_file_number_functor, &data);
937 /* Somebody deleted something important while we weren't looking. */
938 device_set_error(d_self,
939 stralloc(_("Error identifying VFS device contents!")),
940 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
945 return data.best_found;
948 /* Finds the file number, acquires a lock, and returns the new file name. */
950 char * make_new_file_name(VfsDevice * self, const dumpfile_t * ji) {
952 char *base, *sanitary_base;
956 fileno = 1 + get_last_file_number(self);
960 if (open_lock(self, fileno, TRUE)) {
967 /* record that we're at this filenum now */
968 DEVICE(self)->file = fileno;
970 base = g_strdup_printf("%05d.%s.%s.%d", fileno, ji->name, ji->disk,
972 sanitary_base = sanitise_filename(base);
974 rval = g_strdup_printf("%s/%s", self->dir_name, sanitary_base);
975 amfree(sanitary_base);
980 vfs_device_start_file (Device * pself, dumpfile_t * ji) {
982 self = VFS_DEVICE(pself);
984 if (device_in_error(self)) return FALSE;
986 /* set the blocksize in the header to 32k, since the VFS header is always
987 * 32k regardless of the block_size setting */
988 ji->blocksize = 32768;
990 if (self->volume_limit > 0 &&
991 self->volume_bytes + VFS_DEVICE_LABEL_SIZE > self->volume_limit) {
992 device_set_error(pself,
993 stralloc(_("No space left on device")),
994 DEVICE_STATUS_DEVICE_ERROR);
998 /* The basic idea here is thus:
999 1) Try to get a lock on the next filenumber.
1000 2) If that fails, update our idea of "next filenumber" and try again.
1001 3) Then open the file itself.
1005 self->file_name = make_new_file_name(self, ji);
1006 if (self->file_name == NULL) {
1007 device_set_error(pself,
1008 stralloc(_("Could not create header filename")),
1009 DEVICE_STATUS_DEVICE_ERROR);
1013 self->open_file_fd = robust_open(self->file_name,
1014 O_CREAT | O_EXCL | O_RDWR,
1015 VFS_DEVICE_CREAT_MODE);
1016 if (self->open_file_fd < 0) {
1017 device_set_error(pself,
1018 vstrallocf(_("Can't create file %s: %s"), self->file_name, strerror(errno)),
1019 DEVICE_STATUS_DEVICE_ERROR);
1025 if (!write_amanda_header(self, ji)) {
1026 /* write_amanda_header sets error status if necessary */
1031 /* handle some accounting business */
1032 self->volume_bytes += VFS_DEVICE_LABEL_SIZE;
1033 pself->in_file = TRUE;
1035 /* make_new_file_name set pself->file for us */
1041 vfs_device_finish_file (Device * pself) {
1043 self = VFS_DEVICE(pself);
1045 if (device_in_error(self)) return FALSE;
1049 pself->in_file = FALSE;
1053 /* This function is used for two purposes, rather than one. In
1054 * addition to its documented behavior, we also use it to open the
1055 * volume label for reading at startup. In that second case, we avoid
1056 * FdDevice-related side effects. */
1058 vfs_device_seek_file (Device * pself, guint requested_file) {
1062 char header_buffer[VFS_DEVICE_LABEL_SIZE];
1063 int header_buffer_size = sizeof(header_buffer);
1066 self = VFS_DEVICE(pself);
1068 if (device_in_error(self)) return NULL;
1070 pself->in_file = FALSE;
1071 pself->is_eof = FALSE;
1076 if (requested_file > 0) {
1077 file = get_next_file_number(self, requested_file);
1079 file = requested_file;
1083 /* Did they request one past the end? */
1084 char * tmp_file_name;
1085 tmp_file_name = file_number_to_file_name(self, requested_file - 1);
1086 if (tmp_file_name != NULL) {
1087 free(tmp_file_name);
1088 pself->file = requested_file; /* other attributes are already correct */
1089 return make_tapeend_header();
1091 device_set_error(pself,
1092 stralloc(_("Attempt to read past tape-end file")),
1093 DEVICE_STATUS_SUCCESS);
1098 if (!open_lock(self, file, FALSE)) {
1099 device_set_error(pself,
1100 stralloc(_("could not acquire lock")),
1101 DEVICE_STATUS_DEVICE_ERROR);
1105 self->file_name = file_number_to_file_name(self, file);
1106 if (self->file_name == NULL) {
1107 device_set_error(pself,
1108 vstrallocf(_("File %d not found"), file),
1109 DEVICE_STATUS_VOLUME_ERROR);
1114 self->open_file_fd = robust_open(self->file_name, O_RDONLY, 0);
1115 if (self->open_file_fd < 0) {
1116 device_set_error(pself,
1117 vstrallocf(_("Couldn't open file %s: %s"), self->file_name, strerror(errno)),
1118 DEVICE_STATUS_DEVICE_ERROR);
1119 amfree(self->file_name);
1124 result = vfs_device_robust_read(self, header_buffer,
1125 &header_buffer_size);
1126 if (result != RESULT_SUCCESS) {
1127 device_set_error(pself,
1128 vstrallocf(_("Problem reading Amanda header: %s"), device_error(pself)),
1129 DEVICE_STATUS_VOLUME_ERROR);
1134 rval = g_new(dumpfile_t, 1);
1135 parse_file_header(header_buffer, rval, header_buffer_size);
1136 switch (rval->type) {
1138 case F_CONT_DUMPFILE:
1139 case F_SPLIT_DUMPFILE:
1143 /* file 0 should have a TAPESTART header; vfs_device_read_label
1145 if (requested_file == 0)
1150 device_set_error(pself,
1151 stralloc(_("Invalid amanda header while reading file header")),
1152 DEVICE_STATUS_VOLUME_ERROR);
1158 /* update our state */
1159 pself->in_file = TRUE;
1166 vfs_device_seek_block (Device * pself, guint64 block) {
1170 self = VFS_DEVICE(pself);
1172 g_assert(self->open_file_fd >= 0);
1173 g_assert(sizeof(off_t) >= sizeof(guint64));
1174 if (device_in_error(self)) return FALSE;
1176 /* Pretty simple. We figure out the blocksize and use that. */
1177 result = lseek(self->open_file_fd,
1178 (block) * pself->block_size + VFS_DEVICE_LABEL_SIZE,
1181 pself->block = block;
1183 if (result == (off_t)(-1)) {
1184 device_set_error(pself,
1185 vstrallocf(_("Error seeking within file: %s"), strerror(errno)),
1186 DEVICE_STATUS_DEVICE_ERROR);
1193 static gboolean try_unlink(const char * file) {
1194 if (unlink(file) < 0) {
1202 vfs_device_recycle_file (Device * pself, guint filenum) {
1204 struct stat file_status;
1207 self = VFS_DEVICE(pself);
1209 if (device_in_error(self)) return FALSE;
1212 * 1) Get a write lock on the file in question.
1213 * 2) Unlink the file in question.
1214 * 3) Unlink the lock.
1215 * 4) Release the lock.
1216 * FIXME: Is it OK to unlink the lockfile?
1219 self->file_name = file_number_to_file_name(self, filenum);
1220 if (self->file_name == NULL) {
1221 device_set_error(pself,
1222 vstrallocf(_("File %d not found"), filenum),
1223 DEVICE_STATUS_VOLUME_ERROR);
1227 if (!open_lock(self, filenum, FALSE)) {
1228 device_set_error(pself,
1229 stralloc(_("could not acquire lock")),
1230 DEVICE_STATUS_DEVICE_ERROR);
1234 if (0 != stat(self->file_name, &file_status)) {
1235 device_set_error(pself,
1236 vstrallocf(_("Cannot stat file %s (%s), so not removing"),
1237 self->file_name, strerror(errno)),
1238 DEVICE_STATUS_VOLUME_ERROR);
1241 file_size = file_status.st_size;
1243 if (!try_unlink(self->file_name)) {
1244 device_set_error(pself,
1245 vstrallocf(_("Unlink of %s failed: %s"), self->file_name, strerror(errno)),
1246 DEVICE_STATUS_VOLUME_ERROR);
1251 self->volume_bytes -= file_size;
1256 static IoResult vfs_device_robust_read(VfsDevice * self, char *buf,
1258 int fd = self->open_file_fd;
1259 Device *d_self = DEVICE(self);
1260 int want = *count, got = 0;
1262 while (got < want) {
1264 result = read(fd, buf + got, want - got);
1267 } else if (result == 0) {
1270 return RESULT_NO_DATA;
1273 return RESULT_SUCCESS;
1280 || errno == EWOULDBLOCK
1289 /* Error occured. */
1290 device_set_error(d_self,
1291 vstrallocf(_("Error reading fd %d: %s"), fd, strerror(errno)),
1292 DEVICE_STATUS_VOLUME_ERROR);
1294 return RESULT_ERROR;
1299 return RESULT_SUCCESS;
1303 vfs_device_robust_write(VfsDevice * self, char *buf, int count) {
1304 int fd = self->open_file_fd;
1305 Device *d_self = DEVICE(self);
1308 while (rval < count) {
1310 result = write(fd, buf + rval, count - rval);
1319 || errno == EWOULDBLOCK
1335 /* We are definitely out of space. */
1336 device_set_error(d_self,
1337 vstrallocf(_("No space left on device: %s"), strerror(errno)),
1338 DEVICE_STATUS_VOLUME_ERROR);
1339 return RESULT_NO_SPACE;
1341 /* Error occured. Note that here we handle EIO as an error. */
1342 device_set_error(d_self,
1343 vstrallocf(_("Error writing device fd %d: %s"), fd, strerror(errno)),
1344 DEVICE_STATUS_VOLUME_ERROR);
1345 return RESULT_ERROR;
1348 return RESULT_SUCCESS;