blob: d5babc9f222bf38fdfd782fac8fd825b1bae4bf5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/dir.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/dir.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
Mingming Cao617ba132006-10-11 01:20:53 -070016 * ext4 directory handling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -070017 *
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
20 *
21 * Hash Tree Directory indexing (c) 2001 Daniel Phillips
22 *
23 */
24
25#include <linux/fs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026#include <linux/buffer_head.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027#include <linux/slab.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040028#include "ext4.h"
Tao Ma65d165d2012-12-10 14:05:59 -050029#include "xattr.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030
Al Viro725bebb2013-05-17 16:08:53 -040031static int ext4_dx_readdir(struct file *, struct dir_context *);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032
Fan Yongd1f52732012-03-18 22:44:40 -040033/**
34 * Check if the given dir-inode refers to an htree-indexed directory
Anatol Pomozov70261f52013-08-28 14:40:12 -040035 * (or a directory which could potentially get converted to use htree
Fan Yongd1f52732012-03-18 22:44:40 -040036 * indexing).
37 *
38 * Return 1 if it is a dx dir, 0 if not
39 */
40static int is_dx_dir(struct inode *inode)
41{
42 struct super_block *sb = inode->i_sb;
43
Darrick J. Wonge2b911c2015-10-17 16:18:43 -040044 if (ext4_has_feature_dir_index(inode->i_sb) &&
Fan Yongd1f52732012-03-18 22:44:40 -040045 ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
Tao Ma8af0f082013-04-19 17:53:09 -040046 ((inode->i_size >> sb->s_blocksize_bits) == 1) ||
47 ext4_has_inline_data(inode)))
Fan Yongd1f52732012-03-18 22:44:40 -040048 return 1;
49
50 return 0;
51}
52
Theodore Ts'ocad3f002010-12-19 22:07:02 -050053/*
54 * Return 0 if the directory entry is OK, and 1 if there is a problem
55 *
56 * Note: this is the opposite of what ext2 and ext3 historically returned...
Tao Ma65d165d2012-12-10 14:05:59 -050057 *
58 * bh passed here can be an inode block or a dir data block, depending
59 * on the inode inline data flag.
Theodore Ts'ocad3f002010-12-19 22:07:02 -050060 */
Theodore Ts'o60fd4da2010-07-27 11:54:40 -040061int __ext4_check_dir_entry(const char *function, unsigned int line,
Theodore Ts'of7c21172011-01-10 12:10:55 -050062 struct inode *dir, struct file *filp,
Theodore Ts'o60fd4da2010-07-27 11:54:40 -040063 struct ext4_dir_entry_2 *de,
Tao Ma226ba972012-12-10 14:05:58 -050064 struct buffer_head *bh, char *buf, int size,
Theodore Ts'o60fd4da2010-07-27 11:54:40 -040065 unsigned int offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -040067 const char *error_msg = NULL;
Wei Yongjun3d0518f2009-02-14 23:01:36 -050068 const int rlen = ext4_rec_len_from_disk(de->rec_len,
69 dir->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070
Theodore Ts'ocad3f002010-12-19 22:07:02 -050071 if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070072 error_msg = "rec_len is smaller than minimal";
Theodore Ts'ocad3f002010-12-19 22:07:02 -050073 else if (unlikely(rlen % 4 != 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074 error_msg = "rec_len % 4 != 0";
Theodore Ts'ocad3f002010-12-19 22:07:02 -050075 else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 error_msg = "rec_len is too small for name_len";
Tao Ma226ba972012-12-10 14:05:58 -050077 else if (unlikely(((char *) de - buf) + rlen > size))
78 error_msg = "directory entry across range";
Theodore Ts'ocad3f002010-12-19 22:07:02 -050079 else if (unlikely(le32_to_cpu(de->inode) >
80 le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081 error_msg = "inode out of bounds";
Theodore Ts'ocad3f002010-12-19 22:07:02 -050082 else
83 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084
Theodore Ts'of7c21172011-01-10 12:10:55 -050085 if (filp)
Dan Carpenter4fda4002012-02-20 17:53:05 -050086 ext4_error_file(filp, function, line, bh->b_blocknr,
Theodore Ts'of7c21172011-01-10 12:10:55 -050087 "bad entry in directory: %s - offset=%u(%u), "
88 "inode=%u, rec_len=%d, name_len=%d",
Tao Ma226ba972012-12-10 14:05:58 -050089 error_msg, (unsigned) (offset % size),
Theodore Ts'of7c21172011-01-10 12:10:55 -050090 offset, le32_to_cpu(de->inode),
91 rlen, de->name_len);
92 else
Dan Carpenter4fda4002012-02-20 17:53:05 -050093 ext4_error_inode(dir, function, line, bh->b_blocknr,
Theodore Ts'of7c21172011-01-10 12:10:55 -050094 "bad entry in directory: %s - offset=%u(%u), "
95 "inode=%u, rec_len=%d, name_len=%d",
Tao Ma226ba972012-12-10 14:05:58 -050096 error_msg, (unsigned) (offset % size),
Theodore Ts'of7c21172011-01-10 12:10:55 -050097 offset, le32_to_cpu(de->inode),
98 rlen, de->name_len);
99
Theodore Ts'ocad3f002010-12-19 22:07:02 -0500100 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101}
102
Al Viro725bebb2013-05-17 16:08:53 -0400103static int ext4_readdir(struct file *file, struct dir_context *ctx)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500105 unsigned int offset;
Giedrius Rekasiusaa13d5f2014-05-27 12:48:55 -0400106 int i;
Mingming Cao617ba132006-10-11 01:20:53 -0700107 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 int err;
Al Viro725bebb2013-05-17 16:08:53 -0400109 struct inode *inode = file_inode(file);
Fan Yongd1f52732012-03-18 22:44:40 -0400110 struct super_block *sb = inode->i_sb;
Michael Halcrow44614712015-04-12 01:09:05 -0400111 struct buffer_head *bh = NULL;
Eric Sandeen9d9f1772008-10-09 11:15:52 -0400112 int dir_has_error = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400113 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114
Theodore Ts'o28b4c262016-02-07 19:35:05 -0500115 if (ext4_encrypted_inode(inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400116 err = fscrypt_get_encryption_info(inode);
Theodore Ts'o28b4c262016-02-07 19:35:05 -0500117 if (err && err != -ENOKEY)
118 return err;
119 }
120
Fan Yongd1f52732012-03-18 22:44:40 -0400121 if (is_dx_dir(inode)) {
Al Viro725bebb2013-05-17 16:08:53 -0400122 err = ext4_dx_readdir(file, ctx);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 if (err != ERR_BAD_DX_DIR) {
Al Viro725bebb2013-05-17 16:08:53 -0400124 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700125 }
126 /*
127 * We don't set the inode dirty flag since it's not
128 * critical that it get flushed back to the disk.
129 */
Al Viro725bebb2013-05-17 16:08:53 -0400130 ext4_clear_inode_flag(file_inode(file),
Theodore Ts'o07a03822010-06-14 09:54:48 -0400131 EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700132 }
Tao Ma8af0f082013-04-19 17:53:09 -0400133
134 if (ext4_has_inline_data(inode)) {
135 int has_inline_data = 1;
Michael Halcrow44614712015-04-12 01:09:05 -0400136 err = ext4_read_inline_dir(file, ctx,
Tao Ma8af0f082013-04-19 17:53:09 -0400137 &has_inline_data);
138 if (has_inline_data)
Michael Halcrow44614712015-04-12 01:09:05 -0400139 return err;
140 }
141
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400142 if (ext4_encrypted_inode(inode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400143 err = fscrypt_fname_alloc_buffer(inode, EXT4_NAME_LEN, &fstr);
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400144 if (err < 0)
Michael Halcrow44614712015-04-12 01:09:05 -0400145 return err;
Tao Ma8af0f082013-04-19 17:53:09 -0400146 }
147
Al Viro725bebb2013-05-17 16:08:53 -0400148 offset = ctx->pos & (sb->s_blocksize - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149
Al Viro725bebb2013-05-17 16:08:53 -0400150 while (ctx->pos < inode->i_size) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400151 struct ext4_map_blocks map;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400153 if (fatal_signal_pending(current)) {
154 err = -ERESTARTSYS;
155 goto errout;
156 }
157 cond_resched();
Al Viro725bebb2013-05-17 16:08:53 -0400158 map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400159 map.m_len = 1;
160 err = ext4_map_blocks(NULL, inode, &map, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 if (err > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400162 pgoff_t index = map.m_pblk >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300163 (PAGE_SHIFT - inode->i_blkbits);
Al Viro725bebb2013-05-17 16:08:53 -0400164 if (!ra_has_index(&file->f_ra, index))
Rusty Russellcf914a72007-07-19 01:48:08 -0700165 page_cache_sync_readahead(
Fengguang Wudc7868f2007-07-19 01:48:04 -0700166 sb->s_bdev->bd_inode->i_mapping,
Al Viro725bebb2013-05-17 16:08:53 -0400167 &file->f_ra, file,
Rusty Russellcf914a72007-07-19 01:48:08 -0700168 index, 1);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300169 file->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400170 bh = ext4_bread(NULL, inode, map.m_lblk, 0);
Kirill Tkhaic906f382016-02-16 00:20:19 -0500171 if (IS_ERR(bh)) {
172 err = PTR_ERR(bh);
173 bh = NULL;
174 goto errout;
175 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 }
177
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 if (!bh) {
Eric Sandeen9d9f1772008-10-09 11:15:52 -0400179 if (!dir_has_error) {
Al Viro725bebb2013-05-17 16:08:53 -0400180 EXT4_ERROR_FILE(file, 0,
Theodore Ts'of7c21172011-01-10 12:10:55 -0500181 "directory contains a "
182 "hole at offset %llu",
Al Viro725bebb2013-05-17 16:08:53 -0400183 (unsigned long long) ctx->pos);
Eric Sandeen9d9f1772008-10-09 11:15:52 -0400184 dir_has_error = 1;
185 }
Eric Sandeene6c40212006-12-06 20:36:28 -0800186 /* corrupt size? Maybe no more blocks to read */
Al Viro725bebb2013-05-17 16:08:53 -0400187 if (ctx->pos > inode->i_blocks << 9)
Eric Sandeene6c40212006-12-06 20:36:28 -0800188 break;
Al Viro725bebb2013-05-17 16:08:53 -0400189 ctx->pos += sb->s_blocksize - offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 continue;
191 }
192
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400193 /* Check the checksum */
194 if (!buffer_verified(bh) &&
195 !ext4_dirent_csum_verify(inode,
196 (struct ext4_dir_entry *)bh->b_data)) {
Al Viro725bebb2013-05-17 16:08:53 -0400197 EXT4_ERROR_FILE(file, 0, "directory fails checksum "
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400198 "at offset %llu",
Al Viro725bebb2013-05-17 16:08:53 -0400199 (unsigned long long)ctx->pos);
200 ctx->pos += sb->s_blocksize - offset;
Guo Chaod5ac7772013-01-28 21:23:24 -0500201 brelse(bh);
Michael Halcrow44614712015-04-12 01:09:05 -0400202 bh = NULL;
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400203 continue;
204 }
205 set_buffer_verified(bh);
206
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 /* If the dir block has changed since the last call to
208 * readdir(2), then we might be pointing to an invalid
209 * dirent right now. Scan from the start of the block
210 * to make sure. */
Al Viro725bebb2013-05-17 16:08:53 -0400211 if (file->f_version != inode->i_version) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212 for (i = 0; i < sb->s_blocksize && i < offset; ) {
Mingming Cao617ba132006-10-11 01:20:53 -0700213 de = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 (bh->b_data + i);
215 /* It's too expensive to do a full
216 * dirent test each time round this
217 * loop, but we do have to test at
218 * least that it is non-zero. A
219 * failure will be detected in the
220 * dirent test below. */
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500221 if (ext4_rec_len_from_disk(de->rec_len,
222 sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 break;
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500224 i += ext4_rec_len_from_disk(de->rec_len,
225 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 }
227 offset = i;
Al Viro725bebb2013-05-17 16:08:53 -0400228 ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 | offset;
Al Viro725bebb2013-05-17 16:08:53 -0400230 file->f_version = inode->i_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 }
232
Al Viro725bebb2013-05-17 16:08:53 -0400233 while (ctx->pos < inode->i_size
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234 && offset < sb->s_blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -0700235 de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
Al Viro725bebb2013-05-17 16:08:53 -0400236 if (ext4_check_dir_entry(inode, file, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -0500237 bh->b_data, bh->b_size,
238 offset)) {
Andrew Morton63f57932006-10-11 01:21:24 -0700239 /*
Al Viro725bebb2013-05-17 16:08:53 -0400240 * On error, skip to the next block
Andrew Morton63f57932006-10-11 01:21:24 -0700241 */
Al Viro725bebb2013-05-17 16:08:53 -0400242 ctx->pos = (ctx->pos |
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243 (sb->s_blocksize - 1)) + 1;
Al Viro725bebb2013-05-17 16:08:53 -0400244 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500246 offset += ext4_rec_len_from_disk(de->rec_len,
247 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 if (le32_to_cpu(de->inode)) {
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400249 if (!ext4_encrypted_inode(inode)) {
Michael Halcrow44614712015-04-12 01:09:05 -0400250 if (!dir_emit(ctx, de->name,
251 de->name_len,
252 le32_to_cpu(de->inode),
253 get_dtype(sb, de->file_type)))
254 goto done;
255 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400256 int save_len = fstr.len;
257 struct fscrypt_str de_name =
258 FSTR_INIT(de->name,
259 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -0400260
Michael Halcrow44614712015-04-12 01:09:05 -0400261 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400262 err = fscrypt_fname_disk_to_usr(inode,
263 0, 0, &de_name, &fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400264 de_name = fstr;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400265 fstr.len = save_len;
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400266 if (err)
Michael Halcrow44614712015-04-12 01:09:05 -0400267 goto errout;
268 if (!dir_emit(ctx,
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400269 de_name.name, de_name.len,
Michael Halcrow44614712015-04-12 01:09:05 -0400270 le32_to_cpu(de->inode),
271 get_dtype(sb, de->file_type)))
272 goto done;
Al Viro725bebb2013-05-17 16:08:53 -0400273 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 }
Al Viro725bebb2013-05-17 16:08:53 -0400275 ctx->pos += ext4_rec_len_from_disk(de->rec_len,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500276 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 }
Al Viroae053272016-05-12 20:36:01 -0400278 if ((ctx->pos < inode->i_size) && !dir_relax_shared(inode))
Michael Halcrow44614712015-04-12 01:09:05 -0400279 goto done;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400280 brelse(bh);
Michael Halcrow44614712015-04-12 01:09:05 -0400281 bh = NULL;
282 offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 }
Michael Halcrow44614712015-04-12 01:09:05 -0400284done:
285 err = 0;
286errout:
287#ifdef CONFIG_EXT4_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400288 fscrypt_fname_free_buffer(&fstr);
Michael Halcrow44614712015-04-12 01:09:05 -0400289#endif
290 brelse(bh);
291 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292}
293
Fan Yongd1f52732012-03-18 22:44:40 -0400294static inline int is_32bit_api(void)
295{
296#ifdef CONFIG_COMPAT
Andy Lutomirski121cef82016-03-22 14:25:04 -0700297 return in_compat_syscall();
Fan Yongd1f52732012-03-18 22:44:40 -0400298#else
299 return (BITS_PER_LONG == 32);
300#endif
301}
302
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303/*
304 * These functions convert from the major/minor hash to an f_pos
Fan Yongd1f52732012-03-18 22:44:40 -0400305 * value for dx directories
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 *
Fan Yongd1f52732012-03-18 22:44:40 -0400307 * Upper layer (for example NFS) should specify FMODE_32BITHASH or
308 * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
309 * directly on both 32-bit and 64-bit nodes, under such case, neither
310 * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 */
Fan Yongd1f52732012-03-18 22:44:40 -0400312static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
313{
314 if ((filp->f_mode & FMODE_32BITHASH) ||
315 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
316 return major >> 1;
317 else
318 return ((__u64)(major >> 1) << 32) | (__u64)minor;
319}
320
321static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
322{
323 if ((filp->f_mode & FMODE_32BITHASH) ||
324 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
325 return (pos << 1) & 0xffffffff;
326 else
327 return ((pos >> 32) << 1) & 0xffffffff;
328}
329
330static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
331{
332 if ((filp->f_mode & FMODE_32BITHASH) ||
333 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
334 return 0;
335 else
336 return pos & 0xffffffff;
337}
338
339/*
340 * Return 32- or 64-bit end-of-file for dx directories
341 */
342static inline loff_t ext4_get_htree_eof(struct file *filp)
343{
344 if ((filp->f_mode & FMODE_32BITHASH) ||
345 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
346 return EXT4_HTREE_EOF_32BIT;
347 else
348 return EXT4_HTREE_EOF_64BIT;
349}
350
351
352/*
Eric Sandeenec7268c2012-04-30 13:14:03 -0500353 * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
354 * directories, where the "offset" is in terms of the filename hash
355 * value instead of the byte offset.
Fan Yongd1f52732012-03-18 22:44:40 -0400356 *
Eric Sandeenec7268c2012-04-30 13:14:03 -0500357 * Because we may return a 64-bit hash that is well beyond offset limits,
358 * we need to pass the max hash as the maximum allowable offset in
359 * the htree directory case.
360 *
361 * For non-htree, ext4_llseek already chooses the proper max offset.
Fan Yongd1f52732012-03-18 22:44:40 -0400362 */
Zheng Liud4e43952013-03-02 17:24:05 -0500363static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
Fan Yongd1f52732012-03-18 22:44:40 -0400364{
365 struct inode *inode = file->f_mapping->host;
Fan Yongd1f52732012-03-18 22:44:40 -0400366 int dx_dir = is_dx_dir(inode);
Eric Sandeenec7268c2012-04-30 13:14:03 -0500367 loff_t htree_max = ext4_get_htree_eof(file);
Fan Yongd1f52732012-03-18 22:44:40 -0400368
Eric Sandeenec7268c2012-04-30 13:14:03 -0500369 if (likely(dx_dir))
Andrew Morton965c8e52012-12-17 15:59:39 -0800370 return generic_file_llseek_size(file, offset, whence,
Eric Sandeenec7268c2012-04-30 13:14:03 -0500371 htree_max, htree_max);
372 else
Andrew Morton965c8e52012-12-17 15:59:39 -0800373 return ext4_llseek(file, offset, whence);
Fan Yongd1f52732012-03-18 22:44:40 -0400374}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375
376/*
377 * This structure holds the nodes of the red-black tree used to store
378 * the directory entry in hash order.
379 */
380struct fname {
381 __u32 hash;
382 __u32 minor_hash;
383 struct rb_node rb_hash;
384 struct fname *next;
385 __u32 inode;
386 __u8 name_len;
387 __u8 file_type;
388 char name[0];
389};
390
391/*
392 * This functoin implements a non-recursive way of freeing all of the
393 * nodes in the red-black tree.
394 */
395static void free_rb_tree_fname(struct rb_root *root)
396{
Cody P Schaferd1866bd2014-01-23 15:56:10 -0800397 struct fname *fname, *next;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398
Cody P Schaferd1866bd2014-01-23 15:56:10 -0800399 rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 while (fname) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400401 struct fname *old = fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402 fname = fname->next;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400403 kfree(old);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404 }
Cody P Schaferd1866bd2014-01-23 15:56:10 -0800405
406 *root = RB_ROOT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407}
408
409
Fan Yongd1f52732012-03-18 22:44:40 -0400410static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
411 loff_t pos)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412{
413 struct dir_private_info *p;
414
Markus Elfringd695a1b2017-08-24 13:50:24 -0400415 p = kzalloc(sizeof(*p), GFP_KERNEL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700416 if (!p)
417 return NULL;
Fan Yongd1f52732012-03-18 22:44:40 -0400418 p->curr_hash = pos2maj_hash(filp, pos);
419 p->curr_minor_hash = pos2min_hash(filp, pos);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 return p;
421}
422
Mingming Cao617ba132006-10-11 01:20:53 -0700423void ext4_htree_free_dir_info(struct dir_private_info *p)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424{
425 free_rb_tree_fname(&p->root);
426 kfree(p);
427}
428
429/*
430 * Given a directory entry, enter it into the fname rb tree.
Theodore Ts'o2f618302015-04-12 00:56:26 -0400431 *
432 * When filename encryption is enabled, the dirent will hold the
433 * encrypted filename, while the htree will hold decrypted filename.
434 * The decrypted filename is passed in via ent_name. parameter.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 */
Mingming Cao617ba132006-10-11 01:20:53 -0700436int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437 __u32 minor_hash,
Theodore Ts'o2f618302015-04-12 00:56:26 -0400438 struct ext4_dir_entry_2 *dirent,
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400439 struct fscrypt_str *ent_name)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440{
441 struct rb_node **p, *parent = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400442 struct fname *fname, *new_fn;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 struct dir_private_info *info;
444 int len;
445
Joe Perchesa271fe82010-07-27 11:56:04 -0400446 info = dir_file->private_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 p = &info->root.rb_node;
448
449 /* Create and allocate the fname structure */
Theodore Ts'o2f618302015-04-12 00:56:26 -0400450 len = sizeof(struct fname) + ent_name->len + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 new_fn = kzalloc(len, GFP_KERNEL);
452 if (!new_fn)
453 return -ENOMEM;
454 new_fn->hash = hash;
455 new_fn->minor_hash = minor_hash;
456 new_fn->inode = le32_to_cpu(dirent->inode);
Theodore Ts'o2f618302015-04-12 00:56:26 -0400457 new_fn->name_len = ent_name->len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 new_fn->file_type = dirent->file_type;
Theodore Ts'o2f618302015-04-12 00:56:26 -0400459 memcpy(new_fn->name, ent_name->name, ent_name->len);
460 new_fn->name[ent_name->len] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461
462 while (*p) {
463 parent = *p;
464 fname = rb_entry(parent, struct fname, rb_hash);
465
466 /*
467 * If the hash and minor hash match up, then we put
468 * them on a linked list. This rarely happens...
469 */
470 if ((new_fn->hash == fname->hash) &&
471 (new_fn->minor_hash == fname->minor_hash)) {
472 new_fn->next = fname->next;
473 fname->next = new_fn;
474 return 0;
475 }
476
477 if (new_fn->hash < fname->hash)
478 p = &(*p)->rb_left;
479 else if (new_fn->hash > fname->hash)
480 p = &(*p)->rb_right;
481 else if (new_fn->minor_hash < fname->minor_hash)
482 p = &(*p)->rb_left;
483 else /* if (new_fn->minor_hash > fname->minor_hash) */
484 p = &(*p)->rb_right;
485 }
486
487 rb_link_node(&new_fn->rb_hash, parent, p);
488 rb_insert_color(&new_fn->rb_hash, &info->root);
489 return 0;
490}
491
492
493
494/*
Mingming Cao617ba132006-10-11 01:20:53 -0700495 * This is a helper function for ext4_dx_readdir. It calls filldir
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 * for all entres on the fname linked list. (Normally there is only
497 * one entry on the linked list, unless there are 62 bit hash collisions.)
498 */
Al Viro725bebb2013-05-17 16:08:53 -0400499static int call_filldir(struct file *file, struct dir_context *ctx,
500 struct fname *fname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501{
Al Viro725bebb2013-05-17 16:08:53 -0400502 struct dir_private_info *info = file->private_data;
503 struct inode *inode = file_inode(file);
504 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505
506 if (!fname) {
Theodore Ts'o92b97812012-03-19 23:41:49 -0400507 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
508 "called with null fname?!?", __func__, __LINE__,
509 inode->i_ino, current->comm);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 return 0;
511 }
Al Viro725bebb2013-05-17 16:08:53 -0400512 ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513 while (fname) {
Al Viro725bebb2013-05-17 16:08:53 -0400514 if (!dir_emit(ctx, fname->name,
515 fname->name_len,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 fname->inode,
Al Viro725bebb2013-05-17 16:08:53 -0400517 get_dtype(sb, fname->file_type))) {
Theodore Ts'od0156412008-08-19 21:57:43 -0400518 info->extra_fname = fname;
Al Viro725bebb2013-05-17 16:08:53 -0400519 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 }
521 fname = fname->next;
522 }
523 return 0;
524}
525
Al Viro725bebb2013-05-17 16:08:53 -0400526static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527{
Al Viro725bebb2013-05-17 16:08:53 -0400528 struct dir_private_info *info = file->private_data;
529 struct inode *inode = file_inode(file);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530 struct fname *fname;
531 int ret;
532
533 if (!info) {
Al Viro725bebb2013-05-17 16:08:53 -0400534 info = ext4_htree_create_dir_info(file, ctx->pos);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 if (!info)
536 return -ENOMEM;
Al Viro725bebb2013-05-17 16:08:53 -0400537 file->private_data = info;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 }
539
Al Viro725bebb2013-05-17 16:08:53 -0400540 if (ctx->pos == ext4_get_htree_eof(file))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 return 0; /* EOF */
542
543 /* Some one has messed with f_pos; reset the world */
Al Viro725bebb2013-05-17 16:08:53 -0400544 if (info->last_pos != ctx->pos) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 free_rb_tree_fname(&info->root);
546 info->curr_node = NULL;
547 info->extra_fname = NULL;
Al Viro725bebb2013-05-17 16:08:53 -0400548 info->curr_hash = pos2maj_hash(file, ctx->pos);
549 info->curr_minor_hash = pos2min_hash(file, ctx->pos);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 }
551
552 /*
553 * If there are any leftover names on the hash collision
554 * chain, return them first.
555 */
Theodore Ts'od0156412008-08-19 21:57:43 -0400556 if (info->extra_fname) {
Al Viro725bebb2013-05-17 16:08:53 -0400557 if (call_filldir(file, ctx, info->extra_fname))
Theodore Ts'od0156412008-08-19 21:57:43 -0400558 goto finished;
Theodore Ts'od0156412008-08-19 21:57:43 -0400559 info->extra_fname = NULL;
Theodore Ts'o3c37fc82008-10-25 11:39:08 -0400560 goto next_node;
Theodore Ts'od0156412008-08-19 21:57:43 -0400561 } else if (!info->curr_node)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 info->curr_node = rb_first(&info->root);
563
564 while (1) {
565 /*
566 * Fill the rbtree if we have no more entries,
567 * or the inode has changed since we last read in the
568 * cached entries.
569 */
570 if ((!info->curr_node) ||
Al Viro725bebb2013-05-17 16:08:53 -0400571 (file->f_version != inode->i_version)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 info->curr_node = NULL;
573 free_rb_tree_fname(&info->root);
Al Viro725bebb2013-05-17 16:08:53 -0400574 file->f_version = inode->i_version;
575 ret = ext4_htree_fill_tree(file, info->curr_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 info->curr_minor_hash,
577 &info->next_hash);
578 if (ret < 0)
579 return ret;
580 if (ret == 0) {
Al Viro725bebb2013-05-17 16:08:53 -0400581 ctx->pos = ext4_get_htree_eof(file);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 break;
583 }
584 info->curr_node = rb_first(&info->root);
585 }
586
587 fname = rb_entry(info->curr_node, struct fname, rb_hash);
588 info->curr_hash = fname->hash;
589 info->curr_minor_hash = fname->minor_hash;
Al Viro725bebb2013-05-17 16:08:53 -0400590 if (call_filldir(file, ctx, fname))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 break;
Theodore Ts'o3c37fc82008-10-25 11:39:08 -0400592 next_node:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 info->curr_node = rb_next(info->curr_node);
Theodore Ts'o3c37fc82008-10-25 11:39:08 -0400594 if (info->curr_node) {
595 fname = rb_entry(info->curr_node, struct fname,
596 rb_hash);
597 info->curr_hash = fname->hash;
598 info->curr_minor_hash = fname->minor_hash;
599 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 if (info->next_hash == ~0) {
Al Viro725bebb2013-05-17 16:08:53 -0400601 ctx->pos = ext4_get_htree_eof(file);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700602 break;
603 }
604 info->curr_hash = info->next_hash;
605 info->curr_minor_hash = 0;
606 }
607 }
608finished:
Al Viro725bebb2013-05-17 16:08:53 -0400609 info->last_pos = ctx->pos;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 return 0;
611}
612
Theodore Ts'o6bc445e2015-05-31 13:34:57 -0400613static int ext4_dir_open(struct inode * inode, struct file * filp)
614{
615 if (ext4_encrypted_inode(inode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400616 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Theodore Ts'o6bc445e2015-05-31 13:34:57 -0400617 return 0;
618}
619
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400620static int ext4_release_dir(struct inode *inode, struct file *filp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621{
Andrew Morton63f57932006-10-11 01:21:24 -0700622 if (filp->private_data)
Mingming Cao617ba132006-10-11 01:20:53 -0700623 ext4_htree_free_dir_info(filp->private_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624
625 return 0;
626}
Fan Yongd1f52732012-03-18 22:44:40 -0400627
Darrick J. Wong40b163f2014-07-28 13:06:26 -0400628int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
629 int buf_size)
630{
631 struct ext4_dir_entry_2 *de;
Eric Engestrom18017472016-09-30 02:14:56 -0400632 int rlen;
Darrick J. Wong40b163f2014-07-28 13:06:26 -0400633 unsigned int offset = 0;
634 char *top;
635
636 de = (struct ext4_dir_entry_2 *)buf;
637 top = buf + buf_size;
638 while ((char *) de < top) {
639 if (ext4_check_dir_entry(dir, NULL, de, bh,
640 buf, buf_size, offset))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400641 return -EFSCORRUPTED;
Darrick J. Wong40b163f2014-07-28 13:06:26 -0400642 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
643 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
644 offset += rlen;
645 }
646 if ((char *) de > top)
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400647 return -EFSCORRUPTED;
Darrick J. Wong40b163f2014-07-28 13:06:26 -0400648
649 return 0;
650}
651
Fan Yongd1f52732012-03-18 22:44:40 -0400652const struct file_operations ext4_dir_operations = {
653 .llseek = ext4_dir_llseek,
654 .read = generic_read_dir,
Al Viroae053272016-05-12 20:36:01 -0400655 .iterate_shared = ext4_readdir,
Fan Yongd1f52732012-03-18 22:44:40 -0400656 .unlocked_ioctl = ext4_ioctl,
657#ifdef CONFIG_COMPAT
658 .compat_ioctl = ext4_compat_ioctl,
659#endif
660 .fsync = ext4_sync_file,
Theodore Ts'o6bc445e2015-05-31 13:34:57 -0400661 .open = ext4_dir_open,
Fan Yongd1f52732012-03-18 22:44:40 -0400662 .release = ext4_release_dir,
663};