buffer.c 3.9 KB

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