Skip to content

Commit 22c44fe

Browse files
author
Josef Bacik
committed
Btrfs: deal with enospc from dirtying inodes properly
Now that we're properly keeping track of delayed inode space we've been getting a lot of warnings out of btrfs_dirty_inode() when running xfstest 83. This is because a bunch of people call mark_inode_dirty, which is void so we can't return ENOSPC. This needs to be fixed in a few areas 1) file_update_time - this updates the mtime and such when writing to a file, which will call mark_inode_dirty. So copy file_update_time into btrfs so we can call btrfs_dirty_inode directly and return an error if we get one appropriately. 2) fix symlinks to use btrfs_setattr for ->setattr. For some reason we weren't setting ->setattr for symlinks, even though we should have been. This catches one of the cases where we were getting errors in mark_inode_dirty. 3) Fix btrfs_setattr and btrfs_setsize to call btrfs_dirty_inode directly instead of mark_inode_dirty. This lets us return errors properly for truncate and chown/anything related to setattr. 4) Add a new btrfs_fs_dirty_inode which will just call btrfs_dirty_inode and print an error if we have one. The only remaining user we can't control for this is touch_atime(), but we don't really want to keep people from walking down the tree if we don't have space to save the atime update, so just complain but don't worry about it. With this patch xfstests 83 complains a handful of times instead of hundreds of times. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com>
1 parent 0dc3b84 commit 22c44fe

4 files changed

Lines changed: 80 additions & 22 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2692,7 +2692,8 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf);
26922692
int btrfs_readpage(struct file *file, struct page *page);
26932693
void btrfs_evict_inode(struct inode *inode);
26942694
int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc);
2695-
void btrfs_dirty_inode(struct inode *inode, int flags);
2695+
int btrfs_dirty_inode(struct inode *inode);
2696+
int btrfs_update_time(struct file *file);
26962697
struct inode *btrfs_alloc_inode(struct super_block *sb);
26972698
void btrfs_destroy_inode(struct inode *inode);
26982699
int btrfs_drop_inode(struct inode *inode);

‎fs/btrfs/file.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,11 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
13871387
goto out;
13881388
}
13891389

1390-
file_update_time(file);
1390+
err = btrfs_update_time(file);
1391+
if (err) {
1392+
mutex_unlock(&inode->i_mutex);
1393+
goto out;
1394+
}
13911395
BTRFS_I(inode)->sequence++;
13921396

13931397
start_pos = round_down(pos, root->sectorsize);

‎fs/btrfs/inode.c‎

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/falloc.h>
3939
#include <linux/slab.h>
4040
#include <linux/ratelimit.h>
41+
#include <linux/mount.h>
4142
#include "compat.h"
4243
#include "ctree.h"
4344
#include "disk-io.h"
@@ -3386,7 +3387,7 @@ static int btrfs_setsize(struct inode *inode, loff_t newsize)
33863387
return ret;
33873388
}
33883389

3389-
mark_inode_dirty(inode);
3390+
ret = btrfs_dirty_inode(inode);
33903391
} else {
33913392

33923393
/*
@@ -3426,9 +3427,9 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
34263427

34273428
if (attr->ia_valid) {
34283429
setattr_copy(inode, attr);
3429-
mark_inode_dirty(inode);
3430+
err = btrfs_dirty_inode(inode);
34303431

3431-
if (attr->ia_valid & ATTR_MODE)
3432+
if (!err && attr->ia_valid & ATTR_MODE)
34323433
err = btrfs_acl_chmod(inode);
34333434
}
34343435

@@ -4204,42 +4205,80 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
42044205
* FIXME, needs more benchmarking...there are no reasons other than performance
42054206
* to keep or drop this code.
42064207
*/
4207-
void btrfs_dirty_inode(struct inode *inode, int flags)
4208+
int btrfs_dirty_inode(struct inode *inode)
42084209
{
42094210
struct btrfs_root *root = BTRFS_I(inode)->root;
42104211
struct btrfs_trans_handle *trans;
42114212
int ret;
42124213

42134214
if (BTRFS_I(inode)->dummy_inode)
4214-
return;
4215+
return 0;
42154216

42164217
trans = btrfs_join_transaction(root);
4217-
BUG_ON(IS_ERR(trans));
4218+
if (IS_ERR(trans))
4219+
return PTR_ERR(trans);
42184220

42194221
ret = btrfs_update_inode(trans, root, inode);
42204222
if (ret && ret == -ENOSPC) {
42214223
/* whoops, lets try again with the full transaction */
42224224
btrfs_end_transaction(trans, root);
42234225
trans = btrfs_start_transaction(root, 1);
4224-
if (IS_ERR(trans)) {
4225-
printk_ratelimited(KERN_ERR "btrfs: fail to "
4226-
"dirty inode %llu error %ld\n",
4227-
(unsigned long long)btrfs_ino(inode),
4228-
PTR_ERR(trans));
4229-
return;
4230-
}
4226+
if (IS_ERR(trans))
4227+
return PTR_ERR(trans);
42314228

42324229
ret = btrfs_update_inode(trans, root, inode);
4233-
if (ret) {
4234-
printk_ratelimited(KERN_ERR "btrfs: fail to "
4235-
"dirty inode %llu error %d\n",
4236-
(unsigned long long)btrfs_ino(inode),
4237-
ret);
4238-
}
42394230
}
42404231
btrfs_end_transaction(trans, root);
42414232
if (BTRFS_I(inode)->delayed_node)
42424233
btrfs_balance_delayed_items(root);
4234+
4235+
return ret;
4236+
}
4237+
4238+
/*
4239+
* This is a copy of file_update_time. We need this so we can return error on
4240+
* ENOSPC for updating the inode in the case of file write and mmap writes.
4241+
*/
4242+
int btrfs_update_time(struct file *file)
4243+
{
4244+
struct inode *inode = file->f_path.dentry->d_inode;
4245+
struct timespec now;
4246+
int ret;
4247+
enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4248+
4249+
/* First try to exhaust all avenues to not sync */
4250+
if (IS_NOCMTIME(inode))
4251+
return 0;
4252+
4253+
now = current_fs_time(inode->i_sb);
4254+
if (!timespec_equal(&inode->i_mtime, &now))
4255+
sync_it = S_MTIME;
4256+
4257+
if (!timespec_equal(&inode->i_ctime, &now))
4258+
sync_it |= S_CTIME;
4259+
4260+
if (IS_I_VERSION(inode))
4261+
sync_it |= S_VERSION;
4262+
4263+
if (!sync_it)
4264+
return 0;
4265+
4266+
/* Finally allowed to write? Takes lock. */
4267+
if (mnt_want_write_file(file))
4268+
return 0;
4269+
4270+
/* Only change inode inside the lock region */
4271+
if (sync_it & S_VERSION)
4272+
inode_inc_iversion(inode);
4273+
if (sync_it & S_CTIME)
4274+
inode->i_ctime = now;
4275+
if (sync_it & S_MTIME)
4276+
inode->i_mtime = now;
4277+
ret = btrfs_dirty_inode(inode);
4278+
if (!ret)
4279+
mark_inode_dirty_sync(inode);
4280+
mnt_drop_write(file->f_path.mnt);
4281+
return ret;
42434282
}
42444283

42454284
/*
@@ -6304,6 +6343,8 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
63046343
u64 page_end;
63056344

63066345
ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6346+
if (!ret)
6347+
ret = btrfs_update_time(vma->vm_file);
63076348
if (ret) {
63086349
if (ret == -ENOMEM)
63096350
ret = VM_FAULT_OOM;
@@ -7353,6 +7394,7 @@ static const struct inode_operations btrfs_symlink_inode_operations = {
73537394
.follow_link = page_follow_link_light,
73547395
.put_link = page_put_link,
73557396
.getattr = btrfs_getattr,
7397+
.setattr = btrfs_setattr,
73567398
.permission = btrfs_permission,
73577399
.setxattr = btrfs_setxattr,
73587400
.getxattr = btrfs_getxattr,

‎fs/btrfs/super.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/slab.h>
4242
#include <linux/cleancache.h>
4343
#include <linux/mnt_namespace.h>
44+
#include <linux/ratelimit.h>
4445
#include "compat.h"
4546
#include "delayed-inode.h"
4647
#include "ctree.h"
@@ -1290,14 +1291,24 @@ static int btrfs_unfreeze(struct super_block *sb)
12901291
return 0;
12911292
}
12921293

1294+
static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
1295+
{
1296+
int ret;
1297+
1298+
ret = btrfs_dirty_inode(inode);
1299+
if (ret)
1300+
printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
1301+
"error %d\n", btrfs_ino(inode), ret);
1302+
}
1303+
12931304
static const struct super_operations btrfs_super_ops = {
12941305
.drop_inode = btrfs_drop_inode,
12951306
.evict_inode = btrfs_evict_inode,
12961307
.put_super = btrfs_put_super,
12971308
.sync_fs = btrfs_sync_fs,
12981309
.show_options = btrfs_show_options,
12991310
.write_inode = btrfs_write_inode,
1300-
.dirty_inode = btrfs_dirty_inode,
1311+
.dirty_inode = btrfs_fs_dirty_inode,
13011312
.alloc_inode = btrfs_alloc_inode,
13021313
.destroy_inode = btrfs_destroy_inode,
13031314
.statfs = btrfs_statfs,

0 commit comments

Comments
 (0)