David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/pagemap.h> |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 16 | #include <linux/pagevec.h> |
Steven Whitehouse | 9b124fb | 2006-01-30 11:55:32 +0000 | [diff] [blame] | 17 | #include <linux/mpage.h> |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 18 | #include <linux/fs.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | |
| 21 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 22 | #include "lm_interface.h" |
| 23 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "bmap.h" |
| 25 | #include "glock.h" |
| 26 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 27 | #include "log.h" |
| 28 | #include "meta_io.h" |
| 29 | #include "ops_address.h" |
| 30 | #include "page.h" |
| 31 | #include "quota.h" |
| 32 | #include "trans.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 33 | #include "rgrp.h" |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 34 | #include "ops_file.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 35 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 38 | * gfs2_get_block - Fills in a buffer head with details about a block |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 39 | * @inode: The inode |
| 40 | * @lblock: The block number to look up |
| 41 | * @bh_result: The buffer head to return the result in |
| 42 | * @create: Non-zero if we may add block to the file |
| 43 | * |
| 44 | * Returns: errno |
| 45 | */ |
| 46 | |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 47 | int gfs2_get_block(struct inode *inode, sector_t lblock, |
| 48 | struct buffer_head *bh_result, int create) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 49 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 50 | int new = create; |
| 51 | uint64_t dblock; |
| 52 | int error; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 53 | int boundary; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 54 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 55 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | if (error) |
| 57 | return error; |
| 58 | |
| 59 | if (!dblock) |
| 60 | return 0; |
| 61 | |
| 62 | map_bh(bh_result, inode->i_sb, dblock); |
| 63 | if (new) |
| 64 | set_buffer_new(bh_result); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 65 | if (boundary) |
| 66 | set_buffer_boundary(bh_result); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * get_block_noalloc - Fills in a buffer head with details about a block |
| 73 | * @inode: The inode |
| 74 | * @lblock: The block number to look up |
| 75 | * @bh_result: The buffer head to return the result in |
| 76 | * @create: Non-zero if we may add block to the file |
| 77 | * |
| 78 | * Returns: errno |
| 79 | */ |
| 80 | |
| 81 | static int get_block_noalloc(struct inode *inode, sector_t lblock, |
| 82 | struct buffer_head *bh_result, int create) |
| 83 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 84 | int new = 0; |
| 85 | uint64_t dblock; |
| 86 | int error; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 87 | int boundary; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 88 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 89 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | if (error) |
| 91 | return error; |
| 92 | |
| 93 | if (dblock) |
| 94 | map_bh(bh_result, inode->i_sb, dblock); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 95 | else if (gfs2_assert_withdraw(GFS2_SB(inode), !create)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 96 | error = -EIO; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 97 | if (boundary) |
| 98 | set_buffer_boundary(bh_result); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 99 | |
| 100 | return error; |
| 101 | } |
| 102 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | /** |
| 104 | * gfs2_writepage - Write complete page |
| 105 | * @page: Page to write |
| 106 | * |
| 107 | * Returns: errno |
| 108 | * |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 109 | * Some of this is copied from block_write_full_page() although we still |
| 110 | * call it to do most of the work. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 111 | */ |
| 112 | |
| 113 | static int gfs2_writepage(struct page *page, struct writeback_control *wbc) |
| 114 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 115 | struct inode *inode = page->mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 116 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
| 117 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 118 | loff_t i_size = i_size_read(inode); |
| 119 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 120 | unsigned offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | int error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 122 | int done_trans = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 123 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) { |
| 125 | unlock_page(page); |
| 126 | return -EIO; |
| 127 | } |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 128 | if (current->journal_info) |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 129 | goto out_ignore; |
| 130 | |
| 131 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 132 | offset = i_size & (PAGE_CACHE_SIZE-1); |
Steven Whitehouse | d2d7b8a | 2006-05-02 12:09:42 -0400 | [diff] [blame] | 133 | if (page->index > end_index || (page->index == end_index && !offset)) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 134 | page->mapping->a_ops->invalidatepage(page, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | unlock_page(page); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 136 | return 0; /* don't care */ |
| 137 | } |
| 138 | |
| 139 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) { |
| 140 | error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0); |
| 141 | if (error) |
| 142 | goto out_ignore; |
| 143 | gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1); |
| 144 | done_trans = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 145 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 146 | error = block_write_full_page(page, get_block_noalloc, wbc); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 147 | if (done_trans) |
| 148 | gfs2_trans_end(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 149 | gfs2_meta_cache_flush(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 151 | |
| 152 | out_ignore: |
| 153 | redirty_page_for_writepage(wbc, page); |
| 154 | unlock_page(page); |
| 155 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 158 | static int zero_readpage(struct page *page) |
| 159 | { |
| 160 | void *kaddr; |
| 161 | |
| 162 | kaddr = kmap_atomic(page, KM_USER0); |
| 163 | memset(kaddr, 0, PAGE_CACHE_SIZE); |
| 164 | kunmap_atomic(page, KM_USER0); |
| 165 | |
| 166 | SetPageUptodate(page); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 171 | /** |
| 172 | * stuffed_readpage - Fill in a Linux page with stuffed file data |
| 173 | * @ip: the inode |
| 174 | * @page: the page |
| 175 | * |
| 176 | * Returns: errno |
| 177 | */ |
| 178 | |
| 179 | static int stuffed_readpage(struct gfs2_inode *ip, struct page *page) |
| 180 | { |
| 181 | struct buffer_head *dibh; |
| 182 | void *kaddr; |
| 183 | int error; |
| 184 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 185 | /* Only the first page of a stuffed file might contain data */ |
| 186 | if (unlikely(page->index)) |
| 187 | return zero_readpage(page); |
| 188 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 189 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 190 | if (error) |
| 191 | return error; |
| 192 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 193 | kaddr = kmap_atomic(page, KM_USER0); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 194 | memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 195 | ip->i_di.di_size); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 196 | memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 197 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 198 | |
| 199 | brelse(dibh); |
| 200 | |
| 201 | SetPageUptodate(page); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 208 | * gfs2_readpage - readpage with locking |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 209 | * @file: The file to read a page for. N.B. This may be NULL if we are |
| 210 | * reading an internal file. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 211 | * @page: The page to read |
| 212 | * |
| 213 | * Returns: errno |
| 214 | */ |
| 215 | |
| 216 | static int gfs2_readpage(struct file *file, struct page *page) |
| 217 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 218 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
| 219 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 220 | struct gfs2_holder gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 221 | int error; |
| 222 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 223 | if (likely(file != &gfs2_internal_file_sentinal)) { |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 224 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh); |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 225 | error = gfs2_glock_nq_m_atime(1, &gh); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 226 | if (unlikely(error)) |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 227 | goto out_unlock; |
| 228 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 229 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 230 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 231 | error = stuffed_readpage(ip, page); |
| 232 | unlock_page(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 233 | } else |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 234 | error = mpage_readpage(page, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 235 | |
| 236 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 237 | error = -EIO; |
| 238 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 239 | if (file != &gfs2_internal_file_sentinal) { |
| 240 | gfs2_glock_dq_m(1, &gh); |
| 241 | gfs2_holder_uninit(&gh); |
| 242 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 243 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 244 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 245 | out_unlock: |
| 246 | unlock_page(page); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 247 | if (file != &gfs2_internal_file_sentinal) |
| 248 | gfs2_holder_uninit(&gh); |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | #define list_to_page(head) (list_entry((head)->prev, struct page, lru)) |
| 253 | |
| 254 | /** |
| 255 | * gfs2_readpages - Read a bunch of pages at once |
| 256 | * |
| 257 | * Some notes: |
| 258 | * 1. This is only for readahead, so we can simply ignore any things |
| 259 | * which are slightly inconvenient (such as locking conflicts between |
| 260 | * the page lock and the glock) and return having done no I/O. Its |
| 261 | * obviously not something we'd want to do on too regular a basis. |
| 262 | * Any I/O we ignore at this time will be done via readpage later. |
| 263 | * 2. We have to handle stuffed files here too. |
| 264 | * 3. mpage_readpages() does most of the heavy lifting in the common case. |
| 265 | * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places. |
| 266 | * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as |
| 267 | * well as read-ahead. |
| 268 | */ |
| 269 | static int gfs2_readpages(struct file *file, struct address_space *mapping, |
| 270 | struct list_head *pages, unsigned nr_pages) |
| 271 | { |
| 272 | struct inode *inode = mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 273 | struct gfs2_inode *ip = GFS2_I(inode); |
| 274 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 275 | struct gfs2_holder gh; |
| 276 | unsigned page_idx; |
| 277 | int ret; |
| 278 | |
| 279 | if (likely(file != &gfs2_internal_file_sentinal)) { |
| 280 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, |
| 281 | LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh); |
| 282 | ret = gfs2_glock_nq_m_atime(1, &gh); |
| 283 | if (ret == GLR_TRYFAILED) |
| 284 | goto out_noerror; |
| 285 | if (unlikely(ret)) |
| 286 | goto out_unlock; |
| 287 | } |
| 288 | |
| 289 | if (gfs2_is_stuffed(ip)) { |
| 290 | struct pagevec lru_pvec; |
| 291 | pagevec_init(&lru_pvec, 0); |
| 292 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 293 | struct page *page = list_to_page(pages); |
| 294 | list_del(&page->lru); |
| 295 | if (!add_to_page_cache(page, mapping, |
| 296 | page->index, GFP_KERNEL)) { |
| 297 | ret = stuffed_readpage(ip, page); |
| 298 | unlock_page(page); |
| 299 | if (!pagevec_add(&lru_pvec, page)) |
| 300 | __pagevec_lru_add(&lru_pvec); |
| 301 | } |
| 302 | page_cache_release(page); |
| 303 | } |
| 304 | pagevec_lru_add(&lru_pvec); |
| 305 | ret = 0; |
| 306 | } else { |
| 307 | /* What we really want to do .... */ |
| 308 | ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block); |
| 309 | } |
| 310 | |
| 311 | if (likely(file != &gfs2_internal_file_sentinal)) { |
| 312 | gfs2_glock_dq_m(1, &gh); |
| 313 | gfs2_holder_uninit(&gh); |
| 314 | } |
| 315 | out: |
| 316 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 317 | ret = -EIO; |
| 318 | return ret; |
| 319 | out_noerror: |
| 320 | ret = 0; |
| 321 | out_unlock: |
| 322 | /* unlock all pages, we can't do any I/O right now */ |
| 323 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 324 | struct page *page = list_to_page(pages); |
| 325 | list_del(&page->lru); |
| 326 | unlock_page(page); |
| 327 | page_cache_release(page); |
| 328 | } |
| 329 | if (likely(file != &gfs2_internal_file_sentinal)) |
| 330 | gfs2_holder_uninit(&gh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 331 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
| 335 | * gfs2_prepare_write - Prepare to write a page to a file |
| 336 | * @file: The file to write to |
| 337 | * @page: The page which is to be prepared for writing |
| 338 | * @from: From (byte range within page) |
| 339 | * @to: To (byte range within page) |
| 340 | * |
| 341 | * Returns: errno |
| 342 | */ |
| 343 | |
| 344 | static int gfs2_prepare_write(struct file *file, struct page *page, |
| 345 | unsigned from, unsigned to) |
| 346 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 347 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
| 348 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 349 | unsigned int data_blocks, ind_blocks, rblocks; |
| 350 | int alloc_required; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 351 | int error = 0; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 352 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from; |
| 353 | loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 354 | struct gfs2_alloc *al; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 355 | |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 356 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 357 | error = gfs2_glock_nq_m_atime(1, &ip->i_gh); |
| 358 | if (error) |
| 359 | goto out_uninit; |
| 360 | |
| 361 | gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks); |
| 362 | |
| 363 | error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required); |
| 364 | if (error) |
| 365 | goto out_unlock; |
| 366 | |
| 367 | |
| 368 | if (alloc_required) { |
| 369 | al = gfs2_alloc_get(ip); |
| 370 | |
| 371 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 372 | if (error) |
| 373 | goto out_alloc_put; |
| 374 | |
| 375 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); |
| 376 | if (error) |
| 377 | goto out_qunlock; |
| 378 | |
| 379 | al->al_requested = data_blocks + ind_blocks; |
| 380 | error = gfs2_inplace_reserve(ip); |
| 381 | if (error) |
| 382 | goto out_qunlock; |
| 383 | } |
| 384 | |
| 385 | rblocks = RES_DINODE + ind_blocks; |
| 386 | if (gfs2_is_jdata(ip)) |
| 387 | rblocks += data_blocks ? data_blocks : 1; |
| 388 | if (ind_blocks || data_blocks) |
| 389 | rblocks += RES_STATFS + RES_QUOTA; |
| 390 | |
| 391 | error = gfs2_trans_begin(sdp, rblocks, 0); |
| 392 | if (error) |
| 393 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 394 | |
| 395 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 396 | if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 397 | error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, |
| 398 | page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 399 | if (error == 0) |
| 400 | goto prepare_write; |
| 401 | } else if (!PageUptodate(page)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | error = stuffed_readpage(ip, page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 403 | goto out; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 406 | prepare_write: |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 407 | error = block_prepare_write(page, from, to, gfs2_get_block); |
| 408 | |
| 409 | out: |
| 410 | if (error) { |
| 411 | gfs2_trans_end(sdp); |
| 412 | if (alloc_required) { |
| 413 | gfs2_inplace_release(ip); |
| 414 | out_qunlock: |
| 415 | gfs2_quota_unlock(ip); |
| 416 | out_alloc_put: |
| 417 | gfs2_alloc_put(ip); |
| 418 | } |
| 419 | out_unlock: |
| 420 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 421 | out_uninit: |
| 422 | gfs2_holder_uninit(&ip->i_gh); |
| 423 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 424 | |
| 425 | return error; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * gfs2_commit_write - Commit write to a file |
| 430 | * @file: The file to write to |
| 431 | * @page: The page containing the data |
| 432 | * @from: From (byte range within page) |
| 433 | * @to: To (byte range within page) |
| 434 | * |
| 435 | * Returns: errno |
| 436 | */ |
| 437 | |
| 438 | static int gfs2_commit_write(struct file *file, struct page *page, |
| 439 | unsigned from, unsigned to) |
| 440 | { |
| 441 | struct inode *inode = page->mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 442 | struct gfs2_inode *ip = GFS2_I(inode); |
| 443 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 444 | int error = -EOPNOTSUPP; |
| 445 | struct buffer_head *dibh; |
| 446 | struct gfs2_alloc *al = &ip->i_alloc;; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 447 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 448 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl))) |
| 449 | goto fail_nounlock; |
| 450 | |
| 451 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 452 | if (error) |
| 453 | goto fail_endtrans; |
| 454 | |
| 455 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 456 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 457 | if (gfs2_is_stuffed(ip)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 458 | uint64_t file_size; |
| 459 | void *kaddr; |
| 460 | |
| 461 | file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 462 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 463 | kaddr = kmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 464 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 465 | (char *)kaddr + from, to - from); |
| 466 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 467 | |
| 468 | SetPageUptodate(page); |
| 469 | |
| 470 | if (inode->i_size < file_size) |
| 471 | i_size_write(inode, file_size); |
| 472 | } else { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 473 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || |
| 474 | gfs2_is_jdata(ip)) |
Steven Whitehouse | 257f9b4 | 2006-01-31 10:00:25 +0000 | [diff] [blame] | 475 | gfs2_page_add_databufs(ip, page, from, to); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 476 | error = generic_commit_write(file, page, from, to); |
| 477 | if (error) |
| 478 | goto fail; |
| 479 | } |
| 480 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 481 | if (ip->i_di.di_size < inode->i_size) |
| 482 | ip->i_di.di_size = inode->i_size; |
| 483 | |
| 484 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 485 | brelse(dibh); |
| 486 | gfs2_trans_end(sdp); |
| 487 | if (al->al_requested) { |
| 488 | gfs2_inplace_release(ip); |
| 489 | gfs2_quota_unlock(ip); |
| 490 | gfs2_alloc_put(ip); |
| 491 | } |
| 492 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 493 | gfs2_holder_uninit(&ip->i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 496 | fail: |
| 497 | brelse(dibh); |
| 498 | fail_endtrans: |
| 499 | gfs2_trans_end(sdp); |
| 500 | if (al->al_requested) { |
| 501 | gfs2_inplace_release(ip); |
| 502 | gfs2_quota_unlock(ip); |
| 503 | gfs2_alloc_put(ip); |
| 504 | } |
| 505 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 506 | gfs2_holder_uninit(&ip->i_gh); |
| 507 | fail_nounlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | ClearPageUptodate(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 509 | return error; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * gfs2_bmap - Block map function |
| 514 | * @mapping: Address space info |
| 515 | * @lblock: The block to map |
| 516 | * |
| 517 | * Returns: The disk address for the block or 0 on hole or error |
| 518 | */ |
| 519 | |
| 520 | static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock) |
| 521 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 522 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 523 | struct gfs2_holder i_gh; |
| 524 | sector_t dblock = 0; |
| 525 | int error; |
| 526 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 527 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 528 | if (error) |
| 529 | return 0; |
| 530 | |
| 531 | if (!gfs2_is_stuffed(ip)) |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 532 | dblock = generic_block_bmap(mapping, lblock, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | |
| 534 | gfs2_glock_dq_uninit(&i_gh); |
| 535 | |
| 536 | return dblock; |
| 537 | } |
| 538 | |
| 539 | static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh) |
| 540 | { |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 541 | struct gfs2_bufdata *bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | |
| 543 | gfs2_log_lock(sdp); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 544 | bd = bh->b_private; |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 545 | if (bd) { |
| 546 | bd->bd_bh = NULL; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 547 | bh->b_private = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 548 | gfs2_log_unlock(sdp); |
| 549 | brelse(bh); |
| 550 | } else |
| 551 | gfs2_log_unlock(sdp); |
| 552 | |
| 553 | lock_buffer(bh); |
| 554 | clear_buffer_dirty(bh); |
| 555 | bh->b_bdev = NULL; |
| 556 | clear_buffer_mapped(bh); |
| 557 | clear_buffer_req(bh); |
| 558 | clear_buffer_new(bh); |
| 559 | clear_buffer_delay(bh); |
| 560 | unlock_buffer(bh); |
| 561 | } |
| 562 | |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 563 | static void gfs2_invalidatepage(struct page *page, unsigned long offset) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 564 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 565 | struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 566 | struct buffer_head *head, *bh, *next; |
| 567 | unsigned int curr_off = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 568 | |
| 569 | BUG_ON(!PageLocked(page)); |
| 570 | if (!page_has_buffers(page)) |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 571 | return; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 572 | |
| 573 | bh = head = page_buffers(page); |
| 574 | do { |
| 575 | unsigned int next_off = curr_off + bh->b_size; |
| 576 | next = bh->b_this_page; |
| 577 | |
| 578 | if (offset <= curr_off) |
| 579 | discard_buffer(sdp, bh); |
| 580 | |
| 581 | curr_off = next_off; |
| 582 | bh = next; |
| 583 | } while (bh != head); |
| 584 | |
| 585 | if (!offset) |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 586 | try_to_release_page(page, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 587 | |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 588 | return; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 591 | static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov, |
| 592 | loff_t offset, unsigned long nr_segs) |
| 593 | { |
| 594 | struct file *file = iocb->ki_filp; |
| 595 | struct inode *inode = file->f_mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 596 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 597 | struct gfs2_holder gh; |
| 598 | int rv; |
| 599 | |
| 600 | /* |
| 601 | * Shared lock, even though its write, since we do no allocation |
| 602 | * on this path. All we need change is atime. |
| 603 | */ |
| 604 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 605 | rv = gfs2_glock_nq_m_atime(1, &gh); |
| 606 | if (rv) |
| 607 | goto out; |
| 608 | |
| 609 | /* |
| 610 | * Should we return an error here? I can't see that O_DIRECT for |
| 611 | * a journaled file makes any sense. For now we'll silently fall |
| 612 | * back to buffered I/O, likewise we do the same for stuffed |
| 613 | * files since they are (a) small and (b) unaligned. |
| 614 | */ |
| 615 | if (gfs2_is_jdata(ip)) |
| 616 | goto out; |
| 617 | |
| 618 | if (gfs2_is_stuffed(ip)) |
| 619 | goto out; |
| 620 | |
| 621 | rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev, |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 622 | iov, offset, nr_segs, gfs2_get_block, |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 623 | NULL, DIO_OWN_LOCKING); |
| 624 | out: |
| 625 | gfs2_glock_dq_m(1, &gh); |
| 626 | gfs2_holder_uninit(&gh); |
| 627 | |
| 628 | return rv; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * gfs2_direct_IO |
| 633 | * |
| 634 | * This is called with a shared lock already held for the read path. |
| 635 | * Currently, no locks are held when the write path is called. |
| 636 | */ |
| 637 | static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb, |
| 638 | const struct iovec *iov, loff_t offset, |
| 639 | unsigned long nr_segs) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | { |
| 641 | struct file *file = iocb->ki_filp; |
| 642 | struct inode *inode = file->f_mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame^] | 643 | struct gfs2_inode *ip = GFS2_I(inode); |
| 644 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 645 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 646 | if (rw == WRITE) |
| 647 | return gfs2_direct_IO_write(iocb, iov, offset, nr_segs); |
| 648 | |
| 649 | if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) || |
| 650 | gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 653 | return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov, |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 654 | offset, nr_segs, gfs2_get_block, NULL, |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 655 | DIO_OWN_LOCKING); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | struct address_space_operations gfs2_file_aops = { |
| 659 | .writepage = gfs2_writepage, |
| 660 | .readpage = gfs2_readpage, |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 661 | .readpages = gfs2_readpages, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 662 | .sync_page = block_sync_page, |
| 663 | .prepare_write = gfs2_prepare_write, |
| 664 | .commit_write = gfs2_commit_write, |
| 665 | .bmap = gfs2_bmap, |
| 666 | .invalidatepage = gfs2_invalidatepage, |
| 667 | .direct_IO = gfs2_direct_IO, |
| 668 | }; |
| 669 | |