Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 5 | */ |
| 6 | #include "internal.h" |
| 7 | |
Gao Xiang | 33bac91 | 2019-03-29 04:14:58 +0800 | [diff] [blame] | 8 | static void debug_one_dentry(unsigned char d_type, const char *de_name, |
| 9 | unsigned int de_namelen) |
| 10 | { |
| 11 | #ifdef CONFIG_EROFS_FS_DEBUG |
| 12 | /* since the on-disk name could not have the trailing '\0' */ |
| 13 | unsigned char dbg_namebuf[EROFS_NAME_LEN + 1]; |
| 14 | |
| 15 | memcpy(dbg_namebuf, de_name, de_namelen); |
| 16 | dbg_namebuf[de_namelen] = '\0'; |
| 17 | |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 18 | erofs_dbg("found dirent %s de_len %u d_type %d", dbg_namebuf, |
| 19 | de_namelen, d_type); |
Gao Xiang | 33bac91 | 2019-03-29 04:14:58 +0800 | [diff] [blame] | 20 | #endif |
| 21 | } |
| 22 | |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 23 | static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx, |
Bhanusree Pola | cbebe5d | 2019-02-19 14:55:25 +0530 | [diff] [blame] | 24 | void *dentry_blk, unsigned int *ofs, |
| 25 | unsigned int nameoff, unsigned int maxsize) |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 26 | { |
Colin Ian King | 5c8004c | 2019-06-17 13:55:29 +0100 | [diff] [blame] | 27 | struct erofs_dirent *de = dentry_blk + *ofs; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 28 | const struct erofs_dirent *end = dentry_blk + nameoff; |
| 29 | |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 30 | while (de < end) { |
| 31 | const char *de_name; |
Gao Xiang | 33bac91 | 2019-03-29 04:14:58 +0800 | [diff] [blame] | 32 | unsigned int de_namelen; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 33 | unsigned char d_type; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 34 | |
Gao Xiang | 1d819c5 | 2019-08-16 15:11:42 +0800 | [diff] [blame] | 35 | d_type = fs_ftype_to_dtype(de->file_type); |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 36 | |
| 37 | nameoff = le16_to_cpu(de->nameoff); |
| 38 | de_name = (char *)dentry_blk + nameoff; |
| 39 | |
Gao Xiang | 33bac91 | 2019-03-29 04:14:58 +0800 | [diff] [blame] | 40 | /* the last dirent in the block? */ |
| 41 | if (de + 1 >= end) |
| 42 | de_namelen = strnlen(de_name, maxsize - nameoff); |
| 43 | else |
| 44 | de_namelen = le16_to_cpu(de[1].nameoff) - nameoff; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 45 | |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 46 | /* a corrupted entry is found */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 47 | if (nameoff + de_namelen > maxsize || |
| 48 | de_namelen > EROFS_NAME_LEN) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 49 | erofs_err(dir->i_sb, "bogus dirent @ nid %llu", |
| 50 | EROFS_I(dir)->nid); |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 51 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 52 | return -EFSCORRUPTED; |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 53 | } |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 54 | |
Gao Xiang | 33bac91 | 2019-03-29 04:14:58 +0800 | [diff] [blame] | 55 | debug_one_dentry(d_type, de_name, de_namelen); |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 56 | if (!dir_emit(ctx, de_name, de_namelen, |
Aaron Strahlberger | 019ec6c | 2018-12-11 11:54:41 +0100 | [diff] [blame] | 57 | le64_to_cpu(de->nid), d_type)) |
| 58 | /* stopped by some reason */ |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 59 | return 1; |
| 60 | ++de; |
| 61 | *ofs += sizeof(struct erofs_dirent); |
| 62 | } |
| 63 | *ofs = maxsize; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int erofs_readdir(struct file *f, struct dir_context *ctx) |
| 68 | { |
| 69 | struct inode *dir = file_inode(f); |
| 70 | struct address_space *mapping = dir->i_mapping; |
| 71 | const size_t dirsize = i_size_read(dir); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 72 | unsigned int i = ctx->pos / EROFS_BLKSIZ; |
| 73 | unsigned int ofs = ctx->pos % EROFS_BLKSIZ; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 74 | int err = 0; |
| 75 | bool initial = true; |
| 76 | |
| 77 | while (ctx->pos < dirsize) { |
| 78 | struct page *dentry_page; |
| 79 | struct erofs_dirent *de; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 80 | unsigned int nameoff, maxsize; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 81 | |
| 82 | dentry_page = read_mapping_page(mapping, i, NULL); |
Gao Xiang | acb383f | 2019-08-18 20:54:57 +0800 | [diff] [blame] | 83 | if (dentry_page == ERR_PTR(-ENOMEM)) { |
| 84 | err = -ENOMEM; |
| 85 | break; |
| 86 | } else if (IS_ERR(dentry_page)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 87 | erofs_err(dir->i_sb, |
| 88 | "fail to readdir of logical block %u of nid %llu", |
| 89 | i, EROFS_I(dir)->nid); |
Gao Xiang | acb383f | 2019-08-18 20:54:57 +0800 | [diff] [blame] | 90 | err = -EFSCORRUPTED; |
| 91 | break; |
| 92 | } |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 93 | |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 94 | de = (struct erofs_dirent *)kmap(dentry_page); |
| 95 | |
| 96 | nameoff = le16_to_cpu(de->nameoff); |
| 97 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 98 | if (nameoff < sizeof(struct erofs_dirent) || |
| 99 | nameoff >= PAGE_SIZE) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 100 | erofs_err(dir->i_sb, |
| 101 | "invalid de[0].nameoff %u @ nid %llu", |
| 102 | nameoff, EROFS_I(dir)->nid); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 103 | err = -EFSCORRUPTED; |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 104 | goto skip_this; |
| 105 | } |
| 106 | |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 107 | maxsize = min_t(unsigned int, |
| 108 | dirsize - ctx->pos + ofs, PAGE_SIZE); |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 109 | |
| 110 | /* search dirents at the arbitrary position */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 111 | if (initial) { |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 112 | initial = false; |
| 113 | |
| 114 | ofs = roundup(ofs, sizeof(struct erofs_dirent)); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 115 | if (ofs >= nameoff) |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 116 | goto skip_this; |
| 117 | } |
| 118 | |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 119 | err = erofs_fill_dentries(dir, ctx, de, &ofs, |
| 120 | nameoff, maxsize); |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 121 | skip_this: |
| 122 | kunmap(dentry_page); |
| 123 | |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 124 | put_page(dentry_page); |
| 125 | |
| 126 | ctx->pos = blknr_to_addr(i) + ofs; |
| 127 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 128 | if (err) |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 129 | break; |
| 130 | ++i; |
| 131 | ofs = 0; |
| 132 | } |
| 133 | return err < 0 ? err : 0; |
| 134 | } |
| 135 | |
| 136 | const struct file_operations erofs_dir_fops = { |
| 137 | .llseek = generic_file_llseek, |
| 138 | .read = generic_read_dir, |
Gao Xiang | bee1568 | 2019-02-21 10:34:11 +0800 | [diff] [blame] | 139 | .iterate_shared = erofs_readdir, |
Gao Xiang | 3aa8ec7 | 2018-07-26 20:21:49 +0800 | [diff] [blame] | 140 | }; |