blob: c368360c35a167be19592642f803dd4211f20b49 [file] [log] [blame]
Bob Copelanda3ab7152008-07-25 19:45:16 -07001/*
2 * OMFS (as used by RIO Karma) directory operations.
3 * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
4 * Released under GPL v2.
5 */
6
7#include <linux/fs.h>
8#include <linux/ctype.h>
9#include <linux/buffer_head.h>
10#include "omfs.h"
11
12static int omfs_hash(const char *name, int namelen, int mod)
13{
14 int i, hash = 0;
15 for (i = 0; i < namelen; i++)
16 hash ^= tolower(name[i]) << (i % 24);
17 return hash % mod;
18}
19
20/*
21 * Finds the bucket for a given name and reads the containing block;
22 * *ofs is set to the offset of the first list entry.
23 */
24static struct buffer_head *omfs_get_bucket(struct inode *dir,
25 const char *name, int namelen, int *ofs)
26{
27 int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
Bob Copelanda3ab7152008-07-25 19:45:16 -070028 int bucket = omfs_hash(name, namelen, nbuckets);
29
30 *ofs = OMFS_DIR_START + bucket * 8;
Bob Copelandf0682722008-09-06 17:51:53 -040031 return omfs_bread(dir->i_sb, dir->i_ino);
Bob Copelanda3ab7152008-07-25 19:45:16 -070032}
33
34static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
35 const char *name, int namelen,
36 u64 *prev_block)
37{
38 struct buffer_head *bh;
39 struct omfs_inode *oi;
40 int err = -ENOENT;
41 *prev_block = ~0;
42
43 while (block != ~0) {
Bob Copelandf0682722008-09-06 17:51:53 -040044 bh = omfs_bread(dir->i_sb, block);
Bob Copelanda3ab7152008-07-25 19:45:16 -070045 if (!bh) {
46 err = -EIO;
47 goto err;
48 }
49
50 oi = (struct omfs_inode *) bh->b_data;
51 if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, block)) {
52 brelse(bh);
53 goto err;
54 }
55
56 if (strncmp(oi->i_name, name, namelen) == 0)
57 return bh;
58
59 *prev_block = block;
60 block = be64_to_cpu(oi->i_sibling);
61 brelse(bh);
62 }
63err:
64 return ERR_PTR(err);
65}
66
67static struct buffer_head *omfs_find_entry(struct inode *dir,
68 const char *name, int namelen)
69{
70 struct buffer_head *bh;
71 int ofs;
72 u64 block, dummy;
73
74 bh = omfs_get_bucket(dir, name, namelen, &ofs);
75 if (!bh)
76 return ERR_PTR(-EIO);
77
78 block = be64_to_cpu(*((__be64 *) &bh->b_data[ofs]));
79 brelse(bh);
80
81 return omfs_scan_list(dir, block, name, namelen, &dummy);
82}
83
84int omfs_make_empty(struct inode *inode, struct super_block *sb)
85{
86 struct omfs_sb_info *sbi = OMFS_SB(sb);
Bob Copelanda3ab7152008-07-25 19:45:16 -070087 struct buffer_head *bh;
88 struct omfs_inode *oi;
89
Bob Copelandf0682722008-09-06 17:51:53 -040090 bh = omfs_bread(sb, inode->i_ino);
Bob Copelanda3ab7152008-07-25 19:45:16 -070091 if (!bh)
92 return -ENOMEM;
93
94 memset(bh->b_data, 0, sizeof(struct omfs_inode));
95
96 if (inode->i_mode & S_IFDIR) {
97 memset(&bh->b_data[OMFS_DIR_START], 0xff,
98 sbi->s_sys_blocksize - OMFS_DIR_START);
99 } else
100 omfs_make_empty_table(bh, OMFS_EXTENT_START);
101
102 oi = (struct omfs_inode *) bh->b_data;
103 oi->i_head.h_self = cpu_to_be64(inode->i_ino);
Harvey Harrisond406f662008-07-29 22:33:46 -0700104 oi->i_sibling = ~cpu_to_be64(0ULL);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700105
106 mark_buffer_dirty(bh);
107 brelse(bh);
108 return 0;
109}
110
111static int omfs_add_link(struct dentry *dentry, struct inode *inode)
112{
113 struct inode *dir = dentry->d_parent->d_inode;
114 const char *name = dentry->d_name.name;
115 int namelen = dentry->d_name.len;
116 struct omfs_inode *oi;
117 struct buffer_head *bh;
118 u64 block;
119 __be64 *entry;
120 int ofs;
121
122 /* just prepend to head of queue in proper bucket */
123 bh = omfs_get_bucket(dir, name, namelen, &ofs);
124 if (!bh)
125 goto out;
126
127 entry = (__be64 *) &bh->b_data[ofs];
128 block = be64_to_cpu(*entry);
129 *entry = cpu_to_be64(inode->i_ino);
130 mark_buffer_dirty(bh);
131 brelse(bh);
132
133 /* now set the sibling and parent pointers on the new inode */
Bob Copelandf0682722008-09-06 17:51:53 -0400134 bh = omfs_bread(dir->i_sb, inode->i_ino);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700135 if (!bh)
136 goto out;
137
138 oi = (struct omfs_inode *) bh->b_data;
139 memcpy(oi->i_name, name, namelen);
140 memset(oi->i_name + namelen, 0, OMFS_NAMELEN - namelen);
141 oi->i_sibling = cpu_to_be64(block);
142 oi->i_parent = cpu_to_be64(dir->i_ino);
143 mark_buffer_dirty(bh);
144 brelse(bh);
145
146 dir->i_ctime = CURRENT_TIME_SEC;
147
148 /* mark affected inodes dirty to rebuild checksums */
149 mark_inode_dirty(dir);
150 mark_inode_dirty(inode);
151 return 0;
152out:
153 return -ENOMEM;
154}
155
156static int omfs_delete_entry(struct dentry *dentry)
157{
158 struct inode *dir = dentry->d_parent->d_inode;
159 struct inode *dirty;
160 const char *name = dentry->d_name.name;
161 int namelen = dentry->d_name.len;
162 struct omfs_inode *oi;
163 struct buffer_head *bh, *bh2;
164 __be64 *entry, next;
165 u64 block, prev;
166 int ofs;
167 int err = -ENOMEM;
168
169 /* delete the proper node in the bucket's linked list */
170 bh = omfs_get_bucket(dir, name, namelen, &ofs);
171 if (!bh)
172 goto out;
173
174 entry = (__be64 *) &bh->b_data[ofs];
175 block = be64_to_cpu(*entry);
176
177 bh2 = omfs_scan_list(dir, block, name, namelen, &prev);
178 if (IS_ERR(bh2)) {
179 err = PTR_ERR(bh2);
180 goto out_free_bh;
181 }
182
183 oi = (struct omfs_inode *) bh2->b_data;
184 next = oi->i_sibling;
185 brelse(bh2);
186
187 if (prev != ~0) {
188 /* found in middle of list, get list ptr */
189 brelse(bh);
Bob Copelandf0682722008-09-06 17:51:53 -0400190 bh = omfs_bread(dir->i_sb, prev);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700191 if (!bh)
192 goto out;
193
194 oi = (struct omfs_inode *) bh->b_data;
195 entry = &oi->i_sibling;
196 }
197
198 *entry = next;
199 mark_buffer_dirty(bh);
200
201 if (prev != ~0) {
202 dirty = omfs_iget(dir->i_sb, prev);
203 if (!IS_ERR(dirty)) {
204 mark_inode_dirty(dirty);
205 iput(dirty);
206 }
207 }
208
209 err = 0;
210out_free_bh:
211 brelse(bh);
212out:
213 return err;
214}
215
216static int omfs_dir_is_empty(struct inode *inode)
217{
218 int nbuckets = (inode->i_size - OMFS_DIR_START) / 8;
219 struct buffer_head *bh;
220 u64 *ptr;
221 int i;
222
Bob Copelandf0682722008-09-06 17:51:53 -0400223 bh = omfs_bread(inode->i_sb, inode->i_ino);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700224
225 if (!bh)
226 return 0;
227
228 ptr = (u64 *) &bh->b_data[OMFS_DIR_START];
229
230 for (i = 0; i < nbuckets; i++, ptr++)
231 if (*ptr != ~0)
232 break;
233
234 brelse(bh);
235 return *ptr != ~0;
236}
237
Al Virod9328052011-03-04 01:31:03 -0500238static int omfs_remove(struct inode *dir, struct dentry *dentry)
Bob Copelanda3ab7152008-07-25 19:45:16 -0700239{
Bob Copelanda3ab7152008-07-25 19:45:16 -0700240 struct inode *inode = dentry->d_inode;
Al Virod9328052011-03-04 01:31:03 -0500241 int ret;
242
Sage Weil79bf7c72011-05-24 13:06:06 -0700243
244 if (S_ISDIR(inode->i_mode)) {
245 dentry_unhash(dentry);
246 if (!omfs_dir_is_empty(inode))
247 return -ENOTEMPTY;
248 }
Bob Copelanda3ab7152008-07-25 19:45:16 -0700249
250 ret = omfs_delete_entry(dentry);
251 if (ret)
Al Virod9328052011-03-04 01:31:03 -0500252 return ret;
253
254 clear_nlink(inode);
255 mark_inode_dirty(inode);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700256 mark_inode_dirty(dir);
Al Virod9328052011-03-04 01:31:03 -0500257 return 0;
Bob Copelanda3ab7152008-07-25 19:45:16 -0700258}
259
260static int omfs_add_node(struct inode *dir, struct dentry *dentry, int mode)
261{
262 int err;
263 struct inode *inode = omfs_new_inode(dir, mode);
264
265 if (IS_ERR(inode))
266 return PTR_ERR(inode);
267
268 err = omfs_make_empty(inode, dir->i_sb);
269 if (err)
270 goto out_free_inode;
271
272 err = omfs_add_link(dentry, inode);
273 if (err)
274 goto out_free_inode;
275
276 d_instantiate(dentry, inode);
277 return 0;
278
279out_free_inode:
280 iput(inode);
281 return err;
282}
283
284static int omfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
285{
286 return omfs_add_node(dir, dentry, mode | S_IFDIR);
287}
288
289static int omfs_create(struct inode *dir, struct dentry *dentry, int mode,
290 struct nameidata *nd)
291{
292 return omfs_add_node(dir, dentry, mode | S_IFREG);
293}
294
295static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
296 struct nameidata *nd)
297{
298 struct buffer_head *bh;
299 struct inode *inode = NULL;
300
301 if (dentry->d_name.len > OMFS_NAMELEN)
302 return ERR_PTR(-ENAMETOOLONG);
303
304 bh = omfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
305 if (!IS_ERR(bh)) {
306 struct omfs_inode *oi = (struct omfs_inode *)bh->b_data;
307 ino_t ino = be64_to_cpu(oi->i_head.h_self);
308 brelse(bh);
309 inode = omfs_iget(dir->i_sb, ino);
310 if (IS_ERR(inode))
311 return ERR_CAST(inode);
312 }
313 d_add(dentry, inode);
314 return NULL;
315}
316
317/* sanity check block's self pointer */
318int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
319 u64 fsblock)
320{
321 int is_bad;
322 u64 ino = be64_to_cpu(header->h_self);
323 is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
324 (ino > sbi->s_num_blocks));
325
326 if (is_bad)
327 printk(KERN_WARNING "omfs: bad hash chain detected\n");
328
329 return is_bad;
330}
331
332static int omfs_fill_chain(struct file *filp, void *dirent, filldir_t filldir,
333 u64 fsblock, int hindex)
334{
335 struct inode *dir = filp->f_dentry->d_inode;
336 struct buffer_head *bh;
337 struct omfs_inode *oi;
338 u64 self;
339 int res = 0;
340 unsigned char d_type;
341
342 /* follow chain in this bucket */
343 while (fsblock != ~0) {
Bob Copelandf0682722008-09-06 17:51:53 -0400344 bh = omfs_bread(dir->i_sb, fsblock);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700345 if (!bh)
346 goto out;
347
348 oi = (struct omfs_inode *) bh->b_data;
349 if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
350 brelse(bh);
351 goto out;
352 }
353
354 self = fsblock;
355 fsblock = be64_to_cpu(oi->i_sibling);
356
357 /* skip visited nodes */
358 if (hindex) {
359 hindex--;
360 brelse(bh);
361 continue;
362 }
363
364 d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
365
366 res = filldir(dirent, oi->i_name, strnlen(oi->i_name,
367 OMFS_NAMELEN), filp->f_pos, self, d_type);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700368 brelse(bh);
Al Viro31be83a2011-03-04 01:43:36 -0500369 if (res < 0)
370 break;
371 filp->f_pos++;
Bob Copelanda3ab7152008-07-25 19:45:16 -0700372 }
373out:
374 return res;
375}
376
377static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
378 struct inode *new_dir, struct dentry *new_dentry)
379{
380 struct inode *new_inode = new_dentry->d_inode;
381 struct inode *old_inode = old_dentry->d_inode;
Bob Copelanda3ab7152008-07-25 19:45:16 -0700382 int err;
383
Bob Copelanda3ab7152008-07-25 19:45:16 -0700384 if (new_inode) {
Sage Weile4eaac02011-05-24 13:06:07 -0700385 if (S_ISDIR(new_inode->i_mode))
386 dentry_unhash(new_dentry);
387
Bob Copelanda3ab7152008-07-25 19:45:16 -0700388 /* overwriting existing file/dir */
Al Virod9328052011-03-04 01:31:03 -0500389 err = omfs_remove(new_dir, new_dentry);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700390 if (err)
391 goto out;
392 }
393
394 /* since omfs locates files by name, we need to unlink _before_
395 * adding the new link or we won't find the old one */
Al Virocdb26492011-03-04 01:18:19 -0500396 err = omfs_delete_entry(old_dentry);
397 if (err)
Bob Copelanda3ab7152008-07-25 19:45:16 -0700398 goto out;
Bob Copelanda3ab7152008-07-25 19:45:16 -0700399
Al Virocdb26492011-03-04 01:18:19 -0500400 mark_inode_dirty(old_dir);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700401 err = omfs_add_link(new_dentry, old_inode);
402 if (err)
403 goto out;
404
405 old_inode->i_ctime = CURRENT_TIME_SEC;
Al Viro013e4f42011-03-04 01:14:55 -0500406 mark_inode_dirty(old_inode);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700407out:
408 return err;
409}
410
411static int omfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
412{
413 struct inode *dir = filp->f_dentry->d_inode;
414 struct buffer_head *bh;
415 loff_t offset, res;
416 unsigned int hchain, hindex;
417 int nbuckets;
418 u64 fsblock;
419 int ret = -EINVAL;
420
421 if (filp->f_pos >> 32)
422 goto success;
423
424 switch ((unsigned long) filp->f_pos) {
425 case 0:
426 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
427 goto success;
428 filp->f_pos++;
429 /* fall through */
430 case 1:
431 if (filldir(dirent, "..", 2, 1,
432 parent_ino(filp->f_dentry), DT_DIR) < 0)
433 goto success;
434 filp->f_pos = 1 << 20;
435 /* fall through */
436 }
437
438 nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
439
440 /* high 12 bits store bucket + 1 and low 20 bits store hash index */
441 hchain = (filp->f_pos >> 20) - 1;
442 hindex = filp->f_pos & 0xfffff;
443
Bob Copelandf0682722008-09-06 17:51:53 -0400444 bh = omfs_bread(dir->i_sb, dir->i_ino);
Bob Copelanda3ab7152008-07-25 19:45:16 -0700445 if (!bh)
446 goto out;
447
448 offset = OMFS_DIR_START + hchain * 8;
449
450 for (; hchain < nbuckets; hchain++, offset += 8) {
451 fsblock = be64_to_cpu(*((__be64 *) &bh->b_data[offset]));
452
453 res = omfs_fill_chain(filp, dirent, filldir, fsblock, hindex);
454 hindex = 0;
455 if (res < 0)
456 break;
457
458 filp->f_pos = (hchain+2) << 20;
459 }
460 brelse(bh);
461success:
462 ret = 0;
463out:
464 return ret;
465}
466
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700467const struct inode_operations omfs_dir_inops = {
Bob Copelanda3ab7152008-07-25 19:45:16 -0700468 .lookup = omfs_lookup,
469 .mkdir = omfs_mkdir,
470 .rename = omfs_rename,
471 .create = omfs_create,
Al Virod9328052011-03-04 01:31:03 -0500472 .unlink = omfs_remove,
473 .rmdir = omfs_remove,
Bob Copelanda3ab7152008-07-25 19:45:16 -0700474};
475
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700476const struct file_operations omfs_dir_operations = {
Bob Copelanda3ab7152008-07-25 19:45:16 -0700477 .read = generic_read_dir,
478 .readdir = omfs_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200479 .llseek = generic_file_llseek,
Bob Copelanda3ab7152008-07-25 19:45:16 -0700480};