Fixes and cleanups for earlyprintk aka boot console
[safe/jmp/linux-2.6] / fs / ocfs2 / dir.c
index 3d494d1..c441ef1 100644 (file)
@@ -74,14 +74,15 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
 {
        int error = 0;
-       unsigned long offset, blk;
-       int i, num, stored;
+       unsigned long offset, blk, last_ra_blk = 0;
+       int i, stored;
        struct buffer_head * bh, * tmp;
        struct ocfs2_dir_entry * de;
        int err;
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        struct super_block * sb = inode->i_sb;
-       int have_disk_lock = 0;
+       unsigned int ra_sectors = 16;
+       int lock_level = 0;
 
        mlog_entry("dirino=%llu\n",
                   (unsigned long long)OCFS2_I(inode)->ip_blkno);
@@ -89,15 +90,22 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
        stored = 0;
        bh = NULL;
 
-       error = ocfs2_meta_lock(inode, NULL, NULL, 0);
+       error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
+       if (lock_level && error >= 0) {
+               /* We release EX lock which used to update atime
+                * and get PR lock again to reduce contention
+                * on commonly accessed directories. */
+               ocfs2_meta_unlock(inode, 1);
+               lock_level = 0;
+               error = ocfs2_meta_lock(inode, NULL, 0);
+       }
        if (error < 0) {
                if (error != -ENOENT)
                        mlog_errno(error);
                /* we haven't got any yet, so propagate the error. */
                stored = error;
-               goto bail;
+               goto bail_nolock;
        }
-       have_disk_lock = 1;
 
        offset = filp->f_pos & (sb->s_blocksize - 1);
 
@@ -113,16 +121,21 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
                        continue;
                }
 
-               /*
-                * Do the readahead (8k)
-                */
-               if (!offset) {
-                       for (i = 16 >> (sb->s_blocksize_bits - 9), num = 0;
+               /* The idea here is to begin with 8k read-ahead and to stay
+                * 4k ahead of our current position.
+                *
+                * TODO: Use the pagecache for this. We just need to
+                * make sure it's cluster-safe... */
+               if (!last_ra_blk
+                   || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
+                       for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
                             i > 0; i--) {
                                tmp = ocfs2_bread(inode, ++blk, &err, 1);
                                if (tmp)
                                        brelse(tmp);
                        }
+                       last_ra_blk = blk;
+                       ra_sectors = 8;
                }
 
 revalidate:
@@ -194,9 +207,9 @@ revalidate:
 
        stored = 0;
 bail:
-       if (have_disk_lock)
-               ocfs2_meta_unlock(inode, 0);
+       ocfs2_meta_unlock(inode, lock_level);
 
+bail_nolock:
        mlog_exit(stored);
 
        return stored;
@@ -336,7 +349,7 @@ int ocfs2_empty_dir(struct inode *inode)
 
 /* returns a bh of the 1st new block in the allocation. */
 int ocfs2_do_extend_dir(struct super_block *sb,
-                       struct ocfs2_journal_handle *handle,
+                       handle_t *handle,
                        struct inode *dir,
                        struct buffer_head *parent_fe_bh,
                        struct ocfs2_alloc_context *data_ac,
@@ -345,15 +358,17 @@ int ocfs2_do_extend_dir(struct super_block *sb,
 {
        int status;
        int extend;
-       u64 p_blkno;
+       u64 p_blkno, v_blkno;
 
        spin_lock(&OCFS2_I(dir)->ip_lock);
        extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
        spin_unlock(&OCFS2_I(dir)->ip_lock);
 
        if (extend) {
-               status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
-                                                   parent_fe_bh, handle,
+               u32 offset = OCFS2_I(dir)->ip_clusters;
+
+               status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
+                                                   1, parent_fe_bh, handle,
                                                    data_ac, meta_ac, NULL);
                BUG_ON(status == -EAGAIN);
                if (status < 0) {
@@ -362,9 +377,8 @@ int ocfs2_do_extend_dir(struct super_block *sb,
                }
        }
 
-       status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
-                                                  (sb->s_blocksize_bits - 9)),
-                                            1, &p_blkno, NULL);
+       v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
+       status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -389,12 +403,12 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                            struct buffer_head **new_de_bh)
 {
        int status = 0;
-       int credits, num_free_extents;
+       int credits, num_free_extents, drop_alloc_sem = 0;
        loff_t dir_i_size;
        struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
        struct ocfs2_alloc_context *data_ac = NULL;
        struct ocfs2_alloc_context *meta_ac = NULL;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct buffer_head *new_bh = NULL;
        struct ocfs2_dir_entry * de;
        struct super_block *sb = osb->sb;
@@ -405,13 +419,6 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        mlog(0, "extending dir %llu (i_size = %lld)\n",
             (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
 
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
        /* dir->i_size is always block aligned. */
        spin_lock(&OCFS2_I(dir)->ip_lock);
        if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
@@ -424,8 +431,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                }
 
                if (!num_free_extents) {
-                       status = ocfs2_reserve_new_metadata(osb, handle,
-                                                           fe, &meta_ac);
+                       status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
                        if (status < 0) {
                                if (status != -ENOSPC)
                                        mlog_errno(status);
@@ -433,7 +439,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                        }
                }
 
-               status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
+               status = ocfs2_reserve_clusters(osb, 1, &data_ac);
                if (status < 0) {
                        if (status != -ENOSPC)
                                mlog_errno(status);
@@ -446,7 +452,10 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
        }
 
-       handle = ocfs2_start_trans(osb, handle, credits);
+       down_write(&OCFS2_I(dir)->ip_alloc_sem);
+       drop_alloc_sem = 1;
+
+       handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -481,7 +490,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
 
        dir_i_size += dir->i_sb->s_blocksize;
        i_size_write(dir, dir_i_size);
-       dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
+       dir->i_blocks = ocfs2_inode_sector_count(dir);
        status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
        if (status < 0) {
                mlog_errno(status);
@@ -491,8 +500,10 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        *new_de_bh = new_bh;
        get_bh(*new_de_bh);
 bail:
+       if (drop_alloc_sem)
+               up_write(&OCFS2_I(dir)->ip_alloc_sem);
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
 
        if (data_ac)
                ocfs2_free_alloc_context(data_ac);