4
* Copyright (C) 2000 Richard Guenther
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#include "swfs_ctree.h"
32
/* File instance, flags are
33
* SWF_NOT_IN_CORE - swfile.{size,cluster_cnt,clusters} is not filled in
34
* SWF_UNLINKED - on disk file is unlinked
35
* SWF_DIRTY - cluster list is dirty */
36
#define SWF_NOT_IN_CORE 1
37
#define SWF_UNLINKED 2
40
struct swfile *next_swfile_hash;
41
struct swfile **pprev_swfile_hash;
42
pthread_mutex_t mx; /* protects flags & clusters */
44
int usage; /* number of references to this struct swfile,
45
* protected by FILES lock. */
46
int flags; /* SWF_* */
48
/* Fields created out of the file metadata */
49
struct ctree *clusters; /* cluster tree */
53
/* Gets a reference to the file with the specified name -
54
* creates it, if it does not exist and FILEGET_CREAT is
55
* set. If FILEGET_READCLUSTERS is set, the cluster tree
56
* of the file is brought into the core.
57
* Returns a reference to the file or NULL on error. */
58
#define FILEGET_READCLUSTERS 1
59
#define FILEGET_CREAT 2
60
static struct swfile *file_get(long name, int flags);
62
/* Releases the file reference, if FILEPUT_SYNC is set,
63
* the actual cluster tree is synced to the disk, if
64
* FILEPUT_UNLINK is specified, the on-disk representation
65
* is unlinked and as the last reference goes away, the
66
* file and its name is freed. */
67
#define FILEPUT_UNLINK 1
68
#define FILEPUT_SYNC 2
69
static void file_put(struct swfile *f, int flags);
72
/* Syncs all hashed and modified file ctrees to disk. */
73
static void file_sync();
76
/* Get a reference to the cluster containing offset using the
77
* specified flags. Stores the offset of the cluster start in cstart.
78
* Return values/flags like get_cluster. */
79
static struct swcluster *file_getcluster(struct swfile *f,
80
s64 offset, s64 *cstart,
83
/* Get a reference to the cluster containing offset using the
84
* specified flags. Stores the offset of the cluster start in cstart.
85
* Return values/flags like get_cluster. If the cluster is shared,
86
* copy it and replace it with the copy. */
87
static struct swcluster *file_getcluster_private(struct swfile *f,
88
s64 offset, s64 *cstart,
92
/* Truncate the file to the given size. Returns 0 on success
94
static int file_truncate(struct swfile *f, s64 size);
97
/* Insert count bytes from position spos in file sf into file df
98
* at position dpos. Returns -1 on error, 0 on success. */
99
static int file_insert(struct swfile *df, s64 dpos,
100
struct swfile *sf, s64 spos, s64 count);
102
/* Replaces count bytes of file df from position dpos with
103
* data from position spos out of file sf. Returns 0 on success,
105
static int file_replace(struct swfile *df, s64 dpos,
106
struct swfile *sf, s64 spos, s64 count);
108
/* Removes count bytes from position dpos out of file df. Returns
109
* 0 on success, -1 on error. */
110
static int file_cut(struct swfile *df, s64 dpos, s64 count);