Import 2.3.35pre2
[davej-history.git] / fs / qnx4 / namei.c
blob0b6e087e5d6ea245af76a2db7c58c419f3b9693d
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.
15 #include <linux/config.h>
16 #include <linux/sched.h>
17 #include <linux/qnx4_fs.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/fcntl.h>
22 #include <linux/errno.h>
24 #include <asm/segment.h>
27 * check if the filename is correct. For some obscure reason, qnx writes a
28 * new file twice in the directory entry, first with all possible options at 0
29 * and for a second time the way it is, they want us not to access the qnx
30 * filesystem when whe are using linux.
32 static int qnx4_match(int len, const char *name,
33 struct buffer_head *bh, unsigned long *offset)
35 struct qnx4_inode_entry *de;
36 struct qnx4_link_info *le;
37 int namelen, thislen;
39 if (bh == NULL) {
40 printk("qnx4: matching unassigned buffer !\n");
41 return 0;
43 de = (struct qnx4_inode_entry *) (bh->b_data + *offset);
44 *offset += QNX4_DIR_ENTRY_SIZE;
45 if ((de->di_status & QNX4_FILE_LINK) != 0) {
46 namelen = QNX4_NAME_MAX;
47 } else {
48 namelen = QNX4_SHORT_NAME_MAX;
50 /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
51 if (!len && (de->di_fname[0] == '.') && (de->di_fname[1] == '\0')) {
52 return 1;
54 thislen = strlen( de->di_fname );
55 if ( thislen > namelen )
56 thislen = namelen;
57 if (len != thislen) {
58 return 0;
60 if (strncmp(name, de->di_fname, len) == 0) {
61 if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) {
62 return 1;
65 return 0;
68 static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
69 const char *name, struct qnx4_inode_entry **res_dir, int *ino)
71 unsigned long block, offset, blkofs;
72 struct buffer_head *bh;
74 *res_dir = NULL;
75 if (!dir->i_sb) {
76 printk("qnx4: no superblock on dir.\n");
77 return NULL;
79 bh = NULL;
80 block = offset = blkofs = 0;
81 while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
82 if (!bh) {
83 block = qnx4_block_map( dir, blkofs );
84 bh = qnx4_bread(dir, block, 0);
85 if (!bh) {
86 blkofs++;
87 continue;
90 *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
91 if (qnx4_match(len, name, bh, &offset)) {
92 *ino = block * QNX4_INODES_PER_BLOCK +
93 (offset / QNX4_DIR_ENTRY_SIZE) - 1;
94 return bh;
96 if (offset < bh->b_size) {
97 continue;
99 brelse(bh);
100 bh = NULL;
101 offset = 0;
102 blkofs++;
104 brelse(bh);
105 *res_dir = NULL;
106 return NULL;
109 struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry)
111 int ino;
112 struct qnx4_inode_entry *de;
113 struct qnx4_link_info *lnk;
114 struct buffer_head *bh;
115 const char *name = dentry->d_name.name;
116 int len = dentry->d_name.len;
117 struct inode *foundinode = NULL;
119 if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
120 goto out;
121 /* The entry is linked, let's get the real info */
122 if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
123 lnk = (struct qnx4_link_info *) de;
124 ino = (lnk->dl_inode_blk - 1) * QNX4_INODES_PER_BLOCK +
125 lnk->dl_inode_ndx;
127 brelse(bh);
129 if ((foundinode = iget(dir->i_sb, ino)) == NULL) {
130 QNX4DEBUG(("qnx4: lookup->iget -> NULL\n"));
131 return ERR_PTR(-EACCES);
133 out:
134 d_add(dentry, foundinode);
136 return NULL;
139 #ifdef CONFIG_QNX4FS_RW
140 int qnx4_create(struct inode *dir, struct dentry *dentry, int mode)
142 QNX4DEBUG(("qnx4: qnx4_create\n"));
143 if (dir == NULL) {
144 return -ENOENT;
146 return -ENOSPC;
149 int qnx4_rmdir(struct inode *dir, struct dentry *dentry)
151 struct buffer_head *bh;
152 struct qnx4_inode_entry *de;
153 struct inode *inode;
154 int retval;
155 int ino;
157 QNX4DEBUG(("qnx4: qnx4_rmdir [%s]\n", dentry->d_name.name));
158 bh = qnx4_find_entry(dentry->d_name.len, dir, dentry->d_name.name,
159 &de, &ino);
160 if (bh == NULL) {
161 return -ENOENT;
163 inode = dentry->d_inode;
164 if (inode->i_ino != ino) {
165 retval = -EIO;
166 goto end_rmdir;
168 #if 0
169 if (!empty_dir(inode)) {
170 retval = -ENOTEMPTY;
171 goto end_rmdir;
173 #endif
174 if (!list_empty(&dentry->d_hash)) {
175 retval = -EBUSY;
176 goto end_rmdir;
178 if (inode->i_nlink != 2) {
179 QNX4DEBUG(("empty directory has nlink!=2 (%d)\n", inode->i_nlink));
181 QNX4DEBUG(("qnx4: deleting directory\n"));
182 de->di_status = 0;
183 memset(de->di_fname, 0, sizeof de->di_fname);
184 de->di_mode = 0;
185 mark_buffer_dirty(bh, 1);
186 inode->i_nlink = 0;
187 mark_inode_dirty(inode);
188 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
189 dir->i_nlink--;
190 mark_inode_dirty(dir);
191 d_delete(dentry);
192 retval = 0;
194 end_rmdir:
195 brelse(bh);
197 return retval;
200 int qnx4_unlink(struct inode *dir, struct dentry *dentry)
202 struct buffer_head *bh;
203 struct qnx4_inode_entry *de;
204 struct inode *inode;
205 int retval;
206 int ino;
208 QNX4DEBUG(("qnx4: qnx4_unlink [%s]\n", dentry->d_name.name));
209 bh = qnx4_find_entry(dentry->d_name.len, dir, dentry->d_name.name,
210 &de, &ino);
211 if (bh == NULL) {
212 return -ENOENT;
214 inode = dentry->d_inode;
215 if (inode->i_ino != ino) {
216 retval = -EIO;
217 goto end_unlink;
219 retval = -EPERM;
220 if (!inode->i_nlink) {
221 QNX4DEBUG(("Deleting nonexistent file (%s:%lu), %d\n",
222 kdevname(inode->i_dev),
223 inode->i_ino, inode->i_nlink));
224 inode->i_nlink = 1;
226 de->di_status = 0;
227 memset(de->di_fname, 0, sizeof de->di_fname);
228 de->di_mode = 0;
229 mark_buffer_dirty(bh, 1);
230 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
231 mark_inode_dirty(dir);
232 inode->i_nlink--;
233 inode->i_ctime = dir->i_ctime;
234 mark_inode_dirty(inode);
235 d_delete(dentry);
236 retval = 0;
238 end_unlink:
239 brelse(bh);
241 return retval;
243 #endif