blob: 2adb04316941017109ba85b893d504d06b230be0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lcnalloc.h - Exports for NTFS kernel cluster (de)allocation. Part of the
3 * Linux-NTFS project.
4 *
Anton Altaparmakov715dc632005-09-23 11:24:28 +01005 * Copyright (c) 2004-2005 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef _LINUX_NTFS_LCNALLOC_H
24#define _LINUX_NTFS_LCNALLOC_H
25
26#ifdef NTFS_RW
27
28#include <linux/fs.h>
29
Anton Altaparmakov511bea52005-10-04 14:24:21 +010030#include "attrib.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "types.h"
Anton Altaparmakov715dc632005-09-23 11:24:28 +010032#include "inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "runlist.h"
34#include "volume.h"
35
36typedef enum {
37 FIRST_ZONE = 0, /* For sanity checking. */
38 MFT_ZONE = 0, /* Allocate from $MFT zone. */
39 DATA_ZONE = 1, /* Allocate from $DATA zone. */
40 LAST_ZONE = 1, /* For sanity checking. */
41} NTFS_CLUSTER_ALLOCATION_ZONES;
42
43extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
44 const VCN start_vcn, const s64 count, const LCN start_lcn,
Anton Altaparmakovfc0fa7d2005-10-04 14:36:56 +010045 const NTFS_CLUSTER_ALLOCATION_ZONES zone,
Richard Knutssonc49c3112006-09-30 23:27:12 -070046 const bool is_extension);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Anton Altaparmakov715dc632005-09-23 11:24:28 +010048extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
Richard Knutssonc49c3112006-09-30 23:27:12 -070049 s64 count, ntfs_attr_search_ctx *ctx, const bool is_rollback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/**
52 * ntfs_cluster_free - free clusters on an ntfs volume
Anton Altaparmakov715dc632005-09-23 11:24:28 +010053 * @ni: ntfs inode whose runlist describes the clusters to free
54 * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * @count: number of clusters to free or -1 for all clusters
Anton Altaparmakov511bea52005-10-04 14:24:21 +010056 * @ctx: active attribute search context if present or NULL if not
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Free @count clusters starting at the cluster @start_vcn in the runlist
Anton Altaparmakov715dc632005-09-23 11:24:28 +010059 * described by the ntfs inode @ni.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * If @count is -1, all clusters from @start_vcn to the end of the runlist are
62 * deallocated. Thus, to completely free all clusters in a runlist, use
63 * @start_vcn = 0 and @count = -1.
64 *
Anton Altaparmakov511bea52005-10-04 14:24:21 +010065 * If @ctx is specified, it is an active search context of @ni and its base mft
66 * record. This is needed when ntfs_cluster_free() encounters unmapped runlist
67 * fragments and allows their mapping. If you do not have the mft record
68 * mapped, you can specify @ctx as NULL and ntfs_cluster_free() will perform
69 * the necessary mapping and unmapping.
70 *
71 * Note, ntfs_cluster_free() saves the state of @ctx on entry and restores it
72 * before returning. Thus, @ctx will be left pointing to the same attribute on
73 * return as on entry. However, the actual pointers in @ctx may point to
74 * different memory locations on return, so you must remember to reset any
75 * cached pointers from the @ctx, i.e. after the call to ntfs_cluster_free(),
76 * you will probably want to do:
77 * m = ctx->mrec;
78 * a = ctx->attr;
79 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
80 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
81 *
82 * Note, ntfs_cluster_free() does not modify the runlist, so you have to remove
83 * from the runlist or mark sparse the freed runs later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *
85 * Return the number of deallocated clusters (not counting sparse ones) on
86 * success and -errno on error.
87 *
Anton Altaparmakov511bea52005-10-04 14:24:21 +010088 * WARNING: If @ctx is supplied, regardless of whether success or failure is
Richard Knutssonc49c3112006-09-30 23:27:12 -070089 * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
Anton Altaparmakov511bea52005-10-04 14:24:21 +010090 * is no longer valid, i.e. you need to either call
91 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
92 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
93 * why the mapping of the old inode failed.
94 *
Anton Altaparmakov715dc632005-09-23 11:24:28 +010095 * Locking: - The runlist described by @ni must be locked for writing on entry
96 * and is locked on return. Note the runlist may be modified when
97 * needed runlist fragments need to be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * - The volume lcn bitmap must be unlocked on entry and is unlocked
99 * on return.
100 * - This function takes the volume lcn bitmap lock for writing and
101 * modifies the bitmap contents.
Anton Altaparmakov511bea52005-10-04 14:24:21 +0100102 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
103 * entry and it will be left unmapped on return.
104 * - If @ctx is not NULL, the base mft record must be mapped on entry
105 * and it will be left mapped on return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Anton Altaparmakov715dc632005-09-23 11:24:28 +0100107static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
Anton Altaparmakov511bea52005-10-04 14:24:21 +0100108 s64 count, ntfs_attr_search_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Richard Knutssonc49c3112006-09-30 23:27:12 -0700110 return __ntfs_cluster_free(ni, start_vcn, count, ctx, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
114 const runlist_element *rl);
115
116/**
117 * ntfs_cluster_free_from_rl - free clusters from runlist
118 * @vol: mounted ntfs volume on which to free the clusters
119 * @rl: runlist describing the clusters to free
120 *
121 * Free all the clusters described by the runlist @rl on the volume @vol. In
122 * the case of an error being returned, at least some of the clusters were not
123 * freed.
124 *
125 * Return 0 on success and -errno on error.
126 *
Anton Altaparmakovbbf18132005-09-08 21:09:06 +0100127 * Locking: - This function takes the volume lcn bitmap lock for writing and
128 * modifies the bitmap contents.
129 * - The caller must have locked the runlist @rl for reading or
130 * writing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol,
133 const runlist_element *rl)
134{
135 int ret;
136
137 down_write(&vol->lcnbmp_lock);
138 ret = ntfs_cluster_free_from_rl_nolock(vol, rl);
139 up_write(&vol->lcnbmp_lock);
140 return ret;
141}
142
143#endif /* NTFS_RW */
144
145#endif /* defined _LINUX_NTFS_LCNALLOC_H */