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
12d1154eb460efe588eaed3d439c1caaca149fa362Theodore Ts'o#include "config.h"
1319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdio.h>
144cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
1519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <unistd.h>
164cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
1719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
1819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <time.h>
1919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "ext2fs.h"
2219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This function returns 1 if the inode's block entries actually
2519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * contain block entries.
2619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
270c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'oint ext2fs_inode_has_valid_blocks2(ext2_filsys fs, struct ext2_inode *inode)
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	/*
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * Only directories, regular files, and some symbolic links
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * have valid block entries.
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 */
3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (!LINUX_S_ISDIR(inode->i_mode) && !LINUX_S_ISREG(inode->i_mode) &&
3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	    !LINUX_S_ISLNK(inode->i_mode))
3519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return 0;
36efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	/*
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * If the symbolic link is a "fast symlink", then the symlink
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 * target is stored in the block entries.
4019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	 */
410684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o	if (LINUX_S_ISLNK (inode->i_mode)) {
420c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'o		if (ext2fs_file_acl_block(fs, inode) == 0) {
430684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			/* With no EA block, we can rely on i_blocks */
440684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			if (inode->i_blocks == 0)
450684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o				return 0;
460684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o		} else {
470684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			/* With an EA block, life gets more tricky */
480684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			if (inode->i_size >= EXT2_N_BLOCKS*4)
490684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o				return 1; /* definitely using i_block[] */
50ee504128d34315b579d34fad8e9a3b1f80ffc00eTheodore Ts'o			if (inode->i_size > 4 && inode->i_block[1] == 0)
51ee504128d34315b579d34fad8e9a3b1f80ffc00eTheodore Ts'o				return 1; /* definitely using i_block[] */
520684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o			return 0; /* Probably a fast symlink */
530684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o		}
540684a4f33b5c268fe12f57fcbc77a880c79ab282Theodore Ts'o	}
55716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu
56716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu	/*
57716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu	 * If this inode has inline data, it shouldn't have valid block
58716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu	 * entries.
59716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu	 */
60716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu	if (inode->i_flags & EXT4_INLINE_DATA_FL)
61716a3c3536f87372ce16b5b678b9957dabf6d638Zheng Liu		return 0;
6219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 1;
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
640c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'o
650c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'oint ext2fs_inode_has_valid_blocks(struct ext2_inode *inode)
660c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'o{
670c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'o	return ext2fs_inode_has_valid_blocks2(NULL, inode);
680c80c44bd08c60f3cd0ad87f12a71a75cac3bcaaTheodore Ts'o}
69