9 #include <linux/slab.h>
81 return max(pos, new_pos - 1);
86 loff_t pos = bix << inode->
i_sb->s_blocksize_bits;
87 return pos >= i_size_read(inode);
98 static u32 hash_32(
const char *
s,
int len,
u32 seed)
103 for (i = 0; i < len; i++)
104 hash = hash * 293 + s[i];
134 static pgoff_t hash_index(
u32 hash,
int round)
143 return hash % i0_blocks;
145 return i0_blocks + hash % (i1_blocks - i0_blocks);
147 return i1_blocks + hash % (i2_blocks - i1_blocks);
149 return i2_blocks + hash % (i3_blocks - i2_blocks);
151 return i3_blocks + 16 * (hash % (((1<<31) - i3_blocks) / 16))
162 u32 hash = hash_32(name->
name, name->len, 0);
169 for (round = 0; round < 20; round++) {
170 index = hash_index(hash, round);
172 if (beyond_eof(dir, index))
196 static int logfs_remove_inode(
struct inode *
inode)
201 ret = write_inode(inode);
213 static int logfs_unlink(
struct inode *dir,
struct dentry *dentry)
216 struct inode *inode = dentry->
d_inode;
231 page = logfs_get_dd_page(dir, dentry);
238 return PTR_ERR(page);
248 ret = write_inode(dir);
251 abort_transaction(dir, ta);
258 ret = logfs_remove_inode(inode);
264 static inline int logfs_empty_dir(
struct inode *dir)
269 return data >= i_size_read(dir);
272 static int logfs_rmdir(
struct inode *dir,
struct dentry *dentry)
274 struct inode *inode = dentry->
d_inode;
276 if (!logfs_empty_dir(inode))
279 return logfs_unlink(dir, dentry);
284 #define IMPLICIT_NODES 2
287 struct inode *dir = file->f_dentry->d_inode;
295 if (beyond_eof(dir, pos))
299 pos = dir_seek_data(dir, pos);
305 return PTR_ERR(page);
323 struct inode *inode = file->f_dentry->d_inode;
330 if (file->
f_pos == 0) {
331 if (filldir(buf,
".", 1, 1, inode->
i_ino,
DT_DIR) < 0)
335 if (file->
f_pos == 1) {
336 if (filldir(buf,
"..", 2, 2, pino,
DT_DIR) < 0)
341 err = __logfs_readdir(file, buf, filldir);
351 static struct dentry *logfs_lookup(
struct inode *dir,
struct dentry *dentry,
360 page = logfs_get_dd_page(dir, dentry);
362 return ERR_CAST(page);
375 printk(
KERN_ERR"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
376 ino, dir->
i_ino, index);
380 static void grow_dir(
struct inode *dir, loff_t index)
382 index = (index + 1) << dir->
i_sb->s_blocksize_bits;
384 i_size_write(dir, index);
387 static int logfs_write_dir(
struct inode *dir,
struct dentry *dentry,
396 for (round = 0; round < 20; round++) {
397 index = hash_index(hash, round);
406 memset(dd, 0,
sizeof(*dd));
408 dd->
type = logfs_type(inode);
409 logfs_set_name(dd, &dentry->
d_name);
416 grow_dir(dir, index);
426 static int __logfs_create(
struct inode *dir,
struct dentry *dentry,
427 struct inode *inode,
const char *
dest,
long destlen)
450 ret = write_inode(inode);
453 ret = write_inode(inode);
456 abort_transaction(inode, ta);
466 ret = logfs_write_dir(dir, dentry, inode);
469 ret = write_inode(dir);
475 logfs_remove_inode(inode);
485 static int logfs_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
496 return PTR_ERR(inode);
501 return __logfs_create(dir, dentry, inode,
NULL, 0);
504 static int logfs_create(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
511 return PTR_ERR(inode);
517 return __logfs_create(dir, dentry, inode,
NULL, 0);
520 static int logfs_mknod(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
530 return PTR_ERR(inode);
534 return __logfs_create(dir, dentry, inode,
NULL, 0);
537 static int logfs_symlink(
struct inode *dir,
struct dentry *dentry,
541 size_t destlen =
strlen(target) + 1;
543 if (destlen > dir->
i_sb->s_blocksize)
548 return PTR_ERR(inode);
553 return __logfs_create(dir, dentry, inode, target, destlen);
556 static int logfs_link(
struct dentry *old_dentry,
struct inode *dir,
557 struct dentry *dentry)
559 struct inode *inode = old_dentry->
d_inode;
564 mark_inode_dirty_sync(inode);
566 return __logfs_create(dir, dentry, inode,
NULL, 0);
569 static int logfs_get_dd(
struct inode *dir,
struct dentry *dentry,
575 page = logfs_get_dd_page(dir, dentry);
577 return PTR_ERR(page);
580 memcpy(dd, map,
sizeof(*dd));
586 static int logfs_delete_dd(
struct inode *dir, loff_t pos)
593 BUG_ON(beyond_eof(dir, pos));
595 log_dir(
" Delete dentry (%lx, %llx)\n", dir->
i_ino, pos);
604 static int logfs_rename_cross(
struct inode *old_dir,
struct dentry *old_dentry,
605 struct inode *new_dir,
struct dentry *new_dentry)
614 err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
629 err = logfs_write_dir(new_dir, new_dentry, old_dentry->
d_inode);
631 err = write_inode(new_dir);
636 abort_transaction(new_dir, ta);
643 err = logfs_delete_dd(old_dir, pos);
645 err = write_inode(old_dir);
652 static int logfs_replace_inode(
struct inode *dir,
struct dentry *dentry,
658 err = logfs_get_dd(dir, dentry, dd, &pos);
662 dd->
type = logfs_type(inode);
664 err = write_dir(dir, dd, pos);
667 log_dir(
"Replace dentry (%lx, %llx) %s -> %llx\n", dir->
i_ino, pos,
669 return write_inode(dir);
676 static int logfs_rename_target(
struct inode *old_dir,
struct dentry *old_dentry,
677 struct inode *new_dir,
struct dentry *new_dentry)
680 struct inode *old_inode = old_dentry->
d_inode;
690 if (!logfs_empty_dir(new_inode))
695 err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
711 err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
716 abort_transaction(new_dir, ta);
723 err = logfs_delete_dd(old_dir, pos);
725 err = write_inode(old_dir);
731 err = logfs_remove_inode(new_inode);
738 static int logfs_rename(
struct inode *old_dir,
struct dentry *old_dentry,
739 struct inode *new_dir,
struct dentry *new_dentry)
742 return logfs_rename_target(old_dir, old_dentry,
743 new_dir, new_dentry);
744 return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
765 err = logfs_remove_inode(inode);
784 err = logfs_delete_dd(inode, pos);
804 .create = logfs_create,
806 .lookup = logfs_lookup,
807 .mkdir = logfs_mkdir,
808 .mknod = logfs_mknod,
809 .rename = logfs_rename,
810 .rmdir = logfs_rmdir,
811 .symlink = logfs_symlink,
812 .unlink = logfs_unlink,
817 .readdir = logfs_readdir,