18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * index.h - Defines for NTFS kernel index handling.  Part of the Linux-NTFS
48c2ecf20Sopenharmony_ci *	     project.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2004 Anton Altaparmakov
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _LINUX_NTFS_INDEX_H
108c2ecf20Sopenharmony_ci#define _LINUX_NTFS_INDEX_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/fs.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "types.h"
158c2ecf20Sopenharmony_ci#include "layout.h"
168c2ecf20Sopenharmony_ci#include "inode.h"
178c2ecf20Sopenharmony_ci#include "attrib.h"
188c2ecf20Sopenharmony_ci#include "mft.h"
198c2ecf20Sopenharmony_ci#include "aops.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/**
228c2ecf20Sopenharmony_ci * @idx_ni:	index inode containing the @entry described by this context
238c2ecf20Sopenharmony_ci * @entry:	index entry (points into @ir or @ia)
248c2ecf20Sopenharmony_ci * @data:	index entry data (points into @entry)
258c2ecf20Sopenharmony_ci * @data_len:	length in bytes of @data
268c2ecf20Sopenharmony_ci * @is_in_root:	'true' if @entry is in @ir and 'false' if it is in @ia
278c2ecf20Sopenharmony_ci * @ir:		index root if @is_in_root and NULL otherwise
288c2ecf20Sopenharmony_ci * @actx:	attribute search context if @is_in_root and NULL otherwise
298c2ecf20Sopenharmony_ci * @base_ni:	base inode if @is_in_root and NULL otherwise
308c2ecf20Sopenharmony_ci * @ia:		index block if @is_in_root is 'false' and NULL otherwise
318c2ecf20Sopenharmony_ci * @page:	page if @is_in_root is 'false' and NULL otherwise
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * @idx_ni is the index inode this context belongs to.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * @entry is the index entry described by this context.  @data and @data_len
368c2ecf20Sopenharmony_ci * are the index entry data and its length in bytes, respectively.  @data
378c2ecf20Sopenharmony_ci * simply points into @entry.  This is probably what the user is interested in.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * If @is_in_root is 'true', @entry is in the index root attribute @ir described
408c2ecf20Sopenharmony_ci * by the attribute search context @actx and the base inode @base_ni.  @ia and
418c2ecf20Sopenharmony_ci * @page are NULL in this case.
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * If @is_in_root is 'false', @entry is in the index allocation attribute and @ia
448c2ecf20Sopenharmony_ci * and @page point to the index allocation block and the mapped, locked page it
458c2ecf20Sopenharmony_ci * is in, respectively.  @ir, @actx and @base_ni are NULL in this case.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * To obtain a context call ntfs_index_ctx_get().
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * We use this context to allow ntfs_index_lookup() to return the found index
508c2ecf20Sopenharmony_ci * @entry and its @data without having to allocate a buffer and copy the @entry
518c2ecf20Sopenharmony_ci * and/or its @data into it.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * When finished with the @entry and its @data, call ntfs_index_ctx_put() to
548c2ecf20Sopenharmony_ci * free the context and other associated resources.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * If the index entry was modified, call flush_dcache_index_entry_page()
578c2ecf20Sopenharmony_ci * immediately after the modification and either ntfs_index_entry_mark_dirty()
588c2ecf20Sopenharmony_ci * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
598c2ecf20Sopenharmony_ci * ensure that the changes are written to disk.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_citypedef struct {
628c2ecf20Sopenharmony_ci	ntfs_inode *idx_ni;
638c2ecf20Sopenharmony_ci	INDEX_ENTRY *entry;
648c2ecf20Sopenharmony_ci	void *data;
658c2ecf20Sopenharmony_ci	u16 data_len;
668c2ecf20Sopenharmony_ci	bool is_in_root;
678c2ecf20Sopenharmony_ci	INDEX_ROOT *ir;
688c2ecf20Sopenharmony_ci	ntfs_attr_search_ctx *actx;
698c2ecf20Sopenharmony_ci	ntfs_inode *base_ni;
708c2ecf20Sopenharmony_ci	INDEX_ALLOCATION *ia;
718c2ecf20Sopenharmony_ci	struct page *page;
728c2ecf20Sopenharmony_ci} ntfs_index_context;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciextern ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni);
758c2ecf20Sopenharmony_ciextern void ntfs_index_ctx_put(ntfs_index_context *ictx);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciextern int ntfs_index_lookup(const void *key, const int key_len,
788c2ecf20Sopenharmony_ci		ntfs_index_context *ictx);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#ifdef NTFS_RW
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/**
838c2ecf20Sopenharmony_ci * ntfs_index_entry_flush_dcache_page - flush_dcache_page() for index entries
848c2ecf20Sopenharmony_ci * @ictx:	ntfs index context describing the index entry
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * Call flush_dcache_page() for the page in which an index entry resides.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * This must be called every time an index entry is modified, just after the
898c2ecf20Sopenharmony_ci * modification.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * If the index entry is in the index root attribute, simply flush the page
928c2ecf20Sopenharmony_ci * containing the mft record containing the index root attribute.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * If the index entry is in an index block belonging to the index allocation
958c2ecf20Sopenharmony_ci * attribute, simply flush the page cache page containing the index block.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_cistatic inline void ntfs_index_entry_flush_dcache_page(ntfs_index_context *ictx)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	if (ictx->is_in_root)
1008c2ecf20Sopenharmony_ci		flush_dcache_mft_record_page(ictx->actx->ntfs_ino);
1018c2ecf20Sopenharmony_ci	else
1028c2ecf20Sopenharmony_ci		flush_dcache_page(ictx->page);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/**
1068c2ecf20Sopenharmony_ci * ntfs_index_entry_mark_dirty - mark an index entry dirty
1078c2ecf20Sopenharmony_ci * @ictx:	ntfs index context describing the index entry
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * Mark the index entry described by the index entry context @ictx dirty.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * If the index entry is in the index root attribute, simply mark the mft
1128c2ecf20Sopenharmony_ci * record containing the index root attribute dirty.  This ensures the mft
1138c2ecf20Sopenharmony_ci * record, and hence the index root attribute, will be written out to disk
1148c2ecf20Sopenharmony_ci * later.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * If the index entry is in an index block belonging to the index allocation
1178c2ecf20Sopenharmony_ci * attribute, mark the buffers belonging to the index record as well as the
1188c2ecf20Sopenharmony_ci * page cache page the index block is in dirty.  This automatically marks the
1198c2ecf20Sopenharmony_ci * VFS inode of the ntfs index inode to which the index entry belongs dirty,
1208c2ecf20Sopenharmony_ci * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the
1218c2ecf20Sopenharmony_ci * dirty index block, will be written out to disk later.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_cistatic inline void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	if (ictx->is_in_root)
1268c2ecf20Sopenharmony_ci		mark_mft_record_dirty(ictx->actx->ntfs_ino);
1278c2ecf20Sopenharmony_ci	else
1288c2ecf20Sopenharmony_ci		mark_ntfs_record_dirty(ictx->page,
1298c2ecf20Sopenharmony_ci				(u8*)ictx->ia - (u8*)page_address(ictx->page));
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#endif /* NTFS_RW */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#endif /* _LINUX_NTFS_INDEX_H */
135