xref: /openbmc/linux/fs/qnx4/namei.c (revision 9d56dd3b083a3bec56e9da35ce07baca81030b03)
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  * 01-06-1998 by Richard Frowijn : first release.
11  * 21-06-1998 by Frank Denis : dcache support, fixed error codes.
12  * 04-07-1998 by Frank Denis : first step for rmdir/unlink.
13  */
14 
15 #include <linux/smp_lock.h>
16 #include <linux/buffer_head.h>
17 #include "qnx4.h"
18 
19 
20 /*
21  * check if the filename is correct. For some obscure reason, qnx writes a
22  * new file twice in the directory entry, first with all possible options at 0
23  * and for a second time the way it is, they want us not to access the qnx
24  * filesystem when whe are using linux.
25  */
26 static int qnx4_match(int len, const char *name,
27 		      struct buffer_head *bh, unsigned long *offset)
28 {
29 	struct qnx4_inode_entry *de;
30 	int namelen, thislen;
31 
32 	if (bh == NULL) {
33 		printk(KERN_WARNING "qnx4: matching unassigned buffer !\n");
34 		return 0;
35 	}
36 	de = (struct qnx4_inode_entry *) (bh->b_data + *offset);
37 	*offset += QNX4_DIR_ENTRY_SIZE;
38 	if ((de->di_status & QNX4_FILE_LINK) != 0) {
39 		namelen = QNX4_NAME_MAX;
40 	} else {
41 		namelen = QNX4_SHORT_NAME_MAX;
42 	}
43 	/* "" means "." ---> so paths like "/usr/lib//libc.a" work */
44 	if (!len && (de->di_fname[0] == '.') && (de->di_fname[1] == '\0')) {
45 		return 1;
46 	}
47 	thislen = strlen( de->di_fname );
48 	if ( thislen > namelen )
49 		thislen = namelen;
50 	if (len != thislen) {
51 		return 0;
52 	}
53 	if (strncmp(name, de->di_fname, len) == 0) {
54 		if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) {
55 			return 1;
56 		}
57 	}
58 	return 0;
59 }
60 
61 static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
62 	   const char *name, struct qnx4_inode_entry **res_dir, int *ino)
63 {
64 	unsigned long block, offset, blkofs;
65 	struct buffer_head *bh;
66 
67 	*res_dir = NULL;
68 	if (!dir->i_sb) {
69 		printk(KERN_WARNING "qnx4: no superblock on dir.\n");
70 		return NULL;
71 	}
72 	bh = NULL;
73 	block = offset = blkofs = 0;
74 	while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
75 		if (!bh) {
76 			bh = qnx4_bread(dir, blkofs, 0);
77 			if (!bh) {
78 				blkofs++;
79 				continue;
80 			}
81 		}
82 		*res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
83 		if (qnx4_match(len, name, bh, &offset)) {
84 			block = qnx4_block_map( dir, blkofs );
85 			*ino = block * QNX4_INODES_PER_BLOCK +
86 			    (offset / QNX4_DIR_ENTRY_SIZE) - 1;
87 			return bh;
88 		}
89 		if (offset < bh->b_size) {
90 			continue;
91 		}
92 		brelse(bh);
93 		bh = NULL;
94 		offset = 0;
95 		blkofs++;
96 	}
97 	brelse(bh);
98 	*res_dir = NULL;
99 	return NULL;
100 }
101 
102 struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
103 {
104 	int ino;
105 	struct qnx4_inode_entry *de;
106 	struct qnx4_link_info *lnk;
107 	struct buffer_head *bh;
108 	const char *name = dentry->d_name.name;
109 	int len = dentry->d_name.len;
110 	struct inode *foundinode = NULL;
111 
112 	lock_kernel();
113 	if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
114 		goto out;
115 	/* The entry is linked, let's get the real info */
116 	if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
117 		lnk = (struct qnx4_link_info *) de;
118 		ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) *
119                     QNX4_INODES_PER_BLOCK +
120 		    lnk->dl_inode_ndx;
121 	}
122 	brelse(bh);
123 
124 	foundinode = qnx4_iget(dir->i_sb, ino);
125 	if (IS_ERR(foundinode)) {
126 		unlock_kernel();
127 		QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
128 			   PTR_ERR(foundinode)));
129 		return ERR_CAST(foundinode);
130 	}
131 out:
132 	unlock_kernel();
133 	d_add(dentry, foundinode);
134 
135 	return NULL;
136 }
137