lcnalloc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * lcnalloc.h - Exports for NTFS kernel cluster (de)allocation. Part of the
  4. * Linux-NTFS project.
  5. *
  6. * Copyright (c) 2004-2005 Anton Altaparmakov
  7. */
  8. #ifndef _LINUX_NTFS_LCNALLOC_H
  9. #define _LINUX_NTFS_LCNALLOC_H
  10. #ifdef NTFS_RW
  11. #include <linux/fs.h>
  12. #include "attrib.h"
  13. #include "types.h"
  14. #include "inode.h"
  15. #include "runlist.h"
  16. #include "volume.h"
  17. typedef enum {
  18. FIRST_ZONE = 0, /* For sanity checking. */
  19. MFT_ZONE = 0, /* Allocate from $MFT zone. */
  20. DATA_ZONE = 1, /* Allocate from $DATA zone. */
  21. LAST_ZONE = 1, /* For sanity checking. */
  22. } NTFS_CLUSTER_ALLOCATION_ZONES;
  23. extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
  24. const VCN start_vcn, const s64 count, const LCN start_lcn,
  25. const NTFS_CLUSTER_ALLOCATION_ZONES zone,
  26. const bool is_extension);
  27. extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
  28. s64 count, ntfs_attr_search_ctx *ctx, const bool is_rollback);
  29. /**
  30. * ntfs_cluster_free - free clusters on an ntfs volume
  31. * @ni: ntfs inode whose runlist describes the clusters to free
  32. * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
  33. * @count: number of clusters to free or -1 for all clusters
  34. * @ctx: active attribute search context if present or NULL if not
  35. *
  36. * Free @count clusters starting at the cluster @start_vcn in the runlist
  37. * described by the ntfs inode @ni.
  38. *
  39. * If @count is -1, all clusters from @start_vcn to the end of the runlist are
  40. * deallocated. Thus, to completely free all clusters in a runlist, use
  41. * @start_vcn = 0 and @count = -1.
  42. *
  43. * If @ctx is specified, it is an active search context of @ni and its base mft
  44. * record. This is needed when ntfs_cluster_free() encounters unmapped runlist
  45. * fragments and allows their mapping. If you do not have the mft record
  46. * mapped, you can specify @ctx as NULL and ntfs_cluster_free() will perform
  47. * the necessary mapping and unmapping.
  48. *
  49. * Note, ntfs_cluster_free() saves the state of @ctx on entry and restores it
  50. * before returning. Thus, @ctx will be left pointing to the same attribute on
  51. * return as on entry. However, the actual pointers in @ctx may point to
  52. * different memory locations on return, so you must remember to reset any
  53. * cached pointers from the @ctx, i.e. after the call to ntfs_cluster_free(),
  54. * you will probably want to do:
  55. * m = ctx->mrec;
  56. * a = ctx->attr;
  57. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  58. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  59. *
  60. * Note, ntfs_cluster_free() does not modify the runlist, so you have to remove
  61. * from the runlist or mark sparse the freed runs later.
  62. *
  63. * Return the number of deallocated clusters (not counting sparse ones) on
  64. * success and -errno on error.
  65. *
  66. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  67. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  68. * is no longer valid, i.e. you need to either call
  69. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  70. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  71. * why the mapping of the old inode failed.
  72. *
  73. * Locking: - The runlist described by @ni must be locked for writing on entry
  74. * and is locked on return. Note the runlist may be modified when
  75. * needed runlist fragments need to be mapped.
  76. * - The volume lcn bitmap must be unlocked on entry and is unlocked
  77. * on return.
  78. * - This function takes the volume lcn bitmap lock for writing and
  79. * modifies the bitmap contents.
  80. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  81. * entry and it will be left unmapped on return.
  82. * - If @ctx is not NULL, the base mft record must be mapped on entry
  83. * and it will be left mapped on return.
  84. */
  85. static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
  86. s64 count, ntfs_attr_search_ctx *ctx)
  87. {
  88. return __ntfs_cluster_free(ni, start_vcn, count, ctx, false);
  89. }
  90. extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
  91. const runlist_element *rl);
  92. /**
  93. * ntfs_cluster_free_from_rl - free clusters from runlist
  94. * @vol: mounted ntfs volume on which to free the clusters
  95. * @rl: runlist describing the clusters to free
  96. *
  97. * Free all the clusters described by the runlist @rl on the volume @vol. In
  98. * the case of an error being returned, at least some of the clusters were not
  99. * freed.
  100. *
  101. * Return 0 on success and -errno on error.
  102. *
  103. * Locking: - This function takes the volume lcn bitmap lock for writing and
  104. * modifies the bitmap contents.
  105. * - The caller must have locked the runlist @rl for reading or
  106. * writing.
  107. */
  108. static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol,
  109. const runlist_element *rl)
  110. {
  111. int ret;
  112. down_write(&vol->lcnbmp_lock);
  113. ret = ntfs_cluster_free_from_rl_nolock(vol, rl);
  114. up_write(&vol->lcnbmp_lock);
  115. return ret;
  116. }
  117. #endif /* NTFS_RW */
  118. #endif /* defined _LINUX_NTFS_LCNALLOC_H */