18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * QNX4 file system, Linux implementation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Version : 0.2.1
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Using parts of the xiafs filesystem.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * History :
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * 28-05-1998 by Richard Frowijn : first release.
128c2ecf20Sopenharmony_ci * 20-06-1998 by Frank Denis : Linux 2.1.99+ & dcache support.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/buffer_head.h>
168c2ecf20Sopenharmony_ci#include "qnx4.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * A qnx4 directory entry is an inode entry or link info
208c2ecf20Sopenharmony_ci * depending on the status field in the last byte. The
218c2ecf20Sopenharmony_ci * first byte is where the name start either way, and a
228c2ecf20Sopenharmony_ci * zero means it's empty.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Also, due to a bug in gcc, we don't want to use the
258c2ecf20Sopenharmony_ci * real (differently sized) name arrays in the inode and
268c2ecf20Sopenharmony_ci * link entries, but always the 'de_name[]' one in the
278c2ecf20Sopenharmony_ci * fake struct entry.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * See
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c6
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * for details, but basically gcc will take the size of the
348c2ecf20Sopenharmony_ci * 'name' array from one of the used union entries randomly.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * This use of 'de_name[]' (48 bytes) avoids the false positive
378c2ecf20Sopenharmony_ci * warnings that would happen if gcc decides to use 'inode.di_name'
388c2ecf20Sopenharmony_ci * (16 bytes) even when the pointer and size were to come from
398c2ecf20Sopenharmony_ci * 'link.dl_name' (48 bytes).
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * In all cases the actual name pointer itself is the same, it's
428c2ecf20Sopenharmony_ci * only the gcc internal 'what is the size of this field' logic
438c2ecf20Sopenharmony_ci * that can get confused.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ciunion qnx4_directory_entry {
468c2ecf20Sopenharmony_ci	struct {
478c2ecf20Sopenharmony_ci		const char de_name[48];
488c2ecf20Sopenharmony_ci		u8 de_pad[15];
498c2ecf20Sopenharmony_ci		u8 de_status;
508c2ecf20Sopenharmony_ci	};
518c2ecf20Sopenharmony_ci	struct qnx4_inode_entry inode;
528c2ecf20Sopenharmony_ci	struct qnx4_link_info link;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic int qnx4_readdir(struct file *file, struct dir_context *ctx)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(file);
588c2ecf20Sopenharmony_ci	unsigned int offset;
598c2ecf20Sopenharmony_ci	struct buffer_head *bh;
608c2ecf20Sopenharmony_ci	unsigned long blknum;
618c2ecf20Sopenharmony_ci	int ix, ino;
628c2ecf20Sopenharmony_ci	int size;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
658c2ecf20Sopenharmony_ci	QNX4DEBUG((KERN_INFO "pos                 = %ld\n", (long) ctx->pos));
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	while (ctx->pos < inode->i_size) {
688c2ecf20Sopenharmony_ci		blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
698c2ecf20Sopenharmony_ci		bh = sb_bread(inode->i_sb, blknum);
708c2ecf20Sopenharmony_ci		if (bh == NULL) {
718c2ecf20Sopenharmony_ci			printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
728c2ecf20Sopenharmony_ci			return 0;
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci		ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
758c2ecf20Sopenharmony_ci		for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
768c2ecf20Sopenharmony_ci			union qnx4_directory_entry *de;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci			offset = ix * QNX4_DIR_ENTRY_SIZE;
798c2ecf20Sopenharmony_ci			de = (union qnx4_directory_entry *) (bh->b_data + offset);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci			if (!de->de_name[0])
828c2ecf20Sopenharmony_ci				continue;
838c2ecf20Sopenharmony_ci			if (!(de->de_status & (QNX4_FILE_USED|QNX4_FILE_LINK)))
848c2ecf20Sopenharmony_ci				continue;
858c2ecf20Sopenharmony_ci			if (!(de->de_status & QNX4_FILE_LINK)) {
868c2ecf20Sopenharmony_ci				size = sizeof(de->inode.di_fname);
878c2ecf20Sopenharmony_ci				ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
888c2ecf20Sopenharmony_ci			} else {
898c2ecf20Sopenharmony_ci				size = sizeof(de->link.dl_fname);
908c2ecf20Sopenharmony_ci				ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
918c2ecf20Sopenharmony_ci					QNX4_INODES_PER_BLOCK +
928c2ecf20Sopenharmony_ci					de->link.dl_inode_ndx;
938c2ecf20Sopenharmony_ci			}
948c2ecf20Sopenharmony_ci			size = strnlen(de->de_name, size);
958c2ecf20Sopenharmony_ci			QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, name));
968c2ecf20Sopenharmony_ci			if (!dir_emit(ctx, de->de_name, size, ino, DT_UNKNOWN)) {
978c2ecf20Sopenharmony_ci				brelse(bh);
988c2ecf20Sopenharmony_ci				return 0;
998c2ecf20Sopenharmony_ci			}
1008c2ecf20Sopenharmony_ci		}
1018c2ecf20Sopenharmony_ci		brelse(bh);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciconst struct file_operations qnx4_dir_operations =
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	.llseek		= generic_file_llseek,
1098c2ecf20Sopenharmony_ci	.read		= generic_read_dir,
1108c2ecf20Sopenharmony_ci	.iterate_shared	= qnx4_readdir,
1118c2ecf20Sopenharmony_ci	.fsync		= generic_file_fsync,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciconst struct inode_operations qnx4_dir_inode_operations =
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	.lookup		= qnx4_lookup,
1178c2ecf20Sopenharmony_ci};
118