summaryrefslogtreecommitdiff
path: root/ext2fs/getblk.c
blob: 11c73802b73893b06eb69fc5374df7cf59fbf161 (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
/* File block to disk block mapping routines.

   Copyright (C) 1995 Free Software Foundation, Inc.

   Converted to work under the hurd by Miles Bader <miles@gnu.ai.mit.edu>

   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. */

/*
 *  linux/fs/ext2/inode.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
 */

#include <string.h>
#include "ext2fs.h"

/* 
 * ext2_discard_prealloc and ext2_alloc_block are atomic wrt. the
 * superblock in the same manner as are ext2_free_blocks and
 * ext2_new_block.  We just wait on the super rather than locking it
 * here, since ext2_new_block will do the necessary locking and we
 * can't block until then.
 */
void 
ext2_discard_prealloc (struct node *node)
{
#ifdef EXT2_PREALLOCATE
  if (node->dn->info.i_prealloc_count)
    {
      int i = node->dn->info.i_prealloc_count;
      node->dn->info.i_prealloc_count = 0;
      ext2_free_blocks (node->dn->info.i_prealloc_block, i);
    }
#endif
}

static int 
ext2_alloc_block (struct node *node, unsigned long goal)
{
#ifdef EXT2FS_DEBUG
  static unsigned long alloc_hits = 0, alloc_attempts = 0;
#endif
  unsigned long result;
  char *bh;

#ifdef EXT2_PREALLOCATE
  if (node->dn->info.i_prealloc_count &&
      (goal == node->dn->info.i_prealloc_block ||
       goal + 1 == node->dn->info.i_prealloc_block))
    {
      result = node->dn->info.i_prealloc_block++;
      node->dn->info.i_prealloc_count--;
      ext2_debug ("preallocation hit (%lu/%lu).\n",
		  ++alloc_hits, ++alloc_attempts);

      bh = block_image(result);
      memset (bh, 0, block_size);
    }
  else
    {
      ext2_discard_prealloc (node);
      ext2_debug ("preallocation miss (%lu/%lu).\n",
		  alloc_hits, ++alloc_attempts);
      if (S_ISREG (node->dn_stat.st_mode))
	result = ext2_new_block
	  (goal,
	   &node->dn->info.i_prealloc_count,
	   &node->dn->info.i_prealloc_block);
      else
	result = ext2_new_block (goal, 0, 0);
    }
#else
  result = ext2_new_block (goal, 0, 0);
#endif

  return result;
}

static error_t
inode_getblk (struct node *node, int nr, int create, int new_block, char **buf)
{
  u32 block;
  int goal = 0, i;
  int blocks = block_size / 512;

  block = node->dn->info.i_data[nr];
  if (block)
    {
      *buf = block_image(block);
      return 0;
    }

  if (!create)
    return EINVAL;

  if (node->dn->info.i_next_alloc_block == new_block)
    goal = node->dn->info.i_next_alloc_goal;

  ext2_debug ("hint = %d,", goal);

  if (!goal)
    {
      for (i = nr - 1; i >= 0; i--)
	{
	  if (node->dn->info.i_data[i])
	    {
	      goal = node->dn->info.i_data[i];
	      break;
	    }
	}
      if (!goal)
	goal =
	  (node->dn->info.i_block_group * EXT2_BLOCKS_PER_GROUP (sblock))
	  + sblock->s_first_data_block;
    }

  ext2_debug ("goal = %d.\n", goal);

  block = ext2_alloc_block (node, goal);
  if (!block)
    return EIO;

  *buf = block_image(block);
  node->dn->info.i_data[nr] = block;

  node->dn->info.i_next_alloc_block = new_block;
  node->dn->info.i_next_alloc_goal = block;
  node->dn_set_ctime = 1;
  node->dn_stat.st_blocks += blocks;
  node->dn_stat_dirty = 1;

  if (diskfs_synchronous || node->dn->info.i_osync)
    diskfs_node_update (node, 1);

  return 0;
}

error_t
block_getblk (struct node *node,
	      char *bh, int nr,
	      int create, int blocksize,
	      int new_block, char **buf)
{
  int i, goal = 0;
  u32 block;
  int blocks = block_size / 512;

  block = ((u32 *)bh)[nr];
  if (block)
    {
      *buf = block_image (block);
      return 0;
    }

  if (!create)
    return EINVAL;

  if (node->dn->info.i_next_alloc_block == new_block)
    goal = node->dn->info.i_next_alloc_goal;
  if (!goal)
    {
      for (i = nr - 1; i >= 0; i--)
	{
	  if (((u32 *) bh)[i])
	    {
	      goal = ((u32 *) bh)[i];
	      break;
	    }
	}
      if (!goal)
	goal = addrb(bh);
    }

  block = ext2_alloc_block (node, goal);
  if (!block)
    return EIO;			/* XXX? */

  *buf = block_image (block);
  ((u32 *)bh)[nr] = block;

  if (diskfs_synchronous || node->dn->info.i_osync)
    sync_disk_image (bh, block_size, 1);

  node->dn_set_ctime = 1;
  node->dn->info.i_next_alloc_block = new_block;
  node->dn->info.i_next_alloc_goal = block;
  node->dn_stat.st_blocks += blocks;
  node->dn_stat_dirty = 1;

  return 0;
}

error_t
ext2_getblk (struct node *node, long block, int create, char **buf)
{
  error_t err;
  char *bh;
  unsigned long b;
  unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK (sblock);

  if (block < 0)
    {
      ext2_warning ("ext2_getblk", "block < 0");
      return EIO;
    }
  if (block > EXT2_NDIR_BLOCKS + addr_per_block +
      addr_per_block * addr_per_block +
      addr_per_block * addr_per_block * addr_per_block)
    {
      ext2_warning ("ext2_getblk", "block > big");
      return EIO;
    }
  /*
     * If this is a sequential block allocation, set the next_alloc_block
     * to this block now so that all the indblock and data block
     * allocations use the same goal zone
   */

  ext2_debug ("block %lu, next %lu, goal %lu.\n", block,
	      node->dn->info.i_next_alloc_block,
	      node->dn->info.i_next_alloc_goal);

  if (block == node->dn->info.i_next_alloc_block + 1)
    {
      node->dn->info.i_next_alloc_block++;
      node->dn->info.i_next_alloc_goal++;
    }

  b = block;

  if (block < EXT2_NDIR_BLOCKS)
    return inode_getblk (node, block, create, b, buf);

  block -= EXT2_NDIR_BLOCKS;
  if (block < addr_per_block)
    {
      err = inode_getblk (node, EXT2_IND_BLOCK, create, b, &bh);
      if (!err)
	err = block_getblk (node, bh, block, create, block_size, b, buf);
      return err;
    }

  block -= addr_per_block;
  if (block < addr_per_block * addr_per_block)
    {
      err = inode_getblk (node, EXT2_DIND_BLOCK, create, b, &bh);
      if (!err)
	err = block_getblk (node, bh, block / addr_per_block, create,
			    block_size, b, &bh);
      if (!err)
	err = block_getblk (node, bh, block & (addr_per_block - 1),
			    create, block_size, b, buf);
      return err;
    }

  block -= addr_per_block * addr_per_block;
  err = inode_getblk (node, EXT2_TIND_BLOCK, create, b, &bh);
  if (!err)
    err = block_getblk (node, bh, block / (addr_per_block * addr_per_block),
			create, block_size, b, &bh);
  if (!err)
    err =
      block_getblk (node, bh, (block / addr_per_block) & (addr_per_block - 1),
		    create, block_size, b, &bh);
  if (!err)
    err = block_getblk (node, bh, block & (addr_per_block - 1), create,
			block_size, b, buf);

  return err;
}