decompressor_multi_percpu.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2013
  3. * Phillip Lougher <phillip@squashfs.org.uk>
  4. *
  5. * This work is licensed under the terms of the GNU GPL, version 2. See
  6. * the COPYING file in the top-level directory.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/percpu.h>
  11. #include <linux/buffer_head.h>
  12. #include "squashfs_fs.h"
  13. #include "squashfs_fs_sb.h"
  14. #include "decompressor.h"
  15. #include "squashfs.h"
  16. /*
  17. * This file implements multi-threaded decompression using percpu
  18. * variables, one thread per cpu core.
  19. */
  20. struct squashfs_stream {
  21. void *stream;
  22. };
  23. void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
  24. void *comp_opts)
  25. {
  26. struct squashfs_stream *stream;
  27. struct squashfs_stream __percpu *percpu;
  28. int err, cpu;
  29. percpu = alloc_percpu(struct squashfs_stream);
  30. if (percpu == NULL)
  31. return ERR_PTR(-ENOMEM);
  32. for_each_possible_cpu(cpu) {
  33. stream = per_cpu_ptr(percpu, cpu);
  34. stream->stream = msblk->decompressor->init(msblk, comp_opts);
  35. if (IS_ERR(stream->stream)) {
  36. err = PTR_ERR(stream->stream);
  37. goto out;
  38. }
  39. }
  40. kfree(comp_opts);
  41. return (__force void *) percpu;
  42. out:
  43. for_each_possible_cpu(cpu) {
  44. stream = per_cpu_ptr(percpu, cpu);
  45. if (!IS_ERR_OR_NULL(stream->stream))
  46. msblk->decompressor->free(stream->stream);
  47. }
  48. free_percpu(percpu);
  49. return ERR_PTR(err);
  50. }
  51. void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
  52. {
  53. struct squashfs_stream __percpu *percpu =
  54. (struct squashfs_stream __percpu *) msblk->stream;
  55. struct squashfs_stream *stream;
  56. int cpu;
  57. if (msblk->stream) {
  58. for_each_possible_cpu(cpu) {
  59. stream = per_cpu_ptr(percpu, cpu);
  60. msblk->decompressor->free(stream->stream);
  61. }
  62. free_percpu(percpu);
  63. }
  64. }
  65. int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
  66. int b, int offset, int length, struct squashfs_page_actor *output)
  67. {
  68. struct squashfs_stream __percpu *percpu =
  69. (struct squashfs_stream __percpu *) msblk->stream;
  70. struct squashfs_stream *stream = get_cpu_ptr(percpu);
  71. int res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
  72. offset, length, output);
  73. put_cpu_ptr(stream);
  74. if (res < 0)
  75. ERROR("%s decompression failed, data probably corrupt\n",
  76. msblk->decompressor->name);
  77. return res;
  78. }
  79. int squashfs_max_decompressors(void)
  80. {
  81. return num_possible_cpus();
  82. }