123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260 |
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/spinlock.h>
- #include <linux/completion.h>
- #include <linux/buffer_head.h>
- #include <linux/pagemap.h>
- #include <linux/pagevec.h>
- #include <linux/mpage.h>
- #include <linux/fs.h>
- #include <linux/writeback.h>
- #include <linux/swap.h>
- #include <linux/gfs2_ondisk.h>
- #include <linux/backing-dev.h>
- #include <linux/uio.h>
- #include <trace/events/writeback.h>
- #include "gfs2.h"
- #include "incore.h"
- #include "bmap.h"
- #include "glock.h"
- #include "inode.h"
- #include "log.h"
- #include "meta_io.h"
- #include "quota.h"
- #include "trans.h"
- #include "rgrp.h"
- #include "super.h"
- #include "util.h"
- #include "glops.h"
- static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
- unsigned int from, unsigned int to)
- {
- struct buffer_head *head = page_buffers(page);
- unsigned int bsize = head->b_size;
- struct buffer_head *bh;
- unsigned int start, end;
- for (bh = head, start = 0; bh != head || !start;
- bh = bh->b_this_page, start = end) {
- end = start + bsize;
- if (end <= from || start >= to)
- continue;
- if (gfs2_is_jdata(ip))
- set_buffer_uptodate(bh);
- gfs2_trans_add_data(ip->i_gl, bh);
- }
- }
- static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
- struct buffer_head *bh_result, int create)
- {
- int error;
- error = gfs2_block_map(inode, lblock, bh_result, 0);
- if (error)
- return error;
- if (!buffer_mapped(bh_result))
- return -EIO;
- return 0;
- }
- static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
- struct buffer_head *bh_result, int create)
- {
- return gfs2_block_map(inode, lblock, bh_result, 0);
- }
- static int gfs2_writepage_common(struct page *page,
- struct writeback_control *wbc)
- {
- struct inode *inode = page->mapping->host;
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- loff_t i_size = i_size_read(inode);
- pgoff_t end_index = i_size >> PAGE_SHIFT;
- unsigned offset;
- if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
- goto out;
- if (current->journal_info)
- goto redirty;
-
- offset = i_size & (PAGE_SIZE-1);
- if (page->index > end_index || (page->index == end_index && !offset)) {
- page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
- goto out;
- }
- return 1;
- redirty:
- redirty_page_for_writepage(wbc, page);
- out:
- unlock_page(page);
- return 0;
- }
- static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
- {
- int ret;
- ret = gfs2_writepage_common(page, wbc);
- if (ret <= 0)
- return ret;
- return nobh_writepage(page, gfs2_get_block_noalloc, wbc);
- }
- int gfs2_write_full_page(struct page *page, get_block_t *get_block,
- struct writeback_control *wbc)
- {
- struct inode * const inode = page->mapping->host;
- loff_t i_size = i_size_read(inode);
- const pgoff_t end_index = i_size >> PAGE_SHIFT;
- unsigned offset;
-
- offset = i_size & (PAGE_SIZE-1);
- if (page->index == end_index && offset)
- zero_user_segment(page, offset, PAGE_SIZE);
- return __block_write_full_page(inode, page, get_block, wbc,
- end_buffer_async_write);
- }
- static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
- {
- struct inode *inode = page->mapping->host;
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- if (PageChecked(page)) {
- ClearPageChecked(page);
- if (!page_has_buffers(page)) {
- create_empty_buffers(page, inode->i_sb->s_blocksize,
- BIT(BH_Dirty)|BIT(BH_Uptodate));
- }
- gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
- }
- return gfs2_write_full_page(page, gfs2_get_block_noalloc, wbc);
- }
- static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
- {
- struct inode *inode = page->mapping->host;
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- int ret;
- if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
- goto out;
- if (PageChecked(page) || current->journal_info)
- goto out_ignore;
- ret = __gfs2_jdata_writepage(page, wbc);
- return ret;
- out_ignore:
- redirty_page_for_writepage(wbc, page);
- out:
- unlock_page(page);
- return 0;
- }
- static int gfs2_writepages(struct address_space *mapping,
- struct writeback_control *wbc)
- {
- return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
- }
- static int gfs2_write_jdata_pagevec(struct address_space *mapping,
- struct writeback_control *wbc,
- struct pagevec *pvec,
- int nr_pages, pgoff_t end,
- pgoff_t *done_index)
- {
- struct inode *inode = mapping->host;
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- unsigned nrblocks = nr_pages * (PAGE_SIZE/inode->i_sb->s_blocksize);
- int i;
- int ret;
- ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
- if (ret < 0)
- return ret;
- for(i = 0; i < nr_pages; i++) {
- struct page *page = pvec->pages[i];
-
- if (page->index > end) {
-
- ret = 1;
- break;
- }
- *done_index = page->index;
- lock_page(page);
- if (unlikely(page->mapping != mapping)) {
- continue_unlock:
- unlock_page(page);
- continue;
- }
- if (!PageDirty(page)) {
-
- goto continue_unlock;
- }
- if (PageWriteback(page)) {
- if (wbc->sync_mode != WB_SYNC_NONE)
- wait_on_page_writeback(page);
- else
- goto continue_unlock;
- }
- BUG_ON(PageWriteback(page));
- if (!clear_page_dirty_for_io(page))
- goto continue_unlock;
- trace_wbc_writepage(wbc, inode_to_bdi(inode));
- ret = __gfs2_jdata_writepage(page, wbc);
- if (unlikely(ret)) {
- if (ret == AOP_WRITEPAGE_ACTIVATE) {
- unlock_page(page);
- ret = 0;
- } else {
-
- *done_index = page->index + 1;
- ret = 1;
- break;
- }
- }
-
- if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
- ret = 1;
- break;
- }
- }
- gfs2_trans_end(sdp);
- return ret;
- }
- static int gfs2_write_cache_jdata(struct address_space *mapping,
- struct writeback_control *wbc)
- {
- int ret = 0;
- int done = 0;
- struct pagevec pvec;
- int nr_pages;
- pgoff_t uninitialized_var(writeback_index);
- pgoff_t index;
- pgoff_t end;
- pgoff_t done_index;
- int cycled;
- int range_whole = 0;
- int tag;
- pagevec_init(&pvec, 0);
- if (wbc->range_cyclic) {
- writeback_index = mapping->writeback_index;
- index = writeback_index;
- if (index == 0)
- cycled = 1;
- else
- cycled = 0;
- end = -1;
- } else {
- index = wbc->range_start >> PAGE_SHIFT;
- end = wbc->range_end >> PAGE_SHIFT;
- if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
- range_whole = 1;
- cycled = 1;
- }
- if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
- tag = PAGECACHE_TAG_TOWRITE;
- else
- tag = PAGECACHE_TAG_DIRTY;
- retry:
- if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
- tag_pages_for_writeback(mapping, index, end);
- done_index = index;
- while (!done && (index <= end)) {
- nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
- min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
- if (nr_pages == 0)
- break;
- ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end, &done_index);
- if (ret)
- done = 1;
- if (ret > 0)
- ret = 0;
- pagevec_release(&pvec);
- cond_resched();
- }
- if (!cycled && !done) {
-
- cycled = 1;
- index = 0;
- end = writeback_index - 1;
- goto retry;
- }
- if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
- mapping->writeback_index = done_index;
- return ret;
- }
- static int gfs2_jdata_writepages(struct address_space *mapping,
- struct writeback_control *wbc)
- {
- struct gfs2_inode *ip = GFS2_I(mapping->host);
- struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
- int ret;
- ret = gfs2_write_cache_jdata(mapping, wbc);
- if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
- gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
- ret = gfs2_write_cache_jdata(mapping, wbc);
- }
- return ret;
- }
- static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
- {
- struct buffer_head *dibh;
- u64 dsize = i_size_read(&ip->i_inode);
- void *kaddr;
- int error;
-
- if (unlikely(page->index)) {
- zero_user(page, 0, PAGE_SIZE);
- SetPageUptodate(page);
- return 0;
- }
- error = gfs2_meta_inode_buffer(ip, &dibh);
- if (error)
- return error;
- kaddr = kmap_atomic(page);
- if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
- dsize = (dibh->b_size - sizeof(struct gfs2_dinode));
- memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
- memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
- kunmap_atomic(kaddr);
- flush_dcache_page(page);
- brelse(dibh);
- SetPageUptodate(page);
- return 0;
- }
- static int __gfs2_readpage(void *file, struct page *page)
- {
- struct gfs2_inode *ip = GFS2_I(page->mapping->host);
- struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
- int error;
- if (gfs2_is_stuffed(ip)) {
- error = stuffed_readpage(ip, page);
- unlock_page(page);
- } else {
- error = mpage_readpage(page, gfs2_block_map);
- }
- if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
- return -EIO;
- return error;
- }
- static int gfs2_readpage(struct file *file, struct page *page)
- {
- struct address_space *mapping = page->mapping;
- struct gfs2_inode *ip = GFS2_I(mapping->host);
- struct gfs2_holder gh;
- int error;
- unlock_page(page);
- gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
- error = gfs2_glock_nq(&gh);
- if (unlikely(error))
- goto out;
- error = AOP_TRUNCATED_PAGE;
- lock_page(page);
- if (page->mapping == mapping && !PageUptodate(page))
- error = __gfs2_readpage(file, page);
- else
- unlock_page(page);
- gfs2_glock_dq(&gh);
- out:
- gfs2_holder_uninit(&gh);
- if (error && error != AOP_TRUNCATED_PAGE)
- lock_page(page);
- return error;
- }
- int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
- unsigned size)
- {
- struct address_space *mapping = ip->i_inode.i_mapping;
- unsigned long index = *pos / PAGE_SIZE;
- unsigned offset = *pos & (PAGE_SIZE - 1);
- unsigned copied = 0;
- unsigned amt;
- struct page *page;
- void *p;
- do {
- amt = size - copied;
- if (offset + size > PAGE_SIZE)
- amt = PAGE_SIZE - offset;
- page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
- if (IS_ERR(page))
- return PTR_ERR(page);
- p = kmap_atomic(page);
- memcpy(buf + copied, p + offset, amt);
- kunmap_atomic(p);
- put_page(page);
- copied += amt;
- index++;
- offset = 0;
- } while(copied < size);
- (*pos) += size;
- return size;
- }
- static int gfs2_readpages(struct file *file, struct address_space *mapping,
- struct list_head *pages, unsigned nr_pages)
- {
- struct inode *inode = mapping->host;
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- struct gfs2_holder gh;
- int ret;
- gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
- ret = gfs2_glock_nq(&gh);
- if (unlikely(ret))
- goto out_uninit;
- if (!gfs2_is_stuffed(ip))
- ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
- gfs2_glock_dq(&gh);
- out_uninit:
- gfs2_holder_uninit(&gh);
- if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
- ret = -EIO;
- return ret;
- }
- static int gfs2_write_begin(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned flags,
- struct page **pagep, void **fsdata)
- {
- struct gfs2_inode *ip = GFS2_I(mapping->host);
- struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
- struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
- unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
- unsigned requested = 0;
- int alloc_required;
- int error = 0;
- pgoff_t index = pos >> PAGE_SHIFT;
- unsigned from = pos & (PAGE_SIZE - 1);
- struct page *page;
- gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
- error = gfs2_glock_nq(&ip->i_gh);
- if (unlikely(error))
- goto out_uninit;
- if (&ip->i_inode == sdp->sd_rindex) {
- error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
- GL_NOCACHE, &m_ip->i_gh);
- if (unlikely(error)) {
- gfs2_glock_dq(&ip->i_gh);
- goto out_uninit;
- }
- }
- alloc_required = gfs2_write_alloc_required(ip, pos, len);
- if (alloc_required || gfs2_is_jdata(ip))
- gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
- if (alloc_required) {
- struct gfs2_alloc_parms ap = { .aflags = 0, };
- requested = data_blocks + ind_blocks;
- ap.target = requested;
- error = gfs2_quota_lock_check(ip, &ap);
- if (error)
- goto out_unlock;
- error = gfs2_inplace_reserve(ip, &ap);
- if (error)
- goto out_qunlock;
- }
- rblocks = RES_DINODE + ind_blocks;
- if (gfs2_is_jdata(ip))
- rblocks += data_blocks ? data_blocks : 1;
- if (ind_blocks || data_blocks)
- rblocks += RES_STATFS + RES_QUOTA;
- if (&ip->i_inode == sdp->sd_rindex)
- rblocks += 2 * RES_STATFS;
- if (alloc_required)
- rblocks += gfs2_rg_blocks(ip, requested);
- error = gfs2_trans_begin(sdp, rblocks,
- PAGE_SIZE/sdp->sd_sb.sb_bsize);
- if (error)
- goto out_trans_fail;
- error = -ENOMEM;
- flags |= AOP_FLAG_NOFS;
- page = grab_cache_page_write_begin(mapping, index, flags);
- *pagep = page;
- if (unlikely(!page))
- goto out_endtrans;
- if (gfs2_is_stuffed(ip)) {
- error = 0;
- if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
- error = gfs2_unstuff_dinode(ip, page);
- if (error == 0)
- goto prepare_write;
- } else if (!PageUptodate(page)) {
- error = stuffed_readpage(ip, page);
- }
- goto out;
- }
- prepare_write:
- error = __block_write_begin(page, from, len, gfs2_block_map);
- out:
- if (error == 0)
- return 0;
- unlock_page(page);
- put_page(page);
- gfs2_trans_end(sdp);
- if (pos + len > ip->i_inode.i_size)
- gfs2_trim_blocks(&ip->i_inode);
- goto out_trans_fail;
- out_endtrans:
- gfs2_trans_end(sdp);
- out_trans_fail:
- if (alloc_required) {
- gfs2_inplace_release(ip);
- out_qunlock:
- gfs2_quota_unlock(ip);
- }
- out_unlock:
- if (&ip->i_inode == sdp->sd_rindex) {
- gfs2_glock_dq(&m_ip->i_gh);
- gfs2_holder_uninit(&m_ip->i_gh);
- }
- gfs2_glock_dq(&ip->i_gh);
- out_uninit:
- gfs2_holder_uninit(&ip->i_gh);
- return error;
- }
- static void adjust_fs_space(struct inode *inode)
- {
- struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
- struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
- struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
- struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
- struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
- struct buffer_head *m_bh, *l_bh;
- u64 fs_total, new_free;
-
- fs_total = gfs2_ri_total(sdp);
- if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
- return;
- spin_lock(&sdp->sd_statfs_spin);
- gfs2_statfs_change_in(m_sc, m_bh->b_data +
- sizeof(struct gfs2_dinode));
- if (fs_total > (m_sc->sc_total + l_sc->sc_total))
- new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
- else
- new_free = 0;
- spin_unlock(&sdp->sd_statfs_spin);
- fs_warn(sdp, "File system extended by %llu blocks.\n",
- (unsigned long long)new_free);
- gfs2_statfs_change(sdp, new_free, new_free, 0);
- if (gfs2_meta_inode_buffer(l_ip, &l_bh) != 0)
- goto out;
- update_statfs(sdp, m_bh, l_bh);
- brelse(l_bh);
- out:
- brelse(m_bh);
- }
- static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
- loff_t pos, unsigned len, unsigned copied,
- struct page *page)
- {
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
- u64 to = pos + copied;
- void *kaddr;
- unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
- BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
- kaddr = kmap_atomic(page);
- memcpy(buf + pos, kaddr + pos, copied);
- memset(kaddr + pos + copied, 0, len - copied);
- flush_dcache_page(page);
- kunmap_atomic(kaddr);
- if (!PageUptodate(page))
- SetPageUptodate(page);
- unlock_page(page);
- put_page(page);
- if (copied) {
- if (inode->i_size < to)
- i_size_write(inode, to);
- mark_inode_dirty(inode);
- }
- if (inode == sdp->sd_rindex) {
- adjust_fs_space(inode);
- sdp->sd_rindex_uptodate = 0;
- }
- brelse(dibh);
- gfs2_trans_end(sdp);
- if (inode == sdp->sd_rindex) {
- gfs2_glock_dq(&m_ip->i_gh);
- gfs2_holder_uninit(&m_ip->i_gh);
- }
- gfs2_glock_dq(&ip->i_gh);
- gfs2_holder_uninit(&ip->i_gh);
- return copied;
- }
- static int gfs2_write_end(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned copied,
- struct page *page, void *fsdata)
- {
- struct inode *inode = page->mapping->host;
- struct gfs2_inode *ip = GFS2_I(inode);
- struct gfs2_sbd *sdp = GFS2_SB(inode);
- struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
- struct buffer_head *dibh;
- unsigned int from = pos & (PAGE_SIZE - 1);
- unsigned int to = from + len;
- int ret;
- struct gfs2_trans *tr = current->journal_info;
- BUG_ON(!tr);
- BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
- ret = gfs2_meta_inode_buffer(ip, &dibh);
- if (unlikely(ret)) {
- unlock_page(page);
- put_page(page);
- goto failed;
- }
- if (gfs2_is_stuffed(ip))
- return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
- if (!gfs2_is_writeback(ip))
- gfs2_page_add_databufs(ip, page, from, to);
- ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
- if (tr->tr_num_buf_new)
- __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
- else
- gfs2_trans_add_meta(ip->i_gl, dibh);
- if (inode == sdp->sd_rindex) {
- adjust_fs_space(inode);
- sdp->sd_rindex_uptodate = 0;
- }
- brelse(dibh);
- failed:
- gfs2_trans_end(sdp);
- gfs2_inplace_release(ip);
- if (ip->i_qadata && ip->i_qadata->qa_qd_num)
- gfs2_quota_unlock(ip);
- if (inode == sdp->sd_rindex) {
- gfs2_glock_dq(&m_ip->i_gh);
- gfs2_holder_uninit(&m_ip->i_gh);
- }
- gfs2_glock_dq(&ip->i_gh);
- gfs2_holder_uninit(&ip->i_gh);
- return ret;
- }
-
- static int gfs2_set_page_dirty(struct page *page)
- {
- SetPageChecked(page);
- return __set_page_dirty_buffers(page);
- }
- static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
- {
- struct gfs2_inode *ip = GFS2_I(mapping->host);
- struct gfs2_holder i_gh;
- sector_t dblock = 0;
- int error;
- error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
- if (error)
- return 0;
- if (!gfs2_is_stuffed(ip))
- dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
- gfs2_glock_dq_uninit(&i_gh);
- return dblock;
- }
- static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
- {
- struct gfs2_bufdata *bd;
- lock_buffer(bh);
- gfs2_log_lock(sdp);
- clear_buffer_dirty(bh);
- bd = bh->b_private;
- if (bd) {
- if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
- list_del_init(&bd->bd_list);
- else
- gfs2_remove_from_journal(bh, REMOVE_JDATA);
- }
- bh->b_bdev = NULL;
- clear_buffer_mapped(bh);
- clear_buffer_req(bh);
- clear_buffer_new(bh);
- gfs2_log_unlock(sdp);
- unlock_buffer(bh);
- }
- static void gfs2_invalidatepage(struct page *page, unsigned int offset,
- unsigned int length)
- {
- struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
- unsigned int stop = offset + length;
- int partial_page = (offset || length < PAGE_SIZE);
- struct buffer_head *bh, *head;
- unsigned long pos = 0;
- BUG_ON(!PageLocked(page));
- if (!partial_page)
- ClearPageChecked(page);
- if (!page_has_buffers(page))
- goto out;
- bh = head = page_buffers(page);
- do {
- if (pos + bh->b_size > stop)
- return;
- if (offset <= pos)
- gfs2_discard(sdp, bh);
- pos += bh->b_size;
- bh = bh->b_this_page;
- } while (bh != head);
- out:
- if (!partial_page)
- try_to_release_page(page, 0);
- }
- static int gfs2_ok_for_dio(struct gfs2_inode *ip, loff_t offset)
- {
-
- if (gfs2_is_stuffed(ip))
- return 0;
- if (offset >= i_size_read(&ip->i_inode))
- return 0;
- return 1;
- }
- static ssize_t gfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
- {
- struct file *file = iocb->ki_filp;
- struct inode *inode = file->f_mapping->host;
- struct address_space *mapping = inode->i_mapping;
- struct gfs2_inode *ip = GFS2_I(inode);
- loff_t offset = iocb->ki_pos;
- struct gfs2_holder gh;
- int rv;
-
- gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
- rv = gfs2_glock_nq(&gh);
- if (rv)
- goto out_uninit;
- rv = gfs2_ok_for_dio(ip, offset);
- if (rv != 1)
- goto out;
-
- if (mapping->nrpages) {
- loff_t lstart = offset & ~(PAGE_SIZE - 1);
- loff_t len = iov_iter_count(iter);
- loff_t end = PAGE_ALIGN(offset + len) - 1;
- rv = 0;
- if (len == 0)
- goto out;
- if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
- unmap_shared_mapping_range(ip->i_inode.i_mapping, offset, len);
- rv = filemap_write_and_wait_range(mapping, lstart, end);
- if (rv)
- goto out;
- if (iov_iter_rw(iter) == WRITE)
- truncate_inode_pages_range(mapping, lstart, end);
- }
- rv = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
- gfs2_get_block_direct, NULL, NULL, 0);
- out:
- gfs2_glock_dq(&gh);
- out_uninit:
- gfs2_holder_uninit(&gh);
- return rv;
- }
- int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
- {
- struct address_space *mapping = page->mapping;
- struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
- struct buffer_head *bh, *head;
- struct gfs2_bufdata *bd;
- if (!page_has_buffers(page))
- return 0;
-
- gfs2_log_lock(sdp);
- spin_lock(&sdp->sd_ail_lock);
- head = bh = page_buffers(page);
- do {
- if (atomic_read(&bh->b_count))
- goto cannot_release;
- bd = bh->b_private;
- if (bd && bd->bd_tr)
- goto cannot_release;
- if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
- goto cannot_release;
- bh = bh->b_this_page;
- } while(bh != head);
- spin_unlock(&sdp->sd_ail_lock);
- head = bh = page_buffers(page);
- do {
- bd = bh->b_private;
- if (bd) {
- gfs2_assert_warn(sdp, bd->bd_bh == bh);
- if (!list_empty(&bd->bd_list))
- list_del_init(&bd->bd_list);
- bd->bd_bh = NULL;
- bh->b_private = NULL;
- kmem_cache_free(gfs2_bufdata_cachep, bd);
- }
- bh = bh->b_this_page;
- } while (bh != head);
- gfs2_log_unlock(sdp);
- return try_to_free_buffers(page);
- cannot_release:
- spin_unlock(&sdp->sd_ail_lock);
- gfs2_log_unlock(sdp);
- return 0;
- }
- static const struct address_space_operations gfs2_writeback_aops = {
- .writepage = gfs2_writepage,
- .writepages = gfs2_writepages,
- .readpage = gfs2_readpage,
- .readpages = gfs2_readpages,
- .write_begin = gfs2_write_begin,
- .write_end = gfs2_write_end,
- .bmap = gfs2_bmap,
- .invalidatepage = gfs2_invalidatepage,
- .releasepage = gfs2_releasepage,
- .direct_IO = gfs2_direct_IO,
- .migratepage = buffer_migrate_page,
- .is_partially_uptodate = block_is_partially_uptodate,
- .error_remove_page = generic_error_remove_page,
- };
- static const struct address_space_operations gfs2_ordered_aops = {
- .writepage = gfs2_writepage,
- .writepages = gfs2_writepages,
- .readpage = gfs2_readpage,
- .readpages = gfs2_readpages,
- .write_begin = gfs2_write_begin,
- .write_end = gfs2_write_end,
- .set_page_dirty = gfs2_set_page_dirty,
- .bmap = gfs2_bmap,
- .invalidatepage = gfs2_invalidatepage,
- .releasepage = gfs2_releasepage,
- .direct_IO = gfs2_direct_IO,
- .migratepage = buffer_migrate_page,
- .is_partially_uptodate = block_is_partially_uptodate,
- .error_remove_page = generic_error_remove_page,
- };
- static const struct address_space_operations gfs2_jdata_aops = {
- .writepage = gfs2_jdata_writepage,
- .writepages = gfs2_jdata_writepages,
- .readpage = gfs2_readpage,
- .readpages = gfs2_readpages,
- .write_begin = gfs2_write_begin,
- .write_end = gfs2_write_end,
- .set_page_dirty = gfs2_set_page_dirty,
- .bmap = gfs2_bmap,
- .invalidatepage = gfs2_invalidatepage,
- .releasepage = gfs2_releasepage,
- .is_partially_uptodate = block_is_partially_uptodate,
- .error_remove_page = generic_error_remove_page,
- };
- void gfs2_set_aops(struct inode *inode)
- {
- struct gfs2_inode *ip = GFS2_I(inode);
- if (gfs2_is_writeback(ip))
- inode->i_mapping->a_ops = &gfs2_writeback_aops;
- else if (gfs2_is_ordered(ip))
- inode->i_mapping->a_ops = &gfs2_ordered_aops;
- else if (gfs2_is_jdata(ip))
- inode->i_mapping->a_ops = &gfs2_jdata_aops;
- else
- BUG();
- }
|