Commit b71034e5 authored by Evgeniy Dushistov's avatar Evgeniy Dushistov Committed by Linus Torvalds

[PATCH] ufs: directory and page cache: from blocks to pages

Change function in fs/ufs/dir.c and fs/ufs/namei.c to work with pages
instead of straight work with blocks.  It fixed such bugs:

* for i in `seq 1 1000`; do touch $i; done - crash system
* mkdir create directory without "." and ".." entries
Signed-off-by: default avatarEvgeniy Dushistov <dushistov@mail.ru>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 826843a3
...@@ -11,13 +11,15 @@ ...@@ -11,13 +11,15 @@
* 4.4BSD (FreeBSD) support added on February 1st 1998 by * 4.4BSD (FreeBSD) support added on February 1st 1998 by
* Niels Kristian Bech Jensen <nkbj@image.dk> partially based * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
* on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>. * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
*
* Migration to usage of "page cache" on May 2006 by
* Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
*/ */
#include <linux/time.h> #include <linux/time.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/ufs_fs.h> #include <linux/ufs_fs.h>
#include <linux/smp_lock.h> #include <linux/smp_lock.h>
#include <linux/buffer_head.h>
#include <linux/sched.h> #include <linux/sched.h>
#include "swab.h" #include "swab.h"
...@@ -31,11 +33,6 @@ ...@@ -31,11 +33,6 @@
#define UFSD(x) #define UFSD(x)
#endif #endif
static int
ufs_check_dir_entry (const char *, struct inode *, struct ufs_dir_entry *,
struct buffer_head *, unsigned long);
/* /*
* NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure. * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
* *
...@@ -51,495 +48,540 @@ static inline int ufs_match(struct super_block *sb, int len, ...@@ -51,495 +48,540 @@ static inline int ufs_match(struct super_block *sb, int len,
return !memcmp(name, de->d_name, len); return !memcmp(name, de->d_name, len);
} }
/* static int ufs_commit_chunk(struct page *page, unsigned from, unsigned to)
* This is blatantly stolen from ext2fs
*/
static int
ufs_readdir (struct file * filp, void * dirent, filldir_t filldir)
{ {
struct inode *inode = filp->f_dentry->d_inode; struct inode *dir = page->mapping->host;
int error = 0; int err = 0;
unsigned long offset, lblk; dir->i_version++;
int i, stored; page->mapping->a_ops->commit_write(NULL, page, from, to);
struct buffer_head * bh; if (IS_DIRSYNC(dir))
struct ufs_dir_entry * de; err = write_one_page(page, 1);
struct super_block * sb; else
int de_reclen; unlock_page(page);
unsigned flags; return err;
u64 blk= 0L; }
lock_kernel();
sb = inode->i_sb;
flags = UFS_SB(sb)->s_flags;
UFSD(("ENTER, ino %lu f_pos %lu\n", inode->i_ino, (unsigned long) filp->f_pos))
stored = 0;
bh = NULL;
offset = filp->f_pos & (sb->s_blocksize - 1);
while (!error && !stored && filp->f_pos < inode->i_size) {
lblk = (filp->f_pos) >> sb->s_blocksize_bits;
blk = ufs_frag_map(inode, lblk);
if (!blk || !(bh = sb_bread(sb, blk))) {
/* XXX - error - skip to the next block */
printk("ufs_readdir: "
"dir inode %lu has a hole at offset %lu\n",
inode->i_ino, (unsigned long int)filp->f_pos);
filp->f_pos += sb->s_blocksize - offset;
continue;
}
revalidate: static inline void ufs_put_page(struct page *page)
/* If the dir block has changed since the last call to {
* readdir(2), then we might be pointing to an invalid kunmap(page);
* dirent right now. Scan from the start of the block page_cache_release(page);
* to make sure. */ }
if (filp->f_version != inode->i_version) {
for (i = 0; i < sb->s_blocksize && i < offset; ) {
de = (struct ufs_dir_entry *)(bh->b_data + i);
/* It's too expensive to do a full
* dirent test each time round this
* loop, but we do have to test at
* least that it is non-zero. A
* failure will be detected in the
* dirent test below. */
de_reclen = fs16_to_cpu(sb, de->d_reclen);
if (de_reclen < 1)
break;
i += de_reclen;
}
offset = i;
filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
| offset;
filp->f_version = inode->i_version;
}
while (!error && filp->f_pos < inode->i_size static inline unsigned long ufs_dir_pages(struct inode *inode)
&& offset < sb->s_blocksize) { {
de = (struct ufs_dir_entry *) (bh->b_data + offset); return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
/* XXX - put in a real ufs_check_dir_entry() */ }
if ((de->d_reclen == 0) || (ufs_get_de_namlen(sb, de) == 0)) {
filp->f_pos = (filp->f_pos &
(sb->s_blocksize - 1)) +
sb->s_blocksize;
brelse(bh);
unlock_kernel();
return stored;
}
if (!ufs_check_dir_entry ("ufs_readdir", inode, de,
bh, offset)) {
/* On error, skip the f_pos to the
next block. */
filp->f_pos = (filp->f_pos |
(sb->s_blocksize - 1)) +
1;
brelse (bh);
unlock_kernel();
return stored;
}
offset += fs16_to_cpu(sb, de->d_reclen);
if (de->d_ino) {
/* We might block in the next section
* if the data destination is
* currently swapped out. So, use a
* version stamp to detect whether or
* not the directory has been modified
* during the copy operation. */
unsigned long version = filp->f_version;
unsigned char d_type = DT_UNKNOWN;
UFSD(("filldir(%s,%u)\n", de->d_name, ino_t ufs_inode_by_name(struct inode *dir, struct dentry *dentry)
fs32_to_cpu(sb, de->d_ino))) {
UFSD(("namlen %u\n", ufs_get_de_namlen(sb, de))) ino_t res = 0;
struct ufs_dir_entry *de;
struct page *page;
if ((flags & UFS_DE_MASK) == UFS_DE_44BSD) de = ufs_find_entry(dir, dentry, &page);
d_type = de->d_u.d_44.d_type; if (de) {
error = filldir(dirent, de->d_name, res = fs32_to_cpu(dir->i_sb, de->d_ino);
ufs_get_de_namlen(sb, de), filp->f_pos, ufs_put_page(page);
fs32_to_cpu(sb, de->d_ino), d_type);
if (error)
break;
if (version != filp->f_version)
goto revalidate;
stored ++;
}
filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
}
offset = 0;
brelse (bh);
} }
unlock_kernel(); return res;
return 0; }
/* Releases the page */
void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
struct page *page, struct inode *inode)
{
unsigned from = (char *) de - (char *) page_address(page);
unsigned to = from + fs16_to_cpu(dir->i_sb, de->d_reclen);
int err;
lock_page(page);
err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
BUG_ON(err);
de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
ufs_set_de_type(dir->i_sb, de, inode->i_mode);
err = ufs_commit_chunk(page, from, to);
ufs_put_page(page);
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(dir);
}
static void ufs_check_page(struct page *page)
{
struct inode *dir = page->mapping->host;
struct super_block *sb = dir->i_sb;
char *kaddr = page_address(page);
unsigned offs, rec_len;
unsigned limit = PAGE_CACHE_SIZE;
struct ufs_dir_entry *p;
char *error;
if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
limit = dir->i_size & ~PAGE_CACHE_MASK;
if (limit & (UFS_SECTOR_SIZE - 1))
goto Ebadsize;
if (!limit)
goto out;
}
for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
p = (struct ufs_dir_entry *)(kaddr + offs);
rec_len = fs16_to_cpu(sb, p->d_reclen);
if (rec_len < UFS_DIR_REC_LEN(1))
goto Eshort;
if (rec_len & 3)
goto Ealign;
if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
goto Enamelen;
if (((offs + rec_len - 1) ^ offs) & ~(UFS_SECTOR_SIZE-1))
goto Espan;
if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
UFS_SB(sb)->s_uspi->s_ncg))
goto Einumber;
}
if (offs != limit)
goto Eend;
out:
SetPageChecked(page);
return;
/* Too bad, we had an error */
Ebadsize:
ufs_error(sb, "ufs_check_page",
"size of directory #%lu is not a multiple of chunk size",
dir->i_ino
);
goto fail;
Eshort:
error = "rec_len is smaller than minimal";
goto bad_entry;
Ealign:
error = "unaligned directory entry";
goto bad_entry;
Enamelen:
error = "rec_len is too small for name_len";
goto bad_entry;
Espan:
error = "directory entry across blocks";
goto bad_entry;
Einumber:
error = "inode out of bounds";
bad_entry:
ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
"offset=%lu, rec_len=%d, name_len=%d",
dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
rec_len, ufs_get_de_namlen(sb, p));
goto fail;
Eend:
p = (struct ufs_dir_entry *)(kaddr + offs);
ufs_error (sb, "ext2_check_page",
"entry in directory #%lu spans the page boundary"
"offset=%lu",
dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
fail:
SetPageChecked(page);
SetPageError(page);
}
static struct page *ufs_get_page(struct inode *dir, unsigned long n)
{
struct address_space *mapping = dir->i_mapping;
struct page *page = read_cache_page(mapping, n,
(filler_t*)mapping->a_ops->readpage, NULL);
if (!IS_ERR(page)) {
wait_on_page_locked(page);
kmap(page);
if (!PageUptodate(page))
goto fail;
if (!PageChecked(page))
ufs_check_page(page);
if (PageError(page))
goto fail;
}
return page;
fail:
ufs_put_page(page);
return ERR_PTR(-EIO);
} }
/* /*
* define how far ahead to read directories while searching them. * Return the offset into page `page_nr' of the last valid
* byte in that page, plus one.
*/ */
#define NAMEI_RA_CHUNKS 2 static unsigned
#define NAMEI_RA_BLOCKS 4 ufs_last_byte(struct inode *inode, unsigned long page_nr)
#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) {
#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) unsigned last_byte = inode->i_size;
last_byte -= page_nr << PAGE_CACHE_SHIFT;
if (last_byte > PAGE_CACHE_SIZE)
last_byte = PAGE_CACHE_SIZE;
return last_byte;
}
static inline struct ufs_dir_entry *
ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
{
return (struct ufs_dir_entry *)((char *)p +
fs16_to_cpu(sb, p->d_reclen));
}
struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
{
struct page *page = ufs_get_page(dir, 0);
struct ufs_dir_entry *de = NULL;
if (!IS_ERR(page)) {
de = ufs_next_entry(dir->i_sb,
(struct ufs_dir_entry *)page_address(page));
*p = page;
}
return de;
}
/* /*
* ufs_find_entry() * ufs_find_entry()
* *
* finds an entry in the specified directory with the wanted name. It * finds an entry in the specified directory with the wanted name. It
* returns the cache buffer in which the entry was found, and the entry * returns the page in which the entry was found, and the entry itself
* itself (as a parameter - res_bh). It does NOT read the inode of the * (as a parameter - res_dir). Page is returned mapped and unlocked.
* entry - you'll have to do that yourself if you want to. * Entry is guaranteed to be valid.
*/ */
struct ufs_dir_entry * ufs_find_entry (struct dentry *dentry, struct ufs_dir_entry *ufs_find_entry(struct inode *dir, struct dentry *dentry,
struct buffer_head ** res_bh) struct page **res_page)
{ {
struct super_block * sb; struct super_block *sb = dir->i_sb;
struct buffer_head * bh_use[NAMEI_RA_SIZE];
struct buffer_head * bh_read[NAMEI_RA_SIZE];
unsigned long offset;
int block, toread, i, err;
struct inode *dir = dentry->d_parent->d_inode;
const char *name = dentry->d_name.name; const char *name = dentry->d_name.name;
int namelen = dentry->d_name.len; int namelen = dentry->d_name.len;
unsigned reclen = UFS_DIR_REC_LEN(namelen);
UFSD(("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen)) unsigned long start, n;
unsigned long npages = ufs_dir_pages(dir);
*res_bh = NULL; struct page *page = NULL;
struct ufs_dir_entry *de;
sb = dir->i_sb;
UFSD(("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen));
if (namelen > UFS_MAXNAMLEN)
if (npages == 0 || namelen > UFS_MAXNAMLEN)
goto out;
/* OFFSET_CACHE */
*res_page = NULL;
/* start = ei->i_dir_start_lookup; */
start = 0;
if (start >= npages)
start = 0;
n = start;
do {
char *kaddr;
page = ufs_get_page(dir, n);
if (!IS_ERR(page)) {
kaddr = page_address(page);
de = (struct ufs_dir_entry *) kaddr;
kaddr += ufs_last_byte(dir, n) - reclen;
while ((char *) de <= kaddr) {
if (de->d_reclen == 0) {
ufs_error(dir->i_sb, __FUNCTION__,
"zero-length directory entry");
ufs_put_page(page);
goto out;
}
if (ufs_match(sb, namelen, name, de))
goto found;
de = ufs_next_entry(sb, de);
}
ufs_put_page(page);
}
if (++n >= npages)
n = 0;
} while (n != start);
out:
return NULL; return NULL;
memset (bh_use, 0, sizeof (bh_use)); found:
toread = 0; *res_page = page;
for (block = 0; block < NAMEI_RA_SIZE; ++block) { /* ei->i_dir_start_lookup = n; */
struct buffer_head * bh; return de;
}
if ((block << sb->s_blocksize_bits) >= dir->i_size) /*
break; * Parent is locked.
bh = ufs_getfrag (dir, block, 0, &err); */
bh_use[block] = bh; int ufs_add_link(struct dentry *dentry, struct inode *inode)
if (bh && !buffer_uptodate(bh)) {
bh_read[toread++] = bh; struct inode *dir = dentry->d_parent->d_inode;
} const char *name = dentry->d_name.name;
int namelen = dentry->d_name.len;
struct super_block *sb = dir->i_sb;
unsigned reclen = UFS_DIR_REC_LEN(namelen);
unsigned short rec_len, name_len;
struct page *page = NULL;
struct ufs_dir_entry *de;
unsigned long npages = ufs_dir_pages(dir);
unsigned long n;
char *kaddr;
unsigned from, to;
int err;
for (block = 0, offset = 0; offset < dir->i_size; block++) { UFSD(("ENTER, name %s, namelen %u\n", name, namelen));
struct buffer_head * bh;
struct ufs_dir_entry * de;
char * dlimit;
if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
ll_rw_block (READ, toread, bh_read);
toread = 0;
}
bh = bh_use[block % NAMEI_RA_SIZE];
if (!bh) {
ufs_error (sb, "ufs_find_entry",
"directory #%lu contains a hole at offset %lu",
dir->i_ino, offset);
offset += sb->s_blocksize;
continue;
}
wait_on_buffer (bh);
if (!buffer_uptodate(bh)) {
/* /*
* read error: all bets are off * We take care of directory expansion in the same loop.
* This code plays outside i_size, so it locks the page
* to protect that region.
*/ */
break; for (n = 0; n <= npages; n++) {
} char *dir_end;
page = ufs_get_page(dir, n);
err = PTR_ERR(page);
if (IS_ERR(page))
goto out;
lock_page(page);
kaddr = page_address(page);
dir_end = kaddr + ufs_last_byte(dir, n);
de = (struct ufs_dir_entry *)kaddr;
kaddr += PAGE_CACHE_SIZE - reclen;
while ((char *)de <= kaddr) {
if ((char *)de == dir_end) {
/* We hit i_size */
name_len = 0;
rec_len = UFS_SECTOR_SIZE;
de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE);
de->d_ino = 0;
goto got_it;
}
if (de->d_reclen == 0) {
ufs_error(dir->i_sb, __FUNCTION__,
"zero-length directory entry");
err = -EIO;
goto out_unlock;
}
err = -EEXIST;
if (ufs_match(sb, namelen, name, de))
goto out_unlock;
name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
rec_len = fs16_to_cpu(sb, de->d_reclen);
if (!de->d_ino && rec_len >= reclen)
goto got_it;
if (rec_len >= name_len + reclen)
goto got_it;
de = (struct ufs_dir_entry *) ((char *) de + rec_len);
}
unlock_page(page);
ufs_put_page(page);
}
BUG();
return -EINVAL;
de = (struct ufs_dir_entry *) bh->b_data; got_it:
dlimit = bh->b_data + sb->s_blocksize; from = (char*)de - (char*)page_address(page);
while ((char *) de < dlimit && offset < dir->i_size) { to = from + rec_len;
/* this code is executed quadratically often */ err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
/* do minimal checking by hand */ if (err)
int de_len; goto out_unlock;
if (de->d_ino) {
if ((char *) de + namelen <= dlimit && struct ufs_dir_entry *de1 =
ufs_match(sb, namelen, name, de)) { (struct ufs_dir_entry *) ((char *) de + name_len);
/* found a match - de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
just to be sure, do a full check */ de->d_reclen = cpu_to_fs16(sb, name_len);
if (!ufs_check_dir_entry("ufs_find_entry",
dir, de, bh, offset))
goto failed;
for (i = 0; i < NAMEI_RA_SIZE; ++i) {
if (bh_use[i] != bh)
brelse (bh_use[i]);
}
*res_bh = bh;
return de;
}
/* prevent looping on a bad block */
de_len = fs16_to_cpu(sb, de->d_reclen);
if (de_len <= 0)
goto failed;
offset += de_len;
de = (struct ufs_dir_entry *) ((char *) de + de_len);
}
brelse (bh); de = de1;
if (((block + NAMEI_RA_SIZE) << sb->s_blocksize_bits ) >=
dir->i_size)
bh = NULL;
else
bh = ufs_getfrag (dir, block + NAMEI_RA_SIZE, 0, &err);
bh_use[block % NAMEI_RA_SIZE] = bh;
if (bh && !buffer_uptodate(bh))
bh_read[toread++] = bh;
} }
failed: ufs_set_de_namlen(sb, de, namelen);
for (i = 0; i < NAMEI_RA_SIZE; ++i) brelse (bh_use[i]); memcpy(de->d_name, name, namelen + 1);
UFSD(("EXIT\n")) de->d_ino = cpu_to_fs32(sb, inode->i_ino);
return NULL; ufs_set_de_type(sb, de, inode->i_mode);
}
static int
ufs_check_dir_entry (const char *function, struct inode *dir,
struct ufs_dir_entry *de, struct buffer_head *bh,
unsigned long offset)
{
struct super_block *sb = dir->i_sb;
const char *error_msg = NULL;
int rlen = fs16_to_cpu(sb, de->d_reclen);
if (rlen < UFS_DIR_REC_LEN(1))
error_msg = "reclen is smaller than minimal";
else if (rlen % 4 != 0)
error_msg = "reclen % 4 != 0";
else if (rlen < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)))
error_msg = "reclen is too small for namlen";
else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
error_msg = "directory entry across blocks";
else if (fs32_to_cpu(sb, de->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
UFS_SB(sb)->s_uspi->s_ncg))
error_msg = "inode out of bounds";
if (error_msg != NULL) err = ufs_commit_chunk(page, from, to);
ufs_error (sb, function, "bad entry in directory #%lu, size %Lu: %s - " dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
"offset=%lu, inode=%lu, reclen=%d, namlen=%d",
dir->i_ino, dir->i_size, error_msg, offset,
(unsigned long)fs32_to_cpu(sb, de->d_ino),
rlen, ufs_get_de_namlen(sb, de));
return (error_msg == NULL ? 1 : 0); mark_inode_dirty(dir);
/* OFFSET_CACHE */
out_put:
ufs_put_page(page);
out:
return err;
out_unlock:
unlock_page(page);
goto out_put;
} }
struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct buffer_head **p) static inline unsigned
ufs_validate_entry(struct super_block *sb, char *base,
unsigned offset, unsigned mask)
{ {
int err; struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
struct buffer_head *bh = ufs_bread (dir, 0, 0, &err); struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
struct ufs_dir_entry *res = NULL; while ((char*)p < (char*)de) {
if (p->d_reclen == 0)
if (bh) { break;
res = (struct ufs_dir_entry *) bh->b_data; p = ufs_next_entry(sb, p);
res = (struct ufs_dir_entry *)((char *)res +
fs16_to_cpu(dir->i_sb, res->d_reclen));
}
*p = bh;
return res;
}
ino_t ufs_inode_by_name(struct inode * dir, struct dentry *dentry)
{
ino_t res = 0;
struct ufs_dir_entry * de;
struct buffer_head *bh;
de = ufs_find_entry (dentry, &bh);
if (de) {
res = fs32_to_cpu(dir->i_sb, de->d_ino);
brelse(bh);
} }
return res; return (char *)p - base;
} }
void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
struct buffer_head *bh, struct inode *inode)
{
dir->i_version++;
de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
mark_buffer_dirty(bh);
if (IS_DIRSYNC(dir))
sync_dirty_buffer(bh);
brelse (bh);
}
/* /*
* ufs_add_entry() * This is blatantly stolen from ext2fs
*
* adds a file entry to the specified directory, using the same
* semantics as ufs_find_entry(). It returns NULL if it failed.
*/ */
int ufs_add_link(struct dentry *dentry, struct inode *inode) static int
ufs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{ {
struct super_block * sb; loff_t pos = filp->f_pos;
struct ufs_sb_private_info * uspi; struct inode *inode = filp->f_dentry->d_inode;
unsigned long offset; struct super_block *sb = inode->i_sb;
unsigned fragoff; unsigned int offset = pos & ~PAGE_CACHE_MASK;
unsigned short rec_len; unsigned long n = pos >> PAGE_CACHE_SHIFT;
struct buffer_head * bh; unsigned long npages = ufs_dir_pages(inode);
struct ufs_dir_entry * de, * de1; unsigned chunk_mask = ~(UFS_SECTOR_SIZE - 1);
struct inode *dir = dentry->d_parent->d_inode; int need_revalidate = filp->f_version != inode->i_version;
const char *name = dentry->d_name.name; unsigned flags = UFS_SB(sb)->s_flags;
int namelen = dentry->d_name.len;
int err;
UFSD(("ENTER, name %s, namelen %u\n", name, namelen)) UFSD(("BEGIN"));
sb = dir->i_sb; if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
uspi = UFS_SB(sb)->s_uspi; return 0;
if (!namelen) for ( ; n < npages; n++, offset = 0) {
return -EINVAL; char *kaddr, *limit;
bh = ufs_bread (dir, 0, 0, &err); struct ufs_dir_entry *de;
if (!bh)
return err; struct page *page = ufs_get_page(inode, n);
rec_len = UFS_DIR_REC_LEN(namelen);
offset = 0; if (IS_ERR(page)) {
de = (struct ufs_dir_entry *) bh->b_data; ufs_error(sb, __FUNCTION__,
while (1) { "bad page in #%lu",
if ((char *)de >= UFS_SECTOR_SIZE + bh->b_data) { inode->i_ino);
fragoff = offset & ~uspi->s_fmask; filp->f_pos += PAGE_CACHE_SIZE - offset;
if (fragoff != 0 && fragoff != UFS_SECTOR_SIZE) return -EIO;
ufs_error (sb, "ufs_add_entry", "internal error"
" fragoff %u", fragoff);
if (!fragoff) {
brelse (bh);
bh = ufs_bread (dir, offset >> sb->s_blocksize_bits, 1, &err);
if (!bh)
return err;
} }
if (dir->i_size <= offset) { kaddr = page_address(page);
if (dir->i_size == 0) { if (unlikely(need_revalidate)) {
brelse(bh); if (offset) {
return -ENOENT; offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
} filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
de = (struct ufs_dir_entry *) (bh->b_data + fragoff);
de->d_ino = 0;
de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE);
ufs_set_de_namlen(sb, de, 0);
dir->i_size = offset + UFS_SECTOR_SIZE;
mark_inode_dirty(dir);
} else {
de = (struct ufs_dir_entry *) bh->b_data;
} }
filp->f_version = inode->i_version;
need_revalidate = 0;
}
de = (struct ufs_dir_entry *)(kaddr+offset);
limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
if (de->d_reclen == 0) {
ufs_error(sb, __FUNCTION__,
"zero-length directory entry");
ufs_put_page(page);
return -EIO;
} }
if (!ufs_check_dir_entry ("ufs_add_entry", dir, de, bh, offset)) { if (de->d_ino) {
brelse (bh); int over;
return -ENOENT; unsigned char d_type = DT_UNKNOWN;
offset = (char *)de - kaddr;
UFSD(("filldir(%s,%u)\n", de->d_name,
fs32_to_cpu(sb, de->d_ino)));
UFSD(("namlen %u\n", ufs_get_de_namlen(sb, de)));
if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
d_type = de->d_u.d_44.d_type;
over = filldir(dirent, de->d_name,
ufs_get_de_namlen(sb, de),
(n<<PAGE_CACHE_SHIFT) | offset,
fs32_to_cpu(sb, de->d_ino), d_type);
if (over) {
ufs_put_page(page);
return 0;
} }
if (ufs_match(sb, namelen, name, de)) {
brelse (bh);
return -EEXIST;
} }
if (de->d_ino == 0 && fs16_to_cpu(sb, de->d_reclen) >= rec_len) filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
break;
if (fs16_to_cpu(sb, de->d_reclen) >=
UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)) + rec_len)
break;
offset += fs16_to_cpu(sb, de->d_reclen);
de = (struct ufs_dir_entry *) ((char *) de + fs16_to_cpu(sb, de->d_reclen));
} }
ufs_put_page(page);
if (de->d_ino) {
de1 = (struct ufs_dir_entry *) ((char *) de +
UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)));
de1->d_reclen =
cpu_to_fs16(sb, fs16_to_cpu(sb, de->d_reclen) -
UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)));
de->d_reclen =
cpu_to_fs16(sb, UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)));
de = de1;
} }
de->d_ino = 0;
ufs_set_de_namlen(sb, de, namelen);
memcpy (de->d_name, name, namelen + 1);
de->d_ino = cpu_to_fs32(sb, inode->i_ino);
ufs_set_de_type(sb, de, inode->i_mode);
mark_buffer_dirty(bh);
if (IS_DIRSYNC(dir))
sync_dirty_buffer(bh);
brelse (bh);
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
dir->i_version++;
mark_inode_dirty(dir);
UFSD(("EXIT\n"))
return 0; return 0;
} }
/* /*
* ufs_delete_entry deletes a directory entry by merging it with the * ufs_delete_entry deletes a directory entry by merging it with the
* previous entry. * previous entry.
*/ */
int ufs_delete_entry (struct inode * inode, struct ufs_dir_entry * dir, int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
struct buffer_head * bh ) struct page * page)
{ {
struct super_block * sb; struct super_block *sb = inode->i_sb;
struct ufs_dir_entry * de, * pde; struct address_space *mapping = page->mapping;
unsigned i; char *kaddr = page_address(page);
unsigned from = ((char*)dir - kaddr) & ~(UFS_SECTOR_SIZE - 1);
UFSD(("ENTER\n")) unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
struct ufs_dir_entry *pde = NULL;
struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
int err;
sb = inode->i_sb; UFSD(("ENTER\n"));
i = 0;
pde = NULL;
de = (struct ufs_dir_entry *) bh->b_data;
UFSD(("ino %u, reclen %u, namlen %u, name %s\n", UFSD(("ino %u, reclen %u, namlen %u, name %s\n",
fs32_to_cpu(sb, de->d_ino), fs32_to_cpu(sb, de->d_ino),
fs16_to_cpu(sb, de->d_reclen), fs16_to_cpu(sb, de->d_reclen),
ufs_get_de_namlen(sb, de), de->d_name)) ufs_get_de_namlen(sb, de), de->d_name));
while (i < bh->b_size) { while ((char*)de < (char*)dir) {
if (!ufs_check_dir_entry ("ufs_delete_entry", inode, de, bh, i)) { if (de->d_reclen == 0) {
brelse(bh); ufs_error(inode->i_sb, __FUNCTION__,
return -EIO; "zero-length directory entry");
err = -EIO;
goto out;
}
pde = de;
de = ufs_next_entry(sb, de);
} }
if (de == dir) {
if (pde) if (pde)
fs16_add(sb, &pde->d_reclen, from = (char*)pde - (char*)page_address(page);
fs16_to_cpu(sb, dir->d_reclen)); lock_page(page);
err = mapping->a_ops->prepare_write(NULL, page, from, to);
BUG_ON(err);
if (pde)
pde->d_reclen = cpu_to_fs16(sb, to-from);
dir->d_ino = 0; dir->d_ino = 0;
inode->i_version++; err = ufs_commit_chunk(page, from, to);
inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
mark_inode_dirty(inode); mark_inode_dirty(inode);
mark_buffer_dirty(bh); out:
if (IS_DIRSYNC(inode)) ufs_put_page(page);
sync_dirty_buffer(bh); UFSD(("EXIT\n"));
brelse(bh); return err;
UFSD(("EXIT\n"))
return 0;
}
i += fs16_to_cpu(sb, de->d_reclen);
if (i == UFS_SECTOR_SIZE) pde = NULL;
else pde = de;
de = (struct ufs_dir_entry *)
((char *) de + fs16_to_cpu(sb, de->d_reclen));
if (i == UFS_SECTOR_SIZE && de->d_reclen == 0)
break;
}
UFSD(("EXIT\n"))
brelse(bh);
return -ENOENT;
} }
int ufs_make_empty(struct inode * inode, struct inode *dir) int ufs_make_empty(struct inode * inode, struct inode *dir)
{ {
struct super_block * sb = dir->i_sb; struct super_block * sb = dir->i_sb;
struct buffer_head * dir_block; struct address_space *mapping = inode->i_mapping;
struct page *page = grab_cache_page(mapping, 0);
struct ufs_dir_entry * de; struct ufs_dir_entry * de;
char *base;
int err; int err;
dir_block = ufs_bread (inode, 0, 1, &err); if (!page)
if (!dir_block) return -ENOMEM;
return err; kmap(page);
err = mapping->a_ops->prepare_write(NULL, page, 0, UFS_SECTOR_SIZE);
if (err) {
unlock_page(page);
goto fail;
}
base = (char*)page_address(page);
memset(base, 0, PAGE_CACHE_SIZE);
de = (struct ufs_dir_entry *) base;
inode->i_blocks = sb->s_blocksize / UFS_SECTOR_SIZE;
de = (struct ufs_dir_entry *) dir_block->b_data;
de->d_ino = cpu_to_fs32(sb, inode->i_ino); de->d_ino = cpu_to_fs32(sb, inode->i_ino);
ufs_set_de_type(sb, de, inode->i_mode); ufs_set_de_type(sb, de, inode->i_mode);
ufs_set_de_namlen(sb, de, 1); ufs_set_de_namlen(sb, de, 1);
...@@ -552,72 +594,65 @@ int ufs_make_empty(struct inode * inode, struct inode *dir) ...@@ -552,72 +594,65 @@ int ufs_make_empty(struct inode * inode, struct inode *dir)
de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE - UFS_DIR_REC_LEN(1)); de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE - UFS_DIR_REC_LEN(1));
ufs_set_de_namlen(sb, de, 2); ufs_set_de_namlen(sb, de, 2);
strcpy (de->d_name, ".."); strcpy (de->d_name, "..");
mark_buffer_dirty(dir_block);
brelse (dir_block); err = ufs_commit_chunk(page, 0, UFS_SECTOR_SIZE);
mark_inode_dirty(inode); fail:
return 0; kunmap(page);
page_cache_release(page);
return err;
} }
/* /*
* routine to check that the specified directory is empty (for rmdir) * routine to check that the specified directory is empty (for rmdir)
*/ */
int ufs_empty_dir (struct inode * inode) int ufs_empty_dir(struct inode * inode)
{ {
struct super_block * sb; struct super_block *sb = inode->i_sb;
unsigned long offset; struct page *page = NULL;
struct buffer_head * bh; unsigned long i, npages = ufs_dir_pages(inode);
struct ufs_dir_entry * de, * de1;
int err;
sb = inode->i_sb; for (i = 0; i < npages; i++) {
char *kaddr;
struct ufs_dir_entry *de;
page = ufs_get_page(inode, i);
if (inode->i_size < UFS_DIR_REC_LEN(1) + UFS_DIR_REC_LEN(2) || if (IS_ERR(page))
!(bh = ufs_bread (inode, 0, 0, &err))) {
ufs_warning (inode->i_sb, "empty_dir",
"bad directory (dir #%lu) - no data block",
inode->i_ino);
return 1;
}
de = (struct ufs_dir_entry *) bh->b_data;
de1 = (struct ufs_dir_entry *)
((char *)de + fs16_to_cpu(sb, de->d_reclen));
if (fs32_to_cpu(sb, de->d_ino) != inode->i_ino || de1->d_ino == 0 ||
strcmp (".", de->d_name) || strcmp ("..", de1->d_name)) {
ufs_warning (inode->i_sb, "empty_dir",
"bad directory (dir #%lu) - no `.' or `..'",
inode->i_ino);
return 1;
}
offset = fs16_to_cpu(sb, de->d_reclen) + fs16_to_cpu(sb, de1->d_reclen);
de = (struct ufs_dir_entry *)
((char *)de1 + fs16_to_cpu(sb, de1->d_reclen));
while (offset < inode->i_size ) {
if (!bh || (void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
brelse (bh);
bh = ufs_bread (inode, offset >> sb->s_blocksize_bits, 1, &err);
if (!bh) {
ufs_error (sb, "empty_dir",
"directory #%lu contains a hole at offset %lu",
inode->i_ino, offset);
offset += sb->s_blocksize;
continue; continue;
}
de = (struct ufs_dir_entry *) bh->b_data; kaddr = page_address(page);
} de = (struct ufs_dir_entry *)kaddr;
if (!ufs_check_dir_entry ("empty_dir", inode, de, bh, offset)) { kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
brelse (bh);
return 1; while ((char *)de <= kaddr) {
if (de->d_reclen == 0) {
ufs_error(inode->i_sb, __FUNCTION__,
"zero-length directory entry: "
"kaddr=%p, de=%p\n", kaddr, de);
goto not_empty;
} }
if (de->d_ino) { if (de->d_ino) {
brelse (bh); u16 namelen=ufs_get_de_namlen(sb, de);
return 0; /* check for . and .. */
if (de->d_name[0] != '.')
goto not_empty;
if (namelen > 2)
goto not_empty;
if (namelen < 2) {
if (inode->i_ino !=
fs32_to_cpu(sb, de->d_ino))
goto not_empty;
} else if (de->d_name[1] != '.')
goto not_empty;
}
de = ufs_next_entry(sb, de);
}
ufs_put_page(page);
} }
offset += fs16_to_cpu(sb, de->d_reclen);
de = (struct ufs_dir_entry *)
((char *)de + fs16_to_cpu(sb, de->d_reclen));
}
brelse (bh);
return 1; return 1;
not_empty:
ufs_put_page(page);
return 0;
} }
const struct file_operations ufs_dir_operations = { const struct file_operations ufs_dir_operations = {
......
/* /*
* linux/fs/ufs/namei.c * linux/fs/ufs/namei.c
* *
* Migration to usage of "page cache" on May 2006 by
* Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
*
* Copyright (C) 1998 * Copyright (C) 1998
* Daniel Pirkl <daniel.pirkl@email.cz> * Daniel Pirkl <daniel.pirkl@email.cz>
* Charles University, Faculty of Mathematics and Physics * Charles University, Faculty of Mathematics and Physics
...@@ -28,7 +31,6 @@ ...@@ -28,7 +31,6 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/ufs_fs.h> #include <linux/ufs_fs.h>
#include <linux/smp_lock.h> #include <linux/smp_lock.h>
#include <linux/buffer_head.h>
#include "swab.h" /* will go away - see comment in mknod() */ #include "swab.h" /* will go away - see comment in mknod() */
#include "util.h" #include "util.h"
...@@ -232,19 +234,18 @@ out_dir: ...@@ -232,19 +234,18 @@ out_dir:
goto out; goto out;
} }
static int ufs_unlink(struct inode * dir, struct dentry *dentry) static int ufs_unlink(struct inode *dir, struct dentry *dentry)
{ {
struct inode * inode = dentry->d_inode; struct inode * inode = dentry->d_inode;
struct buffer_head * bh; struct ufs_dir_entry *de;
struct ufs_dir_entry * de; struct page *page;
int err = -ENOENT; int err = -ENOENT;
lock_kernel(); de = ufs_find_entry(dir, dentry, &page);
de = ufs_find_entry (dentry, &bh);
if (!de) if (!de)
goto out; goto out;
err = ufs_delete_entry (dir, de, bh); err = ufs_delete_entry(dir, de, page);
if (err) if (err)
goto out; goto out;
...@@ -252,7 +253,6 @@ static int ufs_unlink(struct inode * dir, struct dentry *dentry) ...@@ -252,7 +253,6 @@ static int ufs_unlink(struct inode * dir, struct dentry *dentry)
inode_dec_link_count(inode); inode_dec_link_count(inode);
err = 0; err = 0;
out: out:
unlock_kernel();
return err; return err;
} }
...@@ -274,42 +274,42 @@ static int ufs_rmdir (struct inode * dir, struct dentry *dentry) ...@@ -274,42 +274,42 @@ static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
return err; return err;
} }
static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry, static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode * new_dir, struct dentry * new_dentry ) struct inode *new_dir, struct dentry *new_dentry)
{ {
struct inode *old_inode = old_dentry->d_inode; struct inode *old_inode = old_dentry->d_inode;
struct inode *new_inode = new_dentry->d_inode; struct inode *new_inode = new_dentry->d_inode;
struct buffer_head *dir_bh = NULL; struct page *dir_page = NULL;
struct ufs_dir_entry *dir_de = NULL; struct ufs_dir_entry * dir_de = NULL;
struct buffer_head *old_bh; struct page *old_page;
struct ufs_dir_entry *old_de; struct ufs_dir_entry *old_de;
int err = -ENOENT; int err = -ENOENT;
lock_kernel(); old_de = ufs_find_entry(old_dir, old_dentry, &old_page);
old_de = ufs_find_entry (old_dentry, &old_bh);
if (!old_de) if (!old_de)
goto out; goto out;
if (S_ISDIR(old_inode->i_mode)) { if (S_ISDIR(old_inode->i_mode)) {
err = -EIO; err = -EIO;
dir_de = ufs_dotdot(old_inode, &dir_bh); dir_de = ufs_dotdot(old_inode, &dir_page);
if (!dir_de) if (!dir_de)
goto out_old; goto out_old;
} }
if (new_inode) { if (new_inode) {
struct buffer_head *new_bh; struct page *new_page;
struct ufs_dir_entry *new_de; struct ufs_dir_entry *new_de;
err = -ENOTEMPTY; err = -ENOTEMPTY;
if (dir_de && !ufs_empty_dir (new_inode)) if (dir_de && !ufs_empty_dir(new_inode))
goto out_dir; goto out_dir;
err = -ENOENT; err = -ENOENT;
new_de = ufs_find_entry (new_dentry, &new_bh); new_de = ufs_find_entry(new_dir, new_dentry, &new_page);
if (!new_de) if (!new_de)
goto out_dir; goto out_dir;
inode_inc_link_count(old_inode); inode_inc_link_count(old_inode);
ufs_set_link(new_dir, new_de, new_bh, old_inode); ufs_set_link(new_dir, new_de, new_page, old_inode);
new_inode->i_ctime = CURRENT_TIME_SEC; new_inode->i_ctime = CURRENT_TIME_SEC;
if (dir_de) if (dir_de)
new_inode->i_nlink--; new_inode->i_nlink--;
...@@ -330,24 +330,32 @@ static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry, ...@@ -330,24 +330,32 @@ static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry,
inode_inc_link_count(new_dir); inode_inc_link_count(new_dir);
} }
ufs_delete_entry (old_dir, old_de, old_bh); /*
* Like most other Unix systems, set the ctime for inodes on a
* rename.
* inode_dec_link_count() will mark the inode dirty.
*/
old_inode->i_ctime = CURRENT_TIME_SEC;
ufs_delete_entry(old_dir, old_de, old_page);
inode_dec_link_count(old_inode); inode_dec_link_count(old_inode);
if (dir_de) { if (dir_de) {
ufs_set_link(old_inode, dir_de, dir_bh, new_dir); ufs_set_link(old_inode, dir_de, dir_page, new_dir);
inode_dec_link_count(old_dir); inode_dec_link_count(old_dir);
} }
unlock_kernel();
return 0; return 0;
out_dir: out_dir:
if (dir_de) if (dir_de) {
brelse(dir_bh); kunmap(dir_page);
page_cache_release(dir_page);
}
out_old: out_old:
brelse (old_bh); kunmap(old_page);
page_cache_release(old_page);
out: out:
unlock_kernel();
return err; return err;
} }
......
...@@ -887,11 +887,12 @@ extern struct inode_operations ufs_dir_inode_operations; ...@@ -887,11 +887,12 @@ extern struct inode_operations ufs_dir_inode_operations;
extern int ufs_add_link (struct dentry *, struct inode *); extern int ufs_add_link (struct dentry *, struct inode *);
extern ino_t ufs_inode_by_name(struct inode *, struct dentry *); extern ino_t ufs_inode_by_name(struct inode *, struct dentry *);
extern int ufs_make_empty(struct inode *, struct inode *); extern int ufs_make_empty(struct inode *, struct inode *);
extern struct ufs_dir_entry * ufs_find_entry (struct dentry *, struct buffer_head **); extern struct ufs_dir_entry *ufs_find_entry(struct inode *, struct dentry *, struct page **);
extern int ufs_delete_entry (struct inode *, struct ufs_dir_entry *, struct buffer_head *); extern int ufs_delete_entry(struct inode *, struct ufs_dir_entry *, struct page *);
extern int ufs_empty_dir (struct inode *); extern int ufs_empty_dir (struct inode *);
extern struct ufs_dir_entry * ufs_dotdot (struct inode *, struct buffer_head **); extern struct ufs_dir_entry *ufs_dotdot(struct inode *, struct page **);
extern void ufs_set_link(struct inode *, struct ufs_dir_entry *, struct buffer_head *, struct inode *); extern void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
struct page *page, struct inode *inode);
/* file.c */ /* file.c */
extern struct inode_operations ufs_file_inode_operations; extern struct inode_operations ufs_file_inode_operations;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment