buffer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "hpfs_fn.h"
  11. /* Map a sector into a buffer and return pointers to it and to the buffer. */
  12. void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
  13. int ahead)
  14. {
  15. struct buffer_head *bh;
  16. hpfs_lock_assert(s);
  17. cond_resched();
  18. *bhp = bh = sb_bread(s, secno);
  19. if (bh != NULL)
  20. return bh->b_data;
  21. else {
  22. printk("HPFS: hpfs_map_sector: read error\n");
  23. return NULL;
  24. }
  25. }
  26. /* Like hpfs_map_sector but don't read anything */
  27. void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
  28. {
  29. struct buffer_head *bh;
  30. /*return hpfs_map_sector(s, secno, bhp, 0);*/
  31. hpfs_lock_assert(s);
  32. cond_resched();
  33. if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
  34. if (!buffer_uptodate(bh)) wait_on_buffer(bh);
  35. set_buffer_uptodate(bh);
  36. return bh->b_data;
  37. } else {
  38. printk("HPFS: hpfs_get_sector: getblk failed\n");
  39. return NULL;
  40. }
  41. }
  42. /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
  43. void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
  44. int ahead)
  45. {
  46. struct buffer_head *bh;
  47. char *data;
  48. hpfs_lock_assert(s);
  49. cond_resched();
  50. if (secno & 3) {
  51. printk("HPFS: hpfs_map_4sectors: unaligned read\n");
  52. return NULL;
  53. }
  54. qbh->data = data = kmalloc(2048, GFP_NOFS);
  55. if (!data) {
  56. printk("HPFS: hpfs_map_4sectors: out of memory\n");
  57. goto bail;
  58. }
  59. qbh->bh[0] = bh = sb_bread(s, secno);
  60. if (!bh)
  61. goto bail0;
  62. memcpy(data, bh->b_data, 512);
  63. qbh->bh[1] = bh = sb_bread(s, secno + 1);
  64. if (!bh)
  65. goto bail1;
  66. memcpy(data + 512, bh->b_data, 512);
  67. qbh->bh[2] = bh = sb_bread(s, secno + 2);
  68. if (!bh)
  69. goto bail2;
  70. memcpy(data + 2 * 512, bh->b_data, 512);
  71. qbh->bh[3] = bh = sb_bread(s, secno + 3);
  72. if (!bh)
  73. goto bail3;
  74. memcpy(data + 3 * 512, bh->b_data, 512);
  75. return data;
  76. bail3:
  77. brelse(qbh->bh[2]);
  78. bail2:
  79. brelse(qbh->bh[1]);
  80. bail1:
  81. brelse(qbh->bh[0]);
  82. bail0:
  83. kfree(data);
  84. printk("HPFS: hpfs_map_4sectors: read error\n");
  85. bail:
  86. return NULL;
  87. }
  88. /* Don't read sectors */
  89. void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
  90. struct quad_buffer_head *qbh)
  91. {
  92. cond_resched();
  93. hpfs_lock_assert(s);
  94. if (secno & 3) {
  95. printk("HPFS: hpfs_get_4sectors: unaligned read\n");
  96. return NULL;
  97. }
  98. /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
  99. if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
  100. printk("HPFS: hpfs_get_4sectors: out of memory\n");
  101. return NULL;
  102. }
  103. if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
  104. if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
  105. if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
  106. if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
  107. memcpy(qbh->data, qbh->bh[0]->b_data, 512);
  108. memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
  109. memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
  110. memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
  111. return qbh->data;
  112. bail3: brelse(qbh->bh[2]);
  113. bail2: brelse(qbh->bh[1]);
  114. bail1: brelse(qbh->bh[0]);
  115. bail0:
  116. return NULL;
  117. }
  118. void hpfs_brelse4(struct quad_buffer_head *qbh)
  119. {
  120. brelse(qbh->bh[3]);
  121. brelse(qbh->bh[2]);
  122. brelse(qbh->bh[1]);
  123. brelse(qbh->bh[0]);
  124. kfree(qbh->data);
  125. }
  126. void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
  127. {
  128. memcpy(qbh->bh[0]->b_data, qbh->data, 512);
  129. memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
  130. memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
  131. memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
  132. mark_buffer_dirty(qbh->bh[0]);
  133. mark_buffer_dirty(qbh->bh[1]);
  134. mark_buffer_dirty(qbh->bh[2]);
  135. mark_buffer_dirty(qbh->bh[3]);
  136. }