fs/ufs/inode.c

Source file repositories/reference/linux-study-clean/fs/ufs/inode.c

File Facts

System
Linux kernel
Corpus path
fs/ufs/inode.c
Extension
.c
Size
33315 bytes
Lines
1223
Domain
Core OS
Bucket
VFS And Filesystem Core
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

struct to_free {
	struct inode *inode;
	u64 to;
	unsigned count;
};

static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
{
	if (ctx->count && ctx->to != from) {
		ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
		ctx->count = 0;
	}
	ctx->count += count;
	ctx->to = from + count;
}

#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)

/*
 * used only for truncation down to direct blocks.
 */
static void ufs_trunc_direct(struct inode *inode)
{
	struct ufs_inode_info *ufsi = UFS_I(inode);
	struct super_block *sb = inode->i_sb;
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
	unsigned int new_frags, old_frags;
	unsigned int old_slot, new_slot;
	unsigned int old_tail, new_tail;
	struct to_free ctx = {.inode = inode};

	UFSD("ENTER: ino %llu\n", inode->i_ino);

	new_frags = DIRECT_FRAGMENT;
	// new_frags = first fragment past the new EOF
	old_frags = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
	// old_frags = first fragment past the old EOF or covered by indirects

	if (new_frags >= old_frags)	 // expanding - nothing to free
		goto done;

	old_tail = ufs_fragnum(old_frags);
	old_slot = ufs_fragstoblks(old_frags);
	new_tail = ufs_fragnum(new_frags);
	new_slot = ufs_fragstoblks(new_frags);

	if (old_slot == new_slot) { // old_tail > 0
		void *p = ufs_get_direct_data_ptr(uspi, ufsi, old_slot);
		u64 tmp = ufs_data_ptr_to_cpu(sb, p);
		if (!tmp)
			ufs_panic(sb, __func__, "internal error");
		if (!new_tail) {
			write_seqlock(&ufsi->meta_lock);
			ufs_data_ptr_clear(uspi, p);
			write_sequnlock(&ufsi->meta_lock);
		}
		ufs_free_fragments(inode, tmp + new_tail, old_tail - new_tail);
	} else {
		unsigned int slot = new_slot;

		if (new_tail) {
			void *p = ufs_get_direct_data_ptr(uspi, ufsi, slot++);
			u64 tmp = ufs_data_ptr_to_cpu(sb, p);
			if (!tmp)
				ufs_panic(sb, __func__, "internal error");

			ufs_free_fragments(inode, tmp + new_tail,
						uspi->s_fpb - new_tail);
		}
		while (slot < old_slot) {
			void *p = ufs_get_direct_data_ptr(uspi, ufsi, slot++);
			u64 tmp = ufs_data_ptr_to_cpu(sb, p);
			if (!tmp)
				continue;
			write_seqlock(&ufsi->meta_lock);
			ufs_data_ptr_clear(uspi, p);
			write_sequnlock(&ufsi->meta_lock);

			free_data(&ctx, tmp, uspi->s_fpb);
		}

		free_data(&ctx, 0, 0);

		if (old_tail) {
			void *p = ufs_get_direct_data_ptr(uspi, ufsi, slot);
			u64 tmp = ufs_data_ptr_to_cpu(sb, p);
			if (!tmp)
				ufs_panic(sb, __func__, "internal error");
			write_seqlock(&ufsi->meta_lock);
			ufs_data_ptr_clear(uspi, p);

Annotation

Implementation Notes