buffer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * linux/fs/hpfs/buffer.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * general buffer i/o
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/blkdev.h>
  11. #include "hpfs_fn.h"
  12. void hpfs_prefetch_sectors(struct super_block *s, unsigned secno, int n)
  13. {
  14. struct buffer_head *bh;
  15. struct blk_plug plug;
  16. if (n <= 0 || unlikely(secno >= hpfs_sb(s)->sb_fs_size))
  17. return;
  18. bh = sb_find_get_block(s, secno);
  19. if (bh) {
  20. if (buffer_uptodate(bh)) {
  21. brelse(bh);
  22. return;
  23. }
  24. brelse(bh);
  25. };
  26. blk_start_plug(&plug);
  27. while (n > 0) {
  28. if (unlikely(secno >= hpfs_sb(s)->sb_fs_size))
  29. break;
  30. sb_breadahead(s, secno);
  31. secno++;
  32. n--;
  33. }
  34. blk_finish_plug(&plug);
  35. }
  36. /* Map a sector into a buffer and return pointers to it and to the buffer. */
  37. void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
  38. int ahead)
  39. {
  40. struct buffer_head *bh;
  41. hpfs_lock_assert(s);
  42. hpfs_prefetch_sectors(s, secno, ahead);
  43. cond_resched();
  44. *bhp = bh = sb_bread(s, secno);
  45. if (bh != NULL)
  46. return bh->b_data;
  47. else {
  48. printk("HPFS: hpfs_map_sector: read error\n");
  49. return NULL;
  50. }
  51. }
  52. /* Like hpfs_map_sector but don't read anything */
  53. void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
  54. {
  55. struct buffer_head *bh;
  56. /*return hpfs_map_sector(s, secno, bhp, 0);*/
  57. hpfs_lock_assert(s);
  58. cond_resched();
  59. if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
  60. if (!buffer_uptodate(bh)) wait_on_buffer(bh);
  61. set_buffer_uptodate(bh);
  62. return bh->b_data;
  63. } else {
  64. printk("HPFS: hpfs_get_sector: getblk failed\n");
  65. return NULL;
  66. }
  67. }
  68. /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
  69. void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
  70. int ahead)
  71. {
  72. struct buffer_head *bh;
  73. char *data;
  74. hpfs_lock_assert(s);
  75. cond_resched();
  76. if (secno & 3) {
  77. printk("HPFS: hpfs_map_4sectors: unaligned read\n");
  78. return NULL;
  79. }
  80. hpfs_prefetch_sectors(s, secno, 4 + ahead);
  81. qbh->data = data = kmalloc(2048, GFP_NOFS);
  82. if (!data) {
  83. printk("HPFS: hpfs_map_4sectors: out of memory\n");
  84. goto bail;
  85. }
  86. qbh->bh[0] = bh = sb_bread(s, secno);
  87. if (!bh)
  88. goto bail0;
  89. memcpy(data, bh->b_data, 512);
  90. qbh->bh[1] = bh = sb_bread(s, secno + 1);
  91. if (!bh)
  92. goto bail1;
  93. memcpy(data + 512, bh->b_data, 512);
  94. qbh->bh[2] = bh = sb_bread(s, secno + 2);
  95. if (!bh)
  96. goto bail2;
  97. memcpy(data + 2 * 512, bh->b_data, 512);
  98. qbh->bh[3] = bh = sb_bread(s, secno + 3);
  99. if (!bh)
  100. goto bail3;
  101. memcpy(data + 3 * 512, bh->b_data, 512);
  102. return data;
  103. bail3:
  104. brelse(qbh->bh[2]);
  105. bail2:
  106. brelse(qbh->bh[1]);
  107. bail1:
  108. brelse(qbh->bh[0]);
  109. bail0:
  110. kfree(data);
  111. printk("HPFS: hpfs_map_4sectors: read error\n");
  112. bail:
  113. return NULL;
  114. }
  115. /* Don't read sectors */
  116. void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
  117. struct quad_buffer_head *qbh)
  118. {
  119. cond_resched();
  120. hpfs_lock_assert(s);
  121. if (secno & 3) {
  122. printk("HPFS: hpfs_get_4sectors: unaligned read\n");
  123. return NULL;
  124. }
  125. /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
  126. if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
  127. printk("HPFS: hpfs_get_4sectors: out of memory\n");
  128. return NULL;
  129. }
  130. if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
  131. if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
  132. if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
  133. if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
  134. memcpy(qbh->data, qbh->bh[0]->b_data, 512);
  135. memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
  136. memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
  137. memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
  138. return qbh->data;
  139. bail3: brelse(qbh->bh[2]);
  140. bail2: brelse(qbh->bh[1]);
  141. bail1: brelse(qbh->bh[0]);
  142. bail0:
  143. return NULL;
  144. }
  145. void hpfs_brelse4(struct quad_buffer_head *qbh)
  146. {
  147. brelse(qbh->bh[3]);
  148. brelse(qbh->bh[2]);
  149. brelse(qbh->bh[1]);
  150. brelse(qbh->bh[0]);
  151. kfree(qbh->data);
  152. }
  153. void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
  154. {
  155. memcpy(qbh->bh[0]->b_data, qbh->data, 512);
  156. memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
  157. memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
  158. memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
  159. mark_buffer_dirty(qbh->bh[0]);
  160. mark_buffer_dirty(qbh->bh[1]);
  161. mark_buffer_dirty(qbh->bh[2]);
  162. mark_buffer_dirty(qbh->bh[3]);
  163. }