1 /*
2 * linux/fs/ext4/dir.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/dir.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * ext4 directory handling functions
16 *
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 *
20 * Hash Tree Directory indexing (c) 2001 Daniel Phillips
21 *
22 */
23
24 #include <linux/fs.h>
25 #include <linux/buffer_head.h>
26 #include <linux/slab.h>
27 #include "ext4.h"
28 #include "xattr.h"
29
30 static int ext4_dx_readdir(struct file *, struct dir_context *);
31
32 /**
33 * Check if the given dir-inode refers to an htree-indexed directory
34 * (or a directory which could potentially get converted to use htree
35 * indexing).
36 *
37 * Return 1 if it is a dx dir, 0 if not
38 */
is_dx_dir(struct inode * inode)39 static int is_dx_dir(struct inode *inode)
40 {
41 struct super_block *sb = inode->i_sb;
42
43 if (ext4_has_feature_dir_index(inode->i_sb) &&
44 ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
45 ((inode->i_size >> sb->s_blocksize_bits) == 1) ||
46 ext4_has_inline_data(inode)))
47 return 1;
48
49 return 0;
50 }
51
52 /*
53 * Return 0 if the directory entry is OK, and 1 if there is a problem
54 *
55 * Note: this is the opposite of what ext2 and ext3 historically returned...
56 *
57 * bh passed here can be an inode block or a dir data block, depending
58 * on the inode inline data flag.
59 */
__ext4_check_dir_entry(const char * function,unsigned int line,struct inode * dir,struct file * filp,struct ext4_dir_entry_2 * de,struct buffer_head * bh,char * buf,int size,unsigned int offset)60 int __ext4_check_dir_entry(const char *function, unsigned int line,
61 struct inode *dir, struct file *filp,
62 struct ext4_dir_entry_2 *de,
63 struct buffer_head *bh, char *buf, int size,
64 unsigned int offset)
65 {
66 const char *error_msg = NULL;
67 const int rlen = ext4_rec_len_from_disk(de->rec_len,
68 dir->i_sb->s_blocksize);
69
70 if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
71 error_msg = "rec_len is smaller than minimal";
72 else if (unlikely(rlen % 4 != 0))
73 error_msg = "rec_len % 4 != 0";
74 else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
75 error_msg = "rec_len is too small for name_len";
76 else if (unlikely(((char *) de - buf) + rlen > size))
77 error_msg = "directory entry overrun";
78 else if (unlikely(((char *) de - buf) + rlen >
79 size - EXT4_DIR_REC_LEN(1) &&
80 ((char *) de - buf) + rlen != size)) {
81 error_msg = "directory entry too close to block end";
82 }
83 else if (unlikely(le32_to_cpu(de->inode) >
84 le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
85 error_msg = "inode out of bounds";
86 else
87 return 0;
88
89 if (filp)
90 ext4_error_file(filp, function, line, bh->b_blocknr,
91 "bad entry in directory: %s - offset=%u, "
92 "inode=%u, rec_len=%d, name_len=%d, size=%d",
93 error_msg, offset, le32_to_cpu(de->inode),
94 rlen, de->name_len, size);
95 else
96 ext4_error_inode(dir, function, line, bh->b_blocknr,
97 "bad entry in directory: %s - offset=%u, "
98 "inode=%u, rec_len=%d, name_len=%d, size=%d",
99 error_msg, offset, le32_to_cpu(de->inode),
100 rlen, de->name_len, size);
101
102 return 1;
103 }
104
ext4_readdir(struct file * file,struct dir_context * ctx)105 static int ext4_readdir(struct file *file, struct dir_context *ctx)
106 {
107 unsigned int offset;
108 int i;
109 struct ext4_dir_entry_2 *de;
110 int err;
111 struct inode *inode = file_inode(file);
112 struct super_block *sb = inode->i_sb;
113 struct buffer_head *bh = NULL;
114 int dir_has_error = 0;
115 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
116
117 if (ext4_encrypted_inode(inode)) {
118 err = ext4_get_encryption_info(inode);
119 if (err && err != -ENOKEY)
120 return err;
121 }
122
123 if (is_dx_dir(inode)) {
124 err = ext4_dx_readdir(file, ctx);
125 if (err != ERR_BAD_DX_DIR) {
126 return err;
127 }
128 /* Can we just clear INDEX flag to ignore htree information? */
129 if (!ext4_has_metadata_csum(sb)) {
130 /*
131 * We don't set the inode dirty flag since it's not
132 * critical that it gets flushed back to the disk.
133 */
134 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
135 }
136 }
137
138 if (ext4_has_inline_data(inode)) {
139 int has_inline_data = 1;
140 err = ext4_read_inline_dir(file, ctx,
141 &has_inline_data);
142 if (has_inline_data)
143 return err;
144 }
145
146 if (ext4_encrypted_inode(inode)) {
147 err = ext4_fname_crypto_alloc_buffer(inode, EXT4_NAME_LEN,
148 &fname_crypto_str);
149 if (err < 0)
150 return err;
151 }
152
153 offset = ctx->pos & (sb->s_blocksize - 1);
154
155 while (ctx->pos < inode->i_size) {
156 struct ext4_map_blocks map;
157
158 map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
159 map.m_len = 1;
160 err = ext4_map_blocks(NULL, inode, &map, 0);
161 if (err > 0) {
162 pgoff_t index = map.m_pblk >>
163 (PAGE_CACHE_SHIFT - inode->i_blkbits);
164 if (!ra_has_index(&file->f_ra, index))
165 page_cache_sync_readahead(
166 sb->s_bdev->bd_inode->i_mapping,
167 &file->f_ra, file,
168 index, 1);
169 file->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
170 bh = ext4_bread(NULL, inode, map.m_lblk, 0);
171 if (IS_ERR(bh))
172 return PTR_ERR(bh);
173 }
174
175 if (!bh) {
176 if (!dir_has_error) {
177 EXT4_ERROR_FILE(file, 0,
178 "directory contains a "
179 "hole at offset %llu",
180 (unsigned long long) ctx->pos);
181 dir_has_error = 1;
182 }
183 /* corrupt size? Maybe no more blocks to read */
184 if (ctx->pos > inode->i_blocks << 9)
185 break;
186 ctx->pos += sb->s_blocksize - offset;
187 continue;
188 }
189
190 /* Check the checksum */
191 if (!buffer_verified(bh) &&
192 !ext4_dirent_csum_verify(inode,
193 (struct ext4_dir_entry *)bh->b_data)) {
194 EXT4_ERROR_FILE(file, 0, "directory fails checksum "
195 "at offset %llu",
196 (unsigned long long)ctx->pos);
197 ctx->pos += sb->s_blocksize - offset;
198 brelse(bh);
199 bh = NULL;
200 continue;
201 }
202 set_buffer_verified(bh);
203
204 /* If the dir block has changed since the last call to
205 * readdir(2), then we might be pointing to an invalid
206 * dirent right now. Scan from the start of the block
207 * to make sure. */
208 if (file->f_version != inode->i_version) {
209 for (i = 0; i < sb->s_blocksize && i < offset; ) {
210 de = (struct ext4_dir_entry_2 *)
211 (bh->b_data + i);
212 /* It's too expensive to do a full
213 * dirent test each time round this
214 * loop, but we do have to test at
215 * least that it is non-zero. A
216 * failure will be detected in the
217 * dirent test below. */
218 if (ext4_rec_len_from_disk(de->rec_len,
219 sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
220 break;
221 i += ext4_rec_len_from_disk(de->rec_len,
222 sb->s_blocksize);
223 }
224 offset = i;
225 ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
226 | offset;
227 file->f_version = inode->i_version;
228 }
229
230 while (ctx->pos < inode->i_size
231 && offset < sb->s_blocksize) {
232 de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
233 if (ext4_check_dir_entry(inode, file, de, bh,
234 bh->b_data, bh->b_size,
235 offset)) {
236 /*
237 * On error, skip to the next block
238 */
239 ctx->pos = (ctx->pos |
240 (sb->s_blocksize - 1)) + 1;
241 break;
242 }
243 offset += ext4_rec_len_from_disk(de->rec_len,
244 sb->s_blocksize);
245 if (le32_to_cpu(de->inode)) {
246 if (!ext4_encrypted_inode(inode)) {
247 if (!dir_emit(ctx, de->name,
248 de->name_len,
249 le32_to_cpu(de->inode),
250 get_dtype(sb, de->file_type)))
251 goto done;
252 } else {
253 int save_len = fname_crypto_str.len;
254
255 /* Directory is encrypted */
256 err = ext4_fname_disk_to_usr(inode,
257 NULL, de, &fname_crypto_str);
258 fname_crypto_str.len = save_len;
259 if (err < 0)
260 goto errout;
261 if (!dir_emit(ctx,
262 fname_crypto_str.name, err,
263 le32_to_cpu(de->inode),
264 get_dtype(sb, de->file_type)))
265 goto done;
266 }
267 }
268 ctx->pos += ext4_rec_len_from_disk(de->rec_len,
269 sb->s_blocksize);
270 }
271 if ((ctx->pos < inode->i_size) && !dir_relax(inode))
272 goto done;
273 brelse(bh);
274 bh = NULL;
275 offset = 0;
276 }
277 done:
278 err = 0;
279 errout:
280 #ifdef CONFIG_EXT4_FS_ENCRYPTION
281 ext4_fname_crypto_free_buffer(&fname_crypto_str);
282 #endif
283 brelse(bh);
284 return err;
285 }
286
is_32bit_api(void)287 static inline int is_32bit_api(void)
288 {
289 #ifdef CONFIG_COMPAT
290 return is_compat_task();
291 #else
292 return (BITS_PER_LONG == 32);
293 #endif
294 }
295
296 /*
297 * These functions convert from the major/minor hash to an f_pos
298 * value for dx directories
299 *
300 * Upper layer (for example NFS) should specify FMODE_32BITHASH or
301 * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
302 * directly on both 32-bit and 64-bit nodes, under such case, neither
303 * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
304 */
hash2pos(struct file * filp,__u32 major,__u32 minor)305 static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
306 {
307 if ((filp->f_mode & FMODE_32BITHASH) ||
308 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
309 return major >> 1;
310 else
311 return ((__u64)(major >> 1) << 32) | (__u64)minor;
312 }
313
pos2maj_hash(struct file * filp,loff_t pos)314 static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
315 {
316 if ((filp->f_mode & FMODE_32BITHASH) ||
317 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
318 return (pos << 1) & 0xffffffff;
319 else
320 return ((pos >> 32) << 1) & 0xffffffff;
321 }
322
pos2min_hash(struct file * filp,loff_t pos)323 static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
324 {
325 if ((filp->f_mode & FMODE_32BITHASH) ||
326 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
327 return 0;
328 else
329 return pos & 0xffffffff;
330 }
331
332 /*
333 * Return 32- or 64-bit end-of-file for dx directories
334 */
ext4_get_htree_eof(struct file * filp)335 static inline loff_t ext4_get_htree_eof(struct file *filp)
336 {
337 if ((filp->f_mode & FMODE_32BITHASH) ||
338 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
339 return EXT4_HTREE_EOF_32BIT;
340 else
341 return EXT4_HTREE_EOF_64BIT;
342 }
343
344
345 /*
346 * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
347 * directories, where the "offset" is in terms of the filename hash
348 * value instead of the byte offset.
349 *
350 * Because we may return a 64-bit hash that is well beyond offset limits,
351 * we need to pass the max hash as the maximum allowable offset in
352 * the htree directory case.
353 *
354 * For non-htree, ext4_llseek already chooses the proper max offset.
355 */
ext4_dir_llseek(struct file * file,loff_t offset,int whence)356 static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
357 {
358 struct inode *inode = file->f_mapping->host;
359 int dx_dir = is_dx_dir(inode);
360 loff_t htree_max = ext4_get_htree_eof(file);
361
362 if (likely(dx_dir))
363 return generic_file_llseek_size(file, offset, whence,
364 htree_max, htree_max);
365 else
366 return ext4_llseek(file, offset, whence);
367 }
368
369 /*
370 * This structure holds the nodes of the red-black tree used to store
371 * the directory entry in hash order.
372 */
373 struct fname {
374 __u32 hash;
375 __u32 minor_hash;
376 struct rb_node rb_hash;
377 struct fname *next;
378 __u32 inode;
379 __u8 name_len;
380 __u8 file_type;
381 char name[0];
382 };
383
384 /*
385 * This functoin implements a non-recursive way of freeing all of the
386 * nodes in the red-black tree.
387 */
free_rb_tree_fname(struct rb_root * root)388 static void free_rb_tree_fname(struct rb_root *root)
389 {
390 struct fname *fname, *next;
391
392 rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
393 while (fname) {
394 struct fname *old = fname;
395 fname = fname->next;
396 kfree(old);
397 }
398
399 *root = RB_ROOT;
400 }
401
402
ext4_htree_create_dir_info(struct file * filp,loff_t pos)403 static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
404 loff_t pos)
405 {
406 struct dir_private_info *p;
407
408 p = kzalloc(sizeof(struct dir_private_info), GFP_KERNEL);
409 if (!p)
410 return NULL;
411 p->curr_hash = pos2maj_hash(filp, pos);
412 p->curr_minor_hash = pos2min_hash(filp, pos);
413 return p;
414 }
415
ext4_htree_free_dir_info(struct dir_private_info * p)416 void ext4_htree_free_dir_info(struct dir_private_info *p)
417 {
418 free_rb_tree_fname(&p->root);
419 kfree(p);
420 }
421
422 /*
423 * Given a directory entry, enter it into the fname rb tree.
424 *
425 * When filename encryption is enabled, the dirent will hold the
426 * encrypted filename, while the htree will hold decrypted filename.
427 * The decrypted filename is passed in via ent_name. parameter.
428 */
ext4_htree_store_dirent(struct file * dir_file,__u32 hash,__u32 minor_hash,struct ext4_dir_entry_2 * dirent,struct ext4_str * ent_name)429 int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
430 __u32 minor_hash,
431 struct ext4_dir_entry_2 *dirent,
432 struct ext4_str *ent_name)
433 {
434 struct rb_node **p, *parent = NULL;
435 struct fname *fname, *new_fn;
436 struct dir_private_info *info;
437 int len;
438
439 info = dir_file->private_data;
440 p = &info->root.rb_node;
441
442 /* Create and allocate the fname structure */
443 len = sizeof(struct fname) + ent_name->len + 1;
444 new_fn = kzalloc(len, GFP_KERNEL);
445 if (!new_fn)
446 return -ENOMEM;
447 new_fn->hash = hash;
448 new_fn->minor_hash = minor_hash;
449 new_fn->inode = le32_to_cpu(dirent->inode);
450 new_fn->name_len = ent_name->len;
451 new_fn->file_type = dirent->file_type;
452 memcpy(new_fn->name, ent_name->name, ent_name->len);
453 new_fn->name[ent_name->len] = 0;
454
455 while (*p) {
456 parent = *p;
457 fname = rb_entry(parent, struct fname, rb_hash);
458
459 /*
460 * If the hash and minor hash match up, then we put
461 * them on a linked list. This rarely happens...
462 */
463 if ((new_fn->hash == fname->hash) &&
464 (new_fn->minor_hash == fname->minor_hash)) {
465 new_fn->next = fname->next;
466 fname->next = new_fn;
467 return 0;
468 }
469
470 if (new_fn->hash < fname->hash)
471 p = &(*p)->rb_left;
472 else if (new_fn->hash > fname->hash)
473 p = &(*p)->rb_right;
474 else if (new_fn->minor_hash < fname->minor_hash)
475 p = &(*p)->rb_left;
476 else /* if (new_fn->minor_hash > fname->minor_hash) */
477 p = &(*p)->rb_right;
478 }
479
480 rb_link_node(&new_fn->rb_hash, parent, p);
481 rb_insert_color(&new_fn->rb_hash, &info->root);
482 return 0;
483 }
484
485
486
487 /*
488 * This is a helper function for ext4_dx_readdir. It calls filldir
489 * for all entres on the fname linked list. (Normally there is only
490 * one entry on the linked list, unless there are 62 bit hash collisions.)
491 */
call_filldir(struct file * file,struct dir_context * ctx,struct fname * fname)492 static int call_filldir(struct file *file, struct dir_context *ctx,
493 struct fname *fname)
494 {
495 struct dir_private_info *info = file->private_data;
496 struct inode *inode = file_inode(file);
497 struct super_block *sb = inode->i_sb;
498
499 if (!fname) {
500 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
501 "called with null fname?!?", __func__, __LINE__,
502 inode->i_ino, current->comm);
503 return 0;
504 }
505 ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
506 while (fname) {
507 if (!dir_emit(ctx, fname->name,
508 fname->name_len,
509 fname->inode,
510 get_dtype(sb, fname->file_type))) {
511 info->extra_fname = fname;
512 return 1;
513 }
514 fname = fname->next;
515 }
516 return 0;
517 }
518
ext4_dx_readdir(struct file * file,struct dir_context * ctx)519 static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
520 {
521 struct dir_private_info *info = file->private_data;
522 struct inode *inode = file_inode(file);
523 struct fname *fname;
524 int ret = 0;
525
526 if (!info) {
527 info = ext4_htree_create_dir_info(file, ctx->pos);
528 if (!info)
529 return -ENOMEM;
530 file->private_data = info;
531 }
532
533 if (ctx->pos == ext4_get_htree_eof(file))
534 return 0; /* EOF */
535
536 /* Some one has messed with f_pos; reset the world */
537 if (info->last_pos != ctx->pos) {
538 free_rb_tree_fname(&info->root);
539 info->curr_node = NULL;
540 info->extra_fname = NULL;
541 info->curr_hash = pos2maj_hash(file, ctx->pos);
542 info->curr_minor_hash = pos2min_hash(file, ctx->pos);
543 }
544
545 /*
546 * If there are any leftover names on the hash collision
547 * chain, return them first.
548 */
549 if (info->extra_fname) {
550 if (call_filldir(file, ctx, info->extra_fname))
551 goto finished;
552 info->extra_fname = NULL;
553 goto next_node;
554 } else if (!info->curr_node)
555 info->curr_node = rb_first(&info->root);
556
557 while (1) {
558 /*
559 * Fill the rbtree if we have no more entries,
560 * or the inode has changed since we last read in the
561 * cached entries.
562 */
563 if ((!info->curr_node) ||
564 (file->f_version != inode->i_version)) {
565 info->curr_node = NULL;
566 free_rb_tree_fname(&info->root);
567 file->f_version = inode->i_version;
568 ret = ext4_htree_fill_tree(file, info->curr_hash,
569 info->curr_minor_hash,
570 &info->next_hash);
571 if (ret < 0)
572 goto finished;
573 if (ret == 0) {
574 ctx->pos = ext4_get_htree_eof(file);
575 break;
576 }
577 info->curr_node = rb_first(&info->root);
578 }
579
580 fname = rb_entry(info->curr_node, struct fname, rb_hash);
581 info->curr_hash = fname->hash;
582 info->curr_minor_hash = fname->minor_hash;
583 if (call_filldir(file, ctx, fname))
584 break;
585 next_node:
586 info->curr_node = rb_next(info->curr_node);
587 if (info->curr_node) {
588 fname = rb_entry(info->curr_node, struct fname,
589 rb_hash);
590 info->curr_hash = fname->hash;
591 info->curr_minor_hash = fname->minor_hash;
592 } else {
593 if (info->next_hash == ~0) {
594 ctx->pos = ext4_get_htree_eof(file);
595 break;
596 }
597 info->curr_hash = info->next_hash;
598 info->curr_minor_hash = 0;
599 }
600 }
601 finished:
602 info->last_pos = ctx->pos;
603 return ret < 0 ? ret : 0;
604 }
605
ext4_dir_open(struct inode * inode,struct file * filp)606 static int ext4_dir_open(struct inode * inode, struct file * filp)
607 {
608 if (ext4_encrypted_inode(inode))
609 return ext4_get_encryption_info(inode) ? -EACCES : 0;
610 return 0;
611 }
612
ext4_release_dir(struct inode * inode,struct file * filp)613 static int ext4_release_dir(struct inode *inode, struct file *filp)
614 {
615 if (filp->private_data)
616 ext4_htree_free_dir_info(filp->private_data);
617
618 return 0;
619 }
620
ext4_check_all_de(struct inode * dir,struct buffer_head * bh,void * buf,int buf_size)621 int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
622 int buf_size)
623 {
624 struct ext4_dir_entry_2 *de;
625 int nlen, rlen;
626 unsigned int offset = 0;
627 char *top;
628
629 de = (struct ext4_dir_entry_2 *)buf;
630 top = buf + buf_size;
631 while ((char *) de < top) {
632 if (ext4_check_dir_entry(dir, NULL, de, bh,
633 buf, buf_size, offset))
634 return -EFSCORRUPTED;
635 nlen = EXT4_DIR_REC_LEN(de->name_len);
636 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
637 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
638 offset += rlen;
639 }
640 if ((char *) de > top)
641 return -EFSCORRUPTED;
642
643 return 0;
644 }
645
646 const struct file_operations ext4_dir_operations = {
647 .llseek = ext4_dir_llseek,
648 .read = generic_read_dir,
649 .iterate = ext4_readdir,
650 .unlocked_ioctl = ext4_ioctl,
651 #ifdef CONFIG_COMPAT
652 .compat_ioctl = ext4_compat_ioctl,
653 #endif
654 .fsync = ext4_sync_file,
655 .open = ext4_dir_open,
656 .release = ext4_release_dir,
657 };
658