GNU Linux-libre 4.4.289-gnu1
[releases.git] / fs / qnx4 / dir.c
1 /*
2  * QNX4 file system, Linux implementation.
3  *
4  * Version : 0.2.1
5  *
6  * Using parts of the xiafs filesystem.
7  *
8  * History :
9  *
10  * 28-05-1998 by Richard Frowijn : first release.
11  * 20-06-1998 by Frank Denis : Linux 2.1.99+ & dcache support.
12  */
13
14 #include <linux/buffer_head.h>
15 #include "qnx4.h"
16
17 /*
18  * A qnx4 directory entry is an inode entry or link info
19  * depending on the status field in the last byte. The
20  * first byte is where the name start either way, and a
21  * zero means it's empty.
22  *
23  * Also, due to a bug in gcc, we don't want to use the
24  * real (differently sized) name arrays in the inode and
25  * link entries, but always the 'de_name[]' one in the
26  * fake struct entry.
27  *
28  * See
29  *
30  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c6
31  *
32  * for details, but basically gcc will take the size of the
33  * 'name' array from one of the used union entries randomly.
34  *
35  * This use of 'de_name[]' (48 bytes) avoids the false positive
36  * warnings that would happen if gcc decides to use 'inode.di_name'
37  * (16 bytes) even when the pointer and size were to come from
38  * 'link.dl_name' (48 bytes).
39  *
40  * In all cases the actual name pointer itself is the same, it's
41  * only the gcc internal 'what is the size of this field' logic
42  * that can get confused.
43  */
44 union qnx4_directory_entry {
45         struct {
46                 const char de_name[48];
47                 u8 de_pad[15];
48                 u8 de_status;
49         };
50         struct qnx4_inode_entry inode;
51         struct qnx4_link_info link;
52 };
53
54 static int qnx4_readdir(struct file *file, struct dir_context *ctx)
55 {
56         struct inode *inode = file_inode(file);
57         unsigned int offset;
58         struct buffer_head *bh;
59         unsigned long blknum;
60         int ix, ino;
61         int size;
62
63         QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
64         QNX4DEBUG((KERN_INFO "pos                 = %ld\n", (long) ctx->pos));
65
66         while (ctx->pos < inode->i_size) {
67                 blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
68                 bh = sb_bread(inode->i_sb, blknum);
69                 if (bh == NULL) {
70                         printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
71                         return 0;
72                 }
73                 ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
74                 for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
75                         union qnx4_directory_entry *de;
76
77                         offset = ix * QNX4_DIR_ENTRY_SIZE;
78                         de = (union qnx4_directory_entry *) (bh->b_data + offset);
79
80                         if (!de->de_name[0])
81                                 continue;
82                         if (!(de->de_status & (QNX4_FILE_USED|QNX4_FILE_LINK)))
83                                 continue;
84                         if (!(de->de_status & QNX4_FILE_LINK)) {
85                                 size = sizeof(de->inode.di_fname);
86                                 ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
87                         } else {
88                                 size = sizeof(de->link.dl_fname);
89                                 ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
90                                         QNX4_INODES_PER_BLOCK +
91                                         de->link.dl_inode_ndx;
92                         }
93                         size = strnlen(de->de_name, size);
94                         QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, name));
95                         if (!dir_emit(ctx, de->de_name, size, ino, DT_UNKNOWN)) {
96                                 brelse(bh);
97                                 return 0;
98                         }
99                 }
100                 brelse(bh);
101         }
102         return 0;
103 }
104
105 const struct file_operations qnx4_dir_operations =
106 {
107         .llseek         = generic_file_llseek,
108         .read           = generic_read_dir,
109         .iterate        = qnx4_readdir,
110         .fsync          = generic_file_fsync,
111 };
112
113 const struct inode_operations qnx4_dir_inode_operations =
114 {
115         .lookup         = qnx4_lookup,
116 };