1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * index.h - Defines for NTFS kernel index handling.  Part of the Linux-NTFS
4  *	     project.
5  *
6  * Copyright (c) 2004 Anton Altaparmakov
7  */
8 
9 #ifndef _LINUX_NTFS_INDEX_H
10 #define _LINUX_NTFS_INDEX_H
11 
12 #include <linux/fs.h>
13 
14 #include "types.h"
15 #include "layout.h"
16 #include "inode.h"
17 #include "attrib.h"
18 #include "mft.h"
19 #include "aops.h"
20 
21 /**
22  * @idx_ni:	index inode containing the @entry described by this context
23  * @entry:	index entry (points into @ir or @ia)
24  * @data:	index entry data (points into @entry)
25  * @data_len:	length in bytes of @data
26  * @is_in_root:	'true' if @entry is in @ir and 'false' if it is in @ia
27  * @ir:		index root if @is_in_root and NULL otherwise
28  * @actx:	attribute search context if @is_in_root and NULL otherwise
29  * @base_ni:	base inode if @is_in_root and NULL otherwise
30  * @ia:		index block if @is_in_root is 'false' and NULL otherwise
31  * @page:	page if @is_in_root is 'false' and NULL otherwise
32  *
33  * @idx_ni is the index inode this context belongs to.
34  *
35  * @entry is the index entry described by this context.  @data and @data_len
36  * are the index entry data and its length in bytes, respectively.  @data
37  * simply points into @entry.  This is probably what the user is interested in.
38  *
39  * If @is_in_root is 'true', @entry is in the index root attribute @ir described
40  * by the attribute search context @actx and the base inode @base_ni.  @ia and
41  * @page are NULL in this case.
42  *
43  * If @is_in_root is 'false', @entry is in the index allocation attribute and @ia
44  * and @page point to the index allocation block and the mapped, locked page it
45  * is in, respectively.  @ir, @actx and @base_ni are NULL in this case.
46  *
47  * To obtain a context call ntfs_index_ctx_get().
48  *
49  * We use this context to allow ntfs_index_lookup() to return the found index
50  * @entry and its @data without having to allocate a buffer and copy the @entry
51  * and/or its @data into it.
52  *
53  * When finished with the @entry and its @data, call ntfs_index_ctx_put() to
54  * free the context and other associated resources.
55  *
56  * If the index entry was modified, call flush_dcache_index_entry_page()
57  * immediately after the modification and either ntfs_index_entry_mark_dirty()
58  * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
59  * ensure that the changes are written to disk.
60  */
61 typedef struct {
62 	ntfs_inode *idx_ni;
63 	INDEX_ENTRY *entry;
64 	void *data;
65 	u16 data_len;
66 	bool is_in_root;
67 	INDEX_ROOT *ir;
68 	ntfs_attr_search_ctx *actx;
69 	ntfs_inode *base_ni;
70 	INDEX_ALLOCATION *ia;
71 	struct page *page;
72 } ntfs_index_context;
73 
74 extern ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni);
75 extern void ntfs_index_ctx_put(ntfs_index_context *ictx);
76 
77 extern int ntfs_index_lookup(const void *key, const int key_len,
78 		ntfs_index_context *ictx);
79 
80 #ifdef NTFS_RW
81 
82 /**
83  * ntfs_index_entry_flush_dcache_page - flush_dcache_page() for index entries
84  * @ictx:	ntfs index context describing the index entry
85  *
86  * Call flush_dcache_page() for the page in which an index entry resides.
87  *
88  * This must be called every time an index entry is modified, just after the
89  * modification.
90  *
91  * If the index entry is in the index root attribute, simply flush the page
92  * containing the mft record containing the index root attribute.
93  *
94  * If the index entry is in an index block belonging to the index allocation
95  * attribute, simply flush the page cache page containing the index block.
96  */
ntfs_index_entry_flush_dcache_page(ntfs_index_context * ictx)97 static inline void ntfs_index_entry_flush_dcache_page(ntfs_index_context *ictx)
98 {
99 	if (ictx->is_in_root)
100 		flush_dcache_mft_record_page(ictx->actx->ntfs_ino);
101 	else
102 		flush_dcache_page(ictx->page);
103 }
104 
105 /**
106  * ntfs_index_entry_mark_dirty - mark an index entry dirty
107  * @ictx:	ntfs index context describing the index entry
108  *
109  * Mark the index entry described by the index entry context @ictx dirty.
110  *
111  * If the index entry is in the index root attribute, simply mark the mft
112  * record containing the index root attribute dirty.  This ensures the mft
113  * record, and hence the index root attribute, will be written out to disk
114  * later.
115  *
116  * If the index entry is in an index block belonging to the index allocation
117  * attribute, mark the buffers belonging to the index record as well as the
118  * page cache page the index block is in dirty.  This automatically marks the
119  * VFS inode of the ntfs index inode to which the index entry belongs dirty,
120  * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the
121  * dirty index block, will be written out to disk later.
122  */
ntfs_index_entry_mark_dirty(ntfs_index_context * ictx)123 static inline void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
124 {
125 	if (ictx->is_in_root)
126 		mark_mft_record_dirty(ictx->actx->ntfs_ino);
127 	else
128 		mark_ntfs_record_dirty(ictx->page,
129 				(u8*)ictx->ia - (u8*)page_address(ictx->page));
130 }
131 
132 #endif /* NTFS_RW */
133 
134 #endif /* _LINUX_NTFS_INDEX_H */
135