block.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. * Phillip Lougher <phillip@squashfs.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * block.c
  22. */
  23. /*
  24. * This file implements the low-level routines to read and decompress
  25. * datablocks and metadata blocks.
  26. */
  27. #include <linux/fs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/bio.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/workqueue.h>
  35. #include "squashfs_fs.h"
  36. #include "squashfs_fs_sb.h"
  37. #include "squashfs.h"
  38. #include "decompressor.h"
  39. #include "page_actor.h"
  40. static struct workqueue_struct *squashfs_read_wq;
  41. struct squashfs_read_request {
  42. struct super_block *sb;
  43. u64 index;
  44. int length;
  45. int compressed;
  46. int offset;
  47. u64 read_end;
  48. struct squashfs_page_actor *output;
  49. enum {
  50. SQUASHFS_COPY,
  51. SQUASHFS_DECOMPRESS,
  52. SQUASHFS_METADATA,
  53. } data_processing;
  54. bool synchronous;
  55. /*
  56. * If the read is synchronous, it is possible to retrieve information
  57. * about the request by setting these pointers.
  58. */
  59. int *res;
  60. int *bytes_read;
  61. int *bytes_uncompressed;
  62. int nr_buffers;
  63. struct buffer_head **bh;
  64. struct work_struct offload;
  65. };
  66. struct squashfs_bio_request {
  67. struct buffer_head **bh;
  68. int nr_buffers;
  69. };
  70. static int squashfs_bio_submit(struct squashfs_read_request *req);
  71. int squashfs_init_read_wq(void)
  72. {
  73. squashfs_read_wq = create_workqueue("SquashFS read wq");
  74. return !!squashfs_read_wq;
  75. }
  76. void squashfs_destroy_read_wq(void)
  77. {
  78. flush_workqueue(squashfs_read_wq);
  79. destroy_workqueue(squashfs_read_wq);
  80. }
  81. static void free_read_request(struct squashfs_read_request *req, int error)
  82. {
  83. if (!req->synchronous)
  84. squashfs_page_actor_free(req->output, error);
  85. if (req->res)
  86. *(req->res) = error;
  87. kfree(req->bh);
  88. kfree(req);
  89. }
  90. static void squashfs_process_blocks(struct squashfs_read_request *req)
  91. {
  92. int error = 0;
  93. int bytes, i, length;
  94. struct squashfs_sb_info *msblk = req->sb->s_fs_info;
  95. struct squashfs_page_actor *actor = req->output;
  96. struct buffer_head **bh = req->bh;
  97. int nr_buffers = req->nr_buffers;
  98. for (i = 0; i < nr_buffers; ++i) {
  99. if (!bh[i])
  100. continue;
  101. wait_on_buffer(bh[i]);
  102. if (!buffer_uptodate(bh[i]))
  103. error = -EIO;
  104. }
  105. if (error)
  106. goto cleanup;
  107. if (req->data_processing == SQUASHFS_METADATA) {
  108. /* Extract the length of the metadata block */
  109. if (req->offset != msblk->devblksize - 1) {
  110. length = le16_to_cpup((__le16 *)
  111. (bh[0]->b_data + req->offset));
  112. } else {
  113. length = (unsigned char)bh[0]->b_data[req->offset];
  114. length |= (unsigned char)bh[1]->b_data[0] << 8;
  115. }
  116. req->compressed = SQUASHFS_COMPRESSED(length);
  117. req->data_processing = req->compressed ? SQUASHFS_DECOMPRESS
  118. : SQUASHFS_COPY;
  119. length = SQUASHFS_COMPRESSED_SIZE(length);
  120. if (req->index + length + 2 > req->read_end) {
  121. for (i = 0; i < nr_buffers; ++i)
  122. put_bh(bh[i]);
  123. kfree(bh);
  124. req->length = length;
  125. req->index += 2;
  126. squashfs_bio_submit(req);
  127. return;
  128. }
  129. req->length = length;
  130. req->offset = (req->offset + 2) % PAGE_SIZE;
  131. if (req->offset < 2) {
  132. put_bh(bh[0]);
  133. ++bh;
  134. --nr_buffers;
  135. }
  136. }
  137. if (req->bytes_read)
  138. *(req->bytes_read) = req->length;
  139. if (req->data_processing == SQUASHFS_COPY) {
  140. squashfs_bh_to_actor(bh, nr_buffers, req->output, req->offset,
  141. req->length, msblk->devblksize);
  142. } else if (req->data_processing == SQUASHFS_DECOMPRESS) {
  143. req->length = squashfs_decompress(msblk, bh, nr_buffers,
  144. req->offset, req->length, actor);
  145. if (req->length < 0) {
  146. error = -EIO;
  147. goto cleanup;
  148. }
  149. }
  150. /* Last page may have trailing bytes not filled */
  151. bytes = req->length % PAGE_SIZE;
  152. if (bytes && actor->page[actor->pages - 1])
  153. zero_user_segment(actor->page[actor->pages - 1], bytes,
  154. PAGE_SIZE);
  155. cleanup:
  156. if (req->bytes_uncompressed)
  157. *(req->bytes_uncompressed) = req->length;
  158. if (error) {
  159. for (i = 0; i < nr_buffers; ++i)
  160. if (bh[i])
  161. put_bh(bh[i]);
  162. }
  163. free_read_request(req, error);
  164. }
  165. static void read_wq_handler(struct work_struct *work)
  166. {
  167. squashfs_process_blocks(container_of(work,
  168. struct squashfs_read_request, offload));
  169. }
  170. static void squashfs_bio_end_io(struct bio *bio, int error)
  171. {
  172. int i;
  173. struct squashfs_bio_request *bio_req = bio->bi_private;
  174. bio_put(bio);
  175. for (i = 0; i < bio_req->nr_buffers; ++i) {
  176. if (!bio_req->bh[i])
  177. continue;
  178. if (!error)
  179. set_buffer_uptodate(bio_req->bh[i]);
  180. else
  181. clear_buffer_uptodate(bio_req->bh[i]);
  182. unlock_buffer(bio_req->bh[i]);
  183. }
  184. kfree(bio_req);
  185. }
  186. static int bh_is_optional(struct squashfs_read_request *req, int idx)
  187. {
  188. int start_idx, end_idx;
  189. struct squashfs_sb_info *msblk = req->sb->s_fs_info;
  190. start_idx = (idx * msblk->devblksize - req->offset) >> PAGE_SHIFT;
  191. end_idx = ((idx + 1) * msblk->devblksize - req->offset + 1) >> PAGE_SHIFT;
  192. if (start_idx >= req->output->pages)
  193. return 1;
  194. if (start_idx < 0)
  195. start_idx = end_idx;
  196. if (end_idx >= req->output->pages)
  197. end_idx = start_idx;
  198. return !req->output->page[start_idx] && !req->output->page[end_idx];
  199. }
  200. static int actor_getblks(struct squashfs_read_request *req, u64 block)
  201. {
  202. int i;
  203. req->bh = kmalloc_array(req->nr_buffers, sizeof(*(req->bh)), GFP_NOIO);
  204. if (!req->bh)
  205. return -ENOMEM;
  206. for (i = 0; i < req->nr_buffers; ++i) {
  207. /*
  208. * When dealing with an uncompressed block, the actor may
  209. * contains NULL pages. There's no need to read the buffers
  210. * associated with these pages.
  211. */
  212. if (!req->compressed && bh_is_optional(req, i)) {
  213. req->bh[i] = NULL;
  214. continue;
  215. }
  216. req->bh[i] = sb_getblk(req->sb, block + i);
  217. if (!req->bh[i]) {
  218. while (--i) {
  219. if (req->bh[i])
  220. put_bh(req->bh[i]);
  221. }
  222. return -1;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int squashfs_bio_submit(struct squashfs_read_request *req)
  228. {
  229. struct bio *bio = NULL;
  230. struct buffer_head *bh;
  231. struct squashfs_bio_request *bio_req = NULL;
  232. int b = 0, prev_block = 0;
  233. struct squashfs_sb_info *msblk = req->sb->s_fs_info;
  234. u64 read_start = round_down(req->index, msblk->devblksize);
  235. u64 read_end = round_up(req->index + req->length, msblk->devblksize);
  236. sector_t block = read_start >> msblk->devblksize_log2;
  237. sector_t block_end = read_end >> msblk->devblksize_log2;
  238. int offset = read_start - round_down(req->index, PAGE_SIZE);
  239. int nr_buffers = block_end - block;
  240. int blksz = msblk->devblksize;
  241. int bio_max_pages = nr_buffers > BIO_MAX_PAGES ? BIO_MAX_PAGES
  242. : nr_buffers;
  243. /* Setup the request */
  244. req->read_end = read_end;
  245. req->offset = req->index - read_start;
  246. req->nr_buffers = nr_buffers;
  247. if (actor_getblks(req, block) < 0)
  248. goto getblk_failed;
  249. /* Create and submit the BIOs */
  250. for (b = 0; b < nr_buffers; ++b, offset += blksz) {
  251. bh = req->bh[b];
  252. if (!bh || !trylock_buffer(bh))
  253. continue;
  254. if (buffer_uptodate(bh)) {
  255. unlock_buffer(bh);
  256. continue;
  257. }
  258. offset %= PAGE_SIZE;
  259. /* Append the buffer to the current BIO if it is contiguous */
  260. if (bio && bio_req && prev_block + 1 == b) {
  261. if (bio_add_page(bio, bh->b_page, blksz, offset)) {
  262. bio_req->nr_buffers += 1;
  263. prev_block = b;
  264. continue;
  265. }
  266. }
  267. /* Otherwise, submit the current BIO and create a new one */
  268. if (bio)
  269. submit_bio(READ, bio);
  270. bio_req = kcalloc(1, sizeof(struct squashfs_bio_request),
  271. GFP_NOIO);
  272. if (!bio_req)
  273. goto req_alloc_failed;
  274. bio_req->bh = &req->bh[b];
  275. bio = bio_alloc(GFP_NOIO, bio_max_pages);
  276. if (!bio)
  277. goto bio_alloc_failed;
  278. bio->bi_bdev = req->sb->s_bdev;
  279. bio->bi_sector = (block + b)
  280. << (msblk->devblksize_log2 - 9);
  281. bio->bi_private = bio_req;
  282. bio->bi_end_io = squashfs_bio_end_io;
  283. bio_add_page(bio, bh->b_page, blksz, offset);
  284. bio_req->nr_buffers += 1;
  285. prev_block = b;
  286. }
  287. if (bio)
  288. submit_bio(READ, bio);
  289. if (req->synchronous)
  290. squashfs_process_blocks(req);
  291. else {
  292. INIT_WORK(&req->offload, read_wq_handler);
  293. schedule_work(&req->offload);
  294. }
  295. return 0;
  296. bio_alloc_failed:
  297. kfree(bio_req);
  298. req_alloc_failed:
  299. unlock_buffer(bh);
  300. while (--nr_buffers >= b)
  301. if (req->bh[nr_buffers])
  302. put_bh(req->bh[nr_buffers]);
  303. while (--b >= 0)
  304. if (req->bh[b])
  305. wait_on_buffer(req->bh[b]);
  306. getblk_failed:
  307. free_read_request(req, -ENOMEM);
  308. return -ENOMEM;
  309. }
  310. static int read_metadata_block(struct squashfs_read_request *req,
  311. u64 *next_index)
  312. {
  313. int ret, error, bytes_read = 0, bytes_uncompressed = 0;
  314. struct squashfs_sb_info *msblk = req->sb->s_fs_info;
  315. if (req->index + 2 > msblk->bytes_used) {
  316. free_read_request(req, -EINVAL);
  317. return -EINVAL;
  318. }
  319. req->length = 2;
  320. /* Do not read beyond the end of the device */
  321. if (req->index + req->length > msblk->bytes_used)
  322. req->length = msblk->bytes_used - req->index;
  323. req->data_processing = SQUASHFS_METADATA;
  324. /*
  325. * Reading metadata is always synchronous because we don't know the
  326. * length in advance and the function is expected to update
  327. * 'next_index' and return the length.
  328. */
  329. req->synchronous = true;
  330. req->res = &error;
  331. req->bytes_read = &bytes_read;
  332. req->bytes_uncompressed = &bytes_uncompressed;
  333. TRACE("Metadata block @ 0x%llx, %scompressed size %d, src size %d\n",
  334. req->index, req->compressed ? "" : "un", bytes_read,
  335. req->output->length);
  336. ret = squashfs_bio_submit(req);
  337. if (ret)
  338. return ret;
  339. if (error)
  340. return error;
  341. if (next_index)
  342. *next_index += 2 + bytes_read;
  343. return bytes_uncompressed;
  344. }
  345. static int read_data_block(struct squashfs_read_request *req, int length,
  346. u64 *next_index, bool synchronous)
  347. {
  348. int ret, error = 0, bytes_uncompressed = 0, bytes_read = 0;
  349. req->compressed = SQUASHFS_COMPRESSED_BLOCK(length);
  350. req->length = length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
  351. req->data_processing = req->compressed ? SQUASHFS_DECOMPRESS
  352. : SQUASHFS_COPY;
  353. req->synchronous = synchronous;
  354. if (synchronous) {
  355. req->res = &error;
  356. req->bytes_read = &bytes_read;
  357. req->bytes_uncompressed = &bytes_uncompressed;
  358. }
  359. TRACE("Data block @ 0x%llx, %scompressed size %d, src size %d\n",
  360. req->index, req->compressed ? "" : "un", req->length,
  361. req->output->length);
  362. ret = squashfs_bio_submit(req);
  363. if (ret)
  364. return ret;
  365. if (synchronous)
  366. ret = error ? error : bytes_uncompressed;
  367. if (next_index)
  368. *next_index += length;
  369. return ret;
  370. }
  371. /*
  372. * Read and decompress a metadata block or datablock. Length is non-zero
  373. * if a datablock is being read (the size is stored elsewhere in the
  374. * filesystem), otherwise the length is obtained from the first two bytes of
  375. * the metadata block. A bit in the length field indicates if the block
  376. * is stored uncompressed in the filesystem (usually because compression
  377. * generated a larger block - this does occasionally happen with compression
  378. * algorithms).
  379. */
  380. static int __squashfs_read_data(struct super_block *sb, u64 index, int length,
  381. u64 *next_index, struct squashfs_page_actor *output, bool sync)
  382. {
  383. struct squashfs_read_request *req;
  384. req = kcalloc(1, sizeof(struct squashfs_read_request), GFP_KERNEL);
  385. if (!req) {
  386. if (!sync)
  387. squashfs_page_actor_free(output, -ENOMEM);
  388. return -ENOMEM;
  389. }
  390. req->sb = sb;
  391. req->index = index;
  392. req->output = output;
  393. if (next_index)
  394. *next_index = index;
  395. if (length)
  396. length = read_data_block(req, length, next_index, sync);
  397. else
  398. length = read_metadata_block(req, next_index);
  399. if (length < 0) {
  400. ERROR("squashfs_read_data failed to read block 0x%llx\n",
  401. (unsigned long long)index);
  402. return -EIO;
  403. }
  404. return length;
  405. }
  406. int squashfs_read_data(struct super_block *sb, u64 index, int length,
  407. u64 *next_index, struct squashfs_page_actor *output)
  408. {
  409. return __squashfs_read_data(sb, index, length, next_index, output,
  410. true);
  411. }
  412. int squashfs_read_data_async(struct super_block *sb, u64 index, int length,
  413. u64 *next_index, struct squashfs_page_actor *output)
  414. {
  415. return __squashfs_read_data(sb, index, length, next_index, output,
  416. false);
  417. }