OSDN Git Service

qnx4: avoid stringop-overread errors
[uclinux-h8/linux.git] / fs / qnx4 / dir.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * QNX4 file system, Linux implementation.
4  *
5  * Version : 0.2.1
6  *
7  * Using parts of the xiafs filesystem.
8  *
9  * History :
10  *
11  * 28-05-1998 by Richard Frowijn : first release.
12  * 20-06-1998 by Frank Denis : Linux 2.1.99+ & dcache support.
13  */
14
15 #include <linux/buffer_head.h>
16 #include "qnx4.h"
17
18 /*
19  * A qnx4 directory entry is an inode entry or link info
20  * depending on the status field in the last byte. The
21  * first byte is where the name start either way, and a
22  * zero means it's empty.
23  */
24 union qnx4_directory_entry {
25         struct {
26                 char de_name;
27                 char de_pad[62];
28                 char de_status;
29         };
30         struct qnx4_inode_entry inode;
31         struct qnx4_link_info link;
32 };
33
34 static int qnx4_readdir(struct file *file, struct dir_context *ctx)
35 {
36         struct inode *inode = file_inode(file);
37         unsigned int offset;
38         struct buffer_head *bh;
39         unsigned long blknum;
40         int ix, ino;
41         int size;
42
43         QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
44         QNX4DEBUG((KERN_INFO "pos                 = %ld\n", (long) ctx->pos));
45
46         while (ctx->pos < inode->i_size) {
47                 blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
48                 bh = sb_bread(inode->i_sb, blknum);
49                 if (bh == NULL) {
50                         printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
51                         return 0;
52                 }
53                 ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
54                 for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
55                         union qnx4_directory_entry *de;
56                         const char *name;
57
58                         offset = ix * QNX4_DIR_ENTRY_SIZE;
59                         de = (union qnx4_directory_entry *) (bh->b_data + offset);
60
61                         if (!de->de_name)
62                                 continue;
63                         if (!(de->de_status & (QNX4_FILE_USED|QNX4_FILE_LINK)))
64                                 continue;
65                         if (!(de->de_status & QNX4_FILE_LINK)) {
66                                 size = sizeof(de->inode.di_fname);
67                                 name = de->inode.di_fname;
68                                 ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
69                         } else {
70                                 size = sizeof(de->link.dl_fname);
71                                 name = de->link.dl_fname;
72                                 ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
73                                         QNX4_INODES_PER_BLOCK +
74                                         de->link.dl_inode_ndx;
75                         }
76                         size = strnlen(name, size);
77                         QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, name));
78                         if (!dir_emit(ctx, name, size, ino, DT_UNKNOWN)) {
79                                 brelse(bh);
80                                 return 0;
81                         }
82                 }
83                 brelse(bh);
84         }
85         return 0;
86 }
87
88 const struct file_operations qnx4_dir_operations =
89 {
90         .llseek         = generic_file_llseek,
91         .read           = generic_read_dir,
92         .iterate_shared = qnx4_readdir,
93         .fsync          = generic_file_fsync,
94 };
95
96 const struct inode_operations qnx4_dir_inode_operations =
97 {
98         .lookup         = qnx4_lookup,
99 };