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