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