buffer.c 3.9 KB

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