blob: 66645a5a35f306bc2213baff61166f03f9cb9e91 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/buffer_head.h>
Al Viro964f5362009-06-07 09:47:13 -040016#include "qnx4.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070018/*
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.
Linus Torvaldsd5f65452021-09-20 10:26:21 -070023 *
24 * Also, due to a bug in gcc, we don't want to use the
25 * real (differently sized) name arrays in the inode and
26 * link entries, but always the 'de_name[]' one in the
27 * fake struct entry.
28 *
29 * See
30 *
31 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c6
32 *
33 * for details, but basically gcc will take the size of the
34 * 'name' array from one of the used union entries randomly.
35 *
36 * This use of 'de_name[]' (48 bytes) avoids the false positive
37 * warnings that would happen if gcc decides to use 'inode.di_name'
38 * (16 bytes) even when the pointer and size were to come from
39 * 'link.dl_name' (48 bytes).
40 *
41 * In all cases the actual name pointer itself is the same, it's
42 * only the gcc internal 'what is the size of this field' logic
43 * that can get confused.
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070044 */
45union qnx4_directory_entry {
46 struct {
Linus Torvaldsd5f65452021-09-20 10:26:21 -070047 const char de_name[48];
48 u8 de_pad[15];
49 u8 de_status;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070050 };
51 struct qnx4_inode_entry inode;
52 struct qnx4_link_info link;
53};
54
Al Viro663f4de2013-05-17 15:17:59 -040055static int qnx4_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Al Viro663f4de2013-05-17 15:17:59 -040057 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned int offset;
59 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned long blknum;
61 int ix, ino;
62 int size;
63
Anders Larsen891ddb92009-09-26 20:15:09 +020064 QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
Al Viro663f4de2013-05-17 15:17:59 -040065 QNX4DEBUG((KERN_INFO "pos = %ld\n", (long) ctx->pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Al Viro663f4de2013-05-17 15:17:59 -040067 while (ctx->pos < inode->i_size) {
68 blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 bh = sb_bread(inode->i_sb, blknum);
Al Viro663f4de2013-05-17 15:17:59 -040070 if (bh == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
Al Viro663f4de2013-05-17 15:17:59 -040072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Al Viro663f4de2013-05-17 15:17:59 -040074 ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
75 for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070076 union qnx4_directory_entry *de;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 offset = ix * QNX4_DIR_ENTRY_SIZE;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070079 de = (union qnx4_directory_entry *) (bh->b_data + offset);
80
Linus Torvaldsd5f65452021-09-20 10:26:21 -070081 if (!de->de_name[0])
Al Viro663f4de2013-05-17 15:17:59 -040082 continue;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070083 if (!(de->de_status & (QNX4_FILE_USED|QNX4_FILE_LINK)))
Al Viro663f4de2013-05-17 15:17:59 -040084 continue;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070085 if (!(de->de_status & QNX4_FILE_LINK)) {
86 size = sizeof(de->inode.di_fname);
Al Viro663f4de2013-05-17 15:17:59 -040087 ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070088 } else {
89 size = sizeof(de->link.dl_fname);
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070090 ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
Al Viro663f4de2013-05-17 15:17:59 -040091 QNX4_INODES_PER_BLOCK +
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070092 de->link.dl_inode_ndx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Linus Torvaldsd5f65452021-09-20 10:26:21 -070094 size = strnlen(de->de_name, size);
Linus Torvaldsb7213ff2021-09-15 13:56:37 -070095 QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, name));
Linus Torvaldsd5f65452021-09-20 10:26:21 -070096 if (!dir_emit(ctx, de->de_name, size, ino, DT_UNKNOWN)) {
Al Viro663f4de2013-05-17 15:17:59 -040097 brelse(bh);
98 return 0;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 brelse(bh);
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800106const struct file_operations qnx4_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
jan Blunckca572722010-05-26 14:44:53 -0700108 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -0400110 .iterate_shared = qnx4_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200111 .fsync = generic_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800114const struct inode_operations qnx4_dir_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 .lookup = qnx4_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};