162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * index.h - Defines for NTFS kernel index handling.  Part of the Linux-NTFS
462306a36Sopenharmony_ci *	     project.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (c) 2004 Anton Altaparmakov
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef _LINUX_NTFS_INDEX_H
1062306a36Sopenharmony_ci#define _LINUX_NTFS_INDEX_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/fs.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "types.h"
1562306a36Sopenharmony_ci#include "layout.h"
1662306a36Sopenharmony_ci#include "inode.h"
1762306a36Sopenharmony_ci#include "attrib.h"
1862306a36Sopenharmony_ci#include "mft.h"
1962306a36Sopenharmony_ci#include "aops.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/**
2262306a36Sopenharmony_ci * @idx_ni:	index inode containing the @entry described by this context
2362306a36Sopenharmony_ci * @entry:	index entry (points into @ir or @ia)
2462306a36Sopenharmony_ci * @data:	index entry data (points into @entry)
2562306a36Sopenharmony_ci * @data_len:	length in bytes of @data
2662306a36Sopenharmony_ci * @is_in_root:	'true' if @entry is in @ir and 'false' if it is in @ia
2762306a36Sopenharmony_ci * @ir:		index root if @is_in_root and NULL otherwise
2862306a36Sopenharmony_ci * @actx:	attribute search context if @is_in_root and NULL otherwise
2962306a36Sopenharmony_ci * @base_ni:	base inode if @is_in_root and NULL otherwise
3062306a36Sopenharmony_ci * @ia:		index block if @is_in_root is 'false' and NULL otherwise
3162306a36Sopenharmony_ci * @page:	page if @is_in_root is 'false' and NULL otherwise
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * @idx_ni is the index inode this context belongs to.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * @entry is the index entry described by this context.  @data and @data_len
3662306a36Sopenharmony_ci * are the index entry data and its length in bytes, respectively.  @data
3762306a36Sopenharmony_ci * simply points into @entry.  This is probably what the user is interested in.
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * If @is_in_root is 'true', @entry is in the index root attribute @ir described
4062306a36Sopenharmony_ci * by the attribute search context @actx and the base inode @base_ni.  @ia and
4162306a36Sopenharmony_ci * @page are NULL in this case.
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * If @is_in_root is 'false', @entry is in the index allocation attribute and @ia
4462306a36Sopenharmony_ci * and @page point to the index allocation block and the mapped, locked page it
4562306a36Sopenharmony_ci * is in, respectively.  @ir, @actx and @base_ni are NULL in this case.
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * To obtain a context call ntfs_index_ctx_get().
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * We use this context to allow ntfs_index_lookup() to return the found index
5062306a36Sopenharmony_ci * @entry and its @data without having to allocate a buffer and copy the @entry
5162306a36Sopenharmony_ci * and/or its @data into it.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * When finished with the @entry and its @data, call ntfs_index_ctx_put() to
5462306a36Sopenharmony_ci * free the context and other associated resources.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * If the index entry was modified, call flush_dcache_index_entry_page()
5762306a36Sopenharmony_ci * immediately after the modification and either ntfs_index_entry_mark_dirty()
5862306a36Sopenharmony_ci * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
5962306a36Sopenharmony_ci * ensure that the changes are written to disk.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_citypedef struct {
6262306a36Sopenharmony_ci	ntfs_inode *idx_ni;
6362306a36Sopenharmony_ci	INDEX_ENTRY *entry;
6462306a36Sopenharmony_ci	void *data;
6562306a36Sopenharmony_ci	u16 data_len;
6662306a36Sopenharmony_ci	bool is_in_root;
6762306a36Sopenharmony_ci	INDEX_ROOT *ir;
6862306a36Sopenharmony_ci	ntfs_attr_search_ctx *actx;
6962306a36Sopenharmony_ci	ntfs_inode *base_ni;
7062306a36Sopenharmony_ci	INDEX_ALLOCATION *ia;
7162306a36Sopenharmony_ci	struct page *page;
7262306a36Sopenharmony_ci} ntfs_index_context;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciextern ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni);
7562306a36Sopenharmony_ciextern void ntfs_index_ctx_put(ntfs_index_context *ictx);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciextern int ntfs_index_lookup(const void *key, const int key_len,
7862306a36Sopenharmony_ci		ntfs_index_context *ictx);
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#ifdef NTFS_RW
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/**
8362306a36Sopenharmony_ci * ntfs_index_entry_flush_dcache_page - flush_dcache_page() for index entries
8462306a36Sopenharmony_ci * @ictx:	ntfs index context describing the index entry
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci * Call flush_dcache_page() for the page in which an index entry resides.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * This must be called every time an index entry is modified, just after the
8962306a36Sopenharmony_ci * modification.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * If the index entry is in the index root attribute, simply flush the page
9262306a36Sopenharmony_ci * containing the mft record containing the index root attribute.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * If the index entry is in an index block belonging to the index allocation
9562306a36Sopenharmony_ci * attribute, simply flush the page cache page containing the index block.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_cistatic inline void ntfs_index_entry_flush_dcache_page(ntfs_index_context *ictx)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	if (ictx->is_in_root)
10062306a36Sopenharmony_ci		flush_dcache_mft_record_page(ictx->actx->ntfs_ino);
10162306a36Sopenharmony_ci	else
10262306a36Sopenharmony_ci		flush_dcache_page(ictx->page);
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci/**
10662306a36Sopenharmony_ci * ntfs_index_entry_mark_dirty - mark an index entry dirty
10762306a36Sopenharmony_ci * @ictx:	ntfs index context describing the index entry
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci * Mark the index entry described by the index entry context @ictx dirty.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * If the index entry is in the index root attribute, simply mark the mft
11262306a36Sopenharmony_ci * record containing the index root attribute dirty.  This ensures the mft
11362306a36Sopenharmony_ci * record, and hence the index root attribute, will be written out to disk
11462306a36Sopenharmony_ci * later.
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * If the index entry is in an index block belonging to the index allocation
11762306a36Sopenharmony_ci * attribute, mark the buffers belonging to the index record as well as the
11862306a36Sopenharmony_ci * page cache page the index block is in dirty.  This automatically marks the
11962306a36Sopenharmony_ci * VFS inode of the ntfs index inode to which the index entry belongs dirty,
12062306a36Sopenharmony_ci * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the
12162306a36Sopenharmony_ci * dirty index block, will be written out to disk later.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_cistatic inline void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	if (ictx->is_in_root)
12662306a36Sopenharmony_ci		mark_mft_record_dirty(ictx->actx->ntfs_ino);
12762306a36Sopenharmony_ci	else
12862306a36Sopenharmony_ci		mark_ntfs_record_dirty(ictx->page,
12962306a36Sopenharmony_ci				(u8*)ictx->ia - (u8*)page_address(ictx->page));
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci#endif /* NTFS_RW */
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#endif /* _LINUX_NTFS_INDEX_H */
135