119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * valid_blk.c --- does the inode have valid blocks?
319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright 1997 by Theodore Ts'o
5efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
7543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * This file may be redistributed under the terms of the GNU Library
8543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * General Public License, version 2.
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
1019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
1119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
1219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdio.h>
134cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
1419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <unistd.h>
154cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
1619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
1719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <time.h>
1819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
19b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
2019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "ext2fs.h"
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This function returns 1 if the inode's block entries actually
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * contain block entries.
2519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
26e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallint ext2fs_inode_has_valid_blocks2(ext2_filsys fs, struct ext2_inode *inode)
2719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	/*
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * Only directories, regular files, and some symbolic links
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * have valid block entries.
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 */
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	    !LINUX_S_ISLNK(inode->i_mode))
3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return 0;
35efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
3619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	/*
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * If the symbolic link is a "fast symlink", then the symlink
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * target is stored in the block entries.
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 */
400684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o	if (LINUX_S_ISLNK (inode->i_mode)) {
41e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		if (ext2fs_file_acl_block(fs, inode) == 0) {
420684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			/* With no EA block, we can rely on i_blocks */
430684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			if (inode->i_blocks == 0)
440684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o				return 0;
450684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o		} else {
460684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			/* With an EA block, life gets more tricky */
470684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			if (inode->i_size >= EXT2_N_BLOCKS*4)
480684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o				return 1; /* definitely using i_block[] */
49ee504128d34315b579d34fad8e9a3b1f80ffc00eTheodore Ts'o			if (inode->i_size > 4 && inode->i_block[1] == 0)
50ee504128d34315b579d34fad8e9a3b1f80ffc00eTheodore Ts'o				return 1; /* definitely using i_block[] */
510684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			return 0; /* Probably a fast symlink */
520684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o		}
530684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o	}
5419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 1;
5519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
56e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
57e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallint ext2fs_inode_has_valid_blocks(struct ext2_inode *inode)
58e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
59e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return ext2fs_inode_has_valid_blocks2(NULL, inode);
60e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
61