summaryrefslogtreecommitdiff
path: root/ext2fs/ext2fs.h
blob: c3435404d37bb461b521986fc527e234c39777c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* 
   Copyright (C) 1994, 1995 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <mach.h>
#include <hurd.h>
#include <hurd/ports.h>
#include <hurd/pager.h>
#include <hurd/fshelp.h>
#include <hurd/ioserver.h>
#include <hurd/diskfs.h>
#include <assert.h>
#include <rwlock.h>

#define __hurd__		/* Enable some hurd-specific fields.  */
#include "ext2_fs.h"
#include "ext2_fs_i.h"
#undef __hurd__

/* Define this if memory objects should not be cached by the kernel.
   Normally, don't define it, but defining it causes a much greater rate
   of paging requests, which may be helpful in catching bugs. */

/* #undef DONT_CACHE_MEMORY_OBJECTS */

/* ---------------------------------------------------------------- */

struct poke 
{
  vm_offset_t offset;
  vm_size_t length;
  struct poke *next;
};

struct pokel
{
  struct poke *pokes, *free_pokes;
  spin_lock_t lock;
  struct pager *pager;
  void *image;
};

void pokel_init (struct pokel *pokel, struct pager *pager, void *image);

/* Remember that data here on the disk has been modified. */
void pokel_add (struct pokel *pokel, void *loc, vm_size_t length);

/* Sync all the modified pieces of disk */
void pokel_sync (struct pokel *pokel, int wait);

/* ---------------------------------------------------------------- */

struct disknode 
{
  ino_t number;

  /* For a directory, this array holds the number of directory entries in
     each DIRBLKSIZE piece of the directory. */
  int *dirents;

  /* Links on hash list. */
  struct node *hnext, **hprevp;

  struct rwlock alloc_lock;

  struct pokel pokel;

  struct ext2_inode_info info;

  struct user_pager_info *fileinfo;
};  

struct user_pager_info 
{
  struct node *np;
  enum pager_type 
    {
      DISK,
      FILE_DATA,
    } type;
  struct pager *p;
  struct user_pager_info *next, **prevp;
};

/* ---------------------------------------------------------------- */

/*
 * These are the fs-independent mount-flags: up to 16 flags are supported
 */
#define MS_RDONLY	 1 /* mount read-only */
#define MS_NOSUID	 2 /* ignore suid and sgid bits */
#define MS_NODEV	 4 /* disallow access to device special files */
#define MS_NOEXEC	 8 /* disallow program execution */
#define MS_SYNCHRONOUS	16 /* writes are synced at once */
#define MS_REMOUNT	32 /* alter flags of a mounted FS */

#define S_APPEND    256 /* append-only file */
#define S_IMMUTABLE 512 /* immutable file */

#define IS_APPEND(node) ((node)->dn->info.i_flags & S_APPEND)
#define IS_IMMUTABLE(node) ((inode)->dn->info.i_flags & S_IMMUTABLE)

/* ---------------------------------------------------------------- */

/* The block size we assume the kernel device uses.  */
#define DEV_BSIZE 512

struct user_pager_info *disk_pager;
mach_port_t disk_pager_port;
void *disk_image;

char *device_name;
mach_port_t device_port;
off_t device_size;

/* Our in-core copy of the super-block.  */
struct ext2_super_block *sblock;
/* What to lock if changing the super block.  */
spin_lock_t sblock_lock;
/* True if sblock has been modified.  */
int sblock_dirty;

struct pokel sblock_pokel;

#define SBLOCK_BLOCK 1
#define SBLOCK_OFFS (SBLOCK_BLOCK * EXT2_MIN_BLOCK_SIZE)
#define SBLOCK_SIZE (sizeof (struct ext2_super_block))

/* The filesystem block-size.  */
unsigned long block_size;

vm_address_t zeroblock;

/* Copy the sblock into the disk.  */
void copy_sblock ();
void get_hypermetadata ();

/* ---------------------------------------------------------------- */
/* Random stuff calculated from the super block.  */

unsigned long frag_size;	/* Size of a fragment in bytes */
unsigned long frags_per_block;	/* Number of fragments per block */
unsigned long inodes_per_block;	/* Number of inodes per block */

unsigned long itb_per_group;	/* Number of inode table blocks per group */
unsigned long db_per_group;	/* Number of descriptor blocks per group */
unsigned long desc_per_block;	/* Number of group descriptors per block */
unsigned long addr_per_block;	/* Number of disk addresses per block */

unsigned long groups_count;	/* Number of groups in the fs */

/* ---------------------------------------------------------------- */

spin_lock_t node_to_page_lock;

spin_lock_t gennumberlock;
unsigned long nextgennumber;

/* ---------------------------------------------------------------- */
/* Functions for looking inside disk_image */

#define boffs(block) ((block) * block_size)
#define offsb(offs) ((block) / block_size)
#define offsaddr(offs) (((char *)disk_image) + (offs))
#define addroffs(offs) ((addr) - ((char *)disk_image))
#define bptr(block) offsaddr(boffs(block))
#define addrb(addr) offsb(addroffs(addr))

/* Get the descriptor for the block group inode INUM is in.  */
extern inline struct ext2_group_desc *
group_desc(unsigned long bg_num)
{
  int desc_per_block = EXT2_DESC_PER_BLOCK(sblock);
  unsigned long group_desc = bg_num / desc_per_block;
  unsigned long desc = bg_num % desc_per_block;
  return ((struct ext2_group_desc *)bptr(1 + group_desc)) + desc;
}

#define inode_group_num(inum) (((inum) - 1) / sblock->s_inodes_per_group)

/* Convert an inode number to the dinode on disk. */
extern inline struct ext2_inode *
dino (ino_t inum)
{
  unsigned long bg_num = inode_group_num(inum);
  struct ext2_group_desc *bg = group_desc(bg_num);
  unsigned long inodes_per_block = EXT2_INODES_PER_BLOCK(sblock);
  unsigned long block = bg->bg_inode_table + (bg_num / inodes_per_block);
  return ((struct ext2_inode *)bptr(block)) + inum % inodes_per_block;
}

/* Sync part of the disk */
extern inline void
sync_disk_image (void *place, size_t nbytes, int wait)
{
  pager_sync_some (disk_pager->p,
		   (char *)place - (char *)disk_image, nbytes, wait);
}

/* ---------------------------------------------------------------- */

/* Fetch inode INUM, set *NPP to the node structure; gain one user reference
   and lock the node.  */
error_t iget (ino_t inum, struct node **npp);

/* Lookup node INUM (which must have a reference already) and return it
   without allocating any new references. */
struct node *ifind (ino_t inum);

void inode_init (void);

/* ---------------------------------------------------------------- */

/* Sync all allocation information and node NP if diskfs_synchronous. */
inline void
alloc_sync (struct node *np)
{
  if (diskfs_synchronous)
    {
      if (np)
	{
	  diskfs_node_update (np, 1);
	  pokel_sync (&np->dn->pokel, 1);
	}
      copy_sblock ();
      diskfs_set_hypermetadata (1, 0);
      pokel_sync (&sblock_pokel, 1);
    }
}

/* ---------------------------------------------------------------- */

error_t ext2_getblk (struct node *node, long block, int create, char **buf);

int ext2_new_block (unsigned long goal,
		    u32 * prealloc_count, u32 * prealloc_block);

void ext2_free_blocks (unsigned long block, unsigned long count);

/* ---------------------------------------------------------------- */
/* Bitmap routines.  */

/* Returns TRUE if bit NUM is set in BITMAP.  */
inline int test_bit (unsigned num, char *bitmap)
{
  return bitmap[num >> 3] & (1 << (num & 0x7));
}

/* Sets bit NUM in BITMAP, and returns TRUE if it was already set.  */
inline int set_bit (unsigned num, char *bitmap)
{
  char *p = bitmap + (num >> 3);
  char byte = *p;
  char mask = (1 << (num & 0x7));

  if (byte & mask)
    return 1;
  else
    {
      *p = byte | mask;
      return 0;
    }
}

/* Clears bit NUM in BITMAP, and returns TRUE if it was already clear.  */
inline int clear_bit (unsigned num, char *bitmap)
{
  char *p = bitmap + (num >> 3);
  char byte = *p;
  char mask = (1 << (num & 0x7));

  if (byte & mask)
    {
      *p = byte & ~mask;
      return 0;
    }
  else
    return 1;
}

/* Counts the number of bits unset in MAP, a bitmap NUMCHARS long. */
unsigned long count_free (char * map, unsigned int numchars);

extern int find_first_zero_bit(void * addr, unsigned size);

extern int find_next_zero_bit (void * addr, int size, int offset);

extern unsigned long ffz(unsigned long word);

/* Returns a pointer to the first occurence of CH in the buffer BUF of len
   LEN, or BUF + LEN if CH doesn't occur.  */
void *memscan(void *buf, unsigned char ch, unsigned len);

/* ---------------------------------------------------------------- */

/* Write disk block ADDR with DATA of LEN bytes, waiting for completion.  */
error_t dev_write_sync (daddr_t addr, vm_address_t data, long len);

/* Write diskblock ADDR with DATA of LEN bytes; don't bother waiting
   for completion. */
error_t dev_write (daddr_t addr, vm_address_t data, long len);

/* Read disk block ADDR; put the address of the data in DATA; read LEN
   bytes.  Always *DATA should be a full page no matter what.   */
error_t dev_read_sync (daddr_t addr, vm_address_t *data, long len);

/* ---------------------------------------------------------------- */

extern void ext2_error (const char *, const char *, ...)
	__attribute__ ((format (printf, 2, 3)));
extern void ext2_panic (const char *, const char *, ...)
	__attribute__ ((format (printf, 2, 3)));
extern void ext2_warning (const char *, const char *, ...)
	__attribute__ ((format (printf, 2, 3)));

/* Enable some more error checking.  */
int check_strict;