datastream.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * linux/fs/befs/datastream.c
  3. *
  4. * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com>
  5. *
  6. * Based on portions of file.c by Makoto Kato <m_kato@ga2.so-net.ne.jp>
  7. *
  8. * Many thanks to Dominic Giampaolo, author of "Practical File System
  9. * Design with the Be File System", for such a helpful book.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/string.h>
  16. #include "befs.h"
  17. #include "datastream.h"
  18. #include "io.h"
  19. #include "endian.h"
  20. const befs_inode_addr BAD_IADDR = { 0, 0, 0 };
  21. static int befs_find_brun_direct(struct super_block *sb,
  22. befs_data_stream * data,
  23. befs_blocknr_t blockno, befs_block_run * run);
  24. static int befs_find_brun_indirect(struct super_block *sb,
  25. befs_data_stream * data,
  26. befs_blocknr_t blockno,
  27. befs_block_run * run);
  28. static int befs_find_brun_dblindirect(struct super_block *sb,
  29. befs_data_stream * data,
  30. befs_blocknr_t blockno,
  31. befs_block_run * run);
  32. /**
  33. * befs_read_datastream - get buffer_head containing data, starting from pos.
  34. * @sb: Filesystem superblock
  35. * @ds: datastrem to find data with
  36. * @pos: start of data
  37. * @off: offset of data in buffer_head->b_data
  38. *
  39. * Returns pointer to buffer_head containing data starting with offset @off,
  40. * if you don't need to know offset just set @off = NULL.
  41. */
  42. struct buffer_head *
  43. befs_read_datastream(struct super_block *sb, befs_data_stream * ds,
  44. befs_off_t pos, uint * off)
  45. {
  46. struct buffer_head *bh = NULL;
  47. befs_block_run run;
  48. befs_blocknr_t block; /* block coresponding to pos */
  49. befs_debug(sb, "---> befs_read_datastream() %Lu", pos);
  50. block = pos >> BEFS_SB(sb)->block_shift;
  51. if (off)
  52. *off = pos - (block << BEFS_SB(sb)->block_shift);
  53. if (befs_fblock2brun(sb, ds, block, &run) != BEFS_OK) {
  54. befs_error(sb, "BeFS: Error finding disk addr of block %lu",
  55. block);
  56. befs_debug(sb, "<--- befs_read_datastream() ERROR");
  57. return NULL;
  58. }
  59. bh = befs_bread_iaddr(sb, run);
  60. if (!bh) {
  61. befs_error(sb, "BeFS: Error reading block %lu from datastream",
  62. block);
  63. return NULL;
  64. }
  65. befs_debug(sb, "<--- befs_read_datastream() read data, starting at %Lu",
  66. pos);
  67. return bh;
  68. }
  69. /*
  70. * Takes a file position and gives back a brun who's starting block
  71. * is block number fblock of the file.
  72. *
  73. * Returns BEFS_OK or BEFS_ERR.
  74. *
  75. * Calls specialized functions for each of the three possible
  76. * datastream regions.
  77. *
  78. * 2001-11-15 Will Dyson
  79. */
  80. int
  81. befs_fblock2brun(struct super_block *sb, befs_data_stream * data,
  82. befs_blocknr_t fblock, befs_block_run * run)
  83. {
  84. int err;
  85. befs_off_t pos = fblock << BEFS_SB(sb)->block_shift;
  86. if (pos < data->max_direct_range) {
  87. err = befs_find_brun_direct(sb, data, fblock, run);
  88. } else if (pos < data->max_indirect_range) {
  89. err = befs_find_brun_indirect(sb, data, fblock, run);
  90. } else if (pos < data->max_double_indirect_range) {
  91. err = befs_find_brun_dblindirect(sb, data, fblock, run);
  92. } else {
  93. befs_error(sb,
  94. "befs_fblock2brun() was asked to find block %lu, "
  95. "which is not mapped by the datastream\n", fblock);
  96. err = BEFS_ERR;
  97. }
  98. return err;
  99. }
  100. /**
  101. * befs_read_lsmylink - read long symlink from datastream.
  102. * @sb: Filesystem superblock
  103. * @ds: Datastrem to read from
  104. * @buf: Buffer in which to place long symlink data
  105. * @len: Length of the long symlink in bytes
  106. *
  107. * Returns the number of bytes read
  108. */
  109. size_t
  110. befs_read_lsymlink(struct super_block * sb, befs_data_stream * ds, void *buff,
  111. befs_off_t len)
  112. {
  113. befs_off_t bytes_read = 0; /* bytes readed */
  114. u16 plen;
  115. struct buffer_head *bh = NULL;
  116. befs_debug(sb, "---> befs_read_lsymlink() length: %Lu", len);
  117. while (bytes_read < len) {
  118. bh = befs_read_datastream(sb, ds, bytes_read, NULL);
  119. if (!bh) {
  120. befs_error(sb, "BeFS: Error reading datastream block "
  121. "starting from %Lu", bytes_read);
  122. befs_debug(sb, "<--- befs_read_lsymlink() ERROR");
  123. return bytes_read;
  124. }
  125. plen = ((bytes_read + BEFS_SB(sb)->block_size) < len) ?
  126. BEFS_SB(sb)->block_size : len - bytes_read;
  127. memcpy(buff + bytes_read, bh->b_data, plen);
  128. brelse(bh);
  129. bytes_read += plen;
  130. }
  131. befs_debug(sb, "<--- befs_read_lsymlink() read %u bytes", bytes_read);
  132. return bytes_read;
  133. }
  134. /**
  135. * befs_count_blocks - blocks used by a file
  136. * @sb: Filesystem superblock
  137. * @ds: Datastream of the file
  138. *
  139. * Counts the number of fs blocks that the file represented by
  140. * inode occupies on the filesystem, counting both regular file
  141. * data and filesystem metadata (and eventually attribute data
  142. * when we support attributes)
  143. */
  144. befs_blocknr_t
  145. befs_count_blocks(struct super_block * sb, befs_data_stream * ds)
  146. {
  147. befs_blocknr_t blocks;
  148. befs_blocknr_t datablocks; /* File data blocks */
  149. befs_blocknr_t metablocks; /* FS metadata blocks */
  150. befs_sb_info *befs_sb = BEFS_SB(sb);
  151. befs_debug(sb, "---> befs_count_blocks()");
  152. datablocks = ds->size >> befs_sb->block_shift;
  153. if (ds->size & (befs_sb->block_size - 1))
  154. datablocks += 1;
  155. metablocks = 1; /* Start with 1 block for inode */
  156. /* Size of indirect block */
  157. if (ds->size > ds->max_direct_range)
  158. metablocks += ds->indirect.len;
  159. /*
  160. Double indir block, plus all the indirect blocks it mapps
  161. In the double-indirect range, all block runs of data are
  162. BEFS_DBLINDIR_BRUN_LEN blocks long. Therefore, we know
  163. how many data block runs are in the double-indirect region,
  164. and from that we know how many indirect blocks it takes to
  165. map them. We assume that the indirect blocks are also
  166. BEFS_DBLINDIR_BRUN_LEN blocks long.
  167. */
  168. if (ds->size > ds->max_indirect_range && ds->max_indirect_range != 0) {
  169. uint dbl_bytes;
  170. uint dbl_bruns;
  171. uint indirblocks;
  172. dbl_bytes =
  173. ds->max_double_indirect_range - ds->max_indirect_range;
  174. dbl_bruns =
  175. dbl_bytes / (befs_sb->block_size * BEFS_DBLINDIR_BRUN_LEN);
  176. indirblocks = dbl_bruns / befs_iaddrs_per_block(sb);
  177. metablocks += ds->double_indirect.len;
  178. metablocks += indirblocks;
  179. }
  180. blocks = datablocks + metablocks;
  181. befs_debug(sb, "<--- befs_count_blocks() %u blocks", blocks);
  182. return blocks;
  183. }
  184. /*
  185. Finds the block run that starts at file block number blockno
  186. in the file represented by the datastream data, if that
  187. blockno is in the direct region of the datastream.
  188. sb: the superblock
  189. data: the datastream
  190. blockno: the blocknumber to find
  191. run: The found run is passed back through this pointer
  192. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  193. otherwise.
  194. Algorithm:
  195. Linear search. Checks each element of array[] to see if it
  196. contains the blockno-th filesystem block. This is necessary
  197. because the block runs map variable amounts of data. Simply
  198. keeps a count of the number of blocks searched so far (sum),
  199. incrementing this by the length of each block run as we come
  200. across it. Adds sum to *count before returning (this is so
  201. you can search multiple arrays that are logicaly one array,
  202. as in the indirect region code).
  203. When/if blockno is found, if blockno is inside of a block
  204. run as stored on disk, we offset the start and lenght members
  205. of the block run, so that blockno is the start and len is
  206. still valid (the run ends in the same place).
  207. 2001-11-15 Will Dyson
  208. */
  209. static int
  210. befs_find_brun_direct(struct super_block *sb, befs_data_stream * data,
  211. befs_blocknr_t blockno, befs_block_run * run)
  212. {
  213. int i;
  214. befs_block_run *array = data->direct;
  215. befs_blocknr_t sum;
  216. befs_blocknr_t max_block =
  217. data->max_direct_range >> BEFS_SB(sb)->block_shift;
  218. befs_debug(sb, "---> befs_find_brun_direct(), find %lu", blockno);
  219. if (blockno > max_block) {
  220. befs_error(sb, "befs_find_brun_direct() passed block outside of"
  221. "direct region");
  222. return BEFS_ERR;
  223. }
  224. for (i = 0, sum = 0; i < BEFS_NUM_DIRECT_BLOCKS;
  225. sum += array[i].len, i++) {
  226. if (blockno >= sum && blockno < sum + (array[i].len)) {
  227. int offset = blockno - sum;
  228. run->allocation_group = array[i].allocation_group;
  229. run->start = array[i].start + offset;
  230. run->len = array[i].len - offset;
  231. befs_debug(sb, "---> befs_find_brun_direct(), "
  232. "found %lu at direct[%d]", blockno, i);
  233. return BEFS_OK;
  234. }
  235. }
  236. befs_debug(sb, "---> befs_find_brun_direct() ERROR");
  237. return BEFS_ERR;
  238. }
  239. /*
  240. Finds the block run that starts at file block number blockno
  241. in the file represented by the datastream data, if that
  242. blockno is in the indirect region of the datastream.
  243. sb: the superblock
  244. data: the datastream
  245. blockno: the blocknumber to find
  246. run: The found run is passed back through this pointer
  247. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  248. otherwise.
  249. Algorithm:
  250. For each block in the indirect run of the datastream, read
  251. it in and search through it for search_blk.
  252. XXX:
  253. Really should check to make sure blockno is inside indirect
  254. region.
  255. 2001-11-15 Will Dyson
  256. */
  257. static int
  258. befs_find_brun_indirect(struct super_block *sb,
  259. befs_data_stream * data, befs_blocknr_t blockno,
  260. befs_block_run * run)
  261. {
  262. int i, j;
  263. befs_blocknr_t sum = 0;
  264. befs_blocknr_t indir_start_blk;
  265. befs_blocknr_t search_blk;
  266. struct buffer_head *indirblock;
  267. befs_block_run *array;
  268. befs_block_run indirect = data->indirect;
  269. befs_blocknr_t indirblockno = iaddr2blockno(sb, &indirect);
  270. int arraylen = befs_iaddrs_per_block(sb);
  271. befs_debug(sb, "---> befs_find_brun_indirect(), find %lu", blockno);
  272. indir_start_blk = data->max_direct_range >> BEFS_SB(sb)->block_shift;
  273. search_blk = blockno - indir_start_blk;
  274. /* Examine blocks of the indirect run one at a time */
  275. for (i = 0; i < indirect.len; i++) {
  276. indirblock = befs_bread(sb, indirblockno + i);
  277. if (indirblock == NULL) {
  278. befs_debug(sb,
  279. "---> befs_find_brun_indirect() failed to "
  280. "read disk block %lu from the indirect brun",
  281. indirblockno + i);
  282. return BEFS_ERR;
  283. }
  284. array = (befs_block_run *) indirblock->b_data;
  285. for (j = 0; j < arraylen; ++j) {
  286. int len = fs16_to_cpu(sb, array[j].len);
  287. if (search_blk >= sum && search_blk < sum + len) {
  288. int offset = search_blk - sum;
  289. run->allocation_group =
  290. fs32_to_cpu(sb, array[j].allocation_group);
  291. run->start =
  292. fs16_to_cpu(sb, array[j].start) + offset;
  293. run->len =
  294. fs16_to_cpu(sb, array[j].len) - offset;
  295. brelse(indirblock);
  296. befs_debug(sb,
  297. "<--- befs_find_brun_indirect() found "
  298. "file block %lu at indirect[%d]",
  299. blockno, j + (i * arraylen));
  300. return BEFS_OK;
  301. }
  302. sum += len;
  303. }
  304. brelse(indirblock);
  305. }
  306. /* Only fallthrough is an error */
  307. befs_error(sb, "BeFS: befs_find_brun_indirect() failed to find "
  308. "file block %lu", blockno);
  309. befs_debug(sb, "<--- befs_find_brun_indirect() ERROR");
  310. return BEFS_ERR;
  311. }
  312. /*
  313. Finds the block run that starts at file block number blockno
  314. in the file represented by the datastream data, if that
  315. blockno is in the double-indirect region of the datastream.
  316. sb: the superblock
  317. data: the datastream
  318. blockno: the blocknumber to find
  319. run: The found run is passed back through this pointer
  320. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  321. otherwise.
  322. Algorithm:
  323. The block runs in the double-indirect region are different.
  324. They are always allocated 4 fs blocks at a time, so each
  325. block run maps a constant amount of file data. This means
  326. that we can directly calculate how many block runs into the
  327. double-indirect region we need to go to get to the one that
  328. maps a particular filesystem block.
  329. We do this in two stages. First we calculate which of the
  330. inode addresses in the double-indirect block will point us
  331. to the indirect block that contains the mapping for the data,
  332. then we calculate which of the inode addresses in that
  333. indirect block maps the data block we are after.
  334. Oh, and once we've done that, we actually read in the blocks
  335. that contain the inode addresses we calculated above. Even
  336. though the double-indirect run may be several blocks long,
  337. we can calculate which of those blocks will contain the index
  338. we are after and only read that one. We then follow it to
  339. the indirect block and perform a similar process to find
  340. the actual block run that maps the data block we are interested
  341. in.
  342. Then we offset the run as in befs_find_brun_array() and we are
  343. done.
  344. 2001-11-15 Will Dyson
  345. */
  346. static int
  347. befs_find_brun_dblindirect(struct super_block *sb,
  348. befs_data_stream * data, befs_blocknr_t blockno,
  349. befs_block_run * run)
  350. {
  351. int dblindir_indx;
  352. int indir_indx;
  353. int offset;
  354. int dbl_which_block;
  355. int which_block;
  356. int dbl_block_indx;
  357. int block_indx;
  358. off_t dblindir_leftover;
  359. befs_blocknr_t blockno_at_run_start;
  360. struct buffer_head *dbl_indir_block;
  361. struct buffer_head *indir_block;
  362. befs_block_run indir_run;
  363. befs_inode_addr *iaddr_array = NULL;
  364. befs_sb_info *befs_sb = BEFS_SB(sb);
  365. befs_blocknr_t indir_start_blk =
  366. data->max_indirect_range >> befs_sb->block_shift;
  367. off_t dbl_indir_off = blockno - indir_start_blk;
  368. /* number of data blocks mapped by each of the iaddrs in
  369. * the indirect block pointed to by the double indirect block
  370. */
  371. size_t iblklen = BEFS_DBLINDIR_BRUN_LEN;
  372. /* number of data blocks mapped by each of the iaddrs in
  373. * the double indirect block
  374. */
  375. size_t diblklen = iblklen * befs_iaddrs_per_block(sb)
  376. * BEFS_DBLINDIR_BRUN_LEN;
  377. befs_debug(sb, "---> befs_find_brun_dblindirect() find %lu", blockno);
  378. /* First, discover which of the double_indir->indir blocks
  379. * contains pos. Then figure out how much of pos that
  380. * accounted for. Then discover which of the iaddrs in
  381. * the indirect block contains pos.
  382. */
  383. dblindir_indx = dbl_indir_off / diblklen;
  384. dblindir_leftover = dbl_indir_off % diblklen;
  385. indir_indx = dblindir_leftover / diblklen;
  386. /* Read double indirect block */
  387. dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb);
  388. if (dbl_which_block > data->double_indirect.len) {
  389. befs_error(sb, "The double-indirect index calculated by "
  390. "befs_read_brun_dblindirect(), %d, is outside the range "
  391. "of the double-indirect block", dblindir_indx);
  392. return BEFS_ERR;
  393. }
  394. dbl_indir_block =
  395. befs_bread(sb, iaddr2blockno(sb, &data->double_indirect) +
  396. dbl_which_block);
  397. if (dbl_indir_block == NULL) {
  398. befs_error(sb, "befs_read_brun_dblindirect() couldn't read the "
  399. "double-indirect block at blockno %lu",
  400. iaddr2blockno(sb,
  401. &data->double_indirect) +
  402. dbl_which_block);
  403. brelse(dbl_indir_block);
  404. return BEFS_ERR;
  405. }
  406. dbl_block_indx =
  407. dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb));
  408. iaddr_array = (befs_inode_addr *) dbl_indir_block->b_data;
  409. indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]);
  410. brelse(dbl_indir_block);
  411. iaddr_array = NULL;
  412. /* Read indirect block */
  413. which_block = indir_indx / befs_iaddrs_per_block(sb);
  414. if (which_block > indir_run.len) {
  415. befs_error(sb, "The indirect index calculated by "
  416. "befs_read_brun_dblindirect(), %d, is outside the range "
  417. "of the indirect block", indir_indx);
  418. return BEFS_ERR;
  419. }
  420. indir_block =
  421. befs_bread(sb, iaddr2blockno(sb, &indir_run) + which_block);
  422. if (indir_block == NULL) {
  423. befs_error(sb, "befs_read_brun_dblindirect() couldn't read the "
  424. "indirect block at blockno %lu",
  425. iaddr2blockno(sb, &indir_run) + which_block);
  426. brelse(indir_block);
  427. return BEFS_ERR;
  428. }
  429. block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb));
  430. iaddr_array = (befs_inode_addr *) indir_block->b_data;
  431. *run = fsrun_to_cpu(sb, iaddr_array[block_indx]);
  432. brelse(indir_block);
  433. iaddr_array = NULL;
  434. blockno_at_run_start = indir_start_blk;
  435. blockno_at_run_start += diblklen * dblindir_indx;
  436. blockno_at_run_start += iblklen * indir_indx;
  437. offset = blockno - blockno_at_run_start;
  438. run->start += offset;
  439. run->len -= offset;
  440. befs_debug(sb, "Found file block %lu in double_indirect[%d][%d],"
  441. " double_indirect_leftover = %lu",
  442. blockno, dblindir_indx, indir_indx, dblindir_leftover);
  443. return BEFS_OK;
  444. }