datastream.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/buffer_head.h>
  14. #include <linux/string.h>
  15. #include "befs.h"
  16. #include "datastream.h"
  17. #include "io.h"
  18. const befs_inode_addr BAD_IADDR = { 0, 0, 0 };
  19. static int befs_find_brun_direct(struct super_block *sb,
  20. befs_data_stream * data,
  21. befs_blocknr_t blockno, befs_block_run * run);
  22. static int befs_find_brun_indirect(struct super_block *sb,
  23. befs_data_stream * data,
  24. befs_blocknr_t blockno,
  25. befs_block_run * run);
  26. static int befs_find_brun_dblindirect(struct super_block *sb,
  27. befs_data_stream * data,
  28. befs_blocknr_t blockno,
  29. befs_block_run * run);
  30. /**
  31. * befs_read_datastream - get buffer_head containing data, starting from pos.
  32. * @sb: Filesystem superblock
  33. * @ds: datastrem to find data with
  34. * @pos: start of data
  35. * @off: offset of data in buffer_head->b_data
  36. *
  37. * Returns pointer to buffer_head containing data starting with offset @off,
  38. * if you don't need to know offset just set @off = NULL.
  39. */
  40. struct buffer_head *
  41. befs_read_datastream(struct super_block *sb, befs_data_stream * ds,
  42. befs_off_t pos, uint * off)
  43. {
  44. struct buffer_head *bh = NULL;
  45. befs_block_run run;
  46. befs_blocknr_t block; /* block coresponding to pos */
  47. befs_debug(sb, "---> %s %llu", __func__, pos);
  48. block = pos >> BEFS_SB(sb)->block_shift;
  49. if (off)
  50. *off = pos - (block << BEFS_SB(sb)->block_shift);
  51. if (befs_fblock2brun(sb, ds, block, &run) != BEFS_OK) {
  52. befs_error(sb, "BeFS: Error finding disk addr of block %lu",
  53. (unsigned long)block);
  54. befs_debug(sb, "<--- %s ERROR", __func__);
  55. return NULL;
  56. }
  57. bh = befs_bread_iaddr(sb, run);
  58. if (!bh) {
  59. befs_error(sb, "BeFS: Error reading block %lu from datastream",
  60. (unsigned long)block);
  61. return NULL;
  62. }
  63. befs_debug(sb, "<--- %s read data, starting at %llu", __func__, pos);
  64. return bh;
  65. }
  66. /*
  67. * Takes a file position and gives back a brun who's starting block
  68. * is block number fblock of the file.
  69. *
  70. * Returns BEFS_OK or BEFS_ERR.
  71. *
  72. * Calls specialized functions for each of the three possible
  73. * datastream regions.
  74. *
  75. * 2001-11-15 Will Dyson
  76. */
  77. int
  78. befs_fblock2brun(struct super_block *sb, befs_data_stream * data,
  79. befs_blocknr_t fblock, befs_block_run * run)
  80. {
  81. int err;
  82. befs_off_t pos = fblock << BEFS_SB(sb)->block_shift;
  83. if (pos < data->max_direct_range) {
  84. err = befs_find_brun_direct(sb, data, fblock, run);
  85. } else if (pos < data->max_indirect_range) {
  86. err = befs_find_brun_indirect(sb, data, fblock, run);
  87. } else if (pos < data->max_double_indirect_range) {
  88. err = befs_find_brun_dblindirect(sb, data, fblock, run);
  89. } else {
  90. befs_error(sb,
  91. "befs_fblock2brun() was asked to find block %lu, "
  92. "which is not mapped by the datastream\n",
  93. (unsigned long)fblock);
  94. err = BEFS_ERR;
  95. }
  96. return err;
  97. }
  98. /**
  99. * befs_read_lsmylink - read long symlink from datastream.
  100. * @sb: Filesystem superblock
  101. * @ds: Datastrem to read from
  102. * @buff: Buffer in which to place long symlink data
  103. * @len: Length of the long symlink in bytes
  104. *
  105. * Returns the number of bytes read
  106. */
  107. size_t
  108. befs_read_lsymlink(struct super_block * sb, befs_data_stream * ds, void *buff,
  109. befs_off_t len)
  110. {
  111. befs_off_t bytes_read = 0; /* bytes readed */
  112. u16 plen;
  113. struct buffer_head *bh = NULL;
  114. befs_debug(sb, "---> %s length: %llu", __func__, len);
  115. while (bytes_read < len) {
  116. bh = befs_read_datastream(sb, ds, bytes_read, NULL);
  117. if (!bh) {
  118. befs_error(sb, "BeFS: Error reading datastream block "
  119. "starting from %llu", bytes_read);
  120. befs_debug(sb, "<--- %s ERROR", __func__);
  121. return bytes_read;
  122. }
  123. plen = ((bytes_read + BEFS_SB(sb)->block_size) < len) ?
  124. BEFS_SB(sb)->block_size : len - bytes_read;
  125. memcpy(buff + bytes_read, bh->b_data, plen);
  126. brelse(bh);
  127. bytes_read += plen;
  128. }
  129. befs_debug(sb, "<--- %s read %u bytes", __func__, (unsigned int)
  130. bytes_read);
  131. return bytes_read;
  132. }
  133. /**
  134. * befs_count_blocks - blocks used by a file
  135. * @sb: Filesystem superblock
  136. * @ds: Datastream of the file
  137. *
  138. * Counts the number of fs blocks that the file represented by
  139. * inode occupies on the filesystem, counting both regular file
  140. * data and filesystem metadata (and eventually attribute data
  141. * when we support attributes)
  142. */
  143. befs_blocknr_t
  144. befs_count_blocks(struct super_block * sb, befs_data_stream * ds)
  145. {
  146. befs_blocknr_t blocks;
  147. befs_blocknr_t datablocks; /* File data blocks */
  148. befs_blocknr_t metablocks; /* FS metadata blocks */
  149. struct befs_sb_info *befs_sb = BEFS_SB(sb);
  150. befs_debug(sb, "---> %s", __func__);
  151. datablocks = ds->size >> befs_sb->block_shift;
  152. if (ds->size & (befs_sb->block_size - 1))
  153. datablocks += 1;
  154. metablocks = 1; /* Start with 1 block for inode */
  155. /* Size of indirect block */
  156. if (ds->size > ds->max_direct_range)
  157. metablocks += ds->indirect.len;
  158. /*
  159. Double indir block, plus all the indirect blocks it mapps
  160. In the double-indirect range, all block runs of data are
  161. BEFS_DBLINDIR_BRUN_LEN blocks long. Therefore, we know
  162. how many data block runs are in the double-indirect region,
  163. and from that we know how many indirect blocks it takes to
  164. map them. We assume that the indirect blocks are also
  165. BEFS_DBLINDIR_BRUN_LEN blocks long.
  166. */
  167. if (ds->size > ds->max_indirect_range && ds->max_indirect_range != 0) {
  168. uint dbl_bytes;
  169. uint dbl_bruns;
  170. uint indirblocks;
  171. dbl_bytes =
  172. ds->max_double_indirect_range - ds->max_indirect_range;
  173. dbl_bruns =
  174. dbl_bytes / (befs_sb->block_size * BEFS_DBLINDIR_BRUN_LEN);
  175. indirblocks = dbl_bruns / befs_iaddrs_per_block(sb);
  176. metablocks += ds->double_indirect.len;
  177. metablocks += indirblocks;
  178. }
  179. blocks = datablocks + metablocks;
  180. befs_debug(sb, "<--- %s %u blocks", __func__, (unsigned int)blocks);
  181. return blocks;
  182. }
  183. /*
  184. Finds the block run that starts at file block number blockno
  185. in the file represented by the datastream data, if that
  186. blockno is in the direct region of the datastream.
  187. sb: the superblock
  188. data: the datastream
  189. blockno: the blocknumber to find
  190. run: The found run is passed back through this pointer
  191. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  192. otherwise.
  193. Algorithm:
  194. Linear search. Checks each element of array[] to see if it
  195. contains the blockno-th filesystem block. This is necessary
  196. because the block runs map variable amounts of data. Simply
  197. keeps a count of the number of blocks searched so far (sum),
  198. incrementing this by the length of each block run as we come
  199. across it. Adds sum to *count before returning (this is so
  200. you can search multiple arrays that are logicaly one array,
  201. as in the indirect region code).
  202. When/if blockno is found, if blockno is inside of a block
  203. run as stored on disk, we offset the start and length members
  204. of the block run, so that blockno is the start and len is
  205. still valid (the run ends in the same place).
  206. 2001-11-15 Will Dyson
  207. */
  208. static int
  209. befs_find_brun_direct(struct super_block *sb, befs_data_stream * data,
  210. befs_blocknr_t blockno, befs_block_run * run)
  211. {
  212. int i;
  213. befs_block_run *array = data->direct;
  214. befs_blocknr_t sum;
  215. befs_blocknr_t max_block =
  216. data->max_direct_range >> BEFS_SB(sb)->block_shift;
  217. befs_debug(sb, "---> %s, find %lu", __func__, (unsigned long)blockno);
  218. if (blockno > max_block) {
  219. befs_error(sb, "%s passed block outside of direct region",
  220. __func__);
  221. return BEFS_ERR;
  222. }
  223. for (i = 0, sum = 0; i < BEFS_NUM_DIRECT_BLOCKS;
  224. sum += array[i].len, i++) {
  225. if (blockno >= sum && blockno < sum + (array[i].len)) {
  226. int offset = blockno - sum;
  227. run->allocation_group = array[i].allocation_group;
  228. run->start = array[i].start + offset;
  229. run->len = array[i].len - offset;
  230. befs_debug(sb, "---> %s, "
  231. "found %lu at direct[%d]", __func__,
  232. (unsigned long)blockno, i);
  233. return BEFS_OK;
  234. }
  235. }
  236. befs_debug(sb, "---> %s ERROR", __func__);
  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_disk_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, "---> %s, find %lu", __func__, (unsigned long)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, "---> %s failed to read "
  279. "disk block %lu from the indirect brun",
  280. __func__, (unsigned long)indirblockno + i);
  281. return BEFS_ERR;
  282. }
  283. array = (befs_disk_block_run *) indirblock->b_data;
  284. for (j = 0; j < arraylen; ++j) {
  285. int len = fs16_to_cpu(sb, array[j].len);
  286. if (search_blk >= sum && search_blk < sum + len) {
  287. int offset = search_blk - sum;
  288. run->allocation_group =
  289. fs32_to_cpu(sb, array[j].allocation_group);
  290. run->start =
  291. fs16_to_cpu(sb, array[j].start) + offset;
  292. run->len =
  293. fs16_to_cpu(sb, array[j].len) - offset;
  294. brelse(indirblock);
  295. befs_debug(sb,
  296. "<--- %s found file block "
  297. "%lu at indirect[%d]", __func__,
  298. (unsigned long)blockno,
  299. 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: %s failed to find "
  308. "file block %lu", __func__, (unsigned long)blockno);
  309. befs_debug(sb, "<--- %s ERROR", __func__);
  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_disk_inode_addr *iaddr_array = NULL;
  364. struct 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, "---> %s find %lu", __func__, (unsigned long)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. "%s, %d, is outside the range "
  391. "of the double-indirect block", __func__,
  392. dblindir_indx);
  393. return BEFS_ERR;
  394. }
  395. dbl_indir_block =
  396. befs_bread(sb, iaddr2blockno(sb, &data->double_indirect) +
  397. dbl_which_block);
  398. if (dbl_indir_block == NULL) {
  399. befs_error(sb, "%s couldn't read the "
  400. "double-indirect block at blockno %lu", __func__,
  401. (unsigned long)
  402. iaddr2blockno(sb, &data->double_indirect) +
  403. dbl_which_block);
  404. brelse(dbl_indir_block);
  405. return BEFS_ERR;
  406. }
  407. dbl_block_indx =
  408. dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb));
  409. iaddr_array = (befs_disk_inode_addr *) dbl_indir_block->b_data;
  410. indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]);
  411. brelse(dbl_indir_block);
  412. iaddr_array = NULL;
  413. /* Read indirect block */
  414. which_block = indir_indx / befs_iaddrs_per_block(sb);
  415. if (which_block > indir_run.len) {
  416. befs_error(sb, "The indirect index calculated by "
  417. "%s, %d, is outside the range "
  418. "of the indirect block", __func__, indir_indx);
  419. return BEFS_ERR;
  420. }
  421. indir_block =
  422. befs_bread(sb, iaddr2blockno(sb, &indir_run) + which_block);
  423. if (indir_block == NULL) {
  424. befs_error(sb, "%s couldn't read the indirect block "
  425. "at blockno %lu", __func__, (unsigned long)
  426. iaddr2blockno(sb, &indir_run) + which_block);
  427. brelse(indir_block);
  428. return BEFS_ERR;
  429. }
  430. block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb));
  431. iaddr_array = (befs_disk_inode_addr *) indir_block->b_data;
  432. *run = fsrun_to_cpu(sb, iaddr_array[block_indx]);
  433. brelse(indir_block);
  434. iaddr_array = NULL;
  435. blockno_at_run_start = indir_start_blk;
  436. blockno_at_run_start += diblklen * dblindir_indx;
  437. blockno_at_run_start += iblklen * indir_indx;
  438. offset = blockno - blockno_at_run_start;
  439. run->start += offset;
  440. run->len -= offset;
  441. befs_debug(sb, "Found file block %lu in double_indirect[%d][%d],"
  442. " double_indirect_leftover = %lu", (unsigned long)
  443. blockno, dblindir_indx, indir_indx, dblindir_leftover);
  444. return BEFS_OK;
  445. }