[ALSA] dummy - Use platform_device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / bfs / dir.c
blob5af928fa044909a63d0f82297ee4a70ffec6b71c
1 /*
2 * fs/bfs/dir.c
3 * BFS directory operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
6 */
8 #include <linux/time.h>
9 #include <linux/string.h>
10 #include <linux/fs.h>
11 #include <linux/smp_lock.h>
12 #include <linux/buffer_head.h>
13 #include <linux/sched.h>
14 #include "bfs.h"
16 #undef DEBUG
18 #ifdef DEBUG
19 #define dprintf(x...) printf(x)
20 #else
21 #define dprintf(x...)
22 #endif
24 static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino);
25 static struct buffer_head * bfs_find_entry(struct inode * dir,
26 const unsigned char * name, int namelen, struct bfs_dirent ** res_dir);
28 static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
30 struct inode * dir = f->f_dentry->d_inode;
31 struct buffer_head * bh;
32 struct bfs_dirent * de;
33 unsigned int offset;
34 int block;
36 lock_kernel();
38 if (f->f_pos & (BFS_DIRENT_SIZE-1)) {
39 printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
40 dir->i_sb->s_id, dir->i_ino);
41 unlock_kernel();
42 return -EBADF;
45 while (f->f_pos < dir->i_size) {
46 offset = f->f_pos & (BFS_BSIZE-1);
47 block = BFS_I(dir)->i_sblock + (f->f_pos >> BFS_BSIZE_BITS);
48 bh = sb_bread(dir->i_sb, block);
49 if (!bh) {
50 f->f_pos += BFS_BSIZE - offset;
51 continue;
53 do {
54 de = (struct bfs_dirent *)(bh->b_data + offset);
55 if (de->ino) {
56 int size = strnlen(de->name, BFS_NAMELEN);
57 if (filldir(dirent, de->name, size, f->f_pos, le16_to_cpu(de->ino), DT_UNKNOWN) < 0) {
58 brelse(bh);
59 unlock_kernel();
60 return 0;
63 offset += BFS_DIRENT_SIZE;
64 f->f_pos += BFS_DIRENT_SIZE;
65 } while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
66 brelse(bh);
69 unlock_kernel();
70 return 0;
73 struct file_operations bfs_dir_operations = {
74 .read = generic_read_dir,
75 .readdir = bfs_readdir,
76 .fsync = file_fsync,
79 extern void dump_imap(const char *, struct super_block *);
81 static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
82 struct nameidata *nd)
84 int err;
85 struct inode * inode;
86 struct super_block * s = dir->i_sb;
87 struct bfs_sb_info * info = BFS_SB(s);
88 unsigned long ino;
90 inode = new_inode(s);
91 if (!inode)
92 return -ENOSPC;
93 lock_kernel();
94 ino = find_first_zero_bit(info->si_imap, info->si_lasti);
95 if (ino > info->si_lasti) {
96 unlock_kernel();
97 iput(inode);
98 return -ENOSPC;
100 set_bit(ino, info->si_imap);
101 info->si_freei--;
102 inode->i_uid = current->fsuid;
103 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
104 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
105 inode->i_blocks = inode->i_blksize = 0;
106 inode->i_op = &bfs_file_inops;
107 inode->i_fop = &bfs_file_operations;
108 inode->i_mapping->a_ops = &bfs_aops;
109 inode->i_mode = mode;
110 inode->i_ino = ino;
111 BFS_I(inode)->i_dsk_ino = ino;
112 BFS_I(inode)->i_sblock = 0;
113 BFS_I(inode)->i_eblock = 0;
114 insert_inode_hash(inode);
115 mark_inode_dirty(inode);
116 dump_imap("create",s);
118 err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
119 if (err) {
120 inode->i_nlink--;
121 mark_inode_dirty(inode);
122 iput(inode);
123 unlock_kernel();
124 return err;
126 unlock_kernel();
127 d_instantiate(dentry, inode);
128 return 0;
131 static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
133 struct inode * inode = NULL;
134 struct buffer_head * bh;
135 struct bfs_dirent * de;
137 if (dentry->d_name.len > BFS_NAMELEN)
138 return ERR_PTR(-ENAMETOOLONG);
140 lock_kernel();
141 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
142 if (bh) {
143 unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
144 brelse(bh);
145 inode = iget(dir->i_sb, ino);
146 if (!inode) {
147 unlock_kernel();
148 return ERR_PTR(-EACCES);
151 unlock_kernel();
152 d_add(dentry, inode);
153 return NULL;
156 static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
158 struct inode * inode = old->d_inode;
159 int err;
161 lock_kernel();
162 err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
163 if (err) {
164 unlock_kernel();
165 return err;
167 inode->i_nlink++;
168 inode->i_ctime = CURRENT_TIME_SEC;
169 mark_inode_dirty(inode);
170 atomic_inc(&inode->i_count);
171 d_instantiate(new, inode);
172 unlock_kernel();
173 return 0;
177 static int bfs_unlink(struct inode * dir, struct dentry * dentry)
179 int error = -ENOENT;
180 struct inode * inode;
181 struct buffer_head * bh;
182 struct bfs_dirent * de;
184 inode = dentry->d_inode;
185 lock_kernel();
186 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
187 if (!bh || le16_to_cpu(de->ino) != inode->i_ino)
188 goto out_brelse;
190 if (!inode->i_nlink) {
191 printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode->i_sb->s_id,
192 inode->i_ino, inode->i_nlink);
193 inode->i_nlink = 1;
195 de->ino = 0;
196 mark_buffer_dirty(bh);
197 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
198 mark_inode_dirty(dir);
199 inode->i_nlink--;
200 inode->i_ctime = dir->i_ctime;
201 mark_inode_dirty(inode);
202 error = 0;
204 out_brelse:
205 brelse(bh);
206 unlock_kernel();
207 return error;
210 static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
211 struct inode * new_dir, struct dentry * new_dentry)
213 struct inode * old_inode, * new_inode;
214 struct buffer_head * old_bh, * new_bh;
215 struct bfs_dirent * old_de, * new_de;
216 int error = -ENOENT;
218 old_bh = new_bh = NULL;
219 old_inode = old_dentry->d_inode;
220 if (S_ISDIR(old_inode->i_mode))
221 return -EINVAL;
223 lock_kernel();
224 old_bh = bfs_find_entry(old_dir,
225 old_dentry->d_name.name,
226 old_dentry->d_name.len, &old_de);
228 if (!old_bh || le16_to_cpu(old_de->ino) != old_inode->i_ino)
229 goto end_rename;
231 error = -EPERM;
232 new_inode = new_dentry->d_inode;
233 new_bh = bfs_find_entry(new_dir,
234 new_dentry->d_name.name,
235 new_dentry->d_name.len, &new_de);
237 if (new_bh && !new_inode) {
238 brelse(new_bh);
239 new_bh = NULL;
241 if (!new_bh) {
242 error = bfs_add_entry(new_dir,
243 new_dentry->d_name.name,
244 new_dentry->d_name.len, old_inode->i_ino);
245 if (error)
246 goto end_rename;
248 old_de->ino = 0;
249 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
250 mark_inode_dirty(old_dir);
251 if (new_inode) {
252 new_inode->i_nlink--;
253 new_inode->i_ctime = CURRENT_TIME_SEC;
254 mark_inode_dirty(new_inode);
256 mark_buffer_dirty(old_bh);
257 error = 0;
259 end_rename:
260 unlock_kernel();
261 brelse(old_bh);
262 brelse(new_bh);
263 return error;
266 struct inode_operations bfs_dir_inops = {
267 .create = bfs_create,
268 .lookup = bfs_lookup,
269 .link = bfs_link,
270 .unlink = bfs_unlink,
271 .rename = bfs_rename,
274 static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino)
276 struct buffer_head * bh;
277 struct bfs_dirent * de;
278 int block, sblock, eblock, off, eoff;
279 int i;
281 dprintf("name=%s, namelen=%d\n", name, namelen);
283 if (!namelen)
284 return -ENOENT;
285 if (namelen > BFS_NAMELEN)
286 return -ENAMETOOLONG;
288 sblock = BFS_I(dir)->i_sblock;
289 eblock = BFS_I(dir)->i_eblock;
290 eoff = dir->i_size % BFS_BSIZE;
291 for (block=sblock; block<=eblock; block++) {
292 bh = sb_bread(dir->i_sb, block);
293 if(!bh)
294 return -ENOSPC;
295 for (off=0; off<BFS_BSIZE; off+=BFS_DIRENT_SIZE) {
296 de = (struct bfs_dirent *)(bh->b_data + off);
297 if (block==eblock && off>=eoff) {
298 /* Do not read/interpret the garbage in the end of eblock. */
299 de->ino = 0;
301 if (!de->ino) {
302 if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
303 dir->i_size += BFS_DIRENT_SIZE;
304 dir->i_ctime = CURRENT_TIME_SEC;
306 dir->i_mtime = CURRENT_TIME_SEC;
307 mark_inode_dirty(dir);
308 de->ino = cpu_to_le16((u16)ino);
309 for (i=0; i<BFS_NAMELEN; i++)
310 de->name[i] = (i < namelen) ? name[i] : 0;
311 mark_buffer_dirty(bh);
312 brelse(bh);
313 return 0;
316 brelse(bh);
318 return -ENOSPC;
321 static inline int bfs_namecmp(int len, const unsigned char * name, const char * buffer)
323 if (len < BFS_NAMELEN && buffer[len])
324 return 0;
325 return !memcmp(name, buffer, len);
328 static struct buffer_head * bfs_find_entry(struct inode * dir,
329 const unsigned char * name, int namelen, struct bfs_dirent ** res_dir)
331 unsigned long block, offset;
332 struct buffer_head * bh;
333 struct bfs_dirent * de;
335 *res_dir = NULL;
336 if (namelen > BFS_NAMELEN)
337 return NULL;
338 bh = NULL;
339 block = offset = 0;
340 while (block * BFS_BSIZE + offset < dir->i_size) {
341 if (!bh) {
342 bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
343 if (!bh) {
344 block++;
345 continue;
348 de = (struct bfs_dirent *)(bh->b_data + offset);
349 offset += BFS_DIRENT_SIZE;
350 if (le16_to_cpu(de->ino) && bfs_namecmp(namelen, name, de->name)) {
351 *res_dir = de;
352 return bh;
354 if (offset < bh->b_size)
355 continue;
356 brelse(bh);
357 bh = NULL;
358 offset = 0;
359 block++;
361 brelse(bh);
362 return NULL;