This source file includes following definitions.
- ext2_put_inode
- block_bmap
- ext2_discard_prealloc
- ext2_alloc_block
- ext2_bmap
- inode_getblk
- block_getblk
- ext2_getblk
- ext2_bread
- ext2_read_inode
- ext2_update_inode
- ext2_write_inode
- ext2_sync_inode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <asm/segment.h>
18 #include <asm/system.h>
19
20 #include <linux/errno.h>
21 #include <linux/fs.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/sched.h>
24 #include <linux/stat.h>
25 #include <linux/string.h>
26 #include <linux/locks.h>
27
28 #define clear_block(addr,size) \
29 __asm__("cld\n\t" \
30 "rep\n\t" \
31 "stosl" \
32 : \
33 :"a" (0), "c" (size / 4), "D" ((long) (addr)) \
34 :"cx", "di")
35
36 void ext2_put_inode (struct inode * inode)
37 {
38 ext2_discard_prealloc (inode);
39 if (inode->i_nlink || inode->i_ino == EXT2_ACL_IDX_INO ||
40 inode->i_ino == EXT2_ACL_DATA_INO)
41 return;
42 inode->i_size = 0;
43 if (inode->i_blocks)
44 ext2_truncate (inode);
45 ext2_free_inode (inode);
46 }
47
48 #define inode_bmap(inode, nr) ((inode)->u.ext2_i.i_data[(nr)])
49
50 static int block_bmap (struct buffer_head * bh, int nr)
51 {
52 int tmp;
53
54 if (!bh)
55 return 0;
56 tmp = ((unsigned long *) bh->b_data)[nr];
57 brelse (bh);
58 return tmp;
59 }
60
61
62
63
64
65
66
67
68 void ext2_discard_prealloc (struct inode * inode)
69 {
70 #ifdef EXT2_PREALLOCATE
71 if (inode->u.ext2_i.i_prealloc_count) {
72 ext2_free_blocks (inode->i_sb,
73 inode->u.ext2_i.i_prealloc_block,
74 inode->u.ext2_i.i_prealloc_count);
75 inode->u.ext2_i.i_prealloc_count = 0;
76 }
77 #endif
78 }
79
80 static int ext2_alloc_block (struct inode * inode, unsigned long goal)
81 {
82 #ifdef EXT2FS_DEBUG
83 static unsigned long alloc_hits = 0, alloc_attempts = 0;
84 #endif
85 unsigned long result;
86 struct buffer_head * bh;
87
88 wait_on_super (inode->i_sb);
89
90 #ifdef EXT2_PREALLOCATE
91 if (inode->u.ext2_i.i_prealloc_count &&
92 (goal == inode->u.ext2_i.i_prealloc_block ||
93 goal + 1 == inode->u.ext2_i.i_prealloc_block))
94 {
95 result = inode->u.ext2_i.i_prealloc_block++;
96 inode->u.ext2_i.i_prealloc_count--;
97 ext2_debug ("preallocation hit (%lu/%lu).\n",
98 ++alloc_hits, ++alloc_attempts);
99
100
101
102
103 if (!(bh = getblk (inode->i_sb->s_dev, result,
104 inode->i_sb->s_blocksize))) {
105 ext2_error (inode->i_sb, "ext2_alloc_block",
106 "cannot get block %lu", result);
107 return 0;
108 }
109 clear_block (bh->b_data, inode->i_sb->s_blocksize);
110 bh->b_uptodate = 1;
111 bh->b_dirt = 1;
112 brelse (bh);
113 } else {
114 ext2_discard_prealloc (inode);
115 ext2_debug ("preallocation miss (%lu/%lu).\n",
116 alloc_hits, ++alloc_attempts);
117 if (S_ISREG(inode->i_mode))
118 result = ext2_new_block
119 (inode->i_sb, goal,
120 &inode->u.ext2_i.i_prealloc_count,
121 &inode->u.ext2_i.i_prealloc_block);
122 else
123 result = ext2_new_block (inode->i_sb, goal, 0, 0);
124 }
125 #else
126 result = ext2_new_block (inode->i_sb, goal, 0, 0);
127 #endif
128
129 return result;
130 }
131
132
133 int ext2_bmap (struct inode * inode, int block)
134 {
135 int i;
136 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
137
138 if (block < 0) {
139 ext2_warning (inode->i_sb, "ext2_bmap", "block < 0");
140 return 0;
141 }
142 if (block >= EXT2_NDIR_BLOCKS + addr_per_block +
143 addr_per_block * addr_per_block +
144 addr_per_block * addr_per_block * addr_per_block) {
145 ext2_warning (inode->i_sb, "ext2_bmap", "block > big");
146 return 0;
147 }
148 if (block < EXT2_NDIR_BLOCKS)
149 return inode_bmap (inode, block);
150 block -= EXT2_NDIR_BLOCKS;
151 if (block < addr_per_block) {
152 i = inode_bmap (inode, EXT2_IND_BLOCK);
153 if (!i)
154 return 0;
155 return block_bmap (bread (inode->i_dev, i,
156 inode->i_sb->s_blocksize), block);
157 }
158 block -= addr_per_block;
159 if (block < addr_per_block * addr_per_block) {
160 i = inode_bmap (inode, EXT2_DIND_BLOCK);
161 if (!i)
162 return 0;
163 i = block_bmap (bread (inode->i_dev, i,
164 inode->i_sb->s_blocksize),
165 block / addr_per_block);
166 if (!i)
167 return 0;
168 return block_bmap (bread (inode->i_dev, i,
169 inode->i_sb->s_blocksize),
170 block & (addr_per_block - 1));
171 }
172 block -= addr_per_block * addr_per_block;
173 i = inode_bmap (inode, EXT2_TIND_BLOCK);
174 if (!i)
175 return 0;
176 i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
177 block / (addr_per_block * addr_per_block));
178 if (!i)
179 return 0;
180 i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
181 (block / addr_per_block) & (addr_per_block - 1));
182 if (!i)
183 return 0;
184 return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
185 block & (addr_per_block - 1));
186 }
187
188 static struct buffer_head * inode_getblk (struct inode * inode, int nr,
189 int create, int new_block, int * err)
190 {
191 int tmp, goal = 0;
192 unsigned long * p;
193 struct buffer_head * result;
194 int blocks = inode->i_sb->s_blocksize / 512;
195
196 p = inode->u.ext2_i.i_data + nr;
197 repeat:
198 tmp = *p;
199 if (tmp) {
200 result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
201 if (tmp == *p)
202 return result;
203 brelse (result);
204 goto repeat;
205 }
206 if (!create || new_block >=
207 (current->rlim[RLIMIT_FSIZE].rlim_cur >>
208 EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
209 *err = -EFBIG;
210 return NULL;
211 }
212 if (inode->u.ext2_i.i_next_alloc_block == new_block)
213 goal = inode->u.ext2_i.i_next_alloc_goal;
214
215 ext2_debug ("hint = %d,", goal);
216
217 if (!goal) {
218 for (tmp = nr - 1; tmp >= 0; tmp--) {
219 if (inode->u.ext2_i.i_data[tmp]) {
220 goal = inode->u.ext2_i.i_data[tmp];
221 break;
222 }
223 }
224 if (!goal)
225 goal = (inode->u.ext2_i.i_block_group *
226 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
227 inode->i_sb->u.ext2_sb.s_es->s_first_data_block;
228 }
229
230 ext2_debug ("goal = %d.\n", goal);
231
232 tmp = ext2_alloc_block (inode, goal);
233 if (!tmp)
234 return NULL;
235 result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
236 if (*p) {
237 ext2_free_blocks (inode->i_sb, tmp, 1);
238 brelse (result);
239 goto repeat;
240 }
241 *p = tmp;
242 inode->u.ext2_i.i_next_alloc_block = new_block;
243 inode->u.ext2_i.i_next_alloc_goal = tmp;
244 inode->i_ctime = CURRENT_TIME;
245 inode->i_blocks += blocks;
246 if (IS_SYNC(inode))
247 ext2_sync_inode (inode);
248 else
249 inode->i_dirt = 1;
250 return result;
251 }
252
253 static struct buffer_head * block_getblk (struct inode * inode,
254 struct buffer_head * bh, int nr,
255 int create, int blocksize,
256 int new_block, int * err)
257 {
258 int tmp, goal = 0;
259 unsigned long * p;
260 struct buffer_head * result;
261 int blocks = inode->i_sb->s_blocksize / 512;
262
263 if (!bh)
264 return NULL;
265 if (!bh->b_uptodate) {
266 ll_rw_block (READ, 1, &bh);
267 wait_on_buffer (bh);
268 if (!bh->b_uptodate) {
269 brelse (bh);
270 return NULL;
271 }
272 }
273 p = (unsigned long *) bh->b_data + nr;
274 repeat:
275 tmp = *p;
276 if (tmp) {
277 result = getblk (bh->b_dev, tmp, blocksize);
278 if (tmp == *p) {
279 brelse (bh);
280 return result;
281 }
282 brelse (result);
283 goto repeat;
284 }
285 if (!create || new_block >=
286 (current->rlim[RLIMIT_FSIZE].rlim_cur >>
287 EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
288 brelse (bh);
289 *err = -EFBIG;
290 return NULL;
291 }
292 if (inode->u.ext2_i.i_next_alloc_block == new_block)
293 goal = inode->u.ext2_i.i_next_alloc_goal;
294 if (!goal) {
295 for (tmp = nr - 1; tmp >= 0; tmp--) {
296 if (((unsigned long *) bh->b_data)[tmp]) {
297 goal = ((unsigned long *)bh->b_data)[tmp];
298 break;
299 }
300 }
301 if (!goal)
302 goal = bh->b_blocknr;
303 }
304 tmp = ext2_alloc_block (inode, goal);
305 if (!tmp) {
306 brelse (bh);
307 return NULL;
308 }
309 result = getblk (bh->b_dev, tmp, blocksize);
310 if (*p) {
311 ext2_free_blocks (inode->i_sb, tmp, 1);
312 brelse (result);
313 goto repeat;
314 }
315 *p = tmp;
316 bh->b_dirt = 1;
317 if (IS_SYNC(inode)) {
318 ll_rw_block (WRITE, 1, &bh);
319 wait_on_buffer (bh);
320 }
321 inode->i_ctime = CURRENT_TIME;
322 inode->i_blocks += blocks;
323 inode->i_dirt = 1;
324 inode->u.ext2_i.i_next_alloc_block = new_block;
325 inode->u.ext2_i.i_next_alloc_goal = tmp;
326 brelse (bh);
327 return result;
328 }
329
330 struct buffer_head * ext2_getblk (struct inode * inode, long block,
331 int create, int * err)
332 {
333 struct buffer_head * bh;
334 unsigned long b;
335 unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
336
337 *err = -EIO;
338 if (block < 0) {
339 ext2_warning (inode->i_sb, "ext2_getblk", "block < 0");
340 return NULL;
341 }
342 if (block > EXT2_NDIR_BLOCKS + addr_per_block +
343 addr_per_block * addr_per_block +
344 addr_per_block * addr_per_block * addr_per_block) {
345 ext2_warning (inode->i_sb, "ext2_getblk", "block > big");
346 return NULL;
347 }
348
349
350
351
352
353
354 ext2_debug ("block %lu, next %lu, goal %lu.\n", block,
355 inode->u.ext2_i.i_next_alloc_block,
356 inode->u.ext2_i.i_next_alloc_goal);
357
358 if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
359 inode->u.ext2_i.i_next_alloc_block++;
360 inode->u.ext2_i.i_next_alloc_goal++;
361 }
362
363 *err = -ENOSPC;
364 b = block;
365 if (block < EXT2_NDIR_BLOCKS)
366 return inode_getblk (inode, block, create, b, err);
367 block -= EXT2_NDIR_BLOCKS;
368 if (block < addr_per_block) {
369 bh = inode_getblk (inode, EXT2_IND_BLOCK, create, b, err);
370 return block_getblk (inode, bh, block, create,
371 inode->i_sb->s_blocksize, b, err);
372 }
373 block -= addr_per_block;
374 if (block < addr_per_block * addr_per_block) {
375 bh = inode_getblk (inode, EXT2_DIND_BLOCK, create, b, err);
376 bh = block_getblk (inode, bh, block / addr_per_block, create,
377 inode->i_sb->s_blocksize, b, err);
378 return block_getblk (inode, bh, block & (addr_per_block - 1),
379 create, inode->i_sb->s_blocksize, b, err);
380 }
381 block -= addr_per_block * addr_per_block;
382 bh = inode_getblk (inode, EXT2_TIND_BLOCK, create, b, err);
383 bh = block_getblk (inode, bh, block/(addr_per_block * addr_per_block),
384 create, inode->i_sb->s_blocksize, b, err);
385 bh = block_getblk (inode, bh, (block/addr_per_block) & (addr_per_block - 1),
386 create, inode->i_sb->s_blocksize, b, err);
387 return block_getblk (inode, bh, block & (addr_per_block - 1), create,
388 inode->i_sb->s_blocksize, b, err);
389 }
390
391 struct buffer_head * ext2_bread (struct inode * inode, int block,
392 int create, int *err)
393 {
394 struct buffer_head * bh;
395
396 bh = ext2_getblk (inode, block, create, err);
397 if (!bh || bh->b_uptodate)
398 return bh;
399 ll_rw_block (READ, 1, &bh);
400 wait_on_buffer (bh);
401 if (bh->b_uptodate)
402 return bh;
403 brelse (bh);
404 *err = -EIO;
405 return NULL;
406 }
407
408 void ext2_read_inode (struct inode * inode)
409 {
410 struct buffer_head * bh;
411 struct ext2_inode * raw_inode;
412 unsigned long block_group;
413 unsigned long group_desc;
414 unsigned long desc;
415 unsigned long block;
416 struct ext2_group_desc * gdp;
417
418 if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
419 inode->i_ino != EXT2_ACL_DATA_INO && inode->i_ino < EXT2_FIRST_INO) ||
420 inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
421 ext2_error (inode->i_sb, "ext2_read_inode",
422 "bad inode number: %lu", inode->i_ino);
423 return;
424 }
425 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
426 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
427 ext2_panic (inode->i_sb, "ext2_read_inode",
428 "group >= groups count");
429 group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
430 desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
431 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
432 if (!bh)
433 ext2_panic (inode->i_sb, "ext2_read_inode",
434 "Descriptor not loaded");
435 gdp = (struct ext2_group_desc *) bh->b_data;
436 block = gdp[desc].bg_inode_table +
437 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
438 / EXT2_INODES_PER_BLOCK(inode->i_sb));
439 if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
440 ext2_panic (inode->i_sb, "ext2_read_inode",
441 "unable to read i-node block\n"
442 "inode=%lu, block=%lu", inode->i_ino, block);
443 raw_inode = ((struct ext2_inode *) bh->b_data) +
444 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
445 inode->i_mode = raw_inode->i_mode;
446 inode->i_uid = raw_inode->i_uid;
447 inode->i_gid = raw_inode->i_gid;
448 inode->i_nlink = raw_inode->i_links_count;
449 inode->i_size = raw_inode->i_size;
450 inode->i_atime = raw_inode->i_atime;
451 inode->i_ctime = raw_inode->i_ctime;
452 inode->i_mtime = raw_inode->i_mtime;
453 inode->u.ext2_i.i_dtime = raw_inode->i_dtime;
454 inode->i_blksize = inode->i_sb->s_blocksize;
455 inode->i_blocks = raw_inode->i_blocks;
456 inode->u.ext2_i.i_flags = raw_inode->i_flags;
457 inode->u.ext2_i.i_faddr = raw_inode->i_faddr;
458 inode->u.ext2_i.i_frag = raw_inode->i_frag;
459 inode->u.ext2_i.i_fsize = raw_inode->i_fsize;
460 inode->u.ext2_i.i_file_acl = raw_inode->i_file_acl;
461 inode->u.ext2_i.i_dir_acl = raw_inode->i_dir_acl;
462 inode->u.ext2_i.i_version = raw_inode->i_version;
463 inode->u.ext2_i.i_block_group = block_group;
464 inode->u.ext2_i.i_next_alloc_block = 0;
465 inode->u.ext2_i.i_next_alloc_goal = 0;
466 if (inode->u.ext2_i.i_prealloc_count)
467 ext2_error (inode->i_sb, "ext2_read_inode",
468 "New inode has non-zero prealloc count!");
469 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
470 inode->i_rdev = raw_inode->i_block[0];
471 else for (block = 0; block < EXT2_N_BLOCKS; block++)
472 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
473 brelse (bh);
474 inode->i_op = NULL;
475 if (inode->i_ino == EXT2_ACL_IDX_INO ||
476 inode->i_ino == EXT2_ACL_DATA_INO)
477 ;
478 else if (S_ISREG(inode->i_mode))
479 inode->i_op = &ext2_file_inode_operations;
480 else if (S_ISDIR(inode->i_mode))
481 inode->i_op = &ext2_dir_inode_operations;
482 else if (S_ISLNK(inode->i_mode))
483 inode->i_op = &ext2_symlink_inode_operations;
484 else if (S_ISCHR(inode->i_mode))
485 inode->i_op = &chrdev_inode_operations;
486 else if (S_ISBLK(inode->i_mode))
487 inode->i_op = &blkdev_inode_operations;
488 else if (S_ISFIFO(inode->i_mode))
489 init_fifo(inode);
490 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
491 inode->i_flags |= MS_SYNC;
492 }
493
494 static struct buffer_head * ext2_update_inode (struct inode * inode)
495 {
496 struct buffer_head * bh;
497 struct ext2_inode * raw_inode;
498 unsigned long block_group;
499 unsigned long group_desc;
500 unsigned long desc;
501 unsigned long block;
502 struct ext2_group_desc * gdp;
503
504 if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino < EXT2_FIRST_INO) ||
505 inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
506 ext2_error (inode->i_sb, "ext2_write_inode",
507 "bad inode number: %lu", inode->i_ino);
508 return 0;
509 }
510 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
511 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
512 ext2_panic (inode->i_sb, "ext2_write_inode",
513 "group >= groups count");
514 group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
515 desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
516 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
517 if (!bh)
518 ext2_panic (inode->i_sb, "ext2_write_inode",
519 "Descriptor not loaded");
520 gdp = (struct ext2_group_desc *) bh->b_data;
521 block = gdp[desc].bg_inode_table +
522 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
523 / EXT2_INODES_PER_BLOCK(inode->i_sb));
524 if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
525 ext2_panic (inode->i_sb, "ext2_write_inode",
526 "unable to read i-node block\n"
527 "inode=%lu, block=%lu", inode->i_ino, block);
528 raw_inode = ((struct ext2_inode *)bh->b_data) +
529 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
530 raw_inode->i_mode = inode->i_mode;
531 raw_inode->i_uid = inode->i_uid;
532 raw_inode->i_gid = inode->i_gid;
533 raw_inode->i_links_count = inode->i_nlink;
534 raw_inode->i_size = inode->i_size;
535 raw_inode->i_atime = inode->i_atime;
536 raw_inode->i_ctime = inode->i_ctime;
537 raw_inode->i_mtime = inode->i_mtime;
538 raw_inode->i_blocks = inode->i_blocks;
539 raw_inode->i_dtime = inode->u.ext2_i.i_dtime;
540 raw_inode->i_flags = inode->u.ext2_i.i_flags;
541 raw_inode->i_faddr = inode->u.ext2_i.i_faddr;
542 raw_inode->i_frag = inode->u.ext2_i.i_frag;
543 raw_inode->i_fsize = inode->u.ext2_i.i_fsize;
544 raw_inode->i_file_acl = inode->u.ext2_i.i_file_acl;
545 raw_inode->i_dir_acl = inode->u.ext2_i.i_dir_acl;
546 raw_inode->i_version = inode->u.ext2_i.i_version;
547 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
548 raw_inode->i_block[0] = inode->i_rdev;
549 else for (block = 0; block < EXT2_N_BLOCKS; block++)
550 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
551 bh->b_dirt = 1;
552 inode->i_dirt = 0;
553 return bh;
554 }
555
556 void ext2_write_inode (struct inode * inode)
557 {
558 struct buffer_head * bh;
559 bh = ext2_update_inode (inode);
560 brelse (bh);
561 }
562
563 int ext2_sync_inode (struct inode *inode)
564 {
565 int err = 0;
566 struct buffer_head *bh;
567
568 bh = ext2_update_inode (inode);
569 if (bh && bh->b_dirt)
570 {
571 ll_rw_block (WRITE, 1, &bh);
572 wait_on_buffer (bh);
573 if (bh->b_req && !bh->b_uptodate)
574 {
575 printk ("IO error syncing ext2 inode [%04x:%08lx]\n",
576 inode->i_dev, inode->i_ino);
577 err = -1;
578 }
579 }
580 else if (!bh)
581 err = -1;
582 brelse (bh);
583 return err;
584 }