xref: /linux/fs/qnx4/dir.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
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 <linux/filelock.h>
17 #include "qnx4.h"
18 
19 static int qnx4_readdir(struct file *file, struct dir_context *ctx)
20 {
21 	struct inode *inode = file_inode(file);
22 	unsigned int offset;
23 	struct buffer_head *bh;
24 	unsigned long blknum;
25 	int ix, ino;
26 	int size;
27 
28 	QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
29 	QNX4DEBUG((KERN_INFO "pos                 = %ld\n", (long) ctx->pos));
30 
31 	while (ctx->pos < inode->i_size) {
32 		blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
33 		bh = sb_bread(inode->i_sb, blknum);
34 		if (bh == NULL) {
35 			printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
36 			return 0;
37 		}
38 		ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
39 		for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
40 			union qnx4_directory_entry *de;
41 			const char *fname;
42 
43 			offset = ix * QNX4_DIR_ENTRY_SIZE;
44 			de = (union qnx4_directory_entry *) (bh->b_data + offset);
45 
46 			fname = get_entry_fname(de, &size);
47 			if (!fname)
48 				continue;
49 
50 			if (!(de->de_status & QNX4_FILE_LINK)) {
51 				ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
52 			} else {
53 				ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
54 					QNX4_INODES_PER_BLOCK +
55 					de->link.dl_inode_ndx;
56 			}
57 
58 			QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, fname));
59 			if (!dir_emit(ctx, fname, size, ino, DT_UNKNOWN)) {
60 				brelse(bh);
61 				return 0;
62 			}
63 		}
64 		brelse(bh);
65 	}
66 	return 0;
67 }
68 
69 const struct file_operations qnx4_dir_operations =
70 {
71 	.llseek		= generic_file_llseek,
72 	.read		= generic_read_dir,
73 	.iterate_shared	= qnx4_readdir,
74 	.fsync		= generic_file_fsync,
75 	.setlease	= generic_setlease,
76 };
77 
78 const struct inode_operations qnx4_dir_inode_operations =
79 {
80 	.lookup		= qnx4_lookup,
81 };
82