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