new helper: file_inode(file)
[deliverable/linux.git] / fs / f2fs / dir.c
index 5ec7a06120e18abd33e523b04851c2426f360008..fda0bcc0907f66912b64a932edeefa87dd91f35b 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * fs/f2fs/dir.c
  *
  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
@@ -11,6 +11,7 @@
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
 #include "f2fs.h"
+#include "node.h"
 #include "acl.h"
 
 static unsigned long dir_blocks(struct inode *inode)
@@ -74,7 +75,7 @@ static unsigned long dir_block_index(unsigned int level, unsigned int idx)
        return bidx;
 }
 
-static bool early_match_name(const char *name, int namelen,
+static bool early_match_name(const char *name, size_t namelen,
                        f2fs_hash_t namehash, struct f2fs_dir_entry *de)
 {
        if (le16_to_cpu(de->name_len) != namelen)
@@ -87,7 +88,7 @@ static bool early_match_name(const char *name, int namelen,
 }
 
 static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
-                       const char *name, int namelen, int *max_slots,
+                       const char *name, size_t namelen, int *max_slots,
                        f2fs_hash_t namehash, struct page **res_page)
 {
        struct f2fs_dir_entry *de;
@@ -99,8 +100,7 @@ static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
                                        NR_DENTRY_IN_BLOCK, 0);
        while (bit_pos < NR_DENTRY_IN_BLOCK) {
                de = &dentry_blk->dentry[bit_pos];
-               slots = (le16_to_cpu(de->name_len) + F2FS_NAME_LEN - 1) /
-                                                       F2FS_NAME_LEN;
+               slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
 
                if (early_match_name(name, namelen, namehash, de)) {
                        if (!memcmp(dentry_blk->filename[bit_pos],
@@ -127,10 +127,10 @@ found:
 }
 
 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
-               unsigned int level, const char *name, int namelen,
+               unsigned int level, const char *name, size_t namelen,
                        f2fs_hash_t namehash, struct page **res_page)
 {
-       int s = (namelen + F2FS_NAME_LEN - 1) / F2FS_NAME_LEN;
+       int s = GET_DENTRY_SLOTS(namelen);
        unsigned int nbucket, nblock;
        unsigned int bidx, end_block;
        struct page *dentry_page;
@@ -182,7 +182,7 @@ struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
                        struct qstr *child, struct page **res_page)
 {
        const char *name = child->name;
-       int namelen = child->len;
+       size_t namelen = child->len;
        unsigned long npages = dir_blocks(dir);
        struct f2fs_dir_entry *de = NULL;
        f2fs_hash_t name_hash;
@@ -257,13 +257,16 @@ void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
        set_page_dirty(page);
        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
        mark_inode_dirty(dir);
+
+       /* update parent inode number before releasing dentry page */
+       F2FS_I(inode)->i_pino = dir->i_ino;
+
        f2fs_put_page(page, 1);
        mutex_unlock_op(sbi, DENTRY_OPS);
 }
 
 void init_dent_inode(struct dentry *dentry, struct page *ipage)
 {
-       struct inode *dir = dentry->d_parent->d_inode;
        struct f2fs_node *rn;
 
        if (IS_ERR(ipage))
@@ -273,7 +276,6 @@ void init_dent_inode(struct dentry *dentry, struct page *ipage)
 
        /* copy dentry info. to this inode page */
        rn = (struct f2fs_node *)page_address(ipage);
-       rn->i.i_pino = cpu_to_le32(dir->i_ino);
        rn->i.i_namelen = cpu_to_le32(dentry->d_name.len);
        memcpy(rn->i.i_name, dentry->d_name.name, dentry->d_name.len);
        set_page_dirty(ipage);
@@ -307,6 +309,7 @@ static int init_inode_metadata(struct inode *inode, struct dentry *dentry)
                ipage = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
                if (IS_ERR(ipage))
                        return PTR_ERR(ipage);
+               set_cold_node(inode, ipage);
                init_dent_inode(dentry, ipage);
                f2fs_put_page(ipage, 1);
        }
@@ -380,10 +383,10 @@ int f2fs_add_link(struct dentry *dentry, struct inode *inode)
        struct inode *dir = dentry->d_parent->d_inode;
        struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
        const char *name = dentry->d_name.name;
-       int namelen = dentry->d_name.len;
+       size_t namelen = dentry->d_name.len;
        struct page *dentry_page = NULL;
        struct f2fs_dentry_block *dentry_blk = NULL;
-       int slots = (namelen + F2FS_NAME_LEN - 1) / F2FS_NAME_LEN;
+       int slots = GET_DENTRY_SLOTS(namelen);
        int err = 0;
        int i;
 
@@ -445,7 +448,11 @@ add_dentry:
        for (i = 0; i < slots; i++)
                test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
        set_page_dirty(dentry_page);
+
        update_parent_metadata(dir, inode, current_depth);
+
+       /* update parent inode number before releasing dentry page */
+       F2FS_I(inode)->i_pino = dir->i_ino;
 fail:
        kunmap(dentry_page);
        f2fs_put_page(dentry_page, 1);
@@ -453,7 +460,7 @@ fail:
        return err;
 }
 
-/**
+/*
  * It only removes the dentry from the dentry page,corresponding name
  * entry in name page does not need to be touched during deletion.
  */
@@ -465,8 +472,7 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
        struct address_space *mapping = page->mapping;
        struct inode *dir = mapping->host;
        struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
-       int slots = (le16_to_cpu(dentry->name_len) + F2FS_NAME_LEN - 1) /
-                                                       F2FS_NAME_LEN;
+       int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
        void *kaddr = page_address(page);
        int i;
 
@@ -509,17 +515,14 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
        }
 
        if (bit_pos == NR_DENTRY_IN_BLOCK) {
-               loff_t page_offset;
                truncate_hole(dir, page->index, page->index + 1);
                clear_page_dirty_for_io(page);
                ClearPageUptodate(page);
                dec_page_count(sbi, F2FS_DIRTY_DENTS);
                inode_dec_dirty_dents(dir);
-               page_offset = page->index << PAGE_CACHE_SHIFT;
-               f2fs_put_page(page, 1);
-       } else {
-               f2fs_put_page(page, 1);
        }
+       f2fs_put_page(page, 1);
+
        mutex_unlock_op(sbi, DENTRY_OPS);
 }
 
@@ -539,13 +542,13 @@ int f2fs_make_empty(struct inode *inode, struct inode *parent)
 
        de = &dentry_blk->dentry[0];
        de->name_len = cpu_to_le16(1);
-       de->hash_code = 0;
+       de->hash_code = f2fs_dentry_hash(".", 1);
        de->ino = cpu_to_le32(inode->i_ino);
        memcpy(dentry_blk->filename[0], ".", 1);
        set_de_type(de, inode);
 
        de = &dentry_blk->dentry[1];
-       de->hash_code = 0;
+       de->hash_code = f2fs_dentry_hash("..", 2);
        de->name_len = cpu_to_le16(2);
        de->ino = cpu_to_le32(parent->i_ino);
        memcpy(dentry_blk->filename[1], "..", 2);
@@ -600,7 +603,7 @@ bool f2fs_empty_dir(struct inode *dir)
 static int f2fs_readdir(struct file *file, void *dirent, filldir_t filldir)
 {
        unsigned long pos = file->f_pos;
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file_inode(file);
        unsigned long npages = dir_blocks(inode);
        unsigned char *types = NULL;
        unsigned int bit_pos = 0, start_bit_pos = 0;
@@ -644,8 +647,7 @@ static int f2fs_readdir(struct file *file, void *dirent, filldir_t filldir)
                                file->f_pos += bit_pos - start_bit_pos;
                                goto success;
                        }
-                       slots = (le16_to_cpu(de->name_len) + F2FS_NAME_LEN - 1)
-                                                               / F2FS_NAME_LEN;
+                       slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
                        bit_pos += slots;
                }
                bit_pos = 0;
This page took 0.0281979999999999 seconds and 5 git commands to generate.