datastream.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. const befs_inode_addr BAD_IADDR = { 0, 0, 0 };
  20. static int befs_find_brun_direct(struct super_block *sb,
  21. befs_data_stream * data,
  22. befs_blocknr_t blockno, befs_block_run * run);
  23. static int befs_find_brun_indirect(struct super_block *sb,
  24. befs_data_stream * data,
  25. befs_blocknr_t blockno,
  26. befs_block_run * run);
  27. static int befs_find_brun_dblindirect(struct super_block *sb,
  28. befs_data_stream * data,
  29. befs_blocknr_t blockno,
  30. befs_block_run * run);
  31. /**
  32. * befs_read_datastream - get buffer_head containing data, starting from pos.
  33. * @sb: Filesystem superblock
  34. * @ds: datastrem to find data with
  35. * @pos: start of data
  36. * @off: offset of data in buffer_head->b_data
  37. *
  38. * Returns pointer to buffer_head containing data starting with offset @off,
  39. * if you don't need to know offset just set @off = NULL.
  40. */
  41. struct buffer_head *
  42. befs_read_datastream(struct super_block *sb, befs_data_stream * ds,
  43. befs_off_t pos, uint * off)
  44. {
  45. struct buffer_head *bh = NULL;
  46. befs_block_run run;
  47. befs_blocknr_t block; /* block coresponding to pos */
  48. befs_debug(sb, "---> befs_read_datastream() %Lu", pos);
  49. block = pos >> BEFS_SB(sb)->block_shift;
  50. if (off)
  51. *off = pos - (block << BEFS_SB(sb)->block_shift);
  52. if (befs_fblock2brun(sb, ds, block, &run) != BEFS_OK) {
  53. befs_error(sb, "BeFS: Error finding disk addr of block %lu",
  54. block);
  55. befs_debug(sb, "<--- befs_read_datastream() ERROR");
  56. return NULL;
  57. }
  58. bh = befs_bread_iaddr(sb, run);
  59. if (!bh) {
  60. befs_error(sb, "BeFS: Error reading block %lu from datastream",
  61. block);
  62. return NULL;
  63. }
  64. befs_debug(sb, "<--- befs_read_datastream() read data, starting at %Lu",
  65. pos);
  66. return bh;
  67. }
  68. /*
  69. * Takes a file position and gives back a brun who's starting block
  70. * is block number fblock of the file.
  71. *
  72. * Returns BEFS_OK or BEFS_ERR.
  73. *
  74. * Calls specialized functions for each of the three possible
  75. * datastream regions.
  76. *
  77. * 2001-11-15 Will Dyson
  78. */
  79. int
  80. befs_fblock2brun(struct super_block *sb, befs_data_stream * data,
  81. befs_blocknr_t fblock, befs_block_run * run)
  82. {
  83. int err;
  84. befs_off_t pos = fblock << BEFS_SB(sb)->block_shift;
  85. if (pos < data->max_direct_range) {
  86. err = befs_find_brun_direct(sb, data, fblock, run);
  87. } else if (pos < data->max_indirect_range) {
  88. err = befs_find_brun_indirect(sb, data, fblock, run);
  89. } else if (pos < data->max_double_indirect_range) {
  90. err = befs_find_brun_dblindirect(sb, data, fblock, run);
  91. } else {
  92. befs_error(sb,
  93. "befs_fblock2brun() was asked to find block %lu, "
  94. "which is not mapped by the datastream\n", fblock);
  95. err = BEFS_ERR;
  96. }
  97. return err;
  98. }
  99. /**
  100. * befs_read_lsmylink - read long symlink from datastream.
  101. * @sb: Filesystem superblock
  102. * @ds: Datastrem to read from
  103. * @buf: Buffer in which to place long symlink data
  104. * @len: Length of the long symlink in bytes
  105. *
  106. * Returns the number of bytes read
  107. */
  108. size_t
  109. befs_read_lsymlink(struct super_block * sb, befs_data_stream * ds, void *buff,
  110. befs_off_t len)
  111. {
  112. befs_off_t bytes_read = 0; /* bytes readed */
  113. u16 plen;
  114. struct buffer_head *bh = NULL;
  115. befs_debug(sb, "---> befs_read_lsymlink() length: %Lu", len);
  116. while (bytes_read < len) {
  117. bh = befs_read_datastream(sb, ds, bytes_read, NULL);
  118. if (!bh) {
  119. befs_error(sb, "BeFS: Error reading datastream block "
  120. "starting from %Lu", bytes_read);
  121. befs_debug(sb, "<--- befs_read_lsymlink() ERROR");
  122. return bytes_read;
  123. }
  124. plen = ((bytes_read + BEFS_SB(sb)->block_size) < len) ?
  125. BEFS_SB(sb)->block_size : len - bytes_read;
  126. memcpy(buff + bytes_read, bh->b_data, plen);
  127. brelse(bh);
  128. bytes_read += plen;
  129. }
  130. befs_debug(sb, "<--- befs_read_lsymlink() read %u bytes", 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. befs_sb_info *befs_sb = BEFS_SB(sb);
  150. befs_debug(sb, "---> befs_count_blocks()");
  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, "<--- befs_count_blocks() %u blocks", 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 lenght 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, "---> befs_find_brun_direct(), find %lu", blockno);
  218. if (blockno > max_block) {
  219. befs_error(sb, "befs_find_brun_direct() passed block outside of"
  220. "direct region");
  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, "---> befs_find_brun_direct(), "
  231. "found %lu at direct[%d]", blockno, i);
  232. return BEFS_OK;
  233. }
  234. }
  235. befs_debug(sb, "---> befs_find_brun_direct() ERROR");
  236. return BEFS_ERR;
  237. }
  238. /*
  239. Finds the block run that starts at file block number blockno
  240. in the file represented by the datastream data, if that
  241. blockno is in the indirect region of the datastream.
  242. sb: the superblock
  243. data: the datastream
  244. blockno: the blocknumber to find
  245. run: The found run is passed back through this pointer
  246. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  247. otherwise.
  248. Algorithm:
  249. For each block in the indirect run of the datastream, read
  250. it in and search through it for search_blk.
  251. XXX:
  252. Really should check to make sure blockno is inside indirect
  253. region.
  254. 2001-11-15 Will Dyson
  255. */
  256. static int
  257. befs_find_brun_indirect(struct super_block *sb,
  258. befs_data_stream * data, befs_blocknr_t blockno,
  259. befs_block_run * run)
  260. {
  261. int i, j;
  262. befs_blocknr_t sum = 0;
  263. befs_blocknr_t indir_start_blk;
  264. befs_blocknr_t search_blk;
  265. struct buffer_head *indirblock;
  266. befs_disk_block_run *array;
  267. befs_block_run indirect = data->indirect;
  268. befs_blocknr_t indirblockno = iaddr2blockno(sb, &indirect);
  269. int arraylen = befs_iaddrs_per_block(sb);
  270. befs_debug(sb, "---> befs_find_brun_indirect(), find %lu", blockno);
  271. indir_start_blk = data->max_direct_range >> BEFS_SB(sb)->block_shift;
  272. search_blk = blockno - indir_start_blk;
  273. /* Examine blocks of the indirect run one at a time */
  274. for (i = 0; i < indirect.len; i++) {
  275. indirblock = befs_bread(sb, indirblockno + i);
  276. if (indirblock == NULL) {
  277. befs_debug(sb,
  278. "---> befs_find_brun_indirect() failed to "
  279. "read disk block %lu from the indirect brun",
  280. 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. "<--- befs_find_brun_indirect() found "
  297. "file block %lu at indirect[%d]",
  298. blockno, j + (i * arraylen));
  299. return BEFS_OK;
  300. }
  301. sum += len;
  302. }
  303. brelse(indirblock);
  304. }
  305. /* Only fallthrough is an error */
  306. befs_error(sb, "BeFS: befs_find_brun_indirect() failed to find "
  307. "file block %lu", blockno);
  308. befs_debug(sb, "<--- befs_find_brun_indirect() ERROR");
  309. return BEFS_ERR;
  310. }
  311. /*
  312. Finds the block run that starts at file block number blockno
  313. in the file represented by the datastream data, if that
  314. blockno is in the double-indirect region of the datastream.
  315. sb: the superblock
  316. data: the datastream
  317. blockno: the blocknumber to find
  318. run: The found run is passed back through this pointer
  319. Return value is BEFS_OK if the blockrun is found, BEFS_ERR
  320. otherwise.
  321. Algorithm:
  322. The block runs in the double-indirect region are different.
  323. They are always allocated 4 fs blocks at a time, so each
  324. block run maps a constant amount of file data. This means
  325. that we can directly calculate how many block runs into the
  326. double-indirect region we need to go to get to the one that
  327. maps a particular filesystem block.
  328. We do this in two stages. First we calculate which of the
  329. inode addresses in the double-indirect block will point us
  330. to the indirect block that contains the mapping for the data,
  331. then we calculate which of the inode addresses in that
  332. indirect block maps the data block we are after.
  333. Oh, and once we've done that, we actually read in the blocks
  334. that contain the inode addresses we calculated above. Even
  335. though the double-indirect run may be several blocks long,
  336. we can calculate which of those blocks will contain the index
  337. we are after and only read that one. We then follow it to
  338. the indirect block and perform a similar process to find
  339. the actual block run that maps the data block we are interested
  340. in.
  341. Then we offset the run as in befs_find_brun_array() and we are
  342. done.
  343. 2001-11-15 Will Dyson
  344. */
  345. static int
  346. befs_find_brun_dblindirect(struct super_block *sb,
  347. befs_data_stream * data, befs_blocknr_t blockno,
  348. befs_block_run * run)
  349. {
  350. int dblindir_indx;
  351. int indir_indx;
  352. int offset;
  353. int dbl_which_block;
  354. int which_block;
  355. int dbl_block_indx;
  356. int block_indx;
  357. off_t dblindir_leftover;
  358. befs_blocknr_t blockno_at_run_start;
  359. struct buffer_head *dbl_indir_block;
  360. struct buffer_head *indir_block;
  361. befs_block_run indir_run;
  362. befs_disk_inode_addr *iaddr_array = NULL;
  363. befs_sb_info *befs_sb = BEFS_SB(sb);
  364. befs_blocknr_t indir_start_blk =
  365. data->max_indirect_range >> befs_sb->block_shift;
  366. off_t dbl_indir_off = blockno - indir_start_blk;
  367. /* number of data blocks mapped by each of the iaddrs in
  368. * the indirect block pointed to by the double indirect block
  369. */
  370. size_t iblklen = BEFS_DBLINDIR_BRUN_LEN;
  371. /* number of data blocks mapped by each of the iaddrs in
  372. * the double indirect block
  373. */
  374. size_t diblklen = iblklen * befs_iaddrs_per_block(sb)
  375. * BEFS_DBLINDIR_BRUN_LEN;
  376. befs_debug(sb, "---> befs_find_brun_dblindirect() find %lu", blockno);
  377. /* First, discover which of the double_indir->indir blocks
  378. * contains pos. Then figure out how much of pos that
  379. * accounted for. Then discover which of the iaddrs in
  380. * the indirect block contains pos.
  381. */
  382. dblindir_indx = dbl_indir_off / diblklen;
  383. dblindir_leftover = dbl_indir_off % diblklen;
  384. indir_indx = dblindir_leftover / diblklen;
  385. /* Read double indirect block */
  386. dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb);
  387. if (dbl_which_block > data->double_indirect.len) {
  388. befs_error(sb, "The double-indirect index calculated by "
  389. "befs_read_brun_dblindirect(), %d, is outside the range "
  390. "of the double-indirect block", dblindir_indx);
  391. return BEFS_ERR;
  392. }
  393. dbl_indir_block =
  394. befs_bread(sb, iaddr2blockno(sb, &data->double_indirect) +
  395. dbl_which_block);
  396. if (dbl_indir_block == NULL) {
  397. befs_error(sb, "befs_read_brun_dblindirect() couldn't read the "
  398. "double-indirect block at blockno %lu",
  399. iaddr2blockno(sb,
  400. &data->double_indirect) +
  401. dbl_which_block);
  402. brelse(dbl_indir_block);
  403. return BEFS_ERR;
  404. }
  405. dbl_block_indx =
  406. dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb));
  407. iaddr_array = (befs_disk_inode_addr *) dbl_indir_block->b_data;
  408. indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]);
  409. brelse(dbl_indir_block);
  410. iaddr_array = NULL;
  411. /* Read indirect block */
  412. which_block = indir_indx / befs_iaddrs_per_block(sb);
  413. if (which_block > indir_run.len) {
  414. befs_error(sb, "The indirect index calculated by "
  415. "befs_read_brun_dblindirect(), %d, is outside the range "
  416. "of the indirect block", indir_indx);
  417. return BEFS_ERR;
  418. }
  419. indir_block =
  420. befs_bread(sb, iaddr2blockno(sb, &indir_run) + which_block);
  421. if (indir_block == NULL) {
  422. befs_error(sb, "befs_read_brun_dblindirect() couldn't read the "
  423. "indirect block at blockno %lu",
  424. iaddr2blockno(sb, &indir_run) + which_block);
  425. brelse(indir_block);
  426. return BEFS_ERR;
  427. }
  428. block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb));
  429. iaddr_array = (befs_disk_inode_addr *) indir_block->b_data;
  430. *run = fsrun_to_cpu(sb, iaddr_array[block_indx]);
  431. brelse(indir_block);
  432. iaddr_array = NULL;
  433. blockno_at_run_start = indir_start_blk;
  434. blockno_at_run_start += diblklen * dblindir_indx;
  435. blockno_at_run_start += iblklen * indir_indx;
  436. offset = blockno - blockno_at_run_start;
  437. run->start += offset;
  438. run->len -= offset;
  439. befs_debug(sb, "Found file block %lu in double_indirect[%d][%d],"
  440. " double_indirect_leftover = %lu",
  441. blockno, dblindir_indx, indir_indx, dblindir_leftover);
  442. return BEFS_OK;
  443. }