* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / qnx4 / namei.c
blobfeb014b8ed236593ddafa65a5a60d7451737a481
1 /*
2 * QNX4 file system, Linux implementation.
3 *
4 * Version : 0.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 int namelen;
38 if (bh == NULL) {
39 printk("qnx4: matching unassigned buffer !\n");
40 return 0;
42 de = (struct qnx4_inode_entry *) (bh->b_data + *offset);
43 *offset += QNX4_DIR_ENTRY_SIZE;
44 if ((de->di_status & 0x08) == 0x08) {
45 namelen = QNX4_NAME_MAX;
46 } else {
47 namelen = _SHORT_NAME_MAX;
49 /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
50 if (!len && (de->di_fname[0] == '.') && (de->di_fname[1] == '\0')) {
51 return 1;
53 if (len != strlen(de->di_fname)) {
54 return 0;
56 if (strncmp(name, de->di_fname, len) == 0) {
57 if ((de->di_mode) || (de->di_status == QNX4_FILE_LINK)) {
58 if (de->di_status) {
59 return 1;
63 return 0;
66 static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
67 const char *name, struct qnx4_inode_entry **res_dir, int *ino)
69 unsigned long block, offset, blkofs;
70 struct buffer_head *bh;
72 *res_dir = NULL;
73 if (!dir->i_sb) {
74 printk("qnx4: no superblock on dir.\n");
75 return NULL;
77 bh = NULL;
78 blkofs = dir->u.qnx4_i.i_first_xtnt.xtnt_blk - 1;
79 offset = block = 0;
80 while (block * QNX4_BLOCK_SIZE + offset < dir->i_size) {
81 if (!bh) {
82 bh = qnx4_bread(dir, block + blkofs, 0);
83 if (!bh) {
84 block++;
85 continue;
88 *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
89 if (qnx4_match(len, name, bh, &offset)) {
90 *ino = (block + blkofs) * QNX4_INODES_PER_BLOCK +
91 (offset / QNX4_DIR_ENTRY_SIZE) - 1;
92 return bh;
94 if (offset < bh->b_size) {
95 continue;
97 brelse(bh);
98 bh = NULL;
99 offset = 0;
100 block++;
102 brelse(bh);
103 *res_dir = NULL;
104 return NULL;
107 struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry)
109 int ino;
110 struct qnx4_inode_entry *de;
111 struct qnx4_link_info *lnk;
112 struct buffer_head *bh;
113 const char *name = dentry->d_name.name;
114 int len = dentry->d_name.len;
115 struct inode *foundinode = NULL;
117 if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
118 goto out;
119 /* The entry is linked, let's get the real info */
120 if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
121 lnk = (struct qnx4_link_info *) de;
122 ino = (lnk->dl_inode_blk - 1) * QNX4_INODES_PER_BLOCK +
123 lnk->dl_inode_ndx;
125 brelse(bh);
127 if ((foundinode = iget(dir->i_sb, ino)) == NULL) {
128 QNX4DEBUG(("qnx4: lookup->iget -> NULL\n"));
129 return ERR_PTR(-EACCES);
131 out:
132 d_add(dentry, foundinode);
134 return NULL;
137 #ifdef CONFIG_QNX4FS_RW
138 int qnx4_create(struct inode *dir, struct dentry *dentry, int mode)
140 QNX4DEBUG(("qnx4: qnx4_create\n"));
141 if (dir == NULL) {
142 return -ENOENT;
144 return -ENOSPC;
147 int qnx4_rmdir(struct inode *dir, struct dentry *dentry)
149 struct buffer_head *bh;
150 struct qnx4_inode_entry *de;
151 struct inode *inode;
152 int retval;
153 int ino;
155 QNX4DEBUG(("qnx4: qnx4_rmdir [%s]\n", dentry->d_name.name));
156 bh = qnx4_find_entry(dentry->d_name.len, dir, dentry->d_name.name,
157 &de, &ino);
158 if (bh == NULL) {
159 return -ENOENT;
161 inode = dentry->d_inode;
162 if (inode->i_ino != ino) {
163 retval = -EIO;
164 goto end_rmdir;
166 #if 0
167 if (!empty_dir(inode)) {
168 retval = -ENOTEMPTY;
169 goto end_rmdir;
171 #endif
172 if (!list_empty(&dentry->d_hash)) {
173 retval = -EBUSY;
174 goto end_rmdir;
176 if (inode->i_nlink != 2) {
177 QNX4DEBUG(("empty directory has nlink!=2 (%d)\n", inode->i_nlink));
179 QNX4DEBUG(("qnx4: deleting directory\n"));
180 de->di_status = 0;
181 memset(de->di_fname, 0, sizeof de->di_fname);
182 de->di_mode = 0;
183 mark_buffer_dirty(bh, 1);
184 inode->i_nlink = 0;
185 mark_inode_dirty(inode);
186 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
187 dir->i_nlink--;
188 mark_inode_dirty(dir);
189 d_delete(dentry);
190 retval = 0;
192 end_rmdir:
193 brelse(bh);
195 return retval;
198 int qnx4_unlink(struct inode *dir, struct dentry *dentry)
200 struct buffer_head *bh;
201 struct qnx4_inode_entry *de;
202 struct inode *inode;
203 int retval;
204 int ino;
206 QNX4DEBUG(("qnx4: qnx4_unlink [%s]\n", dentry->d_name.name));
207 bh = qnx4_find_entry(dentry->d_name.len, dir, dentry->d_name.name,
208 &de, &ino);
209 if (bh == NULL) {
210 return -ENOENT;
212 inode = dentry->d_inode;
213 if (inode->i_ino != ino) {
214 retval = -EIO;
215 goto end_unlink;
217 retval = -EPERM;
218 if (!inode->i_nlink) {
219 QNX4DEBUG(("Deleting nonexistent file (%s:%lu), %d\n",
220 kdevname(inode->i_dev),
221 inode->i_ino, inode->i_nlink));
222 inode->i_nlink = 1;
224 de->di_status = 0;
225 memset(de->di_fname, 0, sizeof de->di_fname);
226 de->di_mode = 0;
227 mark_buffer_dirty(bh, 1);
228 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
229 mark_inode_dirty(dir);
230 inode->i_nlink--;
231 inode->i_ctime = dir->i_ctime;
232 mark_inode_dirty(inode);
233 d_delete(dentry);
234 retval = 0;
236 end_unlink:
237 brelse(bh);
239 return retval;
241 #endif