Skip to content

Commit f15ecfe

Browse files
Kirill TkhaiMiklos Szeredi
authored andcommitted
fuse: Introduce fi->lock to protect write related fields
To minimize contention of fc->lock, this patch introduces a new spinlock for protection fuse_inode metadata: fuse_inode: writectr writepages write_files queued_writes attr_version inode: i_size i_nlink i_mtime i_ctime Also, it protects the fields changed in fuse_change_attributes_common() (too many to list). Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 4510d86 commit f15ecfe

4 files changed

Lines changed: 70 additions & 62 deletions

File tree

‎fs/fuse/dir.c‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
658658
struct inode *inode = d_inode(entry);
659659
struct fuse_inode *fi = get_fuse_inode(inode);
660660

661-
spin_lock(&fc->lock);
661+
spin_lock(&fi->lock);
662662
fi->attr_version = atomic64_inc_return(&fc->attr_version);
663663
/*
664664
* If i_nlink == 0 then unlink doesn't make sense, yet this can
@@ -668,7 +668,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
668668
*/
669669
if (inode->i_nlink > 0)
670670
drop_nlink(inode);
671-
spin_unlock(&fc->lock);
671+
spin_unlock(&fi->lock);
672672
fuse_invalidate_attr(inode);
673673
fuse_dir_changed(dir);
674674
fuse_invalidate_entry_cache(entry);
@@ -812,10 +812,10 @@ static int fuse_link(struct dentry *entry, struct inode *newdir,
812812
if (!err) {
813813
struct fuse_inode *fi = get_fuse_inode(inode);
814814

815-
spin_lock(&fc->lock);
815+
spin_lock(&fi->lock);
816816
fi->attr_version = atomic64_inc_return(&fc->attr_version);
817817
inc_nlink(inode);
818-
spin_unlock(&fc->lock);
818+
spin_unlock(&fi->lock);
819819
fuse_invalidate_attr(inode);
820820
fuse_update_ctime(inode);
821821
} else if (err == -EINTR) {
@@ -1343,15 +1343,14 @@ static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
13431343
*/
13441344
void fuse_set_nowrite(struct inode *inode)
13451345
{
1346-
struct fuse_conn *fc = get_fuse_conn(inode);
13471346
struct fuse_inode *fi = get_fuse_inode(inode);
13481347

13491348
BUG_ON(!inode_is_locked(inode));
13501349

1351-
spin_lock(&fc->lock);
1350+
spin_lock(&fi->lock);
13521351
BUG_ON(fi->writectr < 0);
13531352
fi->writectr += FUSE_NOWRITE;
1354-
spin_unlock(&fc->lock);
1353+
spin_unlock(&fi->lock);
13551354
wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
13561355
}
13571356

@@ -1372,11 +1371,11 @@ static void __fuse_release_nowrite(struct inode *inode)
13721371

13731372
void fuse_release_nowrite(struct inode *inode)
13741373
{
1375-
struct fuse_conn *fc = get_fuse_conn(inode);
1374+
struct fuse_inode *fi = get_fuse_inode(inode);
13761375

1377-
spin_lock(&fc->lock);
1376+
spin_lock(&fi->lock);
13781377
__fuse_release_nowrite(inode);
1379-
spin_unlock(&fc->lock);
1378+
spin_unlock(&fi->lock);
13801379
}
13811380

13821381
static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
@@ -1511,7 +1510,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
15111510
goto error;
15121511
}
15131512

1514-
spin_lock(&fc->lock);
1513+
spin_lock(&fi->lock);
15151514
/* the kernel maintains i_mtime locally */
15161515
if (trust_local_cmtime) {
15171516
if (attr->ia_valid & ATTR_MTIME)
@@ -1529,10 +1528,10 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
15291528
i_size_write(inode, outarg.attr.size);
15301529

15311530
if (is_truncate) {
1532-
/* NOTE: this may release/reacquire fc->lock */
1531+
/* NOTE: this may release/reacquire fi->lock */
15331532
__fuse_release_nowrite(inode);
15341533
}
1535-
spin_unlock(&fc->lock);
1534+
spin_unlock(&fi->lock);
15361535

15371536
/*
15381537
* Only call invalidate_inode_pages2() after removing

‎fs/fuse/file.c‎

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,16 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
159159
static void fuse_link_write_file(struct file *file)
160160
{
161161
struct inode *inode = file_inode(file);
162-
struct fuse_conn *fc = get_fuse_conn(inode);
163162
struct fuse_inode *fi = get_fuse_inode(inode);
164163
struct fuse_file *ff = file->private_data;
165164
/*
166165
* file may be written through mmap, so chain it onto the
167166
* inodes's write_file list
168167
*/
169-
spin_lock(&fc->lock);
168+
spin_lock(&fi->lock);
170169
if (list_empty(&ff->write_entry))
171170
list_add(&ff->write_entry, &fi->write_files);
172-
spin_unlock(&fc->lock);
171+
spin_unlock(&fi->lock);
173172
}
174173

175174
void fuse_finish_open(struct inode *inode, struct file *file)
@@ -186,10 +185,10 @@ void fuse_finish_open(struct inode *inode, struct file *file)
186185
if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
187186
struct fuse_inode *fi = get_fuse_inode(inode);
188187

189-
spin_lock(&fc->lock);
188+
spin_lock(&fi->lock);
190189
fi->attr_version = atomic64_inc_return(&fc->attr_version);
191190
i_size_write(inode, 0);
192-
spin_unlock(&fc->lock);
191+
spin_unlock(&fi->lock);
193192
fuse_invalidate_attr(inode);
194193
if (fc->writeback_cache)
195194
file_update_time(file);
@@ -231,8 +230,13 @@ static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
231230
struct fuse_req *req = ff->reserved_req;
232231
struct fuse_release_in *inarg = &req->misc.release.in;
233232

233+
/* Inode is NULL on error path of fuse_create_open() */
234+
if (likely(fi)) {
235+
spin_lock(&fi->lock);
236+
list_del(&ff->write_entry);
237+
spin_unlock(&fi->lock);
238+
}
234239
spin_lock(&fc->lock);
235-
list_del(&ff->write_entry);
236240
if (!RB_EMPTY_NODE(&ff->polled_node))
237241
rb_erase(&ff->polled_node, &fc->polled_files);
238242
spin_unlock(&fc->lock);
@@ -358,13 +362,12 @@ static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
358362
static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
359363
pgoff_t idx_to)
360364
{
361-
struct fuse_conn *fc = get_fuse_conn(inode);
362365
struct fuse_inode *fi = get_fuse_inode(inode);
363366
bool found;
364367

365-
spin_lock(&fc->lock);
368+
spin_lock(&fi->lock);
366369
found = fuse_find_writeback(fi, idx_from, idx_to);
367-
spin_unlock(&fc->lock);
370+
spin_unlock(&fi->lock);
368371

369372
return found;
370373
}
@@ -607,9 +610,9 @@ static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
607610
struct fuse_conn *fc = get_fuse_conn(inode);
608611
struct fuse_inode *fi = get_fuse_inode(inode);
609612

610-
spin_lock(&fc->lock);
613+
spin_lock(&fi->lock);
611614
fi->attr_version = atomic64_inc_return(&fc->attr_version);
612-
spin_unlock(&fc->lock);
615+
spin_unlock(&fi->lock);
613616
}
614617

615618
io->iocb->ki_complete(io->iocb, res, 0);
@@ -684,13 +687,13 @@ static void fuse_read_update_size(struct inode *inode, loff_t size,
684687
struct fuse_conn *fc = get_fuse_conn(inode);
685688
struct fuse_inode *fi = get_fuse_inode(inode);
686689

687-
spin_lock(&fc->lock);
690+
spin_lock(&fi->lock);
688691
if (attr_ver == fi->attr_version && size < inode->i_size &&
689692
!test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
690693
fi->attr_version = atomic64_inc_return(&fc->attr_version);
691694
i_size_write(inode, size);
692695
}
693-
spin_unlock(&fc->lock);
696+
spin_unlock(&fi->lock);
694697
}
695698

696699
static void fuse_short_read(struct fuse_req *req, struct inode *inode,
@@ -1005,13 +1008,13 @@ bool fuse_write_update_size(struct inode *inode, loff_t pos)
10051008
struct fuse_inode *fi = get_fuse_inode(inode);
10061009
bool ret = false;
10071010

1008-
spin_lock(&fc->lock);
1011+
spin_lock(&fi->lock);
10091012
fi->attr_version = atomic64_inc_return(&fc->attr_version);
10101013
if (pos > inode->i_size) {
10111014
i_size_write(inode, pos);
10121015
ret = true;
10131016
}
1014-
spin_unlock(&fc->lock);
1017+
spin_unlock(&fi->lock);
10151018

10161019
return ret;
10171020
}
@@ -1490,21 +1493,18 @@ static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
14901493
wake_up(&fi->page_waitq);
14911494
}
14921495

1493-
/* Called under fc->lock, may release and reacquire it */
1496+
/* Called under fi->lock, may release and reacquire it */
14941497
static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
14951498
loff_t size)
1496-
__releases(fc->lock)
1497-
__acquires(fc->lock)
1499+
__releases(fi->lock)
1500+
__acquires(fi->lock)
14981501
{
14991502
struct fuse_req *aux, *next;
15001503
struct fuse_inode *fi = get_fuse_inode(req->inode);
15011504
struct fuse_write_in *inarg = &req->misc.write.in;
15021505
__u64 data_size = req->num_pages * PAGE_SIZE;
15031506
bool queued;
15041507

1505-
if (!fc->connected)
1506-
goto out_free;
1507-
15081508
if (inarg->offset + data_size <= size) {
15091509
inarg->size = data_size;
15101510
} else if (inarg->offset < size) {
@@ -1515,14 +1515,17 @@ __acquires(fc->lock)
15151515
}
15161516

15171517
req->in.args[1].size = inarg->size;
1518-
fi->writectr++;
15191518
queued = fuse_request_queue_background(fc, req);
1520-
WARN_ON(!queued);
1519+
/* Fails on broken connection only */
1520+
if (unlikely(!queued))
1521+
goto out_free;
1522+
1523+
fi->writectr++;
15211524
return;
15221525

15231526
out_free:
15241527
fuse_writepage_finish(fc, req);
1525-
spin_unlock(&fc->lock);
1528+
spin_unlock(&fi->lock);
15261529

15271530
/* After fuse_writepage_finish() aux request list is private */
15281531
for (aux = req->misc.write.next; aux; aux = next) {
@@ -1534,18 +1537,18 @@ __acquires(fc->lock)
15341537

15351538
fuse_writepage_free(fc, req);
15361539
fuse_put_request(fc, req);
1537-
spin_lock(&fc->lock);
1540+
spin_lock(&fi->lock);
15381541
}
15391542

15401543
/*
15411544
* If fi->writectr is positive (no truncate or fsync going on) send
15421545
* all queued writepage requests.
15431546
*
1544-
* Called with fc->lock
1547+
* Called with fi->lock
15451548
*/
15461549
void fuse_flush_writepages(struct inode *inode)
1547-
__releases(fc->lock)
1548-
__acquires(fc->lock)
1550+
__releases(fi->lock)
1551+
__acquires(fi->lock)
15491552
{
15501553
struct fuse_conn *fc = get_fuse_conn(inode);
15511554
struct fuse_inode *fi = get_fuse_inode(inode);
@@ -1565,7 +1568,7 @@ static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
15651568
struct fuse_inode *fi = get_fuse_inode(inode);
15661569

15671570
mapping_set_error(inode->i_mapping, req->out.h.error);
1568-
spin_lock(&fc->lock);
1571+
spin_lock(&fi->lock);
15691572
while (req->misc.write.next) {
15701573
struct fuse_conn *fc = get_fuse_conn(inode);
15711574
struct fuse_write_in *inarg = &req->misc.write.in;
@@ -1602,7 +1605,7 @@ static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
16021605
}
16031606
fi->writectr--;
16041607
fuse_writepage_finish(fc, req);
1605-
spin_unlock(&fc->lock);
1608+
spin_unlock(&fi->lock);
16061609
fuse_writepage_free(fc, req);
16071610
}
16081611

@@ -1611,13 +1614,13 @@ static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
16111614
{
16121615
struct fuse_file *ff = NULL;
16131616

1614-
spin_lock(&fc->lock);
1617+
spin_lock(&fi->lock);
16151618
if (!list_empty(&fi->write_files)) {
16161619
ff = list_entry(fi->write_files.next, struct fuse_file,
16171620
write_entry);
16181621
fuse_file_get(ff);
16191622
}
1620-
spin_unlock(&fc->lock);
1623+
spin_unlock(&fi->lock);
16211624

16221625
return ff;
16231626
}
@@ -1688,11 +1691,11 @@ static int fuse_writepage_locked(struct page *page)
16881691
inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
16891692
inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
16901693

1691-
spin_lock(&fc->lock);
1694+
spin_lock(&fi->lock);
16921695
list_add(&req->writepages_entry, &fi->writepages);
16931696
list_add_tail(&req->list, &fi->queued_writes);
16941697
fuse_flush_writepages(inode);
1695-
spin_unlock(&fc->lock);
1698+
spin_unlock(&fi->lock);
16961699

16971700
end_page_writeback(page);
16981701

@@ -1741,16 +1744,15 @@ static void fuse_writepages_send(struct fuse_fill_wb_data *data)
17411744
{
17421745
struct fuse_req *req = data->req;
17431746
struct inode *inode = data->inode;
1744-
struct fuse_conn *fc = get_fuse_conn(inode);
17451747
struct fuse_inode *fi = get_fuse_inode(inode);
17461748
int num_pages = req->num_pages;
17471749
int i;
17481750

17491751
req->ff = fuse_file_get(data->ff);
1750-
spin_lock(&fc->lock);
1752+
spin_lock(&fi->lock);
17511753
list_add_tail(&req->list, &fi->queued_writes);
17521754
fuse_flush_writepages(inode);
1753-
spin_unlock(&fc->lock);
1755+
spin_unlock(&fi->lock);
17541756

17551757
for (i = 0; i < num_pages; i++)
17561758
end_page_writeback(data->orig_pages[i]);
@@ -1773,12 +1775,12 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req,
17731775

17741776
WARN_ON(new_req->num_pages != 0);
17751777

1776-
spin_lock(&fc->lock);
1778+
spin_lock(&fi->lock);
17771779
list_del(&new_req->writepages_entry);
17781780
old_req = fuse_find_writeback(fi, page->index, page->index);
17791781
if (!old_req) {
17801782
list_add(&new_req->writepages_entry, &fi->writepages);
1781-
spin_unlock(&fc->lock);
1783+
spin_unlock(&fi->lock);
17821784
return false;
17831785
}
17841786

@@ -1801,7 +1803,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req,
18011803
old_req->misc.write.next = new_req;
18021804
}
18031805

1804-
spin_unlock(&fc->lock);
1806+
spin_unlock(&fi->lock);
18051807

18061808
if (tmp) {
18071809
struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
@@ -1822,6 +1824,7 @@ static int fuse_writepages_fill(struct page *page,
18221824
struct fuse_fill_wb_data *data = _data;
18231825
struct fuse_req *req = data->req;
18241826
struct inode *inode = data->inode;
1827+
struct fuse_inode *fi = get_fuse_inode(inode);
18251828
struct fuse_conn *fc = get_fuse_conn(inode);
18261829
struct page *tmp_page;
18271830
bool is_writeback;
@@ -1892,9 +1895,9 @@ static int fuse_writepages_fill(struct page *page,
18921895
req->end = fuse_writepage_end;
18931896
req->inode = inode;
18941897

1895-
spin_lock(&fc->lock);
1898+
spin_lock(&fi->lock);
18961899
list_add(&req->writepages_entry, &fi->writepages);
1897-
spin_unlock(&fc->lock);
1900+
spin_unlock(&fi->lock);
18981901

18991902
data->req = req;
19001903
}
@@ -1917,12 +1920,12 @@ static int fuse_writepages_fill(struct page *page,
19171920
data->orig_pages[req->num_pages] = page;
19181921

19191922
/*
1920-
* Protected by fc->lock against concurrent access by
1923+
* Protected by fi->lock against concurrent access by
19211924
* fuse_page_is_writeback().
19221925
*/
1923-
spin_lock(&fc->lock);
1926+
spin_lock(&fi->lock);
19241927
req->num_pages++;
1925-
spin_unlock(&fc->lock);
1928+
spin_unlock(&fi->lock);
19261929

19271930
out_unlock:
19281931
unlock_page(page);

0 commit comments

Comments
 (0)