decompressor_multi_percpu.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }