1a1d312deSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * lcnalloc.h - Exports for NTFS kernel cluster (de)allocation. Part of the
41da177e4SLinus Torvalds * Linux-NTFS project.
51da177e4SLinus Torvalds *
6715dc636SAnton Altaparmakov * Copyright (c) 2004-2005 Anton Altaparmakov
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #ifndef _LINUX_NTFS_LCNALLOC_H
101da177e4SLinus Torvalds #define _LINUX_NTFS_LCNALLOC_H
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #ifdef NTFS_RW
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds #include <linux/fs.h>
151da177e4SLinus Torvalds
16511bea5eSAnton Altaparmakov #include "attrib.h"
171da177e4SLinus Torvalds #include "types.h"
18715dc636SAnton Altaparmakov #include "inode.h"
191da177e4SLinus Torvalds #include "runlist.h"
201da177e4SLinus Torvalds #include "volume.h"
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds typedef enum {
231da177e4SLinus Torvalds FIRST_ZONE = 0, /* For sanity checking. */
241da177e4SLinus Torvalds MFT_ZONE = 0, /* Allocate from $MFT zone. */
251da177e4SLinus Torvalds DATA_ZONE = 1, /* Allocate from $DATA zone. */
261da177e4SLinus Torvalds LAST_ZONE = 1, /* For sanity checking. */
271da177e4SLinus Torvalds } NTFS_CLUSTER_ALLOCATION_ZONES;
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
301da177e4SLinus Torvalds const VCN start_vcn, const s64 count, const LCN start_lcn,
31fc0fa7dcSAnton Altaparmakov const NTFS_CLUSTER_ALLOCATION_ZONES zone,
32c49c3111SRichard Knutsson const bool is_extension);
331da177e4SLinus Torvalds
34715dc636SAnton Altaparmakov extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
35c49c3111SRichard Knutsson s64 count, ntfs_attr_search_ctx *ctx, const bool is_rollback);
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds /**
381da177e4SLinus Torvalds * ntfs_cluster_free - free clusters on an ntfs volume
39715dc636SAnton Altaparmakov * @ni: ntfs inode whose runlist describes the clusters to free
40715dc636SAnton Altaparmakov * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
411da177e4SLinus Torvalds * @count: number of clusters to free or -1 for all clusters
42511bea5eSAnton Altaparmakov * @ctx: active attribute search context if present or NULL if not
431da177e4SLinus Torvalds *
441da177e4SLinus Torvalds * Free @count clusters starting at the cluster @start_vcn in the runlist
45715dc636SAnton Altaparmakov * described by the ntfs inode @ni.
461da177e4SLinus Torvalds *
471da177e4SLinus Torvalds * If @count is -1, all clusters from @start_vcn to the end of the runlist are
481da177e4SLinus Torvalds * deallocated. Thus, to completely free all clusters in a runlist, use
491da177e4SLinus Torvalds * @start_vcn = 0 and @count = -1.
501da177e4SLinus Torvalds *
51511bea5eSAnton Altaparmakov * If @ctx is specified, it is an active search context of @ni and its base mft
52511bea5eSAnton Altaparmakov * record. This is needed when ntfs_cluster_free() encounters unmapped runlist
53511bea5eSAnton Altaparmakov * fragments and allows their mapping. If you do not have the mft record
54511bea5eSAnton Altaparmakov * mapped, you can specify @ctx as NULL and ntfs_cluster_free() will perform
55511bea5eSAnton Altaparmakov * the necessary mapping and unmapping.
56511bea5eSAnton Altaparmakov *
57511bea5eSAnton Altaparmakov * Note, ntfs_cluster_free() saves the state of @ctx on entry and restores it
58511bea5eSAnton Altaparmakov * before returning. Thus, @ctx will be left pointing to the same attribute on
59511bea5eSAnton Altaparmakov * return as on entry. However, the actual pointers in @ctx may point to
60511bea5eSAnton Altaparmakov * different memory locations on return, so you must remember to reset any
61511bea5eSAnton Altaparmakov * cached pointers from the @ctx, i.e. after the call to ntfs_cluster_free(),
62511bea5eSAnton Altaparmakov * you will probably want to do:
63511bea5eSAnton Altaparmakov * m = ctx->mrec;
64511bea5eSAnton Altaparmakov * a = ctx->attr;
65511bea5eSAnton Altaparmakov * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
66511bea5eSAnton Altaparmakov * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
67511bea5eSAnton Altaparmakov *
68511bea5eSAnton Altaparmakov * Note, ntfs_cluster_free() does not modify the runlist, so you have to remove
69511bea5eSAnton Altaparmakov * from the runlist or mark sparse the freed runs later.
701da177e4SLinus Torvalds *
711da177e4SLinus Torvalds * Return the number of deallocated clusters (not counting sparse ones) on
721da177e4SLinus Torvalds * success and -errno on error.
731da177e4SLinus Torvalds *
74511bea5eSAnton Altaparmakov * WARNING: If @ctx is supplied, regardless of whether success or failure is
75c49c3111SRichard Knutsson * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
76511bea5eSAnton Altaparmakov * is no longer valid, i.e. you need to either call
77511bea5eSAnton Altaparmakov * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
78511bea5eSAnton Altaparmakov * In that case PTR_ERR(@ctx->mrec) will give you the error code for
79511bea5eSAnton Altaparmakov * why the mapping of the old inode failed.
80511bea5eSAnton Altaparmakov *
81715dc636SAnton Altaparmakov * Locking: - The runlist described by @ni must be locked for writing on entry
82715dc636SAnton Altaparmakov * and is locked on return. Note the runlist may be modified when
83715dc636SAnton Altaparmakov * needed runlist fragments need to be mapped.
841da177e4SLinus Torvalds * - The volume lcn bitmap must be unlocked on entry and is unlocked
851da177e4SLinus Torvalds * on return.
861da177e4SLinus Torvalds * - This function takes the volume lcn bitmap lock for writing and
871da177e4SLinus Torvalds * modifies the bitmap contents.
88511bea5eSAnton Altaparmakov * - If @ctx is NULL, the base mft record of @ni must not be mapped on
89511bea5eSAnton Altaparmakov * entry and it will be left unmapped on return.
90511bea5eSAnton Altaparmakov * - If @ctx is not NULL, the base mft record must be mapped on entry
91511bea5eSAnton Altaparmakov * and it will be left mapped on return.
921da177e4SLinus Torvalds */
ntfs_cluster_free(ntfs_inode * ni,const VCN start_vcn,s64 count,ntfs_attr_search_ctx * ctx)93715dc636SAnton Altaparmakov static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
94511bea5eSAnton Altaparmakov s64 count, ntfs_attr_search_ctx *ctx)
951da177e4SLinus Torvalds {
96c49c3111SRichard Knutsson return __ntfs_cluster_free(ni, start_vcn, count, ctx, false);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds
991da177e4SLinus Torvalds extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
1001da177e4SLinus Torvalds const runlist_element *rl);
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds /**
1031da177e4SLinus Torvalds * ntfs_cluster_free_from_rl - free clusters from runlist
1041da177e4SLinus Torvalds * @vol: mounted ntfs volume on which to free the clusters
1051da177e4SLinus Torvalds * @rl: runlist describing the clusters to free
1061da177e4SLinus Torvalds *
1071da177e4SLinus Torvalds * Free all the clusters described by the runlist @rl on the volume @vol. In
1081da177e4SLinus Torvalds * the case of an error being returned, at least some of the clusters were not
1091da177e4SLinus Torvalds * freed.
1101da177e4SLinus Torvalds *
1111da177e4SLinus Torvalds * Return 0 on success and -errno on error.
1121da177e4SLinus Torvalds *
113bbf1813fSAnton Altaparmakov * Locking: - This function takes the volume lcn bitmap lock for writing and
1141da177e4SLinus Torvalds * modifies the bitmap contents.
115bbf1813fSAnton Altaparmakov * - The caller must have locked the runlist @rl for reading or
116bbf1813fSAnton Altaparmakov * writing.
1171da177e4SLinus Torvalds */
ntfs_cluster_free_from_rl(ntfs_volume * vol,const runlist_element * rl)1181da177e4SLinus Torvalds static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol,
1191da177e4SLinus Torvalds const runlist_element *rl)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds int ret;
1221da177e4SLinus Torvalds
1231da177e4SLinus Torvalds down_write(&vol->lcnbmp_lock);
1241da177e4SLinus Torvalds ret = ntfs_cluster_free_from_rl_nolock(vol, rl);
1251da177e4SLinus Torvalds up_write(&vol->lcnbmp_lock);
1261da177e4SLinus Torvalds return ret;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvalds #endif /* NTFS_RW */
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds #endif /* defined _LINUX_NTFS_LCNALLOC_H */
132