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