file.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * fs/bfs/file.c
  3. * BFS file operations.
  4. * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/smp_lock.h>
  9. #include "bfs.h"
  10. #undef DEBUG
  11. #ifdef DEBUG
  12. #define dprintf(x...) printf(x)
  13. #else
  14. #define dprintf(x...)
  15. #endif
  16. const struct file_operations bfs_file_operations = {
  17. .llseek = generic_file_llseek,
  18. .read = do_sync_read,
  19. .aio_read = generic_file_aio_read,
  20. .write = do_sync_write,
  21. .aio_write = generic_file_aio_write,
  22. .mmap = generic_file_mmap,
  23. .sendfile = generic_file_sendfile,
  24. };
  25. static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
  26. {
  27. struct buffer_head *bh, *new;
  28. bh = sb_bread(sb, from);
  29. if (!bh)
  30. return -EIO;
  31. new = sb_getblk(sb, to);
  32. memcpy(new->b_data, bh->b_data, bh->b_size);
  33. mark_buffer_dirty(new);
  34. bforget(bh);
  35. brelse(new);
  36. return 0;
  37. }
  38. static int bfs_move_blocks(struct super_block *sb, unsigned long start,
  39. unsigned long end, unsigned long where)
  40. {
  41. unsigned long i;
  42. dprintf("%08lx-%08lx->%08lx\n", start, end, where);
  43. for (i = start; i <= end; i++)
  44. if(bfs_move_block(i, where + i, sb)) {
  45. dprintf("failed to move block %08lx -> %08lx\n", i, where + i);
  46. return -EIO;
  47. }
  48. return 0;
  49. }
  50. static int bfs_get_block(struct inode * inode, sector_t block,
  51. struct buffer_head * bh_result, int create)
  52. {
  53. unsigned long phys;
  54. int err;
  55. struct super_block *sb = inode->i_sb;
  56. struct bfs_sb_info *info = BFS_SB(sb);
  57. struct bfs_inode_info *bi = BFS_I(inode);
  58. struct buffer_head *sbh = info->si_sbh;
  59. if (block > info->si_blocks)
  60. return -EIO;
  61. phys = bi->i_sblock + block;
  62. if (!create) {
  63. if (phys <= bi->i_eblock) {
  64. dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
  65. create, (unsigned long)block, phys);
  66. map_bh(bh_result, sb, phys);
  67. }
  68. return 0;
  69. }
  70. /* if the file is not empty and the requested block is within the range
  71. of blocks allocated for this file, we can grant it */
  72. if (inode->i_size && phys <= bi->i_eblock) {
  73. dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
  74. create, (unsigned long)block, phys);
  75. map_bh(bh_result, sb, phys);
  76. return 0;
  77. }
  78. /* the rest has to be protected against itself */
  79. lock_kernel();
  80. /* if the last data block for this file is the last allocated
  81. block, we can extend the file trivially, without moving it
  82. anywhere */
  83. if (bi->i_eblock == info->si_lf_eblk) {
  84. dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
  85. create, (unsigned long)block, phys);
  86. map_bh(bh_result, sb, phys);
  87. info->si_freeb -= phys - bi->i_eblock;
  88. info->si_lf_eblk = bi->i_eblock = phys;
  89. mark_inode_dirty(inode);
  90. mark_buffer_dirty(sbh);
  91. err = 0;
  92. goto out;
  93. }
  94. /* Ok, we have to move this entire file to the next free block */
  95. phys = info->si_lf_eblk + 1;
  96. if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
  97. err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
  98. bi->i_eblock, phys);
  99. if (err) {
  100. dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
  101. goto out;
  102. }
  103. } else
  104. err = 0;
  105. dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
  106. create, (unsigned long)block, phys);
  107. bi->i_sblock = phys;
  108. phys += block;
  109. info->si_lf_eblk = bi->i_eblock = phys;
  110. /* this assumes nothing can write the inode back while we are here
  111. * and thus update inode->i_blocks! (XXX)*/
  112. info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
  113. mark_inode_dirty(inode);
  114. mark_buffer_dirty(sbh);
  115. map_bh(bh_result, sb, phys);
  116. out:
  117. unlock_kernel();
  118. return err;
  119. }
  120. static int bfs_writepage(struct page *page, struct writeback_control *wbc)
  121. {
  122. return block_write_full_page(page, bfs_get_block, wbc);
  123. }
  124. static int bfs_readpage(struct file *file, struct page *page)
  125. {
  126. return block_read_full_page(page, bfs_get_block);
  127. }
  128. static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  129. {
  130. return block_prepare_write(page, from, to, bfs_get_block);
  131. }
  132. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  133. {
  134. return generic_block_bmap(mapping, block, bfs_get_block);
  135. }
  136. const struct address_space_operations bfs_aops = {
  137. .readpage = bfs_readpage,
  138. .writepage = bfs_writepage,
  139. .sync_page = block_sync_page,
  140. .prepare_write = bfs_prepare_write,
  141. .commit_write = generic_commit_write,
  142. .bmap = bfs_bmap,
  143. };
  144. const struct inode_operations bfs_file_inops;