http://linux-ntfs.sourceforge.net/snapshots/ntfsprogs-200307311516.tar.bz2
[ntfsprogs.git] / include / mft.h
1 /*
2  * mft.h - Exports for MFT record handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2000-2002 Anton Altaparmakov
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifndef _NTFS_MFT_H
23 #define _NTFS_MFT_H
24
25 #include "volume.h"
26 #include "inode.h"
27 #include "layout.h"
28
29 extern int ntfs_mft_records_read(const ntfs_volume *vol, const MFT_REF mref,
30                 const s64 count, MFT_RECORD *b);
31
32 /**
33  * ntfs_mft_record_read - read a record from the mft
34  * @vol:        volume to read from
35  * @mref:       mft record number to read
36  * @b:          output data buffer
37  *
38  * Read the mft record specified by @mref from volume @vol into buffer @b.
39  * Return 0 on success or -1 on error, with errno set to the error code.
40  *
41  * The read mft record is mst deprotected and is hence ready to use. The caller
42  * should check the record with is_baad_record() in case mst deprotection
43  * failed.
44  *
45  * NOTE: @b has to be at least of size vol->mft_record_size.
46  */
47 static __inline__ int ntfs_mft_record_read(const ntfs_volume *vol,
48                 const MFT_REF mref, MFT_RECORD *b)
49 {
50         return ntfs_mft_records_read(vol, mref, 1, b);
51 }
52
53 extern int ntfs_file_record_read(const ntfs_volume *vol, const MFT_REF mref,
54                 MFT_RECORD **mrec, ATTR_RECORD **attr);
55
56 extern int ntfs_mft_records_write(const ntfs_volume *vol, const MFT_REF mref,
57                 const s64 count, MFT_RECORD *b);
58
59 /**
60  * ntfs_mft_record_write - write an mft record to disk
61  * @vol:        volume to write to
62  * @mref:       mft record number to write
63  * @b:          data buffer containing the mft record to write
64  *
65  * Write the mft record specified by @mref from buffer @b to volume @vol.
66  * Return 0 on success or -1 on error, with errno set to the error code.
67  *
68  * Before the mft record is written, it is mst protected. After the write, it
69  * is deprotected again, thus resulting in an increase in the update sequence
70  * number inside the buffer @b.
71  *
72  * NOTE: @b has to be at least of size vol->mft_record_size.
73  */
74 static __inline__ int ntfs_mft_record_write(const ntfs_volume *vol,
75                 const MFT_REF mref, MFT_RECORD *b)
76 {
77         return ntfs_mft_records_write(vol, mref, 1, b);
78 }
79
80 /**
81  * ntfs_mft_record_get_data_size - return number of bytes used in mft record @b
82  * @m:          mft record to get the data size of
83  *
84  * Takes the mft record @m and returns the number of bytes used in the record
85  * or 0 on error (i.e. @m is not a valid mft record). Zero is not a valid size
86  * for an mft record as it at least has to have the MFT_RECORD, thus making the
87  * minimum size:
88  *      (sizeof(MFT_RECORD) + 7) & ~7 + sizeof(ATTR_TYPES) = 52 bytes
89  * Aside: The 8-byte alignment and the 4 bytes for the attribute type are needed
90  * as each mft record has to have a list of attributes even if it only contains
91  * the attribute $END which doesn't contain anything else apart from its type.
92  * Also, you would expect every mft record to contain an update sequence array
93  * as well but that could in theory be non-existent (don't know if Windows'
94  * NTFS driver/chkdsk wouldn't view this as corruption in itself though).
95  */
96 static __inline__ u32 ntfs_mft_record_get_data_size(const MFT_RECORD *m)
97 {
98         if (!m || !ntfs_is_mft_record(m->magic))
99                 return 0;
100         /* Get the number of used bytes and return it. */
101         return le32_to_cpu(m->bytes_in_use);
102 }
103
104 extern ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, u64 start);
105
106 extern int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni);
107
108 #endif /* defined _NTFS_MFT_H */
109